This commit was generated by cvs2svn to compensate for changes in r410,
[chise/xemacs-chise.git-] / lisp / rect.el
1 ;;; rect.el --- rectangle functions for XEmacs.
2
3 ;; Copyright (C) 1985, 1993, 1994, 1999 Free Software Foundation, Inc.
4
5 ;; Maintainer: Didier Verna <verna@inf.enst.fr>
6 ;; Keywords: internal
7
8 ;; This file is part of XEmacs.
9
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)
13 ;; any later version.
14
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.
19
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
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: to be incorporated in a forthcoming GNU Emacs
26
27 ;;; Commentary:
28
29 ;; This package provides the operations on rectangles that are ocumented
30 ;; in the XEmacs Reference Manual.
31
32 ;; ### NOTE: this file has been almost completely rewritten by Didier Verna
33 ;; <verna@inf.enst.fr>, 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.
36
37
38 ;;; Code:
39
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
43 ;; touch it. --dv
44 ;; XEmacs: extra-args
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)
56     (save-excursion
57       (goto-char start)
58       (setq startcol (current-column))
59       (beginning-of-line)
60       (setq startlinepos (point)))
61     (save-excursion
62       (goto-char end)
63       (setq endcol (current-column))
64       (forward-line 1)
65       (setq endlinepos (point-marker)))
66     (if (< endcol startcol)
67         ;; XEmacs
68         (let ((tem startcol))
69           (setq startcol endcol endcol tem)))
70     (save-excursion
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)))
79           (if (< begextra 0)
80               (setq endextra (+ endextra begextra)
81                     begextra 0))
82           (if (< endextra 0) (setq endextra 0))
83           (apply function startpos begextra endextra extra-args))
84         (forward-line 1)))
85     (- endcol startcol)))
86
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, 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 when
92 the function is called."
93   (let (startcol startpt endcol endpt)
94     (save-excursion
95       (goto-char start)
96       (setq startcol (current-column))
97       (beginning-of-line)
98       (setq startpt (point))
99       (goto-char end)
100       (setq endcol (current-column))
101       (forward-line 1)
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
108       (goto-char startpt)
109       (while (< (point) endpt)
110         (apply function startcol endcol args)
111         (forward-line 1)))
112     ))
113
114 ;; I love ascii art ;-)
115 (defconst spaces-strings '[""
116                            " "
117                            "  "
118                            "   "
119                            "    "
120                            "     "
121                            "      "
122                            "       "
123                            "        "])
124
125
126 ;; This function is untouched --dv
127 (defun spaces-string (n)
128   (if (<= n 8) (aref spaces-strings n)
129     (let ((val ""))
130       (while (> n 8)
131         (setq val (concat "        " val)
132               n (- n 8)))
133       (concat val (aref spaces-strings n)))))
134
135 ;;;###autoload
136 (defvar killed-rectangle nil
137   "Rectangle for yank-rectangle to insert.")
138
139 ;;;###autoload
140 (defun kill-rectangle (start end &optional fill)
141   "Delete the rectangle with corners at point and mark (START and END when
142 called from a program) and save it as the last killed one. You might prefer to
143 use `delete-extract-rectangle' from a program.
144
145 With a prefix (or a FILL) argument, also fill lines where nothing has to be
146 deleted."
147   (interactive "r\nP")
148   (when buffer-read-only
149     (setq killed-rectangle (extract-rectangle start end))
150     (barf-if-buffer-read-only))
151   (setq killed-rectangle (delete-extract-rectangle start end fill)))
152
153 ;;;###autoload
154 (defun delete-rectangle (start end &optional fill)
155   "Delete (don't save) text in rectangle with corners at point and mark (START
156 and END when called from a program). The same range of columns is deleted in
157 each line starting with the line where the region begins and ending with the
158 line where the region ends.
159
160 With a prefix (or a FILL) argument, also fill lines where nothing has to be
161 deleted."
162   (interactive "r\nP")
163   (apply-on-rectangle 'delete-rectangle-line start end fill))
164
165 (defun delete-rectangle-line (startcol endcol fill)
166   (let ((pt (point-at-eol)))
167     (when (= (move-to-column startcol (or fill 'coerce)) startcol)
168       (if (and (not fill) (<= pt endcol))
169           (delete-region (point) pt)
170         ;; else
171         (setq pt (point))
172         (move-to-column endcol t)
173         (delete-region pt (point))))
174     ))
175
176 ;;;###autoload
177 (defun delete-extract-rectangle (start end &optional fill)
178   "Delete the contents of the rectangle with corners at START and END, and
179 return it as a list of strings, one for each line of the rectangle.
180
181 With an optional FILL argument, also fill lines where nothing has to be
182 deleted."
183   (let ((lines (list nil)))
184     (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
185     (nreverse (cdr lines))))
186
187 (defun delete-extract-rectangle-line (startcol endcol lines fill)
188   (let ((pt (point-at-eol)))
189     (if (< (move-to-column startcol (or fill 'coerce)) startcol)
190         (setcdr lines (cons (spaces-string (- endcol startcol))
191                             (cdr lines)))
192       ;; else
193       (setq pt (point))
194       (move-to-column endcol t)
195       (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
196       (delete-region pt (point)))
197     ))
198
199 ;;;###autoload
200 (defun extract-rectangle (start end)
201   "Return the contents of the rectangle with corners at START and END,
202 as a list of strings, one for each line of the rectangle."
203   (let ((lines (list nil)))
204     (apply-on-rectangle 'extract-rectangle-line start end lines)
205     (nreverse (cdr lines))))
206
207 ;; ### NOTE: this is actually the only function that needs to do complicated
208 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
209 ;; might be read-only. --dv
210 (defun extract-rectangle-line (startcol endcol lines)
211   (let (start end begextra endextra line)
212     (move-to-column startcol)
213     (setq start (point)
214           begextra (- (current-column) startcol))
215     (move-to-column endcol)
216     (setq end (point)
217           endextra (- endcol (current-column)))
218     (setq line (buffer-substring start (point)))
219     (if (< begextra 0)
220         (setq endextra (+ endextra begextra)
221               begextra 0))
222     (if (< endextra 0)
223         (setq endextra 0))
224     (goto-char start)
225     (while (search-forward "\t" end t)
226       (let ((width (- (current-column)
227                       (save-excursion (forward-char -1)
228                                       (current-column)))))
229         (setq line (concat (substring line 0 (- (point) end 1))
230                            (spaces-string width)
231                            (substring line (+ (length line)
232                                               (- (point) end)))))))
233     (if (or (> begextra 0) (> endextra 0))
234         (setq line (concat (spaces-string begextra)
235                            line
236                            (spaces-string endextra))))
237     (setcdr lines (cons line (cdr lines)))))
238
239 ;; This function is untouched --dv
240 ;;;###autoload
241 (defun yank-rectangle ()
242   "Yank the last killed rectangle with upper left corner at point."
243   (interactive)
244   (insert-rectangle killed-rectangle))
245
246 ;; This function is untouched --dv
247 ;;;###autoload
248 (defun insert-rectangle (rectangle)
249   "Insert text of RECTANGLE with upper left corner at point.
250 RECTANGLE's first line is inserted at point, its second
251 line is inserted at a point vertically under point, etc.
252 RECTANGLE should be a list of strings.
253 After this command, the mark is at the upper left corner
254 and point is at the lower right corner."
255   (let ((lines rectangle)
256         (insertcolumn (current-column))
257         (first t))
258     (push-mark)
259     (while lines
260       (or first
261           (progn
262             (forward-line 1)
263             (or (bolp) (insert ?\n))
264             (move-to-column insertcolumn t)))
265       (setq first nil)
266       (insert (car lines))
267       (setq lines (cdr lines)))))
268
269 ;;;###autoload
270 (defun open-rectangle (start end &optional fill)
271   "Blank out rectangle with corners at point and mark (START and END when
272 called from a program), shifting text right. The text previously in the region
273 is not overwritten by the blanks, but instead winds up to the right of the
274 rectangle.
275
276 With a prefix (or a FILL) argument, fill with blanks even if there is no text
277 on the right side of the rectangle."
278   (interactive "r\nP")
279   (apply-on-rectangle 'open-rectangle-line start end fill)
280   (goto-char start))
281
282 (defun open-rectangle-line (startcol endcol fill)
283   (let (spaces)
284     (when (= (move-to-column startcol (or fill 'coerce)) startcol)
285       (unless (and (not fill)
286                    (= (point) (point-at-eol)))
287         (indent-to endcol)))
288     ))
289
290 ;;;###autoload
291 (defun string-rectangle (start end string)
292   "Insert STRING on each line of the rectangle with corners at point and mark
293 (START and END when called from a program), shifting text right. The left edge
294 of the rectangle specifies the column for insertion. This command does not
295 delete or overwrite any existing text."
296   (interactive "r\nsString rectangle: ")
297   (apply-on-rectangle 'string-rectangle-line start end string))
298
299 (defun string-rectangle-line (startcol endcol string)
300   (move-to-column startcol t)
301   (insert string))
302
303 ;;;###autoload
304 (defun clear-rectangle (start end &optional fill)
305   "Blank out the rectangle with corners at point and mark (START and END when
306 called from a program). The text previously in the region is overwritten with
307 blanks.
308
309 With a prefix (or a FILL) argument, also fill with blanks the parts of the
310 rectangle which were empty."
311   (interactive "r\nP")
312   (apply-on-rectangle 'clear-rectangle-line start end fill))
313
314 (defun clear-rectangle-line (startcol endcol fill)
315   (let ((pt (point-at-eol))
316         spaces)
317     (when (= (move-to-column startcol (or fill 'coerce)) startcol)
318       (if (and (not fill)
319                (<= (save-excursion (goto-char pt) (current-column)) endcol))
320           (delete-region (point) pt)
321         ;; else
322         (setq pt (point))
323         (move-to-column endcol t)
324         (setq spaces (- (point) pt))
325         (delete-region pt (point))
326         (indent-to (+ (current-column) spaces))))
327     ))
328
329 (provide 'rect)
330
331 ;;; rect.el ends here