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