* rcs2log (Help): Clarify wording of the usage message.
[emacs.git] / lisp / emulation / tpu-extras.el
blobced88ff3fdf13fdd73ec596cc39d1d353f538b6d
1 ;;; tpu-extras.el --- scroll margins and free cursor mode for TPU-edt
3 ;; Copyright (C) 1993, 1994, 1995, 2000 Free Software Foundation, Inc.
5 ;; Author: Rob Riepel <riepel@networking.stanford.edu>
6 ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
7 ;; Keywords: emulations
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Use the functions defined here to customize TPU-edt to your tastes by
29 ;; setting scroll margins and/or turning on free cursor mode. Here's an
30 ;; example for your .emacs file.
32 ;; (tpu-set-cursor-free) ; Set cursor free.
33 ;; (tpu-set-scroll-margins "10%" "15%") ; Set scroll margins.
35 ;; Scroll margins and cursor binding can be changed from within emacs using
36 ;; the following commands:
38 ;; tpu-set-scroll-margins or set scroll margins
39 ;; tpu-set-cursor-bound or set cursor bound
40 ;; tpu-set-cursor-free or set cursor free
42 ;; Additionally, Gold-F toggles between bound and free cursor modes.
44 ;; Note that switching out of free cursor mode or exiting TPU-edt while in
45 ;; free cursor mode strips trailing whitespace from every line in the file.
48 ;;; Details:
50 ;; The functions contained in this file implement scroll margins and free
51 ;; cursor mode. The following keys and commands are affected.
53 ;; key/command function scroll cursor
55 ;; Up-Arrow previous line x x
56 ;; Down-Arrow next line x x
57 ;; Right-Arrow next character x
58 ;; Left-Arrow previous character x
59 ;; KP0 next or previous line x
60 ;; KP7 next or previous page x
61 ;; KP8 next or previous screen x
62 ;; KP2 next or previous end-of-line x x
63 ;; Control-e current end-of-line x
64 ;; Control-h previous beginning-of-line x
65 ;; Next Scr next screen x
66 ;; Prev Scr previous screen x
67 ;; Search find a string x
68 ;; Replace find and replace a string x
69 ;; Newline insert a newline x
70 ;; Paragraph next or previous paragraph x
71 ;; Auto-Fill break lines on spaces x
73 ;; These functions are not part of the base TPU-edt for the following
74 ;; reasons:
76 ;; Free cursor mode is implemented with the emacs picture-mode functions.
77 ;; These functions support moving the cursor all over the screen, however,
78 ;; when the cursor is moved past the end of a line, spaces or tabs are
79 ;; appended to the line - even if no text is entered in that area. In
80 ;; order for a free cursor mode to work exactly like TPU/edt, this trailing
81 ;; whitespace needs to be dealt with in every function that might encounter
82 ;; it. Such global changes are impractical, however, free cursor mode is
83 ;; too valuable to abandon completely, so it has been implemented in those
84 ;; functions where it serves best.
86 ;; The implementation of scroll margins adds overhead to previously
87 ;; simple and often used commands. These commands are now responsible
88 ;; for their normal operation and part of the display function. There
89 ;; is a possibility that this display overhead could adversely affect the
90 ;; performance of TPU-edt on slower computers. In order to support the
91 ;; widest range of computers, scroll margin support is optional.
93 ;; It's actually not known whether the overhead associated with scroll
94 ;; margin support is significant. If you find that it is, please send
95 ;; a note describing the extent of the performance degradation. Be sure
96 ;; to include a description of the platform where you're running TPU-edt.
97 ;; Send your note to the address provided by Gold-V.
99 ;; Even with these differences and limitations, these functions implement
100 ;; important aspects of the real TPU/edt. Those who miss free cursor mode
101 ;; and/or scroll margins will appreciate these implementations.
103 ;;; Code:
106 ;;; Gotta have tpu-edt
108 (require 'tpu-edt)
111 ;;; Customization variables
113 (defcustom tpu-top-scroll-margin 0
114 "*Scroll margin at the top of the screen.
115 Interpreted as a percent of the current window size."
116 :type 'integer
117 :group 'tpu)
118 (defcustom tpu-bottom-scroll-margin 0
119 "*Scroll margin at the bottom of the screen.
120 Interpreted as a percent of the current window size."
121 :type 'integer
122 :group 'tpu)
124 (defcustom tpu-backward-char-like-tpu t
125 "*If non-nil, in free cursor mode backward-char (left-arrow) works
126 just like TPU/edt. Otherwise, backward-char will move to the end of
127 the previous line when starting from a line beginning."
128 :type 'boolean
129 :group 'tpu)
132 ;;; Global variables
134 (defvar tpu-cursor-free nil
135 "If non-nil, let the cursor roam free.")
138 ;;; Hooks -- Set cursor free in picture mode.
139 ;;; Clean up when writing a file from cursor free mode.
141 (add-hook 'picture-mode-hook 'tpu-set-cursor-free)
143 (defun tpu-write-file-hook nil
144 "Eliminate whitespace at ends of lines, if the cursor is free."
145 (if (and (buffer-modified-p) tpu-cursor-free) (tpu-trim-line-ends)))
147 (or (memq 'tpu-write-file-hook write-file-hooks)
148 (setq write-file-hooks
149 (cons 'tpu-write-file-hook write-file-hooks)))
152 ;;; Utility routines for implementing scroll margins
154 (defun tpu-top-check (beg lines)
155 "Enforce scroll margin at the top of screen."
156 (let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100)))
157 (cond ((< beg margin) (recenter beg))
158 ((< (- beg lines) margin) (recenter margin)))))
160 (defun tpu-bottom-check (beg lines)
161 "Enforce scroll margin at the bottom of screen."
162 (let* ((height (window-height))
163 (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
164 ;; subtract 1 from height because it includes mode line
165 (difference (- height margin 1)))
166 (cond ((> beg difference) (recenter beg))
167 ((> (+ beg lines) difference) (recenter (- margin))))))
170 ;;; Movement by character
172 (defun tpu-forward-char (num)
173 "Move right ARG characters (left if ARG is negative)."
174 (interactive "p")
175 (if tpu-cursor-free (picture-forward-column num) (forward-char num)))
177 (defun tpu-backward-char (num)
178 "Move left ARG characters (right if ARG is negative)."
179 (interactive "p")
180 (cond ((not tpu-cursor-free)
181 (backward-char num))
182 (tpu-backward-char-like-tpu
183 (picture-backward-column num))
184 ((bolp)
185 (backward-char 1)
186 (picture-end-of-line)
187 (picture-backward-column (1- num)))
189 (picture-backward-column num))))
192 ;;; Movement by line
194 (defun tpu-next-line (num)
195 "Move to next line.
196 Prefix argument serves as a repeat count."
197 (interactive "p")
198 (let ((beg (tpu-current-line)))
199 (if tpu-cursor-free (or (eobp) (picture-move-down num))
200 (next-line-internal num))
201 (tpu-bottom-check beg num)
202 (setq this-command 'next-line)))
204 (defun tpu-previous-line (num)
205 "Move to previous line.
206 Prefix argument serves as a repeat count."
207 (interactive "p")
208 (let ((beg (tpu-current-line)))
209 (if tpu-cursor-free (picture-move-up num) (next-line-internal (- num)))
210 (tpu-top-check beg num)
211 (setq this-command 'previous-line)))
213 (defun tpu-next-beginning-of-line (num)
214 "Move to beginning of line; if at beginning, move to beginning of next line.
215 Accepts a prefix argument for the number of lines to move."
216 (interactive "p")
217 (let ((beg (tpu-current-line)))
218 (backward-char 1)
219 (forward-visible-line (- 1 num))
220 (tpu-top-check beg num)))
222 (defun tpu-next-end-of-line (num)
223 "Move to end of line; if at end, move to end of next line.
224 Accepts a prefix argument for the number of lines to move."
225 (interactive "p")
226 (let ((beg (tpu-current-line)))
227 (cond (tpu-cursor-free
228 (let ((beg (point)))
229 (if (< 1 num) (forward-line num))
230 (picture-end-of-line)
231 (if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
233 (forward-char)
234 (end-of-line num)))
235 (tpu-bottom-check beg num)))
237 (defun tpu-previous-end-of-line (num)
238 "Move EOL upward.
239 Accepts a prefix argument for the number of lines to move."
240 (interactive "p")
241 (let ((beg (tpu-current-line)))
242 (cond (tpu-cursor-free
243 (picture-end-of-line (- 1 num)))
245 (end-of-line (- 1 num))))
246 (tpu-top-check beg num)))
248 (defun tpu-current-end-of-line nil
249 "Move point to end of current line."
250 (interactive)
251 (let ((beg (point)))
252 (if tpu-cursor-free (picture-end-of-line) (end-of-line))
253 (if (= beg (point)) (message "You are already at the end of a line."))))
255 (defun tpu-forward-line (num)
256 "Move to beginning of next line.
257 Prefix argument serves as a repeat count."
258 (interactive "p")
259 (let ((beg (tpu-current-line)))
260 (next-line-internal num)
261 (tpu-bottom-check beg num)
262 (beginning-of-line)))
264 (defun tpu-backward-line (num)
265 "Move to beginning of previous line.
266 Prefix argument serves as repeat count."
267 (interactive "p")
268 (let ((beg (tpu-current-line)))
269 (or (bolp) (>= 0 num) (setq num (- num 1)))
270 (next-line-internal (- num))
271 (tpu-top-check beg num)
272 (beginning-of-line)))
275 ;;; Movement by paragraph
277 (defun tpu-paragraph (num)
278 "Move to the next paragraph in the current direction.
279 A repeat count means move that many paragraphs."
280 (interactive "p")
281 (let* ((left nil)
282 (beg (tpu-current-line))
283 (height (window-height))
284 (top-percent
285 (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
286 (bottom-percent
287 (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
288 (top-margin (/ (* height top-percent) 100))
289 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
290 (bottom-margin (max beg (- height bottom-up-margin 1)))
291 (top (save-excursion (move-to-window-line top-margin) (point)))
292 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
293 (far (save-excursion
294 (goto-char bottom) (forward-line (- height 2)) (point))))
295 (cond (tpu-advance
296 (tpu-next-paragraph num)
297 (cond((> (point) far)
298 (setq left (save-excursion (forward-line height)))
299 (if (= 0 left) (recenter top-margin)
300 (recenter (- left bottom-up-margin))))
302 (and (> (point) bottom) (recenter bottom-margin)))))
304 (tpu-previous-paragraph num)
305 (and (< (point) top) (recenter (min beg top-margin)))))))
308 ;;; Movement by page
310 (defun tpu-page (num)
311 "Move to the next page in the current direction.
312 A repeat count means move that many pages."
313 (interactive "p")
314 (let* ((left nil)
315 (beg (tpu-current-line))
316 (height (window-height))
317 (top-percent
318 (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
319 (bottom-percent
320 (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
321 (top-margin (/ (* height top-percent) 100))
322 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
323 (bottom-margin (max beg (- height bottom-up-margin 1)))
324 (top (save-excursion (move-to-window-line top-margin) (point)))
325 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
326 (far (save-excursion
327 (goto-char bottom) (forward-line (- height 2)) (point))))
328 (cond (tpu-advance
329 (forward-page num)
330 (cond((> (point) far)
331 (setq left (save-excursion (forward-line height)))
332 (if (= 0 left) (recenter top-margin)
333 (recenter (- left bottom-up-margin))))
335 (and (> (point) bottom) (recenter bottom-margin)))))
337 (backward-page num)
338 (and (< (point) top) (recenter (min beg top-margin)))))))
341 ;;; Scrolling
343 (defun tpu-scroll-window-down (num)
344 "Scroll the display down to the next section.
345 A repeat count means scroll that many sections."
346 (interactive "p")
347 (let* ((beg (tpu-current-line))
348 (height (1- (window-height)))
349 (lines (* num (/ (* height tpu-percent-scroll) 100))))
350 (next-line-internal (- lines))
351 (tpu-top-check beg lines)))
353 (defun tpu-scroll-window-up (num)
354 "Scroll the display up to the next section.
355 A repeat count means scroll that many sections."
356 (interactive "p")
357 (let* ((beg (tpu-current-line))
358 (height (1- (window-height)))
359 (lines (* num (/ (* height tpu-percent-scroll) 100))))
360 (next-line-internal lines)
361 (tpu-bottom-check beg lines)))
364 ;;; Replace the TPU-edt internal search function
366 (defun tpu-search-internal (pat &optional quiet)
367 "Search for a string or regular expression."
368 (let* ((left nil)
369 (beg (tpu-current-line))
370 (height (window-height))
371 (top-percent
372 (if (= 0 tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
373 (bottom-percent
374 (if (= 0 tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
375 (top-margin (/ (* height top-percent) 100))
376 (bottom-up-margin (+ 1 (/ (* height bottom-percent) 100)))
377 (bottom-margin (max beg (- height bottom-up-margin 1)))
378 (top (save-excursion (move-to-window-line top-margin) (point)))
379 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
380 (far (save-excursion
381 (goto-char bottom) (forward-line (- height 2)) (point))))
382 (tpu-search-internal-core pat quiet)
383 (if tpu-searching-forward
384 (cond((> (point) far)
385 (setq left (save-excursion (forward-line height)))
386 (if (= 0 left) (recenter top-margin)
387 (recenter (- left bottom-up-margin))))
389 (and (> (point) bottom) (recenter bottom-margin))))
390 (and (< (point) top) (recenter (min beg top-margin))))))
394 ;;; Replace the newline, newline-and-indent, and do-auto-fill functions
396 (or (fboundp 'tpu-old-newline)
397 (fset 'tpu-old-newline (symbol-function 'newline)))
398 (or (fboundp 'tpu-old-do-auto-fill)
399 (fset 'tpu-old-do-auto-fill (symbol-function 'do-auto-fill)))
400 (or (fboundp 'tpu-old-newline-and-indent)
401 (fset 'tpu-old-newline-and-indent (symbol-function 'newline-and-indent)))
403 (defun newline (&optional num)
404 "Insert a newline. With arg, insert that many newlines.
405 In Auto Fill mode, can break the preceding line if no numeric arg.
406 This is the TPU-edt version that respects the bottom scroll margin."
407 (interactive "p")
408 (let ((beg (tpu-current-line)))
409 (or num (setq num 1))
410 (tpu-old-newline num)
411 (tpu-bottom-check beg num)))
413 (defun newline-and-indent nil
414 "Insert a newline, then indent according to major mode.
415 Indentation is done using the current indent-line-function.
416 In programming language modes, this is the same as TAB.
417 In some text modes, where TAB inserts a tab, this indents
418 to the specified left-margin column. This is the TPU-edt
419 version that respects the bottom scroll margin."
420 (interactive)
421 (let ((beg (tpu-current-line)))
422 (tpu-old-newline-and-indent)
423 (tpu-bottom-check beg 1)))
425 (defun do-auto-fill nil
426 "TPU-edt version that respects the bottom scroll margin."
427 (let ((beg (tpu-current-line)))
428 (tpu-old-do-auto-fill)
429 (tpu-bottom-check beg 1)))
432 ;;; Function to set scroll margins
434 ;;;###autoload
435 (defun tpu-set-scroll-margins (top bottom)
436 "Set scroll margins."
437 (interactive
438 "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
439 \nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
440 ;; set top scroll margin
441 (or (string= top "")
442 (if (string= "%" (substring top -1))
443 (setq tpu-top-scroll-margin (string-to-int top))
444 (setq tpu-top-scroll-margin
445 (/ (1- (+ (* (string-to-int top) 100) (window-height)))
446 (window-height)))))
447 ;; set bottom scroll margin
448 (or (string= bottom "")
449 (if (string= "%" (substring bottom -1))
450 (setq tpu-bottom-scroll-margin (string-to-int bottom))
451 (setq tpu-bottom-scroll-margin
452 (/ (1- (+ (* (string-to-int bottom) 100) (window-height)))
453 (window-height)))))
454 ;; report scroll margin settings if running interactively
455 (and (interactive-p)
456 (message "Scroll margins set. Top = %s%%, Bottom = %s%%"
457 tpu-top-scroll-margin tpu-bottom-scroll-margin)))
460 ;;; Functions to set cursor bound or free
462 ;;;###autoload
463 (defun tpu-set-cursor-free nil
464 "Allow the cursor to move freely about the screen."
465 (interactive)
466 (setq tpu-cursor-free t)
467 (substitute-key-definition 'tpu-set-cursor-free
468 'tpu-set-cursor-bound
469 GOLD-map)
470 (message "The cursor will now move freely about the screen."))
472 ;;;###autoload
473 (defun tpu-set-cursor-bound nil
474 "Constrain the cursor to the flow of the text."
475 (interactive)
476 (tpu-trim-line-ends)
477 (setq tpu-cursor-free nil)
478 (substitute-key-definition 'tpu-set-cursor-bound
479 'tpu-set-cursor-free
480 GOLD-map)
481 (message "The cursor is now bound to the flow of your text."))
483 ;;; arch-tag: 89676fa4-33ec-48cb-9135-6f3bf230ab1a
484 ;;; tpu-extras.el ends here