diff options
author | Stefan Monnier <monnier@iro.umontreal.ca> | 2022-09-25 16:15:16 -0400 |
---|---|---|
committer | Stefan Monnier <monnier@iro.umontreal.ca> | 2022-09-25 16:15:16 -0400 |
commit | 650c20f1ca4e07591a727e1cfcc74b3363d15985 (patch) | |
tree | 85d11f6437cde22f410c25e0e5f71a3131ebd07d /lisp/delim-col.el | |
parent | 8869332684c2302b5ba1ead4568bbc7ba1c0183e (diff) | |
parent | 4b85ae6a24380fb67a3315eaec9233f17a872473 (diff) | |
download | emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.tar.gz emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.tar.bz2 emacs-650c20f1ca4e07591a727e1cfcc74b3363d15985.zip |
Merge 'master' into noverlay
Diffstat (limited to 'lisp/delim-col.el')
-rw-r--r-- | lisp/delim-col.el | 185 |
1 files changed, 100 insertions, 85 deletions
diff --git a/lisp/delim-col.el b/lisp/delim-col.el index 120131fe034..e1e166cc480 100644 --- a/lisp/delim-col.el +++ b/lisp/delim-col.el @@ -1,12 +1,11 @@ -;;; delim-col.el --- prettify all columns in a region or rectangle +;;; delim-col.el --- prettify all columns in a region or rectangle -*- lexical-binding: t; -*- -;; Copyright (C) 1999-2017 Free Software Foundation, Inc. +;; Copyright (C) 1999-2022 Free Software Foundation, Inc. -;; Author: Vinicius Jose Latorre <viniciusjl@ig.com.br> -;; Maintainer: Vinicius Jose Latorre <viniciusjl@ig.com.br> -;; Version: 2.1 -;; Keywords: internal -;; X-URL: http://www.emacswiki.org/cgi-bin/wiki/ViniciusJoseLatorre +;; Author: Vinicius Jose Latorre <viniciusjl.gnu@gmail.com> +;; Old-Version: 2.1 +;; Keywords: convenience text +;; URL: https://www.emacswiki.org/emacs/ViniciusJoseLatorre ;; This file is part of GNU Emacs. @@ -27,11 +26,6 @@ ;; delim-col helps to prettify columns in a text region or rectangle. ;; -;; To use it, make sure that this file is in load-path and insert in your -;; .emacs: -;; -;; (require 'delim-col) -;; ;; If you have, for example, the following columns: ;; ;; a b c d @@ -91,9 +85,9 @@ ;; aaa [ <bbb>, <cccc> ] dddd ;; aa [ <bb> , <ccccccc> ] ddd ;; -;; Note that `delimit-columns-region' operates over all text region -;; selected, extending the region start to the beginning of line and the -;; region end to the end of line. While `delimit-columns-rectangle' +;; Note that `delimit-columns-region' operates over the entire selected +;; text region, extending the region start to the beginning of line and +;; the region end to the end of line. While `delimit-columns-rectangle' ;; operates over the text rectangle selected which rectangle diagonal is ;; given by the region start and end. ;; @@ -117,6 +111,7 @@ ;;; Code: +(require 'rect) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; User Options: @@ -125,6 +120,7 @@ "Prettify columns." :link '(emacs-library-link :tag "Source Lisp File" "delim-col.el") :prefix "delimit-columns-" + :group 'convenience :group 'text) (defcustom delimit-columns-str-before "" @@ -213,10 +209,11 @@ See also `delimit-columns-end' for documentation. The following relation must hold: 0 <= delimit-columns-start <= delimit-columns-end -The column number start from 0 and it's relative to the beginning of selected -region. So if you selected a text region, the first column (column 0) is -located at beginning of line. If you selected a text rectangle, the first -column (column 0) is located at left corner." +The column number starts at 0 and is relative to the beginning of +the selected region. So if you select a text region, the first +column (column 0) is located at the beginning of line. If you +select a text rectangle, the first column (column 0) is located +at the left corner." :type '(integer :tag "Column Start") :group 'columns) @@ -228,10 +225,11 @@ See also `delimit-columns-start' for documentation. The following relation must hold: 0 <= delimit-columns-start <= delimit-columns-end -The column number start from 0 and it's relative to the beginning of selected -region. So if you selected a text region, the first column (column 0) is -located at beginning of line. If you selected a text rectangle, the first -column (column 0) is located at left corner." +The column number starts at 0 and is relative to the beginning of +the selected region. So if you select a text region, the first +column (column 0) is located at the beginning of line. If you +select a text rectangle, the first column (column 0) is located +at the left corner." :type '(integer :tag "Column End") :group 'columns) @@ -247,76 +245,96 @@ column (column 0) is located at left corner." ;;;###autoload (defun delimit-columns-customize () - "Customization of `columns' group." + "Customize the `columns' group." (interactive) (customize-group 'columns)) -(defmacro delimit-columns-str (str) - `(if (stringp ,str) ,str "")) +(defun delimit-columns-str (str) + (if (stringp str) str "")) ;;;###autoload (defun delimit-columns-region (start end) "Prettify all columns in a text region. -START and END delimits the text region." - (interactive "*r") - (let ((delimit-columns-str-before - (delimit-columns-str delimit-columns-str-before)) - (delimit-columns-str-separator - (delimit-columns-str delimit-columns-str-separator)) - (delimit-columns-str-after - (delimit-columns-str delimit-columns-str-after)) - (delimit-columns-before - (delimit-columns-str delimit-columns-before)) - (delimit-columns-after - (delimit-columns-str delimit-columns-after)) - (delimit-columns-start - (if (and (integerp delimit-columns-start) - (>= delimit-columns-start 0)) - delimit-columns-start - 0)) - (delimit-columns-end - (if (integerp delimit-columns-end) - delimit-columns-end - 1000000)) - (delimit-columns-limit (make-marker)) - (the-end (copy-marker end)) - delimit-columns-max) - (when (<= delimit-columns-start delimit-columns-end) - (save-excursion - (goto-char start) - (beginning-of-line) - ;; get maximum length for each column - (and delimit-columns-format - (save-excursion - (while (< (point) the-end) - (delimit-columns-rectangle-max - (prog1 - (point) - (end-of-line))) - (forward-char 1)))) - ;; prettify columns - (while (< (point) the-end) - (delimit-columns-rectangle-line - (prog1 - (point) - (end-of-line))) - (forward-char 1)) - ;; nullify markers - (set-marker delimit-columns-limit nil) - (set-marker the-end nil))))) +START and END delimit the text region. +If you have, for example, the following columns: -(require 'rect) + a b c d + aaaa bb ccc ddddd + +Depending on your settings (see below), you then obtain the +following result: + + [ a , b , c , d ] + [ aaaa, bb , ccc , ddddd ] + +See the `delimit-columns-str-before', +`delimit-columns-str-after', `delimit-columns-str-separator', +`delimit-columns-before', `delimit-columns-after', +`delimit-columns-separator', `delimit-columns-format' and +`delimit-columns-extra' variables for customization of the +look." + (interactive "*r") + (if rectangle-mark-mode + ;; Delegate to delimit-columns-rectangle when called with a + ;; rectangular region. + (delimit-columns-rectangle start end) + (let ((delimit-columns-str-before + (delimit-columns-str delimit-columns-str-before)) + (delimit-columns-str-separator + (delimit-columns-str delimit-columns-str-separator)) + (delimit-columns-str-after + (delimit-columns-str delimit-columns-str-after)) + (delimit-columns-before + (delimit-columns-str delimit-columns-before)) + (delimit-columns-after + (delimit-columns-str delimit-columns-after)) + (delimit-columns-start + (if (natnump delimit-columns-start) + delimit-columns-start + 0)) + (delimit-columns-end + (if (integerp delimit-columns-end) + delimit-columns-end + 1000000)) + (delimit-columns-limit (make-marker)) + (the-end (copy-marker end)) + delimit-columns-max) + (when (<= delimit-columns-start delimit-columns-end) + (save-excursion + (goto-char start) + (beginning-of-line) + ;; get maximum length for each column + (and delimit-columns-format + (save-excursion + (while (< (point) the-end) + (delimit-columns-rectangle-max + (prog1 + (point) + (end-of-line))) + (forward-char 1)))) + ;; prettify columns + (while (< (point) the-end) + (delimit-columns-rectangle-line + (prog1 + (point) + (end-of-line))) + (forward-char 1)) + ;; nullify markers + (set-marker delimit-columns-limit nil) + (set-marker the-end nil)))))) ;;;###autoload (defun delimit-columns-rectangle (start end) "Prettify all columns in a text rectangle. -START and END delimits the corners of text rectangle." +See `delimit-columns-region' for what this entails. + +START and END delimit the corners of the text rectangle." (interactive "*r") (let ((delimit-columns-str-before (delimit-columns-str delimit-columns-str-before)) @@ -329,8 +347,7 @@ START and END delimits the corners of text rectangle." (delimit-columns-after (delimit-columns-str delimit-columns-after)) (delimit-columns-start - (if (and (integerp delimit-columns-start) - (>= delimit-columns-start 0)) + (if (natnump delimit-columns-start) delimit-columns-start 0)) (delimit-columns-end @@ -344,11 +361,11 @@ START and END delimits the corners of text rectangle." ;; get maximum length for each column (and delimit-columns-format (save-excursion - (operate-on-rectangle 'delimit-columns-rectangle-max + (operate-on-rectangle #'delimit-columns-rectangle-max start the-end nil))) ;; prettify columns (save-excursion - (operate-on-rectangle 'delimit-columns-rectangle-line + (operate-on-rectangle #'delimit-columns-rectangle-line start the-end nil)) ;; nullify markers (set-marker delimit-columns-limit nil) @@ -359,7 +376,7 @@ START and END delimits the corners of text rectangle." ;; Internal Variables and Functions: -(defun delimit-columns-rectangle-max (startpos &optional _ignore1 _ignore2) +(defun delimit-columns-rectangle-max (startpos &optional _begextra _endextra) (set-marker delimit-columns-limit (point)) (goto-char startpos) (let ((ncol 1) @@ -392,7 +409,7 @@ START and END delimits the corners of text rectangle." (setq values (cdr values))))) -(defun delimit-columns-rectangle-line (startpos &optional _ignore1 _ignore2) +(defun delimit-columns-rectangle-line (startpos &optional _begextra _endextra) (let ((len (length delimit-columns-max)) (ncol 0) origin) @@ -442,8 +459,7 @@ START and END delimits the corners of text rectangle." ((eq delimit-columns-format 'padding) (insert spaces delimit-columns-after delimit-columns-str-after)) (t - (insert delimit-columns-after spaces delimit-columns-str-after)) - )) + (insert delimit-columns-after spaces delimit-columns-str-after)))) (goto-char (max (point) delimit-columns-limit)))) @@ -466,8 +482,7 @@ START and END delimits the corners of text rectangle." (insert delimit-columns-after delimit-columns-str-separator spaces - delimit-columns-before)) - )) + delimit-columns-before)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |