comparison lisp/rect.el @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 8de8e3f6228a
children 1ccc32a20af4
comparison
equal deleted inserted replaced
441:72a7cfa4a488 442:abe6d1db359e
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 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
277 (unless (and (not fill)
278 (= (point) (point-at-eol)))
279 (indent-to endcol))))
280
269 ;;;###autoload 281 ;;;###autoload
270 (defun open-rectangle (start end &optional fill) 282 (defun open-rectangle (start end &optional fill)
271 "Blank out the region-rectangle, shifting text right. 283 "Blank out the region-rectangle, shifting text right.
272 284
273 When called from a program, the rectangle's corners are START and END. 285 When called from a program, the rectangle's corners are START and END.
275 on the right side of the rectangle." 287 on the right side of the rectangle."
276 (interactive "*r\nP") 288 (interactive "*r\nP")
277 (apply-on-rectangle 'open-rectangle-line start end fill) 289 (apply-on-rectangle 'open-rectangle-line start end fill)
278 (goto-char start)) 290 (goto-char start))
279 291
280 (defun open-rectangle-line (startcol endcol fill) 292
281 (let (spaces) 293 (defun string-rectangle-line (startcol endcol string delete)
282 (when (= (move-to-column startcol (or fill 'coerce)) startcol) 294 (move-to-column startcol t)
283 (unless (and (not fill) 295 (if delete
284 (= (point) (point-at-eol))) 296 (delete-rectangle-line startcol endcol nil))
285 (indent-to endcol))) 297 (insert string))
286 ))
287 298
288 ;;;###autoload 299 ;;;###autoload
289 (defun string-rectangle (start end string) 300 (defun string-rectangle (start end string)
290 "Insert STRING on each line of the region-rectangle, shifting text right. 301 "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 302 The left edge of the rectangle specifies the column for insertion.
292 command does not delete or overwrite any existing text. 303
304 If `pending-delete-mode' is active the string replace the region.
305 Otherwise this command does not delete or overwrite any existing text.
293 306
294 When called from a program, the rectangle's corners are START and END." 307 When called from a program, the rectangle's corners are START and END."
295 (interactive "*r\nsString rectangle: ") 308 (interactive "*r\nsString rectangle: ")
296 (apply-on-rectangle 'string-rectangle-line start end string)) 309 (defvar pending-delete-mode)
297 310 (apply-on-rectangle 'string-rectangle-line start end string
298 (defun string-rectangle-line (startcol endcol string) 311 (and (boundp 'pending-delete-mode) pending-delete-mode)))
299 (move-to-column startcol t) 312
300 (insert string)) 313 (defun replace-rectangle (start end string)
301 314 "Like `string-rectangle', but unconditionally replace the original region,
302 ;;;###autoload 315 as if `pending-delete-mode' were active."
303 (defun clear-rectangle (start end &optional fill) 316 (interactive "*r\nsString rectangle: ")
304 "Blank out the region-rectangle. 317 (apply-on-rectangle 'string-rectangle-line start end string t))
305 The text previously in the region is overwritten with blanks. 318
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 319
313 (defun clear-rectangle-line (startcol endcol fill) 320 (defun clear-rectangle-line (startcol endcol fill)
314 (let ((pt (point-at-eol)) 321 (let ((pt (point-at-eol))
315 spaces) 322 spaces)
316 (when (= (move-to-column startcol (or fill 'coerce)) startcol) 323 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
323 (setq spaces (- (point) pt)) 330 (setq spaces (- (point) pt))
324 (delete-region pt (point)) 331 (delete-region pt (point))
325 (indent-to (+ (current-column) spaces)))) 332 (indent-to (+ (current-column) spaces))))
326 )) 333 ))
327 334
335 ;;;###autoload
336 (defun clear-rectangle (start end &optional fill)
337 "Blank out the region-rectangle.
338 The text previously in the region is overwritten with blanks.
339
340 When called from a program, the rectangle's corners are START and END.
341 With a prefix (or FILL) argument, also fill with blanks the parts of the
342 rectangle which were empty."
343 (interactive "*r\nP")
344 (apply-on-rectangle 'clear-rectangle-line start end fill))
345
346
328 (provide 'rect) 347 (provide 'rect)
329 348
330 ;;; rect.el ends here 349 ;;; rect.el ends here