aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Kangas2019-05-05 15:48:57 +0200
committerBasil L. Contovounesios2019-05-20 15:29:26 +0100
commit4498e5a13a3b63a3024ceef102ae3b5c50f58be1 (patch)
tree7f4d1809c602b93ac73eacf7ea25d65a87c3a85b
parent9813905f834aa43eb194023f579c7e7951d96d0f (diff)
downloademacs-4498e5a13a3b63a3024ceef102ae3b5c50f58be1.tar.gz
emacs-4498e5a13a3b63a3024ceef102ae3b5c50f58be1.zip
Use lexical-binding in delim-col.el and add tests
Thanks to Basil L. Contovounesios for additional cleanups. For discussion, see the following thread: https://lists.gnu.org/archive/html/emacs-devel/2019-05/msg00177.html * lisp/delim-col.el: Use lexical-binding. * test/lisp/delim-col-tests.el: New file. (delim-col-tests-delimit-colummns-before-after) (delim-col-tests-delimit-columns) (delim-col-tests-delimit-columns-format/nil) (delim-col-tests-delimit-columns-format/padding) (delim-col-tests-delimit-columns-format/separator) (delim-col-tests-delimit-columns-separator) (delim-col-tests-delimit-columns-str-before-after) (delim-col-tests-delimit-columns-str-separator) (delim-col-tests-delimit-rectangle): New unit tests.
-rw-r--r--lisp/delim-col.el70
-rw-r--r--test/lisp/delim-col-tests.el181
2 files changed, 212 insertions, 39 deletions
diff --git a/lisp/delim-col.el b/lisp/delim-col.el
index a968b32052b..4b4fc7fe960 100644
--- a/lisp/delim-col.el
+++ b/lisp/delim-col.el
@@ -1,12 +1,12 @@
1;;; delim-col.el --- prettify all columns in a region or rectangle 1;;; delim-col.el --- prettify all columns in a region or rectangle -*- lexical-binding: t; -*-
2 2
3;; Copyright (C) 1999-2019 Free Software Foundation, Inc. 3;; Copyright (C) 1999-2019 Free Software Foundation, Inc.
4 4
5;; Author: Vinicius Jose Latorre <viniciusjl.gnu@gmail.com> 5;; Author: Vinicius Jose Latorre <viniciusjl.gnu@gmail.com>
6;; Maintainer: Vinicius Jose Latorre <viniciusjl.gnu@gmail.com> 6;; Maintainer: Vinicius Jose Latorre <viniciusjl.gnu@gmail.com>
7;; Version: 2.1 7;; Version: 2.1
8;; Keywords: internal 8;; Keywords: convenience text
9;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre 9;; X-URL: https://www.emacswiki.org/emacs/ViniciusJoseLatorre
10 10
11;; This file is part of GNU Emacs. 11;; This file is part of GNU Emacs.
12 12
@@ -27,11 +27,6 @@
27 27
28;; delim-col helps to prettify columns in a text region or rectangle. 28;; delim-col helps to prettify columns in a text region or rectangle.
29;; 29;;
30;; To use it, make sure that this file is in load-path and insert in your
31;; .emacs:
32;;
33;; (require 'delim-col)
34;;
35;; If you have, for example, the following columns: 30;; If you have, for example, the following columns:
36;; 31;;
37;; a b c d 32;; a b c d
@@ -91,9 +86,9 @@
91;; aaa [ <bbb>, <cccc> ] dddd 86;; aaa [ <bbb>, <cccc> ] dddd
92;; aa [ <bb> , <ccccccc> ] ddd 87;; aa [ <bb> , <ccccccc> ] ddd
93;; 88;;
94;; Note that `delimit-columns-region' operates over all text region 89;; Note that `delimit-columns-region' operates over the entire selected
95;; selected, extending the region start to the beginning of line and the 90;; text region, extending the region start to the beginning of line and
96;; region end to the end of line. While `delimit-columns-rectangle' 91;; the region end to the end of line. While `delimit-columns-rectangle'
97;; operates over the text rectangle selected which rectangle diagonal is 92;; operates over the text rectangle selected which rectangle diagonal is
98;; given by the region start and end. 93;; given by the region start and end.
99;; 94;;
@@ -117,6 +112,7 @@
117 112
118;;; Code: 113;;; Code:
119 114
115(require 'rect)
120 116
121;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 117;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122;; User Options: 118;; User Options:
@@ -125,6 +121,7 @@
125 "Prettify columns." 121 "Prettify columns."
126 :link '(emacs-library-link :tag "Source Lisp File" "delim-col.el") 122 :link '(emacs-library-link :tag "Source Lisp File" "delim-col.el")
127 :prefix "delimit-columns-" 123 :prefix "delimit-columns-"
124 :group 'convenience
128 :group 'text) 125 :group 'text)
129 126
130(defcustom delimit-columns-str-before "" 127(defcustom delimit-columns-str-before ""
@@ -213,10 +210,11 @@ See also `delimit-columns-end' for documentation.
213The following relation must hold: 210The following relation must hold:
214 0 <= delimit-columns-start <= delimit-columns-end 211 0 <= delimit-columns-start <= delimit-columns-end
215 212
216The column number start from 0 and it's relative to the beginning of selected 213The column number starts at 0 and is relative to the beginning of
217region. So if you selected a text region, the first column (column 0) is 214the selected region. So if you select a text region, the first
218located at beginning of line. If you selected a text rectangle, the first 215column (column 0) is located at the beginning of line. If you
219column (column 0) is located at left corner." 216select a text rectangle, the first column (column 0) is located
217at the left corner."
220 :type '(integer :tag "Column Start") 218 :type '(integer :tag "Column Start")
221 :group 'columns) 219 :group 'columns)
222 220
@@ -228,10 +226,11 @@ See also `delimit-columns-start' for documentation.
228The following relation must hold: 226The following relation must hold:
229 0 <= delimit-columns-start <= delimit-columns-end 227 0 <= delimit-columns-start <= delimit-columns-end
230 228
231The column number start from 0 and it's relative to the beginning of selected 229The column number starts at 0 and is relative to the beginning of
232region. So if you selected a text region, the first column (column 0) is 230the selected region. So if you select a text region, the first
233located at beginning of line. If you selected a text rectangle, the first 231column (column 0) is located at the beginning of line. If you
234column (column 0) is located at left corner." 232select a text rectangle, the first column (column 0) is located
233at the left corner."
235 :type '(integer :tag "Column End") 234 :type '(integer :tag "Column End")
236 :group 'columns) 235 :group 'columns)
237 236
@@ -247,20 +246,20 @@ column (column 0) is located at left corner."
247 246
248;;;###autoload 247;;;###autoload
249(defun delimit-columns-customize () 248(defun delimit-columns-customize ()
250 "Customization of `columns' group." 249 "Customize the `columns' group."
251 (interactive) 250 (interactive)
252 (customize-group 'columns)) 251 (customize-group 'columns))
253 252
254 253
255(defmacro delimit-columns-str (str) 254(defun delimit-columns-str (str)
256 `(if (stringp ,str) ,str "")) 255 (if (stringp str) str ""))
257 256
258 257
259;;;###autoload 258;;;###autoload
260(defun delimit-columns-region (start end) 259(defun delimit-columns-region (start end)
261 "Prettify all columns in a text region. 260 "Prettify all columns in a text region.
262 261
263START and END delimits the text region." 262START and END delimit the text region."
264 (interactive "*r") 263 (interactive "*r")
265 (let ((delimit-columns-str-before 264 (let ((delimit-columns-str-before
266 (delimit-columns-str delimit-columns-str-before)) 265 (delimit-columns-str delimit-columns-str-before))
@@ -273,8 +272,7 @@ START and END delimits the text region."
273 (delimit-columns-after 272 (delimit-columns-after
274 (delimit-columns-str delimit-columns-after)) 273 (delimit-columns-str delimit-columns-after))
275 (delimit-columns-start 274 (delimit-columns-start
276 (if (and (integerp delimit-columns-start) 275 (if (natnump delimit-columns-start)
277 (>= delimit-columns-start 0))
278 delimit-columns-start 276 delimit-columns-start
279 0)) 277 0))
280 (delimit-columns-end 278 (delimit-columns-end
@@ -309,14 +307,11 @@ START and END delimits the text region."
309 (set-marker the-end nil))))) 307 (set-marker the-end nil)))))
310 308
311 309
312(require 'rect)
313
314
315;;;###autoload 310;;;###autoload
316(defun delimit-columns-rectangle (start end) 311(defun delimit-columns-rectangle (start end)
317 "Prettify all columns in a text rectangle. 312 "Prettify all columns in a text rectangle.
318 313
319START and END delimits the corners of text rectangle." 314START and END delimit the corners of the text rectangle."
320 (interactive "*r") 315 (interactive "*r")
321 (let ((delimit-columns-str-before 316 (let ((delimit-columns-str-before
322 (delimit-columns-str delimit-columns-str-before)) 317 (delimit-columns-str delimit-columns-str-before))
@@ -329,8 +324,7 @@ START and END delimits the corners of text rectangle."
329 (delimit-columns-after 324 (delimit-columns-after
330 (delimit-columns-str delimit-columns-after)) 325 (delimit-columns-str delimit-columns-after))
331 (delimit-columns-start 326 (delimit-columns-start
332 (if (and (integerp delimit-columns-start) 327 (if (natnump delimit-columns-start)
333 (>= delimit-columns-start 0))
334 delimit-columns-start 328 delimit-columns-start
335 0)) 329 0))
336 (delimit-columns-end 330 (delimit-columns-end
@@ -344,11 +338,11 @@ START and END delimits the corners of text rectangle."
344 ;; get maximum length for each column 338 ;; get maximum length for each column
345 (and delimit-columns-format 339 (and delimit-columns-format
346 (save-excursion 340 (save-excursion
347 (operate-on-rectangle 'delimit-columns-rectangle-max 341 (operate-on-rectangle #'delimit-columns-rectangle-max
348 start the-end nil))) 342 start the-end nil)))
349 ;; prettify columns 343 ;; prettify columns
350 (save-excursion 344 (save-excursion
351 (operate-on-rectangle 'delimit-columns-rectangle-line 345 (operate-on-rectangle #'delimit-columns-rectangle-line
352 start the-end nil)) 346 start the-end nil))
353 ;; nullify markers 347 ;; nullify markers
354 (set-marker delimit-columns-limit nil) 348 (set-marker delimit-columns-limit nil)
@@ -359,7 +353,7 @@ START and END delimits the corners of text rectangle."
359;; Internal Variables and Functions: 353;; Internal Variables and Functions:
360 354
361 355
362(defun delimit-columns-rectangle-max (startpos &optional _ignore1 _ignore2) 356(defun delimit-columns-rectangle-max (startpos &optional _begextra _endextra)
363 (set-marker delimit-columns-limit (point)) 357 (set-marker delimit-columns-limit (point))
364 (goto-char startpos) 358 (goto-char startpos)
365 (let ((ncol 1) 359 (let ((ncol 1)
@@ -392,7 +386,7 @@ START and END delimits the corners of text rectangle."
392 (setq values (cdr values))))) 386 (setq values (cdr values)))))
393 387
394 388
395(defun delimit-columns-rectangle-line (startpos &optional _ignore1 _ignore2) 389(defun delimit-columns-rectangle-line (startpos &optional _begextra _endextra)
396 (let ((len (length delimit-columns-max)) 390 (let ((len (length delimit-columns-max))
397 (ncol 0) 391 (ncol 0)
398 origin) 392 origin)
@@ -442,8 +436,7 @@ START and END delimits the corners of text rectangle."
442 ((eq delimit-columns-format 'padding) 436 ((eq delimit-columns-format 'padding)
443 (insert spaces delimit-columns-after delimit-columns-str-after)) 437 (insert spaces delimit-columns-after delimit-columns-str-after))
444 (t 438 (t
445 (insert delimit-columns-after spaces delimit-columns-str-after)) 439 (insert delimit-columns-after spaces delimit-columns-str-after))))
446 ))
447 (goto-char (max (point) delimit-columns-limit)))) 440 (goto-char (max (point) delimit-columns-limit))))
448 441
449 442
@@ -466,8 +459,7 @@ START and END delimits the corners of text rectangle."
466 (insert delimit-columns-after 459 (insert delimit-columns-after
467 delimit-columns-str-separator 460 delimit-columns-str-separator
468 spaces 461 spaces
469 delimit-columns-before)) 462 delimit-columns-before))))
470 ))
471 463
472 464
473;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 465;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
diff --git a/test/lisp/delim-col-tests.el b/test/lisp/delim-col-tests.el
new file mode 100644
index 00000000000..f2a0377b07b
--- /dev/null
+++ b/test/lisp/delim-col-tests.el
@@ -0,0 +1,181 @@
1;;; delim-col-tests.el --- Tests for delim-col.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Stefan Kangas <stefankangas@gmail.com>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;;; Code:
25
26(require 'ert)
27(require 'delim-col)
28
29(ert-deftest delim-col-tests-delimit-columns ()
30 (with-temp-buffer
31 (insert "a b c\n")
32 (delimit-columns-region (point-min) (point-max))
33 (should (equal (buffer-string) "a, b, c\n")))
34 (with-temp-buffer
35 (insert "a b c d\n"
36 "aaaa bb ccc ddddd\n"
37 "aaa bbb cccc dddd\n"
38 "aa bb ccccccc ddd\n")
39 (delimit-columns-region (point-min) (point-max))
40 (should (equal (buffer-string)
41 (concat "a, b, c, d \n"
42 "aaaa, bb, ccc, ddddd\n"
43 "aaa, bbb, cccc, dddd \n"
44 "aa, bb, ccccccc, ddd \n")))))
45
46(ert-deftest delim-col-tests-delimit-rectangle ()
47 (with-temp-buffer
48 (insert "a b c d\n"
49 "aaaa bb ccc ddddd\n"
50 "aaa bbb cccc dddd\n"
51 "aa bb ccccccc ddd\n")
52 (delimit-columns-rectangle 3 58) ; from first b to last c
53 (should (equal (buffer-string)
54 (concat "a b, c d\n"
55 "aaaa bb, ccc ddddd\n"
56 "aaa bbb, cccc dddd\n"
57 "aa bb, ccccccc ddd\n")))))
58
59(ert-deftest delim-col-tests-delimit-columns-str-separator ()
60 (let ((delimit-columns-str-separator ":"))
61 (with-temp-buffer
62 (insert "a b\n")
63 (delimit-columns-region (point-min) (point-max))
64 (should (equal (buffer-string) "a:b\n")))
65 (with-temp-buffer
66 (insert "a b c d\n"
67 "aa bb cc dd\n")
68 (delimit-columns-rectangle 3 16) ; from first b to last c
69 (should (equal (buffer-string)
70 (concat "a b: c d\n"
71 "aa bb:cc dd\n"))))))
72
73(ert-deftest delim-col-tests-delimit-columns-str-before-after ()
74 (let ((delimit-columns-str-before "[ ")
75 (delimit-columns-str-after " ]"))
76 (with-temp-buffer
77 (insert "a b c\n")
78 (delimit-columns-region (point-min) (point-max))
79 (should (equal (buffer-string) "[ a, b, c ]\n")))
80 (with-temp-buffer
81 (insert "a b c d\n"
82 "aaaa bb ccc ddddd\n"
83 "aaa bbb cccc dddd\n"
84 "aa bb ccccccc ddd\n")
85 (delimit-columns-region (point-min) (point-max))
86 (should (equal (buffer-string)
87 (concat "[ a, b, c, d ]\n"
88 "[ aaaa, bb, ccc, ddddd ]\n"
89 "[ aaa, bbb, cccc, dddd ]\n"
90 "[ aa, bb, ccccccc, ddd ]\n"))))
91 (with-temp-buffer
92 (insert "a b c d\n"
93 "aaaa bb ccc ddddd\n"
94 "aaa bbb cccc dddd\n"
95 "aa bb ccccccc ddd\n")
96 (delimit-columns-rectangle 3 58) ; from first b to last c
97 (should (equal (buffer-string)
98 (concat "a [ b, c ] d\n"
99 "aaaa [ bb, ccc ] ddddd\n"
100 "aaa [ bbb, cccc ] dddd\n"
101 "aa [ bb, ccccccc ] ddd\n"))))))
102
103(ert-deftest delim-col-tests-delimit-colummns-before-after ()
104 (let ((delimit-columns-before "<")
105 (delimit-columns-after ">"))
106 (with-temp-buffer
107 (insert "a b\n")
108 (delimit-columns-region (point-min) (point-max))
109 (should (equal (buffer-string) "<a>, <b>\n")))
110 (with-temp-buffer
111 (insert "a b c d\n"
112 "aa bb cc dd\n")
113 (delimit-columns-rectangle 3 17)
114 (should (equal (buffer-string)
115 (concat "a <b>, <c> d\n"
116 "aa <bb>, <cc> dd\n"))))))
117
118(ert-deftest delim-col-tests-delimit-columns-separator ()
119 (let ((delimit-columns-separator ","))
120 (with-temp-buffer
121 (insert "a,b,c\n")
122 (delimit-columns-region (point-min) (point-max))
123 (should (equal (buffer-string) "a, b, c\n")))))
124
125(ert-deftest delim-col-tests-delimit-columns-format/nil ()
126 (let ((delimit-columns-format nil))
127 (with-temp-buffer
128 (insert "a b\n"
129 "aa bb\n")
130 (delimit-columns-region (point-min) (point-max))
131 (should (equal (buffer-string)
132 (concat "a, b\n"
133 "aa, bb\n"))))
134 (with-temp-buffer
135 (insert "a b c d\n"
136 "aa bb cc dd\n")
137 (delimit-columns-rectangle 3 17) ; from first b to last c
138 (should (equal (buffer-string)
139 (concat "a b, c d\n"
140 "aa bb, cc dd\n"))))))
141
142(ert-deftest delim-col-tests-delimit-columns-format/separator ()
143 (let ((delimit-columns-format 'separator)
144 (delimit-columns-before "<")
145 (delimit-columns-after ">"))
146 (with-temp-buffer
147 (insert "a b\n"
148 "aa bb\n")
149 (delimit-columns-region (point-min) (point-max))
150 (should (equal (buffer-string)
151 (concat "<a> , <b> \n"
152 "<aa>, <bb>\n"))))
153 (with-temp-buffer
154 (insert "a b c d\n"
155 "aa bb cc dd\n")
156 (delimit-columns-rectangle 3 17) ; from first b to last c
157 (should (equal (buffer-string)
158 (concat "a <b> , <c> d\n"
159 "aa <bb>, <cc> dd\n"))))))
160
161(ert-deftest delim-col-tests-delimit-columns-format/padding ()
162 (let ((delimit-columns-format 'padding)
163 (delimit-columns-before "<")
164 (delimit-columns-after ">"))
165 (with-temp-buffer
166 (insert "a b\n"
167 "aa bb\n")
168 (delimit-columns-region (point-min) (point-max))
169 (should (equal (buffer-string)
170 (concat "<a >, <b >\n"
171 "<aa>, <bb>\n"))))
172 (with-temp-buffer
173 (insert "a b c d\n"
174 "aa bb cc dd\n")
175 (delimit-columns-rectangle 3 17) ; from first b to last c
176 (should (equal (buffer-string)
177 (concat "a <b >, <c > d\n"
178 "aa <bb>, <cc> dd\n"))))))
179
180(provide 'delim-col-tests)
181;;; delim-col-tests.el ends here