0
|
1 ;;; rect.el --- rectangle functions for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: internal
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
72
|
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
|
|
23 ;; 02111-1307, USA.
|
0
|
24
|
72
|
25 ;;; Synched up with: FSF 19.34.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This package provides the operations on rectangles that are ocumented
|
|
30 ;; in the XEmacs Reference Manual.
|
|
31
|
|
32 ;;; Code:
|
|
33
|
|
34 ;; XEmacs: extra-args
|
|
35 (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.
|
|
37 If COERCE-TABS is non-nil, convert multi-column characters
|
|
38 that span the starting or ending columns on any line
|
|
39 to multiple spaces before calling FUNCTION.
|
|
40 FUNCTION is called with three arguments:
|
|
41 position of start of segment of this line within the rectangle,
|
|
42 number of columns that belong to rectangle but are before that position,
|
|
43 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."
|
|
45 (let (startcol startlinepos endcol endlinepos)
|
|
46 (save-excursion
|
|
47 (goto-char start)
|
|
48 (setq startcol (current-column))
|
|
49 (beginning-of-line)
|
|
50 (setq startlinepos (point)))
|
|
51 (save-excursion
|
|
52 (goto-char end)
|
|
53 (setq endcol (current-column))
|
|
54 (forward-line 1)
|
|
55 (setq endlinepos (point-marker)))
|
|
56 (if (< endcol startcol)
|
72
|
57 ;; XEmacs
|
0
|
58 (let ((tem startcol))
|
|
59 (setq startcol endcol endcol tem)))
|
|
60 (save-excursion
|
|
61 (goto-char startlinepos)
|
|
62 (while (< (point) endlinepos)
|
72
|
63 (let (startpos begextra endextra)
|
|
64 (move-to-column startcol coerce-tabs)
|
|
65 (setq begextra (- (current-column) startcol))
|
|
66 (setq startpos (point))
|
|
67 (move-to-column endcol coerce-tabs)
|
|
68 (setq endextra (- endcol (current-column)))
|
|
69 (if (< begextra 0)
|
|
70 (setq endextra (+ endextra begextra)
|
|
71 begextra 0))
|
0
|
72 (apply function startpos begextra endextra extra-args))
|
72
|
73 (forward-line 1)))
|
0
|
74 (- endcol startcol)))
|
|
75
|
|
76 (defun delete-rectangle-line (startdelpos ignore ignore)
|
|
77 (delete-region startdelpos (point)))
|
|
78
|
|
79 ;; XEmacs: added lines arg
|
|
80 (defun delete-extract-rectangle-line (startdelpos begextra endextra lines)
|
|
81 (save-excursion
|
|
82 (extract-rectangle-line startdelpos begextra endextra lines))
|
|
83 (delete-region startdelpos (point)))
|
|
84
|
|
85 ;; XEmacs: added lines arg
|
|
86 (defun extract-rectangle-line (startdelpos begextra endextra lines)
|
|
87 (let ((line (buffer-substring startdelpos (point)))
|
|
88 (end (point)))
|
|
89 (goto-char startdelpos)
|
|
90 (while (search-forward "\t" end t)
|
|
91 (let ((width (- (current-column)
|
|
92 (save-excursion (forward-char -1)
|
|
93 (current-column)))))
|
|
94 (setq line (concat (substring line 0 (- (point) end 1))
|
|
95 (spaces-string width)
|
|
96 (substring line (+ (length line) (- (point) end)))))))
|
|
97 (if (or (> begextra 0) (> endextra 0))
|
|
98 (setq line (concat (spaces-string begextra)
|
|
99 line
|
|
100 (spaces-string endextra))))
|
72
|
101 (setcdr lines (cons line (cdr lines))))) ; XEmacs
|
0
|
102
|
|
103 (defconst spaces-strings
|
|
104 (purecopy '["" " " " " " " " " " " " " " " " "]))
|
|
105
|
|
106 (defun spaces-string (n)
|
|
107 (if (<= n 8) (aref spaces-strings n)
|
|
108 (let ((val ""))
|
|
109 (while (> n 8)
|
|
110 (setq val (concat " " val)
|
|
111 n (- n 8)))
|
|
112 (concat val (aref spaces-strings n)))))
|
|
113
|
|
114 ;;;###autoload
|
|
115 (defun delete-rectangle (start end)
|
|
116 "Delete (don't save) text in rectangle with point and mark as corners.
|
72
|
117 The same range of columns is deleted in each line starting with the line
|
|
118 where the region begins and ending with the line where the region ends."
|
0
|
119 (interactive "r")
|
|
120 (operate-on-rectangle 'delete-rectangle-line start end t))
|
|
121
|
|
122 ;;;###autoload
|
|
123 (defun delete-extract-rectangle (start end)
|
|
124 "Delete contents of rectangle and return it as a list of strings.
|
|
125 Arguments START and END are the corners of the rectangle.
|
|
126 The value is list of strings, one for each line of the rectangle."
|
|
127 (let ((lines (list nil))) ; XEmacs change
|
|
128 (operate-on-rectangle 'delete-extract-rectangle-line
|
|
129 start end t lines)
|
|
130 (nreverse (cdr lines))))
|
|
131
|
|
132 ;;;###autoload
|
|
133 (defun extract-rectangle (start end)
|
|
134 "Return contents of rectangle with corners at START and END.
|
|
135 Value is list of strings, one for each line of the rectangle."
|
|
136 (let ((lines (list nil))) ; XEmacs change
|
|
137 (operate-on-rectangle 'extract-rectangle-line start end nil lines)
|
|
138 (nreverse (cdr lines))))
|
|
139
|
|
140 ;;;###autoload
|
|
141 (defvar killed-rectangle nil
|
|
142 "Rectangle for yank-rectangle to insert.")
|
|
143
|
|
144 ;;;###autoload
|
|
145 (defun kill-rectangle (start end)
|
|
146 "Delete rectangle with corners at point and mark; save as last killed one.
|
|
147 Calling from program, supply two args START and END, buffer positions.
|
|
148 But in programs you might prefer to use `delete-extract-rectangle'."
|
|
149 (interactive "r")
|
|
150 (if buffer-read-only
|
|
151 (progn
|
|
152 (setq killed-rectangle (extract-rectangle start end))
|
|
153 (barf-if-buffer-read-only)))
|
|
154 (setq killed-rectangle (delete-extract-rectangle start end)))
|
|
155
|
|
156 ;;;###autoload
|
|
157 (defun yank-rectangle ()
|
|
158 "Yank the last killed rectangle with upper left corner at point."
|
|
159 (interactive)
|
|
160 (insert-rectangle killed-rectangle))
|
|
161
|
|
162 ;;;###autoload
|
|
163 (defun insert-rectangle (rectangle)
|
|
164 "Insert text of RECTANGLE with upper left corner at point.
|
72
|
165 RECTANGLE's first line is inserted at point, its second
|
|
166 line is inserted at a point vertically under point, etc.
|
0
|
167 RECTANGLE should be a list of strings.
|
|
168 After this command, the mark is at the upper left corner
|
|
169 and point is at the lower right corner."
|
|
170 (let ((lines rectangle)
|
|
171 (insertcolumn (current-column))
|
|
172 (first t))
|
|
173 (push-mark)
|
|
174 (while lines
|
|
175 (or first
|
|
176 (progn
|
|
177 (forward-line 1)
|
|
178 (or (bolp) (insert ?\n))
|
|
179 (move-to-column insertcolumn t)))
|
|
180 (setq first nil)
|
|
181 (insert (car lines))
|
|
182 (setq lines (cdr lines)))))
|
|
183
|
|
184 ;;;###autoload
|
|
185 (defun open-rectangle (start end)
|
|
186 "Blank out rectangle with corners at point and mark, shifting text right.
|
|
187 The text previously in the region is not overwritten by the blanks,
|
|
188 but instead winds up to the right of the rectangle."
|
|
189 (interactive "r")
|
|
190 (operate-on-rectangle 'open-rectangle-line start end nil)
|
|
191 (goto-char start))
|
|
192
|
|
193 (defun open-rectangle-line (startpos begextra endextra)
|
|
194 ;; Column where rectangle ends.
|
|
195 (let ((endcol (+ (current-column) endextra))
|
|
196 whitewidth)
|
|
197 (goto-char startpos)
|
|
198 ;; Column where rectangle begins.
|
|
199 (let ((begcol (- (current-column) begextra)))
|
|
200 (skip-chars-forward " \t")
|
|
201 ;; Width of whitespace to be deleted and recreated.
|
|
202 (setq whitewidth (- (current-column) begcol)))
|
|
203 ;; Delete the whitespace following the start column.
|
|
204 (delete-region startpos (point))
|
|
205 ;; Open the desired width, plus same amount of whitespace we just deleted.
|
|
206 (indent-to (+ endcol whitewidth))))
|
|
207
|
|
208 ;;;###autoload
|
|
209 (defun string-rectangle (start end string)
|
|
210 "Insert STRING on each line of the region-rectangle, shifting text right.
|
|
211 The left edge of the rectangle specifies the column for insertion.
|
|
212 This command does not delete or overwrite any existing text.
|
|
213
|
|
214 Called from a program, takes three args; START, END and STRING."
|
|
215 (interactive "r\nsString rectangle: ")
|
72
|
216 (operate-on-rectangle 'string-rectangle-line start end t string)) ; XEmacs
|
0
|
217
|
|
218 ;; XEmacs: add string arg
|
|
219 (defun string-rectangle-line (startpos begextra endextra string)
|
|
220 (let (whitespace)
|
|
221 (goto-char startpos)
|
|
222 ;; Compute horizontal width of following whitespace.
|
|
223 (let ((ocol (current-column)))
|
|
224 (skip-chars-forward " \t")
|
|
225 (setq whitespace (- (current-column) ocol)))
|
|
226 ;; Delete the following whitespace.
|
|
227 (delete-region startpos (point))
|
|
228 ;; Insert the desired string.
|
|
229 (insert string)
|
|
230 ;; Insert the same width of whitespace that we had before.
|
|
231 (indent-to (+ (current-column) whitespace))))
|
|
232
|
|
233 ;;;###autoload
|
|
234 (defun clear-rectangle (start end)
|
|
235 "Blank out rectangle with corners at point and mark.
|
|
236 The text previously in the region is overwritten by the blanks.
|
|
237 When called from a program, requires two args which specify the corners."
|
|
238 (interactive "r")
|
|
239 (operate-on-rectangle 'clear-rectangle-line start end t))
|
|
240
|
|
241 (defun clear-rectangle-line (startpos begextra endextra)
|
|
242 ;; Find end of whitespace after the rectangle.
|
|
243 (skip-chars-forward " \t")
|
|
244 (let ((column (+ (current-column) endextra)))
|
|
245 ;; Delete the text in the rectangle, and following whitespace.
|
|
246 (delete-region (point)
|
|
247 (progn (goto-char startpos)
|
|
248 (skip-chars-backward " \t")
|
|
249 (point)))
|
|
250 ;; Reindent out to same column that we were at.
|
|
251 (indent-to column)))
|
|
252
|
|
253 (provide 'rect)
|
|
254
|
|
255 ;;; rect.el ends here
|