hydra.el (hydra--head-color): Remove

* hydra.el (hydra-fontify-head-default): Move `hydra--head-color' body
  here.
(hydra-fontify-head-greyscale): Simplify.
(hydra--make-defun): Simplify.
(hydra--head-name): Simplify.
(hydra--delete-duplicates): Update.
(defhydra): Update.
master
Oleh Krehel 11 years ago
parent d71386b0f5
commit 88f14a04a3
  1. 26
      hydra-test.el
  2. 71
      hydra.el

@ -1029,32 +1029,6 @@ _f_ auto-fill-mode: %`auto-fill-function
(buffer-narrowed-p))) (buffer-narrowed-p)))
"[[q]]: cancel")))) "[[q]]: cancel"))))
(ert-deftest hydra-compat-colors-1 ()
(should (equal (hydra--head-color
'("e" (message "Exiting now") "blue" :exit t)
'(nil nil :color blue))
'blue))
(should (equal (hydra--head-color
'("c" (message "Continuing") "red" :color red)
'(nil nil :color blue))
'red))
(should (equal (hydra--head-color
'("j" next-line "" :exit t)
'(nil nil))
'blue))
(should (equal (hydra--head-color
'("c" (message "Continuing") "red" :exit nil)
'(nil nil :exit t))
'red))
(equal (hydra--head-color
'("a" abbrev-mode nil :exit t)
'(nil nil :color teal))
'teal)
(equal (hydra--head-color
'("a" abbrev-mode :exit nil)
'(nil nil :color teal))
'amaranth))
(ert-deftest hydra-compat-colors-2 () (ert-deftest hydra-compat-colors-2 ()
(should (should
(equal (equal

@ -330,24 +330,6 @@ one of the properties on the list."
Return DEFAULT if PROP is not in H." Return DEFAULT if PROP is not in H."
(hydra-plist-get-default (cl-cdddr h) prop default)) (hydra-plist-get-default (cl-cdddr h) prop default))
(defun hydra--head-color (h body)
"Return the color of a Hydra head H with BODY."
(let* ((foreign-keys (hydra--body-foreign-keys body))
(head-exit (hydra--head-property h :exit))
(head-color
(if head-exit
(if (eq foreign-keys 'warn)
'teal
'blue)
(cl-case foreign-keys
(warn 'amaranth)
(run 'pink)
(t 'red)))))
(when (and (null (cadr h))
(not (eq head-color 'blue)))
(hydra--complain "nil cmd can only be blue"))
head-color))
(defun hydra--body-foreign-keys (body) (defun hydra--body-foreign-keys (body)
"Return what BODY does with a non-head binding." "Return what BODY does with a non-head binding."
(or (or
@ -423,23 +405,36 @@ BODY, and HEADS are parameters to `defhydra'."
(defun hydra-fontify-head-default (head body) (defun hydra-fontify-head-default (head body)
"Produce a pretty string from HEAD and BODY. "Produce a pretty string from HEAD and BODY.
HEAD's binding is returned as a string with a colored face." HEAD's binding is returned as a string with a colored face."
(propertize (car head) 'face (let* ((foreign-keys (hydra--body-foreign-keys body))
(cl-case (hydra--head-color head body) (head-exit (hydra--head-property head :exit))
(blue 'hydra-face-blue) (head-color
(red 'hydra-face-red) (if head-exit
(amaranth 'hydra-face-amaranth) (if (eq foreign-keys 'warn)
(pink 'hydra-face-pink) 'teal
(teal 'hydra-face-teal) 'blue)
(t (error "Unknown color for %S" head))))) (cl-case foreign-keys
(warn 'amaranth)
(run 'pink)
(t 'red)))))
(when (and (null (cadr head))
(not (eq head-color 'blue)))
(hydra--complain "nil cmd can only be blue"))
(propertize (car head) 'face
(cl-case head-color
(blue 'hydra-face-blue)
(red 'hydra-face-red)
(amaranth 'hydra-face-amaranth)
(pink 'hydra-face-pink)
(teal 'hydra-face-teal)
(t (error "Unknown color for %S" head))))))
(defun hydra-fontify-head-greyscale (head body) (defun hydra-fontify-head-greyscale (head body)
"Produce a pretty string from HEAD and BODY. "Produce a pretty string from HEAD and BODY.
HEAD's binding is returned as a string wrapped with [] or {}." HEAD's binding is returned as a string wrapped with [] or {}."
(let ((color (hydra--head-color head body))) (format
(format (if (hydra--head-property head :exit)
(if (eq color 'blue) "[%s]"
"[%s]" "{%s}") (car head)))
"{%s}") (car head))))
(defun hydra-fontify-head (head body) (defun hydra-fontify-head (head body)
"Produce a pretty string from HEAD and BODY." "Produce a pretty string from HEAD and BODY."
@ -533,8 +528,6 @@ BODY-AFTER-EXIT is added to the end of the wrapper."
(cmd (when (car head) (cmd (when (car head)
(hydra--make-callable (hydra--make-callable
(cadr head)))) (cadr head))))
(color (when (car head)
(hydra--head-color head body)))
(doc (if (car head) (doc (if (car head)
(format "%s\n\nCall the head: `%S'." doc (cadr head)) (format "%s\n\nCall the head: `%S'." doc (cadr head))
doc)) doc))
@ -546,7 +539,7 @@ BODY-AFTER-EXIT is added to the end of the wrapper."
(interactive) (interactive)
(hydra-default-pre) (hydra-default-pre)
,@(when body-pre (list body-pre)) ,@(when body-pre (list body-pre))
,@(if (memq color '(blue teal)) ,@(if (hydra--head-property head :exit)
`((hydra-keyboard-quit) `((hydra-keyboard-quit)
,(if body-after-exit ,(if body-after-exit
`(unwind-protect `(unwind-protect
@ -586,7 +579,7 @@ BODY-AFTER-EXIT is added to the end of the wrapper."
(if (symbolp (cadr h)) (if (symbolp (cadr h))
(cadr h) (cadr h)
(concat "lambda-" (car h)))))) (concat "lambda-" (car h))))))
(when (and (memq (hydra--head-color h body) '(blue teal)) (when (and (hydra--head-property h :exit)
(not (memq (cadr h) '(body nil)))) (not (memq (cadr h) '(body nil))))
(setq str (concat str "-and-exit"))) (setq str (concat str "-and-exit")))
(intern str))) (intern str)))
@ -594,15 +587,15 @@ BODY-AFTER-EXIT is added to the end of the wrapper."
(defun hydra--delete-duplicates (heads) (defun hydra--delete-duplicates (heads)
"Return HEADS without entries that have the same CMD part. "Return HEADS without entries that have the same CMD part.
In duplicate HEADS, :cmd-name is modified to whatever they duplicate." In duplicate HEADS, :cmd-name is modified to whatever they duplicate."
(let ((ali '(((hydra-repeat . red) . hydra-repeat))) (let ((ali '(((hydra-repeat . nil) . hydra-repeat)))
res entry) res entry)
(dolist (h heads) (dolist (h heads)
(if (setq entry (assoc (cons (cadr h) (if (setq entry (assoc (cons (cadr h)
(hydra--head-color h '(nil nil))) (hydra--head-property h :exit))
ali)) ali))
(setf (cl-cdddr h) (plist-put (cl-cdddr h) :cmd-name (cdr entry))) (setf (cl-cdddr h) (plist-put (cl-cdddr h) :cmd-name (cdr entry)))
(push (cons (cons (cadr h) (push (cons (cons (cadr h)
(hydra--head-color h '(nil nil))) (hydra--head-property h :exit))
(plist-get (cl-cdddr h) :cmd-name)) (plist-get (cl-cdddr h) :cmd-name))
ali) ali)
(push h res))) (push h res)))
@ -837,7 +830,7 @@ result of `defhydra'."
(when (memq body-foreign-keys '(run warn)) (when (memq body-foreign-keys '(run warn))
(unless (cl-some (unless (cl-some
(lambda (h) (lambda (h)
(memq (hydra--head-color h body) '(blue teal))) (hydra--head-property h :exit))
heads) heads)
(error (error
"An %S Hydra must have at least one blue head in order to exit" "An %S Hydra must have at least one blue head in order to exit"

Loading…
Cancel
Save