-
Notifications
You must be signed in to change notification settings - Fork 5
/
fast-scroll.el
197 lines (163 loc) · 7.5 KB
/
fast-scroll.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
;;; fast-scroll.el --- Some utilities for faster scrolling over large buffers. -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Matthew Carter <[email protected]>
;; Author: Matthew Carter <[email protected]>
;; Maintainer: Matthew Carter <[email protected]>
;; URL: https://github.com/ahungry/fast-scroll
;; Version: 0.0.6
;; Keywords: ahungry convenience fast scroll scrolling
;; Package-Requires: ((emacs "25.1") (cl-lib "0.6.1"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; An enhanced scrolling experience when quickly navigating through a buffer
;; with fast scrolling (usually via key-repeat instead of manual scrolling).
;;; Code:
(require 'cl-lib)
;; Fix for slow scrolling
(declare-function evil-scroll-up "ext:evil-commands.el" (count) t)
(declare-function evil-scroll-down "ext:evil-commands.el" (count) t)
(defvar fast-scroll-mode nil)
(defvar fast-scroll-mode-line-original nil)
(defvar fast-scroll-pending-reset nil)
(defvar fast-scroll-timeout 0)
(defvar fast-scroll-count 0)
(defvar fast-scroll-throttle 0.2)
(defvar fast-scroll-throttling-p nil)
(defvar fast-scroll-start-hook '())
(defvar fast-scroll-end-hook '())
;; This is more for internal use only to resolve timing issues with
;; flip-flopping between buffers quickly.
;; Initial idea was a single store to restore settings on, but that
;; could end up "locking" a buffer if for some reason end fails to run
;; for some odd reason.
;; If the user does aggressively switch buffers and scroll fast in them,
;; the worst case is prevented (all buffers become un-fast after the timer).
;; However, no attempt is going to be made to 'fast-scroll' every buffer in this way,
;; because all the timing sensitive code is a global package variable.
(defvar fast-scroll--fn-called-in-buffer '())
(defun fast-scroll-default-mode-line ()
"An Emacs default/bare bones mode-line."
(list "%e" mode-line-front-space
mode-line-client
mode-line-modified
mode-line-frame-identification
mode-line-buffer-identification " "
mode-line-position
" " mode-line-modes mode-line-misc-info mode-line-end-spaces))
(defun fast-scroll-get-milliseconds ()
"Get the current MS in float up to 3 precision."
(string-to-number (format-time-string "%s.%3N")))
(defun fast-scroll-end-p ()
"See if we can end or not."
(> (- (fast-scroll-get-milliseconds) fast-scroll-timeout) (- fast-scroll-throttle 0.01)))
(defun fast-scroll--end (buf)
"Re-enable the things we disabled during the fast scroll for buffer BUF."
(when (fast-scroll-end-p)
(with-current-buffer buf
(setq fast-scroll--fn-called-in-buffer nil)
(setq mode-line-format fast-scroll-mode-line-original)
(font-lock-mode 1)
(run-hooks 'fast-scroll-end-hook)
(setq fast-scroll-throttling-p nil)
(setq fast-scroll-count 0))))
(defun fast-scroll-end ()
"Re-enable the things we disabled during the fast scrolls."
(mapcar #'fast-scroll--end fast-scroll--fn-called-in-buffer))
(defun fast-scroll--run-fn-minimally (f &rest r)
"Inner function for applying the function F of args R with the minimized modes.
The outer function is the non-private prefixed one, which will only run when it has set
a new buffer name (or found the existing buffer name to match the current one)."
(unless fast-scroll-mode-line-original
(setq fast-scroll-mode-line-original mode-line-format))
(setq fast-scroll-count (+ 1 fast-scroll-count))
(if (< fast-scroll-count 2)
(progn
(ignore-errors (apply f r))
(run-at-time fast-scroll-throttle nil (lambda () (setq fast-scroll-count 0))))
(setq fast-scroll-timeout (fast-scroll-get-milliseconds))
(if fast-scroll-throttling-p
(ignore-errors (apply f r))
(progn
(setq mode-line-format (fast-scroll-default-mode-line))
(font-lock-mode 0)
(run-hooks 'fast-scroll-start-hook)
(ignore-errors (apply f r))))
(run-at-time fast-scroll-throttle nil #'fast-scroll-end)
(setq fast-scroll-throttling-p t)))
(defun fast-scroll-run-fn-minimally (f &rest r)
"Enables fast execution on function F with args R, by disabling certain modes."
(cl-pushnew (buffer-name) fast-scroll--fn-called-in-buffer :test #'string=)
(apply #'fast-scroll--run-fn-minimally f r))
(defun fast-scroll-scroll-up-command ()
"Scroll up quickly - comparative to `scroll-up-command'."
(interactive)
(fast-scroll-run-fn-minimally #'scroll-up-command))
(defun fast-scroll-scroll-down-command ()
"Scroll down quickly - comparative to `scroll-down-command'."
(interactive)
(fast-scroll-run-fn-minimally #'scroll-down-command))
(defun fast-scroll-evil-scroll-up ()
"Scroll down quickly - comparative to `evil-scroll-up'."
(interactive)
(fast-scroll-run-fn-minimally #'evil-scroll-up))
(defun fast-scroll-evil-scroll-down ()
"Scroll down quickly - comparative to `evil-scroll-down'."
(interactive)
(fast-scroll-run-fn-minimally #'evil-scroll-down))
;;;###autoload
(defun fast-scroll-config ()
"Load some config defaults / binds."
(interactive)
(global-set-key (kbd "<prior>") 'fast-scroll-scroll-down-command)
(global-set-key (kbd "<next>") 'fast-scroll-scroll-up-command))
;;;###autoload
(defun fast-scroll-advice-scroll-functions ()
"Wrap as many scrolling functions that we know of in this advice."
(interactive)
(advice-add #'scroll-up-command :around #'fast-scroll-run-fn-minimally)
(advice-add #'scroll-down-command :around #'fast-scroll-run-fn-minimally)
(advice-add #'evil-scroll-up :around #'fast-scroll-run-fn-minimally)
(advice-add #'evil-scroll-down :around #'fast-scroll-run-fn-minimally))
(defun fast-scroll-unload-function ()
"Remove advice added by `fast-scroll-advice-scroll-functions'.
Note this function's name implies compatibility with `unload-feature'."
(interactive)
(advice-remove #'scroll-up-command #'fast-scroll-run-fn-minimally)
(advice-remove #'scroll-down-command #'fast-scroll-run-fn-minimally)
(advice-remove #'evil-scroll-up #'fast-scroll-run-fn-minimally)
(advice-remove #'evil-scroll-down #'fast-scroll-run-fn-minimally)
nil)
;;;###autoload
(define-minor-mode fast-scroll-mode
"Minor mode to speed up scrolling.
When fast-scroll-mode is on, certain features/modes of Emacs will be
shut off or minimized during the scrolling activity, to ensure
the user experience the least amount of scroll-lag as possible.
By default, enabling this global minor mode will advice the following
scrolling built-ins (or commonly installed scroll functions): `scroll-up-command',
`scroll-down-command', `evil-scroll-up', `evil-scroll-down'.
Disabling this mode will unload the advice that was added when enabling.
The mode-line format will also be set to a minimal mode-line
during scrolling activity."
:group 'fast-scroll
:global t
:lighter " fs"
(if fast-scroll-mode
(progn
(fast-scroll-advice-scroll-functions))
(progn
(fast-scroll-unload-function))))
(provide 'fast-scroll)
;;; fast-scroll.el ends here