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