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