haftmann@24584: (* Title: Doc/antiquote_setup.ML wenzelm@21375: ID: $Id$ wenzelm@21375: Author: Makarius wenzelm@21375: wenzelm@21375: Auxiliary antiquotations for Isabelle manuals. wenzelm@21375: *) wenzelm@21375: wenzelm@21375: local wenzelm@21375: wenzelm@22094: structure O = ThyOutput; wenzelm@21375: wenzelm@21375: val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src; wenzelm@21375: wenzelm@21375: fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");" wenzelm@21375: | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ ");"; wenzelm@21375: wenzelm@21375: fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;" wenzelm@21375: | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];"; haftmann@22289: wenzelm@23651: fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ ": exn);" wenzelm@23651: | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ ": " ^ txt2 ^ " -> exn);"; haftmann@22289: wenzelm@21375: fun ml_structure (txt, _) = "functor DUMMY_FUNCTOR() = struct structure DUMMY = " ^ txt ^ " end;" wenzelm@21375: wenzelm@21375: fun ml_functor _ = "val _ = ();"; (*no check!*) wenzelm@21375: wenzelm@21375: val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; wenzelm@21375: wenzelm@21375: fun index_ml kind ml src ctxt (txt1, txt2) = wenzelm@21375: let wenzelm@21375: val txt = if txt2 = "" then txt1 else wenzelm@21375: if kind = "type" wenzelm@21375: then txt1 ^ " = " ^ txt2 haftmann@22289: else if kind = "exception" haftmann@22289: then txt1 ^ " of " ^ txt2 haftmann@22289: else txt1 ^ ": " ^ txt2; wenzelm@21375: val txt' = if kind = "" then txt else kind ^ " " ^ txt; haftmann@22289: val _ = writeln (ml (txt1, txt2)); wenzelm@22094: val _ = ML_Context.use_mltext (ml (txt1, txt2)) false (SOME (Context.Proof ctxt)); wenzelm@21375: in wenzelm@21375: "\\indexml" ^ kind ^ enclose "{" "}" haftmann@22289: (translate_string (fn "_" => "-" | ">" => "$>$" | "#" => "\\#" | c => c) txt1) ^ wenzelm@21375: ((if ! O.source then str_of_source src else txt') wenzelm@21375: |> (if ! O.quotes then quote else I) wenzelm@21375: |> (if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" wenzelm@21375: else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n")) wenzelm@21375: end; wenzelm@21375: wenzelm@21375: fun output_verbatim _ _ = split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"; wenzelm@21375: wenzelm@21375: fun arguments x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x; wenzelm@21375: wenzelm@24204: fun pretty_thy_file name = (ThyLoad.check_thy Path.current name; Pretty.str name); wenzelm@21375: berghofe@23846: berghofe@23846: (* theorems with names *) berghofe@23846: berghofe@23846: fun tweak_line s = berghofe@23846: if ! O.display then s else Symbol.strip_blanks s; berghofe@23846: berghofe@23846: val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines; berghofe@23846: berghofe@23846: fun output_named_list pretty src ctxt xs = berghofe@23846: map (apfst (pretty ctxt)) xs (*always pretty in order to exhibit errors!*) berghofe@23846: |> (if ! O.quotes then map (apfst Pretty.quote) else I) berghofe@23846: |> (if ! O.display then berghofe@23846: map (fn (p, name) => berghofe@23846: Output.output (Pretty.string_of (Pretty.indent (! O.indent) p)) ^ berghofe@23846: "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}") berghofe@23846: #> space_implode "\\par\\smallskip%\n" berghofe@23846: #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" berghofe@23846: else berghofe@23846: map (fn (p, name) => berghofe@23846: Output.output (Pretty.str_of p) ^ berghofe@23846: "\\rulename{" ^ Output.output (Pretty.str_of (pretty_text name)) ^ "}") berghofe@23846: #> space_implode "\\par\\smallskip%\n" berghofe@23846: #> enclose "\\isa{" "}"); berghofe@23846: wenzelm@24920: fun pretty_term ctxt = Syntax.pretty_term ctxt o ProofContext.revert_skolems ctxt; berghofe@23846: fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; berghofe@23846: wenzelm@21375: in wenzelm@21375: wenzelm@21375: val _ = O.add_commands wenzelm@21375: [("index_ML", arguments (index_ml "" ml_val)), wenzelm@21375: ("index_ML_type", arguments (index_ml "type" ml_type)), wenzelm@23651: ("index_ML_exn", arguments (index_ml "exception" ml_exn)), wenzelm@21375: ("index_ML_structure", arguments (index_ml "structure" ml_structure)), wenzelm@21375: ("index_ML_functor", arguments (index_ml "functor" ml_functor)), wenzelm@21375: ("ML_functor", O.args (Scan.lift Args.name) output_verbatim), wenzelm@21375: ("verbatim", O.args (Scan.lift Args.name) output_verbatim), berghofe@23846: ("thy_file", O.args (Scan.lift Args.name) (O.output (K pretty_thy_file))), berghofe@23846: ("named_thms", O.args (Scan.repeat (Attrib.thm -- berghofe@23846: Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")"))) berghofe@23846: (output_named_list pretty_thm))]; wenzelm@21375: wenzelm@21375: end;