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