1 ;;; rect.el --- rectangle functions for XEmacs.
3 ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of XEmacs.
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)
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.
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
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 ;;; Synched up with: FSF 19.34.
29 ;; This package provides the operations on rectangles that are ocumented
30 ;; in the XEmacs Reference Manual.
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)
48 (setq startcol (current-column))
50 (setq startlinepos (point)))
53 (setq endcol (current-column))
55 (setq endlinepos (point-marker)))
56 (if (< endcol startcol)
59 (setq startcol endcol endcol tem)))
61 (goto-char startlinepos)
62 (while (< (point) endlinepos)
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)))
70 (setq endextra (+ endextra begextra)
72 (if (< endextra 0) (setq endextra 0))
73 (apply function startpos begextra endextra extra-args))
77 (defun delete-rectangle-line (startdelpos ignore ignore)
78 (delete-region startdelpos (point)))
80 ;; XEmacs: added lines arg
81 (defun delete-extract-rectangle-line (startdelpos begextra endextra lines)
83 (extract-rectangle-line startdelpos begextra endextra lines))
84 (delete-region startdelpos (point)))
86 ;; XEmacs: added lines arg
87 (defun extract-rectangle-line (startdelpos begextra endextra lines)
88 (let ((line (buffer-substring startdelpos (point)))
90 (goto-char startdelpos)
91 (while (search-forward "\t" end t)
92 (let ((width (- (current-column)
93 (save-excursion (forward-char -1)
95 (setq line (concat (substring line 0 (- (point) end 1))
97 (substring line (+ (length line) (- (point) end)))))))
98 (if (or (> begextra 0) (> endextra 0))
99 (setq line (concat (spaces-string begextra)
101 (spaces-string endextra))))
102 (setcdr lines (cons line (cdr lines))))) ; XEmacs
104 (defconst spaces-strings
105 (purecopy '["" " " " " " " " " " " " " " " " "]))
107 (defun spaces-string (n)
108 (if (<= n 8) (aref spaces-strings n)
111 (setq val (concat " " val)
113 (concat val (aref spaces-strings n)))))
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."
121 (operate-on-rectangle 'delete-rectangle-line start end t))
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
131 (nreverse (cdr lines))))
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))))
142 (defvar killed-rectangle nil
143 "Rectangle for yank-rectangle to insert.")
146 (defun kill-rectangle (start end)
147 "Delete rectangle with corners at point and mark; save as last killed one.
148 Calling from program, supply two args START and END, buffer positions.
149 But in programs you might prefer to use `delete-extract-rectangle'."
153 (setq killed-rectangle (extract-rectangle start end))
154 (barf-if-buffer-read-only)))
155 (setq killed-rectangle (delete-extract-rectangle start end)))
158 (defun yank-rectangle ()
159 "Yank the last killed rectangle with upper left corner at point."
161 (insert-rectangle killed-rectangle))
164 (defun insert-rectangle (rectangle)
165 "Insert text of RECTANGLE with upper left corner at point.
166 RECTANGLE's first line is inserted at point, its second
167 line is inserted at a point vertically under point, etc.
168 RECTANGLE should be a list of strings.
169 After this command, the mark is at the upper left corner
170 and point is at the lower right corner."
171 (let ((lines rectangle)
172 (insertcolumn (current-column))
179 (or (bolp) (insert ?\n))
180 (move-to-column insertcolumn t)))
183 (setq lines (cdr lines)))))
186 (defun open-rectangle (start end)
187 "Blank out rectangle with corners at point and mark, shifting text right.
188 The text previously in the region is not overwritten by the blanks,
189 but instead winds up to the right of the rectangle."
191 (operate-on-rectangle 'open-rectangle-line start end nil)
194 (defun open-rectangle-line (startpos begextra endextra)
195 ;; Column where rectangle ends.
196 (let ((endcol (+ (current-column) endextra))
199 ;; Column where rectangle begins.
200 (let ((begcol (- (current-column) begextra)))
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))))
210 (defun string-rectangle (start end string)
211 "Insert STRING on each line of the region-rectangle, shifting text right.
212 The left edge of the rectangle specifies the column for insertion.
213 This command does not delete or overwrite any existing text.
215 Called from a program, takes three args; START, END and STRING."
216 (interactive "r\nsString rectangle: ")
217 (operate-on-rectangle 'string-rectangle-line start end t string)) ; XEmacs
219 ;; XEmacs: add string arg
220 (defun string-rectangle-line (startpos begextra endextra string)
223 ;; Compute horizontal width of following whitespace.
224 (let ((ocol (current-column)))
225 (skip-chars-forward " \t")
226 (setq whitespace (- (current-column) ocol)))
227 ;; Delete the following whitespace.
228 (delete-region startpos (point))
229 ;; Insert the desired string.
231 ;; Insert the same width of whitespace that we had before.
232 (indent-to (+ (current-column) whitespace))))
235 (defun clear-rectangle (start end)
236 "Blank out rectangle with corners at point and mark.
237 The text previously in the region is overwritten by the blanks.
238 When called from a program, requires two args which specify the corners."
240 (operate-on-rectangle 'clear-rectangle-line start end t))
242 (defun clear-rectangle-line (startpos begextra endextra)
243 ;; Find end of whitespace after the rectangle.
244 (skip-chars-forward " \t")
245 (let ((column (+ (current-column) endextra)))
246 ;; Delete the text in the rectangle, and following whitespace.
247 (delete-region (point)
248 (progn (goto-char startpos)
249 (skip-chars-backward " \t")
251 ;; Reindent out to same column that we were at.
256 ;;; rect.el ends here