]> code.delx.au - gnu-emacs/blob - lisp/emulation/tpu-edt.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-69
[gnu-emacs] / lisp / emulation / tpu-edt.el
1 ;;; tpu-edt.el --- Emacs emulating TPU emulating EDT
2
3 ;; Copyright (C) 1993, 1994, 1995, 2000, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Rob Riepel <riepel@networking.stanford.edu>
6 ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
7 ;; Version: 4.5
8 ;; Keywords: emulations
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;; TPU-edt is based on tpu.el by Jeff Kowalski and Bob Covey.
28
29 ;;; Commentary:
30
31 ;; %% TPU-edt -- Emacs emulating TPU emulating EDT
32
33 ;; %% Contents
34
35 ;; % Introduction
36 ;; % Differences Between TPU-edt and DEC TPU/edt
37 ;; % Starting TPU-edt
38 ;; % Customizing TPU-edt using the Emacs Initialization File
39 ;; % Regular Expressions in TPU-edt
40
41
42 ;; %% Introduction
43
44 ;; TPU-edt emulates the popular DEC VMS editor EDT (actually, it emulates
45 ;; DEC TPU's EDT emulation, hence the name TPU-edt). TPU-edt features the
46 ;; following TPU/edt functionality:
47
48 ;; . EDT keypad
49 ;; . On-line help
50 ;; . Repeat counts
51 ;; . Scroll margins
52 ;; . Learn sequences
53 ;; . Free cursor mode
54 ;; . Rectangular cut and paste
55 ;; . Multiple windows and buffers
56 ;; . TPU line-mode REPLACE command
57 ;; . Wild card search and substitution
58 ;; . Configurable through an initialization file
59 ;; . History recall of search strings, file names, and commands
60
61 ;; Please note that TPU-edt does NOT emulate TPU. It emulates TPU's EDT
62 ;; emulation. Very few TPU line-mode commands are supported.
63
64 ;; TPU-edt, like its VMS cousin, works on VT-series terminals with DEC
65 ;; style keyboards. VT terminal emulators, including xterm with the
66 ;; appropriate key translations, work just fine too.
67
68 ;; TPU-edt works with X-windows. This is accomplished through a TPU-edt X
69 ;; key map. The TPU-edt module tpu-mapper creates this map and stores it
70 ;; in a file. Tpu-mapper will be run automatically the first time you
71 ;; invoke the X-windows version of emacs, or you can run it by hand. See
72 ;; the commentary in tpu-mapper.el for details.
73
74
75 ;; %% Differences Between TPU-edt and DEC TPU/edt
76
77 ;; In some cases, Emacs doesn't support text highlighting, so selected
78 ;; regions are not shown in inverse video. Emacs uses the concept of "the
79 ;; mark". The mark is set at one end of a selected region; the cursor is
80 ;; at the other. In cases where the selected region cannot be shown in
81 ;; inverse video an at sign (@) appears in the mode line when mark is set.
82 ;; The native emacs command ^X^X (Control-X twice) exchanges the cursor
83 ;; with the mark; this provides a handy way to find the location of the
84 ;; mark.
85
86 ;; In TPU the cursor can be either bound or free. Bound means the cursor
87 ;; cannot wander outside the text of the file being edited. Free means
88 ;; the arrow keys can move the cursor past the ends of lines. Free is the
89 ;; default mode in TPU; bound is the only mode in EDT. Bound is the only
90 ;; mode in the base version of TPU-edt; optional extensions add an
91 ;; approximation of free mode, see the commentary in tpu-extras.el for
92 ;; details.
93
94 ;; Like TPU, emacs uses multiple buffers. Some buffers are used to hold
95 ;; files you are editing; other "internal" buffers are used for emacs' own
96 ;; purposes (like showing you help). Here are some commands for dealing
97 ;; with buffers.
98
99 ;; Gold-B moves to next buffer, including internal buffers
100 ;; Gold-N moves to next buffer containing a file
101 ;; Gold-M brings up a buffer menu (like TPU "show buffers")
102
103 ;; Emacs is very fond of throwing up new windows. Dealing with all these
104 ;; windows can be a little confusing at first, so here are a few commands
105 ;; to that may help:
106
107 ;; Gold-Next_Scr moves to the next window on the screen
108 ;; Gold-Prev_Scr moves to the previous window on the screen
109 ;; Gold-TAB also moves to the next window on the screen
110
111 ;; Control-x 1 deletes all but the current window
112 ;; Control-x 0 deletes the current window
113
114 ;; Note that the buffers associated with deleted windows still exist!
115
116 ;; Like TPU, TPU-edt has a "command" function, invoked with Gold-KP7 or
117 ;; Do. Most of the commands available are emacs commands. Some TPU
118 ;; commands are available, they are: replace, exit, quit, include, and
119 ;; Get (unfortunately, "get" is an internal emacs function, so we are
120 ;; stuck with "Get" - to make life easier, Get is available as Gold-g).
121
122 ;; TPU-edt supports the recall of commands, file names, and search
123 ;; strings. The history of strings recalled differs slightly from
124 ;; TPU/edt, but it is still very convenient.
125
126 ;; Help is available! The traditional help keys (Help and PF2) display
127 ;; a small help file showing the default keypad layout, control key
128 ;; functions, and Gold key functions. Pressing any key inside of help
129 ;; splits the screen and prints a description of the function of the
130 ;; pressed key. Gold-PF2 invokes the native emacs help, with its
131 ;; zillions of options.
132
133 ;; Thanks to emacs, TPU-edt has some extensions that may make your life
134 ;; easier, or at least more interesting. For example, Gold-r toggles
135 ;; TPU-edt rectangular mode. In rectangular mode, Remove and Insert work
136 ;; on rectangles. Likewise, Gold-* toggles TPU-edt regular expression
137 ;; mode. In regular expression mode Find, Find Next, and the line-mode
138 ;; replace command work with regular expressions. [A regular expression
139 ;; is a pattern that denotes a set of strings; like VMS wildcards.]
140
141 ;; Emacs also gives TPU-edt the undo and occur functions. Undo does
142 ;; what it says; it undoes the last change. Multiple undos in a row
143 ;; undo multiple changes. For your convenience, undo is available on
144 ;; Gold-u. Occur shows all the lines containing a specific string in
145 ;; another window. Moving to that window, and typing ^C^C (Control-C
146 ;; twice) on a particular line moves you back to the original window
147 ;; at that line. Occur is on Gold-o.
148
149 ;; Finally, as you edit, remember that all the power of emacs is at
150 ;; your disposal. It really is a fantastic tool. You may even want to
151 ;; take some time and read the emacs tutorial; perhaps not to learn the
152 ;; native emacs key bindings, but to get a feel for all the things
153 ;; emacs can do for you. The emacs tutorial is available from the
154 ;; emacs help function: "Gold-PF2 t"
155
156
157 ;; %% Starting TPU-edt
158
159 ;; All you have to do to start TPU-edt, is turn it on. This can be
160 ;; done from the command line when running emacs.
161
162 ;; prompt> emacs -f tpu-edt
163
164 ;; If you've already started emacs, turn on TPU-edt using the tpu-edt
165 ;; command. First press `M-x' (that's usually `ESC' followed by `x')
166 ;; and type `tpu-edt' followed by a carriage return.
167
168 ;; If you like TPU-edt and want to use it all the time, you can start
169 ;; TPU-edt using the emacs initialization file, .emacs. Simply create
170 ;; a .emacs file in your home directory containing the line:
171
172 ;; (tpu-edt)
173
174 ;; That's all you need to do to start TPU-edt.
175
176
177 ;; %% Customizing TPU-edt using the Emacs Initialization File
178
179 ;; The following is a sample emacs initialization file. It shows how to
180 ;; invoke TPU-edt, and how to customize it.
181
182 ;; ; .emacs - a sample emacs initialization file
183
184 ;; ; Turn on TPU-edt
185 ;; (tpu-edt)
186
187 ;; ; Set scroll margins 10% (top) and 15% (bottom).
188 ;; (tpu-set-scroll-margins "10%" "15%")
189
190 ;; ; Load the vtxxx terminal control functions.
191 ;; (load "vt-control" t)
192
193 ;; ; TPU-edt treats words like EDT; here's how to add word separators.
194 ;; ; Note that backslash (\) and double quote (") are quoted with '\'.
195 ;; (tpu-add-word-separators "]\\[-_,.\"=+()'/*#:!&;$")
196
197 ;; ; Emacs is happy to save files without a final newline; other Unix
198 ;; ; programs hate that! Here we make sure that files end with newlines.
199 ;; (setq require-final-newline t)
200
201 ;; ; Emacs uses Control-s and Control-q. Problems can occur when using
202 ;; ; emacs on terminals that use these codes for flow control (Xon/Xoff
203 ;; ; flow control). These lines disable emacs' use of these characters.
204 ;; (global-unset-key "\C-s")
205 ;; (global-unset-key "\C-q")
206
207 ;; ; The emacs universal-argument function is very useful.
208 ;; ; This line maps universal-argument to Gold-PF1.
209 ;; (define-key GOLD-SS3-map "P" 'universal-argument) ; Gold-PF1
210
211 ;; ; Make KP7 move by paragraphs, instead of pages.
212 ;; (define-key SS3-map "w" 'tpu-paragraph) ; KP7
213
214 ;; ; Repeat the preceding mappings for X-windows.
215 ;; (cond
216 ;; (window-system
217 ;; (global-set-key [kp_7] 'tpu-paragraph) ; KP7
218 ;; (define-key GOLD-map [kp_f1] 'universal-argument))) ; GOLD-PF1
219
220 ;; ; Display the TPU-edt version.
221 ;; (tpu-version)
222
223
224 ;; %% Regular Expressions in TPU-edt
225
226 ;; Gold-* toggles TPU-edt regular expression mode. In regular expression
227 ;; mode, find, find next, replace, and substitute accept emacs regular
228 ;; expressions. A complete list of emacs regular expressions can be found
229 ;; using the emacs "info" command (it's somewhat like the VMS help
230 ;; command). Try the following sequence of commands:
231
232 ;; DO info <enter info mode>
233 ;; m emacs <select the "emacs" topic>
234 ;; m regexs <select the "regular expression" topic>
235
236 ;; Type "q" to quit out of info mode.
237
238 ;; There is a problem in regular expression mode when searching for empty
239 ;; strings, like beginning-of-line (^) and end-of-line ($). When searching
240 ;; for these strings, find-next may find the current string, instead of the
241 ;; next one. This can cause global replace and substitute commands to loop
242 ;; forever in the same location. For this reason, commands like
243
244 ;; replace "^" "> " <add "> " to beginning of line>
245 ;; replace "$" "00711" <add "00711" to end of line>
246
247 ;; may not work properly.
248
249 ;; Commands like those above are very useful for adding text to the
250 ;; beginning or end of lines. They might work on a line-by-line basis, but
251 ;; go into an infinite loop if the "all" response is specified. If the
252 ;; goal is to add a string to the beginning or end of a particular set of
253 ;; lines TPU-edt provides functions to do this.
254
255 ;; Gold-^ Add a string at BOL in region or buffer
256 ;; Gold-$ Add a string at EOL in region or buffer
257
258 ;; There is also a TPU-edt interface to the native emacs string replacement
259 ;; commands. Gold-/ invokes this command. It accepts regular expressions
260 ;; if TPU-edt is in regular expression mode. Given a repeat count, it will
261 ;; perform the replacement without prompting for confirmation.
262
263 ;; This command replaces empty strings correctly, however, it has its
264 ;; drawbacks. As a native emacs command, it has a different interface
265 ;; than the emulated TPU commands. Also, it works only in the forward
266 ;; direction, regardless of the current TPU-edt direction.
267
268 ;;; Todo/Bugs:
269
270 ;; We shouldn't use vt100 ESC sequences since it is uselessly fighting
271 ;; against function-key-map. Better use real key names.
272
273 ;;; Code:
274
275 ;; we use picture-mode functions
276 (require 'picture)
277
278 (defgroup tpu nil
279 "Emacs emulating TPU emulating EDT."
280 :prefix "tpu-"
281 :group 'emulations)
282
283
284 ;;;
285 ;;; Version Information
286 ;;;
287 (defconst tpu-version "4.5" "TPU-edt version number.")
288
289
290 ;;;
291 ;;; User Configurable Variables
292 ;;;
293 (defcustom tpu-have-ispell t
294 "*If non-nil (default), TPU-edt uses ispell for spell checking."
295 :type 'boolean
296 :group 'tpu)
297
298 (defcustom tpu-kill-buffers-silently nil
299 "*If non-nil, TPU-edt kills modified buffers without asking."
300 :type 'boolean
301 :group 'tpu)
302
303 (defcustom tpu-percent-scroll 75
304 "*Percentage of the screen to scroll for next/previous screen commands."
305 :type 'integer
306 :group 'tpu)
307
308 (defcustom tpu-pan-columns 16
309 "*Number of columns the tpu-pan functions scroll left or right."
310 :type 'integer
311 :group 'tpu)
312
313
314 ;;;
315 ;;; Emacs version identifiers - currently referenced by
316 ;;;
317 ;;; o tpu-mark o tpu-set-mark
318 ;;; o mode line section o tpu-load-xkeys
319 ;;;
320 (defconst tpu-lucid-emacs-p
321 (string-match "Lucid" emacs-version)
322 "Non-nil if we are running Lucid Emacs.")
323
324 ;;;
325 ;;; Global Keymaps
326 ;;;
327 (defvar CSI-map
328 (let ((map (make-sparse-keymap)))
329 (define-key map "A" 'tpu-previous-line) ; up
330 (define-key map "B" 'tpu-next-line) ; down
331 (define-key map "D" 'tpu-backward-char) ; left
332 (define-key map "C" 'tpu-forward-char) ; right
333
334 (define-key map "1~" 'tpu-search) ; Find
335 (define-key map "2~" 'tpu-paste) ; Insert Here
336 (define-key map "3~" 'tpu-cut) ; Remove
337 (define-key map "4~" 'tpu-select) ; Select
338 (define-key map "5~" 'tpu-scroll-window-down) ; Prev Screen
339 (define-key map "6~" 'tpu-scroll-window-up) ; Next Screen
340
341 (define-key map "11~" 'nil) ; F1
342 (define-key map "12~" 'nil) ; F2
343 (define-key map "13~" 'nil) ; F3
344 (define-key map "14~" 'nil) ; F4
345 (define-key map "15~" 'nil) ; F5
346 (define-key map "17~" 'nil) ; F6
347 (define-key map "18~" 'nil) ; F7
348 (define-key map "19~" 'nil) ; F8
349 (define-key map "20~" 'nil) ; F9
350 (define-key map "21~" 'tpu-exit) ; F10
351 (define-key map "23~" 'tpu-insert-escape) ; F11 (ESC)
352 (define-key map "24~" 'tpu-next-beginning-of-line) ; F12 (BS)
353 (define-key map "25~" 'tpu-delete-previous-word) ; F13 (LF)
354 (define-key map "26~" 'tpu-toggle-overwrite-mode) ; F14
355 (define-key map "28~" 'tpu-help) ; HELP
356 (define-key map "29~" 'execute-extended-command) ; DO
357 (define-key map "31~" 'tpu-goto-breadcrumb) ; F17
358 (define-key map "32~" 'nil) ; F18
359 (define-key map "33~" 'nil) ; F19
360 (define-key map "34~" 'nil) ; F20
361 map)
362 "Maps the CSI function keys on the VT100 keyboard.
363 CSI is DEC's name for the sequence <ESC>[.")
364
365 (defvar GOLD-CSI-map
366 (let ((map (make-sparse-keymap)))
367 (define-key map "A" 'tpu-move-to-beginning) ; up-arrow
368 (define-key map "B" 'tpu-move-to-end) ; down-arrow
369 (define-key map "C" 'end-of-line) ; right-arrow
370 (define-key map "D" 'beginning-of-line) ; left-arrow
371
372 (define-key map "1~" 'nil) ; Find
373 (define-key map "2~" 'nil) ; Insert Here
374 (define-key map "3~" 'tpu-store-text) ; Remove
375 (define-key map "4~" 'tpu-unselect) ; Select
376 (define-key map "5~" 'tpu-previous-window) ; Prev Screen
377 (define-key map "6~" 'tpu-next-window) ; Next Screen
378
379 (define-key map "11~" 'nil) ; F1
380 (define-key map "12~" 'nil) ; F2
381 (define-key map "13~" 'nil) ; F3
382 (define-key map "14~" 'nil) ; F4
383 (define-key map "16~" 'nil) ; F5
384 (define-key map "17~" 'nil) ; F6
385 (define-key map "18~" 'nil) ; F7
386 (define-key map "19~" 'nil) ; F8
387 (define-key map "20~" 'nil) ; F9
388 (define-key map "21~" 'nil) ; F10
389 (define-key map "23~" 'nil) ; F11
390 (define-key map "24~" 'nil) ; F12
391 (define-key map "25~" 'nil) ; F13
392 (define-key map "26~" 'nil) ; F14
393 (define-key map "28~" 'describe-bindings) ; HELP
394 (define-key map "29~" 'nil) ; DO
395 (define-key map "31~" 'tpu-drop-breadcrumb) ; F17
396 (define-key map "32~" 'nil) ; F18
397 (define-key map "33~" 'nil) ; F19
398 (define-key map "34~" 'nil) ; F20
399 map)
400 "Maps the function keys on the VT100 keyboard preceded by GOLD-CSI.")
401
402 (defvar GOLD-SS3-map
403 (let ((map (make-sparse-keymap)))
404 (define-key map "A" 'tpu-move-to-beginning) ; up-arrow
405 (define-key map "B" 'tpu-move-to-end) ; down-arrow
406 (define-key map "C" 'end-of-line) ; right-arrow
407 (define-key map "D" 'beginning-of-line) ; left-arrow
408
409 (define-key map "P" 'keyboard-quit) ; PF1
410 (define-key map "Q" 'help-for-help) ; PF2
411 (define-key map "R" 'tpu-search) ; PF3
412 (define-key map "S" 'tpu-undelete-lines) ; PF4
413 (define-key map "p" 'open-line) ; KP0
414 (define-key map "q" 'tpu-change-case) ; KP1
415 (define-key map "r" 'tpu-delete-to-eol) ; KP2
416 (define-key map "s" 'tpu-special-insert) ; KP3
417 (define-key map "t" 'tpu-move-to-end) ; KP4
418 (define-key map "u" 'tpu-move-to-beginning) ; KP5
419 (define-key map "v" 'tpu-paste) ; KP6
420 (define-key map "w" 'execute-extended-command) ; KP7
421 (define-key map "x" 'tpu-fill) ; KP8
422 (define-key map "y" 'tpu-replace) ; KP9
423 (define-key map "m" 'tpu-undelete-words) ; KP-
424 (define-key map "l" 'tpu-undelete-char) ; KP,
425 (define-key map "n" 'tpu-unselect) ; KP.
426 (define-key map "M" 'tpu-substitute) ; KPenter
427 map)
428 "Maps the function keys on the VT100 keyboard preceded by GOLD-SS3.")
429
430 (defvar GOLD-map
431 (let ((map (make-keymap)))
432 (define-key map "\e[" GOLD-CSI-map) ; GOLD-CSI map
433 (define-key map "\eO" GOLD-SS3-map) ; GOLD-SS3 map
434 ;;
435 (define-key map "\C-A" 'tpu-toggle-overwrite-mode) ; ^A
436 (define-key map "\C-B" 'nil) ; ^B
437 (define-key map "\C-C" 'nil) ; ^C
438 (define-key map "\C-D" 'nil) ; ^D
439 (define-key map "\C-E" 'nil) ; ^E
440 (define-key map "\C-F" 'set-visited-file-name) ; ^F
441 (define-key map "\C-g" 'keyboard-quit) ; safety first
442 (define-key map "\C-h" 'delete-other-windows) ; BS
443 (define-key map "\C-i" 'other-window) ; TAB
444 (define-key map "\C-J" 'nil) ; ^J
445 (define-key map "\C-K" 'tpu-define-macro-key) ; ^K
446 (define-key map "\C-l" 'downcase-region) ; ^L
447 (define-key map "\C-M" 'nil) ; ^M
448 (define-key map "\C-N" 'nil) ; ^N
449 (define-key map "\C-O" 'nil) ; ^O
450 (define-key map "\C-P" 'nil) ; ^P
451 (define-key map "\C-Q" 'nil) ; ^Q
452 (define-key map "\C-R" 'nil) ; ^R
453 (define-key map "\C-S" 'nil) ; ^S
454 (define-key map "\C-T" 'tpu-toggle-control-keys) ; ^T
455 (define-key map "\C-u" 'upcase-region) ; ^U
456 (define-key map "\C-V" 'nil) ; ^V
457 (define-key map "\C-w" 'tpu-write-current-buffers) ; ^W
458 (define-key map "\C-X" 'nil) ; ^X
459 (define-key map "\C-Y" 'nil) ; ^Y
460 (define-key map "\C-Z" 'nil) ; ^Z
461 (define-key map " " 'undo) ; SPC
462 (define-key map "!" 'nil) ; !
463 (define-key map "#" 'nil) ; #
464 (define-key map "$" 'tpu-add-at-eol) ; $
465 (define-key map "%" 'tpu-goto-percent) ; %
466 (define-key map "&" 'nil) ; &
467 (define-key map "(" 'nil) ; (
468 (define-key map ")" 'nil) ; )
469 (define-key map "*" 'tpu-toggle-regexp) ; *
470 (define-key map "+" 'nil) ; +
471 (define-key map "," 'tpu-goto-breadcrumb) ; ,
472 (define-key map "-" 'negative-argument) ; -
473 (define-key map "." 'tpu-drop-breadcrumb) ; .
474 (define-key map "/" 'tpu-emacs-replace) ; /
475 (define-key map "0" 'digit-argument) ; 0
476 (define-key map "1" 'digit-argument) ; 1
477 (define-key map "2" 'digit-argument) ; 2
478 (define-key map "3" 'digit-argument) ; 3
479 (define-key map "4" 'digit-argument) ; 4
480 (define-key map "5" 'digit-argument) ; 5
481 (define-key map "6" 'digit-argument) ; 6
482 (define-key map "7" 'digit-argument) ; 7
483 (define-key map "8" 'digit-argument) ; 8
484 (define-key map "9" 'digit-argument) ; 9
485 (define-key map ":" 'nil) ; :
486 (define-key map ";" 'tpu-trim-line-ends) ; ;
487 (define-key map "<" 'nil) ; <
488 (define-key map "=" 'nil) ; =
489 (define-key map ">" 'nil) ; >
490 (define-key map "?" 'tpu-spell-check) ; ?
491 (define-key map "A" 'tpu-toggle-newline-and-indent) ; A
492 (define-key map "B" 'tpu-next-buffer) ; B
493 (define-key map "C" 'repeat-complex-command) ; C
494 (define-key map "D" 'shell-command) ; D
495 (define-key map "E" 'tpu-exit) ; E
496 (define-key map "F" 'tpu-set-cursor-free) ; F
497 (define-key map "G" 'tpu-get) ; G
498 (define-key map "H" 'nil) ; H
499 (define-key map "I" 'tpu-include) ; I
500 (define-key map "K" 'tpu-kill-buffer) ; K
501 (define-key map "L" 'tpu-what-line) ; L
502 (define-key map "M" 'buffer-menu) ; M
503 (define-key map "N" 'tpu-next-file-buffer) ; N
504 (define-key map "O" 'occur) ; O
505 (define-key map "P" 'lpr-buffer) ; P
506 (define-key map "Q" 'tpu-quit) ; Q
507 (define-key map "R" 'tpu-toggle-rectangle) ; R
508 (define-key map "S" 'replace) ; S
509 (define-key map "T" 'tpu-line-to-top-of-window) ; T
510 (define-key map "U" 'undo) ; U
511 (define-key map "V" 'tpu-version) ; V
512 (define-key map "W" 'save-buffer) ; W
513 (define-key map "X" 'tpu-save-all-buffers-kill-emacs) ; X
514 (define-key map "Y" 'copy-region-as-kill) ; Y
515 (define-key map "Z" 'suspend-emacs) ; Z
516 (define-key map "[" 'blink-matching-open) ; [
517 (define-key map "\\" 'nil) ; \
518 (define-key map "]" 'blink-matching-open) ; ]
519 (define-key map "^" 'tpu-add-at-bol) ; ^
520 (define-key map "_" 'split-window-vertically) ; -
521 (define-key map "`" 'what-line) ; `
522 (define-key map "a" 'tpu-toggle-newline-and-indent) ; a
523 (define-key map "b" 'tpu-next-buffer) ; b
524 (define-key map "c" 'repeat-complex-command) ; c
525 (define-key map "d" 'shell-command) ; d
526 (define-key map "e" 'tpu-exit) ; e
527 (define-key map "f" 'tpu-set-cursor-free) ; f
528 (define-key map "g" 'tpu-get) ; g
529 (define-key map "h" 'nil) ; h
530 (define-key map "i" 'tpu-include) ; i
531 (define-key map "k" 'tpu-kill-buffer) ; k
532 (define-key map "l" 'goto-line) ; l
533 (define-key map "m" 'buffer-menu) ; m
534 (define-key map "n" 'tpu-next-file-buffer) ; n
535 (define-key map "o" 'occur) ; o
536 (define-key map "p" 'lpr-region) ; p
537 (define-key map "q" 'tpu-quit) ; q
538 (define-key map "r" 'tpu-toggle-rectangle) ; r
539 (define-key map "s" 'replace) ; s
540 (define-key map "t" 'tpu-line-to-top-of-window) ; t
541 (define-key map "u" 'undo) ; u
542 (define-key map "v" 'tpu-version) ; v
543 (define-key map "w" 'save-buffer) ; w
544 (define-key map "x" 'tpu-save-all-buffers-kill-emacs) ; x
545 (define-key map "y" 'copy-region-as-kill) ; y
546 (define-key map "z" 'suspend-emacs) ; z
547 (define-key map "{" 'nil) ; {
548 (define-key map "|" 'split-window-horizontally) ; |
549 (define-key map "}" 'nil) ; }
550 (define-key map "~" 'exchange-point-and-mark) ; ~
551 (define-key map "\177" 'delete-window) ; <X]
552 map)
553 "Maps the function keys on the VT100 keyboard preceded by PF1.
554 GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
555
556 (defvar SS3-map
557 (let ((map (make-sparse-keymap)))
558 (define-key map "P" GOLD-map) ; GOLD map
559 ;;
560 (define-key map "A" 'tpu-previous-line) ; up
561 (define-key map "B" 'tpu-next-line) ; down
562 (define-key map "C" 'tpu-forward-char) ; right
563 (define-key map "D" 'tpu-backward-char) ; left
564
565 (define-key map "Q" 'tpu-help) ; PF2
566 (define-key map "R" 'tpu-search-again) ; PF3
567 (define-key map "S" 'tpu-delete-current-line) ; PF4
568 (define-key map "p" 'tpu-line) ; KP0
569 (define-key map "q" 'tpu-word) ; KP1
570 (define-key map "r" 'tpu-end-of-line) ; KP2
571 (define-key map "s" 'tpu-char) ; KP3
572 (define-key map "t" 'tpu-advance-direction) ; KP4
573 (define-key map "u" 'tpu-backup-direction) ; KP5
574 (define-key map "v" 'tpu-cut) ; KP6
575 (define-key map "w" 'tpu-page) ; KP7
576 (define-key map "x" 'tpu-scroll-window) ; KP8
577 (define-key map "y" 'tpu-append-region) ; KP9
578 (define-key map "m" 'tpu-delete-current-word) ; KP-
579 (define-key map "l" 'tpu-delete-current-char) ; KP,
580 (define-key map "n" 'tpu-select) ; KP.
581 (define-key map "M" 'newline) ; KPenter
582 map)
583 "Maps the SS3 function keys on the VT100 keyboard.
584 SS3 is DEC's name for the sequence <ESC>O.")
585
586 (defvar tpu-global-map nil "TPU-edt global keymap.")
587 (defvar tpu-original-global-map global-map
588 "Original non-TPU global keymap.")
589
590 (and (not (boundp 'minibuffer-local-ns-map))
591 (defvar minibuffer-local-ns-map (make-sparse-keymap)
592 "Hack to give Lucid Emacs the same maps as ordinary Emacs."))
593
594
595 ;;;
596 ;;; Global Variables
597 ;;;
598 (defvar tpu-last-replaced-text ""
599 "Last text deleted by a TPU-edt replace command.")
600 (defvar tpu-last-deleted-region ""
601 "Last text deleted by a TPU-edt remove command.")
602 (defvar tpu-last-deleted-lines ""
603 "Last text deleted by a TPU-edt line-delete command.")
604 (defvar tpu-last-deleted-words ""
605 "Last text deleted by a TPU-edt word-delete command.")
606 (defvar tpu-last-deleted-char ""
607 "Last character deleted by a TPU-edt character-delete command.")
608
609 (defvar tpu-searching-forward t
610 "If non-nil, TPU-edt is searching in the forward direction.")
611 (defvar tpu-search-last-string ""
612 "Last text searched for by the TPU-edt search commands.")
613 (defvar tpu-search-overlay (make-overlay 1 1)
614 "Search highlight overlay.")
615 (overlay-put tpu-search-overlay 'face 'bold)
616
617 (defvar tpu-replace-overlay (make-overlay 1 1)
618 "Replace highlight overlay.")
619 (overlay-put tpu-replace-overlay 'face 'highlight)
620
621 (defvar tpu-regexp-p nil
622 "If non-nil, TPU-edt uses regexp search and replace routines.")
623 (defvar tpu-rectangular-p nil
624 "If non-nil, TPU-edt removes and inserts rectangles.")
625 (defvar tpu-advance t
626 "True when TPU-edt is operating in the forward direction.")
627 (defvar tpu-reverse nil
628 "True when TPU-edt is operating in the backward direction.")
629 (defvar tpu-control-keys nil
630 "If non-nil, control keys are set to perform TPU functions.")
631 (defvar tpu-xkeys-file nil
632 "File containing TPU-edt X key map.")
633
634 (defvar tpu-rectangle-string nil
635 "Mode line string to identify rectangular mode.")
636 (defvar tpu-direction-string nil
637 "Mode line string to identify current direction.")
638
639 (defvar tpu-add-at-bol-hist nil
640 "History variable for tpu-edt-add-at-bol function.")
641 (defvar tpu-add-at-eol-hist nil
642 "History variable for tpu-edt-add-at-eol function.")
643 (defvar tpu-regexp-prompt-hist nil
644 "History variable for search and replace functions.")
645
646
647 ;;;
648 ;;; Buffer Local Variables
649 ;;;
650 (defvar tpu-newline-and-indent-p nil
651 "If non-nil, Return produces a newline and indents.")
652 (make-variable-buffer-local 'tpu-newline-and-indent-p)
653
654 (defvar tpu-newline-and-indent-string nil
655 "Mode line string to identify AutoIndent mode.")
656 (make-variable-buffer-local 'tpu-newline-and-indent-string)
657
658 (defvar tpu-saved-delete-func nil
659 "Saved value of the delete key.")
660 (make-variable-buffer-local 'tpu-saved-delete-func)
661
662 (defvar tpu-buffer-local-map nil
663 "TPU-edt buffer local key map.")
664 (make-variable-buffer-local 'tpu-buffer-local-map)
665
666
667 ;;;
668 ;;; Mode Line - Modify the mode line to show the following
669 ;;;
670 ;;; o Mark state.
671 ;;; o Direction of motion.
672 ;;; o Active rectangle mode.
673 ;;; o Active auto indent mode.
674 ;;;
675 (defvar tpu-original-mm-alist minor-mode-alist)
676
677 (defvar tpu-mark-flag "")
678 (make-variable-buffer-local 'tpu-mark-flag)
679
680 (defun tpu-set-mode-line (for-tpu)
681 "Set ``minor-mode-alist'' for TPU-edt, or reset it to default Emacs."
682 (let ((entries '((tpu-newline-and-indent-p tpu-newline-and-indent-string)
683 (tpu-rectangular-p tpu-rectangle-string)
684 (tpu-direction-string tpu-direction-string)
685 (tpu-mark-flag tpu-mark-flag))))
686 (dolist (entry entries)
687 (if for-tpu
688 (add-to-list 'minor-mode-alist entry)
689 (setq minor-mode-alist (remove entry minor-mode-alist))))))
690
691 (defun tpu-update-mode-line nil
692 "Make sure mode-line in the current buffer reflects all changes."
693 (setq tpu-mark-flag (if transient-mark-mode "" (if (tpu-mark) " @" " ")))
694 (force-mode-line-update))
695
696 (cond (tpu-lucid-emacs-p
697 (add-hook 'zmacs-deactivate-region-hook 'tpu-update-mode-line)
698 (add-hook 'zmacs-activate-region-hook 'tpu-update-mode-line))
699 (t
700 (add-hook 'activate-mark-hook 'tpu-update-mode-line)
701 (add-hook 'deactivate-mark-hook 'tpu-update-mode-line)))
702
703
704 ;;;
705 ;;; Match Markers -
706 ;;;
707 ;;; Set in: Search
708 ;;;
709 ;;; Used in: Replace, Substitute, Store-Text, Cut/Remove,
710 ;;; Append, and Change-Case
711 ;;;
712 (defvar tpu-match-beginning-mark (make-marker))
713 (defvar tpu-match-end-mark (make-marker))
714
715 (defun tpu-set-match nil
716 "Set markers at match beginning and end."
717 ;; Add one to beginning mark so it stays with the first character of
718 ;; the string even if characters are added just before the string.
719 (setq tpu-match-beginning-mark (copy-marker (match-beginning 0) t))
720 (setq tpu-match-end-mark (copy-marker (match-end 0))))
721
722 (defun tpu-unset-match nil
723 "Unset match beginning and end markers."
724 (set-marker tpu-match-beginning-mark nil)
725 (set-marker tpu-match-end-mark nil))
726
727 (defun tpu-match-beginning nil
728 "Returns the location of the last match beginning."
729 (marker-position tpu-match-beginning-mark))
730
731 (defun tpu-match-end nil
732 "Returns the location of the last match end."
733 (marker-position tpu-match-end-mark))
734
735 (defun tpu-check-match nil
736 "Returns t if point is between tpu-match markers.
737 Otherwise sets the tpu-match markers to nil and returns nil."
738 ;; make sure 1- marker is in this buffer
739 ;; 2- point is at or after beginning marker
740 ;; 3- point is before ending marker, or in the case of
741 ;; zero length regions (like bol, or eol) that the
742 ;; beginning, end, and point are equal.
743 (cond ((and
744 (equal (marker-buffer tpu-match-beginning-mark) (current-buffer))
745 (>= (point) (marker-position tpu-match-beginning-mark))
746 (or
747 (< (point) (marker-position tpu-match-end-mark))
748 (and (= (marker-position tpu-match-beginning-mark)
749 (marker-position tpu-match-end-mark))
750 (= (marker-position tpu-match-end-mark) (point))))) t)
751 (t
752 (tpu-unset-match) nil)))
753
754 (defun tpu-show-match-markers nil
755 "Show the values of the match markers."
756 (interactive)
757 (if (markerp tpu-match-beginning-mark)
758 (message "(%s, %s) in %s -- current %s in %s"
759 (marker-position tpu-match-beginning-mark)
760 (marker-position tpu-match-end-mark)
761 (marker-buffer tpu-match-end-mark)
762 (point) (current-buffer))))
763
764
765 ;;;
766 ;;; Utilities
767 ;;;
768 (defun tpu-caar (thingy) (car (car thingy)))
769 (defun tpu-cadr (thingy) (car (cdr thingy)))
770
771 (defvar zmacs-regions)
772
773 (defun tpu-mark nil
774 "TPU-edt version of the mark function.
775 Return the appropriate value of the mark for the current
776 version of Emacs."
777 (cond (tpu-lucid-emacs-p (mark (not zmacs-regions)))
778 (t (and mark-active (mark (not transient-mark-mode))))))
779
780 (defun tpu-set-mark (pos)
781 "TPU-edt version of the `set-mark' function.
782 Sets the mark at POS and activates the region according to the
783 current version of Emacs."
784 (set-mark pos)
785 ;; We use a separate `if' for the fboundp so the byte-compiler notices it
786 ;; and doesn't complain about the subsequent call.
787 (if (fboundp 'zmacs-activate-region) (if pos (zmacs-activate-region))))
788
789 (defun tpu-string-prompt (prompt history-symbol)
790 "Read a string with PROMPT."
791 (read-from-minibuffer prompt nil nil nil history-symbol))
792
793 (defvar tpu-last-answer nil "Most recent response to tpu-y-or-n-p.")
794
795 (defun tpu-y-or-n-p (prompt &optional not-yes)
796 "Prompt for a y or n answer with positive default.
797 Optional second argument NOT-YES changes default to negative.
798 Like Emacs `y-or-n-p', but also accepts space as y and DEL as n."
799 (message "%s[%s]" prompt (if not-yes "n" "y"))
800 (let ((doit t))
801 (while doit
802 (setq doit nil)
803 (let ((ans (read-char)))
804 (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\ ))
805 (setq tpu-last-answer t))
806 ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
807 (setq tpu-last-answer nil))
808 ((= ans ?\r) (setq tpu-last-answer (not not-yes)))
809 (t
810 (setq doit t) (beep)
811 (message "Please answer y or n. %s[%s]"
812 prompt (if not-yes "n" "y")))))))
813 tpu-last-answer)
814
815 (defun tpu-local-set-key (key func)
816 "Replace a key in the TPU-edt local key map.
817 Create the key map if necessary."
818 (cond ((not (keymapp tpu-buffer-local-map))
819 (setq tpu-buffer-local-map (if (current-local-map)
820 (copy-keymap (current-local-map))
821 (make-sparse-keymap)))
822 (use-local-map tpu-buffer-local-map)))
823 (local-set-key key func))
824
825 (defun tpu-current-line nil
826 "Return the vertical position of point in the selected window.
827 Top line is 0. Counts each text line only once, even if it wraps."
828 (+ (count-lines (window-start) (point)) (if (= (current-column) 0) 1 0) -1))
829
830
831 ;;;
832 ;;; Breadcrumbs
833 ;;;
834 (defvar tpu-breadcrumb-plist nil
835 "The set of user-defined markers (breadcrumbs), as a plist.")
836
837 (defun tpu-drop-breadcrumb (num)
838 "Drops a breadcrumb that can be returned to later with goto-breadcrumb."
839 (interactive "p")
840 (put tpu-breadcrumb-plist num (list (current-buffer) (point)))
841 (message "Mark %d set." num))
842
843 (defun tpu-goto-breadcrumb (num)
844 "Returns to a breadcrumb set with drop-breadcrumb."
845 (interactive "p")
846 (cond ((get tpu-breadcrumb-plist num)
847 (switch-to-buffer (car (get tpu-breadcrumb-plist num)))
848 (goto-char (tpu-cadr (get tpu-breadcrumb-plist num)))
849 (message "mark %d found." num))
850 (t
851 (message "mark %d not found." num))))
852
853
854 ;;;
855 ;;; Miscellaneous
856 ;;;
857 (defun tpu-change-case (num)
858 "Change the case of the character under the cursor or region.
859 Accepts a prefix argument of the number of characters to invert."
860 (interactive "p")
861 (cond ((tpu-mark)
862 (let ((beg (region-beginning)) (end (region-end)))
863 (while (> end beg)
864 (funcall (if (= (downcase (char-after beg)) (char-after beg))
865 'upcase-region 'downcase-region)
866 beg (1+ beg))
867 (setq beg (1+ beg)))
868 (tpu-unselect t)))
869 ((tpu-check-match)
870 (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
871 (while (> end beg)
872 (funcall (if (= (downcase (char-after beg)) (char-after beg))
873 'upcase-region 'downcase-region)
874 beg (1+ beg))
875 (setq beg (1+ beg)))
876 (tpu-unset-match)))
877 (t
878 (while (> num 0)
879 (funcall (if (= (downcase (following-char)) (following-char))
880 'upcase-region 'downcase-region)
881 (point) (1+ (point)))
882 (forward-char (if tpu-reverse -1 1))
883 (setq num (1- num))))))
884
885 (defun tpu-fill (num)
886 "Fill paragraph or marked region.
887 With argument, fill and justify."
888 (interactive "P")
889 (cond ((tpu-mark)
890 (fill-region (point) (tpu-mark) num)
891 (tpu-unselect t))
892 (t
893 (fill-paragraph num))))
894
895 (defun tpu-version nil
896 "Print the TPU-edt version number."
897 (interactive)
898 (message
899 "TPU-edt version %s by Rob Riepel (riepel@networking.stanford.edu)"
900 tpu-version))
901
902 (defun tpu-reset-screen-size (height width)
903 "Sets the screen size."
904 (interactive "nnew screen height: \nnnew screen width: ")
905 (set-frame-height (selected-frame) height)
906 (set-frame-width (selected-frame) width))
907
908 (defun tpu-toggle-newline-and-indent nil
909 "Toggle between 'newline and indent' and 'simple newline'."
910 (interactive)
911 (cond (tpu-newline-and-indent-p
912 (setq tpu-newline-and-indent-string "")
913 (setq tpu-newline-and-indent-p nil)
914 (tpu-local-set-key "\C-m" 'newline))
915 (t
916 (setq tpu-newline-and-indent-string " AutoIndent")
917 (setq tpu-newline-and-indent-p t)
918 (tpu-local-set-key "\C-m" 'newline-and-indent)))
919 (tpu-update-mode-line)
920 (and (interactive-p)
921 (message "Carriage return inserts a newline%s"
922 (if tpu-newline-and-indent-p " and indents." "."))))
923
924 (defun tpu-spell-check nil
925 "Checks the spelling of the region, or of the entire buffer if no
926 region is selected."
927 (interactive)
928 (cond (tpu-have-ispell
929 (if (tpu-mark) (ispell-region (tpu-mark) (point)) (ispell-buffer)))
930 (t
931 (if (tpu-mark) (spell-region (tpu-mark) (point)) (spell-buffer))))
932 (if (tpu-mark) (tpu-unselect t)))
933
934 (defun tpu-toggle-overwrite-mode nil
935 "Switches in and out of overwrite mode"
936 (interactive)
937 (cond (overwrite-mode
938 (tpu-local-set-key "\177" tpu-saved-delete-func)
939 (overwrite-mode 0))
940 (t
941 (setq tpu-saved-delete-func (local-key-binding "\177"))
942 (tpu-local-set-key "\177" 'picture-backward-clear-column)
943 (overwrite-mode 1))))
944
945 (defun tpu-special-insert (num)
946 "Insert a character or control code according to
947 its ASCII decimal value."
948 (interactive "P")
949 (if overwrite-mode (delete-char 1))
950 (insert (if num num 0)))
951
952 (defun tpu-quoted-insert (num)
953 "Read next input character and insert it.
954 This is useful for inserting control characters."
955 (interactive "*p")
956 (let ((char (read-char)) )
957 (if overwrite-mode (delete-char num))
958 (insert-char char num)))
959
960
961 ;;;
962 ;;; TPU line-mode commands
963 ;;;
964 (defun tpu-include (file)
965 "TPU-like include file"
966 (interactive "fInclude file: ")
967 (insert-file-contents file)
968 (message ""))
969
970 (defun tpu-get (file)
971 "TPU-like get file"
972 (interactive "FFile to get: ")
973 (find-file file find-file-wildcards))
974
975 (defun tpu-what-line nil
976 "Tells what line the point is on,
977 and the total number of lines in the buffer."
978 (interactive)
979 (if (eobp)
980 (message "You are at the End of Buffer. The last line is %d."
981 (count-lines 1 (point-max)))
982 (let* ((cur (count-lines 1 (1+ (point))))
983 (max (count-lines 1 (point-max)))
984 (pct (/ (* 100 (+ cur (/ max 200))) max)))
985 (message "You are on line %d out of %d (%d%%)." cur max pct))))
986
987 (defun tpu-exit nil
988 "Exit the way TPU does, save current buffer and ask about others."
989 (interactive)
990 (if (not (eq (recursion-depth) 0))
991 (exit-recursive-edit)
992 (progn (save-buffer) (save-buffers-kill-emacs))))
993
994 (defun tpu-quit nil
995 "Quit the way TPU does, ask to make sure changes should be abandoned."
996 (interactive)
997 (let ((list (buffer-list))
998 (working t))
999 (while (and list working)
1000 (let ((buffer (car list)))
1001 (if (and (buffer-file-name buffer) (buffer-modified-p buffer))
1002 (if (tpu-y-or-n-p
1003 "Modifications will not be saved, continue quitting? ")
1004 (kill-emacs t) (setq working nil)))
1005 (setq list (cdr list))))
1006 (if working (kill-emacs t))))
1007
1008
1009 ;;;
1010 ;;; Command and Function Aliases
1011 ;;;
1012 ;;;###autoload
1013 (define-minor-mode tpu-edt-mode
1014 "TPU/edt emulation."
1015 :global t
1016 (if tpu-edt-mode (tpu-edt-on) (tpu-edt-off)))
1017
1018 (defalias 'TPU-EDT-MODE 'tpu-edt-mode)
1019
1020 ;;;###autoload
1021 (defalias 'tpu-edt 'tpu-edt-on)
1022 (defalias 'TPU-EDT 'tpu-edt-on)
1023
1024 ;; Note: The following functions have no `tpu-' prefix. This is unavoidable.
1025 ;; The real TPU/edt editor has interactive commands with these names,
1026 ;; so tpu-edt.el users expect things like M-x exit RET and M-x help RET
1027 ;; to work. Therefore it really is necessary to define these functions,
1028 ;; even in cases where they redefine existing Emacs functions.
1029
1030 (defalias 'exit 'tpu-exit)
1031 (defalias 'EXIT 'tpu-exit)
1032
1033 (defalias 'Get 'tpu-get)
1034 (defalias 'GET 'tpu-get)
1035
1036 (defalias 'include 'tpu-include)
1037 (defalias 'INCLUDE 'tpu-include)
1038
1039 (defalias 'quit 'tpu-quit)
1040 (defalias 'QUIT 'tpu-quit)
1041
1042 (defalias 'spell 'tpu-spell-check)
1043 (defalias 'SPELL 'tpu-spell-check)
1044
1045 (defalias 'what\ line 'tpu-what-line)
1046 (defalias 'WHAT\ LINE 'tpu-what-line)
1047
1048 (defalias 'replace 'tpu-lm-replace)
1049 (defalias 'REPLACE 'tpu-lm-replace)
1050
1051 (defalias 'help 'tpu-help)
1052 (defalias 'HELP 'tpu-help)
1053
1054 (defalias 'set\ cursor\ free 'tpu-set-cursor-free)
1055 (defalias 'SET\ CURSOR\ FREE 'tpu-set-cursor-free)
1056
1057 (defalias 'set\ cursor\ bound 'tpu-set-cursor-bound)
1058 (defalias 'SET\ CURSOR\ BOUND 'tpu-set-cursor-bound)
1059
1060 (defalias 'set\ scroll\ margins 'tpu-set-scroll-margins)
1061 (defalias 'SET\ SCROLL\ MARGINS 'tpu-set-scroll-margins)
1062
1063 ;; Real TPU error messages end in periods.
1064 ;; Define this to avoid openly flouting Emacs coding standards.
1065 (defalias 'tpu-error 'error)
1066
1067
1068 ;; Around emacs version 18.57, function line-move was renamed to
1069 ;; next-line-internal. If we're running under an older emacs,
1070 ;; make next-line-internal equivalent to line-move.
1071
1072 (if (not (fboundp 'next-line-internal)) (fset 'next-line-internal 'line-move))
1073
1074
1075 ;;;
1076 ;;; Help
1077 ;;;
1078 (defvar tpu-help-keypad-map "\f
1079 _______________________ _______________________________
1080 | HELP | Do | | | | | |
1081 |KeyDefs| | | | | | |
1082 |_______|_______________| |_______|_______|_______|_______|
1083 _______________________ _______________________________
1084 | Find |Insert |Remove | | Gold | HELP |FndNxt | Del L |
1085 | | |Sto Tex| | key |E-Help | Find |Undel L|
1086 |_______|_______|_______| |_______|_______|_______|_______|
1087 |Select |Pre Scr|Nex Scr| | Page | Sect |Append | Del W |
1088 | Reset |Pre Win|Nex Win| | Do | Fill |Replace|Undel W|
1089 |_______|_______|_______| |_______|_______|_______|_______|
1090 |Move up| |Forward|Reverse|Remove | Del C |
1091 | Top | |Bottom | Top |Insert |Undel C|
1092 _______|_______|_______ |_______|_______|_______|_______|
1093 |Mov Lef|Mov Dow|Mov Rig| | Word | EOL | Char | |
1094 |StaOfLi|Bottom |EndOfLi| |ChngCas|Del EOL|SpecIns| Enter |
1095 |_______|_______|_______| |_______|_______|_______| |
1096 | Line |Select | Subs |
1097 | Open Line | Reset | |
1098 |_______________|_______|_______|
1099 ")
1100
1101 (defvar tpu-help-text "
1102 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
1103
1104 Control Characters
1105
1106 ^A toggle insert and overwrite
1107 ^B recall
1108 ^E end of line
1109
1110 ^G Cancel current operation
1111 ^H beginning of line
1112 ^J delete previous word
1113
1114 ^K learn
1115 ^L insert page break
1116 ^R remember (during learn), re-center
1117
1118 ^U delete to beginning of line
1119 ^V quote
1120 ^W refresh
1121
1122 ^Z exit
1123 ^X^X exchange point and mark - useful for checking region boundaries
1124
1125 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
1126 Gold-<key> Functions
1127
1128 B Next Buffer - display the next buffer (all buffers)
1129 C Recall - edit and possibly repeat previous commands
1130 E Exit - save current buffer and ask about others
1131 G Get - load a file into a new edit buffer
1132
1133 I Include - include a file in this buffer
1134 K Kill Buffer - abandon edits and delete buffer
1135 M Buffer Menu - display a list of all buffers
1136 N Next File Buffer - display next buffer containing a file
1137
1138 O Occur - show following lines containing REGEXP
1139 Q Quit - exit without saving anything
1140 R Toggle rectangular mode for remove and insert
1141 S Search and substitute - line mode REPLACE command
1142
1143 ^T Toggle control key bindings between TPU and emacs
1144 U Undo - undo the last edit
1145 W Write - save current buffer
1146 X Exit - save all modified buffers and exit
1147
1148 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
1149
1150 More extensive documentation on TPU-edt can be found in the `Commentary'
1151 section of tpu-edt.el. This section can be accessed through the standard
1152 Emacs help facility using the `p' option. Once you exit TPU-edt Help, one
1153 of the following key sequences is sure to get you there.
1154
1155 ^h p if you're not yet using TPU-edt
1156 Gold-PF2 p if you're using TPU-edt
1157
1158 Alternatively, fire up Emacs help from the command prompt, with
1159
1160 M-x help-for-help <CR> p <CR>
1161
1162 Where `M-x' might be any of `Gold-KP7', 'Do', or 'ESC-x'.
1163
1164 When you successfully invoke this part of the Emacs help facility, you
1165 will see a buffer named `*Finder*' listing a number of topics. Look for
1166 tpu-edt under `emulations'.
1167
1168 \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
1169
1170 *** No more help, use P to view previous screen")
1171
1172 (defvar tpu-help-enter (format "%s" "\eOM")) ; tpu-help enter key symbol
1173 (defvar tpu-help-return (format "%s" "\r")) ; tpu-help enter key symbol
1174 (defvar tpu-help-N "N") ; tpu-help "N" symbol
1175 (defvar tpu-help-n "n") ; tpu-help "n" symbol
1176 (defvar tpu-help-P "P") ; tpu-help "P" symbol
1177 (defvar tpu-help-p "p") ; tpu-help "p" symbol
1178
1179 (defun tpu-help nil
1180 "Display TPU-edt help."
1181 (interactive)
1182 ;; Save current window configuration
1183 (save-window-excursion
1184 ;; Create and fill help buffer if necessary
1185 (if (not (get-buffer "*TPU-edt Help*"))
1186 (progn (generate-new-buffer "*TPU-edt Help*")
1187 (switch-to-buffer "*TPU-edt Help*")
1188 (insert tpu-help-keypad-map)
1189 (insert tpu-help-text)
1190 (setq buffer-read-only t)))
1191
1192 ;; Display the help buffer
1193 (switch-to-buffer "*TPU-edt Help*")
1194 (delete-other-windows)
1195 (tpu-move-to-beginning)
1196 (forward-line 1)
1197 (tpu-line-to-top-of-window)
1198
1199 ;; Prompt for keys to describe, based on screen state (split/not split)
1200 (let ((key nil) (fkey nil) (split nil))
1201 (while (not (equal tpu-help-return fkey))
1202 (if split
1203 (setq key
1204 (read-key-sequence
1205 "Press the key you want help on (RET=exit, ENTER=redisplay, N=next, P=prev): "))
1206 (setq key
1207 (read-key-sequence
1208 "Press the key you want help on (RET to exit, N next screen, P prev screen): ")))
1209
1210 ;; Process the read key
1211 ;;
1212 ;; ENTER - Display just the help window
1213 ;; N or n - Next help or describe-key screen
1214 ;; P or p - Previous help or describe-key screen
1215 ;; RETURN - Exit from TPU-help
1216 ;; default - describe the key
1217 ;;
1218 (setq fkey (format "%s" key))
1219 (cond ((equal tpu-help-enter fkey)
1220 (setq split nil)
1221 (delete-other-windows))
1222 ((or (equal tpu-help-N fkey) (equal tpu-help-n fkey))
1223 (cond (split
1224 (condition-case nil
1225 (scroll-other-window 8)
1226 (error nil)))
1227 (t
1228 (forward-page)
1229 (forward-line 1)
1230 (tpu-line-to-top-of-window))))
1231 ((or (equal tpu-help-P fkey) (equal tpu-help-p fkey))
1232 (cond (split
1233 (condition-case nil
1234 (scroll-other-window -8)
1235 (error nil)))
1236 (t
1237 (forward-line -1)
1238 (backward-page)
1239 (forward-line 1)
1240 (tpu-line-to-top-of-window))))
1241 ((not (equal tpu-help-return fkey))
1242 (setq split t)
1243 (describe-key key)
1244 ;; If the key is undefined, leave the
1245 ;; message in the mini-buffer for 3 seconds
1246 (if (not (key-binding key)) (sit-for 3))))))))
1247
1248
1249 ;;;
1250 ;;; Auto-insert
1251 ;;;
1252 (defun tpu-insert-escape nil
1253 "Inserts an escape character, and so becomes the escape-key alias."
1254 (interactive)
1255 (insert "\e"))
1256
1257 (defun tpu-insert-formfeed nil
1258 "Inserts a formfeed character."
1259 (interactive)
1260 (insert "\C-L"))
1261
1262
1263 ;;;
1264 ;;; Define key
1265 ;;;
1266 (defvar tpu-saved-control-r nil "Saved value of Control-r.")
1267
1268 (defun tpu-end-define-macro-key (key)
1269 "Ends the current macro definition"
1270 (interactive "kPress the key you want to use to do what was just learned: ")
1271 (end-kbd-macro nil)
1272 (global-set-key key last-kbd-macro)
1273 (global-set-key "\C-r" tpu-saved-control-r))
1274
1275 (defun tpu-define-macro-key nil
1276 "Bind a set of keystrokes to a single key, or key combination."
1277 (interactive)
1278 (setq tpu-saved-control-r (global-key-binding "\C-r"))
1279 (global-set-key "\C-r" 'tpu-end-define-macro-key)
1280 (start-kbd-macro nil))
1281
1282
1283 ;;;
1284 ;;; Buffers and Windows
1285 ;;;
1286 (defun tpu-kill-buffer nil
1287 "Kills the current buffer. If tpu-kill-buffers-silently is non-nil,
1288 kills modified buffers without asking."
1289 (interactive)
1290 (if tpu-kill-buffers-silently (set-buffer-modified-p nil))
1291 (kill-buffer (current-buffer)))
1292
1293 (defun tpu-save-all-buffers-kill-emacs nil
1294 "Save all buffers and exit emacs."
1295 (interactive)
1296 (let ((delete-old-versions t))
1297 (save-buffers-kill-emacs t)))
1298
1299 (defun tpu-write-current-buffers nil
1300 "Save all modified buffers without exiting."
1301 (interactive)
1302 (save-some-buffers t))
1303
1304 (defun tpu-next-buffer nil
1305 "Go to next buffer in ring."
1306 (interactive)
1307 (switch-to-buffer (car (reverse (buffer-list)))))
1308
1309 (defun tpu-next-file-buffer nil
1310 "Go to next buffer in ring that is visiting a file or directory."
1311 (interactive)
1312 (let ((list (tpu-make-file-buffer-list (buffer-list))))
1313 (setq list (delq (current-buffer) list))
1314 (if (not list) (tpu-error "No other buffers."))
1315 (switch-to-buffer (car (reverse list)))))
1316
1317 (defun tpu-make-file-buffer-list (buffer-list)
1318 "Returns names from BUFFER-LIST excluding those beginning with a space or star."
1319 (delq nil (mapcar '(lambda (b)
1320 (if (or (= (aref (buffer-name b) 0) ? )
1321 (= (aref (buffer-name b) 0) ?*)) nil b))
1322 buffer-list)))
1323
1324 (defun tpu-next-window nil
1325 "Move to the next window."
1326 (interactive)
1327 (if (one-window-p) (message "There is only one window on screen.")
1328 (other-window 1)))
1329
1330 (defun tpu-previous-window nil
1331 "Move to the previous window."
1332 (interactive)
1333 (if (one-window-p) (message "There is only one window on screen.")
1334 (select-window (previous-window))))
1335
1336
1337 ;;;
1338 ;;; Search
1339 ;;;
1340 (defun tpu-toggle-regexp nil
1341 "Switches in and out of regular expression search and replace mode."
1342 (interactive)
1343 (setq tpu-regexp-p (not tpu-regexp-p))
1344 (tpu-set-search)
1345 (and (interactive-p)
1346 (message "Regular expression search and substitute %sabled."
1347 (if tpu-regexp-p "en" "dis"))))
1348
1349 (defun tpu-regexp-prompt (prompt)
1350 "Read a string, adding 'RE' to the prompt if tpu-regexp-p is set."
1351 (let ((re-prompt (concat (if tpu-regexp-p "RE ") prompt)))
1352 (read-from-minibuffer re-prompt nil nil nil 'tpu-regexp-prompt-hist)))
1353
1354 (defun tpu-search-highlight nil
1355 (if (tpu-check-match)
1356 (move-overlay tpu-search-overlay
1357 (tpu-match-beginning) (tpu-match-end) (current-buffer))
1358 (unless (equal (overlay-start tpu-search-overlay)
1359 (overlay-end tpu-search-overlay))
1360 (move-overlay tpu-search-overlay 1 1 (current-buffer)))))
1361
1362 (defun tpu-search nil
1363 "Search for a string or regular expression.
1364 The search is performed in the current direction."
1365 (interactive)
1366 (tpu-set-search)
1367 (tpu-search-internal ""))
1368
1369 (defun tpu-search-forward nil
1370 "Search for a string or regular expression.
1371 The search is begins in the forward direction."
1372 (interactive)
1373 (setq tpu-searching-forward t)
1374 (tpu-set-search t)
1375 (tpu-search-internal ""))
1376
1377 (defun tpu-search-reverse nil
1378 "Search for a string or regular expression.
1379 The search is begins in the reverse direction."
1380 (interactive)
1381 (setq tpu-searching-forward nil)
1382 (tpu-set-search t)
1383 (tpu-search-internal ""))
1384
1385 (defun tpu-search-again nil
1386 "Search for the same string or regular expression as last time.
1387 The search is performed in the current direction."
1388 (interactive)
1389 (tpu-search-internal tpu-search-last-string))
1390
1391 ;; tpu-set-search defines the search functions used by the TPU-edt internal
1392 ;; search function. It should be called whenever the direction changes, or
1393 ;; the regular expression mode is turned on or off. It can also be called
1394 ;; to ensure that the next search will be in the current direction. It is
1395 ;; called from:
1396
1397 ;; tpu-advance tpu-backup
1398 ;; tpu-toggle-regexp tpu-toggle-search-direction (t)
1399 ;; tpu-search tpu-lm-replace
1400 ;; tpu-search-forward (t) tpu-search-reverse (t)
1401 ;; tpu-search-forward-exit (t) tpu-search-backward-exit (t)
1402
1403 (defun tpu-set-search (&optional arg)
1404 "Set the search functions and set the search direction to the current
1405 direction. If an argument is specified, don't set the search direction."
1406 (if (not arg) (setq tpu-searching-forward tpu-advance))
1407 (cond (tpu-searching-forward
1408 (cond (tpu-regexp-p
1409 (fset 'tpu-emacs-search 're-search-forward)
1410 (fset 'tpu-emacs-rev-search 're-search-backward))
1411 (t
1412 (fset 'tpu-emacs-search 'search-forward)
1413 (fset 'tpu-emacs-rev-search 'search-backward))))
1414 (t
1415 (cond (tpu-regexp-p
1416 (fset 'tpu-emacs-search 're-search-backward)
1417 (fset 'tpu-emacs-rev-search 're-search-forward))
1418 (t
1419 (fset 'tpu-emacs-search 'search-backward)
1420 (fset 'tpu-emacs-rev-search 'search-forward))))))
1421
1422 (defun tpu-search-internal (pat &optional quiet)
1423 "Search for a string or regular expression."
1424 (setq tpu-search-last-string
1425 (if (not (string= "" pat)) pat (tpu-regexp-prompt "Search: ")))
1426
1427 (tpu-unset-match)
1428 (tpu-adjust-search)
1429
1430 (let ((case-fold-search
1431 (and case-fold-search (tpu-check-search-case tpu-search-last-string))))
1432
1433 (cond ((tpu-emacs-search tpu-search-last-string nil t)
1434 (tpu-set-match) (goto-char (tpu-match-beginning)))
1435
1436 (t
1437 (tpu-adjust-search t)
1438 (let ((found nil) (pos nil))
1439 (save-excursion
1440 (let ((tpu-searching-forward (not tpu-searching-forward)))
1441 (tpu-adjust-search)
1442 (setq found (tpu-emacs-rev-search tpu-search-last-string nil t))
1443 (setq pos (match-beginning 0))))
1444
1445 (cond
1446 (found
1447 (cond ((tpu-y-or-n-p
1448 (format "Found in %s direction. Go there? "
1449 (if tpu-searching-forward "reverse" "forward")))
1450 (goto-char pos) (tpu-set-match)
1451 (tpu-toggle-search-direction))))
1452
1453 (t
1454 (if (not quiet)
1455 (message
1456 "%sSearch failed: \"%s\""
1457 (if tpu-regexp-p "RE " "") tpu-search-last-string)))))))))
1458
1459 (defalias 'tpu-search-internal-core (symbol-function 'tpu-search-internal))
1460
1461 (defun tpu-check-search-case (string)
1462 "Returns t if string contains upper case."
1463 ;; if using regexp, eliminate upper case forms (\B \W \S.)
1464 (if tpu-regexp-p
1465 (let ((pat (copy-sequence string)) (case-fold-search nil) (pos 0))
1466 (while (setq pos (string-match "\\\\\\\\" pat)) (aset pat (+ 1 pos) ?.))
1467 (while (setq pos (string-match "\\\\B" pat)) (aset pat (+ 1 pos) ?.))
1468 (while (setq pos (string-match "\\\\W" pat)) (aset pat (+ 1 pos) ?.))
1469 (while (setq pos (string-match "\\\\S." pat))
1470 (aset pat (+ 1 pos) ?.) (aset pat (+ 2 pos) ?.))
1471 (string-equal pat (downcase pat)))
1472 (string-equal string (downcase string))))
1473
1474 (defun tpu-adjust-search (&optional arg)
1475 "For forward searches, move forward a character before searching,
1476 and backward a character after a failed search. Arg means end of search."
1477 (if tpu-searching-forward
1478 (cond (arg (if (not (bobp)) (forward-char -1)))
1479 (t (if (not (eobp)) (forward-char 1))))))
1480
1481 (defun tpu-toggle-search-direction nil
1482 "Toggle the TPU-edt search direction.
1483 Used for reversing a search in progress."
1484 (interactive)
1485 (setq tpu-searching-forward (not tpu-searching-forward))
1486 (tpu-set-search t)
1487 (and (interactive-p)
1488 (message "Searching %sward."
1489 (if tpu-searching-forward "for" "back"))))
1490
1491 (defun tpu-search-forward-exit nil
1492 "Set search direction forward and exit minibuffer."
1493 (interactive)
1494 (setq tpu-searching-forward t)
1495 (tpu-set-search t)
1496 (exit-minibuffer))
1497
1498 (defun tpu-search-backward-exit nil
1499 "Set search direction backward and exit minibuffer."
1500 (interactive)
1501 (setq tpu-searching-forward nil)
1502 (tpu-set-search t)
1503 (exit-minibuffer))
1504
1505
1506 ;;;
1507 ;;; Select / Unselect
1508 ;;;
1509 (defun tpu-select (&optional quiet)
1510 "Sets the mark to define one end of a region."
1511 (interactive "P")
1512 (cond ((tpu-mark)
1513 (tpu-unselect quiet))
1514 (t
1515 (tpu-set-mark (point))
1516 (tpu-update-mode-line)
1517 (if (not quiet) (message "Move the text cursor to select text.")))))
1518
1519 (defun tpu-unselect (&optional quiet)
1520 "Removes the mark to unselect the current region."
1521 (interactive "P")
1522 (deactivate-mark)
1523 (setq mark-ring nil)
1524 (tpu-set-mark nil)
1525 (tpu-update-mode-line)
1526 (if (not quiet) (message "Selection canceled.")))
1527
1528
1529 ;;;
1530 ;;; Delete / Cut
1531 ;;;
1532 (defun tpu-toggle-rectangle nil
1533 "Toggle rectangular mode for remove and insert."
1534 (interactive)
1535 (setq tpu-rectangular-p (not tpu-rectangular-p))
1536 (setq tpu-rectangle-string (if tpu-rectangular-p " Rect" ""))
1537 (tpu-update-mode-line)
1538 (and (interactive-p)
1539 (message "Rectangular cut and paste %sabled."
1540 (if tpu-rectangular-p "en" "dis"))))
1541
1542 (defun tpu-arrange-rectangle nil
1543 "Adjust point and mark to mark upper left and lower right
1544 corners of a rectangle."
1545 (let ((mc (current-column))
1546 (pc (progn (exchange-point-and-mark) (current-column))))
1547
1548 (cond ((> (point) (tpu-mark)) ; point on lower line
1549 (cond ((> pc mc) ; point @ lower-right
1550 (exchange-point-and-mark)) ; point -> upper-left
1551
1552 (t ; point @ lower-left
1553 (move-to-column mc t) ; point -> lower-right
1554 (exchange-point-and-mark) ; point -> upper-right
1555 (move-to-column pc t)))) ; point -> upper-left
1556
1557 (t ; point on upper line
1558 (cond ((> pc mc) ; point @ upper-right
1559 (move-to-column mc t) ; point -> upper-left
1560 (exchange-point-and-mark) ; point -> lower-left
1561 (move-to-column pc t) ; point -> lower-right
1562 (exchange-point-and-mark))))))) ; point -> upper-left
1563
1564 (defun tpu-cut-text nil
1565 "Delete the selected region.
1566 The text is saved for the tpu-paste command."
1567 (interactive)
1568 (cond ((tpu-mark)
1569 (cond (tpu-rectangular-p
1570 (tpu-arrange-rectangle)
1571 (picture-clear-rectangle (point) (tpu-mark) (not overwrite-mode))
1572 (tpu-unselect t))
1573 (t
1574 (setq tpu-last-deleted-region
1575 (buffer-substring (tpu-mark) (point)))
1576 (delete-region (tpu-mark) (point))
1577 (tpu-unselect t))))
1578 ((tpu-check-match)
1579 (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
1580 (setq tpu-last-deleted-region (buffer-substring beg end))
1581 (delete-region beg end)
1582 (tpu-unset-match)))
1583 (t
1584 (tpu-error "No selection active."))))
1585
1586 (defun tpu-store-text nil
1587 "Copy the selected region to the cut buffer without deleting it.
1588 The text is saved for the tpu-paste command."
1589 (interactive)
1590 (cond ((tpu-mark)
1591 (cond (tpu-rectangular-p
1592 (save-excursion
1593 (tpu-arrange-rectangle)
1594 (setq picture-killed-rectangle
1595 (extract-rectangle (point) (tpu-mark))))
1596 (tpu-unselect t))
1597 (t
1598 (setq tpu-last-deleted-region
1599 (buffer-substring (tpu-mark) (point)))
1600 (tpu-unselect t))))
1601 ((tpu-check-match)
1602 (setq tpu-last-deleted-region
1603 (buffer-substring (tpu-match-beginning) (tpu-match-end)))
1604 (tpu-unset-match))
1605 (t
1606 (tpu-error "No selection active."))))
1607
1608 (defun tpu-cut (arg)
1609 "Copy selected region to the cut buffer. In the absence of an
1610 argument, delete the selected region too."
1611 (interactive "P")
1612 (if arg (tpu-store-text) (tpu-cut-text)))
1613
1614 (defun tpu-append-region (arg)
1615 "Append selected region to the tpu-cut buffer. In the absence of an
1616 argument, delete the selected region too."
1617 (interactive "P")
1618 (cond ((tpu-mark)
1619 (let ((beg (region-beginning)) (end (region-end)))
1620 (setq tpu-last-deleted-region
1621 (concat tpu-last-deleted-region
1622 (buffer-substring beg end)))
1623 (if (not arg) (delete-region beg end))
1624 (tpu-unselect t)))
1625 ((tpu-check-match)
1626 (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
1627 (setq tpu-last-deleted-region
1628 (concat tpu-last-deleted-region
1629 (buffer-substring beg end)))
1630 (if (not arg) (delete-region beg end))
1631 (tpu-unset-match)))
1632 (t
1633 (tpu-error "No selection active."))))
1634
1635 (defun tpu-delete-current-line (num)
1636 "Delete one or specified number of lines after point.
1637 This includes the newline character at the end of each line.
1638 They are saved for the TPU-edt undelete-lines command."
1639 (interactive "p")
1640 (let ((beg (point)))
1641 (forward-line num)
1642 (if (not (eq (preceding-char) ?\n))
1643 (insert "\n"))
1644 (setq tpu-last-deleted-lines
1645 (buffer-substring beg (point)))
1646 (delete-region beg (point))))
1647
1648 (defun tpu-delete-to-eol (num)
1649 "Delete text up to end of line.
1650 With argument, delete up to the Nth line-end past point.
1651 They are saved for the TPU-edt undelete-lines command."
1652 (interactive "p")
1653 (let ((beg (point)))
1654 (forward-char 1)
1655 (end-of-line num)
1656 (setq tpu-last-deleted-lines
1657 (buffer-substring beg (point)))
1658 (delete-region beg (point))))
1659
1660 (defun tpu-delete-to-bol (num)
1661 "Delete text back to beginning of line.
1662 With argument, delete up to the Nth line-end past point.
1663 They are saved for the TPU-edt undelete-lines command."
1664 (interactive "p")
1665 (let ((beg (point)))
1666 (tpu-next-beginning-of-line num)
1667 (setq tpu-last-deleted-lines
1668 (buffer-substring (point) beg))
1669 (delete-region (point) beg)))
1670
1671 (defun tpu-delete-current-word (num)
1672 "Delete one or specified number of words after point.
1673 They are saved for the TPU-edt undelete-words command."
1674 (interactive "p")
1675 (let ((beg (point)))
1676 (tpu-forward-to-word num)
1677 (setq tpu-last-deleted-words
1678 (buffer-substring beg (point)))
1679 (delete-region beg (point))))
1680
1681 (defun tpu-delete-previous-word (num)
1682 "Delete one or specified number of words before point.
1683 They are saved for the TPU-edt undelete-words command."
1684 (interactive "p")
1685 (let ((beg (point)))
1686 (tpu-backward-to-word num)
1687 (setq tpu-last-deleted-words
1688 (buffer-substring (point) beg))
1689 (delete-region beg (point))))
1690
1691 (defun tpu-delete-current-char (num)
1692 "Delete one or specified number of characters after point. The last
1693 character deleted is saved for the TPU-edt undelete-char command."
1694 (interactive "p")
1695 (while (and (> num 0) (not (eobp)))
1696 (setq tpu-last-deleted-char (char-after (point)))
1697 (cond (overwrite-mode
1698 (picture-clear-column 1)
1699 (forward-char 1))
1700 (t
1701 (delete-char 1)))
1702 (setq num (1- num))))
1703
1704
1705 ;;;
1706 ;;; Undelete / Paste
1707 ;;;
1708 (defun tpu-paste (num)
1709 "Insert the last region or rectangle of killed text.
1710 With argument reinserts the text that many times."
1711 (interactive "p")
1712 (while (> num 0)
1713 (cond (tpu-rectangular-p
1714 (let ((beg (point)))
1715 (save-excursion
1716 (picture-yank-rectangle (not overwrite-mode))
1717 (message ""))
1718 (goto-char beg)))
1719 (t
1720 (insert tpu-last-deleted-region)))
1721 (setq num (1- num))))
1722
1723 (defun tpu-undelete-lines (num)
1724 "Insert lines deleted by last TPU-edt line-deletion command.
1725 With argument reinserts lines that many times."
1726 (interactive "p")
1727 (let ((beg (point)))
1728 (while (> num 0)
1729 (insert tpu-last-deleted-lines)
1730 (setq num (1- num)))
1731 (goto-char beg)))
1732
1733 (defun tpu-undelete-words (num)
1734 "Insert words deleted by last TPU-edt word-deletion command.
1735 With argument reinserts words that many times."
1736 (interactive "p")
1737 (let ((beg (point)))
1738 (while (> num 0)
1739 (insert tpu-last-deleted-words)
1740 (setq num (1- num)))
1741 (goto-char beg)))
1742
1743 (defun tpu-undelete-char (num)
1744 "Insert character deleted by last TPU-edt character-deletion command.
1745 With argument reinserts the character that many times."
1746 (interactive "p")
1747 (while (> num 0)
1748 (if overwrite-mode (prog1 (forward-char -1) (delete-char 1)))
1749 (insert tpu-last-deleted-char)
1750 (forward-char -1)
1751 (setq num (1- num))))
1752
1753
1754 ;;;
1755 ;;; Replace and Substitute
1756 ;;;
1757 (defun tpu-replace nil
1758 "Replace the selected region with the contents of the cut buffer."
1759 (interactive)
1760 (cond ((tpu-mark)
1761 (let ((beg (region-beginning)) (end (region-end)))
1762 (setq tpu-last-replaced-text (buffer-substring beg end))
1763 (delete-region beg end)
1764 (insert tpu-last-deleted-region)
1765 (tpu-unselect t)))
1766 ((tpu-check-match)
1767 (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
1768 (setq tpu-last-replaced-text (buffer-substring beg end))
1769 (replace-match tpu-last-deleted-region
1770 (not case-replace) (not tpu-regexp-p))
1771 (tpu-unset-match)))
1772 (t
1773 (tpu-error "No selection active."))))
1774
1775 (defun tpu-substitute (num)
1776 "Replace the selected region with the contents of the cut buffer, and
1777 repeat most recent search. A numeric argument serves as a repeat count.
1778 A negative argument means replace all occurrences of the search string."
1779 (interactive "p")
1780 (cond ((or (tpu-mark) (tpu-check-match))
1781 (while (and (not (= num 0)) (or (tpu-mark) (tpu-check-match)))
1782 (let ((beg (point)))
1783 (tpu-replace)
1784 (if tpu-searching-forward (forward-char -1) (goto-char beg))
1785 (if (= num 1) (tpu-search-internal tpu-search-last-string)
1786 (tpu-search-internal-core tpu-search-last-string)))
1787 (setq num (1- num))))
1788 (t
1789 (tpu-error "No selection active."))))
1790
1791 (defun tpu-lm-replace (from to)
1792 "Interactively search for OLD-string and substitute NEW-string."
1793 (interactive (list (tpu-regexp-prompt "Old String: ")
1794 (tpu-regexp-prompt "New String: ")))
1795
1796 (let ((doit t) (strings 0))
1797
1798 ;; Can't replace null strings
1799 (if (string= "" from) (tpu-error "No string to replace."))
1800
1801 ;; Find the first occurrence
1802 (tpu-set-search)
1803 (tpu-search-internal from t)
1804
1805 ;; Loop on replace question - yes, no, all, last, or quit.
1806 (while doit
1807 (if (not (tpu-check-match)) (setq doit nil)
1808 (progn
1809 (move-overlay tpu-replace-overlay
1810 (tpu-match-beginning) (tpu-match-end) (current-buffer))
1811 (message "Replace? Type Yes, No, All, Last, or Quit: ")
1812 (let ((ans (read-char)))
1813
1814 (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\r) (= ans ?\ ))
1815 (let ((beg (point)))
1816 (replace-match to (not case-replace) (not tpu-regexp-p))
1817 (setq strings (1+ strings))
1818 (if tpu-searching-forward (forward-char -1) (goto-char beg)))
1819 (tpu-search-internal from t))
1820
1821 ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
1822 (tpu-search-internal from t))
1823
1824 ((or (= ans ?a) (= ans ?A))
1825 (save-excursion
1826 (let ((beg (point)))
1827 (replace-match to (not case-replace) (not tpu-regexp-p))
1828 (setq strings (1+ strings))
1829 (if tpu-searching-forward (forward-char -1) (goto-char beg)))
1830 (tpu-search-internal-core from t)
1831 (while (tpu-check-match)
1832 (let ((beg (point)))
1833 (replace-match to (not case-replace) (not tpu-regexp-p))
1834 (setq strings (1+ strings))
1835 (if tpu-searching-forward (forward-char -1) (goto-char beg)))
1836 (tpu-search-internal-core from t)))
1837 (setq doit nil))
1838
1839 ((or (= ans ?l) (= ans ?L))
1840 (let ((beg (point)))
1841 (replace-match to (not case-replace) (not tpu-regexp-p))
1842 (setq strings (1+ strings))
1843 (if tpu-searching-forward (forward-char -1) (goto-char beg)))
1844 (setq doit nil))
1845
1846 ((or (= ans ?q) (= ans ?Q))
1847 (tpu-unset-match)
1848 (setq doit nil)))))))
1849
1850 (move-overlay tpu-replace-overlay 1 1 (current-buffer))
1851 (message "Replaced %s occurrence%s." strings (if (not (= 1 strings)) "s" ""))))
1852
1853 (defun tpu-emacs-replace (&optional dont-ask)
1854 "A TPU-edt interface to the emacs replace functions. If TPU-edt is
1855 currently in regular expression mode, the emacs regular expression
1856 replace functions are used. If an argument is supplied, replacements
1857 are performed without asking. Only works in forward direction."
1858 (interactive "P")
1859 (cond (dont-ask
1860 (setq current-prefix-arg nil)
1861 (call-interactively
1862 (if tpu-regexp-p 'replace-regexp 'replace-string)))
1863 (t
1864 (call-interactively
1865 (if tpu-regexp-p 'query-replace-regexp 'query-replace)))))
1866
1867 (defun tpu-add-at-bol (text)
1868 "Add text to the beginning of each line in a region,
1869 or each line in the entire buffer if no region is selected."
1870 (interactive
1871 (list (tpu-string-prompt "String to add: " 'tpu-add-at-bol-hist)))
1872 (if (string= "" text) (tpu-error "No string specified."))
1873 (cond ((tpu-mark)
1874 (save-excursion
1875 (if (> (point) (tpu-mark)) (exchange-point-and-mark))
1876 (while (and (< (point) (tpu-mark)) (re-search-forward "^" (tpu-mark) t))
1877 (if (< (point) (tpu-mark)) (replace-match text))))
1878 (tpu-unselect t))
1879 (t
1880 (save-excursion
1881 (goto-char (point-min))
1882 (while (and (re-search-forward "^" nil t) (not (eobp)))
1883 (replace-match text))))))
1884
1885 (defun tpu-add-at-eol (text)
1886 "Add text to the end of each line in a region,
1887 or each line of the entire buffer if no region is selected."
1888 (interactive
1889 (list (tpu-string-prompt "String to add: " 'tpu-add-at-eol-hist)))
1890 (if (string= "" text) (tpu-error "No string specified."))
1891 (cond ((tpu-mark)
1892 (save-excursion
1893 (if (> (point) (tpu-mark)) (exchange-point-and-mark))
1894 (while (< (point) (tpu-mark))
1895 (end-of-line)
1896 (if (<= (point) (tpu-mark)) (insert text))
1897 (forward-line)))
1898 (tpu-unselect t))
1899 (t
1900 (save-excursion
1901 (goto-char (point-min))
1902 (while (not (eobp))
1903 (end-of-line) (insert text) (forward-line))))))
1904
1905 (defun tpu-trim-line-ends nil
1906 "Removes trailing whitespace from every line in the buffer."
1907 (interactive)
1908 (save-match-data
1909 (save-excursion
1910 (goto-char (point-min))
1911 (while (re-search-forward "[ \t][ \t]*$" nil t)
1912 (delete-region (match-beginning 0) (match-end 0))))))
1913
1914
1915 ;;;
1916 ;;; Movement by character
1917 ;;;
1918 (defun tpu-char (num)
1919 "Move to the next character in the current direction.
1920 A repeat count means move that many characters."
1921 (interactive "p")
1922 (if tpu-advance (tpu-forward-char num) (tpu-backward-char num)))
1923
1924 (defun tpu-forward-char (num)
1925 "Move right ARG characters (left if ARG is negative)."
1926 (interactive "p")
1927 (forward-char num))
1928
1929 (defun tpu-backward-char (num)
1930 "Move left ARG characters (right if ARG is negative)."
1931 (interactive "p")
1932 (backward-char num))
1933
1934
1935 ;;;
1936 ;;; Movement by word
1937 ;;;
1938 (defvar tpu-word-separator-list '()
1939 "List of additional word separators.")
1940 (defvar tpu-skip-chars "^ \t"
1941 "Characters to skip when moving by word.
1942 Additional word separators are added to this string.")
1943
1944 (defun tpu-word (num)
1945 "Move to the beginning of the next word in the current direction.
1946 A repeat count means move that many words."
1947 (interactive "p")
1948 (if tpu-advance (tpu-forward-to-word num) (tpu-backward-to-word num)))
1949
1950 (defun tpu-forward-to-word (num)
1951 "Move forward until encountering the beginning of a word.
1952 With argument, do this that many times."
1953 (interactive "p")
1954 (while (and (> num 0) (not (eobp)))
1955 (let* ((beg (point))
1956 (end (prog2 (end-of-line) (point) (goto-char beg))))
1957 (cond ((eolp)
1958 (forward-char 1))
1959 ((memq (char-after (point)) tpu-word-separator-list)
1960 (forward-char 1)
1961 (skip-chars-forward " \t" end))
1962 (t
1963 (skip-chars-forward tpu-skip-chars end)
1964 (skip-chars-forward " \t" end))))
1965 (setq num (1- num))))
1966
1967 (defun tpu-backward-to-word (num)
1968 "Move backward until encountering the beginning of a word.
1969 With argument, do this that many times."
1970 (interactive "p")
1971 (while (and (> num 0) (not (bobp)))
1972 (let* ((beg (point))
1973 (end (prog2 (beginning-of-line) (point) (goto-char beg))))
1974 (cond ((bolp)
1975 ( forward-char -1))
1976 ((memq (char-after (1- (point))) tpu-word-separator-list)
1977 (forward-char -1))
1978 (t
1979 (skip-chars-backward " \t" end)
1980 (skip-chars-backward tpu-skip-chars end)
1981 (if (and (not (bolp)) (= ? (char-syntax (char-after (point)))))
1982 (forward-char -1)))))
1983 (setq num (1- num))))
1984
1985 (defun tpu-add-word-separators (separators)
1986 "Add new word separators for TPU-edt word commands."
1987 (interactive "sSeparators: ")
1988 (let* ((n 0) (length (length separators)))
1989 (while (< n length)
1990 (let ((char (aref separators n))
1991 (ss (substring separators n (1+ n))))
1992 (cond ((not (memq char tpu-word-separator-list))
1993 (setq tpu-word-separator-list
1994 (append ss tpu-word-separator-list))
1995 (cond ((= char ?-)
1996 (setq tpu-skip-chars (concat tpu-skip-chars "\\-")))
1997 ((= char ?\\)
1998 (setq tpu-skip-chars (concat tpu-skip-chars "\\\\")))
1999 ((= char ?^)
2000 (setq tpu-skip-chars (concat tpu-skip-chars "\\^")))
2001 (t
2002 (setq tpu-skip-chars (concat tpu-skip-chars ss))))))
2003 (setq n (1+ n))))))
2004
2005 (defun tpu-reset-word-separators nil
2006 "Reset word separators to default value."
2007 (interactive)
2008 (setq tpu-word-separator-list nil)
2009 (setq tpu-skip-chars "^ \t"))
2010
2011 (defun tpu-set-word-separators (separators)
2012 "Set new word separators for TPU-edt word commands."
2013 (interactive "sSeparators: ")
2014 (tpu-reset-word-separators)
2015 (tpu-add-word-separators separators))
2016
2017
2018 ;;;
2019 ;;; Movement by line
2020 ;;;
2021 (defun tpu-next-line (num)
2022 "Move to next line.
2023 Prefix argument serves as a repeat count."
2024 (interactive "p")
2025 (next-line-internal num)
2026 (setq this-command 'next-line))
2027
2028 (defun tpu-previous-line (num)
2029 "Move to previous line.
2030 Prefix argument serves as a repeat count."
2031 (interactive "p")
2032 (next-line-internal (- num))
2033 (setq this-command 'previous-line))
2034
2035 (defun tpu-next-beginning-of-line (num)
2036 "Move to beginning of line; if at beginning, move to beginning of next line.
2037 Accepts a prefix argument for the number of lines to move."
2038 (interactive "p")
2039 (backward-char 1)
2040 (forward-visible-line (- 1 num)))
2041
2042 (defun tpu-end-of-line (num)
2043 "Move to the next end of line in the current direction.
2044 A repeat count means move that many lines."
2045 (interactive "p")
2046 (if tpu-advance (tpu-next-end-of-line num) (tpu-previous-end-of-line num)))
2047
2048 (defun tpu-next-end-of-line (num)
2049 "Move to end of line; if at end, move to end of next line.
2050 Accepts a prefix argument for the number of lines to move."
2051 (interactive "p")
2052 (forward-char 1)
2053 (end-of-line num))
2054
2055 (defun tpu-previous-end-of-line (num)
2056 "Move EOL upward.
2057 Accepts a prefix argument for the number of lines to move."
2058 (interactive "p")
2059 (end-of-line (- 1 num)))
2060
2061 (defun tpu-current-end-of-line nil
2062 "Move point to end of current line."
2063 (interactive)
2064 (let ((beg (point)))
2065 (end-of-line)
2066 (if (= beg (point)) (message "You are already at the end of a line."))))
2067
2068 (defun tpu-line (num)
2069 "Move to the beginning of the next line in the current direction.
2070 A repeat count means move that many lines."
2071 (interactive "p")
2072 (if tpu-advance (tpu-forward-line num) (tpu-backward-line num)))
2073
2074 (defun tpu-forward-line (num)
2075 "Move to beginning of next line.
2076 Prefix argument serves as a repeat count."
2077 (interactive "p")
2078 (forward-line num))
2079
2080 (defun tpu-backward-line (num)
2081 "Move to beginning of previous line.
2082 Prefix argument serves as repeat count."
2083 (interactive "p")
2084 (or (bolp) (>= 0 num) (setq num (- num 1)))
2085 (forward-line (- num)))
2086
2087
2088 ;;;
2089 ;;; Movement by paragraph
2090 ;;;
2091 (defun tpu-paragraph (num)
2092 "Move to the next paragraph in the current direction.
2093 A repeat count means move that many paragraphs."
2094 (interactive "p")
2095 (if tpu-advance
2096 (tpu-next-paragraph num) (tpu-previous-paragraph num)))
2097
2098 (defun tpu-next-paragraph (num)
2099 "Move to beginning of the next paragraph.
2100 Accepts a prefix argument for the number of paragraphs."
2101 (interactive "p")
2102 (beginning-of-line)
2103 (while (and (not (eobp)) (> num 0))
2104 (if (re-search-forward "^[ \t]*$" nil t)
2105 (if (re-search-forward "[^ \t\n]" nil t)
2106 (goto-char (match-beginning 0))
2107 (goto-char (point-max))))
2108 (setq num (1- num)))
2109 (beginning-of-line))
2110
2111
2112 (defun tpu-previous-paragraph (num)
2113 "Move to beginning of previous paragraph.
2114 Accepts a prefix argument for the number of paragraphs."
2115 (interactive "p")
2116 (end-of-line)
2117 (while (and (not (bobp)) (> num 0))
2118 (if (not (and (re-search-backward "^[ \t]*$" nil t)
2119 (re-search-backward "[^ \t\n]" nil t)
2120 (re-search-backward "^[ \t]*$" nil t)
2121 (progn (re-search-forward "[^ \t\n]" nil t)
2122 (goto-char (match-beginning 0)))))
2123 (goto-char (point-min)))
2124 (setq num (1- num)))
2125 (beginning-of-line))
2126
2127
2128 ;;;
2129 ;;; Movement by page
2130 ;;;
2131 (defun tpu-page (num)
2132 "Move to the next page in the current direction.
2133 A repeat count means move that many pages."
2134 (interactive "p")
2135 (if tpu-advance (forward-page num) (backward-page num))
2136 (if (eobp) (recenter -1)))
2137
2138
2139 ;;;
2140 ;;; Scrolling and movement within the buffer
2141 ;;;
2142 (defun tpu-scroll-window (num)
2143 "Scroll the display to the next section in the current direction.
2144 A repeat count means scroll that many sections."
2145 (interactive "p")
2146 (if tpu-advance (tpu-scroll-window-up num) (tpu-scroll-window-down num)))
2147
2148 (defun tpu-scroll-window-down (num)
2149 "Scroll the display down to the next section.
2150 A repeat count means scroll that many sections."
2151 (interactive "p")
2152 (let* ((beg (tpu-current-line))
2153 (height (1- (window-height)))
2154 (lines (* num (/ (* height tpu-percent-scroll) 100))))
2155 (next-line-internal (- lines))
2156 (if (> lines beg) (recenter 0))))
2157
2158 (defun tpu-scroll-window-up (num)
2159 "Scroll the display up to the next section.
2160 A repeat count means scroll that many sections."
2161 (interactive "p")
2162 (let* ((beg (tpu-current-line))
2163 (height (1- (window-height)))
2164 (lines (* num (/ (* height tpu-percent-scroll) 100))))
2165 (next-line-internal lines)
2166 (if (>= (+ lines beg) height) (recenter -1))))
2167
2168 (defun tpu-pan-right (num)
2169 "Pan right tpu-pan-columns (16 by default).
2170 Accepts a prefix argument for the number of tpu-pan-columns to scroll."
2171 (interactive "p")
2172 (scroll-left (* tpu-pan-columns num)))
2173
2174 (defun tpu-pan-left (num)
2175 "Pan left tpu-pan-columns (16 by default).
2176 Accepts a prefix argument for the number of tpu-pan-columns to scroll."
2177 (interactive "p")
2178 (scroll-right (* tpu-pan-columns num)))
2179
2180 (defun tpu-move-to-beginning nil
2181 "Move cursor to the beginning of buffer, but don't set the mark."
2182 (interactive)
2183 (goto-char (point-min)))
2184
2185 (defun tpu-move-to-end nil
2186 "Move cursor to the end of buffer, but don't set the mark."
2187 (interactive)
2188 (goto-char (point-max))
2189 (recenter -1))
2190
2191 (defun tpu-goto-percent (perc)
2192 "Move point to ARG percentage of the buffer."
2193 (interactive "NGoto-percentage: ")
2194 (if (or (> perc 100) (< perc 0))
2195 (tpu-error "Percentage %d out of range 0 < percent < 100." perc)
2196 (goto-char (/ (* (point-max) perc) 100))))
2197
2198 (defun tpu-beginning-of-window nil
2199 "Move cursor to top of window."
2200 (interactive)
2201 (move-to-window-line 0))
2202
2203 (defun tpu-end-of-window nil
2204 "Move cursor to bottom of window."
2205 (interactive)
2206 (move-to-window-line -1))
2207
2208 (defun tpu-line-to-bottom-of-window nil
2209 "Move the current line to the bottom of the window."
2210 (interactive)
2211 (recenter -1))
2212
2213 (defun tpu-line-to-top-of-window nil
2214 "Move the current line to the top of the window."
2215 (interactive)
2216 (recenter 0))
2217
2218
2219 ;;;
2220 ;;; Direction
2221 ;;;
2222 (defun tpu-advance-direction nil
2223 "Set TPU Advance mode so keypad commands move forward."
2224 (interactive)
2225 (setq tpu-direction-string " Advance")
2226 (setq tpu-advance t)
2227 (setq tpu-reverse nil)
2228 (tpu-set-search)
2229 (tpu-update-mode-line))
2230
2231 (defun tpu-backup-direction nil
2232 "Set TPU Backup mode so keypad commands move backward."
2233 (interactive)
2234 (setq tpu-direction-string " Reverse")
2235 (setq tpu-advance nil)
2236 (setq tpu-reverse t)
2237 (tpu-set-search)
2238 (tpu-update-mode-line))
2239
2240 (defun tpu-toggle-direction nil
2241 "Change the current TPU direction."
2242 (interactive)
2243 (if tpu-advance (tpu-backup-direction) (tpu-advance-direction)))
2244
2245
2246 ;;;
2247 ;;; Minibuffer map additions to make KP_enter = RET
2248 ;;;
2249 ;; Standard Emacs settings under xterm in function-key-map map
2250 ;; "\eOM" to [kp-enter] and [kp-enter] to RET, but since the output of the map
2251 ;; is not fed back into the map, the key stays as kp-enter :-(.
2252 (define-key minibuffer-local-map [kp-enter] 'exit-minibuffer)
2253 ;; These are not necessary because they are inherited.
2254 ;; (define-key minibuffer-local-ns-map [kp-enter] 'exit-minibuffer)
2255 ;; (define-key minibuffer-local-completion-map [kp-enter] 'exit-minibuffer)
2256 (define-key minibuffer-local-must-match-map [kp-enter] 'minibuffer-complete-and-exit)
2257
2258
2259 ;;;
2260 ;;; Minibuffer map additions to set search direction
2261 ;;;
2262 (define-key minibuffer-local-map "\eOt" 'tpu-search-forward-exit) ;KP4
2263 (define-key minibuffer-local-map "\eOu" 'tpu-search-backward-exit) ;KP5
2264
2265
2266 ;;;
2267 ;;; Functions to set, reset, and toggle the control key bindings
2268 ;;;
2269 (defun tpu-set-control-keys nil
2270 "Set control keys to TPU style functions."
2271 (define-key global-map "\C-\\" 'quoted-insert) ; ^\
2272 (define-key global-map "\C-a" 'tpu-toggle-overwrite-mode) ; ^A
2273 (define-key global-map "\C-b" 'repeat-complex-command) ; ^B
2274 (define-key global-map "\C-e" 'tpu-current-end-of-line) ; ^E
2275 (define-key global-map "\C-h" 'tpu-next-beginning-of-line) ; ^H (BS)
2276 (define-key global-map "\C-j" 'tpu-delete-previous-word) ; ^J (LF)
2277 (define-key global-map "\C-k" 'tpu-define-macro-key) ; ^K
2278 (define-key global-map "\C-l" 'tpu-insert-formfeed) ; ^L (FF)
2279 (define-key global-map "\C-r" 'recenter) ; ^R
2280 (define-key global-map "\C-u" 'tpu-delete-to-bol) ; ^U
2281 (define-key global-map "\C-v" 'tpu-quoted-insert) ; ^V
2282 (define-key global-map "\C-w" 'redraw-display) ; ^W
2283 (define-key global-map "\C-z" 'tpu-exit) ; ^Z
2284 (setq tpu-control-keys t))
2285
2286 (defun tpu-reset-control-keys (tpu-style)
2287 "Set control keys to TPU or emacs style functions."
2288 (let* ((tpu (and tpu-style (not tpu-control-keys)))
2289 (emacs (and (not tpu-style) tpu-control-keys))
2290 (doit (or tpu emacs)))
2291 (cond (doit
2292 (if emacs (setq tpu-global-map (copy-keymap global-map)))
2293 (let ((map (if tpu tpu-global-map tpu-original-global-map)))
2294
2295 (define-key global-map "\C-\\" (lookup-key map "\C-\\")) ; ^\
2296 (define-key global-map "\C-a" (lookup-key map "\C-a")) ; ^A
2297 (define-key global-map "\C-b" (lookup-key map "\C-b")) ; ^B
2298 (define-key global-map "\C-e" (lookup-key map "\C-e")) ; ^E
2299 (define-key global-map "\C-h" (lookup-key map "\C-h")) ; ^H (BS)
2300 (define-key global-map "\C-j" (lookup-key map "\C-j")) ; ^J (LF)
2301 (define-key global-map "\C-k" (lookup-key map "\C-k")) ; ^K
2302 (define-key global-map "\C-l" (lookup-key map "\C-l")) ; ^L (FF)
2303 (define-key global-map "\C-r" (lookup-key map "\C-r")) ; ^R
2304 (define-key global-map "\C-u" (lookup-key map "\C-u")) ; ^U
2305 (define-key global-map "\C-v" (lookup-key map "\C-v")) ; ^V
2306 (define-key global-map "\C-w" (lookup-key map "\C-w")) ; ^W
2307 (define-key global-map "\C-z" (lookup-key map "\C-z")) ; ^Z
2308 (setq tpu-control-keys tpu-style))))))
2309
2310 (defun tpu-toggle-control-keys nil
2311 "Toggles control key bindings between TPU-edt and Emacs."
2312 (interactive)
2313 (tpu-reset-control-keys (not tpu-control-keys))
2314 (and (interactive-p)
2315 (message "Control keys function with %s bindings."
2316 (if tpu-control-keys "TPU-edt" "Emacs"))))
2317
2318
2319 ;;;
2320 ;;; Emacs version 19 minibuffer history support
2321 ;;;
2322 (defun tpu-next-history-element (n)
2323 "Insert the next element of the minibuffer history into the minibuffer."
2324 (interactive "p")
2325 (next-history-element n)
2326 (goto-char (point-max)))
2327
2328 (defun tpu-previous-history-element (n)
2329 "Insert the previous element of the minibuffer history into the minibuffer."
2330 (interactive "p")
2331 (previous-history-element n)
2332 (goto-char (point-max)))
2333
2334 (defun tpu-arrow-history nil
2335 "Modify minibuffer maps to use arrows for history recall."
2336 (interactive)
2337 (dolist (cur (where-is-internal 'tpu-previous-line))
2338 (define-key read-expression-map cur 'tpu-previous-history-element)
2339 (define-key minibuffer-local-map cur 'tpu-previous-history-element)
2340 ;; These are inherited anyway. --Stef
2341 ;; (define-key minibuffer-local-ns-map cur 'tpu-previous-history-element)
2342 ;; (define-key minibuffer-local-completion-map cur 'tpu-previous-history-element)
2343 ;; (define-key minibuffer-local-must-match-map cur 'tpu-previous-history-element)
2344 )
2345
2346 (dolist (cur (where-is-internal 'tpu-next-line))
2347 (define-key read-expression-map cur 'tpu-next-history-element)
2348 (define-key minibuffer-local-map cur 'tpu-next-history-element)
2349 ;; These are inherited anyway. --Stef
2350 ;; (define-key minibuffer-local-ns-map cur 'tpu-next-history-element)
2351 ;; (define-key minibuffer-local-completion-map cur 'tpu-next-history-element)
2352 ;; (define-key minibuffer-local-must-match-map cur 'tpu-next-history-element)
2353 ))
2354
2355
2356 ;;;
2357 ;;; Emacs version 19 X-windows key definition support
2358 ;;;
2359 (defun tpu-load-xkeys (file)
2360 "Load the TPU-edt X-windows key definitions FILE.
2361 If FILE is nil, try to load a default file. The default file names are
2362 `~/.tpu-lucid-keys' for Lucid emacs, and `~/.tpu-keys' for Emacs."
2363 (interactive "fX key definition file: ")
2364 (cond (file
2365 (setq file (expand-file-name file)))
2366 (tpu-xkeys-file
2367 (setq file (expand-file-name tpu-xkeys-file)))
2368 (tpu-lucid-emacs-p
2369 (setq file (convert-standard-filename
2370 (expand-file-name "~/.tpu-lucid-keys"))))
2371 (t
2372 (setq file (convert-standard-filename
2373 (expand-file-name "~/.tpu-keys")))
2374 (and (not (file-exists-p file))
2375 (file-exists-p
2376 (convert-standard-filename
2377 (expand-file-name "~/.tpu-gnu-keys")))
2378 (tpu-copy-keyfile
2379 (convert-standard-filename
2380 (expand-file-name "~/.tpu-gnu-keys")) file))))
2381 (cond ((file-readable-p file)
2382 (load-file file))
2383 (t
2384 (switch-to-buffer "*scratch*")
2385 (erase-buffer)
2386 (insert "
2387
2388 Ack!! You're running TPU-edt under X-windows without loading an
2389 X key definition file. To create a TPU-edt X key definition
2390 file, run the tpu-mapper.el program. It came with TPU-edt. It
2391 even includes directions on how to use it! Perhaps it's lying
2392 around here someplace. ")
2393 (let ((file "tpu-mapper.el")
2394 (found nil)
2395 (path nil)
2396 (search-list (append (list (expand-file-name ".")) load-path)))
2397 (while (and (not found) search-list)
2398 (setq path (concat (car search-list)
2399 (if (string-match "/$" (car search-list)) "" "/")
2400 file))
2401 (if (and (file-exists-p path) (not (file-directory-p path)))
2402 (setq found t))
2403 (setq search-list (cdr search-list)))
2404 (cond (found
2405 (insert (format
2406 "Ah yes, there it is, in \n\n %s \n\n" path))
2407 (if (tpu-y-or-n-p "Do you want to run it now? ")
2408 (load-file path)))
2409 (t
2410 (insert "Nope, I can't seem to find it. :-(\n\n")
2411 (sit-for 120)))))))
2412
2413 (defun tpu-copy-keyfile (oldname newname)
2414 "Copy the TPU-edt X key definitions file to the new default name."
2415 (interactive "fOld name: \nFNew name: ")
2416 (if (not (get-buffer "*TPU-Notice*")) (generate-new-buffer "*TPU-Notice*"))
2417 (set-buffer "*TPU-Notice*")
2418 (erase-buffer)
2419 (insert "
2420 NOTICE --
2421
2422 The default name of the TPU-edt key definition file has changed
2423 from `~/.tpu-gnu-keys' to `~/.tpu-keys'. With your permission,
2424 your key definitions will be copied to the new file. If you'll
2425 never use older versions of Emacs, you can remove the old file.
2426 If the copy fails, you'll be asked if you want to create a new
2427 key definitions file. Do you want to copy your key definition
2428 file now?
2429 ")
2430 (save-window-excursion
2431 (switch-to-buffer-other-window "*TPU-Notice*")
2432 (shrink-window-if-larger-than-buffer)
2433 (goto-char (point-min))
2434 (beep)
2435 (and (tpu-y-or-n-p "Copy key definitions to the new file now? ")
2436 (condition-case conditions
2437 (copy-file oldname newname)
2438 (tpu-error (message "Sorry, couldn't copy - %s." (cdr conditions)))))
2439 (kill-buffer "*TPU-Notice*")))
2440
2441
2442 ;;;
2443 ;;; Start and Stop TPU-edt
2444 ;;;
2445 ;;;###autoload
2446 (defun tpu-edt-on ()
2447 "Turn on TPU/edt emulation."
2448 (interactive)
2449 (and window-system (tpu-load-xkeys nil))
2450 (tpu-arrow-history)
2451 (transient-mark-mode t)
2452 (add-hook 'post-command-hook 'tpu-search-highlight)
2453 (tpu-set-mode-line t)
2454 (tpu-advance-direction)
2455 ;; set page delimiter, display line truncation, and scrolling like TPU
2456 (setq-default page-delimiter "\f")
2457 (setq-default truncate-lines t)
2458 (setq scroll-step 1)
2459 (setq global-map (copy-keymap global-map))
2460 (tpu-set-control-keys)
2461 (define-key global-map "\e[" CSI-map)
2462 (define-key global-map "\eO" SS3-map)
2463 (setq tpu-edt-mode t))
2464
2465 (defun tpu-edt-off ()
2466 "Turn off TPU/edt emulation. Note that the keypad is left on."
2467 (interactive)
2468 (tpu-reset-control-keys nil)
2469 (remove-hook 'post-command-hook 'tpu-search-highlight)
2470 (tpu-set-mode-line nil)
2471 (setq-default page-delimiter "^\f")
2472 (setq-default truncate-lines nil)
2473 (setq scroll-step 0)
2474 (setq global-map tpu-original-global-map)
2475 (use-global-map global-map)
2476 (setq tpu-edt-mode nil))
2477
2478 (provide 'tpu-edt)
2479
2480 ;; arch-tag: f3dfe61c-2cbd-4f73-b9cc-eb215020b857
2481 ;;; tpu-edt.el ends here