home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / tpu-edt-3.0 / tpu-edt.el < prev    next >
Encoding:
Text File  |  1993-06-24  |  74.8 KB  |  2,119 lines

  1. ;;; tpu-edt.el --- Emacs emulating TPU emulating EDT
  2.  
  3. ;; Copyright (C) 1993 Rob Riepel.
  4.  
  5. ;; Author: Rob Riepel <riepel@networking.stanford.edu>
  6. ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
  7. ;; Version: 3.0
  8. ;; Keywords: tpu edt tpu-edt
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  12. ;; accepts responsibility to anyone for the consequences of using it
  13. ;; or for whether it serves any particular purpose or works at all,
  14. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  15. ;; License for full details.
  16.  
  17. ;; Everyone is granted permission to copy, modify and redistribute
  18. ;; GNU Emacs, but only under the conditions described in the
  19. ;; GNU Emacs General Public License.   A copy of this license is
  20. ;; supposed to have been given to you along with GNU Emacs so you
  21. ;; can know your rights and responsibilities.  It should be in a
  22. ;; file named COPYING.  Among other things, the copyright notice
  23. ;; and this notice must be preserved on all copies.
  24. ;;
  25.  
  26.  
  27. ;;;
  28. ;;;  Revision Information
  29. ;;;
  30. (defconst tpu-revision "$Revision: 6.4 $"
  31.   "Revision number of TPU-edt.")
  32. (defconst tpu-revision-date "$Date: 1993/06/20 19:43:18 $"
  33.   "Date current revision of TPU-edt was created.")
  34.  
  35.  
  36. ;;;
  37. ;;;  User Configurable Variables
  38. ;;;
  39. (defconst tpu-have-ispell t
  40.   "*If non-nil (default), TPU-edt uses ispell for spell checking.")
  41.  
  42. (defconst tpu-kill-buffers-silently nil
  43.   "*If non-nil, TPU-edt kills modified buffers without asking.")
  44.  
  45. (defvar tpu-percent-scroll 75
  46.   "*Percentage of the screen to scroll for next/previous screen commands.")
  47.  
  48. (defvar tpu-pan-columns 16
  49.   "*Number of columns the tpu-pan functions scroll left or right.")
  50.  
  51.  
  52. ;;;
  53. ;;;  Emacs version identifiers - currently referenced by
  54. ;;;
  55. ;;;     o tpu-mark              o tpu-set-mark
  56. ;;;     o tpu-string-prompt     o tpu-regexp-prompt
  57. ;;;     o tpu-edt-on            o tpu-load-xkeys
  58. ;;;     o tpu-update-mode-line  o mode line section
  59. ;;;
  60. (defconst tpu-emacs19-p (not (string-lessp emacs-version "19"))
  61.   "Non-NIL if we are running Lucid or GNU Emacs version 19.")
  62.  
  63. (defconst tpu-gnu-emacs18-p (not tpu-emacs19-p)
  64.   "Non-NIL if we are running GNU Emacs version 18.")
  65.  
  66. (defconst tpu-lucid-emacs19-p
  67.   (and tpu-emacs19-p (string-match "Lucid" emacs-version))
  68.   "Non-NIL if we are running Lucid Emacs version 19.")
  69.  
  70. (defconst tpu-gnu-emacs19-p (and tpu-emacs19-p (not tpu-lucid-emacs19-p))
  71.   "Non-NIL if we are running GNU Emacs version 19.")
  72.  
  73.  
  74. ;;;
  75. ;;;  Global Keymaps
  76. ;;;
  77. (defvar CSI-map (make-sparse-keymap)
  78.   "Maps the CSI function keys on the VT100 keyboard.
  79. CSI is DEC's name for the sequence <ESC>[.")
  80.  
  81. (defvar SS3-map (make-sparse-keymap)
  82.   "Maps the SS3 function keys on the VT100 keyboard.
  83. SS3 is DEC's name for the sequence <ESC>O.")
  84.  
  85. (defvar GOLD-map (make-keymap)
  86.   "Maps the function keys on the VT100 keyboard preceeded by PF1.
  87. GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
  88.  
  89. (defvar GOLD-CSI-map (make-sparse-keymap)
  90.   "Maps the function keys on the VT100 keyboard preceeded by GOLD-CSI.")
  91.  
  92. (defvar GOLD-SS3-map (make-sparse-keymap)
  93.   "Maps the function keys on the VT100 keyboard preceeded by GOLD-SS3.")
  94.  
  95. (defvar tpu-original-global-map (copy-keymap global-map)
  96.   "Original global keymap.")
  97.  
  98. (and tpu-lucid-emacs19-p
  99.      (defvar minibuffer-local-ns-map (make-sparse-keymap)
  100.        "Hack to give Lucid emacs the same maps as GNU emacs."))
  101.  
  102.  
  103. ;;;
  104. ;;;  Global Variables
  105. ;;;
  106. (defvar tpu-edt-mode nil
  107.   "If non-nil, TPU-edt mode is active.")
  108.  
  109. (defvar tpu-last-replaced-text ""
  110.   "Last text deleted by a TPU-edt replace command.")
  111. (defvar tpu-last-deleted-region ""
  112.   "Last text deleted by a TPU-edt remove command.")
  113. (defvar tpu-last-deleted-lines ""
  114.   "Last text deleted by a TPU-edt line-delete command.")
  115. (defvar tpu-last-deleted-words ""
  116.   "Last text deleted by a TPU-edt word-delete command.")
  117. (defvar tpu-last-deleted-char ""
  118.   "Last character deleted by a TPU-edt character-delete command.")
  119.  
  120. (defvar tpu-search-last-string ""
  121.   "Last text searched for by the TPU-edt search commands.")
  122.  
  123. (defvar tpu-regexp-p nil
  124.   "If non-nil, TPU-edt uses regexp search and replace routines.")
  125. (defvar tpu-rectangular-p nil
  126.   "If non-nil, TPU-edt removes and inserts rectangles.")
  127. (defvar tpu-advance t
  128.   "True when TPU-edt is operating in the forward direction.")
  129. (defvar tpu-reverse nil
  130.   "True when TPU-edt is operating in the backward direction.")
  131. (defvar tpu-control-keys t
  132.   "If non-nil, control keys are set to perform TPU functions.")
  133.  
  134. (defvar tpu-rectangle-string nil
  135.   "Mode line string to identify rectangular mode.")
  136. (defvar tpu-direction-string nil
  137.   "Mode line string to identify current direction.")
  138.  
  139. (defvar tpu-add-at-bol-hist nil
  140.   "History variable for tpu-edt-add-at-bol function.")
  141. (defvar tpu-add-at-eol-hist nil
  142.   "History variable for tpu-edt-add-at-eol function.")
  143. (defvar tpu-regexp-prompt-hist  nil
  144.   "History variable for search and replace functions.")
  145.  
  146.  
  147. ;;;
  148. ;;;  Buffer Local Variables
  149. ;;;
  150. (defvar tpu-newline-and-indent-p nil
  151.   "If non-nil, Return produces a newline and indents.")
  152. (make-variable-buffer-local 'tpu-newline-and-indent-p)
  153.  
  154. (defvar tpu-newline-and-indent-string nil
  155.   "Mode line string to identify AutoIndent mode.")
  156. (make-variable-buffer-local 'tpu-newline-and-indent-string)
  157.  
  158. (defvar tpu-saved-delete-func nil
  159.   "Saved value of the delete key.")
  160. (make-variable-buffer-local 'tpu-saved-delete-func)
  161.  
  162. (defvar tpu-buffer-local-map nil
  163.   "TPU-edt buffer local key map.")
  164. (make-variable-buffer-local 'tpu-buffer-local-map)
  165.  
  166.  
  167. ;;;
  168. ;;;  Mode Line - Modify the mode line to show the following
  169. ;;;
  170. ;;;     o  If the mark is set.
  171. ;;;     o  Direction of motion.
  172. ;;;     o  Active rectangle mode.
  173. ;;;
  174. (defvar tpu-original-mode-line mode-line-format)
  175. (defvar tpu-original-mm-alist minor-mode-alist)
  176.  
  177. (defvar tpu-mark-flag " ")
  178. (make-variable-buffer-local 'tpu-mark-flag)
  179.  
  180. (defun tpu-set-mode-line (for-tpu)
  181.   "Set the mode for TPU-edt, or reset it to default Emacs."
  182.   (cond ((not for-tpu)
  183.      (setq mode-line-format tpu-original-mode-line)
  184.      (setq minor-mode-alist tpu-original-mm-alist))
  185.     (t
  186.      (setq-default mode-line-format
  187.                (list (purecopy "")
  188.                  'mode-line-modified
  189.                  'mode-line-buffer-identification
  190.                  (purecopy "  ")
  191.                  'global-mode-string
  192.                  (purecopy "  ")
  193.                  'tpu-mark-flag
  194.                  (purecopy " %[(")
  195.                  'mode-name 'minor-mode-alist "%n" 'mode-line-process
  196.                  (purecopy ")%]----")
  197.                  (purecopy '(-3 . "%p"))
  198.                  (purecopy "-%-")))
  199.      (or (assq 'tpu-newline-and-indent-p minor-mode-alist)
  200.          (setq minor-mode-alist
  201.            (cons '(tpu-newline-and-indent-p
  202.                tpu-newline-and-indent-string)
  203.              minor-mode-alist)))
  204.      (or (assq 'tpu-rectangular-p minor-mode-alist)
  205.          (setq minor-mode-alist
  206.            (cons '(tpu-rectangular-p tpu-rectangle-string)
  207.              minor-mode-alist)))
  208.      (or (assq 'tpu-direction-string minor-mode-alist)
  209.          (setq minor-mode-alist
  210.            (cons '(tpu-direction-string tpu-direction-string)
  211.              minor-mode-alist))))))
  212.  
  213. (defun tpu-update-mode-line nil
  214.   "Make sure mode-line in the current buffer reflects all changes."
  215.   (setq tpu-mark-flag (if (tpu-mark) "M" " "))
  216.   (cond (tpu-emacs19-p (force-mode-line-update))
  217.     (t (set-buffer-modified-p (buffer-modified-p)) (sit-for 0))))
  218.  
  219. (cond (tpu-gnu-emacs19-p
  220.        (add-hook 'activate-mark-hook 'tpu-update-mode-line)
  221.        (add-hook 'deactivate-mark-hook 'tpu-update-mode-line))
  222.       (tpu-lucid-emacs19-p
  223.        (add-hook 'zmacs-deactivate-region-hook 'tpu-update-mode-line)
  224.        (add-hook 'zmacs-activate-region-hook 'tpu-update-mode-line)))
  225.  
  226.  
  227. ;;;
  228. ;;;  Match Markers -
  229. ;;;
  230. ;;;     Set in:  Search
  231. ;;;
  232. ;;;     Used in: Replace, Substitute, Store-Text, Cut/Remove,
  233. ;;;              Append, and Change-Case
  234. ;;;
  235. (defvar tpu-match-beginning-mark (make-marker))
  236. (defvar tpu-match-end-mark (make-marker))
  237.  
  238. (defun tpu-set-match nil
  239.   "Set markers at match beginning and end."
  240.   ;; Add one to beginning mark so it stays with the first character of
  241.   ;;   the string even if characters are added just before the string.
  242.   (setq tpu-match-beginning-mark (copy-marker (1+ (match-beginning 0))))
  243.   (setq tpu-match-end-mark (copy-marker (match-end 0))))
  244.  
  245. (defun tpu-unset-match nil
  246.   "Unset match beginning and end markers."
  247.   (set-marker tpu-match-beginning-mark nil)
  248.   (set-marker tpu-match-end-mark nil))
  249.  
  250. (defun tpu-match-beginning nil
  251.   "Returns the location of the last match beginning."
  252.   (1- (marker-position tpu-match-beginning-mark)))
  253.  
  254. (defun tpu-match-end nil
  255.   "Returns the location of the last match end."
  256.   (marker-position tpu-match-end-mark))
  257.  
  258. (defun tpu-check-match nil
  259.   "Returns t if point is between tpu-match markers.
  260. Otherwise sets the tpu-match markers to nil and returns nil."
  261.   ;; make sure 1- marker is in this buffer
  262.   ;;           2- point is at or after beginning marker
  263.   ;;           3- point is before ending marker, or in the case of
  264.   ;;              zero length regions (like bol, or eol) that the
  265.   ;;              beginning, end, and point are equal.
  266.   (cond ((and
  267.       (equal (marker-buffer tpu-match-beginning-mark) (current-buffer))
  268.       (>= (point) (1- (marker-position tpu-match-beginning-mark)))
  269.       (or
  270.        (< (point) (marker-position tpu-match-end-mark))
  271.        (and (= (1- (marker-position tpu-match-beginning-mark))
  272.            (marker-position tpu-match-end-mark))
  273.         (= (marker-position tpu-match-end-mark) (point))))) t)
  274.     (t
  275.      (tpu-unset-match) nil)))
  276.  
  277. (defun tpu-show-match-markers nil
  278.   "Show the values of the match markers."
  279.   (interactive)
  280.   (if (markerp tpu-match-beginning-mark)
  281.       (let ((beg (marker-position tpu-match-beginning-mark)))
  282.     (message "(%s, %s) in %s -- current %s in %s"
  283.          (if beg (1- beg) nil)
  284.          (marker-position tpu-match-end-mark)
  285.          (marker-buffer tpu-match-end-mark)
  286.          (point) (current-buffer)))))
  287.  
  288.  
  289. ;;;
  290. ;;;  Utilities
  291. ;;;
  292. (defun tpu-caar (thingy) (car (car thingy)))
  293. (defun tpu-cadr (thingy) (car (cdr thingy)))
  294.  
  295. (defun tpu-mark nil
  296.   "TPU-edt version of the mark function.
  297. Return the appropriate value of the mark for the current
  298. version of emacs."
  299.   (cond (tpu-lucid-emacs19-p (mark (not zmacs-regions)))
  300.     (tpu-gnu-emacs19-p (and mark-active (mark (not transient-mark-mode))))
  301.     (t (mark))))
  302.  
  303. (defun tpu-set-mark (pos)
  304.   "TPU-edt verion of the set-mark function.
  305. Sets the mark at POS and activates the region acording to the
  306. current version of emacs."
  307.   (set-mark pos)
  308.   (and tpu-lucid-emacs19-p pos (zmacs-activate-region)))
  309.  
  310. (defun tpu-string-prompt (prompt history-symbol)
  311.   "Read a string with PROMPT."
  312.   (if tpu-emacs19-p
  313.       (read-from-minibuffer prompt nil nil nil history-symbol)
  314.     (read-string prompt)))
  315.  
  316. (defun tpu-y-or-n-p (prompt &optional not-yes)
  317.   "Prompt for a y or n answer with positive default.
  318. Optional second argument NOT-YES changes default to negative.
  319. Like emacs y-or-n-p, also accepts space as y and DEL as n."
  320.   (message (format "%s[%s]" prompt (if not-yes "n" "y")))
  321.   (let ((doit t))
  322.     (while doit
  323.       (setq doit nil)
  324.       (let ((ans (read-char)))
  325.     (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\ ))
  326.            (setq tpu-last-answer t))
  327.           ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
  328.            (setq tpu-last-answer nil))
  329.           ((= ans ?\r) (setq tpu-last-answer (not not-yes)))
  330.           (t
  331.            (setq doit t) (beep)
  332.            (message (format "Please answer y or n.  %s[%s]"
  333.                 prompt (if not-yes "n" "y"))))))))
  334.   tpu-last-answer)
  335.  
  336. (defun tpu-local-set-key (key func)
  337.   "Replace a key in the TPU-edt local key map.
  338. Create the key map if necessary."
  339.   (cond ((not (keymapp tpu-buffer-local-map))
  340.      (setq tpu-buffer-local-map (if (current-local-map)
  341.                     (copy-keymap (current-local-map))
  342.                       (make-sparse-keymap)))
  343.      (use-local-map tpu-buffer-local-map)))
  344.   (local-set-key key func))
  345.  
  346. (defun tpu-current-line nil
  347.   "Return the vertical position of point in the selected window.
  348. Top line is 0.  Counts each text line only once, even if it wraps."
  349.   (+ (count-lines (window-start) (point)) (if (= (current-column) 0) 1 0) -1))
  350.  
  351.  
  352. ;;;
  353. ;;;  Breadcrumbs
  354. ;;;
  355. (defvar tpu-breadcrumb-plist nil
  356.   "The set of user-defined markers (breadcrumbs), as a plist.")
  357.  
  358. (defun tpu-drop-breadcrumb (num)
  359.   "Drops a breadcrumb that can be returned to later with goto-breadcrumb."
  360.   (interactive "p")
  361.   (put tpu-breadcrumb-plist num (list (current-buffer) (point)))
  362.   (message "Mark %d set." num))
  363.  
  364. (defun tpu-goto-breadcrumb (num)
  365.   "Returns to a breadcrumb set with drop-breadcrumb."
  366.   (interactive "p")
  367.   (cond ((get tpu-breadcrumb-plist num)
  368.      (switch-to-buffer (car (get tpu-breadcrumb-plist num)))
  369.      (goto-char (tpu-cadr (get tpu-breadcrumb-plist num)))
  370.      (message "mark %d found." num))
  371.     (t
  372.      (message "mark %d not found." num))))
  373.  
  374.  
  375. ;;;
  376. ;;;  Miscellaneous
  377. ;;;
  378. (defun tpu-change-case (num)
  379.   "Change the case of the character under the cursor or region.
  380. Accepts a prefix argument of the number of characters to invert."
  381.   (interactive "p")
  382.   (cond ((tpu-mark)
  383.      (let ((beg (region-beginning)) (end (region-end)))
  384.        (while (> end beg)
  385.          (funcall (if (<= ?a (char-after beg))
  386.               'upcase-region 'downcase-region)
  387.               beg (1+ beg))
  388.          (setq beg (1+ beg)))
  389.        (tpu-unselect t)))
  390.     ((tpu-check-match)
  391.      (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  392.        (while (> end beg)
  393.          (funcall (if (<= ?a (char-after beg))
  394.               'upcase-region 'downcase-region)
  395.               beg (1+ beg))
  396.          (setq beg (1+ beg)))
  397.        (tpu-unset-match)))
  398.     (t
  399.      (while (> num 0)
  400.        (funcall (if (<= ?a (following-char))
  401.             'upcase-region 'downcase-region)
  402.             (point) (1+ (point)))
  403.        (forward-char (if tpu-reverse -1 1))
  404.        (setq num (1- num))))))
  405.  
  406. (defun tpu-fill (num)
  407.   "Fill paragraph or marked region.
  408. With argument, fill and justify."
  409.   (interactive "P")
  410.   (cond ((tpu-mark)
  411.      (fill-region (point) (tpu-mark) num)
  412.      (tpu-unselect t))
  413.     (t
  414.      (fill-paragraph num))))
  415.  
  416. (defun tpu-version nil
  417.   "Print the TPU-edt version number."
  418.   (interactive)
  419.   (message (concat "TPU-edt revision "
  420.            (substring tpu-revision 11 -2)
  421.            " by Rob Riepel (riepel@networking.stanford.edu)  "
  422.            (substring tpu-revision-date 12 -11) "/"
  423.            (substring tpu-revision-date 9 11))))
  424.  
  425. (defun tpu-reset-screen-size (height width)
  426.   "Sets the screen size."
  427.   (interactive "nnew screen height: \nnnew screen width: ")
  428.   (set-screen-height height)
  429.   (set-screen-width width))
  430.  
  431. (defun tpu-toggle-newline-and-indent nil
  432.   "Toggle between 'newline and indent' and 'simple newline'."
  433.   (interactive)
  434.   (cond (tpu-newline-and-indent-p
  435.          (setq tpu-newline-and-indent-string "")
  436.          (setq tpu-newline-and-indent-p nil)
  437.          (tpu-local-set-key "\C-m" 'newline))
  438.         (t
  439.          (setq tpu-newline-and-indent-string " AutoIndent")
  440.          (setq tpu-newline-and-indent-p t)
  441.          (tpu-local-set-key "\C-m" 'newline-and-indent)))
  442.   (tpu-update-mode-line)
  443.   (and (interactive-p)
  444.        (message "Carriage return inserts a newline%s"
  445.         (if tpu-newline-and-indent-p " and indents." "."))))
  446.  
  447. (defun tpu-spell-check nil
  448.   "Checks the spelling of the region, or of the entire buffer if no
  449.  region is selected."
  450.   (interactive)
  451.   (cond (tpu-have-ispell
  452.      (if (tpu-mark) (ispell-region (tpu-mark) (point)) (ispell-buffer)))
  453.     (t
  454.      (if (tpu-mark) (spell-region (tpu-mark) (point)) (spell-buffer))))
  455.   (if (tpu-mark) (tpu-unselect t)))
  456.  
  457. (defun tpu-toggle-overwrite-mode nil
  458.   "Switches in and out of overwrite mode"
  459.   (interactive)
  460.   (cond (overwrite-mode
  461.      (tpu-local-set-key "\177" tpu-saved-delete-func)
  462.      (overwrite-mode 0))
  463.     (t
  464.      (setq tpu-saved-delete-func (local-key-binding "\177"))
  465.      (tpu-local-set-key "\177" 'picture-backward-clear-column)
  466.      (overwrite-mode 1))))
  467.  
  468. (defun tpu-special-insert (num)
  469.   "Insert a character or control code according to
  470. its ASCII decimal value."
  471.   (interactive "P")
  472.   (insert (if num num 0)))
  473.  
  474.  
  475. ;;;
  476. ;;;  TPU line-mode commands
  477. ;;;
  478. (defun tpu-include (file)
  479.   "TPU-like include file"
  480.   (interactive "fInclude file: ")
  481.   (save-excursion
  482.     (insert-file file)
  483.     (message "")))
  484.  
  485. (defun tpu-get (file)
  486.   "TPU-like get file"
  487.   (interactive "FFile to get: ")
  488.   (find-file file))
  489.  
  490. (defun tpu-what-line nil
  491.   "Tells what line the point is on,
  492.  and the total number of lines in the buffer."
  493.   (interactive)
  494.   (if (eobp)
  495.       (message "You are at the End of Buffer.  The last line is %d."
  496.            (count-lines 1 (point-max)))
  497.     (message "Line %d of %d"
  498.          (count-lines 1 (1+ (point)))
  499.          (count-lines 1 (point-max)))))
  500.  
  501. (defun tpu-exit nil
  502.   "Exit the way TPU does, save current buffer and ask about others."
  503.   (interactive)
  504.   (if (not (eq (recursion-depth) 0))
  505.       (exit-recursive-edit)
  506.     (progn (save-buffer) (save-buffers-kill-emacs))))
  507.  
  508. (defun tpu-quit nil
  509.   "Quit the way TPU does, ask to make sure changes should be abandoned."
  510.   (interactive)
  511.   (let ((list (buffer-list))
  512.     (working t))
  513.     (while (and list working)
  514.       (let ((buffer (car list)))
  515.     (if (and (buffer-file-name buffer) (buffer-modified-p buffer))
  516.             (if (tpu-y-or-n-p
  517.          "Modifications will not be saved, continue quitting? ")
  518.         (kill-emacs t) (setq working nil)))
  519.     (setq list (cdr list))))
  520.     (if working (kill-emacs t))))
  521.  
  522.  
  523. ;;;
  524. ;;;  Command and Function Aliases
  525. ;;;
  526. (fset 'tpu-edt-mode 'tpu-edt-on)
  527. (fset 'TPU-EDT-MODE 'tpu-edt-on)
  528.  
  529. (fset 'exit 'tpu-exit)
  530. (fset 'EXIT 'tpu-exit)
  531.  
  532. (fset 'Get 'tpu-get)
  533. (fset 'GET 'tpu-get)
  534.  
  535. (fset 'include 'tpu-include)
  536. (fset 'INCLUDE 'tpu-include)
  537.  
  538. (fset 'quit 'tpu-quit)
  539. (fset 'QUIT 'tpu-quit)
  540.  
  541. (fset 'spell 'tpu-spell-check)
  542. (fset 'SPELL 'tpu-spell-check)
  543.  
  544. (fset 'what\ line 'tpu-what-line)
  545. (fset 'WHAT\ LINE 'tpu-what-line)
  546.  
  547. (fset 'replace 'tpu-lm-replace)
  548. (fset 'REPLACE 'tpu-lm-replace)
  549.  
  550. (fset 'help 'tpu-help)
  551. (fset 'HELP 'tpu-help)
  552.  
  553. ;; Around emacs version 18.57, function line-move was renamed to
  554. ;; next-line-internal.  If we're running under an older emacs,
  555. ;; make next-line-internal equivalent to line-move.
  556.  
  557. (if (not (fboundp 'next-line-internal)) (fset 'next-line-internal 'line-move))
  558.  
  559.  
  560. ;;;
  561. ;;;  Help
  562. ;;;
  563. (defconst tpu-help-keypad-map "\f
  564.           _______________________    _______________________________
  565.          | HELP  |      Do       |  |       |       |       |       |
  566.          |KeyDefs|               |  |       |       |       |       |
  567.          |_______|_______________|  |_______|_______|_______|_______|
  568.           _______________________    _______________________________
  569.          | Find  |Insert |Remove |  | Gold  | HELP  |FndNxt | Del L |
  570.          |       |       |Sto Tex|  |  key  |E-Help | Find  |Undel L|
  571.          |_______|_______|_______|  |_______|_______|_______|_______|
  572.          |Select |Pre Scr|Nex Scr|  | Page  | Sect  |Append | Del W |
  573.          | Reset |Pre Win|Nex Win|  |  Do   | Fill  |Replace|Undel W|
  574.          |_______|_______|_______|  |_______|_______|_______|_______|
  575.                  |Move up|          |Forward|Reverse|Remove | Del C |
  576.                  |  Top  |          |Bottom |  Top  |Insert |Undel C|
  577.           _______|_______|_______   |_______|_______|_______|_______|
  578.          |Mov Lef|Mov Dow|Mov Rig|  | Word  |  EOL  | Char  |       |
  579.          |StaOfLi|Bottom |EndOfLi|  |ChngCas|Del EOL|SpecIns| Enter |
  580.          |_______|_______|_______|  |_______|_______|_______|       |
  581.                                     |     Line      |Select | Subs  |
  582.                                     |   Open Line   | Reset |       |
  583.                                     |_______________|_______|_______|
  584. ")
  585.  
  586. (defconst tpu-help-text "
  587. \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
  588.  
  589.       Control Characters
  590.  
  591.       ^A  toggle insert and overwrite
  592.       ^B  recall
  593.       ^E  end of line
  594.  
  595.       ^G  Cancel current operation
  596.       ^H  beginning of line
  597.       ^J  delete previous word
  598.  
  599.       ^K  learn
  600.       ^L  insert page break
  601.       ^R  remember (during learn), re-center
  602.  
  603.       ^U  delete to beginning of line
  604.       ^V  quote
  605.       ^W  refresh
  606.  
  607.       ^Z  exit
  608.     ^X^X  exchange point and mark - useful for checking region boundaries
  609.  
  610. \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
  611.        Gold-<key> Functions
  612.  
  613.        B     Next Buffer - display the next buffer (all buffers)
  614.        C     Recall - edit and possibly repeat previous commands
  615.        E     Exit - save current buffer and ask about others
  616.  
  617.        G     Get - load a file into a new edit buffer
  618.        I     Include - include a file in this buffer
  619.        K     Kill Buffer - abandon edits and delete buffer
  620.  
  621.        M     Buffer Menu - display a list of all buffers
  622.        N     Next File Buffer - display next buffer containing a file
  623.        O     Occur - show following lines containing REGEXP
  624.  
  625.        Q     Quit - exit without saving anything
  626.        R     Toggle rectangular mode for remove and insert
  627.        S     Search and substitute - line mode REPLACE command
  628.  
  629.        U     Undo - undo the last edit
  630.        W     Write - save current buffer
  631.        X     Exit - save all modified buffers and exit
  632.  
  633. \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
  634.  
  635.    *** No more help, use P to view previous screen")
  636.  
  637. (defvar tpu-help-enter (format "%s" "\eOM"))    ; tpu-help enter key symbol
  638. (defvar tpu-help-return (format "%s" "\r"))     ; tpu-help enter key symbol
  639.  
  640. (defun tpu-help nil
  641.   "Display TPU-edt help."
  642.   (interactive)
  643.   ;; Save current window configuration
  644.   (save-window-excursion
  645.     ;; Create and fill help buffer if necessary
  646.     (if (not (get-buffer "*TPU-edt Help*"))
  647.     (progn (generate-new-buffer "*TPU-edt Help*")
  648.            (switch-to-buffer "*TPU-edt Help*")
  649.            (insert tpu-help-keypad-map)
  650.            (insert tpu-help-text)
  651.            (setq buffer-read-only t)))
  652.  
  653.     ;; Display the help buffer
  654.     (switch-to-buffer "*TPU-edt Help*")
  655.     (delete-other-windows)
  656.     (tpu-move-to-beginning)
  657.     (forward-line 1)
  658.     (tpu-line-to-top-of-window)
  659.  
  660.     ;; Prompt for keys to describe, based on screen state (split/not split)
  661.     (let ((key nil) (split nil))
  662.       (while (not (equal tpu-help-return (format "%s" key)))
  663.     (if split
  664.         (setq key
  665.           (read-key-sequence
  666.            "Press the key you want help on (RET=exit, ENTER=redisplay, N=next, P=prev): "))
  667.       (setq key
  668.         (read-key-sequence
  669.          "Press the key you want help on (RET to exit, N next screen, P prev screen): ")))
  670.  
  671.     ;; Process the read key
  672.     ;;
  673.     ;;    ENTER   -  Display just the help window
  674.     ;;    N or n  -  Next help or describe-key screen
  675.     ;;    P or p  -  Previous help or describe-key screen
  676.     ;;    RETURN  -  Exit from TPU-help
  677.     ;;    default -  describe the key
  678.     ;;
  679.     (cond ((equal tpu-help-enter (format "%s" key))
  680.            (setq split nil)
  681.            (delete-other-windows))
  682.           ((or (equal "N" key) (equal "n" key))
  683.            (cond (split
  684.                   (condition-case nil
  685.                   (scroll-other-window 8)
  686.                 (error nil)))
  687.                  (t
  688.                   (forward-page)
  689.                   (forward-line 1)
  690.                   (tpu-line-to-top-of-window))))
  691.           ((or (equal "P" key) (equal "p" key))
  692.            (cond (split
  693.                   (condition-case nil
  694.                   (scroll-other-window -8)
  695.                 (error nil)))
  696.                  (t
  697.                   (backward-page 2)
  698.                   (forward-line 1)
  699.                   (tpu-line-to-top-of-window))))
  700.           ((not (equal tpu-help-return (format "%s" key)))
  701.            (setq split t)
  702.            (describe-key key)
  703.            ;; If the key is undefined, leave the
  704.            ;;   message in the mini-buffer for 3 seconds
  705.            (if (not (key-binding key)) (sit-for 3))))))))
  706.  
  707.  
  708. ;;;
  709. ;;;  Auto-insert
  710. ;;;
  711. (defun tpu-insert-escape nil
  712.   "Inserts an escape character, and so becomes the escape-key alias."
  713.   (interactive)
  714.   (insert "\e"))
  715.  
  716. (defun tpu-insert-formfeed nil
  717.   "Inserts a formfeed character."
  718.   (interactive)
  719.   (insert "\C-L"))
  720.  
  721.  
  722. ;;;
  723. ;;;  Define key
  724. ;;;
  725. (defun tpu-end-define-macro-key (key)
  726.   "Ends the current macro definition"
  727.   (interactive "kPress the key you want to use to do what was just learned: ")
  728.   (end-kbd-macro nil)
  729.   (global-set-key key last-kbd-macro)
  730.   (global-set-key "\C-r" tpu-saved-control-r))
  731.  
  732. (defun tpu-define-macro-key nil
  733.   "Bind a set of keystrokes to a single key, or key combination."
  734.   (interactive)
  735.   (setq tpu-saved-control-r (global-key-binding "\C-r"))
  736.   (global-set-key "\C-r" 'tpu-end-define-macro-key)
  737.   (start-kbd-macro nil))
  738.  
  739.  
  740. ;;;
  741. ;;;  Buffers and Windows
  742. ;;;
  743. (defun tpu-kill-buffer nil
  744.   "Kills the current buffer.  If tpu-kill-buffers-silently is non-nil,
  745. kills modified buffers without asking."
  746.   (interactive)
  747.   (if tpu-kill-buffers-silently (set-buffer-modified-p nil))
  748.   (kill-buffer (current-buffer)))
  749.  
  750. (defun tpu-save-all-buffers-kill-emacs nil
  751.   "Save all buffers and exit emacs."
  752.   (interactive)
  753.   (setq trim-versions-without-asking t)
  754.   (save-buffers-kill-emacs t))
  755.  
  756. (defun tpu-write-current-buffers nil
  757.   "Save all modified buffers without exiting."
  758.   (interactive)
  759.   (save-some-buffers t))
  760.  
  761. (defun tpu-next-buffer nil
  762.   "Go to next buffer in ring."
  763.   (interactive)
  764.   (switch-to-buffer (car (reverse (buffer-list)))))
  765.  
  766. (defun tpu-next-file-buffer nil
  767.   "Go to next buffer in ring that is visiting a file."
  768.   (interactive)
  769.   (setq starting-buffer (buffer-name))
  770.   (switch-to-buffer (car (reverse (buffer-list))))
  771.   (while (and (not (equal (buffer-name) starting-buffer))
  772.               (not (buffer-file-name)))
  773.     (switch-to-buffer (car (reverse (buffer-list)))))
  774.   (if (equal (buffer-name) starting-buffer) (error "No other buffers.")))
  775.  
  776. (defun tpu-next-window nil
  777.   "Move to the next window."
  778.   (interactive)
  779.   (if (one-window-p) (message "There is only one window on screen.")
  780.     (other-window 1)))
  781.  
  782. (defun tpu-previous-window nil
  783.   "Move to the previous window."
  784.   (interactive)
  785.   (if (one-window-p) (message "There is only one window on screen.")
  786.     (select-window (previous-window))))
  787.  
  788.  
  789. ;;;
  790. ;;;  Search
  791. ;;;
  792. (defun tpu-toggle-regexp nil
  793.   "Switches in and out of regular expression search and replace mode."
  794.   (interactive)
  795.   (setq tpu-regexp-p (not tpu-regexp-p))
  796.   (tpu-set-search)
  797.   (and (interactive-p)
  798.        (message "Regular expression search and substitute %sabled."
  799.         (if tpu-regexp-p "en" "dis"))))
  800.  
  801. (defun tpu-regexp-prompt (prompt)
  802.   "Read a string, adding 'RE' to the prompt if tpu-regexp-p is set."
  803.   (let ((re-prompt (concat (if tpu-regexp-p "RE ") prompt)))
  804.     (if tpu-emacs19-p
  805.     (read-from-minibuffer re-prompt nil nil nil 'tpu-regexp-prompt-hist)
  806.       (read-string re-prompt))))
  807.  
  808. (defun tpu-search nil
  809.   "Search for a string or regular expression.
  810. The search is performed in the current direction."
  811.   (interactive)
  812.   (tpu-set-search)
  813.   (tpu-search-internal ""))
  814.  
  815. (defun tpu-search-forward nil
  816.   "Search for a string or regular expression.
  817. The search is begins in the forward direction."
  818.   (interactive)
  819.   (setq searching-forward t)
  820.   (tpu-set-search t)
  821.   (tpu-search-internal ""))
  822.  
  823. (defun tpu-search-reverse nil
  824.   "Search for a string or regular expression.
  825. The search is begins in the reverse direction."
  826.   (interactive)
  827.   (setq searching-forward nil)
  828.   (tpu-set-search t)
  829.   (tpu-search-internal ""))
  830.  
  831. (defun tpu-search-again nil
  832.   "Search for the same string or regular expression as last time.
  833. The search is performed in the current direction."
  834.   (interactive)
  835.   (tpu-search-internal tpu-search-last-string))
  836.  
  837. ;;  tpu-set-search defines the search functions used by the TPU-edt internal
  838. ;;  search function.  It should be called whenever the direction changes, or
  839. ;;  the regular expression mode is turned on or off.  It can also be called
  840. ;;  to ensure that the next search will be in the current direction.  It is
  841. ;;  called from:
  842.  
  843. ;;       tpu-advance              tpu-backup
  844. ;;       tpu-toggle-regexp        tpu-toggle-search-direction (t)
  845. ;;       tpu-search               tpu-lm-replace
  846. ;;       tpu-search-forward (t)   tpu-search-reverse (t)
  847.  
  848. (defun tpu-set-search (&optional arg)
  849.   "Set the search functions and set the search direction to the current
  850. direction.  If an argument is specified, don't set the search direction."
  851.   (if (not arg) (setq searching-forward (if tpu-advance t nil)))
  852.   (cond (searching-forward
  853.      (cond (tpu-regexp-p
  854.         (fset 'tpu-emacs-search 're-search-forward)
  855.         (fset 'tpu-emacs-rev-search 're-search-backward))
  856.            (t
  857.         (fset 'tpu-emacs-search 'search-forward)
  858.         (fset 'tpu-emacs-rev-search 'search-backward))))
  859.     (t
  860.      (cond (tpu-regexp-p
  861.         (fset 'tpu-emacs-search 're-search-backward)
  862.         (fset 'tpu-emacs-rev-search 're-search-forward))
  863.            (t
  864.         (fset 'tpu-emacs-search 'search-backward)
  865.         (fset 'tpu-emacs-rev-search 'search-forward))))))
  866.  
  867. (defun tpu-search-internal (pat &optional quiet)
  868.   "Search for a string or regular expression."
  869.   (setq tpu-search-last-string
  870.     (if (not (string= "" pat)) pat (tpu-regexp-prompt "Search: ")))
  871.  
  872.   (tpu-unset-match)
  873.   (tpu-adjust-search)
  874.  
  875.   (cond ((tpu-emacs-search tpu-search-last-string nil t)
  876.      (tpu-set-match) (goto-char (tpu-match-beginning)))
  877.  
  878.     (t
  879.      (tpu-adjust-search t)
  880.      (let ((found nil) (pos nil))
  881.        (save-excursion
  882.          (let ((searching-forward (not searching-forward)))
  883.            (tpu-adjust-search)
  884.            (setq found (tpu-emacs-rev-search tpu-search-last-string nil t))
  885.            (setq pos (match-beginning 0))))
  886.  
  887.        (cond (found
  888.           (cond ((tpu-y-or-n-p
  889.               (format "Found in %s direction.  Go there? "
  890.                   (if searching-forward "reverse" "forward")))
  891.              (goto-char pos) (tpu-set-match)
  892.              (tpu-toggle-search-direction))))
  893.  
  894.          (t
  895.           (if (not quiet)
  896.               (message
  897.                "%sSearch failed: \"%s\""
  898.                (if tpu-regexp-p "RE " "") tpu-search-last-string))))))))
  899.  
  900. (fset 'tpu-search-internal-core (symbol-function 'tpu-search-internal))
  901.  
  902. (defun tpu-adjust-search (&optional arg)
  903.   "For forward searches, move forward a character before searching,
  904. and backward a character after a failed search.  Arg means end of search."
  905.   (if searching-forward
  906.       (cond (arg (if (not (bobp)) (forward-char -1)))
  907.         (t (if (not (eobp)) (forward-char 1))))))
  908.  
  909. (defun tpu-toggle-search-direction nil
  910.   "Toggle the TPU-edt search direction.
  911. Used for reversing a search in progress."
  912.   (interactive)
  913.   (setq searching-forward (not searching-forward))
  914.   (tpu-set-search t)
  915.   (and (interactive-p)
  916.        (message "Searching %sward."
  917.         (if searching-forward "for" "back"))))
  918.  
  919.  
  920. ;;;
  921. ;;;  Select / Unselect
  922. ;;;
  923. (defun tpu-select (&optional quiet)
  924.   "Sets the mark to define one end of a region."
  925.   (interactive "P")
  926.   (cond ((tpu-mark)
  927.      (tpu-unselect quiet))
  928.     (t
  929.      (tpu-set-mark (point))
  930.      (tpu-update-mode-line)
  931.      (if (not quiet) (message "Move the text cursor to select text.")))))
  932.  
  933. (defun tpu-unselect (&optional quiet)
  934.   "Removes the mark to unselect the current region."
  935.   (interactive "P")
  936.   (setq mark-ring nil)
  937.   (tpu-set-mark nil)
  938.   (tpu-update-mode-line)
  939.   (if (not quiet) (message "Selection canceled.")))
  940.  
  941.  
  942. ;;;
  943. ;;;  Delete / Cut
  944. ;;;
  945. (defun tpu-toggle-rectangle nil
  946.   "Toggle rectangular mode for remove and insert."
  947.   (interactive)
  948.   (setq tpu-rectangular-p (not tpu-rectangular-p))
  949.   (setq tpu-rectangle-string (if tpu-rectangular-p " Rect" ""))
  950.   (tpu-update-mode-line)
  951.   (and (interactive-p)
  952.        (message "Rectangular cut and paste %sabled."
  953.         (if tpu-rectangular-p "en" "dis"))))
  954.  
  955. (defun tpu-arrange-rectangle nil
  956.   "Adjust point and mark to mark upper left and lower right
  957. corners of a rectangle."
  958.   (let ((mc (current-column))
  959.     (pc (progn (exchange-point-and-mark) (current-column))))
  960.  
  961.     (cond ((> (point) (tpu-mark))                      ; point on lower line
  962.        (cond ((> pc mc)                        ; point @  lower-right
  963.           (exchange-point-and-mark))       ; point -> upper-left
  964.  
  965.          (t                               ; point @  lower-left
  966.           (move-to-column-force mc)        ; point -> lower-right
  967.           (exchange-point-and-mark)        ; point -> upper-right
  968.           (move-to-column-force pc))))     ; point -> upper-left
  969.  
  970.       (t                                       ; point on upper line
  971.        (cond ((> pc mc)                        ; point @  upper-right
  972.           (move-to-column-force mc)        ; point -> upper-left
  973.           (exchange-point-and-mark)        ; point -> lower-left
  974.           (move-to-column-force pc)        ; point -> lower-right
  975.           (exchange-point-and-mark)))))))  ; point -> upper-left
  976.  
  977. (defun tpu-cut-text nil
  978.   "Delete the selected region.
  979. The text is saved for the tpu-paste command."
  980.   (interactive)
  981.   (cond ((tpu-mark)
  982.      (cond (tpu-rectangular-p
  983.         (tpu-arrange-rectangle)
  984.         (picture-clear-rectangle (point) (tpu-mark) (not overwrite-mode))
  985.         (tpu-unselect t))
  986.            (t
  987.         (setq tpu-last-deleted-region
  988.               (buffer-substring (tpu-mark) (point)))
  989.         (delete-region (tpu-mark) (point))
  990.         (tpu-unselect t))))
  991.     ((tpu-check-match)
  992.      (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  993.        (setq tpu-last-deleted-region (buffer-substring beg end))
  994.        (delete-region beg end)
  995.        (tpu-unset-match)))
  996.     (t
  997.      (error "No selection active."))))
  998.  
  999. (defun tpu-store-text nil
  1000.   "Copy the selected region to the cut buffer without deleting it.
  1001. The text is saved for the tpu-paste command."
  1002.   (interactive)
  1003.   (cond ((tpu-mark)
  1004.      (cond (tpu-rectangular-p
  1005.         (save-excursion
  1006.           (tpu-arrange-rectangle)
  1007.           (setq picture-killed-rectangle
  1008.             (extract-rectangle (point) (tpu-mark))))
  1009.         (tpu-unselect t))
  1010.            (t
  1011.         (setq tpu-last-deleted-region
  1012.               (buffer-substring (tpu-mark) (point)))
  1013.         (tpu-unselect t))))
  1014.     ((tpu-check-match)
  1015.      (setq tpu-last-deleted-region
  1016.            (buffer-substring (tpu-match-beginning) (tpu-match-end)))
  1017.      (tpu-unset-match))
  1018.     (t
  1019.      (error "No selection active."))))
  1020.  
  1021. (defun tpu-cut (arg)
  1022.   "Copy selected region to the cut buffer.  In the absence of an
  1023. argument, delete the selected region too."
  1024.   (interactive "P")
  1025.   (if arg (tpu-store-text) (tpu-cut-text)))
  1026.  
  1027. (defun tpu-append-region (arg)
  1028.   "Append selected region to the tpu-cut buffer.  In the absence of an
  1029. argument, delete the selected region too."
  1030.   (interactive "P")
  1031.   (cond ((tpu-mark)
  1032.      (let ((beg (region-beginning)) (end (region-end)))
  1033.        (setq tpu-last-deleted-region
  1034.          (concat tpu-last-deleted-region
  1035.              (buffer-substring beg end)))
  1036.        (if (not arg) (delete-region beg end))
  1037.        (tpu-unselect t)))
  1038.     ((tpu-check-match)
  1039.      (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  1040.        (setq tpu-last-deleted-region
  1041.          (concat tpu-last-deleted-region
  1042.              (buffer-substring beg end)))
  1043.        (if (not arg) (delete-region beg end))
  1044.        (tpu-unset-match)))
  1045.     (t
  1046.      (error "No selection active."))))
  1047.  
  1048. (defun tpu-delete-current-line (num)
  1049.   "Delete one or specified number of lines after point.
  1050. This includes the newline character at the end of each line.
  1051. They are saved for the TPU-edt undelete-lines command."
  1052.   (interactive "p")
  1053.   (let ((beg (point)))
  1054.     (forward-line num)
  1055.     (if (not (eq (preceding-char) ?\n))
  1056.         (insert "\n"))
  1057.     (setq tpu-last-deleted-lines
  1058.           (buffer-substring beg (point)))
  1059.     (delete-region beg (point))))
  1060.  
  1061. (defun tpu-delete-to-eol (num)
  1062.   "Delete text up to end of line.
  1063. With argument, delete up to to Nth line-end past point.
  1064. They are saved for the TPU-edt undelete-lines command."
  1065.   (interactive "p")
  1066.   (let ((beg (point)))
  1067.     (forward-char 1)
  1068.     (end-of-line num)
  1069.     (setq tpu-last-deleted-lines
  1070.           (buffer-substring beg (point)))
  1071.     (delete-region beg (point))))
  1072.  
  1073. (defun tpu-delete-to-bol (num)
  1074.   "Delete text back to beginning of line.
  1075. With argument, delete up to to Nth line-end past point.
  1076. They are saved for the TPU-edt undelete-lines command."
  1077.   (interactive "p")
  1078.   (let ((beg (point)))
  1079.     (tpu-next-beginning-of-line num)
  1080.     (setq tpu-last-deleted-lines
  1081.           (buffer-substring (point) beg))
  1082.     (delete-region (point) beg)))
  1083.  
  1084. (defun tpu-delete-current-word (num)
  1085.   "Delete one or specified number of words after point.
  1086. They are saved for the TPU-edt undelete-words command."
  1087.   (interactive "p")
  1088.   (let ((beg (point)))
  1089.     (tpu-forward-to-word num)
  1090.     (setq tpu-last-deleted-words
  1091.           (buffer-substring beg (point)))
  1092.     (delete-region beg (point))))
  1093.  
  1094. (defun tpu-delete-previous-word (num)
  1095.   "Delete one or specified number of words before point.
  1096. They are saved for the TPU-edt undelete-words command."
  1097.   (interactive "p")
  1098.   (let ((beg (point)))
  1099.     (tpu-backward-to-word num)
  1100.     (setq tpu-last-deleted-words
  1101.           (buffer-substring (point) beg))
  1102.     (delete-region beg (point))))
  1103.  
  1104. (defun tpu-delete-current-char (num)
  1105.   "Delete one or specified number of characters after point.  The last
  1106. character deleted is saved for the TPU-edt undelete-char command."
  1107.   (interactive "p")
  1108.   (while (and (> num 0) (not (eobp)))
  1109.     (setq tpu-last-deleted-char (char-after (point)))
  1110.     (cond (overwrite-mode
  1111.        (picture-clear-column 1)
  1112.        (forward-char 1))
  1113.       (t
  1114.        (delete-char 1)))
  1115.     (setq num (1- num))))
  1116.  
  1117.  
  1118. ;;;
  1119. ;;;  Undelete / Paste
  1120. ;;;
  1121. (defun tpu-paste (num)
  1122.   "Insert the last region or rectangle of killed text.
  1123. With argument reinserts the text that many times."
  1124.   (interactive "p")
  1125.   (while (> num 0)
  1126.     (cond (tpu-rectangular-p
  1127.        (let ((beg (point)))
  1128.          (save-excursion
  1129.            (picture-yank-rectangle (not overwrite-mode))
  1130.            (message ""))
  1131.          (goto-char beg)))
  1132.       (t
  1133.        (insert tpu-last-deleted-region)))
  1134.     (setq num (1- num))))
  1135.  
  1136. (defun tpu-undelete-lines (num)
  1137.   "Insert lines deleted by last TPU-edt line-deletion command.
  1138. With argument reinserts lines that many times."
  1139.   (interactive "p")
  1140.   (let ((beg (point)))
  1141.     (while (> num 0)
  1142.       (insert tpu-last-deleted-lines)
  1143.       (setq num (1- num)))
  1144.     (goto-char beg)))
  1145.  
  1146. (defun tpu-undelete-words (num)
  1147.   "Insert words deleted by last TPU-edt word-deletion command.
  1148. With argument reinserts words that many times."
  1149.   (interactive "p")
  1150.   (let ((beg (point)))
  1151.     (while (> num 0)
  1152.       (insert tpu-last-deleted-words)
  1153.       (setq num (1- num)))
  1154.     (goto-char beg)))
  1155.  
  1156. (defun tpu-undelete-char (num)
  1157.   "Insert character deleted by last TPU-edt character-deletion command.
  1158. With argument reinserts the character that many times."
  1159.   (interactive "p")
  1160.   (while (> num 0)
  1161.     (if overwrite-mode (prog1 (forward-char -1) (delete-char 1)))
  1162.     (insert tpu-last-deleted-char)
  1163.     (forward-char -1)
  1164.     (setq num (1- num))))
  1165.  
  1166.  
  1167. ;;;
  1168. ;;;  Replace and Substitute
  1169. ;;;
  1170. (defun tpu-replace nil
  1171.   "Replace the selected region with the contents of the cut buffer."
  1172.   (interactive)
  1173.   (cond ((tpu-mark)
  1174.      (let ((beg (region-beginning)) (end (region-end)))
  1175.        (setq tpu-last-replaced-text (buffer-substring beg end))
  1176.        (delete-region beg end)
  1177.        (insert tpu-last-deleted-region)
  1178.        (tpu-unselect t)))
  1179.     ((tpu-check-match)
  1180.      (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  1181.        (setq tpu-last-replaced-text (buffer-substring beg end))
  1182.        (replace-match tpu-last-deleted-region
  1183.               (not case-replace) (not tpu-regexp-p))
  1184.        (tpu-unset-match)))
  1185.     (t
  1186.      (error "No selection active."))))
  1187.  
  1188. (defun tpu-substitute (num)
  1189.   "Replace the selected region with the contents of the cut buffer, and
  1190. repeat most recent search.  A numeric argument serves as a repeat count.
  1191. A negative argument means replace all occurrences of the search string."
  1192.   (interactive "p")
  1193.   (cond ((or (tpu-mark) (tpu-check-match))
  1194.      (while (and (not (= num 0)) (or (tpu-mark) (tpu-check-match)))
  1195.        (let ((beg (point)))
  1196.          (tpu-replace)
  1197.          (if searching-forward (forward-char -1) (goto-char beg))
  1198.          (if (= num 1) (tpu-search-internal tpu-search-last-string)
  1199.            (tpu-search-internal-core tpu-search-last-string)))
  1200.        (setq num (1- num))))
  1201.     (t
  1202.      (error "No selection active."))))
  1203.  
  1204. (defun tpu-lm-replace (from to)
  1205.   "Interactively search for OLD-string and substitute NEW-string."
  1206.   (interactive (list (tpu-regexp-prompt "Old String: ")
  1207.              (tpu-regexp-prompt "New String: ")))
  1208.  
  1209.   (let ((doit t) (strings 0))
  1210.  
  1211.     ;; Can't replace null strings
  1212.     (if (string= "" from) (error "No string to replace."))
  1213.  
  1214.     ;; Find the first occurrence
  1215.     (tpu-set-search)
  1216.     (tpu-search-internal from t)
  1217.  
  1218.     ;; Loop on replace question - yes, no, all, last, or quit.
  1219.     (while doit
  1220.       (if (not (tpu-check-match)) (setq doit nil)
  1221.     (progn (message "Replace? Type Yes, No, All, Last, or Quit: ")
  1222.            (let ((ans (read-char)))
  1223.  
  1224.          (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\r) (= ans ?\ ))
  1225.             (let ((beg (point)))
  1226.               (replace-match to (not case-replace) (not tpu-regexp-p))
  1227.               (setq strings (1+ strings))
  1228.               (if searching-forward (forward-char -1) (goto-char beg)))
  1229.             (tpu-search-internal from t))
  1230.  
  1231.                ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
  1232.             (tpu-search-internal from t))
  1233.  
  1234.                ((or (= ans ?a) (= ans ?A))
  1235.             (save-excursion
  1236.               (let ((beg (point)))
  1237.                 (replace-match to (not case-replace) (not tpu-regexp-p))
  1238.                 (setq strings (1+ strings))
  1239.                 (if searching-forward (forward-char -1) (goto-char beg)))
  1240.               (tpu-search-internal-core from t)
  1241.               (while (tpu-check-match)
  1242.                 (let ((beg (point)))
  1243.                   (replace-match to (not case-replace) (not tpu-regexp-p))
  1244.                   (setq strings (1+ strings))
  1245.                   (if searching-forward (forward-char -1) (goto-char beg)))
  1246.                 (tpu-search-internal-core from t)))
  1247.             (setq doit nil))
  1248.  
  1249.                ((or (= ans ?l) (= ans ?L))
  1250.             (let ((beg (point)))
  1251.               (replace-match to (not case-replace) (not tpu-regexp-p))
  1252.               (setq strings (1+ strings))
  1253.               (if searching-forward (forward-char -1) (goto-char beg)))
  1254.             (setq doit nil))
  1255.  
  1256.                ((or (= ans ?q) (= ans ?Q))
  1257.             (setq doit nil)))))))
  1258.  
  1259.     (message "Replaced %s occurrence%s." strings
  1260.          (if (not (= 1 strings)) "s" ""))))
  1261.  
  1262. (defun tpu-emacs-replace (&optional dont-ask)
  1263.   "A TPU-edt interface to the emacs replace functions.  If TPU-edt is
  1264. currently in regular expression mode, the emacs regular expression
  1265. replace functions are used.  If an argument is supplied, replacements
  1266. are performed without asking.  Only works in forward direction."
  1267.   (interactive "P")
  1268.   (cond (dont-ask
  1269.      (setq current-prefix-arg nil)
  1270.      (call-interactively
  1271.       (if tpu-regexp-p 'replace-regexp 'replace-string)))
  1272.     (t
  1273.      (call-interactively
  1274.       (if tpu-regexp-p 'query-replace-regexp 'query-replace)))))
  1275.  
  1276. (defun tpu-add-at-bol (text)
  1277.   "Add text to the beginning of each line in a region,
  1278. or each line in the entire buffer if no region is selected."
  1279.   (interactive
  1280.    (list (tpu-string-prompt "String to add: " 'tpu-add-at-bol-hist)))
  1281.   (if (string= "" text) (error "No string specified."))
  1282.   (cond ((tpu-mark)
  1283.      (save-excursion
  1284.        (if (> (point) (tpu-mark)) (exchange-point-and-mark))
  1285.        (while (and (< (point) (tpu-mark)) (re-search-forward "^" (tpu-mark) t))
  1286.          (if (< (point) (tpu-mark)) (replace-match text))))
  1287.      (tpu-unselect t))
  1288.     (t
  1289.      (save-excursion
  1290.        (goto-char (point-min))
  1291.        (while (and (re-search-forward "^" nil t) (not (eobp)))
  1292.          (replace-match text))))))
  1293.  
  1294. (defun tpu-add-at-eol (text)
  1295.   "Add text to the end of each line in a region,
  1296. or each line of the entire buffer if no region is selected."
  1297.   (interactive
  1298.    (list (tpu-string-prompt "String to add: " 'tpu-add-at-eol-hist)))
  1299.   (if (string= "" text) (error "No string specified."))
  1300.   (cond ((tpu-mark)
  1301.      (save-excursion
  1302.        (if (> (point) (tpu-mark)) (exchange-point-and-mark))
  1303.        (while (< (point) (tpu-mark))
  1304.          (end-of-line)
  1305.          (if (<= (point) (tpu-mark)) (insert text))
  1306.          (forward-line)))
  1307.      (tpu-unselect t))
  1308.     (t
  1309.      (save-excursion
  1310.        (goto-char (point-min))
  1311.        (while (not (eobp))
  1312.          (end-of-line) (insert text) (forward-line))))))
  1313.  
  1314. (defun tpu-trim-line-ends nil
  1315.   "Removes trailing whitespace from every line in the buffer."
  1316.   (interactive)
  1317.   (picture-clean))
  1318.  
  1319.  
  1320. ;;;
  1321. ;;;  Movement by character
  1322. ;;;
  1323. (defun tpu-char (num)
  1324.   "Move to the next character in the current direction.
  1325. A repeat count means move that many characters."
  1326.   (interactive "p")
  1327.   (if tpu-advance (tpu-forward-char num) (tpu-backward-char num)))
  1328.  
  1329. (defun tpu-forward-char (num)
  1330.   "Move right ARG characters (left if ARG is negative)."
  1331.   (interactive "p")
  1332.   (forward-char num))
  1333.  
  1334. (defun tpu-backward-char (num)
  1335.   "Move left ARG characters (right if ARG is negative)."
  1336.   (interactive "p")
  1337.   (backward-char num))
  1338.  
  1339.  
  1340. ;;;
  1341. ;;;  Movement by word
  1342. ;;;
  1343. (defconst tpu-word-separator-list '()
  1344.   "List of additional word separators.")
  1345. (defconst tpu-skip-chars "^ \t"
  1346.   "Characters to skip when moving by word.
  1347. Additional word separators are added to this string.")
  1348.  
  1349. (defun tpu-word (num)
  1350.   "Move to the beginning of the next word in the current direction.
  1351. A repeat count means move that many words."
  1352.   (interactive "p")
  1353.   (if tpu-advance (tpu-forward-to-word num) (tpu-backward-to-word num)))
  1354.  
  1355. (defun tpu-forward-to-word (num)
  1356.   "Move forward until encountering the beginning of a word.
  1357. With argument, do this that many times."
  1358.   (interactive "p")
  1359.   (while (and (> num 0) (not (eobp)))
  1360.     (let* ((beg (point))
  1361.        (end (prog2 (end-of-line) (point) (goto-char beg))))
  1362.       (cond ((eolp)
  1363.          (forward-char 1))
  1364.         ((memq (char-after (point)) tpu-word-separator-list)
  1365.          (forward-char 1)
  1366.          (skip-chars-forward " \t" end))
  1367.         (t
  1368.          (skip-chars-forward tpu-skip-chars end)
  1369.          (skip-chars-forward " \t" end))))
  1370.     (setq num (1- num))))
  1371.  
  1372. (defun tpu-backward-to-word (num)
  1373.   "Move backward until encountering the beginning of a word.
  1374. With argument, do this that many times."
  1375.   (interactive "p")
  1376.   (while (and (> num 0) (not (bobp)))
  1377.     (let* ((beg (point))
  1378.        (end (prog2 (beginning-of-line) (point) (goto-char beg))))
  1379.       (cond ((bolp)
  1380.          ( forward-char -1))
  1381.         ((memq (char-after (1- (point)))  tpu-word-separator-list)
  1382.          (forward-char -1))
  1383.         (t
  1384.          (skip-chars-backward " \t" end)
  1385.          (skip-chars-backward tpu-skip-chars end)
  1386.          (if (and (not (bolp)) (= ?  (char-syntax (char-after (point)))))
  1387.          (forward-char -1)))))
  1388.     (setq num (1- num))))
  1389.  
  1390. (defun tpu-add-word-separators (separators)
  1391.   "Add new word separators for TPU-edt word commands."
  1392.   (interactive "sSeparators: ")
  1393.   (let* ((n 0) (length (length separators)))
  1394.     (while (< n length)
  1395.       (let ((char (aref separators n))
  1396.         (ss (substring separators n (1+ n))))
  1397.     (cond ((not (memq char tpu-word-separator-list))
  1398.            (setq tpu-word-separator-list
  1399.              (append ss tpu-word-separator-list))
  1400.            (cond ((= char ?-)
  1401.               (setq tpu-skip-chars (concat tpu-skip-chars "\\-")))
  1402.              ((= char ?\\)
  1403.               (setq tpu-skip-chars (concat tpu-skip-chars "\\\\")))
  1404.              ((= char ?^)
  1405.               (setq tpu-skip-chars (concat tpu-skip-chars "\\^")))
  1406.              (t
  1407.               (setq tpu-skip-chars (concat tpu-skip-chars ss))))))
  1408.     (setq n (1+ n))))))
  1409.  
  1410. (defun tpu-reset-word-separators nil
  1411.   "Reset word separators to default value."
  1412.   (interactive)
  1413.   (setq tpu-word-separator-list nil)
  1414.   (setq tpu-skip-chars "^ \t"))
  1415.  
  1416. (defun tpu-set-word-separators (separators)
  1417.   "Set new word separators for TPU-edt word commands."
  1418.   (interactive "sSeparators: ")
  1419.   (tpu-reset-word-separators)
  1420.   (tpu-add-word-separators separators))
  1421.  
  1422.  
  1423. ;;;
  1424. ;;;  Movement by line
  1425. ;;;
  1426. (defun tpu-next-line (num)
  1427.   "Move to next line.
  1428. Prefix argument serves as a repeat count."
  1429.   (interactive "p")
  1430.   (next-line-internal num)
  1431.   (setq this-command 'next-line))
  1432.  
  1433. (defun tpu-previous-line (num)
  1434.   "Move to previous line.
  1435. Prefix argument serves as a repeat count."
  1436.   (interactive "p")
  1437.   (next-line-internal (- num))
  1438.   (setq this-command 'previous-line))
  1439.  
  1440. (defun tpu-next-beginning-of-line (num)
  1441.   "Move to beginning of line; if at beginning, move to beginning of next line.
  1442. Accepts a prefix argument for the number of lines to move."
  1443.   (interactive "p")
  1444.   (backward-char 1)
  1445.   (forward-line (- 1 num)))
  1446.  
  1447. (defun tpu-end-of-line (num)
  1448.   "Move to the next end of line in the current direction.
  1449. A repeat count means move that many lines."
  1450.   (interactive "p")
  1451.   (if tpu-advance (tpu-next-end-of-line num) (tpu-previous-end-of-line num)))
  1452.  
  1453. (defun tpu-next-end-of-line (num)
  1454.   "Move to end of line; if at end, move to end of next line.
  1455. Accepts a prefix argument for the number of lines to move."
  1456.   (interactive "p")
  1457.   (forward-char 1)
  1458.   (end-of-line num))
  1459.  
  1460. (defun tpu-previous-end-of-line (num)
  1461.   "Move EOL upward.
  1462. Accepts a prefix argument for the number of lines to move."
  1463.   (interactive "p")
  1464.   (end-of-line (- 1 num)))
  1465.  
  1466. (defun tpu-current-end-of-line nil
  1467.   "Move point to end of current line."
  1468.   (interactive)
  1469.   (let ((beg (point)))
  1470.     (end-of-line)
  1471.     (if (= beg (point)) (message "You are already at the end of a line."))))
  1472.  
  1473. (defun tpu-line (num)
  1474.   "Move to the beginning of the next line in the current direction.
  1475. A repeat count means move that many lines."
  1476.   (interactive "p")
  1477.   (if tpu-advance (tpu-forward-line num) (tpu-backward-line num)))
  1478.  
  1479. (defun tpu-forward-line (num)
  1480.   "Move to beginning of next line.
  1481. Prefix argument serves as a repeat count."
  1482.   (interactive "p")
  1483.   (forward-line num))
  1484.  
  1485. (defun tpu-backward-line (num)
  1486.   "Move to beginning of previous line.
  1487. Prefix argument serves as repeat count."
  1488.   (interactive "p")
  1489.   (forward-line (- num)))
  1490.  
  1491.  
  1492. ;;;
  1493. ;;;  Movement by paragraph
  1494. ;;;
  1495. (defun tpu-paragraph (num)
  1496.   "Move to the next paragraph in the current direction.
  1497. A repeat count means move that many paragraphs."
  1498.   (interactive "p")
  1499.   (if tpu-advance
  1500.       (tpu-next-paragraph num) (tpu-previous-paragraph num)))
  1501.  
  1502. (defun tpu-next-paragraph (num)
  1503.   "Move to beginning of the next paragraph.
  1504. Accepts a prefix argument for the number of paragraphs."
  1505.   (interactive "p")
  1506.   (beginning-of-line)
  1507.   (while (and (not (eobp)) (> num 0))
  1508.     (if (re-search-forward "^[ \t]*$" nil t)
  1509.     (if (re-search-forward "[^ \t\n]" nil t)
  1510.         (goto-char (match-beginning 0))
  1511.       (goto-char (point-max))))
  1512.     (setq num (1- num)))
  1513.   (beginning-of-line))
  1514.  
  1515.  
  1516. (defun tpu-previous-paragraph (num)
  1517.   "Move to beginning of previous paragraph.
  1518. Accepts a prefix argument for the number of paragraphs."
  1519.   (interactive "p")
  1520.   (end-of-line)
  1521.   (while (and (not (bobp)) (> num 0))
  1522.     (if (not (and (re-search-backward "^[ \t]*$" nil t)
  1523.           (re-search-backward "[^ \t\n]" nil t)
  1524.           (re-search-backward "^[ \t]*$" nil t)
  1525.           (progn (re-search-forward "[^ \t\n]" nil t)
  1526.              (goto-char (match-beginning 0)))))
  1527.     (goto-char (point-min)))
  1528.     (setq num (1- num)))
  1529.   (beginning-of-line))
  1530.  
  1531.  
  1532. ;;;
  1533. ;;;  Movement by page
  1534. ;;;
  1535. (defun tpu-page (num)
  1536.   "Move to the next page in the current direction.
  1537. A repeat count means move that many pages."
  1538.   (interactive "p")
  1539.   (if tpu-advance (forward-page num) (backward-page num))
  1540.   (if (eobp) (recenter -1)))
  1541.  
  1542.  
  1543. ;;;
  1544. ;;;  Scrolling and movement within the buffer
  1545. ;;;
  1546. (defun tpu-scroll-window (num)
  1547.   "Scroll the display to the next section in the current direction.
  1548. A repeat count means scroll that many sections."
  1549.   (interactive "p")
  1550.   (if tpu-advance (tpu-scroll-window-up num) (tpu-scroll-window-down num)))
  1551.  
  1552. (defun tpu-scroll-window-down (num)
  1553.   "Scroll the display down to the next section.
  1554. A repeat count means scroll that many sections."
  1555.   (interactive "p")
  1556.   (let* ((beg (tpu-current-line))
  1557.      (height (1- (window-height)))
  1558.      (lines (* num (/ (* height tpu-percent-scroll) 100))))
  1559.     (next-line-internal (- lines))
  1560.     (if (> lines beg) (recenter 0))))
  1561.  
  1562. (defun tpu-scroll-window-up (num)
  1563.   "Scroll the display up to the next section.
  1564. A repeat count means scroll that many sections."
  1565.   (interactive "p")
  1566.   (let* ((beg (tpu-current-line))
  1567.      (height (1- (window-height)))
  1568.      (lines (* num (/ (* height tpu-percent-scroll) 100))))
  1569.     (next-line-internal lines)
  1570.     (if (>= (+ lines beg) height) (recenter -1))))
  1571.  
  1572. (defun tpu-pan-right (num)
  1573.   "Pan right tpu-pan-columns (16 by default).
  1574. Accepts a prefix argument for the number of tpu-pan-columns to scroll."
  1575.   (interactive "p")
  1576.   (scroll-left (* tpu-pan-columns num)))
  1577.  
  1578. (defun tpu-pan-left (num)
  1579.   "Pan left tpu-pan-columns (16 by default).
  1580. Accepts a prefix argument for the number of tpu-pan-columns to scroll."
  1581.   (interactive "p")
  1582.   (scroll-right (* tpu-pan-columns num)))
  1583.  
  1584. (defun tpu-move-to-beginning nil
  1585.   "Move cursor to the beginning of buffer, but don't set the mark."
  1586.   (interactive)
  1587.   (goto-char (point-min)))
  1588.  
  1589. (defun tpu-move-to-end nil
  1590.   "Move cursor to the end of buffer, but don't set the mark."
  1591.   (interactive)
  1592.   (goto-char (point-max))
  1593.   (recenter -1))
  1594.  
  1595. (defun tpu-goto-percent (perc)
  1596.   "Move point to ARG percentage of the buffer."
  1597.   (interactive "NGoto-percentage: ")
  1598.   (if (or (> perc 100) (< perc 0))
  1599.       (error "Percentage %d out of range 0 < percent < 100" perc)
  1600.     (goto-char (/ (* (point-max) perc) 100))))
  1601.  
  1602. (defun tpu-beginning-of-window nil
  1603.   "Move cursor to top of window."
  1604.   (interactive)
  1605.   (move-to-window-line 0))
  1606.  
  1607. (defun tpu-end-of-window nil
  1608.   "Move cursor to bottom of window."
  1609.   (interactive)
  1610.   (move-to-window-line -1))
  1611.  
  1612. (defun tpu-line-to-bottom-of-window nil
  1613.   "Move the current line to the bottom of the window."
  1614.   (interactive)
  1615.   (recenter -1))
  1616.  
  1617. (defun tpu-line-to-top-of-window nil
  1618.   "Move the current line to the top of the window."
  1619.   (interactive)
  1620.   (recenter 0))
  1621.  
  1622.  
  1623. ;;;
  1624. ;;;  Direction
  1625. ;;;
  1626. (defun tpu-advance-direction nil
  1627.   "Set TPU Advance mode so keypad commands move forward."
  1628.   (interactive)
  1629.   (setq tpu-direction-string " Advance")
  1630.   (setq tpu-advance t)
  1631.   (setq tpu-reverse nil)
  1632.   (tpu-set-search)
  1633.   (tpu-update-mode-line))
  1634.  
  1635. (defun tpu-backup-direction nil
  1636.   "Set TPU Backup mode so keypad commands move backward."
  1637.   (interactive)
  1638.   (setq tpu-direction-string " Reverse")
  1639.   (setq tpu-advance nil)
  1640.   (setq tpu-reverse t)
  1641.   (tpu-set-search)
  1642.   (tpu-update-mode-line))
  1643.  
  1644.  
  1645. ;;;
  1646. ;;;  Define keymaps
  1647. ;;;
  1648. (define-key global-map "\e[" CSI-map)                         ; CSI map
  1649. (define-key global-map "\eO" SS3-map)                         ; SS3 map
  1650. (define-key SS3-map "P" GOLD-map)                             ; GOLD map
  1651. (define-key GOLD-map "\e[" GOLD-CSI-map)                      ; GOLD-CSI map
  1652. (define-key GOLD-map "\eO" GOLD-SS3-map)                      ; GOLD-SS3 map
  1653.  
  1654.  
  1655. ;;;
  1656. ;;;  CSI-map key definitions
  1657. ;;;
  1658. (define-key CSI-map "A" 'tpu-previous-line)                   ; up
  1659. (define-key CSI-map "B" 'tpu-next-line)                       ; down
  1660. (define-key CSI-map "D" 'tpu-backward-char)                   ; left
  1661. (define-key CSI-map "C" 'tpu-forward-char)                    ; right
  1662.  
  1663. (define-key CSI-map "1~" 'tpu-search)                         ; Find
  1664. (define-key CSI-map "2~" 'tpu-paste)                          ; Insert Here
  1665. (define-key CSI-map "3~" 'tpu-cut)                            ; Remove
  1666. (define-key CSI-map "4~" 'tpu-select)                         ; Select
  1667. (define-key CSI-map "5~" 'tpu-scroll-window-down)             ; Prev Screen
  1668. (define-key CSI-map "6~" 'tpu-scroll-window-up)               ; Next Screen
  1669.  
  1670. (define-key CSI-map "11~" 'nil)                               ; F1
  1671. (define-key CSI-map "12~" 'nil)                               ; F2
  1672. (define-key CSI-map "13~" 'nil)                               ; F3
  1673. (define-key CSI-map "14~" 'nil)                               ; F4
  1674. (define-key CSI-map "15~" 'nil)                               ; F5
  1675. (define-key CSI-map "17~" 'nil)                               ; F6
  1676. (define-key CSI-map "18~" 'nil)                               ; F7
  1677. (define-key CSI-map "19~" 'nil)                               ; F8
  1678. (define-key CSI-map "20~" 'nil)                               ; F9
  1679. (define-key CSI-map "21~" 'tpu-exit)                          ; F10
  1680. (define-key CSI-map "23~" 'tpu-insert-escape)                 ; F11 (ESC)
  1681. (define-key CSI-map "24~" 'tpu-next-beginning-of-line)        ; F12 (BS)
  1682. (define-key CSI-map "25~" 'tpu-delete-previous-word)          ; F13 (LF)
  1683. (define-key CSI-map "26~" 'tpu-toggle-overwrite-mode)         ; F14
  1684. (define-key CSI-map "28~" 'tpu-help)                          ; HELP
  1685. (define-key CSI-map "29~" 'execute-extended-command)          ; DO
  1686. (define-key CSI-map "31~" 'tpu-goto-breadcrumb)               ; F17
  1687. (define-key CSI-map "32~" 'nil)                               ; F18
  1688. (define-key CSI-map "33~" 'nil)                               ; F19
  1689. (define-key CSI-map "34~" 'nil)                               ; F20
  1690.  
  1691.  
  1692. ;;;
  1693. ;;;  SS3-map key definitions
  1694. ;;;
  1695. (define-key SS3-map "A" 'tpu-previous-line)                   ; up
  1696. (define-key SS3-map "B" 'tpu-next-line)                       ; down
  1697. (define-key SS3-map "C" 'tpu-forward-char)                    ; right
  1698. (define-key SS3-map "D" 'tpu-backward-char)                   ; left
  1699.  
  1700. (define-key SS3-map "Q" 'tpu-help)                            ; PF2
  1701. (define-key SS3-map "R" 'tpu-search-again)                    ; PF3
  1702. (define-key SS3-map "S" 'tpu-delete-current-line)             ; PF4
  1703. (define-key SS3-map "p" 'tpu-line)                            ; KP0
  1704. (define-key SS3-map "q" 'tpu-word)                            ; KP1
  1705. (define-key SS3-map "r" 'tpu-end-of-line)                     ; KP2
  1706. (define-key SS3-map "s" 'tpu-char)                            ; KP3
  1707. (define-key SS3-map "t" 'tpu-advance-direction)               ; KP4
  1708. (define-key SS3-map "u" 'tpu-backup-direction)                ; KP5
  1709. (define-key SS3-map "v" 'tpu-cut)                             ; KP6
  1710. (define-key SS3-map "w" 'tpu-page)                            ; KP7
  1711. (define-key SS3-map "x" 'tpu-scroll-window)                   ; KP8
  1712. (define-key SS3-map "y" 'tpu-append-region)                   ; KP9
  1713. (define-key SS3-map "m" 'tpu-delete-current-word)             ; KP-
  1714. (define-key SS3-map "l" 'tpu-delete-current-char)             ; KP,
  1715. (define-key SS3-map "n" 'tpu-select)                          ; KP.
  1716. (define-key SS3-map "M" 'newline)                             ; KPenter
  1717.  
  1718.  
  1719. ;;;
  1720. ;;;  GOLD-map key definitions
  1721. ;;;
  1722. (define-key GOLD-map "\C-A" 'tpu-toggle-overwrite-mode)       ; ^A
  1723. (define-key GOLD-map "\C-B" 'nil)                             ; ^B
  1724. (define-key GOLD-map "\C-C" 'nil)                             ; ^C
  1725. (define-key GOLD-map "\C-D" 'nil)                             ; ^D
  1726. (define-key GOLD-map "\C-E" 'nil)                             ; ^E
  1727. (define-key GOLD-map "\C-F" 'set-visited-file-name)           ; ^F
  1728. (define-key GOLD-map "\C-g" 'keyboard-quit)                   ; safety first
  1729. (define-key GOLD-map "\C-h" 'delete-other-windows)            ; BS
  1730. (define-key GOLD-map "\C-i" 'other-window)                    ; TAB
  1731. (define-key GOLD-map "\C-J" 'nil)                             ; ^J
  1732. (define-key GOLD-map "\C-K" 'tpu-define-macro-key)            ; ^K
  1733. (define-key GOLD-map "\C-l" 'downcase-region)                 ; ^L
  1734. (define-key GOLD-map "\C-M" 'nil)                             ; ^M
  1735. (define-key GOLD-map "\C-N" 'nil)                             ; ^N
  1736. (define-key GOLD-map "\C-O" 'nil)                             ; ^O
  1737. (define-key GOLD-map "\C-P" 'nil)                             ; ^P
  1738. (define-key GOLD-map "\C-Q" 'nil)                             ; ^Q
  1739. (define-key GOLD-map "\C-R" 'nil)                             ; ^R
  1740. (define-key GOLD-map "\C-S" 'nil)                             ; ^S
  1741. (define-key GOLD-map "\C-T" 'tpu-toggle-control-keys)         ; ^T
  1742. (define-key GOLD-map "\C-u" 'upcase-region)                   ; ^U
  1743. (define-key GOLD-map "\C-V" 'nil)                             ; ^V
  1744. (define-key GOLD-map "\C-w" 'tpu-write-current-buffers)       ; ^W
  1745. (define-key GOLD-map "\C-X" 'nil)                             ; ^X
  1746. (define-key GOLD-map "\C-Y" 'nil)                             ; ^Y
  1747. (define-key GOLD-map "\C-Z" 'nil)                             ; ^Z
  1748. (define-key GOLD-map " " 'undo)                               ; SPC
  1749. (define-key GOLD-map "!" 'nil)                                ; !
  1750. (define-key GOLD-map "#" 'nil)                                ; #
  1751. (define-key GOLD-map "$" 'tpu-add-at-eol)                     ; $
  1752. (define-key GOLD-map "%" 'tpu-goto-percent)                   ; %
  1753. (define-key GOLD-map "&" 'nil)                                ; &
  1754. (define-key GOLD-map "(" 'nil)                                ; (
  1755. (define-key GOLD-map ")" 'nil)                                ; )
  1756. (define-key GOLD-map "*" 'tpu-toggle-regexp)                  ; *
  1757. (define-key GOLD-map "+" 'nil)                                ; +
  1758. (define-key GOLD-map "," 'tpu-goto-breadcrumb)                ; ,
  1759. (define-key GOLD-map "-" 'negative-argument)                  ; -
  1760. (define-key GOLD-map "." 'tpu-drop-breadcrumb)                ; .
  1761. (define-key GOLD-map "/" 'tpu-emacs-replace)                  ; /
  1762. (define-key GOLD-map "0" 'digit-argument)                     ; 0
  1763. (define-key GOLD-map "1" 'digit-argument)                     ; 1
  1764. (define-key GOLD-map "2" 'digit-argument)                     ; 2
  1765. (define-key GOLD-map "3" 'digit-argument)                     ; 3
  1766. (define-key GOLD-map "4" 'digit-argument)                     ; 4
  1767. (define-key GOLD-map "5" 'digit-argument)                     ; 5
  1768. (define-key GOLD-map "6" 'digit-argument)                     ; 6
  1769. (define-key GOLD-map "7" 'digit-argument)                     ; 7
  1770. (define-key GOLD-map "8" 'digit-argument)                     ; 8
  1771. (define-key GOLD-map "9" 'digit-argument)                     ; 9
  1772. (define-key GOLD-map ":" 'nil)                                ; :
  1773. (define-key GOLD-map ";" 'tpu-trim-line-ends)                 ; ;
  1774. (define-key GOLD-map "<" 'nil)                                ; <
  1775. (define-key GOLD-map "=" 'nil)                                ; =
  1776. (define-key GOLD-map ">" 'nil)                                ; >
  1777. (define-key GOLD-map "?" 'tpu-spell-check)                    ; ?
  1778. (define-key GOLD-map "A" 'tpu-toggle-newline-and-indent)      ; A
  1779. (define-key GOLD-map "B" 'tpu-next-buffer)                    ; B
  1780. (define-key GOLD-map "C" 'repeat-complex-command)             ; C
  1781. (define-key GOLD-map "D" 'shell-command)                      ; D
  1782. (define-key GOLD-map "E" 'tpu-exit)                           ; E
  1783. (define-key GOLD-map "F" 'nil)                                ; F
  1784. (define-key GOLD-map "G" 'tpu-get)                            ; G
  1785. (define-key GOLD-map "H" 'nil)                                ; H
  1786. (define-key GOLD-map "I" 'tpu-include)                        ; I
  1787. (define-key GOLD-map "K" 'tpu-kill-buffer)                    ; K
  1788. (define-key GOLD-map "L" 'tpu-what-line)                      ; L
  1789. (define-key GOLD-map "M" 'buffer-menu)                        ; M
  1790. (define-key GOLD-map "N" 'tpu-next-file-buffer)               ; N
  1791. (define-key GOLD-map "O" 'occur)                              ; O
  1792. (define-key GOLD-map "P" 'lpr-buffer)                         ; P
  1793. (define-key GOLD-map "Q" 'tpu-quit)                           ; Q
  1794. (define-key GOLD-map "R" 'tpu-toggle-rectangle)               ; R
  1795. (define-key GOLD-map "S" 'replace)                            ; S
  1796. (define-key GOLD-map "T" 'tpu-line-to-top-of-window)          ; T
  1797. (define-key GOLD-map "U" 'undo)                               ; U
  1798. (define-key GOLD-map "V" 'tpu-version)                        ; V
  1799. (define-key GOLD-map "W" 'save-buffer)                        ; W
  1800. (define-key GOLD-map "X" 'tpu-save-all-buffers-kill-emacs)    ; X
  1801. (define-key GOLD-map "Y" 'copy-region-as-kill)                ; Y
  1802. (define-key GOLD-map "Z" 'suspend-emacs)                      ; Z
  1803. (define-key GOLD-map "[" 'blink-matching-open)                ; [
  1804. (define-key GOLD-map "\\" 'nil)                               ; \
  1805. (define-key GOLD-map "]" 'blink-matching-open)                ; ]
  1806. (define-key GOLD-map "^" 'tpu-add-at-bol)                     ; ^
  1807. (define-key GOLD-map "_" 'split-window-vertically)            ; -
  1808. (define-key GOLD-map "`" 'what-line)                          ; `
  1809. (define-key GOLD-map "a" 'tpu-toggle-newline-and-indent)      ; a
  1810. (define-key GOLD-map "b" 'tpu-next-buffer)                    ; b
  1811. (define-key GOLD-map "c" 'repeat-complex-command)             ; c
  1812. (define-key GOLD-map "d" 'shell-command)                      ; d
  1813. (define-key GOLD-map "e" 'tpu-exit)                           ; e
  1814. (define-key GOLD-map "f" 'nil)                                ; f
  1815. (define-key GOLD-map "g" 'tpu-get)                            ; g
  1816. (define-key GOLD-map "h" 'nil)                                ; h
  1817. (define-key GOLD-map "i" 'tpu-include)                        ; i
  1818. (define-key GOLD-map "k" 'tpu-kill-buffer)                    ; k
  1819. (define-key GOLD-map "l" 'goto-line)                          ; l
  1820. (define-key GOLD-map "m" 'buffer-menu)                        ; m
  1821. (define-key GOLD-map "n" 'tpu-next-file-buffer)               ; n
  1822. (define-key GOLD-map "o" 'occur)                              ; o
  1823. (define-key GOLD-map "p" 'lpr-region)                         ; p
  1824. (define-key GOLD-map "q" 'tpu-quit)                           ; q
  1825. (define-key GOLD-map "r" 'tpu-toggle-rectangle)               ; r
  1826. (define-key GOLD-map "s" 'replace)                            ; s
  1827. (define-key GOLD-map "t" 'tpu-line-to-top-of-window)          ; t
  1828. (define-key GOLD-map "u" 'undo)                               ; u
  1829. (define-key GOLD-map "v" 'tpu-version)                        ; v
  1830. (define-key GOLD-map "w" 'save-buffer)                        ; w
  1831. (define-key GOLD-map "x" 'tpu-save-all-buffers-kill-emacs)    ; x
  1832. (define-key GOLD-map "y" 'copy-region-as-kill)                ; y
  1833. (define-key GOLD-map "z" 'suspend-emacs)                      ; z
  1834. (define-key GOLD-map "{" 'nil)                                ; {
  1835. (define-key GOLD-map "|" 'split-window-horizontally)          ; |
  1836. (define-key GOLD-map "}" 'nil)                                ; }
  1837. (define-key GOLD-map "~" 'exchange-point-and-mark)            ; ~
  1838. (define-key GOLD-map "\177" 'delete-window)                   ; <X]
  1839.  
  1840.  
  1841. ;;;
  1842. ;;;  GOLD-CSI-map key definitions
  1843. ;;;
  1844. (define-key GOLD-CSI-map "A" 'tpu-move-to-beginning)          ; up-arrow
  1845. (define-key GOLD-CSI-map "B" 'tpu-move-to-end)                ; down-arrow
  1846. (define-key GOLD-CSI-map "C" 'end-of-line)                    ; right-arrow
  1847. (define-key GOLD-CSI-map "D" 'beginning-of-line)              ; left-arrow
  1848.  
  1849. (define-key GOLD-CSI-map "1~" 'nil)                           ; Find
  1850. (define-key GOLD-CSI-map "2~" 'nil)                           ; Insert Here
  1851. (define-key GOLD-CSI-map "3~" 'tpu-store-text)                ; Remove
  1852. (define-key GOLD-CSI-map "4~" 'tpu-unselect)                  ; Select
  1853. (define-key GOLD-CSI-map "5~" 'tpu-previous-window)           ; Prev Screen
  1854. (define-key GOLD-CSI-map "6~" 'tpu-next-window)               ; Next Screen
  1855.  
  1856. (define-key GOLD-CSI-map "11~" 'nil)                          ; F1
  1857. (define-key GOLD-CSI-map "12~" 'nil)                          ; F2
  1858. (define-key GOLD-CSI-map "13~" 'nil)                          ; F3
  1859. (define-key GOLD-CSI-map "14~" 'nil)                          ; F4
  1860. (define-key GOLD-CSI-map "16~" 'nil)                          ; F5
  1861. (define-key GOLD-CSI-map "17~" 'nil)                          ; F6
  1862. (define-key GOLD-CSI-map "18~" 'nil)                          ; F7
  1863. (define-key GOLD-CSI-map "19~" 'nil)                          ; F8
  1864. (define-key GOLD-CSI-map "20~" 'nil)                          ; F9
  1865. (define-key GOLD-CSI-map "21~" 'nil)                          ; F10
  1866. (define-key GOLD-CSI-map "23~" 'nil)                          ; F11
  1867. (define-key GOLD-CSI-map "24~" 'nil)                          ; F12
  1868. (define-key GOLD-CSI-map "25~" 'nil)                          ; F13
  1869. (define-key GOLD-CSI-map "26~" 'nil)                          ; F14
  1870. (define-key GOLD-CSI-map "28~" 'describe-bindings)            ; HELP
  1871. (define-key GOLD-CSI-map "29~" 'nil)                          ; DO
  1872. (define-key GOLD-CSI-map "31~" 'tpu-drop-breadcrumb)          ; F17
  1873. (define-key GOLD-CSI-map "32~" 'nil)                          ; F18
  1874. (define-key GOLD-CSI-map "33~" 'nil)                          ; F19
  1875. (define-key GOLD-CSI-map "34~" 'nil)                          ; F20
  1876.  
  1877.  
  1878. ;;;
  1879. ;;;  GOLD-SS3-map key definitions
  1880. ;;;
  1881. (define-key GOLD-SS3-map "A" 'tpu-move-to-beginning)          ; up-arrow
  1882. (define-key GOLD-SS3-map "B" 'tpu-move-to-end)                ; down-arrow
  1883. (define-key GOLD-SS3-map "C" 'end-of-line)                    ; right-arrow
  1884. (define-key GOLD-SS3-map "D" 'beginning-of-line)              ; left-arrow
  1885.  
  1886. (define-key GOLD-SS3-map "P" 'keyboard-quit)                  ; PF1
  1887. (define-key GOLD-SS3-map "Q" 'help-for-help)                  ; PF2
  1888. (define-key GOLD-SS3-map "R" 'tpu-search)                     ; PF3
  1889. (define-key GOLD-SS3-map "S" 'tpu-undelete-lines)             ; PF4
  1890. (define-key GOLD-SS3-map "p" 'open-line)                      ; KP0
  1891. (define-key GOLD-SS3-map "q" 'tpu-change-case)                ; KP1
  1892. (define-key GOLD-SS3-map "r" 'tpu-delete-to-eol)              ; KP2
  1893. (define-key GOLD-SS3-map "s" 'tpu-special-insert)             ; KP3
  1894. (define-key GOLD-SS3-map "t" 'tpu-move-to-end)                ; KP4
  1895. (define-key GOLD-SS3-map "u" 'tpu-move-to-beginning)          ; KP5
  1896. (define-key GOLD-SS3-map "v" 'tpu-paste)                      ; KP6
  1897. (define-key GOLD-SS3-map "w" 'execute-extended-command)       ; KP7
  1898. (define-key GOLD-SS3-map "x" 'tpu-fill)                       ; KP8
  1899. (define-key GOLD-SS3-map "y" 'tpu-replace)                    ; KP9
  1900. (define-key GOLD-SS3-map "m" 'tpu-undelete-words)             ; KP-
  1901. (define-key GOLD-SS3-map "l" 'tpu-undelete-char)              ; KP,
  1902. (define-key GOLD-SS3-map "n" 'tpu-unselect)                   ; KP.
  1903. (define-key GOLD-SS3-map "M" 'tpu-substitute)                 ; KPenter
  1904.  
  1905.  
  1906. ;;;
  1907. ;;;  Repeat complex command map additions to make arrows work
  1908. ;;;
  1909. (cond ((boundp 'repeat-complex-command-map)
  1910.        (define-key repeat-complex-command-map "\e[A" 'previous-complex-command)
  1911.        (define-key repeat-complex-command-map "\e[B" 'next-complex-command)
  1912.        (define-key repeat-complex-command-map "\eOA" 'previous-complex-command)
  1913.        (define-key repeat-complex-command-map "\eOB" 'next-complex-command)))
  1914.  
  1915.  
  1916. ;;;
  1917. ;;;  Minibuffer map additions to make KP_enter = RET
  1918. ;;;
  1919. (define-key minibuffer-local-map "\eOM" 'exit-minibuffer)
  1920. (define-key minibuffer-local-ns-map "\eOM" 'exit-minibuffer)
  1921. (define-key minibuffer-local-completion-map "\eOM" 'exit-minibuffer)
  1922. (define-key minibuffer-local-must-match-map "\eOM" 'minibuffer-complete-and-exit)
  1923. (and (boundp 'repeat-complex-command-map)
  1924.      (define-key repeat-complex-command-map "\eOM" 'exit-minibuffer))
  1925.  
  1926.  
  1927. ;;;
  1928. ;;;  Map control keys
  1929. ;;;
  1930. (define-key global-map "\C-\\" 'quoted-insert)                ; ^\
  1931. (define-key global-map "\C-a" 'tpu-toggle-overwrite-mode)     ; ^A
  1932. (define-key global-map "\C-b" 'repeat-complex-command)        ; ^B
  1933. (define-key global-map "\C-e" 'tpu-current-end-of-line)       ; ^E
  1934. (define-key global-map "\C-f" 'set-visited-file-name)         ; ^F
  1935. (define-key global-map "\C-h" 'tpu-next-beginning-of-line)    ; ^H (BS)
  1936. (define-key global-map "\C-j" 'tpu-delete-previous-word)      ; ^J (LF)
  1937. (define-key global-map "\C-k" 'tpu-define-macro-key)          ; ^K
  1938. (define-key global-map "\C-l" 'tpu-insert-formfeed)           ; ^L (FF)
  1939. (define-key global-map "\C-r" 'recenter)                      ; ^R
  1940. (define-key global-map "\C-u" 'tpu-delete-to-bol)             ; ^U
  1941. (define-key global-map "\C-v" 'quoted-insert)                 ; ^V
  1942. (define-key global-map "\C-w" 'redraw-display)                ; ^W
  1943. (define-key global-map "\C-z" 'tpu-exit)                      ; ^Z
  1944.  
  1945.  
  1946. ;;;
  1947. ;;;  Functions to reset and toggle the control key bindings
  1948. ;;;
  1949. (defun tpu-reset-control-keys (tpu-style)
  1950.   "Set control keys to TPU or emacs style functions."
  1951.   (let* ((tpu   (and tpu-style (not tpu-control-keys)))
  1952.      (emacs (and (not tpu-style) tpu-control-keys))
  1953.      (doit  (or tpu emacs)))
  1954.     (cond (doit
  1955.        (if emacs (setq tpu-global-map (copy-keymap global-map)))
  1956.        (let ((map (if tpu
  1957.               (copy-keymap tpu-global-map)
  1958.             (copy-keymap tpu-original-global-map))))
  1959.  
  1960.           (define-key global-map "\C-\\" (lookup-key map "\C-\\"))   ; ^\
  1961.           (define-key global-map "\C-a" (lookup-key map "\C-a"))     ; ^A
  1962.           (define-key global-map "\C-b" (lookup-key map "\C-b"))     ; ^B
  1963.           (define-key global-map "\C-e" (lookup-key map "\C-e"))     ; ^E
  1964.           (define-key global-map "\C-f" (lookup-key map "\C-f"))     ; ^F
  1965.          (define-key global-map "\C-h" (lookup-key map "\C-h"))     ; ^H (BS)
  1966.          (define-key global-map "\C-j" (lookup-key map "\C-j"))     ; ^J (LF)
  1967.          (define-key global-map "\C-k" (lookup-key map "\C-k"))     ; ^K
  1968.          (define-key global-map "\C-l" (lookup-key map "\C-l"))     ; ^L (FF)
  1969.          (define-key global-map "\C-r" (lookup-key map "\C-r"))     ; ^R
  1970.          (define-key global-map "\C-u" (lookup-key map "\C-u"))     ; ^U
  1971.          (define-key global-map "\C-v" (lookup-key map "\C-v"))     ; ^V
  1972.          (define-key global-map "\C-w" (lookup-key map "\C-w"))     ; ^W
  1973.          (define-key global-map "\C-z" (lookup-key map "\C-z"))     ; ^Z
  1974.          (setq tpu-control-keys tpu-style))))))
  1975.  
  1976. (defun tpu-toggle-control-keys nil
  1977.   "Toggles control key bindings between TPU-edt and Emacs."
  1978.   (interactive)
  1979.   (tpu-reset-control-keys (not tpu-control-keys))
  1980.   (and (interactive-p)
  1981.        (message "Control keys function with %s bindings."
  1982.         (if tpu-control-keys "TPU-edt" "Emacs"))))
  1983.  
  1984.  
  1985. ;;;
  1986. ;;;  Emacs version 19 minibuffer history support
  1987. ;;;
  1988. (defun tpu-next-history-element (n)
  1989.   "Insert the next element of the minibuffer history into the minibuffer."
  1990.   (interactive "p")
  1991.   (next-history-element n)
  1992.   (goto-char (point-max)))
  1993.  
  1994. (defun tpu-previous-history-element (n)
  1995.   "Insert the previous element of the minibuffer history into the minibuffer."
  1996.   (interactive "p")
  1997.   (previous-history-element n)
  1998.   (goto-char (point-max)))
  1999.  
  2000. (defun tpu-arrow-history nil
  2001.   "Modify minibuffer maps to use arrows for history recall."
  2002.   (interactive)
  2003.   (let ((loc (where-is-internal 'tpu-previous-line)) (cur nil))
  2004.     (while (setq cur (car loc))
  2005.       (define-key read-expression-map cur 'tpu-previous-history-element)
  2006.       (define-key minibuffer-local-map cur 'tpu-previous-history-element)
  2007.       (define-key minibuffer-local-ns-map cur 'tpu-previous-history-element)
  2008.       (define-key minibuffer-local-completion-map cur 'tpu-previous-history-element)
  2009.       (define-key minibuffer-local-must-match-map cur 'tpu-previous-history-element)
  2010.       (setq loc (cdr loc)))
  2011.  
  2012.     (setq loc (where-is-internal 'tpu-next-line))
  2013.     (while (setq cur (car loc))
  2014.       (define-key read-expression-map cur 'tpu-next-history-element)
  2015.       (define-key minibuffer-local-map cur 'tpu-next-history-element)
  2016.       (define-key minibuffer-local-ns-map cur 'tpu-next-history-element)
  2017.       (define-key minibuffer-local-completion-map cur 'tpu-next-history-element)
  2018.       (define-key minibuffer-local-must-match-map cur 'tpu-next-history-element)
  2019.       (setq loc (cdr loc)))))
  2020.  
  2021.  
  2022. ;;;
  2023. ;;;  Emacs version 19 X-windows key definition support
  2024. ;;;
  2025. (defun tpu-load-xkeys (file)
  2026.   "Load the TPU-edt X-windows key definitions FILE.
  2027. If FILE is nil, try to load a default file.  The default file names are
  2028. ~/.tpu-lucid-keys for Lucid emacs, and ~/.tpu-gnu-keys for GNU emacs."
  2029.   (interactive "fX key definition file: ")
  2030.   (cond (file
  2031.      (setq file (expand-file-name file)))
  2032.     ((boundp 'tpu-xkeys-file)
  2033.      (setq file (expand-file-name tpu-xkeys-file)))
  2034.     (tpu-gnu-emacs19-p
  2035.      (setq file (expand-file-name "~/.tpu-gnu-keys")))
  2036.     (tpu-lucid-emacs19-p
  2037.      (setq file (expand-file-name "~/.tpu-lucid-keys"))))
  2038.   (cond ((file-readable-p file)
  2039.      (load-file file))
  2040.     (t
  2041.      (insert "
  2042.  
  2043.     Ack!!  You're running TPU-edt under X-windows without loading an X
  2044.     key definition file.   To create  a TPU-edt X key definition file,
  2045.     run the tpu-edt-mapper.el program.  It came with TPU-edt.  It even
  2046.     includes directions on  how to use it!  Perhaps it's laying around
  2047.     here someplace.  ")
  2048.      (let ((file "tpu-edt-mapper.el")
  2049.            (found nil)
  2050.            (path nil)
  2051.            (search-list (append (list (expand-file-name ".")) load-path)))
  2052.        (while (and (not found) search-list)
  2053.          (setq path (concat (car search-list)
  2054.                 (if (string-match "/$" (car search-list)) "" "/")
  2055.                 file))
  2056.          (if (and (file-exists-p path) (not (file-directory-p path)))
  2057.          (setq found t))
  2058.          (setq search-list (cdr search-list)))
  2059.        (cond (found
  2060.           (insert (format
  2061.                "Ah yes, there it is, in \n\n       %s \n\n" path))
  2062.           (if (tpu-y-or-n-p "Do you want to run it now? ")
  2063.               (load-file path)))
  2064.          (t
  2065.           (insert "Nope, I can't seem to find it.  :-(\n\n")
  2066.           (sit-for 120)))))))
  2067.  
  2068.  
  2069. ;;;
  2070. ;;;  Start and Stop TPU-edt
  2071. ;;;
  2072. (defun tpu-edt-on nil
  2073.   "Sets TPU-edt emulation on."
  2074.   (interactive)
  2075.   (cond
  2076.    ((not tpu-edt-mode)
  2077.     ;; we use picture-mode functions
  2078.     (require 'picture)
  2079.     (tpu-reset-control-keys t)
  2080.     (cond (tpu-emacs19-p
  2081.        (and window-system (tpu-load-xkeys nil))
  2082.        (tpu-arrow-history))
  2083.       (t
  2084.        ;; define ispell functions
  2085.        (autoload 'ispell-word "ispell" "Check spelling of word at or before point" t)
  2086.        (autoload 'ispell-complete-word "ispell" "Complete word at or before point" t)
  2087.        (autoload 'ispell-buffer "ispell" "Check spelling of entire buffer" t)
  2088.        (autoload 'ispell-region "ispell" "Check spelling of region" t)))
  2089.     (tpu-set-mode-line t)
  2090.     (tpu-advance-direction)
  2091.     ;; set page delimiter, display line truncation, and scrolling like TPU
  2092.     (setq-default page-delimiter "\f")
  2093.     (setq-default truncate-lines t)
  2094.     (setq scroll-step 1)
  2095.     (setq tpu-edt-mode t))))
  2096.  
  2097. (defun tpu-edt-off nil
  2098.   "Sets TPU-edt emulation off.  Note that the keypad is left on."
  2099.   (interactive)
  2100.   (cond
  2101.    (tpu-edt-mode
  2102.     (tpu-reset-control-keys nil)
  2103.     (tpu-set-mode-line nil)
  2104.     (setq-default page-delimiter "^\f")
  2105.     (setq-default truncate-lines nil)
  2106.     (setq scroll-step 0)
  2107.     (use-global-map global-map)
  2108.     (setq tpu-edt-mode nil))))
  2109.  
  2110.  
  2111. ;;;
  2112. ;;;  Turn on TPU-edt and announce it as a feature
  2113. ;;;
  2114. (tpu-edt-mode)
  2115.  
  2116. (provide 'tpu-edt)
  2117.  
  2118. ;;; tpu-edt.el ends here
  2119.