* lisp/follow.el: Rework, eliminating reliance on advice.
[emacs.git] / lisp / follow.el
blob53dd4c2c3540e8e13c285f1f163d06770f09016d
1 ;;; follow.el --- synchronize windows showing the same buffer
2 ;; Copyright (C) 1995-1997, 1999, 2001-2012 Free Software Foundation, Inc.
4 ;; Author: Anders Lindgren <andersl@andersl.com>
5 ;; Maintainer: FSF (Anders' email bounces, Sep 2005)
6 ;; Created: 1995-05-25
7 ;; Keywords: display, window, minor-mode, convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
27 ;; combines windows into one tall virtual window.
29 ;; The feeling of a "virtual window" has been accomplished by the use
30 ;; of two major techniques:
32 ;; * The windows always display adjacent sections of the buffer.
33 ;; This means that whenever one window is moved, all the
34 ;; others will follow. (Hence the name Follow mode.)
36 ;; * Should the point (cursor) end up outside a window, another
37 ;; window displaying that point is selected, if possible. This
38 ;; makes it possible to walk between windows using normal cursor
39 ;; movement commands.
41 ;; Follow mode comes to its prime when a large screen and two
42 ;; side-by-side window are used. The user can, with the help of Follow
43 ;; mode, use two full-height windows as though they are one.
44 ;; Imagine yourself editing a large function, or section of text,
45 ;; and being able to use 144 lines instead of the normal 72... (your
46 ;; mileage may vary).
48 ;; To test this package, make sure `follow' is loaded, or will be
49 ;; autoloaded when activated (see below). Then do the following:
51 ;; * Find your favorite file (preferably a long one).
53 ;; * Resize Emacs so that it will be wide enough for two full size
54 ;; columns. Delete the other windows and split the window with
55 ;; the commands `C-x 1 C-x 3'.
57 ;; * Give the command:
58 ;; M-x follow-mode <RETURN>
60 ;; * Now the display should look something like (assuming the text "71"
61 ;; is on line 71):
63 ;; +----------+----------+
64 ;; |1 |73 |
65 ;; |2 |74 |
66 ;; |3 |75 |
67 ;; ... ...
68 ;; |71 |143 |
69 ;; |72 |144 |
70 ;; +----------+----------+
72 ;; As you can see, the right-hand window starts at line 73, the line
73 ;; immediately below the end of the left-hand window. As long as
74 ;; `follow-mode' is active, the two windows will follow each other!
76 ;; * Play around and enjoy! Scroll one window and watch the other.
77 ;; Jump to the beginning or end. Press `Cursor down' at the last
78 ;; line of the left-hand window. Enter new lines into the
79 ;; text. Enter long lines spanning several lines, or several
80 ;; windows.
82 ;; * Should you find `Follow' mode annoying, just type
83 ;; M-x follow-mode <RETURN>
84 ;; to turn it off.
87 ;; The command `follow-delete-other-windows-and-split' maximizes the
88 ;; visible area of the current buffer.
90 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
91 ;; key map. To do so, add the following lines (replacing `[f7]' and
92 ;; `[f8]' with your favorite keys) to the init file:
94 ;; (global-set-key [f8] 'follow-mode)
95 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
98 ;; There exist two system variables that control the appearance of
99 ;; lines wider than the window containing them. The default is to
100 ;; truncate long lines whenever a window isn't as wide as the frame.
102 ;; To make sure lines are never truncated, please place the following
103 ;; lines in your init file:
105 ;; (setq truncate-lines nil)
106 ;; (setq truncate-partial-width-windows nil)
109 ;; The correct way to configure Follow mode, or any other mode for
110 ;; that matter, is to create one or more functions that do
111 ;; whatever you would like to do. These functions are then added to
112 ;; a hook.
114 ;; The keymap `follow-key-map' contains key bindings activated by
115 ;; `follow-mode'.
117 ;; Example:
118 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
120 ;; (defun my-follow-mode-hook ()
121 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
122 ;; (define-key follow-mode-map "\C-cb" 'another-function))
125 ;; Usage:
127 ;; To activate, issue the command "M-x follow-mode"
128 ;; and press Return. To deactivate, do it again.
130 ;; The following is a list of commands useful when follow-mode is active.
132 ;; follow-scroll-up C-c . C-v
133 ;; Scroll text in a Follow mode window chain up.
135 ;; follow-scroll-down C-c . v
136 ;; Like `follow-scroll-up', but in the other direction.
138 ;; follow-delete-other-windows-and-split C-c . 1
139 ;; Maximize the visible area of the current buffer,
140 ;; and enter Follow mode. This is a very convenient
141 ;; way to start Follow mode, hence we recommend that
142 ;; this command be added to the global keymap.
144 ;; follow-recenter C-c . C-l
145 ;; Place the point in the center of the middle window,
146 ;; or a specified number of lines from either top or bottom.
148 ;; follow-switch-to-buffer C-c . b
149 ;; Switch buffer in all windows displaying the current buffer
150 ;; in this frame.
152 ;; follow-switch-to-buffer-all C-c . C-b
153 ;; Switch buffer in all windows in the selected frame.
155 ;; follow-switch-to-current-buffer-all
156 ;; Show the current buffer in all windows on the current
157 ;; frame and turn on `follow-mode'.
159 ;; follow-first-window C-c . <
160 ;; Select the first window in the frame showing the same buffer.
162 ;; follow-last-window C-c . >
163 ;; Select the last window in the frame showing the same buffer.
165 ;; follow-next-window C-c . n
166 ;; Select the next window in the frame showing the same buffer.
168 ;; follow-previous-window C-c . p
169 ;; Select the previous window showing the same buffer.
172 ;; Well, it seems ok, but what if I really want to look at two different
173 ;; positions in the text? Here are two simple methods to use:
175 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
176 ;; in the same frame. (My apologies to you who can't use frames.)
178 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
179 ;; you want to view two locations. Of course, `follow' mode can
180 ;; be reactivated by hitting the same key again.
182 ;; Example from my ~/.emacs:
183 ;; (global-set-key [f8] 'follow-mode)
185 ;; Implementation:
187 ;; The main method by which Follow mode aligns windows is via the
188 ;; function `follow-post-command-hook', which is run after each
189 ;; command. This "fixes up" the alignment of other windows which are
190 ;; showing the same Follow mode buffer, on the same frame as the
191 ;; selected window. It does not try to deal with buffers other than
192 ;; the buffer of the selected frame, or windows on other frames.
194 ;; Comint mode specially calls `follow-comint-scroll-to-bottom' on
195 ;; Follow mode buffers. This function scrolls the bottom-most window
196 ;; in a window chain and aligns the other windows accordingly. Follow
197 ;; mode adds a function to `compilation-filter-hook' to align
198 ;; compilation buffers.
200 ;;; Code:
202 (require 'easymenu)
204 ;;; Variables
206 (defgroup follow nil
207 "Synchronize windows showing the same buffer."
208 :group 'windows
209 :group 'convenience)
211 (defcustom follow-mode-hook nil
212 "Normal hook run by `follow-mode'."
213 :type 'hook
214 :group 'follow)
216 ;;; Keymap/Menu
218 ;; Define keys for the follow-mode minor mode map and replace some
219 ;; functions in the global map. All Follow mode special functions can
220 ;; be found on the `C-c .' prefix key.
222 ;; To change the prefix, redefine `follow-mode-prefix' before `follow'
223 ;; is loaded, or see the section on `follow-mode-hook' above for an
224 ;; example of how to bind the keys the way you like.
226 (defcustom follow-mode-prefix "\C-c."
227 "Prefix key to use for follow commands in Follow mode.
228 The value of this variable is checked as part of loading Follow mode.
229 After that, changing the prefix key requires manipulating keymaps."
230 :type 'string
231 :group 'follow)
233 (defvar follow-mode-map
234 (let ((mainmap (make-sparse-keymap))
235 (map (make-sparse-keymap)))
236 (define-key map "\C-v" 'follow-scroll-up)
237 (define-key map "\M-v" 'follow-scroll-down)
238 (define-key map "v" 'follow-scroll-down)
239 (define-key map "1" 'follow-delete-other-windows-and-split)
240 (define-key map "b" 'follow-switch-to-buffer)
241 (define-key map "\C-b" 'follow-switch-to-buffer-all)
242 (define-key map "\C-l" 'follow-recenter)
243 (define-key map "<" 'follow-first-window)
244 (define-key map ">" 'follow-last-window)
245 (define-key map "n" 'follow-next-window)
246 (define-key map "p" 'follow-previous-window)
248 (define-key mainmap follow-mode-prefix map)
250 ;; Replace the standard `end-of-buffer', when in Follow mode. (I
251 ;; don't see the point in trying to replace every function that
252 ;; could be enhanced in Follow mode. End-of-buffer is a special
253 ;; case since it is very simple to define and it greatly enhances
254 ;; the look and feel of Follow mode.)
255 (define-key mainmap [remap end-of-buffer] 'follow-end-of-buffer)
257 (define-key mainmap [remap scroll-bar-toolkit-scroll] 'follow-scroll-bar-toolkit-scroll)
258 (define-key mainmap [remap scroll-bar-drag] 'follow-scroll-bar-drag)
259 (define-key mainmap [remap scroll-bar-scroll-up] 'follow-scroll-bar-scroll-up)
260 (define-key mainmap [remap scroll-bar-scroll-down] 'follow-scroll-bar-scroll-down)
262 mainmap)
263 "Minor mode keymap for Follow mode.")
265 ;; When the mode is not activated, only one item is visible to activate
266 ;; the mode.
267 (defun follow-menu-filter (menu)
268 (if (bound-and-true-p follow-mode)
269 menu
270 '(["Follow mode" follow-mode
271 :style toggle :selected follow-mode])))
273 (easy-menu-add-item nil '("Tools")
274 '("Follow"
275 :filter follow-menu-filter
276 ["Scroll Up" follow-scroll-up follow-mode]
277 ["Scroll Down" follow-scroll-down follow-mode]
278 "--"
279 ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode]
280 "--"
281 ["Switch To Buffer" follow-switch-to-buffer follow-mode]
282 ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode]
283 "--"
284 ["First Window" follow-first-window follow-mode]
285 ["Last Window" follow-last-window follow-mode]
286 ["Next Window" follow-next-window follow-mode]
287 ["Previous Window" follow-previous-window follow-mode]
288 "--"
289 ["Recenter" follow-recenter follow-mode]
290 "--"
291 ["Follow mode" follow-mode :style toggle :selected follow-mode]))
293 (defcustom follow-mode-line-text " Follow"
294 "Text shown in the mode line when Follow mode is active.
295 Defaults to \" Follow\". Examples of other values
296 are \" Fw\", or simply \"\"."
297 :type 'string
298 :group 'follow)
300 (defcustom follow-auto nil
301 "Non-nil activates Follow mode whenever a file is loaded."
302 :type 'boolean
303 :group 'follow
304 :set (lambda (symbol value)
305 (if value
306 (add-hook 'find-file-hook 'follow-find-file-hook t)
307 (remove-hook 'find-file-hook 'follow-find-file-hook))
308 (set-default symbol value)))
310 (defvar follow-cache-command-list
311 '(next-line previous-line forward-char backward-char)
312 "List of commands that don't require recalculation.
314 In order to be able to use the cache, a command should not change the
315 contents of the buffer, nor should it change selected window or current
316 buffer.
318 The commands in this list are checked at load time.
320 To mark other commands as suitable for caching, set the symbol
321 property `follow-mode-use-cache' to non-nil.")
323 (defvar follow-debug nil
324 "Non-nil when debugging Follow mode.")
327 ;; Internal variables:
329 (defvar follow-internal-force-redisplay nil
330 "True when Follow mode should redisplay the windows.")
332 (defvar follow-active-menu nil
333 "The menu visible when Follow mode is active.")
335 (defvar follow-deactive-menu nil
336 "The menu visible when Follow mode is deactivated.")
338 (defvar follow-inside-post-command-hook nil
339 "Non-nil when inside Follow modes `post-command-hook'.
340 Used by `follow-window-size-change'.")
342 (defvar follow-windows-start-end-cache nil
343 "Cache used by `follow-window-start-end'.")
345 ;;; Debug messages
347 ;; This inline function must be as small as possible!
348 ;; Maybe we should define a macro that expands to nil if
349 ;; the variable is not set.
351 (defsubst follow-debug-message (&rest args)
352 "Like `message', but only active when `follow-debug' is non-nil."
353 (if (and (boundp 'follow-debug) follow-debug)
354 (apply 'message args)))
356 ;;; Cache
358 (dolist (cmd follow-cache-command-list)
359 (put cmd 'follow-mode-use-cache t))
361 ;;; The mode
363 ;;;###autoload
364 (defun turn-on-follow-mode ()
365 "Turn on Follow mode. Please see the function `follow-mode'."
366 (follow-mode 1))
369 ;;;###autoload
370 (defun turn-off-follow-mode ()
371 "Turn off Follow mode. Please see the function `follow-mode'."
372 (follow-mode -1))
374 (put 'follow-mode 'permanent-local t)
375 ;;;###autoload
376 (define-minor-mode follow-mode
377 "Toggle Follow mode.
378 With a prefix argument ARG, enable Follow mode if ARG is
379 positive, and disable it otherwise. If called from Lisp, enable
380 the mode if ARG is omitted or nil.
382 Follow mode is a minor mode that combines windows into one tall
383 virtual window. This is accomplished by two main techniques:
385 * The windows always displays adjacent sections of the buffer.
386 This means that whenever one window is moved, all the
387 others will follow. (Hence the name Follow mode.)
389 * Should the point (cursor) end up outside a window, another
390 window displaying that point is selected, if possible. This
391 makes it possible to walk between windows using normal cursor
392 movement commands.
394 Follow mode comes to its prime when used on a large screen and two
395 side-by-side windows are used. The user can, with the help of Follow
396 mode, use two full-height windows as though they would have been
397 one. Imagine yourself editing a large function, or section of text,
398 and being able to use 144 lines instead of the normal 72... (your
399 mileage may vary).
401 To split one large window into two side-by-side windows, the commands
402 `\\[split-window-right]' or \
403 `M-x follow-delete-other-windows-and-split' can be used.
405 Only windows displayed in the same frame follow each other.
407 This command runs the normal hook `follow-mode-hook'.
409 Keys specific to Follow mode:
410 \\{follow-mode-map}"
411 :keymap follow-mode-map
412 (if follow-mode
413 (progn
414 (add-hook 'compilation-filter-hook 'follow-align-compilation-windows t t)
415 (add-hook 'post-command-hook 'follow-post-command-hook t)
416 (add-hook 'window-size-change-functions 'follow-window-size-change t))
417 ;; Remove globally-installed hook functions only if there is no
418 ;; other Follow mode buffer.
419 (let ((buffers (buffer-list))
420 following)
421 (while (and (not following) buffers)
422 (setq following (buffer-local-value 'follow-mode (car buffers))
423 buffers (cdr buffers)))
424 (unless following
425 (remove-hook 'post-command-hook 'follow-post-command-hook)
426 (remove-hook 'window-size-change-functions 'follow-window-size-change)))
427 (remove-hook 'compilation-filter-hook 'follow-align-compilation-windows t)))
429 (defun follow-find-file-hook ()
430 "Find-file hook for Follow mode. See the variable `follow-auto'."
431 (if follow-auto (follow-mode 1)))
433 ;;; User functions
435 ;;; Scroll
437 ;; `scroll-up' and `-down', but for windows in Follow mode.
439 ;; Almost like the real thing, except when the cursor ends up outside
440 ;; the top or bottom... In our case however, we end up outside the
441 ;; window and hence we are recentered. Should we let `recenter' handle
442 ;; the point position we would never leave the selected window. To do
443 ;; it ourselves we would need to do our own redisplay, which is easier
444 ;; said than done. (Why didn't I do a real display abstraction from
445 ;; the beginning?)
447 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
448 ;; our post-command-hook will move our windows back into the old
449 ;; position... (This would also be corrected if we would have had a
450 ;; good redisplay abstraction.)
452 (defun follow-scroll-up (&optional arg)
453 "Scroll text in a Follow mode window chain up.
455 If called with no ARG, the `next-screen-context-lines' last lines of
456 the bottom window in the chain will be visible in the top window.
458 If called with an argument, scroll ARG lines up.
459 Negative ARG means scroll downward.
461 Works like `scroll-up' when not in Follow mode."
462 (interactive "P")
463 (cond ((not follow-mode)
464 (scroll-up arg))
465 (arg
466 (save-excursion (scroll-up arg))
467 (setq follow-internal-force-redisplay t))
469 (let* ((windows (follow-all-followers))
470 (end (window-end (car (reverse windows)))))
471 (if (eq end (point-max))
472 (signal 'end-of-buffer nil)
473 (select-window (car windows))
474 ;; `window-end' might return nil.
475 (if end
476 (goto-char end))
477 (vertical-motion (- next-screen-context-lines))
478 (set-window-start (car windows) (point)))))))
481 (defun follow-scroll-down (&optional arg)
482 "Scroll text in a Follow mode window chain down.
484 If called with no ARG, the `next-screen-context-lines' top lines of
485 the top window in the chain will be visible in the bottom window.
487 If called with an argument, scroll ARG lines down.
488 Negative ARG means scroll upward.
490 Works like `scroll-up' when not in Follow mode."
491 (interactive "P")
492 (cond ((not follow-mode)
493 (scroll-up arg))
494 (arg
495 (save-excursion (scroll-down arg)))
497 (let* ((windows (follow-all-followers))
498 (win (car (reverse windows)))
499 (start (window-start (car windows))))
500 (if (eq start (point-min))
501 (signal 'beginning-of-buffer nil)
502 (select-window win)
503 (goto-char start)
504 (vertical-motion (- (- (window-height win)
505 (if header-line-format 2 1)
506 next-screen-context-lines)))
507 (set-window-start win (point))
508 (goto-char start)
509 (vertical-motion (- next-screen-context-lines 1))
510 (setq follow-internal-force-redisplay t))))))
512 (declare-function comint-adjust-point "comint" (window))
513 (defvar comint-scroll-show-maximum-output)
515 (defun follow-comint-scroll-to-bottom (&optional window)
516 "Scroll the bottom-most window in the current Follow chain.
517 This is to be called by `comint-postoutput-scroll-to-bottom'."
518 (let* ((buffer (current-buffer))
519 (selected (selected-window))
520 (is-selected (eq (window-buffer) buffer))
521 some-window)
522 (when (or is-selected
523 (setq some-window (get-buffer-window)))
524 (let* ((pos (progn (comint-adjust-point nil) (point)))
525 (win (if is-selected
526 selected
527 (car (last (follow-all-followers some-window))))))
528 (select-window win)
529 (goto-char pos)
530 (setq follow-windows-start-end-cache nil)
531 (follow-adjust-window win pos)
532 (unless is-selected
533 (select-window selected)
534 (set-buffer buffer))))))
536 (defun follow-align-compilation-windows ()
537 "Align the windows of the current Follow mode buffer.
538 This is to be called from `compilation-filter-hook'."
539 (let ((buffer (current-buffer))
540 (win (get-buffer-window))
541 (selected (selected-window)))
542 (when (and follow-mode (waiting-for-user-input-p) win)
543 (let ((windows (follow-all-followers win)))
544 (unless (eq (window-buffer selected) buffer)
545 (setq win (car windows))
546 (select-window win))
547 (follow-redisplay windows win t)
548 (setq follow-windows-start-end-cache nil)
549 (unless (eq selected win)
550 (select-window selected)
551 (set-buffer buffer))))))
553 ;;; Buffer
555 ;;;###autoload
556 (defun follow-delete-other-windows-and-split (&optional arg)
557 "Create two side by side windows and enter Follow mode.
559 Execute this command to display as much as possible of the text
560 in the selected window. All other windows, in the current
561 frame, are deleted and the selected window is split in two
562 side-by-side windows. Follow mode is activated, hence the
563 two windows always will display two successive pages.
564 \(If one window is moved, the other one will follow.)
566 If ARG is positive, the leftmost window is selected. If negative,
567 the rightmost is selected. If ARG is nil, the leftmost window is
568 selected if the original window is the first one in the frame."
569 (interactive "P")
570 (let ((other (or (and (null arg)
571 (not (eq (selected-window)
572 (frame-first-window (selected-frame)))))
573 (and arg
574 (< (prefix-numeric-value arg) 0))))
575 (start (window-start)))
576 (delete-other-windows)
577 (split-window-right)
578 (if other
579 (progn
580 (other-window 1)
581 (set-window-start (selected-window) start)
582 (setq follow-internal-force-redisplay t)))
583 (follow-mode 1)))
585 (defun follow-switch-to-buffer (buffer)
586 "Show BUFFER in all windows in the current Follow mode window chain."
587 (interactive "BSwitch to Buffer: ")
588 (let ((orig-window (selected-window))
589 (windows (follow-all-followers)))
590 (while windows
591 (select-window (car windows))
592 (switch-to-buffer buffer)
593 (setq windows (cdr windows)))
594 (select-window orig-window)))
597 (defun follow-switch-to-buffer-all (&optional buffer)
598 "Show BUFFER in all windows on this frame.
599 Defaults to current buffer."
600 (interactive (list (read-buffer "Switch to Buffer: "
601 (current-buffer))))
602 (or buffer (setq buffer (current-buffer)))
603 (let ((orig-window (selected-window)))
604 (walk-windows (lambda (win)
605 (select-window win)
606 (switch-to-buffer buffer))
607 'no-minibuf)
608 (select-window orig-window)
609 (follow-redisplay)))
612 (defun follow-switch-to-current-buffer-all ()
613 "Show current buffer in all windows on this frame, and enter Follow mode."
614 (interactive)
615 (unless follow-mode
616 (follow-mode 1))
617 (follow-switch-to-buffer-all))
619 ;;; Movement
621 ;; Note, these functions are not very useful, at least not unless you
622 ;; rebind the rather cumbersome key sequence `C-c . p'.
624 (defun follow-next-window ()
625 "Select the next window showing the same buffer."
626 (interactive)
627 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
628 (if succ
629 (select-window (car succ))
630 (error "%s" "No more windows"))))
633 (defun follow-previous-window ()
634 "Select the previous window showing the same buffer."
635 (interactive)
636 (let ((pred (car (follow-split-followers (follow-all-followers)))))
637 (if pred
638 (select-window (car pred))
639 (error "%s" "No more windows"))))
642 (defun follow-first-window ()
643 "Select the first window in the frame showing the same buffer."
644 (interactive)
645 (select-window (car (follow-all-followers))))
648 (defun follow-last-window ()
649 "Select the last window in the frame showing the same buffer."
650 (interactive)
651 (select-window (car (reverse (follow-all-followers)))))
653 ;;; Redraw
655 (defun follow-recenter (&optional arg)
656 "Recenter the middle window around point.
657 Rearrange all other windows around the middle window.
659 With a positive argument, place the current line ARG lines
660 from the top. With a negative argument, place it -ARG lines
661 from the bottom."
662 (interactive "P")
663 (if arg
664 (let ((p (point))
665 (arg (prefix-numeric-value arg)))
666 (if (>= arg 0)
667 ;; Recenter relative to the top.
668 (progn
669 (follow-first-window)
670 (goto-char p)
671 (recenter arg))
672 ;; Recenter relative to the bottom.
673 (follow-last-window)
674 (goto-char p)
675 (recenter arg)
676 ;; Otherwise, our post-command-hook will move the window
677 ;; right back.
678 (setq follow-internal-force-redisplay t)))
679 ;; Recenter in the middle.
680 (let* ((dest (point))
681 (windows (follow-all-followers))
682 (win (nth (/ (- (length windows) 1) 2) windows)))
683 (select-window win)
684 (goto-char dest)
685 (recenter))))
688 (defun follow-redraw ()
689 "Arrange windows displaying the same buffer in successor order.
690 This function can be called even if the buffer is not in Follow mode.
692 Hopefully, there should be no reason to call this function when in
693 Follow mode since the windows should always be aligned."
694 (interactive)
695 (sit-for 0)
696 (follow-redisplay))
698 ;;; End of buffer
700 (defun follow-end-of-buffer (&optional arg)
701 "Move point to the end of the buffer, Follow mode style.
703 If the end is not visible, it will be displayed in the last possible
704 window in the Follow mode window chain.
706 The mark is left at the previous position. With arg N, put point N/10
707 of the way from the true end."
708 (interactive "P")
709 (let ((followers (follow-all-followers))
710 (pos (point)))
711 (cond (arg
712 (select-window (car (reverse followers))))
713 ((follow-select-if-end-visible
714 (follow-windows-start-end followers)))
716 (select-window (car (reverse followers)))))
717 (goto-char pos)
718 (with-no-warnings
719 (end-of-buffer arg))))
721 ;;; Display
723 (defun follow--window-sorter (w1 w2)
724 "Sorting function for W1 and W2 based on their positions.
725 Return non-nil if W1 is above W2; if their top-lines
726 are at the same position, return non-nil if W1 is to the
727 left of W2."
728 (let* ((edge-1 (window-pixel-edges w1))
729 (edge-2 (window-pixel-edges w2))
730 (y1 (nth 1 edge-1))
731 (y2 (nth 1 edge-2)))
732 (if (= y1 y2)
733 (< (car edge-1) (car edge-2))
734 (< y1 y2))))
736 (defun follow-all-followers (&optional win)
737 "Return all windows displaying the same buffer as the WIN.
738 The list is sorted with topmost and leftmost windows first, and
739 contains only windows in the same frame as WIN. If WIN is nil,
740 it defaults to the selected window."
741 (unless (window-live-p win)
742 (setq win (selected-window)))
743 (let ((buffer (window-buffer win))
744 windows)
745 (dolist (w (window-list (window-frame win) 'no-minibuf win))
746 (if (eq (window-buffer w) buffer)
747 (push w windows)))
748 (sort windows 'follow--window-sorter)))
750 (defun follow-split-followers (windows &optional win)
751 "Split WINDOWS into two sets: predecessors and successors.
752 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
753 from the selected window."
754 (or win
755 (setq win (selected-window)))
756 (let ((pred '()))
757 (while (not (eq (car windows) win))
758 (setq pred (cons (car windows) pred))
759 (setq windows (cdr windows)))
760 (cons pred (cdr windows))))
762 (defun follow-calc-win-end (&optional win)
763 "Calculate the end position for window WIN.
764 Return (END-POS END-OF-BUFFER).
766 Actually, the position returned is the start of the line after
767 the last fully-visible line in WIN. If WIN is nil, the selected
768 window is used."
769 (let* ((win (or win (selected-window)))
770 (edges (window-inside-pixel-edges win))
771 (ht (- (nth 3 edges) (nth 1 edges)))
772 (last-line-pos (posn-point (posn-at-x-y 0 (1- ht) win))))
773 (if (pos-visible-in-window-p last-line-pos win)
774 (let ((end (window-end win t)))
775 (list end (= end (point-max))))
776 (list last-line-pos nil))))
778 (defun follow-calc-win-start (windows pos win)
779 "Determine the start of window WIN in a Follow mode window chain.
780 WINDOWS is a list of chained windows, and POS is the starting
781 position for the first window in the list. If WIN is nil, return
782 the point below all windows."
783 (while (and windows (not (eq (car windows) win)))
784 (let ((old-start (window-start (car windows))))
785 ;; Can't use `save-window-excursion' since it triggers a redraw.
786 (set-window-start (car windows) pos 'noforce)
787 (setq pos (car (follow-calc-win-end (car windows))))
788 (set-window-start (car windows) old-start 'noforce)
789 (setq windows (cdr windows))))
790 pos)
792 ;; The result from `follow-windows-start-end' is cached when using
793 ;; a handful simple commands, like cursor movement commands.
795 (defsubst follow-cache-valid-p (windows)
796 "Test if the cached value of `follow-windows-start-end' can be used.
797 Note that this handles the case when the cache has been set to nil."
798 (let ((res t)
799 (cache follow-windows-start-end-cache))
800 (while (and res windows cache)
801 (setq res (and (eq (car windows)
802 (car (car cache)))
803 (eq (window-start (car windows))
804 (car (cdr (car cache))))))
805 (setq windows (cdr windows))
806 (setq cache (cdr cache)))
807 (and res (null windows) (null cache))))
809 (defun follow-windows-start-end (windows)
810 "Return a list of (WIN START END BUFFER-END-P) for window list WINDOWS."
811 (if (follow-cache-valid-p windows)
812 follow-windows-start-end-cache
813 (let ((orig-win (selected-window))
814 win-start-end)
815 (dolist (w windows)
816 (select-window w)
817 (push (cons w (cons (window-start) (follow-calc-win-end)))
818 win-start-end))
819 (select-window orig-win)
820 (setq follow-windows-start-end-cache (nreverse win-start-end)))))
822 (defsubst follow-pos-visible (pos win win-start-end)
823 "Non-nil when POS is visible in WIN."
824 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
825 (and (>= pos (car wstart-wend-bend))
826 (or (< pos (cadr wstart-wend-bend))
827 (nth 2 wstart-wend-bend)))))
830 ;; By `aligned' we mean that for all adjacent windows, the end of the
831 ;; first is equal with the start of the successor. The first window
832 ;; should start at a full screen line.
834 (defsubst follow-windows-aligned-p (win-start-end)
835 "Non-nil if the follower windows are aligned.
836 The argument, WIN-START-END, should be a list of the form
837 returned by `follow-windows-start-end'."
838 (let ((result t))
839 (while (and win-start-end result)
840 (if (cdr win-start-end)
841 (setq result (eq (nth 2 (car win-start-end))
842 (nth 1 (cadr win-start-end)))))
843 (setq win-start-end (cdr win-start-end)))
844 result))
846 ;; Check if the point is visible in all windows. (So that
847 ;; no one will be recentered.)
849 (defun follow-point-visible-all-windows-p (win-start-end)
850 "Non-nil when the `window-point' is visible in all windows."
851 (let ((res t))
852 (while (and res win-start-end)
853 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
854 (car (car win-start-end))
855 win-start-end))
856 (setq win-start-end (cdr win-start-end)))
857 res))
860 ;; Make sure WIN always starts at the beginning of a whole screen
861 ;; line. If WIN is not aligned the start is updated which probably
862 ;; will lead to a redisplay of the screen later on.
864 ;; This is used with the first window in a follow chain. The reason
865 ;; is that we want to detect that the point is outside the window.
866 ;; (Without the update, the start of the window will move as the
867 ;; user presses BackSpace, and the other window redisplay routines
868 ;; will move the start of the window in the wrong direction.)
870 (defun follow-update-window-start (win)
871 "Make sure that the start of WIN starts at a full screen line."
872 (save-excursion
873 (goto-char (window-start win))
874 (unless (bolp)
875 (vertical-motion 0 win)
876 (unless (eq (point) (window-start win))
877 (vertical-motion 1 win)
878 (set-window-start win (point) 'noforce)))))
880 (defun follow-select-if-visible (dest win-start-end)
881 "Select and return a window, if DEST is visible in it.
882 Return the selected window."
883 (let (win win-end wse)
884 (while (and (not win) win-start-end)
885 ;; Don't select a window that was just moved. This makes it
886 ;; possible to later select the last window after a
887 ;; `end-of-buffer' command.
888 (setq wse (car win-start-end))
889 (when (follow-pos-visible dest (car wse) win-start-end)
890 (setq win (car wse)
891 win-end (nth 2 wse))
892 (select-window win))
893 (setq win-start-end (cdr win-start-end)))
894 win))
896 ;; Lets select a window showing the end. Make sure we only select it if
897 ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place
898 ;; the point in the selected window.)
900 ;; (Compatibility kludge: in Emacs `window-end' is equal to `point-max';
901 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
902 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
904 (defun follow-select-if-end-visible (win-start-end)
905 "Select and return a window, if end is visible in it."
906 (let ((win nil))
907 (while (and (not win) win-start-end)
908 ;; Don't select a window that was just moved. This makes it
909 ;; possible to later select the last window after a `end-of-buffer'
910 ;; command.
911 (if (and (eq (point-max) (nth 2 (car win-start-end)))
912 (nth 3 (car win-start-end))
913 ;; `window-end' might return nil.
914 (let ((end (window-end (car (car win-start-end)))))
915 (and end
916 (eq (point-max) (min (point-max) end)))))
917 (progn
918 (setq win (car (car win-start-end)))
919 (select-window win)))
920 (setq win-start-end (cdr win-start-end)))
921 win))
924 ;; Select a window that will display the point if the windows would
925 ;; be redisplayed with the first window fixed. This is useful for
926 ;; example when the user has pressed return at the bottom of a window
927 ;; as the point is not visible in any window.
929 (defun follow-select-if-visible-from-first (dest windows)
930 "Try to select one of WINDOWS without repositioning the topmost window.
931 If one of the windows in WINDOWS contains DEST, select it, call
932 `follow-redisplay', move point to DEST, and return that window.
933 Otherwise, return nil."
934 (let (win end-pos-end-p)
935 (save-excursion
936 (goto-char (window-start (car windows)))
937 ;; Make sure the line start in the beginning of a real screen
938 ;; line.
939 (vertical-motion 0 (car windows))
940 (when (>= dest (point))
941 ;; At or below the start. Check the windows.
942 (save-window-excursion
943 (let ((windows windows))
944 (while (and (not win) windows)
945 (set-window-start (car windows) (point) 'noforce)
946 (setq end-pos-end-p (follow-calc-win-end (car windows)))
947 (goto-char (car end-pos-end-p))
948 ;; Visible, if dest above end, or if eob is visible
949 ;; inside the window.
950 (if (or (car (cdr end-pos-end-p))
951 (< dest (point)))
952 (setq win (car windows))
953 (setq windows (cdr windows))))))))
954 (when win
955 (select-window win)
956 (follow-redisplay windows (car windows))
957 (goto-char dest))
958 win))
960 ;;; Redisplay
962 ;; Redraw all the windows on the screen, starting with the top window.
963 ;; The window used as as marker is WIN, or the selected window if WIN
964 ;; is nil. Start every window directly after the end of the previous
965 ;; window, to make sure long lines are displayed correctly.
967 (defun follow-redisplay (&optional windows win preserve-win)
968 "Reposition the WINDOWS around WIN.
969 Should the point be too close to the roof we redisplay everything
970 from the top. WINDOWS should contain a list of windows to
971 redisplay; it is assumed that WIN is a member of the list.
972 Should WINDOWS be nil, the windows displaying the
973 same buffer as WIN, in the current frame, are used.
974 Should WIN be nil, the selected window is used.
975 If PRESERVE-WIN is non-nil, keep WIN itself unchanged while
976 repositioning the other windows."
977 (or win (setq win (selected-window)))
978 (or windows (setq windows (follow-all-followers win)))
979 ;; Calculate the start of the first window.
980 (let* ((old-win-start (window-start win))
981 (try-first-start (follow-estimate-first-window-start
982 windows win old-win-start))
983 (try-win-start (follow-calc-win-start
984 windows try-first-start win))
985 (start (cond ((= try-win-start old-win-start)
986 (follow-debug-message "exact")
987 try-first-start)
988 ((< try-win-start old-win-start)
989 (follow-debug-message "above")
990 (follow-calculate-first-window-start-from-above
991 windows try-first-start win old-win-start))
993 (follow-debug-message "below")
994 (follow-calculate-first-window-start-from-below
995 windows try-first-start win old-win-start)))))
996 (dolist (w windows)
997 (unless (and preserve-win (eq w win))
998 (set-window-start w start))
999 (setq start (car (follow-calc-win-end w))))))
1001 (defun follow-estimate-first-window-start (windows win start)
1002 "Estimate the position of the first window.
1003 The estimate is computed by assuming that the window WIN, which
1004 should be a member of WINDOWS, starts at position START."
1005 (let ((windows-before (car (follow-split-followers windows win))))
1006 (save-excursion
1007 (goto-char start)
1008 (vertical-motion 0 win)
1009 (dolist (w windows-before)
1010 (vertical-motion (- 1 (window-text-height w)) w))
1011 (point))))
1014 ;; Find the starting point, start at GUESS and search downward.
1015 ;; The returned point is always a point below GUESS.
1017 (defun follow-calculate-first-window-start-from-above
1018 (windows guess win start)
1019 (save-excursion
1020 (let ((done nil)
1021 win-start
1022 res)
1023 (goto-char guess)
1024 (while (not done)
1025 (if (not (= (vertical-motion 1 (car windows)) 1))
1026 ;; Hit bottom! (Can we really do this?)
1027 ;; We'll keep it, since it ensures termination.
1028 (progn
1029 (setq done t)
1030 (setq res (point-max)))
1031 (setq win-start (follow-calc-win-start windows (point) win))
1032 (if (>= win-start start)
1033 (setq done t res (point)))))
1034 res)))
1037 ;; Find the starting point, start at GUESS and search upward. Return
1038 ;; a point on the same line as GUESS, or above.
1040 (defun follow-calculate-first-window-start-from-below
1041 (windows guess &optional win start)
1042 (setq win (or win (selected-window)))
1043 (setq start (or start (window-start win)))
1044 (save-excursion
1045 (let (done win-start res opoint)
1046 ;; Always calculate what happens when no line is displayed in the first
1047 ;; window. (The `previous' res is needed below!)
1048 (goto-char guess)
1049 (vertical-motion 0 (car windows))
1050 (setq res (point))
1051 (while (not done)
1052 (setq opoint (point))
1053 (if (not (= (vertical-motion -1 (car windows)) -1))
1054 ;; Hit roof!
1055 (setq done t res (point-min))
1056 (setq win-start (follow-calc-win-start windows (point) win))
1057 (cond ((>= (point) opoint)
1058 ;; In some pathological cases, vertical-motion may
1059 ;; return -1 even though point has not decreased. In
1060 ;; that case, avoid looping forever.
1061 (setq done t res (point)))
1062 ((= win-start start) ; Perfect match, use this value
1063 (setq done t res (point)))
1064 ((< win-start start) ; Walked to far, use previous result
1065 (setq done t))
1066 (t ; Store result for next iteration
1067 (setq res (point))))))
1068 res)))
1070 ;;; Avoid tail recenter
1072 ;; This sets the window internal flag `force_start'. The effect is
1073 ;; that windows only displaying the tail aren't recentered.
1075 ;; A window displaying only the tail, is a window whose window-start
1076 ;; position is equal to (point-max) of the buffer it displays.
1078 (defun follow-avoid-tail-recenter (&rest _rest)
1079 "Make sure windows displaying the end of a buffer aren't recentered.
1080 This is done by reading and rewriting the start position of
1081 non-first windows in Follow mode."
1082 (let* ((orig-buffer (current-buffer))
1083 (top (frame-first-window (selected-frame)))
1084 (win top)
1085 who) ; list of (buffer . frame)
1086 ;; If the only window in the frame is a minibuffer
1087 ;; window, `next-window' will never find it again...
1088 (unless (window-minibuffer-p top)
1089 (while ;; look, no body!
1090 (let ((start (window-start win))
1091 (pair (cons (window-buffer win) (window-frame win))))
1092 (set-buffer (window-buffer win))
1093 (cond ((null (member pair who))
1094 (setq who (cons pair who)))
1095 ((and follow-mode (eq (point-max) start))
1096 ;; Write the same window start back, but don't
1097 ;; set the NOFORCE flag.
1098 (set-window-start win start)))
1099 (setq win (next-window win 'not t))
1100 (not (eq win top)))) ;; Loop while this is true.
1101 (set-buffer orig-buffer))))
1103 ;;; Post Command Hook
1105 ;; The magic little box. This function is called after every command.
1107 ;; This is not as complicated as it seems. It is simply a list of common
1108 ;; display situations and the actions to take, plus commands for redrawing
1109 ;; the screen if it should be unaligned.
1111 ;; We divide the check into two parts; whether we are at the end or not.
1112 ;; This is due to the fact that the end can actually be visible
1113 ;; in several window even though they are aligned.
1115 (defun follow-post-command-hook ()
1116 "Ensure that the windows in Follow mode are adjacent after each command."
1117 (unless (input-pending-p)
1118 (let ((follow-inside-post-command-hook t)
1119 (win (selected-window)))
1120 ;; Work in the selected window, not in the current buffer.
1121 (with-current-buffer (window-buffer win)
1122 (unless (and (symbolp this-command)
1123 (get this-command 'follow-mode-use-cache))
1124 (setq follow-windows-start-end-cache nil)))
1125 (follow-adjust-window win (point)))))
1127 (defun follow-adjust-window (win dest)
1128 ;; Adjust the window WIN and its followers.
1129 (with-current-buffer (window-buffer win)
1130 (when (and follow-mode
1131 (not (window-minibuffer-p win)))
1132 (let* ((windows (follow-all-followers win))
1133 (win-start-end (progn
1134 (follow-update-window-start (car windows))
1135 (follow-windows-start-end windows)))
1136 (aligned (follow-windows-aligned-p win-start-end))
1137 (visible (follow-pos-visible dest win win-start-end))
1138 selected-window-up-to-date)
1139 (unless (and aligned visible)
1140 (setq follow-windows-start-end-cache nil))
1142 ;; Select a window to display point.
1143 (unless follow-internal-force-redisplay
1144 (if (eq dest (point-max))
1145 ;; Be careful at point-max: the display can be aligned
1146 ;; while DEST can be visible in several windows.
1147 (cond
1148 ;; Select the current window, but only when the display
1149 ;; is correct. (When inserting characters in a tail
1150 ;; window, the display is not correct, as they are
1151 ;; shown twice.)
1153 ;; Never stick to the current window after a deletion.
1154 ;; Otherwise, when typing `DEL' in a window showing
1155 ;; only the end of the file, a character would be
1156 ;; removed from the window above, which is very
1157 ;; unintuitive.
1158 ((and visible
1159 aligned
1160 (not (memq this-command
1161 '(backward-delete-char
1162 delete-backward-char
1163 backward-delete-char-untabify
1164 kill-region))))
1165 (follow-debug-message "Max: same"))
1166 ;; If the end is visible, and the window doesn't
1167 ;; seems like it just has been moved, select it.
1168 ((follow-select-if-end-visible win-start-end)
1169 (follow-debug-message "Max: end visible")
1170 (setq visible t aligned nil)
1171 (goto-char dest))
1172 ;; Just show the end...
1174 (follow-debug-message "Max: default")
1175 (select-window (car (last windows)))
1176 (goto-char dest)
1177 (setq visible nil aligned nil)))
1179 ;; We're not at the end, here life is much simpler.
1180 (cond
1181 ;; This is the normal case!
1182 ;; It should be optimized for speed.
1183 ((and visible aligned)
1184 (follow-debug-message "same"))
1185 ;; Pick a position in any window. If the display is ok,
1186 ;; this picks the `correct' window.
1187 ((follow-select-if-visible dest win-start-end)
1188 (follow-debug-message "visible")
1189 (goto-char dest)
1190 ;; Perform redisplay, in case line is partially visible.
1191 (setq visible nil))
1192 ;; Not visible anywhere else, lets pick this one.
1193 (visible
1194 (follow-debug-message "visible in selected."))
1195 ;; If DEST is before the first window start, select the
1196 ;; first window.
1197 ((< dest (nth 1 (car win-start-end)))
1198 (follow-debug-message "before first")
1199 (select-window (car windows))
1200 (goto-char dest)
1201 (setq visible nil aligned nil))
1202 ;; If we can position the cursor without moving the first
1203 ;; window, do it. This is the case that catches `RET' at
1204 ;; the bottom of a window.
1205 ((follow-select-if-visible-from-first dest windows)
1206 (follow-debug-message "Below first")
1207 (setq visible t aligned t))
1208 ;; None of the above. Stick to the selected window.
1210 (follow-debug-message "None")
1211 (setq visible nil aligned nil))))
1213 ;; If a new window was selected, make sure that the old is
1214 ;; not scrolled when the point is outside the window.
1215 (unless (eq win (selected-window))
1216 (let ((p (window-point win)))
1217 (set-window-start win (window-start win) nil)
1218 (set-window-point win p))))
1220 (unless visible
1221 ;; If point may not be visible in the selected window,
1222 ;; perform a redisplay; this ensures scrolling.
1223 (let ((opoint (point)))
1224 (redisplay)
1225 ;; If this `redisplay' moved point, we got clobbered by a
1226 ;; previous call to `set-window-start'. Try again.
1227 (when (/= (point) opoint)
1228 (goto-char opoint)
1229 (redisplay)))
1231 (setq selected-window-up-to-date t)
1232 (follow-avoid-tail-recenter)
1233 (setq win-start-end (follow-windows-start-end windows)
1234 follow-windows-start-end-cache nil
1235 aligned nil))
1237 ;; Now redraw the windows around the selected window.
1238 (unless (and (not follow-internal-force-redisplay)
1239 (or aligned
1240 (follow-windows-aligned-p win-start-end))
1241 (follow-point-visible-all-windows-p win-start-end))
1242 (setq follow-internal-force-redisplay nil)
1243 (follow-redisplay windows (selected-window)
1244 selected-window-up-to-date)
1245 (setq win-start-end (follow-windows-start-end windows)
1246 follow-windows-start-end-cache nil)
1247 ;; The point can ends up in another window when DEST is at
1248 ;; the beginning of the buffer and the selected window is
1249 ;; not the first. It can also happen when long lines are
1250 ;; used and there is a big difference between the width of
1251 ;; the windows. (When scrolling one line in a wide window
1252 ;; which will cause a move larger that an entire small
1253 ;; window.)
1254 (unless (follow-pos-visible dest win win-start-end)
1255 (follow-select-if-visible dest win-start-end)
1256 (goto-char dest)))
1258 ;; If the region is visible, make it look good when spanning
1259 ;; multiple windows.
1260 (when (region-active-p)
1261 (follow-maximize-region
1262 (selected-window) windows win-start-end)))
1264 ;; Whether or not the buffer was in follow mode, update windows
1265 ;; displaying the tail so that Emacs won't recenter them.
1266 (follow-avoid-tail-recenter))))
1268 ;;; The region
1270 ;; Tries to make the highlighted area representing the region look
1271 ;; good when spanning several windows.
1273 ;; Not perfect, as the point can't be placed at window end, only at
1274 ;; end-1. This will highlight a little bit in windows above
1275 ;; the current.
1277 (defun follow-maximize-region (win windows win-start-end)
1278 "Make a highlighted region stretching multiple windows look good."
1279 (let* ((all (follow-split-followers windows win))
1280 (pred (car all))
1281 (succ (cdr all))
1282 data)
1283 (while pred
1284 (setq data (assq (car pred) win-start-end))
1285 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1286 (setq pred (cdr pred)))
1287 (while succ
1288 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1289 (setq succ (cdr succ)))))
1291 ;;; Scroll bar
1293 ;;;; Scroll-bar support code.
1295 ;; This handles the case where the user drags the scroll bar of a
1296 ;; non-selected window whose buffer is in Follow mode.
1298 (defun follow-scroll-bar-toolkit-scroll (event)
1299 (interactive "e")
1300 (scroll-bar-toolkit-scroll event)
1301 (follow-redraw-after-event event))
1303 (defun follow-scroll-bar-drag (event)
1304 (interactive "e")
1305 (scroll-bar-drag event)
1306 (follow-redraw-after-event event))
1308 (defun follow-scroll-bar-scroll-up (event)
1309 (interactive "e")
1310 (scroll-bar-scroll-up event)
1311 (follow-redraw-after-event event))
1313 (defun follow-scroll-bar-scroll-down (event)
1314 (interactive "e")
1315 (scroll-bar-scroll-down event)
1316 (follow-redraw-after-event event))
1318 (defun follow-redraw-after-event (event)
1319 "Re-align the Follow mode windows acted on by EVENT."
1320 (let ((window (nth 0 (event-end event)))
1321 (orig-win (selected-window)))
1322 (when (and (buffer-local-value 'follow-mode (window-buffer window))
1323 ;; Ignore the case where we scroll the selected window;
1324 ;; that is handled by the post-command hook function.
1325 (not (eq window (selected-window))))
1326 (select-window window)
1327 (follow-redisplay)
1328 (select-window orig-win))))
1330 ;;; Window size change
1332 ;; The functions in `window-size-change-functions' are called every
1333 ;; time a window in a frame changes size, most notably after the frame
1334 ;; has been resized. We call `follow-post-command-hook' for every
1335 ;; Follow mode buffer visible in any window in the resized frame.
1337 ;; Since `follow-window-size-change' can be called indirectly from
1338 ;; `follow-post-command-hook' we have a potential infinite loop. To
1339 ;; avoid this, we simply do not do anything in this situation. The
1340 ;; variable `follow-inside-post-command-hook' contains information
1341 ;; about whether the execution actually is inside the
1342 ;; post-command-hook or not.
1344 (defun follow-window-size-change (frame)
1345 "Redraw all windows in FRAME, when in Follow mode."
1346 ;; Below, we call `post-command-hook'. Avoid an infloop.
1347 (unless follow-inside-post-command-hook
1348 (let ((buffers '())
1349 (orig-window (selected-window))
1350 (orig-buffer (current-buffer))
1351 (orig-frame (selected-frame))
1352 windows
1353 buf)
1354 (select-frame frame)
1355 (unwind-protect
1356 (walk-windows
1357 (lambda (win)
1358 (setq buf (window-buffer win))
1359 (unless (memq buf buffers)
1360 (set-buffer buf)
1361 (when follow-mode
1362 (setq windows (follow-all-followers win))
1363 (if (not (memq orig-window windows))
1364 (follow-redisplay windows win)
1365 ;; Make sure we're redrawing around the selected
1366 ;; window.
1367 (select-window orig-window)
1368 (follow-post-command-hook)
1369 (setq orig-window (selected-window)))
1370 (setq buffers (cons buf buffers)))))
1371 'no-minibuf)
1372 (select-frame orig-frame)
1373 (set-buffer orig-buffer)
1374 (select-window orig-window)))))
1376 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t)
1378 ;;; Profile support
1380 ;; The following (non-evaluated) section can be used to
1381 ;; profile this package using `elp'.
1383 ;; Invalid indentation on purpose!
1385 ;; (setq elp-function-list
1386 ;; '(window-end
1387 ;; vertical-motion
1388 ;; follow-mode
1389 ;; follow-all-followers
1390 ;; follow-split-followers
1391 ;; follow-redisplay
1392 ;; follow-estimate-first-window-start
1393 ;; follow-calculate-first-window-start-from-above
1394 ;; follow-calculate-first-window-start-from-below
1395 ;; follow-calc-win-end
1396 ;; follow-calc-win-start
1397 ;; follow-pos-visible
1398 ;; follow-windows-start-end
1399 ;; follow-cache-valid-p
1400 ;; follow-select-if-visible
1401 ;; follow-select-if-visible-from-first
1402 ;; follow-windows-aligned-p
1403 ;; follow-point-visible-all-windows-p
1404 ;; follow-avoid-tail-recenter
1405 ;; follow-update-window-start
1406 ;; follow-post-command-hook))
1408 (provide 'follow)
1410 ;; /------------------------------------------------------------------------\
1411 ;; | "I [..] am rarely happier then when spending an entire day programming |
1412 ;; | my computer to perform automatically a task that it would otherwise |
1413 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
1414 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
1415 ;; | worth the investment of a day's happy activity working out a way to |
1416 ;; | save it". -- Douglas Adams, "Last Chance to See" |
1417 ;; \------------------------------------------------------------------------/
1419 ;;; follow.el ends here