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