Sync to HEAD
[bpt/emacs.git] / lisp / rect.el
CommitLineData
e8af40ee 1;;; rect.el --- rectangle functions for GNU Emacs
6594deb0 2
35f901fa 3;; Copyright (C) 1985, 1999, 2000, 2001 Free Software Foundation, Inc.
9750e079 4
aa01bed1 5;; Maintainer: Didier Verna <didier@xemacs.org>
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
e037c34c 27;; This package provides the operations on rectangles that are documented
edbd2f74
ER
28;; in the Emacs manual.
29
e417c66f 30;; ### NOTE: this file has been almost completely rewritten by Didier Verna
aa01bed1 31;; <didier@xemacs.org> in July 1999. The purpose of this rewrite is to be less
e417c66f
RS
32;; intrusive and fill lines with whitespaces only when needed. A few functions
33;; are untouched though, as noted above their definition.
34
35
4821e2af 36;;; Code:
a2535589 37
e40261d0 38;;;###autoload
e417c66f 39(defun move-to-column-force (column &optional flag)
92e30637 40 "If COLUMN is within a multi-column character, replace it by spaces and tab.
e417c66f
RS
41As for `move-to-column', passing anything but nil or t in FLAG will move to
42the desired column only if the line is long enough."
b9e81d0a 43 (move-to-column column (or flag t)))
df2801ec
JB
44
45;;;###autoload
92e30637 46(make-obsolete 'move-to-column-force 'move-to-column "21.2")
e40261d0 47
e417c66f 48;; not used any more --dv
3d1b9783
RS
49;; extract-rectangle-line stores lines into this list
50;; to accumulate them for extract-rectangle and delete-extract-rectangle.
51(defvar operate-on-rectangle-lines)
52
74be0ade 53;; ### NOTE: this function is untouched, but not used anymore apart from
e417c66f 54;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
a2535589
JA
55(defun operate-on-rectangle (function start end coerce-tabs)
56 "Call FUNCTION for each line of rectangle with corners at START, END.
57If COERCE-TABS is non-nil, convert multi-column characters
58that span the starting or ending columns on any line
59to multiple spaces before calling FUNCTION.
60FUNCTION is called with three arguments:
61 position of start of segment of this line within the rectangle,
62 number of columns that belong to rectangle but are before that position,
63 number of columns that belong to rectangle but are after point.
64Point is at the end of the segment of this line within the rectangle."
65 (let (startcol startlinepos endcol endlinepos)
66 (save-excursion
67 (goto-char start)
68 (setq startcol (current-column))
69 (beginning-of-line)
70 (setq startlinepos (point)))
71 (save-excursion
72 (goto-char end)
73 (setq endcol (current-column))
74 (forward-line 1)
75 (setq endlinepos (point-marker)))
76 (if (< endcol startcol)
08ce70d1 77 (setq startcol (prog1 endcol (setq endcol startcol))))
bc8ea543
RS
78 (save-excursion
79 (goto-char startlinepos)
80 (while (< (point) endlinepos)
81 (let (startpos begextra endextra)
e40261d0 82 (if coerce-tabs
b9e81d0a 83 (move-to-column startcol t)
e40261d0 84 (move-to-column startcol))
bc8ea543
RS
85 (setq begextra (- (current-column) startcol))
86 (setq startpos (point))
e40261d0 87 (if coerce-tabs
b9e81d0a 88 (move-to-column endcol t)
e40261d0 89 (move-to-column endcol))
daabd795
RS
90 ;; If we overshot, move back one character
91 ;; so that endextra will be positive.
92 (if (and (not coerce-tabs) (> (current-column) endcol))
93 (backward-char 1))
bc8ea543
RS
94 (setq endextra (- endcol (current-column)))
95 (if (< begextra 0)
96 (setq endextra (+ endextra begextra)
97 begextra 0))
98 (funcall function startpos begextra endextra))
99 (forward-line 1)))
a2535589
JA
100 (- endcol startcol)))
101
e417c66f
RS
102;; The replacement for `operate-on-rectangle' -- dv
103(defun apply-on-rectangle (function start end &rest args)
104 "Call FUNCTION for each line of rectangle with corners at START, END.
105FUNCTION is called with two arguments: the start and end columns of the
e037c34c 106rectangle, plus ARGS extra arguments. Point is at the beginning of line when
e417c66f
RS
107the function is called."
108 (let (startcol startpt endcol endpt)
109 (save-excursion
110 (goto-char start)
111 (setq startcol (current-column))
112 (beginning-of-line)
113 (setq startpt (point))
114 (goto-char end)
115 (setq endcol (current-column))
116 (forward-line 1)
117 (setq endpt (point-marker))
118 ;; ensure the start column is the left one.
119 (if (< endcol startcol)
120 (let ((col startcol))
121 (setq startcol endcol endcol col)))
122 ;; start looping over lines
123 (goto-char startpt)
124 (while (< (point) endpt)
125 (apply function startcol endcol args)
126 (forward-line 1)))
127 ))
128
129(defun delete-rectangle-line (startcol endcol fill)
b9e81d0a
SM
130 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
131 (delete-region (point)
132 (progn (move-to-column endcol 'coerce)
133 (point)))))
e417c66f
RS
134
135(defun delete-extract-rectangle-line (startcol endcol lines fill)
136 (let ((pt (point-at-eol)))
b9e81d0a 137 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
e417c66f
RS
138 (setcdr lines (cons (spaces-string (- endcol startcol))
139 (cdr lines)))
140 ;; else
141 (setq pt (point))
b9e81d0a 142 (move-to-column endcol t)
e417c66f
RS
143 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
144 (delete-region pt (point)))
145 ))
146
147;; ### NOTE: this is actually the only function that needs to do complicated
148;; stuff like what's happening in `operate-on-rectangle', because the buffer
149;; might be read-only. --dv
150(defun extract-rectangle-line (startcol endcol lines)
151 (let (start end begextra endextra line)
152 (move-to-column startcol)
153 (setq start (point)
154 begextra (- (current-column) startcol))
155 (move-to-column endcol)
156 (setq end (point)
157 endextra (- endcol (current-column)))
158 (setq line (buffer-substring start (point)))
159 (if (< begextra 0)
160 (setq endextra (+ endextra begextra)
161 begextra 0))
162 (if (< endextra 0)
163 (setq endextra 0))
164 (goto-char start)
a2535589
JA
165 (while (search-forward "\t" end t)
166 (let ((width (- (current-column)
167 (save-excursion (forward-char -1)
168 (current-column)))))
169 (setq line (concat (substring line 0 (- (point) end 1))
170 (spaces-string width)
e417c66f
RS
171 (substring line (+ (length line)
172 (- (point) end)))))))
a2535589
JA
173 (if (or (> begextra 0) (> endextra 0))
174 (setq line (concat (spaces-string begextra)
175 line
176 (spaces-string endextra))))
e417c66f 177 (setcdr lines (cons line (cdr lines)))))
a2535589
JA
178
179(defconst spaces-strings
180 '["" " " " " " " " " " " " " " " " "])
181
e417c66f 182;; this one is untouched --dv
a2535589
JA
183(defun spaces-string (n)
184 (if (<= n 8) (aref spaces-strings n)
185 (let ((val ""))
186 (while (> n 8)
187 (setq val (concat " " val)
188 n (- n 8)))
189 (concat val (aref spaces-strings n)))))
f1180544 190
f9f9507e 191;;;###autoload
e417c66f 192(defun delete-rectangle (start end &optional fill)
e037c34c
DL
193 "Delete (don't save) text in the region-rectangle.
194The same range of columns is deleted in each line starting with the
195line where the region begins and ending with the line where the region
196ends.
197
198When called from a program the rectangle's corners are START and END.
199With a prefix (or a FILL) argument, also fill lines where nothing has
200to be deleted."
201 (interactive "*r\nP")
e417c66f 202 (apply-on-rectangle 'delete-rectangle-line start end fill))
a2535589 203
f9f9507e 204;;;###autoload
e417c66f 205(defun delete-extract-rectangle (start end &optional fill)
7db6139a 206 "Delete the contents of the rectangle with corners at START and END.
e037c34c 207Return it as a list of strings, one for each line of the rectangle.
e417c66f 208
e037c34c 209When called from a program the rectangle's corners are START and END.
e417c66f
RS
210With an optional FILL argument, also fill lines where nothing has to be
211deleted."
212 (let ((lines (list nil)))
213 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
214 (nreverse (cdr lines))))
a2535589 215
f9f9507e 216;;;###autoload
a2535589 217(defun extract-rectangle (start end)
e037c34c
DL
218 "Return the contents of the rectangle with corners at START and END.
219Return it as a list of strings, one for each line of the rectangle."
e417c66f
RS
220 (let ((lines (list nil)))
221 (apply-on-rectangle 'extract-rectangle-line start end lines)
222 (nreverse (cdr lines))))
a2535589
JA
223
224(defvar killed-rectangle nil
e037c34c 225 "Rectangle for `yank-rectangle' to insert.")
a2535589 226
f9f9507e 227;;;###autoload
e417c66f 228(defun kill-rectangle (start end &optional fill)
e037c34c
DL
229 "Delete the region-rectangle and save it as the last killed one.
230
231When called from a program the rectangle's corners are START and END.
232You might prefer to use `delete-extract-rectangle' from a program.
e417c66f
RS
233
234With a prefix (or a FILL) argument, also fill lines where nothing has to be
235deleted."
e037c34c 236 (interactive "*r\nP")
e417c66f
RS
237 (when buffer-read-only
238 (setq killed-rectangle (extract-rectangle start end))
239 (barf-if-buffer-read-only))
240 (setq killed-rectangle (delete-extract-rectangle start end fill)))
241
242;; this one is untouched --dv
f9f9507e 243;;;###autoload
a2535589
JA
244(defun yank-rectangle ()
245 "Yank the last killed rectangle with upper left corner at point."
e037c34c 246 (interactive "*")
a2535589
JA
247 (insert-rectangle killed-rectangle))
248
e417c66f 249;; this one is untoutched --dv
f9f9507e 250;;;###autoload
a2535589
JA
251(defun insert-rectangle (rectangle)
252 "Insert text of RECTANGLE with upper left corner at point.
573f9b32
RS
253RECTANGLE's first line is inserted at point, its second
254line is inserted at a point vertically under point, etc.
23317eac
RS
255RECTANGLE should be a list of strings.
256After this command, the mark is at the upper left corner
257and point is at the lower right corner."
a2535589
JA
258 (let ((lines rectangle)
259 (insertcolumn (current-column))
260 (first t))
23317eac 261 (push-mark)
a2535589
JA
262 (while lines
263 (or first
264 (progn
265 (forward-line 1)
266 (or (bolp) (insert ?\n))
b9e81d0a 267 (move-to-column insertcolumn t)))
a2535589 268 (setq first nil)
afa0467f 269 (insert-for-yank (car lines))
a2535589
JA
270 (setq lines (cdr lines)))))
271
f9f9507e 272;;;###autoload
e417c66f 273(defun open-rectangle (start end &optional fill)
e037c34c
DL
274 "Blank out the region-rectangle, shifting text right.
275
276The text previously in the region is not overwritten by the blanks,
277but instead winds up to the right of the rectangle.
e417c66f 278
e037c34c 279When called from a program the rectangle's corners are START and END.
e417c66f
RS
280With a prefix (or a FILL) argument, fill with blanks even if there is no text
281on the right side of the rectangle."
e037c34c 282 (interactive "*r\nP")
e417c66f 283 (apply-on-rectangle 'open-rectangle-line start end fill)
08ce70d1 284 (goto-char start))
a2535589 285
e417c66f 286(defun open-rectangle-line (startcol endcol fill)
b9e81d0a 287 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
74be0ade
DL
288 (unless (and (not fill)
289 (= (point) (point-at-eol)))
290 (indent-to endcol))))
e417c66f
RS
291
292(defun delete-whitespace-rectangle-line (startcol endcol fill)
b9e81d0a 293 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
e417c66f 294 (unless (= (point) (point-at-eol))
35f901fa 295 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
a2535589 296
b3a4edce
MR
297;;;###autoload
298(defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
299
ecb079ed 300;;;###autoload
e417c66f 301(defun delete-whitespace-rectangle (start end &optional fill)
ecb079ed
RS
302 "Delete all whitespace following a specified column in each line.
303The left edge of the rectangle specifies the position in each line
304at which whitespace deletion should begin. On each line in the
e417c66f 305rectangle, all continuous whitespace starting at that column is deleted.
ecb079ed 306
e037c34c 307When called from a program the rectangle's corners are START and END.
e417c66f 308With a prefix (or a FILL) argument, also fill too short lines."
e037c34c 309 (interactive "*r\nP")
e417c66f
RS
310 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
311
312;; not used any more --dv
3d1b9783
RS
313;; string-rectangle uses this variable to pass the string
314;; to string-rectangle-line.
315(defvar string-rectangle-string)
b9e81d0a 316(defvar string-rectangle-history nil)
197615f3 317(defun string-rectangle-line (startcol endcol string delete)
b9e81d0a 318 (move-to-column startcol t)
197615f3
DL
319 (if delete
320 (delete-rectangle-line startcol endcol nil))
e417c66f 321 (insert string))
131ca136 322
852eeeaf 323;;;###autoload
35f901fa
GM
324(defun string-rectangle (start end string)
325 "Replace rectangle contents with STRING on each line.
326The length of STRING need not be the same as the rectangle width.
327
328Called from a program, takes three args; START, END and STRING."
b9e81d0a
SM
329 (interactive
330 (progn (barf-if-buffer-read-only)
331 (list
332 (region-beginning)
333 (region-end)
334 (read-string (format "String rectangle (default `%s'): "
335 (or (car string-rectangle-history) ""))
336 nil 'string-rectangle-history
337 (car string-rectangle-history)))))
197615f3 338 (apply-on-rectangle 'string-rectangle-line start end string t))
852eeeaf 339
0c54cd99 340;;;###autoload
35f901fa
GM
341(defalias 'replace-rectangle 'string-rectangle)
342
343;;;###autoload
344(defun string-insert-rectangle (start end string)
345 "Insert STRING on each line of region-rectangle, shifting text right.
346
347When called from a program, the rectangle's corners are START and END.
348The left edge of the rectangle specifies the column for insertion.
349This command does not delete or overwrite any existing text."
b9e81d0a
SM
350 (interactive
351 (progn (barf-if-buffer-read-only)
352 (list
353 (region-beginning)
354 (region-end)
355 (read-string (format "String insert rectangle (default `%s'): "
356 (or (car string-rectangle-history) ""))
357 nil 'string-rectangle-history
358 (car string-rectangle-history)))))
35f901fa
GM
359 (apply-on-rectangle 'string-rectangle-line start end string nil))
360
f9f9507e 361;;;###autoload
e417c66f 362(defun clear-rectangle (start end &optional fill)
e037c34c
DL
363 "Blank out the region-rectangle.
364The text previously in the region is overwritten with blanks.
e417c66f 365
e037c34c 366When called from a program the rectangle's corners are START and END.
e417c66f
RS
367With a prefix (or a FILL) argument, also fill with blanks the parts of the
368rectangle which were empty."
e037c34c 369 (interactive "*r\nP")
e417c66f
RS
370 (apply-on-rectangle 'clear-rectangle-line start end fill))
371
372(defun clear-rectangle-line (startcol endcol fill)
7dfee029 373 (let ((pt (point-at-eol)))
b9e81d0a 374 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
e417c66f
RS
375 (if (and (not fill)
376 (<= (save-excursion (goto-char pt) (current-column)) endcol))
377 (delete-region (point) pt)
378 ;; else
379 (setq pt (point))
b9e81d0a 380 (move-to-column endcol t)
7dfee029 381 (setq endcol (current-column))
e417c66f 382 (delete-region pt (point))
7dfee029 383 (indent-to endcol)))))
a2535589 384
08ce70d1 385(provide 'rect)
6594deb0 386
6b61353c 387;;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
6594deb0 388;;; rect.el ends here