Skip to content

Commit

Permalink
Allow for the "%s`foo" spec.
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
abo-abo committed Mar 23, 2015
1 parent 03771f4 commit f6697f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
31 changes: 29 additions & 2 deletions hydra-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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")
Expand Down
23 changes: 7 additions & 16 deletions hydra.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))))
Expand All @@ -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)
Expand Down

0 comments on commit f6697f3

Please sign in to comment.