/
key-combo.el
527 lines (495 loc) · 16.4 KB
/
key-combo.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
;;; key-combo.el --- map key sequence to commands
;;-------------------------------------------------------------------
;;
;; Copyright (C) 2011 Yuuki Arisawa
;;
;; This file is NOT part of Emacs.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;;
;;-------------------------------------------------------------------
;; Author: Yuuki Arisawa <yuuki.ari@gmail.com>
;; URL:https://github.com/uk-ar/key-combo
;; Created: 30 November 2011
;; Version: 0.4
;; Keywords: keyboard input
;;; Commentary:
;; ######## Compatibility ########################################
;;
;; Works with Emacs-23.2.1, 23.1.1
;; ######## Quick start ########################################
;;
;; Add to your ~/.emacs
;;
;; (require 'key-combo)
;;
;; and some chords, for example
;;
;; (key-combo-define-global (kbd "=") '(" = " " == " " === " ))
;; (key-combo-define-global (kbd "=>") " => ")
;;
;;
;; or load default settings
;;
;; (key-combo-load-default)
;;; History:
;; Revision 0.4
;; * Map key to minor mode to toggle enable and disable.
;;
;; Revision 0.3
;; * Not to cleanup when 1 sequence key
;; * Bugfix by tomykaira
;; * Refactoring
;; * Add test cases
;;
;; Revision 0.2
;; * First release
;;
;; Revision 0.1
;; * Initial revision
;; Code goes here
(require 'cl)
(defvar key-combo-loop-option 'only-same-key;'allways 'only-same-key 'never
"Loop mode setting.
\n'allways:do loop both same key sequence and not same key sequence.
\n'only-same-key:do loop only same key sequence.
\n'never:don't loop.")
(defun key-combo-lookup-key1 (keymap key)
;; copy from key-chord-lookup-key
"Like lookup-key but no third arg and no numeric return value."
(let ((res (lookup-key keymap key)))
(if (numberp res)
nil
;; else
res)))
(defun key-combo-describe ()
"List key combo bindings in a help buffer."
(interactive)
(describe-bindings [key-combo]))
(defun key-combo-lookup-key (key)
;; copy from key-chord-lookup-key
"Lookup KEY in all current key maps."
(let ((maps (current-minor-mode-maps))
res)
(while (and maps (not res))
(setq res (key-combo-lookup-key1 (car maps) key)
maps (cdr maps)))
(or res
(if (current-local-map)
(key-combo-lookup-key1 (current-local-map) key))
(key-combo-lookup-key1 (current-global-map) key))))
(defun key-combo-lookup (events)
(let ((key
(intern
(if (characterp events)
(char-to-string events)
(mapconcat 'char-to-string events "")))))
(key-combo-lookup-key (vector 'key-combo key))))
(defun key-combo-undo(command)
(cond
((not (cdr-safe command)) nil);;no clean up
((commandp (cdr-safe command))
(command-execute (cdr command)))
((functionp (cdr-safe command))
(funcall (cdr command)))
))
(defun key-combo-command-execute(command)
(cond
((not (listp command))
(command-execute command))
((commandp (car command))
(command-execute (car command)))
((functionp (car command))
(funcall (car command))))
)
;;bug (C-/
(defun key-combo()
(interactive)
(insert last-input-event)
(undo-boundary)
;;for undo
(let* ((same-key last-input-event)
(all-command-keys (list last-input-event))
(command (key-combo-lookup all-command-keys))
(old-command (key-combo-get-command
(char-to-string last-input-event))))
(catch 'invalid-event
(while command
(key-combo-undo old-command)
(key-combo-command-execute command)
(undo-boundary);;for undo
(if (not (characterp (read-event))) (throw 'invalid-event t))
(setq same-key
(cond ((eq key-combo-loop-option 'allways) t)
((eq key-combo-loop-option 'only-same-key)
(if (eq last-input-event same-key) same-key nil))
((eq key-combo-loop-option 'never) nil))
old-command command)
(setq all-command-keys (append all-command-keys
(list last-input-event)))
(setq command (key-combo-lookup all-command-keys))
(if (and (not command) same-key);;for loop
(progn
(if (eq 2 (length all-command-keys)) (throw 'invalid-event t))
(setq all-command-keys (char-to-string last-input-event))
(setq command (key-combo-lookup all-command-keys))))
);;end while
);;end catch
(setq unread-command-events
(cons last-input-event unread-command-events))
);;end let
);;end key-combo
(defun key-combo-get-command(command)
(cond
((functionp command) command)
((listp command) command)
((not (stringp command)) nil)
((string-match "`!!'" command)
(destructuring-bind (pre post) (split-string command "`!!'")
(lexical-let ((pre pre) (post post))
(cons
(lambda()
(insert pre)
(save-excursion (insert post)))
(lambda()
(delete-backward-char (length pre))
(delete-backward-char (- (length post)))))
)))
(t
(lexical-let ((command command))
(cons
(lambda()
(insert command))
(lambda()
(delete-backward-char (length command))))))
);;end cond
)
(defun key-combo-define (keymap keys commands)
"Define in KEYMAP, a key-combo of two keys in KEYS starting a COMMAND.
\nKEYS can be a string or a vector of two elements. Currently only elements
that corresponds to ascii codes in the range 32 to 126 can be used.
\nCOMMAND can be an interactive function, a string, or nil.
If COMMAND is nil, the key-combo is removed."
;;copy from key-chord-define
(cond
((eq commands nil) nil)
;;for sequence '(" = " " == ")
((and (listp commands) (listp (cdr-safe commands)))
(let ((base-key keys)
(seq-keys keys))
(mapc '(lambda(command)
(key-combo-define1 keymap seq-keys command)
(setq seq-keys (concat seq-keys base-key)))
commands)))
(t
(key-combo-define1 keymap keys commands))
))
(defun key-combo-define1 (keymap keys command)
;;copy from key-chord-define
(let* ((key1 (substring keys 0 1))
(command1 (key-combo-lookup-key key1)))
(cond
((not (eq command1 'key-combo))
(define-key keymap key1 'key-combo)
(define-key keymap
(vector 'key-combo (intern key1)) 'key-combo)
(define-key keymap (vector 'key-combo (intern keys))
(key-combo-get-command command)))
(t
(define-key keymap (vector 'key-combo (intern keys))
(key-combo-get-command command)))
)))
(defvar key-combo-mode-map (make-sparse-keymap))
(define-minor-mode key-combo-mode
"Toggle key combo."
:global t
:lighter " KC"
:init-value t)
(defun key-combo-define-global (keys command)
"Define a key-combo of two keys in KEYS starting a COMMAND.
\nKEYS can be a string or a vector of two elements. Currently only elements
that corresponds to ascii codes in the range 32 to 126 can be used.
\nCOMMAND can be an interactive function, a string, or nil.
If COMMAND is nil, the key-combo is removed."
;;(interactive "sSet key chord globally (2 keys): \nCSet chord \"%s\" to command: ")
(key-combo-define key-combo-mode-map keys command))
(defun key-combo-load-default ()
(key-combo-load-default-1 key-combo-mode-map)
)
;;
(defun key-combo-load-default-1 (map)
(key-combo-define map (kbd "=") '(" = " " == " " === " ))
;; (key-combo-define map (kbd "+") '(" + " "++"))
;; (key-combo-define map (kbd "&") '(" & " "&&"))
;;(key-combo-define map (kbd "-") '(" - " "-"))
(key-combo-define map (kbd "=>") " => ")
(key-combo-define map (kbd ">") '(">"))
(key-combo-define map (kbd ">=") " >= ")
;; (key-combo-define map (kbd "=~") " =~ ")
;; (key-combo-define map (kbd "(=") "(=`!!')")
;; (key-combo-define map (kbd "<<") " << ")
)
;;ok
;;(key-combo-define-global (kbd "=") '(" = " " == " "="))
;;(key-combo-define-global (kbd "(=") "(=`!!')")
;;ok
;;(key-combo-define-global (kbd "(") 'skeleton-pair-insert-maybe)
;;ok
;;(key-combo-define-global (kbd "=>") " => ")
;;(key-combo-define-global (kbd "=") '(" = " " == " "="))
;;ok
;;(key-combo-define-global (kbd "=") " = ")
;;ok
;;(global-set-key (kbd "=") 'key-combo)
;;ok
;;(key-combo-define-global (kbd "=") '(" = " " == " "="))
;;(key-combo-define-global (kbd "=>") " => ")
;;ng
;;(global-set-key (kbd "==") 'key-combo) => ng
;;ok
;;(key-combo-define-global (kbd "=>") " => ")
;;(key-combo-define-global (kbd "=") '(" = " " == " "="))
;;(key-combo-define-global (kbd ">") '(" > " " >> " ))
;;(key-combo-define-global (kbd "->") " -> ")
;;ok
;;(key-combo-define-global (kbd "=") " = ")
;;(key-combo-define-global (kbd "==") " == ")
;;(key-combo-define-global (kbd "===") "=")
;;skeleton(())後ろがかっこの場合囲む
;;clean up
;;(key-combo-define-global (kbd "=") '(nil nil nil))
;;(key-combo-define-global (kbd "-") '(nil nil))
;;(global-set-key(kbd "-") 'self-insert-command)
(defun test()
(if (y-or-n-p "?")
(split-window-horizontally 20)
(split-window-vertically 10))
1)
(dont-compile
(when(fboundp 'expectations)
(expectations
(desc "key-combo")
(expect ">>"
(with-temp-buffer
(setq unread-command-events (listify-key-sequence ">>\C-a"))
(read-event)
(call-interactively 'key-combo)
(call-interactively 'key-combo)
;;(insert (char-to-string(car unread-command-events)))
(buffer-string)
))
(expect " = "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "=\C-a"))
(read-key)
(call-interactively 'key-combo)
(buffer-string)
))
(expect " = *"
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "=*\C-a"))
(read-event)
(call-interactively 'key-combo)
(insert (char-to-string(car unread-command-events)))
(buffer-string)
))
(expect " == "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "==\C-a"))
(read-event)
(call-interactively 'key-combo)
(buffer-string)
))
(expect " => "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "=>\C-a"))
(read-event)
(call-interactively 'key-combo)
(buffer-string)
))
(expect " === "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "===\C-a"))
(read-event)
(call-interactively 'key-combo)
(buffer-string)
))
;;(desc "key-combo-undo")
;; (expect ""
;; (with-temp-buffer
;; (buffer-enable-undo)
;; (key-combo-undo '((lambda() (insert "a")) . nil))
;; (buffer-string)
;; ))
;; (expect "a"
;; (with-temp-buffer
;; (buffer-enable-undo)
;; (key-combo-undo '((lambda() (insert "a")) . (lambda() (insert "a"))))
;; (buffer-string)
;; ))
;; (desc "key-combo-command-execute")
;; (expect "a"
;; (with-temp-buffer
;; (buffer-enable-undo)
;; (key-combo-undo '((lambda() (insert "a")) . (lambda() (insert "a"))))
;; (buffer-string)
;; ))
(desc "key-combo-get-command")
(expect "a"
(with-temp-buffer
(funcall (car (key-combo-get-command "a")))
(buffer-string)
))
(expect ""
(with-temp-buffer
(funcall (car (key-combo-get-command "a")))
(funcall (cdr (key-combo-get-command "a")))
(buffer-string)
))
(expect t
(with-temp-buffer
(funcall (car (key-combo-get-command "a`!!'a")))
(buffer-string)
(and (equal (buffer-string) "aa") (eq (point) 2))
))
(expect ""
(with-temp-buffer
(funcall (car (key-combo-get-command "a`!!'a")))
(funcall (cdr (key-combo-get-command "a`!!'a")))
(buffer-string)
))
(desc "key-combo-define")
(expect (mock (define-key * * *) :times 3);;=> nil
(stub key-combo-lookup-key => nil)
(key-combo-define (current-global-map) "a" "a")
)
(expect (mock (define-key * * *) :times 1);;=> nil
;;(not-called define-key)
(stub key-combo-lookup-key =>'key-combo)
(key-combo-define (current-global-map) "a" "a")
)
(expect (mock (define-key * * *) :times 2);;(not-called define-key)
;;(mock (define-key * * *) :times 0);;=> nil
(stub key-combo-lookup-key =>'key-combo)
(key-combo-define (current-global-map) "a" '("a" "bb"))
)
(desc "undo")
(expect "="
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "=\C-a"))
(read-event)
(buffer-enable-undo)
(call-interactively 'key-combo)
(undo)
(buffer-string)
))
(expect " = "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "==\C-a"))
(read-event)
(buffer-enable-undo)
(call-interactively 'key-combo)
(undo)
(buffer-string)
))
(desc "loop")
(expect " = "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "====\C-a"))
(read-event)
(call-interactively 'key-combo)
(buffer-string)
))
(expect " => = "
(with-temp-buffer
(setq unread-command-events (listify-key-sequence "=>=\C-a"))
(read-event)
(call-interactively 'key-combo)
(read-event)
(call-interactively 'key-combo)
(buffer-string)
))
(desc "key-combo-lookup-key")
(expect " = "
(with-temp-buffer
(funcall
(car (key-combo-lookup-key (vector 'key-combo (intern "=")))))
(buffer-string)
))
(expect " == "
(with-temp-buffer
(funcall
(car (key-combo-lookup-key (vector 'key-combo (intern "==")))))
(buffer-string)))
(expect " => "
(with-temp-buffer
(funcall
(car (key-combo-lookup-key (vector 'key-combo (intern "=>")))))
(buffer-string)))
(expect " === "
(with-temp-buffer
(funcall
(car (key-combo-lookup-key (vector 'key-combo (intern "===")))))
(buffer-string)))
(expect nil
(key-combo-lookup-key (vector 'key-combo (intern "===="))))
(expect nil
(key-combo-lookup-key (vector 'key-combo (intern "====="))))
(desc "key-combo-lookup")
(expect " = "
(with-temp-buffer
(funcall
(car (key-combo-lookup [?=])))
(buffer-string)))
(expect " == "
(with-temp-buffer
(funcall
(car (key-combo-lookup [?= ?=])))
(buffer-string)))
(expect " => "
(with-temp-buffer
(funcall
(car (key-combo-lookup [?= ?>])))
(buffer-string)))
(expect " === "
(with-temp-buffer
(funcall
(car (key-combo-lookup [?= ?= ?=])))
(buffer-string)))
(expect nil
(key-combo-lookup [?= ?= ?= ?=]))
;; (desc "vertically")
;; (expect (mock (split-window-vertically 10))
;; (stub y-or-n-p => nil)
;; (test))
;; (desc "horizontally")
;; (expect (mock (split-window-horizontally *))
;; (stub y-or-n-p => t)
;; (test))
;; (desc "return")
;; (expect 1
;; (stub y-or-n-p)
;; (stub split-window-horizontally)
;; (stub split-window-vertically)
;; (test))
)))
;;todo filter
;; filter for mode
;; filter for inside string ""
;; filter for inside comment ;;
;; support lamda func
(provide 'key-combo)
;;; key-combo.el ends here