Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; rect.el --- rectangle functions for GNU Emacs. |
2 | ||
d733c5ec | 3 | ;; Copyright (C) 1985, 1994 Free Software Foundation, Inc. |
9750e079 | 4 | |
4821e2af | 5 | ;; Maintainer: FSF |
d7b4d18f | 6 | ;; Keywords: internal |
4821e2af | 7 | |
a2535589 JA |
8 | ;; This file is part of GNU Emacs. |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
13 | ;; any later version. |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
a2535589 | 24 | |
edbd2f74 ER |
25 | ;;; Commentary: |
26 | ||
27 | ;; This package provides the operations on rectangles that are ocumented | |
28 | ;; in the Emacs manual. | |
29 | ||
4821e2af | 30 | ;;; Code: |
a2535589 JA |
31 | |
32 | (defun operate-on-rectangle (function start end coerce-tabs) | |
33 | "Call FUNCTION for each line of rectangle with corners at START, END. | |
34 | If COERCE-TABS is non-nil, convert multi-column characters | |
35 | that span the starting or ending columns on any line | |
36 | to multiple spaces before calling FUNCTION. | |
37 | FUNCTION is called with three arguments: | |
38 | position of start of segment of this line within the rectangle, | |
39 | number of columns that belong to rectangle but are before that position, | |
40 | number of columns that belong to rectangle but are after point. | |
41 | Point is at the end of the segment of this line within the rectangle." | |
42 | (let (startcol startlinepos endcol endlinepos) | |
43 | (save-excursion | |
44 | (goto-char start) | |
45 | (setq startcol (current-column)) | |
46 | (beginning-of-line) | |
47 | (setq startlinepos (point))) | |
48 | (save-excursion | |
49 | (goto-char end) | |
50 | (setq endcol (current-column)) | |
51 | (forward-line 1) | |
52 | (setq endlinepos (point-marker))) | |
53 | (if (< endcol startcol) | |
08ce70d1 | 54 | (setq startcol (prog1 endcol (setq endcol startcol)))) |
bc8ea543 RS |
55 | (save-excursion |
56 | (goto-char startlinepos) | |
57 | (while (< (point) endlinepos) | |
58 | (let (startpos begextra endextra) | |
59 | (move-to-column startcol coerce-tabs) | |
60 | (setq begextra (- (current-column) startcol)) | |
61 | (setq startpos (point)) | |
62 | (move-to-column endcol coerce-tabs) | |
daabd795 RS |
63 | ;; If we overshot, move back one character |
64 | ;; so that endextra will be positive. | |
65 | (if (and (not coerce-tabs) (> (current-column) endcol)) | |
66 | (backward-char 1)) | |
bc8ea543 RS |
67 | (setq endextra (- endcol (current-column))) |
68 | (if (< begextra 0) | |
69 | (setq endextra (+ endextra begextra) | |
70 | begextra 0)) | |
71 | (funcall function startpos begextra endextra)) | |
72 | (forward-line 1))) | |
a2535589 JA |
73 | (- endcol startcol))) |
74 | ||
75 | (defun delete-rectangle-line (startdelpos ignore ignore) | |
76 | (delete-region startdelpos (point))) | |
77 | ||
78 | (defun delete-extract-rectangle-line (startdelpos begextra endextra) | |
79 | (save-excursion | |
80 | (extract-rectangle-line startdelpos begextra endextra)) | |
81 | (delete-region startdelpos (point))) | |
82 | ||
83 | (defun extract-rectangle-line (startdelpos begextra endextra) | |
84 | (let ((line (buffer-substring startdelpos (point))) | |
85 | (end (point))) | |
86 | (goto-char startdelpos) | |
87 | (while (search-forward "\t" end t) | |
88 | (let ((width (- (current-column) | |
89 | (save-excursion (forward-char -1) | |
90 | (current-column))))) | |
91 | (setq line (concat (substring line 0 (- (point) end 1)) | |
92 | (spaces-string width) | |
93 | (substring line (+ (length line) (- (point) end))))))) | |
94 | (if (or (> begextra 0) (> endextra 0)) | |
95 | (setq line (concat (spaces-string begextra) | |
96 | line | |
97 | (spaces-string endextra)))) | |
98 | (setq lines (cons line lines)))) | |
99 | ||
100 | (defconst spaces-strings | |
101 | '["" " " " " " " " " " " " " " " " "]) | |
102 | ||
103 | (defun spaces-string (n) | |
104 | (if (<= n 8) (aref spaces-strings n) | |
105 | (let ((val "")) | |
106 | (while (> n 8) | |
107 | (setq val (concat " " val) | |
108 | n (- n 8))) | |
109 | (concat val (aref spaces-strings n))))) | |
110 | ||
f9f9507e | 111 | ;;;###autoload |
a2535589 JA |
112 | (defun delete-rectangle (start end) |
113 | "Delete (don't save) text in rectangle with point and mark as corners. | |
573f9b32 RS |
114 | The same range of columns is deleted in each line starting with the line |
115 | where the region begins and ending with the line where the region ends." | |
a2535589 JA |
116 | (interactive "r") |
117 | (operate-on-rectangle 'delete-rectangle-line start end t)) | |
118 | ||
f9f9507e | 119 | ;;;###autoload |
a2535589 JA |
120 | (defun delete-extract-rectangle (start end) |
121 | "Delete contents of rectangle and return it as a list of strings. | |
122 | Arguments START and END are the corners of the rectangle. | |
123 | The value is list of strings, one for each line of the rectangle." | |
124 | (let (lines) | |
125 | (operate-on-rectangle 'delete-extract-rectangle-line | |
126 | start end t) | |
127 | (nreverse lines))) | |
128 | ||
f9f9507e | 129 | ;;;###autoload |
a2535589 JA |
130 | (defun extract-rectangle (start end) |
131 | "Return contents of rectangle with corners at START and END. | |
132 | Value is list of strings, one for each line of the rectangle." | |
133 | (let (lines) | |
134 | (operate-on-rectangle 'extract-rectangle-line start end nil) | |
135 | (nreverse lines))) | |
136 | ||
137 | (defvar killed-rectangle nil | |
138 | "Rectangle for yank-rectangle to insert.") | |
139 | ||
f9f9507e | 140 | ;;;###autoload |
a2535589 JA |
141 | (defun kill-rectangle (start end) |
142 | "Delete rectangle with corners at point and mark; save as last killed one. | |
143 | Calling from program, supply two args START and END, buffer positions. | |
573f9b32 | 144 | But in programs you might prefer to use `delete-extract-rectangle'." |
a2535589 | 145 | (interactive "r") |
db03492e RS |
146 | (if buffer-read-only |
147 | (progn | |
148 | (setq killed-rectangle (extract-rectangle start end)) | |
149 | (barf-if-buffer-read-only))) | |
a2535589 JA |
150 | (setq killed-rectangle (delete-extract-rectangle start end))) |
151 | ||
f9f9507e | 152 | ;;;###autoload |
a2535589 JA |
153 | (defun yank-rectangle () |
154 | "Yank the last killed rectangle with upper left corner at point." | |
155 | (interactive) | |
156 | (insert-rectangle killed-rectangle)) | |
157 | ||
f9f9507e | 158 | ;;;###autoload |
a2535589 JA |
159 | (defun insert-rectangle (rectangle) |
160 | "Insert text of RECTANGLE with upper left corner at point. | |
573f9b32 RS |
161 | RECTANGLE's first line is inserted at point, its second |
162 | line is inserted at a point vertically under point, etc. | |
23317eac RS |
163 | RECTANGLE should be a list of strings. |
164 | After this command, the mark is at the upper left corner | |
165 | and point is at the lower right corner." | |
a2535589 JA |
166 | (let ((lines rectangle) |
167 | (insertcolumn (current-column)) | |
168 | (first t)) | |
23317eac | 169 | (push-mark) |
a2535589 JA |
170 | (while lines |
171 | (or first | |
172 | (progn | |
173 | (forward-line 1) | |
174 | (or (bolp) (insert ?\n)) | |
08ce70d1 | 175 | (move-to-column insertcolumn t))) |
a2535589 JA |
176 | (setq first nil) |
177 | (insert (car lines)) | |
178 | (setq lines (cdr lines))))) | |
179 | ||
f9f9507e | 180 | ;;;###autoload |
a2535589 JA |
181 | (defun open-rectangle (start end) |
182 | "Blank out rectangle with corners at point and mark, shifting text right. | |
183 | The text previously in the region is not overwritten by the blanks, | |
08ce70d1 | 184 | but instead winds up to the right of the rectangle." |
a2535589 | 185 | (interactive "r") |
08ce70d1 JB |
186 | (operate-on-rectangle 'open-rectangle-line start end nil) |
187 | (goto-char start)) | |
a2535589 JA |
188 | |
189 | (defun open-rectangle-line (startpos begextra endextra) | |
bc8ea543 RS |
190 | ;; Column where rectangle ends. |
191 | (let ((endcol (+ (current-column) endextra)) | |
192 | whitewidth) | |
a2535589 | 193 | (goto-char startpos) |
bc8ea543 RS |
194 | ;; Column where rectangle begins. |
195 | (let ((begcol (- (current-column) begextra))) | |
a2535589 | 196 | (skip-chars-forward " \t") |
bc8ea543 RS |
197 | ;; Width of whitespace to be deleted and recreated. |
198 | (setq whitewidth (- (current-column) begcol))) | |
199 | ;; Delete the whitespace following the start column. | |
200 | (delete-region startpos (point)) | |
201 | ;; Open the desired width, plus same amount of whitespace we just deleted. | |
202 | (indent-to (+ endcol whitewidth)))) | |
a2535589 | 203 | |
131ca136 | 204 | ;;;###autoload |
ccb629e4 RS |
205 | (defun string-rectangle (start end string) |
206 | "Insert STRING on each line of the region-rectangle, shifting text right. | |
207 | The left edge of the rectangle specifies the column for insertion. | |
208 | This command does not delete or overwrite any existing text. | |
209 | ||
210 | Called from a program, takes three args; START, END and STRING." | |
211 | (interactive "r\nsString rectangle: ") | |
b6c1b08c | 212 | (operate-on-rectangle 'string-rectangle-line start end t)) |
131ca136 | 213 | |
ccb629e4 | 214 | (defun string-rectangle-line (startpos begextra endextra) |
bc8ea543 | 215 | (let (whitespace) |
131ca136 | 216 | (goto-char startpos) |
bc8ea543 | 217 | ;; Compute horizontal width of following whitespace. |
131ca136 ER |
218 | (let ((ocol (current-column))) |
219 | (skip-chars-forward " \t") | |
bc8ea543 RS |
220 | (setq whitespace (- (current-column) ocol))) |
221 | ;; Delete the following whitespace. | |
222 | (delete-region startpos (point)) | |
223 | ;; Insert the desired string. | |
224 | (insert string) | |
225 | ;; Insert the same width of whitespace that we had before. | |
226 | (indent-to (+ (current-column) whitespace)))) | |
131ca136 | 227 | |
f9f9507e | 228 | ;;;###autoload |
a2535589 JA |
229 | (defun clear-rectangle (start end) |
230 | "Blank out rectangle with corners at point and mark. | |
231 | The text previously in the region is overwritten by the blanks. | |
232 | When called from a program, requires two args which specify the corners." | |
233 | (interactive "r") | |
234 | (operate-on-rectangle 'clear-rectangle-line start end t)) | |
235 | ||
236 | (defun clear-rectangle-line (startpos begextra endextra) | |
bc8ea543 | 237 | ;; Find end of whitespace after the rectangle. |
a2535589 JA |
238 | (skip-chars-forward " \t") |
239 | (let ((column (+ (current-column) endextra))) | |
bc8ea543 | 240 | ;; Delete the text in the rectangle, and following whitespace. |
a2535589 JA |
241 | (delete-region (point) |
242 | (progn (goto-char startpos) | |
243 | (skip-chars-backward " \t") | |
244 | (point))) | |
bc8ea543 | 245 | ;; Reindent out to same column that we were at. |
a2535589 JA |
246 | (indent-to column))) |
247 | ||
08ce70d1 | 248 | (provide 'rect) |
6594deb0 ER |
249 | |
250 | ;;; rect.el ends here |