1 (* Title: Doc/antiquote_setup.ML
4 Auxiliary antiquotations for the Isabelle manuals.
7 signature ANTIQUOTE_SETUP =
9 val setup: theory -> theory
12 structure Antiquote_Setup: ANTIQUOTE_SETUP =
17 fun translate f = Symbol.explode #> map f #> implode;
19 val clean_string = translate
32 fun clean_name "\<dots>" = "dots"
33 | clean_name ".." = "ddot"
34 | clean_name "." = "dot"
35 | clean_name "_" = "underscore"
36 | clean_name "{" = "braceleft"
37 | clean_name "}" = "braceright"
38 | clean_name s = s |> translate (fn "_" => "-" | "\<hyphen>" => "-" | c => c);
43 val verbatim = space_implode "\\verb,|," o map (enclose "\\verb|" "|") o space_explode "|";
46 Thy_Output.antiquotation @{binding verbatim} (Scan.lift Args.name)
47 (K (split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"));
54 fun ml_val (txt1, "") = "fn _ => (" ^ txt1 ^ ");"
55 | ml_val (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ ");";
57 fun ml_op (txt1, "") = "fn _ => (op " ^ txt1 ^ ");"
58 | ml_op (txt1, txt2) = "fn _ => (op " ^ txt1 ^ " : " ^ txt2 ^ ");";
60 fun ml_type (txt1, "") = "val _ = NONE : (" ^ txt1 ^ ") option;"
61 | ml_type (txt1, txt2) = "val _ = [NONE : (" ^ txt1 ^ ") option, NONE : (" ^ txt2 ^ ") option];";
63 fun ml_exn (txt1, "") = "fn _ => (" ^ txt1 ^ " : exn);"
64 | ml_exn (txt1, txt2) = "fn _ => (" ^ txt1 ^ " : " ^ txt2 ^ " -> exn);";
66 fun ml_structure (txt, _) = "functor XXX() = struct structure XX = " ^ txt ^ " end;";
68 fun ml_functor (txt, _) = "ML_Env.check_functor " ^ ML_Syntax.print_string txt;
70 val is_name = ML_Lex.kind_of #> (fn kind => kind = ML_Lex.Ident orelse kind = ML_Lex.LongIdent);
73 (case filter is_name (ML_Lex.tokenize txt) of
74 toks as [_] => ML_Lex.flatten toks
75 | _ => error ("Single ML name expected in input: " ^ quote txt));
77 fun index_ml name kind ml = Thy_Output.antiquotation name
78 (Scan.lift (Args.name -- Scan.optional (Args.colon |-- Args.name) ""))
79 (fn {context = ctxt, ...} => fn (txt1, txt2) =>
82 if txt2 = "" then txt1
83 else if kind = "type" then txt1 ^ " = " ^ txt2
84 else if kind = "exception" then txt1 ^ " of " ^ txt2
85 else if Symbol_Pos.is_identifier (Long_Name.base_name (ml_name txt1))
86 then txt1 ^ ": " ^ txt2
87 else txt1 ^ " : " ^ txt2;
88 val txt' = if kind = "" then txt else kind ^ " " ^ txt;
89 val _ = ML_Context.eval_text_in (SOME ctxt) false Position.none (ml (txt1, txt2)); (* ML_Lex.read (!?) *)
90 val kind' = if kind = "" then "ML" else "ML " ^ kind;
92 "\\indexdef{}{" ^ kind' ^ "}{" ^ clean_string (ml_name txt1) ^ "}" ^
94 |> (if Config.get ctxt Thy_Output.quotes then quote else I)
95 |> (if Config.get ctxt Thy_Output.display then enclose "\\begin{verbatim}\n" "\n\\end{verbatim}"
96 else split_lines #> map verbatim #> space_implode "\\isasep\\isanewline%\n"))
102 index_ml @{binding index_ML} "" ml_val #>
103 index_ml @{binding index_ML_op} "infix" ml_op #>
104 index_ml @{binding index_ML_type} "type" ml_type #>
105 index_ml @{binding index_ML_exn} "exception" ml_exn #>
106 index_ml @{binding index_ML_structure} "structure" ml_structure #>
107 index_ml @{binding index_ML_functor} "functor" ml_functor;
114 val named_thms_setup =
115 Thy_Output.antiquotation @{binding named_thms}
116 (Scan.repeat (Attrib.thm -- Scan.lift (Args.parens Args.name)))
117 (fn {context = ctxt, ...} =>
118 map (apfst (Thy_Output.pretty_thm ctxt))
119 #> (if Config.get ctxt Thy_Output.quotes then map (apfst Pretty.quote) else I)
120 #> (if Config.get ctxt Thy_Output.display
123 Output.output (Pretty.string_of (Pretty.indent (Config.get ctxt Thy_Output.indent) p)) ^
124 "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
125 #> space_implode "\\par\\smallskip%\n"
126 #> enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
129 Output.output (Pretty.str_of p) ^
130 "\\rulename{" ^ Output.output (Pretty.str_of (Thy_Output.pretty_text ctxt name)) ^ "}")
131 #> space_implode "\\par\\smallskip%\n"
132 #> enclose "\\isa{" "}"));
138 Thy_Output.antiquotation @{binding thy_file} (Scan.lift Args.name)
139 (fn {context = ctxt, ...} =>
140 fn name => (Thy_Load.check_thy Path.current name; Thy_Output.output ctxt [Pretty.str name]));
143 (* Isabelle/Isar entities (with index) *)
147 fun no_check _ _ = true;
149 fun thy_check check ctxt = can (check (Proof_Context.theory_of ctxt));
151 fun check_tool (name, pos) =
153 (* FIXME ISABELLE_TOOLS !? *)
154 val dirs = map Path.explode ["~~/lib/Tools", "~~/src/Tools/jEdit/lib/Tools"];
156 let val path = Path.append dir (Path.basic name)
157 in if File.exists path then SOME path else NONE end;
159 (case get_first tool dirs of
161 | SOME path => (Position.report pos (Markup.path (Path.implode path)); true))
164 val arg = enclose "{" "}" o clean_string;
166 fun entity check markup kind index =
167 Thy_Output.antiquotation
168 (Binding.name (translate (fn " " => "_" | c => c) kind ^
169 (case index of NONE => "" | SOME true => "_def" | SOME false => "_ref")))
170 (Scan.lift (Scan.optional (Args.parens Args.name) "" -- Parse.position Args.name))
171 (fn {context = ctxt, ...} => fn (logic, (name, pos)) =>
174 "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
176 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
177 index = SOME true ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
182 "\\index" ^ (if is_def then "def" else "ref") ^ arg logic ^ arg kind ^ arg name);
184 if check ctxt (name, pos) then
187 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
188 |> (if Config.get ctxt Thy_Output.quotes then quote else I)
189 |> (if Config.get ctxt Thy_Output.display
190 then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
191 else hyper o enclose "\\mbox{\\isa{" "}}"))
192 else error ("Bad " ^ kind ^ " " ^ quote name ^ Position.here pos)
195 fun entity_antiqs check markup kind =
196 entity check markup kind NONE #>
197 entity check markup kind (SOME true) #>
198 entity check markup kind (SOME false);
203 entity_antiqs no_check "" "syntax" #>
204 entity_antiqs (K (is_some o Keyword.command_keyword o #1)) "isacommand" "command" #>
205 entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "keyword" #>
206 entity_antiqs (K (Keyword.is_keyword o #1)) "isakeyword" "element" #>
207 entity_antiqs (thy_check Method.check) "" "method" #>
208 entity_antiqs (thy_check Attrib.check) "" "attribute" #>
209 entity_antiqs no_check "" "fact" #>
210 entity_antiqs no_check "" "variable" #>
211 entity_antiqs no_check "" "case" #>
212 entity_antiqs (thy_check Thy_Output.check_command) "" "antiquotation" #>
213 entity_antiqs (thy_check Thy_Output.check_option) "" "antiquotation option" #>
214 entity_antiqs no_check "isatt" "setting" #>
215 entity_antiqs no_check "isatt" "system option" #>
216 entity_antiqs no_check "" "inference" #>
217 entity_antiqs no_check "isatt" "executable" #>
218 entity_antiqs (K check_tool) "isatool" "tool" #>
219 entity_antiqs (thy_check ML_Context.check_antiq) "" Markup.ML_antiquotationN;