Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; array.el --- array editing commands for GNU Emacs |
c0274f38 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1987, 2000-2013 Free Software Foundation, Inc. |
9750e079 | 4 | |
d53fa592 | 5 | ;; Author: David M. Brown |
e5167999 | 6 | ;; Maintainer: FSF |
e9571d2a | 7 | ;; Keywords: extensions |
b1d6ae0b | 8 | |
b1d6ae0b JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b1d6ae0b | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b1d6ae0b JB |
15 | |
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 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b1d6ae0b | 23 | |
e5167999 ER |
24 | ;;; Commentary: |
25 | ||
e41b2db1 ER |
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. | |
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 |
38 | |
39 | \f | |
e5167999 | 40 | ;;; Code: |
b1d6ae0b | 41 | |
7a9ac688 SM |
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?") | |
41674a5a | 55 | |
b1d6ae0b | 56 | ;;; Internal information functions. |
71296446 | 57 | |
b1d6ae0b | 58 | (defun array-cursor-in-array-range () |
41674a5a | 59 | "Return t if the cursor is in a valid array cell. |
b1d6ae0b | 60 | Its ok to be on a row number line." |
41674a5a DL |
61 | (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 |
63 | (not (or |
64 | ;; The cursor is too far to the right. | |
41674a5a | 65 | (>= array-buffer-column array-line-length) |
b1d6ae0b | 66 | ;; The cursor is below the last row. |
41674a5a | 67 | (>= array-buffer-line (* array-lines-per-row array-max-row)) |
b1d6ae0b JB |
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. | |
41674a5a | 71 | (and (zerop (% (1+ array-buffer-line) array-lines-per-row)) |
b1d6ae0b | 72 | (not (zerop columns-last-line)) |
41674a5a | 73 | (>= array-buffer-column (* columns-last-line array-field-width))))))) |
b1d6ae0b JB |
74 | |
75 | (defun array-current-row () | |
76 | "Return the array row of the field in which the cursor is located." | |
41674a5a | 77 | ;; Requires array-buffer-line and array-buffer-column to be current. |
b1d6ae0b | 78 | (and (array-cursor-in-array-range) |
41674a5a | 79 | (1+ (floor array-buffer-line array-lines-per-row)))) |
b1d6ae0b JB |
80 | |
81 | (defun array-current-column () | |
82 | "Return the array column of the field in which the cursor is located." | |
41674a5a | 83 | ;; Requires array-buffer-line and array-buffer-column to be current. |
b1d6ae0b JB |
84 | (and (array-cursor-in-array-range) |
85 | ;; It's not okay to be on a row number line. | |
41674a5a DL |
86 | (not (and array-rows-numbered |
87 | (zerop (% array-buffer-line array-lines-per-row)))) | |
b1d6ae0b JB |
88 | (+ |
89 | ;; Array columns due to line differences. | |
41674a5a DL |
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))) | |
b1d6ae0b | 94 | ;; Array columns on the current line. |
41674a5a | 95 | (1+ (floor array-buffer-column array-field-width))))) |
b1d6ae0b JB |
96 | |
97 | (defun array-update-array-position (&optional a-row a-column) | |
41674a5a DL |
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. | |
b1d6ae0b JB |
101 | (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 | 105 | "Set `array-buffer-line' and `array-buffer-column' to their current values." |
41674a5a DL |
106 | (setq array-buffer-line (current-line) |
107 | array-buffer-column (current-column))) | |
b1d6ae0b JB |
108 | |
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 |
116 | (let ((array-buffer-line (current-line)) |
117 | (array-buffer-column (current-column))) | |
118 | (message "Array row: %s Array column: %s" | |
4bf785ba KH |
119 | (prin1-to-string (array-current-row)) |
120 | (prin1-to-string (array-current-column))))) | |
b1d6ae0b JB |
121 | |
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 | 131 | (prin1-to-string array-max-row))) |
b1d6ae0b | 132 | (princ (format " max-column: %s\n" |
41674a5a | 133 | (prin1-to-string array-max-column))) |
b1d6ae0b | 134 | (princ (format " columns-per-line: %s\n" |
41674a5a | 135 | (prin1-to-string array-columns-per-line))) |
b1d6ae0b | 136 | (princ (format " field-width: %s\n" |
41674a5a | 137 | (prin1-to-string array-field-width))) |
b1d6ae0b | 138 | (princ (format " rows-numbered: %s\n" |
41674a5a | 139 | (prin1-to-string array-rows-numbered))) |
b1d6ae0b | 140 | (princ (format " lines-per-row: %s\n" |
41674a5a | 141 | (prin1-to-string array-lines-per-row))) |
b1d6ae0b | 142 | (princ (format " line-length: %s\n" |
41674a5a | 143 | (prin1-to-string array-line-length)))))) |
b1d6ae0b JB |
144 | |
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. | |
151 | Optional argument GO-THERE, if non-nil, means go there too." | |
41674a5a DL |
152 | ;; Requires that array-buffer-column be current. |
153 | (let ((goal-column (- array-buffer-column (% array-buffer-column array-field-width)))) | |
b1d6ae0b JB |
154 | (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. | |
160 | If optional argument GO-THERE is non-nil, go there too." | |
41674a5a DL |
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))) | |
b1d6ae0b JB |
164 | (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 | 169 | "Move to array row A-ROW and array column A-COLUMN. |
b1d6ae0b | 170 | Leave point at the beginning of the field and return the new buffer column." |
41674a5a DL |
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)))) | |
b1d6ae0b JB |
175 | (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. | |
181 | Leave point at the beginning of the field and return the new array row." | |
41674a5a DL |
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)) | |
b1d6ae0b JB |
187 | (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. | |
192 | Leave point at the beginning of the field and return the new array column." | |
41674a5a DL |
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)) | |
b1d6ae0b JB |
199 | (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). | |
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." | |
41674a5a | 206 | ;; Requires that array-buffer-line and array-buffer-column be current. |
b1d6ae0b JB |
207 | (let ((goal-column (array-beginning-of-field)) |
208 | (array-row (or (array-current-row) | |
41674a5a DL |
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")) | |
b1d6ae0b | 212 | ((and (= array-row 1) (= sign -1)) |
41674a5a | 213 | (error "Beginning of array")) |
b1d6ae0b JB |
214 | (t |
215 | (progn | |
41674a5a | 216 | (forward-line (* sign array-lines-per-row)) |
b1d6ae0b JB |
217 | (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). | |
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." | |
41674a5a | 224 | ;; Requires that array-buffer-line and array-buffer-column be current. |
b1d6ae0b | 225 | (let ((array-column (or (array-current-column) |
41674a5a DL |
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")) | |
b1d6ae0b | 229 | ((and (= array-column 1) (= sign -1)) |
41674a5a | 230 | (error "Beginning of array")) |
b1d6ae0b | 231 | (t |
41674a5a | 232 | (cond |
b1d6ae0b | 233 | ;; Going backward from first column on the line. |
41674a5a | 234 | ((and (= sign -1) (= 1 (% array-column array-columns-per-line))) |
b1d6ae0b JB |
235 | (forward-line -1) |
236 | (move-to-column-untabify | |
41674a5a | 237 | (* array-field-width (1- array-columns-per-line)))) |
b1d6ae0b | 238 | ;; Going forward from last column on the line. |
41674a5a | 239 | ((and (= sign 1) (zerop (% array-column array-columns-per-line))) |
b1d6ae0b JB |
240 | (forward-line 1)) |
241 | ;; Somewhere in the middle of the line. | |
242 | (t | |
243 | (move-to-column-untabify (+ (array-beginning-of-field) | |
41674a5a | 244 | (* array-field-width sign))))) |
b1d6ae0b JB |
245 | (+ array-column sign))))) |
246 | ||
247 | (defun array-normalize-cursor () | |
41674a5a DL |
248 | "Move the cursor to the first non-whitespace character in the field. |
249 | If necessary, scroll horizontally to keep the cursor in view." | |
b1d6ae0b | 250 | ;; Assumes point is at the beginning of the field. |
41674a5a | 251 | (let ((array-buffer-column (current-column))) |
b1d6ae0b JB |
252 | (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 | 259 | ;; array-buffer-column will always be current. |
b1d6ae0b JB |
260 | (let ((w-hscroll (window-hscroll)) |
261 | (w-width (window-width))) | |
262 | (cond | |
41674a5a DL |
263 | ((and (>= array-buffer-column w-hscroll) |
264 | (<= array-buffer-column (+ w-hscroll w-width))) | |
b1d6ae0b JB |
265 | ;; It's already visible. Do nothing. |
266 | nil) | |
41674a5a | 267 | ((> array-buffer-column (+ w-hscroll w-width)) |
b1d6ae0b | 268 | ;; It's to the right. Scroll left. |
41674a5a | 269 | (scroll-left (- (- array-buffer-column w-hscroll) |
b1d6ae0b JB |
270 | (/ w-width 2)))) |
271 | (t | |
272 | ;; It's to the left. Scroll right. | |
41674a5a | 273 | (scroll-right (+ (- w-hscroll array-buffer-column) |
b1d6ae0b JB |
274 | (/ 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. | |
282 | If optional ARG is given, move down ARG array rows." | |
283 | (interactive "p") | |
41674a5a DL |
284 | (let ((array-buffer-line (current-line)) |
285 | (array-buffer-column (current-column))) | |
b1d6ae0b JB |
286 | (if (= (abs arg) 1) |
287 | (array-move-one-row arg) | |
288 | (array-move-to-row | |
289 | (limit-index (+ (or (array-current-row) | |
41674a5a | 290 | (error "Cursor is not in an array cell")) |
b1d6ae0b | 291 | arg) |
41674a5a | 292 | array-max-row)))) |
b1d6ae0b JB |
293 | (array-normalize-cursor)) |
294 | ||
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))) | |
300 | ||
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") | |
41674a5a DL |
306 | (let ((array-buffer-line (current-line)) |
307 | (array-buffer-column (current-column))) | |
b1d6ae0b JB |
308 | (if (= (abs arg) 1) |
309 | (array-move-one-column arg) | |
310 | (array-move-to-column | |
311 | (limit-index (+ (or (array-current-column) | |
41674a5a | 312 | (error "Cursor is not in an array cell")) |
b1d6ae0b | 313 | arg) |
41674a5a | 314 | array-max-column)))) |
b1d6ae0b JB |
315 | (array-normalize-cursor)) |
316 | ||
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))) | |
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 |
328 | (limit-index a-row array-max-row) |
329 | (limit-index a-column array-max-column)) | |
b1d6ae0b JB |
330 | (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 | 338 | ;; Requires that array-buffer-column be current. |
b1d6ae0b JB |
339 | (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). | |
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." | |
41674a5a | 347 | ;; Requires that array-buffer-line, array-buffer-column, and array-copy-string be current. |
b1d6ae0b JB |
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))) | |
41674a5a DL |
351 | (insert array-copy-string)) |
352 | (move-to-column array-buffer-column) | |
b1d6ae0b JB |
353 | 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). | |
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." | |
41674a5a | 359 | ;; Requires that array-buffer-line, array-buffer-column, and array-copy-string be current. |
b1d6ae0b JB |
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))) | |
41674a5a DL |
364 | (insert array-copy-string)) |
365 | (move-to-column array-buffer-column) | |
b1d6ae0b JB |
366 | 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. | |
370 | Leave point at the beginning of the field." | |
41674a5a DL |
371 | ;; Requires that array-buffer-line, array-buffer-column, array-row, and |
372 | ;; array-copy-string be current. | |
b1d6ae0b JB |
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))) | |
41674a5a DL |
381 | (insert array-copy-string)) |
382 | (move-to-column array-buffer-column) | |
b1d6ae0b | 383 | (setq count (1- count))))) |
71296446 | 384 | |
b1d6ae0b | 385 | (defun array-copy-to-column (a-column) |
41674a5a DL |
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. | |
b1d6ae0b JB |
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))) | |
41674a5a DL |
398 | (insert array-copy-string)) |
399 | (move-to-column array-buffer-column) | |
b1d6ae0b JB |
400 | (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. | |
404 | Leave point at the beginning of the field." | |
41674a5a | 405 | ;; Requires that array-copy-string be current. |
b1d6ae0b JB |
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))) | |
41674a5a DL |
409 | (insert array-copy-string) |
410 | (move-to-column array-buffer-column)) | |
b1d6ae0b JB |
411 | |
412 | \f | |
413 | ||
414 | ;;; Commands for copying. | |
415 | ||
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") | |
41674a5a DL |
420 | (let* ((array-buffer-line (current-line)) |
421 | (array-buffer-column (current-column)) | |
b1d6ae0b | 422 | (array-row (or (array-current-row) |
41674a5a DL |
423 | (error "Cursor is not in a valid array cell"))) |
424 | (array-copy-string (array-field-string))) | |
b1d6ae0b JB |
425 | (if (= (abs arg) 1) |
426 | (array-copy-once-vertically arg) | |
427 | (array-copy-to-row | |
41674a5a | 428 | (limit-index (+ array-row arg) array-max-row)))) |
b1d6ae0b JB |
429 | (array-normalize-cursor)) |
430 | ||
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))) | |
436 | ||
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") | |
41674a5a DL |
441 | (let* ((array-buffer-line (current-line)) |
442 | (array-buffer-column (current-column)) | |
b1d6ae0b | 443 | (array-column (or (array-current-column) |
41674a5a DL |
444 | (error "Cursor is not in a valid array cell"))) |
445 | (array-copy-string (array-field-string))) | |
b1d6ae0b JB |
446 | (if (= (abs arg) 1) |
447 | (array-copy-once-horizontally arg) | |
448 | (array-copy-to-column | |
41674a5a | 449 | (limit-index (+ array-column arg) array-max-column)))) |
b1d6ae0b JB |
450 | (array-normalize-cursor)) |
451 | ||
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))) | |
457 | ||
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) | |
41674a5a | 465 | (error "Cursor is not in a valid array cell")) |
b1d6ae0b JB |
466 | (message "Working...") |
467 | (let ((this-row 0)) | |
41674a5a | 468 | (while (< this-row array-max-row) |
b1d6ae0b JB |
469 | (setq this-row (1+ this-row)) |
470 | (array-move-to-cell this-row array-column) | |
471 | (array-update-buffer-position) | |
41674a5a | 472 | (let ((array-copy-string (array-field-string))) |
b1d6ae0b JB |
473 | (if (= (abs arg) 1) |
474 | (array-copy-once-horizontally arg) | |
475 | (array-copy-to-column | |
41674a5a | 476 | (limit-index (+ array-column arg) array-max-column)))))) |
b1d6ae0b JB |
477 | (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. | |
483 | If 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. | |
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) | |
41674a5a | 494 | (error "Cursor is not in a valid array cell")) |
b1d6ae0b JB |
495 | (cond |
496 | ((and (= array-row 1) (= arg -1)) | |
41674a5a DL |
497 | (error "Beginning of array")) |
498 | ((and (= array-row array-max-row) (= arg 1)) | |
499 | (error "End of array")) | |
b1d6ae0b | 500 | (t |
41674a5a | 501 | (let* ((array-copy-string |
b1d6ae0b JB |
502 | (buffer-substring |
503 | (save-excursion (array-move-to-cell array-row 1) | |
504 | (point)) | |
41674a5a | 505 | (save-excursion (array-move-to-cell array-row array-max-column) |
b1d6ae0b JB |
506 | (forward-line 1) |
507 | (point)))) | |
508 | (this-row array-row) | |
41674a5a | 509 | (goal-row (limit-index (+ this-row arg) array-max-row)) |
b1d6ae0b JB |
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 | |
41674a5a | 519 | (array-move-to-cell this-row array-max-column) |
b1d6ae0b JB |
520 | (forward-line 1) |
521 | (point))) | |
41674a5a | 522 | (insert array-copy-string)) |
b1d6ae0b JB |
523 | (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. | |
529 | If 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 | 539 | (error "Cursor is not in a valid array cell"))) |
b1d6ae0b | 540 | (p-column (or (array-current-column) |
41674a5a | 541 | (error "Cursor is not in a valid array cell"))) |
b1d6ae0b JB |
542 | (m-row |
543 | (save-excursion | |
544 | (exchange-point-and-mark) | |
545 | (array-update-buffer-position) | |
546 | (or (array-current-row) | |
41674a5a DL |
547 | (error "Mark is not in a valid array cell")))) |
548 | (m-column | |
b1d6ae0b JB |
549 | (save-excursion |
550 | (exchange-point-and-mark) | |
551 | (array-update-buffer-position) | |
552 | (or (array-current-column) | |
41674a5a | 553 | (error "Mark is not in a valid array cell"))))) |
b1d6ae0b JB |
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. | |
41674a5a | 560 | (let ((array-copy-string |
b1d6ae0b JB |
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) | |
41674a5a | 571 | (let ((array-copy-string |
b1d6ae0b JB |
572 | (buffer-substring |
573 | (point) | |
574 | (save-excursion | |
575 | (array-move-to-cell top-row right-column) | |
41674a5a | 576 | (setq array-buffer-column (current-column)) |
b1d6ae0b JB |
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) | |
41674a5a | 588 | (setq array-buffer-column (current-column)) |
b1d6ae0b JB |
589 | (array-end-of-field t) |
590 | (point))) | |
41674a5a | 591 | (insert array-copy-string))))) |
b1d6ae0b JB |
592 | (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 | 602 | ;; If there is a conflict between array-field-width and init-string, resolve it. |
b1d6ae0b | 603 | (let ((check t) |
7a9ac688 SM |
604 | (len) |
605 | init-field) | |
b1d6ae0b | 606 | (while check |
7a9ac688 SM |
607 | (setq init-field (read-string "Initial field value: ")) |
608 | (setq len (length init-field)) | |
41674a5a | 609 | (if (/= len array-field-width) |
b1d6ae0b | 610 | (if (y-or-n-p (format "Change field width to %d? " len)) |
41674a5a | 611 | (progn (setq array-field-width len) |
b1d6ae0b | 612 | (setq check nil))) |
7a9ac688 SM |
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")) | |
b1d6ae0b JB |
632 | (array-goto-cell 1 1)) |
633 | ||
634 | (defun array-reconfigure-rows (new-columns-per-line new-rows-numbered) | |
41674a5a DL |
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 | |
b1d6ae0b | 637 | NEW-ROWS-NUMBERED (a character, either ?y or ?n) is the desired value |
26cf3f33 | 638 | of `array-rows-numbered'." |
b1d6ae0b JB |
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) | |
41674a5a | 644 | (<= new-columns-per-line array-max-column)) |
b1d6ae0b JB |
645 | (setq check nil) |
646 | (setq new-columns-per-line | |
027a4b6b | 647 | (string-to-number |
3981e5b5 | 648 | (read-string |
41674a5a | 649 | (format "Columns per line (1 - %d): " array-max-column))))))) |
b1d6ae0b JB |
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)) | |
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 | 662 | (temp-buffer (generate-new-buffer " *Array*")) |
41674a5a DL |
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) | |
b1d6ae0b JB |
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. | |
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 | 688 | (insert (format "%d:\n" this-row)))) |
b1d6ae0b JB |
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)))) | |
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 | 717 | (insert-buffer-substring temp-buffer) |
b1d6ae0b | 718 | ;; Update local variables. |
41674a5a DL |
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 | |
9cd77daa PE |
723 | (+ (floor (1- temp-max-column) new-columns-per-line) |
724 | (if new-rows-numbered 2 1))) | |
b1d6ae0b JB |
725 | (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 | 732 | (array-reconfigure-rows array-max-column ?n)) |
b1d6ae0b JB |
733 | |
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 | 743 | (defun xor (pred1 pred2) |
41674a5a | 744 | "Return the logical exclusive or of predicates PRED1 and PRED2." |
b1d6ae0b JB |
745 | (and (or pred1 pred2) |
746 | (not (and pred1 pred2)))) | |
747 | ||
748 | (defun current-line () | |
41674a5a | 749 | "Return the current buffer line at point. The first line is 0." |
9b026d9f | 750 | (count-lines (point-min) (line-beginning-position))) |
b1d6ae0b JB |
751 | |
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. | |
41674a5a DL |
758 | (if array-respect-tabs |
759 | (error "There is a TAB character in the way") | |
b1d6ae0b JB |
760 | (progn |
761 | (untabify-backward) | |
762 | (move-to-column column))))) | |
763 | ||
764 | (defun untabify-backward () | |
26cf3f33 | 765 | "Untabify the preceding TAB." |
b1d6ae0b JB |
766 | (save-excursion |
767 | (let ((start (point))) | |
768 | (backward-char 1) | |
769 | (untabify (point) start)))) | |
770 | ||
771 | \f | |
772 | ||
773 | ;;; Array mode. | |
774 | ||
a0310a6c DN |
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) | |
b1d6ae0b JB |
797 | "Keymap used in array mode.") |
798 | ||
b1d6ae0b JB |
799 | |
800 | (put 'array-mode 'mode-class 'special) | |
801 | ||
499bfd5f | 802 | ;;;###autoload |
1b3b87df | 803 | (define-derived-mode array-mode fundamental-mode "Array" |
b1d6ae0b JB |
804 | "Major mode for editing arrays. |
805 | ||
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. | |
809 | ||
41674a5a | 810 | The array MUST reside at the top of the buffer. |
b1d6ae0b JB |
811 | |
812 | TABs are not respected, and may be converted into spaces at any time. | |
26cf3f33 | 813 | Setting the variable `array-respect-tabs' to non-nil will prevent TAB conversion, |
b1d6ae0b JB |
814 | but 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 | |
817 | several variables. Others will be calculated based on the values you | |
246e8695 | 818 | supply. These variables are all local to the buffer. Other buffer |
b1d6ae0b JB |
819 | in array mode may have different values assigned to the variables. |
820 | The variables are: | |
821 | ||
822 | Variables you assign: | |
41674a5a DL |
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 | |
b1d6ae0b JB |
828 | row numbers in the buffer. |
829 | ||
830 | Variables which are calculated: | |
41674a5a DL |
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. | |
b1d6ae0b JB |
833 | |
834 | The following commands are available (an asterisk indicates it may | |
835 | take 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 | ||
865 | Entering array mode calls the function `array-mode-hook'." | |
41674a5a | 866 | (make-local-variable 'array-buffer-line) |
41674a5a | 867 | (make-local-variable 'array-buffer-column) |
b1d6ae0b | 868 | (make-local-variable 'array-row) |
b1d6ae0b | 869 | (make-local-variable 'array-column) |
41674a5a | 870 | (make-local-variable 'array-copy-string) |
7a9ac688 SM |
871 | (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 | 888 | (force-mode-line-update) |
7a9ac688 | 889 | (set (make-local-variable 'truncate-lines) t) |
1b3b87df | 890 | (setq overwrite-mode 'overwrite-mode-textual)) |
b1d6ae0b JB |
891 | |
892 | \f | |
893 | ||
bcc7f26d KH |
894 | (provide 'array) |
895 | ||
c0274f38 | 896 | ;;; array.el ends here |