summaryrefslogtreecommitdiff
path: root/center-buffer.el
blob: 42c38ef29d2fbe06e53bbcbd44ed3e0501e23c81 (plain)
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
;;; center-buffer.el --- Center the buffer -*- lexical-binding: t; -*-

;;; Commentary:

;;; This is some code I extracted from "olivetti" as a lightweight
;;; version of that package to center my buffer, only temporarily,
;;; unfortunately.

;;; Code:

;;;###autoload
(defvar center-buffer-width 80
  "The width of the body.")

;;;###autoload
(defun center-buffer-toggle ()
  "Toggle the centering of the buffer."
  (interactive)
  (cond
   ((car (window-margins (selected-window)))
    (center-buffer-off))
   ((center-buffer-on))))

;;;###autoload
(defun center-buffer-on ()
  "Center the buffer."
  (interactive)
  (let* ((mt (/ (- (window-total-width) 80) 2))
	 (fringes (window-fringes))
	 (lf (/ (car fringes) (float (frame-char-width))))
	 (rf (/ (cadr fringes) (float (frame-char-width))))
	 (lm (max (round (- mt lf)) 0))
	 (rm (max (round (- mt rf)) 0)))
    (set-window-margins (selected-window) lm rm)))

;;;###autoload
(defun center-buffer-off ()
  "Reset buffer."
  (interactive)
  (set-window-margins (selected-window) 0 0))

(provide 'center-bufrfer)
;;; center-bufrfer.el ends here.