branch: master
commit f6697f3f785c58ce71deafb30c77939ab5e3a7a4
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Allow for the "%s`foo" spec.
* hydra.el (hydra--format): Update. Treat variables in the same way as
s-expressions.
* hydra-test.el (hydra-format-2): Add test.
(hydra-format-with-sexp-2): Add test.
Fixes #85
---
hydra-test.el | 31 +++++++++++++++++++++++++++++--
hydra.el | 23 +++++++----------------
2 files changed, 36 insertions(+), 18 deletions(-)
diff --git a/hydra-test.el b/hydra-test.el
index b908ac0..5209936 100644
--- a/hydra-test.el
+++ b/hydra-test.el
@@ -705,7 +705,7 @@ The body can be accessed via `hydra-vi/body'."
("l" text-scale-decrease "out")
("q" nil "quit"))))))
-(ert-deftest hydra-format ()
+(ert-deftest hydra-format-1 ()
(should (equal
(let ((hydra-fontify-head-function
'hydra-fontify-head-greyscale))
@@ -728,7 +728,19 @@ _f_ auto-fill-mode: %`auto-fill-function
%s auto-fill-mode: %S
" "{a}" abbrev-mode "{d}" debug-on-error "{f}" auto-fill-function) "[[q]]:
quit"))))
-(ert-deftest hydra-format-with-sexp ()
+(ert-deftest hydra-format-2 ()
+ (should (equal
+ (let ((hydra-fontify-head-function
+ 'hydra-fontify-head-greyscale))
+ (hydra--format
+ 'bar
+ nil
+ "\n bar %s`foo\n"
+ '(("a" (quote t) "" :cmd-name bar/lambda-a)
+ ("q" nil "" :cmd-name bar/nil))))
+ '(concat (format " bar %s\n" foo) "{a}, [q]"))))
+
+(ert-deftest hydra-format-with-sexp-1 ()
(should (equal
(let ((hydra-fontify-head-function
'hydra-fontify-head-greyscale))
@@ -743,6 +755,21 @@ _f_ auto-fill-mode: %`auto-fill-function
(buffer-narrowed-p)))
"[[q]]: cancel"))))
+(ert-deftest hydra-format-with-sexp-2 ()
+ (should (equal
+ (let ((hydra-fontify-head-function
+ 'hydra-fontify-head-greyscale))
+ (hydra--format
+ 'hydra-toggle nil
+ "\n_n_ narrow-or-widen-dwim %s(progn (message
\"checking\")(buffer-narrowed-p))asdf\n"
+ '(("n" narrow-to-region nil) ("q" nil "cancel"))))
+ '(concat (format "%s narrow-or-widen-dwim %sasdf\n"
+ "{n}"
+ (progn
+ (message "checking")
+ (buffer-narrowed-p)))
+ "[[q]]: cancel"))))
+
(ert-deftest hydra-compat-colors-1 ()
(should (equal (hydra--head-color
'("e" (message "Exiting now") "blue")
diff --git a/hydra.el b/hydra.el
index 17948ec..924fe57 100644
--- a/hydra.el
+++ b/hydra.el
@@ -466,14 +466,13 @@ HEAD's binding is returned as a string wrapped with [] or
{}."
(funcall (or hydra-fontify-head-function 'hydra-fontify-head-default)
head body))
-(defun hydra--format (name body docstring heads)
+(defun hydra--format (_name body docstring heads)
"Generate a `format' statement from STR.
\"%`...\" expressions are extracted into \"%S\".
NAME, BODY, DOCSTRING and HEADS are parameters of `defhydra'.
The expressions can be auto-expanded according to NAME."
(setq docstring (replace-regexp-in-string "\\^" "" docstring))
(let ((rest (hydra--hint body heads))
- (prefix (symbol-name name))
(start 0)
varlist
offset)
@@ -495,22 +494,14 @@ The expressions can be auto-expanded according to NAME."
nil nil docstring)))
(error "Unrecognized key: _%s_" key))))
- ((eq ?` (aref (match-string 2 docstring) 0))
- (push (hydra--unalias-var
- (substring (match-string 2 docstring) 1) prefix)
- varlist)
- (setq docstring
- (replace-match
- (concat "%" (match-string 1 docstring) "S")
- nil nil docstring 0)))
-
(t
- (let* ((spec (match-string 1 docstring))
- (lspec (length spec))
- (me2 (match-end 2)))
+ (let* ((varp (if (eq ?` (aref (match-string 2 docstring) 0)) 1 0))
+ (spec (match-string 1 docstring))
+ (lspec (length spec)))
(setq offset
(with-temp-buffer
- (insert (substring docstring (+ 1 start (length spec))))
+ (insert (substring docstring (+ 1 start varp
+ (length spec))))
(goto-char (point-min))
(push (read (current-buffer)) varlist)
(- (point) (point-min))))
@@ -521,7 +512,7 @@ The expressions can be auto-expanded according to NAME."
(concat
(substring docstring 0 start)
"%" spec
- (substring docstring (+ me2 offset -1))))))))
+ (substring docstring (+ start offset 1 lspec varp))))))))
(if (eq ?\n (aref docstring 0))
`(concat (format ,(substring docstring 1) ,@(nreverse varlist))
,rest)