emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 481d36e 1/3: tiny.el: tiny-mapconcat returns sexp, not str


From: Oleh Krehel
Subject: [elpa] master 481d36e 1/3: tiny.el: tiny-mapconcat returns sexp, not string
Date: Sun, 3 Sep 2017 09:12:10 -0400 (EDT)

branch: master
commit 481d36e47e51f27e64c826633c01518459f17d1c
Author: Stefan Monnier <address@hidden>
Commit: Oleh Krehel <address@hidden>

    tiny.el: tiny-mapconcat returns sexp, not string
    
    * tiny-test.el: Update tests.
    
    * Makefile: Set `LC_ALL'.
---
 Makefile     |   3 +-
 tiny-test.el |  64 +++++++++++++++------------------
 tiny.el      | 116 ++++++++++++++++++-----------------------------------------
 3 files changed, 64 insertions(+), 119 deletions(-)

diff --git a/Makefile b/Makefile
index a63f955..c56adc8 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,9 @@ all: test
 update:
        $(emacs) -batch -l targets/install-deps.el
 
+# Use LC_ALL=C to avoid locale dependencies in the dates!
 test: clean
-       $(BEMACS) -l tiny-test.el -l tiny.el -f ert-run-tests-batch-and-exit
+       LC_ALL=C $(BEMACS) -l tiny-test.el -l tiny.el -f 
ert-run-tests-batch-and-exit
 
 compile:
        $(emacs) -Q -batch -f batch-byte-compile tiny.el
diff --git a/tiny-test.el b/tiny-test.el
index 1c85aa1..ec4694d 100644
--- a/tiny-test.el
+++ b/tiny-test.el
@@ -1,6 +1,6 @@
 ;;; tiny-test.el --- Tests for Tiny
 
-;; Copyright (C) 2015  Free Software Foundation, Inc.
+;; Copyright (C) 2015, 2017  Free Software Foundation, Inc.
 
 ;; Author: Oleh Krehel
 
@@ -25,7 +25,7 @@
 (require 'tiny nil t)
 
 (defun with-text-value (txt fn &rest args)
-  "Return the result of (apply 'FN ARGS), in a temp buffer with TXT,
+  "Return the result of (apply FN ARGS), in a temp buffer with TXT,
 with point at the end of TXT."
   (with-temp-buffer
     (insert txt)
@@ -72,41 +72,41 @@ with point at the end of TXT."
 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)))))
+  (should (equal (with-text-value "m10" (lambda()(eval (tiny-mapconcat))))
                  "0 1 2 3 4 5 6 7 8 9 10"))
-  (should (equal (with-text-value "mm10" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "mm10" (lambda()(eval (tiny-mapconcat))))
                  "012345678910"))
-  (should (equal (with-text-value "m5 10" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m5 10" (lambda()(eval (tiny-mapconcat))))
                  "5 6 7 8 9 10"))
-  (should (equal (with-text-value "m5 10*xx" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m5 10*xx" (lambda()(eval (tiny-mapconcat))))
                  "25 36 49 64 81 100"))
-  (should (equal (with-text-value "m5 10*xx%x" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m5 10*xx%x" (lambda()(eval 
(tiny-mapconcat))))
                  "19 24 31 40 51 64"))
-  (should (equal (with-text-value "m5 10*xx|0x%x" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m5 10*xx|0x%x" (lambda()(eval 
(tiny-mapconcat))))
                  "0x19 0x24 0x31 0x40 0x51 0x64"))
-  (should (equal (with-text-value "m25+x?a%c" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m25+x?a%c" (lambda()(eval 
(tiny-mapconcat))))
                  "a b c d e f g h i j k l m n o p q r s t u v w x y z"))
-  (should (equal (with-text-value "m25+x?A%c" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m25+x?A%c" (lambda()(eval 
(tiny-mapconcat))))
                  "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"))
-  (should (equal (with-text-value "m97,122(string x)" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m97,122(string x)" (lambda()(eval 
(tiny-mapconcat))))
                  "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"))
-  (should (equal (with-text-value "m97,122stringxx" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m97,122stringxx" (lambda()(eval 
(tiny-mapconcat))))
                  
"aa,bb,cc,dd,ee,ff,gg,hh,ii,jj,kk,ll,mm,nn,oo,pp,qq,rr,ss,tt,uu,vv,ww,xx,yy,zz"))
-  (should (equal (with-text-value "m97,120stringxupcasex" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m97,120stringxupcasex" (lambda()(eval 
(tiny-mapconcat))))
                  
"aA,bB,cC,dD,eE,fF,gG,hH,iI,jJ,kK,lL,mM,nN,oO,pP,qQ,rR,sS,tT,uU,vV,wW,xX"))
-  (should (equal (with-text-value "m97,120stringxupcasex)x" (lambda()(eval 
(read (tiny-mapconcat)))))
+  (should (equal (with-text-value "m97,120stringxupcasex)x" (lambda()(eval 
(tiny-mapconcat))))
                  
"aAa,bBb,cCc,dDd,eEe,fFf,gGg,hHh,iIi,jJj,kKk,lLl,mMm,nNn,oOo,pPp,qQq,rRr,sSs,tTt,uUu,vVv,wWw,xXx"))
-  (should (equal (with-text-value "m10|%(+ x x) and %(* x x) and %s" 
(lambda()(eval (read (tiny-mapconcat)))))
+  (should (equal (with-text-value "m10|%(+ x x) and %(* x x) and %s" 
(lambda()(eval (tiny-mapconcat))))
                  "0 and 0 and 0 2 and 1 and 1 4 and 4 and 2 6 and 9 and 3 8 
and 16 and 4 10 and 25 and 5 12 and 36 and 6 14 and 49 and 7 16 and 64 and 8 18 
and 81 and 9 20 and 100 and 10"))
-  (should (equal (with-text-value "m10*2+3x" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m10*2+3x" (lambda()(eval (tiny-mapconcat))))
                  "6 8 10 12 14 16 18 20 22 24 26"))
-  (should (equal (with-text-value "m10expx" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m10expx" (lambda()(eval (tiny-mapconcat))))
                  "1.0 2.718281828459045 7.38905609893065 20.085536923187668 
54.598150033144236 148.4131591025766 403.4287934927351 1096.6331584284585 
2980.9579870417283 8103.083927575384 22026.465794806718"))
-  (should (equal (with-text-value "m5 20expx%014.2f" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m5 20expx%014.2f" (lambda()(eval 
(tiny-mapconcat))))
                  "00000000148.41 00000000403.43 00000001096.63 00000002980.96 
00000008103.08 00000022026.47 00000059874.14 00000162754.79 00000442413.39 
00001202604.28 00003269017.37 00008886110.52 00024154952.75 00065659969.14 
00178482300.96 00485165195.41"))
-  (should (equal (with-text-value "m, 7|0x%02x" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m, 7|0x%02x" (lambda()(eval 
(tiny-mapconcat))))
                  "0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07"))
-  (should (equal (with-text-value "m1\\n14|*** TODO 
http://emacsrocks.com/e%02d.html"; (lambda()(eval (read (tiny-mapconcat)))))
+  (should (equal (with-text-value "m1\\n14|*** TODO 
http://emacsrocks.com/e%02d.html"; (lambda()(eval (tiny-mapconcat))))
                  "*** TODO http://emacsrocks.com/e01.html
 *** TODO http://emacsrocks.com/e02.html
 *** TODO http://emacsrocks.com/e03.html
@@ -121,7 +121,7 @@ with point at the end of TXT."
 *** TODO http://emacsrocks.com/e12.html
 *** TODO http://emacsrocks.com/e13.html
 *** TODO http://emacsrocks.com/e14.html";))
-  (should (equal (with-text-value "m1\\n10|convert img%s.jpg -monochrome 
-resize 50%% -rotate 180 img%s_mono.pdf" (lambda()(eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m1\\n10|convert img%s.jpg -monochrome 
-resize 50%% -rotate 180 img%s_mono.pdf" (lambda()(eval (tiny-mapconcat))))
                  "convert img1.jpg -monochrome -resize 50% -rotate 180 
img1_mono.pdf
 convert img2.jpg -monochrome -resize 50% -rotate 180 img2_mono.pdf
 convert img3.jpg -monochrome -resize 50% -rotate 180 img3_mono.pdf
@@ -132,7 +132,7 @@ convert img7.jpg -monochrome -resize 50% -rotate 180 
img7_mono.pdf
 convert img8.jpg -monochrome -resize 50% -rotate 180 img8_mono.pdf
 convert img9.jpg -monochrome -resize 50% -rotate 180 img9_mono.pdf
 convert img10.jpg -monochrome -resize 50% -rotate 180 img10_mono.pdf"))
-  (should (equal (with-text-value "m\\n;; 16list*xxx)*xx%s:%s:%s" 
(lambda()(eval (read (tiny-mapconcat)))))
+  (should (equal (with-text-value "m\\n;; 16list*xxx)*xx%s:%s:%s" 
(lambda()(eval (tiny-mapconcat))))
                  "0:0:0
 ;; 1:1:1
 ;; 8:4:2
@@ -151,7 +151,7 @@ convert img10.jpg -monochrome -resize 50% -rotate 180 
img10_mono.pdf"))
 ;; 3375:225:15
 ;; 4096:256:16"))
   (should (equal (with-text-value "m\\n8|**** TODO Learning from Data Week %(+ 
x 2)\\nSCHEDULED: <%(date \"Oct 7 2015\" (* x 7))> DEADLINE: <%(date \"Oct 14 
2015\" (* x 7))>"
-                   (lambda()(eval (read (tiny-mapconcat)))))
+                   (lambda()(eval (tiny-mapconcat))))
                  "**** TODO Learning from Data Week 2
 SCHEDULED: <2015-10-07 Wed> DEADLINE: <2015-10-14 Wed>
 **** TODO Learning from Data Week 3
@@ -171,7 +171,7 @@ SCHEDULED: <2015-11-25 Wed> DEADLINE: <2015-12-02 Wed>
 **** TODO Learning from Data Week 10
 SCHEDULED: <2015-12-02 Wed> DEADLINE: <2015-12-09 Wed>"))
   (should (string= (with-text-value "m\\n4|**** TODO Classical Mechanics Week 
%(+ x 5)\\nSCHEDULED: <%(date \"Oct 15 2015\" (* x 7))> DEADLINE: <%(date \"Oct 
23 2015\" (* x 7))>"
-                     (lambda()(eval (read (tiny-mapconcat)))))
+                     (lambda()(eval (tiny-mapconcat))))
                    "**** TODO Classical Mechanics Week 5
 SCHEDULED: <2015-10-15 Thu> DEADLINE: <2015-10-23 Fri>
 **** TODO Classical Mechanics Week 6
@@ -183,20 +183,12 @@ SCHEDULED: <2015-11-05 Thu> DEADLINE: <2015-11-13 Fri>
 **** TODO Classical Mechanics Week 9
 SCHEDULED: <2015-11-12 Thu> DEADLINE: <2015-11-20 Fri>"))
   (should (string= (with-text-value "m7|%(expt 2 x)"
-                     (lambda()(eval (read (tiny-mapconcat)))))
+                     (lambda()(eval (tiny-mapconcat))))
                    "1 2 4 8 16 32 64 128"))
   (should (string= (with-text-value "m\\n25+?ax|(\"%c\")"
-                     (lambda()(eval (read (tiny-mapconcat)))))
+                     (lambda()(eval (tiny-mapconcat))))
                    
"(\"a\")\n(\"b\")\n(\"c\")\n(\"d\")\n(\"e\")\n(\"f\")\n(\"g\")\n(\"h\")\n(\"i\")\n(\"j\")\n(\"k\")\n(\"l\")\n(\"m\")\n(\"n\")\n(\"o\")\n(\"p\")\n(\"q\")\n(\"r\")\n(\"s\")\n(\"t\")\n(\"u\")\n(\"v\")\n(\"w\")\n(\"x\")\n(\"y\")\n(\"z\")")))
 
-(ert-deftest tiny-replace-this-sexp ()
-  (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
-                   (lambda()(goto-char 
16)(tiny-replace-this-sexp)(buffer-string)))
-                 "(1 4 9)"))
-  (should (equal (with-text-value "(mapcar (lambda (x) (* x x)) '(1 2 3))"
-                   (lambda()(goto-char 
2)(tiny-replace-this-sexp)(buffer-string)))
-                 "(1 4 9)")))
-
 (ert-deftest tiny-tokenize ()
     (should (equal (tiny-tokenize "stringxx") "(string x x)"))
     (should (equal (tiny-tokenize "*2+xxx") "(* 2 (+ x x x))"))
@@ -214,10 +206,10 @@ SCHEDULED: <2015-11-12 Thu> DEADLINE: <2015-11-20 Fri>"))
     (should (equal (tiny-tokenize "(string x (upcase x) x)") "(string x 
(upcase x) x)")))
 
 (ert-deftest tiny-decreasing-seq ()
-  (should (equal (with-text-value "m2 -2" (lambda () (eval (read 
(tiny-mapconcat)))))
+  (should (equal (with-text-value "m2 -2" (lambda () (eval (tiny-mapconcat))))
                  "2 1 0 -1 -2"))
   (should (equal (with-text-value "m3 -1|%(+ x x)" (lambda ()
-                                                     (eval (read 
(tiny-mapconcat)))))
+                                                     (eval (tiny-mapconcat))))
                  "6 4 2 0 -2")))
 
 (provide 'tiny-test)
diff --git a/tiny.el b/tiny.el
index 48ca65d..66eea40 100644
--- a/tiny.el
+++ b/tiny.el
@@ -1,10 +1,10 @@
 ;;; tiny.el --- Quickly generate linear ranges in Emacs
 
-;; Copyright (C) 2013-2015  Free Software Foundation, Inc.
+;; Copyright (C) 2013-2015, 2017  Free Software Foundation, Inc.
 
 ;; Author: Oleh Krehel <address@hidden>
 ;; URL: https://github.com/abo-abo/tiny
-;; Version: 0.2.0
+;; Version: 0.2.1
 ;; Keywords: convenience
 
 ;; This file is part of GNU Emacs.
@@ -126,69 +126,25 @@ to the bounds of the snippet that they matched.
 At the moment, only `tiny-mapconcat' is supported.
 `tiny-mapconcat2' should be added to expand rectangles."
   (interactive)
-  (let ((str (tiny-mapconcat)))
-    (when str
+  (let ((e (tiny-mapconcat)))
+    (when e
+      (goto-char tiny-beg)
       (delete-region tiny-beg tiny-end)
-      (insert str)
-      (tiny-replace-this-sexp))))
+      (insert (eval e t)))))
 
 (defun tiny-setup-default ()
   "Setup shortcuts."
   (global-set-key (kbd "C-;") 'tiny-expand))
 
-(defalias 'tiny--preceding-sexp
-    (if (fboundp 'elisp--preceding-sexp)
-        'elisp--preceding-sexp
-      'preceding-sexp))
-
-;;;###autoload
-(defun tiny-replace-this-sexp ()
-  "Eval and replace the current sexp.
-On error go up list and try again."
-  (interactive)
-  (if (region-active-p)
-      (let ((s (buffer-substring-no-properties
-                (region-beginning)
-                (region-end))))
-        (delete-region (region-beginning)
-                       (region-end))
-        (insert (format "%s" (eval (read s)))))
-    (catch 'success
-      (while t
-        (ignore-errors
-          (unless (looking-back ")" (line-beginning-position))
-            (error "Bad location"))
-          (let ((sexp (tiny--preceding-sexp)))
-            (if (eq (car sexp) 'lambda)
-                (error "Lambda evaluates to itself")
-              (let ((value (eval sexp)))
-                (kill-sexp -1)
-                (insert (format "%s" value))
-                (throw 'success t)))))
-        ;; if can't replace, go up list
-        (condition-case nil
-            (tiny-up-list)
-          (error
-           (message "reached the highest point, couldn't eval.")
-           (throw 'success nil)))))))
-
-(defun tiny-up-list ()
-  "An `up-list' that can exit from string.
-Must throw an error when can't go up further."
-  (interactive)
-  ;; check if inside string
-  (let ((p (nth 8 (syntax-ppss))))
-    (when (eq (char-after p) ?\")
-      ;; go to beginning for string
-      (goto-char p)))
-  (up-list))
+(defun tiny--strip-\n (str)
+  (replace-regexp-in-string "\\\\n" "\n" str))
 
 (defun tiny-mapconcat ()
   "Format output of `tiny-mapconcat-parse'.
 Defaults are used in place of null values."
   (let ((parsed (tiny-mapconcat-parse)))
     (when parsed
-      (let* ((n0 (or (nth 0 parsed) "0"))
+      (let* ((n0 (string-to-number (or (nth 0 parsed) "0")))
              (n1 (nth 1 parsed))
              (s1 (cond ((null n1)
                         " ")
@@ -196,7 +152,7 @@ Defaults are used in place of null values."
                         "")
                        (t
                         n1)))
-             (n2 (nth 2 parsed))
+             (n2 (read (nth 2 parsed)))
              (expr (or (nth 3 parsed) "x"))
              (lexpr (read expr))
              (n-have (if (and (listp lexpr) (eq (car lexpr) 'list))
@@ -206,26 +162,20 @@ Defaults are used in place of null values."
              (n-have (if (zerop n-have) 1 n-have))
              (tes (tiny-extract-sexps (or (nth 4 parsed) "%s")))
              (fmt (car tes))
-             (n-need (cl-count nil (cdr tes)))
              (idx -1)
-             (seq (number-sequence (read n0) (read n2)
-                                   (if (>= (read n0) (read n2)) -1 1)))
-             (format-expression
-              (concat "(mapconcat (lambda(x) (let ((lst %s)) (format %S "
-                      (mapconcat (lambda (x)
-                                   (or x
-                                       (if (>= (1+ idx) n-have)
-                                           "x"
-                                         (format "(nth %d lst)" (incf idx)))))
-                                 (cdr tes)
-                                 " ")
-                      ")))'%S \"%s\")")))
-        (format
-         format-expression
-         expr
-         (replace-regexp-in-string "\\\\n" "\n" fmt)
-         seq
-         s1)))))
+             (seq (number-sequence n0 n2 (if (>= n0 n2) -1 1))))
+        `(mapconcat (lambda (x)
+                      (let ((lst ,expr))
+                        (format ,(tiny--strip-\n fmt)
+                                ,@(mapcar (lambda (x)
+                                            (if x
+                                                (read x)
+                                              (if (>= (1+ idx) n-have)
+                                                  'x
+                                                `(nth ,(incf idx) lst))))
+                                          (cdr tes)))))
+                    ',seq
+                    ,(tiny--strip-\n s1))))))
 
 (defconst tiny-format-str
   (let ((flags "[+ #-0]\\{0,1\\}")
@@ -254,7 +204,7 @@ Each element of FORMS corresponds to a `format'-style % 
form in STR.
                 ((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)
+                 (destructuring-bind (_sexp . end)
                      (read-from-string str beg)
                    (push
                     (replace-regexp-in-string "(date" "(tiny-date"
@@ -276,22 +226,21 @@ m[START][SEPARATOR]END[EXPR]|[FORMAT]
 * END       - integer (required)
 * EXPR      - Lisp expression: function body with argument x (defaults to x)
   Parens are optional if it's unambiguous:
-  - `(* 2 (+ x 3))'  <-> *2+x3
-  - `(exp x)'        <-> expx
+  - `(* 2 (+ x 3))'   <-> *2+x3
+  - `(exp x)'         <-> expx
   A closing paren may be added to resolve ambiguity:
-  - `(* 2 (+ x 3) x) <-> *2+x3)
+  - `(* 2 (+ x 3) x)' <-> *2+x3)
 * FORMAT    - string, `format'-style (defaults to \"%s\")
   | separator can be omitted if FORMAT starts with %.
 
 Return nil if nothing was matched, otherwise
  (START SEPARATOR END EXPR FORMAT)"
   (let ((case-fold-search nil)
-        n1 s1 n2 expr fmt str
-        n-uses)
+        n1 s1 n2 expr fmt str)
     (when (catch 'done
             (cond
               ;; either start with a number
-              ((looking-back "\\bm\\(-?[0-9]+\\)\\([^\n]*?\\)"
+              ((looking-back "\\bm\\(-?[0-9]+\\)\\(.*?\\)"
                              (line-beginning-position))
                (setq n1 (match-string-no-properties 1)
                      str (match-string-no-properties 2)
@@ -458,8 +407,11 @@ Usage: Call TINY-HELPER, ↵↵↵↵↵            -> 0 1 2 3 4 5 6 
7 8 9
        Call TINY-HELPER, 9↵2↵_↵+1*x2↵↵    -> 5_7_9_11_13_15_17_19
        Call TINY-HELPER, 15↵1↵↵-30*2x↵%x↵ -> 1c 1a 18 16 14 12 10 e c a 8 6 4 
2 0"
   (interactive
-   (when (and (null (barf-if-buffer-read-only)) ;Use the helper only if tiny 
expansion is not
-              (null (tiny-mapconcat))) ;possible at point and if the buffer is 
editable.
+   (unless (or
+            ;; Use the helper only if tiny expansion is not
+            ;; possible at point and if the buffer is editable.d
+            (barf-if-buffer-read-only)
+            (tiny-mapconcat))
      (let ((prompt (propertize "tiny-helper: " 'face 
'font-lock-function-name-face)))
        (list (read-string (concat prompt
                                   "END value "



reply via email to

[Prev in Thread] Current Thread [Next in Thread]