home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / picture.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  22.1 KB  |  568 lines

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