*** empty log message ***
[emacs.git] / lisp / follow.el
blobe251d4c7b3c25c2745fdacd2abc1c134b19a4242
1 ;;; follow.el --- synchronize windows showing the same buffer
3 ;; Copyright (C) 1995, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Anders Lindgren <andersl@andersl.com>
7 ;; Maintainer: FSF (Anders' email bounces, Sep 2005)
8 ;; Created: 1995-05-25
9 ;; Keywords: display, window, minor-mode, convenience
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;;{{{ Documentation
32 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
33 ;; combines windows into one tall virtual window.
35 ;; The feeling of a "virtual window" has been accomplished by the use
36 ;; of two major techniques:
38 ;; * The windows always display adjacent sections of the buffer.
39 ;; This means that whenever one window is moved, all the
40 ;; others will follow. (Hence the name Follow mode.)
42 ;; * Should the point (cursor) end up outside a window, another
43 ;; window displaying that point is selected, if possible. This
44 ;; makes it possible to walk between windows using normal cursor
45 ;; movement commands.
47 ;; Follow mode comes to its prime when a large screen and two
48 ;; side-by-side window are used. The user can, with the help of Follow
49 ;; mode, use two full-height windows as though they are one.
50 ;; Imagine yourself editing a large function, or section of text,
51 ;; and being able to use 144 lines instead of the normal 72... (your
52 ;; mileage may vary).
54 ;; To test this package, make sure `follow' is loaded, or will be
55 ;; autoloaded when activated (see below). Then do the following:
57 ;; * Find your favorite file (preferably a long one).
59 ;; * Resize Emacs so that it will be wide enough for two full size
60 ;; columns. Delete the other windows and split the window with
61 ;; the commands `C-x 1 C-x 3'.
63 ;; * Give the command:
64 ;; M-x follow-mode <RETURN>
66 ;; * Now the display should look something like (assuming the text "71"
67 ;; is on line 71):
69 ;; +----------+----------+
70 ;; |1 |73 |
71 ;; |2 |74 |
72 ;; |3 |75 |
73 ;; ... ...
74 ;; |71 |143 |
75 ;; |72 |144 |
76 ;; +----------+----------+
78 ;; As you can see, the right-hand window starts at line 73, the line
79 ;; immediately below the end of the left-hand window. As long as
80 ;; `follow-mode' is active, the two windows will follow each other!
82 ;; * Play around and enjoy! Scroll one window and watch the other.
83 ;; Jump to the beginning or end. Press `Cursor down' at the last
84 ;; line of the left-hand window. Enter new lines into the
85 ;; text. Enter long lines spanning several lines, or several
86 ;; windows.
88 ;; * Should you find `Follow' mode annoying, just type
89 ;; M-x follow-mode <RETURN>
90 ;; to turn it off.
93 ;; The command `follow-delete-other-windows-and-split' maximises the
94 ;; visible area of the current buffer.
96 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
97 ;; key map. To do so, add the following lines (replacing `[f7]' and
98 ;; `[f8]' with your favorite keys) to the init file:
100 ;; (global-set-key [f8] 'follow-mode)
101 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
104 ;; There exist two system variables that control the appearence of
105 ;; lines wider than the window containing them. The default is to
106 ;; truncate long lines whenever a window isn't as wide as the frame.
108 ;; To make sure lines are never truncated, please place the following
109 ;; lines in your init file:
111 ;; (setq truncate-lines nil)
112 ;; (setq truncate-partial-width-windows nil)
115 ;; Since the display of XEmacs is pixel-oriented, a line could be
116 ;; clipped in half at the bottom of the window.
118 ;; To make XEmacs avoid clipping (normal) lines, please place the
119 ;; following line in your init-file:
121 ;; (setq pixel-vertical-clip-threshold 30)
124 ;; The correct way to cofigurate Follow mode, or any other mode for
125 ;; that matter, is to create one or more functions that do
126 ;; whatever you would like to do. These functions are then added to
127 ;; a hook.
129 ;; When `Follow' mode is activated, functions stored in the hook
130 ;; `follow-mode-hook' are called. When it is deactivated
131 ;; `follow-mode-off-hook' is run.
133 ;; The keymap `follow-key-map' contains key bindings activated by
134 ;; `follow-mode'.
136 ;; Example:
137 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
139 ;; (defun my-follow-mode-hook ()
140 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
141 ;; (define-key follow-mode-map "\C-cb" 'another-function))
144 ;; Usage:
146 ;; To activate, issue the command "M-x follow-mode"
147 ;; and press Return. To deactivate, do it again.
149 ;; The following is a list of commands useful when follow-mode is active.
151 ;; follow-scroll-up C-c . C-v
152 ;; Scroll text in a Follow mode window chain up.
154 ;; follow-scroll-down C-c . v
155 ;; Like `follow-scroll-up', but in the other direction.
157 ;; follow-delete-other-windows-and-split C-c . 1
158 ;; Maximize the visible area of the current buffer,
159 ;; and enter Follow mode. This is a very convenient
160 ;; way to start Follow mode, hence we recomend that
161 ;; this command be added to the global keymap.
163 ;; follow-recenter C-c . C-l
164 ;; Place the point in the center of the middle window,
165 ;; or a specified number of lines from either top or bottom.
167 ;; follow-switch-to-buffer C-c . b
168 ;; Switch buffer in all windows displaying the current buffer
169 ;; in this frame.
171 ;; follow-switch-to-buffer-all C-c . C-b
172 ;; Switch buffer in all windows in the selected frame.
174 ;; follow-switch-to-current-buffer-all
175 ;; Show the current buffer in all windows on the current
176 ;; frame and turn on `follow-mode'.
178 ;; follow-first-window C-c . <
179 ;; Select the first window in the frame showing the same buffer.
181 ;; follow-last-window C-c . >
182 ;; Select the last window in the frame showing the same buffer.
184 ;; follow-next-window C-c . n
185 ;; Select the next window in the frame showing the same buffer.
187 ;; follow-previous-window C-c . p
188 ;; Select the previous window showing the same buffer.
191 ;; Well, it seems ok, but what if I really want to look at two different
192 ;; positions in the text? Here are two simple methods to use:
194 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
195 ;; in the same frame. (My apoligies to you who can't use frames.)
197 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
198 ;; you want to view two locations. Of course, `follow' mode can
199 ;; be reactivated by hitting the same key again.
201 ;; Example from my ~/.emacs:
202 ;; (global-set-key [f8] 'follow-mode)
205 ;; Implementation:
207 ;; In an ideal world, follow mode would have been implemented in the
208 ;; kernel of the display routines, making sure that the windows (using
209 ;; follow mode) ALWAYS are aligned. On planet Earth, however, we must
210 ;; accept a solution where we ALMOST ALWAYS can make sure that the
211 ;; windows are aligned.
213 ;; Follow mode does this in three places:
214 ;; 1) After each user command.
215 ;; 2) After a process output has been perfomed.
216 ;; 3) When a scrollbar has been moved.
218 ;; This will cover most situations. (Let me know if there are other
219 ;; situations that should be covered.)
221 ;; Note that only the selected window is checked, for the reason of
222 ;; efficiency and code complexity. (I.e. it is possible to make a
223 ;; non-selected windows unaligned. It will, however, pop right back
224 ;; when it is selected.)
226 ;;}}}
228 ;;; Code:
230 ;;{{{ Preliminaries
232 ;; Make the compiler shut up!
233 ;; There are two strategies:
234 ;; 1) Shut warnings off completely.
235 ;; 2) Handle each warning separately.
237 ;; Since I would like to see real errors, I've selected the latter
238 ;; method.
240 ;; The problem with undefined variables and functions has been solved
241 ;; by using `set', `symbol-value' and `symbol-function' rather than
242 ;; `setq' and direct references to variables and functions.
244 ;; For example:
245 ;; (if (boundp 'foo) ... (symbol-value 'foo) )
246 ;; (set 'foo ...) <-- XEmacs doesn't fall for this one.
247 ;; (funcall (symbol-function 'set) 'bar ...)
249 ;; Note: When this file is interpreted, `eval-when-compile' is
250 ;; evaluted. Since it doesn't hurt to evaluate it, but it is a bit
251 ;; annoying, we test if the byte-compiler has been loaded. This can,
252 ;; of course, lead to some occasional unintended evaluation...
254 ;; Should someone come up with a better solution, please let me
255 ;; know.
257 (eval-when-compile
258 (if (or (featurep 'bytecomp)
259 (featurep 'byte-compile))
260 (cond ((featurep 'xemacs)
261 ;; Make XEmacs shut up! I'm using standard Emacs
262 ;; functions, they are NOT obsolete!
263 (if (eq (get 'force-mode-line-update 'byte-compile)
264 'byte-compile-obsolete)
265 (put 'force-mode-line-update 'byte-compile 'nil))
266 (if (eq (get 'frame-first-window 'byte-compile)
267 'byte-compile-obsolete)
268 (put 'frame-first-window 'byte-compile 'nil))))))
270 ;;}}}
271 ;;{{{ Variables
273 (defgroup follow nil
274 "Synchronize windows showing the same buffer."
275 :prefix "follow-"
276 :group 'windows
277 :group 'convenience)
279 (defcustom follow-mode-hook nil
280 "Normal hook run by `follow-mode'."
281 :type 'hook
282 :group 'follow)
284 (defcustom follow-mode-off-hook nil
285 "Hooks to run when Follow mode is turned off."
286 :type 'hook
287 :group 'follow)
288 (make-obsolete-variable 'follow-mode-off-hook 'follow-mode-hook "22.2")
290 ;;{{{ Keymap/Menu
292 ;; Define keys for the follow-mode minor mode map and replace some
293 ;; functions in the global map. All `follow' mode special functions
294 ;; can be found on (the somewhat cumbersome) "C-c . <key>"
295 ;; (Control-C dot <key>). (As of Emacs 19.29 the keys
296 ;; C-c <punctuation character> are reserved for minor modes.)
298 ;; To change the prefix, redefine `follow-mode-prefix' before
299 ;; `follow' is loaded, or see the section on `follow-mode-hook'
300 ;; above for an example of how to bind the keys the way you like.
302 ;; Please note that the keymap is defined the first time this file is
303 ;; loaded. Also note that the only valid way to manipulate the
304 ;; keymap is to use `define-key'. Don't change it using `setq' or
305 ;; similar!
307 (defcustom follow-mode-prefix "\C-c."
308 "Prefix key to use for follow commands in Follow mode.
309 The value of this variable is checked as part of loading Follow mode.
310 After that, changing the prefix key requires manipulating keymaps."
311 :type 'string
312 :group 'follow)
314 (defvar follow-mode-map
315 (let ((mainmap (make-sparse-keymap))
316 (map (make-sparse-keymap)))
317 (define-key map "\C-v" 'follow-scroll-up)
318 (define-key map "\M-v" 'follow-scroll-down)
319 (define-key map "v" 'follow-scroll-down)
320 (define-key map "1" 'follow-delete-other-windows-and-split)
321 (define-key map "b" 'follow-switch-to-buffer)
322 (define-key map "\C-b" 'follow-switch-to-buffer-all)
323 (define-key map "\C-l" 'follow-recenter)
324 (define-key map "<" 'follow-first-window)
325 (define-key map ">" 'follow-last-window)
326 (define-key map "n" 'follow-next-window)
327 (define-key map "p" 'follow-previous-window)
329 (define-key mainmap follow-mode-prefix map)
331 ;; Replace the standard `end-of-buffer', when in Follow mode. (I
332 ;; don't see the point in trying to replace every function that
333 ;; could be enhanced in Follow mode. End-of-buffer is a special
334 ;; case since it is very simple to define and it greatly enhances
335 ;; the look and feel of Follow mode.)
336 (define-key mainmap [remap end-of-buffer] 'follow-end-of-buffer)
339 ;; The menu.
342 (if (not (featurep 'xemacs))
345 ;; Emacs
347 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
348 "Follow"))
349 (count 0)
351 (mapcar
352 (function
353 (lambda (item)
354 (setq id
355 (or (cdr item)
356 (progn
357 (setq count (+ count 1))
358 (intern (format "separator-%d" count)))))
359 (define-key menumap (vector id) item)
360 (or (eq id 'follow-mode)
361 (put id 'menu-enable 'follow-mode))))
362 ;; In reverse order:
363 '(("Toggle Follow mode" . follow-mode)
364 ("--")
365 ("Recenter" . follow-recenter)
366 ("--")
367 ("Previous Window" . follow-previous-window)
368 ("Next Windows" . follow-next-window)
369 ("Last Window" . follow-last-window)
370 ("First Window" . follow-first-window)
371 ("--")
372 ("Switch To Buffer (all windows)"
373 . follow-switch-to-buffer-all)
374 ("Switch To Buffer" . follow-switch-to-buffer)
375 ("--")
376 ("Delete Other Windows and Split"
377 . follow-delete-other-windows-and-split)
378 ("--")
379 ("Scroll Down" . follow-scroll-down)
380 ("Scroll Up" . follow-scroll-up)))
382 ;; If there is a `tools' menu, we use it. However, we can't add a
383 ;; minor-mode specific item to it (it's broken), so we make the
384 ;; contents ghosted when not in use, and add ourselves to the
385 ;; global map. If no `tools' menu is present, just make a
386 ;; top-level menu visible when the mode is activated.
388 (let ((tools-map (lookup-key (current-global-map) [menu-bar tools]))
389 (last nil))
390 (if (sequencep tools-map)
391 (progn
392 ;; Find the last entry in the menu and store it in `last'.
393 (mapcar (function
394 (lambda (x)
395 (setq last (or (cdr-safe
396 (cdr-safe
397 (cdr-safe x)))
398 last))))
399 tools-map)
400 (if last
401 (progn
402 (funcall (symbol-function 'define-key-after)
403 tools-map [separator-follow] '("--") last)
404 (funcall (symbol-function 'define-key-after)
405 tools-map [follow] (cons "Follow" menumap)
406 'separator-follow))
407 ;; Didn't find the last item, adding to the top of
408 ;; tools. (This will probably never happen...)
409 (define-key (current-global-map) [menu-bar tools follow]
410 (cons "Follow" menumap))))
411 ;; No tools menu, add "Follow" to the menubar.
412 (define-key mainmap [menu-bar follow]
413 (cons "Follow" menumap)))))
416 ;; XEmacs.
419 ;; place the menu in the `Tools' menu.
420 (let ((menu '("Follow"
421 :filter follow-menu-filter
422 ["Scroll Up" follow-scroll-up t]
423 ["Scroll Down" follow-scroll-down t]
424 ["Delete Other Windows and Split"
425 follow-delete-other-windows-and-split t]
426 ["Switch To Buffer" follow-switch-to-buffer t]
427 ["Switch To Buffer (all windows)"
428 follow-switch-to-buffer-all t]
429 ["First Window" follow-first-window t]
430 ["Last Window" follow-last-window t]
431 ["Next Windows" follow-next-window t]
432 ["Previous Window" follow-previous-window t]
433 ["Recenter" follow-recenter t]
434 ["Deactivate" follow-mode t])))
436 ;; Why not just `(set-buffer-menubar current-menubar)'? The
437 ;; question is a very good question. The reason is that under
438 ;; Emacs, neither `set-buffer-menubar' nor
439 ;; `current-menubar' is defined, hence the byte-compiler will
440 ;; warn.
441 (funcall (symbol-function 'set-buffer-menubar)
442 (symbol-value 'current-menubar))
443 (funcall (symbol-function 'add-submenu) '("Tools") menu))
445 ;; When the mode is not activated, only one item is visible:
446 ;; "Activate".
447 (defun follow-menu-filter (menu)
448 (if follow-mode
449 menu
450 '(["Activate " follow-mode t]))))
452 mainmap)
453 "Minor mode keymap for Follow mode.")
455 ;;}}}
457 (defcustom follow-mode-line-text " Follow"
458 "Text shown in the mode line when Follow mode is active.
459 Defaults to \" Follow\". Examples of other values
460 are \" Fw\", or simply \"\"."
461 :type 'string
462 :group 'follow)
464 (defcustom follow-auto nil
465 "Non-nil activates Follow mode whenever a file is loaded."
466 :type 'boolean
467 :group 'follow)
469 (defcustom follow-intercept-processes (fboundp 'start-process)
470 "When non-nil, Follow mode will monitor process output."
471 :type 'boolean
472 :group 'follow)
474 (defvar follow-avoid-tail-recenter-p (not (featurep 'xemacs))
475 "*When non-nil, patch Emacs so that tail windows won't be recentered.
477 A \"tail window\" is a window that displays only the end of
478 the buffer. Normally it is practical for the user that empty
479 windows are recentered automatically. However, when using
480 Follow mode it breaks the display when the end is displayed
481 in a window \"above\" the last window. This is for
482 example the case when displaying a short page in info.
484 Must be set before Follow mode is loaded.
486 Please note that it is not possible to fully prevent Emacs from
487 recentering empty windows. Please report if you find a repeatable
488 situation in which Emacs recenters empty windows.
490 XEmacs, as of 19.12, does not recenter windows, good!")
492 (defvar follow-cache-command-list
493 '(next-line previous-line forward-char backward-char)
494 "List of commands that don't require recalculation.
496 In order to be able to use the cache, a command should not change the
497 contents of the buffer, nor should it change selected window or current
498 buffer.
500 The commands in this list are checked at load time.
502 To mark other commands as suitable for caching, set the symbol
503 property `follow-mode-use-cache' to non-nil.")
505 (defvar follow-debug nil
506 "*Non-nil when debugging Follow mode.")
509 ;; Internal variables:
511 (defvar follow-internal-force-redisplay nil
512 "True when Follow mode should redisplay the windows.")
514 (defvar follow-process-filter-alist '()
515 "The original filters for processes intercepted by Follow mode.")
517 (defvar follow-active-menu nil
518 "The menu visible when Follow mode is active.")
520 (defvar follow-deactive-menu nil
521 "The menu visible when Follow mode is deactivated.")
523 (defvar follow-inside-post-command-hook nil
524 "Non-nil when inside Follow modes `post-command-hook'.
525 Used by `follow-window-size-change'.")
527 (defvar follow-windows-start-end-cache nil
528 "Cache used by `follow-window-start-end'.")
530 ;;}}}
531 ;;{{{ Debug messages
533 ;; This inline function must be as small as possible!
534 ;; Maybe we should define a macro that expands to nil if
535 ;; the variable is not set.
537 (defsubst follow-debug-message (&rest args)
538 "Like message, but only active when `follow-debug' is non-nil."
539 (if (and (boundp 'follow-debug) follow-debug)
540 (apply 'message args)))
542 ;;}}}
543 ;;{{{ Cache
545 (dolist (cmd follow-cache-command-list)
546 (put cmd 'follow-mode-use-cache t))
548 ;;}}}
550 ;;{{{ The mode
552 ;;;###autoload
553 (defun turn-on-follow-mode ()
554 "Turn on Follow mode. Please see the function `follow-mode'."
555 (interactive)
556 (follow-mode 1))
559 ;;;###autoload
560 (defun turn-off-follow-mode ()
561 "Turn off Follow mode. Please see the function `follow-mode'."
562 (interactive)
563 (follow-mode -1))
565 (put 'follow-mode 'permanent-local t)
566 ;;;###autoload
567 (define-minor-mode follow-mode
568 "Minor mode that combines windows into one tall virtual window.
570 The feeling of a \"virtual window\" has been accomplished by the use
571 of two major techniques:
573 * The windows always displays adjacent sections of the buffer.
574 This means that whenever one window is moved, all the
575 others will follow. (Hence the name Follow mode.)
577 * Should the point (cursor) end up outside a window, another
578 window displaying that point is selected, if possible. This
579 makes it possible to walk between windows using normal cursor
580 movement commands.
582 Follow mode comes to its prime when used on a large screen and two
583 side-by-side windows are used. The user can, with the help of Follow
584 mode, use two full-height windows as though they would have been
585 one. Imagine yourself editing a large function, or section of text,
586 and being able to use 144 lines instead of the normal 72... (your
587 mileage may vary).
589 To split one large window into two side-by-side windows, the commands
590 `\\[split-window-horizontally]' or \
591 `M-x follow-delete-other-windows-and-split' can be used.
593 Only windows displayed in the same frame follow each other.
595 If the variable `follow-intercept-processes' is non-nil, Follow mode
596 will listen to the output of processes and redisplay accordingly.
597 \(This is the default.)
599 This command runs the normal hook `follow-mode-hook'.
601 Keys specific to Follow mode:
602 \\{follow-mode-map}"
603 :keymap follow-mode-map
604 (when (and follow-mode follow-intercept-processes)
605 (follow-intercept-process-output))
606 (cond (follow-mode ; On
607 ;; XEmacs: If this is non-nil, the window will scroll before
608 ;; the point will have a chance to get into the next window.
609 (when (boundp 'scroll-on-clipped-lines)
610 (setq scroll-on-clipped-lines nil))
611 (force-mode-line-update)
612 (add-hook 'post-command-hook 'follow-post-command-hook t))
614 ((not follow-mode) ; Off
615 (force-mode-line-update))))
617 ;;}}}
618 ;;{{{ Find file hook
620 ;; This will start follow-mode whenever a new file is loaded, if
621 ;; the variable `follow-auto' is non-nil.
623 (add-hook 'find-file-hook 'follow-find-file-hook t)
625 (defun follow-find-file-hook ()
626 "Find-file hook for Follow mode. See the variable `follow-auto'."
627 (if follow-auto (follow-mode t)))
629 ;;}}}
631 ;;{{{ User functions
634 ;;; User functions usable when in Follow mode.
637 ;;{{{ Scroll
639 ;; `scroll-up' and `-down', but for windows in Follow mode.
641 ;; Almost like the real thing, excpet when the cursor ends up outside
642 ;; the top or bottom... In our case however, we end up outside the
643 ;; window and hence we are recenterd. Should we let `recenter' handle
644 ;; the point position we would never leave the selected window. To do
645 ;; it ourselves we would need to do our own redisplay, which is easier
646 ;; said than done. (Why didn't I do a real display abstraction from
647 ;; the beginning?)
649 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
650 ;; our post-command-hook will move our windows back into the old
651 ;; position... (This would also be corrected if we would have had a
652 ;; good redisplay abstraction.)
654 (defun follow-scroll-up (&optional arg)
655 "Scroll text in a Follow mode window chain up.
657 If called with no ARG, the `next-screen-context-lines' last lines of
658 the bottom window in the chain will be visible in the top window.
660 If called with an argument, scroll ARG lines up.
661 Negative ARG means scroll downward.
663 Works like `scroll-up' when not in Follow mode."
664 (interactive "P")
665 (cond ((not (and (boundp 'follow-mode) follow-mode))
666 (scroll-up arg))
667 (arg
668 (save-excursion (scroll-up arg))
669 (setq follow-internal-force-redisplay t))
671 (let* ((windows (follow-all-followers))
672 (end (window-end (car (reverse windows)))))
673 (if (eq end (point-max))
674 (signal 'end-of-buffer nil)
675 (select-window (car windows))
676 ;; `window-end' might return nil.
677 (if end
678 (goto-char end))
679 (vertical-motion (- next-screen-context-lines))
680 (set-window-start (car windows) (point)))))))
683 (defun follow-scroll-down (&optional arg)
684 "Scroll text in a Follow mode window chain down.
686 If called with no ARG, the `next-screen-context-lines' top lines of
687 the top window in the chain will be visible in the bottom window.
689 If called with an argument, scroll ARG lines down.
690 Negative ARG means scroll upward.
692 Works like `scroll-up' when not in Follow mode."
693 (interactive "P")
694 (cond ((not (and (boundp 'follow-mode) follow-mode))
695 (scroll-up arg))
696 (arg
697 (save-excursion (scroll-down arg)))
699 (let* ((windows (follow-all-followers))
700 (win (car (reverse windows)))
701 (start (window-start (car windows))))
702 (if (eq start (point-min))
703 (signal 'beginning-of-buffer nil)
704 (select-window win)
705 (goto-char start)
706 (vertical-motion (- (- (window-height win)
708 next-screen-context-lines)))
709 (set-window-start win (point))
710 (goto-char start)
711 (vertical-motion (- next-screen-context-lines 1))
712 (setq follow-internal-force-redisplay t))))))
714 ;;}}}
715 ;;{{{ Buffer
717 ;;;###autoload
718 (defun follow-delete-other-windows-and-split (&optional arg)
719 "Create two side by side windows and enter Follow mode.
721 Execute this command to display as much as possible of the text
722 in the selected window. All other windows, in the current
723 frame, are deleted and the selected window is split in two
724 side-by-side windows. Follow mode is activated, hence the
725 two windows always will display two successive pages.
726 \(If one window is moved, the other one will follow.)
728 If ARG is positive, the leftmost window is selected. If negative,
729 the rightmost is selected. If ARG is nil, the leftmost window is
730 selected if the original window is the first one in the frame.
732 To bind this command to a hotkey, place the following line
733 in your `~/.emacs' file, replacing [f7] by your favourite key:
734 (global-set-key [f7] 'follow-delete-other-windows-and-split)"
735 (interactive "P")
736 (let ((other (or (and (null arg)
737 (not (eq (selected-window)
738 (frame-first-window (selected-frame)))))
739 (and arg
740 (< (prefix-numeric-value arg) 0))))
741 (start (window-start)))
742 (delete-other-windows)
743 (split-window-horizontally)
744 (if other
745 (progn
746 (other-window 1)
747 (set-window-start (selected-window) start)
748 (setq follow-internal-force-redisplay t)))
749 (follow-mode 1)))
751 (defun follow-switch-to-buffer (buffer)
752 "Show BUFFER in all windows in the current Follow mode window chain."
753 (interactive "BSwitch to Buffer: ")
754 (let ((orig-window (selected-window))
755 (windows (follow-all-followers)))
756 (while windows
757 (select-window (car windows))
758 (switch-to-buffer buffer)
759 (setq windows (cdr windows)))
760 (select-window orig-window)))
763 (defun follow-switch-to-buffer-all (&optional buffer)
764 "Show BUFFER in all windows on this frame.
765 Defaults to current buffer."
766 (interactive (list (read-buffer "Switch to Buffer: "
767 (current-buffer))))
768 (or buffer (setq buffer (current-buffer)))
769 (let ((orig-window (selected-window)))
770 (walk-windows
771 (function
772 (lambda (win)
773 (select-window win)
774 (switch-to-buffer buffer))))
775 (select-window orig-window)
776 (follow-redisplay)))
779 (defun follow-switch-to-current-buffer-all ()
780 "Show current buffer in all windows on this frame, and enter Follow mode.
782 To bind this command to a hotkey place the following line
783 in your `~/.emacs' file:
784 (global-set-key [f7] 'follow-switch-to-current-buffer-all)"
785 (interactive)
786 (or (and (boundp 'follow-mode) follow-mode)
787 (follow-mode 1))
788 (follow-switch-to-buffer-all))
790 ;;}}}
791 ;;{{{ Movement
793 ;; Note, these functions are not very useful, at least not unless you
794 ;; rebind the rather cumbersome key sequence `C-c . p'.
796 (defun follow-next-window ()
797 "Select the next window showing the same buffer."
798 (interactive)
799 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
800 (if succ
801 (select-window (car succ))
802 (error "%s" "No more windows"))))
805 (defun follow-previous-window ()
806 "Select the previous window showing the same buffer."
807 (interactive)
808 (let ((pred (car (follow-split-followers (follow-all-followers)))))
809 (if pred
810 (select-window (car pred))
811 (error "%s" "No more windows"))))
814 (defun follow-first-window ()
815 "Select the first window in the frame showing the same buffer."
816 (interactive)
817 (select-window (car (follow-all-followers))))
820 (defun follow-last-window ()
821 "Select the last window in the frame showing the same buffer."
822 (interactive)
823 (select-window (car (reverse (follow-all-followers)))))
825 ;;}}}
826 ;;{{{ Redraw
828 (defun follow-recenter (&optional arg)
829 "Recenter the middle window around point.
830 Rearrange all other windows around the middle window.
832 With a positive argument, place the current line ARG lines
833 from the top. With a negative argument, place it -ARG lines
834 from the bottom."
835 (interactive "P")
836 (if arg
837 (let ((p (point))
838 (arg (prefix-numeric-value arg)))
839 (if (>= arg 0)
840 ;; Recenter relative to the top.
841 (progn
842 (follow-first-window)
843 (goto-char p)
844 (recenter arg))
845 ;; Recenter relative to the bottom.
846 (follow-last-window)
847 (goto-char p)
848 (recenter arg)
849 ;; Otherwise, our post-command-hook will move the window
850 ;; right back.
851 (setq follow-internal-force-redisplay t)))
852 ;; Recenter in the middle.
853 (let* ((dest (point))
854 (windows (follow-all-followers))
855 (win (nth (/ (- (length windows) 1) 2) windows)))
856 (select-window win)
857 (goto-char dest)
858 (recenter)
859 ;;(setq follow-internal-force-redisplay t)
863 (defun follow-redraw ()
864 "Arrange windows displaying the same buffer in successor order.
865 This function can be called even if the buffer is not in Follow mode.
867 Hopefully, there should be no reason to call this function when in
868 Follow mode since the windows should always be aligned."
869 (interactive)
870 (sit-for 0)
871 (follow-redisplay))
873 ;;}}}
874 ;;{{{ End of buffer
876 (defun follow-end-of-buffer (&optional arg)
877 "Move point to the end of the buffer, Follow mode style.
879 If the end is not visible, it will be displayed in the last possible
880 window in the Follow mode window chain.
882 The mark is left at the previous position. With arg N, put point N/10
883 of the way from the true end."
884 (interactive "P")
885 (let ((followers (follow-all-followers))
886 (pos (point)))
887 (cond (arg
888 (select-window (car (reverse followers))))
889 ((follow-select-if-end-visible
890 (follow-windows-start-end followers)))
892 (select-window (car (reverse followers)))))
893 (goto-char pos)
894 (with-no-warnings
895 (end-of-buffer arg))))
897 ;;}}}
899 ;;}}}
901 ;;{{{ Display
903 ;;;; The display routines
905 ;;{{{ Information gathering functions
907 (defun follow-all-followers (&optional testwin)
908 "Return all windows displaying the same buffer as the TESTWIN.
909 The list contains only windows displayed in the same frame as TESTWIN.
910 If TESTWIN is nil the selected window is used."
911 (or (and testwin (window-live-p testwin))
912 (setq testwin (selected-window)))
913 (let* ((top (frame-first-window (window-frame testwin)))
914 (win top)
915 (done nil)
916 (windows '())
917 (buffer (window-buffer testwin)))
918 (while (and (not done) win)
919 (if (eq (window-buffer win) buffer)
920 (setq windows (cons win windows)))
921 (setq win (next-window win 'not))
922 (if (eq win top)
923 (setq done t)))
924 (nreverse windows)))
927 (defun follow-split-followers (windows &optional win)
928 "Split the WINDOWS into the sets: predecessors and successors.
929 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
930 from the selected window."
931 (or win
932 (setq win (selected-window)))
933 (let ((pred '()))
934 (while (not (eq (car windows) win))
935 (setq pred (cons (car windows) pred))
936 (setq windows (cdr windows)))
937 (cons pred (cdr windows))))
940 ;; This function is optimized function for speed!
942 (defun follow-calc-win-end (&optional win)
943 "Calculate the presumed window end for WIN.
945 Actually, the position returned is the start of the next
946 window, normally is the end plus one.
948 If WIN is nil, the selected window is used.
950 Returns (end-pos end-of-buffer-p)"
951 (if (featurep 'xemacs)
952 ;; XEmacs can calculate the end of the window by using
953 ;; the 'guarantee options. GOOD!
954 (let ((end (window-end win t)))
955 (if (= end (funcall (symbol-function 'point-max)
956 (window-buffer win)))
957 (list end t)
958 (list (+ end 1) nil)))
959 ;; Emacs: We have to calculate the end by ourselves.
960 ;; This code works on both XEmacs and Emacs, but now
961 ;; that XEmacs has got custom-written code, this could
962 ;; be optimized for Emacs.
963 (let ((orig-win (and win (selected-window)))
964 height
965 buffer-end-p)
966 (if win (select-window win))
967 (prog1
968 (save-excursion
969 (goto-char (window-start))
970 (setq height (- (window-height) 1))
971 (setq buffer-end-p
972 (if (bolp)
973 (not (= height (vertical-motion height)))
974 (save-restriction
975 ;; Fix a mis-feature in `vertical-motion':
976 ;; The start of the window is assumed to
977 ;; coinside with the start of a line.
978 (narrow-to-region (point) (point-max))
979 (not (= height (vertical-motion height))))))
980 (list (point) buffer-end-p))
981 (if orig-win
982 (select-window orig-win))))))
985 ;; Can't use `save-window-excursion' since it triggers a redraw.
986 (defun follow-calc-win-start (windows pos win)
987 "Calculate where WIN will start if the first in WINDOWS start at POS.
989 If WIN is nil the point below all windows is returned."
990 (let (start)
991 (while (and windows (not (eq (car windows) win)))
992 (setq start (window-start (car windows)))
993 (set-window-start (car windows) pos 'noforce)
994 (setq pos (car (inline (follow-calc-win-end (car windows)))))
995 (set-window-start (car windows) start 'noforce)
996 (setq windows (cdr windows)))
997 pos))
1000 ;; The result from `follow-windows-start-end' is cached when using
1001 ;; a handful simple commands, like cursor movement commands.
1003 (defsubst follow-cache-valid-p (windows)
1004 "Test if the cached value of `follow-windows-start-end' can be used.
1005 Note that this handles the case when the cache has been set to nil."
1006 (let ((res t)
1007 (cache follow-windows-start-end-cache))
1008 (while (and res windows cache)
1009 (setq res (and (eq (car windows)
1010 (car (car cache)))
1011 (eq (window-start (car windows))
1012 (car (cdr (car cache))))))
1013 (setq windows (cdr windows))
1014 (setq cache (cdr cache)))
1015 (and res (null windows) (null cache))))
1018 (defsubst follow-invalidate-cache ()
1019 "Force `follow-windows-start-end' to recalculate the end of the window."
1020 (setq follow-windows-start-end-cache nil))
1023 ;; Build a list of windows and their start and end positions.
1024 ;; Useful to avoid calculating start/end position whenever they are needed.
1025 ;; The list has the format:
1026 ;; ((Win Start End End-of-buffer-visible-p) ...)
1028 ;; Used to have a `save-window-excursion', but it obviously triggered
1029 ;; redraws of the display. Check if I used it for anything.
1032 (defun follow-windows-start-end (windows)
1033 "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
1034 (if (follow-cache-valid-p windows)
1035 follow-windows-start-end-cache
1036 (let ((win-start-end '())
1037 (orig-win (selected-window)))
1038 (while windows
1039 (select-window (car windows))
1040 (setq win-start-end
1041 (cons (cons (car windows)
1042 (cons (window-start)
1043 (follow-calc-win-end)))
1044 win-start-end))
1045 (setq windows (cdr windows)))
1046 (select-window orig-win)
1047 (setq follow-windows-start-end-cache (nreverse win-start-end))
1048 follow-windows-start-end-cache)))
1051 (defsubst follow-pos-visible (pos win win-start-end)
1052 "Non-nil when POS is visible in WIN."
1053 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
1054 (and (>= pos (car wstart-wend-bend))
1055 (or (< pos (car (cdr wstart-wend-bend)))
1056 (nth 2 wstart-wend-bend)))))
1059 ;; By `aligned' we mean that for all adjecent windows, the end of the
1060 ;; first is equal with the start of the successor. The first window
1061 ;; should start at a full screen line.
1063 (defsubst follow-windows-aligned-p (win-start-end)
1064 "Non-nil if the follower windows are aligned."
1065 (let ((res t))
1066 (save-excursion
1067 (goto-char (window-start (car (car win-start-end))))
1068 (if (bolp)
1070 (vertical-motion 0 (car (car win-start-end)))
1071 (setq res (eq (point) (window-start (car (car win-start-end)))))))
1072 (while (and res (cdr win-start-end))
1073 ;; At least two followers left
1074 (setq res (eq (car (cdr (cdr (car win-start-end))))
1075 (car (cdr (car (cdr win-start-end))))))
1076 (setq win-start-end (cdr win-start-end)))
1077 res))
1080 ;; Check if the point is visible in all windows. (So that
1081 ;; no one will be recentered.)
1083 (defun follow-point-visible-all-windows-p (win-start-end)
1084 "Non-nil when the `window-point' is visible in all windows."
1085 (let ((res t))
1086 (while (and res win-start-end)
1087 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
1088 (car (car win-start-end))
1089 win-start-end))
1090 (setq win-start-end (cdr win-start-end)))
1091 res))
1094 ;; Make sure WIN always starts at the beginning of an whole screen
1095 ;; line. If WIN is not aligned the start is updated which probably
1096 ;; will lead to a redisplay of the screen later on.
1098 ;; This is used with the first window in a follow chain. The reason
1099 ;; is that we want to detect that the point is outside the window.
1100 ;; (Without the update, the start of the window will move as the
1101 ;; user presses BackSpace, and the other window redisplay routines
1102 ;; will move the start of the window in the wrong direction.)
1104 (defun follow-update-window-start (win)
1105 "Make sure that the start of WIN starts at a full screen line."
1106 (save-excursion
1107 (goto-char (window-start win))
1108 (if (bolp)
1110 (vertical-motion 0 win)
1111 (if (eq (point) (window-start win))
1113 (vertical-motion 1 win)
1114 (set-window-start win (point) 'noforce)))))
1116 ;;}}}
1117 ;;{{{ Selection functions
1119 ;; Make a window in WINDOWS selected if it currently
1120 ;; is displaying the position DEST.
1122 ;; We don't select a window if it just has been moved.
1124 (defun follow-select-if-visible (dest win-start-end)
1125 "Select and return a window, if DEST is visible in it.
1126 Return the selected window."
1127 (let ((win nil))
1128 (while (and (not win) win-start-end)
1129 ;; Don't select a window that was just moved. This makes it
1130 ;; possible to later select the last window after a `end-of-buffer'
1131 ;; command.
1132 (if (follow-pos-visible dest (car (car win-start-end)) win-start-end)
1133 (progn
1134 (setq win (car (car win-start-end)))
1135 (select-window win)))
1136 (setq win-start-end (cdr win-start-end)))
1137 win))
1140 ;; Lets select a window showing the end. Make sure we only select it if it
1141 ;; it wasn't just moved here. (i.e. M-> shall not unconditionally place
1142 ;; the point in the selected window.)
1144 ;; (Compability cludge: in Emacs `window-end' is equal to `point-max';
1145 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1146 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1148 (defun follow-select-if-end-visible (win-start-end)
1149 "Select and return a window, if end is visible in it."
1150 (let ((win nil))
1151 (while (and (not win) win-start-end)
1152 ;; Don't select a window that was just moved. This makes it
1153 ;; possible to later select the last window after a `end-of-buffer'
1154 ;; command.
1155 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1156 (nth 3 (car win-start-end))
1157 ;; `window-end' might return nil.
1158 (let ((end (window-end (car (car win-start-end)))))
1159 (and end
1160 (eq (point-max) (min (point-max) end)))))
1161 (progn
1162 (setq win (car (car win-start-end)))
1163 (select-window win)))
1164 (setq win-start-end (cdr win-start-end)))
1165 win))
1168 ;; Select a window that will display the point if the windows would
1169 ;; be redisplayed with the first window fixed. This is useful for
1170 ;; example when the user has pressed return at the bottom of a window
1171 ;; as the point is not visible in any window.
1173 (defun follow-select-if-visible-from-first (dest windows)
1174 "Select and return a window with DEST, if WINDOWS are redrawn from top."
1175 (let ((win nil)
1176 end-pos-end-p)
1177 (save-excursion
1178 (goto-char (window-start (car windows)))
1179 ;; Make sure the line start in the beginning of a real screen
1180 ;; line.
1181 (vertical-motion 0 (car windows))
1182 (if (< dest (point))
1183 ;; Above the start, not visible.
1185 ;; At or below the start. Check the windows.
1186 (save-window-excursion
1187 (while (and (not win) windows)
1188 (set-window-start (car windows) (point) 'noforce)
1189 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1190 (goto-char (car end-pos-end-p))
1191 ;; Visible, if dest above end, or if eob is visible inside
1192 ;; the window.
1193 (if (or (car (cdr end-pos-end-p))
1194 (< dest (point)))
1195 (setq win (car windows))
1196 (setq windows (cdr windows)))))))
1197 (if win
1198 (select-window win))
1199 win))
1202 ;;}}}
1203 ;;{{{ Redisplay
1205 ;; Redraw all the windows on the screen, starting with the top window.
1206 ;; The window used as as marker is WIN, or the selcted window if WIN
1207 ;; is nil.
1209 (defun follow-redisplay (&optional windows win)
1210 "Reposition the WINDOWS around WIN.
1211 Should the point be too close to the roof we redisplay everything
1212 from the top. WINDOWS should contain a list of windows to
1213 redisplay, it is assumed that WIN is a member of the list.
1214 Should WINDOWS be nil, the windows displaying the
1215 same buffer as WIN, in the current frame, are used.
1216 Should WIN be nil, the selected window is used."
1217 (or win
1218 (setq win (selected-window)))
1219 (or windows
1220 (setq windows (follow-all-followers win)))
1221 (follow-downward windows (follow-calculate-first-window-start windows win)))
1224 ;; Redisplay a chain of windows. Start every window directly after the
1225 ;; end of the previous window, to make sure long lines are displayed
1226 ;; correctly.
1228 (defun follow-downward (windows pos)
1229 "Redisplay all WINDOWS starting at POS."
1230 (while windows
1231 (set-window-start (car windows) pos)
1232 (setq pos (car (follow-calc-win-end (car windows))))
1233 (setq windows (cdr windows))))
1236 ;;(defun follow-downward (windows pos)
1237 ;; "Redisplay all WINDOWS starting at POS."
1238 ;; (let (p)
1239 ;; (while windows
1240 ;; (setq p (window-point (car windows)))
1241 ;; (set-window-start (car windows) pos)
1242 ;; (set-window-point (car windows) (max p pos))
1243 ;; (setq pos (car (follow-calc-win-end (car windows))))
1244 ;; (setq windows (cdr windows)))))
1247 ;; Return the start of the first window.
1249 ;; First, estimate the position. It the value is not perfect (i.e. we
1250 ;; have somewhere splited a line between windows) we try to enhance
1251 ;; the value.
1253 ;; The guess is always perfect if no long lines is split between
1254 ;; windows.
1256 ;; The worst case peformace of probably very bad, but it is very
1257 ;; unlikely that we ever will miss the correct start by more than one
1258 ;; or two lines.
1260 (defun follow-calculate-first-window-start (windows &optional win start)
1261 "Calculate the start of the first window.
1263 WINDOWS is a chain of windows to work with. WIN is the window
1264 to recenter around. It is assumed that WIN starts at position
1265 START."
1266 (or win
1267 (setq win (selected-window)))
1268 (or start
1269 (setq start (window-start win)))
1270 (let ((guess (follow-estimate-first-window-start windows win start)))
1271 (if (car guess)
1272 (cdr guess)
1273 ;; The guess wasn't exact, try to enhance it.
1274 (let ((win-start (follow-calc-win-start windows (cdr guess) win)))
1275 (cond ((= win-start start)
1276 (follow-debug-message "exact")
1277 (cdr guess))
1278 ((< win-start start)
1279 (follow-debug-message "above")
1280 (follow-calculate-first-window-start-from-above
1281 windows (cdr guess) win start))
1283 (follow-debug-message "below")
1284 (follow-calculate-first-window-start-from-below
1285 windows (cdr guess) win start)))))))
1288 ;; `exact' is disabled due to XEmacs and fonts of variable
1289 ;; height.
1290 (defun follow-estimate-first-window-start (windows win start)
1291 "Estimate the position of the first window.
1293 Returns (EXACT . POS). If EXACT is non-nil, POS is the starting
1294 position of the first window. Otherwise it is a good guess."
1295 (let ((pred (car (follow-split-followers windows win)))
1296 (exact nil))
1297 (save-excursion
1298 (goto-char start)
1299 ;(setq exact (bolp))
1300 (vertical-motion 0 win)
1301 (while pred
1302 (vertical-motion (- 1 (window-height (car pred))) (car pred))
1303 (if (not (bolp))
1304 (setq exact nil))
1305 (setq pred (cdr pred)))
1306 (cons exact (point)))))
1309 ;; Find the starting point, start at GUESS and search downward.
1310 ;; The returned point is always a point below GUESS.
1312 (defun follow-calculate-first-window-start-from-above
1313 (windows guess win start)
1314 (save-excursion
1315 (let ((done nil)
1316 win-start
1317 res)
1318 (goto-char guess)
1319 (while (not done)
1320 (if (not (= (vertical-motion 1 (car windows)) 1))
1321 ;; Hit bottom! (Can we really do this?)
1322 ;; We'll keep it, since it ensures termination.
1323 (progn
1324 (setq done t)
1325 (setq res (point-max)))
1326 (setq win-start (follow-calc-win-start windows (point) win))
1327 (if (>= win-start start)
1328 (progn
1329 (setq done t)
1330 (setq res (point))))))
1331 res)))
1334 ;; Find the starting point, start at GUESS and search upward. Return
1335 ;; a point on the same line as GUESS, or above.
1337 ;; (Is this ever used? I must make sure it works just in case it is
1338 ;; ever called.)
1340 (defun follow-calculate-first-window-start-from-below
1341 (windows guess &optional win start)
1342 (setq win (or win (selected-window)))
1343 (setq start (or start (window-start win)))
1344 (save-excursion
1345 (let ((done nil)
1346 win-start
1347 res)
1348 ;; Always calculate what happend when no line is displayed in the first
1349 ;; window. (The `previous' res is needed below!)
1350 (goto-char guess)
1351 (vertical-motion 0 (car windows))
1352 (setq res (point))
1353 (while (not done)
1354 (if (not (= (vertical-motion -1 (car windows)) -1))
1355 ;; Hit roof!
1356 (progn
1357 (setq done t)
1358 (setq res (point-min)))
1359 (setq win-start (follow-calc-win-start windows (point) win))
1360 (cond ((= win-start start) ; Perfect match, use this value
1361 (setq done t)
1362 (setq res (point)))
1363 ((< win-start start) ; Walked to far, use preious result
1364 (setq done t))
1365 (t ; Store result for next iteration
1366 (setq res (point))))))
1367 res)))
1369 ;;}}}
1370 ;;{{{ Avoid tail recenter
1372 ;; This sets the window internal flag `force_start'. The effect is that
1373 ;; windows only displaying the tail isn't recentered.
1374 ;; Has to be called before every redisplay... (Great isn't it?)
1376 ;; XEmacs doesn't recenter the tail, GOOD!
1378 ;; A window displaying only the tail, is a windows whose
1379 ;; window-start position is equal to (point-max) of the buffer it
1380 ;; displays.
1382 ;; This function is also added to `post-command-idle-hook', introduced
1383 ;; in Emacs 19.30. This is needed since the vaccine injected by the
1384 ;; call from `post-command-hook' only works until the next redisplay.
1385 ;; It is possible that the functions in the `post-command-idle-hook'
1386 ;; can cause a redisplay, and hence a new vaccine is needed.
1388 ;; Sometimes, calling this function could actually cause a redisplay,
1389 ;; especially if it is placed in the debug filter section. I must
1390 ;; investigate this further...
1392 (defun follow-avoid-tail-recenter (&rest rest)
1393 "Make sure windows displaying the end of a buffer aren't recentered.
1395 This is done by reading and rewriting the start position of
1396 non-first windows in Follow mode."
1397 (if follow-avoid-tail-recenter-p
1398 (let* ((orig-buffer (current-buffer))
1399 (top (frame-first-window (selected-frame)))
1400 (win top)
1401 (who '()) ; list of (buffer . frame)
1402 start
1403 pair) ; (buffer . frame)
1404 ;; If the only window in the frame is a minibuffer
1405 ;; window, `next-window' will never find it again...
1406 (if (window-minibuffer-p top)
1408 (while ;; look, no body!
1409 (progn
1410 (setq start (window-start win))
1411 (set-buffer (window-buffer win))
1412 (setq pair (cons (window-buffer win) (window-frame win)))
1413 (if (member pair who)
1414 (if (and (boundp 'follow-mode) follow-mode
1415 (eq (point-max) start))
1416 ;; Write the same window start back, but don't
1417 ;; set the NOFORCE flag.
1418 (set-window-start win start))
1419 (setq who (cons pair who)))
1420 (setq win (next-window win 'not t))
1421 (not (eq win top)))) ;; Loop while this is true.
1422 (set-buffer orig-buffer)))))
1424 ;;}}}
1426 ;;}}}
1427 ;;{{{ Post Command Hook
1429 ;; The magic little box. This function is called after every command.
1431 ;; This is not as complicated as it seems. It is simply a list of common
1432 ;; display situations and the actions to take, plus commands for redrawing
1433 ;; the screen if it should be unaligned.
1435 ;; We divide the check into two parts; whether we are at the end or not.
1436 ;; This is due to the fact that the end can actaually be visible
1437 ;; in several window even though they are aligned.
1439 (defun follow-post-command-hook ()
1440 "Ensure that the windows in Follow mode are adjacent after each command."
1441 (setq follow-inside-post-command-hook t)
1442 (if (or (not (input-pending-p))
1443 ;; Sometimes, in XEmacs, mouse events are not handled
1444 ;; properly by `input-pending-p'. A typical example is
1445 ;; when clicking on a node in `info'.
1446 (and (boundp 'current-mouse-event)
1447 (symbol-value 'current-mouse-event)
1448 (fboundp 'button-event-p)
1449 (funcall (symbol-function 'button-event-p)
1450 (symbol-value 'current-mouse-event))))
1451 ;; Work in the selected window, not in the current buffer.
1452 (let ((orig-buffer (current-buffer))
1453 (win (selected-window)))
1454 (set-buffer (window-buffer win))
1455 (or (and (symbolp this-command)
1456 (get this-command 'follow-mode-use-cache))
1457 (follow-invalidate-cache))
1458 (if (and (boundp 'follow-mode) follow-mode
1459 (not (window-minibuffer-p win)))
1460 ;; The buffer shown in the selected window is in follow
1461 ;; mode, lets find the current state of the display and
1462 ;; cache the result for speed (i.e. `aligned' and `visible'.)
1463 (let* ((windows (inline (follow-all-followers win)))
1464 (dest (point))
1465 (win-start-end (inline
1466 (follow-update-window-start (car windows))
1467 (follow-windows-start-end windows)))
1468 (aligned (follow-windows-aligned-p win-start-end))
1469 (visible (follow-pos-visible dest win win-start-end)))
1470 (if (not (and aligned visible))
1471 (follow-invalidate-cache))
1472 (inline (follow-avoid-tail-recenter))
1473 ;; Select a window to display the point.
1474 (or follow-internal-force-redisplay
1475 (progn
1476 (if (eq dest (point-max))
1477 ;; We're at the end, we have to be careful since
1478 ;; the display can be aligned while `dest' can
1479 ;; be visible in several windows.
1480 (cond
1481 ;; Select the current window, but only when
1482 ;; the display is correct. (When inserting
1483 ;; character in a tail window, the display is
1484 ;; not correct, as they are shown twice.)
1486 ;; Never stick to the current window after a
1487 ;; deletion. The reason is cosmetic, when
1488 ;; typing `DEL' in a window showing only the
1489 ;; end of the file, character are removed
1490 ;; from the window above, which is very
1491 ;; unintuitive.
1492 ((and visible
1493 aligned
1494 (not (memq this-command
1495 '(backward-delete-char
1496 delete-backward-char
1497 backward-delete-char-untabify
1498 kill-region))))
1499 (follow-debug-message "Max: same"))
1500 ;; If the end is visible, and the window
1501 ;; doesn't seems like it just has been moved,
1502 ;; select it.
1503 ((follow-select-if-end-visible win-start-end)
1504 (follow-debug-message "Max: end visible")
1505 (setq visible t)
1506 (setq aligned nil)
1507 (goto-char dest))
1508 ;; Just show the end...
1510 (follow-debug-message "Max: default")
1511 (select-window (car (reverse windows)))
1512 (goto-char dest)
1513 (setq visible nil)
1514 (setq aligned nil)))
1516 ;; We're not at the end, here life is much simpler.
1517 (cond
1518 ;; This is the normal case!
1519 ;; It should be optimized for speed.
1520 ((and visible aligned)
1521 (follow-debug-message "same"))
1522 ;; Pick a position in any window. If the
1523 ;; display is ok, this will pick the `correct'
1524 ;; window. If the display is wierd do this
1525 ;; anyway, this will be the case after a delete
1526 ;; at the beginning of the window.
1527 ((follow-select-if-visible dest win-start-end)
1528 (follow-debug-message "visible")
1529 (setq visible t)
1530 (goto-char dest))
1531 ;; Not visible anywhere else, lets pick this one.
1532 ;; (Is this case used?)
1533 (visible
1534 (follow-debug-message "visible in selected."))
1535 ;; Far out!
1536 ((eq dest (point-min))
1537 (follow-debug-message "min")
1538 (select-window (car windows))
1539 (goto-char dest)
1540 (set-window-start (selected-window) (point-min))
1541 (setq win-start-end (follow-windows-start-end windows))
1542 (follow-invalidate-cache)
1543 (setq visible t)
1544 (setq aligned nil))
1545 ;; If we can position the cursor without moving the first
1546 ;; window, do it. This is the case that catches `RET'
1547 ;; at the bottom of a window.
1548 ((follow-select-if-visible-from-first dest windows)
1549 (follow-debug-message "Below first")
1550 (setq visible t)
1551 (setq aligned t)
1552 (follow-redisplay windows (car windows))
1553 (goto-char dest))
1554 ;; None of the above. For simplicity, we stick to the
1555 ;; selected window.
1557 (follow-debug-message "None")
1558 (setq visible nil)
1559 (setq aligned nil))))
1560 ;; If a new window has been selected, make sure that the
1561 ;; old is not scrolled when the point is outside the
1562 ;; window.
1563 (or (eq win (selected-window))
1564 (let ((p (window-point win)))
1565 (set-window-start win (window-start win) nil)
1566 (set-window-point win p)))))
1567 ;; Make sure the point is visible in the selected window.
1568 ;; (This could lead to a scroll.)
1569 (if (or visible
1570 (follow-pos-visible dest win win-start-end))
1572 (sit-for 0)
1573 (follow-avoid-tail-recenter)
1574 (setq win-start-end (follow-windows-start-end windows))
1575 (follow-invalidate-cache)
1576 (setq aligned nil))
1577 ;; Redraw the windows whenever needed.
1578 (if (or follow-internal-force-redisplay
1579 (not (or aligned
1580 (follow-windows-aligned-p win-start-end)))
1581 (not (inline (follow-point-visible-all-windows-p
1582 win-start-end))))
1583 (progn
1584 (setq follow-internal-force-redisplay nil)
1585 (follow-redisplay windows (selected-window))
1586 (setq win-start-end (follow-windows-start-end windows))
1587 (follow-invalidate-cache)
1588 ;; When the point ends up in another window. This
1589 ;; happends when dest is in the beginning of the
1590 ;; file and the selected window is not the first.
1591 ;; It can also, in rare situations happend when
1592 ;; long lines are used and there is a big
1593 ;; difference between the width of the windows.
1594 ;; (When scrolling one line in a wide window which
1595 ;; will cause a move larger that an entire small
1596 ;; window.)
1597 (if (follow-pos-visible dest win win-start-end)
1599 (follow-select-if-visible dest win-start-end)
1600 (goto-char dest))))
1602 ;; If the region is visible, make it look good when spanning
1603 ;; multiple windows.
1604 (if (or (and (boundp 'mark-active) (symbol-value 'mark-active))
1605 (and (fboundp 'region-active-p)
1606 (funcall (symbol-function 'region-active-p))))
1607 (follow-maximize-region
1608 (selected-window) windows win-start-end))
1610 (inline (follow-avoid-tail-recenter))
1611 ;; DEBUG
1612 ;;(if (not (follow-windows-aligned-p
1613 ;; (follow-windows-start-end windows)))
1614 ;; (message "follow-mode: windows still unaligend!"))
1615 ;; END OF DEBUG
1616 ) ; Matches (let*
1617 ;; Buffer not in follow mode:
1618 ;; We still must update the windows displaying the tail so that
1619 ;; Emacs won't recenter them.
1620 (follow-avoid-tail-recenter))
1621 (set-buffer orig-buffer)))
1622 (setq follow-inside-post-command-hook nil))
1624 ;;}}}
1625 ;;{{{ The region
1627 ;; Tries to make the highlighted area representing the region look
1628 ;; good when spanning several windows.
1630 ;; Not perfect, as the point can't be placed at window end, only at
1631 ;; end-1. This will highlight a little bit in windows above
1632 ;; the current.
1634 (defun follow-maximize-region (win windows win-start-end)
1635 "Make a highlighted region stretching multiple windows look good."
1636 (let* ((all (follow-split-followers windows win))
1637 (pred (car all))
1638 (succ (cdr all))
1639 data)
1640 (while pred
1641 (setq data (assq (car pred) win-start-end))
1642 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1643 (setq pred (cdr pred)))
1644 (while succ
1645 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1646 (setq succ (cdr succ)))))
1648 ;;}}}
1649 ;;{{{ Scroll bar
1651 ;;;; Scroll-bar support code.
1653 ;; Why is it needed? Well, if the selected window is in follow mode,
1654 ;; all its follower stick to it blindly. If one of them is scrolled,
1655 ;; it immediately returns to the original position when the mouse is
1656 ;; released. If the selected window is not a follower of the dragged
1657 ;; window the windows will be unaligned.
1659 ;; The advices doesn't get compiled. Aestetically, this might be a
1660 ;; problem but in practical life it isn't.
1662 ;; Discussion: Now when the other windows in the chain follow the
1663 ;; dragged, should we really select it?
1665 (cond ((fboundp 'scroll-bar-drag)
1667 ;;; Emacs style scrollbars.
1670 ;; Select the dragged window if it is a follower of the
1671 ;; selected window.
1673 ;; Generate advices of the form:
1674 ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
1675 ;; "Adviced by `follow-mode'."
1676 ;; (follow-redraw-after-event (ad-get-arg 0)))
1677 (let ((cmds '(scroll-bar-drag
1678 scroll-bar-drag-1 ; Executed at every move.
1679 scroll-bar-scroll-down
1680 scroll-bar-scroll-up
1681 scroll-bar-set-window-start)))
1682 (while cmds
1683 (eval
1684 `(defadvice ,(intern (symbol-name (car cmds)))
1685 (after
1686 ,(intern (concat "follow-" (symbol-name (car cmds))))
1687 activate)
1688 "Adviced by Follow mode."
1689 (follow-redraw-after-event (ad-get-arg 0))))
1690 (setq cmds (cdr cmds))))
1693 (defun follow-redraw-after-event (event)
1694 "Adviced by Follow mode."
1695 (condition-case nil
1696 (let* ((orig-win (selected-window))
1697 (win (nth 0 (funcall
1698 (symbol-function 'event-start) event)))
1699 (fmode (assq 'follow-mode
1700 (buffer-local-variables
1701 (window-buffer win)))))
1702 (if (and fmode (cdr fmode))
1703 ;; The selected window is in follow-mode
1704 (progn
1705 ;; Recenter around the dragged window.
1706 (select-window win)
1707 (follow-redisplay)
1708 (select-window orig-win))))
1709 (error nil))))
1712 ((fboundp 'scrollbar-vertical-drag)
1714 ;;; XEmacs style scrollbars.
1717 ;; Advice all scrollbar functions on the form:
1719 ;; (defadvice scrollbar-line-down
1720 ;; (after follow-scrollbar-line-down activate)
1721 ;; (follow-xemacs-scrollbar-support (ad-get-arg 0)))
1723 (let ((cmds '(scrollbar-line-down ; Window
1724 scrollbar-line-up
1725 scrollbar-page-down ; Object
1726 scrollbar-page-up
1727 scrollbar-to-bottom ; Window
1728 scrollbar-to-top
1729 scrollbar-vertical-drag ; Object
1732 (while cmds
1733 (eval
1734 `(defadvice ,(intern (symbol-name (car cmds)))
1735 (after
1736 ,(intern (concat "follow-" (symbol-name (car cmds))))
1737 activate)
1738 "Adviced by `follow-mode'."
1739 (follow-xemacs-scrollbar-support (ad-get-arg 0))))
1740 (setq cmds (cdr cmds))))
1743 (defun follow-xemacs-scrollbar-support (window)
1744 "Redraw windows showing the same buffer as shown in WINDOW.
1745 WINDOW is either the dragged window, or a cons containing the
1746 window as its first element. This is called while the user drags
1747 the scrollbar.
1749 WINDOW can be an object or a window."
1750 (condition-case nil
1751 (progn
1752 (if (consp window)
1753 (setq window (car window)))
1754 (let ((fmode (assq 'follow-mode
1755 (buffer-local-variables
1756 (window-buffer window))))
1757 (orig-win (selected-window)))
1758 (if (and fmode (cdr fmode))
1759 (progn
1760 ;; Recenter around the dragged window.
1761 (select-window window)
1762 (follow-redisplay)
1763 (select-window orig-win)))))
1764 (error nil)))))
1766 ;;}}}
1767 ;;{{{ Process output
1769 ;; The following sections installs a spy that listens to process
1770 ;; output and tries to reposition the windows whose buffers are in
1771 ;; Follow mode. We play safe as much as possible...
1773 ;; When follow-mode is activated all active processes are
1774 ;; intercepted. All new processes that change their filter function
1775 ;; using `set-process-filter' are also intercepted. The reason is
1776 ;; that a process can cause a redisplay recentering "tail" windows.
1777 ;; Note that it doesn't hurt to spy on more processes than needed.
1779 ;; Technically, we set the process filter to `follow-generic-filter'.
1780 ;; The original filter is stored in `follow-process-filter-alist'.
1781 ;; Our generic filter calls the original filter, or inserts the
1782 ;; output into the buffer, if the buffer originally didn't have an
1783 ;; output filter. It also makes sure that the windows connected to
1784 ;; the buffer are aligned.
1786 ;; Discussion: How do we find processes that don't call
1787 ;; `set-process-filter'? (How often are processes created in a
1788 ;; buffer after Follow mode are activated?)
1790 ;; Discussion: Should we also advice `process-filter' to make our
1791 ;; filter invisible to others?
1793 ;;{{{ Advice for `set-process-filter'
1795 ;; Do not call this with 'follow-generic-filter as the name of the
1796 ;; filter...
1798 (defadvice set-process-filter (before follow-set-process-filter activate)
1799 "Ensure process output will be displayed correctly in Follow mode buffers.
1801 Follow mode inserts its own process filter to do its
1802 magic stuff before the real process filter is called."
1803 (if follow-intercept-processes
1804 (progn
1805 (setq follow-process-filter-alist
1806 (delq (assq (ad-get-arg 0) follow-process-filter-alist)
1807 follow-process-filter-alist))
1808 (follow-tidy-process-filter-alist)
1809 (cond ((eq (ad-get-arg 1) t))
1810 ((eq (ad-get-arg 1) nil)
1811 (ad-set-arg 1 'follow-generic-filter))
1813 (setq follow-process-filter-alist
1814 (cons (cons (ad-get-arg 0) (ad-get-arg 1))
1815 follow-process-filter-alist))
1816 (ad-set-arg 1 'follow-generic-filter))))))
1819 (defun follow-call-set-process-filter (proc filter)
1820 "Call original `set-process-filter' without the Follow mode advice."
1821 (ad-disable-advice 'set-process-filter 'before
1822 'follow-set-process-filter)
1823 (ad-activate 'set-process-filter)
1824 (prog1
1825 (set-process-filter proc filter)
1826 (ad-enable-advice 'set-process-filter 'before
1827 'follow-set-process-filter)
1828 (ad-activate 'set-process-filter)))
1831 (defadvice process-filter (after follow-process-filter activate)
1832 "Return the original process filter, not `follow-generic-filter'."
1833 (cond ((eq ad-return-value 'follow-generic-filter)
1834 (setq ad-return-value
1835 (cdr-safe (assq (ad-get-arg 0)
1836 follow-process-filter-alist))))))
1839 (defun follow-call-process-filter (proc)
1840 "Call original `process-filter' without the Follow mode advice."
1841 (ad-disable-advice 'process-filter 'after
1842 'follow-process-filter)
1843 (ad-activate 'process-filter)
1844 (prog1
1845 (process-filter proc)
1846 (ad-enable-advice 'process-filter 'after
1847 'follow-process-filter)
1848 (ad-activate 'process-filter)))
1851 (defun follow-tidy-process-filter-alist ()
1852 "Remove old processes from `follow-process-filter-alist'."
1853 (let ((alist follow-process-filter-alist)
1854 (ps (process-list))
1855 (new ()))
1856 (while alist
1857 (if (and (not (memq (process-status (car (car alist)))
1858 '(exit signal closed nil)))
1859 (memq (car (car alist)) ps))
1860 (setq new (cons (car alist) new)))
1861 (setq alist (cdr alist)))
1862 (setq follow-process-filter-alist new)))
1864 ;;}}}
1865 ;;{{{ Start/stop interception of processes.
1867 ;; Normally, all new processed are intercepted by our `set-process-filter'.
1868 ;; This is needed to intercept old processed that were started before we were
1869 ;; loaded, and processes we have forgotten by calling
1870 ;; `follow-stop-intercept-process-output'.
1872 (defun follow-intercept-process-output ()
1873 "Intercept all active processes.
1875 This is needed so that Follow mode can track all display events in the
1876 system. (See `follow-mode'.)"
1877 (interactive)
1878 (let ((list (process-list)))
1879 (while list
1880 (if (eq (process-filter (car list)) 'follow-generic-filter)
1882 ;; The custom `set-process-filter' defined above.
1883 (set-process-filter (car list) (process-filter (car list))))
1884 (setq list (cdr list))))
1885 (setq follow-intercept-processes t))
1888 (defun follow-stop-intercept-process-output ()
1889 "Stop Follow mode from spying on processes.
1891 All current spypoints are removed and no new will be added.
1893 The effect is that Follow mode won't be able to handle buffers
1894 connected to processes.
1896 The only reason to call this function is if the Follow mode spy filter
1897 would interfere with some other package. If this happens, please
1898 report this using the `report-emacs-bug' function."
1899 (interactive)
1900 (follow-tidy-process-filter-alist)
1901 (let ((list (process-list)))
1902 (while list
1903 (if (eq (follow-call-process-filter (car list)) 'follow-generic-filter)
1904 (progn
1905 (follow-call-set-process-filter
1906 (car list)
1907 (cdr-safe (assq (car list) follow-process-filter-alist)))
1908 (setq follow-process-filter-alist
1909 (delq (assq (car list) follow-process-filter-alist)
1910 follow-process-filter-alist))))
1911 (setq list (cdr list))))
1912 (setq follow-intercept-processes nil))
1914 ;;}}}
1915 ;;{{{ The filter
1917 ;; The following section is a naive method to make buffers with
1918 ;; process output to work with Follow mode. Whenever the start of the
1919 ;; window displaying the buffer is moved, we moves it back to its
1920 ;; original position and try to select a new window. (If we fail,
1921 ;; the normal redisplay functions of Emacs will scroll it right
1922 ;; back!)
1924 (defun follow-generic-filter (proc output)
1925 "Process output filter for process connected to buffers in Follow mode."
1926 (let* ((old-buffer (current-buffer))
1927 (orig-win (selected-window))
1928 (buf (process-buffer proc))
1929 (win (and buf (if (eq buf (window-buffer orig-win))
1930 orig-win
1931 (get-buffer-window buf t))))
1932 (return-to-orig-win (and win (not (eq win orig-win))))
1933 (orig-window-start (and win (window-start win))))
1935 ;; If input is pending, the `sit-for' below won't redraw the
1936 ;; display. In that case, calling `follow-avoid-tail-recenter' may
1937 ;; provoke the process hadnling code to sceduling a redisplay.
1938 ;(or (input-pending-p)
1939 ; (follow-avoid-tail-recenter))
1941 ;; Output the `output'.
1942 (let ((filter (cdr-safe (assq proc follow-process-filter-alist))))
1943 (cond
1944 ;; Call the original filter function
1945 (filter
1946 (funcall filter proc output))
1948 ;; No filter, but we've got a buffer. Just output into it.
1949 (buf
1950 (set-buffer buf)
1951 (if (not (marker-buffer (process-mark proc)))
1952 (set-marker (process-mark proc) (point-max)))
1953 (let ((moving (= (point) (process-mark proc)))
1954 deactivate-mark
1955 (inhibit-read-only t))
1956 (save-excursion
1957 (goto-char (process-mark proc))
1958 ;; `insert-before-markers' just in case the users next
1959 ;; command is M-y.
1960 (insert-before-markers output)
1961 (set-marker (process-mark proc) (point)))
1962 (if moving (goto-char (process-mark proc)))))))
1964 ;; If we're in follow mode, do our stuff. Select a new window and
1965 ;; redisplay. (Actually, it is redundant to check `buf', but I
1966 ;; feel it's more correct.)
1967 (if (and buf win (window-live-p win))
1968 (progn
1969 (set-buffer buf)
1970 (if (and (boundp 'follow-mode) follow-mode)
1971 (progn
1972 (select-window win)
1973 (let* ((windows (follow-all-followers win))
1974 (win-start-end (follow-windows-start-end windows))
1975 (new-window-start (window-start win))
1976 (new-window-point (window-point win)))
1977 (cond
1978 ;; The start of the selected window was repositioned.
1979 ;; Try to use the original start position and continue
1980 ;; working with a window to the "right" in the window
1981 ;; chain. This will create the effect that the output
1982 ;; starts in one window and continues into the next.
1984 ;; If the display has changed so much that it is not
1985 ;; possible to keep the original window fixed and still
1986 ;; display the point then we give up and use the new
1987 ;; window start.
1989 ;; This case is typically used when the process filter
1990 ;; tries to reposition the start of the window in order
1991 ;; to view the tail of the output.
1992 ((not (eq orig-window-start new-window-start))
1993 (follow-debug-message "filter: Moved")
1994 (set-window-start win orig-window-start)
1995 (follow-redisplay windows win)
1996 (setq win-start-end (follow-windows-start-end windows))
1997 (follow-select-if-visible new-window-point
1998 win-start-end)
1999 (goto-char new-window-point)
2000 (if (eq win (selected-window))
2001 (set-window-start win new-window-start))
2002 (setq win-start-end (follow-windows-start-end windows)))
2003 ;; Stick to this window, if point is visible in it.
2004 ((pos-visible-in-window-p new-window-point)
2005 (follow-debug-message "filter: Visible in window"))
2006 ;; Avoid redisplaying the first window. If the
2007 ;; point is visible at a window below,
2008 ;; redisplay and select it.
2009 ((follow-select-if-visible-from-first
2010 new-window-point windows)
2011 (follow-debug-message "filter: Seen from first")
2012 (follow-redisplay windows (car windows))
2013 (goto-char new-window-point)
2014 (setq win-start-end
2015 (follow-windows-start-end windows)))
2016 ;; None of the above. We stick to the current window.
2018 (follow-debug-message "filter: nothing")))
2020 ;; Here we have slected a window. Make sure the
2021 ;; windows are aligned and the point is visible
2022 ;; in the selected window.
2023 (if (and (not (follow-pos-visible
2024 (point) (selected-window) win-start-end))
2025 (not return-to-orig-win))
2026 (progn
2027 (sit-for 0)
2028 (setq win-start-end
2029 (follow-windows-start-end windows))))
2031 (if (or follow-internal-force-redisplay
2032 (not (follow-windows-aligned-p win-start-end)))
2033 (follow-redisplay windows)))))))
2035 ;; return to the original window.
2036 (if return-to-orig-win
2037 (select-window orig-win))
2038 ;; Restore the orignal buffer, unless the filter explicitly
2039 ;; changed buffer or killed the old buffer.
2040 (if (and (eq buf (current-buffer))
2041 (buffer-name old-buffer))
2042 (set-buffer old-buffer)))
2044 (follow-invalidate-cache)
2046 ;; Normally, if the display has been changed, it is redrawn. All
2047 ;; windows showing only the end of a buffer are unconditionally
2048 ;; recentered; we can't prevent that by calling
2049 ;; `follow-avoid-tail-recenter'.
2051 ;; We force a redisplay here on our own, so Emacs does need to.
2052 ;; (However, redisplaying when there's input available just seems
2053 ;; to make things worse, so we exclude that case.)
2054 (if (and follow-avoid-tail-recenter-p
2055 (not (input-pending-p)))
2056 (sit-for 0)))
2058 ;;}}}
2060 ;;}}}
2061 ;;{{{ Window size change
2063 ;; In Emacs 19.29, the functions in `window-size-change-functions' are
2064 ;; called every time a window in a frame changes size. Most notably, it
2065 ;; is called after the frame has been resized.
2067 ;; We basically call our post-command-hook for every buffer that is
2068 ;; visible in any window in the resized frame, which is in follow-mode.
2070 ;; Since this function can be called indirectly from
2071 ;; `follow-post-command-hook' we have a potential infinite loop. We
2072 ;; handle this problem by simply not doing anything at all in this
2073 ;; situation. The variable `follow-inside-post-command-hook' contains
2074 ;; information about whether the execution actually is inside the
2075 ;; post-command-hook or not.
2077 (if (boundp 'window-size-change-functions)
2078 (add-hook 'window-size-change-functions 'follow-window-size-change))
2081 (defun follow-window-size-change (frame)
2082 "Redraw all windows in FRAME, when in Follow mode."
2083 ;; Below, we call `post-command-hook'. This makes sure that we
2084 ;; don't start a mutually recursive endless loop.
2085 (if follow-inside-post-command-hook
2087 (let ((buffers '())
2088 (orig-window (selected-window))
2089 (orig-buffer (current-buffer))
2090 (orig-frame (selected-frame))
2091 windows
2092 buf)
2093 (select-frame frame)
2094 (unwind-protect
2095 (walk-windows
2096 (function
2097 (lambda (win)
2098 (setq buf (window-buffer win))
2099 (if (memq buf buffers)
2101 (set-buffer buf)
2102 (if (and (boundp 'follow-mode)
2103 follow-mode)
2104 (progn
2105 (setq windows (follow-all-followers win))
2106 (if (memq orig-window windows)
2107 (progn
2108 ;; Make sure we're redrawing around the
2109 ;; selected window.
2111 ;; We must be really careful not to do this
2112 ;; when we are (indirectly) called by
2113 ;; `post-command-hook'.
2114 (select-window orig-window)
2115 (follow-post-command-hook)
2116 (setq orig-window (selected-window)))
2117 (follow-redisplay windows win))
2118 (setq buffers (cons buf buffers))))))))
2119 (select-frame orig-frame)
2120 (set-buffer orig-buffer)
2121 (select-window orig-window)))))
2123 ;;}}}
2125 ;;{{{ XEmacs isearch
2127 ;; In XEmacs, isearch often finds matches in other windows than the
2128 ;; currently selected. However, when exiting the old window
2129 ;; configuration is restored, with the exception of the beginning of
2130 ;; the start of the window for the selected window. This is not much
2131 ;; help for us.
2133 ;; We overwrite the stored window configuration with the current,
2134 ;; unless we are in `slow-search-mode', i.e. only a few lines
2135 ;; of text is visible.
2137 (if (featurep 'xemacs)
2138 (defadvice isearch-done (before follow-isearch-done activate)
2139 (if (and (boundp 'follow-mode)
2140 follow-mode
2141 (boundp 'isearch-window-configuration)
2142 isearch-window-configuration
2143 (boundp 'isearch-slow-terminal-mode)
2144 (not isearch-slow-terminal-mode))
2145 (let ((buf (current-buffer)))
2146 (setq isearch-window-configuration
2147 (current-window-configuration))
2148 (set-buffer buf)))))
2150 ;;}}}
2151 ;;{{{ Tail window handling
2153 ;; In Emacs (not XEmacs) windows showing nothing are sometimes
2154 ;; recentered. When in Follow mode, this is not desirable for
2155 ;; non-first windows in the window chain. This section tries to
2156 ;; make the windows stay where they should be.
2158 ;; If the display is updated, all windows starting at (point-max) are
2159 ;; going to be recentered at the next redisplay, unless we do a
2160 ;; read-and-write cycle to update the `force' flag inside the windows.
2162 ;; In 19.30, a new varible `window-scroll-functions' is called every
2163 ;; time a window is recentered. It is not perfect for our situation,
2164 ;; since when it is called for a tail window, it is to late. However,
2165 ;; if it is called for another window, we can try to update our
2166 ;; windows.
2168 ;; By patching `sit-for' we can make sure that to catch all explicit
2169 ;; updates initiated by lisp programs. Internal calls, on the other
2170 ;; hand, are not handled.
2172 ;; Please note that the function `follow-avoid-tail-recenter' is also
2173 ;; called from other places, e.g. `post-command-hook' and
2174 ;; `post-command-idle-hook'.
2176 ;; If this function is called it is too late for this window, but
2177 ;; we might save other windows from being recentered.
2179 (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions))
2180 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t))
2183 ;; This prevents all packages that calls `sit-for' directly
2184 ;; to recenter tail windows.
2186 (if follow-avoid-tail-recenter-p
2187 (defadvice sit-for (before follow-sit-for activate)
2188 "Adviced by Follow mode.
2190 Avoid to recenter windows displaying only the end of a file as when
2191 displaying a short file in two windows, using Follow mode."
2192 (follow-avoid-tail-recenter)))
2195 ;; Without this advice, `mouse-drag-region' would start to recenter
2196 ;; tail windows.
2198 (if (and follow-avoid-tail-recenter-p
2199 (fboundp 'move-overlay))
2200 (defadvice move-overlay (before follow-move-overlay activate)
2201 "Adviced by Follow mode.
2202 Don't recenter windows showing only the end of a buffer.
2203 This prevents `mouse-drag-region' from messing things up."
2204 (follow-avoid-tail-recenter)))
2206 ;;}}}
2207 ;;{{{ profile support
2209 ;; The following (non-evaluated) section can be used to
2210 ;; profile this package using `elp'.
2212 ;; Invalid indentation on purpose!
2214 (cond (nil
2215 (setq elp-function-list
2216 '(window-end
2217 vertical-motion
2218 ; sit-for ;; elp can't handle advices...
2219 follow-mode
2220 follow-all-followers
2221 follow-split-followers
2222 follow-redisplay
2223 follow-downward
2224 follow-calculate-first-window-start
2225 follow-estimate-first-window-start
2226 follow-calculate-first-window-start-from-above
2227 follow-calculate-first-window-start-from-below
2228 follow-calc-win-end
2229 follow-calc-win-start
2230 follow-pos-visible
2231 follow-windows-start-end
2232 follow-cache-valid-p
2233 follow-select-if-visible
2234 follow-select-if-visible-from-first
2235 follow-windows-aligned-p
2236 follow-point-visible-all-windows-p
2237 follow-avoid-tail-recenter
2238 follow-update-window-start
2239 follow-post-command-hook
2240 ))))
2242 ;;}}}
2244 ;;{{{ The end
2246 (defun follow-unload-function ()
2247 "Unload Follow mode library."
2248 (follow-stop-intercept-process-output)
2249 (dolist (group '((before
2250 ;; XEmacs
2251 isearch-done
2252 ;; both
2253 set-process-filter sit-for move-overlay)
2254 (after
2255 ;; Emacs
2256 scroll-bar-drag scroll-bar-drag-1 scroll-bar-scroll-down
2257 scroll-bar-scroll-up scroll-bar-set-window-start
2258 ;; XEmacs
2259 scrollbar-line-down scrollbar-line-up scrollbar-page-down
2260 scrollbar-page-up scrollbar-to-bottom scrollbar-to-top
2261 scrollbar-vertical-drag
2262 ;; both
2263 process-filter)))
2264 (let ((class (car group)))
2265 (dolist (fun (cdr group))
2266 (when (functionp fun)
2267 (condition-case nil
2268 (progn
2269 (ad-remove-advice fun class
2270 (intern (concat "follow-" (symbol-name fun))))
2271 (ad-update fun))
2272 (error nil))))))
2273 ;; continue standard processing
2274 nil)
2277 ;; We're done!
2280 (provide 'follow)
2282 ;;}}}
2284 ;; /------------------------------------------------------------------------\
2285 ;; | "I [..] am rarely happier then when spending an entire day programming |
2286 ;; | my computer to perform automatically a task that it would otherwise |
2287 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
2288 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
2289 ;; | worth the investment of a day's happy activity working out a way to |
2290 ;; | save it". -- Douglas Adams, "Last Chance to See" |
2291 ;; \------------------------------------------------------------------------/
2293 ;; arch-tag: 7b16bb1a-808c-4991-a8cc-66d3822936d0
2294 ;;; follow.el ends here