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