Skip to content

Commit e8bbb70

Browse files
committed
Add :pre and :post clauses to Hydra body
* hydra.el (defhydra): the PLIST part of BODY argument now recognizes :pre and :post keys. These should be single Elisp statements, wrappable in a lambda. When you need more than one statement, use a `progn'. :pre will be called by `hydra-foo/body', as well as by all heads. :post will be called by the blue heads, as well as on Hydra termination by a command that isn't a head. Fixes #16. An Example: (global-set-key (kbd "C-z") (defhydra hydra-vi (:pre (set-cursor-color "#40e0d0") :post (set-cursor-color "#ffffff")) "vi" ("l" forward-char) ("h" backward-char) ("j" next-line) ("k" previous-line) ("q" nil "quit")))
1 parent e21d1d8 commit e8bbb70

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

hydra.el

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Oleh Krehel <ohwoeowho@gmail.com>
66
;; Maintainer: Oleh Krehel <ohwoeowho@gmail.com>
77
;; URL: https://github.com/abo-abo/hydra
8-
;; Version: 0.6.1
8+
;; Version: 0.7.0
99
;; Keywords: bindings
1010
;; Package-Requires: ((cl-lib "0.5"))
1111

@@ -116,7 +116,7 @@
116116
(define-key map [kp-9] 'hydra--digit-argument)
117117
(define-key map [kp-subtract] 'hydra--negative-argument)
118118
map)
119-
"Keymap that all Hydras inherit. See `universal-argument-map'.")
119+
"Keymap that all Hydras inherit. See `universal-argument-map'.")
120120

121121
(defvar hydra-curr-map
122122
(make-sparse-keymap)
@@ -130,7 +130,7 @@
130130
(if (eq arg '-)
131131
(list -4)
132132
'(4))))
133-
(hydra-set-transient-map hydra-curr-map))
133+
(hydra-set-transient-map hydra-curr-map t))
134134

135135
(defun hydra--digit-argument (arg)
136136
"Forward to (`digit-argument' ARG)."
@@ -298,21 +298,28 @@ in turn can be either red or blue."
298298
'red
299299
(or (plist-get (cddr body) :color)
300300
'red)))
301+
(body-pre (plist-get (cddr body) :pre))
302+
(body-post (plist-get (cddr body) :post))
301303
(method (if (hydra--callablep body)
302304
body
303305
(car body)))
304306
(hint (hydra--hint docstring heads body-color))
305307
(doc (hydra--doc body-key body-name heads)))
308+
(when (and (or body-pre body-post)
309+
(version< emacs-version "24.4"))
310+
(error "At least Emacs 24.4 is needed for :pre and :post"))
306311
`(progn
307312
,@(cl-mapcar
308313
(lambda (head name)
309314
`(defun ,name ()
310315
,(format "%s\n\nCall the head: `%S'." doc (cadr head))
311316
(interactive)
317+
,@(if body-pre (list body-pre))
312318
,@(if (eq (hydra--color head body-color) 'blue)
313319
`((hydra-disable)
314320
,@(unless (null (cadr head))
315-
`((call-interactively #',(cadr head)))))
321+
`((call-interactively #',(cadr head))))
322+
,@(if body-post (list body-post)))
316323
`((catch 'hydra-disable
317324
(hydra-disable)
318325
(condition-case err
@@ -325,7 +332,10 @@ in turn can be either red or blue."
325332
(when hydra-is-helpful
326333
(message ,hint))
327334
(setq hydra-last
328-
(hydra-set-transient-map (setq hydra-curr-map ',keymap) t)))))))
335+
(hydra-set-transient-map
336+
(setq hydra-curr-map ',keymap)
337+
t
338+
,@(if body-post `((lambda () ,body-post))))))))))
329339
heads names)
330340
,@(unless (or (null body-key)
331341
(null method)
@@ -347,10 +357,14 @@ in turn can be either red or blue."
347357
(defun ,body-name ()
348358
,doc
349359
(interactive)
360+
,@(if body-pre (list body-pre))
350361
(when hydra-is-helpful
351362
(message ,hint))
352363
(setq hydra-last
353-
(hydra-set-transient-map ',keymap t))))))
364+
(hydra-set-transient-map
365+
',keymap
366+
t
367+
,@(if body-post `((lambda () ,body-post)))))))))
354368

355369
(provide 'hydra)
356370

0 commit comments

Comments
 (0)