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