1 ;;; follow.el --- synchronize windows showing the same buffer
3 ;; Copyright (C) 1995-1997, 1999, 2001-2017 Free Software Foundation,
6 ;; Author: Anders Lindgren
7 ;; Maintainer: emacs-devel@gnu.org
9 ;; Keywords: display, window, minor-mode, convenience
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
28 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
29 ;; combines windows into one tall virtual window.
31 ;; The feeling of a "virtual window" has been accomplished by the use
32 ;; of two major techniques:
34 ;; * The windows always display adjacent sections of the buffer.
35 ;; This means that whenever one window is moved, all the
36 ;; others will follow. (Hence the name Follow mode.)
38 ;; * Should point (cursor) end up outside a window, another
39 ;; window displaying that point is selected, if possible. This
40 ;; makes it possible to walk between windows using normal cursor
43 ;; Follow mode comes to its prime when a large screen and two
44 ;; side-by-side window are used. The user can, with the help of Follow
45 ;; mode, use two full-height windows as though they are one.
46 ;; Imagine yourself editing a large function, or section of text,
47 ;; and being able to use 144 lines instead of the normal 72... (your
50 ;; To test this package, make sure `follow' is loaded, or will be
51 ;; autoloaded when activated (see below). Then do the following:
53 ;; * Find your favorite file (preferably a long one).
55 ;; * Resize Emacs so that it will be wide enough for two full size
56 ;; columns. Delete the other windows and split the window with
57 ;; the commands `C-x 1 C-x 3'.
59 ;; * Give the command:
60 ;; M-x follow-mode <RETURN>
62 ;; * Now the display should look something like (assuming the text "71"
65 ;; +----------+----------+
72 ;; +----------+----------+
74 ;; As you can see, the right-hand window starts at line 73, the line
75 ;; immediately below the end of the left-hand window. As long as
76 ;; `follow-mode' is active, the two windows will follow each other!
78 ;; * Play around and enjoy! Scroll one window and watch the other.
79 ;; Jump to the beginning or end. Press `Cursor down' at the last
80 ;; line of the left-hand window. Enter new lines into the
81 ;; text. Enter long lines spanning several lines, or several
84 ;; * Should you find `Follow' mode annoying, just type
85 ;; M-x follow-mode <RETURN>
89 ;; The command `follow-delete-other-windows-and-split' maximizes the
90 ;; visible area of the current buffer.
92 ;; I recommend adding it, and `follow-mode', to hotkeys in the global
93 ;; key map. To do so, add the following lines (replacing `[f7]' and
94 ;; `[f8]' with your favorite keys) to the init file:
96 ;; (global-set-key [f8] 'follow-mode)
97 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
100 ;; There exist two system variables that control the appearance of
101 ;; lines wider than the window containing them. The default is to
102 ;; truncate long lines whenever a window isn't as wide as the frame.
104 ;; To make sure lines are never truncated, please place the following
105 ;; lines in your init file:
107 ;; (setq truncate-lines nil)
108 ;; (setq truncate-partial-width-windows nil)
111 ;; The correct way to configure Follow mode, or any other mode for
112 ;; that matter, is to create one or more functions that do
113 ;; whatever you would like to do. These functions are then added to
116 ;; The keymap `follow-key-map' contains key bindings activated by
120 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
122 ;; (defun my-follow-mode-hook ()
123 ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function)
124 ;; (define-key follow-mode-map "\C-cb" 'another-function))
129 ;; To activate, issue the command "M-x follow-mode"
130 ;; and press Return. To deactivate, do it again.
132 ;; The following is a list of commands useful when follow-mode is active.
134 ;; follow-scroll-up C-c . C-v
135 ;; Scroll text in a Follow mode window chain up.
137 ;; follow-scroll-down C-c . v
138 ;; Like `follow-scroll-up', but in the other direction.
140 ;; follow-delete-other-windows-and-split C-c . 1
141 ;; Maximize the visible area of the current buffer,
142 ;; and enter Follow mode. This is a very convenient
143 ;; way to start Follow mode, hence we recommend that
144 ;; this command be added to the global keymap.
146 ;; follow-recenter C-c . C-l
147 ;; Place point in the center of the middle window,
148 ;; or a specified number of lines from either top or bottom.
150 ;; follow-switch-to-buffer C-c . b
151 ;; Switch buffer in all windows displaying the current buffer
154 ;; follow-switch-to-buffer-all C-c . C-b
155 ;; Switch buffer in all windows in the selected frame.
157 ;; follow-switch-to-current-buffer-all
158 ;; Show the current buffer in all windows on the current
159 ;; frame and turn on `follow-mode'.
161 ;; follow-first-window C-c . <
162 ;; Select the first window in the frame showing the same buffer.
164 ;; follow-last-window C-c . >
165 ;; Select the last window in the frame showing the same buffer.
167 ;; follow-next-window C-c . n
168 ;; Select the next window in the frame showing the same buffer.
170 ;; follow-previous-window C-c . p
171 ;; Select the previous window showing the same buffer.
174 ;; Well, it seems ok, but what if I really want to look at two different
175 ;; positions in the text? Here are two simple methods to use:
177 ;; 1) Use multiple frames; `follow' mode only affects windows displayed
178 ;; in the same frame. (My apologies to you who can't use frames.)
180 ;; 2) Bind `follow-mode' to key so you can turn it off whenever
181 ;; you want to view two locations. Of course, `follow' mode can
182 ;; be reactivated by hitting the same key again.
184 ;; Example from my ~/.emacs:
185 ;; (global-set-key [f8] 'follow-mode)
189 ;; The main method by which Follow mode aligns windows is via the
190 ;; function `follow-post-command-hook', which is run after each
191 ;; command. This "fixes up" the alignment of other windows which are
192 ;; showing the same Follow mode buffer, on the same frame as the
193 ;; selected window. It does not try to deal with buffers other than
194 ;; the buffer of the selected frame, or windows on other frames.
196 ;; Comint mode specially calls `follow-comint-scroll-to-bottom' on
197 ;; Follow mode buffers. This function scrolls the bottom-most window
198 ;; in a window chain and aligns the other windows accordingly. Follow
199 ;; mode adds a function to `compilation-filter-hook' to align
200 ;; compilation buffers.
205 (eval-when-compile (require 'cl-lib
))
210 "Synchronize windows showing the same buffer."
214 (defcustom follow-mode-hook nil
215 "Normal hook run by `follow-mode'."
221 ;; Define keys for the follow-mode minor mode map and replace some
222 ;; functions in the global map. All Follow mode special functions can
223 ;; be found on the `C-c .' prefix key.
225 ;; To change the prefix, redefine `follow-mode-prefix' before `follow'
226 ;; is loaded, or see the section on `follow-mode-hook' above for an
227 ;; example of how to bind the keys the way you like.
229 (defcustom follow-mode-prefix
"\C-c."
230 "Prefix key to use for follow commands in Follow mode.
231 The value of this variable is checked as part of loading Follow mode.
232 After that, changing the prefix key requires manipulating keymaps."
236 (defvar follow-mode-map
237 (let ((mainmap (make-sparse-keymap))
238 (map (make-sparse-keymap)))
239 (define-key map
"\C-v" 'follow-scroll-up
)
240 (define-key map
"\M-v" 'follow-scroll-down
)
241 (define-key map
"v" 'follow-scroll-down
)
242 (define-key map
"1" 'follow-delete-other-windows-and-split
)
243 (define-key map
"b" 'follow-switch-to-buffer
)
244 (define-key map
"\C-b" 'follow-switch-to-buffer-all
)
245 (define-key map
"\C-l" 'follow-recenter
)
246 (define-key map
"<" 'follow-first-window
)
247 (define-key map
">" 'follow-last-window
)
248 (define-key map
"n" 'follow-next-window
)
249 (define-key map
"p" 'follow-previous-window
)
251 (define-key mainmap follow-mode-prefix map
)
253 ;; Replace the standard `end-of-buffer', when in Follow mode. (I
254 ;; don't see the point in trying to replace every function that
255 ;; could be enhanced in Follow mode. End-of-buffer is a special
256 ;; case since it is very simple to define and it greatly enhances
257 ;; the look and feel of Follow mode.)
258 (define-key mainmap
[remap end-of-buffer
] 'follow-end-of-buffer
)
260 (define-key mainmap
[remap scroll-bar-toolkit-scroll
] 'follow-scroll-bar-toolkit-scroll
)
261 (define-key mainmap
[remap scroll-bar-drag
] 'follow-scroll-bar-drag
)
262 (define-key mainmap
[remap scroll-bar-scroll-up
] 'follow-scroll-bar-scroll-up
)
263 (define-key mainmap
[remap scroll-bar-scroll-down
] 'follow-scroll-bar-scroll-down
)
264 (define-key mainmap
[remap mwheel-scroll
] 'follow-mwheel-scroll
)
267 "Minor mode keymap for Follow mode.")
269 ;; When the mode is not activated, only one item is visible to activate
271 (defun follow-menu-filter (menu)
272 (if (bound-and-true-p follow-mode
)
274 '(["Follow mode" follow-mode
275 :style toggle
:selected follow-mode
])))
277 (easy-menu-add-item nil
'("Tools")
279 :filter follow-menu-filter
280 ["Scroll Up" follow-scroll-up follow-mode
]
281 ["Scroll Down" follow-scroll-down follow-mode
]
283 ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode
]
285 ["Switch To Buffer" follow-switch-to-buffer follow-mode
]
286 ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode
]
288 ["First Window" follow-first-window follow-mode
]
289 ["Last Window" follow-last-window follow-mode
]
290 ["Next Window" follow-next-window follow-mode
]
291 ["Previous Window" follow-previous-window follow-mode
]
293 ["Recenter" follow-recenter follow-mode
]
295 ["Follow mode" follow-mode
:style toggle
:selected follow-mode
]))
297 (defcustom follow-mode-line-text
" Follow"
298 "Text shown in the mode line when Follow mode is active.
299 Defaults to \" Follow\". Examples of other values
300 are \" Fw\", or simply \"\"."
304 (defcustom follow-auto nil
305 "Non-nil activates Follow mode whenever a file is loaded."
308 :set
(lambda (symbol value
)
310 (add-hook 'find-file-hook
'follow-find-file-hook t
)
311 (remove-hook 'find-file-hook
'follow-find-file-hook
))
312 (set-default symbol value
)))
314 (defvar follow-cache-command-list
315 '(next-line previous-line forward-char backward-char right-char left-char
)
316 "List of commands that don't require recalculation.
318 In order to be able to use the cache, a command should not change the
319 contents of the buffer, nor should it change selected window or current
322 The commands in this list are checked at load time.
324 To mark other commands as suitable for caching, set the symbol
325 property `follow-mode-use-cache' to non-nil.")
327 (defcustom follow-debug nil
328 "If non-nil, emit Follow mode debugging messages."
332 ;; Internal variables:
334 (defvar follow-internal-force-redisplay nil
335 "True when Follow mode should redisplay the windows.")
337 (defvar follow-active-menu nil
338 "The menu visible when Follow mode is active.")
340 (defvar follow-inactive-menu nil
341 "The menu visible when Follow mode is inactive.")
343 (defvar follow-inside-post-command-hook-call nil
344 "Non-nil when inside Follow modes `post-command-hook'.
345 Used by `follow-window-size-change'.")
347 (defvar follow-windows-start-end-cache nil
348 "Cache used by `follow-window-start-end'.")
350 (defvar follow-fixed-window nil
351 "If non-nil, the current window must not be scrolled.
352 This is typically set by explicit scrolling commands.")
355 ;; This inline function must be as small as possible!
356 ;; Maybe we should define a macro that expands to nil if
357 ;; the variable is not set.
359 (defsubst follow-debug-message
(&rest args
)
360 "Like `message', but only active when `follow-debug' is non-nil."
361 (if (and (boundp 'follow-debug
) follow-debug
)
362 (apply 'message args
)))
366 (dolist (cmd follow-cache-command-list
)
367 (put cmd
'follow-mode-use-cache t
))
372 (defun turn-on-follow-mode ()
373 "Turn on Follow mode. Please see the function `follow-mode'."
378 (defun turn-off-follow-mode ()
379 "Turn off Follow mode. Please see the function `follow-mode'."
382 (put 'follow-mode
'permanent-local t
)
384 (define-minor-mode follow-mode
386 With a prefix argument ARG, enable Follow mode if ARG is
387 positive, and disable it otherwise. If called from Lisp, enable
388 the mode if ARG is omitted or nil.
390 Follow mode is a minor mode that combines windows into one tall
391 virtual window. This is accomplished by two main techniques:
393 * The windows always displays adjacent sections of the buffer.
394 This means that whenever one window is moved, all the
395 others will follow. (Hence the name Follow mode.)
397 * Should point (cursor) end up outside a window, another
398 window displaying that point is selected, if possible. This
399 makes it possible to walk between windows using normal cursor
402 Follow mode comes to its prime when used on a large screen and two or
403 more side-by-side windows are used. The user can, with the help of
404 Follow mode, use these full-height windows as though they were one.
405 Imagine yourself editing a large function, or section of text, and
406 being able to use 144 or 216 lines instead of the normal 72... (your
409 To split one large window into two side-by-side windows, the commands
410 `\\[split-window-right]' or \
411 `\\[follow-delete-other-windows-and-split]' can be used.
413 Only windows displayed in the same frame follow each other.
415 This command runs the normal hook `follow-mode-hook'.
417 Keys specific to Follow mode:
419 :keymap follow-mode-map
422 (add-hook 'compilation-filter-hook
'follow-align-compilation-windows t t
)
423 (add-hook 'post-command-hook
'follow-post-command-hook t
)
424 (add-hook 'window-size-change-functions
'follow-window-size-change t
)
425 (add-hook 'after-change-functions
'follow-after-change nil t
)
426 (add-hook 'isearch-update-post-hook
'follow-post-command-hook nil t
)
427 (add-hook 'replace-update-post-hook
'follow-post-command-hook nil t
)
428 (add-hook 'ispell-update-post-hook
'follow-post-command-hook nil t
)
430 (when isearch-lazy-highlight
431 (setq-local isearch-lazy-highlight
'all-windows
))
433 (setq window-group-start-function
'follow-window-start
)
434 (setq window-group-end-function
'follow-window-end
)
435 (setq set-window-group-start-function
'follow-set-window-start
)
436 (setq recenter-window-group-function
'follow-recenter
)
437 (setq pos-visible-in-window-group-p-function
438 'follow-pos-visible-in-window-p
)
439 (setq selected-window-group-function
'follow-all-followers
)
440 (setq move-to-window-group-line-function
'follow-move-to-window-line
))
442 ;; Remove globally-installed hook functions only if there is no
443 ;; other Follow mode buffer.
444 (let ((buffers (buffer-list))
446 (while (and (not following
) buffers
)
447 (setq following
(buffer-local-value 'follow-mode
(car buffers
))
448 buffers
(cdr buffers
)))
450 (remove-hook 'post-command-hook
'follow-post-command-hook
)
451 (remove-hook 'window-size-change-functions
'follow-window-size-change
)))
453 (kill-local-variable 'move-to-window-group-line-function
)
454 (kill-local-variable 'selected-window-group-function
)
455 (kill-local-variable 'pos-visible-in-window-group-p-function
)
456 (kill-local-variable 'recenter-window-group-function
)
457 (kill-local-variable 'set-window-group-start-function
)
458 (kill-local-variable 'window-group-end-function
)
459 (kill-local-variable 'window-group-start-function
)
461 (remove-hook 'ispell-update-post-hook
'follow-post-command-hook t
)
462 (remove-hook 'replace-update-post-hook
'follow-post-command-hook t
)
463 (remove-hook 'isearch-update-post-hook
'follow-post-command-hook t
)
464 (remove-hook 'after-change-functions
'follow-after-change t
)
465 (remove-hook 'compilation-filter-hook
'follow-align-compilation-windows t
)))
467 (defun follow-find-file-hook ()
468 "Find-file hook for Follow mode. See the variable `follow-auto'."
469 (if follow-auto
(follow-mode 1)))
475 (defun follow-get-scrolled-point (dest windows
)
476 "Calculate the correct value for point after a scrolling operation.
478 DEST is our default position, typically where point was before the scroll.
479 If `scroll-preserve-screen-position' is non-nil and active, DEST will be
480 in the same screen position as before the scroll. WINDOWS is the list of
481 windows in the follow chain.
483 This function attempts to duplicate the point placing from
484 `window_scroll_line_based' in the Emacs core source window.c.
486 Return the new position."
487 (if (and scroll-preserve-screen-position
488 (get this-command
'scroll-command
))
494 (progn (vertical-motion 0) (current-column)))))
496 (with-selected-window (car windows
)
498 (goto-char (window-start))
502 (with-selected-window (car (reverse windows
))
504 (goto-char (window-end nil t
))
505 (if (pos-visible-in-window-p)
506 (point) ; i.e. (point-max)
510 (with-selected-window (car windows
)
513 (vertical-motion (cons dest-column
0))
516 (with-selected-window (car (reverse windows
))
519 (vertical-motion (cons dest-column
0))
523 ;; `scroll-up' and `-down', but for windows in Follow mode.
525 ;; Almost like the real thing, except when the cursor ends up outside
526 ;; the top or bottom... In our case however, we end up outside the
527 ;; window and hence we are recentered. Should we let `recenter' handle
528 ;; the point position we would never leave the selected window. To do
529 ;; it ourselves we would need to do our own redisplay, which is easier
530 ;; said than done. (Why didn't I do a real display abstraction from
533 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
534 ;; our post-command-hook will move our windows back into the old
535 ;; position... (This would also be corrected if we would have had a
536 ;; good redisplay abstraction.)
538 (defun follow-scroll-up-arg (arg)
539 "Scroll the text in a follow mode window chain up by ARG lines.
540 If ARG is nil, scroll the size of the current window.
542 This is an internal function for `follow-scroll-up' and
543 `follow-scroll-up-window'."
544 (let ((opoint (point)) (owin (selected-window)))
546 ;; If we are too near EOB, try scrolling the previous window.
547 (condition-case nil
(progn (scroll-up arg
) nil
)
549 (condition-case nil
(progn (follow-previous-window) t
)
553 (signal 'end-of-buffer nil
))))))
554 (unless (and scroll-preserve-screen-position
555 (get this-command
'scroll-command
))
557 (setq follow-fixed-window t
)))
559 (defun follow-scroll-down-arg (arg)
560 "Scroll the text in a follow mode window chain down by ARG lines.
561 If ARG is nil, scroll the size of the current window.
563 This is an internal function for `follow-scroll-down' and
564 `follow-scroll-down-window'."
565 (let ((opoint (point)))
567 (unless (and scroll-preserve-screen-position
568 (get this-command
'scroll-command
))
570 (setq follow-fixed-window t
)))
573 (defun follow-scroll-up-window (&optional arg
)
574 "Scroll text in a Follow mode window up by that window's size.
575 The other windows in the window chain will scroll synchronously.
577 If called with no ARG, the `next-screen-context-lines' last lines of
578 the window will be visible after the scroll.
580 If called with an argument, scroll ARG lines up.
581 Negative ARG means scroll downward.
583 Works like `scroll-up' when not in Follow mode."
585 (cond ((not follow-mode
)
588 (follow-scroll-down-window))
589 (t (follow-scroll-up-arg arg
))))
590 (put 'follow-scroll-up-window
'scroll-command t
)
593 (defun follow-scroll-down-window (&optional arg
)
594 "Scroll text in a Follow mode window down by that window's size.
595 The other windows in the window chain will scroll synchronously.
597 If called with no ARG, the `next-screen-context-lines' top lines of
598 the window in the chain will be visible after the scroll.
600 If called with an argument, scroll ARG lines down.
601 Negative ARG means scroll upward.
603 Works like `scroll-down' when not in Follow mode."
605 (cond ((not follow-mode
)
608 (follow-scroll-up-window))
609 (t (follow-scroll-down-arg arg
))))
610 (put 'follow-scroll-down-window
'scroll-command t
)
613 (defun follow-scroll-up (&optional arg
)
614 "Scroll text in a Follow mode window chain up.
616 If called with no ARG, the `next-screen-context-lines' last lines of
617 the bottom window in the chain will be visible in the top window.
619 If called with an argument, scroll ARG lines up.
620 Negative ARG means scroll downward.
622 Works like `scroll-up' when not in Follow mode."
624 (cond ((not follow-mode
)
626 (arg (follow-scroll-up-arg arg
))
628 (let* ((windows (follow-all-followers))
629 (end (window-end (car (reverse windows
)))))
630 (if (eq end
(point-max))
631 (signal 'end-of-buffer nil
)
632 (select-window (car windows
))
633 ;; `window-end' might return nil.
636 (vertical-motion (- next-screen-context-lines
))
637 (set-window-start (car windows
) (point)))))))
638 (put 'follow-scroll-up
'scroll-command t
)
641 (defun follow-scroll-down (&optional arg
)
642 "Scroll text in a Follow mode window chain down.
644 If called with no ARG, the `next-screen-context-lines' top lines of
645 the top window in the chain will be visible in the bottom window.
647 If called with an argument, scroll ARG lines down.
648 Negative ARG means scroll upward.
650 Works like `scroll-down' when not in Follow mode."
652 (cond ((not follow-mode
)
654 (arg (follow-scroll-down-arg arg
))
656 (let* ((windows (follow-all-followers))
657 (win (car (reverse windows
)))
658 (start (window-start (car windows
))))
659 (if (eq start
(point-min))
660 (signal 'beginning-of-buffer nil
)
663 (vertical-motion (- (- (window-height win
)
664 (if header-line-format
2 1)
665 next-screen-context-lines
)))
666 (set-window-start win
(point))
668 (vertical-motion (- next-screen-context-lines
1))
669 (setq follow-internal-force-redisplay t
))))))
670 (put 'follow-scroll-down
'scroll-command t
)
672 (declare-function comint-adjust-point
"comint" (window))
673 (defvar comint-scroll-show-maximum-output
)
675 (defun follow-comint-scroll-to-bottom (&optional _window
)
676 "Scroll the bottom-most window in the current Follow chain.
677 This is to be called by `comint-postoutput-scroll-to-bottom'."
678 (let* ((buffer (current-buffer))
679 (selected (selected-window))
680 (is-selected (eq (window-buffer) buffer
))
682 (when (or is-selected
683 (setq some-window
(get-buffer-window)))
684 (let* ((pos (progn (comint-adjust-point nil
) (point)))
687 (car (last (follow-all-followers some-window
))))))
690 (setq follow-windows-start-end-cache nil
)
691 (follow-adjust-window win
)
693 (select-window selected
)
694 (set-buffer buffer
))))))
696 (defun follow-align-compilation-windows ()
697 "Align the windows of the current Follow mode buffer.
698 This is to be called from `compilation-filter-hook'."
699 (let ((buffer (current-buffer))
700 (win (get-buffer-window))
701 (selected (selected-window)))
702 (when (and follow-mode
(waiting-for-user-input-p) win
)
703 (let ((windows (follow-all-followers win
)))
704 (unless (eq (window-buffer selected
) buffer
)
705 (setq win
(car windows
))
707 (follow-redisplay windows win t
)
708 (setq follow-windows-start-end-cache nil
)
709 (unless (eq selected win
)
710 (select-window selected
)
711 (set-buffer buffer
))))))
716 (defun follow-delete-other-windows-and-split (&optional arg
)
717 "Create two side by side windows and enter Follow mode.
719 Execute this command to display as much as possible of the text
720 in the selected window. All other windows, in the current
721 frame, are deleted and the selected window is split in two
722 side-by-side windows. Follow mode is activated, hence the
723 two windows always will display two successive pages.
724 \(If one window is moved, the other one will follow.)
726 If ARG is positive, the leftmost window is selected. If negative,
727 the rightmost is selected. If ARG is nil, the leftmost window is
728 selected if the original window is the first one in the frame."
730 (let ((other (or (and (null arg
)
731 (not (eq (selected-window)
732 (frame-first-window))))
734 (< (prefix-numeric-value arg
) 0))))
735 (start (window-start)))
736 (delete-other-windows)
741 (set-window-start (selected-window) start
)
742 (setq follow-internal-force-redisplay t
)))
745 (defun follow-switch-to-buffer (buffer)
746 "Show BUFFER in all windows in the current Follow mode window chain."
747 (interactive "BSwitch to Buffer: ")
748 (let ((orig-window (selected-window))
749 (windows (follow-all-followers)))
751 (select-window (car windows
))
752 (switch-to-buffer buffer
)
753 (setq windows
(cdr windows
)))
754 (select-window orig-window
)))
757 (defun follow-switch-to-buffer-all (&optional buffer
)
758 "Show BUFFER in all windows on this frame.
759 Defaults to current buffer."
760 (interactive (list (read-buffer "Switch to Buffer: "
762 (or buffer
(setq buffer
(current-buffer)))
763 (let ((orig-window (selected-window)))
764 (walk-windows (lambda (win)
766 (switch-to-buffer buffer
))
768 (select-window orig-window
)
772 (defun follow-switch-to-current-buffer-all ()
773 "Show current buffer in all windows on this frame, and enter Follow mode."
777 (follow-switch-to-buffer-all))
781 ;; Note, these functions are not very useful, at least not unless you
782 ;; rebind the rather cumbersome key sequence `C-c . p'.
784 (defun follow-next-window ()
785 "Select the next window showing the same buffer."
787 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
789 (select-window (car succ
))
790 (error "%s" "No more windows"))))
793 (defun follow-previous-window ()
794 "Select the previous window showing the same buffer."
796 (let ((pred (car (follow-split-followers (follow-all-followers)))))
798 (select-window (car pred
))
799 (error "%s" "No more windows"))))
802 (defun follow-first-window ()
803 "Select the first window in the frame showing the same buffer."
805 (select-window (car (follow-all-followers))))
808 (defun follow-last-window ()
809 "Select the last window in the frame showing the same buffer."
811 (select-window (car (reverse (follow-all-followers)))))
815 (defun follow-recenter (&optional arg
)
816 "Recenter the middle window around point.
817 Rearrange all other windows around the middle window.
819 With a positive argument, place the current line ARG lines
820 from the top. With a negative argument, place it -ARG lines
825 (arg (prefix-numeric-value arg
)))
827 ;; Recenter relative to the top.
829 (follow-first-window)
832 ;; Recenter relative to the bottom.
836 ;; Otherwise, our post-command-hook will move the window
838 (setq follow-internal-force-redisplay t
)))
839 ;; Recenter in the middle.
840 (let* ((dest (point))
841 (windows (follow-all-followers))
842 (win (nth (/ (- (length windows
) 1) 2) windows
)))
848 (defun follow-redraw ()
849 "Arrange windows displaying the same buffer in successor order.
850 This function can be called even if the buffer is not in Follow mode.
852 Hopefully, there should be no reason to call this function when in
853 Follow mode since the windows should always be aligned."
860 (defun follow-end-of-buffer (&optional arg
)
861 "Move point to the end of the buffer, Follow mode style.
863 If the end is not visible, it will be displayed in the last possible
864 window in the Follow mode window chain.
866 The mark is left at the previous position. With arg N, put point N/10
867 of the way from the true end."
869 (let ((followers (follow-all-followers))
872 (select-window (car (reverse followers
))))
873 ((follow-select-if-end-visible
874 (follow-windows-start-end followers
)))
876 (select-window (car (reverse followers
)))))
879 (end-of-buffer arg
))))
883 (defun follow--window-sorter (w1 w2
)
884 "Sorting function for W1 and W2 based on their positions.
885 Return non-nil if W1 is above W2; if their top-lines
886 are at the same position, return non-nil if W1 is to the
888 (let* ((edge-1 (window-pixel-edges w1
))
889 (edge-2 (window-pixel-edges w2
))
893 (< (car edge-1
) (car edge-2
))
896 (defun follow-all-followers (&optional win
)
897 "Return all windows displaying the same buffer as the WIN.
898 The list is sorted with topmost and leftmost windows first, and
899 contains only windows in the same frame as WIN. If WIN is nil,
900 it defaults to the selected window."
901 (unless (window-live-p win
)
902 (setq win
(selected-window)))
903 (let ((windows (get-buffer-window-list
904 (window-buffer win
) 'no-minibuf
(window-frame win
))))
905 (sort windows
#'follow--window-sorter
)))
907 (defun follow-split-followers (windows &optional win
)
908 "Split WINDOWS into two sets: predecessors and successors.
909 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
910 from the selected window."
912 (setq win
(selected-window)))
914 (while (not (eq (car windows
) win
))
915 (setq pred
(cons (car windows
) pred
))
916 (setq windows
(cdr windows
)))
917 (cons pred
(cdr windows
))))
919 (defun follow-calc-win-end (&optional win
)
920 "Calculate the end position for window WIN.
921 Return (END-POS END-OF-BUFFER).
923 Actually, the position returned is the start of the line after
924 the last fully-visible line in WIN. END-OF-BUFFER is t when EOB
925 is fully-visible in WIN. If WIN is nil, the selected window is
927 (let* ((win (or win
(selected-window)))
928 (edges (window-inside-pixel-edges win
))
929 (ht (- (nth 3 edges
) (nth 1 edges
)))
930 (last-line-pos (posn-point (posn-at-x-y 0 (1- ht
) win
))))
931 (if (pos-visible-in-window-p last-line-pos win
)
932 (let ((end (window-end win t
)))
933 (list end
(pos-visible-in-window-p (point-max) win
)))
934 (list last-line-pos nil
))))
936 (defun follow-calc-win-start (windows pos win
)
937 "Determine the start of window WIN in a Follow mode window chain.
938 WINDOWS is a list of chained windows, and POS is the starting
939 position for the first window in the list. If WIN is nil, return
940 the point below all windows."
941 (while (and windows
(not (eq (car windows
) win
)))
942 (let ((old-start (window-start (car windows
))))
943 ;; Can't use `save-window-excursion' since it triggers a redraw.
944 (set-window-start (car windows
) pos
'noforce
)
945 (setq pos
(car (follow-calc-win-end (car windows
))))
946 (set-window-start (car windows
) old-start
'noforce
)
947 (setq windows
(cdr windows
))))
950 ;; The result from `follow-windows-start-end' is cached when using
951 ;; a handful simple commands, like cursor movement commands.
953 (defsubst follow-cache-valid-p
(windows)
954 "Test if the cached value of `follow-windows-start-end' can be used.
955 Note that this handles the case when the cache has been set to nil."
957 (cache follow-windows-start-end-cache
))
958 (while (and res windows cache
)
959 (setq res
(and (eq (car windows
)
961 (eq (window-start (car windows
))
962 (car (cdr (car cache
))))))
963 (setq windows
(cdr windows
))
964 (setq cache
(cdr cache
)))
965 (and res
(null windows
) (null cache
))))
967 (defun follow-windows-start-end (windows)
968 "Return a list of (WIN START END BUFFER-END-P) for window list WINDOWS."
969 (if (follow-cache-valid-p windows
)
970 follow-windows-start-end-cache
971 (let ((orig-win (selected-window))
974 (select-window w
'norecord
)
975 (push (cons w
(cons (window-start) (follow-calc-win-end)))
977 (select-window orig-win
'norecord
)
978 (setq follow-windows-start-end-cache
(nreverse win-start-end
)))))
980 (defsubst follow-pos-visible
(pos win win-start-end
)
981 "Non-nil when POS is visible in WIN."
982 (let ((wstart-wend-bend (cdr (assq win win-start-end
))))
983 (and (>= pos
(car wstart-wend-bend
))
984 (or (< pos
(cadr wstart-wend-bend
))
985 (nth 2 wstart-wend-bend
)))))
988 ;; By `aligned' we mean that for all adjacent windows, the end of the
989 ;; first is equal with the start of the successor. The first window
990 ;; should start at a full screen line.
992 (defsubst follow-windows-aligned-p
(win-start-end)
993 "Non-nil if the follower windows are aligned.
994 The argument, WIN-START-END, should be a list of the form
995 returned by `follow-windows-start-end'."
997 (while (and win-start-end result
)
998 (if (cdr win-start-end
)
999 (setq result
(eq (nth 2 (car win-start-end
))
1000 (nth 1 (cadr win-start-end
)))))
1001 (setq win-start-end
(cdr win-start-end
)))
1004 ;; Check if point is visible in all windows. (So that
1005 ;; no one will be recentered.)
1007 (defun follow-point-visible-all-windows-p (win-start-end)
1008 "Non-nil when the `window-point' is visible in all windows."
1010 (while (and res win-start-end
)
1011 (setq res
(follow-pos-visible (window-point (car (car win-start-end
)))
1012 (car (car win-start-end
))
1014 (setq win-start-end
(cdr win-start-end
)))
1018 ;; Make sure WIN always starts at the beginning of a whole screen
1019 ;; line. If WIN is not aligned the start is updated which probably
1020 ;; will lead to a redisplay of the screen later on.
1022 ;; This is used with the first window in a follow chain. The reason
1023 ;; is that we want to detect that point is outside the window.
1024 ;; (Without the update, the start of the window will move as the
1025 ;; user presses BackSpace, and the other window redisplay routines
1026 ;; will move the start of the window in the wrong direction.)
1028 (defun follow-update-window-start (win)
1029 "Make sure that the start of WIN starts at a full screen line."
1031 (goto-char (window-start win
))
1033 (vertical-motion 0 win
)
1034 (unless (eq (point) (window-start win
))
1035 (vertical-motion 1 win
)
1036 (set-window-start win
(point) 'noforce
)))))
1038 (defun follow-select-if-visible (dest win-start-end
)
1039 "Select and return a window, if DEST is visible in it.
1040 Return the selected window."
1042 (while (and (not win
) win-start-end
)
1043 ;; Don't select a window that was just moved. This makes it
1044 ;; possible to later select the last window after a
1045 ;; `end-of-buffer' command.
1046 (setq wse
(car win-start-end
))
1047 (when (follow-pos-visible dest
(car wse
) win-start-end
)
1048 (setq win
(car wse
))
1049 (select-window win
))
1050 (setq win-start-end
(cdr win-start-end
)))
1053 ;; Lets select a window showing the end. Make sure we only select it if
1054 ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place
1055 ;; point in the selected window.)
1057 ;; (Compatibility kludge: in Emacs `window-end' is equal to `point-max';
1058 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1059 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1061 (defun follow-select-if-end-visible (win-start-end)
1062 "Select and return a window, if end is visible in it."
1064 (while (and (not win
) win-start-end
)
1065 ;; Don't select a window that was just moved. This makes it
1066 ;; possible to later select the last window after a `end-of-buffer'
1068 (if (and (eq (point-max) (nth 2 (car win-start-end
)))
1069 (nth 3 (car win-start-end
))
1070 ;; `window-end' might return nil.
1071 (let ((end (window-end (car (car win-start-end
)))))
1073 (eq (point-max) (min (point-max) end
)))))
1075 (setq win
(car (car win-start-end
)))
1076 (select-window win
)))
1077 (setq win-start-end
(cdr win-start-end
)))
1081 ;; Select a window that will display point if the windows would
1082 ;; be redisplayed with the first window fixed. This is useful for
1083 ;; example when the user has pressed return at the bottom of a window
1084 ;; as point is not visible in any window.
1086 (defun follow-select-if-visible-from-first (dest windows
)
1087 "Try to select one of WINDOWS without repositioning the topmost window.
1088 If one of the windows in WINDOWS contains DEST, select it, call
1089 `follow-redisplay', move point to DEST, and return that window.
1090 Otherwise, return nil."
1091 (let (win end-pos-end-p
)
1093 (goto-char (window-start (car windows
)))
1094 ;; Make sure the line start in the beginning of a real screen
1096 (vertical-motion 0 (car windows
))
1097 (when (>= dest
(point))
1098 ;; At or below the start. Check the windows.
1099 (save-window-excursion
1100 (let ((windows windows
))
1101 (while (and (not win
) windows
)
1102 (set-window-start (car windows
) (point) 'noforce
)
1103 (setq end-pos-end-p
(follow-calc-win-end (car windows
)))
1104 (goto-char (car end-pos-end-p
))
1105 ;; Visible, if dest above end, or if eob is visible
1106 ;; inside the window.
1107 (if (or (car (cdr end-pos-end-p
))
1109 (setq win
(car windows
))
1110 (setq windows
(cdr windows
))))))))
1113 (follow-redisplay windows
(car windows
))
1119 ;; Redraw all the windows on the screen, starting with the top window.
1120 ;; The window used as as marker is WIN, or the selected window if WIN
1121 ;; is nil. Start every window directly after the end of the previous
1122 ;; window, to make sure long lines are displayed correctly.
1124 (defvar follow-start-end-invalid t
1125 "When non-nil, indicates `follow-windows-start-end-cache' is invalid.")
1126 (make-variable-buffer-local 'follow-start-end-invalid
)
1128 (defun follow-redisplay (&optional windows win preserve-win
)
1129 "Reposition the WINDOWS around WIN.
1130 Should point be too close to the roof we redisplay everything
1131 from the top. WINDOWS should contain a list of windows to
1132 redisplay; it is assumed that WIN is a member of the list.
1133 Should WINDOWS be nil, the windows displaying the
1134 same buffer as WIN, in the current frame, are used.
1135 Should WIN be nil, the selected window is used.
1136 If PRESERVE-WIN is non-nil, keep WIN itself unchanged while
1137 repositioning the other windows."
1138 (or win
(setq win
(selected-window)))
1139 (or windows
(setq windows
(follow-all-followers win
)))
1140 ;; Calculate the start of the first window.
1141 (let* ((old-win-start (window-start win
))
1142 (try-first-start (follow-estimate-first-window-start
1143 windows win old-win-start
))
1144 (try-win-start (follow-calc-win-start
1145 windows try-first-start win
))
1146 (start (cond ((= try-win-start old-win-start
)
1147 (follow-debug-message "exact")
1149 ((< try-win-start old-win-start
)
1150 (follow-debug-message "above")
1151 (follow-calculate-first-window-start-from-above
1152 windows try-first-start win old-win-start
))
1154 (follow-debug-message "below")
1155 (follow-calculate-first-window-start-from-below
1156 windows try-first-start win old-win-start
)))))
1158 (unless (and preserve-win
(eq w win
))
1159 (set-window-start w start
))
1160 (setq start
(car (follow-calc-win-end w
))))
1161 (setq follow-start-end-invalid nil
)))
1163 (defun follow-estimate-first-window-start (windows win start
)
1164 "Estimate the position of the first window.
1165 The estimate is computed by assuming that the window WIN, which
1166 should be a member of WINDOWS, starts at position START."
1167 (let ((windows-before (car (follow-split-followers windows win
))))
1170 (vertical-motion 0 win
)
1171 (dolist (w windows-before
)
1172 (vertical-motion (- (window-text-height w
)) w
))
1176 ;; Find the starting point, start at GUESS and search downward.
1177 ;; The returned point is always a point below GUESS.
1179 (defun follow-calculate-first-window-start-from-above
1180 (windows guess win start
)
1187 (if (not (= (vertical-motion 1 (car windows
)) 1))
1188 ;; Hit bottom! (Can we really do this?)
1189 ;; We'll keep it, since it ensures termination.
1192 (setq res
(point-max)))
1193 (setq win-start
(follow-calc-win-start windows
(point) win
))
1194 (if (>= win-start start
)
1195 (setq done t res
(point)))))
1199 ;; Find the starting point, start at GUESS and search upward. Return
1200 ;; a point on the same line as GUESS, or above.
1202 (defun follow-calculate-first-window-start-from-below
1203 (windows guess
&optional win start
)
1204 (setq win
(or win
(selected-window)))
1205 (setq start
(or start
(window-start win
)))
1207 (let (done win-start res opoint
)
1208 ;; Always calculate what happens when no line is displayed in the first
1209 ;; window. (The `previous' res is needed below!)
1211 (vertical-motion 0 (car windows
))
1214 (setq opoint
(point))
1215 (if (not (= (vertical-motion -
1 (car windows
)) -
1))
1217 (setq done t res
(point-min))
1218 (setq win-start
(follow-calc-win-start windows
(point) win
))
1219 (cond ((>= (point) opoint
)
1220 ;; In some pathological cases, vertical-motion may
1221 ;; return -1 even though point has not decreased. In
1222 ;; that case, avoid looping forever.
1223 (setq done t res
(point)))
1224 ((= win-start start
) ; Perfect match, use this value
1225 (setq done t res
(point)))
1226 ((< win-start start
) ; Walked to far, use previous result
1228 (t ; Store result for next iteration
1229 (setq res
(point))))))
1232 ;;; Avoid tail recenter
1234 ;; This sets the window internal flag `force_start'. The effect is
1235 ;; that windows only displaying the tail aren't recentered.
1237 ;; A window displaying only the tail, is a window whose window-start
1238 ;; position is equal to (point-max) of the buffer it displays.
1240 (defun follow-avoid-tail-recenter (&rest _rest
)
1241 "Make sure windows displaying the end of a buffer aren't recentered.
1242 This is done by reading and rewriting the start position of
1243 non-first windows in Follow mode."
1244 (let* ((orig-buffer (current-buffer))
1245 (top (frame-first-window))
1247 who
) ; list of (buffer . frame)
1248 ;; If the only window in the frame is a minibuffer
1249 ;; window, `next-window' will never find it again...
1250 (unless (window-minibuffer-p top
)
1251 (while ;; look, no body!
1252 (let ((start (window-start win
))
1253 (pair (cons (window-buffer win
) (window-frame win
))))
1254 (set-buffer (window-buffer win
))
1255 (cond ((null (member pair who
))
1256 (setq who
(cons pair who
)))
1257 ((and follow-mode
(eq (point-max) start
))
1258 ;; Write the same window start back, but don't
1259 ;; set the NOFORCE flag.
1260 (set-window-start win start
)))
1261 (setq win
(next-window win
'not t
))
1262 (not (eq win top
)))) ;; Loop while this is true.
1263 (set-buffer orig-buffer
))))
1265 ;;; Post Command Hook
1267 ;; The magic little box. This function is called after every command.
1269 ;; This is not as complicated as it seems. It is simply a list of common
1270 ;; display situations and the actions to take, plus commands for redrawing
1271 ;; the screen if it should be unaligned.
1273 ;; We divide the check into two parts; whether we are at the end or not.
1274 ;; This is due to the fact that the end can actually be visible
1275 ;; in several window even though they are aligned.
1277 (defun follow-post-command-hook ()
1278 "Ensure that the windows in Follow mode are adjacent after each command."
1279 (unless (input-pending-p)
1280 (let ((follow-inside-post-command-hook-call t
)
1281 (win (selected-window)))
1282 ;; Work in the selected window, not in the current buffer.
1283 (with-current-buffer (window-buffer win
)
1284 (unless (and (symbolp this-command
)
1285 (get this-command
'follow-mode-use-cache
))
1286 (setq follow-windows-start-end-cache nil
))
1287 (follow-adjust-window win
)))))
1289 (defun follow-adjust-window (win)
1290 ;; Adjust the window WIN and its followers.
1291 (cl-assert (eq (window-buffer win
) (current-buffer)))
1292 (when (and follow-mode
1293 (not (window-minibuffer-p win
)))
1294 (let ((windows (follow-all-followers win
)))
1295 ;; If we've explicitly scrolled, align the windows first.
1296 (when follow-fixed-window
1297 (follow-debug-message "fixed")
1298 (follow-redisplay windows win
)
1299 (goto-char (follow-get-scrolled-point (point) windows
))
1300 (setq follow-fixed-window nil
))
1301 (let* ((dest (point))
1302 (win-start-end (progn
1303 (follow-update-window-start (car windows
))
1304 (follow-windows-start-end windows
)))
1305 (aligned (follow-windows-aligned-p win-start-end
))
1306 (visible (follow-pos-visible dest win win-start-end
))
1307 selected-window-up-to-date
)
1308 (unless (and aligned visible
)
1309 (setq follow-windows-start-end-cache nil
))
1311 ;; Select a window to display point.
1312 (unless follow-internal-force-redisplay
1313 (if (eq dest
(point-max))
1314 ;; Be careful at point-max: the display can be aligned
1315 ;; while DEST can be visible in several windows.
1317 ;; Select the current window, but only when the display
1318 ;; is correct. (When inserting characters in a tail
1319 ;; window, the display is not correct, as they are
1322 ;; Never stick to the current window after a deletion.
1323 ;; Otherwise, when typing `DEL' in a window showing
1324 ;; only the end of the file, a character would be
1325 ;; removed from the window above, which is very
1329 (not (memq this-command
1330 '(backward-delete-char
1331 delete-backward-char
1332 backward-delete-char-untabify
1334 (follow-debug-message "Max: same"))
1335 ;; If the end is visible, and the window doesn't
1336 ;; seems like it just has been moved, select it.
1337 ((follow-select-if-end-visible win-start-end
)
1338 (follow-debug-message "Max: end visible")
1339 (setq visible t aligned nil
)
1341 ;; Just show the end...
1343 (follow-debug-message "Max: default")
1344 (select-window (car (last windows
)))
1346 (setq visible nil aligned nil
)))
1348 ;; We're not at the end, here life is much simpler.
1350 ;; This is the normal case!
1351 ;; It should be optimized for speed.
1352 ((and visible aligned
)
1353 (follow-debug-message "same"))
1354 ;; Pick a position in any window. If the display is ok,
1355 ;; this picks the `correct' window.
1356 ((follow-select-if-visible dest win-start-end
)
1357 (follow-debug-message "visible")
1359 ;; Perform redisplay, in case line is partially visible.
1361 ;; Not visible anywhere else, lets pick this one.
1363 (follow-debug-message "visible in selected."))
1364 ;; If DEST is before the first window start, select the
1366 ((< dest
(nth 1 (car win-start-end
)))
1367 (follow-debug-message "before first")
1368 (select-window (car windows
))
1370 (setq visible nil aligned nil
))
1371 ;; If we can position the cursor without moving the first
1372 ;; window, do it. This is the case that catches `RET' at
1373 ;; the bottom of a window.
1374 ((follow-select-if-visible-from-first dest windows
)
1375 (follow-debug-message "Below first")
1376 (setq visible t aligned t
))
1377 ;; None of the above. Stick to the selected window.
1379 (follow-debug-message "None")
1380 (setq visible nil aligned nil
))))
1382 ;; If a new window was selected, make sure that the old is
1383 ;; not scrolled when point is outside the window.
1384 (unless (eq win
(selected-window))
1385 (let ((p (window-point win
)))
1386 (set-window-start win
(window-start win
) nil
)
1387 (set-window-point win p
))))
1390 ;; If point may not be visible in the selected window,
1391 ;; perform a redisplay; this ensures scrolling.
1392 (let ((opoint (point)))
1394 ;; If this `redisplay' moved point, we got clobbered by a
1395 ;; previous call to `set-window-start'. Try again.
1396 (when (/= (point) opoint
)
1400 (setq selected-window-up-to-date t
)
1401 (follow-avoid-tail-recenter)
1402 (setq win-start-end
(follow-windows-start-end windows
)
1403 follow-windows-start-end-cache nil
1406 ;; Now redraw the windows around the selected window.
1407 (unless (and (not follow-internal-force-redisplay
)
1409 (follow-windows-aligned-p win-start-end
))
1410 (follow-point-visible-all-windows-p win-start-end
))
1411 (setq follow-internal-force-redisplay nil
)
1412 (follow-redisplay windows
(selected-window)
1413 selected-window-up-to-date
)
1414 (setq win-start-end
(follow-windows-start-end windows
)
1415 follow-windows-start-end-cache nil
)
1416 ;; Point can end up in another window when DEST is at
1417 ;; the beginning of the buffer and the selected window is
1418 ;; not the first. It can also happen when long lines are
1419 ;; used and there is a big difference between the width of
1420 ;; the windows. (When scrolling one line in a wide window
1421 ;; which will cause a move larger that an entire small
1423 (unless (follow-pos-visible dest win win-start-end
)
1424 (follow-select-if-visible dest win-start-end
)
1427 ;; If the region is visible, make it look good when spanning
1428 ;; multiple windows.
1429 (when (region-active-p)
1430 (follow-maximize-region
1431 (selected-window) windows win-start-end
))))
1433 ;; Whether or not the buffer was in follow mode, update windows
1434 ;; displaying the tail so that Emacs won't recenter them.
1435 (follow-avoid-tail-recenter)))
1439 ;; Tries to make the highlighted area representing the region look
1440 ;; good when spanning several windows.
1442 ;; Not perfect, as point can't be placed at window end, only at
1443 ;; end-1. This will highlight a little bit in windows above
1446 (defun follow-maximize-region (win windows win-start-end
)
1447 "Make a highlighted region stretching multiple windows look good."
1448 (let* ((all (follow-split-followers windows win
))
1453 (setq data
(assq (car pred
) win-start-end
))
1454 (set-window-point (car pred
) (max (nth 1 data
) (- (nth 2 data
) 1)))
1455 (setq pred
(cdr pred
)))
1457 (set-window-point (car succ
) (nth 1 (assq (car succ
) win-start-end
)))
1458 (setq succ
(cdr succ
)))))
1462 ;;;; Scroll-bar support code.
1464 ;; This handles the case where the user drags the scroll bar of a
1465 ;; non-selected window whose buffer is in Follow mode.
1467 (declare-function scroll-bar-toolkit-scroll
"scroll-bar" (event))
1468 (declare-function scroll-bar-drag
"scroll-bar" (event))
1469 (declare-function scroll-bar-scroll-up
"scroll-bar" (event))
1470 (declare-function scroll-bar-scroll-down
"scroll-bar" (event))
1471 (declare-function mwheel-scroll
"mwheel" (event))
1473 (defun follow-scroll-bar-toolkit-scroll (event)
1475 (scroll-bar-toolkit-scroll event
)
1476 (follow-redraw-after-event event
))
1478 (defun follow-scroll-bar-drag (event)
1480 (scroll-bar-drag event
)
1481 (follow-redraw-after-event event
))
1483 (defun follow-scroll-bar-scroll-up (event)
1485 (scroll-bar-scroll-up event
)
1486 (follow-redraw-after-event event
))
1488 (defun follow-scroll-bar-scroll-down (event)
1490 (scroll-bar-scroll-down event
)
1491 (follow-redraw-after-event event
))
1493 (defun follow-mwheel-scroll (event)
1495 (mwheel-scroll event
)
1496 (follow-redraw-after-event event
))
1498 (defun follow-redraw-after-event (event)
1499 "Re-align the Follow mode windows affected by EVENT."
1500 (let* ((window (nth 0 (event-end event
)))
1501 (buffer (window-buffer window
))
1502 (orig-win (selected-window)))
1503 (when (and (buffer-local-value 'follow-mode buffer
)
1504 ;; Ignore the case where we scroll the selected window;
1505 ;; that is handled by the post-command hook function.
1506 (not (eq window
(selected-window))))
1507 (select-window window
)
1509 (unless (eq (window-buffer orig-win
) buffer
)
1510 (select-window orig-win
)))))
1512 ;;; Window size change
1514 ;; The functions in `window-size-change-functions' are called every
1515 ;; time a window in a frame changes size, most notably after the frame
1516 ;; has been resized. We call `follow-post-command-hook' for every
1517 ;; Follow mode buffer visible in any window in the resized frame.
1519 ;; Since `follow-window-size-change' can be called indirectly from
1520 ;; `follow-post-command-hook' we have a potential infinite loop. To
1521 ;; avoid this, we simply do not do anything in this situation. The
1522 ;; variable `follow-inside-post-command-hook-call' contains
1523 ;; information about whether the execution actually is inside the
1524 ;; post-command-hook or not.
1526 (defun follow-window-size-change (frame)
1527 "Redraw all windows in FRAME, when in Follow mode."
1528 ;; Below, we call `post-command-hook'. Avoid an infloop.
1529 (unless follow-inside-post-command-hook-call
1530 (save-current-buffer
1531 (let ((orig-frame (selected-frame)))
1532 (select-frame frame
)
1533 (let ((picked-window (selected-window)) ; Note: May change below.
1538 (let ((buf (window-buffer win
)))
1539 (unless (memq buf seen-buffers
)
1542 (let ((windows (follow-all-followers win
)))
1543 (if (not (memq picked-window windows
))
1544 (follow-redisplay windows win
)
1545 ;; Make sure we're redrawing around the selected
1547 (select-window picked-window
'norecord
)
1548 (follow-post-command-hook)
1549 (setq picked-window
(selected-window))))
1550 (push buf seen-buffers
)))))
1552 (select-window picked-window
'norecord
)))
1553 (select-frame orig-frame
)))))
1555 (add-hook 'window-scroll-functions
'follow-avoid-tail-recenter t
)
1557 ;;; Low level window start and end.
1559 ;; These routines are the Follow Mode versions of the low level
1560 ;; functions described on page "Window Start and End" of the elisp
1561 ;; manual, e.g. `window-group-start'. The aim is to be able to handle
1562 ;; Follow Mode windows by replacing `window-start' by
1563 ;; `window-group-start', etc.
1565 (defun follow-after-change (_beg _end _old-len
)
1566 "After change function: set `follow-start-end-invalid'."
1567 (setq follow-start-end-invalid t
))
1569 (defun follow-window-start (&optional window
)
1570 "Return position at which display currently starts in the
1571 Follow Mode group of windows which includes WINDOW.
1573 WINDOW must be a live window and defaults to the selected one.
1574 This is updated by redisplay or by calling
1575 `follow-set-window-start'."
1576 (let ((windows (follow-all-followers window
)))
1577 (window-start (car windows
))))
1579 (defun follow-window-end (&optional window update
)
1580 "Return position at which display currently ends in the Follow
1581 Mode group of windows which includes WINDOW.
1583 WINDOW must be a live window and defaults to the selected one.
1584 This is updated by redisplay, when it runs to completion.
1585 Simply changing the buffer text or setting `window-start' does
1586 not update this value.
1588 Return nil if there is no recorded value. (This can happen if
1589 the last redisplay of WINDOW was preempted, and did not
1590 finish.) If UPDATE is non-nil, compute the up-to-date position
1591 if it isn't already recorded."
1592 (let* ((windows (follow-all-followers window
))
1593 (last (car (last windows
))))
1594 (when (and update follow-start-end-invalid
)
1595 (follow-redisplay windows
(car windows
)))
1596 (window-end last update
)))
1598 (defun follow-set-window-start (window pos
&optional noforce
)
1599 "Make display in the Follow Mode group of windows which includes
1600 WINDOW start at position POS in WINDOW's buffer.
1602 WINDOW must be a live window and defaults to the selected one. Return
1603 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1604 overriding motion of point in order to display at this exact start."
1605 (let ((windows (follow-all-followers window
)))
1606 (setq follow-start-end-invalid t
)
1607 (set-window-start (car windows
) pos noforce
)))
1609 (defun follow-pos-visible-in-window-p (&optional pos window partially
)
1610 "Return non-nil if position POS is currently on the frame in one of
1611 the windows in the Follow Mode group which includes WINDOW.
1613 WINDOW must be a live window and defaults to the selected one.
1615 Return nil if that position is scrolled vertically out of view. If a
1616 character is only partially visible, nil is returned, unless the
1617 optional argument PARTIALLY is non-nil. If POS is only out of view
1618 because of horizontal scrolling, return non-nil. If POS is t, it
1619 specifies the position of the last visible glyph in WINDOW. POS
1620 defaults to point in WINDOW; WINDOW defaults to the selected window.
1622 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1623 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1624 where X and Y are the pixel coordinates relative to the top left corner
1625 of the actual window containing it. The remaining elements are
1626 omitted if the character after POS is fully visible; otherwise, RTOP
1627 and RBOT are the number of pixels off-window at the top and bottom of
1628 the screen line (\"row\") containing POS, ROWH is the visible height
1629 of that row, and VPOS is the row number \(zero-based)."
1630 (let* ((windows (follow-all-followers window
))
1631 (last (car (last windows
))))
1632 (when follow-start-end-invalid
1633 (follow-redisplay windows
(car windows
)))
1634 (let* ((cache (follow-windows-start-end windows
))
1635 (last-elt (car (last cache
)))
1636 our-pos pertinent-elt
)
1640 (setq our-pos
(or pos
(point)))
1643 (when (< our-pos
(nth 2 (car cache
)))
1644 (throw 'element
(car cache
)))
1645 (setq cache
(cdr cache
)))
1647 (pos-visible-in-window-p our-pos
(car pertinent-elt
) partially
))))
1649 (defun follow-move-to-window-line (arg)
1650 "Position point relative to the Follow mode group containing the selected window.
1651 ARG nil means position point at center of the window group.
1652 Else, ARG specifies vertical position within the window group;
1653 zero means top of the first window in the group, negative means
1654 relative to bottom of the last window in the group."
1655 (let* ((windows (follow-all-followers))
1656 (start-end (follow-windows-start-end windows
))
1657 (rev-start-end (reverse start-end
))
1659 middle-window elt count
)
1663 (setq rev-start-end
(nthcdr (/ (length windows
) 2) rev-start-end
))
1664 (prog1 (car (car rev-start-end
))
1665 (while (setq rev-start-end
(cdr rev-start-end
))
1666 (setq elt
(car rev-start-end
)
1667 count
(count-screen-lines (cadr elt
) (nth 2 elt
)
1669 lines
(+ lines count
)))))
1671 (while (and (cdr start-end
)
1673 (setq elt
(car start-end
)
1674 count
(count-screen-lines (cadr elt
) (nth 2 elt
)
1677 (setq arg
(- arg count
)
1678 lines
(+ lines count
)
1679 start-end
(cdr start-end
)))
1680 (car (car start-end
)))
1682 (while (and (cadr rev-start-end
)
1684 (setq elt
(car rev-start-end
)
1685 count
(count-lines (cadr elt
) (nth 2 elt
)))
1686 (<= arg
(- count
))))
1687 (setq arg
(+ arg count
)
1688 rev-start-end
(cdr rev-start-end
)))
1689 (prog1 (car (car rev-start-end
))
1690 (while (setq rev-start-end
(cdr rev-start-end
))
1691 (setq elt
(car rev-start-end
)
1692 count
(count-screen-lines (cadr elt
) (nth 2 elt
)
1694 lines
(+ lines count
)))))))
1695 (+ lines
(move-to-window-line arg
))))
1699 ;; The following (non-evaluated) section can be used to
1700 ;; profile this package using `elp'.
1702 ;; Invalid indentation on purpose!
1704 ;; (setq elp-function-list
1708 ;; follow-all-followers
1709 ;; follow-split-followers
1711 ;; follow-estimate-first-window-start
1712 ;; follow-calculate-first-window-start-from-above
1713 ;; follow-calculate-first-window-start-from-below
1714 ;; follow-calc-win-end
1715 ;; follow-calc-win-start
1716 ;; follow-pos-visible
1717 ;; follow-windows-start-end
1718 ;; follow-cache-valid-p
1719 ;; follow-select-if-visible
1720 ;; follow-select-if-visible-from-first
1721 ;; follow-windows-aligned-p
1722 ;; follow-point-visible-all-windows-p
1723 ;; follow-avoid-tail-recenter
1724 ;; follow-update-window-start
1725 ;; follow-post-command-hook))
1729 ;; /------------------------------------------------------------------------\
1730 ;; | "I [..] am rarely happier then when spending an entire day programming |
1731 ;; | my computer to perform automatically a task that it would otherwise |
1732 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
1733 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
1734 ;; | worth the investment of a day's happy activity working out a way to |
1735 ;; | save it". -- Douglas Adams, "Last Chance to See" |
1736 ;; \------------------------------------------------------------------------/
1738 ;;; follow.el ends here