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