1 ;;; rect.el --- rectangle functions for GNU Emacs
3 ;; Copyright (C) 1985, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 2005, 2006 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This package provides the operations on rectangles that are documented
29 ;; in the Emacs manual.
31 ;; ### NOTE: this file has been almost completely rewritten by Didier Verna
32 ;; <didier@xemacs.org> in July 1999. The purpose of this rewrite is to be less
33 ;; intrusive and fill lines with whitespaces only when needed. A few functions
34 ;; are untouched though, as noted above their definition.
40 (defun move-to-column-force (column &optional flag
)
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
)))
47 (make-obsolete 'move-to-column-force
'move-to-column
"21.2")
49 ;; not used any more --dv
50 ;; extract-rectangle-line stores lines into this list
51 ;; to accumulate them for extract-rectangle and delete-extract-rectangle.
52 (defvar operate-on-rectangle-lines
)
54 ;; ### NOTE: this function is untouched, but not used anymore apart from
55 ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
56 (defun operate-on-rectangle (function start end coerce-tabs
)
57 "Call FUNCTION for each line of rectangle with corners at START, END.
58 If COERCE-TABS is non-nil, convert multi-column characters
59 that span the starting or ending columns on any line
60 to multiple spaces before calling FUNCTION.
61 FUNCTION is called with three arguments:
62 position of start of segment of this line within the rectangle,
63 number of columns that belong to rectangle but are before that position,
64 number of columns that belong to rectangle but are after point.
65 Point is at the end of the segment of this line within the rectangle."
66 (let (startcol startlinepos endcol endlinepos
)
69 (setq startcol
(current-column))
71 (setq startlinepos
(point)))
74 (setq endcol
(current-column))
76 (setq endlinepos
(point-marker)))
77 (if (< endcol startcol
)
78 (setq startcol
(prog1 endcol
(setq endcol startcol
))))
80 (goto-char startlinepos
)
81 (while (< (point) endlinepos
)
82 (let (startpos begextra endextra
)
84 (move-to-column startcol t
)
85 (move-to-column startcol
))
86 (setq begextra
(- (current-column) startcol
))
87 (setq startpos
(point))
89 (move-to-column endcol t
)
90 (move-to-column endcol
))
91 ;; If we overshot, move back one character
92 ;; so that endextra will be positive.
93 (if (and (not coerce-tabs
) (> (current-column) endcol
))
95 (setq endextra
(- endcol
(current-column)))
97 (setq endextra
(+ endextra begextra
)
99 (funcall function startpos begextra endextra
))
101 (- endcol startcol
)))
103 ;; The replacement for `operate-on-rectangle' -- dv
104 (defun apply-on-rectangle (function start end
&rest args
)
105 "Call FUNCTION for each line of rectangle with corners at START, END.
106 FUNCTION is called with two arguments: the start and end columns of the
107 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
108 the function is called."
109 (let (startcol startpt endcol endpt
)
112 (setq startcol
(current-column))
114 (setq startpt
(point))
116 (setq endcol
(current-column))
118 (setq endpt
(point-marker))
119 ;; ensure the start column is the left one.
120 (if (< endcol startcol
)
121 (let ((col startcol
))
122 (setq startcol endcol endcol col
)))
123 ;; start looping over lines
125 (while (< (point) endpt
)
126 (apply function startcol endcol args
)
130 (defun delete-rectangle-line (startcol endcol fill
)
131 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
132 (delete-region (point)
133 (progn (move-to-column endcol
'coerce
)
136 (defun delete-extract-rectangle-line (startcol endcol lines fill
)
137 (let ((pt (point-at-eol)))
138 (if (< (move-to-column startcol
(if fill t
'coerce
)) startcol
)
139 (setcdr lines
(cons (spaces-string (- endcol startcol
))
143 (move-to-column endcol t
)
144 (setcdr lines
(cons (buffer-substring pt
(point)) (cdr lines
)))
145 (delete-region pt
(point)))
148 ;; ### NOTE: this is actually the only function that needs to do complicated
149 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
150 ;; might be read-only. --dv
151 (defun extract-rectangle-line (startcol endcol lines
)
152 (let (start end begextra endextra line
)
153 (move-to-column startcol
)
155 begextra
(- (current-column) startcol
))
156 (move-to-column endcol
)
158 endextra
(- endcol
(current-column)))
159 (setq line
(buffer-substring start
(point)))
161 (setq endextra
(+ endextra begextra
)
166 (while (search-forward "\t" end t
)
167 (let ((width (- (current-column)
168 (save-excursion (forward-char -
1)
170 (setq line
(concat (substring line
0 (- (point) end
1))
171 (spaces-string width
)
172 (substring line
(+ (length line
)
173 (- (point) end
)))))))
174 (if (or (> begextra
0) (> endextra
0))
175 (setq line
(concat (spaces-string begextra
)
177 (spaces-string endextra
))))
178 (setcdr lines
(cons line
(cdr lines
)))))
180 (defconst spaces-strings
181 '["" " " " " " " " " " " " " " " " "])
183 ;; this one is untouched --dv
184 (defun spaces-string (n)
185 (if (<= n
8) (aref spaces-strings n
)
188 (setq val
(concat " " val
)
190 (concat val
(aref spaces-strings n
)))))
193 (defun delete-rectangle (start end
&optional fill
)
194 "Delete (don't save) text in the region-rectangle.
195 The same range of columns is deleted in each line starting with the
196 line where the region begins and ending with the line where the region
199 When called from a program the rectangle's corners are START and END.
200 With a prefix (or a FILL) argument, also fill lines where nothing has
202 (interactive "*r\nP")
203 (apply-on-rectangle 'delete-rectangle-line start end fill
))
206 (defun delete-extract-rectangle (start end
&optional fill
)
207 "Delete the contents of the rectangle with corners at START and END.
208 Return it as a list of strings, one for each line of the rectangle.
210 When called from a program the rectangle's corners are START and END.
211 With an optional FILL argument, also fill lines where nothing has to be
213 (let ((lines (list nil
)))
214 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill
)
215 (nreverse (cdr lines
))))
218 (defun extract-rectangle (start end
)
219 "Return the contents of the rectangle with corners at START and END.
220 Return it as a list of strings, one for each line of the rectangle."
221 (let ((lines (list nil
)))
222 (apply-on-rectangle 'extract-rectangle-line start end lines
)
223 (nreverse (cdr lines
))))
225 (defvar killed-rectangle nil
226 "Rectangle for `yank-rectangle' to insert.")
229 (defun kill-rectangle (start end
&optional fill
)
230 "Delete the region-rectangle and save it as the last killed one.
232 When called from a program the rectangle's corners are START and END.
233 You might prefer to use `delete-extract-rectangle' from a program.
235 With a prefix (or a FILL) argument, also fill lines where nothing has to be
237 (interactive "*r\nP")
238 (when buffer-read-only
239 (setq killed-rectangle
(extract-rectangle start end
))
240 (barf-if-buffer-read-only))
241 (setq killed-rectangle
(delete-extract-rectangle start end fill
)))
243 ;; this one is untouched --dv
245 (defun yank-rectangle ()
246 "Yank the last killed rectangle with upper left corner at point."
248 (insert-rectangle killed-rectangle
))
250 ;; this one is untoutched --dv
252 (defun insert-rectangle (rectangle)
253 "Insert text of RECTANGLE with upper left corner at point.
254 RECTANGLE's first line is inserted at point, its second
255 line is inserted at a point vertically under point, etc.
256 RECTANGLE should be a list of strings.
257 After this command, the mark is at the upper left corner
258 and point is at the lower right corner."
259 (let ((lines rectangle
)
260 (insertcolumn (current-column))
267 (or (bolp) (insert ?
\n))
268 (move-to-column insertcolumn t
)))
270 (insert-for-yank (car lines
))
271 (setq lines
(cdr lines
)))))
274 (defun open-rectangle (start end
&optional fill
)
275 "Blank out the region-rectangle, shifting text right.
277 The text previously in the region is not overwritten by the blanks,
278 but instead winds up to the right of the rectangle.
280 When called from a program the rectangle's corners are START and END.
281 With a prefix (or a FILL) argument, fill with blanks even if there is no text
282 on the right side of the rectangle."
283 (interactive "*r\nP")
284 (apply-on-rectangle 'open-rectangle-line start end fill
)
287 (defun open-rectangle-line (startcol endcol fill
)
288 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
289 (unless (and (not fill
)
290 (= (point) (point-at-eol)))
291 (indent-to endcol
))))
293 (defun delete-whitespace-rectangle-line (startcol endcol fill
)
294 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
295 (unless (= (point) (point-at-eol))
296 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
299 (defalias 'close-rectangle
'delete-whitespace-rectangle
) ;; Old name
302 (defun delete-whitespace-rectangle (start end
&optional fill
)
303 "Delete all whitespace following a specified column in each line.
304 The left edge of the rectangle specifies the position in each line
305 at which whitespace deletion should begin. On each line in the
306 rectangle, all continuous whitespace starting at that column is deleted.
308 When called from a program the rectangle's corners are START and END.
309 With a prefix (or a FILL) argument, also fill too short lines."
310 (interactive "*r\nP")
311 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill
))
313 ;; not used any more --dv
314 ;; string-rectangle uses this variable to pass the string
315 ;; to string-rectangle-line.
316 (defvar string-rectangle-string
)
317 (defvar string-rectangle-history nil
)
318 (defun string-rectangle-line (startcol endcol string delete
)
319 (move-to-column startcol t
)
321 (delete-rectangle-line startcol endcol nil
))
325 (defun string-rectangle (start end string
)
326 "Replace rectangle contents with STRING on each line.
327 The length of STRING need not be the same as the rectangle width.
329 Called from a program, takes three args; START, END and STRING."
331 (progn (barf-if-buffer-read-only)
335 (read-string (format "String rectangle (default %s): "
336 (or (car string-rectangle-history
) ""))
337 nil
'string-rectangle-history
338 (car string-rectangle-history
)))))
339 (apply-on-rectangle 'string-rectangle-line start end string t
))
342 (defalias 'replace-rectangle
'string-rectangle
)
345 (defun string-insert-rectangle (start end string
)
346 "Insert STRING on each line of region-rectangle, shifting text right.
348 When called from a program, the rectangle's corners are START and END.
349 The left edge of the rectangle specifies the column for insertion.
350 This command does not delete or overwrite any existing text."
352 (progn (barf-if-buffer-read-only)
356 (read-string (format "String insert rectangle (default %s): "
357 (or (car string-rectangle-history
) ""))
358 nil
'string-rectangle-history
359 (car string-rectangle-history
)))))
360 (apply-on-rectangle 'string-rectangle-line start end string nil
))
363 (defun clear-rectangle (start end
&optional fill
)
364 "Blank out the region-rectangle.
365 The text previously in the region is overwritten with blanks.
367 When called from a program the rectangle's corners are START and END.
368 With a prefix (or a FILL) argument, also fill with blanks the parts of the
369 rectangle which were empty."
370 (interactive "*r\nP")
371 (apply-on-rectangle 'clear-rectangle-line start end fill
))
373 (defun clear-rectangle-line (startcol endcol fill
)
374 (let ((pt (point-at-eol)))
375 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
377 (<= (save-excursion (goto-char pt
) (current-column)) endcol
))
378 (delete-region (point) pt
)
381 (move-to-column endcol t
)
382 (setq endcol
(current-column))
383 (delete-region pt
(point))
384 (indent-to endcol
)))))
388 ;;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
389 ;;; rect.el ends here