author | wenzelm |
Sat, 07 Jul 2007 18:39:21 +0200 | |
changeset 23641 | d6f9d3acffaa |
parent 23621 | e070a6ab1891 |
child 23662 | 91d06b04951f |
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 = |
|
22590 | 26 |
if print_mode_active pgasciiN then chr 1 ^ chr (ord (oct_char oct) - 167) |
21642 | 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 = |
|
22590 | 38 |
if print_mode_active Symbol.xsymbolsN andalso exists_string (equal "\\") s then |
21642 | 39 |
let val syms = Symbol.explode s |
23621 | 40 |
in (implode (map xsym_output syms), Symbol.length syms) end |
41 |
else Output.default_output s; |
|
21642 | 42 |
|
43 |
in |
|
44 |
||
45 |
fun setup_xsymbols_output () = |
|
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
46 |
Output.add_mode Symbol.xsymbolsN xsymbols_output Symbol.encode_raw; |
21642 | 47 |
|
48 |
end; |
|
49 |
||
50 |
||
51 |
(* token translations *) |
|
52 |
||
53 |
local |
|
54 |
||
55 |
fun end_tag () = special "350"; |
|
56 |
val class_tag = ("class", fn () => special "351"); |
|
57 |
val tfree_tag = ("tfree", fn () => special "352"); |
|
58 |
val tvar_tag = ("tvar", fn () => special "353"); |
|
59 |
val free_tag = ("free", fn () => special "354"); |
|
60 |
val bound_tag = ("bound", fn () => special "355"); |
|
61 |
val var_tag = ("var", fn () => special "356"); |
|
62 |
val skolem_tag = ("skolem", fn () => special "357"); |
|
63 |
||
64 |
fun xml_atom kind x = XML.element "atom" [("kind", kind)] [XML.text x]; |
|
65 |
||
66 |
fun tagit (kind, bg_tag) x = |
|
23621 | 67 |
(bg_tag () ^ x ^ end_tag (), Symbol.length (Symbol.explode x)); |
21642 | 68 |
|
69 |
fun free_or_skolem x = |
|
70 |
(case try Name.dest_skolem x of |
|
71 |
NONE => tagit free_tag x |
|
72 |
| SOME x' => tagit skolem_tag x'); |
|
73 |
||
74 |
fun var_or_skolem s = |
|
75 |
(case Syntax.read_variable s of |
|
76 |
SOME (x, i) => |
|
77 |
(case try Name.dest_skolem x of |
|
78 |
NONE => tagit var_tag s |
|
79 |
| SOME x' => tagit skolem_tag |
|
22678 | 80 |
(setmp show_question_marks true Term.string_of_vname (x', i))) |
21642 | 81 |
| NONE => tagit var_tag s); |
82 |
||
83 |
val proof_general_trans = |
|
84 |
Syntax.tokentrans_mode proof_generalN |
|
85 |
[("class", tagit class_tag), |
|
86 |
("tfree", tagit tfree_tag), |
|
87 |
("tvar", tagit tvar_tag), |
|
88 |
("free", free_or_skolem), |
|
89 |
("bound", tagit bound_tag), |
|
90 |
("var", var_or_skolem)]; |
|
91 |
||
92 |
in |
|
93 |
||
94 |
val _ = Context.add_setup (Theory.add_tokentrfuns proof_general_trans); |
|
95 |
||
96 |
end; |
|
97 |
||
98 |
||
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
99 |
(* common markup *) |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
100 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
101 |
fun proof_general_markup (name, props) = |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
102 |
(if name = Markup.promptN then ("", special "372") |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
103 |
else if name = Markup.no_stateN orelse name = Markup.stateN |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
104 |
then (special "366" ^ "\n", "\n" ^ special "367") |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
105 |
else ("", "")) |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
106 |
|> (name <> Markup.promptN andalso print_mode_active "test_markup") ? |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
107 |
(fn (bg, en) => |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
108 |
(bg ^ enclose "<" ">" (space_implode " " (name :: map XML.attribute props)), |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
109 |
enclose "</" ">" name ^ en)); |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
110 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
111 |
val _ = Pretty.add_mode proof_generalN Pretty.default_indent proof_general_markup; |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
112 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
113 |
|
21642 | 114 |
(* messages and notification *) |
115 |
||
116 |
fun decorate bg en prfx = |
|
22590 | 117 |
Output.writeln_default o enclose bg en o prefix_lines prfx; |
21642 | 118 |
|
119 |
fun setup_messages () = |
|
22590 | 120 |
(Output.writeln_fn := (fn s => decorate "" "" "" s); |
121 |
Output.priority_fn := (fn s => decorate (special "360") (special "361") "" s); |
|
122 |
Output.tracing_fn := (fn s => decorate (special "360" ^ special "375") (special "361") "" s); |
|
123 |
Output.debug_fn := (fn s => decorate (special "362") (special "363") "+++ " s); |
|
124 |
Output.warning_fn := (fn s => decorate (special "362") (special "363") "### " s); |
|
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
125 |
Output.error_fn := (fn s => decorate (special "364") (special "365") "*** " s)); |
21642 | 126 |
|
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
127 |
fun panic s = |
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
128 |
(decorate (special "364") (special "365") "!!! " ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
21642 | 129 |
|
130 |
fun emacs_notify s = decorate (special "360") (special "361") "" s; |
|
131 |
||
132 |
fun tell_clear_goals () = |
|
21940 | 133 |
emacs_notify "Proof General, please clear the goals buffer."; |
21642 | 134 |
|
135 |
fun tell_clear_response () = |
|
21940 | 136 |
emacs_notify "Proof General, please clear the response buffer."; |
21642 | 137 |
|
138 |
fun tell_file_loaded path = |
|
21940 | 139 |
emacs_notify ("Proof General, this file is loaded: " ^ quote (File.platform_path path)); |
21642 | 140 |
|
141 |
fun tell_file_retracted path = |
|
21940 | 142 |
emacs_notify ("Proof General, you can unlock the file " ^ quote (File.platform_path path)); |
21642 | 143 |
|
144 |
||
145 |
(* theory loader actions *) |
|
146 |
||
147 |
local |
|
148 |
||
149 |
fun trace_action action name = |
|
150 |
if action = ThyInfo.Update then |
|
151 |
List.app tell_file_loaded (ThyInfo.loaded_files name) |
|
152 |
else if action = ThyInfo.Outdate orelse action = ThyInfo.Remove then |
|
153 |
List.app tell_file_retracted (ThyInfo.loaded_files name) |
|
154 |
else (); |
|
155 |
||
156 |
in |
|
157 |
fun setup_thy_loader () = ThyInfo.add_hook trace_action; |
|
158 |
fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.names ()); |
|
159 |
end; |
|
160 |
||
161 |
||
21948 | 162 |
(* get informed about files *) |
21642 | 163 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
164 |
val thy_name = Path.implode o #1 o Path.split_ext o Path.base o Path.explode; |
21642 | 165 |
|
166 |
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name; |
|
167 |
val inform_file_processed = ThyInfo.if_known_thy ThyInfo.touch_child_thys o thy_name; |
|
168 |
||
169 |
fun proper_inform_file_processed file state = |
|
170 |
let val name = thy_name file in |
|
171 |
if Toplevel.is_toplevel state andalso ThyInfo.known_thy name then |
|
172 |
(ThyInfo.touch_child_thys name; |
|
173 |
ThyInfo.pretend_use_thy_only name handle ERROR msg => |
|
174 |
(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
|
175 |
tell_file_retracted (Path.base (Path.explode file)))) |
21642 | 176 |
else raise Toplevel.UNDEF |
177 |
end; |
|
178 |
||
179 |
fun vacuous_inform_file_processed file state = |
|
180 |
(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
|
181 |
tell_file_retracted (Path.base (Path.explode file))); |
21642 | 182 |
|
183 |
||
184 |
(* restart top-level loop (keeps most state information) *) |
|
185 |
||
186 |
val welcome = priority o Session.welcome; |
|
187 |
||
188 |
fun restart () = |
|
21940 | 189 |
(sync_thy_loader (); |
190 |
tell_clear_goals (); |
|
191 |
tell_clear_response (); |
|
192 |
welcome (); |
|
193 |
raise Toplevel.RESTART); |
|
21642 | 194 |
|
195 |
||
196 |
(* theorem dependency output *) |
|
197 |
||
198 |
local |
|
199 |
||
200 |
val spaces_quote = space_implode " " o map quote; |
|
201 |
||
202 |
fun thm_deps_message (thms, deps) = |
|
21948 | 203 |
emacs_notify ("Proof General, theorem dependencies of " ^ thms ^ " are " ^ deps); |
21642 | 204 |
|
21968 | 205 |
fun tell_thm_deps ths = |
22590 | 206 |
if print_mode_active thm_depsN then |
21968 | 207 |
let |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
208 |
val names = map PureThy.get_name_hint (filter PureThy.has_name_hint ths); |
22225 | 209 |
val deps = Symtab.keys (fold Proofterm.thms_of_proof' |
210 |
(map Thm.proof_of ths) Symtab.empty); |
|
21968 | 211 |
in |
212 |
if null names orelse null deps then () |
|
213 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
|
214 |
end |
|
215 |
else (); |
|
21642 | 216 |
|
217 |
in |
|
218 |
||
219 |
fun setup_present_hook () = |
|
220 |
Present.add_hook (fn _ => fn res => tell_thm_deps (maps #2 res)); |
|
221 |
||
222 |
end; |
|
223 |
||
224 |
||
225 |
(* additional outer syntax for Isar *) |
|
226 |
||
227 |
local structure P = OuterParse and K = OuterKeyword in |
|
228 |
||
229 |
val undoP = (*undo without output*) |
|
230 |
OuterSyntax.improper_command "ProofGeneral.undo" "(internal)" K.control |
|
231 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.undo)); |
|
232 |
||
233 |
val restartP = |
|
234 |
OuterSyntax.improper_command "ProofGeneral.restart" "(internal)" K.control |
|
235 |
(P.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative restart))); |
|
236 |
||
237 |
val kill_proofP = |
|
238 |
OuterSyntax.improper_command "ProofGeneral.kill_proof" "(internal)" K.control |
|
239 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.kill_proof_notify tell_clear_goals)); |
|
240 |
||
241 |
val inform_file_processedP = |
|
242 |
OuterSyntax.improper_command "ProofGeneral.inform_file_processed" "(internal)" K.control |
|
243 |
(P.name >> (fn file => Toplevel.no_timing o |
|
21959 | 244 |
Toplevel.init_empty (vacuous_inform_file_processed file) o |
21642 | 245 |
Toplevel.kill o |
21959 | 246 |
Toplevel.init_empty (proper_inform_file_processed file))); |
21642 | 247 |
|
248 |
val inform_file_retractedP = |
|
249 |
OuterSyntax.improper_command "ProofGeneral.inform_file_retracted" "(internal)" K.control |
|
250 |
(P.name >> (Toplevel.no_timing oo |
|
251 |
(fn file => Toplevel.imperative (fn () => inform_file_retracted file)))); |
|
252 |
||
253 |
val process_pgipP = |
|
254 |
OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" K.control |
|
255 |
(P.text >> (Toplevel.no_timing oo |
|
256 |
(fn txt => Toplevel.imperative (fn () => ProofGeneralPgip.process_pgip txt)))); |
|
257 |
||
258 |
fun init_outer_syntax () = OuterSyntax.add_parsers |
|
21948 | 259 |
[undoP, restartP, kill_proofP, inform_file_processedP, inform_file_retractedP, process_pgipP]; |
21642 | 260 |
|
261 |
end; |
|
262 |
||
263 |
||
264 |
(* init *) |
|
265 |
||
266 |
val initialized = ref false; |
|
267 |
||
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
268 |
fun init false = panic "No Proof General interface support for Isabelle/classic mode." |
21940 | 269 |
| init true = |
21968 | 270 |
(! initialized orelse |
22590 | 271 |
(Output.no_warnings init_outer_syntax (); |
21968 | 272 |
setup_xsymbols_output (); |
273 |
setup_messages (); |
|
22590 | 274 |
ProofGeneralPgip.init_pgip_channel (! Output.priority_fn); |
21968 | 275 |
setup_thy_loader (); |
276 |
setup_present_hook (); |
|
277 |
set initialized); |
|
278 |
sync_thy_loader (); |
|
279 |
change print_mode (cons proof_generalN o remove (op =) proof_generalN); |
|
280 |
Isar.sync_main ()); |
|
21642 | 281 |
|
282 |
||
283 |
||
21968 | 284 |
(** generate elisp file for keyword classification **) |
21642 | 285 |
|
286 |
local |
|
287 |
||
288 |
val regexp_meta = member (op =) (explode ".*+?[]^$"); |
|
21940 | 289 |
val regexp_quote = translate_string (fn c => if regexp_meta c then "\\\\" ^ c else c); |
21642 | 290 |
|
291 |
fun defconst name strs = |
|
292 |
"\n(defconst isar-keywords-" ^ name ^ |
|
293 |
"\n '(" ^ space_implode "\n " (map (quote o regexp_quote) strs) ^ "))\n"; |
|
294 |
||
295 |
fun make_elisp_commands commands kind = defconst kind |
|
296 |
(commands |> map_filter (fn (c, _, k, _) => if k = kind then SOME c else NONE)); |
|
297 |
||
298 |
fun make_elisp_syntax (keywords, commands) = |
|
299 |
";;\n\ |
|
300 |
\;; Keyword classification tables for Isabelle/Isar.\n\ |
|
301 |
\;; This file was generated by " ^ Session.name () ^ " -- DO NOT EDIT!\n\ |
|
302 |
\;;\n\ |
|
303 |
\;; $" ^ "Id$\n\ |
|
304 |
\;;\n" ^ |
|
305 |
defconst "major" (map #1 commands) ^ |
|
21940 | 306 |
defconst "minor" (filter Syntax.is_identifier keywords) ^ |
21642 | 307 |
implode (map (make_elisp_commands commands) OuterKeyword.kinds) ^ |
308 |
"\n(provide 'isar-keywords)\n"; |
|
309 |
||
310 |
in |
|
311 |
||
312 |
fun write_keywords s = |
|
313 |
(init_outer_syntax (); |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21649
diff
changeset
|
314 |
File.write (Path.explode ("isar-keywords" ^ (if s = "" then "" else "-" ^ s) ^".el")) |
21642 | 315 |
(make_elisp_syntax (OuterSyntax.dest_keywords (), OuterSyntax.dest_parsers ()))); |
316 |
||
317 |
end; |
|
318 |
||
319 |
end; |