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