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