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