]> code.delx.au - gnu-emacs-elpa/blob - packages/hydra/README.md
Merge commit '7eca7d023c95bc21c7838467b3a58d549afaf68d'
[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 ### Community wiki
54 A few useful hydras are aggregated in projects [community wiki](https://github.com/abo-abo/hydra/wiki/Hydras%20by%20Topic). Feel free to add your own or edit existing ones.
55
56 ## Using the functions generated by `defhydra`
57
58 With the example above, you can e.g.:
59
60 ```cl
61 (key-chord-define-global "tt" 'hydra-zoom/body)
62 ```
63
64 In fact, since `defhydra` returns the body symbol, you can even write
65 it like this:
66
67 ```cl
68 (key-chord-define-global
69 "tt"
70 (defhydra hydra-zoom (global-map "<f2>")
71 "zoom"
72 ("g" text-scale-increase "in")
73 ("l" text-scale-decrease "out")))
74 ```
75
76 If you like key chords so much that you don't want to touch the global
77 map at all, you can e.g.:
78
79 ```
80 (key-chord-define-global
81 "hh"
82 (defhydra hydra-error ()
83 "goto-error"
84 ("h" first-error "first")
85 ("j" next-error "next")
86 ("k" previous-error "prev")))
87 ```
88
89 You can also substitute `global-map` with any other keymap, like
90 `c++-mode-map` or `yas-minor-mode-map`.
91
92 See the [introductory blog post](http://oremacs.com/2015/01/20/introducing-hydra/) for more information.
93
94 ## Using Hydra for major-mode or minor-mode bindings
95
96 Here's an example:
97
98 ```cl
99 (defhydra lispy-vi (lispy-mode-map "C-z")
100 "vi"
101 ("l" forward-char)
102 ("h" backward-char)
103 ("j" next-line)
104 ("k" previous-line))
105 ```
106
107 ## Can Hydras can be helpful?
108
109 They can, if
110
111 ```cl
112 (setq hydra-is-helpful t)
113 ```
114
115 This is the default setting. In this case, you'll get a hint in the
116 echo area consisting of current Hydra's base comment and heads. You
117 can even add comments to the heads like this:
118
119 ```
120 (defhydra hydra-zoom (global-map "<f2>")
121 "zoom"
122 ("g" text-scale-increase "in")
123 ("l" text-scale-decrease "out"))
124 ```
125
126 With this, you'll see `zoom: [g]: in, [l]: out.` in your echo area,
127 once the zoom Hydra becomes active.
128
129 ## Colorful Hydras
130
131 Since version `0.5.0`, Hydra's heads all have a color associated with them:
132
133 - *red* (default) means the calling this head will not vanquish the Hydra
134 - *blue* means that the Hydra will be vanquished after calling this head
135
136 In all the older examples, all heads are red by default. You can specify blue heads like this:
137
138 ```cl
139 (global-set-key
140 (kbd "C-c C-v")
141 (defhydra toggle ()
142 "toggle"
143 ("a" abbrev-mode "abbrev" :color blue)
144 ("d" toggle-debug-on-error "debug" :color blue)
145 ("f" auto-fill-mode "fill" :color blue)
146 ("t" toggle-truncate-lines "truncate" :color blue)
147 ("w" whitespace-mode "whitespace" :color blue)
148 ("q" nil "cancel")))
149 ```
150
151 Or, since the heads can inherit the color from the body, the following is equivalent:
152
153 ```cl
154 (global-set-key
155 (kbd "C-c C-v")
156 (defhydra toggle (:color blue)
157 "toggle"
158 ("a" abbrev-mode "abbrev")
159 ("d" toggle-debug-on-error "debug")
160 ("f" auto-fill-mode "fill")
161 ("t" toggle-truncate-lines "truncate")
162 ("w" whitespace-mode "whitespace")
163 ("q" nil "cancel")))
164 ```
165
166 The above Hydra is very similar to this code:
167
168 ```cl
169 (global-set-key (kbd "C-c C-v t") 'toggle-truncate-lines)
170 (global-set-key (kbd "C-c C-v f") 'auto-fill-mode)
171 (global-set-key (kbd "C-c C-v a") 'abbrev-mode)
172 ```
173
174 However, there are two important differences:
175
176 - you get a hint like this right after <kbd>C-c C-v</kbd>:
177
178 toggle: [t]: truncate, [f]: fill, [a]: abbrev, [q]: cancel.
179
180 - you can cancel <kbd>C-c C-v</kbd> with a command while executing that command, instead of e.g.
181 getting an error `C-c C-v C-n is undefined` for <kbd>C-c C-v C-n</kbd>.
182
183 ## Hydras and numeric arguments
184
185 Since version `0.6.0`, for any Hydra:
186
187 - `digit-argment` can be called with <kbd>0</kbd>-<kbd>9</kbd>.
188 - `negative-argument` can be called with <kbd>-</kbd>
189 - `universal-argument` can be called with <kbd>C-u</kbd>
190
191 ## Hydras can have `:pre` and `:post` statements
192
193 Since version `0.7.0`, you can specify code that will be called before each head, and
194 after the body. For example:
195
196 ```cl
197 (global-set-key
198 (kbd "C-z")
199 (defhydra hydra-vi
200 (:pre
201 (set-cursor-color "#40e0d0")
202 :post
203 (progn
204 (set-cursor-color "#ffffff")
205 (message
206 "Thank you, come again.")))
207 "vi"
208 ("l" forward-char)
209 ("h" backward-char)
210 ("j" next-line)
211 ("k" previous-line)
212 ("q" nil "quit")))
213 ```
214
215 ## New Hydra color: amaranth
216
217 Since version `0.8.0`, a new color - amaranth, in addition to the previous red and blue, is
218 available for the Hydra body.
219
220 According to [Wikipedia](http://en.wikipedia.org/wiki/Amaranth):
221
222 > The word amaranth comes from the Greek word amaranton, meaning "unwilting" (from the
223 > verb marainesthai, meaning "wilt"). The word was applied to amaranth because it did not
224 > soon fade and so symbolized immortality.
225
226 Hydras with amaranth body are impossible to quit with any binding *except* a blue head.
227 A check for at least one blue head exists in `defhydra`, so that you don't get stuck by accident.
228
229 Here's an example of an amaranth Hydra:
230
231 ```cl
232 (global-set-key
233 (kbd "C-z")
234 (defhydra hydra-vi
235 (:pre
236 (set-cursor-color "#40e0d0")
237 :post
238 (set-cursor-color "#ffffff")
239 :color amaranth)
240 "vi"
241 ("l" forward-char)
242 ("h" backward-char)
243 ("j" next-line)
244 ("k" previous-line)
245 ("q" nil "quit")))
246 ```
247
248 The only way to exit it, is to press <kbd>q</kbd>. No other methods will work. You can
249 use an amaranth Hydra instead of a red one, if for you the cost of being able to exit only
250 though certain bindings is less than the cost of accidentally exiting a red Hydra by
251 pressing the wrong prefix.
252
253 Note that it does not make sense to define a single amaranth head, so this color can only
254 be assigned to the body. An amaranth body will always have some amaranth heads and some
255 blue heads (otherwise, it's impossible to exit), no reds.
256
257 ## Generate simple lambdas in-place:
258
259 Since version `0.9.0` it's possible to pass a single sexp instead of a function name or a lambda
260 to a head. This sexp will be wrapped in an interactive lambda. Here's an example:
261
262 ```cl
263 (defhydra hydra-launcher (:color blue)
264 "Launch"
265 ("h" man "man")
266 ("r" (browse-url "http://www.reddit.com/r/emacs/") "reddit")
267 ("w" (browse-url "http://www.emacswiki.org/") "emacswiki")
268 ("s" shell "shell")
269 ("q" nil "cancel"))
270 (global-set-key (kbd "C-c r") 'hydra-launcher/body)
271 ```
272
273 ## Define Hydra heads that don't show up in the hint at all
274
275 This can be done by setting the head's hint explicitly to `nil`, instead of the usual string.
276
277 ## Use a dedicated window for Hydra hints
278
279 Since version `0.10.0`, setting `hydra-lv` to `t` (the default setting) will make it use a dedicated
280 window right above the Echo Area for hints. This has the advantage that you can immediately see
281 any `message` output from the functions that you call, since Hydra no longer uses `message` to display
282 the hint. You can still have the old behavior by setting `hydra-lv` to `nil`.
283
284 ## Color table
285
286
287 | Body Color | Head Inherited | Executing NON-HEADS | Executing HEADS |
288 |------------+----------------+-----------------------+-----------------|
289 | amaranth | red | Disallow and Continue | Continue |
290 | teal | blue | Disallow and Continue | Quit |
291 | pink | red | Allow and Continue | Continue |
292 | red | red | Allow and Quit | Continue |
293 | blue | blue | Allow and Quit | Quit |
294
295 ## Color to toggle correspondence
296
297 By popular demand, an alternative syntax has been implemented that translates to colors without
298 using them in the syntax. `:exit` can be used both in body (heads will inherit) and in heads
299 (possible to override body). `:exit` is nil by default, corresponding to `red` head; you don't need
300 to set it explicitly to nil. `:foreign-keys` can be used only in body and can be either nil (default),
301 `warn` or `run`.
302
303 | color | toggle |
304 |----------+----------------------------|
305 | red | |
306 | blue | :exit t |
307 | amaranth | :foreign-keys warn |
308 | teal | :foreign-keys warn :exit t |
309 | pink | :foreign-keys run |