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