From 174b52526b21de631ff28fadb1343841e2a46204 Mon Sep 17 00:00:00 2001 From: Fabian Brosda Date: Fri, 8 Nov 2024 21:38:56 +0100 Subject: [PATCH 1/3] Add which-key support This adds support for the which-key-mode based on the advice in https://github.com/justbur/emacs-which-key/pull/353#issuecomment-1965404948 It reuses most parts of the original implementation found in https://github.com/justbur/emacs-which-key/pull/353/files and adjusts it for the changes made in https://github.com/justbur/emacs-which-key/commit/5fbdf05351e77ee62e3933c7b5f46459693bd04c --- devil.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/devil.el b/devil.el index f8d8fc9..9c9432f 100644 --- a/devil.el +++ b/devil.el @@ -254,7 +254,10 @@ locally." (define-minor-mode devil-mode "Local minor mode to support Devil key sequences." :lighter devil-lighter - (devil--log "Mode is %s in %s" devil-mode (buffer-name))) + (devil--log "Mode is %s in %s" devil-mode (buffer-name)) + (if devil-mode + (devil--enable-which-key-support) + (devil--enable-which-key-support -1))) ;;;###autoload (define-globalized-minor-mode @@ -618,6 +621,58 @@ last-command-event: %s; char-before: %s" (when (string= described-key (devil-format repeatable-key)) (throw 'break repeatable-group)))))) + +;;;; Which-Key Support =============================================== + +(defvar devil--which-key-string nil + "Holds key string to use for which-key support.") + +(defun devil--which-key-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 +`devil--which-key-string'." + (setq devil--which-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 devil--which-key-this-command-keys () + "Version of `which-key-this-command-keys-function' for devil-mode." + (let ((this-command-keys (this-single-command-keys))) + (when (devil--which-key-self-insert-p) + (setq this-command-keys (when devil--which-key-string + (kbd devil--which-key-string)))) + this-command-keys)) + +(defun devil--which-key-self-insert-p () + (and (bound-and-true-p devil-mode) + (eq this-command 'devil))) + +(defun devil--enable-which-key-support (&optional disable) + "Enable support for which-key if non-nil. +If DISABLE is non-nil disable support." + (interactive "P") + (when (bound-and-true-p which-key-mode) + (if disable + (progn + (advice-remove 'devil--read-key :around + #'devil--which-key-read-key-advice) + (remove-function which-key-this-command-keys-function + #'devil--which-key-this-command-keys) + (remove-hook 'which-key-inhibit-display-hook + #'devil--which-key-self-insert-p)) + (advice-add 'devil--read-key :around + #'devil--which-key-read-key-advice) + (add-function :override which-key-this-command-keys-function + #'devil--which-key-this-command-keys) + (add-hook 'which-key-inhibit-display-hook + #'devil--which-key-self-insert-p)))) + ;;; Utility Functions ================================================ From 609f4542e168ac17628cbc55585c4935685df793 Mon Sep 17 00:00:00 2001 From: Fabian Brosda Date: Tue, 10 Feb 2026 21:15:35 +0100 Subject: [PATCH 2/3] Make code independent of order in which devil- and which-key-mode are activated --- devil.el | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/devil.el b/devil.el index 9c9432f..9f33b5a 100644 --- a/devil.el +++ b/devil.el @@ -255,9 +255,7 @@ locally." "Local minor mode to support Devil key sequences." :lighter devil-lighter (devil--log "Mode is %s in %s" devil-mode (buffer-name)) - (if devil-mode - (devil--enable-which-key-support) - (devil--enable-which-key-support -1))) + (devil--enable-which-key-support)) ;;;###autoload (define-globalized-minor-mode @@ -653,25 +651,34 @@ passed to ORIG-FUN. This current command will be stored in (and (bound-and-true-p devil-mode) (eq this-command 'devil))) -(defun devil--enable-which-key-support (&optional disable) - "Enable support for which-key if non-nil. -If DISABLE is non-nil disable support." - (interactive "P") - (when (bound-and-true-p which-key-mode) - (if disable - (progn - (advice-remove 'devil--read-key :around - #'devil--which-key-read-key-advice) - (remove-function which-key-this-command-keys-function - #'devil--which-key-this-command-keys) - (remove-hook 'which-key-inhibit-display-hook - #'devil--which-key-self-insert-p)) - (advice-add 'devil--read-key :around - #'devil--which-key-read-key-advice) - (add-function :override which-key-this-command-keys-function - #'devil--which-key-this-command-keys) - (add-hook 'which-key-inhibit-display-hook - #'devil--which-key-self-insert-p)))) +(defun devil--enable-which-key-support () + "Enable support for which-key." + (if devil-mode + (add-hook 'which-key-mode-hook #'devil--enable-which-key-support-later) + (remove-hook 'which-key-mode-hook #'devil--enable-which-key-support-later)) + + (when (boundp which-key-mode) + (devil--enable-which-key-support-later))) + +(defun devil--enable-which-key-support-later (&optional _) + "Enable support for which-key.P +This function adds the actual logic, it is called directly, when +enabling/disabling devil-mode and is also added to the +which-key-mode-hook." + (if (and which-key-mode devil-mode) + (progn + (advice-add 'devil--read-key :around + #'devil--which-key-read-key-advice) + (add-function :override which-key-this-command-keys-function + #'devil--which-key-this-command-keys) + (add-hook 'which-key-inhibit-display-hook + #'devil--which-key-self-insert-p)) + (advice-remove 'devil--read-key + #'devil--which-key-read-key-advice) + (remove-function which-key-this-command-keys-function + #'devil--which-key-this-command-keys) + (remove-hook 'which-key-inhibit-display-hook + #'devil--which-key-self-insert-p))) ;;; Utility Functions ================================================ From 1f7e3526723c0f7cc56f6d5279dab015258e89cf Mon Sep 17 00:00:00 2001 From: Fabian Brosda Date: Wed, 11 Feb 2026 08:15:58 +0100 Subject: [PATCH 3/3] fix check, if which-key is loaded --- devil.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devil.el b/devil.el index 9f33b5a..e82f5cd 100644 --- a/devil.el +++ b/devil.el @@ -657,11 +657,11 @@ passed to ORIG-FUN. This current command will be stored in (add-hook 'which-key-mode-hook #'devil--enable-which-key-support-later) (remove-hook 'which-key-mode-hook #'devil--enable-which-key-support-later)) - (when (boundp which-key-mode) + (when (featurep 'which-key) (devil--enable-which-key-support-later))) (defun devil--enable-which-key-support-later (&optional _) - "Enable support for which-key.P + "Enable support for which-key. This function adds the actual logic, it is called directly, when enabling/disabling devil-mode and is also added to the which-key-mode-hook."