1 ;;; follow.el --- Minor mode, Synchronize windows showing the same buffer.
3 ;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren <andersl@csd.uu.se>
6 ;; Maintainer: Anders Lindgren <andersl@csd.uu.se>
7 ;; Created: 25 May 1995
9 ;; Keywords: display, window, minor-mode
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
33 ;; `Follow mode' is a minor mode for Emacs 19 and XEmacs which
34 ;; combines windows into one tall virtual window.
36 ;; The feeling of a "virtual window" has been accomplished by the use
37 ;; of two major techniques:
39 ;; * The windows always displays adjacent sections of the buffer.
40 ;; This means that whenever one window is moved, all the
41 ;; others will follow. (Hence the name Follow Mode.)
43 ;; * Should the point (cursor) end up outside a window, another
44 ;; window displaying that point is selected, if possible. This
45 ;; makes it possible to walk between windows using normal cursor
48 ;; Follow mode comes to its prime when used on a large screen and two
49 ;; side-by-side window are used. The user can, with the help of Follow
50 ;; mode, use two full-height windows as though they would have been
51 ;; one. Imagine yourself editing a large function, or section of text,
52 ;; and being able to use 144 lines instead of the normal 72... (your
55 ;; The latest version, and a demonstration, are avaiable at:
57 ;; ftp://ftp.csd.uu.se/pub/users/andersl/emacs/follow.el
58 ;; http://www.csd.uu.se/~andersl/follow.shtml
60 ;; `Follow mode' can be used together with Emacs 19 and XEmacs.
61 ;; It has been tested together with Emacs 19.27, 19.28, 19.29,
62 ;; 19.30, XEmacs 19.12, and 19.13.
65 ;; To test this package, make sure `follow' is loaded, or will be
66 ;; autoloaded when activated (see below). Then do the following:
68 ;; * Find your favorite file (preferably a long one.)
70 ;; * Resize Emacs so that it will be wide enough for two full sized
71 ;; columns. Delete the other windows and split with the commands
74 ;; * Give the command:
75 ;; M-x follow-mode <RETURN>
77 ;; * Now the display should look something like (assuming the text "71"
80 ;; +----------+----------+
87 ;; +----------+----------+
89 ;; As you can see, the right-hand window starts at line 73, the line
90 ;; immediately below the end of the left-hand window. As long as
91 ;; `follow-mode' is active, the two windows will follow eachother!
93 ;; * Play around and enjoy! Scroll one window and watch the other.
94 ;; Jump to the beginning or end. Press `Cursor down' at the last
95 ;; line of the left-hand window. Enter new lines into the
96 ;; text. Enter long lines spanning several lines, or several
99 ;; * Should you find `Follow' mode annoying, just type
100 ;; M-x follow-mode <RETURN>
106 ;; To fully install this, add this file to your Emacs Lisp directory and
107 ;; compile it with M-x byte-compile-file. Then add the following to the
108 ;; appropriate init file (normally your `~/.emacs' file):
110 ;; (autoload 'follow-mode "follow"
111 ;; "Synchronize windows showing the same buffer, minor mode." t)
114 ;; The command `follow-delete-other-windows-and-split' maximises the
115 ;; visible area of the current buffer.
117 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
118 ;; key map. To do so, add the following lines (replacing `[f7]' and
119 ;; `[f8]' with your favorite keys) to the init file:
121 ;; (autoload 'follow-mode "follow"
122 ;; "Synchronize windows showing the same buffer, minor mode." t)
123 ;; (global-set-key [f8] 'follow-mode)
125 ;; (autoload 'follow-delete-other-windows-and-split "follow"
126 ;; "Delete other windows, split the frame in two, and enter Follow Mode." t)
127 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
130 ;; There exists two system variables which controls the appearence of
131 ;; lines which are wider than the window containing them. The default
132 ;; is to truncate long lines whenever a window isn't as wide as the
135 ;; To make sure lines are never truncated, please place the following
136 ;; lines in your init file:
138 ;; (setq truncate-lines nil)
139 ;; (setq truncate-partial-width-windows nil)
142 ;; Since the display of XEmacs is pixel-oriented, a line could be
143 ;; clipped in half at the bottom of the window.
145 ;; To make XEmacs avoid clipping (normal) lines, please place the
146 ;; following line in your init-file:
148 ;; (setq pixel-vertical-clip-threshold 30)
151 ;; The correct way to cofigurate Follow mode, or any other mode for
152 ;; that matter, is to create one (or more) function which does
153 ;; whatever you would like to do. The function is then added to
156 ;; When `Follow' mode is activated, functions stored in the hook
157 ;; `follow-mode-hook' are called. When it is deactivated
158 ;; `follow-mode-off-hook' is runed.
160 ;; The keymap `follow-key-map' contains key bindings activated by
164 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
166 ;; (defun my-follow-mode-hook ()
167 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
168 ;; (define-key follow-mode-map "\C-cb" 'another-function))
173 ;; To activate give the command: M-x follow-mode
174 ;; and press return. To deactivate, do it again.
176 ;; Some special commands have been developed to make life even easier:
177 ;; follow-scroll-up C-c . C-v
178 ;; Scroll text in a Follow Mode window chain up.
180 ;; follow-scroll-down C-c . v
181 ;; Like `follow-scroll-up', but in the other direction.
183 ;; follow-delete-other-windows-and-split C-c . 1
184 ;; Maximise the visible area of the current buffer,
185 ;; and enter Follow Mode. This is a very convenient
186 ;; way to start Follow Mode, hence it is recomended
187 ;; that this command is added to the global keymap.
189 ;; follow-recenter C-c . C-l
190 ;; Place the point in the center of the middle window,
191 ;; or a specified number of lines from either top or bottom.
193 ;; follow-switch-to-buffer C-c . b
194 ;; Switch buffer in all windows displaying the current buffer
197 ;; follow-switch-to-buffer-all C-c . C-b
198 ;; Switch buffer in all windows in the active frame.
200 ;; follow-switch-to-current-buffer-all
201 ;; Show the current buffer in all windows on the current
202 ;; frame and turn on `follow-mode'.
204 ;; follow-first-window C-c . <
205 ;; Select the first window in the frame showing the same buffer.
207 ;; follow-last-window C-c . >
208 ;; Select the last window in the frame showing the same buffer.
210 ;; follow-next-window C-c . n
211 ;; Select the next window in the frame showing the same buffer.
213 ;; follow-previous-window C-c . p
214 ;; Select the previous window showing the same buffer.
217 ;; Well, it seems ok, but what if I really want to look at two different
218 ;; positions in the text? Here are two simple methods to use:
220 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
221 ;; in the same frame. (My apoligies to you who can't use frames.)
223 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
224 ;; you want to view two locations. Of course, `follow' mode can
225 ;; be reactivated by hitting the same key again.
227 ;; Example from my ~/.emacs:
228 ;; (global-set-key [f8] 'follow-mode)
233 ;; In an ideal world, follow mode would have been implemented in the
234 ;; kernal of the display routines, making sure that the windows (in
235 ;; follow mode) ALWAYS are aligned. On planet earth, however, we must
236 ;; accept a solution where we ALMOST ALWAYS can make sure that the
237 ;; windows are aligned.
239 ;; Follow mode does this in three places:
240 ;; 1) After each user command.
241 ;; 2) After a process output has been perfomed.
242 ;; 3) When a scrollbar has been moved.
244 ;; This will cover most situations. (Let me know if there are other
245 ;; situations which should be covered.)
247 ;; However, only the selected window is checked, for the reason of
248 ;; efficiency and code complexity. (i.e. it is possible to make a
249 ;; non-selected windows unaligned. It will, however, pop right back
250 ;; when it is selected.)
256 ;; 25-May-95 andersl * File created.
257 ;; 26-May-95 andersl * It works!
258 ;; 27-May-95 andersl * Avoids hitting the head in the roof.
259 ;; * follow-scroll-up, -scroll-down, and -recenter.
260 ;; * V0.1 Sent to Ohio.
261 ;; 28-May-95 andersl * Scroll-bar support added.
262 ;; 30-May-95 andersl * Code adopted to standard style.
263 ;; * Minor mode keymap.
264 ;; 2-Jun-95 andersl * Processor output.
265 ;; 3-Jun-95 andersl * V0.4
266 ;; 5-Jun-95 andersl * V0.5. Copyright notice corrected.
267 ;; (The old one stated that I had copyright, but
268 ;; that Emacs could be freely distributed ;-) )
269 ;; 6-Jun-95 andersl * Lucid support added. (no longer valid.)
270 ;; 7-Jun-95 andersl * Menu bar added.
271 ;; * Bug fix, (at-window 0 0) => (frame-first-window)
272 ;; 15-Jun-95 andersl * 0.8 Major rework. looong lines and outline mode.
273 ;; 18-Jun-95 andersl * 0.9 Allow a tail window to be selected, but pick
274 ;; a better one when edited.
275 ;; 26-Jun-95 andersl * Inlineing.
276 ;; 02-Jul-95 andersl * compute-motion imitated with a ugly workaround,
277 ;; Works with XEmacs again!
278 ;; 15-Jul-95 andersl * find-file hook.
279 ;; * submit-feedback.
280 ;; * Survives major mode changes.
281 ;; * Region spanning multiple windows looks
283 ;; 19-Jul-95 andersl * New process-filter handling.
284 ;; 1-Aug-95 andersl * XEmacs scrollbar support.
285 ;; * Emacs 19 `window-size-change' support.
286 ;; * `save-window-excursion' removed, it triggered
288 ;; 5-Aug-95 andersl * `follow-switch-to-current-buffer-all' added.
289 ;; 16-Nov-95 andersl * V1.0 released!
290 ;; 17-Nov-95 andersl * Byte compiler silencer for XEmacs broken.
291 ;; * fkey-end-of-buffer treated the same way
293 ;; * follow-mode-off-hook added.
294 ;; (Suggested by David Hughes, thanks!)
295 ;; 20-Nov-95 andersl * Bug in menu code corrected.
296 ;; (Reported by Robert E. Brown, thanks!)
297 ;; 5-Dec-95 andersl * `follow-avoid-tail-recenter' added to the
298 ;; post-command-idle-hook to avoid recentering
299 ;; caused by `paren' et. al.
300 ;; 7-Dec-95 andersl * `follow-avoid-tail-recenter' called by
301 ;; `window-scroll-functions'.
302 ;; 18-Dec-95 andersl * All processes intercepted.
303 ;; 20-Dec-95 andersl * `follow-recenter' accepts arguments.
304 ;; * `move-overlay' advices, drag-region works.
305 ;; 2-Jan-96 andersl * XEmacs: isearch fixed.
306 ;; * `follow-calc-win-end' created.
307 ;; 8-Jan-96 andersl * XEmacs: `window-end' with `guarantee'
308 ;; argument used in `follow-calc-win-end'.
309 ;; 9-Jan-96 andersl * `follow-end-of-buffer' added.
310 ;; Code in post hook removed.
311 ;; * XEmacs: Post hook is always executed
312 ;; after a mouse button event.
313 ;; 22-Jan-96 andersl * 1.5 released.
319 ;;; LCD Archive Entry:
320 ;; follow|Anders Lindgren|andersl@csd.uu.se|
321 ;; Combines windows into tall virtual window, minor mode.
322 ;; 20-Feb-1996|1.6|~/modes/follow.el.Z|
330 ;; Make the compiler shut up!
331 ;; There are two strategies:
332 ;; 1) Shut warnings off completely.
333 ;; 2) Handle each warning separately.
335 ;; Since I would like to see real errors, I've selected the latter
338 ;; The problem with undefined variables and functions has been solved
339 ;; by using `set', `symbol-value' and `symbol-function' rather than
340 ;; `setq' and direct references to variables and functions.
343 ;; (if (boundp 'foo) ... (symbol-value 'foo) )
344 ;; (set 'foo ...) <-- XEmacs doesn't fall for this one.
345 ;; (funcall (symbol-function 'set) 'bar ...)
347 ;; Note: When this file is interpreted, `eval-when-compile' is
348 ;; evaluted (really smart...) Since it doesn't hurt to evaluate it,
349 ;; but it is a bit annoying, we test if the byte-compiler has been
350 ;; loaded. This can, of course, lead to some occasional unintended
353 ;; Should someone come up with a better solution, please let me
357 (if (or (featurep 'bytecomp
)
358 (featurep 'byte-compile
))
359 (cond ((string-match "XEmacs" emacs-version
)
360 ;; Make XEmacs shut up! I'm using standard Emacs
361 ;; functions, they are NOT obsolete!
362 (if (eq (get 'force-mode-line-update
'byte-compile
)
363 'byte-compile-obsolete
)
364 (put 'force-mode-line-update
'byte-compile
'nil
))
365 (if (eq (get 'frame-first-window
'byte-compile
)
366 'byte-compile-obsolete
)
367 (put 'frame-first-window
'byte-compile
'nil
))))))
373 "Synchronize windows showing the same buffer."
377 (defvar follow-mode nil
378 "Variable indicating if Follow mode is active.")
380 (defcustom follow-mode-hook nil
381 "*Hooks to run when follow-mode is turned on."
385 (defcustom follow-mode-off-hook nil
386 "*Hooks to run when follow-mode is turned off."
390 (defvar follow-mode-version
"follow.el (Release 1.6)"
391 "The current version of Follow mode.")
393 (defvar follow-mode-map nil
394 "*Minor mode keymap for Follow mode.")
396 (defcustom follow-mode-line-text
" Follow"
397 "*Text shown in the mode line when Follow mode is active.
398 Defaults to \" Follow\". Examples of other values
399 are \" Fw\", or simply \"\"."
403 (defcustom follow-auto nil
404 "*Non-nil activates Follow mode whenever a file is loaded."
408 (defcustom follow-mode-prefix
"\C-c."
409 "*Prefix key to use for follow commands in Follow mode.
410 The value of this variable is checked as part of loading Follow mode.
411 After that, changing the prefix key requires manipulating keymaps."
415 (defcustom follow-intercept-processes t
416 "*When non-nil, Follow Mode will monitor process output."
420 (defvar follow-emacs-version-xemacs-p
421 (string-match "XEmacs" emacs-version
)
422 "Non-nil when running under XEmacs.")
424 (defvar follow-avoid-tail-recenter-p
425 (not follow-emacs-version-xemacs-p
)
426 "*When non-nil, patch emacs so that tail windows won't be recentered.
428 A \"tail window\" is a window which displays only the end of
429 the buffer. Normally it is practical for the user that empty
430 windows are recentered automatically. However, when using
431 Follow Mode it breaks the display when the end is displayed
432 in a window \"above\" the last window. This is for
433 example the case when displaying a short page in info.
435 Must be set before Follow Mode is loaded.
437 Please note that it is not possible to fully prevent Emacs from
438 recentering empty windows. Please report if you find a repeatable
439 situation in which Emacs recenters empty windows.
441 XEmacs, as of 19.12, does not recenter windows, good!")
443 (defvar follow-cache-command-list
444 '(next-line previous-line forward-char backward-char
)
445 "List of commands which don't require recalculation.
447 In order to be able to use the cache, a command should not change the
448 contents of the buffer, nor should it change selected window or current
451 The commands in this list are checked at load time.
453 To mark other commands as suitable for caching, set the symbol
454 property `follow-mode-use-cache' to non-nil.")
456 (defvar follow-debug nil
457 "*Non-nil when debugging Follow mode.")
460 ;; Internal variables:
462 (defvar follow-internal-force-redisplay nil
463 "True when Follow mode should redisplay the windows.")
465 (defvar follow-process-filter-alist
'()
466 "The original filters for processes intercepted by Follow mode.")
468 (defvar follow-active-menu nil
469 "The menu visible when Follow mode is active.")
471 (defvar follow-deactive-menu nil
472 "The menu visible when Follow mode is deactivated.")
474 (defvar follow-inside-post-command-hook nil
475 "Non-nil when inside Follow modes `post-command-hook'.
476 Used by `follow-window-size-change'.")
478 (defvar follow-windows-start-end-cache nil
479 "Cache used by `follow-window-start-end'.")
484 (eval-when-compile (require 'reporter
))
486 (defun follow-submit-feedback ()
487 "Submit feedback on Follow mode to the author: andersl@csd.uu.se"
490 (and (y-or-n-p "Do you really want to submit a report on Follow mode? ")
491 (reporter-submit-bug-report
492 "Anders Lindgren <andersl@csd.uu.se>"
495 post-command-idle-hook
497 window-size-change-functions
498 window-scroll-functions
502 follow-intercept-processes
503 follow-avoid-tail-recenter-p
504 follow-process-filter-alist
)
509 "(I have read the section on how to report bugs in the "
511 "Even though I know you are busy, I thought you might "
512 "want to know...\n\n"))))
517 ;; This inline function must be as small as possible!
518 ;; Maybe we should define a macro which expands to nil if
519 ;; the variable is not set.
521 (defsubst follow-debug-message
(&rest args
)
522 "Like message, but only active when `follow-debug' is non-nil."
523 (if (and (boundp 'follow-debug
) follow-debug
)
524 (apply 'message args
)))
530 ;;; Define keys for the follow-mode minor mode map and replace some
531 ;;; functions in the global map. All `follow' mode special functions
532 ;;; can be found on (the somewhat cumbersome) "C-c . <key>"
533 ;;; (Control-C dot <key>). (As of Emacs 19.29 the keys
534 ;;; C-c <punctuation character> are reserved for minor modes.)
536 ;;; To change the prefix, redefine `follow-mode-prefix' before
537 ;;; `follow' is loaded, or see the section on `follow-mode-hook'
538 ;;; above for an example of how to bind the keys the way you like.
540 ;;; Please note that the keymap is defined the first time this file is
541 ;;; loaded. Also note that the only legal way to manipulate the
542 ;;; keymap is to use `define-key'. Don't change it using `setq' or
548 (setq follow-mode-map
(make-sparse-keymap))
549 (let ((map (make-sparse-keymap)))
550 (define-key map
"\C-v" 'follow-scroll-up
)
551 (define-key map
"\M-v" 'follow-scroll-down
)
552 (define-key map
"v" 'follow-scroll-down
)
553 (define-key map
"1" 'follow-delete-other-windows-and-split
)
554 (define-key map
"b" 'follow-switch-to-buffer
)
555 (define-key map
"\C-b" 'follow-switch-to-buffer-all
)
556 (define-key map
"\C-l" 'follow-recenter
)
557 (define-key map
"<" 'follow-first-window
)
558 (define-key map
">" 'follow-last-window
)
559 (define-key map
"n" 'follow-next-window
)
560 (define-key map
"p" 'follow-previous-window
)
562 (define-key follow-mode-map follow-mode-prefix map
)
564 ;; Replace the standard `end-of-buffer', when in Follow Mode. (I
565 ;; don't see the point in trying to replace every function which
566 ;; could be enhanced in Follow mode. End-of-buffer is a special
567 ;; case since it is very simple to define and it greatly enhances
568 ;; the look and feel of Follow mode.)
570 ;; (The function `substitute-key-definition' does not work
571 ;; in all versions of Emacs.)
575 (let ((old (car pair
))
577 (mapcar (function (lambda (key)
578 (define-key follow-mode-map key new
)))
579 (where-is-internal old global-map
)))))
580 '((end-of-buffer . follow-end-of-buffer
)
581 (fkey-end-of-buffer . follow-end-of-buffer
)))
587 (if (not follow-emacs-version-xemacs-p
)
592 (let ((menumap (funcall (symbol-function 'make-sparse-keymap
)
602 (setq count
(+ count
1))
603 (intern (format "separator-%d" count
)))))
604 (define-key menumap
(vector id
) item
)
605 (or (eq id
'follow-mode
)
606 (put id
'menu-enable
'follow-mode
))))
608 '(("Toggle Follow mode" . follow-mode
)
610 ("Recenter" . follow-recenter
)
612 ("Previous Window" . follow-previous-window
)
613 ("Next Windows" . follow-next-window
)
614 ("Last Window" . follow-last-window
)
615 ("First Window" . follow-first-window
)
617 ("Switch To Buffer (all windows)"
618 . follow-switch-to-buffer-all
)
619 ("Switch To Buffer" . follow-switch-to-buffer
)
621 ("Delete Other Windows and Split"
622 . follow-delete-other-windows-and-split
)
624 ("Scroll Down" . follow-scroll-down
)
625 ("Scroll Up" . follow-scroll-up
)))
627 ;; If there is a `tools' meny, we use it. However, we can't add a
628 ;; minor-mode specific item to it (it's broken), so we make the
629 ;; contents ghosted when not in use, and add ourselves to the
630 ;; global map. If no `tools' menu is present, just make a
631 ;; top-level menu visible when the mode is activated.
633 (let ((tools-map (lookup-key (current-global-map) [menu-bar tools
]))
635 (if (sequencep tools-map
)
637 ;; Find the last entry in the menu and store it in `last'.
640 (setq last
(or (cdr-safe
647 (funcall (symbol-function 'define-key-after
)
648 tools-map
[separator-follow
] '("--") last
)
649 (funcall (symbol-function 'define-key-after
)
650 tools-map
[follow] (cons "Follow" menumap)
652 ;; Didn't find the last item, Adding to the top of
653 ;; tools. (This will probably never happend...)
654 (define-key (current-global-map) [menu-bar tools follow]
655 (cons "Follow" menumap))))
656 ;; No tools menu, add "Follow" to the menubar.
657 (define-key follow-mode-map [menu-bar follow]
658 (cons "Follow" menumap)))))
664 ;; place the menu in the `Tools' menu.
665 (let ((menu '("Follow"
666 :filter follow-menu-filter
667 ["Scroll Up" follow-scroll-up t]
668 ["Scroll Down" follow-scroll-down t]
669 ["Delete Other Windows and Split"
670 follow-delete-other-windows-and-split t]
671 ["Switch To Buffer" follow-switch-to-buffer t]
672 ["Switch To Buffer (all windows)"
673 follow-switch-to-buffer-all t]
674 ["First Window" follow-first-window t]
675 ["Last Window" follow-last-window t]
676 ["Next Windows" follow-next-window t]
677 ["Previous Window" follow-previous-window t]
678 ["Recenter" follow-recenter t]
679 ["Deactivate" follow-mode t])))
681 ;; Why not just `(set-buffer-menubar current-menubar)'? The
682 ;; question is a very good question. The reason is that under
683 ;; Emacs 19, neither `set-buffer-menubar' nor
684 ;; `current-menubar' is defined, hence the byte-compiler will
686 (funcall (symbol-function 'set-buffer-menubar)
687 (symbol-value 'current-menubar))
688 (funcall (symbol-function 'add-submenu) '("Tools") menu))
690 ;; When the mode is not activated, only one item is visible:
692 (defun follow-menu-filter (menu)
695 '(["Activate " follow-mode t]))))))
698 ;;; Register the follow mode keymap.
699 (or (assq 'follow-mode minor-mode-map-alist)
700 (setq minor-mode-map-alist
701 (cons (cons 'follow-mode follow-mode-map) minor-mode-map-alist)))
706 (let ((cmds follow-cache-command-list))
708 (put (car cmds) 'follow-mode-use-cache t)
709 (setq cmds (cdr cmds))))
716 (defun turn-on-follow-mode ()
717 "Turn on Follow mode. Please see the function `follow-mode'."
723 (defun turn-off-follow-mode ()
724 "Turn off Follow mode. Please see the function `follow-mode'."
730 (defun follow-mode (arg)
731 "Minor mode which combines windows into one tall virtual window.
733 The feeling of a \"virtual window\" has been accomplished by the use
734 of two major techniques:
736 * The windows always displays adjacent sections of the buffer.
737 This means that whenever one window is moved, all the
738 others will follow. (Hence the name Follow Mode.)
740 * Should the point (cursor) end up outside a window, another
741 window displaying that point is selected, if possible. This
742 makes it possible to walk between windows using normal cursor
745 Follow mode comes to its prime when used on a large screen and two
746 side-by-side window are used. The user can, with the help of Follow
747 mode, use two full-height windows as though they would have been
748 one. Imagine yourself editing a large function, or section of text,
749 and being able to use 144 lines instead of the normal 72... (your
752 To split one large window into two side-by-side windows, the commands
753 `\\[split-window-horizontally]' or \
754 `M-x follow-delete-other-windows-and-split' can be used.
756 Only windows displayed in the same frame follow each-other.
758 If the variable `follow-intercept-processes' is non-nil, Follow mode
759 will listen to the output of processes and redisplay accordingly.
760 \(This is the default.)
762 When Follow mode is switched on, the hook `follow-mode-hook'
763 is called. When turned off, `follow-mode-off-hook' is called.
765 Keys specific to Follow mode:
768 (make-local-variable 'follow-mode)
769 (put 'follow-mode 'permanent-local t)
770 (let ((follow-mode-orig follow-mode))
774 (> (prefix-numeric-value arg) 0)))
775 (if (and follow-mode follow-intercept-processes)
776 (follow-intercept-process-output))
777 (cond ((and follow-mode (not follow-mode-orig)) ; On
778 ;; XEmacs: If this is non-nil, the window will scroll before
779 ;; the point will have a chance to get into the next window.
780 (if (boundp 'scroll-on-clipped-lines)
781 (set 'scroll-on-clipped-lines nil))
782 (force-mode-line-update)
783 (add-hook 'post-command-hook 'follow-post-command-hook t)
784 (if (boundp 'post-command-idle-hook)
785 (add-hook 'post-command-idle-hook
786 'follow-avoid-tail-recenter t))
787 (run-hooks 'follow-mode-hook))
789 ((and (not follow-mode) follow-mode-orig) ; Off
790 (force-mode-line-update)
791 (run-hooks 'follow-mode-off-hook)))))
794 ;; Register follow-mode as a minor mode.
796 (if (fboundp 'add-minor-mode)
798 (funcall (symbol-function 'add-minor-mode)
799 'follow-mode 'follow-mode-line-text)
800 (or (assq 'follow-mode minor-mode-alist)
801 (setq minor-mode-alist
802 (cons '(follow-mode follow-mode-line-text) minor-mode-alist))))
807 ;; This will start follow-mode whenever a new file is loaded, if
808 ;; the variable `follow-auto' is non-nil.
810 (add-hook 'find-file-hooks 'follow-find-file-hook t)
812 (defun follow-find-file-hook ()
813 "Find-file hook for Follow Mode. See the variable `follow-auto'."
814 (if follow-auto (follow-mode t)))
821 ;;; User functions usable when in Follow mode.
826 ;; `scroll-up' and `-down', but for windows in Follow Mode.
828 ;; Almost like the real thing, excpet when the cursor ends up outside
829 ;; the top or bottom... In our case however, we end up outside the
830 ;; window and hence we are recenterd. Should we let `recenter' handle
831 ;; the point position we would never leave the selected window. To do
832 ;; it ourselves we would need to do our own redisplay, which is easier
833 ;; said than done. (Why didn't I do a real display abstraction from
836 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
837 ;; our post-command-hook will move our windows back into the old
838 ;; position... (This would also be corrected if we would have had a
839 ;; good redisplay abstraction.)
841 (defun follow-scroll-up (&optional arg)
842 "Scroll text in a Follow Mode window chain up.
844 If called with no ARG, the `next-screen-context-lines' last lines of
845 the bottom window in the chain will be visible in the top window.
847 If called with an argument, scroll ARG lines up.
848 Negative ARG means scroll downward.
850 Works like `scroll-up' when not in Follow Mode."
852 (cond ((not (and (boundp 'follow-mode) follow-mode))
855 (save-excursion (scroll-up arg))
856 (setq follow-internal-force-redisplay t))
858 (let* ((windows (follow-all-followers))
859 (end (window-end (car (reverse windows)))))
860 (if (eq end (point-max))
861 (signal 'end-of-buffer nil)
862 (select-window (car windows))
864 (vertical-motion (- next-screen-context-lines))
865 (set-window-start (car windows) (point)))))))
868 (defun follow-scroll-down (&optional arg)
869 "Scroll text in a Follow Mode window chain down.
871 If called with no ARG, the `next-screen-context-lines' top lines of
872 the top window in the chain will be visible in the bottom window.
874 If called with an argument, scroll ARG lines down.
875 Negative ARG means scroll upward.
877 Works like `scroll-up' when not in Follow Mode."
879 (cond ((not (and (boundp 'follow-mode) follow-mode))
882 (save-excursion (scroll-down arg)))
884 (let* ((windows (follow-all-followers))
885 (win (car (reverse windows)))
886 (start (window-start (car windows))))
887 (if (eq start (point-min))
888 (signal 'beginning-of-buffer nil)
891 (vertical-motion (- (- (window-height win)
893 next-screen-context-lines)))
894 (set-window-start win (point))
896 (vertical-motion (- next-screen-context-lines 1))
897 (setq follow-internal-force-redisplay t))))))
903 (defun follow-delete-other-windows-and-split (&optional arg)
904 "Create two side by side windows and enter Follow Mode.
906 Execute this command to display as much as possible of the text
907 in the selected window. All other windows, in the current
908 frame, are deleted and the selected window is split in two
909 side-by-side windows. Follow Mode is activated, hence the
910 two windows always will display two successive pages.
911 \(If one window is moved, the other one will follow.)
913 If ARG is positive, the leftmost window is selected. If it negative,
914 the rightmost is selected. If ARG is nil, the leftmost window is
915 selected if the original window is the first one in the frame.
917 To bind this command to a hotkey, place the following line
918 in your `~/.emacs' file, replacing [f7] by your favourite key:
919 (global-set-key [f7] 'follow-delete-other-windows-and-split)"
921 (let ((other (or (and (null arg)
922 (not (eq (selected-window)
923 (frame-first-window (selected-frame)))))
925 (< (prefix-numeric-value arg) 0))))
926 (start (window-start)))
927 (delete-other-windows)
928 (split-window-horizontally)
932 (set-window-start (selected-window) start)
933 (setq follow-internal-force-redisplay t)))
936 (defun follow-switch-to-buffer (buffer)
937 "Show BUFFER in all windows in the current Follow Mode window chain."
938 (interactive "BSwitch to Buffer: ")
939 (let ((orig-window (selected-window))
940 (windows (follow-all-followers)))
942 (select-window (car windows))
943 (switch-to-buffer buffer)
944 (setq windows (cdr windows)))
945 (select-window orig-window)))
948 (defun follow-switch-to-buffer-all (&optional buffer)
949 "Show BUFFER in all windows on this frame.
950 Defaults to current buffer."
951 (interactive (list (read-buffer "Switch to Buffer: "
953 (or buffer (setq buffer (current-buffer)))
954 (let ((orig-window (selected-window)))
959 (switch-to-buffer buffer))))
960 (select-window orig-window)
964 (defun follow-switch-to-current-buffer-all ()
965 "Show current buffer in all windows on this frame, and enter Follow Mode.
967 To bind this command to a hotkey place the following line
968 in your `~/.emacs' file:
969 (global-set-key [f7] 'follow-switch-to-current-buffer-all)"
971 (or (and (boundp 'follow-mode) follow-mode)
973 (follow-switch-to-buffer-all))
978 ;; Note, these functions are not very useful, atleast not unless you
979 ;; rebind the rather cumbersome key sequence `C-c . p'.
981 (defun follow-next-window ()
982 "Select the next window showing the same buffer."
984 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
986 (select-window (car succ))
987 (error "%s" "No more windows"))))
990 (defun follow-previous-window ()
991 "Select the previous window showing the same buffer."
993 (let ((pred (car (follow-split-followers (follow-all-followers)))))
995 (select-window (car pred))
996 (error "%s" "No more windows"))))
999 (defun follow-first-window ()
1000 "Select the first window in the frame showing the same buffer."
1002 (select-window (car (follow-all-followers))))
1005 (defun follow-last-window ()
1006 "Select the last window in the frame showing the same buffer."
1008 (select-window (car (reverse (follow-all-followers)))))
1013 (defun follow-recenter (&optional arg)
1014 "Recenter the middle window around point.
1015 Rearrange all other windows around the middle window.
1017 With a positive argument, place the current line ARG lines
1018 from the top. With a negative, place it -ARG lines from the
1023 (arg (prefix-numeric-value arg)))
1025 ;; Recenter relative to the top.
1027 (follow-first-window)
1030 ;; Recenter relative to the bottom.
1031 (follow-last-window)
1034 ;; Otherwise, our post-command-hook will move the window
1036 (setq follow-internal-force-redisplay t)))
1037 ;; Recenter in the middle.
1038 (let* ((dest (point))
1039 (windows (follow-all-followers))
1040 (win (nth (/ (- (length windows) 1) 2) windows)))
1044 ;;(setq follow-internal-force-redisplay t)
1048 (defun follow-redraw ()
1049 "Arrange windows displaying the same buffer in successor order.
1050 This function can be called even if the buffer is not in Follow mode.
1052 Hopefully, there should be no reason to call this function when in
1053 Follow mode since the windows should always be aligned."
1061 (defun follow-end-of-buffer (&optional arg)
1062 "Move point to the end of the buffer, Follow Mode style.
1064 If the end is not visible, it will be displayed in the last possible
1065 window in the Follow Mode window chain.
1067 The mark is left at the previous position. With arg N, put point N/10
1068 of the way from the true end."
1070 (let ((followers (follow-all-followers))
1073 (select-window (car (reverse followers))))
1074 ((follow-select-if-end-visible
1075 (follow-windows-start-end followers)))
1077 (select-window (car (reverse followers)))))
1079 (end-of-buffer arg)))
1087 ;;;; The display routines
1089 ;;{{{ Information gathering functions
1091 (defun follow-all-followers (&optional testwin)
1092 "Return all windows displaying the same buffer as the TESTWIN.
1093 The list contains only windows displayed in the same frame as TESTWIN.
1094 If TESTWIN is nil the selected window is used."
1095 (or (and testwin (window-live-p testwin))
1096 (setq testwin (selected-window)))
1097 (let* ((top (frame-first-window (window-frame testwin)))
1101 (buffer (window-buffer testwin)))
1102 (while (and (not done) win)
1103 (if (eq (window-buffer win) buffer)
1104 (setq windows (cons win windows)))
1105 (setq win (next-window win 'not))
1108 (nreverse windows)))
1111 (defun follow-split-followers (windows &optional win)
1112 "Split the WINDOWS into the sets: predecessors and successors.
1113 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
1114 from the selected window."
1116 (setq win (selected-window)))
1118 (while (not (eq (car windows) win))
1119 (setq pred (cons (car windows) pred))
1120 (setq windows (cdr windows)))
1121 (cons pred (cdr windows))))
1124 ;; Try to optimize this function for speed!
1126 (defun follow-calc-win-end (&optional win)
1127 "Calculate the presumed window end for WIN.
1129 Actually, the position returned is the start of the next
1130 window, normally is the end plus one.
1132 If WIN is nil, the selected window is used.
1134 Returns (end-pos end-of-buffer-p)"
1135 (if follow-emacs-version-xemacs-p
1136 ;; XEmacs can calculate the end of the window by using
1137 ;; the 'guarantee options. GOOD!
1138 (let ((end (window-end win t)))
1139 (if (= end (funcall (symbol-function 'point-max)
1140 (window-buffer win)))
1142 (list (+ end 1) nil)))
1143 ;; Emacs 19: We have to calculate the end by ourselves.
1144 ;; This code works on both XEmacs and Emacs 19, but now
1145 ;; that XEmacs has got custom-written code, this could
1146 ;; be optimized for Emacs 19.
1147 (let ((orig-win (and win (selected-window)))
1150 (if win (select-window win))
1153 (goto-char (window-start))
1154 (setq height (- (window-height) 1))
1157 (not (= height (vertical-motion height)))
1159 ;; Fix a mis-feature in `vertical-motion':
1160 ;; The start of the window is assumed to
1161 ;; coinside with the start of a line.
1162 (narrow-to-region (point) (point-max))
1163 (not (= height (vertical-motion height))))))
1164 (list (point) buffer-end-p))
1166 (select-window orig-win))))))
1169 ;; Can't use `save-window-excursion' since it triggers a redraw.
1170 (defun follow-calc-win-start (windows pos win)
1171 "Calculate where WIN will start if the first in WINDOWS start at POS.
1173 If WIN is nil the point below all windows is returned."
1175 (while (and windows (not (eq (car windows) win)))
1176 (setq start (window-start (car windows)))
1177 (set-window-start (car windows) pos 'noforce)
1178 (setq pos (car (inline (follow-calc-win-end (car windows)))))
1179 (set-window-start (car windows) start 'noforce)
1180 (setq windows (cdr windows)))
1184 ;; The result from `follow-windows-start-end' is cached when using
1185 ;; a handful simple commands, like cursor movement commands.
1187 (defsubst follow-cache-valid-p (windows)
1188 "Test if the cached value of `follow-windows-start-end' can be used.
1189 Note that this handles the case when the cache has been set to nil."
1191 (cache follow-windows-start-end-cache))
1192 (while (and res windows cache)
1193 (setq res (and (eq (car windows)
1195 (eq (window-start (car windows))
1196 (car (cdr (car cache))))))
1197 (setq windows (cdr windows))
1198 (setq cache (cdr cache)))
1199 (and res (null windows) (null cache))))
1202 (defsubst follow-invalidate-cache ()
1203 "Force `follow-windows-start-end' to recalculate the end of the window."
1204 (setq follow-windows-start-end-cache nil))
1207 ;; Build a list of windows and their start and end positions.
1208 ;; Useful to avoid calculating start/end position whenever they are needed.
1209 ;; The list has the format:
1210 ;; ((Win Start End End-of-buffer-visible-p) ...)
1212 ;; Used to have a `save-window-excursion', but it obviously triggered
1213 ;; redraws of the display. Check if I used it for anything.
1216 (defun follow-windows-start-end (windows)
1217 "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
1218 (if (follow-cache-valid-p windows)
1219 follow-windows-start-end-cache
1220 (let ((win-start-end '())
1221 (orig-win (selected-window)))
1223 (select-window (car windows))
1225 (cons (cons (car windows)
1226 (cons (window-start)
1227 (follow-calc-win-end)))
1229 (setq windows (cdr windows)))
1230 (select-window orig-win)
1231 (setq follow-windows-start-end-cache (nreverse win-start-end))
1232 follow-windows-start-end-cache)))
1235 (defsubst follow-pos-visible (pos win win-start-end)
1236 "Non-nil when POS is visible in WIN."
1237 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
1238 (and (>= pos (car wstart-wend-bend))
1239 (or (< pos (car (cdr wstart-wend-bend)))
1240 (nth 2 wstart-wend-bend)))))
1243 ;; By `aligned' we mean that for all adjecent windows, the end of the
1244 ;; first is equal with the start of the successor. The first window
1245 ;; should start at a full screen line.
1247 (defsubst follow-windows-aligned-p (win-start-end)
1248 "Non-nil if the follower WINDOWS are aligned."
1251 (goto-char (window-start (car (car win-start-end))))
1254 (vertical-motion 0 (car (car win-start-end)))
1255 (setq res (eq (point) (window-start (car (car win-start-end)))))))
1256 (while (and res (cdr win-start-end))
1257 ;; At least two followers left
1258 (setq res (eq (car (cdr (cdr (car win-start-end))))
1259 (car (cdr (car (cdr win-start-end))))))
1260 (setq win-start-end (cdr win-start-end)))
1264 ;; Check if the point is visible in all windows. (So that
1265 ;; no one will be recentered.)
1267 (defun follow-point-visible-all-windows-p (win-start-end)
1268 "Non-nil when the window-point is visible in all windows."
1270 (while (and res win-start-end)
1271 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
1272 (car (car win-start-end))
1274 (setq win-start-end (cdr win-start-end)))
1278 ;; Make sure WIN always starts at the beginning of an whole screen
1279 ;; line. If WIN is not aligned the start is updated which probably
1280 ;; will lead to a redisplay of the screen later on.
1282 ;; This is used with the first window in a follow chain. The reason
1283 ;; is that we want to detect that the point is outside the window.
1284 ;; (Without the update, the start of the window will move as the
1285 ;; user presses BackSpace, and the other window redisplay routines
1286 ;; will move the start of the window in the wrong direction.)
1288 (defun follow-update-window-start (win)
1289 "Make sure that the start of WIN starts at a full screen line."
1291 (goto-char (window-start win))
1294 (vertical-motion 0 win)
1295 (if (eq (point) (window-start win))
1297 (vertical-motion 1 win)
1298 (set-window-start win (point) 'noforce)))))
1301 ;;{{{ Selection functions
1303 ;; Make a window in WINDOWS selected if it currently
1304 ;; is displaying the position DEST.
1306 ;; We don't select a window if it just has been moved.
1308 (defun follow-select-if-visible (dest win-start-end)
1309 "Select and return a window, if DEST is visible in it.
1310 Return the selected window."
1312 (while (and (not win) win-start-end)
1313 ;; Don't select a window which was just moved. This makes it
1314 ;; possible to later select the last window after a `end-of-buffer'
1316 (if (follow-pos-visible dest (car (car win-start-end)) win-start-end)
1318 (setq win (car (car win-start-end)))
1319 (select-window win)))
1320 (setq win-start-end (cdr win-start-end)))
1324 ;; Lets select a window showing the end. Make sure we only select it if it
1325 ;; it wasn't just moved here. (i.e. M-> shall not unconditionally place
1326 ;; the point in the selected window.)
1328 ;; (Compability cludge: in Emacs 19 `window-end' is equal to `point-max';
1329 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1330 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1332 (defun follow-select-if-end-visible (win-start-end)
1333 "Select and return a window, if end is visible in it."
1335 (while (and (not win) win-start-end)
1336 ;; Don't select a window which was just moved. This makes it
1337 ;; possible to later select the last window after a `end-of-buffer'
1339 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1340 (nth 3 (car win-start-end))
1341 (eq (point-max) (min (point-max)
1342 (window-end (car (car win-start-end))))))
1344 (setq win (car (car win-start-end)))
1345 (select-window win)))
1346 (setq win-start-end (cdr win-start-end)))
1350 ;; Select a window which will display the point if the windows would
1351 ;; be redisplayed with the first window fixed. This is useful for
1352 ;; example when the user has pressed return at the bottom of a window
1353 ;; as the point is not visible in any window.
1355 (defun follow-select-if-visible-from-first (dest windows)
1356 "Select and return a window with DEST, if WINDOWS are redrawn from top."
1360 (goto-char (window-start (car windows)))
1361 ;; Make sure the line start in the beginning of a real screen
1363 (vertical-motion 0 (car windows))
1364 (if (< dest (point))
1365 ;; Above the start, not visible.
1367 ;; At or below the start. Check the windows.
1368 (save-window-excursion
1369 (while (and (not win) windows)
1370 (set-window-start (car windows) (point) 'noforce)
1371 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1372 (goto-char (car end-pos-end-p))
1373 ;; Visible, if dest above end, or if eob is visible inside
1375 (if (or (car (cdr end-pos-end-p))
1377 (setq win (car windows))
1378 (setq windows (cdr windows)))))))
1380 (select-window win))
1387 ;; Redraw all the windows on the screen, starting with the top window.
1388 ;; The window used as as marker is WIN, or the selcted window if WIN
1391 (defun follow-redisplay (&optional windows win)
1392 "Reposition the WINDOWS around WIN.
1393 Should the point be too close to the roof we redisplay everything
1394 from the top. WINDOWS should contain a list of windows to
1395 redisplay, it is assumed that WIN is a member of the list.
1396 Should WINDOWS be nil, the windows displaying the
1397 same buffer as WIN, in the current frame, are used.
1398 Should WIN be nil, the selected window is used."
1400 (setq win (selected-window)))
1402 (setq windows (follow-all-followers win)))
1403 (follow-downward windows (follow-calculate-first-window-start windows win)))
1406 ;; Redisplay a chain of windows. Start every window directly after the
1407 ;; end of the previous window, to make sure long lines are displayed
1410 (defun follow-downward (windows pos)
1411 "Redisplay all WINDOWS starting at POS."
1413 (set-window-start (car windows) pos)
1414 (setq pos (car (follow-calc-win-end (car windows))))
1415 (setq windows (cdr windows))))
1418 ;;(defun follow-downward (windows pos)
1419 ;; "Redisplay all WINDOWS starting at POS."
1422 ;; (setq p (window-point (car windows)))
1423 ;; (set-window-start (car windows) pos)
1424 ;; (set-window-point (car windows) (max p pos))
1425 ;; (setq pos (car (follow-calc-win-end (car windows))))
1426 ;; (setq windows (cdr windows)))))
1429 ;; Return the start of the first window.
1431 ;; First, estimate the position. It the value is not perfect (i.e. we
1432 ;; have somewhere splited a line between windows) we try to enhance
1435 ;; The guess is always perfect if no long lines is split between
1438 ;; The worst case peformace of probably very bad, but it is very
1439 ;; unlikely that we ever will miss the correct start by more than one
1442 (defun follow-calculate-first-window-start (windows &optional win start)
1443 "Calculate the start of the first window.
1445 WINDOWS is a chain of windows to work with. WIN is the window
1446 to recenter around. It is assumed that WIN starts at position
1449 (setq win (selected-window)))
1451 (setq start (window-start win)))
1452 (let ((guess (follow-estimate-first-window-start windows win start)))
1455 ;; The guess wasn't exact, try to enhance it.
1456 (let ((win-start (follow-calc-win-start windows (cdr guess) win)))
1457 (cond ((= win-start start)
1458 (follow-debug-message "exact")
1460 ((< win-start start)
1461 (follow-debug-message "above")
1462 (follow-calculate-first-window-start-from-above
1463 windows (cdr guess) win start))
1465 (follow-debug-message "below")
1466 (follow-calculate-first-window-start-from-below
1467 windows (cdr guess) win start)))))))
1470 ;; `exact' is disabled due to XEmacs and fonts of variable
1472 (defun follow-estimate-first-window-start (windows win start)
1473 "Estimate the position of the first window.
1475 Returns (EXACT . POS). If EXACT is non-nil, POS is the starting
1476 position of the first window. Otherwise it is a good guess."
1477 (let ((pred (car (follow-split-followers windows win)))
1481 ;(setq exact (bolp))
1482 (vertical-motion 0 win)
1484 (vertical-motion (- 1 (window-height (car pred))) (car pred))
1487 (setq pred (cdr pred)))
1488 (cons exact (point)))))
1491 ;; Find the starting point, start at GUESS and search downward.
1492 ;; The returned point is always a point below GUESS.
1494 (defun follow-calculate-first-window-start-from-above
1495 (windows guess win start)
1502 (if (not (= (vertical-motion 1 (car windows)) 1))
1503 ;; Hit bottom! (Can we really do this?)
1504 ;; We'll keep it, since it ensures termination.
1507 (setq res (point-max)))
1508 (setq win-start (follow-calc-win-start windows (point) win))
1509 (if (>= win-start start)
1512 (setq res (point))))))
1516 ;; Find the starting point, start at GUESS and search upward. Return
1517 ;; a point on the same line as GUESS, or above.
1519 ;; (Is this ever used? I must make sure it works just in case it is
1522 (defun follow-calculate-first-window-start-from-below
1523 (windows guess &optional win start)
1524 (setq win (or win (selected-window)))
1525 (setq start (or start (window-start win)))
1530 ;; Always calculate what happend when no line is displayed in the first
1531 ;; window. (The `previous' res is needed below!)
1533 (vertical-motion 0 (car windows))
1536 (if (not (= (vertical-motion -1 (car windows)) -1))
1540 (setq res (point-min)))
1541 (setq win-start (follow-calc-win-start windows (point) win))
1542 (cond ((= win-start start) ; Perfect match, use this value
1545 ((< win-start start) ; Walked to far, use preious result
1547 (t ; Store result for next iteration
1548 (setq res (point))))))
1552 ;;{{{ Avoid tail recenter
1554 ;; This sets the window internal flag `force_start'. The effect is that
1555 ;; windows only displaying the tail isn't recentered.
1556 ;; Has to be called before every redisplay... (Great isn't it?)
1558 ;; XEmacs doesn't recenter the tail, GOOD!
1560 ;; A window displaying only the tail, is a windows whose
1561 ;; window-start position is equal to (point-max) of the buffer it
1564 ;; This function is also added to `post-command-idle-hook', introduced
1565 ;; in Emacs 19.30. This is needed since the vaccine injected by the
1566 ;; call from `post-command-hook' only works until the next redisplay.
1567 ;; It is possible that the functions in the `post-command-idle-hook'
1568 ;; can cause a redisplay, and hence a new vaccine is needed.
1570 ;; Sometimes, calling this function could actually cause a redisplay,
1571 ;; especially if it is placed in the debug filter section. I must
1572 ;; investigate this further...
1574 (defun follow-avoid-tail-recenter (&rest rest)
1575 "Make sure windows displaying the end of a buffer aren't recentered.
1577 This is done by reading and rewriting the start positon of
1578 non-first windows in Follow Mode."
1579 (if follow-avoid-tail-recenter-p
1580 (let* ((orig-buffer (current-buffer))
1581 (top (frame-first-window (selected-frame)))
1583 (who '()) ; list of (buffer . frame)
1585 pair) ; (buffer . frame)
1586 ;; If the only window in the frame is a minibuffer
1587 ;; window, `next-window' will never find it again...
1588 (if (window-minibuffer-p top)
1590 (while ;; look, no body!
1592 (setq start (window-start win))
1593 (set-buffer (window-buffer win))
1594 (setq pair (cons (window-buffer win) (window-frame win)))
1595 (if (member pair who)
1596 (if (and (boundp 'follow-mode) follow-mode
1597 (eq (point-max) start))
1598 ;; Write the same window start back, but don't
1599 ;; set the NOFORCE flag.
1600 (set-window-start win start))
1601 (setq who (cons pair who)))
1602 (setq win (next-window win 'not t))
1603 (not (eq win top)))) ;; Loop while this is true.
1604 (set-buffer orig-buffer)))))
1609 ;;{{{ Post Command Hook
1611 ;;; The magic little box. This function is called after every command.
1613 ;; This is not as complicated as it seems. It is simply a list of common
1614 ;; display situations and the actions to take, plus commands for redrawing
1615 ;; the screen if it should be unaligned.
1617 ;; We divide the check into two parts; whether we are at the end or not.
1618 ;; This is due to the fact that the end can actaually be visible
1619 ;; in several window even though they are aligned.
1621 (defun follow-post-command-hook ()
1622 "Ensure that the windows in Follow mode are adjacent after each command."
1623 (setq follow-inside-post-command-hook t)
1624 (if (or (not (input-pending-p))
1625 ;; Sometimes, in XEmacs, mouse events are not handled
1626 ;; properly by `input-pending-p'. A typical example is
1627 ;; when clicking on a node in `info'.
1628 (and (boundp 'current-mouse-event)
1629 (symbol-value 'current-mouse-event)
1630 (fboundp 'button-event-p)
1631 (funcall (symbol-function 'button-event-p)
1632 (symbol-value 'current-mouse-event))))
1633 ;; Work in the selected window, not in the current buffer.
1634 (let ((orig-buffer (current-buffer))
1635 (win (selected-window)))
1636 (set-buffer (window-buffer win))
1637 (or (and (symbolp this-command)
1638 (get this-command 'follow-mode-use-cache))
1639 (follow-invalidate-cache))
1640 (if (and (boundp 'follow-mode) follow-mode
1641 (not (window-minibuffer-p win)))
1642 ;; The buffer shown in the selected window is in follow
1643 ;; mode, lets find the current state of the display and
1644 ;; cache the result for speed (i.e. `aligned' and `visible'.)
1645 (let* ((windows (inline (follow-all-followers win)))
1647 (win-start-end (inline
1648 (follow-update-window-start (car windows))
1649 (follow-windows-start-end windows)))
1650 (aligned (follow-windows-aligned-p win-start-end))
1651 (visible (follow-pos-visible dest win win-start-end)))
1652 (if (not (and aligned visible))
1653 (follow-invalidate-cache))
1654 (inline (follow-avoid-tail-recenter))
1655 ;; Select a window to display the point.
1656 (or follow-internal-force-redisplay
1658 (if (eq dest (point-max))
1659 ;; We're at the end, we have be be careful since
1660 ;; the display can be aligned while `dest' can
1661 ;; be visible in several windows.
1663 ;; Select the current window, but only when
1664 ;; the display is correct. (When inserting
1665 ;; character in a tail window, the display is
1666 ;; not correct, as they are shown twice.)
1668 ;; Never stick to the current window after a
1669 ;; deletion. The reason is cosmetic, when
1670 ;; typing `DEL' in a window showing only the
1671 ;; end of the file, character are removed
1672 ;; from the window above, which is very
1676 (not (memq this-command
1677 '(backward-delete-char
1678 delete-backward-char
1679 backward-delete-char-untabify
1681 (follow-debug-message "Max: same"))
1682 ;; If the end is visible, and the window
1683 ;; doesn't seems like it just has been moved,
1685 ((follow-select-if-end-visible win-start-end)
1686 (follow-debug-message "Max: end visible")
1690 ;; Just show the end...
1692 (follow-debug-message "Max: default")
1693 (select-window (car (reverse windows)))
1696 (setq aligned nil)))
1698 ;; We're not at the end, here life is much simpler.
1700 ;; This is the normal case!
1701 ;; It should be optimized for speed.
1702 ((and visible aligned)
1703 (follow-debug-message "same"))
1704 ;; Pick a position in any window. If the
1705 ;; display is ok, this will pick the `correct'
1706 ;; window. If the display is wierd do this
1707 ;; anyway, this will be the case after a delete
1708 ;; at the beginning of the window.
1709 ((follow-select-if-visible dest win-start-end)
1710 (follow-debug-message "visible")
1713 ;; Not visible anywhere else, lets pick this one.
1714 ;; (Is this case used?)
1716 (follow-debug-message "visible in selected."))
1718 ((eq dest (point-min))
1719 (follow-debug-message "min")
1720 (select-window (car windows))
1722 (set-window-start (selected-window) (point-min))
1723 (setq win-start-end (follow-windows-start-end windows))
1724 (follow-invalidate-cache)
1727 ;; If we can position the cursor without moving the first
1728 ;; window, do it. This is the case which catches `RET'
1729 ;; at the bottom of a window.
1730 ((follow-select-if-visible-from-first dest windows)
1731 (follow-debug-message "Below first")
1734 (follow-redisplay windows (car windows))
1736 ;; None of the above. For simplicity, we stick to the
1739 (follow-debug-message "None")
1741 (setq aligned nil))))
1742 ;; If a new window has been selected, make sure that the
1743 ;; old is not scrolled when the point is outside the
1745 (or (eq win (selected-window))
1746 (let ((p (window-point win)))
1747 (set-window-start win (window-start win) nil)
1748 (set-window-point win p)))))
1749 ;; Make sure the point is visible in the selected window.
1750 ;; (This could lead to a scroll.)
1752 (follow-pos-visible dest win win-start-end))
1755 (follow-avoid-tail-recenter)
1756 (setq win-start-end (follow-windows-start-end windows))
1757 (follow-invalidate-cache)
1759 ;; Redraw the windows whenever needed.
1760 (if (or follow-internal-force-redisplay
1762 (follow-windows-aligned-p win-start-end)))
1763 (not (inline (follow-point-visible-all-windows-p
1766 (setq follow-internal-force-redisplay nil)
1767 (follow-redisplay windows (selected-window))
1768 (setq win-start-end (follow-windows-start-end windows))
1769 (follow-invalidate-cache)
1770 ;; When the point ends up in another window. This
1771 ;; happends when dest is in the beginning of the
1772 ;; file and the selected window is not the first.
1773 ;; It can also, in rare situations happend when
1774 ;; long lines are used and there is a big
1775 ;; difference between the width of the windows.
1776 ;; (When scrolling one line in a wide window which
1777 ;; will cause a move larger that an entire small
1779 (if (follow-pos-visible dest win win-start-end)
1781 (follow-select-if-visible dest win-start-end)
1784 ;; If the region is visible, make it look good when spanning
1785 ;; multiple windows.
1786 (if (or (and (boundp 'mark-active) (symbol-value 'mark-active))
1787 (and (fboundp 'region-active-p)
1788 (funcall (symbol-function 'region-active-p))))
1789 (follow-maximize-region
1790 (selected-window) windows win-start-end))
1792 (inline (follow-avoid-tail-recenter))
1794 ;;(if (not (follow-windows-aligned-p
1795 ;; (follow-windows-start-end windows)))
1796 ;; (message "follow-mode: windows still unaligend!"))
1799 ;; Buffer not in follow mode:
1800 ;; We still must update the windows displaying the tail so that
1801 ;; Emacs won't recenter them.
1802 (follow-avoid-tail-recenter))
1803 (set-buffer orig-buffer)))
1804 (setq follow-inside-post-command-hook nil))
1809 ;; Tries to make the highlighted area representing the region look
1810 ;; good when spanning several windows.
1812 ;; Not perfect, as the point can't be placed at window end, only at
1813 ;; end-1. This will highlight a little bit in windows above
1816 (defun follow-maximize-region (win windows win-start-end)
1817 "Make a highlighted region stretching multiple windows look good."
1818 (let* ((all (follow-split-followers windows win))
1823 (setq data (assq (car pred) win-start-end))
1824 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1825 (setq pred (cdr pred)))
1827 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1828 (setq succ (cdr succ)))))
1833 ;;;; Scroll-bar support code.
1835 ;;; Why is it needed? Well, if the selected window is in follow mode,
1836 ;;; all its follower stick to it blindly. If one of them is scrolled,
1837 ;;; it immediately returns to the original position when the mouse is
1838 ;;; released. If the selected window is not a follower of the dragged
1839 ;;; window the windows will be unaligned.
1841 ;;; The advices doesn't get compiled. Aestetically, this might be a
1842 ;;; problem but in practical life it isn't.
1844 ;;; Discussion: Now when the other windows in the chain follow the
1845 ;;; dragged, should we really select it?
1847 (cond ((fboundp 'scroll-bar-drag)
1849 ;;; Emacs 19 style scrollbars.
1852 ;; Select the dragged window if it is a follower of the
1855 ;; Generate advices of the form:
1856 ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
1857 ;; "Adviced by `follow-mode'."
1858 ;; (follow-redraw-after-event (ad-get-arg 0)))
1859 (let ((cmds '(scroll-bar-drag
1860 scroll-bar-drag-1 ; Executed at every move.
1861 scroll-bar-scroll-down
1862 scroll-bar-scroll-up
1863 scroll-bar-set-window-start)))
1866 (` (defadvice (, (intern (symbol-name (car cmds))))
1868 (, (intern (concat "follow-" (symbol-name (car cmds)))))
1870 "Adviced by Follow Mode."
1871 (follow-redraw-after-event (ad-get-arg 0)))))
1872 (setq cmds (cdr cmds))))
1875 (defun follow-redraw-after-event (event)
1876 "Adviced by Follow mode."
1878 (let* ((orig-win (selected-window))
1879 (win (nth 0 (funcall
1880 (symbol-function 'event-start) event)))
1881 (fmode (assq 'follow-mode
1882 (buffer-local-variables
1883 (window-buffer win)))))
1884 (if (and fmode (cdr fmode))
1885 ;; The selected window is in follow-mode
1887 ;; Recenter around the dragged window.
1890 (select-window orig-win))))
1894 ((fboundp 'scrollbar-vertical-drag)
1896 ;;; XEmacs style scrollbars.
1899 ;; Advice all scrollbar functions on the form:
1901 ;; (defadvice scrollbar-line-down
1902 ;; (after follow-scrollbar-line-down activate)
1903 ;; (follow-xemacs-scrollbar-support (ad-get-arg 0)))
1905 (let ((cmds '(scrollbar-line-down ; Window
1907 scrollbar-page-down ; Object
1909 scrollbar-to-bottom ; Window
1911 scrollbar-vertical-drag ; Object
1916 (` (defadvice (, (intern (symbol-name (car cmds))))
1918 (, (intern (concat "follow-" (symbol-name (car cmds)))))
1920 "Adviced by `follow-mode'."
1921 (follow-xemacs-scrollbar-support (ad-get-arg 0)))))
1922 (setq cmds (cdr cmds))))
1925 (defun follow-xemacs-scrollbar-support (window)
1926 "Redraw windows showing the same buffer as shown in WINDOW.
1927 WINDOW is either the dragged window, or a cons containing the
1928 window as its first element. This is called while the user drags
1931 WINDOW can be an object or a window."
1935 (setq window (car window)))
1936 (let ((fmode (assq 'follow-mode
1937 (buffer-local-variables
1938 (window-buffer window))))
1939 (orig-win (selected-window)))
1940 (if (and fmode (cdr fmode))
1942 ;; Recenter around the dragged window.
1943 (select-window window)
1945 (select-window orig-win)))))
1949 ;;{{{ Process output
1951 ;;; The following sections installs a spy which listens to process
1952 ;;; output and tries to reposition the windows whose buffers are in
1953 ;;; Follow mode. We play safe as much as possible...
1955 ;;; When follow-mode is activated all active processes are
1956 ;;; intercepted. All new processes which change their filter function
1957 ;;; using `set-process-filter' are also intercepted. The reason is
1958 ;;; that a process can cause a redisplay recentering "tail" windows.
1959 ;;; Note that it doesn't hurt to spy on more processes than needed.
1961 ;;; Technically, we set the process filter to `follow-generic-filter'.
1962 ;;; The original filter is stored in `follow-process-filter-alist'.
1963 ;;; Our generic filter calls the original filter, or inserts the
1964 ;;; output into the buffer, if the buffer originally didn't have an
1965 ;;; output filter. It also makes sure that the windows connected to
1966 ;;; the buffer are aligned.
1968 ;;; Discussion: How do we find processes which don't call
1969 ;;; `set-process-filter'? (How often are processes created in a
1970 ;;; buffer after Follow mode are activated?)
1972 ;;; Discussion: Should we also advice `process-filter' to make our
1973 ;;; filter invisible to others?
1975 ;;{{{ Advice for `set-process-filter'
1977 ;; Do not call this with 'follow-generic-filter as the name of the
1980 (defadvice set-process-filter (before follow-set-process-filter activate)
1981 "Ensure process output will be displayed correctly in Follow Mode buffers.
1983 Follow Mode inserts its own process filter to do its
1984 magic stuff before the real process filter is called."
1985 (if follow-intercept-processes
1987 (setq follow-process-filter-alist
1988 (delq (assq (ad-get-arg 0) follow-process-filter-alist)
1989 follow-process-filter-alist))
1990 (follow-tidy-process-filter-alist)
1991 (cond ((eq (ad-get-arg 1) t))
1992 ((eq (ad-get-arg 1) nil)
1993 (ad-set-arg 1 'follow-generic-filter))
1995 (setq follow-process-filter-alist
1996 (cons (cons (ad-get-arg 0) (ad-get-arg 1))
1997 follow-process-filter-alist))
1998 (ad-set-arg 1 'follow-generic-filter))))))
2001 (defun follow-call-set-process-filter (proc filter)
2002 "Call original `set-process-filter' without the Follow mode advice."
2003 (ad-disable-advice 'set-process-filter 'before
2004 'follow-set-process-filter)
2005 (ad-activate 'set-process-filter)
2007 (set-process-filter proc filter)
2008 (ad-enable-advice 'set-process-filter 'before
2009 'follow-set-process-filter)
2010 (ad-activate 'set-process-filter)))
2013 (defadvice process-filter (after follow-process-filter activate)
2014 "Return the original process filter, not `follow-generic-filter'."
2015 (cond ((eq ad-return-value 'follow-generic-filter)
2016 (setq ad-return-value
2017 (cdr-safe (assq (ad-get-arg 0)
2018 follow-process-filter-alist))))))
2021 (defun follow-call-process-filter (proc)
2022 "Call original `process-filter' without the Follow mode advice."
2023 (ad-disable-advice 'process-filter 'after
2024 'follow-process-filter)
2025 (ad-activate 'process-filter)
2027 (process-filter proc)
2028 (ad-enable-advice 'process-filter 'after
2029 'follow-process-filter)
2030 (ad-activate 'process-filter)))
2033 (defun follow-tidy-process-filter-alist ()
2034 "Remove old processes from `follow-process-filter-alist'."
2035 (let ((alist follow-process-filter-alist)
2039 (if (and (not (memq (process-status (car (car alist)))
2040 '(exit signal closed nil)))
2041 (memq (car (car alist)) ps))
2042 (setq new (cons (car alist) new)))
2043 (setq alist (cdr alist)))
2044 (setq follow-process-filter-alist new)))
2047 ;;{{{ Start/stop interception of processes.
2049 ;; Normally, all new processed are intercepted by our `set-process-filter'.
2050 ;; This is needed to intercept old processed which were started before we were
2051 ;; loaded, and processes we have forgotten by calling
2052 ;; `follow-stop-intercept-process-output'.
2054 (defun follow-intercept-process-output ()
2055 "Intercept all active processes.
2057 This is needed so that Follow Mode can track all display events in the
2058 system. (See `follow-mode')"
2060 (let ((list (process-list)))
2062 (if (eq (process-filter (car list)) 'follow-generic-filter)
2064 ;; The custom `set-process-filter' defined above.
2065 (set-process-filter (car list) (process-filter (car list))))
2066 (setq list (cdr list))))
2067 (setq follow-intercept-processes t))
2070 (defun follow-stop-intercept-process-output ()
2071 "Stop Follow Mode from spying on processes.
2073 All current spypoints are removed and no new will be added.
2075 The effect is that Follow mode won't be able to handle buffers
2076 connected to processes.
2078 The only reason to call this function is if the Follow mode spy filter
2079 would interfere with some other package. If this happens, please
2080 report this using the `follow-submit-feedback' function."
2082 (follow-tidy-process-filter-alist)
2083 (let ((list (process-list)))
2085 (if (eq (process-filter (car list)) 'follow-generic-filter)
2087 (follow-call-set-process-filter
2089 (cdr-safe (assq (car list) follow-process-filter-alist)))
2090 (setq follow-process-filter-alist
2091 (delq (assq (car list) follow-process-filter-alist)
2092 follow-process-filter-alist))))
2093 (setq list (cdr list))))
2094 (setq follow-intercept-processes nil))
2099 ;;; The following section is a naive method to make buffers with
2100 ;;; process output to work with Follow mode. Whenever the start of the
2101 ;;; window displaying the buffer is moved, we moves it back to its
2102 ;;; original position and try to select a new window. (If we fail,
2103 ;;; the normal redisplay functions of Emacs will scroll it right
2106 (defun follow-generic-filter (proc output)
2107 "Process output filter for process connected to buffers in Follow mode."
2108 (let* ((old-buffer (current-buffer))
2109 (orig-win (selected-window))
2110 (buf (process-buffer proc))
2111 (win (and buf (if (eq buf (window-buffer orig-win))
2113 (get-buffer-window buf t))))
2114 (return-to-orig-win (and win (not (eq win orig-win))))
2115 (orig-window-start (and win (window-start win))))
2117 ;; If input is pending, the `sit-for' below won't redraw the
2118 ;; display. In that case, calling `follow-avoid-tail-recenter' may
2119 ;; provoke the process hadnling code to sceduling a redisplay.
2120 ;(or (input-pending-p)
2121 ; (follow-avoid-tail-recenter))
2123 ;; Output the `output'.
2124 (let ((filter (cdr-safe (assq proc follow-process-filter-alist))))
2126 ;; Call the original filter function
2128 (funcall filter proc output))
2130 ;; No filter, but we've got a buffer. Just output into it.
2133 (if (not (marker-buffer (process-mark proc)))
2134 (set-marker (process-mark proc) (point-max)))
2135 (let ((moving (= (point) (process-mark proc)))
2136 (odeactivate (and (boundp 'deactivate-mark)
2137 (symbol-value 'deactivate-mark)))
2138 (old-buffer-read-only buffer-read-only))
2139 (setq buffer-read-only nil)
2141 (goto-char (process-mark proc))
2142 ;; `insert-before-markers' just in case the users next
2144 (insert-before-markers output)
2145 (set-marker (process-mark proc) (point)))
2146 (if moving (goto-char (process-mark proc)))
2147 (if (boundp 'deactivate-mark)
2148 ;; This could really be
2149 ;; (setq deactivate-mark odeactivate)
2150 ;; but this raises an error when compiling on XEmacs.
2151 (funcall (symbol-function 'set)
2152 'deactivate-mark odeactivate))
2153 (setq buffer-read-only old-buffer-read-only)))))
2155 ;; If we're in follow mode, do our stuff. Select a new window and
2156 ;; redisplay. (Actually, it is redundant to check `buf', but I
2157 ;; feel it's more correct.)
2158 (if (and buf win (window-live-p win))
2161 (if (and (boundp 'follow-mode) follow-mode)
2164 (let* ((windows (follow-all-followers win))
2165 (win-start-end (follow-windows-start-end windows))
2166 (new-window-start (window-start win))
2167 (new-window-point (window-point win)))
2169 ;; The window was moved. Move it back and
2170 ;; select a new. If no better could be found,
2171 ;; we stick the the new start position. This
2172 ;; is used when the original process filter
2173 ;; tries to position the cursor at the bottom
2174 ;; of the window. Example: `lyskom'.
2175 ((not (eq orig-window-start new-window-start))
2176 (follow-debug-message "filter: Moved")
2177 (set-window-start win orig-window-start)
2178 (follow-redisplay windows win)
2179 (setq win-start-end (follow-windows-start-end windows))
2180 (follow-select-if-visible new-window-point
2182 (goto-char new-window-point)
2183 (if (eq win (selected-window))
2184 (set-window-start win new-window-start))
2185 (setq win-start-end (follow-windows-start-end windows)))
2186 ;; Stick to this window, if point is visible in it.
2187 ((pos-visible-in-window-p new-window-point)
2188 (follow-debug-message "filter: Visible in window"))
2189 ;; Avoid redisplaying the first window. If the
2190 ;; point is visible at a window below,
2191 ;; redisplay and select it.
2192 ((follow-select-if-visible-from-first
2193 new-window-point windows)
2194 (follow-debug-message "filter: Seen from first")
2195 (follow-redisplay windows (car windows))
2196 (goto-char new-window-point)
2198 (follow-windows-start-end windows)))
2199 ;; None of the above. We stick to the current window.
2201 (follow-debug-message "filter: nothing")))
2203 ;; Here we have slected a window. Make sure the
2204 ;; windows are aligned and the point is visible
2205 ;; in the selected window.
2206 (if (and (not (follow-pos-visible
2207 (point) (selected-window) win-start-end))
2208 (not return-to-orig-win))
2212 (follow-windows-start-end windows))))
2214 (if (or follow-internal-force-redisplay
2215 (not (follow-windows-aligned-p win-start-end)))
2216 (follow-redisplay windows)))))))
2218 ;; return to the original window.
2219 (if return-to-orig-win
2220 (select-window orig-win))
2221 ;; Restore the orignal buffer, unless the filter explicitly
2222 ;; changed buffer or killed the old buffer.
2223 (if (and (eq buf (current-buffer))
2224 (buffer-name old-buffer))
2225 (set-buffer old-buffer)))
2227 (follow-invalidate-cache)
2229 ;; Normally, if the display has been changed, it is redrawn. All
2230 ;; windows showing only the end of a buffer is unconditionally
2231 ;; recentered, we can't prevent it by calling
2232 ;; `follow-avoid-tail-recenter'.
2234 ;; By performing a redisplay on our own, Emacs need not perform
2235 ;; the above described redisplay. (However, bu performing it when
2236 ;; there are input available just seems to make things worse.)
2237 (if (and follow-avoid-tail-recenter-p
2238 (not (input-pending-p)))
2244 ;;{{{ Window size change
2246 ;; In Emacs 19.29, the functions in `window-size-change-functions' are
2247 ;; called every time a window in a frame changes size. Most notably, it
2248 ;; is called after the frame has been resized.
2250 ;; We basically call our post-command-hook for every buffer which is
2251 ;; visible in any window in the resized frame, which is in follow-mode.
2253 ;; Since this function can be called indirectly from
2254 ;; `follow-post-command-hook' we have a potential infinite loop. We
2255 ;; handle this problem by simply not doing anything at all in this
2256 ;; situation. The variable `follow-inside-post-command-hook' contains
2257 ;; information about whether the execution actually is inside the
2258 ;; post-command-hook or not.
2260 (if (boundp 'window-size-change-functions)
2261 (add-hook 'window-size-change-functions 'follow-window-size-change))
2264 (defun follow-window-size-change (frame)
2265 "Redraw all windows in FRAME, when in Follow mode."
2266 ;; Below, we call `post-command-hook'. This makes sure that we
2267 ;; doesn't start a mutally recursive endless loop.
2268 (if follow-inside-post-command-hook
2271 (orig-window (selected-window))
2272 (orig-buffer (current-buffer))
2273 (orig-frame (selected-frame))
2276 (select-frame frame)
2281 (setq buf (window-buffer win))
2282 (if (memq buf buffers)
2285 (if (and (boundp 'follow-mode)
2288 (setq windows (follow-all-followers win))
2289 (if (memq orig-window windows)
2291 ;; Make sure we're redrawing around the
2294 ;; We must be really careful not to do this
2295 ;; when we are (indirectly) called by
2296 ;; `post-command-hook'.
2297 (select-window orig-window)
2298 (follow-post-command-hook)
2299 (setq orig-window (selected-window)))
2300 (follow-redisplay windows win))
2301 (setq buffers (cons buf buffers))))))))
2302 (select-frame orig-frame)
2303 (set-buffer orig-buffer)
2304 (select-window orig-window)))))
2308 ;;{{{ XEmacs isearch
2310 ;; In XEmacs, isearch often finds matches in other windows than the
2311 ;; currently selected. However, when exiting the old window
2312 ;; configuration is restored, with the exception of the beginning of
2313 ;; the start of the window for the selected window. This is not much
2316 ;; We overwrite the stored window configuration with the current,
2317 ;; unless we are in `slow-search-mode', i.e. only a few lines
2318 ;; of text is visible.
2320 (if follow-emacs-version-xemacs-p
2321 (defadvice isearch-done (before follow-isearch-done activate)
2322 (if (and (boundp 'follow-mode)
2324 (boundp 'isearch-window-configuration)
2325 isearch-window-configuration
2326 (boundp 'isearch-slow-terminal-mode)
2327 (not isearch-slow-terminal-mode))
2328 (let ((buf (current-buffer)))
2329 (setq isearch-window-configuration
2330 (current-window-configuration))
2331 (set-buffer buf)))))
2334 ;;{{{ Tail window handling
2336 ;;; In Emacs (not XEmacs) windows showing nothing are sometimes
2337 ;;; recentered. When in Follow Mode, this is not desireable for
2338 ;;; non-first windows in the window chain. This section tries to
2339 ;;; make the windows stay where they should be.
2341 ;;; If the display is updated, all windows starting at (point-max) are
2342 ;;; going to be recentered at the next redisplay, unless we do a
2343 ;;; read-and-write cycle to update the `force' flag inside the windows.
2345 ;;; In 19.30, a new varible `window-scroll-functions' is called every
2346 ;;; time a window is recentered. It is not perfect for our situation,
2347 ;;; since when it is called for a tail window, it is to late. However,
2348 ;;; if it is called for another window, we can try to update our
2351 ;;; By patching `sit-for' we can make sure that to catch all explicit
2352 ;;; updates initiated by lisp programs. Internal calls, on the other
2353 ;;; hand, are not handled.
2355 ;;; Please note that the function `follow-avoid-tail-recenter' is also
2356 ;;; called from other places, e.g. `post-command-hook' and
2357 ;;; `post-command-idle-hook'.
2359 ;; If this function is called it is too late for this window, but
2360 ;; we might save other windows from being recentered.
2362 (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions))
2363 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t))
2366 ;; This prevents all packages which calls `sit-for' directly
2367 ;; to recenter tail windows.
2369 (if follow-avoid-tail-recenter-p
2370 (defadvice sit-for (before follow-sit-for activate)
2371 "Adviced by Follow Mode.
2373 Avoid to recenter windows displaying only the end of a file as when
2374 displaying a short file in two windows, using Follow Mode."
2375 (follow-avoid-tail-recenter)))
2378 ;; Without this advice, `mouse-drag-region' would start to recenter
2381 (if (and follow-avoid-tail-recenter-p
2382 (fboundp 'move-overlay))
2383 (defadvice move-overlay (before follow-move-overlay activate)
2384 "Adviced by Follow Mode.
2385 Don't recenter windows showing only the end of a buffer.
2386 This prevents `mouse-drag-region' from messing things up."
2387 (follow-avoid-tail-recenter)))
2390 ;;{{{ profile support
2392 ;; The following (non-evaluated) section can be used to
2393 ;; profile this package using `elp'.
2395 ;; Invalid indentation on purpose!
2398 (setq elp-function-list
2401 ; sit-for ;; elp can't handle advices...
2403 follow-all-followers
2404 follow-split-followers
2407 follow-calculate-first-window-start
2408 follow-estimate-first-window-start
2409 follow-calculate-first-window-start-from-above
2410 follow-calculate-first-window-start-from-below
2412 follow-calc-win-start
2414 follow-windows-start-end
2415 follow-cache-valid-p
2416 follow-select-if-visible
2417 follow-select-if-visible-from-first
2418 follow-windows-aligned-p
2419 follow-point-visible-all-windows-p
2420 follow-avoid-tail-recenter
2421 follow-update-window-start
2422 follow-post-command-hook
2437 ;; /------------------------------------------------------------------------\
2438 ;; | "I [..] am rarely happier then when spending an entire day programming |
2439 ;; | my computer to perform automatically a task that it would otherwise |
2440 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
2441 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
2442 ;; | worth the investment of a day's happy activity working out a way to |
2443 ;; | save it". -- Douglas Adams, "Last Chance to See" |
2444 ;; \------------------------------------------------------------------------/
2446 ;;; follow.el ends here