picture.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. ;;; picture.el --- "Picture mode" -- editing using quarter-plane screen model
  2. ;; Copyright (C) 1985, 1994, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: K. Shane Hartman
  4. ;; Maintainer: FSF
  5. ;; Keywords: convenience wp
  6. ;; This file is part of GNU Emacs.
  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 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This code provides the picture-mode commands documented in the Emacs
  19. ;; manual. The screen is treated as a semi-infinite quarter-plane with
  20. ;; support for rectangle operations and `etch-a-sketch' character
  21. ;; insertion in any of eight directions.
  22. ;;; Code:
  23. (defgroup picture nil
  24. "Picture mode --- editing using quarter-plane screen model."
  25. :prefix "picture-"
  26. :group 'wp)
  27. (defcustom picture-rectangle-ctl ?+
  28. "Character `picture-draw-rectangle' uses for top left corners."
  29. :type 'character
  30. :group 'picture)
  31. (defcustom picture-rectangle-ctr ?+
  32. "Character `picture-draw-rectangle' uses for top right corners."
  33. :type 'character
  34. :group 'picture)
  35. (defcustom picture-rectangle-cbr ?+
  36. "Character `picture-draw-rectangle' uses for bottom right corners."
  37. :type 'character
  38. :group 'picture)
  39. (defcustom picture-rectangle-cbl ?+
  40. "Character `picture-draw-rectangle' uses for bottom left corners."
  41. :type 'character
  42. :group 'picture)
  43. (defcustom picture-rectangle-v ?|
  44. "Character `picture-draw-rectangle' uses for vertical lines."
  45. :type 'character
  46. :group 'picture)
  47. (defcustom picture-rectangle-h ?-
  48. "Character `picture-draw-rectangle' uses for horizontal lines."
  49. :type 'character
  50. :group 'picture)
  51. ;; Picture Movement Commands
  52. ;; When a cursor is on a wide-column character (e.g. Chinese,
  53. ;; Japanese, Korean), this variable tells the desired current column
  54. ;; which may be different from (current-column).
  55. (defvar picture-desired-column 0)
  56. ;; If the value of picture-desired-column is far from the current
  57. ;; column, or if the arg ADJUST-TO-CURRENT is non-nil, set it to the
  58. ;; current column. Return the current column.
  59. (defun picture-update-desired-column (adjust-to-current)
  60. (let ((current-column (current-column)))
  61. (if (or adjust-to-current
  62. (< picture-desired-column (1- current-column))
  63. (> picture-desired-column (1+ current-column)))
  64. (setq picture-desired-column current-column))
  65. current-column))
  66. (defun picture-beginning-of-line (&optional arg)
  67. "Position point at the beginning of the line.
  68. With ARG not nil, move forward ARG - 1 lines first.
  69. If scan reaches end of buffer, stop there without error."
  70. (interactive "^P")
  71. (if arg (forward-line (1- (prefix-numeric-value arg))))
  72. (beginning-of-line)
  73. (setq picture-desired-column 0))
  74. (defun picture-end-of-line (&optional arg)
  75. "Position point after last non-blank character on current line.
  76. With ARG not nil, move forward ARG - 1 lines first.
  77. If scan reaches end of buffer, stop there without error."
  78. (interactive "^P")
  79. (if arg (forward-line (1- (prefix-numeric-value arg))))
  80. (beginning-of-line)
  81. (skip-chars-backward " \t" (prog1 (point) (end-of-line)))
  82. (setq picture-desired-column (current-column)))
  83. (defun picture-forward-column (arg &optional interactive)
  84. "Move cursor right, making whitespace if necessary.
  85. With argument, move that many columns."
  86. (interactive "^p\nd")
  87. (let (deactivate-mark)
  88. (picture-update-desired-column interactive)
  89. (setq picture-desired-column (max 0 (+ picture-desired-column arg)))
  90. (let ((current-column (move-to-column picture-desired-column t)))
  91. (if (and (> current-column picture-desired-column)
  92. (< arg 0))
  93. ;; It seems that we have just tried to move to the right
  94. ;; column of a multi-column character.
  95. (forward-char -1)))))
  96. (defun picture-backward-column (arg &optional interactive)
  97. "Move cursor left, making whitespace if necessary.
  98. With argument, move that many columns."
  99. (interactive "^p\nd")
  100. (picture-update-desired-column interactive)
  101. (picture-forward-column (- arg)))
  102. (defun picture-move-down (arg)
  103. "Move vertically down, making whitespace if necessary.
  104. With argument, move that many lines."
  105. (interactive "^p")
  106. (let (deactivate-mark)
  107. (picture-update-desired-column nil)
  108. (picture-newline arg)
  109. (let ((current-column (move-to-column picture-desired-column t)))
  110. (if (> current-column picture-desired-column)
  111. (forward-char -1)))))
  112. (defvar picture-vertical-step 0
  113. "Amount to move vertically after text character in Picture mode.")
  114. (defvar picture-horizontal-step 1
  115. "Amount to move horizontally after text character in Picture mode.")
  116. (defun picture-move-up (arg)
  117. "Move vertically up, making whitespace if necessary.
  118. With argument, move that many lines."
  119. (interactive "^p")
  120. (picture-update-desired-column nil)
  121. (picture-move-down (- arg)))
  122. (defun picture-movement-right ()
  123. "Move right after self-inserting character in Picture mode."
  124. (interactive)
  125. (picture-set-motion 0 1))
  126. (defun picture-movement-left ()
  127. "Move left after self-inserting character in Picture mode."
  128. (interactive)
  129. (picture-set-motion 0 -1))
  130. (defun picture-movement-up ()
  131. "Move up after self-inserting character in Picture mode."
  132. (interactive)
  133. (picture-set-motion -1 0))
  134. (defun picture-movement-down ()
  135. "Move down after self-inserting character in Picture mode."
  136. (interactive)
  137. (picture-set-motion 1 0))
  138. (defun picture-movement-nw (&optional arg)
  139. "Move up and left after self-inserting character in Picture mode.
  140. With prefix argument, move up and two-column left."
  141. (interactive "P")
  142. (picture-set-motion -1 (if arg -2 -1)))
  143. (defun picture-movement-ne (&optional arg)
  144. "Move up and right after self-inserting character in Picture mode.
  145. With prefix argument, move up and two-column right."
  146. (interactive "P")
  147. (picture-set-motion -1 (if arg 2 1)))
  148. (defun picture-movement-sw (&optional arg)
  149. "Move down and left after self-inserting character in Picture mode.
  150. With prefix argument, move down and two-column left."
  151. (interactive "P")
  152. (picture-set-motion 1 (if arg -2 -1)))
  153. (defun picture-movement-se (&optional arg)
  154. "Move down and right after self-inserting character in Picture mode.
  155. With prefix argument, move down and two-column right."
  156. (interactive "P")
  157. (picture-set-motion 1 (if arg 2 1)))
  158. (defun picture-set-motion (vert horiz)
  159. "Set VERTICAL and HORIZONTAL increments for movement in Picture mode.
  160. The mode line is updated to reflect the current direction."
  161. (setq picture-vertical-step vert
  162. picture-horizontal-step horiz)
  163. (setq mode-name
  164. (format "Picture:%s"
  165. (nth (+ 2 (% horiz 3) (* 5 (1+ (% vert 2))))
  166. '(wnw nw up ne ene Left left none right Right
  167. wsw sw down se ese))))
  168. (force-mode-line-update)
  169. (message ""))
  170. (defun picture-move ()
  171. "Move in direction of `picture-vertical-step' and `picture-horizontal-step'."
  172. (if (/= picture-vertical-step 0)
  173. (picture-move-down picture-vertical-step))
  174. (if (/= picture-horizontal-step 0)
  175. (picture-forward-column picture-horizontal-step)))
  176. (defun picture-motion (arg)
  177. "Move point in direction of current picture motion in Picture mode.
  178. With ARG do it that many times. Useful for delineating rectangles in
  179. conjunction with diagonal picture motion.
  180. Use \"\\[command-apropos] picture-movement\" to see commands which control motion."
  181. (interactive "^p")
  182. (picture-move-down (* arg picture-vertical-step))
  183. (picture-forward-column (* arg picture-horizontal-step)))
  184. (defun picture-motion-reverse (arg)
  185. "Move point in direction opposite of current picture motion in Picture mode.
  186. With ARG do it that many times. Useful for delineating rectangles in
  187. conjunction with diagonal picture motion.
  188. Use \"\\[command-apropos] picture-movement\" to see commands which control motion."
  189. (interactive "^p")
  190. (picture-motion (- arg)))
  191. (defun picture-mouse-set-point (event)
  192. "Move point to the position of EVENT, making whitespace if necessary."
  193. (interactive "e")
  194. (let ((position (event-start event)))
  195. (unless (posn-area position) ; Ignore EVENT unless in text area
  196. (let* ((window (posn-window position))
  197. (frame (if (framep window) window (window-frame window)))
  198. (pair (posn-x-y position))
  199. (start-pos (window-start window))
  200. (start-pair (posn-x-y (posn-at-point start-pos)))
  201. (dx (- (car pair) (car start-pair)))
  202. (dy (- (cdr pair) (cdr start-pair)))
  203. (char-ht (frame-char-height frame))
  204. (spacing (when (display-graphic-p frame)
  205. (or (with-current-buffer (window-buffer window)
  206. line-spacing)
  207. (frame-parameter frame 'line-spacing))))
  208. rows cols)
  209. (cond ((floatp spacing)
  210. (setq spacing (truncate (* spacing char-ht))))
  211. ((null spacing)
  212. (setq spacing 0)))
  213. (goto-char start-pos)
  214. (picture-move-down (/ dy (+ char-ht spacing)))
  215. (picture-forward-column (/ dx (frame-char-width frame)))))))
  216. ;; Picture insertion and deletion.
  217. (defun picture-insert (ch arg)
  218. (let* ((width (char-width ch))
  219. ;; We must be sure that the succeeding insertion won't delete
  220. ;; the just inserted character.
  221. (picture-horizontal-step
  222. (if (and (= picture-vertical-step 0)
  223. (> width 1)
  224. (< (abs picture-horizontal-step) 2))
  225. (* picture-horizontal-step 2)
  226. picture-horizontal-step)))
  227. (while (> arg 0)
  228. (setq arg (1- arg))
  229. (if (/= picture-desired-column (current-column))
  230. (move-to-column picture-desired-column t))
  231. (let ((col (+ picture-desired-column width)))
  232. (or (eolp)
  233. (let ((pos (point)))
  234. (move-to-column col t)
  235. (delete-region pos (point)))))
  236. (insert ch)
  237. (forward-char -1)
  238. (picture-move))))
  239. (defun picture-self-insert (arg)
  240. "Insert this character in place of character previously at the cursor.
  241. The cursor then moves in the direction you previously specified
  242. with the commands `picture-movement-right', `picture-movement-up', etc.
  243. Use \"\\[command-apropos] picture-movement\" to see those commands."
  244. (interactive "p")
  245. (picture-update-desired-column (not (eq this-command last-command)))
  246. (picture-insert last-command-event arg)) ; Always a character in this case.
  247. (defun picture-clear-column (arg)
  248. "Clear out ARG columns after point without moving."
  249. (interactive "p")
  250. (let* ((original-col (current-column))
  251. (target-col (max 0 (+ original-col arg)))
  252. pos)
  253. (move-to-column target-col t)
  254. (setq pos (point))
  255. (move-to-column original-col)
  256. (delete-region pos (point))
  257. (save-excursion
  258. (indent-to (max target-col original-col))))
  259. (setq picture-desired-column (current-column)))
  260. (defun picture-backward-clear-column (arg)
  261. "Clear out ARG columns before point, moving back over them."
  262. (interactive "p")
  263. (picture-clear-column (- arg)))
  264. (defun picture-clear-line (arg)
  265. "Clear out rest of line; if at end of line, advance to next line.
  266. Cleared-out line text goes into the kill ring, as do newlines that are
  267. advanced over. With argument, clear out (and save in kill ring) that
  268. many lines."
  269. (interactive "P")
  270. (if arg
  271. (progn
  272. (setq arg (prefix-numeric-value arg))
  273. (kill-line arg)
  274. (newline (if (> arg 0) arg (- arg))))
  275. (if (looking-at "[ \t]*$")
  276. (kill-ring-save (point) (progn (forward-line 1) (point)))
  277. (kill-region (point) (progn (end-of-line) (point))))))
  278. (defun picture-newline (arg)
  279. "Move to the beginning of the following line.
  280. With argument, moves that many lines (up, if negative argument);
  281. always moves to the beginning of a line."
  282. (interactive "^p")
  283. (let ((start (point))
  284. (lines-left (forward-line arg)))
  285. (if (and (eobp)
  286. (> (point) start))
  287. (newline))
  288. (if (> lines-left 0)
  289. (newline lines-left))))
  290. (defun picture-open-line (arg)
  291. "Insert an empty line after the current line.
  292. With positive argument insert that many lines."
  293. (interactive "p")
  294. (save-excursion
  295. (end-of-line)
  296. (open-line arg)))
  297. (defun picture-duplicate-line ()
  298. "Insert a duplicate of the current line, below it."
  299. (interactive)
  300. (save-excursion
  301. (let ((contents
  302. (buffer-substring
  303. (progn (beginning-of-line) (point))
  304. (progn (picture-newline 1) (point)))))
  305. (forward-line -1)
  306. (insert contents))))
  307. ;; Like replace-match, but overwrites.
  308. (defun picture-replace-match (newtext fixedcase literal)
  309. (let (ocolumn change pos)
  310. (goto-char (setq pos (match-end 0)))
  311. (setq ocolumn (current-column))
  312. ;; Make the replacement and undo it, to see how it changes the length.
  313. (let ((buffer-undo-list nil)
  314. list1)
  315. (replace-match newtext fixedcase literal)
  316. (setq change (- (current-column) ocolumn))
  317. (setq list1 buffer-undo-list)
  318. (while list1
  319. (setq list1 (primitive-undo 1 list1))))
  320. (goto-char pos)
  321. (if (> change 0)
  322. (delete-region (point)
  323. (progn
  324. (move-to-column (+ change (current-column)) t)
  325. (point))))
  326. (replace-match newtext fixedcase literal)
  327. (if (< change 0)
  328. (insert-char ?\s (- change)))))
  329. ;; Picture Tabs
  330. (defcustom picture-tab-chars "!-~"
  331. "A character set which controls behavior of commands.
  332. \\[picture-set-tab-stops] and \\[picture-tab-search].
  333. The syntax for this variable is like the syntax used inside of `[...]'
  334. in a regular expression--but without the `[' and the `]'.
  335. It is NOT a regular expression, any regexp special characters will be quoted.
  336. It defines a set of \"interesting characters\" to look for when setting
  337. \(or searching for) tab stops, initially \"!-~\" (all printing characters).
  338. For example, suppose that you are editing a table which is formatted thus:
  339. | foo | bar + baz | 23 *
  340. | bubbles | and + etc | 97 *
  341. and that `picture-tab-chars' is \"|+*\". Then invoking
  342. \\[picture-set-tab-stops] on either of the previous lines would result
  343. in the following tab stops
  344. : : : :
  345. Another example - \"A-Za-z0-9\" would produce the tab stops
  346. : : : :
  347. Note that if you want the character `-' to be in the set, it must be
  348. included in a range or else appear in a context where it cannot be
  349. taken for indicating a range (e.g. \"-A-Z\" declares the set to be the
  350. letters `A' through `Z' and the character `-'). If you want the
  351. character `\\' in the set it must be preceded by itself: \"\\\\\".
  352. The command \\[picture-tab-search] is defined to move beneath (or to) a
  353. character belonging to this set independent of the tab stops list."
  354. :type 'string
  355. :group 'picture)
  356. (defun picture-set-tab-stops (&optional arg)
  357. "Set value of `tab-stop-list' according to context of this line.
  358. This controls the behavior of \\[picture-tab]. A tab stop is set at
  359. every column occupied by an \"interesting character\" that is preceded
  360. by whitespace. Interesting characters are defined by the variable
  361. `picture-tab-chars', see its documentation for an example of usage.
  362. With ARG, just (re)set `tab-stop-list' to its default value. The tab
  363. stops computed are displayed in the minibuffer with `:' at each stop."
  364. (interactive "P")
  365. (save-excursion
  366. (let (tabs)
  367. (if arg
  368. (setq tabs (default-value 'tab-stop-list))
  369. (let ((regexp (concat "[ \t]+[" (regexp-quote picture-tab-chars) "]")))
  370. (beginning-of-line)
  371. (let ((bol (point)))
  372. (end-of-line)
  373. (while (re-search-backward regexp bol t)
  374. (skip-chars-forward " \t")
  375. (setq tabs (cons (current-column) tabs)))
  376. (if (null tabs)
  377. (error "No characters in set %s on this line"
  378. (regexp-quote picture-tab-chars))))))
  379. (setq tab-stop-list tabs)
  380. (let ((blurb (make-string (1+ (nth (1- (length tabs)) tabs)) ?\ )))
  381. (while tabs
  382. (aset blurb (car tabs) ?:)
  383. (setq tabs (cdr tabs)))
  384. (message blurb)))))
  385. (defun picture-tab-search (&optional arg)
  386. "Move to column beneath next interesting char in previous line.
  387. With ARG move to column occupied by next interesting character in this
  388. line. The character must be preceded by whitespace.
  389. \"interesting characters\" are defined by variable `picture-tab-chars'.
  390. If no such character is found, move to beginning of line."
  391. (interactive "^P")
  392. (let ((target (current-column)))
  393. (save-excursion
  394. (if (and (not arg)
  395. (progn
  396. (beginning-of-line)
  397. (skip-chars-backward
  398. (concat "^" (regexp-quote picture-tab-chars))
  399. (point-min))
  400. (not (bobp))))
  401. (move-to-column target))
  402. (if (re-search-forward
  403. (concat "[ \t]+[" (regexp-quote picture-tab-chars) "]")
  404. (line-end-position)
  405. 'move)
  406. (setq target (1- (current-column)))
  407. (setq target nil)))
  408. (if target
  409. (move-to-column target t)
  410. (beginning-of-line))))
  411. (defun picture-tab (&optional arg)
  412. "Tab transparently (just move point) to next tab stop.
  413. With prefix arg, overwrite the traversed text with spaces. The tab stop
  414. list can be changed by \\[picture-set-tab-stops] and \\[edit-tab-stops].
  415. See also documentation for variable `picture-tab-chars'."
  416. (interactive "^P")
  417. (let* ((opoint (point)))
  418. (move-to-tab-stop)
  419. (if arg
  420. (let (indent-tabs-mode
  421. (column (current-column)))
  422. (delete-region opoint (point))
  423. (indent-to column)))))
  424. ;; Picture Rectangles
  425. (defvar picture-killed-rectangle nil
  426. "Rectangle killed or copied by \\[picture-clear-rectangle] in Picture mode.
  427. The contents can be retrieved by \\[picture-yank-rectangle]")
  428. (defun picture-clear-rectangle (start end &optional killp)
  429. "Clear and save rectangle delineated by point and mark.
  430. The rectangle is saved for yanking by \\[picture-yank-rectangle] and replaced
  431. with whitespace. The previously saved rectangle, if any, is lost. With
  432. prefix argument, the rectangle is actually killed, shifting remaining text."
  433. (interactive "r\nP")
  434. (setq picture-killed-rectangle (picture-snarf-rectangle start end killp)))
  435. (defun picture-clear-rectangle-to-register (start end register &optional killp)
  436. "Clear rectangle delineated by point and mark into REGISTER.
  437. The rectangle is saved in REGISTER and replaced with whitespace. With
  438. prefix argument, the rectangle is actually killed, shifting remaining text."
  439. (interactive "r\ncRectangle to register: \nP")
  440. (set-register register (picture-snarf-rectangle start end killp)))
  441. (defun picture-snarf-rectangle (start end &optional killp)
  442. (let ((column (current-column))
  443. (indent-tabs-mode nil))
  444. (prog1 (save-excursion
  445. (if killp
  446. (delete-extract-rectangle start end)
  447. (prog1 (extract-rectangle start end)
  448. (clear-rectangle start end))))
  449. (move-to-column column t))))
  450. (defun picture-yank-rectangle (&optional insertp)
  451. "Overlay rectangle saved by \\[picture-clear-rectangle]
  452. The rectangle is positioned with upper left corner at point, overwriting
  453. existing text. With prefix argument, the rectangle is inserted instead,
  454. shifting existing text. Leaves mark at one corner of rectangle and
  455. point at the other (diagonally opposed) corner."
  456. (interactive "P")
  457. (if (not (consp picture-killed-rectangle))
  458. (error "No rectangle saved")
  459. (picture-insert-rectangle picture-killed-rectangle insertp)))
  460. (defun picture-yank-at-click (click arg)
  461. "Insert the last killed rectangle at the position clicked on.
  462. Also move point to one end of the text thus inserted (normally the end).
  463. Prefix arguments are interpreted as with \\[yank].
  464. If `mouse-yank-at-point' is non-nil, insert at point
  465. regardless of where you click."
  466. (interactive "e\nP")
  467. (or mouse-yank-at-point (mouse-set-point click))
  468. (picture-yank-rectangle arg))
  469. (defun picture-yank-rectangle-from-register (register &optional insertp)
  470. "Overlay rectangle saved in REGISTER.
  471. The rectangle is positioned with upper left corner at point, overwriting
  472. existing text. With prefix argument, the rectangle is
  473. inserted instead, shifting existing text. Leaves mark at one corner
  474. of rectangle and point at the other (diagonally opposed) corner."
  475. (interactive "cRectangle from register: \nP")
  476. (let ((rectangle (get-register register)))
  477. (if (not (consp rectangle))
  478. (error "Register %c does not contain a rectangle" register)
  479. (picture-insert-rectangle rectangle insertp))))
  480. (defun picture-insert-rectangle (rectangle &optional insertp)
  481. "Overlay RECTANGLE with upper left corner at point.
  482. Optional argument INSERTP, if non-nil causes RECTANGLE to be inserted.
  483. Leaves the region surrounding the rectangle."
  484. (let ((indent-tabs-mode nil))
  485. (if (not insertp)
  486. (save-excursion
  487. (delete-rectangle (point)
  488. (progn
  489. (picture-forward-column (length (car rectangle)))
  490. (picture-move-down (1- (length rectangle)))
  491. (point)))))
  492. (push-mark)
  493. (insert-rectangle rectangle)))
  494. (defun picture-current-line ()
  495. "Return the vertical position of point. Top line is 1."
  496. (+ (count-lines (point-min) (point))
  497. (if (= (current-column) 0) 1 0)))
  498. (defun picture-draw-rectangle (start end)
  499. "Draw a rectangle around region."
  500. (interactive "*r") ; start will be less than end
  501. (let* ((sl (picture-current-line))
  502. (sc (current-column))
  503. (pvs picture-vertical-step)
  504. (phs picture-horizontal-step)
  505. (c1 (progn (goto-char start) (current-column)))
  506. (r1 (picture-current-line))
  507. (c2 (progn (goto-char end) (current-column)))
  508. (r2 (picture-current-line))
  509. (right (max c1 c2))
  510. (left (min c1 c2))
  511. (top (min r1 r2))
  512. (bottom (max r1 r2)))
  513. (goto-char (point-min))
  514. (forward-line (1- top))
  515. (move-to-column left t)
  516. (picture-update-desired-column t)
  517. (picture-movement-right)
  518. (picture-insert picture-rectangle-ctl 1)
  519. (picture-insert picture-rectangle-h (- right picture-desired-column))
  520. (picture-movement-down)
  521. (picture-insert picture-rectangle-ctr 1)
  522. (picture-insert picture-rectangle-v (- bottom (picture-current-line)))
  523. (picture-movement-left)
  524. (picture-insert picture-rectangle-cbr 1)
  525. (picture-insert picture-rectangle-h (- picture-desired-column left))
  526. (picture-movement-up)
  527. (picture-insert picture-rectangle-cbl 1)
  528. (picture-insert picture-rectangle-v (- (picture-current-line) top))
  529. (picture-set-motion pvs phs)
  530. (goto-char (point-min))
  531. (forward-line (1- sl))
  532. (move-to-column sc t)))
  533. ;; Picture Keymap, entry and exit points.
  534. (defalias 'picture-delete-char 'delete-char)
  535. (defvar picture-mode-map nil)
  536. (defun picture-substitute (oldfun newfun)
  537. (define-key picture-mode-map (vector 'remap oldfun) newfun))
  538. (if (not picture-mode-map)
  539. (progn
  540. (setq picture-mode-map (make-keymap))
  541. (picture-substitute 'self-insert-command 'picture-self-insert)
  542. (picture-substitute 'completion-separator-self-insert-command
  543. 'picture-self-insert)
  544. (picture-substitute 'completion-separator-self-insert-autofilling
  545. 'picture-self-insert)
  546. (picture-substitute 'forward-char 'picture-forward-column)
  547. (picture-substitute 'backward-char 'picture-backward-column)
  548. (picture-substitute 'delete-char 'picture-clear-column)
  549. ;; There are two possibilities for what is normally on DEL.
  550. (picture-substitute 'backward-delete-char-untabify 'picture-backward-clear-column)
  551. (picture-substitute 'delete-backward-char 'picture-backward-clear-column)
  552. (picture-substitute 'kill-line 'picture-clear-line)
  553. (picture-substitute 'open-line 'picture-open-line)
  554. (picture-substitute 'newline 'picture-newline)
  555. (picture-substitute 'newline-and-indent 'picture-duplicate-line)
  556. (picture-substitute 'next-line 'picture-move-down)
  557. (picture-substitute 'previous-line 'picture-move-up)
  558. (picture-substitute 'move-beginning-of-line 'picture-beginning-of-line)
  559. (picture-substitute 'move-end-of-line 'picture-end-of-line)
  560. (picture-substitute 'mouse-set-point 'picture-mouse-set-point)
  561. (define-key picture-mode-map "\C-c\C-d" 'picture-delete-char)
  562. (define-key picture-mode-map "\e\t" 'picture-toggle-tab-state)
  563. (define-key picture-mode-map "\t" 'picture-tab)
  564. (define-key picture-mode-map "\e\t" 'picture-tab-search)
  565. (define-key picture-mode-map "\C-c\t" 'picture-set-tab-stops)
  566. (define-key picture-mode-map "\C-c\C-k" 'picture-clear-rectangle)
  567. (define-key picture-mode-map "\C-c\C-w" 'picture-clear-rectangle-to-register)
  568. (define-key picture-mode-map "\C-c\C-y" 'picture-yank-rectangle)
  569. (define-key picture-mode-map "\C-c\C-x" 'picture-yank-rectangle-from-register)
  570. (define-key picture-mode-map "\C-c\C-r" 'picture-draw-rectangle)
  571. (define-key picture-mode-map "\C-c\C-c" 'picture-mode-exit)
  572. (define-key picture-mode-map "\C-c\C-f" 'picture-motion)
  573. (define-key picture-mode-map "\C-c\C-b" 'picture-motion-reverse)
  574. (define-key picture-mode-map "\C-c<" 'picture-movement-left)
  575. (define-key picture-mode-map "\C-c>" 'picture-movement-right)
  576. (define-key picture-mode-map "\C-c^" 'picture-movement-up)
  577. (define-key picture-mode-map "\C-c." 'picture-movement-down)
  578. (define-key picture-mode-map "\C-c`" 'picture-movement-nw)
  579. (define-key picture-mode-map "\C-c'" 'picture-movement-ne)
  580. (define-key picture-mode-map "\C-c/" 'picture-movement-sw)
  581. (define-key picture-mode-map "\C-c\\" 'picture-movement-se)
  582. (define-key picture-mode-map [(control ?c) left] 'picture-movement-left)
  583. (define-key picture-mode-map [(control ?c) right] 'picture-movement-right)
  584. (define-key picture-mode-map [(control ?c) up] 'picture-movement-up)
  585. (define-key picture-mode-map [(control ?c) down] 'picture-movement-down)
  586. (define-key picture-mode-map [(control ?c) home] 'picture-movement-nw)
  587. (define-key picture-mode-map [(control ?c) prior] 'picture-movement-ne)
  588. (define-key picture-mode-map [(control ?c) end] 'picture-movement-sw)
  589. (define-key picture-mode-map [(control ?c) next] 'picture-movement-se)))
  590. (defcustom picture-mode-hook nil
  591. "If non-nil, its value is called on entry to Picture mode.
  592. Picture mode is invoked by the command \\[picture-mode]."
  593. :type 'hook
  594. :group 'picture)
  595. (defvar picture-mode-old-local-map)
  596. (defvar picture-mode-old-mode-name)
  597. (defvar picture-mode-old-major-mode)
  598. (defvar picture-mode-old-truncate-lines)
  599. ;;;###autoload
  600. (defun picture-mode ()
  601. "Switch to Picture mode, in which a quarter-plane screen model is used.
  602. \\<picture-mode-map>
  603. Printing characters replace instead of inserting themselves with motion
  604. afterwards settable by these commands:
  605. Move left after insertion: \\[picture-movement-left]
  606. Move right after insertion: \\[picture-movement-right]
  607. Move up after insertion: \\[picture-movement-up]
  608. Move down after insertion: \\[picture-movement-down]
  609. Move northwest (nw) after insertion: \\[picture-movement-nw]
  610. Move northeast (ne) after insertion: \\[picture-movement-ne]
  611. Move southwest (sw) after insertion: \\[picture-movement-sw]
  612. Move southeast (se) after insertion: \\[picture-movement-se]
  613. Move westnorthwest (wnw) after insertion: C-u \\[picture-movement-nw]
  614. Move eastnortheast (ene) after insertion: C-u \\[picture-movement-ne]
  615. Move westsouthwest (wsw) after insertion: C-u \\[picture-movement-sw]
  616. Move eastsoutheast (ese) after insertion: C-u \\[picture-movement-se]
  617. The current direction is displayed in the mode line. The initial
  618. direction is right. Whitespace is inserted and tabs are changed to
  619. spaces when required by movement. You can move around in the buffer
  620. with these commands:
  621. Move vertically to SAME column in previous line: \\[picture-move-down]
  622. Move vertically to SAME column in next line: \\[picture-move-up]
  623. Move to column following last
  624. non-whitespace character: \\[picture-end-of-line]
  625. Move right, inserting spaces if required: \\[picture-forward-column]
  626. Move left changing tabs to spaces if required: \\[picture-backward-column]
  627. Move in direction of current picture motion: \\[picture-motion]
  628. Move opposite to current picture motion: \\[picture-motion-reverse]
  629. Move to beginning of next line: \\[next-line]
  630. You can edit tabular text with these commands:
  631. Move to column beneath (or at) next interesting
  632. character (see variable `picture-tab-chars'): \\[picture-tab-search]
  633. Move to next stop in tab stop list: \\[picture-tab]
  634. Set tab stops according to context of this line: \\[picture-set-tab-stops]
  635. (With ARG, resets tab stops to default value.)
  636. Change the tab stop list: \\[edit-tab-stops]
  637. You can manipulate text with these commands:
  638. Clear ARG columns after point without moving: \\[picture-clear-column]
  639. Delete char at point: \\[picture-delete-char]
  640. Clear ARG columns backward: \\[picture-backward-clear-column]
  641. Clear ARG lines, advancing over them: \\[picture-clear-line]
  642. (the cleared text is saved in the kill ring)
  643. Open blank line(s) beneath current line: \\[picture-open-line]
  644. You can manipulate rectangles with these commands:
  645. Clear a rectangle and save it: \\[picture-clear-rectangle]
  646. Clear a rectangle, saving in a named register: \\[picture-clear-rectangle-to-register]
  647. Insert currently saved rectangle at point: \\[picture-yank-rectangle]
  648. Insert rectangle from named register: \\[picture-yank-rectangle-from-register]
  649. Draw a rectangular box around mark and point: \\[picture-draw-rectangle]
  650. Copies a rectangle to a register: \\[copy-rectangle-to-register]
  651. Undo effects of rectangle overlay commands: \\[undo]
  652. You can return to the previous mode with \\[picture-mode-exit], which
  653. also strips trailing whitespace from every line. Stripping is suppressed
  654. by supplying an argument.
  655. Entry to this mode calls the value of `picture-mode-hook' if non-nil.
  656. Note that Picture mode commands will work outside of Picture mode, but
  657. they are not by default assigned to keys."
  658. (interactive)
  659. (if (eq major-mode 'picture-mode)
  660. (error "You are already editing a picture")
  661. (set (make-local-variable 'picture-mode-old-local-map) (current-local-map))
  662. (use-local-map picture-mode-map)
  663. (set (make-local-variable 'picture-mode-old-mode-name) mode-name)
  664. (set (make-local-variable 'picture-mode-old-major-mode) major-mode)
  665. (setq major-mode 'picture-mode)
  666. (set (make-local-variable 'picture-killed-rectangle) nil)
  667. (set (make-local-variable 'tab-stop-list) (default-value 'tab-stop-list))
  668. (set (make-local-variable 'picture-tab-chars)
  669. (default-value 'picture-tab-chars))
  670. (make-local-variable 'picture-vertical-step)
  671. (make-local-variable 'picture-horizontal-step)
  672. (set (make-local-variable 'picture-mode-old-truncate-lines) truncate-lines)
  673. (setq truncate-lines t)
  674. (picture-set-motion 0 1)
  675. ;; edit-picture-hook is what we used to run, picture-mode-hook is in doc.
  676. (run-hooks 'edit-picture-hook 'picture-mode-hook)
  677. (message "Type %s in this buffer to return it to %s mode."
  678. (substitute-command-keys "\\[picture-mode-exit]")
  679. picture-mode-old-mode-name)))
  680. ;;;###autoload
  681. (defalias 'edit-picture 'picture-mode)
  682. (defun picture-mode-exit (&optional nostrip)
  683. "Undo `picture-mode' and return to previous major mode.
  684. With no argument, strip whitespace from end of every line in Picture buffer;
  685. otherwise, just return to previous mode.
  686. Runs `picture-mode-exit-hook' at the end."
  687. (interactive "P")
  688. (if (not (eq major-mode 'picture-mode))
  689. (error "You aren't editing a Picture")
  690. (if (not nostrip) (delete-trailing-whitespace))
  691. (setq mode-name picture-mode-old-mode-name)
  692. (use-local-map picture-mode-old-local-map)
  693. (setq major-mode picture-mode-old-major-mode)
  694. (kill-local-variable 'tab-stop-list)
  695. (setq truncate-lines picture-mode-old-truncate-lines)
  696. (force-mode-line-update)
  697. (run-hooks 'picture-mode-exit-hook)))
  698. (provide 'picture)
  699. ;;; picture.el ends here