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