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