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 "<f2>")
  "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"))
master
Oleh Krehel 11 years ago
parent 8be0bff746
commit 192c5b07d0
  1. 17
      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

Loading…
Cancel
Save