[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 2086614 10/72: hydra.el (defhydradio): New macro
From: |
Oleh Krehel |
Subject: |
[elpa] master 2086614 10/72: hydra.el (defhydradio): New macro |
Date: |
Fri, 06 Mar 2015 13:04:02 +0000 |
branch: master
commit 208661423bc4cb805004f93997659e27cfe8b2a3
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
hydra.el (defhydradio): New macro
* hydra.el (hydra--radio): New defun.
(hydra--quote-maybe): New defun.
(hydra--cycle-radio): New defun.
* hydra-test.el (defhydradio): New test.
---
hydra-test.el | 18 +++++++++++++++++
hydra.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)
diff --git a/hydra-test.el b/hydra-test.el
index 8e1df9a..914c4ad 100644
--- a/hydra-test.el
+++ b/hydra-test.el
@@ -555,6 +555,24 @@ The body can be accessed via `hydra-vi/body'."
t (lambda nil (hydra-disable t))))
(setq prefix-arg current-prefix-arg))))))))
+(ert-deftest defhydradio ()
+ (should (equal
+ (macroexpand
+ '(defhydradio hydra-test ()
+ (num [0 1 2 3 4 5 6 7 8 9 10])
+ (str ["foo" "bar" "baz"])))
+ '(progn
+ (defvar hydra-test/num 0
+ "Num")
+ (put 'hydra-test/num 'range [0 1 2 3 4 5 6 7 8 9 10])
+ (defun hydra-test/num ()
+ (hydra--cycle-radio 'hydra-test/num))
+ (defvar hydra-test/str "foo"
+ "Str")
+ (put 'hydra-test/str 'range ["foo" "bar" "baz"])
+ (defun hydra-test/str ()
+ (hydra--cycle-radio 'hydra-test/str))))))
+
(provide 'hydra-test)
;;; hydra-test.el ends here
diff --git a/hydra.el b/hydra.el
index 40aae23..7ccf47e 100644
--- a/hydra.el
+++ b/hydra.el
@@ -483,6 +483,64 @@ except a blue head can stop the Hydra state.
body-color body-pre body-post
'(setq prefix-arg current-prefix-arg)))))
+(defmacro defhydradio (name body &rest heads)
+ "Create toggles with prefix NAME.
+BODY specifies the options; there are none currently.
+HEADS have the format:
+
+ (TOGGLE-NAME &optional VALUE DOC)
+
+TOGGLE-NAME will be used along with NAME to generate a variable
+name and a function that cycles it with the same name. VALUE
+should be an array. The first element of VALUE will be used to
+inialize the variable.
+VALUE defaults to [nil t].
+DOC defaults to TOGGLE-NAME split and capitalized."
+ (declare (indent defun))
+ (cons 'progn
+ (apply #'append
+ (mapcar (lambda (h)
+ (hydra--radio name h))
+ heads))))
+
+(defun hydra--radio (parent head)
+ "Generate a hydradio from HEAD."
+ (let* ((name (car head))
+ (full-name (intern (format "%S/%S" parent name)))
+ (val (or (cadr head) [nil t]))
+ (doc (or (cl-caddr head)
+ (mapconcat #'capitalize
+ (split-string (symbol-name name) "-")
+ " "))))
+ `((defvar ,full-name ,(hydra--quote-maybe (aref val 0)) ,doc)
+ (put ',full-name 'range ,val)
+ (defun ,full-name ()
+ (hydra--cycle-radio ',full-name)))))
+
+(defun hydra--quote-maybe (x)
+ "Quote X if it's a symbol."
+ (if (symbolp x)
+ (list 'quote x)
+ x))
+
+(defun hydra--cycle-radio (sym)
+ "Set SYM to the next value in its range."
+ (let* ((val (symbol-value sym))
+ (range (get sym 'range))
+ (i 0)
+ (l (length range)))
+ (setq i (catch 'done
+ (while (< i l)
+ (if (equal (aref range i) val)
+ (throw 'done (1+ i))
+ (incf i)))
+ (error "Val not in range for %S" sym)))
+ (set sym
+ (aref range
+ (if (>= i l)
+ 0
+ i)))))
+
(provide 'hydra)
;;; Local Variables:
- [elpa] master updated (ffa5405 -> 3222b0c), Oleh Krehel, 2015/03/06
- [elpa] master bb38641 05/72: hydra.el (hydra--make-defun): simplify, Oleh Krehel, 2015/03/06
- [elpa] master 614e1f7 07/72: `defhydra' should be indented as `defun', Oleh Krehel, 2015/03/06
- [elpa] master 8bca51b 04/72: Warn about explict red heads on amaranth body, Oleh Krehel, 2015/03/06
- [elpa] master 216550e 06/72: Add option to font-lock `defhydra', Oleh Krehel, 2015/03/06
- [elpa] master f3c4dd7 02/72: hydra.el (hydra--hint): allow duplicate functions in heads, Oleh Krehel, 2015/03/06
- [elpa] master 62f9b05 01/72: Allow some heads with no hints, Oleh Krehel, 2015/03/06
- [elpa] master f04f14c 14/72: hydra.el (defhydra): Update doc, Oleh Krehel, 2015/03/06
- [elpa] master 2086614 10/72: hydra.el (defhydradio): New macro,
Oleh Krehel <=
- [elpa] master d04ef9b 12/72: hydra.el (hydra-cleanup): reorder, Oleh Krehel, 2015/03/06
- [elpa] master 926d9a4 15/72: Add pink body color, Oleh Krehel, 2015/03/06
- [elpa] master d25e560 03/72: Fix head color for heads with nil as HINT, Oleh Krehel, 2015/03/06
- [elpa] master abb76e8 13/72: lv.el (lv-message): Don't deactivate mark, Oleh Krehel, 2015/03/06
- [elpa] master 81ffca0 20/72: Update faces, Oleh Krehel, 2015/03/06
- [elpa] master 9769143 25/72: hydra-examples.el: Add example 7, Oleh Krehel, 2015/03/06
- [elpa] master c049a33 16/72: Allow for Ruby-style Hydra docstrings, Oleh Krehel, 2015/03/06
- [elpa] master 688e8fd 23/72: hydra.el (hydra--head-color): fix for teal color, Oleh Krehel, 2015/03/06
- [elpa] master 0cda4ce 26/72: Fix typos, Oleh Krehel, 2015/03/06
- [elpa] master caf114a 11/72: Fix unintentional recursion in Emacs 25, Oleh Krehel, 2015/03/06