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@26768: val clean_string = translate_string wenzelm@26853: (fn "_" => "\\_" wenzelm@26897: | "<" => "$<$" wenzelm@26768: | ">" => "$>$" wenzelm@26768: | "#" => "\\#" wenzelm@26768: | "{" => "\\{" wenzelm@26768: | "}" => "\\}" wenzelm@26768: | c => c); wenzelm@26751: wenzelm@26897: fun clean_name "\\" = "dots" wenzelm@26897: | clean_name ".." = "ddot" wenzelm@26897: | clean_name "." = "dot" wenzelm@26897: | clean_name "{" = "braceleft" wenzelm@26897: | clean_name "}" = "braceright" wenzelm@26897: | clean_name s = s; wenzelm@26897: 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@26751: "\\indexml" ^ kind ^ enclose "{" "}" (clean_string 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@26751: 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@26751: Scan.lift (Args.parens Args.name))) 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@26751: wenzelm@26751: (* Isar entities (with index) *) wenzelm@26751: wenzelm@26751: local wenzelm@26751: wenzelm@26894: fun no_check _ _ = true; wenzelm@26894: wenzelm@26894: fun thy_check intern defined ctxt = wenzelm@26894: let val thy = ProofContext.theory_of ctxt wenzelm@26894: in defined thy o intern thy end; wenzelm@26894: wenzelm@26751: val arg = enclose "{" "}" o clean_string; wenzelm@26751: wenzelm@26894: fun output_entity check markup index kind ctxt (logic, name) = wenzelm@26897: let wenzelm@26897: val hyper_name = "{" ^ NameSpace.append kind (NameSpace.append logic (clean_name name)) ^ "}"; wenzelm@26897: val hyper = wenzelm@26897: enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #> wenzelm@26897: index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}"; wenzelm@26897: val idx = wenzelm@26897: (case index of wenzelm@26897: NONE => "" wenzelm@26897: | SOME is_def => wenzelm@26897: "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); wenzelm@26897: in wenzelm@26897: if check ctxt name then wenzelm@26897: idx ^ wenzelm@26897: (Output.output name wenzelm@26897: |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}") wenzelm@26897: |> (if ! O.quotes then quote else I) wenzelm@26897: |> (if ! O.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}" wenzelm@26897: else hyper o enclose "\\mbox{\\isa{" "}}")) wenzelm@26897: else error ("Undefined " ^ kind ^ " " ^ quote name) wenzelm@26897: end; wenzelm@26751: wenzelm@26894: fun entity check markup index kind = wenzelm@26751: O.args (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name)) wenzelm@26894: (K (output_entity check markup index kind)); wenzelm@26897: wenzelm@26894: fun entity_antiqs check markup kind = wenzelm@26894: [(kind, entity check markup NONE kind), wenzelm@26897: (kind ^ "_def", entity check markup (SOME true) kind), wenzelm@26894: (kind ^ "_ref", entity check markup (SOME false) kind)]; wenzelm@26751: wenzelm@26751: in wenzelm@26751: wenzelm@26751: val _ = O.add_commands wenzelm@26894: (entity_antiqs no_check "" "syntax" @ wenzelm@26894: entity_antiqs (K (is_some o OuterSyntax.command_keyword)) "isacommand" "command" @ wenzelm@26894: entity_antiqs (K OuterSyntax.is_keyword) "isakeyword" "keyword" @ wenzelm@26894: entity_antiqs (K OuterSyntax.is_keyword) "isakeyword" "element" @ wenzelm@26894: entity_antiqs (thy_check Method.intern Method.defined) "" "method" @ wenzelm@26894: entity_antiqs (thy_check Attrib.intern Attrib.defined) "" "attribute" @ wenzelm@26894: entity_antiqs no_check "" "fact" @ wenzelm@26894: entity_antiqs no_check "" "variable" @ wenzelm@26894: entity_antiqs no_check "" "case" @ wenzelm@26894: entity_antiqs (K ThyOutput.defined_command) "" "antiquotation"); wenzelm@26751: wenzelm@26742: end; wenzelm@26751: wenzelm@26751: end;