follow.el 72 KB

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