]> code.delx.au - gnu-emacs/blob - lisp/follow.el
Fix Follow mode's calculation of window ends.
[gnu-emacs] / lisp / follow.el
1 ;;; follow.el --- synchronize windows showing the same buffer
2
3 ;; Copyright (C) 1995-1997, 1999, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Maintainer: FSF (Anders' email bounces, Sep 2005)
7 ;; Created: 1995-05-25
8 ;; Keywords: display, window, minor-mode, convenience
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
27 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
28 ;; combines windows into one tall virtual window.
29 ;;
30 ;; The feeling of a "virtual window" has been accomplished by the use
31 ;; of two major techniques:
32 ;;
33 ;; * The windows always display adjacent sections of the buffer.
34 ;; This means that whenever one window is moved, all the
35 ;; others will follow. (Hence the name Follow mode.)
36 ;;
37 ;; * Should the point (cursor) end up outside a window, another
38 ;; window displaying that point is selected, if possible. This
39 ;; makes it possible to walk between windows using normal cursor
40 ;; movement commands.
41 ;;
42 ;; Follow mode comes to its prime when a large screen and two
43 ;; side-by-side window are used. The user can, with the help of Follow
44 ;; mode, use two full-height windows as though they are one.
45 ;; Imagine yourself editing a large function, or section of text,
46 ;; and being able to use 144 lines instead of the normal 72... (your
47 ;; mileage may vary).
48
49 ;; To test this package, make sure `follow' is loaded, or will be
50 ;; autoloaded when activated (see below). Then do the following:
51 ;;
52 ;; * Find your favorite file (preferably a long one).
53 ;;
54 ;; * Resize Emacs so that it will be wide enough for two full size
55 ;; columns. Delete the other windows and split the window with
56 ;; the commands `C-x 1 C-x 3'.
57 ;;
58 ;; * Give the command:
59 ;; M-x follow-mode <RETURN>
60 ;;
61 ;; * Now the display should look something like (assuming the text "71"
62 ;; is on line 71):
63 ;;
64 ;; +----------+----------+
65 ;; |1 |73 |
66 ;; |2 |74 |
67 ;; |3 |75 |
68 ;; ... ...
69 ;; |71 |143 |
70 ;; |72 |144 |
71 ;; +----------+----------+
72 ;;
73 ;; As you can see, the right-hand window starts at line 73, the line
74 ;; immediately below the end of the left-hand window. As long as
75 ;; `follow-mode' is active, the two windows will follow each other!
76 ;;
77 ;; * Play around and enjoy! Scroll one window and watch the other.
78 ;; Jump to the beginning or end. Press `Cursor down' at the last
79 ;; line of the left-hand window. Enter new lines into the
80 ;; text. Enter long lines spanning several lines, or several
81 ;; windows.
82 ;;
83 ;; * Should you find `Follow' mode annoying, just type
84 ;; M-x follow-mode <RETURN>
85 ;; to turn it off.
86
87
88 ;; The command `follow-delete-other-windows-and-split' maximizes the
89 ;; visible area of the current buffer.
90 ;;
91 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
92 ;; key map. To do so, add the following lines (replacing `[f7]' and
93 ;; `[f8]' with your favorite keys) to the init file:
94 ;;
95 ;; (global-set-key [f8] 'follow-mode)
96 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
97
98
99 ;; There exist two system variables that control the appearance of
100 ;; lines wider than the window containing them. The default is to
101 ;; truncate long lines whenever a window isn't as wide as the frame.
102 ;;
103 ;; To make sure lines are never truncated, please place the following
104 ;; lines in your init file:
105 ;;
106 ;; (setq truncate-lines nil)
107 ;; (setq truncate-partial-width-windows nil)
108
109
110 ;; Since the display of XEmacs is pixel-oriented, a line could be
111 ;; clipped in half at the bottom of the window.
112 ;;
113 ;; To make XEmacs avoid clipping (normal) lines, please place the
114 ;; following line in your init-file:
115 ;;
116 ;; (setq pixel-vertical-clip-threshold 30)
117
118
119 ;; The correct way to configure Follow mode, or any other mode for
120 ;; that matter, is to create one or more functions that do
121 ;; whatever you would like to do. These functions are then added to
122 ;; a hook.
123 ;;
124 ;; When `Follow' mode is activated, functions stored in the hook
125 ;; `follow-mode-hook' are called. When it is deactivated
126 ;; `follow-mode-off-hook' is run.
127 ;;
128 ;; The keymap `follow-key-map' contains key bindings activated by
129 ;; `follow-mode'.
130 ;;
131 ;; Example:
132 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
133 ;;
134 ;; (defun my-follow-mode-hook ()
135 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
136 ;; (define-key follow-mode-map "\C-cb" 'another-function))
137
138
139 ;; Usage:
140 ;;
141 ;; To activate, issue the command "M-x follow-mode"
142 ;; and press Return. To deactivate, do it again.
143 ;;
144 ;; The following is a list of commands useful when follow-mode is active.
145 ;;
146 ;; follow-scroll-up C-c . C-v
147 ;; Scroll text in a Follow mode window chain up.
148 ;;
149 ;; follow-scroll-down C-c . v
150 ;; Like `follow-scroll-up', but in the other direction.
151 ;;
152 ;; follow-delete-other-windows-and-split C-c . 1
153 ;; Maximize the visible area of the current buffer,
154 ;; and enter Follow mode. This is a very convenient
155 ;; way to start Follow mode, hence we recommend that
156 ;; this command be added to the global keymap.
157 ;;
158 ;; follow-recenter C-c . C-l
159 ;; Place the point in the center of the middle window,
160 ;; or a specified number of lines from either top or bottom.
161 ;;
162 ;; follow-switch-to-buffer C-c . b
163 ;; Switch buffer in all windows displaying the current buffer
164 ;; in this frame.
165 ;;
166 ;; follow-switch-to-buffer-all C-c . C-b
167 ;; Switch buffer in all windows in the selected frame.
168 ;;
169 ;; follow-switch-to-current-buffer-all
170 ;; Show the current buffer in all windows on the current
171 ;; frame and turn on `follow-mode'.
172 ;;
173 ;; follow-first-window C-c . <
174 ;; Select the first window in the frame showing the same buffer.
175 ;;
176 ;; follow-last-window C-c . >
177 ;; Select the last window in the frame showing the same buffer.
178 ;;
179 ;; follow-next-window C-c . n
180 ;; Select the next window in the frame showing the same buffer.
181 ;;
182 ;; follow-previous-window C-c . p
183 ;; Select the previous window showing the same buffer.
184
185
186 ;; Well, it seems ok, but what if I really want to look at two different
187 ;; positions in the text? Here are two simple methods to use:
188 ;;
189 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
190 ;; in the same frame. (My apologies to you who can't use frames.)
191 ;;
192 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
193 ;; you want to view two locations. Of course, `follow' mode can
194 ;; be reactivated by hitting the same key again.
195 ;;
196 ;; Example from my ~/.emacs:
197 ;; (global-set-key [f8] 'follow-mode)
198
199
200 ;; Implementation:
201 ;;
202 ;; In an ideal world, follow mode would have been implemented in the
203 ;; kernel of the display routines, making sure that the windows (using
204 ;; follow mode) ALWAYS are aligned. On planet Earth, however, we must
205 ;; accept a solution where we ALMOST ALWAYS can make sure that the
206 ;; windows are aligned.
207 ;;
208 ;; Follow mode does this in three places:
209 ;; 1) After each user command.
210 ;; 2) After a process output has been performed.
211 ;; 3) When a scrollbar has been moved.
212 ;;
213 ;; This will cover most situations. (Let me know if there are other
214 ;; situations that should be covered.)
215 ;;
216 ;; Note that only the selected window is checked, for the reason of
217 ;; efficiency and code complexity. (I.e. it is possible to make a
218 ;; non-selected window unaligned. It will, however, pop right back
219 ;; when it is selected.)
220
221 ;;; Code:
222
223 ;; Preliminaries
224
225 ;; Make the compiler shut up!
226 ;; There are two strategies:
227 ;; 1) Shut warnings off completely.
228 ;; 2) Handle each warning separately.
229 ;;
230 ;; Since I would like to see real errors, I've selected the latter
231 ;; method.
232 ;;
233 ;; The problem with undefined variables and functions has been solved
234 ;; by using `set', `symbol-value' and `symbol-function' rather than
235 ;; `setq' and direct references to variables and functions.
236 ;;
237 ;; For example:
238 ;; (if (boundp 'foo) ... (symbol-value 'foo) )
239 ;; (set 'foo ...) <-- XEmacs doesn't fall for this one.
240 ;; (funcall (symbol-function 'set) 'bar ...)
241 ;;
242 ;; Note: When this file is interpreted, `eval-when-compile' is
243 ;; evaluated. Since it doesn't hurt to evaluate it, but it is a bit
244 ;; annoying, we test if the byte-compiler has been loaded. This can,
245 ;; of course, lead to some occasional unintended evaluation...
246 ;;
247 ;; Should someone come up with a better solution, please let me
248 ;; know.
249
250 (require 'easymenu)
251
252 (eval-when-compile
253 (if (or (featurep 'bytecomp)
254 (featurep 'byte-compile))
255 (cond ((featurep 'xemacs)
256 ;; Make XEmacs shut up! I'm using standard Emacs
257 ;; functions, they are NOT obsolete!
258 (if (eq (get 'force-mode-line-update 'byte-compile)
259 'byte-compile-obsolete)
260 (put 'force-mode-line-update 'byte-compile 'nil))
261 (if (eq (get 'frame-first-window 'byte-compile)
262 'byte-compile-obsolete)
263 (put 'frame-first-window 'byte-compile 'nil))))))
264
265 ;;; Variables
266
267 (defgroup follow nil
268 "Synchronize windows showing the same buffer."
269 :prefix "follow-"
270 :group 'windows
271 :group 'convenience)
272
273 (defcustom follow-mode-hook nil
274 "Normal hook run by `follow-mode'."
275 :type 'hook
276 :group 'follow)
277
278 (defcustom follow-mode-off-hook nil
279 "Hooks to run when Follow mode is turned off."
280 :type 'hook
281 :group 'follow)
282 (make-obsolete-variable 'follow-mode-off-hook 'follow-mode-hook "22.2")
283
284 ;;; Keymap/Menu
285
286 ;; Define keys for the follow-mode minor mode map and replace some
287 ;; functions in the global map. All `follow' mode special functions
288 ;; can be found on (the somewhat cumbersome) "C-c . <key>"
289 ;; (Control-C dot <key>). (As of Emacs 19.29 the keys
290 ;; C-c <punctuation character> are reserved for minor modes.)
291 ;;
292 ;; To change the prefix, redefine `follow-mode-prefix' before
293 ;; `follow' is loaded, or see the section on `follow-mode-hook'
294 ;; above for an example of how to bind the keys the way you like.
295 ;;
296 ;; Please note that the keymap is defined the first time this file is
297 ;; loaded. Also note that the only valid way to manipulate the
298 ;; keymap is to use `define-key'. Don't change it using `setq' or
299 ;; similar!
300
301 (defcustom follow-mode-prefix "\C-c."
302 "Prefix key to use for follow commands in Follow mode.
303 The value of this variable is checked as part of loading Follow mode.
304 After that, changing the prefix key requires manipulating keymaps."
305 :type 'string
306 :group 'follow)
307
308 (defvar follow-mode-map
309 (let ((mainmap (make-sparse-keymap))
310 (map (make-sparse-keymap)))
311 (define-key map "\C-v" 'follow-scroll-up)
312 (define-key map "\M-v" 'follow-scroll-down)
313 (define-key map "v" 'follow-scroll-down)
314 (define-key map "1" 'follow-delete-other-windows-and-split)
315 (define-key map "b" 'follow-switch-to-buffer)
316 (define-key map "\C-b" 'follow-switch-to-buffer-all)
317 (define-key map "\C-l" 'follow-recenter)
318 (define-key map "<" 'follow-first-window)
319 (define-key map ">" 'follow-last-window)
320 (define-key map "n" 'follow-next-window)
321 (define-key map "p" 'follow-previous-window)
322
323 (define-key mainmap follow-mode-prefix map)
324
325 ;; Replace the standard `end-of-buffer', when in Follow mode. (I
326 ;; don't see the point in trying to replace every function that
327 ;; could be enhanced in Follow mode. End-of-buffer is a special
328 ;; case since it is very simple to define and it greatly enhances
329 ;; the look and feel of Follow mode.)
330 (define-key mainmap [remap end-of-buffer] 'follow-end-of-buffer)
331
332 mainmap)
333 "Minor mode keymap for Follow mode.")
334
335 ;; When the mode is not activated, only one item is visible to activate
336 ;; the mode.
337 (defun follow-menu-filter (menu)
338 (if (bound-and-true-p follow-mode)
339 menu
340 '(["Follow mode" follow-mode
341 :style toggle :selected follow-mode])))
342
343 ;; If there is a `tools' menu, we use it. However, we can't add a
344 ;; minor-mode specific item to it (it's broken), so we make the
345 ;; contents ghosted when not in use, and add ourselves to the
346 ;; global map.
347 (easy-menu-add-item nil '("Tools")
348 '("Follow"
349 ;; The Emacs code used to just gray out operations when follow-mode was
350 ;; not enabled, whereas the XEmacs code used to remove it altogether.
351 ;; Not sure which is preferable, but clearly the preference should not
352 ;; depend on the flavor.
353 :filter follow-menu-filter
354 ["Scroll Up" follow-scroll-up follow-mode]
355 ["Scroll Down" follow-scroll-down follow-mode]
356 "--"
357 ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode]
358 "--"
359 ["Switch To Buffer" follow-switch-to-buffer follow-mode]
360 ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode]
361 "--"
362 ["First Window" follow-first-window follow-mode]
363 ["Last Window" follow-last-window follow-mode]
364 ["Next Window" follow-next-window follow-mode]
365 ["Previous Window" follow-previous-window follow-mode]
366 "--"
367 ["Recenter" follow-recenter follow-mode]
368 "--"
369 ["Follow mode" follow-mode :style toggle :selected follow-mode]))
370
371 (defcustom follow-mode-line-text " Follow"
372 "Text shown in the mode line when Follow mode is active.
373 Defaults to \" Follow\". Examples of other values
374 are \" Fw\", or simply \"\"."
375 :type 'string
376 :group 'follow)
377
378 (defcustom follow-auto nil
379 "Non-nil activates Follow mode whenever a file is loaded."
380 :type 'boolean
381 :group 'follow)
382
383 (defcustom follow-intercept-processes (fboundp 'start-process)
384 "When non-nil, Follow mode will monitor process output."
385 :type 'boolean
386 :group 'follow)
387
388 (defvar follow-avoid-tail-recenter-p (not (featurep 'xemacs))
389 "When non-nil, patch Emacs so that tail windows won't be recentered.
390
391 A \"tail window\" is a window that displays only the end of
392 the buffer. Normally it is practical for the user that empty
393 windows are recentered automatically. However, when using
394 Follow mode it breaks the display when the end is displayed
395 in a window \"above\" the last window. This is for
396 example the case when displaying a short page in info.
397
398 Must be set before Follow mode is loaded.
399
400 Please note that it is not possible to fully prevent Emacs from
401 recentering empty windows. Please report if you find a repeatable
402 situation in which Emacs recenters empty windows.
403
404 XEmacs, as of 19.12, does not recenter windows, good!")
405
406 (defvar follow-cache-command-list
407 '(next-line previous-line forward-char backward-char)
408 "List of commands that don't require recalculation.
409
410 In order to be able to use the cache, a command should not change the
411 contents of the buffer, nor should it change selected window or current
412 buffer.
413
414 The commands in this list are checked at load time.
415
416 To mark other commands as suitable for caching, set the symbol
417 property `follow-mode-use-cache' to non-nil.")
418
419 (defvar follow-debug nil
420 "Non-nil when debugging Follow mode.")
421
422
423 ;; Internal variables:
424
425 (defvar follow-internal-force-redisplay nil
426 "True when Follow mode should redisplay the windows.")
427
428 (defvar follow-process-filter-alist '()
429 "The original filters for processes intercepted by Follow mode.")
430
431 (defvar follow-active-menu nil
432 "The menu visible when Follow mode is active.")
433
434 (defvar follow-deactive-menu nil
435 "The menu visible when Follow mode is deactivated.")
436
437 (defvar follow-inside-post-command-hook nil
438 "Non-nil when inside Follow modes `post-command-hook'.
439 Used by `follow-window-size-change'.")
440
441 (defvar follow-windows-start-end-cache nil
442 "Cache used by `follow-window-start-end'.")
443
444 ;;; Debug messages
445
446 ;; This inline function must be as small as possible!
447 ;; Maybe we should define a macro that expands to nil if
448 ;; the variable is not set.
449
450 (defsubst follow-debug-message (&rest args)
451 "Like `message', but only active when `follow-debug' is non-nil."
452 (if (and (boundp 'follow-debug) follow-debug)
453 (apply 'message args)))
454
455 ;;; Cache
456
457 (dolist (cmd follow-cache-command-list)
458 (put cmd 'follow-mode-use-cache t))
459
460 ;;; The mode
461
462 ;;;###autoload
463 (defun turn-on-follow-mode ()
464 "Turn on Follow mode. Please see the function `follow-mode'."
465 (follow-mode 1))
466
467
468 ;;;###autoload
469 (defun turn-off-follow-mode ()
470 "Turn off Follow mode. Please see the function `follow-mode'."
471 (follow-mode -1))
472
473 (put 'follow-mode 'permanent-local t)
474 ;;;###autoload
475 (define-minor-mode follow-mode
476 "Toggle Follow mode.
477 With a prefix argument ARG, enable Follow mode if ARG is
478 positive, and disable it otherwise. If called from Lisp, enable
479 the mode if ARG is omitted or nil.
480
481 Follow mode is a minor mode that combines windows into one tall
482 virtual window. This is accomplished by two main techniques:
483
484 * The windows always displays adjacent sections of the buffer.
485 This means that whenever one window is moved, all the
486 others will follow. (Hence the name Follow mode.)
487
488 * Should the point (cursor) end up outside a window, another
489 window displaying that point is selected, if possible. This
490 makes it possible to walk between windows using normal cursor
491 movement commands.
492
493 Follow mode comes to its prime when used on a large screen and two
494 side-by-side windows are used. The user can, with the help of Follow
495 mode, use two full-height windows as though they would have been
496 one. Imagine yourself editing a large function, or section of text,
497 and being able to use 144 lines instead of the normal 72... (your
498 mileage may vary).
499
500 To split one large window into two side-by-side windows, the commands
501 `\\[split-window-right]' or \
502 `M-x follow-delete-other-windows-and-split' can be used.
503
504 Only windows displayed in the same frame follow each other.
505
506 If the variable `follow-intercept-processes' is non-nil, Follow mode
507 will listen to the output of processes and redisplay accordingly.
508 \(This is the default.)
509
510 This command runs the normal hook `follow-mode-hook'.
511
512 Keys specific to Follow mode:
513 \\{follow-mode-map}"
514 :keymap follow-mode-map
515 (when (and follow-mode follow-intercept-processes)
516 (follow-intercept-process-output))
517 (cond (follow-mode ; On
518 ;; XEmacs: If this is non-nil, the window will scroll before
519 ;; the point will have a chance to get into the next window.
520 (when (boundp 'scroll-on-clipped-lines)
521 (setq scroll-on-clipped-lines nil))
522 (force-mode-line-update)
523 (add-hook 'post-command-hook 'follow-post-command-hook t))
524
525 ((not follow-mode) ; Off
526 (force-mode-line-update))))
527
528 ;;; Find file hook
529
530 ;; This will start follow-mode whenever a new file is loaded, if
531 ;; the variable `follow-auto' is non-nil.
532
533 (add-hook 'find-file-hook 'follow-find-file-hook t)
534
535 (defun follow-find-file-hook ()
536 "Find-file hook for Follow mode. See the variable `follow-auto'."
537 (if follow-auto (follow-mode t)))
538
539 ;;; User functions
540
541 ;;; Scroll
542
543 ;; `scroll-up' and `-down', but for windows in Follow mode.
544 ;;
545 ;; Almost like the real thing, except when the cursor ends up outside
546 ;; the top or bottom... In our case however, we end up outside the
547 ;; window and hence we are recentered. Should we let `recenter' handle
548 ;; the point position we would never leave the selected window. To do
549 ;; it ourselves we would need to do our own redisplay, which is easier
550 ;; said than done. (Why didn't I do a real display abstraction from
551 ;; the beginning?)
552 ;;
553 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
554 ;; our post-command-hook will move our windows back into the old
555 ;; position... (This would also be corrected if we would have had a
556 ;; good redisplay abstraction.)
557
558 (defun follow-scroll-up (&optional arg)
559 "Scroll text in a Follow mode window chain up.
560
561 If called with no ARG, the `next-screen-context-lines' last lines of
562 the bottom window in the chain will be visible in the top window.
563
564 If called with an argument, scroll ARG lines up.
565 Negative ARG means scroll downward.
566
567 Works like `scroll-up' when not in Follow mode."
568 (interactive "P")
569 (cond ((not (and (boundp 'follow-mode) follow-mode))
570 (scroll-up arg))
571 (arg
572 (save-excursion (scroll-up arg))
573 (setq follow-internal-force-redisplay t))
574 (t
575 (let* ((windows (follow-all-followers))
576 (end (window-end (car (reverse windows)))))
577 (if (eq end (point-max))
578 (signal 'end-of-buffer nil)
579 (select-window (car windows))
580 ;; `window-end' might return nil.
581 (if end
582 (goto-char end))
583 (vertical-motion (- next-screen-context-lines))
584 (set-window-start (car windows) (point)))))))
585
586
587 (defun follow-scroll-down (&optional arg)
588 "Scroll text in a Follow mode window chain down.
589
590 If called with no ARG, the `next-screen-context-lines' top lines of
591 the top window in the chain will be visible in the bottom window.
592
593 If called with an argument, scroll ARG lines down.
594 Negative ARG means scroll upward.
595
596 Works like `scroll-up' when not in Follow mode."
597 (interactive "P")
598 (cond ((not (and (boundp 'follow-mode) follow-mode))
599 (scroll-up arg))
600 (arg
601 (save-excursion (scroll-down arg)))
602 (t
603 (let* ((windows (follow-all-followers))
604 (win (car (reverse windows)))
605 (start (window-start (car windows))))
606 (if (eq start (point-min))
607 (signal 'beginning-of-buffer nil)
608 (select-window win)
609 (goto-char start)
610 (vertical-motion (- (- (window-height win)
611 (if header-line-format 2 1)
612 next-screen-context-lines)))
613 (set-window-start win (point))
614 (goto-char start)
615 (vertical-motion (- next-screen-context-lines 1))
616 (setq follow-internal-force-redisplay t))))))
617
618 ;;; Buffer
619
620 ;;;###autoload
621 (defun follow-delete-other-windows-and-split (&optional arg)
622 "Create two side by side windows and enter Follow mode.
623
624 Execute this command to display as much as possible of the text
625 in the selected window. All other windows, in the current
626 frame, are deleted and the selected window is split in two
627 side-by-side windows. Follow mode is activated, hence the
628 two windows always will display two successive pages.
629 \(If one window is moved, the other one will follow.)
630
631 If ARG is positive, the leftmost window is selected. If negative,
632 the rightmost is selected. If ARG is nil, the leftmost window is
633 selected if the original window is the first one in the frame.
634
635 To bind this command to a hotkey, place the following line
636 in your `~/.emacs' file, replacing [f7] by your favorite key:
637 (global-set-key [f7] 'follow-delete-other-windows-and-split)"
638 (interactive "P")
639 (let ((other (or (and (null arg)
640 (not (eq (selected-window)
641 (frame-first-window (selected-frame)))))
642 (and arg
643 (< (prefix-numeric-value arg) 0))))
644 (start (window-start)))
645 (delete-other-windows)
646 (split-window-right)
647 (if other
648 (progn
649 (other-window 1)
650 (set-window-start (selected-window) start)
651 (setq follow-internal-force-redisplay t)))
652 (follow-mode 1)))
653
654 (defun follow-switch-to-buffer (buffer)
655 "Show BUFFER in all windows in the current Follow mode window chain."
656 (interactive "BSwitch to Buffer: ")
657 (let ((orig-window (selected-window))
658 (windows (follow-all-followers)))
659 (while windows
660 (select-window (car windows))
661 (switch-to-buffer buffer)
662 (setq windows (cdr windows)))
663 (select-window orig-window)))
664
665
666 (defun follow-switch-to-buffer-all (&optional buffer)
667 "Show BUFFER in all windows on this frame.
668 Defaults to current buffer."
669 (interactive (list (read-buffer "Switch to Buffer: "
670 (current-buffer))))
671 (or buffer (setq buffer (current-buffer)))
672 (let ((orig-window (selected-window)))
673 (walk-windows
674 (function
675 (lambda (win)
676 (select-window win)
677 (switch-to-buffer buffer))))
678 (select-window orig-window)
679 (follow-redisplay)))
680
681
682 (defun follow-switch-to-current-buffer-all ()
683 "Show current buffer in all windows on this frame, and enter Follow mode.
684
685 To bind this command to a hotkey place the following line
686 in your `~/.emacs' file:
687 (global-set-key [f7] 'follow-switch-to-current-buffer-all)"
688 (interactive)
689 (or (and (boundp 'follow-mode) follow-mode)
690 (follow-mode 1))
691 (follow-switch-to-buffer-all))
692
693 ;;; Movement
694
695 ;; Note, these functions are not very useful, at least not unless you
696 ;; rebind the rather cumbersome key sequence `C-c . p'.
697
698 (defun follow-next-window ()
699 "Select the next window showing the same buffer."
700 (interactive)
701 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
702 (if succ
703 (select-window (car succ))
704 (error "%s" "No more windows"))))
705
706
707 (defun follow-previous-window ()
708 "Select the previous window showing the same buffer."
709 (interactive)
710 (let ((pred (car (follow-split-followers (follow-all-followers)))))
711 (if pred
712 (select-window (car pred))
713 (error "%s" "No more windows"))))
714
715
716 (defun follow-first-window ()
717 "Select the first window in the frame showing the same buffer."
718 (interactive)
719 (select-window (car (follow-all-followers))))
720
721
722 (defun follow-last-window ()
723 "Select the last window in the frame showing the same buffer."
724 (interactive)
725 (select-window (car (reverse (follow-all-followers)))))
726
727 ;;; Redraw
728
729 (defun follow-recenter (&optional arg)
730 "Recenter the middle window around point.
731 Rearrange all other windows around the middle window.
732
733 With a positive argument, place the current line ARG lines
734 from the top. With a negative argument, place it -ARG lines
735 from the bottom."
736 (interactive "P")
737 (if arg
738 (let ((p (point))
739 (arg (prefix-numeric-value arg)))
740 (if (>= arg 0)
741 ;; Recenter relative to the top.
742 (progn
743 (follow-first-window)
744 (goto-char p)
745 (recenter arg))
746 ;; Recenter relative to the bottom.
747 (follow-last-window)
748 (goto-char p)
749 (recenter arg)
750 ;; Otherwise, our post-command-hook will move the window
751 ;; right back.
752 (setq follow-internal-force-redisplay t)))
753 ;; Recenter in the middle.
754 (let* ((dest (point))
755 (windows (follow-all-followers))
756 (win (nth (/ (- (length windows) 1) 2) windows)))
757 (select-window win)
758 (goto-char dest)
759 (recenter)
760 ;;(setq follow-internal-force-redisplay t)
761 )))
762
763
764 (defun follow-redraw ()
765 "Arrange windows displaying the same buffer in successor order.
766 This function can be called even if the buffer is not in Follow mode.
767
768 Hopefully, there should be no reason to call this function when in
769 Follow mode since the windows should always be aligned."
770 (interactive)
771 (sit-for 0)
772 (follow-redisplay))
773
774 ;;; End of buffer
775
776 (defun follow-end-of-buffer (&optional arg)
777 "Move point to the end of the buffer, Follow mode style.
778
779 If the end is not visible, it will be displayed in the last possible
780 window in the Follow mode window chain.
781
782 The mark is left at the previous position. With arg N, put point N/10
783 of the way from the true end."
784 (interactive "P")
785 (let ((followers (follow-all-followers))
786 (pos (point)))
787 (cond (arg
788 (select-window (car (reverse followers))))
789 ((follow-select-if-end-visible
790 (follow-windows-start-end followers)))
791 (t
792 (select-window (car (reverse followers)))))
793 (goto-char pos)
794 (with-no-warnings
795 (end-of-buffer arg))))
796
797 ;;; Display
798
799 (defun follow-all-followers (&optional testwin)
800 "Return all windows displaying the same buffer as the TESTWIN.
801 The list contains only windows displayed in the same frame as TESTWIN.
802 If TESTWIN is nil the selected window is used."
803 (or (window-live-p testwin)
804 (setq testwin (selected-window)))
805 (let* ((top (frame-first-window (window-frame testwin)))
806 (win top)
807 (done nil)
808 (windows '())
809 (buffer (window-buffer testwin)))
810 (while (and (not done) win)
811 (if (eq (window-buffer win) buffer)
812 (setq windows (cons win windows)))
813 (setq win (next-window win 'not))
814 (if (eq win top)
815 (setq done t)))
816 (nreverse windows)))
817
818
819 (defun follow-split-followers (windows &optional win)
820 "Split the WINDOWS into the sets: predecessors and successors.
821 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
822 from the selected window."
823 (or win
824 (setq win (selected-window)))
825 (let ((pred '()))
826 (while (not (eq (car windows) win))
827 (setq pred (cons (car windows) pred))
828 (setq windows (cdr windows)))
829 (cons pred (cdr windows))))
830
831
832 (defun follow-calc-win-end (&optional win)
833 "Calculate the end position for window WIN.
834 Return (END-POS END-OF-BUFFER).
835
836 Actually, the position returned is the start of the line after
837 the last fully-visible line in WIN. If WIN is nil, the selected
838 window is used."
839 (let* ((win (or win (selected-window)))
840 (edges (window-inside-pixel-edges win))
841 (ht (- (nth 3 edges) (nth 1 edges)))
842 (last-line-pos (posn-point (posn-at-x-y 0 (1- ht) win))))
843 (if (pos-visible-in-window-p last-line-pos win)
844 (let ((end (window-end win t)))
845 (list end (= end (point-max))))
846 (list last-line-pos nil))))
847
848 ;; Can't use `save-window-excursion' since it triggers a redraw.
849 (defun follow-calc-win-start (windows pos win)
850 "Calculate where WIN will start if the first in WINDOWS start at POS.
851
852 If WIN is nil the point below all windows is returned."
853 (let (start)
854 (while (and windows (not (eq (car windows) win)))
855 (setq start (window-start (car windows)))
856 (set-window-start (car windows) pos 'noforce)
857 (setq pos (car (follow-calc-win-end (car windows))))
858 (set-window-start (car windows) start 'noforce)
859 (setq windows (cdr windows)))
860 pos))
861
862
863 ;; The result from `follow-windows-start-end' is cached when using
864 ;; a handful simple commands, like cursor movement commands.
865
866 (defsubst follow-cache-valid-p (windows)
867 "Test if the cached value of `follow-windows-start-end' can be used.
868 Note that this handles the case when the cache has been set to nil."
869 (let ((res t)
870 (cache follow-windows-start-end-cache))
871 (while (and res windows cache)
872 (setq res (and (eq (car windows)
873 (car (car cache)))
874 (eq (window-start (car windows))
875 (car (cdr (car cache))))))
876 (setq windows (cdr windows))
877 (setq cache (cdr cache)))
878 (and res (null windows) (null cache))))
879
880
881 (defsubst follow-invalidate-cache ()
882 "Force `follow-windows-start-end' to recalculate the end of the window."
883 (setq follow-windows-start-end-cache nil))
884
885
886 ;; Build a list of windows and their start and end positions.
887 ;; Useful to avoid calculating start/end position whenever they are needed.
888 ;; The list has the format:
889 ;; ((Win Start End End-of-buffer-visible-p) ...)
890
891 ;; Used to have a `save-window-excursion', but it obviously triggered
892 ;; redraws of the display. Check if I used it for anything.
893
894
895 (defun follow-windows-start-end (windows)
896 "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
897 (if (follow-cache-valid-p windows)
898 follow-windows-start-end-cache
899 (let ((orig-win (selected-window))
900 win-start-end)
901 (dolist (w windows)
902 (select-window w)
903 (push (cons w (cons (window-start) (follow-calc-win-end)))
904 win-start-end))
905 (select-window orig-win)
906 (setq follow-windows-start-end-cache (nreverse win-start-end)))))
907
908
909 (defsubst follow-pos-visible (pos win win-start-end)
910 "Non-nil when POS is visible in WIN."
911 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
912 (and (>= pos (car wstart-wend-bend))
913 (or (< pos (cadr wstart-wend-bend))
914 (nth 2 wstart-wend-bend)))))
915
916
917 ;; By `aligned' we mean that for all adjacent windows, the end of the
918 ;; first is equal with the start of the successor. The first window
919 ;; should start at a full screen line.
920
921 (defsubst follow-windows-aligned-p (win-start-end)
922 "Non-nil if the follower windows are aligned."
923 (let ((res t))
924 (save-excursion
925 (goto-char (window-start (caar win-start-end)))
926 (unless (bolp)
927 (vertical-motion 0 (caar win-start-end))
928 (setq res (eq (point) (window-start (caar win-start-end))))))
929 (while (and res (cdr win-start-end))
930 ;; At least two followers left
931 (setq res (eq (car (cdr (cdr (car win-start-end))))
932 (car (cdr (car (cdr win-start-end))))))
933 (setq win-start-end (cdr win-start-end)))
934 res))
935
936
937 ;; Check if the point is visible in all windows. (So that
938 ;; no one will be recentered.)
939
940 (defun follow-point-visible-all-windows-p (win-start-end)
941 "Non-nil when the `window-point' is visible in all windows."
942 (let ((res t))
943 (while (and res win-start-end)
944 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
945 (car (car win-start-end))
946 win-start-end))
947 (setq win-start-end (cdr win-start-end)))
948 res))
949
950
951 ;; Make sure WIN always starts at the beginning of a whole screen
952 ;; line. If WIN is not aligned the start is updated which probably
953 ;; will lead to a redisplay of the screen later on.
954 ;;
955 ;; This is used with the first window in a follow chain. The reason
956 ;; is that we want to detect that the point is outside the window.
957 ;; (Without the update, the start of the window will move as the
958 ;; user presses BackSpace, and the other window redisplay routines
959 ;; will move the start of the window in the wrong direction.)
960
961 (defun follow-update-window-start (win)
962 "Make sure that the start of WIN starts at a full screen line."
963 (save-excursion
964 (goto-char (window-start win))
965 (unless (bolp)
966 (vertical-motion 0 win)
967 (unless (eq (point) (window-start win))
968 (vertical-motion 1 win)
969 (set-window-start win (point) 'noforce)))))
970
971 ;;; Selection functions
972
973 ;; Make a window in WINDOWS selected if it currently
974 ;; is displaying the position DEST.
975 ;;
976 ;; We don't select a window if it just has been moved.
977
978 (defun follow-select-if-visible (dest win-start-end)
979 "Select and return a window, if DEST is visible in it.
980 Return the selected window."
981 (let (win win-end)
982 (while (and (not win) win-start-end)
983 ;; Don't select a window that was just moved. This makes it
984 ;; possible to later select the last window after a `end-of-buffer'
985 ;; command.
986 (when (follow-pos-visible dest (caar win-start-end) win-start-end)
987 (setq win (caar win-start-end)
988 win-end (car (cddr (car win-start-end))))
989 (select-window win))
990 (setq win-start-end (cdr win-start-end)))
991 ;; The last line of the window may be partially visible; if so,
992 ;; and if point is visible in the next window, select the next
993 ;; window instead.
994 (and win
995 (/= dest (point-max))
996 win-start-end
997 (follow-pos-visible dest (caar win-start-end) win-start-end)
998 (save-excursion
999 (goto-char dest)
1000 (vertical-motion 1 win)
1001 (>= (point) win-end))
1002 (setq win (caar win-start-end))
1003 (select-window win))
1004 win))
1005
1006
1007 ;; Lets select a window showing the end. Make sure we only select it if
1008 ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place
1009 ;; the point in the selected window.)
1010 ;;
1011 ;; (Compatibility kludge: in Emacs `window-end' is equal to `point-max';
1012 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1013 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1014
1015 (defun follow-select-if-end-visible (win-start-end)
1016 "Select and return a window, if end is visible in it."
1017 (let ((win nil))
1018 (while (and (not win) win-start-end)
1019 ;; Don't select a window that was just moved. This makes it
1020 ;; possible to later select the last window after a `end-of-buffer'
1021 ;; command.
1022 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1023 (nth 3 (car win-start-end))
1024 ;; `window-end' might return nil.
1025 (let ((end (window-end (car (car win-start-end)))))
1026 (and end
1027 (eq (point-max) (min (point-max) end)))))
1028 (progn
1029 (setq win (car (car win-start-end)))
1030 (select-window win)))
1031 (setq win-start-end (cdr win-start-end)))
1032 win))
1033
1034
1035 ;; Select a window that will display the point if the windows would
1036 ;; be redisplayed with the first window fixed. This is useful for
1037 ;; example when the user has pressed return at the bottom of a window
1038 ;; as the point is not visible in any window.
1039
1040 (defun follow-select-if-visible-from-first (dest windows)
1041 "Try to select one of WINDOWS without repositioning the topmost window.
1042 If one of the windows in WINDOWS contains DEST, select it, call
1043 `follow-redisplay', move point to DEST, and return that window.
1044 Otherwise, return nil."
1045 (let (win end-pos-end-p)
1046 (save-excursion
1047 (goto-char (window-start (car windows)))
1048 ;; Make sure the line start in the beginning of a real screen
1049 ;; line.
1050 (vertical-motion 0 (car windows))
1051 (when (>= dest (point))
1052 ;; At or below the start. Check the windows.
1053 (save-window-excursion
1054 (let ((windows windows))
1055 (while (and (not win) windows)
1056 (set-window-start (car windows) (point) 'noforce)
1057 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1058 (goto-char (car end-pos-end-p))
1059 ;; Visible, if dest above end, or if eob is visible
1060 ;; inside the window.
1061 (if (or (car (cdr end-pos-end-p))
1062 (< dest (point)))
1063 (setq win (car windows))
1064 (setq windows (cdr windows))))))))
1065 (when win
1066 (select-window win)
1067 (follow-redisplay windows (car windows))
1068 (goto-char dest))
1069 win))
1070
1071 ;;; Redisplay
1072
1073 ;; Redraw all the windows on the screen, starting with the top window.
1074 ;; The window used as as marker is WIN, or the selected window if WIN
1075 ;; is nil. Start every window directly after the end of the previous
1076 ;; window, to make sure long lines are displayed correctly.
1077
1078 (defun follow-redisplay (&optional windows win preserve-win)
1079 "Reposition the WINDOWS around WIN.
1080 Should the point be too close to the roof we redisplay everything
1081 from the top. WINDOWS should contain a list of windows to
1082 redisplay; it is assumed that WIN is a member of the list.
1083 Should WINDOWS be nil, the windows displaying the
1084 same buffer as WIN, in the current frame, are used.
1085 Should WIN be nil, the selected window is used.
1086 If PRESERVE-WIN is non-nil, keep WIN itself unchanged while
1087 repositioning the other windows."
1088 (or win (setq win (selected-window)))
1089 (or windows (setq windows (follow-all-followers win)))
1090 ;; Calculate the start of the first window.
1091 (let* ((old-win-start (window-start win))
1092 (try-first-start (follow-estimate-first-window-start
1093 windows win old-win-start))
1094 (try-win-start (follow-calc-win-start
1095 windows try-first-start win))
1096 (start (cond ((= try-win-start old-win-start)
1097 (follow-debug-message "exact")
1098 try-first-start)
1099 ((< try-win-start old-win-start)
1100 (follow-debug-message "above")
1101 (follow-calculate-first-window-start-from-above
1102 windows try-first-start win old-win-start))
1103 (t
1104 (follow-debug-message "below")
1105 (follow-calculate-first-window-start-from-below
1106 windows try-first-start win old-win-start)))))
1107 (dolist (w windows)
1108 (unless (and preserve-win (eq w win))
1109 (set-window-start w start))
1110 (setq start (car (follow-calc-win-end w))))))
1111
1112
1113 (defun follow-estimate-first-window-start (windows win start)
1114 "Estimate the position of the first window.
1115 The estimate is computed by assuming that the window WIN, which
1116 should be a member of WINDOWS, starts at position START."
1117 (let ((windows-before (car (follow-split-followers windows win))))
1118 (save-excursion
1119 (goto-char start)
1120 (vertical-motion 0 win)
1121 (dolist (w windows-before)
1122 (vertical-motion (- 1 (window-text-height w)) w))
1123 (point))))
1124
1125
1126 ;; Find the starting point, start at GUESS and search downward.
1127 ;; The returned point is always a point below GUESS.
1128
1129 (defun follow-calculate-first-window-start-from-above
1130 (windows guess win start)
1131 (save-excursion
1132 (let ((done nil)
1133 win-start
1134 res)
1135 (goto-char guess)
1136 (while (not done)
1137 (if (not (= (vertical-motion 1 (car windows)) 1))
1138 ;; Hit bottom! (Can we really do this?)
1139 ;; We'll keep it, since it ensures termination.
1140 (progn
1141 (setq done t)
1142 (setq res (point-max)))
1143 (setq win-start (follow-calc-win-start windows (point) win))
1144 (if (>= win-start start)
1145 (setq done t res (point)))))
1146 res)))
1147
1148
1149 ;; Find the starting point, start at GUESS and search upward. Return
1150 ;; a point on the same line as GUESS, or above.
1151 ;;
1152 ;; (Is this ever used? I must make sure it works just in case it is
1153 ;; ever called.)
1154
1155 (defun follow-calculate-first-window-start-from-below
1156 (windows guess &optional win start)
1157 (setq win (or win (selected-window)))
1158 (setq start (or start (window-start win)))
1159 (save-excursion
1160 (let (done win-start res opoint)
1161 ;; Always calculate what happens when no line is displayed in the first
1162 ;; window. (The `previous' res is needed below!)
1163 (goto-char guess)
1164 (vertical-motion 0 (car windows))
1165 (setq res (point))
1166 (while (not done)
1167 (setq opoint (point))
1168 (if (not (= (vertical-motion -1 (car windows)) -1))
1169 ;; Hit roof!
1170 (setq done t res (point-min))
1171 (setq win-start (follow-calc-win-start windows (point) win))
1172 (cond ((>= (point) opoint)
1173 ;; In some pathological cases, vertical-motion may
1174 ;; return -1 even though point has not decreased. In
1175 ;; that case, avoid looping forever.
1176 (setq done t res (point)))
1177 ((= win-start start) ; Perfect match, use this value
1178 (setq done t res (point)))
1179 ((< win-start start) ; Walked to far, use previous result
1180 (setq done t))
1181 (t ; Store result for next iteration
1182 (setq res (point))))))
1183 res)))
1184
1185 ;;; Avoid tail recenter
1186
1187 ;; This sets the window internal flag `force_start'. The effect is that
1188 ;; windows only displaying the tail aren't recentered.
1189 ;; Has to be called before every redisplay... (Great isn't it?)
1190 ;;
1191 ;; XEmacs doesn't recenter the tail, GOOD!
1192 ;;
1193 ;; A window displaying only the tail, is a window whose
1194 ;; window-start position is equal to (point-max) of the buffer it
1195 ;; displays.
1196 ;;
1197 ;; Sometimes, calling this function could actually cause a redisplay,
1198 ;; especially if it is placed in the debug filter section. I must
1199 ;; investigate this further...
1200
1201 (defun follow-avoid-tail-recenter (&rest _rest)
1202 "Make sure windows displaying the end of a buffer aren't recentered.
1203
1204 This is done by reading and rewriting the start position of
1205 non-first windows in Follow mode."
1206 (if follow-avoid-tail-recenter-p
1207 (let* ((orig-buffer (current-buffer))
1208 (top (frame-first-window (selected-frame)))
1209 (win top)
1210 who) ; list of (buffer . frame)
1211 ;; If the only window in the frame is a minibuffer
1212 ;; window, `next-window' will never find it again...
1213 (unless (window-minibuffer-p top)
1214 (while ;; look, no body!
1215 (let ((start (window-start win))
1216 (pair (cons (window-buffer win) (window-frame win))))
1217 (set-buffer (window-buffer win))
1218 (cond ((null (member pair who))
1219 (setq who (cons pair who)))
1220 ((and follow-mode (eq (point-max) start))
1221 ;; Write the same window start back, but don't
1222 ;; set the NOFORCE flag.
1223 (set-window-start win start)))
1224 (setq win (next-window win 'not t))
1225 (not (eq win top)))) ;; Loop while this is true.
1226 (set-buffer orig-buffer)))))
1227
1228 ;;; Post Command Hook
1229
1230 ;; The magic little box. This function is called after every command.
1231
1232 ;; This is not as complicated as it seems. It is simply a list of common
1233 ;; display situations and the actions to take, plus commands for redrawing
1234 ;; the screen if it should be unaligned.
1235 ;;
1236 ;; We divide the check into two parts; whether we are at the end or not.
1237 ;; This is due to the fact that the end can actually be visible
1238 ;; in several window even though they are aligned.
1239
1240 (defun follow-post-command-hook ()
1241 "Ensure that the windows in Follow mode are adjacent after each command."
1242 (unless (input-pending-p)
1243 (let ((follow-inside-post-command-hook t)
1244 (win (selected-window)))
1245 ;; Work in the selected window, not in the current buffer.
1246 (with-current-buffer (window-buffer win)
1247 (unless (and (symbolp this-command)
1248 (get this-command 'follow-mode-use-cache))
1249 (follow-invalidate-cache))
1250 (when (and follow-mode
1251 (not (window-minibuffer-p win)))
1252 ;; The buffer shown in the selected window is in follow
1253 ;; mode. Find the current state of the display.
1254 (let* ((windows (follow-all-followers win))
1255 (dest (point))
1256 (win-start-end (progn
1257 (follow-update-window-start (car windows))
1258 (follow-windows-start-end windows)))
1259 (aligned (follow-windows-aligned-p win-start-end))
1260 (visible (follow-pos-visible dest win win-start-end))
1261 selected-window-up-to-date)
1262 (unless (and aligned visible)
1263 (follow-invalidate-cache))
1264 (follow-avoid-tail-recenter)
1265 ;; Select a window to display point.
1266 (unless follow-internal-force-redisplay
1267 (if (eq dest (point-max))
1268 ;; At point-max, we have to be careful since the
1269 ;; display can be aligned while `dest' can be
1270 ;; visible in several windows.
1271 (cond
1272 ;; Select the current window, but only when the
1273 ;; display is correct. (When inserting characters
1274 ;; in a tail window, the display is not correct, as
1275 ;; they are shown twice.)
1276 ;;
1277 ;; Never stick to the current window after a
1278 ;; deletion. The reason is cosmetic: when typing
1279 ;; `DEL' in a window showing only the end of the
1280 ;; file, a character would be removed from the
1281 ;; window above, which is very unintuitive.
1282 ((and visible
1283 aligned
1284 (not (memq this-command
1285 '(backward-delete-char
1286 delete-backward-char
1287 backward-delete-char-untabify
1288 kill-region))))
1289 (follow-debug-message "Max: same"))
1290 ;; If the end is visible, and the window doesn't
1291 ;; seems like it just has been moved, select it.
1292 ((follow-select-if-end-visible win-start-end)
1293 (follow-debug-message "Max: end visible")
1294 (setq visible t aligned nil)
1295 (goto-char dest))
1296 ;; Just show the end...
1297 (t
1298 (follow-debug-message "Max: default")
1299 (select-window (car (reverse windows)))
1300 (goto-char dest)
1301 (setq visible nil aligned nil)))
1302
1303 ;; We're not at the end, here life is much simpler.
1304 (cond
1305 ;; This is the normal case!
1306 ;; It should be optimized for speed.
1307 ((and visible aligned)
1308 (follow-debug-message "same"))
1309 ;; Pick a position in any window. If the display is
1310 ;; ok, this will pick the `correct' window.
1311 ((follow-select-if-visible dest win-start-end)
1312 (follow-debug-message "visible")
1313 (goto-char dest)
1314 ;; We have to perform redisplay, since scrolling is
1315 ;; needed in case the line is partially visible.
1316 (setq visible nil))
1317 ;; Not visible anywhere else, lets pick this one.
1318 ;; (Is this case used?)
1319 (visible
1320 (follow-debug-message "visible in selected."))
1321 ;; Far out!
1322 ((eq dest (point-min))
1323 (follow-debug-message "min")
1324 (select-window (car windows))
1325 (goto-char dest)
1326 (set-window-start (selected-window) (point-min))
1327 (setq win-start-end (follow-windows-start-end windows))
1328 (follow-invalidate-cache)
1329 (setq visible t aligned nil))
1330 ;; If we can position the cursor without moving the first
1331 ;; window, do it. This is the case that catches `RET'
1332 ;; at the bottom of a window.
1333 ((follow-select-if-visible-from-first dest windows)
1334 (follow-debug-message "Below first")
1335 (setq visible t aligned t))
1336 ;; None of the above. For simplicity, we stick to the
1337 ;; selected window.
1338 (t
1339 (follow-debug-message "None")
1340 (setq visible nil aligned nil))))
1341 ;; If a new window has been selected, make sure that the
1342 ;; old is not scrolled when the point is outside the
1343 ;; window.
1344 (unless (eq win (selected-window))
1345 (let ((p (window-point win)))
1346 (set-window-start win (window-start win) nil)
1347 (set-window-point win p))))
1348 (unless visible
1349 ;; If point may not be visible in the selected window,
1350 ;; perform a redisplay; this ensures scrolling.
1351 (redisplay)
1352 (setq selected-window-up-to-date t)
1353 (follow-avoid-tail-recenter)
1354 (setq win-start-end (follow-windows-start-end windows))
1355 (follow-invalidate-cache)
1356 (setq aligned nil))
1357 ;; Now redraw the windows around the selected window.
1358 (unless (and (not follow-internal-force-redisplay)
1359 (or aligned
1360 (follow-windows-aligned-p win-start-end))
1361 (follow-point-visible-all-windows-p
1362 win-start-end))
1363 (setq follow-internal-force-redisplay nil)
1364 (follow-redisplay windows (selected-window)
1365 selected-window-up-to-date)
1366 (setq win-start-end (follow-windows-start-end windows))
1367 (follow-invalidate-cache)
1368 ;; When the point ends up in another window. This
1369 ;; happens when dest is in the beginning of the file and
1370 ;; the selected window is not the first. It can also,
1371 ;; in rare situations happen when long lines are used
1372 ;; and there is a big difference between the width of
1373 ;; the windows. (When scrolling one line in a wide
1374 ;; window which will cause a move larger that an entire
1375 ;; small window.)
1376 (unless (follow-pos-visible dest win win-start-end)
1377 (follow-select-if-visible dest win-start-end)
1378 (goto-char dest)))
1379
1380 ;; If the region is visible, make it look good when spanning
1381 ;; multiple windows.
1382 (when (region-active-p)
1383 (follow-maximize-region
1384 (selected-window) windows win-start-end))))
1385 ;; Whether or not the buffer was in follow mode, we must
1386 ;; update the windows displaying the tail so that Emacs won't
1387 ;; recenter them.
1388 (follow-avoid-tail-recenter)))))
1389
1390 ;;; The region
1391
1392 ;; Tries to make the highlighted area representing the region look
1393 ;; good when spanning several windows.
1394 ;;
1395 ;; Not perfect, as the point can't be placed at window end, only at
1396 ;; end-1. This will highlight a little bit in windows above
1397 ;; the current.
1398
1399 (defun follow-maximize-region (win windows win-start-end)
1400 "Make a highlighted region stretching multiple windows look good."
1401 (let* ((all (follow-split-followers windows win))
1402 (pred (car all))
1403 (succ (cdr all))
1404 data)
1405 (while pred
1406 (setq data (assq (car pred) win-start-end))
1407 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1408 (setq pred (cdr pred)))
1409 (while succ
1410 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1411 (setq succ (cdr succ)))))
1412
1413 ;;; Scroll bar
1414
1415 ;;;; Scroll-bar support code.
1416
1417 ;; Why is it needed? Well, if the selected window is in follow mode,
1418 ;; all its followers stick to it blindly. If one of them is scrolled,
1419 ;; it immediately returns to the original position when the mouse is
1420 ;; released. If the selected window is not a follower of the dragged
1421 ;; window the windows will be unaligned.
1422
1423 ;; The advices don't get compiled. Aesthetically, this might be a
1424 ;; problem but in practical life it isn't.
1425
1426 ;; Discussion: Now when the other windows in the chain follow the
1427 ;; dragged, should we really select it?
1428
1429 (cond ((fboundp 'scroll-bar-drag)
1430 ;;;
1431 ;;; Emacs style scrollbars.
1432 ;;;
1433
1434 ;; Select the dragged window if it is a follower of the
1435 ;; selected window.
1436 ;;
1437 ;; Generate advices of the form:
1438 ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
1439 ;; "Adviced by `follow-mode'."
1440 ;; (follow-redraw-after-event (ad-get-arg 0)))
1441 (let ((cmds '(scroll-bar-drag
1442 scroll-bar-drag-1 ; Executed at every move.
1443 scroll-bar-scroll-down
1444 scroll-bar-scroll-up
1445 scroll-bar-set-window-start)))
1446 (while cmds
1447 (eval
1448 `(defadvice ,(intern (symbol-name (car cmds)))
1449 (after
1450 ,(intern (concat "follow-" (symbol-name (car cmds))))
1451 activate)
1452 "Adviced by Follow mode."
1453 (follow-redraw-after-event (ad-get-arg 0))))
1454 (setq cmds (cdr cmds))))
1455
1456
1457 (defun follow-redraw-after-event (event)
1458 "Adviced by Follow mode."
1459 (condition-case nil
1460 (let* ((orig-win (selected-window))
1461 (win (nth 0 (funcall
1462 (symbol-function 'event-start) event)))
1463 (fmode (assq 'follow-mode
1464 (buffer-local-variables
1465 (window-buffer win)))))
1466 (if (and fmode (cdr fmode))
1467 ;; The selected window is in follow-mode
1468 (progn
1469 ;; Recenter around the dragged window.
1470 (select-window win)
1471 (follow-redisplay)
1472 (select-window orig-win))))
1473 (error nil))))
1474
1475
1476 ((fboundp 'scrollbar-vertical-drag)
1477 ;;;
1478 ;;; XEmacs style scrollbars.
1479 ;;;
1480
1481 ;; Advice all scrollbar functions on the form:
1482 ;;
1483 ;; (defadvice scrollbar-line-down
1484 ;; (after follow-scrollbar-line-down activate)
1485 ;; (follow-xemacs-scrollbar-support (ad-get-arg 0)))
1486
1487 (let ((cmds '(scrollbar-line-down ; Window
1488 scrollbar-line-up
1489 scrollbar-page-down ; Object
1490 scrollbar-page-up
1491 scrollbar-to-bottom ; Window
1492 scrollbar-to-top
1493 scrollbar-vertical-drag ; Object
1494 )))
1495
1496 (while cmds
1497 (eval
1498 `(defadvice ,(intern (symbol-name (car cmds)))
1499 (after
1500 ,(intern (concat "follow-" (symbol-name (car cmds))))
1501 activate)
1502 "Adviced by `follow-mode'."
1503 (follow-xemacs-scrollbar-support (ad-get-arg 0))))
1504 (setq cmds (cdr cmds))))
1505
1506
1507 (defun follow-xemacs-scrollbar-support (window)
1508 "Redraw windows showing the same buffer as shown in WINDOW.
1509 WINDOW is either the dragged window, or a cons containing the
1510 window as its first element. This is called while the user drags
1511 the scrollbar.
1512
1513 WINDOW can be an object or a window."
1514 (condition-case nil
1515 (progn
1516 (if (consp window)
1517 (setq window (car window)))
1518 (let ((fmode (assq 'follow-mode
1519 (buffer-local-variables
1520 (window-buffer window))))
1521 (orig-win (selected-window)))
1522 (if (and fmode (cdr fmode))
1523 (progn
1524 ;; Recenter around the dragged window.
1525 (select-window window)
1526 (follow-redisplay)
1527 (select-window orig-win)))))
1528 (error nil)))))
1529
1530 ;;; Process output
1531
1532 ;; The following sections installs a spy that listens to process
1533 ;; output and tries to reposition the windows whose buffers are in
1534 ;; Follow mode. We play safe as much as possible...
1535 ;;
1536 ;; When follow-mode is activated all active processes are
1537 ;; intercepted. All new processes that change their filter function
1538 ;; using `set-process-filter' are also intercepted. The reason is
1539 ;; that a process can cause a redisplay recentering "tail" windows.
1540 ;; Note that it doesn't hurt to spy on more processes than needed.
1541 ;;
1542 ;; Technically, we set the process filter to `follow-generic-filter'.
1543 ;; The original filter is stored in `follow-process-filter-alist'.
1544 ;; Our generic filter calls the original filter, or inserts the
1545 ;; output into the buffer, if the buffer originally didn't have an
1546 ;; output filter. It also makes sure that the windows connected to
1547 ;; the buffer are aligned.
1548 ;;
1549 ;; Discussion: How do we find processes that don't call
1550 ;; `set-process-filter'? (How often are processes created in a
1551 ;; buffer after Follow mode are activated?)
1552 ;;
1553 ;; Discussion: Should we also advice `process-filter' to make our
1554 ;; filter invisible to others?
1555
1556 ;;; Advice for `set-process-filter'
1557
1558 ;; Do not call this with 'follow-generic-filter as the name of the
1559 ;; filter...
1560
1561 (defadvice set-process-filter (before follow-set-process-filter activate)
1562 "Ensure process output will be displayed correctly in Follow mode buffers.
1563
1564 Follow mode inserts its own process filter to do its
1565 magic stuff before the real process filter is called."
1566 (if follow-intercept-processes
1567 (progn
1568 (setq follow-process-filter-alist
1569 (delq (assq (ad-get-arg 0) follow-process-filter-alist)
1570 follow-process-filter-alist))
1571 (follow-tidy-process-filter-alist)
1572 (cond ((eq (ad-get-arg 1) t))
1573 ((eq (ad-get-arg 1) nil)
1574 (ad-set-arg 1 'follow-generic-filter))
1575 (t
1576 (setq follow-process-filter-alist
1577 (cons (cons (ad-get-arg 0) (ad-get-arg 1))
1578 follow-process-filter-alist))
1579 (ad-set-arg 1 'follow-generic-filter))))))
1580
1581
1582 (defun follow-call-set-process-filter (proc filter)
1583 "Call original `set-process-filter' without the Follow mode advice."
1584 (ad-disable-advice 'set-process-filter 'before
1585 'follow-set-process-filter)
1586 (ad-activate 'set-process-filter)
1587 (prog1
1588 (set-process-filter proc filter)
1589 (ad-enable-advice 'set-process-filter 'before
1590 'follow-set-process-filter)
1591 (ad-activate 'set-process-filter)))
1592
1593
1594 (defadvice process-filter (after follow-process-filter activate)
1595 "Return the original process filter, not `follow-generic-filter'."
1596 (cond ((eq ad-return-value 'follow-generic-filter)
1597 (setq ad-return-value
1598 (cdr-safe (assq (ad-get-arg 0)
1599 follow-process-filter-alist))))))
1600
1601
1602 (defun follow-call-process-filter (proc)
1603 "Call original `process-filter' without the Follow mode advice."
1604 (ad-disable-advice 'process-filter 'after
1605 'follow-process-filter)
1606 (ad-activate 'process-filter)
1607 (prog1
1608 (process-filter proc)
1609 (ad-enable-advice 'process-filter 'after
1610 'follow-process-filter)
1611 (ad-activate 'process-filter)))
1612
1613
1614 (defun follow-tidy-process-filter-alist ()
1615 "Remove old processes from `follow-process-filter-alist'."
1616 (let ((alist follow-process-filter-alist)
1617 (ps (process-list))
1618 (new ()))
1619 (while alist
1620 (if (and (not (memq (process-status (car (car alist)))
1621 '(exit signal closed nil)))
1622 (memq (car (car alist)) ps))
1623 (setq new (cons (car alist) new)))
1624 (setq alist (cdr alist)))
1625 (setq follow-process-filter-alist new)))
1626
1627 ;;; Start/stop interception of processes.
1628
1629 ;; Normally, all new processes are intercepted by our `set-process-filter'.
1630 ;; This is needed to intercept old processes that were started before we were
1631 ;; loaded, and processes we have forgotten by calling
1632 ;; `follow-stop-intercept-process-output'.
1633
1634 (defun follow-intercept-process-output ()
1635 "Intercept all active processes.
1636
1637 This is needed so that Follow mode can track all display events in the
1638 system. (See `follow-mode'.)"
1639 (interactive)
1640 (let ((list (process-list)))
1641 (while list
1642 (if (eq (process-filter (car list)) 'follow-generic-filter)
1643 nil
1644 ;; The custom `set-process-filter' defined above.
1645 (set-process-filter (car list) (process-filter (car list))))
1646 (setq list (cdr list))))
1647 (setq follow-intercept-processes t))
1648
1649
1650 (defun follow-stop-intercept-process-output ()
1651 "Stop Follow mode from spying on processes.
1652
1653 All current spypoints are removed and no new will be added.
1654
1655 The effect is that Follow mode won't be able to handle buffers
1656 connected to processes.
1657
1658 The only reason to call this function is if the Follow mode spy filter
1659 would interfere with some other package. If this happens, please
1660 report this using the `report-emacs-bug' function."
1661 (interactive)
1662 (follow-tidy-process-filter-alist)
1663 (dolist (process (process-list))
1664 (when (eq (follow-call-process-filter process) 'follow-generic-filter)
1665 (follow-call-set-process-filter
1666 process
1667 (cdr-safe (assq process follow-process-filter-alist)))
1668 (setq follow-process-filter-alist
1669 (delq (assq process follow-process-filter-alist)
1670 follow-process-filter-alist))))
1671 (setq follow-intercept-processes nil))
1672
1673 ;;; The filter
1674
1675 ;; The following section is a naive method to make buffers with
1676 ;; process output to work with Follow mode. Whenever the start of the
1677 ;; window displaying the buffer is moved, we move it back to its
1678 ;; original position and try to select a new window. (If we fail,
1679 ;; the normal redisplay functions of Emacs will scroll it right
1680 ;; back!)
1681
1682 (defun follow-generic-filter (proc output)
1683 "Process output filter for process connected to buffers in Follow mode."
1684 (let* ((old-buffer (current-buffer))
1685 (orig-win (selected-window))
1686 (buf (process-buffer proc))
1687 (win (and buf (if (eq buf (window-buffer orig-win))
1688 orig-win
1689 (get-buffer-window buf t))))
1690 (return-to-orig-win (and win (not (eq win orig-win))))
1691 (orig-window-start (and win (window-start win))))
1692
1693 ;; If input is pending, the `sit-for' below won't redraw the
1694 ;; display. In that case, calling `follow-avoid-tail-recenter' may
1695 ;; provoke the process handling code to schedule a redisplay.
1696 ;(or (input-pending-p)
1697 ; (follow-avoid-tail-recenter))
1698
1699 ;; Output the `output'.
1700 (let ((filter (cdr-safe (assq proc follow-process-filter-alist))))
1701 (cond
1702 ;; Call the original filter function
1703 (filter
1704 (funcall filter proc output))
1705
1706 ;; No filter, but we've got a buffer. Just output into it.
1707 (buf
1708 (set-buffer buf)
1709 (if (not (marker-buffer (process-mark proc)))
1710 (set-marker (process-mark proc) (point-max)))
1711 (let ((moving (= (point) (process-mark proc)))
1712 deactivate-mark
1713 (inhibit-read-only t))
1714 (save-excursion
1715 (goto-char (process-mark proc))
1716 ;; `insert-before-markers' just in case the user's next
1717 ;; command is M-y.
1718 (insert-before-markers output)
1719 (set-marker (process-mark proc) (point)))
1720 (if moving (goto-char (process-mark proc)))))))
1721
1722 ;; If we're in follow mode, do our stuff. Select a new window and
1723 ;; redisplay. (Actually, it is redundant to check `buf', but I
1724 ;; feel it's more correct.)
1725 (if (and buf (window-live-p win))
1726 (progn
1727 (set-buffer buf)
1728 (if (and (boundp 'follow-mode) follow-mode)
1729 (progn
1730 (select-window win)
1731 (let* ((windows (follow-all-followers win))
1732 (win-start-end (follow-windows-start-end windows))
1733 (new-window-start (window-start win))
1734 (new-window-point (window-point win)))
1735 (cond
1736 ;; The start of the selected window was repositioned.
1737 ;; Try to use the original start position and continue
1738 ;; working with a window to the "right" in the window
1739 ;; chain. This will create the effect that the output
1740 ;; starts in one window and continues into the next.
1741
1742 ;; If the display has changed so much that it is not
1743 ;; possible to keep the original window fixed and still
1744 ;; display the point then we give up and use the new
1745 ;; window start.
1746
1747 ;; This case is typically used when the process filter
1748 ;; tries to reposition the start of the window in order
1749 ;; to view the tail of the output.
1750 ((not (eq orig-window-start new-window-start))
1751 (follow-debug-message "filter: Moved")
1752 (set-window-start win orig-window-start)
1753 (follow-redisplay windows win)
1754 (setq win-start-end (follow-windows-start-end windows))
1755 (follow-select-if-visible new-window-point
1756 win-start-end)
1757 (goto-char new-window-point)
1758 (if (eq win (selected-window))
1759 (set-window-start win new-window-start))
1760 (setq win-start-end (follow-windows-start-end windows)))
1761 ;; Stick to this window, if point is visible in it.
1762 ((pos-visible-in-window-p new-window-point)
1763 (follow-debug-message "filter: Visible in window"))
1764 ;; Avoid redisplaying the first window. If the
1765 ;; point is visible at a window below,
1766 ;; redisplay and select it.
1767 ((follow-select-if-visible-from-first
1768 new-window-point windows)
1769 (follow-debug-message "filter: Seen from first")
1770 (setq win-start-end
1771 (follow-windows-start-end windows)))
1772 ;; None of the above. We stick to the current window.
1773 (t
1774 (follow-debug-message "filter: nothing")))
1775
1776 ;; Here we have selected a window. Make sure the
1777 ;; windows are aligned and the point is visible
1778 ;; in the selected window.
1779 (if (and (not (follow-pos-visible
1780 (point) (selected-window) win-start-end))
1781 (not return-to-orig-win))
1782 (progn
1783 (sit-for 0)
1784 (setq win-start-end
1785 (follow-windows-start-end windows))))
1786
1787 (if (or follow-internal-force-redisplay
1788 (not (follow-windows-aligned-p win-start-end)))
1789 (follow-redisplay windows)))))))
1790
1791 ;; return to the original window.
1792 (if return-to-orig-win
1793 (select-window orig-win))
1794 ;; Restore the original buffer, unless the filter explicitly
1795 ;; changed buffer or killed the old buffer.
1796 (if (and (eq buf (current-buffer))
1797 (buffer-name old-buffer))
1798 (set-buffer old-buffer)))
1799
1800 (follow-invalidate-cache)
1801
1802 ;; Normally, if the display has been changed, it is redrawn. All
1803 ;; windows showing only the end of a buffer are unconditionally
1804 ;; recentered; we can't prevent that by calling
1805 ;; `follow-avoid-tail-recenter'.
1806 ;;
1807 ;; We force a redisplay here on our own, so Emacs does need to.
1808 ;; (However, redisplaying when there's input available just seems
1809 ;; to make things worse, so we exclude that case.)
1810 (if (and follow-avoid-tail-recenter-p
1811 (not (input-pending-p)))
1812 (sit-for 0)))
1813
1814 ;;; Window size change
1815
1816 ;; In Emacs 19.29, the functions in `window-size-change-functions' are
1817 ;; called every time a window in a frame changes size. Most notably, it
1818 ;; is called after the frame has been resized.
1819 ;;
1820 ;; We basically call our post-command-hook for every buffer that is
1821 ;; visible in any window in the resized frame, which is in follow-mode.
1822 ;;
1823 ;; Since this function can be called indirectly from
1824 ;; `follow-post-command-hook' we have a potential infinite loop. We
1825 ;; handle this problem by simply not doing anything at all in this
1826 ;; situation. The variable `follow-inside-post-command-hook' contains
1827 ;; information about whether the execution actually is inside the
1828 ;; post-command-hook or not.
1829
1830 (if (boundp 'window-size-change-functions)
1831 (add-hook 'window-size-change-functions 'follow-window-size-change))
1832
1833
1834 (defun follow-window-size-change (frame)
1835 "Redraw all windows in FRAME, when in Follow mode."
1836 ;; Below, we call `post-command-hook'. This makes sure that we
1837 ;; don't start a mutually recursive endless loop.
1838 (if follow-inside-post-command-hook
1839 nil
1840 (let ((buffers '())
1841 (orig-window (selected-window))
1842 (orig-buffer (current-buffer))
1843 (orig-frame (selected-frame))
1844 windows
1845 buf)
1846 (select-frame frame)
1847 (unwind-protect
1848 (walk-windows
1849 (function
1850 (lambda (win)
1851 (setq buf (window-buffer win))
1852 (if (memq buf buffers)
1853 nil
1854 (set-buffer buf)
1855 (if (and (boundp 'follow-mode)
1856 follow-mode)
1857 (progn
1858 (setq windows (follow-all-followers win))
1859 (if (memq orig-window windows)
1860 (progn
1861 ;; Make sure we're redrawing around the
1862 ;; selected window.
1863 ;;
1864 ;; We must be really careful not to do this
1865 ;; when we are (indirectly) called by
1866 ;; `post-command-hook'.
1867 (select-window orig-window)
1868 (follow-post-command-hook)
1869 (setq orig-window (selected-window)))
1870 (follow-redisplay windows win))
1871 (setq buffers (cons buf buffers))))))))
1872 (select-frame orig-frame)
1873 (set-buffer orig-buffer)
1874 (select-window orig-window)))))
1875
1876 ;;; XEmacs isearch
1877
1878 ;; In XEmacs, isearch often finds matches in other windows than the
1879 ;; currently selected. However, when exiting the old window
1880 ;; configuration is restored, with the exception of the beginning of
1881 ;; the start of the window for the selected window. This is not much
1882 ;; help for us.
1883 ;;
1884 ;; We overwrite the stored window configuration with the current,
1885 ;; unless we are in `slow-search-mode', i.e. only a few lines
1886 ;; of text is visible.
1887
1888 (if (featurep 'xemacs)
1889 (defadvice isearch-done (before follow-isearch-done activate)
1890 (if (and (boundp 'follow-mode)
1891 follow-mode
1892 (boundp 'isearch-window-configuration)
1893 isearch-window-configuration
1894 (boundp 'isearch-slow-terminal-mode)
1895 (not isearch-slow-terminal-mode))
1896 (let ((buf (current-buffer)))
1897 (setq isearch-window-configuration
1898 (current-window-configuration))
1899 (set-buffer buf)))))
1900
1901 ;;; Tail window handling
1902
1903 ;; In Emacs (not XEmacs) windows showing nothing are sometimes
1904 ;; recentered. When in Follow mode, this is not desirable for
1905 ;; non-first windows in the window chain. This section tries to
1906 ;; make the windows stay where they should be.
1907 ;;
1908 ;; If the display is updated, all windows starting at (point-max) are
1909 ;; going to be recentered at the next redisplay, unless we do a
1910 ;; read-and-write cycle to update the `force' flag inside the windows.
1911 ;;
1912 ;; In 19.30, a new variable `window-scroll-functions' is called every
1913 ;; time a window is recentered. It is not perfect for our situation,
1914 ;; since when it is called for a tail window, it is to late. However,
1915 ;; if it is called for another window, we can try to update our
1916 ;; windows.
1917 ;;
1918 ;; By patching `sit-for' we can make sure that to catch all explicit
1919 ;; updates initiated by lisp programs. Internal calls, on the other
1920 ;; hand, are not handled.
1921
1922 ;; If this function is called it is too late for this window, but
1923 ;; we might save other windows from being recentered.
1924
1925 (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions))
1926 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t))
1927
1928
1929 ;; This prevents all packages that calls `sit-for' directly
1930 ;; to recenter tail windows.
1931
1932 (if follow-avoid-tail-recenter-p
1933 (defadvice sit-for (before follow-sit-for activate)
1934 "Adviced by Follow mode.
1935
1936 Avoid to recenter windows displaying only the end of a file as when
1937 displaying a short file in two windows, using Follow mode."
1938 (follow-avoid-tail-recenter)))
1939
1940
1941 ;; Without this advice, `mouse-drag-region' would start to recenter
1942 ;; tail windows.
1943
1944 (if (and follow-avoid-tail-recenter-p
1945 (fboundp 'move-overlay))
1946 (defadvice move-overlay (before follow-move-overlay activate)
1947 "Adviced by Follow mode.
1948 Don't recenter windows showing only the end of a buffer.
1949 This prevents `mouse-drag-region' from messing things up."
1950 (follow-avoid-tail-recenter)))
1951
1952 ;;; Profile support
1953
1954 ;; The following (non-evaluated) section can be used to
1955 ;; profile this package using `elp'.
1956 ;;
1957 ;; Invalid indentation on purpose!
1958
1959 (cond (nil
1960 (setq elp-function-list
1961 '(window-end
1962 vertical-motion
1963 ; sit-for ;; elp can't handle advices...
1964 follow-mode
1965 follow-all-followers
1966 follow-split-followers
1967 follow-redisplay
1968 follow-estimate-first-window-start
1969 follow-calculate-first-window-start-from-above
1970 follow-calculate-first-window-start-from-below
1971 follow-calc-win-end
1972 follow-calc-win-start
1973 follow-pos-visible
1974 follow-windows-start-end
1975 follow-cache-valid-p
1976 follow-select-if-visible
1977 follow-select-if-visible-from-first
1978 follow-windows-aligned-p
1979 follow-point-visible-all-windows-p
1980 follow-avoid-tail-recenter
1981 follow-update-window-start
1982 follow-post-command-hook
1983 ))))
1984
1985 ;;; The end
1986
1987 (defun follow-unload-function ()
1988 "Unload Follow mode library."
1989 (easy-menu-remove-item nil '("Tools") "Follow")
1990 (follow-stop-intercept-process-output)
1991 (dolist (group '((before
1992 ;; XEmacs
1993 isearch-done
1994 ;; both
1995 set-process-filter sit-for move-overlay)
1996 (after
1997 ;; Emacs
1998 scroll-bar-drag scroll-bar-drag-1 scroll-bar-scroll-down
1999 scroll-bar-scroll-up scroll-bar-set-window-start
2000 ;; XEmacs
2001 scrollbar-line-down scrollbar-line-up scrollbar-page-down
2002 scrollbar-page-up scrollbar-to-bottom scrollbar-to-top
2003 scrollbar-vertical-drag
2004 ;; both
2005 process-filter)))
2006 (let ((class (car group)))
2007 (dolist (fun (cdr group))
2008 (when (functionp fun)
2009 (condition-case nil
2010 (progn
2011 (ad-remove-advice fun class
2012 (intern (concat "follow-" (symbol-name fun))))
2013 (ad-update fun))
2014 (error nil))))))
2015 ;; continue standard processing
2016 nil)
2017
2018 (provide 'follow)
2019
2020 ;; /------------------------------------------------------------------------\
2021 ;; | "I [..] am rarely happier then when spending an entire day programming |
2022 ;; | my computer to perform automatically a task that it would otherwise |
2023 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
2024 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
2025 ;; | worth the investment of a day's happy activity working out a way to |
2026 ;; | save it". -- Douglas Adams, "Last Chance to See" |
2027 ;; \------------------------------------------------------------------------/
2028
2029 ;;; follow.el ends here