1 ;;; rect.el --- rectangle functions for GNU Emacs
3 ;; Copyright (C) 1985, 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
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.
27 ;; This package provides the operations on rectangles that are documented
28 ;; in the Emacs manual.
30 ;; ### NOTE: this file has been almost completely rewritten by Didier Verna
31 ;; <didier@xemacs.org> in July 1999. The purpose of this rewrite is to be less
32 ;; intrusive and fill lines with whitespaces only when needed. A few functions
33 ;; are untouched though, as noted above their definition.
39 (defun move-to-column-force (column &optional flag
)
40 "Obsolete. Use `move-to-column'.
41 If COLUMN is within a multi-column character, replace it by spaces and tab.
42 As for `move-to-column', passing anything but nil or t in FLAG will move to
43 the desired column only if the line is long enough."
44 (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
(or fill
'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
(or fill
'coerce
)) startcol
)
137 (setcdr lines
(cons (spaces-string (- endcol startcol
))
141 (move-to-column endcol t
)
142 (setcdr lines
(cons (buffer-substring pt
(point)) (cdr lines
)))
143 (delete-region pt
(point)))
146 ;; ### NOTE: this is actually the only function that needs to do complicated
147 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
148 ;; might be read-only. --dv
149 (defun extract-rectangle-line (startcol endcol lines
)
150 (let (start end begextra endextra line
)
151 (move-to-column startcol
)
153 begextra
(- (current-column) startcol
))
154 (move-to-column endcol
)
156 endextra
(- endcol
(current-column)))
157 (setq line
(buffer-substring start
(point)))
159 (setq endextra
(+ endextra begextra
)
164 (while (search-forward "\t" end t
)
165 (let ((width (- (current-column)
166 (save-excursion (forward-char -
1)
168 (setq line
(concat (substring line
0 (- (point) end
1))
169 (spaces-string width
)
170 (substring line
(+ (length line
)
171 (- (point) end
)))))))
172 (if (or (> begextra
0) (> endextra
0))
173 (setq line
(concat (spaces-string begextra
)
175 (spaces-string endextra
))))
176 (setcdr lines
(cons line
(cdr lines
)))))
178 (defconst spaces-strings
179 '["" " " " " " " " " " " " " " " " "])
181 ;; this one is untouched --dv
182 (defun spaces-string (n)
183 (if (<= n
8) (aref spaces-strings n
)
186 (setq val
(concat " " val
)
188 (concat val
(aref spaces-strings n
)))))
191 (defun delete-rectangle (start end
&optional fill
)
192 "Delete (don't save) text in the region-rectangle.
193 The same range of columns is deleted in each line starting with the
194 line where the region begins and ending with the line where the region
197 When called from a program the rectangle's corners are START and END.
198 With a prefix (or a FILL) argument, also fill lines where nothing has
200 (interactive "*r\nP")
201 (apply-on-rectangle 'delete-rectangle-line start end fill
))
204 (defun delete-extract-rectangle (start end
&optional fill
)
205 "Delete the contents of the rectangle with corners at START and END.
206 Return it as a list of strings, one for each line of the rectangle.
208 When called from a program the rectangle's corners are START and END.
209 With an optional FILL argument, also fill lines where nothing has to be
211 (let ((lines (list nil
)))
212 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill
)
213 (nreverse (cdr lines
))))
216 (defun extract-rectangle (start end
)
217 "Return the contents of the rectangle with corners at START and END.
218 Return it as a list of strings, one for each line of the rectangle."
219 (let ((lines (list nil
)))
220 (apply-on-rectangle 'extract-rectangle-line start end lines
)
221 (nreverse (cdr lines
))))
223 (defvar killed-rectangle nil
224 "Rectangle for `yank-rectangle' to insert.")
227 (defun kill-rectangle (start end
&optional fill
)
228 "Delete the region-rectangle and save it as the last killed one.
230 When called from a program the rectangle's corners are START and END.
231 You might prefer to use `delete-extract-rectangle' from a program.
233 With a prefix (or a FILL) argument, also fill lines where nothing has to be
235 (interactive "*r\nP")
236 (when buffer-read-only
237 (setq killed-rectangle
(extract-rectangle start end
))
238 (barf-if-buffer-read-only))
239 (setq killed-rectangle
(delete-extract-rectangle start end fill
)))
241 ;; this one is untouched --dv
243 (defun yank-rectangle ()
244 "Yank the last killed rectangle with upper left corner at point."
246 (insert-rectangle killed-rectangle
))
248 ;; this one is untoutched --dv
250 (defun insert-rectangle (rectangle)
251 "Insert text of RECTANGLE with upper left corner at point.
252 RECTANGLE's first line is inserted at point, its second
253 line is inserted at a point vertically under point, etc.
254 RECTANGLE should be a list of strings.
255 After this command, the mark is at the upper left corner
256 and point is at the lower right corner."
257 (let ((lines rectangle
)
258 (insertcolumn (current-column))
265 (or (bolp) (insert ?
\n))
266 (move-to-column insertcolumn t
)))
269 (setq lines
(cdr lines
)))))
272 (defun open-rectangle (start end
&optional fill
)
273 "Blank out the region-rectangle, shifting text right.
275 The text previously in the region is not overwritten by the blanks,
276 but instead winds up to the right of the rectangle.
278 When called from a program the rectangle's corners are START and END.
279 With a prefix (or a FILL) argument, fill with blanks even if there is no text
280 on the right side of the rectangle."
281 (interactive "*r\nP")
282 (apply-on-rectangle 'open-rectangle-line start end fill
)
285 (defun open-rectangle-line (startcol endcol fill
)
286 (when (= (move-to-column startcol
(or fill
'coerce
)) startcol
)
287 (unless (and (not fill
)
288 (= (point) (point-at-eol)))
289 (indent-to endcol
))))
291 (defun delete-whitespace-rectangle-line (startcol endcol fill
)
292 (when (= (move-to-column startcol
(or fill
'coerce
)) startcol
)
293 (unless (= (point) (point-at-eol))
294 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
296 ;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
298 (defun delete-whitespace-rectangle (start end
&optional fill
)
299 "Delete all whitespace following a specified column in each line.
300 The left edge of the rectangle specifies the position in each line
301 at which whitespace deletion should begin. On each line in the
302 rectangle, all continuous whitespace starting at that column is deleted.
304 When called from a program the rectangle's corners are START and END.
305 With a prefix (or a FILL) argument, also fill too short lines."
306 (interactive "*r\nP")
307 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill
))
309 ;; not used any more --dv
310 ;; string-rectangle uses this variable to pass the string
311 ;; to string-rectangle-line.
312 (defvar string-rectangle-string
)
313 (defvar string-rectangle-history nil
)
314 (defun string-rectangle-line (startcol endcol string delete
)
315 (move-to-column startcol t
)
317 (delete-rectangle-line startcol endcol nil
))
322 (defun string-rectangle (start end string
)
323 "Replace rectangle contents with STRING on each line.
324 The length of STRING need not be the same as the rectangle width.
326 Called from a program, takes three args; START, END and STRING."
328 (progn (barf-if-buffer-read-only)
332 (read-string (format "String rectangle (default `%s'): "
333 (or (car string-rectangle-history
) ""))
334 nil
'string-rectangle-history
335 (car string-rectangle-history
)))))
336 (apply-on-rectangle 'string-rectangle-line start end string t
))
338 (defalias 'replace-rectangle
'string-rectangle
)
341 (defun string-insert-rectangle (start end string
)
342 "Insert STRING on each line of region-rectangle, shifting text right.
344 When called from a program, the rectangle's corners are START and END.
345 The left edge of the rectangle specifies the column for insertion.
346 This command does not delete or overwrite any existing text."
348 (progn (barf-if-buffer-read-only)
352 (read-string (format "String insert rectangle (default `%s'): "
353 (or (car string-rectangle-history
) ""))
354 nil
'string-rectangle-history
355 (car string-rectangle-history
)))))
356 (apply-on-rectangle 'string-rectangle-line start end string nil
))
359 (defun clear-rectangle (start end
&optional fill
)
360 "Blank out the region-rectangle.
361 The text previously in the region is overwritten with blanks.
363 When called from a program the rectangle's corners are START and END.
364 With a prefix (or a FILL) argument, also fill with blanks the parts of the
365 rectangle which were empty."
366 (interactive "*r\nP")
367 (apply-on-rectangle 'clear-rectangle-line start end fill
))
369 (defun clear-rectangle-line (startcol endcol fill
)
370 (let ((pt (point-at-eol)))
371 (when (= (move-to-column startcol
(or fill
'coerce
)) startcol
)
373 (<= (save-excursion (goto-char pt
) (current-column)) endcol
))
374 (delete-region (point) pt
)
377 (move-to-column endcol t
)
378 (setq endcol
(current-column))
379 (delete-region pt
(point))
380 (indent-to endcol
)))))
384 ;;; rect.el ends here