scroll-lock.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; scroll-lock.el --- Scroll lock scrolling.
  2. ;; Copyright (C) 2005-2012 Free Software Foundation, Inc.
  3. ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
  4. ;; Maintainer: FSF
  5. ;; Created: 2005-06-18
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; By activating Scroll Lock mode, keys for moving point by line or
  19. ;; paragraph will scroll the buffer by the respective amount of lines
  20. ;; instead. Point will be kept vertically fixed relative to window
  21. ;; boundaries.
  22. ;;; Code:
  23. (defvar scroll-lock-mode-map
  24. (let ((map (make-sparse-keymap)))
  25. (define-key map [remap next-line] 'scroll-lock-next-line)
  26. (define-key map [remap previous-line] 'scroll-lock-previous-line)
  27. (define-key map [remap forward-paragraph] 'scroll-lock-forward-paragraph)
  28. (define-key map [remap backward-paragraph] 'scroll-lock-backward-paragraph)
  29. map)
  30. "Keymap for Scroll Lock mode.")
  31. (defvar scroll-lock-preserve-screen-pos-save scroll-preserve-screen-position
  32. "Used for saving the state of `scroll-preserve-screen-position'.")
  33. (make-variable-buffer-local 'scroll-lock-preserve-screen-pos-save)
  34. (defvar scroll-lock-temporary-goal-column 0
  35. "Like `temporary-goal-column' but for scroll-lock-* commands.")
  36. ;;;###autoload
  37. (define-minor-mode scroll-lock-mode
  38. "Buffer-local minor mode for pager-like scrolling.
  39. With a prefix argument ARG, enable the mode if ARG is positive,
  40. and disable it otherwise. If called from Lisp, enable the mode
  41. if ARG is omitted or nil. When enabled, keys that normally move
  42. point by line or paragraph will scroll the buffer by the
  43. respective amount of lines instead and point will be kept
  44. vertically fixed relative to window boundaries during scrolling."
  45. :lighter " ScrLck"
  46. :keymap scroll-lock-mode-map
  47. (if scroll-lock-mode
  48. (progn
  49. (setq scroll-lock-preserve-screen-pos-save
  50. scroll-preserve-screen-position)
  51. (set (make-local-variable 'scroll-preserve-screen-position) 'always))
  52. (setq scroll-preserve-screen-position
  53. scroll-lock-preserve-screen-pos-save)))
  54. (defun scroll-lock-update-goal-column ()
  55. "Update `scroll-lock-temporary-goal-column' if necessary."
  56. (unless (memq last-command '(scroll-lock-next-line
  57. scroll-lock-previous-line
  58. scroll-lock-forward-paragraph
  59. scroll-lock-backward-paragraph))
  60. (setq scroll-lock-temporary-goal-column (current-column))))
  61. (defun scroll-lock-move-to-column (column)
  62. "Like `move-to-column' but cater for wrapped lines."
  63. (if (or (bolp)
  64. ;; Start of a screen line.
  65. (not (zerop (mod (- (point) (line-beginning-position))
  66. (window-width)))))
  67. (move-to-column column)
  68. (forward-char (min column (- (line-end-position) (point))))))
  69. (defun scroll-lock-next-line (&optional arg)
  70. "Scroll up ARG lines keeping point fixed."
  71. (interactive "p")
  72. (or arg (setq arg 1))
  73. (scroll-lock-update-goal-column)
  74. (if (pos-visible-in-window-p (point-max))
  75. (forward-line arg)
  76. (scroll-up arg))
  77. (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
  78. (defun scroll-lock-previous-line (&optional arg)
  79. "Scroll up ARG lines keeping point fixed."
  80. (interactive "p")
  81. (or arg (setq arg 1))
  82. (scroll-lock-update-goal-column)
  83. (condition-case nil
  84. (scroll-down arg)
  85. (beginning-of-buffer (forward-line (- arg))))
  86. (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
  87. (defun scroll-lock-forward-paragraph (&optional arg)
  88. "Scroll down ARG paragraphs keeping point fixed."
  89. (interactive "p")
  90. (or arg (setq arg 1))
  91. (scroll-lock-update-goal-column)
  92. (scroll-up (count-screen-lines (point) (save-excursion
  93. (forward-paragraph arg)
  94. (point))))
  95. (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
  96. (defun scroll-lock-backward-paragraph (&optional arg)
  97. "Scroll up ARG paragraphs keeping point fixed."
  98. (interactive "p")
  99. (or arg (setq arg 1))
  100. (scroll-lock-update-goal-column)
  101. (let ((goal (save-excursion (backward-paragraph arg) (point))))
  102. (condition-case nil
  103. (scroll-down (count-screen-lines goal (point)))
  104. (beginning-of-buffer (goto-char goal))))
  105. (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
  106. (provide 'scroll-lock)
  107. ;;; scroll-lock.el ends here