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