Instructions to create pretest or release tarballs.
[emacs.git] / lisp / rect.el
blobc4a47b5b15d10e2effd7f9b16efd94378b40ba97
1 ;;; rect.el --- rectangle functions for GNU Emacs
3 ;; Copyright (C) 1985, 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Maintainer: Didier Verna <didier@xemacs.org>
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This package provides the operations on rectangles that are documented
28 ;; in the Emacs manual.
30 ;; ### NOTE: this file has been almost completely rewritten by Didier Verna
31 ;; <didier@xemacs.org> in July 1999. 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.
36 ;;; Code:
38 ;;;###autoload
39 (defun move-to-column-force (column &optional flag)
40 "Move point to column COLUMN rigidly in the current line.
41 If COLUMN is within a multi-column character, replace it by
42 spaces and tab.
44 As for `move-to-column', passing anything but nil or t in FLAG will move to
45 the desired column only if the line is long enough."
46 (let ((col (move-to-column column (or flag t))))
47 (if (> col column)
48 (let (pos)
49 (delete-char -1)
50 (insert-char ? (- column (current-column)))
51 (setq pos (point))
52 (indent-to col)
53 (goto-char pos)))
54 column))
56 ;; not used any more --dv
57 ;; extract-rectangle-line stores lines into this list
58 ;; to accumulate them for extract-rectangle and delete-extract-rectangle.
59 (defvar operate-on-rectangle-lines)
61 ;; ### NOTE: this function is untouched, but not used anymore apart from
62 ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
63 (defun operate-on-rectangle (function start end coerce-tabs)
64 "Call FUNCTION for each line of rectangle with corners at START, END.
65 If COERCE-TABS is non-nil, convert multi-column characters
66 that span the starting or ending columns on any line
67 to multiple spaces before calling FUNCTION.
68 FUNCTION is called with three arguments:
69 position of start of segment of this line within the rectangle,
70 number of columns that belong to rectangle but are before that position,
71 number of columns that belong to rectangle but are after point.
72 Point is at the end of the segment of this line within the rectangle."
73 (let (startcol startlinepos endcol endlinepos)
74 (save-excursion
75 (goto-char start)
76 (setq startcol (current-column))
77 (beginning-of-line)
78 (setq startlinepos (point)))
79 (save-excursion
80 (goto-char end)
81 (setq endcol (current-column))
82 (forward-line 1)
83 (setq endlinepos (point-marker)))
84 (if (< endcol startcol)
85 (setq startcol (prog1 endcol (setq endcol startcol))))
86 (save-excursion
87 (goto-char startlinepos)
88 (while (< (point) endlinepos)
89 (let (startpos begextra endextra)
90 (if coerce-tabs
91 (move-to-column-force startcol)
92 (move-to-column startcol))
93 (setq begextra (- (current-column) startcol))
94 (setq startpos (point))
95 (if coerce-tabs
96 (move-to-column-force endcol)
97 (move-to-column endcol))
98 ;; If we overshot, move back one character
99 ;; so that endextra will be positive.
100 (if (and (not coerce-tabs) (> (current-column) endcol))
101 (backward-char 1))
102 (setq endextra (- endcol (current-column)))
103 (if (< begextra 0)
104 (setq endextra (+ endextra begextra)
105 begextra 0))
106 (funcall function startpos begextra endextra))
107 (forward-line 1)))
108 (- endcol startcol)))
110 ;; The replacement for `operate-on-rectangle' -- dv
111 (defun apply-on-rectangle (function start end &rest args)
112 "Call FUNCTION for each line of rectangle with corners at START, END.
113 FUNCTION is called with two arguments: the start and end columns of the
114 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
115 the function is called."
116 (let (startcol startpt endcol endpt)
117 (save-excursion
118 (goto-char start)
119 (setq startcol (current-column))
120 (beginning-of-line)
121 (setq startpt (point))
122 (goto-char end)
123 (setq endcol (current-column))
124 (forward-line 1)
125 (setq endpt (point-marker))
126 ;; ensure the start column is the left one.
127 (if (< endcol startcol)
128 (let ((col startcol))
129 (setq startcol endcol endcol col)))
130 ;; start looping over lines
131 (goto-char startpt)
132 (while (< (point) endpt)
133 (apply function startcol endcol args)
134 (forward-line 1)))
137 (defun delete-rectangle-line (startcol endcol fill)
138 (let ((pt (line-end-position)))
139 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
140 (setq pt (point))
141 (move-to-column-force endcol)
142 (delete-region pt (point)))))
144 (defun delete-extract-rectangle-line (startcol endcol lines fill)
145 (let ((pt (point-at-eol)))
146 (if (< (move-to-column-force startcol (or fill 'coerce)) startcol)
147 (setcdr lines (cons (spaces-string (- endcol startcol))
148 (cdr lines)))
149 ;; else
150 (setq pt (point))
151 (move-to-column-force endcol)
152 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
153 (delete-region pt (point)))
156 ;; ### NOTE: this is actually the only function that needs to do complicated
157 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
158 ;; might be read-only. --dv
159 (defun extract-rectangle-line (startcol endcol lines)
160 (let (start end begextra endextra line)
161 (move-to-column startcol)
162 (setq start (point)
163 begextra (- (current-column) startcol))
164 (move-to-column endcol)
165 (setq end (point)
166 endextra (- endcol (current-column)))
167 (setq line (buffer-substring start (point)))
168 (if (< begextra 0)
169 (setq endextra (+ endextra begextra)
170 begextra 0))
171 (if (< endextra 0)
172 (setq endextra 0))
173 (goto-char start)
174 (while (search-forward "\t" end t)
175 (let ((width (- (current-column)
176 (save-excursion (forward-char -1)
177 (current-column)))))
178 (setq line (concat (substring line 0 (- (point) end 1))
179 (spaces-string width)
180 (substring line (+ (length line)
181 (- (point) end)))))))
182 (if (or (> begextra 0) (> endextra 0))
183 (setq line (concat (spaces-string begextra)
184 line
185 (spaces-string endextra))))
186 (setcdr lines (cons line (cdr lines)))))
188 (defconst spaces-strings
189 '["" " " " " " " " " " " " " " " " "])
191 ;; this one is untouched --dv
192 (defun spaces-string (n)
193 (if (<= n 8) (aref spaces-strings n)
194 (let ((val ""))
195 (while (> n 8)
196 (setq val (concat " " val)
197 n (- n 8)))
198 (concat val (aref spaces-strings n)))))
200 ;;;###autoload
201 (defun delete-rectangle (start end &optional fill)
202 "Delete (don't save) text in the region-rectangle.
203 The same range of columns is deleted in each line starting with the
204 line where the region begins and ending with the line where the region
205 ends.
207 When called from a program the rectangle's corners are START and END.
208 With a prefix (or a FILL) argument, also fill lines where nothing has
209 to be deleted."
210 (interactive "*r\nP")
211 (apply-on-rectangle 'delete-rectangle-line start end fill))
213 ;;;###autoload
214 (defun delete-extract-rectangle (start end &optional fill)
215 "Delete the contents of the rectangle with corners at START and END.
216 Return it as a list of strings, one for each line of the rectangle.
218 When called from a program the rectangle's corners are START and END.
219 With an optional FILL argument, also fill lines where nothing has to be
220 deleted."
221 (let ((lines (list nil)))
222 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
223 (nreverse (cdr lines))))
225 ;;;###autoload
226 (defun extract-rectangle (start end)
227 "Return the contents of the rectangle with corners at START and END.
228 Return it as a list of strings, one for each line of the rectangle."
229 (let ((lines (list nil)))
230 (apply-on-rectangle 'extract-rectangle-line start end lines)
231 (nreverse (cdr lines))))
233 (defvar killed-rectangle nil
234 "Rectangle for `yank-rectangle' to insert.")
236 ;;;###autoload
237 (defun kill-rectangle (start end &optional fill)
238 "Delete the region-rectangle and save it as the last killed one.
240 When called from a program the rectangle's corners are START and END.
241 You might prefer to use `delete-extract-rectangle' from a program.
243 With a prefix (or a FILL) argument, also fill lines where nothing has to be
244 deleted."
245 (interactive "*r\nP")
246 (when buffer-read-only
247 (setq killed-rectangle (extract-rectangle start end))
248 (barf-if-buffer-read-only))
249 (setq killed-rectangle (delete-extract-rectangle start end fill)))
251 ;; this one is untouched --dv
252 ;;;###autoload
253 (defun yank-rectangle ()
254 "Yank the last killed rectangle with upper left corner at point."
255 (interactive "*")
256 (insert-rectangle killed-rectangle))
258 ;; this one is untoutched --dv
259 ;;;###autoload
260 (defun insert-rectangle (rectangle)
261 "Insert text of RECTANGLE with upper left corner at point.
262 RECTANGLE's first line is inserted at point, its second
263 line is inserted at a point vertically under point, etc.
264 RECTANGLE should be a list of strings.
265 After this command, the mark is at the upper left corner
266 and point is at the lower right corner."
267 (let ((lines rectangle)
268 (insertcolumn (current-column))
269 (first t))
270 (push-mark)
271 (while lines
272 (or first
273 (progn
274 (forward-line 1)
275 (or (bolp) (insert ?\n))
276 (move-to-column-force insertcolumn)))
277 (setq first nil)
278 (insert (car lines))
279 (setq lines (cdr lines)))))
281 ;;;###autoload
282 (defun open-rectangle (start end &optional fill)
283 "Blank out the region-rectangle, shifting text right.
285 The text previously in the region is not overwritten by the blanks,
286 but instead winds up to the right of the rectangle.
288 When called from a program the rectangle's corners are START and END.
289 With a prefix (or a FILL) argument, fill with blanks even if there is no text
290 on the right side of the rectangle."
291 (interactive "*r\nP")
292 (apply-on-rectangle 'open-rectangle-line start end fill)
293 (goto-char start))
295 (defun open-rectangle-line (startcol endcol fill)
296 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
297 (unless (and (not fill)
298 (= (point) (point-at-eol)))
299 (indent-to endcol))))
301 (defun delete-whitespace-rectangle-line (startcol endcol fill)
302 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
303 (unless (= (point) (point-at-eol))
304 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
306 ;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
307 ;;;###autoload
308 (defun delete-whitespace-rectangle (start end &optional fill)
309 "Delete all whitespace following a specified column in each line.
310 The left edge of the rectangle specifies the position in each line
311 at which whitespace deletion should begin. On each line in the
312 rectangle, all continuous whitespace starting at that column is deleted.
314 When called from a program the rectangle's corners are START and END.
315 With a prefix (or a FILL) argument, also fill too short lines."
316 (interactive "*r\nP")
317 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
319 ;; not used any more --dv
320 ;; string-rectangle uses this variable to pass the string
321 ;; to string-rectangle-line.
322 (defvar string-rectangle-string)
324 (defun string-rectangle-line (startcol endcol string delete)
325 (move-to-column-force startcol)
326 (if delete
327 (delete-rectangle-line startcol endcol nil))
328 (insert string))
330 ;;;###autoload
331 (defun string-rectangle (start end string)
332 "Replace rectangle contents with STRING on each line.
333 The length of STRING need not be the same as the rectangle width.
335 Called from a program, takes three args; START, END and STRING."
336 (interactive "*r\nsString rectangle: ")
337 (apply-on-rectangle 'string-rectangle-line start end string t))
339 (defalias 'replace-rectangle 'string-rectangle)
341 ;;;###autoload
342 (defun string-insert-rectangle (start end string)
343 "Insert STRING on each line of region-rectangle, shifting text right.
345 When called from a program, the rectangle's corners are START and END.
346 The left edge of the rectangle specifies the column for insertion.
347 This command does not delete or overwrite any existing text."
348 (interactive "*r\nsString insert rectangle: ")
349 (apply-on-rectangle 'string-rectangle-line start end string nil))
351 ;;;###autoload
352 (defun clear-rectangle (start end &optional fill)
353 "Blank out the region-rectangle.
354 The text previously in the region is overwritten with blanks.
356 When called from a program the rectangle's corners are START and END.
357 With a prefix (or a FILL) argument, also fill with blanks the parts of the
358 rectangle which were empty."
359 (interactive "*r\nP")
360 (apply-on-rectangle 'clear-rectangle-line start end fill))
362 (defun clear-rectangle-line (startcol endcol fill)
363 (let ((pt (point-at-eol))
364 spaces)
365 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
366 (if (and (not fill)
367 (<= (save-excursion (goto-char pt) (current-column)) endcol))
368 (delete-region (point) pt)
369 ;; else
370 (setq pt (point))
371 (move-to-column-force endcol)
372 (setq spaces (- (point) pt))
373 (delete-region pt (point))
374 (indent-to (+ (current-column) spaces))))))
376 (provide 'rect)
378 ;;; rect.el ends here