forked from justbur/emacs-which-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhich-key.el
2809 lines (2530 loc) · 116 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 Free Software Foundation, Inc.
;; Author: Justin Burkett <[email protected]>
;; Maintainer: Justin Burkett <[email protected]>
;; URL: https://github.com/justbur/emacs-which-key
;; Version: 3.5.0
;; Keywords:
;; Package-Requires: ((emacs "24.4"))
;; 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:
;; which-key provides the minor mode which-key-mode for Emacs. The 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. Screenshots of what the popup will look like along with
;; information about additional features can be found at
;; https://github.com/justbur/emacs-which-key.
;;
;;; 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)."
:group 'which-key
:type 'float)
(defcustom which-key-idle-secondary-delay nil
"Once the which-key buffer shows once for a key sequence reduce
the idle time to this amount (in seconds). 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."
:group 'which-key
:type '(choice float (const :tag "Disabled" nil)))
(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."
:group 'which-key
:type 'float)
(defcustom which-key-max-description-length 27
"Truncate the description of keys to this length.
Also adds \"..\". If nil, disable any truncation."
:group 'which-key
:type '(choice integer (const :tag "Disable truncation" nil)))
(defcustom which-key-add-column-padding 0
"Additional padding (number of spaces) to add to the left of
each key column."
:group 'which-key
:type 'integer)
(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."
:group 'which-key
:type 'integer)
(defcustom which-key-dont-use-unicode nil
"If non-nil, don't use any unicode characters in default setup."
:group 'which-key
:type 'boolean)
(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 \" : \"."
:group 'which-key
:type 'string)
(defcustom which-key-prefix-prefix "+"
"String to insert in front of prefix commands (i.e., commands
that represent a sub-map). Default is \"+\"."
:group 'which-key
:type 'string)
(defcustom which-key-compute-remaps nil
"If non-nil, show remapped command if a command has been
remapped given the currently active keymaps."
:group 'which-key
:type 'boolean)
(defvar which-key-key-replacement-alist nil)
(make-obsolete-variable 'which-key-key-replacement-alist
'which-key-replacement-alist "2016-11-21")
(defvar which-key-description-replacement-alist nil)
(make-obsolete-variable 'which-key-description-replacement-alist
'which-key-replacement-alist "2016-11-21")
(defvar which-key-key-based-description-replacement-alist nil)
(make-obsolete-variable 'which-key-key-based-description-replacement-alist
'which-key-replacement-alist "2016-11-21")
(defcustom which-key-replacement-alist
(delq nil
`(((nil . "Prefix Command") . (nil . "prefix"))
((nil . "\\`\\?\\?\\'") . (nil . "lambda"))
((nil . "which-key-show-next-page-no-cycle") . (nil . "wk next pg"))
,@(unless which-key-dont-use-unicode
'((("<left>") . ("←"))
(("<right>") . ("→"))))
(("<\\([[:alnum:]-]+\\)>") . ("\\1"))))
"Association list to determine how to manipulate descriptions
of key bindings in the which-key popup. 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."
:group 'which-key
:type '(alist :key-type (cons (choice regexp (const nil))
(choice regexp (const nil)))
:value-type (cons (choice string (const nil))
(choice string (const nil)))))
(when (bound-and-true-p which-key-key-replacement-alist)
(mapc
(lambda (repl)
(push (cons (cons (car repl) nil) (cons (cdr repl) nil))
which-key-replacement-alist))
which-key-key-replacement-alist))
(when (bound-and-true-p which-key-description-replacement-alist)
(mapc
(lambda (repl)
(push (cons (cons nil (car repl)) (cons nil (cdr repl)))
which-key-replacement-alist))
which-key-description-replacement-alist))
(defcustom which-key-allow-multiple-replacements nil
"Allow a key binding to match and be modified by multiple
elements in `which-key-replacement-alist' if non-nil. When nil,
only the first match is used to perform replacements from
`which-key-replacement-alist'."
:group 'which-key
:type 'boolean)
(defcustom which-key-show-docstrings nil
"If non-nil, show each command's docstring next to the command
in the which-key buffer. 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."
:group 'which-key
: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)))
(defcustom which-key-highlighted-command-list '()
"A list of strings and/or cons cells 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)."
:group 'which-key
:type '(repeat (choice string (cons regexp face))))
(defcustom which-key-special-keys '()
"These keys will automatically be truncated to one character
and have `which-key-special-key-face' applied to them. This is
disabled by default. Try this to see the effect.
\(setq which-key-special-keys '(\"SPC\" \"TAB\" \"RET\" \"ESC\" \"DEL\")\)"
:group 'which-key
:type '(repeat string))
(defcustom which-key-buffer-name " *which-key*"
"Name of which-key buffer."
:group 'which-key
:type 'string)
(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."
:group 'which-key
: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)))
(defcustom which-key-popup-type 'side-window
"Supported types are minibuffer, side-window, frame, and custom."
:group 'which-key
: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)))
(defcustom which-key-min-display-lines 1
"The minimum number of horizontal lines to display in the
which-key buffer."
:group 'which-key
:type 'integer)
(defcustom which-key-max-display-columns nil
"The maximum number of columns to display in the which-key
buffer. nil means don't impose a maximum."
:group 'which-key
:type '(choice integer (const :tag "Unbounded" nil)))
(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."
:group 'which-key
:type '(radio (const right)
(const bottom)
(const left)
(const top)
(const (right bottom))
(const (bottom right))))
(defcustom which-key-side-window-slot 0
"The `slot' to use for `display-buffer-in-side-window' 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."
:group 'which-key
:type 'integer)
(defcustom which-key-side-window-max-width 0.333
"Maximum width of which-key popup when type is side-window and
location is left or right.
This variable can also be a number between 0 and 1. In that case, it denotes
a percentage out of the frame's width."
:group 'which-key
:type 'float)
(defcustom which-key-side-window-max-height 0.25
"Maximum height of which-key popup when type is side-window and
location is top or bottom.
This variable can also be a number between 0 and 1. In that case, it denotes
a percentage out of the frame's height."
:group 'which-key
:type 'float)
(defcustom which-key-frame-max-width 60
"Maximum width of which-key popup when type is frame."
:group 'which-key
:type 'integer)
(defcustom which-key-frame-max-height 20
"Maximum height of which-key popup when type is frame."
:group 'which-key
:type 'integer)
(defcustom which-key-allow-imprecise-window-fit (not (display-graphic-p))
"If non-nil allow which-key to use a less intensive method of
fitting the popup window to the buffer. 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."
:group 'which-key
:type 'boolean)
(defcustom which-key-show-remaining-keys nil
"Show remaining keys in last slot, when keys are hidden."
:group 'which-key
:type '(radio (const :tag "Yes" t)
(const :tag "No" nil)))
(defcustom which-key-sort-order 'which-key-key-order
"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."
:group 'which-key
: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)))
(defcustom which-key-sort-uppercase-first t
"If non-nil, uppercase comes before lowercase in sorting
function chosen in `which-key-sort-order'. Otherwise, the order
is reversed."
:group 'which-key
:type 'boolean)
(defcustom which-key-paging-prefixes '()
"Enable paging for these prefixes."
:group 'which-key
:type '(repeat string))
(defcustom which-key-paging-key "<f5>"
"Key to use for changing pages. Bound after each of the
prefixes in `which-key-paging-prefixes'"
:group 'which-key
:type 'string)
;; (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 (or whatever `help-char' is set to) for paging if
non-nil. Normally C-h after a prefix calls
`describe-prefix-bindings'. This changes that command to a
which-key paging command when which-key-mode is active."
:group 'which-key
:type 'boolean)
(defcustom which-key-show-early-on-C-h nil
"Show the which-key buffer before if C-h (or whatever
`help-char' is set to) is pressed in the middle of a prefix
before the which-key buffer would normally be triggered through
the idle delay. 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'. "
:group 'which-key
:type 'boolean)
(defcustom which-key-is-verbose nil
"Whether to warn about potential mistakes in configuration."
:group 'which-key
:type 'boolean)
(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 '(which-key-C-h-dispatch
which-key-manual-update
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 http://www.gnu.org/software/emacs/manual/html_node/emacs/Modifier-Keys.html"
:group 'which-key
:type 'boolean)
(defcustom which-key-delay-functions nil
"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'."
:group 'which-key
:type '(repeat function))
(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'."
:group 'which-key
:type '(repeat regexp))
(defcustom which-key-inhibit-regexps nil
"Similar to `which-key-allow-regexps', 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'."
:group 'which-key
:type '(repeat regexp))
(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."
:group 'which-key
:type 'boolean)
(defcustom which-key-enable-extended-define-key nil
"Advise `define-key' to make which-key aware of definitions of the form
\(define-key KEYMAP KEY '(\"DESCRIPTION\" . DEF))
With the advice, this definition will have the side effect of
creating a replacement in `which-key-replacement-alist' that
replaces DEF with DESCRIPTION when the key sequence ends in
KEY. Using a cons cell like this is a valid definition for
`define-key'. All this does is to make which-key aware of it.
Since many higher level keybinding functions use `define-key'
internally, this will affect most if not all of those as well.
This variable must be set before loading which-key."
:group 'which-key
:type 'boolean)
;; Hooks
(defcustom which-key-init-buffer-hook '()
"Hook run when which-key buffer is initialized."
:group 'which-key
:type 'hook)
;;;; 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)
(defface which-key-separator-face
'((t . (:inherit font-lock-comment-face)))
"Face for the separator (default separator is an arrow)"
:group 'which-key-faces)
(defface which-key-note-face
'((t . (:inherit which-key-separator-face)))
"Face for notes or hints occasionally provided"
:group 'which-key-faces)
(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)
(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)
(defface which-key-highlighted-command-face
'((t . (:inherit which-key-command-description-face :underline t)))
"Default face for the command description when it is a command
and it matches a string in `which-key-highlighted-command-list'."
:group 'which-key-faces)
(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)
(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)
(defface which-key-docstring-face
'((t . (:inherit which-key-note-face)))
"Face for docstrings"
:group 'which-key-faces)
;;;; Custom popup
(defcustom which-key-custom-popup-max-dimensions-function nil
"Variable to hold 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)))
(defcustom which-key-custom-hide-popup-function nil
"Variable to hold a custom hide-popup function.
It takes no arguments and the return value is ignored."
:group 'which-key
:type '(choice function (const nil)))
(defcustom which-key-custom-show-popup-function nil
"Variable to hold 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)))
(defcustom which-key-lighter " WK"
"Minor mode lighter to use in the mode-line."
:group 'which-key
:type 'string)
(defvar which-key-inhibit nil
"Prevent which-key from popping up momentarily by setting this
to a non-nil value for the execution of a command. Like this
\(let \(\(which-key-inhibit t\)\)
...\)")
(defvar which-key-keymap-history nil
"History of keymap selections in functions like
`which-key-show-keymap'.")
;;; Internal Vars
(defvar which-key--buffer nil
"Internal: Holds reference to which-key buffer.")
(defvar which-key--timer nil
"Internal: Holds reference to open window timer.")
(defvar which-key--secondary-timer-active nil
"Internal: Non-nil if the secondary timer is active.")
(defvar which-key--paging-timer nil
"Internal: Holds reference to timer for paging.")
(defvar which-key--frame nil
"Internal: Holds reference to which-key frame.
Used when `which-key-popup-type' is frame.")
(defvar which-key--echo-keystrokes-backup nil
"Internal: Backup the initial value of `echo-keystrokes'.")
(defvar which-key--prefix-help-cmd-backup nil
"Internal: Backup the value of `prefix-help-command'.")
(defvar which-key--last-try-2-loc nil
"Internal: Last location of side-window when two locations
used.")
(defvar which-key--automatic-display nil
"Internal: 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
(eval-when-compile
(regexp-opt '("mouse-" "wheel-" "remap" "drag-" "scroll-bar"
"select-window" "switch-frame" "which-key-"))))
(defvar which-key--ignore-keys-regexp
(eval-when-compile
(regexp-opt '("mouse-" "wheel-" "remap" "drag-" "scroll-bar"
"select-window" "switch-frame" "-state"
"which-key-"))))
(make-obsolete-variable 'which-key-prefix-name-alist nil "2016-10-05")
(make-obsolete-variable 'which-key-prefix-title-alist nil "2016-10-05")
(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)
(defun which-key--rotate (list n)
(let* ((len (length list))
(n (if (< n 0) (+ len n) n))
(n (mod n len)))
(append (last list (- len n)) (butlast list (- len 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 ()
(when 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")))))
;;; Third-party library support
;;;; Evil
(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)
(defcustom which-key-show-operator-state-maps nil
"Experimental: Try to show the right keys following an evil
command that reads a motion, 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)
;;;;; God-mode
(defvar which-key--god-mode-support-enabled nil
"Support god-mode if non-nil. This is experimental,
so you need to explicitly opt-in for now. Please report any
problems at github.")
(defvar which-key--god-mode-key-string nil
"Holds key string to use for god-mode support.")
(defadvice god-mode-lookup-command
(around which-key--god-mode-lookup-command-advice disable)
(setq which-key--god-mode-key-string (ad-get-arg 0))
(unwind-protect
ad-do-it
(when (bound-and-true-p which-key-mode)
(which-key--hide-popup))))
(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
(ad-disable-advice
'god-mode-lookup-command
'around 'which-key--god-mode-lookup-command-advice)
(ad-enable-advice
'god-mode-lookup-command
'around 'which-key--god-mode-lookup-command-advice))
(ad-activate 'god-mode-lookup-command))
;;; Mode
;;;###autoload
(define-minor-mode which-key-mode
"Toggle which-key-mode."
:global t
: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 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
it's 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 ()
"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 " : "))
(setq which-key-key-replacement-alist
(delete '("left" . "←") which-key-key-replacement-alist))
(setq which-key-key-replacement-alist
(delete '("right" . "→") which-key-key-replacement-alist)))
;;; Default configuration functions for use by users.
;;;###autoload
(defun which-key-setup-side-window-right ()
"Apply suggested settings for side-window that opens 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 ()
"Apply suggested settings for side-window that opens on right
if there is space and the bottom otherwise."
(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 ()
"Apply suggested settings for 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 ()
"Apply suggested settings for minibuffer.
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))
;;; Helper functions to modify replacement lists.
;;;###autoload
(defun which-key-add-keymap-based-replacements (keymap key replacement &rest more)
"Replace the description of KEY using REPLACEMENT in KEYMAP.
KEY should take a format suitable for use in
`kbd'. REPLACEMENT is the string to use to describe the
command associated with KEY in the KEYMAP. You may also use a
cons cell of the form \(STRING . COMMAND\) for each REPLACEMENT,
where STRING is the replacement string and COMMAND is a symbol
corresponding to the intended command to be replaced. In the
latter case, which-key will verify the intended command before
performing the replacement. COMMAND should be nil if the binding
corresponds to a key prefix. For example,
\(which-key-add-keymap-based-replacements global-map
\"C-x w\" \"Save as\"\)
and
\(which-key-add-keymap-based-replacements global-map
\"C-x w\" '\(\"Save as\" . write-file\)\)
both have the same effect for the \"C-x C-w\" key binding, but
the latter causes which-key to verify that the key sequence is
actually bound to write-file before performing the replacement."
(while key
(let ((string (if (stringp replacement)
replacement
(car-safe replacement)))
(command (cdr-safe replacement)))
(define-key keymap (which-key--pseudo-key (kbd key))
`(which-key ,(cons string command))))
(setq key (pop more)
replacement (pop more))))
(put 'which-key-add-keymap-based-replacements 'lisp-indent-function 'defun)
;;;###autoload
(defun which-key-add-key-based-replacements
(key-sequence replacement &rest more)
"Replace the description of KEY-SEQUENCE with REPLACEMENT.
KEY-SEQUENCE is a string suitable for use in `kbd'. REPLACEMENT
may either be a string, as in
\(which-key-add-key-based-replacements \"C-x 1\" \"maximize\"\)
a cons of two strings as in
\(which-key-add-key-based-replacements \"C-x 8\"
'(\"unicode\" . \"Unicode keys\")\)
or a function that takes a \(KEY . BINDING\) cons and returns a
replacement.
In the second case, the second string is used to provide a longer
name for the keys under a prefix.
MORE allows you to specifcy additional KEY REPLACEMENT pairs. All
replacements are added to
`which-key-key-based-description-replacement-alist'."
;; TODO: Make interactive
(while key-sequence
;; normalize key sequences before adding
(let ((key-seq (key-description (kbd key-sequence)))
(replace (or (and (functionp replacement) replacement)
(car-safe replacement)
replacement)))
(push (cons (cons (concat "\\`" (regexp-quote key-seq) "\\'") nil)
(if (functionp replace) replace (cons nil replace)))
which-key-replacement-alist)
(when (and (not (functionp replacement)) (consp replacement))
(push (cons key-seq (cdr-safe replacement))
which-key--prefix-title-alist)))
(setq key-sequence (pop more) replacement (pop more))))
(put 'which-key-add-key-based-replacements 'lisp-indent-function 'defun)
;;;###autoload
(defun which-key-add-major-mode-key-based-replacements
(mode key-sequence replacement &rest more)
"Functions like `which-key-add-key-based-replacements'.
The difference is that MODE specifies the `major-mode' that must
be active for KEY-SEQUENCE and REPLACEMENT (MORE contains
addition KEY-SEQUENCE REPLACEMENT pairs) to apply."
;; TODO: Make interactive
(when (not (symbolp mode))
(error "MODE should be a symbol corresponding to a value of major-mode"))
(let ((mode-alist
(or (cdr-safe (assq mode which-key-replacement-alist)) (list)))
(title-mode-alist
(or (cdr-safe (assq mode which-key--prefix-title-alist)) (list))))
(while key-sequence
;; normalize key sequences before adding