Mercurial > emacs
comparison lisp/rect.el @ 22213:84c3c863f0bd
(string-rectangle-string): New variable.
(string-rectangle): Bind it.
(string-rectangle-line): Use it.
(operate-on-rectangle-lines): New variable.
(extract-rectangle-line): Update it.
(delete-extract-rectangle, extract-rectangle): Bind and use it.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 24 May 1998 17:04:55 +0000 |
parents | 8576d32229fc |
children | 6c4756ecd4a2 |
comparison
equal
deleted
inserted
replaced
22212:b95ba3830bc9 | 22213:84c3c863f0bd |
---|---|
26 | 26 |
27 ;; This package provides the operations on rectangles that are ocumented | 27 ;; This package provides the operations on rectangles that are ocumented |
28 ;; in the Emacs manual. | 28 ;; in the Emacs manual. |
29 | 29 |
30 ;;; Code: | 30 ;;; Code: |
31 | |
32 ;; extract-rectangle-line stores lines into this list | |
33 ;; to accumulate them for extract-rectangle and delete-extract-rectangle. | |
34 (defvar operate-on-rectangle-lines) | |
31 | 35 |
32 (defun operate-on-rectangle (function start end coerce-tabs) | 36 (defun operate-on-rectangle (function start end coerce-tabs) |
33 "Call FUNCTION for each line of rectangle with corners at START, END. | 37 "Call FUNCTION for each line of rectangle with corners at START, END. |
34 If COERCE-TABS is non-nil, convert multi-column characters | 38 If COERCE-TABS is non-nil, convert multi-column characters |
35 that span the starting or ending columns on any line | 39 that span the starting or ending columns on any line |
93 (substring line (+ (length line) (- (point) end))))))) | 97 (substring line (+ (length line) (- (point) end))))))) |
94 (if (or (> begextra 0) (> endextra 0)) | 98 (if (or (> begextra 0) (> endextra 0)) |
95 (setq line (concat (spaces-string begextra) | 99 (setq line (concat (spaces-string begextra) |
96 line | 100 line |
97 (spaces-string endextra)))) | 101 (spaces-string endextra)))) |
98 (setq lines (cons line lines)))) | 102 (setq operate-on-rectangle-lines (cons line operate-on-rectangle-lines)))) |
99 | 103 |
100 (defconst spaces-strings | 104 (defconst spaces-strings |
101 '["" " " " " " " " " " " " " " " " "]) | 105 '["" " " " " " " " " " " " " " " " "]) |
102 | 106 |
103 (defun spaces-string (n) | 107 (defun spaces-string (n) |
119 ;;;###autoload | 123 ;;;###autoload |
120 (defun delete-extract-rectangle (start end) | 124 (defun delete-extract-rectangle (start end) |
121 "Delete contents of rectangle and return it as a list of strings. | 125 "Delete contents of rectangle and return it as a list of strings. |
122 Arguments START and END are the corners of the rectangle. | 126 Arguments START and END are the corners of the rectangle. |
123 The value is list of strings, one for each line of the rectangle." | 127 The value is list of strings, one for each line of the rectangle." |
124 (let (lines) | 128 (let (operate-on-rectangle-lines) |
125 (operate-on-rectangle 'delete-extract-rectangle-line | 129 (operate-on-rectangle 'delete-extract-rectangle-line |
126 start end t) | 130 start end t) |
127 (nreverse lines))) | 131 (nreverse operate-on-rectangle-lines))) |
128 | 132 |
129 ;;;###autoload | 133 ;;;###autoload |
130 (defun extract-rectangle (start end) | 134 (defun extract-rectangle (start end) |
131 "Return contents of rectangle with corners at START and END. | 135 "Return contents of rectangle with corners at START and END. |
132 Value is list of strings, one for each line of the rectangle." | 136 Value is list of strings, one for each line of the rectangle." |
133 (let (lines) | 137 (let (operate-on-rectangle-lines) |
134 (operate-on-rectangle 'extract-rectangle-line start end nil) | 138 (operate-on-rectangle 'extract-rectangle-line start end nil) |
135 (nreverse lines))) | 139 (nreverse operate-on-rectangle-lines))) |
136 | 140 |
137 (defvar killed-rectangle nil | 141 (defvar killed-rectangle nil |
138 "Rectangle for yank-rectangle to insert.") | 142 "Rectangle for yank-rectangle to insert.") |
139 | 143 |
140 ;;;###autoload | 144 ;;;###autoload |
215 (progn | 219 (progn |
216 (skip-syntax-forward " ") | 220 (skip-syntax-forward " ") |
217 (point))))) | 221 (point))))) |
218 start end t)) | 222 start end t)) |
219 | 223 |
224 ;; string-rectangle uses this variable to pass the string | |
225 ;; to string-rectangle-line. | |
226 (defvar string-rectangle-string) | |
220 | 227 |
221 ;;;###autoload | 228 ;;;###autoload |
222 (defun string-rectangle (start end string) | 229 (defun string-rectangle (start end string) |
223 "Insert STRING on each line of the region-rectangle, shifting text right. | 230 "Insert STRING on each line of the region-rectangle, shifting text right. |
224 The left edge of the rectangle specifies the column for insertion. | 231 The left edge of the rectangle specifies the column for insertion. |
225 This command does not delete or overwrite any existing text. | 232 This command does not delete or overwrite any existing text. |
226 | 233 |
227 Called from a program, takes three args; START, END and STRING." | 234 Called from a program, takes three args; START, END and STRING." |
228 (interactive "r\nsString rectangle: ") | 235 (interactive "r\nsString rectangle: ") |
229 (operate-on-rectangle 'string-rectangle-line start end t)) | 236 (let ((string-rectangle-string string)) |
237 (operate-on-rectangle 'string-rectangle-line start end t))) | |
230 | 238 |
231 (defun string-rectangle-line (startpos begextra endextra) | 239 (defun string-rectangle-line (startpos begextra endextra) |
232 (let (whitespace) | 240 (let (whitespace) |
233 (goto-char startpos) | 241 (goto-char startpos) |
234 ;; Compute horizontal width of following whitespace. | 242 ;; Compute horizontal width of following whitespace. |
236 (skip-chars-forward " \t") | 244 (skip-chars-forward " \t") |
237 (setq whitespace (- (current-column) ocol))) | 245 (setq whitespace (- (current-column) ocol))) |
238 ;; Delete the following whitespace. | 246 ;; Delete the following whitespace. |
239 (delete-region startpos (point)) | 247 (delete-region startpos (point)) |
240 ;; Insert the desired string. | 248 ;; Insert the desired string. |
241 (insert string) | 249 (insert string-rectangle-string) |
242 ;; Insert the same width of whitespace that we had before. | 250 ;; Insert the same width of whitespace that we had before. |
243 (indent-to (+ (current-column) whitespace)))) | 251 (indent-to (+ (current-column) whitespace)))) |
244 | 252 |
245 ;;;###autoload | 253 ;;;###autoload |
246 (defun clear-rectangle (start end) | 254 (defun clear-rectangle (start end) |