doc-src/antiquote_setup.ML
author blanchet
Wed Mar 04 10:45:52 2009 +0100 (2009-03-04)
changeset 30240 5b25fee0362c
parent 28759 8358fabeea95
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
Merge.
haftmann@24584
     1
(*  Title:      Doc/antiquote_setup.ML
wenzelm@21375
     2
    Author:     Makarius
wenzelm@21375
     3
wenzelm@26742
     4
Auxiliary antiquotations for the Isabelle manuals.
wenzelm@21375
     5
*)
wenzelm@21375
     6
wenzelm@26742
     7
structure AntiquoteSetup: sig end =
wenzelm@26742
     8
struct
wenzelm@21375
     9
wenzelm@22094
    10
structure O = ThyOutput;
wenzelm@21375
    11
wenzelm@26742
    12
wenzelm@26742
    13
(* misc utils *)
wenzelm@26742
    14
blanchet@30240
    15
fun translate f = Symbol.explode #> map f #> implode;
blanchet@30240
    16
blanchet@30240
    17
val clean_string = translate
wenzelm@26853
    18
  (fn "_" => "\\_"
blanchet@30240
    19
    | "#" => "\\#"
wenzelm@26897
    20
    | "<" => "$<$"
wenzelm@26768
    21
    | ">" => "$>$"
wenzelm@26768
    22
    | "{" => "\\{"
blanchet@30240
    23
    | "|" => "$\\mid$"
wenzelm@26768
    24
    | "}" => "\\}"
blanchet@30240
    25
    | "\\<dash>" => "-"
wenzelm@26768
    26
    | c => c);
wenzelm@26751
    27
wenzelm@26897
    28
fun clean_name "\\<dots>" = "dots"
wenzelm@26897
    29
  | clean_name ".." = "ddot"
wenzelm@26897
    30
  | clean_name "." = "dot"
wenzelm@26910
    31
  | clean_name "_" = "underscore"
wenzelm@26897
    32
  | clean_name "{" = "braceleft"
wenzelm@26897
    33
  | clean_name "}" = "braceright"
blanchet@30240
    34
  | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
wenzelm@26897
    35
wenzelm@26742
    36
wenzelm@26742
    37
(* verbatim text *)
wenzelm@26742
    38
wenzelm@26742
    39
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    40
wenzelm@26742
    41
val _ = O.add_commands
wenzelm@26742
    42
 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
wenzelm@26742
    43
      split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
wenzelm@26742
    44
wenzelm@26742
    45
wenzelm@26742
    46
(* ML text *)
wenzelm@26742
    47
wenzelm@26742
    48
local
wenzelm@26742
    49
wenzelm@21375
    50
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    51
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    52
wenzelm@21375
    53
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    54
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    55
wenzelm@23651
    56
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    57
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    58
wenzelm@21375
    59
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@21375
    60
wenzelm@21375
    61
fun ml_functor _ = "val _ = ();";  (*no check!*)
wenzelm@21375
    62
wenzelm@21375
    63
fun index_ml kind ml src ctxt (txt1, txt2) =
wenzelm@21375
    64
  let
wenzelm@21375
    65
    val txt = if txt2 = "" then txt1 else
wenzelm@26742
    66
      if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@26742
    67
      else if kind = "exception" then txt1 ^ " of " ^ txt2
haftmann@22289
    68
      else txt1 ^ ": " ^ txt2;
wenzelm@21375
    69
    val txt' = if kind = "" then txt else kind ^ " " ^ txt;
haftmann@22289
    70
    val _ = writeln (ml (txt1, txt2));
wenzelm@28273
    71
    val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
blanchet@30240
    72
    val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@21375
    73
  in
blanchet@30240
    74
    "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
wenzelm@28759
    75
    (txt'
wenzelm@21375
    76
    |> (if ! O.quotes then quote else I)
wenzelm@21375
    77
    |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26751
    78
        else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@21375
    79
  end;
wenzelm@21375
    80
haftmann@26461
    81
fun output_ml ctxt src =
haftmann@26461
    82
  if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26742
    83
  else
haftmann@26461
    84
    split_lines
haftmann@26461
    85
    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
haftmann@26461
    86
    #> space_implode "\\isasep\\isanewline%\n";
haftmann@26461
    87
wenzelm@26742
    88
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
wenzelm@26742
    89
wenzelm@26742
    90
in
wenzelm@21375
    91
wenzelm@26742
    92
val _ = O.add_commands
wenzelm@26742
    93
 [("index_ML", ml_args (index_ml "" ml_val)),
wenzelm@26742
    94
  ("index_ML_type", ml_args (index_ml "type" ml_type)),
wenzelm@26742
    95
  ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
wenzelm@26742
    96
  ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
wenzelm@26742
    97
  ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
wenzelm@26742
    98
  ("ML_functor", O.args (Scan.lift Args.name) output_ml),
wenzelm@26742
    99
  ("ML_text", O.args (Scan.lift Args.name) output_ml)];
wenzelm@21375
   100
wenzelm@26742
   101
end;
wenzelm@21375
   102
berghofe@23846
   103
berghofe@23846
   104
(* theorems with names *)
berghofe@23846
   105
wenzelm@26742
   106
local
berghofe@23846
   107
wenzelm@26742
   108
fun output_named_thms src ctxt xs =
wenzelm@28644
   109
  map (apfst (ThyOutput.pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
berghofe@23846
   110
  |> (if ! O.quotes then map (apfst Pretty.quote) else I)
berghofe@23846
   111
  |> (if ! O.display then
berghofe@23846
   112
    map (fn (p, name) =>
berghofe@23846
   113
      Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
wenzelm@28644
   114
      "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
berghofe@23846
   115
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   116
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
berghofe@23846
   117
  else
berghofe@23846
   118
    map (fn (p, name) =>
berghofe@23846
   119
      Output.output (Pretty.str_of p) ^
wenzelm@28644
   120
      "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
berghofe@23846
   121
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   122
    #> enclose "\\isa{" "}");
berghofe@23846
   123
wenzelm@21375
   124
in
wenzelm@21375
   125
wenzelm@21375
   126
val _ = O.add_commands
wenzelm@26742
   127
 [("named_thms", O.args (Scan.repeat (Attrib.thm --
wenzelm@26751
   128
      Scan.lift (Args.parens Args.name))) output_named_thms)];
wenzelm@21375
   129
wenzelm@21375
   130
end;
wenzelm@26742
   131
wenzelm@26742
   132
wenzelm@26742
   133
(* theory files *)
wenzelm@26742
   134
wenzelm@26742
   135
val _ = O.add_commands
wenzelm@26742
   136
 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
wenzelm@26742
   137
      (ThyLoad.check_thy Path.current name; Pretty.str name))))];
wenzelm@26742
   138
wenzelm@26751
   139
wenzelm@28218
   140
(* Isabelle entities (with index) *)
wenzelm@26751
   141
wenzelm@26751
   142
local
wenzelm@26751
   143
wenzelm@26894
   144
fun no_check _ _ = true;
wenzelm@26894
   145
wenzelm@26894
   146
fun thy_check intern defined ctxt =
wenzelm@26894
   147
  let val thy = ProofContext.theory_of ctxt
wenzelm@26894
   148
  in defined thy o intern thy end;
wenzelm@26894
   149
wenzelm@28237
   150
fun check_tool name =
wenzelm@28237
   151
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
wenzelm@28237
   152
wenzelm@26751
   153
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   154
wenzelm@26894
   155
fun output_entity check markup index kind ctxt (logic, name) =
wenzelm@26897
   156
  let
wenzelm@26897
   157
    val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}";
wenzelm@26897
   158
    val hyper =
wenzelm@26897
   159
      enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@26897
   160
      index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@26897
   161
    val idx =
wenzelm@26897
   162
      (case index of
wenzelm@26897
   163
        NONE => ""
wenzelm@26897
   164
      | SOME is_def =>
wenzelm@26897
   165
          "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@26897
   166
  in
wenzelm@26897
   167
    if check ctxt name then
wenzelm@26897
   168
      idx ^
wenzelm@26897
   169
      (Output.output name
wenzelm@26897
   170
        |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@26897
   171
        |> (if ! O.quotes then quote else I)
wenzelm@26897
   172
        |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@26897
   173
            else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@28237
   174
    else error ("Bad " ^ kind ^ " " ^ quote name)
wenzelm@26897
   175
  end;
wenzelm@26751
   176
wenzelm@26894
   177
fun entity check markup index kind =
wenzelm@26751
   178
  O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@26894
   179
    (K (output_entity check markup index kind));
wenzelm@26897
   180
wenzelm@26894
   181
fun entity_antiqs check markup kind =
wenzelm@26894
   182
 [(kind, entity check markup NONE kind),
wenzelm@26897
   183
  (kind ^ "_def", entity check markup (SOME true) kind),
wenzelm@26894
   184
  (kind ^ "_ref", entity check markup (SOME false) kind)];
wenzelm@26751
   185
wenzelm@26751
   186
in
wenzelm@26751
   187
wenzelm@26751
   188
val _ = O.add_commands
wenzelm@26894
   189
 (entity_antiqs no_check "" "syntax" @
wenzelm@27353
   190
  entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
wenzelm@27353
   191
  entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
wenzelm@27353
   192
  entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
wenzelm@26894
   193
  entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
wenzelm@26894
   194
  entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
wenzelm@26894
   195
  entity_antiqs no_check "" "fact" @
wenzelm@26894
   196
  entity_antiqs no_check "" "variable" @
wenzelm@26894
   197
  entity_antiqs no_check "" "case" @
wenzelm@28217
   198
  entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
wenzelm@28237
   199
  entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
blanchet@30240
   200
  entity_antiqs no_check "" "inference" @
wenzelm@28217
   201
  entity_antiqs no_check "isatt" "executable" @
wenzelm@28237
   202
  entity_antiqs (K check_tool) "isatt" "tool" @
haftmann@28399
   203
  entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
haftmann@28399
   204
  entity_antiqs (K ThyInfo.known_thy) "" "theory");
wenzelm@26751
   205
wenzelm@26742
   206
end;
wenzelm@26751
   207
wenzelm@26751
   208
end;