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