Skip to content

Commit

Permalink
Add :timeout option to hydra body
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
abo-abo committed Mar 2, 2015
1 parent 764f4b6 commit 9c68e0a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions hydra.el
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ BODY is the second argument to `defhydra'"
(interactive)
(hydra-disable)
(hydra-cleanup)
(cancel-timer hydra-timer)
nil)

(defun hydra-disable ()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c68e0a

Please sign in to comment.