From 6abe544835d67f92a633fbf2622b9a8b6eb10a3c Mon Sep 17 00:00:00 2001 From: Wesley Nelson Date: Mon, 31 Jul 2023 17:52:56 -0700 Subject: [PATCH] Add support for Devil What is Devil? > Devil mode trades your comma key in exchange for a modifier-free editing experience in Emacs. The above description comes from their README at: https://github.com/susam/devil Unfortunately, Devil is not currently supported by which-key. For example, when I type `, x`, the which-key popup for `C-x` does not appear. Instead, I merely see the following text in the echo area: ``` Devil: C-x ``` With this patch, I have mimicked the existing god-mode logic in which-key to intercept the current Devil prompt and spawn a which-key popup. This feature was requested by susam/devil#3: - https://github.com/susam/devil/issues/3 --- which-key.el | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/which-key.el b/which-key.el index 25f2397..cdbcf76 100644 --- a/which-key.el +++ b/which-key.el @@ -812,6 +812,43 @@ disable support." (advice-add 'god-mode-lookup-command :around #'which-key--god-mode-lookup-command-advice))) +;;;; Devil + +(defvar which-key--devil-support-enabled nil + "Support devil if non-nil. +This is experimental, so you need to explicitly opt-in for now. Please +report any problems at github.") + +(defvar which-key--devil-key-string nil + "Holds key string to use for devil support.") + +(defun which-key--devil--read-key-advice (orig-fun prompt key &rest args) + "Wrap `devil--read-key' to store the current command. +ORIG-FUN is the function which is being wrapped. PROMPT and KEY are +the first and second arguments which are passed to ORIG-FUN, and ARGS +is a catch-all for any other arguments which may be passed to +ORIG-FUN. This current command will be stored in +`which-key--devil-key-string'." + (setq which-key--devil-key-string + (when (fboundp 'devil--translate) (devil--translate key))) + (unwind-protect + (apply orig-fun prompt key args) + (when (bound-and-true-p which-key-mode) + (which-key--hide-popup)))) + +(defun which-key-enable-devil-support (&optional disable) + "Enable support for devil if non-nil. +This is experimental, so you need to explicitly opt-in for now. +Please report any problems at github. If DISABLE is non-nil +disable support." + (interactive "P") + (setq which-key--devil-support-enabled (null disable)) + (if disable + (advice-remove 'devil--read-key + #'which-key--devil--read-key-advice) + (advice-add 'devil--read-key :around + #'which-key--devil--read-key-advice))) + ;;; Mode ;;;###autoload @@ -2640,7 +2677,9 @@ Finally, show the buffer." (* 1000 (float-time (time-since start-time)))))) (defun which-key--this-command-keys () - "Version of `this-single-command-keys' corrected for key-chords and god-mode." + "Version of `this-single-command-keys' corrected for dependencies. +The dependencies currently considered (if available and enabled) are +key-chords, god-mode, and devil." (let ((this-command-keys (this-single-command-keys))) (when (and (vectorp this-command-keys) (> (length this-command-keys) 0) @@ -2652,6 +2691,11 @@ Finally, show the buffer." (eq this-command 'god-mode-self-insert)) (setq this-command-keys (when which-key--god-mode-key-string (kbd which-key--god-mode-key-string)))) + (when (and which-key--devil-support-enabled + (bound-and-true-p devil-mode) + (eq this-command 'devil)) + (setq this-command-keys (when which-key--devil-key-string + (kbd which-key--devil-key-string)))) this-command-keys)) (defun which-key--update () @@ -2682,6 +2726,9 @@ Finally, show the buffer." (and which-key--god-mode-support-enabled (bound-and-true-p god-local-mode) (eq this-command 'god-mode-self-insert)) + (and which-key--devil-support-enabled + (bound-and-true-p devil-mode) + (eq this-command 'devil)) (null this-command))) (when (and (not (equal prefix-keys (which-key--current-prefix))) (or (null which-key-delay-functions)