Commit | Line | Data |
---|---|---|
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 |
522f9216 | 9 | |
75993094 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
ed0f493f | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
75993094 | 13 | ;; it under the terms of the GNU General Public License as published by |
ed0f493f GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
75993094 | 16 | |
522f9216 | 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
75993094 RS |
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 | |
ed0f493f | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
522f9216 | 24 | |
522f9216 RS |
25 | ;;; Commentary: |
26 | ||
84299083 RS |
27 | ;; Use the functions defined here to customize TPU-edt to your tastes by |
28 | ;; setting scroll margins and/or turning on free cursor mode. Here's an | |
865fe16f | 29 | ;; example for your init file. |
84299083 RS |
30 | |
31 | ;; (tpu-set-cursor-free) ; Set cursor free. | |
32 | ;; (tpu-set-scroll-margins "10%" "15%") ; Set scroll margins. | |
33 | ||
34 | ;; Scroll margins and cursor binding can be changed from within emacs using | |
35 | ;; the following commands: | |
36 | ||
37 | ;; tpu-set-scroll-margins or set scroll margins | |
38 | ;; tpu-set-cursor-bound or set cursor bound | |
39 | ;; tpu-set-cursor-free or set cursor free | |
40 | ||
41 | ;; Additionally, Gold-F toggles between bound and free cursor modes. | |
42 | ||
43 | ;; Note that switching out of free cursor mode or exiting TPU-edt while in | |
44 | ;; free cursor mode strips trailing whitespace from every line in the file. | |
45 | ||
46 | ||
47 | ;;; Details: | |
48 | ||
522f9216 RS |
49 | ;; The functions contained in this file implement scroll margins and free |
50 | ;; cursor mode. The following keys and commands are affected. | |
51 | ||
52 | ;; key/command function scroll cursor | |
53 | ||
54 | ;; Up-Arrow previous line x x | |
55 | ;; Down-Arrow next line x x | |
56 | ;; Right-Arrow next character x | |
57 | ;; Left-Arrow previous character x | |
58 | ;; KP0 next or previous line x | |
59 | ;; KP7 next or previous page x | |
60 | ;; KP8 next or previous screen x | |
61 | ;; KP2 next or previous end-of-line x x | |
62 | ;; Control-e current end-of-line x | |
63 | ;; Control-h previous beginning-of-line x | |
64 | ;; Next Scr next screen x | |
65 | ;; Prev Scr previous screen x | |
66 | ;; Search find a string x | |
67 | ;; Replace find and replace a string x | |
68 | ;; Newline insert a newline x | |
69 | ;; Paragraph next or previous paragraph x | |
70 | ;; Auto-Fill break lines on spaces x | |
71 | ||
72 | ;; These functions are not part of the base TPU-edt for the following | |
73 | ;; reasons: | |
74 | ||
75 | ;; Free cursor mode is implemented with the emacs picture-mode functions. | |
76 | ;; These functions support moving the cursor all over the screen, however, | |
77 | ;; when the cursor is moved past the end of a line, spaces or tabs are | |
78 | ;; appended to the line - even if no text is entered in that area. In | |
79 | ;; order for a free cursor mode to work exactly like TPU/edt, this trailing | |
80 | ;; whitespace needs to be dealt with in every function that might encounter | |
81 | ;; it. Such global changes are impractical, however, free cursor mode is | |
82 | ;; too valuable to abandon completely, so it has been implemented in those | |
83 | ;; functions where it serves best. | |
84 | ||
85 | ;; The implementation of scroll margins adds overhead to previously | |
86 | ;; simple and often used commands. These commands are now responsible | |
87 | ;; for their normal operation and part of the display function. There | |
88 | ;; is a possibility that this display overhead could adversely affect the | |
89 | ;; performance of TPU-edt on slower computers. In order to support the | |
90 | ;; widest range of computers, scroll margin support is optional. | |
91 | ||
84299083 RS |
92 | ;; It's actually not known whether the overhead associated with scroll |
93 | ;; margin support is significant. If you find that it is, please send | |
522f9216 RS |
94 | ;; a note describing the extent of the performance degradation. Be sure |
95 | ;; to include a description of the platform where you're running TPU-edt. | |
96 | ;; Send your note to the address provided by Gold-V. | |
97 | ||
98 | ;; Even with these differences and limitations, these functions implement | |
99 | ;; important aspects of the real TPU/edt. Those who miss free cursor mode | |
100 | ;; and/or scroll margins will appreciate these implementations. | |
101 | ||
522f9216 RS |
102 | ;;; Code: |
103 | ||
104 | ||
2582e468 | 105 | ;;; Gotta have tpu-edt |
522f9216 | 106 | |
2582e468 | 107 | (require 'tpu-edt) |
522f9216 RS |
108 | |
109 | ||
110 | ;;; Customization variables | |
111 | ||
786b07e8 | 112 | (defcustom tpu-top-scroll-margin 0 |
944a8b1d | 113 | "Scroll margin at the top of the screen. |
786b07e8 SE |
114 | Interpreted as a percent of the current window size." |
115 | :type 'integer | |
116 | :group 'tpu) | |
117 | (defcustom tpu-bottom-scroll-margin 0 | |
944a8b1d | 118 | "Scroll margin at the bottom of the screen. |
786b07e8 SE |
119 | Interpreted as a percent of the current window size." |
120 | :type 'integer | |
121 | :group 'tpu) | |
522f9216 | 122 | |
786b07e8 | 123 | (defcustom tpu-backward-char-like-tpu t |
944a8b1d | 124 | "If non-nil, in free cursor mode backward-char (left-arrow) works |
522f9216 | 125 | just like TPU/edt. Otherwise, backward-char will move to the end of |
786b07e8 SE |
126 | the previous line when starting from a line beginning." |
127 | :type 'boolean | |
128 | :group 'tpu) | |
522f9216 RS |
129 | |
130 | ||
131 | ;;; Global variables | |
132 | ||
944a8b1d SM |
133 | ;;;###autoload |
134 | (define-minor-mode tpu-cursor-free-mode | |
e1ac4066 GM |
135 | "Minor mode to allow the cursor to move freely about the screen. |
136 | With a prefix argument ARG, enable the mode if ARG is positive, | |
137 | and disable it otherwise. If called from Lisp, enable the mode | |
138 | if ARG is omitted or nil." | |
944a8b1d SM |
139 | :init-value nil |
140 | (if (not tpu-cursor-free-mode) | |
9946be46 SM |
141 | (tpu-trim-line-ends)) |
142 | (if (not tpu-cursor-free-mode) | |
143 | (message "The cursor is now bound to the flow of your text.") | |
144 | (message "The cursor will now move freely about the screen."))) | |
522f9216 RS |
145 | |
146 | ||
147 | ;;; Hooks -- Set cursor free in picture mode. | |
148 | ;;; Clean up when writing a file from cursor free mode. | |
149 | ||
158a958d | 150 | (add-hook 'picture-mode-hook 'tpu-set-cursor-free) |
522f9216 | 151 | |
944a8b1d | 152 | (defun tpu-trim-line-ends-if-needed () |
522f9216 | 153 | "Eliminate whitespace at ends of lines, if the cursor is free." |
944a8b1d SM |
154 | (if (and (buffer-modified-p) tpu-cursor-free-mode) (tpu-trim-line-ends))) |
155 | (add-hook 'before-save-hook 'tpu-trim-line-ends-if-needed) | |
522f9216 RS |
156 | |
157 | ||
158 | ;;; Utility routines for implementing scroll margins | |
159 | ||
160 | (defun tpu-top-check (beg lines) | |
161 | "Enforce scroll margin at the top of screen." | |
162 | (let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100))) | |
163 | (cond ((< beg margin) (recenter beg)) | |
164 | ((< (- beg lines) margin) (recenter margin))))) | |
165 | ||
166 | (defun tpu-bottom-check (beg lines) | |
167 | "Enforce scroll margin at the bottom of screen." | |
168 | (let* ((height (window-height)) | |
169 | (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100))) | |
170 | ;; subtract 1 from height because it includes mode line | |
171 | (difference (- height margin 1))) | |
172 | (cond ((> beg difference) (recenter beg)) | |
173 | ((> (+ beg lines) difference) (recenter (- margin)))))) | |
174 | ||
175 | ||
176 | ;;; Movement by character | |
177 | ||
178 | (defun tpu-forward-char (num) | |
179 | "Move right ARG characters (left if ARG is negative)." | |
180 | (interactive "p") | |
944a8b1d | 181 | (if tpu-cursor-free-mode (picture-forward-column num) (forward-char num))) |
522f9216 RS |
182 | |
183 | (defun tpu-backward-char (num) | |
184 | "Move left ARG characters (right if ARG is negative)." | |
185 | (interactive "p") | |
944a8b1d | 186 | (cond ((not tpu-cursor-free-mode) |
522f9216 RS |
187 | (backward-char num)) |
188 | (tpu-backward-char-like-tpu | |
189 | (picture-backward-column num)) | |
190 | ((bolp) | |
191 | (backward-char 1) | |
192 | (picture-end-of-line) | |
193 | (picture-backward-column (1- num))) | |
194 | (t | |
195 | (picture-backward-column num)))) | |
196 | ||
197 | ||
198 | ;;; Movement by line | |
199 | ||
200 | (defun tpu-next-line (num) | |
201 | "Move to next line. | |
202 | Prefix argument serves as a repeat count." | |
203 | (interactive "p") | |
204 | (let ((beg (tpu-current-line))) | |
944a8b1d SM |
205 | (if tpu-cursor-free-mode (or (eobp) (picture-move-down num)) |
206 | (line-move num)) | |
522f9216 RS |
207 | (tpu-bottom-check beg num) |
208 | (setq this-command 'next-line))) | |
209 | ||
210 | (defun tpu-previous-line (num) | |
211 | "Move to previous line. | |
212 | Prefix argument serves as a repeat count." | |
213 | (interactive "p") | |
214 | (let ((beg (tpu-current-line))) | |
944a8b1d | 215 | (if tpu-cursor-free-mode (picture-move-up num) (line-move (- num))) |
522f9216 RS |
216 | (tpu-top-check beg num) |
217 | (setq this-command 'previous-line))) | |
218 | ||
219 | (defun tpu-next-beginning-of-line (num) | |
220 | "Move to beginning of line; if at beginning, move to beginning of next line. | |
221 | Accepts a prefix argument for the number of lines to move." | |
222 | (interactive "p") | |
223 | (let ((beg (tpu-current-line))) | |
224 | (backward-char 1) | |
1bb45f86 | 225 | (forward-visible-line (- 1 num)) |
522f9216 RS |
226 | (tpu-top-check beg num))) |
227 | ||
228 | (defun tpu-next-end-of-line (num) | |
229 | "Move to end of line; if at end, move to end of next line. | |
230 | Accepts a prefix argument for the number of lines to move." | |
231 | (interactive "p") | |
232 | (let ((beg (tpu-current-line))) | |
944a8b1d | 233 | (cond (tpu-cursor-free-mode |
522f9216 RS |
234 | (let ((beg (point))) |
235 | (if (< 1 num) (forward-line num)) | |
236 | (picture-end-of-line) | |
237 | (if (<= (point) beg) (progn (forward-line) (picture-end-of-line))))) | |
238 | (t | |
239 | (forward-char) | |
240 | (end-of-line num))) | |
241 | (tpu-bottom-check beg num))) | |
242 | ||
243 | (defun tpu-previous-end-of-line (num) | |
244 | "Move EOL upward. | |
245 | Accepts a prefix argument for the number of lines to move." | |
246 | (interactive "p") | |
247 | (let ((beg (tpu-current-line))) | |
944a8b1d | 248 | (cond (tpu-cursor-free-mode |
522f9216 RS |
249 | (picture-end-of-line (- 1 num))) |
250 | (t | |
251 | (end-of-line (- 1 num)))) | |
252 | (tpu-top-check beg num))) | |
253 | ||
f2b18979 | 254 | (defun tpu-current-end-of-line () |
522f9216 RS |
255 | "Move point to end of current line." |
256 | (interactive) | |
257 | (let ((beg (point))) | |
944a8b1d | 258 | (if tpu-cursor-free-mode (picture-end-of-line) (end-of-line)) |
522f9216 RS |
259 | (if (= beg (point)) (message "You are already at the end of a line.")))) |
260 | ||
261 | (defun tpu-forward-line (num) | |
262 | "Move to beginning of next line. | |
263 | Prefix argument serves as a repeat count." | |
264 | (interactive "p") | |
265 | (let ((beg (tpu-current-line))) | |
944a8b1d SM |
266 | (forward-line num) |
267 | (tpu-bottom-check beg num))) | |
522f9216 RS |
268 | |
269 | (defun tpu-backward-line (num) | |
270 | "Move to beginning of previous line. | |
271 | Prefix argument serves as repeat count." | |
272 | (interactive "p") | |
273 | (let ((beg (tpu-current-line))) | |
84299083 | 274 | (or (bolp) (>= 0 num) (setq num (- num 1))) |
944a8b1d SM |
275 | (forward-line (- num)) |
276 | (tpu-top-check beg num))) | |
522f9216 RS |
277 | |
278 | ||
279 | ;;; Movement by paragraph | |
280 | ||
f8a09adb GM |
281 | ;; Cf edt-with-position. |
282 | (defmacro tpu-with-position (&rest body) | |
283 | "Execute BODY with some position-related variables bound." | |
284 | `(let* ((left nil) | |
285 | (beg (tpu-current-line)) | |
286 | (height (window-height)) | |
287 | (top-percent | |
288 | (if (zerop tpu-top-scroll-margin) 10 tpu-top-scroll-margin)) | |
289 | (bottom-percent | |
290 | (if (zerop tpu-bottom-scroll-margin) 15 tpu-bottom-scroll-margin)) | |
291 | (top-margin (/ (* height top-percent) 100)) | |
292 | (bottom-up-margin (1+ (/ (* height bottom-percent) 100))) | |
293 | (bottom-margin (max beg (- height bottom-up-margin 1))) | |
294 | (top (save-excursion (move-to-window-line top-margin) (point))) | |
295 | (bottom (save-excursion (move-to-window-line bottom-margin) (point))) | |
296 | (far (save-excursion | |
297 | (goto-char bottom) | |
298 | (point-at-bol (1- height))))) | |
299 | ,@body)) | |
300 | ||
522f9216 RS |
301 | (defun tpu-paragraph (num) |
302 | "Move to the next paragraph in the current direction. | |
303 | A repeat count means move that many paragraphs." | |
304 | (interactive "p") | |
f8a09adb GM |
305 | (tpu-with-position |
306 | (if tpu-advance | |
307 | (progn | |
308 | (tpu-next-paragraph num) | |
309 | (if (> (point) far) | |
310 | (if (zerop (setq left (save-excursion (forward-line height)))) | |
311 | (recenter top-margin) | |
312 | (recenter (- left bottom-up-margin))) | |
313 | (and (> (point) bottom) (recenter bottom-margin)))) | |
314 | (tpu-previous-paragraph num) | |
315 | (and (< (point) top) (recenter (min beg top-margin)))))) | |
522f9216 RS |
316 | |
317 | ;;; Movement by page | |
318 | ||
319 | (defun tpu-page (num) | |
320 | "Move to the next page in the current direction. | |
321 | A repeat count means move that many pages." | |
322 | (interactive "p") | |
f8a09adb GM |
323 | (tpu-with-position |
324 | (if tpu-advance | |
325 | (progn | |
326 | (forward-page num) | |
327 | (if (> (point) far) | |
328 | (if (zerop (setq left (save-excursion (forward-line height)))) | |
329 | (recenter top-margin) | |
330 | (recenter (- left bottom-up-margin))) | |
331 | (and (> (point) bottom) (recenter bottom-margin)))) | |
332 | (backward-page num) | |
333 | (and (< (point) top) (recenter (min beg top-margin)))))) | |
522f9216 RS |
334 | |
335 | ;;; Scrolling | |
336 | ||
337 | (defun tpu-scroll-window-down (num) | |
338 | "Scroll the display down to the next section. | |
339 | A repeat count means scroll that many sections." | |
340 | (interactive "p") | |
341 | (let* ((beg (tpu-current-line)) | |
342 | (height (1- (window-height))) | |
343 | (lines (* num (/ (* height tpu-percent-scroll) 100)))) | |
944a8b1d | 344 | (line-move (- lines)) |
522f9216 RS |
345 | (tpu-top-check beg lines))) |
346 | ||
347 | (defun tpu-scroll-window-up (num) | |
348 | "Scroll the display up to the next section. | |
349 | A repeat count means scroll that many sections." | |
350 | (interactive "p") | |
351 | (let* ((beg (tpu-current-line)) | |
352 | (height (1- (window-height))) | |
353 | (lines (* num (/ (* height tpu-percent-scroll) 100)))) | |
944a8b1d | 354 | (line-move lines) |
522f9216 RS |
355 | (tpu-bottom-check beg lines))) |
356 | ||
357 | ||
358 | ;;; Replace the TPU-edt internal search function | |
359 | ||
360 | (defun tpu-search-internal (pat &optional quiet) | |
361 | "Search for a string or regular expression." | |
f8a09adb GM |
362 | (tpu-with-position |
363 | (tpu-search-internal-core pat quiet) | |
364 | (if tpu-searching-forward | |
365 | (progn | |
366 | (if (> (point) far) | |
367 | (if (zerop (setq left (save-excursion (forward-line height)))) | |
368 | (recenter top-margin) | |
369 | (recenter (- left bottom-up-margin))) | |
370 | (and (> (point) bottom) (recenter bottom-margin)))) | |
371 | (and (< (point) top) (recenter (min beg top-margin)))))) | |
522f9216 | 372 | |
f2b18979 SM |
373 | ;; Advise the newline, newline-and-indent, and do-auto-fill functions. |
374 | (defadvice newline (around tpu-respect-bottom-scroll-margin activate disable) | |
375 | "Respect `tpu-bottom-scroll-margin'." | |
376 | (let ((beg (tpu-current-line)) | |
377 | (num (prefix-numeric-value (ad-get-arg 0)))) | |
378 | ad-do-it | |
522f9216 RS |
379 | (tpu-bottom-check beg num))) |
380 | ||
f2b18979 SM |
381 | (defadvice newline-and-indent (around tpu-respect-bottom-scroll-margin) |
382 | "Respect `tpu-bottom-scroll-margin'." | |
522f9216 | 383 | (let ((beg (tpu-current-line))) |
f2b18979 | 384 | ad-do-it |
522f9216 RS |
385 | (tpu-bottom-check beg 1))) |
386 | ||
f2b18979 SM |
387 | (defadvice do-auto-fill (around tpu-respect-bottom-scroll-margin) |
388 | "Respect `tpu-bottom-scroll-margin'." | |
522f9216 | 389 | (let ((beg (tpu-current-line))) |
f2b18979 | 390 | ad-do-it |
522f9216 RS |
391 | (tpu-bottom-check beg 1))) |
392 | ||
393 | ||
394 | ;;; Function to set scroll margins | |
395 | ||
83ba7a93 | 396 | ;;;###autoload |
522f9216 RS |
397 | (defun tpu-set-scroll-margins (top bottom) |
398 | "Set scroll margins." | |
399 | (interactive | |
400 | "sEnter top scroll margin (N lines or N%% or RETURN for current value): \ | |
401 | \nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ") | |
402 | ;; set top scroll margin | |
403 | (or (string= top "") | |
f2b18979 SM |
404 | (setq tpu-top-scroll-margin |
405 | (if (string= "%" (substring top -1)) | |
406 | (string-to-number top) | |
027a4b6b | 407 | (/ (1- (+ (* (string-to-number top) 100) (window-height))) |
522f9216 RS |
408 | (window-height))))) |
409 | ;; set bottom scroll margin | |
410 | (or (string= bottom "") | |
f2b18979 SM |
411 | (setq tpu-bottom-scroll-margin |
412 | (if (string= "%" (substring bottom -1)) | |
413 | (string-to-number bottom) | |
027a4b6b | 414 | (/ (1- (+ (* (string-to-number bottom) 100) (window-height))) |
522f9216 | 415 | (window-height))))) |
f2b18979 SM |
416 | (dolist (f '(newline newline-and-indent do-auto-fill)) |
417 | (ad-enable-advice f 'around 'tpu-respect-bottom-scroll-margin) | |
418 | (ad-activate f)) | |
522f9216 | 419 | ;; report scroll margin settings if running interactively |
32226619 | 420 | (and (called-interactively-p 'interactive) |
522f9216 RS |
421 | (message "Scroll margins set. Top = %s%%, Bottom = %s%%" |
422 | tpu-top-scroll-margin tpu-bottom-scroll-margin))) | |
423 | ||
522f9216 RS |
424 | |
425 | ;;; Functions to set cursor bound or free | |
426 | ||
83ba7a93 | 427 | ;;;###autoload |
f2b18979 | 428 | (defun tpu-set-cursor-free () |
522f9216 RS |
429 | "Allow the cursor to move freely about the screen." |
430 | (interactive) | |
9946be46 | 431 | (tpu-cursor-free-mode 1)) |
522f9216 | 432 | |
83ba7a93 | 433 | ;;;###autoload |
f2b18979 | 434 | (defun tpu-set-cursor-bound () |
522f9216 RS |
435 | "Constrain the cursor to the flow of the text." |
436 | (interactive) | |
9946be46 | 437 | (tpu-cursor-free-mode -1)) |
522f9216 | 438 | |
36f84c37 GM |
439 | (provide 'tpu-extras) |
440 | ||
944a8b1d SM |
441 | ;; Local Variables: |
442 | ;; generated-autoload-file: "tpu-edt.el" | |
443 | ;; End: | |
444 | ||
522f9216 | 445 | ;;; tpu-extras.el ends here |