Skip to content

Commit 989ed95

Browse files
committed
Rename compat toggle - :nonheads -> :foreign-keys
* hydra-test.el: Add tests. * hydra.el (hydra--head-color): Update. (hydra--body-foreign-keys): New defun. (hydra--body-color): Update. (hydra--handle-nonhead): Update. * README.md: Update.
1 parent 54004d2 commit 989ed95

File tree

3 files changed

+131
-55
lines changed

3 files changed

+131
-55
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ the hint. You can still have the old behavior by setting `hydra-lv` to `nil`.
297297
By popular demand, an alternative syntax has been implemented that translates to colors without
298298
using them in the syntax. `:exit` can be used both in body (heads will inherit) and in heads
299299
(possible to override body). `:exit` is nil by default, corresponding to `red` head; you don't need
300-
to set it explicitly to nil. `:nonheads` can be used only in body and can be either nil (default),
300+
to set it explicitly to nil. `:foreign-keys` can be used only in body and can be either nil (default),
301301
`warn` or `run`.
302302

303-
| color | toggle |
304-
|----------+------------------------|
305-
| red | |
306-
| blue | :exit t |
307-
| amaranth | :nonheads warn |
308-
| teal | :nonheads warn :exit t |
309-
| pink | :nonheads run |
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 |

hydra-test.el

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -605,31 +605,30 @@ The body can be accessed via `hydra-vi/body'."
605605
("q" nil "cancel"))))))
606606

607607
(ert-deftest hydra-amaranth-compat ()
608-
(unless (version< emacs-version "24.4")
609-
(should
610-
(equal
611-
(macroexpand
612-
'(defhydra hydra-vi
613-
(:pre
614-
(set-cursor-color "#e52b50")
615-
:post
616-
(set-cursor-color "#ffffff")
617-
:color amaranth)
618-
"vi"
619-
("j" next-line)
620-
("k" previous-line)
621-
("q" nil "quit")))
622-
(macroexpand
623-
'(defhydra hydra-vi
624-
(:pre
625-
(set-cursor-color "#e52b50")
626-
:post
627-
(set-cursor-color "#ffffff")
628-
:nonheads warn)
629-
"vi"
630-
("j" next-line)
631-
("k" previous-line)
632-
("q" nil "quit")))))))
608+
(should
609+
(equal
610+
(macroexpand
611+
'(defhydra hydra-vi
612+
(:pre
613+
(set-cursor-color "#e52b50")
614+
:post
615+
(set-cursor-color "#ffffff")
616+
:color amaranth)
617+
"vi"
618+
("j" next-line)
619+
("k" previous-line)
620+
("q" nil "quit")))
621+
(macroexpand
622+
'(defhydra hydra-vi
623+
(:pre
624+
(set-cursor-color "#e52b50")
625+
:post
626+
(set-cursor-color "#ffffff")
627+
:foreign-keys warn)
628+
"vi"
629+
("j" next-line)
630+
("k" previous-line)
631+
("q" nil "quit"))))))
633632

634633
(ert-deftest hydra-pink-compat ()
635634
(should
@@ -643,7 +642,7 @@ The body can be accessed via `hydra-vi/body'."
643642
("q" nil "quit")))
644643
(macroexpand
645644
'(defhydra hydra-zoom (global-map "<f2>"
646-
:nonheads run)
645+
:foreign-keys run)
647646
"zoom"
648647
("g" text-scale-increase "in")
649648
("l" text-scale-decrease "out")
@@ -661,7 +660,7 @@ The body can be accessed via `hydra-vi/body'."
661660
("q" nil "quit")))
662661
(macroexpand
663662
'(defhydra hydra-zoom (global-map "<f2>"
664-
:nonheads warn
663+
:foreign-keys warn
665664
:exit t)
666665
"zoom"
667666
("g" text-scale-increase "in")
@@ -706,7 +705,7 @@ _f_ auto-fill-mode: %`auto-fill-function
706705
(buffer-narrowed-p)))
707706
"[[q]]: cancel"))))
708707

709-
(ert-deftest hydra-compat-colors ()
708+
(ert-deftest hydra-compat-colors-1 ()
710709
(should (equal (hydra--head-color
711710
'("e" (message "Exiting now") "blue")
712711
'(nil nil :color blue))
@@ -722,7 +721,73 @@ _f_ auto-fill-mode: %`auto-fill-function
722721
(should (equal (hydra--head-color
723722
'("c" (message "Continuing") "red" :exit nil)
724723
'(nil nil :exit t))
725-
'red)))
724+
'red))
725+
(equal (hydra--head-color
726+
'("a" abbrev-mode nil)
727+
'(nil nil :color teal))
728+
'teal)
729+
(equal (hydra--head-color
730+
'("a" abbrev-mode :exit nil)
731+
'(nil nil :color teal))
732+
'amaranth)
733+
)
734+
735+
(ert-deftest hydra-compat-colors-2 ()
736+
(equal
737+
(macroexpand
738+
'(defhydra hydra-test (:color amaranth)
739+
("a" fun-a)
740+
("b" fun-b :color blue)
741+
("c" fun-c :color blue)
742+
("d" fun-d :color blue)
743+
("e" fun-e :color blue)
744+
("f" fun-f :color blue)))
745+
(macroexpand
746+
'(defhydra hydra-test (:color teal)
747+
("a" fun-a :color red)
748+
("b" fun-b)
749+
("c" fun-c)
750+
("d" fun-d)
751+
("e" fun-e)
752+
("f" fun-f)))))
753+
754+
(ert-deftest hydra-compat-colors-3 ()
755+
(equal
756+
(macroexpand
757+
'(defhydra hydra-test ()
758+
("a" fun-a)
759+
("b" fun-b :color blue)
760+
("c" fun-c :color blue)
761+
("d" fun-d :color blue)
762+
("e" fun-e :color blue)
763+
("f" fun-f :color blue)))
764+
(macroexpand
765+
'(defhydra hydra-test (:color blue)
766+
("a" fun-a :color red)
767+
("b" fun-b)
768+
("c" fun-c)
769+
("d" fun-d)
770+
("e" fun-e)
771+
("f" fun-f)))))
772+
773+
(ert-deftest hydra-compat-colors-4 ()
774+
(equal
775+
(macroexpand
776+
'(defhydra hydra-test ()
777+
("a" fun-a)
778+
("b" fun-b :exit t)
779+
("c" fun-c :exit t)
780+
("d" fun-d :exit t)
781+
("e" fun-e :exit t)
782+
("f" fun-f :exit t)))
783+
(macroexpand
784+
'(defhydra hydra-test (:exit t)
785+
("a" fun-a :exit nil)
786+
("b" fun-b)
787+
("c" fun-c)
788+
("d" fun-d)
789+
("e" fun-e)
790+
("f" fun-f)))))
726791

727792
(provide 'hydra-test)
728793

hydra.el

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Oleh Krehel <ohwoeowho@gmail.com>
66
;; Maintainer: Oleh Krehel <ohwoeowho@gmail.com>
77
;; URL: https://github.com/abo-abo/hydra
8-
;; Version: 0.10.0
8+
;; Version: 0.11.0
99
;; Keywords: bindings
1010
;; Package-Requires: ((cl-lib "0.5"))
1111

@@ -267,6 +267,7 @@ Return DEFAULT if PROP is not in H."
267267
"Return the color of a Hydra head H with BODY."
268268
(let* ((exit (hydra--head-property h :exit 'default))
269269
(color (hydra--head-property h :color))
270+
(foreign-keys (hydra--body-foreign-keys body))
270271
(head-color
271272
(cond ((eq exit 'default)
272273
(cl-case color
@@ -278,44 +279,55 @@ Return DEFAULT if PROP is not in H."
278279
((null exit)
279280
(if color
280281
(error "Don't mix :color and :exit - they are aliases: %S" h)
281-
'red))
282+
(cl-case foreign-keys
283+
(run 'pink)
284+
(warn 'amaranth)
285+
(t 'red))))
282286
((eq exit t)
283287
(if color
284288
(error "Don't mix :color and :exit - they are aliases: %S" h)
285289
'blue))
286290
(t
287291
(error "Unknown :exit %S" exit)))))
288-
(let ((nonheads (plist-get (cddr body) :nonheads))
289-
(body-exit (plist-get (cddr body) :exit)))
292+
(let ((body-exit (plist-get (cddr body) :exit)))
290293
(cond ((null (cadr h))
291294
(when head-color
292295
(hydra--complain
293296
"Doubly specified blue head - nil cmd is already blue: %S" h))
294297
'blue)
295298
((null head-color)
296299
(hydra--body-color body))
297-
((null nonheads)
300+
((null foreign-keys)
298301
head-color)
299-
((eq nonheads 'run)
302+
((eq foreign-keys 'run)
300303
(if (eq head-color 'red)
301304
'pink
302305
'blue))
303-
((eq nonheads 'warn)
304-
(if (eq head-color 'red)
306+
((eq foreign-keys 'warn)
307+
(if (memq head-color '(red amaranth))
305308
'amaranth
306309
'teal))
307310
(t
308311
(error "Unexpected %S %S" h body))))))
309312

313+
(defun hydra--body-foreign-keys (body)
314+
"Return what BODY does with a non-head binding."
315+
(or
316+
(plist-get (cddr body) :foreign-keys)
317+
(let ((color (plist-get (cddr body) :color)))
318+
(cl-case color
319+
((amaranth teal) 'warn)
320+
(pink 'run)))))
321+
310322
(defun hydra--body-color (body)
311323
"Return the color of BODY.
312324
BODY is the second argument to `defhydra'"
313325
(let ((color (plist-get (cddr body) :color))
314326
(exit (plist-get (cddr body) :exit))
315-
(nonheads (plist-get (cddr body) :nonheads)))
316-
(cond ((eq nonheads 'warn)
327+
(foreign-keys (plist-get (cddr body) :foreign-keys)))
328+
(cond ((eq foreign-keys 'warn)
317329
(if exit 'teal 'amaranth))
318-
((eq nonheads 'run) 'pink)
330+
((eq foreign-keys 'run) 'pink)
319331
(exit 'blue)
320332
(color color)
321333
(t 'red))))
@@ -585,27 +597,26 @@ NAME, BODY and HEADS are parameters to `defhydra'."
585597
(define-key keymap hydra-keyboard-quit #'hydra-keyboard-quit))
586598
(when (memq body-color '(amaranth pink teal))
587599
(if (cl-some `(lambda (h)
588-
(eq (hydra--head-color h body) 'blue))
600+
(memq (hydra--head-color h body) '(blue teal)))
589601
heads)
590602
(progn
591603
(define-key keymap [t]
592604
`(lambda ()
593605
(interactive)
594606
,(cond
595-
((eq body-color 'amaranth)
607+
((memq body-color '(amaranth teal))
596608
'(message "An amaranth Hydra can only exit through a blue head"))
597-
((eq body-color 'teal)
598-
'(message "A teal Hydra can only exit through a blue head"))
599609
(t
600610
'(hydra-pink-fallback)))
601611
(hydra-set-transient-map hydra-curr-map t)
602612
(when hydra-is-helpful
603613
(unless hydra-lv
604614
(sit-for 0.8))
605615
(,(intern (format "%S/hint" name)))))))
606-
(error
607-
"An %S Hydra must have at least one blue head in order to exit"
608-
body-color)))))
616+
(unless (eq body-color 'teal)
617+
(error
618+
"An %S Hydra must have at least one blue head in order to exit"
619+
body-color))))))
609620

610621
(defun hydra--head-name (h body-name)
611622
"Return the symbol for head H of body BODY-NAME."

0 commit comments

Comments
 (0)