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