| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Ideutils |
| 12 |
open Session |
| 13 |
open Preferences |
| 14 |
open GdkKeysyms |
| 15 |
open Printf |
| 16 |
|
| 17 |
let eprintf x = |
| 18 |
if CDebug.(get_flag misc) then Printf.eprintf x else Printf.ifprintf stderr x |
| 19 |
|
| 20 |
type gui = { |
| 21 |
notebook : session Wg_Notebook.typed_notebook; |
| 22 |
action_groups : GAction.action_group list; |
| 23 |
} |
| 24 |
|
| 25 |
let actiong gui name = List.find (fun ag -> ag#name = name) gui.action_groups |
| 26 |
let ct gui = gui.notebook#current_term |
| 27 |
|
| 28 |
let get_sel b = b#selection_bounds |
| 29 |
let sel_nonempty b = let i, j = get_sel b in not (i#equal j) |
| 30 |
let get_sel_txt b = let i, j = get_sel b in i#get_text ~stop:j |
| 31 |
|
| 32 |
type status = { move : int option; kill : (string * bool) option; sel: bool } |
| 33 |
|
| 34 |
let pr_status { move; kill; sel } = |
| 35 |
let move = Option.cata (fun i -> string_of_int i) "" move in |
| 36 |
let kill = Option.cata (fun (s,b) -> sprintf "kill(%b) %S" b s) "" kill in |
| 37 |
let sel = string_of_bool sel in |
| 38 |
Printf.sprintf "{ move: %s; kill: %s; sel: %s }" move kill sel |
| 39 |
let pr_key t = |
| 40 |
let kv = GdkEvent.Key.keyval t in |
| 41 |
let str = GdkEvent.Key.string t in |
| 42 |
let str_of_mod = function |
| 43 |
| `SHIFT -> "SHIFT" | `LOCK -> "LOCK" | `CONTROL -> "CONTROL" |
| 44 |
| `MOD1 -> "MOD1" | `MOD2 -> "MOD2" | `MOD3 -> "MOD3" | `MOD4 -> "MOD4" |
| 45 |
| `MOD5 -> "MOD5" | `BUTTON1 -> "BUTTON1" | `BUTTON2 -> "BUTTON2" |
| 46 |
| `BUTTON3 -> "BUTTON3" | `BUTTON4 -> "BUTTON4" | `BUTTON5 -> "BUTTON5" |
| 47 |
| `SUPER -> "SUPER" | `HYPER -> "HYPER" | `META -> "META" |
| 48 |
| `RELEASE -> "RELEASE" in |
| 49 |
let mods = String.concat " " (List.map str_of_mod (GdkEvent.Key.state t)) in |
| 50 |
Printf.sprintf "'%s' (%d, %s)" str kv mods |
| 51 |
|
| 52 |
type action = |
| 53 |
| Action of string * string |
| 54 |
| Callback of (gui -> unit) |
| 55 |
| Edit of (status -> GSourceView3.source_buffer -> GText.iter -> |
| 56 |
(string -> string -> unit) -> status) |
| 57 |
| Motion of (status -> GText.iter -> GText.iter * status) |
| 58 |
|
| 59 |
type 'c entry = { |
| 60 |
mods : Gdk.Tags.modifier list; |
| 61 |
key : Gdk.keysym; |
| 62 |
keyname : string; |
| 63 |
doc : string; |
| 64 |
contents : 'c |
| 65 |
} |
| 66 |
|
| 67 |
let mC = [`CONTROL] |
| 68 |
let mM = |
| 69 |
if Coq_config.arch = "Darwin" then |
| 70 |
|
| 71 |
|
| 72 |
[`CONTROL;`MOD2;`META] |
| 73 |
else |
| 74 |
[`MOD1] |
| 75 |
|
| 76 |
let mod_of t x = |
| 77 |
let y = GdkEvent.Key.state t in |
| 78 |
List.for_all (fun m -> List.mem m y) x && |
| 79 |
List.for_all (fun m -> List.mem m x) y |
| 80 |
|
| 81 |
let pr_keymod l = |
| 82 |
if l = mC then |
| 83 |
"Ctrl-" |
| 84 |
else |
| 85 |
if l = mM then |
| 86 |
if Coq_config.arch = "Darwin" then "Ctrl-Cmd-" else "Meta-" |
| 87 |
else |
| 88 |
"" |
| 89 |
|
| 90 |
let mkE ?(mods=mC) key keyname doc ?(alias=[]) contents = |
| 91 |
List.map (fun (mods, key, keyname) -> { mods; key; keyname; doc; contents }) |
| 92 |
((mods, key, keyname)::alias) |
| 93 |
|
| 94 |
type keypaths = Step of action entry list * keypaths entry list |
| 95 |
|
| 96 |
let print_keypaths kps = |
| 97 |
let rec aux prefix (Step (l, konts)) = |
| 98 |
String.concat "\n" ( |
| 99 |
(List.map (fun x -> |
| 100 |
prefix ^ pr_keymod x.mods ^ x.keyname ^ " " ^ x.doc ) l) @ |
| 101 |
(List.map (fun x -> |
| 102 |
aux (prefix^pr_keymod x.mods^x.keyname^" ") x.contents) konts)) in |
| 103 |
aux " " kps |
| 104 |
|
| 105 |
let empty = Step([],[]) |
| 106 |
|
| 107 |
let frontier (Step(l1,l2)) = |
| 108 |
List.map (fun x -> pr_keymod x.mods ^ x.keyname) l1 @ |
| 109 |
List.map (fun x -> pr_keymod x.mods ^ x.keyname) l2 |
| 110 |
|
| 111 |
let insert kps name enter_syms bindings = |
| 112 |
let rec aux kps enter_syms = |
| 113 |
match enter_syms, kps with |
| 114 |
| [], Step (el, konts) -> Step (List.flatten bindings @ el, konts) |
| 115 |
| (mods, key, keyname)::gs, Step (el, konts) -> |
| 116 |
if List.exists (fun { key = k; mods = m } -> key = k && mods = m) konts |
| 117 |
then |
| 118 |
let konts = |
| 119 |
List.map |
| 120 |
(fun ({ key = k; contents } as x) -> |
| 121 |
if key <> k then x else { x with contents = aux contents gs }) |
| 122 |
konts in |
| 123 |
Step(el,konts) |
| 124 |
else |
| 125 |
let kont = |
| 126 |
{ mods; key; keyname; doc = name; contents = aux empty gs } in |
| 127 |
Step(el, kont::konts) in |
| 128 |
aux kps enter_syms |
| 129 |
|
| 130 |
let run_action gui group name = |
| 131 |
((actiong gui group)#get_action name)#activate () |
| 132 |
|
| 133 |
let run key gui action status = |
| 134 |
match action with |
| 135 |
| Callback f -> f gui; status |
| 136 |
| Action(group, name) -> run_action gui group name; status |
| 137 |
| Edit f -> |
| 138 |
let b = (ct gui).script#source_buffer in |
| 139 |
let i = b#get_iter_at_mark `INSERT in |
| 140 |
let status = f status b i (run_action gui) in |
| 141 |
if not status.sel then |
| 142 |
b#place_cursor ~where:(b#get_iter_at_mark `SEL_BOUND); |
| 143 |
status |
| 144 |
| Motion f -> |
| 145 |
let b, script = (ct gui).script#source_buffer, (ct gui).script in |
| 146 |
let sel_mode = status.sel || List.mem `SHIFT (GdkEvent.Key.state key) in |
| 147 |
let i = |
| 148 |
if sel_mode then b#get_iter_at_mark `SEL_BOUND |
| 149 |
else b#get_iter_at_mark `INSERT in |
| 150 |
let where, status = f status i in |
| 151 |
let sel_mode = status.sel || List.mem `SHIFT (GdkEvent.Key.state key) in |
| 152 |
if sel_mode then (b#move_mark `SEL_BOUND ~where; script#scroll_mark_onscreen `SEL_BOUND) |
| 153 |
else (b#place_cursor ~where; script#scroll_mark_onscreen `INSERT); |
| 154 |
status |
| 155 |
|
| 156 |
let emacs = empty |
| 157 |
|
| 158 |
let emacs = insert emacs "Emacs" [] [ |
| 159 |
|
| 160 |
mkE _e "e" "Move to end of line" (Motion(fun s i -> |
| 161 |
(if not i#ends_line then i#forward_to_line_end else i), |
| 162 |
{ s with move = None })); |
| 163 |
mkE ~mods:mM _Right "->" "Move to end of buffer" (Motion(fun s i -> |
| 164 |
i#forward_to_end, |
| 165 |
{ s with move = None })); |
| 166 |
mkE ~mods:mM _Left "<-" "Move to start of buffer" (Motion(fun s i -> |
| 167 |
let buffer = new GText.buffer i#buffer in |
| 168 |
buffer#start_iter, |
| 169 |
{ s with move = None })); |
| 170 |
mkE _a "a" "Move to beginning of line" (Motion(fun s i -> |
| 171 |
(i#set_line_offset 0), { s with move = None })); |
| 172 |
mkE ~mods:mM _e "e" "Move to end of sentence" (Motion(fun s i -> |
| 173 |
i#forward_sentence_end, { s with move = None })); |
| 174 |
mkE ~mods:mM _a "a" "Move to beginning of sentence" (Motion(fun s i -> |
| 175 |
i#backward_sentence_start, { s with move = None })); |
| 176 |
mkE _n "n" "Move to next line" (Motion(fun s i -> |
| 177 |
let orig_off = Option.default i#line_offset s.move in |
| 178 |
let i = i#forward_line in |
| 179 |
let new_off = min (i#chars_in_line - 1) orig_off in |
| 180 |
(if new_off > 0 then i#set_line_offset new_off else i), |
| 181 |
{ s with move = Some orig_off })); |
| 182 |
mkE _p "p" "Move to previous line" (Motion(fun s i -> |
| 183 |
let orig_off = Option.default i#line_offset s.move in |
| 184 |
let i = i#backward_line in |
| 185 |
let new_off = min (i#chars_in_line - 1) orig_off in |
| 186 |
(if new_off > 0 then i#set_line_offset new_off else i), |
| 187 |
{ s with move = Some orig_off })); |
| 188 |
mkE _f "f" "Forward char" ~alias:[[],_Right,"RIGHT"] |
| 189 |
(Motion(fun s i -> i#forward_char, { s with move = None })); |
| 190 |
mkE _b "b" "Backward char" ~alias:[[],_Left,"LEFT"] |
| 191 |
(Motion(fun s i -> i#backward_char, { s with move = None })); |
| 192 |
mkE ~mods:mM _f "f" "Forward word" ~alias:[mC,_Right,"RIGHT"] |
| 193 |
(Motion(fun s i -> i#forward_word_end, { s with move = None })); |
| 194 |
mkE ~mods:mM _b "b" "Backward word" ~alias:[mC,_Left,"LEFT"] |
| 195 |
(Motion(fun s i -> i#backward_word_start, { s with move = None })); |
| 196 |
mkE _space "SPC" "Set mark" ~alias:[mC,_at,"@"] (Motion(fun s i -> |
| 197 |
if s.sel = false then i, { s with sel = true } |
| 198 |
else i, { s with sel = false } )); |
| 199 |
|
| 200 |
mkE ~mods:mM _w "w" "Copy selected region" (Edit(fun s b i run -> |
| 201 |
if sel_nonempty b then |
| 202 |
let txt = get_sel_txt b in |
| 203 |
run "Edit" "Copy"; |
| 204 |
{ s with kill = Some(txt,false); sel = false } |
| 205 |
else s)); |
| 206 |
mkE _w "w" "Kill selected region" (Edit(fun s b i run -> |
| 207 |
if sel_nonempty b then |
| 208 |
let txt = get_sel_txt b in |
| 209 |
run "Edit" "Cut"; |
| 210 |
{ s with kill = Some(txt,false); sel = false } |
| 211 |
else s)); |
| 212 |
mkE _k "k" "Kill until the end of line" (Edit(fun s b i _ -> |
| 213 |
let already_killed = match s.kill with Some (k,true) -> k | _ -> "" in |
| 214 |
let k = |
| 215 |
if i#ends_line then begin |
| 216 |
b#delete ~start:i ~stop:i#forward_char; "\n" |
| 217 |
end else begin |
| 218 |
let k = b#get_text ~start:i ~stop:i#forward_to_line_end () in |
| 219 |
b#delete ~start:i ~stop:i#forward_to_line_end; k |
| 220 |
end in |
| 221 |
{ s with kill = Some (already_killed ^ k,true) })); |
| 222 |
mkE ~mods:mM _d "d" "Kill next word" (Edit(fun s b i _ -> |
| 223 |
let already_killed = match s.kill with Some (k,true) -> k | _ -> "" in |
| 224 |
let k = |
| 225 |
let k = b#get_text ~start:i ~stop:i#forward_word_end () in |
| 226 |
b#delete ~start:i ~stop:i#forward_word_end; k in |
| 227 |
{ s with kill = Some (already_killed ^ k,true) })); |
| 228 |
mkE ~mods:mM _k "k" "Kill until sentence end" (Edit(fun s b i _ -> |
| 229 |
let already_killed = match s.kill with Some (k,true) -> k | _ -> "" in |
| 230 |
let k = |
| 231 |
let k = b#get_text ~start:i ~stop:i#forward_sentence_end () in |
| 232 |
b#delete ~start:i ~stop:i#forward_sentence_end; k in |
| 233 |
{ s with kill = Some (already_killed ^ k,true) })); |
| 234 |
mkE ~mods:mM _BackSpace "DELBACK" "Kill word before cursor" |
| 235 |
(Edit(fun s b i _ -> |
| 236 |
let already_killed = match s.kill with Some (k,true) -> k | _ -> "" in |
| 237 |
let k = |
| 238 |
let k = b#get_text ~start:i ~stop:i#backward_word_start () in |
| 239 |
b#delete ~start:i ~stop:i#backward_word_start; k in |
| 240 |
{ s with kill = Some (already_killed ^ k,true) })); |
| 241 |
mkE _d "d" "Delete next character" (Edit(fun s b i _ -> |
| 242 |
b#delete ~start:i ~stop:i#forward_char; s)); |
| 243 |
mkE _y "y" "Yank killed text back " (Edit(fun s b i _ -> |
| 244 |
let k, s = match s.kill with |
| 245 |
| Some (k,_) -> k, { s with kill = Some (k,false) } |
| 246 |
| _ -> "", s in |
| 247 |
b#insert ~iter:i k; |
| 248 |
s)); |
| 249 |
|
| 250 |
mkE _underscore "_" "Undo" (Action("Edit", "Undo")); |
| 251 |
mkE _g "g" "Esc" (Callback(fun gui -> (ct gui).finder#hide ())); |
| 252 |
mkE _s "s" "Search" (Callback(fun gui -> |
| 253 |
if (ct gui).finder#coerce#misc#visible |
| 254 |
then run_action gui "Edit" "Find Next" |
| 255 |
else run_action gui "Edit" "Find")); |
| 256 |
mkE _s "r" "Search backward" (Callback(fun gui -> |
| 257 |
if (ct gui).finder#coerce#misc#visible |
| 258 |
then run_action gui "Edit" "Find Previous" |
| 259 |
else run_action gui "Edit" "Find")); |
| 260 |
] |
| 261 |
|
| 262 |
let emacs = insert emacs "Emacs" [mC,_x,"x"] [ |
| 263 |
mkE _s "s" "Save" (Action("File", "Save")); |
| 264 |
mkE _c "c" "Quit" (Action("File", "Quit")); |
| 265 |
mkE _f "f" "Open" (Action("File", "Open")); |
| 266 |
mkE ~mods:[] _u "u" "Undo" (Action("Edit", "Undo")); |
| 267 |
] |
| 268 |
|
| 269 |
let pg = insert emacs "Proof General" [mC,_c,"c"] [ |
| 270 |
mkE _Return "RET" "Go to" (Action("Navigation", "Go to")); |
| 271 |
mkE _n "n" "Advance 1 sentence" (Action("Navigation", "Forward")); |
| 272 |
mkE _u "u" "Retract 1 sentence" (Action("Navigation", "Backward")); |
| 273 |
mkE _b "b" "Advance" (Action("Navigation", "End")); |
| 274 |
mkE _r "r" "Reset" (Action("Navigation", "Reset")); |
| 275 |
mkE _c "c" "Stop" (Action("Navigation", "Interrupt")); |
| 276 |
] |
| 277 |
|
| 278 |
let command gui c = |
| 279 |
let command = (ct gui).command in |
| 280 |
let script = (ct gui).script in |
| 281 |
let term = |
| 282 |
let i, j = script#source_buffer#selection_bounds in |
| 283 |
if i#equal j then None |
| 284 |
else Some (script#buffer#get_text ~start:i ~stop:j ()) in |
| 285 |
command#show; |
| 286 |
command#new_query ~command:c ?term () |
| 287 |
|
| 288 |
let pg = insert pg "Proof General" [mC,_c,"c"; mC,_a,"a"] [ |
| 289 |
mkE _p "p" "Print" (Callback (fun gui -> command gui "Print")); |
| 290 |
mkE _c "c" "Check" (Callback (fun gui -> command gui "Check")); |
| 291 |
mkE _b "b" "About" (Callback (fun gui -> command gui "About")); |
| 292 |
mkE _o "o" "Search Pattern" (Callback (fun gui->command gui "SearchPattern")); |
| 293 |
mkE _l "l" "Locate" (Callback (fun gui -> command gui "Locate")); |
| 294 |
mkE _Return "RET" "match template" (Action("Templates","match")); |
| 295 |
] |
| 296 |
|
| 297 |
let empty = { sel = false; kill = None; move = None } |
| 298 |
|
| 299 |
let find gui (Step(here,konts)) t = |
| 300 |
|
| 301 |
let sel_nonempty () = sel_nonempty (ct gui).script#source_buffer in |
| 302 |
let k = GdkEvent.Key.keyval t in |
| 303 |
if k = _x && mod_of t mC && sel_nonempty () then |
| 304 |
ignore(run t gui (Action("Edit","Cut")) empty) |
| 305 |
else |
| 306 |
if k = _c && mod_of t mC && sel_nonempty () then |
| 307 |
ignore(run t gui (Action("Edit","Copy")) empty); |
| 308 |
let cmp { key; mods } = key = k && mod_of t mods in |
| 309 |
try `Do (List.find cmp here) with Not_found -> |
| 310 |
try `Cont (List.find cmp konts).contents with Not_found -> `NotFound |
| 311 |
|
| 312 |
let init w nb ags = |
| 313 |
let gui = { notebook = nb; action_groups = ags } in |
| 314 |
let cur = ref pg in |
| 315 |
let status = ref empty in |
| 316 |
let reset () = eprintf "reset\n%!"; cur := pg in |
| 317 |
ignore(w#event#connect#key_press ~callback:(fun t -> |
| 318 |
let on_current_term f = |
| 319 |
let term = try Some nb#current_term with Invalid_argument _ -> None in |
| 320 |
match term with None -> false | Some t -> f t |
| 321 |
in |
| 322 |
on_current_term (fun x -> |
| 323 |
if x.script#misc#get_property "has-focus" <> `BOOL true |
| 324 |
then false |
| 325 |
else begin |
| 326 |
eprintf "got key %s\n%!" (pr_key t); |
| 327 |
if microPG#get then begin |
| 328 |
match find gui !cur t with |
| 329 |
| `Do e -> |
| 330 |
eprintf "run (%s) %s on %s\n%!" e.keyname e.doc (pr_status !status); |
| 331 |
status := run t gui e.contents !status; reset (); true |
| 332 |
| `Cont c -> |
| 333 |
flash_info ("Waiting one of " ^ String.concat " " (frontier c)); |
| 334 |
cur := c; true |
| 335 |
| `NotFound -> reset (); false |
| 336 |
end else false |
| 337 |
end))); |
| 338 |
ignore(w#event#connect#button_press ~callback:(fun t -> reset (); false)) |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
let get_documentation () = |
| 343 |
"Chars, words, lines and sentences below pertain to standard unicode segmentation rules\n" ^ |
| 344 |
print_keypaths pg |