1 ;;; hydra.el --- Make bindings that stick around
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel <ohwoeowho@gmail.com>
6 ;; Maintainer: Oleh Krehel <ohwoeowho@gmail.com>
7 ;; URL: https://github.com/abo-abo/hydra
10 ;; Package-Requires: ((cl-lib "0.5"))
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This package can be used to tie related commands into a family of
30 ;; short bindings with a common prefix - a Hydra.
32 ;; Once you summon the Hydra (through the prefixed binding), all the
33 ;; heads can be called in succession with only a short extension.
34 ;; The Hydra is vanquished once Hercules, any binding that isn't the
35 ;; Hydra's head, arrives. Note that Hercules, besides vanquishing the
36 ;; Hydra, will still serve his orignal purpose, calling his proper
37 ;; command. This makes the Hydra very seamless, it's like a minor
38 ;; mode that disables itself automagically.
40 ;; Here's an example Hydra, bound in the global map (you can use any
41 ;; keymap in place of `global-map'):
43 ;; (defhydra hydra-zoom (global-map "<f2>")
45 ;; ("g" text-scale-increase "in")
46 ;; ("l" text-scale-decrease "out"))
48 ;; It allows to start a command chain either like this:
49 ;; "<f2> gg4ll5g", or "<f2> lgllg".
51 ;; Here's another approach, when you just want a "callable keymap":
53 ;; (defhydra hydra-toggle (:color blue)
55 ;; ("a" abbrev-mode "abbrev")
56 ;; ("d" toggle-debug-on-error "debug")
57 ;; ("f" auto-fill-mode "fill")
58 ;; ("t" toggle-truncate-lines "truncate")
59 ;; ("w" whitespace-mode "whitespace")
60 ;; ("q" nil "cancel"))
62 ;; This binds nothing so far, but if you follow up with:
64 ;; (global-set-key (kbd "C-c C-v") 'hydra-toggle/body)
66 ;; you will have bound "C-c C-v a", "C-c C-v d" etc.
68 ;; Knowing that `defhydra' defines e.g. `hydra-toggle/body' command,
69 ;; you can nest Hydras if you wish, with `hydra-toggle/body' possibly
70 ;; becoming a blue head of another Hydra.
72 ;; Initially, Hydra shipped with a simplified `hydra-create' macro, to
73 ;; which you could hook up the examples from hydra-examples.el. It's
74 ;; better to take the examples simply as templates and use `defhydra'
75 ;; instead of `hydra-create', since it's more flexible.
81 (defalias 'hydra-set-transient-map
82 (if (fboundp 'set-transient-map)
84 'set-temporary-overlay-map))
88 "Make bindings that stick around."
92 (defcustom hydra-is-helpful t
93 "When t, display a hint with possible bindings in the echo area."
97 (defcustom hydra-keyboard-quit "
\a"
98 "This binding will quit an amaranth Hydra.
99 It's the only other way to quit it besides though a blue head.
100 It's possible to set this to nil.")
102 (defface hydra-face-red
103 '((t (:foreground "#7F0055" :bold t)))
104 "Red Hydra heads will persist indefinitely."
107 (defface hydra-face-blue
108 '((t (:foreground "#758BC6" :bold t)))
109 "Blue Hydra heads will vanquish the Hydra.")
111 (defface hydra-face-amaranth
112 '((t (:foreground "#E52B50" :bold t)))
113 "Amaranth Hydra can exit only through a blue head.")
115 ;;* Universal Argument
116 (defvar hydra-base-map
117 (let ((map (make-sparse-keymap)))
118 (define-key map [?\C-u] 'hydra--universal-argument)
119 (define-key map [?-] 'hydra--negative-argument)
120 (define-key map [?0] 'hydra--digit-argument)
121 (define-key map [?1] 'hydra--digit-argument)
122 (define-key map [?2] 'hydra--digit-argument)
123 (define-key map [?3] 'hydra--digit-argument)
124 (define-key map [?4] 'hydra--digit-argument)
125 (define-key map [?5] 'hydra--digit-argument)
126 (define-key map [?6] 'hydra--digit-argument)
127 (define-key map [?7] 'hydra--digit-argument)
128 (define-key map [?8] 'hydra--digit-argument)
129 (define-key map [?9] 'hydra--digit-argument)
130 (define-key map [kp-0] 'hydra--digit-argument)
131 (define-key map [kp-1] 'hydra--digit-argument)
132 (define-key map [kp-2] 'hydra--digit-argument)
133 (define-key map [kp-3] 'hydra--digit-argument)
134 (define-key map [kp-4] 'hydra--digit-argument)
135 (define-key map [kp-5] 'hydra--digit-argument)
136 (define-key map [kp-6] 'hydra--digit-argument)
137 (define-key map [kp-7] 'hydra--digit-argument)
138 (define-key map [kp-8] 'hydra--digit-argument)
139 (define-key map [kp-9] 'hydra--digit-argument)
140 (define-key map [kp-subtract] 'hydra--negative-argument)
142 "Keymap that all Hydras inherit. See `universal-argument-map'.")
144 (defvar hydra-curr-map
146 "Keymap of the current Hydra called.")
148 (defun hydra--universal-argument (arg)
149 "Forward to (`universal-argument' ARG)."
151 (setq prefix-arg (if (consp arg)
152 (list (* 4 (car arg)))
156 (hydra-set-transient-map hydra-curr-map t))
158 (defun hydra--digit-argument (arg)
159 "Forward to (`digit-argument' ARG)."
161 (let ((universal-argument-map hydra-curr-map))
162 (digit-argument arg)))
164 (defun hydra--negative-argument (arg)
165 "Forward to (`negative-argument' ARG)."
167 (let ((universal-argument-map hydra-curr-map))
168 (negative-argument arg)))
171 (defvar hydra-last nil
172 "The result of the last `hydra-set-transient-map' call.")
174 (defun hydra--callablep (x)
175 "Test if X is callable."
178 (memq (car x) '(function quote)))))
180 (defun hydra--make-callable (x)
181 "Generate a callable symbol from X.
182 If X is a function symbol or a lambda, return it. Otherwise, it
183 should be a single statement. Wrap it in an interactive lambda."
184 (if (or (symbolp x) (functionp x))
190 (defun hydra--head-property (h prop &optional default)
191 "Return for Hydra head H the value of property PROP.
192 Return DEFAULT if PROP is not in H."
193 (let ((plist (if (stringp (cl-caddr h))
197 (plist-get plist prop)
200 (defun hydra--color (h body-color)
201 "Return the color of a Hydra head H with BODY-COLOR."
204 (or (hydra--head-property h :color) body-color)))
206 (defun hydra--face (h body-color)
207 "Return the face for a Hydra head H with BODY-COLOR."
208 (cl-case (hydra--color h body-color)
209 (blue 'hydra-face-blue)
210 (red 'hydra-face-red)
211 (amaranth 'hydra-face-amaranth)
212 (t (error "Unknown color for %S" h))))
214 (defun hydra--hint (docstring heads body-color)
215 "Generate a hint from DOCSTRING and HEADS and BODY-COLOR.
216 It's intended for the echo area, when a Hydra is active."
222 (if (stringp (cl-caddr h))
223 (concat "[%s]: " (cl-caddr h))
227 (hydra--face h body-color))))
230 (defun hydra-disable ()
231 "Disable the current Hydra."
234 ((functionp hydra-last)
235 (funcall hydra-last))
238 ((boundp 'overriding-terminal-local-map)
239 (setq overriding-terminal-local-map nil))
243 (while (and (consp (car emulation-mode-map-alists))
244 (consp (caar emulation-mode-map-alists))
245 (equal (cl-cdaar emulation-mode-map-alists) ',keymap))
246 (setq emulation-mode-map-alists
247 (cdr emulation-mode-map-alists))))))
249 (defun hydra--doc (body-key body-name heads)
250 "Generate a part of Hydra docstring.
251 BODY-KEY is the body key binding.
252 BODY-NAME is the symbol that identifies the Hydra.
253 HEADS is a list of heads."
255 "Create a hydra with %s body and the heads:\n\n%s\n\n%s"
257 (format "a \"%s\"" body-key)
261 (format "\"%s\": `%S'" (car x) (cadr x)))
263 (format "The body can be accessed via `%S'." body-name)))
265 (defun hydra--make-defun (name cmd color
267 body-color body-pre body-post &optional other-post)
268 "Make a defun wrapper, using NAME, CMD, COLOR, DOC, HINT, and KEYMAP.
269 BODY-COLOR, BODY-PRE, BODY-POST, and OTHER-POST are used as well."
273 ,@(when body-pre (list body-pre))
275 (catch 'hydra-disable
278 `(,(when cmd `(call-interactively #',cmd))
283 (call-interactively #',cmd))
288 (when hydra-is-helpful
291 (hydra-set-transient-map
292 (setq hydra-curr-map ',keymap)
294 ,@(if (and (not (eq body-color 'amaranth)) body-post)
295 `((lambda () ,body-post)))))
301 (defmacro hydra-create (body heads &optional method)
302 "Create a hydra with a BODY prefix and HEADS with METHOD.
303 This will result in `global-set-key' statements with the keys
304 being the concatenation of BODY and each head in HEADS. HEADS is
305 an list of (KEY FUNCTION &optional HINT).
307 After one of the HEADS is called via BODY+KEY, it and the other
308 HEADS can be called with only KEY (no need for BODY). This state
309 is broken once any key binding that is not in HEADS is called.
311 METHOD is a lambda takes two arguments: a KEY and a COMMAND.
312 It defaults to `global-set-key'.
313 When `(keymapp METHOD)`, it becomes:
315 (lambda (key command) (define-key METHOD key command))"
317 (obsolete defhydra "0.8.0"))
320 "hydra-" (replace-regexp-in-string " " "_" body)))
321 ,(cond ((hydra--callablep method)
332 (defmacro defhydra (name body &optional docstring &rest heads)
333 "Create a Hydra - a family of functions with prefix NAME.
335 NAME should be a symbol, it will be the prefix of all functions
340 (BODY-MAP BODY-KEY &rest PLIST)
342 DOCSTRING will be displayed in the echo area to identify the
345 Functions are created on basis of HEADS, each of which has the
348 (KEY CMD &optional HINT &rest PLIST)
350 BODY-MAP is a keymap; `global-map' is used quite often. Each
351 function generated from HEADS will be bound in BODY-MAP to
352 BODY-KEY + KEY, and will set the transient map so that all
353 following heads can be called though KEY only.
355 The heads inherit their PLIST from the body and are allowed to
356 override each key. The keys recognized are :color and :bind.
359 - red (default): this head will continue the Hydra state.
360 - blue: this head will stop the Hydra state.
361 - amaranth (applies to body only): similar to red, but no binding
362 except a blue head can stop the Hydra state.
365 - nil: this head will not be bound in BODY-MAP.
366 - a lambda taking KEY and CMD used to bind a head"
368 (unless (stringp docstring)
369 (setq heads (cons docstring heads))
370 (setq docstring "hydra"))
371 (when (keywordp (car body))
372 (setq body (cons nil (cons nil body))))
373 (let* ((keymap (copy-keymap hydra-base-map))
376 (define-key keymap (kbd (car x))
377 (intern (format "%S/%s" name
378 (if (symbolp (cadr x))
380 (concat "lambda-" (car x)))))))
382 (body-name (intern (format "%S/body" name)))
383 (body-key (unless (hydra--callablep body)
385 (body-color (if (hydra--callablep body)
387 (or (plist-get (cddr body) :color)
389 (body-pre (plist-get (cddr body) :pre))
390 (body-post (plist-get (cddr body) :post))
391 (method (or (plist-get body :bind)
393 (hint (hydra--hint docstring heads body-color))
394 (doc (hydra--doc body-key body-name heads)))
395 (when (and (or body-pre body-post)
396 (version< emacs-version "24.4"))
397 (error "At least Emacs 24.4 is needed for :pre and :post"))
398 (when (and body-pre (symbolp body-pre))
399 (setq body-pre `(funcall #',body-pre)))
400 (when (and body-post (symbolp body-post))
401 (setq body-post `(funcall #',body-post)))
402 (when (eq body-color 'amaranth)
403 (if (cl-some `(lambda (h)
404 (eq (hydra--color h ',body-color) 'blue))
406 (define-key keymap [t]
409 (message "An amaranth Hydra can only exit through a blue head")
410 (hydra-set-transient-map hydra-curr-map t)
411 (when hydra-is-helpful
414 (error "An amaranth Hydra must have at least one blue head in order to exit"))
415 (when hydra-keyboard-quit
416 (define-key keymap hydra-keyboard-quit
425 name (hydra--make-callable (cadr head)) (hydra--color head body-color)
426 (format "%s\n\nCall the head: `%S'." doc (cadr head))
428 body-color body-pre body-post))
430 ,@(unless (or (null body-key)
432 (hydra--callablep method))
433 `((unless (keymapp (lookup-key ,method (kbd ,body-key)))
434 (define-key ,method (kbd ,body-key) nil))))
438 (when (or body-key method)
439 (let ((bind (hydra--head-property head :bind 'default))
440 (final-key (if body-key
441 (vconcat (kbd body-key) (kbd (car head)))
443 (cond ((null bind) nil)
447 (if (hydra--callablep method)
452 (list 'function name)))
454 ((hydra--callablep bind)
455 `(funcall (function ,bind)
460 (error "Invalid :bind property %S" head))))))
462 ,(hydra--make-defun body-name nil nil doc hint keymap
463 body-color body-pre body-post
464 '(setq prefix-arg current-prefix-arg)))))
469 ;;; outline-regexp: ";;\\*+"
472 ;;; hydra.el ends here