Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

add which-key-description-abbreviations #330

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions which-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ Also adds \"..\". If nil, disable any truncation."
:group 'which-key
:type '(choice integer (const :tag "Disable truncation" nil)))

(defcustom which-key-description-abbreviations nil
"A list of abbreviations to use when the key description is too long.
Will be applied before the text is truncated.
Replacements are done in sequence, until all replacements are applied
or if the text is below `which-key-max-description-length'"
:group 'which-key
:type '(choice (alist :key-type string :value-type string)
(nil :tag "Disabled")))

(defcustom which-key-min-column-description-width 0
"Every column should at least have this width."
:group 'which-key
Expand Down Expand Up @@ -141,7 +150,6 @@ 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 \"+\"."
Expand Down Expand Up @@ -1586,6 +1594,20 @@ If KEY contains any \"special keys\" defined in
(which-key--string-width key-w-face))))
key-w-face))))

(defsubst which-key--shorten-description (desc)
"Shorten DESC descripion using the `which-key-desciption-abbreviations'"
(let ((short desc)
(pos 0))
(when which-key-max-description-length
(while (and (> (length short) which-key-max-description-length)
(< pos (length which-key-description-abbreviations)))
(let* ((elt (nth pos which-key-description-abbreviations))
(replace (format "\\1%s\\2" (cdr elt)))
(regex (format "\\(-\\)%s\\(-\\|$\\)" (car elt))))
(setq short (replace-regexp-in-string regex replace short t nil nil))
(setq pos (1+ pos)))))
short))

(defsubst which-key--truncate-description (desc)
"Truncate DESC description to `which-key-max-description-length'."
(let* ((last-face (get-text-property (1- (length desc)) 'face desc))
Expand Down Expand Up @@ -1699,13 +1721,14 @@ alists. Returns a list (key separator description)."
(dolist (key-binding unformatted)
(let* ((keys (car key-binding))
(orig-desc (cdr key-binding))
(short-desc (which-key--shorten-description orig-desc))
(group (which-key--group-p orig-desc))
(local (eq (which-key--safe-lookup-key local-map (kbd keys))
(intern orig-desc)))
(hl-face (which-key--highlight-face orig-desc))
(key-binding (which-key--maybe-replace key-binding))
(final-desc (which-key--propertize-description
(cdr key-binding) group local hl-face orig-desc)))
short-desc group local hl-face orig-desc)))
(when final-desc
(setq final-desc
(which-key--truncate-description
Expand Down