picture.el 22 KB

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