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 (defun hydra-add-font-lock ()
116 "Fontify `defhydra' statements."
117 (font-lock-add-keywords
119 '(("(\\(defhydra\\)\\_> +\\(.*?\\)\\_>"
120 (1 font-lock-keyword-face)
121 (2 font-lock-type-face)))))
123 ;;* Universal Argument
124 (defvar hydra-base-map
125 (let ((map (make-sparse-keymap)))
126 (define-key map [?\C-u] 'hydra--universal-argument)
127 (define-key map [?-] 'hydra--negative-argument)
128 (define-key map [?0] 'hydra--digit-argument)
129 (define-key map [?1] 'hydra--digit-argument)
130 (define-key map [?2] 'hydra--digit-argument)
131 (define-key map [?3] 'hydra--digit-argument)
132 (define-key map [?4] 'hydra--digit-argument)
133 (define-key map [?5] 'hydra--digit-argument)
134 (define-key map [?6] 'hydra--digit-argument)
135 (define-key map [?7] 'hydra--digit-argument)
136 (define-key map [?8] 'hydra--digit-argument)
137 (define-key map [?9] 'hydra--digit-argument)
138 (define-key map [kp-0] 'hydra--digit-argument)
139 (define-key map [kp-1] 'hydra--digit-argument)
140 (define-key map [kp-2] 'hydra--digit-argument)
141 (define-key map [kp-3] 'hydra--digit-argument)
142 (define-key map [kp-4] 'hydra--digit-argument)
143 (define-key map [kp-5] 'hydra--digit-argument)
144 (define-key map [kp-6] 'hydra--digit-argument)
145 (define-key map [kp-7] 'hydra--digit-argument)
146 (define-key map [kp-8] 'hydra--digit-argument)
147 (define-key map [kp-9] 'hydra--digit-argument)
148 (define-key map [kp-subtract] 'hydra--negative-argument)
150 "Keymap that all Hydras inherit. See `universal-argument-map'.")
152 (defvar hydra-curr-map
154 "Keymap of the current Hydra called.")
156 (defun hydra--universal-argument (arg)
157 "Forward to (`universal-argument' ARG)."
159 (setq prefix-arg (if (consp arg)
160 (list (* 4 (car arg)))
164 (hydra-set-transient-map hydra-curr-map t))
166 (defun hydra--digit-argument (arg)
167 "Forward to (`digit-argument' ARG)."
169 (let ((universal-argument-map hydra-curr-map))
170 (digit-argument arg)))
172 (defun hydra--negative-argument (arg)
173 "Forward to (`negative-argument' ARG)."
175 (let ((universal-argument-map hydra-curr-map))
176 (negative-argument arg)))
179 (defvar hydra-last nil
180 "The result of the last `hydra-set-transient-map' call.")
182 (defun hydra--callablep (x)
183 "Test if X is callable."
186 (memq (car x) '(function quote)))))
188 (defun hydra--make-callable (x)
189 "Generate a callable symbol from X.
190 If X is a function symbol or a lambda, return it. Otherwise, it
191 should be a single statement. Wrap it in an interactive lambda."
192 (if (or (symbolp x) (functionp x))
198 (defun hydra--head-property (h prop &optional default)
199 "Return for Hydra head H the value of property PROP.
200 Return DEFAULT if PROP is not in H."
201 (let ((plist (if (or (stringp (cl-caddr h))
206 (plist-get plist prop)
209 (defun hydra--color (h body-color)
210 "Return the color of a Hydra head H with BODY-COLOR."
213 (or (hydra--head-property h :color) body-color)))
215 (defun hydra--face (h body-color)
216 "Return the face for a Hydra head H with BODY-COLOR."
217 (cl-case (hydra--color h body-color)
218 (blue 'hydra-face-blue)
219 (red 'hydra-face-red)
220 (amaranth 'hydra-face-amaranth)
221 (t (error "Unknown color for %S" h))))
223 (defun hydra--hint (docstring heads body-color)
224 "Generate a hint from DOCSTRING and HEADS and BODY-COLOR.
225 It's intended for the echo area, when a Hydra is active."
228 (let ((val (assoc (cadr h) alist))
229 (pstr (propertize (car h) 'face
230 (hydra--face h body-color))))
231 (unless (and (> (length h) 2)
235 (concat (cadr val) " " pstr))
239 (and (stringp (cl-caddr h)) (cl-caddr h))))
248 (concat "[%s]: " (cdr x))
251 (nreverse (mapcar #'cdr alist))
254 (defun hydra-disable ()
255 "Disable the current Hydra."
258 ((functionp hydra-last)
259 (funcall hydra-last))
262 ((boundp 'overriding-terminal-local-map)
263 (setq overriding-terminal-local-map nil))
267 (while (and (consp (car emulation-mode-map-alists))
268 (consp (caar emulation-mode-map-alists))
269 (equal (cl-cdaar emulation-mode-map-alists) ',keymap))
270 (setq emulation-mode-map-alists
271 (cdr emulation-mode-map-alists))))))
273 (defun hydra--doc (body-key body-name heads)
274 "Generate a part of Hydra docstring.
275 BODY-KEY is the body key binding.
276 BODY-NAME is the symbol that identifies the Hydra.
277 HEADS is a list of heads."
279 "Create a hydra with %s body and the heads:\n\n%s\n\n%s"
281 (format "a \"%s\"" body-key)
285 (format "\"%s\": `%S'" (car x) (cadr x)))
287 (format "The body can be accessed via `%S'." body-name)))
289 (defun hydra--make-defun (name cmd color
291 body-color body-pre body-post &optional other-post)
292 "Make a defun wrapper, using NAME, CMD, COLOR, DOC, HINT, and KEYMAP.
293 BODY-COLOR, BODY-PRE, BODY-POST, and OTHER-POST are used as well."
297 ,@(when body-pre (list body-pre))
299 (catch 'hydra-disable
302 `(,(when cmd `(call-interactively #',cmd))
307 (call-interactively #',cmd))
312 (when hydra-is-helpful
315 (hydra-set-transient-map
316 (setq hydra-curr-map ',keymap)
318 ,@(if (and (not (eq body-color 'amaranth)) body-post)
319 `((lambda () ,body-post)))))
325 (defmacro hydra-create (body heads &optional method)
326 "Create a hydra with a BODY prefix and HEADS with METHOD.
327 This will result in `global-set-key' statements with the keys
328 being the concatenation of BODY and each head in HEADS. HEADS is
329 an list of (KEY FUNCTION &optional HINT).
331 After one of the HEADS is called via BODY+KEY, it and the other
332 HEADS can be called with only KEY (no need for BODY). This state
333 is broken once any key binding that is not in HEADS is called.
335 METHOD is a lambda takes two arguments: a KEY and a COMMAND.
336 It defaults to `global-set-key'.
337 When `(keymapp METHOD)`, it becomes:
339 (lambda (key command) (define-key METHOD key command))"
341 (obsolete defhydra "0.8.0"))
344 "hydra-" (replace-regexp-in-string " " "_" body)))
345 ,(cond ((hydra--callablep method)
356 (defmacro defhydra (name body &optional docstring &rest heads)
357 "Create a Hydra - a family of functions with prefix NAME.
359 NAME should be a symbol, it will be the prefix of all functions
364 (BODY-MAP BODY-KEY &rest PLIST)
366 DOCSTRING will be displayed in the echo area to identify the
369 Functions are created on basis of HEADS, each of which has the
372 (KEY CMD &optional HINT &rest PLIST)
374 BODY-MAP is a keymap; `global-map' is used quite often. Each
375 function generated from HEADS will be bound in BODY-MAP to
376 BODY-KEY + KEY, and will set the transient map so that all
377 following heads can be called though KEY only.
379 The heads inherit their PLIST from the body and are allowed to
380 override each key. The keys recognized are :color and :bind.
383 - red (default): this head will continue the Hydra state.
384 - blue: this head will stop the Hydra state.
385 - amaranth (applies to body only): similar to red, but no binding
386 except a blue head can stop the Hydra state.
389 - nil: this head will not be bound in BODY-MAP.
390 - a lambda taking KEY and CMD used to bind a head"
391 (declare (indent defun))
392 (unless (stringp docstring)
393 (setq heads (cons docstring heads))
394 (setq docstring "hydra"))
395 (when (keywordp (car body))
396 (setq body (cons nil (cons nil body))))
397 (let* ((keymap (copy-keymap hydra-base-map))
400 (define-key keymap (kbd (car x))
401 (intern (format "%S/%s" name
402 (if (symbolp (cadr x))
404 (concat "lambda-" (car x)))))))
406 (body-name (intern (format "%S/body" name)))
407 (body-key (unless (hydra--callablep body)
409 (body-color (if (hydra--callablep body)
411 (or (plist-get (cddr body) :color)
413 (body-pre (plist-get (cddr body) :pre))
414 (body-post (plist-get (cddr body) :post))
415 (method (or (plist-get body :bind)
417 (hint (hydra--hint docstring heads body-color))
418 (doc (hydra--doc body-key body-name heads)))
419 (when (and (or body-pre body-post)
420 (version< emacs-version "24.4"))
421 (error "At least Emacs 24.4 is needed for :pre and :post"))
422 (when (and body-pre (symbolp body-pre))
423 (setq body-pre `(funcall #',body-pre)))
424 (when (and body-post (symbolp body-post))
425 (setq body-post `(funcall #',body-post)))
426 (when (eq body-color 'amaranth)
427 (if (cl-some `(lambda (h)
428 (eq (hydra--color h ',body-color) 'blue))
431 (when (cl-some `(lambda (h)
432 (eq (hydra--color h ',body-color) 'red))
434 (warn "Amaranth body color: upgrading all red heads to amaranth"))
435 (define-key keymap [t]
438 (message "An amaranth Hydra can only exit through a blue head")
439 (hydra-set-transient-map hydra-curr-map t)
440 (when hydra-is-helpful
443 (error "An amaranth Hydra must have at least one blue head in order to exit"))
444 (when hydra-keyboard-quit
445 (define-key keymap hydra-keyboard-quit
454 name (hydra--make-callable (cadr head)) (hydra--color head body-color)
455 (format "%s\n\nCall the head: `%S'." doc (cadr head))
457 body-color body-pre body-post))
459 ,@(unless (or (null body-key)
461 (hydra--callablep method))
462 `((unless (keymapp (lookup-key ,method (kbd ,body-key)))
463 (define-key ,method (kbd ,body-key) nil))))
467 (when (or body-key method)
468 (let ((bind (hydra--head-property head :bind 'default))
469 (final-key (if body-key
470 (vconcat (kbd body-key) (kbd (car head)))
472 (cond ((null bind) nil)
476 (if (hydra--callablep method)
481 (list 'function name)))
483 ((hydra--callablep bind)
484 `(funcall (function ,bind)
489 (error "Invalid :bind property %S" head))))))
491 ,(hydra--make-defun body-name nil nil doc hint keymap
492 body-color body-pre body-post
493 '(setq prefix-arg current-prefix-arg)))))
498 ;;; outline-regexp: ";;\\*+"
501 ;;; hydra.el ends here