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