]> code.delx.au - gnu-emacs/blob - lisp/follow.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / follow.el
1 ;;; follow.el --- synchronize windows showing the same buffer
2
3 ;; Copyright (C) 1995-1997, 1999, 2001-2016 Free Software Foundation,
4 ;; Inc.
5
6 ;; Author: Anders Lindgren
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Created: 1995-05-25
9 ;; Keywords: display, window, minor-mode, convenience
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
28 ;; `Follow mode' is a minor mode for Emacs and XEmacs that
29 ;; combines windows into one tall virtual window.
30 ;;
31 ;; The feeling of a "virtual window" has been accomplished by the use
32 ;; of two major techniques:
33 ;;
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.)
37 ;;
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.
42 ;;
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).
49
50 ;; To test this package, make sure `follow' is loaded, or will be
51 ;; autoloaded when activated (see below). Then do the following:
52 ;;
53 ;; * Find your favorite file (preferably a long one).
54 ;;
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'.
58 ;;
59 ;; * Give the command:
60 ;; M-x follow-mode <RETURN>
61 ;;
62 ;; * Now the display should look something like (assuming the text "71"
63 ;; is on line 71):
64 ;;
65 ;; +----------+----------+
66 ;; |1 |73 |
67 ;; |2 |74 |
68 ;; |3 |75 |
69 ;; ... ...
70 ;; |71 |143 |
71 ;; |72 |144 |
72 ;; +----------+----------+
73 ;;
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!
77 ;;
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.
83 ;;
84 ;; * Should you find `Follow' mode annoying, just type
85 ;; M-x follow-mode <RETURN>
86 ;; to turn it off.
87
88
89 ;; The command `follow-delete-other-windows-and-split' maximizes the
90 ;; visible area of the current buffer.
91 ;;
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:
95 ;;
96 ;; (global-set-key [f8] 'follow-mode)
97 ;; (global-set-key [f7] 'follow-delete-other-windows-and-split)
98
99
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.
103 ;;
104 ;; To make sure lines are never truncated, please place the following
105 ;; lines in your init file:
106 ;;
107 ;; (setq truncate-lines nil)
108 ;; (setq truncate-partial-width-windows nil)
109
110
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.
115 ;;
116 ;; The keymap `follow-key-map' contains key bindings activated by
117 ;; `follow-mode'.
118 ;;
119 ;; Example:
120 ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook)
121 ;;
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))
125
126
127 ;; Usage:
128 ;;
129 ;; To activate, issue the command "M-x follow-mode"
130 ;; and press Return. To deactivate, do it again.
131 ;;
132 ;; The following is a list of commands useful when follow-mode is active.
133 ;;
134 ;; follow-scroll-up C-c . C-v
135 ;; Scroll text in a Follow mode window chain up.
136 ;;
137 ;; follow-scroll-down C-c . v
138 ;; Like `follow-scroll-up', but in the other direction.
139 ;;
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.
145 ;;
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.
149 ;;
150 ;; follow-switch-to-buffer C-c . b
151 ;; Switch buffer in all windows displaying the current buffer
152 ;; in this frame.
153 ;;
154 ;; follow-switch-to-buffer-all C-c . C-b
155 ;; Switch buffer in all windows in the selected frame.
156 ;;
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'.
160 ;;
161 ;; follow-first-window C-c . <
162 ;; Select the first window in the frame showing the same buffer.
163 ;;
164 ;; follow-last-window C-c . >
165 ;; Select the last window in the frame showing the same buffer.
166 ;;
167 ;; follow-next-window C-c . n
168 ;; Select the next window in the frame showing the same buffer.
169 ;;
170 ;; follow-previous-window C-c . p
171 ;; Select the previous window showing the same buffer.
172
173
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:
176 ;;
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.)
179 ;;
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.
183 ;;
184 ;; Example from my ~/.emacs:
185 ;; (global-set-key [f8] 'follow-mode)
186
187 ;; Implementation:
188 ;;
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.
195 ;;
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.
201
202 ;;; Code:
203
204 (require 'easymenu)
205 (eval-when-compile (require 'cl-lib))
206
207 ;;; Variables
208
209 (defgroup follow nil
210 "Synchronize windows showing the same buffer."
211 :group 'windows
212 :group 'convenience)
213
214 (defcustom follow-mode-hook nil
215 "Normal hook run by `follow-mode'."
216 :type 'hook
217 :group 'follow)
218
219 ;;; Keymap/Menu
220
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.
224 ;;
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.
228
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)
235
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)
250
251 (define-key mainmap follow-mode-prefix map)
252
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)
259
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)
265
266 mainmap)
267 "Minor mode keymap for Follow mode.")
268
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])))
276
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]))
296
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)
303
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)))
313
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.
317
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.
321
322 The commands in this list are checked at load time.
323
324 To mark other commands as suitable for caching, set the symbol
325 property `follow-mode-use-cache' to non-nil.")
326
327 (defcustom follow-debug nil
328 "If non-nil, emit Follow mode debugging messages."
329 :type 'boolean
330 :group 'follow)
331
332 ;; Internal variables:
333
334 (defvar follow-internal-force-redisplay nil
335 "True when Follow mode should redisplay the windows.")
336
337 (defvar follow-active-menu nil
338 "The menu visible when Follow mode is active.")
339
340 (defvar follow-inactive-menu nil
341 "The menu visible when Follow mode is inactive.")
342
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'.")
346
347 (defvar follow-windows-start-end-cache nil
348 "Cache used by `follow-window-start-end'.")
349
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
354
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.
358
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)))
363
364 ;;; Cache
365
366 (dolist (cmd follow-cache-command-list)
367 (put cmd 'follow-mode-use-cache t))
368
369 ;;; The mode
370
371 ;;;###autoload
372 (defun turn-on-follow-mode ()
373 "Turn on Follow mode. Please see the function `follow-mode'."
374 (follow-mode 1))
375
376
377 ;;;###autoload
378 (defun turn-off-follow-mode ()
379 "Turn off Follow mode. Please see the function `follow-mode'."
380 (follow-mode -1))
381
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.
389
390 Follow mode is a minor mode that combines windows into one tall
391 virtual window. This is accomplished by two main techniques:
392
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.)
396
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.
401
402 Follow mode comes to its prime when used on a large screen and two
403 side-by-side windows are used. The user can, with the help of Follow
404 mode, use two full-height windows as though they would have been
405 one. Imagine yourself editing a large function, or section of text,
406 and being able to use 144 lines instead of the normal 72... (your
407 mileage may vary).
408
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.
412
413 Only windows displayed in the same frame follow each other.
414
415 This command runs the normal hook `follow-mode-hook'.
416
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)
429
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))
438
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)))
449
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)
457
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)))
463
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)))
467
468 ;;; User functions
469
470 ;;; Scroll
471
472 (defun follow-get-scrolled-point (dest windows)
473 "Calculate the correct value for point after a scrolling operation.
474
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.
479
480 This function attempts to duplicate the point placing from
481 `window_scroll_line_based' in the Emacs core source window.c.
482
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)))))
519
520 ;; `scroll-up' and `-down', but for windows in Follow mode.
521 ;;
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?)
529 ;;
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.)
534
535 ;;;###autoload
536 (defun follow-scroll-up (&optional arg)
537 "Scroll text in a Follow mode window chain up.
538
539 If called with no ARG, the `next-screen-context-lines' last lines of
540 the bottom window in the chain will be visible in the top window.
541
542 If called with an argument, scroll ARG lines up.
543 Negative ARG means scroll downward.
544
545 Works like `scroll-up' when not in Follow mode."
546 (interactive "P")
547 (cond ((not follow-mode)
548 (scroll-up arg))
549 ((eq arg '-)
550 (follow-scroll-down))
551 (t
552 (let ((opoint (point)) (owin (selected-window)))
553 (while
554 ;; If we are too near EOB, try scrolling the previous window.
555 (condition-case nil (progn (scroll-up arg) nil)
556 (end-of-buffer
557 (condition-case nil (progn (follow-previous-window) t)
558 (error
559 (select-window owin)
560 (goto-char opoint)
561 (signal 'end-of-buffer nil))))))
562 (unless (and scroll-preserve-screen-position
563 (get this-command 'scroll-command))
564 (goto-char opoint))
565 (setq follow-fixed-window t)))))
566 (put 'follow-scroll-up 'scroll-command t)
567
568 ;;;###autoload
569 (defun follow-scroll-down (&optional arg)
570 "Scroll text in a Follow mode window chain down.
571
572 If called with no ARG, the `next-screen-context-lines' top lines of
573 the top window in the chain will be visible in the bottom window.
574
575 If called with an argument, scroll ARG lines down.
576 Negative ARG means scroll upward.
577
578 Works like `scroll-down' when not in Follow mode."
579 (interactive "P")
580 (cond ((not follow-mode)
581 (scroll-down arg))
582 ((eq arg '-)
583 (follow-scroll-up))
584 (t
585 (let ((opoint (point)))
586 (scroll-down arg)
587 (unless (and scroll-preserve-screen-position
588 (get this-command 'scroll-command))
589 (goto-char opoint))
590 (setq follow-fixed-window t)))))
591 (put 'follow-scroll-down 'scroll-command t)
592
593 (declare-function comint-adjust-point "comint" (window))
594 (defvar comint-scroll-show-maximum-output)
595
596 (defun follow-comint-scroll-to-bottom (&optional _window)
597 "Scroll the bottom-most window in the current Follow chain.
598 This is to be called by `comint-postoutput-scroll-to-bottom'."
599 (let* ((buffer (current-buffer))
600 (selected (selected-window))
601 (is-selected (eq (window-buffer) buffer))
602 some-window)
603 (when (or is-selected
604 (setq some-window (get-buffer-window)))
605 (let* ((pos (progn (comint-adjust-point nil) (point)))
606 (win (if is-selected
607 selected
608 (car (last (follow-all-followers some-window))))))
609 (select-window win)
610 (goto-char pos)
611 (setq follow-windows-start-end-cache nil)
612 (follow-adjust-window win)
613 (unless is-selected
614 (select-window selected)
615 (set-buffer buffer))))))
616
617 (defun follow-align-compilation-windows ()
618 "Align the windows of the current Follow mode buffer.
619 This is to be called from `compilation-filter-hook'."
620 (let ((buffer (current-buffer))
621 (win (get-buffer-window))
622 (selected (selected-window)))
623 (when (and follow-mode (waiting-for-user-input-p) win)
624 (let ((windows (follow-all-followers win)))
625 (unless (eq (window-buffer selected) buffer)
626 (setq win (car windows))
627 (select-window win))
628 (follow-redisplay windows win t)
629 (setq follow-windows-start-end-cache nil)
630 (unless (eq selected win)
631 (select-window selected)
632 (set-buffer buffer))))))
633
634 ;;; Buffer
635
636 ;;;###autoload
637 (defun follow-delete-other-windows-and-split (&optional arg)
638 "Create two side by side windows and enter Follow mode.
639
640 Execute this command to display as much as possible of the text
641 in the selected window. All other windows, in the current
642 frame, are deleted and the selected window is split in two
643 side-by-side windows. Follow mode is activated, hence the
644 two windows always will display two successive pages.
645 \(If one window is moved, the other one will follow.)
646
647 If ARG is positive, the leftmost window is selected. If negative,
648 the rightmost is selected. If ARG is nil, the leftmost window is
649 selected if the original window is the first one in the frame."
650 (interactive "P")
651 (let ((other (or (and (null arg)
652 (not (eq (selected-window)
653 (frame-first-window))))
654 (and arg
655 (< (prefix-numeric-value arg) 0))))
656 (start (window-start)))
657 (delete-other-windows)
658 (split-window-right)
659 (if other
660 (progn
661 (other-window 1)
662 (set-window-start (selected-window) start)
663 (setq follow-internal-force-redisplay t)))
664 (follow-mode 1)))
665
666 (defun follow-switch-to-buffer (buffer)
667 "Show BUFFER in all windows in the current Follow mode window chain."
668 (interactive "BSwitch to Buffer: ")
669 (let ((orig-window (selected-window))
670 (windows (follow-all-followers)))
671 (while windows
672 (select-window (car windows))
673 (switch-to-buffer buffer)
674 (setq windows (cdr windows)))
675 (select-window orig-window)))
676
677
678 (defun follow-switch-to-buffer-all (&optional buffer)
679 "Show BUFFER in all windows on this frame.
680 Defaults to current buffer."
681 (interactive (list (read-buffer "Switch to Buffer: "
682 (current-buffer))))
683 (or buffer (setq buffer (current-buffer)))
684 (let ((orig-window (selected-window)))
685 (walk-windows (lambda (win)
686 (select-window win)
687 (switch-to-buffer buffer))
688 'no-minibuf)
689 (select-window orig-window)
690 (follow-redisplay)))
691
692
693 (defun follow-switch-to-current-buffer-all ()
694 "Show current buffer in all windows on this frame, and enter Follow mode."
695 (interactive)
696 (unless follow-mode
697 (follow-mode 1))
698 (follow-switch-to-buffer-all))
699
700 ;;; Movement
701
702 ;; Note, these functions are not very useful, at least not unless you
703 ;; rebind the rather cumbersome key sequence `C-c . p'.
704
705 (defun follow-next-window ()
706 "Select the next window showing the same buffer."
707 (interactive)
708 (let ((succ (cdr (follow-split-followers (follow-all-followers)))))
709 (if succ
710 (select-window (car succ))
711 (error "%s" "No more windows"))))
712
713
714 (defun follow-previous-window ()
715 "Select the previous window showing the same buffer."
716 (interactive)
717 (let ((pred (car (follow-split-followers (follow-all-followers)))))
718 (if pred
719 (select-window (car pred))
720 (error "%s" "No more windows"))))
721
722
723 (defun follow-first-window ()
724 "Select the first window in the frame showing the same buffer."
725 (interactive)
726 (select-window (car (follow-all-followers))))
727
728
729 (defun follow-last-window ()
730 "Select the last window in the frame showing the same buffer."
731 (interactive)
732 (select-window (car (reverse (follow-all-followers)))))
733
734 ;;; Redraw
735
736 (defun follow-recenter (&optional arg)
737 "Recenter the middle window around point.
738 Rearrange all other windows around the middle window.
739
740 With a positive argument, place the current line ARG lines
741 from the top. With a negative argument, place it -ARG lines
742 from the bottom."
743 (interactive "P")
744 (if arg
745 (let ((p (point))
746 (arg (prefix-numeric-value arg)))
747 (if (>= arg 0)
748 ;; Recenter relative to the top.
749 (progn
750 (follow-first-window)
751 (goto-char p)
752 (recenter arg))
753 ;; Recenter relative to the bottom.
754 (follow-last-window)
755 (goto-char p)
756 (recenter arg)
757 ;; Otherwise, our post-command-hook will move the window
758 ;; right back.
759 (setq follow-internal-force-redisplay t)))
760 ;; Recenter in the middle.
761 (let* ((dest (point))
762 (windows (follow-all-followers))
763 (win (nth (/ (- (length windows) 1) 2) windows)))
764 (select-window win)
765 (goto-char dest)
766 (recenter))))
767
768
769 (defun follow-redraw ()
770 "Arrange windows displaying the same buffer in successor order.
771 This function can be called even if the buffer is not in Follow mode.
772
773 Hopefully, there should be no reason to call this function when in
774 Follow mode since the windows should always be aligned."
775 (interactive)
776 (sit-for 0)
777 (follow-redisplay))
778
779 ;;; End of buffer
780
781 (defun follow-end-of-buffer (&optional arg)
782 "Move point to the end of the buffer, Follow mode style.
783
784 If the end is not visible, it will be displayed in the last possible
785 window in the Follow mode window chain.
786
787 The mark is left at the previous position. With arg N, put point N/10
788 of the way from the true end."
789 (interactive "P")
790 (let ((followers (follow-all-followers))
791 (pos (point)))
792 (cond (arg
793 (select-window (car (reverse followers))))
794 ((follow-select-if-end-visible
795 (follow-windows-start-end followers)))
796 (t
797 (select-window (car (reverse followers)))))
798 (goto-char pos)
799 (with-no-warnings
800 (end-of-buffer arg))))
801
802 ;;; Display
803
804 (defun follow--window-sorter (w1 w2)
805 "Sorting function for W1 and W2 based on their positions.
806 Return non-nil if W1 is above W2; if their top-lines
807 are at the same position, return non-nil if W1 is to the
808 left of W2."
809 (let* ((edge-1 (window-pixel-edges w1))
810 (edge-2 (window-pixel-edges w2))
811 (y1 (nth 1 edge-1))
812 (y2 (nth 1 edge-2)))
813 (if (= y1 y2)
814 (< (car edge-1) (car edge-2))
815 (< y1 y2))))
816
817 (defun follow-all-followers (&optional win)
818 "Return all windows displaying the same buffer as the WIN.
819 The list is sorted with topmost and leftmost windows first, and
820 contains only windows in the same frame as WIN. If WIN is nil,
821 it defaults to the selected window."
822 (unless (window-live-p win)
823 (setq win (selected-window)))
824 (let ((windows (get-buffer-window-list
825 (window-buffer win) 'no-minibuf (window-frame win))))
826 (sort windows #'follow--window-sorter)))
827
828 (defun follow-split-followers (windows &optional win)
829 "Split WINDOWS into two sets: predecessors and successors.
830 Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting
831 from the selected window."
832 (or win
833 (setq win (selected-window)))
834 (let ((pred '()))
835 (while (not (eq (car windows) win))
836 (setq pred (cons (car windows) pred))
837 (setq windows (cdr windows)))
838 (cons pred (cdr windows))))
839
840 (defun follow-calc-win-end (&optional win)
841 "Calculate the end position for window WIN.
842 Return (END-POS END-OF-BUFFER).
843
844 Actually, the position returned is the start of the line after
845 the last fully-visible line in WIN. END-OF-BUFFER is t when EOB
846 is fully-visible in WIN. If WIN is nil, the selected window is
847 used."
848 (let* ((win (or win (selected-window)))
849 (edges (window-inside-pixel-edges win))
850 (ht (- (nth 3 edges) (nth 1 edges)))
851 (last-line-pos (posn-point (posn-at-x-y 0 (1- ht) win))))
852 (if (pos-visible-in-window-p last-line-pos win)
853 (let ((end (window-end win t)))
854 (list end (pos-visible-in-window-p (point-max) win)))
855 (list last-line-pos nil))))
856
857 (defun follow-calc-win-start (windows pos win)
858 "Determine the start of window WIN in a Follow mode window chain.
859 WINDOWS is a list of chained windows, and POS is the starting
860 position for the first window in the list. If WIN is nil, return
861 the point below all windows."
862 (while (and windows (not (eq (car windows) win)))
863 (let ((old-start (window-start (car windows))))
864 ;; Can't use `save-window-excursion' since it triggers a redraw.
865 (set-window-start (car windows) pos 'noforce)
866 (setq pos (car (follow-calc-win-end (car windows))))
867 (set-window-start (car windows) old-start 'noforce)
868 (setq windows (cdr windows))))
869 pos)
870
871 ;; The result from `follow-windows-start-end' is cached when using
872 ;; a handful simple commands, like cursor movement commands.
873
874 (defsubst follow-cache-valid-p (windows)
875 "Test if the cached value of `follow-windows-start-end' can be used.
876 Note that this handles the case when the cache has been set to nil."
877 (let ((res t)
878 (cache follow-windows-start-end-cache))
879 (while (and res windows cache)
880 (setq res (and (eq (car windows)
881 (car (car cache)))
882 (eq (window-start (car windows))
883 (car (cdr (car cache))))))
884 (setq windows (cdr windows))
885 (setq cache (cdr cache)))
886 (and res (null windows) (null cache))))
887
888 (defun follow-windows-start-end (windows)
889 "Return a list of (WIN START END BUFFER-END-P) for window list WINDOWS."
890 (if (follow-cache-valid-p windows)
891 follow-windows-start-end-cache
892 (let ((orig-win (selected-window))
893 win-start-end)
894 (dolist (w windows)
895 (select-window w 'norecord)
896 (push (cons w (cons (window-start) (follow-calc-win-end)))
897 win-start-end))
898 (select-window orig-win 'norecord)
899 (setq follow-windows-start-end-cache (nreverse win-start-end)))))
900
901 (defsubst follow-pos-visible (pos win win-start-end)
902 "Non-nil when POS is visible in WIN."
903 (let ((wstart-wend-bend (cdr (assq win win-start-end))))
904 (and (>= pos (car wstart-wend-bend))
905 (or (< pos (cadr wstart-wend-bend))
906 (nth 2 wstart-wend-bend)))))
907
908
909 ;; By `aligned' we mean that for all adjacent windows, the end of the
910 ;; first is equal with the start of the successor. The first window
911 ;; should start at a full screen line.
912
913 (defsubst follow-windows-aligned-p (win-start-end)
914 "Non-nil if the follower windows are aligned.
915 The argument, WIN-START-END, should be a list of the form
916 returned by `follow-windows-start-end'."
917 (let ((result t))
918 (while (and win-start-end result)
919 (if (cdr win-start-end)
920 (setq result (eq (nth 2 (car win-start-end))
921 (nth 1 (cadr win-start-end)))))
922 (setq win-start-end (cdr win-start-end)))
923 result))
924
925 ;; Check if point is visible in all windows. (So that
926 ;; no one will be recentered.)
927
928 (defun follow-point-visible-all-windows-p (win-start-end)
929 "Non-nil when the `window-point' is visible in all windows."
930 (let ((res t))
931 (while (and res win-start-end)
932 (setq res (follow-pos-visible (window-point (car (car win-start-end)))
933 (car (car win-start-end))
934 win-start-end))
935 (setq win-start-end (cdr win-start-end)))
936 res))
937
938
939 ;; Make sure WIN always starts at the beginning of a whole screen
940 ;; line. If WIN is not aligned the start is updated which probably
941 ;; will lead to a redisplay of the screen later on.
942 ;;
943 ;; This is used with the first window in a follow chain. The reason
944 ;; is that we want to detect that point is outside the window.
945 ;; (Without the update, the start of the window will move as the
946 ;; user presses BackSpace, and the other window redisplay routines
947 ;; will move the start of the window in the wrong direction.)
948
949 (defun follow-update-window-start (win)
950 "Make sure that the start of WIN starts at a full screen line."
951 (save-excursion
952 (goto-char (window-start win))
953 (unless (bolp)
954 (vertical-motion 0 win)
955 (unless (eq (point) (window-start win))
956 (vertical-motion 1 win)
957 (set-window-start win (point) 'noforce)))))
958
959 (defun follow-select-if-visible (dest win-start-end)
960 "Select and return a window, if DEST is visible in it.
961 Return the selected window."
962 (let (win wse)
963 (while (and (not win) win-start-end)
964 ;; Don't select a window that was just moved. This makes it
965 ;; possible to later select the last window after a
966 ;; `end-of-buffer' command.
967 (setq wse (car win-start-end))
968 (when (follow-pos-visible dest (car wse) win-start-end)
969 (setq win (car wse))
970 (select-window win))
971 (setq win-start-end (cdr win-start-end)))
972 win))
973
974 ;; Lets select a window showing the end. Make sure we only select it if
975 ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place
976 ;; point in the selected window.)
977 ;;
978 ;; (Compatibility kludge: in Emacs `window-end' is equal to `point-max';
979 ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother
980 ;; checking `window-end' now when I check `end-of-buffer' explicitly?)
981
982 (defun follow-select-if-end-visible (win-start-end)
983 "Select and return a window, if end is visible in it."
984 (let ((win nil))
985 (while (and (not win) win-start-end)
986 ;; Don't select a window that was just moved. This makes it
987 ;; possible to later select the last window after a `end-of-buffer'
988 ;; command.
989 (if (and (eq (point-max) (nth 2 (car win-start-end)))
990 (nth 3 (car win-start-end))
991 ;; `window-end' might return nil.
992 (let ((end (window-end (car (car win-start-end)))))
993 (and end
994 (eq (point-max) (min (point-max) end)))))
995 (progn
996 (setq win (car (car win-start-end)))
997 (select-window win)))
998 (setq win-start-end (cdr win-start-end)))
999 win))
1000
1001
1002 ;; Select a window that will display point if the windows would
1003 ;; be redisplayed with the first window fixed. This is useful for
1004 ;; example when the user has pressed return at the bottom of a window
1005 ;; as point is not visible in any window.
1006
1007 (defun follow-select-if-visible-from-first (dest windows)
1008 "Try to select one of WINDOWS without repositioning the topmost window.
1009 If one of the windows in WINDOWS contains DEST, select it, call
1010 `follow-redisplay', move point to DEST, and return that window.
1011 Otherwise, return nil."
1012 (let (win end-pos-end-p)
1013 (save-excursion
1014 (goto-char (window-start (car windows)))
1015 ;; Make sure the line start in the beginning of a real screen
1016 ;; line.
1017 (vertical-motion 0 (car windows))
1018 (when (>= dest (point))
1019 ;; At or below the start. Check the windows.
1020 (save-window-excursion
1021 (let ((windows windows))
1022 (while (and (not win) windows)
1023 (set-window-start (car windows) (point) 'noforce)
1024 (setq end-pos-end-p (follow-calc-win-end (car windows)))
1025 (goto-char (car end-pos-end-p))
1026 ;; Visible, if dest above end, or if eob is visible
1027 ;; inside the window.
1028 (if (or (car (cdr end-pos-end-p))
1029 (< dest (point)))
1030 (setq win (car windows))
1031 (setq windows (cdr windows))))))))
1032 (when win
1033 (select-window win)
1034 (follow-redisplay windows (car windows))
1035 (goto-char dest))
1036 win))
1037
1038 ;;; Redisplay
1039
1040 ;; Redraw all the windows on the screen, starting with the top window.
1041 ;; The window used as as marker is WIN, or the selected window if WIN
1042 ;; is nil. Start every window directly after the end of the previous
1043 ;; window, to make sure long lines are displayed correctly.
1044
1045 (defvar follow-start-end-invalid t
1046 "When non-nil, indicates `follow-windows-start-end-cache' is invalid.")
1047 (make-variable-buffer-local 'follow-start-end-invalid)
1048
1049 (defun follow-redisplay (&optional windows win preserve-win)
1050 "Reposition the WINDOWS around WIN.
1051 Should point be too close to the roof we redisplay everything
1052 from the top. WINDOWS should contain a list of windows to
1053 redisplay; it is assumed that WIN is a member of the list.
1054 Should WINDOWS be nil, the windows displaying the
1055 same buffer as WIN, in the current frame, are used.
1056 Should WIN be nil, the selected window is used.
1057 If PRESERVE-WIN is non-nil, keep WIN itself unchanged while
1058 repositioning the other windows."
1059 (or win (setq win (selected-window)))
1060 (or windows (setq windows (follow-all-followers win)))
1061 ;; Calculate the start of the first window.
1062 (let* ((old-win-start (window-start win))
1063 (try-first-start (follow-estimate-first-window-start
1064 windows win old-win-start))
1065 (try-win-start (follow-calc-win-start
1066 windows try-first-start win))
1067 (start (cond ((= try-win-start old-win-start)
1068 (follow-debug-message "exact")
1069 try-first-start)
1070 ((< try-win-start old-win-start)
1071 (follow-debug-message "above")
1072 (follow-calculate-first-window-start-from-above
1073 windows try-first-start win old-win-start))
1074 (t
1075 (follow-debug-message "below")
1076 (follow-calculate-first-window-start-from-below
1077 windows try-first-start win old-win-start)))))
1078 (dolist (w windows)
1079 (unless (and preserve-win (eq w win))
1080 (set-window-start w start))
1081 (setq start (car (follow-calc-win-end w))))
1082 (setq follow-start-end-invalid nil)))
1083
1084 (defun follow-estimate-first-window-start (windows win start)
1085 "Estimate the position of the first window.
1086 The estimate is computed by assuming that the window WIN, which
1087 should be a member of WINDOWS, starts at position START."
1088 (let ((windows-before (car (follow-split-followers windows win))))
1089 (save-excursion
1090 (goto-char start)
1091 (vertical-motion 0 win)
1092 (dolist (w windows-before)
1093 (vertical-motion (- (window-text-height w)) w))
1094 (point))))
1095
1096
1097 ;; Find the starting point, start at GUESS and search downward.
1098 ;; The returned point is always a point below GUESS.
1099
1100 (defun follow-calculate-first-window-start-from-above
1101 (windows guess win start)
1102 (save-excursion
1103 (let ((done nil)
1104 win-start
1105 res)
1106 (goto-char guess)
1107 (while (not done)
1108 (if (not (= (vertical-motion 1 (car windows)) 1))
1109 ;; Hit bottom! (Can we really do this?)
1110 ;; We'll keep it, since it ensures termination.
1111 (progn
1112 (setq done t)
1113 (setq res (point-max)))
1114 (setq win-start (follow-calc-win-start windows (point) win))
1115 (if (>= win-start start)
1116 (setq done t res (point)))))
1117 res)))
1118
1119
1120 ;; Find the starting point, start at GUESS and search upward. Return
1121 ;; a point on the same line as GUESS, or above.
1122
1123 (defun follow-calculate-first-window-start-from-below
1124 (windows guess &optional win start)
1125 (setq win (or win (selected-window)))
1126 (setq start (or start (window-start win)))
1127 (save-excursion
1128 (let (done win-start res opoint)
1129 ;; Always calculate what happens when no line is displayed in the first
1130 ;; window. (The `previous' res is needed below!)
1131 (goto-char guess)
1132 (vertical-motion 0 (car windows))
1133 (setq res (point))
1134 (while (not done)
1135 (setq opoint (point))
1136 (if (not (= (vertical-motion -1 (car windows)) -1))
1137 ;; Hit roof!
1138 (setq done t res (point-min))
1139 (setq win-start (follow-calc-win-start windows (point) win))
1140 (cond ((>= (point) opoint)
1141 ;; In some pathological cases, vertical-motion may
1142 ;; return -1 even though point has not decreased. In
1143 ;; that case, avoid looping forever.
1144 (setq done t res (point)))
1145 ((= win-start start) ; Perfect match, use this value
1146 (setq done t res (point)))
1147 ((< win-start start) ; Walked to far, use previous result
1148 (setq done t))
1149 (t ; Store result for next iteration
1150 (setq res (point))))))
1151 res)))
1152
1153 ;;; Avoid tail recenter
1154
1155 ;; This sets the window internal flag `force_start'. The effect is
1156 ;; that windows only displaying the tail aren't recentered.
1157 ;;
1158 ;; A window displaying only the tail, is a window whose window-start
1159 ;; position is equal to (point-max) of the buffer it displays.
1160
1161 (defun follow-avoid-tail-recenter (&rest _rest)
1162 "Make sure windows displaying the end of a buffer aren't recentered.
1163 This is done by reading and rewriting the start position of
1164 non-first windows in Follow mode."
1165 (let* ((orig-buffer (current-buffer))
1166 (top (frame-first-window))
1167 (win top)
1168 who) ; list of (buffer . frame)
1169 ;; If the only window in the frame is a minibuffer
1170 ;; window, `next-window' will never find it again...
1171 (unless (window-minibuffer-p top)
1172 (while ;; look, no body!
1173 (let ((start (window-start win))
1174 (pair (cons (window-buffer win) (window-frame win))))
1175 (set-buffer (window-buffer win))
1176 (cond ((null (member pair who))
1177 (setq who (cons pair who)))
1178 ((and follow-mode (eq (point-max) start))
1179 ;; Write the same window start back, but don't
1180 ;; set the NOFORCE flag.
1181 (set-window-start win start)))
1182 (setq win (next-window win 'not t))
1183 (not (eq win top)))) ;; Loop while this is true.
1184 (set-buffer orig-buffer))))
1185
1186 ;;; Post Command Hook
1187
1188 ;; The magic little box. This function is called after every command.
1189
1190 ;; This is not as complicated as it seems. It is simply a list of common
1191 ;; display situations and the actions to take, plus commands for redrawing
1192 ;; the screen if it should be unaligned.
1193 ;;
1194 ;; We divide the check into two parts; whether we are at the end or not.
1195 ;; This is due to the fact that the end can actually be visible
1196 ;; in several window even though they are aligned.
1197
1198 (defun follow-post-command-hook ()
1199 "Ensure that the windows in Follow mode are adjacent after each command."
1200 (unless (input-pending-p)
1201 (let ((follow-inside-post-command-hook t)
1202 (win (selected-window)))
1203 ;; Work in the selected window, not in the current buffer.
1204 (with-current-buffer (window-buffer win)
1205 (unless (and (symbolp this-command)
1206 (get this-command 'follow-mode-use-cache))
1207 (setq follow-windows-start-end-cache nil))
1208 (follow-adjust-window win)))))
1209
1210 (defun follow-adjust-window (win)
1211 ;; Adjust the window WIN and its followers.
1212 (cl-assert (eq (window-buffer win) (current-buffer)))
1213 (when (and follow-mode
1214 (not (window-minibuffer-p win)))
1215 (let ((windows (follow-all-followers win)))
1216 ;; If we've explicitly scrolled, align the windows first.
1217 (when follow-fixed-window
1218 (follow-debug-message "fixed")
1219 (follow-redisplay windows win)
1220 (goto-char (follow-get-scrolled-point (point) windows))
1221 (setq follow-fixed-window nil))
1222 (let* ((dest (point))
1223 (win-start-end (progn
1224 (follow-update-window-start (car windows))
1225 (follow-windows-start-end windows)))
1226 (aligned (follow-windows-aligned-p win-start-end))
1227 (visible (follow-pos-visible dest win win-start-end))
1228 selected-window-up-to-date)
1229 (unless (and aligned visible)
1230 (setq follow-windows-start-end-cache nil))
1231
1232 ;; Select a window to display point.
1233 (unless follow-internal-force-redisplay
1234 (if (eq dest (point-max))
1235 ;; Be careful at point-max: the display can be aligned
1236 ;; while DEST can be visible in several windows.
1237 (cond
1238 ;; Select the current window, but only when the display
1239 ;; is correct. (When inserting characters in a tail
1240 ;; window, the display is not correct, as they are
1241 ;; shown twice.)
1242 ;;
1243 ;; Never stick to the current window after a deletion.
1244 ;; Otherwise, when typing `DEL' in a window showing
1245 ;; only the end of the file, a character would be
1246 ;; removed from the window above, which is very
1247 ;; unintuitive.
1248 ((and visible
1249 aligned
1250 (not (memq this-command
1251 '(backward-delete-char
1252 delete-backward-char
1253 backward-delete-char-untabify
1254 kill-region))))
1255 (follow-debug-message "Max: same"))
1256 ;; If the end is visible, and the window doesn't
1257 ;; seems like it just has been moved, select it.
1258 ((follow-select-if-end-visible win-start-end)
1259 (follow-debug-message "Max: end visible")
1260 (setq visible t aligned nil)
1261 (goto-char dest))
1262 ;; Just show the end...
1263 (t
1264 (follow-debug-message "Max: default")
1265 (select-window (car (last windows)))
1266 (goto-char dest)
1267 (setq visible nil aligned nil)))
1268
1269 ;; We're not at the end, here life is much simpler.
1270 (cond
1271 ;; This is the normal case!
1272 ;; It should be optimized for speed.
1273 ((and visible aligned)
1274 (follow-debug-message "same"))
1275 ;; Pick a position in any window. If the display is ok,
1276 ;; this picks the `correct' window.
1277 ((follow-select-if-visible dest win-start-end)
1278 (follow-debug-message "visible")
1279 (goto-char dest)
1280 ;; Perform redisplay, in case line is partially visible.
1281 (setq visible nil))
1282 ;; Not visible anywhere else, lets pick this one.
1283 (visible
1284 (follow-debug-message "visible in selected."))
1285 ;; If DEST is before the first window start, select the
1286 ;; first window.
1287 ((< dest (nth 1 (car win-start-end)))
1288 (follow-debug-message "before first")
1289 (select-window (car windows))
1290 (goto-char dest)
1291 (setq visible nil aligned nil))
1292 ;; If we can position the cursor without moving the first
1293 ;; window, do it. This is the case that catches `RET' at
1294 ;; the bottom of a window.
1295 ((follow-select-if-visible-from-first dest windows)
1296 (follow-debug-message "Below first")
1297 (setq visible t aligned t))
1298 ;; None of the above. Stick to the selected window.
1299 (t
1300 (follow-debug-message "None")
1301 (setq visible nil aligned nil))))
1302
1303 ;; If a new window was selected, make sure that the old is
1304 ;; not scrolled when point is outside the window.
1305 (unless (eq win (selected-window))
1306 (let ((p (window-point win)))
1307 (set-window-start win (window-start win) nil)
1308 (set-window-point win p))))
1309
1310 (unless visible
1311 ;; If point may not be visible in the selected window,
1312 ;; perform a redisplay; this ensures scrolling.
1313 (let ((opoint (point)))
1314 (redisplay)
1315 ;; If this `redisplay' moved point, we got clobbered by a
1316 ;; previous call to `set-window-start'. Try again.
1317 (when (/= (point) opoint)
1318 (goto-char opoint)
1319 (redisplay)))
1320
1321 (setq selected-window-up-to-date t)
1322 (follow-avoid-tail-recenter)
1323 (setq win-start-end (follow-windows-start-end windows)
1324 follow-windows-start-end-cache nil
1325 aligned nil))
1326
1327 ;; Now redraw the windows around the selected window.
1328 (unless (and (not follow-internal-force-redisplay)
1329 (or aligned
1330 (follow-windows-aligned-p win-start-end))
1331 (follow-point-visible-all-windows-p win-start-end))
1332 (setq follow-internal-force-redisplay nil)
1333 (follow-redisplay windows (selected-window)
1334 selected-window-up-to-date)
1335 (setq win-start-end (follow-windows-start-end windows)
1336 follow-windows-start-end-cache nil)
1337 ;; Point can end up in another window when DEST is at
1338 ;; the beginning of the buffer and the selected window is
1339 ;; not the first. It can also happen when long lines are
1340 ;; used and there is a big difference between the width of
1341 ;; the windows. (When scrolling one line in a wide window
1342 ;; which will cause a move larger that an entire small
1343 ;; window.)
1344 (unless (follow-pos-visible dest win win-start-end)
1345 (follow-select-if-visible dest win-start-end)
1346 (goto-char dest)))
1347
1348 ;; If the region is visible, make it look good when spanning
1349 ;; multiple windows.
1350 (when (region-active-p)
1351 (follow-maximize-region
1352 (selected-window) windows win-start-end))))
1353
1354 ;; Whether or not the buffer was in follow mode, update windows
1355 ;; displaying the tail so that Emacs won't recenter them.
1356 (follow-avoid-tail-recenter)))
1357
1358 ;;; The region
1359
1360 ;; Tries to make the highlighted area representing the region look
1361 ;; good when spanning several windows.
1362 ;;
1363 ;; Not perfect, as point can't be placed at window end, only at
1364 ;; end-1. This will highlight a little bit in windows above
1365 ;; the current.
1366
1367 (defun follow-maximize-region (win windows win-start-end)
1368 "Make a highlighted region stretching multiple windows look good."
1369 (let* ((all (follow-split-followers windows win))
1370 (pred (car all))
1371 (succ (cdr all))
1372 data)
1373 (while pred
1374 (setq data (assq (car pred) win-start-end))
1375 (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1)))
1376 (setq pred (cdr pred)))
1377 (while succ
1378 (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end)))
1379 (setq succ (cdr succ)))))
1380
1381 ;;; Scroll bar
1382
1383 ;;;; Scroll-bar support code.
1384
1385 ;; This handles the case where the user drags the scroll bar of a
1386 ;; non-selected window whose buffer is in Follow mode.
1387
1388 (declare-function scroll-bar-toolkit-scroll "scroll-bar" (event))
1389 (declare-function scroll-bar-drag "scroll-bar" (event))
1390 (declare-function scroll-bar-scroll-up "scroll-bar" (event))
1391 (declare-function scroll-bar-scroll-down "scroll-bar" (event))
1392 (declare-function mwheel-scroll "mwheel" (event))
1393
1394 (defun follow-scroll-bar-toolkit-scroll (event)
1395 (interactive "e")
1396 (scroll-bar-toolkit-scroll event)
1397 (follow-redraw-after-event event))
1398
1399 (defun follow-scroll-bar-drag (event)
1400 (interactive "e")
1401 (scroll-bar-drag event)
1402 (follow-redraw-after-event event))
1403
1404 (defun follow-scroll-bar-scroll-up (event)
1405 (interactive "e")
1406 (scroll-bar-scroll-up event)
1407 (follow-redraw-after-event event))
1408
1409 (defun follow-scroll-bar-scroll-down (event)
1410 (interactive "e")
1411 (scroll-bar-scroll-down event)
1412 (follow-redraw-after-event event))
1413
1414 (defun follow-mwheel-scroll (event)
1415 (interactive "e")
1416 (mwheel-scroll event)
1417 (follow-redraw-after-event event))
1418
1419 (defun follow-redraw-after-event (event)
1420 "Re-align the Follow mode windows affected by EVENT."
1421 (let* ((window (nth 0 (event-end event)))
1422 (buffer (window-buffer window))
1423 (orig-win (selected-window)))
1424 (when (and (buffer-local-value 'follow-mode buffer)
1425 ;; Ignore the case where we scroll the selected window;
1426 ;; that is handled by the post-command hook function.
1427 (not (eq window (selected-window))))
1428 (select-window window)
1429 (follow-redisplay)
1430 (unless (eq (window-buffer orig-win) buffer)
1431 (select-window orig-win)))))
1432
1433 ;;; Window size change
1434
1435 ;; The functions in `window-size-change-functions' are called every
1436 ;; time a window in a frame changes size, most notably after the frame
1437 ;; has been resized. We call `follow-post-command-hook' for every
1438 ;; Follow mode buffer visible in any window in the resized frame.
1439 ;;
1440 ;; Since `follow-window-size-change' can be called indirectly from
1441 ;; `follow-post-command-hook' we have a potential infinite loop. To
1442 ;; avoid this, we simply do not do anything in this situation. The
1443 ;; variable `follow-inside-post-command-hook' contains information
1444 ;; about whether the execution actually is inside the
1445 ;; post-command-hook or not.
1446
1447 (defun follow-window-size-change (frame)
1448 "Redraw all windows in FRAME, when in Follow mode."
1449 ;; Below, we call `post-command-hook'. Avoid an infloop.
1450 (unless follow-inside-post-command-hook
1451 (save-current-buffer
1452 (let ((orig-frame (selected-frame)))
1453 (select-frame frame)
1454 (let ((picked-window (selected-window)) ; Note: May change below.
1455 (seen-buffers '()))
1456 (unwind-protect
1457 (walk-windows
1458 (lambda (win)
1459 (let ((buf (window-buffer win)))
1460 (unless (memq buf seen-buffers)
1461 (set-buffer buf)
1462 (when follow-mode
1463 (let ((windows (follow-all-followers win)))
1464 (if (not (memq picked-window windows))
1465 (follow-redisplay windows win)
1466 ;; Make sure we're redrawing around the selected
1467 ;; window.
1468 (select-window picked-window 'norecord)
1469 (follow-post-command-hook)
1470 (setq picked-window (selected-window))))
1471 (push buf seen-buffers)))))
1472 'no-minibuf)
1473 (select-window picked-window 'norecord)))
1474 (select-frame orig-frame)))))
1475
1476 (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t)
1477
1478 ;;; Low level window start and end.
1479
1480 ;; These routines are the Follow Mode versions of the low level
1481 ;; functions described on page "Window Start and End" of the elisp
1482 ;; manual, e.g. `window-group-start'. The aim is to be able to handle
1483 ;; Follow Mode windows by replacing `window-start' by
1484 ;; `window-group-start', etc.
1485
1486 (defun follow-after-change (_beg _end _old-len)
1487 "After change function: set `follow-start-end-invalid'."
1488 (setq follow-start-end-invalid t))
1489
1490 (defun follow-window-start (&optional window)
1491 "Return position at which display currently starts in the
1492 Follow Mode group of windows which includes WINDOW.
1493
1494 WINDOW must be a live window and defaults to the selected one.
1495 This is updated by redisplay or by calling
1496 `follow-set-window-start'."
1497 (let ((windows (follow-all-followers window)))
1498 (window-start (car windows))))
1499
1500 (defun follow-window-end (&optional window update)
1501 "Return position at which display currently ends in the Follow
1502 Mode group of windows which includes WINDOW.
1503
1504 WINDOW must be a live window and defaults to the selected one.
1505 This is updated by redisplay, when it runs to completion.
1506 Simply changing the buffer text or setting `window-start' does
1507 not update this value.
1508
1509 Return nil if there is no recorded value. (This can happen if
1510 the last redisplay of WINDOW was preempted, and did not
1511 finish.) If UPDATE is non-nil, compute the up-to-date position
1512 if it isn't already recorded."
1513 (let* ((windows (follow-all-followers window))
1514 (last (car (last windows))))
1515 (when (and update follow-start-end-invalid)
1516 (follow-redisplay windows (car windows)))
1517 (window-end last update)))
1518
1519 (defun follow-set-window-start (window pos &optional noforce)
1520 "Make display in the Follow Mode group of windows which includes
1521 WINDOW start at position POS in WINDOW's buffer.
1522
1523 WINDOW must be a live window and defaults to the selected one. Return
1524 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1525 overriding motion of point in order to display at this exact start."
1526 (let ((windows (follow-all-followers window)))
1527 (setq follow-start-end-invalid t)
1528 (set-window-start (car windows) pos noforce)))
1529
1530 (defun follow-pos-visible-in-window-p (&optional pos window partially)
1531 "Return non-nil if position POS is currently on the frame in one of
1532 the windows in the Follow Mode group which includes WINDOW.
1533
1534 WINDOW must be a live window and defaults to the selected one.
1535
1536 Return nil if that position is scrolled vertically out of view. If a
1537 character is only partially visible, nil is returned, unless the
1538 optional argument PARTIALLY is non-nil. If POS is only out of view
1539 because of horizontal scrolling, return non-nil. If POS is t, it
1540 specifies the position of the last visible glyph in WINDOW. POS
1541 defaults to point in WINDOW; WINDOW defaults to the selected window.
1542
1543 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1544 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1545 where X and Y are the pixel coordinates relative to the top left corner
1546 of the actual window containing it. The remaining elements are
1547 omitted if the character after POS is fully visible; otherwise, RTOP
1548 and RBOT are the number of pixels off-window at the top and bottom of
1549 the screen line (\"row\") containing POS, ROWH is the visible height
1550 of that row, and VPOS is the row number \(zero-based)."
1551 (let* ((windows (follow-all-followers window))
1552 (last (car (last windows))))
1553 (when follow-start-end-invalid
1554 (follow-redisplay windows (car windows)))
1555 (let* ((cache (follow-windows-start-end windows))
1556 (last-elt (car (last cache)))
1557 our-pos pertinent-elt)
1558 (setq pertinent-elt
1559 (if (eq pos t)
1560 last-elt
1561 (setq our-pos (or pos (point)))
1562 (catch 'element
1563 (while cache
1564 (when (< our-pos (nth 2 (car cache)))
1565 (throw 'element (car cache)))
1566 (setq cache (cdr cache)))
1567 last-elt)))
1568 (pos-visible-in-window-p our-pos (car pertinent-elt) partially))))
1569
1570 (defun follow-move-to-window-line (arg)
1571 "Position point relative to the Follow mode group containing the selected window.
1572 ARG nil means position point at center of the window group.
1573 Else, ARG specifies vertical position within the window group;
1574 zero means top of the first window in the group, negative means
1575 relative to bottom of the last window in the group."
1576 (let* ((windows (follow-all-followers))
1577 (start-end (follow-windows-start-end windows))
1578 (rev-start-end (reverse start-end))
1579 (lines 0)
1580 middle-window elt count)
1581 (select-window
1582 (cond
1583 ((null arg)
1584 (setq rev-start-end (nthcdr (/ (length windows) 2) rev-start-end))
1585 (prog1 (car (car rev-start-end))
1586 (while (setq rev-start-end (cdr rev-start-end))
1587 (setq elt (car rev-start-end)
1588 count (count-screen-lines (cadr elt) (nth 2 elt)
1589 nil (car elt))
1590 lines (+ lines count)))))
1591 ((>= arg 0)
1592 (while (and (cdr start-end)
1593 (progn
1594 (setq elt (car start-end)
1595 count (count-screen-lines (cadr elt) (nth 2 elt)
1596 nil (car elt)))
1597 (>= arg count)))
1598 (setq arg (- arg count)
1599 lines (+ lines count)
1600 start-end (cdr start-end)))
1601 (car (car start-end)))
1602 (t ; (< arg 0)
1603 (while (and (cadr rev-start-end)
1604 (progn
1605 (setq elt (car rev-start-end)
1606 count (count-lines (cadr elt) (nth 2 elt)))
1607 (<= arg (- count))))
1608 (setq arg (+ arg count)
1609 rev-start-end (cdr rev-start-end)))
1610 (prog1 (car (car rev-start-end))
1611 (while (setq rev-start-end (cdr rev-start-end))
1612 (setq elt (car rev-start-end)
1613 count (count-screen-lines (cadr elt) (nth 2 elt)
1614 nil (car elt))
1615 lines (+ lines count)))))))
1616 (+ lines (move-to-window-line arg))))
1617
1618 ;;; Profile support
1619
1620 ;; The following (non-evaluated) section can be used to
1621 ;; profile this package using `elp'.
1622 ;;
1623 ;; Invalid indentation on purpose!
1624
1625 ;; (setq elp-function-list
1626 ;; '(window-end
1627 ;; vertical-motion
1628 ;; follow-mode
1629 ;; follow-all-followers
1630 ;; follow-split-followers
1631 ;; follow-redisplay
1632 ;; follow-estimate-first-window-start
1633 ;; follow-calculate-first-window-start-from-above
1634 ;; follow-calculate-first-window-start-from-below
1635 ;; follow-calc-win-end
1636 ;; follow-calc-win-start
1637 ;; follow-pos-visible
1638 ;; follow-windows-start-end
1639 ;; follow-cache-valid-p
1640 ;; follow-select-if-visible
1641 ;; follow-select-if-visible-from-first
1642 ;; follow-windows-aligned-p
1643 ;; follow-point-visible-all-windows-p
1644 ;; follow-avoid-tail-recenter
1645 ;; follow-update-window-start
1646 ;; follow-post-command-hook))
1647
1648 (provide 'follow)
1649
1650 ;; /------------------------------------------------------------------------\
1651 ;; | "I [..] am rarely happier then when spending an entire day programming |
1652 ;; | my computer to perform automatically a task that it would otherwise |
1653 ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, |
1654 ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well |
1655 ;; | worth the investment of a day's happy activity working out a way to |
1656 ;; | save it". -- Douglas Adams, "Last Chance to See" |
1657 ;; \------------------------------------------------------------------------/
1658
1659 ;;; follow.el ends here