doc-src/antiquote_setup.ML
author wenzelm
Tue May 06 00:08:52 2008 +0200 (2008-05-06)
changeset 26785 e77f9b8c7514
parent 26774 e258050a3076
child 26843 612ca951afee
permissions -rw-r--r--
element: isakeyword markup;
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@26768
    17
  (fn "_" => "-"
wenzelm@26768
    18
    | ">" => "$>$"
wenzelm@26768
    19
    | "#" => "\\#"
wenzelm@26768
    20
    | "{" => "\\{"
wenzelm@26768
    21
    | "}" => "\\}"
wenzelm@26768
    22
    | c => c);
wenzelm@26751
    23
wenzelm@21375
    24
val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
wenzelm@21375
    25
wenzelm@26742
    26
fun tweak_line s =
wenzelm@26742
    27
  if ! O.display then s else Symbol.strip_blanks s;
wenzelm@26742
    28
wenzelm@26742
    29
val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines;
wenzelm@26742
    30
wenzelm@26742
    31
fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t;
wenzelm@26742
    32
fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of;
wenzelm@26742
    33
wenzelm@26742
    34
wenzelm@26742
    35
(* verbatim text *)
wenzelm@26742
    36
wenzelm@26742
    37
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    38
wenzelm@26742
    39
val _ = O.add_commands
wenzelm@26742
    40
 [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
wenzelm@26742
    41
      split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
wenzelm@26742
    42
wenzelm@26742
    43
wenzelm@26742
    44
(* ML text *)
wenzelm@26742
    45
wenzelm@26742
    46
local
wenzelm@26742
    47
wenzelm@21375
    48
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    49
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    50
wenzelm@21375
    51
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    52
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    53
wenzelm@23651
    54
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    55
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    56
wenzelm@21375
    57
fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
wenzelm@21375
    58
wenzelm@21375
    59
fun ml_functor _ = "val _ = ();";  (*no check!*)
wenzelm@21375
    60
wenzelm@21375
    61
fun index_ml kind ml src ctxt (txt1, txt2) =
wenzelm@21375
    62
  let
wenzelm@21375
    63
    val txt = if txt2 = "" then txt1 else
wenzelm@26742
    64
      if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@26742
    65
      else if kind = "exception" then txt1 ^ " of " ^ txt2
haftmann@22289
    66
      else txt1 ^ ": " ^ txt2;
wenzelm@21375
    67
    val txt' = if kind = "" then txt else kind ^ " " ^ txt;
haftmann@22289
    68
    val _ = writeln (ml (txt1, txt2));
wenzelm@26455
    69
    val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2));
wenzelm@21375
    70
  in
wenzelm@26751
    71
    "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
wenzelm@21375
    72
    ((if ! O.source then str_of_source src else txt')
wenzelm@21375
    73
    |> (if ! O.quotes then quote else I)
wenzelm@21375
    74
    |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26751
    75
        else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@21375
    76
  end;
wenzelm@21375
    77
haftmann@26461
    78
fun output_ml ctxt src =
haftmann@26461
    79
  if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@26742
    80
  else
haftmann@26461
    81
    split_lines
haftmann@26461
    82
    #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
haftmann@26461
    83
    #> space_implode "\\isasep\\isanewline%\n";
haftmann@26461
    84
wenzelm@26742
    85
fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
wenzelm@26742
    86
wenzelm@26742
    87
in
wenzelm@21375
    88
wenzelm@26742
    89
val _ = O.add_commands
wenzelm@26742
    90
 [("index_ML", ml_args (index_ml "" ml_val)),
wenzelm@26742
    91
  ("index_ML_type", ml_args (index_ml "type" ml_type)),
wenzelm@26742
    92
  ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
wenzelm@26742
    93
  ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
wenzelm@26742
    94
  ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
wenzelm@26742
    95
  ("ML_functor", O.args (Scan.lift Args.name) output_ml),
wenzelm@26742
    96
  ("ML_text", O.args (Scan.lift Args.name) output_ml)];
wenzelm@21375
    97
wenzelm@26742
    98
end;
wenzelm@21375
    99
berghofe@23846
   100
berghofe@23846
   101
(* theorems with names *)
berghofe@23846
   102
wenzelm@26742
   103
local
berghofe@23846
   104
wenzelm@26742
   105
fun output_named_thms src ctxt xs =
wenzelm@26742
   106
  map (apfst (pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
berghofe@23846
   107
  |> (if ! O.quotes then map (apfst Pretty.quote) else I)
berghofe@23846
   108
  |> (if ! O.display then
berghofe@23846
   109
    map (fn (p, name) =>
berghofe@23846
   110
      Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
berghofe@23846
   111
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   112
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   113
    #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
berghofe@23846
   114
  else
berghofe@23846
   115
    map (fn (p, name) =>
berghofe@23846
   116
      Output.output (Pretty.str_of p) ^
berghofe@23846
   117
      "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}")
berghofe@23846
   118
    #> space_implode "\\par\\smallskip%\n"
berghofe@23846
   119
    #> enclose "\\isa{" "}");
berghofe@23846
   120
wenzelm@21375
   121
in
wenzelm@21375
   122
wenzelm@21375
   123
val _ = O.add_commands
wenzelm@26742
   124
 [("named_thms", O.args (Scan.repeat (Attrib.thm --
wenzelm@26751
   125
      Scan.lift (Args.parens Args.name))) output_named_thms)];
wenzelm@21375
   126
wenzelm@21375
   127
end;
wenzelm@26742
   128
wenzelm@26742
   129
wenzelm@26742
   130
(* theory files *)
wenzelm@26742
   131
wenzelm@26742
   132
val _ = O.add_commands
wenzelm@26742
   133
 [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
wenzelm@26742
   134
      (ThyLoad.check_thy Path.current name; Pretty.str name))))];
wenzelm@26742
   135
wenzelm@26751
   136
wenzelm@26751
   137
(* Isar entities (with index) *)
wenzelm@26751
   138
wenzelm@26751
   139
local
wenzelm@26751
   140
wenzelm@26751
   141
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   142
wenzelm@26756
   143
fun output_entity markup index kind src ctxt (logic, name) =
wenzelm@26751
   144
  (case index of
wenzelm@26751
   145
    NONE => ""
wenzelm@26751
   146
  | SOME is_def =>
wenzelm@26751
   147
      "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name)
wenzelm@26751
   148
  ^
wenzelm@26751
   149
  (Output.output (if ! O.source then str_of_source src else name)
wenzelm@26756
   150
    |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@26751
   151
    |> (if ! O.quotes then quote else I)
wenzelm@26751
   152
    |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@26774
   153
        else enclose "\\mbox{\\isa{" "}}"));
wenzelm@26751
   154
wenzelm@26756
   155
fun entity markup index kind =
wenzelm@26751
   156
  O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@26756
   157
    (output_entity markup index kind);
wenzelm@26751
   158
    
wenzelm@26756
   159
fun entity_antiqs markup kind =
wenzelm@26756
   160
 [(kind, entity markup NONE kind),
wenzelm@26756
   161
  (kind ^ "_def", entity markup (SOME true) kind), 
wenzelm@26756
   162
  (kind ^ "_ref", entity markup (SOME false) kind)];
wenzelm@26751
   163
wenzelm@26751
   164
in
wenzelm@26751
   165
wenzelm@26751
   166
val _ = O.add_commands
wenzelm@26756
   167
 (entity_antiqs "" "syntax" @
wenzelm@26756
   168
  entity_antiqs "isacommand" "command" @
wenzelm@26756
   169
  entity_antiqs "isakeyword" "keyword" @
wenzelm@26785
   170
  entity_antiqs "isakeyword" "element" @
wenzelm@26756
   171
  entity_antiqs "" "method" @
wenzelm@26756
   172
  entity_antiqs "" "attribute" @
wenzelm@26756
   173
  entity_antiqs "" "fact" @
wenzelm@26756
   174
  entity_antiqs "" "variable" @
wenzelm@26756
   175
  entity_antiqs "" "case" @
wenzelm@26756
   176
  entity_antiqs "" "antiquotation");
wenzelm@26751
   177
wenzelm@26742
   178
end;
wenzelm@26751
   179
wenzelm@26751
   180
end;