-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user-keys.el
939 lines (825 loc) · 36.7 KB
/
user-keys.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
;;; user-keys.el --- Clean, manage, and inspect your keymaps -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Positron Solutions
;; Author: Psionik K <73710933+psionic-k@users.noreply.github.com>
;; Keywords: convenience
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (keymap-utils "4.0.0"))
;; Homepage: http://github.com/positron-solutions/user-keys
;;; License notice:
;; SPDX-License-Identifier: GPL-3.0-or-later
;; 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 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; User-keys contains tools to diagnose and remove poor key bindings
;; and to free up key sequences that users actually want.
;;; Code:
(require 'derived)
(require 'dash)
(require 'keymap-utils)
(require 'transient)
(require 'loadhist)
(eval-when-compile (require 'subr-x))
;; implementation state and constants
(defvar user-keys-sequence nil "Current sequence being analyzed.")
(defvar user-keys-operation nil "Current analysis operation.")
(defvar user-keys-target-buffer nil "Current buffer for inspecting bindings.")
;; TODO f-keys are not currently part of a predicate
(defconst user-keys--fkey-events
(mapcar
(lambda (c) (make-symbol (concat "f" (int-to-string c))))
(number-sequence 1 20))
"List of function key events, through F20.")
;; TODO stupid events not currently used to create a predicate
(defvar user-keys--stupid-events
'('insert
'pause
'print
'again
'begin
'insertchar
'insertline
'prior)
"Set this to events you can't input on your keyboard.")
;; TODO exception sequences not implemented in stupid keys report
(defvar user-keys--exception-sequences
'([f1] [f11])
"Sequences to except from usual unbinding.
Some key sequences are from desktop environment idioms rather
than traditional Emacs design errors. While these desktop
environments are also mostly wrong, the cost to benefit is in favor
of keeping them bound.")
(defvar user-keys--available-string (propertize "available" 'face 'success)
"Nobody has spammed their keymap ads on this key sequence.")
;; helper functions for custom
(defun user-keys-meta-ize (key-letters)
"Return key-sequence encoded forms for meta modified KEY-LETTERS.
KEY-LETERS should be a list of chars or integers."
(--map (vconcat (kbd (format "M-%c" it))) key-letters))
(defun user-keys-ctl-fy (key-letters)
"Return key-sequence encoded forms for ctl modified KEY-LETTERS.
KEY-LETERS should be a list of chars or integers."
(--map (vconcat (kbd (format "C-%c" it))) key-letters))
;; customize
(defgroup user-keys nil "Clean up, inspect, manage keymaps."
:group 'convenience)
(defcustom user-keys-buffer-name "*user-keys*"
"Where should we display interactive output?"
:type 'string
:group 'user-keys)
(defcustom user-keys-stupid-modifiers
'(hyper super)
"Sequences should almost never require these modifiers.
Valid modifiers are in A-C-H-M-S-s order. Use `kbd', `vconcat' or
`string-to-vector' and `event-modifiers' to translate Emacs notation to actual
events and modifiers."
:type '(repeat (repeat symbol))
:group 'user-keys)
(defcustom user-keys-shifted-keys
(string-to-list "~!@#$%^&*()_+{}|:\"<>?")
"Keys that have an implied shift modifier."
:type '(repeat 'character)
:group 'user-keys)
(defcustom user-keys-preferred-sequences
(let ((home-row (string-to-list "asdfjkl;"))
(other-meta (string-to-list "qwertyuiop[]\\gh'zxcvbnm,./"))
;; C-m is RET, C-i is TAB...
;; Control is just a good preferred modifier...
(other-ctl (string-to-list "qwertyuop\\gh'zxcvbn,./")))
`((,(propertize "home meta keys\n" 'face 'success)
,(user-keys-meta-ize home-row))
(,(propertize "home ctl keys\n" 'face 'font-lock-builtin-face)
,(user-keys-ctl-fy home-row))
(,(propertize "meta keys\n" 'face 'font-lock-keyword-face)
,(user-keys-meta-ize other-meta))
(,(propertize "ctl keys\n" 'face 'font-lock-string-face)
,(user-keys-ctl-fy other-ctl))))
"Sequences to look report in `user-keys-report-preferred'.
Set to an expression or function that returns a list of elements.
Each element is a string and a list of key sequence vectors or a
function that will return such a list.
The string will be used to generate the match reasons, which are
shown beside each match. There can be overlap in the sequences,
in which case all match reasons will be shown. Sequences are
sorted before display.
By default, some top-level single-modifier keys on an English US
keyboard are considered preferred sequences. Home row and non-home
row are divided into two groups.
You are welcome to contribute a function that will respect the
user's keyboard layout and language settings.
Here's an example expression that just has two sections,
'((\"control keys\"
,(--map (vector it (number-sequence ?\C-a ?\C-z))))
(\"home left meta keys\"
'([134217825] [134217843] [134217828] [134217830])))
Key sequences have a lot of variety. Too much. Use `kbd' and
`vconcat' in ielm to output sequences and `key-description' to see
what the outputs mean in Emacs style key sequence notation."
:type '(repeat (list string (choice function (list key-sequence))))
:group 'user-keys)
(defcustom user-keys-ignore-maps '(yank-menu
xterm-function-map
key-translation-map
function-key-map)
"Some maps are simultaneously very weird and not very useful.
In particular, maps that cause errors because of, for example,
failing the `keymapp' test after autoloaded but not before,
should just be ignored."
:type '(repeat symbol)
:group 'user-keys)
;; implementation functions
(defun user-keys--get-buffer ()
"Obtain the report buffer."
(let ((buffer (get-buffer user-keys-buffer-name)))
(or buffer
(let ((buffer (get-buffer-create user-keys-buffer-name)))
(set-buffer buffer)
(user-keys-mode)
buffer))))
;; TODO tests for this macro
(defmacro user-keys--with-buffer (&rest body)
"Wrap the evaluation of BODY forms with buffer handling.
We don't want to get read-only at the wrong time or execute in the
wrong buffer's context."
`(progn
(set-buffer (user-keys--get-buffer))
(setq buffer-read-only nil)
(condition-case form-value
(progn ,@body)
((debug error)
(setq buffer-read-only t)
;; propagate errors from BODY
(signal (car form-value) (cadr form-value)))
(:success
(setq buffer-read-only t)
form-value))))
(defun user-keys--pop-to-buffer ()
"Pop to the reporting buffer."
(pop-to-buffer (user-keys--get-buffer)))
(defsubst user-keys--maybe-button (text-or-symbol)
"Format TEXT-OR-SYMBOL as a button that displays its documentation."
(if (symbolp text-or-symbol)
(propertize (symbol-name text-or-symbol)
'face 'button
'button '(t)
'category 'default-button)
text-or-symbol))
;; TODO make click actions appropriate for types. Keys should show shadows.
;; Maps should show preferred and stupid. Modes should show active maps etc.
(defsubst user-keys--section-header (text-or-symbol indentation)
"Make a dividing section header for TEXT-OR-SYMBOL.
Indent by INDENTATION. If not indented, add an underline since
this is a proper section. Make the symbol a button if it
probably should be clickable."
(let ((header-text (user-keys--maybe-button text-or-symbol)))
(if (> indentation 0)
(format "\n%s%s\n" (make-string indentation ?\s) header-text)
(format "\n%s\n%s\n\n" header-text
(make-string (length header-text) ?\-)))))
(defun user-keys--describe-binding (binding)
"Return a formatted representation of what the BINDING for a key is.
This function handles the numerous possible types of values
that can be returned from `key-binding' and during
`kmu-map-keymap'."
(cond
((eq nil binding)
nil)
((or (symbolp binding)
(stringp binding))
(user-keys--maybe-button binding))
((keymapp binding)
"<keymap>")
((vectorp binding) ; maps to another sequence
(key-description binding))
((numberp binding) ; sequence too long
(format "sequence %s keys too long for prefix" binding))
((kmu-menu-binding-p binding)
(format "menu-item - %s"
;; Some menus are... weird. Because.
(if (proper-list-p binding)
(nth 2 binding)
(if (consp binding)
(car binding)
"<idk>"))))
;; TODO we can sometimes get the name out.
;; This was originally intended to fix up a weird menu item.
((functionp binding)
"<function>")
(t
(let* ((str-binding (format "%S" binding)))
(if (> (length str-binding) 20)
(format "Bound to: %.20s..." str-binding)
(format "Bound to: %s" str-binding))))))
(defun user-keys--major-mode-keymaps ()
"Return a list of major modes map symbols."
(let* ((major-mode-keymaps '())
(_ (mapatoms
(lambda (a) (when (get a 'derived-mode-parent)
(let ((mode-map-name (derived-mode-map-name a)))
(with-demoted-errors "user-keys major mode lookup: %S"
(when (keymapp
(user-keys--symbol-to-map mode-map-name))
(push mode-map-name major-mode-keymaps)))))))))
major-mode-keymaps))
(defun user-keys--minor-mode-keymaps (&optional active-only)
"Return a list of all minor mode keymaps.
Optional ACTIVE-ONLY argument will control if only active maps
are returned."
(-non-nil
(--map (let* ((mode-name (car it))
(mode-map-name (derived-mode-map-name mode-name)))
(if (and (or (not active-only) (symbol-value mode-name))
(keymapp (user-keys--symbol-to-map mode-map-name)))
mode-map-name
nil))
minor-mode-map-alist)))
(defun user-keys--emulation-keymaps (&optional active-only)
"Return a list of all minor mode keymaps.
Optional ACTIVE-ONLY argument will control if only active maps
are returned."
;; TODO go find how to scan these in Ikaruga
(-map emulation-mode-map-alists))
(defun user-keys--other-maps(keymaps)
"Return a list of all keymaps not in KEYMAPS.
KEYMAP-LISTS is a list of lists of map symbols."
(let ((other-keymaps (make-hash-table :test 'eq))
(known-keymaps (make-hash-table :test 'eq)))
(mapc
(lambda (map)
(puthash map map known-keymaps))
keymaps)
;; TODO keymapp on symbols that have symbol-function is annoying
;; See `user-keys-symbol-to-map' for the reverse operation.
(mapatoms (lambda (a) (when (or (keymapp a)
(and (boundp a) (keymapp (symbol-value a)))
(and (fboundp a) (keymapp (symbol-function a))))
(unless (or (indirect-function a) ; obsolete symbol
(gethash a known-keymaps))
(puthash a a other-keymaps)))))
(--remove (member it user-keys-ignore-maps) (hash-table-keys other-keymaps))))
(defun user-keys--maps-to-symbols (maps symbols)
"Find which SYMBOLS refer to keymaps in MAPS.
Also return any maps that didn't match.
This is useful when trying to reconstruct outputs of
`current-active-maps'.
It would be nice to get a list of these objects as symbols in
order for inferencing keymaps. This is the hard way."
(let ((found)
;; warns on mapcar, just some dash noise
(maps (--remove (equal it '(keymap)) maps)))
(--map
(let ((m (if (boundp it)
(symbol-value it)
(symbol-function it))))
(when (seq-contains-p maps m #'eq) ; only match objects
(setq maps (delete m maps))
(push it found)))
symbols)
`(,found ,maps)))
(defun user-keys--symbol-to-map (symbol)
"Get an actual keymap for SYMBOL.
`keymapp' will return t for all kinds of values. Keymaps,
autoloads, symbols whose function definitions are keymaps. This
function encapsulates these little quirks."
(condition-case
keymap-error
(progn
(let ((value (or (and (boundp symbol) (symbol-value symbol))
(and (fboundp symbol) (symbol-function symbol)))))
;; Go ahead and eagerly load all keymaps so that we can inspect them.
;; Sorry, user memory.
(when (autoloadp value)
(autoload-do-load value)
(setq value (or (and (boundp symbol) (symbol-value symbol))
(and (fboundp symbol)(symbol-function symbol)))))
(unless (keymapp value)
(error "Values was not a keymap: %s" symbol))
value))
(error (message "No keymap could be obtained from symbol: %s" symbol))))
(defun user-keys--symbol-to-feature (symbol &optional ask prompt)
"Return the feature that will define SYMBOL.
If ASK is non-nil, ask the user, with optional PROMPT. This
function attempts definitive answers first before using a
heuristic approach and then finally asks the user to handle
degenerate cases."
(or (when (featurep symbol) symbol)
(alist-get 'provide (assoc-string (symbol-file symbol) load-history))
(let ((found) ; attempt to find longest matching feature name.
(words (string-split (symbol-name symbol) "-")))
(while (and words (not found))
(setq words (-butlast words))
(setq found (locate-library (string-join words "-"))))
(when words (intern (string-join words "-")))) ; could create new symbols
(when ask
(read-feature
(or prompt
"Feature loading inference failed. Please select correct feature:")))))
(defsubst user-keys--normalize-sequence (sequence)
"Round trip the SEQUENCE to eliminate common prefix effect.
Within a keymap, having a common prefix seems to result in sequences
with multiple keys even though the simple `kbd' result is just one key."
(let ((reconverted (kbd (key-description sequence))))
(if (stringp reconverted) (vconcat reconverted)
reconverted)))
(defsubst user-keys--maybe-unroll (sequence)
"Unrolls SEQUENCE if it is a range, returning first sequence."
(vconcat (--map
(if (and (consp it) (atom (cdr it)))
(car it)
it)
sequence)))
(defun user-keys--remove-mouse-mods (modifiers)
"Remove mouse modifiers from MODIFIERS."
(--remove (member it '(click drag down)) modifiers))
(defun user-keys--find (keymap seq-predicates &optional exclude-predicates)
"Find all sequences in KEYMAP matched by SEQ-PREDICATES.
Do not attempt to match sequences that are excluded by
EXCLUDE-PREDICATES.
Each predicate is called with SEQUENCE and DEFINITION.
The only potential modification to SEQUENCE before evaluating
predicates is that some sequences representing ranges will be
unrolled into just the first sequence in the range. This means
sequences like `C-0..C-9' will instead be called with just `C-0' and
the result will apply to every key in the range. You can exclude
ranges in EXCLUDE-PREDICATES when this creates a problem.
Within each predicate, you may need to map over the individual keys in
the sequence. If you need modifiers for a key, call
`event-modifiers'. For the basic key or event, call
`event-basic-type'. These functions return a list of modifiers and a
list of basic events respectively.
Each predicate can return a reason, and multiple reasons will be
output into the buffer during analysis of bad keys. This is for
easier design and debugging of rules."
(let (matches match-excludes)
(kmu-map-keymap
(lambda (sequence definition)
(let ((orig-sequence sequence)
(sequence
(condition-case error
(user-keys--normalize-sequence
(user-keys--maybe-unroll sequence))
(error
(message "user-keys: sequence processed raw: %s" sequence)
sequence)))
(exclusions (-non-nil (--map
(funcall it sequence definition)
exclude-predicates))))
(if exclusions
(push (list orig-sequence definition exclusions) match-excludes)
(when-let ((reasons (-non-nil (--map (funcall it sequence definition)
seq-predicates))))
(push (list orig-sequence definition reasons) matches)))))
keymap)
(list matches match-excludes)))
(defun user-keys--render-report (report)
"Generic output function for similar-ishly structured reports.
REPORT is a plist.
:title - This header is rendered first. Any string supported.
:data - A list of elements. Each element is a plist with :header
:col-labels and :rows.
Each :data element will be rendered as a section and formatted so
that :col-labels, if present, will be printed and aligned above
data in :rows."
(user-keys--with-buffer
(erase-buffer)
;; TODO title is less prominent than section headers
(insert (plist-get report :title) "\n")
(user-keys--insert-rows (plist-get report :data) 0)
(goto-char 0)))
(defun user-keys--insert-rows (rows indentation)
"Recursively insert ROWS into the buffer.
Begin each row with INDENTATION spaces. Descend if ROWS is a
plist containing :header, then print :rows. This can handle
recursive plists."
(when rows ; TODO filter empty sections during generation
(let ((header (plist-get rows :header)))
(when header
(insert (user-keys--section-header header indentation))))
(let* ((rows (or (plist-get rows :rows) rows))
(recursive (plist-get (car rows) :rows)))
(if recursive
;; if each row contains embedded rows, recurse into each
;; iteration. If not, just render each row.
(--each rows (user-keys--insert-rows it (+ 2 indentation)))
;; TODO add col-labels to the header row after the widths
;; are known, just before inserting rows. Probably
;; requires backup up in the buffer and popping a mark.
;; column information needs to be extracted prior to
;; rendering any rows.
(let* ((ncols (-max (-map #'length rows)))
(widths (-map
(lambda (n)
(1+ (-max (--map
;; TODO munge this beforehand so we don't
;; ever need to convert
(let ((item (nth n it)))
(if (stringp item)
(length item)
(length (format "%s" item))))
rows))))
(number-sequence 0 (1- ncols)))))
(--each rows
(apply #'insert
`(,(make-string indentation ?\s)
,@(-interpose
" "
(--map-indexed
(let* ((width (nth it-index widths))
(str (if (stringp it) it (format "%s" it)))
(short (- width (length str))))
(concat str (make-string short ?\s)))
it))
"\n"))))))))
(defun user-keys-report-preferred ()
"Show each of the user's preferred sequences in the current buffer."
(interactive)
;; expand the preferred sequences if they contained functions
;; TODO test with functions
(let* ((preferred (--map
(let ((seqs (cadr it)))
`(,(car it) ,(if (functionp seqs)
(funcall seqs)
seqs)))
user-keys-preferred-sequences))
;; First we want to see what bindings will be calculated.
;; This lookup doesn't tell us which map or why, but it does
;; tell us what the result will be.
(target-buffer (or user-keys-target-buffer
(current-buffer)))
(local-lookups
(with-current-buffer target-buffer
(--map
(let* ((sequences (cadr it))
(lookups
(--map
(list
(key-description it)
(or (when-let ((description (key-binding it t)))
(user-keys--describe-binding description))
user-keys--available-string))
sequences)))
(list :header (car it)
:rows lookups))
preferred)))
;; TODO this section was being written to work on active
;; scan active maps with predicates, combining results with
;; map data to augment the key-binding pairs earlier
;; maps. It's not clear what the use case is or how it
;; should fit with other use cases. I left this section
;; commented in case someone wants to play around.
;; (predicates (--map (user-keys-sequences-predicate
;; (cadr it) (car it))
;; preferred))
;; (active-map-symbols
;; (let ((results (user-keys--maps-to-symbols
;; (current-active-maps) (user-keys--other-maps '()))))
;; (when (cadr results)
;; ;; this shouldn't ruin anyone's day, but it is weird.
;; (message "user-keys: Some maps could not be resolved to symbols."))
;; (car results)))
;; ;; then report on active maps (which may save time by revealing shadows)
;; (scanned-lookups (--map
;; (list :header it
;; :rows (user-keys--find it predicates))
;; active-map-symbols))
(report `(:title ,(format "Preferred Sequences in: %s" target-buffer)
:data ,local-lookups)))
(user-keys--render-report report)))
(defun user-keys-report-shadows (sequence)
"Show all keymaps that potentially could shadow SEQUENCE."
(interactive (list (or user-keys-sequence
(call-interactively
#'user-keys-set-sequence-key))))
(let* ((basic-mode-maps '(special-mode-map
text-mode-map
prog-mode-map))
;; `override-global-map' appears as an emulation map.
(major-mode-maps (user-keys--major-mode-keymaps))
(minor-mode-maps (user-keys--minor-mode-keymaps))
(most-maps (append '(global-map)
basic-mode-maps
major-mode-maps
minor-mode-maps))
(other-maps (user-keys--other-maps most-maps))
;; `keymap-lookup' uses string input. `lookup-key' doc string
;; says to prefer `keymap-lookup'.
(key-str (key-description sequence))
(lookups (->>
`(,'(global-map)
,basic-mode-maps
,major-mode-maps
,minor-mode-maps
,other-maps)
(--map (-sort #'string< it))
(--map ; it is a list of maps
(-non-nil
(--map ; it is a single map symbol
(let ((binding (with-demoted-errors
"couldn't get keymap for symbol %s"
(keymap-lookup
(user-keys--symbol-to-map it)
key-str))))
(when binding
(list it (user-keys--describe-binding binding))))
it)))))
(sections '("Global Map"
"Basic Mode Maps"
"Major Mode Maps"
"Minor Mode Maps"
;; TODO support overriding maps,
;; support showing maps in lookup order.
"Other Maps"))
(data (->>
(-zip sections lookups)
(--map (when (cdr it) (list
:header (car it)
:rows (cdr it))))
(-non-nil)))
(report `(:title
,(format
"Shadows for %s"
(propertize key-str 'face 'success))
:data ,data)))
(user-keys--render-report report)))
(defun user-keys-report-stupid ()
"Show all of the stupid key sequences that are currently bound."
(interactive)
;; scan all keymaps and present each one as a section.
;; TODO it may be more user friendly to look at a single keymap or
;; buffer to see the preferred and stupid bindings.
(let* ((basic-mode-maps '(special-mode-map
text-mode-map
prog-mode-map))
;; `override-global-map' appears as an emulation map.
(major-mode-maps (user-keys--major-mode-keymaps))
(minor-mode-maps (user-keys--minor-mode-keymaps))
(most-maps (append '(global-map)
basic-mode-maps
major-mode-maps
minor-mode-maps))
(other-maps (user-keys--other-maps most-maps))
(predicates user-keys-stupid-predicates)
;; Scan each map with predicates and amend results by
;; appending the map name to each value.
(lookups (->>
`((global-map)
,basic-mode-maps
,major-mode-maps
,minor-mode-maps
,other-maps)
(--map (-sort #'string< it))
(--map ; it is a list of map symbols
(-non-nil
(--map ; it is a single map symbol
(if-let ((map (user-keys--symbol-to-map it)))
(condition-case error
(when-let ((scanned (car (user-keys--find map predicates)))
(display
(--map
(list
(key-description (nth 0 it))
(user-keys--describe-binding (nth 1 it))
(mapconcat #'identity (nth 2 it) ", "))
scanned)))
(when display (list :header it
:rows display)))
(error (warn "Keymap scan failed: %s" it)))
(warn "Keymap could not be scanned: %s" it))
it)))))
(sections '("Global Map"
"Basic Mode Maps"
"Major Mode Maps"
"Minor Mode Maps"
"Other Maps"))
(data (->>
(-zip sections lookups)
(--map (when (cdr it) (list
:header (car it)
:rows (cdr it))))
(-non-nil)))
(report `(:title "Stupid Keys - bindings that should just not"
:data ,data)))
(user-keys--render-report report)))
(defun user-keys-generate-unbinds (output-type)
"Generate an unbinding expression for OUTPUT-TYPE."
(interactive)
(undefined))
;; functions useful for those extending user-keys, part of external API
(defun user-keys-key-predicate (key reason)
"Return a predicate that will match KEY.
The REASON will be returned for reporters."
(lambda (sequence _)
(when (seq-contains-p sequence key)
reason)))
(defun user-keys-basic-events-predicate (basic-events reason)
"Return predicate matching sequences containing any of BASIC-EVENTS.
The REASON will be returned for reporters.
Basic events are a list, and for mouse events for example, can contain
multiple elements."
(lambda (sequence _)
(when (-non-nil
(--map (seq-contains-p basic-events (event-basic-type it))
sequence))
reason)))
(defun user-keys-sequences-predicate (sequences reason)
"Return a predicate that matches any sequence from SEQUENCES.
The REASON will be returned for reporters."
(lambda (sequence _)
(message "sequence: %s" sequence)
(when (member sequence sequences) reason)))
(defun user-keys-multiple-modifiers-predicate (reason)
"Return predicate matching keys with multiple modifiers.
The REASON will be returned for reporters."
(lambda (sequence _)
(when (-non-nil
(--map
(> (length (user-keys--remove-mouse-mods
(event-modifiers it)))
1)
sequence))
reason)))
(defun user-keys-modifiers-predicate (modifiers reason)
"Return predicate matching keys with one of MODIFIERS.
The REASON will be returned for reporters."
(lambda (sequence _)
(when (-non-nil
(--map
(when-let ((event-mods (user-keys--remove-mouse-mods
(event-modifiers it))))
(-non-nil (--map (member it modifiers) event-mods)))
sequence))
reason)))
(defun user-keys-modified-basic-events-predicate (basic-events reason)
"Return a predicate matching any modified use of BASIC-EVENTS.
The REASON will be returned for reporters."
(lambda (sequence _)
(when (-non-nil
(--map (and (event-modifiers it)
(seq-contains-p basic-events (event-basic-type it)))
sequence))
reason)))
(defun user-keys-one-mod-events-predicate (modifiers
basic-events reason)
"Return a predicate matching single MODIFIERS and BASIC-EVENTS.
This is useful for looking at top-level, simply modified sequences.
The REASON will be returned for reporters."
(lambda (sequence _)
(when (-non-nil
(--map
(let ((sequence-mods (event-modifiers it)))
(and (= (length sequence-mods) 1)
(seq-contains-p modifiers
(car sequence-mods))
(seq-contains-p basic-events
(event-basic-type it))))
sequence))
reason)))
(defun user-keys-commands-predicate (commands reason)
"Return a predicate matching COMMANDS.
The REASON will be returned for reporters."
(lambda (_ description)
(message "description %s" description)
(when (member description commands)
reason)))
;; TODO add exceptions
;; Had to move this after predicates for loading order
(defcustom user-keys-stupid-predicates
(list (user-keys-multiple-modifiers-predicate
"multiple modifiers")
(user-keys-modified-basic-events-predicate
user-keys--fkey-events "modified function keys")
(user-keys-modifiers-predicate user-keys-stupid-modifiers
"difficult modifiers")
(user-keys-modified-basic-events-predicate
user-keys-shifted-keys "modified shift keys"))
"Predicates used to report stupid bindings."
:type '(repeat function)
:group 'user-keys)
;; user commands and modes
(defun user-keys--describe-current-sequence ()
"Format's the current sequence for display as a group description."
(format "Current key: %s" (if user-keys-sequence
(propertize (key-description
user-keys-sequence)
'face 'success)
(propertize "none" 'face 'shadow))))
(defun user-keys--describe-target-buffer ()
"Format's the current buffer for display as a group description."
(format "Target buffer: %s" (if user-keys-target-buffer
(propertize
(buffer-name (get-buffer
user-keys-target-buffer))
'face 'success)
(propertize "none" 'face 'shadow))))
(defun user-keys-set-target-buffer (buffer)
"Set the BUFFER used for inspecting bindings.
See `user-keys-target-buffer'."
(interactive "bTarget buffer for binding inspection: ")
(setq user-keys-target-buffer buffer))
(defun user-keys-set-sequence-key (key)
"Set `user-keys-sequence' by inputting just one KEY.
The key does not need to be bound in any active maps."
(interactive (list (read-key "Enter a key: ")))
(setq user-keys-sequence (vector key)))
(defun user-keys-set-sequence (sequence)
"Set the SEQUENCE to analyze for buffer or mode maps.
The sequence needs to be bound. Incomplete sequences will
continue reading. TODO This seems to behave differently when
called within transient versus directly. The transient menu has
different maps active."
(interactive (list (read-key-sequence-vector "Bound key sequence: ")))
(setq user-keys-sequence sequence)
;; there's no feedback when a sequence terminates
(message "Sequence set!"))
(defun user-keys-set-sequence-string (sequence)
"Set the SEQUENCE, but use a string input.
This can be useful when `read-key-sequence' will not terminate input
because the input in the active maps is still a prefix."
(interactive
(list (let (sequence)
(while (not sequence)
(let ((input (read-string "Key description (for `kbd'): ")))
(if (with-demoted-errors
"`kbd' failed for input: %s"
(kbd input))
(setq sequence (kbd input))
(message "Input must be valid argument to call `kbd' function.")
(sit-for 3))))
sequence)))
(setq user-keys-sequence sequence))
(defun user-keys-refresh ()
"Refresh the results buffer."
(interactive)
(undefined))
;;;###autoload
(transient-define-prefix user-keys-dispatch ()
"Controls for user-keys package."
[["Generate Report"
("s" "sequence shadows report" user-keys-report-shadows)
("p" "preferred sequences report" user-keys-report-preferred)
("t" "stupid sequences report" user-keys-report-stupid)
("T" "unbinds" user-keys-generate-unbinds)]
["Controls"
("h" "toggle menu" transient-quit-one)
("g" "refresh" user-keys-refresh)]]
["Options"
[:description
user-keys--describe-current-sequence
""
("k" "set sequence (key)" user-keys-set-sequence-key :transient t)
;; setting the sequence with `kbd' doesn't depend on active maps.
("S" "set sequence (string)" user-keys-set-sequence-string
:transient t)
("K" "set sequence" user-keys-set-sequence :transient t)]
[:description
user-keys--describe-target-buffer
""
("b" "active buffer" user-keys-set-target-buffer :transient t)]])
(declare-function helpful-at-point "helpful" ())
(defun user-keys--push-button ()
"Open help for symbol at point.
Use `helpful' package if loaded."
(interactive)
(if (featurep 'helpful)
(helpful-at-point)
(describe-symbol (symbol-at-point))))
(defvar user-keys-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent
map
(make-composed-keymap '(button-buffer-map special-mode-map)))
(define-key map "g" 'user-keys-refresh)
(define-key map "h" 'user-keys-dispatch)
(define-key map "?" 'user-keys-dispatch)
(define-key map "k" 'user-keys-set-sequence-key)
(define-key map "S" 'user-keys-set-sequence-string)
(define-key map "K" 'user-keys-set-sequence)
(define-key map "b" 'user-keys-set-target-buffer)
(define-key map "s" 'user-keys-report-shadows)
(define-key map "p" 'user-keys-report-preferred)
(define-key map "t" 'user-keys-report-stupid)
(define-key map "T" 'user-keys-report-unbinds)
;; TODO different kinds of values could exist. Function
;; keymaps are an example.
(define-key map [remap push-button] #'user-keys--push-button)
map))
(define-derived-mode user-keys-mode special-mode
"user-keys-mode"
:interactive nil
:group 'user-keys
(use-local-map user-keys-mode-map))
;;;###autoload
(defun user-keys-start ()
"An application to inspect bindings and generate re-bindings."
(interactive)
(pop-to-buffer (user-keys--get-buffer))
(call-interactively #'user-keys-dispatch))
(provide 'user-keys)
;;; user-keys.el ends here