(texinfo-font-lock-keywords): Disable the automatic environment name update.
[emacs.git] / lisp / rect.el
blob933a2de81ff1d538e484a8a6caec3d810eedad6f
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 "If COLUMN is within a multi-column character, replace it by spaces and tab.
41 As for `move-to-column', passing anything but nil or t in FLAG will move to
42 the desired column only if the line is long enough."
43 (move-to-column column (or flag t)))
44 (make-obsolete 'move-to-column-force 'move-to-column "21.2")
46 ;; not used any more --dv
47 ;; extract-rectangle-line stores lines into this list
48 ;; to accumulate them for extract-rectangle and delete-extract-rectangle.
49 (defvar operate-on-rectangle-lines)
51 ;; ### NOTE: this function is untouched, but not used anymore apart from
52 ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
53 (defun operate-on-rectangle (function start end coerce-tabs)
54 "Call FUNCTION for each line of rectangle with corners at START, END.
55 If COERCE-TABS is non-nil, convert multi-column characters
56 that span the starting or ending columns on any line
57 to multiple spaces before calling FUNCTION.
58 FUNCTION is called with three arguments:
59 position of start of segment of this line within the rectangle,
60 number of columns that belong to rectangle but are before that position,
61 number of columns that belong to rectangle but are after point.
62 Point is at the end of the segment of this line within the rectangle."
63 (let (startcol startlinepos endcol endlinepos)
64 (save-excursion
65 (goto-char start)
66 (setq startcol (current-column))
67 (beginning-of-line)
68 (setq startlinepos (point)))
69 (save-excursion
70 (goto-char end)
71 (setq endcol (current-column))
72 (forward-line 1)
73 (setq endlinepos (point-marker)))
74 (if (< endcol startcol)
75 (setq startcol (prog1 endcol (setq endcol startcol))))
76 (save-excursion
77 (goto-char startlinepos)
78 (while (< (point) endlinepos)
79 (let (startpos begextra endextra)
80 (if coerce-tabs
81 (move-to-column startcol t)
82 (move-to-column startcol))
83 (setq begextra (- (current-column) startcol))
84 (setq startpos (point))
85 (if coerce-tabs
86 (move-to-column endcol t)
87 (move-to-column endcol))
88 ;; If we overshot, move back one character
89 ;; so that endextra will be positive.
90 (if (and (not coerce-tabs) (> (current-column) endcol))
91 (backward-char 1))
92 (setq endextra (- endcol (current-column)))
93 (if (< begextra 0)
94 (setq endextra (+ endextra begextra)
95 begextra 0))
96 (funcall function startpos begextra endextra))
97 (forward-line 1)))
98 (- endcol startcol)))
100 ;; The replacement for `operate-on-rectangle' -- dv
101 (defun apply-on-rectangle (function start end &rest args)
102 "Call FUNCTION for each line of rectangle with corners at START, END.
103 FUNCTION is called with two arguments: the start and end columns of the
104 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
105 the function is called."
106 (let (startcol startpt endcol endpt)
107 (save-excursion
108 (goto-char start)
109 (setq startcol (current-column))
110 (beginning-of-line)
111 (setq startpt (point))
112 (goto-char end)
113 (setq endcol (current-column))
114 (forward-line 1)
115 (setq endpt (point-marker))
116 ;; ensure the start column is the left one.
117 (if (< endcol startcol)
118 (let ((col startcol))
119 (setq startcol endcol endcol col)))
120 ;; start looping over lines
121 (goto-char startpt)
122 (while (< (point) endpt)
123 (apply function startcol endcol args)
124 (forward-line 1)))
127 (defun delete-rectangle-line (startcol endcol fill)
128 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
129 (delete-region (point)
130 (progn (move-to-column endcol 'coerce)
131 (point)))))
133 (defun delete-extract-rectangle-line (startcol endcol lines fill)
134 (let ((pt (point-at-eol)))
135 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
136 (setcdr lines (cons (spaces-string (- endcol startcol))
137 (cdr lines)))
138 ;; else
139 (setq pt (point))
140 (move-to-column endcol t)
141 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
142 (delete-region pt (point)))
145 ;; ### NOTE: this is actually the only function that needs to do complicated
146 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
147 ;; might be read-only. --dv
148 (defun extract-rectangle-line (startcol endcol lines)
149 (let (start end begextra endextra line)
150 (move-to-column startcol)
151 (setq start (point)
152 begextra (- (current-column) startcol))
153 (move-to-column endcol)
154 (setq end (point)
155 endextra (- endcol (current-column)))
156 (setq line (buffer-substring start (point)))
157 (if (< begextra 0)
158 (setq endextra (+ endextra begextra)
159 begextra 0))
160 (if (< endextra 0)
161 (setq endextra 0))
162 (goto-char start)
163 (while (search-forward "\t" end t)
164 (let ((width (- (current-column)
165 (save-excursion (forward-char -1)
166 (current-column)))))
167 (setq line (concat (substring line 0 (- (point) end 1))
168 (spaces-string width)
169 (substring line (+ (length line)
170 (- (point) end)))))))
171 (if (or (> begextra 0) (> endextra 0))
172 (setq line (concat (spaces-string begextra)
173 line
174 (spaces-string endextra))))
175 (setcdr lines (cons line (cdr lines)))))
177 (defconst spaces-strings
178 '["" " " " " " " " " " " " " " " " "])
180 ;; this one is untouched --dv
181 (defun spaces-string (n)
182 (if (<= n 8) (aref spaces-strings n)
183 (let ((val ""))
184 (while (> n 8)
185 (setq val (concat " " val)
186 n (- n 8)))
187 (concat val (aref spaces-strings n)))))
189 ;;;###autoload
190 (defun delete-rectangle (start end &optional fill)
191 "Delete (don't save) text in the region-rectangle.
192 The same range of columns is deleted in each line starting with the
193 line where the region begins and ending with the line where the region
194 ends.
196 When called from a program the rectangle's corners are START and END.
197 With a prefix (or a FILL) argument, also fill lines where nothing has
198 to be deleted."
199 (interactive "*r\nP")
200 (apply-on-rectangle 'delete-rectangle-line start end fill))
202 ;;;###autoload
203 (defun delete-extract-rectangle (start end &optional fill)
204 "Delete 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.
207 When called from a program the rectangle's corners are START and END.
208 With an optional FILL argument, also fill lines where nothing has to be
209 deleted."
210 (let ((lines (list nil)))
211 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
212 (nreverse (cdr lines))))
214 ;;;###autoload
215 (defun extract-rectangle (start end)
216 "Return the contents of the rectangle with corners at START and END.
217 Return it as a list of strings, one for each line of the rectangle."
218 (let ((lines (list nil)))
219 (apply-on-rectangle 'extract-rectangle-line start end lines)
220 (nreverse (cdr lines))))
222 (defvar killed-rectangle nil
223 "Rectangle for `yank-rectangle' to insert.")
225 ;;;###autoload
226 (defun kill-rectangle (start end &optional fill)
227 "Delete the region-rectangle and save it as the last killed one.
229 When called from a program the rectangle's corners are START and END.
230 You might prefer to use `delete-extract-rectangle' from a program.
232 With a prefix (or a FILL) argument, also fill lines where nothing has to be
233 deleted."
234 (interactive "*r\nP")
235 (when buffer-read-only
236 (setq killed-rectangle (extract-rectangle start end))
237 (barf-if-buffer-read-only))
238 (setq killed-rectangle (delete-extract-rectangle start end fill)))
240 ;; this one is untouched --dv
241 ;;;###autoload
242 (defun yank-rectangle ()
243 "Yank the last killed rectangle with upper left corner at point."
244 (interactive "*")
245 (insert-rectangle killed-rectangle))
247 ;; this one is untoutched --dv
248 ;;;###autoload
249 (defun insert-rectangle (rectangle)
250 "Insert text of RECTANGLE with upper left corner at point.
251 RECTANGLE's first line is inserted at point, its second
252 line is inserted at a point vertically under point, etc.
253 RECTANGLE should be a list of strings.
254 After this command, the mark is at the upper left corner
255 and point is at the lower right corner."
256 (let ((lines rectangle)
257 (insertcolumn (current-column))
258 (first t))
259 (push-mark)
260 (while lines
261 (or first
262 (progn
263 (forward-line 1)
264 (or (bolp) (insert ?\n))
265 (move-to-column insertcolumn t)))
266 (setq first nil)
267 (insert-for-yank (car lines))
268 (setq lines (cdr lines)))))
270 ;;;###autoload
271 (defun open-rectangle (start end &optional fill)
272 "Blank out the region-rectangle, shifting text right.
274 The text previously in the region is not overwritten by the blanks,
275 but instead winds up to the right of the rectangle.
277 When called from a program the rectangle's corners are START and END.
278 With a prefix (or a FILL) argument, fill with blanks even if there is no text
279 on the right side of the rectangle."
280 (interactive "*r\nP")
281 (apply-on-rectangle 'open-rectangle-line start end fill)
282 (goto-char start))
284 (defun open-rectangle-line (startcol endcol fill)
285 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
286 (unless (and (not fill)
287 (= (point) (point-at-eol)))
288 (indent-to endcol))))
290 (defun delete-whitespace-rectangle-line (startcol endcol fill)
291 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
292 (unless (= (point) (point-at-eol))
293 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
295 ;;;###autoload
296 (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
298 ;;;###autoload
299 (defun delete-whitespace-rectangle (start end &optional fill)
300 "Delete all whitespace following a specified column in each line.
301 The left edge of the rectangle specifies the position in each line
302 at which whitespace deletion should begin. On each line in the
303 rectangle, all continuous whitespace starting at that column is deleted.
305 When called from a program the rectangle's corners are START and END.
306 With a prefix (or a FILL) argument, also fill too short lines."
307 (interactive "*r\nP")
308 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
310 ;; not used any more --dv
311 ;; string-rectangle uses this variable to pass the string
312 ;; to string-rectangle-line.
313 (defvar string-rectangle-string)
314 (defvar string-rectangle-history nil)
315 (defun string-rectangle-line (startcol endcol string delete)
316 (move-to-column startcol t)
317 (if delete
318 (delete-rectangle-line startcol endcol nil))
319 (insert string))
321 ;;;###autoload
322 (defun string-rectangle (start end string)
323 "Replace rectangle contents with STRING on each line.
324 The length of STRING need not be the same as the rectangle width.
326 Called from a program, takes three args; START, END and STRING."
327 (interactive
328 (progn (barf-if-buffer-read-only)
329 (list
330 (region-beginning)
331 (region-end)
332 (read-string (format "String rectangle (default `%s'): "
333 (or (car string-rectangle-history) ""))
334 nil 'string-rectangle-history
335 (car string-rectangle-history)))))
336 (apply-on-rectangle 'string-rectangle-line start end string t))
338 ;;;###autoload
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
349 (progn (barf-if-buffer-read-only)
350 (list
351 (region-beginning)
352 (region-end)
353 (read-string (format "String insert rectangle (default `%s'): "
354 (or (car string-rectangle-history) ""))
355 nil 'string-rectangle-history
356 (car string-rectangle-history)))))
357 (apply-on-rectangle 'string-rectangle-line start end string nil))
359 ;;;###autoload
360 (defun clear-rectangle (start end &optional fill)
361 "Blank out the region-rectangle.
362 The text previously in the region is overwritten with blanks.
364 When called from a program the rectangle's corners are START and END.
365 With a prefix (or a FILL) argument, also fill with blanks the parts of the
366 rectangle which were empty."
367 (interactive "*r\nP")
368 (apply-on-rectangle 'clear-rectangle-line start end fill))
370 (defun clear-rectangle-line (startcol endcol fill)
371 (let ((pt (point-at-eol)))
372 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
373 (if (and (not fill)
374 (<= (save-excursion (goto-char pt) (current-column)) endcol))
375 (delete-region (point) pt)
376 ;; else
377 (setq pt (point))
378 (move-to-column endcol t)
379 (setq endcol (current-column))
380 (delete-region pt (point))
381 (indent-to endcol)))))
383 (provide 'rect)
385 ;;; rect.el ends here