Initial revision
[bpt/emacs.git] / lisp / textmodes / picture.el
CommitLineData
695d13c7
BP
1;; "Picture mode" -- editing using quarter-plane screen model.
2;; Copyright (C) 1985 Free Software Foundation, Inc.
3;; Principal author K. Shane Hartman
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 1, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22(provide 'picture)
23
24(defun move-to-column-force (column)
25 "Move to column COLUMN in current line.
26Differs from `move-to-column' in that it creates or modifies whitespace
27if necessary to attain exactly the specified column."
28 (move-to-column column)
29 (let ((col (current-column)))
30 (if (< col column)
31 (indent-to column)
32 (if (and (/= col column)
33 (= (preceding-char) ?\t))
34 (let (indent-tabs-mode)
35 (delete-char -1)
36 (indent-to col)
37 (move-to-column column))))))
38
39\f
40;; Picture Movement Commands
41
42(defun picture-end-of-line (&optional arg)
43 "Position point after last non-blank character on current line.
44With ARG not nil, move forward ARG - 1 lines first.
45If scan reaches end of buffer, stop there without error."
46 (interactive "P")
47 (if arg (forward-line (1- (prefix-numeric-value arg))))
48 (beginning-of-line)
49 (skip-chars-backward " \t" (prog1 (point) (end-of-line))))
50
51(defun picture-forward-column (arg)
52 "Move cursor right, making whitespace if necessary.
53With argument, move that many columns."
54 (interactive "p")
55 (move-to-column-force (+ (current-column) arg)))
56
57(defun picture-backward-column (arg)
58 "Move cursor left, making whitespace if necessary.
59With argument, move that many columns."
60 (interactive "p")
61 (move-to-column-force (- (current-column) arg)))
62
63(defun picture-move-down (arg)
64 "Move vertically down, making whitespace if necessary.
65With argument, move that many lines."
66 (interactive "p")
67 (let ((col (current-column)))
68 (picture-newline arg)
69 (move-to-column-force col)))
70
71(defconst picture-vertical-step 0
72 "Amount to move vertically after text character in Picture mode.")
73
74(defconst picture-horizontal-step 1
75 "Amount to move horizontally after text character in Picture mode.")
76
77(defun picture-move-up (arg)
78 "Move vertically up, making whitespace if necessary.
79With argument, move that many lines."
80 (interactive "p")
81 (picture-move-down (- arg)))
82
83(defun picture-movement-right ()
84 "Move right after self-inserting character in Picture mode."
85 (interactive)
86 (picture-set-motion 0 1))
87
88(defun picture-movement-left ()
89 "Move left after self-inserting character in Picture mode."
90 (interactive)
91 (picture-set-motion 0 -1))
92
93(defun picture-movement-up ()
94 "Move up after self-inserting character in Picture mode."
95 (interactive)
96 (picture-set-motion -1 0))
97
98(defun picture-movement-down ()
99 "Move down after self-inserting character in Picture mode."
100 (interactive)
101 (picture-set-motion 1 0))
102
103(defun picture-movement-nw ()
104 "Move up and left after self-inserting character in Picture mode."
105 (interactive)
106 (picture-set-motion -1 -1))
107
108(defun picture-movement-ne ()
109 "Move up and right after self-inserting character in Picture mode."
110 (interactive)
111 (picture-set-motion -1 1))
112
113(defun picture-movement-sw ()
114 "Move down and left after self-inserting character in Picture mode."
115 (interactive)
116 (picture-set-motion 1 -1))
117
118(defun picture-movement-se ()
119 "Move down and right after self-inserting character in Picture mode."
120 (interactive)
121 (picture-set-motion 1 1))
122
123(defun picture-set-motion (vert horiz)
124 "Set VERTICAL and HORIZONTAL increments for movement in Picture mode.
125The mode line is updated to reflect the current direction."
126 (setq picture-vertical-step vert
127 picture-horizontal-step horiz)
128 (setq mode-name
129 (format "Picture:%s"
130 (car (nthcdr (+ 1 (% horiz 2) (* 3 (1+ (% vert 2))))
131 '(nw up ne left none right sw down se)))))
132 ;; Kludge - force the mode line to be updated. Is there a better
133 ;; way to this?
134 (set-buffer-modified-p (buffer-modified-p))
135 (message ""))
136
137(defun picture-move ()
138 "Move in direction of `picture-vertical-step' and `picture-horizontal-step'."
139 (picture-move-down picture-vertical-step)
140 (picture-forward-column picture-horizontal-step))
141
142(defun picture-motion (arg)
143 "Move point in direction of current picture motion in Picture mode.
144With ARG do it that many times. Useful for delineating rectangles in
145conjunction with diagonal picture motion.
146Do \\[command-apropos] picture-movement to see commands which control motion."
147 (interactive "p")
148 (picture-move-down (* arg picture-vertical-step))
149 (picture-forward-column (* arg picture-horizontal-step)))
150
151(defun picture-motion-reverse (arg)
152 "Move point in direction opposite of current picture motion in Picture mode.
153With ARG do it that many times. Useful for delineating rectangles in
154conjunction with diagonal picture motion.
155Do \\[command-apropos] `picture-movement' to see commands which control motion."
156 (interactive "p")
157 (picture-motion (- arg)))
158
159\f
160;; Picture insertion and deletion.
161
162(defun picture-self-insert (arg)
163 "Insert this character in place of character previously at the cursor.
164The cursor then moves in the direction you previously specified
165with the commands `picture-movement-right', `picture-movement-up', etc.
166Do \\[command-apropos] `picture-movement' to see those commands."
167 (interactive "p")
168 (while (> arg 0)
169 (setq arg (1- arg))
170 (move-to-column-force (1+ (current-column)))
171 (delete-char -1)
172 (insert last-input-char)
173 (forward-char -1)
174 (picture-move)))
175
176(defun picture-clear-column (arg)
177 "Clear out ARG columns after point without moving."
178 (interactive "p")
179 (let* ((opoint (point))
180 (original-col (current-column))
181 (target-col (+ original-col arg)))
182 (move-to-column-force target-col)
183 (delete-region opoint (point))
184 (save-excursion
185 (indent-to (max target-col original-col)))))
186
187(defun picture-backward-clear-column (arg)
188 "Clear out ARG columns before point, moving back over them."
189 (interactive "p")
190 (picture-clear-column (- arg)))
191
192(defun picture-clear-line (arg)
193 "Clear out rest of line; if at end of line, advance to next line.
194Cleared-out line text goes into the kill ring, as do newlines that are
195advanced over. With argument, clear out (and save in kill ring) that
196many lines."
197 (interactive "P")
198 (if arg
199 (progn
200 (setq arg (prefix-numeric-value arg))
201 (kill-line arg)
202 (newline (if (> arg 0) arg (- arg))))
203 (if (looking-at "[ \t]*$")
204 (kill-ring-save (point) (progn (forward-line 1) (point)))
205 (kill-region (point) (progn (end-of-line) (point))))))
206
207(defun picture-newline (arg)
208 "Move to the beginning of the following line.
209With argument, moves that many lines (up, if negative argument);
210always moves to the beginning of a line."
211 (interactive "p")
212 (if (< arg 0)
213 (forward-line arg)
214 (while (> arg 0)
215 (end-of-line)
216 (if (eobp) (newline) (forward-char 1))
217 (setq arg (1- arg)))))
218
219(defun picture-open-line (arg)
220 "Insert an empty line after the current line.
221With positive argument insert that many lines."
222 (interactive "p")
223 (save-excursion
224 (end-of-line)
225 (open-line arg)))
226
227(defun picture-duplicate-line ()
228 "Insert a duplicate of the current line, below it."
229 (interactive)
230 (save-excursion
231 (let ((contents
232 (buffer-substring
233 (progn (beginning-of-line) (point))
234 (progn (picture-newline 1) (point)))))
235 (forward-line -1)
236 (insert contents))))
237
238\f
239;; Picture Tabs
240
241(defvar picture-tab-chars "!-~"
242 "*A character set which controls behavior of commands
243\\[picture-set-tab-stops] and \\[picture-tab-search]. It is NOT a
244regular expression, any regexp special characters will be quoted.
245It defines a set of \"interesting characters\" to look for when setting
246\(or searching for) tab stops, initially \"!-~\" (all printing characters).
247For example, suppose that you are editing a table which is formatted thus:
248| foo | bar + baz | 23 *
249| bubbles | and + etc | 97 *
250and that `picture-tab-chars' is \"|+*\". Then invoking
251\\[picture-set-tab-stops] on either of the previous lines would result
252in the following tab stops
253 : : : :
254Another example - \"A-Za-z0-9\" would produce the tab stops
255 : : : :
256
257Note that if you want the character `-' to be in the set, it must be
258included in a range or else appear in a context where it cannot be
259taken for indicating a range (e.g. \"-A-Z\" declares the set to be the
260letters `A' through `Z' and the character `-'). If you want the
261character `\\' in the set it must be preceded by itself: \"\\\\\".
262
263The command \\[picture-tab-search] is defined to move beneath (or to) a
264character belonging to this set independent of the tab stops list.")
265
266(defun picture-set-tab-stops (&optional arg)
267 "Set value of `tab-stop-list' according to context of this line.
268This controls the behavior of \\[picture-tab]. A tab stop is set at
269every column occupied by an \"interesting character\" that is preceded
270by whitespace. Interesting characters are defined by the variable
271`picture-tab-chars', see its documentation for an example of usage.
272With ARG, just (re)set `tab-stop-list' to its default value. The tab
273stops computed are displayed in the minibuffer with `:' at each stop."
274 (interactive "P")
275 (save-excursion
276 (let (tabs)
277 (if arg
278 (setq tabs (default-value 'tab-stop-list))
279 (let ((regexp (concat "[ \t]+[" (regexp-quote picture-tab-chars) "]")))
280 (beginning-of-line)
281 (let ((bol (point)))
282 (end-of-line)
283 (while (re-search-backward regexp bol t)
284 (skip-chars-forward " \t")
285 (setq tabs (cons (current-column) tabs)))
286 (if (null tabs)
287 (error "No characters in set %s on this line."
288 (regexp-quote picture-tab-chars))))))
289 (setq tab-stop-list tabs)
290 (let ((blurb (make-string (1+ (nth (1- (length tabs)) tabs)) ?\ )))
291 (while tabs
292 (aset blurb (car tabs) ?:)
293 (setq tabs (cdr tabs)))
294 (message blurb)))))
295
296(defun picture-tab-search (&optional arg)
297 "Move to column beneath next interesting char in previous line.
298With ARG move to column occupied by next interesting character in this
299line. The character must be preceded by whitespace.
300\"interesting characters\" are defined by variable `picture-tab-chars'.
301If no such character is found, move to beginning of line."
302 (interactive "P")
303 (let ((target (current-column)))
304 (save-excursion
305 (if (and (not arg)
306 (progn
307 (beginning-of-line)
308 (skip-chars-backward
309 (concat "^" (regexp-quote picture-tab-chars))
310 (point-min))
311 (not (bobp))))
312 (move-to-column target))
313 (if (re-search-forward
314 (concat "[ \t]+[" (regexp-quote picture-tab-chars) "]")
315 (save-excursion (end-of-line) (point))
316 'move)
317 (setq target (1- (current-column)))
318 (setq target nil)))
319 (if target
320 (move-to-column-force target)
321 (beginning-of-line))))
322
323(defun picture-tab (&optional arg)
324 "Tab transparently (just move point) to next tab stop.
325With prefix arg, overwrite the traversed text with spaces. The tab stop
326list can be changed by \\[picture-set-tab-stops] and \\[edit-tab-stops].
327See also documentation for variable `picture-tab-chars'."
328 (interactive "P")
329 (let* ((opoint (point)))
330 (move-to-tab-stop)
331 (if arg
332 (let (indent-tabs-mode
333 (column (current-column)))
334 (delete-region opoint (point))
335 (indent-to column)))))
336\f
337;; Picture Rectangles
338
339(defconst picture-killed-rectangle nil
340 "Rectangle killed or copied by \\[picture-clear-rectangle] in Picture mode.
341The contents can be retrieved by \\[picture-yank-rectangle]")
342
343(defun picture-clear-rectangle (start end &optional killp)
344 "Clear and save rectangle delineated by point and mark.
345The rectangle is saved for yanking by \\[picture-yank-rectangle] and replaced
346with whitespace. The previously saved rectangle, if any, is lost. With
347prefix argument, the rectangle is actually killed, shifting remaining text."
348 (interactive "r\nP")
349 (setq picture-killed-rectangle (picture-snarf-rectangle start end killp)))
350
351(defun picture-clear-rectangle-to-register (start end register &optional killp)
352 "Clear rectangle delineated by point and mark into REGISTER.
353The rectangle is saved in REGISTER and replaced with whitespace. With
354prefix argument, the rectangle is actually killed, shifting remaining text."
355 (interactive "r\ncRectangle to register: \nP")
356 (set-register register (picture-snarf-rectangle start end killp)))
357
358(defun picture-snarf-rectangle (start end &optional killp)
359 (let ((column (current-column))
360 (indent-tabs-mode nil))
361 (prog1 (save-excursion
362 (if killp
363 (delete-extract-rectangle start end)
364 (prog1 (extract-rectangle start end)
365 (clear-rectangle start end))))
366 (move-to-column-force column))))
367
368(defun picture-yank-rectangle (&optional insertp)
369 "Overlay rectangle saved by \\[picture-clear-rectangle]
370The rectangle is positioned with upper left corner at point, overwriting
371existing text. With prefix argument, the rectangle is inserted instead,
372shifting existing text. Leaves mark at one corner of rectangle and
373point at the other (diagonally opposed) corner."
374 (interactive "P")
375 (if (not (consp picture-killed-rectangle))
376 (error "No rectangle saved.")
377 (picture-insert-rectangle picture-killed-rectangle insertp)))
378
379(defun picture-yank-rectangle-from-register (register &optional insertp)
380 "Overlay rectangle saved in REGISTER.
381The rectangle is positioned with upper left corner at point, overwriting
382existing text. With prefix argument, the rectangle is
383inserted instead, shifting existing text. Leaves mark at one corner
384of rectangle and point at the other (diagonally opposed) corner."
385 (interactive "cRectangle from register: \nP")
386 (let ((rectangle (get-register register)))
387 (if (not (consp rectangle))
388 (error "Register %c does not contain a rectangle." register)
389 (picture-insert-rectangle rectangle insertp))))
390
391(defun picture-insert-rectangle (rectangle &optional insertp)
392 "Overlay RECTANGLE with upper left corner at point.
393Optional argument INSERTP, if non-nil causes RECTANGLE to be inserted.
394Leaves the region surrounding the rectangle."
395 (let ((indent-tabs-mode nil))
396 (if (not insertp)
397 (save-excursion
398 (delete-rectangle (point)
399 (progn
400 (picture-forward-column (length (car rectangle)))
401 (picture-move-down (1- (length rectangle)))
402 (point)))))
403 (push-mark)
404 (insert-rectangle rectangle)))
405
406\f
407;; Picture Keymap, entry and exit points.
408
409(defconst picture-mode-map nil)
410
411(if (not picture-mode-map)
412 (let ((i ?\ ))
413 (setq picture-mode-map (make-keymap))
414 (while (< i ?\177)
415 (aset picture-mode-map i 'picture-self-insert)
416 (setq i (1+ i)))
417 (define-key picture-mode-map "\C-f" 'picture-forward-column)
418 (define-key picture-mode-map "\C-b" 'picture-backward-column)
419 (define-key picture-mode-map "\C-d" 'picture-clear-column)
420 (define-key picture-mode-map "\C-c\C-d" 'delete-char)
421 (define-key picture-mode-map "\177" 'picture-backward-clear-column)
422 (define-key picture-mode-map "\C-k" 'picture-clear-line)
423 (define-key picture-mode-map "\C-o" 'picture-open-line)
424 (define-key picture-mode-map "\C-m" 'picture-newline)
425 (define-key picture-mode-map "\C-j" 'picture-duplicate-line)
426 (define-key picture-mode-map "\C-n" 'picture-move-down)
427 (define-key picture-mode-map "\C-p" 'picture-move-up)
428 (define-key picture-mode-map "\C-e" 'picture-end-of-line)
429 (define-key picture-mode-map "\e\t" 'picture-toggle-tab-state)
430 (define-key picture-mode-map "\t" 'picture-tab)
431 (define-key picture-mode-map "\e\t" 'picture-tab-search)
432 (define-key picture-mode-map "\C-c\t" 'picture-set-tab-stops)
433 (define-key picture-mode-map "\C-c\C-k" 'picture-clear-rectangle)
434 (define-key picture-mode-map "\C-c\C-w" 'picture-clear-rectangle-to-register)
435 (define-key picture-mode-map "\C-c\C-y" 'picture-yank-rectangle)
436 (define-key picture-mode-map "\C-c\C-x" 'picture-yank-rectangle-from-register)
437 (define-key picture-mode-map "\C-c\C-c" 'picture-mode-exit)
438 (define-key picture-mode-map "\C-c\C-f" 'picture-motion)
439 (define-key picture-mode-map "\C-c\C-b" 'picture-motion-reverse)
440 (define-key picture-mode-map "\C-c<" 'picture-movement-left)
441 (define-key picture-mode-map "\C-c>" 'picture-movement-right)
442 (define-key picture-mode-map "\C-c^" 'picture-movement-up)
443 (define-key picture-mode-map "\C-c." 'picture-movement-down)
444 (define-key picture-mode-map "\C-c`" 'picture-movement-nw)
445 (define-key picture-mode-map "\C-c'" 'picture-movement-ne)
446 (define-key picture-mode-map "\C-c/" 'picture-movement-sw)
447 (define-key picture-mode-map "\C-c\\" 'picture-movement-se)))
448
449(defvar edit-picture-hook nil
450 "If non-nil, it's value is called on entry to Picture mode.
451Picture mode is invoked by the command \\[edit-picture].")
452
453(defun edit-picture ()
454 "Switch to Picture mode, in which a quarter-plane screen model is used.
455Printing characters replace instead of inserting themselves with motion
456afterwards settable by these commands:
457 C-c < Move left after insertion.
458 C-c > Move right after insertion.
459 C-c ^ Move up after insertion.
460 C-c . Move down after insertion.
461 C-c ` Move northwest (nw) after insertion.
462 C-c ' Move northeast (ne) after insertion.
463 C-c / Move southwest (sw) after insertion.
464 C-c \\ Move southeast (se) after insertion.
465The current direction is displayed in the mode line. The initial
466direction is right. Whitespace is inserted and tabs are changed to
467spaces when required by movement. You can move around in the buffer
468with these commands:
469 C-p Move vertically to SAME column in previous line.
470 C-n Move vertically to SAME column in next line.
471 C-e Move to column following last non-whitespace character.
472 C-f Move right inserting spaces if required.
473 C-b Move left changing tabs to spaces if required.
474 C-c C-f Move in direction of current picture motion.
475 C-c C-b Move in opposite direction of current picture motion.
476 Return Move to beginning of next line.
477You can edit tabular text with these commands:
478 M-Tab Move to column beneath (or at) next interesting character.
479 `Indents' relative to a previous line.
480 Tab Move to next stop in tab stop list.
481 C-c Tab Set tab stops according to context of this line.
482 With ARG resets tab stops to default (global) value.
483 See also documentation of variable picture-tab-chars
484 which defines \"interesting character\". You can manually
485 change the tab stop list with command \\[edit-tab-stops].
486You can manipulate text with these commands:
487 C-d Clear (replace) ARG columns after point without moving.
488 C-c C-d Delete char at point - the command normally assigned to C-d.
489 Delete Clear (replace) ARG columns before point, moving back over them.
490 C-k Clear ARG lines, advancing over them. The cleared
491 text is saved in the kill ring.
492 C-o Open blank line(s) beneath current line.
493You can manipulate rectangles with these commands:
494 C-c C-k Clear (or kill) a rectangle and save it.
495 C-c C-w Like C-c C-k except rectangle is saved in named register.
496 C-c C-y Overlay (or insert) currently saved rectangle at point.
497 C-c C-x Like C-c C-y except rectangle is taken from named register.
498 \\[copy-rectangle-to-register] Copies a rectangle to a register.
499 \\[advertised-undo] Can undo effects of rectangle overlay commands
500 commands if invoked soon enough.
501You can return to the previous mode with:
502 C-c C-c Which also strips trailing whitespace from every line.
503 Stripping is suppressed by supplying an argument.
504
505Entry to this mode calls the value of edit-picture-hook if non-nil.
506
507Note that Picture mode commands will work outside of Picture mode, but
508they are not defaultly assigned to keys."
509 (interactive)
510 (if (eq major-mode 'edit-picture)
511 (error "You are already editing a Picture.")
512 (make-local-variable 'picture-mode-old-local-map)
513 (setq picture-mode-old-local-map (current-local-map))
514 (use-local-map picture-mode-map)
515 (make-local-variable 'picture-mode-old-mode-name)
516 (setq picture-mode-old-mode-name mode-name)
517 (make-local-variable 'picture-mode-old-major-mode)
518 (setq picture-mode-old-major-mode major-mode)
519 (setq major-mode 'edit-picture)
520 (make-local-variable 'picture-killed-rectangle)
521 (setq picture-killed-rectangle nil)
522 (make-local-variable 'tab-stop-list)
523 (setq tab-stop-list (default-value 'tab-stop-list))
524 (make-local-variable 'picture-tab-chars)
525 (setq picture-tab-chars (default-value 'picture-tab-chars))
526 (make-local-variable 'picture-vertical-step)
527 (make-local-variable 'picture-horizontal-step)
528 (picture-set-motion 0 1)
529 (run-hooks 'edit-picture-hook)
530 (message
531 (substitute-command-keys
532 "Type \\[picture-mode-exit] in this buffer to return it to %s mode.")
533 picture-mode-old-mode-name)))
534
535(fset 'picture-mode 'edit-picture) ; for the confused
536
537(defun picture-mode-exit (&optional nostrip)
538 "Undo edit-picture and return to previous major mode.
539With no argument strips whitespace from end of every line in Picture buffer
540 otherwise just return to previous mode."
541 (interactive "P")
542 (if (not (eq major-mode 'edit-picture))
543 (error "You aren't editing a Picture.")
544 (if (not nostrip) (picture-clean))
545 (setq mode-name picture-mode-old-mode-name)
546 (use-local-map picture-mode-old-local-map)
547 (setq major-mode picture-mode-old-major-mode)
548 (kill-local-variable 'tab-stop-list)
549 ;; Kludge - force the mode line to be updated. Is there a better
550 ;; way to do this?
551 (set-buffer-modified-p (buffer-modified-p))))
552
553(defun picture-clean ()
554 "Eliminate whitespace at ends of lines."
555 (save-excursion
556 (goto-char (point-min))
557 (while (re-search-forward "[ \t][ \t]*$" nil t)
558 (delete-region (match-beginning 0) (point)))))