Skip to content

Commit d71386b

Browse files
committed
hydra.el (hydra--head-color): Simplify
* hydra.el (hydra-face-red): (hydra-face-blue): (hydra-face-amaranth): (hydra-face-pink): (hydra-face-teal): Improve docstrings. (hydra--head-color): Simplify. (defhydra): Use copy-sequence on inherited heads. Move :cmd-name setting to the very end, when :exit is already set. * hydra-test.el: Update tests.
1 parent 8875bf1 commit d71386b

File tree

2 files changed

+37
-78
lines changed

2 files changed

+37
-78
lines changed

hydra-test.el

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ The body can be accessed via `hydra-toggle/body'."
450450
previous-line
451451
""
452452
:exit nil)
453-
("q" nil "quit" :exit nil))))
453+
("q" nil "quit" :exit t))))
454454
(defun hydra-vi/next-line nil
455455
"Create a hydra with no body and the heads:
456456
@@ -534,7 +534,7 @@ Call the head: `nil'."
534534
#("vi: j, k, [q]: quit."
535535
4 5 (face hydra-face-amaranth)
536536
7 8 (face hydra-face-amaranth)
537-
11 12 (face hydra-face-blue)))))
537+
11 12 (face hydra-face-teal)))))
538538
(defun hydra-vi/body nil
539539
"Create a hydra with no body and the heads:
540540
@@ -963,7 +963,7 @@ _f_ auto-fill-mode: %`auto-fill-function
963963
'(concat (format "%s abbrev-mode: %S
964964
%s debug-on-error: %S
965965
%s auto-fill-mode: %S
966-
" "{a}" abbrev-mode "{d}" debug-on-error "{f}" auto-fill-function) "[[q]]: quit"))))
966+
" "{a}" abbrev-mode "{d}" debug-on-error "{f}" auto-fill-function) "[{q}]: quit"))))
967967

968968
(ert-deftest hydra-format-2 ()
969969
(should (equal
@@ -973,8 +973,8 @@ _f_ auto-fill-mode: %`auto-fill-function
973973
'bar
974974
nil
975975
"\n bar %s`foo\n"
976-
'(("a" (quote t) "" :cmd-name bar/lambda-a)
977-
("q" nil "" :cmd-name bar/nil))))
976+
'(("a" (quote t) "" :cmd-name bar/lambda-a :exit nil)
977+
("q" nil "" :cmd-name bar/nil :exit t))))
978978
'(concat (format " bar %s\n" foo) "{a}, [q]"))))
979979

980980
(ert-deftest hydra-format-3 ()
@@ -1006,7 +1006,7 @@ _f_ auto-fill-mode: %`auto-fill-function
10061006
(hydra--format
10071007
'hydra-toggle nil
10081008
"\n_n_ narrow-or-widen-dwim %(progn (message \"checking\")(buffer-narrowed-p))asdf\n"
1009-
'(("n" narrow-to-region nil) ("q" nil "cancel"))))
1009+
'(("n" narrow-to-region nil) ("q" nil "cancel" :exit t))))
10101010
'(concat (format "%s narrow-or-widen-dwim %Sasdf\n"
10111011
"{n}"
10121012
(progn
@@ -1021,7 +1021,7 @@ _f_ auto-fill-mode: %`auto-fill-function
10211021
(hydra--format
10221022
'hydra-toggle nil
10231023
"\n_n_ narrow-or-widen-dwim %s(progn (message \"checking\")(buffer-narrowed-p))asdf\n"
1024-
'(("n" narrow-to-region nil) ("q" nil "cancel"))))
1024+
'(("n" narrow-to-region nil) ("q" nil "cancel" :exit t))))
10251025
'(concat (format "%s narrow-or-widen-dwim %sasdf\n"
10261026
"{n}"
10271027
(progn
@@ -1031,17 +1031,13 @@ _f_ auto-fill-mode: %`auto-fill-function
10311031

10321032
(ert-deftest hydra-compat-colors-1 ()
10331033
(should (equal (hydra--head-color
1034-
'("e" (message "Exiting now") "blue")
1034+
'("e" (message "Exiting now") "blue" :exit t)
10351035
'(nil nil :color blue))
10361036
'blue))
10371037
(should (equal (hydra--head-color
10381038
'("c" (message "Continuing") "red" :color red)
10391039
'(nil nil :color blue))
10401040
'red))
1041-
(should (equal (hydra--head-color
1042-
'("e" (message "Exiting now") "blue")
1043-
'(nil nil :exit t))
1044-
'blue))
10451041
(should (equal (hydra--head-color
10461042
'("j" next-line "" :exit t)
10471043
'(nil nil))
@@ -1051,7 +1047,7 @@ _f_ auto-fill-mode: %`auto-fill-function
10511047
'(nil nil :exit t))
10521048
'red))
10531049
(equal (hydra--head-color
1054-
'("a" abbrev-mode nil)
1050+
'("a" abbrev-mode nil :exit t)
10551051
'(nil nil :color teal))
10561052
'teal)
10571053
(equal (hydra--head-color

hydra.el

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,29 @@ When nil, you can specify your own at each location like this: _ 5a_.")
178178

179179
(defface hydra-face-red
180180
'((t (:foreground "#FF0000" :bold t)))
181-
"Red Hydra heads will persist indefinitely."
181+
"Red Hydra heads don't exit the Hydra.
182+
Every other command exits the Hydra."
182183
:group 'hydra)
183184

184185
(defface hydra-face-blue
185186
'((t (:foreground "#0000FF" :bold t)))
186-
"Blue Hydra heads will vanquish the Hydra.")
187+
"Blue Hydra heads exit the Hydra.
188+
Every other command exits as well.")
187189

188190
(defface hydra-face-amaranth
189191
'((t (:foreground "#E52B50" :bold t)))
190192
"Amaranth body has red heads and warns on intercepting non-heads.
191-
Vanquishable only through a blue head.")
193+
Exitable only through a blue head.")
192194

193195
(defface hydra-face-pink
194196
'((t (:foreground "#FF6EB4" :bold t)))
195-
"Pink body has red heads and on intercepting non-heads calls them without quitting.
196-
Vanquishable only through a blue head.")
197+
"Pink body has red heads and runs intercepted non-heads.
198+
Exitable only through a blue head.")
197199

198200
(defface hydra-face-teal
199201
'((t (:foreground "#367588" :bold t)))
200202
"Teal body has blue heads an warns on intercepting non-heads.
201-
Vanquishable only through a blue head.")
203+
Exitable only through a blue head.")
202204

203205
;;* Fontification
204206
(defun hydra-add-font-lock ()
@@ -330,57 +332,21 @@ Return DEFAULT if PROP is not in H."
330332

331333
(defun hydra--head-color (h body)
332334
"Return the color of a Hydra head H with BODY."
333-
(let* ((head-exit (hydra--head-property h :exit 'default))
334-
(foreign-keys (hydra--body-foreign-keys body))
335-
(head-color (hydra--head-property h :color))
335+
(let* ((foreign-keys (hydra--body-foreign-keys body))
336+
(head-exit (hydra--head-property h :exit))
336337
(head-color
337-
(cond ((eq head-exit 'default)
338-
(cl-case head-color
339-
(blue 'blue)
340-
(red 'red)
341-
(t
342-
(unless (null head-color)
343-
(error "Use only :blue or :red for heads: %S" h)))))
344-
((null head-exit)
345-
(if head-color
346-
(error "Don't mix :color and :exit - they are aliases: %S" h)
347-
(cl-case foreign-keys
348-
(run 'pink)
349-
(warn 'amaranth)
350-
(t 'red))))
351-
((eq head-exit t)
352-
(if head-color
353-
(error "Don't mix :color and :exit - they are aliases: %S" h)
354-
'blue))
355-
(t
356-
(error "Unknown :exit %S" head-exit)))))
357-
(cond ((null (cadr h))
358-
(when head-color
359-
(hydra--complain
360-
"Doubly specified blue head - nil cmd is already blue: %S" h))
361-
'blue)
362-
((null head-color)
363-
(let ((color (plist-get (cddr body) :color))
364-
(exit (plist-get (cddr body) :exit))
365-
(foreign-keys (plist-get (cddr body) :foreign-keys)))
366-
(cond ((eq foreign-keys 'warn)
367-
(if exit 'teal 'amaranth))
368-
((eq foreign-keys 'run) 'pink)
369-
(exit 'blue)
370-
(color color)
371-
(t 'red))))
372-
((null foreign-keys)
373-
head-color)
374-
((eq foreign-keys 'run)
375-
(if (eq head-color 'red)
376-
'pink
377-
'blue))
378-
((eq foreign-keys 'warn)
379-
(if (memq head-color '(red amaranth))
380-
'amaranth
381-
'teal))
382-
(t
383-
(error "Unexpected %S %S" h body)))))
338+
(if head-exit
339+
(if (eq foreign-keys 'warn)
340+
'teal
341+
'blue)
342+
(cl-case foreign-keys
343+
(warn 'amaranth)
344+
(run 'pink)
345+
(t 'red)))))
346+
(when (and (null (cadr h))
347+
(not (eq head-color 'blue)))
348+
(hydra--complain "nil cmd can only be blue"))
349+
head-color))
384350

385351
(defun hydra--body-foreign-keys (body)
386352
"Return what BODY does with a non-head binding."
@@ -823,7 +789,7 @@ result of `defhydra'."
823789
(hydra--make-funcall body-before-exit)
824790
(hydra--make-funcall body-after-exit)
825791
(dolist (base body-inherit)
826-
(setq heads (append heads (eval base))))
792+
(setq heads (append heads (copy-sequence (eval base)))))
827793
(dolist (h heads)
828794
(let ((len (length h)))
829795
(cond ((< len 2)
@@ -832,9 +798,7 @@ result of `defhydra'."
832798
(setcdr (cdr h)
833799
(list
834800
(hydra-plist-get-default body-plist :hint "")))
835-
(setcdr (nthcdr 2 h)
836-
(list :cmd-name (hydra--head-name h name body)
837-
:exit body-exit)))
801+
(setcdr (nthcdr 2 h) (list :exit body-exit)))
838802
(t
839803
(let ((hint (cl-caddr h)))
840804
(unless (or (null hint)
@@ -844,9 +808,7 @@ result of `defhydra'."
844808
(cddr h)))))
845809
(let ((hint-and-plist (cddr h)))
846810
(if (null (cdr hint-and-plist))
847-
(setcdr hint-and-plist
848-
(list :cmd-name (hydra--head-name h name body)
849-
:exit body-exit))
811+
(setcdr hint-and-plist (list :exit body-exit))
850812
(let* ((plist (cl-cdddr h))
851813
(h-color (plist-get plist :color)))
852814
(if h-color
@@ -860,8 +822,9 @@ result of `defhydra'."
860822
(plist-put plist :exit
861823
(if (eq h-exit 'default)
862824
body-exit
863-
h-exit))))
864-
(plist-put plist :cmd-name (hydra--head-name h name body)))))))))
825+
h-exit))))))))))
826+
(plist-put (cl-cdddr h) :cmd-name (hydra--head-name h name body))
827+
(when (null (cadr h)) (plist-put (cl-cdddr h) :exit t)))
865828
(let ((doc (hydra--doc body-key body-name heads))
866829
(heads-nodup (hydra--delete-duplicates heads)))
867830
(mapc

0 commit comments

Comments
 (0)