Regenerate autotools stuff, grumble.
[emacs.git] / lisp / delim-col.el
blob009b179fa49df47321e53be0febdaa310be14d4a
1 ;;; delim-col.el --- prettify all columns in a region or rectangle
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
6 ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
7 ;; Version: 2.1
8 ;; Keywords: internal
9 ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; delim-col helps to prettify columns in a text region or rectangle.
30 ;; To use it, make sure that this file is in load-path and insert in your
31 ;; .emacs:
33 ;; (require 'delim-col)
35 ;; If you have, for example, the following columns:
37 ;; a b c d
38 ;; aaaa bb ccc ddddd
39 ;; aaa bbb cccc dddd
40 ;; aa bb ccccccc ddd
42 ;; And the following settings:
44 ;; (setq delimit-columns-str-before "[ ")
45 ;; (setq delimit-columns-str-after " ]")
46 ;; (setq delimit-columns-str-separator ", ")
47 ;; (setq delimit-columns-before "")
48 ;; (setq delimit-columns-after "")
49 ;; (setq delimit-columns-separator "\t")
50 ;; (setq delimit-columns-format 'separator)
51 ;; (setq delimit-columns-extra t)
53 ;; If you select the lines above and type:
55 ;; M-x delimit-columns-region RET
57 ;; You obtain the following result:
59 ;; [ a , b , c , d ]
60 ;; [ aaaa, bb , ccc , ddddd ]
61 ;; [ aaa , bbb, cccc , dddd ]
62 ;; [ aa , bb , ccccccc, ddd ]
64 ;; But if you select start from the very first b to the very last c and type:
66 ;; M-x delimit-columns-rectangle RET
68 ;; You obtain the following result:
70 ;; a [ b , c ] d
71 ;; aaaa [ bb , ccc ] ddddd
72 ;; aaa [ bbb, cccc ] dddd
73 ;; aa [ bb , ccccccc ] ddd
75 ;; Now, if we change settings to:
77 ;; (setq delimit-columns-before "<")
78 ;; (setq delimit-columns-after ">")
80 ;; For the `delimit-columns-region' example above, the result is:
82 ;; [ <a> , <b> , <c> , <d> ]
83 ;; [ <aaaa>, <bb> , <ccc> , <ddddd> ]
84 ;; [ <aaa> , <bbb>, <cccc> , <dddd> ]
85 ;; [ <aa> , <bb> , <ccccccc>, <ddd> ]
87 ;; And for the `delimit-columns-rectangle' example above, the result is:
89 ;; a [ <b> , <c> ] d
90 ;; aaaa [ <bb> , <ccc> ] ddddd
91 ;; aaa [ <bbb>, <cccc> ] dddd
92 ;; aa [ <bb> , <ccccccc> ] ddd
94 ;; Note that `delimit-columns-region' operates over all text region
95 ;; selected, extending the region start to the beginning of line and the
96 ;; region end to the end of line. While `delimit-columns-rectangle'
97 ;; operates over the text rectangle selected which rectangle diagonal is
98 ;; given by the region start and end.
100 ;; See `delimit-columns-format' variable documentation for column formating.
102 ;; `delimit-columns-region' is useful when you have columns of text that
103 ;; are not well aligned, like:
105 ;; horse apple bus
106 ;; dog pineapple car
107 ;; porcupine strawberry airplane
109 ;; `delimit-columns-region' and `delimit-columns-rectangle' handle lines
110 ;; with different number of columns, like:
112 ;; horse apple bus
113 ;; dog pineapple car EXTRA
114 ;; porcupine strawberry airplane
116 ;; Use `delimit-columns-customize' to customize delim-col package variables.
118 ;;; Code:
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;; User Options:
124 (defgroup columns nil
125 "Prettify columns."
126 :link '(emacs-library-link :tag "Source Lisp File" "delim-col.el")
127 :prefix "delimit-columns-"
128 :group 'wp)
130 (defcustom delimit-columns-str-before ""
131 "Specify a string to be inserted before all columns."
132 :type '(string :tag "Before All Columns")
133 :group 'columns)
135 (defcustom delimit-columns-str-separator ", "
136 "Specify a string to be inserted between each column."
137 :type '(string :tag "Between Each Column")
138 :group 'columns)
140 (defcustom delimit-columns-str-after ""
141 "Specify a string to be inserted after all columns."
142 :type '(string :tag "After All Columns")
143 :group 'columns)
145 (defcustom delimit-columns-before ""
146 "Specify a string to be inserted before each column."
147 :type '(string :tag "Before Each Column")
148 :group 'columns)
150 (defcustom delimit-columns-after ""
151 "Specify a string to be inserted after each column."
152 :type '(string :tag "After Each Column")
153 :group 'columns)
155 (defcustom delimit-columns-separator "\t"
156 "Specify a regexp which separates each column."
157 :type '(regexp :tag "Column Separator")
158 :group 'columns)
160 (defcustom delimit-columns-format t
161 "Specify how to format columns.
163 For examples below, consider:
165 + columns `ccc' and `dddd',
166 + the maximum column length for each column is 6,
167 + and the following settings:
168 (setq delimit-columns-before \"<\")
169 (setq delimit-columns-after \">\")
170 (setq delimit-columns-separator \":\")
172 Valid values are:
174 nil no formating. That is, `delimit-columns-after' is followed by
175 `delimit-columns-separator'.
176 For example, the result is: \"<ccc>:<dddd>:\"
178 t align columns. That is, `delimit-columns-after' is followed by
179 `delimit-columns-separator' and then followed by spaces.
180 For example, the result is: \"<ccc>: <dddd>: \"
182 'separator align separators. That is, `delimit-columns-after' is followed
183 by spaces and then followed by `delimit-columns-separator'.
184 For example, the result is: \"<ccc> :<dddd> :\"
186 'padding format column by filling with spaces before
187 `delimit-columns-after'. That is, spaces are followed by
188 `delimit-columns-after' and then followed by
189 `delimit-columns-separator'.
190 For example, the result is: \"<ccc >:<dddd >:\"
192 Any other value is treated as t."
193 :type '(choice :menu-tag "Column Formating"
194 :tag "Column Formating"
195 (const :tag "No Formating" nil)
196 (const :tag "Column Alignment" t)
197 (const :tag "Separator Aligment" separator)
198 (const :tag "Column Padding" padding))
199 :group 'columns)
201 (defcustom delimit-columns-extra t
202 "Non-nil means that lines will have the same number of columns.
204 This has effect only when there are lines with different number of columns."
205 :type '(boolean :tag "Lines With Same Number Of Column")
206 :group 'columns)
208 (defcustom delimit-columns-start 0
209 "Specify column number to start prettifing.
211 See also `delimit-columns-end' for documentation.
213 The following relation must hold:
214 0 <= delimit-columns-start <= delimit-columns-end
216 The column number start from 0 and it's relative to the beginning of selected
217 region. So if you selected a text region, the first column (column 0) is
218 located at beginning of line. If you selected a text rectangle, the first
219 column (column 0) is located at left corner."
220 :type '(integer :tag "Column Start")
221 :group 'columns)
223 (defcustom delimit-columns-end 1000000
224 "Specify column number to end prettifing.
226 See also `delimit-columns-start' for documentation.
228 The following relation must hold:
229 0 <= delimit-columns-start <= delimit-columns-end
231 The column number start from 0 and it's relative to the beginning of selected
232 region. So if you selected a text region, the first column (column 0) is
233 located at beginning of line. If you selected a text rectangle, the first
234 column (column 0) is located at left corner."
235 :type '(integer :tag "Column End")
236 :group 'columns)
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;; User Commands:
243 ;;;###autoload
244 (defun delimit-columns-customize ()
245 "Customization of `columns' group."
246 (interactive)
247 (customize-group 'columns))
250 (defmacro delimit-columns-str (str)
251 `(if (stringp ,str) ,str ""))
254 ;;;###autoload
255 (defun delimit-columns-region (start end)
256 "Prettify all columns in a text region.
258 START and END delimits the text region."
259 (interactive "*r")
260 (let ((delimit-columns-str-before
261 (delimit-columns-str delimit-columns-str-before))
262 (delimit-columns-str-separator
263 (delimit-columns-str delimit-columns-str-separator))
264 (delimit-columns-str-after
265 (delimit-columns-str delimit-columns-str-after))
266 (delimit-columns-before
267 (delimit-columns-str delimit-columns-before))
268 (delimit-columns-after
269 (delimit-columns-str delimit-columns-after))
270 (delimit-columns-start
271 (if (and (integerp delimit-columns-start)
272 (>= delimit-columns-start 0))
273 delimit-columns-start
275 (delimit-columns-end
276 (if (integerp delimit-columns-end)
277 delimit-columns-end
278 1000000))
279 (delimit-columns-limit (make-marker))
280 (the-end (copy-marker end))
281 delimit-columns-max)
282 (when (<= delimit-columns-start delimit-columns-end)
283 (save-excursion
284 (goto-char start)
285 (beginning-of-line)
286 ;; get maximum length for each column
287 (and delimit-columns-format
288 (save-excursion
289 (while (< (point) the-end)
290 (delimit-columns-rectangle-max
291 (prog1
292 (point)
293 (end-of-line)))
294 (forward-char 1))))
295 ;; prettify columns
296 (while (< (point) the-end)
297 (delimit-columns-rectangle-line
298 (prog1
299 (point)
300 (end-of-line)))
301 (forward-char 1))
302 ;; nullify markers
303 (set-marker delimit-columns-limit nil)
304 (set-marker the-end nil)))))
307 (require 'rect)
310 ;;;###autoload
311 (defun delimit-columns-rectangle (start end)
312 "Prettify all columns in a text rectangle.
314 START and END delimits the corners of text rectangle."
315 (interactive "*r")
316 (let ((delimit-columns-str-before
317 (delimit-columns-str delimit-columns-str-before))
318 (delimit-columns-str-separator
319 (delimit-columns-str delimit-columns-str-separator))
320 (delimit-columns-str-after
321 (delimit-columns-str delimit-columns-str-after))
322 (delimit-columns-before
323 (delimit-columns-str delimit-columns-before))
324 (delimit-columns-after
325 (delimit-columns-str delimit-columns-after))
326 (delimit-columns-start
327 (if (and (integerp delimit-columns-start)
328 (>= delimit-columns-start 0))
329 delimit-columns-start
331 (delimit-columns-end
332 (if (integerp delimit-columns-end)
333 delimit-columns-end
334 1000000))
335 (delimit-columns-limit (make-marker))
336 (the-end (copy-marker end))
337 delimit-columns-max)
338 (when (<= delimit-columns-start delimit-columns-end)
339 ;; get maximum length for each column
340 (and delimit-columns-format
341 (save-excursion
342 (operate-on-rectangle 'delimit-columns-rectangle-max
343 start the-end nil)))
344 ;; prettify columns
345 (save-excursion
346 (operate-on-rectangle 'delimit-columns-rectangle-line
347 start the-end nil))
348 ;; nullify markers
349 (set-marker delimit-columns-limit nil)
350 (set-marker the-end nil))))
353 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
354 ;; Internal Variables and Functions:
357 ;; to avoid compilation gripes
358 (defvar delimit-columns-max nil)
359 (defvar delimit-columns-limit nil)
362 (defun delimit-columns-rectangle-max (startpos &optional ignore1 ignore2)
363 (set-marker delimit-columns-limit (point))
364 (goto-char startpos)
365 (let ((ncol 1)
366 origin values)
367 ;; get current column length
368 (while (progn
369 (setq origin (current-column))
370 (re-search-forward delimit-columns-separator
371 delimit-columns-limit 'move))
372 (save-excursion
373 (goto-char (match-beginning 0))
374 (setq values (cons (- (current-column) origin)
375 values)))
376 (setq ncol (1+ ncol)))
377 (setq values (cons (- (current-column) origin)
378 values))
379 ;; extend delimit-columns-max, if needed
380 (let ((index (length delimit-columns-max)))
381 (and (> ncol index)
382 (let ((extend (make-vector ncol 0)))
383 (while (> index 0)
384 (setq index (1- index))
385 (aset extend index (aref delimit-columns-max index)))
386 (setq delimit-columns-max extend))))
387 ;; get maximum column length
388 (while values
389 (setq ncol (1- ncol))
390 (aset delimit-columns-max ncol (max (aref delimit-columns-max ncol)
391 (car values)))
392 (setq values (cdr values)))))
395 (defun delimit-columns-rectangle-line (startpos &optional ignore1 ignore2)
396 (let ((len (length delimit-columns-max))
397 (ncol 0)
398 origin)
399 (set-marker delimit-columns-limit (point))
400 (goto-char startpos)
401 ;; skip initial columns
402 (while (and (< ncol delimit-columns-start)
403 (< (point) delimit-columns-limit)
404 (re-search-forward delimit-columns-separator
405 delimit-columns-limit 'move))
406 (setq ncol (1+ ncol)))
407 ;; insert first formating
408 (insert delimit-columns-str-before delimit-columns-before)
409 ;; Adjust all columns but last one
410 (while (progn
411 (setq origin (current-column))
412 (and (< (point) delimit-columns-limit)
413 (re-search-forward delimit-columns-separator
414 delimit-columns-limit 'move)
415 (or (< ncol delimit-columns-end)
416 (progn
417 (goto-char (match-beginning 0))
418 nil))))
419 (delete-region (match-beginning 0) (point))
420 (delimit-columns-format
421 (and delimit-columns-format
422 (make-string (- (aref delimit-columns-max ncol)
423 (- (current-column) origin))
424 ?\s)))
425 (setq ncol (1+ ncol)))
426 ;; Prepare last column spaces
427 (let ((spaces (and delimit-columns-format
428 (make-string (- (aref delimit-columns-max ncol)
429 (- (current-column) origin))
430 ?\s))))
431 ;; Adjust extra columns, if needed
432 (and delimit-columns-extra
433 (while (and (< (setq ncol (1+ ncol)) len)
434 (<= ncol delimit-columns-end))
435 (delimit-columns-format spaces)
436 (setq spaces (and delimit-columns-format
437 (make-string (aref delimit-columns-max ncol)
438 ?\s)))))
439 ;; insert last formating
440 (cond ((null delimit-columns-format)
441 (insert delimit-columns-after delimit-columns-str-after))
442 ((eq delimit-columns-format 'padding)
443 (insert spaces delimit-columns-after delimit-columns-str-after))
445 (insert delimit-columns-after spaces delimit-columns-str-after))
447 (goto-char (max (point) delimit-columns-limit))))
450 (defun delimit-columns-format (spaces)
451 (cond ((null delimit-columns-format)
452 (insert delimit-columns-after
453 delimit-columns-str-separator
454 delimit-columns-before))
455 ((eq delimit-columns-format 'separator)
456 (insert delimit-columns-after
457 spaces
458 delimit-columns-str-separator
459 delimit-columns-before))
460 ((eq delimit-columns-format 'padding)
461 (insert spaces
462 delimit-columns-after
463 delimit-columns-str-separator
464 delimit-columns-before))
466 (insert delimit-columns-after
467 delimit-columns-str-separator
468 spaces
469 delimit-columns-before))
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
476 (provide 'delim-col)
479 ;;; delim-col.el ends here