Nuke arch-tags.
[emacs.git] / lisp / rect.el
blob1adb95257e9426b89efc378a0d8d6183c3be1818
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, 2011 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 was almost completely rewritten by Didier Verna
31 ;; <didier@xemacs.org> in July 1999.
33 ;;; Global key bindings
35 ;;;###autoload (define-key ctl-x-r-map "c" 'clear-rectangle)
36 ;;;###autoload (define-key ctl-x-r-map "k" 'kill-rectangle)
37 ;;;###autoload (define-key ctl-x-r-map "d" 'delete-rectangle)
38 ;;;###autoload (define-key ctl-x-r-map "y" 'yank-rectangle)
39 ;;;###autoload (define-key ctl-x-r-map "o" 'open-rectangle)
40 ;;;###autoload (define-key ctl-x-r-map "t" 'string-rectangle)
41 ;;;###autoload (define-key ctl-x-r-map "N" 'rectangle-number-lines)
43 ;;; Code:
45 ;; FIXME: this function should be replaced by `apply-on-rectangle'
46 (defun operate-on-rectangle (function start end coerce-tabs)
47 "Call FUNCTION for each line of rectangle with corners at START, END.
48 If COERCE-TABS is non-nil, convert multi-column characters
49 that span the starting or ending columns on any line
50 to multiple spaces before calling FUNCTION.
51 FUNCTION is called with three arguments:
52 position of start of segment of this line within the rectangle,
53 number of columns that belong to rectangle but are before that position,
54 number of columns that belong to rectangle but are after point.
55 Point is at the end of the segment of this line within the rectangle."
56 (let (startcol startlinepos endcol endlinepos)
57 (save-excursion
58 (goto-char start)
59 (setq startcol (current-column))
60 (beginning-of-line)
61 (setq startlinepos (point)))
62 (save-excursion
63 (goto-char end)
64 (setq endcol (current-column))
65 (forward-line 1)
66 (setq endlinepos (point-marker)))
67 (if (< endcol startcol)
68 (setq startcol (prog1 endcol (setq endcol startcol))))
69 (save-excursion
70 (goto-char startlinepos)
71 (while (< (point) endlinepos)
72 (let (startpos begextra endextra)
73 (if coerce-tabs
74 (move-to-column startcol t)
75 (move-to-column startcol))
76 (setq begextra (- (current-column) startcol))
77 (setq startpos (point))
78 (if coerce-tabs
79 (move-to-column endcol t)
80 (move-to-column endcol))
81 ;; If we overshot, move back one character
82 ;; so that endextra will be positive.
83 (if (and (not coerce-tabs) (> (current-column) endcol))
84 (backward-char 1))
85 (setq endextra (- endcol (current-column)))
86 (if (< begextra 0)
87 (setq endextra (+ endextra begextra)
88 begextra 0))
89 (funcall function startpos begextra endextra))
90 (forward-line 1)))
91 (- endcol startcol)))
93 (defun apply-on-rectangle (function start end &rest args)
94 "Call FUNCTION for each line of rectangle with corners at START, END.
95 FUNCTION is called with two arguments: the start and end columns of the
96 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
97 the function is called."
98 (let (startcol startpt endcol endpt)
99 (save-excursion
100 (goto-char start)
101 (setq startcol (current-column))
102 (beginning-of-line)
103 (setq startpt (point))
104 (goto-char end)
105 (setq endcol (current-column))
106 (forward-line 1)
107 (setq endpt (point-marker))
108 ;; ensure the start column is the left one.
109 (if (< endcol startcol)
110 (let ((col startcol))
111 (setq startcol endcol endcol col)))
112 ;; start looping over lines
113 (goto-char startpt)
114 (while (< (point) endpt)
115 (apply function startcol endcol args)
116 (forward-line 1)))
119 (defun delete-rectangle-line (startcol endcol fill)
120 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
121 (delete-region (point)
122 (progn (move-to-column endcol 'coerce)
123 (point)))))
125 (defun delete-extract-rectangle-line (startcol endcol lines fill)
126 (let ((pt (point-at-eol)))
127 (if (< (move-to-column startcol (if fill t 'coerce)) startcol)
128 (setcdr lines (cons (spaces-string (- endcol startcol))
129 (cdr lines)))
130 ;; else
131 (setq pt (point))
132 (move-to-column endcol t)
133 (setcdr lines (cons (filter-buffer-substring pt (point) t) (cdr lines))))
136 ;; This is actually the only function that needs to do complicated
137 ;; stuff like what's happening in `operate-on-rectangle', because the
138 ;; buffer might be read-only.
139 (defun extract-rectangle-line (startcol endcol lines)
140 (let (start end begextra endextra line)
141 (move-to-column startcol)
142 (setq start (point)
143 begextra (- (current-column) startcol))
144 (move-to-column endcol)
145 (setq end (point)
146 endextra (- endcol (current-column)))
147 (setq line (buffer-substring start (point)))
148 (if (< begextra 0)
149 (setq endextra (+ endextra begextra)
150 begextra 0))
151 (if (< endextra 0)
152 (setq endextra 0))
153 (goto-char start)
154 (while (search-forward "\t" end t)
155 (let ((width (- (current-column)
156 (save-excursion (forward-char -1)
157 (current-column)))))
158 (setq line (concat (substring line 0 (- (point) end 1))
159 (spaces-string width)
160 (substring line (+ (length line)
161 (- (point) end)))))))
162 (if (or (> begextra 0) (> endextra 0))
163 (setq line (concat (spaces-string begextra)
164 line
165 (spaces-string endextra))))
166 (setcdr lines (cons line (cdr lines)))))
168 (defconst spaces-strings
169 '["" " " " " " " " " " " " " " " " "])
171 (defun spaces-string (n)
172 "Return a string with N spaces."
173 (if (<= n 8) (aref spaces-strings n)
174 (make-string n ?\s)))
176 ;;;###autoload
177 (defun delete-rectangle (start end &optional fill)
178 "Delete (don't save) text in the region-rectangle.
179 The same range of columns is deleted in each line starting with the
180 line where the region begins and ending with the line where the region
181 ends.
183 When called from a program the rectangle's corners are START and END.
184 With a prefix (or a FILL) argument, also fill lines where nothing has
185 to be deleted."
186 (interactive "*r\nP")
187 (apply-on-rectangle 'delete-rectangle-line start end fill))
189 ;;;###autoload
190 (defun delete-extract-rectangle (start end &optional fill)
191 "Delete the contents of the rectangle with corners at START and END.
192 Return it as a list of strings, one for each line of the rectangle.
194 When called from a program the rectangle's corners are START and END.
195 With an optional FILL argument, also fill lines where nothing has to be
196 deleted."
197 (let ((lines (list nil)))
198 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
199 (nreverse (cdr lines))))
201 ;;;###autoload
202 (defun extract-rectangle (start end)
203 "Return the contents of the rectangle with corners at START and END.
204 Return it as a list of strings, one for each line of the rectangle."
205 (let ((lines (list nil)))
206 (apply-on-rectangle 'extract-rectangle-line start end lines)
207 (nreverse (cdr lines))))
209 (defvar killed-rectangle nil
210 "Rectangle for `yank-rectangle' to insert.")
212 ;;;###autoload
213 (defun kill-rectangle (start end &optional fill)
214 "Delete the region-rectangle and save it as the last killed one.
216 When called from a program the rectangle's corners are START and END.
217 You might prefer to use `delete-extract-rectangle' from a program.
219 With a prefix (or a FILL) argument, also fill lines where nothing has to be
220 deleted.
222 If the buffer is read-only, Emacs will beep and refrain from deleting
223 the rectangle, but put it in the kill ring anyway. This means that
224 you can use this command to copy text from a read-only buffer.
225 \(If the variable `kill-read-only-ok' is non-nil, then this won't
226 even beep.)"
227 (interactive "r\nP")
228 (condition-case nil
229 (setq killed-rectangle (delete-extract-rectangle start end fill))
230 ((buffer-read-only text-read-only)
231 (setq killed-rectangle (extract-rectangle start end))
232 (if kill-read-only-ok
233 (progn (message "Read only text copied to kill ring") nil)
234 (barf-if-buffer-read-only)
235 (signal 'text-read-only (list (current-buffer)))))))
237 ;;;###autoload
238 (defun yank-rectangle ()
239 "Yank the last killed rectangle with upper left corner at point."
240 (interactive "*")
241 (insert-rectangle killed-rectangle))
243 ;;;###autoload
244 (defun insert-rectangle (rectangle)
245 "Insert text of RECTANGLE with upper left corner at point.
246 RECTANGLE's first line is inserted at point, its second
247 line is inserted at a point vertically under point, etc.
248 RECTANGLE should be a list of strings.
249 After this command, the mark is at the upper left corner
250 and point is at the lower right corner."
251 (let ((lines rectangle)
252 (insertcolumn (current-column))
253 (first t))
254 (push-mark)
255 (while lines
256 (or first
257 (progn
258 (forward-line 1)
259 (or (bolp) (insert ?\n))
260 (move-to-column insertcolumn t)))
261 (setq first nil)
262 (insert-for-yank (car lines))
263 (setq lines (cdr lines)))))
265 ;;;###autoload
266 (defun open-rectangle (start end &optional fill)
267 "Blank out the region-rectangle, shifting text right.
269 The text previously in the region is not overwritten by the blanks,
270 but instead winds up to the right of the rectangle.
272 When called from a program the rectangle's corners are START and END.
273 With a prefix (or a FILL) argument, fill with blanks even if there is
274 no text on the right side of the rectangle."
275 (interactive "*r\nP")
276 (apply-on-rectangle 'open-rectangle-line start end fill)
277 (goto-char start))
279 (defun open-rectangle-line (startcol endcol fill)
280 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
281 (unless (and (not fill)
282 (= (point) (point-at-eol)))
283 (indent-to endcol))))
285 (defun delete-whitespace-rectangle-line (startcol endcol fill)
286 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
287 (unless (= (point) (point-at-eol))
288 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
290 ;;;###autoload
291 (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
293 ;;;###autoload
294 (defun delete-whitespace-rectangle (start end &optional fill)
295 "Delete all whitespace following a specified column in each line.
296 The left edge of the rectangle specifies the position in each line
297 at which whitespace deletion should begin. On each line in the
298 rectangle, all continuous whitespace starting at that column is deleted.
300 When called from a program the rectangle's corners are START and END.
301 With a prefix (or a FILL) argument, also fill too short lines."
302 (interactive "*r\nP")
303 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
305 (defvar string-rectangle-history nil)
306 (defun string-rectangle-line (startcol endcol string delete)
307 (move-to-column startcol t)
308 (if delete
309 (delete-rectangle-line startcol endcol nil))
310 (insert string))
312 ;;;###autoload
313 (defun string-rectangle (start end string)
314 "Replace rectangle contents with STRING on each line.
315 The length of STRING need not be the same as the rectangle width.
317 Called from a program, takes three args; START, END and STRING."
318 (interactive
319 (progn (barf-if-buffer-read-only)
320 (list
321 (region-beginning)
322 (region-end)
323 (read-string (format "String rectangle (default %s): "
324 (or (car string-rectangle-history) ""))
325 nil 'string-rectangle-history
326 (car string-rectangle-history)))))
327 (apply-on-rectangle 'string-rectangle-line start end string t))
329 ;;;###autoload
330 (defalias 'replace-rectangle 'string-rectangle)
332 ;;;###autoload
333 (defun string-insert-rectangle (start end string)
334 "Insert STRING on each line of region-rectangle, shifting text right.
336 When called from a program, the rectangle's corners are START and END.
337 The left edge of the rectangle specifies the column for insertion.
338 This command does not delete or overwrite any existing text."
339 (interactive
340 (progn (barf-if-buffer-read-only)
341 (list
342 (region-beginning)
343 (region-end)
344 (read-string (format "String insert 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 nil))
350 ;;;###autoload
351 (defun clear-rectangle (start end &optional fill)
352 "Blank out the region-rectangle.
353 The text previously in the region is overwritten with blanks.
355 When called from a program the rectangle's corners are START and END.
356 With a prefix (or a FILL) argument, also fill with blanks the parts of the
357 rectangle which were empty."
358 (interactive "*r\nP")
359 (apply-on-rectangle 'clear-rectangle-line start end fill))
361 (defun clear-rectangle-line (startcol endcol fill)
362 (let ((pt (point-at-eol)))
363 (when (= (move-to-column startcol (if fill t 'coerce)) startcol)
364 (if (and (not fill)
365 (<= (save-excursion (goto-char pt) (current-column)) endcol))
366 (delete-region (point) pt)
367 ;; else
368 (setq pt (point))
369 (move-to-column endcol t)
370 (setq endcol (current-column))
371 (delete-region pt (point))
372 (indent-to endcol)))))
374 ;; Line numbers for `rectangle-number-line-callback'.
375 (defvar rectangle-number-line-counter)
377 (defun rectangle-number-line-callback (start end format-string)
378 (move-to-column start t)
379 (insert (format format-string rectangle-number-line-counter))
380 (setq rectangle-number-line-counter
381 (1+ rectangle-number-line-counter)))
383 (defun rectange--default-line-number-format (start end start-at)
384 (concat "%"
385 (int-to-string (length (int-to-string (+ (count-lines start end)
386 start-at))))
387 "d "))
389 ;;;###autoload
390 (defun rectangle-number-lines (start end start-at &optional format)
391 "Insert numbers in front of the region-rectangle.
393 START-AT, if non-nil, should be a number from which to begin
394 counting. FORMAT, if non-nil, should be a format string to pass
395 to `format' along with the line count. When called interactively
396 with a prefix argument, prompt for START-AT and FORMAT."
397 (interactive
398 (if current-prefix-arg
399 (let* ((start (region-beginning))
400 (end (region-end))
401 (start-at (read-number "Number to count from: " 1)))
402 (list start end start-at
403 (read-string "Format string: "
404 (rectange--default-line-number-format
405 start end start-at))))
406 (list (region-beginning) (region-end) 1 nil)))
407 (unless format
408 (setq format (rectange--default-line-number-format start end start-at)))
409 (let ((rectangle-number-line-counter start-at))
410 (apply-on-rectangle 'rectangle-number-line-callback
411 start end format)))
413 (provide 'rect)
415 ;;; rect.el ends here