aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1999-08-03 18:36:16 +0000
committerRichard M. Stallman1999-08-03 18:36:16 +0000
commite417c66fa3b2a593f1e134654b72bf15f67ba223 (patch)
treee5a04e6707f2c14039cb4f76bdff328baa2c2b28
parent84482eb3014e0269828e12d4b05b53963de9d95a (diff)
downloademacs-e417c66fa3b2a593f1e134654b72bf15f67ba223.tar.gz
emacs-e417c66fa3b2a593f1e134654b72bf15f67ba223.zip
All functions rewritten, except when noted above
their declaration. Below is a list of interface changes. (apply-on-rectangle): New function, mostly replaces `operate-on-rectangle'. All callers changed. (move-to-column-force): Pass new second argument to `move-to-column'. (kill-rectangle): Added optional prefix arg to fill lines. (delete-rectangle): Ditto. (delete-whitespace-rectangle): Ditto. (delete-extract-rectangle): Ditto. (open-rectangle): Ditto. (clear-rectangle): Ditto. (delete-whitespace-rectangle-line): New function. (delete-rectangle-line): Added third arg FILL. (delete-extract-rectangle-line): Ditto. (open-rectangle-line): Ditto. (clear-rectangle-line): Ditto.
-rw-r--r--lisp/rect.el312
1 files changed, 191 insertions, 121 deletions
diff --git a/lisp/rect.el b/lisp/rect.el
index 3a643c63add..4f5ae2d8146 100644
--- a/lisp/rect.el
+++ b/lisp/rect.el
@@ -1,8 +1,8 @@
1;;; rect.el --- rectangle functions for GNU Emacs. 1;;; rect.el --- rectangle functions for GNU Emacs.
2 2
3;; Copyright (C) 1985, 1994 Free Software Foundation, Inc. 3;; Copyright (C) 1985, 1999 Free Software Foundation, Inc.
4 4
5;; Maintainer: FSF 5;; Maintainer: Didier Verna <verna@inf.enst.fr>
6;; Keywords: internal 6;; Keywords: internal
7 7
8;; This file is part of GNU Emacs. 8;; This file is part of GNU Emacs.
@@ -27,14 +27,23 @@
27;; This package provides the operations on rectangles that are ocumented 27;; This package provides the operations on rectangles that are ocumented
28;; in the Emacs manual. 28;; in the Emacs manual.
29 29
30;; ### NOTE: this file has been almost completely rewritten by Didier Verna
31;; <verna@inf.enst.fr> 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.
34
35
30;;; Code: 36;;; Code:
31 37
32;;;###autoload 38;;;###autoload
33(defun move-to-column-force (column) 39(defun move-to-column-force (column &optional flag)
34 "Move point to column COLUMN rigidly in the current line. 40 "Move point to column COLUMN rigidly in the current line.
35If COLUMN is within a multi-column character, replace it by 41If COLUMN is within a multi-column character, replace it by
36spaces and tab." 42spaces and tab.
37 (let ((col (move-to-column column t))) 43
44As for `move-to-column', passing anything but nil or t in FLAG will move to
45the desired column only if the line is long enough."
46 (let ((col (move-to-column column (or flag t))))
38 (if (> col column) 47 (if (> col column)
39 (let (pos) 48 (let (pos)
40 (delete-char -1) 49 (delete-char -1)
@@ -44,10 +53,13 @@ spaces and tab."
44 (goto-char pos))) 53 (goto-char pos)))
45 column)) 54 column))
46 55
56;; not used any more --dv
47;; extract-rectangle-line stores lines into this list 57;; extract-rectangle-line stores lines into this list
48;; to accumulate them for extract-rectangle and delete-extract-rectangle. 58;; to accumulate them for extract-rectangle and delete-extract-rectangle.
49(defvar operate-on-rectangle-lines) 59(defvar operate-on-rectangle-lines)
50 60
61;; ### NOTE: this function is untouched, but not used anymore appart in
62;; `delete-whitespace-rectangle'. `apply-on-rectangle' is used instead. --dv
51(defun operate-on-rectangle (function start end coerce-tabs) 63(defun operate-on-rectangle (function start end coerce-tabs)
52 "Call FUNCTION for each line of rectangle with corners at START, END. 64 "Call FUNCTION for each line of rectangle with corners at START, END.
53If COERCE-TABS is non-nil, convert multi-column characters 65If COERCE-TABS is non-nil, convert multi-column characters
@@ -95,34 +107,92 @@ Point is at the end of the segment of this line within the rectangle."
95 (forward-line 1))) 107 (forward-line 1)))
96 (- endcol startcol))) 108 (- endcol startcol)))
97 109
98(defun delete-rectangle-line (startdelpos ignore ignore) 110;; The replacement for `operate-on-rectangle' -- dv
99 (delete-region startdelpos (point))) 111(defun apply-on-rectangle (function start end &rest args)
100 112 "Call FUNCTION for each line of rectangle with corners at START, END.
101(defun delete-extract-rectangle-line (startdelpos begextra endextra) 113FUNCTION is called with two arguments: the start and end columns of the
102 (save-excursion 114rectangle, plus ARGS extra arguments. Point is at the beginning of line when
103 (extract-rectangle-line startdelpos begextra endextra)) 115the function is called."
104 (delete-region startdelpos (point))) 116 (let (startcol startpt endcol endpt)
105 117 (save-excursion
106(defun extract-rectangle-line (startdelpos begextra endextra) 118 (goto-char start)
107 (let ((line (buffer-substring startdelpos (point))) 119 (setq startcol (current-column))
108 (end (point))) 120 (beginning-of-line)
109 (goto-char startdelpos) 121 (setq startpt (point))
122 (goto-char end)
123 (setq endcol (current-column))
124 (forward-line 1)
125 (setq endpt (point-marker))
126 ;; ensure the start column is the left one.
127 (if (< endcol startcol)
128 (let ((col startcol))
129 (setq startcol endcol endcol col)))
130 ;; start looping over lines
131 (goto-char startpt)
132 (while (< (point) endpt)
133 (apply function startcol endcol args)
134 (forward-line 1)))
135 ))
136
137(defun delete-rectangle-line (startcol endcol fill)
138 (let ((pt (point-at-eol)))
139 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
140 (if (and (not fill) (<= pt endcol))
141 (delete-region (point) pt)
142 ;; else
143 (setq pt (point))
144 (move-to-column-force endcol)
145 (delete-region pt (point))))
146 ))
147
148(defun delete-extract-rectangle-line (startcol endcol lines fill)
149 (let ((pt (point-at-eol)))
150 (if (< (move-to-column-force startcol (or fill 'coerce)) startcol)
151 (setcdr lines (cons (spaces-string (- endcol startcol))
152 (cdr lines)))
153 ;; else
154 (setq pt (point))
155 (move-to-column-force endcol)
156 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
157 (delete-region pt (point)))
158 ))
159
160;; ### NOTE: this is actually the only function that needs to do complicated
161;; stuff like what's happening in `operate-on-rectangle', because the buffer
162;; might be read-only. --dv
163(defun extract-rectangle-line (startcol endcol lines)
164 (let (start end begextra endextra line)
165 (move-to-column startcol)
166 (setq start (point)
167 begextra (- (current-column) startcol))
168 (move-to-column endcol)
169 (setq end (point)
170 endextra (- endcol (current-column)))
171 (setq line (buffer-substring start (point)))
172 (if (< begextra 0)
173 (setq endextra (+ endextra begextra)
174 begextra 0))
175 (if (< endextra 0)
176 (setq endextra 0))
177 (goto-char start)
110 (while (search-forward "\t" end t) 178 (while (search-forward "\t" end t)
111 (let ((width (- (current-column) 179 (let ((width (- (current-column)
112 (save-excursion (forward-char -1) 180 (save-excursion (forward-char -1)
113 (current-column))))) 181 (current-column)))))
114 (setq line (concat (substring line 0 (- (point) end 1)) 182 (setq line (concat (substring line 0 (- (point) end 1))
115 (spaces-string width) 183 (spaces-string width)
116 (substring line (+ (length line) (- (point) end))))))) 184 (substring line (+ (length line)
185 (- (point) end)))))))
117 (if (or (> begextra 0) (> endextra 0)) 186 (if (or (> begextra 0) (> endextra 0))
118 (setq line (concat (spaces-string begextra) 187 (setq line (concat (spaces-string begextra)
119 line 188 line
120 (spaces-string endextra)))) 189 (spaces-string endextra))))
121 (setq operate-on-rectangle-lines (cons line operate-on-rectangle-lines)))) 190 (setcdr lines (cons line (cdr lines)))))
122 191
123(defconst spaces-strings 192(defconst spaces-strings
124 '["" " " " " " " " " " " " " " " " "]) 193 '["" " " " " " " " " " " " " " " " "])
125 194
195;; this one is untouched --dv
126(defun spaces-string (n) 196(defun spaces-string (n)
127 (if (<= n 8) (aref spaces-strings n) 197 (if (<= n 8) (aref spaces-strings n)
128 (let ((val "")) 198 (let ((val ""))
@@ -132,52 +202,61 @@ Point is at the end of the segment of this line within the rectangle."
132 (concat val (aref spaces-strings n))))) 202 (concat val (aref spaces-strings n)))))
133 203
134;;;###autoload 204;;;###autoload
135(defun delete-rectangle (start end) 205(defun delete-rectangle (start end &optional fill)
136 "Delete (don't save) text in rectangle with point and mark as corners. 206 "Delete (don't save) text in rectangle with corners at point and mark (START
137The same range of columns is deleted in each line starting with the line 207and END when called from a program). The same range of columns is deleted in
138where the region begins and ending with the line where the region ends." 208each line starting with the line where the region begins and ending with the
139 (interactive "r") 209line where the region ends.
140 (operate-on-rectangle 'delete-rectangle-line start end t)) 210
211With a prefix (or a FILL) argument, also fill lines where nothing has to be
212deleted."
213 (interactive "r\nP")
214 (apply-on-rectangle 'delete-rectangle-line start end fill))
141 215
142;;;###autoload 216;;;###autoload
143(defun delete-extract-rectangle (start end) 217(defun delete-extract-rectangle (start end &optional fill)
144 "Delete contents of rectangle and return it as a list of strings. 218 "Delete the contents of the rectangle with corners at START and END, and
145Arguments START and END are the corners of the rectangle. 219return it as a list of strings, one for each line of the rectangle.
146The value is list of strings, one for each line of the rectangle." 220
147 (let (operate-on-rectangle-lines) 221With an optional FILL argument, also fill lines where nothing has to be
148 (operate-on-rectangle 'delete-extract-rectangle-line 222deleted."
149 start end t) 223 (let ((lines (list nil)))
150 (nreverse operate-on-rectangle-lines))) 224 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
225 (nreverse (cdr lines))))
151 226
152;;;###autoload 227;;;###autoload
153(defun extract-rectangle (start end) 228(defun extract-rectangle (start end)
154 "Return contents of rectangle with corners at START and END. 229 "Return the contents of the rectangle with corners at START and END,
155Value is list of strings, one for each line of the rectangle." 230as a list of strings, one for each line of the rectangle."
156 (let (operate-on-rectangle-lines) 231 (let ((lines (list nil)))
157 (operate-on-rectangle 'extract-rectangle-line start end nil) 232 (apply-on-rectangle 'extract-rectangle-line start end lines)
158 (nreverse operate-on-rectangle-lines))) 233 (nreverse (cdr lines))))
159 234
160(defvar killed-rectangle nil 235(defvar killed-rectangle nil
161 "Rectangle for yank-rectangle to insert.") 236 "Rectangle for yank-rectangle to insert.")
162 237
163;;;###autoload 238;;;###autoload
164(defun kill-rectangle (start end) 239(defun kill-rectangle (start end &optional fill)
165 "Delete rectangle with corners at point and mark; save as last killed one. 240 "Delete the rectangle with corners at point and mark (START and END when
166Calling from program, supply two args START and END, buffer positions. 241called from a program) and save it as the last killed one. You might prefer to
167But in programs you might prefer to use `delete-extract-rectangle'." 242use `delete-extract-rectangle' from a program.
168 (interactive "r") 243
169 (if buffer-read-only 244With a prefix (or a FILL) argument, also fill lines where nothing has to be
170 (progn 245deleted."
171 (setq killed-rectangle (extract-rectangle start end)) 246 (interactive "r\nP")
172 (barf-if-buffer-read-only))) 247 (when buffer-read-only
173 (setq killed-rectangle (delete-extract-rectangle start end))) 248 (setq killed-rectangle (extract-rectangle start end))
174 249 (barf-if-buffer-read-only))
250 (setq killed-rectangle (delete-extract-rectangle start end fill)))
251
252;; this one is untouched --dv
175;;;###autoload 253;;;###autoload
176(defun yank-rectangle () 254(defun yank-rectangle ()
177 "Yank the last killed rectangle with upper left corner at point." 255 "Yank the last killed rectangle with upper left corner at point."
178 (interactive) 256 (interactive)
179 (insert-rectangle killed-rectangle)) 257 (insert-rectangle killed-rectangle))
180 258
259;; this one is untoutched --dv
181;;;###autoload 260;;;###autoload
182(defun insert-rectangle (rectangle) 261(defun insert-rectangle (rectangle)
183 "Insert text of RECTANGLE with upper left corner at point. 262 "Insert text of RECTANGLE with upper left corner at point.
@@ -201,96 +280,87 @@ and point is at the lower right corner."
201 (setq lines (cdr lines))))) 280 (setq lines (cdr lines)))))
202 281
203;;;###autoload 282;;;###autoload
204(defun open-rectangle (start end) 283(defun open-rectangle (start end &optional fill)
205 "Blank out rectangle with corners at point and mark, shifting text right. 284 "Blank out rectangle with corners at point and mark (START and END when
206The text previously in the region is not overwritten by the blanks, 285called from a program), shifting text right. The text previously in the region
207but instead winds up to the right of the rectangle." 286is not overwritten by the blanks, but instead winds up to the right of the
208 (interactive "r") 287rectangle.
209 (operate-on-rectangle 'open-rectangle-line start end nil) 288
289With a prefix (or a FILL) argument, fill with blanks even if there is no text
290on the right side of the rectangle."
291 (interactive "r\nP")
292 (apply-on-rectangle 'open-rectangle-line start end fill)
210 (goto-char start)) 293 (goto-char start))
211 294
212(defun open-rectangle-line (startpos begextra endextra) 295(defun open-rectangle-line (startcol endcol fill)
213 ;; Column where rectangle ends. 296 (let (spaces)
214 (let ((endcol (+ (current-column) endextra)) 297 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
215 whitewidth) 298 (unless (and (not fill)
216 (goto-char startpos) 299 (= (point) (point-at-eol)))
217 ;; Column where rectangle begins. 300 (indent-to endcol)))
218 (let ((begcol (- (current-column) begextra))) 301 ))
219 (if (> begextra 0) 302
220 (move-to-column-force begcol)) 303(defun delete-whitespace-rectangle-line (startcol endcol fill)
221 (skip-chars-forward " \t") 304 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
222 ;; Width of whitespace to be deleted and recreated. 305 (unless (= (point) (point-at-eol))
223 (setq whitewidth (- (current-column) begcol))) 306 (delete-region (point) (progn (skip-syntax-forward " ") (point))))
224 ;; Delete the whitespace following the start column. 307 ))
225 (delete-region startpos (point))
226 ;; Open the desired width, plus same amount of whitespace we just deleted.
227 (indent-to (+ endcol whitewidth))))
228 308
229;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name 309;;;###autoload (defalias 'close-rectangle 'delete-whitespace-rectangle) ;; Old name
230;;;###autoload 310;;;###autoload
231(defun delete-whitespace-rectangle (start end) 311(defun delete-whitespace-rectangle (start end &optional fill)
232 "Delete all whitespace following a specified column in each line. 312 "Delete all whitespace following a specified column in each line.
233The left edge of the rectangle specifies the position in each line 313The left edge of the rectangle specifies the position in each line
234at which whitespace deletion should begin. On each line in the 314at which whitespace deletion should begin. On each line in the
235rectangle, all continuous whitespace starting at that column is deleted." 315rectangle, all continuous whitespace starting at that column is deleted.
236 (interactive "r")
237 (operate-on-rectangle '(lambda (startpos begextra endextra)
238 (save-excursion
239 (goto-char startpos)
240 (delete-region (point)
241 (progn
242 (skip-syntax-forward " ")
243 (point)))))
244 start end t))
245 316
317With a prefix (or a FILL) argument, also fill too short lines."
318 (interactive "r\nP")
319 (apply-on-rectangle 'delete-whitespace-rectangle-line start end fill))
320
321;; not used any more --dv
246;; string-rectangle uses this variable to pass the string 322;; string-rectangle uses this variable to pass the string
247;; to string-rectangle-line. 323;; to string-rectangle-line.
248(defvar string-rectangle-string) 324(defvar string-rectangle-string)
249 325
250;;;###autoload 326;;;###autoload
251(defun string-rectangle (start end string) 327(defun string-rectangle (start end string)
252 "Replace rectangle contents with STRING on each line. 328 "Insert STRING on each line of the rectangle with corners at point and mark
253The length of STRING need not be the same as the rectangle width. 329(START and END when called from a program), shifting text right. The left edge
254 330of the rectangle specifies the column for insertion. This command does not
255Called from a program, takes three args; START, END and STRING." 331delete or overwrite any existing text."
256 (interactive "r\nsString rectangle: ") 332 (interactive "r\nsString rectangle: ")
257 (let ((string-rectangle-string string)) 333 (apply-on-rectangle 'string-rectangle-line start end string))
258 (operate-on-rectangle 'string-rectangle-line start end t))) 334
259 335(defun string-rectangle-line (startcol endcol string)
260(defun string-rectangle-line (startpos begextra endextra) 336 (move-to-column-force startcol)
261 (let (whitespace) 337 (insert string))
262 ;; Delete the width of the rectangle.
263 (delete-region startpos (point))
264 ;; Compute horizontal width of following whitespace.
265 (let ((ocol (current-column)))
266 (skip-chars-forward " \t")
267 (setq whitespace (- (current-column) ocol)))
268 ;; Delete the following whitespace.
269 (delete-region startpos (point))
270 ;; Insert the desired string.
271 (insert string-rectangle-string)
272 ;; Insert the same width of whitespace that we had before.
273 (indent-to (+ (current-column) whitespace))))
274 338
275;;;###autoload 339;;;###autoload
276(defun clear-rectangle (start end) 340(defun clear-rectangle (start end &optional fill)
277 "Blank out rectangle with corners at point and mark. 341 "Blank out the rectangle with corners at point and mark (START and END when
278The text previously in the region is overwritten by the blanks. 342called from a program). The text previously in the region is overwritten with
279When called from a program, requires two args which specify the corners." 343blanks.
280 (interactive "r") 344
281 (operate-on-rectangle 'clear-rectangle-line start end t)) 345With a prefix (or a FILL) argument, also fill with blanks the parts of the
282 346rectangle which were empty."
283(defun clear-rectangle-line (startpos begextra endextra) 347 (interactive "r\nP")
284 ;; Find end of whitespace after the rectangle. 348 (apply-on-rectangle 'clear-rectangle-line start end fill))
285 (skip-chars-forward " \t") 349
286 (let ((column (+ (current-column) endextra))) 350(defun clear-rectangle-line (startcol endcol fill)
287 ;; Delete the text in the rectangle, and following whitespace. 351 (let ((pt (point-at-eol))
288 (delete-region (point) 352 spaces)
289 (progn (goto-char startpos) 353 (when (= (move-to-column-force startcol (or fill 'coerce)) startcol)
290 (skip-chars-backward " \t") 354 (if (and (not fill)
291 (point))) 355 (<= (save-excursion (goto-char pt) (current-column)) endcol))
292 ;; Reindent out to same column that we were at. 356 (delete-region (point) pt)
293 (indent-to column))) 357 ;; else
358 (setq pt (point))
359 (move-to-column-force endcol)
360 (setq spaces (- (point) pt))
361 (delete-region pt (point))
362 (indent-to (+ (current-column) spaces))))
363 ))
294 364
295(provide 'rect) 365(provide 'rect)
296 366