Mercurial > emacs
annotate lisp/array.el @ 11546:66db15ec8ad8
Don't turn on tpu-edt mode.
(tpu-gnu-emacs19-p): Function deleted. Callers changed.
(tpu-load-xkeys): Use file name .tpu-keys by default.
(tpu-gnu-emacs18-p): Function deleted.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 25 Apr 1995 00:37:27 +0000 |
parents | e64f012944e0 |
children | 5f3aa9df448e |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
1 ;;; array.el --- array editing commands for Gnu Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
2 |
845 | 3 ;; Copyright (C) 1987 Free Software Foundation, Inc. |
4 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Author David M. Brown |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
6 ;; Maintainer: FSF |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
845
diff
changeset
|
7 ;; Keywords: extensions |
180 | 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:
662
diff
changeset
|
13 ;; the Free Software Foundation; either version 2, or (at your option) |
180 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
25 ;;; Commentary: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
26 |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
27 ;; Commands for editing a buffer interpreted as a rectangular array |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
28 ;; or matrix of whitespace-separated strings. You specify the array |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
29 ;; dimensions and some other parameters at startup time. |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
30 |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
31 ;; Written by dmb%morgoth@harvard.harvard.edu (address is old) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
32 ;; (David M. Brown at Goldberg-Zoino & Associates, Inc.) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
33 ;; Thanks to cph@kleph.ai.mit.edu for assistance |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
34 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
35 ;; To do: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
36 ;; Smooth initialization process by grokking local variables list |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
37 ;; at end of buffer or parsing buffer using whitespace as delimiters. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
38 ;; Make 'array-copy-column-right faster. |
180 | 39 |
40 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
41 ;;; Code: |
180 | 42 |
43 ;;; Internal information functions. | |
44 | |
45 (defun array-cursor-in-array-range () | |
46 "Returns t if the cursor is in a valid array cell. | |
47 Its ok to be on a row number line." | |
48 (let ((columns-last-line (% max-column columns-per-line))) | |
49 ;; Requires buffer-line and buffer-column to be current. | |
50 (not (or | |
51 ;; The cursor is too far to the right. | |
52 (>= buffer-column line-length) | |
53 ;; The cursor is below the last row. | |
54 (>= buffer-line (* lines-per-row max-row)) | |
55 ;; The cursor is on the last line of the row, the line is smaller | |
56 ;; than the others, and the cursor is after the last array column | |
57 ;; on the line. | |
58 (and (zerop (% (1+ buffer-line) lines-per-row)) | |
59 (not (zerop columns-last-line)) | |
60 (>= buffer-column (* columns-last-line field-width))))))) | |
61 | |
62 (defun array-current-row () | |
63 "Return the array row of the field in which the cursor is located." | |
64 ;; Requires buffer-line and buffer-column to be current. | |
65 (and (array-cursor-in-array-range) | |
66 (1+ (floor buffer-line lines-per-row)))) | |
67 | |
68 (defun array-current-column () | |
69 "Return the array column of the field in which the cursor is located." | |
70 ;; Requires buffer-line and buffer-column to be current. | |
71 (and (array-cursor-in-array-range) | |
72 ;; It's not okay to be on a row number line. | |
73 (not (and rows-numbered | |
74 (zerop (% buffer-line lines-per-row)))) | |
75 (+ | |
76 ;; Array columns due to line differences. | |
77 (* columns-per-line | |
78 (if rows-numbered | |
79 (1- (% buffer-line lines-per-row)) | |
80 (% buffer-line lines-per-row))) | |
81 ;; Array columns on the current line. | |
4514
e64f012944e0
(abs, ceiling, floor): Remove, since they now redefine
Paul Eggert <eggert@twinsun.com>
parents:
2307
diff
changeset
|
82 (1+ (floor buffer-column field-width))))) |
180 | 83 |
84 (defun array-update-array-position (&optional a-row a-column) | |
85 "Set `array-row' and `array-column' to their current values or | |
86 to the optional arguments A-ROW and A-COLUMN." | |
87 ;; Requires that buffer-line and buffer-column be current. | |
88 (setq array-row (or a-row (array-current-row)) | |
89 array-column (or a-column (array-current-column)))) | |
90 | |
91 (defun array-update-buffer-position () | |
92 "Set buffer-line and buffer-column to their current values." | |
93 (setq buffer-line (current-line) | |
94 buffer-column (current-column))) | |
95 | |
96 | |
97 | |
98 ;;; Information commands. | |
99 | |
100 (defun array-what-position () | |
101 "Display the row and column in which the cursor is positioned." | |
102 (interactive) | |
103 (let ((buffer-line (current-line)) | |
104 (buffer-column (current-column))) | |
105 (message (format "Array row: %s Array column: %s" | |
106 (prin1-to-string (array-current-row)) | |
107 (prin1-to-string (array-current-column)))))) | |
108 | |
109 (defun array-display-local-variables () | |
110 "Display the current state of the local variables in the minibuffer." | |
111 (interactive) | |
112 (let ((buf (buffer-name (current-buffer)))) | |
113 (with-output-to-temp-buffer "*Local Variables*" | |
114 (buffer-disable-undo standard-output) | |
115 (terpri) | |
116 (princ (format " Buffer: %s\n\n" buf)) | |
117 (princ (format " max-row: %s\n" | |
118 (prin1-to-string max-row))) | |
119 (princ (format " max-column: %s\n" | |
120 (prin1-to-string max-column))) | |
121 (princ (format " columns-per-line: %s\n" | |
122 (prin1-to-string columns-per-line))) | |
123 (princ (format " field-width: %s\n" | |
124 (prin1-to-string field-width))) | |
125 (princ (format " rows-numbered: %s\n" | |
126 (prin1-to-string rows-numbered))) | |
127 (princ (format " lines-per-row: %s\n" | |
128 (prin1-to-string lines-per-row))) | |
129 (princ (format " line-length: %s\n" | |
130 (prin1-to-string line-length)))))) | |
131 | |
132 | |
133 | |
134 ;;; Internal movement functions. | |
135 | |
136 (defun array-beginning-of-field (&optional go-there) | |
137 "Return the column of the beginning of the current field. | |
138 Optional argument GO-THERE, if non-nil, means go there too." | |
139 ;; Requires that buffer-column be current. | |
140 (let ((goal-column (- buffer-column (% buffer-column field-width)))) | |
141 (if go-there | |
142 (move-to-column-untabify goal-column) | |
143 goal-column))) | |
144 | |
145 (defun array-end-of-field (&optional go-there) | |
146 "Return the column of the end of the current array field. | |
147 If optional argument GO-THERE is non-nil, go there too." | |
148 ;; Requires that buffer-column be current. | |
149 (let ((goal-column (+ (- buffer-column (% buffer-column field-width)) | |
150 field-width))) | |
151 (if go-there | |
152 (move-to-column-untabify goal-column) | |
153 goal-column))) | |
154 | |
155 (defun array-move-to-cell (a-row a-column) | |
156 "Move to array row A-ROW and array column A-COLUMN. | |
157 Leave point at the beginning of the field and return the new buffer column." | |
158 (let ((goal-line (+ (* lines-per-row (1- a-row)) | |
159 (if rows-numbered 1 0) | |
160 (floor (1- a-column) columns-per-line))) | |
161 (goal-column (* field-width (% (1- a-column) columns-per-line)))) | |
162 (goto-char (point-min)) | |
163 (forward-line goal-line) | |
164 (move-to-column-untabify goal-column))) | |
165 | |
166 (defun array-move-to-row (a-row) | |
167 "Move to array row A-ROW preserving the current array column. | |
168 Leave point at the beginning of the field and return the new array row." | |
169 ;; Requires that buffer-line and buffer-column be current. | |
170 (let ((goal-line (+ (* lines-per-row (1- a-row)) | |
171 (% buffer-line lines-per-row))) | |
172 (goal-column (- buffer-column (% buffer-column field-width)))) | |
173 (forward-line (- goal-line buffer-line)) | |
174 (move-to-column-untabify goal-column) | |
175 a-row)) | |
176 | |
177 (defun array-move-to-column (a-column) | |
178 "Move to array column A-COLUMN preserving the current array row. | |
179 Leave point at the beginning of the field and return the new array column." | |
180 ;; Requires that buffer-line and buffer-column be current. | |
181 (let ((goal-line (+ (- buffer-line (% buffer-line lines-per-row)) | |
182 (if rows-numbered 1 0) | |
183 (floor (1- a-column) columns-per-line))) | |
184 (goal-column (* field-width (% (1- a-column) columns-per-line)))) | |
185 (forward-line (- goal-line buffer-line)) | |
186 (move-to-column-untabify goal-column) | |
187 a-column)) | |
188 | |
189 (defun array-move-one-row (sign) | |
190 "Move one array row in direction SIGN (1 or -1). | |
191 Leave point at the beginning of the field and return the new array row. | |
192 If requested to move beyond the array bounds, signal an error." | |
193 ;; Requires that buffer-line and buffer-column be current. | |
194 (let ((goal-column (array-beginning-of-field)) | |
195 (array-row (or (array-current-row) | |
196 (error "Cursor is not in a valid array cell.")))) | |
197 (cond ((and (= array-row max-row) (= sign 1)) | |
198 (error "End of array.")) | |
199 ((and (= array-row 1) (= sign -1)) | |
200 (error "Beginning of array.")) | |
201 (t | |
202 (progn | |
203 (forward-line (* sign lines-per-row)) | |
204 (move-to-column-untabify goal-column) | |
205 (+ array-row sign)))))) | |
206 | |
207 (defun array-move-one-column (sign) | |
208 "Move one array column in direction SIGN (1 or -1). | |
209 Leave point at the beginning of the field and return the new array column. | |
210 If requested to move beyond the array bounds, signal an error." | |
211 ;; Requires that buffer-line and buffer-column be current. | |
212 (let ((array-column (or (array-current-column) | |
213 (error "Cursor is not in a valid array cell.")))) | |
214 (cond ((and (= array-column max-column) (= sign 1)) | |
215 (error "End of array.")) | |
216 ((and (= array-column 1) (= sign -1)) | |
217 (error "Beginning of array.")) | |
218 (t | |
219 (cond | |
220 ;; Going backward from first column on the line. | |
221 ((and (= sign -1) (= 1 (% array-column columns-per-line))) | |
222 (forward-line -1) | |
223 (move-to-column-untabify | |
224 (* field-width (1- columns-per-line)))) | |
225 ;; Going forward from last column on the line. | |
226 ((and (= sign 1) (zerop (% array-column columns-per-line))) | |
227 (forward-line 1)) | |
228 ;; Somewhere in the middle of the line. | |
229 (t | |
230 (move-to-column-untabify (+ (array-beginning-of-field) | |
231 (* field-width sign))))) | |
232 (+ array-column sign))))) | |
233 | |
234 (defun array-normalize-cursor () | |
235 "Move the cursor to the first non-whitespace character in the field and, | |
236 if necessary, scroll horizontally to keep the cursor in view." | |
237 ;; Assumes point is at the beginning of the field. | |
238 (let ((buffer-column (current-column))) | |
239 (skip-chars-forward " \t" | |
240 (1- (save-excursion (array-end-of-field t) (point)))) | |
241 (array-maybe-scroll-horizontally))) | |
242 | |
243 (defun array-maybe-scroll-horizontally () | |
244 "If necessary, scroll horizontally to keep the cursor in view." | |
245 ;; This is only called from array-normalize-cursor so | |
246 ;; buffer-column will always be current. | |
247 (let ((w-hscroll (window-hscroll)) | |
248 (w-width (window-width))) | |
249 (cond | |
250 ((and (>= buffer-column w-hscroll) | |
251 (<= buffer-column (+ w-hscroll w-width))) | |
252 ;; It's already visible. Do nothing. | |
253 nil) | |
254 ((> buffer-column (+ w-hscroll w-width)) | |
255 ;; It's to the right. Scroll left. | |
256 (scroll-left (- (- buffer-column w-hscroll) | |
257 (/ w-width 2)))) | |
258 (t | |
259 ;; It's to the left. Scroll right. | |
260 (scroll-right (+ (- w-hscroll buffer-column) | |
261 (/ w-width 2))))))) | |
262 | |
263 | |
264 | |
265 ;;; Movement commands. | |
266 | |
267 (defun array-next-row (&optional arg) | |
268 "Move down one array row, staying in the current array column. | |
269 If optional ARG is given, move down ARG array rows." | |
270 (interactive "p") | |
271 (let ((buffer-line (current-line)) | |
272 (buffer-column (current-column))) | |
273 (if (= (abs arg) 1) | |
274 (array-move-one-row arg) | |
275 (array-move-to-row | |
276 (limit-index (+ (or (array-current-row) | |
277 (error "Cursor is not in an array cell.")) | |
278 arg) | |
279 max-row)))) | |
280 (array-normalize-cursor)) | |
281 | |
282 (defun array-previous-row (&optional arg) | |
283 "Move up one array row, staying in the current array column. | |
284 If optional ARG is given, move up ARG array rows." | |
285 (interactive "p") | |
286 (array-next-row (- arg))) | |
287 | |
288 (defun array-forward-column (&optional arg) | |
289 "Move forward one field, staying in the current array row. | |
290 If optional ARG is given, move forward ARG array columns. | |
291 If necessary, keep the cursor in the window by scrolling right or left." | |
292 (interactive "p") | |
293 (let ((buffer-line (current-line)) | |
294 (buffer-column (current-column))) | |
295 (if (= (abs arg) 1) | |
296 (array-move-one-column arg) | |
297 (array-move-to-column | |
298 (limit-index (+ (or (array-current-column) | |
299 (error "Cursor is not in an array cell.")) | |
300 arg) | |
301 max-column)))) | |
302 (array-normalize-cursor)) | |
303 | |
304 (defun array-backward-column (&optional arg) | |
305 "Move backward one field, staying in the current array row. | |
306 If optional ARG is given, move backward ARG array columns. | |
307 If necessary, keep the cursor in the window by scrolling right or left." | |
308 (interactive "p") | |
309 (array-forward-column (- arg))) | |
310 | |
311 (defun array-goto-cell (a-row a-column) | |
312 "Go to array row A-ROW and array column A-COLUMN." | |
313 (interactive "nArray row: \nnArray column: ") | |
314 (array-move-to-cell | |
315 (limit-index a-row max-row) | |
316 (limit-index a-column max-column)) | |
317 (array-normalize-cursor)) | |
318 | |
319 | |
320 | |
321 ;;; Internal copying functions. | |
322 | |
323 (defun array-field-string () | |
324 "Return the field string at the current cursor location." | |
325 ;; Requires that buffer-column be current. | |
326 (buffer-substring | |
327 (save-excursion (array-beginning-of-field t) (point)) | |
328 (save-excursion (array-end-of-field t) (point)))) | |
329 | |
330 (defun array-copy-once-vertically (sign) | |
331 "Copy the current field into one array row in direction SIGN (1 or -1). | |
332 Leave point at the beginning of the field and return the new array row. | |
333 If requested to move beyond the array bounds, signal an error." | |
334 ;; Requires that buffer-line, buffer-column, and copy-string be current. | |
335 (let ((a-row (array-move-one-row sign))) | |
336 (let ((inhibit-quit t)) | |
337 (delete-region (point) (save-excursion (array-end-of-field t) (point))) | |
338 (insert copy-string)) | |
339 (move-to-column buffer-column) | |
340 a-row)) | |
341 | |
342 (defun array-copy-once-horizontally (sign) | |
343 "Copy the current field into one array column in direction SIGN (1 or -1). | |
344 Leave point at the beginning of the field and return the new array column. | |
345 If requested to move beyond the array bounds, signal an error." | |
346 ;; Requires that buffer-line, buffer-column, and copy-string be current. | |
347 (let ((a-column (array-move-one-column sign))) | |
348 (array-update-buffer-position) | |
349 (let ((inhibit-quit t)) | |
350 (delete-region (point) (save-excursion (array-end-of-field t) (point))) | |
351 (insert copy-string)) | |
352 (move-to-column buffer-column) | |
353 a-column)) | |
354 | |
355 (defun array-copy-to-row (a-row) | |
356 "Copy the current field vertically into every cell up to and including A-ROW. | |
357 Leave point at the beginning of the field." | |
358 ;; Requires that buffer-line, buffer-column, array-row, and | |
359 ;; copy-string be current. | |
360 (let* ((num (- a-row array-row)) | |
361 (count (abs num)) | |
362 (sign (if (zerop count) () (/ num count)))) | |
363 (while (> count 0) | |
364 (array-move-one-row sign) | |
365 (array-update-buffer-position) | |
366 (let ((inhibit-quit t)) | |
367 (delete-region (point) (save-excursion (array-end-of-field t) (point))) | |
368 (insert copy-string)) | |
369 (move-to-column buffer-column) | |
370 (setq count (1- count))))) | |
371 | |
372 (defun array-copy-to-column (a-column) | |
373 "Copy the current field horizontally into every cell up to and including | |
374 A-COLUMN. Leave point at the beginning of the field." | |
375 ;; Requires that buffer-line, buffer-column, array-column, and | |
376 ;; copy-string be current. | |
377 (let* ((num (- a-column array-column)) | |
378 (count (abs num)) | |
379 (sign (if (zerop count) () (/ num count)))) | |
380 (while (> count 0) | |
381 (array-move-one-column sign) | |
382 (array-update-buffer-position) | |
383 (let ((inhibit-quit t)) | |
384 (delete-region (point) (save-excursion (array-end-of-field t) (point))) | |
385 (insert copy-string)) | |
386 (move-to-column buffer-column) | |
387 (setq count (1- count))))) | |
388 | |
389 (defun array-copy-to-cell (a-row a-column) | |
390 "Copy the current field into the cell at A-ROW, A-COLUMN. | |
391 Leave point at the beginning of the field." | |
392 ;; Requires that copy-string be current. | |
393 (array-move-to-cell a-row a-column) | |
394 (array-update-buffer-position) | |
395 (delete-region (point) (save-excursion (array-end-of-field t) (point))) | |
396 (insert copy-string) | |
397 (move-to-column buffer-column)) | |
398 | |
399 | |
400 | |
401 ;;; Commands for copying. | |
402 | |
403 (defun array-copy-down (&optional arg) | |
404 "Copy the current field one array row down. | |
405 If optional ARG is given, copy down through ARG array rows." | |
406 (interactive "p") | |
407 (let* ((buffer-line (current-line)) | |
408 (buffer-column (current-column)) | |
409 (array-row (or (array-current-row) | |
410 (error "Cursor is not in a valid array cell."))) | |
411 (copy-string (array-field-string))) | |
412 (if (= (abs arg) 1) | |
413 (array-copy-once-vertically arg) | |
414 (array-copy-to-row | |
415 (limit-index (+ array-row arg) max-row)))) | |
416 (array-normalize-cursor)) | |
417 | |
418 (defun array-copy-up (&optional arg) | |
419 "Copy the current field one array row up. | |
420 If optional ARG is given, copy up through ARG array rows." | |
421 (interactive "p") | |
422 (array-copy-down (- arg))) | |
423 | |
424 (defun array-copy-forward (&optional arg) | |
425 "Copy the current field one array column to the right. | |
426 If optional ARG is given, copy through ARG array columns to the right." | |
427 (interactive "p") | |
428 (let* ((buffer-line (current-line)) | |
429 (buffer-column (current-column)) | |
430 (array-column (or (array-current-column) | |
431 (error "Cursor is not in a valid array cell."))) | |
432 (copy-string (array-field-string))) | |
433 (if (= (abs arg) 1) | |
434 (array-copy-once-horizontally arg) | |
435 (array-copy-to-column | |
436 (limit-index (+ array-column arg) max-column)))) | |
437 (array-normalize-cursor)) | |
438 | |
439 (defun array-copy-backward (&optional arg) | |
440 "Copy the current field one array column to the left. | |
441 If optional ARG is given, copy through ARG array columns to the left." | |
442 (interactive "p") | |
443 (array-copy-forward (- arg))) | |
444 | |
445 (defun array-copy-column-forward (&optional arg) | |
446 "Copy the entire current column in to the column to the right. | |
447 If optional ARG is given, copy through ARG array columns to the right." | |
448 (interactive "p") | |
449 (array-update-buffer-position) | |
450 (array-update-array-position) | |
451 (if (not array-column) | |
452 (error "Cursor is not in a valid array cell.")) | |
453 (message "Working...") | |
454 (let ((this-row 0)) | |
455 (while (< this-row max-row) | |
456 (setq this-row (1+ this-row)) | |
457 (array-move-to-cell this-row array-column) | |
458 (array-update-buffer-position) | |
459 (let ((copy-string (array-field-string))) | |
460 (if (= (abs arg) 1) | |
461 (array-copy-once-horizontally arg) | |
462 (array-copy-to-column | |
463 (limit-index (+ array-column arg) max-column)))))) | |
464 (message "Working...done") | |
465 (array-move-to-row array-row) | |
466 (array-normalize-cursor)) | |
467 | |
468 (defun array-copy-column-backward (&optional arg) | |
469 "Copy the entire current column one column to the left. | |
470 If optional ARG is given, copy through ARG columns to the left." | |
471 (interactive "p") | |
472 (array-copy-column-forward (- arg))) | |
473 | |
474 (defun array-copy-row-down (&optional arg) | |
475 "Copy the entire current row one row down. | |
476 If optional ARG is given, copy through ARG rows down." | |
477 (interactive "p") | |
478 (array-update-buffer-position) | |
479 (array-update-array-position) | |
480 (if (not array-row) | |
481 (error "Cursor is not in a valid array cell.")) | |
482 (cond | |
483 ((and (= array-row 1) (= arg -1)) | |
484 (error "Beginning of array.")) | |
485 ((and (= array-row max-row) (= arg 1)) | |
486 (error "End of array.")) | |
487 (t | |
488 (let* ((copy-string | |
489 (buffer-substring | |
490 (save-excursion (array-move-to-cell array-row 1) | |
491 (point)) | |
492 (save-excursion (array-move-to-cell array-row max-column) | |
493 (forward-line 1) | |
494 (point)))) | |
495 (this-row array-row) | |
496 (goal-row (limit-index (+ this-row arg) max-row)) | |
497 (num (- goal-row this-row)) | |
498 (count (abs num)) | |
499 (sign (if (not (zerop count)) (/ num count)))) | |
500 (while (> count 0) | |
501 (setq this-row (+ this-row sign)) | |
502 (array-move-to-cell this-row 1) | |
503 (let ((inhibit-quit t)) | |
504 (delete-region (point) | |
505 (save-excursion | |
506 (array-move-to-cell this-row max-column) | |
507 (forward-line 1) | |
508 (point))) | |
509 (insert copy-string)) | |
510 (setq count (1- count))) | |
511 (array-move-to-cell goal-row (or array-column 1))))) | |
512 (array-normalize-cursor)) | |
513 | |
514 (defun array-copy-row-up (&optional arg) | |
515 "Copy the entire current array row into the row above. | |
516 If optional ARG is given, copy through ARG rows up." | |
517 (interactive "p") | |
518 (array-copy-row-down (- arg))) | |
519 | |
520 (defun array-fill-rectangle () | |
521 "Copy the field at mark into every cell between mark and point." | |
522 (interactive) | |
523 ;; Bind arguments. | |
524 (array-update-buffer-position) | |
525 (let ((p-row (or (array-current-row) | |
526 (error "Cursor is not in a valid array cell."))) | |
527 (p-column (or (array-current-column) | |
528 (error "Cursor is not in a valid array cell."))) | |
529 (m-row | |
530 (save-excursion | |
531 (exchange-point-and-mark) | |
532 (array-update-buffer-position) | |
533 (or (array-current-row) | |
534 (error "Mark is not in a valid array cell.")))) | |
535 (m-column | |
536 (save-excursion | |
537 (exchange-point-and-mark) | |
538 (array-update-buffer-position) | |
539 (or (array-current-column) | |
540 (error "Mark is not in a valid array cell."))))) | |
541 (message "Working...") | |
542 (let ((top-row (min m-row p-row)) | |
543 (bottom-row (max m-row p-row)) | |
544 (left-column (min m-column p-column)) | |
545 (right-column (max m-column p-column))) | |
546 ;; Do the first row. | |
547 (let ((copy-string | |
548 (save-excursion | |
549 (array-move-to-cell m-row m-column) | |
550 (array-update-buffer-position) | |
551 (array-field-string)))) | |
552 (array-copy-to-cell top-row left-column) | |
553 (array-update-array-position top-row left-column) | |
554 (array-update-buffer-position) | |
555 (array-copy-to-column right-column)) | |
556 ;; Do the rest of the rows. | |
557 (array-move-to-cell top-row left-column) | |
558 (let ((copy-string | |
559 (buffer-substring | |
560 (point) | |
561 (save-excursion | |
562 (array-move-to-cell top-row right-column) | |
563 (setq buffer-column (current-column)) | |
564 (array-end-of-field t) | |
565 (point)))) | |
566 (this-row top-row)) | |
567 (while (/= this-row bottom-row) | |
568 (setq this-row (1+ this-row)) | |
569 (array-move-to-cell this-row left-column) | |
570 (let ((inhibit-quit t)) | |
571 (delete-region | |
572 (point) | |
573 (save-excursion | |
574 (array-move-to-cell this-row right-column) | |
575 (setq buffer-column (current-column)) | |
576 (array-end-of-field t) | |
577 (point))) | |
578 (insert copy-string))))) | |
579 (message "Working...done") | |
580 (array-goto-cell p-row p-column))) | |
581 | |
582 | |
583 | |
584 ;;; Reconfiguration of the array. | |
585 | |
586 (defun array-make-template () | |
587 "Create the template of an array." | |
588 (interactive) | |
589 ;; If there is a conflict between field-width and init-string, resolve it. | |
590 (let ((check t) | |
591 (len)) | |
592 (while check | |
593 (setq init-field (read-input "Initial field value: ")) | |
594 (setq len (length init-field)) | |
595 (if (/= len field-width) | |
596 (if (y-or-n-p (format "Change field width to %d? " len)) | |
597 (progn (setq field-width len) | |
598 (setq check nil))) | |
599 (setq check nil)))) | |
600 (goto-char (point-min)) | |
601 (message "Working...") | |
602 (let ((this-row 1)) | |
603 ;; Loop through the rows. | |
604 (while (<= this-row max-row) | |
605 (if rows-numbered | |
606 (insert (format "%d:\n" this-row))) | |
607 (let ((this-column 1)) | |
608 ;; Loop through the columns. | |
609 (while (<= this-column max-column) | |
610 (insert init-field) | |
611 (if (and (zerop (% this-column columns-per-line)) | |
612 (/= this-column max-column)) | |
613 (newline)) | |
614 (setq this-column (1+ this-column)))) | |
615 (setq this-row (1+ this-row)) | |
616 (newline))) | |
617 (message "Working...done") | |
618 (array-goto-cell 1 1)) | |
619 | |
620 (defun array-reconfigure-rows (new-columns-per-line new-rows-numbered) | |
621 "Reconfigure the state of `rows-numbered' and `columns-per-line'. | |
622 NEW-COLUMNS-PER-LINE is the desired value of `columns-per-line' and | |
623 NEW-ROWS-NUMBERED (a character, either ?y or ?n) is the desired value | |
624 of rows-numbered." | |
625 (interactive "nColumns per line: \ncRows numbered? (y or n) ") | |
626 ;; Check on new-columns-per-line | |
627 (let ((check t)) | |
628 (while check | |
629 (if (and (>= new-columns-per-line 1) | |
630 (<= new-columns-per-line max-column)) | |
631 (setq check nil) | |
632 (setq new-columns-per-line | |
633 (string-to-int | |
634 (read-input | |
635 (format "Columns per line (1 - %d): " max-column))))))) | |
636 ;; Check on new-rows-numbered. It has to be done this way | |
637 ;; because interactive does not have y-or-n-p. | |
638 (cond | |
639 ((eq new-rows-numbered ?y) | |
640 (setq new-rows-numbered t)) | |
641 ((eq new-rows-numbered ?n) | |
642 (setq new-rows-numbered nil)) | |
643 (t | |
644 (setq new-rows-numbered (y-or-n-p "Rows numbered? ")))) | |
645 (message "Working...") | |
646 (array-update-buffer-position) | |
647 (let* ((main-buffer (buffer-name (current-buffer))) | |
648 (temp-buffer (make-temp-name "Array")) | |
649 (temp-max-row max-row) | |
650 (temp-max-column max-column) | |
651 (old-rows-numbered rows-numbered) | |
652 (old-columns-per-line columns-per-line) | |
653 (old-lines-per-row lines-per-row) | |
654 (old-field-width field-width) | |
655 (old-line-length line-length) | |
656 (this-row 1)) | |
657 (array-update-array-position) | |
658 ;; Do the cutting in a temporary buffer. | |
659 (copy-to-buffer temp-buffer (point-min) (point-max)) | |
660 (set-buffer temp-buffer) | |
661 (goto-char (point-min)) | |
662 (while (<= this-row temp-max-row) | |
663 ;; Deal with row number. | |
664 (cond | |
665 ((or (and old-rows-numbered new-rows-numbered) | |
666 (and (not old-rows-numbered) (not new-rows-numbered))) | |
667 ;; Nothing is changed. | |
668 ()) | |
669 ((and old-rows-numbered (not new-rows-numbered)) | |
670 ;; Delete the row number. | |
671 (kill-line 1)) | |
672 (t | |
673 ;; Add the row number. | |
674 (insert-string (format "%d:\n" this-row)))) | |
675 ;; Deal with the array columns in this row. | |
676 (cond | |
677 ((= old-columns-per-line new-columns-per-line) | |
678 ;; Nothing is changed. Go to the next row. | |
679 (forward-line (- old-lines-per-row (if old-rows-numbered 1 0)))) | |
680 (t | |
681 ;; First expand the row. Then cut it up into new pieces. | |
682 (let ((newlines-to-be-removed | |
683 (floor (1- temp-max-column) old-columns-per-line)) | |
684 (newlines-removed 0) | |
685 (newlines-to-be-added | |
686 (floor (1- temp-max-column) new-columns-per-line)) | |
687 (newlines-added 0)) | |
688 (while (< newlines-removed newlines-to-be-removed) | |
689 (move-to-column-untabify | |
690 (* (1+ newlines-removed) old-line-length)) | |
691 (kill-line 1) | |
692 (setq newlines-removed (1+ newlines-removed))) | |
693 (beginning-of-line) | |
694 (while (< newlines-added newlines-to-be-added) | |
695 (move-to-column-untabify (* old-field-width new-columns-per-line)) | |
696 (newline) | |
697 (setq newlines-added (1+ newlines-added))) | |
698 (forward-line 1)))) | |
699 (setq this-row (1+ this-row))) | |
700 (let ((inhibit-quit t)) | |
701 (set-buffer main-buffer) | |
702 (erase-buffer) | |
703 (insert-buffer temp-buffer) | |
704 ;; Update local variables. | |
705 (setq columns-per-line new-columns-per-line) | |
706 (setq rows-numbered new-rows-numbered) | |
707 (setq line-length (* old-field-width new-columns-per-line)) | |
708 (setq lines-per-row | |
4514
e64f012944e0
(abs, ceiling, floor): Remove, since they now redefine
Paul Eggert <eggert@twinsun.com>
parents:
2307
diff
changeset
|
709 (+ (floor (1- temp-max-column) new-columns-per-line) |
e64f012944e0
(abs, ceiling, floor): Remove, since they now redefine
Paul Eggert <eggert@twinsun.com>
parents:
2307
diff
changeset
|
710 (if new-rows-numbered 2 1))) |
180 | 711 (array-goto-cell (or array-row 1) (or array-column 1))) |
712 (kill-buffer temp-buffer)) | |
713 (message "Working...done")) | |
714 | |
715 (defun array-expand-rows () | |
716 "Expand the rows so each fits on one line and remove row numbers." | |
717 (interactive) | |
718 (array-reconfigure-rows max-column ?n)) | |
719 | |
720 | |
721 | |
722 ;;; Utilities. | |
723 | |
724 (defun limit-index (index limit) | |
725 (cond ((< index 1) 1) | |
726 ((> index limit) limit) | |
727 (t index))) | |
728 | |
729 (defun xor (pred1 pred2) | |
730 "Returns the logical exclusive or of predicates PRED1 and PRED2." | |
731 (and (or pred1 pred2) | |
732 (not (and pred1 pred2)))) | |
733 | |
734 (defun current-line () | |
735 "Return the current buffer line at point. The first line is 0." | |
736 (save-excursion | |
737 (beginning-of-line) | |
738 (count-lines (point-min) (point)))) | |
739 | |
740 (defun move-to-column-untabify (column) | |
741 "Move to COLUMN on the current line, untabifying if necessary. | |
742 Return COLUMN." | |
743 (or (and (= column (move-to-column column)) | |
744 column) | |
745 ;; There is a tab in the way. | |
746 (if respect-tabs | |
747 (error "There is a TAB character in the way.") | |
748 (progn | |
749 (untabify-backward) | |
750 (move-to-column column))))) | |
751 | |
752 (defun untabify-backward () | |
753 "Untabify the preceding tab." | |
754 (save-excursion | |
755 (let ((start (point))) | |
756 (backward-char 1) | |
757 (untabify (point) start)))) | |
758 | |
759 | |
760 | |
761 ;;; Array mode. | |
762 | |
763 (defvar array-mode-map nil | |
764 "Keymap used in array mode.") | |
765 | |
766 (if array-mode-map | |
767 () | |
768 (setq array-mode-map (make-keymap)) | |
769 ;; Bind keys. | |
770 (define-key array-mode-map "\M-ad" 'array-display-local-variables) | |
771 (define-key array-mode-map "\M-am" 'array-make-template) | |
772 (define-key array-mode-map "\M-ae" 'array-expand-rows) | |
773 (define-key array-mode-map "\M-ar" 'array-reconfigure-rows) | |
774 (define-key array-mode-map "\M-a=" 'array-what-position) | |
775 (define-key array-mode-map "\M-ag" 'array-goto-cell) | |
776 (define-key array-mode-map "\M-af" 'array-fill-rectangle) | |
777 (define-key array-mode-map "\C-n" 'array-next-row) | |
778 (define-key array-mode-map "\C-p" 'array-previous-row) | |
779 (define-key array-mode-map "\C-f" 'array-forward-column) | |
780 (define-key array-mode-map "\C-b" 'array-backward-column) | |
781 (define-key array-mode-map "\M-n" 'array-copy-down) | |
782 (define-key array-mode-map "\M-p" 'array-copy-up) | |
783 (define-key array-mode-map "\M-f" 'array-copy-forward) | |
784 (define-key array-mode-map "\M-b" 'array-copy-backward) | |
785 (define-key array-mode-map "\M-\C-n" 'array-copy-row-down) | |
786 (define-key array-mode-map "\M-\C-p" 'array-copy-row-up) | |
787 (define-key array-mode-map "\M-\C-f" 'array-copy-column-forward) | |
788 (define-key array-mode-map "\M-\C-b" 'array-copy-column-backward)) | |
789 | |
790 (put 'array-mode 'mode-class 'special) | |
791 | |
792 (defun array-mode () | |
793 "Major mode for editing arrays. | |
794 | |
795 Array mode is a specialized mode for editing arrays. An array is | |
796 considered to be a two-dimensional set of strings. The strings are | |
797 NOT recognized as integers or real numbers. | |
798 | |
799 The array MUST reside at the top of the buffer. | |
800 | |
801 TABs are not respected, and may be converted into spaces at any time. | |
802 Setting the variable 'respect-tabs to non-nil will prevent TAB conversion, | |
803 but will cause many functions to give errors if they encounter one. | |
804 | |
805 Upon entering array mode, you will be prompted for the values of | |
806 several variables. Others will be calculated based on the values you | |
807 supply. These variables are all local the the buffer. Other buffer | |
808 in array mode may have different values assigned to the variables. | |
809 The variables are: | |
810 | |
811 Variables you assign: | |
812 max-row: The number of rows in the array. | |
813 max-column: The number of columns in the array. | |
814 columns-per-line: The number of columns in the array per line of buffer. | |
815 field-width: The width of each field, in characters. | |
816 rows-numbered: A logical variable describing whether to ignore | |
817 row numbers in the buffer. | |
818 | |
819 Variables which are calculated: | |
820 line-length: The number of characters in a buffer line. | |
821 lines-per-row: The number of buffer lines used to display each row. | |
822 | |
823 The following commands are available (an asterisk indicates it may | |
824 take a numeric prefix argument): | |
825 | |
826 * \\<array-mode-map>\\[array-forward-column] Move forward one column. | |
827 * \\[array-backward-column] Move backward one column. | |
828 * \\[array-next-row] Move down one row. | |
829 * \\[array-previous-row] Move up one row. | |
830 | |
831 * \\[array-copy-forward] Copy the current field into the column to the right. | |
832 * \\[array-copy-backward] Copy the current field into the column to the left. | |
833 * \\[array-copy-down] Copy the current field into the row below. | |
834 * \\[array-copy-up] Copy the current field into the row above. | |
835 | |
836 * \\[array-copy-column-forward] Copy the current column into the column to the right. | |
837 * \\[array-copy-column-backward] Copy the current column into the column to the left. | |
838 * \\[array-copy-row-down] Copy the current row into the row below. | |
839 * \\[array-copy-row-up] Copy the current row into the row above. | |
840 | |
841 \\[array-fill-rectangle] Copy the field at mark into every cell with row and column | |
842 between that of point and mark. | |
843 | |
844 \\[array-what-position] Display the current array row and column. | |
845 \\[array-goto-cell] Go to a particular array cell. | |
846 | |
847 \\[array-make-template] Make a template for a new array. | |
848 \\[array-reconfigure-rows] Reconfigure the array. | |
849 \\[array-expand-rows] Expand the array (remove row numbers and | |
850 newlines inside rows) | |
851 | |
852 \\[array-display-local-variables] Display the current values of local variables. | |
853 | |
854 Entering array mode calls the function `array-mode-hook'." | |
855 | |
856 (interactive) | |
857 ;; Number of rows in the array. | |
858 (make-local-variable 'max-row) | |
859 ;; Number of columns in the array. | |
860 (make-local-variable 'max-column) | |
861 ;; Number of array columns per line. | |
862 (make-local-variable 'columns-per-line) | |
863 ;; Width of a field in the array. | |
864 (make-local-variable 'field-width) | |
865 ;; Are rows numbered in the buffer? | |
866 (make-local-variable 'rows-numbered) | |
867 ;; Length of a line in the array. | |
868 (make-local-variable 'line-length) | |
869 ;; Number of lines per array row. | |
870 (make-local-variable 'lines-per-row) | |
871 ;; Current line number of point in the buffer. | |
872 (make-local-variable 'buffer-line) | |
873 ;; Current column number of point in the buffer. | |
874 (make-local-variable 'buffer-column) | |
875 ;; Current array row location of point. | |
876 (make-local-variable 'array-row) | |
877 ;; Current array column location of point. | |
878 (make-local-variable 'array-column) | |
879 ;; Current field string being copied. | |
880 (make-local-variable 'copy-string) | |
881 ;; Should TAB conversion be prevented? | |
882 (make-local-variable 'respect-tabs) | |
883 (setq respect-tabs nil) | |
884 (array-init-local-variables) | |
885 (setq major-mode 'array-mode) | |
886 (setq mode-name "Array") | |
887 ;; Update mode-line. | |
888 (progn (save-excursion (set-buffer (other-buffer))) | |
889 (set-buffer-modified-p (buffer-modified-p)) | |
890 (sit-for 0)) | |
891 (make-variable-buffer-local 'truncate-lines) | |
892 (setq truncate-lines t) | |
893 (setq overwrite-mode t) | |
894 (use-local-map array-mode-map) | |
895 (run-hooks 'array-mode-hook)) | |
896 | |
897 | |
898 | |
899 ;;; Initialization functions. These are not interactive. | |
900 | |
901 (defun array-init-local-variables () | |
902 "Initialize the variables associated with the | |
903 array in this buffer." | |
904 (array-init-max-row) | |
905 (array-init-max-column) | |
906 (array-init-columns-per-line) | |
907 (array-init-field-width) | |
908 (array-init-rows-numbered) | |
909 (array-init-line-length) | |
910 (array-init-lines-per-row) | |
911 (message "")) | |
912 | |
913 (defun array-init-max-row (&optional arg) | |
914 "Initialize the value of max-row." | |
915 (setq max-row | |
916 (or arg (string-to-int (read-input "Number of array rows: "))))) | |
917 | |
918 (defun array-init-max-column (&optional arg) | |
919 "Initialize the value of max-column." | |
920 (setq max-column | |
921 (or arg (string-to-int (read-input "Number of array columns: "))))) | |
922 | |
923 (defun array-init-columns-per-line (&optional arg) | |
924 "Initialize the value of columns-per-line." | |
925 (setq columns-per-line | |
926 (or arg (string-to-int (read-input "Array columns per line: "))))) | |
927 | |
928 (defun array-init-field-width (&optional arg) | |
929 "Initialize the value of field-width." | |
930 (setq field-width | |
931 (or arg (string-to-int (read-input "Field width: "))))) | |
932 | |
933 (defun array-init-rows-numbered (&optional arg) | |
934 "Initialize the value of rows-numbered." | |
935 (setq rows-numbered | |
936 (or arg (y-or-n-p "Rows numbered? ")))) | |
937 | |
938 (defun array-init-line-length (&optional arg) | |
939 "Initialize the value of line-length." | |
940 (setq line-length | |
941 (or arg | |
942 (* field-width columns-per-line)))) | |
943 | |
944 (defun array-init-lines-per-row (&optional arg) | |
945 "Initialize the value of lines-per-row." | |
946 (setq lines-per-row | |
947 (or arg | |
4514
e64f012944e0
(abs, ceiling, floor): Remove, since they now redefine
Paul Eggert <eggert@twinsun.com>
parents:
2307
diff
changeset
|
948 (+ (floor (1- max-column) columns-per-line) |
e64f012944e0
(abs, ceiling, floor): Remove, since they now redefine
Paul Eggert <eggert@twinsun.com>
parents:
2307
diff
changeset
|
949 (if rows-numbered 2 1))))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
950 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
180
diff
changeset
|
951 ;;; array.el ends here |