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