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