]> code.delx.au - gnu-emacs-elpa/commitdiff
Call :post for :timeout
authorOleh Krehel <ohwoeowho@gmail.com>
Fri, 20 Mar 2015 14:27:00 +0000 (15:27 +0100)
committerOleh Krehel <ohwoeowho@gmail.com>
Fri, 20 Mar 2015 14:27:00 +0000 (15:27 +0100)
* hydra.el (hydra--make-defun): When both :timeout and :post are given,
  if `hydra-timeout' runs out, it will call :post before
  `hydra-keyboard-quit'.
(hydra-timeout): Update to call function if it's given. Always call
`hydra-keyboard-quit' afterwards.

For this example code:

(global-set-key
 (kbd "C-c t")
 (defhydra test-hydra-b
     (:timeout 2.0 :post (message "test b quit"))
   "test b"
   ("x" (message "x"))
   ("y" (message "y"))
   ("q" nil "quit")))

The message "test b quit" is issued:

- on "q"

- on "x" or "y", after one of them has started the timer, and the timer
  ran out. The timer is set to 2.0 seconds, and is reset each time "x"
  or "y" is pressed.

- not on a plain "C-c t"

Fixes #34

hydra.el

index 2680f386634702437776bec024724879435f2b80..c5a74a235fefd7afd2407ef7aedca808859e793a 100644 (file)
--- a/hydra.el
+++ b/hydra.el
@@ -610,7 +610,10 @@ OTHER-POST is an optional extension to the :post key of BODY."
                                   `(lambda () (hydra-cleanup)))))
                       ,(or other-post
                            (when body-timeout
-                             `(hydra-timeout ,body-timeout))))))))))
+                             (list 'hydra-timeout
+                                   body-timeout
+                                   (when body-post
+                                     (hydra--make-callable body-post))))))))))))
 
 (defun hydra-pink-fallback ()
   "On intercepting a non-head, try to run it."
@@ -774,8 +777,7 @@ NAMES should be defined by `defhydradio' or similar."
   "Timer for `hydra-timeout'.")
 
 (defun hydra-timeout (secs &optional function)
-  "In SECS seconds call FUNCTION.
-FUNCTION defaults to `hydra-disable'.
+  "In SECS seconds call FUNCTION, then `hydra-keyboard-quit'.
 Cancel the previous `hydra-timeout'."
   (cancel-timer hydra-timer)
   (setq hydra-timer (timer-create))
@@ -783,7 +785,10 @@ Cancel the previous `hydra-timeout'."
                   (timer-relative-time (current-time) secs))
   (timer-set-function
    hydra-timer
-   (or function #'hydra-keyboard-quit))
+   `(lambda ()
+      ,(when function
+             `(funcall ,function))
+      (hydra-keyboard-quit)))
   (timer-activate hydra-timer))
 
 ;;* Macros