Mercurial > hg > xemacs-beta
comparison lisp/rect.el @ 422:95016f13131a r21-2-19
Import from CVS: tag r21-2-19
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:25:01 +0200 |
parents | 697ef44129c6 |
children | 11054d720c21 |
comparison
equal
deleted
inserted
replaced
421:fff06e11db74 | 422:95016f13131a |
---|---|
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 ocumented |
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, 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 when |
82 (save-excursion | 92 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 | |
115 ;;;###autoload | |
116 (defun delete-rectangle (start end) | |
117 "Delete (don't save) text in rectangle with point and mark as corners. | |
118 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." | |
120 (interactive "r") | |
121 (operate-on-rectangle 'delete-rectangle-line start end t)) | |
122 | |
123 ;;;###autoload | |
124 (defun delete-extract-rectangle (start end) | |
125 "Delete contents of rectangle and return it as a list of strings. | |
126 Arguments START and END are the corners of the rectangle. | |
127 The value is list of strings, one for each line of the rectangle." | |
128 (let ((lines (list nil))) ; XEmacs change | |
129 (operate-on-rectangle 'delete-extract-rectangle-line | |
130 start end t lines) | |
131 (nreverse (cdr lines)))) | |
132 | |
133 ;;;###autoload | |
134 (defun extract-rectangle (start end) | |
135 "Return contents of rectangle with corners at START and END. | |
136 Value is list of strings, one for each line of the rectangle." | |
137 (let ((lines (list nil))) ; XEmacs change | |
138 (operate-on-rectangle 'extract-rectangle-line start end nil lines) | |
139 (nreverse (cdr lines)))) | |
140 | 134 |
141 ;;;###autoload | 135 ;;;###autoload |
142 (defvar killed-rectangle nil | 136 (defvar killed-rectangle nil |
143 "Rectangle for yank-rectangle to insert.") | 137 "Rectangle for yank-rectangle to insert.") |
144 | 138 |
145 ;;;###autoload | 139 ;;;###autoload |
146 (defun kill-rectangle (start end) | 140 (defun kill-rectangle (start end &optional fill) |
147 "Delete rectangle with corners at point and mark; save as last killed one. | 141 "Delete the rectangle with corners at point and mark (START and END when |
148 Calling from program, supply two args START and END, buffer positions. | 142 called from a program) and save it as the last killed one. You might prefer to |
149 But in programs you might prefer to use `delete-extract-rectangle'." | 143 use `delete-extract-rectangle' from a program. |
150 (interactive "r") | 144 |
151 (if buffer-read-only | 145 With a prefix (or a FILL) argument, also fill lines where nothing has to be |
152 (progn | 146 deleted." |
153 (setq killed-rectangle (extract-rectangle start end)) | 147 (interactive "r\nP") |
154 (barf-if-buffer-read-only))) | 148 (when buffer-read-only |
155 (setq killed-rectangle (delete-extract-rectangle start end))) | 149 (setq killed-rectangle (extract-rectangle start end)) |
156 | 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 (don't save) text in rectangle with corners at point and mark (START | |
156 and END when called from a program). The same range of columns is deleted in | |
157 each line starting with the line where the region begins and ending with the | |
158 line where the region ends. | |
159 | |
160 With a prefix (or a 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 | |
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 | |
199 ;;;###autoload | |
200 (defun extract-rectangle (start end) | |
201 "Return the contents of the rectangle with corners at START and END, | |
202 as a list of strings, one for each line of the rectangle." | |
203 (let ((lines (list nil))) | |
204 (apply-on-rectangle 'extract-rectangle-line start end lines) | |
205 (nreverse (cdr lines)))) | |
206 | |
207 ;; ### 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 | |
209 ;; might be read-only. --dv | |
210 (defun extract-rectangle-line (startcol endcol lines) | |
211 (let (start end begextra endextra line) | |
212 (move-to-column startcol) | |
213 (setq start (point) | |
214 begextra (- (current-column) startcol)) | |
215 (move-to-column endcol) | |
216 (setq end (point) | |
217 endextra (- endcol (current-column))) | |
218 (setq line (buffer-substring start (point))) | |
219 (if (< begextra 0) | |
220 (setq endextra (+ endextra begextra) | |
221 begextra 0)) | |
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 rectangle with corners at point and mark (START and END when |
188 The text previously in the region is not overwritten by the blanks, | 272 called from a program), shifting text right. The text previously in the region |
189 but instead winds up to the right of the rectangle." | 273 is not overwritten by the blanks, but instead winds up to the right of the |
190 (interactive "r") | 274 rectangle. |
191 (operate-on-rectangle 'open-rectangle-line start end nil) | 275 |
276 With a prefix (or a FILL) argument, fill with blanks even if there is no text | |
277 on the right side of the rectangle." | |
278 (interactive "r\nP") | |
279 (apply-on-rectangle 'open-rectangle-line start end fill) | |
192 (goto-char start)) | 280 (goto-char start)) |
193 | 281 |
194 (defun open-rectangle-line (startpos begextra endextra) | 282 (defun open-rectangle-line (startcol endcol fill) |
195 ;; Column where rectangle ends. | 283 (let (spaces) |
196 (let ((endcol (+ (current-column) endextra)) | 284 (when (= (move-to-column startcol (or fill 'coerce)) startcol) |
197 whitewidth) | 285 (unless (and (not fill) |
198 (goto-char startpos) | 286 (= (point) (point-at-eol))) |
199 ;; Column where rectangle begins. | 287 (indent-to endcol))) |
200 (let ((begcol (- (current-column) begextra))) | 288 )) |
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 | 289 |
209 ;;;###autoload | 290 ;;;###autoload |
210 (defun string-rectangle (start end string) | 291 (defun string-rectangle (start end string) |
211 "Insert STRING on each line of the region-rectangle, shifting text right. | 292 "Insert STRING on each line of the rectangle with corners at point and mark |
212 The left edge of the rectangle specifies the column for insertion. | 293 (START and END when called from a program), shifting text right. The left edge |
213 This command does not delete or overwrite any existing text. | 294 of the rectangle specifies the column for insertion. This command does not |
214 | 295 delete or overwrite any existing text." |
215 Called from a program, takes three args; START, END and STRING." | |
216 (interactive "r\nsString rectangle: ") | 296 (interactive "r\nsString rectangle: ") |
217 (operate-on-rectangle 'string-rectangle-line start end t string)) ; XEmacs | 297 (apply-on-rectangle 'string-rectangle-line start end string)) |
218 | 298 |
219 ;; XEmacs: add string arg | 299 (defun string-rectangle-line (startcol endcol string) |
220 (defun string-rectangle-line (startpos begextra endextra string) | 300 (move-to-column startcol t) |
221 (let (whitespace) | 301 (insert string)) |
222 (goto-char startpos) | 302 |
223 ;; Compute horizontal width of following whitespace. | 303 ;;;###autoload |
224 (let ((ocol (current-column))) | 304 (defun clear-rectangle (start end &optional fill) |
225 (skip-chars-forward " \t") | 305 "Blank out the rectangle with corners at point and mark (START and END when |
226 (setq whitespace (- (current-column) ocol))) | 306 called from a program). The text previously in the region is overwritten with |
227 ;; Delete the following whitespace. | 307 blanks. |
228 (delete-region startpos (point)) | 308 |
229 ;; Insert the desired string. | 309 With a prefix (or a FILL) argument, also fill with blanks the parts of the |
230 (insert string) | 310 rectangle which were empty." |
231 ;; Insert the same width of whitespace that we had before. | 311 (interactive "r\nP") |
232 (indent-to (+ (current-column) whitespace)))) | 312 (apply-on-rectangle 'clear-rectangle-line start end fill)) |
233 | 313 |
234 ;;;###autoload | 314 (defun clear-rectangle-line (startcol endcol fill) |
235 (defun clear-rectangle (start end) | 315 (let ((pt (point-at-eol)) |
236 "Blank out rectangle with corners at point and mark. | 316 spaces) |
237 The text previously in the region is overwritten by the blanks. | 317 (when (= (move-to-column startcol (or fill 'coerce)) startcol) |
238 When called from a program, requires two args which specify the corners." | 318 (if (and (not fill) |
239 (interactive "r") | 319 (<= (save-excursion (goto-char pt) (current-column)) endcol)) |
240 (operate-on-rectangle 'clear-rectangle-line start end t)) | 320 (delete-region (point) pt) |
241 | 321 ;; else |
242 (defun clear-rectangle-line (startpos begextra endextra) | 322 (setq pt (point)) |
243 ;; Find end of whitespace after the rectangle. | 323 (move-to-column endcol t) |
244 (skip-chars-forward " \t") | 324 (setq spaces (- (point) pt)) |
245 (let ((column (+ (current-column) endextra))) | 325 (delete-region pt (point)) |
246 ;; Delete the text in the rectangle, and following whitespace. | 326 (indent-to (+ (current-column) spaces)))) |
247 (delete-region (point) | 327 )) |
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 | 328 |
254 (provide 'rect) | 329 (provide 'rect) |
255 | 330 |
256 ;;; rect.el ends here | 331 ;;; rect.el ends here |