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