doc-src/antiquote_setup.ML
author wenzelm
Mon Nov 23 22:47:08 2009 +0100 (2009-11-23)
changeset 33873 e9120a7b2779
parent 31546 d58d6acab331
child 36163 823c9400eb62
permissions -rw-r--r--
more tuning for release;
wenzelm@30394
     1
(*  Title:      doc-src/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@26742
    10
(* misc utils *)
wenzelm@26742
    11
wenzelm@29736
    12
fun translate f = Symbol.explode #> map f #> implode;
wenzelm@29736
    13
wenzelm@29736
    14
val clean_string = translate
wenzelm@26853
    15
  (fn "_" => "\\_"
wenzelm@30120
    16
    | "#" => "\\#"
wenzelm@26897
    17
    | "<" => "$<$"
wenzelm@26768
    18
    | ">" => "$>$"
wenzelm@26768
    19
    | "{" => "\\{"
wenzelm@30120
    20
    | "|" => "$\\mid$"
wenzelm@26768
    21
    | "}" => "\\}"
wenzelm@31546
    22
    | "\<dash>" => "-"
wenzelm@26768
    23
    | c => c);
wenzelm@26751
    24
wenzelm@31546
    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@31546
    31
  | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
wenzelm@26897
    32
wenzelm@26742
    33
wenzelm@26742
    34
(* verbatim text *)
wenzelm@26742
    35
wenzelm@26742
    36
val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
wenzelm@26742
    37
wenzelm@30382
    38
val _ = ThyOutput.antiquotation "verbatim" (Scan.lift Args.name)
wenzelm@30382
    39
  (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
wenzelm@26742
    40
wenzelm@26742
    41
wenzelm@26742
    42
(* ML text *)
wenzelm@26742
    43
wenzelm@26742
    44
local
wenzelm@26742
    45
wenzelm@21375
    46
fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
wenzelm@21375
    47
  | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
wenzelm@21375
    48
wenzelm@21375
    49
fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
wenzelm@21375
    50
  | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
haftmann@22289
    51
wenzelm@23651
    52
fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
wenzelm@23651
    53
  | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
haftmann@22289
    54
wenzelm@30394
    55
fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
wenzelm@21375
    56
wenzelm@30394
    57
fun ml_functor _ = "";  (*no check!*)
wenzelm@21375
    58
wenzelm@30394
    59
fun index_ml name kind ml = ThyOutput.antiquotation name
wenzelm@30394
    60
  (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
wenzelm@30394
    61
  (fn {context = ctxt, ...} => fn (txt1, txt2) =>
wenzelm@30394
    62
    let
wenzelm@30394
    63
      val txt =
wenzelm@30394
    64
        if txt2 = "" then txt1
wenzelm@30394
    65
        else if kind = "type" then txt1 ^ " = " ^ txt2
wenzelm@30394
    66
        else if kind = "exception" then txt1 ^ " of " ^ txt2
wenzelm@30394
    67
        else txt1 ^ ": " ^ txt2;
wenzelm@30394
    68
      val txt' = if kind = "" then txt else kind ^ " " ^ txt;
wenzelm@30394
    69
      val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
wenzelm@30394
    70
      val kind' = if kind = "" then "ML" else "ML " ^ kind;
wenzelm@30394
    71
    in
wenzelm@30394
    72
      "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string txt1 ^ "}" ^
wenzelm@30394
    73
      (txt'
wenzelm@30394
    74
      |> (if ! ThyOutput.quotes then quote else I)
wenzelm@30394
    75
      |> (if ! ThyOutput.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
wenzelm@30394
    76
          else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
wenzelm@30394
    77
    end);
wenzelm@26742
    78
wenzelm@26742
    79
in
wenzelm@21375
    80
wenzelm@30394
    81
val _ = index_ml "index_ML" "" ml_val;
wenzelm@30394
    82
val _ = index_ml "index_ML_type" "type" ml_type;
wenzelm@30394
    83
val _ = index_ml "index_ML_exn" "exception" ml_exn;
wenzelm@30394
    84
val _ = index_ml "index_ML_structure" "structure" ml_structure;
wenzelm@30394
    85
val _ = index_ml "index_ML_functor" "functor" ml_functor;
wenzelm@21375
    86
wenzelm@26742
    87
end;
wenzelm@21375
    88
berghofe@23846
    89
wenzelm@30394
    90
(* named theorems *)
berghofe@23846
    91
wenzelm@30394
    92
val _ = ThyOutput.antiquotation "named_thms"
wenzelm@30394
    93
  (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
wenzelm@30394
    94
  (fn {context = ctxt, ...} =>
wenzelm@30394
    95
    map (apfst (ThyOutput.pretty_thm ctxt))
wenzelm@30394
    96
    #> (if ! ThyOutput.quotes then map (apfst Pretty.quote) else I)
wenzelm@30394
    97
    #> (if ! ThyOutput.display
wenzelm@30394
    98
        then
wenzelm@30394
    99
          map (fn (p, name) =>
wenzelm@30394
   100
            Output.output (Pretty.string_of (Pretty.indent (! ThyOutput.indent) p)) ^
wenzelm@30394
   101
            "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
wenzelm@30394
   102
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   103
          #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   104
        else
wenzelm@30394
   105
          map (fn (p, name) =>
wenzelm@30394
   106
            Output.output (Pretty.str_of p) ^
wenzelm@30394
   107
            "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
wenzelm@30394
   108
          #> space_implode "\\par\\smallskip%\n"
wenzelm@30394
   109
          #> enclose "\\isa{" "}"));
wenzelm@26742
   110
wenzelm@26742
   111
wenzelm@30394
   112
(* theory file *)
wenzelm@26742
   113
wenzelm@30394
   114
val _ = ThyOutput.antiquotation "thy_file" (Scan.lift Args.name)
wenzelm@30394
   115
  (fn _ => fn name => (ThyLoad.check_thy Path.current name; ThyOutput.output [Pretty.str name]));
wenzelm@26742
   116
wenzelm@26751
   117
wenzelm@30394
   118
(* Isabelle/Isar entities (with index) *)
wenzelm@26751
   119
wenzelm@26751
   120
local
wenzelm@26751
   121
wenzelm@26894
   122
fun no_check _ _ = true;
wenzelm@26894
   123
wenzelm@26894
   124
fun thy_check intern defined ctxt =
wenzelm@26894
   125
  let val thy = ProofContext.theory_of ctxt
wenzelm@26894
   126
  in defined thy o intern thy end;
wenzelm@26894
   127
wenzelm@28237
   128
fun check_tool name =
wenzelm@28237
   129
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
wenzelm@28237
   130
wenzelm@26751
   131
val arg = enclose "{" "}" o clean_string;
wenzelm@26751
   132
wenzelm@30394
   133
fun entity check markup kind index =
wenzelm@30394
   134
  ThyOutput.antiquotation
wenzelm@30396
   135
    (translate (fn " " => "_" | c => c) kind ^
wenzelm@30396
   136
      (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))
wenzelm@30394
   137
    (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
wenzelm@30394
   138
    (fn {context = ctxt, ...} => fn (logic, name) =>
wenzelm@30394
   139
      let
wenzelm@30394
   140
        val hyper_name =
wenzelm@30394
   141
          "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@30394
   142
        val hyper =
wenzelm@30394
   143
          enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@30394
   144
          index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@30394
   145
        val idx =
wenzelm@30394
   146
          (case index of
wenzelm@30394
   147
            NONE => ""
wenzelm@30394
   148
          | SOME is_def =>
wenzelm@30394
   149
              "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
wenzelm@30394
   150
      in
wenzelm@30394
   151
        if check ctxt name then
wenzelm@30394
   152
          idx ^
wenzelm@30394
   153
          (Output.output name
wenzelm@30394
   154
            |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@30394
   155
            |> (if ! ThyOutput.quotes then quote else I)
wenzelm@30394
   156
            |> (if ! ThyOutput.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@30394
   157
                else hyper o enclose "\\mbox{\\isa{" "}}"))
wenzelm@30394
   158
        else error ("Bad " ^ kind ^ " " ^ quote name)
wenzelm@30394
   159
      end);
wenzelm@26897
   160
wenzelm@26894
   161
fun entity_antiqs check markup kind =
wenzelm@31297
   162
 ((entity check markup kind NONE);
wenzelm@31297
   163
  (entity check markup kind (SOME true));
wenzelm@31297
   164
  (entity check markup kind (SOME false)));
wenzelm@26751
   165
wenzelm@26751
   166
in
wenzelm@26751
   167
wenzelm@30394
   168
val _ = entity_antiqs no_check "" "syntax";
wenzelm@30394
   169
val _ = entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command";
wenzelm@30394
   170
val _ = entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword";
wenzelm@30394
   171
val _ = entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element";
wenzelm@30394
   172
val _ = entity_antiqs (thy_check Method.intern Method.defined) "" "method";
wenzelm@30394
   173
val _ = entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute";
wenzelm@30394
   174
val _ = entity_antiqs no_check "" "fact";
wenzelm@30394
   175
val _ = entity_antiqs no_check "" "variable";
wenzelm@30394
   176
val _ = entity_antiqs no_check "" "case";
wenzelm@30394
   177
val _ = entity_antiqs (K ThyOutput.defined_command) "" "antiquotation";
wenzelm@30396
   178
val _ = entity_antiqs (K ThyOutput.defined_option) "" "antiquotation option";
wenzelm@30394
   179
val _ = entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting";
wenzelm@30394
   180
val _ = entity_antiqs no_check "" "inference";
wenzelm@30394
   181
val _ = entity_antiqs no_check "isatt" "executable";
wenzelm@30394
   182
val _ = entity_antiqs (K check_tool) "isatt" "tool";
wenzelm@30394
   183
val _ = entity_antiqs (K (File.exists o Path.explode)) "isatt" "file";
wenzelm@30394
   184
val _ = entity_antiqs (K ThyInfo.known_thy) "" "theory";
wenzelm@26751
   185
wenzelm@26742
   186
end;
wenzelm@26751
   187
wenzelm@26751
   188
end;