annotate lisp/array.el @ 389:6e0510766e66

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