1 ;;; rect.el --- rectangle functions for SXEmacs.
3 ;; Copyright (C) 1985-2000 Free Software Foundation, Inc.
5 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; This file is part of SXEmacs.
10 ;; SXEmacs 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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Synched up with: to be incorporated in a forthcoming GNU Emacs
27 ;; This package provides the operations on rectangles that are documented
28 ;; in the XEmacs Reference Manual.
30 ;; #### NOTE: this file has been almost completely rewritten by Didier Verna
31 ;; <didier@xemacs.org>, Jul 99. 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.
38 ;; #### NOTE: this function is untouched, but not used anymore.
39 ;; `apply-on-rectangle' is used instead. It's still there because it's
40 ;; documented so people might use it in their code, so I've decided not to
43 (defun operate-on-rectangle (function start end coerce-tabs &rest extra-args)
44 "Call FUNCTION for each line of rectangle with corners at START, END.
45 If COERCE-TABS is non-nil, convert multi-column characters
46 that span the starting or ending columns on any line
47 to multiple spaces before calling FUNCTION.
48 FUNCTION is called with three arguments:
49 position of start of segment of this line within the rectangle,
50 number of columns that belong to rectangle but are before that position,
51 number of columns that belong to rectangle but are after point.
52 Point is at the end of the segment of this line within the rectangle."
53 (let (startcol startlinepos endcol endlinepos)
56 (setq startcol (current-column))
58 (setq startlinepos (point)))
61 (setq endcol (current-column))
63 (setq endlinepos (point-marker)))
64 (if (< endcol startcol)
67 (setq startcol endcol endcol tem)))
69 (goto-char startlinepos)
70 (while (< (point) endlinepos)
71 (let (startpos begextra endextra)
72 (move-to-column startcol coerce-tabs)
73 (setq begextra (- (current-column) startcol))
74 (setq startpos (point))
75 (move-to-column endcol coerce-tabs)
76 (setq endextra (- endcol (current-column)))
78 (setq endextra (+ endextra begextra)
80 (if (< endextra 0) (setq endextra 0))
81 (apply function startpos begextra endextra extra-args))
85 ;; The replacement for `operate-on-rectangle' -- dv
86 (defun apply-on-rectangle (function start end &rest args)
87 "Call FUNCTION for each line of rectangle with corners at START and END.
88 FUNCTION is called with two arguments: the start and end columns of the
89 rectangle, plus ARGS extra arguments. Point is at the beginning of line
90 when the function is called."
91 (let (startcol startpt endcol endpt)
94 (setq startcol (current-column))
96 (setq startpt (point))
98 (setq endcol (current-column))
100 (setq endpt (point-marker))
101 ;; ensure the start column is the left one.
102 (if (< endcol startcol)
103 (let ((col startcol))
104 (setq startcol endcol endcol col)))
105 ;; start looping over lines
107 (while (< (point) endpt)
108 (apply function startcol endcol args)
113 (defun delete-rectangle-line (startcol endcol fill)
114 (let ((pt (point-at-eol)))
115 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
116 (if (and (not fill) (<= pt endcol))
117 (delete-region (point) pt)
120 (move-to-column endcol t)
121 (delete-region pt (point))))
125 (defun delete-rectangle (start end &optional fill)
126 "Delete the text in the region-rectangle without saving it.
127 The same range of columns is deleted in each line starting with the line
128 where the region begins and ending with the line where the region ends.
130 When called from a program, the rectangle's corners are START and END.
131 With a prefix (or FILL) argument, also fill lines where nothing has to be
133 (interactive "*r\nP")
134 (apply-on-rectangle 'delete-rectangle-line start end fill))
137 ;; I love ascii art ;-)
138 (defconst spaces-strings '[""
148 ;; This function is untouched --dv
149 (defun spaces-string (n)
150 (if (<= n 8) (aref spaces-strings n)
153 (setq val (concat " " val)
155 (concat val (aref spaces-strings n)))))
158 (defun delete-extract-rectangle-line (startcol endcol lines fill)
159 (let ((pt (point-at-eol)))
160 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
161 (setcdr lines (cons (spaces-string (- endcol startcol))
165 (move-to-column endcol t)
166 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
167 (delete-region pt (point)))
171 (defun delete-extract-rectangle (start end &optional fill)
172 "Delete the contents of the rectangle with corners at START and END, and
173 return it as a list of strings, one for each line of the rectangle.
175 With an optional FILL argument, also fill lines where nothing has to be
177 (let ((lines (list nil)))
178 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
179 (nreverse (cdr lines))))
182 ;; #### NOTE: this is actually the only function that needs to do complicated
183 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
184 ;; might be read-only. --dv
185 (defun extract-rectangle-line (startcol endcol lines)
186 (let (start end begextra endextra line)
187 (move-to-column startcol)
189 begextra (- (current-column) startcol))
190 (move-to-column endcol)
192 endextra (- endcol (current-column)))
193 (setq line (buffer-substring start (point)))
195 (setq endextra (+ endextra begextra)
200 (while (search-forward "\t" end t)
201 (let ((width (- (current-column)
202 (save-excursion (backward-char 1)
204 (setq line (concat (substring line 0 (- (point) end 1))
205 (spaces-string width)
206 (substring line (+ (length line)
207 (- (point) end)))))))
208 (if (or (> begextra 0) (> endextra 0))
209 (setq line (concat (spaces-string begextra)
211 (spaces-string endextra))))
212 (setcdr lines (cons line (cdr lines)))))
215 (defun extract-rectangle (start end)
216 "Return the contents of the rectangle with corners at START and END,
217 as a list of strings, one for each line of the rectangle."
218 (let ((lines (list nil)))
219 (apply-on-rectangle 'extract-rectangle-line start end lines)
220 (nreverse (cdr lines))))
224 (defvar killed-rectangle nil
225 "Rectangle for `yank-rectangle' to insert.")
228 (defun kill-rectangle (start end &optional fill)
229 "Delete the region-rectangle and save it as the last killed one.
230 You might prefer to use `delete-extract-rectangle' from a program.
232 When called from a program, the rectangle's corners are START and END.
233 With a prefix (or 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 function is untouched --dv
243 (defun yank-rectangle ()
244 "Yank the last killed rectangle with upper left corner at point."
246 (insert-rectangle killed-rectangle))
249 ;; This function is untouched --dv
251 (defun insert-rectangle (rectangle)
252 "Insert text of RECTANGLE with upper left corner at point.
253 RECTANGLE's first line is inserted at point, its second
254 line is inserted at a point vertically under point, etc.
255 RECTANGLE should be a list of strings.
256 After this command, the mark is at the upper left corner
257 and point is at the lower right corner."
258 (let ((lines rectangle)
259 (insertcolumn (current-column))
266 (or (bolp) (insert ?\n))
267 (move-to-column insertcolumn t)))
270 (setq lines (cdr lines)))))
273 (defun open-rectangle-line (startcol endcol fill)
274 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
275 (unless (and (not fill)
276 (= (point) (point-at-eol)))
277 (indent-to endcol))))
280 (defun open-rectangle (start end &optional fill)
281 "Blank out the region-rectangle, shifting text right.
283 When called from a program, the rectangle's corners are START and END.
284 With a prefix (or FILL) argument, fill with blanks even if there is no text
285 on the right side of the rectangle."
286 (interactive "*r\nP")
287 (apply-on-rectangle 'open-rectangle-line start end fill)
291 (defun string-rectangle-line (startcol endcol string delete)
292 (move-to-column startcol t)
294 (delete-rectangle-line startcol endcol nil))
298 (defun string-rectangle (start end string)
299 "Insert STRING on each line of the region-rectangle, shifting text right.
300 The left edge of the rectangle specifies the column for insertion.
302 If `pending-delete-mode' is active the string replace the region.
303 Otherwise this command does not delete or overwrite any existing text.
305 When called from a program, the rectangle's corners are START and END."
306 (interactive "*r\nsString rectangle: ")
307 (defvar pending-delete-mode)
308 (apply-on-rectangle 'string-rectangle-line start end string
309 (and (boundp 'pending-delete-mode) pending-delete-mode)))
312 (defun replace-rectangle (start end string)
313 "Like `string-rectangle', but unconditionally replace the original region,
314 as if `pending-delete-mode' were active."
315 (interactive "*r\nsString rectangle: ")
316 (apply-on-rectangle 'string-rectangle-line start end string t))
319 (defun clear-rectangle-line (startcol endcol fill)
320 (let ((pt (point-at-eol))
322 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
324 (<= (save-excursion (goto-char pt) (current-column)) endcol))
325 (delete-region (point) pt)
328 (move-to-column endcol t)
329 (setq spaces (- (point) pt))
330 (delete-region pt (point))
331 (indent-to (+ (current-column) spaces))))
335 (defun clear-rectangle (start end &optional fill)
336 "Blank out the region-rectangle.
337 The text previously in the region is overwritten with blanks.
339 When called from a program, the rectangle's corners are START and END.
340 With a prefix (or FILL) argument, also fill with blanks the parts of the
341 rectangle which were empty."
342 (interactive "*r\nP")
343 (apply-on-rectangle 'clear-rectangle-line start end fill))
348 ;;; rect.el ends here