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
6 ;; Copyright (C) 1987 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
31 ;;; Internal information functions.
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.
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
46 (and (zerop (%
(1+ buffer-line
) lines-per-row
))
47 (not (zerop columns-last-line
))
48 (>= buffer-column
(* columns-last-line field-width
)))))))
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
))))
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
))))
64 ;; Array columns due to line differences.
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
))))
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))))
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)))
86 ;;; Information commands.
88 (defun array-what-position ()
89 "Display the row and column in which the cursor is positioned."
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))))))
97 (defun array-display-local-variables ()
98 "Display the current state of the local variables in the minibuffer."
100 (let ((buf (buffer-name (current-buffer))))
101 (with-output-to-temp-buffer "*Local Variables*"
102 (buffer-disable-undo standard-output
)
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
))))))
122 ;;; Internal movement functions.
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
))))
130 (move-to-column-untabify goal-column
)
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
))
140 (move-to-column-untabify goal-column
)
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
)))
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
)
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
)
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."))
191 (forward-line (* sign lines-per-row
))
192 (move-to-column-untabify goal-column
)
193 (+ array-row sign
))))))
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."))
208 ;; Going backward from first column on the line.
209 ((and (= sign -
1) (= 1 (% array-column columns-per-line
)))
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
)))
216 ;; Somewhere in the middle of the line.
218 (move-to-column-untabify (+ (array-beginning-of-field)
219 (* field-width sign
)))))
220 (+ array-column sign
)))))
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)))
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)))
238 ((and (>= buffer-column w-hscroll
)
239 (<= buffer-column
(+ w-hscroll w-width
)))
240 ;; It's already visible. Do nothing.
242 ((> buffer-column
(+ w-hscroll w-width
))
243 ;; It's to the right. Scroll left.
244 (scroll-left (- (- buffer-column w-hscroll
)
247 ;; It's to the left. Scroll right.
248 (scroll-right (+ (- w-hscroll buffer-column
)
253 ;;; Movement commands.
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."
259 (let ((buffer-line (current-line))
260 (buffer-column (current-column)))
262 (array-move-one-row arg
)
264 (limit-index (+ (or (array-current-row)
265 (error "Cursor is not in an array cell."))
268 (array-normalize-cursor))
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."
274 (array-next-row (- arg
)))
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."
281 (let ((buffer-line (current-line))
282 (buffer-column (current-column)))
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."))
290 (array-normalize-cursor))
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."
297 (array-forward-column (- arg
)))
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: ")
303 (limit-index a-row max-row
)
304 (limit-index a-column max-column
))
305 (array-normalize-cursor))
309 ;;; Internal copying functions.
311 (defun array-field-string ()
312 "Return the field string at the current cursor location."
313 ;; Requires that buffer-column be current.
315 (save-excursion (array-beginning-of-field t
) (point))
316 (save-excursion (array-end-of-field t
) (point))))
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
)
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
)
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
))
350 (sign (if (zerop count
) () (/ num count
))))
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
)))))
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
))
367 (sign (if (zerop count
) () (/ num count
))))
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
)))))
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)))
385 (move-to-column buffer-column
))
389 ;;; Commands for copying.
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."
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)))
401 (array-copy-once-vertically arg
)
403 (limit-index (+ array-row arg
) max-row
))))
404 (array-normalize-cursor))
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."
410 (array-copy-down (- arg
)))
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."
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)))
422 (array-copy-once-horizontally arg
)
423 (array-copy-to-column
424 (limit-index (+ array-column arg
) max-column
))))
425 (array-normalize-cursor))
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."
431 (array-copy-forward (- arg
)))
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."
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...")
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)))
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))
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."
460 (array-copy-column-forward (- arg
)))
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."
466 (array-update-buffer-position)
467 (array-update-array-position)
469 (error "Cursor is not in a valid array cell."))
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."))
478 (save-excursion (array-move-to-cell array-row
1)
480 (save-excursion (array-move-to-cell array-row max-column
)
484 (goal-row (limit-index (+ this-row arg
) max-row
))
485 (num (- goal-row this-row
))
487 (sign (if (not (zerop count
)) (/ num count
))))
489 (setq this-row
(+ this-row sign
))
490 (array-move-to-cell this-row
1)
491 (let ((inhibit-quit t
))
492 (delete-region (point)
494 (array-move-to-cell this-row max-column
)
497 (insert copy-string
))
498 (setq count
(1- count
)))
499 (array-move-to-cell goal-row
(or array-column
1)))))
500 (array-normalize-cursor))
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."
506 (array-copy-row-down (- arg
)))
508 (defun array-fill-rectangle ()
509 "Copy the field at mark into every cell between mark and point."
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.")))
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."))))
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
)))
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
)
550 (array-move-to-cell top-row right-column
)
551 (setq buffer-column
(current-column))
552 (array-end-of-field t
)
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
))
562 (array-move-to-cell this-row right-column
)
563 (setq buffer-column
(current-column))
564 (array-end-of-field t
)
566 (insert copy-string
)))))
567 (message "Working...done")
568 (array-goto-cell p-row p-column
)))
572 ;;; Reconfiguration of the array.
574 (defun array-make-template ()
575 "Create the template of an array."
577 ;; If there is a conflict between field-width and init-string, resolve it.
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
)
588 (goto-char (point-min))
589 (message "Working...")
591 ;; Loop through the rows.
592 (while (<= this-row max-row
)
594 (insert (format "%d:\n" this-row
)))
595 (let ((this-column 1))
596 ;; Loop through the columns.
597 (while (<= this-column max-column
)
599 (if (and (zerop (% this-column columns-per-line
))
600 (/= this-column max-column
))
602 (setq this-column
(1+ this-column
))))
603 (setq this-row
(1+ this-row
))
605 (message "Working...done")
606 (array-goto-cell 1 1))
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
613 (interactive "nColumns per line: \ncRows numbered? (y or n) ")
614 ;; Check on new-columns-per-line
617 (if (and (>= new-columns-per-line
1)
618 (<= new-columns-per-line max-column
))
620 (setq new-columns-per-line
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.
627 ((eq new-rows-numbered ?y
)
628 (setq new-rows-numbered t
))
629 ((eq new-rows-numbered ?n
)
630 (setq new-rows-numbered nil
))
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
)
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.
653 ((or (and old-rows-numbered new-rows-numbered
)
654 (and (not old-rows-numbered
) (not new-rows-numbered
)))
655 ;; Nothing is changed.
657 ((and old-rows-numbered
(not new-rows-numbered
))
658 ;; Delete the row number.
661 ;; Add the row number.
662 (insert-string (format "%d:\n" this-row
))))
663 ;; Deal with the array columns in this row.
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))))
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
))
673 (newlines-to-be-added
674 (floor (1- temp-max-column
) new-columns-per-line
))
676 (while (< newlines-removed newlines-to-be-removed
)
677 (move-to-column-untabify
678 (* (1+ newlines-removed
) old-line-length
))
680 (setq newlines-removed
(1+ newlines-removed
)))
682 (while (< newlines-added newlines-to-be-added
)
683 (move-to-column-untabify (* old-field-width new-columns-per-line
))
685 (setq newlines-added
(1+ newlines-added
)))
687 (setq this-row
(1+ this-row
)))
688 (let ((inhibit-quit t
))
689 (set-buffer main-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
))
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"))
703 (defun array-expand-rows ()
704 "Expand the rows so each fits on one line and remove row numbers."
706 (array-reconfigure-rows max-column ?n
))
712 (defun limit-index (index limit
)
713 (cond ((< index
1) 1)
714 ((> index limit
) limit
)
718 "Return the absolute value of INT."
719 (if (< int
0) (- int
) int
))
722 (defun floor (int1 int2
)
723 "Returns the floor of INT1 divided by INT2.
724 INT1 may be negative. INT2 must be positive."
726 (- (ceiling (- int1
) int2
))
729 (defun ceiling (int1 int2
)
730 "Returns the ceiling of INT1 divided by INT2.
731 Assumes that both arguments are nonnegative."
733 (if (zerop (mod int1 int2
))
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
))))
742 (defun current-line ()
743 "Return the current buffer line at point. The first line is 0."
746 (count-lines (point-min) (point))))
748 (defun move-to-column-untabify (column)
749 "Move to COLUMN on the current line, untabifying if necessary.
751 (or (and (= column
(move-to-column column
))
753 ;; There is a tab in the way.
755 (error "There is a TAB character in the way.")
758 (move-to-column column
)))))
760 (defun untabify-backward ()
761 "Untabify the preceding tab."
763 (let ((start (point)))
765 (untabify (point) start
))))
771 (defvar array-mode-map nil
772 "Keymap used in array mode.")
776 (setq array-mode-map
(make-keymap))
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
))
798 (put 'array-mode
'mode-class
'special
)
801 "Major mode for editing arrays.
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.
807 The array MUST reside at the top of the buffer.
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.
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.
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.
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.
831 The following commands are available (an asterisk indicates it may
832 take a numeric prefix argument):
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.
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.
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.
849 \\[array-fill-rectangle] Copy the field at mark into every cell with row and column
850 between that of point and mark.
852 \\[array-what-position] Display the current array row and column.
853 \\[array-goto-cell] Go to a particular array cell.
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)
860 \\[array-display-local-variables] Display the current values of local variables.
862 Entering array mode calls the function `array-mode-hook'."
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")
896 (progn (save-excursion (set-buffer (other-buffer)))
897 (set-buffer-modified-p (buffer-modified-p))
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
))
907 ;;; Initialization functions. These are not interactive.
909 (defun array-init-local-variables ()
910 "Initialize the variables associated with the
911 array in this buffer."
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)
921 (defun array-init-max-row (&optional arg
)
922 "Initialize the value of max-row."
924 (or arg
(string-to-int (read-input "Number of array rows: ")))))
926 (defun array-init-max-column (&optional arg
)
927 "Initialize the value of max-column."
929 (or arg
(string-to-int (read-input "Number of array columns: ")))))
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: ")))))
936 (defun array-init-field-width (&optional arg
)
937 "Initialize the value of field-width."
939 (or arg
(string-to-int (read-input "Field width: ")))))
941 (defun array-init-rows-numbered (&optional arg
)
942 "Initialize the value of rows-numbered."
944 (or arg
(y-or-n-p "Rows numbered? "))))
946 (defun array-init-line-length (&optional arg
)
947 "Initialize the value of line-length."
950 (* field-width columns-per-line
))))
952 (defun array-init-lines-per-row (&optional arg
)
953 "Initialize the value of lines-per-row."
956 (+ (ceiling max-column columns-per-line
)
957 (if rows-numbered
1 0)))))