Subject: Unicode symbols to represent which key special keys

From: प्रद्युम्न परांजपे<[email-masked]>
Date: Sat, 22 Jun 2024 14:31:15 +0530

Tags: patch

This patch corresponds to the pull request #372
<https://github.com/justbur/emacs-which-key/pull/372>.

This is a proposed feature enhancement in which-key to allow
propertized special key: a single user-defined unicode character that
will replace special keys such as SPC, TAB, RET, ESC, DEL, backspace, ...

Commit:

    Unicode symbols to represent which key special keys

    * lisp/which-key.el (which-key-speical-keys):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 truncation to single character
    works.

In GNU Emacs 29.3.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.42, cairo version 1.18.0) of 2024-06-04 built on
 ekadanta.anubandha.home
Repository revision: 00360258caddc0d8cf29ba3d9971125a06f8959b
Repository branch: emacs-29
System Description: Arch Linux

Configured using:
 'configure --prefix=/home/pradyumna/.local --with-native-compilation
 --with-imagemagick --with-gnutls --with-gif --with-tree-sitter
 --with-mailutils --with-harfbuzz --with-modules --with-cairo
 --with-jpeg --with-rsvg --with-xft --with-xpm --with-png --with-pgtk
 --with-dbus --with-tiff --with-webp --with-json'

-- 
Pradyumna Swanand Paranjape
diff --git a/lisp/which-key.el b/lisp/which-key.el
index 1de599e5497..7d986b7cb05 100644
--- a/lisp/which-key.el
+++ b/lisp/which-key.el
@@ -239,12 +239,20 @@ which-key-highlighted-command-list
   :package-version "1.0" :version "30.1")
 
 (defcustom which-key-special-keys '()
-  "These keys will automatically be truncated to one character.
+  "These keys will be truncated to first character or provided unicode 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)
+is disabled by default.  Example configurations are
+
+\(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))
   :package-version "1.0" :version "30.1")
 
 (defcustom which-key-buffer-name " *which-key*"
@@ -1686,7 +1694,10 @@ which-key--propertize-key
 `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))
@@ -1695,8 +1706,12 @@ which-key--propertize-key
                (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))))