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