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