File ‹~~/src/Doc/antiquote_setup.ML›
structure Antiquote_Setup: sig end =
struct
fun translate f = Symbol.explode #> map f #> implode;
val clean_string = translate
(fn "_" => "\\_"
| "#" => "\\#"
| "$" => "\\$"
| "%" => "\\%"
| "<" => "$<$"
| ">" => "$>$"
| "{" => "\\{"
| "|" => "$\\mid$"
| "}" => "\\}"
| "‐" => "-"
| c => c);
fun clean_name "…" = "dots"
| clean_name ".." = "ddot"
| clean_name "." = "dot"
| clean_name "_" = "underscore"
| clean_name "{" = "braceleft"
| clean_name "}" = "braceright"
| clean_name s = s |> translate (fn "_" => "-" | "‐" => "-" | c => c);
val _ =
Theory.setup (Document_Output.antiquotation_raw \<^binding>‹named_thms›
(Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
(fn ctxt =>
map (fn (thm, name) =>
Latex.output_
(Document_Antiquotation.format ctxt
(Document_Antiquotation.delimit ctxt (Document_Output.pretty_thm ctxt thm))) ^
enclose "\\rulename{" "}" (Latex.output_ name))
#> space_implode "\\par\\smallskip%\n"
#> Latex.string
#> Document_Output.isabelle ctxt));
local
fun no_check (_: Proof.context) (name, _: Position.T) = name;
fun check_keyword ctxt (name, pos) =
if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name
else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
fun check_system_option ctxt arg =
(Completion.check_option (Options.default ()) ctxt arg; true)
handle ERROR _ => false;
val arg = enclose "{" "}" o clean_string;
fun entity check markup binding index =
Document_Output.antiquotation_raw
(binding |> Binding.map_name (fn name => name ^
(case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
(Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name_position))
(fn ctxt => fn (logic, (name, pos)) =>
let
val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding);
val hyper_name =
"{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
val hyper =
enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
val idx =
(case index of
NONE => ""
| SOME is_def =>
"\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
val _ =
if Context_Position.is_reported ctxt pos then ignore (check ctxt (name, pos)) else ();
val latex =
idx ^
(Latex.output_ name
|> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
|> hyper o enclose "\\mbox{\\isa{" "}}");
in Latex.string latex end);
fun entity_antiqs check markup kind =
entity check markup kind NONE #>
entity check markup kind (SOME true) #>
entity check markup kind (SOME false);
in
val _ =
Theory.setup
(entity_antiqs no_check "" \<^binding>‹syntax› #>
entity_antiqs Outer_Syntax.check_command "isacommand" \<^binding>‹command› #>
entity_antiqs check_keyword "isakeyword" \<^binding>‹keyword› #>
entity_antiqs check_keyword "isakeyword" \<^binding>‹element› #>
entity_antiqs Method.check_name "" \<^binding>‹method› #>
entity_antiqs Attrib.check_name "" \<^binding>‹attribute› #>
entity_antiqs no_check "" \<^binding>‹fact› #>
entity_antiqs no_check "" \<^binding>‹variable› #>
entity_antiqs no_check "" \<^binding>‹case› #>
entity_antiqs Document_Antiquotation.check "" \<^binding>‹antiquotation› #>
entity_antiqs Document_Antiquotation.check_option "" \<^binding>‹antiquotation_option› #>
entity_antiqs Document_Marker.check "" \<^binding>‹document_marker› #>
entity_antiqs no_check "isasystem" \<^binding>‹setting› #>
entity_antiqs check_system_option "isasystem" \<^binding>‹system_option› #>
entity_antiqs no_check "" \<^binding>‹inference› #>
entity_antiqs no_check "isasystem" \<^binding>‹executable› #>
entity_antiqs Isabelle_Tool.check "isatool" \<^binding>‹tool› #>
entity_antiqs ML_Context.check_antiquotation "" \<^binding>‹ML_antiquotation› #>
entity_antiqs (K JEdit.check_action) "isasystem" \<^binding>‹action› #>
entity_antiqs Markup_Kind.check_notation_kind "" \<^binding>‹notation_kind›);
end;
val _ =
Theory.setup (Document_Output.antiquotation_raw \<^binding>‹show_symbols› (Scan.succeed ())
(fn _ => fn _ =>
let
val symbol_name =
unprefix "\\newcommand{\\isasym"
#> raw_explode
#> take_prefix Symbol.is_ascii_letter
#> implode;
val symbols =
File.read 🗏‹~~/lib/texinputs/isabellesym.sty›
|> split_lines
|> map_filter (fn line =>
(case try symbol_name line of
NONE => NONE
| SOME "" => NONE
| SOME name => SOME ("\\verb,\\" ^ "<" ^ name ^ ">, & {\\isasym" ^ name ^ "}")));
val eol = "\\\\\n";
fun table (a :: b :: rest) = a ^ " & " ^ b ^ eol :: table rest
| table [a] = [a ^ eol]
| table [] = [];
in
Latex.string
("\\begin{supertabular}{ll@{\\qquad}ll}\n" ^ implode (table symbols) ^
"\\end{supertabular}\n")
end))
end;