(Creating Hash, Hash Access, Defining Hash, Other Hash): Remove @tindex.
[bpt/emacs.git] / lisp / emulation / tpu-edt.el
index cbe0144..878ac56 100644 (file)
@@ -1,11 +1,12 @@
 ;;; tpu-edt.el --- Emacs emulating TPU emulating EDT
 
-;; Copyright (C) 1993 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994, 1995, 2000, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
 
 ;; Author: Rob Riepel <riepel@networking.stanford.edu>
 ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
-;; Version: 3.1
-;; Keywords: edt emulations
+;; Version: 4.5
+;; Keywords: emulations
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;; TPU-edt is based on tpu.el by Jeff Kowalski and Bob Covey.
+
+;;; Commentary:
+
+;; %% TPU-edt -- Emacs emulating TPU emulating EDT
+
+;; %% Contents
+
+;;  % Introduction
+;;  % Differences Between TPU-edt and DEC TPU/edt
+;;  % Starting TPU-edt
+;;  % Customizing TPU-edt using the Emacs Initialization File
+;;  % Regular Expressions in TPU-edt
+
+
+;; %% Introduction
+
+;;    TPU-edt emulates the popular DEC VMS editor EDT (actually, it emulates
+;;    DEC TPU's EDT emulation, hence the name TPU-edt).  TPU-edt features the
+;;    following TPU/edt functionality:
+
+;;     .  EDT keypad
+;;     .  On-line help
+;;     .  Repeat counts
+;;     .  Scroll margins
+;;     .  Learn sequences
+;;     .  Free cursor mode
+;;     .  Rectangular cut and paste
+;;     .  Multiple windows and buffers
+;;     .  TPU line-mode REPLACE command
+;;     .  Wild card search and substitution
+;;     .  Configurable through an initialization file
+;;     .  History recall of search strings, file names, and commands
+
+;;    Please note that TPU-edt does NOT emulate TPU.  It emulates TPU's EDT
+;;    emulation.  Very few TPU line-mode commands are supported.
+
+;;    TPU-edt, like its VMS cousin, works on VT-series terminals with DEC
+;;    style keyboards.  VT terminal emulators, including xterm with the
+;;    appropriate key translations, work just fine too.
+
+;;    TPU-edt works with X-windows.  This is accomplished through a TPU-edt X
+;;    key map.  The TPU-edt module tpu-mapper creates this map and stores it
+;;    in a file.  Tpu-mapper will be run automatically the first time you
+;;    invoke the X-windows version of emacs, or you can run it by hand.  See
+;;    the commentary in tpu-mapper.el for details.
+
+
+;; %% Differences Between TPU-edt and DEC TPU/edt
+
+;;    In some cases, Emacs doesn't support text highlighting, so selected
+;;    regions are not shown in inverse video.  Emacs uses the concept of "the
+;;    mark".  The mark is set at one end of a selected region; the cursor is
+;;    at the other.  In cases where the selected region cannot be shown in
+;;    inverse video an at sign (@) appears in the mode line when mark is set.
+;;    The native emacs command ^X^X (Control-X twice) exchanges the cursor
+;;    with the mark; this provides a handy way to find the location of the
+;;    mark.
+
+;;    In TPU the cursor can be either bound or free.  Bound means the cursor
+;;    cannot wander outside the text of the file being edited.  Free means
+;;    the arrow keys can move the cursor past the ends of lines.  Free is the
+;;    default mode in TPU; bound is the only mode in EDT.  Bound is the only
+;;    mode in the base version of TPU-edt; optional extensions add an
+;;    approximation of free mode, see the commentary in tpu-extras.el for
+;;    details.
+
+;;    Like TPU, emacs uses multiple buffers.  Some buffers are used to hold
+;;    files you are editing; other "internal" buffers are used for emacs' own
+;;    purposes (like showing you help).  Here are some commands for dealing
+;;    with buffers.
+
+;;       Gold-B   moves to next buffer, including internal buffers
+;;       Gold-N   moves to next buffer containing a file
+;;       Gold-M   brings up a buffer menu (like TPU "show buffers")
+
+;;    Emacs is very fond of throwing up new windows.  Dealing with all these
+;;    windows can be a little confusing at first, so here are a few commands
+;;    to that may help:
+
+;;       Gold-Next_Scr  moves to the next window on the screen
+;;       Gold-Prev_Scr  moves to the previous window on the screen
+;;       Gold-TAB       also moves to the next window on the screen
+
+;;       Control-x 1    deletes all but the current window
+;;       Control-x 0    deletes the current window
+
+;;    Note that the buffers associated with deleted windows still exist!
+
+;;    Like TPU, TPU-edt has a "command" function, invoked with Gold-KP7 or
+;;    Do.  Most of the commands available are emacs commands.  Some TPU
+;;    commands are available, they are: replace, exit, quit, include, and
+;;    Get (unfortunately, "get" is an internal emacs function, so we are
+;;    stuck with "Get" - to make life easier, Get is available as Gold-g).
+
+;;    TPU-edt supports the recall of commands, file names, and search
+;;    strings.  The history of strings recalled differs slightly from
+;;    TPU/edt, but it is still very convenient.
+
+;;    Help is available!  The traditional help keys (Help and PF2) display
+;;    a small help file showing the default keypad layout, control key
+;;    functions, and Gold key functions.  Pressing any key inside of help
+;;    splits the screen and prints a description of the function of the
+;;    pressed key.  Gold-PF2 invokes the native emacs help, with its
+;;    zillions of options.
+
+;;    Thanks to emacs, TPU-edt has some extensions that may make your life
+;;    easier, or at least more interesting.  For example, Gold-r toggles
+;;    TPU-edt rectangular mode.  In rectangular mode, Remove and Insert work
+;;    on rectangles.  Likewise, Gold-* toggles TPU-edt regular expression
+;;    mode.  In regular expression mode Find, Find Next, and the line-mode
+;;    replace command work with regular expressions.  [A regular expression
+;;    is a pattern that denotes a set of strings; like VMS wildcards.]
+
+;;    Emacs also gives TPU-edt the undo and occur functions.  Undo does
+;;    what it says; it undoes the last change.  Multiple undos in a row
+;;    undo multiple changes.  For your convenience, undo is available on
+;;    Gold-u.  Occur shows all the lines containing a specific string in
+;;    another window.  Moving to that window, and typing ^C^C (Control-C
+;;    twice) on a particular line moves you back to the original window
+;;    at that line.  Occur is on Gold-o.
+
+;;    Finally, as you edit, remember that all the power of emacs is at
+;;    your disposal.  It really is a fantastic tool.  You may even want to
+;;    take some time and read the emacs tutorial; perhaps not to learn the
+;;    native emacs key bindings, but to get a feel for all the things
+;;    emacs can do for you.  The emacs tutorial is available from the
+;;    emacs help function: "Gold-PF2 t"
+
+
+;; %% Starting TPU-edt
+
+;;    All you have to do to start TPU-edt, is turn it on.  This can be
+;;    done from the command line when running emacs.
+
+;;        prompt> emacs -f tpu-edt
+
+;;    If you've already started emacs, turn on TPU-edt using the tpu-edt
+;;    command.  First press `M-x' (that's usually `ESC' followed by `x')
+;;    and type `tpu-edt' followed by a carriage return.
+
+;;    If you like TPU-edt and want to use it all the time, you can start
+;;    TPU-edt using the emacs initialization file, .emacs.  Simply create
+;;    a .emacs file in your home directory containing the line:
+
+;;        (tpu-edt)
+
+;;    That's all you need to do to start TPU-edt.
+
+
+;; %% Customizing TPU-edt using the Emacs Initialization File
+
+;;    The following is a sample emacs initialization file.  It shows how to
+;;    invoke TPU-edt, and how to customize it.
+
+;;    ; .emacs - a sample emacs initialization file
+
+;;    ; Turn on TPU-edt
+;;    (tpu-edt)
+
+;;    ; Set scroll margins 10% (top) and 15% (bottom).
+;;    (tpu-set-scroll-margins "10%" "15%")
+
+;;    ; Load the vtxxx terminal control functions.
+;;    (load "vt-control" t)
+
+;;    ; TPU-edt treats words like EDT; here's how to add word separators.
+;;    ; Note that backslash (\) and double quote (") are quoted with '\'.
+;;    (tpu-add-word-separators "]\\[-_,.\"=+()'/*#:!&;$")
+
+;;    ; Emacs is happy to save files without a final newline; other Unix
+;;    ; programs hate that!  Here we make sure that files end with newlines.
+;;    (setq require-final-newline t)
+
+;;    ; Emacs uses Control-s and Control-q.  Problems can occur when using
+;;    ; emacs on terminals that use these codes for flow control (Xon/Xoff
+;;    ; flow control).  These lines disable emacs' use of these characters.
+;;    (global-unset-key "\C-s")
+;;    (global-unset-key "\C-q")
+
+;;    ; The emacs universal-argument function is very useful.
+;;    ; This line maps universal-argument to Gold-PF1.
+;;    (define-key GOLD-SS3-map "P" 'universal-argument)          ; Gold-PF1
+
+;;    ; Make KP7 move by paragraphs, instead of pages.
+;;    (define-key SS3-map "w" 'tpu-paragraph)                    ; KP7
+
+;;    ; Repeat the preceding mappings for X-windows.
+;;    (cond
+;;     (window-system
+;;      (global-set-key [kp_7] 'tpu-paragraph)                   ; KP7
+;;      (define-key GOLD-map [kp_f1] 'universal-argument)))      ; GOLD-PF1
+
+;;    ; Display the TPU-edt version.
+;;    (tpu-version)
+
+
+;; %% Regular Expressions in TPU-edt
+
+;;    Gold-* toggles TPU-edt regular expression mode.  In regular expression
+;;    mode, find, find next, replace, and substitute accept emacs regular
+;;    expressions.  A complete list of emacs regular expressions can be found
+;;    using the emacs "info" command (it's somewhat like the VMS help
+;;    command).  Try the following sequence of commands:
+
+;;        DO info             <enter info mode>
+;;        m emacs             <select the "emacs" topic>
+;;        m regexs            <select the "regular expression" topic>
+
+;;    Type "q" to quit out of info mode.
+
+;;    There is a problem in regular expression mode when searching for empty
+;;    strings, like beginning-of-line (^) and end-of-line ($).  When searching
+;;    for these strings, find-next may find the current string, instead of the
+;;    next one.  This can cause global replace and substitute commands to loop
+;;    forever in the same location.  For this reason, commands like
+
+;;        replace "^" "> "       <add "> " to beginning of line>
+;;        replace "$" "00711"    <add "00711" to end of line>
+
+;;    may not work properly.
+
+;;    Commands like those above are very useful for adding text to the
+;;    beginning or end of lines.  They might work on a line-by-line basis, but
+;;    go into an infinite loop if the "all" response is specified.  If the
+;;    goal is to add a string to the beginning or end of a particular set of
+;;    lines TPU-edt provides functions to do this.
+
+;;        Gold-^  Add a string at BOL in region or buffer
+;;        Gold-$  Add a string at EOL in region or buffer
+
+;;    There is also a TPU-edt interface to the native emacs string replacement
+;;    commands.  Gold-/ invokes this command.  It accepts regular expressions
+;;    if TPU-edt is in regular expression mode.  Given a repeat count, it will
+;;    perform the replacement without prompting for confirmation.
+
+;;    This command replaces empty strings correctly, however, it has its
+;;    drawbacks.  As a native emacs command, it has a different interface
+;;    than the emulated TPU commands.  Also, it works only in the forward
+;;    direction, regardless of the current TPU-edt direction.
+
+;;; Todo/Bugs:
+
+;; We shouldn't use vt100 ESC sequences since it is uselessly fighting
+;; against function-key-map.  Better use real key names.
 
 ;;; Code:
 
+;; we use picture-mode functions
+(require 'picture)
+
+(defgroup tpu nil
+  "Emacs emulating TPU emulating EDT."
+  :prefix "tpu-"
+  :group 'emulations)
+
 
 ;;;
-;;;  Revision and Version Information
+;;;  Version Information
 ;;;
-(defconst tpu-version "3.1" "TPU-edt version number.")
-(defconst tpu-revision "$Revision: 6.11 $" "Revision number of TPU-edt.")
+(defconst tpu-version "4.5" "TPU-edt version number.")
 
 
 ;;;
 ;;;  User Configurable Variables
 ;;;
-(defconst tpu-have-ispell t
-  "*If non-nil (default), TPU-edt uses ispell for spell checking.")
+(defcustom tpu-have-ispell t
+  "*If non-nil (default), TPU-edt uses ispell for spell checking."
+  :type 'boolean
+  :group 'tpu)
 
-(defconst tpu-kill-buffers-silently nil
-  "*If non-nil, TPU-edt kills modified buffers without asking.")
+(defcustom tpu-kill-buffers-silently nil
+  "*If non-nil, TPU-edt kills modified buffers without asking."
+  :type 'boolean
+  :group 'tpu)
 
-(defvar tpu-percent-scroll 75
-  "*Percentage of the screen to scroll for next/previous screen commands.")
+(defcustom tpu-percent-scroll 75
+  "*Percentage of the screen to scroll for next/previous screen commands."
+  :type 'integer
+  :group 'tpu)
 
-(defvar tpu-pan-columns 16
-  "*Number of columns the tpu-pan functions scroll left or right.")
+(defcustom tpu-pan-columns 16
+  "*Number of columns the tpu-pan functions scroll left or right."
+  :type 'integer
+  :group 'tpu)
 
 
 ;;;
 ;;;  Emacs version identifiers - currently referenced by
 ;;;
 ;;;     o tpu-mark              o tpu-set-mark
-;;;     o tpu-string-prompt     o tpu-regexp-prompt
-;;;     o tpu-edt-on            o tpu-load-xkeys
-;;;     o tpu-update-mode-line  o mode line section
+;;;     o mode line section     o tpu-load-xkeys
 ;;;
-(defconst tpu-emacs19-p (not (string-lessp emacs-version "19"))
-  "Non-NIL if we are running Lucid or GNU Emacs version 19.")
-
-(defconst tpu-gnu-emacs18-p (not tpu-emacs19-p)
-  "Non-NIL if we are running GNU Emacs version 18.")
-
-(defconst tpu-lucid-emacs19-p
-  (and tpu-emacs19-p (string-match "Lucid" emacs-version))
-  "Non-NIL if we are running Lucid Emacs version 19.")
-
-(defconst tpu-gnu-emacs19-p (and tpu-emacs19-p (not tpu-lucid-emacs19-p))
-  "Non-NIL if we are running GNU Emacs version 19.")
-
+(defconst tpu-lucid-emacs-p
+  (string-match "Lucid" emacs-version)
+  "Non-nil if we are running Lucid Emacs.")
 
 ;;;
 ;;;  Global Keymaps
 ;;;
-(defvar CSI-map (make-sparse-keymap)
+(defvar CSI-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "A" 'tpu-previous-line)               ; up
+    (define-key map "B" 'tpu-next-line)                   ; down
+    (define-key map "D" 'tpu-backward-char)               ; left
+    (define-key map "C" 'tpu-forward-char)                ; right
+
+    (define-key map "1~" 'tpu-search)                     ; Find
+    (define-key map "2~" 'tpu-paste)                      ; Insert Here
+    (define-key map "3~" 'tpu-cut)                        ; Remove
+    (define-key map "4~" 'tpu-select)                     ; Select
+    (define-key map "5~" 'tpu-scroll-window-down)         ; Prev Screen
+    (define-key map "6~" 'tpu-scroll-window-up)           ; Next Screen
+
+    (define-key map "11~" 'nil)                           ; F1
+    (define-key map "12~" 'nil)                           ; F2
+    (define-key map "13~" 'nil)                           ; F3
+    (define-key map "14~" 'nil)                           ; F4
+    (define-key map "15~" 'nil)                           ; F5
+    (define-key map "17~" 'nil)                           ; F6
+    (define-key map "18~" 'nil)                           ; F7
+    (define-key map "19~" 'nil)                           ; F8
+    (define-key map "20~" 'nil)                           ; F9
+    (define-key map "21~" 'tpu-exit)                      ; F10
+    (define-key map "23~" 'tpu-insert-escape)             ; F11 (ESC)
+    (define-key map "24~" 'tpu-next-beginning-of-line)    ; F12 (BS)
+    (define-key map "25~" 'tpu-delete-previous-word)      ; F13 (LF)
+    (define-key map "26~" 'tpu-toggle-overwrite-mode)     ; F14
+    (define-key map "28~" 'tpu-help)                      ; HELP
+    (define-key map "29~" 'execute-extended-command)      ; DO
+    (define-key map "31~" 'tpu-goto-breadcrumb)           ; F17
+    (define-key map "32~" 'nil)                           ; F18
+    (define-key map "33~" 'nil)                           ; F19
+    (define-key map "34~" 'nil)                           ; F20
+    map)
   "Maps the CSI function keys on the VT100 keyboard.
 CSI is DEC's name for the sequence <ESC>[.")
 
-(defvar SS3-map (make-sparse-keymap)
-  "Maps the SS3 function keys on the VT100 keyboard.
-SS3 is DEC's name for the sequence <ESC>O.")
-
-(defvar GOLD-map (make-keymap)
-  "Maps the function keys on the VT100 keyboard preceeded by PF1.
+(defvar GOLD-CSI-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "A" 'tpu-move-to-beginning)      ; up-arrow
+    (define-key map "B" 'tpu-move-to-end)            ; down-arrow
+    (define-key map "C" 'end-of-line)                ; right-arrow
+    (define-key map "D" 'beginning-of-line)          ; left-arrow
+
+    (define-key map "1~" 'nil)                       ; Find
+    (define-key map "2~" 'nil)                       ; Insert Here
+    (define-key map "3~" 'tpu-store-text)            ; Remove
+    (define-key map "4~" 'tpu-unselect)              ; Select
+    (define-key map "5~" 'tpu-previous-window)       ; Prev Screen
+    (define-key map "6~" 'tpu-next-window)           ; Next Screen
+
+    (define-key map "11~" 'nil)                      ; F1
+    (define-key map "12~" 'nil)                      ; F2
+    (define-key map "13~" 'nil)                      ; F3
+    (define-key map "14~" 'nil)                      ; F4
+    (define-key map "16~" 'nil)                      ; F5
+    (define-key map "17~" 'nil)                      ; F6
+    (define-key map "18~" 'nil)                      ; F7
+    (define-key map "19~" 'nil)                      ; F8
+    (define-key map "20~" 'nil)                      ; F9
+    (define-key map "21~" 'nil)                      ; F10
+    (define-key map "23~" 'nil)                      ; F11
+    (define-key map "24~" 'nil)                      ; F12
+    (define-key map "25~" 'nil)                      ; F13
+    (define-key map "26~" 'nil)                      ; F14
+    (define-key map "28~" 'describe-bindings)        ; HELP
+    (define-key map "29~" 'nil)                      ; DO
+    (define-key map "31~" 'tpu-drop-breadcrumb)      ; F17
+    (define-key map "32~" 'nil)                      ; F18
+    (define-key map "33~" 'nil)                      ; F19
+    (define-key map "34~" 'nil)                      ; F20
+    map)
+  "Maps the function keys on the VT100 keyboard preceded by GOLD-CSI.")
+
+(defvar GOLD-SS3-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "A" 'tpu-move-to-beginning)      ; up-arrow
+    (define-key map "B" 'tpu-move-to-end)            ; down-arrow
+    (define-key map "C" 'end-of-line)                ; right-arrow
+    (define-key map "D" 'beginning-of-line)          ; left-arrow
+
+    (define-key map "P" 'keyboard-quit)              ; PF1
+    (define-key map "Q" 'help-for-help)              ; PF2
+    (define-key map "R" 'tpu-search)                 ; PF3
+    (define-key map "S" 'tpu-undelete-lines)         ; PF4
+    (define-key map "p" 'open-line)                  ; KP0
+    (define-key map "q" 'tpu-change-case)            ; KP1
+    (define-key map "r" 'tpu-delete-to-eol)          ; KP2
+    (define-key map "s" 'tpu-special-insert)         ; KP3
+    (define-key map "t" 'tpu-move-to-end)            ; KP4
+    (define-key map "u" 'tpu-move-to-beginning)      ; KP5
+    (define-key map "v" 'tpu-paste)                  ; KP6
+    (define-key map "w" 'execute-extended-command)   ; KP7
+    (define-key map "x" 'tpu-fill)                   ; KP8
+    (define-key map "y" 'tpu-replace)                ; KP9
+    (define-key map "m" 'tpu-undelete-words)         ; KP-
+    (define-key map "l" 'tpu-undelete-char)          ; KP,
+    (define-key map "n" 'tpu-unselect)               ; KP.
+    (define-key map "M" 'tpu-substitute)             ; KPenter
+    map)
+  "Maps the function keys on the VT100 keyboard preceded by GOLD-SS3.")
+
+(defvar GOLD-map
+  (let ((map (make-keymap)))
+    (define-key map "\e[" GOLD-CSI-map)                  ; GOLD-CSI map
+    (define-key map "\eO" GOLD-SS3-map)                  ; GOLD-SS3 map
+    ;; 
+    (define-key map "\C-A" 'tpu-toggle-overwrite-mode)   ; ^A
+    (define-key map "\C-B" 'nil)                         ; ^B
+    (define-key map "\C-C" 'nil)                         ; ^C
+    (define-key map "\C-D" 'nil)                         ; ^D
+    (define-key map "\C-E" 'nil)                         ; ^E
+    (define-key map "\C-F" 'set-visited-file-name)       ; ^F
+    (define-key map "\C-g" 'keyboard-quit)               ; safety first
+    (define-key map "\C-h" 'delete-other-windows)        ; BS
+    (define-key map "\C-i" 'other-window)                ; TAB
+    (define-key map "\C-J" 'nil)                         ; ^J
+    (define-key map "\C-K" 'tpu-define-macro-key)        ; ^K
+    (define-key map "\C-l" 'downcase-region)             ; ^L
+    (define-key map "\C-M" 'nil)                         ; ^M
+    (define-key map "\C-N" 'nil)                         ; ^N
+    (define-key map "\C-O" 'nil)                         ; ^O
+    (define-key map "\C-P" 'nil)                         ; ^P
+    (define-key map "\C-Q" 'nil)                         ; ^Q
+    (define-key map "\C-R" 'nil)                         ; ^R
+    (define-key map "\C-S" 'nil)                         ; ^S
+    (define-key map "\C-T" 'tpu-toggle-control-keys)     ; ^T
+    (define-key map "\C-u" 'upcase-region)               ; ^U
+    (define-key map "\C-V" 'nil)                         ; ^V
+    (define-key map "\C-w" 'tpu-write-current-buffers)   ; ^W
+    (define-key map "\C-X" 'nil)                         ; ^X
+    (define-key map "\C-Y" 'nil)                         ; ^Y
+    (define-key map "\C-Z" 'nil)                         ; ^Z
+    (define-key map " " 'undo)                           ; SPC
+    (define-key map "!" 'nil)                            ; !
+    (define-key map "#" 'nil)                            ; #
+    (define-key map "$" 'tpu-add-at-eol)                 ; $
+    (define-key map "%" 'tpu-goto-percent)               ; %
+    (define-key map "&" 'nil)                            ; &
+    (define-key map "(" 'nil)                            ; (
+    (define-key map ")" 'nil)                            ; )
+    (define-key map "*" 'tpu-toggle-regexp)              ; *
+    (define-key map "+" 'nil)                            ; +
+    (define-key map "," 'tpu-goto-breadcrumb)            ; ,
+    (define-key map "-" 'negative-argument)              ; -
+    (define-key map "." 'tpu-drop-breadcrumb)            ; .
+    (define-key map "/" 'tpu-emacs-replace)              ; /
+    (define-key map "0" 'digit-argument)                 ; 0
+    (define-key map "1" 'digit-argument)                 ; 1
+    (define-key map "2" 'digit-argument)                 ; 2
+    (define-key map "3" 'digit-argument)                 ; 3
+    (define-key map "4" 'digit-argument)                 ; 4
+    (define-key map "5" 'digit-argument)                 ; 5
+    (define-key map "6" 'digit-argument)                 ; 6
+    (define-key map "7" 'digit-argument)                 ; 7
+    (define-key map "8" 'digit-argument)                 ; 8
+    (define-key map "9" 'digit-argument)                 ; 9
+    (define-key map ":" 'nil)                            ; :
+    (define-key map ";" 'tpu-trim-line-ends)             ; ;
+    (define-key map "<" 'nil)                            ; <
+    (define-key map "=" 'nil)                            ; =
+    (define-key map ">" 'nil)                            ; >
+    (define-key map "?" 'tpu-spell-check)                ; ?
+    (define-key map "A" 'tpu-toggle-newline-and-indent)  ; A
+    (define-key map "B" 'tpu-next-buffer)                ; B
+    (define-key map "C" 'repeat-complex-command)         ; C
+    (define-key map "D" 'shell-command)                  ; D
+    (define-key map "E" 'tpu-exit)                       ; E
+    (define-key map "F" 'tpu-set-cursor-free)            ; F
+    (define-key map "G" 'tpu-get)                        ; G
+    (define-key map "H" 'nil)                            ; H
+    (define-key map "I" 'tpu-include)                    ; I
+    (define-key map "K" 'tpu-kill-buffer)                ; K
+    (define-key map "L" 'tpu-what-line)                  ; L
+    (define-key map "M" 'buffer-menu)                    ; M
+    (define-key map "N" 'tpu-next-file-buffer)           ; N
+    (define-key map "O" 'occur)                          ; O
+    (define-key map "P" 'lpr-buffer)                     ; P
+    (define-key map "Q" 'tpu-quit)                       ; Q
+    (define-key map "R" 'tpu-toggle-rectangle)           ; R
+    (define-key map "S" 'replace)                        ; S
+    (define-key map "T" 'tpu-line-to-top-of-window)      ; T
+    (define-key map "U" 'undo)                           ; U
+    (define-key map "V" 'tpu-version)                    ; V
+    (define-key map "W" 'save-buffer)                    ; W
+    (define-key map "X" 'tpu-save-all-buffers-kill-emacs) ; X
+    (define-key map "Y" 'copy-region-as-kill)             ; Y
+    (define-key map "Z" 'suspend-emacs)                   ; Z
+    (define-key map "[" 'blink-matching-open)             ; [
+    (define-key map "\\" 'nil)                            ; \
+    (define-key map "]" 'blink-matching-open)             ; ]
+    (define-key map "^" 'tpu-add-at-bol)                  ; ^
+    (define-key map "_" 'split-window-vertically)         ; -
+    (define-key map "`" 'what-line)                       ; `
+    (define-key map "a" 'tpu-toggle-newline-and-indent)   ; a
+    (define-key map "b" 'tpu-next-buffer)                 ; b
+    (define-key map "c" 'repeat-complex-command)          ; c
+    (define-key map "d" 'shell-command)                   ; d
+    (define-key map "e" 'tpu-exit)                        ; e
+    (define-key map "f" 'tpu-set-cursor-free)             ; f
+    (define-key map "g" 'tpu-get)                         ; g
+    (define-key map "h" 'nil)                             ; h
+    (define-key map "i" 'tpu-include)                     ; i
+    (define-key map "k" 'tpu-kill-buffer)                 ; k
+    (define-key map "l" 'goto-line)                       ; l
+    (define-key map "m" 'buffer-menu)                     ; m
+    (define-key map "n" 'tpu-next-file-buffer)            ; n
+    (define-key map "o" 'occur)                           ; o
+    (define-key map "p" 'lpr-region)                      ; p
+    (define-key map "q" 'tpu-quit)                        ; q
+    (define-key map "r" 'tpu-toggle-rectangle)            ; r
+    (define-key map "s" 'replace)                         ; s
+    (define-key map "t" 'tpu-line-to-top-of-window)       ; t
+    (define-key map "u" 'undo)                            ; u
+    (define-key map "v" 'tpu-version)                     ; v
+    (define-key map "w" 'save-buffer)                     ; w
+    (define-key map "x" 'tpu-save-all-buffers-kill-emacs) ; x
+    (define-key map "y" 'copy-region-as-kill)             ; y
+    (define-key map "z" 'suspend-emacs)                   ; z
+    (define-key map "{" 'nil)                             ; {
+    (define-key map "|" 'split-window-horizontally)       ; |
+    (define-key map "}" 'nil)                             ; }
+    (define-key map "~" 'exchange-point-and-mark)         ; ~
+    (define-key map "\177" 'delete-window)                ; <X]
+    map)
+  "Maps the function keys on the VT100 keyboard preceded by PF1.
 GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
 
-(defvar GOLD-CSI-map (make-sparse-keymap)
-  "Maps the function keys on the VT100 keyboard preceeded by GOLD-CSI.")
-
-(defvar GOLD-SS3-map (make-sparse-keymap)
-  "Maps the function keys on the VT100 keyboard preceeded by GOLD-SS3.")
+(defvar SS3-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map "P" GOLD-map)                         ; GOLD map
+    ;; 
+    (define-key map "A" 'tpu-previous-line)               ; up
+    (define-key map "B" 'tpu-next-line)                   ; down
+    (define-key map "C" 'tpu-forward-char)                ; right
+    (define-key map "D" 'tpu-backward-char)               ; left
+
+    (define-key map "Q" 'tpu-help)                        ; PF2
+    (define-key map "R" 'tpu-search-again)                ; PF3
+    (define-key map "S" 'tpu-delete-current-line)         ; PF4
+    (define-key map "p" 'tpu-line)                        ; KP0
+    (define-key map "q" 'tpu-word)                        ; KP1
+    (define-key map "r" 'tpu-end-of-line)                 ; KP2
+    (define-key map "s" 'tpu-char)                        ; KP3
+    (define-key map "t" 'tpu-advance-direction)           ; KP4
+    (define-key map "u" 'tpu-backup-direction)            ; KP5
+    (define-key map "v" 'tpu-cut)                         ; KP6
+    (define-key map "w" 'tpu-page)                        ; KP7
+    (define-key map "x" 'tpu-scroll-window)               ; KP8
+    (define-key map "y" 'tpu-append-region)               ; KP9
+    (define-key map "m" 'tpu-delete-current-word)         ; KP-
+    (define-key map "l" 'tpu-delete-current-char)         ; KP,
+    (define-key map "n" 'tpu-select)                      ; KP.
+    (define-key map "M" 'newline)                         ; KPenter
+    map)
+  "Maps the SS3 function keys on the VT100 keyboard.
+SS3 is DEC's name for the sequence <ESC>O.")
 
 (defvar tpu-global-map nil "TPU-edt global keymap.")
-(defvar tpu-original-global-map (copy-keymap global-map)
-  "Original global keymap.")
+(defvar tpu-original-global-map global-map
+  "Original non-TPU global keymap.")
 
-(and tpu-lucid-emacs19-p
+(and (not (boundp 'minibuffer-local-ns-map))
      (defvar minibuffer-local-ns-map (make-sparse-keymap)
-       "Hack to give Lucid emacs the same maps as GNU emacs."))
+       "Hack to give Lucid Emacs the same maps as ordinary Emacs."))
 
 
 ;;;
 ;;;  Global Variables
 ;;;
-(defvar tpu-edt-mode nil
-  "If non-nil, TPU-edt mode is active.")
-
 (defvar tpu-last-replaced-text ""
   "Last text deleted by a TPU-edt replace command.")
 (defvar tpu-last-deleted-region ""
@@ -122,6 +611,13 @@ GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
   "If non-nil, TPU-edt is searching in the forward direction.")
 (defvar tpu-search-last-string ""
   "Last text searched for by the TPU-edt search commands.")
+(defvar tpu-search-overlay (make-overlay 1 1)
+  "Search highlight overlay.")
+(overlay-put tpu-search-overlay 'face 'bold)
+
+(defvar tpu-replace-overlay (make-overlay 1 1)
+  "Replace highlight overlay.")
+(overlay-put tpu-replace-overlay 'face 'highlight)
 
 (defvar tpu-regexp-p nil
   "If non-nil, TPU-edt uses regexp search and replace routines.")
@@ -131,7 +627,7 @@ GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
   "True when TPU-edt is operating in the forward direction.")
 (defvar tpu-reverse nil
   "True when TPU-edt is operating in the backward direction.")
-(defvar tpu-control-keys t
+(defvar tpu-control-keys nil
   "If non-nil, control keys are set to perform TPU functions.")
 (defvar tpu-xkeys-file nil
   "File containing TPU-edt X key map.")
@@ -172,61 +668,38 @@ GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
 ;;;
 ;;;  Mode Line - Modify the mode line to show the following
 ;;;
-;;;     o  If the mark is set.
+;;;     o  Mark state.
 ;;;     o  Direction of motion.
 ;;;     o  Active rectangle mode.
+;;;     o  Active auto indent mode.
 ;;;
-(defvar tpu-original-mode-line mode-line-format)
 (defvar tpu-original-mm-alist minor-mode-alist)
 
-(defvar tpu-mark-flag " ")
+(defvar tpu-mark-flag "")
 (make-variable-buffer-local 'tpu-mark-flag)
 
 (defun tpu-set-mode-line (for-tpu)
-  "Set the mode for TPU-edt, or reset it to default Emacs."
-  (cond ((not for-tpu)
-        (setq mode-line-format tpu-original-mode-line)
-        (setq minor-mode-alist tpu-original-mm-alist))
-       (t
-        (setq-default mode-line-format
-                      (list (purecopy "")
-                            'mode-line-modified
-                            'mode-line-buffer-identification
-                            (purecopy "  ")
-                            'global-mode-string
-                            (purecopy "  ")
-                            'tpu-mark-flag
-                            (purecopy " %[(")
-                            'mode-name 'minor-mode-alist "%n" 'mode-line-process
-                            (purecopy ")%]----")
-                            (purecopy '(-3 . "%p"))
-                            (purecopy "-%-")))
-        (or (assq 'tpu-newline-and-indent-p minor-mode-alist)
-            (setq minor-mode-alist
-                  (cons '(tpu-newline-and-indent-p
-                          tpu-newline-and-indent-string)
-                        minor-mode-alist)))
-        (or (assq 'tpu-rectangular-p minor-mode-alist)
-            (setq minor-mode-alist
-                  (cons '(tpu-rectangular-p tpu-rectangle-string)
-                        minor-mode-alist)))
-        (or (assq 'tpu-direction-string minor-mode-alist)
-            (setq minor-mode-alist
-                  (cons '(tpu-direction-string tpu-direction-string)
-                        minor-mode-alist))))))
+  "Set ``minor-mode-alist'' for TPU-edt, or reset it to default Emacs."
+  (let ((entries '((tpu-newline-and-indent-p tpu-newline-and-indent-string)
+                   (tpu-rectangular-p tpu-rectangle-string)
+                   (tpu-direction-string tpu-direction-string)
+                   (tpu-mark-flag tpu-mark-flag))))
+    (dolist (entry entries)
+      (if for-tpu
+          (add-to-list 'minor-mode-alist entry)
+        (setq minor-mode-alist (remove entry minor-mode-alist))))))
 
 (defun tpu-update-mode-line nil
   "Make sure mode-line in the current buffer reflects all changes."
-  (setq tpu-mark-flag (if (tpu-mark) "M" " "))
-  (cond (tpu-emacs19-p (force-mode-line-update))
-       (t (set-buffer-modified-p (buffer-modified-p)) (sit-for 0))))
+  (setq tpu-mark-flag (if transient-mark-mode "" (if (tpu-mark) " @" "  ")))
+  (force-mode-line-update))
 
-(cond (tpu-gnu-emacs19-p
-       (add-hook 'activate-mark-hook 'tpu-update-mode-line)
-       (add-hook 'deactivate-mark-hook 'tpu-update-mode-line))
-      (tpu-lucid-emacs19-p
+(cond (tpu-lucid-emacs-p
        (add-hook 'zmacs-deactivate-region-hook 'tpu-update-mode-line)
-       (add-hook 'zmacs-activate-region-hook 'tpu-update-mode-line)))
+       (add-hook 'zmacs-activate-region-hook 'tpu-update-mode-line))
+      (t
+       (add-hook 'activate-mark-hook 'tpu-update-mode-line)
+       (add-hook 'deactivate-mark-hook 'tpu-update-mode-line)))
 
 
 ;;;
@@ -244,7 +717,7 @@ GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
   "Set markers at match beginning and end."
   ;; Add one to beginning mark so it stays with the first character of
   ;;   the string even if characters are added just before the string.
-  (setq tpu-match-beginning-mark (copy-marker (1+ (match-beginning 0))))
+  (setq tpu-match-beginning-mark (copy-marker (match-beginning 0) t))
   (setq tpu-match-end-mark (copy-marker (match-end 0))))
 
 (defun tpu-unset-match nil
@@ -254,7 +727,7 @@ GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
 
 (defun tpu-match-beginning nil
   "Returns the location of the last match beginning."
-  (1- (marker-position tpu-match-beginning-mark)))
+  (marker-position tpu-match-beginning-mark))
 
 (defun tpu-match-end nil
   "Returns the location of the last match end."
@@ -270,10 +743,10 @@ Otherwise sets the tpu-match markers to nil and returns nil."
   ;;              beginning, end, and point are equal.
   (cond ((and
          (equal (marker-buffer tpu-match-beginning-mark) (current-buffer))
-         (>= (point) (1- (marker-position tpu-match-beginning-mark)))
+         (>= (point) (marker-position tpu-match-beginning-mark))
          (or
           (< (point) (marker-position tpu-match-end-mark))
-          (and (= (1- (marker-position tpu-match-beginning-mark))
+          (and (= (marker-position tpu-match-beginning-mark)
                   (marker-position tpu-match-end-mark))
                (= (marker-position tpu-match-end-mark) (point))))) t)
        (t
@@ -283,12 +756,11 @@ Otherwise sets the tpu-match markers to nil and returns nil."
   "Show the values of the match markers."
   (interactive)
   (if (markerp tpu-match-beginning-mark)
-      (let ((beg (marker-position tpu-match-beginning-mark)))
-       (message "(%s, %s) in %s -- current %s in %s"
-                (if beg (1- beg) nil)
-                (marker-position tpu-match-end-mark)
-                (marker-buffer tpu-match-end-mark)
-                (point) (current-buffer)))))
+      (message "(%s, %s) in %s -- current %s in %s"
+               (marker-position tpu-match-beginning-mark)
+               (marker-position tpu-match-end-mark)
+               (marker-buffer tpu-match-end-mark)
+               (point) (current-buffer))))
 
 
 ;;;
@@ -297,34 +769,35 @@ Otherwise sets the tpu-match markers to nil and returns nil."
 (defun tpu-caar (thingy) (car (car thingy)))
 (defun tpu-cadr (thingy) (car (cdr thingy)))
 
+(defvar zmacs-regions)
+
 (defun tpu-mark nil
   "TPU-edt version of the mark function.
 Return the appropriate value of the mark for the current
-version of emacs."
-  (cond (tpu-lucid-emacs19-p (mark (not zmacs-regions)))
-       (tpu-gnu-emacs19-p (and mark-active (mark (not transient-mark-mode))))
-       (t (mark))))
+version of Emacs."
+  (cond (tpu-lucid-emacs-p (mark (not zmacs-regions)))
+       (t (and mark-active (mark (not transient-mark-mode))))))
 
 (defun tpu-set-mark (pos)
-  "TPU-edt verion of the set-mark function.
-Sets the mark at POS and activates the region acording to the
-current version of emacs."
+  "TPU-edt version of the `set-mark' function.
+Sets the mark at POS and activates the region according to the
+current version of Emacs."
   (set-mark pos)
-  (and tpu-lucid-emacs19-p pos (zmacs-activate-region)))
+  ;; We use a separate `if' for the fboundp so the byte-compiler notices it
+  ;; and doesn't complain about the subsequent call.
+  (if (fboundp 'zmacs-activate-region) (if pos (zmacs-activate-region))))
 
 (defun tpu-string-prompt (prompt history-symbol)
   "Read a string with PROMPT."
-  (if tpu-emacs19-p
-      (read-from-minibuffer prompt nil nil nil history-symbol)
-    (read-string prompt)))
+  (read-from-minibuffer prompt nil nil nil history-symbol))
 
 (defvar tpu-last-answer nil "Most recent response to tpu-y-or-n-p.")
 
 (defun tpu-y-or-n-p (prompt &optional not-yes)
   "Prompt for a y or n answer with positive default.
 Optional second argument NOT-YES changes default to negative.
-Like emacs y-or-n-p, also accepts space as y and DEL as n."
-  (message (format "%s[%s]" prompt (if not-yes "n" "y")))
+Like Emacs `y-or-n-p', but also accepts space as y and DEL as n."
+  (message "%s[%s]" prompt (if not-yes "n" "y"))
   (let ((doit t))
     (while doit
       (setq doit nil)
@@ -336,8 +809,8 @@ Like emacs y-or-n-p, also accepts space as y and DEL as n."
              ((= ans ?\r) (setq tpu-last-answer (not not-yes)))
              (t
               (setq doit t) (beep)
-              (message (format "Please answer y or n.  %s[%s]"
-                               prompt (if not-yes "n" "y"))))))))
+              (message "Please answer y or n.  %s[%s]"
+                       prompt (if not-yes "n" "y")))))))
   tpu-last-answer)
 
 (defun tpu-local-set-key (key func)
@@ -430,8 +903,8 @@ With argument, fill and justify."
 (defun tpu-reset-screen-size (height width)
   "Sets the screen size."
   (interactive "nnew screen height: \nnnew screen width: ")
-  (set-screen-height height)
-  (set-screen-width width))
+  (set-frame-height (selected-frame) height)
+  (set-frame-width (selected-frame) width))
 
 (defun tpu-toggle-newline-and-indent nil
   "Toggle between 'newline and indent' and 'simple newline'."
@@ -477,6 +950,14 @@ its ASCII decimal value."
   (if overwrite-mode (delete-char 1))
   (insert (if num num 0)))
 
+(defun tpu-quoted-insert (num)
+  "Read next input character and insert it.
+This is useful for inserting control characters."
+  (interactive "*p")
+  (let ((char (read-char)) )
+    (if overwrite-mode (delete-char num))
+    (insert-char char num)))
+
 
 ;;;
 ;;;  TPU line-mode commands
@@ -484,14 +965,13 @@ its ASCII decimal value."
 (defun tpu-include (file)
   "TPU-like include file"
   (interactive "fInclude file: ")
-  (save-excursion
-    (insert-file file)
-    (message "")))
+  (insert-file-contents file)
+  (message ""))
 
 (defun tpu-get (file)
   "TPU-like get file"
   (interactive "FFile to get: ")
-  (find-file file))
+  (find-file file find-file-wildcards))
 
 (defun tpu-what-line nil
   "Tells what line the point is on,
@@ -500,9 +980,10 @@ its ASCII decimal value."
   (if (eobp)
       (message "You are at the End of Buffer.  The last line is %d."
               (count-lines 1 (point-max)))
-    (message "Line %d of %d"
-            (count-lines 1 (1+ (point)))
-            (count-lines 1 (point-max)))))
+    (let* ((cur (count-lines 1 (1+ (point))))
+          (max (count-lines 1 (point-max)))
+          (pct (/ (* 100 (+ cur (/ max 200))) max)))
+      (message "You are on line %d out of %d (%d%%)." cur max pct))))
 
 (defun tpu-exit nil
   "Exit the way TPU does, save current buffer and ask about others."
@@ -530,36 +1011,60 @@ its ASCII decimal value."
 ;;;  Command and Function Aliases
 ;;;
 ;;;###autoload
-(fset 'tpu-edt-mode 'tpu-edt-on)
-(fset 'TPU-EDT-MODE 'tpu-edt-on)
+(define-minor-mode tpu-edt-mode
+  "TPU/edt emulation."
+  :global t
+  (if tpu-edt-mode (tpu-edt-on) (tpu-edt-off)))
+
+(defalias 'TPU-EDT-MODE 'tpu-edt-mode)
 
 ;;;###autoload
-(fset 'tpu-edt 'tpu-edt-on)
-(fset 'TPU-EDT 'tpu-edt-on)
+(defalias 'tpu-edt 'tpu-edt-on)
+(defalias 'TPU-EDT 'tpu-edt-on)
+
+;; Note:  The following functions have no `tpu-' prefix.  This is unavoidable.
+;;        The real TPU/edt editor has interactive commands with these names,
+;;        so tpu-edt.el users expect things like M-x exit RET and M-x help RET
+;;        to work.  Therefore it really is necessary to define these functions,
+;;        even in cases where they redefine existing Emacs functions.
+
+(defalias 'exit 'tpu-exit)
+(defalias 'EXIT 'tpu-exit)
+
+(defalias 'Get 'tpu-get)
+(defalias 'GET 'tpu-get)
+
+(defalias 'include 'tpu-include)
+(defalias 'INCLUDE 'tpu-include)
 
-(fset 'exit 'tpu-exit)
-(fset 'EXIT 'tpu-exit)
+(defalias 'quit 'tpu-quit)
+(defalias 'QUIT 'tpu-quit)
 
-(fset 'Get 'tpu-get)
-(fset 'GET 'tpu-get)
+(defalias 'spell 'tpu-spell-check)
+(defalias 'SPELL 'tpu-spell-check)
 
-(fset 'include 'tpu-include)
-(fset 'INCLUDE 'tpu-include)
+(defalias 'what\ line 'tpu-what-line)
+(defalias 'WHAT\ LINE 'tpu-what-line)
 
-(fset 'quit 'tpu-quit)
-(fset 'QUIT 'tpu-quit)
+(defalias 'replace 'tpu-lm-replace)
+(defalias 'REPLACE 'tpu-lm-replace)
 
-(fset 'spell 'tpu-spell-check)
-(fset 'SPELL 'tpu-spell-check)
+(defalias 'help 'tpu-help)
+(defalias 'HELP 'tpu-help)
 
-(fset 'what\ line 'tpu-what-line)
-(fset 'WHAT\ LINE 'tpu-what-line)
+(defalias 'set\ cursor\ free 'tpu-set-cursor-free)
+(defalias 'SET\ CURSOR\ FREE 'tpu-set-cursor-free)
 
-(fset 'replace 'tpu-lm-replace)
-(fset 'REPLACE 'tpu-lm-replace)
+(defalias 'set\ cursor\ bound 'tpu-set-cursor-bound)
+(defalias 'SET\ CURSOR\ BOUND 'tpu-set-cursor-bound)
+
+(defalias 'set\ scroll\ margins 'tpu-set-scroll-margins)
+(defalias 'SET\ SCROLL\ MARGINS 'tpu-set-scroll-margins)
+
+;; Real TPU error messages end in periods.
+;; Define this to avoid openly flouting Emacs coding standards.
+(defalias 'tpu-error 'error)
 
-(fset 'help 'tpu-help)
-(fset 'HELP 'tpu-help)
 
 ;; Around emacs version 18.57, function line-move was renamed to
 ;; next-line-internal.  If we're running under an older emacs,
@@ -571,7 +1076,7 @@ its ASCII decimal value."
 ;;;
 ;;;  Help
 ;;;
-(defconst tpu-help-keypad-map "\f
+(defvar tpu-help-keypad-map "\f
           _______________________    _______________________________
          | HELP  |      Do       |  |       |       |       |       |
          |KeyDefs|               |  |       |       |       |       |
@@ -594,7 +1099,7 @@ its ASCII decimal value."
                                     |_______________|_______|_______|
 ")
 
-(defconst tpu-help-text "
+(defvar tpu-help-text "
 \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
 
       Control Characters
@@ -624,23 +1129,43 @@ its ASCII decimal value."
        B     Next Buffer - display the next buffer (all buffers)
        C     Recall - edit and possibly repeat previous commands
        E     Exit - save current buffer and ask about others
-
        G     Get - load a file into a new edit buffer
+
        I     Include - include a file in this buffer
        K     Kill Buffer - abandon edits and delete buffer
-
        M     Buffer Menu - display a list of all buffers
        N     Next File Buffer - display next buffer containing a file
-       O     Occur - show following lines containing REGEXP
 
+       O     Occur - show following lines containing REGEXP
        Q     Quit - exit without saving anything
        R     Toggle rectangular mode for remove and insert
        S     Search and substitute - line mode REPLACE command
 
+      ^T     Toggle control key bindings between TPU and emacs
        U     Undo - undo the last edit
        W     Write - save current buffer
        X     Exit - save all modified buffers and exit
 
+\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
+
+  More extensive documentation on TPU-edt can be found in the `Commentary'
+  section of tpu-edt.el.  This section can be accessed through the standard
+  Emacs help facility using the `p' option.  Once you exit TPU-edt Help, one
+  of the following key sequences is sure to get you there.
+
+    ^h p        if you're not yet using TPU-edt
+    Gold-PF2 p  if you're using TPU-edt
+
+  Alternatively, fire up Emacs help from the command prompt, with
+
+    M-x help-for-help <CR> p <CR>
+
+  Where `M-x' might be any of `Gold-KP7', 'Do', or 'ESC-x'.
+
+  When you successfully invoke this part of the Emacs help facility, you
+  will see a buffer named `*Finder*' listing a number of topics.  Look for
+  tpu-edt under `emulations'.
+
 \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
 
    *** No more help, use P to view previous screen")
@@ -710,7 +1235,8 @@ its ASCII decimal value."
                          (scroll-other-window -8)
                        (error nil)))
                     (t
-                     (backward-page 2)
+                     (forward-line -1)
+                     (backward-page)
                      (forward-line 1)
                      (tpu-line-to-top-of-window))))
              ((not (equal tpu-help-return fkey))
@@ -768,8 +1294,8 @@ kills modified buffers without asking."
 (defun tpu-save-all-buffers-kill-emacs nil
   "Save all buffers and exit emacs."
   (interactive)
-  (setq trim-versions-without-asking t)
-  (save-buffers-kill-emacs t))
+  (let ((delete-old-versions t))
+    (save-buffers-kill-emacs t)))
 
 (defun tpu-write-current-buffers nil
   "Save all modified buffers without exiting."
@@ -782,14 +1308,19 @@ kills modified buffers without asking."
   (switch-to-buffer (car (reverse (buffer-list)))))
 
 (defun tpu-next-file-buffer nil
-  "Go to next buffer in ring that is visiting a file."
+  "Go to next buffer in ring that is visiting a file or directory."
   (interactive)
-  (let ((starting-buffer (buffer-name)))
-    (switch-to-buffer (car (reverse (buffer-list))))
-    (while (and (not (equal (buffer-name) starting-buffer))
-               (not (buffer-file-name)))
-      (switch-to-buffer (car (reverse (buffer-list)))))
-    (if (equal (buffer-name) starting-buffer) (error "No other buffers."))))
+  (let ((list (tpu-make-file-buffer-list (buffer-list))))
+    (setq list (delq (current-buffer) list))
+    (if (not list) (tpu-error "No other buffers."))
+    (switch-to-buffer (car (reverse list)))))
+
+(defun tpu-make-file-buffer-list (buffer-list)
+  "Returns names from BUFFER-LIST excluding those beginning with a space or star."
+  (delq nil (mapcar '(lambda (b)
+                       (if (or (= (aref (buffer-name b) 0) ? )
+                               (= (aref (buffer-name b) 0) ?*)) nil b))
+                    buffer-list)))
 
 (defun tpu-next-window nil
   "Move to the next window."
@@ -819,9 +1350,15 @@ kills modified buffers without asking."
 (defun tpu-regexp-prompt (prompt)
   "Read a string, adding 'RE' to the prompt if tpu-regexp-p is set."
   (let ((re-prompt (concat (if tpu-regexp-p "RE ") prompt)))
-    (if tpu-emacs19-p
-       (read-from-minibuffer re-prompt nil nil nil 'tpu-regexp-prompt-hist)
-      (read-string re-prompt))))
+    (read-from-minibuffer re-prompt nil nil nil 'tpu-regexp-prompt-hist)))
+
+(defun tpu-search-highlight nil
+  (if (tpu-check-match)
+      (move-overlay tpu-search-overlay
+                    (tpu-match-beginning) (tpu-match-end) (current-buffer))
+    (unless (equal (overlay-start tpu-search-overlay)
+                  (overlay-end tpu-search-overlay))
+      (move-overlay tpu-search-overlay 1 1 (current-buffer)))))
 
 (defun tpu-search nil
   "Search for a string or regular expression.
@@ -858,15 +1395,16 @@ The search is performed in the current direction."
 ;;  to ensure that the next search will be in the current direction.  It is
 ;;  called from:
 
-;;       tpu-advance              tpu-backup
-;;       tpu-toggle-regexp        tpu-toggle-search-direction (t)
-;;       tpu-search               tpu-lm-replace
-;;       tpu-search-forward (t)   tpu-search-reverse (t)
+;;       tpu-advance                   tpu-backup
+;;       tpu-toggle-regexp             tpu-toggle-search-direction (t)
+;;       tpu-search                    tpu-lm-replace
+;;       tpu-search-forward (t)        tpu-search-reverse (t)
+;;       tpu-search-forward-exit (t)   tpu-search-backward-exit (t)
 
 (defun tpu-set-search (&optional arg)
   "Set the search functions and set the search direction to the current
 direction.  If an argument is specified, don't set the search direction."
-  (if (not arg) (setq tpu-searching-forward (if tpu-advance t nil)))
+  (if (not arg) (setq tpu-searching-forward tpu-advance))
   (cond (tpu-searching-forward
         (cond (tpu-regexp-p
                (fset 'tpu-emacs-search 're-search-forward)
@@ -890,32 +1428,49 @@ direction.  If an argument is specified, don't set the search direction."
   (tpu-unset-match)
   (tpu-adjust-search)
 
-  (cond ((tpu-emacs-search tpu-search-last-string nil t)
-        (tpu-set-match) (goto-char (tpu-match-beginning)))
-
-       (t
-        (tpu-adjust-search t)
-        (let ((found nil) (pos nil))
-          (save-excursion
-            (let ((tpu-searching-forward (not tpu-searching-forward)))
-              (tpu-adjust-search)
-              (setq found (tpu-emacs-rev-search tpu-search-last-string nil t))
-              (setq pos (match-beginning 0))))
+  (let ((case-fold-search
+        (and case-fold-search (tpu-check-search-case tpu-search-last-string))))
 
-          (cond (found
-                 (cond ((tpu-y-or-n-p
-                         (format "Found in %s direction.  Go there? "
-                                 (if tpu-searching-forward "reverse" "forward")))
-                        (goto-char pos) (tpu-set-match)
-                        (tpu-toggle-search-direction))))
+    (cond ((tpu-emacs-search tpu-search-last-string nil t)
+          (tpu-set-match) (goto-char (tpu-match-beginning)))
 
-                (t
-                 (if (not quiet)
-                     (message
-                      "%sSearch failed: \"%s\""
-                      (if tpu-regexp-p "RE " "") tpu-search-last-string))))))))
+         (t
+          (tpu-adjust-search t)
+          (let ((found nil) (pos nil))
+            (save-excursion
+              (let ((tpu-searching-forward (not tpu-searching-forward)))
+                (tpu-adjust-search)
+                (setq found (tpu-emacs-rev-search tpu-search-last-string nil t))
+                (setq pos (match-beginning 0))))
+
+            (cond
+             (found
+              (cond ((tpu-y-or-n-p
+                      (format "Found in %s direction.  Go there? "
+                              (if tpu-searching-forward "reverse" "forward")))
+                     (goto-char pos) (tpu-set-match)
+                     (tpu-toggle-search-direction))))
 
-(fset 'tpu-search-internal-core (symbol-function 'tpu-search-internal))
+             (t
+              (if (not quiet)
+                  (message
+                   "%sSearch failed: \"%s\""
+                   (if tpu-regexp-p "RE " "") tpu-search-last-string)))))))))
+
+(defalias 'tpu-search-internal-core (symbol-function 'tpu-search-internal))
+
+(defun tpu-check-search-case (string)
+  "Returns t if string contains upper case."
+  ;; if using regexp, eliminate upper case forms (\B \W \S.)
+  (if tpu-regexp-p
+      (let ((pat (copy-sequence string)) (case-fold-search nil) (pos 0))
+       (while (setq pos (string-match "\\\\\\\\" pat)) (aset pat (+ 1 pos) ?.))
+       (while (setq pos (string-match "\\\\B" pat)) (aset pat (+ 1 pos) ?.))
+       (while (setq pos (string-match "\\\\W" pat)) (aset pat (+ 1 pos) ?.))
+       (while (setq pos (string-match "\\\\S." pat))
+         (aset pat (+ 1 pos) ?.) (aset pat (+ 2 pos) ?.))
+       (string-equal pat (downcase pat)))
+    (string-equal string (downcase string))))
 
 (defun tpu-adjust-search (&optional arg)
   "For forward searches, move forward a character before searching,
@@ -934,6 +1489,20 @@ Used for reversing a search in progress."
        (message "Searching %sward."
                (if tpu-searching-forward "for" "back"))))
 
+(defun tpu-search-forward-exit nil
+  "Set search direction forward and exit minibuffer."
+  (interactive)
+  (setq tpu-searching-forward t)
+  (tpu-set-search t)
+  (exit-minibuffer))
+
+(defun tpu-search-backward-exit nil
+  "Set search direction backward and exit minibuffer."
+  (interactive)
+  (setq tpu-searching-forward nil)
+  (tpu-set-search t)
+  (exit-minibuffer))
+
 
 ;;;
 ;;;  Select / Unselect
@@ -951,6 +1520,7 @@ Used for reversing a search in progress."
 (defun tpu-unselect (&optional quiet)
   "Removes the mark to unselect the current region."
   (interactive "P")
+  (deactivate-mark)
   (setq mark-ring nil)
   (tpu-set-mark nil)
   (tpu-update-mode-line)
@@ -976,20 +1546,20 @@ corners of a rectangle."
   (let ((mc (current-column))
        (pc (progn (exchange-point-and-mark) (current-column))))
 
-    (cond ((> (point) (tpu-mark))                      ; point on lower line
+    (cond ((> (point) (tpu-mark))                  ; point on lower line
           (cond ((> pc mc)                        ; point @  lower-right
                  (exchange-point-and-mark))       ; point -> upper-left
 
                 (t                                ; point @  lower-left
-                 (move-to-column-force mc)        ; point -> lower-right
+                 (move-to-column mc t)            ; point -> lower-right
                  (exchange-point-and-mark)        ; point -> upper-right
-                 (move-to-column-force pc))))     ; point -> upper-left
+                 (move-to-column pc t))))         ; point -> upper-left
 
          (t                                       ; point on upper line
           (cond ((> pc mc)                        ; point @  upper-right
-                 (move-to-column-force mc)        ; point -> upper-left
+                 (move-to-column mc t)            ; point -> upper-left
                  (exchange-point-and-mark)        ; point -> lower-left
-                 (move-to-column-force pc)        ; point -> lower-right
+                 (move-to-column pc t)            ; point -> lower-right
                  (exchange-point-and-mark)))))))  ; point -> upper-left
 
 (defun tpu-cut-text nil
@@ -1012,7 +1582,7 @@ The text is saved for the tpu-paste command."
           (delete-region beg end)
           (tpu-unset-match)))
        (t
-        (error "No selection active."))))
+        (tpu-error "No selection active."))))
 
 (defun tpu-store-text nil
   "Copy the selected region to the cut buffer without deleting it.
@@ -1034,7 +1604,7 @@ The text is saved for the tpu-paste command."
               (buffer-substring (tpu-match-beginning) (tpu-match-end)))
         (tpu-unset-match))
        (t
-        (error "No selection active."))))
+        (tpu-error "No selection active."))))
 
 (defun tpu-cut (arg)
   "Copy selected region to the cut buffer.  In the absence of an
@@ -1061,7 +1631,7 @@ argument, delete the selected region too."
           (if (not arg) (delete-region beg end))
           (tpu-unset-match)))
        (t
-        (error "No selection active."))))
+        (tpu-error "No selection active."))))
 
 (defun tpu-delete-current-line (num)
   "Delete one or specified number of lines after point.
@@ -1078,7 +1648,7 @@ They are saved for the TPU-edt undelete-lines command."
 
 (defun tpu-delete-to-eol (num)
   "Delete text up to end of line.
-With argument, delete up to to Nth line-end past point.
+With argument, delete up to the Nth line-end past point.
 They are saved for the TPU-edt undelete-lines command."
   (interactive "p")
   (let ((beg (point)))
@@ -1090,7 +1660,7 @@ They are saved for the TPU-edt undelete-lines command."
 
 (defun tpu-delete-to-bol (num)
   "Delete text back to beginning of line.
-With argument, delete up to to Nth line-end past point.
+With argument, delete up to the Nth line-end past point.
 They are saved for the TPU-edt undelete-lines command."
   (interactive "p")
   (let ((beg (point)))
@@ -1201,7 +1771,7 @@ With argument reinserts the character that many times."
                          (not case-replace) (not tpu-regexp-p))
           (tpu-unset-match)))
        (t
-        (error "No selection active."))))
+        (tpu-error "No selection active."))))
 
 (defun tpu-substitute (num)
   "Replace the selected region with the contents of the cut buffer, and
@@ -1217,7 +1787,7 @@ A negative argument means replace all occurrences of the search string."
               (tpu-search-internal-core tpu-search-last-string)))
           (setq num (1- num))))
        (t
-        (error "No selection active."))))
+        (tpu-error "No selection active."))))
 
 (defun tpu-lm-replace (from to)
   "Interactively search for OLD-string and substitute NEW-string."
@@ -1227,7 +1797,7 @@ A negative argument means replace all occurrences of the search string."
   (let ((doit t) (strings 0))
 
     ;; Can't replace null strings
-    (if (string= "" from) (error "No string to replace."))
+    (if (string= "" from) (tpu-error "No string to replace."))
 
     ;; Find the first occurrence
     (tpu-set-search)
@@ -1236,46 +1806,50 @@ A negative argument means replace all occurrences of the search string."
     ;; Loop on replace question - yes, no, all, last, or quit.
     (while doit
       (if (not (tpu-check-match)) (setq doit nil)
-       (progn (message "Replace? Type Yes, No, All, Last, or Quit: ")
-              (let ((ans (read-char)))
-
-                (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\r) (= ans ?\ ))
-                       (let ((beg (point)))
-                         (replace-match to (not case-replace) (not tpu-regexp-p))
-                         (setq strings (1+ strings))
-                         (if tpu-searching-forward (forward-char -1) (goto-char beg)))
-                       (tpu-search-internal from t))
-
-                      ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
-                       (tpu-search-internal from t))
-
-                      ((or (= ans ?a) (= ans ?A))
-                       (save-excursion
-                         (let ((beg (point)))
-                           (replace-match to (not case-replace) (not tpu-regexp-p))
-                           (setq strings (1+ strings))
-                           (if tpu-searching-forward (forward-char -1) (goto-char beg)))
-                         (tpu-search-internal-core from t)
-                         (while (tpu-check-match)
-                           (let ((beg (point)))
-                             (replace-match to (not case-replace) (not tpu-regexp-p))
-                             (setq strings (1+ strings))
-                             (if tpu-searching-forward (forward-char -1) (goto-char beg)))
-                           (tpu-search-internal-core from t)))
-                       (setq doit nil))
-
-                      ((or (= ans ?l) (= ans ?L))
-                       (let ((beg (point)))
-                         (replace-match to (not case-replace) (not tpu-regexp-p))
-                         (setq strings (1+ strings))
-                         (if tpu-searching-forward (forward-char -1) (goto-char beg)))
-                       (setq doit nil))
-
-                      ((or (= ans ?q) (= ans ?Q))
-                       (setq doit nil)))))))
-
-    (message "Replaced %s occurrence%s." strings
-            (if (not (= 1 strings)) "s" ""))))
+       (progn
+         (move-overlay tpu-replace-overlay
+                       (tpu-match-beginning) (tpu-match-end) (current-buffer))
+         (message "Replace? Type Yes, No, All, Last, or Quit: ")
+         (let ((ans (read-char)))
+
+           (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\r) (= ans ?\ ))
+                  (let ((beg (point)))
+                    (replace-match to (not case-replace) (not tpu-regexp-p))
+                    (setq strings (1+ strings))
+                    (if tpu-searching-forward (forward-char -1) (goto-char beg)))
+                  (tpu-search-internal from t))
+
+                 ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
+                  (tpu-search-internal from t))
+
+                 ((or (= ans ?a) (= ans ?A))
+                  (save-excursion
+                    (let ((beg (point)))
+                      (replace-match to (not case-replace) (not tpu-regexp-p))
+                      (setq strings (1+ strings))
+                      (if tpu-searching-forward (forward-char -1) (goto-char beg)))
+                    (tpu-search-internal-core from t)
+                    (while (tpu-check-match)
+                      (let ((beg (point)))
+                        (replace-match to (not case-replace) (not tpu-regexp-p))
+                        (setq strings (1+ strings))
+                        (if tpu-searching-forward (forward-char -1) (goto-char beg)))
+                      (tpu-search-internal-core from t)))
+                  (setq doit nil))
+
+                 ((or (= ans ?l) (= ans ?L))
+                  (let ((beg (point)))
+                    (replace-match to (not case-replace) (not tpu-regexp-p))
+                    (setq strings (1+ strings))
+                    (if tpu-searching-forward (forward-char -1) (goto-char beg)))
+                  (setq doit nil))
+
+                 ((or (= ans ?q) (= ans ?Q))
+                  (tpu-unset-match)
+                  (setq doit nil)))))))
+
+    (move-overlay tpu-replace-overlay 1 1 (current-buffer))
+    (message "Replaced %s occurrence%s." strings (if (not (= 1 strings)) "s" ""))))
 
 (defun tpu-emacs-replace (&optional dont-ask)
   "A TPU-edt interface to the emacs replace functions.  If TPU-edt is
@@ -1296,7 +1870,7 @@ are performed without asking.  Only works in forward direction."
 or each line in the entire buffer if no region is selected."
   (interactive
    (list (tpu-string-prompt "String to add: " 'tpu-add-at-bol-hist)))
-  (if (string= "" text) (error "No string specified."))
+  (if (string= "" text) (tpu-error "No string specified."))
   (cond ((tpu-mark)
         (save-excursion
           (if (> (point) (tpu-mark)) (exchange-point-and-mark))
@@ -1314,7 +1888,7 @@ or each line in the entire buffer if no region is selected."
 or each line of the entire buffer if no region is selected."
   (interactive
    (list (tpu-string-prompt "String to add: " 'tpu-add-at-eol-hist)))
-  (if (string= "" text) (error "No string specified."))
+  (if (string= "" text) (tpu-error "No string specified."))
   (cond ((tpu-mark)
         (save-excursion
           (if (> (point) (tpu-mark)) (exchange-point-and-mark))
@@ -1332,7 +1906,11 @@ or each line of the entire buffer if no region is selected."
 (defun tpu-trim-line-ends nil
   "Removes trailing whitespace from every line in the buffer."
   (interactive)
-  (picture-clean))
+  (save-match-data
+    (save-excursion
+      (goto-char (point-min))
+      (while (re-search-forward "[ \t][ \t]*$" nil t)
+       (delete-region (match-beginning 0) (match-end 0))))))
 
 
 ;;;
@@ -1358,9 +1936,9 @@ A repeat count means move that many characters."
 ;;;
 ;;;  Movement by word
 ;;;
-(defconst tpu-word-separator-list '()
+(defvar tpu-word-separator-list '()
   "List of additional word separators.")
-(defconst tpu-skip-chars "^ \t"
+(defvar tpu-skip-chars "^ \t"
   "Characters to skip when moving by word.
 Additional word separators are added to this string.")
 
@@ -1460,7 +2038,7 @@ Prefix argument serves as a repeat count."
 Accepts a prefix argument for the number of lines to move."
   (interactive "p")
   (backward-char 1)
-  (forward-line (- 1 num)))
+  (forward-visible-line (- 1 num)))
 
 (defun tpu-end-of-line (num)
   "Move to the next end of line in the current direction.
@@ -1504,6 +2082,7 @@ Prefix argument serves as a repeat count."
   "Move to beginning of previous line.
 Prefix argument serves as repeat count."
   (interactive "p")
+  (or (bolp) (>= 0 num) (setq num (- num 1)))
   (forward-line (- num)))
 
 
@@ -1614,7 +2193,7 @@ Accepts a prefix argument for the number of tpu-pan-columns to scroll."
   "Move point to ARG percentage of the buffer."
   (interactive "NGoto-percentage: ")
   (if (or (> perc 100) (< perc 0))
-      (error "Percentage %d out of range 0 < percent < 100" perc)
+      (tpu-error "Percentage %d out of range 0 < percent < 100." perc)
     (goto-char (/ (* (point-max) perc) 100))))
 
 (defun tpu-beginning-of-window nil
@@ -1659,311 +2238,52 @@ Accepts a prefix argument for the number of tpu-pan-columns to scroll."
   (tpu-set-search)
   (tpu-update-mode-line))
 
-
-;;;
-;;;  Define keymaps
-;;;
-(define-key global-map "\e[" CSI-map)                         ; CSI map
-(define-key global-map "\eO" SS3-map)                         ; SS3 map
-(define-key SS3-map "P" GOLD-map)                             ; GOLD map
-(define-key GOLD-map "\e[" GOLD-CSI-map)                      ; GOLD-CSI map
-(define-key GOLD-map "\eO" GOLD-SS3-map)                      ; GOLD-SS3 map
-
-
-;;;
-;;;  CSI-map key definitions
-;;;
-(define-key CSI-map "A" 'tpu-previous-line)                   ; up
-(define-key CSI-map "B" 'tpu-next-line)                       ; down
-(define-key CSI-map "D" 'tpu-backward-char)                   ; left
-(define-key CSI-map "C" 'tpu-forward-char)                    ; right
-
-(define-key CSI-map "1~" 'tpu-search)                         ; Find
-(define-key CSI-map "2~" 'tpu-paste)                          ; Insert Here
-(define-key CSI-map "3~" 'tpu-cut)                            ; Remove
-(define-key CSI-map "4~" 'tpu-select)                         ; Select
-(define-key CSI-map "5~" 'tpu-scroll-window-down)             ; Prev Screen
-(define-key CSI-map "6~" 'tpu-scroll-window-up)               ; Next Screen
-
-(define-key CSI-map "11~" 'nil)                               ; F1
-(define-key CSI-map "12~" 'nil)                               ; F2
-(define-key CSI-map "13~" 'nil)                               ; F3
-(define-key CSI-map "14~" 'nil)                               ; F4
-(define-key CSI-map "15~" 'nil)                               ; F5
-(define-key CSI-map "17~" 'nil)                               ; F6
-(define-key CSI-map "18~" 'nil)                               ; F7
-(define-key CSI-map "19~" 'nil)                               ; F8
-(define-key CSI-map "20~" 'nil)                               ; F9
-(define-key CSI-map "21~" 'tpu-exit)                          ; F10
-(define-key CSI-map "23~" 'tpu-insert-escape)                 ; F11 (ESC)
-(define-key CSI-map "24~" 'tpu-next-beginning-of-line)        ; F12 (BS)
-(define-key CSI-map "25~" 'tpu-delete-previous-word)          ; F13 (LF)
-(define-key CSI-map "26~" 'tpu-toggle-overwrite-mode)         ; F14
-(define-key CSI-map "28~" 'tpu-help)                          ; HELP
-(define-key CSI-map "29~" 'execute-extended-command)          ; DO
-(define-key CSI-map "31~" 'tpu-goto-breadcrumb)               ; F17
-(define-key CSI-map "32~" 'nil)                               ; F18
-(define-key CSI-map "33~" 'nil)                               ; F19
-(define-key CSI-map "34~" 'nil)                               ; F20
-
-
-;;;
-;;;  SS3-map key definitions
-;;;
-(define-key SS3-map "A" 'tpu-previous-line)                   ; up
-(define-key SS3-map "B" 'tpu-next-line)                       ; down
-(define-key SS3-map "C" 'tpu-forward-char)                    ; right
-(define-key SS3-map "D" 'tpu-backward-char)                   ; left
-
-(define-key SS3-map "Q" 'tpu-help)                            ; PF2
-(define-key SS3-map "R" 'tpu-search-again)                    ; PF3
-(define-key SS3-map "S" 'tpu-delete-current-line)             ; PF4
-(define-key SS3-map "p" 'tpu-line)                            ; KP0
-(define-key SS3-map "q" 'tpu-word)                            ; KP1
-(define-key SS3-map "r" 'tpu-end-of-line)                     ; KP2
-(define-key SS3-map "s" 'tpu-char)                            ; KP3
-(define-key SS3-map "t" 'tpu-advance-direction)               ; KP4
-(define-key SS3-map "u" 'tpu-backup-direction)                ; KP5
-(define-key SS3-map "v" 'tpu-cut)                             ; KP6
-(define-key SS3-map "w" 'tpu-page)                            ; KP7
-(define-key SS3-map "x" 'tpu-scroll-window)                   ; KP8
-(define-key SS3-map "y" 'tpu-append-region)                   ; KP9
-(define-key SS3-map "m" 'tpu-delete-current-word)             ; KP-
-(define-key SS3-map "l" 'tpu-delete-current-char)             ; KP,
-(define-key SS3-map "n" 'tpu-select)                          ; KP.
-(define-key SS3-map "M" 'newline)                             ; KPenter
-
-
-;;;
-;;;  GOLD-map key definitions
-;;;
-(define-key GOLD-map "\C-A" 'tpu-toggle-overwrite-mode)       ; ^A
-(define-key GOLD-map "\C-B" 'nil)                             ; ^B
-(define-key GOLD-map "\C-C" 'nil)                             ; ^C
-(define-key GOLD-map "\C-D" 'nil)                             ; ^D
-(define-key GOLD-map "\C-E" 'nil)                             ; ^E
-(define-key GOLD-map "\C-F" 'set-visited-file-name)           ; ^F
-(define-key GOLD-map "\C-g" 'keyboard-quit)                   ; safety first
-(define-key GOLD-map "\C-h" 'delete-other-windows)            ; BS
-(define-key GOLD-map "\C-i" 'other-window)                    ; TAB
-(define-key GOLD-map "\C-J" 'nil)                             ; ^J
-(define-key GOLD-map "\C-K" 'tpu-define-macro-key)            ; ^K
-(define-key GOLD-map "\C-l" 'downcase-region)                 ; ^L
-(define-key GOLD-map "\C-M" 'nil)                             ; ^M
-(define-key GOLD-map "\C-N" 'nil)                             ; ^N
-(define-key GOLD-map "\C-O" 'nil)                             ; ^O
-(define-key GOLD-map "\C-P" 'nil)                             ; ^P
-(define-key GOLD-map "\C-Q" 'nil)                             ; ^Q
-(define-key GOLD-map "\C-R" 'nil)                             ; ^R
-(define-key GOLD-map "\C-S" 'nil)                             ; ^S
-(define-key GOLD-map "\C-T" 'tpu-toggle-control-keys)         ; ^T
-(define-key GOLD-map "\C-u" 'upcase-region)                   ; ^U
-(define-key GOLD-map "\C-V" 'nil)                             ; ^V
-(define-key GOLD-map "\C-w" 'tpu-write-current-buffers)       ; ^W
-(define-key GOLD-map "\C-X" 'nil)                             ; ^X
-(define-key GOLD-map "\C-Y" 'nil)                             ; ^Y
-(define-key GOLD-map "\C-Z" 'nil)                             ; ^Z
-(define-key GOLD-map " " 'undo)                               ; SPC
-(define-key GOLD-map "!" 'nil)                                ; !
-(define-key GOLD-map "#" 'nil)                                ; #
-(define-key GOLD-map "$" 'tpu-add-at-eol)                     ; $
-(define-key GOLD-map "%" 'tpu-goto-percent)                   ; %
-(define-key GOLD-map "&" 'nil)                                ; &
-(define-key GOLD-map "(" 'nil)                                ; (
-(define-key GOLD-map ")" 'nil)                                ; )
-(define-key GOLD-map "*" 'tpu-toggle-regexp)                  ; *
-(define-key GOLD-map "+" 'nil)                                ; +
-(define-key GOLD-map "," 'tpu-goto-breadcrumb)                ; ,
-(define-key GOLD-map "-" 'negative-argument)                  ; -
-(define-key GOLD-map "." 'tpu-drop-breadcrumb)                ; .
-(define-key GOLD-map "/" 'tpu-emacs-replace)                  ; /
-(define-key GOLD-map "0" 'digit-argument)                     ; 0
-(define-key GOLD-map "1" 'digit-argument)                     ; 1
-(define-key GOLD-map "2" 'digit-argument)                     ; 2
-(define-key GOLD-map "3" 'digit-argument)                     ; 3
-(define-key GOLD-map "4" 'digit-argument)                     ; 4
-(define-key GOLD-map "5" 'digit-argument)                     ; 5
-(define-key GOLD-map "6" 'digit-argument)                     ; 6
-(define-key GOLD-map "7" 'digit-argument)                     ; 7
-(define-key GOLD-map "8" 'digit-argument)                     ; 8
-(define-key GOLD-map "9" 'digit-argument)                     ; 9
-(define-key GOLD-map ":" 'nil)                                ; :
-(define-key GOLD-map ";" 'tpu-trim-line-ends)                 ; ;
-(define-key GOLD-map "<" 'nil)                                ; <
-(define-key GOLD-map "=" 'nil)                                ; =
-(define-key GOLD-map ">" 'nil)                                ; >
-(define-key GOLD-map "?" 'tpu-spell-check)                    ; ?
-(define-key GOLD-map "A" 'tpu-toggle-newline-and-indent)      ; A
-(define-key GOLD-map "B" 'tpu-next-buffer)                    ; B
-(define-key GOLD-map "C" 'repeat-complex-command)             ; C
-(define-key GOLD-map "D" 'shell-command)                      ; D
-(define-key GOLD-map "E" 'tpu-exit)                           ; E
-(define-key GOLD-map "F" 'nil)                                ; F
-(define-key GOLD-map "G" 'tpu-get)                            ; G
-(define-key GOLD-map "H" 'nil)                                ; H
-(define-key GOLD-map "I" 'tpu-include)                        ; I
-(define-key GOLD-map "K" 'tpu-kill-buffer)                    ; K
-(define-key GOLD-map "L" 'tpu-what-line)                      ; L
-(define-key GOLD-map "M" 'buffer-menu)                        ; M
-(define-key GOLD-map "N" 'tpu-next-file-buffer)               ; N
-(define-key GOLD-map "O" 'occur)                              ; O
-(define-key GOLD-map "P" 'lpr-buffer)                         ; P
-(define-key GOLD-map "Q" 'tpu-quit)                           ; Q
-(define-key GOLD-map "R" 'tpu-toggle-rectangle)               ; R
-(define-key GOLD-map "S" 'replace)                            ; S
-(define-key GOLD-map "T" 'tpu-line-to-top-of-window)          ; T
-(define-key GOLD-map "U" 'undo)                               ; U
-(define-key GOLD-map "V" 'tpu-version)                        ; V
-(define-key GOLD-map "W" 'save-buffer)                        ; W
-(define-key GOLD-map "X" 'tpu-save-all-buffers-kill-emacs)    ; X
-(define-key GOLD-map "Y" 'copy-region-as-kill)                ; Y
-(define-key GOLD-map "Z" 'suspend-emacs)                      ; Z
-(define-key GOLD-map "[" 'blink-matching-open)                ; [
-(define-key GOLD-map "\\" 'nil)                               ; \
-(define-key GOLD-map "]" 'blink-matching-open)                ; ]
-(define-key GOLD-map "^" 'tpu-add-at-bol)                     ; ^
-(define-key GOLD-map "_" 'split-window-vertically)            ; -
-(define-key GOLD-map "`" 'what-line)                          ; `
-(define-key GOLD-map "a" 'tpu-toggle-newline-and-indent)      ; a
-(define-key GOLD-map "b" 'tpu-next-buffer)                    ; b
-(define-key GOLD-map "c" 'repeat-complex-command)             ; c
-(define-key GOLD-map "d" 'shell-command)                      ; d
-(define-key GOLD-map "e" 'tpu-exit)                           ; e
-(define-key GOLD-map "f" 'nil)                                ; f
-(define-key GOLD-map "g" 'tpu-get)                            ; g
-(define-key GOLD-map "h" 'nil)                                ; h
-(define-key GOLD-map "i" 'tpu-include)                        ; i
-(define-key GOLD-map "k" 'tpu-kill-buffer)                    ; k
-(define-key GOLD-map "l" 'goto-line)                          ; l
-(define-key GOLD-map "m" 'buffer-menu)                        ; m
-(define-key GOLD-map "n" 'tpu-next-file-buffer)               ; n
-(define-key GOLD-map "o" 'occur)                              ; o
-(define-key GOLD-map "p" 'lpr-region)                         ; p
-(define-key GOLD-map "q" 'tpu-quit)                           ; q
-(define-key GOLD-map "r" 'tpu-toggle-rectangle)               ; r
-(define-key GOLD-map "s" 'replace)                            ; s
-(define-key GOLD-map "t" 'tpu-line-to-top-of-window)          ; t
-(define-key GOLD-map "u" 'undo)                               ; u
-(define-key GOLD-map "v" 'tpu-version)                        ; v
-(define-key GOLD-map "w" 'save-buffer)                        ; w
-(define-key GOLD-map "x" 'tpu-save-all-buffers-kill-emacs)    ; x
-(define-key GOLD-map "y" 'copy-region-as-kill)                ; y
-(define-key GOLD-map "z" 'suspend-emacs)                      ; z
-(define-key GOLD-map "{" 'nil)                                ; {
-(define-key GOLD-map "|" 'split-window-horizontally)          ; |
-(define-key GOLD-map "}" 'nil)                                ; }
-(define-key GOLD-map "~" 'exchange-point-and-mark)            ; ~
-(define-key GOLD-map "\177" 'delete-window)                   ; <X]
-
-
-;;;
-;;;  GOLD-CSI-map key definitions
-;;;
-(define-key GOLD-CSI-map "A" 'tpu-move-to-beginning)          ; up-arrow
-(define-key GOLD-CSI-map "B" 'tpu-move-to-end)                ; down-arrow
-(define-key GOLD-CSI-map "C" 'end-of-line)                    ; right-arrow
-(define-key GOLD-CSI-map "D" 'beginning-of-line)              ; left-arrow
-
-(define-key GOLD-CSI-map "1~" 'nil)                           ; Find
-(define-key GOLD-CSI-map "2~" 'nil)                           ; Insert Here
-(define-key GOLD-CSI-map "3~" 'tpu-store-text)                ; Remove
-(define-key GOLD-CSI-map "4~" 'tpu-unselect)                  ; Select
-(define-key GOLD-CSI-map "5~" 'tpu-previous-window)           ; Prev Screen
-(define-key GOLD-CSI-map "6~" 'tpu-next-window)               ; Next Screen
-
-(define-key GOLD-CSI-map "11~" 'nil)                          ; F1
-(define-key GOLD-CSI-map "12~" 'nil)                          ; F2
-(define-key GOLD-CSI-map "13~" 'nil)                          ; F3
-(define-key GOLD-CSI-map "14~" 'nil)                          ; F4
-(define-key GOLD-CSI-map "16~" 'nil)                          ; F5
-(define-key GOLD-CSI-map "17~" 'nil)                          ; F6
-(define-key GOLD-CSI-map "18~" 'nil)                          ; F7
-(define-key GOLD-CSI-map "19~" 'nil)                          ; F8
-(define-key GOLD-CSI-map "20~" 'nil)                          ; F9
-(define-key GOLD-CSI-map "21~" 'nil)                          ; F10
-(define-key GOLD-CSI-map "23~" 'nil)                          ; F11
-(define-key GOLD-CSI-map "24~" 'nil)                          ; F12
-(define-key GOLD-CSI-map "25~" 'nil)                          ; F13
-(define-key GOLD-CSI-map "26~" 'nil)                          ; F14
-(define-key GOLD-CSI-map "28~" 'describe-bindings)            ; HELP
-(define-key GOLD-CSI-map "29~" 'nil)                          ; DO
-(define-key GOLD-CSI-map "31~" 'tpu-drop-breadcrumb)          ; F17
-(define-key GOLD-CSI-map "32~" 'nil)                          ; F18
-(define-key GOLD-CSI-map "33~" 'nil)                          ; F19
-(define-key GOLD-CSI-map "34~" 'nil)                          ; F20
-
-
-;;;
-;;;  GOLD-SS3-map key definitions
-;;;
-(define-key GOLD-SS3-map "A" 'tpu-move-to-beginning)          ; up-arrow
-(define-key GOLD-SS3-map "B" 'tpu-move-to-end)                ; down-arrow
-(define-key GOLD-SS3-map "C" 'end-of-line)                    ; right-arrow
-(define-key GOLD-SS3-map "D" 'beginning-of-line)              ; left-arrow
-
-(define-key GOLD-SS3-map "P" 'keyboard-quit)                  ; PF1
-(define-key GOLD-SS3-map "Q" 'help-for-help)                  ; PF2
-(define-key GOLD-SS3-map "R" 'tpu-search)                     ; PF3
-(define-key GOLD-SS3-map "S" 'tpu-undelete-lines)             ; PF4
-(define-key GOLD-SS3-map "p" 'open-line)                      ; KP0
-(define-key GOLD-SS3-map "q" 'tpu-change-case)                ; KP1
-(define-key GOLD-SS3-map "r" 'tpu-delete-to-eol)              ; KP2
-(define-key GOLD-SS3-map "s" 'tpu-special-insert)             ; KP3
-(define-key GOLD-SS3-map "t" 'tpu-move-to-end)                ; KP4
-(define-key GOLD-SS3-map "u" 'tpu-move-to-beginning)          ; KP5
-(define-key GOLD-SS3-map "v" 'tpu-paste)                      ; KP6
-(define-key GOLD-SS3-map "w" 'execute-extended-command)       ; KP7
-(define-key GOLD-SS3-map "x" 'tpu-fill)                       ; KP8
-(define-key GOLD-SS3-map "y" 'tpu-replace)                    ; KP9
-(define-key GOLD-SS3-map "m" 'tpu-undelete-words)             ; KP-
-(define-key GOLD-SS3-map "l" 'tpu-undelete-char)              ; KP,
-(define-key GOLD-SS3-map "n" 'tpu-unselect)                   ; KP.
-(define-key GOLD-SS3-map "M" 'tpu-substitute)                 ; KPenter
-
-
-;;;
-;;;  Repeat complex command map additions to make arrows work
-;;;
-(cond ((boundp 'repeat-complex-command-map)
-       (define-key repeat-complex-command-map "\e[A" 'previous-complex-command)
-       (define-key repeat-complex-command-map "\e[B" 'next-complex-command)
-       (define-key repeat-complex-command-map "\eOA" 'previous-complex-command)
-       (define-key repeat-complex-command-map "\eOB" 'next-complex-command)))
+(defun tpu-toggle-direction nil
+  "Change the current TPU direction."
+  (interactive)
+  (if tpu-advance (tpu-backup-direction) (tpu-advance-direction)))
 
 
 ;;;
 ;;;  Minibuffer map additions to make KP_enter = RET
 ;;;
-(define-key minibuffer-local-map "\eOM" 'exit-minibuffer)
-(define-key minibuffer-local-ns-map "\eOM" 'exit-minibuffer)
-(define-key minibuffer-local-completion-map "\eOM" 'exit-minibuffer)
-(define-key minibuffer-local-must-match-map "\eOM" 'minibuffer-complete-and-exit)
-(and (boundp 'repeat-complex-command-map)
-     (define-key repeat-complex-command-map "\eOM" 'exit-minibuffer))
+;; Standard Emacs settings under xterm in function-key-map map
+;; "\eOM" to [kp-enter] and [kp-enter] to RET, but since the output of the map
+;; is not fed back into the map, the key stays as kp-enter :-(.
+(define-key minibuffer-local-map [kp-enter] 'exit-minibuffer)
+;; These are not necessary because they are inherited.
+;; (define-key minibuffer-local-ns-map [kp-enter] 'exit-minibuffer)
+;; (define-key minibuffer-local-completion-map [kp-enter] 'exit-minibuffer)
+(define-key minibuffer-local-must-match-map [kp-enter] 'minibuffer-complete-and-exit)
 
 
 ;;;
-;;;  Map control keys
+;;;  Minibuffer map additions to set search direction
 ;;;
-(define-key global-map "\C-\\" 'quoted-insert)                ; ^\
-(define-key global-map "\C-a" 'tpu-toggle-overwrite-mode)     ; ^A
-(define-key global-map "\C-b" 'repeat-complex-command)        ; ^B
-(define-key global-map "\C-e" 'tpu-current-end-of-line)       ; ^E
-(define-key global-map "\C-f" 'set-visited-file-name)         ; ^F
-(define-key global-map "\C-h" 'tpu-next-beginning-of-line)    ; ^H (BS)
-(define-key global-map "\C-j" 'tpu-delete-previous-word)      ; ^J (LF)
-(define-key global-map "\C-k" 'tpu-define-macro-key)          ; ^K
-(define-key global-map "\C-l" 'tpu-insert-formfeed)           ; ^L (FF)
-(define-key global-map "\C-r" 'recenter)                      ; ^R
-(define-key global-map "\C-u" 'tpu-delete-to-bol)             ; ^U
-(define-key global-map "\C-v" 'quoted-insert)                 ; ^V
-(define-key global-map "\C-w" 'redraw-display)                ; ^W
-(define-key global-map "\C-z" 'tpu-exit)                      ; ^Z
+(define-key minibuffer-local-map "\eOt" 'tpu-search-forward-exit)  ;KP4
+(define-key minibuffer-local-map "\eOu" 'tpu-search-backward-exit) ;KP5
 
 
 ;;;
-;;;  Functions to reset and toggle the control key bindings
+;;;  Functions to set, reset, and toggle the control key bindings
 ;;;
+(defun tpu-set-control-keys nil
+  "Set control keys to TPU style functions."
+  (define-key global-map "\C-\\" 'quoted-insert)                ; ^\
+  (define-key global-map "\C-a" 'tpu-toggle-overwrite-mode)     ; ^A
+  (define-key global-map "\C-b" 'repeat-complex-command)        ; ^B
+  (define-key global-map "\C-e" 'tpu-current-end-of-line)       ; ^E
+  (define-key global-map "\C-h" 'tpu-next-beginning-of-line)    ; ^H (BS)
+  (define-key global-map "\C-j" 'tpu-delete-previous-word)      ; ^J (LF)
+  (define-key global-map "\C-k" 'tpu-define-macro-key)          ; ^K
+  (define-key global-map "\C-l" 'tpu-insert-formfeed)           ; ^L (FF)
+  (define-key global-map "\C-r" 'recenter)                      ; ^R
+  (define-key global-map "\C-u" 'tpu-delete-to-bol)             ; ^U
+  (define-key global-map "\C-v" 'tpu-quoted-insert)             ; ^V
+  (define-key global-map "\C-w" 'redraw-display)                ; ^W
+  (define-key global-map "\C-z" 'tpu-exit)                      ; ^Z
+  (setq tpu-control-keys t))
+
 (defun tpu-reset-control-keys (tpu-style)
   "Set control keys to TPU or emacs style functions."
   (let* ((tpu   (and tpu-style (not tpu-control-keys)))
@@ -1971,15 +2291,12 @@ Accepts a prefix argument for the number of tpu-pan-columns to scroll."
         (doit  (or tpu emacs)))
     (cond (doit
           (if emacs (setq tpu-global-map (copy-keymap global-map)))
-          (let ((map (if tpu
-                         (copy-keymap tpu-global-map)
-                       (copy-keymap tpu-original-global-map))))
+          (let ((map (if tpu tpu-global-map tpu-original-global-map)))
 
             (define-key global-map "\C-\\" (lookup-key map "\C-\\"))   ; ^\
             (define-key global-map "\C-a" (lookup-key map "\C-a"))     ; ^A
             (define-key global-map "\C-b" (lookup-key map "\C-b"))     ; ^B
             (define-key global-map "\C-e" (lookup-key map "\C-e"))     ; ^E
-            (define-key global-map "\C-f" (lookup-key map "\C-f"))     ; ^F
             (define-key global-map "\C-h" (lookup-key map "\C-h"))     ; ^H (BS)
             (define-key global-map "\C-j" (lookup-key map "\C-j"))     ; ^J (LF)
             (define-key global-map "\C-k" (lookup-key map "\C-k"))     ; ^K
@@ -2018,23 +2335,23 @@ Accepts a prefix argument for the number of tpu-pan-columns to scroll."
 (defun tpu-arrow-history nil
   "Modify minibuffer maps to use arrows for history recall."
   (interactive)
-  (let ((loc (where-is-internal 'tpu-previous-line)) (cur nil))
-    (while (setq cur (car loc))
-      (define-key read-expression-map cur 'tpu-previous-history-element)
-      (define-key minibuffer-local-map cur 'tpu-previous-history-element)
-      (define-key minibuffer-local-ns-map cur 'tpu-previous-history-element)
-      (define-key minibuffer-local-completion-map cur 'tpu-previous-history-element)
-      (define-key minibuffer-local-must-match-map cur 'tpu-previous-history-element)
-      (setq loc (cdr loc)))
-
-    (setq loc (where-is-internal 'tpu-next-line))
-    (while (setq cur (car loc))
-      (define-key read-expression-map cur 'tpu-next-history-element)
-      (define-key minibuffer-local-map cur 'tpu-next-history-element)
-      (define-key minibuffer-local-ns-map cur 'tpu-next-history-element)
-      (define-key minibuffer-local-completion-map cur 'tpu-next-history-element)
-      (define-key minibuffer-local-must-match-map cur 'tpu-next-history-element)
-      (setq loc (cdr loc)))))
+  (dolist (cur (where-is-internal 'tpu-previous-line))
+    (define-key read-expression-map cur 'tpu-previous-history-element)
+    (define-key minibuffer-local-map cur 'tpu-previous-history-element)
+    ;; These are inherited anyway.  --Stef
+    ;; (define-key minibuffer-local-ns-map cur 'tpu-previous-history-element)
+    ;; (define-key minibuffer-local-completion-map cur 'tpu-previous-history-element)
+    ;; (define-key minibuffer-local-must-match-map cur 'tpu-previous-history-element)
+    )
+
+  (dolist (cur (where-is-internal 'tpu-next-line))
+    (define-key read-expression-map cur 'tpu-next-history-element)
+    (define-key minibuffer-local-map cur 'tpu-next-history-element)
+    ;; These are inherited anyway.  --Stef
+    ;; (define-key minibuffer-local-ns-map cur 'tpu-next-history-element)
+    ;; (define-key minibuffer-local-completion-map cur 'tpu-next-history-element)
+    ;; (define-key minibuffer-local-must-match-map cur 'tpu-next-history-element)
+    ))
 
 
 ;;;
@@ -2043,16 +2360,25 @@ Accepts a prefix argument for the number of tpu-pan-columns to scroll."
 (defun tpu-load-xkeys (file)
   "Load the TPU-edt X-windows key definitions FILE.
 If FILE is nil, try to load a default file.  The default file names are
-~/.tpu-lucid-keys for Lucid emacs, and ~/.tpu-gnu-keys for GNU emacs."
+`~/.tpu-lucid-keys' for Lucid emacs, and `~/.tpu-keys' for Emacs."
   (interactive "fX key definition file: ")
   (cond (file
         (setq file (expand-file-name file)))
        (tpu-xkeys-file
         (setq file (expand-file-name tpu-xkeys-file)))
-       (tpu-gnu-emacs19-p
-        (setq file (expand-file-name "~/.tpu-gnu-keys")))
-       (tpu-lucid-emacs19-p
-        (setq file (expand-file-name "~/.tpu-lucid-keys"))))
+       (tpu-lucid-emacs-p
+        (setq file (convert-standard-filename
+                    (expand-file-name "~/.tpu-lucid-keys"))))
+       (t
+        (setq file (convert-standard-filename
+                    (expand-file-name "~/.tpu-keys")))
+        (and (not (file-exists-p file))
+             (file-exists-p
+              (convert-standard-filename
+               (expand-file-name "~/.tpu-gnu-keys")))
+             (tpu-copy-keyfile
+              (convert-standard-filename
+               (expand-file-name "~/.tpu-gnu-keys")) file))))
   (cond ((file-readable-p file)
         (load-file file))
        (t
@@ -2063,7 +2389,7 @@ If FILE is nil, try to load a default file.  The default file names are
      Ack!!  You're running TPU-edt under X-windows without loading an
      X  key definition file.   To create a  TPU-edt X  key definition
      file, run the tpu-mapper.el program.  It  came with TPU-edt.  It
-     even includes directions on how to  use it!  Perhaps it's laying
+     even includes directions on how to  use it!   Perhaps it's lying
      around here someplace.  ")
         (let ((file "tpu-mapper.el")
               (found nil)
@@ -2085,55 +2411,72 @@ If FILE is nil, try to load a default file.  The default file names are
                  (insert "Nope, I can't seem to find it.  :-(\n\n")
                  (sit-for 120)))))))
 
+(defun tpu-copy-keyfile (oldname newname)
+  "Copy the TPU-edt X key definitions file to the new default name."
+  (interactive "fOld name: \nFNew name: ")
+  (if (not (get-buffer "*TPU-Notice*")) (generate-new-buffer "*TPU-Notice*"))
+  (set-buffer "*TPU-Notice*")
+  (erase-buffer)
+  (insert "
+  NOTICE --
+
+  The default name of the TPU-edt key definition file has changed
+  from `~/.tpu-gnu-keys' to `~/.tpu-keys'.  With your permission,
+  your key definitions will be copied to the new file.  If you'll
+  never use older versions of Emacs, you can remove the old file.
+  If the copy fails, you'll be asked if you want to create a new
+  key definitions file.  Do you want to copy your key definition
+  file now?
+  ")
+  (save-window-excursion
+    (switch-to-buffer-other-window "*TPU-Notice*")
+    (shrink-window-if-larger-than-buffer)
+    (goto-char (point-min))
+    (beep)
+    (and (tpu-y-or-n-p "Copy key definitions to the new file now? ")
+        (condition-case conditions
+             (copy-file oldname newname)
+          (tpu-error (message "Sorry, couldn't copy - %s." (cdr conditions)))))
+    (kill-buffer "*TPU-Notice*")))
+
 
 ;;;
 ;;;  Start and Stop TPU-edt
 ;;;
 ;;;###autoload
-(defun tpu-edt-on nil
+(defun tpu-edt-on ()
   "Turn on TPU/edt emulation."
   (interactive)
-  (cond
-   ((not tpu-edt-mode)
-    ;; we use picture-mode functions
-    (require 'picture)
-    (tpu-reset-control-keys t)
-    (cond (tpu-emacs19-p
-          (and window-system (tpu-load-xkeys nil))
-          (tpu-arrow-history))
-         (t
-          ;; define ispell functions
-          (autoload 'ispell-word "ispell" "Check spelling of word at or before point" t)
-          (autoload 'ispell-complete-word "ispell" "Complete word at or before point" t)
-          (autoload 'ispell-buffer "ispell" "Check spelling of entire buffer" t)
-          (autoload 'ispell-region "ispell" "Check spelling of region" t)))
-    (tpu-set-mode-line t)
-    (tpu-advance-direction)
-    ;; set page delimiter, display line truncation, and scrolling like TPU
-    (setq-default page-delimiter "\f")
-    (setq-default truncate-lines t)
-    (setq scroll-step 1)
-    (setq tpu-edt-mode t))))
-
-(defun tpu-edt-off nil
+  (and window-system (tpu-load-xkeys nil))
+  (tpu-arrow-history)
+  (transient-mark-mode t)
+  (add-hook 'post-command-hook 'tpu-search-highlight)
+  (tpu-set-mode-line t)
+  (tpu-advance-direction)
+  ;; set page delimiter, display line truncation, and scrolling like TPU
+  (setq-default page-delimiter "\f")
+  (setq-default truncate-lines t)
+  (setq scroll-step 1)
+  (setq global-map (copy-keymap global-map))
+  (tpu-set-control-keys)
+  (define-key global-map "\e[" CSI-map)
+  (define-key global-map "\eO" SS3-map)
+  (setq tpu-edt-mode t))
+
+(defun tpu-edt-off ()
   "Turn off TPU/edt emulation.  Note that the keypad is left on."
   (interactive)
-  (cond
-   (tpu-edt-mode
-    (tpu-reset-control-keys nil)
-    (tpu-set-mode-line nil)
-    (setq-default page-delimiter "^\f")
-    (setq-default truncate-lines nil)
-    (setq scroll-step 0)
-    (use-global-map global-map)
-    (setq tpu-edt-mode nil))))
-
-
-;;;
-;;;  Turn on TPU-edt and announce it as a feature
-;;;
-(tpu-edt-mode)
+  (tpu-reset-control-keys nil)
+  (remove-hook 'post-command-hook 'tpu-search-highlight)
+  (tpu-set-mode-line nil)
+  (setq-default page-delimiter "^\f")
+  (setq-default truncate-lines nil)
+  (setq scroll-step 0)
+  (setq global-map tpu-original-global-map)
+  (use-global-map global-map)
+  (setq tpu-edt-mode nil))
 
 (provide 'tpu-edt)
 
+;; arch-tag: f3dfe61c-2cbd-4f73-b9cc-eb215020b857
 ;;; tpu-edt.el ends here