home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / scroll-in-place.el < prev    next >
Encoding:
Text File  |  1995-02-17  |  72.1 KB  |  1,560 lines

  1.  
  2. ;;;; -*-Emacs-Lisp-*- Improved Vertical Scrolling Commands
  3. ;;;; Written by Eric Eide, last modified on 1994/11/18 21:23:01.
  4. ;;;; (C) Copyright 1993, 1994, Eric Eide and the University of Utah
  5. ;;;;
  6. ;;;; COPYRIGHT NOTICE
  7. ;;;;
  8. ;;;; This program is free software; you can redistribute it and/or modify it
  9. ;;;; under the terms of the GNU General Public License as published by the Free
  10. ;;;; Software Foundation; either version 2 of the License, or (at your option)
  11. ;;;; any later version.
  12. ;;;;
  13. ;;;; This program is distributed in the hope that it will be useful, but
  14. ;;;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;;;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. ;;;; for more details.
  17. ;;;;
  18. ;;;; You should have received a copy of the GNU General Public License along
  19. ;;;; with GNU Emacs.  If you did not, write to the Free Software Foundation,
  20. ;;;; Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
  21.  
  22. ;;;; AUTHORS
  23. ;;;;
  24. ;;;; This package was written by Eric Eide (eeide@cs.utah.edu) and was based on
  25. ;;;; a very similar package ("scroll-fix") by Joe Wells.  Almost all of the
  26. ;;;; code in this file is original, but I owe a great debt to Mr. Wells for his
  27. ;;;; ideas and his original implementation.
  28. ;;;;
  29. ;;;;   Eric Eide (eeide@cs.utah.edu)
  30. ;;;;   University of Utah
  31. ;;;;   3190 Merrill Engineering Building
  32. ;;;;   Salt Lake City, Utah  84112
  33. ;;;;
  34. ;;;;   Joe Wells (jbw@cs.bu.edu)
  35. ;;;;
  36. ;;;; Joe Wells' "scroll-fix" package is Copyright (C) 1988, 1989, and 1991 by
  37. ;;;; the Free Software Foundation.  It is distributed under the terms of the
  38. ;;;; GNU General Public License.
  39.  
  40. ;;;; LISP CODE DIRECTORY INFORMATION
  41. ;;;;
  42. ;;;; LCD Archive Entry:
  43. ;;;; scroll-in-place|Eric Eide|eeide@cs.utah.edu|
  44. ;;;; Improved vertical scrolling commands|
  45. ;;;; 1994/11/18 21:23:01|1.3|~/misc/scroll-in-place.el.Z|
  46.  
  47. ;;;; SUMMARY
  48. ;;;;
  49. ;;;; This package provides improved vertical scrolling commands for GNU Emacs.
  50. ;;;; These new commands offer the following features:
  51. ;;;;
  52. ;;;; + When a scrolling command is executed, GNU Emacs tries to keep point as
  53. ;;;;   close as possible to its original window position (window line and
  54. ;;;;   column).  This is what "scroll in place" means: point stays "in place"
  55. ;;;;   within the window.  (There are times when point must be moved from its
  56. ;;;;   original window position in order to execute the scroll; see below.)
  57. ;;;;
  58. ;;;;   The variable `scroll-in-place', which is true by default, determines
  59. ;;;;   whether or not the standard GNU Emacs scrolling commands (`scroll-down',
  60. ;;;;   `scroll-up', `scroll-other-window-down', and `scroll-other-window') use
  61. ;;;;   the "in place" features listed here.  When `scroll-in-place' is `nil'
  62. ;;;;   the standard GNU Emacs scrolling commands essentially just call the
  63. ;;;;   original versions of themselves.  (Note that even when `scroll-in-place'
  64. ;;;;   is `nil' the new versions of `scroll-down' and `scroll-up' have slightly
  65. ;;;;   different behavior when a minibuffer window is the selected window.  See
  66. ;;;;   below.)
  67. ;;;;
  68. ;;;;   It is possible to turn off (or turn on) "in place" scrolling for certain
  69. ;;;;   buffers by making buffer-local bindings of the variable `scroll-in-
  70. ;;;;   place' for those buffers.  The variable `scroll-in-place' is not usually
  71. ;;;;   buffer-local, but you can make it so if you desire.
  72. ;;;;
  73. ;;;; + Because the improved scrolling commands keep point at its original
  74. ;;;;   window position, these scrolling commands are "reversible."  The
  75. ;;;;   `scroll-up' command undoes the effect of the immediately previous
  76. ;;;;   `scroll-down' command (if any) and vice versa.  In other words, if you
  77. ;;;;   scroll up and then immediately scroll back down, the window config-
  78. ;;;;   uration is restored to its exact original state.  This allows you to
  79. ;;;;   browse through a buffer more easily, as you can always get back to the
  80. ;;;;   original configuration.
  81. ;;;;
  82. ;;;;   Note, however, that the improved scrolling commands are guaranteed to be
  83. ;;;;   reversible only if there are no intervening non-scrolling commands.
  84. ;;;;   Also, if you give a prefix argument to a scrolling command (in order to
  85. ;;;;   specify the number of lines to scroll by), previous scrolling commands
  86. ;;;;   may no longer be reversible.  More specifically, if the new prefix
  87. ;;;;   argument has a different magnitude than the previous scrolling distance,
  88. ;;;;   then any previous scrolling commands are not reversible.  The new prefix
  89. ;;;;   argument takes precedence.
  90. ;;;;
  91. ;;;;   You might find it useful to think of the scrolling commands as forming
  92. ;;;;   "chains."  A scrolling command either starts or continues a chain.  By
  93. ;;;;   issuing a non-scrolling command or by changing the number of lines to be
  94. ;;;;   scrolled, you break the chain.  (Note that simply changing the scrolling
  95. ;;;;   direction won't break the chain; changing the absolute number of lines
  96. ;;;;   to be scrolled is what breaks the chain.)  Scrolling commands are
  97. ;;;;   guaranteed to be reversible only within the current chain.  Hopefully
  98. ;;;;   that's clear enough.
  99. ;;;;
  100. ;;;; + When a scrolling command is given a prefix argument (which specifies the
  101. ;;;;   number of lines to scroll by), then that argument becomes the default
  102. ;;;;   scrolling distance for all immediately subsequent scrolling commands.
  103. ;;;;   This means that you can easily set the scrolling distance for a chain
  104. ;;;;   of scrolling commands.  Note that a new prefix argument or any non-
  105. ;;;;   scrolling command breaks the chain (as described above), and any further
  106. ;;;;   scrolling commands will use the usual defaults (or the prefix argument
  107. ;;;;   you specify at that time, of course).
  108. ;;;;
  109. ;;;;   However, there are cases in which one doesn't want the current scrolling
  110. ;;;;   command to use the default scrolling distance that was set by the
  111. ;;;;   previous scrolling command.  For example, suppose that you had special
  112. ;;;;   commands that scrolled one line up and one line down.  When you invoke
  113. ;;;;   one of these commands, the "in place" scrolling routines set the default
  114. ;;;;   scrolling distance to be just one line.  Now suppose that you use one of
  115. ;;;;   your special commands and then immediately invoke `scroll-up' (`C-v'),
  116. ;;;;   expecting it to scroll by a near windowful of text.  You would be
  117. ;;;;   disappointed --- because the previous command set the default scrolling
  118. ;;;;   distance to be just one line, `scroll-up' just scrolls by one line.
  119. ;;;;
  120. ;;;;   To solve this problem, "scroll-in-place" allows you to divide scrolling
  121. ;;;;   commands into separate "groups."  Commands in a group can only form
  122. ;;;;   chains with (and therefore, inherit defaults from) commands in the same
  123. ;;;;   group.  (Note that no command can be in more than one group.)  If you
  124. ;;;;   invoke a scrolling command that is not in the same group as that of the
  125. ;;;;   immediately previous scrolling command, then the previous chain is
  126. ;;;;   broken and you start a new chain --- with a new set of defaults.
  127. ;;;;
  128. ;;;;   So to solve the problem described above, you could put your one-line
  129. ;;;;   scrolling commands in their own group.  Once that is done, the standard
  130. ;;;;   scrolling commands will not form chains with your one-line scrolling
  131. ;;;;   commands, and therefore will not use the default scrolling distance set
  132. ;;;;   by those commands.  Problem solved!
  133. ;;;;
  134. ;;;;   By default, all "in place" scrolling commands are in a single group.  If
  135. ;;;;   you want to partition some commands into separate groups, you must do
  136. ;;;;   that yourself *before* any "in place" commands are invoked.  For more
  137. ;;;;   information about grouping commands, see the documentation for the
  138. ;;;;   variables `scroll-command-groups' and `scroll-default-command-group'.
  139. ;;;;
  140. ;;;; + The improved scrolling commands will avoid displaying empty lines past
  141. ;;;;   the end of the buffer when possible.  In other words, just as you can't
  142. ;;;;   see "dead space" before the beginning of the buffer text, the new
  143. ;;;;   scrolling commands try to avoid displaying "dead space" past the end of
  144. ;;;;   the buffer text.  This behavior is somewhat configurable; see the
  145. ;;;;   documentation for the variable `scroll-allow-blank-lines-past-eob'.
  146. ;;;;
  147. ;;;;   Dead space will be displayed if it is necessary in order to make a
  148. ;;;;   previous scrolling action reversible, however.
  149. ;;;;
  150. ;;;; + If the scrolling commands cannot keep point at its initial window
  151. ;;;;   position (because a buffer boundary is on screen and the window can't be
  152. ;;;;   scrolled as far as necessary to keep point at the right place), point is
  153. ;;;;   allowed to temporarily stray from its initial window position.  That is,
  154. ;;;;   point moves the correct number of window lines, even if it means that it
  155. ;;;;   has to stray from its desired window position.  This straying is undone
  156. ;;;;   when (and if) the scrolling action is reversed.
  157. ;;;;
  158. ;;;; + If a scrolling command tries to move point past a buffer boundary, point
  159. ;;;;   is instead moved to the boundary (the beginning or the end of the buffer
  160. ;;;;   as appropriate) and an appropriate message is displayed.  This motion is
  161. ;;;;   reversible, of course.
  162. ;;;;
  163. ;;;;   However, if point was already at the buffer boundary when the scrolling
  164. ;;;;   command was invoked, the command signals an appropriate error instead.
  165. ;;;;
  166. ;;;; + When a minibuffer window is the selected window, the new versions of
  167. ;;;;   `scroll-up' and `scroll-down' either scroll the window in the variable
  168. ;;;;   `minibuffer-scroll-window' (which is usually the window of completions)
  169. ;;;;   or the `next-window' if there is no `minibuffer-scroll-window'.  This is
  170. ;;;;   usually much more useful than scrolling the minibuffer itself.  (Note
  171. ;;;;   that this feature is available even when the variable `scroll-in-place'
  172. ;;;;   is `nil'.)
  173. ;;;;
  174. ;;;; + When a scrolling command is scrolling a window other than the selected
  175. ;;;;   window, it will signal an appropriate buffer boundary error if the
  176. ;;;;   window cannot be scrolled (because the appropriate buffer boundary is
  177. ;;;;   already visible).  This means that an error is signalled even in cases
  178. ;;;;   that would be allowed (by "straying" point or by moving it to the buffer
  179. ;;;;   boundary) if the window were selected.
  180. ;;;;
  181. ;;;;   (If an error were not signalled in these cases, then there would be many
  182. ;;;;   cases in which the last scroll in a particular direction would appear to
  183. ;;;;   do nothing because only the point position would change --- the
  184. ;;;;   displayed text would stay the same!  To avoid these cases the scrolling
  185. ;;;;   commands signal boundary errors "prematurely" when the window to be
  186. ;;;;   scrolled is not selected.)
  187. ;;;;
  188. ;;;; So how is this package different than Joe Wells' "scroll-fix" package?
  189. ;;;;
  190. ;;;; + This package provides "in place" behavior for the standard GNU Emacs
  191. ;;;;   commands by default; "scroll-fix" does not.
  192. ;;;;
  193. ;;;; + "scroll-fix" behaves differently when the window is near a buffer
  194. ;;;;   boundary.  Instead of allowing point to stray, "scroll-fix" first does
  195. ;;;;   an incomplete scroll (i.e., moves point less than the full distance in
  196. ;;;;   order to keep point at the desired window position) and then pops point
  197. ;;;;   to the buffer boundary.  I think that the behavior of this package is
  198. ;;;;   somewhat move intuitive, especially for small scrolling distances.
  199. ;;;;
  200. ;;;; + The scrolling commands in this package will appropriately signal buffer
  201. ;;;;   boundary errors; the commands in "scroll-fix" never signal boundary
  202. ;;;;   errors.  This makes it difficult to allow "scroll-fix" to replace the
  203. ;;;;   standard `scroll-down' and `scroll-up' commands because some other
  204. ;;;;   packages (e.g., VM and GNUS) expect the scrolling commands to signal
  205. ;;;;   these errors as necessary.
  206. ;;;;
  207. ;;;; + This package handles long lines correctly.  (But see PROBLEMS, below.)
  208. ;;;;
  209. ;;;; + "scroll-fix" handles prefix arguments differently.  In "scroll-fix", a
  210. ;;;;   number-containing prefix argument always breaks any running chain of
  211. ;;;;   scrolling commands.  The prefix argument `-' (the symbol minus,
  212. ;;;;   generated by `C-u -') causes a temporary change in direction --- a
  213. ;;;;   change for only the current command.  In this package, however, a
  214. ;;;;   number-containing prefix argument only breaks a running chain if it has
  215. ;;;;   a different magnitude than the default scrolling distance, and the
  216. ;;;;   prefix argument `-' causes a permanent change in the sign of the default
  217. ;;;;   scrolling distance --- a change visible to immediately subsequent
  218. ;;;;   scrolling commands.
  219. ;;;;
  220. ;;;; + This package keeps track of the set of "in place" scrolling commands
  221. ;;;;   dynamically, in order to detect "chains" of scrolling commands.
  222. ;;;;   "scroll-fix" has a fixed list of scrolling commands, so "scroll-fix"
  223. ;;;;   cannot keep track of some chains.  (Again, "scroll-fix" interacts badly
  224. ;;;;   with VM and GNUS.)  And because "scroll-fix" keeps a static list of
  225. ;;;;   scrolling commands, it is a bad idea to call its "in place" commands
  226. ;;;;   from a program.  This package, because it maintains the information
  227. ;;;;   dynamically, has no such problems.
  228. ;;;;
  229. ;;;; + This package allows one to divide the "in place" scrolling commands into
  230. ;;;;   groups; a command in a group only forms chains with the members of its
  231. ;;;;   group.  "scroll-fix" has no notion of command groups.
  232. ;;;;
  233. ;;;; + This package provides "in place" versions of the standard GNU Emacs
  234. ;;;;   commands `scroll-other-window-down' and `scroll-other-window'.
  235. ;;;;
  236. ;;;; + This package will refuse to scroll non-selected windows (by signalling
  237. ;;;;   an error) when the displayed text would not change, as described in the
  238. ;;;;   feature list above.
  239. ;;;;
  240. ;;;; + When a minibuffer window is selected, this package always scrolls a
  241. ;;;;   window other than the minibuffer.  "scroll-fix" will scroll another
  242. ;;;;   window only if the entire minibuffer contents are visible.
  243. ;;;;
  244. ;;;; + "scroll-fix" provides a command to toggle the "in place" behavior of the
  245. ;;;;   standard GNU Emacs commands.  This package doesn't; you'll have to set
  246. ;;;;   the option manually with the command `set-variable'.
  247. ;;;;
  248. ;;;; + This package has gratuitous variable renaming (insert smile here!):
  249. ;;;;
  250. ;;;;   "scroll-fix" user variable            Equivalent in this package
  251. ;;;;   -----------------------------------------------------------------------
  252. ;;;;   scroll-in-place                       (none)
  253. ;;;;   scroll-in-place-replace-original      scroll-in-place
  254. ;;;;   scroll-in-place-eob-blank-allowed     scroll-allow-blank-lines-past-eob
  255. ;;;;
  256. ;;;; + This package allows programmers to specify the default scrolling
  257. ;;;;   distance (i.e., the default distance used when starting a new chain of
  258. ;;;;   scrolling commands) for custom scrolling commands.
  259.  
  260. ;;;; COMMANDS AND FUNCTIONS
  261. ;;;;
  262. ;;;; This package provides the following "in place" versions of GNU Emacs'
  263. ;;;; standard vertical scrolling commands:
  264. ;;;;
  265. ;;;;   scroll-down-in-place
  266. ;;;;   scroll-up-in-place
  267. ;;;;   scroll-other-window-down-in-place
  268. ;;;;   scroll-other-window-in-place
  269. ;;;;
  270. ;;;; The variable `scroll-in-place', which is true by default, determines
  271. ;;;; whether or not the new versions of the standard GNU Emacs scrolling
  272. ;;;; commands (`scroll-down', `scroll-up', `scroll-other-window-down', and
  273. ;;;; `scroll-other-window') use the "in place" features listed above.  When
  274. ;;;; `scroll-in-place' is `nil' the standard GNU Emacs scrolling commands
  275. ;;;; essentially just call the original versions of themselves.  (Note that
  276. ;;;; even when `scroll-in-place' is `nil' the new versions of `scroll-down' and
  277. ;;;; `scroll-up' have slightly different behavior when a minibuffer window is
  278. ;;;; the selected window.  See the feature list above.)
  279. ;;;;
  280. ;;;; NOTE that this package redefines the standard GNU Emacs commands `scroll-
  281. ;;;; down', `scroll-up', `scroll-other-window-down', and `scroll-other-window'
  282. ;;;; (in order to check the variable `scroll-in-place', as described above).
  283. ;;;; The command `scroll-other-window-down' first appeared as a standard
  284. ;;;; command in the FSF's GNU Emacs 19.26.
  285. ;;;;
  286. ;;;; This package also provides the following functions and variables which are
  287. ;;;; of use to programmers:
  288. ;;;;
  289. ;;;;   scroll-window
  290. ;;;;   scroll-window-in-place
  291. ;;;;   scroll-window-in-place-continue-sequence
  292. ;;;;   scroll-default-lines (variable)
  293. ;;;;   scroll-command-groups (variable)
  294. ;;;;
  295. ;;;; The `scroll-window-in-place' function is the heart of the "in place"
  296. ;;;; scrolling commands.  `scroll-window' is a function that checks the
  297. ;;;; variable `scroll-in-place' and calls the appropriate scrolling function
  298. ;;;; (either `scroll-window-in-place' or one of the original versions of
  299. ;;;; `scroll-down' and `scroll-up').  The function `scroll-window-in-place-
  300. ;;;; continue-sequence' is provided in order to preserve running "chains" of
  301. ;;;; scrolling commands as described above.
  302. ;;;;
  303. ;;;; The variable `scroll-default-lines' determines the default scrolling
  304. ;;;; distance when a new chain of "in place" scrolling commands begins.  If
  305. ;;;; this variable is not a number, then the default distance is the height of
  306. ;;;; the window to be scrolled minus `next-screen-context-lines'.  The variable
  307. ;;;; `scroll-command-groups' contains the explicit groups of "in place"
  308. ;;;; scrolling commands; for more information read the variable documentation.
  309.  
  310. ;;;; YOUR .EMACS FILE
  311. ;;;;
  312. ;;;; To use this package, you simply need to load it from within your ".emacs"
  313. ;;;; file:
  314. ;;;;
  315. ;;;;   (require 'scroll-in-place)
  316. ;;;;
  317. ;;;; By default, this package provides for the standard GNU Emacs vertical
  318. ;;;; scrolling commands (`scroll-down', `scroll-up', `scroll-other-window-
  319. ;;;; down', and `scroll-other-window') to use the "in place" features.  If you
  320. ;;;; would rather not have this, set the variable `scroll-in-place' to `nil':
  321. ;;;;
  322. ;;;;   (setq scroll-in-place nil)
  323. ;;;;
  324. ;;;; When `scroll-in-place' is `nil' you will have to bind keys in order to
  325. ;;;; call the "in place" scrolling commands.  For example, you might want to do
  326. ;;;; the following:
  327. ;;;;
  328. ;;;;   (global-set-key "\M-v" 'scroll-down-in-place)
  329. ;;;;   (global-set-key "\C-v" 'scroll-up-in-place)
  330. ;;;;
  331. ;;;; Sun users should also read the PROBLEMS section, below.
  332. ;;;;
  333. ;;;; ADVANCED CUSTOMIZATION
  334. ;;;;
  335. ;;;; If you want to partition certain "in place" scrolling commands into
  336. ;;;; separate groups, you should do something like the following:
  337. ;;;;
  338. ;;;;   ;; Make one group containing the commands `scroll-down-one-line' and
  339. ;;;;   ;; `scroll-up-one-line'.  (These are not standard GNU Emacs commands.)
  340. ;;;;   (setq scroll-command-groups
  341. ;;;;         (list '(scroll-down-one-line scroll-up-one-line)))
  342. ;;;;
  343. ;;;; You could write the `scroll-down-one-line' command like this:
  344. ;;;;
  345. ;;;;   (defun scroll-down-one-line (arg)
  346. ;;;;     "Scroll down one line, or number of lines specified by prefix arg."
  347. ;;;;     (interactive "P")
  348. ;;;;     (let ((scroll-default-lines 1))
  349. ;;;;       (scroll-down-in-place arg)))
  350. ;;;;
  351. ;;;; If you want to disable "in place" scrolling for windows that display a
  352. ;;;; particular buffer (while leaving it available in other windows), you can
  353. ;;;; make `scroll-in-place' a buffer-local variable for that buffer and then
  354. ;;;; bind that local copy of `scroll-in-place' to `nil'.  This is the kind of
  355. ;;;; thing that one generally does in a major mode hook.  For example, you can
  356. ;;;; disable "in place" scrolling of GNUS article windows with the following
  357. ;;;; code:
  358. ;;;;
  359. ;;;;   (setq gnus-article-mode-hook
  360. ;;;;         (function (lambda ()
  361. ;;;;                     (make-local-variable 'scroll-in-place)
  362. ;;;;                     (setq scroll-in-place nil))))
  363. ;;;;   ;; Set the variable `gnus-Article-mode-hook' instead if you are using
  364. ;;;;   ;; an old version of GNUS, say version 3.13 or 3.14.
  365. ;;;;
  366. ;;;; The variable `scroll-allow-blank-lines-past-eob' can also be made local to
  367. ;;;; particular buffers, if you desire.  (But why would you want to do that?)
  368.  
  369. ;;;; PROBLEMS
  370. ;;;;
  371. ;;;; + It is sometimes difficult for one's eyes to follow an incomplete scroll
  372. ;;;;   (i.e., a scroll in which the text doesn't move as far as one expected),
  373. ;;;;   especially when the scrolled window is not selected (and therefore that
  374. ;;;;   window's point is not highlighted).  One can lose one's place in the
  375. ;;;;   text.
  376. ;;;;
  377. ;;;; + The names `scroll-down-in-place' and `scroll-up-in-place' conflict with
  378. ;;;;   two commands in the GNU Emacs terminal-specific file "term/sun.el".
  379. ;;;;   This means that in order to load this package correctly, Sunterm users
  380. ;;;;   will have to use the hook `term-setup-hook'.  For example, you might put
  381. ;;;;   the following form in your ".emacs" file:
  382. ;;;;
  383. ;;;;   (setq term-setup-hook (function (lambda () (require 'scroll-in-place))))
  384. ;;;;
  385. ;;;;   If this is confusing, get help from your local GNU Emacs guru.
  386. ;;;;
  387. ;;;; + `scroll-determine-goal-column' tries to honor the variable `track-eol'
  388. ;;;;   if it is set.  But when lines are being wrapped we can't move point past
  389. ;;;;   the wrap --- or else it is possible that scrolling won't work correctly.
  390. ;;;;   In short, this package honors `track-eol' as best it can.
  391. ;;;;
  392. ;;;; + `scroll-window-in-place' can become confused when something changes the
  393. ;;;;   window "out from under it."  By "confused" I mean that it is possible
  394. ;;;;   for `scroll-window-in-place' to think that it should continue the
  395. ;;;;   running sequence of "in place" scrolls when it should really probably
  396. ;;;;   start a new sequence.  For example, if a process filter inserts text
  397. ;;;;   into the buffer and moves point, `scroll-window-in-place' loses track of
  398. ;;;;   where point should be and where the window should start.  Commands that
  399. ;;;;   call a "scroll in place" function and then subsequently move point can
  400. ;;;;   also confuse `scroll-window-in-place'.
  401. ;;;;
  402. ;;;;   To correct some of this confusion, `scroll-window-in-place' could keep
  403. ;;;;   track of the final positions of `window-start' and `window-point',
  404. ;;;;   possibly with both markers and character positions.  In my experience
  405. ;;;;   the "in place" scrolling commands are almost never confused (except by
  406. ;;;;   fancy packages that do their own fancy kinds of scrolling, as described
  407. ;;;;   below), so the extra sanity checking isn't worth the effort.  If your
  408. ;;;;   mileage varies let me know.
  409. ;;;;
  410. ;;;; + The "in place" scrolling commands can interact poorly with packages that
  411. ;;;;   provide their own special scrolling commands.  For example, there are
  412. ;;;;   varying degrees of conflict with Rmail, VM, and GNUS.
  413. ;;;;
  414. ;;;;   RMAIL
  415. ;;;;
  416. ;;;;   In the version of Rmail that is part of the FSF's GNU Emacs 19 (19.25
  417. ;;;;   through 19.28 at least), the command `rmail-summary-scroll-msg-down' in
  418. ;;;;   the file "rmailsum.el" fails to work properly when "in place" scrolling
  419. ;;;;   is enabled for the Rmail message buffer.  (The source of the conflict:
  420. ;;;;   the "in place" scrolling commands and Emacs' standard scrolling commands
  421. ;;;;   interpret the argument '- in different ways.)  Fortunately it is easy to
  422. ;;;;   patch Rmail.  Send me mail if you would like to receive a copy of these
  423. ;;;;   patches.
  424. ;;;;
  425. ;;;;   I know of no conflicts between the "in place" scrolling commands and
  426. ;;;;   older versions of Rmail (i.e., versions that came with GNU Emacs 18).
  427. ;;;;
  428. ;;;;   VM
  429. ;;;;
  430. ;;;;   `scroll-window-in-place' is *very* confused by VM 5's message scrolling
  431. ;;;;   commands, especially because VM 5 rebuilds Emacs' window configuration
  432. ;;;;   from scratch so often.  I have written an experimental set of patches
  433. ;;;;   for VM 5.70 that allows VM 5 to use the "scroll-in-place" features; send
  434. ;;;;   me mail if you would like to receive a copy of these patches.  I hope
  435. ;;;;   that someday my patches will be incorporated into VM.
  436. ;;;;
  437. ;;;;   `scroll-window-in-place' is not confused by VM 4.41's message scrolling
  438. ;;;;   commands, however.
  439. ;;;;
  440. ;;;;   GNUS
  441. ;;;;
  442. ;;;;   `scroll-window-in-place' can be *slightly* confused by GNUS' article
  443. ;;;;   scrolling commands because they move point to the last line of the
  444. ;;;;   article window and then scroll the text.  (This is the case for at least
  445. ;;;;   GNUS versions 3.13 through 4.1, inclusive.)  The potential conflict is
  446. ;;;;   so minor, however, that you'll probably never notice it.  I never do.
  447. ;;;;
  448. ;;;;   A severe conflict, however, exists between the "in place" scrolling
  449. ;;;;   commands and the add-on "gnus-hide" package.  "gnus-hide" can elide
  450. ;;;;   signatures at the ends of articles but it does so in a way that causes
  451. ;;;;   `scroll-window-in-place', as invoked by the GNUS scrolling commands, not
  452. ;;;;   to signal end-of-buffer conditions at the right times.  Someday I may
  453. ;;;;   write new article scrolling commands for GNUS.
  454. ;;;;
  455. ;;;; + Process filters that call scrolling functions can cause confusion.  They
  456. ;;;;   may break running chains of "in place" scrolling commands and they may
  457. ;;;;   set up inappropriate defaults for future scrolling commands.  Maybe this
  458. ;;;;   is a moot problem, as I am currently unaware of any process filters that
  459. ;;;;   invoke scrolling commands (although many filters move point around,
  460. ;;;;   which will also confuse `scroll-window-in-place').
  461.  
  462. ;; (provide 'scroll-in-place) at the end of this file.
  463.  
  464.  
  465. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  466. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  467. ;;;;
  468. ;;;; Here are the variable declarations, both user options and internal
  469. ;;;; variables.
  470. ;;;;
  471. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  472. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  473.  
  474. (defvar scroll-in-place t
  475.   "*When this variable is true (i.e., non-`nil'), the standard GNU Emacs
  476. vertical scrolling commands `scroll-down', `scroll-up', `scroll-other-window-
  477. down', and `scroll-other-window' will attempt to keep point at its current
  478. position in the window (window line and column).  In other words, point stays
  479. \"in place\" within the window.
  480.  
  481. When this variable is `nil' the standard GNU Emacs vertical scrolling commands
  482. behave as usual.  The \"in place\" equivalents, however, are still available as
  483. separate commands.
  484.  
  485. This variable may be made buffer-local in order to disable (or enable) \"in
  486. place\" scrolling in particular buffers."
  487.   ;; I have thought about dividing `scroll-in-place' into three variables: a
  488.   ;; list of commands that always scroll in place, a list of commands that
  489.   ;; never scroll in place, and a flag that determines the default behavior of
  490.   ;; other scrolling commands.  This could make it easier to make "in place"
  491.   ;; scrolling the default because one could single out certain ill-behaved
  492.   ;; commands.  But as of now I'm sure that the added complexity would really
  493.   ;; be worth it.
  494.   )
  495.  
  496. (defvar scroll-allow-blank-lines-past-eob nil
  497.   "*When this variable is `nil' the \"in place\" scrolling commands will avoid
  498. displaying empty lines past the end of the buffer text.  In other words, just
  499. as you can't see \"dead space\" before the beginning of the buffer text, the
  500. \"in place\" scrolling commands try to avoid displaying \"dead space\" past the
  501. end of the buffer text.  This helps make the most of window real estate.
  502.  
  503. Note that sometimes it is necessary to display \"dead space\" in order to make
  504. a previous scrolling action reversible.
  505.  
  506. When this variable is non-`nil' the \"in place\" scrolling commands will always
  507. allow blank lines to be shown past the end of the buffer.")
  508.  
  509. ;;;;
  510. ;;;; The following variables are not user options, but are intended to be set
  511. ;;;; by code outside this package.
  512. ;;;;
  513.  
  514. (defvar scroll-default-lines nil
  515.   "The default number of lines to be scrolled by when a new sequence of \"in
  516. place\" scrolling commands begins.  Of course, when an explicit number of lines
  517. is specified, that explicit number takes precedence.  See the documentation for
  518. the function `scroll-window-in-place' for more information.
  519.  
  520. If this variable is not bound to a number, then the default number of lines is
  521. the height of the window to be scrolled minus `next-screen-context-lines'.
  522.  
  523. This variable should not be set globally!  Commands that want to specify a
  524. default scrolling distance should just bind the variable `scroll-default-lines'
  525. temporarily.")
  526.  
  527. (defvar scroll-command-groups nil
  528.   "The explicitly specified \"groups\" of \"in place\" scrolling commands.
  529. This variable should be set before or immediately after the \"in place\"
  530. scrolling package is loaded, and then not changed after that.
  531.  
  532. Usually, \"in place\" scrolling commands share state (e.g., the number of lines
  533. to scroll by) with any and all immediately previous \"in place\" scrolling
  534. commands.  Sometimes, however, this is undesirable.  In these cases the \"in
  535. place\" scrolling commands can be divided into groups.  A command in a group
  536. only shares state with members of its group.
  537.  
  538. Each element of `scroll-command-groups' is a list that contains all of the
  539. members of a unique command group.  For example, if there were only one
  540. explicit group and that group contained the commands `scroll-down-one-line' and
  541. `scroll-up-one-line', then `scroll-command-groups' would be set to:
  542.  
  543.   ((scroll-down-one-line scroll-up-one-line))
  544.  
  545. Commands that are not in any explicitly specified group are added to a default
  546. group.  That group is stored in the variable `scroll-default-command-group'.
  547.  
  548. The \"in place\" scrolling functions assume that all of the scrolling command
  549. groups are nonintersecting (i.e., no command is in more than one group) and
  550. only contain \"in place\" scrolling commands.")
  551.  
  552. ;;;;
  553. ;;;; The variables below this point are internal to this package.
  554. ;;;;
  555.  
  556. (defvar scroll-default-command-group nil
  557.   "The set of \"in place\" scrolling commands that are not members of any
  558. explicitly defined group of commands.  This set of commands is an implicitly
  559. defined group, constructed as \"in place\" commands are invoked, and members of
  560. this group share state among themselves.  See the documentation for the
  561. variable `scroll-command-groups' for more information.")
  562.  
  563. (defvar scroll-initially-displayed-lines 0
  564.   "The number of window lines that contained buffer text when the current
  565. sequence of \"in place\" scrolling commands started.  Unless the variable
  566. `scroll-in-place-allow-blank-lines-past-eob' is true, the \"in place\"
  567. scrolling commands ensure that at least this many text lines are visible at all
  568. times.")
  569.  
  570. (defvar scroll-previous-window nil
  571.   "The window that was most recently scrolled by an \"in place\" scrolling
  572. command.")
  573.  
  574. (defvar scroll-previous-lines 0
  575.   "The number of window lines that the previous \"in place\" scrolling command
  576. attempted to scroll.")
  577.  
  578. (defvar scroll-goal-column 0
  579.   "The desired horizontal window position for point, used by the \"in place\"
  580. scrolling commands.")
  581.  
  582. (defvar scroll-boundary-previous-point nil
  583.   "The value of point before point was moved to a buffer boundary.")
  584.  
  585. (defvar scroll-boundary-previous-lines 0
  586.   "The number of lines that point moved when it moved to a buffer boundary.")
  587.  
  588. (defvar scroll-boundary-error-command nil
  589.   "The value of `this-command' when an \"in place\" scrolling command signalled
  590. a buffer boundary error.  This is used to decide how subsequent scrolling
  591. commands should recover from the error.")
  592.  
  593. (defvar scroll-boundary-error-point nil
  594.   "The value of point when an \"in place\" scrolling command signalled a buffer
  595. boundary error.  This is used to decide how subsequent scrolling commands
  596. should recover from the error."
  597.   ;; This variable is used as a flag, indicating whether or not the previous
  598.   ;; "in place" scrolling command signalled an error.
  599.   )
  600.  
  601. (defvar scroll-window-debt 0
  602.   "The difference between the number of lines an \"in place\" scrolling command
  603. tried to scroll a window and the number of lines that the window actually
  604. scrolled.  This difference is the \"debt\" in the window's starting position.
  605. Subsequent \"in place\" scrolling commands try to make up this debt.")
  606.  
  607. (defconst scroll-pos-visible-bug-p
  608.   ;; On September 14, 1993, David Hughes <djh@Harston.CV.COM> told me that
  609.   ;; Lucid GNU Emacs 19.8 had inherited the bug from Epoch... sigh.
  610.   (let ((old-match-data (match-data)))
  611.     (unwind-protect
  612.     (or (and (boundp 'epoch::version)
  613.          (if (string-match "\\`4\\." emacs-version) t nil)
  614.          )
  615.         (and (string-match "Lucid" emacs-version)
  616.          (if (string-match "\\`19\\.8\\." emacs-version) t nil)
  617.          )
  618.         )
  619.       (store-match-data old-match-data)))
  620.   "A flag, set when this version of GNU Emacs has a buggy version of the
  621. function `pos-visible-in-window-p' that returns `nil' when given `(point-max)'
  622. and `(point-max)' is on the last line of the window.  Currently, this flag is
  623. set for all versions of Epoch 4 and for Lucid GNU Emacs 19.8.")
  624.  
  625.  
  626. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  627. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  628. ;;;;
  629. ;;;; Here are the window-choosing auxiliary functions used by the new scrolling
  630. ;;;; commands.
  631. ;;;;
  632. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  633. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  634.  
  635. (defun scroll-choose-window ()
  636.   "Choose the window to be scrolled by the commands `scroll-down', `scroll-up',
  637. `scroll-down-in-place', and `scroll-up-in-place'.
  638.  
  639. The rules are simple.  If the selected window is not a minibuffer window, then
  640. just choose the selected window.
  641.  
  642. However, when a minibuffer window is selected, look first for the `minibuffer-
  643. scroll-window'.  The `minibuffer-scroll-window' is usually the window that
  644. displays completions.  If it exists, choose it; otherwise choose the next
  645. window after the selected window in the canonical ordering of windows.  The
  646. next window is generally the one below the selected window, or the one at the
  647. top of the screen if the selected window is at the bottom of the screen."
  648.   (let ((selected-window (selected-window)))
  649.     (if (window-minibuffer-p selected-window)
  650.     ;; A minibuffer window is selected --- scroll some other window.
  651.     (if (window-live-p minibuffer-scroll-window)
  652.         minibuffer-scroll-window
  653.       ;; We know that the (selected) minibuffer is active, so `next-window'
  654.       ;; will examine all of the frames that share this minibuffer.
  655.       ;; Should we consider `other-window-scroll-buffer' here?  I don't
  656.       ;; believe so.
  657.       (next-window selected-window))
  658.       selected-window)))
  659.  
  660. ;;;
  661. ;;;
  662. ;;;
  663.  
  664. (defun scroll-choose-other-window ()
  665.   "Choose the window to be scrolled by the commands `scroll-other-window-down',
  666. `scroll-other-window', `scroll-other-window-down-in-place', and `scroll-other-
  667. window-in-place'.
  668.  
  669. The rules are these.  If the selected window is not a minibuffer window, then
  670. choose either:
  671.  
  672.   + a window that displays the `other-window-scroll-buffer', if that buffer
  673.     exists.  Note, this function will display that buffer if necessary.
  674.  
  675.   + the next window after the selected window in the canonical ordering of
  676.     windows.  The next window is generally the one below the selected window,
  677.     or the one at the top of the screen if the selected window is at the bottom
  678.     of the screen.
  679.  
  680. However, when a minibuffer window is selected, look first for the `minibuffer-
  681. scroll-window'.  The `minibuffer-scroll-window' is usually the window that
  682. displays completions.  If it exists, choose it; otherwise choose the window to
  683. be scrolled as described above (`other-window-scroll-buffer' or next window).
  684.  
  685. This function is essentially a Lisp version of the function `other-window-for-
  686. scrolling' which first appeared in the FSF's GNU Emacs 19.26."
  687.   (let* ((no-error nil)
  688.      (selected-window (selected-window))
  689.      (other-window nil))
  690.     (setq other-window
  691.       (cond ((and (window-minibuffer-p selected-window)
  692.               (window-live-p minibuffer-scroll-window))
  693.          ;; Don't signal an error when `minibuffer-scroll-window' is
  694.          ;; the minibuffer itself --- which would be really weird, but
  695.          ;; isn't necessarily erroneous.
  696.          (setq no-error t)
  697.          minibuffer-scroll-window)
  698.         
  699.         ((and ;; `other-window-scroll-buffer' is an Emacs 19 invention.
  700.               (boundp 'other-window-scroll-buffer)
  701.               (bufferp other-window-scroll-buffer)
  702.               ;; `buffer-name' is `nil' if the buffer has been killed.
  703.               (buffer-name other-window-scroll-buffer))
  704.          ;; This is what FSF GNU Emacs 19.26 does, but it occurred to
  705.          ;; me: what if one of these forms returns the selected window?
  706.          ;; Signalling an error would be bad news, so I added a flag.
  707.          (setq no-error t)
  708.          (or (get-buffer-window other-window-scroll-buffer)
  709.              (display-buffer other-window-scroll-buffer t)))
  710.         
  711.         ((let ((next-window (next-window selected-window)))
  712.            (if (eq next-window selected-window)
  713.                nil
  714.              next-window)))
  715.         
  716.         (t
  717.          ;; In Emacs 19 (FSF, Lucid, and XEmacs), look for a window on
  718.          ;; another visible frame.  This could be written for
  719.          ;; Epoch, too, I suppose...
  720.          (condition-case nil
  721.              (let ((this-window (next-window selected-window nil t)))
  722.                (while (not (or (eq this-window selected-window)
  723.                        (scroll-choose-window-frame-visible-p
  724.                     this-window)))
  725.              (setq this-window (next-window this-window nil t)))
  726.                this-window)
  727.            ;; In older versions of Emacs, `next-window' didn't accept
  728.            ;; three arguments.  Catch this error and then return the
  729.            ;; selected window --- which will cause another error to be
  730.            ;; signalled later on.
  731.            (wrong-number-of-arguments selected-window))
  732.          )
  733.         ))
  734.     
  735.     (if (and (not no-error)
  736.          (eq selected-window other-window))
  737.     (error "There is no other window."))
  738.     other-window))
  739.  
  740. ;;;
  741. ;;;
  742. ;;;
  743.  
  744. (defun scroll-choose-window-frame-visible-p (window)
  745.   "Return a true value if the frame of the given WINDOW is visible."
  746.   (cond ((fboundp 'window-frame)
  747.      (eq t (frame-visible-p (window-frame window))))
  748.     (t t)))
  749.  
  750.  
  751. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  752. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  753. ;;;;
  754. ;;;; Here are the "in place" scrolling commands (interactive functions) and the
  755. ;;;; replacements for the standard GNU Emacs vertical scrolling commands.
  756. ;;;;
  757. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  758. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  759.  
  760. ;;;;
  761. ;;;; Here are the new scroll "in place" commands.
  762. ;;;;
  763.  
  764. (defun scroll-down-in-place (&optional lines)
  765.   "Scroll the text of the current window downward by LINES lines, leaving point
  766. as close as possible to its current window position (window line and column).
  767. In other words, point is left \"in place\" within the window.  As a special
  768. case, when the current window is a minibuffer window, this command scrolls the
  769. `minibuffer-scroll-window' (which is usually the list of completions) if it
  770. exists, or otherwise the next window in the canonical ordering of windows.
  771.  
  772. If the optional argument LINES is `nil', scroll the window by the same amount
  773. it was moved by the immediately previous \"in place\" scrolling command, or by
  774. the value of the variable `scroll-default-lines' (usually almost a windowful)
  775. if the previous command was not an \"in place\" scrolling command (or when that
  776. previous command scrolled some other window, or when other circumstances
  777. prevent the previous scrolling distance from being used).  If LINES is the
  778. symbol `-', then the scrolling distance is determined as if LINES had been
  779. `nil' and then that distance is multiplied by -1.
  780.  
  781. If the window cannot be scrolled by the full distance, point is allowed to
  782. stray from its initial position so that it can move the full number of lines.
  783. If point cannot move the full number of lines, point is moved to the buffer
  784. boundary.  Any immediately subsequent \"in place\" scrolling commands will try
  785. to restore point to its initial window position."
  786.   (interactive "P")
  787.   (scroll-window-in-place (scroll-choose-window) lines -1))
  788.  
  789. ;;;
  790. ;;;
  791. ;;;
  792.  
  793. (defun scroll-up-in-place (&optional lines)
  794.   "Scroll the text of the current window upward by LINES lines, leaving point
  795. as close as possible to its current window position (window line and column).
  796. In other words, point is left \"in place\" within the window.  As a special
  797. case, when the current window is a minibuffer window, this command scrolls the
  798. `minibuffer-scroll-window' (which is usually the list of completions) if it
  799. exists, or otherwise the next window in the canonical ordering of windows.
  800.  
  801. If the optional argument LINES is `nil', scroll the window by the same amount
  802. it was moved by the immediately previous \"in place\" scrolling command, or by
  803. the value of the variable `scroll-default-lines' (usually almost a windowful)
  804. if the previous command was not an \"in place\" scrolling command (or when that
  805. previous command scrolled some other window, or when other circumstances
  806. prevent the previous scrolling distance from being used).  If LINES is the
  807. symbol `-', then the scrolling distance is determined as if LINES had been
  808. `nil' and then that distance is multiplied by -1.
  809.  
  810. If the window cannot be scrolled by the full distance, point is allowed to
  811. stray from its initial position so that it can move the full number of lines.
  812. If point cannot move the full number of lines, point is moved to the buffer
  813. boundary.  Any immediately subsequent \"in place\" scrolling commands will try
  814. to restore point to its initial window position."
  815.   (interactive "P")
  816.   (scroll-window-in-place (scroll-choose-window) lines 1))
  817.  
  818. ;;;
  819. ;;; The command `scroll-other-window-down' first appeared in FSF GNU Emacs
  820. ;;; 19.26.
  821. ;;;
  822.  
  823. (defun scroll-other-window-down-in-place (&optional lines)
  824.   "Scroll the text of the next window downward by LINES lines, leaving point in
  825. that window as close as possible to its current window position (window line
  826. and column).  In other words, point is left \"in place\" within the window.
  827. The next window is generally the one below the current one, or the one at the
  828. top of the screen if the current window is at the bottom of the screen.  In
  829. special circumstances this command will scroll a window other than the next
  830. window.  Read the documentation for the function `scroll-choose-other-window'
  831. for details.
  832.  
  833. If the optional argument LINES is `nil', scroll the window by the same amount
  834. it was moved by the immediately previous \"in place\" scrolling command, or by
  835. the value of the variable `scroll-default-lines' (usually almost a windowful)
  836. if the previous command was not an \"in place\" scrolling command (or when that
  837. previous command scrolled some other window, or when other circumstances
  838. prevent the previous scrolling distance from being used).  If LINES is the
  839. symbol `-', then the scrolling distance is determined as if LINES had been
  840. `nil' and then that distance is multiplied by -1.
  841.  
  842. If the window cannot be scrolled by the full distance, point is allowed to
  843. stray from its initial position so that it can move the full number of lines.
  844. If point cannot move the full number of lines, point is moved to the buffer
  845. boundary.  Any immediately subsequent \"in place\" scrolling commands will try
  846. to restore point to its initial window position.
  847.  
  848. If it is impossible to scroll the text of the window at all (because a buffer
  849. boundary is already visible), this command signals a buffer boundary error.
  850. The error is signalled even if point could otherwise move the full number of
  851. lines."
  852.   (interactive "P")
  853.   (scroll-window-in-place (scroll-choose-other-window) lines -1))
  854.  
  855. ;;;
  856. ;;;
  857. ;;;
  858.  
  859. (defun scroll-other-window-in-place (&optional lines)
  860.   "Scroll the text of the next window upward by LINES lines, leaving point in
  861. that window as close as possible to its current window position (window line
  862. and column).  In other words, point is left \"in place\" within the window.
  863. The next window is generally the one below the current one, or the one at the
  864. top of the screen if the current window is at the bottom of the screen.  In
  865. special circumstances this command will scroll a window other than the next
  866. window.  Read the documentation for the function `scroll-choose-other-window'
  867. for details.
  868.  
  869. If the optional argument LINES is `nil', scroll the window by the same amount
  870. it was moved by the immediately previous \"in place\" scrolling command, or by
  871. the value of the variable `scroll-default-lines' (usually almost a windowful)
  872. if the previous command was not an \"in place\" scrolling command (or when that
  873. previous command scrolled some other window, or when other circumstances
  874. prevent the previous scrolling distance from being used).  If LINES is the
  875. symbol `-', then the scrolling distance is determined as if LINES had been
  876. `nil' and then that distance is multiplied by -1.
  877.  
  878. If the window cannot be scrolled by the full distance, point is allowed to
  879. stray from its initial position so that it can move the full number of lines.
  880. If point cannot move the full number of lines, point is moved to the buffer
  881. boundary.  Any immediately subsequent \"in place\" scrolling commands will try
  882. to restore point to its initial window position.
  883.  
  884. If it is impossible to scroll the text of the window at all (because a buffer
  885. boundary is already visible), this command signals a buffer boundary error.
  886. The error is signalled even if point could otherwise move the full number of
  887. lines."
  888.   (interactive "P")
  889.   (scroll-window-in-place (scroll-choose-other-window) lines 1))
  890.  
  891. ;;;;
  892. ;;;; Here are the replacements for GNU Emacs' standard vertical scrolling
  893. ;;;; commands.
  894. ;;;;
  895.  
  896. (or (fboundp 'original-scroll-down)
  897.     (fset 'original-scroll-down (symbol-function 'scroll-down)))
  898. (or (fboundp 'original-scroll-up)
  899.     (fset 'original-scroll-up (symbol-function 'scroll-up)))
  900. (or (fboundp 'original-scroll-other-window-down)
  901.     ;; `scroll-other-window-down' first appeared in FSF GNU Emacs 19.26.
  902.     (if (fboundp 'scroll-other-window-down)
  903.     (fset 'original-scroll-other-window-down
  904.           (symbol-function 'scroll-other-window-down))
  905.       ))
  906. (or (fboundp 'original-scroll-other-window)
  907.     (fset 'original-scroll-other-window (symbol-function 'scroll-other-window))
  908.     )
  909.  
  910. ;;;
  911. ;;;
  912. ;;;
  913.  
  914. (defun scroll-down (&optional lines)
  915.   "Scroll the text of the current window downward by LINES lines.  As a special
  916. case, when the current window is a minibuffer window, this command scrolls the
  917. `minibuffer-scroll-window' (which is usually the list of completions) if it
  918. exists, or otherwise the next window in the canonical ordering of windows.
  919.  
  920. The argument LINES is optional.  Its meaning depends on the current value of
  921. the variable `scroll-in-place'.
  922.  
  923. When the variable `scroll-in-place' is true, this command works just like the
  924. command `scroll-down-in-place', scrolling the current window and leaving point
  925. \"in place\" within the window.  See the documentation for the command
  926. `scroll-down-in-place' for more information.
  927.  
  928. When the variable `scroll-in-place' is `nil' this command invokes the standard
  929. GNU Emacs version of `scroll-down'.  In that case, when LINES is `nil' the
  930. current window is scrolled by nearly a complete windowful of text.
  931.  
  932. Note that this command correctly handles cases in which `scroll-in-place' has a
  933. buffer-local value in the window to be scrolled.  That value is honored."
  934.   (interactive "P")
  935.   (scroll-window (scroll-choose-window) lines -1))
  936.  
  937. ;;;
  938. ;;;
  939. ;;;
  940.  
  941. (defun scroll-up (&optional lines)
  942.   "Scroll the text of the current window upward by LINES lines.  As a special
  943. case, when the current window is a minibuffer window, this command scrolls the
  944. `minibuffer-scroll-window' (which is usually the list of completions) if it
  945. exists, or otherwise the next window in the canonical ordering of windows.
  946.  
  947. The argument LINES is optional.  Its meaning depends on the current value of
  948. the variable `scroll-in-place'.
  949.  
  950. When the variable `scroll-in-place' is true, this command works just like the
  951. command `scroll-up-in-place', scrolling the current window and leaving point
  952. \"in place\" within the window.  See the documentation for the command
  953. `scroll-up-in-place' for more information.
  954.  
  955. When the variable `scroll-in-place' is `nil' this command invokes the standard
  956. GNU Emacs version of `scroll-up'.  In that case, when LINES is `nil' the
  957. current window is scrolled by nearly a complete windowful of text.
  958.  
  959. Note that this command correctly handles cases in which `scroll-in-place' has a
  960. buffer-local value in the window to be scrolled.  That value is honored."
  961.   (interactive "P")
  962.   (scroll-window (scroll-choose-window) lines 1))
  963.  
  964. ;;;
  965. ;;; NOTE that in the FSF GNU Emacs 19.26 version of `scroll-other-window-down',
  966. ;;; the `lines' argument is required.  I've left it optional in order to be
  967. ;;; like `scroll-other-window'.
  968. ;;;
  969.  
  970. (defun scroll-other-window-down (&optional lines)
  971.   "Scroll the text of the next window downward by LINES lines.  The next window
  972. is generally the one below the current one, or the one at the top of the screen
  973. if the current window is at the bottom of the screen.  In special circumstances
  974. this command will scroll a window other than the next window.  Read the
  975. documentation for the function `scroll-choose-other-window' for details.
  976.  
  977. The argument LINES is optional.  Its meaning depends on the current value of
  978. the variable `scroll-in-place'.
  979.  
  980. When the variable `scroll-in-place' is true, this command works just like the
  981. command `scroll-other-window-down-in-place', scrolling the next window and
  982. leaving point \"in place\" within that window.  See the documentation for the
  983. command `scroll-other-window-down-in-place' for more information.
  984.  
  985. When the variable `scroll-in-place' is `nil' this command invokes the standard
  986. GNU Emacs version of `scroll-other-window-down'.  In that case, when LINES is
  987. `nil' the next window is scrolled by nearly a complete windowful of text.
  988. \(Note that `scroll-other-window-down' first appeared as a standard command in
  989. the FSF's GNU Emacs 19.26.  If the builtin version of that command is not
  990. available in the current Emacs system, an equivalent action is invoked
  991. instead.)
  992.  
  993. Note that this command correctly handles cases in which `scroll-in-place' has a
  994. buffer-local value in the window to be scrolled.  That value is honored."
  995.   (interactive "P")
  996.   ;; This code is similar to the body of `scroll-window', below.
  997.   (let* ((other-window (scroll-choose-other-window))
  998.      (other-window-buffer (window-buffer other-window)))
  999.     (if ;; Allow `scroll-in-place' to be a buffer-local variable.
  1000.     (save-excursion (set-buffer other-window-buffer) scroll-in-place)
  1001.     (scroll-window-in-place other-window lines -1)
  1002.       
  1003.       ;; Paranoid, we forcibly break any running sequence of "in place"
  1004.       ;; scrolling commands.
  1005.       (setq scroll-previous-window nil)
  1006.       ;; For XEmacs and Lucid GNU Emacs, preserve the region's state.
  1007.       (if (boundp 'zmacs-region-stays)
  1008.       (setq zmacs-region-stays t))
  1009.       (if (fboundp 'original-scroll-other-window-down)
  1010.       (original-scroll-other-window-down lines)
  1011.     ;; `scroll-other-window-down' first appeared as a builtin in FSF GNU
  1012.     ;; Emacs 19.26, so it may not be available in the current Emacs system.
  1013.     ;; Do the equivalent thing.
  1014.     (original-scroll-other-window (cond
  1015.                        ((null lines) '-)
  1016.                        ((eq lines '-) nil)
  1017.                        (t (- (prefix-numeric-value lines)))
  1018.                        ))
  1019.     ))
  1020.     ))
  1021.  
  1022. ;;;
  1023. ;;;
  1024. ;;;
  1025.  
  1026. (defun scroll-other-window (&optional lines)
  1027.   "Scroll the text of the next window upward by LINES lines.  The next window
  1028. is generally the one below the current one, or the one at the top of the screen
  1029. if the current window is at the bottom of the screen.  In special circumstances
  1030. this command will scroll a window other than the next window.  Read the
  1031. documentation for the function `scroll-choose-other-window' for details.
  1032.  
  1033. The argument LINES is optional.  Its meaning depends on the current value of
  1034. the variable `scroll-in-place'.
  1035.  
  1036. When the variable `scroll-in-place' is true, this command works just like the
  1037. command `scroll-other-window-in-place', scrolling the next window and leaving
  1038. point \"in place\" within that window.  See the documentation for the command
  1039. `scroll-other-window-in-place' for more information.
  1040.  
  1041. When the variable `scroll-in-place' is `nil' this command invokes the standard
  1042. GNU Emacs version of `scroll-other-window'.  In that case, when LINES is `nil'
  1043. the next window is scrolled by nearly a complete windowful of text.
  1044.  
  1045. Note that this command correctly handles cases in which `scroll-in-place' has a
  1046. buffer-local value in the window to be scrolled.  That value is honored."
  1047.   (interactive "P")
  1048.   ;; This code is similar to the body of `scroll-window', below.
  1049.   (let* ((other-window (scroll-choose-other-window))
  1050.      (other-window-buffer (window-buffer other-window)))
  1051.     (if ;; Allow `scroll-in-place' to be a buffer-local variable.
  1052.     (save-excursion (set-buffer other-window-buffer) scroll-in-place)
  1053.     (scroll-window-in-place other-window lines 1)
  1054.       
  1055.       ;; Paranoid, we forcibly break any running sequence of "in place"
  1056.       ;; scrolling commands.
  1057.       (setq scroll-previous-window nil)
  1058.       ;; For XEmacs and Lucid GNU Emacs, preserve the region's state.
  1059.       (if (boundp 'zmacs-region-stays)
  1060.       (setq zmacs-region-stays t))
  1061.       (original-scroll-other-window lines))
  1062.     ))
  1063.  
  1064.  
  1065. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1066. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1067. ;;;;
  1068. ;;;; Here are the new functions `scroll-window-in-place', `scroll-window', and
  1069. ;;;; `scroll-window-in-place-continue-sequence'.  These functions are intended
  1070. ;;;; to be available to programs outside this package.
  1071. ;;;;
  1072. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1073. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1074.  
  1075. (defun scroll-window-in-place (window lines direction)
  1076.   "Scroll WINDOW vertically by the given number of window LINES in the given
  1077. DIRECTION, leaving the window's point as close as possible to its original
  1078. window position (window line and column).  In other words, the window's point
  1079. is left \"in place\" within the window.
  1080.  
  1081. Note that the window to be scrolled does not have to be the selected window,
  1082. and that this function does not change which window is selected.
  1083.  
  1084. LINES specifies the number of window lines to scroll and is interpreted as if
  1085. it were a raw prefix argument.  If LINES is `nil', the window is scrolled by
  1086. the amount it was moved by the immediately previous \"in place\" scrolling
  1087. command, or by the value of the variable `scroll-default-lines' (by default,
  1088. almost a windowful) if the previous command was not an \"in place\" scrolling
  1089. command (or when WINDOW is not the previously scrolled window, or when the
  1090. value of `this-command' is not in the same group as the previous scrolling
  1091. command (see the documentation for the variable `scroll-command-groups'), or
  1092. when other circumstances prevent the previous scrolling distance from being
  1093. used).  If LINES is the symbol `-', then the scrolling distance is determined
  1094. as if LINES had been `nil' and then that distance is multiplied by -1.
  1095.  
  1096. DIRECTION determines the direction of the scrolling motion.  The values -1 and
  1097. `down' indicate downward motion; the values 1 and `up' indicate upward motion.
  1098. Any other value causes an error.
  1099.  
  1100. If the window cannot be scrolled by the full distance (because the window hits
  1101. the boundary of its buffer), the window's point is allowed to stray from its
  1102. initial position so that it can move the full number of lines.  If point cannot
  1103. move the full number of lines, point is moved to the buffer boundary (unless it
  1104. was already there, in which case a buffer boundary error is signalled instead).
  1105. Any immediately subsequent \"in place\" scrolling commands will try to restore
  1106. point to its initial window position.
  1107.  
  1108. Unless the variable `scroll-allow-blank-lines-past-eob' is true, this function
  1109. avoids displaying blank lines past the end of the buffer except as necessary to
  1110. make a previous \"in place\" scrolling action reversible.  Effectively, this
  1111. means that this function will not display any more past-end-of-buffer blank
  1112. lines than were visible when the current sequence of \"in place\" scrolling
  1113. commands started.  When the variable `scroll-allow-blank-lines-past-eob' is
  1114. true, this function will display as many blank lines as is necessary to keep
  1115. point \"in place\" in the window.
  1116.  
  1117. Note that if WINDOW is not the selected window and it is impossible to scroll
  1118. the text of WINDOW at all (because a buffer boundary is already visible), then
  1119. this function signals a buffer boundary error.  The error is signalled even if
  1120. point could otherwise move the full number of lines."
  1121.   (let* (;; Make sure that the user doesn't quit in the middle and leave us
  1122.      ;; with our variables out of sync.
  1123.      (inhibit-quit t)
  1124.      (original-window (selected-window))
  1125.      (original-buffer (current-buffer))
  1126.      (window-height (- (window-height window)
  1127.                (if (window-minibuffer-p window)
  1128.                    0 1)))
  1129.      (this-command-group (scroll-get-command-group this-command))
  1130.      (continue-scroll-p
  1131.       (and ;; We're scrolling the previously scrolled window...
  1132.            (windowp scroll-previous-window)
  1133.            (eq window scroll-previous-window)
  1134.            ;; ...and the last command was an "in place" scrolling command
  1135.            ;; that can be continued by this command.
  1136.            (if (eq last-command t)
  1137.            ;; If the previous command signalled an error, the value of
  1138.            ;; `last-command' is `t'.  Try to see if we signalled the
  1139.            ;; error and if point is where we left it.  (NOTE that FSF
  1140.            ;; GNU Emacs 19.23+ no longer sets `last-command' to `t'
  1141.            ;; when a command signals an error.  This is OK because the
  1142.            ;; else part of this `if' does the appropriate thing.)
  1143.            (and    scroll-boundary-error-point
  1144.             (eq (window-point window) scroll-boundary-error-point)
  1145.             (memq scroll-boundary-error-command this-command-group)
  1146.             )
  1147.          ;; Otherwise...
  1148.          (memq last-command this-command-group))
  1149.            ))
  1150.      (lines-value (prefix-numeric-value lines))
  1151.      )
  1152.     
  1153.     ;; For XEmacs and Lucid GNU Emacs, preserve the region's state.  Note that
  1154.     ;; these Emacsen will forcibly deactivate the region if we signal an error
  1155.     ;; later on.  Is this bad?
  1156.     (if (boundp 'zmacs-region-stays)
  1157.     (setq zmacs-region-stays t))
  1158.     ;; Parse the direction into a unit distance (1 or -1).
  1159.     (setq direction (scroll-parse-direction direction))
  1160.     
  1161.     (setq scroll-previous-window window
  1162.       ;; `(setq scroll-boundary-error-command nil)' is not necessary.
  1163.       scroll-boundary-error-point nil)
  1164.     (unwind-protect
  1165.     (progn
  1166.       ;; `select-window' does an implicit `set-buffer'.
  1167.       (select-window window)
  1168.       
  1169.       (if (or ;; The current command is not a continuation of a running
  1170.           ;; sequence of "in place" scrolling commands...
  1171.           (not continue-scroll-p)
  1172.           ;; ...or we were given an explicit number of lines to scroll,
  1173.           ;; and that number has a different magnitude than the last
  1174.           ;; number of lines we scrolled...
  1175.               (and (or (numberp lines) (consp lines))
  1176.                (/= scroll-previous-lines lines-value)
  1177.                (/= scroll-previous-lines (- lines-value)))
  1178.           ;; ...or the last successful scrolling command moved to a
  1179.           ;; buffer boundary, but the buffer is no longer in the state
  1180.           ;; we left it.  (This can occur if, for example, we signal an
  1181.           ;; end-of-buffer error and something catches it and moves
  1182.           ;; point or renarrows.  VM, for example, does this.)
  1183.           (and scroll-boundary-previous-point
  1184.                (or (not (or (bobp) (eobp)))
  1185.                (< scroll-boundary-previous-point (point-min))
  1186.                (> scroll-boundary-previous-point (point-max))
  1187.                (eq scroll-boundary-previous-point (point)))))
  1188.           
  1189.           ;; We're starting a new sequence of scrolling commands.
  1190.           (setq lines (if (or (numberp lines) (consp lines))
  1191.                   lines-value
  1192.                 ;; The default number of lines...
  1193.                 (* (if (eq lines '-) -1 1)
  1194.                    (if (numberp scroll-default-lines)
  1195.                    scroll-default-lines
  1196.                  (max (- window-height
  1197.                      next-screen-context-lines)
  1198.                       1))))
  1199.             scroll-previous-lines lines
  1200.             scroll-goal-column (scroll-determine-goal-column window)
  1201.             scroll-boundary-previous-point nil
  1202.             ;; `(setq scroll-boundary-previous-lines 0)' is not
  1203.             ;; necessary.
  1204.             scroll-window-debt 0
  1205.             scroll-initially-displayed-lines
  1206.             (if scroll-allow-blank-lines-past-eob
  1207.             0
  1208.               (save-excursion
  1209.             (goto-char (window-start window))
  1210.             (vertical-motion (1- window-height)))))
  1211.         
  1212.         ;; Otherwise we want to scroll by the same number of lines (but
  1213.         ;; possibly in a different direction) that we scrolled in previous
  1214.         ;; invocations of this function.
  1215.         (cond ((null lines)
  1216.            (setq lines scroll-previous-lines))
  1217.           ((eq lines '-)
  1218.            (setq lines (- scroll-previous-lines)
  1219.              scroll-previous-lines lines))
  1220.           (t
  1221.            (setq lines lines-value
  1222.              scroll-previous-lines lines)))
  1223.         )
  1224.       
  1225.       (setq lines (* direction lines))
  1226.       
  1227.       ;; If point is not in the window, center window around point.  We try
  1228.       ;; to account for a bug in `pos-visible-in-window-p' in some versions
  1229.       ;; of Emacs (see `scroll-pos-visible-bug-p', above).
  1230.       (save-excursion
  1231.         (if (pos-visible-in-window-p (let ((point (point)))
  1232.                        (if (and scroll-pos-visible-bug-p
  1233.                             (= point (point-max)))
  1234.                            (max (1- point) (point-min))
  1235.                          point))
  1236.                      window)
  1237.         nil
  1238.           (vertical-motion (/ (- window-height) 2))
  1239.           (set-window-start window (point))))
  1240.       
  1241.       (cond ((and scroll-boundary-previous-point
  1242.               ;; `lines' is the same sign as the direction from point
  1243.               ;; to the `scroll-boundary-previous-point'.
  1244.               (cond ((> lines 0)
  1245.                  (> (- scroll-boundary-previous-point (point)) 0))
  1246.                 ((< lines 0)
  1247.                  (< (- scroll-boundary-previous-point (point)) 0))
  1248.                 (t nil)))
  1249.          ;; We're moving away from the buffer boundary.
  1250.          (goto-char scroll-boundary-previous-point)
  1251.          ;; Always move here (i.e., don't reject cases in which the
  1252.          ;; window doesn't move).
  1253.          (scroll-set-window-start window
  1254.                       (- scroll-boundary-previous-lines))
  1255.          ;; (message "Back, window debt is %s." scroll-window-debt)
  1256.          (setq scroll-boundary-previous-point nil))
  1257.  
  1258.         ((= lines 0)
  1259.          ;; We're going nowhere, so save ourselves some work.
  1260.          ;; (message "Scrolled zero lines.")
  1261.          )
  1262.         
  1263.         (t
  1264.          ;; Perform the scrolling motion.
  1265.          (let ((initial-point (point))
  1266.                (moved nil))
  1267.            ;; First move point and see how far it goes.
  1268.            (setq moved (vertical-motion lines))
  1269.            (if (= moved lines)
  1270.                (progn
  1271.              ;; Point moved the full distance.  Move to the desired
  1272.              ;; column and then try to move the window the full
  1273.              ;; distance, too.
  1274.              (move-to-column (+ (current-column)
  1275.                         scroll-goal-column))
  1276.              (or (scroll-set-window-start window moved
  1277.                               original-window)
  1278.                  (scroll-signal-boundary-error initial-point
  1279.                                lines))
  1280.              ;; (message "Normal, window debt is %s."
  1281.              ;;          scroll-window-debt)
  1282.              )
  1283.              ;; Point couldn't move all the way.  Move to the buffer
  1284.              ;; boundary if we're not already there, or signal a buffer
  1285.              ;; boundary error otherwise.
  1286.              (let ((boundary-point (if (< lines 0)
  1287.                            (point-min)
  1288.                          (point-max)))
  1289.                (boundary-symbol (if (< lines 0)
  1290.                         'beginning-of-buffer
  1291.                           'end-of-buffer)))
  1292.                (if (= initial-point boundary-point)
  1293.                (scroll-signal-boundary-error initial-point lines)
  1294.              ;; Scroll the window by as many lines as point could
  1295.              ;; move.
  1296.              (or (scroll-set-window-start window moved
  1297.                               original-window)
  1298.                  (scroll-signal-boundary-error initial-point
  1299.                                lines))
  1300.              (message "%s" (get boundary-symbol 'error-message))
  1301.              ;; (message "Boundary, window debt is %s."
  1302.              ;;          scroll-window-debt)
  1303.              (setq scroll-boundary-previous-lines moved)
  1304.              (setq scroll-boundary-previous-point initial-point)
  1305.              (goto-char boundary-point))
  1306.                )))
  1307.          )))
  1308.       
  1309.       ;; The unwind forms of the `unwind-protect', above.  Restore the
  1310.       ;; originally selected window and current buffer.
  1311.       (select-window original-window)
  1312.       (set-buffer original-buffer)))
  1313.   
  1314.   ;; The standard GNU Emacs scrolling commands return `nil' so we do, too.
  1315.   nil)
  1316.  
  1317. ;;;
  1318. ;;;
  1319. ;;;
  1320.  
  1321. (defun scroll-window (window lines direction)
  1322.   "Scroll WINDOW vertically by the given number of window LINES in the given
  1323. DIRECTION.  Note that the window to be scrolled does not have to be the
  1324. selected window, and that this function does not change which window is
  1325. selected.
  1326.  
  1327. When the variable `scroll-in-place' is true, this function simply invokes the
  1328. function `scroll-window-in-place' to scroll the window and leave point \"in
  1329. place\" within that window.  See the documentation for `scroll-window-in-place'
  1330. for more information.
  1331.  
  1332. When the variable `scroll-in-place' is `nil' this function invokes the original
  1333. version of the standard GNU Emacs command `scroll-down' or `scroll-up', as
  1334. determined by DIRECTION, to scroll the window.  If DIRECTION is -1 or `down',
  1335. the original `scroll-down' is called; if DIRECTION is 1 or `up', the original
  1336. `scroll-up' is called.  Any other DIRECTION is an error.  LINES is interpreted
  1337. as if it were a raw prefix argument.  If LINES is `nil', the window is scrolled
  1338. by almost a complete windowful.  If LINES is the symbol `-', the window is
  1339. scrolled by almost a complete windowful in the opposite direction.
  1340.  
  1341. Note that this function correctly handles cases in which `scroll-in-place' has
  1342. a buffer-local value in the WINDOW's buffer.  That value is honored."
  1343.   (let ((current-buffer (current-buffer))
  1344.     (selected-window (selected-window))
  1345.     (window-buffer (window-buffer window)))
  1346.     (if ;; Allow `scroll-in-place' to be a buffer-local variable.
  1347.     (if (eq current-buffer window-buffer)
  1348.         scroll-in-place
  1349.       (save-excursion (set-buffer window-buffer) scroll-in-place))
  1350.     (scroll-window-in-place window lines direction)
  1351.       
  1352.       (unwind-protect
  1353.       (progn
  1354.         ;; Paranoid, we forcibly break any running sequence of "in place"
  1355.         ;; scrolling commands.
  1356.         (setq scroll-previous-window nil)
  1357.         ;; For XEmacs and Lucid GNU Emacs, preserve the region's state.
  1358.         (if (boundp 'zmacs-region-stays)
  1359.         (setq zmacs-region-stays t))
  1360.         (select-window window)
  1361.         (if (= (scroll-parse-direction direction) 1)
  1362.         (original-scroll-up lines)
  1363.           (original-scroll-down lines)))
  1364.     (select-window selected-window)
  1365.     (set-buffer current-buffer))
  1366.       )))
  1367.  
  1368. ;;;
  1369. ;;; The following function is sometimes useful.  For example, I call it from
  1370. ;;; functions that are invoked by certain mouse button down events in order to
  1371. ;;; preserve any running chain of "in place" scrolling commands.  This lets me
  1372. ;;; continue the sequence from my mouse button up functions.
  1373. ;;;
  1374. ;;; I haven't yet needed a function to purposely break a running sequence of
  1375. ;;; "in place" scrolling commands.  Such a function would be easy to write,
  1376. ;;; however; just set the variable `scroll-previous-window' to `nil'.
  1377. ;;;
  1378.  
  1379. (defun scroll-window-in-place-continue-sequence ()
  1380.   "If the previous command was a \"scroll in place\" command, set the variable
  1381. `this-command' to the name of that previous command.  This ensures that any
  1382. running sequence of \"in place\" scrolling commands will not be broken by the
  1383. current command.  See the documentation for the commands `scroll-down-in-place'
  1384. and `scroll-up-in-place' for more information about \"in place\" scrolling.
  1385.  
  1386. NOTE that you don't need to call this function if the current command scrolls
  1387. in place!  You only need to call this function when the current command is not
  1388. a \"scroll in place\" command but you still want to preserve any running
  1389. sequence of \"in place\" commands.  Such situations are rare.
  1390.  
  1391. NOTE that this function sets `this-command' in order to trick the \"in place\"
  1392. scrolling commands.  If something else subsequently sets `this-command', any
  1393. running sequence of scrolling commands will probably be broken anyway."
  1394.   (if (if (eq last-command t)
  1395.       ;; If `last-command' is `t', then the previous command signalled an
  1396.       ;; error.  See if the last invocation of `scroll-window-in-place'
  1397.       ;; signalled an error.  (NOTE that FSF GNU Emacs 19.23+ no longer
  1398.       ;; sets `last-command' to `t' when a command signals an error.  This
  1399.       ;; is OK because the else part of this `if' does the appropriate
  1400.       ;; thing.)
  1401.       scroll-boundary-error-point
  1402.     ;; Otherwise, the value of `last-command' must belong to some group of
  1403.     ;; "in place" scrolling commands.
  1404.     (or (memq last-command scroll-default-command-group)
  1405.         (let ((groups scroll-command-groups)
  1406.           (found nil))
  1407.           (while (and groups (not found))
  1408.         (if (memq last-command (car groups))
  1409.             (setq found t)
  1410.           (setq groups (cdr groups)))
  1411.         )
  1412.           found)))
  1413.       (setq this-command last-command)))
  1414.  
  1415.  
  1416. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1417. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1418. ;;;;
  1419. ;;;; Here are the various auxiliary functions called by the function `scroll-
  1420. ;;;; window-in-place'.  None of the functions are intended to be called from
  1421. ;;;; outside this package.
  1422. ;;;;
  1423. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1424. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1425.  
  1426. (defun scroll-get-command-group (command)
  1427.   "Return the group of \"in place\" scrolling commands that contains the given
  1428. COMMAND.  This is the list of commands with which the given command may share
  1429. state and form \"chains.\"
  1430.  
  1431. This function is an auxiliary for the function `scroll-window-in-place'.  Don't
  1432. call this function from other code."
  1433.   ;; This function assumes that the given command is an "in place" scrolling
  1434.   ;; command.
  1435.   (let ((groups scroll-command-groups)
  1436.     (found nil))
  1437.     (while (and groups (not found))
  1438.       (if (memq command (car groups))
  1439.       (setq found t)
  1440.     (setq groups (cdr groups)))
  1441.       )
  1442.     (if groups
  1443.     (car groups)
  1444.       ;; Otherwise return the default command group.  If necessary, add the
  1445.       ;; given command to the default command group.
  1446.       (or (memq command scroll-default-command-group)
  1447.       (setq scroll-default-command-group
  1448.         (cons command scroll-default-command-group)))
  1449.       scroll-default-command-group)
  1450.     ))
  1451.  
  1452. ;;;
  1453. ;;;
  1454. ;;;
  1455.  
  1456. (defun scroll-parse-direction (direction)
  1457.   "Return the signed unit distance for the given DIRECTION.  If DIRECTION is
  1458. unacceptable, signal an error."
  1459.   (cond ((or (eq direction 1) (eq direction -1)) direction)
  1460.     ((eq direction 'up) 1)
  1461.     ((eq direction 'down) -1)
  1462.     (t (signal 'args-out-of-range (list 'direction direction)))
  1463.     ))
  1464.  
  1465. ;;;
  1466. ;;;
  1467. ;;;
  1468.  
  1469. (defun scroll-determine-goal-column (window)
  1470.   "Return the goal column for the \"in place\" vertical scrolling commands.
  1471. This is the horizontal window position at which these commands try to keep
  1472. point.
  1473.  
  1474. This function is an auxiliary for the function `scroll-window-in-place'.  Don't
  1475. call this function from other code."
  1476.   ;; NOTE that `window' must be the selected window!  `scroll-window-in-place'
  1477.   ;; ensures that this is so.
  1478.   (cond ((or truncate-lines
  1479.          (and truncate-partial-width-windows
  1480.           (< (window-width window) (screen-width)))
  1481.          (> (window-hscroll window) 0))
  1482.      ;; Lines in this window are being truncated.
  1483.      (if (and track-eol (eolp))
  1484.          9999
  1485.        (current-column)))
  1486.     ((and track-eol (eolp))
  1487.      ;; In some ways this isn't quite right, as point doesn't track the
  1488.      ;; ends of wrapped lines.  But if it did so, point would be on the
  1489.      ;; wrong window line.  This is the best we can do.
  1490.      (1- (window-width window)))
  1491.     (t (% (current-column) (1- (window-width window))))
  1492.     ))
  1493.  
  1494. ;;;
  1495. ;;;
  1496. ;;;
  1497.  
  1498. (defun scroll-set-window-start (window lines &optional original-window)
  1499.   "Move the `window-start' of the given window, which must be the selected
  1500. window.  If the window was successfully scrolled, update the variable
  1501. `scroll-window-debt' and return `t'.  Otherwise return `nil'.
  1502.  
  1503. This function is an auxiliary for the function `scroll-window-in-place'.  Don't
  1504. call this function from other code."
  1505.   (save-excursion
  1506.     (goto-char (window-start window))
  1507.     ;; Try to move the window start by the specified number of lines.  In
  1508.     ;; addition, try to make up any existing debt in the window start's
  1509.     ;; position and make sure that we don't move too close to the end of the
  1510.     ;; buffer.
  1511.     (let ((moved (+ (vertical-motion (+ lines
  1512.                     scroll-window-debt
  1513.                     scroll-initially-displayed-lines))
  1514.             (vertical-motion (- scroll-initially-displayed-lines)))))
  1515.       ;; If we're not scrolling the `original-window' (i.e., the originally
  1516.       ;; selected window), punt if we didn't move the window start at all.
  1517.       (if (and original-window
  1518.            (not (eq window original-window))
  1519.            (= moved 0))
  1520.       nil
  1521.     ;; Otherwise update the window start and keep track of the debt in our
  1522.     ;; position.  Return `t' to indicate success.
  1523.     (set-window-start window (point))
  1524.     (setq scroll-window-debt (- (+ lines scroll-window-debt) moved))
  1525.     t))
  1526.     ))
  1527.  
  1528. ;;;
  1529. ;;;
  1530. ;;;
  1531.  
  1532. (defun scroll-signal-boundary-error (initial-point lines)
  1533.   "Move point to its initial location and signal an appropriate buffer boundary
  1534. error.
  1535.  
  1536. This function is an auxiliary for the function `scroll-window-in-place'.  Don't
  1537. call this function from other code."
  1538.   (goto-char initial-point)
  1539.   ;; Remember what we were doing and where point was when we signalled the
  1540.   ;; error so that subsequent "in place" scrolling commands can decide how to
  1541.   ;; recover.
  1542.   (setq scroll-boundary-error-command this-command
  1543.     scroll-boundary-error-point initial-point)
  1544.   (signal (if (< lines 0) 'beginning-of-buffer 'end-of-buffer)
  1545.       nil))
  1546.  
  1547.  
  1548. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1549. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1550. ;;;;
  1551. ;;;; Finally, here is the `provide' statement.
  1552. ;;;;
  1553. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1554. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1555.  
  1556. (provide 'scroll-in-place)
  1557.  
  1558. ;; End of file.
  1559.  
  1560.