[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master f6697f3 02/18: Allow for the "%s`foo" spec.
From: |
Oleh Krehel |
Subject: |
[elpa] master f6697f3 02/18: Allow for the "%s`foo" spec. |
Date: |
Sat, 28 Mar 2015 15:04:23 +0000 |
branch: master
commit f6697f3f785c58ce71deafb30c77939ab5e3a7a4
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
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)
- [elpa] master updated (059c3d5 -> da45e68), Oleh Krehel, 2015/03/28
- [elpa] master 84f4fe7 06/18: Don't default `hydra-repeat--prefix-arg' to 1, Oleh Krehel, 2015/03/28
- [elpa] master 6c6fc0b 07/18: Allow `hydra-repeat' to take a numeric arg, Oleh Krehel, 2015/03/28
- [elpa] master 87e4574 05/18: Mention the leading newline in `defhydra' docstring, Oleh Krehel, 2015/03/28
- [elpa] master 71491b9 04/18: Fix argument to `replace-match', Oleh Krehel, 2015/03/28
- [elpa] master f6697f3 02/18: Allow for the "%s`foo" spec.,
Oleh Krehel <=
- [elpa] master 5c3ab46 08/18: hydra.el (hydra--format): Amend regex, Oleh Krehel, 2015/03/28
- [elpa] master 1bad0c4 09/18: hydra.el (hydra--format): Update regex order, Oleh Krehel, 2015/03/28
- [elpa] master 03771f4 01/18: hydra.el: Turn on lexical-binding, Oleh Krehel, 2015/03/28
- [elpa] master fe1cfee 10/18: hydra-examples.el (hydra-rectangle): Update, Oleh Krehel, 2015/03/28
- [elpa] master c8c6b8c 03/18: Fix blue/red heads with same cmd, Oleh Krehel, 2015/03/28
- [elpa] master 1290237 12/18: Disable key-chord for the duration of the hydra, Oleh Krehel, 2015/03/28
- [elpa] master cb4b78a 11/18: hydra-test.el (hydra-format-4): Add test, Oleh Krehel, 2015/03/28
- [elpa] master 640af46 13/18: hydra.el (defhydra): Improve docstring, Oleh Krehel, 2015/03/28
- [elpa] master 7843563 14/18: hydra.el (hydra--make-funcall): Add, Oleh Krehel, 2015/03/28
- [elpa] master e403363 16/18: hydra.el (defhydra): Avoid eager macroexpansion failure, Oleh Krehel, 2015/03/28