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