(emacs-version): Check for gtk. Include gtk version info.
[emacs.git] / lisp / follow.el
blob4480ba6b8491ad196c5c19338fd98b2f88915ec6
1 ;;; follow.el --- synchronize windows showing the same buffer
3 ;; Copyright (C) 1995, 1996, 1997, 1999, 2001 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren <andersl@andersl.com>
6 ;; Maintainer: Anders Lindgren <andersl@andersl.com>
7 ;; Created: 1995-05-25
8 ;; Keywords: display, window, minor-mode, convenience
9 ;; Last Changed: 1999-11-17
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 2, 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., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, 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 displays 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 would have been
50 ;; one. 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 eachother!
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 exists two system variables that controls the appearence of
105 ;; lines that are wider than the window containing them. The default
106 ;; is to truncate long lines whenever a window isn't as wide as the
107 ;; frame.
109 ;; To make sure lines are never truncated, please place the following
110 ;; lines in your init file:
112 ;; (setq truncate-lines nil)
113 ;; (setq truncate-partial-width-windows nil)
116 ;; Since the display of XEmacs is pixel-oriented, a line could be
117 ;; clipped in half at the bottom of the window.
119 ;; To make XEmacs avoid clipping (normal) lines, please place the
120 ;; following line in your init-file:
122 ;; (setq pixel-vertical-clip-threshold 30)
125 ;; The correct way to cofigurate Follow mode, or any other mode for
126 ;; that matter, is to create one (or more) function that does
127 ;; whatever you would like to do. The function is then added to
128 ;; a hook.
130 ;; When `Follow' mode is activated, functions stored in the hook
131 ;; `follow-mode-hook' are called. When it is deactivated
132 ;; `follow-mode-off-hook' is run.
134 ;; The keymap `follow-key-map' contains key bindings activated by
135 ;; `follow-mode'.
137 ;; Example:
138 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
140 ;; (defun my-follow-mode-hook ()
141 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
142 ;; (define-key follow-mode-map "\C-cb" 'another-function))
145 ;; Usage:
147 ;; To activate issue the command "M-x follow-mode"
148 ;; and press return. To deactivate, do it again.
150 ;; The following is a list of commands useful when follow-mode is active.
152 ;; follow-scroll-up C-c . C-v
153 ;; Scroll text in a Follow Mode window chain up.
155 ;; follow-scroll-down C-c . v
156 ;; Like `follow-scroll-up', but in the other direction.
158 ;; follow-delete-other-windows-and-split C-c . 1
159 ;; Maximise the visible area of the current buffer,
160 ;; and enter Follow Mode. This is a very convenient
161 ;; way to start Follow Mode, hence it is recomended
162 ;; that this command is added to the global keymap.
164 ;; follow-recenter C-c . C-l
165 ;; Place the point in the center of the middle window,
166 ;; or a specified number of lines from either top or bottom.
168 ;; follow-switch-to-buffer C-c . b
169 ;; Switch buffer in all windows displaying the current buffer
170 ;; in this frame.
172 ;; follow-switch-to-buffer-all C-c . C-b
173 ;; Switch buffer in all windows in the active frame.
175 ;; follow-switch-to-current-buffer-all
176 ;; Show the current buffer in all windows on the current
177 ;; frame and turn on `follow-mode'.
179 ;; follow-first-window C-c . <
180 ;; Select the first window in the frame showing the same buffer.
182 ;; follow-last-window C-c . >
183 ;; Select the last window in the frame showing the same buffer.
185 ;; follow-next-window C-c . n
186 ;; Select the next window in the frame showing the same buffer.
188 ;; follow-previous-window C-c . p
189 ;; Select the previous window showing the same buffer.
192 ;; Well, it seems ok, but what if I really want to look at two different
193 ;; positions in the text? Here are two simple methods to use:
195 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
196 ;; in the same frame. (My apoligies to you who can't use frames.)
198 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
199 ;; you want to view two locations. Of course, `follow' mode can
200 ;; be reactivated by hitting the same key again.
202 ;; Example from my ~/.emacs:
203 ;; (global-set-key [f8] 'follow-mode)
206 ;; Implementation:
208 ;; In an ideal world, follow mode would have been implemented in the
209 ;; kernel of the display routines, making sure that the windows (using
210 ;; follow mode) ALWAYS are aligned. On planet earth, however, we must
211 ;; accept a solution where we ALMOST ALWAYS can make sure that the
212 ;; windows are aligned.
214 ;; Follow mode does this in three places:
215 ;; 1) After each user command.
216 ;; 2) After a process output has been perfomed.
217 ;; 3) When a scrollbar has been moved.
219 ;; This will cover most situations. (Let me know if there are other
220 ;; situations that should be covered.)
222 ;; Note that only the selected window is checked, for the reason of
223 ;; efficiency and code complexity. (I.e. it is possible to make a
224 ;; non-selected windows unaligned. It will, however, pop right back
225 ;; when it is selected.)
227 ;;}}}
229 ;;; Code:
231 ;;{{{ Preliminaries
233 ;; Make the compiler shut up!
234 ;; There are two strategies:
235 ;; 1) Shut warnings off completely.
236 ;; 2) Handle each warning separately.
238 ;; Since I would like to see real errors, I've selected the latter
239 ;; method.
241 ;; The problem with undefined variables and functions has been solved
242 ;; by using `set', `symbol-value' and `symbol-function' rather than
243 ;; `setq' and direct references to variables and functions.
245 ;; For example:
246 ;; (if (boundp 'foo) ... (symbol-value 'foo) )
247 ;; (set 'foo ...) <-- XEmacs doesn't fall for this one.
248 ;; (funcall (symbol-function 'set) 'bar ...)
250 ;; Note: When this file is interpreted, `eval-when-compile' is
251 ;; evaluted. Since it doesn't hurt to evaluate it, but it is a bit
252 ;; annoying, we test if the byte-compiler has been loaded. This can,
253 ;; of course, lead to some occasional unintended evaluation...
255 ;; Should someone come up with a better solution, please let me
256 ;; know.
258 (eval-when-compile
259 (if (or (featurep 'bytecomp)
260 (featurep 'byte-compile))
261 (cond ((string-match "XEmacs" emacs-version)
262 ;; Make XEmacs shut up! I'm using standard Emacs
263 ;; functions, they are NOT obsolete!
264 (if (eq (get 'force-mode-line-update 'byte-compile)
265 'byte-compile-obsolete)
266 (put 'force-mode-line-update 'byte-compile 'nil))
267 (if (eq (get 'frame-first-window 'byte-compile)
268 'byte-compile-obsolete)
269 (put 'frame-first-window 'byte-compile 'nil))))))
271 ;;}}}
272 ;;{{{ Variables
274 (defgroup follow nil
275 "Synchronize windows showing the same buffer."
276 :prefix "follow-"
277 :group 'windows
278 :group 'convenience)
280 (defvar follow-mode nil
281 "Variable indicating if Follow mode is active.")
283 (defcustom follow-mode-hook nil
284 "*Hooks to run when follow-mode is turned on."
285 :type 'hook
286 :group 'follow)
288 (defcustom follow-mode-off-hook nil
289 "*Hooks to run when follow-mode is turned off."
290 :type 'hook
291 :group 'follow)
293 (defvar follow-mode-map nil
294 "*Minor mode keymap for Follow mode.")
296 (defcustom follow-mode-line-text " Follow"
297 "*Text shown in the mode line when Follow mode is active.
298 Defaults to \" Follow\". Examples of other values
299 are \" Fw\", or simply \"\"."
300 :type 'string
301 :group 'follow)
303 (defcustom follow-auto nil
304 "*Non-nil activates Follow mode whenever a file is loaded."
305 :type 'boolean
306 :group 'follow)
308 (defcustom follow-mode-prefix "\C-c."
309 "*Prefix key to use for follow commands in Follow mode.
310 The value of this variable is checked as part of loading Follow mode.
311 After that, changing the prefix key requires manipulating keymaps."
312 :type 'string
313 :group 'follow)
315 (defcustom follow-intercept-processes
316 (fboundp 'start-process)
317 "*When non-nil, Follow Mode will monitor process output."
318 :type 'boolean
319 :group 'follow)
321 (defvar follow-emacs-version-xemacs-p
322 (string-match "XEmacs" emacs-version)
323 "Non-nil when running under XEmacs.")
325 (defvar follow-avoid-tail-recenter-p
326 (not follow-emacs-version-xemacs-p)
327 "*When non-nil, patch emacs so that tail windows won't be recentered.
329 A \"tail window\" is a window that displays only the end of
330 the buffer. Normally it is practical for the user that empty
331 windows are recentered automatically. However, when using
332 Follow Mode it breaks the display when the end is displayed
333 in a window \"above\" the last window. This is for
334 example the case when displaying a short page in info.
336 Must be set before Follow Mode is loaded.
338 Please note that it is not possible to fully prevent Emacs from
339 recentering empty windows. Please report if you find a repeatable
340 situation in which Emacs recenters empty windows.
342 XEmacs, as of 19.12, does not recenter windows, good!")
344 (defvar follow-cache-command-list
345 '(next-line previous-line forward-char backward-char)
346 "List of commands that don't require recalculation.
348 In order to be able to use the cache, a command should not change the
349 contents of the buffer, nor should it change selected window or current
350 buffer.
352 The commands in this list are checked at load time.
354 To mark other commands as suitable for caching, set the symbol
355 property `follow-mode-use-cache' to non-nil.")
357 (defvar follow-debug nil
358 "*Non-nil when debugging Follow mode.")
361 ;; Internal variables:
363 (defvar follow-internal-force-redisplay nil
364 "True when Follow mode should redisplay the windows.")
366 (defvar follow-process-filter-alist '()
367 "The original filters for processes intercepted by Follow mode.")
369 (defvar follow-active-menu nil
370 "The menu visible when Follow mode is active.")
372 (defvar follow-deactive-menu nil
373 "The menu visible when Follow mode is deactivated.")
375 (defvar follow-inside-post-command-hook nil
376 "Non-nil when inside Follow modes `post-command-hook'.
377 Used by `follow-window-size-change'.")
379 (defvar follow-windows-start-end-cache nil
380 "Cache used by `follow-window-start-end'.")
382 ;;}}}
383 ;;{{{ Bug report
385 (eval-when-compile (require 'reporter))
387 (defun follow-submit-feedback ()
388 "Submit feedback on Follow mode to the author: andersl@andersl.com"
389 (interactive)
390 (require 'reporter)
391 (and (y-or-n-p "Do you really want to submit a report on Follow mode? ")
392 (reporter-submit-bug-report
393 "Anders Lindgren <andersl@andersl.com>"
394 "follow.el"
395 '(post-command-hook
396 post-command-idle-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 (if (boundp 'post-command-idle-hook)
686 (add-hook 'post-command-idle-hook
687 'follow-avoid-tail-recenter t))
688 (run-hooks 'follow-mode-hook))
690 ((and (not follow-mode) follow-mode-orig) ; Off
691 (force-mode-line-update)
692 (run-hooks 'follow-mode-off-hook)))))
695 ;; Register follow-mode as a minor mode.
697 (if (fboundp 'add-minor-mode)
698 ;; XEmacs
699 (funcall (symbol-function 'add-minor-mode)
700 'follow-mode 'follow-mode-line-text)
701 (or (assq 'follow-mode minor-mode-alist)
702 (setq minor-mode-alist
703 (cons '(follow-mode follow-mode-line-text) minor-mode-alist))))
705 ;;}}}
706 ;;{{{ Find file hook
708 ;; This will start follow-mode whenever a new file is loaded, if
709 ;; the variable `follow-auto' is non-nil.
711 (add-hook 'find-file-hooks 'follow-find-file-hook t)
713 (defun follow-find-file-hook ()
714 "Find-file hook for Follow Mode. See the variable `follow-auto'."
715 (if follow-auto (follow-mode t)))
717 ;;}}}
719 ;;{{{ User functions
722 ;;; User functions usable when in Follow mode.
725 ;;{{{ Scroll
727 ;; `scroll-up' and `-down', but for windows in Follow Mode.
729 ;; Almost like the real thing, excpet when the cursor ends up outside
730 ;; the top or bottom... In our case however, we end up outside the
731 ;; window and hence we are recenterd. Should we let `recenter' handle
732 ;; the point position we would never leave the selected window. To do
733 ;; it ourselves we would need to do our own redisplay, which is easier
734 ;; said than done. (Why didn't I do a real display abstraction from
735 ;; the beginning?)
737 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
738 ;; our post-command-hook will move our windows back into the old
739 ;; position... (This would also be corrected if we would have had a
740 ;; good redisplay abstraction.)
742 (defun follow-scroll-up (&optional arg)
743 "Scroll text in a Follow Mode window chain up.
745 If called with no ARG, the `next-screen-context-lines' last lines of
746 the bottom window in the chain will be visible in the top window.
748 If called with an argument, scroll ARG lines up.
749 Negative ARG means scroll downward.
751 Works like `scroll-up' when not in Follow Mode."
752 (interactive "P")
753 (cond ((not (and (boundp 'follow-mode) follow-mode))
754 (scroll-up arg))
755 (arg
756 (save-excursion (scroll-up arg))
757 (setq follow-internal-force-redisplay t))
759 (let* ((windows (follow-all-followers))
760 (end (window-end (car (reverse windows)))))
761 (if (eq end (point-max))
762 (signal 'end-of-buffer nil)
763 (select-window (car windows))
764 ;; `window-end' might return nil.
765 (if end
766 (goto-char end))
767 (vertical-motion (- next-screen-context-lines))
768 (set-window-start (car windows) (point)))))))
771 (defun follow-scroll-down (&optional arg)
772 "Scroll text in a Follow Mode window chain down.
774 If called with no ARG, the `next-screen-context-lines' top lines of
775 the top window in the chain will be visible in the bottom window.
777 If called with an argument, scroll ARG lines down.
778 Negative ARG means scroll upward.
780 Works like `scroll-up' when not in Follow Mode."
781 (interactive "P")
782 (cond ((not (and (boundp 'follow-mode) follow-mode))
783 (scroll-up arg))
784 (arg
785 (save-excursion (scroll-down arg)))
787 (let* ((windows (follow-all-followers))
788 (win (car (reverse windows)))
789 (start (window-start (car windows))))
790 (if (eq start (point-min))
791 (signal 'beginning-of-buffer nil)
792 (select-window win)
793 (goto-char start)
794 (vertical-motion (- (- (window-height win)
796 next-screen-context-lines)))
797 (set-window-start win (point))
798 (goto-char start)
799 (vertical-motion (- next-screen-context-lines 1))
800 (setq follow-internal-force-redisplay t))))))
802 ;;}}}
803 ;;{{{ Buffer
805 ;;;###autoload
806 (defun follow-delete-other-windows-and-split (&optional arg)
807 "Create two side by side windows and enter Follow Mode.
809 Execute this command to display as much as possible of the text
810 in the selected window. All other windows, in the current
811 frame, are deleted and the selected window is split in two
812 side-by-side windows. Follow Mode is activated, hence the
813 two windows always will display two successive pages.
814 \(If one window is moved, the other one will follow.)
816 If ARG is positive, the leftmost window is selected. If it negative,
817 the rightmost is selected. If ARG is nil, the leftmost window is
818 selected if the original window is the first one in the frame.
820 To bind this command to a hotkey, place the following line
821 in your `~/.emacs' file, replacing [f7] by your favourite key:
822 (global-set-key [f7] 'follow-delete-other-windows-and-split)"
823 (interactive "P")
824 (let ((other (or (and (null arg)
825 (not (eq (selected-window)
826 (frame-first-window (selected-frame)))))
827 (and arg
828 (< (prefix-numeric-value arg) 0))))
829 (start (window-start)))
830 (delete-other-windows)
831 (split-window-horizontally)
832 (if other
833 (progn
834 (other-window 1)
835 (set-window-start (selected-window) start)
836 (setq follow-internal-force-redisplay t)))
837 (follow-mode 1)))
839 (defun follow-switch-to-buffer (buffer)
840 "Show BUFFER in all windows in the current Follow Mode window chain."
841 (interactive "BSwitch to Buffer: ")
842 (let ((orig-window (selected-window))
843 (windows (follow-all-followers)))
844 (while windows
845 (select-window (car windows))
846 (switch-to-buffer buffer)
847 (setq windows (cdr windows)))
848 (select-window orig-window)))
851 (defun follow-switch-to-buffer-all (&optional buffer)
852 "Show BUFFER in all windows on this frame.
853 Defaults to current buffer."
854 (interactive (list (read-buffer "Switch to Buffer: "
855 (current-buffer))))
856 (or buffer (setq buffer (current-buffer)))
857 (let ((orig-window (selected-window)))
858 (walk-windows
859 (function
860 (lambda (win)
861 (select-window win)
862 (switch-to-buffer buffer))))
863 (select-window orig-window)
864 (follow-redisplay)))
867 (defun follow-switch-to-current-buffer-all ()
868 "Show current buffer in all windows on this frame, and enter Follow Mode.
870 To bind this command to a hotkey place the following line
871 in your `~/.emacs' file:
872 (global-set-key [f7] 'follow-switch-to-current-buffer-all)"
873 (interactive)
874 (or (and (boundp 'follow-mode) follow-mode)
875 (follow-mode 1))
876 (follow-switch-to-buffer-all))
878 ;;}}}
879 ;;{{{ Movement
881 ;; Note, these functions are not very useful, atleast not unless you
882 ;; rebind the rather cumbersome key sequence `C-c . p'.
884 (defun follow-next-window ()
885 "Select the next window showing the same buffer."
886 (interactive)
887 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
888 (if succ
889 (select-window (car succ))
890 (error "%s" "No more windows"))))
893 (defun follow-previous-window ()
894 "Select the previous window showing the same buffer."
895 (interactive)
896 (let ((pred (car (follow-split-followers (follow-all-followers)))))
897 (if pred
898 (select-window (car pred))
899 (error "%s" "No more windows"))))
902 (defun follow-first-window ()
903 "Select the first window in the frame showing the same buffer."
904 (interactive)
905 (select-window (car (follow-all-followers))))
908 (defun follow-last-window ()
909 "Select the last window in the frame showing the same buffer."
910 (interactive)
911 (select-window (car (reverse (follow-all-followers)))))
913 ;;}}}
914 ;;{{{ Redraw
916 (defun follow-recenter (&optional arg)
917 "Recenter the middle window around point.
918 Rearrange all other windows around the middle window.
920 With a positive argument, place the current line ARG lines
921 from the top. With a negative, place it -ARG lines from the
922 bottom."
923 (interactive "P")
924 (if arg
925 (let ((p (point))
926 (arg (prefix-numeric-value arg)))
927 (if (>= arg 0)
928 ;; Recenter relative to the top.
929 (progn
930 (follow-first-window)
931 (goto-char p)
932 (recenter arg))
933 ;; Recenter relative to the bottom.
934 (follow-last-window)
935 (goto-char p)
936 (recenter arg)
937 ;; Otherwise, our post-command-hook will move the window
938 ;; right back.
939 (setq follow-internal-force-redisplay t)))
940 ;; Recenter in the middle.
941 (let* ((dest (point))
942 (windows (follow-all-followers))
943 (win (nth (/ (- (length windows) 1) 2) windows)))
944 (select-window win)
945 (goto-char dest)
946 (recenter)
947 ;;(setq follow-internal-force-redisplay t)
951 (defun follow-redraw ()
952 "Arrange windows displaying the same buffer in successor order.
953 This function can be called even if the buffer is not in Follow mode.
955 Hopefully, there should be no reason to call this function when in
956 Follow mode since the windows should always be aligned."
957 (interactive)
958 (sit-for 0)
959 (follow-redisplay))
961 ;;}}}
962 ;;{{{ End of buffer
964 (defun follow-end-of-buffer (&optional arg)
965 "Move point to the end of the buffer, Follow Mode style.
967 If the end is not visible, it will be displayed in the last possible
968 window in the Follow Mode window chain.
970 The mark is left at the previous position. With arg N, put point N/10
971 of the way from the true end."
972 (interactive "P")
973 (let ((followers (follow-all-followers))
974 (pos (point)))
975 (cond (arg
976 (select-window (car (reverse followers))))
977 ((follow-select-if-end-visible
978 (follow-windows-start-end followers)))
980 (select-window (car (reverse followers)))))
981 (goto-char pos)
982 (end-of-buffer arg)))
984 ;;}}}
986 ;;}}}
988 ;;{{{ Display
990 ;;;; The display routines
992 ;;{{{ Information gathering functions
994 (defun follow-all-followers (&optional testwin)
995 "Return all windows displaying the same buffer as the TESTWIN.
996 The list contains only windows displayed in the same frame as TESTWIN.
997 If TESTWIN is nil the selected window is used."
998 (or (and testwin (window-live-p testwin))
999 (setq testwin (selected-window)))
1000 (let* ((top (frame-first-window (window-frame testwin)))
1001 (win top)
1002 (done nil)
1003 (windows '())
1004 (buffer (window-buffer testwin)))
1005 (while (and (not done) win)
1006 (if (eq (window-buffer win) buffer)
1007 (setq windows (cons win windows)))
1008 (setq win (next-window win 'not))
1009 (if (eq win top)
1010 (setq done t)))
1011 (nreverse windows)))
1014 (defun follow-split-followers (windows &optional win)
1015 "Split the WINDOWS into the sets: predecessors and successors.
1016 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
1017 from the selected window."
1018 (or win
1019 (setq win (selected-window)))
1020 (let ((pred '()))
1021 (while (not (eq (car windows) win))
1022 (setq pred (cons (car windows) pred))
1023 (setq windows (cdr windows)))
1024 (cons pred (cdr windows))))
1027 ;; This function is optimized function for speed!
1029 (defun follow-calc-win-end (&optional win)
1030 "Calculate the presumed window end for WIN.
1032 Actually, the position returned is the start of the next
1033 window, normally is the end plus one.
1035 If WIN is nil, the selected window is used.
1037 Returns (end-pos end-of-buffer-p)"
1038 (if follow-emacs-version-xemacs-p
1039 ;; XEmacs can calculate the end of the window by using
1040 ;; the 'guarantee options. GOOD!
1041 (let ((end (window-end win t)))
1042 (if (= end (funcall (symbol-function 'point-max)
1043 (window-buffer win)))
1044 (list end t)
1045 (list (+ end 1) nil)))
1046 ;; Emacs: We have to calculate the end by ourselves.
1047 ;; This code works on both XEmacs and Emacs, but now
1048 ;; that XEmacs has got custom-written code, this could
1049 ;; be optimized for Emacs.
1050 (let ((orig-win (and win (selected-window)))
1051 height
1052 buffer-end-p)
1053 (if win (select-window win))
1054 (prog1
1055 (save-excursion
1056 (goto-char (window-start))
1057 (setq height (- (window-height) 1))
1058 (setq buffer-end-p
1059 (if (bolp)
1060 (not (= height (vertical-motion height)))
1061 (save-restriction
1062 ;; Fix a mis-feature in `vertical-motion':
1063 ;; The start of the window is assumed to
1064 ;; coinside with the start of a line.
1065 (narrow-to-region (point) (point-max))
1066 (not (= height (vertical-motion height))))))
1067 (list (point) buffer-end-p))
1068 (if orig-win
1069 (select-window orig-win))))))
1072 ;; Can't use `save-window-excursion' since it triggers a redraw.
1073 (defun follow-calc-win-start (windows pos win)
1074 "Calculate where WIN will start if the first in WINDOWS start at POS.
1076 If WIN is nil the point below all windows is returned."
1077 (let (start)
1078 (while (and windows (not (eq (car windows) win)))
1079 (setq start (window-start (car windows)))
1080 (set-window-start (car windows) pos 'noforce)
1081 (setq pos (car (inline (follow-calc-win-end (car windows)))))
1082 (set-window-start (car windows) start 'noforce)
1083 (setq windows (cdr windows)))
1084 pos))
1087 ;; The result from `follow-windows-start-end' is cached when using
1088 ;; a handful simple commands, like cursor movement commands.
1090 (defsubst follow-cache-valid-p (windows)
1091 "Test if the cached value of `follow-windows-start-end' can be used.
1092 Note that this handles the case when the cache has been set to nil."
1093 (let ((res t)
1094 (cache follow-windows-start-end-cache))
1095 (while (and res windows cache)
1096 (setq res (and (eq (car windows)
1097 (car (car cache)))
1098 (eq (window-start (car windows))
1099 (car (cdr (car cache))))))
1100 (setq windows (cdr windows))
1101 (setq cache (cdr cache)))
1102 (and res (null windows) (null cache))))
1105 (defsubst follow-invalidate-cache ()
1106 "Force `follow-windows-start-end' to recalculate the end of the window."
1107 (setq follow-windows-start-end-cache nil))
1110 ;; Build a list of windows and their start and end positions.
1111 ;; Useful to avoid calculating start/end position whenever they are needed.
1112 ;; The list has the format:
1113 ;; ((Win Start End End-of-buffer-visible-p) ...)
1115 ;; Used to have a `save-window-excursion', but it obviously triggered
1116 ;; redraws of the display. Check if I used it for anything.
1119 (defun follow-windows-start-end (windows)
1120 "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
1121 (if (follow-cache-valid-p windows)
1122 follow-windows-start-end-cache
1123 (let ((win-start-end '())
1124 (orig-win (selected-window)))
1125 (while windows
1126 (select-window (car windows))
1127 (setq win-start-end
1128 (cons (cons (car windows)
1129 (cons (window-start)
1130 (follow-calc-win-end)))
1131 win-start-end))
1132 (setq windows (cdr windows)))
1133 (select-window orig-win)
1134 (setq follow-windows-start-end-cache (nreverse win-start-end))
1135 follow-windows-start-end-cache)))
1138 (defsubst follow-pos-visible (pos win win-start-end)
1139 "Non-nil when POS is visible in WIN."
1140 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
1141 (and (>= pos (car wstart-wend-bend))
1142 (or (< pos (car (cdr wstart-wend-bend)))
1143 (nth 2 wstart-wend-bend)))))
1146 ;; By `aligned' we mean that for all adjecent windows, the end of the
1147 ;; first is equal with the start of the successor. The first window
1148 ;; should start at a full screen line.
1150 (defsubst follow-windows-aligned-p (win-start-end)
1151 "Non-nil if the follower WINDOWS are aligned."
1152 (let ((res t))
1153 (save-excursion
1154 (goto-char (window-start (car (car win-start-end))))
1155 (if (bolp)
1157 (vertical-motion 0 (car (car win-start-end)))
1158 (setq res (eq (point) (window-start (car (car win-start-end)))))))
1159 (while (and res (cdr win-start-end))
1160 ;; At least two followers left
1161 (setq res (eq (car (cdr (cdr (car win-start-end))))
1162 (car (cdr (car (cdr win-start-end))))))
1163 (setq win-start-end (cdr win-start-end)))
1164 res))
1167 ;; Check if the point is visible in all windows. (So that
1168 ;; no one will be recentered.)
1170 (defun follow-point-visible-all-windows-p (win-start-end)
1171 "Non-nil when the window-point is visible in all windows."
1172 (let ((res t))
1173 (while (and res win-start-end)
1174 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
1175 (car (car win-start-end))
1176 win-start-end))
1177 (setq win-start-end (cdr win-start-end)))
1178 res))
1181 ;; Make sure WIN always starts at the beginning of an whole screen
1182 ;; line. If WIN is not aligned the start is updated which probably
1183 ;; will lead to a redisplay of the screen later on.
1185 ;; This is used with the first window in a follow chain. The reason
1186 ;; is that we want to detect that the point is outside the window.
1187 ;; (Without the update, the start of the window will move as the
1188 ;; user presses BackSpace, and the other window redisplay routines
1189 ;; will move the start of the window in the wrong direction.)
1191 (defun follow-update-window-start (win)
1192 "Make sure that the start of WIN starts at a full screen line."
1193 (save-excursion
1194 (goto-char (window-start win))
1195 (if (bolp)
1197 (vertical-motion 0 win)
1198 (if (eq (point) (window-start win))
1200 (vertical-motion 1 win)
1201 (set-window-start win (point) 'noforce)))))
1203 ;;}}}
1204 ;;{{{ Selection functions
1206 ;; Make a window in WINDOWS selected if it currently
1207 ;; is displaying the position DEST.
1209 ;; We don't select a window if it just has been moved.
1211 (defun follow-select-if-visible (dest win-start-end)
1212 "Select and return a window, if DEST is visible in it.
1213 Return the selected window."
1214 (let ((win nil))
1215 (while (and (not win) win-start-end)
1216 ;; Don't select a window that was just moved. This makes it
1217 ;; possible to later select the last window after a `end-of-buffer'
1218 ;; command.
1219 (if (follow-pos-visible dest (car (car win-start-end)) win-start-end)
1220 (progn
1221 (setq win (car (car win-start-end)))
1222 (select-window win)))
1223 (setq win-start-end (cdr win-start-end)))
1224 win))
1227 ;; Lets select a window showing the end. Make sure we only select it if it
1228 ;; it wasn't just moved here. (i.e. M-> shall not unconditionally place
1229 ;; the point in the selected window.)
1231 ;; (Compability cludge: in Emacs `window-end' is equal to `point-max';
1232 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1233 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1235 (defun follow-select-if-end-visible (win-start-end)
1236 "Select and return a window, if end is visible in it."
1237 (let ((win nil))
1238 (while (and (not win) win-start-end)
1239 ;; Don't select a window that was just moved. This makes it
1240 ;; possible to later select the last window after a `end-of-buffer'
1241 ;; command.
1242 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1243 (nth 3 (car win-start-end))
1244 ;; `window-end' might return nil.
1245 (let ((end (window-end (car (car win-start-end)))))
1246 (and end
1247 (eq (point-max) (min (point-max) end)))))
1248 (progn
1249 (setq win (car (car win-start-end)))
1250 (select-window win)))
1251 (setq win-start-end (cdr win-start-end)))
1252 win))
1255 ;; Select a window that will display the point if the windows would
1256 ;; be redisplayed with the first window fixed. This is useful for
1257 ;; example when the user has pressed return at the bottom of a window
1258 ;; as the point is not visible in any window.
1260 (defun follow-select-if-visible-from-first (dest windows)
1261 "Select and return a window with DEST, if WINDOWS are redrawn from top."
1262 (let ((win nil)
1263 end-pos-end-p)
1264 (save-excursion
1265 (goto-char (window-start (car windows)))
1266 ;; Make sure the line start in the beginning of a real screen
1267 ;; line.
1268 (vertical-motion 0 (car windows))
1269 (if (< dest (point))
1270 ;; Above the start, not visible.
1272 ;; At or below the start. Check the windows.
1273 (save-window-excursion
1274 (while (and (not win) windows)
1275 (set-window-start (car windows) (point) 'noforce)
1276 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1277 (goto-char (car end-pos-end-p))
1278 ;; Visible, if dest above end, or if eob is visible inside
1279 ;; the window.
1280 (if (or (car (cdr end-pos-end-p))
1281 (< dest (point)))
1282 (setq win (car windows))
1283 (setq windows (cdr windows)))))))
1284 (if win
1285 (select-window win))
1286 win))
1289 ;;}}}
1290 ;;{{{ Redisplay
1292 ;; Redraw all the windows on the screen, starting with the top window.
1293 ;; The window used as as marker is WIN, or the selcted window if WIN
1294 ;; is nil.
1296 (defun follow-redisplay (&optional windows win)
1297 "Reposition the WINDOWS around WIN.
1298 Should the point be too close to the roof we redisplay everything
1299 from the top. WINDOWS should contain a list of windows to
1300 redisplay, it is assumed that WIN is a member of the list.
1301 Should WINDOWS be nil, the windows displaying the
1302 same buffer as WIN, in the current frame, are used.
1303 Should WIN be nil, the selected window is used."
1304 (or win
1305 (setq win (selected-window)))
1306 (or windows
1307 (setq windows (follow-all-followers win)))
1308 (follow-downward windows (follow-calculate-first-window-start windows win)))
1311 ;; Redisplay a chain of windows. Start every window directly after the
1312 ;; end of the previous window, to make sure long lines are displayed
1313 ;; correctly.
1315 (defun follow-downward (windows pos)
1316 "Redisplay all WINDOWS starting at POS."
1317 (while windows
1318 (set-window-start (car windows) pos)
1319 (setq pos (car (follow-calc-win-end (car windows))))
1320 (setq windows (cdr windows))))
1323 ;;(defun follow-downward (windows pos)
1324 ;; "Redisplay all WINDOWS starting at POS."
1325 ;; (let (p)
1326 ;; (while windows
1327 ;; (setq p (window-point (car windows)))
1328 ;; (set-window-start (car windows) pos)
1329 ;; (set-window-point (car windows) (max p pos))
1330 ;; (setq pos (car (follow-calc-win-end (car windows))))
1331 ;; (setq windows (cdr windows)))))
1334 ;; Return the start of the first window.
1336 ;; First, estimate the position. It the value is not perfect (i.e. we
1337 ;; have somewhere splited a line between windows) we try to enhance
1338 ;; the value.
1340 ;; The guess is always perfect if no long lines is split between
1341 ;; windows.
1343 ;; The worst case peformace of probably very bad, but it is very
1344 ;; unlikely that we ever will miss the correct start by more than one
1345 ;; or two lines.
1347 (defun follow-calculate-first-window-start (windows &optional win start)
1348 "Calculate the start of the first window.
1350 WINDOWS is a chain of windows to work with. WIN is the window
1351 to recenter around. It is assumed that WIN starts at position
1352 START."
1353 (or win
1354 (setq win (selected-window)))
1355 (or start
1356 (setq start (window-start win)))
1357 (let ((guess (follow-estimate-first-window-start windows win start)))
1358 (if (car guess)
1359 (cdr guess)
1360 ;; The guess wasn't exact, try to enhance it.
1361 (let ((win-start (follow-calc-win-start windows (cdr guess) win)))
1362 (cond ((= win-start start)
1363 (follow-debug-message "exact")
1364 (cdr guess))
1365 ((< win-start start)
1366 (follow-debug-message "above")
1367 (follow-calculate-first-window-start-from-above
1368 windows (cdr guess) win start))
1370 (follow-debug-message "below")
1371 (follow-calculate-first-window-start-from-below
1372 windows (cdr guess) win start)))))))
1375 ;; `exact' is disabled due to XEmacs and fonts of variable
1376 ;; height.
1377 (defun follow-estimate-first-window-start (windows win start)
1378 "Estimate the position of the first window.
1380 Returns (EXACT . POS). If EXACT is non-nil, POS is the starting
1381 position of the first window. Otherwise it is a good guess."
1382 (let ((pred (car (follow-split-followers windows win)))
1383 (exact nil))
1384 (save-excursion
1385 (goto-char start)
1386 ;(setq exact (bolp))
1387 (vertical-motion 0 win)
1388 (while pred
1389 (vertical-motion (- 1 (window-height (car pred))) (car pred))
1390 (if (not (bolp))
1391 (setq exact nil))
1392 (setq pred (cdr pred)))
1393 (cons exact (point)))))
1396 ;; Find the starting point, start at GUESS and search downward.
1397 ;; The returned point is always a point below GUESS.
1399 (defun follow-calculate-first-window-start-from-above
1400 (windows guess win start)
1401 (save-excursion
1402 (let ((done nil)
1403 win-start
1404 res)
1405 (goto-char guess)
1406 (while (not done)
1407 (if (not (= (vertical-motion 1 (car windows)) 1))
1408 ;; Hit bottom! (Can we really do this?)
1409 ;; We'll keep it, since it ensures termination.
1410 (progn
1411 (setq done t)
1412 (setq res (point-max)))
1413 (setq win-start (follow-calc-win-start windows (point) win))
1414 (if (>= win-start start)
1415 (progn
1416 (setq done t)
1417 (setq res (point))))))
1418 res)))
1421 ;; Find the starting point, start at GUESS and search upward. Return
1422 ;; a point on the same line as GUESS, or above.
1424 ;; (Is this ever used? I must make sure it works just in case it is
1425 ;; ever called.)
1427 (defun follow-calculate-first-window-start-from-below
1428 (windows guess &optional win start)
1429 (setq win (or win (selected-window)))
1430 (setq start (or start (window-start win)))
1431 (save-excursion
1432 (let ((done nil)
1433 win-start
1434 res)
1435 ;; Always calculate what happend when no line is displayed in the first
1436 ;; window. (The `previous' res is needed below!)
1437 (goto-char guess)
1438 (vertical-motion 0 (car windows))
1439 (setq res (point))
1440 (while (not done)
1441 (if (not (= (vertical-motion -1 (car windows)) -1))
1442 ;; Hit roof!
1443 (progn
1444 (setq done t)
1445 (setq res (point-min)))
1446 (setq win-start (follow-calc-win-start windows (point) win))
1447 (cond ((= win-start start) ; Perfect match, use this value
1448 (setq done t)
1449 (setq res (point)))
1450 ((< win-start start) ; Walked to far, use preious result
1451 (setq done t))
1452 (t ; Store result for next iteration
1453 (setq res (point))))))
1454 res)))
1456 ;;}}}
1457 ;;{{{ Avoid tail recenter
1459 ;; This sets the window internal flag `force_start'. The effect is that
1460 ;; windows only displaying the tail isn't recentered.
1461 ;; Has to be called before every redisplay... (Great isn't it?)
1463 ;; XEmacs doesn't recenter the tail, GOOD!
1465 ;; A window displaying only the tail, is a windows whose
1466 ;; window-start position is equal to (point-max) of the buffer it
1467 ;; displays.
1469 ;; This function is also added to `post-command-idle-hook', introduced
1470 ;; in Emacs 19.30. This is needed since the vaccine injected by the
1471 ;; call from `post-command-hook' only works until the next redisplay.
1472 ;; It is possible that the functions in the `post-command-idle-hook'
1473 ;; can cause a redisplay, and hence a new vaccine is needed.
1475 ;; Sometimes, calling this function could actually cause a redisplay,
1476 ;; especially if it is placed in the debug filter section. I must
1477 ;; investigate this further...
1479 (defun follow-avoid-tail-recenter (&rest rest)
1480 "Make sure windows displaying the end of a buffer aren't recentered.
1482 This is done by reading and rewriting the start position of
1483 non-first windows in Follow Mode."
1484 (if follow-avoid-tail-recenter-p
1485 (let* ((orig-buffer (current-buffer))
1486 (top (frame-first-window (selected-frame)))
1487 (win top)
1488 (who '()) ; list of (buffer . frame)
1489 start
1490 pair) ; (buffer . frame)
1491 ;; If the only window in the frame is a minibuffer
1492 ;; window, `next-window' will never find it again...
1493 (if (window-minibuffer-p top)
1495 (while ;; look, no body!
1496 (progn
1497 (setq start (window-start win))
1498 (set-buffer (window-buffer win))
1499 (setq pair (cons (window-buffer win) (window-frame win)))
1500 (if (member pair who)
1501 (if (and (boundp 'follow-mode) follow-mode
1502 (eq (point-max) start))
1503 ;; Write the same window start back, but don't
1504 ;; set the NOFORCE flag.
1505 (set-window-start win start))
1506 (setq who (cons pair who)))
1507 (setq win (next-window win 'not t))
1508 (not (eq win top)))) ;; Loop while this is true.
1509 (set-buffer orig-buffer)))))
1511 ;;}}}
1513 ;;}}}
1514 ;;{{{ Post Command Hook
1516 ;;; The magic little box. This function is called after every command.
1518 ;; This is not as complicated as it seems. It is simply a list of common
1519 ;; display situations and the actions to take, plus commands for redrawing
1520 ;; the screen if it should be unaligned.
1522 ;; We divide the check into two parts; whether we are at the end or not.
1523 ;; This is due to the fact that the end can actaually be visible
1524 ;; in several window even though they are aligned.
1526 (defun follow-post-command-hook ()
1527 "Ensure that the windows in Follow mode are adjacent after each command."
1528 (setq follow-inside-post-command-hook t)
1529 (if (or (not (input-pending-p))
1530 ;; Sometimes, in XEmacs, mouse events are not handled
1531 ;; properly by `input-pending-p'. A typical example is
1532 ;; when clicking on a node in `info'.
1533 (and (boundp 'current-mouse-event)
1534 (symbol-value 'current-mouse-event)
1535 (fboundp 'button-event-p)
1536 (funcall (symbol-function 'button-event-p)
1537 (symbol-value 'current-mouse-event))))
1538 ;; Work in the selected window, not in the current buffer.
1539 (let ((orig-buffer (current-buffer))
1540 (win (selected-window)))
1541 (set-buffer (window-buffer win))
1542 (or (and (symbolp this-command)
1543 (get this-command 'follow-mode-use-cache))
1544 (follow-invalidate-cache))
1545 (if (and (boundp 'follow-mode) follow-mode
1546 (not (window-minibuffer-p win)))
1547 ;; The buffer shown in the selected window is in follow
1548 ;; mode, lets find the current state of the display and
1549 ;; cache the result for speed (i.e. `aligned' and `visible'.)
1550 (let* ((windows (inline (follow-all-followers win)))
1551 (dest (point))
1552 (win-start-end (inline
1553 (follow-update-window-start (car windows))
1554 (follow-windows-start-end windows)))
1555 (aligned (follow-windows-aligned-p win-start-end))
1556 (visible (follow-pos-visible dest win win-start-end)))
1557 (if (not (and aligned visible))
1558 (follow-invalidate-cache))
1559 (inline (follow-avoid-tail-recenter))
1560 ;; Select a window to display the point.
1561 (or follow-internal-force-redisplay
1562 (progn
1563 (if (eq dest (point-max))
1564 ;; We're at the end, we have be be careful since
1565 ;; the display can be aligned while `dest' can
1566 ;; be visible in several windows.
1567 (cond
1568 ;; Select the current window, but only when
1569 ;; the display is correct. (When inserting
1570 ;; character in a tail window, the display is
1571 ;; not correct, as they are shown twice.)
1573 ;; Never stick to the current window after a
1574 ;; deletion. The reason is cosmetic, when
1575 ;; typing `DEL' in a window showing only the
1576 ;; end of the file, character are removed
1577 ;; from the window above, which is very
1578 ;; unintuitive.
1579 ((and visible
1580 aligned
1581 (not (memq this-command
1582 '(backward-delete-char
1583 delete-backward-char
1584 backward-delete-char-untabify
1585 kill-region))))
1586 (follow-debug-message "Max: same"))
1587 ;; If the end is visible, and the window
1588 ;; doesn't seems like it just has been moved,
1589 ;; select it.
1590 ((follow-select-if-end-visible win-start-end)
1591 (follow-debug-message "Max: end visible")
1592 (setq visible t)
1593 (setq aligned nil)
1594 (goto-char dest))
1595 ;; Just show the end...
1597 (follow-debug-message "Max: default")
1598 (select-window (car (reverse windows)))
1599 (goto-char dest)
1600 (setq visible nil)
1601 (setq aligned nil)))
1603 ;; We're not at the end, here life is much simpler.
1604 (cond
1605 ;; This is the normal case!
1606 ;; It should be optimized for speed.
1607 ((and visible aligned)
1608 (follow-debug-message "same"))
1609 ;; Pick a position in any window. If the
1610 ;; display is ok, this will pick the `correct'
1611 ;; window. If the display is wierd do this
1612 ;; anyway, this will be the case after a delete
1613 ;; at the beginning of the window.
1614 ((follow-select-if-visible dest win-start-end)
1615 (follow-debug-message "visible")
1616 (setq visible t)
1617 (goto-char dest))
1618 ;; Not visible anywhere else, lets pick this one.
1619 ;; (Is this case used?)
1620 (visible
1621 (follow-debug-message "visible in selected."))
1622 ;; Far out!
1623 ((eq dest (point-min))
1624 (follow-debug-message "min")
1625 (select-window (car windows))
1626 (goto-char dest)
1627 (set-window-start (selected-window) (point-min))
1628 (setq win-start-end (follow-windows-start-end windows))
1629 (follow-invalidate-cache)
1630 (setq visible t)
1631 (setq aligned nil))
1632 ;; If we can position the cursor without moving the first
1633 ;; window, do it. This is the case that catches `RET'
1634 ;; at the bottom of a window.
1635 ((follow-select-if-visible-from-first dest windows)
1636 (follow-debug-message "Below first")
1637 (setq visible t)
1638 (setq aligned t)
1639 (follow-redisplay windows (car windows))
1640 (goto-char dest))
1641 ;; None of the above. For simplicity, we stick to the
1642 ;; selected window.
1644 (follow-debug-message "None")
1645 (setq visible nil)
1646 (setq aligned nil))))
1647 ;; If a new window has been selected, make sure that the
1648 ;; old is not scrolled when the point is outside the
1649 ;; window.
1650 (or (eq win (selected-window))
1651 (let ((p (window-point win)))
1652 (set-window-start win (window-start win) nil)
1653 (set-window-point win p)))))
1654 ;; Make sure the point is visible in the selected window.
1655 ;; (This could lead to a scroll.)
1656 (if (or visible
1657 (follow-pos-visible dest win win-start-end))
1659 (sit-for 0)
1660 (follow-avoid-tail-recenter)
1661 (setq win-start-end (follow-windows-start-end windows))
1662 (follow-invalidate-cache)
1663 (setq aligned nil))
1664 ;; Redraw the windows whenever needed.
1665 (if (or follow-internal-force-redisplay
1666 (not (or aligned
1667 (follow-windows-aligned-p win-start-end)))
1668 (not (inline (follow-point-visible-all-windows-p
1669 win-start-end))))
1670 (progn
1671 (setq follow-internal-force-redisplay nil)
1672 (follow-redisplay windows (selected-window))
1673 (setq win-start-end (follow-windows-start-end windows))
1674 (follow-invalidate-cache)
1675 ;; When the point ends up in another window. This
1676 ;; happends when dest is in the beginning of the
1677 ;; file and the selected window is not the first.
1678 ;; It can also, in rare situations happend when
1679 ;; long lines are used and there is a big
1680 ;; difference between the width of the windows.
1681 ;; (When scrolling one line in a wide window which
1682 ;; will cause a move larger that an entire small
1683 ;; window.)
1684 (if (follow-pos-visible dest win win-start-end)
1686 (follow-select-if-visible dest win-start-end)
1687 (goto-char dest))))
1689 ;; If the region is visible, make it look good when spanning
1690 ;; multiple windows.
1691 (if (or (and (boundp 'mark-active) (symbol-value 'mark-active))
1692 (and (fboundp 'region-active-p)
1693 (funcall (symbol-function 'region-active-p))))
1694 (follow-maximize-region
1695 (selected-window) windows win-start-end))
1697 (inline (follow-avoid-tail-recenter))
1698 ;; DEBUG
1699 ;;(if (not (follow-windows-aligned-p
1700 ;; (follow-windows-start-end windows)))
1701 ;; (message "follow-mode: windows still unaligend!"))
1702 ;; END OF DEBUG
1703 ) ; Matches (let*
1704 ;; Buffer not in follow mode:
1705 ;; We still must update the windows displaying the tail so that
1706 ;; Emacs won't recenter them.
1707 (follow-avoid-tail-recenter))
1708 (set-buffer orig-buffer)))
1709 (setq follow-inside-post-command-hook nil))
1711 ;;}}}
1712 ;;{{{ The region
1714 ;; Tries to make the highlighted area representing the region look
1715 ;; good when spanning several windows.
1717 ;; Not perfect, as the point can't be placed at window end, only at
1718 ;; end-1. This will highlight a little bit in windows above
1719 ;; the current.
1721 (defun follow-maximize-region (win windows win-start-end)
1722 "Make a highlighted region stretching multiple windows look good."
1723 (let* ((all (follow-split-followers windows win))
1724 (pred (car all))
1725 (succ (cdr all))
1726 data)
1727 (while pred
1728 (setq data (assq (car pred) win-start-end))
1729 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1730 (setq pred (cdr pred)))
1731 (while succ
1732 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1733 (setq succ (cdr succ)))))
1735 ;;}}}
1736 ;;{{{ Scroll bar
1738 ;;;; Scroll-bar support code.
1740 ;;; Why is it needed? Well, if the selected window is in follow mode,
1741 ;;; all its follower stick to it blindly. If one of them is scrolled,
1742 ;;; it immediately returns to the original position when the mouse is
1743 ;;; released. If the selected window is not a follower of the dragged
1744 ;;; window the windows will be unaligned.
1746 ;;; The advices doesn't get compiled. Aestetically, this might be a
1747 ;;; problem but in practical life it isn't.
1749 ;;; Discussion: Now when the other windows in the chain follow the
1750 ;;; dragged, should we really select it?
1752 (cond ((fboundp 'scroll-bar-drag)
1754 ;;; Emacs style scrollbars.
1757 ;; Select the dragged window if it is a follower of the
1758 ;; selected window.
1760 ;; Generate advices of the form:
1761 ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
1762 ;; "Adviced by `follow-mode'."
1763 ;; (follow-redraw-after-event (ad-get-arg 0)))
1764 (let ((cmds '(scroll-bar-drag
1765 scroll-bar-drag-1 ; Executed at every move.
1766 scroll-bar-scroll-down
1767 scroll-bar-scroll-up
1768 scroll-bar-set-window-start)))
1769 (while cmds
1770 (eval
1771 `(defadvice ,(intern (symbol-name (car cmds)))
1772 (after
1773 ,(intern (concat "follow-" (symbol-name (car cmds))))
1774 activate)
1775 "Adviced by Follow Mode."
1776 (follow-redraw-after-event (ad-get-arg 0))))
1777 (setq cmds (cdr cmds))))
1780 (defun follow-redraw-after-event (event)
1781 "Adviced by Follow mode."
1782 (condition-case nil
1783 (let* ((orig-win (selected-window))
1784 (win (nth 0 (funcall
1785 (symbol-function 'event-start) event)))
1786 (fmode (assq 'follow-mode
1787 (buffer-local-variables
1788 (window-buffer win)))))
1789 (if (and fmode (cdr fmode))
1790 ;; The selected window is in follow-mode
1791 (progn
1792 ;; Recenter around the dragged window.
1793 (select-window win)
1794 (follow-redisplay)
1795 (select-window orig-win))))
1796 (error nil))))
1799 ((fboundp 'scrollbar-vertical-drag)
1801 ;;; XEmacs style scrollbars.
1804 ;; Advice all scrollbar functions on the form:
1806 ;; (defadvice scrollbar-line-down
1807 ;; (after follow-scrollbar-line-down activate)
1808 ;; (follow-xemacs-scrollbar-support (ad-get-arg 0)))
1810 (let ((cmds '(scrollbar-line-down ; Window
1811 scrollbar-line-up
1812 scrollbar-page-down ; Object
1813 scrollbar-page-up
1814 scrollbar-to-bottom ; Window
1815 scrollbar-to-top
1816 scrollbar-vertical-drag ; Object
1819 (while cmds
1820 (eval
1821 `(defadvice ,(intern (symbol-name (car cmds)))
1822 (after
1823 ,(intern (concat "follow-" (symbol-name (car cmds))))
1824 activate)
1825 "Adviced by `follow-mode'."
1826 (follow-xemacs-scrollbar-support (ad-get-arg 0))))
1827 (setq cmds (cdr cmds))))
1830 (defun follow-xemacs-scrollbar-support (window)
1831 "Redraw windows showing the same buffer as shown in WINDOW.
1832 WINDOW is either the dragged window, or a cons containing the
1833 window as its first element. This is called while the user drags
1834 the scrollbar.
1836 WINDOW can be an object or a window."
1837 (condition-case nil
1838 (progn
1839 (if (consp window)
1840 (setq window (car window)))
1841 (let ((fmode (assq 'follow-mode
1842 (buffer-local-variables
1843 (window-buffer window))))
1844 (orig-win (selected-window)))
1845 (if (and fmode (cdr fmode))
1846 (progn
1847 ;; Recenter around the dragged window.
1848 (select-window window)
1849 (follow-redisplay)
1850 (select-window orig-win)))))
1851 (error nil)))))
1853 ;;}}}
1854 ;;{{{ Process output
1856 ;;; The following sections installs a spy that listens to process
1857 ;;; output and tries to reposition the windows whose buffers are in
1858 ;;; Follow mode. We play safe as much as possible...
1860 ;;; When follow-mode is activated all active processes are
1861 ;;; intercepted. All new processes that change their filter function
1862 ;;; using `set-process-filter' are also intercepted. The reason is
1863 ;;; that a process can cause a redisplay recentering "tail" windows.
1864 ;;; Note that it doesn't hurt to spy on more processes than needed.
1866 ;;; Technically, we set the process filter to `follow-generic-filter'.
1867 ;;; The original filter is stored in `follow-process-filter-alist'.
1868 ;;; Our generic filter calls the original filter, or inserts the
1869 ;;; output into the buffer, if the buffer originally didn't have an
1870 ;;; output filter. It also makes sure that the windows connected to
1871 ;;; the buffer are aligned.
1873 ;;; Discussion: How do we find processes that don't call
1874 ;;; `set-process-filter'? (How often are processes created in a
1875 ;;; buffer after Follow mode are activated?)
1877 ;;; Discussion: Should we also advice `process-filter' to make our
1878 ;;; filter invisible to others?
1880 ;;{{{ Advice for `set-process-filter'
1882 ;; Do not call this with 'follow-generic-filter as the name of the
1883 ;; filter...
1885 (defadvice set-process-filter (before follow-set-process-filter activate)
1886 "Ensure process output will be displayed correctly in Follow Mode buffers.
1888 Follow Mode inserts its own process filter to do its
1889 magic stuff before the real process filter is called."
1890 (if follow-intercept-processes
1891 (progn
1892 (setq follow-process-filter-alist
1893 (delq (assq (ad-get-arg 0) follow-process-filter-alist)
1894 follow-process-filter-alist))
1895 (follow-tidy-process-filter-alist)
1896 (cond ((eq (ad-get-arg 1) t))
1897 ((eq (ad-get-arg 1) nil)
1898 (ad-set-arg 1 'follow-generic-filter))
1900 (setq follow-process-filter-alist
1901 (cons (cons (ad-get-arg 0) (ad-get-arg 1))
1902 follow-process-filter-alist))
1903 (ad-set-arg 1 'follow-generic-filter))))))
1906 (defun follow-call-set-process-filter (proc filter)
1907 "Call original `set-process-filter' without the Follow mode advice."
1908 (ad-disable-advice 'set-process-filter 'before
1909 'follow-set-process-filter)
1910 (ad-activate 'set-process-filter)
1911 (prog1
1912 (set-process-filter proc filter)
1913 (ad-enable-advice 'set-process-filter 'before
1914 'follow-set-process-filter)
1915 (ad-activate 'set-process-filter)))
1918 (defadvice process-filter (after follow-process-filter activate)
1919 "Return the original process filter, not `follow-generic-filter'."
1920 (cond ((eq ad-return-value 'follow-generic-filter)
1921 (setq ad-return-value
1922 (cdr-safe (assq (ad-get-arg 0)
1923 follow-process-filter-alist))))))
1926 (defun follow-call-process-filter (proc)
1927 "Call original `process-filter' without the Follow mode advice."
1928 (ad-disable-advice 'process-filter 'after
1929 'follow-process-filter)
1930 (ad-activate 'process-filter)
1931 (prog1
1932 (process-filter proc)
1933 (ad-enable-advice 'process-filter 'after
1934 'follow-process-filter)
1935 (ad-activate 'process-filter)))
1938 (defun follow-tidy-process-filter-alist ()
1939 "Remove old processes from `follow-process-filter-alist'."
1940 (let ((alist follow-process-filter-alist)
1941 (ps (process-list))
1942 (new ()))
1943 (while alist
1944 (if (and (not (memq (process-status (car (car alist)))
1945 '(exit signal closed nil)))
1946 (memq (car (car alist)) ps))
1947 (setq new (cons (car alist) new)))
1948 (setq alist (cdr alist)))
1949 (setq follow-process-filter-alist new)))
1951 ;;}}}
1952 ;;{{{ Start/stop interception of processes.
1954 ;; Normally, all new processed are intercepted by our `set-process-filter'.
1955 ;; This is needed to intercept old processed that were started before we were
1956 ;; loaded, and processes we have forgotten by calling
1957 ;; `follow-stop-intercept-process-output'.
1959 (defun follow-intercept-process-output ()
1960 "Intercept all active processes.
1962 This is needed so that Follow Mode can track all display events in the
1963 system. (See `follow-mode')"
1964 (interactive)
1965 (let ((list (process-list)))
1966 (while list
1967 (if (eq (process-filter (car list)) 'follow-generic-filter)
1969 ;; The custom `set-process-filter' defined above.
1970 (set-process-filter (car list) (process-filter (car list))))
1971 (setq list (cdr list))))
1972 (setq follow-intercept-processes t))
1975 (defun follow-stop-intercept-process-output ()
1976 "Stop Follow Mode from spying on processes.
1978 All current spypoints are removed and no new will be added.
1980 The effect is that Follow mode won't be able to handle buffers
1981 connected to processes.
1983 The only reason to call this function is if the Follow mode spy filter
1984 would interfere with some other package. If this happens, please
1985 report this using the `follow-submit-feedback' function."
1986 (interactive)
1987 (follow-tidy-process-filter-alist)
1988 (let ((list (process-list)))
1989 (while list
1990 (if (eq (process-filter (car list)) 'follow-generic-filter)
1991 (progn
1992 (follow-call-set-process-filter
1993 (car list)
1994 (cdr-safe (assq (car list) follow-process-filter-alist)))
1995 (setq follow-process-filter-alist
1996 (delq (assq (car list) follow-process-filter-alist)
1997 follow-process-filter-alist))))
1998 (setq list (cdr list))))
1999 (setq follow-intercept-processes nil))
2001 ;;}}}
2002 ;;{{{ The filter
2004 ;;; The following section is a naive method to make buffers with
2005 ;;; process output to work with Follow mode. Whenever the start of the
2006 ;;; window displaying the buffer is moved, we moves it back to its
2007 ;;; original position and try to select a new window. (If we fail,
2008 ;;; the normal redisplay functions of Emacs will scroll it right
2009 ;;; back!)
2011 (defun follow-generic-filter (proc output)
2012 "Process output filter for process connected to buffers in Follow mode."
2013 (let* ((old-buffer (current-buffer))
2014 (orig-win (selected-window))
2015 (buf (process-buffer proc))
2016 (win (and buf (if (eq buf (window-buffer orig-win))
2017 orig-win
2018 (get-buffer-window buf t))))
2019 (return-to-orig-win (and win (not (eq win orig-win))))
2020 (orig-window-start (and win (window-start win))))
2022 ;; If input is pending, the `sit-for' below won't redraw the
2023 ;; display. In that case, calling `follow-avoid-tail-recenter' may
2024 ;; provoke the process hadnling code to sceduling a redisplay.
2025 ;(or (input-pending-p)
2026 ; (follow-avoid-tail-recenter))
2028 ;; Output the `output'.
2029 (let ((filter (cdr-safe (assq proc follow-process-filter-alist))))
2030 (cond
2031 ;; Call the original filter function
2032 (filter
2033 (funcall filter proc output))
2035 ;; No filter, but we've got a buffer. Just output into it.
2036 (buf
2037 (set-buffer buf)
2038 (if (not (marker-buffer (process-mark proc)))
2039 (set-marker (process-mark proc) (point-max)))
2040 (let ((moving (= (point) (process-mark proc)))
2041 deactivate-mark
2042 (inhibit-read-only t))
2043 (save-excursion
2044 (goto-char (process-mark proc))
2045 ;; `insert-before-markers' just in case the users next
2046 ;; command is M-y.
2047 (insert-before-markers output)
2048 (set-marker (process-mark proc) (point)))
2049 (if moving (goto-char (process-mark proc)))))))
2051 ;; If we're in follow mode, do our stuff. Select a new window and
2052 ;; redisplay. (Actually, it is redundant to check `buf', but I
2053 ;; feel it's more correct.)
2054 (if (and buf win (window-live-p win))
2055 (progn
2056 (set-buffer buf)
2057 (if (and (boundp 'follow-mode) follow-mode)
2058 (progn
2059 (select-window win)
2060 (let* ((windows (follow-all-followers win))
2061 (win-start-end (follow-windows-start-end windows))
2062 (new-window-start (window-start win))
2063 (new-window-point (window-point win)))
2064 (cond
2065 ;; The start of the selected window was repositioned.
2066 ;; Try to use the original start position and continue
2067 ;; working with a window to the "right" in the window
2068 ;; chain. This will create the effect that the output
2069 ;; starts in one window and continues into the next.
2071 ;; If the display has changed so much that it is not
2072 ;; possible to keep the original window fixed and still
2073 ;; display the point then we give up and use the new
2074 ;; window start.
2076 ;; This case is typically used when the process filter
2077 ;; tries to reposition the start of the window in order
2078 ;; to view the tail of the output.
2079 ((not (eq orig-window-start new-window-start))
2080 (follow-debug-message "filter: Moved")
2081 (set-window-start win orig-window-start)
2082 (follow-redisplay windows win)
2083 (setq win-start-end (follow-windows-start-end windows))
2084 (follow-select-if-visible new-window-point
2085 win-start-end)
2086 (goto-char new-window-point)
2087 (if (eq win (selected-window))
2088 (set-window-start win new-window-start))
2089 (setq win-start-end (follow-windows-start-end windows)))
2090 ;; Stick to this window, if point is visible in it.
2091 ((pos-visible-in-window-p new-window-point)
2092 (follow-debug-message "filter: Visible in window"))
2093 ;; Avoid redisplaying the first window. If the
2094 ;; point is visible at a window below,
2095 ;; redisplay and select it.
2096 ((follow-select-if-visible-from-first
2097 new-window-point windows)
2098 (follow-debug-message "filter: Seen from first")
2099 (follow-redisplay windows (car windows))
2100 (goto-char new-window-point)
2101 (setq win-start-end
2102 (follow-windows-start-end windows)))
2103 ;; None of the above. We stick to the current window.
2105 (follow-debug-message "filter: nothing")))
2107 ;; Here we have slected a window. Make sure the
2108 ;; windows are aligned and the point is visible
2109 ;; in the selected window.
2110 (if (and (not (follow-pos-visible
2111 (point) (selected-window) win-start-end))
2112 (not return-to-orig-win))
2113 (progn
2114 (sit-for 0)
2115 (setq win-start-end
2116 (follow-windows-start-end windows))))
2118 (if (or follow-internal-force-redisplay
2119 (not (follow-windows-aligned-p win-start-end)))
2120 (follow-redisplay windows)))))))
2122 ;; return to the original window.
2123 (if return-to-orig-win
2124 (select-window orig-win))
2125 ;; Restore the orignal buffer, unless the filter explicitly
2126 ;; changed buffer or killed the old buffer.
2127 (if (and (eq buf (current-buffer))
2128 (buffer-name old-buffer))
2129 (set-buffer old-buffer)))
2131 (follow-invalidate-cache)
2133 ;; Normally, if the display has been changed, it is redrawn. All
2134 ;; windows showing only the end of a buffer is unconditionally
2135 ;; recentered, we can't prevent it by calling
2136 ;; `follow-avoid-tail-recenter'.
2138 ;; By performing a redisplay on our own, Emacs need not perform
2139 ;; the above described redisplay. (However, bu performing it when
2140 ;; there are input available just seems to make things worse.)
2141 (if (and follow-avoid-tail-recenter-p
2142 (not (input-pending-p)))
2143 (sit-for 0)))
2145 ;;}}}
2147 ;;}}}
2148 ;;{{{ Window size change
2150 ;; In Emacs 19.29, the functions in `window-size-change-functions' are
2151 ;; called every time a window in a frame changes size. Most notably, it
2152 ;; is called after the frame has been resized.
2154 ;; We basically call our post-command-hook for every buffer that is
2155 ;; visible in any window in the resized frame, which is in follow-mode.
2157 ;; Since this function can be called indirectly from
2158 ;; `follow-post-command-hook' we have a potential infinite loop. We
2159 ;; handle this problem by simply not doing anything at all in this
2160 ;; situation. The variable `follow-inside-post-command-hook' contains
2161 ;; information about whether the execution actually is inside the
2162 ;; post-command-hook or not.
2164 (if (boundp 'window-size-change-functions)
2165 (add-hook 'window-size-change-functions 'follow-window-size-change))
2168 (defun follow-window-size-change (frame)
2169 "Redraw all windows in FRAME, when in Follow mode."
2170 ;; Below, we call `post-command-hook'. This makes sure that we
2171 ;; doesn't start a mutally recursive endless loop.
2172 (if follow-inside-post-command-hook
2174 (let ((buffers '())
2175 (orig-window (selected-window))
2176 (orig-buffer (current-buffer))
2177 (orig-frame (selected-frame))
2178 windows
2179 buf)
2180 (select-frame frame)
2181 (unwind-protect
2182 (walk-windows
2183 (function
2184 (lambda (win)
2185 (setq buf (window-buffer win))
2186 (if (memq buf buffers)
2188 (set-buffer buf)
2189 (if (and (boundp 'follow-mode)
2190 follow-mode)
2191 (progn
2192 (setq windows (follow-all-followers win))
2193 (if (memq orig-window windows)
2194 (progn
2195 ;; Make sure we're redrawing around the
2196 ;; selected window.
2198 ;; We must be really careful not to do this
2199 ;; when we are (indirectly) called by
2200 ;; `post-command-hook'.
2201 (select-window orig-window)
2202 (follow-post-command-hook)
2203 (setq orig-window (selected-window)))
2204 (follow-redisplay windows win))
2205 (setq buffers (cons buf buffers))))))))
2206 (select-frame orig-frame)
2207 (set-buffer orig-buffer)
2208 (select-window orig-window)))))
2210 ;;}}}
2212 ;;{{{ XEmacs isearch
2214 ;; In XEmacs, isearch often finds matches in other windows than the
2215 ;; currently selected. However, when exiting the old window
2216 ;; configuration is restored, with the exception of the beginning of
2217 ;; the start of the window for the selected window. This is not much
2218 ;; help for us.
2220 ;; We overwrite the stored window configuration with the current,
2221 ;; unless we are in `slow-search-mode', i.e. only a few lines
2222 ;; of text is visible.
2224 (if follow-emacs-version-xemacs-p
2225 (defadvice isearch-done (before follow-isearch-done activate)
2226 (if (and (boundp 'follow-mode)
2227 follow-mode
2228 (boundp 'isearch-window-configuration)
2229 isearch-window-configuration
2230 (boundp 'isearch-slow-terminal-mode)
2231 (not isearch-slow-terminal-mode))
2232 (let ((buf (current-buffer)))
2233 (setq isearch-window-configuration
2234 (current-window-configuration))
2235 (set-buffer buf)))))
2237 ;;}}}
2238 ;;{{{ Tail window handling
2240 ;;; In Emacs (not XEmacs) windows showing nothing are sometimes
2241 ;;; recentered. When in Follow Mode, this is not desireable for
2242 ;;; non-first windows in the window chain. This section tries to
2243 ;;; make the windows stay where they should be.
2245 ;;; If the display is updated, all windows starting at (point-max) are
2246 ;;; going to be recentered at the next redisplay, unless we do a
2247 ;;; read-and-write cycle to update the `force' flag inside the windows.
2249 ;;; In 19.30, a new varible `window-scroll-functions' is called every
2250 ;;; time a window is recentered. It is not perfect for our situation,
2251 ;;; since when it is called for a tail window, it is to late. However,
2252 ;;; if it is called for another window, we can try to update our
2253 ;;; windows.
2255 ;;; By patching `sit-for' we can make sure that to catch all explicit
2256 ;;; updates initiated by lisp programs. Internal calls, on the other
2257 ;;; hand, are not handled.
2259 ;;; Please note that the function `follow-avoid-tail-recenter' is also
2260 ;;; called from other places, e.g. `post-command-hook' and
2261 ;;; `post-command-idle-hook'.
2263 ;; If this function is called it is too late for this window, but
2264 ;; we might save other windows from being recentered.
2266 (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions))
2267 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t))
2270 ;; This prevents all packages that calls `sit-for' directly
2271 ;; to recenter tail windows.
2273 (if follow-avoid-tail-recenter-p
2274 (defadvice sit-for (before follow-sit-for activate)
2275 "Adviced by Follow Mode.
2277 Avoid to recenter windows displaying only the end of a file as when
2278 displaying a short file in two windows, using Follow Mode."
2279 (follow-avoid-tail-recenter)))
2282 ;; Without this advice, `mouse-drag-region' would start to recenter
2283 ;; tail windows.
2285 (if (and follow-avoid-tail-recenter-p
2286 (fboundp 'move-overlay))
2287 (defadvice move-overlay (before follow-move-overlay activate)
2288 "Adviced by Follow Mode.
2289 Don't recenter windows showing only the end of a buffer.
2290 This prevents `mouse-drag-region' from messing things up."
2291 (follow-avoid-tail-recenter)))
2293 ;;}}}
2294 ;;{{{ profile support
2296 ;; The following (non-evaluated) section can be used to
2297 ;; profile this package using `elp'.
2299 ;; Invalid indentation on purpose!
2301 (cond (nil
2302 (setq elp-function-list
2303 '(window-end
2304 vertical-motion
2305 ; sit-for ;; elp can't handle advices...
2306 follow-mode
2307 follow-all-followers
2308 follow-split-followers
2309 follow-redisplay
2310 follow-downward
2311 follow-calculate-first-window-start
2312 follow-estimate-first-window-start
2313 follow-calculate-first-window-start-from-above
2314 follow-calculate-first-window-start-from-below
2315 follow-calc-win-end
2316 follow-calc-win-start
2317 follow-pos-visible
2318 follow-windows-start-end
2319 follow-cache-valid-p
2320 follow-select-if-visible
2321 follow-select-if-visible-from-first
2322 follow-windows-aligned-p
2323 follow-point-visible-all-windows-p
2324 follow-avoid-tail-recenter
2325 follow-update-window-start
2326 follow-post-command-hook
2327 ))))
2329 ;;}}}
2331 ;;{{{ The end
2334 ;;; We're done!
2337 (provide 'follow)
2339 ;;}}}
2341 ;; /------------------------------------------------------------------------\
2342 ;; | "I [..] am rarely happier then when spending an entire day programming |
2343 ;; | my computer to perform automatically a task that it would otherwise |
2344 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
2345 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
2346 ;; | worth the investment of a day's happy activity working out a way to |
2347 ;; | save it". -- Douglas Adams, "Last Chance to See" |
2348 ;; \------------------------------------------------------------------------/
2350 ;;; follow.el ends here