src/Pure/Thy/thy_output.ML
author wenzelm
Thu, 25 Jan 2018 14:13:55 +0100
changeset 67505 ceb324e34c14
parent 67475 1a279ad4c6a4
child 67506 30233285270a
permissions -rw-r--r--
clarified signature: items with \isasep are special;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/thy_output.ML
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67381
diff changeset
     2
    Author:     Makarius
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     3
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67381
diff changeset
     4
Theory document output.
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     5
*)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     6
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     7
signature THY_OUTPUT =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
     8
sig
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
     9
  val output_text: Proof.context -> {markdown: bool} -> Input.source -> Latex.text list
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    10
  val check_comments: Proof.context -> Symbol_Pos.T list -> unit
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    11
  val check_token_comments: Proof.context -> Token.T -> unit
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    12
  val output_token: Proof.context -> Token.T -> Latex.text list
67474
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
    13
  val output_source: Proof.context -> string -> Latex.text list
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    14
  val present_thy: theory -> (Toplevel.transition * Toplevel.state) list ->
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    15
    Token.T list -> Latex.text list
28644
e2ae4a6cf166 ThyOutput: export some auxiliary operations;
wenzelm
parents: 28427
diff changeset
    16
  val pretty_term: Proof.context -> term -> Pretty.T
e2ae4a6cf166 ThyOutput: export some auxiliary operations;
wenzelm
parents: 28427
diff changeset
    17
  val pretty_thm: Proof.context -> thm -> Pretty.T
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    18
  val items: Latex.text list -> Latex.text list
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    19
  val isabelle: Proof.context -> Latex.text list -> Latex.text
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    20
  val isabelle_typewriter: Proof.context -> Latex.text list -> Latex.text
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    21
  val typewriter: Proof.context -> string -> Latex.text
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    22
  val verbatim: Proof.context -> string -> Latex.text
67474
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
    23
  val source: Proof.context -> Token.src -> Latex.text
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    24
  val pretty: Proof.context -> Pretty.T -> Latex.text
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    25
  val pretty_source: Proof.context -> Token.src -> Pretty.T -> Latex.text
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    26
  val pretty_items: Proof.context -> Pretty.T list -> Latex.text
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    27
  val pretty_items_source: Proof.context -> Token.src -> Pretty.T list -> Latex.text
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    28
  val antiquotation_pretty:
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    29
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    30
  val antiquotation_pretty_source:
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
    31
    binding -> 'a context_parser -> (Proof.context -> 'a -> Pretty.T) -> theory -> theory
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    32
  val antiquotation_raw:
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    33
    binding -> 'a context_parser -> (Proof.context -> 'a -> Latex.text) -> theory -> theory
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    34
  val antiquotation_verbatim:
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    35
    binding -> 'a context_parser -> (Proof.context -> 'a -> string) -> theory -> theory
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
    36
end;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
    37
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37198
diff changeset
    38
structure Thy_Output: THY_OUTPUT =
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
    39
struct
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
    40
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    41
(* output text *)
56548
ae6870efc28d markup for prose words within formal comments;
wenzelm
parents: 56438
diff changeset
    42
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    43
fun output_text ctxt {markdown} source =
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    44
  let
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    45
    val pos = Input.pos_of source;
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    46
    val syms = Input.source_explode source;
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
    47
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    48
    val _ = Context_Position.report ctxt pos (Markup.language_document (Input.is_delimited source));
62749
eba34ff9671c clarified reports;
wenzelm
parents: 62453
diff changeset
    49
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    50
    val output_antiquotes = maps (Document_Antiquotation.evaluate ctxt);
61036
f6f2959bed67 clarified language context, e.g. relevant for symbols;
wenzelm
parents: 60100
diff changeset
    51
61461
77c9643a6353 more explicit output of list items;
wenzelm
parents: 61459
diff changeset
    52
    fun output_line line =
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    53
      (if Markdown.line_is_item line then [Latex.string "\\item "] else []) @
61461
77c9643a6353 more explicit output of list items;
wenzelm
parents: 61459
diff changeset
    54
        output_antiquotes (Markdown.line_content line);
77c9643a6353 more explicit output of list items;
wenzelm
parents: 61459
diff changeset
    55
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    56
    fun output_block (Markdown.Par lines) =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    57
          Latex.block (separate (Latex.string "\n") (map (Latex.block o output_line) lines))
61459
5f2ddeb15c06 clarified nesting of paragraphs: indentation is taken into account more uniformly;
wenzelm
parents: 61458
diff changeset
    58
      | output_block (Markdown.List {kind, body, ...}) =
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    59
          Latex.environment_block (Markdown.print_kind kind) (output_blocks body)
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
    60
    and output_blocks blocks = separate (Latex.string "\n\n") (map output_block blocks);
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    61
  in
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    62
    if Toplevel.is_skipped_proof (Toplevel.presentation_state ctxt) then []
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    63
    else if markdown andalso exists (Markdown.is_control o Symbol_Pos.symbol) syms
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    64
    then
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    65
      let
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    66
        val ants = Antiquote.parse pos syms;
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    67
        val reports = Antiquote.antiq_reports ants;
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    68
        val blocks = Markdown.read_antiquotes ants;
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    69
        val _ = Context_Position.reports ctxt (reports @ Markdown.reports blocks);
67186
a58bbe66ac81 avoid excessive whitespace between antiquotations and text;
wenzelm
parents: 67184
diff changeset
    70
      in output_blocks blocks end
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    71
    else
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    72
      let
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
    73
        val ants = Antiquote.parse pos (trim (Symbol.is_blank o Symbol_Pos.symbol) syms);
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    74
        val reports = Antiquote.antiq_reports ants;
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
    75
        val _ = Context_Position.reports ctxt (reports @ Markdown.text_reports ants);
67186
a58bbe66ac81 avoid excessive whitespace between antiquotations and text;
wenzelm
parents: 67184
diff changeset
    76
      in output_antiquotes ants end
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67173
diff changeset
    77
  end;
48918
6e5fd4585512 check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents: 47005
diff changeset
    78
6e5fd4585512 check side-comments of command spans (normally filtered out in Outer_Syntax.toplevel_source);
wenzelm
parents: 47005
diff changeset
    79
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
    80
(* output tokens with formal comments *)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
    81
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
    82
local
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
    83
67461
aad5c0982c3d tuned signature;
wenzelm
parents: 67446
diff changeset
    84
val output_symbols = single o Latex.symbols_output;
67356
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
    85
67358
wenzelm
parents: 67357
diff changeset
    86
val output_symbols_antiq =
wenzelm
parents: 67357
diff changeset
    87
  (fn Antiquote.Text syms => output_symbols syms
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
    88
    | Antiquote.Control {name = (name, _), body, ...} =>
67374
5a049cf98438 clarified output (see also 909dcdec2122, 34d1913f0b20);
wenzelm
parents: 67372
diff changeset
    89
        Latex.string (Latex.output_symbols [Symbol.encode (Symbol.Control name)]) ::
67358
wenzelm
parents: 67357
diff changeset
    90
          output_symbols body
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
    91
    | Antiquote.Antiq {body, ...} =>
67358
wenzelm
parents: 67357
diff changeset
    92
        Latex.enclose_body "%\n\\isaantiq\n" "{}%\n\\endisaantiq\n" (output_symbols body));
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
    93
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
    94
fun output_symbols_comment ctxt {antiq} (comment, syms) =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
    95
  (case (comment, antiq) of
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
    96
    (NONE, false) => output_symbols syms
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
    97
  | (NONE, true) =>
67413
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
    98
      Antiquote.parse (#1 (Symbol_Pos.range syms)) syms
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
    99
      |> maps output_symbols_antiq
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   100
  | (SOME Comment.Comment, _) =>
67413
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
   101
      let
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
   102
        val body = Symbol_Pos.cartouche_content syms;
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
   103
        val source = Input.source true (Symbol_Pos.implode body) (Symbol_Pos.range body);
67475
1a279ad4c6a4 disable "display" style in marginal (line) comment;
wenzelm
parents: 67474
diff changeset
   104
        val ctxt' = ctxt
1a279ad4c6a4 disable "display" style in marginal (line) comment;
wenzelm
parents: 67474
diff changeset
   105
          |> Config.put Document_Antiquotation.thy_output_display false;
67413
2555713586c8 added \<^cancel> operator for unused text;
wenzelm
parents: 67393
diff changeset
   106
      in
67475
1a279ad4c6a4 disable "display" style in marginal (line) comment;
wenzelm
parents: 67474
diff changeset
   107
        output_text ctxt' {markdown = false} source
67421
c4a10621c72e allow TeX comment % in formal comment body, but avoid extra space (cf. d7c6054b2ab1);
wenzelm
parents: 67413
diff changeset
   108
        |> Latex.enclose_body "%\n\\isamarkupcmt{" "%\n}"
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   109
      end
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   110
  | (SOME Comment.Cancel, _) =>
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   111
      output_symbols (Symbol_Pos.cartouche_content syms)
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67425
diff changeset
   112
      |> Latex.enclose_body "%\n\\isamarkupcancel{" "}"
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67425
diff changeset
   113
  | (SOME Comment.Latex, _) =>
67461
aad5c0982c3d tuned signature;
wenzelm
parents: 67446
diff changeset
   114
      [Latex.symbols (Symbol_Pos.cartouche_content syms)]);
67372
wenzelm
parents: 67360
diff changeset
   115
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   116
in
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   117
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   118
fun output_body ctxt antiq bg en syms =
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   119
  (case Comment.read_body syms of
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   120
    SOME res => maps (output_symbols_comment ctxt {antiq = antiq}) res
67375
wenzelm
parents: 67374
diff changeset
   121
  | NONE => output_symbols syms) |> Latex.enclose_body bg en
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   122
and output_token ctxt tok =
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   123
  let
67378
wenzelm
parents: 67377
diff changeset
   124
    fun output antiq bg en =
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   125
      output_body ctxt antiq bg en (Input.source_explode (Token.input_of tok));
67378
wenzelm
parents: 67377
diff changeset
   126
  in
wenzelm
parents: 67377
diff changeset
   127
    (case Token.kind_of tok of
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   128
      Token.Comment NONE => []
67378
wenzelm
parents: 67377
diff changeset
   129
    | Token.Command => output false "\\isacommand{" "}"
wenzelm
parents: 67377
diff changeset
   130
    | Token.Keyword =>
wenzelm
parents: 67377
diff changeset
   131
        if Symbol.is_ascii_identifier (Token.content_of tok)
wenzelm
parents: 67377
diff changeset
   132
        then output false "\\isakeyword{" "}"
wenzelm
parents: 67377
diff changeset
   133
        else output false "" ""
wenzelm
parents: 67377
diff changeset
   134
    | Token.String => output false "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}"
wenzelm
parents: 67377
diff changeset
   135
    | Token.Alt_String => output false "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}"
wenzelm
parents: 67377
diff changeset
   136
    | Token.Verbatim => output true "{\\isacharverbatimopen}" "{\\isacharverbatimclose}"
wenzelm
parents: 67377
diff changeset
   137
    | Token.Cartouche => output false "{\\isacartoucheopen}" "{\\isacartoucheclose}"
wenzelm
parents: 67377
diff changeset
   138
    | _ => output false "" "")
wenzelm
parents: 67377
diff changeset
   139
  end handle ERROR msg => error (msg ^ Position.here (Token.pos_of tok));
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   140
67474
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   141
fun output_source ctxt s =
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   142
  output_body ctxt false "" "" (Symbol_Pos.explode (s, Position.none));
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   143
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   144
fun check_comments ctxt =
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   145
  Comment.read_body #> (Option.app o List.app) (fn (comment, syms) =>
67388
5fc0b0c9a735 clarified markup: more like outer syntax side-comment;
wenzelm
parents: 67386
diff changeset
   146
    let
5fc0b0c9a735 clarified markup: more like outer syntax side-comment;
wenzelm
parents: 67386
diff changeset
   147
      val pos = #1 (Symbol_Pos.range syms);
5fc0b0c9a735 clarified markup: more like outer syntax side-comment;
wenzelm
parents: 67386
diff changeset
   148
      val _ =
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67425
diff changeset
   149
        comment |> Option.app (fn kind =>
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67425
diff changeset
   150
          Context_Position.reports ctxt [(pos, Comment.markup kind), (pos, Markup.cartouche)]);
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   151
      val _ = output_symbols_comment ctxt {antiq = false} (comment, syms);
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents: 67421
diff changeset
   152
    in if comment = SOME Comment.Comment then check_comments ctxt syms else () end);
67377
143665524d8e check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents: 67375
diff changeset
   153
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   154
fun check_token_comments ctxt tok =
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   155
  check_comments ctxt (Input.source_explode (Token.input_of tok));
67377
143665524d8e check formal comments recursively, within arbitrary cartouches (unknown sublanguages);
wenzelm
parents: 67375
diff changeset
   156
67353
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   157
end;
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   158
95f5f4bec7af clarified modules;
wenzelm
parents: 67194
diff changeset
   159
67356
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   160
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   161
(** present theory source **)
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   162
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   163
(*NB: arranging white space around command spans is a black art*)
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   164
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   165
val is_white = Token.is_space orf Token.is_informal_comment;
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   166
val is_black = not o is_white;
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   167
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   168
val is_white_comment = Token.is_informal_comment;
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   169
val is_black_comment = Token.is_formal_comment;
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   170
67356
ba226b87c69e output token content with formal comments and antiquotations;
wenzelm
parents: 67354
diff changeset
   171
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   172
(* presentation tokens *)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   173
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   174
datatype token =
67360
wenzelm
parents: 67359
diff changeset
   175
    Ignore_Token
59065
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   176
  | Basic_Token of Token.T
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   177
  | Markup_Token of string * Input.source
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   178
  | Markup_Env_Token of string * Input.source
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61455
diff changeset
   179
  | Raw_Token of Input.source;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   180
59065
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   181
fun basic_token pred (Basic_Token tok) = pred tok
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   182
  | basic_token _ _ = false;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   183
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   184
val white_token = basic_token is_white;
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   185
val white_comment_token = basic_token is_white_comment;
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   186
val blank_token = basic_token Token.is_blank;
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   187
val newline_token = basic_token Token.is_newline;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   188
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   189
fun present_token ctxt tok =
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
   190
  (case tok of
67360
wenzelm
parents: 67359
diff changeset
   191
    Ignore_Token => []
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   192
  | Basic_Token tok => output_token ctxt tok
67357
d7c6054b2ab1 clarified output: avoid extra space;
wenzelm
parents: 67356
diff changeset
   193
  | Markup_Token (cmd, source) =>
d7c6054b2ab1 clarified output: avoid extra space;
wenzelm
parents: 67356
diff changeset
   194
      Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n"
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   195
        (output_text ctxt {markdown = false} source)
61457
3e21699bb83b clarified Antiquote.antiq_reports;
wenzelm
parents: 61456
diff changeset
   196
  | Markup_Env_Token (cmd, source) =>
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   197
      [Latex.environment_block ("isamarkup" ^ cmd) (output_text ctxt {markdown = true} source)]
67186
a58bbe66ac81 avoid excessive whitespace between antiquotations and text;
wenzelm
parents: 67184
diff changeset
   198
  | Raw_Token source =>
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   199
      Latex.string "%\n" :: output_text ctxt {markdown = true} source @ [Latex.string "\n"]);
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   200
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   201
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   202
(* command spans *)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   203
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   204
type command = string * Position.T * string list;   (*name, position, tags*)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   205
type source = (token * (string * int)) list;        (*token, markup flag, meta-comment depth*)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   206
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   207
datatype span = Span of command * (source * source * source * source) * bool;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   208
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   209
fun make_span cmd src =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   210
  let
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   211
    fun take_newline (tok :: toks) =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   212
          if newline_token (fst tok) then ([tok], toks, true)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   213
          else ([], tok :: toks, false)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   214
      | take_newline [] = ([], [], false);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   215
    val (((src_prefix, src_main), src_suffix1), (src_suffix2, src_appendix, newline)) =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   216
      src
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   217
      |> take_prefix (white_token o fst)
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   218
      ||>> take_suffix (white_token o fst)
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   219
      ||>> take_prefix (white_comment_token o fst)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   220
      ||> take_newline;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   221
  in Span (cmd, (src_prefix, src_main, src_suffix1 @ src_suffix2, src_appendix), newline) end;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   222
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   223
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   224
(* present spans *)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   225
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   226
local
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   227
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   228
fun err_bad_nesting pos =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   229
  error ("Bad nesting of commands in presentation" ^ pos);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   230
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   231
fun edge which f (x: string option, y) =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   232
  if x = y then I
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   233
  else (case which (x, y) of NONE => I | SOME txt => cons (Latex.string (f txt)));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   234
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   235
val begin_tag = edge #2 Latex.begin_tag;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   236
val end_tag = edge #1 Latex.end_tag;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   237
fun open_delim delim e = edge #2 Latex.begin_delim e #> delim #> edge #2 Latex.end_delim e;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   238
fun close_delim delim e = edge #1 Latex.begin_delim e #> delim #> edge #1 Latex.end_delim e;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   239
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   240
in
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   241
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   242
fun present_span thy keywords document_tags span state state'
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   243
    (tag_stack, active_tag, newline, latex, present_cont) =
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   244
  let
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   245
    val ctxt' =
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   246
      Toplevel.presentation_context state'
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   247
        handle Toplevel.UNDEF => Proof_Context.init_global (Context.get_theory thy Context.PureN);
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   248
    val present = fold (fn (tok, (flag, 0)) =>
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   249
        fold cons (present_token ctxt' tok)
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   250
        #> cons (Latex.string flag)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   251
      | _ => I);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   252
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   253
    val Span ((cmd_name, cmd_pos, cmd_tags), srcs, span_newline) = span;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   254
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   255
    val (tag, tags) = tag_stack;
46924
wenzelm
parents: 46922
diff changeset
   256
    val tag' = try hd (fold (update (op =)) cmd_tags (the_list tag));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   257
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   258
    val nesting = Toplevel.level state' - Toplevel.level state;
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   259
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   260
    val active_tag' =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   261
      if is_some tag' then tag'
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   262
      else if cmd_name = "end" andalso not (Toplevel.is_toplevel state') then NONE
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   263
      else
67138
82283d52b4d6 system option for default command tags;
wenzelm
parents: 66021
diff changeset
   264
        (case Keyword.command_tags keywords cmd_name @ document_tags of
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   265
          default_tag :: _ => SOME default_tag
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   266
        | [] =>
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   267
            if Keyword.is_vacuous keywords cmd_name andalso Toplevel.is_proof state
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   268
            then active_tag
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   269
            else NONE);
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   270
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   271
    val edge = (active_tag, active_tag');
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   272
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   273
    val newline' =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   274
      if is_none active_tag' then span_newline else newline;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   275
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   276
    val tag_stack' =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   277
      if nesting = 0 andalso not (Toplevel.is_proof state) then tag_stack
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   278
      else if nesting >= 0 then (tag', replicate nesting tag @ tags)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   279
      else
33957
e9afca2118d4 normalized uncurry take/drop
haftmann
parents: 33955
diff changeset
   280
        (case drop (~ nesting - 1) tags of
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   281
          tg :: tgs => (tg, tgs)
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 48927
diff changeset
   282
        | [] => err_bad_nesting (Position.here cmd_pos));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   283
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   284
    val latex' =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   285
      latex
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   286
      |> end_tag edge
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   287
      |> close_delim (fst present_cont) edge
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   288
      |> snd present_cont
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   289
      |> open_delim (present (#1 srcs)) edge
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   290
      |> begin_tag edge
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   291
      |> present (#2 srcs);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   292
    val present_cont' =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   293
      if newline then (present (#3 srcs), present (#4 srcs))
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   294
      else (I, present (#3 srcs) #> present (#4 srcs));
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   295
  in (tag_stack', active_tag', newline', latex', present_cont') end;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   296
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   297
fun present_trailer ((_, tags), active_tag, _, latex, present_cont) =
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   298
  if not (null tags) then err_bad_nesting " at end of theory"
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   299
  else
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   300
    latex
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   301
    |> end_tag (active_tag, NONE)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   302
    |> close_delim (fst present_cont) (active_tag, NONE)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   303
    |> snd present_cont;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   304
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   305
end;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   306
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   307
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   308
(* present_thy *)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   309
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   310
local
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   311
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   312
val markup_true = "\\isamarkuptrue%\n";
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   313
val markup_false = "\\isamarkupfalse%\n";
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   314
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   315
val space_proper =
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   316
  Scan.one Token.is_blank -- Scan.many is_white_comment -- Scan.one is_black;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   317
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   318
val is_improper = not o (is_black orf Token.is_begin_ignore orf Token.is_end_ignore);
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   319
val improper = Scan.many is_improper;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   320
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   321
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   322
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   323
val opt_newline = Scan.option (Scan.one Token.is_newline);
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   324
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   325
val ignore =
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   326
  Scan.depend (fn d => opt_newline |-- Scan.one Token.is_begin_ignore
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   327
    >> pair (d + 1)) ||
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36950
diff changeset
   328
  Scan.depend (fn d => Scan.one Token.is_end_ignore --|
43947
9b00f09f7721 defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents: 43564
diff changeset
   329
    (if d = 0 then Scan.fail_with (K (fn () => "Bad nesting of meta-comments")) else opt_newline)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   330
    >> pair (d - 1));
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   331
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36745
diff changeset
   332
val tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (Parse.tag_name --| blank_end);
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   333
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   334
val locale =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36745
diff changeset
   335
  Scan.option ((Parse.$$$ "(" -- improper -- Parse.$$$ "in") |--
62969
9f394a16c557 eliminated "xname" and variants;
wenzelm
parents: 62749
diff changeset
   336
    Parse.!!! (improper |-- Parse.name --| (improper -- Parse.$$$ ")")));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   337
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   338
in
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   339
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58923
diff changeset
   340
fun present_thy thy command_results toks =
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   341
  let
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58923
diff changeset
   342
    val keywords = Thy_Header.get_keywords thy;
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58923
diff changeset
   343
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58923
diff changeset
   344
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   345
    (* tokens *)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   346
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   347
    val ignored = Scan.state --| ignore
67360
wenzelm
parents: 67359
diff changeset
   348
      >> (fn d => (NONE, (Ignore_Token, ("", d))));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   349
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   350
    fun markup pred mk flag = Scan.peek (fn d =>
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 36745
diff changeset
   351
      improper |--
58999
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   352
        Parse.position (Scan.one (fn tok =>
ed09ae4ea2d8 uniform treatment of all document markup commands: 'text' and 'txt' merely differ in LaTeX style;
wenzelm
parents: 58992
diff changeset
   353
          Token.is_command tok andalso pred keywords (Token.content_of tok))) --
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   354
      Scan.repeat tag --
51627
589daaf48dba tuned signature -- agree with markup terminology;
wenzelm
parents: 51626
diff changeset
   355
      Parse.!!!! ((improper -- locale -- improper) |-- Parse.document_source --| improper_end)
59064
a8bcb5a446c8 more abstract type Input.source;
wenzelm
parents: 58999
diff changeset
   356
      >> (fn (((tok, pos'), tags), source) =>
59065
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   357
        let val name = Token.content_of tok
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   358
        in (SOME (name, pos', tags), (mk (name, source), (flag, d))) end));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   359
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   360
    val command = Scan.peek (fn d =>
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 61748
diff changeset
   361
      Scan.optional (Scan.one Token.is_command_modifier ::: improper) [] --
59924
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   362
      Scan.one Token.is_command -- Scan.repeat tag
59939
7d46aa03696e support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents: 59924
diff changeset
   363
      >> (fn ((cmd_mod, cmd), tags) =>
7d46aa03696e support for 'restricted' modifier: only qualified accesses outside the local scope;
wenzelm
parents: 59924
diff changeset
   364
        map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @
59924
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   365
          [(SOME (Token.content_of cmd, Token.pos_of cmd, tags),
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   366
            (Basic_Token cmd, (markup_false, d)))]));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   367
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   368
    val cmt = Scan.peek (fn d =>
67446
1f4d167b6ac9 discontinued old form of marginal comments;
wenzelm
parents: 67439
diff changeset
   369
      Scan.one is_black_comment >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   370
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   371
    val other = Scan.peek (fn d =>
59065
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   372
       Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   373
59924
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   374
    val tokens =
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   375
      (ignored ||
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   376
        markup Keyword.is_document_heading Markup_Token markup_true ||
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   377
        markup Keyword.is_document_body Markup_Env_Token markup_true ||
61456
b521b8b400f7 trim_blanks after read, before eval;
wenzelm
parents: 61455
diff changeset
   378
        markup Keyword.is_document_raw (Raw_Token o #2) "") >> single ||
59924
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   379
      command ||
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   380
      (cmt || other) >> single;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   381
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   382
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   383
    (* spans *)
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   384
59065
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   385
    val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false;
8ce02aafc363 tuned signature -- prefer Input.source;
wenzelm
parents: 59064
diff changeset
   386
    val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   387
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   388
    val cmd = Scan.one (is_some o fst);
27732
8dbf5761a24a abstract type Scan.stopper;
wenzelm
parents: 27566
diff changeset
   389
    val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   390
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   391
    val white_comments = Scan.many (white_comment_token o fst o snd);
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   392
    val blank = Scan.one (blank_token o fst o snd);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   393
    val newline = Scan.one (newline_token o fst o snd);
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   394
    val before_cmd =
67439
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   395
      Scan.option (newline -- white_comments) --
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   396
      Scan.option (newline -- white_comments) --
78759a7bd874 more uniform support for formal comments in outer syntax, notably \<^cancel> and \<^latex>;
wenzelm
parents: 67429
diff changeset
   397
      Scan.option (blank -- white_comments) -- cmd;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   398
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   399
    val span =
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   400
      Scan.repeat non_cmd -- cmd --
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   401
        Scan.repeat (Scan.unless before_cmd non_cmd) --
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   402
        Scan.option (newline >> (single o snd))
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   403
      >> (fn (((toks1, (cmd, tok2)), toks3), tok4) =>
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   404
          make_span (the cmd) (toks1 @ (tok2 :: (toks3 @ the_default [] tok4))));
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   405
57080
0e5fa27d3293 strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents: 56548
diff changeset
   406
    val spans = toks
0e5fa27d3293 strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents: 56548
diff changeset
   407
      |> take_suffix Token.is_space |> #1
0e5fa27d3293 strip trailing white space, to avoid notorious problems of jEdit with last line;
wenzelm
parents: 56548
diff changeset
   408
      |> Source.of_list
59924
801b979ec0c2 more general notion of command span: command keyword not necessarily at start;
wenzelm
parents: 59917
diff changeset
   409
      |> Source.source' 0 Token.stopper (Scan.error (Scan.bulk tokens >> flat))
58864
505a8150368a recover via scanner;
wenzelm
parents: 58861
diff changeset
   410
      |> Source.source stopper (Scan.error (Scan.bulk span))
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   411
      |> Source.exhaust;
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   412
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   413
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   414
    (* present commands *)
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   415
67147
dea94b1aabc3 prefer control symbol antiquotations;
wenzelm
parents: 67138
diff changeset
   416
    val document_tags = space_explode "," (Options.default_string \<^system_option>\<open>document_tags\<close>);
67138
82283d52b4d6 system option for default command tags;
wenzelm
parents: 66021
diff changeset
   417
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   418
    fun present_command tr span st st' =
67191
9ab34bb83a84 option document_positions;
wenzelm
parents: 67188
diff changeset
   419
      Toplevel.setmp_thread_position tr
67381
146757999c8d theory Pure is default presentation context;
wenzelm
parents: 67378
diff changeset
   420
        (present_span thy keywords document_tags span st st');
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   421
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   422
    fun present _ [] = I
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   423
      | present st (((tr, st'), span) :: rest) = present_command tr span st st' #> present st' rest;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   424
  in
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   425
    if length command_results = length spans then
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   426
      ((NONE, []), NONE, true, [], (I, I))
28427
cc9f7d99fb73 turned process_thy into present_thy, which merely does presentation (wrt. persistent intermediate states);
wenzelm
parents: 28273
diff changeset
   427
      |> present Toplevel.toplevel (command_results ~~ spans)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   428
      |> present_trailer
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67191
diff changeset
   429
      |> rev
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   430
    else error "Messed-up outer syntax for presentation"
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   431
  end;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   432
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   433
end;
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   434
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   435
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   436
67386
998e01d6f8fd clarified modules;
wenzelm
parents: 67381
diff changeset
   437
(** standard output operations **)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   438
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   439
(* pretty printing *)
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   440
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   441
fun pretty_term ctxt t =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   442
  Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24680
diff changeset
   443
32898
e871d897969c term styles also cover antiquotations term_type and typeof
haftmann
parents: 32890
diff changeset
   444
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
e871d897969c term styles also cover antiquotations term_type and typeof
haftmann
parents: 32890
diff changeset
   445
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   446
30390
ad591ee76c78 simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents: 30381
diff changeset
   447
(* default output *)
ad591ee76c78 simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents: 30381
diff changeset
   448
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   449
val items = separate (Latex.string "\\isasep\\isanewline%\n");
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   450
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   451
fun isabelle ctxt body =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   452
  if Config.get ctxt Document_Antiquotation.thy_output_display
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   453
  then Latex.environment_block "isabelle" body
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   454
  else Latex.block (Latex.enclose_body "\\isa{" "}" body);
59175
bf465f335e85 system option "pretty_margin" is superseded by "thy_output_margin";
wenzelm
parents: 59067
diff changeset
   455
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   456
fun isabelle_typewriter ctxt body =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   457
  if Config.get ctxt Document_Antiquotation.thy_output_display
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   458
  then Latex.environment_block "isabellett" body
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   459
  else Latex.block (Latex.enclose_body "\\isatt{" "}" body);
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   460
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   461
fun typewriter ctxt s =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   462
  isabelle_typewriter ctxt [Latex.string (Latex.output_ascii s)];
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   464
fun verbatim ctxt =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   465
  if Config.get ctxt Document_Antiquotation.thy_output_display
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   466
  then Document_Antiquotation.indent_lines ctxt #> typewriter ctxt
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   467
  else split_lines #> map (typewriter ctxt) #> items #> Latex.block;
22124
27b674312b2f renamed Isar/isar_output.ML to Thy/thy_output.ML;
wenzelm
parents:
diff changeset
   468
67474
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   469
fun source ctxt =
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   470
  Token.args_of_src
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   471
  #> map (Token.unparse #> Document_Antiquotation.prepare_lines ctxt)
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   472
  #> space_implode " "
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   473
  #> output_source ctxt
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   474
  #> isabelle ctxt;
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   475
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   476
fun pretty ctxt =
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   477
  Document_Antiquotation.output ctxt #> Latex.string #> single #> isabelle ctxt;
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   478
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   479
fun pretty_source ctxt src prt =
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   480
  if Config.get ctxt Document_Antiquotation.thy_output_source
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   481
  then source ctxt src else pretty ctxt prt;
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   482
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   483
fun pretty_items ctxt =
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   484
  map (Document_Antiquotation.output ctxt #> Latex.string) #> items #> isabelle ctxt;
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   485
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   486
fun pretty_items_source ctxt src prts =
67474
90def2b06305 more uniform output of source / text / theory_text, with handling of formal comments etc.;
wenzelm
parents: 67465
diff changeset
   487
  if Config.get ctxt Document_Antiquotation.thy_output_source
67505
ceb324e34c14 clarified signature: items with \isasep are special;
wenzelm
parents: 67475
diff changeset
   488
  then source ctxt src else pretty_items ctxt prts;
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   489
67465
wenzelm
parents: 67463
diff changeset
   490
wenzelm
parents: 67463
diff changeset
   491
(* antiquotation variants *)
wenzelm
parents: 67463
diff changeset
   492
67463
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   493
fun antiquotation_pretty name scan f =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   494
  Document_Antiquotation.setup name scan
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   495
    (fn {context = ctxt, argument = x, ...} => pretty ctxt (f ctxt x));
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   496
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   497
fun antiquotation_pretty_source name scan f =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   498
  Document_Antiquotation.setup name scan
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   499
    (fn {context = ctxt, source = src, argument = x} => pretty_source ctxt src (f ctxt x));
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   500
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   501
fun antiquotation_raw name scan f =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   502
  Document_Antiquotation.setup name scan
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   503
    (fn {context = ctxt, argument = x, ...} => f ctxt x);
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   504
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   505
fun antiquotation_verbatim name scan f =
a5ca98950a91 clarified access to antiquotation options;
wenzelm
parents: 67461
diff changeset
   506
  antiquotation_raw name scan (fn ctxt => verbatim ctxt o f ctxt);
61491
97261e6c1d42 another antiquotation short form: undecorated cartouche as alias for @{text};
wenzelm
parents: 61473
diff changeset
   507
30390
ad591ee76c78 simplified interface to define document antiquotations, cf. antiquotatation, maybe_pretty_source, output;
wenzelm
parents: 30381
diff changeset
   508
end;