comparison lisp/rect.el @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 262b8bb4a523
children 2f8bb876ab1d
comparison
equal deleted inserted replaced
397:f4aeb21a5bad 398:74fd4e045ea6
1 ;;; rect.el --- rectangle functions for XEmacs. 1 ;;; rect.el --- rectangle functions for XEmacs.
2 2
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc. 3 ;; Copyright (C) 1985, 1993, 1994, 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 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
20 ;; You should have received a copy of the GNU General Public License 20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free 21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA. 23 ;; 02111-1307, USA.
24 24
25 ;;; Synched up with: FSF 19.34. 25 ;;; Synched up with: to be incorporated in a forthcoming GNU Emacs
26 26
27 ;;; Commentary: 27 ;;; Commentary:
28 28
29 ;; This package provides the operations on rectangles that are ocumented 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
33 ;; <verna@inf.enst.fr>, Jul 99. The purpose of this rewrite is to be less
34 ;; intrusive and fill lines with whitespaces only when needed. A few functions
35 ;; are untouched though, as noted above their definition.
36
37
32 ;;; Code: 38 ;;; Code:
33 39
40 ;; #### NOTE: this function is untouched, but not used anymore.
41 ;; `apply-on-rectangle' is used instead. It's still there because it's
42 ;; documented so people might use it in their code, so I've decided not to
43 ;; touch it. --dv
34 ;; XEmacs: extra-args 44 ;; XEmacs: extra-args
35 (defun operate-on-rectangle (function start end coerce-tabs &rest extra-args) 45 (defun operate-on-rectangle (function start end coerce-tabs &rest extra-args)
36 "Call FUNCTION for each line of rectangle with corners at START, END. 46 "Call FUNCTION for each line of rectangle with corners at START, END.
37 If COERCE-TABS is non-nil, convert multi-column characters 47 If COERCE-TABS is non-nil, convert multi-column characters
38 that span the starting or ending columns on any line 48 that span the starting or ending columns on any line
42 number of columns that belong to rectangle but are before that position, 52 number of columns that belong to rectangle but are before that position,
43 number of columns that belong to rectangle but are after point. 53 number of columns that belong to rectangle but are after point.
44 Point is at the end of the segment of this line within the rectangle." 54 Point is at the end of the segment of this line within the rectangle."
45 (let (startcol startlinepos endcol endlinepos) 55 (let (startcol startlinepos endcol endlinepos)
46 (save-excursion 56 (save-excursion
47 (goto-char start) 57 (goto-char start)
48 (setq startcol (current-column)) 58 (setq startcol (current-column))
49 (beginning-of-line) 59 (beginning-of-line)
50 (setq startlinepos (point))) 60 (setq startlinepos (point)))
51 (save-excursion 61 (save-excursion
52 (goto-char end) 62 (goto-char end)
53 (setq endcol (current-column)) 63 (setq endcol (current-column))
54 (forward-line 1) 64 (forward-line 1)
55 (setq endlinepos (point-marker))) 65 (setq endlinepos (point-marker)))
56 (if (< endcol startcol) 66 (if (< endcol startcol)
57 ;; XEmacs 67 ;; XEmacs
58 (let ((tem startcol)) 68 (let ((tem startcol))
59 (setq startcol endcol endcol tem))) 69 (setq startcol endcol endcol tem)))
60 (save-excursion 70 (save-excursion
72 (if (< endextra 0) (setq endextra 0)) 82 (if (< endextra 0) (setq endextra 0))
73 (apply function startpos begextra endextra extra-args)) 83 (apply function startpos begextra endextra extra-args))
74 (forward-line 1))) 84 (forward-line 1)))
75 (- endcol startcol))) 85 (- endcol startcol)))
76 86
77 (defun delete-rectangle-line (startdelpos ignore ignore) 87 ;; The replacement for `operate-on-rectangle' -- dv
78 (delete-region startdelpos (point))) 88 (defun apply-on-rectangle (function start end &rest args)
79 89 "Call FUNCTION for each line of rectangle with corners at START and END.
80 ;; XEmacs: added lines arg 90 FUNCTION is called with two arguments: the start and end columns of the
81 (defun delete-extract-rectangle-line (startdelpos begextra endextra lines) 91 rectangle, plus ARGS extra arguments. Point is at the beginning of line
82 (save-excursion 92 when the function is called."
83 (extract-rectangle-line startdelpos begextra endextra lines)) 93 (let (startcol startpt endcol endpt)
84 (delete-region startdelpos (point))) 94 (save-excursion
85 95 (goto-char start)
86 ;; XEmacs: added lines arg 96 (setq startcol (current-column))
87 (defun extract-rectangle-line (startdelpos begextra endextra lines) 97 (beginning-of-line)
88 (let ((line (buffer-substring startdelpos (point))) 98 (setq startpt (point))
89 (end (point))) 99 (goto-char end)
90 (goto-char startdelpos) 100 (setq endcol (current-column))
91 (while (search-forward "\t" end t) 101 (forward-line 1)
92 (let ((width (- (current-column) 102 (setq endpt (point-marker))
93 (save-excursion (forward-char -1) 103 ;; ensure the start column is the left one.
94 (current-column))))) 104 (if (< endcol startcol)
95 (setq line (concat (substring line 0 (- (point) end 1)) 105 (let ((col startcol))
96 (spaces-string width) 106 (setq startcol endcol endcol col)))
97 (substring line (+ (length line) (- (point) end))))))) 107 ;; start looping over lines
98 (if (or (> begextra 0) (> endextra 0)) 108 (goto-char startpt)
99 (setq line (concat (spaces-string begextra) 109 (while (< (point) endpt)
100 line 110 (apply function startcol endcol args)
101 (spaces-string endextra)))) 111 (forward-line 1)))
102 (setcdr lines (cons line (cdr lines))))) ; XEmacs 112 ))
103 113
104 (defconst spaces-strings 114 ;; I love ascii art ;-)
105 (purecopy '["" " " " " " " " " " " " " " " " "])) 115 (defconst spaces-strings '[""
106 116 " "
117 " "
118 " "
119 " "
120 " "
121 " "
122 " "
123 " "])
124
125
126 ;; This function is untouched --dv
107 (defun spaces-string (n) 127 (defun spaces-string (n)
108 (if (<= n 8) (aref spaces-strings n) 128 (if (<= n 8) (aref spaces-strings n)
109 (let ((val "")) 129 (let ((val ""))
110 (while (> n 8) 130 (while (> n 8)
111 (setq val (concat " " val) 131 (setq val (concat " " val)
112 n (- n 8))) 132 n (- n 8)))
113 (concat val (aref spaces-strings n))))) 133 (concat val (aref spaces-strings n)))))
114 134
115 ;;;###autoload 135 ;;;###autoload
116 (defun delete-rectangle (start end) 136 (defvar killed-rectangle nil
117 "Delete (don't save) text in rectangle with point and mark as corners. 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.
118 The same range of columns is deleted in each line starting with the line 156 The same range of columns is deleted in each line starting with the line
119 where the region begins and ending with the line where the region ends." 157 where the region begins and ending with the line where the region ends.
120 (interactive "r") 158
121 (operate-on-rectangle 'delete-rectangle-line start end t)) 159 When called from a program, the rectangle's corners are START and END.
122 160 With a prefix (or FILL) argument, also fill lines where nothing has to be
123 ;;;###autoload 161 deleted."
124 (defun delete-extract-rectangle (start end) 162 (interactive "*r\nP")
125 "Delete contents of rectangle and return it as a list of strings. 163 (apply-on-rectangle 'delete-rectangle-line start end fill))
126 Arguments START and END are the corners of the rectangle. 164
127 The value is list of strings, one for each line of the rectangle." 165 (defun delete-rectangle-line (startcol endcol fill)
128 (let ((lines (list nil))) ; XEmacs change 166 (let ((pt (point-at-eol)))
129 (operate-on-rectangle 'delete-extract-rectangle-line 167 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
130 start end t lines) 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)
131 (nreverse (cdr lines)))) 185 (nreverse (cdr lines))))
132 186
187 (defun delete-extract-rectangle-line (startcol endcol lines fill)
188 (let ((pt (point-at-eol)))
189 (if (< (move-to-column startcol (or fill 'coerce)) startcol)
190 (setcdr lines (cons (spaces-string (- endcol startcol))
191 (cdr lines)))
192 ;; else
193 (setq pt (point))
194 (move-to-column endcol t)
195 (setcdr lines (cons (buffer-substring pt (point)) (cdr lines)))
196 (delete-region pt (point)))
197 ))
198
133 ;;;###autoload 199 ;;;###autoload
134 (defun extract-rectangle (start end) 200 (defun extract-rectangle (start end)
135 "Return contents of rectangle with corners at START and END. 201 "Return the contents of the rectangle with corners at START and END,
136 Value is list of strings, one for each line of the rectangle." 202 as a list of strings, one for each line of the rectangle."
137 (let ((lines (list nil))) ; XEmacs change 203 (let ((lines (list nil)))
138 (operate-on-rectangle 'extract-rectangle-line start end nil lines) 204 (apply-on-rectangle 'extract-rectangle-line start end lines)
139 (nreverse (cdr lines)))) 205 (nreverse (cdr lines))))
140 206
141 ;;;###autoload 207 ;; #### NOTE: this is actually the only function that needs to do complicated
142 (defvar killed-rectangle nil 208 ;; stuff like what's happening in `operate-on-rectangle', because the buffer
143 "Rectangle for yank-rectangle to insert.") 209 ;; might be read-only. --dv
144 210 (defun extract-rectangle-line (startcol endcol lines)
145 ;;;###autoload 211 (let (start end begextra endextra line)
146 (defun kill-rectangle (start end) 212 (move-to-column startcol)
147 "Delete rectangle with corners at point and mark; save as last killed one. 213 (setq start (point)
148 Calling from program, supply two args START and END, buffer positions. 214 begextra (- (current-column) startcol))
149 But in programs you might prefer to use `delete-extract-rectangle'." 215 (move-to-column endcol)
150 (interactive "r") 216 (setq end (point)
151 (if buffer-read-only 217 endextra (- endcol (current-column)))
152 (progn 218 (setq line (buffer-substring start (point)))
153 (setq killed-rectangle (extract-rectangle start end)) 219 (if (< begextra 0)
154 (barf-if-buffer-read-only))) 220 (setq endextra (+ endextra begextra)
155 (setq killed-rectangle (delete-extract-rectangle start end))) 221 begextra 0))
156 222 (if (< endextra 0)
223 (setq endextra 0))
224 (goto-char start)
225 (while (search-forward "\t" end t)
226 (let ((width (- (current-column)
227 (save-excursion (forward-char -1)
228 (current-column)))))
229 (setq line (concat (substring line 0 (- (point) end 1))
230 (spaces-string width)
231 (substring line (+ (length line)
232 (- (point) end)))))))
233 (if (or (> begextra 0) (> endextra 0))
234 (setq line (concat (spaces-string begextra)
235 line
236 (spaces-string endextra))))
237 (setcdr lines (cons line (cdr lines)))))
238
239 ;; This function is untouched --dv
157 ;;;###autoload 240 ;;;###autoload
158 (defun yank-rectangle () 241 (defun yank-rectangle ()
159 "Yank the last killed rectangle with upper left corner at point." 242 "Yank the last killed rectangle with upper left corner at point."
160 (interactive) 243 (interactive "*")
161 (insert-rectangle killed-rectangle)) 244 (insert-rectangle killed-rectangle))
162 245
246 ;; This function is untouched --dv
163 ;;;###autoload 247 ;;;###autoload
164 (defun insert-rectangle (rectangle) 248 (defun insert-rectangle (rectangle)
165 "Insert text of RECTANGLE with upper left corner at point. 249 "Insert text of RECTANGLE with upper left corner at point.
166 RECTANGLE's first line is inserted at point, its second 250 RECTANGLE's first line is inserted at point, its second
167 line is inserted at a point vertically under point, etc. 251 line is inserted at a point vertically under point, etc.
173 (first t)) 257 (first t))
174 (push-mark) 258 (push-mark)
175 (while lines 259 (while lines
176 (or first 260 (or first
177 (progn 261 (progn
178 (forward-line 1) 262 (forward-line 1)
179 (or (bolp) (insert ?\n)) 263 (or (bolp) (insert ?\n))
180 (move-to-column insertcolumn t))) 264 (move-to-column insertcolumn t)))
181 (setq first nil) 265 (setq first nil)
182 (insert (car lines)) 266 (insert (car lines))
183 (setq lines (cdr lines))))) 267 (setq lines (cdr lines)))))
184 268
185 ;;;###autoload 269 ;;;###autoload
186 (defun open-rectangle (start end) 270 (defun open-rectangle (start end &optional fill)
187 "Blank out rectangle with corners at point and mark, shifting text right. 271 "Blank out the region-rectangle, shifting text right.
188 The text previously in the region is not overwritten by the blanks, 272
189 but instead winds up to the right of the rectangle." 273 When called from a program, the rectangle's corners are START and END.
190 (interactive "r") 274 With a prefix (or FILL) argument, fill with blanks even if there is no text
191 (operate-on-rectangle 'open-rectangle-line start end nil) 275 on the right side of the rectangle."
276 (interactive "*r\nP")
277 (apply-on-rectangle 'open-rectangle-line start end fill)
192 (goto-char start)) 278 (goto-char start))
193 279
194 (defun open-rectangle-line (startpos begextra endextra) 280 (defun open-rectangle-line (startcol endcol fill)
195 ;; Column where rectangle ends. 281 (let (spaces)
196 (let ((endcol (+ (current-column) endextra)) 282 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
197 whitewidth) 283 (unless (and (not fill)
198 (goto-char startpos) 284 (= (point) (point-at-eol)))
199 ;; Column where rectangle begins. 285 (indent-to endcol)))
200 (let ((begcol (- (current-column) begextra))) 286 ))
201 (skip-chars-forward " \t")
202 ;; Width of whitespace to be deleted and recreated.
203 (setq whitewidth (- (current-column) begcol)))
204 ;; Delete the whitespace following the start column.
205 (delete-region startpos (point))
206 ;; Open the desired width, plus same amount of whitespace we just deleted.
207 (indent-to (+ endcol whitewidth))))
208 287
209 ;;;###autoload 288 ;;;###autoload
210 (defun string-rectangle (start end string) 289 (defun string-rectangle (start end string)
211 "Insert STRING on each line of the region-rectangle, shifting text right. 290 "Insert STRING on each line of the region-rectangle, shifting text right.
212 The left edge of the rectangle specifies the column for insertion. 291 The left edge of the rectangle specifies the column for insertion. This
213 This command does not delete or overwrite any existing text. 292 command does not delete or overwrite any existing text.
214 293
215 Called from a program, takes three args; START, END and STRING." 294 When called from a program, the rectangle's corners are START and END."
216 (interactive "r\nsString rectangle: ") 295 (interactive "*r\nsString rectangle: ")
217 (operate-on-rectangle 'string-rectangle-line start end t string)) ; XEmacs 296 (apply-on-rectangle 'string-rectangle-line start end string))
218 297
219 ;; XEmacs: add string arg 298 (defun string-rectangle-line (startcol endcol string)
220 (defun string-rectangle-line (startpos begextra endextra string) 299 (move-to-column startcol t)
221 (let (whitespace) 300 (insert string))
222 (goto-char startpos) 301
223 ;; Compute horizontal width of following whitespace. 302 ;;;###autoload
224 (let ((ocol (current-column))) 303 (defun clear-rectangle (start end &optional fill)
225 (skip-chars-forward " \t") 304 "Blank out the region-rectangle.
226 (setq whitespace (- (current-column) ocol))) 305 The text previously in the region is overwritten with blanks.
227 ;; Delete the following whitespace. 306
228 (delete-region startpos (point)) 307 When called from a program, the rectangle's corners are START and END.
229 ;; Insert the desired string. 308 With a prefix (or FILL) argument, also fill with blanks the parts of the
230 (insert string) 309 rectangle which were empty."
231 ;; Insert the same width of whitespace that we had before. 310 (interactive "*r\nP")
232 (indent-to (+ (current-column) whitespace)))) 311 (apply-on-rectangle 'clear-rectangle-line start end fill))
233 312
234 ;;;###autoload 313 (defun clear-rectangle-line (startcol endcol fill)
235 (defun clear-rectangle (start end) 314 (let ((pt (point-at-eol))
236 "Blank out rectangle with corners at point and mark. 315 spaces)
237 The text previously in the region is overwritten by the blanks. 316 (when (= (move-to-column startcol (or fill 'coerce)) startcol)
238 When called from a program, requires two args which specify the corners." 317 (if (and (not fill)
239 (interactive "r") 318 (<= (save-excursion (goto-char pt) (current-column)) endcol))
240 (operate-on-rectangle 'clear-rectangle-line start end t)) 319 (delete-region (point) pt)
241 320 ;; else
242 (defun clear-rectangle-line (startpos begextra endextra) 321 (setq pt (point))
243 ;; Find end of whitespace after the rectangle. 322 (move-to-column endcol t)
244 (skip-chars-forward " \t") 323 (setq spaces (- (point) pt))
245 (let ((column (+ (current-column) endextra))) 324 (delete-region pt (point))
246 ;; Delete the text in the rectangle, and following whitespace. 325 (indent-to (+ (current-column) spaces))))
247 (delete-region (point) 326 ))
248 (progn (goto-char startpos)
249 (skip-chars-backward " \t")
250 (point)))
251 ;; Reindent out to same column that we were at.
252 (indent-to column)))
253 327
254 (provide 'rect) 328 (provide 'rect)
255 329
256 ;;; rect.el ends here 330 ;;; rect.el ends here