| author | wenzelm | 
| Fri, 03 Jan 2025 22:35:28 +0100 | |
| changeset 81711 | a55b236f9e1d | 
| parent 81114 | 6538332c08e0 | 
| permissions | -rw-r--r-- | 
| 48985 | 1 | (* Title: Doc/antiquote_setup.ML | 
| 21375 | 2 | Author: Makarius | 
| 3 | ||
| 26742 | 4 | Auxiliary antiquotations for the Isabelle manuals. | 
| 21375 | 5 | *) | 
| 6 | ||
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 7 | structure Antiquote_Setup: sig end = | 
| 26742 | 8 | struct | 
| 21375 | 9 | |
| 26742 | 10 | (* misc utils *) | 
| 11 | ||
| 29736 
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
 wenzelm parents: 
29726diff
changeset | 12 | fun translate f = Symbol.explode #> map f #> implode; | 
| 
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
 wenzelm parents: 
29726diff
changeset | 13 | |
| 
ec3fc818b82e
clean_string/clean_name: proper treatment of \<dash>;
 wenzelm parents: 
29726diff
changeset | 14 | val clean_string = translate | 
| 26853 
52cb0e965041
clean_string: map "_" to "\\_" (best used with underscore.sty);
 wenzelm parents: 
26843diff
changeset | 15 | (fn "_" => "\\_" | 
| 30120 
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
 wenzelm parents: 
29736diff
changeset | 16 | | "#" => "\\#" | 
| 52408 | 17 | | "$" => "\\$" | 
| 18 | | "%" => "\\%" | |
| 26897 | 19 | | "<" => "$<$" | 
| 26768 | 20 | | ">" => "$>$" | 
| 21 |     | "{" => "\\{"
 | |
| 30120 
aaa4667285c8
uniform treatment of ML indexing, using general \indexdef macro for formal Isabelle/Isar entities;
 wenzelm parents: 
29736diff
changeset | 22 | | "|" => "$\\mid$" | 
| 26768 | 23 | | "}" => "\\}" | 
| 42666 | 24 | | "\<hyphen>" => "-" | 
| 26768 | 25 | | c => c); | 
| 26751 | 26 | |
| 31546 | 27 | fun clean_name "\<dots>" = "dots" | 
| 26897 | 28 | | clean_name ".." = "ddot" | 
| 29 | | clean_name "." = "dot" | |
| 26910 | 30 | | clean_name "_" = "underscore" | 
| 26897 | 31 |   | clean_name "{" = "braceleft"
 | 
| 32 | | clean_name "}" = "braceright" | |
| 42666 | 33 | | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c); | 
| 26897 | 34 | |
| 26742 | 35 | |
| 30394 | 36 | (* named theorems *) | 
| 23846 | 37 | |
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 38 | val _ = | 
| 73761 | 39 | Theory.setup (Document_Output.antiquotation_raw \<^binding>\<open>named_thms\<close> | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 40 | (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name))) | 
| 67463 | 41 | (fn ctxt => | 
| 42 | map (fn (thm, name) => | |
| 80846 
9ed32b8a03a9
clarified print mode "latex": no longer impact Output/Markup/Pretty operations;
 wenzelm parents: 
73780diff
changeset | 43 | Latex.output_ | 
| 67463 | 44 | (Document_Antiquotation.format ctxt | 
| 73761 | 45 | (Document_Antiquotation.delimit ctxt (Document_Output.pretty_thm ctxt thm))) ^ | 
| 80846 
9ed32b8a03a9
clarified print mode "latex": no longer impact Output/Markup/Pretty operations;
 wenzelm parents: 
73780diff
changeset | 46 |         enclose "\\rulename{" "}" (Latex.output_ name))
 | 
| 67463 | 47 | #> space_implode "\\par\\smallskip%\n" | 
| 73780 
466fae6bf22e
compose Latex text as XML, output exported YXML in Isabelle/Scala;
 wenzelm parents: 
73765diff
changeset | 48 | #> Latex.string | 
| 73761 | 49 | #> Document_Output.isabelle ctxt)); | 
| 26742 | 50 | |
| 51 | ||
| 30394 | 52 | (* Isabelle/Isar entities (with index) *) | 
| 26751 | 53 | |
| 54 | local | |
| 55 | ||
| 61620 | 56 | fun no_check (_: Proof.context) (name, _: Position.T) = name; | 
| 26894 | 57 | |
| 61620 | 58 | fun check_keyword ctxt (name, pos) = | 
| 59 | if Keyword.is_keyword (Thy_Header.get_keywords' ctxt) name then name | |
| 60 |   else error ("Bad outer syntax keyword " ^ quote name ^ Position.here pos);
 | |
| 53061 | 61 | |
| 71913 | 62 | fun check_system_option ctxt arg = | 
| 63 | (Completion.check_option (Options.default ()) ctxt arg; true) | |
| 56467 | 64 | handle ERROR _ => false; | 
| 65 | ||
| 26751 | 66 | val arg = enclose "{" "}" o clean_string;
 | 
| 67 | ||
| 56185 | 68 | fun entity check markup binding index = | 
| 73761 | 69 | Document_Output.antiquotation_raw | 
| 56185 | 70 | (binding |> Binding.map_name (fn name => name ^ | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 71 | (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref"))) | 
| 69349 
7cef9e386ffe
more accurate positions for "name" (quoted string) and "embedded" (cartouche): refer to content without delimiters, which is e.g. relevant for systematic selection/renaming of scope groups;
 wenzelm parents: 
68823diff
changeset | 72 | (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Args.name_position)) | 
| 67463 | 73 | (fn ctxt => fn (logic, (name, pos)) => | 
| 30394 | 74 | let | 
| 56185 | 75 | val kind = translate (fn "_" => " " | c => c) (Binding.name_of binding); | 
| 30394 | 76 | val hyper_name = | 
| 77 |           "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
 | |
| 78 | val hyper = | |
| 79 |           enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
 | |
| 80 |           index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
 | |
| 81 | val idx = | |
| 82 | (case index of | |
| 83 | NONE => "" | |
| 84 | | SOME is_def => | |
| 85 | "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name); | |
| 66682 
c4cbe609f6a8
avoid duplicate message for @{action} in particular (see also @{action} within Pure);
 wenzelm parents: 
62829diff
changeset | 86 | val _ = | 
| 
c4cbe609f6a8
avoid duplicate message for @{action} in particular (see also @{action} within Pure);
 wenzelm parents: 
62829diff
changeset | 87 | if Context_Position.is_reported ctxt pos then ignore (check ctxt (name, pos)) else (); | 
| 67463 | 88 | val latex = | 
| 89 | idx ^ | |
| 80846 
9ed32b8a03a9
clarified print mode "latex": no longer impact Output/Markup/Pretty operations;
 wenzelm parents: 
73780diff
changeset | 90 | (Latex.output_ name | 
| 67463 | 91 |             |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
 | 
| 92 |             |> hyper o enclose "\\mbox{\\isa{" "}}");
 | |
| 93 | in Latex.string latex end); | |
| 26897 | 94 | |
| 26894 | 95 | fun entity_antiqs check markup kind = | 
| 43564 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 96 | entity check markup kind NONE #> | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 97 | entity check markup kind (SOME true) #> | 
| 
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
 wenzelm parents: 
43563diff
changeset | 98 | entity check markup kind (SOME false); | 
| 26751 | 99 | |
| 100 | in | |
| 101 | ||
| 56059 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 102 | val _ = | 
| 
2390391584c2
some document antiquotations for Isabelle/jEdit elements;
 wenzelm parents: 
55997diff
changeset | 103 | Theory.setup | 
| 69593 | 104 | (entity_antiqs no_check "" \<^binding>\<open>syntax\<close> #> | 
| 105 | entity_antiqs Outer_Syntax.check_command "isacommand" \<^binding>\<open>command\<close> #> | |
| 106 | entity_antiqs check_keyword "isakeyword" \<^binding>\<open>keyword\<close> #> | |
| 107 | entity_antiqs check_keyword "isakeyword" \<^binding>\<open>element\<close> #> | |
| 108 | entity_antiqs Method.check_name "" \<^binding>\<open>method\<close> #> | |
| 109 | entity_antiqs Attrib.check_name "" \<^binding>\<open>attribute\<close> #> | |
| 110 | entity_antiqs no_check "" \<^binding>\<open>fact\<close> #> | |
| 111 | entity_antiqs no_check "" \<^binding>\<open>variable\<close> #> | |
| 112 | entity_antiqs no_check "" \<^binding>\<open>case\<close> #> | |
| 113 | entity_antiqs Document_Antiquotation.check "" \<^binding>\<open>antiquotation\<close> #> | |
| 114 | entity_antiqs Document_Antiquotation.check_option "" \<^binding>\<open>antiquotation_option\<close> #> | |
| 69962 
82e945d472d5
documentation of document markers and re-interpreted command tags;
 wenzelm parents: 
69593diff
changeset | 115 | entity_antiqs Document_Marker.check "" \<^binding>\<open>document_marker\<close> #> | 
| 69593 | 116 | entity_antiqs no_check "isasystem" \<^binding>\<open>setting\<close> #> | 
| 117 | entity_antiqs check_system_option "isasystem" \<^binding>\<open>system_option\<close> #> | |
| 118 | entity_antiqs no_check "" \<^binding>\<open>inference\<close> #> | |
| 119 | entity_antiqs no_check "isasystem" \<^binding>\<open>executable\<close> #> | |
| 72763 | 120 | entity_antiqs Isabelle_Tool.check "isatool" \<^binding>\<open>tool\<close> #> | 
| 69593 | 121 | entity_antiqs ML_Context.check_antiquotation "" \<^binding>\<open>ML_antiquotation\<close> #> | 
| 81114 
6538332c08e0
documentation for pretty block "notation" markup;
 wenzelm parents: 
80846diff
changeset | 122 | entity_antiqs (K JEdit.check_action) "isasystem" \<^binding>\<open>action\<close> #> | 
| 
6538332c08e0
documentation for pretty block "notation" markup;
 wenzelm parents: 
80846diff
changeset | 123 | entity_antiqs Markup_Kind.check_notation_kind "" \<^binding>\<open>notation_kind\<close>); | 
| 26751 | 124 | |
| 26742 | 125 | end; | 
| 26751 | 126 | |
| 73734 | 127 | |
| 128 | (* show symbols *) | |
| 129 | ||
| 130 | val _ = | |
| 73761 | 131 | Theory.setup (Document_Output.antiquotation_raw \<^binding>\<open>show_symbols\<close> (Scan.succeed ()) | 
| 73734 | 132 | (fn _ => fn _ => | 
| 133 | let | |
| 134 | val symbol_name = | |
| 135 |           unprefix "\\newcommand{\\isasym"
 | |
| 136 | #> raw_explode | |
| 137 | #> take_prefix Symbol.is_ascii_letter | |
| 138 | #> implode; | |
| 139 | ||
| 140 | val symbols = | |
| 141 | File.read \<^file>\<open>~~/lib/texinputs/isabellesym.sty\<close> | |
| 142 | |> split_lines | |
| 143 | |> map_filter (fn line => | |
| 144 | (case try symbol_name line of | |
| 145 | NONE => NONE | |
| 146 | | SOME "" => NONE | |
| 147 |             | SOME name => SOME ("\\verb,\\" ^ "<" ^ name ^ ">, & {\\isasym" ^ name ^ "}")));
 | |
| 148 | ||
| 149 | val eol = "\\\\\n"; | |
| 150 | fun table (a :: b :: rest) = a ^ " & " ^ b ^ eol :: table rest | |
| 151 | | table [a] = [a ^ eol] | |
| 152 | | table [] = []; | |
| 153 | in | |
| 154 | Latex.string | |
| 155 |           ("\\begin{supertabular}{ll@{\\qquad}ll}\n" ^ implode (table symbols) ^
 | |
| 156 |            "\\end{supertabular}\n")
 | |
| 157 | end)) | |
| 158 | ||
| 26751 | 159 | end; |