1 ;;; picture.el --- "Picture mode" -- editing using quarter-plane screen model.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; Author: K. Shane Hartman
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This code provides the picture-mode commands documented in the Emacs
27 ;; manual. The screen is treated as a semi-infinite quarter-plane with
28 ;; support for rectangle operations and `etch-a-sketch' character
29 ;; insertion in any of eight directions.
33 (defun move-to-column-force (column)
34 "Move to column COLUMN in current line.
35 Differs from `move-to-column' in that it creates or modifies whitespace
36 if necessary to attain exactly the specified column."
37 (or (natnump column
) (setq column
0))
38 (move-to-column column
)
39 (let ((col (current-column)))
42 (if (and (/= col column
)
43 (= (preceding-char) ?
\t))
44 (let (indent-tabs-mode)
47 (move-to-column column
))))
48 ;; This call will go away when Emacs gets real horizontal autoscrolling
49 (hscroll-point-visible)))
52 ;; Picture Movement Commands
54 (defun picture-beginning-of-line (&optional arg
)
55 "Position point at the beginning of the line.
56 With ARG not nil, move forward ARG - 1 lines first.
57 If scan reaches end of buffer, stop there without error."
59 (if arg
(forward-line (1- (prefix-numeric-value arg
))))
61 ;; This call will go away when Emacs gets real horizontal autoscrolling
62 (hscroll-point-visible))
64 (defun picture-end-of-line (&optional arg
)
65 "Position point after last non-blank character on current line.
66 With ARG not nil, move forward ARG - 1 lines first.
67 If scan reaches end of buffer, stop there without error."
69 (if arg
(forward-line (1- (prefix-numeric-value arg
))))
71 (skip-chars-backward " \t" (prog1 (point) (end-of-line)))
72 ;; This call will go away when Emacs gets real horizontal autoscrolling
73 (hscroll-point-visible))
75 (defun picture-forward-column (arg)
76 "Move cursor right, making whitespace if necessary.
77 With argument, move that many columns."
79 (move-to-column-force (+ (current-column) arg
)))
81 (defun picture-backward-column (arg)
82 "Move cursor left, making whitespace if necessary.
83 With argument, move that many columns."
85 (move-to-column-force (- (current-column) arg
)))
87 (defun picture-move-down (arg)
88 "Move vertically down, making whitespace if necessary.
89 With argument, move that many lines."
91 (let ((col (current-column)))
93 (move-to-column-force col
)))
95 (defconst picture-vertical-step
0
96 "Amount to move vertically after text character in Picture mode.")
98 (defconst picture-horizontal-step
1
99 "Amount to move horizontally after text character in Picture mode.")
101 (defun picture-move-up (arg)
102 "Move vertically up, making whitespace if necessary.
103 With argument, move that many lines."
105 (picture-move-down (- arg
)))
107 (defun picture-movement-right ()
108 "Move right after self-inserting character in Picture mode."
110 (picture-set-motion 0 1))
112 (defun picture-movement-left ()
113 "Move left after self-inserting character in Picture mode."
115 (picture-set-motion 0 -
1))
117 (defun picture-movement-up ()
118 "Move up after self-inserting character in Picture mode."
120 (picture-set-motion -
1 0))
122 (defun picture-movement-down ()
123 "Move down after self-inserting character in Picture mode."
125 (picture-set-motion 1 0))
127 (defun picture-movement-nw ()
128 "Move up and left after self-inserting character in Picture mode."
130 (picture-set-motion -
1 -
1))
132 (defun picture-movement-ne ()
133 "Move up and right after self-inserting character in Picture mode."
135 (picture-set-motion -
1 1))
137 (defun picture-movement-sw ()
138 "Move down and left after self-inserting character in Picture mode."
140 (picture-set-motion 1 -
1))
142 (defun picture-movement-se ()
143 "Move down and right after self-inserting character in Picture mode."
145 (picture-set-motion 1 1))
147 (defun picture-set-motion (vert horiz
)
148 "Set VERTICAL and HORIZONTAL increments for movement in Picture mode.
149 The mode line is updated to reflect the current direction."
150 (setq picture-vertical-step vert
151 picture-horizontal-step horiz
)
154 (car (nthcdr (+ 1 (% horiz
2) (* 3 (1+ (% vert
2))))
155 '(nw up ne left none right sw down se
)))))
156 ;; Kludge - force the mode line to be updated. Is there a better
158 (set-buffer-modified-p (buffer-modified-p))
161 (defun picture-move ()
162 "Move in direction of `picture-vertical-step' and `picture-horizontal-step'."
163 (picture-move-down picture-vertical-step
)
164 (picture-forward-column picture-horizontal-step
))
166 (defun picture-motion (arg)
167 "Move point in direction of current picture motion in Picture mode.
168 With ARG do it that many times. Useful for delineating rectangles in
169 conjunction with diagonal picture motion.
170 Do \\[command-apropos] picture-movement to see commands which control motion."
172 (picture-move-down (* arg picture-vertical-step
))
173 (picture-forward-column (* arg picture-horizontal-step
)))
175 (defun picture-motion-reverse (arg)
176 "Move point in direction opposite of current picture motion in Picture mode.
177 With ARG do it that many times. Useful for delineating rectangles in
178 conjunction with diagonal picture motion.
179 Do \\[command-apropos] `picture-movement' to see commands which control motion."
181 (picture-motion (- arg
)))
184 ;; Picture insertion and deletion.
186 (defun picture-self-insert (arg)
187 "Insert this character in place of character previously at the cursor.
188 The cursor then moves in the direction you previously specified
189 with the commands `picture-movement-right', `picture-movement-up', etc.
190 Do \\[command-apropos] `picture-movement' to see those commands."
194 (move-to-column-force (1+ (current-column)))
196 (insert last-input-char
)
200 (defun picture-clear-column (arg)
201 "Clear out ARG columns after point without moving."
203 (let* ((opoint (point))
204 (original-col (current-column))
205 (target-col (+ original-col arg
)))
206 (move-to-column-force target-col
)
207 (delete-region opoint
(point))
209 (indent-to (max target-col original-col
)))))
211 (defun picture-backward-clear-column (arg)
212 "Clear out ARG columns before point, moving back over them."
214 (picture-clear-column (- arg
)))
216 (defun picture-clear-line (arg)
217 "Clear out rest of line; if at end of line, advance to next line.
218 Cleared-out line text goes into the kill ring, as do newlines that are
219 advanced over. With argument, clear out (and save in kill ring) that
224 (setq arg
(prefix-numeric-value arg
))
226 (newline (if (> arg
0) arg
(- arg
))))
227 (if (looking-at "[ \t]*$")
228 (kill-ring-save (point) (progn (forward-line 1) (point)))
229 (kill-region (point) (progn (end-of-line) (point))))))
231 (defun picture-newline (arg)
232 "Move to the beginning of the following line.
233 With argument, moves that many lines (up, if negative argument);
234 always moves to the beginning of a line."
240 (if (eobp) (newline) (forward-char 1))
241 (setq arg
(1- arg
))))
242 ;; This call will go away when Emacs gets real horizontal autoscrolling
243 (hscroll-point-visible))
245 (defun picture-open-line (arg)
246 "Insert an empty line after the current line.
247 With positive argument insert that many lines."
252 ;; This call will go away when Emacs gets real horizontal autoscrolling
253 (hscroll-point-visible))
255 (defun picture-duplicate-line ()
256 "Insert a duplicate of the current line, below it."
261 (progn (beginning-of-line) (point))
262 (progn (picture-newline 1) (point)))))
266 ;; Like replace-match, but overwrites.
267 (defun picture-replace-match (newtext fixedcase literal
)
268 (let (ocolumn change pos
)
269 (goto-char (setq pos
(match-end 0)))
270 (setq ocolumn
(current-column))
271 ;; Make the replacement and undo it, to see how it changes the length.
272 (let ((buffer-undo-list nil
)
274 (replace-match newtext fixedcase literal
)
275 (setq change
(- (current-column) ocolumn
))
276 (setq list1 buffer-undo-list
)
278 (setq list1
(primitive-undo 1 list1
))))
281 (delete-region (point)
283 (move-to-column-force (+ change
(current-column)))
285 (replace-match newtext fixedcase literal
)
287 (insert-char ?\
(- change
)))))
291 (defvar picture-tab-chars
"!-~"
292 "*A character set which controls behavior of commands
293 \\[picture-set-tab-stops] and \\[picture-tab-search]. It is NOT a
294 regular expression, any regexp special characters will be quoted.
295 It defines a set of \"interesting characters\" to look for when setting
296 \(or searching for) tab stops, initially \"!-~\" (all printing characters).
297 For example, suppose that you are editing a table which is formatted thus:
298 | foo | bar + baz | 23 *
299 | bubbles | and + etc | 97 *
300 and that `picture-tab-chars' is \"|+*\". Then invoking
301 \\[picture-set-tab-stops] on either of the previous lines would result
302 in the following tab stops
304 Another example - \"A-Za-z0-9\" would produce the tab stops
307 Note that if you want the character `-' to be in the set, it must be
308 included in a range or else appear in a context where it cannot be
309 taken for indicating a range (e.g. \"-A-Z\" declares the set to be the
310 letters `A' through `Z' and the character `-'). If you want the
311 character `\\' in the set it must be preceded by itself: \"\\\\\".
313 The command \\[picture-tab-search] is defined to move beneath (or to) a
314 character belonging to this set independent of the tab stops list.")
316 (defun picture-set-tab-stops (&optional arg
)
317 "Set value of `tab-stop-list' according to context of this line.
318 This controls the behavior of \\[picture-tab]. A tab stop is set at
319 every column occupied by an \"interesting character\" that is preceded
320 by whitespace. Interesting characters are defined by the variable
321 `picture-tab-chars', see its documentation for an example of usage.
322 With ARG, just (re)set `tab-stop-list' to its default value. The tab
323 stops computed are displayed in the minibuffer with `:' at each stop."
328 (setq tabs
(default-value 'tab-stop-list
))
329 (let ((regexp (concat "[ \t]+[" (regexp-quote picture-tab-chars
) "]")))
333 (while (re-search-backward regexp bol t
)
334 (skip-chars-forward " \t")
335 (setq tabs
(cons (current-column) tabs
)))
337 (error "No characters in set %s on this line."
338 (regexp-quote picture-tab-chars
))))))
339 (setq tab-stop-list tabs
)
340 (let ((blurb (make-string (1+ (nth (1- (length tabs
)) tabs
)) ?\
)))
342 (aset blurb
(car tabs
) ?
:)
343 (setq tabs
(cdr tabs
)))
346 (defun picture-tab-search (&optional arg
)
347 "Move to column beneath next interesting char in previous line.
348 With ARG move to column occupied by next interesting character in this
349 line. The character must be preceded by whitespace.
350 \"interesting characters\" are defined by variable `picture-tab-chars'.
351 If no such character is found, move to beginning of line."
353 (let ((target (current-column)))
359 (concat "^" (regexp-quote picture-tab-chars
))
362 (move-to-column target
))
363 (if (re-search-forward
364 (concat "[ \t]+[" (regexp-quote picture-tab-chars
) "]")
365 (save-excursion (end-of-line) (point))
367 (setq target
(1- (current-column)))
370 (move-to-column-force target
)
371 (beginning-of-line))))
373 (defun picture-tab (&optional arg
)
374 "Tab transparently (just move point) to next tab stop.
375 With prefix arg, overwrite the traversed text with spaces. The tab stop
376 list can be changed by \\[picture-set-tab-stops] and \\[edit-tab-stops].
377 See also documentation for variable `picture-tab-chars'."
379 (let* ((opoint (point)))
382 (let (indent-tabs-mode
383 (column (current-column)))
384 (delete-region opoint
(point))
385 (indent-to column
)))))
387 ;; Picture Rectangles
389 (defconst picture-killed-rectangle nil
390 "Rectangle killed or copied by \\[picture-clear-rectangle] in Picture mode.
391 The contents can be retrieved by \\[picture-yank-rectangle]")
393 (defun picture-clear-rectangle (start end
&optional killp
)
394 "Clear and save rectangle delineated by point and mark.
395 The rectangle is saved for yanking by \\[picture-yank-rectangle] and replaced
396 with whitespace. The previously saved rectangle, if any, is lost. With
397 prefix argument, the rectangle is actually killed, shifting remaining text."
399 (setq picture-killed-rectangle
(picture-snarf-rectangle start end killp
)))
401 (defun picture-clear-rectangle-to-register (start end register
&optional killp
)
402 "Clear rectangle delineated by point and mark into REGISTER.
403 The rectangle is saved in REGISTER and replaced with whitespace. With
404 prefix argument, the rectangle is actually killed, shifting remaining text."
405 (interactive "r\ncRectangle to register: \nP")
406 (set-register register
(picture-snarf-rectangle start end killp
)))
408 (defun picture-snarf-rectangle (start end
&optional killp
)
409 (let ((column (current-column))
410 (indent-tabs-mode nil
))
411 (prog1 (save-excursion
413 (delete-extract-rectangle start end
)
414 (prog1 (extract-rectangle start end
)
415 (clear-rectangle start end
))))
416 (move-to-column-force column
))))
418 (defun picture-yank-rectangle (&optional insertp
)
419 "Overlay rectangle saved by \\[picture-clear-rectangle]
420 The rectangle is positioned with upper left corner at point, overwriting
421 existing text. With prefix argument, the rectangle is inserted instead,
422 shifting existing text. Leaves mark at one corner of rectangle and
423 point at the other (diagonally opposed) corner."
425 (if (not (consp picture-killed-rectangle
))
426 (error "No rectangle saved.")
427 (picture-insert-rectangle picture-killed-rectangle insertp
)))
429 (defun picture-yank-rectangle-from-register (register &optional insertp
)
430 "Overlay rectangle saved in REGISTER.
431 The rectangle is positioned with upper left corner at point, overwriting
432 existing text. With prefix argument, the rectangle is
433 inserted instead, shifting existing text. Leaves mark at one corner
434 of rectangle and point at the other (diagonally opposed) corner."
435 (interactive "cRectangle from register: \nP")
436 (let ((rectangle (get-register register
)))
437 (if (not (consp rectangle
))
438 (error "Register %c does not contain a rectangle." register
)
439 (picture-insert-rectangle rectangle insertp
))))
441 (defun picture-insert-rectangle (rectangle &optional insertp
)
442 "Overlay RECTANGLE with upper left corner at point.
443 Optional argument INSERTP, if non-nil causes RECTANGLE to be inserted.
444 Leaves the region surrounding the rectangle."
445 (let ((indent-tabs-mode nil
))
448 (delete-rectangle (point)
450 (picture-forward-column (length (car rectangle
)))
451 (picture-move-down (1- (length rectangle
)))
454 (insert-rectangle rectangle
)))
457 ;; Picture Keymap, entry and exit points.
459 (defconst picture-mode-map nil
)
461 (defun picture-substitute (oldfun newfun
)
462 (substitute-key-definition oldfun newfun picture-mode-map global-map
))
464 (if (not picture-mode-map
)
466 (setq picture-mode-map
(make-keymap))
468 (define-key picture-mode-map
(make-string 1 i
) 'picture-self-insert
)
471 (picture-substitute 'forward-char
'picture-forward-column
)
472 (picture-substitute 'backward-char
'picture-backward-column
)
473 (picture-substitute 'delete-char
'picture-clear-column
)
474 (picture-substitute 'backward-delete-char-untabify
'picture-backward-clear-column
)
475 (picture-substitute 'kill-line
'picture-clear-line
)
476 (picture-substitute 'open-line
'picture-open-line
)
477 (picture-substitute 'newline
'picture-newline
)
478 (picture-substitute 'newline-andindent
'picture-duplicate-line
)
479 (picture-substitute 'next-line
'picture-move-down
)
480 (picture-substitute 'previous-line
'picture-move-up
)
481 (picture-substitute 'beginning-of-line
'picture-beginning-of-line
)
482 (picture-substitute 'end-of-line
'picture-end-of-line
)
484 (define-key picture-mode-map
"\C-c\C-d" 'delete-char
)
485 (define-key picture-mode-map
"\e\t" 'picture-toggle-tab-state
)
486 (define-key picture-mode-map
"\t" 'picture-tab
)
487 (define-key picture-mode-map
"\e\t" 'picture-tab-search
)
488 (define-key picture-mode-map
"\C-c\t" 'picture-set-tab-stops
)
489 (define-key picture-mode-map
"\C-c\C-k" 'picture-clear-rectangle
)
490 (define-key picture-mode-map
"\C-c\C-w" 'picture-clear-rectangle-to-register
)
491 (define-key picture-mode-map
"\C-c\C-y" 'picture-yank-rectangle
)
492 (define-key picture-mode-map
"\C-c\C-x" 'picture-yank-rectangle-from-register
)
493 (define-key picture-mode-map
"\C-c\C-c" 'picture-mode-exit
)
494 (define-key picture-mode-map
"\C-c\C-f" 'picture-motion
)
495 (define-key picture-mode-map
"\C-c\C-b" 'picture-motion-reverse
)
496 (define-key picture-mode-map
"\C-c<" 'picture-movement-left
)
497 (define-key picture-mode-map
"\C-c>" 'picture-movement-right
)
498 (define-key picture-mode-map
"\C-c^" 'picture-movement-up
)
499 (define-key picture-mode-map
"\C-c." 'picture-movement-down
)
500 (define-key picture-mode-map
"\C-c`" 'picture-movement-nw
)
501 (define-key picture-mode-map
"\C-c'" 'picture-movement-ne
)
502 (define-key picture-mode-map
"\C-c/" 'picture-movement-sw
)
503 (define-key picture-mode-map
"\C-c\\" 'picture-movement-se
)))
505 (defvar picture-mode-hook nil
506 "If non-nil, its value is called on entry to Picture mode.
507 Picture mode is invoked by the command \\[picture-mode].")
509 (defvar picture-mode-old-local-map
)
510 (defvar picture-mode-old-mode-name
)
511 (defvar picture-mode-old-major-mode
)
514 (defun picture-mode ()
515 "Switch to Picture mode, in which a quarter-plane screen model is used.
516 Printing characters replace instead of inserting themselves with motion
517 afterwards settable by these commands:
518 C-c < Move left after insertion.
519 C-c > Move right after insertion.
520 C-c ^ Move up after insertion.
521 C-c . Move down after insertion.
522 C-c ` Move northwest (nw) after insertion.
523 C-c ' Move northeast (ne) after insertion.
524 C-c / Move southwest (sw) after insertion.
525 C-c \\ Move southeast (se) after insertion.
526 The current direction is displayed in the mode line. The initial
527 direction is right. Whitespace is inserted and tabs are changed to
528 spaces when required by movement. You can move around in the buffer
530 \\[picture-move-down] Move vertically to SAME column in previous line.
531 \\[picture-move-up] Move vertically to SAME column in next line.
532 \\[picture-end-of-line] Move to column following last non-whitespace character.
533 \\[picture-forward-column] Move right inserting spaces if required.
534 \\[picture-backward-column] Move left changing tabs to spaces if required.
535 C-c C-f Move in direction of current picture motion.
536 C-c C-b Move in opposite direction of current picture motion.
537 Return Move to beginning of next line.
538 You can edit tabular text with these commands:
539 M-Tab Move to column beneath (or at) next interesting character.
540 `Indents' relative to a previous line.
541 Tab Move to next stop in tab stop list.
542 C-c Tab Set tab stops according to context of this line.
543 With ARG resets tab stops to default (global) value.
544 See also documentation of variable picture-tab-chars
545 which defines \"interesting character\". You can manually
546 change the tab stop list with command \\[edit-tab-stops].
547 You can manipulate text with these commands:
548 C-d Clear (replace) ARG columns after point without moving.
549 C-c C-d Delete char at point - the command normally assigned to C-d.
550 \\[picture-backward-clear-column] Clear (replace) ARG columns before point, moving back over them.
551 \\[picture-clear-line] Clear ARG lines, advancing over them. The cleared
552 text is saved in the kill ring.
553 \\[picture-open-line] Open blank line(s) beneath current line.
554 You can manipulate rectangles with these commands:
555 C-c C-k Clear (or kill) a rectangle and save it.
556 C-c C-w Like C-c C-k except rectangle is saved in named register.
557 C-c C-y Overlay (or insert) currently saved rectangle at point.
558 C-c C-x Like C-c C-y except rectangle is taken from named register.
559 \\[copy-rectangle-to-register] Copies a rectangle to a register.
560 \\[advertised-undo] Can undo effects of rectangle overlay commands
561 commands if invoked soon enough.
562 You can return to the previous mode with:
563 C-c C-c Which also strips trailing whitespace from every line.
564 Stripping is suppressed by supplying an argument.
566 Entry to this mode calls the value of picture-mode-hook if non-nil.
568 Note that Picture mode commands will work outside of Picture mode, but
569 they are not defaultly assigned to keys."
571 (if (eq major-mode
'picture-mode
)
572 (error "You are already editing a picture.")
573 (make-local-variable 'picture-mode-old-local-map
)
574 (setq picture-mode-old-local-map
(current-local-map))
575 (use-local-map picture-mode-map
)
576 (make-local-variable 'picture-mode-old-mode-name
)
577 (setq picture-mode-old-mode-name mode-name
)
578 (make-local-variable 'picture-mode-old-major-mode
)
579 (setq picture-mode-old-major-mode major-mode
)
580 (setq major-mode
'picture-mode
)
581 (make-local-variable 'picture-killed-rectangle
)
582 (setq picture-killed-rectangle nil
)
583 (make-local-variable 'tab-stop-list
)
584 (setq tab-stop-list
(default-value 'tab-stop-list
))
585 (make-local-variable 'picture-tab-chars
)
586 (setq picture-tab-chars
(default-value 'picture-tab-chars
))
587 (make-local-variable 'picture-vertical-step
)
588 (make-local-variable 'picture-horizontal-step
)
589 (setq truncate-lines t
)
590 (picture-set-motion 0 1)
592 ;; edit-picture-hook is what we used to run, picture-mode-hook is in doc.
593 (run-hooks 'edit-picture-hook
'picture-mode-hook
)
595 (substitute-command-keys
596 "Type \\[picture-mode-exit] in this buffer to return it to %s mode.")
597 picture-mode-old-mode-name
)))
600 (defalias 'edit-picture
'picture-mode
)
602 (defun picture-mode-exit (&optional nostrip
)
603 "Undo picture-mode and return to previous major mode.
604 With no argument strips whitespace from end of every line in Picture buffer
605 otherwise just return to previous mode."
607 (if (not (eq major-mode
'picture-mode
))
608 (error "You aren't editing a Picture.")
609 (if (not nostrip
) (picture-clean))
610 (setq mode-name picture-mode-old-mode-name
)
611 (use-local-map picture-mode-old-local-map
)
612 (setq major-mode picture-mode-old-major-mode
)
613 (kill-local-variable 'tab-stop-list
)
614 ;; Kludge - force the mode line to be updated. Is there a better
616 (set-buffer-modified-p (buffer-modified-p))))
618 (defun picture-clean ()
619 "Eliminate whitespace at ends of lines."
621 (goto-char (point-min))
622 (while (re-search-forward "[ \t][ \t]*$" nil t
)
623 (delete-region (match-beginning 0) (point)))))
627 ;;; picture.el ends here