From 192c5b07d0d2bafb67015ecfb2bccdda95feea86 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Thu, 23 Apr 2015 11:10:32 +0200 Subject: [PATCH] Allow heads to conditionally exit * hydra.el (hydra-deactivate): New defvar. (hydra-set-transient-map): When `hydra-deactivate' is set, quit. (hydra-disable): Make sure that `hydra-deactivate' is reset back to nil. Fixes #115 Example: zoom in at most 5 times, then quit. (defvar hydra-zoom-amount 1) (defhydra hydra-zoom (global-map "") "zoom" ("g" (if (>= hydra-zoom-amount 5) (progn (setq hydra-zoom-amount 1) (setq hydra-deactivate t)) (cl-incf hydra-zoom-amount) (call-interactively 'text-scale-increase)) "in") ("l" text-scale-decrease "out")) --- hydra.el | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/hydra.el b/hydra.el index 89254b6..fdd3847 100644 --- a/hydra.el +++ b/hydra.el @@ -88,6 +88,10 @@ (defvar hydra-curr-foreign-keys nil "The current :foreign-keys behavior.") +(defvar hydra-deactivate nil + "If a Hydra head sets this to t, exit the Hydra even if the + head wasn't designated for exiting.") + (defun hydra-set-transient-map (keymap on-exit &optional foreign-keys) "Set KEYMAP to the highest priority. @@ -99,11 +103,13 @@ that isn't in KEYMAP is called: nil: deactivate KEYMAP and run the command. run: keep KEYMAP and run the command. warn: keep KEYMAP and issue a warning instead of running the command." - (setq hydra-curr-map keymap) - (setq hydra-curr-on-exit on-exit) - (setq hydra-curr-foreign-keys foreign-keys) - (add-hook 'pre-command-hook 'hydra--clearfun) - (internal-push-keymap keymap 'overriding-terminal-local-map)) + (if hydra-deactivate + (hydra-keyboard-quit) + (setq hydra-curr-map keymap) + (setq hydra-curr-on-exit on-exit) + (setq hydra-curr-foreign-keys foreign-keys) + (add-hook 'pre-command-hook 'hydra--clearfun) + (internal-push-keymap keymap 'overriding-terminal-local-map))) (defun hydra--clearfun () "Disable the current Hydra unless `this-command' is a head." @@ -128,6 +134,7 @@ warn: keep KEYMAP and issue a warning instead of running the command." (defun hydra-disable () "Disable the current Hydra." + (setq hydra-deactivate nil) (remove-hook 'pre-command-hook 'hydra--clearfun) (dolist (frame (frame-list)) (with-selected-frame frame