Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; follow.el --- synchronize windows showing the same buffer |
e79016aa | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1995-1997, 1999, 2001-2011 Free Software Foundation, Inc. |
e79016aa | 4 | |
c86b6fd2 | 5 | ;; Author: Anders Lindgren <andersl@andersl.com> |
ec8d89ec | 6 | ;; Maintainer: FSF (Anders' email bounces, Sep 2005) |
c86b6fd2 | 7 | ;; Created: 1995-05-25 |
f5f727f8 | 8 | ;; Keywords: display, window, minor-mode, convenience |
e79016aa | 9 | |
dcde1278 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
e79016aa | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
e79016aa | 16 | |
dcde1278 | 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
e79016aa KH |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
e79016aa KH |
24 | |
25 | ;;; Commentary: | |
26 | ||
27 | ;;{{{ Documentation | |
28 | ||
ff753438 | 29 | ;; `Follow mode' is a minor mode for Emacs and XEmacs that |
e79016aa KH |
30 | ;; combines windows into one tall virtual window. |
31 | ;; | |
32 | ;; The feeling of a "virtual window" has been accomplished by the use | |
33 | ;; of two major techniques: | |
34 | ;; | |
1ece6cdb | 35 | ;; * The windows always display adjacent sections of the buffer. |
e79016aa | 36 | ;; This means that whenever one window is moved, all the |
17f6d002 | 37 | ;; others will follow. (Hence the name Follow mode.) |
e79016aa | 38 | ;; |
cb02be17 KH |
39 | ;; * Should the point (cursor) end up outside a window, another |
40 | ;; window displaying that point is selected, if possible. This | |
41 | ;; makes it possible to walk between windows using normal cursor | |
e79016aa KH |
42 | ;; movement commands. |
43 | ;; | |
ff753438 | 44 | ;; Follow mode comes to its prime when a large screen and two |
08a1dbe6 | 45 | ;; side-by-side window are used. The user can, with the help of Follow |
1ece6cdb EZ |
46 | ;; mode, use two full-height windows as though they are one. |
47 | ;; Imagine yourself editing a large function, or section of text, | |
c8d05b03 | 48 | ;; and being able to use 144 lines instead of the normal 72... (your |
e79016aa KH |
49 | ;; mileage may vary). |
50 | ||
e79016aa | 51 | ;; To test this package, make sure `follow' is loaded, or will be |
08a1dbe6 | 52 | ;; autoloaded when activated (see below). Then do the following: |
e79016aa | 53 | ;; |
ff753438 | 54 | ;; * Find your favorite file (preferably a long one). |
e79016aa | 55 | ;; |
ff753438 RS |
56 | ;; * Resize Emacs so that it will be wide enough for two full size |
57 | ;; columns. Delete the other windows and split the window with | |
58 | ;; the commands `C-x 1 C-x 3'. | |
e79016aa KH |
59 | ;; |
60 | ;; * Give the command: | |
61 | ;; M-x follow-mode <RETURN> | |
62 | ;; | |
63 | ;; * Now the display should look something like (assuming the text "71" | |
64 | ;; is on line 71): | |
65 | ;; | |
66 | ;; +----------+----------+ | |
67 | ;; |1 |73 | | |
68 | ;; |2 |74 | | |
69 | ;; |3 |75 | | |
cb02be17 | 70 | ;; ... ... |
e79016aa KH |
71 | ;; |71 |143 | |
72 | ;; |72 |144 | | |
73 | ;; +----------+----------+ | |
74 | ;; | |
75 | ;; As you can see, the right-hand window starts at line 73, the line | |
08a1dbe6 | 76 | ;; immediately below the end of the left-hand window. As long as |
1ece6cdb | 77 | ;; `follow-mode' is active, the two windows will follow each other! |
e79016aa KH |
78 | ;; |
79 | ;; * Play around and enjoy! Scroll one window and watch the other. | |
08a1dbe6 SM |
80 | ;; Jump to the beginning or end. Press `Cursor down' at the last |
81 | ;; line of the left-hand window. Enter new lines into the | |
82 | ;; text. Enter long lines spanning several lines, or several | |
e79016aa KH |
83 | ;; windows. |
84 | ;; | |
85 | ;; * Should you find `Follow' mode annoying, just type | |
cb02be17 | 86 | ;; M-x follow-mode <RETURN> |
e79016aa KH |
87 | ;; to turn it off. |
88 | ||
89 | ||
e79016aa KH |
90 | ;; The command `follow-delete-other-windows-and-split' maximises the |
91 | ;; visible area of the current buffer. | |
92 | ;; | |
93 | ;; I recommend adding it, and `follow-mode', to hotkeys in the global | |
94 | ;; key map. To do so, add the following lines (replacing `[f7]' and | |
95 | ;; `[f8]' with your favorite keys) to the init file: | |
96 | ;; | |
e79016aa | 97 | ;; (global-set-key [f8] 'follow-mode) |
e79016aa KH |
98 | ;; (global-set-key [f7] 'follow-delete-other-windows-and-split) |
99 | ||
100 | ||
1ece6cdb EZ |
101 | ;; There exist two system variables that control the appearence of |
102 | ;; lines wider than the window containing them. The default is to | |
103 | ;; truncate long lines whenever a window isn't as wide as the frame. | |
e79016aa KH |
104 | ;; |
105 | ;; To make sure lines are never truncated, please place the following | |
106 | ;; lines in your init file: | |
107 | ;; | |
108 | ;; (setq truncate-lines nil) | |
109 | ;; (setq truncate-partial-width-windows nil) | |
110 | ||
111 | ||
f3bfa025 KH |
112 | ;; Since the display of XEmacs is pixel-oriented, a line could be |
113 | ;; clipped in half at the bottom of the window. | |
114 | ;; | |
115 | ;; To make XEmacs avoid clipping (normal) lines, please place the | |
116 | ;; following line in your init-file: | |
117 | ;; | |
118 | ;; (setq pixel-vertical-clip-threshold 30) | |
119 | ||
120 | ||
5eba16a3 | 121 | ;; The correct way to configurate Follow mode, or any other mode for |
1ece6cdb EZ |
122 | ;; that matter, is to create one or more functions that do |
123 | ;; whatever you would like to do. These functions are then added to | |
e79016aa KH |
124 | ;; a hook. |
125 | ;; | |
126 | ;; When `Follow' mode is activated, functions stored in the hook | |
127 | ;; `follow-mode-hook' are called. When it is deactivated | |
28ccdfff | 128 | ;; `follow-mode-off-hook' is run. |
e79016aa KH |
129 | ;; |
130 | ;; The keymap `follow-key-map' contains key bindings activated by | |
131 | ;; `follow-mode'. | |
132 | ;; | |
133 | ;; Example: | |
134 | ;; (add-hook 'follow-mode-hook 'my-follow-mode-hook) | |
135 | ;; | |
136 | ;; (defun my-follow-mode-hook () | |
137 | ;; (define-key follow-mode-map "\C-ca" 'your-favorite-function) | |
138 | ;; (define-key follow-mode-map "\C-cb" 'another-function)) | |
139 | ||
140 | ||
141 | ;; Usage: | |
142 | ;; | |
1ece6cdb EZ |
143 | ;; To activate, issue the command "M-x follow-mode" |
144 | ;; and press Return. To deactivate, do it again. | |
e79016aa | 145 | ;; |
ff753438 RS |
146 | ;; The following is a list of commands useful when follow-mode is active. |
147 | ;; | |
e79016aa | 148 | ;; follow-scroll-up C-c . C-v |
17f6d002 | 149 | ;; Scroll text in a Follow mode window chain up. |
e79016aa KH |
150 | ;; |
151 | ;; follow-scroll-down C-c . v | |
152 | ;; Like `follow-scroll-up', but in the other direction. | |
153 | ;; | |
154 | ;; follow-delete-other-windows-and-split C-c . 1 | |
1ece6cdb | 155 | ;; Maximize the visible area of the current buffer, |
17f6d002 JB |
156 | ;; and enter Follow mode. This is a very convenient |
157 | ;; way to start Follow mode, hence we recomend that | |
1ece6cdb | 158 | ;; this command be added to the global keymap. |
e79016aa KH |
159 | ;; |
160 | ;; follow-recenter C-c . C-l | |
161 | ;; Place the point in the center of the middle window, | |
162 | ;; or a specified number of lines from either top or bottom. | |
163 | ;; | |
164 | ;; follow-switch-to-buffer C-c . b | |
165 | ;; Switch buffer in all windows displaying the current buffer | |
166 | ;; in this frame. | |
167 | ;; | |
168 | ;; follow-switch-to-buffer-all C-c . C-b | |
1ece6cdb | 169 | ;; Switch buffer in all windows in the selected frame. |
e79016aa KH |
170 | ;; |
171 | ;; follow-switch-to-current-buffer-all | |
172 | ;; Show the current buffer in all windows on the current | |
173 | ;; frame and turn on `follow-mode'. | |
174 | ;; | |
175 | ;; follow-first-window C-c . < | |
176 | ;; Select the first window in the frame showing the same buffer. | |
177 | ;; | |
178 | ;; follow-last-window C-c . > | |
179 | ;; Select the last window in the frame showing the same buffer. | |
180 | ;; | |
181 | ;; follow-next-window C-c . n | |
182 | ;; Select the next window in the frame showing the same buffer. | |
183 | ;; | |
184 | ;; follow-previous-window C-c . p | |
185 | ;; Select the previous window showing the same buffer. | |
186 | ||
187 | ||
188 | ;; Well, it seems ok, but what if I really want to look at two different | |
189 | ;; positions in the text? Here are two simple methods to use: | |
190 | ;; | |
191 | ;; 1) Use multiple frames; `follow' mode only affects windows displayed | |
5eba16a3 | 192 | ;; in the same frame. (My apologies to you who can't use frames.) |
e79016aa KH |
193 | ;; |
194 | ;; 2) Bind `follow-mode' to key so you can turn it off whenever | |
08a1dbe6 | 195 | ;; you want to view two locations. Of course, `follow' mode can |
e79016aa KH |
196 | ;; be reactivated by hitting the same key again. |
197 | ;; | |
198 | ;; Example from my ~/.emacs: | |
199 | ;; (global-set-key [f8] 'follow-mode) | |
200 | ||
201 | ||
cb02be17 | 202 | ;; Implementation: |
e79016aa KH |
203 | ;; |
204 | ;; In an ideal world, follow mode would have been implemented in the | |
ff753438 | 205 | ;; kernel of the display routines, making sure that the windows (using |
1ece6cdb | 206 | ;; follow mode) ALWAYS are aligned. On planet Earth, however, we must |
e79016aa KH |
207 | ;; accept a solution where we ALMOST ALWAYS can make sure that the |
208 | ;; windows are aligned. | |
209 | ;; | |
210 | ;; Follow mode does this in three places: | |
211 | ;; 1) After each user command. | |
5eba16a3 | 212 | ;; 2) After a process output has been performed. |
e79016aa KH |
213 | ;; 3) When a scrollbar has been moved. |
214 | ;; | |
215 | ;; This will cover most situations. (Let me know if there are other | |
ff753438 | 216 | ;; situations that should be covered.) |
e79016aa | 217 | ;; |
ff753438 | 218 | ;; Note that only the selected window is checked, for the reason of |
5eba16a3 JB |
219 | ;; efficiency and code complexity. (I.e. it is possible to make a |
220 | ;; non-selected window unaligned. It will, however, pop right back | |
e79016aa KH |
221 | ;; when it is selected.) |
222 | ||
e79016aa KH |
223 | ;;}}} |
224 | ||
225 | ;;; Code: | |
226 | ||
227 | ;;{{{ Preliminaries | |
228 | ||
229 | ;; Make the compiler shut up! | |
230 | ;; There are two strategies: | |
231 | ;; 1) Shut warnings off completely. | |
232 | ;; 2) Handle each warning separately. | |
233 | ;; | |
234 | ;; Since I would like to see real errors, I've selected the latter | |
235 | ;; method. | |
236 | ;; | |
237 | ;; The problem with undefined variables and functions has been solved | |
238 | ;; by using `set', `symbol-value' and `symbol-function' rather than | |
239 | ;; `setq' and direct references to variables and functions. | |
240 | ;; | |
241 | ;; For example: | |
242 | ;; (if (boundp 'foo) ... (symbol-value 'foo) ) | |
243 | ;; (set 'foo ...) <-- XEmacs doesn't fall for this one. | |
244 | ;; (funcall (symbol-function 'set) 'bar ...) | |
245 | ;; | |
246 | ;; Note: When this file is interpreted, `eval-when-compile' is | |
5eba16a3 | 247 | ;; evaluated. Since it doesn't hurt to evaluate it, but it is a bit |
ff753438 RS |
248 | ;; annoying, we test if the byte-compiler has been loaded. This can, |
249 | ;; of course, lead to some occasional unintended evaluation... | |
e79016aa KH |
250 | ;; |
251 | ;; Should someone come up with a better solution, please let me | |
252 | ;; know. | |
253 | ||
9271083a JB |
254 | (require 'easymenu) |
255 | ||
e79016aa KH |
256 | (eval-when-compile |
257 | (if (or (featurep 'bytecomp) | |
258 | (featurep 'byte-compile)) | |
08a1dbe6 | 259 | (cond ((featurep 'xemacs) |
e79016aa KH |
260 | ;; Make XEmacs shut up! I'm using standard Emacs |
261 | ;; functions, they are NOT obsolete! | |
262 | (if (eq (get 'force-mode-line-update 'byte-compile) | |
263 | 'byte-compile-obsolete) | |
264 | (put 'force-mode-line-update 'byte-compile 'nil)) | |
265 | (if (eq (get 'frame-first-window 'byte-compile) | |
266 | 'byte-compile-obsolete) | |
267 | (put 'frame-first-window 'byte-compile 'nil)))))) | |
268 | ||
269 | ;;}}} | |
270 | ;;{{{ Variables | |
271 | ||
4bef9110 SE |
272 | (defgroup follow nil |
273 | "Synchronize windows showing the same buffer." | |
274 | :prefix "follow-" | |
f5f727f8 DN |
275 | :group 'windows |
276 | :group 'convenience) | |
4bef9110 | 277 | |
4bef9110 | 278 | (defcustom follow-mode-hook nil |
eceb3266 | 279 | "Normal hook run by `follow-mode'." |
4bef9110 SE |
280 | :type 'hook |
281 | :group 'follow) | |
e79016aa | 282 | |
4bef9110 | 283 | (defcustom follow-mode-off-hook nil |
8cd3480b | 284 | "Hooks to run when Follow mode is turned off." |
4bef9110 SE |
285 | :type 'hook |
286 | :group 'follow) | |
eceb3266 | 287 | (make-obsolete-variable 'follow-mode-off-hook 'follow-mode-hook "22.2") |
e79016aa | 288 | |
08a1dbe6 | 289 | ;;{{{ Keymap/Menu |
e79016aa | 290 | |
08a1dbe6 SM |
291 | ;; Define keys for the follow-mode minor mode map and replace some |
292 | ;; functions in the global map. All `follow' mode special functions | |
293 | ;; can be found on (the somewhat cumbersome) "C-c . <key>" | |
294 | ;; (Control-C dot <key>). (As of Emacs 19.29 the keys | |
295 | ;; C-c <punctuation character> are reserved for minor modes.) | |
296 | ;; | |
297 | ;; To change the prefix, redefine `follow-mode-prefix' before | |
298 | ;; `follow' is loaded, or see the section on `follow-mode-hook' | |
299 | ;; above for an example of how to bind the keys the way you like. | |
300 | ;; | |
301 | ;; Please note that the keymap is defined the first time this file is | |
9b9a4122 | 302 | ;; loaded. Also note that the only valid way to manipulate the |
08a1dbe6 SM |
303 | ;; keymap is to use `define-key'. Don't change it using `setq' or |
304 | ;; similar! | |
e79016aa | 305 | |
4bef9110 | 306 | (defcustom follow-mode-prefix "\C-c." |
08a1dbe6 | 307 | "Prefix key to use for follow commands in Follow mode. |
e79016aa | 308 | The value of this variable is checked as part of loading Follow mode. |
4bef9110 SE |
309 | After that, changing the prefix key requires manipulating keymaps." |
310 | :type 'string | |
311 | :group 'follow) | |
312 | ||
08a1dbe6 SM |
313 | (defvar follow-mode-map |
314 | (let ((mainmap (make-sparse-keymap)) | |
315 | (map (make-sparse-keymap))) | |
e79016aa KH |
316 | (define-key map "\C-v" 'follow-scroll-up) |
317 | (define-key map "\M-v" 'follow-scroll-down) | |
318 | (define-key map "v" 'follow-scroll-down) | |
319 | (define-key map "1" 'follow-delete-other-windows-and-split) | |
320 | (define-key map "b" 'follow-switch-to-buffer) | |
321 | (define-key map "\C-b" 'follow-switch-to-buffer-all) | |
322 | (define-key map "\C-l" 'follow-recenter) | |
323 | (define-key map "<" 'follow-first-window) | |
324 | (define-key map ">" 'follow-last-window) | |
325 | (define-key map "n" 'follow-next-window) | |
326 | (define-key map "p" 'follow-previous-window) | |
327 | ||
08a1dbe6 | 328 | (define-key mainmap follow-mode-prefix map) |
e79016aa | 329 | |
17f6d002 | 330 | ;; Replace the standard `end-of-buffer', when in Follow mode. (I |
ff753438 | 331 | ;; don't see the point in trying to replace every function that |
e79016aa KH |
332 | ;; could be enhanced in Follow mode. End-of-buffer is a special |
333 | ;; case since it is very simple to define and it greatly enhances | |
334 | ;; the look and feel of Follow mode.) | |
08a1dbe6 | 335 | (define-key mainmap [remap end-of-buffer] 'follow-end-of-buffer) |
e79016aa | 336 | |
08a1dbe6 SM |
337 | mainmap) |
338 | "Minor mode keymap for Follow mode.") | |
e79016aa | 339 | |
7dcef48d SM |
340 | ;; When the mode is not activated, only one item is visible to activate |
341 | ;; the mode. | |
342 | (defun follow-menu-filter (menu) | |
17f6d002 | 343 | (if (bound-and-true-p follow-mode) |
7dcef48d | 344 | menu |
17f6d002 | 345 | '(["Follow mode" follow-mode |
7dcef48d SM |
346 | :style toggle :selected follow-mode]))) |
347 | ||
348 | ;; If there is a `tools' menu, we use it. However, we can't add a | |
349 | ;; minor-mode specific item to it (it's broken), so we make the | |
350 | ;; contents ghosted when not in use, and add ourselves to the | |
351 | ;; global map. | |
352 | (easy-menu-add-item nil '("Tools") | |
353 | '("Follow" | |
354 | ;; The Emacs code used to just grey out operations when follow-mode was | |
355 | ;; not enabled, whereas the XEmacs code used to remove it altogether. | |
356 | ;; Not sure which is preferable, but clearly the preference should not | |
357 | ;; depend on the flavor. | |
358 | :filter follow-menu-filter | |
359 | ["Scroll Up" follow-scroll-up follow-mode] | |
360 | ["Scroll Down" follow-scroll-down follow-mode] | |
361 | "--" | |
362 | ["Delete Other Windows and Split" follow-delete-other-windows-and-split follow-mode] | |
363 | "--" | |
364 | ["Switch To Buffer" follow-switch-to-buffer follow-mode] | |
365 | ["Switch To Buffer (all windows)" follow-switch-to-buffer-all follow-mode] | |
366 | "--" | |
367 | ["First Window" follow-first-window follow-mode] | |
368 | ["Last Window" follow-last-window follow-mode] | |
369 | ["Next Window" follow-next-window follow-mode] | |
370 | ["Previous Window" follow-previous-window follow-mode] | |
371 | "--" | |
372 | ["Recenter" follow-recenter follow-mode] | |
373 | "--" | |
374 | ["Follow mode" follow-mode :style toggle :selected follow-mode])) | |
375 | ||
08a1dbe6 | 376 | ;;}}} |
e79016aa | 377 | |
08a1dbe6 SM |
378 | (defcustom follow-mode-line-text " Follow" |
379 | "Text shown in the mode line when Follow mode is active. | |
380 | Defaults to \" Follow\". Examples of other values | |
381 | are \" Fw\", or simply \"\"." | |
382 | :type 'string | |
383 | :group 'follow) | |
384 | ||
385 | (defcustom follow-auto nil | |
386 | "Non-nil activates Follow mode whenever a file is loaded." | |
387 | :type 'boolean | |
388 | :group 'follow) | |
389 | ||
390 | (defcustom follow-intercept-processes (fboundp 'start-process) | |
17f6d002 | 391 | "When non-nil, Follow mode will monitor process output." |
08a1dbe6 SM |
392 | :type 'boolean |
393 | :group 'follow) | |
394 | ||
395 | (defvar follow-avoid-tail-recenter-p (not (featurep 'xemacs)) | |
f02725b4 | 396 | "*When non-nil, patch Emacs so that tail windows won't be recentered. |
08a1dbe6 SM |
397 | |
398 | A \"tail window\" is a window that displays only the end of | |
399 | the buffer. Normally it is practical for the user that empty | |
400 | windows are recentered automatically. However, when using | |
17f6d002 | 401 | Follow mode it breaks the display when the end is displayed |
08a1dbe6 SM |
402 | in a window \"above\" the last window. This is for |
403 | example the case when displaying a short page in info. | |
404 | ||
17f6d002 | 405 | Must be set before Follow mode is loaded. |
08a1dbe6 SM |
406 | |
407 | Please note that it is not possible to fully prevent Emacs from | |
408 | recentering empty windows. Please report if you find a repeatable | |
409 | situation in which Emacs recenters empty windows. | |
410 | ||
411 | XEmacs, as of 19.12, does not recenter windows, good!") | |
412 | ||
413 | (defvar follow-cache-command-list | |
414 | '(next-line previous-line forward-char backward-char) | |
415 | "List of commands that don't require recalculation. | |
416 | ||
417 | In order to be able to use the cache, a command should not change the | |
418 | contents of the buffer, nor should it change selected window or current | |
419 | buffer. | |
420 | ||
421 | The commands in this list are checked at load time. | |
422 | ||
423 | To mark other commands as suitable for caching, set the symbol | |
424 | property `follow-mode-use-cache' to non-nil.") | |
425 | ||
426 | (defvar follow-debug nil | |
427 | "*Non-nil when debugging Follow mode.") | |
428 | ||
429 | ||
430 | ;; Internal variables: | |
431 | ||
432 | (defvar follow-internal-force-redisplay nil | |
433 | "True when Follow mode should redisplay the windows.") | |
434 | ||
435 | (defvar follow-process-filter-alist '() | |
436 | "The original filters for processes intercepted by Follow mode.") | |
437 | ||
438 | (defvar follow-active-menu nil | |
439 | "The menu visible when Follow mode is active.") | |
440 | ||
441 | (defvar follow-deactive-menu nil | |
442 | "The menu visible when Follow mode is deactivated.") | |
443 | ||
444 | (defvar follow-inside-post-command-hook nil | |
445 | "Non-nil when inside Follow modes `post-command-hook'. | |
446 | Used by `follow-window-size-change'.") | |
447 | ||
448 | (defvar follow-windows-start-end-cache nil | |
449 | "Cache used by `follow-window-start-end'.") | |
450 | ||
451 | ;;}}} | |
452 | ;;{{{ Debug messages | |
453 | ||
454 | ;; This inline function must be as small as possible! | |
455 | ;; Maybe we should define a macro that expands to nil if | |
456 | ;; the variable is not set. | |
457 | ||
458 | (defsubst follow-debug-message (&rest args) | |
5eba16a3 | 459 | "Like `message', but only active when `follow-debug' is non-nil." |
08a1dbe6 SM |
460 | (if (and (boundp 'follow-debug) follow-debug) |
461 | (apply 'message args))) | |
e79016aa | 462 | |
f3bfa025 KH |
463 | ;;}}} |
464 | ;;{{{ Cache | |
465 | ||
08a1dbe6 SM |
466 | (dolist (cmd follow-cache-command-list) |
467 | (put cmd 'follow-mode-use-cache t)) | |
f3bfa025 | 468 | |
e79016aa KH |
469 | ;;}}} |
470 | ||
471 | ;;{{{ The mode | |
472 | ||
473 | ;;;###autoload | |
474 | (defun turn-on-follow-mode () | |
08a1dbe6 | 475 | "Turn on Follow mode. Please see the function `follow-mode'." |
e79016aa KH |
476 | (follow-mode 1)) |
477 | ||
478 | ||
479 | ;;;###autoload | |
480 | (defun turn-off-follow-mode () | |
08a1dbe6 | 481 | "Turn off Follow mode. Please see the function `follow-mode'." |
e79016aa KH |
482 | (follow-mode -1)) |
483 | ||
08a1dbe6 | 484 | (put 'follow-mode 'permanent-local t) |
e79016aa | 485 | ;;;###autoload |
08a1dbe6 | 486 | (define-minor-mode follow-mode |
06e21633 CY |
487 | "Toggle Follow mode. |
488 | With a prefix argument ARG, enable Follow mode if ARG is | |
489 | positive, and disable it otherwise. If called from Lisp, enable | |
490 | the mode if ARG is omitted or nil. | |
e79016aa | 491 | |
06e21633 CY |
492 | Follow mode is a minor mode that combines windows into one tall |
493 | virtual window. This is accomplished by two main techniques: | |
e79016aa | 494 | |
cb02be17 | 495 | * The windows always displays adjacent sections of the buffer. |
e79016aa | 496 | This means that whenever one window is moved, all the |
17f6d002 | 497 | others will follow. (Hence the name Follow mode.) |
e79016aa | 498 | |
cb02be17 KH |
499 | * Should the point (cursor) end up outside a window, another |
500 | window displaying that point is selected, if possible. This | |
501 | makes it possible to walk between windows using normal cursor | |
e79016aa KH |
502 | movement commands. |
503 | ||
504 | Follow mode comes to its prime when used on a large screen and two | |
3eaf40f7 | 505 | side-by-side windows are used. The user can, with the help of Follow |
e79016aa | 506 | mode, use two full-height windows as though they would have been |
8cd3480b | 507 | one. Imagine yourself editing a large function, or section of text, |
c8d05b03 | 508 | and being able to use 144 lines instead of the normal 72... (your |
e79016aa KH |
509 | mileage may vary). |
510 | ||
511 | To split one large window into two side-by-side windows, the commands | |
2d197ffb | 512 | `\\[split-window-right]' or \ |
e79016aa KH |
513 | `M-x follow-delete-other-windows-and-split' can be used. |
514 | ||
8cd3480b | 515 | Only windows displayed in the same frame follow each other. |
e79016aa KH |
516 | |
517 | If the variable `follow-intercept-processes' is non-nil, Follow mode | |
518 | will listen to the output of processes and redisplay accordingly. | |
519 | \(This is the default.) | |
520 | ||
eceb3266 | 521 | This command runs the normal hook `follow-mode-hook'. |
e79016aa KH |
522 | |
523 | Keys specific to Follow mode: | |
524 | \\{follow-mode-map}" | |
08a1dbe6 | 525 | :keymap follow-mode-map |
0a957b2f JB |
526 | (when (and follow-mode follow-intercept-processes) |
527 | (follow-intercept-process-output)) | |
08a1dbe6 SM |
528 | (cond (follow-mode ; On |
529 | ;; XEmacs: If this is non-nil, the window will scroll before | |
530 | ;; the point will have a chance to get into the next window. | |
0a957b2f JB |
531 | (when (boundp 'scroll-on-clipped-lines) |
532 | (setq scroll-on-clipped-lines nil)) | |
08a1dbe6 | 533 | (force-mode-line-update) |
0a957b2f | 534 | (add-hook 'post-command-hook 'follow-post-command-hook t)) |
08a1dbe6 SM |
535 | |
536 | ((not follow-mode) ; Off | |
0a957b2f | 537 | (force-mode-line-update)))) |
e79016aa KH |
538 | |
539 | ;;}}} | |
540 | ;;{{{ Find file hook | |
541 | ||
542 | ;; This will start follow-mode whenever a new file is loaded, if | |
543 | ;; the variable `follow-auto' is non-nil. | |
544 | ||
ffc30f4f | 545 | (add-hook 'find-file-hook 'follow-find-file-hook t) |
e79016aa KH |
546 | |
547 | (defun follow-find-file-hook () | |
17f6d002 | 548 | "Find-file hook for Follow mode. See the variable `follow-auto'." |
e79016aa KH |
549 | (if follow-auto (follow-mode t))) |
550 | ||
551 | ;;}}} | |
552 | ||
553 | ;;{{{ User functions | |
554 | ||
555 | ;;; | |
556 | ;;; User functions usable when in Follow mode. | |
557 | ;;; | |
558 | ||
559 | ;;{{{ Scroll | |
560 | ||
17f6d002 | 561 | ;; `scroll-up' and `-down', but for windows in Follow mode. |
e79016aa KH |
562 | ;; |
563 | ;; Almost like the real thing, excpet when the cursor ends up outside | |
564 | ;; the top or bottom... In our case however, we end up outside the | |
565 | ;; window and hence we are recenterd. Should we let `recenter' handle | |
566 | ;; the point position we would never leave the selected window. To do | |
567 | ;; it ourselves we would need to do our own redisplay, which is easier | |
568 | ;; said than done. (Why didn't I do a real display abstraction from | |
569 | ;; the beginning?) | |
570 | ;; | |
571 | ;; We must sometimes set `follow-internal-force-redisplay', otherwise | |
572 | ;; our post-command-hook will move our windows back into the old | |
573 | ;; position... (This would also be corrected if we would have had a | |
574 | ;; good redisplay abstraction.) | |
575 | ||
576 | (defun follow-scroll-up (&optional arg) | |
17f6d002 | 577 | "Scroll text in a Follow mode window chain up. |
e79016aa KH |
578 | |
579 | If called with no ARG, the `next-screen-context-lines' last lines of | |
580 | the bottom window in the chain will be visible in the top window. | |
581 | ||
582 | If called with an argument, scroll ARG lines up. | |
583 | Negative ARG means scroll downward. | |
584 | ||
17f6d002 | 585 | Works like `scroll-up' when not in Follow mode." |
e79016aa KH |
586 | (interactive "P") |
587 | (cond ((not (and (boundp 'follow-mode) follow-mode)) | |
588 | (scroll-up arg)) | |
589 | (arg | |
590 | (save-excursion (scroll-up arg)) | |
591 | (setq follow-internal-force-redisplay t)) | |
592 | (t | |
593 | (let* ((windows (follow-all-followers)) | |
594 | (end (window-end (car (reverse windows))))) | |
595 | (if (eq end (point-max)) | |
596 | (signal 'end-of-buffer nil) | |
597 | (select-window (car windows)) | |
ff753438 RS |
598 | ;; `window-end' might return nil. |
599 | (if end | |
600 | (goto-char end)) | |
e79016aa KH |
601 | (vertical-motion (- next-screen-context-lines)) |
602 | (set-window-start (car windows) (point))))))) | |
603 | ||
604 | ||
605 | (defun follow-scroll-down (&optional arg) | |
17f6d002 | 606 | "Scroll text in a Follow mode window chain down. |
e79016aa KH |
607 | |
608 | If called with no ARG, the `next-screen-context-lines' top lines of | |
609 | the top window in the chain will be visible in the bottom window. | |
610 | ||
611 | If called with an argument, scroll ARG lines down. | |
612 | Negative ARG means scroll upward. | |
613 | ||
17f6d002 | 614 | Works like `scroll-up' when not in Follow mode." |
e79016aa KH |
615 | (interactive "P") |
616 | (cond ((not (and (boundp 'follow-mode) follow-mode)) | |
617 | (scroll-up arg)) | |
618 | (arg | |
619 | (save-excursion (scroll-down arg))) | |
620 | (t | |
621 | (let* ((windows (follow-all-followers)) | |
622 | (win (car (reverse windows))) | |
623 | (start (window-start (car windows)))) | |
624 | (if (eq start (point-min)) | |
625 | (signal 'beginning-of-buffer nil) | |
626 | (select-window win) | |
627 | (goto-char start) | |
cb02be17 | 628 | (vertical-motion (- (- (window-height win) |
99dfcc0d | 629 | (if header-line-format 2 1) |
e79016aa KH |
630 | next-screen-context-lines))) |
631 | (set-window-start win (point)) | |
632 | (goto-char start) | |
633 | (vertical-motion (- next-screen-context-lines 1)) | |
634 | (setq follow-internal-force-redisplay t)))))) | |
635 | ||
636 | ;;}}} | |
637 | ;;{{{ Buffer | |
638 | ||
639 | ;;;###autoload | |
640 | (defun follow-delete-other-windows-and-split (&optional arg) | |
17f6d002 | 641 | "Create two side by side windows and enter Follow mode. |
e79016aa KH |
642 | |
643 | Execute this command to display as much as possible of the text | |
cb02be17 | 644 | in the selected window. All other windows, in the current |
e79016aa | 645 | frame, are deleted and the selected window is split in two |
17f6d002 | 646 | side-by-side windows. Follow mode is activated, hence the |
e79016aa KH |
647 | two windows always will display two successive pages. |
648 | \(If one window is moved, the other one will follow.) | |
649 | ||
8cd3480b | 650 | If ARG is positive, the leftmost window is selected. If negative, |
e79016aa KH |
651 | the rightmost is selected. If ARG is nil, the leftmost window is |
652 | selected if the original window is the first one in the frame. | |
653 | ||
654 | To bind this command to a hotkey, place the following line | |
655 | in your `~/.emacs' file, replacing [f7] by your favourite key: | |
656 | (global-set-key [f7] 'follow-delete-other-windows-and-split)" | |
657 | (interactive "P") | |
cb02be17 | 658 | (let ((other (or (and (null arg) |
e79016aa KH |
659 | (not (eq (selected-window) |
660 | (frame-first-window (selected-frame))))) | |
661 | (and arg | |
662 | (< (prefix-numeric-value arg) 0)))) | |
663 | (start (window-start))) | |
664 | (delete-other-windows) | |
2d197ffb | 665 | (split-window-right) |
cb02be17 | 666 | (if other |
e79016aa KH |
667 | (progn |
668 | (other-window 1) | |
669 | (set-window-start (selected-window) start) | |
670 | (setq follow-internal-force-redisplay t))) | |
671 | (follow-mode 1))) | |
672 | ||
673 | (defun follow-switch-to-buffer (buffer) | |
17f6d002 | 674 | "Show BUFFER in all windows in the current Follow mode window chain." |
e79016aa KH |
675 | (interactive "BSwitch to Buffer: ") |
676 | (let ((orig-window (selected-window)) | |
677 | (windows (follow-all-followers))) | |
678 | (while windows | |
679 | (select-window (car windows)) | |
680 | (switch-to-buffer buffer) | |
681 | (setq windows (cdr windows))) | |
682 | (select-window orig-window))) | |
683 | ||
684 | ||
685 | (defun follow-switch-to-buffer-all (&optional buffer) | |
686 | "Show BUFFER in all windows on this frame. | |
687 | Defaults to current buffer." | |
cb02be17 | 688 | (interactive (list (read-buffer "Switch to Buffer: " |
e79016aa KH |
689 | (current-buffer)))) |
690 | (or buffer (setq buffer (current-buffer))) | |
691 | (let ((orig-window (selected-window))) | |
cb02be17 KH |
692 | (walk-windows |
693 | (function | |
e79016aa KH |
694 | (lambda (win) |
695 | (select-window win) | |
696 | (switch-to-buffer buffer)))) | |
697 | (select-window orig-window) | |
698 | (follow-redisplay))) | |
699 | ||
700 | ||
701 | (defun follow-switch-to-current-buffer-all () | |
17f6d002 | 702 | "Show current buffer in all windows on this frame, and enter Follow mode. |
e79016aa | 703 | |
cb02be17 | 704 | To bind this command to a hotkey place the following line |
e79016aa KH |
705 | in your `~/.emacs' file: |
706 | (global-set-key [f7] 'follow-switch-to-current-buffer-all)" | |
707 | (interactive) | |
708 | (or (and (boundp 'follow-mode) follow-mode) | |
709 | (follow-mode 1)) | |
710 | (follow-switch-to-buffer-all)) | |
711 | ||
712 | ;;}}} | |
713 | ;;{{{ Movement | |
714 | ||
fc779383 | 715 | ;; Note, these functions are not very useful, at least not unless you |
e79016aa KH |
716 | ;; rebind the rather cumbersome key sequence `C-c . p'. |
717 | ||
718 | (defun follow-next-window () | |
719 | "Select the next window showing the same buffer." | |
720 | (interactive) | |
721 | (let ((succ (cdr (follow-split-followers (follow-all-followers))))) | |
722 | (if succ | |
723 | (select-window (car succ)) | |
724 | (error "%s" "No more windows")))) | |
725 | ||
726 | ||
727 | (defun follow-previous-window () | |
728 | "Select the previous window showing the same buffer." | |
729 | (interactive) | |
730 | (let ((pred (car (follow-split-followers (follow-all-followers))))) | |
731 | (if pred | |
732 | (select-window (car pred)) | |
733 | (error "%s" "No more windows")))) | |
734 | ||
735 | ||
736 | (defun follow-first-window () | |
737 | "Select the first window in the frame showing the same buffer." | |
738 | (interactive) | |
739 | (select-window (car (follow-all-followers)))) | |
740 | ||
741 | ||
742 | (defun follow-last-window () | |
743 | "Select the last window in the frame showing the same buffer." | |
744 | (interactive) | |
745 | (select-window (car (reverse (follow-all-followers))))) | |
746 | ||
747 | ;;}}} | |
748 | ;;{{{ Redraw | |
749 | ||
750 | (defun follow-recenter (&optional arg) | |
cb02be17 KH |
751 | "Recenter the middle window around point. |
752 | Rearrange all other windows around the middle window. | |
e79016aa KH |
753 | |
754 | With a positive argument, place the current line ARG lines | |
8cd3480b JB |
755 | from the top. With a negative argument, place it -ARG lines |
756 | from the bottom." | |
e79016aa KH |
757 | (interactive "P") |
758 | (if arg | |
759 | (let ((p (point)) | |
760 | (arg (prefix-numeric-value arg))) | |
761 | (if (>= arg 0) | |
762 | ;; Recenter relative to the top. | |
763 | (progn | |
764 | (follow-first-window) | |
765 | (goto-char p) | |
766 | (recenter arg)) | |
767 | ;; Recenter relative to the bottom. | |
768 | (follow-last-window) | |
769 | (goto-char p) | |
770 | (recenter arg) | |
771 | ;; Otherwise, our post-command-hook will move the window | |
772 | ;; right back. | |
773 | (setq follow-internal-force-redisplay t))) | |
774 | ;; Recenter in the middle. | |
775 | (let* ((dest (point)) | |
776 | (windows (follow-all-followers)) | |
777 | (win (nth (/ (- (length windows) 1) 2) windows))) | |
778 | (select-window win) | |
779 | (goto-char dest) | |
780 | (recenter) | |
781 | ;;(setq follow-internal-force-redisplay t) | |
782 | ))) | |
783 | ||
784 | ||
785 | (defun follow-redraw () | |
786 | "Arrange windows displaying the same buffer in successor order. | |
787 | This function can be called even if the buffer is not in Follow mode. | |
788 | ||
789 | Hopefully, there should be no reason to call this function when in | |
790 | Follow mode since the windows should always be aligned." | |
791 | (interactive) | |
792 | (sit-for 0) | |
793 | (follow-redisplay)) | |
794 | ||
795 | ;;}}} | |
796 | ;;{{{ End of buffer | |
797 | ||
798 | (defun follow-end-of-buffer (&optional arg) | |
17f6d002 | 799 | "Move point to the end of the buffer, Follow mode style. |
e79016aa KH |
800 | |
801 | If the end is not visible, it will be displayed in the last possible | |
17f6d002 | 802 | window in the Follow mode window chain. |
e79016aa | 803 | |
cb02be17 | 804 | The mark is left at the previous position. With arg N, put point N/10 |
e79016aa KH |
805 | of the way from the true end." |
806 | (interactive "P") | |
807 | (let ((followers (follow-all-followers)) | |
808 | (pos (point))) | |
809 | (cond (arg | |
810 | (select-window (car (reverse followers)))) | |
cb02be17 | 811 | ((follow-select-if-end-visible |
e79016aa KH |
812 | (follow-windows-start-end followers))) |
813 | (t | |
814 | (select-window (car (reverse followers))))) | |
815 | (goto-char pos) | |
2a4b9211 RS |
816 | (with-no-warnings |
817 | (end-of-buffer arg)))) | |
e79016aa KH |
818 | |
819 | ;;}}} | |
820 | ||
821 | ;;}}} | |
822 | ||
823 | ;;{{{ Display | |
824 | ||
825 | ;;;; The display routines | |
826 | ||
827 | ;;{{{ Information gathering functions | |
828 | ||
829 | (defun follow-all-followers (&optional testwin) | |
830 | "Return all windows displaying the same buffer as the TESTWIN. | |
831 | The list contains only windows displayed in the same frame as TESTWIN. | |
832 | If TESTWIN is nil the selected window is used." | |
09c013ef | 833 | (or (window-live-p testwin) |
e79016aa KH |
834 | (setq testwin (selected-window))) |
835 | (let* ((top (frame-first-window (window-frame testwin))) | |
836 | (win top) | |
837 | (done nil) | |
838 | (windows '()) | |
839 | (buffer (window-buffer testwin))) | |
840 | (while (and (not done) win) | |
841 | (if (eq (window-buffer win) buffer) | |
842 | (setq windows (cons win windows))) | |
843 | (setq win (next-window win 'not)) | |
844 | (if (eq win top) | |
845 | (setq done t))) | |
846 | (nreverse windows))) | |
847 | ||
848 | ||
849 | (defun follow-split-followers (windows &optional win) | |
850 | "Split the WINDOWS into the sets: predecessors and successors. | |
cb02be17 | 851 | Return `(PRED . SUCC)' where `PRED' and `SUCC' are ordered starting |
e79016aa | 852 | from the selected window." |
cb02be17 | 853 | (or win |
e79016aa KH |
854 | (setq win (selected-window))) |
855 | (let ((pred '())) | |
856 | (while (not (eq (car windows) win)) | |
857 | (setq pred (cons (car windows) pred)) | |
858 | (setq windows (cdr windows))) | |
859 | (cons pred (cdr windows)))) | |
860 | ||
861 | ||
ff753438 | 862 | ;; This function is optimized function for speed! |
e79016aa KH |
863 | |
864 | (defun follow-calc-win-end (&optional win) | |
865 | "Calculate the presumed window end for WIN. | |
866 | ||
867 | Actually, the position returned is the start of the next | |
868 | window, normally is the end plus one. | |
869 | ||
870 | If WIN is nil, the selected window is used. | |
871 | ||
872 | Returns (end-pos end-of-buffer-p)" | |
08a1dbe6 | 873 | (if (featurep 'xemacs) |
e79016aa KH |
874 | ;; XEmacs can calculate the end of the window by using |
875 | ;; the 'guarantee options. GOOD! | |
876 | (let ((end (window-end win t))) | |
a9de04fa | 877 | (if (= end (point-max (window-buffer win))) |
e79016aa | 878 | (list end t) |
cb02be17 | 879 | (list (+ end 1) nil))) |
ff753438 RS |
880 | ;; Emacs: We have to calculate the end by ourselves. |
881 | ;; This code works on both XEmacs and Emacs, but now | |
e79016aa | 882 | ;; that XEmacs has got custom-written code, this could |
ff753438 | 883 | ;; be optimized for Emacs. |
30bf4750 CY |
884 | (let (height buffer-end-p) |
885 | (with-selected-window (or win (selected-window)) | |
886 | (save-excursion | |
887 | (goto-char (window-start)) | |
888 | (setq height | |
889 | (- (window-height) | |
890 | (if header-line-format 2 1))) | |
891 | (setq buffer-end-p | |
892 | (if (bolp) | |
893 | (not (= height (vertical-motion height))) | |
894 | (save-restriction | |
895 | ;; Fix a mis-feature in `vertical-motion': | |
896 | ;; The start of the window is assumed to | |
897 | ;; coinside with the start of a line. | |
898 | (narrow-to-region (point) (point-max)) | |
899 | (not (= height (vertical-motion height)))))) | |
900 | (list (point) buffer-end-p)))))) | |
e79016aa KH |
901 | |
902 | ||
903 | ;; Can't use `save-window-excursion' since it triggers a redraw. | |
904 | (defun follow-calc-win-start (windows pos win) | |
905 | "Calculate where WIN will start if the first in WINDOWS start at POS. | |
906 | ||
907 | If WIN is nil the point below all windows is returned." | |
908 | (let (start) | |
909 | (while (and windows (not (eq (car windows) win))) | |
910 | (setq start (window-start (car windows))) | |
911 | (set-window-start (car windows) pos 'noforce) | |
4484b97d | 912 | (setq pos (car (follow-calc-win-end (car windows)))) |
e79016aa KH |
913 | (set-window-start (car windows) start 'noforce) |
914 | (setq windows (cdr windows))) | |
915 | pos)) | |
916 | ||
917 | ||
f3bfa025 KH |
918 | ;; The result from `follow-windows-start-end' is cached when using |
919 | ;; a handful simple commands, like cursor movement commands. | |
920 | ||
921 | (defsubst follow-cache-valid-p (windows) | |
922 | "Test if the cached value of `follow-windows-start-end' can be used. | |
923 | Note that this handles the case when the cache has been set to nil." | |
924 | (let ((res t) | |
925 | (cache follow-windows-start-end-cache)) | |
926 | (while (and res windows cache) | |
927 | (setq res (and (eq (car windows) | |
928 | (car (car cache))) | |
929 | (eq (window-start (car windows)) | |
930 | (car (cdr (car cache)))))) | |
931 | (setq windows (cdr windows)) | |
932 | (setq cache (cdr cache))) | |
933 | (and res (null windows) (null cache)))) | |
934 | ||
935 | ||
936 | (defsubst follow-invalidate-cache () | |
937 | "Force `follow-windows-start-end' to recalculate the end of the window." | |
938 | (setq follow-windows-start-end-cache nil)) | |
939 | ||
940 | ||
e79016aa KH |
941 | ;; Build a list of windows and their start and end positions. |
942 | ;; Useful to avoid calculating start/end position whenever they are needed. | |
943 | ;; The list has the format: | |
944 | ;; ((Win Start End End-of-buffer-visible-p) ...) | |
945 | ||
946 | ;; Used to have a `save-window-excursion', but it obviously triggered | |
947 | ;; redraws of the display. Check if I used it for anything. | |
948 | ||
949 | ||
950 | (defun follow-windows-start-end (windows) | |
951 | "Builds a list of (WIN START END BUFFER-END-P) for every window in WINDOWS." | |
f3bfa025 KH |
952 | (if (follow-cache-valid-p windows) |
953 | follow-windows-start-end-cache | |
30bf4750 CY |
954 | (let ((orig-win (selected-window)) |
955 | win-start-end) | |
956 | (dolist (w windows) | |
957 | (select-window w) | |
958 | (push (cons w (cons (window-start) (follow-calc-win-end))) | |
959 | win-start-end)) | |
f3bfa025 | 960 | (select-window orig-win) |
30bf4750 | 961 | (setq follow-windows-start-end-cache (nreverse win-start-end))))) |
f3bfa025 KH |
962 | |
963 | ||
964 | (defsubst follow-pos-visible (pos win win-start-end) | |
e79016aa KH |
965 | "Non-nil when POS is visible in WIN." |
966 | (let ((wstart-wend-bend (cdr (assq win win-start-end)))) | |
967 | (and (>= pos (car wstart-wend-bend)) | |
30bf4750 | 968 | (or (< pos (cadr wstart-wend-bend)) |
e79016aa KH |
969 | (nth 2 wstart-wend-bend))))) |
970 | ||
971 | ||
30bf4750 | 972 | ;; By `aligned' we mean that for all adjacent windows, the end of the |
e79016aa KH |
973 | ;; first is equal with the start of the successor. The first window |
974 | ;; should start at a full screen line. | |
975 | ||
f3bfa025 | 976 | (defsubst follow-windows-aligned-p (win-start-end) |
8cd3480b | 977 | "Non-nil if the follower windows are aligned." |
cb02be17 | 978 | (let ((res t)) |
e79016aa | 979 | (save-excursion |
30bf4750 CY |
980 | (goto-char (window-start (caar win-start-end))) |
981 | (unless (bolp) | |
982 | (vertical-motion 0 (caar win-start-end)) | |
983 | (setq res (eq (point) (window-start (caar win-start-end)))))) | |
e79016aa KH |
984 | (while (and res (cdr win-start-end)) |
985 | ;; At least two followers left | |
f3bfa025 KH |
986 | (setq res (eq (car (cdr (cdr (car win-start-end)))) |
987 | (car (cdr (car (cdr win-start-end)))))) | |
e79016aa KH |
988 | (setq win-start-end (cdr win-start-end))) |
989 | res)) | |
990 | ||
991 | ||
992 | ;; Check if the point is visible in all windows. (So that | |
993 | ;; no one will be recentered.) | |
994 | ||
995 | (defun follow-point-visible-all-windows-p (win-start-end) | |
8cd3480b | 996 | "Non-nil when the `window-point' is visible in all windows." |
e79016aa KH |
997 | (let ((res t)) |
998 | (while (and res win-start-end) | |
f3bfa025 KH |
999 | (setq res (follow-pos-visible (window-point (car (car win-start-end))) |
1000 | (car (car win-start-end)) | |
1001 | win-start-end)) | |
e79016aa KH |
1002 | (setq win-start-end (cdr win-start-end))) |
1003 | res)) | |
1004 | ||
1005 | ||
5eba16a3 | 1006 | ;; Make sure WIN always starts at the beginning of a whole screen |
e79016aa KH |
1007 | ;; line. If WIN is not aligned the start is updated which probably |
1008 | ;; will lead to a redisplay of the screen later on. | |
1009 | ;; | |
1010 | ;; This is used with the first window in a follow chain. The reason | |
1011 | ;; is that we want to detect that the point is outside the window. | |
1012 | ;; (Without the update, the start of the window will move as the | |
1013 | ;; user presses BackSpace, and the other window redisplay routines | |
1014 | ;; will move the start of the window in the wrong direction.) | |
1015 | ||
1016 | (defun follow-update-window-start (win) | |
1017 | "Make sure that the start of WIN starts at a full screen line." | |
1018 | (save-excursion | |
1019 | (goto-char (window-start win)) | |
4484b97d | 1020 | (unless (bolp) |
e79016aa | 1021 | (vertical-motion 0 win) |
4484b97d | 1022 | (unless (eq (point) (window-start win)) |
e79016aa KH |
1023 | (vertical-motion 1 win) |
1024 | (set-window-start win (point) 'noforce))))) | |
1025 | ||
1026 | ;;}}} | |
1027 | ;;{{{ Selection functions | |
1028 | ||
1029 | ;; Make a window in WINDOWS selected if it currently | |
1030 | ;; is displaying the position DEST. | |
1031 | ;; | |
1032 | ;; We don't select a window if it just has been moved. | |
1033 | ||
1034 | (defun follow-select-if-visible (dest win-start-end) | |
1035 | "Select and return a window, if DEST is visible in it. | |
1036 | Return the selected window." | |
747ae2f2 | 1037 | (let (win win-end) |
e79016aa | 1038 | (while (and (not win) win-start-end) |
ff753438 | 1039 | ;; Don't select a window that was just moved. This makes it |
e79016aa KH |
1040 | ;; possible to later select the last window after a `end-of-buffer' |
1041 | ;; command. | |
4484b97d | 1042 | (when (follow-pos-visible dest (caar win-start-end) win-start-end) |
747ae2f2 CY |
1043 | (setq win (caar win-start-end) |
1044 | win-end (car (cddr (car win-start-end)))) | |
4484b97d | 1045 | (select-window win)) |
e79016aa | 1046 | (setq win-start-end (cdr win-start-end))) |
30bf4750 CY |
1047 | ;; The last line of the window may be partially visible; if so, |
1048 | ;; and if point is visible in the next window, select the next | |
1049 | ;; window instead. | |
747ae2f2 CY |
1050 | (and win |
1051 | (/= dest (point-max)) | |
30bf4750 CY |
1052 | win-start-end |
1053 | (follow-pos-visible dest (caar win-start-end) win-start-end) | |
747ae2f2 CY |
1054 | (save-excursion |
1055 | (goto-char dest) | |
1056 | (vertical-motion 1 win) | |
1057 | (>= (point) win-end)) | |
30bf4750 CY |
1058 | (setq win (caar win-start-end)) |
1059 | (select-window win)) | |
e79016aa KH |
1060 | win)) |
1061 | ||
1062 | ||
5eba16a3 JB |
1063 | ;; Lets select a window showing the end. Make sure we only select it if |
1064 | ;; it wasn't just moved here. (I.e. M-> shall not unconditionally place | |
e79016aa KH |
1065 | ;; the point in the selected window.) |
1066 | ;; | |
135377f1 | 1067 | ;; (Compatibility cludge: in Emacs `window-end' is equal to `point-max'; |
e79016aa | 1068 | ;; in XEmacs, it is equal to `point-max + 1'. Should I really bother |
cb02be17 | 1069 | ;; checking `window-end' now when I check `end-of-buffer' explicitly?) |
e79016aa KH |
1070 | |
1071 | (defun follow-select-if-end-visible (win-start-end) | |
1072 | "Select and return a window, if end is visible in it." | |
1073 | (let ((win nil)) | |
1074 | (while (and (not win) win-start-end) | |
ff753438 | 1075 | ;; Don't select a window that was just moved. This makes it |
e79016aa KH |
1076 | ;; possible to later select the last window after a `end-of-buffer' |
1077 | ;; command. | |
1078 | (if (and (eq (point-max) (nth 2 (car win-start-end))) | |
1079 | (nth 3 (car win-start-end)) | |
ff753438 RS |
1080 | ;; `window-end' might return nil. |
1081 | (let ((end (window-end (car (car win-start-end))))) | |
1082 | (and end | |
1083 | (eq (point-max) (min (point-max) end))))) | |
e79016aa KH |
1084 | (progn |
1085 | (setq win (car (car win-start-end))) | |
1086 | (select-window win))) | |
1087 | (setq win-start-end (cdr win-start-end))) | |
1088 | win)) | |
1089 | ||
1090 | ||
ff753438 | 1091 | ;; Select a window that will display the point if the windows would |
e79016aa KH |
1092 | ;; be redisplayed with the first window fixed. This is useful for |
1093 | ;; example when the user has pressed return at the bottom of a window | |
1094 | ;; as the point is not visible in any window. | |
1095 | ||
1096 | (defun follow-select-if-visible-from-first (dest windows) | |
30bf4750 CY |
1097 | "Try to select one of WINDOWS without repositioning the topmost window. |
1098 | If one of the windows in WINDOWS contains DEST, select it, call | |
1099 | `follow-redisplay', move point to DEST, and return that window. | |
1100 | Otherwise, return nil." | |
1101 | (let (win end-pos-end-p) | |
e79016aa KH |
1102 | (save-excursion |
1103 | (goto-char (window-start (car windows))) | |
1104 | ;; Make sure the line start in the beginning of a real screen | |
1105 | ;; line. | |
cb02be17 | 1106 | (vertical-motion 0 (car windows)) |
30bf4750 | 1107 | (when (>= dest (point)) |
e79016aa KH |
1108 | ;; At or below the start. Check the windows. |
1109 | (save-window-excursion | |
30bf4750 CY |
1110 | (let ((windows windows)) |
1111 | (while (and (not win) windows) | |
1112 | (set-window-start (car windows) (point) 'noforce) | |
1113 | (setq end-pos-end-p (follow-calc-win-end (car windows))) | |
1114 | (goto-char (car end-pos-end-p)) | |
1115 | ;; Visible, if dest above end, or if eob is visible inside | |
1116 | ;; the window. | |
1117 | (if (or (car (cdr end-pos-end-p)) | |
1118 | (< dest (point))) | |
e79016aa | 1119 | (setq win (car windows)) |
30bf4750 CY |
1120 | (setq windows (cdr windows)))))))) |
1121 | (when win | |
1122 | (select-window win) | |
1123 | (follow-redisplay windows (car windows)) | |
1124 | (goto-char dest)) | |
e79016aa KH |
1125 | win)) |
1126 | ||
1127 | ||
1128 | ;;}}} | |
cb02be17 | 1129 | ;;{{{ Redisplay |
e79016aa KH |
1130 | |
1131 | ;; Redraw all the windows on the screen, starting with the top window. | |
1132 | ;; The window used as as marker is WIN, or the selcted window if WIN | |
4484b97d CY |
1133 | ;; is nil. Start every window directly after the end of the previous |
1134 | ;; window, to make sure long lines are displayed correctly. | |
e79016aa | 1135 | |
30bf4750 | 1136 | (defun follow-redisplay (&optional windows win preserve-win) |
e79016aa KH |
1137 | "Reposition the WINDOWS around WIN. |
1138 | Should the point be too close to the roof we redisplay everything | |
8cd3480b | 1139 | from the top. WINDOWS should contain a list of windows to |
5eba16a3 | 1140 | redisplay; it is assumed that WIN is a member of the list. |
e79016aa KH |
1141 | Should WINDOWS be nil, the windows displaying the |
1142 | same buffer as WIN, in the current frame, are used. | |
30bf4750 CY |
1143 | Should WIN be nil, the selected window is used. |
1144 | If PRESERVE-WIN is non-nil, keep WIN itself unchanged while | |
1145 | repositioning the other windows." | |
4484b97d CY |
1146 | (or win (setq win (selected-window))) |
1147 | (or windows (setq windows (follow-all-followers win))) | |
1148 | ;; Calculate the start of the first window. | |
1149 | (let* ((old-win-start (window-start win)) | |
1150 | (try-first-start (follow-estimate-first-window-start | |
1151 | windows win old-win-start)) | |
1152 | (try-win-start (follow-calc-win-start | |
1153 | windows try-first-start win)) | |
1154 | (start (cond ((= try-win-start old-win-start) | |
1155 | (follow-debug-message "exact") | |
1156 | try-first-start) | |
1157 | ((< try-win-start old-win-start) | |
1158 | (follow-debug-message "above") | |
1159 | (follow-calculate-first-window-start-from-above | |
1160 | windows try-first-start win old-win-start)) | |
1161 | (t | |
1162 | (follow-debug-message "below") | |
1163 | (follow-calculate-first-window-start-from-below | |
1164 | windows try-first-start win old-win-start))))) | |
1165 | (dolist (w windows) | |
30bf4750 CY |
1166 | (unless (and preserve-win (eq w win)) |
1167 | (set-window-start w start)) | |
4484b97d | 1168 | (setq start (car (follow-calc-win-end w)))))) |
e79016aa KH |
1169 | |
1170 | ||
e79016aa KH |
1171 | (defun follow-estimate-first-window-start (windows win start) |
1172 | "Estimate the position of the first window. | |
4484b97d CY |
1173 | The estimate is computed by assuming that the window WIN, which |
1174 | should be a member of WINDOWS, starts at position START." | |
1175 | (let ((windows-before (car (follow-split-followers windows win)))) | |
e79016aa KH |
1176 | (save-excursion |
1177 | (goto-char start) | |
e79016aa | 1178 | (vertical-motion 0 win) |
4484b97d CY |
1179 | (dolist (w windows-before) |
1180 | (vertical-motion (- 1 (window-text-height w)) w)) | |
1181 | (point)))) | |
e79016aa KH |
1182 | |
1183 | ||
1184 | ;; Find the starting point, start at GUESS and search downward. | |
1185 | ;; The returned point is always a point below GUESS. | |
1186 | ||
cb02be17 | 1187 | (defun follow-calculate-first-window-start-from-above |
e79016aa KH |
1188 | (windows guess win start) |
1189 | (save-excursion | |
1190 | (let ((done nil) | |
1191 | win-start | |
1192 | res) | |
1193 | (goto-char guess) | |
1194 | (while (not done) | |
1195 | (if (not (= (vertical-motion 1 (car windows)) 1)) | |
1196 | ;; Hit bottom! (Can we really do this?) | |
1197 | ;; We'll keep it, since it ensures termination. | |
1198 | (progn | |
1199 | (setq done t) | |
1200 | (setq res (point-max))) | |
1201 | (setq win-start (follow-calc-win-start windows (point) win)) | |
1202 | (if (>= win-start start) | |
4484b97d | 1203 | (setq done t res (point))))) |
e79016aa KH |
1204 | res))) |
1205 | ||
1206 | ||
1207 | ;; Find the starting point, start at GUESS and search upward. Return | |
1208 | ;; a point on the same line as GUESS, or above. | |
1209 | ;; | |
1210 | ;; (Is this ever used? I must make sure it works just in case it is | |
1211 | ;; ever called.) | |
1212 | ||
1213 | (defun follow-calculate-first-window-start-from-below | |
1214 | (windows guess &optional win start) | |
1215 | (setq win (or win (selected-window))) | |
1216 | (setq start (or start (window-start win))) | |
1217 | (save-excursion | |
13d5c73f | 1218 | (let (done win-start res opoint) |
fc779383 | 1219 | ;; Always calculate what happens when no line is displayed in the first |
e79016aa KH |
1220 | ;; window. (The `previous' res is needed below!) |
1221 | (goto-char guess) | |
1222 | (vertical-motion 0 (car windows)) | |
1223 | (setq res (point)) | |
1224 | (while (not done) | |
13d5c73f | 1225 | (setq opoint (point)) |
e79016aa KH |
1226 | (if (not (= (vertical-motion -1 (car windows)) -1)) |
1227 | ;; Hit roof! | |
4484b97d | 1228 | (setq done t res (point-min)) |
e79016aa | 1229 | (setq win-start (follow-calc-win-start windows (point) win)) |
13d5c73f CY |
1230 | (cond ((>= (point) opoint) |
1231 | ;; In some pathological cases, vertical-motion may | |
1232 | ;; return -1 even though point has not decreased. In | |
1233 | ;; that case, avoid looping forever. | |
1234 | (setq done t res (point))) | |
1235 | ((= win-start start) ; Perfect match, use this value | |
1236 | (setq done t res (point))) | |
5eba16a3 | 1237 | ((< win-start start) ; Walked to far, use previous result |
e79016aa KH |
1238 | (setq done t)) |
1239 | (t ; Store result for next iteration | |
1240 | (setq res (point)))))) | |
1241 | res))) | |
1242 | ||
1243 | ;;}}} | |
1244 | ;;{{{ Avoid tail recenter | |
1245 | ||
1246 | ;; This sets the window internal flag `force_start'. The effect is that | |
5eba16a3 | 1247 | ;; windows only displaying the tail aren't recentered. |
e79016aa KH |
1248 | ;; Has to be called before every redisplay... (Great isn't it?) |
1249 | ;; | |
1250 | ;; XEmacs doesn't recenter the tail, GOOD! | |
1251 | ;; | |
5eba16a3 | 1252 | ;; A window displaying only the tail, is a window whose |
e79016aa KH |
1253 | ;; window-start position is equal to (point-max) of the buffer it |
1254 | ;; displays. | |
1255 | ;; | |
1256 | ;; This function is also added to `post-command-idle-hook', introduced | |
1257 | ;; in Emacs 19.30. This is needed since the vaccine injected by the | |
1258 | ;; call from `post-command-hook' only works until the next redisplay. | |
1259 | ;; It is possible that the functions in the `post-command-idle-hook' | |
1260 | ;; can cause a redisplay, and hence a new vaccine is needed. | |
1261 | ;; | |
1262 | ;; Sometimes, calling this function could actually cause a redisplay, | |
1263 | ;; especially if it is placed in the debug filter section. I must | |
1264 | ;; investigate this further... | |
1265 | ||
06b60517 | 1266 | (defun follow-avoid-tail-recenter (&rest _rest) |
e79016aa KH |
1267 | "Make sure windows displaying the end of a buffer aren't recentered. |
1268 | ||
7194219d | 1269 | This is done by reading and rewriting the start position of |
17f6d002 | 1270 | non-first windows in Follow mode." |
e79016aa KH |
1271 | (if follow-avoid-tail-recenter-p |
1272 | (let* ((orig-buffer (current-buffer)) | |
f3bfa025 KH |
1273 | (top (frame-first-window (selected-frame))) |
1274 | (win top) | |
1275 | (who '()) ; list of (buffer . frame) | |
1276 | start | |
1277 | pair) ; (buffer . frame) | |
1278 | ;; If the only window in the frame is a minibuffer | |
1279 | ;; window, `next-window' will never find it again... | |
1280 | (if (window-minibuffer-p top) | |
1281 | nil | |
1282 | (while ;; look, no body! | |
1283 | (progn | |
1284 | (setq start (window-start win)) | |
1285 | (set-buffer (window-buffer win)) | |
1286 | (setq pair (cons (window-buffer win) (window-frame win))) | |
1287 | (if (member pair who) | |
cb02be17 | 1288 | (if (and (boundp 'follow-mode) follow-mode |
f3bfa025 KH |
1289 | (eq (point-max) start)) |
1290 | ;; Write the same window start back, but don't | |
1291 | ;; set the NOFORCE flag. | |
1292 | (set-window-start win start)) | |
1293 | (setq who (cons pair who))) | |
1294 | (setq win (next-window win 'not t)) | |
1295 | (not (eq win top)))) ;; Loop while this is true. | |
1296 | (set-buffer orig-buffer))))) | |
e79016aa KH |
1297 | |
1298 | ;;}}} | |
1299 | ||
1300 | ;;}}} | |
1301 | ;;{{{ Post Command Hook | |
1302 | ||
08a1dbe6 | 1303 | ;; The magic little box. This function is called after every command. |
e79016aa KH |
1304 | |
1305 | ;; This is not as complicated as it seems. It is simply a list of common | |
1306 | ;; display situations and the actions to take, plus commands for redrawing | |
1307 | ;; the screen if it should be unaligned. | |
1308 | ;; | |
1309 | ;; We divide the check into two parts; whether we are at the end or not. | |
99dfcc0d | 1310 | ;; This is due to the fact that the end can actually be visible |
e79016aa KH |
1311 | ;; in several window even though they are aligned. |
1312 | ||
1313 | (defun follow-post-command-hook () | |
cb02be17 | 1314 | "Ensure that the windows in Follow mode are adjacent after each command." |
4484b97d CY |
1315 | (unless (input-pending-p) |
1316 | (let ((follow-inside-post-command-hook t) | |
1317 | (win (selected-window))) | |
e79016aa | 1318 | ;; Work in the selected window, not in the current buffer. |
4484b97d CY |
1319 | (with-current-buffer (window-buffer win) |
1320 | (unless (and (symbolp this-command) | |
1321 | (get this-command 'follow-mode-use-cache)) | |
1322 | (follow-invalidate-cache)) | |
1323 | (when (and follow-mode | |
1324 | (not (window-minibuffer-p win))) | |
1325 | ;; The buffer shown in the selected window is in follow | |
30bf4750 | 1326 | ;; mode. Find the current state of the display. |
4484b97d CY |
1327 | (let* ((windows (follow-all-followers win)) |
1328 | (dest (point)) | |
1329 | (win-start-end (progn | |
1330 | (follow-update-window-start (car windows)) | |
1331 | (follow-windows-start-end windows))) | |
1332 | (aligned (follow-windows-aligned-p win-start-end)) | |
30bf4750 CY |
1333 | (visible (follow-pos-visible dest win win-start-end)) |
1334 | selected-window-up-to-date) | |
4484b97d CY |
1335 | (unless (and aligned visible) |
1336 | (follow-invalidate-cache)) | |
1337 | (follow-avoid-tail-recenter) | |
1338 | ;; Select a window to display point. | |
1339 | (unless follow-internal-force-redisplay | |
1340 | (if (eq dest (point-max)) | |
1341 | ;; At point-max, we have to be careful since the | |
1342 | ;; display can be aligned while `dest' can be | |
1343 | ;; visible in several windows. | |
1344 | (cond | |
1345 | ;; Select the current window, but only when the | |
1346 | ;; display is correct. (When inserting characters | |
1347 | ;; in a tail window, the display is not correct, as | |
1348 | ;; they are shown twice.) | |
1349 | ;; | |
1350 | ;; Never stick to the current window after a | |
1351 | ;; deletion. The reason is cosmetic: when typing | |
1352 | ;; `DEL' in a window showing only the end of the | |
1353 | ;; file, a character would be removed from the | |
1354 | ;; window above, which is very unintuitive. | |
1355 | ((and visible | |
1356 | aligned | |
1357 | (not (memq this-command | |
1358 | '(backward-delete-char | |
1359 | delete-backward-char | |
1360 | backward-delete-char-untabify | |
1361 | kill-region)))) | |
1362 | (follow-debug-message "Max: same")) | |
1363 | ;; If the end is visible, and the window doesn't | |
1364 | ;; seems like it just has been moved, select it. | |
1365 | ((follow-select-if-end-visible win-start-end) | |
1366 | (follow-debug-message "Max: end visible") | |
1367 | (setq visible t aligned nil) | |
1368 | (goto-char dest)) | |
1369 | ;; Just show the end... | |
1370 | (t | |
1371 | (follow-debug-message "Max: default") | |
1372 | (select-window (car (reverse windows))) | |
1373 | (goto-char dest) | |
1374 | (setq visible nil aligned nil))) | |
1375 | ||
1376 | ;; We're not at the end, here life is much simpler. | |
1377 | (cond | |
1378 | ;; This is the normal case! | |
1379 | ;; It should be optimized for speed. | |
1380 | ((and visible aligned) | |
1381 | (follow-debug-message "same")) | |
1382 | ;; Pick a position in any window. If the display is | |
747ae2f2 | 1383 | ;; ok, this will pick the `correct' window. |
4484b97d CY |
1384 | ((follow-select-if-visible dest win-start-end) |
1385 | (follow-debug-message "visible") | |
747ae2f2 CY |
1386 | (goto-char dest) |
1387 | ;; We have to perform redisplay, since scrolling is | |
1388 | ;; needed in case the line is partially visible. | |
1389 | (setq visible nil)) | |
4484b97d CY |
1390 | ;; Not visible anywhere else, lets pick this one. |
1391 | ;; (Is this case used?) | |
1392 | (visible | |
1393 | (follow-debug-message "visible in selected.")) | |
1394 | ;; Far out! | |
1395 | ((eq dest (point-min)) | |
1396 | (follow-debug-message "min") | |
1397 | (select-window (car windows)) | |
1398 | (goto-char dest) | |
1399 | (set-window-start (selected-window) (point-min)) | |
1400 | (setq win-start-end (follow-windows-start-end windows)) | |
1401 | (follow-invalidate-cache) | |
1402 | (setq visible t aligned nil)) | |
1403 | ;; If we can position the cursor without moving the first | |
1404 | ;; window, do it. This is the case that catches `RET' | |
1405 | ;; at the bottom of a window. | |
1406 | ((follow-select-if-visible-from-first dest windows) | |
1407 | (follow-debug-message "Below first") | |
30bf4750 | 1408 | (setq visible t aligned t)) |
4484b97d CY |
1409 | ;; None of the above. For simplicity, we stick to the |
1410 | ;; selected window. | |
1411 | (t | |
1412 | (follow-debug-message "None") | |
1413 | (setq visible nil aligned nil)))) | |
1414 | ;; If a new window has been selected, make sure that the | |
1415 | ;; old is not scrolled when the point is outside the | |
1416 | ;; window. | |
30bf4750 CY |
1417 | (unless (eq win (selected-window)) |
1418 | (let ((p (window-point win))) | |
1419 | (set-window-start win (window-start win) nil) | |
1420 | (set-window-point win p)))) | |
747ae2f2 CY |
1421 | (unless visible |
1422 | ;; If point may not be visible in the selected window, | |
1423 | ;; perform a redisplay; this ensures scrolling. | |
1424 | (redisplay) | |
30bf4750 | 1425 | (setq selected-window-up-to-date t) |
4484b97d CY |
1426 | (follow-avoid-tail-recenter) |
1427 | (setq win-start-end (follow-windows-start-end windows)) | |
1428 | (follow-invalidate-cache) | |
1429 | (setq aligned nil)) | |
30bf4750 | 1430 | ;; Now redraw the windows around the selected window. |
4484b97d CY |
1431 | (unless (and (not follow-internal-force-redisplay) |
1432 | (or aligned | |
1433 | (follow-windows-aligned-p win-start-end)) | |
1434 | (follow-point-visible-all-windows-p | |
1435 | win-start-end)) | |
1436 | (setq follow-internal-force-redisplay nil) | |
30bf4750 CY |
1437 | (follow-redisplay windows (selected-window) |
1438 | selected-window-up-to-date) | |
4484b97d CY |
1439 | (setq win-start-end (follow-windows-start-end windows)) |
1440 | (follow-invalidate-cache) | |
1441 | ;; When the point ends up in another window. This | |
1442 | ;; happens when dest is in the beginning of the file and | |
1443 | ;; the selected window is not the first. It can also, | |
1444 | ;; in rare situations happen when long lines are used | |
1445 | ;; and there is a big difference between the width of | |
1446 | ;; the windows. (When scrolling one line in a wide | |
1447 | ;; window which will cause a move larger that an entire | |
1448 | ;; small window.) | |
1449 | (unless (follow-pos-visible dest win win-start-end) | |
1450 | (follow-select-if-visible dest win-start-end) | |
1451 | (goto-char dest))) | |
1452 | ||
1453 | ;; If the region is visible, make it look good when spanning | |
1454 | ;; multiple windows. | |
1455 | (when (region-active-p) | |
1456 | (follow-maximize-region | |
1457 | (selected-window) windows win-start-end)))) | |
1458 | ;; Whether or not the buffer was in follow mode, we must | |
1459 | ;; update the windows displaying the tail so that Emacs won't | |
1460 | ;; recenter them. | |
1461 | (follow-avoid-tail-recenter))))) | |
e79016aa KH |
1462 | |
1463 | ;;}}} | |
1464 | ;;{{{ The region | |
1465 | ||
1466 | ;; Tries to make the highlighted area representing the region look | |
cb02be17 | 1467 | ;; good when spanning several windows. |
e79016aa KH |
1468 | ;; |
1469 | ;; Not perfect, as the point can't be placed at window end, only at | |
cb02be17 | 1470 | ;; end-1. This will highlight a little bit in windows above |
e79016aa KH |
1471 | ;; the current. |
1472 | ||
1473 | (defun follow-maximize-region (win windows win-start-end) | |
cb02be17 | 1474 | "Make a highlighted region stretching multiple windows look good." |
e79016aa KH |
1475 | (let* ((all (follow-split-followers windows win)) |
1476 | (pred (car all)) | |
1477 | (succ (cdr all)) | |
1478 | data) | |
1479 | (while pred | |
1480 | (setq data (assq (car pred) win-start-end)) | |
1481 | (set-window-point (car pred) (max (nth 1 data) (- (nth 2 data) 1))) | |
1482 | (setq pred (cdr pred))) | |
1483 | (while succ | |
1484 | (set-window-point (car succ) (nth 1 (assq (car succ) win-start-end))) | |
1485 | (setq succ (cdr succ))))) | |
1486 | ||
1487 | ;;}}} | |
1488 | ;;{{{ Scroll bar | |
1489 | ||
1490 | ;;;; Scroll-bar support code. | |
1491 | ||
08a1dbe6 | 1492 | ;; Why is it needed? Well, if the selected window is in follow mode, |
5eba16a3 | 1493 | ;; all its followers stick to it blindly. If one of them is scrolled, |
08a1dbe6 SM |
1494 | ;; it immediately returns to the original position when the mouse is |
1495 | ;; released. If the selected window is not a follower of the dragged | |
1496 | ;; window the windows will be unaligned. | |
e79016aa | 1497 | |
5eba16a3 | 1498 | ;; The advices don't get compiled. Aesthetically, this might be a |
08a1dbe6 | 1499 | ;; problem but in practical life it isn't. |
e79016aa | 1500 | |
08a1dbe6 SM |
1501 | ;; Discussion: Now when the other windows in the chain follow the |
1502 | ;; dragged, should we really select it? | |
e79016aa KH |
1503 | |
1504 | (cond ((fboundp 'scroll-bar-drag) | |
1505 | ;;; | |
ff753438 | 1506 | ;;; Emacs style scrollbars. |
e79016aa KH |
1507 | ;;; |
1508 | ||
1509 | ;; Select the dragged window if it is a follower of the | |
1510 | ;; selected window. | |
1511 | ;; | |
1512 | ;; Generate advices of the form: | |
1513 | ;; (defadvice scroll-bar-drag (after follow-scroll-bar-drag activate) | |
1514 | ;; "Adviced by `follow-mode'." | |
1515 | ;; (follow-redraw-after-event (ad-get-arg 0))) | |
1516 | (let ((cmds '(scroll-bar-drag | |
1517 | scroll-bar-drag-1 ; Executed at every move. | |
1518 | scroll-bar-scroll-down | |
1519 | scroll-bar-scroll-up | |
1520 | scroll-bar-set-window-start))) | |
1521 | (while cmds | |
1522 | (eval | |
c86b6fd2 | 1523 | `(defadvice ,(intern (symbol-name (car cmds))) |
cb02be17 | 1524 | (after |
c86b6fd2 | 1525 | ,(intern (concat "follow-" (symbol-name (car cmds)))) |
e79016aa | 1526 | activate) |
17f6d002 | 1527 | "Adviced by Follow mode." |
c86b6fd2 | 1528 | (follow-redraw-after-event (ad-get-arg 0)))) |
e79016aa | 1529 | (setq cmds (cdr cmds)))) |
cb02be17 KH |
1530 | |
1531 | ||
e79016aa KH |
1532 | (defun follow-redraw-after-event (event) |
1533 | "Adviced by Follow mode." | |
1534 | (condition-case nil | |
1535 | (let* ((orig-win (selected-window)) | |
cb02be17 | 1536 | (win (nth 0 (funcall |
e79016aa | 1537 | (symbol-function 'event-start) event))) |
cb02be17 KH |
1538 | (fmode (assq 'follow-mode |
1539 | (buffer-local-variables | |
e79016aa KH |
1540 | (window-buffer win))))) |
1541 | (if (and fmode (cdr fmode)) | |
1542 | ;; The selected window is in follow-mode | |
1543 | (progn | |
1544 | ;; Recenter around the dragged window. | |
1545 | (select-window win) | |
1546 | (follow-redisplay) | |
1547 | (select-window orig-win)))) | |
1548 | (error nil)))) | |
1549 | ||
1550 | ||
1551 | ((fboundp 'scrollbar-vertical-drag) | |
1552 | ;;; | |
1553 | ;;; XEmacs style scrollbars. | |
1554 | ;;; | |
1555 | ||
1556 | ;; Advice all scrollbar functions on the form: | |
1557 | ;; | |
cb02be17 | 1558 | ;; (defadvice scrollbar-line-down |
e79016aa KH |
1559 | ;; (after follow-scrollbar-line-down activate) |
1560 | ;; (follow-xemacs-scrollbar-support (ad-get-arg 0))) | |
1561 | ||
1562 | (let ((cmds '(scrollbar-line-down ; Window | |
1563 | scrollbar-line-up | |
1564 | scrollbar-page-down ; Object | |
1565 | scrollbar-page-up | |
1566 | scrollbar-to-bottom ; Window | |
1567 | scrollbar-to-top | |
1568 | scrollbar-vertical-drag ; Object | |
1569 | ))) | |
cb02be17 | 1570 | |
e79016aa KH |
1571 | (while cmds |
1572 | (eval | |
c86b6fd2 | 1573 | `(defadvice ,(intern (symbol-name (car cmds))) |
cb02be17 | 1574 | (after |
c86b6fd2 | 1575 | ,(intern (concat "follow-" (symbol-name (car cmds)))) |
e79016aa KH |
1576 | activate) |
1577 | "Adviced by `follow-mode'." | |
c86b6fd2 | 1578 | (follow-xemacs-scrollbar-support (ad-get-arg 0)))) |
e79016aa KH |
1579 | (setq cmds (cdr cmds)))) |
1580 | ||
1581 | ||
1582 | (defun follow-xemacs-scrollbar-support (window) | |
1583 | "Redraw windows showing the same buffer as shown in WINDOW. | |
1584 | WINDOW is either the dragged window, or a cons containing the | |
8cd3480b | 1585 | window as its first element. This is called while the user drags |
e79016aa KH |
1586 | the scrollbar. |
1587 | ||
1588 | WINDOW can be an object or a window." | |
1589 | (condition-case nil | |
1590 | (progn | |
1591 | (if (consp window) | |
1592 | (setq window (car window))) | |
cb02be17 KH |
1593 | (let ((fmode (assq 'follow-mode |
1594 | (buffer-local-variables | |
e79016aa KH |
1595 | (window-buffer window)))) |
1596 | (orig-win (selected-window))) | |
1597 | (if (and fmode (cdr fmode)) | |
1598 | (progn | |
1599 | ;; Recenter around the dragged window. | |
1600 | (select-window window) | |
1601 | (follow-redisplay) | |
1602 | (select-window orig-win))))) | |
1603 | (error nil))))) | |
1604 | ||
1605 | ;;}}} | |
1606 | ;;{{{ Process output | |
1607 | ||
08a1dbe6 SM |
1608 | ;; The following sections installs a spy that listens to process |
1609 | ;; output and tries to reposition the windows whose buffers are in | |
1610 | ;; Follow mode. We play safe as much as possible... | |
1611 | ;; | |
1612 | ;; When follow-mode is activated all active processes are | |
1613 | ;; intercepted. All new processes that change their filter function | |
1614 | ;; using `set-process-filter' are also intercepted. The reason is | |
1615 | ;; that a process can cause a redisplay recentering "tail" windows. | |
1616 | ;; Note that it doesn't hurt to spy on more processes than needed. | |
1617 | ;; | |
1618 | ;; Technically, we set the process filter to `follow-generic-filter'. | |
1619 | ;; The original filter is stored in `follow-process-filter-alist'. | |
1620 | ;; Our generic filter calls the original filter, or inserts the | |
1621 | ;; output into the buffer, if the buffer originally didn't have an | |
1622 | ;; output filter. It also makes sure that the windows connected to | |
1623 | ;; the buffer are aligned. | |
1624 | ;; | |
1625 | ;; Discussion: How do we find processes that don't call | |
1626 | ;; `set-process-filter'? (How often are processes created in a | |
1627 | ;; buffer after Follow mode are activated?) | |
1628 | ;; | |
1629 | ;; Discussion: Should we also advice `process-filter' to make our | |
1630 | ;; filter invisible to others? | |
e79016aa KH |
1631 | |
1632 | ;;{{{ Advice for `set-process-filter' | |
1633 | ||
1634 | ;; Do not call this with 'follow-generic-filter as the name of the | |
1635 | ;; filter... | |
1636 | ||
1637 | (defadvice set-process-filter (before follow-set-process-filter activate) | |
17f6d002 | 1638 | "Ensure process output will be displayed correctly in Follow mode buffers. |
e79016aa | 1639 | |
17f6d002 | 1640 | Follow mode inserts its own process filter to do its |
e79016aa KH |
1641 | magic stuff before the real process filter is called." |
1642 | (if follow-intercept-processes | |
1643 | (progn | |
1644 | (setq follow-process-filter-alist | |
1645 | (delq (assq (ad-get-arg 0) follow-process-filter-alist) | |
1646 | follow-process-filter-alist)) | |
1647 | (follow-tidy-process-filter-alist) | |
1648 | (cond ((eq (ad-get-arg 1) t)) | |
1649 | ((eq (ad-get-arg 1) nil) | |
1650 | (ad-set-arg 1 'follow-generic-filter)) | |
1651 | (t | |
cb02be17 KH |
1652 | (setq follow-process-filter-alist |
1653 | (cons (cons (ad-get-arg 0) (ad-get-arg 1)) | |
e79016aa KH |
1654 | follow-process-filter-alist)) |
1655 | (ad-set-arg 1 'follow-generic-filter)))))) | |
1656 | ||
1657 | ||
1658 | (defun follow-call-set-process-filter (proc filter) | |
1659 | "Call original `set-process-filter' without the Follow mode advice." | |
1660 | (ad-disable-advice 'set-process-filter 'before | |
1661 | 'follow-set-process-filter) | |
1662 | (ad-activate 'set-process-filter) | |
cb02be17 | 1663 | (prog1 |
e79016aa KH |
1664 | (set-process-filter proc filter) |
1665 | (ad-enable-advice 'set-process-filter 'before | |
1666 | 'follow-set-process-filter) | |
1667 | (ad-activate 'set-process-filter))) | |
1668 | ||
1669 | ||
1670 | (defadvice process-filter (after follow-process-filter activate) | |
cb02be17 | 1671 | "Return the original process filter, not `follow-generic-filter'." |
e79016aa KH |
1672 | (cond ((eq ad-return-value 'follow-generic-filter) |
1673 | (setq ad-return-value | |
cb02be17 | 1674 | (cdr-safe (assq (ad-get-arg 0) |
e79016aa KH |
1675 | follow-process-filter-alist)))))) |
1676 | ||
1677 | ||
1678 | (defun follow-call-process-filter (proc) | |
1679 | "Call original `process-filter' without the Follow mode advice." | |
1680 | (ad-disable-advice 'process-filter 'after | |
1681 | 'follow-process-filter) | |
1682 | (ad-activate 'process-filter) | |
cb02be17 | 1683 | (prog1 |
e79016aa | 1684 | (process-filter proc) |
cb02be17 | 1685 | (ad-enable-advice 'process-filter 'after |
e79016aa KH |
1686 | 'follow-process-filter) |
1687 | (ad-activate 'process-filter))) | |
cb02be17 | 1688 | |
e79016aa KH |
1689 | |
1690 | (defun follow-tidy-process-filter-alist () | |
1691 | "Remove old processes from `follow-process-filter-alist'." | |
1692 | (let ((alist follow-process-filter-alist) | |
1693 | (ps (process-list)) | |
1694 | (new ())) | |
1695 | (while alist | |
cb02be17 | 1696 | (if (and (not (memq (process-status (car (car alist))) |
e79016aa KH |
1697 | '(exit signal closed nil))) |
1698 | (memq (car (car alist)) ps)) | |
1699 | (setq new (cons (car alist) new))) | |
1700 | (setq alist (cdr alist))) | |
1701 | (setq follow-process-filter-alist new))) | |
1702 | ||
1703 | ;;}}} | |
1704 | ;;{{{ Start/stop interception of processes. | |
1705 | ||
5eba16a3 JB |
1706 | ;; Normally, all new processes are intercepted by our `set-process-filter'. |
1707 | ;; This is needed to intercept old processes that were started before we were | |
e79016aa KH |
1708 | ;; loaded, and processes we have forgotten by calling |
1709 | ;; `follow-stop-intercept-process-output'. | |
1710 | ||
1711 | (defun follow-intercept-process-output () | |
1712 | "Intercept all active processes. | |
1713 | ||
17f6d002 | 1714 | This is needed so that Follow mode can track all display events in the |
8cd3480b | 1715 | system. (See `follow-mode'.)" |
e79016aa KH |
1716 | (interactive) |
1717 | (let ((list (process-list))) | |
1718 | (while list | |
1719 | (if (eq (process-filter (car list)) 'follow-generic-filter) | |
1720 | nil | |
1721 | ;; The custom `set-process-filter' defined above. | |
1722 | (set-process-filter (car list) (process-filter (car list)))) | |
1723 | (setq list (cdr list)))) | |
1724 | (setq follow-intercept-processes t)) | |
1725 | ||
1726 | ||
1727 | (defun follow-stop-intercept-process-output () | |
17f6d002 | 1728 | "Stop Follow mode from spying on processes. |
e79016aa KH |
1729 | |
1730 | All current spypoints are removed and no new will be added. | |
1731 | ||
cb02be17 KH |
1732 | The effect is that Follow mode won't be able to handle buffers |
1733 | connected to processes. | |
e79016aa KH |
1734 | |
1735 | The only reason to call this function is if the Follow mode spy filter | |
1736 | would interfere with some other package. If this happens, please | |
08a1dbe6 | 1737 | report this using the `report-emacs-bug' function." |
e79016aa KH |
1738 | (interactive) |
1739 | (follow-tidy-process-filter-alist) | |
1ee4a6c5 JB |
1740 | (dolist (process (process-list)) |
1741 | (when (eq (follow-call-process-filter process) 'follow-generic-filter) | |
1742 | (follow-call-set-process-filter | |
1743 | process | |
1744 | (cdr-safe (assq process follow-process-filter-alist))) | |
1745 | (setq follow-process-filter-alist | |
1746 | (delq (assq process follow-process-filter-alist) | |
1747 | follow-process-filter-alist)))) | |
e79016aa KH |
1748 | (setq follow-intercept-processes nil)) |
1749 | ||
1750 | ;;}}} | |
1751 | ;;{{{ The filter | |
1752 | ||
08a1dbe6 SM |
1753 | ;; The following section is a naive method to make buffers with |
1754 | ;; process output to work with Follow mode. Whenever the start of the | |
5eba16a3 | 1755 | ;; window displaying the buffer is moved, we move it back to its |
08a1dbe6 SM |
1756 | ;; original position and try to select a new window. (If we fail, |
1757 | ;; the normal redisplay functions of Emacs will scroll it right | |
1758 | ;; back!) | |
e79016aa KH |
1759 | |
1760 | (defun follow-generic-filter (proc output) | |
1761 | "Process output filter for process connected to buffers in Follow mode." | |
1762 | (let* ((old-buffer (current-buffer)) | |
1763 | (orig-win (selected-window)) | |
1764 | (buf (process-buffer proc)) | |
1765 | (win (and buf (if (eq buf (window-buffer orig-win)) | |
1766 | orig-win | |
1767 | (get-buffer-window buf t)))) | |
1768 | (return-to-orig-win (and win (not (eq win orig-win)))) | |
1769 | (orig-window-start (and win (window-start win)))) | |
1770 | ||
1771 | ;; If input is pending, the `sit-for' below won't redraw the | |
1772 | ;; display. In that case, calling `follow-avoid-tail-recenter' may | |
5eba16a3 | 1773 | ;; provoke the process handling code to schedule a redisplay. |
e79016aa KH |
1774 | ;(or (input-pending-p) |
1775 | ; (follow-avoid-tail-recenter)) | |
1776 | ||
1777 | ;; Output the `output'. | |
1778 | (let ((filter (cdr-safe (assq proc follow-process-filter-alist)))) | |
cb02be17 | 1779 | (cond |
e79016aa KH |
1780 | ;; Call the original filter function |
1781 | (filter | |
1782 | (funcall filter proc output)) | |
1783 | ||
1784 | ;; No filter, but we've got a buffer. Just output into it. | |
1785 | (buf | |
1786 | (set-buffer buf) | |
1787 | (if (not (marker-buffer (process-mark proc))) | |
1788 | (set-marker (process-mark proc) (point-max))) | |
1789 | (let ((moving (= (point) (process-mark proc))) | |
bbf04be4 RS |
1790 | deactivate-mark |
1791 | (inhibit-read-only t)) | |
e79016aa KH |
1792 | (save-excursion |
1793 | (goto-char (process-mark proc)) | |
5eba16a3 | 1794 | ;; `insert-before-markers' just in case the user's next |
e79016aa KH |
1795 | ;; command is M-y. |
1796 | (insert-before-markers output) | |
1797 | (set-marker (process-mark proc) (point))) | |
bbf04be4 | 1798 | (if moving (goto-char (process-mark proc))))))) |
e79016aa KH |
1799 | |
1800 | ;; If we're in follow mode, do our stuff. Select a new window and | |
1801 | ;; redisplay. (Actually, it is redundant to check `buf', but I | |
1802 | ;; feel it's more correct.) | |
09c013ef | 1803 | (if (and buf (window-live-p win)) |
e79016aa KH |
1804 | (progn |
1805 | (set-buffer buf) | |
1806 | (if (and (boundp 'follow-mode) follow-mode) | |
cb02be17 | 1807 | (progn |
e79016aa KH |
1808 | (select-window win) |
1809 | (let* ((windows (follow-all-followers win)) | |
1810 | (win-start-end (follow-windows-start-end windows)) | |
1811 | (new-window-start (window-start win)) | |
1812 | (new-window-point (window-point win))) | |
1813 | (cond | |
bb40a524 PJ |
1814 | ;; The start of the selected window was repositioned. |
1815 | ;; Try to use the original start position and continue | |
1816 | ;; working with a window to the "right" in the window | |
1817 | ;; chain. This will create the effect that the output | |
1818 | ;; starts in one window and continues into the next. | |
1819 | ||
1820 | ;; If the display has changed so much that it is not | |
1821 | ;; possible to keep the original window fixed and still | |
1822 | ;; display the point then we give up and use the new | |
1823 | ;; window start. | |
1824 | ||
1825 | ;; This case is typically used when the process filter | |
1826 | ;; tries to reposition the start of the window in order | |
1827 | ;; to view the tail of the output. | |
e79016aa KH |
1828 | ((not (eq orig-window-start new-window-start)) |
1829 | (follow-debug-message "filter: Moved") | |
1830 | (set-window-start win orig-window-start) | |
1831 | (follow-redisplay windows win) | |
1832 | (setq win-start-end (follow-windows-start-end windows)) | |
cb02be17 | 1833 | (follow-select-if-visible new-window-point |
e79016aa KH |
1834 | win-start-end) |
1835 | (goto-char new-window-point) | |
1836 | (if (eq win (selected-window)) | |
1837 | (set-window-start win new-window-start)) | |
1838 | (setq win-start-end (follow-windows-start-end windows))) | |
1839 | ;; Stick to this window, if point is visible in it. | |
1840 | ((pos-visible-in-window-p new-window-point) | |
1841 | (follow-debug-message "filter: Visible in window")) | |
1842 | ;; Avoid redisplaying the first window. If the | |
1843 | ;; point is visible at a window below, | |
1844 | ;; redisplay and select it. | |
cb02be17 | 1845 | ((follow-select-if-visible-from-first |
e79016aa KH |
1846 | new-window-point windows) |
1847 | (follow-debug-message "filter: Seen from first") | |
e79016aa KH |
1848 | (setq win-start-end |
1849 | (follow-windows-start-end windows))) | |
1850 | ;; None of the above. We stick to the current window. | |
cb02be17 | 1851 | (t |
e79016aa KH |
1852 | (follow-debug-message "filter: nothing"))) |
1853 | ||
5eba16a3 | 1854 | ;; Here we have selected a window. Make sure the |
e79016aa KH |
1855 | ;; windows are aligned and the point is visible |
1856 | ;; in the selected window. | |
cb02be17 | 1857 | (if (and (not (follow-pos-visible |
e79016aa KH |
1858 | (point) (selected-window) win-start-end)) |
1859 | (not return-to-orig-win)) | |
1860 | (progn | |
1861 | (sit-for 0) | |
cb02be17 | 1862 | (setq win-start-end |
e79016aa KH |
1863 | (follow-windows-start-end windows)))) |
1864 | ||
1865 | (if (or follow-internal-force-redisplay | |
1866 | (not (follow-windows-aligned-p win-start-end))) | |
1867 | (follow-redisplay windows))))))) | |
1868 | ||
1869 | ;; return to the original window. | |
1870 | (if return-to-orig-win | |
1871 | (select-window orig-win)) | |
5eba16a3 | 1872 | ;; Restore the original buffer, unless the filter explicitly |
c8d05b03 KH |
1873 | ;; changed buffer or killed the old buffer. |
1874 | (if (and (eq buf (current-buffer)) | |
1875 | (buffer-name old-buffer)) | |
1876 | (set-buffer old-buffer))) | |
cb02be17 | 1877 | |
f3bfa025 KH |
1878 | (follow-invalidate-cache) |
1879 | ||
1880 | ;; Normally, if the display has been changed, it is redrawn. All | |
4b4d84b4 EZ |
1881 | ;; windows showing only the end of a buffer are unconditionally |
1882 | ;; recentered; we can't prevent that by calling | |
f3bfa025 KH |
1883 | ;; `follow-avoid-tail-recenter'. |
1884 | ;; | |
4b4d84b4 EZ |
1885 | ;; We force a redisplay here on our own, so Emacs does need to. |
1886 | ;; (However, redisplaying when there's input available just seems | |
1887 | ;; to make things worse, so we exclude that case.) | |
f3bfa025 KH |
1888 | (if (and follow-avoid-tail-recenter-p |
1889 | (not (input-pending-p))) | |
1890 | (sit-for 0))) | |
e79016aa KH |
1891 | |
1892 | ;;}}} | |
1893 | ||
1894 | ;;}}} | |
1895 | ;;{{{ Window size change | |
1896 | ||
1897 | ;; In Emacs 19.29, the functions in `window-size-change-functions' are | |
1898 | ;; called every time a window in a frame changes size. Most notably, it | |
1899 | ;; is called after the frame has been resized. | |
1900 | ;; | |
ff753438 | 1901 | ;; We basically call our post-command-hook for every buffer that is |
e79016aa KH |
1902 | ;; visible in any window in the resized frame, which is in follow-mode. |
1903 | ;; | |
1904 | ;; Since this function can be called indirectly from | |
1905 | ;; `follow-post-command-hook' we have a potential infinite loop. We | |
1906 | ;; handle this problem by simply not doing anything at all in this | |
1907 | ;; situation. The variable `follow-inside-post-command-hook' contains | |
1908 | ;; information about whether the execution actually is inside the | |
1909 | ;; post-command-hook or not. | |
1910 | ||
1911 | (if (boundp 'window-size-change-functions) | |
1912 | (add-hook 'window-size-change-functions 'follow-window-size-change)) | |
1913 | ||
1914 | ||
1915 | (defun follow-window-size-change (frame) | |
1916 | "Redraw all windows in FRAME, when in Follow mode." | |
7dcef48d SM |
1917 | ;; Below, we call `post-command-hook'. This makes sure that we |
1918 | ;; don't start a mutually recursive endless loop. | |
e79016aa KH |
1919 | (if follow-inside-post-command-hook |
1920 | nil | |
1921 | (let ((buffers '()) | |
1922 | (orig-window (selected-window)) | |
1923 | (orig-buffer (current-buffer)) | |
1924 | (orig-frame (selected-frame)) | |
1925 | windows | |
1926 | buf) | |
1927 | (select-frame frame) | |
1928 | (unwind-protect | |
cb02be17 KH |
1929 | (walk-windows |
1930 | (function | |
e79016aa KH |
1931 | (lambda (win) |
1932 | (setq buf (window-buffer win)) | |
1933 | (if (memq buf buffers) | |
1934 | nil | |
1935 | (set-buffer buf) | |
1936 | (if (and (boundp 'follow-mode) | |
1937 | follow-mode) | |
1938 | (progn | |
1939 | (setq windows (follow-all-followers win)) | |
1940 | (if (memq orig-window windows) | |
1941 | (progn | |
7dcef48d SM |
1942 | ;; Make sure we're redrawing around the |
1943 | ;; selected window. | |
1944 | ;; | |
1945 | ;; We must be really careful not to do this | |
1946 | ;; when we are (indirectly) called by | |
1947 | ;; `post-command-hook'. | |
e79016aa KH |
1948 | (select-window orig-window) |
1949 | (follow-post-command-hook) | |
1950 | (setq orig-window (selected-window))) | |
1951 | (follow-redisplay windows win)) | |
1952 | (setq buffers (cons buf buffers)))))))) | |
1953 | (select-frame orig-frame) | |
1954 | (set-buffer orig-buffer) | |
1955 | (select-window orig-window))))) | |
1956 | ||
1957 | ;;}}} | |
1958 | ||
1959 | ;;{{{ XEmacs isearch | |
1960 | ||
1961 | ;; In XEmacs, isearch often finds matches in other windows than the | |
1962 | ;; currently selected. However, when exiting the old window | |
1963 | ;; configuration is restored, with the exception of the beginning of | |
1964 | ;; the start of the window for the selected window. This is not much | |
1965 | ;; help for us. | |
1966 | ;; | |
1967 | ;; We overwrite the stored window configuration with the current, | |
1968 | ;; unless we are in `slow-search-mode', i.e. only a few lines | |
1969 | ;; of text is visible. | |
1970 | ||
08a1dbe6 | 1971 | (if (featurep 'xemacs) |
e79016aa KH |
1972 | (defadvice isearch-done (before follow-isearch-done activate) |
1973 | (if (and (boundp 'follow-mode) | |
1974 | follow-mode | |
1975 | (boundp 'isearch-window-configuration) | |
1976 | isearch-window-configuration | |
1977 | (boundp 'isearch-slow-terminal-mode) | |
1978 | (not isearch-slow-terminal-mode)) | |
1979 | (let ((buf (current-buffer))) | |
cb02be17 | 1980 | (setq isearch-window-configuration |
e79016aa KH |
1981 | (current-window-configuration)) |
1982 | (set-buffer buf))))) | |
1983 | ||
1984 | ;;}}} | |
1985 | ;;{{{ Tail window handling | |
1986 | ||
08a1dbe6 | 1987 | ;; In Emacs (not XEmacs) windows showing nothing are sometimes |
17f6d002 | 1988 | ;; recentered. When in Follow mode, this is not desirable for |
08a1dbe6 SM |
1989 | ;; non-first windows in the window chain. This section tries to |
1990 | ;; make the windows stay where they should be. | |
1991 | ;; | |
1992 | ;; If the display is updated, all windows starting at (point-max) are | |
1993 | ;; going to be recentered at the next redisplay, unless we do a | |
1994 | ;; read-and-write cycle to update the `force' flag inside the windows. | |
1995 | ;; | |
1996 | ;; In 19.30, a new varible `window-scroll-functions' is called every | |
1997 | ;; time a window is recentered. It is not perfect for our situation, | |
1998 | ;; since when it is called for a tail window, it is to late. However, | |
1999 | ;; if it is called for another window, we can try to update our | |
2000 | ;; windows. | |
2001 | ;; | |
2002 | ;; By patching `sit-for' we can make sure that to catch all explicit | |
2003 | ;; updates initiated by lisp programs. Internal calls, on the other | |
2004 | ;; hand, are not handled. | |
2005 | ;; | |
2006 | ;; Please note that the function `follow-avoid-tail-recenter' is also | |
2007 | ;; called from other places, e.g. `post-command-hook' and | |
2008 | ;; `post-command-idle-hook'. | |
e79016aa | 2009 | |
c8d05b03 KH |
2010 | ;; If this function is called it is too late for this window, but |
2011 | ;; we might save other windows from being recentered. | |
e79016aa KH |
2012 | |
2013 | (if (and follow-avoid-tail-recenter-p (boundp 'window-scroll-functions)) | |
2014 | (add-hook 'window-scroll-functions 'follow-avoid-tail-recenter t)) | |
2015 | ||
2016 | ||
ff753438 | 2017 | ;; This prevents all packages that calls `sit-for' directly |
e79016aa KH |
2018 | ;; to recenter tail windows. |
2019 | ||
2020 | (if follow-avoid-tail-recenter-p | |
2021 | (defadvice sit-for (before follow-sit-for activate) | |
17f6d002 | 2022 | "Adviced by Follow mode. |
e79016aa KH |
2023 | |
2024 | Avoid to recenter windows displaying only the end of a file as when | |
17f6d002 | 2025 | displaying a short file in two windows, using Follow mode." |
e79016aa KH |
2026 | (follow-avoid-tail-recenter))) |
2027 | ||
2028 | ||
2029 | ;; Without this advice, `mouse-drag-region' would start to recenter | |
2030 | ;; tail windows. | |
2031 | ||
2032 | (if (and follow-avoid-tail-recenter-p | |
2033 | (fboundp 'move-overlay)) | |
2034 | (defadvice move-overlay (before follow-move-overlay activate) | |
17f6d002 | 2035 | "Adviced by Follow mode. |
cb02be17 KH |
2036 | Don't recenter windows showing only the end of a buffer. |
2037 | This prevents `mouse-drag-region' from messing things up." | |
e79016aa KH |
2038 | (follow-avoid-tail-recenter))) |
2039 | ||
2040 | ;;}}} | |
2041 | ;;{{{ profile support | |
2042 | ||
2043 | ;; The following (non-evaluated) section can be used to | |
2044 | ;; profile this package using `elp'. | |
2045 | ;; | |
2046 | ;; Invalid indentation on purpose! | |
2047 | ||
2048 | (cond (nil | |
2049 | (setq elp-function-list | |
cb02be17 KH |
2050 | '(window-end |
2051 | vertical-motion | |
e79016aa KH |
2052 | ; sit-for ;; elp can't handle advices... |
2053 | follow-mode | |
2054 | follow-all-followers | |
cb02be17 | 2055 | follow-split-followers |
e79016aa | 2056 | follow-redisplay |
e79016aa KH |
2057 | follow-estimate-first-window-start |
2058 | follow-calculate-first-window-start-from-above | |
2059 | follow-calculate-first-window-start-from-below | |
2060 | follow-calc-win-end | |
2061 | follow-calc-win-start | |
2062 | follow-pos-visible | |
2063 | follow-windows-start-end | |
f3bfa025 | 2064 | follow-cache-valid-p |
e79016aa KH |
2065 | follow-select-if-visible |
2066 | follow-select-if-visible-from-first | |
2067 | follow-windows-aligned-p | |
2068 | follow-point-visible-all-windows-p | |
2069 | follow-avoid-tail-recenter | |
2070 | follow-update-window-start | |
2071 | follow-post-command-hook | |
2072 | )))) | |
2073 | ||
2074 | ;;}}} | |
2075 | ||
2076 | ;;{{{ The end | |
2077 | ||
fc779383 | 2078 | (defun follow-unload-function () |
fc8c78cc | 2079 | "Unload Follow mode library." |
fc779383 JB |
2080 | (easy-menu-remove-item nil '("Tools") "Follow") |
2081 | (follow-stop-intercept-process-output) | |
2082 | (dolist (group '((before | |
2083 | ;; XEmacs | |
2084 | isearch-done | |
2085 | ;; both | |
2086 | set-process-filter sit-for move-overlay) | |
2087 | (after | |
2088 | ;; Emacs | |
2089 | scroll-bar-drag scroll-bar-drag-1 scroll-bar-scroll-down | |
2090 | scroll-bar-scroll-up scroll-bar-set-window-start | |
2091 | ;; XEmacs | |
2092 | scrollbar-line-down scrollbar-line-up scrollbar-page-down | |
2093 | scrollbar-page-up scrollbar-to-bottom scrollbar-to-top | |
2094 | scrollbar-vertical-drag | |
2095 | ;; both | |
2096 | process-filter))) | |
2097 | (let ((class (car group))) | |
2098 | (dolist (fun (cdr group)) | |
2099 | (when (functionp fun) | |
2100 | (condition-case nil | |
2101 | (progn | |
2102 | (ad-remove-advice fun class | |
2103 | (intern (concat "follow-" (symbol-name fun)))) | |
2104 | (ad-update fun)) | |
2105 | (error nil)))))) | |
fc8c78cc | 2106 | ;; continue standard processing |
fc779383 JB |
2107 | nil) |
2108 | ||
08a1dbe6 SM |
2109 | ;; |
2110 | ;; We're done! | |
2111 | ;; | |
e79016aa KH |
2112 | |
2113 | (provide 'follow) | |
2114 | ||
2115 | ;;}}} | |
2116 | ||
2117 | ;; /------------------------------------------------------------------------\ | |
2118 | ;; | "I [..] am rarely happier then when spending an entire day programming | | |
2119 | ;; | my computer to perform automatically a task that it would otherwise | | |
2120 | ;; | take me a good ten seconds to do by hand. Ten seconds, I tell myself, | | |
2121 | ;; | is ten seconds. Time is valuable and ten seconds' worth of it is well | | |
2122 | ;; | worth the investment of a day's happy activity working out a way to | | |
2123 | ;; | save it". -- Douglas Adams, "Last Chance to See" | | |
2124 | ;; \------------------------------------------------------------------------/ | |
2125 | ||
2126 | ;;; follow.el ends here |