1 ;;; rect.el --- rectangle functions for XEmacs.
3 ;; Copyright (C) 1985-2000 Free Software Foundation, Inc.
5 ;; Maintainer: Didier Verna <didier@xemacs.org>
8 ;; This file is part of XEmacs.
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;;; Synched up with: to be incorporated in a forthcoming GNU Emacs
29 ;; This package provides the operations on rectangles that are documented
30 ;; in the XEmacs Reference Manual.
32 ;; #### NOTE: this file has been almost completely rewritten by Didier Verna
33 ;; <didier@xemacs.org>, Jul 99. The purpose of this rewrite is to be less
34 ;; intrusive and fill lines with whitespaces only when needed. A few functions
35 ;; are untouched though, as noted above their definition.
40 ;; #### NOTE: this function is untouched, but not used anymore.
41 ;; `apply-on-rectangle' is used instead. It's still there because it's
42 ;; documented so people might use it in their code, so I've decided not to
45 (defun operate-on-rectangle (function start end coerce-tabs &rest extra-args)
46 "Call FUNCTION for each line of rectangle with corners at START, END.
47 If COERCE-TABS is non-nil, convert multi-column characters
48 that span the starting or ending columns on any line
49 to multiple spaces before calling FUNCTION.
50 FUNCTION is called with three arguments:
51 position of start of segment of this line within the rectangle,
52 number of columns that belong to rectangle but are before that position,
53 number of columns that belong to rectangle but are after point.
54 Point is at the end of the segment of this line within the rectangle."
55 (let (startcol startlinepos endcol endlinepos)
58 (setq startcol (current-column))
60 (setq startlinepos (point)))
63 (setq endcol (current-column))
65 (setq endlinepos (point-marker)))
66 (if (< endcol startcol)
69 (setq startcol endcol endcol tem)))
71 (goto-char startlinepos)
72 (while (< (point) endlinepos)
73 (let (startpos begextra endextra)
74 (move-to-column startcol coerce-tabs)
75 (setq begextra (- (current-column) startcol))
76 (setq startpos (point))
77 (move-to-column endcol coerce-tabs)
78 (setq endextra (- endcol (current-column)))
80 (setq endextra (+ endextra begextra)
82 (if (< endextra 0) (setq endextra 0))
83 (apply function startpos begextra endextra extra-args))
87 ;; The replacement for `operate-on-rectangle' -- dv
88 (defun apply-on-rectangle (function start end &rest args)
89 "Call FUNCTION for each line of rectangle with corners at START and END.
90 FUNCTION is called with two arguments: the start and end columns of the
91 rectangle, plus ARGS extra arguments. Point is at the beginning of line
92 when the function is called."
93 (let (startcol startpt endcol endpt)
96 (setq startcol (current-column))
98 (setq startpt (point))
100 (setq endcol (current-column))
102 (setq endpt (point-marker))
103 ;; ensure the start column is the left one.
104 (if (< endcol startcol)
105 (let ((col startcol))
106 (setq startcol endcol endcol col)))
107 ;; start looping over lines
109 (while (< (point) endpt)
110 (apply function startcol endcol args)
115 (defun delete-rectangle-line (startcol endcol fill)
116 (let ((pt (point-at-eol)))
117 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
118 (if (and (not fill) (<= pt endcol))
119 (delete-region (point) pt)
122 (move-to-column endcol t)
123 (delete-region pt (point))))
127 (defun delete-rectangle (start end &optional fill)
128 "Delete the text in the region-rectangle without saving it.
129 The same range of columns is deleted in each line starting with the line
130 where the region begins and ending with the line where the region ends.
132 When called from a program, the rectangle's corners are START and END.
133 With a prefix (or FILL) argument, also fill lines where nothing has to be
135 (interactive "*r\nP")
136 (apply-on-rectangle 'delete-rectangle-line start end fill))
139 ;; I love ascii art ;-)
140 (defconst spaces-strings '[""
150 ;; This function is untouched --dv
151 (defun spaces-string (n)
152 (if (<= n 8) (aref spaces-strings n)
155 (setq val (concat " " val)
157 (concat val (aref spaces-strings n)))))
160 (defun delete-extract-rectangle-line (startcol endcol lines fill)
161 (let ((pt (point-at-eol)))
162 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
163 (setcdr lines (cons (spaces-string (- endcol startcol))
167 (move-to-column endcol t)
168 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
169 (delete-region pt (point)))
173 (defun delete-extract-rectangle (start end &optional fill)
174 "Delete the contents of the rectangle with corners at START and END, and
175 return it as a list of strings, one for each line of the rectangle.
177 With an optional FILL argument, also fill lines where nothing has to be
179 (let ((lines (list nil)))
180 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
181 (nreverse (cdr lines))))
184 ;; #### NOTE: this is actually the only function that needs to do complicated
185 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
186 ;; might be read-only. --dv
187 (defun extract-rectangle-line (startcol endcol lines)
188 (let (start end begextra endextra line)
189 (move-to-column startcol)
191 begextra (- (current-column) startcol))
192 (move-to-column endcol)
194 endextra (- endcol (current-column)))
195 (setq line (buffer-substring start (point)))
197 (setq endextra (+ endextra begextra)
202 (while (search-forward "\t" end t)
203 (let ((width (- (current-column)
204 (save-excursion (backward-char 1)
206 (setq line (concat (substring line 0 (- (point) end 1))
207 (spaces-string width)
208 (substring line (+ (length line)
209 (- (point) end)))))))
210 (if (or (> begextra 0) (> endextra 0))
211 (setq line (concat (spaces-string begextra)
213 (spaces-string endextra))))
214 (setcdr lines (cons line (cdr lines)))))
217 (defun extract-rectangle (start end)
218 "Return the contents of the rectangle with corners at START and END,
219 as a list of strings, one for each line of the rectangle."
220 (let ((lines (list nil)))
221 (apply-on-rectangle 'extract-rectangle-line start end lines)
222 (nreverse (cdr lines))))
226 (defvar killed-rectangle nil
227 "Rectangle for `yank-rectangle' to insert.")
230 (defun kill-rectangle (start end &optional fill)
231 "Delete the region-rectangle and save it as the last killed one.
232 You might prefer to use `delete-extract-rectangle' from a program.
234 When called from a program, the rectangle's corners are START and END.
235 With a prefix (or 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 function is untouched --dv
245 (defun yank-rectangle ()
246 "Yank the last killed rectangle with upper left corner at point."
248 (insert-rectangle killed-rectangle))
251 ;; This function is untouched --dv
253 (defun insert-rectangle (rectangle)
254 "Insert text of RECTANGLE with upper left corner at point.
255 RECTANGLE's first line is inserted at point, its second
256 line is inserted at a point vertically under point, etc.
257 RECTANGLE should be a list of strings.
258 After this command, the mark is at the upper left corner
259 and point is at the lower right corner."
260 (let ((lines rectangle)
261 (insertcolumn (current-column))
268 (or (bolp) (insert ?\n))
269 (move-to-column insertcolumn t)))
272 (setq lines (cdr lines)))))
275 (defun open-rectangle-line (startcol endcol fill)
276 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
277 (unless (and (not fill)
278 (= (point) (point-at-eol)))
279 (indent-to endcol))))
282 (defun open-rectangle (start end &optional fill)
283 "Blank out the region-rectangle, shifting text right.
285 When called from a program, the rectangle's corners are START and END.
286 With a prefix (or FILL) argument, fill with blanks even if there is no text
287 on the right side of the rectangle."
288 (interactive "*r\nP")
289 (apply-on-rectangle 'open-rectangle-line start end fill)
293 (defun string-rectangle-line (startcol endcol string delete)
294 (move-to-column startcol t)
296 (delete-rectangle-line startcol endcol nil))
300 (defun string-rectangle (start end string)
301 "Insert STRING on each line of the region-rectangle, shifting text right.
302 The left edge of the rectangle specifies the column for insertion.
304 If `pending-delete-mode' is active the string replace the region.
305 Otherwise this command does not delete or overwrite any existing text.
307 When called from a program, the rectangle's corners are START and END."
308 (interactive "*r\nsString rectangle: ")
309 (defvar pending-delete-mode)
310 (apply-on-rectangle 'string-rectangle-line start end string
311 (and (boundp 'pending-delete-mode) pending-delete-mode)))
314 (defun replace-rectangle (start end string)
315 "Like `string-rectangle', but unconditionally replace the original region,
316 as if `pending-delete-mode' were active."
317 (interactive "*r\nsString rectangle: ")
318 (apply-on-rectangle 'string-rectangle-line start end string t))
321 (defun clear-rectangle-line (startcol endcol fill)
322 (let ((pt (point-at-eol))
324 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
326 (<= (save-excursion (goto-char pt) (current-column)) endcol))
327 (delete-region (point) pt)
330 (move-to-column endcol t)
331 (setq spaces (- (point) pt))
332 (delete-region pt (point))
333 (indent-to (+ (current-column) spaces))))
337 (defun clear-rectangle (start end &optional fill)
338 "Blank out the region-rectangle.
339 The text previously in the region is overwritten with blanks.
341 When called from a program, the rectangle's corners are START and END.
342 With a prefix (or FILL) argument, also fill with blanks the parts of the
343 rectangle which were empty."
344 (interactive "*r\nP")
345 (apply-on-rectangle 'clear-rectangle-line start end fill))
350 ;;; rect.el ends here