[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master a6e4144 30/46: tiny.el (tiny-extract-sexps): can handle e.
From: |
Oleh Krehel |
Subject: |
[elpa] master a6e4144 30/46: tiny.el (tiny-extract-sexps): can handle e.g. %0.1f(...) style formatting. |
Date: |
Sun, 22 Mar 2015 17:42:34 +0000 |
branch: master
commit a6e4144169717609bdb1811ef3c97c7a60c8a6d9
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
tiny.el (tiny-extract-sexps): can handle e.g. %0.1f(...) style formatting.
* tiny.el (tiny-format-str): new constant
* tiny-test.el (tiny-extract-sexps): add test
---
tiny-test.el | 5 ++++-
tiny.el | 27 ++++++++++++++++++++-------
2 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/tiny-test.el b/tiny-test.el
index 77cfdc7..e959345 100644
--- a/tiny-test.el
+++ b/tiny-test.el
@@ -40,7 +40,10 @@ with point at the end of TXT."
(ert-deftest tiny-extract-sexps ()
(should (equal (tiny-extract-sexps "expr1 %(+ x x), nothing %% char %c, hex
%x, and expr2 %(* x x), float %0.2f and sym %s")
'("expr1 %s, nothing %% char %c, hex %x, and expr2 %s, float
%0.2f and sym %s"
- "(+ x x)" nil nil "(* x x)" nil nil))))
+ "(+ x x)" nil nil "(* x x)" nil nil)))
+ (should (equal (tiny-extract-sexps "m1\n5| (%c(+ x ?a -1)) %0.1f(* x 0.2)")
+ '("m1
+5| (%c) %0.1f" "(+ x ?a -1)" "(* x 0.2)"))))
(ert-deftest tiny-mapconcat ()
(should (equal (with-text-value "m10" (lambda()(eval (read
(tiny-mapconcat)))))
diff --git a/tiny.el b/tiny.el
index 319fc24..4cd8626 100644
--- a/tiny.el
+++ b/tiny.el
@@ -186,6 +186,14 @@ expression."
n2
s1))))))
+(defconst tiny-format-str
+ (let ((flags "[+ #-0]\\{0,1\\}")
+ (width "[0-9]*")
+ (precision "\\(?:\\.[0-9]+\\)?")
+ (character "[sdoxXefgcS]?"))
+ (format "\\(%s%s%s%s\\)("
+ flags width precision character)))
+
(defun tiny-extract-sexps (str)
"Returns (STR & FORMS), where each element of FORMS
corresponds to a `format'-style % form in STR.
@@ -197,16 +205,21 @@ corresponds to a `format'-style % form in STR.
forms beg)
(condition-case nil
(while (setq beg (string-match "%" str start))
- (incf beg)
- (setq start beg)
- (case (aref str beg)
- (?% (incf start))
- (?\( (destructuring-bind (sexp . end) (read-from-string str beg)
+ (setq start (1+ beg))
+
+ (cond ((= ?% (aref str (1+ beg)))
+ (incf start))
+
+ ((and (eq beg (string-match tiny-format-str str beg))
+ (setq fexp (match-string-no-properties 1 str)))
+ (incf beg (length fexp))
+ (destructuring-bind (sexp . end)
+ (read-from-string str beg)
(push (substring str beg end) forms)
(setq str (concat (substring str 0 beg)
- "s"
+ (if (string= fexp "%") "s" "")
(substring str end)))))
- (t (push nil forms))))
+ (t (push nil forms))))
(error (message "Malformed sexp: %s" (substring str beg))))
(cons str (nreverse forms))))
- [elpa] master 97b9fdf 14/46: tiny.el: fixed up the previous change., (continued)
- [elpa] master 97b9fdf 14/46: tiny.el: fixed up the previous change., Oleh Krehel, 2015/03/22
- [elpa] master 80e64b8 18/46: README.md: minor update., Oleh Krehel, 2015/03/22
- [elpa] master 2bd9abb 21/46: tiny.el: moved tests to tiny-test.el., Oleh Krehel, 2015/03/22
- [elpa] master b99315d 20/46: tiny.el: updated the doc., Oleh Krehel, 2015/03/22
- [elpa] master 11722a1 19/46: tiny.el: fixed typos., Oleh Krehel, 2015/03/22
- [elpa] master b4fff55 23/46: tiny.el: fixed `tiny-tokenize', Oleh Krehel, 2015/03/22
- [elpa] master 5df4f79 25/46: tiny.el: removed `tiny-beginning-of-string', Oleh Krehel, 2015/03/22
- [elpa] master 8a3cab0 27/46: tiny.el: removed `tiny-replace-preceding-sexp', Oleh Krehel, 2015/03/22
- [elpa] master f1d1a2e 22/46: tiny-test.el: switched to ERT., Oleh Krehel, 2015/03/22
- [elpa] master 4c87ade 29/46: tiny.el: fixed a bug in `tiny-mapconcat-parse'., Oleh Krehel, 2015/03/22
- [elpa] master a6e4144 30/46: tiny.el (tiny-extract-sexps): can handle e.g. %0.1f(...) style formatting.,
Oleh Krehel <=
- [elpa] master 80cd28f 28/46: tiny-test.el: added a test for `tiny-replace-this-sexp', Oleh Krehel, 2015/03/22
- [elpa] master 08ba593 24/46: tiny.el: fixed `tiny-tokenize', Oleh Krehel, 2015/03/22
- [elpa] master e6abbc6 31/46: tiny.el (tiny-date): renamed from t-date. In format string it's just "date", Oleh Krehel, 2015/03/22
- [elpa] master c59bc94 36/46: tiny.el (tiny-extract-sexps): add missing let binding, Oleh Krehel, 2015/03/22
- [elpa] master cf74dd1 34/46: tiny.el: fix docs, Oleh Krehel, 2015/03/22
- [elpa] master b843226 26/46: tiny.el: removed `tiny-replace-sexp-desperately', Oleh Krehel, 2015/03/22
- [elpa] master 7893a02 35/46: tiny.el: add autoload to `tiny-expand', Oleh Krehel, 2015/03/22
- [elpa] master 075e4d0 33/46: Fix "m10|%0.2f", Oleh Krehel, 2015/03/22
- [elpa] master 4eef4e6 37/46: tiny.el: add a test case, Oleh Krehel, 2015/03/22
- [elpa] master 3a56283 38/46: tiny.el (tiny-mapconcat-parse): case-sensitive, Oleh Krehel, 2015/03/22