branch: master
commit 9c68e0af394ab742419779e2a1df894cbbfd8fcb
Author: Oleh Krehel <ohwoeo...@gmail.com>
Commit: Oleh Krehel <ohwoeo...@gmail.com>

    Add :timeout option to hydra body
    
    * hydra.el (hydra--make-defun): Call `hydra-timeout' with :timeout if
      it's given.
    (hydra-timer): New var to hold the timer.
    (hydra-timeout): New function to call `hydra-keyboard-quit' with delay.
    (hydra-keyboard-quit): Cancel `hydra-timeout' timer.
    
    Re #34.
---
 hydra.el |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/hydra.el b/hydra.el
index 46465c8..308de99 100644
--- a/hydra.el
+++ b/hydra.el
@@ -370,6 +370,7 @@ BODY is the second argument to `defhydra'"
   (interactive)
   (hydra-disable)
   (hydra-cleanup)
+  (cancel-timer hydra-timer)
   nil)
 
 (defun hydra-disable ()
@@ -552,7 +553,8 @@ OTHER-POST is an optional extension to the :post key of 
BODY."
                  (format "%s\n\nCall the head: `%S'." doc (cadr head))
                doc))
         (hint (intern (format "%S/hint" name)))
-        (body-color (hydra--body-color body)))
+        (body-color (hydra--body-color body))
+        (body-timeout (plist-get body :timeout)))
     `(defun ,name ()
        ,doc
        (interactive)
@@ -585,7 +587,9 @@ OTHER-POST is an optional extension to the :post key of 
BODY."
                                    body-post)
                                   `(lambda () (hydra-cleanup) ,body-post)
                                   `(lambda () (hydra-cleanup)))))
-                      ,other-post)))))))
+                      ,(or other-post
+                           (when body-timeout
+                             `(hydra-timeout ,body-timeout))))))))))
 
 (defun hydra-pink-fallback ()
   "On intercepting a non-head, try to run it."
@@ -744,6 +748,22 @@ NAMES should be defined by `defhydradio' or similar."
   (dolist (n names)
     (set n (aref (get n 'range) 0))))
 
+(defvar hydra-timer (timer-create)
+  "Timer for `hydra-timeout'.")
+
+(defun hydra-timeout (secs &optional function)
+  "In SECS seconds call FUNCTION.
+FUNCTION defaults to `hydra-disable'.
+Cancel the previous `hydra-timeout'."
+  (cancel-timer hydra-timer)
+  (setq hydra-timer (timer-create))
+  (timer-set-time hydra-timer
+                  (timer-relative-time nil secs))
+  (timer-set-function
+   hydra-timer
+   (or function #'hydra-keyboard-quit))
+  (timer-activate hydra-timer))
+
 ;;* Macros
 ;;** defhydra
 ;;;###autoload

Reply via email to