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