]> code.delx.au - gnu-emacs-elpa/blob - darkroom.el
fix: save HEADER-LINE-FORMAT as well
[gnu-emacs-elpa] / darkroom.el
1 (defvar darkroom-margins 0.15
2 "Margins to use in darkroom-mode.
3
4 It's value can be:
5
6 - a floating point value betweeen 0 and 1, specifies percentage of
7 window width in columns to use as a margin.
8
9 - a cons cell (LEFT RIGHT) specifying the left and right margins
10 in columns.
11
12 - a function that returns a cons cell interpreted like the
13 previous option.
14
15 Value is effective when `darkroom-minor-mode' is toggled, when
16 changing window or by calling `darkroom-set-margins'")
17
18 (defvar darkroom-turns-on-visual-line-mode t
19 "If non-nil pair `visual-line-mode' with
20 `darkroom-minor-mode'")
21 (defvar darkroom-fringes-outside-margins t
22 "If non-nil use fringes outside margins for `darkroom-minor-mode'")
23
24 (defun darkroom-margins ()
25 (cond ((functionp darkroom-margins)
26 (funcall darkroom-margins))
27 ((consp darkroom-margins)
28 darkroom-margins)
29 ((and (floatp darkroom-margins)
30 (< darkroom-margins 1))
31 (let ((delta (darkroom-float-to-columns darkroom-margins)))
32 (cons delta delta)))))
33
34 (defun darkroom-float-to-columns (f)
35 (ceiling (* (let ((edges (window-edges)))
36 (- (nth 2 edges) (nth 0 edges)))
37 f)))
38
39 (defun darkroom-set-margins (&optional margins)
40 "Adjust margins to `darkroom-margins' or optional MARGINS."
41 (let ((margins
42 (or margins
43 (darkroom-margins))))
44 (when margins
45 (set-window-margins (selected-window) (car margins) (cdr margins)))))
46
47 (defun darkroom-increase-margins ()
48 (interactive)
49 (when (floatp darkroom-margins)
50 (setq darkroom-margins (+ 0.05 darkroom-margins))
51 (darkroom-set-margins)))
52
53 (defun darkroom-decrease-margins ()
54 (interactive)
55 (when (floatp darkroom-margins)
56 (setq darkroom-margins (- darkroom-margins 0.05))
57 (darkroom-set-margins)))
58
59 (defun darkroom-fill-paragraph-maybe (really)
60 (interactive "P")
61 (cond (visual-line-mode
62 (if (not really)
63 (message "not filling paragraph")
64 (call-interactively 'fill-paragraph)
65 (message "filled paragraph even in visual-line-mode")))
66 (t
67 (call-interactively 'fill-paragraph))))
68
69 (defvar darkroom-minor-mode-map (let ((map (make-sparse-keymap)))
70 (define-key map (kbd "C-M-+") 'darkroom-increase-margins)
71 (define-key map (kbd "C-M--") 'darkroom-decrease-margins)
72 (define-key map (kbd "M-q") 'darkroom-fill-paragraph-maybe)
73 map))
74
75 (defvar darkroom-saved-mode-line-format nil)
76 (defvar darkroom-saved-visual-mode nil)
77 (define-minor-mode darkroom-minor-mode
78 "A minor mode that emulates the darkroom editor."
79 nil
80 " dark"
81 nil
82 (cond (darkroom-minor-mode
83 (setq darkroom-saved-mode-line-format mode-line-format
84 mode-line-format nil
85 darkroom-saved-header-line-format header-line-format
86 header-line-format "")
87 (setq fringes-outside-margins darkroom-fringes-outside-margins)
88 (add-hook 'window-configuration-change-hook 'darkroom-set-margins nil t)
89 (darkroom-set-margins)
90 ;; a hack shoulnd't be needed but apparently is
91 (set-window-buffer (selected-window) (current-buffer))
92 (when darkroom-turns-on-visual-line-mode
93 (visual-line-mode 1)))
94 (t
95 (setq mode-line-format darkroom-saved-mode-line-format
96 header-line-format darkroom-saved-header-line-format)
97 (when darkroom-turns-on-visual-line-mode
98 (visual-line-mode -1))
99 (remove-hook 'window-configuration-change-hook 'darkroom-set-margins t)
100 (set-window-margins (selected-window) 0 0))))
101
102
103
104
105 (provide 'darkroom)