src/Pure/Thy/thy_output.ML
author wenzelm
Sat Oct 17 19:47:34 2015 +0200 (2015-10-17)
changeset 61461 77c9643a6353
parent 61459 5f2ddeb15c06
child 61462 e16649b70107
permissions -rw-r--r--
more explicit output of list items;
wenzelm@22124
     1
(*  Title:      Pure/Thy/thy_output.ML
wenzelm@22124
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@22124
     3
wenzelm@30390
     4
Theory document output with antiquotations.
wenzelm@22124
     5
*)
wenzelm@22124
     6
wenzelm@22124
     7
signature THY_OUTPUT =
wenzelm@22124
     8
sig
wenzelm@38767
     9
  val display: bool Config.T
wenzelm@38767
    10
  val quotes: bool Config.T
wenzelm@59175
    11
  val margin: int Config.T
wenzelm@38767
    12
  val indent: int Config.T
wenzelm@38767
    13
  val source: bool Config.T
wenzelm@38767
    14
  val break: bool Config.T
wenzelm@52042
    15
  val modes: string Config.T
wenzelm@38766
    16
  val add_wrapper: ((unit -> string) -> unit -> string) -> Proof.context -> Proof.context
wenzelm@43564
    17
  val add_option: binding -> (string -> Proof.context -> Proof.context) -> theory -> theory
wenzelm@55743
    18
  val check_command: Proof.context -> xstring * Position.T -> string
wenzelm@55743
    19
  val check_option: Proof.context -> xstring * Position.T -> string
wenzelm@59917
    20
  val print_antiquotations: bool -> Proof.context -> unit
wenzelm@43564
    21
  val antiquotation: binding -> 'a context_parser ->
wenzelm@58011
    22
    ({source: Token.src, state: Toplevel.state, context: Proof.context} -> 'a -> string) ->
wenzelm@43564
    23
      theory -> theory
wenzelm@22124
    24
  val boolean: string -> bool
wenzelm@22124
    25
  val integer: string -> int
wenzelm@61456
    26
  val eval_antiquote: Toplevel.state -> Antiquote.text_antiquote -> string
wenzelm@61457
    27
  val output_text: Toplevel.state -> {markdown: bool} -> Input.source -> string
wenzelm@58928
    28
  val present_thy: theory -> (Toplevel.transition * Toplevel.state) list -> Token.T list -> Buffer.T
wenzelm@38767
    29
  val pretty_text: Proof.context -> string -> Pretty.T
wenzelm@28644
    30
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@28644
    31
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@58011
    32
  val str_of_source: Token.src -> string
wenzelm@38767
    33
  val maybe_pretty_source: (Proof.context -> 'a -> Pretty.T) -> Proof.context ->
wenzelm@58011
    34
    Token.src -> 'a list -> Pretty.T list
wenzelm@59175
    35
  val string_of_margin: Proof.context -> Pretty.T -> string
wenzelm@38767
    36
  val output: Proof.context -> Pretty.T list -> string
wenzelm@58716
    37
  val verbatim_text: Proof.context -> string -> string
wenzelm@59064
    38
  val old_header_command: Input.source -> Toplevel.transition -> Toplevel.transition
wenzelm@61457
    39
  val document_command: {markdown: bool} -> (xstring * Position.T) option * Input.source ->
wenzelm@58928
    40
    Toplevel.transition -> Toplevel.transition
wenzelm@22124
    41
end;
wenzelm@22124
    42
wenzelm@37216
    43
structure Thy_Output: THY_OUTPUT =
wenzelm@22124
    44
struct
wenzelm@22124
    45
wenzelm@52042
    46
(** options **)
wenzelm@22124
    47
wenzelm@56438
    48
val display = Attrib.setup_option_bool ("thy_output_display", @{here});
wenzelm@56438
    49
val break = Attrib.setup_option_bool ("thy_output_break", @{here});
wenzelm@56438
    50
val quotes = Attrib.setup_option_bool ("thy_output_quotes", @{here});
wenzelm@59175
    51
val margin = Attrib.setup_option_int ("thy_output_margin", @{here});
wenzelm@56438
    52
val indent = Attrib.setup_option_int ("thy_output_indent", @{here});
wenzelm@56438
    53
val source = Attrib.setup_option_bool ("thy_output_source", @{here});
wenzelm@56438
    54
val modes = Attrib.setup_option_string ("thy_output_modes", @{here});
wenzelm@22124
    55
wenzelm@22124
    56
wenzelm@38766
    57
structure Wrappers = Proof_Data
wenzelm@38766
    58
(
wenzelm@38766
    59
  type T = ((unit -> string) -> unit -> string) list;
wenzelm@38766
    60
  fun init _ = [];
wenzelm@38766
    61
);
wenzelm@38766
    62
wenzelm@38766
    63
fun add_wrapper wrapper = Wrappers.map (cons wrapper);
wenzelm@38766
    64
wenzelm@38766
    65
val wrap = Wrappers.get #> fold (fn wrapper => fn f => wrapper f);
wenzelm@38766
    66
wenzelm@38766
    67
wenzelm@22124
    68
wenzelm@30390
    69
(** maintain global antiquotations **)
wenzelm@22124
    70
wenzelm@43564
    71
structure Antiquotations = Theory_Data
wenzelm@43564
    72
(
wenzelm@43564
    73
  type T =
wenzelm@58011
    74
    (Token.src -> Toplevel.state -> Proof.context -> string) Name_Space.table *
wenzelm@43564
    75
      (string -> Proof.context -> Proof.context) Name_Space.table;
wenzelm@43564
    76
  val empty : T =
wenzelm@50201
    77
    (Name_Space.empty_table Markup.document_antiquotationN,
wenzelm@50201
    78
      Name_Space.empty_table Markup.document_antiquotation_optionN);
wenzelm@43564
    79
  val extend = I;
wenzelm@43564
    80
  fun merge ((commands1, options1), (commands2, options2)) : T =
wenzelm@43564
    81
    (Name_Space.merge_tables (commands1, commands2),
wenzelm@43564
    82
      Name_Space.merge_tables (options1, options2));
wenzelm@43564
    83
);
wenzelm@22124
    84
wenzelm@55743
    85
val get_antiquotations = Antiquotations.get o Proof_Context.theory_of;
wenzelm@55743
    86
wenzelm@47005
    87
fun add_command name cmd thy = thy
wenzelm@47005
    88
  |> Antiquotations.map (apfst (Name_Space.define (Context.Theory thy) true (name, cmd) #> snd));
wenzelm@22124
    89
wenzelm@47005
    90
fun add_option name opt thy = thy
wenzelm@47005
    91
  |> Antiquotations.map (apsnd (Name_Space.define (Context.Theory thy) true (name, opt) #> snd));
wenzelm@22124
    92
wenzelm@55743
    93
fun check_command ctxt = #1 o Name_Space.check (Context.Proof ctxt) (#1 (get_antiquotations ctxt));
wenzelm@26893
    94
wenzelm@55743
    95
fun check_option ctxt = #1 o Name_Space.check (Context.Proof ctxt) (#2 (get_antiquotations ctxt));
wenzelm@43564
    96
wenzelm@43564
    97
fun command src state ctxt =
wenzelm@58011
    98
  let val (src', f) = Token.check_src ctxt (#1 (get_antiquotations ctxt)) src
wenzelm@56029
    99
  in f src' state ctxt end;
wenzelm@22124
   100
wenzelm@43564
   101
fun option ((xname, pos), s) ctxt =
wenzelm@43564
   102
  let
wenzelm@55743
   103
    val (_, opt) =
wenzelm@55743
   104
      Name_Space.check (Context.Proof ctxt) (#2 (get_antiquotations ctxt)) (xname, pos);
wenzelm@43564
   105
  in opt s ctxt end;
wenzelm@22124
   106
wenzelm@59917
   107
fun print_antiquotations verbose ctxt =
wenzelm@43564
   108
  let
wenzelm@55743
   109
    val (commands, options) = get_antiquotations ctxt;
wenzelm@59917
   110
    val command_names = map #1 (Name_Space.markup_table verbose ctxt commands);
wenzelm@59917
   111
    val option_names = map #1 (Name_Space.markup_table verbose ctxt options);
wenzelm@43564
   112
  in
wenzelm@50301
   113
    [Pretty.big_list "document antiquotations:" (map Pretty.mark_str command_names),
wenzelm@59175
   114
     Pretty.big_list "document antiquotation options:" (map Pretty.mark_str option_names)]
wenzelm@59175
   115
  end |> Pretty.writeln_chunks;
wenzelm@22124
   116
wenzelm@56069
   117
fun antiquotation name scan body =
wenzelm@38766
   118
  add_command name
wenzelm@38766
   119
    (fn src => fn state => fn ctxt =>
wenzelm@58011
   120
      let val (x, ctxt') = Token.syntax scan src ctxt
wenzelm@56069
   121
      in body {source = src, state = state, context = ctxt'} x end);
wenzelm@30390
   122
wenzelm@22124
   123
wenzelm@22124
   124
wenzelm@22124
   125
(** syntax of antiquotations **)
wenzelm@22124
   126
wenzelm@22124
   127
(* option values *)
wenzelm@22124
   128
wenzelm@22124
   129
fun boolean "" = true
wenzelm@22124
   130
  | boolean "true" = true
wenzelm@22124
   131
  | boolean "false" = false
wenzelm@22124
   132
  | boolean s = error ("Bad boolean value: " ^ quote s);
wenzelm@22124
   133
wenzelm@22124
   134
fun integer s =
wenzelm@22124
   135
  let
wenzelm@22124
   136
    fun int ss =
wenzelm@22124
   137
      (case Library.read_int ss of (i, []) => i
wenzelm@22124
   138
      | _ => error ("Bad integer value: " ^ quote s));
wenzelm@22124
   139
  in (case Symbol.explode s of "-" :: ss => ~ (int ss) | ss => int ss) end;
wenzelm@22124
   140
wenzelm@22124
   141
wenzelm@22124
   142
(* outer syntax *)
wenzelm@22124
   143
wenzelm@22124
   144
local
wenzelm@22124
   145
wenzelm@36950
   146
val property =
wenzelm@43564
   147
  Parse.position Parse.xname -- Scan.optional (Parse.$$$ "=" |-- Parse.!!! Parse.xname) "";
wenzelm@36950
   148
wenzelm@36950
   149
val properties =
wenzelm@36950
   150
  Scan.optional (Parse.$$$ "[" |-- Parse.!!! (Parse.enum "," property --| Parse.$$$ "]")) [];
wenzelm@22124
   151
wenzelm@22124
   152
in
wenzelm@22124
   153
wenzelm@36950
   154
val antiq =
wenzelm@40800
   155
  Parse.!!!
wenzelm@56201
   156
    (Parse.position Parse.liberal_name -- properties -- Parse.args --| Scan.ahead Parse.eof)
wenzelm@58011
   157
  >> (fn ((name, props), args) => (props, Token.src name args));
wenzelm@22124
   158
wenzelm@22124
   159
end;
wenzelm@22124
   160
wenzelm@22124
   161
wenzelm@61457
   162
(* eval antiquote *)
wenzelm@58928
   163
wenzelm@61456
   164
fun eval_antiquote _ (Antiquote.Text ss) = Symbol_Pos.content ss
wenzelm@61456
   165
  | eval_antiquote state (Antiquote.Antiq (ss, {range = (pos, _), ...})) =
wenzelm@61456
   166
      let
wenzelm@61456
   167
        val keywords =
wenzelm@61456
   168
          (case try Toplevel.presentation_context_of state of
wenzelm@61456
   169
            SOME ctxt => Thy_Header.get_keywords' ctxt
wenzelm@61456
   170
          | NONE =>
wenzelm@61456
   171
              error ("Unknown context -- cannot expand document antiquotations" ^
wenzelm@61456
   172
                Position.here pos));
wenzelm@58928
   173
wenzelm@61456
   174
        val (opts, src) = Token.read_antiq keywords antiq (ss, pos);
wenzelm@61456
   175
        fun cmd ctxt = wrap ctxt (fn () => command src state ctxt) ();
wenzelm@61456
   176
wenzelm@61456
   177
        val preview_ctxt = fold option opts (Toplevel.presentation_context_of state);
wenzelm@61456
   178
        val print_ctxt = Context_Position.set_visible false preview_ctxt;
wenzelm@61456
   179
        val _ = cmd preview_ctxt;
wenzelm@61456
   180
        val print_modes = space_explode "," (Config.get print_ctxt modes) @ Latex.modes;
wenzelm@61456
   181
      in Print_Mode.with_modes print_modes (fn () => cmd print_ctxt) () end;
wenzelm@42508
   182
wenzelm@56548
   183
wenzelm@61457
   184
(* output text *)
wenzelm@56548
   185
wenzelm@61457
   186
fun output_text state {markdown} source =
wenzelm@61457
   187
  let
wenzelm@61457
   188
    val pos = Input.pos_of source;
wenzelm@61457
   189
    val _ = Position.report pos (Markup.language_document (Input.is_delimited source));
wenzelm@61457
   190
    val syms = Input.source_explode source;
wenzelm@61457
   191
wenzelm@61457
   192
    val output_antiquote = eval_antiquote state #> Symbol.explode #> Latex.output_ctrl_symbols;
wenzelm@61457
   193
    val output_antiquotes = map output_antiquote #> implode;
wenzelm@61036
   194
wenzelm@61461
   195
    fun output_line line =
wenzelm@61461
   196
      (if Markdown.line_is_item line then "\\item " else "") ^
wenzelm@61461
   197
        output_antiquotes (Markdown.line_content line);
wenzelm@61461
   198
wenzelm@61457
   199
    fun output_blocks blocks = space_implode "\n\n" (map output_block blocks)
wenzelm@61461
   200
    and output_block (Markdown.Paragraph lines) = cat_lines (map output_line lines)
wenzelm@61459
   201
      | output_block (Markdown.List {kind, body, ...}) =
wenzelm@61459
   202
          let val env = Markdown.print_kind kind in
wenzelm@61457
   203
            "%\n\\begin{" ^ env ^ "}%\n" ^
wenzelm@61457
   204
            output_blocks body ^
wenzelm@61457
   205
            "%\n\\end{" ^ env ^ "}%\n"
wenzelm@61457
   206
          end;
wenzelm@61457
   207
  in
wenzelm@61457
   208
    if Toplevel.is_skipped_proof state then ""
wenzelm@61457
   209
    else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms
wenzelm@61457
   210
    then
wenzelm@61457
   211
      let
wenzelm@61457
   212
        val ants = Antiquote.read' pos syms;
wenzelm@61457
   213
        val blocks = Markdown.read_antiquotes ants;
wenzelm@61457
   214
        val _ = Position.reports (Markdown.reports blocks);
wenzelm@61457
   215
      in output_blocks blocks end
wenzelm@61457
   216
    else
wenzelm@61457
   217
      let
wenzelm@61457
   218
        val ants = Antiquote.read' pos (Symbol_Pos.trim_blanks syms);
wenzelm@61457
   219
        val _ = Position.reports (Markdown.text_reports ants);
wenzelm@61457
   220
      in output_antiquotes ants end
wenzelm@61457
   221
  end;
wenzelm@48918
   222
wenzelm@48918
   223
wenzelm@22124
   224
wenzelm@22124
   225
(** present theory source **)
wenzelm@22124
   226
wenzelm@59924
   227
(*NB: arranging white space around command spans is a black art*)
wenzelm@22124
   228
wenzelm@22124
   229
(* presentation tokens *)
wenzelm@22124
   230
wenzelm@22124
   231
datatype token =
wenzelm@59065
   232
    No_Token
wenzelm@59065
   233
  | Basic_Token of Token.T
wenzelm@59065
   234
  | Markup_Token of string * Input.source
wenzelm@59065
   235
  | Markup_Env_Token of string * Input.source
wenzelm@61456
   236
  | Raw_Token of Input.source;
wenzelm@22124
   237
wenzelm@22124
   238
wenzelm@59065
   239
fun basic_token pred (Basic_Token tok) = pred tok
wenzelm@22124
   240
  | basic_token _ _ = false;
wenzelm@22124
   241
wenzelm@51267
   242
val improper_token = basic_token Token.is_improper;
wenzelm@36959
   243
val comment_token = basic_token Token.is_comment;
wenzelm@36959
   244
val blank_token = basic_token Token.is_blank;
wenzelm@36959
   245
val newline_token = basic_token Token.is_newline;
wenzelm@22124
   246
wenzelm@22124
   247
wenzelm@61455
   248
(* output token *)
wenzelm@61455
   249
wenzelm@61457
   250
fun output_token state tok =
wenzelm@61457
   251
  (case tok of
wenzelm@61457
   252
    No_Token => ""
wenzelm@61457
   253
  | Basic_Token tok => Latex.output_token tok
wenzelm@61457
   254
  | Markup_Token (cmd, source) =>
wenzelm@61457
   255
      "%\n\\isamarkup" ^ cmd ^ "{" ^ output_text state {markdown = false} source ^ "%\n}\n"
wenzelm@61457
   256
  | Markup_Env_Token (cmd, source) =>
wenzelm@61457
   257
      "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@61457
   258
      output_text state {markdown = true} source ^
wenzelm@61457
   259
      "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
wenzelm@61457
   260
  | Raw_Token source =>
wenzelm@61457
   261
      "%\n" ^ output_text state {markdown = true} source ^ "\n");
wenzelm@61455
   262
wenzelm@61455
   263
wenzelm@22124
   264
(* command spans *)
wenzelm@22124
   265
wenzelm@22124
   266
type command = string * Position.T * string list;   (*name, position, tags*)
wenzelm@22124
   267
type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
wenzelm@22124
   268
wenzelm@22124
   269
datatype span = Span of command * (source * source * source * source) * bool;
wenzelm@22124
   270
wenzelm@22124
   271
fun make_span cmd src =
wenzelm@22124
   272
  let
wenzelm@22124
   273
    fun take_newline (tok :: toks) =
wenzelm@22124
   274
          if newline_token (fst tok) then ([tok], toks, true)
wenzelm@22124
   275
          else ([], tok :: toks, false)
wenzelm@22124
   276
      | take_newline [] = ([], [], false);
wenzelm@22124
   277
    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
wenzelm@22124
   278
      src
wenzelm@22124
   279
      |> take_prefix (improper_token o fst)
wenzelm@22124
   280
      ||>> take_suffix (improper_token o fst)
wenzelm@22124
   281
      ||>> take_prefix (comment_token o fst)
wenzelm@22124
   282
      ||> take_newline;
wenzelm@22124
   283
  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
wenzelm@22124
   284
wenzelm@22124
   285
wenzelm@22124
   286
(* present spans *)
wenzelm@22124
   287
wenzelm@22124
   288
local
wenzelm@22124
   289
wenzelm@22124
   290
fun err_bad_nesting pos =
wenzelm@22124
   291
  error ("Bad nesting of commands in presentation" ^ pos);
wenzelm@22124
   292
wenzelm@22124
   293
fun edge which f (x: string option, y) =
wenzelm@22124
   294
  if x = y then I
wenzelm@22124
   295
  else (case which (x, y) of NONE => I | SOME txt => Buffer.add (f txt));
wenzelm@22124
   296
wenzelm@22124
   297
val begin_tag = edge #2 Latex.begin_tag;
wenzelm@22124
   298
val end_tag = edge #1 Latex.end_tag;
wenzelm@22124
   299
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
wenzelm@22124
   300
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
wenzelm@22124
   301
wenzelm@22124
   302
in
wenzelm@22124
   303
wenzelm@58923
   304
fun present_span keywords span state state' (tag_stack, active_tag, newline, buffer, present_cont) =
wenzelm@22124
   305
  let
wenzelm@22124
   306
    val present = fold (fn (tok, (flag, 0)) =>
wenzelm@58928
   307
        Buffer.add (output_token state' tok)
wenzelm@22124
   308
        #> Buffer.add flag
wenzelm@22124
   309
      | _ => I);
wenzelm@22124
   310
wenzelm@22124
   311
    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
wenzelm@22124
   312
wenzelm@22124
   313
    val (tag, tags) = tag_stack;
wenzelm@46924
   314
    val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag));
wenzelm@22124
   315
wenzelm@58999
   316
    val nesting = Toplevel.level state' - Toplevel.level state;
wenzelm@58999
   317
wenzelm@22124
   318
    val active_tag' =
wenzelm@22124
   319
      if is_some tag' then tag'
wenzelm@22124
   320
      else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
wenzelm@58999
   321
      else
wenzelm@58999
   322
        (case Keyword.command_tags keywords cmd_name of
wenzelm@58999
   323
          default_tag :: _ => SOME default_tag
wenzelm@58999
   324
        | [] =>
wenzelm@58999
   325
            if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state
wenzelm@58999
   326
            then active_tag
wenzelm@58999
   327
            else NONE);
wenzelm@58999
   328
wenzelm@22124
   329
    val edge = (active_tag, active_tag');
wenzelm@22124
   330
wenzelm@22124
   331
    val newline' =
wenzelm@22124
   332
      if is_none active_tag' then span_newline else newline;
wenzelm@22124
   333
wenzelm@22124
   334
    val tag_stack' =
wenzelm@22124
   335
      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
wenzelm@22124
   336
      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
wenzelm@22124
   337
      else
haftmann@33957
   338
        (case drop (~ nesting - 1) tags of
wenzelm@58999
   339
          tg :: tgs => (tg, tgs)
wenzelm@48992
   340
        | [] => err_bad_nesting (Position.here cmd_pos));
wenzelm@22124
   341
wenzelm@22124
   342
    val buffer' =
wenzelm@22124
   343
      buffer
wenzelm@22124
   344
      |> end_tag edge
wenzelm@22124
   345
      |> close_delim (fst present_cont) edge
wenzelm@22124
   346
      |> snd present_cont
wenzelm@22124
   347
      |> open_delim (present (#1 srcs)) edge
wenzelm@22124
   348
      |> begin_tag edge
wenzelm@22124
   349
      |> present (#2 srcs);
wenzelm@22124
   350
    val present_cont' =
wenzelm@22124
   351
      if newline then (present (#3 srcs), present (#4 srcs))
wenzelm@22124
   352
      else (I, present (#3 srcs) #> present (#4 srcs));
wenzelm@22124
   353
  in (tag_stack', active_tag', newline', buffer', present_cont') end;
wenzelm@22124
   354
wenzelm@22124
   355
fun present_trailer ((_, tags), active_tag, _, buffer, present_cont) =
wenzelm@22124
   356
  if not (null tags) then err_bad_nesting " at end of theory"
wenzelm@22124
   357
  else
wenzelm@22124
   358
    buffer
wenzelm@22124
   359
    |> end_tag (active_tag, NONE)
wenzelm@22124
   360
    |> close_delim (fst present_cont) (active_tag, NONE)
wenzelm@22124
   361
    |> snd present_cont;
wenzelm@22124
   362
wenzelm@22124
   363
end;
wenzelm@22124
   364
wenzelm@22124
   365
wenzelm@28427
   366
(* present_thy *)
wenzelm@22124
   367
wenzelm@22124
   368
local
wenzelm@22124
   369
wenzelm@61455
   370
val markup_true = "\\isamarkuptrue%\n";
wenzelm@61455
   371
val markup_false = "\\isamarkupfalse%\n";
wenzelm@61455
   372
wenzelm@22124
   373
val space_proper =
wenzelm@36959
   374
  Scan.one Token.is_blank -- Scan.many Token.is_comment -- Scan.one Token.is_proper;
wenzelm@22124
   375
wenzelm@36959
   376
val is_improper = not o (Token.is_proper orf Token.is_begin_ignore orf Token.is_end_ignore);
wenzelm@22124
   377
val improper = Scan.many is_improper;
wenzelm@22124
   378
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
wenzelm@36959
   379
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank));
wenzelm@22124
   380
wenzelm@36959
   381
val opt_newline = Scan.option (Scan.one Token.is_newline);
wenzelm@22124
   382
wenzelm@22124
   383
val ignore =
wenzelm@36959
   384
  Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore
wenzelm@22124
   385
    >> pair (d + 1)) ||
wenzelm@36959
   386
  Scan.depend (fn d => Scan.one Token.is_end_ignore --|
wenzelm@43947
   387
    (if d = 0 then Scan.fail_with (K (fn () => "Bad nesting of meta-comments")) else opt_newline)
wenzelm@22124
   388
    >> pair (d - 1));
wenzelm@22124
   389
wenzelm@36950
   390
val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end);
wenzelm@22124
   391
wenzelm@22124
   392
val locale =
wenzelm@36950
   393
  Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |--
wenzelm@36950
   394
    Parse.!!! (improper |-- Parse.xname --| (improper -- Parse.$$$ ")")));
wenzelm@22124
   395
wenzelm@22124
   396
in
wenzelm@22124
   397
wenzelm@58928
   398
fun present_thy thy command_results toks =
wenzelm@22124
   399
  let
wenzelm@58928
   400
    val keywords = Thy_Header.get_keywords thy;
wenzelm@58928
   401
wenzelm@58928
   402
wenzelm@22124
   403
    (* tokens *)
wenzelm@22124
   404
wenzelm@22124
   405
    val ignored = Scan.state --| ignore
wenzelm@59065
   406
      >> (fn d => (NONE, (No_Token, ("", d))));
wenzelm@22124
   407
wenzelm@58999
   408
    fun markup pred mk flag = Scan.peek (fn d =>
wenzelm@36950
   409
      improper |--
wenzelm@58999
   410
        Parse.position (Scan.one (fn tok =>
wenzelm@58999
   411
          Token.is_command tok andalso pred keywords (Token.content_of tok))) --
wenzelm@22124
   412
      Scan.repeat tag --
wenzelm@51627
   413
      Parse.!!!! ((improper -- locale -- improper) |-- Parse.document_source --| improper_end)
wenzelm@59064
   414
      >> (fn (((tok, pos'), tags), source) =>
wenzelm@59065
   415
        let val name = Token.content_of tok
wenzelm@59065
   416
        in (SOME (name, pos', tags), (mk (name, source), (flag, d))) end));
wenzelm@22124
   417
wenzelm@22124
   418
    val command = Scan.peek (fn d =>
wenzelm@59939
   419
      Scan.optional (Scan.one Token.is_command_modifier -- improper >> op ::) [] --
wenzelm@59924
   420
      Scan.one Token.is_command -- Scan.repeat tag
wenzelm@59939
   421
      >> (fn ((cmd_mod, cmd), tags) =>
wenzelm@59939
   422
        map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @
wenzelm@59924
   423
          [(SOME (Token.content_of cmd, Token.pos_of cmd, tags),
wenzelm@61455
   424
            (Basic_Token cmd, (markup_false, d)))]));
wenzelm@22124
   425
wenzelm@22124
   426
    val cmt = Scan.peek (fn d =>
wenzelm@59065
   427
      Parse.$$$ "--" |-- Parse.!!!! (improper |-- Parse.document_source) >>
wenzelm@59065
   428
        (fn source => (NONE, (Markup_Token ("cmt", source), ("", d)))));
wenzelm@22124
   429
wenzelm@22124
   430
    val other = Scan.peek (fn d =>
wenzelm@59065
   431
       Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
wenzelm@22124
   432
wenzelm@59924
   433
    val tokens =
wenzelm@59924
   434
      (ignored ||
wenzelm@61455
   435
        markup Keyword.is_document_heading Markup_Token markup_true ||
wenzelm@61455
   436
        markup Keyword.is_document_body Markup_Env_Token markup_true ||
wenzelm@61456
   437
        markup Keyword.is_document_raw (Raw_Token o #2) "") >> single ||
wenzelm@59924
   438
      command ||
wenzelm@59924
   439
      (cmt || other) >> single;
wenzelm@22124
   440
wenzelm@22124
   441
wenzelm@22124
   442
    (* spans *)
wenzelm@22124
   443
wenzelm@59065
   444
    val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false;
wenzelm@59065
   445
    val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof;
wenzelm@22124
   446
wenzelm@22124
   447
    val cmd = Scan.one (is_some o fst);
wenzelm@27732
   448
    val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
wenzelm@22124
   449
wenzelm@22124
   450
    val comments = Scan.many (comment_token o fst o snd);
wenzelm@22124
   451
    val blank = Scan.one (blank_token o fst o snd);
wenzelm@22124
   452
    val newline = Scan.one (newline_token o fst o snd);
wenzelm@22124
   453
    val before_cmd =
wenzelm@22124
   454
      Scan.option (newline -- comments) --
wenzelm@22124
   455
      Scan.option (newline -- comments) --
wenzelm@22124
   456
      Scan.option (blank -- comments) -- cmd;
wenzelm@22124
   457
wenzelm@22124
   458
    val span =
wenzelm@22124
   459
      Scan.repeat non_cmd -- cmd --
wenzelm@22124
   460
        Scan.repeat (Scan.unless before_cmd non_cmd) --
wenzelm@22124
   461
        Scan.option (newline >> (single o snd))
wenzelm@22124
   462
      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
wenzelm@22124
   463
          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
wenzelm@22124
   464
wenzelm@57080
   465
    val spans = toks
wenzelm@57080
   466
      |> take_suffix Token.is_space |> #1
wenzelm@57080
   467
      |> Source.of_list
wenzelm@59924
   468
      |> Source.source' 0 Token.stopper (Scan.error (Scan.bulk tokens >> flat))
wenzelm@58864
   469
      |> Source.source stopper (Scan.error (Scan.bulk span))
wenzelm@22124
   470
      |> Source.exhaust;
wenzelm@28427
   471
wenzelm@28427
   472
wenzelm@28427
   473
    (* present commands *)
wenzelm@28427
   474
wenzelm@28427
   475
    fun present_command tr span st st' =
wenzelm@58923
   476
      Toplevel.setmp_thread_position tr (present_span keywords span st st');
wenzelm@28427
   477
wenzelm@28427
   478
    fun present _ [] = I
wenzelm@28427
   479
      | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest;
wenzelm@22124
   480
  in
wenzelm@28427
   481
    if length command_results = length spans then
wenzelm@22124
   482
      ((NONE, []), NONE, true, Buffer.empty, (I, I))
wenzelm@28427
   483
      |> present Toplevel.toplevel (command_results ~~ spans)
wenzelm@22124
   484
      |> present_trailer
wenzelm@22124
   485
    else error "Messed-up outer syntax for presentation"
wenzelm@22124
   486
  end;
wenzelm@22124
   487
wenzelm@22124
   488
end;
wenzelm@22124
   489
wenzelm@22124
   490
wenzelm@22124
   491
wenzelm@22124
   492
(** setup default output **)
wenzelm@22124
   493
wenzelm@22124
   494
(* options *)
wenzelm@22124
   495
wenzelm@53171
   496
val _ = Theory.setup
wenzelm@56204
   497
 (add_option @{binding show_types} (Config.put show_types o boolean) #>
wenzelm@56204
   498
  add_option @{binding show_sorts} (Config.put show_sorts o boolean) #>
wenzelm@56204
   499
  add_option @{binding show_structs} (Config.put show_structs o boolean) #>
wenzelm@56204
   500
  add_option @{binding show_question_marks} (Config.put show_question_marks o boolean) #>
wenzelm@56204
   501
  add_option @{binding show_abbrevs} (Config.put show_abbrevs o boolean) #>
wenzelm@56204
   502
  add_option @{binding names_long} (Config.put Name_Space.names_long o boolean) #>
wenzelm@56204
   503
  add_option @{binding names_short} (Config.put Name_Space.names_short o boolean) #>
wenzelm@56204
   504
  add_option @{binding names_unique} (Config.put Name_Space.names_unique o boolean) #>
wenzelm@56204
   505
  add_option @{binding eta_contract} (Config.put Syntax_Trans.eta_contract o boolean) #>
wenzelm@56204
   506
  add_option @{binding display} (Config.put display o boolean) #>
wenzelm@56204
   507
  add_option @{binding break} (Config.put break o boolean) #>
wenzelm@56204
   508
  add_option @{binding quotes} (Config.put quotes o boolean) #>
wenzelm@56204
   509
  add_option @{binding mode} (add_wrapper o Print_Mode.with_modes o single) #>
wenzelm@59175
   510
  add_option @{binding margin} (Config.put margin o integer) #>
wenzelm@56204
   511
  add_option @{binding indent} (Config.put indent o integer) #>
wenzelm@56204
   512
  add_option @{binding source} (Config.put source o boolean) #>
wenzelm@56204
   513
  add_option @{binding goals_limit} (Config.put Goal_Display.goals_limit o integer));
wenzelm@22124
   514
wenzelm@22124
   515
wenzelm@22124
   516
(* basic pretty printing *)
wenzelm@22124
   517
wenzelm@61435
   518
fun perhaps_trim ctxt =
wenzelm@61435
   519
  not (Config.get ctxt display) ? Symbol.trim_blanks;
wenzelm@22124
   520
wenzelm@38767
   521
fun pretty_text ctxt =
wenzelm@61435
   522
  Pretty.chunks o map Pretty.str o map (perhaps_trim ctxt) o split_lines;
wenzelm@22124
   523
wenzelm@55829
   524
fun pretty_text_report ctxt source =
wenzelm@59066
   525
 (Context_Position.report ctxt (Input.pos_of source)
wenzelm@59066
   526
    (Markup.language_text (Input.is_delimited source));
wenzelm@59066
   527
  pretty_text ctxt (Input.source_content source));
wenzelm@55561
   528
wenzelm@26710
   529
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@24920
   530
haftmann@32898
   531
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
haftmann@32898
   532
haftmann@32898
   533
fun pretty_term_style ctxt (style, t) =
haftmann@32898
   534
  pretty_term ctxt (style t);
wenzelm@22124
   535
haftmann@32898
   536
fun pretty_thm_style ctxt (style, th) =
haftmann@32898
   537
  pretty_term ctxt (style (Thm.full_prop_of th));
wenzelm@22124
   538
haftmann@32898
   539
fun pretty_term_typ ctxt (style, t) =
haftmann@32898
   540
  let val t' = style t
wenzelm@39288
   541
  in pretty_term ctxt (Type.constraint (Term.fastype_of t') t') end;
haftmann@32898
   542
haftmann@32898
   543
fun pretty_term_typeof ctxt (style, t) =
haftmann@32898
   544
  Syntax.pretty_typ ctxt (Term.fastype_of (style t));
wenzelm@22124
   545
wenzelm@25373
   546
fun pretty_const ctxt c =
wenzelm@25373
   547
  let
wenzelm@42360
   548
    val t = Const (c, Consts.type_scheme (Proof_Context.consts_of ctxt) c)
wenzelm@25373
   549
      handle TYPE (msg, _, _) => error msg;
wenzelm@25373
   550
    val ([t'], _) = Variable.import_terms true [t] ctxt;
wenzelm@25373
   551
  in pretty_term ctxt t' end;
wenzelm@22124
   552
wenzelm@25407
   553
fun pretty_abbrev ctxt s =
wenzelm@22124
   554
  let
wenzelm@42399
   555
    val t = Syntax.read_term (Proof_Context.set_mode Proof_Context.mode_abbrev ctxt) s;
wenzelm@24920
   556
    fun err () = error ("Abbreviated constant expected: " ^ Syntax.string_of_term ctxt t);
wenzelm@22124
   557
    val (head, args) = Term.strip_comb t;
wenzelm@22124
   558
    val (c, T) = Term.dest_Const head handle TERM _ => err ();
wenzelm@42360
   559
    val (U, u) = Consts.the_abbreviation (Proof_Context.consts_of ctxt) c
wenzelm@22124
   560
      handle TYPE _ => err ();
wenzelm@22124
   561
    val t' = Term.betapplys (Envir.expand_atom T (U, u), args);
haftmann@32898
   562
    val eq = Logic.mk_equals (t, t');
haftmann@32898
   563
    val ctxt' = Variable.auto_fixes eq ctxt;
wenzelm@42360
   564
  in Proof_Context.pretty_term_abbrev ctxt' eq end;
wenzelm@22124
   565
haftmann@39305
   566
fun pretty_class ctxt =
wenzelm@42360
   567
  Pretty.str o Proof_Context.extern_class ctxt o Proof_Context.read_class ctxt;
haftmann@39305
   568
haftmann@39309
   569
fun pretty_type ctxt s =
wenzelm@56002
   570
  let val Type (name, _) = Proof_Context.read_type_name {proper = true, strict = false} ctxt s
wenzelm@42360
   571
  in Pretty.str (Proof_Context.extern_type ctxt name) end;
haftmann@39305
   572
wenzelm@33388
   573
fun pretty_prf full ctxt = Proof_Syntax.pretty_proof_of ctxt full;
wenzelm@22124
   574
wenzelm@60100
   575
fun pretty_theory ctxt (name, pos) = (Theory.check ctxt (name, pos); Pretty.str name);
wenzelm@22124
   576
wenzelm@22124
   577
wenzelm@30390
   578
(* default output *)
wenzelm@30390
   579
wenzelm@58011
   580
val str_of_source = space_implode " " o Token.unparse_src;
wenzelm@22124
   581
wenzelm@38767
   582
fun maybe_pretty_source pretty ctxt src xs =
wenzelm@38767
   583
  map (pretty ctxt) xs  (*always pretty in order to exhibit errors!*)
wenzelm@38767
   584
  |> (if Config.get ctxt source then K [pretty_text ctxt (str_of_source src)] else I);
wenzelm@30390
   585
wenzelm@59175
   586
fun string_of_margin ctxt = Pretty.string_of_margin (Config.get ctxt margin);
wenzelm@59175
   587
wenzelm@38767
   588
fun output ctxt prts =
wenzelm@30390
   589
  prts
wenzelm@58716
   590
  |> Config.get ctxt quotes ? map Pretty.quote
wenzelm@38767
   591
  |> (if Config.get ctxt display then
wenzelm@59175
   592
        map (Pretty.indent (Config.get ctxt indent) #> string_of_margin ctxt #> Output.output)
wenzelm@58716
   593
        #> space_implode "\\isasep\\isanewline%\n"
wenzelm@58716
   594
        #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@58716
   595
      else
wenzelm@59175
   596
        map ((if Config.get ctxt break then string_of_margin ctxt else Pretty.str_of) #>
wenzelm@59175
   597
          Output.output)
wenzelm@58716
   598
        #> space_implode "\\isasep\\isanewline%\n"
wenzelm@58716
   599
        #> enclose "\\isa{" "}");
wenzelm@22124
   600
wenzelm@30390
   601
wenzelm@30390
   602
wenzelm@30390
   603
(** concrete antiquotations **)
wenzelm@30390
   604
wenzelm@30390
   605
(* basic entities *)
wenzelm@30390
   606
wenzelm@30390
   607
local
wenzelm@30390
   608
wenzelm@59175
   609
fun basic_entities name scan pretty =
wenzelm@59175
   610
  antiquotation name scan (fn {source, context, ...} =>
wenzelm@59175
   611
    output context o maybe_pretty_source pretty context source);
wenzelm@30390
   612
wenzelm@59175
   613
fun basic_entities_style name scan pretty =
wenzelm@59175
   614
  antiquotation name scan (fn {source, context, ...} => fn (style, xs) =>
wenzelm@38767
   615
    output context
wenzelm@38767
   616
      (maybe_pretty_source (fn ctxt => fn x => pretty ctxt (style, x)) context source xs));
haftmann@32890
   617
wenzelm@30390
   618
fun basic_entity name scan = basic_entities name (scan >> single);
wenzelm@30390
   619
wenzelm@30390
   620
in
wenzelm@30390
   621
wenzelm@53171
   622
val _ = Theory.setup
wenzelm@56204
   623
 (basic_entities_style @{binding thm} (Term_Style.parse -- Attrib.thms) pretty_thm_style #>
wenzelm@56204
   624
  basic_entity @{binding prop} (Term_Style.parse -- Args.prop) pretty_term_style #>
wenzelm@56204
   625
  basic_entity @{binding term} (Term_Style.parse -- Args.term) pretty_term_style #>
wenzelm@56204
   626
  basic_entity @{binding term_type} (Term_Style.parse -- Args.term) pretty_term_typ #>
wenzelm@56204
   627
  basic_entity @{binding typeof} (Term_Style.parse -- Args.term) pretty_term_typeof #>
wenzelm@56204
   628
  basic_entity @{binding const} (Args.const {proper = true, strict = false}) pretty_const #>
wenzelm@56204
   629
  basic_entity @{binding abbrev} (Scan.lift Args.name_inner_syntax) pretty_abbrev #>
wenzelm@56204
   630
  basic_entity @{binding typ} Args.typ_abbrev Syntax.pretty_typ #>
wenzelm@56204
   631
  basic_entity @{binding class} (Scan.lift Args.name_inner_syntax) pretty_class #>
wenzelm@56204
   632
  basic_entity @{binding type} (Scan.lift Args.name) pretty_type #>
wenzelm@59809
   633
  basic_entity @{binding text} (Scan.lift Args.text_input) pretty_text_report #>
wenzelm@56204
   634
  basic_entities @{binding prf} Attrib.thms (pretty_prf false) #>
wenzelm@56204
   635
  basic_entities @{binding full_prf} Attrib.thms (pretty_prf true) #>
wenzelm@56204
   636
  basic_entity @{binding theory} (Scan.lift (Parse.position Args.name)) pretty_theory);
haftmann@32898
   637
wenzelm@30390
   638
end;
wenzelm@30390
   639
wenzelm@30390
   640
wenzelm@30390
   641
(* goal state *)
wenzelm@30390
   642
wenzelm@30390
   643
local
wenzelm@22124
   644
wenzelm@49847
   645
fun goal_state name main = antiquotation name (Scan.succeed ())
wenzelm@59175
   646
  (fn {state, context = ctxt, ...} => fn () =>
wenzelm@59175
   647
    output ctxt
wenzelm@59175
   648
      [Goal_Display.pretty_goal
wenzelm@60094
   649
        (Config.put Goal_Display.show_main_goal main ctxt)
wenzelm@60094
   650
        (#goal (Proof.goal (Toplevel.proof_of state)))]);
wenzelm@22124
   651
wenzelm@30390
   652
in
wenzelm@30390
   653
wenzelm@53171
   654
val _ = Theory.setup
wenzelm@56204
   655
 (goal_state @{binding goals} true #>
wenzelm@56204
   656
  goal_state @{binding subgoals} false);
wenzelm@30390
   657
wenzelm@30390
   658
end;
wenzelm@22124
   659
wenzelm@22124
   660
wenzelm@30390
   661
(* embedded lemma *)
wenzelm@27522
   662
wenzelm@53171
   663
val _ = Theory.setup
wenzelm@56204
   664
  (antiquotation @{binding lemma}
wenzelm@56029
   665
    (Scan.lift (Scan.ahead Parse.not_eof) -- Args.prop --
wenzelm@58866
   666
      Scan.lift (Parse.position (Parse.reserved "by") -- Method.parse -- Scan.option Method.parse))
wenzelm@56029
   667
    (fn {source, context = ctxt, ...} => fn ((prop_token, prop), (((_, by_pos), m1), m2)) =>
wenzelm@43564
   668
      let
wenzelm@58011
   669
        val prop_src = Token.src (Token.name_of_src source) [prop_token];
wenzelm@55795
   670
wenzelm@55795
   671
        val reports = (by_pos, Markup.keyword1) :: maps Method.reports_of (m1 :: the_list m2);
wenzelm@55795
   672
        val _ = Context_Position.reports ctxt reports;
wenzelm@55795
   673
wenzelm@43564
   674
        (* FIXME check proof!? *)
wenzelm@55739
   675
        val _ = ctxt
wenzelm@43564
   676
          |> Proof.theorem NONE (K I) [[(prop, [])]]
wenzelm@55795
   677
          |> Proof.global_terminal_proof (m1, m2);
wenzelm@55739
   678
      in output ctxt (maybe_pretty_source pretty_term ctxt prop_src [prop]) end));
wenzelm@30390
   679
wenzelm@30390
   680
wenzelm@58716
   681
(* verbatim text *)
wenzelm@58716
   682
wenzelm@58741
   683
fun verbatim_text ctxt =
wenzelm@58741
   684
  if Config.get ctxt display then
wenzelm@58741
   685
    Latex.output_ascii #>
wenzelm@58741
   686
    enclose "\\begin{isabellett}%\n" "%\n\\end{isabellett}"
wenzelm@58741
   687
  else
wenzelm@58741
   688
    split_lines #>
wenzelm@58741
   689
    map (Latex.output_ascii #> enclose "\\isatt{" "}") #>
wenzelm@58741
   690
    space_implode "\\isasep\\isanewline%\n";
wenzelm@30390
   691
wenzelm@58741
   692
val _ =
wenzelm@58741
   693
  Theory.setup
wenzelm@58741
   694
    (antiquotation @{binding verbatim} (Scan.lift Args.text) (verbatim_text o #context));
wenzelm@58716
   695
wenzelm@58716
   696
wenzelm@58716
   697
(* ML text *)
wenzelm@40801
   698
wenzelm@30390
   699
local
wenzelm@30390
   700
wenzelm@59809
   701
fun ml_text name ml = antiquotation name (Scan.lift Args.text_input)
wenzelm@58716
   702
  (fn {context = ctxt, ...} => fn source =>
wenzelm@59064
   703
   (ML_Context.eval_in (SOME ctxt) ML_Compiler.flags (Input.pos_of source) (ml source);
wenzelm@59066
   704
    verbatim_text ctxt (Input.source_content source)));
wenzelm@30390
   705
wenzelm@55828
   706
fun ml_enclose bg en source =
wenzelm@59067
   707
  ML_Lex.read bg @ ML_Lex.read_source false source @ ML_Lex.read en;
wenzelm@37198
   708
wenzelm@30390
   709
in
wenzelm@30390
   710
wenzelm@53171
   711
val _ = Theory.setup
wenzelm@56204
   712
 (ml_text @{binding ML} (ml_enclose "fn _ => (" ");") #>
wenzelm@56204
   713
  ml_text @{binding ML_op} (ml_enclose "fn _ => (op " ");") #>
wenzelm@56204
   714
  ml_text @{binding ML_type} (ml_enclose "val _ = NONE : (" ") option;") #>
wenzelm@56204
   715
  ml_text @{binding ML_structure}
wenzelm@53171
   716
    (ml_enclose "functor XXX() = struct structure XX = " " end;") #>
wenzelm@37198
   717
wenzelm@56204
   718
  ml_text @{binding ML_functor}   (* FIXME formal treatment of functor name (!?) *)
wenzelm@55829
   719
    (fn source =>
wenzelm@59067
   720
      ML_Lex.read ("ML_Env.check_functor " ^
wenzelm@59066
   721
        ML_Syntax.print_string (Input.source_content source))) #>
wenzelm@37198
   722
wenzelm@56204
   723
  ml_text @{binding ML_text} (K []));
wenzelm@22124
   724
wenzelm@22124
   725
end;
wenzelm@30390
   726
wenzelm@54702
   727
wenzelm@54702
   728
(* URLs *)
wenzelm@54702
   729
wenzelm@54702
   730
val _ = Theory.setup
wenzelm@56204
   731
  (antiquotation @{binding url} (Scan.lift (Parse.position Parse.name))
wenzelm@54702
   732
    (fn {context = ctxt, ...} => fn (name, pos) =>
wenzelm@56034
   733
      (Context_Position.reports ctxt [(pos, Markup.language_path), (pos, Markup.url name)];
wenzelm@56034
   734
       enclose "\\url{" "}" name)));
wenzelm@54702
   735
wenzelm@58928
   736
wenzelm@58928
   737
wenzelm@58999
   738
(** document commands **)
wenzelm@58928
   739
wenzelm@58999
   740
fun old_header_command txt =
wenzelm@58928
   741
  Toplevel.keep (fn state =>
wenzelm@58928
   742
    if Toplevel.is_toplevel state then
wenzelm@58928
   743
     (legacy_feature "Obsolete 'header' command -- use 'chapter', 'section' etc. instead";
wenzelm@61457
   744
      ignore (output_text state {markdown = false} txt))
wenzelm@58928
   745
    else raise Toplevel.UNDEF);
wenzelm@58928
   746
wenzelm@61457
   747
fun document_command markdown (loc, txt) =
wenzelm@58928
   748
  Toplevel.keep (fn state =>
wenzelm@58999
   749
    (case loc of
wenzelm@61457
   750
      NONE => ignore (output_text state markdown txt)
wenzelm@58999
   751
    | SOME (_, pos) =>
wenzelm@58999
   752
        error ("Illegal target specification -- not a theory context" ^ Position.here pos))) o
wenzelm@61457
   753
  Toplevel.present_local_theory loc (fn state => ignore (output_text state markdown txt));
wenzelm@58928
   754
wenzelm@30390
   755
end;