]> code.delx.au - gnu-emacs/blob - lisp/rect.el
(normal-splash-screen, fancy-splash-screens-1): Add a reference to the Lisp
[gnu-emacs] / lisp / rect.el
1 ;;; rect.el --- rectangle functions for GNU Emacs
2
3 ;; Copyright (C) 1985, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Maintainer: Didier Verna <didier@xemacs.org>
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; This package provides the operations on rectangles that are documented
29 ;; in the Emacs manual.
30
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.
35
36
37 ;;; Code:
38
39 ;;;###autoload
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)))
45
46 ;;;###autoload
47 (make-obsolete 'move-to-column-force 'move-to-column "21.2")
48
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)
53
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)
67 (save-excursion
68 (goto-char start)
69 (setq startcol (current-column))
70 (beginning-of-line)
71 (setq startlinepos (point)))
72 (save-excursion
73 (goto-char end)
74 (setq endcol (current-column))
75 (forward-line 1)
76 (setq endlinepos (point-marker)))
77 (if (< endcol startcol)
78 (setq startcol (prog1 endcol (setq endcol startcol))))
79 (save-excursion
80 (goto-char startlinepos)
81 (while (< (point) endlinepos)
82 (let (startpos begextra endextra)
83 (if coerce-tabs
84 (move-to-column startcol t)
85 (move-to-column startcol))
86 (setq begextra (- (current-column) startcol))
87 (setq startpos (point))
88 (if coerce-tabs
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))
94 (backward-char 1))
95 (setq endextra (- endcol (current-column)))
96 (if (< begextra 0)
97 (setq endextra (+ endextra begextra)
98 begextra 0))
99 (funcall function startpos begextra endextra))
100 (forward-line 1)))
101 (- endcol startcol)))
102
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)
110 (save-excursion
111 (goto-char start)
112 (setq startcol (current-column))
113 (beginning-of-line)
114 (setq startpt (point))
115 (goto-char end)
116 (setq endcol (current-column))
117 (forward-line 1)
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
124 (goto-char startpt)
125 (while (< (point) endpt)
126 (apply function startcol endcol args)
127 (forward-line 1)))
128 ))
129
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)
134 (point)))))
135
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))
140 (cdr lines)))
141 ;; else
142 (setq pt (point))
143 (move-to-column endcol t)
144 (setcdr lines (cons (filter-buffer-substring pt (point) t) (cdr lines))))
145 ))
146
147 ;; ### NOTE: this is actually the only function that needs to do complicated
148 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
149 ;; might be read-only. --dv
150 (defun extract-rectangle-line (startcol endcol lines)
151 (let (start end begextra endextra line)
152 (move-to-column startcol)
153 (setq start (point)
154 begextra (- (current-column) startcol))
155 (move-to-column endcol)
156 (setq end (point)
157 endextra (- endcol (current-column)))
158 (setq line (buffer-substring start (point)))
159 (if (< begextra 0)
160 (setq endextra (+ endextra begextra)
161 begextra 0))
162 (if (< endextra 0)
163 (setq endextra 0))
164 (goto-char start)
165 (while (search-forward "\t" end t)
166 (let ((width (- (current-column)
167 (save-excursion (forward-char -1)
168 (current-column)))))
169 (setq line (concat (substring line 0 (- (point) end 1))
170 (spaces-string width)
171 (substring line (+ (length line)
172 (- (point) end)))))))
173 (if (or (> begextra 0) (> endextra 0))
174 (setq line (concat (spaces-string begextra)
175 line
176 (spaces-string endextra))))
177 (setcdr lines (cons line (cdr lines)))))
178
179 (defconst spaces-strings
180 '["" " " " " " " " " " " " " " " " "])
181
182 ;; this one is untouched --dv
183 (defun spaces-string (n)
184 (if (<= n 8) (aref spaces-strings n)
185 (let ((val ""))
186 (while (> n 8)
187 (setq val (concat " " val)
188 n (- n 8)))
189 (concat val (aref spaces-strings n)))))
190
191 ;;;###autoload
192 (defun delete-rectangle (start end &optional fill)
193 "Delete (don't save) text in the region-rectangle.
194 The same range of columns is deleted in each line starting with the
195 line where the region begins and ending with the line where the region
196 ends.
197
198 When called from a program the rectangle's corners are START and END.
199 With a prefix (or a FILL) argument, also fill lines where nothing has
200 to be deleted."
201 (interactive "*r\nP")
202 (apply-on-rectangle 'delete-rectangle-line start end fill))
203
204 ;;;###autoload
205 (defun delete-extract-rectangle (start end &optional fill)
206 "Delete the contents of the rectangle with corners at START and END.
207 Return it as a list of strings, one for each line of the rectangle.
208
209 When called from a program the rectangle's corners are START and END.
210 With an optional FILL argument, also fill lines where nothing has to be
211 deleted."
212 (let ((lines (list nil)))
213 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
214 (nreverse (cdr lines))))
215
216 ;;;###autoload
217 (defun extract-rectangle (start end)
218 "Return the contents of the rectangle with corners at START and END.
219 Return it 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))))
223
224 (defvar killed-rectangle nil
225 "Rectangle for `yank-rectangle' to insert.")
226
227 ;;;###autoload
228 (defun kill-rectangle (start end &optional fill)
229 "Delete the region-rectangle and save it as the last killed one.
230
231 When called from a program the rectangle's corners are START and END.
232 You might prefer to use `delete-extract-rectangle' from a program.
233
234 With a prefix (or a FILL) argument, also fill lines where nothing has to be
235 deleted.
236
237 If the buffer is read-only, Emacs will beep and refrain from deleting
238 the rectangle, but put it in the kill ring anyway. This means that
239 you can use this command to copy text from a read-only buffer.
240 \(If the variable `kill-read-only-ok' is non-nil, then this won't
241 even beep.)"
242 (interactive "r\nP")
243 (condition-case nil
244 (setq killed-rectangle (delete-extract-rectangle start end fill))
245 ((buffer-read-only text-read-only)
246 (setq killed-rectangle (extract-rectangle start end))
247 (if kill-read-only-ok
248 (progn (message "Read only text copied to kill ring") nil)
249 (barf-if-buffer-read-only)
250 (signal 'text-read-only (list (current-buffer)))))))
251
252 ;; this one is untouched --dv
253 ;;;###autoload
254 (defun yank-rectangle ()
255 "Yank the last killed rectangle with upper left corner at point."
256 (interactive "*")
257 (insert-rectangle killed-rectangle))
258
259 ;; this one is untoutched --dv
260 ;;;###autoload
261 (defun insert-rectangle (rectangle)
262 "Insert text of RECTANGLE with upper left corner at point.
263 RECTANGLE's first line is inserted at point, its second
264 line is inserted at a point vertically under point, etc.
265 RECTANGLE should be a list of strings.
266 After this command, the mark is at the upper left corner
267 and point is at the lower right corner."
268 (let ((lines rectangle)
269 (insertcolumn (current-column))
270 (first t))
271 (push-mark)
272 (while lines
273 (or first
274 (progn
275 (forward-line 1)
276 (or (bolp) (insert ?\n))
277 (move-to-column insertcolumn t)))
278 (setq first nil)
279 (insert-for-yank (car lines))
280 (setq lines (cdr lines)))))
281
282 ;;;###autoload
283 (defun open-rectangle (start end &optional fill)
284 "Blank out the region-rectangle, shifting text right.
285
286 The text previously in the region is not overwritten by the blanks,
287 but instead winds up to the right of the rectangle.
288
289 When called from a program the rectangle's corners are START and END.
290 With a prefix (or a FILL) argument, fill with blanks even if there is no text
291 on the right side of the rectangle."
292 (interactive "*r\nP")
293 (apply-on-rectangle 'open-rectangle-line start end fill)
294 (goto-char start))
295
296 (defun open-rectangle-line (startcol endcol fill)
297 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
298 (unless (and (not fill)
299 (= (point) (point-at-eol)))
300 (indent-to endcol))))
301
302 (defun delete-whitespace-rectangle-line (startcol endcol fill)
303 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
304 (unless (= (point) (point-at-eol))
305 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
306
307 ;;;###autoload
308 (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
309
310 ;;;###autoload
311 (defun delete-whitespace-rectangle (start end &optional fill)
312 "Delete all whitespace following a specified column in each line.
313 The left edge of the rectangle specifies the position in each line
314 at which whitespace deletion should begin. On each line in the
315 rectangle, all continuous whitespace starting at that column is deleted.
316
317 When called from a program the rectangle's corners are START and END.
318 With a prefix (or a FILL) argument, also fill too short lines."
319 (interactive "*r\nP")
320 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
321
322 ;; not used any more --dv
323 ;; string-rectangle uses this variable to pass the string
324 ;; to string-rectangle-line.
325 (defvar string-rectangle-string)
326 (defvar string-rectangle-history nil)
327 (defun string-rectangle-line (startcol endcol string delete)
328 (move-to-column startcol t)
329 (if delete
330 (delete-rectangle-line startcol endcol nil))
331 (insert string))
332
333 ;;;###autoload
334 (defun string-rectangle (start end string)
335 "Replace rectangle contents with STRING on each line.
336 The length of STRING need not be the same as the rectangle width.
337
338 Called from a program, takes three args; START, END and STRING."
339 (interactive
340 (progn (barf-if-buffer-read-only)
341 (list
342 (region-beginning)
343 (region-end)
344 (read-string (format "String rectangle (default %s): "
345 (or (car string-rectangle-history) ""))
346 nil 'string-rectangle-history
347 (car string-rectangle-history)))))
348 (apply-on-rectangle 'string-rectangle-line start end string t))
349
350 ;;;###autoload
351 (defalias 'replace-rectangle 'string-rectangle)
352
353 ;;;###autoload
354 (defun string-insert-rectangle (start end string)
355 "Insert STRING on each line of region-rectangle, shifting text right.
356
357 When called from a program, the rectangle's corners are START and END.
358 The left edge of the rectangle specifies the column for insertion.
359 This command does not delete or overwrite any existing text."
360 (interactive
361 (progn (barf-if-buffer-read-only)
362 (list
363 (region-beginning)
364 (region-end)
365 (read-string (format "String insert rectangle (default %s): "
366 (or (car string-rectangle-history) ""))
367 nil 'string-rectangle-history
368 (car string-rectangle-history)))))
369 (apply-on-rectangle 'string-rectangle-line start end string nil))
370
371 ;;;###autoload
372 (defun clear-rectangle (start end &optional fill)
373 "Blank out the region-rectangle.
374 The text previously in the region is overwritten with blanks.
375
376 When called from a program the rectangle's corners are START and END.
377 With a prefix (or a FILL) argument, also fill with blanks the parts of the
378 rectangle which were empty."
379 (interactive "*r\nP")
380 (apply-on-rectangle 'clear-rectangle-line start end fill))
381
382 (defun clear-rectangle-line (startcol endcol fill)
383 (let ((pt (point-at-eol)))
384 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
385 (if (and (not fill)
386 (<= (save-excursion (goto-char pt) (current-column)) endcol))
387 (delete-region (point) pt)
388 ;; else
389 (setq pt (point))
390 (move-to-column endcol t)
391 (setq endcol (current-column))
392 (delete-region pt (point))
393 (indent-to endcol)))))
394
395 (provide 'rect)
396
397 ;;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
398 ;;; rect.el ends here