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