36
|
1 ;; Rectangle functions for GNU Emacs.
|
|
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 ;; it under the terms of the GNU General Public License as published by
|
|
8 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
9 ;; any later version.
|
|
10
|
|
11 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 ;; GNU General Public License for more details.
|
|
15
|
|
16 ;; You should have received a copy of the GNU General Public License
|
|
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20
|
|
21 (defun operate-on-rectangle (function start end coerce-tabs)
|
|
22 "Call FUNCTION for each line of rectangle with corners at START, END.
|
|
23 If COERCE-TABS is non-nil, convert multi-column characters
|
|
24 that span the starting or ending columns on any line
|
|
25 to multiple spaces before calling FUNCTION.
|
|
26 FUNCTION is called with three arguments:
|
|
27 position of start of segment of this line within the rectangle,
|
|
28 number of columns that belong to rectangle but are before that position,
|
|
29 number of columns that belong to rectangle but are after point.
|
|
30 Point is at the end of the segment of this line within the rectangle."
|
|
31 (let (startcol startlinepos endcol endlinepos)
|
|
32 (save-excursion
|
|
33 (goto-char start)
|
|
34 (setq startcol (current-column))
|
|
35 (beginning-of-line)
|
|
36 (setq startlinepos (point)))
|
|
37 (save-excursion
|
|
38 (goto-char end)
|
|
39 (setq endcol (current-column))
|
|
40 (forward-line 1)
|
|
41 (setq endlinepos (point-marker)))
|
|
42 (if (< endcol startcol)
|
|
43 (let ((tem startcol))
|
|
44 (setq startcol endcol endcol tem)))
|
|
45 (if (/= endcol startcol)
|
|
46 (save-excursion
|
|
47 (goto-char startlinepos)
|
|
48 (while (< (point) endlinepos)
|
|
49 (let (startpos begextra endextra)
|
|
50 (move-to-column startcol)
|
|
51 (and coerce-tabs
|
|
52 (> (current-column) startcol)
|
|
53 (rectangle-coerce-tab startcol))
|
|
54 (setq begextra (- (current-column) startcol))
|
|
55 (setq startpos (point))
|
|
56 (move-to-column endcol)
|
|
57 (if (> (current-column) endcol)
|
|
58 (if coerce-tabs
|
|
59 (rectangle-coerce-tab endcol)
|
|
60 (forward-char -1)))
|
|
61 (setq endextra (- endcol (current-column)))
|
|
62 (if (< begextra 0)
|
|
63 (setq endextra (+ endextra begextra)
|
|
64 begextra 0))
|
|
65 (funcall function startpos begextra endextra))
|
|
66 (forward-line 1))))
|
|
67 (- endcol startcol)))
|
|
68
|
|
69 (defun delete-rectangle-line (startdelpos ignore ignore)
|
|
70 (delete-region startdelpos (point)))
|
|
71
|
|
72 (defun delete-extract-rectangle-line (startdelpos begextra endextra)
|
|
73 (save-excursion
|
|
74 (extract-rectangle-line startdelpos begextra endextra))
|
|
75 (delete-region startdelpos (point)))
|
|
76
|
|
77 (defun extract-rectangle-line (startdelpos begextra endextra)
|
|
78 (let ((line (buffer-substring startdelpos (point)))
|
|
79 (end (point)))
|
|
80 (goto-char startdelpos)
|
|
81 (while (search-forward "\t" end t)
|
|
82 (let ((width (- (current-column)
|
|
83 (save-excursion (forward-char -1)
|
|
84 (current-column)))))
|
|
85 (setq line (concat (substring line 0 (- (point) end 1))
|
|
86 (spaces-string width)
|
|
87 (substring line (+ (length line) (- (point) end)))))))
|
|
88 (if (or (> begextra 0) (> endextra 0))
|
|
89 (setq line (concat (spaces-string begextra)
|
|
90 line
|
|
91 (spaces-string endextra))))
|
|
92 (setq lines (cons line lines))))
|
|
93
|
|
94 (defconst spaces-strings
|
|
95 '["" " " " " " " " " " " " " " " " "])
|
|
96
|
|
97 (defun spaces-string (n)
|
|
98 (if (<= n 8) (aref spaces-strings n)
|
|
99 (let ((val ""))
|
|
100 (while (> n 8)
|
|
101 (setq val (concat " " val)
|
|
102 n (- n 8)))
|
|
103 (concat val (aref spaces-strings n)))))
|
|
104
|
|
105 (defun delete-rectangle (start end)
|
|
106 "Delete (don't save) text in rectangle with point and mark as corners.
|
|
107 The same range of columns is deleted in each line
|
|
108 starting with the line where the region begins
|
|
109 and ending with the line where the region ends."
|
|
110 (interactive "r")
|
|
111 (operate-on-rectangle 'delete-rectangle-line start end t))
|
|
112
|
|
113 (defun delete-extract-rectangle (start end)
|
|
114 "Delete contents of rectangle and return it as a list of strings.
|
|
115 Arguments START and END are the corners of the rectangle.
|
|
116 The value is list of strings, one for each line of the rectangle."
|
|
117 (let (lines)
|
|
118 (operate-on-rectangle 'delete-extract-rectangle-line
|
|
119 start end t)
|
|
120 (nreverse lines)))
|
|
121
|
|
122 (defun extract-rectangle (start end)
|
|
123 "Return contents of rectangle with corners at START and END.
|
|
124 Value is list of strings, one for each line of the rectangle."
|
|
125 (let (lines)
|
|
126 (operate-on-rectangle 'extract-rectangle-line start end nil)
|
|
127 (nreverse lines)))
|
|
128
|
|
129 (defvar killed-rectangle nil
|
|
130 "Rectangle for yank-rectangle to insert.")
|
|
131
|
|
132 (defun kill-rectangle (start end)
|
|
133 "Delete rectangle with corners at point and mark; save as last killed one.
|
|
134 Calling from program, supply two args START and END, buffer positions.
|
|
135 But in programs you might prefer to use delete-extract-rectangle."
|
|
136 (interactive "r")
|
|
137 (setq killed-rectangle (delete-extract-rectangle start end)))
|
|
138
|
|
139 (defun yank-rectangle ()
|
|
140 "Yank the last killed rectangle with upper left corner at point."
|
|
141 (interactive)
|
|
142 (insert-rectangle killed-rectangle))
|
|
143
|
|
144 (defun insert-rectangle (rectangle)
|
|
145 "Insert text of RECTANGLE with upper left corner at point.
|
|
146 RECTANGLE's first line is inserted at point,
|
|
147 its second line is inserted at a point vertically under point, etc.
|
|
148 RECTANGLE should be a list of strings."
|
|
149 (let ((lines rectangle)
|
|
150 (insertcolumn (current-column))
|
|
151 (first t))
|
|
152 (while lines
|
|
153 (or first
|
|
154 (progn
|
|
155 (forward-line 1)
|
|
156 (or (bolp) (insert ?\n))
|
|
157 (move-to-column insertcolumn)
|
|
158 (if (> (current-column) insertcolumn)
|
|
159 (rectangle-coerce-tab insertcolumn))
|
|
160 (if (< (current-column) insertcolumn)
|
|
161 (indent-to insertcolumn))))
|
|
162 (setq first nil)
|
|
163 (insert (car lines))
|
|
164 (setq lines (cdr lines)))))
|
|
165
|
|
166 (defun open-rectangle (start end)
|
|
167 "Blank out rectangle with corners at point and mark, shifting text right.
|
|
168 The text previously in the region is not overwritten by the blanks,
|
|
169 but insted winds up to the right of the rectangle."
|
|
170 (interactive "r")
|
|
171 (operate-on-rectangle 'open-rectangle-line start end nil))
|
|
172
|
|
173 (defun open-rectangle-line (startpos begextra endextra)
|
|
174 (let ((column (+ (current-column) begextra endextra)))
|
|
175 (goto-char startpos)
|
|
176 (let ((ocol (current-column)))
|
|
177 (skip-chars-forward " \t")
|
|
178 (setq column (+ column (- (current-column) ocol))))
|
|
179 (delete-region (point)
|
|
180 (progn (skip-chars-backward " \t")
|
|
181 (point)))
|
|
182 (indent-to column)))
|
|
183
|
|
184 (defun clear-rectangle (start end)
|
|
185 "Blank out rectangle with corners at point and mark.
|
|
186 The text previously in the region is overwritten by the blanks.
|
|
187 When called from a program, requires two args which specify the corners."
|
|
188 (interactive "r")
|
|
189 (operate-on-rectangle 'clear-rectangle-line start end t))
|
|
190
|
|
191 (defun clear-rectangle-line (startpos begextra endextra)
|
|
192 (skip-chars-forward " \t")
|
|
193 (let ((column (+ (current-column) endextra)))
|
|
194 (delete-region (point)
|
|
195 (progn (goto-char startpos)
|
|
196 (skip-chars-backward " \t")
|
|
197 (point)))
|
|
198 (indent-to column)))
|
|
199
|
|
200 (defun rectangle-coerce-tab (column)
|
|
201 (let ((aftercol (current-column))
|
|
202 (indent-tabs-mode nil))
|
|
203 (delete-char -1)
|
|
204 (indent-to aftercol)
|
|
205 (backward-char (- aftercol column))))
|