doc-src/antiquote_setup.ML
author wenzelm
Wed Apr 23 15:04:14 2008 +0200 (2008-04-23)
changeset 26742 5a86bc79431c
parent 26710 f79aa228c582
child 26751 2b97ea3130c2
permissions -rw-r--r--
misc cleanup;
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@21375
    16
val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
wenzelm@21375
    17
wenzelm@26742
    18
fun tweak_line s =
wenzelm@26742
    19
  if ! O.display then s else Symbol.strip_blanks s;
wenzelm@26742
    20
wenzelm@26742
    21
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
wenzelm@26742
    22
wenzelm@26742
    23
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@26742
    24
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
wenzelm@26742
    25
wenzelm@26742
    26
wenzelm@26742
    27
(* verbatim text *)
wenzelm@26742
    28
wenzelm@26742
    29
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    30
wenzelm@26742
    31
val _ = O.add_commands
wenzelm@26742
    32
 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
wenzelm@26742
    33
      split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
wenzelm@26742
    34
wenzelm@26742
    35
wenzelm@26742
    36
(* ML text *)
wenzelm@26742
    37
wenzelm@26742
    38
local
wenzelm@26742
    39
wenzelm@21375
    40
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    41
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    42
wenzelm@21375
    43
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    44
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    45
wenzelm@23651
    46
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    47
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    48
wenzelm@21375
    49
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@21375
    50
wenzelm@21375
    51
fun ml_functor _ = "val _ = ();";  (*no check!*)
wenzelm@21375
    52
wenzelm@21375
    53
fun index_ml kind ml src ctxt (txt1, txt2) =
wenzelm@21375
    54
  let
wenzelm@21375
    55
    val txt = if txt2 = "" then txt1 else
wenzelm@26742
    56
      if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@26742
    57
      else if kind = "exception" then txt1 ^ " of " ^ txt2
haftmann@22289
    58
      else txt1 ^ ": " ^ txt2;
wenzelm@21375
    59
    val txt' = if kind = "" then txt else kind ^ " " ^ txt;
haftmann@22289
    60
    val _ = writeln (ml (txt1, txt2));
wenzelm@26455
    61
    val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
wenzelm@21375
    62
  in
wenzelm@21375
    63
    "\\indexml" ^ kind ^ enclose "{" "}"
haftmann@22289
    64
      (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^
wenzelm@21375
    65
    ((if ! O.source then str_of_source src else txt')
wenzelm@21375
    66
    |> (if ! O.quotes then quote else I)
wenzelm@21375
    67
    |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@21375
    68
    else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@21375
    69
  end;
wenzelm@21375
    70
haftmann@26461
    71
fun output_ml ctxt src =
haftmann@26461
    72
  if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26742
    73
  else
haftmann@26461
    74
    split_lines
haftmann@26461
    75
    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
haftmann@26461
    76
    #> space_implode "\\isasep\\isanewline%\n";
haftmann@26461
    77
wenzelm@26742
    78
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
wenzelm@26742
    79
wenzelm@26742
    80
in
wenzelm@21375
    81
wenzelm@26742
    82
val _ = O.add_commands
wenzelm@26742
    83
 [("index_ML", ml_args (index_ml "" ml_val)),
wenzelm@26742
    84
  ("index_ML_type", ml_args (index_ml "type" ml_type)),
wenzelm@26742
    85
  ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
wenzelm@26742
    86
  ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
wenzelm@26742
    87
  ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
wenzelm@26742
    88
  ("ML_functor", O.args (Scan.lift Args.name) output_ml),
wenzelm@26742
    89
  ("ML_text", O.args (Scan.lift Args.name) output_ml)];
wenzelm@21375
    90
wenzelm@26742
    91
end;
wenzelm@21375
    92
berghofe@23846
    93
berghofe@23846
    94
(* theorems with names *)
berghofe@23846
    95
wenzelm@26742
    96
local
berghofe@23846
    97
wenzelm@26742
    98
fun output_named_thms src ctxt xs =
wenzelm@26742
    99
  map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
berghofe@23846
   100
  |> (if ! O.quotes then map (apfst Pretty.quote) else I)
berghofe@23846
   101
  |> (if ! O.display then
berghofe@23846
   102
    map (fn (p, name) =>
berghofe@23846
   103
      Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
berghofe@23846
   104
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   105
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   106
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
berghofe@23846
   107
  else
berghofe@23846
   108
    map (fn (p, name) =>
berghofe@23846
   109
      Output.output (Pretty.str_of p) ^
berghofe@23846
   110
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   111
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   112
    #> enclose "\\isa{" "}");
berghofe@23846
   113
wenzelm@21375
   114
in
wenzelm@21375
   115
wenzelm@21375
   116
val _ = O.add_commands
wenzelm@26742
   117
 [("named_thms", O.args (Scan.repeat (Attrib.thm --
wenzelm@26742
   118
      Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")"))) output_named_thms)];
wenzelm@21375
   119
wenzelm@21375
   120
end;
wenzelm@26742
   121
wenzelm@26742
   122
wenzelm@26742
   123
(* theory files *)
wenzelm@26742
   124
wenzelm@26742
   125
val _ = O.add_commands
wenzelm@26742
   126
 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
wenzelm@26742
   127
      (ThyLoad.check_thy Path.current name; Pretty.str name))))];
wenzelm@26742
   128
wenzelm@26742
   129
end;