author | wenzelm |
Mon, 14 Apr 2008 14:28:47 +0200 | |
changeset 26643 | 99f5407c05ef |
parent 26613 | 725a3d9011d1 |
child 26706 | 4ea64590d28b |
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. |
|
26549 | 6 |
See also http://proofgeneral.inf.ed.ac.uk |
21642 | 7 |
*) |
8 |
||
9 |
signature PROOF_GENERAL = |
|
10 |
sig |
|
26549 | 11 |
val test_markupN: string |
21642 | 12 |
val init: bool -> unit |
24874 | 13 |
val init_outer_syntax: unit -> unit |
24289 | 14 |
val sendback: string -> Pretty.T list -> unit |
21642 | 15 |
end; |
16 |
||
17 |
structure ProofGeneral: PROOF_GENERAL = |
|
18 |
struct |
|
19 |
||
21945 | 20 |
|
21642 | 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*) |
|
26549 | 26 |
val test_markupN = "test_markup"; (*XML markup for everything*) |
27 |
||
28 |
val _ = Markup.add_mode test_markupN XML.output_markup; |
|
21642 | 29 |
|
30 |
fun special oct = |
|
26526 | 31 |
if print_mode_active pgasciiN then Symbol.SOH ^ chr (ord (oct_char oct) - 167) |
21642 | 32 |
else oct_char oct; |
33 |
||
34 |
||
35 |
(* text output: print modes for xsymbol *) |
|
36 |
||
37 |
local |
|
38 |
||
39 |
fun xsym_output "\\" = "\\\\" |
|
40 |
| xsym_output s = if Symbol.is_raw s then Symbol.decode_raw s else s; |
|
41 |
||
42 |
fun xsymbols_output s = |
|
22590 | 43 |
if print_mode_active Symbol.xsymbolsN andalso exists_string (equal "\\") s then |
21642 | 44 |
let val syms = Symbol.explode s |
23621 | 45 |
in (implode (map xsym_output syms), Symbol.length syms) end |
46 |
else Output.default_output s; |
|
21642 | 47 |
|
48 |
in |
|
49 |
||
50 |
fun setup_xsymbols_output () = |
|
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
51 |
Output.add_mode Symbol.xsymbolsN xsymbols_output Symbol.encode_raw; |
21642 | 52 |
|
53 |
end; |
|
54 |
||
55 |
||
56 |
(* token translations *) |
|
57 |
||
58 |
local |
|
59 |
||
60 |
fun end_tag () = special "350"; |
|
61 |
val class_tag = ("class", fn () => special "351"); |
|
62 |
val tfree_tag = ("tfree", fn () => special "352"); |
|
63 |
val tvar_tag = ("tvar", fn () => special "353"); |
|
64 |
val free_tag = ("free", fn () => special "354"); |
|
65 |
val bound_tag = ("bound", fn () => special "355"); |
|
66 |
val var_tag = ("var", fn () => special "356"); |
|
67 |
val skolem_tag = ("skolem", fn () => special "357"); |
|
68 |
||
69 |
fun xml_atom kind x = XML.element "atom" [("kind", kind)] [XML.text x]; |
|
70 |
||
71 |
fun tagit (kind, bg_tag) x = |
|
23621 | 72 |
(bg_tag () ^ x ^ end_tag (), Symbol.length (Symbol.explode x)); |
21642 | 73 |
|
74 |
fun free_or_skolem x = |
|
75 |
(case try Name.dest_skolem x of |
|
76 |
NONE => tagit free_tag x |
|
77 |
| SOME x' => tagit skolem_tag x'); |
|
78 |
||
79 |
fun var_or_skolem s = |
|
24244 | 80 |
(case Lexicon.read_variable s of |
21642 | 81 |
SOME (x, i) => |
82 |
(case try Name.dest_skolem x of |
|
83 |
NONE => tagit var_tag s |
|
84 |
| SOME x' => tagit skolem_tag |
|
22678 | 85 |
(setmp show_question_marks true Term.string_of_vname (x', i))) |
21642 | 86 |
| NONE => tagit var_tag s); |
87 |
||
88 |
val proof_general_trans = |
|
89 |
Syntax.tokentrans_mode proof_generalN |
|
90 |
[("class", tagit class_tag), |
|
91 |
("tfree", tagit tfree_tag), |
|
92 |
("tvar", tagit tvar_tag), |
|
93 |
("free", free_or_skolem), |
|
94 |
("bound", tagit bound_tag), |
|
95 |
("var", var_or_skolem)]; |
|
96 |
||
97 |
in |
|
98 |
||
26463 | 99 |
val _ = Context.>> (Context.map_theory (Sign.add_tokentrfuns proof_general_trans)); |
21642 | 100 |
|
101 |
end; |
|
102 |
||
103 |
||
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
104 |
(* common markup *) |
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
105 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
106 |
fun proof_general_markup (name, props) = |
25630 | 107 |
let |
108 |
val (bg1, en1) = |
|
25848 | 109 |
if name = Markup.stateN then (special "366" ^ "\n", "\n" ^ special "367") |
25630 | 110 |
else if name = Markup.sendbackN then (special "376", special "377") |
111 |
else if name = Markup.hiliteN then (special "327", special "330") |
|
112 |
else ("", ""); |
|
113 |
val (bg2, en2) = |
|
26549 | 114 |
if print_mode_active test_markupN |
26542 | 115 |
then XML.output_markup (name, props) |
25630 | 116 |
else ("", ""); |
117 |
in (bg1 ^ bg2, en2 ^ en1) end; |
|
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
118 |
|
23702 | 119 |
val _ = Markup.add_mode proof_generalN proof_general_markup; |
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
120 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
121 |
|
21642 | 122 |
(* messages and notification *) |
123 |
||
124 |
fun decorate bg en prfx = |
|
22590 | 125 |
Output.writeln_default o enclose bg en o prefix_lines prfx; |
21642 | 126 |
|
127 |
fun setup_messages () = |
|
23662 | 128 |
(Output.writeln_fn := Output.writeln_default; |
22590 | 129 |
Output.priority_fn := (fn s => decorate (special "360") (special "361") "" s); |
130 |
Output.tracing_fn := (fn s => decorate (special "360" ^ special "375") (special "361") "" s); |
|
131 |
Output.debug_fn := (fn s => decorate (special "362") (special "363") "+++ " s); |
|
132 |
Output.warning_fn := (fn s => decorate (special "362") (special "363") "### " s); |
|
25848 | 133 |
Output.error_fn := (fn s => decorate (special "364") (special "365") "*** " s); |
134 |
Output.prompt_fn := (fn s => Output.std_output (s ^ special "372"))); |
|
21642 | 135 |
|
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
136 |
fun panic s = |
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
137 |
(decorate (special "364") (special "365") "!!! " ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
21642 | 138 |
|
139 |
fun emacs_notify s = decorate (special "360") (special "361") "" s; |
|
140 |
||
141 |
fun tell_clear_goals () = |
|
21940 | 142 |
emacs_notify "Proof General, please clear the goals buffer."; |
21642 | 143 |
|
144 |
fun tell_clear_response () = |
|
21940 | 145 |
emacs_notify "Proof General, please clear the response buffer."; |
21642 | 146 |
|
147 |
fun tell_file_loaded path = |
|
21940 | 148 |
emacs_notify ("Proof General, this file is loaded: " ^ quote (File.platform_path path)); |
21642 | 149 |
|
150 |
fun tell_file_retracted path = |
|
21940 | 151 |
emacs_notify ("Proof General, you can unlock the file " ^ quote (File.platform_path path)); |
21642 | 152 |
|
24289 | 153 |
fun sendback heading prts = |
154 |
Pretty.writeln (Pretty.big_list heading [Pretty.markup Markup.sendback prts]); |
|
155 |
||
21642 | 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; |
|
26613 | 170 |
fun sync_thy_loader () = List.app (trace_action ThyInfo.Update) (ThyInfo.get_names ()); |
21642 | 171 |
end; |
172 |
||
173 |
||
21948 | 174 |
(* get informed about files *) |
21642 | 175 |
|
25442 | 176 |
local |
21642 | 177 |
|
25442 | 178 |
(*liberal low-level version*) |
179 |
val thy_name = perhaps (try (unsuffix ".thy")) o List.last o space_explode "/"; |
|
180 |
val thy_path = ThyLoad.thy_path o thy_name; |
|
181 |
||
182 |
in |
|
183 |
||
25443
1af14e8f225b
inform_file_processed: made even more robust against bad file specs;
wenzelm
parents:
25442
diff
changeset
|
184 |
fun check_thy "" = false |
1af14e8f225b
inform_file_processed: made even more robust against bad file specs;
wenzelm
parents:
25442
diff
changeset
|
185 |
| check_thy file = ThyInfo.check_known_thy (thy_name file); |
21642 | 186 |
|
23913 | 187 |
fun proper_inform_file_processed file () = |
25442 | 188 |
let |
189 |
val name = thy_name file; |
|
190 |
val _ = ThyInfo.touch_child_thys name; |
|
191 |
val _ = ThyInfo.register_thy name handle ERROR msg => |
|
192 |
(warning (cat_lines [msg, "Failed to register theory: " ^ quote name]); |
|
193 |
tell_file_retracted (thy_path file)); |
|
194 |
in () end; |
|
21642 | 195 |
|
25442 | 196 |
fun vacuous_inform_file_processed file () = tell_file_retracted (thy_path file); |
197 |
||
198 |
val inform_file_retracted = ThyInfo.if_known_thy ThyInfo.remove_thy o thy_name; |
|
199 |
||
200 |
end; |
|
21642 | 201 |
|
202 |
||
203 |
(* restart top-level loop (keeps most state information) *) |
|
204 |
||
205 |
val welcome = priority o Session.welcome; |
|
206 |
||
207 |
fun restart () = |
|
21940 | 208 |
(sync_thy_loader (); |
209 |
tell_clear_goals (); |
|
210 |
tell_clear_response (); |
|
211 |
welcome (); |
|
212 |
raise Toplevel.RESTART); |
|
21642 | 213 |
|
214 |
||
215 |
(* theorem dependency output *) |
|
216 |
||
217 |
local |
|
218 |
||
219 |
val spaces_quote = space_implode " " o map quote; |
|
220 |
||
221 |
fun thm_deps_message (thms, deps) = |
|
21948 | 222 |
emacs_notify ("Proof General, theorem dependencies of " ^ thms ^ " are " ^ deps); |
21642 | 223 |
|
21968 | 224 |
fun tell_thm_deps ths = |
22590 | 225 |
if print_mode_active thm_depsN then |
21968 | 226 |
let |
22228
7c27195a4afc
proper use of PureThy.has_name_hint instead of hard-wired string'';
wenzelm
parents:
22225
diff
changeset
|
227 |
val names = map PureThy.get_name_hint (filter PureThy.has_name_hint ths); |
22225 | 228 |
val deps = Symtab.keys (fold Proofterm.thms_of_proof' |
229 |
(map Thm.proof_of ths) Symtab.empty); |
|
21968 | 230 |
in |
231 |
if null names orelse null deps then () |
|
232 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
|
233 |
end |
|
234 |
else (); |
|
21642 | 235 |
|
236 |
in |
|
237 |
||
238 |
fun setup_present_hook () = |
|
239 |
Present.add_hook (fn _ => fn res => tell_thm_deps (maps #2 res)); |
|
240 |
||
241 |
end; |
|
242 |
||
243 |
||
244 |
(* additional outer syntax for Isar *) |
|
245 |
||
246 |
local structure P = OuterParse and K = OuterKeyword in |
|
247 |
||
25192
b568f8c5d5ca
made command 'undo' silent ('ProofGeneral.undo' becomes a historical relic);
wenzelm
parents:
24882
diff
changeset
|
248 |
fun undoP () = (*undo without output -- historical*) |
21642 | 249 |
OuterSyntax.improper_command "ProofGeneral.undo" "(internal)" K.control |
250 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.undo)); |
|
251 |
||
24867 | 252 |
fun restartP () = |
21642 | 253 |
OuterSyntax.improper_command "ProofGeneral.restart" "(internal)" K.control |
254 |
(P.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative restart))); |
|
255 |
||
24867 | 256 |
fun kill_proofP () = |
21642 | 257 |
OuterSyntax.improper_command "ProofGeneral.kill_proof" "(internal)" K.control |
258 |
(Scan.succeed (Toplevel.no_timing o IsarCmd.kill_proof_notify tell_clear_goals)); |
|
259 |
||
24867 | 260 |
fun inform_file_processedP () = |
21642 | 261 |
OuterSyntax.improper_command "ProofGeneral.inform_file_processed" "(internal)" K.control |
262 |
(P.name >> (fn file => Toplevel.no_timing o |
|
25443
1af14e8f225b
inform_file_processed: made even more robust against bad file specs;
wenzelm
parents:
25442
diff
changeset
|
263 |
Toplevel.imperative (fn () => error "Bad file name") o |
1af14e8f225b
inform_file_processed: made even more robust against bad file specs;
wenzelm
parents:
25442
diff
changeset
|
264 |
Toplevel.init_empty (fn _ => file <> "") (vacuous_inform_file_processed file) o |
21642 | 265 |
Toplevel.kill o |
25442 | 266 |
Toplevel.init_empty (fn _ => check_thy file) (proper_inform_file_processed file))); |
21642 | 267 |
|
24867 | 268 |
fun inform_file_retractedP () = |
21642 | 269 |
OuterSyntax.improper_command "ProofGeneral.inform_file_retracted" "(internal)" K.control |
270 |
(P.name >> (Toplevel.no_timing oo |
|
271 |
(fn file => Toplevel.imperative (fn () => inform_file_retracted file)))); |
|
272 |
||
24867 | 273 |
fun process_pgipP () = |
21642 | 274 |
OuterSyntax.improper_command "ProofGeneral.process_pgip" "(internal)" K.control |
275 |
(P.text >> (Toplevel.no_timing oo |
|
276 |
(fn txt => Toplevel.imperative (fn () => ProofGeneralPgip.process_pgip txt)))); |
|
277 |
||
24867 | 278 |
fun init_outer_syntax () = List.app (fn f => f ()) |
21948 | 279 |
[undoP, restartP, kill_proofP, inform_file_processedP, inform_file_retractedP, process_pgipP]; |
21642 | 280 |
|
281 |
end; |
|
282 |
||
283 |
||
284 |
(* init *) |
|
285 |
||
286 |
val initialized = ref false; |
|
287 |
||
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
288 |
fun init false = panic "No Proof General interface support for Isabelle/classic mode." |
21940 | 289 |
| init true = |
21968 | 290 |
(! initialized orelse |
22590 | 291 |
(Output.no_warnings init_outer_syntax (); |
21968 | 292 |
setup_xsymbols_output (); |
293 |
setup_messages (); |
|
22590 | 294 |
ProofGeneralPgip.init_pgip_channel (! Output.priority_fn); |
21968 | 295 |
setup_thy_loader (); |
296 |
setup_present_hook (); |
|
297 |
set initialized); |
|
298 |
sync_thy_loader (); |
|
25749 | 299 |
change print_mode (update (op =) proof_generalN); |
26643 | 300 |
Isar.toplevel_loop {init = true, welcome = true, sync = true, secure = Secure.is_secure ()}); |
21642 | 301 |
|
302 |
end; |