-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhich-key.el
2809 lines (2536 loc) · 115 KB
/
which-key.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
;;; which-key.el --- Display available keybindings in popup -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2024 Free Software Foundation, Inc.
;; Author: Justin Burkett <justin@burkett.cc>
;; Maintainer: Justin Burkett <justin@burkett.cc>
;; Version: 3.6.0
;; Package-Requires: ((emacs "25.1"))
;; This file is part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The `which-key' mode displays the key bindings following your
;; currently entered incomplete command (a prefix) in a popup. For
;; example, after enabling the minor mode if you enter C-x and wait for
;; the default of 1 second the minibuffer will expand with all of the
;; available key bindings that follow C-x (or as many as space allows
;; given your settings).
;;
;; This includes prefixes like C-x 8 which are shown in a different
;; face.
;;; Code:
(require 'cl-lib)
(require 'button)
(require 'regexp-opt)
;; For compiler
(defvar evil-operator-shortcut-map)
(defvar evil-operator-state-map)
(defvar evil-motion-state-map)
(defvar golden-ratio-mode)
(declare-function evil-get-command-property "ext:evil-common.el")
;;; Options
(defgroup which-key nil
"Customization options for `which-key-mode'."
:group 'help
:prefix "which-key-")
(defcustom which-key-idle-delay 1.0
"Delay (in seconds) for which-key buffer to popup.
This variable should be set before activating `which-key-mode'.
A value of zero might lead to issues, so a non-zero value is
recommended
(see https://github.com/justbur/emacs-which-key/issues/134)."
:type 'float
:package-version "1.0" :version "30.1")
(defcustom which-key-idle-secondary-delay nil
"Seconds to wait for which-key to pop up after initial display.
This makes it possible to shorten the delay for subsequent popups
in the same key sequence. The default is for this value to be
nil, which disables this behavior."
:type '(choice float (const :tag "Disabled" nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-echo-keystrokes (if (and echo-keystrokes
(> (+ echo-keystrokes 0.01)
which-key-idle-delay))
(/ (float which-key-idle-delay) 4)
echo-keystrokes)
"Value to use for `echo-keystrokes'.
This only applies if `which-key-popup-type' is minibuffer or
`which-key-show-prefix' is echo. It needs to be less than
`which-key-idle-delay' or else the keystroke echo will erase the
which-key popup."
:type 'float
:package-version "1.0" :version "30.1")
(defcustom which-key-max-description-length 27
"Truncate the description of keys to this length.
Either nil (no truncation), an integer (truncate after that many
characters), a float (use that fraction of the available width),
or a function, which takes one argument, the available width in
characters, and whose return value has one of the types mentioned
before. Truncation is done using `which-key-ellipsis'."
:type '(choice (const :tag "Disable truncation" nil)
(integer :tag "Width in characters")
(float :tag "Use fraction of available width")
function)
:package-version "1.0" :version "30.1")
(defcustom which-key-min-column-description-width 0
"Every column should at least have this width."
:type 'natnum
:package-version "1.0" :version "30.1")
(defcustom which-key-add-column-padding 0
"Additional spaces to add to the left of each key column."
:type 'integer
:package-version "1.0" :version "30.1")
(defcustom which-key-unicode-correction 3
"Correction for wide unicode characters.
Since we measure width in terms of the number of characters,
Unicode characters that are wider than ASCII characters throw off
the calculation for available width in the which-key buffer. This
variable allows you to adjust for the wide unicode characters by
artificially reducing the available width in the buffer.
The default of 3 means allow for the total extra width
contributed by any wide unicode characters to be up to one
additional ASCII character in the which-key buffer. Increase this
number if you are seeing characters get cutoff on the right side
of the which-key popup."
:type 'integer
:package-version "1.0" :version "30.1")
(defcustom which-key-dont-use-unicode t
"If non-nil, don't use any unicode characters in default setup.
For affected settings, see `which-key-replacement-alist', `which-key-ellipsis'
`which-key-separator'."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-separator
(if which-key-dont-use-unicode " : " " → ")
"Separator to use between key and description.
Default is \" → \", unless `which-key-dont-use-unicode' is non
nil, in which case the default is \" : \"."
:type 'string
:set-after '(which-key-dont-use-unicode)
:package-version "1.0" :version "30.1")
(defcustom which-key-ellipsis
(if which-key-dont-use-unicode ".." "…")
"Ellipsis to use when truncating.
Default is \"…\", unless `which-key-dont-use-unicode' is non nil,
in which case the default is \"..\". This can also be the empty
string to truncate without using any ellipsis."
:type 'string
:set-after '(which-key-dont-use-unicode)
:package-version "1.0" :version "30.1")
(defcustom which-key-prefix-prefix "+"
"Prefix string to indicate a key bound to a keymap.
Default is \"+\"."
:type 'string
:package-version "1.0" :version "30.1")
(defcustom which-key-compute-remaps nil
"If non-nil, show remapped commands.
This applies to commands that have been remapped given the
currently active keymaps."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-replacement-alist
`(((nil . "which-key-show-next-page-no-cycle") . (nil . "wk next pg"))
,@(unless which-key-dont-use-unicode
'((("<left>") . ("←"))
(("<right>") . ("→"))))
(("<\\([[:alnum:]-]+\\)>") . ("\\1")))
"ALIST for manipulating display of binding descriptions.
Each element of the list is a nested cons cell with the format
\(MATCH CONS . REPLACEMENT\).
The MATCH CONS determines when a replacement should occur and
REPLACEMENT determines how the replacement should occur. Each may
have the format \(KEY REGEXP . BINDING REGEXP\). For the
replacement to apply the key binding must match both the KEY
REGEXP and the BINDING REGEXP. A value of nil in either position
can be used to match every possibility. The replacement is
performed by using `replace-regexp-in-string' on the KEY REGEXP
from the MATCH CONS and REPLACEMENT when it is a cons cell, and
then similarly for the BINDING REGEXP. A nil value in the BINDING
REGEXP position cancels the replacement. For example, the entry
\(\(nil . \"Prefix Command\"\) . \(nil . \"prefix\"\)\)
matches any binding with the descriptions \"Prefix Command\" and
replaces the description with \"prefix\", ignoring the
corresponding key.
REPLACEMENT may also be a function taking a cons cell
\(KEY . BINDING\) and producing a new corresponding cons cell.
If REPLACEMENT is anything other than a cons cell \(and non nil\)
the key binding is ignored by which-key.
Finally, you can multiple replacements to occur for a given key
binding by setting `which-key-allow-multiple-replacements' to a
non-nil value."
:type '(alist :key-type (cons (choice regexp (const nil))
(choice regexp (const nil)))
:value-type (cons (choice string (const nil))
(choice string (const nil))))
:package-version "1.0" :version "30.1")
(defcustom which-key-allow-multiple-replacements nil
"Allow a key binding to be modified by multiple elements.
When non-nil, this allows a single key binding to match multiple
patterns in `which-key-replacement-alist'. When nil, only the
first match is used to perform replacements from
`which-key-replacement-alist'."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-show-docstrings nil
"If non-nil, show each command's docstring in the which-key popup.
This will only display the docstring up to the first line
break. If you set this variable to the symbol docstring-only,
then the command's name with be omitted. You probably also want
to adjust `which-key-max-description-length' at the same time if
you use this feature."
:type '(radio
(const :tag "Do not show docstrings" nil)
(const :tag "Add docstring to command names" t)
(const :tag "Replace command name with docstring" docstring-only))
:package-version "1.0" :version "30.1")
(defcustom which-key-highlighted-command-list '()
"Rules used to highlight certain commands.
If the element is a string, assume it is a regexp pattern for
matching command names and use
`which-key-highlighted-command-face' for any matching names. If
the element is a cons cell, it should take the form (regexp .
face to apply)."
:type '(repeat (choice string (cons regexp face)))
:package-version "1.0" :version "30.1")
(defcustom which-key-special-keys '()
"These keys will automatically be truncated to one character.
They also have `which-key-special-key-face' applied to them. This
is disabled by default. An example configuration is
\(setq which-key-special-keys \\='(\"SPC\" \"TAB\" \"RET\" \"ESC\" \"DEL\")\)"
:type '(repeat string)
:package-version "1.0" :version "30.1")
(defcustom which-key-buffer-name " *which-key*"
"Name of which-key buffer."
:type 'string
:package-version "1.0" :version "30.1")
(defcustom which-key-show-prefix 'echo
"Whether to and where to display the current prefix sequence.
Possible choices are echo for echo area (the default), left, top
and nil. nil turns the feature off."
:type '(radio (const :tag "Left of the keys" left)
(const :tag "In the first line" top)
(const :tag "In the last line" bottom)
(const :tag "In the echo area" echo)
(const :tag "In the mode-line" mode-line)
(const :tag "Hide" nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-popup-type 'side-window
"Supported types are minibuffer, side-window, frame, and custom."
:type '(radio (const :tag "Show in minibuffer" minibuffer)
(const :tag "Show in side window" side-window)
(const :tag "Show in popup frame" frame)
(const :tag "Use your custom display functions" custom))
:package-version "1.0" :version "30.1")
(defcustom which-key-min-display-lines 1
"Minimum number of horizontal lines to display in the which-key buffer."
:type 'integer
:package-version "1.0" :version "30.1")
(defcustom which-key-max-display-columns nil
"Maximum number of columns to display in the which-key buffer.
A value of nil means don't impose a maximum."
:type '(choice integer (const :tag "Unbounded" nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-side-window-location 'bottom
"Location of which-key popup when `which-key-popup-type' is side-window.
Should be one of top, bottom, left or right. You can also specify
a list of two locations, like (right bottom). In this case, the
first location is tried. If there is not enough room, the second
location is tried."
:type '(radio (const right)
(const bottom)
(const left)
(const top)
(const (right bottom))
(const (bottom right)))
:package-version "1.0" :version "30.1")
(defcustom which-key-side-window-slot 0
"The `slot' to use for `display-buffer-in-side-window'.
This applies when `which-key-popup-type' is `side-window'.
Quoting from the docstring of `display-buffer-in-side-window',
`slot' if non-nil, specifies the window slot where to display
BUFFER. A value of zero or nil means use the middle slot on the
specified side. A negative value means use a slot
preceding (that is, above or on the left of) the middle slot. A
positive value means use a slot following (that is, below or on
the right of) the middle slot. The default is zero."
:type 'integer
:package-version "1.0" :version "30.1")
(defcustom which-key-side-window-max-width 0.333
"Maximum width of which-key popup when type is side-window.
This variable can also be a number between 0 and 1. In that case,
it denotes a percentage out of the frame's width."
:type 'float
:package-version "1.0" :version "30.1")
(defcustom which-key-side-window-max-height 0.25
"Maximum height of which-key popup when type is side-window.
This variable can also be a number between 0 and 1. In that case, it denotes
a percentage out of the frame's height."
:type 'float
:package-version "1.0" :version "30.1")
(defcustom which-key-frame-max-width 60
"Maximum width of which-key popup when type is frame."
:type 'natnum
:package-version "1.0" :version "30.1")
(defcustom which-key-frame-max-height 20
"Maximum height of which-key popup when type is frame."
:type 'natnum
:package-version "1.0" :version "30.1")
(defcustom which-key-allow-imprecise-window-fit (not (display-graphic-p))
"Allow which-key to use a simpler method for resizing the popup.
If you are noticing lag when the which-key popup displays turning
this on may help.
See https://github.com/justbur/emacs-which-key/issues/130
and https://github.com/justbur/emacs-which-key/issues/225."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-show-remaining-keys nil
"Show remaining keys in last slot, when keys are hidden."
:type '(radio (const :tag "Yes" t)
(const :tag "No" nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-sort-order #'which-key-key-order
"Order in which the key bindings are sorted.
If nil, do not resort the output from `describe-buffer-bindings'
which groups by mode. Ordering options are:
1. `which-key-key-order': by key (default)
2. `which-key-key-order-alpha': by key using alphabetical order
3. `which-key-description-order': by description
4. `which-key-prefix-then-key-order': prefix (no prefix first) then key
5. `which-key-local-then-key-order': local binding then key
See the README and the docstrings for those functions for more
information."
:type '(choice (function-item which-key-key-order)
(function-item which-key-key-order-alpha)
(function-item which-key-description-order)
(function-item which-key-prefix-then-key-order)
(function-item which-key-local-then-key-order))
:package-version "1.0" :version "30.1")
(defcustom which-key-sort-uppercase-first t
"If non-nil, uppercase comes before lowercase in sorting.
This applies to the function chosen in
`which-key-sort-order'. Otherwise, the order is reversed."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-paging-prefixes '()
"Enable paging for these prefixes."
:type '(repeat string)
:package-version "1.0" :version "30.1")
(defcustom which-key-paging-key "<f5>"
"Key to use for changing pages.
Bound after each of the prefixes in `which-key-paging-prefixes'"
:type 'string
:package-version "1.0" :version "30.1")
;; (defcustom which-key-undo-key nil
;; "Key (string) to use for undoing keypresses. Bound recursively
;; in each of the maps in `which-key-undo-keymaps'."
;; :group 'which-key
;; :type 'string)
;; (defcustom which-key-undo-keymaps '()
;; "Keymaps in which to bind `which-key-undo-key'"
;; :group 'which-key
;; :type '(repeat symbol))
(defcustom which-key-use-C-h-commands t
"Use \\`C-h' (`help-char') for paging if non-nil.
Normally `help-char' after a prefix calls
`describe-prefix-bindings'. This changes that command to a
which-key paging command when `which-key-mode' is active."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-show-early-on-C-h nil
"Allow \\`C-h' (`help-char') to trigger which-key popup before timer.
Show the which-key buffer if `help-char' is pressed in the middle
of a prefix before the which-key buffer would normally be
triggered by the time. If combined with the following settings,
which-key will effectively only show when triggered \"manually\"
using \\`C-h'.
\(setq `which-key-idle-delay' 10000)
\(setq `which-key-idle-secondary-delay' 0.05)
Note that `which-key-idle-delay' should be set before turning on
`which-key-mode'."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-preserve-window-configuration nil
"Save and restore window configuration around which-key popup display.
If non-nil, save window configuration before which-key buffer is
shown and restore it after which-key buffer is hidden. It
prevents which-key from changing window position of visible
buffers. Only takken into account when popup type is
side-window."
:type 'boolean
:package-version "1.0" :version "30.1")
(defvar which-key-C-h-map-prompt
(concat " \\<which-key-C-h-map>"
" \\[which-key-show-next-page-cycle]"
which-key-separator "next-page,"
" \\[which-key-show-previous-page-cycle]"
which-key-separator "previous-page,"
" \\[which-key-undo-key]"
which-key-separator "undo-key,"
" \\[which-key-toggle-docstrings]"
which-key-separator "toggle-docstrings,"
" \\[which-key-show-standard-help]"
which-key-separator "help,"
" \\[which-key-abort]"
which-key-separator "abort"
" 1..9"
which-key-separator "digit-arg")
"Prompt to display when invoking `which-key-C-h-map'.
This string is fed into `substitute-command-keys'")
(defvar which-key-C-h-map
(let ((map (make-sparse-keymap)))
(dolist (bind `(("\C-a" . which-key-abort)
("a" . which-key-abort)
("\C-d" . which-key-toggle-docstrings)
("d" . which-key-toggle-docstrings)
(,(vector help-char) . which-key-show-standard-help)
("h" . which-key-show-standard-help)
("\C-n" . which-key-show-next-page-cycle)
("n" . which-key-show-next-page-cycle)
("\C-p" . which-key-show-previous-page-cycle)
("p" . which-key-show-previous-page-cycle)
("\C-u" . which-key-undo-key)
("u" . which-key-undo-key)
("1" . which-key-digit-argument)
("2" . which-key-digit-argument)
("3" . which-key-digit-argument)
("4" . which-key-digit-argument)
("5" . which-key-digit-argument)
("6" . which-key-digit-argument)
("7" . which-key-digit-argument)
("8" . which-key-digit-argument)
("9" . which-key-digit-argument)))
(define-key map (car bind) (cdr bind)))
map)
"Keymap for \\`C-h' commands.")
(defvar which-key--paging-functions
(list #'which-key-C-h-dispatch
#'which-key-turn-page
#'which-key-show-next-page-cycle
#'which-key-show-next-page-no-cycle
#'which-key-show-previous-page-cycle
#'which-key-show-previous-page-no-cycle
#'which-key-undo-key
#'which-key-undo))
(defvar which-key-persistent-popup nil
"Whether or not to disable `which-key--hide-popup'.")
(defcustom which-key-hide-alt-key-translations t
"Hide key translations using Alt key if non nil.
These translations are not relevant most of the times since a lot
of terminals issue META modifier for the Alt key.
See Info node `(emacs)Modifier Keys'."
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-delay-functions nil
"List of functions that may delay the which-key popup.
A list of functions that may decide whether to delay the
which-key popup based on the current incomplete key
sequence. Each function in the list is run with two arguments,
the current key sequence as produced by `key-description' and the
length of the key sequence. If the popup should be delayed based
on that key sequence, the function should return the delay time
in seconds. Returning nil means no delay. The first function in
this list to return a value is the value that is used.
The delay time is effectively added to the normal
`which-key-idle-delay'."
:type '(repeat function)
:package-version "1.0" :version "30.1")
(defcustom which-key-allow-regexps nil
"A list of regexp strings to use to filter key sequences.
When non-nil, for a key sequence to trigger the which-key popup
it must match one of the regexps in this list. The format of the
key sequences is what is produced by `key-description'."
:type '(repeat regexp)
:package-version "1.0" :version "30.1")
(defcustom which-key-inhibit-regexps nil
"A list of regexp strings to use to filter key sequences.
When non-nil, for a key sequence to trigger the which-key popup
it cannot match one of the regexps in this list. The format of
the key sequences is what is produced by `key-description'."
:type '(repeat regexp)
:package-version "1.0" :version "30.1")
(defcustom which-key-show-transient-maps nil
"Show keymaps created by `set-transient-map' when applicable.
More specifically, detect when `overriding-terminal-local-map' is
set (this is the keymap used by `set-transient-map') and display
it."
:type 'boolean
:package-version "1.0" :version "30.1")
(make-obsolete-variable
'which-key-enable-extended-define-key
"which-key-enable-extended-define-key is obsolete and has no effect."
"2021-06-21")
;; Hooks
(defcustom which-key-init-buffer-hook '()
"Hook run when which-key buffer is initialized."
:type 'hook
:package-version "1.0" :version "30.1")
;;;; Faces
(defgroup which-key-faces nil
"Faces for `which-key-mode'."
:group 'which-key
:prefix "which-key-")
(defface which-key-key-face
'((t . (:inherit font-lock-constant-face)))
"Face for which-key keys."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-separator-face
'((t . (:inherit font-lock-comment-face)))
"Face for the separator (default separator is an arrow)."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-note-face
'((t . (:inherit which-key-separator-face)))
"Face for notes or hints occasionally provided."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-command-description-face
'((t . (:inherit font-lock-function-name-face)))
"Face for the key description when it is a command."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-local-map-description-face
'((t . (:inherit which-key-command-description-face)))
"Face for the key description when it is found in `current-local-map'."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-highlighted-command-face
'((t . (:inherit (which-key-command-description-face highlight))))
"Default face for highlighted command descriptions.
A command is highlighted, when it matches a string in
`which-key-highlighted-command-list'."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-group-description-face
'((t . (:inherit font-lock-keyword-face)))
"Face for the key description when it is a group or prefix."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-special-key-face
'((t . (:inherit which-key-key-face :inverse-video t :weight bold)))
"Face for special keys (\\`SPC', \\`TAB', \\`RET')."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
(defface which-key-docstring-face
'((t . (:inherit which-key-note-face)))
"Face for docstrings."
:group 'which-key-faces
:package-version "1.0" :version "30.1")
;;;; Custom popup
(defcustom which-key-custom-popup-max-dimensions-function nil
"Set a custom max-dimensions function.
Will be passed the width of the active window and is expected to
return the maximum height in lines and width in characters of the
which-key popup in the form a cons cell (height . width)."
:group 'which-key
:type '(choice function (const nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-custom-hide-popup-function nil
"Set a custom hide-popup function.
It takes no arguments and the return value is ignored."
:group 'which-key
:type '(choice function (const nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-custom-show-popup-function nil
"Set a custom show-popup function.
Will be passed the required dimensions in the form (height .
width) in lines and characters respectively. The return value is
ignored."
:group 'which-key
:type '(choice function (const nil))
:package-version "1.0" :version "30.1")
(defcustom which-key-lighter " WK"
"Minor mode lighter to use in the mode-line."
:group 'which-key
:type 'string
:package-version "1.0" :version "30.1")
(defvar which-key-inhibit nil
"Prevent which-key from popping up momentarily.
This can be used by setting this to a non-nil value for the
execution of a command, as in
\(let \(\(which-key-inhibit t\)\)
...\)")
(defcustom which-key-inhibit-display-hook nil
"Hook run before display of which-key popup.
Each function in the hook is run before displaying the which-key
popup. If any function returns a non-nil value, the popup will
not display."
:group 'which-key
:type 'hook
:package-version "1.0" :version "30.1")
(defvar which-key-keymap-history nil
"History of keymap selections.
Used in functions like `which-key-show-keymap'.")
;;; Internal Vars
(defvar which-key--buffer nil
"Holds reference to which-key buffer.")
(defvar which-key--timer nil
"Holds reference to open window timer.")
(defvar which-key--secondary-timer-active nil
"Non-nil if the secondary timer is active.")
(defvar which-key--paging-timer nil
"Holds reference to timer for paging.")
(defvar which-key--frame nil
"Holds reference to which-key frame.
Used when `which-key-popup-type' is frame.")
(defvar which-key--echo-keystrokes-backup nil
"Backup the initial value of `echo-keystrokes'.")
(defvar which-key--prefix-help-cmd-backup nil
"Backup the value of `prefix-help-command'.")
(defvar which-key--last-try-2-loc nil
"Last location of side-window when two locations used.")
(defvar which-key--automatic-display nil
"Non-nil if popup was triggered with automatic update.")
(defvar which-key--debug-buffer-name nil
"If non-nil, use this buffer for debug messages.")
(defvar which-key--multiple-locations nil)
(defvar which-key--inhibit-next-operator-popup nil)
(defvar which-key--prior-show-keymap-args nil)
(defvar which-key--previous-frame-size nil)
(defvar which-key--prefix-title-alist nil)
(defvar which-key--evil-keys-regexp (eval-when-compile
(regexp-opt '("-state"))))
(defvar which-key--ignore-non-evil-keys-regexp
(regexp-opt '("mouse-" "wheel-" "remap" "drag-" "scroll-bar"
"select-window" "switch-frame" "which-key")))
(defvar which-key--ignore-keys-regexp
(regexp-opt '("mouse-" "wheel-" "remap" "drag-" "scroll-bar"
"select-window" "switch-frame" "-state"
"which-key")))
(defvar which-key--pages-obj nil)
(cl-defstruct which-key--pages
pages
height
widths
keys/page
page-nums
num-pages
total-keys
prefix
prefix-title)
(defvar which-key--saved-window-configuration nil)
(defun which-key--rotate (list n)
(let* ((len (length list))
(n (- len (mod n len))))
(append (last list n) (butlast list n))))
(defun which-key--pages-set-current-page (pages-obj n)
(setf (which-key--pages-pages pages-obj)
(which-key--rotate (which-key--pages-pages pages-obj) n))
(setf (which-key--pages-widths pages-obj)
(which-key--rotate (which-key--pages-widths pages-obj) n))
(setf (which-key--pages-keys/page pages-obj)
(which-key--rotate (which-key--pages-keys/page pages-obj) n))
(setf (which-key--pages-page-nums pages-obj)
(which-key--rotate (which-key--pages-page-nums pages-obj) n))
pages-obj)
(defsubst which-key--on-first-page ()
(= (which-key--pages-page-nums which-key--pages-obj) 1))
(defsubst which-key--on-last-page ()
(= (which-key--pages-page-nums which-key--pages-obj)
(which-key--pages-num-pages which-key--pages-obj)))
(defsubst which-key--current-prefix ()
(and which-key--pages-obj
(which-key--pages-prefix which-key--pages-obj)))
(defmacro which-key--debug-message (&rest msg)
`(when which-key--debug-buffer-name
(let ((buf (get-buffer-create which-key--debug-buffer-name))
(fmt-msg (format ,@msg)))
(with-current-buffer buf
(goto-char (point-max))
(insert "\n" fmt-msg "\n")))))
(defsubst which-key--safe-lookup-key (keymap key)
"Version of `lookup-key' that allows KEYMAP to be nil.
Also convert numeric results of `lookup-key' to nil.
KEY is not checked."
(when (keymapp keymap)
(let ((result (lookup-key keymap key)))
(when (and result (not (numberp result)))
result))))
(defsubst which-key--safe-lookup-key-description (keymap key)
"Version of `lookup-key' that allows KEYMAP to be nil.
Also convert numeric results of `lookup-key' to nil.
KEY should be formatted as an input for `kbd'."
(let ((key (ignore-errors (kbd key))))
(when (and key (keymapp keymap))
(let ((result (lookup-key keymap key)))
(when (and result (not (numberp result)))
result)))))
;;; Third-party library support
(defun which-key--this-command-keys ()
"Version of `this-single-command-keys' corrected for key-chords."
(let ((this-command-keys (this-single-command-keys)))
(when (and (vectorp this-command-keys)
(> (length this-command-keys) 0)
(eq (aref this-command-keys 0) 'key-chord)
(bound-and-true-p key-chord-mode))
(setq this-command-keys (this-single-command-raw-keys)))
this-command-keys))
(defcustom which-key-this-command-keys-function #'which-key--this-command-keys
"Function used to retrieve current key sequence.
The purpose of allowing this variable to be customized is to
allow which-key to support packages that insert non-standard
`keys' into the key sequence being read by Emacs."
:group 'which-key
:type 'function
:package-version "1.0" :version "30.1")
;;;; Evil
(defvar evil-state nil)
(defcustom which-key-allow-evil-operators (boundp 'evil-this-operator)
"Allow popup to show for evil operators.
The popup is normally inhibited in the middle of commands, but
setting this to non-nil will override this behavior for evil
operators."
:group 'which-key
:type 'boolean
:package-version "1.0" :version "30.1")
(defcustom which-key-show-operator-state-maps nil
"Show the keys following an evil command that reads a motion.
These are commands typically mapped to keys such as \"y\", \"d\"
and \"c\" from normal state. This is experimental, because there
might be some valid keys missing and it might be showing some
invalid keys."
:group 'which-key
:type 'boolean
:package-version "1.0" :version "30.1")
(defun which-key-evil-this-operator-p ()
(and which-key-allow-evil-operators
(bound-and-true-p evil-this-operator)))
(add-hook 'which-key-inhibit-display-hook
#'which-key-evil-this-operator-p)
;;;; God-mode
(defvar which-key--god-mode-support-enabled nil
"Support god-mode if non-nil.")
(defvar which-key--god-mode-key-string nil
"String to use for god-mode support.")
(defun which-key--god-mode-lookup-command-advice (orig-fun arg1 &rest args)
(setq which-key--god-mode-key-string arg1)
(unwind-protect
(apply orig-fun arg1 args)
(when (bound-and-true-p which-key-mode)
(which-key--hide-popup))))
(defun which-key--god-mode-this-command-keys ()
"Version of `this-single-command-keys' corrected for god-mode."
(let ((this-command-keys (this-single-command-keys)))
(when (and which-key--god-mode-support-enabled
(bound-and-true-p god-local-mode)
(eq this-command 'god-mode-self-insert))
(setq this-command-keys (when which-key--god-mode-key-string
(kbd which-key--god-mode-key-string))))
this-command-keys))
(defun which-key-god-mode-self-insert-p ()
(and which-key--god-mode-support-enabled
(bound-and-true-p god-local-mode)
(eq this-command 'god-mode-self-insert)))
(defun which-key-enable-god-mode-support (&optional disable)
"Enable support for god-mode if non-nil.
This is experimental, so you need to explicitly opt-in for
now. Please report any problems at github. If DISABLE is non-nil
disable support."
(interactive "P")
(setq which-key--god-mode-support-enabled (null disable))
(if disable
(progn
(advice-remove 'god-mode-lookup-command
#'which-key--god-mode-lookup-command-advice)
(remove-function which-key-this-command-keys-function
#'which-key--god-mode-this-command-keys)
(remove-hook 'which-key-inhibit-display-hook
#'which-key-god-mode-self-insert-p))
(advice-add 'god-mode-lookup-command :around
#'which-key--god-mode-lookup-command-advice)
(add-function :override which-key-this-command-keys-function
#'which-key--god-mode-this-command-keys)
(add-hook 'which-key-inhibit-display-hook
#'which-key-god-mode-self-insert-p)))
;;; Mode
;;;###autoload
(define-minor-mode which-key-mode
"Toggle `which-key-mode'."
:global t
:group 'which-key
:lighter which-key-lighter
:keymap (let ((map (make-sparse-keymap)))
(mapc
(lambda (prefix)
(define-key map
(kbd (concat prefix " " which-key-paging-key))
#'which-key-C-h-dispatch))
which-key-paging-prefixes)
map)
(if which-key-mode
(progn
(setq which-key--echo-keystrokes-backup echo-keystrokes)
(when (or (eq which-key-show-prefix 'echo)
(eq which-key-popup-type 'minibuffer))
(which-key--setup-echo-keystrokes))
(unless (member prefix-help-command which-key--paging-functions)
(setq which-key--prefix-help-cmd-backup prefix-help-command))
(when (or which-key-use-C-h-commands
which-key-show-early-on-C-h)
(setq prefix-help-command #'which-key-C-h-dispatch))
(when which-key-show-remaining-keys
(add-hook 'pre-command-hook #'which-key--lighter-restore))
(add-hook 'pre-command-hook #'which-key--hide-popup)
(add-hook 'window-size-change-functions
#'which-key--hide-popup-on-frame-size-change)
(which-key--start-timer))
(setq echo-keystrokes which-key--echo-keystrokes-backup)
(when which-key--prefix-help-cmd-backup
(setq prefix-help-command which-key--prefix-help-cmd-backup))
(when which-key-show-remaining-keys
(remove-hook 'pre-command-hook #'which-key--lighter-restore))
(remove-hook 'pre-command-hook #'which-key--hide-popup)
(remove-hook 'window-size-change-functions
#'which-key--hide-popup-on-frame-size-change)
(which-key--stop-timer)))
(defun which-key--init-buffer ()
"Initialize which-key buffer."
(unless (buffer-live-p which-key--buffer)
(setq which-key--buffer (get-buffer-create which-key-buffer-name))
(with-current-buffer which-key--buffer
;; suppress confusing minibuffer message
(let (message-log-max)
(toggle-truncate-lines 1)
(message ""))
(setq-local cursor-type nil)
(setq-local cursor-in-non-selected-windows nil)
(setq-local mode-line-format nil)
(setq-local header-line-format nil)
(setq-local word-wrap nil)
(setq-local show-trailing-whitespace nil)
(run-hooks 'which-key-init-buffer-hook))))
(defun which-key--setup-echo-keystrokes ()
"Reduce `echo-keystrokes' if necessary.
It will interfere if set too high."
(when (and echo-keystrokes
(> (abs (- echo-keystrokes which-key-echo-keystrokes)) 0.000001))
(if (> which-key-idle-delay which-key-echo-keystrokes)
(setq echo-keystrokes which-key-echo-keystrokes)
(setq which-key-echo-keystrokes (/ (float which-key-idle-delay) 4)
echo-keystrokes which-key-echo-keystrokes))))
(defun which-key-remove-default-unicode-chars ()
"Remove default unicode chars from settings.
Use of `which-key-dont-use-unicode' is preferred to this
function, but it's included here in case someone cannot set that
variable early enough in their configuration, if they are using a
starter kit for example."
(when (string-equal which-key-separator " → ")
(setq which-key-separator " : ")))
;;; Default configuration functions for use by users.
;;;###autoload
(defun which-key-setup-side-window-right ()
"Set up side-window on right."
(interactive)
(setq which-key-popup-type 'side-window
which-key-side-window-location 'right
which-key-show-prefix 'top))
;;;###autoload
(defun which-key-setup-side-window-right-bottom ()
"Set up side-window on right if space allows.
Otherwise, use bottom."
(interactive)
(setq which-key-popup-type 'side-window
which-key-side-window-location '(right bottom)
which-key-show-prefix 'top))
;;;###autoload
(defun which-key-setup-side-window-bottom ()
"Set up side-window that opens on bottom."
(interactive)
(which-key--setup-echo-keystrokes)
(setq which-key-popup-type 'side-window
which-key-side-window-location 'bottom
which-key-show-prefix 'echo))
;;;###autoload
(defun which-key-setup-minibuffer ()
"Set up minibuffer display.
Do not use this setup if you use the paging commands. Instead use
`which-key-setup-side-window-bottom', which is nearly identical
but more functional."
(interactive)
(which-key--setup-echo-keystrokes)
(setq which-key-popup-type 'minibuffer
which-key-show-prefix 'left))