diff options
| author | Jonas Bernoulli | 2022-08-04 14:05:13 +0200 |
|---|---|---|
| committer | Jonas Bernoulli | 2022-08-04 14:05:13 +0200 |
| commit | 783d6954bcea18c94d7eee723286f820fbd8b4b2 (patch) | |
| tree | 11c16ce47deaffb65bbebf28e0d2fe7de82218a4 | |
| parent | fe68fe28126efa44a1050a2eedfc719c23054db1 (diff) | |
| download | emacs-783d6954bcea18c94d7eee723286f820fbd8b4b2.tar.gz emacs-783d6954bcea18c94d7eee723286f820fbd8b4b2.zip | |
Support different types for which-key-max-description-length
Using a function is useful, e.g., to use a different maximal
width, depending on the value of `which-key-show-docstrings'.
| -rw-r--r-- | README.org | 1 | ||||
| -rw-r--r-- | which-key.el | 35 |
2 files changed, 26 insertions, 10 deletions
diff --git a/README.org b/README.org index 9d9a4c4a57b..82a1466961f 100644 --- a/README.org +++ b/README.org | |||
| @@ -520,6 +520,7 @@ | |||
| 520 | 520 | ||
| 521 | ;; Set the maximum length (in characters) for key descriptions (commands or | 521 | ;; Set the maximum length (in characters) for key descriptions (commands or |
| 522 | ;; prefixes). Descriptions that are longer are truncated and have ".." added. | 522 | ;; prefixes). Descriptions that are longer are truncated and have ".." added. |
| 523 | ;; This can also be a float (fraction of available width) or a function. | ||
| 523 | (setq which-key-max-description-length 27) | 524 | (setq which-key-max-description-length 27) |
| 524 | 525 | ||
| 525 | ;; Use additional padding between columns of keys. This variable specifies the | 526 | ;; Use additional padding between columns of keys. This variable specifies the |
diff --git a/which-key.el b/which-key.el index 529cceb40a1..a2986269fdd 100644 --- a/which-key.el +++ b/which-key.el | |||
| @@ -89,9 +89,16 @@ which-key popup." | |||
| 89 | 89 | ||
| 90 | (defcustom which-key-max-description-length 27 | 90 | (defcustom which-key-max-description-length 27 |
| 91 | "Truncate the description of keys to this length. | 91 | "Truncate the description of keys to this length. |
| 92 | Also adds \"..\". If nil, disable any truncation." | 92 | Either nil (no truncation), an integer (truncate after that many |
| 93 | characters), a float (use that fraction of the available width), | ||
| 94 | or a function, which takes one argument, the available width in | ||
| 95 | characters, and whose return value has one of the types mentioned | ||
| 96 | before. Truncation is done using `which-key-ellipsis'." | ||
| 93 | :group 'which-key | 97 | :group 'which-key |
| 94 | :type '(choice integer (const :tag "Disable truncation" nil))) | 98 | :type '(choice (const :tag "Disable truncation" nil) |
| 99 | (integer :tag "Width in characters") | ||
| 100 | (float :tag "Use fraction of available width") | ||
| 101 | function)) | ||
| 95 | 102 | ||
| 96 | (defcustom which-key-min-column-description-width 0 | 103 | (defcustom which-key-min-column-description-width 0 |
| 97 | "Every column should at least have this width." | 104 | "Every column should at least have this width." |
| @@ -1587,14 +1594,20 @@ If KEY contains any \"special keys\" defined in | |||
| 1587 | (which-key--string-width key-w-face)))) | 1594 | (which-key--string-width key-w-face)))) |
| 1588 | key-w-face)))) | 1595 | key-w-face)))) |
| 1589 | 1596 | ||
| 1590 | (defsubst which-key--truncate-description (desc) | 1597 | (defsubst which-key--truncate-description (desc avl-width) |
| 1591 | "Truncate DESC description to `which-key-max-description-length'." | 1598 | "Truncate DESC description to `which-key-max-description-length'." |
| 1592 | (if (and which-key-max-description-length | 1599 | (let* ((max which-key-max-description-length) |
| 1593 | (> (length desc) which-key-max-description-length)) | 1600 | (max (cl-etypecase max |
| 1594 | (let* ((last-face (get-text-property (1- (length desc)) 'face desc)) | 1601 | (null nil) |
| 1595 | (dots (which-key--propertize which-key-ellipsis 'face last-face))) | 1602 | (integer max) |
| 1596 | (concat (substring desc 0 which-key-max-description-length) dots)) | 1603 | (float (truncate (* max avl-width))) |
| 1597 | desc)) | 1604 | (function (let ((val (funcall max avl-width))) |
| 1605 | (if (floatp val) (truncate val) val)))))) | ||
| 1606 | (if (and max (> (length desc) max)) | ||
| 1607 | (let* ((last-face (get-text-property (1- (length desc)) 'face desc)) | ||
| 1608 | (dots (which-key--propertize which-key-ellipsis 'face last-face))) | ||
| 1609 | (concat (substring desc 0 max) dots)) | ||
| 1610 | desc))) | ||
| 1598 | 1611 | ||
| 1599 | (defun which-key--highlight-face (description) | 1612 | (defun which-key--highlight-face (description) |
| 1600 | "Return the highlight face for DESCRIPTION if it has one." | 1613 | "Return the highlight face for DESCRIPTION if it has one." |
| @@ -1696,6 +1709,7 @@ alists. Returns a list (key separator description)." | |||
| 1696 | (which-key--propertize which-key-separator | 1709 | (which-key--propertize which-key-separator |
| 1697 | 'face 'which-key-separator-face)) | 1710 | 'face 'which-key-separator-face)) |
| 1698 | (local-map (current-local-map)) | 1711 | (local-map (current-local-map)) |
| 1712 | (avl-width (cdr (which-key--popup-max-dimensions))) | ||
| 1699 | new-list) | 1713 | new-list) |
| 1700 | (dolist (key-binding unformatted) | 1714 | (dolist (key-binding unformatted) |
| 1701 | (let* ((keys (car key-binding)) | 1715 | (let* ((keys (car key-binding)) |
| @@ -1710,7 +1724,8 @@ alists. Returns a list (key separator description)." | |||
| 1710 | (when final-desc | 1724 | (when final-desc |
| 1711 | (setq final-desc | 1725 | (setq final-desc |
| 1712 | (which-key--truncate-description | 1726 | (which-key--truncate-description |
| 1713 | (which-key--maybe-add-docstring final-desc orig-desc)))) | 1727 | (which-key--maybe-add-docstring final-desc orig-desc) |
| 1728 | avl-width))) | ||
| 1714 | (when (consp key-binding) | 1729 | (when (consp key-binding) |
| 1715 | (push | 1730 | (push |
| 1716 | (list (which-key--propertize-key | 1731 | (list (which-key--propertize-key |