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