haftmann@24584: (* Title: Doc/antiquote_setup.ML wenzelm@21375: ID: $Id$ wenzelm@21375: Author: Makarius wenzelm@21375: wenzelm@26742: Auxiliary antiquotations for the Isabelle manuals. wenzelm@21375: *) wenzelm@21375: wenzelm@26742: structure AntiquoteSetup: sig end = wenzelm@26742: struct wenzelm@21375: wenzelm@22094: structure O = ThyOutput; wenzelm@21375: wenzelm@26742: wenzelm@26742: (* misc utils *) wenzelm@26742: wenzelm@21375: val str_of_source = space_implode " " o map Args.string_of o #2 o #1 o Args.dest_src; wenzelm@21375: wenzelm@26742: fun tweak_line s = wenzelm@26742: if ! O.display then s else Symbol.strip_blanks s; wenzelm@26742: wenzelm@26742: val pretty_text = Pretty.chunks o map Pretty.str o map tweak_line o Library.split_lines; wenzelm@26742: wenzelm@26742: fun pretty_term ctxt t = Syntax.pretty_term (Variable.auto_fixes t ctxt) t; wenzelm@26742: fun pretty_thm ctxt = pretty_term ctxt o Thm.full_prop_of; wenzelm@26742: wenzelm@26742: wenzelm@26742: (* verbatim text *) wenzelm@26742: wenzelm@26742: val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|"; wenzelm@26742: wenzelm@26742: val _ = O.add_commands wenzelm@26742: [("verbatim", O.args (Scan.lift Args.name) (fn _ => fn _ => wenzelm@26742: split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))]; wenzelm@26742: wenzelm@26742: wenzelm@26742: (* ML text *) wenzelm@26742: wenzelm@26742: local wenzelm@26742: 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: fun index_ml kind ml src ctxt (txt1, txt2) = wenzelm@21375: let wenzelm@21375: val txt = if txt2 = "" then txt1 else wenzelm@26742: if kind = "type" then txt1 ^ " = " ^ txt2 wenzelm@26742: else if kind = "exception" 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@26455: val _ = ML_Context.eval_in (SOME (Context.Proof ctxt)) false Position.none (ml (txt1, txt2)); 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: haftmann@26461: fun output_ml ctxt src = haftmann@26461: if ! O.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}" wenzelm@26742: else haftmann@26461: split_lines haftmann@26461: #> map (space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|") haftmann@26461: #> space_implode "\\isasep\\isanewline%\n"; haftmann@26461: wenzelm@26742: fun ml_args x = O.args (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) "")) x; wenzelm@26742: wenzelm@26742: in wenzelm@21375: wenzelm@26742: val _ = O.add_commands wenzelm@26742: [("index_ML", ml_args (index_ml "" ml_val)), wenzelm@26742: ("index_ML_type", ml_args (index_ml "type" ml_type)), wenzelm@26742: ("index_ML_exn", ml_args (index_ml "exception" ml_exn)), wenzelm@26742: ("index_ML_structure", ml_args (index_ml "structure" ml_structure)), wenzelm@26742: ("index_ML_functor", ml_args (index_ml "functor" ml_functor)), wenzelm@26742: ("ML_functor", O.args (Scan.lift Args.name) output_ml), wenzelm@26742: ("ML_text", O.args (Scan.lift Args.name) output_ml)]; wenzelm@21375: wenzelm@26742: end; wenzelm@21375: berghofe@23846: berghofe@23846: (* theorems with names *) berghofe@23846: wenzelm@26742: local berghofe@23846: wenzelm@26742: fun output_named_thms src ctxt xs = wenzelm@26742: map (apfst (pretty_thm 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@21375: in wenzelm@21375: wenzelm@21375: val _ = O.add_commands wenzelm@26742: [("named_thms", O.args (Scan.repeat (Attrib.thm -- wenzelm@26742: Scan.lift (Args.$$$ "(" |-- Args.name --| Args.$$$ ")"))) output_named_thms)]; wenzelm@21375: wenzelm@21375: end; wenzelm@26742: wenzelm@26742: wenzelm@26742: (* theory files *) wenzelm@26742: wenzelm@26742: val _ = O.add_commands wenzelm@26742: [("thy_file", O.args (Scan.lift Args.name) (O.output (fn _ => fn name => wenzelm@26742: (ThyLoad.check_thy Path.current name; Pretty.str name))))]; wenzelm@26742: wenzelm@26742: end;