-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcentered-window.el
291 lines (258 loc) · 9.77 KB
/
centered-window.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
;;; centered-window.el --- Center the text when there's only one window -*- lexical-binding: t; -*-
;;
;; Author: Anler Hernández Peral <inbox+emacs@anler.me>
;; Version: 1.4.0
;; Contributors:
;; Mickael Kerjean <https://github.com/mickael-kerjean>
;; Pierre Lecocq <https://github.com/pierre-lecocq>
;; Syohei YOSHIDA <https://github.com/syohex>
;; Lars Tveito <https://github.com/larstvei>
;; Tianxiang Xiong <https://github.com/xiongtx>
;; Keywords: faces windows
;; URL: https://github.com/anler/centered-window-mode
;; Package-Requires: ((emacs "24.4"))
;; Compatibility: GNU Emacs 24.x
;;
;; This file is NOT part of GNU Emacs.
;;
;; 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, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Enable centered-window-mode and your text is going to be centered when there's
;; only one window in the frame.
;;
;; Customizable options are:
;; cwm-lighter
;; cwm-centered-window-width
;; cwm-ignore-buffer-predicates
;; cwm-incremental-padding
;; cwm-incremental-padding-%
;; cwm-use-vertical-padding
;; cwm-frame-internal-border
;;
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'face-remap)
(require 'subr-x)
(require 'mac-win nil t)
(require 'mwheel nil t)
(defgroup centered-window nil
"Center text in windows."
:group 'windows
:prefix "cwm-")
(defcustom cwm-lighter
" #"
"Mode's lighter used in the mode line."
:group 'centered-window
:type 'string)
(defcustom cwm-centered-window-width
110
"Minimum line length required to apply the margins."
:group 'centered-window
:initialize #'custom-initialize-default
:set #'cwm--set-and-recenter-windows
:type 'integer)
(defcustom cwm-incremental-padding
nil
"If t even when the window's width is less than `cwm-centered-window-width' a padding of `cwm-incremental-padding-%' will be applied to each side."
:group 'centered-window
:type 'boolean)
(defcustom cwm-incremental-padding-%
0
"Incremental padding percentage to use when `cwm-incremental-padding' is t."
:group 'centered-window
:type 'integer)
(defcustom cwm-use-vertical-padding
nil
"Whether or not use experimental vertical padding."
:group 'centered-window
:initialize #'custom-initialize-default
:set #'cwm--set-and-recenter-windows
:type 'boolean)
(defcustom cwm-frame-internal-border
5
"Frame internal border to use when vertical padding is used."
:group 'centered-window
:initialize #'custom-initialize-default
:set #'cwm--set-and-recenter-windows
:type 'integer)
(defcustom cwm-left-fringe-ratio
0
"Ratio by which the left fringe is padded more than the right.
Should be a value between 0 and 100. A value of 0 means off."
:group 'centered-window
:initialize #'custom-initialize-default
:set #'cwm--set-and-recenter-windows
:type '(integer
:validate (lambda (widget)
(let ((ratio (widget-value widget)))
(unless (<= 0 ratio 100)
(widget-put widget :error (format "Invalid ratio (0-100): '%s'" ratio))
widget)))))
(defcustom cwm-ignore-buffer-predicates
(list #'cwm-special-buffer-p)
"List of predicate functions.
Each is run with current buffer and if it returns 't the
mode won't activate in that buffer."
:group 'centered-window
:type '(list function))
(define-obsolete-variable-alias
'centered-window-mode-hooks
'cwm-hooks "1.3.0")
(defcustom cwm-hooks
nil
"Hooks to run every time window is centered (be careful)."
:group 'centered-window
:type 'hook)
(defun cwm--set-and-recenter-windows (var val)
"Set customizable variable VAR to VAL and recenter windows.
All windows in all frames are recentered.
This is intended for use as the `setfunction' of a
`defcustom'. See Info node `(elisp) Variable Definitions'."
(set-default var val)
(dolist (frame (frame-list))
(with-selected-frame frame
(cwm-center-windows))))
(defadvice load-theme (after cwm-set-faces-on-load-theme activate)
"Change the default fringe background whenever the theme changes."
(cwm-update-fringe-background))
(defun cwm-ignore-window-p (window)
"Check if BUFF should be ignored when activating the mode."
(not
(null
(delq nil
(mapcar (lambda (predicate)
(funcall predicate (window-buffer window)))
cwm-ignore-buffer-predicates)))))
(defun cwm-special-buffer-p (buffer)
"Return 't if BUFF buffer name is special (starts with an *).
The *scratch* buffer although special, is treated as not special
by this function."
(let ((buffname (string-trim (buffer-name buffer))))
(and buffname
(string-prefix-p "*" buffname)
(not (string= "*scratch*" buffname)))))
(defun cwm-update-fringe-background ()
(custom-set-faces
`(fringe ((t (:background ,(face-attribute 'default :background)))))))
(defun cwm-turn-on ()
(add-hook 'window-configuration-change-hook #'cwm-center-windows)
(add-hook 'window-size-change-functions #'cwm-center-windows-frame)
(cwm-center-windows)
(when cwm-use-vertical-padding
(set-frame-parameter nil 'internal-border-width cwm-frame-internal-border))
(cwm-bind-fringe-mouse-events))
(defun cwm-turn-off ()
(remove-hook 'window-configuration-change-hook #'cwm-center-windows)
(remove-hook 'window-size-change-functions #'cwm-center-windows-frame)
(cwm-center-windows)
(set-frame-parameter
nil
'internal-border-width
(or (alist-get 'internal-border-width default-frame-alist)
0))
(cwm-unbind-fringe-mouse-events))
(defun cwm-center-windows-frame (frame)
(when (frame-size-changed-p frame)
(cwm-center-windows)))
(defun cwm-center-windows ()
(let ((windows (window-list nil :exclude-minibuffer)))
(mapc #'cwm-center-window-instructions
(mapcar #'cwm-centering-instructions
(cl-remove-if #'cwm-ignore-window-p windows)))
(run-hooks 'centered-window-mode-hooks)))
(cl-defstruct cwm-centering-instructions
window
left-width
right-width)
(defun cwm-center-window-instructions (instructions)
(let* ((window (cwm-centering-instructions-window instructions)))
(set-window-fringes window
(cwm-centering-instructions-left-width instructions)
(cwm-centering-instructions-right-width instructions))))
(defun cwm-centering-instructions (window)
(let ((widths (cwm-calculate-appropriate-fringe-widths window)))
(make-cwm-centering-instructions
:window window
:left-width (car widths)
:right-width (cdr widths))))
(defun cwm-calculate-appropriate-fringe-widths (window)
(let* ((mode-active-p (with-current-buffer (window-buffer window) centered-window-mode))
(pixel (frame-char-width (window-frame window)))
(window-width (window-total-width window))
(n (if mode-active-p
(max
(/ (- window-width cwm-centered-window-width)
2)
(if cwm-incremental-padding
(/ (* window-width cwm-incremental-padding-%)
100)
0))
0))
(ratio (/ (* n cwm-left-fringe-ratio) 100))
(left-width (and mode-active-p (* pixel (if (> n 0) (+ n ratio) n))))
(right-width (and mode-active-p (* pixel (if (> n 0) (- n ratio) n)))))
`(,left-width . ,right-width)))
(defun cwm-toggle-bind-fringe-mouse-events (&optional bind direction-command-alist)
(dolist (fringe '("left" "right"))
(dolist (wheel-speed '("" "double" "triple"))
(dolist (scroll-direction '("left" "right" "up" "down"))
(let ((key-name (kbd (concat
"<" fringe "-fringe> "
"<" wheel-speed
(if (string= wheel-speed "") "" "-")
"wheel-" scroll-direction ">"))))
(if bind
(global-set-key key-name
(alist-get (intern-soft scroll-direction)
direction-command-alist))
(global-unset-key key-name)))))))
(defun cwm-bind-fringe-mouse-events ()
(cond ((and (eq window-system 'mac) (featurep 'mac-win))
(cwm-toggle-bind-fringe-mouse-events
t
'((left . mac-mwheel-scroll)
(right . mac-mwheel-scroll)
(up . mac-mwheel-scroll)
(down . mac-mwheel-scroll))))
((eq window-system nil) nil)
(t
(cwm-toggle-bind-fringe-mouse-events
t
'((left . mwheel-scroll)
(right . mwheel-scroll)
(up . mwheel-scroll)
(down . mwheel-scroll))))))
(defun cwm-unbind-fringe-mouse-events ()
(cond ((and (eq window-system 'mac) (featurep 'mac-win))
(cwm-toggle-bind-fringe-mouse-events nil))
((eq window-system nil) nil)
(t (cwm-toggle-bind-fringe-mouse-events nil))))
;;;###autoload
(defun centered-window-mode-toggle ()
(if centered-window-mode
(centered-window-mode -1)
(centered-window-mode +1)))
;;;###autoload
(define-minor-mode centered-window-mode
"Minor mode to center text on the current buffer"
:init-value nil
:global t
:lighter cwm-lighter
(if centered-window-mode (cwm-turn-on) (cwm-turn-off)))
(provide 'centered-window-mode)
(provide 'centered-window)
;;; centered-window.el ends here