comparison lisp/rect.el @ 404:2f8bb876ab1d r21-2-32

Import from CVS: tag r21-2-32
author cvs
date Mon, 13 Aug 2007 11:16:07 +0200
parents 74fd4e045ea6
children de805c49cfc1
comparison
equal deleted inserted replaced
403:9f011ab08d48 404:2f8bb876ab1d
1 ;;; rect.el --- rectangle functions for XEmacs. 1 ;;; rect.el --- rectangle functions for XEmacs.
2 2
3 ;; Copyright (C) 1985, 1993, 1994, 1999 Free Software Foundation, Inc. 3 ;; Copyright (C) 1985-2000 Free Software Foundation, Inc.
4 4
5 ;; Maintainer: Didier Verna <verna@inf.enst.fr> 5 ;; Maintainer: Didier Verna <didier@xemacs.org>
6 ;; Keywords: internal 6 ;; Keywords: internal
7 7
8 ;; This file is part of XEmacs. 8 ;; This file is part of XEmacs.
9 9
10 ;; XEmacs is free software; you can redistribute it and/or modify it 10 ;; XEmacs is free software; you can redistribute it and/or modify it
28 28
29 ;; This package provides the operations on rectangles that are documented 29 ;; This package provides the operations on rectangles that are documented
30 ;; in the XEmacs Reference Manual. 30 ;; in the XEmacs Reference Manual.
31 31
32 ;; #### NOTE: this file has been almost completely rewritten by Didier Verna 32 ;; #### NOTE: this file has been almost completely rewritten by Didier Verna
33 ;; <verna@inf.enst.fr>, Jul 99. The purpose of this rewrite is to be less 33 ;; <didier@xemacs.org>, Jul 99. The purpose of this rewrite is to be less
34 ;; intrusive and fill lines with whitespaces only when needed. A few functions 34 ;; intrusive and fill lines with whitespaces only when needed. A few functions
35 ;; are untouched though, as noted above their definition. 35 ;; are untouched though, as noted above their definition.
36 36
37 37
38 ;;; Code: 38 ;;; Code:
109 (while (< (point) endpt) 109 (while (< (point) endpt)
110 (apply function startcol endcol args) 110 (apply function startcol endcol args)
111 (forward-line 1))) 111 (forward-line 1)))
112 )) 112 ))
113 113
114
115 (defun delete-rectangle-line (startcol endcol fill)
116 (let ((pt (point-at-eol)))
117 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
118 (if (and (not fill) (<= pt endcol))
119 (delete-region (point) pt)
120 ;; else
121 (setq pt (point))
122 (move-to-column endcol t)
123 (delete-region pt (point))))
124 ))
125
126 ;;;###autoload
127 (defun delete-rectangle (start end &optional fill)
128 "Delete the text in the region-rectangle without saving it.
129 The same range of columns is deleted in each line starting with the line
130 where the region begins and ending with the line where the region ends.
131
132 When called from a program, the rectangle's corners are START and END.
133 With a prefix (or FILL) argument, also fill lines where nothing has to be
134 deleted."
135 (interactive "*r\nP")
136 (apply-on-rectangle 'delete-rectangle-line start end fill))
137
138
114 ;; I love ascii art ;-) 139 ;; I love ascii art ;-)
115 (defconst spaces-strings '["" 140 (defconst spaces-strings '[""
116 " " 141 " "
117 " " 142 " "
118 " " 143 " "
120 " " 145 " "
121 " " 146 " "
122 " " 147 " "
123 " "]) 148 " "])
124 149
125
126 ;; This function is untouched --dv 150 ;; This function is untouched --dv
127 (defun spaces-string (n) 151 (defun spaces-string (n)
128 (if (<= n 8) (aref spaces-strings n) 152 (if (<= n 8) (aref spaces-strings n)
129 (let ((val "")) 153 (let ((val ""))
130 (while (> n 8) 154 (while (> n 8)
131 (setq val (concat " " val) 155 (setq val (concat " " val)
132 n (- n 8))) 156 n (- n 8)))
133 (concat val (aref spaces-strings n))))) 157 (concat val (aref spaces-strings n)))))
134 158
135 ;;;###autoload
136 (defvar killed-rectangle nil
137 "Rectangle for `yank-rectangle' to insert.")
138
139 ;;;###autoload
140 (defun kill-rectangle (start end &optional fill)
141 "Delete the region-rectangle and save it as the last killed one.
142 You might prefer to use `delete-extract-rectangle' from a program.
143
144 When called from a program, the rectangle's corners are START and END.
145 With a prefix (or FILL) argument, also fill lines where nothing has to be
146 deleted."
147 (interactive "*r\nP")
148 (when buffer-read-only
149 (setq killed-rectangle (extract-rectangle start end))
150 (barf-if-buffer-read-only))
151 (setq killed-rectangle (delete-extract-rectangle start end fill)))
152
153 ;;;###autoload
154 (defun delete-rectangle (start end &optional fill)
155 "Delete the text in the region-rectangle without saving it.
156 The same range of columns is deleted in each line starting with the line
157 where the region begins and ending with the line where the region ends.
158
159 When called from a program, the rectangle's corners are START and END.
160 With a prefix (or FILL) argument, also fill lines where nothing has to be
161 deleted."
162 (interactive "*r\nP")
163 (apply-on-rectangle 'delete-rectangle-line start end fill))
164
165 (defun delete-rectangle-line (startcol endcol fill)
166 (let ((pt (point-at-eol)))
167 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
168 (if (and (not fill) (<= pt endcol))
169 (delete-region (point) pt)
170 ;; else
171 (setq pt (point))
172 (move-to-column endcol t)
173 (delete-region pt (point))))
174 ))
175
176 ;;;###autoload
177 (defun delete-extract-rectangle (start end &optional fill)
178 "Delete the contents of the rectangle with corners at START and END, and
179 return it as a list of strings, one for each line of the rectangle.
180
181 With an optional FILL argument, also fill lines where nothing has to be
182 deleted."
183 (let ((lines (list nil)))
184 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
185 (nreverse (cdr lines))))
186 159
187 (defun delete-extract-rectangle-line (startcol endcol lines fill) 160 (defun delete-extract-rectangle-line (startcol endcol lines fill)
188 (let ((pt (point-at-eol))) 161 (let ((pt (point-at-eol)))
189 (if (< (move-to-column startcol (or fill 'coerce)) startcol) 162 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
190 (setcdr lines (cons (spaces-string (- endcol startcol)) 163 (setcdr lines (cons (spaces-string (- endcol startcol))
195 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines))) 168 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
196 (delete-region pt (point))) 169 (delete-region pt (point)))
197 )) 170 ))
198 171
199 ;;;###autoload 172 ;;;###autoload
200 (defun extract-rectangle (start end) 173 (defun delete-extract-rectangle (start end &optional fill)
201 "Return the contents of the rectangle with corners at START and END, 174 "Delete the contents of the rectangle with corners at START and END, and
202 as a list of strings, one for each line of the rectangle." 175 return it as a list of strings, one for each line of the rectangle.
176
177 With an optional FILL argument, also fill lines where nothing has to be
178 deleted."
203 (let ((lines (list nil))) 179 (let ((lines (list nil)))
204 (apply-on-rectangle 'extract-rectangle-line start end lines) 180 (apply-on-rectangle 'delete-extract-rectangle-line start end lines fill)
205 (nreverse (cdr lines)))) 181 (nreverse (cdr lines))))
182
206 183
207 ;; #### NOTE: this is actually the only function that needs to do complicated 184 ;; #### NOTE: this is actually the only function that needs to do complicated
208 ;; stuff like what's happening in `operate-on-rectangle', because the buffer 185 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
209 ;; might be read-only. --dv 186 ;; might be read-only. --dv
210 (defun extract-rectangle-line (startcol endcol lines) 187 (defun extract-rectangle-line (startcol endcol lines)
234 (setq line (concat (spaces-string begextra) 211 (setq line (concat (spaces-string begextra)
235 line 212 line
236 (spaces-string endextra)))) 213 (spaces-string endextra))))
237 (setcdr lines (cons line (cdr lines))))) 214 (setcdr lines (cons line (cdr lines)))))
238 215
216 ;;;###autoload
217 (defun extract-rectangle (start end)
218 "Return the contents of the rectangle with corners at START and END,
219 as a list of strings, one for each line of the rectangle."
220 (let ((lines (list nil)))
221 (apply-on-rectangle 'extract-rectangle-line start end lines)
222 (nreverse (cdr lines))))
223
224
225 ;;;###autoload
226 (defvar killed-rectangle nil
227 "Rectangle for `yank-rectangle' to insert.")
228
229 ;;;###autoload
230 (defun kill-rectangle (start end &optional fill)
231 "Delete the region-rectangle and save it as the last killed one.
232 You might prefer to use `delete-extract-rectangle' from a program.
233
234 When called from a program, the rectangle's corners are START and END.
235 With a prefix (or FILL) argument, also fill lines where nothing has to be
236 deleted."
237 (interactive "*r\nP")
238 (when buffer-read-only
239 (setq killed-rectangle (extract-rectangle start end))
240 (barf-if-buffer-read-only))
241 (setq killed-rectangle (delete-extract-rectangle start end fill)))
242
239 ;; This function is untouched --dv 243 ;; This function is untouched --dv
240 ;;;###autoload 244 ;;;###autoload
241 (defun yank-rectangle () 245 (defun yank-rectangle ()
242 "Yank the last killed rectangle with upper left corner at point." 246 "Yank the last killed rectangle with upper left corner at point."
243 (interactive "*") 247 (interactive "*")
244 (insert-rectangle killed-rectangle)) 248 (insert-rectangle killed-rectangle))
249
245 250
246 ;; This function is untouched --dv 251 ;; This function is untouched --dv
247 ;;;###autoload 252 ;;;###autoload
248 (defun insert-rectangle (rectangle) 253 (defun insert-rectangle (rectangle)
249 "Insert text of RECTANGLE with upper left corner at point. 254 "Insert text of RECTANGLE with upper left corner at point.
264 (move-to-column insertcolumn t))) 269 (move-to-column insertcolumn t)))
265 (setq first nil) 270 (setq first nil)
266 (insert (car lines)) 271 (insert (car lines))
267 (setq lines (cdr lines))))) 272 (setq lines (cdr lines)))))
268 273
274
275 (defun open-rectangle-line (startcol endcol fill)
276 (let (spaces)
277 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
278 (unless (and (not fill)
279 (= (point) (point-at-eol)))
280 (indent-to endcol)))
281 ))
282
269 ;;;###autoload 283 ;;;###autoload
270 (defun open-rectangle (start end &optional fill) 284 (defun open-rectangle (start end &optional fill)
271 "Blank out the region-rectangle, shifting text right. 285 "Blank out the region-rectangle, shifting text right.
272 286
273 When called from a program, the rectangle's corners are START and END. 287 When called from a program, the rectangle's corners are START and END.
275 on the right side of the rectangle." 289 on the right side of the rectangle."
276 (interactive "*r\nP") 290 (interactive "*r\nP")
277 (apply-on-rectangle 'open-rectangle-line start end fill) 291 (apply-on-rectangle 'open-rectangle-line start end fill)
278 (goto-char start)) 292 (goto-char start))
279 293
280 (defun open-rectangle-line (startcol endcol fill) 294
281 (let (spaces) 295 (defun string-rectangle-line (startcol endcol string delete)
282 (when (= (move-to-column startcol (or fill 'coerce)) startcol) 296 (move-to-column startcol t)
283 (unless (and (not fill) 297 (if delete
284 (= (point) (point-at-eol))) 298 (delete-rectangle-line startcol endcol nil))
285 (indent-to endcol))) 299 (insert string))
286 ))
287 300
288 ;;;###autoload 301 ;;;###autoload
289 (defun string-rectangle (start end string) 302 (defun string-rectangle (start end string)
290 "Insert STRING on each line of the region-rectangle, shifting text right. 303 "Insert STRING on each line of the region-rectangle, shifting text right.
291 The left edge of the rectangle specifies the column for insertion. This 304 The left edge of the rectangle specifies the column for insertion.
292 command does not delete or overwrite any existing text. 305
306 If `pending-delete-mode' is active the string replace the region.
307 Otherwise this command does not delete or overwrite any existing text.
293 308
294 When called from a program, the rectangle's corners are START and END." 309 When called from a program, the rectangle's corners are START and END."
295 (interactive "*r\nsString rectangle: ") 310 (interactive "*r\nsString rectangle: ")
296 (apply-on-rectangle 'string-rectangle-line start end string)) 311 (apply-on-rectangle 'string-rectangle-line start end string
297 312 (and (boundp 'pending-delete-mode) pending-delete-mode)))
298 (defun string-rectangle-line (startcol endcol string) 313
299 (move-to-column startcol t) 314 (defun replace-rectangle (start end string)
300 (insert string)) 315 "Like `string-rectangle', but unconditionally replace the original region,
301 316 as if `pending-delete-mode' were active."
302 ;;;###autoload 317 (interactive "*r\nsString rectangle: ")
303 (defun clear-rectangle (start end &optional fill) 318 (apply-on-rectangle 'string-rectangle-line start end string t))
304 "Blank out the region-rectangle. 319
305 The text previously in the region is overwritten with blanks.
306
307 When called from a program, the rectangle's corners are START and END.
308 With a prefix (or FILL) argument, also fill with blanks the parts of the
309 rectangle which were empty."
310 (interactive "*r\nP")
311 (apply-on-rectangle 'clear-rectangle-line start end fill))
312 320
313 (defun clear-rectangle-line (startcol endcol fill) 321 (defun clear-rectangle-line (startcol endcol fill)
314 (let ((pt (point-at-eol)) 322 (let ((pt (point-at-eol))
315 spaces) 323 spaces)
316 (when (= (move-to-column startcol (or fill 'coerce)) startcol) 324 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
323 (setq spaces (- (point) pt)) 331 (setq spaces (- (point) pt))
324 (delete-region pt (point)) 332 (delete-region pt (point))
325 (indent-to (+ (current-column) spaces)))) 333 (indent-to (+ (current-column) spaces))))
326 )) 334 ))
327 335
336 ;;;###autoload
337 (defun clear-rectangle (start end &optional fill)
338 "Blank out the region-rectangle.
339 The text previously in the region is overwritten with blanks.
340
341 When called from a program, the rectangle's corners are START and END.
342 With a prefix (or FILL) argument, also fill with blanks the parts of the
343 rectangle which were empty."
344 (interactive "*r\nP")
345 (apply-on-rectangle 'clear-rectangle-line start end fill))
346
347
328 (provide 'rect) 348 (provide 'rect)
329 349
330 ;;; rect.el ends here 350 ;;; rect.el ends here