Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom var which-key-format-desc-first #302

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 27 additions & 5 deletions which-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,17 @@ Note that `which-key-idle-delay' should be set before turning on
:group 'which-key
:type 'boolean)

(defcustom which-key-format-desc-first 'nil
"When set to non-nil, for each key binding print the
description followed by the key, as:
find-find -> C-x C-f

The default, nil, is to print the key first, as:
C-x C-f -> find-file
"
:group 'which-key
:type 'boolean)

(defvar which-key-C-h-map
(let ((map (make-sparse-keymap)))
(dolist (bind `(("\C-a" . which-key-abort)
Expand Down Expand Up @@ -1966,6 +1977,16 @@ element in each list element of KEYS."
(lambda (x y) (max x (which-key--string-width (nth index y))))
keys :initial-value 0))

(defun which-key--print-desc-first (key-width desc-width binding)
(format (concat "%-" (int-to-string desc-width)
"s%s%-" (int-to-string key-width) "s")
(nth 2 binding) (nth 1 binding) (nth 0 binding)))

(defun which-key--print-key-first (key-width desc-width binding)
(format (concat "%" (int-to-string key-width)
"s%s%-" (int-to-string desc-width) "s")
(nth 0 binding) (nth 1 binding) (nth 2 binding)))

(defun which-key--pad-column (col-keys)
"Take a column of (key separator description) COL-KEYS,
calculate the max width in the column and pad all cells out to
Expand All @@ -1974,12 +1995,13 @@ that width."
(which-key--max-len col-keys 0)))
(col-sep-width (which-key--max-len col-keys 1))
(col-desc-width (which-key--max-len col-keys 2))
(col-width (+ 1 col-key-width col-sep-width col-desc-width)))
(col-width (+ 1 col-key-width col-sep-width col-desc-width))
(formatter (if which-key-format-desc-first
#'which-key--print-desc-first
#'which-key--print-key-first)))
(cons col-width
(mapcar (lambda (k)
(format (concat "%" (int-to-string col-key-width)
"s%s%-" (int-to-string col-desc-width) "s")
(nth 0 k) (nth 1 k) (nth 2 k)))
(mapcar (apply-partially
formatter col-key-width col-desc-width)
col-keys))))

(defun which-key--partition-list (n list)
Expand Down