author | wenzelm |
Mon, 09 Aug 2010 18:18:32 +0200 | |
changeset 38253 | 3d4e521014f7 |
parent 38236 | d8c7be27e01d |
child 38798 | 89f273ab1d42 |
permissions | -rw-r--r-- |
21642 | 1 |
(* Title: Pure/ProofGeneral/proof_general_emacs.ML |
2 |
Author: David Aspinall and Markus Wenzel |
|
3 |
||
4 |
Isabelle/Isar configuration for Emacs Proof General. |
|
26549 | 5 |
See also http://proofgeneral.inf.ed.ac.uk |
21642 | 6 |
*) |
7 |
||
8 |
signature PROOF_GENERAL = |
|
9 |
sig |
|
26549 | 10 |
val test_markupN: string |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
11 |
val sendback: string -> Pretty.T list -> unit |
21642 | 12 |
val init: bool -> unit |
24874 | 13 |
val init_outer_syntax: unit -> unit |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
14 |
structure ThyLoad: sig val add_path: string -> unit end |
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 thm_depsN = "thm_deps"; (*meta-information about theorem deps*) |
|
26549 | 25 |
val test_markupN = "test_markup"; (*XML markup for everything*) |
26 |
||
29321
6b9ecb3a70ab
removed print mode "PGASCII" -- 7-bit ASCII communication now always enabled;
wenzelm
parents:
28426
diff
changeset
|
27 |
fun special ch = Symbol.SOH ^ ch; |
21642 | 28 |
|
29 |
||
29326 | 30 |
(* render markup *) |
21642 | 31 |
|
32 |
local |
|
33 |
||
29326 | 34 |
fun render_trees ts = fold render_tree ts |
35 |
and render_tree (XML.Text s) = Buffer.add s |
|
38228
ada3ab6b9085
simplified type XML.tree: embed Markup.T directly, avoid slightly odd triple;
wenzelm
parents:
37981
diff
changeset
|
36 |
| render_tree (XML.Elem ((name, props), ts)) = |
29326 | 37 |
let |
38 |
val (bg1, en1) = |
|
39 |
if name <> Markup.promptN andalso print_mode_active test_markupN |
|
40 |
then XML.output_markup (name, props) |
|
41 |
else Markup.no_output; |
|
42 |
val (bg2, en2) = |
|
30292
a3bb22493f11
render_tree: suppress markup only for empty body (of status messages, cf. da275b7809bd) in order to recover hilite;
wenzelm
parents:
29349
diff
changeset
|
43 |
if null ts then Markup.no_output |
29326 | 44 |
else if name = Markup.stateN then (special "O" ^ "\n", "\n" ^ special "P") |
45 |
else if name = Markup.sendbackN then (special "W", special "X") |
|
46 |
else if name = Markup.hiliteN then (special "0", special "1") |
|
47 |
else if name = Markup.tclassN then (special "B", special "A") |
|
48 |
else if name = Markup.tfreeN then (special "C", special "A") |
|
49 |
else if name = Markup.tvarN then (special "D", special "A") |
|
50 |
else if name = Markup.freeN then (special "E", special "A") |
|
51 |
else if name = Markup.boundN then (special "F", special "A") |
|
52 |
else if name = Markup.varN then (special "G", special "A") |
|
53 |
else if name = Markup.skolemN then (special "H", special "A") |
|
54 |
else Markup.no_output; |
|
55 |
in |
|
56 |
Buffer.add bg1 #> |
|
57 |
Buffer.add bg2 #> |
|
58 |
render_trees ts #> |
|
59 |
Buffer.add en2 #> |
|
60 |
Buffer.add en1 |
|
61 |
end; |
|
21642 | 62 |
|
63 |
in |
|
64 |
||
29326 | 65 |
fun render text = |
66 |
Buffer.content (render_trees (YXML.parse_body text) Buffer.empty); |
|
21642 | 67 |
|
68 |
end; |
|
69 |
||
70 |
||
29326 | 71 |
(* messages *) |
72 |
||
73 |
fun message bg en prfx text = |
|
74 |
(case render text of |
|
75 |
"" => () |
|
76 |
| s => Output.writeln_default (enclose bg en (prefix_lines prfx s))); |
|
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
77 |
|
29326 | 78 |
fun setup_messages () = |
79 |
(Output.writeln_fn := message "" "" ""; |
|
30670
9bb872667af6
suppress status output for traditional tty modes (including Proof General);
wenzelm
parents:
30292
diff
changeset
|
80 |
Output.status_fn := (fn _ => ()); |
38236
d8c7be27e01d
explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents:
38228
diff
changeset
|
81 |
Output.report_fn := (fn _ => ()); |
29326 | 82 |
Output.priority_fn := message (special "I") (special "J") ""; |
83 |
Output.tracing_fn := message (special "I" ^ special "V") (special "J") ""; |
|
84 |
Output.debug_fn := message (special "K") (special "L") "+++ "; |
|
85 |
Output.warning_fn := message (special "K") (special "L") "### "; |
|
86 |
Output.error_fn := message (special "M") (special "N") "*** "; |
|
87 |
Output.prompt_fn := (fn s => Output.std_output (render s ^ special "S"))); |
|
88 |
||
89 |
fun panic s = |
|
90 |
(message (special "M") (special "N") "!!! " ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
|
23641
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
91 |
|
d6f9d3acffaa
toplevel prompt/print_state: proper markup, removed hooks;
wenzelm
parents:
23621
diff
changeset
|
92 |
|
29326 | 93 |
(* notification *) |
21642 | 94 |
|
29326 | 95 |
val emacs_notify = message (special "I") (special "J") ""; |
21642 | 96 |
|
97 |
fun tell_clear_goals () = |
|
21940 | 98 |
emacs_notify "Proof General, please clear the goals buffer."; |
21642 | 99 |
|
100 |
fun tell_clear_response () = |
|
21940 | 101 |
emacs_notify "Proof General, please clear the response buffer."; |
21642 | 102 |
|
103 |
fun tell_file_loaded path = |
|
21940 | 104 |
emacs_notify ("Proof General, this file is loaded: " ^ quote (File.platform_path path)); |
21642 | 105 |
|
106 |
fun tell_file_retracted path = |
|
21940 | 107 |
emacs_notify ("Proof General, you can unlock the file " ^ quote (File.platform_path path)); |
21642 | 108 |
|
24289 | 109 |
fun sendback heading prts = |
110 |
Pretty.writeln (Pretty.big_list heading [Pretty.markup Markup.sendback prts]); |
|
111 |
||
21642 | 112 |
|
113 |
(* theory loader actions *) |
|
114 |
||
115 |
local |
|
116 |
||
117 |
fun trace_action action name = |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
36953
diff
changeset
|
118 |
if action = Thy_Info.Update then |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
36953
diff
changeset
|
119 |
List.app tell_file_loaded (Thy_Info.loaded_files name) |
37981
9a15982f41fe
theory loader: removed obsolete touch/outdate operations (require_thy no longer changes the database implicitly);
wenzelm
parents:
37977
diff
changeset
|
120 |
else if action = Thy_Info.Remove then |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
36953
diff
changeset
|
121 |
List.app tell_file_retracted (Thy_Info.loaded_files name) |
21642 | 122 |
else (); |
123 |
||
124 |
in |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
36953
diff
changeset
|
125 |
fun setup_thy_loader () = Thy_Info.add_hook trace_action; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
36953
diff
changeset
|
126 |
fun sync_thy_loader () = List.app (trace_action Thy_Info.Update) (Thy_Info.get_names ()); |
21642 | 127 |
end; |
128 |
||
129 |
||
21948 | 130 |
(* get informed about files *) |
21642 | 131 |
|
25442 | 132 |
(*liberal low-level version*) |
133 |
val thy_name = perhaps (try (unsuffix ".thy")) o List.last o space_explode "/"; |
|
134 |
||
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
135 |
val inform_file_retracted = Thy_Info.kill_thy o thy_name; |
25442 | 136 |
|
27577
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
137 |
fun inform_file_processed file = |
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
138 |
let |
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
139 |
val name = thy_name file; |
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
140 |
val _ = name = "" andalso error ("Bad file name: " ^ quote file); |
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
141 |
val _ = |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
142 |
Thy_Info.register_thy (Toplevel.end_theory Position.none (Isar.state ())) |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
143 |
handle ERROR msg => |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
144 |
(warning (cat_lines ["Failed to register theory: " ^ quote name, msg]); |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
145 |
tell_file_retracted (Thy_Header.thy_path name)) |
29349 | 146 |
val _ = Isar.init (); |
27577
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
147 |
in () end; |
21642 | 148 |
|
149 |
||
150 |
(* restart top-level loop (keeps most state information) *) |
|
151 |
||
152 |
val welcome = priority o Session.welcome; |
|
153 |
||
154 |
fun restart () = |
|
21940 | 155 |
(sync_thy_loader (); |
156 |
tell_clear_goals (); |
|
157 |
tell_clear_response (); |
|
29349 | 158 |
Isar.init (); |
27577
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
159 |
welcome ()); |
21642 | 160 |
|
161 |
||
162 |
(* theorem dependency output *) |
|
163 |
||
164 |
local |
|
165 |
||
166 |
val spaces_quote = space_implode " " o map quote; |
|
167 |
||
168 |
fun thm_deps_message (thms, deps) = |
|
21948 | 169 |
emacs_notify ("Proof General, theorem dependencies of " ^ thms ^ " are " ^ deps); |
21642 | 170 |
|
28096
046418f64474
theorem dependency output: Toplevel.add_hook, ProofGeneralPgip.new_thms_deps;
wenzelm
parents:
28020
diff
changeset
|
171 |
in |
046418f64474
theorem dependency output: Toplevel.add_hook, ProofGeneralPgip.new_thms_deps;
wenzelm
parents:
28020
diff
changeset
|
172 |
|
28103
b79e61861f0f
simplified Toplevel.add_hook: cover successful transactions only;
wenzelm
parents:
28100
diff
changeset
|
173 |
fun setup_present_hook () = Toplevel.add_hook (fn _ => fn state => fn state' => |
28106 | 174 |
if print_mode_active thm_depsN andalso |
175 |
can Toplevel.theory_of state andalso Toplevel.is_theory state' |
|
176 |
then |
|
28100
7650d5e0f8fb
refined theorem dependency output: previous state needs to contain a theory (not empty toplevel);
wenzelm
parents:
28096
diff
changeset
|
177 |
let val (names, deps) = |
7650d5e0f8fb
refined theorem dependency output: previous state needs to contain a theory (not empty toplevel);
wenzelm
parents:
28096
diff
changeset
|
178 |
ProofGeneralPgip.new_thms_deps (Toplevel.theory_of state) (Toplevel.theory_of state') |
7650d5e0f8fb
refined theorem dependency output: previous state needs to contain a theory (not empty toplevel);
wenzelm
parents:
28096
diff
changeset
|
179 |
in |
21968 | 180 |
if null names orelse null deps then () |
181 |
else thm_deps_message (spaces_quote names, spaces_quote deps) |
|
182 |
end |
|
28096
046418f64474
theorem dependency output: Toplevel.add_hook, ProofGeneralPgip.new_thms_deps;
wenzelm
parents:
28020
diff
changeset
|
183 |
else ()); |
21642 | 184 |
|
185 |
end; |
|
186 |
||
187 |
||
188 |
(* additional outer syntax for Isar *) |
|
189 |
||
33872 | 190 |
fun prP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
191 |
Outer_Syntax.improper_command "ProofGeneral.pr" "print state (internal)" Keyword.diag |
33872 | 192 |
(Scan.succeed (Toplevel.no_timing o Toplevel.keep (fn state => |
193 |
if Toplevel.is_toplevel state orelse Toplevel.is_theory state then tell_clear_goals () |
|
194 |
else (Toplevel.quiet := false; Toplevel.print_state true state)))); |
|
195 |
||
25192
b568f8c5d5ca
made command 'undo' silent ('ProofGeneral.undo' becomes a historical relic);
wenzelm
parents:
24882
diff
changeset
|
196 |
fun undoP () = (*undo without output -- historical*) |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
197 |
Outer_Syntax.improper_command "ProofGeneral.undo" "(internal)" Keyword.control |
27535 | 198 |
(Scan.succeed (Toplevel.no_timing o Toplevel.imperative (fn () => Isar.undo 1))); |
21642 | 199 |
|
24867 | 200 |
fun restartP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
201 |
Outer_Syntax.improper_command "ProofGeneral.restart" "(internal)" Keyword.control |
36950 | 202 |
(Parse.opt_unit >> (Toplevel.no_timing oo K (Toplevel.imperative restart))); |
21642 | 203 |
|
24867 | 204 |
fun kill_proofP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
205 |
Outer_Syntax.improper_command "ProofGeneral.kill_proof" "(internal)" Keyword.control |
27532 | 206 |
(Scan.succeed (Toplevel.no_timing o |
207 |
Toplevel.imperative (fn () => (Isar.kill_proof (); tell_clear_goals ())))); |
|
208 |
||
24867 | 209 |
fun inform_file_processedP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
210 |
Outer_Syntax.improper_command "ProofGeneral.inform_file_processed" "(internal)" Keyword.control |
36950 | 211 |
(Parse.name >> (fn file => |
27577
7c7a9a343ca5
proper_inform_file_processed: new implementation based on global Isar state (cf. isar.ML);
wenzelm
parents:
27537
diff
changeset
|
212 |
Toplevel.no_timing o Toplevel.imperative (fn () => inform_file_processed file))); |
21642 | 213 |
|
24867 | 214 |
fun inform_file_retractedP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
215 |
Outer_Syntax.improper_command "ProofGeneral.inform_file_retracted" "(internal)" Keyword.control |
36950 | 216 |
(Parse.name >> (Toplevel.no_timing oo |
21642 | 217 |
(fn file => Toplevel.imperative (fn () => inform_file_retracted file)))); |
218 |
||
24867 | 219 |
fun process_pgipP () = |
36953
2af1ad9aa1a3
renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents:
36950
diff
changeset
|
220 |
Outer_Syntax.improper_command "ProofGeneral.process_pgip" "(internal)" Keyword.control |
36950 | 221 |
(Parse.text >> (Toplevel.no_timing oo |
21642 | 222 |
(fn txt => Toplevel.imperative (fn () => ProofGeneralPgip.process_pgip txt)))); |
223 |
||
24867 | 224 |
fun init_outer_syntax () = List.app (fn f => f ()) |
33872 | 225 |
[prP, undoP, restartP, kill_proofP, inform_file_processedP, |
226 |
inform_file_retractedP, process_pgipP]; |
|
21642 | 227 |
|
228 |
||
229 |
(* init *) |
|
230 |
||
32738 | 231 |
val initialized = Unsynchronized.ref false; |
21642 | 232 |
|
22699
938c1011ac94
removed unused Output.panic hook -- internal to PG wrapper;
wenzelm
parents:
22678
diff
changeset
|
233 |
fun init false = panic "No Proof General interface support for Isabelle/classic mode." |
21940 | 234 |
| init true = |
21968 | 235 |
(! initialized orelse |
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32738
diff
changeset
|
236 |
(Output.no_warnings_CRITICAL init_outer_syntax (); |
29326 | 237 |
Output.add_mode Symbol.xsymbolsN Symbol.output Output.default_escape; |
238 |
Output.add_mode proof_generalN Output.default_output Output.default_escape; |
|
239 |
Markup.add_mode proof_generalN YXML.output_markup; |
|
21968 | 240 |
setup_messages (); |
22590 | 241 |
ProofGeneralPgip.init_pgip_channel (! Output.priority_fn); |
21968 | 242 |
setup_thy_loader (); |
243 |
setup_present_hook (); |
|
32738 | 244 |
Unsynchronized.set initialized); |
21968 | 245 |
sync_thy_loader (); |
32738 | 246 |
Unsynchronized.change print_mode (update (op =) proof_generalN); |
37239 | 247 |
Secure.PG_setup (); |
38253
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
38236
diff
changeset
|
248 |
Isar.toplevel_loop TextIO.stdIn |
3d4e521014f7
Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents:
38236
diff
changeset
|
249 |
{init = true, welcome = true, sync = true, secure = Secure.is_secure ()}); |
21642 | 250 |
|
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
251 |
|
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
252 |
(* fake old ThyLoad -- with new semantics *) |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
253 |
|
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
254 |
structure ThyLoad = |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
255 |
struct |
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
256 |
val add_path = Thy_Load.set_master_path o Path.explode; |
21642 | 257 |
end; |
37977
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
258 |
|
3ceccd415145
simplified/clarified theory loader: more explicit task management, kill old versions at start, commit results only in the very end, non-optional master dependency, do not store text in deps;
wenzelm
parents:
37954
diff
changeset
|
259 |
end; |