From fd8a6b0b0c564d8242259e20e557ee6041f40908 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Mon, 22 Jul 2019 14:12:09 +0200 Subject: tiny.el: Make "m10|fun(%s)" work Stop relying on 'cl, use 'cl-lib instead. --- tiny-test.el | 11 ++++++++--- tiny.el | 30 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/tiny-test.el b/tiny-test.el index ec4694d..dca458c 100644 --- a/tiny-test.el +++ b/tiny-test.el @@ -61,15 +61,20 @@ with point at the end of TXT." (should (equal (with-text-value "m\\n;; 10|%(+ x x) and %(* x x) and %s" #'tiny-mapconcat-parse) '(nil "\\n;; " "10" nil "%(+ x x) and %(* x x) and %s"))) (should (equal (with-text-value "m10|%0.2f" #'tiny-mapconcat-parse) - '(nil nil "10" nil "%0.2f")))) + '(nil nil "10" nil "%0.2f"))) + (should (equal (with-text-value "m1\\n999|fun%s();" #'tiny-mapconcat-parse) + '("1" "\\n" "999" nil "fun%s();")))) (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))) (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)")))) + '("m1\n5| (%c) %0.1f" "(+ x ?a -1)" "(* x 0.2)"))) + (should (equal (tiny-extract-sexps "fun%s ();") + '("fun%s ();" nil))) + (should (equal (tiny-extract-sexps "fun%s();") + '("fun%s();" nil)))) (ert-deftest tiny-mapconcat () (should (equal (with-text-value "m10" (lambda()(eval (tiny-mapconcat)))) diff --git a/tiny.el b/tiny.el index 652fe51..b04a310 100644 --- a/tiny.el +++ b/tiny.el @@ -104,8 +104,7 @@ ;;; Code: -(eval-when-compile - (require 'cl)) +(require 'cl-lib) (require 'help-fns) (require 'org) @@ -173,7 +172,7 @@ Defaults are used in place of null values." (read x) (if (>= (1+ idx) n-have) 'x - `(nth ,(incf idx) lst)))) + `(nth ,(cl-incf idx) lst)))) (cdr tes))))) ',seq ,(tiny--strip-newlines s1)))))) @@ -183,7 +182,7 @@ Defaults are used in place of null values." (width "[0-9]*") (precision "\\(?:\\.[0-9]+\\)?") (character "[sdoxXefgcS]?")) - (format "\\(%s%s%s%s\\)(" + (format "\\(%s%s%s%s\\)([^)]+)" flags width precision character))) (defun tiny-extract-sexps (str) @@ -200,12 +199,12 @@ Each element of FORMS corresponds to a `format'-style % form in STR. (setq start (1+ beg)) (cond ((= ?% (aref str (1+ beg))) - (incf start)) + (cl-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) + (cl-incf beg (length fexp)) + (cl-destructuring-bind (_sexp . end) (read-from-string str beg) (push (replace-regexp-in-string "(date" "(tiny-date" @@ -271,9 +270,10 @@ Return nil if nothing was matched, otherwise ;; or [expr][fmt] ;; ;; First, try to match [expr][fmt] - (string-match "^\\(.*?\\)|?\\(%.*\\)?$" str) - (setq expr (match-string-no-properties 1 str)) - (setq fmt (match-string-no-properties 2 str)) + (when (or (string-match "^\\([^|]*\\)|\\(.*\\)?$" str) + (string-match "^\\(.*?\\)\\(%.*\\)?$" str)) + (setq expr (match-string-no-properties 1 str)) + (setq fmt (match-string-no-properties 2 str))) ;; If it's a valid expression, we're done (when (setq expr (tiny-tokenize expr)) (setq n2 n1 @@ -324,13 +324,13 @@ Return nil if nothing was matched, otherwise (error "Unexpected \" \""))) ;; special syntax to read chars ((string= s "?") - (setq s (format "%s" (read (substring str i (incf j))))) + (setq s (format "%s" (read (substring str i (cl-incf j))))) (push s out) (push " " out)) ((string= s ")") ;; expect a close paren only if it's necessary (if (>= n-paren 0) - (decf n-paren) + (cl-decf n-paren) (error "Unexpected \")\"")) (when (string= (car out) " ") (pop out)) @@ -340,7 +340,7 @@ Return nil if nothing was matched, otherwise ;; open paren is used sometimes ;; when there are numbers in the expression (setq expect-fun t) - (incf n-paren) + (cl-incf n-paren) (push "(" out)) ((progn (setq sym (intern-soft s)) (cond @@ -352,7 +352,7 @@ Return nil if nothing was matched, otherwise ;; (when (zerop n-paren) (push "(" out)) (unless (equal (car out) "(") (push "(" out) - (incf n-paren)) + (cl-incf n-paren)) t) ((and sym (boundp sym) (not expect-fun)) t))) @@ -365,7 +365,7 @@ Return nil if nothing was matched, otherwise (push " " out) (setq j (+ i (length num-s))))) (t - (incf j) + (cl-incf j) nil)) (setq i j) (setq j (1+ i)))) -- cgit v1.0