-
Notifications
You must be signed in to change notification settings - Fork 3
/
keymap.lisp
714 lines (640 loc) · 30.2 KB
/
keymap.lisp
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
;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause
(in-package :nkeymaps/core)
;; TODO: Add timeout support, e.g. "jk" in less than 0.1s could be ESC in VI-style.
(defstruct modifier
(string "" :type string)
(shortcut "" :type string))
(declaim (ftype (function ((or string modifier) (or string modifier)) boolean) modifier=))
(defun modifier= (string-or-modifier1 string-or-modifier2)
"Return non-nil if STRING-OR-MODIFIER1 and STRING-OR-MODIFIER2 represent the same modifier."
(unless (or (modifier-p string-or-modifier1)
(modifier-p string-or-modifier2))
(error 'bad-modifier :message "At least one of the arguments must be a modifier."))
(flet ((match-modifier (modifier string)
(or (string= (modifier-string modifier) string)
(string= (modifier-shortcut modifier) string))))
(cond
((stringp string-or-modifier1)
(match-modifier string-or-modifier2 string-or-modifier1))
((stringp string-or-modifier2)
(match-modifier string-or-modifier1 string-or-modifier2))
(t
(or (string= (modifier-string string-or-modifier1)
(modifier-string string-or-modifier2))
(string= (modifier-shortcut string-or-modifier1)
(modifier-shortcut string-or-modifier2)))))))
(defmethod fset:compare ((x modifier) (y modifier))
"Modifier sets need this comparison function to be ordered, so that (\"C\"
\"M\") is the same as (\"M\" \"C\")."
(fset:compare-lexicographically (modifier-string x) (modifier-string y)))
(defparameter *modifier-list* (list)
"List of known modifiers.
`make-key' and `define-key' raise an error when setting a modifier that is not
in this list.")
(defun define-modifier (&rest args &key string shortcut)
"Return a new modifier.
It is registered globally and can be used from any new keymap, unless the keymap
filters out the modifier in its `modifiers' slot."
(declare (ignore string shortcut))
(let ((result (apply #'make-modifier args)))
(setf *modifier-list* (delete result *modifier-list*
:test #'modifier=))
(push result *modifier-list*)
result))
(deftype key-status-type ()
`(member :pressed :released))
(deftype modifier-list ()
"List of modifiers are sets: they are ordered and have no duplicates."
`fset:wb-set)
;; Must be a structure so that it can served as a key in a hash-table with
;; #'equalp tests.
(defstruct (key (:constructor %make-key (code value modifiers status))
(:copier %copy-key))
"The fundamental type to represent any input, be it keyboard, mouse or others."
(code 0 :type integer) ; TODO: Can a keycode be 0? I think not, so 0 might be a good non-value.
(value "" :type string)
(modifiers (fset:set) :type modifier-list)
(status :pressed :type key-status-type))
(defun key= (key1 key2)
"Two keys are equal if the have the same modifiers, status and key code.
If codes don't match, the values are compared instead. This way, code-matching
keys match before the value which is usually what the users want when they
specify a key-code binding."
(and (or (and (not (zerop (key-code key1)))
(= (key-code key1)
(key-code key2)))
(and
(not (uiop:emptyp (key-value key1)))
(string= (key-value key1)
(key-value key2))))
(fset:equal? (key-modifiers key1)
(key-modifiers key2))
(eq (key-status key1)
(key-status key2))))
(declaim (ftype (function ((or string modifier)) modifier) modspec->modifier))
(defun modspec->modifier (string-or-modifier)
"Return the `modifier' corresponding to STRING-OR-MODIFIER."
(if (modifier-p string-or-modifier)
string-or-modifier
(let ((modifier (find-if (alex:curry #'modifier= string-or-modifier) *modifier-list*)))
(or modifier
(error 'bad-modifier
:message (format nil "Unknown modifier ~a" string-or-modifier))))))
(declaim (ftype (function ((or (list-of string)
(list-of modifier)
modifier-list))
modifier-list)
modspecs->modifiers))
(defun modspecs->modifiers (strings-or-modifiers)
"Return the list of `modifier's corresponding to STRINGS-OR-MODIFIERS."
(flet ((list-difference (list1 list2)
(dolist (list2-elt list2 list1)
(setf list1 (delete list2-elt list1 :count 1)))))
(if (typep strings-or-modifiers 'modifier-list)
strings-or-modifiers
(the (values modifier-list &optional)
(fset:convert 'fset:set
(let* ((mods (mapcar #'modspec->modifier strings-or-modifiers))
(no-dups-mods (delete-duplicates mods :test #'modifier=)))
(when (/= (length mods) (length no-dups-mods))
(warn 'duplicate-modifiers
:modifiers (mapcar #'modifier-string (list-difference mods no-dups-mods))))
no-dups-mods))))))
(declaim (ftype (function (&key (:code integer) (:value string)
(:modifiers list) (:status keyword))
key)
make-key))
(defun make-key (&key (code 0 explicit-code) (value "" explicit-value)
modifiers
(status :pressed))
"Return new `key'.
Modifiers can be either a list of `modifier' types or of strings that will be
looked up in `*modifier-list*'."
(unless (or explicit-code explicit-value)
(error 'make-key-required-arg))
(%make-key
code
value
(modspecs->modifiers modifiers)
status))
(declaim (ftype (function (key &key (:code integer) (:value string)
(:modifiers modifier-list) (:status keyword))
key)
copy-key))
(defun copy-key (key &key (code (key-code key)) (value (key-value key))
(modifiers (key-modifiers key))
(status (key-status key)))
"Return a new copy of KEY.
This is useful as a low-level function to translate keys without modifying existing keys."
(let ((new-key (%copy-key key)))
(setf (key-value new-key) value
(key-code new-key) code
(key-status new-key) status
(key-modifiers new-key) (modspecs->modifiers modifiers))
new-key))
(defmethod fset:compare ((x key) (y key))
"Needed to use the KEY structure as keys in Fset maps.
Would we use the default comparison function, case-sensitivity would be lost on
key values because `fset:equal?' folds case."
(if (key= x y)
:equal
:unequal))
(declaim (ftype (function (string &optional boolean) (or key null)) keyspec->key))
(defun keyspec->key (string &optional error-p)
"Parse STRING and return a new `key'.
STRING must be passed is one of two forms:
MODIFIERS-#<code>
MODIFIERS-<value>
MODIFIERS are hyphen-separated modifiers as per `*modifier-list*'.
E.g. control-#1 or control-a.
Note that '-' or '#' as a last character is supported, e.g. 'control--' and
'control-#' are valid."
(handler-bind ((error (if error-p
#'identity
(lambda (c)
(warn 'bad-keyspec :error-condition c)
(return-from keyspec->key nil)))))
(when (string= string "")
(error 'empty-keyspec))
(let* ((last-nonval-hyphen (or (position #\- string :from-end t
:end (1- (length string)))
-1))
(code 0)
(value "")
(code-or-value (subseq string (1+ last-nonval-hyphen)))
(rest (subseq string 0 (1+ last-nonval-hyphen)))
(modifiers (butlast (uiop:split-string rest :separator "-"))))
(when (find "" modifiers :test #'string=)
(error 'empty-modifiers :keyspec string))
(when (and (<= 2 (length code-or-value))
(string= (subseq code-or-value (1- (length code-or-value)))
"-"))
(error 'empty-value :keyspec string))
(if (and (<= 2 (length code-or-value))
(string= "#" (subseq code-or-value 0 1)))
(setf code (or (parse-integer code-or-value :start 1 :junk-allowed t)
code))
(setf value code-or-value))
(make-key :code code :value value :modifiers modifiers))))
(declaim (ftype (function (string &optional boolean) (list-of key)) keyspecs->keys))
(defun keyspecs->keys (spec &optional error-p)
"Parse SPEC and return corresponding list of keys."
;; TODO: Return nil if SPEC is invalid?
(let* ((result (delete "" (uiop:split-string spec) :test #'string=)))
(mapcar (rcurry #'keyspec->key error-p) result)))
(defparameter *translator* (lambda (keys) (list keys))
"Key translator to use in `keymap' objects.
When no binding is found, call this function to
generate new bindings to lookup. The function takes a list of `key' objects and
returns a list of list of keys.
This parameter can be let-bound around `lookup-key' calls.")
(defparameter *default-bound-type* '(or keymap t)
"Default value for the `bound-type' slot of `keymap'.
Do not change this, instead create new a `keyscheme' or subclass `keymap'.")
(defclass keymap ()
((name :accessor name
:initarg :name
:initform "anonymous"
:type string
:documentation
"Name of the keymap.
Used for documentation purposes, e.g. referring to a keymap by a well known name.")
(entries :accessor entries
:initarg :entries
;; We cannot use the standard (make-hash-table :test #'equalp)
;; because then (set "a") and (set "A") would be the same thing.
:initform (fset:empty-map)
:type fset:wb-map
:documentation
"Hash table of which the keys are key-chords and the values are a
symbol or a keymap.")
(bound-type :accessor bound-type
:initarg :bound-type
:initform *default-bound-type*
:documentation
"Type of the bound-value.
The type is enforced in `define-key' at macro-expansion time.
Type should allow `keymap's, so it should probably be in the form
\(or keymap NON-KEYMAP-BOUND-TYPE).")
(parents :accessor parents
:initarg :parents
:initform nil
:type (list-of keymap)
:documentation "List of parent keymaps.
Parents are ordered by priority, the first parent has highest priority.")
(modifiers :accessor modifiers
:initarg :modifiers
:initform (fset:convert 'fset:set *modifier-list*)
:type modifier-list
:documentation "
Accepted modifiers for this `keymap'."))
(:documentation "A map of `key' bindings to values.
Values must be of type `bound-type'.
The bindings from the parents are inherited, with the children having precedence."))
(defmethod print-object ((keymap keymap) stream)
(print-unreadable-object (keymap stream :type t :identity t)
(format stream "~a" (name keymap))))
(declaim (ftype (function (string &rest keymap)
(values keymap &optional))
make-keymap))
(defun make-keymap (name &rest parents)
"Return a new `keymap'."
;; We use (values keymap &optional) type because of an SBCL limitation.
;; See http://www.sbcl.org/manual/#Implementation-Limitations.
(the (values keymap &optional)
(make-instance 'keymap
:name name
:parents parents)))
(defun keymap-p (object)
"Return non-nil of OBJECT is a `keymap'."
(typep object 'keymap))
(defun copy-keymap (keymap)
(let ((new-keymap (apply #'make-keymap (name keymap)
(parents keymap))))
(setf (bound-type new-keymap)
(bound-type keymap))
(setf (entries new-keymap)
(entries keymap))
new-keymap))
;; "keyspec" is better then "keydesc" since the strings are well specified,
;; while a "description" could be anything.
(deftype keyspecs-type ()
`(and string (satisfies keyspecs->keys)))
(declaim (ftype (function (keymap (or keyspecs-type list) (or keymap t)
&rest (or keyspecs-type list keymap t)))
define-key))
(defun define-key (keymap keyspecs bound-value &rest more-keyspecs-value-pairs)
"Bind KEYS to BOUND-VALUE in KEYMAP.
Return KEYMAP.
KEYSPECS is either a `keyspecs-type', or a list of arguments passed to invocations
of `make-key's, or (:REMAP OTHER-VALUE &OPTIONAL OTHER-KEYMAP).
BOUND-VALUE can be anything. If NIL, the binding is removed.
With (:REMAP OTHER-VALUE &OPTIONAL OTHER-KEYMAP), define-key maps the binding of
OTHER-VALUE in OTHER-KEYMAP (default to KEYMAP) to BOUND-VALUE.
In other words, it remaps OTHER-VALUE to VALUE.
Examples:
(define-key foo-map \"C-x C-f\" 'open-file)
(define-key foo-map
\"C-x C-f\" 'open-file
\"C-h k\" 'describe-key)
\"C-M-1 x\" on a QWERTY:
(define-key foo-map '((:code 10 :modifiers (\"C\" \"M\") (:value \"x\"))) 'open-file)
or the shorter:
(define-key foo-map \"C-M-#1\" 'open-file)
Remapping keys:
(define-key foo-map '(:remap foo-a) 'foo-value)
(define-key foo-map `(:remap foo-a ,bar-map) 'new-value)"
(let ((keyspecs-value-pairs (append (list keyspecs bound-value) more-keyspecs-value-pairs)))
(alex:doplist (keyspecs bound-value keyspecs-value-pairs)
(unless (typep bound-value (bound-type keymap))
;; (error 'type-error "Bound value ~a not of type ~a"
;; bound-value (bound-type keymap))
(assert (typep bound-value (bound-type keymap)) (bound-value)
'type-error :datum bound-value :expected-type (bound-type keymap)))
(let ((keys (cond
((and (listp keyspecs) (eq (first keyspecs) :remap))
(let ((other-value (second keyspecs))
(other-keymap (or (third keyspecs) keymap)))
(keyspecs->keys (first (binding-keys other-value other-keymap))
t)))
((listp keyspecs)
(mapcar (alex:curry #'apply #'make-key) keyspecs))
(t (keyspecs->keys keyspecs t)))))
(bind-key keymap keys bound-value)))
keymap))
(define-compiler-macro define-key (&whole form
keymap
keyspecs bound-value
&rest more-keyspecs-value-pairs)
"We need a compiler macro to check that bindings are valid at compile time.
This is because most Common Lisp implementations are not capable of checking
types that use `satisfies' for non-top-level symbols.
We can verify this with:
\(compile nil (lambda () (nkeymaps::define-key keymap \"C-x C-f\" 'open-file)))"
(declare (ignore keymap))
(let ((keyspecs-value-pairs (append (list keyspecs bound-value) more-keyspecs-value-pairs)))
(alex:doplist (keyspecs _ keyspecs-value-pairs)
(when (stringp keyspecs)
(check-type keyspecs (or keyspecs-type list))))
form))
(defun legal-modifiers-p (key keymap)
"Whether KEY's modifiers are allowed in KEYMAP."
(fset:empty? (fset:set-difference (key-modifiers key)
(modifiers keymap))))
(declaim (ftype (function (keymap (list-of key) (or keymap t)) (values keymap &optional))
bind-key))
(defun bind-key (keymap keys bound-value)
"Recursively bind the KEYS to keymaps starting from KEYMAP.
The last key is bound to BOUND-VALUE.
If BOUND-VALUE is nil, the key is unbound.
If KEYS has modifiers that are not allowed in KEYMAP, do nothing.
Return KEYMAP."
(cond
((not (legal-modifiers-p (first keys) keymap))
(error 'bad-modifier
:message (format nil "Keymap ~a only accepts modifiers ~a, got ~a"
keymap
(modifiers keymap)
(key-modifiers (first keys)))))
((= (length keys) 1)
(when (fset:@ (entries keymap) (first keys))
;; TODO: Notify caller properly?
(warn 'override-existing-binding
:existing-binding-value (fset:@ (entries keymap) (first keys))))
(if bound-value
(setf (fset:@ (entries keymap) (first keys)) bound-value)
(setf (entries keymap) (fset:less (entries keymap) (first keys)))))
(t
(let ((submap (fset:@ (entries keymap) (first keys))))
(when (and (not (keymap-p submap))
bound-value)
(setf submap (make-keymap "anonymous"))
(setf (fset:@ (entries keymap) (first keys)) submap))
(bind-key submap (rest keys) bound-value)
(unless bound-value
(when (fset:equal? (fset:empty-map) (entries submap))
(setf (entries keymap) (fset:less (entries keymap) (first keys))))))))
keymap)
(declaim (ftype (function (keymap
(list-of key)
(list-of keymap))
(or keymap t))
lookup-keys-in-keymap))
(defun lookup-keys-in-keymap (keymap keys visited)
"Return bound value or keymap for KEYS.
Return nil when KEYS is not found in KEYMAP.
VISITED is used to detect cycles."
(when keys
(if (legal-modifiers-p (first keys) keymap)
(let ((hit (fset:@ (entries keymap) (first keys))))
(when hit
(cond
((and (keymap-p hit)
(rest keys))
(lookup-key* hit (rest keys) visited))
((and (not (keymap-p hit))
(rest keys))
;; Found a binding instead of a prefix keymap, skip it since it
;; should be shadowed.
;; Example: we loop up "C-x C-f" which is meant to be bound to
;; 'open-file, but "C-x" is bound to 'cut in a parent keymap.
nil)
(t hit))))
(error 'bad-modifier
:message (format nil "Keymap ~a only accepts modifiers ~a, got ~a"
keymap
(modifiers keymap)
(key-modifiers (first keys)))))))
(defun keymap-tree->list (keymaps visited)
"Flatten the KEYMAPS into a list.
It traverses KEYMAPS first, then traverses the each keymap `parent' layer after layer.
Example:
- keymap1 has parents (k1a k1b)
- k1a has parents (k1ap)
- keymap2 has parents (k2a)
Return (keymap1 keymap2 k1a k1b k2a k1ap)."
(when keymaps
(append keymaps
(keymap-tree->list
(delete-if (lambda (keymap)
(when (find keymap visited)
(warn 'cycle :keymap keymap)
t))
(alex:mappend #'parents keymaps))
(append keymaps visited)))))
(declaim (ftype (function ((or keymap (list-of keymap))
(list-of key)
(list-of keymap))
(values (or keymap t) (or keymap null)))
lookup-key*))
(defun lookup-key* (keymap-or-keymaps keys visited)
"Internal function, see `lookup-key' for the user-facing function.
VISITED is used to detect cycles.
As a second value, return the matching keymap."
(let* ((matching-keymap nil)
(result
(some (lambda (keymap)
(if (find keymap visited)
(warn 'cycle :keymap keymap)
(let ((hit (lookup-keys-in-keymap
keymap
keys
(cons keymap visited))))
(when hit
(setf matching-keymap keymap)
hit))))
(keymap-tree->list (uiop:ensure-list keymap-or-keymaps) '()))))
(values result matching-keymap)))
(declaim (ftype (function ((or (list-of key) keyspecs-type)
(or keymap (list-of keymap)))
(values (or keymap t) (or keymap null) (list-of key)))
lookup-key))
(defun lookup-key (keys-or-keyspecs keymap-or-keymaps)
;; We name this user-facing function using the singular form to be consistent
;; with `define-key'.
"Return the value bound to KEYS-OR-KEYSPECS in KEYMAP-OR-KEYMAPS.
As a second value, return the matching keymap.
As a third value, return the possibly translated KEYS.
Return NIL if no value is found.
The successive keymaps from KEYMAP-OR-KEYMAPS (if a list) are looked up one
after the other.
If no binding is found, the direct parents are looked up in the same order.
And so on if the binding is still not found."
(let* ((keys (if (stringp keys-or-keyspecs)
(keyspecs->keys keys-or-keyspecs t)
keys-or-keyspecs))
(matching-keymap nil)
(matching-key nil)
(result
(some (lambda (keys)
(multiple-value-bind (hit hit-keymap)
(lookup-key* keymap-or-keymaps keys '())
(when hit
(setf matching-keymap hit-keymap)
(setf matching-key keys)
hit)))
(cons keys (funcall (or *translator*
(constantly nil))
keys)))))
(values result matching-keymap matching-key)))
(defparameter *print-shortcut* t
"Whether to print the short form of the modifiers.")
(defun string-join (strings separator &key end)
"Adapted from `serapeum:string-join'."
(with-output-to-string (s)
(if strings
(progn
(write-string (string (first strings)) s)
(dolist (string (cdr strings))
(write-string (string separator) s)
(write-string (string string) s))
(when end
(write-string (string separator) s)))
(make-string 0))))
(declaim (ftype (function (key) keyspecs-type) key->keyspec))
(defun key->keyspec (key)
"Return the keyspec of KEY.
If the key has a code, return it prefixed with '#'.
The status is not encoded in the keyspec, but this may change in the future."
(let ((value (if (zerop (key-code key))
(key-value key)
(format nil "#~a" (key-code key))))
(modifiers (fset:reduce (lambda (&rest mods) (string-join mods "-"))
(key-modifiers key)
:key (if *print-shortcut*
#'modifier-shortcut
#'modifier-string))))
(the (values keyspecs-type &optional)
(uiop:strcat (if (uiop:emptyp modifiers) "" (uiop:strcat modifiers "-"))
value))))
(declaim (ftype (function ((list-of key)) keyspecs-type) keys->keyspecs))
(defun keys->keyspecs (keys)
"Return the keyspecs (a list of `keyspec') for KEYS.
See `key->keyspec' for the details."
(the (values keyspecs-type &optional)
(string-join (mapcar #'key->keyspec keys) " ")))
(declaim (ftype (function (keymap &optional (list-of keymap)) fset:map) keymap->map*))
(defun keymap->map* (keymap &optional visited)
"Return a map of (KEYSPEC SYM) from KEYMAP."
(flet ((fold-keymap (result key sym)
(let ((keyspec (key->keyspec key)))
(if (keymap-p sym)
(cond
((find sym visited)
(warn 'cycle :keymap keymap)
result)
(t
(fset:map-union result
(fset:image (lambda (subkey subsym)
(values (format nil "~a ~a" keyspec subkey)
subsym))
(keymap->map* sym (cons sym visited))))))
(fset:with result keyspec sym)))))
(the (values fset:map &optional)
(fset:reduce #'fold-keymap (entries keymap)
:initial-value (fset:empty-map)))))
(declaim (ftype (function (keymap &rest keymap) hash-table) keymap->map))
(defun keymap->map (keymap &rest more-keymaps)
"Return a hash-table of (KEYSPEC BOUND-VALUE) from KEYMAP.
Parent bindings are not listed; see `keymap-with-parents->map' instead.
This is convenient if the caller wants to list all the bindings.
When multiple keymaps are provided, return the union of the `fset:map' of each arguments.
Keymaps are ordered by precedence, highest precedence comes first."
(let ((keymaps (reverse (cons keymap more-keymaps))))
(the (values hash-table &optional)
(fset:convert 'hash-table
(reduce #'fset:map-union
(mapcar #'keymap->map* keymaps))))))
(defun keymap-with-parents (keymap)
"Return the list of keymap and all its parents."
(labels ((list-keymaps (keymap visited)
(cond
((null keymap)
'())
((find keymap visited)
(warn 'cycle :keymap keymap)
'())
(t
(cons keymap
(alex:mappend (alex:rcurry #'list-keymaps (cons keymap visited))
(parents keymap)))))))
(list-keymaps keymap '())))
(declaim (ftype (function (keymap) hash-table) keymap-with-parents->map))
(defun keymap-with-parents->map (keymap)
"List bindings in KEYMAP and all its parents.
See `keymap->map'."
(apply #'keymap->map (keymap-with-parents keymap)))
(declaim (ftype (function (keymap &rest keymap) (or keymap null)) compose))
(defun compose (keymap &rest more-keymaps)
"Return a new keymap that's the composition of all given KEYMAPS.
KEYMAPS are composed by order of precedence, first keymap being the one with
highest precedence."
(flet ((stable-union (list1 list2)
(delete-duplicates (append list1 list2)
:from-end t)))
(let ((keymaps (cons keymap more-keymaps)))
(cond
((uiop:emptyp keymaps)
nil)
((= 1 (length keymaps))
;; Copy the map, don't just refer to it or all modifying the source
;; will alter the target.
(let ((keymap (apply #'make-keymap (name (first keymaps)) (parents (first keymaps)))))
(setf (entries keymap) (fset:convert 'fset:map (entries (first keymaps))))
keymap))
(t
(let* ((keymap1 (first keymaps))
(keymap2 (second keymaps))
(merge (make-keymap (format nil "~a+~a" (name keymap1) (name keymap2)))))
(setf (parents merge) (stable-union (parents keymap1) (parents keymap2)))
(setf (entries merge) (fset:map-union (entries keymap2) (entries keymap1)))
(apply #'compose merge (rest (rest keymaps)))))))))
(declaim (ftype (function (t keymap &key (:test function)) (list-of string)) binding-keys*))
(defun binding-keys* (binding keymap &key (test #'eql))
"Return a the list of `keyspec's bound to BINDING in KEYMAP.
The list is sorted alphabetically to ensure reproducible results.
Comparison against BINDING is done with TEST."
;; This code is a bit redundant with `keymap->map*' but it's necessary to
;; avoid calling key->keyspec thousands of times.
(labels ((scan-keymap (keymap visited)
(let ((result '()))
(fset:do-map (key sym (entries keymap))
(cond
((funcall test binding sym)
(push (list key) result))
((and (keymap-p sym) (find sym visited))
(warn 'cycle :keymap keymap))
((keymap-p sym)
(dolist (hit (scan-keymap sym (cons keymap visited)))
(push (cons key hit) result)))))
result)))
(sort (mapcar #'keys->keyspecs (scan-keymap keymap '())) #'string<)))
(declaim (ftype (function (t (or keymap (list-of keymap)) &key (:test function))
(values list list))
binding-keys))
(defun binding-keys (bound-value keymap-or-keymaps &key (test #'eql))
"Return the list of `keyspec's bound to BINDING in KEYMAP-OR-KEYMAPS.
The order of the result is dictated by the order of KEYMAP-OR-KEYMAPS.
As a second value, return an alist of (keyspec keymap) for all the
`keyspec's bound to BINDING in KEYMAP.
Comparison against BINDING is done with TEST.
Duplicates and shadowed bindings are removed.
For instance, to list all keymaps that have a binding, call:
(mapcar #'second (nth-value 1 (binding-keys ...)))"
(let ((alist (alex:mappend (lambda (keymap)
(let ((hit (binding-keys* bound-value keymap :test test)))
(when hit
(mapcar (alex:rcurry #'list keymap) hit))))
(delete-duplicates
(alex:mappend #'keymap-with-parents
(uiop:ensure-list keymap-or-keymaps))))))
(setf alist (delete-duplicates alist :key #'first :test #'string= :from-end t))
(setf alist (delete-if (lambda (keyspec)
(not
;; TODO: Which comparison operator should we use?
(eq bound-value (lookup-key keyspec keymap-or-keymaps))))
alist :key #'first))
(values
(mapcar #'first alist)
alist)))
(defun pretty-binding-keys (bound-value keymap-or-keymaps &key (test #'eql) (print-style "cua"))
"Print the binding keys in a pretty manner.
This violates the keyspec <-> key functional relationship, and should not be
used for anything but rendering to the user."
(cond ((equal print-style "cua")
(let ((*print-shortcut* t))
(mapcar
(lambda (i)
(str:replace-all
"M-" #-(or darwin win32) "Alt+" #+darwin "Option+" #+win32 "Alt+"
(str:replace-all
"S-" #-(or darwin win32) "Super+" #+darwin "Command+" #+win32 "Win+"
(str:replace-all
"s-" #-(or darwin win32) "Shift+" #+darwin "Shift+" #+win32 "Shift+"
(str:replace-all
"C-" #-(or darwin win32) "Ctrl+" #+darwin "Ctrl+" #+win32 "Ctrl+"
i)))))
(binding-keys bound-value keymap-or-keymaps :test test))))
(t
(binding-keys bound-value keymap-or-keymaps :test test))))