gnus-util.el (gnus-macroexpand-all): Fix last change.
[emacs.git] / lisp / rect.el
blob665840899108649048693a6f3f2019b71aab58bb
1 ;;; rect.el --- rectangle functions for GNU Emacs
3 ;; Copyright (C) 1985, 1999, 2000, 2001, 2002, 2003, 2004
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: Didier Verna <didier@xemacs.org>
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
35 ;;; Global key bindings
37 ;;;###autoload (define-key ctl-x-r-map "c" 'clear-rectangle)
38 ;;;###autoload (define-key ctl-x-r-map "k" 'kill-rectangle)
39 ;;;###autoload (define-key ctl-x-r-map "d" 'delete-rectangle)
40 ;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle)
41 ;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle)
42 ;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle)
44 ;;; Code:
46 ;;;###autoload
47 (defun move-to-column-force (column &optional flag)
48 "If COLUMN is within a multi-column character, replace it by spaces and tab.
49 As for `move-to-column', passing anything but nil or t in FLAG will move to
50 the desired column only if the line is long enough."
51 (move-to-column column (or flag t)))
53 ;;;###autoload
54 (make-obsolete 'move-to-column-force 'move-to-column "21.2")
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 startcol t)
92 (move-to-column startcol))
93 (setq begextra (- (current-column) startcol))
94 (setq startpos (point))
95 (if coerce-tabs
96 (move-to-column endcol t)
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 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
139 (delete-region (point)
140 (progn (move-to-column endcol 'coerce)
141 (point)))))
143 (defun delete-extract-rectangle-line (startcol endcol lines fill)
144 (let ((pt (point-at-eol)))
145 (if (< (move-to-column startcol (if fill t 'coerce)) startcol)
146 (setcdr lines (cons (spaces-string (- endcol startcol))
147 (cdr lines)))
148 ;; else
149 (setq pt (point))
150 (move-to-column endcol t)
151 (setcdr lines (cons (filter-buffer-substring pt (point) t) (cdr lines))))
154 ;; ### NOTE: this is actually the only function that needs to do complicated
155 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
156 ;; might be read-only. --dv
157 (defun extract-rectangle-line (startcol endcol lines)
158 (let (start end begextra endextra line)
159 (move-to-column startcol)
160 (setq start (point)
161 begextra (- (current-column) startcol))
162 (move-to-column endcol)
163 (setq end (point)
164 endextra (- endcol (current-column)))
165 (setq line (buffer-substring start (point)))
166 (if (< begextra 0)
167 (setq endextra (+ endextra begextra)
168 begextra 0))
169 (if (< endextra 0)
170 (setq endextra 0))
171 (goto-char start)
172 (while (search-forward "\t" end t)
173 (let ((width (- (current-column)
174 (save-excursion (forward-char -1)
175 (current-column)))))
176 (setq line (concat (substring line 0 (- (point) end 1))
177 (spaces-string width)
178 (substring line (+ (length line)
179 (- (point) end)))))))
180 (if (or (> begextra 0) (> endextra 0))
181 (setq line (concat (spaces-string begextra)
182 line
183 (spaces-string endextra))))
184 (setcdr lines (cons line (cdr lines)))))
186 (defconst spaces-strings
187 '["" " " " " " " " " " " " " " " " "])
189 ;; this one is untouched --dv
190 (defun spaces-string (n)
191 "Return a string with N spaces."
192 (if (<= n 8) (aref spaces-strings n)
193 (make-string n ?\s)))
195 ;;;###autoload
196 (defun delete-rectangle (start end &optional fill)
197 "Delete (don't save) text in the region-rectangle.
198 The same range of columns is deleted in each line starting with the
199 line where the region begins and ending with the line where the region
200 ends.
202 When called from a program the rectangle's corners are START and END.
203 With a prefix (or a FILL) argument, also fill lines where nothing has
204 to be deleted."
205 (interactive "*r\nP")
206 (apply-on-rectangle 'delete-rectangle-line start end fill))
208 ;;;###autoload
209 (defun delete-extract-rectangle (start end &optional fill)
210 "Delete the contents of the rectangle with corners at START and END.
211 Return it as a list of strings, one for each line of the rectangle.
213 When called from a program the rectangle's corners are START and END.
214 With an optional FILL argument, also fill lines where nothing has to be
215 deleted."
216 (let ((lines (list nil)))
217 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
218 (nreverse (cdr lines))))
220 ;;;###autoload
221 (defun extract-rectangle (start end)
222 "Return the contents of the rectangle with corners at START and END.
223 Return it as a list of strings, one for each line of the rectangle."
224 (let ((lines (list nil)))
225 (apply-on-rectangle 'extract-rectangle-line start end lines)
226 (nreverse (cdr lines))))
228 (defvar killed-rectangle nil
229 "Rectangle for `yank-rectangle' to insert.")
231 ;;;###autoload
232 (defun kill-rectangle (start end &optional fill)
233 "Delete the region-rectangle and save it as the last killed one.
235 When called from a program the rectangle's corners are START and END.
236 You might prefer to use `delete-extract-rectangle' from a program.
238 With a prefix (or a FILL) argument, also fill lines where nothing has to be
239 deleted.
241 If the buffer is read-only, Emacs will beep and refrain from deleting
242 the rectangle, but put it in the kill ring anyway. This means that
243 you can use this command to copy text from a read-only buffer.
244 \(If the variable `kill-read-only-ok' is non-nil, then this won't
245 even beep.)"
246 (interactive "r\nP")
247 (condition-case nil
248 (setq killed-rectangle (delete-extract-rectangle start end fill))
249 ((buffer-read-only text-read-only)
250 (setq killed-rectangle (extract-rectangle start end))
251 (if kill-read-only-ok
252 (progn (message "Read only text copied to kill ring") nil)
253 (barf-if-buffer-read-only)
254 (signal 'text-read-only (list (current-buffer)))))))
256 ;; this one is untouched --dv
257 ;;;###autoload
258 (defun yank-rectangle ()
259 "Yank the last killed rectangle with upper left corner at point."
260 (interactive "*")
261 (insert-rectangle killed-rectangle))
263 ;; this one is untoutched --dv
264 ;;;###autoload
265 (defun insert-rectangle (rectangle)
266 "Insert text of RECTANGLE with upper left corner at point.
267 RECTANGLE's first line is inserted at point, its second
268 line is inserted at a point vertically under point, etc.
269 RECTANGLE should be a list of strings.
270 After this command, the mark is at the upper left corner
271 and point is at the lower right corner."
272 (let ((lines rectangle)
273 (insertcolumn (current-column))
274 (first t))
275 (push-mark)
276 (while lines
277 (or first
278 (progn
279 (forward-line 1)
280 (or (bolp) (insert ?\n))
281 (move-to-column insertcolumn t)))
282 (setq first nil)
283 (insert-for-yank (car lines))
284 (setq lines (cdr lines)))))
286 ;;;###autoload
287 (defun open-rectangle (start end &optional fill)
288 "Blank out the region-rectangle, shifting text right.
290 The text previously in the region is not overwritten by the blanks,
291 but instead winds up to the right of the rectangle.
293 When called from a program the rectangle's corners are START and END.
294 With a prefix (or a FILL) argument, fill with blanks even if there is
295 no text on the right side of the rectangle."
296 (interactive "*r\nP")
297 (apply-on-rectangle 'open-rectangle-line start end fill)
298 (goto-char start))
300 (defun open-rectangle-line (startcol endcol fill)
301 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
302 (unless (and (not fill)
303 (= (point) (point-at-eol)))
304 (indent-to endcol))))
306 (defun delete-whitespace-rectangle-line (startcol endcol fill)
307 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
308 (unless (= (point) (point-at-eol))
309 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
311 ;;;###autoload
312 (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
314 ;;;###autoload
315 (defun delete-whitespace-rectangle (start end &optional fill)
316 "Delete all whitespace following a specified column in each line.
317 The left edge of the rectangle specifies the position in each line
318 at which whitespace deletion should begin. On each line in the
319 rectangle, all continuous whitespace starting at that column is deleted.
321 When called from a program the rectangle's corners are START and END.
322 With a prefix (or a FILL) argument, also fill too short lines."
323 (interactive "*r\nP")
324 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
326 ;; not used any more --dv
327 ;; string-rectangle uses this variable to pass the string
328 ;; to string-rectangle-line.
329 (defvar string-rectangle-string)
330 (defvar string-rectangle-history nil)
331 (defun string-rectangle-line (startcol endcol string delete)
332 (move-to-column startcol t)
333 (if delete
334 (delete-rectangle-line startcol endcol nil))
335 (insert string))
337 ;;;###autoload
338 (defun string-rectangle (start end string)
339 "Replace rectangle contents with STRING on each line.
340 The length of STRING need not be the same as the rectangle width.
342 Called from a program, takes three args; START, END and STRING."
343 (interactive
344 (progn (barf-if-buffer-read-only)
345 (list
346 (region-beginning)
347 (region-end)
348 (read-string (format "String rectangle (default %s): "
349 (or (car string-rectangle-history) ""))
350 nil 'string-rectangle-history
351 (car string-rectangle-history)))))
352 (apply-on-rectangle 'string-rectangle-line start end string t))
354 ;;;###autoload
355 (defalias 'replace-rectangle 'string-rectangle)
357 ;;;###autoload
358 (defun string-insert-rectangle (start end string)
359 "Insert STRING on each line of region-rectangle, shifting text right.
361 When called from a program, the rectangle's corners are START and END.
362 The left edge of the rectangle specifies the column for insertion.
363 This command does not delete or overwrite any existing text."
364 (interactive
365 (progn (barf-if-buffer-read-only)
366 (list
367 (region-beginning)
368 (region-end)
369 (read-string (format "String insert rectangle (default %s): "
370 (or (car string-rectangle-history) ""))
371 nil 'string-rectangle-history
372 (car string-rectangle-history)))))
373 (apply-on-rectangle 'string-rectangle-line start end string nil))
375 ;;;###autoload
376 (defun clear-rectangle (start end &optional fill)
377 "Blank out the region-rectangle.
378 The text previously in the region is overwritten with blanks.
380 When called from a program the rectangle's corners are START and END.
381 With a prefix (or a FILL) argument, also fill with blanks the parts of the
382 rectangle which were empty."
383 (interactive "*r\nP")
384 (apply-on-rectangle 'clear-rectangle-line start end fill))
386 (defun clear-rectangle-line (startcol endcol fill)
387 (let ((pt (point-at-eol)))
388 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
389 (if (and (not fill)
390 (<= (save-excursion (goto-char pt) (current-column)) endcol))
391 (delete-region (point) pt)
392 ;; else
393 (setq pt (point))
394 (move-to-column endcol t)
395 (setq endcol (current-column))
396 (delete-region pt (point))
397 (indent-to endcol)))))
399 (provide 'rect)
401 ;; arch-tag: 178847b3-1f50-4b03-83de-a6e911cc1d16
402 ;;; rect.el ends here