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