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