Skip to content

Instantly share code, notes, and snippets.

@mmarshall540
Created February 27, 2025 23:30
Show Gist options
  • Save mmarshall540/b51fcfb7813c99578e18406572c5c669 to your computer and use it in GitHub Desktop.
Save mmarshall540/b51fcfb7813c99578e18406572c5c669 to your computer and use it in GitHub Desktop.
A macro for shortening which-key descriptions
(defmacro my/wk-abbrev (prefixstr &optional prefixabbrev mode)
"Remove PREFIXSTR from which-key descriptions.
Or, with PREFIXABBREV, replace it. Optionally, condition
replacement on the value of MODE. This should typically be used
in the `:config' block of the package that uses the prefix. An
`eval-after-load' is included, to ensure that which-key will have
loaded before adding to `which-key-replacement-alist'."
(let ((wkabbfuncsym (intern
(concat "my-wk-" prefixstr "-abbreviator")))
(rx (concat "^\\(\\[[ X]\\] \\|\\)" prefixstr "-")))
`(eval-after-load 'init
(quote
(setq which-key-replacement-alist
(append
which-key-replacement-alist
(list
(quote
,(cons (cons nil rx)
(fset wkabbfuncsym
(lambda (wkcons)
(let ((inkey (car wkcons))
(indesc (cdr wkcons)))
(if (and mode
(not (eval mode)))
wkcons
(cons
inkey
(replace-regexp-in-string
rx (if prefixabbrev
(concat "\\1"
prefixabbrev "-")
"\\1")
indesc)))))))))))))))
@mmarshall540
Copy link
Author

Example usage:

(use-package gnus
  ;; stuff here
  :config
  (my/wk-abbrev "gnus-agent"   "Ga")
  (my/wk-abbrev "gnus-server"  "Gs")
  (my/wk-abbrev "gnus-topic"   "GT")
  (my/wk-abbrev "gnus-group"   "GG")
  (my/wk-abbrev "gnus-article" "GA")
  (my/wk-abbrev "gnus-summary" "GS")
  (my/wk-abbrev "gnus-browse"  "GB"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment