(ps-default-fg, ps-default-bg): Fix typos in docstrings.
[emacs.git] / lisp / follow.el
blob74dc12f88814772952b9941b76b75ba03e67bff7
1 ;;; follow.el --- synchronize windows showing the same buffer
3 ;; Copyright (C) 1995, 1996, 1997, 1999, 2001, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Author: Anders Lindgren <andersl@andersl.com>
7 ;; Maintainer: Anders Lindgren <andersl@andersl.com>
8 ;; Created: 1995-05-25
9 ;; Keywords: display, window, minor-mode, convenience
10 ;; Last Changed: 1999-11-17
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)
17 ;; any later version.
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.
29 ;;; Commentary:
31 ;;{{{ Documentation
33 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
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
46 ;; movement commands.
48 ;; Follow mode comes to its prime when 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
53 ;; mileage may vary).
55 ;; To test this package, make sure `follow' is loaded, or will be
56 ;; autoloaded when activated (see below). Then do the following:
58 ;; * Find your favorite file (preferably a long one).
60 ;; * Resize Emacs so that it will be wide enough for two full size
61 ;; columns. Delete the other windows and split the window with
62 ;; the commands `C-x 1 C-x 3'.
64 ;; * Give the command:
65 ;; M-x follow-mode <RETURN>
67 ;; * Now the display should look something like (assuming the text "71"
68 ;; is on line 71):
70 ;; +----------+----------+
71 ;; |1 |73 |
72 ;; |2 |74 |
73 ;; |3 |75 |
74 ;; ... ...
75 ;; |71 |143 |
76 ;; |72 |144 |
77 ;; +----------+----------+
79 ;; As you can see, the right-hand window starts at line 73, the line
80 ;; immediately below the end of the left-hand window. As long as
81 ;; `follow-mode' is active, the two windows will follow eachother!
83 ;; * Play around and enjoy! Scroll one window and watch the other.
84 ;; Jump to the beginning or end. Press `Cursor down' at the last
85 ;; line of the left-hand window. Enter new lines into the
86 ;; text. Enter long lines spanning several lines, or several
87 ;; windows.
89 ;; * Should you find `Follow' mode annoying, just type
90 ;; M-x follow-mode <RETURN>
91 ;; to turn it off.
94 ;; The command `follow-delete-other-windows-and-split' maximises the
95 ;; visible area of the current buffer.
97 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
98 ;; key map. To do so, add the following lines (replacing `[f7]' and
99 ;; `[f8]' with your favorite keys) to the init file:
101 ;; (global-set-key [f8] 'follow-mode)
102 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
105 ;; There exists two system variables that controls the appearence of
106 ;; lines that are wider than the window containing them. The default
107 ;; is to truncate long lines whenever a window isn't as wide as the
108 ;; frame.
110 ;; To make sure lines are never truncated, please place the following
111 ;; lines in your init file:
113 ;; (setq truncate-lines nil)
114 ;; (setq truncate-partial-width-windows nil)
117 ;; Since the display of XEmacs is pixel-oriented, a line could be
118 ;; clipped in half at the bottom of the window.
120 ;; To make XEmacs avoid clipping (normal) lines, please place the
121 ;; following line in your init-file:
123 ;; (setq pixel-vertical-clip-threshold 30)
126 ;; The correct way to cofigurate Follow mode, or any other mode for
127 ;; that matter, is to create one (or more) function that does
128 ;; whatever you would like to do. The function is then added to
129 ;; a hook.
131 ;; When `Follow' mode is activated, functions stored in the hook
132 ;; `follow-mode-hook' are called. When it is deactivated
133 ;; `follow-mode-off-hook' is run.
135 ;; The keymap `follow-key-map' contains key bindings activated by
136 ;; `follow-mode'.
138 ;; Example:
139 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
141 ;; (defun my-follow-mode-hook ()
142 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
143 ;; (define-key follow-mode-map "\C-cb" 'another-function))
146 ;; Usage:
148 ;; To activate issue the command "M-x follow-mode"
149 ;; and press return. To deactivate, do it again.
151 ;; The following is a list of commands useful when follow-mode is active.
153 ;; follow-scroll-up C-c . C-v
154 ;; Scroll text in a Follow Mode window chain up.
156 ;; follow-scroll-down C-c . v
157 ;; Like `follow-scroll-up', but in the other direction.
159 ;; follow-delete-other-windows-and-split C-c . 1
160 ;; Maximise the visible area of the current buffer,
161 ;; and enter Follow Mode. This is a very convenient
162 ;; way to start Follow Mode, hence it is recomended
163 ;; that this command is added to the global keymap.
165 ;; follow-recenter C-c . C-l
166 ;; Place the point in the center of the middle window,
167 ;; or a specified number of lines from either top or bottom.
169 ;; follow-switch-to-buffer C-c . b
170 ;; Switch buffer in all windows displaying the current buffer
171 ;; in this frame.
173 ;; follow-switch-to-buffer-all C-c . C-b
174 ;; Switch buffer in all windows in the active frame.
176 ;; follow-switch-to-current-buffer-all
177 ;; Show the current buffer in all windows on the current
178 ;; frame and turn on `follow-mode'.
180 ;; follow-first-window C-c . <
181 ;; Select the first window in the frame showing the same buffer.
183 ;; follow-last-window C-c . >
184 ;; Select the last window in the frame showing the same buffer.
186 ;; follow-next-window C-c . n
187 ;; Select the next window in the frame showing the same buffer.
189 ;; follow-previous-window C-c . p
190 ;; Select the previous window showing the same buffer.
193 ;; Well, it seems ok, but what if I really want to look at two different
194 ;; positions in the text? Here are two simple methods to use:
196 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
197 ;; in the same frame. (My apoligies to you who can't use frames.)
199 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
200 ;; you want to view two locations. Of course, `follow' mode can
201 ;; be reactivated by hitting the same key again.
203 ;; Example from my ~/.emacs:
204 ;; (global-set-key [f8] 'follow-mode)
207 ;; Implementation:
209 ;; In an ideal world, follow mode would have been implemented in the
210 ;; kernel of the display routines, making sure that the windows (using
211 ;; follow mode) ALWAYS are aligned. On planet earth, however, we must
212 ;; accept a solution where we ALMOST ALWAYS can make sure that the
213 ;; windows are aligned.
215 ;; Follow mode does this in three places:
216 ;; 1) After each user command.
217 ;; 2) After a process output has been perfomed.
218 ;; 3) When a scrollbar has been moved.
220 ;; This will cover most situations. (Let me know if there are other
221 ;; situations that should be covered.)
223 ;; Note that only the selected window is checked, for the reason of
224 ;; efficiency and code complexity. (I.e. it is possible to make a
225 ;; non-selected windows unaligned. It will, however, pop right back
226 ;; when it is selected.)
228 ;;}}}
230 ;;; Code:
232 ;;{{{ Preliminaries
234 ;; Make the compiler shut up!
235 ;; There are two strategies:
236 ;; 1) Shut warnings off completely.
237 ;; 2) Handle each warning separately.
239 ;; Since I would like to see real errors, I've selected the latter
240 ;; method.
242 ;; The problem with undefined variables and functions has been solved
243 ;; by using `set', `symbol-value' and `symbol-function' rather than
244 ;; `setq' and direct references to variables and functions.
246 ;; For example:
247 ;; (if (boundp 'foo) ... (symbol-value 'foo) )
248 ;; (set 'foo ...) <-- XEmacs doesn't fall for this one.
249 ;; (funcall (symbol-function 'set) 'bar ...)
251 ;; Note: When this file is interpreted, `eval-when-compile' is
252 ;; evaluted. Since it doesn't hurt to evaluate it, but it is a bit
253 ;; annoying, we test if the byte-compiler has been loaded. This can,
254 ;; of course, lead to some occasional unintended evaluation...
256 ;; Should someone come up with a better solution, please let me
257 ;; know.
259 (eval-when-compile
260 (if (or (featurep 'bytecomp)
261 (featurep 'byte-compile))
262 (cond ((string-match "XEmacs" emacs-version)
263 ;; Make XEmacs shut up! I'm using standard Emacs
264 ;; functions, they are NOT obsolete!
265 (if (eq (get 'force-mode-line-update 'byte-compile)
266 'byte-compile-obsolete)
267 (put 'force-mode-line-update 'byte-compile 'nil))
268 (if (eq (get 'frame-first-window 'byte-compile)
269 'byte-compile-obsolete)
270 (put 'frame-first-window 'byte-compile 'nil))))))
272 ;;}}}
273 ;;{{{ Variables
275 (defgroup follow nil
276 "Synchronize windows showing the same buffer."
277 :prefix "follow-"
278 :group 'windows
279 :group 'convenience)
281 (defvar follow-mode nil
282 "Variable indicating if Follow mode is active.")
284 (defcustom follow-mode-hook nil
285 "*Hooks to run when follow-mode is turned on."
286 :type 'hook
287 :group 'follow)
289 (defcustom follow-mode-off-hook nil
290 "*Hooks to run when follow-mode is turned off."
291 :type 'hook
292 :group 'follow)
294 (defvar follow-mode-map nil
295 "*Minor mode keymap for Follow mode.")
297 (defcustom follow-mode-line-text " Follow"
298 "*Text shown in the mode line when Follow mode is active.
299 Defaults to \" Follow\". Examples of other values
300 are \" Fw\", or simply \"\"."
301 :type 'string
302 :group 'follow)
304 (defcustom follow-auto nil
305 "*Non-nil activates Follow mode whenever a file is loaded."
306 :type 'boolean
307 :group 'follow)
309 (defcustom follow-mode-prefix "\C-c."
310 "*Prefix key to use for follow commands in Follow mode.
311 The value of this variable is checked as part of loading Follow mode.
312 After that, changing the prefix key requires manipulating keymaps."
313 :type 'string
314 :group 'follow)
316 (defcustom follow-intercept-processes
317 (fboundp 'start-process)
318 "*When non-nil, Follow Mode will monitor process output."
319 :type 'boolean
320 :group 'follow)
322 (defvar follow-emacs-version-xemacs-p
323 (string-match "XEmacs" emacs-version)
324 "Non-nil when running under XEmacs.")
326 (defvar follow-avoid-tail-recenter-p
327 (not follow-emacs-version-xemacs-p)
328 "*When non-nil, patch emacs so that tail windows won't be recentered.
330 A \"tail window\" is a window that displays only the end of
331 the buffer. Normally it is practical for the user that empty
332 windows are recentered automatically. However, when using
333 Follow Mode it breaks the display when the end is displayed
334 in a window \"above\" the last window. This is for
335 example the case when displaying a short page in info.
337 Must be set before Follow Mode is loaded.
339 Please note that it is not possible to fully prevent Emacs from
340 recentering empty windows. Please report if you find a repeatable
341 situation in which Emacs recenters empty windows.
343 XEmacs, as of 19.12, does not recenter windows, good!")
345 (defvar follow-cache-command-list
346 '(next-line previous-line forward-char backward-char)
347 "List of commands that don't require recalculation.
349 In order to be able to use the cache, a command should not change the
350 contents of the buffer, nor should it change selected window or current
351 buffer.
353 The commands in this list are checked at load time.
355 To mark other commands as suitable for caching, set the symbol
356 property `follow-mode-use-cache' to non-nil.")
358 (defvar follow-debug nil
359 "*Non-nil when debugging Follow mode.")
362 ;; Internal variables:
364 (defvar follow-internal-force-redisplay nil
365 "True when Follow mode should redisplay the windows.")
367 (defvar follow-process-filter-alist '()
368 "The original filters for processes intercepted by Follow mode.")
370 (defvar follow-active-menu nil
371 "The menu visible when Follow mode is active.")
373 (defvar follow-deactive-menu nil
374 "The menu visible when Follow mode is deactivated.")
376 (defvar follow-inside-post-command-hook nil
377 "Non-nil when inside Follow modes `post-command-hook'.
378 Used by `follow-window-size-change'.")
380 (defvar follow-windows-start-end-cache nil
381 "Cache used by `follow-window-start-end'.")
383 ;;}}}
384 ;;{{{ Bug report
386 (eval-when-compile (require 'reporter))
388 (defun follow-submit-feedback ()
389 "Submit feedback on Follow mode to the author: andersl@andersl.com"
390 (interactive)
391 (require 'reporter)
392 (and (y-or-n-p "Do you really want to submit a report on Follow mode? ")
393 (reporter-submit-bug-report
394 "Anders Lindgren <andersl@andersl.com>"
395 "follow.el"
396 '(post-command-hook
397 pre-command-hook
398 window-size-change-functions
399 window-scroll-functions
400 follow-mode-hook
401 follow-mode-off-hook
402 follow-auto
403 follow-intercept-processes
404 follow-avoid-tail-recenter-p
405 follow-process-filter-alist)
408 (concat
409 "Hi Anders!\n\n"
410 "(I have read the section on how to report bugs in the "
411 "Emacs manual.)\n\n"
412 "Even though I know you are busy, I thought you might "
413 "want to know...\n\n"))))
415 ;;}}}
416 ;;{{{ Debug messages
418 ;; This inline function must be as small as possible!
419 ;; Maybe we should define a macro that expands to nil if
420 ;; the variable is not set.
422 (defsubst follow-debug-message (&rest args)
423 "Like message, but only active when `follow-debug' is non-nil."
424 (if (and (boundp 'follow-debug) follow-debug)
425 (apply 'message args)))
427 ;;}}}
429 ;;{{{ Keymap/Menu
431 ;;; Define keys for the follow-mode minor mode map and replace some
432 ;;; functions in the global map. All `follow' mode special functions
433 ;;; can be found on (the somewhat cumbersome) "C-c . <key>"
434 ;;; (Control-C dot <key>). (As of Emacs 19.29 the keys
435 ;;; C-c <punctuation character> are reserved for minor modes.)
437 ;;; To change the prefix, redefine `follow-mode-prefix' before
438 ;;; `follow' is loaded, or see the section on `follow-mode-hook'
439 ;;; above for an example of how to bind the keys the way you like.
441 ;;; Please note that the keymap is defined the first time this file is
442 ;;; loaded. Also note that the only legal way to manipulate the
443 ;;; keymap is to use `define-key'. Don't change it using `setq' or
444 ;;; similar!
447 (if follow-mode-map
449 (setq follow-mode-map (make-sparse-keymap))
450 (let ((map (make-sparse-keymap)))
451 (define-key map "\C-v" 'follow-scroll-up)
452 (define-key map "\M-v" 'follow-scroll-down)
453 (define-key map "v" 'follow-scroll-down)
454 (define-key map "1" 'follow-delete-other-windows-and-split)
455 (define-key map "b" 'follow-switch-to-buffer)
456 (define-key map "\C-b" 'follow-switch-to-buffer-all)
457 (define-key map "\C-l" 'follow-recenter)
458 (define-key map "<" 'follow-first-window)
459 (define-key map ">" 'follow-last-window)
460 (define-key map "n" 'follow-next-window)
461 (define-key map "p" 'follow-previous-window)
463 (define-key follow-mode-map follow-mode-prefix map)
465 ;; Replace the standard `end-of-buffer', when in Follow Mode. (I
466 ;; don't see the point in trying to replace every function that
467 ;; could be enhanced in Follow mode. End-of-buffer is a special
468 ;; case since it is very simple to define and it greatly enhances
469 ;; the look and feel of Follow mode.)
471 ;; (The function `substitute-key-definition' does not work
472 ;; in all versions of Emacs.)
473 (mapcar
474 (function
475 (lambda (pair)
476 (let ((old (car pair))
477 (new (cdr pair)))
478 (mapcar (function (lambda (key)
479 (define-key follow-mode-map key new)))
480 (where-is-internal old global-map)))))
481 '((end-of-buffer . follow-end-of-buffer)
482 (fkey-end-of-buffer . follow-end-of-buffer)))
485 ;;; The menu.
488 (if (not follow-emacs-version-xemacs-p)
491 ;; Emacs
493 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
494 "Follow"))
495 (count 0)
497 (mapcar
498 (function
499 (lambda (item)
500 (setq id
501 (or (cdr item)
502 (progn
503 (setq count (+ count 1))
504 (intern (format "separator-%d" count)))))
505 (define-key menumap (vector id) item)
506 (or (eq id 'follow-mode)
507 (put id 'menu-enable 'follow-mode))))
508 ;; In reverse order:
509 '(("Toggle Follow mode" . follow-mode)
510 ("--")
511 ("Recenter" . follow-recenter)
512 ("--")
513 ("Previous Window" . follow-previous-window)
514 ("Next Windows" . follow-next-window)
515 ("Last Window" . follow-last-window)
516 ("First Window" . follow-first-window)
517 ("--")
518 ("Switch To Buffer (all windows)"
519 . follow-switch-to-buffer-all)
520 ("Switch To Buffer" . follow-switch-to-buffer)
521 ("--")
522 ("Delete Other Windows and Split"
523 . follow-delete-other-windows-and-split)
524 ("--")
525 ("Scroll Down" . follow-scroll-down)
526 ("Scroll Up" . follow-scroll-up)))
528 ;; If there is a `tools' meny, we use it. However, we can't add a
529 ;; minor-mode specific item to it (it's broken), so we make the
530 ;; contents ghosted when not in use, and add ourselves to the
531 ;; global map. If no `tools' menu is present, just make a
532 ;; top-level menu visible when the mode is activated.
534 (let ((tools-map (lookup-key (current-global-map) [menu-bar tools]))
535 (last nil))
536 (if (sequencep tools-map)
537 (progn
538 ;; Find the last entry in the menu and store it in `last'.
539 (mapcar (function
540 (lambda (x)
541 (setq last (or (cdr-safe
542 (cdr-safe
543 (cdr-safe x)))
544 last))))
545 tools-map)
546 (if last
547 (progn
548 (funcall (symbol-function 'define-key-after)
549 tools-map [separator-follow] '("--") last)
550 (funcall (symbol-function 'define-key-after)
551 tools-map [follow] (cons "Follow" menumap)
552 'separator-follow))
553 ;; Didn't find the last item, Adding to the top of
554 ;; tools. (This will probably never happend...)
555 (define-key (current-global-map) [menu-bar tools follow]
556 (cons "Follow" menumap))))
557 ;; No tools menu, add "Follow" to the menubar.
558 (define-key follow-mode-map [menu-bar follow]
559 (cons "Follow" menumap)))))
562 ;; XEmacs.
565 ;; place the menu in the `Tools' menu.
566 (let ((menu '("Follow"
567 :filter follow-menu-filter
568 ["Scroll Up" follow-scroll-up t]
569 ["Scroll Down" follow-scroll-down t]
570 ["Delete Other Windows and Split"
571 follow-delete-other-windows-and-split t]
572 ["Switch To Buffer" follow-switch-to-buffer t]
573 ["Switch To Buffer (all windows)"
574 follow-switch-to-buffer-all t]
575 ["First Window" follow-first-window t]
576 ["Last Window" follow-last-window t]
577 ["Next Windows" follow-next-window t]
578 ["Previous Window" follow-previous-window t]
579 ["Recenter" follow-recenter t]
580 ["Deactivate" follow-mode t])))
582 ;; Why not just `(set-buffer-menubar current-menubar)'? The
583 ;; question is a very good question. The reason is that under
584 ;; Emacs, neither `set-buffer-menubar' nor
585 ;; `current-menubar' is defined, hence the byte-compiler will
586 ;; warn.
587 (funcall (symbol-function 'set-buffer-menubar)
588 (symbol-value 'current-menubar))
589 (funcall (symbol-function 'add-submenu) '("Tools") menu))
591 ;; When the mode is not activated, only one item is visible:
592 ;; "Activate".
593 (defun follow-menu-filter (menu)
594 (if follow-mode
595 menu
596 '(["Activate " follow-mode t]))))))
599 ;;; Register the follow mode keymap.
600 (or (assq 'follow-mode minor-mode-map-alist)
601 (setq minor-mode-map-alist
602 (cons (cons 'follow-mode follow-mode-map) minor-mode-map-alist)))
604 ;;}}}
605 ;;{{{ Cache
607 (let ((cmds follow-cache-command-list))
608 (while cmds
609 (put (car cmds) 'follow-mode-use-cache t)
610 (setq cmds (cdr cmds))))
612 ;;}}}
614 ;;{{{ The mode
616 ;;;###autoload
617 (defun turn-on-follow-mode ()
618 "Turn on Follow mode. Please see the function `follow-mode'."
619 (interactive)
620 (follow-mode 1))
623 ;;;###autoload
624 (defun turn-off-follow-mode ()
625 "Turn off Follow mode. Please see the function `follow-mode'."
626 (interactive)
627 (follow-mode -1))
630 ;;;###autoload
631 (defun follow-mode (arg)
632 "Minor mode that combines windows into one tall virtual window.
634 The feeling of a \"virtual window\" has been accomplished by the use
635 of two major techniques:
637 * The windows always displays adjacent sections of the buffer.
638 This means that whenever one window is moved, all the
639 others will follow. (Hence the name Follow Mode.)
641 * Should the point (cursor) end up outside a window, another
642 window displaying that point is selected, if possible. This
643 makes it possible to walk between windows using normal cursor
644 movement commands.
646 Follow mode comes to its prime when used on a large screen and two
647 side-by-side window are used. The user can, with the help of Follow
648 mode, use two full-height windows as though they would have been
649 one. Imagine yourself editing a large function, or section of text,
650 and being able to use 144 lines instead of the normal 72... (your
651 mileage may vary).
653 To split one large window into two side-by-side windows, the commands
654 `\\[split-window-horizontally]' or \
655 `M-x follow-delete-other-windows-and-split' can be used.
657 Only windows displayed in the same frame follow each-other.
659 If the variable `follow-intercept-processes' is non-nil, Follow mode
660 will listen to the output of processes and redisplay accordingly.
661 \(This is the default.)
663 When Follow mode is switched on, the hook `follow-mode-hook'
664 is called. When turned off, `follow-mode-off-hook' is called.
666 Keys specific to Follow mode:
667 \\{follow-mode-map}"
668 (interactive "P")
669 (make-local-variable 'follow-mode)
670 (put 'follow-mode 'permanent-local t)
671 (let ((follow-mode-orig follow-mode))
672 (setq follow-mode
673 (if (null arg)
674 (not follow-mode)
675 (> (prefix-numeric-value arg) 0)))
676 (if (and follow-mode follow-intercept-processes)
677 (follow-intercept-process-output))
678 (cond ((and follow-mode (not follow-mode-orig)) ; On
679 ;; XEmacs: If this is non-nil, the window will scroll before
680 ;; the point will have a chance to get into the next window.
681 (if (boundp 'scroll-on-clipped-lines)
682 (set 'scroll-on-clipped-lines nil))
683 (force-mode-line-update)
684 (add-hook 'post-command-hook 'follow-post-command-hook t)
685 (run-hooks 'follow-mode-hook))
687 ((and (not follow-mode) follow-mode-orig) ; Off
688 (force-mode-line-update)
689 (run-hooks 'follow-mode-off-hook)))))
692 ;; Register follow-mode as a minor mode.
694 (if (fboundp 'add-minor-mode)
695 ;; XEmacs
696 (funcall (symbol-function 'add-minor-mode)
697 'follow-mode 'follow-mode-line-text)
698 (or (assq 'follow-mode minor-mode-alist)
699 (setq minor-mode-alist
700 (cons '(follow-mode follow-mode-line-text) minor-mode-alist))))
702 ;;}}}
703 ;;{{{ Find file hook
705 ;; This will start follow-mode whenever a new file is loaded, if
706 ;; the variable `follow-auto' is non-nil.
708 (add-hook 'find-file-hook 'follow-find-file-hook t)
710 (defun follow-find-file-hook ()
711 "Find-file hook for Follow Mode. See the variable `follow-auto'."
712 (if follow-auto (follow-mode t)))
714 ;;}}}
716 ;;{{{ User functions
719 ;;; User functions usable when in Follow mode.
722 ;;{{{ Scroll
724 ;; `scroll-up' and `-down', but for windows in Follow Mode.
726 ;; Almost like the real thing, excpet when the cursor ends up outside
727 ;; the top or bottom... In our case however, we end up outside the
728 ;; window and hence we are recenterd. Should we let `recenter' handle
729 ;; the point position we would never leave the selected window. To do
730 ;; it ourselves we would need to do our own redisplay, which is easier
731 ;; said than done. (Why didn't I do a real display abstraction from
732 ;; the beginning?)
734 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
735 ;; our post-command-hook will move our windows back into the old
736 ;; position... (This would also be corrected if we would have had a
737 ;; good redisplay abstraction.)
739 (defun follow-scroll-up (&optional arg)
740 "Scroll text in a Follow Mode window chain up.
742 If called with no ARG, the `next-screen-context-lines' last lines of
743 the bottom window in the chain will be visible in the top window.
745 If called with an argument, scroll ARG lines up.
746 Negative ARG means scroll downward.
748 Works like `scroll-up' when not in Follow Mode."
749 (interactive "P")
750 (cond ((not (and (boundp 'follow-mode) follow-mode))
751 (scroll-up arg))
752 (arg
753 (save-excursion (scroll-up arg))
754 (setq follow-internal-force-redisplay t))
756 (let* ((windows (follow-all-followers))
757 (end (window-end (car (reverse windows)))))
758 (if (eq end (point-max))
759 (signal 'end-of-buffer nil)
760 (select-window (car windows))
761 ;; `window-end' might return nil.
762 (if end
763 (goto-char end))
764 (vertical-motion (- next-screen-context-lines))
765 (set-window-start (car windows) (point)))))))
768 (defun follow-scroll-down (&optional arg)
769 "Scroll text in a Follow Mode window chain down.
771 If called with no ARG, the `next-screen-context-lines' top lines of
772 the top window in the chain will be visible in the bottom window.
774 If called with an argument, scroll ARG lines down.
775 Negative ARG means scroll upward.
777 Works like `scroll-up' when not in Follow Mode."
778 (interactive "P")
779 (cond ((not (and (boundp 'follow-mode) follow-mode))
780 (scroll-up arg))
781 (arg
782 (save-excursion (scroll-down arg)))
784 (let* ((windows (follow-all-followers))
785 (win (car (reverse windows)))
786 (start (window-start (car windows))))
787 (if (eq start (point-min))
788 (signal 'beginning-of-buffer nil)
789 (select-window win)
790 (goto-char start)
791 (vertical-motion (- (- (window-height win)
793 next-screen-context-lines)))
794 (set-window-start win (point))
795 (goto-char start)
796 (vertical-motion (- next-screen-context-lines 1))
797 (setq follow-internal-force-redisplay t))))))
799 ;;}}}
800 ;;{{{ Buffer
802 ;;;###autoload
803 (defun follow-delete-other-windows-and-split (&optional arg)
804 "Create two side by side windows and enter Follow Mode.
806 Execute this command to display as much as possible of the text
807 in the selected window. All other windows, in the current
808 frame, are deleted and the selected window is split in two
809 side-by-side windows. Follow Mode is activated, hence the
810 two windows always will display two successive pages.
811 \(If one window is moved, the other one will follow.)
813 If ARG is positive, the leftmost window is selected. If it negative,
814 the rightmost is selected. If ARG is nil, the leftmost window is
815 selected if the original window is the first one in the frame.
817 To bind this command to a hotkey, place the following line
818 in your `~/.emacs' file, replacing [f7] by your favourite key:
819 (global-set-key [f7] 'follow-delete-other-windows-and-split)"
820 (interactive "P")
821 (let ((other (or (and (null arg)
822 (not (eq (selected-window)
823 (frame-first-window (selected-frame)))))
824 (and arg
825 (< (prefix-numeric-value arg) 0))))
826 (start (window-start)))
827 (delete-other-windows)
828 (split-window-horizontally)
829 (if other
830 (progn
831 (other-window 1)
832 (set-window-start (selected-window) start)
833 (setq follow-internal-force-redisplay t)))
834 (follow-mode 1)))
836 (defun follow-switch-to-buffer (buffer)
837 "Show BUFFER in all windows in the current Follow Mode window chain."
838 (interactive "BSwitch to Buffer: ")
839 (let ((orig-window (selected-window))
840 (windows (follow-all-followers)))
841 (while windows
842 (select-window (car windows))
843 (switch-to-buffer buffer)
844 (setq windows (cdr windows)))
845 (select-window orig-window)))
848 (defun follow-switch-to-buffer-all (&optional buffer)
849 "Show BUFFER in all windows on this frame.
850 Defaults to current buffer."
851 (interactive (list (read-buffer "Switch to Buffer: "
852 (current-buffer))))
853 (or buffer (setq buffer (current-buffer)))
854 (let ((orig-window (selected-window)))
855 (walk-windows
856 (function
857 (lambda (win)
858 (select-window win)
859 (switch-to-buffer buffer))))
860 (select-window orig-window)
861 (follow-redisplay)))
864 (defun follow-switch-to-current-buffer-all ()
865 "Show current buffer in all windows on this frame, and enter Follow Mode.
867 To bind this command to a hotkey place the following line
868 in your `~/.emacs' file:
869 (global-set-key [f7] 'follow-switch-to-current-buffer-all)"
870 (interactive)
871 (or (and (boundp 'follow-mode) follow-mode)
872 (follow-mode 1))
873 (follow-switch-to-buffer-all))
875 ;;}}}
876 ;;{{{ Movement
878 ;; Note, these functions are not very useful, atleast not unless you
879 ;; rebind the rather cumbersome key sequence `C-c . p'.
881 (defun follow-next-window ()
882 "Select the next window showing the same buffer."
883 (interactive)
884 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
885 (if succ
886 (select-window (car succ))
887 (error "%s" "No more windows"))))
890 (defun follow-previous-window ()
891 "Select the previous window showing the same buffer."
892 (interactive)
893 (let ((pred (car (follow-split-followers (follow-all-followers)))))
894 (if pred
895 (select-window (car pred))
896 (error "%s" "No more windows"))))
899 (defun follow-first-window ()
900 "Select the first window in the frame showing the same buffer."
901 (interactive)
902 (select-window (car (follow-all-followers))))
905 (defun follow-last-window ()
906 "Select the last window in the frame showing the same buffer."
907 (interactive)
908 (select-window (car (reverse (follow-all-followers)))))
910 ;;}}}
911 ;;{{{ Redraw
913 (defun follow-recenter (&optional arg)
914 "Recenter the middle window around point.
915 Rearrange all other windows around the middle window.
917 With a positive argument, place the current line ARG lines
918 from the top. With a negative, place it -ARG lines from the
919 bottom."
920 (interactive "P")
921 (if arg
922 (let ((p (point))
923 (arg (prefix-numeric-value arg)))
924 (if (>= arg 0)
925 ;; Recenter relative to the top.
926 (progn
927 (follow-first-window)
928 (goto-char p)
929 (recenter arg))
930 ;; Recenter relative to the bottom.
931 (follow-last-window)
932 (goto-char p)
933 (recenter arg)
934 ;; Otherwise, our post-command-hook will move the window
935 ;; right back.
936 (setq follow-internal-force-redisplay t)))
937 ;; Recenter in the middle.
938 (let* ((dest (point))
939 (windows (follow-all-followers))
940 (win (nth (/ (- (length windows) 1) 2) windows)))
941 (select-window win)
942 (goto-char dest)
943 (recenter)
944 ;;(setq follow-internal-force-redisplay t)
948 (defun follow-redraw ()
949 "Arrange windows displaying the same buffer in successor order.
950 This function can be called even if the buffer is not in Follow mode.
952 Hopefully, there should be no reason to call this function when in
953 Follow mode since the windows should always be aligned."
954 (interactive)
955 (sit-for 0)
956 (follow-redisplay))
958 ;;}}}
959 ;;{{{ End of buffer
961 (defun follow-end-of-buffer (&optional arg)
962 "Move point to the end of the buffer, Follow Mode style.
964 If the end is not visible, it will be displayed in the last possible
965 window in the Follow Mode window chain.
967 The mark is left at the previous position. With arg N, put point N/10
968 of the way from the true end."
969 (interactive "P")
970 (let ((followers (follow-all-followers))
971 (pos (point)))
972 (cond (arg
973 (select-window (car (reverse followers))))
974 ((follow-select-if-end-visible
975 (follow-windows-start-end followers)))
977 (select-window (car (reverse followers)))))
978 (goto-char pos)
979 (with-no-warnings
980 (end-of-buffer arg))))
982 ;;}}}
984 ;;}}}
986 ;;{{{ Display
988 ;;;; The display routines
990 ;;{{{ Information gathering functions
992 (defun follow-all-followers (&optional testwin)
993 "Return all windows displaying the same buffer as the TESTWIN.
994 The list contains only windows displayed in the same frame as TESTWIN.
995 If TESTWIN is nil the selected window is used."
996 (or (and testwin (window-live-p testwin))
997 (setq testwin (selected-window)))
998 (let* ((top (frame-first-window (window-frame testwin)))
999 (win top)
1000 (done nil)
1001 (windows '())
1002 (buffer (window-buffer testwin)))
1003 (while (and (not done) win)
1004 (if (eq (window-buffer win) buffer)
1005 (setq windows (cons win windows)))
1006 (setq win (next-window win 'not))
1007 (if (eq win top)
1008 (setq done t)))
1009 (nreverse windows)))
1012 (defun follow-split-followers (windows &optional win)
1013 "Split the WINDOWS into the sets: predecessors and successors.
1014 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
1015 from the selected window."
1016 (or win
1017 (setq win (selected-window)))
1018 (let ((pred '()))
1019 (while (not (eq (car windows) win))
1020 (setq pred (cons (car windows) pred))
1021 (setq windows (cdr windows)))
1022 (cons pred (cdr windows))))
1025 ;; This function is optimized function for speed!
1027 (defun follow-calc-win-end (&optional win)
1028 "Calculate the presumed window end for WIN.
1030 Actually, the position returned is the start of the next
1031 window, normally is the end plus one.
1033 If WIN is nil, the selected window is used.
1035 Returns (end-pos end-of-buffer-p)"
1036 (if follow-emacs-version-xemacs-p
1037 ;; XEmacs can calculate the end of the window by using
1038 ;; the 'guarantee options. GOOD!
1039 (let ((end (window-end win t)))
1040 (if (= end (funcall (symbol-function 'point-max)
1041 (window-buffer win)))
1042 (list end t)
1043 (list (+ end 1) nil)))
1044 ;; Emacs: We have to calculate the end by ourselves.
1045 ;; This code works on both XEmacs and Emacs, but now
1046 ;; that XEmacs has got custom-written code, this could
1047 ;; be optimized for Emacs.
1048 (let ((orig-win (and win (selected-window)))
1049 height
1050 buffer-end-p)
1051 (if win (select-window win))
1052 (prog1
1053 (save-excursion
1054 (goto-char (window-start))
1055 (setq height (- (window-height) 1))
1056 (setq buffer-end-p
1057 (if (bolp)
1058 (not (= height (vertical-motion height)))
1059 (save-restriction
1060 ;; Fix a mis-feature in `vertical-motion':
1061 ;; The start of the window is assumed to
1062 ;; coinside with the start of a line.
1063 (narrow-to-region (point) (point-max))
1064 (not (= height (vertical-motion height))))))
1065 (list (point) buffer-end-p))
1066 (if orig-win
1067 (select-window orig-win))))))
1070 ;; Can't use `save-window-excursion' since it triggers a redraw.
1071 (defun follow-calc-win-start (windows pos win)
1072 "Calculate where WIN will start if the first in WINDOWS start at POS.
1074 If WIN is nil the point below all windows is returned."
1075 (let (start)
1076 (while (and windows (not (eq (car windows) win)))
1077 (setq start (window-start (car windows)))
1078 (set-window-start (car windows) pos 'noforce)
1079 (setq pos (car (inline (follow-calc-win-end (car windows)))))
1080 (set-window-start (car windows) start 'noforce)
1081 (setq windows (cdr windows)))
1082 pos))
1085 ;; The result from `follow-windows-start-end' is cached when using
1086 ;; a handful simple commands, like cursor movement commands.
1088 (defsubst follow-cache-valid-p (windows)
1089 "Test if the cached value of `follow-windows-start-end' can be used.
1090 Note that this handles the case when the cache has been set to nil."
1091 (let ((res t)
1092 (cache follow-windows-start-end-cache))
1093 (while (and res windows cache)
1094 (setq res (and (eq (car windows)
1095 (car (car cache)))
1096 (eq (window-start (car windows))
1097 (car (cdr (car cache))))))
1098 (setq windows (cdr windows))
1099 (setq cache (cdr cache)))
1100 (and res (null windows) (null cache))))
1103 (defsubst follow-invalidate-cache ()
1104 "Force `follow-windows-start-end' to recalculate the end of the window."
1105 (setq follow-windows-start-end-cache nil))
1108 ;; Build a list of windows and their start and end positions.
1109 ;; Useful to avoid calculating start/end position whenever they are needed.
1110 ;; The list has the format:
1111 ;; ((Win Start End End-of-buffer-visible-p) ...)
1113 ;; Used to have a `save-window-excursion', but it obviously triggered
1114 ;; redraws of the display. Check if I used it for anything.
1117 (defun follow-windows-start-end (windows)
1118 "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
1119 (if (follow-cache-valid-p windows)
1120 follow-windows-start-end-cache
1121 (let ((win-start-end '())
1122 (orig-win (selected-window)))
1123 (while windows
1124 (select-window (car windows))
1125 (setq win-start-end
1126 (cons (cons (car windows)
1127 (cons (window-start)
1128 (follow-calc-win-end)))
1129 win-start-end))
1130 (setq windows (cdr windows)))
1131 (select-window orig-win)
1132 (setq follow-windows-start-end-cache (nreverse win-start-end))
1133 follow-windows-start-end-cache)))
1136 (defsubst follow-pos-visible (pos win win-start-end)
1137 "Non-nil when POS is visible in WIN."
1138 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
1139 (and (>= pos (car wstart-wend-bend))
1140 (or (< pos (car (cdr wstart-wend-bend)))
1141 (nth 2 wstart-wend-bend)))))
1144 ;; By `aligned' we mean that for all adjecent windows, the end of the
1145 ;; first is equal with the start of the successor. The first window
1146 ;; should start at a full screen line.
1148 (defsubst follow-windows-aligned-p (win-start-end)
1149 "Non-nil if the follower WINDOWS are aligned."
1150 (let ((res t))
1151 (save-excursion
1152 (goto-char (window-start (car (car win-start-end))))
1153 (if (bolp)
1155 (vertical-motion 0 (car (car win-start-end)))
1156 (setq res (eq (point) (window-start (car (car win-start-end)))))))
1157 (while (and res (cdr win-start-end))
1158 ;; At least two followers left
1159 (setq res (eq (car (cdr (cdr (car win-start-end))))
1160 (car (cdr (car (cdr win-start-end))))))
1161 (setq win-start-end (cdr win-start-end)))
1162 res))
1165 ;; Check if the point is visible in all windows. (So that
1166 ;; no one will be recentered.)
1168 (defun follow-point-visible-all-windows-p (win-start-end)
1169 "Non-nil when the window-point is visible in all windows."
1170 (let ((res t))
1171 (while (and res win-start-end)
1172 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
1173 (car (car win-start-end))
1174 win-start-end))
1175 (setq win-start-end (cdr win-start-end)))
1176 res))
1179 ;; Make sure WIN always starts at the beginning of an whole screen
1180 ;; line. If WIN is not aligned the start is updated which probably
1181 ;; will lead to a redisplay of the screen later on.
1183 ;; This is used with the first window in a follow chain. The reason
1184 ;; is that we want to detect that the point is outside the window.
1185 ;; (Without the update, the start of the window will move as the
1186 ;; user presses BackSpace, and the other window redisplay routines
1187 ;; will move the start of the window in the wrong direction.)
1189 (defun follow-update-window-start (win)
1190 "Make sure that the start of WIN starts at a full screen line."
1191 (save-excursion
1192 (goto-char (window-start win))
1193 (if (bolp)
1195 (vertical-motion 0 win)
1196 (if (eq (point) (window-start win))
1198 (vertical-motion 1 win)
1199 (set-window-start win (point) 'noforce)))))
1201 ;;}}}
1202 ;;{{{ Selection functions
1204 ;; Make a window in WINDOWS selected if it currently
1205 ;; is displaying the position DEST.
1207 ;; We don't select a window if it just has been moved.
1209 (defun follow-select-if-visible (dest win-start-end)
1210 "Select and return a window, if DEST is visible in it.
1211 Return the selected window."
1212 (let ((win nil))
1213 (while (and (not win) win-start-end)
1214 ;; Don't select a window that was just moved. This makes it
1215 ;; possible to later select the last window after a `end-of-buffer'
1216 ;; command.
1217 (if (follow-pos-visible dest (car (car win-start-end)) win-start-end)
1218 (progn
1219 (setq win (car (car win-start-end)))
1220 (select-window win)))
1221 (setq win-start-end (cdr win-start-end)))
1222 win))
1225 ;; Lets select a window showing the end. Make sure we only select it if it
1226 ;; it wasn't just moved here. (i.e. M-> shall not unconditionally place
1227 ;; the point in the selected window.)
1229 ;; (Compability cludge: in Emacs `window-end' is equal to `point-max';
1230 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1231 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1233 (defun follow-select-if-end-visible (win-start-end)
1234 "Select and return a window, if end is visible in it."
1235 (let ((win nil))
1236 (while (and (not win) win-start-end)
1237 ;; Don't select a window that was just moved. This makes it
1238 ;; possible to later select the last window after a `end-of-buffer'
1239 ;; command.
1240 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1241 (nth 3 (car win-start-end))
1242 ;; `window-end' might return nil.
1243 (let ((end (window-end (car (car win-start-end)))))
1244 (and end
1245 (eq (point-max) (min (point-max) end)))))
1246 (progn
1247 (setq win (car (car win-start-end)))
1248 (select-window win)))
1249 (setq win-start-end (cdr win-start-end)))
1250 win))
1253 ;; Select a window that will display the point if the windows would
1254 ;; be redisplayed with the first window fixed. This is useful for
1255 ;; example when the user has pressed return at the bottom of a window
1256 ;; as the point is not visible in any window.
1258 (defun follow-select-if-visible-from-first (dest windows)
1259 "Select and return a window with DEST, if WINDOWS are redrawn from top."
1260 (let ((win nil)
1261 end-pos-end-p)
1262 (save-excursion
1263 (goto-char (window-start (car windows)))
1264 ;; Make sure the line start in the beginning of a real screen
1265 ;; line.
1266 (vertical-motion 0 (car windows))
1267 (if (< dest (point))
1268 ;; Above the start, not visible.
1270 ;; At or below the start. Check the windows.
1271 (save-window-excursion
1272 (while (and (not win) windows)
1273 (set-window-start (car windows) (point) 'noforce)
1274 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1275 (goto-char (car end-pos-end-p))
1276 ;; Visible, if dest above end, or if eob is visible inside
1277 ;; the window.
1278 (if (or (car (cdr end-pos-end-p))
1279 (< dest (point)))
1280 (setq win (car windows))
1281 (setq windows (cdr windows)))))))
1282 (if win
1283 (select-window win))
1284 win))
1287 ;;}}}
1288 ;;{{{ Redisplay
1290 ;; Redraw all the windows on the screen, starting with the top window.
1291 ;; The window used as as marker is WIN, or the selcted window if WIN
1292 ;; is nil.
1294 (defun follow-redisplay (&optional windows win)
1295 "Reposition the WINDOWS around WIN.
1296 Should the point be too close to the roof we redisplay everything
1297 from the top. WINDOWS should contain a list of windows to
1298 redisplay, it is assumed that WIN is a member of the list.
1299 Should WINDOWS be nil, the windows displaying the
1300 same buffer as WIN, in the current frame, are used.
1301 Should WIN be nil, the selected window is used."
1302 (or win
1303 (setq win (selected-window)))
1304 (or windows
1305 (setq windows (follow-all-followers win)))
1306 (follow-downward windows (follow-calculate-first-window-start windows win)))
1309 ;; Redisplay a chain of windows. Start every window directly after the
1310 ;; end of the previous window, to make sure long lines are displayed
1311 ;; correctly.
1313 (defun follow-downward (windows pos)
1314 "Redisplay all WINDOWS starting at POS."
1315 (while windows
1316 (set-window-start (car windows) pos)
1317 (setq pos (car (follow-calc-win-end (car windows))))
1318 (setq windows (cdr windows))))
1321 ;;(defun follow-downward (windows pos)
1322 ;; "Redisplay all WINDOWS starting at POS."
1323 ;; (let (p)
1324 ;; (while windows
1325 ;; (setq p (window-point (car windows)))
1326 ;; (set-window-start (car windows) pos)
1327 ;; (set-window-point (car windows) (max p pos))
1328 ;; (setq pos (car (follow-calc-win-end (car windows))))
1329 ;; (setq windows (cdr windows)))))
1332 ;; Return the start of the first window.
1334 ;; First, estimate the position. It the value is not perfect (i.e. we
1335 ;; have somewhere splited a line between windows) we try to enhance
1336 ;; the value.
1338 ;; The guess is always perfect if no long lines is split between
1339 ;; windows.
1341 ;; The worst case peformace of probably very bad, but it is very
1342 ;; unlikely that we ever will miss the correct start by more than one
1343 ;; or two lines.
1345 (defun follow-calculate-first-window-start (windows &optional win start)
1346 "Calculate the start of the first window.
1348 WINDOWS is a chain of windows to work with. WIN is the window
1349 to recenter around. It is assumed that WIN starts at position
1350 START."
1351 (or win
1352 (setq win (selected-window)))
1353 (or start
1354 (setq start (window-start win)))
1355 (let ((guess (follow-estimate-first-window-start windows win start)))
1356 (if (car guess)
1357 (cdr guess)
1358 ;; The guess wasn't exact, try to enhance it.
1359 (let ((win-start (follow-calc-win-start windows (cdr guess) win)))
1360 (cond ((= win-start start)
1361 (follow-debug-message "exact")
1362 (cdr guess))
1363 ((< win-start start)
1364 (follow-debug-message "above")
1365 (follow-calculate-first-window-start-from-above
1366 windows (cdr guess) win start))
1368 (follow-debug-message "below")
1369 (follow-calculate-first-window-start-from-below
1370 windows (cdr guess) win start)))))))
1373 ;; `exact' is disabled due to XEmacs and fonts of variable
1374 ;; height.
1375 (defun follow-estimate-first-window-start (windows win start)
1376 "Estimate the position of the first window.
1378 Returns (EXACT . POS). If EXACT is non-nil, POS is the starting
1379 position of the first window. Otherwise it is a good guess."
1380 (let ((pred (car (follow-split-followers windows win)))
1381 (exact nil))
1382 (save-excursion
1383 (goto-char start)
1384 ;(setq exact (bolp))
1385 (vertical-motion 0 win)
1386 (while pred
1387 (vertical-motion (- 1 (window-height (car pred))) (car pred))
1388 (if (not (bolp))
1389 (setq exact nil))
1390 (setq pred (cdr pred)))
1391 (cons exact (point)))))
1394 ;; Find the starting point, start at GUESS and search downward.
1395 ;; The returned point is always a point below GUESS.
1397 (defun follow-calculate-first-window-start-from-above
1398 (windows guess win start)
1399 (save-excursion
1400 (let ((done nil)
1401 win-start
1402 res)
1403 (goto-char guess)
1404 (while (not done)
1405 (if (not (= (vertical-motion 1 (car windows)) 1))
1406 ;; Hit bottom! (Can we really do this?)
1407 ;; We'll keep it, since it ensures termination.
1408 (progn
1409 (setq done t)
1410 (setq res (point-max)))
1411 (setq win-start (follow-calc-win-start windows (point) win))
1412 (if (>= win-start start)
1413 (progn
1414 (setq done t)
1415 (setq res (point))))))
1416 res)))
1419 ;; Find the starting point, start at GUESS and search upward. Return
1420 ;; a point on the same line as GUESS, or above.
1422 ;; (Is this ever used? I must make sure it works just in case it is
1423 ;; ever called.)
1425 (defun follow-calculate-first-window-start-from-below
1426 (windows guess &optional win start)
1427 (setq win (or win (selected-window)))
1428 (setq start (or start (window-start win)))
1429 (save-excursion
1430 (let ((done nil)
1431 win-start
1432 res)
1433 ;; Always calculate what happend when no line is displayed in the first
1434 ;; window. (The `previous' res is needed below!)
1435 (goto-char guess)
1436 (vertical-motion 0 (car windows))
1437 (setq res (point))
1438 (while (not done)
1439 (if (not (= (vertical-motion -1 (car windows)) -1))
1440 ;; Hit roof!
1441 (progn
1442 (setq done t)
1443 (setq res (point-min)))
1444 (setq win-start (follow-calc-win-start windows (point) win))
1445 (cond ((= win-start start) ; Perfect match, use this value
1446 (setq done t)
1447 (setq res (point)))
1448 ((< win-start start) ; Walked to far, use preious result
1449 (setq done t))
1450 (t ; Store result for next iteration
1451 (setq res (point))))))
1452 res)))
1454 ;;}}}
1455 ;;{{{ Avoid tail recenter
1457 ;; This sets the window internal flag `force_start'. The effect is that
1458 ;; windows only displaying the tail isn't recentered.
1459 ;; Has to be called before every redisplay... (Great isn't it?)
1461 ;; XEmacs doesn't recenter the tail, GOOD!
1463 ;; A window displaying only the tail, is a windows whose
1464 ;; window-start position is equal to (point-max) of the buffer it
1465 ;; displays.
1467 ;; This function is also added to `post-command-idle-hook', introduced
1468 ;; in Emacs 19.30. This is needed since the vaccine injected by the
1469 ;; call from `post-command-hook' only works until the next redisplay.
1470 ;; It is possible that the functions in the `post-command-idle-hook'
1471 ;; can cause a redisplay, and hence a new vaccine is needed.
1473 ;; Sometimes, calling this function could actually cause a redisplay,
1474 ;; especially if it is placed in the debug filter section. I must
1475 ;; investigate this further...
1477 (defun follow-avoid-tail-recenter (&rest rest)
1478 "Make sure windows displaying the end of a buffer aren't recentered.
1480 This is done by reading and rewriting the start position of
1481 non-first windows in Follow Mode."
1482 (if follow-avoid-tail-recenter-p
1483 (let* ((orig-buffer (current-buffer))
1484 (top (frame-first-window (selected-frame)))
1485 (win top)
1486 (who '()) ; list of (buffer . frame)
1487 start
1488 pair) ; (buffer . frame)
1489 ;; If the only window in the frame is a minibuffer
1490 ;; window, `next-window' will never find it again...
1491 (if (window-minibuffer-p top)
1493 (while ;; look, no body!
1494 (progn
1495 (setq start (window-start win))
1496 (set-buffer (window-buffer win))
1497 (setq pair (cons (window-buffer win) (window-frame win)))
1498 (if (member pair who)
1499 (if (and (boundp 'follow-mode) follow-mode
1500 (eq (point-max) start))
1501 ;; Write the same window start back, but don't
1502 ;; set the NOFORCE flag.
1503 (set-window-start win start))
1504 (setq who (cons pair who)))
1505 (setq win (next-window win 'not t))
1506 (not (eq win top)))) ;; Loop while this is true.
1507 (set-buffer orig-buffer)))))
1509 ;;}}}
1511 ;;}}}
1512 ;;{{{ Post Command Hook
1514 ;;; The magic little box. This function is called after every command.
1516 ;; This is not as complicated as it seems. It is simply a list of common
1517 ;; display situations and the actions to take, plus commands for redrawing
1518 ;; the screen if it should be unaligned.
1520 ;; We divide the check into two parts; whether we are at the end or not.
1521 ;; This is due to the fact that the end can actaually be visible
1522 ;; in several window even though they are aligned.
1524 (defun follow-post-command-hook ()
1525 "Ensure that the windows in Follow mode are adjacent after each command."
1526 (setq follow-inside-post-command-hook t)
1527 (if (or (not (input-pending-p))
1528 ;; Sometimes, in XEmacs, mouse events are not handled
1529 ;; properly by `input-pending-p'. A typical example is
1530 ;; when clicking on a node in `info'.
1531 (and (boundp 'current-mouse-event)
1532 (symbol-value 'current-mouse-event)
1533 (fboundp 'button-event-p)
1534 (funcall (symbol-function 'button-event-p)
1535 (symbol-value 'current-mouse-event))))
1536 ;; Work in the selected window, not in the current buffer.
1537 (let ((orig-buffer (current-buffer))
1538 (win (selected-window)))
1539 (set-buffer (window-buffer win))
1540 (or (and (symbolp this-command)
1541 (get this-command 'follow-mode-use-cache))
1542 (follow-invalidate-cache))
1543 (if (and (boundp 'follow-mode) follow-mode
1544 (not (window-minibuffer-p win)))
1545 ;; The buffer shown in the selected window is in follow
1546 ;; mode, lets find the current state of the display and
1547 ;; cache the result for speed (i.e. `aligned' and `visible'.)
1548 (let* ((windows (inline (follow-all-followers win)))
1549 (dest (point))
1550 (win-start-end (inline
1551 (follow-update-window-start (car windows))
1552 (follow-windows-start-end windows)))
1553 (aligned (follow-windows-aligned-p win-start-end))
1554 (visible (follow-pos-visible dest win win-start-end)))
1555 (if (not (and aligned visible))
1556 (follow-invalidate-cache))
1557 (inline (follow-avoid-tail-recenter))
1558 ;; Select a window to display the point.
1559 (or follow-internal-force-redisplay
1560 (progn
1561 (if (eq dest (point-max))
1562 ;; We're at the end, we have to be careful since
1563 ;; the display can be aligned while `dest' can
1564 ;; be visible in several windows.
1565 (cond
1566 ;; Select the current window, but only when
1567 ;; the display is correct. (When inserting
1568 ;; character in a tail window, the display is
1569 ;; not correct, as they are shown twice.)
1571 ;; Never stick to the current window after a
1572 ;; deletion. The reason is cosmetic, when
1573 ;; typing `DEL' in a window showing only the
1574 ;; end of the file, character are removed
1575 ;; from the window above, which is very
1576 ;; unintuitive.
1577 ((and visible
1578 aligned
1579 (not (memq this-command
1580 '(backward-delete-char
1581 delete-backward-char
1582 backward-delete-char-untabify
1583 kill-region))))
1584 (follow-debug-message "Max: same"))
1585 ;; If the end is visible, and the window
1586 ;; doesn't seems like it just has been moved,
1587 ;; select it.
1588 ((follow-select-if-end-visible win-start-end)
1589 (follow-debug-message "Max: end visible")
1590 (setq visible t)
1591 (setq aligned nil)
1592 (goto-char dest))
1593 ;; Just show the end...
1595 (follow-debug-message "Max: default")
1596 (select-window (car (reverse windows)))
1597 (goto-char dest)
1598 (setq visible nil)
1599 (setq aligned nil)))
1601 ;; We're not at the end, here life is much simpler.
1602 (cond
1603 ;; This is the normal case!
1604 ;; It should be optimized for speed.
1605 ((and visible aligned)
1606 (follow-debug-message "same"))
1607 ;; Pick a position in any window. If the
1608 ;; display is ok, this will pick the `correct'
1609 ;; window. If the display is wierd do this
1610 ;; anyway, this will be the case after a delete
1611 ;; at the beginning of the window.
1612 ((follow-select-if-visible dest win-start-end)
1613 (follow-debug-message "visible")
1614 (setq visible t)
1615 (goto-char dest))
1616 ;; Not visible anywhere else, lets pick this one.
1617 ;; (Is this case used?)
1618 (visible
1619 (follow-debug-message "visible in selected."))
1620 ;; Far out!
1621 ((eq dest (point-min))
1622 (follow-debug-message "min")
1623 (select-window (car windows))
1624 (goto-char dest)
1625 (set-window-start (selected-window) (point-min))
1626 (setq win-start-end (follow-windows-start-end windows))
1627 (follow-invalidate-cache)
1628 (setq visible t)
1629 (setq aligned nil))
1630 ;; If we can position the cursor without moving the first
1631 ;; window, do it. This is the case that catches `RET'
1632 ;; at the bottom of a window.
1633 ((follow-select-if-visible-from-first dest windows)
1634 (follow-debug-message "Below first")
1635 (setq visible t)
1636 (setq aligned t)
1637 (follow-redisplay windows (car windows))
1638 (goto-char dest))
1639 ;; None of the above. For simplicity, we stick to the
1640 ;; selected window.
1642 (follow-debug-message "None")
1643 (setq visible nil)
1644 (setq aligned nil))))
1645 ;; If a new window has been selected, make sure that the
1646 ;; old is not scrolled when the point is outside the
1647 ;; window.
1648 (or (eq win (selected-window))
1649 (let ((p (window-point win)))
1650 (set-window-start win (window-start win) nil)
1651 (set-window-point win p)))))
1652 ;; Make sure the point is visible in the selected window.
1653 ;; (This could lead to a scroll.)
1654 (if (or visible
1655 (follow-pos-visible dest win win-start-end))
1657 (sit-for 0)
1658 (follow-avoid-tail-recenter)
1659 (setq win-start-end (follow-windows-start-end windows))
1660 (follow-invalidate-cache)
1661 (setq aligned nil))
1662 ;; Redraw the windows whenever needed.
1663 (if (or follow-internal-force-redisplay
1664 (not (or aligned
1665 (follow-windows-aligned-p win-start-end)))
1666 (not (inline (follow-point-visible-all-windows-p
1667 win-start-end))))
1668 (progn
1669 (setq follow-internal-force-redisplay nil)
1670 (follow-redisplay windows (selected-window))
1671 (setq win-start-end (follow-windows-start-end windows))
1672 (follow-invalidate-cache)
1673 ;; When the point ends up in another window. This
1674 ;; happends when dest is in the beginning of the
1675 ;; file and the selected window is not the first.
1676 ;; It can also, in rare situations happend when
1677 ;; long lines are used and there is a big
1678 ;; difference between the width of the windows.
1679 ;; (When scrolling one line in a wide window which
1680 ;; will cause a move larger that an entire small
1681 ;; window.)
1682 (if (follow-pos-visible dest win win-start-end)
1684 (follow-select-if-visible dest win-start-end)
1685 (goto-char dest))))
1687 ;; If the region is visible, make it look good when spanning
1688 ;; multiple windows.
1689 (if (or (and (boundp 'mark-active) (symbol-value 'mark-active))
1690 (and (fboundp 'region-active-p)
1691 (funcall (symbol-function 'region-active-p))))
1692 (follow-maximize-region
1693 (selected-window) windows win-start-end))
1695 (inline (follow-avoid-tail-recenter))
1696 ;; DEBUG
1697 ;;(if (not (follow-windows-aligned-p
1698 ;; (follow-windows-start-end windows)))
1699 ;; (message "follow-mode: windows still unaligend!"))
1700 ;; END OF DEBUG
1701 ) ; Matches (let*
1702 ;; Buffer not in follow mode:
1703 ;; We still must update the windows displaying the tail so that
1704 ;; Emacs won't recenter them.
1705 (follow-avoid-tail-recenter))
1706 (set-buffer orig-buffer)))
1707 (setq follow-inside-post-command-hook nil))
1709 ;;}}}
1710 ;;{{{ The region
1712 ;; Tries to make the highlighted area representing the region look
1713 ;; good when spanning several windows.
1715 ;; Not perfect, as the point can't be placed at window end, only at
1716 ;; end-1. This will highlight a little bit in windows above
1717 ;; the current.
1719 (defun follow-maximize-region (win windows win-start-end)
1720 "Make a highlighted region stretching multiple windows look good."
1721 (let* ((all (follow-split-followers windows win))
1722 (pred (car all))
1723 (succ (cdr all))
1724 data)
1725 (while pred
1726 (setq data (assq (car pred) win-start-end))
1727 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1728 (setq pred (cdr pred)))
1729 (while succ
1730 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1731 (setq succ (cdr succ)))))
1733 ;;}}}
1734 ;;{{{ Scroll bar
1736 ;;;; Scroll-bar support code.
1738 ;;; Why is it needed? Well, if the selected window is in follow mode,
1739 ;;; all its follower stick to it blindly. If one of them is scrolled,
1740 ;;; it immediately returns to the original position when the mouse is
1741 ;;; released. If the selected window is not a follower of the dragged
1742 ;;; window the windows will be unaligned.
1744 ;;; The advices doesn't get compiled. Aestetically, this might be a
1745 ;;; problem but in practical life it isn't.
1747 ;;; Discussion: Now when the other windows in the chain follow the
1748 ;;; dragged, should we really select it?
1750 (cond ((fboundp 'scroll-bar-drag)
1752 ;;; Emacs style scrollbars.
1755 ;; Select the dragged window if it is a follower of the
1756 ;; selected window.
1758 ;; Generate advices of the form:
1759 ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
1760 ;; "Adviced by `follow-mode'."
1761 ;; (follow-redraw-after-event (ad-get-arg 0)))
1762 (let ((cmds '(scroll-bar-drag
1763 scroll-bar-drag-1 ; Executed at every move.
1764 scroll-bar-scroll-down
1765 scroll-bar-scroll-up
1766 scroll-bar-set-window-start)))
1767 (while cmds
1768 (eval
1769 `(defadvice ,(intern (symbol-name (car cmds)))
1770 (after
1771 ,(intern (concat "follow-" (symbol-name (car cmds))))
1772 activate)
1773 "Adviced by Follow Mode."
1774 (follow-redraw-after-event (ad-get-arg 0))))
1775 (setq cmds (cdr cmds))))
1778 (defun follow-redraw-after-event (event)
1779 "Adviced by Follow mode."
1780 (condition-case nil
1781 (let* ((orig-win (selected-window))
1782 (win (nth 0 (funcall
1783 (symbol-function 'event-start) event)))
1784 (fmode (assq 'follow-mode
1785 (buffer-local-variables
1786 (window-buffer win)))))
1787 (if (and fmode (cdr fmode))
1788 ;; The selected window is in follow-mode
1789 (progn
1790 ;; Recenter around the dragged window.
1791 (select-window win)
1792 (follow-redisplay)
1793 (select-window orig-win))))
1794 (error nil))))
1797 ((fboundp 'scrollbar-vertical-drag)
1799 ;;; XEmacs style scrollbars.
1802 ;; Advice all scrollbar functions on the form:
1804 ;; (defadvice scrollbar-line-down
1805 ;; (after follow-scrollbar-line-down activate)
1806 ;; (follow-xemacs-scrollbar-support (ad-get-arg 0)))
1808 (let ((cmds '(scrollbar-line-down ; Window
1809 scrollbar-line-up
1810 scrollbar-page-down ; Object
1811 scrollbar-page-up
1812 scrollbar-to-bottom ; Window
1813 scrollbar-to-top
1814 scrollbar-vertical-drag ; Object
1817 (while cmds
1818 (eval
1819 `(defadvice ,(intern (symbol-name (car cmds)))
1820 (after
1821 ,(intern (concat "follow-" (symbol-name (car cmds))))
1822 activate)
1823 "Adviced by `follow-mode'."
1824 (follow-xemacs-scrollbar-support (ad-get-arg 0))))
1825 (setq cmds (cdr cmds))))
1828 (defun follow-xemacs-scrollbar-support (window)
1829 "Redraw windows showing the same buffer as shown in WINDOW.
1830 WINDOW is either the dragged window, or a cons containing the
1831 window as its first element. This is called while the user drags
1832 the scrollbar.
1834 WINDOW can be an object or a window."
1835 (condition-case nil
1836 (progn
1837 (if (consp window)
1838 (setq window (car window)))
1839 (let ((fmode (assq 'follow-mode
1840 (buffer-local-variables
1841 (window-buffer window))))
1842 (orig-win (selected-window)))
1843 (if (and fmode (cdr fmode))
1844 (progn
1845 ;; Recenter around the dragged window.
1846 (select-window window)
1847 (follow-redisplay)
1848 (select-window orig-win)))))
1849 (error nil)))))
1851 ;;}}}
1852 ;;{{{ Process output
1854 ;;; The following sections installs a spy that listens to process
1855 ;;; output and tries to reposition the windows whose buffers are in
1856 ;;; Follow mode. We play safe as much as possible...
1858 ;;; When follow-mode is activated all active processes are
1859 ;;; intercepted. All new processes that change their filter function
1860 ;;; using `set-process-filter' are also intercepted. The reason is
1861 ;;; that a process can cause a redisplay recentering "tail" windows.
1862 ;;; Note that it doesn't hurt to spy on more processes than needed.
1864 ;;; Technically, we set the process filter to `follow-generic-filter'.
1865 ;;; The original filter is stored in `follow-process-filter-alist'.
1866 ;;; Our generic filter calls the original filter, or inserts the
1867 ;;; output into the buffer, if the buffer originally didn't have an
1868 ;;; output filter. It also makes sure that the windows connected to
1869 ;;; the buffer are aligned.
1871 ;;; Discussion: How do we find processes that don't call
1872 ;;; `set-process-filter'? (How often are processes created in a
1873 ;;; buffer after Follow mode are activated?)
1875 ;;; Discussion: Should we also advice `process-filter' to make our
1876 ;;; filter invisible to others?
1878 ;;{{{ Advice for `set-process-filter'
1880 ;; Do not call this with 'follow-generic-filter as the name of the
1881 ;; filter...
1883 (defadvice set-process-filter (before follow-set-process-filter activate)
1884 "Ensure process output will be displayed correctly in Follow Mode buffers.
1886 Follow Mode inserts its own process filter to do its
1887 magic stuff before the real process filter is called."
1888 (if follow-intercept-processes
1889 (progn
1890 (setq follow-process-filter-alist
1891 (delq (assq (ad-get-arg 0) follow-process-filter-alist)
1892 follow-process-filter-alist))
1893 (follow-tidy-process-filter-alist)
1894 (cond ((eq (ad-get-arg 1) t))
1895 ((eq (ad-get-arg 1) nil)
1896 (ad-set-arg 1 'follow-generic-filter))
1898 (setq follow-process-filter-alist
1899 (cons (cons (ad-get-arg 0) (ad-get-arg 1))
1900 follow-process-filter-alist))
1901 (ad-set-arg 1 'follow-generic-filter))))))
1904 (defun follow-call-set-process-filter (proc filter)
1905 "Call original `set-process-filter' without the Follow mode advice."
1906 (ad-disable-advice 'set-process-filter 'before
1907 'follow-set-process-filter)
1908 (ad-activate 'set-process-filter)
1909 (prog1
1910 (set-process-filter proc filter)
1911 (ad-enable-advice 'set-process-filter 'before
1912 'follow-set-process-filter)
1913 (ad-activate 'set-process-filter)))
1916 (defadvice process-filter (after follow-process-filter activate)
1917 "Return the original process filter, not `follow-generic-filter'."
1918 (cond ((eq ad-return-value 'follow-generic-filter)
1919 (setq ad-return-value
1920 (cdr-safe (assq (ad-get-arg 0)
1921 follow-process-filter-alist))))))
1924 (defun follow-call-process-filter (proc)
1925 "Call original `process-filter' without the Follow mode advice."
1926 (ad-disable-advice 'process-filter 'after
1927 'follow-process-filter)
1928 (ad-activate 'process-filter)
1929 (prog1
1930 (process-filter proc)
1931 (ad-enable-advice 'process-filter 'after
1932 'follow-process-filter)
1933 (ad-activate 'process-filter)))
1936 (defun follow-tidy-process-filter-alist ()
1937 "Remove old processes from `follow-process-filter-alist'."
1938 (let ((alist follow-process-filter-alist)
1939 (ps (process-list))
1940 (new ()))
1941 (while alist
1942 (if (and (not (memq (process-status (car (car alist)))
1943 '(exit signal closed nil)))
1944 (memq (car (car alist)) ps))
1945 (setq new (cons (car alist) new)))
1946 (setq alist (cdr alist)))
1947 (setq follow-process-filter-alist new)))
1949 ;;}}}
1950 ;;{{{ Start/stop interception of processes.
1952 ;; Normally, all new processed are intercepted by our `set-process-filter'.
1953 ;; This is needed to intercept old processed that were started before we were
1954 ;; loaded, and processes we have forgotten by calling
1955 ;; `follow-stop-intercept-process-output'.
1957 (defun follow-intercept-process-output ()
1958 "Intercept all active processes.
1960 This is needed so that Follow Mode can track all display events in the
1961 system. (See `follow-mode')"
1962 (interactive)
1963 (let ((list (process-list)))
1964 (while list
1965 (if (eq (process-filter (car list)) 'follow-generic-filter)
1967 ;; The custom `set-process-filter' defined above.
1968 (set-process-filter (car list) (process-filter (car list))))
1969 (setq list (cdr list))))
1970 (setq follow-intercept-processes t))
1973 (defun follow-stop-intercept-process-output ()
1974 "Stop Follow Mode from spying on processes.
1976 All current spypoints are removed and no new will be added.
1978 The effect is that Follow mode won't be able to handle buffers
1979 connected to processes.
1981 The only reason to call this function is if the Follow mode spy filter
1982 would interfere with some other package. If this happens, please
1983 report this using the `follow-submit-feedback' function."
1984 (interactive)
1985 (follow-tidy-process-filter-alist)
1986 (let ((list (process-list)))
1987 (while list
1988 (if (eq (process-filter (car list)) 'follow-generic-filter)
1989 (progn
1990 (follow-call-set-process-filter
1991 (car list)
1992 (cdr-safe (assq (car list) follow-process-filter-alist)))
1993 (setq follow-process-filter-alist
1994 (delq (assq (car list) follow-process-filter-alist)
1995 follow-process-filter-alist))))
1996 (setq list (cdr list))))
1997 (setq follow-intercept-processes nil))
1999 ;;}}}
2000 ;;{{{ The filter
2002 ;;; The following section is a naive method to make buffers with
2003 ;;; process output to work with Follow mode. Whenever the start of the
2004 ;;; window displaying the buffer is moved, we moves it back to its
2005 ;;; original position and try to select a new window. (If we fail,
2006 ;;; the normal redisplay functions of Emacs will scroll it right
2007 ;;; back!)
2009 (defun follow-generic-filter (proc output)
2010 "Process output filter for process connected to buffers in Follow mode."
2011 (let* ((old-buffer (current-buffer))
2012 (orig-win (selected-window))
2013 (buf (process-buffer proc))
2014 (win (and buf (if (eq buf (window-buffer orig-win))
2015 orig-win
2016 (get-buffer-window buf t))))
2017 (return-to-orig-win (and win (not (eq win orig-win))))
2018 (orig-window-start (and win (window-start win))))
2020 ;; If input is pending, the `sit-for' below won't redraw the
2021 ;; display. In that case, calling `follow-avoid-tail-recenter' may
2022 ;; provoke the process hadnling code to sceduling a redisplay.
2023 ;(or (input-pending-p)
2024 ; (follow-avoid-tail-recenter))
2026 ;; Output the `output'.
2027 (let ((filter (cdr-safe (assq proc follow-process-filter-alist))))
2028 (cond
2029 ;; Call the original filter function
2030 (filter
2031 (funcall filter proc output))
2033 ;; No filter, but we've got a buffer. Just output into it.
2034 (buf
2035 (set-buffer buf)
2036 (if (not (marker-buffer (process-mark proc)))
2037 (set-marker (process-mark proc) (point-max)))
2038 (let ((moving (= (point) (process-mark proc)))
2039 deactivate-mark
2040 (inhibit-read-only t))
2041 (save-excursion
2042 (goto-char (process-mark proc))
2043 ;; `insert-before-markers' just in case the users next
2044 ;; command is M-y.
2045 (insert-before-markers output)
2046 (set-marker (process-mark proc) (point)))
2047 (if moving (goto-char (process-mark proc)))))))
2049 ;; If we're in follow mode, do our stuff. Select a new window and
2050 ;; redisplay. (Actually, it is redundant to check `buf', but I
2051 ;; feel it's more correct.)
2052 (if (and buf win (window-live-p win))
2053 (progn
2054 (set-buffer buf)
2055 (if (and (boundp 'follow-mode) follow-mode)
2056 (progn
2057 (select-window win)
2058 (let* ((windows (follow-all-followers win))
2059 (win-start-end (follow-windows-start-end windows))
2060 (new-window-start (window-start win))
2061 (new-window-point (window-point win)))
2062 (cond
2063 ;; The start of the selected window was repositioned.
2064 ;; Try to use the original start position and continue
2065 ;; working with a window to the "right" in the window
2066 ;; chain. This will create the effect that the output
2067 ;; starts in one window and continues into the next.
2069 ;; If the display has changed so much that it is not
2070 ;; possible to keep the original window fixed and still
2071 ;; display the point then we give up and use the new
2072 ;; window start.
2074 ;; This case is typically used when the process filter
2075 ;; tries to reposition the start of the window in order
2076 ;; to view the tail of the output.
2077 ((not (eq orig-window-start new-window-start))
2078 (follow-debug-message "filter: Moved")
2079 (set-window-start win orig-window-start)
2080 (follow-redisplay windows win)
2081 (setq win-start-end (follow-windows-start-end windows))
2082 (follow-select-if-visible new-window-point
2083 win-start-end)
2084 (goto-char new-window-point)
2085 (if (eq win (selected-window))
2086 (set-window-start win new-window-start))
2087 (setq win-start-end (follow-windows-start-end windows)))
2088 ;; Stick to this window, if point is visible in it.
2089 ((pos-visible-in-window-p new-window-point)
2090 (follow-debug-message "filter: Visible in window"))
2091 ;; Avoid redisplaying the first window. If the
2092 ;; point is visible at a window below,
2093 ;; redisplay and select it.
2094 ((follow-select-if-visible-from-first
2095 new-window-point windows)
2096 (follow-debug-message "filter: Seen from first")
2097 (follow-redisplay windows (car windows))
2098 (goto-char new-window-point)
2099 (setq win-start-end
2100 (follow-windows-start-end windows)))
2101 ;; None of the above. We stick to the current window.
2103 (follow-debug-message "filter: nothing")))
2105 ;; Here we have slected a window. Make sure the
2106 ;; windows are aligned and the point is visible
2107 ;; in the selected window.
2108 (if (and (not (follow-pos-visible
2109 (point) (selected-window) win-start-end))
2110 (not return-to-orig-win))
2111 (progn
2112 (sit-for 0)
2113 (setq win-start-end
2114 (follow-windows-start-end windows))))
2116 (if (or follow-internal-force-redisplay
2117 (not (follow-windows-aligned-p win-start-end)))
2118 (follow-redisplay windows)))))))
2120 ;; return to the original window.
2121 (if return-to-orig-win
2122 (select-window orig-win))
2123 ;; Restore the orignal buffer, unless the filter explicitly
2124 ;; changed buffer or killed the old buffer.
2125 (if (and (eq buf (current-buffer))
2126 (buffer-name old-buffer))
2127 (set-buffer old-buffer)))
2129 (follow-invalidate-cache)
2131 ;; Normally, if the display has been changed, it is redrawn. All
2132 ;; windows showing only the end of a buffer is unconditionally
2133 ;; recentered, we can't prevent it by calling
2134 ;; `follow-avoid-tail-recenter'.
2136 ;; By performing a redisplay on our own, Emacs need not perform
2137 ;; the above described redisplay. (However, bu performing it when
2138 ;; there are input available just seems to make things worse.)
2139 (if (and follow-avoid-tail-recenter-p
2140 (not (input-pending-p)))
2141 (sit-for 0)))
2143 ;;}}}
2145 ;;}}}
2146 ;;{{{ Window size change
2148 ;; In Emacs 19.29, the functions in `window-size-change-functions' are
2149 ;; called every time a window in a frame changes size. Most notably, it
2150 ;; is called after the frame has been resized.
2152 ;; We basically call our post-command-hook for every buffer that is
2153 ;; visible in any window in the resized frame, which is in follow-mode.
2155 ;; Since this function can be called indirectly from
2156 ;; `follow-post-command-hook' we have a potential infinite loop. We
2157 ;; handle this problem by simply not doing anything at all in this
2158 ;; situation. The variable `follow-inside-post-command-hook' contains
2159 ;; information about whether the execution actually is inside the
2160 ;; post-command-hook or not.
2162 (if (boundp 'window-size-change-functions)
2163 (add-hook 'window-size-change-functions 'follow-window-size-change))
2166 (defun follow-window-size-change (frame)
2167 "Redraw all windows in FRAME, when in Follow mode."
2168 ;; Below, we call `post-command-hook'. This makes sure that we
2169 ;; doesn't start a mutally recursive endless loop.
2170 (if follow-inside-post-command-hook
2172 (let ((buffers '())
2173 (orig-window (selected-window))
2174 (orig-buffer (current-buffer))
2175 (orig-frame (selected-frame))
2176 windows
2177 buf)
2178 (select-frame frame)
2179 (unwind-protect
2180 (walk-windows
2181 (function
2182 (lambda (win)
2183 (setq buf (window-buffer win))
2184 (if (memq buf buffers)
2186 (set-buffer buf)
2187 (if (and (boundp 'follow-mode)
2188 follow-mode)
2189 (progn
2190 (setq windows (follow-all-followers win))
2191 (if (memq orig-window windows)
2192 (progn
2193 ;; Make sure we're redrawing around the
2194 ;; selected window.
2196 ;; We must be really careful not to do this
2197 ;; when we are (indirectly) called by
2198 ;; `post-command-hook'.
2199 (select-window orig-window)
2200 (follow-post-command-hook)
2201 (setq orig-window (selected-window)))
2202 (follow-redisplay windows win))
2203 (setq buffers (cons buf buffers))))))))
2204 (select-frame orig-frame)
2205 (set-buffer orig-buffer)
2206 (select-window orig-window)))))
2208 ;;}}}
2210 ;;{{{ XEmacs isearch
2212 ;; In XEmacs, isearch often finds matches in other windows than the
2213 ;; currently selected. However, when exiting the old window
2214 ;; configuration is restored, with the exception of the beginning of
2215 ;; the start of the window for the selected window. This is not much
2216 ;; help for us.
2218 ;; We overwrite the stored window configuration with the current,
2219 ;; unless we are in `slow-search-mode', i.e. only a few lines
2220 ;; of text is visible.
2222 (if follow-emacs-version-xemacs-p
2223 (defadvice isearch-done (before follow-isearch-done activate)
2224 (if (and (boundp 'follow-mode)
2225 follow-mode
2226 (boundp 'isearch-window-configuration)
2227 isearch-window-configuration
2228 (boundp 'isearch-slow-terminal-mode)
2229 (not isearch-slow-terminal-mode))
2230 (let ((buf (current-buffer)))
2231 (setq isearch-window-configuration
2232 (current-window-configuration))
2233 (set-buffer buf)))))
2235 ;;}}}
2236 ;;{{{ Tail window handling
2238 ;;; In Emacs (not XEmacs) windows showing nothing are sometimes
2239 ;;; recentered. When in Follow Mode, this is not desireable for
2240 ;;; non-first windows in the window chain. This section tries to
2241 ;;; make the windows stay where they should be.
2243 ;;; If the display is updated, all windows starting at (point-max) are
2244 ;;; going to be recentered at the next redisplay, unless we do a
2245 ;;; read-and-write cycle to update the `force' flag inside the windows.
2247 ;;; In 19.30, a new varible `window-scroll-functions' is called every
2248 ;;; time a window is recentered. It is not perfect for our situation,
2249 ;;; since when it is called for a tail window, it is to late. However,
2250 ;;; if it is called for another window, we can try to update our
2251 ;;; windows.
2253 ;;; By patching `sit-for' we can make sure that to catch all explicit
2254 ;;; updates initiated by lisp programs. Internal calls, on the other
2255 ;;; hand, are not handled.
2257 ;;; Please note that the function `follow-avoid-tail-recenter' is also
2258 ;;; called from other places, e.g. `post-command-hook' and
2259 ;;; `post-command-idle-hook'.
2261 ;; If this function is called it is too late for this window, but
2262 ;; we might save other windows from being recentered.
2264 (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions))
2265 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t))
2268 ;; This prevents all packages that calls `sit-for' directly
2269 ;; to recenter tail windows.
2271 (if follow-avoid-tail-recenter-p
2272 (defadvice sit-for (before follow-sit-for activate)
2273 "Adviced by Follow Mode.
2275 Avoid to recenter windows displaying only the end of a file as when
2276 displaying a short file in two windows, using Follow Mode."
2277 (follow-avoid-tail-recenter)))
2280 ;; Without this advice, `mouse-drag-region' would start to recenter
2281 ;; tail windows.
2283 (if (and follow-avoid-tail-recenter-p
2284 (fboundp 'move-overlay))
2285 (defadvice move-overlay (before follow-move-overlay activate)
2286 "Adviced by Follow Mode.
2287 Don't recenter windows showing only the end of a buffer.
2288 This prevents `mouse-drag-region' from messing things up."
2289 (follow-avoid-tail-recenter)))
2291 ;;}}}
2292 ;;{{{ profile support
2294 ;; The following (non-evaluated) section can be used to
2295 ;; profile this package using `elp'.
2297 ;; Invalid indentation on purpose!
2299 (cond (nil
2300 (setq elp-function-list
2301 '(window-end
2302 vertical-motion
2303 ; sit-for ;; elp can't handle advices...
2304 follow-mode
2305 follow-all-followers
2306 follow-split-followers
2307 follow-redisplay
2308 follow-downward
2309 follow-calculate-first-window-start
2310 follow-estimate-first-window-start
2311 follow-calculate-first-window-start-from-above
2312 follow-calculate-first-window-start-from-below
2313 follow-calc-win-end
2314 follow-calc-win-start
2315 follow-pos-visible
2316 follow-windows-start-end
2317 follow-cache-valid-p
2318 follow-select-if-visible
2319 follow-select-if-visible-from-first
2320 follow-windows-aligned-p
2321 follow-point-visible-all-windows-p
2322 follow-avoid-tail-recenter
2323 follow-update-window-start
2324 follow-post-command-hook
2325 ))))
2327 ;;}}}
2329 ;;{{{ The end
2332 ;;; We're done!
2335 (provide 'follow)
2337 ;;}}}
2339 ;; /------------------------------------------------------------------------\
2340 ;; | "I [..] am rarely happier then when spending an entire day programming |
2341 ;; | my computer to perform automatically a task that it would otherwise |
2342 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
2343 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
2344 ;; | worth the investment of a day's happy activity working out a way to |
2345 ;; | save it". -- Douglas Adams, "Last Chance to See" |
2346 ;; \------------------------------------------------------------------------/
2348 ;; arch-tag: 7b16bb1a-808c-4991-a8cc-66d3822936d0
2349 ;;; follow.el ends here