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