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