Flymake fix to handle more C++ filenames (Bug#5488).
[emacs.git] / lisp / emulation / tpu-extras.el
blob2fc9ce516f521a8ee8967fa4983c1fb43197cd3b
1 ;;; tpu-extras.el --- scroll margins and free cursor mode for TPU-edt
3 ;; Copyright (C) 1993, 1994, 1995, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Rob Riepel <riepel@networking.stanford.edu>
7 ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
8 ;; Keywords: emulations
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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
29 ;; example for your .emacs file.
31 ;; (tpu-set-cursor-free) ; Set cursor free.
32 ;; (tpu-set-scroll-margins "10%" "15%") ; Set scroll margins.
34 ;; Scroll margins and cursor binding can be changed from within emacs using
35 ;; the following commands:
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
41 ;; Additionally, Gold-F toggles between bound and free cursor modes.
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.
47 ;;; Details:
49 ;; The functions contained in this file implement scroll margins and free
50 ;; cursor mode. The following keys and commands are affected.
52 ;; key/command function scroll cursor
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
72 ;; These functions are not part of the base TPU-edt for the following
73 ;; reasons:
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.
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.
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
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.
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.
102 ;;; Code:
105 ;;; Gotta have tpu-edt
107 (require 'tpu-edt)
110 ;;; Customization variables
112 (defcustom 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 :type 'integer
116 :group 'tpu)
117 (defcustom tpu-bottom-scroll-margin 0
118 "Scroll margin at the bottom of the screen.
119 Interpreted as a percent of the current window size."
120 :type 'integer
121 :group 'tpu)
123 (defcustom tpu-backward-char-like-tpu t
124 "If non-nil, in free cursor mode backward-char (left-arrow) works
125 just like TPU/edt. Otherwise, backward-char will move to the end of
126 the previous line when starting from a line beginning."
127 :type 'boolean
128 :group 'tpu)
131 ;;; Global variables
133 ;;;###autoload
134 (define-minor-mode tpu-cursor-free-mode
135 "Minor mode to allow the cursor to move freely about the screen."
136 :init-value nil
137 (if (not tpu-cursor-free-mode)
138 (tpu-trim-line-ends))
139 (if (not tpu-cursor-free-mode)
140 (message "The cursor is now bound to the flow of your text.")
141 (message "The cursor will now move freely about the screen.")))
144 ;;; Hooks -- Set cursor free in picture mode.
145 ;;; Clean up when writing a file from cursor free mode.
147 (add-hook 'picture-mode-hook 'tpu-set-cursor-free)
149 (defun tpu-trim-line-ends-if-needed ()
150 "Eliminate whitespace at ends of lines, if the cursor is free."
151 (if (and (buffer-modified-p) tpu-cursor-free-mode) (tpu-trim-line-ends)))
152 (add-hook 'before-save-hook 'tpu-trim-line-ends-if-needed)
155 ;;; Utility routines for implementing scroll margins
157 (defun tpu-top-check (beg lines)
158 "Enforce scroll margin at the top of screen."
159 (let ((margin (/ (* (window-height) tpu-top-scroll-margin) 100)))
160 (cond ((< beg margin) (recenter beg))
161 ((< (- beg lines) margin) (recenter margin)))))
163 (defun tpu-bottom-check (beg lines)
164 "Enforce scroll margin at the bottom of screen."
165 (let* ((height (window-height))
166 (margin (+ 1 (/ (* height tpu-bottom-scroll-margin) 100)))
167 ;; subtract 1 from height because it includes mode line
168 (difference (- height margin 1)))
169 (cond ((> beg difference) (recenter beg))
170 ((> (+ beg lines) difference) (recenter (- margin))))))
173 ;;; Movement by character
175 (defun tpu-forward-char (num)
176 "Move right ARG characters (left if ARG is negative)."
177 (interactive "p")
178 (if tpu-cursor-free-mode (picture-forward-column num) (forward-char num)))
180 (defun tpu-backward-char (num)
181 "Move left ARG characters (right if ARG is negative)."
182 (interactive "p")
183 (cond ((not tpu-cursor-free-mode)
184 (backward-char num))
185 (tpu-backward-char-like-tpu
186 (picture-backward-column num))
187 ((bolp)
188 (backward-char 1)
189 (picture-end-of-line)
190 (picture-backward-column (1- num)))
192 (picture-backward-column num))))
195 ;;; Movement by line
197 (defun tpu-next-line (num)
198 "Move to next line.
199 Prefix argument serves as a repeat count."
200 (interactive "p")
201 (let ((beg (tpu-current-line)))
202 (if tpu-cursor-free-mode (or (eobp) (picture-move-down num))
203 (line-move num))
204 (tpu-bottom-check beg num)
205 (setq this-command 'next-line)))
207 (defun tpu-previous-line (num)
208 "Move to previous line.
209 Prefix argument serves as a repeat count."
210 (interactive "p")
211 (let ((beg (tpu-current-line)))
212 (if tpu-cursor-free-mode (picture-move-up num) (line-move (- num)))
213 (tpu-top-check beg num)
214 (setq this-command 'previous-line)))
216 (defun tpu-next-beginning-of-line (num)
217 "Move to beginning of line; if at beginning, move to beginning of next line.
218 Accepts a prefix argument for the number of lines to move."
219 (interactive "p")
220 (let ((beg (tpu-current-line)))
221 (backward-char 1)
222 (forward-visible-line (- 1 num))
223 (tpu-top-check beg num)))
225 (defun tpu-next-end-of-line (num)
226 "Move to end of line; if at end, move to end of next line.
227 Accepts a prefix argument for the number of lines to move."
228 (interactive "p")
229 (let ((beg (tpu-current-line)))
230 (cond (tpu-cursor-free-mode
231 (let ((beg (point)))
232 (if (< 1 num) (forward-line num))
233 (picture-end-of-line)
234 (if (<= (point) beg) (progn (forward-line) (picture-end-of-line)))))
236 (forward-char)
237 (end-of-line num)))
238 (tpu-bottom-check beg num)))
240 (defun tpu-previous-end-of-line (num)
241 "Move EOL upward.
242 Accepts a prefix argument for the number of lines to move."
243 (interactive "p")
244 (let ((beg (tpu-current-line)))
245 (cond (tpu-cursor-free-mode
246 (picture-end-of-line (- 1 num)))
248 (end-of-line (- 1 num))))
249 (tpu-top-check beg num)))
251 (defun tpu-current-end-of-line ()
252 "Move point to end of current line."
253 (interactive)
254 (let ((beg (point)))
255 (if tpu-cursor-free-mode (picture-end-of-line) (end-of-line))
256 (if (= beg (point)) (message "You are already at the end of a line."))))
258 (defun tpu-forward-line (num)
259 "Move to beginning of next line.
260 Prefix argument serves as a repeat count."
261 (interactive "p")
262 (let ((beg (tpu-current-line)))
263 (forward-line num)
264 (tpu-bottom-check beg num)))
266 (defun tpu-backward-line (num)
267 "Move to beginning of previous line.
268 Prefix argument serves as repeat count."
269 (interactive "p")
270 (let ((beg (tpu-current-line)))
271 (or (bolp) (>= 0 num) (setq num (- num 1)))
272 (forward-line (- num))
273 (tpu-top-check beg num)))
276 ;;; Movement by paragraph
278 (defun tpu-paragraph (num)
279 "Move to the next paragraph in the current direction.
280 A 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))))
303 (and (> (point) bottom) (recenter bottom-margin)))))
305 (tpu-previous-paragraph num)
306 (and (< (point) top) (recenter (min beg top-margin)))))))
309 ;;; Movement by page
311 (defun tpu-page (num)
312 "Move to the next page in the current direction.
313 A 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))))
336 (and (> (point) bottom) (recenter bottom-margin)))))
338 (backward-page num)
339 (and (< (point) top) (recenter (min beg top-margin)))))))
342 ;;; Scrolling
344 (defun tpu-scroll-window-down (num)
345 "Scroll the display down to the next section.
346 A 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 (line-move (- lines))
352 (tpu-top-check beg lines)))
354 (defun tpu-scroll-window-up (num)
355 "Scroll the display up to the next section.
356 A 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 (line-move lines)
362 (tpu-bottom-check beg lines)))
365 ;;; Replace the TPU-edt internal search function
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))))
390 (and (> (point) bottom) (recenter bottom-margin))))
391 (and (< (point) top) (recenter (min beg top-margin))))))
395 ;; Advise the newline, newline-and-indent, and do-auto-fill functions.
396 (defadvice newline (around tpu-respect-bottom-scroll-margin activate disable)
397 "Respect `tpu-bottom-scroll-margin'."
398 (let ((beg (tpu-current-line))
399 (num (prefix-numeric-value (ad-get-arg 0))))
400 ad-do-it
401 (tpu-bottom-check beg num)))
403 (defadvice newline-and-indent (around tpu-respect-bottom-scroll-margin)
404 "Respect `tpu-bottom-scroll-margin'."
405 (let ((beg (tpu-current-line)))
406 ad-do-it
407 (tpu-bottom-check beg 1)))
409 (defadvice do-auto-fill (around tpu-respect-bottom-scroll-margin)
410 "Respect `tpu-bottom-scroll-margin'."
411 (let ((beg (tpu-current-line)))
412 ad-do-it
413 (tpu-bottom-check beg 1)))
416 ;;; Function to set scroll margins
418 ;;;###autoload
419 (defun tpu-set-scroll-margins (top bottom)
420 "Set scroll margins."
421 (interactive
422 "sEnter top scroll margin (N lines or N%% or RETURN for current value): \
423 \nsEnter bottom scroll margin (N lines or N%% or RETURN for current value): ")
424 ;; set top scroll margin
425 (or (string= top "")
426 (setq tpu-top-scroll-margin
427 (if (string= "%" (substring top -1))
428 (string-to-number top)
429 (/ (1- (+ (* (string-to-number top) 100) (window-height)))
430 (window-height)))))
431 ;; set bottom scroll margin
432 (or (string= bottom "")
433 (setq tpu-bottom-scroll-margin
434 (if (string= "%" (substring bottom -1))
435 (string-to-number bottom)
436 (/ (1- (+ (* (string-to-number bottom) 100) (window-height)))
437 (window-height)))))
438 (dolist (f '(newline newline-and-indent do-auto-fill))
439 (ad-enable-advice f 'around 'tpu-respect-bottom-scroll-margin)
440 (ad-activate f))
441 ;; report scroll margin settings if running interactively
442 (and (called-interactively-p 'interactive)
443 (message "Scroll margins set. Top = %s%%, Bottom = %s%%"
444 tpu-top-scroll-margin tpu-bottom-scroll-margin)))
447 ;;; Functions to set cursor bound or free
449 ;;;###autoload
450 (defun tpu-set-cursor-free ()
451 "Allow the cursor to move freely about the screen."
452 (interactive)
453 (tpu-cursor-free-mode 1))
455 ;;;###autoload
456 (defun tpu-set-cursor-bound ()
457 "Constrain the cursor to the flow of the text."
458 (interactive)
459 (tpu-cursor-free-mode -1))
461 ;; Local Variables:
462 ;; generated-autoload-file: "tpu-edt.el"
463 ;; End:
465 ;; arch-tag: 89676fa4-33ec-48cb-9135-6f3bf230ab1a
466 ;;; tpu-extras.el ends here