follow.el 82.2 KB
Newer Older
Karl Heuer's avatar
Karl Heuer committed
1 2 3 4 5 6 7
;;; follow.el --- Minor mode, Synchronize windows showing the same buffer.

;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.

;; Author: Anders Lindgren <andersl@csd.uu.se>
;; Maintainer: Anders Lindgren <andersl@csd.uu.se>
;; Created: 25 May 1995
8
;; Version: 1.7
Karl Heuer's avatar
Karl Heuer committed
9
;; Keywords: display, window, minor-mode
10
;; Date: 27 May 1997
Karl Heuer's avatar
Karl Heuer committed
11

Richard M. Stallman's avatar
Richard M. Stallman committed
12 13 14
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
Karl Heuer's avatar
Karl Heuer committed
15 16 17 18
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

Richard M. Stallman's avatar
Richard M. Stallman committed
19
;; GNU Emacs is distributed in the hope that it will be useful,
Karl Heuer's avatar
Karl Heuer committed
20 21 22 23 24
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Richard M. Stallman's avatar
Richard M. Stallman committed
25 26 27
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Karl Heuer's avatar
Karl Heuer committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

;;; Commentary:

;;{{{ Documentation

;; `Follow mode' is a minor mode for Emacs 19 and XEmacs which
;; combines windows into one tall virtual window.
;;
;; The feeling of a "virtual window" has been accomplished by the use
;; of two major techniques:
;;
;; * The windows always displays adjacent sections of the buffer. 
;;   This means that whenever one window is moved, all the
;;   others will follow.  (Hence the name Follow Mode.)
;;
;; * Should the point (cursor) end up outside a window, another 
;;   window displaying that point is selected, if possible.  This 
;;   makes it possible to walk between windows using normal cursor 
;;   movement commands.
;;
;; Follow mode comes to its prime when used on a large screen and two
;; side-by-side window are used. The user can, with the help of Follow
;; mode, use two full-height windows as though they would have been
;; 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
Karl Heuer's avatar
Karl Heuer committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
;; mileage may vary).

;; The latest version, and a demonstration, are avaiable at:
;;
;;	ftp://ftp.csd.uu.se/pub/users/andersl/emacs/follow.el
;;	http://www.csd.uu.se/~andersl/follow.shtml

;; `Follow mode' can be used together with Emacs 19 and XEmacs.
;; It has been tested together with Emacs 19.27, 19.28, 19.29,
;; 19.30, XEmacs 19.12, and 19.13.


;; To test this package, make sure `follow' is loaded, or will be
;; autoloaded when activated (see below). Then do the following:
;;
;; * Find your favorite file (preferably a long one.)
;;
;; * Resize Emacs so that it will be wide enough for two full sized
;;   columns.  Delete the other windows and split with the commands
;;   `C-x 1 C-x 3'.
;;
;; * Give the command:
;;	M-x follow-mode <RETURN>
;;
;; * Now the display should look something like (assuming the text "71"
;;   is on line 71):
;;
;;		    +----------+----------+
;;		    |1         |73        |
;;		    |2         |74        |
;;		    |3         |75        |
;;		         ...        ...   
;;		    |71        |143       |
;;		    |72        |144       |
;;		    +----------+----------+
;;
;;   As you can see, the right-hand window starts at line 73, the line
;;   immediately below the end of the left-hand window. As long as
;;   `follow-mode' is active, the two windows will follow eachother!
;;
;; * Play around and enjoy! Scroll one window and watch the other.
;;   Jump to the beginning or end. Press `Cursor down' at the last
;;   line of the left-hand window. Enter new lines into the
;;   text. Enter long lines spanning several lines, or several
;;   windows.
;;
;; * Should you find `Follow' mode annoying, just type
;;	M-x follow-mode <RETURN> 
;;   to turn it off.


;; Installation:
;;
;; To fully install this, add this file to your Emacs Lisp directory and
;; compile it with M-x byte-compile-file.  Then add the following to the
;; appropriate init file (normally your `~/.emacs' file):
;;
;; (autoload 'follow-mode "follow"
;;   "Synchronize windows showing the same buffer, minor mode." t)


;; The command `follow-delete-other-windows-and-split' maximises the
;; visible area of the current buffer.
;;
;; I recommend adding it, and `follow-mode', to hotkeys in the global
;; key map.  To do so, add the following lines (replacing `[f7]' and
;; `[f8]' with your favorite keys) to the init file:
;;
;; (autoload 'follow-mode "follow"
;;   "Synchronize windows showing the same buffer, minor mode." t)
;; (global-set-key [f8] 'follow-mode)
;;
;; (autoload 'follow-delete-other-windows-and-split "follow"
;;   "Delete other windows, split the frame in two, and enter Follow Mode." t)
;; (global-set-key [f7] 'follow-delete-other-windows-and-split)


;; There exists two system variables which controls the appearence of
;; lines which are wider than the window containing them.  The default
132 133
;; is to truncate long lines whenever a window isn't as wide as the
;; frame.
Karl Heuer's avatar
Karl Heuer committed
134 135 136 137 138 139 140 141
;;
;; To make sure lines are never truncated, please place the following
;; lines in your init file:
;;
;; (setq truncate-lines nil)
;; (setq truncate-partial-width-windows nil)


142 143 144 145 146 147 148 149 150
;; Since the display of XEmacs is pixel-oriented, a line could be
;; clipped in half at the bottom of the window.
;;
;; To make XEmacs avoid clipping (normal) lines, please place the
;; following line in your init-file:
;;
;; (setq pixel-vertical-clip-threshold 30)


Karl Heuer's avatar
Karl Heuer committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
;; The correct way to cofigurate Follow mode, or any other mode for
;; that matter, is to create one (or more) function which does
;; whatever you would like to do.  The function is then added to
;; a hook.
;;
;; When `Follow' mode is activated, functions stored in the hook
;; `follow-mode-hook' are called.  When it is deactivated
;; `follow-mode-off-hook' is runed.
;;
;; The keymap `follow-key-map' contains key bindings activated by
;; `follow-mode'.
;;
;; Example:
;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
;;
;; (defun my-follow-mode-hook ()
;;    (define-key follow-mode-map "\C-ca" 'your-favorite-function)
;;    (define-key follow-mode-map "\C-cb" 'another-function))


;; Usage:
;;
;; To activate give the command:  M-x follow-mode
;; and press return. To deactivate, do it again.
;;
;; Some special commands have been developed to make life even easier:
;;	follow-scroll-up			 C-c . C-v
;;		Scroll text in a Follow Mode window chain up.
;;
;;	follow-scroll-down			 C-c . v
;;		Like `follow-scroll-up', but in the other direction.
;;
;;	follow-delete-other-windows-and-split	 C-c . 1
;;		Maximise the visible area of the current buffer,
;;		and enter Follow Mode. 	This is a very convenient
;;		way to start Follow Mode, hence it is recomended
;;		that this command is added to the global keymap.
;;
;;	follow-recenter				 C-c . C-l
;;		Place the point in the center of the middle window,
;;		or a specified number of lines from either top or bottom.
;;
;;	follow-switch-to-buffer			 C-c . b
;;		Switch buffer in all windows displaying the current buffer
;;		in this frame.
;;
;;	follow-switch-to-buffer-all		 C-c . C-b
;;		Switch buffer in all windows in the active frame.
;;
;;	follow-switch-to-current-buffer-all
;;		Show the current buffer in all windows on the current
;;		frame and turn on `follow-mode'.
;;
;;	follow-first-window			 C-c . <
;;		Select the first window in the frame showing the same buffer.
;;
;;	follow-last-window			 C-c . >
;;		Select the last window in the frame showing the same buffer.
;;
;;	follow-next-window			 C-c . n
;;		Select the next window in the frame showing the same buffer.
;;
;;	follow-previous-window			 C-c . p
;;		Select the previous window showing the same buffer.


;; Well, it seems ok, but what if I really want to look at two different
;; positions in the text? Here are two simple methods to use:
;;
;; 1) Use multiple frames; `follow' mode only affects windows displayed
;;    in the same frame. (My apoligies to you who can't use frames.)
;;
;; 2) Bind `follow-mode' to key so you can turn it off whenever
;;    you want to view two locations. Of course, `follow' mode can
;;    be reactivated by hitting the same key again.
;;
;;    Example from my ~/.emacs:
;;	(global-set-key [f8] 'follow-mode)


;; Implementation: 
;;
;; In an ideal world, follow mode would have been implemented in the
;; kernal of the display routines, making sure that the windows (in
;; follow mode) ALWAYS are aligned. On planet earth, however, we must
;; accept a solution where we ALMOST ALWAYS can make sure that the
;; windows are aligned.
;;
;; Follow mode does this in three places:
;; 1) After each user command.
;; 2) After a process output has been perfomed.
;; 3) When a scrollbar has been moved.
;;
;; This will cover most situations. (Let me know if there are other
;; situations which should be covered.)
;;
;; However, only the selected window is checked, for the reason of
;; efficiency and code complexity. (i.e. it is possible to make a
;; non-selected windows unaligned. It will, however, pop right back
;; when it is selected.)

;;}}}
;;{{{ Change Log

;;; Change log:
;;	25-May-95 andersl  * File created.
;;	26-May-95 andersl  * It works!
;;	27-May-95 andersl  * Avoids hitting the head in the roof.
;;		           * follow-scroll-up, -scroll-down, and -recenter.
;;		           * V0.1 Sent to Ohio.
;;	28-May-95 andersl  * Scroll-bar support added.
;;	30-May-95 andersl  * Code adopted to standard style.
;;			   * Minor mode keymap.
;;	 2-Jun-95 andersl  * Processor output.
;;	 3-Jun-95 andersl  * V0.4
;;	 5-Jun-95 andersl  * V0.5. Copyright notice corrected.
;;			     (The old one stated that I had copyright, but
;;			     that Emacs could be freely distributed ;-)  )
;;	 6-Jun-95 andersl  * Lucid support added. (no longer valid.)
;;	 7-Jun-95 andersl  * Menu bar added.
;;			   * Bug fix, (at-window 0 0) => (frame-first-window)
;;	15-Jun-95 andersl  * 0.8 Major rework. looong lines and outline mode.
;;	18-Jun-95 andersl  * 0.9 Allow a tail window to be selected, but pick
;;			     a better one when edited.
;;	26-Jun-95 andersl  * Inlineing.
;;      02-Jul-95 andersl  * compute-motion imitated with a ugly workaround,
;;			     Works with XEmacs again!
;;	15-Jul-95 andersl  * find-file hook.
;;                         * submit-feedback.
;;                         * Survives major mode changes.
;;                         * Region spanning multiple windows looks 
;;			       resonabely good.
;;	19-Jul-95 andersl  * New process-filter handling.
;;	 1-Aug-95 andersl  * XEmacs scrollbar support.
;;			   * Emacs 19 `window-size-change' support.
;;			   * `save-window-excursion' removed, it triggered
;;			     a redraw!
;;	 5-Aug-95 andersl  * `follow-switch-to-current-buffer-all' added.
;;	16-Nov-95 andersl  * V1.0 released!
;;	17-Nov-95 andersl  * Byte compiler silencer for XEmacs broken.
;;			   * fkey-end-of-buffer treated the same way 
;;			     end-of-buffer is.
;;			   * follow-mode-off-hook added. 
;;			     (Suggested by David Hughes, thanks!)
;;	20-Nov-95 andersl  * Bug in menu code corrected.
;;			     (Reported by Robert E. Brown, thanks!)
;;	 5-Dec-95 andersl  * `follow-avoid-tail-recenter' added to the
;;			     post-command-idle-hook to avoid recentering
;;			     caused by `paren' et. al.
;;	 7-Dec-95 andersl  * `follow-avoid-tail-recenter' called by
;;			     `window-scroll-functions'.
;;	18-Dec-95 andersl  * All processes intercepted.
;;	20-Dec-95 andersl  * `follow-recenter' accepts arguments.
;;                         * `move-overlay' advices, drag-region works.
;;	 2-Jan-96 andersl  * XEmacs: isearch fixed.
;;                         * `follow-calc-win-end' created.
;;       8-Jan-96 andersl  * XEmacs: `window-end' with `guarantee'
;;                           argument used in `follow-calc-win-end'.
;;       9-Jan-96 andersl  * `follow-end-of-buffer' added. 
;;                           Code in post hook removed.
;;                         * XEmacs: Post hook is always executed
;;			     after a mouse button event.
313
;;      22-Jan-96 andersl  * 1.5 released.
Karl Heuer's avatar
Karl Heuer committed
314 315 316 317 318 319 320 321
;;

;;}}}
;;{{{ LCD Entry

;;; LCD Archive Entry:
;; follow|Anders Lindgren|andersl@csd.uu.se|
;; Combines windows into tall virtual window, minor mode.
322
;; 20-Feb-1996|1.6|~/modes/follow.el.Z|
Karl Heuer's avatar
Karl Heuer committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

;;}}}

;;; Code:

;;{{{ Preliminaries

;; Make the compiler shut up!
;; There are two strategies:
;; 1) Shut warnings off completely.
;; 2) Handle each warning separately.
;;
;; Since I would like to see real errors, I've selected the latter
;; method.
;;
;; The problem with undefined variables and functions has been solved
;; by using `set', `symbol-value' and `symbol-function' rather than
;; `setq' and direct references to variables and functions.
;;
;; For example:
;;	(if (boundp 'foo)   ... (symbol-value 'foo) )
;;	(set 'foo ...)   <-- XEmacs doesn't fall for this one.
;;	(funcall (symbol-function 'set) 'bar ...)
;;
;; Note: When this file is interpreted, `eval-when-compile' is
;; evaluted (really smart...)  Since it doesn't hurt to evaluate it,
;; but it is a bit annoying, we test if the byte-compiler has been
;; loaded.  This can, of course, lead to some occasional unintended
;; evaluation...
;;
;; Should someone come up with a better solution, please let me
;; know.

(eval-when-compile
  (if (or (featurep 'bytecomp)
	  (featurep 'byte-compile))
      (cond ((string-match "XEmacs" emacs-version)
	     ;; Make XEmacs shut up!  I'm using standard Emacs
	     ;; functions, they are NOT obsolete!
	     (if (eq (get 'force-mode-line-update 'byte-compile)
		     'byte-compile-obsolete)
		 (put 'force-mode-line-update 'byte-compile 'nil))
	     (if (eq (get 'frame-first-window 'byte-compile)
		     'byte-compile-obsolete)
		 (put 'frame-first-window 'byte-compile 'nil))))))

;;}}}
;;{{{ Variables

(defvar follow-mode nil
  "Variable indicating if Follow mode is active.")

(defvar follow-mode-hook nil
  "*Hooks to run when follow-mode is turned on.")

(defvar follow-mode-off-hook nil
  "*Hooks to run when follow-mode is turned off.")

381
(defvar follow-mode-version "follow.el (Release 1.6)"
Karl Heuer's avatar
Karl Heuer committed
382 383 384
  "The current version of Follow mode.")

(defvar follow-mode-map nil
385
  "*Minor mode keymap for Follow mode.")
Karl Heuer's avatar
Karl Heuer committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

(defvar follow-mode-line-text " Follow"
  "*Text shown in the mode line when Follow mode is active.  
Defaults to \" Follow\".  Examples of other values
are \" Fw\", or simply \"\".")

(defvar follow-auto nil
  "*Non-nil activates Follow mode whenever a file is loaded.")

(defvar follow-mode-prefix "\C-c."
  "*Prefix key to use for follow commands in Follow mode.
The value of this variable is checked as part of loading Follow mode.
After that, changing the prefix key requires manipulating keymaps.")

(defvar follow-intercept-processes t
  "*When non-nil, Follow Mode will monitor process output.")

(defvar follow-emacs-version-xemacs-p 
  (string-match "XEmacs" emacs-version)
  "Non-nil when running under XEmacs.")

(defvar follow-avoid-tail-recenter-p 
  (not follow-emacs-version-xemacs-p)
  "*When non-nil, patch emacs so that tail windows won't be recentered.

A \"tail window\" is a window which displays only the end of 
the buffer.  Normally it is practical for the user that empty
windows are recentered automatically.  However, when using 
Follow Mode it breaks the display when the end is displayed 
in a window \"above\" the last window.  This is for 
416
example the case when displaying a short page in info.
Karl Heuer's avatar
Karl Heuer committed
417 418 419 420 421 422 423 424 425

Must be set before Follow Mode is loaded.

Please note that it is not possible to fully prevent Emacs from
recentering empty windows.  Please report if you find a repeatable
situation in which Emacs recenters empty windows.

XEmacs, as of 19.12, does not recenter windows, good!")

426 427 428 429 430 431 432 433 434 435 436 437 438
(defvar follow-cache-command-list
  '(next-line previous-line forward-char backward-char)
  "List of commands which don't require recalculation.

In order to be able to use the cache, a command should not change the
contents of the buffer, nor should it change selected window or current
buffer.

The commands in this list are checked at load time.

To mark other commands as suitable for caching, set the symbol
property `follow-mode-use-cache' to non-nil.")

Karl Heuer's avatar
Karl Heuer committed
439 440 441 442
(defvar follow-debug nil
  "*Non-nil when debugging Follow mode.")


443
;; Internal variables:
Karl Heuer's avatar
Karl Heuer committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

(defvar follow-internal-force-redisplay nil
  "True when Follow mode should redisplay the windows.")

(defvar follow-process-filter-alist '()
  "The original filters for processes intercepted by Follow mode.")

(defvar follow-active-menu nil
  "The menu visible when Follow mode is active.")

(defvar follow-deactive-menu nil
  "The menu visible when Follow mode is deactivated.")

(defvar follow-inside-post-command-hook nil
  "Non-nil when inside Follow modes `post-command-hook'. 
Used by `follow-window-size-change'.")

461 462 463
(defvar follow-windows-start-end-cache nil
  "Cache used by `follow-window-start-end'.")

Karl Heuer's avatar
Karl Heuer committed
464 465 466 467 468 469
;;}}}
;;{{{ Bug report

(eval-when-compile (require 'reporter))

(defun follow-submit-feedback ()
470
  "Submit feedback on Follow mode to the author: andersl@csd.uu.se"
Karl Heuer's avatar
Karl Heuer committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  (interactive)
  (require 'reporter)
  (and (y-or-n-p "Do you really want to submit a report on Follow mode? ")
       (reporter-submit-bug-report
	"Anders Lindgren <andersl@csd.uu.se>"
	follow-mode-version
	'(post-command-hook 
	  post-command-idle-hook
	  pre-command-hook
	  window-size-change-functions
	  window-scroll-functions
	  follow-mode-hook
	  follow-mode-off-hook
	  follow-auto 
	  follow-intercept-processes
	  follow-avoid-tail-recenter-p
	  follow-process-filter-alist)
	nil
	nil
	(concat 
	 "Hi Anders!\n\n"
	 "(I have read the section on how to report bugs in the "
	 "Emacs manual.)\n\n"
	 "Even though I know you are busy, I thought you might "
	 "want to know...\n\n"))))

;;}}}
;;{{{ Debug messages

;; This inline function must be as small as possible!
;; Maybe we should define a macro which expands to nil if
;; the varible is not set.

(defsubst follow-debug-message (&rest args)
  "Like message, but only active when `follow-debug' is non-nil."
  (if (and (boundp 'follow-debug) follow-debug)
      (apply 'message args)))

;;}}}

;;{{{ Keymap/Menu

;;; Define keys for the follow-mode minor mode map and replace some
;;; functions in the global map.  All `follow' mode special functions
;;; can be found on (the somewhat cumbersome) "C-c . <key>"
;;; (Control-C dot <key>). (As of Emacs 19.29 the keys
;;; C-c <punctuation character> are reserved for minor modes.)
;;;
;;; To change the prefix, redefine `follow-mode-prefix' before
;;; `follow' is loaded, or see the section on `follow-mode-hook'
;;; above for an example of how to bind the keys the way you like.
;;;
;;; Please note that the keymap is defined the first time this file is
;;; loaded.  Also note that the only legal way to manipulate the
;;; keymap is to use `define-key'.  Don't change it using `setq' or
;;; similar!


(if follow-mode-map
    nil
  (setq follow-mode-map (make-sparse-keymap))
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-v"	'follow-scroll-up)
    (define-key map "\M-v"	'follow-scroll-down)
    (define-key map "v"		'follow-scroll-down)
    (define-key map "1"		'follow-delete-other-windows-and-split)
    (define-key map "b"		'follow-switch-to-buffer)
    (define-key map "\C-b"	'follow-switch-to-buffer-all)
    (define-key map "\C-l"	'follow-recenter)
    (define-key map "<"		'follow-first-window)
    (define-key map ">"		'follow-last-window)
    (define-key map "n"		'follow-next-window)
    (define-key map "p"		'follow-previous-window)

    (define-key follow-mode-map follow-mode-prefix map)

    ;; Replace the standard `end-of-buffer', when in Follow Mode.  (I
    ;; don't see the point in trying to replace every function which
    ;; could be enhanced in Follow mode.  End-of-buffer is a special
    ;; case since it is very simple to define and it greatly enhances
    ;; the look and feel of Follow mode.)
    ;;
    ;; (The function `substitute-key-definition' does not work
    ;; in all versions of Emacs.)
    (mapcar
     (function 
      (lambda (pair)
	(let ((old (car pair))
	      (new (cdr pair)))
	  (mapcar (function (lambda (key) 
			      (define-key follow-mode-map key new)))
		  (where-is-internal old global-map)))))
     '((end-of-buffer      . follow-end-of-buffer)
       (fkey-end-of-buffer . follow-end-of-buffer)))

    ;;;
    ;;; The menu.
    ;;;

    (if (not follow-emacs-version-xemacs-p)

	;;
	;; Emacs 19
	;;
	(let ((menumap (funcall (symbol-function 'make-sparse-keymap)	
			"Follow"))
	      (count 0)
	      id)
	  (mapcar 
	   (function 
	    (lambda (item)
	      (setq id 
		    (or (cdr item)
			(progn
			  (setq count (+ count 1))
			  (intern (format "separator-%d" count)))))
	      (define-key menumap (vector id) item)
	      (or (eq id 'follow-mode)
		  (put id 'menu-enable 'follow-mode))))
	   ;; In reverse order:
	   '(("Toggle Follow mode" . follow-mode)
	     ("--")
	     ("Recenter"           . follow-recenter)
	     ("--")
	     ("Previous Window"    . follow-previous-window)
	     ("Next Windows"       . follow-next-window)
	     ("Last Window"        . follow-last-window)
	     ("First Window"       . follow-first-window)
	     ("--")
	     ("Switch To Buffer (all windows)"
	                           . follow-switch-to-buffer-all)
	     ("Switch To Buffer"   . follow-switch-to-buffer)
	     ("--")
	     ("Delete Other Windows and Split"
	                           . follow-delete-other-windows-and-split)
	     ("--")
	     ("Scroll Down"        . follow-scroll-down)
	     ("Scroll Up"          . follow-scroll-up)))
	  
	  ;; If there is a `tools' meny, we use it. However, we can't add a
	  ;; minor-mode specific item to it (it's broken), so we make the
	  ;; contents ghosted when not in use, and add ourselves to the
	  ;; global map.  If no `tools' menu is present, just make a
	  ;; top-level menu visible when the mode is activated.
	  
	  (let ((tools-map (lookup-key (current-global-map) [menu-bar tools]))
		(last nil))
	    (if (sequencep tools-map)
		(progn
		  ;; Find the last entry in the menu and store it in `last'.
		  (mapcar (function
			   (lambda (x) 
			     (setq last (or (cdr-safe 
					     (cdr-safe 
					      (cdr-safe x))) 
					    last))))
			  tools-map)
		  (if last
		      (progn
			(funcall (symbol-function 'define-key-after)
				  tools-map [separator-follow] '("--") last)
			(funcall (symbol-function 'define-key-after)
				 tools-map [follow] (cons "Follow" menumap)
				 'separator-follow))
		    ;; Didn't find the last item, Adding to the top of
		    ;; tools.  (This will probably never happend...)
		    (define-key (current-global-map) [menu-bar tools follow]
		      (cons "Follow" menumap))))
	      ;; No tools menu, add "Follow" to the menubar.
	      (define-key follow-mode-map [menu-bar follow]
		(cons "Follow" menumap)))))

      ;;
      ;; XEmacs.
      ;;

      ;; place the menu in the `Tools' menu.
      (let ((menu '("Follow"
		    :filter follow-menu-filter
		    ["Scroll Up" follow-scroll-up t]
		    ["Scroll Down" follow-scroll-down t]
		    ["Delete Other Windows and Split"
		     follow-delete-other-windows-and-split t]
		    ["Switch To Buffer" follow-switch-to-buffer t]
		    ["Switch To Buffer (all windows)" 
		     follow-switch-to-buffer-all t]
		    ["First Window" follow-first-window t]
		    ["Last Window" follow-last-window t]
		    ["Next Windows" follow-next-window t]
		    ["Previous Window" follow-previous-window t]
		    ["Recenter" follow-recenter t]
		    ["Deactivate" follow-mode t])))

	;; Why not just `(set-buffer-menubar current-menubar)'?  The
	;; question is a very good question.  The reason is that under
	;; Emacs 19, neither `set-buffer-menubar' nor
	;; `current-menubar' is defined, hence the byte-compiler will
	;; warn.
	(funcall (symbol-function 'set-buffer-menubar)
		 (symbol-value 'current-menubar))
	(funcall (symbol-function 'add-submenu) '("Tools") menu))

      ;; When the mode is not activated, only one item is visible:
      ;; "Activate".
      (defun follow-menu-filter (menu)
	(if follow-mode
	    menu
	  '(["Activate          " follow-mode t]))))))


;;; Register the follow mode keymap.
(or (assq 'follow-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'follow-mode follow-mode-map) minor-mode-map-alist)))

686 687 688 689 690 691 692 693
;;}}}
;;{{{ Cache

(let ((cmds follow-cache-command-list))
  (while cmds
    (put (car cmds) 'follow-mode-use-cache t)
    (setq cmds (cdr cmds))))

Karl Heuer's avatar
Karl Heuer committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
;;}}}

;;{{{ The mode

;;;###autoload
(defun turn-on-follow-mode ()
  "Turn on Follow mode. Please see the function `follow-mode'."
  (interactive)
  (follow-mode 1))


;;;###autoload
(defun turn-off-follow-mode ()
  "Turn off Follow mode. Please see the function `follow-mode'."
  (interactive)
  (follow-mode -1))


;;;###autoload
(defun follow-mode (arg) 
  "Minor mode which combines windows into one tall virtual window.

The feeling of a \"virtual window\" has been accomplished by the use
of two major techniques:

* The windows always displays adjacent sections of the buffer. 
  This means that whenever one window is moved, all the
  others will follow.  (Hence the name Follow Mode.)

* Should the point (cursor) end up outside a window, another 
  window displaying that point is selected, if possible.  This 
  makes it possible to walk between windows using normal cursor 
  movement commands.

Follow mode comes to its prime when used on a large screen and two
side-by-side window are used. The user can, with the help of Follow
mode, use two full-height windows as though they would have been
one. Imagine yourself editing a large function, or section of text,
732
and being able to use 144 lines instead of the normal 72... (your
Karl Heuer's avatar
Karl Heuer committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
mileage may vary).

To split one large window into two side-by-side windows, the commands
`\\[split-window-horizontally]' or \
`M-x follow-delete-other-windows-and-split' can be used.

Only windows displayed in the same frame follow each-other.

If the variable `follow-intercept-processes' is non-nil, Follow mode
will listen to the output of processes and redisplay accordingly.
\(This is the default.)

When Follow mode is switched on, the hook `follow-mode-hook'
is called.  When turned off, `follow-mode-off-hook' is called.

Keys specific to Follow mode:
\\{follow-mode-map}"
  (interactive "P")
  (make-local-variable 'follow-mode)
  (put 'follow-mode 'permanent-local t)
  (let ((follow-mode-orig follow-mode))
    (setq follow-mode
	  (if (null arg)
	      (not follow-mode)
	    (> (prefix-numeric-value arg) 0)))
    (if (and follow-mode follow-intercept-processes)
	(follow-intercept-process-output))
    (cond ((and follow-mode (not follow-mode-orig)) ; On
	   ;; XEmacs: If this is non-nil, the window will scroll before
	   ;; the point will have a chance to get into the next window.
	   (if (boundp 'scroll-on-clipped-lines)
	       (set 'scroll-on-clipped-lines nil))
	   (force-mode-line-update)
	   (add-hook 'post-command-hook 'follow-post-command-hook t)
	   (if (boundp 'post-command-idle-hook)
	       (add-hook 'post-command-idle-hook 
			 'follow-avoid-tail-recenter t))
	   (run-hooks 'follow-mode-hook))

	  ((and (not follow-mode) follow-mode-orig) ; Off
	   (force-mode-line-update)
	   (run-hooks 'follow-mode-off-hook)))))


;; Register follow-mode as a minor mode.

779 780 781 782 783 784 785
(if (fboundp 'add-minor-mode)
    ;; XEmacs
    (funcall (symbol-function 'add-minor-mode)
	     'follow-mode 'follow-mode-line-text)
  (or (assq 'follow-mode minor-mode-alist)
      (setq minor-mode-alist
	    (cons '(follow-mode follow-mode-line-text) minor-mode-alist))))
Karl Heuer's avatar
Karl Heuer committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

;;}}}
;;{{{ Find file hook

;; This will start follow-mode whenever a new file is loaded, if
;; the variable `follow-auto' is non-nil.

(add-hook 'find-file-hooks 'follow-find-file-hook t)

(defun follow-find-file-hook ()
  "Find-file hook for Follow Mode.  See the variable `follow-auto'."
  (if follow-auto (follow-mode t)))

;;}}}

;;{{{ User functions

;;;
;;; User functions usable when in Follow mode.
;;;

;;{{{ Scroll

;; `scroll-up' and `-down', but for windows in Follow Mode.
;;
;; Almost like the real thing, excpet when the cursor ends up outside
;; the top or bottom...  In our case however, we end up outside the
;; window and hence we are recenterd.  Should we let `recenter' handle
;; the point position we would never leave the selected window.  To do
;; it ourselves we would need to do our own redisplay, which is easier
;; said than done.  (Why didn't I do a real display abstraction from
;; the beginning?)
;;
;; We must sometimes set `follow-internal-force-redisplay', otherwise
;; our post-command-hook will move our windows back into the old
;; position...  (This would also be corrected if we would have had a
;; good redisplay abstraction.)

(defun follow-scroll-up (&optional arg)
  "Scroll text in a Follow Mode window chain up.

If called with no ARG, the `next-screen-context-lines' last lines of
the bottom window in the chain will be visible in the top window.

If called with an argument, scroll ARG lines up.
Negative ARG means scroll downward.

Works like `scroll-up' when not in Follow Mode."
  (interactive "P")
  (cond ((not (and (boundp 'follow-mode) follow-mode))
	 (scroll-up arg))
	(arg
	 (save-excursion (scroll-up arg))
	 (setq follow-internal-force-redisplay t))
	(t
	 (let* ((windows (follow-all-followers))
		(end (window-end (car (reverse windows)))))
	   (if (eq end (point-max))
	       (signal 'end-of-buffer nil)
	     (select-window (car windows))
	     (goto-char end)
	     (vertical-motion (- next-screen-context-lines))
	     (set-window-start (car windows) (point)))))))


(defun follow-scroll-down (&optional arg)
  "Scroll text in a Follow Mode window chain down.

If called with no ARG, the `next-screen-context-lines' top lines of
the top window in the chain will be visible in the bottom window.

If called with an argument, scroll ARG lines down.
Negative ARG means scroll upward.

Works like `scroll-up' when not in Follow Mode."
  (interactive "P")
  (cond ((not (and (boundp 'follow-mode) follow-mode))
	 (scroll-up arg))
	(arg
	 (save-excursion (scroll-down arg)))
	(t
	 (let* ((windows (follow-all-followers))
		(win (car (reverse windows)))
		(start (window-start (car windows))))
	   (if (eq start (point-min))
	       (signal 'beginning-of-buffer nil)
	     (select-window win)
	     (goto-char start)
	     (vertical-motion (- (- (window-height win) 
				    1 
				    next-screen-context-lines)))
	     (set-window-start win (point))
	     (goto-char start)
	     (vertical-motion (- next-screen-context-lines 1))
	     (setq follow-internal-force-redisplay t))))))

;;}}}
;;{{{ Buffer

;;;###autoload
(defun follow-delete-other-windows-and-split (&optional arg)
  "Create two side by side windows and enter Follow Mode.

Execute this command to display as much as possible of the text
in the selected window.  All other windows, in the current 
frame, are deleted and the selected window is split in two
side-by-side windows. Follow Mode is activated, hence the 
two windows always will display two successive pages.
\(If one window is moved, the other one will follow.)

If ARG is positive, the leftmost window is selected.  If it negative,
the rightmost is selected.  If ARG is nil, the leftmost window is
selected if the original window is the first one in the frame.

To bind this command to a hotkey, place the following line
in your `~/.emacs' file, replacing [f7] by your favourite key:
    (global-set-key [f7] 'follow-delete-other-windows-and-split)"
  (interactive "P")
  (let ((other (or (and (null arg) 
			(not (eq (selected-window)
				 (frame-first-window (selected-frame)))))
		   (and arg
			(< (prefix-numeric-value arg) 0))))
	(start (window-start)))
    (delete-other-windows)
    (split-window-horizontally)
    (if other 
	(progn
	  (other-window 1)
	  (set-window-start (selected-window) start)
	  (setq follow-internal-force-redisplay t)))
    (follow-mode 1)))

(defun follow-switch-to-buffer (buffer)
  "Show BUFFER in all windows in the current Follow Mode window chain."
  (interactive "BSwitch to Buffer: ")
  (let ((orig-window (selected-window))
	(windows (follow-all-followers)))
    (while windows
      (select-window (car windows))
      (switch-to-buffer buffer)
      (setq windows (cdr windows)))
    (select-window orig-window)))


(defun follow-switch-to-buffer-all (&optional buffer)
  "Show BUFFER in all windows on this frame.
Defaults to current buffer."
  (interactive (list (read-buffer "Switch to Buffer: " 
				  (current-buffer))))
  (or buffer (setq buffer (current-buffer)))
  (let ((orig-window (selected-window)))
    (walk-windows 
     (function 
      (lambda (win)
	(select-window win)
	(switch-to-buffer buffer))))
    (select-window orig-window)
    (follow-redisplay)))


(defun follow-switch-to-current-buffer-all ()
  "Show current buffer in all windows on this frame, and enter Follow Mode.

To bind this command to a hotkey place the following line 
in your `~/.emacs' file:
	(global-set-key [f7] 'follow-switch-to-current-buffer-all)"
  (interactive)
  (or (and (boundp 'follow-mode) follow-mode)
      (follow-mode 1))
  (follow-switch-to-buffer-all))

;;}}}
;;{{{ Movement

;; Note, these functions are not very useful, atleast not unless you
;; rebind the rather cumbersome key sequence `C-c . p'.

(defun follow-next-window ()
  "Select the next window showing the same buffer."
  (interactive)
  (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
    (if succ
	(select-window (car succ))
      (error "%s" "No more windows"))))


(defun follow-previous-window ()
  "Select the previous window showing the same buffer."
  (interactive)
  (let ((pred (car (follow-split-followers (follow-all-followers)))))
    (if pred
	(select-window (car pred))
      (error "%s" "No more windows"))))


(defun follow-first-window ()
  "Select the first window in the frame showing the same buffer."
  (interactive)
  (select-window (car (follow-all-followers))))


(defun follow-last-window ()
  "Select the last window in the frame showing the same buffer."
  (interactive)
  (select-window (car (reverse (follow-all-followers)))))

;;}}}
;;{{{ Redraw

(defun follow-recenter (&optional arg)
  "Recenter the middle window around the point,
and rearrange all other windows around the middle window.

With a positive argument, place the current line ARG lines
from the top.  With a negative, place it -ARG lines from the
bottom."
  (interactive "P")
  (if arg
      (let ((p (point))
	    (arg (prefix-numeric-value arg)))
	(if (>= arg 0)
	    ;; Recenter relative to the top.
	    (progn
	      (follow-first-window)
	      (goto-char p)
	      (recenter arg))
	  ;; Recenter relative to the bottom.
	  (follow-last-window)
	  (goto-char p)
	  (recenter arg)
	  ;; Otherwise, our post-command-hook will move the window
	  ;; right back.
	  (setq follow-internal-force-redisplay t)))
    ;; Recenter in the middle.
    (let* ((dest (point))
	   (windows (follow-all-followers))
	   (win (nth (/ (- (length windows) 1) 2) windows)))
      (select-window win)
      (goto-char dest)
      (recenter)
      ;;(setq follow-internal-force-redisplay t)
      )))


(defun follow-redraw ()
  "Arrange windows displaying the same buffer in successor order.
This function can be called even if the buffer is not in Follow mode.

Hopefully, there should be no reason to call this function when in
Follow mode since the windows should always be aligned."
  (interactive)
  (sit-for 0)
  (follow-redisplay))

;;}}}
;;{{{ End of buffer

(defun follow-end-of-buffer (&optional arg)
  "Move point to the end of the buffer. Follow Mode style.

If the end is not visible, it will be displayed in the last possible
window in the Follow Mode window chain.

The mark is left at the previous position. With arg N, put point N/10
of the way from the true end."
  (interactive "P")
  (let ((followers (follow-all-followers))
	(pos (point)))
    (cond (arg
	   (select-window (car (reverse followers))))
	  ((follow-select-if-end-visible 
	    (follow-windows-start-end followers)))
	  (t
	   (select-window (car (reverse followers)))))
    (goto-char pos)
    (end-of-buffer arg)))

;;}}}

;;}}}

;;{{{ Display

;;;; The display routines

;;{{{ Information gathering functions

(defun follow-all-followers (&optional testwin)
  "Return all windows displaying the same buffer as the TESTWIN.
The list contains only windows displayed in the same frame as TESTWIN.
If TESTWIN is nil the selected window is used."
  (or (and testwin (window-live-p testwin))
      (setq testwin (selected-window)))
  (let* ((top (frame-first-window (window-frame testwin)))
	 (win top)
	 (done nil)
	 (windows '())
	 (buffer (window-buffer testwin)))
    (while (and (not done) win)
      (if (eq (window-buffer win) buffer)
	  (setq windows (cons win windows)))
      (setq win (next-window win 'not))
      (if (eq win top)
	  (setq done t)))
    (nreverse windows)))


(defun follow-split-followers (windows &optional win)
  "Split the WINDOWS into the sets: predecessors and successors.
Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting 
from the selected window."
  (or win 
      (setq win (selected-window)))
  (let ((pred '()))
    (while (not (eq (car windows) win))
      (setq pred (cons (car windows) pred))
      (setq windows (cdr windows)))
    (cons pred (cdr windows))))


;; Try to optimize this function for speed!

(defun follow-calc-win-end (&optional win)
  "Calculate the presumed window end for WIN.

Actually, the position returned is the start of the next
window, normally is the end plus one.

If WIN is nil, the selected window is used.

Returns (end-pos end-of-buffer-p)"
  (if follow-emacs-version-xemacs-p
      ;; XEmacs can calculate the end of the window by using
      ;; the 'guarantee options. GOOD!
      (let ((end (window-end win t)))
	(if (= end (funcall (symbol-function 'point-max)
			    (window-buffer win)))
	    (list end t)
	  (list (+ end 1) nil))) 
    ;; Emacs 19: We have to calculate the end by ourselves.
    ;; This code works on both XEmacs and Emacs 19, but now
    ;; that XEmacs has got custom-written code, this could
    ;; be optimized for Emacs 19.
    (let ((orig-win (and win (selected-window)))
	  height
	  buffer-end-p)
      (if win (select-window win))
      (prog1
	  (save-excursion
	    (goto-char (window-start))
	    (setq height (- (window-height) 1))
	    (setq buffer-end-p
		  (if (bolp)
		      (not (= height (vertical-motion height)))
		    (save-restriction
		      ;; Fix a mis-feature in `vertical-motion':
		      ;; The start of the window is assumed to
		      ;; coinside with the start of a line.
		      (narrow-to-region (point) (point-max))
		      (not (= height (vertical-motion height))))))
	    (list (point) buffer-end-p))
	(if orig-win
	    (select-window orig-win))))))


;; Can't use `save-window-excursion' since it triggers a redraw.
(defun follow-calc-win-start (windows pos win)
  "Calculate where WIN will start if the first in WINDOWS start at POS.

If WIN is nil the point below all windows is returned."
  (let (start)
    (while (and windows (not (eq (car windows) win)))
      (setq start (window-start (car windows)))
      (set-window-start (car windows) pos 'noforce)
      (setq pos (car (inline (follow-calc-win-end (car windows)))))
      (set-window-start (car windows) start 'noforce)
      (setq windows (cdr windows)))
    pos))


1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
;; The result from `follow-windows-start-end' is cached when using
;; a handful simple commands, like cursor movement commands.

(defsubst follow-cache-valid-p (windows)
  "Test if the cached value of `follow-windows-start-end' can be used.
Note that this handles the case when the cache has been set to nil."
  (let ((res t)
	(cache follow-windows-start-end-cache))
    (while (and res windows cache)
      (setq res (and (eq (car windows)
			 (car (car cache)))
		     (eq (window-start (car windows))
			 (car (cdr (car cache))))))
      (setq windows (cdr windows))
      (setq cache (cdr cache)))
    (and res (null windows) (null cache))))


(defsubst follow-invalidate-cache ()
  "Force `follow-windows-start-end' to recalculate the end of the window."
  (setq follow-windows-start-end-cache nil))


Karl Heuer's avatar
Karl Heuer committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
;; Build a list of windows and their start and end positions.
;; Useful to avoid calculating start/end position whenever they are needed.
;; The list has the format:
;; ((Win Start End End-of-buffer-visible-p) ...)

;; Used to have a `save-window-excursion', but it obviously triggered
;; redraws of the display. Check if I used it for anything.


(defun follow-windows-start-end (windows)
  "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS."
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  (if (follow-cache-valid-p windows)
      follow-windows-start-end-cache
    (let ((win-start-end '())
	  (orig-win (selected-window)))
      (while windows
	(select-window (car windows))
	(setq win-start-end 
	      (cons (cons (car windows) 
			  (cons (window-start)
				(follow-calc-win-end)))
		    win-start-end))
	(setq windows (cdr windows)))
      (select-window orig-win)
      (setq follow-windows-start-end-cache (nreverse win-start-end))
      follow-windows-start-end-cache)))


(defsubst follow-pos-visible (pos win win-start-end)
Karl Heuer's avatar
Karl Heuer committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
  "Non-nil when POS is visible in WIN."
  (let ((wstart-wend-bend (cdr (assq win win-start-end))))
    (and (>= pos (car wstart-wend-bend))
	 (or (< pos (car (cdr wstart-wend-bend)))
	     (nth 2 wstart-wend-bend)))))


;; By `aligned' we mean that for all adjecent windows, the end of the
;; first is equal with the start of the successor.  The first window
;; should start at a full screen line.

1230
(defsubst follow-windows-aligned-p (win-start-end)
Karl Heuer's avatar
Karl Heuer committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
  "Non-nil if the follower WINDOWS are alinged."
  (let ((res t)) 
    (save-excursion
       (goto-char (window-start (car (car win-start-end))))
       (if (bolp)
	   nil
	 (vertical-motion 0 (car (car win-start-end)))
	 (setq res (eq (point) (window-start (car (car win-start-end)))))))
    (while (and res (cdr win-start-end))
      ;; At least two followers left
1241 1242
      (setq res (eq (car (cdr (cdr (car win-start-end))))
		    (car (cdr (car (cdr win-start-end))))))
Karl Heuer's avatar
Karl Heuer committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
      (setq win-start-end (cdr win-start-end)))
    res))


;; Check if the point is visible in all windows. (So that
;; no one will be recentered.)

(defun follow-point-visible-all-windows-p (win-start-end)
  "Non-nil when the window-point is visible in all windows."
  (let ((res t))
    (while (and res win-start-end)
1254 1255 1256
      (setq res (follow-pos-visible (window-point (car (car win-start-end)))
				    (car (car win-start-end))
				    win-start-end))
Karl Heuer's avatar
Karl Heuer committed
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
      (setq win-start-end (cdr win-start-end)))
    res))


;; Make sure WIN always starts at the beginning of an whole screen
;; line. If WIN is not aligned the start is updated which probably
;; will lead to a redisplay of the screen later on.
;;
;; This is used with the first window in a follow chain.  The reason
;; is that we want to detect that the point is outside the window.
;; (Without the update, the start of the window will move as the
;; user presses BackSpace, and the other window redisplay routines
;; will move the start of the window in the wrong direction.)

(defun follow-update-window-start (win)
  "Make sure that the start of WIN starts at a full screen line."
  (save-excursion
    (goto-char (window-start win))
    (if (bolp)
	nil
      (vertical-motion 0 win)
      (if (eq (point) (window-start win))
	  nil
	(vertical-motion 1 win)
	(set-window-start win (point) 'noforce)))))

;;}}}
;;{{{ Selection functions

;; Make a window in WINDOWS selected if it currently
;; is displaying the position DEST.
;;
;; We don't select a window if it just has been moved.

(defun follow-select-if-visible (dest win-start-end)
  "Select and return a window, if DEST is visible in it.
Return the selected window."
  (let ((win nil))
    (while (and (not win) win-start-end)
      ;; Don't select a window which was just moved. This makes it
      ;; possible to later select the last window after a `end-of-buffer'
      ;; command.
      (if (follow-pos-visible dest (car (car win-start-end)) win-start-end)
	  (progn
	    (setq win (car (car win-start-end)))
	    (select-window win)))
      (setq win-start-end (cdr win-start-end)))
    win))


;; Lets select a window showing the end. Make sure we only select it if it
;; it wasn't just moved here. (i.e. M-> shall not unconditionally place
;; the point in the selected window.)
;;
;; (Compability cludge: in Emacs 19 `window-end' is equal to `point-max';
;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
;; checking `window-end' now when I check `end-of-buffer' explicitylt?)

(defun follow-select-if-end-visible (win-start-end)
  "Select and return a window, if end is visible in it."
  (let ((win nil))
    (while (and (not win) win-start-end)
      ;; Don't select a window which was just moved. This makes it
      ;; possible to later select the last window after a `end-of-buffer'
      ;; command.
      (if (and (eq (point-max) (nth 2 (car win-start-end)))
	       (nth 3 (car win-start-end))
	       (eq (point-max) (min (point-max)
				    (window-end (car (car win-start-end))))))
	  (progn
	    (setq win (car (car win-start-end)))
	    (select-window win)))
      (setq win-start-end (cdr win-start-end)))
    win))


;; Select a window which will display the point if the windows would
;; be redisplayed with the first window fixed. This is useful for
;; example when the user has pressed return at the bottom of a window
;; as the point is not visible in any window.

(defun follow-select-if-visible-from-first (dest windows)
  "Select and return a window with DEST, if WINDOWS are redrawn from top."
  (let ((win nil)
	end-pos-end-p)
    (save-excursion
      (goto-char (window-start (car windows)))
      ;; Make sure the line start in the beginning of a real screen
      ;; line.
      (vertical-motion 0 (car windows))    
      (if (< dest (point))
	  ;; Above the start, not visible.
	  nil
	;; At or below the start. Check the windows.
	(save-window-excursion
	  (while (and (not win) windows)
	    (set-window-start (car windows) (point) 'noforce)
	    (setq end-pos-end-p (follow-calc-win-end (car windows)))
	    (goto-char (car end-pos-end-p))
	    ;; Visible, if dest above end, or if eob is visible inside
	    ;; the window.
	    (if (or (car (cdr end-pos-end-p))
		    (< dest (point)))
		  (setq win (car windows))
		(setq windows (cdr windows)))))))
    (if win 
	(select-window win))
    win))


;;}}}
;;{{{ Redisplay 

;; Redraw all the windows on the screen, starting with the top window.
;; The window used as as marker is WIN, or the selcted window if WIN
;; is nil.

(defun follow-redisplay (&optional windows win)
  "Reposition the WINDOWS around WIN.
Should the point be too close to the roof we redisplay everything
from the top. WINDOWS should contain a list of windows to
redisplay, it is assumed that WIN is a member of the list. 
Should WINDOWS be nil, the windows displaying the
same buffer as WIN, in the current frame, are used.
Should WIN be nil, the selected window is used." 
  (or win
      (setq win (selected-window)))
  (or windows 
      (setq windows (follow-all-followers win)))
  (follow-downward windows (follow-calculate-first-window-start windows win)))


;; Redisplay a chain of windows. Start every window directly after the
;; end of the previous window, to make sure long lines are displayed
;; correctly.

(defun follow-downward (windows pos)
  "Redisplay all WINDOWS starting at POS."
  (while windows
    (set-window-start (car windows) pos)
    (setq pos (car (follow-calc-win-end (car windows))))
    (setq windows (cdr windows))))


;;(defun follow-downward (windows pos)
;;  "Redisplay all WINDOWS starting at POS."
;;  (let (p)
;;    (while windows
;;      (setq p (window-point (car windows)))
;;      (set-window-start (car windows) pos)
;;      (set-window-point (car windows) (max p pos))
;;      (setq pos (car (follow-calc-win-end (car windows))))
;;      (setq windows (cdr windows)))))


;; Return the start of the first window.
;;
;; First, estimate the position. It the value is not perfect (i.e. we
;; have somewhere splited a line between windows) we try to enhance
;; the value.
;;
;; The guess is always perfect if no long lines is split between
;; windows.
;;
;; The worst case peformace of probably very bad, but it is very
;; unlikely that we ever will miss the correct start by more than one
;; or two lines.

(defun follow-calculate-first-window-start (windows &optional win start)
  "Calculate the start of the first window.

WINDOWS is a chain of windows to work with.  WIN is the window
to recenter around.  It is assumed that WIN starts at position 
START."
  (or win 
      (setq win (selected-window)))
  (or start 
      (setq start (window-start win)))
  (let ((guess (follow-estimate-first-window-start windows win start)))
    (if (car guess)
	(cdr guess)
      ;; The guess wasn't exact, try to enhance it.
      (let ((win-start (follow-calc-win-start windows (cdr guess) win)))
	(cond ((= win-start start) 
	       (follow-debug-message "exact")
	       (cdr guess))
	      ((< win-start start)
	       (follow-debug-message "above")
	       (follow-calculate-first-window-start-from-above 
		windows (cdr guess) win start))
	      (t
	       (follow-debug-message "below")
	       (follow-calculate-first-window-start-from-below 
		windows (cdr guess) win start)))))))


;; `exact' is disabled due to XEmacs and fonts of variable
;; height.
(defun follow-estimate-first-window-start (windows win start)
  "Estimate the position of the first window.

Returns (EXACT . POS). If EXACT is non-nil, POS is the starting
position of the first window. Otherwise it is a good guess."
  (let ((pred (car (follow-split-followers windows win)))
	(exact nil))
    (save-excursion
      (goto-char start)
      ;(setq exact (bolp))
      (vertical-motion 0 win)
      (while pred
	(vertical-motion (- 1 (window-height (car pred))) (car pred))
	(if (not (bolp))
	  (setq exact nil))
	(setq pred (cdr pred)))
      (cons exact (point)))))


;; Find the starting point, start at GUESS and search downward.
;; The returned point is always a point below GUESS.

(defun follow-calculate-first-window-start-from-above 
       (windows guess win start)
  (save-excursion
    (let ((done nil)
	  win-start
	  res)
      (goto-char guess)
      (while (not done)
	(if (not (= (vertical-motion 1 (car windows)) 1))
	    ;; Hit bottom! (Can we really do this?)
	    ;; We'll keep it, since it ensures termination.
	    (progn
	      (setq done t)
	      (setq res (point-max)))
	  (setq win-start (follow-calc-win-start windows (point) win))
	  (if (>= win-start start)
	      (progn 
		(setq done t)
		(setq res (point))))))
      res)))


;; Find the starting point, start at GUESS and search upward.  Return
;; a point on the same line as GUESS, or above.
;;
;; (Is this ever used? I must make sure it works just in case it is
;; ever called.)

(defun follow-calculate-first-window-start-from-below
       (windows guess &optional win start)
  (setq win (or win (selected-window)))
  (setq start (or start (window-start win)))
  (save-excursion
    (let ((done nil)
	  win-start
	  res)
      ;; Always calculate what happend when no line is displayed in the first
      ;; window. (The `previous' res is needed below!)
      (goto-char guess)
      (vertical-motion 0 (car windows))
      (setq res (point))
      (while (not done)
	(if (not (= (vertical-motion -1 (car windows)) -1))
	    ;; Hit roof!
	    (progn
	      (setq done t)
	      (setq res (point-min)))
	  (setq win-start (follow-calc-win-start windows (point) win))
	  (cond ((= win-start start)	; Perfect match, use this value
		 (setq done t)
		 (setq res (point)))
		((< win-start start)	; Walked to far, use preious result
		 (setq done t))
		(t			; Store result for next iteration
		 (setq res (point))))))
      res)))

;;}}}
;;{{{ Avoid tail recenter

;; This sets the window internal flag `force_start'. The effect is that
;; windows only displaying the tail isn't recentered.
;; Has to be called before every redisplay... (Great isn't it?)
;;
;; XEmacs doesn't recenter the tail, GOOD!
;;
;; A window displaying only the tail, is a windows whose
;; window-start position is equal to (point-max) of the buffer it
;; displays.
;;
;; This function is also added to `post-command-idle-hook', introduced
;; in Emacs 19.30.  This is needed since the vaccine injected by the
;; call from `post-command-hook' only works until the next redisplay.
;; It is possible that the functions in the `post-command-idle-hook'
;; can cause a redisplay, and hence a new vaccine is needed.
;;
;; Sometimes, calling this function could actually cause a redisplay,
;; especially if it is placed in the debug filter section.  I must
;; investigate this further...

(defun follow-avoid-tail-recenter (&rest rest)
  "Make sure windows displaying the end of a buffer aren't recentered.

This is done by reading and rewriting the start positon of 
non-first windows in Follow Mode."
  (if follow-avoid-tail-recenter-p
      (let* ((orig-buffer (current-buffer))
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	    (top (frame-first-window (selected-frame)))
	    (win top)
	    (who '())			; list of (buffer . frame)
	    start
	    pair)			; (buffer . frame)
	;; If the only window in the frame is a minibuffer
	;; window, `next-window' will never find it again...
	(if (window-minibuffer-p top)
	    nil
	  (while  ;; look, no body!
	      (progn
		(setq start (window-start win))
		(set-buffer (window-buffer win))
		(setq pair (cons (window-buffer win) (window-frame win)))
		(if (member pair who)
		    (if (and (boundp 'follow-mode) follow-mode 
			     (eq (point-max) start))
			;; Write the same window start back, but don't
			;; set the NOFORCE flag.
			(set-window-start win start))
		  (setq who (cons pair who)))
		(setq win (next-window win 'not t))
		(not (eq win top))))  ;; Loop while this is true.
	  (set-buffer orig-buffer)))))
Karl Heuer's avatar
Karl Heuer committed
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

;;}}}

;;}}}
;;{{{ Post Command Hook

;;; The magic little box. This function is called after every command.

;; This is not as complicated as it seems. It is simply a list of common
;; display situations and the actions to take, plus commands for redrawing
;; the screen if it should be unaligned.
;;
;; We divide the check into two parts; whether we are at the end or not.
;; This is due to the fact that the end can actaually be visible
;; in several window even though they are aligned.

(defun follow-post-command-hook ()
  "Ensure that the windows in Follow mode are adjecent after each command."
  (setq follow-inside-post-command-hook t)
  (if (or (not (input-pending-p))
	  ;; Sometimes, in XEmacs, mouse events are not handled
	  ;; properly by `input-pending-p'.  A typical example is
	  ;; when clicking on a node in `info'.
	  (and (boundp 'current-mouse-event)
	       (symbol-value 'current-mouse-event)
	       (fboundp 'button-event-p)
	       (funcall (symbol-function 'button-event-p)
			(symbol-value 'current-mouse-event))))
      ;; Work in the selected window, not in the current buffer.
      (let ((orig-buffer (current-buffer))
	    (win (selected-window)))
	(set-buffer (window-buffer win))
1620 1621 1622
	(or (and (symbolp this-command) 
		 (get this-command 'follow-mode-use-cache))
	    (follow-invalidate-cache))
Karl Heuer's avatar
Karl Heuer committed
1623 1624 1625 1626 1627 1628 1629
	(if (and (boundp 'follow-mode) follow-mode
		 (not (window-minibuffer-p win)))
	    ;; The buffer shown in the selected window is in follow
	    ;; mode, lets find the current state of the display and
	    ;; cache the result for speed (i.e. `aligned' and `visible'.)
	    (let* ((windows (inline (follow-all-followers win)))
		   (dest (point))
1630
		   (win-start-end (inline
Karl Heuer's avatar
Karl Heuer committed
1631 1632 1633 1634
				    (follow-update-window-start (car windows))
				    (follow-windows-start-end windows)))
		   (aligned (follow-windows-aligned-p win-start-end))
		   (visible (follow-pos-visible dest win win-start-end)))
1635 1636 1637
	      (if (not (and aligned visible))
		  (follow-invalidate-cache))
	      (inline (follow-avoid-tail-recenter))
Karl Heuer's avatar
Karl Heuer committed
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	      ;; Select a window to display the point.
	      (or follow-internal-force-redisplay
		  (progn
		    (if (eq dest (point-max))
			;; We're at the end, we have be be careful since
			;; the display can be aligned while `dest' can
			;; be visible in several windows.
			(cond
			 ;; Select the current window, but only when
			 ;; the display is correct. (When inserting
			 ;; character in a tail window, the display is
			 ;; not correct, as they are shown twice.)
			 ;;
			 ;; Never stick to the current window after a
			 ;; deletion.  The reason is cosmetic, when
			 ;; typing `DEL' in a window showing only the
			 ;; end of the file, character are removed
			 ;; from the window above, which is very
			 ;; unintuitive.
			 ((and visible
			       aligned
			       (not (memq this-command 
					  '(backward-delete-char
					    delete-backward-char
					    backward-delete-char-untabify
					    kill-region))))
			  (follow-debug-message "Max: same"))
			 ;; If the end is visible, and the window
			 ;; doesn't seems like it just has been moved,
			 ;; select it.
			 ((follow-select-if-end-visible win-start-end)
			  (follow-debug-message "Max: end visible")
			  (setq visible t)
			  (setq aligned nil)
			  (goto-char dest))
			 ;; Just show the end...
			 (t
			  (follow-debug-message "Max: default")
			  (select-window (car (reverse windows)))
			  (goto-char dest)
			  (setq visible nil)
			  (setq aligned nil)))
		      
		      ;; We're not at the end, here life is much simpler.
		      (cond
		       ;; This is the normal case! 
		       ;; It should be optimized for speed.
		       ((and visible aligned)
			(follow-debug-message "same"))
		       ;; Pick a position in any window.  If the
		       ;; display is ok, this will pick the `correct'
		       ;; window.  If the display is wierd do this
		       ;; anyway, this will be the case after a delete
		       ;; at the beginning of the window.
		       ((follow-select-if-visible dest win-start-end)
			(follow-debug-message "visible")
			(setq visible t)
			(goto-char dest))
		       ;; Not visible anywhere else, lets pick this one.
		       ;; (Is this case used?)
		       (visible
			(follow-debug-message "visible in selected."))
		       ;; Far out!
		       ((eq dest (point-min))
			(follow-debug-message "min")
			(select-window (car windows))
			(goto-char dest)
			(set-window-start (selected-window) (point-min))
			(setq win-start-end (follow-windows-start-end windows))
1707
			(follow-invalidate-cache)
Karl Heuer's avatar
Karl Heuer committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
			(setq visible t)
			(setq aligned nil))
		       ;; If we can position the cursor without moving the first
		       ;; window, do it. This is the case which catches `RET'
		       ;; at the bottom of a window.
		       ((follow-select-if-visible-from-first dest windows)
			(follow-debug-message "Below first")
			(setq visible t)		  
			(setq aligned t)
			(follow-redisplay windows (car windows))
			(goto-char dest))
		       ;; None of the above. For simplicity, we stick to the
		       ;; selected window.
		       (t
			(follow-debug-message "None")
			(setq visible nil)
			(setq aligned nil))))
		    ;; If a new window has been selected, make sure that the
		    ;; old is not scrolled when the point is outside the
		    ;; window.
		    (or (eq win (selected-window))
			(let ((p (window-point win)))
			  (set-window-start win (window-start win) nil)
			  (set-window-point win p)))))
	      ;; Make sure the point is visible in the selected window.
	      ;; (This could lead to a scroll.)
	      (if (or visible
		      (follow-pos-visible dest win win-start-end))
		  nil
		(sit-for 0)
		(follow-avoid-tail-recenter)
		(setq win-start-end (follow-windows-start-end windows))
1740
		(follow-invalidate-cache)
Karl Heuer's avatar
Karl Heuer committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
		(setq aligned nil))
	      ;; Redraw the windows whenever needed.
	      (if (or follow-internal-force-redisplay
		      (not (or aligned
			       (follow-windows-aligned-p win-start-end)))
		      (not (inline (follow-point-visible-all-windows-p 
				    win-start-end))))
		  (progn
		    (setq follow-internal-force-redisplay nil)
		    (follow-redisplay windows (selected-window))
		    (setq win-start-end (follow-windows-start-end windows))
1752
		    (follow-invalidate-cache)
Karl Heuer's avatar
Karl Heuer committed
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
		    ;; When the point ends up in another window. This
		    ;; happends when dest is in the beginning of the
		    ;; file and the selected window is not the first.
		    ;; It can also, in rare situations happend when
		    ;; long lines are used and there is a big
		    ;; difference between the width of the windows.
		    ;; (When scrolling one line in a wide window which
		    ;; will cause a move larger that an entire small
		    ;; window.)
		    (if (follow-pos-visible dest win win-start-end)
			nil
		      (follow-select-if-visible dest win-start-end)
		      (goto-char dest))))
	      
	      ;; If the region is visible, make it look good when spanning
	      ;; multiple windows.
	      (if (or (and (boundp 'mark-active) (symbol-value 'mark-active))
		      (and (fboundp 'region-active-p) 
			   (funcall (symbol-function 'region-active-p))))
		  (follow-maximize-region 
		   (selected-window) windows win-start-end))

1775
	      (inline (follow-avoid-tail-recenter))
Karl Heuer's avatar
Karl Heuer committed
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
	      ;; DEBUG
	      ;;(if (not (follow-windows-aligned-p 
	      ;;           (follow-windows-start-end windows)))
	      ;;    (message "follow-mode: windows still unaligend!"))
	      ;; END OF DEBUG
	      )				; Matches (let*
	  ;; Buffer not in follow mode:
	  ;; We still must update the windows displaying the tail so that
	  ;; Emacs won't recenter them.
	  (follow-avoid-tail-recenter))
	(set-buffer orig-buffer)))
  (setq follow-inside-post-command-hook nil))

;;}}}
;;{{{ The region

;; Tries to make the highlighted area representing the region look
;; good when spanning several windows. 
;;
;; Not perfect, as the point can't be placed at window end, only at
;; end-1. Whis will highlight a little bit in windows above
;; the current.

(defun follow-maximize-region (win windows win-start-end)
  "Make a highlighted region stretching multiple windows look good
when in Follow mode."
  (let* ((all (follow-split-followers windows win))
	 (pred (car all))
	 (succ (cdr all))
	 data)
    (while pred
      (setq data (assq (car pred) win-start-end))
      (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
      (setq pred (cdr pred)))
    (while succ
      (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
      (setq succ (cdr succ)))))

;;}}}
;;{{{ Scroll bar

;;;; Scroll-bar support code.

;;; Why is it needed? Well, if the selected window is in follow mode,
;;; all its follower stick to it blindly. If one of them is scrolled,
;;; it immediately returns to the original position when the mouse is
;;; released. If the selected window is not a follower of the dragged
;;; window the windows will be unaligned.

;;; The advices doesn't get compiled. Aestetically, this might be a
;;; problem but in practical life it isn't.

;;; Discussion: Now when the other windows in the chain follow the
;;; dragged, should we really select it?

(cond ((fboundp 'scroll-bar-drag)
       ;;;
       ;;; Emacs 19 style scrollbars.
       ;;;

       ;; Select the dragged window if it is a follower of the
       ;; selected window.
       ;;
       ;; Generate advices of the form:
       ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate)
       ;;   "Adviced by `follow-mode'."
       ;;   (follow-redraw-after-event (ad-get-arg 0)))
       (let ((cmds '(scroll-bar-drag
		     scroll-bar-drag-1	; Executed at every move.
		     scroll-bar-scroll-down
		     scroll-bar-scroll-up
		     scroll-bar-set-window-start)))
	 (while cmds
	   (eval
	    (` (defadvice (, (intern (symbol-name (car cmds))))
		 (after 
		  (, (intern (concat "follow-" (symbol-name (car cmds))))) 
		  activate)
		 "Adviced by Follow Mode."
		 (follow-redraw-after-event (ad-get-arg 0)))))
	   (setq cmds (cdr cmds))))
    
    
       (defun follow-redraw-after-event (event)
	 "Adviced by Follow mode."
	 (condition-case nil
	     (let* ((orig-win (selected-window))
		    (win (nth 0 (funcall 
				 (symbol-function 'event-start) event)))
		    (fmode (assq 'follow-mode 
				 (buffer-local-variables 
				  (window-buffer win)))))
	       (if (and fmode (cdr fmode))
		   ;; The selected window is in follow-mode
		   (progn
		     ;; Recenter around the dragged window.
		     (select-window win)
		     (follow-redisplay)
		     (select-window orig-win))))
	   (error nil))))


      ((fboundp 'scrollbar-vertical-drag)
       ;;;
       ;;; XEmacs style scrollbars.
       ;;;

       ;; Advice all scrollbar functions on the form:
       ;;
       ;; (defadvice scrollbar-line-down 
       ;;	(after follow-scrollbar-line-down activate)
       ;;   (follow-xemacs-scrollbar-support (ad-get-arg 0)))

      (let ((cmds '(scrollbar-line-down	; Window
		    scrollbar-line-up
		    scrollbar-page-down	; Object
		    scrollbar-page-up
		    scrollbar-to-bottom	; Window
		    scrollbar-to-top
		    scrollbar-vertical-drag ; Object
		    )))
	
	(while cmds
	  (eval
	   (` (defadvice (, (intern (symbol-name (car cmds))))
		(after 
		 (, (intern (concat "follow-" (symbol-name (car cmds))))) 
		 activate)
		"Adviced by `follow-mode'."
		(follow-xemacs-scrollbar-support (ad-get-arg 0)))))
	  (setq cmds (cdr cmds))))


      (defun follow-xemacs-scrollbar-support (window)
	"Redraw windows showing the same buffer as shown in WINDOW.
WINDOW is either the dragged window, or a cons containing the
window as its first element. This is called while the user drags
the scrollbar.

WINDOW can be an object or a window."
	(condition-case nil
	    (progn
	      (if (consp window)
		  (setq window (car window)))
	      (let ((fmode (assq 'follow-mode 
				 (buffer-local-variables 
				  (window-buffer window))))
		    (orig-win (selected-window)))
		(if (and fmode (cdr fmode))
		    (progn
		      ;; Recenter around the dragged window.
		      (select-window window)
		      (follow-redisplay)
		      (select-window orig-win)))))
	  (error nil)))))

;;}}}
;;{{{ Process output

;;; The following sections installs a spy which listens to process
;;; output and tries to reposition the windows whose buffers are in
;;; Follow mode.  We play safe as much as possible...
;;;
;;; When follow-mode is activated all active processes are
;;; intercepted.  All new processes which change their filter function
;;; using `set-process-filter' are also intercepted.  The reason is
;;; that a process can cause a redisplay recentering "tail" windows.
;;; Note that it doesn't hurt to spy on more processes than needed.
;;;
;;; Technically, we set the process filter to `follow-generic-filter'.
;;; The original filter is stored in `follow-process-filter-alist'.
;;; Our generic filter calls the original filter, or inserts the
;;; output into the buffer, if the buffer originally didn't have an
;;; output filter.  It also makes sure that the windows connected to
;;; the buffer are aligned.
;;;
;;; Discussion: How to we find processes which doesn't call
;;; `set-process-filter'?  (How often are processes created in a
;;; buffer after Follow mode are activated?)
;;;
;;; Discussion: Should we also advice `process-filter' to make our
;;; filter invisible to others?

;;{{{ Advice for `set-process-filter'

;; Do not call this with 'follow-generic-filter as the name of the
;; filter...

(defadvice set-process-filter (before follow-set-process-filter activate)
  "Follow Mode listens to calls to this function to make 
sure process output will be displayed correctly in buffers
in which the mode is activated.

1969
Follow Mode inserts its own process filter to do its 
Karl Heuer's avatar
Karl Heuer committed
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
magic stuff before the real process filter is called."
  (if follow-intercept-processes
      (progn
	(setq follow-process-filter-alist
	      (delq (assq (ad-get-arg 0) follow-process-filter-alist)
		    follow-process-filter-alist))
	(follow-tidy-process-filter-alist)
	(cond ((eq (ad-get-arg 1) t))
	      ((eq (ad-get-arg 1) nil)
	       (ad-set-arg 1 'follow-generic-filter))
	      (t
	       (setq follow-process-filter-alist 
		     (cons (cons (ad-get-arg 0) (ad-get-arg 1)) 
			   follow-process-filter-alist))
	       (ad-set-arg 1 'follow-generic-filter))))))


(defun follow-call-set-process-filter (proc filter)
  "Call original `set-process-filter' without the Follow mode advice."
  (ad-disable-advice 'set-process-filter 'before
		     'follow-set-process-filter)
  (ad-activate 'set-process-filter)
  (prog1 
      (set-process-filter proc filter)
    (ad-enable-advice 'set-process-filter 'before
		      'follow-set-process-filter)
    (ad-activate 'set-process-filter)))


(defadvice process-filter (after follow-process-filter activate)
  "Normally when Follow mode is activated all processes has the
process filter set to `follow-generic-filter'.  With this advice,
the original process filter is returned."
  (cond ((eq ad-return-value 'follow-generic-filter)
	 (setq ad-return-value
	       (cdr-safe (assq (ad-get-arg 0) 
			       follow-process-filter-alist))))))


(defun follow-call-process-filter (proc)
  "Call original `process-filter' without the Follow mode advice."
  (ad-disable-advice 'process-filter 'after
		     'follow-process-filter)
  (ad-activate 'process-filter)
  (prog1 
      (process-filter proc)
    (ad-enable-advice 'process-filter 'after 
		      'follow-process-filter)
    (ad-activate 'process-filter)))
  

(defun follow-tidy-process-filter-alist ()
  "Remove old processes from `follow-process-filter-alist'."
  (let ((alist follow-process-filter-alist)
	(ps (process-list))
	(new ()))
    (while alist
      (if (and (not (memq (process-status (car (car alist))) 
			  '(exit signal closed nil)))
	       (memq (car (car alist)) ps))
	  (setq new (cons (car alist) new)))
      (setq alist (cdr alist)))
    (setq follow-process-filter-alist new)))

;;}}}
;;{{{ Start/stop interception of processes.

;; Normally, all new processed are intercepted by our `set-process-filter'.
;; This is needed to intercept old processed which were started before we were
;; loaded, and processes we have forgotten by calling
;; `follow-stop-intercept-process-output'.

(defun follow-intercept-process-output ()
  "Intercept all active processes.

This is needed so that Follow Mode can track all display events in the
system.  (See `follow-mode')"
  (interactive)
  (let ((list (process-list)))
    (while list
      (if (eq (process-filter (car list)) 'follow-generic-filter)
	  nil
	;; The custom `set-process-filter' defined above.
	(set-process-filter (car list) (process-filter (car list))))
      (setq list (cdr list))))
  (setq follow-intercept-processes t))


(defun follow-stop-intercept-process-output ()
  "Stop Follow Mode from spying on processes.

All current spypoints are removed and no new will be added.

The effect is that Follow mode won't be able to handle buffers 
connected to processes.  

The only reason to call this function is if the Follow mode spy filter
would interfere with some other package.  If this happens, please
report this using the `follow-submit-feedback' function."
  (interactive)
  (follow-tidy-process-filter-alist)
  (let ((list (process-list)))
    (while list
      (if (eq (process-filter (car list)) 'follow-generic-filter)
	  (progn
	    (follow-call-set-process-filter 
	     (car list) 
	     (cdr-safe (assq (car list) follow-process-filter-alist)))
	    (setq follow-process-filter-alist 
		  (delq (assq (car list) follow-process-filter-alist)
			follow-process-filter-alist))))
      (setq list (cdr list))))
  (setq follow-intercept-processes nil))

;;}}}
;;{{{ The filter

;;; The following section is a naive method to make buffers with
;;; process output to work with Follow mode. Whenever the start of the
2089
;;; window displaying the buffer is moved, we moves it back to its
Karl Heuer's avatar
Karl Heuer committed
2090 2091 2092 2093 2094 2095