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