author | wenzelm |
Thu, 01 Feb 2007 20:40:34 +0100 | |
changeset 22228 | 7c27195a4afc |
parent 22225 | 30ab97554602 |
child 22590 | ac84debdd7d3 |
permissions | -rw-r--r-- |
21642 | 1 |
(* Title: Pure/ProofGeneral/proof_general_emacs.ML |
2 |
ID: $Id$ |
|
3 |
Author: David Aspinall and Markus Wenzel |
|
4 |
||
5 |
Isabelle/Isar configuration for Emacs Proof General. |
|
6 |
See http://proofgeneral.inf.ed.ac.uk |
|
7 |
*) |
|
8 |
||
9 |
signature PROOF_GENERAL = |
|
10 |
sig |
|
11 |
val init: bool -> unit |
|
12 |
val write_keywords: string -> unit |
|
13 |
end; |
|
14 |
||
15 |
structure ProofGeneral: PROOF_GENERAL = |
|
16 |
struct |
|
17 |
||
21945 | 18 |
|
21642 | 19 |
(* print modes *) |
20 |
||
21 |
val proof_generalN = "ProofGeneralEmacs"; (*token markup (colouring vars, etc.)*) |
|
22 |
val pgasciiN = "PGASCII"; (*plain 7-bit ASCII communication*) |
|
23 |
val thm_depsN = "thm_deps"; (*meta-information about theorem deps*) |
|
24 |
||
25 |
fun special oct = |
|
26 |
if Output.has_mode pgasciiN then chr 1 ^ chr (ord (oct_char oct) - 167) |
|
27 |
else oct_char oct; |
|
28 |
||
29 |
||
30 |
(* text output: print modes for xsymbol *) |
|
31 |
||
32 |
local |
|
33 |
||
34 |
fun xsym_output "\\" = "\\\\" |
|
35 |
| xsym_output s = if Symbol.is_raw s then Symbol.decode_raw s else s; |
|
36 |
||
37 |
fun xsymbols_output s = |
|
21940 | 38 |
if Output.has_mode Symbol.xsymbolsN andalso exists_string (equal "\\") s then |
21642 | 39 |
let val syms = Symbol.explode s |
40 |
in (implode (map xsym_output syms), real (Symbol.length syms)) end |
|
41 |
else Symbol.default_output s; |
|
42 |
||
43 |
in |
|
44 |
||
45 |
fun setup_xsymbols_output () = |
|
21940 | 46 |
Output.add_mode Symbol.xsymbolsN |
21642 | 47 |
(xsymbols_output, K xsymbols_output, Symbol.default_indent, Symbol.encode_raw); |
48 |
||
49 |
end; |
|
50 |
||
51 |
||
52 |
(* token translations *) |
|
53 |
||
54 |
local |
|
55 |
||
56 |
fun end_tag () = special "350"; |
|
57 |
val class_tag = ("class", fn () => special "351"); |
|
58 |
val tfree_tag = ("tfree", fn () => special "352"); |
|
59 |
val tvar_tag = ("tvar", fn () => special "353"); |
|
60 |
val free_tag = ("free", fn () => special "354"); |
|
61 |
val bound_tag = ("bound", fn () => special "355"); |
|
62 |
val var_tag = ("var", fn () => special "356"); |
|
63 |
val skolem_tag = ("skolem", fn () => special "357"); |
|
64 |
||
65 |
fun xml_atom kind x = XML.element "atom" [("kind", kind)] [XML.text x]; |
|
66 |
||
67 |
fun tagit (kind, bg_tag) x = |
|
21948 | 68 |
(bg_tag () ^ x ^ end_tag (), real (Symbol.length (Symbol.explode x))); |
21642 | 69 |
|
70 |
fun free_or_skolem x = |
|
71 |
(case try Name.dest_skolem x of |
|
72 |
NONE => tagit free_tag x |
|
73 |
| SOME x' => tagit skolem_tag x'); |
|
74 |
||
75 |
fun var_or_skolem s = |
|
76 |
(case Syntax.read_variable s of |
|
77 |
SOME (x, i) => |
|
78 |
(case try Name.dest_skolem x of |
|
79 |
NONE => tagit var_tag s |
|
80 |
| SOME x' => tagit skolem_tag |
|
81 |
(setmp show_question_marks true Syntax.string_of_vname (x', i))) |
|
82 |
| NONE => tagit var_tag s); |
|
83 |
||
84 |
val proof_general_trans = |
|
85 |
Syntax.tokentrans_mode proof_generalN |
|
86 |
[("class", tagit class_tag), |
|
87 |
("tfree", tagit tfree_tag), |
|
88 |
("tvar", tagit tvar_tag), |
|
89 |
("free", free_or_skolem), |
|
90 |
("bound", tagit bound_tag), |
|
91 |
("var", var_or_skolem)]; |
|
92 |
||
93 |
in |
|
94 |
||
95 |
val _ = Context.add_setup (Theory.add_tokentrfuns proof_general_trans); |
|
96 |
||
97 |
end; |
|
98 |
||
99 |
||
100 |
(* messages and notification *) |
|
101 |
||
102 |
fun decorate bg en prfx = |
|
103 |
writeln_default o enclose bg en o prefix_lines prfx; |
|
104 |
||
105 |
fun setup_messages () = |
|
106 |
(writeln_fn := (fn s => decorate "" "" "" s); |
|
107 |
priority_fn := (fn s => decorate (special "360") (special "361") "" s); |
|
108 |
tracing_fn := (fn s => decorate (special "360" ^ special "375") (special "361") "" s); |
|
109 |
info_fn := (fn s => decorate (special "362") (special "363") "+++ " s); |
|
110 |
debug_fn := (fn s => decorate (special "362") (special "363") "+++ " s); |
|
111 |
warning_fn := (fn s => decorate (special "362") (special "363") "### " s); |
|
22002
5c60e46a07c1
broken error handling for command syntax -- reverted to revision 1.9;
wenzelm
parents:
21986
diff
changeset
|
112 |
error_fn := (fn s => decorate (special "364") (special "365") "*** " s); |
21642 | 113 |
panic_fn := (fn s => decorate (special "364") (special "365") "!!! " s)); |
114 |
||
115 |
||
116 |
fun emacs_notify s = decorate (special "360") (special "361") "" s; |
|
117 |
||
118 |
fun tell_clear_goals () = |
|
21940 | 119 |
emacs_notify "Proof General, please clear the goals buffer."; |
21642 | 120 |
|
121 |
fun tell_clear_response () = |
|
21940 | 122 |
emacs_notify "Proof General, please clear the response buffer."; |
21642 | 123 |
|
124 |
fun tell_file_loaded path = |
|
21940 | 125 |
emacs_notify ("Proof General, this file is loaded: " ^ quote (File.platform_path path)); |
21642 | 126 |
|
127 |
fun tell_file_retracted path = |
|
21940 | 128 |
emacs_notify ("Proof General, you can unlock the file " ^ quote (File.platform_path path)); |
21642 | 129 |
|
130 |
||
131 |
(* theory / proof state output *) |
|
132 |
||
133 |
local |
|
134 |
||
135 |
fun tmp_markers f = |
|
136 |
setmp Display.current_goals_markers (special "366", special "367", "") f (); |
|
137 |
||
138 |
fun print_current_goals n m st = |
|
21945 | 139 |
tmp_markers (fn () => Display.print_current_goals_default n m st); |
21642 | 140 |
|
141 |
fun print_state b st = |
|
21945 | 142 |
tmp_markers (fn () => Toplevel.print_state_default b st); |
21642 | 143 |
|
144 |
in |
|
145 |
||
146 |
fun setup_state () = |
|
21948 | 147 |
(Display.print_current_goals_fn := print_current_goals; |
148 |
Toplevel.print_state_fn := print_state; |
|
149 |
Toplevel.prompt_state_fn := |
|
150 |
(fn s => suffix (special "372") (Toplevel.prompt_state_default s))); |
|
21642 | 151 |
|
152 |
end; |
|
153 |
||
154 |
||
155 |
(* theory loader actions *) |
|
156 |
||
157 |
local |
|
158 |
||
159 |
fun trace_action action name = |
|
160 |
if action = ThyInfo.Update then |
|
161 |
List.app tell_file_loaded (ThyInfo.loaded_files name) |
|
162 |
else if action = ThyInfo.Outdate orelse action = ThyInfo.Remove then |
|
163 |
List.app tell_file_retracted (ThyInfo.loaded_files name) |
|
164 |
else (); |
|
165 |
||
166 |
in |
|
167 |
fun setup_thy_loader () = ThyInfo.add_hook trace_action; |
|
168 |
fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.names ()); |
|
169 |
end; |
|
170 |
||
171 |
||
21948 | 172 |
(* get informed about files *) |
21642 | 173 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
174 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base o Path.explode; |
21642 | 175 |
|
176 |
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name; |
|
177 |
val inform_file_processed = ThyInfo.if_known_thy ThyInfo.touch_child_thys o thy_name; |
|
178 |
||
179 |
fun proper_inform_file_processed file state = |
|
180 |
let val name = thy_name file in |
|
181 |
if Toplevel.is_toplevel state andalso ThyInfo.known_thy name then |
|
182 |
(ThyInfo.touch_child_thys name; |
|
183 |
ThyInfo.pretend_use_thy_only name handle ERROR msg => |
|
184 |
(warning msg; warning ("Failed to register theory: " ^ quote name); |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
185 |
tell_file_retracted (Path.base (Path.explode file)))) |
21642 | 186 |
else raise Toplevel.UNDEF |
187 |
end; |
|
188 |
||
189 |
fun vacuous_inform_file_processed file state = |
|
190 |
(warning ("No theory " ^ quote (thy_name file)); |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
191 |
tell_file_retracted (Path.base (Path.explode file))); |
21642 | 192 |
|
193 |
||
194 |
(* restart top-level loop (keeps most state information) *) |
|
195 |
||
196 |
val welcome = priority o Session.welcome; |
|
197 |
||
198 |
fun restart () = |
|
21940 | 199 |
(sync_thy_loader (); |
200 |
tell_clear_goals (); |
|
201 |
tell_clear_response (); |
|
202 |
welcome (); |
|
203 |
raise Toplevel.RESTART); |
|
21642 | 204 |
|
205 |
||
206 |
(* theorem dependency output *) |
|
207 |
||
208 |
local |
|
209 |
||
210 |
val spaces_quote = space_implode " " o map quote; |
|
211 |
||
212 |
fun thm_deps_message (thms, deps) = |
|
21948 | 213 |
emacs_notify ("Proof General, theorem dependencies of " ^ thms ^ " are " ^ deps); |
21642 | 214 |
|
21968 | 215 |
fun tell_thm_deps ths = |
216 |
if Output.has_mode thm_depsN then |
|
217 |
let |
|
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
218 |
val names = map PureThy.get_name_hint (filter PureThy.has_name_hint ths); |
22225 | 219 |
val deps = Symtab.keys (fold Proofterm.thms_of_proof' |
220 |
(map Thm.proof_of ths) Symtab.empty); |
|
21968 | 221 |
in |
222 |
if null names orelse null deps then () |
|
223 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
|
224 |
end |
|
225 |
else (); |
|
21642 | 226 |
|
227 |
in |
|
228 |
||
229 |
fun setup_present_hook () = |
|
230 |
Present.add_hook (fn _ => fn res => tell_thm_deps (maps #2 res)); |
|
231 |
||
232 |
end; |
|
233 |
||
234 |
||
235 |
(* additional outer syntax for Isar *) |
|
236 |
||
237 |
local structure P = OuterParse and K = OuterKeyword in |
|
238 |
||
239 |
val undoP = (*undo without output*) |
|
240 |
OuterSyntax.improper_command "ProofGeneral.undo" "(internal)" K.control |
|
241 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.undo)); |
|
242 |
||
243 |
val restartP = |
|
244 |
OuterSyntax.improper_command "ProofGeneral.restart" "(internal)" K.control |
|
245 |
(P.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative restart))); |
|
246 |
||
247 |
val kill_proofP = |
|
248 |
OuterSyntax.improper_command "ProofGeneral.kill_proof" "(internal)" K.control |
|
249 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.kill_proof_notify tell_clear_goals)); |
|
250 |
||
251 |
val inform_file_processedP = |
|
252 |
OuterSyntax.improper_command "ProofGeneral.inform_file_processed" "(internal)" K.control |
|
253 |
(P.name >> (fn file => Toplevel.no_timing o |
|
21959 | 254 |
Toplevel.init_empty (vacuous_inform_file_processed file) o |
21642 | 255 |
Toplevel.kill o |
21959 | 256 |
Toplevel.init_empty (proper_inform_file_processed file))); |
21642 | 257 |
|
258 |
val inform_file_retractedP = |
|
259 |
OuterSyntax.improper_command "ProofGeneral.inform_file_retracted" "(internal)" K.control |
|
260 |
(P.name >> (Toplevel.no_timing oo |
|
261 |
(fn file => Toplevel.imperative (fn () => inform_file_retracted file)))); |
|
262 |
||
263 |
val process_pgipP = |
|
264 |
OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" K.control |
|
265 |
(P.text >> (Toplevel.no_timing oo |
|
266 |
(fn txt => Toplevel.imperative (fn () => ProofGeneralPgip.process_pgip txt)))); |
|
267 |
||
268 |
fun init_outer_syntax () = OuterSyntax.add_parsers |
|
21948 | 269 |
[undoP, restartP, kill_proofP, inform_file_processedP, inform_file_retractedP, process_pgipP]; |
21642 | 270 |
|
271 |
end; |
|
272 |
||
273 |
||
274 |
(* init *) |
|
275 |
||
276 |
val initialized = ref false; |
|
277 |
||
21940 | 278 |
fun init false = |
21968 | 279 |
Output.panic "No Proof General interface support for Isabelle/classic mode." |
21940 | 280 |
| init true = |
21968 | 281 |
(! initialized orelse |
282 |
(setmp warning_fn (K ()) init_outer_syntax (); |
|
283 |
setup_xsymbols_output (); |
|
284 |
setup_messages (); |
|
285 |
ProofGeneralPgip.init_pgip_channel (! priority_fn); |
|
286 |
setup_state (); |
|
287 |
setup_thy_loader (); |
|
288 |
setup_present_hook (); |
|
289 |
set initialized); |
|
290 |
sync_thy_loader (); |
|
291 |
change print_mode (cons proof_generalN o remove (op =) proof_generalN); |
|
292 |
Isar.sync_main ()); |
|
21642 | 293 |
|
294 |
||
295 |
||
21968 | 296 |
(** generate elisp file for keyword classification **) |
21642 | 297 |
|
298 |
local |
|
299 |
||
300 |
val regexp_meta = member (op =) (explode ".*+?[]^$"); |
|
21940 | 301 |
val regexp_quote = translate_string (fn c => if regexp_meta c then "\\\\" ^ c else c); |
21642 | 302 |
|
303 |
fun defconst name strs = |
|
304 |
"\n(defconst isar-keywords-" ^ name ^ |
|
305 |
"\n '(" ^ space_implode "\n " (map (quote o regexp_quote) strs) ^ "))\n"; |
|
306 |
||
307 |
fun make_elisp_commands commands kind = defconst kind |
|
308 |
(commands |> map_filter (fn (c, _, k, _) => if k = kind then SOME c else NONE)); |
|
309 |
||
310 |
fun make_elisp_syntax (keywords, commands) = |
|
311 |
";;\n\ |
|
312 |
\;; Keyword classification tables for Isabelle/Isar.\n\ |
|
313 |
\;; This file was generated by " ^ Session.name () ^ " -- DO NOT EDIT!\n\ |
|
314 |
\;;\n\ |
|
315 |
\;; $" ^ "Id$\n\ |
|
316 |
\;;\n" ^ |
|
317 |
defconst "major" (map #1 commands) ^ |
|
21940 | 318 |
defconst "minor" (filter Syntax.is_identifier keywords) ^ |
21642 | 319 |
implode (map (make_elisp_commands commands) OuterKeyword.kinds) ^ |
320 |
"\n(provide 'isar-keywords)\n"; |
|
321 |
||
322 |
in |
|
323 |
||
324 |
fun write_keywords s = |
|
325 |
(init_outer_syntax (); |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
326 |
File.write (Path.explode ("isar-keywords" ^ (if s = "" then "" else "-" ^ s) ^".el")) |
21642 | 327 |
(make_elisp_syntax (OuterSyntax.dest_keywords (), OuterSyntax.dest_parsers ()))); |
328 |
||
329 |
end; |
|
330 |
||
331 |
end; |