emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master ad771a4 03/17: Move defun-creating code into a defun


From: Oleh Krehel
Subject: [elpa] master ad771a4 03/17: Move defun-creating code into a defun
Date: Tue, 10 Feb 2015 07:13:23 +0000

branch: master
commit ad771a42945be630ca2faa246b597cd9b2acc550
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Move defun-creating code into a defun
    
    * hydra.el (hydra--doc): New function.
    (defhydra): Simplify.
    
    * hydra-test.el: Update tests.
---
 hydra-test.el |  567 +++++++++++++++++++++++++++++----------------------------
 hydra.el      |   78 ++++----
 2 files changed, 331 insertions(+), 314 deletions(-)

diff --git a/hydra-test.el b/hydra-test.el
index 7ae5fd0..57409fd 100644
--- a/hydra-test.el
+++ b/hydra-test.el
@@ -52,39 +52,40 @@ Call the head: `first-error'."
                  ((debug error)
                   (message "%S" err)
                   (sit-for 0.8)
-                  nil))
-               (when hydra-is-helpful (message #("error: [h]: first, [j]: 
next, [k]: prev." 8 9 (face hydra-face-red)
-                                                 20 21 (face hydra-face-red)
-                                                 31 32 (face hydra-face-red))))
-               (setq hydra-last
-                     (hydra-set-transient-map
-                      (setq hydra-curr-map '(keymap (107 . 
hydra-error/previous-error)
-                                             (106 . hydra-error/next-error)
-                                             (104 . hydra-error/first-error)
-                                             (kp-subtract . 
hydra--negative-argument)
-                                             (kp-9 . hydra--digit-argument)
-                                             (kp-8 . hydra--digit-argument)
-                                             (kp-7 . hydra--digit-argument)
-                                             (kp-6 . hydra--digit-argument)
-                                             (kp-5 . hydra--digit-argument)
-                                             (kp-4 . hydra--digit-argument)
-                                             (kp-3 . hydra--digit-argument)
-                                             (kp-2 . hydra--digit-argument)
-                                             (kp-1 . hydra--digit-argument)
-                                             (kp-0 . hydra--digit-argument)
-                                             (57 . hydra--digit-argument)
-                                             (56 . hydra--digit-argument)
-                                             (55 . hydra--digit-argument)
-                                             (54 . hydra--digit-argument)
-                                             (53 . hydra--digit-argument)
-                                             (52 . hydra--digit-argument)
-                                             (51 . hydra--digit-argument)
-                                             (50 . hydra--digit-argument)
-                                             (49 . hydra--digit-argument)
-                                             (48 . hydra--digit-argument)
-                                             (45 . hydra--negative-argument)
-                                             (21 . hydra--universal-argument)))
-                      t))))
+                  nil)))
+             (when hydra-is-helpful (message #("error: [h]: first, [j]: next, 
[k]: prev." 8 9 (face hydra-face-red)
+                                               20 21 (face hydra-face-red)
+                                               31 32 (face hydra-face-red))))
+             (setq hydra-last
+                   (hydra-set-transient-map
+                    (setq hydra-curr-map
+                          (quote (keymap (107 . hydra-error/previous-error)
+                                         (106 . hydra-error/next-error)
+                                         (104 . hydra-error/first-error)
+                                         (kp-subtract . 
hydra--negative-argument)
+                                         (kp-9 . hydra--digit-argument)
+                                         (kp-8 . hydra--digit-argument)
+                                         (kp-7 . hydra--digit-argument)
+                                         (kp-6 . hydra--digit-argument)
+                                         (kp-5 . hydra--digit-argument)
+                                         (kp-4 . hydra--digit-argument)
+                                         (kp-3 . hydra--digit-argument)
+                                         (kp-2 . hydra--digit-argument)
+                                         (kp-1 . hydra--digit-argument)
+                                         (kp-0 . hydra--digit-argument)
+                                         (57 . hydra--digit-argument)
+                                         (56 . hydra--digit-argument)
+                                         (55 . hydra--digit-argument)
+                                         (54 . hydra--digit-argument)
+                                         (53 . hydra--digit-argument)
+                                         (52 . hydra--digit-argument)
+                                         (51 . hydra--digit-argument)
+                                         (50 . hydra--digit-argument)
+                                         (49 . hydra--digit-argument)
+                                         (48 . hydra--digit-argument)
+                                         (45 . hydra--negative-argument)
+                                         (21 . hydra--universal-argument))))
+                    t)))
       (defun hydra-error/next-error nil "Create a hydra with a \"M-g\" body 
and the heads:
 
 \"h\":    `first-error',
@@ -101,39 +102,40 @@ Call the head: `next-error'."
                  ((debug error)
                   (message "%S" err)
                   (sit-for 0.8)
-                  nil))
-               (when hydra-is-helpful (message #("error: [h]: first, [j]: 
next, [k]: prev." 8 9 (face hydra-face-red)
-                                                 20 21 (face hydra-face-red)
-                                                 31 32 (face hydra-face-red))))
-               (setq hydra-last
-                     (hydra-set-transient-map
-                      (setq hydra-curr-map '(keymap (107 . 
hydra-error/previous-error)
-                                             (106 . hydra-error/next-error)
-                                             (104 . hydra-error/first-error)
-                                             (kp-subtract . 
hydra--negative-argument)
-                                             (kp-9 . hydra--digit-argument)
-                                             (kp-8 . hydra--digit-argument)
-                                             (kp-7 . hydra--digit-argument)
-                                             (kp-6 . hydra--digit-argument)
-                                             (kp-5 . hydra--digit-argument)
-                                             (kp-4 . hydra--digit-argument)
-                                             (kp-3 . hydra--digit-argument)
-                                             (kp-2 . hydra--digit-argument)
-                                             (kp-1 . hydra--digit-argument)
-                                             (kp-0 . hydra--digit-argument)
-                                             (57 . hydra--digit-argument)
-                                             (56 . hydra--digit-argument)
-                                             (55 . hydra--digit-argument)
-                                             (54 . hydra--digit-argument)
-                                             (53 . hydra--digit-argument)
-                                             (52 . hydra--digit-argument)
-                                             (51 . hydra--digit-argument)
-                                             (50 . hydra--digit-argument)
-                                             (49 . hydra--digit-argument)
-                                             (48 . hydra--digit-argument)
-                                             (45 . hydra--negative-argument)
-                                             (21 . hydra--universal-argument)))
-                      t))))
+                  nil)))
+             (when hydra-is-helpful (message #("error: [h]: first, [j]: next, 
[k]: prev." 8 9 (face hydra-face-red)
+                                               20 21 (face hydra-face-red)
+                                               31 32 (face hydra-face-red))))
+             (setq hydra-last
+                   (hydra-set-transient-map
+                    (setq hydra-curr-map
+                          (quote (keymap (107 . hydra-error/previous-error)
+                                         (106 . hydra-error/next-error)
+                                         (104 . hydra-error/first-error)
+                                         (kp-subtract . 
hydra--negative-argument)
+                                         (kp-9 . hydra--digit-argument)
+                                         (kp-8 . hydra--digit-argument)
+                                         (kp-7 . hydra--digit-argument)
+                                         (kp-6 . hydra--digit-argument)
+                                         (kp-5 . hydra--digit-argument)
+                                         (kp-4 . hydra--digit-argument)
+                                         (kp-3 . hydra--digit-argument)
+                                         (kp-2 . hydra--digit-argument)
+                                         (kp-1 . hydra--digit-argument)
+                                         (kp-0 . hydra--digit-argument)
+                                         (57 . hydra--digit-argument)
+                                         (56 . hydra--digit-argument)
+                                         (55 . hydra--digit-argument)
+                                         (54 . hydra--digit-argument)
+                                         (53 . hydra--digit-argument)
+                                         (52 . hydra--digit-argument)
+                                         (51 . hydra--digit-argument)
+                                         (50 . hydra--digit-argument)
+                                         (49 . hydra--digit-argument)
+                                         (48 . hydra--digit-argument)
+                                         (45 . hydra--negative-argument)
+                                         (21 . hydra--universal-argument))))
+                    t)))
       (defun hydra-error/previous-error nil "Create a hydra with a \"M-g\" 
body and the heads:
 
 \"h\":    `first-error',
@@ -150,39 +152,40 @@ Call the head: `previous-error'."
                  ((debug error)
                   (message "%S" err)
                   (sit-for 0.8)
-                  nil))
-               (when hydra-is-helpful (message #("error: [h]: first, [j]: 
next, [k]: prev." 8 9 (face hydra-face-red)
-                                                 20 21 (face hydra-face-red)
-                                                 31 32 (face hydra-face-red))))
-               (setq hydra-last
-                     (hydra-set-transient-map
-                      (setq hydra-curr-map '(keymap (107 . 
hydra-error/previous-error)
-                                             (106 . hydra-error/next-error)
-                                             (104 . hydra-error/first-error)
-                                             (kp-subtract . 
hydra--negative-argument)
-                                             (kp-9 . hydra--digit-argument)
-                                             (kp-8 . hydra--digit-argument)
-                                             (kp-7 . hydra--digit-argument)
-                                             (kp-6 . hydra--digit-argument)
-                                             (kp-5 . hydra--digit-argument)
-                                             (kp-4 . hydra--digit-argument)
-                                             (kp-3 . hydra--digit-argument)
-                                             (kp-2 . hydra--digit-argument)
-                                             (kp-1 . hydra--digit-argument)
-                                             (kp-0 . hydra--digit-argument)
-                                             (57 . hydra--digit-argument)
-                                             (56 . hydra--digit-argument)
-                                             (55 . hydra--digit-argument)
-                                             (54 . hydra--digit-argument)
-                                             (53 . hydra--digit-argument)
-                                             (52 . hydra--digit-argument)
-                                             (51 . hydra--digit-argument)
-                                             (50 . hydra--digit-argument)
-                                             (49 . hydra--digit-argument)
-                                             (48 . hydra--digit-argument)
-                                             (45 . hydra--negative-argument)
-                                             (21 . hydra--universal-argument)))
-                      t))))
+                  nil)))
+             (when hydra-is-helpful (message #("error: [h]: first, [j]: next, 
[k]: prev." 8 9 (face hydra-face-red)
+                                               20 21 (face hydra-face-red)
+                                               31 32 (face hydra-face-red))))
+             (setq hydra-last
+                   (hydra-set-transient-map
+                    (setq hydra-curr-map
+                          (quote (keymap (107 . hydra-error/previous-error)
+                                         (106 . hydra-error/next-error)
+                                         (104 . hydra-error/first-error)
+                                         (kp-subtract . 
hydra--negative-argument)
+                                         (kp-9 . hydra--digit-argument)
+                                         (kp-8 . hydra--digit-argument)
+                                         (kp-7 . hydra--digit-argument)
+                                         (kp-6 . hydra--digit-argument)
+                                         (kp-5 . hydra--digit-argument)
+                                         (kp-4 . hydra--digit-argument)
+                                         (kp-3 . hydra--digit-argument)
+                                         (kp-2 . hydra--digit-argument)
+                                         (kp-1 . hydra--digit-argument)
+                                         (kp-0 . hydra--digit-argument)
+                                         (57 . hydra--digit-argument)
+                                         (56 . hydra--digit-argument)
+                                         (55 . hydra--digit-argument)
+                                         (54 . hydra--digit-argument)
+                                         (53 . hydra--digit-argument)
+                                         (52 . hydra--digit-argument)
+                                         (51 . hydra--digit-argument)
+                                         (50 . hydra--digit-argument)
+                                         (49 . hydra--digit-argument)
+                                         (48 . hydra--digit-argument)
+                                         (45 . hydra--negative-argument)
+                                         (21 . hydra--universal-argument))))
+                    t)))
       (unless (keymapp (lookup-key global-map (kbd "M-g")))
         (define-key global-map (kbd "M-g")
           nil))
@@ -205,32 +208,33 @@ The body can be accessed via `hydra-error/body'."
                                                31 32 (face hydra-face-red))))
              (setq hydra-last
                    (hydra-set-transient-map
-                    '(keymap (107 . hydra-error/previous-error)
-                      (106 . hydra-error/next-error)
-                      (104 . hydra-error/first-error)
-                      (kp-subtract . hydra--negative-argument)
-                      (kp-9 . hydra--digit-argument)
-                      (kp-8 . hydra--digit-argument)
-                      (kp-7 . hydra--digit-argument)
-                      (kp-6 . hydra--digit-argument)
-                      (kp-5 . hydra--digit-argument)
-                      (kp-4 . hydra--digit-argument)
-                      (kp-3 . hydra--digit-argument)
-                      (kp-2 . hydra--digit-argument)
-                      (kp-1 . hydra--digit-argument)
-                      (kp-0 . hydra--digit-argument)
-                      (57 . hydra--digit-argument)
-                      (56 . hydra--digit-argument)
-                      (55 . hydra--digit-argument)
-                      (54 . hydra--digit-argument)
-                      (53 . hydra--digit-argument)
-                      (52 . hydra--digit-argument)
-                      (51 . hydra--digit-argument)
-                      (50 . hydra--digit-argument)
-                      (49 . hydra--digit-argument)
-                      (48 . hydra--digit-argument)
-                      (45 . hydra--negative-argument)
-                      (21 . hydra--universal-argument))
+                    (setq hydra-curr-map
+                          (quote (keymap (107 . hydra-error/previous-error)
+                                         (106 . hydra-error/next-error)
+                                         (104 . hydra-error/first-error)
+                                         (kp-subtract . 
hydra--negative-argument)
+                                         (kp-9 . hydra--digit-argument)
+                                         (kp-8 . hydra--digit-argument)
+                                         (kp-7 . hydra--digit-argument)
+                                         (kp-6 . hydra--digit-argument)
+                                         (kp-5 . hydra--digit-argument)
+                                         (kp-4 . hydra--digit-argument)
+                                         (kp-3 . hydra--digit-argument)
+                                         (kp-2 . hydra--digit-argument)
+                                         (kp-1 . hydra--digit-argument)
+                                         (kp-0 . hydra--digit-argument)
+                                         (57 . hydra--digit-argument)
+                                         (56 . hydra--digit-argument)
+                                         (55 . hydra--digit-argument)
+                                         (54 . hydra--digit-argument)
+                                         (53 . hydra--digit-argument)
+                                         (52 . hydra--digit-argument)
+                                         (51 . hydra--digit-argument)
+                                         (50 . hydra--digit-argument)
+                                         (49 . hydra--digit-argument)
+                                         (48 . hydra--digit-argument)
+                                         (45 . hydra--negative-argument)
+                                         (21 . hydra--universal-argument))))
                     t)))))))
 
 (ert-deftest hydra-blue-toggle ()
@@ -310,49 +314,54 @@ The body can be accessed via `toggle/body'."
                                                48 49 (face hydra-face-blue))))
              (setq hydra-last
                    (hydra-set-transient-map
-                    '(keymap (113 . toggle/nil)
-                      (97 . toggle/abbrev-mode)
-                      (102 . toggle/auto-fill-mode)
-                      (116 . toggle/toggle-truncate-lines)
-                      (kp-subtract . hydra--negative-argument)
-                      (kp-9 . hydra--digit-argument)
-                      (kp-8 . hydra--digit-argument)
-                      (kp-7 . hydra--digit-argument)
-                      (kp-6 . hydra--digit-argument)
-                      (kp-5 . hydra--digit-argument)
-                      (kp-4 . hydra--digit-argument)
-                      (kp-3 . hydra--digit-argument)
-                      (kp-2 . hydra--digit-argument)
-                      (kp-1 . hydra--digit-argument)
-                      (kp-0 . hydra--digit-argument)
-                      (57 . hydra--digit-argument)
-                      (56 . hydra--digit-argument)
-                      (55 . hydra--digit-argument)
-                      (54 . hydra--digit-argument)
-                      (53 . hydra--digit-argument)
-                      (52 . hydra--digit-argument)
-                      (51 . hydra--digit-argument)
-                      (50 . hydra--digit-argument)
-                      (49 . hydra--digit-argument)
-                      (48 . hydra--digit-argument)
-                      (45 . hydra--negative-argument)
-                      (21 . hydra--universal-argument))
+                    (setq hydra-curr-map
+                          '(keymap (113 . toggle/nil)
+                            (97 . toggle/abbrev-mode)
+                            (102 . toggle/auto-fill-mode)
+                            (116 . toggle/toggle-truncate-lines)
+                            (kp-subtract . hydra--negative-argument)
+                            (kp-9 . hydra--digit-argument)
+                            (kp-8 . hydra--digit-argument)
+                            (kp-7 . hydra--digit-argument)
+                            (kp-6 . hydra--digit-argument)
+                            (kp-5 . hydra--digit-argument)
+                            (kp-4 . hydra--digit-argument)
+                            (kp-3 . hydra--digit-argument)
+                            (kp-2 . hydra--digit-argument)
+                            (kp-1 . hydra--digit-argument)
+                            (kp-0 . hydra--digit-argument)
+                            (57 . hydra--digit-argument)
+                            (56 . hydra--digit-argument)
+                            (55 . hydra--digit-argument)
+                            (54 . hydra--digit-argument)
+                            (53 . hydra--digit-argument)
+                            (52 . hydra--digit-argument)
+                            (51 . hydra--digit-argument)
+                            (50 . hydra--digit-argument)
+                            (49 . hydra--digit-argument)
+                            (48 . hydra--digit-argument)
+                            (45 . hydra--negative-argument)
+                            (21 . hydra--universal-argument)))
                     t)))))))
 
 (ert-deftest hydra-amaranth-vi ()
-  (should
-   (equal
-    (macroexpand
-     '(defhydra hydra-vi
-       (:color amaranth)
-       "vi"
-       ("l" forward-char)
-       ("h" backward-char)
-       ("j" next-line)
-       ("k" previous-line)
-       ("q" nil "quit")))
-    '(progn
-      (defun hydra-vi/forward-char nil "Create a hydra with no body and the 
heads:
+  (unless (version< emacs-version "24.4")
+    (should
+     (equal
+      (macroexpand
+       '(defhydra hydra-vi
+         (:pre
+          (set-cursor-color "#e52b50")
+          :post
+          (set-cursor-color "#ffffff")
+          :color amaranth)
+         "vi"
+         ("l" forward-char)
+         ("h" backward-char)
+         ("j" next-line)
+         ("k" previous-line)
+         ("q" nil "quit")))
+      '(progn (defun hydra-vi/forward-char nil "Create a hydra with no body 
and the heads:
 
 \"l\":    `forward-char',
 \"h\":    `backward-char',
@@ -363,14 +372,81 @@ The body can be accessed via `toggle/body'."
 The body can be accessed via `hydra-vi/body'.
 
 Call the head: `forward-char'."
-             (interactive)
-             (catch (quote hydra-disable)
-               (hydra-disable)
-               (condition-case err (prog1 t (call-interactively (function 
forward-char)))
-                 ((debug error)
-                  (message "%S" err)
-                  (sit-for 0.8)
-                  nil))
+                     (interactive)
+                     (set-cursor-color "#e52b50")
+                     (catch (quote hydra-disable)
+                       (hydra-disable)
+                       (condition-case err (prog1 t (call-interactively 
(function forward-char)))
+                         ((debug error)
+                          (message "%S" err)
+                          (sit-for 0.8)
+                          nil)))
+                     (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: 
quit." 4 5 (face hydra-face-amaranth)
+                                                       7 8 (face 
hydra-face-amaranth)
+                                                       10 11 (face 
hydra-face-amaranth)
+                                                       13 14 (face 
hydra-face-amaranth)
+                                                       17 18 (face 
hydra-face-blue))))
+                     (setq hydra-last
+                           (hydra-set-transient-map
+                            (setq hydra-curr-map
+                                  (quote (keymap (t lambda nil (interactive)
+                                                    (message "An amaranth 
Hydra can only exit through a blue head")
+                                                    (hydra-set-transient-map 
hydra-curr-map t)
+                                                    (when hydra-is-helpful 
(sit-for 0.8)
+                                                          (message #("vi: l, 
h, j, k, [q]: quit." 4 5 (face hydra-face-amaranth)
+                                                                     7 8 (face 
hydra-face-amaranth)
+                                                                     10 11 
(face hydra-face-amaranth)
+                                                                     13 14 
(face hydra-face-amaranth)
+                                                                     17 18 
(face hydra-face-blue)))))
+                                                 (113 . hydra-vi/nil)
+                                                 (107 . hydra-vi/previous-line)
+                                                 (106 . hydra-vi/next-line)
+                                                 (104 . hydra-vi/backward-char)
+                                                 (108 . hydra-vi/forward-char)
+                                                 (kp-subtract . 
hydra--negative-argument)
+                                                 (kp-9 . hydra--digit-argument)
+                                                 (kp-8 . hydra--digit-argument)
+                                                 (kp-7 . hydra--digit-argument)
+                                                 (kp-6 . hydra--digit-argument)
+                                                 (kp-5 . hydra--digit-argument)
+                                                 (kp-4 . hydra--digit-argument)
+                                                 (kp-3 . hydra--digit-argument)
+                                                 (kp-2 . hydra--digit-argument)
+                                                 (kp-1 . hydra--digit-argument)
+                                                 (kp-0 . hydra--digit-argument)
+                                                 (57 . hydra--digit-argument)
+                                                 (56 . hydra--digit-argument)
+                                                 (55 . hydra--digit-argument)
+                                                 (54 . hydra--digit-argument)
+                                                 (53 . hydra--digit-argument)
+                                                 (52 . hydra--digit-argument)
+                                                 (51 . hydra--digit-argument)
+                                                 (50 . hydra--digit-argument)
+                                                 (49 . hydra--digit-argument)
+                                                 (48 . hydra--digit-argument)
+                                                 (45 . 
hydra--negative-argument)
+                                                 (21 . 
hydra--universal-argument))))
+                            t)))
+        (defun hydra-vi/backward-char nil "Create a hydra with no body and the 
heads:
+
+\"l\":    `forward-char',
+\"h\":    `backward-char',
+\"j\":    `next-line',
+\"k\":    `previous-line',
+\"q\":    `nil'
+
+The body can be accessed via `hydra-vi/body'.
+
+Call the head: `backward-char'."
+               (interactive)
+               (set-cursor-color "#e52b50")
+               (catch (quote hydra-disable)
+                 (hydra-disable)
+                 (condition-case err (prog1 t (call-interactively (function 
backward-char)))
+                   ((debug error)
+                    (message "%S" err)
+                    (sit-for 0.8)
+                    nil)))
                (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: quit." 
4 5 (face hydra-face-amaranth)
                                                  7 8 (face hydra-face-amaranth)
                                                  10 11 (face 
hydra-face-amaranth)
@@ -416,8 +492,8 @@ Call the head: `forward-char'."
                                            (48 . hydra--digit-argument)
                                            (45 . hydra--negative-argument)
                                            (21 . hydra--universal-argument))))
-                      t))))
-      (defun hydra-vi/backward-char nil "Create a hydra with no body and the 
heads:
+                      t)))
+        (defun hydra-vi/next-line nil "Create a hydra with no body and the 
heads:
 
 \"l\":    `forward-char',
 \"h\":    `backward-char',
@@ -427,16 +503,16 @@ Call the head: `forward-char'."
 
 The body can be accessed via `hydra-vi/body'.
 
-Call the head: `backward-char'."
-             (interactive)
-             (catch
-                 (quote hydra-disable)
-               (hydra-disable)
-               (condition-case err (prog1 t (call-interactively (function 
backward-char)))
-                 ((debug error)
-                  (message "%S" err)
-                  (sit-for 0.8)
-                  nil))
+Call the head: `next-line'."
+               (interactive)
+               (set-cursor-color "#e52b50")
+               (catch (quote hydra-disable)
+                 (hydra-disable)
+                 (condition-case err (prog1 t (call-interactively (function 
next-line)))
+                   ((debug error)
+                    (message "%S" err)
+                    (sit-for 0.8)
+                    nil)))
                (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: quit." 
4 5 (face hydra-face-amaranth)
                                                  7 8 (face hydra-face-amaranth)
                                                  10 11 (face 
hydra-face-amaranth)
@@ -482,8 +558,8 @@ Call the head: `backward-char'."
                                            (48 . hydra--digit-argument)
                                            (45 . hydra--negative-argument)
                                            (21 . hydra--universal-argument))))
-                      t))))
-      (defun hydra-vi/next-line nil "Create a hydra with no body and the heads:
+                      t)))
+        (defun hydra-vi/previous-line nil "Create a hydra with no body and the 
heads:
 
 \"l\":    `forward-char',
 \"h\":    `backward-char',
@@ -493,16 +569,16 @@ Call the head: `backward-char'."
 
 The body can be accessed via `hydra-vi/body'.
 
-Call the head: `next-line'."
-             (interactive)
-             (catch
-                 (quote hydra-disable)
-               (hydra-disable)
-               (condition-case err (prog1 t (call-interactively (function 
next-line)))
-                 ((debug error)
-                  (message "%S" err)
-                  (sit-for 0.8)
-                  nil))
+Call the head: `previous-line'."
+               (interactive)
+               (set-cursor-color "#e52b50")
+               (catch (quote hydra-disable)
+                 (hydra-disable)
+                 (condition-case err (prog1 t (call-interactively (function 
previous-line)))
+                   ((debug error)
+                    (message "%S" err)
+                    (sit-for 0.8)
+                    nil)))
                (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: quit." 
4 5 (face hydra-face-amaranth)
                                                  7 8 (face hydra-face-amaranth)
                                                  10 11 (face 
hydra-face-amaranth)
@@ -548,8 +624,8 @@ Call the head: `next-line'."
                                            (48 . hydra--digit-argument)
                                            (45 . hydra--negative-argument)
                                            (21 . hydra--universal-argument))))
-                      t))))
-      (defun hydra-vi/previous-line nil "Create a hydra with no body and the 
heads:
+                      t)))
+        (defun hydra-vi/nil nil "Create a hydra with no body and the heads:
 
 \"l\":    `forward-char',
 \"h\":    `backward-char',
@@ -559,15 +635,22 @@ Call the head: `next-line'."
 
 The body can be accessed via `hydra-vi/body'.
 
-Call the head: `previous-line'."
-             (interactive)
-             (catch (quote hydra-disable)
+Call the head: `nil'."
+               (interactive)
+               (set-cursor-color "#e52b50")
                (hydra-disable)
-               (condition-case err (prog1 t (call-interactively (function 
previous-line)))
-                 ((debug error)
-                  (message "%S" err)
-                  (sit-for 0.8)
-                  nil))
+               (set-cursor-color "#ffffff"))
+        (defun hydra-vi/body nil "Create a hydra with no body and the heads:
+
+\"l\":    `forward-char',
+\"h\":    `backward-char',
+\"j\":    `next-line',
+\"k\":    `previous-line',
+\"q\":    `nil'
+
+The body can be accessed via `hydra-vi/body'."
+               (interactive)
+               (set-cursor-color "#e52b50")
                (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: quit." 
4 5 (face hydra-face-amaranth)
                                                  7 8 (face hydra-face-amaranth)
                                                  10 11 (face 
hydra-face-amaranth)
@@ -613,75 +696,7 @@ Call the head: `previous-line'."
                                            (48 . hydra--digit-argument)
                                            (45 . hydra--negative-argument)
                                            (21 . hydra--universal-argument))))
-                      t))))
-      (defun hydra-vi/nil nil "Create a hydra with no body and the heads:
-
-\"l\":    `forward-char',
-\"h\":    `backward-char',
-\"j\":    `next-line',
-\"k\":    `previous-line',
-\"q\":    `nil'
-
-The body can be accessed via `hydra-vi/body'.
-
-Call the head: `nil'."
-             (interactive)
-             (hydra-disable))
-      (defun hydra-vi/body nil "Create a hydra with no body and the heads:
-
-\"l\":    `forward-char',
-\"h\":    `backward-char',
-\"j\":    `next-line',
-\"k\":    `previous-line',
-\"q\":    `nil'
-
-The body can be accessed via `hydra-vi/body'."
-             (interactive)
-             (when hydra-is-helpful (message #("vi: l, h, j, k, [q]: quit." 4 
5 (face hydra-face-amaranth)
-                                               7 8 (face hydra-face-amaranth)
-                                               10 11 (face hydra-face-amaranth)
-                                               13 14 (face hydra-face-amaranth)
-                                               17 18 (face hydra-face-blue))))
-             (setq hydra-last
-                   (hydra-set-transient-map
-                    (quote (keymap (t lambda nil (interactive)
-                                      (message "An amaranth Hydra can only 
exit through a blue head")
-                                      (hydra-set-transient-map hydra-curr-map 
t)
-                                      (when hydra-is-helpful (sit-for 0.8)
-                                            (message #("vi: l, h, j, k, [q]: 
quit." 4 5 (face hydra-face-amaranth)
-                                                       7 8 (face 
hydra-face-amaranth)
-                                                       10 11 (face 
hydra-face-amaranth)
-                                                       13 14 (face 
hydra-face-amaranth)
-                                                       17 18 (face 
hydra-face-blue)))))
-                                   (113 . hydra-vi/nil)
-                                   (107 . hydra-vi/previous-line)
-                                   (106 . hydra-vi/next-line)
-                                   (104 . hydra-vi/backward-char)
-                                   (108 . hydra-vi/forward-char)
-                                   (kp-subtract . hydra--negative-argument)
-                                   (kp-9 . hydra--digit-argument)
-                                   (kp-8 . hydra--digit-argument)
-                                   (kp-7 . hydra--digit-argument)
-                                   (kp-6 . hydra--digit-argument)
-                                   (kp-5 . hydra--digit-argument)
-                                   (kp-4 . hydra--digit-argument)
-                                   (kp-3 . hydra--digit-argument)
-                                   (kp-2 . hydra--digit-argument)
-                                   (kp-1 . hydra--digit-argument)
-                                   (kp-0 . hydra--digit-argument)
-                                   (57 . hydra--digit-argument)
-                                   (56 . hydra--digit-argument)
-                                   (55 . hydra--digit-argument)
-                                   (54 . hydra--digit-argument)
-                                   (53 . hydra--digit-argument)
-                                   (52 . hydra--digit-argument)
-                                   (51 . hydra--digit-argument)
-                                   (50 . hydra--digit-argument)
-                                   (49 . hydra--digit-argument)
-                                   (48 . hydra--digit-argument)
-                                   (45 . hydra--negative-argument)
-                                   (21 . hydra--universal-argument)))
-                    t)))))))
+                      t))))))))
 
 (provide 'hydra-test)
 
diff --git a/hydra.el b/hydra.el
index b615c1e..b070e21 100644
--- a/hydra.el
+++ b/hydra.el
@@ -226,6 +226,39 @@ HEADS is a list of heads."
     heads ",\n")
    (format "The body can be accessed via `%S'." body-name)))
 
+(defun hydra--make-defun (name cmd color
+                          doc hint keymap
+                          body-color body-pre body-post)
+  "Make a defun wrapper, using NAME, CMD, COLOR, DOC, HINT, and KEYMAP.
+BODY-COLOR, BODY-PRE, and BODY-POST are used as well."
+  `(defun ,name ()
+     ,doc
+     (interactive)
+     ,@(when body-pre (list body-pre))
+     ,@(delq nil
+             (if (eq color 'blue)
+                 `((hydra-disable)
+                   ,(when cmd `(call-interactively #',cmd))
+                   ,body-post)
+               `(,(when cmd
+                        `(catch 'hydra-disable
+                           (hydra-disable)
+                           (condition-case err
+                               (prog1 t
+                                 (call-interactively #',cmd))
+                             ((debug error)
+                              (message "%S" err)
+                              (sit-for 0.8)
+                              nil))))
+                  (when hydra-is-helpful
+                    (message ,hint))
+                  (setq hydra-last
+                        (hydra-set-transient-map
+                         (setq hydra-curr-map ',keymap)
+                         t
+                         ,@(if (and (not (eq body-color 'amaranth)) body-post)
+                               `((lambda () ,body-post))))))))))
+
 ;;* Macros
 ;;** hydra-create
 ;;;###autoload
@@ -329,32 +362,11 @@ in turn can be either red or blue."
     `(progn
        ,@(cl-mapcar
           (lambda (head name)
-            `(defun ,name ()
-               ,(format "%s\n\nCall the head: `%S'." doc (cadr head))
-               (interactive)
-               ,@(if body-pre (list body-pre))
-               ,@(if (eq (hydra--color head body-color) 'blue)
-                     `((hydra-disable)
-                       ,@(unless (null (cadr head))
-                                 `((call-interactively #',(cadr head))))
-                       ,@(if body-post (list body-post)))
-                     `((catch 'hydra-disable
-                         (hydra-disable)
-                         (condition-case err
-                             (prog1 t
-                               (call-interactively #',(cadr head)))
-                           ((debug error)
-                            (message "%S" err)
-                            (sit-for 0.8)
-                            nil))
-                         (when hydra-is-helpful
-                           (message ,hint))
-                         (setq hydra-last
-                               (hydra-set-transient-map
-                                (setq hydra-curr-map ',keymap)
-                                t
-                                ,@(if (and body-post (not (eq body-color 
'amaranth)))
-                                      `((lambda () ,body-post))))))))))
+            (hydra--make-defun
+             name (cadr head) (hydra--color head body-color)
+             (format "%s\n\nCall the head: `%S'." doc (cadr head))
+             hint keymap
+             body-color body-pre body-post))
           heads names)
        ,@(unless (or (null body-key)
                      (null method)
@@ -373,18 +385,8 @@ in turn can be either red or blue."
                      (vconcat (kbd body-key) (kbd (car head)))
                      (list 'function name))))
                 heads names))
-       (defun ,body-name ()
-         ,doc
-         (interactive)
-         ,@(if body-pre (list body-pre))
-         (when hydra-is-helpful
-           (message ,hint))
-         (setq hydra-last
-               (hydra-set-transient-map
-                ',keymap
-                t
-                ,@(if (and body-post (not (eq body-color 'amaranth)))
-                      `((lambda () ,body-post)))))))))
+       ,(hydra--make-defun body-name nil nil doc hint keymap
+                           body-color body-pre body-post))))
 
 (provide 'hydra)
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]