Skip to content

Commit

Permalink
Symbolize special keys
Browse files Browse the repository at this point in the history
Use symbols to represent special keys. If value of
``which-key-special-keys`` is an *alist* of cons
cells, whose cars are special keys such as SPC,
TAB,  ... and the cdrs are symbol strings, replace
the special keys with corresponding symbols.

For backward-compatibility, if
``which-key-special-keys`` is an ordinary list of
keys, old logic of trucation to single character
works.
  • Loading branch information
pradyparanjpe committed Apr 16, 2024
1 parent e21ee10 commit 2f3a780
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions which-key.el
Expand Up @@ -238,12 +238,19 @@ face to apply)."
:version "1.0")

(defcustom which-key-special-keys '()
"These keys will automatically be truncated to one character.
"These keys will automatically be truncated to one character or symbol.
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)
\(setq which-key-special-keys \\='(\"SPC\" \"TAB\" \"RET\" \"ESC\" \"DEL\")\) OR
\(setq which-key-special-keys
\\='((\"SPC\" . \"\")
(\"TAB\" . \"\")
(\"RET\" . \"\")
(\"ESC\" . \"\")
(\"DEL\" . \"\")
(\"backspace\" . \"\"))\)"
:type '(repeat (choice string cons))
:version "1.0")

(defcustom which-key-buffer-name " *which-key*"
Expand Down Expand Up @@ -1681,11 +1688,14 @@ no title exists."
(defun which-key--propertize-key (key)
"Add a face to KEY.
If KEY contains any \"special keys\" defined in
`which-key-special-keys' then truncate and add the corresponding
`which-key-special-keys' then truncate or symbolize and add the corresponding
`which-key-special-key-face'."
(let ((key-w-face (which-key--propertize key 'face 'which-key-key-face))
(regexp (concat "\\("
(mapconcat #'identity which-key-special-keys
(mapconcat #'identity
(if (consp (car which-key-special-keys))
(mapcar #'car which-key-special-keys)
which-key-special-keys)
"\\|")
"\\)"))
(case-fold-search nil))
Expand All @@ -1694,8 +1704,12 @@ If KEY contains any \"special keys\" defined in
(string-match regexp key))
(let ((beg (match-beginning 0)) (end (match-end 0)))
(concat (substring key-w-face 0 beg)
(which-key--propertize (substring key-w-face beg (1+ beg))
'face 'which-key-special-key-face)
(which-key--propertize
(or (cdr
(assoc (substring key-w-face beg end)
which-key-special-keys #'string=))
(substring key-w-face beg (1+ beg)))
'face 'which-key-special-key-face)
(substring key-w-face end
(which-key--string-width key-w-face))))
key-w-face))))
Expand Down

0 comments on commit 2f3a780

Please sign in to comment.