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