doc-src/antiquote_setup.ML
author wenzelm
Sat Feb 14 21:34:12 2009 +0100 (2009-02-14)
changeset 29736 ec3fc818b82e
parent 29726 5f91ff5c03a2
child 30120 aaa4667285c8
permissions -rw-r--r--
clean_string/clean_name: proper treatment of \<dash>;
     1 (*  Title:      Doc/antiquote_setup.ML
     2     Author:     Makarius
     3 
     4 Auxiliary antiquotations for the Isabelle manuals.
     5 *)
     6 
     7 structure AntiquoteSetup: sig end =
     8 struct
     9 
    10 structure O = ThyOutput;
    11 
    12 
    13 (* misc utils *)
    14 
    15 fun translate f = Symbol.explode #> map f #> implode;
    16 
    17 val clean_string = translate
    18   (fn "_" => "\\_"
    19     | "<" => "$<$"
    20     | ">" => "$>$"
    21     | "#" => "\\#"
    22     | "{" => "\\{"
    23     | "}" => "\\}"
    24     | "\\<dash>" => "-"
    25     | c => c);
    26 
    27 fun clean_name "\\<dots>" = "dots"
    28   | clean_name ".." = "ddot"
    29   | clean_name "." = "dot"
    30   | clean_name "_" = "underscore"
    31   | clean_name "{" = "braceleft"
    32   | clean_name "}" = "braceright"
    33   | clean_name s = s |> translate (fn "_" => "-" | "\\<dash>" => "-" | c => c);
    34 
    35 
    36 (* verbatim text *)
    37 
    38 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    39 
    40 val _ = O.add_commands
    41  [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ =>
    42       split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))];
    43 
    44 
    45 (* ML text *)
    46 
    47 local
    48 
    49 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    50   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    51 
    52 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    53   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    54 
    55 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);"
    56   | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);";
    57 
    58 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    59 
    60 fun ml_functor _ = "val _ = ();";  (*no check!*)
    61 
    62 fun index_ml kind ml src ctxt (txt1, txt2) =
    63   let
    64     val txt = if txt2 = "" then txt1 else
    65       if kind = "type" then txt1 ^ " = " ^ txt2
    66       else if kind = "exception" then txt1 ^ " of " ^ txt2
    67       else txt1 ^ ": " ^ txt2;
    68     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    69     val _ = writeln (ml (txt1, txt2));
    70     val _ = ML_Context.eval_in (SOME ctxt) false Position.none (ml (txt1, txt2));
    71   in
    72     "\\indexml" ^ kind ^ enclose "{" "}" (clean_string txt1) ^
    73     (txt'
    74     |> (if ! O.quotes then quote else I)
    75     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    76         else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    77   end;
    78 
    79 fun output_ml ctxt src =
    80   if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    81   else
    82     split_lines
    83     #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|")
    84     #> space_implode "\\isasep\\isanewline%\n";
    85 
    86 fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    87 
    88 in
    89 
    90 val _ = O.add_commands
    91  [("index_ML", ml_args (index_ml "" ml_val)),
    92   ("index_ML_type", ml_args (index_ml "type" ml_type)),
    93   ("index_ML_exn", ml_args (index_ml "exception" ml_exn)),
    94   ("index_ML_structure", ml_args (index_ml "structure" ml_structure)),
    95   ("index_ML_functor", ml_args (index_ml "functor" ml_functor)),
    96   ("ML_functor", O.args (Scan.lift Args.name) output_ml),
    97   ("ML_text", O.args (Scan.lift Args.name) output_ml)];
    98 
    99 end;
   100 
   101 
   102 (* theorems with names *)
   103 
   104 local
   105 
   106 fun output_named_thms src ctxt xs =
   107   map (apfst (ThyOutput.pretty_thm ctxt)) xs        (*always pretty in order to exhibit errors!*)
   108   |> (if ! O.quotes then map (apfst Pretty.quote) else I)
   109   |> (if ! O.display then
   110     map (fn (p, name) =>
   111       Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^
   112       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   113     #> space_implode "\\par\\smallskip%\n"
   114     #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   115   else
   116     map (fn (p, name) =>
   117       Output.output (Pretty.str_of p) ^
   118       "\\rulename{" ^ Output.output (Pretty.str_of (ThyOutput.pretty_text name)) ^ "}")
   119     #> space_implode "\\par\\smallskip%\n"
   120     #> enclose "\\isa{" "}");
   121 
   122 in
   123 
   124 val _ = O.add_commands
   125  [("named_thms", O.args (Scan.repeat (Attrib.thm --
   126       Scan.lift (Args.parens Args.name))) output_named_thms)];
   127 
   128 end;
   129 
   130 
   131 (* theory files *)
   132 
   133 val _ = O.add_commands
   134  [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name =>
   135       (ThyLoad.check_thy Path.current name; Pretty.str name))))];
   136 
   137 
   138 (* Isabelle entities (with index) *)
   139 
   140 local
   141 
   142 fun no_check _ _ = true;
   143 
   144 fun thy_check intern defined ctxt =
   145   let val thy = ProofContext.theory_of ctxt
   146   in defined thy o intern thy end;
   147 
   148 fun check_tool name =
   149   File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
   150 
   151 val arg = enclose "{" "}" o clean_string;
   152 
   153 fun output_entity check markup index kind ctxt (logic, name) =
   154   let
   155     val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}";
   156     val hyper =
   157       enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
   158       index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
   159     val idx =
   160       (case index of
   161         NONE => ""
   162       | SOME is_def =>
   163           "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
   164   in
   165     if check ctxt name then
   166       idx ^
   167       (Output.output name
   168         |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
   169         |> (if ! O.quotes then quote else I)
   170         |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
   171             else hyper o enclose "\\mbox{\\isa{" "}}"))
   172     else error ("Bad " ^ kind ^ " " ^ quote name)
   173   end;
   174 
   175 fun entity check markup index kind =
   176   O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name))
   177     (K (output_entity check markup index kind));
   178 
   179 fun entity_antiqs check markup kind =
   180  [(kind, entity check markup NONE kind),
   181   (kind ^ "_def", entity check markup (SOME true) kind),
   182   (kind ^ "_ref", entity check markup (SOME false) kind)];
   183 
   184 in
   185 
   186 val _ = O.add_commands
   187  (entity_antiqs no_check "" "syntax" @
   188   entity_antiqs (K (is_some o OuterKeyword.command_keyword)) "isacommand" "command" @
   189   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "keyword" @
   190   entity_antiqs (K OuterKeyword.is_keyword) "isakeyword" "element" @
   191   entity_antiqs (thy_check Method.intern Method.defined) "" "method" @
   192   entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @
   193   entity_antiqs no_check "" "fact" @
   194   entity_antiqs no_check "" "variable" @
   195   entity_antiqs no_check "" "case" @
   196   entity_antiqs (K ThyOutput.defined_command) "" "antiquotation" @
   197   entity_antiqs (fn _ => fn name => is_some (OS.Process.getEnv name)) "isatt" "setting" @
   198   entity_antiqs no_check "" "inference" @
   199   entity_antiqs no_check "isatt" "executable" @
   200   entity_antiqs (K check_tool) "isatt" "tool" @
   201   entity_antiqs (K (File.exists o Path.explode)) "isatt" "file" @
   202   entity_antiqs (K ThyInfo.known_thy) "" "theory");
   203 
   204 end;
   205 
   206 end;