*** empty log message ***
[bpt/emacs.git] / lisp / emulation / edt.el
1 ;;; edt.el --- EDT emulation in Emacs
2
3 ;; Author: Mike Clarkson <mike@yetti.UUCP>
4 ;; Maintainer: FSF
5 ;; Created: 27 Aug 1986
6 ;; Last-Modified: 09 May 1991
7 ;; Keywords: emulations
8
9 ;; Copyright (C) 1986 Free Software Foundation, Inc.
10 ;; It started from public domain code by Mike Clarkson
11 ;; but has been greatly altered.
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28
29 ;;; Commentary:
30
31 ;; Here's my EDT emulation for GNU Emacs that is based on the EDT emulation
32 ;; for Gosling's Emacs sent out on the net a couple of years ago by Lynn Olson
33 ;; at Tektronics. This emulation was widely distributed as the file edt.ml
34 ;; in the maclib directory of most Emacs distributions.
35 ;;
36 ;; I will gladly take all criticisms and complaints to heart, and will fix what
37 ;; bugs I can find. As this is my first Emacs Lisp hack, you may have to root
38 ;; out a few nasties hidden in the code. Please let me know if you find any
39 ;; (sorry, no rewards :-). I would also be interested if there are better,
40 ;; cleaner, faster ways of doing some of the things that I have done.
41 ;;
42 ;; You must understand some design considerations that I had in mind.
43 ;; The intention was not really to "emulate" EDT, but rather to take advantage
44 ;; of the years of EDT experience that had accumulated in my right hand,
45 ;; while at the same time taking advantage of EMACS.
46 ;;
47 ;; Some major differences are:
48 ;;
49 ;; HELP is describe-key;
50 ;; GOLD/HELP is describe-function;
51 ;; FIND is isearch-forward/backward;
52 ;; GOLD/HELP is occur-menu, which finds all instances of a search string;
53 ;; ENTER is other-window;
54 ;; SUBS is subprocess-command. Note that you have to change this
55 ;; to `shell' if you are running Un*x;
56 ;; PAGE is next-paragraph, because that's more useful than page.
57 ;; SPECINS is copy-to-killring;
58 ;; GOLD/GOLD is mark-section-wisely, which is my command to mark the
59 ;; section in a manner consistent with the major-mode. It
60 ;; uses mark-defun for emacs-lisp, lisp, mark-c-function for C,
61 ;; and mark-paragraph for other modes.
62 ;;
63 ;;
64 ;; Some subtle differences are:
65 ;;
66 ;; APPEND is append-to-buffer. One doesn't append to the kill ring
67 ;; much and SPECINS is now copy-to-killring;
68 ;; REPLACE is replace-regexp;
69 ;; FILL is fill-region-wisely, which uses indent-region for C, lisp
70 ;; emacs-lisp, and fill-region for others. It asks if you
71 ;; really want to fill-region in TeX-mode, because I find this
72 ;; to be very dangerous.
73 ;; CHNGCASE is case-flip for the character under the cursor only.
74 ;; I felt that case-flip region is unlikely, as usually you
75 ;; upcase-region or downcase region. Also, unlike EDT it
76 ;; is independent of the direction you are going, as that
77 ;; drives me nuts.
78 ;;
79 ;; I use Emacs definition of what a word is. This is considerably different
80 ;; from what EDT thinks a word is. This is not good for dyed-in-the-wool EDT
81 ;; fans, but is probably preferable for experienced Emacs users. My assumption
82 ;; is that the former are a dying breed now that GNU Emacs has made it to VMS,
83 ;; but let me know how you feel. Also, when you undelete a word it leave the
84 ;; point at the end of the undeleted text, rather than the beginning. I might
85 ;; change this as I'm not sure if I like this or not. I'm also not sure if I
86 ;; want it to set the mark each time you delete a character or word.
87 ;;
88 ;; Backspace does not invoke beginning-of-line, because ^H is the help prefix,
89 ;; and I felt it should be left as such. You can change this if you like.
90 ;;
91 ;; The ADVANCE and BACKUP keys do not work as terminators for forward or
92 ;; backward searches. In Emacs, all search strings are terminated by return.
93 ;; The searches will however go forward or backward depending on your current
94 ;; direction. Also, when you change directions, the mode line will not be
95 ;; updated immediately, but only when you next execute an emacs function.
96 ;; Personally, I consider this to be a bug, not a feature.
97 ;;
98 ;; This should also work with VT-2xx's, though I haven't tested it extensively
99 ;; on those terminals. It assumes that the CSI-map of vt_200.el has been
100 ;; defined.
101 ;;
102 ;; There are also a whole bunch of GOLD letter, and GOLD character bindings:
103 ;; look at edtdoc.el for them, or better still, look at the edt.el lisp code,
104 ;; because after all, in the true Lisp tradition, the source code is *assumed*
105 ;; to be self-documenting :-)
106 ;;
107 ;; Mike Clarkson, ...!allegra \ BITNET: mike@YUYETTI or
108 ;; CRESS, York University, ...!decvax \ SYMALG@YUSOL
109 ;; 4700 Keele Street, ...!ihnp4 > !utzoo!yetti!mike
110 ;; North York, Ontario, ...!linus /
111 ;; CANADA M3J 1P3. ...!watmath / Phone: +1 (416) 736-2100 x 7767
112 ;;
113 ;; Note that I am not on ARPA, and must gateway any ARPA mail through BITNET or
114 ;; UUCP. If you have a UUCP or BITNET address please use it for communication
115 ;; so that I can reach you directly. If you have both, the BITNET address
116 ;; is preferred.
117
118 ;;; Code:
119
120 (require 'keypad)
121
122 (defvar edt-last-deleted-lines ""
123 "Last text deleted by an EDT emulation `line-delete' command.")
124 (defvar edt-last-deleted-words ""
125 "Last text deleted by an EDT emulation `word-delete' command.")
126 (defvar edt-last-deleted-chars ""
127 "Last text deleted by an EDT emulation `character-delete' command.")
128
129 (defun delete-current-line (num)
130 "Delete one or specified number of lines after point.
131 This includes the newline character at the end of each line.
132 They are saved for the EDT `undelete-lines' command."
133 (interactive "p")
134 (let ((beg (point)))
135 (forward-line num)
136 (if (not (eq (preceding-char) ?\n))
137 (insert "\n"))
138 (setq edt-last-deleted-lines
139 (buffer-substring beg (point)))
140 (delete-region beg (point))))
141
142 (defun delete-to-eol (num)
143 "Delete text up to end of line.
144 With argument, delete up to to Nth line-end past point.
145 They are saved for the EDT `undelete-lines' command."
146 (interactive "p")
147 (let ((beg (point)))
148 (forward-char 1)
149 (end-of-line num)
150 (setq edt-last-deleted-lines
151 (buffer-substring beg (point)))
152 (delete-region beg (point))))
153
154 (defun delete-current-word (num)
155 "Delete one or specified number of words after point.
156 They are saved for the EDT `undelete-words' command."
157 (interactive "p")
158 (let ((beg (point)))
159 (forward-word num)
160 (setq edt-last-deleted-words
161 (buffer-substring beg (point)))
162 (delete-region beg (point))))
163
164 (defun edt-delete-previous-word (num)
165 "Delete one or specified number of words before point.
166 They are saved for the EDT `undelete-words' command."
167 (interactive "p")
168 (let ((beg (point)))
169 (forward-word (- num))
170 (setq edt-last-deleted-words
171 (buffer-substring (point) beg))
172 (delete-region beg (point))))
173
174 (defun delete-current-char (num)
175 "Delete one or specified number of characters after point.
176 They are saved for the EDT `undelete-chars' command."
177 (interactive "p")
178 (setq edt-last-deleted-chars
179 (buffer-substring (point) (min (point-max) (+ (point) num))))
180 (delete-region (point) (min (point-max) (+ (point) num))))
181
182 (defun delete-previous-char (num)
183 "Delete one or specified number of characters before point.
184 They are saved for the EDT `undelete-chars' command."
185 (interactive "p")
186 (setq edt-last-deleted-chars
187 (buffer-substring (max (point-min) (- (point) num)) (point)))
188 (delete-region (max (point-min) (- (point) num)) (point)))
189
190 (defun undelete-lines ()
191 "Yank lines deleted by last EDT `line-delete' command."
192 (interactive)
193 (insert edt-last-deleted-lines))
194
195 (defun undelete-words ()
196 "Yank words deleted by last EDT `word-delete' command."
197 (interactive)
198 (insert edt-last-deleted-words))
199
200 (defun undelete-chars ()
201 "Yank characters deleted by last EDT `character-delete' command."
202 (interactive)
203 (insert edt-last-deleted-chars))
204
205 (defun next-end-of-line (num)
206 "Move to end of line; if at end, move to end of next line.
207 Accepts a prefix argument for the number of lines to move."
208 (interactive "p")
209 (forward-char)
210 (end-of-line num))
211
212 (defun previous-end-of-line (num)
213 "Move EOL upward.
214 Accepts a prefix argument for the number of lines to move."
215 (interactive "p")
216 (end-of-line (- 1 num)))
217
218 (defun forward-to-word (num)
219 "Move to next word-beginning, or to Nth following word-beginning."
220 (interactive "p")
221 (forward-word (1+ num))
222 (forward-word -1))
223
224 (defun backward-to-word (num)
225 "Move back to word-end, or to Nth word-end seen."
226 (interactive "p")
227 (forward-word (- (1+ num)))
228 (forward-word 1))
229
230 (defun backward-line (num)
231 "Move point to start of previous line.
232 Prefix argument serves as repeat-count."
233 (interactive "p")
234 (forward-line (- num)))
235
236 (defun scroll-window-down (num)
237 "Scroll the display down a window-full.
238 Accepts a prefix argument for the number of window-fulls to scroll."
239 (interactive "p")
240 (scroll-down (- (* (window-height) num) 2)))
241
242 (defun scroll-window-up (num)
243 "Scroll the display up a window-full.
244 Accepts a prefix argument for the number of window-fulls to scroll."
245 (interactive "p")
246 (scroll-up (- (* (window-height) num) 2)))
247
248 (defun next-paragraph (num)
249 "Move to beginning of the next indented paragraph.
250 Accepts a prefix argument for the number of paragraphs."
251 (interactive "p")
252 (while (> num 0)
253 (next-line 1)
254 (forward-paragraph)
255 (previous-line 1)
256 (if (eolp) (next-line 1))
257 (setq num (1- num))))
258
259 (defun previous-paragraph (num)
260 "Move to beginning of previous indented paragraph.
261 Accepts a prefix argument for the number of paragraphs."
262 (interactive "p")
263 (while (> num 0)
264 (backward-paragraph)
265 (previous-line 1)
266 (if (eolp) (next-line 1))
267 (setq num (1- num))))
268
269 (defun move-to-beginning ()
270 "Move cursor to the beginning of buffer, but don't set the mark."
271 (interactive)
272 (goto-char (point-min)))
273
274 (defun move-to-end ()
275 "Move cursor to the end of buffer, but don't set the mark."
276 (interactive)
277 (goto-char (point-max)))
278
279 (defun goto-percent (perc)
280 "Move point to ARG percentage of the buffer."
281 (interactive "NGoto-percentage: ")
282 (if (or (> perc 100) (< perc 0))
283 (error "Percentage %d out of range 0 < percent < 100" perc)
284 (goto-char (/ (* (point-max) perc) 100))))
285
286 (defun update-mode-line ()
287 "Ensure mode-line reflects all changes."
288 (set-buffer-modified-p (buffer-modified-p))
289 (sit-for 0))
290
291 (defun advance-direction ()
292 "Set EDT Advance mode so keypad commands move forward."
293 (interactive)
294 (setq edt-direction-string " ADVANCE")
295 (define-key function-keymap "\C-c" 'isearch-forward) ; PF3
296 (define-key function-keymap "8" 'scroll-window-up) ; "8"
297 (define-key function-keymap "7" 'next-paragraph) ; "7"
298 (define-key function-keymap "1" 'forward-to-word) ; "1"
299 (define-key function-keymap "2" 'next-end-of-line) ; "2"
300 (define-key function-keymap "3" 'forward-char) ; "3"
301 (define-key function-keymap "0" 'forward-line) ; "0"
302 (update-mode-line))
303
304 (defun backup-direction ()
305 "Set EDT Backup mode so keypad commands move backward."
306 (interactive)
307 (setq edt-direction-string " BACKUP")
308 (define-key function-keymap "\C-c" 'isearch-backward) ; PF3
309 (define-key function-keymap "8" 'scroll-window-down) ; "8"
310 (define-key function-keymap "7" 'previous-paragraph) ; "7"
311 (define-key function-keymap "1" 'backward-to-word) ; "1"
312 (define-key function-keymap "2" 'previous-end-of-line) ; "2"
313 (define-key function-keymap "3" 'backward-char) ; "3"
314 (define-key function-keymap "0" 'backward-line) ; "0"
315 (update-mode-line))
316
317 (defun edt-beginning-of-window ()
318 "Home cursor to top of window."
319 (interactive)
320 (move-to-window-line 0))
321
322 (defun edt-line-to-bottom-of-window ()
323 "Move the current line to the top of the window."
324 (interactive)
325 (recenter -1))
326
327 (defun edt-line-to-top-of-window ()
328 "Move the current line to the top of the window."
329 (interactive)
330 (recenter 0))
331
332 (defun case-flip-character (num)
333 "Change the case of the character under the cursor.
334 Accepts a prefix argument of the number of characters to invert."
335 (interactive "p")
336 (while (> num 0)
337 (funcall (if (<= ?a (following-char))
338 'upcase-region 'downcase-region)
339 (point) (1+ (point)))
340 (forward-char 1)
341 (setq num (1- num))))
342
343 (defun indent-or-fill-region ()
344 "Fill region in text modes, indent region in programming language modes."
345 (interactive)
346 (if (string= paragraph-start "^$\\|^\f")
347 (indent-region (point) (mark) nil)
348 (fill-region (point) (mark))))
349
350 (defun mark-section-wisely ()
351 "Mark the section in a manner consistent with the major-mode.
352 Uses mark-defun for emacs-lisp, lisp,
353 mark-c-function for C,
354 and mark-paragraph for other modes."
355 (interactive)
356 (cond ((eq major-mode 'emacs-lisp-mode)
357 (mark-defun))
358 ((eq major-mode 'lisp-mode)
359 (mark-defun))
360 ((eq major-mode 'c-mode)
361 (mark-c-function))
362 (t (mark-paragraph))))
363
364 ;;; Key Bindings
365 ;;;###autoload
366 (defun edt-emulation-on ()
367 "Emulate DEC's EDT editor.
368 Note that many keys are rebound; including nearly all keypad keys.
369 Use \\[edt-emulation-off] to undo all rebindings except the keypad keys.
370 Note that this function does not work if called directly from the .emacs file.
371 Instead, the .emacs file should do \"(setq term-setup-hook 'edt-emulation-on)\"
372 Then this function will be called at the time when it will work."
373 (interactive)
374 (advance-direction)
375 (edt-bind-gold-keypad) ;Must do this *after* $TERM.el is loaded
376 (setq edt-mode-old-c-\\ (lookup-key global-map "\C-\\"))
377 (global-set-key "\C-\\" 'quoted-insert)
378 (setq edt-mode-old-delete (lookup-key global-map "\177"))
379 (global-set-key "\177" 'delete-previous-char) ;"Delete"
380 (setq edt-mode-old-lisp-delete (lookup-key emacs-lisp-mode-map "\177"))
381 (define-key emacs-lisp-mode-map "\177" 'delete-previous-char) ;"Delete"
382 (define-key lisp-mode-map "\177" 'delete-previous-char) ;"Delete"
383 (setq edt-mode-old-linefeed (lookup-key global-map "\C-j"))
384 (global-set-key "\C-j" 'edt-delete-previous-word) ;"LineFeed"
385 (define-key esc-map "?" 'apropos)) ;"<ESC>?"
386
387 (defun edt-emulation-off ()
388 "Return from EDT emulation to normal Emacs key bindings.
389 The keys redefined by \\[edt-emulation-on] are given their old definitions."
390 (interactive)
391 (setq edt-direction-string nil)
392 (global-set-key "\C-\\" edt-mode-old-c-\\)
393 (global-set-key "\177" edt-mode-old-delete) ;"Delete"
394 (define-key emacs-lisp-mode-map "\177" edt-mode-old-lisp-delete) ;"Delete"
395 (define-key lisp-mode-map "\177" edt-mode-old-lisp-delete) ;"Delete"
396 (global-set-key "\C-j" edt-mode-old-linefeed)) ;"LineFeed"
397
398 (define-key function-keymap "u" 'previous-line) ;Up arrow
399 (define-key function-keymap "d" 'next-line) ;down arrow
400 (define-key function-keymap "l" 'backward-char) ;right arrow
401 (define-key function-keymap "r" 'forward-char) ;left arrow
402 (define-key function-keymap "h" 'edt-beginning-of-window) ;home
403 (define-key function-keymap "\C-b" 'describe-key) ;PF2
404 (define-key function-keymap "\C-d" 'delete-current-line);PF4
405 (define-key function-keymap "9" 'append-to-buffer) ;9 keypad key, etc.
406 (define-key function-keymap "-" 'delete-current-word)
407 (define-key function-keymap "4" 'advance-direction)
408 (define-key function-keymap "5" 'backup-direction)
409 (define-key function-keymap "6" 'kill-region)
410 (define-key function-keymap "," 'delete-current-char)
411 (define-key function-keymap "." 'set-mark-command)
412 (define-key function-keymap "e" 'other-window) ;enter key
413 (define-key function-keymap "\C-a" 'GOLD-prefix) ;PF1 ("gold")
414
415 (fset 'GOLD-prefix GOLD-map)
416
417 (defvar GOLD-map (make-keymap)
418 "`GOLD-map' maps the function keys on the VT100 keyboard preceeded
419 by the PF1 key. GOLD is the ASCII the 7-bit escape sequence <ESC>OP.")
420
421 (defun define-keypad-key (keymap function-keymap-slot definition)
422 (let ((function-key-sequence (function-key-sequence function-keymap-slot)))
423 (if function-key-sequence
424 (define-key keymap function-key-sequence definition))))
425
426 ;;Bind GOLD/Keyboard keys
427
428 (define-key GOLD-map "\C-g" 'keyboard-quit) ; just for safety
429 (define-key GOLD-map "\177" 'delete-window) ;"Delete"
430 (define-key GOLD-map "\C-h" 'delete-other-windows) ;"BackSpace"
431 (define-key GOLD-map "\C-m" 'newline-and-indent) ;"Return"
432 (define-key GOLD-map " " 'undo) ;"Spacebar"
433 (define-key GOLD-map "%" 'goto-percent) ; "%"
434 (define-key GOLD-map "=" 'goto-line) ; "="
435 (define-key GOLD-map "`" 'what-line) ; "`"
436 (define-key GOLD-map "\C-\\" 'split-window-vertically) ; "Control-\"
437
438 ; GOLD letter combinations:
439 (define-key GOLD-map "b" 'buffer-menu) ; "b"
440 (define-key GOLD-map "B" 'buffer-menu) ; "B"
441 (define-key GOLD-map "d" 'delete-window) ; "d"
442 (define-key GOLD-map "D" 'delete-window) ; "D"
443 (define-key GOLD-map "e" 'compile) ; "e"
444 (define-key GOLD-map "E" 'compile) ; "E"
445 (define-key GOLD-map "i" 'insert-file) ; "i"
446 (define-key GOLD-map "I" 'insert-file) ; "I"
447 (define-key GOLD-map "l" 'goto-line) ; "l"
448 (define-key GOLD-map "L" 'goto-line) ; "L"
449 (define-key GOLD-map "m" 'save-some-buffers) ; "m"
450 (define-key GOLD-map "M" 'save-some-buffers) ; "m"
451 (define-key GOLD-map "n" 'next-error) ; "n"
452 (define-key GOLD-map "N" 'next-error) ; "N"
453 (define-key GOLD-map "o" 'switch-to-buffer-other-window) ; "o"
454 (define-key GOLD-map "O" 'switch-to-buffer-other-window) ; "O"
455 (define-key GOLD-map "r" 'revert-file) ; "r"
456 (define-key GOLD-map "r" 'revert-file) ; "R"
457 (define-key GOLD-map "s" 'save-buffer) ; "s"
458 (define-key GOLD-map "S" 'save-buffer) ; "S"
459 (define-key GOLD-map "v" 'find-file-other-window) ; "v"
460 (define-key GOLD-map "V" 'find-file-other-window) ; "V"
461 (define-key GOLD-map "w" 'write-file) ; "w"
462 (define-key GOLD-map "w" 'write-file) ; "W"
463 ;(define-key GOLD-map "z" 'shrink-window) ; "z"
464 ;(define-key GOLD-map "Z" 'shrink-window) ; "z"
465
466 ;Bind GOLD/Keypad keys
467 (defun edt-bind-gold-keypad ()
468 (define-keypad-key GOLD-map ?u 'edt-line-to-top-of-window) ;"up-arrow"
469 (define-keypad-key GOLD-map ?d 'edt-line-to-bottom-of-window) ;"down-arrow"
470 (define-keypad-key GOLD-map ?l 'backward-sentence) ;"left-arrow"
471 (define-keypad-key GOLD-map ?r 'forward-sentence) ;"right-arrow"
472 (define-keypad-key GOLD-map ?\C-a 'mark-section-wisely) ;Gold "PF1"
473 (define-keypad-key GOLD-map ?\C-b 'describe-function) ;Help "PF2"
474 (define-keypad-key GOLD-map ?\C-c 'occur) ;Find "PF3"
475 (define-keypad-key GOLD-map ?\C-d 'undelete-lines) ;Und Line "PF4"
476 (define-keypad-key GOLD-map ?0 'open-line) ;Open L "0"
477 (define-keypad-key GOLD-map ?1 'case-flip-character) ;Chgcase "1"
478 (define-keypad-key GOLD-map ?2 'delete-to-eol) ;Del EOL "2"
479 (define-keypad-key GOLD-map ?3 'copy-region-as-kill) ;Copy "3"
480 (define-keypad-key GOLD-map ?4 'move-to-end) ;Bottom "4"
481 (define-keypad-key GOLD-map ?5 'move-to-beginning) ;Top "5"
482 (define-keypad-key GOLD-map ?6 'yank) ;Paste "6"
483 (define-keypad-key GOLD-map ?7 'execute-extended-command) ;Command "7"
484 (define-keypad-key GOLD-map ?8 'indent-or-fill-region) ;Fill "8"
485 (define-keypad-key GOLD-map ?9 'replace-regexp) ;Replace "9"
486 (define-keypad-key GOLD-map ?- 'undelete-words) ;UND word "-"
487 (define-keypad-key GOLD-map ?, 'undelete-chars) ;UND Char ","
488 (define-keypad-key GOLD-map ?. 'redraw-display) ;Reset Window "."
489 (define-keypad-key GOLD-map ?e 'shell-command)) ;"ENTER"
490
491 ;; Make direction of motion show in mode line
492 ;; while EDT emulation is turned on.
493 ;; Note that the keypad is always turned on when in Emacs.
494
495 (or (assq 'edt-direction-string minor-mode-alist)
496 (setq minor-mode-alist (cons '(edt-direction-string edt-direction-string)
497 minor-mode-alist)))
498
499 ;;; edt.el ends here