1 ;;; rect.el --- rectangle functions for GNU Emacs
3 ;; Copyright (C) 1985, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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.
34 ;;; Global key bindings
36 ;;;###autoload (define-key ctl-x-r-map "c" 'clear-rectangle)
37 ;;;###autoload (define-key ctl-x-r-map "k" 'kill-rectangle)
38 ;;;###autoload (define-key ctl-x-r-map "d" 'delete-rectangle)
39 ;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle)
40 ;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle)
41 ;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle)
46 (defun move-to-column-force (column &optional flag
)
47 "If COLUMN is within a multi-column character, replace it by spaces and tab.
48 As for `move-to-column', passing anything but nil or t in FLAG will move to
49 the desired column only if the line is long enough."
50 (move-to-column column
(or flag t
)))
53 (make-obsolete 'move-to-column-force
'move-to-column
"21.2")
55 ;; not used any more --dv
56 ;; extract-rectangle-line stores lines into this list
57 ;; to accumulate them for extract-rectangle and delete-extract-rectangle.
58 (defvar operate-on-rectangle-lines
)
60 ;; ### NOTE: this function is untouched, but not used anymore apart from
61 ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
62 (defun operate-on-rectangle (function start end coerce-tabs
)
63 "Call FUNCTION for each line of rectangle with corners at START, END.
64 If COERCE-TABS is non-nil, convert multi-column characters
65 that span the starting or ending columns on any line
66 to multiple spaces before calling FUNCTION.
67 FUNCTION is called with three arguments:
68 position of start of segment of this line within the rectangle,
69 number of columns that belong to rectangle but are before that position,
70 number of columns that belong to rectangle but are after point.
71 Point is at the end of the segment of this line within the rectangle."
72 (let (startcol startlinepos endcol endlinepos
)
75 (setq startcol
(current-column))
77 (setq startlinepos
(point)))
80 (setq endcol
(current-column))
82 (setq endlinepos
(point-marker)))
83 (if (< endcol startcol
)
84 (setq startcol
(prog1 endcol
(setq endcol startcol
))))
86 (goto-char startlinepos
)
87 (while (< (point) endlinepos
)
88 (let (startpos begextra endextra
)
90 (move-to-column startcol t
)
91 (move-to-column startcol
))
92 (setq begextra
(- (current-column) startcol
))
93 (setq startpos
(point))
95 (move-to-column endcol t
)
96 (move-to-column endcol
))
97 ;; If we overshot, move back one character
98 ;; so that endextra will be positive.
99 (if (and (not coerce-tabs
) (> (current-column) endcol
))
101 (setq endextra
(- endcol
(current-column)))
103 (setq endextra
(+ endextra begextra
)
105 (funcall function startpos begextra endextra
))
107 (- endcol startcol
)))
109 ;; The replacement for `operate-on-rectangle' -- dv
110 (defun apply-on-rectangle (function start end
&rest args
)
111 "Call FUNCTION for each line of rectangle with corners at START, END.
112 FUNCTION is called with two arguments: the start and end columns of the
113 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
114 the function is called."
115 (let (startcol startpt endcol endpt
)
118 (setq startcol
(current-column))
120 (setq startpt
(point))
122 (setq endcol
(current-column))
124 (setq endpt
(point-marker))
125 ;; ensure the start column is the left one.
126 (if (< endcol startcol
)
127 (let ((col startcol
))
128 (setq startcol endcol endcol col
)))
129 ;; start looping over lines
131 (while (< (point) endpt
)
132 (apply function startcol endcol args
)
136 (defun delete-rectangle-line (startcol endcol fill
)
137 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
138 (delete-region (point)
139 (progn (move-to-column endcol
'coerce
)
142 (defun delete-extract-rectangle-line (startcol endcol lines fill
)
143 (let ((pt (point-at-eol)))
144 (if (< (move-to-column startcol
(if fill t
'coerce
)) startcol
)
145 (setcdr lines
(cons (spaces-string (- endcol startcol
))
149 (move-to-column endcol t
)
150 (setcdr lines
(cons (filter-buffer-substring pt
(point) t
) (cdr lines
))))
153 ;; ### NOTE: this is actually the only function that needs to do complicated
154 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
155 ;; might be read-only. --dv
156 (defun extract-rectangle-line (startcol endcol lines
)
157 (let (start end begextra endextra line
)
158 (move-to-column startcol
)
160 begextra
(- (current-column) startcol
))
161 (move-to-column endcol
)
163 endextra
(- endcol
(current-column)))
164 (setq line
(buffer-substring start
(point)))
166 (setq endextra
(+ endextra begextra
)
171 (while (search-forward "\t" end t
)
172 (let ((width (- (current-column)
173 (save-excursion (forward-char -
1)
175 (setq line
(concat (substring line
0 (- (point) end
1))
176 (spaces-string width
)
177 (substring line
(+ (length line
)
178 (- (point) end
)))))))
179 (if (or (> begextra
0) (> endextra
0))
180 (setq line
(concat (spaces-string begextra
)
182 (spaces-string endextra
))))
183 (setcdr lines
(cons line
(cdr lines
)))))
185 (defconst spaces-strings
186 '["" " " " " " " " " " " " " " " " "])
188 ;; this one is untouched --dv
189 (defun spaces-string (n)
190 "Return a string with N spaces."
191 (if (<= n
8) (aref spaces-strings n
)
192 (make-string n ?\s
)))
195 (defun delete-rectangle (start end
&optional fill
)
196 "Delete (don't save) text in the region-rectangle.
197 The same range of columns is deleted in each line starting with the
198 line where the region begins and ending with the line where the region
201 When called from a program the rectangle's corners are START and END.
202 With a prefix (or a FILL) argument, also fill lines where nothing has
204 (interactive "*r\nP")
205 (apply-on-rectangle 'delete-rectangle-line start end fill
))
208 (defun delete-extract-rectangle (start end
&optional fill
)
209 "Delete the contents of the rectangle with corners at START and END.
210 Return it as a list of strings, one for each line of the rectangle.
212 When called from a program the rectangle's corners are START and END.
213 With an optional FILL argument, also fill lines where nothing has to be
215 (let ((lines (list nil
)))
216 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill
)
217 (nreverse (cdr lines
))))
220 (defun extract-rectangle (start end
)
221 "Return the contents of the rectangle with corners at START and END.
222 Return it as a list of strings, one for each line of the rectangle."
223 (let ((lines (list nil
)))
224 (apply-on-rectangle 'extract-rectangle-line start end lines
)
225 (nreverse (cdr lines
))))
227 (defvar killed-rectangle nil
228 "Rectangle for `yank-rectangle' to insert.")
231 (defun kill-rectangle (start end
&optional fill
)
232 "Delete the region-rectangle and save it as the last killed one.
234 When called from a program the rectangle's corners are START and END.
235 You might prefer to use `delete-extract-rectangle' from a program.
237 With a prefix (or a FILL) argument, also fill lines where nothing has to be
240 If the buffer is read-only, Emacs will beep and refrain from deleting
241 the rectangle, but put it in the kill ring anyway. This means that
242 you can use this command to copy text from a read-only buffer.
243 \(If the variable `kill-read-only-ok' is non-nil, then this won't
247 (setq killed-rectangle
(delete-extract-rectangle start end fill
))
248 ((buffer-read-only text-read-only
)
249 (setq killed-rectangle
(extract-rectangle start end
))
250 (if kill-read-only-ok
251 (progn (message "Read only text copied to kill ring") nil
)
252 (barf-if-buffer-read-only)
253 (signal 'text-read-only
(list (current-buffer)))))))
255 ;; this one is untouched --dv
257 (defun yank-rectangle ()
258 "Yank the last killed rectangle with upper left corner at point."
260 (insert-rectangle killed-rectangle
))
262 ;; this one is untoutched --dv
264 (defun insert-rectangle (rectangle)
265 "Insert text of RECTANGLE with upper left corner at point.
266 RECTANGLE's first line is inserted at point, its second
267 line is inserted at a point vertically under point, etc.
268 RECTANGLE should be a list of strings.
269 After this command, the mark is at the upper left corner
270 and point is at the lower right corner."
271 (let ((lines rectangle
)
272 (insertcolumn (current-column))
279 (or (bolp) (insert ?
\n))
280 (move-to-column insertcolumn t
)))
282 (insert-for-yank (car lines
))
283 (setq lines
(cdr lines
)))))
286 (defun open-rectangle (start end
&optional fill
)
287 "Blank out the region-rectangle, shifting text right.
289 The text previously in the region is not overwritten by the blanks,
290 but instead winds up to the right of the rectangle.
292 When called from a program the rectangle's corners are START and END.
293 With a prefix (or a FILL) argument, fill with blanks even if there is
294 no text on the right side of the rectangle."
295 (interactive "*r\nP")
296 (apply-on-rectangle 'open-rectangle-line start end fill
)
299 (defun open-rectangle-line (startcol endcol fill
)
300 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
301 (unless (and (not fill
)
302 (= (point) (point-at-eol)))
303 (indent-to endcol
))))
305 (defun delete-whitespace-rectangle-line (startcol endcol fill
)
306 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
307 (unless (= (point) (point-at-eol))
308 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
311 (defalias 'close-rectangle
'delete-whitespace-rectangle
) ;; Old name
314 (defun delete-whitespace-rectangle (start end
&optional fill
)
315 "Delete all whitespace following a specified column in each line.
316 The left edge of the rectangle specifies the position in each line
317 at which whitespace deletion should begin. On each line in the
318 rectangle, all continuous whitespace starting at that column is deleted.
320 When called from a program the rectangle's corners are START and END.
321 With a prefix (or a FILL) argument, also fill too short lines."
322 (interactive "*r\nP")
323 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill
))
325 ;; not used any more --dv
326 ;; string-rectangle uses this variable to pass the string
327 ;; to string-rectangle-line.
328 (defvar string-rectangle-string
)
329 (defvar string-rectangle-history nil
)
330 (defun string-rectangle-line (startcol endcol string delete
)
331 (move-to-column startcol t
)
333 (delete-rectangle-line startcol endcol nil
))
337 (defun string-rectangle (start end string
)
338 "Replace rectangle contents with STRING on each line.
339 The length of STRING need not be the same as the rectangle width.
341 Called from a program, takes three args; START, END and STRING."
343 (progn (barf-if-buffer-read-only)
347 (read-string (format "String rectangle (default %s): "
348 (or (car string-rectangle-history
) ""))
349 nil
'string-rectangle-history
350 (car string-rectangle-history
)))))
351 (apply-on-rectangle 'string-rectangle-line start end string t
))
354 (defalias 'replace-rectangle
'string-rectangle
)
357 (defun string-insert-rectangle (start end string
)
358 "Insert STRING on each line of region-rectangle, shifting text right.
360 When called from a program, the rectangle's corners are START and END.
361 The left edge of the rectangle specifies the column for insertion.
362 This command does not delete or overwrite any existing text."
364 (progn (barf-if-buffer-read-only)
368 (read-string (format "String insert rectangle (default %s): "
369 (or (car string-rectangle-history
) ""))
370 nil
'string-rectangle-history
371 (car string-rectangle-history
)))))
372 (apply-on-rectangle 'string-rectangle-line start end string nil
))
375 (defun clear-rectangle (start end
&optional fill
)
376 "Blank out the region-rectangle.
377 The text previously in the region is overwritten with blanks.
379 When called from a program the rectangle's corners are START and END.
380 With a prefix (or a FILL) argument, also fill with blanks the parts of the
381 rectangle which were empty."
382 (interactive "*r\nP")
383 (apply-on-rectangle 'clear-rectangle-line start end fill
))
385 (defun clear-rectangle-line (startcol endcol fill
)
386 (let ((pt (point-at-eol)))
387 (when (= (move-to-column startcol
(if fill t
'coerce
)) startcol
)
389 (<= (save-excursion (goto-char pt
) (current-column)) endcol
))
390 (delete-region (point) pt
)
393 (move-to-column endcol t
)
394 (setq endcol
(current-column))
395 (delete-region pt
(point))
396 (indent-to endcol
)))))
400 ;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
401 ;;; rect.el ends here