scroll-all.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; scroll-all.el --- scroll all buffers together minor mode
  2. ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Gary D. Foster <Gary.Foster@corp.sun.com>
  4. ;; Keywords: scroll crisp brief lock
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This mode allows multiple buffers to be 'locked' so that scrolling
  18. ;; up or down lines in any buffer causes all the buffers to mirror
  19. ;; the scrolling. It hooks into the post-command-hook to check for
  20. ;; potential scrolling commands and if we're locked, mirrors them in all
  21. ;; windows. This allows us to grab line-at-a-time scrolling as well as
  22. ;; screen-at-a-time scrolling, and doesn't remap any of the keyboard
  23. ;; commands to do it.
  24. ;; You can enable and disable this mode with the 'scroll-all-mode' command.
  25. ;; Suggestions/ideas from:
  26. ;; Rick Macdonald <rickm@vsl.com>
  27. ;; Anders Lindgren <andersl@csd.uu.se>
  28. ;;; Code:
  29. (defun scroll-all-function-all (func arg)
  30. "Apply function FUNC with argument ARG to all visible windows."
  31. (let ((num-windows (count-windows))
  32. (count 1))
  33. (when (> num-windows 1)
  34. (other-window 1)
  35. (while (< count num-windows)
  36. (condition-case nil
  37. (funcall func arg)
  38. ;; Ignore beginning- or end-of-buffer error in other windows.
  39. (error nil)
  40. )
  41. (other-window 1)
  42. (setq count (1+ count))))))
  43. (defun scroll-all-scroll-down-all (arg)
  44. "Scroll down in all visible windows."
  45. (interactive "p")
  46. (scroll-all-function-all 'next-line arg))
  47. (defun scroll-all-scroll-up-all (arg)
  48. "Scroll up in all visible windows."
  49. (interactive "p")
  50. (scroll-all-function-all 'previous-line arg))
  51. (defun scroll-all-page-down-all (arg)
  52. "Page down in all visible windows."
  53. (interactive "P")
  54. (scroll-all-function-all 'scroll-up arg))
  55. (defun scroll-all-page-up-all (arg)
  56. "Page up in all visible windows."
  57. (interactive "P")
  58. (scroll-all-function-all 'scroll-down arg))
  59. (defun scroll-all-beginning-of-buffer-all (arg)
  60. "Go to the beginning of the buffer in all visible windows."
  61. (interactive "P")
  62. (scroll-all-function-all 'beginning-of-buffer arg))
  63. (defun scroll-all-end-of-buffer-all (arg)
  64. "Go to the end of the buffer in all visible windows."
  65. (interactive "P")
  66. (scroll-all-function-all 'end-of-buffer arg))
  67. (defun scroll-all-check-to-scroll ()
  68. "Check `this-command' to see if a scroll is to be done."
  69. (cond ((eq this-command 'next-line)
  70. (call-interactively 'scroll-all-scroll-down-all))
  71. ((eq this-command 'previous-line)
  72. (call-interactively 'scroll-all-scroll-up-all))
  73. ((memq this-command '(scroll-up scroll-up-command))
  74. (call-interactively 'scroll-all-page-down-all))
  75. ((memq this-command '(scroll-down scroll-down-command))
  76. (call-interactively 'scroll-all-page-up-all))
  77. ((eq this-command 'beginning-of-buffer)
  78. (call-interactively 'scroll-all-beginning-of-buffer-all))
  79. ((eq this-command 'end-of-buffer)
  80. (call-interactively 'scroll-all-end-of-buffer-all))))
  81. ;;;###autoload
  82. (define-minor-mode scroll-all-mode
  83. "Toggle shared scrolling in same-frame windows (Scroll-All mode).
  84. With a prefix argument ARG, enable Scroll-All mode if ARG is
  85. positive, and disable it otherwise. If called from Lisp, enable
  86. the mode if ARG is omitted or nil.
  87. When Scroll-All mode is enabled, scrolling commands invoked in
  88. one window apply to all visible windows in the same frame."
  89. nil " *SL*" nil
  90. :global t
  91. :group 'windows
  92. :group 'scrolling
  93. (if scroll-all-mode
  94. (add-hook 'post-command-hook 'scroll-all-check-to-scroll)
  95. (remove-hook 'post-command-hook 'scroll-all-check-to-scroll)))
  96. (provide 'scroll-all)
  97. ;;; scroll-all.el ends here