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