* lisp/term/screen.el (xterm-screen-extra-capabilities): New custom
[emacs.git] / lisp / array.el
Commit [+]AuthorDateLineData
55535639 Pavel Janík2001-07-15 16:15:35 +00001;;; array.el --- array editing commands for GNU Emacs
c0274f38 Eric S. Raymond1992-05-30 23:54:21 +00002
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1987, 2000-2015 Free Software Foundation, Inc.
9750e079 Eric S. Raymond1992-07-22 04:22:30 +00004
d53fa592 Glenn Morris2009-01-10 22:53:56 +00005;; Author: David M. Brown
34dc21db Glenn Morris2014-02-09 17:34:22 -08006;; Maintainer: emacs-devel@gnu.org
e9571d2a Eric S. Raymond1993-03-18 21:29:42 +00007;; Keywords: extensions
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +00008
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +00009;; This file is part of GNU Emacs.
10
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000012;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
Glenn Morris2008-05-06 08:06:51 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000015
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf Glenn Morris2008-05-06 08:06:51 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000023
e5167999
ER
Eric S. Raymond1992-07-16 21:47:34 +000024;;; Commentary:
25
e41b2db1
ER
Eric S. Raymond1993-03-22 03:27:18 +000026;; 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.
29
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
33
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.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000038
39\f
e5167999 Eric S. Raymond1992-07-16 21:47:34 +000040;;; Code:
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000041
7a9ac688
SM
Stefan Monnier2006-01-18 20:36:19 +000042(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?")
41674a5a Dave Love2000-01-06 23:12:02 +000055
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000056;;; Internal information functions.
71296446 Juanma Barranquero2003-02-04 11:26:42 +000057
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000058(defun array-cursor-in-array-range ()
41674a5a Dave Love2000-01-06 23:12:02 +000059 "Return t if the cursor is in a valid array cell.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000060Its ok to be on a row number line."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +000061 (let ((columns-last-line (% array-max-column array-columns-per-line)))
62 ;; Requires array-buffer-line and array-buffer-column to be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000063 (not (or
64 ;; The cursor is too far to the right.
41674a5a Dave Love2000-01-06 23:12:02 +000065 (>= array-buffer-column array-line-length)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000066 ;; The cursor is below the last row.
41674a5a Dave Love2000-01-06 23:12:02 +000067 (>= array-buffer-line (* array-lines-per-row array-max-row))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000068 ;; 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.
41674a5a Dave Love2000-01-06 23:12:02 +000071 (and (zerop (% (1+ array-buffer-line) array-lines-per-row))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000072 (not (zerop columns-last-line))
41674a5a Dave Love2000-01-06 23:12:02 +000073 (>= array-buffer-column (* columns-last-line array-field-width)))))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000074
75(defun array-current-row ()
76 "Return the array row of the field in which the cursor is located."
41674a5a Dave Love2000-01-06 23:12:02 +000077 ;; Requires array-buffer-line and array-buffer-column to be current.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000078 (and (array-cursor-in-array-range)
41674a5a Dave Love2000-01-06 23:12:02 +000079 (1+ (floor array-buffer-line array-lines-per-row))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000080
81(defun array-current-column ()
82 "Return the array column of the field in which the cursor is located."
41674a5a Dave Love2000-01-06 23:12:02 +000083 ;; Requires array-buffer-line and array-buffer-column to be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000084 (and (array-cursor-in-array-range)
85 ;; It's not okay to be on a row number line.
41674a5a
DL
Dave Love2000-01-06 23:12:02 +000086 (not (and array-rows-numbered
87 (zerop (% array-buffer-line array-lines-per-row))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000088 (+
89 ;; Array columns due to line differences.
41674a5a
DL
Dave Love2000-01-06 23:12:02 +000090 (* 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)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +000094 ;; Array columns on the current line.
41674a5a Dave Love2000-01-06 23:12:02 +000095 (1+ (floor array-buffer-column array-field-width)))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +000096
97(defun array-update-array-position (&optional a-row a-column)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +000098 "Set `array-row' and `array-column' to their current values.
99Set 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.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000101 (setq array-row (or a-row (array-current-row))
102 array-column (or a-column (array-current-column))))
103
104(defun array-update-buffer-position ()
7a9ac688 Stefan Monnier2006-01-18 20:36:19 +0000105 "Set `array-buffer-line' and `array-buffer-column' to their current values."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000106 (setq array-buffer-line (current-line)
107 array-buffer-column (current-column)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000108
109\f
110
111;;; Information commands.
112
113(defun array-what-position ()
114 "Display the row and column in which the cursor is positioned."
115 (interactive)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000116 (let ((array-buffer-line (current-line))
117 (array-buffer-column (current-column)))
118 (message "Array row: %s Array column: %s"
4bf785ba
KH
Karl Heuer1996-01-25 00:59:25 +0000119 (prin1-to-string (array-current-row))
120 (prin1-to-string (array-current-column)))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000121
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"
41674a5a Dave Love2000-01-06 23:12:02 +0000131 (prin1-to-string array-max-row)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000132 (princ (format " max-column: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000133 (prin1-to-string array-max-column)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000134 (princ (format " columns-per-line: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000135 (prin1-to-string array-columns-per-line)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000136 (princ (format " field-width: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000137 (prin1-to-string array-field-width)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000138 (princ (format " rows-numbered: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000139 (prin1-to-string array-rows-numbered)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000140 (princ (format " lines-per-row: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000141 (prin1-to-string array-lines-per-row)))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000142 (princ (format " line-length: %s\n"
41674a5a Dave Love2000-01-06 23:12:02 +0000143 (prin1-to-string array-line-length))))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000144
145\f
146
147;;; Internal movement functions.
148
149(defun array-beginning-of-field (&optional go-there)
150 "Return the column of the beginning of the current field.
151Optional argument GO-THERE, if non-nil, means go there too."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000152 ;; Requires that array-buffer-column be current.
153 (let ((goal-column (- array-buffer-column (% array-buffer-column array-field-width))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000154 (if go-there
155 (move-to-column-untabify goal-column)
156 goal-column)))
157
158(defun array-end-of-field (&optional go-there)
159 "Return the column of the end of the current array field.
160If optional argument GO-THERE is non-nil, go there too."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000161 ;; Requires that array-buffer-column be current.
162 (let ((goal-column (+ (- array-buffer-column (% array-buffer-column array-field-width))
163 array-field-width)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000164 (if go-there
165 (move-to-column-untabify goal-column)
166 goal-column)))
167
168(defun array-move-to-cell (a-row a-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000169 "Move to array row A-ROW and array column A-COLUMN.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000170Leave point at the beginning of the field and return the new buffer column."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000171 (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))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000175 (goto-char (point-min))
176 (forward-line goal-line)
177 (move-to-column-untabify goal-column)))
178
179(defun array-move-to-row (a-row)
180 "Move to array row A-ROW preserving the current array column.
181Leave point at the beginning of the field and return the new array row."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000182 ;; 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))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000187 (move-to-column-untabify goal-column)
188 a-row))
189
190(defun array-move-to-column (a-column)
191 "Move to array column A-COLUMN preserving the current array row.
192Leave point at the beginning of the field and return the new array column."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000193 ;; 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))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000199 (move-to-column-untabify goal-column)
200 a-column))
201
202(defun array-move-one-row (sign)
203 "Move one array row in direction SIGN (1 or -1).
204Leave point at the beginning of the field and return the new array row.
205If requested to move beyond the array bounds, signal an error."
41674a5a Dave Love2000-01-06 23:12:02 +0000206 ;; Requires that array-buffer-line and array-buffer-column be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000207 (let ((goal-column (array-beginning-of-field))
208 (array-row (or (array-current-row)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000209 (error "Cursor is not in a valid array cell"))))
210 (cond ((and (= array-row array-max-row) (= sign 1))
211 (error "End of array"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000212 ((and (= array-row 1) (= sign -1))
41674a5a Dave Love2000-01-06 23:12:02 +0000213 (error "Beginning of array"))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000214 (t
215 (progn
41674a5a Dave Love2000-01-06 23:12:02 +0000216 (forward-line (* sign array-lines-per-row))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000217 (move-to-column-untabify goal-column)
218 (+ array-row sign))))))
219
220(defun array-move-one-column (sign)
221 "Move one array column in direction SIGN (1 or -1).
222Leave point at the beginning of the field and return the new array column.
223If requested to move beyond the array bounds, signal an error."
41674a5a Dave Love2000-01-06 23:12:02 +0000224 ;; Requires that array-buffer-line and array-buffer-column be current.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000225 (let ((array-column (or (array-current-column)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000226 (error "Cursor is not in a valid array cell"))))
227 (cond ((and (= array-column array-max-column) (= sign 1))
228 (error "End of array"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000229 ((and (= array-column 1) (= sign -1))
41674a5a Dave Love2000-01-06 23:12:02 +0000230 (error "Beginning of array"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000231 (t
41674a5a Dave Love2000-01-06 23:12:02 +0000232 (cond
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000233 ;; Going backward from first column on the line.
41674a5a Dave Love2000-01-06 23:12:02 +0000234 ((and (= sign -1) (= 1 (% array-column array-columns-per-line)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000235 (forward-line -1)
236 (move-to-column-untabify
41674a5a Dave Love2000-01-06 23:12:02 +0000237 (* array-field-width (1- array-columns-per-line))))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000238 ;; Going forward from last column on the line.
41674a5a Dave Love2000-01-06 23:12:02 +0000239 ((and (= sign 1) (zerop (% array-column array-columns-per-line)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000240 (forward-line 1))
241 ;; Somewhere in the middle of the line.
242 (t
243 (move-to-column-untabify (+ (array-beginning-of-field)
41674a5a Dave Love2000-01-06 23:12:02 +0000244 (* array-field-width sign)))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000245 (+ array-column sign)))))
246
247(defun array-normalize-cursor ()
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000248 "Move the cursor to the first non-whitespace character in the field.
249If necessary, scroll horizontally to keep the cursor in view."
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000250 ;; Assumes point is at the beginning of the field.
41674a5a Dave Love2000-01-06 23:12:02 +0000251 (let ((array-buffer-column (current-column)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000252 (skip-chars-forward " \t"
253 (1- (save-excursion (array-end-of-field t) (point))))
254 (array-maybe-scroll-horizontally)))
255
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
41674a5a Dave Love2000-01-06 23:12:02 +0000259 ;; array-buffer-column will always be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000260 (let ((w-hscroll (window-hscroll))
261 (w-width (window-width)))
262 (cond
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000263 ((and (>= array-buffer-column w-hscroll)
264 (<= array-buffer-column (+ w-hscroll w-width)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000265 ;; It's already visible. Do nothing.
266 nil)
41674a5a Dave Love2000-01-06 23:12:02 +0000267 ((> array-buffer-column (+ w-hscroll w-width))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000268 ;; It's to the right. Scroll left.
41674a5a Dave Love2000-01-06 23:12:02 +0000269 (scroll-left (- (- array-buffer-column w-hscroll)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000270 (/ w-width 2))))
271 (t
272 ;; It's to the left. Scroll right.
41674a5a Dave Love2000-01-06 23:12:02 +0000273 (scroll-right (+ (- w-hscroll array-buffer-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000274 (/ w-width 2)))))))
275
276\f
277
278;;; Movement commands.
279
280(defun array-next-row (&optional arg)
281 "Move down one array row, staying in the current array column.
282If optional ARG is given, move down ARG array rows."
283 (interactive "p")
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000284 (let ((array-buffer-line (current-line))
285 (array-buffer-column (current-column)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000286 (if (= (abs arg) 1)
287 (array-move-one-row arg)
288 (array-move-to-row
289 (limit-index (+ (or (array-current-row)
41674a5a Dave Love2000-01-06 23:12:02 +0000290 (error "Cursor is not in an array cell"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000291 arg)
41674a5a Dave Love2000-01-06 23:12:02 +0000292 array-max-row))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000293 (array-normalize-cursor))
294
295(defun array-previous-row (&optional arg)
296 "Move up one array row, staying in the current array column.
297If optional ARG is given, move up ARG array rows."
298 (interactive "p")
299 (array-next-row (- arg)))
300
301(defun array-forward-column (&optional arg)
302 "Move forward one field, staying in the current array row.
303If optional ARG is given, move forward ARG array columns.
304If necessary, keep the cursor in the window by scrolling right or left."
305 (interactive "p")
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000306 (let ((array-buffer-line (current-line))
307 (array-buffer-column (current-column)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000308 (if (= (abs arg) 1)
309 (array-move-one-column arg)
310 (array-move-to-column
311 (limit-index (+ (or (array-current-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000312 (error "Cursor is not in an array cell"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000313 arg)
41674a5a Dave Love2000-01-06 23:12:02 +0000314 array-max-column))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000315 (array-normalize-cursor))
316
317(defun array-backward-column (&optional arg)
318 "Move backward one field, staying in the current array row.
319If optional ARG is given, move backward ARG array columns.
320If necessary, keep the cursor in the window by scrolling right or left."
321 (interactive "p")
322 (array-forward-column (- arg)))
323
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
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000328 (limit-index a-row array-max-row)
329 (limit-index a-column array-max-column))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000330 (array-normalize-cursor))
331
332\f
333
334;;; Internal copying functions.
335
336(defun array-field-string ()
337 "Return the field string at the current cursor location."
41674a5a Dave Love2000-01-06 23:12:02 +0000338 ;; Requires that array-buffer-column be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000339 (buffer-substring
340 (save-excursion (array-beginning-of-field t) (point))
341 (save-excursion (array-end-of-field t) (point))))
342
343(defun array-copy-once-vertically (sign)
344 "Copy the current field into one array row in direction SIGN (1 or -1).
345Leave point at the beginning of the field and return the new array row.
346If requested to move beyond the array bounds, signal an error."
41674a5a Dave Love2000-01-06 23:12:02 +0000347 ;; Requires that array-buffer-line, array-buffer-column, and array-copy-string be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000348 (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)))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000351 (insert array-copy-string))
352 (move-to-column array-buffer-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000353 a-row))
354
355(defun array-copy-once-horizontally (sign)
356 "Copy the current field into one array column in direction SIGN (1 or -1).
357Leave point at the beginning of the field and return the new array column.
358If requested to move beyond the array bounds, signal an error."
41674a5a Dave Love2000-01-06 23:12:02 +0000359 ;; Requires that array-buffer-line, array-buffer-column, and array-copy-string be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000360 (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)))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000364 (insert array-copy-string))
365 (move-to-column array-buffer-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000366 a-column))
367
368(defun array-copy-to-row (a-row)
369 "Copy the current field vertically into every cell up to and including A-ROW.
370Leave point at the beginning of the field."
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000371 ;; Requires that array-buffer-line, array-buffer-column, array-row, and
372 ;; array-copy-string be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000373 (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)))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000381 (insert array-copy-string))
382 (move-to-column array-buffer-column)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000383 (setq count (1- count)))))
71296446 Juanma Barranquero2003-02-04 11:26:42 +0000384
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000385(defun array-copy-to-column (a-column)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000386 "Copy current field horizontally into every cell up to and including A-COLUMN.
387Leave 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.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000390 (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)))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000398 (insert array-copy-string))
399 (move-to-column array-buffer-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000400 (setq count (1- count)))))
401
402(defun array-copy-to-cell (a-row a-column)
403 "Copy the current field into the cell at A-ROW, A-COLUMN.
404Leave point at the beginning of the field."
41674a5a Dave Love2000-01-06 23:12:02 +0000405 ;; Requires that array-copy-string be current.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000406 (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)))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000409 (insert array-copy-string)
410 (move-to-column array-buffer-column))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000411
412\f
413
414;;; Commands for copying.
415
416(defun array-copy-down (&optional arg)
417 "Copy the current field one array row down.
418If optional ARG is given, copy down through ARG array rows."
419 (interactive "p")
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000420 (let* ((array-buffer-line (current-line))
421 (array-buffer-column (current-column))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000422 (array-row (or (array-current-row)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000423 (error "Cursor is not in a valid array cell")))
424 (array-copy-string (array-field-string)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000425 (if (= (abs arg) 1)
426 (array-copy-once-vertically arg)
427 (array-copy-to-row
41674a5a Dave Love2000-01-06 23:12:02 +0000428 (limit-index (+ array-row arg) array-max-row))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000429 (array-normalize-cursor))
430
431(defun array-copy-up (&optional arg)
432 "Copy the current field one array row up.
433If optional ARG is given, copy up through ARG array rows."
434 (interactive "p")
435 (array-copy-down (- arg)))
436
437(defun array-copy-forward (&optional arg)
438 "Copy the current field one array column to the right.
439If optional ARG is given, copy through ARG array columns to the right."
440 (interactive "p")
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000441 (let* ((array-buffer-line (current-line))
442 (array-buffer-column (current-column))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000443 (array-column (or (array-current-column)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000444 (error "Cursor is not in a valid array cell")))
445 (array-copy-string (array-field-string)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000446 (if (= (abs arg) 1)
447 (array-copy-once-horizontally arg)
448 (array-copy-to-column
41674a5a Dave Love2000-01-06 23:12:02 +0000449 (limit-index (+ array-column arg) array-max-column))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000450 (array-normalize-cursor))
451
452(defun array-copy-backward (&optional arg)
453 "Copy the current field one array column to the left.
454If optional ARG is given, copy through ARG array columns to the left."
455 (interactive "p")
456 (array-copy-forward (- arg)))
457
458(defun array-copy-column-forward (&optional arg)
459 "Copy the entire current column in to the column to the right.
460If 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)
41674a5a Dave Love2000-01-06 23:12:02 +0000465 (error "Cursor is not in a valid array cell"))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000466 (message "Working...")
467 (let ((this-row 0))
41674a5a Dave Love2000-01-06 23:12:02 +0000468 (while (< this-row array-max-row)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000469 (setq this-row (1+ this-row))
470 (array-move-to-cell this-row array-column)
471 (array-update-buffer-position)
41674a5a Dave Love2000-01-06 23:12:02 +0000472 (let ((array-copy-string (array-field-string)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000473 (if (= (abs arg) 1)
474 (array-copy-once-horizontally arg)
475 (array-copy-to-column
41674a5a Dave Love2000-01-06 23:12:02 +0000476 (limit-index (+ array-column arg) array-max-column))))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000477 (message "Working...done")
478 (array-move-to-row array-row)
479 (array-normalize-cursor))
480
481(defun array-copy-column-backward (&optional arg)
482 "Copy the entire current column one column to the left.
483If optional ARG is given, copy through ARG columns to the left."
484 (interactive "p")
485 (array-copy-column-forward (- arg)))
486
487(defun array-copy-row-down (&optional arg)
488 "Copy the entire current row one row down.
489If 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)
41674a5a Dave Love2000-01-06 23:12:02 +0000494 (error "Cursor is not in a valid array cell"))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000495 (cond
496 ((and (= array-row 1) (= arg -1))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000497 (error "Beginning of array"))
498 ((and (= array-row array-max-row) (= arg 1))
499 (error "End of array"))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000500 (t
41674a5a Dave Love2000-01-06 23:12:02 +0000501 (let* ((array-copy-string
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000502 (buffer-substring
503 (save-excursion (array-move-to-cell array-row 1)
504 (point))
41674a5a Dave Love2000-01-06 23:12:02 +0000505 (save-excursion (array-move-to-cell array-row array-max-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000506 (forward-line 1)
507 (point))))
508 (this-row array-row)
41674a5a Dave Love2000-01-06 23:12:02 +0000509 (goal-row (limit-index (+ this-row arg) array-max-row))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000510 (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
41674a5a Dave Love2000-01-06 23:12:02 +0000519 (array-move-to-cell this-row array-max-column)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000520 (forward-line 1)
521 (point)))
41674a5a Dave Love2000-01-06 23:12:02 +0000522 (insert array-copy-string))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000523 (setq count (1- count)))
524 (array-move-to-cell goal-row (or array-column 1)))))
525 (array-normalize-cursor))
526
527(defun array-copy-row-up (&optional arg)
528 "Copy the entire current array row into the row above.
529If optional ARG is given, copy through ARG rows up."
530 (interactive "p")
531 (array-copy-row-down (- arg)))
532
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)
41674a5a Dave Love2000-01-06 23:12:02 +0000539 (error "Cursor is not in a valid array cell")))
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000540 (p-column (or (array-current-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000541 (error "Cursor is not in a valid array cell")))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000542 (m-row
543 (save-excursion
544 (exchange-point-and-mark)
545 (array-update-buffer-position)
546 (or (array-current-row)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000547 (error "Mark is not in a valid array cell"))))
548 (m-column
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000549 (save-excursion
550 (exchange-point-and-mark)
551 (array-update-buffer-position)
552 (or (array-current-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000553 (error "Mark is not in a valid array cell")))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000554 (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.
41674a5a Dave Love2000-01-06 23:12:02 +0000560 (let ((array-copy-string
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000561 (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)
41674a5a Dave Love2000-01-06 23:12:02 +0000571 (let ((array-copy-string
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000572 (buffer-substring
573 (point)
574 (save-excursion
575 (array-move-to-cell top-row right-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000576 (setq array-buffer-column (current-column))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000577 (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)
41674a5a Dave Love2000-01-06 23:12:02 +0000588 (setq array-buffer-column (current-column))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000589 (array-end-of-field t)
590 (point)))
41674a5a Dave Love2000-01-06 23:12:02 +0000591 (insert array-copy-string)))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000592 (message "Working...done")
593 (array-goto-cell p-row p-column)))
594
595\f
596
597;;; Reconfiguration of the array.
598
599(defun array-make-template ()
600 "Create the template of an array."
601 (interactive)
41674a5a Dave Love2000-01-06 23:12:02 +0000602 ;; If there is a conflict between array-field-width and init-string, resolve it.
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000603 (let ((check t)
7a9ac688
SM
Stefan Monnier2006-01-18 20:36:19 +0000604 (len)
605 init-field)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000606 (while check
7a9ac688
SM
Stefan Monnier2006-01-18 20:36:19 +0000607 (setq init-field (read-string "Initial field value: "))
608 (setq len (length init-field))
41674a5a Dave Love2000-01-06 23:12:02 +0000609 (if (/= len array-field-width)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000610 (if (y-or-n-p (format "Change field width to %d? " len))
41674a5a Dave Love2000-01-06 23:12:02 +0000611 (progn (setq array-field-width len)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000612 (setq check nil)))
7a9ac688
SM
Stefan Monnier2006-01-18 20:36:19 +0000613 (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"))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000632 (array-goto-cell 1 1))
633
634(defun array-reconfigure-rows (new-columns-per-line new-rows-numbered)
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000635 "Reconfigure the state of `array-rows-numbered' and `array-columns-per-line'.
636NEW-COLUMNS-PER-LINE is the desired value of `array-columns-per-line' and
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000637NEW-ROWS-NUMBERED (a character, either ?y or ?n) is the desired value
26cf3f33 Juanma Barranquero2005-07-18 09:35:43 +0000638of `array-rows-numbered'."
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000639 (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)
41674a5a Dave Love2000-01-06 23:12:02 +0000644 (<= new-columns-per-line array-max-column))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000645 (setq check nil)
646 (setq new-columns-per-line
027a4b6b Juanma Barranquero2005-05-16 11:34:49 +0000647 (string-to-number
3981e5b5 Juanma Barranquero2005-05-18 10:17:18 +0000648 (read-string
41674a5a Dave Love2000-01-06 23:12:02 +0000649 (format "Columns per line (1 - %d): " array-max-column)))))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000650 ;; 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))
657 (t
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)))
18bf4ad8 Richard M. Stallman1999-08-29 19:21:07 +0000662 (temp-buffer (generate-new-buffer " *Array*"))
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000663 (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)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000670 (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.
682 ())
683 ((and old-rows-numbered (not new-rows-numbered))
684 ;; Delete the row number.
685 (kill-line 1))
686 (t
687 ;; Add the row number.
cc94298a Pavel Janík2001-11-26 16:20:18 +0000688 (insert (format "%d:\n" this-row))))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000689 ;; 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))))
694 (t
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)
26cf3f33 Juanma Barranquero2005-07-18 09:35:43 +0000717 (insert-buffer-substring temp-buffer)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000718 ;; Update local variables.
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000719 (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
9cd77daa
PE
Paul Eggert1993-08-10 04:14:17 +0000723 (+ (floor (1- temp-max-column) new-columns-per-line)
724 (if new-rows-numbered 2 1)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000725 (array-goto-cell (or array-row 1) (or array-column 1)))
726 (kill-buffer temp-buffer))
727 (message "Working...done"))
728
729(defun array-expand-rows ()
730 "Expand the rows so each fits on one line and remove row numbers."
731 (interactive)
41674a5a Dave Love2000-01-06 23:12:02 +0000732 (array-reconfigure-rows array-max-column ?n))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000733
734\f
735
736;;; Utilities.
737
738(defun limit-index (index limit)
739 (cond ((< index 1) 1)
740 ((> index limit) limit)
741 (t index)))
742
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000743(defun xor (pred1 pred2)
41674a5a Dave Love2000-01-06 23:12:02 +0000744 "Return the logical exclusive or of predicates PRED1 and PRED2."
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000745 (and (or pred1 pred2)
746 (not (and pred1 pred2))))
747
748(defun current-line ()
41674a5a Dave Love2000-01-06 23:12:02 +0000749 "Return the current buffer line at point. The first line is 0."
9b026d9f Glenn Morris2010-11-08 21:33:07 -0800750 (count-lines (point-min) (line-beginning-position)))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000751
752(defun move-to-column-untabify (column)
753 "Move to COLUMN on the current line, untabifying if necessary.
754Return COLUMN."
755 (or (and (= column (move-to-column column))
756 column)
757 ;; There is a tab in the way.
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000758 (if array-respect-tabs
759 (error "There is a TAB character in the way")
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000760 (progn
761 (untabify-backward)
762 (move-to-column column)))))
763
764(defun untabify-backward ()
26cf3f33 Juanma Barranquero2005-07-18 09:35:43 +0000765 "Untabify the preceding TAB."
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000766 (save-excursion
767 (let ((start (point)))
768 (backward-char 1)
769 (untabify (point) start))))
770
771\f
772
773;;; Array mode.
774
a0310a6c
DN
Dan Nicolaescu2010-10-10 16:12:30 -0700775(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)
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000797 "Keymap used in array mode.")
798
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000799
800(put 'array-mode 'mode-class 'special)
801
499bfd5f Dave Love1998-04-17 20:40:30 +0000802;;;###autoload
1b3b87df Stefan Monnier2013-09-10 23:31:56 -0400803(define-derived-mode array-mode fundamental-mode "Array"
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000804 "Major mode for editing arrays.
805
806 Array mode is a specialized mode for editing arrays. An array is
807considered to be a two-dimensional set of strings. The strings are
808NOT recognized as integers or real numbers.
809
41674a5a Dave Love2000-01-06 23:12:02 +0000810 The array MUST reside at the top of the buffer.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000811
812 TABs are not respected, and may be converted into spaces at any time.
26cf3f33 Juanma Barranquero2005-07-18 09:35:43 +0000813Setting the variable `array-respect-tabs' to non-nil will prevent TAB conversion,
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000814but will cause many functions to give errors if they encounter one.
815
816 Upon entering array mode, you will be prompted for the values of
817several variables. Others will be calculated based on the values you
246e8695 Pavel Janík2002-01-13 09:02:35 +0000818supply. These variables are all local to the buffer. Other buffer
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000819in array mode may have different values assigned to the variables.
820The variables are:
821
822Variables you assign:
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000823 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
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000828 row numbers in the buffer.
829
830Variables which are calculated:
41674a5a
DL
Dave Love2000-01-06 23:12:02 +0000831 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.
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000833
834 The following commands are available (an asterisk indicates it may
835take a numeric prefix argument):
836
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.
841
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.
846
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.
851
852 \\[array-fill-rectangle] Copy the field at mark into every cell with row and column
853 between that of point and mark.
854
855 \\[array-what-position] Display the current array row and column.
856 \\[array-goto-cell] Go to a particular array cell.
857
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)
862
863 \\[array-display-local-variables] Display the current values of local variables.
864
865Entering array mode calls the function `array-mode-hook'."
41674a5a Dave Love2000-01-06 23:12:02 +0000866 (make-local-variable 'array-buffer-line)
41674a5a Dave Love2000-01-06 23:12:02 +0000867 (make-local-variable 'array-buffer-column)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000868 (make-local-variable 'array-row)
b1d6ae0b Jim Blandy1991-01-30 22:51:05 +0000869 (make-local-variable 'array-column)
41674a5a Dave Love2000-01-06 23:12:02 +0000870 (make-local-variable 'array-copy-string)
7a9ac688
SM
Stefan Monnier2006-01-18 20:36:19 +0000871 (set (make-local-variable 'array-respect-tabs) nil)
872 (set (make-local-variable 'array-max-row)
873 (read-number "Number of array rows: "))
874 (set (make-local-variable 'array-max-column)
875 (read-number "Number of array columns: "))
876 (set (make-local-variable 'array-columns-per-line)
877 (read-number "Array columns per line: "))
878 (set (make-local-variable 'array-field-width)
879 (read-number "Field width: "))
880 (set (make-local-variable 'array-rows-numbered)
881 (y-or-n-p "Rows numbered? "))
882 (set (make-local-variable 'array-line-length)
883 (* array-field-width array-columns-per-line))
884 (set (make-local-variable 'array-lines-per-row)
885 (+ (floor (1- array-max-column) array-columns-per-line)
886 (if array-rows-numbered 2 1)))
887 (message "")
a838308c Karl Heuer1995-04-25 22:33:13 +0000888 (force-mode-line-update)
7a9ac688 Stefan Monnier2006-01-18 20:36:19 +0000889 (set (make-local-variable 'truncate-lines) t)
1b3b87df Stefan Monnier2013-09-10 23:31:56 -0400890 (setq overwrite-mode 'overwrite-mode-textual))
b1d6ae0b
JB
Jim Blandy1991-01-30 22:51:05 +0000891
892\f
893
bcc7f26d
KH
Karl Heuer1999-08-10 17:06:18 +0000894(provide 'array)
895
c0274f38 Eric S. Raymond1992-05-30 23:54:21 +0000896;;; array.el ends here