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