branch: master commit cb28124af8c9f0231662f4d184d1cbc8d7e83a86 Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
Allow sexps in Ruby-style docstrings * hydra.el (hydra--format): Improve. No error handling or checking yet, but should work fine if the docstring is correct. * hydra-test.el (hydra-format-with-sexp): Add test. Fixes #42. --- hydra-test.el | 15 ++++++++++++++- hydra.el | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/hydra-test.el b/hydra-test.el index fe02d6d..ae3e4c7 100644 --- a/hydra-test.el +++ b/hydra-test.el @@ -697,7 +697,20 @@ _f_ auto-fill-mode: %`auto-fill-function % 3s auto-fill-mode: %S " "{a}" abbrev-mode "{d}" debug-on-error "{f}" auto-fill-function) "[[q]]: quit")))) - +(ert-deftest hydra-format-with-sexp () + (should (equal + (let ((hydra-fontify-head-function + 'hydra-fontify-head-greyscale)) + (hydra--format + 'hydra-toggle nil + "\n_n_ narrow-or-widen-dwim %(progn (message \"checking\")(buffer-narrowed-p))asdf\n" + '(("n" narrow-to-region nil) ("q" nil "cancel")))) + '(concat (format "\n% 3s narrow-or-widen-dwim %Sasdf\n" + "{n}" + (progn + (message "checking") + (buffer-narrowed-p))) + "[[q]]: cancel")))) (provide 'hydra-test) diff --git a/hydra.el b/hydra.el index 5ea4640..ca3e23a 100644 --- a/hydra.el +++ b/hydra.el @@ -378,21 +378,42 @@ The expressions can be auto-expanded according to NAME." (body-color (hydra--body-color body)) (prefix (symbol-name name)) (start 0) - varlist) + varlist + offset) (while (setq start (string-match - "\\(?:%\\( ?-?[0-9]*\\)`\\([a-z-A-Z/0-9]+\\)\\)\\|\\(?:_\\([a-z-~A-Z]+\\)_\\)" + "\\(?:%\\( ?-?[0-9]*\\)\\(`[a-z-A-Z/0-9]+\\|(\\)\\)\\|\\(?:_\\([a-z-~A-Z]+\\)_\\)" docstring start)) - (if (eq ?_ (aref (match-string 0 docstring) 0)) - (let* ((key (match-string 3 docstring)) - (head (assoc key heads))) - (if head - (progn - (push (hydra-fontify-head head body) varlist) - (setq docstring (replace-match "% 3s" nil nil docstring))) - (error "Unrecognized key: _%s_" key))) - (push (hydra--unalias-var (match-string 2 docstring) prefix) varlist) - (setq docstring (replace-match (concat "%" (match-string 1 docstring) "S") nil nil docstring 0)))) + (cond ((eq ?_ (aref (match-string 0 docstring) 0)) + (let* ((key (match-string 3 docstring)) + (head (assoc key heads))) + (if head + (progn + (push (hydra-fontify-head head body) varlist) + (setq docstring (replace-match "% 3s" 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 + (setq offset + (with-temp-buffer + (insert (substring docstring (1+ start))) + (goto-char (point-min)) + (push (read (current-buffer)) varlist) + (point))) + (setq docstring + (concat + (substring docstring 0 start) + "%" (match-string 1 docstring) "S" + (substring docstring + (+ (match-end 2) offset -2))))))) (if (eq ?\n (aref docstring 0)) `(concat (format ,docstring ,@(nreverse varlist)) ,rest)