declare smobs in alloc.c
[bpt/emacs.git] / lisp / obsolete / tpu-extras.el
CommitLineData
e8af40ee 1;;; tpu-extras.el --- scroll margins and free cursor mode for TPU-edt
b4196a99 2
ba318903 3;; Copyright (C) 1993-1995, 2000-2014 Free Software Foundation, Inc.
522f9216
RS
4
5;; Author: Rob Riepel <riepel@networking.stanford.edu>
6;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
b7f66977 7;; Keywords: emulations
bd78fa1d 8;; Package: tpu-edt
4982861a 9;; Obsolete-since: 24.5
522f9216 10
75993094
RS
11;; This file is part of GNU Emacs.
12
ed0f493f 13;; GNU Emacs is free software: you can redistribute it and/or modify
75993094 14;; it under the terms of the GNU General Public License as published by
ed0f493f
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
75993094 17
522f9216 18;; GNU Emacs is distributed in the hope that it will be useful,
75993094
RS
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
ed0f493f 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
522f9216 25
522f9216
RS
26;;; Commentary:
27
84299083
RS
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
865fe16f 30;; example for your init file.
84299083
RS
31
32;; (tpu-set-cursor-free) ; Set cursor free.
33;; (tpu-set-scroll-margins "10%" "15%") ; Set scroll margins.
34
35;; Scroll margins and cursor binding can be changed from within emacs using
36;; the following commands:
37
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
41
42;; Additionally, Gold-F toggles between bound and free cursor modes.
43
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.
46
47
48;;; Details:
49
522f9216
RS
50;; The functions contained in this file implement scroll margins and free
51;; cursor mode. The following keys and commands are affected.
52
53;; key/command function scroll cursor
54
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
72
73;; These functions are not part of the base TPU-edt for the following
74;; reasons:
75
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.
85
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.
92
84299083
RS
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
522f9216
RS
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.
98
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.
102
522f9216
RS
103;;; Code:
104
105
2582e468 106;;; Gotta have tpu-edt
522f9216 107
2582e468 108(require 'tpu-edt)
522f9216
RS
109
110
111;;; Customization variables
112
786b07e8 113(defcustom tpu-top-scroll-margin 0
944a8b1d 114 "Scroll margin at the top of the screen.
786b07e8
SE
115Interpreted as a percent of the current window size."
116 :type 'integer
117 :group 'tpu)
118(defcustom tpu-bottom-scroll-margin 0
944a8b1d 119 "Scroll margin at the bottom of the screen.
786b07e8
SE
120Interpreted as a percent of the current window size."
121 :type 'integer
122 :group 'tpu)
522f9216 123
786b07e8 124(defcustom tpu-backward-char-like-tpu t
944a8b1d 125 "If non-nil, in free cursor mode backward-char (left-arrow) works
522f9216 126just like TPU/edt. Otherwise, backward-char will move to the end of
786b07e8
SE
127the previous line when starting from a line beginning."
128 :type 'boolean
129 :group 'tpu)
522f9216
RS
130
131
132;;; Global variables
133
944a8b1d
SM
134;;;###autoload
135(define-minor-mode tpu-cursor-free-mode
e1ac4066
GM
136 "Minor mode to allow the cursor to move freely about the screen.
137With a prefix argument ARG, enable the mode if ARG is positive,
138and disable it otherwise. If called from Lisp, enable the mode
139if ARG is omitted or nil."
944a8b1d
SM
140 :init-value nil
141 (if (not tpu-cursor-free-mode)
9946be46
SM
142 (tpu-trim-line-ends))
143 (if (not tpu-cursor-free-mode)
144 (message "The cursor is now bound to the flow of your text.")
145 (message "The cursor will now move freely about the screen.")))
522f9216
RS
146
147
148;;; Hooks -- Set cursor free in picture mode.
149;;; Clean up when writing a file from cursor free mode.
150
158a958d 151(add-hook 'picture-mode-hook 'tpu-set-cursor-free)
522f9216 152
944a8b1d 153(defun tpu-trim-line-ends-if-needed ()
522f9216 154 "Eliminate whitespace at ends of lines, if the cursor is free."
944a8b1d
SM
155 (if (and (buffer-modified-p) tpu-cursor-free-mode) (tpu-trim-line-ends)))
156(add-hook 'before-save-hook 'tpu-trim-line-ends-if-needed)
522f9216
RS
157
158
159;;; Utility routines for implementing scroll margins
160
161(defun tpu-top-check (beg lines)
162 "Enforce scroll margin at the top of screen."
163 (let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100)))
164 (cond ((< beg margin) (recenter beg))
165 ((< (- beg lines) margin) (recenter margin)))))
166
167(defun tpu-bottom-check (beg lines)
168 "Enforce scroll margin at the bottom of screen."
169 (let* ((height (window-height))
170 (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
171 ;; subtract 1 from height because it includes mode line
172 (difference (- height margin 1)))
173 (cond ((> beg difference) (recenter beg))
174 ((> (+ beg lines) difference) (recenter (- margin))))))
175
176
177;;; Movement by character
178
179(defun tpu-forward-char (num)
180 "Move right ARG characters (left if ARG is negative)."
181 (interactive "p")
944a8b1d 182 (if tpu-cursor-free-mode (picture-forward-column num) (forward-char num)))
522f9216
RS
183
184(defun tpu-backward-char (num)
185 "Move left ARG characters (right if ARG is negative)."
186 (interactive "p")
944a8b1d 187 (cond ((not tpu-cursor-free-mode)
522f9216
RS
188 (backward-char num))
189 (tpu-backward-char-like-tpu
190 (picture-backward-column num))
191 ((bolp)
192 (backward-char 1)
193 (picture-end-of-line)
194 (picture-backward-column (1- num)))
195 (t
196 (picture-backward-column num))))
197
198
199;;; Movement by line
200
201(defun tpu-next-line (num)
202 "Move to next line.
203Prefix argument serves as a repeat count."
204 (interactive "p")
205 (let ((beg (tpu-current-line)))
944a8b1d
SM
206 (if tpu-cursor-free-mode (or (eobp) (picture-move-down num))
207 (line-move num))
522f9216
RS
208 (tpu-bottom-check beg num)
209 (setq this-command 'next-line)))
210
211(defun tpu-previous-line (num)
212 "Move to previous line.
213Prefix argument serves as a repeat count."
214 (interactive "p")
215 (let ((beg (tpu-current-line)))
944a8b1d 216 (if tpu-cursor-free-mode (picture-move-up num) (line-move (- num)))
522f9216
RS
217 (tpu-top-check beg num)
218 (setq this-command 'previous-line)))
219
220(defun tpu-next-beginning-of-line (num)
221 "Move to beginning of line; if at beginning, move to beginning of next line.
222Accepts a prefix argument for the number of lines to move."
223 (interactive "p")
224 (let ((beg (tpu-current-line)))
225 (backward-char 1)
1bb45f86 226 (forward-visible-line (- 1 num))
522f9216
RS
227 (tpu-top-check beg num)))
228
229(defun tpu-next-end-of-line (num)
230 "Move to end of line; if at end, move to end of next line.
231Accepts a prefix argument for the number of lines to move."
232 (interactive "p")
233 (let ((beg (tpu-current-line)))
944a8b1d 234 (cond (tpu-cursor-free-mode
522f9216
RS
235 (let ((beg (point)))
236 (if (< 1 num) (forward-line num))
237 (picture-end-of-line)
238 (if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
239 (t
240 (forward-char)
241 (end-of-line num)))
242 (tpu-bottom-check beg num)))
243
244(defun tpu-previous-end-of-line (num)
245 "Move EOL upward.
246Accepts a prefix argument for the number of lines to move."
247 (interactive "p")
248 (let ((beg (tpu-current-line)))
944a8b1d 249 (cond (tpu-cursor-free-mode
522f9216
RS
250 (picture-end-of-line (- 1 num)))
251 (t
252 (end-of-line (- 1 num))))
253 (tpu-top-check beg num)))
254
f2b18979 255(defun tpu-current-end-of-line ()
522f9216
RS
256 "Move point to end of current line."
257 (interactive)
258 (let ((beg (point)))
944a8b1d 259 (if tpu-cursor-free-mode (picture-end-of-line) (end-of-line))
522f9216
RS
260 (if (= beg (point)) (message "You are already at the end of a line."))))
261
262(defun tpu-forward-line (num)
263 "Move to beginning of next line.
264Prefix argument serves as a repeat count."
265 (interactive "p")
266 (let ((beg (tpu-current-line)))
944a8b1d
SM
267 (forward-line num)
268 (tpu-bottom-check beg num)))
522f9216
RS
269
270(defun tpu-backward-line (num)
271 "Move to beginning of previous line.
272Prefix argument serves as repeat count."
273 (interactive "p")
274 (let ((beg (tpu-current-line)))
84299083 275 (or (bolp) (>= 0 num) (setq num (- num 1)))
944a8b1d
SM
276 (forward-line (- num))
277 (tpu-top-check beg num)))
522f9216
RS
278
279
280;;; Movement by paragraph
281
f8a09adb
GM
282;; Cf edt-with-position.
283(defmacro tpu-with-position (&rest body)
284 "Execute BODY with some position-related variables bound."
285 `(let* ((left nil)
286 (beg (tpu-current-line))
287 (height (window-height))
288 (top-percent
289 (if (zerop tpu-top-scroll-margin) 10 tpu-top-scroll-margin))
290 (bottom-percent
291 (if (zerop tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin))
292 (top-margin (/ (* height top-percent) 100))
293 (bottom-up-margin (1+ (/ (* height bottom-percent) 100)))
294 (bottom-margin (max beg (- height bottom-up-margin 1)))
295 (top (save-excursion (move-to-window-line top-margin) (point)))
296 (bottom (save-excursion (move-to-window-line bottom-margin) (point)))
297 (far (save-excursion
298 (goto-char bottom)
299 (point-at-bol (1- height)))))
300 ,@body))
301
522f9216
RS
302(defun tpu-paragraph (num)
303 "Move to the next paragraph in the current direction.
304A repeat count means move that many paragraphs."
305 (interactive "p")
f8a09adb
GM
306 (tpu-with-position
307 (if tpu-advance
308 (progn
309 (tpu-next-paragraph num)
310 (if (> (point) far)
311 (if (zerop (setq left (save-excursion (forward-line height))))
312 (recenter top-margin)
313 (recenter (- left bottom-up-margin)))
314 (and (> (point) bottom) (recenter bottom-margin))))
315 (tpu-previous-paragraph num)
316 (and (< (point) top) (recenter (min beg top-margin))))))
522f9216
RS
317
318;;; Movement by page
319
320(defun tpu-page (num)
321 "Move to the next page in the current direction.
322A repeat count means move that many pages."
323 (interactive "p")
f8a09adb
GM
324 (tpu-with-position
325 (if tpu-advance
326 (progn
327 (forward-page num)
328 (if (> (point) far)
329 (if (zerop (setq left (save-excursion (forward-line height))))
330 (recenter top-margin)
331 (recenter (- left bottom-up-margin)))
332 (and (> (point) bottom) (recenter bottom-margin))))
333 (backward-page num)
334 (and (< (point) top) (recenter (min beg top-margin))))))
522f9216
RS
335
336;;; Scrolling
337
338(defun tpu-scroll-window-down (num)
339 "Scroll the display down to the next section.
340A repeat count means scroll that many sections."
341 (interactive "p")
342 (let* ((beg (tpu-current-line))
343 (height (1- (window-height)))
344 (lines (* num (/ (* height tpu-percent-scroll) 100))))
944a8b1d 345 (line-move (- lines))
522f9216
RS
346 (tpu-top-check beg lines)))
347
348(defun tpu-scroll-window-up (num)
349 "Scroll the display up to the next section.
350A repeat count means scroll that many sections."
351 (interactive "p")
352 (let* ((beg (tpu-current-line))
353 (height (1- (window-height)))
354 (lines (* num (/ (* height tpu-percent-scroll) 100))))
944a8b1d 355 (line-move lines)
522f9216
RS
356 (tpu-bottom-check beg lines)))
357
358
359;;; Replace the TPU-edt internal search function
360
361(defun tpu-search-internal (pat &optional quiet)
362 "Search for a string or regular expression."
f8a09adb
GM
363 (tpu-with-position
364 (tpu-search-internal-core pat quiet)
365 (if tpu-searching-forward
366 (progn
367 (if (> (point) far)
368 (if (zerop (setq left (save-excursion (forward-line height))))
369 (recenter top-margin)
370 (recenter (- left bottom-up-margin)))
371 (and (> (point) bottom) (recenter bottom-margin))))
372 (and (< (point) top) (recenter (min beg top-margin))))))
522f9216 373
f2b18979
SM
374;; Advise the newline, newline-and-indent, and do-auto-fill functions.
375(defadvice newline (around tpu-respect-bottom-scroll-margin activate disable)
376 "Respect `tpu-bottom-scroll-margin'."
377 (let ((beg (tpu-current-line))
378 (num (prefix-numeric-value (ad-get-arg 0))))
379 ad-do-it
522f9216
RS
380 (tpu-bottom-check beg num)))
381
f2b18979
SM
382(defadvice newline-and-indent (around tpu-respect-bottom-scroll-margin)
383 "Respect `tpu-bottom-scroll-margin'."
522f9216 384 (let ((beg (tpu-current-line)))
f2b18979 385 ad-do-it
522f9216
RS
386 (tpu-bottom-check beg 1)))
387
f2b18979
SM
388(defadvice do-auto-fill (around tpu-respect-bottom-scroll-margin)
389 "Respect `tpu-bottom-scroll-margin'."
522f9216 390 (let ((beg (tpu-current-line)))
f2b18979 391 ad-do-it
522f9216
RS
392 (tpu-bottom-check beg 1)))
393
394
395;;; Function to set scroll margins
396
83ba7a93 397;;;###autoload
522f9216
RS
398(defun tpu-set-scroll-margins (top bottom)
399 "Set scroll margins."
400 (interactive
401 "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
402\nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
403 ;; set top scroll margin
404 (or (string= top "")
f2b18979
SM
405 (setq tpu-top-scroll-margin
406 (if (string= "%" (substring top -1))
407 (string-to-number top)
027a4b6b 408 (/ (1- (+ (* (string-to-number top) 100) (window-height)))
522f9216
RS
409 (window-height)))))
410 ;; set bottom scroll margin
411 (or (string= bottom "")
f2b18979
SM
412 (setq tpu-bottom-scroll-margin
413 (if (string= "%" (substring bottom -1))
414 (string-to-number bottom)
027a4b6b 415 (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
522f9216 416 (window-height)))))
f2b18979
SM
417 (dolist (f '(newline newline-and-indent do-auto-fill))
418 (ad-enable-advice f 'around 'tpu-respect-bottom-scroll-margin)
419 (ad-activate f))
522f9216 420 ;; report scroll margin settings if running interactively
32226619 421 (and (called-interactively-p 'interactive)
522f9216
RS
422 (message "Scroll margins set. Top = %s%%, Bottom = %s%%"
423 tpu-top-scroll-margin tpu-bottom-scroll-margin)))
424
522f9216
RS
425
426;;; Functions to set cursor bound or free
427
83ba7a93 428;;;###autoload
f2b18979 429(defun tpu-set-cursor-free ()
522f9216
RS
430 "Allow the cursor to move freely about the screen."
431 (interactive)
9946be46 432 (tpu-cursor-free-mode 1))
522f9216 433
83ba7a93 434;;;###autoload
f2b18979 435(defun tpu-set-cursor-bound ()
522f9216
RS
436 "Constrain the cursor to the flow of the text."
437 (interactive)
9946be46 438 (tpu-cursor-free-mode -1))
522f9216 439
36f84c37
GM
440(provide 'tpu-extras)
441
944a8b1d
SM
442;; Local Variables:
443;; generated-autoload-file: "tpu-edt.el"
444;; End:
445
522f9216 446;;; tpu-extras.el ends here