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
)))
268 (insert-for-yank (car lines
))
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))))))
297 (defalias 'close-rectangle
'delete-whitespace-rectangle
) ;; Old name
300 (defun delete-whitespace-rectangle (start end
&optional fill
)
301 "Delete all whitespace following a specified column in each line.
302 The left edge of the rectangle specifies the position in each line
303 at which whitespace deletion should begin. On each line in the
304 rectangle, all continuous whitespace starting at that column is deleted.
306 When called from a program the rectangle's corners are START and END.
307 With a prefix (or a FILL) argument, also fill too short lines."
308 (interactive "*r\nP")
309 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill
))
311 ;; not used any more --dv
312 ;; string-rectangle uses this variable to pass the string
313 ;; to string-rectangle-line.
314 (defvar string-rectangle-string
)
315 (defvar string-rectangle-history nil
)
316 (defun string-rectangle-line (startcol endcol string delete
)
317 (move-to-column startcol t
)
319 (delete-rectangle-line startcol endcol nil
))
323 (defun string-rectangle (start end string
)
324 "Replace rectangle contents with STRING on each line.
325 The length of STRING need not be the same as the rectangle width.
327 Called from a program, takes three args; START, END and STRING."
329 (progn (barf-if-buffer-read-only)
333 (read-string (format "String rectangle (default `%s'): "
334 (or (car string-rectangle-history
) ""))
335 nil
'string-rectangle-history
336 (car string-rectangle-history
)))))
337 (apply-on-rectangle 'string-rectangle-line start end string t
))
340 (defalias 'replace-rectangle
'string-rectangle
)
343 (defun string-insert-rectangle (start end string
)
344 "Insert STRING on each line of region-rectangle, shifting text right.
346 When called from a program, the rectangle's corners are START and END.
347 The left edge of the rectangle specifies the column for insertion.
348 This command does not delete or overwrite any existing text."
350 (progn (barf-if-buffer-read-only)
354 (read-string (format "String insert rectangle (default `%s'): "
355 (or (car string-rectangle-history
) ""))
356 nil
'string-rectangle-history
357 (car string-rectangle-history
)))))
358 (apply-on-rectangle 'string-rectangle-line start end string nil
))
361 (defun clear-rectangle (start end
&optional fill
)
362 "Blank out the region-rectangle.
363 The text previously in the region is overwritten with blanks.
365 When called from a program the rectangle's corners are START and END.
366 With a prefix (or a FILL) argument, also fill with blanks the parts of the
367 rectangle which were empty."
368 (interactive "*r\nP")
369 (apply-on-rectangle 'clear-rectangle-line start end fill
))
371 (defun clear-rectangle-line (startcol endcol fill
)
372 (let ((pt (point-at-eol)))
373 (when (= (move-to-column startcol
(or fill
'coerce
)) startcol
)
375 (<= (save-excursion (goto-char pt
) (current-column)) endcol
))
376 (delete-region (point) pt
)
379 (move-to-column endcol t
)
380 (setq endcol
(current-column))
381 (delete-region pt
(point))
382 (indent-to endcol
)))))
386 ;;; rect.el ends here