src/Pure/PIDE/markup.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50498 6647ba2775c1
child 50500 c94bba7906d2
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
     1 (*  Title:      Pure/PIDE/markup.ML
     2     Author:     Makarius
     3 
     4 Isabelle-specific implementation of quasi-abstract markup elements.
     5 *)
     6 
     7 signature MARKUP =
     8 sig
     9   val parse_int: string -> int
    10   val print_int: int -> string
    11   type T = string * Properties.T
    12   val empty: T
    13   val is_empty: T -> bool
    14   val properties: Properties.T -> T -> T
    15   val nameN: string
    16   val name: string -> T -> T
    17   val kindN: string
    18   val bindingN: string val binding: T
    19   val entityN: string val entity: string -> string -> T
    20   val get_entity_kind: T -> string option
    21   val defN: string
    22   val refN: string
    23   val lineN: string
    24   val offsetN: string
    25   val end_offsetN: string
    26   val fileN: string
    27   val idN: string
    28   val position_properties': string list
    29   val position_properties: string list
    30   val positionN: string val position: T
    31   val pathN: string val path: string -> T
    32   val indentN: string
    33   val blockN: string val block: int -> T
    34   val widthN: string
    35   val breakN: string val break: int -> T
    36   val fbreakN: string val fbreak: T
    37   val hiddenN: string val hidden: T
    38   val theoryN: string
    39   val classN: string
    40   val type_nameN: string
    41   val constantN: string
    42   val fixedN: string val fixed: string -> T
    43   val dynamic_factN: string val dynamic_fact: string -> T
    44   val tfreeN: string val tfree: T
    45   val tvarN: string val tvar: T
    46   val freeN: string val free: T
    47   val skolemN: string val skolem: T
    48   val boundN: string val bound: T
    49   val varN: string val var: T
    50   val numeralN: string val numeral: T
    51   val literalN: string val literal: T
    52   val delimiterN: string val delimiter: T
    53   val inner_stringN: string val inner_string: T
    54   val inner_commentN: string val inner_comment: T
    55   val token_rangeN: string val token_range: T
    56   val sortN: string val sort: T
    57   val typN: string val typ: T
    58   val termN: string val term: T
    59   val propN: string val prop: T
    60   val sortingN: string val sorting: T
    61   val typingN: string val typing: T
    62   val ML_keywordN: string val ML_keyword: T
    63   val ML_delimiterN: string val ML_delimiter: T
    64   val ML_tvarN: string val ML_tvar: T
    65   val ML_numeralN: string val ML_numeral: T
    66   val ML_charN: string val ML_char: T
    67   val ML_stringN: string val ML_string: T
    68   val ML_commentN: string val ML_comment: T
    69   val ML_defN: string
    70   val ML_openN: string
    71   val ML_structN: string
    72   val ML_typingN: string val ML_typing: T
    73   val ML_sourceN: string val ML_source: T
    74   val doc_sourceN: string val doc_source: T
    75   val antiqN: string val antiq: T
    76   val ML_antiquotationN: string
    77   val document_antiquotationN: string
    78   val document_antiquotation_optionN: string
    79   val paragraphN: string val paragraph: T
    80   val keywordN: string val keyword: T
    81   val operatorN: string val operator: T
    82   val commandN: string val command: T
    83   val stringN: string val string: T
    84   val altstringN: string val altstring: T
    85   val verbatimN: string val verbatim: T
    86   val commentN: string val comment: T
    87   val controlN: string val control: T
    88   val tokenN: string val token: Properties.T -> T
    89   val keyword1N: string val keyword1: T
    90   val keyword2N: string val keyword2: T
    91   val elapsedN: string
    92   val cpuN: string
    93   val gcN: string
    94   val timingN: string val timing: Timing.timing -> T
    95   val subgoalsN: string
    96   val proof_stateN: string val proof_state: int -> T
    97   val stateN: string val state: T
    98   val subgoalN: string val subgoal: T
    99   val graphviewN: string
   100   val sendbackN: string
   101   val paddingN: string
   102   val padding_line: string * string
   103   val dialogN: string val dialog: string -> string -> T
   104   val intensifyN: string val intensify: T
   105   val taskN: string
   106   val acceptedN: string val accepted: T
   107   val forkedN: string val forked: T
   108   val joinedN: string val joined: T
   109   val runningN: string val running: T
   110   val finishedN: string val finished: T
   111   val failedN: string val failed: T
   112   val serialN: string
   113   val initN: string
   114   val statusN: string
   115   val writelnN: string
   116   val tracingN: string
   117   val warningN: string
   118   val errorN: string
   119   val protocolN: string
   120   val legacyN: string val legacy: T
   121   val promptN: string val prompt: T
   122   val reportN: string val report: T
   123   val no_reportN: string val no_report: T
   124   val badN: string val bad: T
   125   val functionN: string
   126   val assign_execs: Properties.T
   127   val removed_versions: Properties.T
   128   val invoke_scala: string -> string -> Properties.T
   129   val cancel_scala: string -> Properties.T
   130   val ML_statistics: Properties.T
   131   val no_output: Output.output * Output.output
   132   val default_output: T -> Output.output * Output.output
   133   val add_mode: string -> (T -> Output.output * Output.output) -> unit
   134   val output: T -> Output.output * Output.output
   135   val enclose: T -> Output.output -> Output.output
   136   val markup: T -> string -> string
   137   val markup_only: T -> string
   138 end;
   139 
   140 structure Markup: MARKUP =
   141 struct
   142 
   143 (** markup elements **)
   144 
   145 (* integers *)
   146 
   147 fun parse_int s =
   148   let val i = Int.fromString s in
   149     if is_none i orelse String.isPrefix "~" s
   150     then raise Fail ("Bad integer: " ^ quote s)
   151     else the i
   152   end;
   153 
   154 val print_int = signed_string_of_int;
   155 
   156 
   157 (* basic markup *)
   158 
   159 type T = string * Properties.T;
   160 
   161 val empty = ("", []);
   162 
   163 fun is_empty ("", _) = true
   164   | is_empty _ = false;
   165 
   166 
   167 fun properties more_props ((elem, props): T) =
   168   (elem, fold_rev Properties.put more_props props);
   169 
   170 fun markup_elem elem = (elem, (elem, []): T);
   171 fun markup_string elem prop = (elem, fn s => (elem, [(prop, s)]): T);
   172 fun markup_int elem prop = (elem, fn i => (elem, [(prop, print_int i)]): T);
   173 
   174 
   175 (* misc properties *)
   176 
   177 val nameN = "name";
   178 fun name a = properties [(nameN, a)];
   179 
   180 val kindN = "kind";
   181 
   182 
   183 (* formal entities *)
   184 
   185 val (bindingN, binding) = markup_elem "binding";
   186 
   187 val entityN = "entity";
   188 fun entity kind name = (entityN, [(nameN, name), (kindN, kind)]);
   189 
   190 fun get_entity_kind (name, props) =
   191   if name = entityN then AList.lookup (op =) props kindN
   192   else NONE;
   193 
   194 val defN = "def";
   195 val refN = "ref";
   196 
   197 
   198 (* position *)
   199 
   200 val lineN = "line";
   201 val offsetN = "offset";
   202 val end_offsetN = "end_offset";
   203 val fileN = "file";
   204 val idN = "id";
   205 
   206 val position_properties' = [fileN, idN];
   207 val position_properties = [lineN, offsetN, end_offsetN] @ position_properties';
   208 
   209 val (positionN, position) = markup_elem "position";
   210 
   211 
   212 (* path *)
   213 
   214 val (pathN, path) = markup_string "path" nameN;
   215 
   216 
   217 (* pretty printing *)
   218 
   219 val indentN = "indent";
   220 val (blockN, block) = markup_int "block" indentN;
   221 
   222 val widthN = "width";
   223 val (breakN, break) = markup_int "break" widthN;
   224 
   225 val (fbreakN, fbreak) = markup_elem "fbreak";
   226 
   227 
   228 (* hidden text *)
   229 
   230 val (hiddenN, hidden) = markup_elem "hidden";
   231 
   232 
   233 (* logical entities *)
   234 
   235 val theoryN = "theory";
   236 val classN = "class";
   237 val type_nameN = "type_name";
   238 val constantN = "constant";
   239 
   240 val (fixedN, fixed) = markup_string "fixed" nameN;
   241 val (dynamic_factN, dynamic_fact) = markup_string "dynamic_fact" nameN;
   242 
   243 
   244 (* inner syntax *)
   245 
   246 val (tfreeN, tfree) = markup_elem "tfree";
   247 val (tvarN, tvar) = markup_elem "tvar";
   248 val (freeN, free) = markup_elem "free";
   249 val (skolemN, skolem) = markup_elem "skolem";
   250 val (boundN, bound) = markup_elem "bound";
   251 val (varN, var) = markup_elem "var";
   252 val (numeralN, numeral) = markup_elem "numeral";
   253 val (literalN, literal) = markup_elem "literal";
   254 val (delimiterN, delimiter) = markup_elem "delimiter";
   255 val (inner_stringN, inner_string) = markup_elem "inner_string";
   256 val (inner_commentN, inner_comment) = markup_elem "inner_comment";
   257 
   258 val (token_rangeN, token_range) = markup_elem "token_range";
   259 
   260 val (sortN, sort) = markup_elem "sort";
   261 val (typN, typ) = markup_elem "typ";
   262 val (termN, term) = markup_elem "term";
   263 val (propN, prop) = markup_elem "prop";
   264 
   265 val (sortingN, sorting) = markup_elem "sorting";
   266 val (typingN, typing) = markup_elem "typing";
   267 
   268 
   269 (* ML syntax *)
   270 
   271 val (ML_keywordN, ML_keyword) = markup_elem "ML_keyword";
   272 val (ML_delimiterN, ML_delimiter) = markup_elem "ML_delimiter";
   273 val (ML_tvarN, ML_tvar) = markup_elem "ML_tvar";
   274 val (ML_numeralN, ML_numeral) = markup_elem "ML_numeral";
   275 val (ML_charN, ML_char) = markup_elem "ML_char";
   276 val (ML_stringN, ML_string) = markup_elem "ML_string";
   277 val (ML_commentN, ML_comment) = markup_elem "ML_comment";
   278 
   279 val ML_defN = "ML_def";
   280 val ML_openN = "ML_open";
   281 val ML_structN = "ML_struct";
   282 val (ML_typingN, ML_typing) = markup_elem "ML_typing";
   283 
   284 
   285 (* embedded source text *)
   286 
   287 val (ML_sourceN, ML_source) = markup_elem "ML_source";
   288 val (doc_sourceN, doc_source) = markup_elem "doc_source";
   289 
   290 val (antiqN, antiq) = markup_elem "antiq";
   291 val ML_antiquotationN = "ML_antiquotation";
   292 val document_antiquotationN = "document_antiquotation";
   293 val document_antiquotation_optionN = "document_antiquotation_option";
   294 
   295 
   296 (* text structure *)
   297 
   298 val (paragraphN, paragraph) = markup_elem "paragraph";
   299 
   300 
   301 (* outer syntax *)
   302 
   303 val (keywordN, keyword) = markup_elem "keyword";
   304 val (operatorN, operator) = markup_elem "operator";
   305 val (commandN, command) = markup_elem "command";
   306 val (stringN, string) = markup_elem "string";
   307 val (altstringN, altstring) = markup_elem "altstring";
   308 val (verbatimN, verbatim) = markup_elem "verbatim";
   309 val (commentN, comment) = markup_elem "comment";
   310 val (controlN, control) = markup_elem "control";
   311 
   312 val tokenN = "token";
   313 fun token props = (tokenN, props);
   314 
   315 val (keyword1N, keyword1) = markup_elem "keyword1";
   316 val (keyword2N, keyword2) = markup_elem "keyword2";
   317 
   318 
   319 (* timing *)
   320 
   321 val timingN = "timing";
   322 val elapsedN = "elapsed";
   323 val cpuN = "cpu";
   324 val gcN = "gc";
   325 
   326 fun timing {elapsed, cpu, gc} =
   327   (timingN,
   328    [(elapsedN, Time.toString elapsed),
   329     (cpuN, Time.toString cpu),
   330     (gcN, Time.toString gc)]);
   331 
   332 
   333 (* toplevel *)
   334 
   335 val subgoalsN = "subgoals";
   336 val (proof_stateN, proof_state) = markup_int "proof_state" subgoalsN;
   337 
   338 val (stateN, state) = markup_elem "state";
   339 val (subgoalN, subgoal) = markup_elem "subgoal";
   340 
   341 
   342 (* active areas *)
   343 
   344 val graphviewN = "graphview";
   345 
   346 val sendbackN = "sendback";
   347 val paddingN = "padding";
   348 val padding_line = (paddingN, lineN);
   349 
   350 val dialogN = "dialog";
   351 fun dialog name result = (dialogN, [(nameN, name), ("result", result)]);
   352 
   353 val (intensifyN, intensify) = markup_elem "intensify";
   354 
   355 
   356 (* command status *)
   357 
   358 val taskN = "task";
   359 
   360 val (acceptedN, accepted) = markup_elem "accepted";
   361 val (forkedN, forked) = markup_elem "forked";
   362 val (joinedN, joined) = markup_elem "joined";
   363 val (runningN, running) = markup_elem "running";
   364 val (finishedN, finished) = markup_elem "finished";
   365 val (failedN, failed) = markup_elem "failed";
   366 
   367 
   368 (* messages *)
   369 
   370 val serialN = "serial";
   371 
   372 val initN = "init";
   373 val statusN = "status";
   374 val writelnN = "writeln";
   375 val tracingN = "tracing";
   376 val warningN = "warning";
   377 val errorN = "error";
   378 val protocolN = "protocol";
   379 
   380 val (legacyN, legacy) = markup_elem "legacy";
   381 val (promptN, prompt) = markup_elem "prompt";
   382 
   383 val (reportN, report) = markup_elem "report";
   384 val (no_reportN, no_report) = markup_elem "no_report";
   385 
   386 val (badN, bad) = markup_elem "bad";
   387 
   388 
   389 (* protocol message functions *)
   390 
   391 val functionN = "function"
   392 
   393 val assign_execs = [(functionN, "assign_execs")];
   394 val removed_versions = [(functionN, "removed_versions")];
   395 
   396 fun invoke_scala name id = [(functionN, "invoke_scala"), (nameN, name), (idN, id)];
   397 fun cancel_scala id = [(functionN, "cancel_scala"), (idN, id)];
   398 
   399 val ML_statistics = [(functionN, "ML_statistics")];
   400 
   401 
   402 
   403 (** print mode operations **)
   404 
   405 val no_output = ("", "");
   406 fun default_output (_: T) = no_output;
   407 
   408 local
   409   val default = {output = default_output};
   410   val modes = Synchronized.var "Markup.modes" (Symtab.make [("", default)]);
   411 in
   412   fun add_mode name output =
   413     Synchronized.change modes (fn tab =>
   414       (if not (Symtab.defined tab name) then ()
   415        else warning ("Redefining markup mode " ^ quote name);
   416        Symtab.update (name, {output = output}) tab));
   417   fun get_mode () =
   418     the_default default
   419       (Library.get_first (Symtab.lookup (Synchronized.value modes)) (print_mode_value ()));
   420 end;
   421 
   422 fun output m = if is_empty m then no_output else #output (get_mode ()) m;
   423 
   424 val enclose = output #-> Library.enclose;
   425 
   426 fun markup m =
   427   let val (bg, en) = output m
   428   in Library.enclose (Output.escape bg) (Output.escape en) end;
   429 
   430 fun markup_only m = markup m "";
   431 
   432 end;