doc-src/antiquote_setup.ML
author wenzelm
Fri Jan 19 22:08:01 2007 +0100 (2007-01-19)
changeset 22094 008794185f4d
parent 22090 bc8aee017f8a
child 22289 41ce4f5c97c9
permissions -rw-r--r--
renamed IsarOutput to ThyOutput;
moved ML context stuff to from Context to ML_Context;
     1 (*  Title:      antiquote_setup.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Auxiliary antiquotations for Isabelle manuals.
     6 *)
     7 
     8 local
     9 
    10 structure O = ThyOutput;
    11 
    12 val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src;
    13 
    14 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
    15   | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");";
    16 
    17 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
    18   | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
    19   
    20 fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;"
    21 
    22 fun ml_functor _ = "val _ = ();";  (*no check!*)
    23 
    24 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
    25 
    26 fun index_ml kind ml src ctxt (txt1, txt2) =
    27   let
    28     val txt = if txt2 = "" then txt1 else
    29       if kind = "type"
    30         then txt1 ^ " = " ^ txt2
    31         else txt1 ^ ": " ^ txt2;
    32     val txt' = if kind = "" then txt else kind ^ " " ^ txt;
    33     val _ = ML_Context.use_mltext (ml (txt1, txt2)) false (SOME (Context.Proof ctxt));
    34   in
    35     "\\indexml" ^ kind ^ enclose "{" "}"
    36       (translate_string (fn "_" => "-" | ">" => "$>$" | c => c) txt1) ^
    37     ((if ! O.source then str_of_source src else txt')
    38     |> (if ! O.quotes then quote else I)
    39     |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
    40     else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
    41   end;
    42 
    43 fun output_verbatim _ _ = split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n";
    44 
    45 fun arguments x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x;
    46 
    47 fun pretty_thy_file name = (ThyLoad.check_thy Path.current name false; Pretty.str name);
    48 
    49 in
    50 
    51 val _ = O.add_commands
    52  [("index_ML", arguments (index_ml "" ml_val)),
    53   ("index_ML_type", arguments (index_ml "type" ml_type)),
    54   ("index_ML_structure", arguments (index_ml "structure" ml_structure)),
    55   ("index_ML_functor", arguments (index_ml "functor" ml_functor)),
    56   ("ML_functor", O.args (Scan.lift Args.name) output_verbatim),
    57   ("verbatim", O.args (Scan.lift Args.name) output_verbatim),
    58   ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file)))];
    59 
    60 end;