Replace `string-to-int' by `string-to-number'.
[bpt/emacs.git] / lisp / emulation / tpu-extras.el
CommitLineData
e8af40ee 1;;; tpu-extras.el --- scroll margins and free cursor mode for TPU-edt
b4196a99 2
1bb45f86 3;; Copyright (C) 1993, 1994, 1995, 2000 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
522f9216 8
75993094
RS
9;; This file is part of GNU Emacs.
10
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.
15
522f9216 16;; GNU Emacs is distributed in the hope that it will be useful,
75993094
RS
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
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
30;; example for your .emacs file.
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
522f9216 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
522f9216 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
522f9216
RS
125 "*If non-nil, in free cursor mode backward-char (left-arrow) works
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
134(defvar tpu-cursor-free nil
135 "If non-nil, let the cursor roam free.")
136
137
138;;; Hooks -- Set cursor free in picture mode.
139;;; Clean up when writing a file from cursor free mode.
140
158a958d 141(add-hook 'picture-mode-hook 'tpu-set-cursor-free)
522f9216
RS
142
143(defun tpu-write-file-hook nil
144 "Eliminate whitespace at ends of lines, if the cursor is free."
918b0076 145 (if (and (buffer-modified-p) tpu-cursor-free) (tpu-trim-line-ends)))
522f9216
RS
146
147(or (memq 'tpu-write-file-hook write-file-hooks)
148 (setq write-file-hooks
149 (cons 'tpu-write-file-hook write-file-hooks)))
150
151
152;;; Utility routines for implementing scroll margins
153
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)))))
159
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))))))
168
169
170;;; Movement by character
171
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)))
176
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)))
188 (t
189 (picture-backward-column num))))
190
191
192;;; Movement by line
193
194(defun tpu-next-line (num)
195 "Move to next line.
196Prefix 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)))
203
204(defun tpu-previous-line (num)
205 "Move to previous line.
206Prefix 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)))
212
213(defun tpu-next-beginning-of-line (num)
214 "Move to beginning of line; if at beginning, move to beginning of next line.
215Accepts a prefix argument for the number of lines to move."
216 (interactive "p")
217 (let ((beg (tpu-current-line)))
218 (backward-char 1)
1bb45f86 219 (forward-visible-line (- 1 num))
522f9216
RS
220 (tpu-top-check beg num)))
221
222(defun tpu-next-end-of-line (num)
223 "Move to end of line; if at end, move to end of next line.
224Accepts 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)))))
232 (t
233 (forward-char)
234 (end-of-line num)))
235 (tpu-bottom-check beg num)))
236
237(defun tpu-previous-end-of-line (num)
238 "Move EOL upward.
239Accepts 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)))
244 (t
245 (end-of-line (- 1 num))))
246 (tpu-top-check beg num)))
247
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."))))
254
255(defun tpu-forward-line (num)
256 "Move to beginning of next line.
257Prefix 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)))
263
264(defun tpu-backward-line (num)
265 "Move to beginning of previous line.
266Prefix argument serves as repeat count."
267 (interactive "p")
268 (let ((beg (tpu-current-line)))
84299083 269 (or (bolp) (>= 0 num) (setq num (- num 1)))
522f9216
RS
270 (next-line-internal (- num))
271 (tpu-top-check beg num)
272 (beginning-of-line)))
273
274
275;;; Movement by paragraph
276
277(defun tpu-paragraph (num)
278 "Move to the next paragraph in the current direction.
279A 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))))
301 (t
302 (and (> (point) bottom) (recenter bottom-margin)))))
303 (t
304 (tpu-previous-paragraph num)
305 (and (< (point) top) (recenter (min beg top-margin)))))))
306
307
308;;; Movement by page
309
310(defun tpu-page (num)
311 "Move to the next page in the current direction.
312A 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))))
334 (t
335 (and (> (point) bottom) (recenter bottom-margin)))))
336 (t
337 (backward-page num)
338 (and (< (point) top) (recenter (min beg top-margin)))))))
339
340
341;;; Scrolling
342
343(defun tpu-scroll-window-down (num)
344 "Scroll the display down to the next section.
345A 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)))
352
353(defun tpu-scroll-window-up (num)
354 "Scroll the display up to the next section.
355A 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)))
362
363
364;;; Replace the TPU-edt internal search function
365
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)
b4196a99 383 (if tpu-searching-forward
522f9216
RS
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))))
388 (t
389 (and (> (point) bottom) (recenter bottom-margin))))
390 (and (< (point) top) (recenter (min beg top-margin))))))
391
392
393
394;;; Replace the newline, newline-and-indent, and do-auto-fill functions
395
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)))
402
403(defun newline (&optional num)
404 "Insert a newline. With arg, insert that many newlines.
405In Auto Fill mode, can break the preceding line if no numeric arg.
406This 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)))
412
413(defun newline-and-indent nil
414 "Insert a newline, then indent according to major mode.
415Indentation is done using the current indent-line-function.
416In programming language modes, this is the same as TAB.
417In some text modes, where TAB inserts a tab, this indents
418to the specified left-margin column. This is the TPU-edt
419version 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)))
424
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)))
430
431
432;;; Function to set scroll margins
433
83ba7a93 434;;;###autoload
522f9216
RS
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))
027a4b6b 443 (setq tpu-top-scroll-margin (string-to-number top))
522f9216 444 (setq tpu-top-scroll-margin
027a4b6b 445 (/ (1- (+ (* (string-to-number top) 100) (window-height)))
522f9216
RS
446 (window-height)))))
447 ;; set bottom scroll margin
448 (or (string= bottom "")
449 (if (string= "%" (substring bottom -1))
027a4b6b 450 (setq tpu-bottom-scroll-margin (string-to-number bottom))
522f9216 451 (setq tpu-bottom-scroll-margin
027a4b6b 452 (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
522f9216
RS
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)))
458
522f9216
RS
459
460;;; Functions to set cursor bound or free
461
83ba7a93 462;;;###autoload
522f9216
RS
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."))
471
83ba7a93 472;;;###autoload
522f9216
RS
473(defun tpu-set-cursor-bound nil
474 "Constrain the cursor to the flow of the text."
475 (interactive)
918b0076 476 (tpu-trim-line-ends)
522f9216
RS
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."))
482
ab5796a9 483;;; arch-tag: 89676fa4-33ec-48cb-9135-6f3bf230ab1a
522f9216 484;;; tpu-extras.el ends here