delim-col.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. ;;; delim-col.el --- prettify all columns in a region or rectangle
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  4. ;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br>
  5. ;; Version: 2.1
  6. ;; Keywords: internal
  7. ;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre
  8. ;; This file is part of GNU Emacs.
  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. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; delim-col helps to prettify columns in a text region or rectangle.
  21. ;;
  22. ;; To use it, make sure that this file is in load-path and insert in your
  23. ;; .emacs:
  24. ;;
  25. ;; (require 'delim-col)
  26. ;;
  27. ;; If you have, for example, the following columns:
  28. ;;
  29. ;; a b c d
  30. ;; aaaa bb ccc ddddd
  31. ;; aaa bbb cccc dddd
  32. ;; aa bb ccccccc ddd
  33. ;;
  34. ;; And the following settings:
  35. ;;
  36. ;; (setq delimit-columns-str-before "[ ")
  37. ;; (setq delimit-columns-str-after " ]")
  38. ;; (setq delimit-columns-str-separator ", ")
  39. ;; (setq delimit-columns-before "")
  40. ;; (setq delimit-columns-after "")
  41. ;; (setq delimit-columns-separator "\t")
  42. ;; (setq delimit-columns-format 'separator)
  43. ;; (setq delimit-columns-extra t)
  44. ;;
  45. ;; If you select the lines above and type:
  46. ;;
  47. ;; M-x delimit-columns-region RET
  48. ;;
  49. ;; You obtain the following result:
  50. ;;
  51. ;; [ a , b , c , d ]
  52. ;; [ aaaa, bb , ccc , ddddd ]
  53. ;; [ aaa , bbb, cccc , dddd ]
  54. ;; [ aa , bb , ccccccc, ddd ]
  55. ;;
  56. ;; But if you select start from the very first b to the very last c and type:
  57. ;;
  58. ;; M-x delimit-columns-rectangle RET
  59. ;;
  60. ;; You obtain the following result:
  61. ;;
  62. ;; a [ b , c ] d
  63. ;; aaaa [ bb , ccc ] ddddd
  64. ;; aaa [ bbb, cccc ] dddd
  65. ;; aa [ bb , ccccccc ] ddd
  66. ;;
  67. ;; Now, if we change settings to:
  68. ;;
  69. ;; (setq delimit-columns-before "<")
  70. ;; (setq delimit-columns-after ">")
  71. ;;
  72. ;; For the `delimit-columns-region' example above, the result is:
  73. ;;
  74. ;; [ <a> , <b> , <c> , <d> ]
  75. ;; [ <aaaa>, <bb> , <ccc> , <ddddd> ]
  76. ;; [ <aaa> , <bbb>, <cccc> , <dddd> ]
  77. ;; [ <aa> , <bb> , <ccccccc>, <ddd> ]
  78. ;;
  79. ;; And for the `delimit-columns-rectangle' example above, the result is:
  80. ;;
  81. ;; a [ <b> , <c> ] d
  82. ;; aaaa [ <bb> , <ccc> ] ddddd
  83. ;; aaa [ <bbb>, <cccc> ] dddd
  84. ;; aa [ <bb> , <ccccccc> ] ddd
  85. ;;
  86. ;; Note that `delimit-columns-region' operates over all text region
  87. ;; selected, extending the region start to the beginning of line and the
  88. ;; region end to the end of line. While `delimit-columns-rectangle'
  89. ;; operates over the text rectangle selected which rectangle diagonal is
  90. ;; given by the region start and end.
  91. ;;
  92. ;; See `delimit-columns-format' variable documentation for column formatting.
  93. ;;
  94. ;; `delimit-columns-region' is useful when you have columns of text that
  95. ;; are not well aligned, like:
  96. ;;
  97. ;; horse apple bus
  98. ;; dog pineapple car
  99. ;; porcupine strawberry airplane
  100. ;;
  101. ;; `delimit-columns-region' and `delimit-columns-rectangle' handle lines
  102. ;; with different number of columns, like:
  103. ;;
  104. ;; horse apple bus
  105. ;; dog pineapple car EXTRA
  106. ;; porcupine strawberry airplane
  107. ;;
  108. ;; Use `delimit-columns-customize' to customize delim-col package variables.
  109. ;;; Code:
  110. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  111. ;; User Options:
  112. (defgroup columns nil
  113. "Prettify columns."
  114. :link '(emacs-library-link :tag "Source Lisp File" "delim-col.el")
  115. :prefix "delimit-columns-"
  116. :group 'wp)
  117. (defcustom delimit-columns-str-before ""
  118. "Specify a string to be inserted before all columns."
  119. :type '(string :tag "Before All Columns")
  120. :group 'columns)
  121. (defcustom delimit-columns-str-separator ", "
  122. "Specify a string to be inserted between each column."
  123. :type '(string :tag "Between Each Column")
  124. :group 'columns)
  125. (defcustom delimit-columns-str-after ""
  126. "Specify a string to be inserted after all columns."
  127. :type '(string :tag "After All Columns")
  128. :group 'columns)
  129. (defcustom delimit-columns-before ""
  130. "Specify a string to be inserted before each column."
  131. :type '(string :tag "Before Each Column")
  132. :group 'columns)
  133. (defcustom delimit-columns-after ""
  134. "Specify a string to be inserted after each column."
  135. :type '(string :tag "After Each Column")
  136. :group 'columns)
  137. (defcustom delimit-columns-separator "\t"
  138. "Specify a regexp which separates each column."
  139. :type '(regexp :tag "Column Separator")
  140. :group 'columns)
  141. (defcustom delimit-columns-format t
  142. "Specify how to format columns.
  143. For examples below, consider:
  144. + columns `ccc' and `dddd',
  145. + the maximum column length for each column is 6,
  146. + and the following settings:
  147. (setq delimit-columns-before \"<\")
  148. (setq delimit-columns-after \">\")
  149. (setq delimit-columns-separator \":\")
  150. Valid values are:
  151. nil no formatting. That is, `delimit-columns-after' is followed by
  152. `delimit-columns-separator'.
  153. For example, the result is: \"<ccc>:<dddd>:\"
  154. t align columns. That is, `delimit-columns-after' is followed by
  155. `delimit-columns-separator' and then followed by spaces.
  156. For example, the result is: \"<ccc>: <dddd>: \"
  157. 'separator align separators. That is, `delimit-columns-after' is followed
  158. by spaces and then followed by `delimit-columns-separator'.
  159. For example, the result is: \"<ccc> :<dddd> :\"
  160. 'padding format column by filling with spaces before
  161. `delimit-columns-after'. That is, spaces are followed by
  162. `delimit-columns-after' and then followed by
  163. `delimit-columns-separator'.
  164. For example, the result is: \"<ccc >:<dddd >:\"
  165. Any other value is treated as t."
  166. :type '(choice :menu-tag "Column Formatting"
  167. :tag "Column Formatting"
  168. (const :tag "No Formatting" nil)
  169. (const :tag "Column Alignment" t)
  170. (const :tag "Separator Alignment" separator)
  171. (const :tag "Column Padding" padding))
  172. :group 'columns)
  173. (defcustom delimit-columns-extra t
  174. "Non-nil means that lines will have the same number of columns.
  175. This has effect only when there are lines with different number of columns."
  176. :type '(boolean :tag "Lines With Same Number Of Column")
  177. :group 'columns)
  178. (defcustom delimit-columns-start 0
  179. "Specify column number to start prettifying.
  180. See also `delimit-columns-end' for documentation.
  181. The following relation must hold:
  182. 0 <= delimit-columns-start <= delimit-columns-end
  183. The column number start from 0 and it's relative to the beginning of selected
  184. region. So if you selected a text region, the first column (column 0) is
  185. located at beginning of line. If you selected a text rectangle, the first
  186. column (column 0) is located at left corner."
  187. :type '(integer :tag "Column Start")
  188. :group 'columns)
  189. (defcustom delimit-columns-end 1000000
  190. "Specify column number to end prettifying.
  191. See also `delimit-columns-start' for documentation.
  192. The following relation must hold:
  193. 0 <= delimit-columns-start <= delimit-columns-end
  194. The column number start from 0 and it's relative to the beginning of selected
  195. region. So if you selected a text region, the first column (column 0) is
  196. located at beginning of line. If you selected a text rectangle, the first
  197. column (column 0) is located at left corner."
  198. :type '(integer :tag "Column End")
  199. :group 'columns)
  200. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  201. ;; User Commands:
  202. ;; to avoid compilation gripes
  203. (defvar delimit-columns-max nil)
  204. (defvar delimit-columns-limit nil)
  205. ;;;###autoload
  206. (defun delimit-columns-customize ()
  207. "Customization of `columns' group."
  208. (interactive)
  209. (customize-group 'columns))
  210. (defmacro delimit-columns-str (str)
  211. `(if (stringp ,str) ,str ""))
  212. ;;;###autoload
  213. (defun delimit-columns-region (start end)
  214. "Prettify all columns in a text region.
  215. START and END delimits the text region."
  216. (interactive "*r")
  217. (let ((delimit-columns-str-before
  218. (delimit-columns-str delimit-columns-str-before))
  219. (delimit-columns-str-separator
  220. (delimit-columns-str delimit-columns-str-separator))
  221. (delimit-columns-str-after
  222. (delimit-columns-str delimit-columns-str-after))
  223. (delimit-columns-before
  224. (delimit-columns-str delimit-columns-before))
  225. (delimit-columns-after
  226. (delimit-columns-str delimit-columns-after))
  227. (delimit-columns-start
  228. (if (and (integerp delimit-columns-start)
  229. (>= delimit-columns-start 0))
  230. delimit-columns-start
  231. 0))
  232. (delimit-columns-end
  233. (if (integerp delimit-columns-end)
  234. delimit-columns-end
  235. 1000000))
  236. (delimit-columns-limit (make-marker))
  237. (the-end (copy-marker end))
  238. delimit-columns-max)
  239. (when (<= delimit-columns-start delimit-columns-end)
  240. (save-excursion
  241. (goto-char start)
  242. (beginning-of-line)
  243. ;; get maximum length for each column
  244. (and delimit-columns-format
  245. (save-excursion
  246. (while (< (point) the-end)
  247. (delimit-columns-rectangle-max
  248. (prog1
  249. (point)
  250. (end-of-line)))
  251. (forward-char 1))))
  252. ;; prettify columns
  253. (while (< (point) the-end)
  254. (delimit-columns-rectangle-line
  255. (prog1
  256. (point)
  257. (end-of-line)))
  258. (forward-char 1))
  259. ;; nullify markers
  260. (set-marker delimit-columns-limit nil)
  261. (set-marker the-end nil)))))
  262. (require 'rect)
  263. ;;;###autoload
  264. (defun delimit-columns-rectangle (start end)
  265. "Prettify all columns in a text rectangle.
  266. START and END delimits the corners of text rectangle."
  267. (interactive "*r")
  268. (let ((delimit-columns-str-before
  269. (delimit-columns-str delimit-columns-str-before))
  270. (delimit-columns-str-separator
  271. (delimit-columns-str delimit-columns-str-separator))
  272. (delimit-columns-str-after
  273. (delimit-columns-str delimit-columns-str-after))
  274. (delimit-columns-before
  275. (delimit-columns-str delimit-columns-before))
  276. (delimit-columns-after
  277. (delimit-columns-str delimit-columns-after))
  278. (delimit-columns-start
  279. (if (and (integerp delimit-columns-start)
  280. (>= delimit-columns-start 0))
  281. delimit-columns-start
  282. 0))
  283. (delimit-columns-end
  284. (if (integerp delimit-columns-end)
  285. delimit-columns-end
  286. 1000000))
  287. (delimit-columns-limit (make-marker))
  288. (the-end (copy-marker end))
  289. delimit-columns-max)
  290. (when (<= delimit-columns-start delimit-columns-end)
  291. ;; get maximum length for each column
  292. (and delimit-columns-format
  293. (save-excursion
  294. (operate-on-rectangle 'delimit-columns-rectangle-max
  295. start the-end nil)))
  296. ;; prettify columns
  297. (save-excursion
  298. (operate-on-rectangle 'delimit-columns-rectangle-line
  299. start the-end nil))
  300. ;; nullify markers
  301. (set-marker delimit-columns-limit nil)
  302. (set-marker the-end nil))))
  303. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  304. ;; Internal Variables and Functions:
  305. (defun delimit-columns-rectangle-max (startpos &optional _ignore1 _ignore2)
  306. (set-marker delimit-columns-limit (point))
  307. (goto-char startpos)
  308. (let ((ncol 1)
  309. origin values)
  310. ;; get current column length
  311. (while (progn
  312. (setq origin (current-column))
  313. (re-search-forward delimit-columns-separator
  314. delimit-columns-limit 'move))
  315. (save-excursion
  316. (goto-char (match-beginning 0))
  317. (setq values (cons (- (current-column) origin)
  318. values)))
  319. (setq ncol (1+ ncol)))
  320. (setq values (cons (- (current-column) origin)
  321. values))
  322. ;; extend delimit-columns-max, if needed
  323. (let ((index (length delimit-columns-max)))
  324. (and (> ncol index)
  325. (let ((extend (make-vector ncol 0)))
  326. (while (> index 0)
  327. (setq index (1- index))
  328. (aset extend index (aref delimit-columns-max index)))
  329. (setq delimit-columns-max extend))))
  330. ;; get maximum column length
  331. (while values
  332. (setq ncol (1- ncol))
  333. (aset delimit-columns-max ncol (max (aref delimit-columns-max ncol)
  334. (car values)))
  335. (setq values (cdr values)))))
  336. (defun delimit-columns-rectangle-line (startpos &optional _ignore1 _ignore2)
  337. (let ((len (length delimit-columns-max))
  338. (ncol 0)
  339. origin)
  340. (set-marker delimit-columns-limit (point))
  341. (goto-char startpos)
  342. ;; skip initial columns
  343. (while (and (< ncol delimit-columns-start)
  344. (< (point) delimit-columns-limit)
  345. (re-search-forward delimit-columns-separator
  346. delimit-columns-limit 'move))
  347. (setq ncol (1+ ncol)))
  348. ;; insert first formatting
  349. (insert delimit-columns-str-before delimit-columns-before)
  350. ;; Adjust all columns but last one
  351. (while (progn
  352. (setq origin (current-column))
  353. (and (< (point) delimit-columns-limit)
  354. (re-search-forward delimit-columns-separator
  355. delimit-columns-limit 'move)
  356. (or (< ncol delimit-columns-end)
  357. (progn
  358. (goto-char (match-beginning 0))
  359. nil))))
  360. (delete-region (match-beginning 0) (point))
  361. (delimit-columns-format
  362. (and delimit-columns-format
  363. (make-string (- (aref delimit-columns-max ncol)
  364. (- (current-column) origin))
  365. ?\s)))
  366. (setq ncol (1+ ncol)))
  367. ;; Prepare last column spaces
  368. (let ((spaces (and delimit-columns-format
  369. (make-string (- (aref delimit-columns-max ncol)
  370. (- (current-column) origin))
  371. ?\s))))
  372. ;; Adjust extra columns, if needed
  373. (and delimit-columns-extra
  374. (while (and (< (setq ncol (1+ ncol)) len)
  375. (<= ncol delimit-columns-end))
  376. (delimit-columns-format spaces)
  377. (setq spaces (and delimit-columns-format
  378. (make-string (aref delimit-columns-max ncol)
  379. ?\s)))))
  380. ;; insert last formatting
  381. (cond ((null delimit-columns-format)
  382. (insert delimit-columns-after delimit-columns-str-after))
  383. ((eq delimit-columns-format 'padding)
  384. (insert spaces delimit-columns-after delimit-columns-str-after))
  385. (t
  386. (insert delimit-columns-after spaces delimit-columns-str-after))
  387. ))
  388. (goto-char (max (point) delimit-columns-limit))))
  389. (defun delimit-columns-format (spaces)
  390. (cond ((null delimit-columns-format)
  391. (insert delimit-columns-after
  392. delimit-columns-str-separator
  393. delimit-columns-before))
  394. ((eq delimit-columns-format 'separator)
  395. (insert delimit-columns-after
  396. spaces
  397. delimit-columns-str-separator
  398. delimit-columns-before))
  399. ((eq delimit-columns-format 'padding)
  400. (insert spaces
  401. delimit-columns-after
  402. delimit-columns-str-separator
  403. delimit-columns-before))
  404. (t
  405. (insert delimit-columns-after
  406. delimit-columns-str-separator
  407. spaces
  408. delimit-columns-before))
  409. ))
  410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  411. (provide 'delim-col)
  412. ;;; delim-col.el ends here