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