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