-
Notifications
You must be signed in to change notification settings - Fork 26
/
sublimity.el
206 lines (172 loc) · 7.41 KB
/
sublimity.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
198
199
200
201
202
203
204
205
206
;;; -*- lexical-binding: t -*-
;;; sublimity.el --- smooth-scrolling, minimap and distraction-free mode
;; Copyright (C) 2013- zk_phi
;; 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 2 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, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
;; Author: zk_phi
;; URL: https://github.com/zk-phi/sublimity
;; Version: 1.1.5
;; Package-Requires: ((emacs "26.1"))
;;; Commentary:
;; Require this script and some of "sublimity-scroll" "sublimity-map".
;;
;; (require 'sublimity)
;; (require 'sublimity-scroll)
;; (require 'sublimity-map)
;;
;; then call command "M-x sublimity-mode".
;; If you want to enable sublimity everywhere, call function
;; sublimity-global-mode.
;;
;; (sublimity-global-mode)
;; For more informations, see "Readme".
;;; Change Log:
;; 1.0.0 first released
;; 1.1.0 turn into minor-mode
;; 1.1.1 add sublimity-mode-hook
;; 1.1.2 add sublimity-handle-scroll-criteria
;; remove add-hook in toplevel
;; make sublimity-mode global
;; 1.1.3 scroll-bar workaround
;; 1.1.4 divide `sublimity-handle-scroll-criteria' into three separate options
;; 1.1.5 remove deprecated variable
;; migrate to nadvice.el (now requires Emacs 25.1 or later)
;; update window margins when frame size changes
;;; Code:
(defconst sublimity-version "1.1.5")
;; + customs
(defgroup sublimity nil
"smooth-scrolling and minimap, like sublime editor"
:group 'emacs)
(defcustom sublimity-mode-hook nil
"hook run when entering sublimity-mode"
:type 'hook
:group 'sublimity)
(defcustom sublimity-ignored-scroll-commands
'(scroll-bar-drag scroll-bar-toolkit-scroll scroll-bar-scroll-up scroll-bar-scroll-down)
"List of scroll commands which sublimity should not handle."
:type '(repeat symbol)
:group 'sublimity)
(defcustom sublimity-disabled-major-modes
'(shell-mode image-mode)
"List of major-modes in which sublimity should be disabled."
:type '(repeat symbol)
:group 'sublimity)
(defcustom sublimity-disabled-minor-modes
'(cua--rectangle multiple-cursors-mode)
"List of minor-modes which sublimity does not work well with."
:type '(repeat symbol)
:group 'sublimity)
;; + minor mode
(defvar sublimity-auto-hscroll-mode nil)
;; + sublimity common vars, functions
(defvar sublimity--pre-command-functions nil)
(defvar sublimity--post-command-functions nil)
(defvar sublimity--window-change-functions nil)
(defvar sublimity--post-vscroll-functions nil
"called with number of lines, when vertical scroll is occurred.")
(defvar sublimity--post-hscroll-functions nil
"called with number of columns, when horizontal scroll is occurred.")
;;;###autoload
(define-minor-mode sublimity-mode
"smooth-scrolling and minimap, like sublime editor"
:init-value nil
:global t
(cond (sublimity-mode
(setq sublimity-auto-hscroll-mode auto-hscroll-mode)
(setq auto-hscroll-mode nil)
(add-hook 'pre-command-hook 'sublimity--pre-command nil)
(add-hook 'post-command-hook 'sublimity--post-command t)
(add-hook 'window-configuration-change-hook 'sublimity--window-change t)
(add-hook 'window-size-change-functions 'sublimity--window-change t)
(add-hook 'window-buffer-change-functions 'sublimity--window-change t)
(add-hook 'window-setup-hook 'sublimity--window-change t)
(run-hooks 'sublimity-mode-hook))
(t
(remove-hook 'pre-command-hook 'sublimity--pre-command)
(remove-hook 'post-command-hook 'sublimity--post-command)
(remove-hook 'window-configuration-change-hook 'sublimity--window-change)
(remove-hook 'window-size-change-functions 'sublimity--window-change)
(remove-hook 'window-buffer-change-functions 'sublimity--window-change)
(remove-hook 'window-setup-hook 'sublimity--window-change)
(run-hooks 'sublimity-mode-turn-off-hook)
(setq auto-hscroll-mode sublimity-auto-hscroll-mode))))
;; + internal vars, functions
(defvar sublimity--prepared nil)
(defvar sublimity--prev-lin (line-number-at-pos (window-start)))
(defvar sublimity--prev-col (window-hscroll))
(defvar sublimity--prev-buf (current-buffer))
(defvar sublimity--prev-wnd (selected-window))
(defun sublimity--run-hooks (hook &optional arg)
(let* ((sublimity--window-change-functions nil))
(if arg
(run-hook-with-args hook arg)
(run-hooks hook))))
(defun sublimity--horizontal-recenter ()
;; NOT accurate for some propertized texts.
(let ((cols (- (current-column)
(window-hscroll)
(/ (window-width) 2))))
(if (< cols 0)
(scroll-right (- cols))
(scroll-left cols))))
;; + hook functions
(defun sublimity--pre-command ()
(setq sublimity--prev-lin (line-number-at-pos (window-start))
sublimity--prev-col (window-hscroll)
sublimity--prev-buf (current-buffer)
sublimity--prev-wnd (selected-window)
sublimity--prepared t)
(sublimity--run-hooks 'sublimity--pre-command-functions))
(defun sublimity--post-command ()
;; avoid running post-command multiple times
(when sublimity--prepared
(setq sublimity--prepared nil)
(let ((handle-scroll (and (eq sublimity--prev-buf (current-buffer))
(eq sublimity--prev-wnd (selected-window))
(not (memq major-mode sublimity-disabled-major-modes))
(not (delq nil
(mapcar (lambda (x) (and (boundp x) (symbol-value x)))
sublimity-disabled-minor-modes)))
(not (memq this-command sublimity-ignored-scroll-commands)))))
(when handle-scroll
(let (deactivate-mark)
;; do vscroll
(with-selected-window sublimity--prev-wnd
(when (not (pos-visible-in-window-p))
(recenter)))
;; do hscroll
(when (and sublimity-auto-hscroll-mode
(or truncate-lines
(truncated-partial-width-window-p))
(or (< (current-column) (window-hscroll))
(< (+ (window-hscroll) (window-width))
(current-column))))
(sublimity--horizontal-recenter))))
;; call post-command functions
(sublimity--run-hooks 'sublimity--post-command-functions)
;; animation
(when handle-scroll
(let ((lins (- (line-number-at-pos (window-start))
sublimity--prev-lin))
(cols (- (window-hscroll) sublimity--prev-col)))
(when (not (zerop lins))
(sublimity--run-hooks 'sublimity--post-vscroll-functions lins))
(when (not (zerop cols))
(sublimity--run-hooks 'sublimity--post-hscroll-functions cols)))))))
(defun sublimity--window-change (&optional _)
(run-hooks 'sublimity--window-change-functions))
;; * provide
(provide 'sublimity)
;;; sublimity.el ends here