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