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>
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/>.
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.
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
)))
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
)
67 (setq startcol
(current-column))
69 (setq startlinepos
(point)))
72 (setq endcol
(current-column))
74 (setq endlinepos
(point-marker)))
75 (if (< endcol startcol
)
76 (setq startcol
(prog1 endcol
(setq endcol startcol
))))
78 (goto-char startlinepos
)
79 (while (< (point) endlinepos
)
80 (let (startpos begextra endextra
)
82 (move-to-column startcol t
)
83 (move-to-column startcol
))
84 (setq begextra
(- (current-column) startcol
))
85 (setq startpos
(point))
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
))
93 (setq endextra
(- endcol
(current-column)))
95 (setq endextra
(+ endextra begextra
)
97 (funcall function startpos begextra endextra
))
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
)
110 (setq startcol
(current-column))
112 (setq startpt
(point))
114 (setq endcol
(current-column))
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
123 (while (< (point) endpt
)
124 (apply function startcol endcol args
)
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
)
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
))
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
)
152 begextra
(- (current-column) startcol
))
153 (move-to-column endcol
)
155 endextra
(- endcol
(current-column)))
156 (setq line
(buffer-substring start
(point)))
158 (setq endextra
(+ endextra begextra
)
163 (while (search-forward "\t" end t
)
164 (let ((width (- (current-column)
165 (save-excursion (forward-char -
1)
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
)
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
)
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
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
196 (interactive "*r\nP")
197 (apply-on-rectangle 'delete-rectangle-line start end fill
))
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
207 (let ((lines (list nil
)))
208 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill
)
209 (nreverse (cdr lines
))))
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.")
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
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
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
249 (defun yank-rectangle ()
250 "Yank the last killed rectangle with upper left corner at point."
252 (insert-rectangle killed-rectangle
))
254 ;; this one is untoutched --dv
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))
271 (or (bolp) (insert ?
\n))
272 (move-to-column insertcolumn t
)))
274 (insert-for-yank (car lines
))
275 (setq lines
(cdr lines
)))))
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
)
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))))))
303 (defalias 'close-rectangle
'delete-whitespace-rectangle
) ;; Old name
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
)
325 (delete-rectangle-line startcol endcol nil
))
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."
335 (progn (barf-if-buffer-read-only)
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
))
346 (defalias 'replace-rectangle
'string-rectangle
)
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."
356 (progn (barf-if-buffer-read-only)
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
))
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
)
381 (<= (save-excursion (goto-char pt
) (current-column)) endcol
))
382 (delete-region (point) pt
)
385 (move-to-column endcol t
)
386 (setq endcol
(current-column))
387 (delete-region pt
(point))
388 (indent-to endcol
)))))
392 ;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
393 ;;; rect.el ends here