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