branch: master commit 1eebfed70e792d266cb08c6e6b11bec140a0977f Author: Oleh Krehel <ohwoeo...@gmail.com> Commit: Oleh Krehel <ohwoeo...@gmail.com>
Allow `format'-style width specifiers in docstring * hydra.el (hydra--hint): Process less data. (hydra--format): Change to add `format'-style width specifiers. Example: (defhydra hydra-toggle (:color pink) " _a_ abbrev-mode: % 4`abbrev-mode^^^^ _f_ auto-fill-mode: %`auto-fill-function _d_ debug-on-error: % 4`debug-on-error^ _t_ truncate-lines: %`truncate-lines _w_ whitespace-mode:% 4`whitespace-mode _g_ golden-ratio-mode: %`golden-ratio-mode " ("a" abbrev-mode nil) ("d" toggle-debug-on-error nil) ("f" auto-fill-mode nil) ("g" golden-ratio-mode nil) ("t" toggle-truncate-lines nil) ("w" whitespace-mode nil) ("q" nil "quit")) Here, ^^^ represent empty characters used to compensate for the fact that the lengths of variable symbols are different. You can choose not to use them. The result will be the same, but your code will look misaligned. Fixes #39. --- hydra.el | 57 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 29 insertions(+), 28 deletions(-) diff --git a/hydra.el b/hydra.el index bf1b558..1b23eaf 100644 --- a/hydra.el +++ b/hydra.el @@ -322,44 +322,45 @@ NAME, BODY, DOCSTRING and HEADS are parameters to `defhydra'." (cons pstr (and (stringp (cl-caddr h)) (cl-caddr h)))) alist))))) - - (format (if (eq ?\n (aref docstring 0)) - "%s%s" - "%s: %s.") - docstring - (mapconcat - (lambda (x) - (format - (if (cdr x) - (concat "[%s]: " (cdr x)) - "%s") - (car x))) - (nreverse (mapcar #'cdr alist)) - ", ")))) + (mapconcat + (lambda (x) + (format + (if (cdr x) + (concat "[%s]: " (cdr x)) + "%s") + (car x))) + (nreverse (mapcar #'cdr alist)) + ", "))) (defun hydra--format (name body docstring heads) "Generate a `format' statement from STR. \"%`...\" expressions are extracted into \"%S\". NAME, HEADS and BODY-COLOR are parameters of `defhydra'. The expressions can be auto-expanded according to NAME." - (let ((str (hydra--hint name body docstring heads)) + (setq docstring (replace-regexp-in-string "\\^" "" docstring)) + (let ((rest (hydra--hint name body docstring heads)) (body-color (hydra--body-color body)) (prefix (symbol-name name)) (start 0) varlist) - (while (setq start (string-match "%`\\([a-z-A-Z/0-9]+\\)" str start)) - (push (hydra--unalias-var (match-string 1 str) prefix) varlist) - (setq str (replace-match "%S" nil nil str 0))) - (setq start 0) - (while (setq start (string-match "_\\([a-z-A-Z]+\\)_" str start)) - (let* ((key (match-string 1 str)) - (head (assoc key heads))) - (if head - (setq str (replace-match - (propertize key 'face (hydra--face head body)) - nil nil str)) - (error "Unrecognized key: _%s_" key)))) - `(format ,str ,@(nreverse varlist)))) + (while (setq start + (string-match + "\\(?:%\\( ?-?[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 (propertize key 'face (hydra--face head body)) varlist) + (setq docstring (replace-match "%s" 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)))) + (if (eq ?\n (aref docstring 0)) + `(concat (format ,docstring ,@(nreverse varlist)) + ,rest) + `(format ,(concat docstring ": " rest "."))))) (defun hydra--message (name body docstring heads) "Generate code to display the hint in the preferred echo area.