follow.el 73.5 KB
Newer Older
1
;;; follow.el --- synchronize windows showing the same buffer
Karl Heuer's avatar
Karl Heuer committed
2

3
;; Copyright (C) 1995, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Karl Heuer's avatar
Karl Heuer committed
5

6
;; Author: Anders Lindgren <andersl@andersl.com>
Richard M. Stallman's avatar
Richard M. Stallman committed
7
;; Maintainer: FSF (Anders' email bounces, Sep 2005)
8
;; Created: 1995-05-25
Dan Nicolaescu's avatar
Dan Nicolaescu committed
9
;; Keywords: display, window, minor-mode, convenience
Karl Heuer's avatar
Karl Heuer committed
10

Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13
;; 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
14
;; it under the terms of the GNU General Public License as published by
15
;; the Free Software Foundation; either version 3, or (at your option)
Karl Heuer's avatar
Karl Heuer committed
16 17
;; any later version.

Richard M. Stallman's avatar
Richard M. Stallman committed
18
;; GNU Emacs is distributed in the hope that it will be useful,
Karl Heuer's avatar
Karl Heuer committed
19 20 21 22 23
;; 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
24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
25 26
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Karl Heuer's avatar
Karl Heuer committed
27 28 29 30 31

;;; Commentary:

;;{{{ Documentation

32
;; `Follow mode' is a minor mode for Emacs and XEmacs that
Karl Heuer's avatar
Karl Heuer committed
33 34 35 36 37
;; combines windows into one tall virtual window.
;;
;; The feeling of a "virtual window" has been accomplished by the use
;; of two major techniques:
;;
38
;; * The windows always display adjacent sections of the buffer.
Karl Heuer's avatar
Karl Heuer committed
39
;;   This means that whenever one window is moved, all the
40
;;   others will follow.  (Hence the name Follow mode.)
Karl Heuer's avatar
Karl Heuer committed
41
;;
42 43 44
;; * 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
Karl Heuer's avatar
Karl Heuer committed
45 46
;;   movement commands.
;;
47
;; Follow mode comes to its prime when a large screen and two
48
;; side-by-side window are used.  The user can, with the help of Follow
49 50
;; mode, use two full-height windows as though they are one.
;; Imagine yourself editing a large function, or section of text,
51
;; and being able to use 144 lines instead of the normal 72... (your
Karl Heuer's avatar
Karl Heuer committed
52 53 54
;; mileage may vary).

;; To test this package, make sure `follow' is loaded, or will be
55
;; autoloaded when activated (see below).  Then do the following:
Karl Heuer's avatar
Karl Heuer committed
56
;;
57
;; * Find your favorite file (preferably a long one).
Karl Heuer's avatar
Karl Heuer committed
58
;;
59 60 61
;; * Resize Emacs so that it will be wide enough for two full size
;;   columns.  Delete the other windows and split the window with
;;   the commands `C-x 1 C-x 3'.
Karl Heuer's avatar
Karl Heuer committed
62 63 64 65 66 67 68 69 70 71 72
;;
;; * 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        |
73
;;		         ...        ...
Karl Heuer's avatar
Karl Heuer committed
74 75 76 77 78
;;		    |71        |143       |
;;		    |72        |144       |
;;		    +----------+----------+
;;
;;   As you can see, the right-hand window starts at line 73, the line
79
;;   immediately below the end of the left-hand window.  As long as
80
;;   `follow-mode' is active, the two windows will follow each other!
Karl Heuer's avatar
Karl Heuer committed
81 82
;;
;; * Play around and enjoy! Scroll one window and watch the other.
83 84 85
;;   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
Karl Heuer's avatar
Karl Heuer committed
86 87 88
;;   windows.
;;
;; * Should you find `Follow' mode annoying, just type
89
;;	M-x follow-mode <RETURN>
Karl Heuer's avatar
Karl Heuer committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103
;;   to turn it off.


;; 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:
;;
;; (global-set-key [f8] 'follow-mode)
;; (global-set-key [f7] 'follow-delete-other-windows-and-split)


104 105 106
;; There exist two system variables that control the appearence of
;; lines wider than the window containing them.  The default is to
;; truncate long lines whenever a window isn't as wide as the frame.
Karl Heuer's avatar
Karl Heuer committed
107 108 109 110 111 112 113 114
;;
;; 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)


115 116 117 118 119 120 121 122 123
;; 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
124
;; The correct way to cofigurate Follow mode, or any other mode for
125 126
;; that matter, is to create one or more functions that do
;; whatever you would like to do.  These functions are then added to
Karl Heuer's avatar
Karl Heuer committed
127 128 129 130
;; a hook.
;;
;; When `Follow' mode is activated, functions stored in the hook
;; `follow-mode-hook' are called.  When it is deactivated
Richard M. Stallman's avatar
Richard M. Stallman committed
131
;; `follow-mode-off-hook' is run.
Karl Heuer's avatar
Karl Heuer committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145
;;
;; 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:
;;
146 147
;; To activate, issue the command "M-x follow-mode"
;; and press Return.  To deactivate, do it again.
Karl Heuer's avatar
Karl Heuer committed
148
;;
149 150
;; The following is a list of commands useful when follow-mode is active.
;;
Karl Heuer's avatar
Karl Heuer committed
151
;;	follow-scroll-up			 C-c . C-v
152
;;		Scroll text in a Follow mode window chain up.
Karl Heuer's avatar
Karl Heuer committed
153 154 155 156 157
;;
;;	follow-scroll-down			 C-c . v
;;		Like `follow-scroll-up', but in the other direction.
;;
;;	follow-delete-other-windows-and-split	 C-c . 1
158
;;		Maximize the visible area of the current buffer,
159 160
;;		and enter Follow mode. 	This is a very convenient
;;		way to start Follow mode, hence we recomend that
161
;;		this command be added to the global keymap.
Karl Heuer's avatar
Karl Heuer committed
162 163 164 165 166 167 168 169 170 171
;;
;;	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
172
;;		Switch buffer in all windows in the selected frame.
Karl Heuer's avatar
Karl Heuer committed
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
;;
;;	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
198
;;    you want to view two locations.  Of course, `follow' mode can
Karl Heuer's avatar
Karl Heuer committed
199 200 201 202 203 204
;;    be reactivated by hitting the same key again.
;;
;;    Example from my ~/.emacs:
;;	(global-set-key [f8] 'follow-mode)


205
;; Implementation:
Karl Heuer's avatar
Karl Heuer committed
206 207
;;
;; In an ideal world, follow mode would have been implemented in the
208
;; kernel of the display routines, making sure that the windows (using
209
;; follow mode) ALWAYS are aligned.  On planet Earth, however, we must
Karl Heuer's avatar
Karl Heuer committed
210 211 212 213 214 215 216 217 218
;; 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
219
;; situations that should be covered.)
Karl Heuer's avatar
Karl Heuer committed
220
;;
221 222
;; Note that only the selected window is checked, for the reason of
;; efficiency and code complexity. (I.e. it is possible to make a
223
;; non-selected windows unaligned.  It will, however, pop right back
Karl Heuer's avatar
Karl Heuer committed
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
;; when it is selected.)

;;}}}

;;; 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
250 251 252
;; evaluted.  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...
Karl Heuer's avatar
Karl Heuer committed
253 254 255 256
;;
;; Should someone come up with a better solution, please let me
;; know.

Juanma Barranquero's avatar
Juanma Barranquero committed
257 258
(require 'easymenu)

Karl Heuer's avatar
Karl Heuer committed
259 260 261
(eval-when-compile
  (if (or (featurep 'bytecomp)
	  (featurep 'byte-compile))
262
      (cond ((featurep 'xemacs)
Karl Heuer's avatar
Karl Heuer committed
263 264 265 266 267 268 269 270 271 272 273 274
	     ;; 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

Stephen Eglen's avatar
Stephen Eglen committed
275 276 277
(defgroup follow nil
  "Synchronize windows showing the same buffer."
  :prefix "follow-"
Dan Nicolaescu's avatar
Dan Nicolaescu committed
278 279
  :group 'windows
  :group 'convenience)
Stephen Eglen's avatar
Stephen Eglen committed
280 281

(defcustom follow-mode-hook nil
282
  "Normal hook run by `follow-mode'."
Stephen Eglen's avatar
Stephen Eglen committed
283 284
  :type 'hook
  :group 'follow)
Karl Heuer's avatar
Karl Heuer committed
285

Stephen Eglen's avatar
Stephen Eglen committed
286
(defcustom follow-mode-off-hook nil
287
  "Hooks to run when Follow mode is turned off."
Stephen Eglen's avatar
Stephen Eglen committed
288 289
  :type 'hook
  :group 'follow)
290
(make-obsolete-variable 'follow-mode-off-hook 'follow-mode-hook "22.2")
Karl Heuer's avatar
Karl Heuer committed
291

292
;;{{{ Keymap/Menu
Karl Heuer's avatar
Karl Heuer committed
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307
;; 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!
Karl Heuer's avatar
Karl Heuer committed
308

Stephen Eglen's avatar
Stephen Eglen committed
309
(defcustom follow-mode-prefix "\C-c."
310
  "Prefix key to use for follow commands in Follow mode.
Karl Heuer's avatar
Karl Heuer committed
311
The value of this variable is checked as part of loading Follow mode.
Stephen Eglen's avatar
Stephen Eglen committed
312 313 314 315
After that, changing the prefix key requires manipulating keymaps."
  :type 'string
  :group 'follow)

316 317 318
(defvar follow-mode-map
  (let ((mainmap (make-sparse-keymap))
        (map (make-sparse-keymap)))
Karl Heuer's avatar
Karl Heuer committed
319 320 321 322 323 324 325 326 327 328 329 330
    (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)

331
    (define-key mainmap follow-mode-prefix map)
Karl Heuer's avatar
Karl Heuer committed
332

333
    ;; Replace the standard `end-of-buffer', when in Follow mode.  (I
334
    ;; don't see the point in trying to replace every function that
Karl Heuer's avatar
Karl Heuer committed
335 336 337
    ;; 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.)
338
    (define-key mainmap [remap end-of-buffer] 'follow-end-of-buffer)
Karl Heuer's avatar
Karl Heuer committed
339

340 341
    mainmap)
  "Minor mode keymap for Follow mode.")
Karl Heuer's avatar
Karl Heuer committed
342

343 344 345
;; When the mode is not activated, only one item is visible to activate
;; the mode.
(defun follow-menu-filter (menu)
346
  (if (bound-and-true-p follow-mode)
347
      menu
348
    '(["Follow mode"	follow-mode
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
       :style toggle :selected follow-mode])))

;; If there is a `tools' menu, 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.
(easy-menu-add-item nil '("Tools")
  '("Follow"
    ;; The Emacs code used to just grey out operations when follow-mode was
    ;; not enabled, whereas the XEmacs code used to remove it altogether.
    ;; Not sure which is preferable, but clearly the preference should not
    ;; depend on the flavor.
    :filter follow-menu-filter
    ["Scroll Up"	follow-scroll-up	follow-mode]
    ["Scroll Down"	follow-scroll-down	follow-mode]
    "--"
    ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode]
    "--"
    ["Switch To Buffer"	follow-switch-to-buffer	follow-mode]
    ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode]
    "--"
    ["First Window"	follow-first-window	follow-mode]
    ["Last Window"	follow-last-window	follow-mode]
    ["Next Window"	follow-next-window	follow-mode]
    ["Previous Window"	follow-previous-window	follow-mode]
    "--"
    ["Recenter"		follow-recenter		follow-mode]
    "--"
    ["Follow mode"	follow-mode :style toggle :selected follow-mode]))

379
;;}}}
Karl Heuer's avatar
Karl Heuer committed
380

381 382 383 384 385 386 387 388 389 390 391 392 393
(defcustom 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 \"\"."
  :type 'string
  :group 'follow)

(defcustom follow-auto nil
  "Non-nil activates Follow mode whenever a file is loaded."
  :type 'boolean
  :group 'follow)

(defcustom follow-intercept-processes (fboundp 'start-process)
394
  "When non-nil, Follow mode will monitor process output."
395 396 397 398
  :type 'boolean
  :group 'follow)

(defvar follow-avoid-tail-recenter-p (not (featurep 'xemacs))
399
  "*When non-nil, patch Emacs so that tail windows won't be recentered.
400 401 402 403

A \"tail window\" is a window that displays only the end of
the buffer.  Normally it is practical for the user that empty
windows are recentered automatically.  However, when using
404
Follow mode it breaks the display when the end is displayed
405 406 407
in a window \"above\" the last window.  This is for
example the case when displaying a short page in info.

408
Must be set before Follow mode is loaded.
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

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!")

(defvar follow-cache-command-list
  '(next-line previous-line forward-char backward-char)
  "List of commands that 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.")

(defvar follow-debug nil
  "*Non-nil when debugging Follow mode.")


;; Internal variables:

(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'.")

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

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

;; This inline function must be as small as possible!
;; Maybe we should define a macro that expands to nil if
;; the variable 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)))
Karl Heuer's avatar
Karl Heuer committed
465

466 467 468
;;}}}
;;{{{ Cache

469 470
(dolist (cmd follow-cache-command-list)
  (put cmd 'follow-mode-use-cache t))
471

Karl Heuer's avatar
Karl Heuer committed
472 473 474 475 476 477
;;}}}

;;{{{ The mode

;;;###autoload
(defun turn-on-follow-mode ()
478
  "Turn on Follow mode.  Please see the function `follow-mode'."
Karl Heuer's avatar
Karl Heuer committed
479 480 481 482 483
  (follow-mode 1))


;;;###autoload
(defun turn-off-follow-mode ()
484
  "Turn off Follow mode.  Please see the function `follow-mode'."
Karl Heuer's avatar
Karl Heuer committed
485 486
  (follow-mode -1))

487
(put 'follow-mode 'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
488
;;;###autoload
489
(define-minor-mode follow-mode
490
  "Minor mode that combines windows into one tall virtual window.
Karl Heuer's avatar
Karl Heuer committed
491 492 493 494

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

495
* The windows always displays adjacent sections of the buffer.
Karl Heuer's avatar
Karl Heuer committed
496
  This means that whenever one window is moved, all the
497
  others will follow.  (Hence the name Follow mode.)
Karl Heuer's avatar
Karl Heuer committed
498

499 500 501
* 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
Karl Heuer's avatar
Karl Heuer committed
502 503 504
  movement commands.

Follow mode comes to its prime when used on a large screen and two
Juanma Barranquero's avatar
Juanma Barranquero committed
505
side-by-side windows are used.  The user can, with the help of Follow
Karl Heuer's avatar
Karl Heuer committed
506
mode, use two full-height windows as though they would have been
507
one.  Imagine yourself editing a large function, or section of text,
508
and being able to use 144 lines instead of the normal 72... (your
Karl Heuer's avatar
Karl Heuer committed
509 510 511 512 513 514
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.

515
Only windows displayed in the same frame follow each other.
Karl Heuer's avatar
Karl Heuer committed
516 517 518 519 520

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.)

521
This command runs the normal hook `follow-mode-hook'.
Karl Heuer's avatar
Karl Heuer committed
522 523 524

Keys specific to Follow mode:
\\{follow-mode-map}"
525
  :keymap follow-mode-map
526 527
  (when (and follow-mode follow-intercept-processes)
    (follow-intercept-process-output))
528 529 530
  (cond (follow-mode ; On
         ;; XEmacs: If this is non-nil, the window will scroll before
         ;; the point will have a chance to get into the next window.
531 532
         (when (boundp 'scroll-on-clipped-lines)
	   (setq scroll-on-clipped-lines nil))
533
         (force-mode-line-update)
534
         (add-hook 'post-command-hook 'follow-post-command-hook t))
535 536

        ((not follow-mode) ; Off
537
         (force-mode-line-update))))
Karl Heuer's avatar
Karl Heuer committed
538 539 540 541 542 543 544

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

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

545
(add-hook 'find-file-hook 'follow-find-file-hook t)
Karl Heuer's avatar
Karl Heuer committed
546 547

(defun follow-find-file-hook ()
548
  "Find-file hook for Follow mode.  See the variable `follow-auto'."
Karl Heuer's avatar
Karl Heuer committed
549 550 551 552 553 554 555 556 557 558 559 560
  (if follow-auto (follow-mode t)))

;;}}}

;;{{{ User functions

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

;;{{{ Scroll

561
;; `scroll-up' and `-down', but for windows in Follow mode.
Karl Heuer's avatar
Karl Heuer committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
;;
;; 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)
577
  "Scroll text in a Follow mode window chain up.
Karl Heuer's avatar
Karl Heuer committed
578 579 580 581 582 583 584

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.

585
Works like `scroll-up' when not in Follow mode."
Karl Heuer's avatar
Karl Heuer committed
586 587 588 589 590 591 592 593 594 595 596 597
  (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))
598 599 600
	     ;; `window-end' might return nil.
	     (if end
		 (goto-char end))
Karl Heuer's avatar
Karl Heuer committed
601 602 603 604 605
	     (vertical-motion (- next-screen-context-lines))
	     (set-window-start (car windows) (point)))))))


(defun follow-scroll-down (&optional arg)
606
  "Scroll text in a Follow mode window chain down.
Karl Heuer's avatar
Karl Heuer committed
607 608 609 610 611 612 613

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.

614
Works like `scroll-up' when not in Follow mode."
Karl Heuer's avatar
Karl Heuer committed
615 616 617 618 619 620 621 622 623 624 625 626 627
  (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)
628 629
	     (vertical-motion (- (- (window-height win)
				    1
Karl Heuer's avatar
Karl Heuer committed
630 631 632 633 634 635 636 637 638 639 640
				    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)
641
  "Create two side by side windows and enter Follow mode.
Karl Heuer's avatar
Karl Heuer committed
642 643

Execute this command to display as much as possible of the text
644
in the selected window.  All other windows, in the current
Karl Heuer's avatar
Karl Heuer committed
645
frame, are deleted and the selected window is split in two
646
side-by-side windows.  Follow mode is activated, hence the
Karl Heuer's avatar
Karl Heuer committed
647 648 649
two windows always will display two successive pages.
\(If one window is moved, the other one will follow.)

650
If ARG is positive, the leftmost window is selected.  If negative,
Karl Heuer's avatar
Karl Heuer committed
651 652 653 654 655 656 657
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")
658
  (let ((other (or (and (null arg)
Karl Heuer's avatar
Karl Heuer committed
659 660 661 662 663 664 665
			(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)
666
    (if other
Karl Heuer's avatar
Karl Heuer committed
667 668 669 670 671 672 673
	(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)
674
  "Show BUFFER in all windows in the current Follow mode window chain."
Karl Heuer's avatar
Karl Heuer committed
675 676 677 678 679 680 681 682 683 684 685 686 687
  (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."
688
  (interactive (list (read-buffer "Switch to Buffer: "
Karl Heuer's avatar
Karl Heuer committed
689 690 691
				  (current-buffer))))
  (or buffer (setq buffer (current-buffer)))
  (let ((orig-window (selected-window)))
692 693
    (walk-windows
     (function
Karl Heuer's avatar
Karl Heuer committed
694 695 696 697 698 699 700 701
      (lambda (win)
	(select-window win)
	(switch-to-buffer buffer))))
    (select-window orig-window)
    (follow-redisplay)))


(defun follow-switch-to-current-buffer-all ()
702
  "Show current buffer in all windows on this frame, and enter Follow mode.
Karl Heuer's avatar
Karl Heuer committed
703

704
To bind this command to a hotkey place the following line
Karl Heuer's avatar
Karl Heuer committed
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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
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)
751 752
  "Recenter the middle window around point.
Rearrange all other windows around the middle window.
Karl Heuer's avatar
Karl Heuer committed
753 754

With a positive argument, place the current line ARG lines
755 756
from the top.  With a negative argument, place it -ARG lines
from the bottom."
Karl Heuer's avatar
Karl Heuer committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
  (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)
799
  "Move point to the end of the buffer, Follow mode style.
Karl Heuer's avatar
Karl Heuer committed
800 801

If the end is not visible, it will be displayed in the last possible
802
window in the Follow mode window chain.
Karl Heuer's avatar
Karl Heuer committed
803

804
The mark is left at the previous position.  With arg N, put point N/10
Karl Heuer's avatar
Karl Heuer committed
805 806 807 808 809 810
of the way from the true end."
  (interactive "P")
  (let ((followers (follow-all-followers))
	(pos (point)))
    (cond (arg
	   (select-window (car (reverse followers))))
811
	  ((follow-select-if-end-visible
Karl Heuer's avatar
Karl Heuer committed
812 813 814 815
	    (follow-windows-start-end followers)))
	  (t
	   (select-window (car (reverse followers)))))
    (goto-char pos)
816 817
    (with-no-warnings
      (end-of-buffer arg))))
Karl Heuer's avatar
Karl Heuer committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

;;}}}

;;}}}

;;{{{ 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."
833
  (or (window-live-p testwin)
Karl Heuer's avatar
Karl Heuer committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
      (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.
851
Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
Karl Heuer's avatar
Karl Heuer committed
852
from the selected window."
853
  (or win
Karl Heuer's avatar
Karl Heuer committed
854 855 856 857 858 859 860 861
      (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))))


862
;; This function is optimized function for speed!
Karl Heuer's avatar
Karl Heuer committed
863 864 865 866 867 868 869 870 871 872

(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)"
873
  (if (featurep 'xemacs)
Karl Heuer's avatar
Karl Heuer committed
874 875 876 877 878 879
      ;; 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)
880
	  (list (+ end 1) nil)))
881 882
    ;; Emacs: We have to calculate the end by ourselves.
    ;; This code works on both XEmacs and Emacs, but now
Karl Heuer's avatar
Karl Heuer committed
883
    ;; that XEmacs has got custom-written code, this could
884
    ;; be optimized for Emacs.
Karl Heuer's avatar
Karl Heuer committed
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
    (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))


922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
;; 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
945 946 947 948 949 950 951 952 953 954 955
;; 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."
956 957 958 959 960 961
  (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))
962 963
	(setq win-start-end
	      (cons (cons (car windows)
964 965 966 967 968 969 970 971 972 973
			  (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
974 975 976 977 978 979 980 981 982 983 984
  "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.

985
(defsubst follow-windows-aligned-p (win-start-end)
986
  "Non-nil if the follower windows are aligned."
987
  (let ((res t))
Karl Heuer's avatar
Karl Heuer committed
988 989 990 991 992 993 994 995
    (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
996 997
      (setq res (eq (car (cdr (cdr (car win-start-end))))
		    (car (cdr (car (cdr win-start-end))))))
Karl Heuer's avatar
Karl Heuer committed
998 999 1000 1001 1002 1003 1004 1005
      (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)
1006
  "Non-nil when the `window-point' is visible in all windows."
Karl Heuer's avatar
Karl Heuer committed
1007 1008
  (let ((res t))
    (while (and res win-start-end)
1009 1010 1011
      (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
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
      (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)
1051
      ;; Don't select a window that was just moved. This makes it
Karl Heuer's avatar
Karl Heuer committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
      ;; 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.)
;;
1066
;; (Compability cludge: in Emacs `window-end' is equal to `point-max';
Karl Heuer's avatar
Karl Heuer committed
1067
;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1068
;; checking `window-end' now when I check `end-of-buffer' explicitly?)
Karl Heuer's avatar
Karl Heuer committed
1069 1070 1071 1072 1073

(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)
1074
      ;; Don't select a window that was just moved. This makes it
Karl Heuer's avatar
Karl Heuer committed
1075 1076 1077 1078
      ;; 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))
1079 1080 1081 1082
	       ;; `window-end' might return nil.
	       (let ((end (window-end (car (car win-start-end)))))
		 (and end
		      (eq (point-max) (min (point-max) end)))))
Karl Heuer's avatar
Karl Heuer committed
1083 1084 1085 1086 1087 1088 1089
	  (progn
	    (setq win (car (car win-start-end)))
	    (select-window win)))
      (setq win-start-end (cdr win-start-end)))
    win))


1090
;; Select a window that will display the point if the windows would
Karl Heuer's avatar
Karl Heuer committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
;; 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.
1103
      (vertical-motion 0 (car windows))
Karl Heuer's avatar
Karl Heuer committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
      (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)))))))
1119
    (if win
Karl Heuer's avatar
Karl Heuer committed
1120 1121 1122 1123 1124
	(select-window win))
    win))


;;}}}
1125
;;{{{ Redisplay
Karl Heuer's avatar
Karl Heuer committed
1126 1127 1128 1129 1130 1131 1132 1133

;; 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
1134
from the top.  WINDOWS should contain a list of windows to
1135
redisplay, it is assumed that WIN is a member of the list.
Karl Heuer's avatar
Karl Heuer committed
1136 1137
Should WINDOWS be nil, the windows displaying the
same buffer as WIN, in the current frame, are used.
1138
Should WIN be nil, the selected window is used."
Karl Heuer's avatar
Karl Heuer committed
1139 1140
  (or win
      (setq win (selected-window)))
1141
  (or windows
Karl Heuer's avatar
Karl Heuer committed
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 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
      (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
1186
to recenter around.  It is assumed that WIN starts at position
Karl Heuer's avatar
Karl Heuer committed
1187
START."
1188
  (or win
Karl Heuer's avatar
Karl Heuer committed
1189
      (setq win (selected-window)))
1190
  (or start
Karl Heuer's avatar
Karl Heuer committed
1191 1192 1193 1194 1195 1196
      (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)))
1197
	(cond ((= win-start start)
Karl Heuer's avatar
Karl Heuer committed
1198 1199 1200 1201
	       (follow-debug-message "exact")
	       (cdr guess))
	      ((< win-start start)
	       (follow-debug-message "above")
1202
	       (follow-calculate-first-window-start-from-above
Karl Heuer's avatar
Karl Heuer committed
1203 1204 1205
		windows (cdr guess) win start))
	      (t
	       (follow-debug-message "below")
1206
	       (follow-calculate-first-window-start-from-below
Karl Heuer's avatar
Karl Heuer committed
1207 1208 1209 1210 1211 1212 1213 1214
		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.

1215 1216
Returns (EXACT . POS).  If EXACT is non-nil, POS is the starting
position of the first window.  Otherwise it is a good guess."
Karl Heuer's avatar
Karl Heuer committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  (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.

1234
(defun follow-calculate-first-window-start-from-above
Karl Heuer's avatar
Karl Heuer committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
       (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)
1250
	      (progn
Karl Heuer's avatar
Karl Heuer committed
1251 1252 1253 1254 1255 1256 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
		(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