src/Doc/antiquote_setup.ML
author wenzelm
Thu, 28 Aug 2014 15:47:26 +0200
changeset 58069 0255436b3d85
parent 57918 f5d73caba4e5
child 58716 23a380cc45f4
permissions -rw-r--r--
more liberal embedded "text", which includes cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48985
5386df44a037 renamed doc-src to src/Doc;
wenzelm
parents: 48899
diff changeset
     1
(*  Title:      Doc/antiquote_setup.ML
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
     3
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
     4
Auxiliary antiquotations for the Isabelle manuals.
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
     5
*)
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
     6
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
     7
structure Antiquote_Setup: sig end =
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
     8
struct
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
     9
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    10
(* misc utils *)
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    11
29736
ec3fc818b82e clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents: 29726
diff changeset
    12
fun translate f = Symbol.explode #> map f #> implode;
ec3fc818b82e clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents: 29726
diff changeset
    13
ec3fc818b82e clean_string/clean_name: proper treatment of \<dash>;
wenzelm
parents: 29726
diff changeset
    14
val clean_string = translate
26853
52cb0e965041 clean_string: map "_" to "\\_" (best used with underscore.sty);
wenzelm
parents: 26843
diff changeset
    15
  (fn "_" => "\\_"
30120
aaa4667285c8 uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents: 29736
diff changeset
    16
    | "#" => "\\#"
52408
fa2dc6c6c94f updated operations on proof terms;
wenzelm
parents: 50239
diff changeset
    17
    | "$" => "\\$"
fa2dc6c6c94f updated operations on proof terms;
wenzelm
parents: 50239
diff changeset
    18
    | "%" => "\\%"
26897
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    19
    | "<" => "$<$"
26768
844068d16ba0 clean_string: handle { };
wenzelm
parents: 26756
diff changeset
    20
    | ">" => "$>$"
844068d16ba0 clean_string: handle { };
wenzelm
parents: 26756
diff changeset
    21
    | "{" => "\\{"
30120
aaa4667285c8 uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
wenzelm
parents: 29736
diff changeset
    22
    | "|" => "$\\mid$"
26768
844068d16ba0 clean_string: handle { };
wenzelm
parents: 26756
diff changeset
    23
    | "}" => "\\}"
42666
fee67c099d03 use existing \<hyphen>;
wenzelm
parents: 42361
diff changeset
    24
    | "\<hyphen>" => "-"
26768
844068d16ba0 clean_string: handle { };
wenzelm
parents: 26756
diff changeset
    25
    | c => c);
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
    26
31546
d58d6acab331 eliminated escaped symbols;
wenzelm
parents: 31297
diff changeset
    27
fun clean_name "\<dots>" = "dots"
26897
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    28
  | clean_name ".." = "ddot"
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    29
  | clean_name "." = "dot"
26910
aa6357b39212 tuned clean_name (underscore);
wenzelm
parents: 26903
diff changeset
    30
  | clean_name "_" = "underscore"
26897
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    31
  | clean_name "{" = "braceleft"
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    32
  | clean_name "}" = "braceright"
42666
fee67c099d03 use existing \<hyphen>;
wenzelm
parents: 42361
diff changeset
    33
  | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
26897
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
    34
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    35
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    36
(* verbatim text *)
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    37
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    38
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    39
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
    40
val _ =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
    41
  Theory.setup (Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
    42
    (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")));
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    43
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    44
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    45
(* ML text *)
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    46
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    47
local
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
    48
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    49
val ml_toks = ML_Lex.read Position.none;
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
    50
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    51
fun ml_val (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks ");"
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    52
  | ml_val (toks1, toks2) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    53
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    54
fun ml_op (toks1, []) = ml_toks "fn _ => (op " @ toks1 @ ml_toks ");"
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    55
  | ml_op (toks1, toks2) = ml_toks "fn _ => (op " @ toks1 @ ml_toks " : " @ toks2 @ ml_toks ");";
46261
b03897da3c90 document antiquotations for ML infix operators;
wenzelm
parents: 45675
diff changeset
    56
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    57
fun ml_type (toks1, []) = ml_toks "val _ = NONE : (" @ toks1 @ ml_toks ") option;"
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    58
  | ml_type (toks1, toks2) =
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    59
      ml_toks "val _ = [NONE : (" @ toks1 @ ml_toks ") option, NONE : (" @
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    60
        toks2 @ ml_toks ") option];";
22289
41ce4f5c97c9 added antiquotation for exceptions
haftmann
parents: 22094
diff changeset
    61
55837
154855d9a564 clarified names of antiquotations and markup;
wenzelm
parents: 55831
diff changeset
    62
fun ml_exception (toks1, []) = ml_toks "fn _ => (" @ toks1 @ ml_toks " : exn);"
154855d9a564 clarified names of antiquotations and markup;
wenzelm
parents: 55831
diff changeset
    63
  | ml_exception (toks1, toks2) =
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    64
      ml_toks "fn _ => (" @ toks1 @ ml_toks " : " @ toks2 @ ml_toks " -> exn);";
22289
41ce4f5c97c9 added antiquotation for exceptions
haftmann
parents: 22094
diff changeset
    65
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    66
fun ml_structure (toks, _) =
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    67
  ml_toks "functor XXX() = struct structure XX = " @ toks @ ml_toks " end;";
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
    68
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    69
fun ml_functor (Antiquote.Text tok :: _, _) =
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    70
      ml_toks "ML_Env.check_functor " @ ml_toks (ML_Syntax.print_string (ML_Lex.content_of tok))
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    71
  | ml_functor _ = raise Fail "Bad ML functor specification";
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
    72
39869
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    73
val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    74
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    75
fun ml_name txt =
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    76
  (case filter is_name (ML_Lex.tokenize txt) of
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    77
    toks as [_] => ML_Lex.flatten toks
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    78
  | _ => error ("Single ML name expected in input: " ^ quote txt));
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
    79
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    80
fun prep_ml source =
56278
2576d3a40ed6 separate tokenization and language context for SML: no symbols, no antiquotes;
wenzelm
parents: 56275
diff changeset
    81
  (#1 (Symbol_Pos.source_content source), ML_Lex.read_source false source);
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    82
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37198
diff changeset
    83
fun index_ml name kind ml = Thy_Output.antiquotation name
58069
0255436b3d85 more liberal embedded "text", which includes cartouches;
wenzelm
parents: 57918
diff changeset
    84
  (Scan.lift (Args.text_source_position -- Scan.option (Args.colon |-- Args.text_source_position)))
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    85
  (fn {context = ctxt, ...} => fn (source1, opt_source2) =>
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
    86
    let
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    87
      val (txt1, toks1) = prep_ml source1;
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    88
      val (txt2, toks2) =
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    89
        (case opt_source2 of
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    90
          SOME source => prep_ml source
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    91
        | NONE => ("", []));
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
    92
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
    93
      val txt =
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
    94
        if txt2 = "" then txt1
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
    95
        else if kind = "type" then txt1 ^ " = " ^ txt2
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
    96
        else if kind = "exception" then txt1 ^ " of " ^ txt2
50239
fb579401dc26 tuned signature;
wenzelm
parents: 50201
diff changeset
    97
        else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 40801
diff changeset
    98
        then txt1 ^ ": " ^ txt2
39858
5be7a57c3b4e more robust treatment of symbolic indentifiers (which may contain colons);
wenzelm
parents: 39829
diff changeset
    99
        else txt1 ^ " : " ^ txt2;
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   100
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
   101
57477
c3b5cb53ea79 redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
wenzelm
parents: 57334
diff changeset
   102
      val pos = #pos source1;
56275
600f432ab556 added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents: 56208
diff changeset
   103
      val _ =
57477
c3b5cb53ea79 redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
wenzelm
parents: 57334
diff changeset
   104
        ML_Context.eval_in (SOME ctxt) ML_Compiler.flags pos (ml (toks1, toks2))
c3b5cb53ea79 redundant error position, to ensure the message is attached somewhere, despite the distortion of positions due to glued tokens;
wenzelm
parents: 57334
diff changeset
   105
          handle ERROR msg => error (msg ^ Position.here pos);
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   106
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   107
    in
39869
c269f6bd0a1f more robust index_ML antiquotations: guess name from text (affects infixes and type constructors in particular);
wenzelm
parents: 39858
diff changeset
   108
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   109
      (txt'
38767
d8da44a8dd25 proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents: 37982
diff changeset
   110
      |> (if Config.get ctxt Thy_Output.quotes then quote else I)
55831
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
   111
      |> (if Config.get ctxt Thy_Output.display
3a9386b32211 more markup for ML source;
wenzelm
parents: 55828
diff changeset
   112
          then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   113
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   114
    end);
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   115
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   116
in
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
   117
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   118
val _ =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   119
  Theory.setup
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   120
   (index_ml @{binding index_ML} "" ml_val #>
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   121
    index_ml @{binding index_ML_op} "infix" ml_op #>
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   122
    index_ml @{binding index_ML_type} "type" ml_type #>
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   123
    index_ml @{binding index_ML_exception} "exception" ml_exception #>
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   124
    index_ml @{binding index_ML_structure} "structure" ml_structure #>
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   125
    index_ml @{binding index_ML_functor} "functor" ml_functor);
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
   126
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   127
end;
21375
ae8a112b62d7 Auxiliary antiquotations for Isabelle manuals.
wenzelm
parents:
diff changeset
   128
23846
bfedd1a024fc Added named_thms antiquotation.
berghofe
parents: 23651
diff changeset
   129
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   130
(* named theorems *)
23846
bfedd1a024fc Added named_thms antiquotation.
berghofe
parents: 23651
diff changeset
   131
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   132
val _ =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   133
  Theory.setup (Thy_Output.antiquotation @{binding named_thms}
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   134
    (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   135
    (fn {context = ctxt, ...} =>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   136
      map (apfst (Thy_Output.pretty_thm ctxt))
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   137
      #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   138
      #> (if Config.get ctxt Thy_Output.display
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   139
          then
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   140
            map (fn (p, name) =>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   141
              Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   142
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   143
            #> space_implode "\\par\\smallskip%\n"
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   144
            #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   145
          else
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   146
            map (fn (p, name) =>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   147
              Output.output (Pretty.str_of p) ^
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   148
              "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   149
            #> space_implode "\\par\\smallskip%\n"
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   150
            #> enclose "\\isa{" "}")));
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   151
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   152
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   153
(* theory file *)
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   154
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   155
val _ =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   156
  Theory.setup (Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   157
    (fn {context = ctxt, ...} =>
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 56185
diff changeset
   158
      fn name => (Resources.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name])));
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   159
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   160
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   161
(* Isabelle/jEdit elements *)
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   162
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   163
local
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   164
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   165
fun parse_named a (XML.Elem ((b, props), _)) =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   166
      (case Properties.get props "NAME" of
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   167
        SOME name => if a = b then [name] else []
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   168
      | NONE => [])
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   169
  | parse_named _ _ = [];
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   170
57334
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   171
val isabelle_jedit_actions =
56135
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56070
diff changeset
   172
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/actions.xml"}) of
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   173
    XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   174
  | _ => []);
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   175
57334
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   176
val isabelle_jedit_dockables =
56135
efa24d31e595 added ML antiquotation @{path};
wenzelm
parents: 56070
diff changeset
   177
  (case XML.parse (File.read @{path "~~/src/Tools/jEdit/src/dockables.xml"}) of
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   178
    XML.Elem (("DOCKABLES", _), body) => maps (parse_named "DOCKABLE") body
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   179
  | _ => []);
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   180
57334
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   181
val jedit_actions =
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   182
  Lazy.lazy (fn () =>
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   183
    (case Isabelle_System.bash_output
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   184
      "unzip -p \"$JEDIT_HOME/dist/jedit.jar\" org/gjt/sp/jedit/actions.xml" of
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   185
      (txt, 0) =>
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   186
        (case XML.parse txt of
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   187
          XML.Elem (("ACTIONS", _), body) => maps (parse_named "ACTION") body
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   188
        | _ => [])
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   189
    | (_, rc) => error ("Cannot unzip jedit.jar\nreturn code = " ^ string_of_int rc)));
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   190
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   191
in
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   192
57334
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   193
fun is_action a =
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   194
  member (op =) isabelle_jedit_actions a orelse
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   195
  member (op =) isabelle_jedit_dockables a orelse
54c6b73774d2 formal check of jEdit actions;
wenzelm
parents: 56467
diff changeset
   196
  member (op =) (Lazy.force jedit_actions) a;
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   197
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   198
end;
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   199
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   200
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   201
(* Isabelle/Isar entities (with index) *)
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   202
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   203
local
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   204
26894
1120f6cc10b0 proper checking of various Isar elements;
wenzelm
parents: 26853
diff changeset
   205
fun no_check _ _ = true;
1120f6cc10b0 proper checking of various Isar elements;
wenzelm
parents: 26853
diff changeset
   206
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   207
fun check_command ctxt (name, pos) =
53061
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   208
  is_some (Keyword.command_keyword name) andalso
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   209
    let
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   210
      val markup =
57918
f5d73caba4e5 tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents: 57477
diff changeset
   211
        Outer_Syntax.scan (Keyword.get_lexicons ()) Position.none name
53061
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   212
        |> maps (Outer_Syntax.command_reports (#2 (Outer_Syntax.get_syntax ())))
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   213
        |> map (snd o fst);
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   214
      val _ = Context_Position.reports ctxt (map (pair pos) markup);
53061
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   215
    in true end;
417cb0f713e0 more markup;
wenzelm
parents: 53045
diff changeset
   216
56467
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56304
diff changeset
   217
fun check_system_option ctxt (name, pos) =
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56304
diff changeset
   218
  (Context_Position.report ctxt pos (Options.default_markup (name, pos)); true)
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56304
diff changeset
   219
    handle ERROR _ => false;
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56304
diff changeset
   220
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   221
fun check_tool ctxt (name, pos) =
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   222
  let
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   223
    fun tool dir =
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   224
      let val path = Path.append dir (Path.basic name)
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   225
      in if File.exists path then SOME path else NONE end;
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   226
  in
53045
4c297ee47c28 check_tool wrt. official ISABELLE_TOOLS;
wenzelm
parents: 53044
diff changeset
   227
    (case get_first tool (Path.split (getenv "ISABELLE_TOOLS")) of
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   228
      NONE => false
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   229
    | SOME path => (Context_Position.report ctxt pos (Markup.path (Path.implode path)); true))
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   230
  end;
28237
f1fc11c73569 check setting and tool;
wenzelm
parents: 28218
diff changeset
   231
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   232
val arg = enclose "{" "}" o clean_string;
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   233
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   234
fun entity check markup binding index =
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37198
diff changeset
   235
  Thy_Output.antiquotation
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   236
    (binding |> Binding.map_name (fn name => name ^
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   237
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   238
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   239
    (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   240
      let
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   241
        val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   242
        val hyper_name =
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   243
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   244
        val hyper =
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   245
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   246
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   247
        val idx =
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   248
          (case index of
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   249
            NONE => ""
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   250
          | SOME is_def =>
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   251
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   252
      in
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   253
        if check ctxt (name, pos) then
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   254
          idx ^
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   255
          (Output.output name
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   256
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
38767
d8da44a8dd25 proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents: 37982
diff changeset
   257
            |> (if Config.get ctxt Thy_Output.quotes then quote else I)
d8da44a8dd25 proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents: 37982
diff changeset
   258
            |> (if Config.get ctxt Thy_Output.display
d8da44a8dd25 proper context for various Thy_Output options, via official configuration options in ML and Isar;
wenzelm
parents: 37982
diff changeset
   259
                then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   260
                else hyper o enclose "\\mbox{\\isa{" "}}"))
53044
be27b6be8027 more markup via Name_Space.check;
wenzelm
parents: 52408
diff changeset
   261
        else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
30394
c11a1e65a2ed moved @{ML_functor} and @{ML_text} to Pure;
wenzelm
parents: 30382
diff changeset
   262
      end);
26897
044619358d3a clean_string: cover <;
wenzelm
parents: 26894
diff changeset
   263
26894
1120f6cc10b0 proper checking of various Isar elements;
wenzelm
parents: 26853
diff changeset
   264
fun entity_antiqs check markup kind =
43564
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   265
  entity check markup kind NONE #>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   266
  entity check markup kind (SOME true) #>
9864182c6bad document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents: 43563
diff changeset
   267
  entity check markup kind (SOME false);
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   268
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   269
in
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   270
56059
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   271
val _ =
2390391584c2 some document antiquotations for Isabelle/jEdit elements;
wenzelm
parents: 55997
diff changeset
   272
  Theory.setup
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   273
   (entity_antiqs no_check "" @{binding syntax} #>
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   274
    entity_antiqs check_command "isacommand" @{binding command} #>
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   275
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding keyword} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   276
    entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" @{binding element} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   277
    entity_antiqs (can o Method.check_name) "" @{binding method} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   278
    entity_antiqs (can o Attrib.check_name) "" @{binding attribute} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   279
    entity_antiqs no_check "" @{binding fact} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   280
    entity_antiqs no_check "" @{binding variable} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   281
    entity_antiqs no_check "" @{binding case} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   282
    entity_antiqs (can o Thy_Output.check_command) "" @{binding antiquotation} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   283
    entity_antiqs (can o Thy_Output.check_option) "" @{binding antiquotation_option} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   284
    entity_antiqs no_check "isatt" @{binding setting} #>
56467
8d7d6f17c6a7 more uniform ML/document antiquotations;
wenzelm
parents: 56304
diff changeset
   285
    entity_antiqs check_system_option "isatt" @{binding system_option} #>
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   286
    entity_antiqs no_check "" @{binding inference} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   287
    entity_antiqs no_check "isatt" @{binding executable} #>
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56278
diff changeset
   288
    entity_antiqs check_tool "isatool" @{binding tool} #>
56185
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   289
    entity_antiqs (can o ML_Context.check_antiquotation) "" @{binding ML_antiquotation} #>
851c7b05eb92 more antiquotations;
wenzelm
parents: 56135
diff changeset
   290
    entity_antiqs (K (is_action o #1)) "isatt" @{binding action});
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   291
26742
5a86bc79431c misc cleanup;
wenzelm
parents: 26710
diff changeset
   292
end;
26751
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   293
2b97ea3130c2 added setup for Isar entities;
wenzelm
parents: 26742
diff changeset
   294
end;