* admin/gitmerge.el (gitmerge-missing):
[emacs.git] / lisp / pixel-scroll.el
blobf64a4392b49a9619ed0bc56966403669603aefd5
1 ;;; pixel-scroll.el --- Scroll a line smoothly
3 ;; Copyright (C) 2017 Free Software Foundation, Inc.
4 ;; Author: Tak Kunihiro <tkk@misasa.okayama-u.ac.jp>
5 ;; Keywords: mouse
6 ;; Package: emacs
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;; Usage:
25 ;; To interactively toggle the mode:
27 ;; M-x pixel-scroll-mode RET
29 ;; To make the mode permanent, put these in your init file:
31 ;; (require 'pixel-scroll)
32 ;; (pixel-scroll-mode 1)
34 ;;; Commentary:
36 ;; This package offers a global minor mode which makes mouse-wheel
37 ;; scroll a line smoothly.
39 ;; Scrolling a line up by `set-window-vscroll' and that by `scroll-up'
40 ;; give similar display as shown below.
42 ;; A: (scroll-up 1)
43 ;; B: (set-window-vscroll nil (frame-char-height) t)
45 ;; Also scrolling a pixel up by `set-window-vscroll' and that by
46 ;; `scroll-up' give similar display, when vscroll is the last pixel of
47 ;; the line, as shown below.
49 ;; A: (scroll-up 1)
50 ;; B: (set-window-vscroll nil (1- (frame-char-height) t)) (scroll-up 1)
52 ;; When point reaches to the top of a window on scroll by
53 ;; `set-window-vscroll', vscroll is set to zero. To scroll a line
54 ;; smoothly and continuously, this package scrolls a line by following
55 ;; sequences.
57 ;; (vertical-motion 1)
58 ;; (dolist (vs (number-sequence 1 (1- (frame-char-height))))
59 ;; (set-window-vscroll nil vs t) (sit-for 0))
60 ;; (scroll-up 1)
62 ;;; Todo:
64 ;; Allowing pixel-level scrolling in Emacs requires a thorough review
65 ;; of the related functionalities, to make sure none of them zeroes
66 ;; out vscroll where users won't want that.
68 ;;; Code:
70 (require 'mwheel)
72 (defvar pixel-wait 0
73 "Idle time on each step of pixel scroll specified in second.
74 More wait will result in slow and gentle scroll.")
76 (defvar pixel-resolution-fine-flag nil
77 "Set scrolling resolution to pixels instead of a line.
78 When it is t, scrolling resolution is number of pixels obtained
79 by `frame-char-height' instead of a line. When it is number,
80 scrolling resolution is set to number of pixels specified. In
81 case you need scrolling resolution of a pixel, set to 1. After a
82 pixel scroll, typing \\[next-line] or \\[previous-line] scrolls the window to make it
83 fully visible, and undoes the effect of the pixel-level scroll.")
85 ;;;###autoload
86 (define-minor-mode pixel-scroll-mode
87 "A minor mode to scroll text pixel-by-pixel.
88 With a prefix argument ARG, enable Pixel Scroll mode if ARG is positive,
89 and disable it otherwise. If called from Lisp, enable Pixel Scroll mode
90 if ARG is omitted or nil."
91 :init-value nil
92 :group 'scrolling
93 :global t
94 :version "26.1"
96 (if pixel-scroll-mode
97 (setq mwheel-scroll-up-function 'pixel-scroll-up
98 mwheel-scroll-down-function 'pixel-scroll-down)
99 (setq mwheel-scroll-up-function 'scroll-up
100 mwheel-scroll-down-function 'scroll-down)))
102 (defun pixel-scroll-up (&optional arg)
103 "Scroll text of selected window up ARG lines.
104 This is an alternative of `scroll-up'. Scope moves downward."
105 (interactive)
106 (or arg (setq arg 1))
107 (dotimes (ii arg) ; move scope downward
108 (let ((amt (if pixel-resolution-fine-flag
109 (if (integerp pixel-resolution-fine-flag)
110 pixel-resolution-fine-flag
111 (frame-char-height))
112 (pixel-line-height))))
113 (while (pixel-point-at-top-p amt) ; prevent too late (multi tries)
114 (vertical-motion 1)) ; move point downward
115 (if (pixel-eob-at-top-p) ; when end-of-the-buffer is close
116 (scroll-up 1) ; relay on robust method
117 (pixel-scroll-pixel-up amt))))) ; move scope downward
119 (defun pixel-scroll-down (&optional arg)
120 "Scroll text of selected window down ARG lines.
121 This is and alternative of `scroll-down'. Scope moves upward."
122 (interactive)
123 (or arg (setq arg 1))
124 (dotimes (ii arg)
125 (let ((amt (if pixel-resolution-fine-flag
126 (if (integerp pixel-resolution-fine-flag)
127 pixel-resolution-fine-flag
128 (frame-char-height))
129 (pixel-line-height -1))))
130 (while (pixel-point-at-bottom-p amt) ; prevent too late (multi tries)
131 (vertical-motion -1)) ; move point upward
132 (if (or (pixel-bob-at-top-p amt) ; when beginning-of-the-buffer is seen
133 (pixel-eob-at-top-p)) ; for file with a long line
134 (scroll-down 1) ; relay on robust method
135 (pixel-scroll-pixel-down amt)))))
137 (defun pixel-bob-at-top-p (amt)
138 "Return non-nil if window-start is at beginning of the current buffer.
139 Window must be vertically scrolled by not more than AMT pixels."
140 (and (equal (window-start) (point-min))
141 (< (window-vscroll nil t) amt)))
143 (defun pixel-eob-at-top-p ()
144 "Return non-nil if end of buffer is at top of window."
145 (<= (count-lines (window-start) (window-end)) 2)) ; count-screen-lines
147 (defun pixel-posn-y-at-point ()
148 "Return y coordinates of point in pixels of current window.
149 This returns nil when horizontally scrolled."
150 (when (equal (window-hscroll) 0)
151 (save-excursion
152 ;; When there's an overlay string on a line, move
153 ;; point by (beginning-of-visual-line).
154 (beginning-of-visual-line)
155 ;; (- (cadr (pos-visible-in-window-p (point) nil t))
156 ;; (line-pixel-height))
157 (cdr (posn-x-y (posn-at-point))))))
159 (defun pixel-point-at-top-p (amt)
160 "Return if point is located at top of a window on coming scroll of AMT pixels.
161 When location of point was not obtained, this returns if point is at top
162 of window."
163 (let ((y (pixel-posn-y-at-point))
164 top-margin)
165 (cond
167 (setq top-margin y)
168 (< top-margin amt))
170 (<= (count-lines (window-start) (point)) 1)))))
172 (defun pixel-point-at-bottom-p (amt)
173 "Return if point is located at bottom of window on coming scroll of AMT pixels.
174 When location of point was not obtained, this returns nil."
175 (let* ((edges (window-inside-pixel-edges))
176 (height (- (nth 3 edges) (nth 1 edges))) ; (- bottom top)
177 (y (pixel-posn-y-at-point))
178 bottom-margin)
179 (when y
180 (setq bottom-margin (- height (+ y (pixel-visual-line-height))))
181 (< bottom-margin amt)))) ; coming unseen line
183 (defun pixel-scroll-pixel-up (amt)
184 "Scroll text of selected windows up AMT pixels.
185 Scope moves downward."
186 (while (>= (+ (window-vscroll nil t) amt)
187 (pixel-line-height))
188 (setq amt (- amt (pixel--whistlestop-line-up)))) ; major scroll
189 (pixel--whistlestop-pixel-up amt)) ; minor scroll
191 (defun pixel-scroll-pixel-down (amt)
192 "Scroll text of selected windows down AMT pixels.
193 Scope moves upward."
194 (while (> amt 0)
195 (let ((vs (window-vscroll nil t)))
196 (if (equal vs 0)
197 (progn
198 ;; On horizontal scrolling, move cursor.
199 (when (> (window-hscroll) 0)
200 (vertical-motion -1))
201 (pixel-scroll-down-and-set-window-vscroll
202 (1- (pixel-line-height -1))))
203 (set-window-vscroll nil (1- vs) t))
204 (setq amt (1- amt))
205 (sit-for pixel-wait))))
207 (defun pixel--whistlestop-line-up ()
208 "Scroll text upward a line with each pixel whistlestopped.
209 When `vscroll' is non-zero, complete scrolling a line. When
210 `vscroll' is larger than height of multiple lines, for example
211 88, this flushes multiple lines. At the end, `vscroll' will be
212 zero. This assumes that the lines are with the same height.
213 Scope moves downward. This function returns number of pixels
214 that was scrolled."
215 (let* ((src (window-vscroll nil t)) ; EXAMPLE (initial) @0 @8 @88
216 (height (pixel-line-height)) ; 25 25 23
217 (line (1+ (/ src height))) ; catch up + one line 1 1 4
218 (dst (* line height)) ; goal @25 @25 @92
219 (delta (- dst src))) ; pixels to be scrolled 25 17 4
220 (pixel--whistlestop-pixel-up (1- delta)) ; until one less @24 @24 @91
221 (dotimes (ii line)
222 ;; On horizontal scrolling, move cursor.
223 (when (> (window-hscroll) 0)
224 (vertical-motion 1))
225 (scroll-up 1))
226 (sit-for pixel-wait) ; scroll 1 pixel @0 @0 @0
227 delta))
229 (defun pixel--whistlestop-pixel-up (n)
230 "Scroll text upward by N pixels with each pixel whistlestopped.
231 Scope moves downward."
232 (when (> n 0)
233 (let ((vs0 (window-vscroll nil t)))
234 (dolist (vs (number-sequence (1+ vs0) (+ vs0 n)))
235 (set-window-vscroll nil vs t) (sit-for pixel-wait)))))
237 (defun pixel-line-height (&optional pos)
238 "Return height in pixels of text line at POS in the selected window.
239 When POS is nil or negative, height of the first line or the coming
240 unseen line above the first line, respectively, is provided."
241 (or pos (setq pos (window-start)))
242 (when (< pos 0)
243 (setq pos (pixel-point-at-unseen-line)))
244 (let ((vs1 (window-vscroll nil t))
245 height)
246 (set-window-vscroll nil 0 t)
247 (save-excursion
248 (goto-char pos)
249 (setq height (pixel-visual-line-height))) ; line-pixel-height, frame-char-height
250 (set-window-vscroll nil vs1 t)
251 height))
253 (defun pixel-visual-line-height ()
254 "Return height in pixels of text line where cursor is in the selected window."
255 (let ((pos (pixel-visible-pos-in-window)))
256 (cond
257 ;; When a char of line is shown, obtain height by
258 ;; (line-pixel-height).
259 (pos (save-excursion (goto-char pos) (line-pixel-height)))
260 ;; When no char of line is shown but the line is at the top,
261 ;; obtain height by (line-pixel-height). This is based on
262 ;; expected response from display engine. See following
263 ;; discussion.
264 ;; https://lists.gnu.org/r/emacs-devel/2017-10/msg00621.html
265 ((equal (count-lines (window-start) (point)) 1)
266 (line-pixel-height))
267 ;; No char of line is shown and the line is not at the top,
268 ;; obtain height by (frame-char-height).
269 (t (frame-char-height)))))
271 (defun pixel-visible-pos-in-window ()
272 "Return position shown on text line where cursor is in the selected window.
273 This will look for positions of point and end-of-visual-line,
274 then positions from beginning-of-visual-line to
275 end-of-visual-line. When no char in a line is shown, this
276 returns nil."
277 (let* ((beginning-of-visual-line-pos (save-excursion (beginning-of-visual-line) (point)))
278 (end-of-visual-line-pos (save-excursion (end-of-visual-line) (point)))
279 (pos-list (number-sequence beginning-of-visual-line-pos end-of-visual-line-pos))
280 (edges (window-inside-pixel-edges))
281 (width (- (nth 2 edges) (nth 0 edges)))
282 posn-x
283 visible-pos)
284 ;; Optimize list of position to be surveyed.
285 (push end-of-visual-line-pos pos-list)
286 (push (point) pos-list)
287 (delete-dups pos-list)
288 ;; Find out a char with position X that is more than zero and less
289 ;; than width of screen.
290 (while (and (not visible-pos)
291 pos-list)
292 (setq posn-x (car (pos-visible-in-window-p (car pos-list) nil t)))
293 (if (and posn-x
294 (<= 0 posn-x)
295 (< posn-x width))
296 (setq visible-pos (car pos-list))
297 (setq pos-list (cdr pos-list))))
298 visible-pos))
300 (defun pixel-point-at-unseen-line ()
301 "Return the character position of line above the selected window.
302 The returned value is the position of the first character on the
303 unseen line just above the scope of current window."
304 (let* ((pos0 (window-start))
305 (vscroll0 (window-vscroll nil t))
306 (pos
307 (save-excursion
308 (goto-char pos0)
309 (if (bobp)
310 (point-min)
311 ;; When there's an overlay string at window-start,
312 ;; (beginning-of-visual-line 0) stays put.
313 (let ((ppos (point))
314 (tem (beginning-of-visual-line 0)))
315 (if (eq tem ppos)
316 (vertical-motion -1))
317 (point))))))
318 ;; restore initial position
319 (set-window-start nil pos0 t)
320 (set-window-vscroll nil vscroll0 t)
321 pos))
323 (defun pixel-scroll-down-and-set-window-vscroll (vscroll)
324 "Scroll down a line and set VSCROLL in pixels.
325 It is important to call `set-window-start' to force the display
326 engine use that particular position as the window-start point.
327 Otherwise, redisplay will reset the window's vscroll."
328 (set-window-start nil (pixel-point-at-unseen-line) t)
329 (set-window-vscroll nil vscroll t))
331 (provide 'pixel-scroll)
332 ;;; pixel-scroll.el ends here