-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-margin.el
53 lines (43 loc) · 1.75 KB
/
auto-margin.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
(defun auto-margin--buffer? (window)
;; TODO: Try filtering by the window's height instead of the name.
(not (string-match-p ".*Minibuf.*" (buffer-name (window-buffer window)))))
(defun auto-margin--non-minibuffer-windows (frame)
(seq-filter 'auto-margin--buffer? (window-list frame)))
(defun auto-margin--single-window? (frame)
(= (length (auto-margin--non-minibuffer-windows frame)) 1))
(defun auto-margin--calculate-margin ()
(/ (- (frame-width) (current-fill-column)) 3))
(defun auto-margin--window (window)
(if (auto-margin--buffer? window)
(let ((margin (auto-margin--calculate-margin)))
(set-window-margins window margin margin))
(set-window-margins window 0)))
(defun auto-margin--reset-margin-frame (frame)
(dolist (window (window-list frame))
(set-window-margins window 0)))
(defun auto-margin--frame (frame)
(if (auto-margin--single-window? frame)
(dolist (window (window-list frame))
(auto-margin--window window))
(auto-margin--reset-margin-frame frame)))
(defvar auto-margin--mode-enabled nil)
(defun auto-margin--toggle-mode ()
(setq auto-margin--mode-enabled (not auto-margin--mode-enabled))
(if auto-margin--mode-enabled
(progn
(add-hook 'window-buffer-change-functions 'auto-margin--frame)
(add-hook 'window-size-change-functions 'auto-margin--frame))
(progn
(remove-hook 'window-buffer-change-functions 'auto-margin--frame)
(remove-hook 'window-size-change-functions 'auto-margin--frame))
(auto-margin--reset-margin-frame nil)))
;;;###autoload
(define-minor-mode auto-margin-mode
"Toggle auto-margin mode.
Automatically adjusts set-widow-margins to center text when only
a single window is displayed."
:init-value nil
:lighter " Auto-Margin"
:global t
(auto-margin--toggle-mode))
(provide 'auto-margin)