[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 1eebfed 34/72: Allow `format'-style width specifiers in do
From: |
Oleh Krehel |
Subject: |
[elpa] master 1eebfed 34/72: Allow `format'-style width specifiers in docstring |
Date: |
Fri, 06 Mar 2015 13:04:12 +0000 |
branch: master
commit 1eebfed70e792d266cb08c6e6b11bec140a0977f
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
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.
- [elpa] master 42cb833 21/72: hydra.el (hydra--hint): Take same arguments as `defhydra', (continued)
- [elpa] master 42cb833 21/72: hydra.el (hydra--hint): Take same arguments as `defhydra', Oleh Krehel, 2015/03/06
- [elpa] master 0a3cc60 30/72: Add compat color-less syntax, Oleh Krehel, 2015/03/06
- [elpa] master 92e1922 19/72: hydra.el (hydra--hint): Take same arguments as `defhydra', Oleh Krehel, 2015/03/06
- [elpa] master 6f7cef2 22/72: Add teal body color, Oleh Krehel, 2015/03/06
- [elpa] master 7010772 32/72: hydra.el (hydra--head-color): Improve, Oleh Krehel, 2015/03/06
- [elpa] master d0e8d57 33/72: hydra.el (hydra-add-font-lock): Add `defhydradio', Oleh Krehel, 2015/03/06
- [elpa] master 0881733 28/72: hydra.el (defhydra): Fix typo, Oleh Krehel, 2015/03/06
- [elpa] master a4c4eb6 36/72: Add example for `Buffer-menu-mode', Oleh Krehel, 2015/03/06
- [elpa] master 88b32d5 24/72: hydra.el (hydra--hint): Add exception, Oleh Krehel, 2015/03/06
- [elpa] master c41c932 31/72: README.md: fix tables, Oleh Krehel, 2015/03/06
- [elpa] master 1eebfed 34/72: Allow `format'-style width specifiers in docstring,
Oleh Krehel <=
- [elpa] master e962ff1 39/72: hydra.el: remove no :pre, :post restriction for Emacs<24.4, Oleh Krehel, 2015/03/06
- [elpa] master a6c6a55 38/72: Improve compatibility for Emacs <= 24.3, Oleh Krehel, 2015/03/06
- [elpa] master 2c01db0 42/72: hydra.el (hydra--handle-nonhead): Update signature, Oleh Krehel, 2015/03/06
- [elpa] master a71b76e 35/72: Intercept quit signals, Oleh Krehel, 2015/03/06
- [elpa] master ae70e9a 41/72: Fix pink and teal Hydras running :post, Oleh Krehel, 2015/03/06
- [elpa] master 71d70f6 46/72: Add :body-pre switch, Oleh Krehel, 2015/03/06
- [elpa] master cb28124 44/72: Allow sexps in Ruby-style docstrings, Oleh Krehel, 2015/03/06
- [elpa] master ace99b3 51/72: hydra.el (hydra--make-defun): Update signature, Oleh Krehel, 2015/03/06
- [elpa] master efcffc0 45/72: hydra-examples.el: Add example 9, Oleh Krehel, 2015/03/06
- [elpa] master ef1f61d 40/72: hydra.el (hydra--handle-nonhead): Fix bug, Oleh Krehel, 2015/03/06