38436
|
1 ;;; rect.el --- rectangle functions for GNU Emacs
|
659
|
2
|
37444
|
3 ;; Copyright (C) 1985, 1999, 2000, 2001 Free Software Foundation, Inc.
|
845
|
4
|
39537
|
5 ;; Maintainer: Didier Verna <didier@xemacs.org>
|
814
|
6 ;; Keywords: internal
|
789
|
7
|
36
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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
|
807
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
36
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
14169
|
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.
|
36
|
24
|
2308
|
25 ;;; Commentary:
|
|
26
|
25379
|
27 ;; This package provides the operations on rectangles that are documented
|
2308
|
28 ;; in the Emacs manual.
|
|
29
|
25168
|
30 ;; ### NOTE: this file has been almost completely rewritten by Didier Verna
|
39537
|
31 ;; <didier@xemacs.org> in July 1999. The purpose of this rewrite is to be less
|
25168
|
32 ;; intrusive and fill lines with whitespaces only when needed. A few functions
|
|
33 ;; are untouched though, as noted above their definition.
|
|
34
|
|
35
|
789
|
36 ;;; Code:
|
36
|
37
|
23757
|
38 ;;;###autoload
|
25168
|
39 (defun move-to-column-force (column &optional flag)
|
41173
|
40 "Obsolete. Use `move-to-column'.
|
|
41 If COLUMN is within a multi-column character, replace it by spaces and tab.
|
25168
|
42 As for `move-to-column', passing anything but nil or t in FLAG will move to
|
|
43 the desired column only if the line is long enough."
|
41173
|
44 (move-to-column column (or flag t)))
|
|
45 (make-obsolete 'move-to-column-force "move-to-column" "21.2")
|
23757
|
46
|
25168
|
47 ;; not used any more --dv
|
22213
|
48 ;; extract-rectangle-line stores lines into this list
|
|
49 ;; to accumulate them for extract-rectangle and delete-extract-rectangle.
|
|
50 (defvar operate-on-rectangle-lines)
|
|
51
|
30283
|
52 ;; ### NOTE: this function is untouched, but not used anymore apart from
|
25168
|
53 ;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
|
36
|
54 (defun operate-on-rectangle (function start end coerce-tabs)
|
|
55 "Call FUNCTION for each line of rectangle with corners at START, END.
|
|
56 If COERCE-TABS is non-nil, convert multi-column characters
|
|
57 that span the starting or ending columns on any line
|
|
58 to multiple spaces before calling FUNCTION.
|
|
59 FUNCTION is called with three arguments:
|
|
60 position of start of segment of this line within the rectangle,
|
|
61 number of columns that belong to rectangle but are before that position,
|
|
62 number of columns that belong to rectangle but are after point.
|
|
63 Point is at the end of the segment of this line within the rectangle."
|
|
64 (let (startcol startlinepos endcol endlinepos)
|
|
65 (save-excursion
|
|
66 (goto-char start)
|
|
67 (setq startcol (current-column))
|
|
68 (beginning-of-line)
|
|
69 (setq startlinepos (point)))
|
|
70 (save-excursion
|
|
71 (goto-char end)
|
|
72 (setq endcol (current-column))
|
|
73 (forward-line 1)
|
|
74 (setq endlinepos (point-marker)))
|
|
75 (if (< endcol startcol)
|
1619
|
76 (setq startcol (prog1 endcol (setq endcol startcol))))
|
5787
|
77 (save-excursion
|
|
78 (goto-char startlinepos)
|
|
79 (while (< (point) endlinepos)
|
|
80 (let (startpos begextra endextra)
|
23757
|
81 (if coerce-tabs
|
41173
|
82 (move-to-column startcol t)
|
23757
|
83 (move-to-column startcol))
|
5787
|
84 (setq begextra (- (current-column) startcol))
|
|
85 (setq startpos (point))
|
23757
|
86 (if coerce-tabs
|
41173
|
87 (move-to-column endcol t)
|
23757
|
88 (move-to-column endcol))
|
19732
d8478556c7f0
(operate-on-rectangle): If we overshoot when looking for endcol, back up.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
89 ;; If we overshot, move back one character
|
d8478556c7f0
(operate-on-rectangle): If we overshoot when looking for endcol, back up.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
90 ;; so that endextra will be positive.
|
d8478556c7f0
(operate-on-rectangle): If we overshoot when looking for endcol, back up.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
91 (if (and (not coerce-tabs) (> (current-column) endcol))
|
d8478556c7f0
(operate-on-rectangle): If we overshoot when looking for endcol, back up.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
92 (backward-char 1))
|
5787
|
93 (setq endextra (- endcol (current-column)))
|
|
94 (if (< begextra 0)
|
|
95 (setq endextra (+ endextra begextra)
|
|
96 begextra 0))
|
|
97 (funcall function startpos begextra endextra))
|
|
98 (forward-line 1)))
|
36
|
99 (- endcol startcol)))
|
|
100
|
25168
|
101 ;; The replacement for `operate-on-rectangle' -- dv
|
|
102 (defun apply-on-rectangle (function start end &rest args)
|
|
103 "Call FUNCTION for each line of rectangle with corners at START, END.
|
|
104 FUNCTION is called with two arguments: the start and end columns of the
|
25379
|
105 rectangle, plus ARGS extra arguments. Point is at the beginning of line when
|
25168
|
106 the function is called."
|
|
107 (let (startcol startpt endcol endpt)
|
|
108 (save-excursion
|
|
109 (goto-char start)
|
|
110 (setq startcol (current-column))
|
|
111 (beginning-of-line)
|
|
112 (setq startpt (point))
|
|
113 (goto-char end)
|
|
114 (setq endcol (current-column))
|
|
115 (forward-line 1)
|
|
116 (setq endpt (point-marker))
|
|
117 ;; ensure the start column is the left one.
|
|
118 (if (< endcol startcol)
|
|
119 (let ((col startcol))
|
|
120 (setq startcol endcol endcol col)))
|
|
121 ;; start looping over lines
|
|
122 (goto-char startpt)
|
|
123 (while (< (point) endpt)
|
|
124 (apply function startcol endcol args)
|
|
125 (forward-line 1)))
|
|
126 ))
|
36
|
127
|
25168
|
128 (defun delete-rectangle-line (startcol endcol fill)
|
41173
|
129 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
|
|
130 (delete-region (point)
|
|
131 (progn (move-to-column endcol 'coerce)
|
|
132 (point)))))
|
36
|
133
|
25168
|
134 (defun delete-extract-rectangle-line (startcol endcol lines fill)
|
|
135 (let ((pt (point-at-eol)))
|
41173
|
136 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
|
25168
|
137 (setcdr lines (cons (spaces-string (- endcol startcol))
|
|
138 (cdr lines)))
|
|
139 ;; else
|
|
140 (setq pt (point))
|
41173
|
141 (move-to-column endcol t)
|
25168
|
142 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
|
|
143 (delete-region pt (point)))
|
|
144 ))
|
|
145
|
|
146 ;; ### NOTE: this is actually the only function that needs to do complicated
|
|
147 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
|
|
148 ;; might be read-only. --dv
|
|
149 (defun extract-rectangle-line (startcol endcol lines)
|
|
150 (let (start end begextra endextra line)
|
|
151 (move-to-column startcol)
|
|
152 (setq start (point)
|
|
153 begextra (- (current-column) startcol))
|
|
154 (move-to-column endcol)
|
|
155 (setq end (point)
|
|
156 endextra (- endcol (current-column)))
|
|
157 (setq line (buffer-substring start (point)))
|
|
158 (if (< begextra 0)
|
|
159 (setq endextra (+ endextra begextra)
|
|
160 begextra 0))
|
|
161 (if (< endextra 0)
|
|
162 (setq endextra 0))
|
|
163 (goto-char start)
|
36
|
164 (while (search-forward "\t" end t)
|
|
165 (let ((width (- (current-column)
|
|
166 (save-excursion (forward-char -1)
|
|
167 (current-column)))))
|
|
168 (setq line (concat (substring line 0 (- (point) end 1))
|
|
169 (spaces-string width)
|
25168
|
170 (substring line (+ (length line)
|
|
171 (- (point) end)))))))
|
36
|
172 (if (or (> begextra 0) (> endextra 0))
|
|
173 (setq line (concat (spaces-string begextra)
|
|
174 line
|
|
175 (spaces-string endextra))))
|
25168
|
176 (setcdr lines (cons line (cdr lines)))))
|
36
|
177
|
|
178 (defconst spaces-strings
|
|
179 '["" " " " " " " " " " " " " " " " "])
|
|
180
|
25168
|
181 ;; this one is untouched --dv
|
36
|
182 (defun spaces-string (n)
|
|
183 (if (<= n 8) (aref spaces-strings n)
|
|
184 (let ((val ""))
|
|
185 (while (> n 8)
|
|
186 (setq val (concat " " val)
|
|
187 n (- n 8)))
|
|
188 (concat val (aref spaces-strings n)))))
|
|
189
|
258
|
190 ;;;###autoload
|
25168
|
191 (defun delete-rectangle (start end &optional fill)
|
25379
|
192 "Delete (don't save) text in the region-rectangle.
|
|
193 The same range of columns is deleted in each line starting with the
|
|
194 line where the region begins and ending with the line where the region
|
|
195 ends.
|
25168
|
196
|
25379
|
197 When called from a program the rectangle's corners are START and END.
|
|
198 With a prefix (or a FILL) argument, also fill lines where nothing has
|
|
199 to be deleted."
|
|
200 (interactive "*r\nP")
|
25168
|
201 (apply-on-rectangle 'delete-rectangle-line start end fill))
|
36
|
202
|
258
|
203 ;;;###autoload
|
25168
|
204 (defun delete-extract-rectangle (start end &optional fill)
|
25380
|
205 "Delete the contents of the rectangle with corners at START and END.
|
25379
|
206 Return it as a list of strings, one for each line of the rectangle.
|
25168
|
207
|
25379
|
208 When called from a program the rectangle's corners are START and END.
|
25168
|
209 With an optional FILL argument, also fill lines where nothing has to be
|
|
210 deleted."
|
|
211 (let ((lines (list nil)))
|
|
212 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
|
|
213 (nreverse (cdr lines))))
|
36
|
214
|
258
|
215 ;;;###autoload
|
36
|
216 (defun extract-rectangle (start end)
|
25379
|
217 "Return the contents of the rectangle with corners at START and END.
|
|
218 Return it as a list of strings, one for each line of the rectangle."
|
25168
|
219 (let ((lines (list nil)))
|
|
220 (apply-on-rectangle 'extract-rectangle-line start end lines)
|
|
221 (nreverse (cdr lines))))
|
36
|
222
|
|
223 (defvar killed-rectangle nil
|
25379
|
224 "Rectangle for `yank-rectangle' to insert.")
|
36
|
225
|
258
|
226 ;;;###autoload
|
25168
|
227 (defun kill-rectangle (start end &optional fill)
|
25379
|
228 "Delete the region-rectangle and save it as the last killed one.
|
|
229
|
|
230 When called from a program the rectangle's corners are START and END.
|
|
231 You might prefer to use `delete-extract-rectangle' from a program.
|
36
|
232
|
25168
|
233 With a prefix (or a FILL) argument, also fill lines where nothing has to be
|
|
234 deleted."
|
25379
|
235 (interactive "*r\nP")
|
25168
|
236 (when buffer-read-only
|
|
237 (setq killed-rectangle (extract-rectangle start end))
|
|
238 (barf-if-buffer-read-only))
|
|
239 (setq killed-rectangle (delete-extract-rectangle start end fill)))
|
|
240
|
|
241 ;; this one is untouched --dv
|
258
|
242 ;;;###autoload
|
36
|
243 (defun yank-rectangle ()
|
|
244 "Yank the last killed rectangle with upper left corner at point."
|
25379
|
245 (interactive "*")
|
36
|
246 (insert-rectangle killed-rectangle))
|
|
247
|
25168
|
248 ;; this one is untoutched --dv
|
258
|
249 ;;;###autoload
|
36
|
250 (defun insert-rectangle (rectangle)
|
|
251 "Insert text of RECTANGLE with upper left corner at point.
|
242
|
252 RECTANGLE's first line is inserted at point, its second
|
|
253 line is inserted at a point vertically under point, etc.
|
1542
|
254 RECTANGLE should be a list of strings.
|
|
255 After this command, the mark is at the upper left corner
|
|
256 and point is at the lower right corner."
|
36
|
257 (let ((lines rectangle)
|
|
258 (insertcolumn (current-column))
|
|
259 (first t))
|
1542
|
260 (push-mark)
|
36
|
261 (while lines
|
|
262 (or first
|
|
263 (progn
|
|
264 (forward-line 1)
|
|
265 (or (bolp) (insert ?\n))
|
41173
|
266 (move-to-column insertcolumn t)))
|
36
|
267 (setq first nil)
|
44676
|
268 (insert-for-yank (car lines))
|
36
|
269 (setq lines (cdr lines)))))
|
|
270
|
258
|
271 ;;;###autoload
|
25168
|
272 (defun open-rectangle (start end &optional fill)
|
25379
|
273 "Blank out the region-rectangle, shifting text right.
|
25168
|
274
|
25379
|
275 The text previously in the region is not overwritten by the blanks,
|
|
276 but instead winds up to the right of the rectangle.
|
|
277
|
|
278 When called from a program the rectangle's corners are START and END.
|
25168
|
279 With a prefix (or a FILL) argument, fill with blanks even if there is no text
|
|
280 on the right side of the rectangle."
|
25379
|
281 (interactive "*r\nP")
|
25168
|
282 (apply-on-rectangle 'open-rectangle-line start end fill)
|
1619
|
283 (goto-char start))
|
36
|
284
|
25168
|
285 (defun open-rectangle-line (startcol endcol fill)
|
41173
|
286 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
|
30283
|
287 (unless (and (not fill)
|
|
288 (= (point) (point-at-eol)))
|
|
289 (indent-to endcol))))
|
25168
|
290
|
|
291 (defun delete-whitespace-rectangle-line (startcol endcol fill)
|
41173
|
292 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
|
25168
|
293 (unless (= (point) (point-at-eol))
|
37444
|
294 (delete-region (point) (progn (skip-syntax-forward " ") (point))))))
|
36
|
295
|
23812
|
296 ;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
|
258
|
297 ;;;###autoload
|
25168
|
298 (defun delete-whitespace-rectangle (start end &optional fill)
|
22205
|
299 "Delete all whitespace following a specified column in each line.
|
|
300 The left edge of the rectangle specifies the position in each line
|
|
301 at which whitespace deletion should begin. On each line in the
|
25168
|
302 rectangle, all continuous whitespace starting at that column is deleted.
|
22205
|
303
|
25379
|
304 When called from a program the rectangle's corners are START and END.
|
25168
|
305 With a prefix (or a FILL) argument, also fill too short lines."
|
25379
|
306 (interactive "*r\nP")
|
25168
|
307 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
|
|
308
|
|
309 ;; not used any more --dv
|
22213
|
310 ;; string-rectangle uses this variable to pass the string
|
|
311 ;; to string-rectangle-line.
|
|
312 (defvar string-rectangle-string)
|
41173
|
313 (defvar string-rectangle-history nil)
|
33772
|
314 (defun string-rectangle-line (startcol endcol string delete)
|
41173
|
315 (move-to-column startcol t)
|
33772
|
316 (if delete
|
|
317 (delete-rectangle-line startcol endcol nil))
|
25168
|
318 (insert string))
|
2380
e67f6d2679e3
(fill-rectangle) Added. Inspired by Lynn Slater's insert-box package in LCD,
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
319
|
e67f6d2679e3
(fill-rectangle) Added. Inspired by Lynn Slater's insert-box package in LCD,
Eric S. Raymond <esr@snark.thyrsus.com>
diff
changeset
|
320 ;;;###autoload
|
41173
|
321
|
37444
|
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.
|
|
325
|
|
326 Called from a program, takes three args; START, END and STRING."
|
41173
|
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)))))
|
33772
|
336 (apply-on-rectangle 'string-rectangle-line start end string t))
|
28078
|
337
|
44207
|
338 ;;;###autoload
|
37444
|
339 (defalias 'replace-rectangle 'string-rectangle)
|
|
340
|
|
341 ;;;###autoload
|
|
342 (defun string-insert-rectangle (start end string)
|
|
343 "Insert STRING on each line of region-rectangle, shifting text right.
|
|
344
|
|
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."
|
41173
|
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)))))
|
37444
|
357 (apply-on-rectangle 'string-rectangle-line start end string nil))
|
|
358
|
28078
|
359 ;;;###autoload
|
25168
|
360 (defun clear-rectangle (start end &optional fill)
|
25379
|
361 "Blank out the region-rectangle.
|
|
362 The text previously in the region is overwritten with blanks.
|
25168
|
363
|
25379
|
364 When called from a program the rectangle's corners are START and END.
|
25168
|
365 With a prefix (or a FILL) argument, also fill with blanks the parts of the
|
|
366 rectangle which were empty."
|
25379
|
367 (interactive "*r\nP")
|
25168
|
368 (apply-on-rectangle 'clear-rectangle-line start end fill))
|
36
|
369
|
25168
|
370 (defun clear-rectangle-line (startcol endcol fill)
|
43555
|
371 (let ((pt (point-at-eol)))
|
41173
|
372 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
|
25168
|
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))
|
41173
|
378 (move-to-column endcol t)
|
43555
|
379 (setq endcol (current-column))
|
25168
|
380 (delete-region pt (point))
|
43555
|
381 (indent-to endcol)))))
|
36
|
382
|
1619
|
383 (provide 'rect)
|
659
|
384
|
|
385 ;;; rect.el ends here
|