; Revert "Prohibit multibyte characters in url-http-data"
[emacs.git] / lisp / follow.el
blobc510e5a848beeed12a9004cc135ebea7c4bd2d26
1 ;;; follow.el --- synchronize windows showing the same buffer
3 ;; Copyright (C) 1995-1997, 1999, 2001-2016 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Anders Lindgren
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Created: 1995-05-25
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 <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
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
41 ;; movement commands.
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
48 ;; mileage may vary).
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"
63 ;; is on line 71):
65 ;; +----------+----------+
66 ;; |1 |73 |
67 ;; |2 |74 |
68 ;; |3 |75 |
69 ;; ... ...
70 ;; |71 |143 |
71 ;; |72 |144 |
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
82 ;; windows.
84 ;; * Should you find `Follow' mode annoying, just type
85 ;; M-x follow-mode <RETURN>
86 ;; to turn it off.
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
114 ;; a hook.
116 ;; The keymap `follow-key-map' contains key bindings activated by
117 ;; `follow-mode'.
119 ;; Example:
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))
127 ;; Usage:
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
152 ;; in this frame.
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)
187 ;; Implementation:
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.
202 ;;; Code:
204 (require 'easymenu)
205 (eval-when-compile (require 'cl-lib))
207 ;;; Variables
209 (defgroup follow nil
210 "Synchronize windows showing the same buffer."
211 :group 'windows
212 :group 'convenience)
214 (defcustom follow-mode-hook nil
215 "Normal hook run by `follow-mode'."
216 :type 'hook
217 :group 'follow)
219 ;;; Keymap/Menu
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."
233 :type 'string
234 :group 'follow)
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)
266 mainmap)
267 "Minor mode keymap for Follow mode.")
269 ;; When the mode is not activated, only one item is visible to activate
270 ;; the mode.
271 (defun follow-menu-filter (menu)
272 (if (bound-and-true-p follow-mode)
273 menu
274 '(["Follow mode" follow-mode
275 :style toggle :selected follow-mode])))
277 (easy-menu-add-item nil '("Tools")
278 '("Follow"
279 :filter follow-menu-filter
280 ["Scroll Up" follow-scroll-up follow-mode]
281 ["Scroll Down" follow-scroll-down follow-mode]
282 "--"
283 ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode]
284 "--"
285 ["Switch To Buffer" follow-switch-to-buffer follow-mode]
286 ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode]
287 "--"
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]
292 "--"
293 ["Recenter" follow-recenter follow-mode]
294 "--"
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 \"\"."
301 :type 'string
302 :group 'follow)
304 (defcustom follow-auto nil
305 "Non-nil activates Follow mode whenever a file is loaded."
306 :type 'boolean
307 :group 'follow
308 :set (lambda (symbol value)
309 (if 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
320 buffer.
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."
329 :type 'boolean
330 :group 'follow)
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 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.")
353 ;;; Debug messages
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)))
364 ;;; Cache
366 (dolist (cmd follow-cache-command-list)
367 (put cmd 'follow-mode-use-cache t))
369 ;;; The mode
371 ;;;###autoload
372 (defun turn-on-follow-mode ()
373 "Turn on Follow mode. Please see the function `follow-mode'."
374 (follow-mode 1))
377 ;;;###autoload
378 (defun turn-off-follow-mode ()
379 "Turn off Follow mode. Please see the function `follow-mode'."
380 (follow-mode -1))
382 (put 'follow-mode 'permanent-local t)
383 ;;;###autoload
384 (define-minor-mode follow-mode
385 "Toggle 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
400 movement commands.
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
407 mileage may vary).
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:
418 \\{follow-mode-map}"
419 :keymap follow-mode-map
420 (if follow-mode
421 (progn
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 (setq window-group-start-function 'follow-window-start)
431 (setq window-group-end-function 'follow-window-end)
432 (setq set-window-group-start-function 'follow-set-window-start)
433 (setq recenter-window-group-function 'follow-recenter)
434 (setq pos-visible-in-window-group-p-function
435 'follow-pos-visible-in-window-p)
436 (setq selected-window-group-function 'follow-all-followers)
437 (setq move-to-window-group-line-function 'follow-move-to-window-line))
439 ;; Remove globally-installed hook functions only if there is no
440 ;; other Follow mode buffer.
441 (let ((buffers (buffer-list))
442 following)
443 (while (and (not following) buffers)
444 (setq following (buffer-local-value 'follow-mode (car buffers))
445 buffers (cdr buffers)))
446 (unless following
447 (remove-hook 'post-command-hook 'follow-post-command-hook)
448 (remove-hook 'window-size-change-functions 'follow-window-size-change)))
450 (kill-local-variable 'move-to-window-group-line-function)
451 (kill-local-variable 'selected-window-group-function)
452 (kill-local-variable 'pos-visible-in-window-group-p-function)
453 (kill-local-variable 'recenter-window-group-function)
454 (kill-local-variable 'set-window-group-start-function)
455 (kill-local-variable 'window-group-end-function)
456 (kill-local-variable 'window-group-start-function)
458 (remove-hook 'ispell-update-post-hook 'follow-post-command-hook t)
459 (remove-hook 'replace-update-post-hook 'follow-post-command-hook t)
460 (remove-hook 'isearch-update-post-hook 'follow-post-command-hook t)
461 (remove-hook 'after-change-functions 'follow-after-change t)
462 (remove-hook 'compilation-filter-hook 'follow-align-compilation-windows t)))
464 (defun follow-find-file-hook ()
465 "Find-file hook for Follow mode. See the variable `follow-auto'."
466 (if follow-auto (follow-mode 1)))
468 ;;; User functions
470 ;;; Scroll
472 (defun follow-get-scrolled-point (dest windows)
473 "Calculate the correct value for point after a scrolling operation.
475 DEST is our default position, typically where point was before the scroll.
476 If `scroll-preserve-screen-position' is non-nil and active, DEST will be
477 in the same screen position as before the scroll. WINDOWS is the list of
478 windows in the follow chain.
480 This function attempts to duplicate the point placing from
481 `window_scroll_line_based' in the Emacs core source window.c.
483 Return the new position."
484 (if (and scroll-preserve-screen-position
485 (get this-command 'scroll-command))
486 dest
487 (let ((dest-column
488 (save-excursion
489 (goto-char dest)
490 (- (current-column)
491 (progn (vertical-motion 0) (current-column)))))
492 (limit0
493 (with-selected-window (car windows)
494 (save-excursion
495 (goto-char (window-start))
496 (vertical-motion 0)
497 (point))))
498 (limitn
499 (with-selected-window (car (reverse windows))
500 (save-excursion
501 (goto-char (window-end nil t))
502 (if (pos-visible-in-window-p)
503 (point) ; i.e. (point-max)
504 (1- (point)))))))
505 (cond
506 ((< dest limit0)
507 (with-selected-window (car windows)
508 (save-excursion
509 (goto-char limit0)
510 (vertical-motion (cons dest-column 0))
511 (point))))
512 ((> dest limitn)
513 (with-selected-window (car (reverse windows))
514 (save-excursion
515 (goto-char limitn)
516 (vertical-motion (cons dest-column 0))
517 (point))))
518 (t dest)))))
520 ;; `scroll-up' and `-down', but for windows in Follow mode.
522 ;; Almost like the real thing, except when the cursor ends up outside
523 ;; the top or bottom... In our case however, we end up outside the
524 ;; window and hence we are recentered. Should we let `recenter' handle
525 ;; the point position we would never leave the selected window. To do
526 ;; it ourselves we would need to do our own redisplay, which is easier
527 ;; said than done. (Why didn't I do a real display abstraction from
528 ;; the beginning?)
530 ;; We must sometimes set `follow-internal-force-redisplay', otherwise
531 ;; our post-command-hook will move our windows back into the old
532 ;; position... (This would also be corrected if we would have had a
533 ;; good redisplay abstraction.)
535 (defun follow-scroll-up-arg (arg)
536 "Scroll the text in a follow mode window chain up by ARG lines.
537 If ARG is nil, scroll the size of the current window.
539 This is an internal function for `follow-scroll-up' and
540 `follow-scroll-up-window'."
541 (let ((opoint (point)) (owin (selected-window)))
542 (while
543 ;; If we are too near EOB, try scrolling the previous window.
544 (condition-case nil (progn (scroll-up arg) nil)
545 (end-of-buffer
546 (condition-case nil (progn (follow-previous-window) t)
547 (error
548 (select-window owin)
549 (goto-char opoint)
550 (signal 'end-of-buffer nil))))))
551 (unless (and scroll-preserve-screen-position
552 (get this-command 'scroll-command))
553 (goto-char opoint))
554 (setq follow-fixed-window t)))
556 (defun follow-scroll-down-arg (arg)
557 "Scroll the text in a follow mode window chain down by ARG lines.
558 If ARG is nil, scroll the size of the current window.
560 This is an internal function for `follow-scroll-down' and
561 `follow-scroll-down-window'."
562 (let ((opoint (point)))
563 (scroll-down arg)
564 (unless (and scroll-preserve-screen-position
565 (get this-command 'scroll-command))
566 (goto-char opoint))
567 (setq follow-fixed-window t)))
569 ;;;###autoload
570 (defun follow-scroll-up-window (&optional arg)
571 "Scroll text in a Follow mode window up by that window's size.
572 The other windows in the window chain will scroll synchronously.
574 If called with no ARG, the `next-screen-context-lines' last lines of
575 the window will be visible after the scroll.
577 If called with an argument, scroll ARG lines up.
578 Negative ARG means scroll downward.
580 Works like `scroll-up' when not in Follow mode."
581 (interactive "P")
582 (cond ((not follow-mode)
583 (scroll-up arg))
584 ((eq arg '-)
585 (follow-scroll-down-window))
586 (t (follow-scroll-up-arg arg))))
587 (put 'follow-scroll-up-window 'scroll-command t)
589 ;;;###autoload
590 (defun follow-scroll-down-window (&optional arg)
591 "Scroll text in a Follow mode window down by that window's size.
592 The other windows in the window chain will scroll synchronously.
594 If called with no ARG, the `next-screen-context-lines' top lines of
595 the window in the chain will be visible after the scroll.
597 If called with an argument, scroll ARG lines down.
598 Negative ARG means scroll upward.
600 Works like `scroll-down' when not in Follow mode."
601 (interactive "P")
602 (cond ((not follow-mode)
603 (scroll-down arg))
604 ((eq arg '-)
605 (follow-scroll-up-window))
606 (t (follow-scroll-down-arg arg))))
607 (put 'follow-scroll-down-window 'scroll-command t)
609 ;;;###autoload
610 (defun follow-scroll-up (&optional arg)
611 "Scroll text in a Follow mode window chain up.
613 If called with no ARG, the `next-screen-context-lines' last lines of
614 the bottom window in the chain will be visible in the top window.
616 If called with an argument, scroll ARG lines up.
617 Negative ARG means scroll downward.
619 Works like `scroll-up' when not in Follow mode."
620 (interactive "P")
621 (cond ((not follow-mode)
622 (scroll-up arg))
623 (arg (follow-scroll-up-arg arg))
625 (let* ((windows (follow-all-followers))
626 (end (window-end (car (reverse windows)))))
627 (if (eq end (point-max))
628 (signal 'end-of-buffer nil)
629 (select-window (car windows))
630 ;; `window-end' might return nil.
631 (if end
632 (goto-char end))
633 (vertical-motion (- next-screen-context-lines))
634 (set-window-start (car windows) (point)))))))
635 (put 'follow-scroll-up 'scroll-command t)
637 ;;;###autoload
638 (defun follow-scroll-down (&optional arg)
639 "Scroll text in a Follow mode window chain down.
641 If called with no ARG, the `next-screen-context-lines' top lines of
642 the top window in the chain will be visible in the bottom window.
644 If called with an argument, scroll ARG lines down.
645 Negative ARG means scroll upward.
647 Works like `scroll-down' when not in Follow mode."
648 (interactive "P")
649 (cond ((not follow-mode)
650 (scroll-down arg))
651 (arg (follow-scroll-down-arg arg))
653 (let* ((windows (follow-all-followers))
654 (win (car (reverse windows)))
655 (start (window-start (car windows))))
656 (if (eq start (point-min))
657 (signal 'beginning-of-buffer nil)
658 (select-window win)
659 (goto-char start)
660 (vertical-motion (- (- (window-height win)
661 (if header-line-format 2 1)
662 next-screen-context-lines)))
663 (set-window-start win (point))
664 (goto-char start)
665 (vertical-motion (- next-screen-context-lines 1))
666 (setq follow-internal-force-redisplay t))))))
667 (put 'follow-scroll-down 'scroll-command t)
669 (declare-function comint-adjust-point "comint" (window))
670 (defvar comint-scroll-show-maximum-output)
672 (defun follow-comint-scroll-to-bottom (&optional _window)
673 "Scroll the bottom-most window in the current Follow chain.
674 This is to be called by `comint-postoutput-scroll-to-bottom'."
675 (let* ((buffer (current-buffer))
676 (selected (selected-window))
677 (is-selected (eq (window-buffer) buffer))
678 some-window)
679 (when (or is-selected
680 (setq some-window (get-buffer-window)))
681 (let* ((pos (progn (comint-adjust-point nil) (point)))
682 (win (if is-selected
683 selected
684 (car (last (follow-all-followers some-window))))))
685 (select-window win)
686 (goto-char pos)
687 (setq follow-windows-start-end-cache nil)
688 (follow-adjust-window win)
689 (unless is-selected
690 (select-window selected)
691 (set-buffer buffer))))))
693 (defun follow-align-compilation-windows ()
694 "Align the windows of the current Follow mode buffer.
695 This is to be called from `compilation-filter-hook'."
696 (let ((buffer (current-buffer))
697 (win (get-buffer-window))
698 (selected (selected-window)))
699 (when (and follow-mode (waiting-for-user-input-p) win)
700 (let ((windows (follow-all-followers win)))
701 (unless (eq (window-buffer selected) buffer)
702 (setq win (car windows))
703 (select-window win))
704 (follow-redisplay windows win t)
705 (setq follow-windows-start-end-cache nil)
706 (unless (eq selected win)
707 (select-window selected)
708 (set-buffer buffer))))))
710 ;;; Buffer
712 ;;;###autoload
713 (defun follow-delete-other-windows-and-split (&optional arg)
714 "Create two side by side windows and enter Follow mode.
716 Execute this command to display as much as possible of the text
717 in the selected window. All other windows, in the current
718 frame, are deleted and the selected window is split in two
719 side-by-side windows. Follow mode is activated, hence the
720 two windows always will display two successive pages.
721 \(If one window is moved, the other one will follow.)
723 If ARG is positive, the leftmost window is selected. If negative,
724 the rightmost is selected. If ARG is nil, the leftmost window is
725 selected if the original window is the first one in the frame."
726 (interactive "P")
727 (let ((other (or (and (null arg)
728 (not (eq (selected-window)
729 (frame-first-window))))
730 (and arg
731 (< (prefix-numeric-value arg) 0))))
732 (start (window-start)))
733 (delete-other-windows)
734 (split-window-right)
735 (if other
736 (progn
737 (other-window 1)
738 (set-window-start (selected-window) start)
739 (setq follow-internal-force-redisplay t)))
740 (follow-mode 1)))
742 (defun follow-switch-to-buffer (buffer)
743 "Show BUFFER in all windows in the current Follow mode window chain."
744 (interactive "BSwitch to Buffer: ")
745 (let ((orig-window (selected-window))
746 (windows (follow-all-followers)))
747 (while windows
748 (select-window (car windows))
749 (switch-to-buffer buffer)
750 (setq windows (cdr windows)))
751 (select-window orig-window)))
754 (defun follow-switch-to-buffer-all (&optional buffer)
755 "Show BUFFER in all windows on this frame.
756 Defaults to current buffer."
757 (interactive (list (read-buffer "Switch to Buffer: "
758 (current-buffer))))
759 (or buffer (setq buffer (current-buffer)))
760 (let ((orig-window (selected-window)))
761 (walk-windows (lambda (win)
762 (select-window win)
763 (switch-to-buffer buffer))
764 'no-minibuf)
765 (select-window orig-window)
766 (follow-redisplay)))
769 (defun follow-switch-to-current-buffer-all ()
770 "Show current buffer in all windows on this frame, and enter Follow mode."
771 (interactive)
772 (unless follow-mode
773 (follow-mode 1))
774 (follow-switch-to-buffer-all))
776 ;;; Movement
778 ;; Note, these functions are not very useful, at least not unless you
779 ;; rebind the rather cumbersome key sequence `C-c . p'.
781 (defun follow-next-window ()
782 "Select the next window showing the same buffer."
783 (interactive)
784 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
785 (if succ
786 (select-window (car succ))
787 (error "%s" "No more windows"))))
790 (defun follow-previous-window ()
791 "Select the previous window showing the same buffer."
792 (interactive)
793 (let ((pred (car (follow-split-followers (follow-all-followers)))))
794 (if pred
795 (select-window (car pred))
796 (error "%s" "No more windows"))))
799 (defun follow-first-window ()
800 "Select the first window in the frame showing the same buffer."
801 (interactive)
802 (select-window (car (follow-all-followers))))
805 (defun follow-last-window ()
806 "Select the last window in the frame showing the same buffer."
807 (interactive)
808 (select-window (car (reverse (follow-all-followers)))))
810 ;;; Redraw
812 (defun follow-recenter (&optional arg)
813 "Recenter the middle window around point.
814 Rearrange all other windows around the middle window.
816 With a positive argument, place the current line ARG lines
817 from the top. With a negative argument, place it -ARG lines
818 from the bottom."
819 (interactive "P")
820 (if arg
821 (let ((p (point))
822 (arg (prefix-numeric-value arg)))
823 (if (>= arg 0)
824 ;; Recenter relative to the top.
825 (progn
826 (follow-first-window)
827 (goto-char p)
828 (recenter arg))
829 ;; Recenter relative to the bottom.
830 (follow-last-window)
831 (goto-char p)
832 (recenter arg)
833 ;; Otherwise, our post-command-hook will move the window
834 ;; right back.
835 (setq follow-internal-force-redisplay t)))
836 ;; Recenter in the middle.
837 (let* ((dest (point))
838 (windows (follow-all-followers))
839 (win (nth (/ (- (length windows) 1) 2) windows)))
840 (select-window win)
841 (goto-char dest)
842 (recenter))))
845 (defun follow-redraw ()
846 "Arrange windows displaying the same buffer in successor order.
847 This function can be called even if the buffer is not in Follow mode.
849 Hopefully, there should be no reason to call this function when in
850 Follow mode since the windows should always be aligned."
851 (interactive)
852 (sit-for 0)
853 (follow-redisplay))
855 ;;; End of buffer
857 (defun follow-end-of-buffer (&optional arg)
858 "Move point to the end of the buffer, Follow mode style.
860 If the end is not visible, it will be displayed in the last possible
861 window in the Follow mode window chain.
863 The mark is left at the previous position. With arg N, put point N/10
864 of the way from the true end."
865 (interactive "P")
866 (let ((followers (follow-all-followers))
867 (pos (point)))
868 (cond (arg
869 (select-window (car (reverse followers))))
870 ((follow-select-if-end-visible
871 (follow-windows-start-end followers)))
873 (select-window (car (reverse followers)))))
874 (goto-char pos)
875 (with-no-warnings
876 (end-of-buffer arg))))
878 ;;; Display
880 (defun follow--window-sorter (w1 w2)
881 "Sorting function for W1 and W2 based on their positions.
882 Return non-nil if W1 is above W2; if their top-lines
883 are at the same position, return non-nil if W1 is to the
884 left of W2."
885 (let* ((edge-1 (window-pixel-edges w1))
886 (edge-2 (window-pixel-edges w2))
887 (y1 (nth 1 edge-1))
888 (y2 (nth 1 edge-2)))
889 (if (= y1 y2)
890 (< (car edge-1) (car edge-2))
891 (< y1 y2))))
893 (defun follow-all-followers (&optional win)
894 "Return all windows displaying the same buffer as the WIN.
895 The list is sorted with topmost and leftmost windows first, and
896 contains only windows in the same frame as WIN. If WIN is nil,
897 it defaults to the selected window."
898 (unless (window-live-p win)
899 (setq win (selected-window)))
900 (let ((windows (get-buffer-window-list
901 (window-buffer win) 'no-minibuf (window-frame win))))
902 (sort windows #'follow--window-sorter)))
904 (defun follow-split-followers (windows &optional win)
905 "Split WINDOWS into two sets: predecessors and successors.
906 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
907 from the selected window."
908 (or win
909 (setq win (selected-window)))
910 (let ((pred '()))
911 (while (not (eq (car windows) win))
912 (setq pred (cons (car windows) pred))
913 (setq windows (cdr windows)))
914 (cons pred (cdr windows))))
916 (defun follow-calc-win-end (&optional win)
917 "Calculate the end position for window WIN.
918 Return (END-POS END-OF-BUFFER).
920 Actually, the position returned is the start of the line after
921 the last fully-visible line in WIN. END-OF-BUFFER is t when EOB
922 is fully-visible in WIN. If WIN is nil, the selected window is
923 used."
924 (let* ((win (or win (selected-window)))
925 (edges (window-inside-pixel-edges win))
926 (ht (- (nth 3 edges) (nth 1 edges)))
927 (last-line-pos (posn-point (posn-at-x-y 0 (1- ht) win))))
928 (if (pos-visible-in-window-p last-line-pos win)
929 (let ((end (window-end win t)))
930 (list end (pos-visible-in-window-p (point-max) win)))
931 (list last-line-pos nil))))
933 (defun follow-calc-win-start (windows pos win)
934 "Determine the start of window WIN in a Follow mode window chain.
935 WINDOWS is a list of chained windows, and POS is the starting
936 position for the first window in the list. If WIN is nil, return
937 the point below all windows."
938 (while (and windows (not (eq (car windows) win)))
939 (let ((old-start (window-start (car windows))))
940 ;; Can't use `save-window-excursion' since it triggers a redraw.
941 (set-window-start (car windows) pos 'noforce)
942 (setq pos (car (follow-calc-win-end (car windows))))
943 (set-window-start (car windows) old-start 'noforce)
944 (setq windows (cdr windows))))
945 pos)
947 ;; The result from `follow-windows-start-end' is cached when using
948 ;; a handful simple commands, like cursor movement commands.
950 (defsubst follow-cache-valid-p (windows)
951 "Test if the cached value of `follow-windows-start-end' can be used.
952 Note that this handles the case when the cache has been set to nil."
953 (let ((res t)
954 (cache follow-windows-start-end-cache))
955 (while (and res windows cache)
956 (setq res (and (eq (car windows)
957 (car (car cache)))
958 (eq (window-start (car windows))
959 (car (cdr (car cache))))))
960 (setq windows (cdr windows))
961 (setq cache (cdr cache)))
962 (and res (null windows) (null cache))))
964 (defun follow-windows-start-end (windows)
965 "Return a list of (WIN START END BUFFER-END-P) for window list WINDOWS."
966 (if (follow-cache-valid-p windows)
967 follow-windows-start-end-cache
968 (let ((orig-win (selected-window))
969 win-start-end)
970 (dolist (w windows)
971 (select-window w 'norecord)
972 (push (cons w (cons (window-start) (follow-calc-win-end)))
973 win-start-end))
974 (select-window orig-win 'norecord)
975 (setq follow-windows-start-end-cache (nreverse win-start-end)))))
977 (defsubst follow-pos-visible (pos win win-start-end)
978 "Non-nil when POS is visible in WIN."
979 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
980 (and (>= pos (car wstart-wend-bend))
981 (or (< pos (cadr wstart-wend-bend))
982 (nth 2 wstart-wend-bend)))))
985 ;; By `aligned' we mean that for all adjacent windows, the end of the
986 ;; first is equal with the start of the successor. The first window
987 ;; should start at a full screen line.
989 (defsubst follow-windows-aligned-p (win-start-end)
990 "Non-nil if the follower windows are aligned.
991 The argument, WIN-START-END, should be a list of the form
992 returned by `follow-windows-start-end'."
993 (let ((result t))
994 (while (and win-start-end result)
995 (if (cdr win-start-end)
996 (setq result (eq (nth 2 (car win-start-end))
997 (nth 1 (cadr win-start-end)))))
998 (setq win-start-end (cdr win-start-end)))
999 result))
1001 ;; Check if point is visible in all windows. (So that
1002 ;; no one will be recentered.)
1004 (defun follow-point-visible-all-windows-p (win-start-end)
1005 "Non-nil when the `window-point' is visible in all windows."
1006 (let ((res t))
1007 (while (and res win-start-end)
1008 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
1009 (car (car win-start-end))
1010 win-start-end))
1011 (setq win-start-end (cdr win-start-end)))
1012 res))
1015 ;; Make sure WIN always starts at the beginning of a whole screen
1016 ;; line. If WIN is not aligned the start is updated which probably
1017 ;; will lead to a redisplay of the screen later on.
1019 ;; This is used with the first window in a follow chain. The reason
1020 ;; is that we want to detect that point is outside the window.
1021 ;; (Without the update, the start of the window will move as the
1022 ;; user presses BackSpace, and the other window redisplay routines
1023 ;; will move the start of the window in the wrong direction.)
1025 (defun follow-update-window-start (win)
1026 "Make sure that the start of WIN starts at a full screen line."
1027 (save-excursion
1028 (goto-char (window-start win))
1029 (unless (bolp)
1030 (vertical-motion 0 win)
1031 (unless (eq (point) (window-start win))
1032 (vertical-motion 1 win)
1033 (set-window-start win (point) 'noforce)))))
1035 (defun follow-select-if-visible (dest win-start-end)
1036 "Select and return a window, if DEST is visible in it.
1037 Return the selected window."
1038 (let (win wse)
1039 (while (and (not win) win-start-end)
1040 ;; Don't select a window that was just moved. This makes it
1041 ;; possible to later select the last window after a
1042 ;; `end-of-buffer' command.
1043 (setq wse (car win-start-end))
1044 (when (follow-pos-visible dest (car wse) win-start-end)
1045 (setq win (car wse))
1046 (select-window win))
1047 (setq win-start-end (cdr win-start-end)))
1048 win))
1050 ;; Lets select a window showing the end. Make sure we only select it if
1051 ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place
1052 ;; point in the selected window.)
1054 ;; (Compatibility kludge: in Emacs `window-end' is equal to `point-max';
1055 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
1056 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
1058 (defun follow-select-if-end-visible (win-start-end)
1059 "Select and return a window, if end is visible in it."
1060 (let ((win nil))
1061 (while (and (not win) win-start-end)
1062 ;; Don't select a window that was just moved. This makes it
1063 ;; possible to later select the last window after a `end-of-buffer'
1064 ;; command.
1065 (if (and (eq (point-max) (nth 2 (car win-start-end)))
1066 (nth 3 (car win-start-end))
1067 ;; `window-end' might return nil.
1068 (let ((end (window-end (car (car win-start-end)))))
1069 (and end
1070 (eq (point-max) (min (point-max) end)))))
1071 (progn
1072 (setq win (car (car win-start-end)))
1073 (select-window win)))
1074 (setq win-start-end (cdr win-start-end)))
1075 win))
1078 ;; Select a window that will display point if the windows would
1079 ;; be redisplayed with the first window fixed. This is useful for
1080 ;; example when the user has pressed return at the bottom of a window
1081 ;; as point is not visible in any window.
1083 (defun follow-select-if-visible-from-first (dest windows)
1084 "Try to select one of WINDOWS without repositioning the topmost window.
1085 If one of the windows in WINDOWS contains DEST, select it, call
1086 `follow-redisplay', move point to DEST, and return that window.
1087 Otherwise, return nil."
1088 (let (win end-pos-end-p)
1089 (save-excursion
1090 (goto-char (window-start (car windows)))
1091 ;; Make sure the line start in the beginning of a real screen
1092 ;; line.
1093 (vertical-motion 0 (car windows))
1094 (when (>= dest (point))
1095 ;; At or below the start. Check the windows.
1096 (save-window-excursion
1097 (let ((windows windows))
1098 (while (and (not win) windows)
1099 (set-window-start (car windows) (point) 'noforce)
1100 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1101 (goto-char (car end-pos-end-p))
1102 ;; Visible, if dest above end, or if eob is visible
1103 ;; inside the window.
1104 (if (or (car (cdr end-pos-end-p))
1105 (< dest (point)))
1106 (setq win (car windows))
1107 (setq windows (cdr windows))))))))
1108 (when win
1109 (select-window win)
1110 (follow-redisplay windows (car windows))
1111 (goto-char dest))
1112 win))
1114 ;;; Redisplay
1116 ;; Redraw all the windows on the screen, starting with the top window.
1117 ;; The window used as as marker is WIN, or the selected window if WIN
1118 ;; is nil. Start every window directly after the end of the previous
1119 ;; window, to make sure long lines are displayed correctly.
1121 (defvar follow-start-end-invalid t
1122 "When non-nil, indicates `follow-windows-start-end-cache' is invalid.")
1123 (make-variable-buffer-local 'follow-start-end-invalid)
1125 (defun follow-redisplay (&optional windows win preserve-win)
1126 "Reposition the WINDOWS around WIN.
1127 Should point be too close to the roof we redisplay everything
1128 from the top. WINDOWS should contain a list of windows to
1129 redisplay; it is assumed that WIN is a member of the list.
1130 Should WINDOWS be nil, the windows displaying the
1131 same buffer as WIN, in the current frame, are used.
1132 Should WIN be nil, the selected window is used.
1133 If PRESERVE-WIN is non-nil, keep WIN itself unchanged while
1134 repositioning the other windows."
1135 (or win (setq win (selected-window)))
1136 (or windows (setq windows (follow-all-followers win)))
1137 ;; Calculate the start of the first window.
1138 (let* ((old-win-start (window-start win))
1139 (try-first-start (follow-estimate-first-window-start
1140 windows win old-win-start))
1141 (try-win-start (follow-calc-win-start
1142 windows try-first-start win))
1143 (start (cond ((= try-win-start old-win-start)
1144 (follow-debug-message "exact")
1145 try-first-start)
1146 ((< try-win-start old-win-start)
1147 (follow-debug-message "above")
1148 (follow-calculate-first-window-start-from-above
1149 windows try-first-start win old-win-start))
1151 (follow-debug-message "below")
1152 (follow-calculate-first-window-start-from-below
1153 windows try-first-start win old-win-start)))))
1154 (dolist (w windows)
1155 (unless (and preserve-win (eq w win))
1156 (set-window-start w start))
1157 (setq start (car (follow-calc-win-end w))))
1158 (setq follow-start-end-invalid nil)))
1160 (defun follow-estimate-first-window-start (windows win start)
1161 "Estimate the position of the first window.
1162 The estimate is computed by assuming that the window WIN, which
1163 should be a member of WINDOWS, starts at position START."
1164 (let ((windows-before (car (follow-split-followers windows win))))
1165 (save-excursion
1166 (goto-char start)
1167 (vertical-motion 0 win)
1168 (dolist (w windows-before)
1169 (vertical-motion (- (window-text-height w)) w))
1170 (point))))
1173 ;; Find the starting point, start at GUESS and search downward.
1174 ;; The returned point is always a point below GUESS.
1176 (defun follow-calculate-first-window-start-from-above
1177 (windows guess win start)
1178 (save-excursion
1179 (let ((done nil)
1180 win-start
1181 res)
1182 (goto-char guess)
1183 (while (not done)
1184 (if (not (= (vertical-motion 1 (car windows)) 1))
1185 ;; Hit bottom! (Can we really do this?)
1186 ;; We'll keep it, since it ensures termination.
1187 (progn
1188 (setq done t)
1189 (setq res (point-max)))
1190 (setq win-start (follow-calc-win-start windows (point) win))
1191 (if (>= win-start start)
1192 (setq done t res (point)))))
1193 res)))
1196 ;; Find the starting point, start at GUESS and search upward. Return
1197 ;; a point on the same line as GUESS, or above.
1199 (defun follow-calculate-first-window-start-from-below
1200 (windows guess &optional win start)
1201 (setq win (or win (selected-window)))
1202 (setq start (or start (window-start win)))
1203 (save-excursion
1204 (let (done win-start res opoint)
1205 ;; Always calculate what happens when no line is displayed in the first
1206 ;; window. (The `previous' res is needed below!)
1207 (goto-char guess)
1208 (vertical-motion 0 (car windows))
1209 (setq res (point))
1210 (while (not done)
1211 (setq opoint (point))
1212 (if (not (= (vertical-motion -1 (car windows)) -1))
1213 ;; Hit roof!
1214 (setq done t res (point-min))
1215 (setq win-start (follow-calc-win-start windows (point) win))
1216 (cond ((>= (point) opoint)
1217 ;; In some pathological cases, vertical-motion may
1218 ;; return -1 even though point has not decreased. In
1219 ;; that case, avoid looping forever.
1220 (setq done t res (point)))
1221 ((= win-start start) ; Perfect match, use this value
1222 (setq done t res (point)))
1223 ((< win-start start) ; Walked to far, use previous result
1224 (setq done t))
1225 (t ; Store result for next iteration
1226 (setq res (point))))))
1227 res)))
1229 ;;; Avoid tail recenter
1231 ;; This sets the window internal flag `force_start'. The effect is
1232 ;; that windows only displaying the tail aren't recentered.
1234 ;; A window displaying only the tail, is a window whose window-start
1235 ;; position is equal to (point-max) of the buffer it displays.
1237 (defun follow-avoid-tail-recenter (&rest _rest)
1238 "Make sure windows displaying the end of a buffer aren't recentered.
1239 This is done by reading and rewriting the start position of
1240 non-first windows in Follow mode."
1241 (let* ((orig-buffer (current-buffer))
1242 (top (frame-first-window))
1243 (win top)
1244 who) ; list of (buffer . frame)
1245 ;; If the only window in the frame is a minibuffer
1246 ;; window, `next-window' will never find it again...
1247 (unless (window-minibuffer-p top)
1248 (while ;; look, no body!
1249 (let ((start (window-start win))
1250 (pair (cons (window-buffer win) (window-frame win))))
1251 (set-buffer (window-buffer win))
1252 (cond ((null (member pair who))
1253 (setq who (cons pair who)))
1254 ((and follow-mode (eq (point-max) start))
1255 ;; Write the same window start back, but don't
1256 ;; set the NOFORCE flag.
1257 (set-window-start win start)))
1258 (setq win (next-window win 'not t))
1259 (not (eq win top)))) ;; Loop while this is true.
1260 (set-buffer orig-buffer))))
1262 ;;; Post Command Hook
1264 ;; The magic little box. This function is called after every command.
1266 ;; This is not as complicated as it seems. It is simply a list of common
1267 ;; display situations and the actions to take, plus commands for redrawing
1268 ;; the screen if it should be unaligned.
1270 ;; We divide the check into two parts; whether we are at the end or not.
1271 ;; This is due to the fact that the end can actually be visible
1272 ;; in several window even though they are aligned.
1274 (defun follow-post-command-hook ()
1275 "Ensure that the windows in Follow mode are adjacent after each command."
1276 (unless (input-pending-p)
1277 (let ((follow-inside-post-command-hook t)
1278 (win (selected-window)))
1279 ;; Work in the selected window, not in the current buffer.
1280 (with-current-buffer (window-buffer win)
1281 (unless (and (symbolp this-command)
1282 (get this-command 'follow-mode-use-cache))
1283 (setq follow-windows-start-end-cache nil))
1284 (follow-adjust-window win)))))
1286 (defun follow-adjust-window (win)
1287 ;; Adjust the window WIN and its followers.
1288 (cl-assert (eq (window-buffer win) (current-buffer)))
1289 (when (and follow-mode
1290 (not (window-minibuffer-p win)))
1291 (let ((windows (follow-all-followers win)))
1292 ;; If we've explicitly scrolled, align the windows first.
1293 (when follow-fixed-window
1294 (follow-debug-message "fixed")
1295 (follow-redisplay windows win)
1296 (goto-char (follow-get-scrolled-point (point) windows))
1297 (setq follow-fixed-window nil))
1298 (let* ((dest (point))
1299 (win-start-end (progn
1300 (follow-update-window-start (car windows))
1301 (follow-windows-start-end windows)))
1302 (aligned (follow-windows-aligned-p win-start-end))
1303 (visible (follow-pos-visible dest win win-start-end))
1304 selected-window-up-to-date)
1305 (unless (and aligned visible)
1306 (setq follow-windows-start-end-cache nil))
1308 ;; Select a window to display point.
1309 (unless follow-internal-force-redisplay
1310 (if (eq dest (point-max))
1311 ;; Be careful at point-max: the display can be aligned
1312 ;; while DEST can be visible in several windows.
1313 (cond
1314 ;; Select the current window, but only when the display
1315 ;; is correct. (When inserting characters in a tail
1316 ;; window, the display is not correct, as they are
1317 ;; shown twice.)
1319 ;; Never stick to the current window after a deletion.
1320 ;; Otherwise, when typing `DEL' in a window showing
1321 ;; only the end of the file, a character would be
1322 ;; removed from the window above, which is very
1323 ;; unintuitive.
1324 ((and visible
1325 aligned
1326 (not (memq this-command
1327 '(backward-delete-char
1328 delete-backward-char
1329 backward-delete-char-untabify
1330 kill-region))))
1331 (follow-debug-message "Max: same"))
1332 ;; If the end is visible, and the window doesn't
1333 ;; seems like it just has been moved, select it.
1334 ((follow-select-if-end-visible win-start-end)
1335 (follow-debug-message "Max: end visible")
1336 (setq visible t aligned nil)
1337 (goto-char dest))
1338 ;; Just show the end...
1340 (follow-debug-message "Max: default")
1341 (select-window (car (last windows)))
1342 (goto-char dest)
1343 (setq visible nil aligned nil)))
1345 ;; We're not at the end, here life is much simpler.
1346 (cond
1347 ;; This is the normal case!
1348 ;; It should be optimized for speed.
1349 ((and visible aligned)
1350 (follow-debug-message "same"))
1351 ;; Pick a position in any window. If the display is ok,
1352 ;; this picks the `correct' window.
1353 ((follow-select-if-visible dest win-start-end)
1354 (follow-debug-message "visible")
1355 (goto-char dest)
1356 ;; Perform redisplay, in case line is partially visible.
1357 (setq visible nil))
1358 ;; Not visible anywhere else, lets pick this one.
1359 (visible
1360 (follow-debug-message "visible in selected."))
1361 ;; If DEST is before the first window start, select the
1362 ;; first window.
1363 ((< dest (nth 1 (car win-start-end)))
1364 (follow-debug-message "before first")
1365 (select-window (car windows))
1366 (goto-char dest)
1367 (setq visible nil aligned nil))
1368 ;; If we can position the cursor without moving the first
1369 ;; window, do it. This is the case that catches `RET' at
1370 ;; the bottom of a window.
1371 ((follow-select-if-visible-from-first dest windows)
1372 (follow-debug-message "Below first")
1373 (setq visible t aligned t))
1374 ;; None of the above. Stick to the selected window.
1376 (follow-debug-message "None")
1377 (setq visible nil aligned nil))))
1379 ;; If a new window was selected, make sure that the old is
1380 ;; not scrolled when point is outside the window.
1381 (unless (eq win (selected-window))
1382 (let ((p (window-point win)))
1383 (set-window-start win (window-start win) nil)
1384 (set-window-point win p))))
1386 (unless visible
1387 ;; If point may not be visible in the selected window,
1388 ;; perform a redisplay; this ensures scrolling.
1389 (let ((opoint (point)))
1390 (redisplay)
1391 ;; If this `redisplay' moved point, we got clobbered by a
1392 ;; previous call to `set-window-start'. Try again.
1393 (when (/= (point) opoint)
1394 (goto-char opoint)
1395 (redisplay)))
1397 (setq selected-window-up-to-date t)
1398 (follow-avoid-tail-recenter)
1399 (setq win-start-end (follow-windows-start-end windows)
1400 follow-windows-start-end-cache nil
1401 aligned nil))
1403 ;; Now redraw the windows around the selected window.
1404 (unless (and (not follow-internal-force-redisplay)
1405 (or aligned
1406 (follow-windows-aligned-p win-start-end))
1407 (follow-point-visible-all-windows-p win-start-end))
1408 (setq follow-internal-force-redisplay nil)
1409 (follow-redisplay windows (selected-window)
1410 selected-window-up-to-date)
1411 (setq win-start-end (follow-windows-start-end windows)
1412 follow-windows-start-end-cache nil)
1413 ;; Point can end up in another window when DEST is at
1414 ;; the beginning of the buffer and the selected window is
1415 ;; not the first. It can also happen when long lines are
1416 ;; used and there is a big difference between the width of
1417 ;; the windows. (When scrolling one line in a wide window
1418 ;; which will cause a move larger that an entire small
1419 ;; window.)
1420 (unless (follow-pos-visible dest win win-start-end)
1421 (follow-select-if-visible dest win-start-end)
1422 (goto-char dest)))
1424 ;; If the region is visible, make it look good when spanning
1425 ;; multiple windows.
1426 (when (region-active-p)
1427 (follow-maximize-region
1428 (selected-window) windows win-start-end))))
1430 ;; Whether or not the buffer was in follow mode, update windows
1431 ;; displaying the tail so that Emacs won't recenter them.
1432 (follow-avoid-tail-recenter)))
1434 ;;; The region
1436 ;; Tries to make the highlighted area representing the region look
1437 ;; good when spanning several windows.
1439 ;; Not perfect, as point can't be placed at window end, only at
1440 ;; end-1. This will highlight a little bit in windows above
1441 ;; the current.
1443 (defun follow-maximize-region (win windows win-start-end)
1444 "Make a highlighted region stretching multiple windows look good."
1445 (let* ((all (follow-split-followers windows win))
1446 (pred (car all))
1447 (succ (cdr all))
1448 data)
1449 (while pred
1450 (setq data (assq (car pred) win-start-end))
1451 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1452 (setq pred (cdr pred)))
1453 (while succ
1454 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1455 (setq succ (cdr succ)))))
1457 ;;; Scroll bar
1459 ;;;; Scroll-bar support code.
1461 ;; This handles the case where the user drags the scroll bar of a
1462 ;; non-selected window whose buffer is in Follow mode.
1464 (declare-function scroll-bar-toolkit-scroll "scroll-bar" (event))
1465 (declare-function scroll-bar-drag "scroll-bar" (event))
1466 (declare-function scroll-bar-scroll-up "scroll-bar" (event))
1467 (declare-function scroll-bar-scroll-down "scroll-bar" (event))
1468 (declare-function mwheel-scroll "mwheel" (event))
1470 (defun follow-scroll-bar-toolkit-scroll (event)
1471 (interactive "e")
1472 (scroll-bar-toolkit-scroll event)
1473 (follow-redraw-after-event event))
1475 (defun follow-scroll-bar-drag (event)
1476 (interactive "e")
1477 (scroll-bar-drag event)
1478 (follow-redraw-after-event event))
1480 (defun follow-scroll-bar-scroll-up (event)
1481 (interactive "e")
1482 (scroll-bar-scroll-up event)
1483 (follow-redraw-after-event event))
1485 (defun follow-scroll-bar-scroll-down (event)
1486 (interactive "e")
1487 (scroll-bar-scroll-down event)
1488 (follow-redraw-after-event event))
1490 (defun follow-mwheel-scroll (event)
1491 (interactive "e")
1492 (mwheel-scroll event)
1493 (follow-redraw-after-event event))
1495 (defun follow-redraw-after-event (event)
1496 "Re-align the Follow mode windows affected by EVENT."
1497 (let* ((window (nth 0 (event-end event)))
1498 (buffer (window-buffer window))
1499 (orig-win (selected-window)))
1500 (when (and (buffer-local-value 'follow-mode buffer)
1501 ;; Ignore the case where we scroll the selected window;
1502 ;; that is handled by the post-command hook function.
1503 (not (eq window (selected-window))))
1504 (select-window window)
1505 (follow-redisplay)
1506 (unless (eq (window-buffer orig-win) buffer)
1507 (select-window orig-win)))))
1509 ;;; Window size change
1511 ;; The functions in `window-size-change-functions' are called every
1512 ;; time a window in a frame changes size, most notably after the frame
1513 ;; has been resized. We call `follow-post-command-hook' for every
1514 ;; Follow mode buffer visible in any window in the resized frame.
1516 ;; Since `follow-window-size-change' can be called indirectly from
1517 ;; `follow-post-command-hook' we have a potential infinite loop. To
1518 ;; avoid this, we simply do not do anything in this situation. The
1519 ;; variable `follow-inside-post-command-hook' contains information
1520 ;; about whether the execution actually is inside the
1521 ;; post-command-hook or not.
1523 (defun follow-window-size-change (frame)
1524 "Redraw all windows in FRAME, when in Follow mode."
1525 ;; Below, we call `post-command-hook'. Avoid an infloop.
1526 (unless follow-inside-post-command-hook
1527 (save-current-buffer
1528 (let ((orig-frame (selected-frame)))
1529 (select-frame frame)
1530 (let ((picked-window (selected-window)) ; Note: May change below.
1531 (seen-buffers '()))
1532 (unwind-protect
1533 (walk-windows
1534 (lambda (win)
1535 (let ((buf (window-buffer win)))
1536 (unless (memq buf seen-buffers)
1537 (set-buffer buf)
1538 (when follow-mode
1539 (let ((windows (follow-all-followers win)))
1540 (if (not (memq picked-window windows))
1541 (follow-redisplay windows win)
1542 ;; Make sure we're redrawing around the selected
1543 ;; window.
1544 (select-window picked-window 'norecord)
1545 (follow-post-command-hook)
1546 (setq picked-window (selected-window))))
1547 (push buf seen-buffers)))))
1548 'no-minibuf)
1549 (select-window picked-window 'norecord)))
1550 (select-frame orig-frame)))))
1552 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t)
1554 ;;; Low level window start and end.
1556 ;; These routines are the Follow Mode versions of the low level
1557 ;; functions described on page "Window Start and End" of the elisp
1558 ;; manual, e.g. `window-group-start'. The aim is to be able to handle
1559 ;; Follow Mode windows by replacing `window-start' by
1560 ;; `window-group-start', etc.
1562 (defun follow-after-change (_beg _end _old-len)
1563 "After change function: set `follow-start-end-invalid'."
1564 (setq follow-start-end-invalid t))
1566 (defun follow-window-start (&optional window)
1567 "Return position at which display currently starts in the
1568 Follow Mode group of windows which includes WINDOW.
1570 WINDOW must be a live window and defaults to the selected one.
1571 This is updated by redisplay or by calling
1572 `follow-set-window-start'."
1573 (let ((windows (follow-all-followers window)))
1574 (window-start (car windows))))
1576 (defun follow-window-end (&optional window update)
1577 "Return position at which display currently ends in the Follow
1578 Mode group of windows which includes WINDOW.
1580 WINDOW must be a live window and defaults to the selected one.
1581 This is updated by redisplay, when it runs to completion.
1582 Simply changing the buffer text or setting `window-start' does
1583 not update this value.
1585 Return nil if there is no recorded value. (This can happen if
1586 the last redisplay of WINDOW was preempted, and did not
1587 finish.) If UPDATE is non-nil, compute the up-to-date position
1588 if it isn't already recorded."
1589 (let* ((windows (follow-all-followers window))
1590 (last (car (last windows))))
1591 (when (and update follow-start-end-invalid)
1592 (follow-redisplay windows (car windows)))
1593 (window-end last update)))
1595 (defun follow-set-window-start (window pos &optional noforce)
1596 "Make display in the Follow Mode group of windows which includes
1597 WINDOW start at position POS in WINDOW's buffer.
1599 WINDOW must be a live window and defaults to the selected one. Return
1600 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1601 overriding motion of point in order to display at this exact start."
1602 (let ((windows (follow-all-followers window)))
1603 (setq follow-start-end-invalid t)
1604 (set-window-start (car windows) pos noforce)))
1606 (defun follow-pos-visible-in-window-p (&optional pos window partially)
1607 "Return non-nil if position POS is currently on the frame in one of
1608 the windows in the Follow Mode group which includes WINDOW.
1610 WINDOW must be a live window and defaults to the selected one.
1612 Return nil if that position is scrolled vertically out of view. If a
1613 character is only partially visible, nil is returned, unless the
1614 optional argument PARTIALLY is non-nil. If POS is only out of view
1615 because of horizontal scrolling, return non-nil. If POS is t, it
1616 specifies the position of the last visible glyph in WINDOW. POS
1617 defaults to point in WINDOW; WINDOW defaults to the selected window.
1619 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1620 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1621 where X and Y are the pixel coordinates relative to the top left corner
1622 of the actual window containing it. The remaining elements are
1623 omitted if the character after POS is fully visible; otherwise, RTOP
1624 and RBOT are the number of pixels off-window at the top and bottom of
1625 the screen line (\"row\") containing POS, ROWH is the visible height
1626 of that row, and VPOS is the row number \(zero-based)."
1627 (let* ((windows (follow-all-followers window))
1628 (last (car (last windows))))
1629 (when follow-start-end-invalid
1630 (follow-redisplay windows (car windows)))
1631 (let* ((cache (follow-windows-start-end windows))
1632 (last-elt (car (last cache)))
1633 our-pos pertinent-elt)
1634 (setq pertinent-elt
1635 (if (eq pos t)
1636 last-elt
1637 (setq our-pos (or pos (point)))
1638 (catch 'element
1639 (while cache
1640 (when (< our-pos (nth 2 (car cache)))
1641 (throw 'element (car cache)))
1642 (setq cache (cdr cache)))
1643 last-elt)))
1644 (pos-visible-in-window-p our-pos (car pertinent-elt) partially))))
1646 (defun follow-move-to-window-line (arg)
1647 "Position point relative to the Follow mode group containing the selected window.
1648 ARG nil means position point at center of the window group.
1649 Else, ARG specifies vertical position within the window group;
1650 zero means top of the first window in the group, negative means
1651 relative to bottom of the last window in the group."
1652 (let* ((windows (follow-all-followers))
1653 (start-end (follow-windows-start-end windows))
1654 (rev-start-end (reverse start-end))
1655 (lines 0)
1656 middle-window elt count)
1657 (select-window
1658 (cond
1659 ((null arg)
1660 (setq rev-start-end (nthcdr (/ (length windows) 2) rev-start-end))
1661 (prog1 (car (car rev-start-end))
1662 (while (setq rev-start-end (cdr rev-start-end))
1663 (setq elt (car rev-start-end)
1664 count (count-screen-lines (cadr elt) (nth 2 elt)
1665 nil (car elt))
1666 lines (+ lines count)))))
1667 ((>= arg 0)
1668 (while (and (cdr start-end)
1669 (progn
1670 (setq elt (car start-end)
1671 count (count-screen-lines (cadr elt) (nth 2 elt)
1672 nil (car elt)))
1673 (>= arg count)))
1674 (setq arg (- arg count)
1675 lines (+ lines count)
1676 start-end (cdr start-end)))
1677 (car (car start-end)))
1678 (t ; (< arg 0)
1679 (while (and (cadr rev-start-end)
1680 (progn
1681 (setq elt (car rev-start-end)
1682 count (count-lines (cadr elt) (nth 2 elt)))
1683 (<= arg (- count))))
1684 (setq arg (+ arg count)
1685 rev-start-end (cdr rev-start-end)))
1686 (prog1 (car (car rev-start-end))
1687 (while (setq rev-start-end (cdr rev-start-end))
1688 (setq elt (car rev-start-end)
1689 count (count-screen-lines (cadr elt) (nth 2 elt)
1690 nil (car elt))
1691 lines (+ lines count)))))))
1692 (+ lines (move-to-window-line arg))))
1694 ;;; Profile support
1696 ;; The following (non-evaluated) section can be used to
1697 ;; profile this package using `elp'.
1699 ;; Invalid indentation on purpose!
1701 ;; (setq elp-function-list
1702 ;; '(window-end
1703 ;; vertical-motion
1704 ;; follow-mode
1705 ;; follow-all-followers
1706 ;; follow-split-followers
1707 ;; follow-redisplay
1708 ;; follow-estimate-first-window-start
1709 ;; follow-calculate-first-window-start-from-above
1710 ;; follow-calculate-first-window-start-from-below
1711 ;; follow-calc-win-end
1712 ;; follow-calc-win-start
1713 ;; follow-pos-visible
1714 ;; follow-windows-start-end
1715 ;; follow-cache-valid-p
1716 ;; follow-select-if-visible
1717 ;; follow-select-if-visible-from-first
1718 ;; follow-windows-aligned-p
1719 ;; follow-point-visible-all-windows-p
1720 ;; follow-avoid-tail-recenter
1721 ;; follow-update-window-start
1722 ;; follow-post-command-hook))
1724 (provide 'follow)
1726 ;; /------------------------------------------------------------------------\
1727 ;; | "I [..] am rarely happier then when spending an entire day programming |
1728 ;; | my computer to perform automatically a task that it would otherwise |
1729 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
1730 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
1731 ;; | worth the investment of a day's happy activity working out a way to |
1732 ;; | save it". -- Douglas Adams, "Last Chance to See" |
1733 ;; \------------------------------------------------------------------------/
1735 ;;; follow.el ends here