-
Notifications
You must be signed in to change notification settings - Fork 35
/
ergoemacs-command-loop.el
2377 lines (2087 loc) · 102 KB
/
ergoemacs-command-loop.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ergoemacs-command-loop.el --- Keyboard translation functions -*- lexical-binding: t -*-
;; Copyright © 2013-2023 Free Software Foundation, Inc.
;; Filename: ergoemacs-command-loop.el
;; Description:
;; Author: Matthew L. Fidler
;; Maintainer: Matthew L. Fidler
;; Created: Sat Sep 28 20:08:09 2013 (-0500)
;;
;;; Commentary:
;; This is the functions for the `ergoemacs-mode' command loop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 3, 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, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'cl-lib)
(eval-when-compile
(require 'ergoemacs-macros))
(declare-function ergoemacs-translate--emacs-shift "ergoemacs-translate")
(declare-function ergoemacs-warn "ergoemacs-lib")
(declare-function guide-key/close-guide-buffer "guide-key")
(declare-function guide-key/popup-function "guide-key")
(declare-function ergoemacs-key-description "ergoemacs-key-description")
(declare-function ergoemacs-key-description--unicode-char "ergoemacs-key-description")
(declare-function ergoemacs-mode-line "ergoemacs-mode")
(declare-function ergoemacs-layout--regexp "ergoemacs-layouts")
(declare-function ergoemacs-layouts--list "ergoemacs-layouts")
(declare-function ergoemacs-map-properties--movement-p "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--put "ergoemacs-map-properties")
(declare-function ergoemacs-translate--define-key "ergoemacs-translate")
(declare-function ergoemacs-translate--escape-to-meta "ergoemacs-translate")
(declare-function ergoemacs-translate--event-basic-type "ergoemacs-translate")
(declare-function ergoemacs-translate--event-convert-list "ergoemacs-translate")
(declare-function ergoemacs-translate--event-modifiers "ergoemacs-translate")
(declare-function ergoemacs-translate--event-mods "ergoemacs-translate")
(declare-function ergoemacs-translate--get "ergoemacs-translate")
(declare-function ergoemacs-translate--keymap "ergoemacs-translate")
(declare-function ergoemacs-translate--meta-to-escape "ergoemacs-translate")
(declare-function ergoemacs-translate--trials "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-key "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-keymap "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-keymap-modal "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-modal-always "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-modal-color "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-p "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-text "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-translation "ergoemacs-translate")
(declare-function ergoemacs-translation-struct-unchorded "ergoemacs-translate")
(declare-function ergoemacs-key-description--modifier "ergoemacs-key-description")
(declare-function ergoemacs-posnp "ergoemacs-command-loop")
(if (fboundp 'posnp)
(defalias 'ergoemacs-posnp #'posnp)
;; For emacs 24.1
(defun ergoemacs-posnp (obj)
"Return non-nil if OBJ appears to be a valid `posn' object."
(and (windowp (car-safe obj))
(atom (car-safe (setq obj (cdr obj)))) ;AREA-OR-POS.
(integerp (car-safe (car-safe (setq obj (cdr obj))))) ;XOFFSET.
(integerp (car-safe (cdr obj))))))
(defvar ergoemacs-command-loop-echo-keystrokes)
(defvar ergoemacs-default-cursor-color)
(defvar ergoemacs-echo-function)
(defvar ergoemacs-map--quit-map)
(defvar ergoemacs-modal-emacs-state-modes)
(defvar ergoemacs-modal-ignored-buffers)
(defvar ergoemacs-modal-ignored-keymap)
(defvar ergoemacs-mode-started-p)
(defvar guide-key/guide-key-sequence)
(defvar keyfreq-mode)
(defvar keyfreq-table)
(defvar ergoemacs-translate--emacs-shift)
(defvar ergoemacs-command-loop-start)
(defvar universal-argument-num-events) ;; Not in Emacs 24.5
(defvar ergoemacs-command-loop--mark-active nil
"Determines if mark was active before ergoemacs command loop.")
(define-obsolete-variable-alias 'ergoemacs-universal-fns 'ergoemacs-command-loop--universal-functions "Ergoemacs-v5.16")
(defvar ergoemacs-command-loop--universal-functions '(universal-argument ergoemacs-universal-argument ergoemacs-command-loop--universal-argument)
"List of `ergoemacs-mode' recognized functions.")
(defvar ergoemacs-command-loop--next-key-hash
(let ((hash (make-hash-table)))
(puthash 'event-apply-shift-modifier (list '(shift) :force) hash)
(puthash 'event-apply-alt-modifier (list '(alt) :force) hash)
(puthash 'event-apply-control-modifier (list '(control) :force) hash)
(puthash 'event-apply-hyper-modifier (list '(hyper) :force) hash)
(puthash 'event-apply-meta-modifier (list '(meta) :force) hash)
(puthash 'event-apply-super-modifier (list '(super) :force) hash)
hash)
"Hash table of how functions force the unchorded next key translation to behave.")
(defvar ergoemacs-command-loop--undo-functions
'(ergoemacs-read-key-undo-last
ergoemacs-command-loop--undo-last ergoemacs-read-key-force-undo-last ergoemacs-command-loop--force-undo-last)
"Undo functions recognized by `ergoemacs-mode'.")
(defvar ergoemacs-command-loop--help-last-key nil)
(define-obsolete-variable-alias 'ergoemacs-read-key-delay 'ergoemacs-command-loop--decode-event-delay "Ergoemacs-v5.16")
(defvar ergoemacs-command-loop--decode-event-delay 0.01
"Timeout for `ergoemacs-command-loop--decode-event'.
This is to distinguish events in a terminal, like xterm.
It needs to be less than `ergoemacs-command-loop-blink-rate'.")
(defvar ergoemacs-command-loop--history nil
"History of command loop locations.")
(defvar ergoemacs-command-loop--last-event-time nil
"Stores the last event time.")
(defvar ergoemacs-command-loop--first-type nil
"This is the first type for the `ergoemacs-mode' command loop.")
(defvar ergoemacs-command-loop--current-type nil
"This is the current translation type for the `ergoemacs-mode' command loop.")
(defvar ergoemacs-command-loop--universal nil
"This determines if `ergoemacs-mode' will start editing/completing universal arguments.")
(defvar ergoemacs-command-loop--self-insert-command-count 0
"The count of `self-insert-command' to allow inserting correct undo boundaries.")
;; (defvar ergoemacs-command-loop--mouse-event nil)
(defvar ergoemacs-command-loop--exit nil
"External variable controlling if the `ergoemacs-command-loop' will exit.
:ignore-post-command-hook means that the command will exit, and
ignore the post-command hooks.")
(defvar ergoemacs-command-loop--execute-modify-command-list
'(last-repeatable-command
this-command
this-original-command
mc--this-command)
"Commands that will be set to what `ergoemacs-command-loop' executes.")
(defun ergoemacs-command-loop--execute-modify-command-list (command)
"Set variables in `ergoemacs-command-loop--execute-modify-command-list' to COMMAND."
(with-silent-modifications
(dolist (var ergoemacs-command-loop--execute-modify-command-list)
(set var command))))
(defvar ergoemacs-command-loop--single-command-keys nil
"If defined, a vector of the command keys pressed in the `ergoemacs-command-loop'.")
(defvar ergoemacs-command-loop--echo-keystrokes-complete nil
"Echoed keystrokes, keep echoing active.")
(defvar ergoemacs-command-loop--modal-stack '()
"The Modal Stack.")
(defcustom ergoemacs-command-loop-swap-translation
'(((:normal :normal) :unchorded-ctl)
((:normal :unchorded-ctl) :ctl-to-alt)
((:normal :unchorded-ctl) :normal)
((:ctl-to-alt :ctl-to-alt) :unchorded-ctl)
((:ctl-to-alt :unchorded-ctl) :ctl-to-alt)
((:unchorded-ctl :unchorded-ctl) :ctl-to-alt)
((:unchorded-ctl :ctl-to-alt) :unchorded-ctl))
"How the translation will be swapped."
:type '(repeat
(list
(list
(sexp :tag "First Type")
(sexp :tag "Current Type"))
(sexp :tag "Translated Type")))
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-command-loop-blink-character "•"
"Blink character."
:type '(choice
(string :tag "Cursor")
(const :tag "No cursor" nil))
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-command-loop-blink-rate 0.4
"Rate that the ergoemacs-command loop cursor blinks."
:type 'number
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-command-loop-type nil
"Type of `ergoemacs-mode' command loop."
:type '(choice
(const :tag "Replace emacs command loop (full)" :full)
;; (const :tag "Test mode; Don't actually run command " :test)
(const :tag "Command Loop when called directly" nil)
(const :tag "Emacs" :emacs))
:group 'ergoemacs-comamnd-loop)
(defcustom ergoemacs-command-loop-hide-shift-translations t
"Hide shift translations in the command loop help."
:type 'boolean
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-command-loop-echo-keystrokes 1
"The amount of time before `ergoemacs-mode' displays keystrokes."
:type 'number
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-command-loop-timeout 2
"The number of seconds before hook has froze."
:type 'number
:group 'ergoemacs-command-loop)
(defcustom ergoemacs-echo-function :on-translation
"Shows the function evaluated with a key."
:type '(choice
(const :tag "Always echo" t)
(const :tag "For multi-key commands" :multi-key)
(const :tag "Echo on translations" :on-translation)
(const :tag "Don't Echo" nil))
:group 'ergoemacs-command-loop)
(defvar ergoemacs-command-loop-time-before-blink)
(defvar ergoemacs-command-loop-hide-shift-translations)
(defvar ergoemacs-mode)
(defvar ergoemacs-command-loop-type)
(defvar ergoemacs-keymap)
(defun ergoemacs-command-loop--modal-show ()
"Show modal translation.
Returns the mode-line text."
(let (tmp color text)
(with-silent-modifications
(cond
((setq tmp (ergoemacs :modal-p))
(setq color (ergoemacs-translation-struct-modal-color tmp))
(if color
(set-cursor-color color)
(when ergoemacs-default-cursor-color
(set-cursor-color ergoemacs-default-cursor-color)))
(setq text (ergoemacs-translation-struct-text tmp))
(when (functionp text)
(setq text (funcall text)))
(if text
(ergoemacs-mode-line ;; Indicate Alt+ in mode-line
text)
(ergoemacs-mode-line))
(or text "Unnamed"))
(t
(when ergoemacs-default-cursor-color
(set-cursor-color ergoemacs-default-cursor-color))
(ergoemacs-mode-line)
nil)))))
(defun ergoemacs-command-loop--match-buffer-name-p (reg)
"Determine if the REG is found in `buffer-name'."
(and (stringp (buffer-name))
(string-match reg (buffer-name))))
(defun ergoemacs-command-loop--modal-p ()
"Determine if the command should be modal.
If so return the translation."
(if (not ergoemacs-command-loop--modal-stack) nil
(let* ((translation (nth 0 ergoemacs-command-loop--modal-stack))
(always)
ret)
(when (ergoemacs-translation-struct-p translation)
(setq always (ergoemacs-translation-struct-modal-always translation))
(cond
((and (minibufferp)
(not always)))
((and (not always)
(memq major-mode ergoemacs-modal-emacs-state-modes)))
((and (not always)
(catch 'match-modal
(dolist (reg ergoemacs-modal-ignored-buffers)
(when (ergoemacs-command-loop--match-buffer-name-p reg)
(throw 'match-modal t)))
nil)))
(t
(setq ret translation))))
ret)))
(defun ergoemacs-command-loop--modal-pop ()
"Turn off the last ergoemacs modal in the modal-stack."
(when ergoemacs-command-loop--modal-stack
(ergoemacs-command-loop--modal (ergoemacs-translation-struct-key (nth 0 ergoemacs-command-loop--modal-stack)))))
(defun ergoemacs-command-loop--modal (type)
"Toggle ergoemacs command modes.
The TYPE is the type of command translation/modal keymaps that are installed."
(cond
((or (not ergoemacs-command-loop--modal-stack) ;; First time to turn on
(not (eq (ergoemacs-translation-struct-key (nth 0 ergoemacs-command-loop--modal-stack)) type)) ;; New modal
)
(push (ergoemacs-translate--get type) ergoemacs-command-loop--modal-stack)
(unless ergoemacs-default-cursor-color
(setq ergoemacs-default-cursor-color
(or (frame-parameter nil 'cursor-color) "black")))
(ergoemacs-command-loop--message "%s command mode installed" (ergoemacs-command-loop--modal-show)))
(t ;; Turn off.
(setq ergoemacs-command-loop--modal-stack (cdr ergoemacs-command-loop--modal-stack))
(if (ergoemacs :modal-p)
(ergoemacs-command-loop--message "%s command mode resumed." (ergoemacs-command-loop--modal-show))
(ergoemacs-command-loop--modal-show)
(ergoemacs-command-loop--message "Resume regular ergoemacs-mode")))))
(defun ergoemacs-command-loop--redefine-quit-key (&optional key)
"Redefines the quit-key in Emacs to KEY or Ctrl+g.
Typically the Emacs quit key is Ctrl+g, but it can be redefined
with this function."
(let ((cur-input (current-input-mode))
(new-key (listify-key-sequence (or key [7]))))
(when (> 1 (length new-key))
(error "Will not set a key sequence to the Emacs key sequence"))
(setf (nth 3 cur-input) new-key)
(and (ignore-errors (apply #'set-input-mode cur-input))
(message "Redefined Emacs quit key to %s"
(ergoemacs-key-description (or key [7])))
t)))
(defun ergoemacs-command-loop--setup-quit-key ()
"Setup the `ergoemacs-mode' quit key."
(let (quit-keys vect-key)
(dolist (key (reverse (append (where-is-internal 'keyboard-quit)
(where-is-internal 'ergoemacs-keyboard-quit))))
(setq vect-key (vconcat key))
(unless (or (symbolp (aref vect-key 0))
(not (= 1 (length vect-key)))
(member key quit-keys))
(push vect-key quit-keys)))
(when quit-keys
(catch 'found-quit
(dolist (key quit-keys)
(when (ergoemacs-command-loop--redefine-quit-key key)
(throw 'found-quit t)))
nil))))
(add-hook 'ergoemacs-mode-startup-hook #'ergoemacs-command-loop--setup-quit-key)
(add-hook 'ergoemacs-mode-shutdown-hook #'ergoemacs-command-loop--redefine-quit-key)
(defun ergoemacs-command-loop--universal-argument (&rest _ignore)
"`ergoemacs-mode' universal argument.
This is called through `ergoemacs-command-loop'"
(interactive)
(cond
((not (ergoemacs-command-loop-p))
;; Command loop hasn't started.
(setq current-prefix-arg '(4))
(setq ergoemacs-command-loop-start t)
(ergoemacs-command-loop nil (ergoemacs-command-loop--modal-p) nil t))
((not current-prefix-arg)
(setq current-prefix-arg '(4)
ergoemacs-command-loop--universal t
ergoemacs-command-loop--exit :ignore-post-command-hook))
((listp current-prefix-arg)
;; Change current prefix argument
(setq current-prefix-arg (list (* (nth 0 current-prefix-arg) 4))
ergoemacs-command-loop--universal t
ergoemacs-command-loop--exit :ignore-post-command-hook))
(t
(setq ergoemacs-command-loop--universal t
ergoemacs-command-loop--exit :ignore-post-command-hook))))
(defalias 'ergoemacs-read-key--universal-argument #'ergoemacs-command-loop--universal-argument)
(defalias 'ergoemacs-universal-argument #'ergoemacs-command-loop--universal-argument)
(defun ergoemacs-command-loop--digit-argument (&optional type)
"Ergoemacs digit argument.
This is called through `ergoemacs-command-loop'.
TYPE is the keyboard translation type, defined by `ergoemacs-translate'.
Ergoemacs-mode sets up: :ctl-to-alt :unchorded :normal."
(interactive)
(let* ((char (if (integerp last-command-event)
last-command-event
(get last-command-event 'ascii-character)))
(digit (- (logand char ?\177) ?0)))
(setq current-prefix-arg digit))
(ergoemacs-command-loop nil type nil t))
(defalias 'ergoemacs-digit-argument #'ergoemacs-command-loop--digit-argument)
(defun ergoemacs-command-loop--negative-argument (&optional type)
"Ergoemacs negative argument.
This is called through `ergoemacs-command-loop'.
TYPE is the keyboard translation type, defined by `ergoemacs-translate'
Ergoemacs-mode sets up: :ctl-to-alt :unchorded :normal."
(setq current-prefix-arg '-)
(ergoemacs-command-loop nil type nil t))
(defalias 'ergoemacs-negative-argument #'ergoemacs-command-loop--negative-argument)
(dolist (arg '((next-key-is-alt (meta))
(next-key-is-meta (meta))
(next-key-is-ctl (control))
(next-key-is-control (control))
(next-key-is-alt-ctl (control meta))
(next-key-is-ctl-alt (control meta))
(next-key-is-control-meta (control meta))
(next-key-is-meta-control (control meta))
(next-key-is-quoted nil)))
(eval (macroexpand-all ;FIXME: Why macroexpand-all?
`(progn
(defun ,(intern (concat "ergoemacs-command-loop--" (symbol-name (nth 0 arg)))) ()
,(format "Ergoemacs function to allow %s to be the Emacs modifiers" (nth 1 arg))
(interactive)
(message "Dummy Function for %s" (ergoemacs :modifier-desc ,(nth 1 arg))))
(defalias ',(intern (concat "ergoemacs-read-key-" (symbol-name (nth 0 arg)))) ',(intern (concat "ergoemacs-command-loop--" (symbol-name (nth 0 arg)))))
(puthash ',(intern (concat "ergoemacs-command-loop--" (symbol-name (nth 0 arg)))) '(,(nth 1 arg) nil) ergoemacs-command-loop--next-key-hash)
(puthash ',(intern (concat "ergoemacs-read-key-" (symbol-name (nth 0 arg)))) '(,(nth 1 arg) nil) ergoemacs-command-loop--next-key-hash)
(defun ,(intern (concat "ergoemacs-command-loop--force-" (symbol-name (nth 0 arg)))) ()
,(format "Ergoemacs function to allow %s to be the Emacs modifiers" (nth 1 arg))
(interactive)
(message "Dummy Function for %s" (ergoemacs :modifier-desc ,(nth 1 arg))))
(defalias ',(intern (concat "ergoemacs-read-key-force-" (symbol-name (nth 0 arg)))) ',(intern (concat "ergoemacs-command-loop--force-" (symbol-name (nth 0 arg)))))
(puthash ',(intern (concat "ergoemacs-command-loop--force-" (symbol-name (nth 0 arg)))) '(,(nth 1 arg) :force) ergoemacs-command-loop--next-key-hash)
(puthash ',(intern (concat "ergoemacs-read-key-force-" (symbol-name (nth 0 arg)))) '(,(nth 1 arg) :force) ergoemacs-command-loop--next-key-hash)
t))
t))
(defvar ergoemacs-last-command-event nil
"`ergoemacs-mode' command loop last read command.")
(defun ergoemacs-command-loop--undo-last ()
"Function to undo the last key-press.
Uses the `ergoemacs-command-loop--history' variable/function."
(interactive)
(if ergoemacs-command-loop--history
(let ((tmp (pop ergoemacs-command-loop--history)))
(setq ergoemacs-command-loop--single-command-keys (nth 0 tmp)
ergoemacs-command-loop--current-type (nth 1 tmp)
ergoemacs-command-loop--universal (nth 2 tmp)
current-prefix-arg (nth 3 tmp)
last-command-event (nth 4 tmp)
last-input-event last-command-event
ergoemacs-last-command-event last-command-event))
;; Nothing to undo, exit the command loop.
(setq ergoemacs-command-loop--exit t)))
(defalias 'ergoemacs-read-key-undo-last #'ergoemacs-command-loop--undo-last)
(defun ergoemacs-command-loop--force-undo-last ()
"Function to undo the last key-press.
Unlike `ergoemacs-command-loop--undo-last', this ignores any bindings like \\[backward-kill-sentence]
This is actually a dummy function. The actual work is done in `ergoemacs-command-loop'"
(interactive)
(call-interactively #'ergoemacs-command-loop--undo-last))
(put 'ergoemacs-command-loop--force-undo-last :ergoemacs-local :force)
(defalias 'ergoemacs-read-key-force-undo-last #'ergoemacs-command-loop--force-undo-last)
(put 'ergoemacs-read-key-force-undo-last :ergoemacs-local :force)
(defun ergoemacs-command-loop--swap-translation ()
"Function to swap translations.
Uses the `ergoemacs-command-loop-swap-translation' variable."
(interactive)
(let ((next-swap (assoc (list ergoemacs-command-loop--first-type ergoemacs-command-loop--current-type) ergoemacs-command-loop-swap-translation)))
(setq ergoemacs-command-loop--current-type
(if next-swap
(nth 1 next-swap)
:normal))))
(defalias 'ergoemacs-read-key-swap #'ergoemacs-command-loop--swap-translation)
(defun ergoemacs-command-loop--help ()
"Show help for the current sequence KEY."
(interactive)
;; Eventually...
(if (not ergoemacs-command-loop--single-command-keys) nil
(cond
;; TEST:
;; ((and (boundp 'icicle-mode) icicle-mode)
;; (let ((key (vconcat ergoemacs-command-loop--single-command-keys [ergoemacs-ignore])))
;; (ergoemacs-read-key-call 'icicle-complete-keys nil key)))
;; FIXME:
((and (boundp 'guide-key-mode) guide-key-mode)
(let ((key ergoemacs-command-loop--single-command-keys))
(cond
((equal ergoemacs-command-loop--help-last-key ergoemacs-command-loop--single-command-keys)
(setq ergoemacs-command-loop--help-last-key nil
guide-key/guide-key-sequence (delete (key-description ergoemacs-command-loop--single-command-keys) guide-key/guide-key-sequence))
(guide-key/close-guide-buffer))
(t
;; Not using pushnew because the test is equal and
;; guide-key/guide-key-sequence is a global variable.
(setq ergoemacs-command-loop--help-last-key ergoemacs-command-loop--single-command-keys)
(unless (member (key-description ergoemacs-command-loop--single-command-keys) guide-key/guide-key-sequence)
(push (key-description ergoemacs-command-loop--single-command-keys) guide-key/guide-key-sequence))
(guide-key/popup-function key)))))
(t (let ((cb (current-buffer))
(key ergoemacs-command-loop--single-command-keys))
(save-excursion
(with-help-window (help-buffer)
(set-buffer (help-buffer))
(describe-buffer-bindings cb key)))
(setq ergoemacs-command-loop--exit t))))))
(defalias 'ergoemacs-read-key-help #'ergoemacs-command-loop--help)
;; Command Loop
;; (1) Read Key sequence
(defvar ergoemacs-command-loop--read-key-prompt ""
"Extra prompt for `ergoemacs-command-loop--read-key'.")
(defun ergoemacs-command-loop--read-key-help-text-prefix-argument (&optional blink-on universal)
"Display prefix argument portion of the `ergoemacs-mode' help text.
BLINK-ON
UNIVERSAL"
(or (and (not current-prefix-arg)
(concat (or
(and (not universal) "")
(or (and (string= ergoemacs-command-loop--read-key-prompt "") "") " ")
(and ergoemacs-command-loop-blink-character
(or (and blink-on ergoemacs-command-loop-blink-character)
" "))
" ")
(or
(and (not universal) "")
"▸")))
(format
"%s%s%s %s "
(cond
((listp current-prefix-arg)
(make-string (round (log (nth 0 current-prefix-arg) 4)) ?u))
(t current-prefix-arg))
(or (and (not universal) "")
(and ergoemacs-command-loop-blink-character
(or (and blink-on ergoemacs-command-loop-blink-character)
" "))
" ")
(or (and (listp current-prefix-arg)
(format "%s" current-prefix-arg))
"")
"▸")))
(defun ergoemacs-command-loop--ensure-sane-variables ()
"Make sure that certain variables won't lock up Emacs.
Currently this ensures:
`ergoemacs-command-loop--decode-event-delay' is less than `ergoemacs-command-loop-blink-rate'."
(when (>= ergoemacs-command-loop--decode-event-delay ergoemacs-command-loop-blink-rate)
(ergoemacs-warn "ergoemacs-command-loop--decode-event-delay >= ergoemacs-command-loop-blink-rate; Reset to ergoemacs-command-loop-blink-rate / 1000")
(setq ergoemacs-command-loop--decode-event-delay (/ ergoemacs-command-loop-blink-rate 1000))))
(add-hook 'ergoemacs-mode-startup-hook #'ergoemacs-command-loop--ensure-sane-variables)
(defun ergoemacs-command-loop--combine (current-key next-event)
"Combine CURRENT-KEY and NEXT-EVENT into a vector."
(let (tmp)
(cond
((and (vectorp current-key)
(eventp (setq tmp (aref current-key 0)))
(consp tmp)
(memq (event-basic-type (car tmp))
'(mouse-1 mouse-2 mouse-3 mouse-4 mouse-5 mouse-6 mouse-7 mouse-8 mouse-9)))
(push next-event unread-command-events))
(t (vconcat current-key (vector next-event))))))
(defvar ergoemacs-comand-loop--untranslated-event nil)
(declare-function ergoemacs--real-read-key-sequence "ergoemacs-command-loop")
(fset 'ergoemacs--real-read-key-sequence (symbol-function #'read-key-sequence))
(declare-function ergoemacs--real-describe-key "ergoemacs-command-loop")
(fset 'ergoemacs--real-describe-key (symbol-function #'describe-key))
(defun ergoemacs-command-loop--input-method (event)
"Call `input-method-function' on EVENT.
Ensure that `read-key-sequence' is the original function (not
`ergoemacs-command-loop--read-key-sequence')."
(ergoemacs-no-specials
(ignore-errors (funcall input-method-function event))))
(defun ergoemacs-command-loop--history (&optional prompt seconds current-key)
"Read event and add to event history.
PROMPT is the prompt that will be displayed.
SECONDS is the number of seconds between cursor blink.
CURRENT-KEY is the current key being read. This is used
inconjunction with `input-method-function' to translate keys if
`set-input-method' is using a different keyboard layout.
Also add to `last-command-event' to allow `self-insert-character'
to work appropriately. I'm not sure the purpose of
`last-event-frame', but this is modified as well.
This is not done when the event is [ergoemacs-ignore]"
(or (let ((event (pop unread-command-events))
translate)
(setq ergoemacs-comand-loop--untranslated-event event)
(when (and current-input-method (not current-key)
(not overriding-local-map) (not overriding-terminal-local-map)
(setq translate (ergoemacs-command-loop--input-method event)))
(setq event (pop translate))
(when translate
(setq unread-command-events (append translate unread-command-events))))
(unless (eq event 'ergoemacs-ignore)
(setq last-command-event event
last-input-event last-command-event
ergoemacs-last-command-event last-command-event
last-event-frame (selected-frame)))
event)
(let* ((last-event-time (or (and ergoemacs-command-loop--last-event-time
(- (float-time) ergoemacs-command-loop--last-event-time))
(and (setq ergoemacs-command-loop--last-event-time (float-time)) 0)))
(prompt (cond
((not prompt) nil)
((not (stringp prompt)))
((not (string= "" ergoemacs-command-loop--read-key-prompt)) prompt)
((or (string= prompt " ")
(string-match-p prompt (concat " *" ergoemacs-command-loop-blink-character " *")))
nil)
(ergoemacs-command-loop--universal prompt)
(ergoemacs-command-loop--echo-keystrokes-complete prompt)
((not (numberp ergoemacs-command-loop-echo-keystrokes)) prompt)
((= 0 ergoemacs-command-loop-echo-keystrokes) prompt)
((< last-event-time ergoemacs-command-loop-echo-keystrokes) nil)
;; ((and (not ergoemacs-command-loop--echo-keystrokes-complete)
;; (numberp ergoemacs-command-loop-echo-keystrokes)
;; (or (= 0 ergoemacs-command-loop-echo-keystrokes)
;; (< last-event-time ergoemacs-command-loop-echo-keystrokes))) nil)
;; ((and (< last-event-time ergoemacs-command-loop-time-before-blink) (string= prompt "")) nil)
;; ((and (< last-event-time ergoemacs-command-loop-time-before-blink) ) nil)
(t
(setq ergoemacs-command-loop--echo-keystrokes-complete t)
prompt)))
(echo-keystrokes 0)
;; Run (with-timeout) so that idle timers will work.
(event (cond
(prompt (with-timeout (seconds nil)
(progn
(ergoemacs-command-loop--message prompt)
(ignore-errors (read-event)))))
((and (not ergoemacs-command-loop--echo-keystrokes-complete)
ergoemacs-command-loop--single-command-keys)
(with-timeout (ergoemacs-command-loop-echo-keystrokes nil)
(ignore-errors (read-event))))
(t (ignore-errors (read-event)))))
translate)
(when (eventp event)
(setq ergoemacs-comand-loop--untranslated-event event)
(unless (consp event) ;; Don't record mouse events
(when (and current-input-method (not current-key)
(not overriding-local-map) (not overriding-terminal-local-map)
(setq translate (ergoemacs-command-loop--input-method event)))
(setq event (pop translate))
(when translate
(setq unread-command-events (append translate unread-command-events))))
(push (list ergoemacs-command-loop--single-command-keys
ergoemacs-command-loop--current-type
ergoemacs-command-loop--universal
current-prefix-arg
last-command-event)
ergoemacs-command-loop--history))
(unless (eq event 'ergoemacs-ignore)
(setq ergoemacs-command-loop--last-event-time (float-time)
last-command-event event
last-input-event last-command-event
ergoemacs-last-command-event last-command-event
last-event-frame (selected-frame))))
event)))
(defvar ergoemacs-command-loop--decode-event-timeout-p nil
"Determines if `ergoemacs-command-loop--decode-event' timed out.")
(defun ergoemacs-command-loop--decode-event (event keymap &optional current-key)
"Change EVENT based on KEYMAP.
Used to help with translation keymaps like `input-decode-map'.
CURRENT-KEY is the current key being read. This is used
inconjunction with `input-method-function' to translate keys if
`set-input-method' is using a different keyboard layout."
(let* ((new-event event)
(old-ergoemacs-input unread-command-events)
new-ergoemacs-input
(current-test-key (or (and (listp event)
(vector (ergoemacs-translate--event-convert-list
(append (ergoemacs-translate--event-modifiers event)
(list (ergoemacs-translate--event-basic-type event))))))
(vector event)))
(test-ret (lookup-key keymap current-test-key))
;; (timeout-key (key-binding (vconcat current-test-key [ergoemacs-timeout])))
next-key)
(while (and current-test-key
(ergoemacs-keymapp test-ret))
;; The translation needs more keys...
(setq next-key (ergoemacs-command-loop--history nil ergoemacs-command-loop--decode-event-delay current-key))
(when next-key ;; Since a key was read, save it to be read later.
(push last-command-event new-ergoemacs-input))
(if next-key
(setq current-test-key (ergoemacs :combine current-test-key next-key)
;; timeout-key (key-binding (vconcat current-test-key [ergoemacs-timeout]))
test-ret (lookup-key keymap current-test-key))
(setq current-test-key nil)))
;; Change strings to emacs keys.
(when (stringp test-ret)
;; Should it be read-kbd-macro?
(setq test-ret (vconcat test-ret)))
(when (functionp test-ret)
(when (memq test-ret '(xterm-mouse-translate xterm-mouse-translate-extended))
(message "xterm-mouse-translate: %s->%s" current-test-key (funcall test-ret nil)))
(setq last-input-event event
test-ret (if (or (eq keymap input-decode-map)
(eq keymap key-translation-map)
(eq keymap local-function-key-map))
(funcall test-ret nil) ;; Pretend emacs called this from command loop.
(funcall test-ret)))
(when (not (equal unread-command-events old-ergoemacs-input))
(push (pop unread-command-events) new-ergoemacs-input)))
(if (and (vectorp test-ret)
(= (length test-ret) 1))
(progn
(setq new-event (elt test-ret 0)))
;; Not a new event, restore anything that was popped off the
;; unread command events.
(when old-ergoemacs-input
(setq unread-command-events old-ergoemacs-input))
;; Add anything read to the
;; unread-command-events
(when new-ergoemacs-input
(setq unread-command-events (append new-ergoemacs-input unread-command-events))))
new-event))
(defun ergoemacs-command-loop--read-event (prompt &optional current-key)
"Read a single event.
PROMPT is the prompt used when reading an event.
CURRENT-KEY is the current key sequence that has alerady been
read.
This respects `input-decode-map', `local-function-key-map' and
`key-translation-map'.
It also inputs real read events into the history with
`ergoemacs-command-loop--history'
It will timeout after `ergoemacs-command-loop-blink-rate' and
return nil."
(let ((input (ergoemacs-command-loop--history prompt ergoemacs-command-loop-blink-rate current-key))
last-input
basic mods
binding gui)
;; Fix issues with `input-decode-map'
(when input
;; Fix input as if you defined C-i -> <C-i> on `input-decode-map'
;; http://emacs.stackexchange.com/questions/10271/how-to-bind-c-for-real-seriously-for-real-this-time/15174
(if (and (display-graphic-p)
(setq basic (event-basic-type input))
(memq basic (list 'i 'm '\[ ?i ?m ?\[))
(setq mods (event-modifiers input))
(memq 'control mods)
(setq gui (ergoemacs-translate--event-convert-list (append (list 'ergoemacs-gui) mods (list basic))))
(setq binding (key-binding (ergoemacs :combine current-key input) t)))
(setq input gui)
(setq input (ergoemacs-command-loop--decode-event input input-decode-map current-key)
binding (key-binding (ergoemacs :combine current-key input) t)))
;; These should only be replaced if they are not bound.
(unless binding
(setq last-input input
input (ergoemacs-command-loop--decode-event input local-function-key-map current-key))
(unless (eq last-input input)
(setq binding (key-binding (ergoemacs :combine current-key input) t))))
(setq last-input input
input (ergoemacs-command-loop--decode-event input key-translation-map current-key))
(unless (eq last-input input)
(setq binding (key-binding (ergoemacs :combine current-key input) t))))
input))
(defun ergoemacs-command-loop--key-msg (blink-on universal text current-key unchorded _trans keys)
"Key message.
BLINK-ON is the flag for if the blink is on
UNIVERSAL is if the prompt is in the universal argument.
TEXT for prompting.
CURRENT-KEY Current key.
UNCHORDED is this a unchorded key?
TRANS translation information.
KEYS is the keys information"
(format
"%s" (concat
ergoemacs-command-loop--read-key-prompt
(ergoemacs-command-loop--read-key-help-text-prefix-argument blink-on universal)
text
(ergoemacs-key-description current-key)
unchorded
;; Cursor
(or (and (string= ergoemacs-command-loop--read-key-prompt "") "") " ")
(or (and universal "")
(and ergoemacs-command-loop-blink-character
(or (and blink-on ergoemacs-command-loop-blink-character)
" "))
" ")
;trans
keys)))
(defvar erogemacs-command--echo-timer nil)
(defvar ergoemacs-command--blink-on nil)
(defvar ergoemacs-orig-echo-keystrokes nil)
(defvar ergoemacs-command--timeout-timer nil)
(defvar ergoemacs-command--timeout-keys nil)
(defvar ergoemacs-this-command-keys-shift-translated nil
"ergoemacs override of shift translation in command loop.")
(defun ergoemacs-command--echo-prefix (&optional new-keys)
"Echos prefix keys in the ergoemacs-mode way.
NEW-KEYS replaces the value of `this-single-command-keys' if specified."
(let ((keys (or new-keys (this-single-command-keys))))
(when (and ergoemacs-command--timeout-timer
(not (equal keys ergoemacs-command--timeout-keys)))
(cancel-timer ergoemacs-command--timeout-timer)
(setq ergoemacs-command--timeout-keys nil
ergoemacs-command--timeout-timer nil))
(unless (or (equal [] keys)
(ergoemacs-command-loop-p))
(when (keymapp (key-binding keys))
(unless unread-command-events
(ergoemacs-command-loop--message
"%s" (ergoemacs-command-loop--key-msg
(setq ergoemacs-command--blink-on (not ergoemacs-command--blink-on))
nil nil keys
nil nil nil)))))))
(defun ergoemacs-command--echo-timer ()
"Echo the keystrokes in the `ergoemacs-mode' way."
(when (and (not ergoemacs-command-loop-type)
(not erogemacs-command--echo-timer))
(unless ergoemacs-orig-echo-keystrokes
(setq ergoemacs-orig-echo-keystrokes echo-keystrokes))
(setq echo-keystrokes 0)
(setq erogemacs-command--echo-timer
(run-at-time t ergoemacs-command-loop-blink-rate #'ergoemacs-command--echo-prefix))))
(defun ergoemacs-command--echo-timer-off ()
"Turn off the timer."
(setq echo-keystrokes ergoemacs-orig-echo-keystrokes)
(when erogemacs-command--echo-timer
(cancel-timer erogemacs-command--echo-timer)))
(add-hook 'ergoemacs-post-command-hook #'ergoemacs-command--echo-timer)
(add-hook 'ergoemacs-shutdown-hook #'ergoemacs-command--echo-timer-off)
(defun ergoemacs-command-loop--read-key (&optional current-key type universal)
"Read a key for the `ergoemacs-mode' command loop.
This uses `ergoemacs-command-loop--read-event'.
CURRENT-KEY is the current key that is being read, the next key
read will be appended to this key.
TYPE is the type of translation being applied. By default,
the :normal traslation is used.
UNIVERSAL flag telss if this is a univeral argument that is being
read."
(let* ((universal universal)
(type (or type :normal))
(translation (ergoemacs-translate--get type))
(local-keymap (ergoemacs-translate--keymap translation))
(text (ergoemacs-translation-struct-text translation))
(unchorded (ergoemacs-translation-struct-unchorded translation))
(trans (ergoemacs-translation-struct-translation translation))
(modal (ergoemacs :modal-p))
(keys nil)
(blink-on nil)
input
raw-input
mod-keys tmp
reset-key-p
double
first)
;; Setup modal translation
(when (and (eq type :normal) modal)
(setq type (ergoemacs-translation-struct-key modal)
local-keymap (ergoemacs-translation-struct-keymap-modal modal)
text (ergoemacs-translation-struct-text modal)
unchorded (ergoemacs-translation-struct-unchorded modal)
trans (ergoemacs-translation-struct-translation modal)
tmp translation
translation modal
modal tmp
tmp nil))
(when (functionp text)
(setq text (funcall text)))
(when trans
;; Don't echo the uncommon hyper/super/alt translations (alt is
;; not the alt key...)
(dolist (tr trans)
(unless (or (memq 'hyper (nth 0 tr)) (memq 'super (nth 0 tr)) (memq 'alt (nth 0 tr))
(and ergoemacs-command-loop-hide-shift-translations (memq 'shift (nth 0 tr))))
(if (member (list (nth 1 tr) (nth 0 tr)) trans)
(when (not (member (list (nth 1 tr) (nth 0 tr)) double))
(push tr double))
(push tr tmp))))
(setq trans tmp))
(setq trans (or (and (or trans double)
(concat "\nTranslations: "
(or (and double
(mapconcat
(lambda(elt)
;; (and (setq tmp (elt current-key 0))
;; (or (and (consp tmp) (symbolp (setq tmp (car tmp)))))
;; (stringp tmp)
;; (string-match-p "\\<mouse\\>" tmp))
(format "%s%s%s"
(ergoemacs :modifier-desc (nth 0 elt))
"↔"
(ergoemacs :modifier-desc (nth 1 elt))))
double ", "))
"")
(or (and double trans ", ") "")
(mapconcat
(lambda(elt)
(format "%s%s%s"
(ergoemacs :modifier-desc (nth 0 elt))
"→"
(ergoemacs :modifier-desc (nth 1 elt))))
trans ", "))) ""))
(maphash
(lambda(key item)
(let ((local-key (where-is-internal key local-keymap t))
tmp)
(when local-key
(setq tmp (format "%s%s%s"
(ergoemacs-key-description local-key)
(if (eq (nth 1 item) :force)
"⇒"
"→")
(ergoemacs :modifier-desc (nth 0 item))))
(push (elt local-key 0) mod-keys)
(setq keys (or (and (not keys) tmp)
(and keys (concat keys ", " tmp)))))))
ergoemacs-command-loop--next-key-hash)
(setq keys (or (and keys (concat "\nKeys: " keys)) ""))
(setq unchorded (or (and unchorded (concat " " (ergoemacs :modifier-desc unchorded))) ""))
(when (not first)
(message (ergoemacs-command-loop--key-msg blink-on universal text current-key unchorded trans keys))
(setq first t))