Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / scroll-lock.el
blob123fbb2b37bb7de634d61a2695e9cff458e9ad2d
1 ;;; scroll-lock.el --- Scroll lock scrolling.
3 ;; Copyright (C) 2005-2018 Free Software Foundation, Inc.
5 ;; Author: Ralf Angeli <angeli@iwi.uni-sb.de>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 2005-06-18
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; By activating Scroll Lock mode, keys for moving point by line or
27 ;; paragraph will scroll the buffer by the respective amount of lines
28 ;; instead. Point will be kept vertically fixed relative to window
29 ;; boundaries.
31 ;;; Code:
33 (defvar scroll-lock-mode-map
34 (let ((map (make-sparse-keymap)))
35 (define-key map [remap next-line] 'scroll-lock-next-line)
36 (define-key map [remap previous-line] 'scroll-lock-previous-line)
37 (define-key map [remap forward-paragraph] 'scroll-lock-forward-paragraph)
38 (define-key map [remap backward-paragraph] 'scroll-lock-backward-paragraph)
39 map)
40 "Keymap for Scroll Lock mode.")
42 (defvar scroll-lock-preserve-screen-pos-save scroll-preserve-screen-position
43 "Used for saving the state of `scroll-preserve-screen-position'.")
44 (make-variable-buffer-local 'scroll-lock-preserve-screen-pos-save)
46 (defvar scroll-lock-temporary-goal-column 0
47 "Like `temporary-goal-column' but for scroll-lock-* commands.")
49 ;;;###autoload
50 (define-minor-mode scroll-lock-mode
51 "Buffer-local minor mode for pager-like scrolling.
53 When enabled, keys that normally move point by line or paragraph
54 will scroll the buffer by the respective amount of lines instead
55 and point will be kept vertically fixed relative to window
56 boundaries during scrolling."
57 :lighter " ScrLck"
58 :keymap scroll-lock-mode-map
59 (if scroll-lock-mode
60 (progn
61 (setq scroll-lock-preserve-screen-pos-save
62 scroll-preserve-screen-position)
63 (set (make-local-variable 'scroll-preserve-screen-position) 'always))
64 (setq scroll-preserve-screen-position
65 scroll-lock-preserve-screen-pos-save)))
67 (defun scroll-lock-update-goal-column ()
68 "Update `scroll-lock-temporary-goal-column' if necessary."
69 (unless (memq last-command '(scroll-lock-next-line
70 scroll-lock-previous-line
71 scroll-lock-forward-paragraph
72 scroll-lock-backward-paragraph))
73 (setq scroll-lock-temporary-goal-column (current-column))))
75 (defun scroll-lock-move-to-column (column)
76 "Like `move-to-column' but cater for wrapped lines."
77 (if (or (bolp)
78 ;; Start of a screen line.
79 (not (zerop (mod (- (point) (line-beginning-position))
80 (window-width)))))
81 (move-to-column column)
82 (forward-char (min column (- (line-end-position) (point))))))
84 (defun scroll-lock-next-line (&optional arg)
85 "Scroll up ARG lines keeping point fixed."
86 (interactive "p")
87 (or arg (setq arg 1))
88 (scroll-lock-update-goal-column)
89 (if (pos-visible-in-window-p (point-max))
90 (forward-line arg)
91 (scroll-up arg))
92 (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
94 (defun scroll-lock-previous-line (&optional arg)
95 "Scroll up ARG lines keeping point fixed."
96 (interactive "p")
97 (or arg (setq arg 1))
98 (scroll-lock-update-goal-column)
99 (condition-case nil
100 (scroll-down arg)
101 (beginning-of-buffer (forward-line (- arg))))
102 (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
104 (defun scroll-lock-forward-paragraph (&optional arg)
105 "Scroll down ARG paragraphs keeping point fixed."
106 (interactive "p")
107 (or arg (setq arg 1))
108 (scroll-lock-update-goal-column)
109 (scroll-up (count-screen-lines (point) (save-excursion
110 (forward-paragraph arg)
111 (point))))
112 (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
114 (defun scroll-lock-backward-paragraph (&optional arg)
115 "Scroll up ARG paragraphs keeping point fixed."
116 (interactive "p")
117 (or arg (setq arg 1))
118 (scroll-lock-update-goal-column)
119 (let ((goal (save-excursion (backward-paragraph arg) (point))))
120 (condition-case nil
121 (scroll-down (count-screen-lines goal (point)))
122 (beginning-of-buffer (goto-char goal))))
123 (scroll-lock-move-to-column scroll-lock-temporary-goal-column))
125 (provide 'scroll-lock)
127 ;;; scroll-lock.el ends here