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