branch: master
commit b982fe5811e896b19835570efb571af2e1040ea6
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Allow to add one head that disables the Hydra
* hydra.el (hydra-last): Store the lambda to disable the Hydra.
(hydra-create): Update.
Sometimes, I have nothing particualr on my mind to do, but I want to
stop the Hydra. I could just type "C-g", but it's possible to have
something more convenient. For instance:
(hydra-create "C-z"
'(("l" forward-char)
("h" backward-char)
("j" next-line)
("k" previous-line)
("z")))
---
hydra.el | 53 +++++++++++++++++++++++++++--------------------------
1 files changed, 27 insertions(+), 26 deletions(-)
diff --git a/hydra.el b/hydra.el
index fa21e65..f1428ab 100644
--- a/hydra.el
+++ b/hydra.el
@@ -63,6 +63,9 @@
:type 'boolean
:group 'hydra)
+(defvar hydra-last nil
+ "The result of the last `set-transient-map' call.")
+
;;;###autoload
(defmacro hydra-create (body heads &optional method)
"Create a hydra with a BODY prefix and HEADS with METHOD.
@@ -89,45 +92,43 @@ When `(keymapp METHOD)`, it becomes:
heads))
(method (cond ((null method)
'global-set-key)
-
((keymapp (eval method))
- `(lambda (key command) (define-key ,method key
command)))
-
+ `(lambda (key command)
+ (define-key ,method key command)))
(t
method)))
(hint (concat "hydra: "
(mapconcat
- (lambda (h) (if (caddr h)
- (format "[%s]: %s"
- (propertize (car h)
- 'face
'font-lock-keyword-face)
- (caddr h))
- (propertize (car h) 'face
'font-lock-keyword-face)))
+ (lambda (h)
+ (if (caddr h)
+ (format "[%s]: %s"
+ (propertize (car h)
+ 'face
'font-lock-keyword-face)
+ (caddr h))
+ (propertize (car h) 'face
'font-lock-keyword-face)))
heads ", ")
- ".")))
+ "."))
+ (doc (format
+ "Create a hydra with a \"%s\" body and the heads:\n\n%s."
+ body
+ (mapconcat
+ (lambda (x)
+ (format "\"%s\": `%S'" (car x) (cadr x)))
+ heads ",\n"))))
`(progn
(when (eq ',method 'global-set-key)
(global-set-key ,(kbd body) nil))
,@(cl-mapcar
(lambda (head name)
`(defun ,name ()
- ,(format
- "Create a hydra with a \"%s\" body and the heads:
-
-%s.
-
-Call the head: `%S'."
- body
- (mapconcat
- (lambda (x)
- (format "\"%s\": `%S'" (car x) (cadr x)))
- heads ",\n")
- (cadr head))
+ ,(format "%s\n\nCall the head: `%S'." doc (cadr head))
(interactive)
- (call-interactively #',(cadr head))
- (when hydra-is-helpful
- (message ,hint))
- (set-transient-map ',keymap t)))
+ (if (null ',(cadr head))
+ (funcall hydra-last)
+ (call-interactively #',(cadr head))
+ (when hydra-is-helpful
+ (message ,hint))
+ (setq hydra-last (set-transient-map ',keymap t)))))
heads names)
,@(cl-mapcar
(lambda (head name)