-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathergoemacs-translate.el
1269 lines (1159 loc) · 50.1 KB
/
ergoemacs-translate.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-translate.el --- Keyboard translation functions -*- lexical-binding: t -*-
;; Copyright © 2013-2023 Free Software Foundation, Inc.
;; Filename: ergoemacs-translate.el
;; Description:
;; Author: Matthew L. Fidler
;; Maintainer:
;; Created: Sat Sep 28 20:08:09 2013 (-0500)
;; Version:
;; Last-Updated:
;; By:
;; Update #: 0
;; URL:
;; Doc URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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:
(eval-when-compile
(require 'cl-lib)
(require 'ergoemacs-macros))
(defvar ergoemacs-define-key-after-p)
(defvar ergoemacs-keyboard-layout)
(defvar ergoemacs-keyboard-mirror)
(defvar ergoemacs-translation-hash)
(defvar ergoemacs-translate--hash)
(defvar ergoemacs-translate--event-hash)
(defvar ergoemacs-dir)
(defvar ergoemacs-theme)
(defvar ergoemacs-inkscape)
(defvar ergoemacs-command-loop--universal-functions)
(declare-function ergoemacs-layouts--list "ergoemacs-layouts")
(declare-function ergoemacs-theme--list "ergoemacs-theme-engine")
(declare-function ergoemacs-mode-reset "ergoemacs-mode")
(declare-function ergoemacs-layouts--custom-documentation "ergoemacs-layouts")
(declare-function ergoemacs-theme--custom-documentation "ergoemacs-theme-engine")
(declare-function ergoemacs-mode-line "ergoemacs-mode")
(declare-function ergoemacs-key-description--unicode-char "ergoemacs-key-description")
(declare-function ergoemacs-key-description-kbd "ergoemacs-key-description")
(declare-function ergoemacs-key-description--display-char-p "ergoemacs-key-description")
(declare-function ergoemacs-layouts--current "ergoemacs-layouts")
(declare-function ergoemacs-map-properties--label "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--label-map "ergoemacs-map-properties")
(declare-function ergoemacs-map-properties--put "ergoemacs-map-properties")
(declare-function ergoemacs-map-- "ergoemacs-map")
(declare-function ergoemacs-command-loop--modal-p "ergoemacs-command-loop")
(declare-function ergoemacs-translate--key-description "ergoemacs-translate")
(fset #'ergoemacs-translate--key-description (symbol-function #'key-description))
(defun ergoemacs-translate--get-hash (&optional layout-to layout-from)
"Gets the translation hash."
(let* ((to (ergoemacs :layout (or layout-to ergoemacs-keyboard-layout)))
(from (ergoemacs :layout (or layout-from "us")))
(hash-f (gethash from ergoemacs-translate--hash (make-hash-table)))
(hash-f-t (gethash to hash-f))
(i 0)
hash-t hash-t-f lay-t lay-f r-t r-f)
(if hash-f-t hash-f-t
(setq hash-f-t (make-hash-table)
hash-t (gethash to ergoemacs-translate--hash (make-hash-table))
hash-t-f (make-hash-table)
lay-t (symbol-value to)
lay-f (symbol-value from))
(while (< i 120)
(unless (or (string= "" (nth i lay-t))
(string= "" (nth i lay-f)))
(setq r-t (aref (read-kbd-macro (nth i lay-t) t) 0)
r-f (aref (read-kbd-macro (nth i lay-f) t) 0))
(puthash r-t r-f hash-t-f)
(puthash r-f r-t hash-f-t))
(setq i (+ i 1)))
(puthash from hash-t-f hash-t)
(puthash to hash-f-t hash-f)
(puthash to hash-t ergoemacs-translate--hash)
(puthash from hash-f ergoemacs-translate--hash)
hash-f-t)))
(defun ergoemacs-translate--emacs-shift (key-seq &optional modifier prefix)
"Uses Emacs style shift-translation: M-Q becomes M-q.
KEY-SEQ must be a vector. If there is no need to shift-translate
the key sequence return nil.
Optionally you can change how this function behaves.
Instead of translating the shifted key to the unshifted key, you
can remove another modifier. For example if you wanted to
convert C-M-a to C-a, you could use `meta' as the MODIFIER
argument to remove the M- modifier.
The PREFIX argument can add a key before the key where the
modifier occurred, such as in `ergoemacs-translate--meta-to-escape'.
"
(if (not (vectorp key-seq)) nil
(let ((rev-seq (reverse (append key-seq ())))
(which-mod (or modifier 'shift))
modifiers new-mod
found
(seq '()))
(dolist (event rev-seq)
(setq modifiers (ergoemacs-translate--event-modifiers event))
(if (not (memq which-mod modifiers)) (push event seq)
(setq new-mod (list (ergoemacs-translate--event-basic-type event)))
(dolist (mod modifiers)
(unless (eq which-mod mod)
(push mod new-mod)))
(push (ergoemacs-translate--event-convert-list new-mod) seq)
(when prefix
(push prefix seq))
(setq found t)))
(if found (vconcat seq) nil))))
(defvar ergoemacs-translate--define-key-if-defined-p t
"Define a key if even if it is already defined in the keymap.")
(defvar ergoemacs-translate--define-key-replacement-function nil
"When non-nil, use the replacement function for defining a key.")
(defun ergoemacs-translate--meta-to-escape (key-seq)
"Escapes a KEY-SEQ M-q becomes ESC q.
KEY-SEQ must be a vector. If there is no need to escape the key sequence return nil."
(ergoemacs-translate--emacs-shift key-seq 'meta 27))
(defun ergoemacs-translate--escape-to-meta (key-seq)
"Changes key sequences ESC q to M-q.
KEY-SEQ must be a vector or string. If there is no need to change the sequence, return nil."
(let ((key-seq (or (and (vectorp key-seq) key-seq)
(vconcat key-seq))))
(let ((rev-seq (reverse (append key-seq ())))
old-event
modifiers
found
seq)
(dolist (event rev-seq)
;; [27 134217736] -> nil
;; [27 8] -> [134217832]
;; [27 8 27] -> [134217832 27]
;; [27 27 8 27] -> [27 134217832 27]
;; [27 9] -> [134217737]
(cond
((and (eq 27 event) seq)
(setq old-event (pop seq)
modifiers (event-modifiers old-event))
(if (memq 'meta modifiers)
(progn
(push old-event seq)
(push event seq))
(setq found t)
(push (event-convert-list (append '(meta) modifiers (list (event-basic-type old-event)))) seq)))
(t
(push event seq))))
(and found (vconcat seq)))))
(defun ergoemacs-translate--swap-apps (key &optional what with)
"In KEY, swap apps key with menu key.
Optionally specify WHAT you want to replace WITH.
If no changes have been done, return nil."
(let ((seq (reverse (append key ())))
(what (or what 'apps))
(with (or with 'menu))
found-p
ret)
(dolist (e seq)
(cond
((eq e what)
(push with ret)
(setq found-p t))
(t (push e ret))))
(if found-p
(vconcat ret)
nil)))
(defun ergoemacs-translate--swap-menu (key)
"In KEY swap menu key with apps key."
(ergoemacs-translate--swap-apps key 'menu 'apps))
(defun ergoemacs-translate--to-vector (key)
"Translates KEY to vector format.
If no changes are performed, return nil."
(when (stringp key)
(let ((new-key (vconcat key))
ret)
(unless (equal key new-key)
(setq ret new-key))
ret)))
(defun ergoemacs-translate--ergoemacs-shift-select (key)
"Translate KEY to allow `ergoemacs-mode' shift translation.
This will shift translate Alt+# to Alt+3."
(let (modifiers basic)
(when (and (vectorp key)
;; only makes sense for single key combinations.
(= (length key) 1)
;; Doesn't make sense if shifted...
(not (or (memq 'shift (setq modifiers (ergoemacs-translate--event-modifiers (aref key 0))))
(memq 'ergoemacs-shift modifiers)))
;; Only define if emacs doesn't handle shift selection.
(not (eq (event-convert-list (list 'shift (setq basic (event-basic-type (aref key 0)))))
(ergoemacs-translate--event-convert-list (list 'ergoemacs-shift basic)))))
(setq ergoemacs-translate--define-key-if-defined-p nil
ergoemacs-translate--define-key-replacement-function #'ergoemacs-command-loop--shift-translate)
(vector (ergoemacs-translate--event-convert-list (append modifiers (list 'ergoemacs-shift basic)))))))
(defun ergoemacs-translate--ergoemacs-timeout (key)
"Translates KEY to allow Shift translation to default to key sequence.
This is done for key sequences like Ctrl+Shift+c which should
allow the Ctrl+c key sequence to be called when text is
seleceted (instead of copying the text)."
(let (modifiers basic)
(when (and (vectorp key)
;; only makes sense for single key combinations.
(= (length key) 2)
(eq 'ergoemacs-timeout (aref key 1))
;; Doesn't make sense if shifted...
(not (or (memq 'shift (setq modifiers (ergoemacs-translate--event-modifiers (aref key 0))))
(memq 'ergoemacs-shift modifiers))))
(setq basic (ergoemacs-translate--event-basic-type (aref key 0))
ergoemacs-translate--define-key-if-defined-p nil
ergoemacs-translate--define-key-replacement-function #'ergoemacs-command-loop--shift-timeout)
(vector (ergoemacs-translate--event-convert-list (append modifiers (list 'shift basic)))))))
(defun ergoemacs-translate--to-string (key)
"Translates KEY to string format.
If no chanegs are performed, return nil."
(catch 'not-ascii
(mapconcat
(lambda(key)
(if (and (integerp key) (< key 256))
(make-string 1 key)
(throw 'not-ascii nil)))
(append key) "")))
(defvar ergoemacs-translate--apply-funs
'(ergoemacs-translate--escape-to-meta
ergoemacs-translate--meta-to-escape
ergoemacs-translate--swap-apps
ergoemacs-translate--swap-menu
ergoemacs-translate--to-string
ergoemacs-translate--to-vector
ergoemacs-translate--ergoemacs-timeout
ergoemacs-translate--ergoemacs-shift-select)
"Functions to apply to key.
These functions take a key as an argument and translate it in
some way. If there is no appropriate translation, the function
should return nil.")
(defun ergoemacs-translate--apply-key (key function &rest args)
"Apply KEY to FUNCTION with ARGS.
In addition to the normal KEY variants are also applied. These
variants are created using `ergoemacs-translate--apply-funs'."
(let ((key key) test-key)
(apply function key args)
(dolist (fn ergoemacs-translate--apply-funs)
(when (setq test-key (funcall fn key))
(apply function test-key args)
(setq ergoemacs-translate--define-key-if-defined-p t
ergoemacs-translate--define-key-replacement-function nil)))))
(defun ergoemacs-translate--define-key (keymap key def)
"Similar to `define-key', with the following differences:
- Both the Meta and escape sequences are bound.
- Both <apps> and <menu> key sequences are bound.
- `ergoemacs-mode' advice to `define-key' is supressed.
KEYMAP is the keymap that will be used for the definition.
KEY is the key that is Emacs key that will be defined.
DEF is the definition of what will be run.
This uses `ergoemacs-translate--apply-key'"
(unwind-protect
(ergoemacs-translate--apply-key
key (lambda(new-key)
(when (or ergoemacs-translate--define-key-if-defined-p
(not (lookup-key keymap new-key)))
(define-key keymap new-key (or ergoemacs-translate--define-key-replacement-function def)))))
(setq ergoemacs-define-key-after-p nil)))
(defun ergoemacs-translate--event-modifier-hash (&optional layout)
"Gets the event modifier hash for LAYOUT."
(let* ((layout-symbol (ergoemacs :layout layout))
(hash (gethash layout-symbol ergoemacs-translate--event-hash)))
(if hash hash
;; Not present setup modifier hash
(setq hash (make-hash-table))
(let ((lay (symbol-value layout-symbol))
(i 0)
r1 r2)
(while (< i 60)
(unless (or (string= "" (nth i lay))
(string= "" (nth (+ i 60) lay)))
(setq r1 (aref (read-kbd-macro (nth i lay) t) 0)
r2 (aref (read-kbd-macro (nth (+ i 60) lay) t) 0))
(unless (eq (event-basic-type r1) (event-basic-type r2))
;; This shifted expression isn't covered by basic emacs.
(puthash r2 r1 hash)
(puthash (intern (format "s%s" r1)) r2 hash)))
(setq i (+ i 1)))
(puthash layout-symbol hash ergoemacs-translate--event-hash))
hash)))
(defun ergoemacs-translate--event-modifiers (event &optional layout)
"Return a list of symbols representing the modifier keys in event EVENT.
This is different than `event-modifiers' in two ways:
- Symbol keys like # will return `ergoemacs-shift' for a QWERTY keyboard.
- Special keys like C-RET will return `ergoemacs-control'
LAYOUT is the keyboard layout."
(let ((modifiers (event-modifiers event))
tmp
basic)
(unless (memq 'shift modifiers)
;; Add 'shift for # type events.
(setq basic (event-basic-type event))
(when (gethash basic (ergoemacs-translate--event-modifier-hash layout))
(push 'ergoemacs-shift modifiers)))
;; Add 'ergoemacs-gui to the modifiers
(when (and (symbolp event)
(setq tmp (symbol-name event))
(>= (length tmp) 2)
(char-equal (aref (substring tmp -2 -1) 0) ?-)
(memq (aref (substring tmp -1) 0)
(list ?\[ ?i ?m)))
(push 'ergoemacs-gui modifiers))
;; Also add 'ergoemacs-control to C-RET which translates to C-m
(when (and (integerp event)
(> event 1000)
(memq 'control modifiers))
(unless basic
(setq basic (event-basic-type event)))
(cond
((and (memq basic '(?m ?\[))
(string-match-p "C-" (ergoemacs-translate--key-description (vector event))))
(push 'ergoemacs-control modifiers))
((and (eq basic ?i)
(string-match-p "C-.*TAB" (ergoemacs-translate--key-description (vector event))))
(push 'ergoemacs-control modifiers))))
modifiers))
(defun ergoemacs-translate--event-layout (event &optional layout-to layout-from basic modifiers)
"Translate EVENT to the appropriate keyboard layout.
EVENT can be an event, or a vector. When EVENT is a vector,
each of the events within the vector/key are translated.
LAYOUT-TO is the layout to translate to, (default
`ergoemacs-keyboard-layout')
LAYOUT-FROM is the layout to translate from, (default: \"us\")
BASIC is the precalculated basic event from
`ergoemacs-translate--event-basic-type'.
MODIFIERS is the precalculated modifiers from
`ergoemacs-translate--event-modifiers'."
(if (vectorp event)
(progn
(apply #'vector (mapcar (lambda(x) (ergoemacs-translate--event-layout x layout-to layout-from basic modifiers)) event)))
(let* ((basic (or basic (ergoemacs-translate--event-basic-type event layout-from)))
(modifiers (or modifiers (ergoemacs-translate--event-modifiers event layout-from)))
new-modifiers
new-event
(translation-hash (ergoemacs-translate--get-hash layout-to layout-from)))
(cond
((and (eq system-type 'windows-nt) (eq basic 'menu))
(setq basic 'apps))
((and (not (eq system-type 'windows-nt)) (eq basic 'apps))
(setq basic 'menu)))
(if (memq 'ergoemacs-control modifiers)
(setq new-event basic
new-modifiers modifiers)
(if (or (memq 'shift modifiers)
(memq 'ergoemacs-shift modifiers))
(dolist (m modifiers)
(if (not (memq m '(shift ergoemacs-shift)))
(push m new-modifiers)
(setq new-event (ergoemacs-translate--event-convert-list (list m basic) layout-from))
(setq new-event (or (gethash new-event translation-hash) new-event))))
(setq new-event (or (gethash basic translation-hash) basic)
new-modifiers modifiers)))
(ergoemacs-translate--event-convert-list (append new-modifiers (list new-event)) layout-to))))
(defun ergoemacs-translate--event-basic-type (event &optional layout)
"Return the basic type of the given event (all modifiers removed).
This is different than `event-basic-type' because ?# would return
?3 on a QWERTY LAYOUT.
This also translates <C-i> to ?i, <C-m> to ?m <C-[> to ?[
"
(let* ((basic (event-basic-type event))
(new-basic (and basic (gethash basic (ergoemacs-translate--event-modifier-hash layout)))))
(or new-basic basic
(and (symbolp event)
(setq basic (symbol-name event))
(string-match "\\([im[]\\)$" basic)
(aref (match-string 1 basic) 0)))))
(defun ergoemacs-translate--event-convert-list (list &optional layout)
"Convert the event description LIST to an event type.
This is different than `event-convert-list' because:
- (shift ?3) or (ergoemacs-shift ?3) produces ?# on a QWERTY LAYOUT.
- (ergoemacs-control control ?m) produces C-RET
- (ergoemacs-gui control ?m) produces <C-m>. this applies for ?i and ?[ as well.
- Mouse events allow click modifiers"
(let ((cur-list list)
elt
tmp
control-p
new-list
first second base
(gui-p (memq 'ergoemacs-gui list)))
(when (or gui-p (setq control-p (memq 'ergoemacs-control cur-list)))
(setq cur-list (reverse cur-list))
(if (and gui-p (memq (car cur-list) (list '\[ 'm 'i ?\[ ?m ?i))
(memq 'control cur-list))
(setq gui-p (pop cur-list))
(setq gui-p nil))
(dolist (elt cur-list)
(unless (memq elt '(ergoemacs-control ergoemacs-gui))
(push elt new-list)))
(setq cur-list new-list)
(setq new-list nil))
(if (not (or (memq 'shift list) (memq 'ergoemacs-shift list)))
(setq new-list cur-list)
(while (> (length cur-list) 0)
(setq elt (pop cur-list))
(cond
((and cur-list (memq elt '(shift ergoemacs-shift))))
((and (not cur-list)
(setq tmp (gethash (intern (format "s%s" elt))
(ergoemacs-translate--event-modifier-hash layout))))
;; Special case.
(setq new-list (append new-list (list tmp))))
((not cur-list)
(push 'shift new-list)
(setq new-list (append new-list (list elt))))
(t
(push elt new-list)))))
(setq new-list (reverse new-list)
base (pop new-list)
tmp nil)
(dolist (elt new-list)
(cond
((memq elt '(double triple))
(setq first (format "%s-" elt)))
((memq elt '(click)))
((memq elt '(drag down))
(setq second (format "%s-" elt)))
(t (push elt tmp))))
(when (or first second)
(setq base (intern (format "%s%s%s" (or first "") (or second "") base))))
(setq new-list (append tmp (list base)))
(cond
(gui-p (aref (read-kbd-macro (concat (substring (ergoemacs-translate--key-description (vector (event-convert-list (append new-list (list 'ack))))) 0 -4) (or (and (symbolp gui-p) (symbol-name gui-p)) (make-string 1 gui-p)) ">")) 0))
(control-p (aref (read-kbd-macro (concat "C-" (ergoemacs-translate--key-description (vector (event-convert-list new-list)))) t) 0))
(t (event-convert-list new-list)))))
(defun ergoemacs-translate (kbd &optional just-first-keys variable-modifiers
variable-prefixes layout-to layout-from)
"Translate between ergoemacs-mode keyboard layouts.
KBD is the key.
JUST-FIRST-KEYS is a list of keys where the keyboard translation
stops. For example when JUST-FIRST-KEYS is [apps ?n] would
translate QWERTY [apps ?n ?n] to colemak [apps ?k ?n] instead of
[apps ?k ?k].
VARIABLE-MODIFIERS are the modifiers that cause translation
between keyboards to occur.
VARIABLE-PREFIXES are the list of prefix keys that are variable.
LAYOUT-TO is the layout that `ergoemacs-mode' will translate
to. By default it is the layout specified by
`ergoemacs-keyboard-layout'.
LAYOUT-FROM is the layout that `ergoemacs-mode' will translate
from. By default this is the us layout.
This uses the function `ergoemacs-translate--event-layout' to
make the translation."
(let ((var-mod variable-modifiers)
(var-pre variable-prefixes)
(ret [])
(untranslated [])
(just-first-keys (or (and (vectorp just-first-keys) (list just-first-keys))
just-first-keys))
translated-event
just-first-p
translate-prefix-p
basic modifiers)
(dolist (event (listify-key-sequence kbd))
(setq basic (ergoemacs-translate--event-basic-type event layout-from)
modifiers (ergoemacs-translate--event-modifiers event layout-from))
(unless translate-prefix-p
(setq translate-prefix-p (member untranslated var-pre)))
(when (and just-first-keys (not just-first-p))
(setq just-first-p (member untranslated just-first-keys)))
(cond
((and (or (catch 'found-modifiers
(dolist (m modifiers)
(when (memq m var-mod)
(throw 'found-modifiers t)))
nil)
translate-prefix-p)
(not just-first-p))
(setq translated-event
(ergoemacs-translate--event-layout event layout-to layout-from basic modifiers)))
((and (eq system-type 'windows-nt) (eq basic 'menu))
(setq translated-event (ergoemacs-translate--event-convert-list (append modifiers '(apps)))))
((and (not (eq system-type 'windows-nt)) (eq basic 'apps))
(setq translated-event (ergoemacs-translate--event-convert-list (append modifiers '(menu)))))
(t (setq translated-event event)))
(setq untranslated (vconcat untranslated (list event))
ret (vconcat ret (list translated-event))))
ret))
(defun ergoemacs-translate--event-trials (event &optional exclude-basic extra-modifiers mirror-p)
"Gets a list of `ergoemacs-mode' event trials.
When EXCLUDE-BASIC is non-nil, don't include the keys that are likely to produce a character when typing.
When MIRROR-P is non-nil, this is another call to the event trials for a keyboard mirror."
(if (not (eventp event))
(error "Need an event for event trials (%s)" event))
(let* ((key event)
(ret '())
(basic (ergoemacs-translate--event-basic-type key))
(gui-p (and (symbolp key) (memq basic '(m i \[ ?m ?i ?\[))))
(extra-modifiers extra-modifiers)
trial)
(when gui-p
(push 'ergoemacs-gui extra-modifiers))
(unless exclude-basic
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'shift basic))))
(unless (eq trial key)
(push trial ret)))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'control basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'meta basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'meta 'control basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'shift 'control basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'shift 'meta basic))))
(unless (eq trial key)
(push trial ret))
(setq trial (ergoemacs-translate--event-convert-list (append extra-modifiers (list 'shift 'meta 'control basic))))
(unless (eq trial key)
(push trial ret))
(unless extra-modifiers
(setq ret (append (ergoemacs-translate--event-trials event exclude-basic (list 'hyper)) ret))
(setq ret (append (ergoemacs-translate--event-trials event exclude-basic (list 'super)) ret))
(setq ret (append (ergoemacs-translate--event-trials event exclude-basic (list 'hyper 'super)) ret)))
(when gui-p
(setq extra-modifiers (cdr extra-modifiers)
event (ergoemacs-translate--event-convert-list (append extra-modifiers (list basic))))
(setq ret (append (ergoemacs-translate--event-trials event exclude-basic nil) ret)))
(when (and (not mirror-p) ergoemacs-keyboard-mirror)
(setq ret (append ret
(ergoemacs-translate--event-trials
(ergoemacs-translate--event-layout event (format "%s" ergoemacs-keyboard-layout) (format "%s" ergoemacs-keyboard-mirror))
exclude-basic extra-modifiers t)
(ergoemacs-translate--event-trials
(ergoemacs-translate--event-layout event (format "%s" ergoemacs-keyboard-mirror) (format "%s" ergoemacs-keyboard-layout))
exclude-basic extra-modifiers t))))
ret))
(defun ergoemacs-translate--trials (key)
"Get the `ergoemacs-mode' keys to lookup for KEY.
For keys, the list consists of:
- 1: The key itself
- 2: The `ergoemacs-mode' shift translation (if needed/applicable or nil)
- 3: The other translations"
;; (nth 1 (ergoemacs-translate--trials (kbd "M-A"))) = (kbd "M-a")
(let* ((key (vconcat key))
(event (elt (substring key -1) 0))
(base (substring key 0 -1))
ret)
(if (consp event)
(let ((mod (event-modifiers event))
(basic (event-basic-type event))
double-p
triple-p
strip-mod)
(when (or (setq double-p (memq 'double mod))
(setq triple-p (memq 'triple mod)))
(dolist (a mod)
(unless (memq a '(double triple))
(push a strip-mod))))
(cond
(double-p
;; Double -> single
(push (vector (list (event-convert-list (append strip-mod (list basic)))
(cdr event)))
ret)
(push nil ret)
(push key ret))
(triple-p
;; double -> single
(push (vector (list (event-convert-list (append strip-mod (list basic)))
(cdr event)))
ret)
;; triple->double
(push (vector (list (event-convert-list (append strip-mod (list 'double basic)))
(cdr event)))
ret)
(push nil ret)
(push key ret))
(t
;; Do not put any trials in just use the mouse event.
(push key ret)))
ret)
(dolist (new (ergoemacs-translate--event-trials event (= 1 (length key))))
(push (vconcat base (vector new)) ret))
(when (memq 'ergoemacs-gui (ergoemacs-translate--event-modifiers event))
(push (ergoemacs-translate--no-gui key) ret))
(if (= 1 (length key))
(push (or (ergoemacs-translate--emacs-shift key 'shift)
(ergoemacs-translate--emacs-shift key 'ergoemacs-shift)) ret)
(push nil ret) ;; No shift translation
)
(push key ret))
ret))
(cl-defstruct ergoemacs-translation-struct
"A basic ergoemacs translation structure."
(name "default-name")
(translation '())
(universal-argument nil)
(negative-argument nil)
(digit-argument nil)
(modal nil)
(text "")
(keymap (make-sparse-keymap))
(keymap-modal (make-sparse-keymap))
(modal-always nil)
(modal-color nil)
(key nil)
(unchorded nil))
(defvar ergoemacs-translate--setup-command-loop-regexp
"^\\(?:ergoemacs\\(?:-translate-\\)?\\)-\\(.*?\\)-\\(universal-argument\\|negative-argument\\|digit-argument\\|modal\\)$"
"Command loop command match/setup regular expression.")
(defun ergoemacs-translate--setup-command-loop ()
"Setup command loop.
To do anything, `this-command' must match
`ergoemacs-translate--setup-command-loop-regexp'. The first
match is the NAME of the translation, the second match is the
TYPE of command. This command will then
call (ergoemacs-command-loop-TYPE :NAME)."
(interactive)
(let ((command-str (symbol-name this-command))
name type)
(save-match-data
(when (string-match ergoemacs-translate--setup-command-loop-regexp command-str)
(setq name (match-string 1 command-str)
type (match-string 2 command-str))
(funcall (intern (concat "ergoemacs-command-loop--" type)) (intern (concat ":" name)))))))
(defun ergoemacs-translate--setup-translation (&optional name)
"Setup translation functions and keymaps.
If NAME is nil, setup all translations.
When NAME is a symbol, setup the translation function for the symbol."
(if (not name)
(maphash
(lambda(name _item)
(ergoemacs-translate--setup-translation name))
ergoemacs-translation-hash)
(let ((name-str (and (symbolp name) (substring (symbol-name name) 1))))
(eval
(macroexpand ; Fixme why?
`(progn
(defvar ,(intern (concat "ergoemacs-translate--" name-str "-map")) (make-sparse-keymap)
,(concat "Ergoemacs local map for translation :"
name-str
" while completing a key sequence."))
(define-obsolete-variable-alias ',(intern (concat "ergoemacs-" name-str "-translation-local-map"))
',(intern (concat "ergoemacs-translate--" name-str "-map"))
"Ergoemacs-v5.16")))
t)
;;
(dolist (type '("-universal-argument" "-negative-argument"
"-digit-argument" "-modal"))
(fset (intern (concat "ergoemacs-translate--" name-str type))
#'ergoemacs-translate--setup-command-loop)
(fset (intern (concat "ergoemacs-" name-str type))
#'ergoemacs-translate--setup-command-loop)
(when (string= type "-universal-argument")
(cl-pushnew (intern (concat "ergoemacs-" name-str type)) ergoemacs-command-loop--universal-functions)
(cl-pushnew (intern (concat "ergoemacs-translate--" name-str type)) ergoemacs-command-loop--universal-functions))))))
(add-hook 'ergoemacs-mode-intialize-hook #'ergoemacs-translate--setup-translation)
(defun ergoemacs-translate--create (&rest plist)
"Create a translation from PLIST and return translation object."
(let ((plist plist)
struct
-universal-argument
-negative-argument
-digit-argument
-modal
translation
(local-keymap (or (plist-get plist :keymap) (make-sparse-keymap))))
(let (tmp
cur-trans
ret)
(dolist (elt plist)
(cond
((and (symbolp elt)
(progn
(setq cur-trans nil)
(string-match-p "\\(hyper\\|super\\|shift\\|meta\\|control\\|alt\\|cn?tr?l\\)"
(setq tmp (symbol-name elt)))))
(while (string-match "\\(hyper\\|super\\|shift\\|meta\\|control\\)" tmp)
(cond
((string-match-p "cn?tr?l" (match-string 1 tmp))
(push 'conrtol cur-trans))
((string= "alt" (match-string 1 tmp))
(push 'meta cur-trans))
(t
(push (intern (match-string 1 tmp)) cur-trans)
(setq tmp (replace-match "" t t tmp))))))
(cur-trans
(cond
((consp elt) ;; Assume list is correct.
(push (list (sort cur-trans #'string<) elt) ret))
((stringp elt)
;; Allow "C-M-" syntax (backward compatible)
(push (list (sort cur-trans #'string<) (event-modifiers (elt (read-kbd-macro (concat elt "q") t) 0))) ret)))
(setq cur-trans nil))))
(when (setq tmp (plist-get plist :unchorded))
(push (list nil tmp) ret))
(setq translation ret))
(setq struct
(make-ergoemacs-translation-struct
:name (plist-get plist :name)
:translation translation
:universal-argument -universal-argument
:negative-argument -negative-argument
:digit-argument -digit-argument
:modal -modal
:text (plist-get plist :text)
:keymap local-keymap
:keymap-modal (or (plist-get plist :keymap-modal) (make-sparse-keymap))
:modal-always (plist-get plist :modal-always)
:modal-color (plist-get plist :modal-color)
:key (plist-get plist :key)
:unchorded (plist-get plist :unchorded)))
(puthash (plist-get plist :key) struct ergoemacs-translation-hash)
struct))
(defun ergoemacs-translate--get (type)
"Get translation object TYPE."
(let ((ret (gethash type ergoemacs-translation-hash)))
(cond
((and ret (ergoemacs-translation-struct-p ret))
ret)
((and ret (functionp ret))
(setq ret (funcall ret)))
(t
(error "Somethings wrong; cant get translation %s" type)))))
(defun ergoemacs-translate--event-mods (event &optional type)
"Translate EVENT modifiers by the translation TYPE.
If TYPE is unspecified, assume :normal translation"
(let* ((type (or type :normal))
(translation (and (symbolp type) (ergoemacs-translate--get type)))
(basic (ergoemacs-translate--event-basic-type event))
(e-mod (ergoemacs-translate--event-modifiers event))
(modifiers (sort (mapcar
(lambda(e)
(cond
((eq 'ergoemacs-shift e) 'shift)
((eq 'ergoemacs-control e) 'control)
(t e)))
e-mod)
#'string<))
(special-p (memq basic (list ?m ?i ?\[)))
(ambiguous-p (and special-p (memq 'control e-mod)))
tmp
(ret event))
(when ambiguous-p
(dolist (m modifiers)
(unless (eq 'control m)
(push m tmp)))
(when (memq 'ergoemacs-control e-mod)
(push 'control tmp))
(setq modifiers tmp))
(when (catch 'found-mod
(dolist (mod (or (and (ergoemacs-translation-struct-p translation)
(ergoemacs-translation-struct-translation translation))
(and (consp type) (consp (nth 0 type)) type)
(and (consp type) (list (list nil type)))))
(when (equal (nth 0 mod) modifiers)
(setq modifiers (nth 1 mod))
(cond
((and ambiguous-p
(memq 'control modifiers))
(push 'ergoemacs-control modifiers))
((and special-p (display-graphic-p)
(memq 'control modifiers))
(push 'ergoemacs-gui modifiers)))
(throw 'found-mod t)))
nil)
(if ambiguous-p
(setq ret (ergoemacs-translate--event-convert-list `(control ,@modifiers ,basic)))
(setq ret (ergoemacs-translate--event-convert-list `(,@modifiers ,basic)))))
ret))
(defun ergoemacs-translate--no-gui (event)
"Remove any gui elements to the EVENT.
If there are no gui elements, return nil."
(if (vectorp event)
(apply #'vector (mapcar (lambda(x) (or (ergoemacs-translate--no-gui x) x)) event))
(let* ((last-event event)
(last-mod (ergoemacs-translate--event-modifiers last-event))
(last-basic-event (ergoemacs-translate--event-basic-type last-event))
new-mod)
(when (memq 'ergoemacs-gui last-mod)
(dolist (elt last-mod)
(unless (eq elt 'ergoemacs-gui)
(push elt new-mod)))
(ergoemacs-translate--event-convert-list `(,@new-mod ,last-basic-event))))))
(defvar ergoemacs-translate--parent-map (make-sparse-keymap)
"Parent map for keymaps when completing a key sequence.")
(defvar ergoemacs-translate--modal-parent-map (make-sparse-keymap)
"Parent map for modal `ergoemacs-mode'")
(defvar ergoemacs-translate--keymap-hash (make-hash-table)
"Translation keymaps")
(defun ergoemacs-translate--keymap-reset ()
"Reset `ergoemacs-translate--keymap-hash'"
(setq ergoemacs-translate--keymap-hash (make-hash-table)))
;; (add-hook 'ergoemacs-mode-intialize-hook #'ergoemacs-translate--keymap-reset)
(defun ergoemacs-translate--keymap (&optional translation)
"Get the keymap for TRANSLATION.
This takes into consideration the modal state of `ergoemacs-mode'."
(let* ((modal (ergoemacs :modal-p))
(translation (or (and (ergoemacs-translation-struct-p translation)
(or (not modal) ;; prefer modal when :normal
(not (eq :normal (ergoemacs-translation-struct-key translation))))
translation)
modal
(ergoemacs-translate--get (or translation :normal))))
(key (or (and modal (intern (concat ":" (ergoemacs-translation-struct-name translation) "-modal")))
(ergoemacs-translation-struct-key translation)))
(ret (gethash key ergoemacs-translate--keymap-hash))
keymap)
(unless ret
(if modal
(setq keymap (ergoemacs-translation-struct-keymap-modal translation)
ret keymap)
(setq keymap (ergoemacs-translation-struct-keymap translation)
ret (make-composed-keymap keymap ergoemacs-translate--parent-map)))
(puthash key ret ergoemacs-translate--keymap-hash))
ret))
(defun ergoemacs-translate--ergoemacs-to-quail (layout)
"Translates an ergoemacs-mode layout to a quail layout."
(let ((lay layout)
(ret (make-string 32 ? ))
(i 2))
(while (< i 14)
(setq ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay)))
i (+ i 1)))
(setq i 1
ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay))
(make-string 4 ? ))
i 17)
(while (< i 29)
(setq ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay)))
i (+ i 1)))
(setq ret (concat ret (make-string 6 ? ))
i 32)
(while (< i 43)
(setq ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay)))
i (+ i 1)))
(setq i 29
ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay))
(make-string 6 ? ))
i 47)
(while (< i 57)
(setq ret (concat ret (or (and (string= "" (nth i lay)) " ")
(nth i lay))
(or (and (string= "" (nth (+ i 60) lay)) " ")
(nth (+ i 60) lay)))
i (+ i 1)))
(setq ret (concat ret (make-string 38 ? )))
ret))
(defun ergoemacs-translate--quail-to-ergoemacs (quail)
"Translate QUAIL layout to ergoemacs layout"
(let ((vec (vconcat quail))
(i 0)
retu
retl)
(while (< i 3)
(push "" retu)
(push "" retl)
(setq i (+ i 1)))
(setq i (+ (* 4 30) 21))
(while (>= i (+ (* 4 30) 2))
(push (make-string 1 (aref vec i)) retu)