]> code.delx.au - gnu-emacs-elpa/blob - packages/hydra/README.md
Merge commit '3fa56d7925d5b24c5cd11c61bd6b3235e753a055'
[gnu-emacs-elpa] / packages / hydra / README.md
1 [![Build Status](https://travis-ci.org/abo-abo/hydra.svg?branch=master)](https://travis-ci.org/abo-abo/hydra)
2
3 This is a package for GNU Emacs that can be used to tie related
4 commands into a family of short bindings with a common prefix - a
5 Hydra.
6
7 ![hydra](http://oremacs.com/download/Hydra.png)
8
9 Once you summon the Hydra through the prefixed binding (the body + any
10 one head), all heads can be called in succession with only a short
11 extension.
12
13 The Hydra is vanquished once Hercules, any binding that isn't the
14 Hydra's head, arrives. Note that Hercules, besides vanquishing the
15 Hydra, will still serve his orignal purpose, calling his proper
16 command. This makes the Hydra very seamless, it's like a minor mode
17 that disables itself auto-magically.
18
19 ## Sample global Hydras
20 ### Zoom
21
22 ```cl
23 (defhydra hydra-zoom (global-map "<f2>")
24 "zoom"
25 ("g" text-scale-increase "in")
26 ("l" text-scale-decrease "out"))
27 ```
28
29 ### Goto-error
30
31 ```cl
32 (defhydra hydra-error (global-map "M-g")
33 "goto-error"
34 ("h" first-error "first")
35 ("j" next-error "next")
36 ("k" previous-error "prev")
37 ("v" recenter-top-bottom "recenter")
38 ("q" nil "quit"))
39 ```
40
41 ### Splitter
42
43 ```cl
44 (require 'hydra-examples)
45 (defhydra hydra-splitter (global-map "C-M-s")
46 "splitter"
47 ("h" hydra-move-splitter-left)
48 ("j" hydra-move-splitter-down)
49 ("k" hydra-move-splitter-up)
50 ("l" hydra-move-splitter-right))
51 ```
52
53 ## Using the functions generated by `defhydra`
54
55 With the example above, you can e.g.:
56
57 ```cl
58 (key-chord-define-global "tt" 'hydra-zoom/body)
59 ```
60
61 In fact, since `defhydra` returns the body symbol, you can even write
62 it like this:
63
64 ```cl
65 (key-chord-define-global
66 "tt"
67 (defhydra hydra-zoom (global-map "<f2>")
68 "zoom"
69 ("g" text-scale-increase "in")
70 ("l" text-scale-decrease "out")))
71 ```
72
73 If you like key chords so much that you don't want to touch the global
74 map at all, you can e.g.:
75
76 ```
77 (key-chord-define-global
78 "hh"
79 (defhydra hydra-error ()
80 "goto-error"
81 ("h" first-error "first")
82 ("j" next-error "next")
83 ("k" previous-error "prev")))
84 ```
85
86 You can also substitute `global-map` with any other keymap, like
87 `c++-mode-map` or `yas-minor-mode-map`.
88
89 See the [introductory blog post](http://oremacs.com/2015/01/20/introducing-hydra/) for more information.
90
91 ## Using Hydra for major-mode or minor-mode bindings
92
93 Here's an example:
94
95 ```cl
96 (defhydra lispy-vi (lispy-mode-map "C-z")
97 "vi"
98 ("l" forward-char)
99 ("h" backward-char)
100 ("j" next-line)
101 ("k" previous-line))
102 ```
103
104 ## Can Hydras can be helpful?
105
106 They can, if
107
108 ```cl
109 (setq hydra-is-helpful t)
110 ```
111
112 This is the default setting. In this case, you'll get a hint in the
113 echo area consisting of current Hydra's base comment and heads. You
114 can even add comments to the heads like this:
115
116 ```
117 (defhydra hydra-zoom (global-map "<f2>")
118 "zoom"
119 ("g" text-scale-increase "in")
120 ("l" text-scale-decrease "out"))
121 ```
122
123 With this, you'll see `zoom: [g]: in, [l]: out.` in your echo area,
124 once the zoom Hydra becomes active.
125
126 ## Colorful Hydras
127
128 Since version `0.5.0`, Hydra's heads all have a color associated with them:
129
130 - *red* (default) means the calling this head will not vanquish the Hydra
131 - *blue* means that the Hydra will be vanquished after calling this head
132
133 In all the older examples, all heads are red by default. You can specify blue heads like this:
134
135 ```cl
136 (global-set-key
137 (kbd "C-c C-v")
138 (defhydra toggle ()
139 "toggle"
140 ("a" abbrev-mode "abbrev" :color blue)
141 ("d" toggle-debug-on-error "debug" :color blue)
142 ("f" auto-fill-mode "fill" :color blue)
143 ("t" toggle-truncate-lines "truncate" :color blue)
144 ("w" whitespace-mode "whitespace" :color blue)
145 ("q" nil "cancel")))
146 ```
147
148 Or, since the heads can inherit the color from the body, the following is equivalent:
149
150 ```cl
151 (global-set-key
152 (kbd "C-c C-v")
153 (defhydra toggle (:color blue)
154 "toggle"
155 ("a" abbrev-mode "abbrev")
156 ("d" toggle-debug-on-error "debug")
157 ("f" auto-fill-mode "fill")
158 ("t" toggle-truncate-lines "truncate")
159 ("w" whitespace-mode "whitespace")
160 ("q" nil "cancel")))
161 ```
162
163 The above Hydra is very similar to this code:
164
165 ```cl
166 (global-set-key (kbd "C-c C-v t") 'toggle-truncate-lines)
167 (global-set-key (kbd "C-c C-v f") 'auto-fill-mode)
168 (global-set-key (kbd "C-c C-v a") 'abbrev-mode)
169 ```
170
171 However, there are two important differences:
172
173 - you get a hint like this right after <kbd>C-c C-v</kbd>:
174
175 toggle: [t]: truncate, [f]: fill, [a]: abbrev, [q]: cancel.
176
177 - you can cancel <kbd>C-c C-v</kbd> with a command while executing that command, instead of e.g.
178 getting an error `C-c C-v C-n is undefined` for <kbd>C-c C-v C-n</kbd>.
179
180 ## Hydras and numeric arguments
181
182 Since version `0.6.0`, for any Hydra:
183
184 - `digit-argment` can be called with <kbd>0</kbd>-<kbd>9</kbd>.
185 - `negative-argument` can be called with <kbd>-</kbd>
186 - `universal-argument` can be called with <kbd>C-u</kbd>
187
188 ## Hydras can have `:pre` and `:post` statements
189
190 Since version `0.7.0`, you can specify code that will be called before each head, and
191 after the body. For example:
192
193 ```cl
194 (global-set-key
195 (kbd "C-z")
196 (defhydra hydra-vi
197 (:pre
198 (set-cursor-color "#40e0d0")
199 :post
200 (progn
201 (set-cursor-color "#ffffff")
202 (message
203 "Thank you, come again.")))
204 "vi"
205 ("l" forward-char)
206 ("h" backward-char)
207 ("j" next-line)
208 ("k" previous-line)
209 ("q" nil "quit")))
210 ```
211
212 ## New Hydra color: amaranth
213
214 Since version `0.8.0`, a new color - amaranth, in addition to the previous red and blue, is
215 available for the Hydra body.
216
217 According to [Wikipedia](http://en.wikipedia.org/wiki/Amaranth):
218
219 > The word amaranth comes from the Greek word amaranton, meaning "unwilting" (from the
220 > verb marainesthai, meaning "wilt"). The word was applied to amaranth because it did not
221 > soon fade and so symbolized immortality.
222
223 Hydras with amaranth body are impossible to quit with any binding *except* a blue head.
224 A check for at least one blue head exists in `defhydra`, so that you don't get stuck by accident.
225
226 Here's an example of an amaranth Hydra:
227
228 ```cl
229 (global-set-key
230 (kbd "C-z")
231 (defhydra hydra-vi
232 (:pre
233 (set-cursor-color "#40e0d0")
234 :post
235 (set-cursor-color "#ffffff")
236 :color amaranth)
237 "vi"
238 ("l" forward-char)
239 ("h" backward-char)
240 ("j" next-line)
241 ("k" previous-line)
242 ("q" nil "quit")))
243 ```
244
245 The only way to exit it, is to press <kbd>q</kbd>. No other methods will work. You can
246 use an amaranth Hydra instead of a red one, if for you the cost of being able to exit only
247 though certain bindings is less than the cost of accidentally exiting a red Hydra by
248 pressing the wrong prefix.
249
250 Note that it does not make sense to define a single amaranth head, so this color can only
251 be assigned to the body. An amaranth body will always have some amaranth heads and some
252 blue heads (otherwise, it's impossible to exit), no reds.
253
254 ## Generate simple lambdas in-place:
255
256 Since version `0.9.0` it's possible to pass a single sexp instead of a function name or a lambda
257 to a head. This sexp will be wrapped in an interactive lambda. Here's an example:
258
259 ```cl
260 (defhydra hydra-launcher (:color blue)
261 "Launch"
262 ("h" man "man")
263 ("r" (browse-url "http://www.reddit.com/r/emacs/") "reddit")
264 ("w" (browse-url "http://www.emacswiki.org/") "emacswiki")
265 ("s" shell "shell")
266 ("q" nil "cancel"))
267 (global-set-key (kbd "C-c r") 'hydra-launcher/body)
268 ```