New in Hydra - :pre and :post clauses
04 Feb 2015Only three commits happened since 0.6.1, but already 0.7.0 has to
be released, since the behavior has changed slightly.
The ultimate window switching setup, revised
The code from the last post had to be changed.
Note that only ace-window-related heads are affected, since ace-window uses set-transient-map as well.
This was necessary to fix issue #15 (unable to use goto-line in a Hydra, since it requires input).
(global-set-key
(kbd "C-M-o")
(defhydra hydra-window ()
"window"
("h" windmove-left)
("j" windmove-down)
("k" windmove-up)
("l" windmove-right)
("a" (lambda ()
(interactive)
(ace-window 1)
(add-hook 'ace-window-end-once-hook
'hydra-window/body)
(throw 'hydra-disable t))
"ace")
("v" (lambda ()
(interactive)
(split-window-right)
(windmove-right))
"vert")
("x" (lambda ()
(interactive)
(split-window-below)
(windmove-down))
"horz")
("s" (lambda ()
(interactive)
(ace-window 4)
(add-hook 'ace-window-end-once-hook
'hydra-window/body)
(throw 'hydra-disable t))
"swap")
("t" transpose-frame "'")
("d" (lambda ()
(interactive)
(ace-window 16)
(add-hook 'ace-window-end-once-hook
'hydra-window/body)
(throw 'hydra-disable t))
"del")
("o" delete-other-windows "one" :color blue)
("i" ace-maximize-window "ace-one" :color blue)
("q" nil "cancel")))
You can also see an awesome addition sneak in, with transpose-frame
on t.
Technical note
If you look closely at the code, you'll see the ace-window-related
lambdas throwing the hydra-disable symbol. If the Hydra code catches
that symbol, it will not call set-transient-map again, effectively
making the head that threw the symbol blue. In this example, though,
ace-window-related functions are still red, since the Hydra is
resumed by calling hydra-window/body in ace-window-end-once-hook.
This is a trick that I have to play because of how ace-jump-mode
works. Hopefully, it won't be needed for other commands. But still,
it's a simple trick that you can use if you want to have a head quit
conditionally, or something.
goto-line ad infinum
(defhydra hydra-test (global-map "M-g")
("g" goto-line "goto-line"))
With the Hydra above, it's possible to:
- M-g g
10RET to go to line10 - g
20RET to go to line20 - g
50RET to go to line50etc.
Change the cursor color when a Hydra is active
Here's how the new :pre and :post statements work:
(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")))
In this example, the cursor color will change for the duration of the Hydra.
Both :pre and :post should match to a single Elisp statement; you can use progn to tie
a bunch of statements together if you want.
A more evil helm
Lit Wakefield has a nice article on
using hydra and helm together. Do check it out, I'll certainly steal some of his code.
Outro
I hope that you don't have an objection to Hydra being developed at a fast pace with many features popping up. I try my best to keep things backwards compatible, but I really want to quickly fix the areas where the code is lacking. Thanks to all the people contributing issues, especially @atykhonov and @nandryshak for the last two, which are the core of this version bump.