emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/olivetti 3f237b0 075/134: Use split-window window-param


From: Stefan Monnier
Subject: [elpa] externals/olivetti 3f237b0 075/134: Use split-window window-parameter
Date: Thu, 25 Apr 2019 09:57:32 -0400 (EDT)

branch: externals/olivetti
commit 3f237b03a12d0be58fddd99b979c4c191937d02c
Author: Paul Rankin <address@hidden>
Commit: Paul Rankin <address@hidden>

    Use split-window window-parameter
    
    Also set local split-window-preferred-function to
    olivetti-split-window-sensibly to allow vertical splitting
---
 olivetti.el | 82 +++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 52 insertions(+), 30 deletions(-)

diff --git a/olivetti.el b/olivetti.el
index 7eaa40d..b33aea9 100644
--- a/olivetti.el
+++ b/olivetti.el
@@ -215,32 +215,55 @@ fraction of the window width."
   (olivetti-set-environment)
   (message "Text body width set to %s" olivetti-body-width))
 
-(defun olivetti-set-environment (&optional arg)
+(defun olivetti-set-environment ()
   "Set text body width to `olivetti-body-width' with relative margins.
 
-Cycle through all windows displaying current buffer and:
-
-If ARG is 'exit set window margins to nil.
-
-If ARG is nil, first find the `olivetti-safe-width' to which to
-set `olivetti-body-width', then find the appropriate margin size
+Cycle through all windows displaying current buffer and, first,
+find the `olivetti-safe-width' to which to set
+`olivetti-body-width', then find the appropriate margin size
 relative to each window. Finally set the window margins, taking
 care that the maximum size is 0."
-  (dolist (window (get-buffer-window-list (current-buffer) nil t))
-    (if (eq arg 'exit)
-        (set-window-margins window nil nil)
-      (let* ((n (olivetti-safe-width (if (integerp olivetti-body-width)
-                                         (olivetti-scale-width 
olivetti-body-width)
-                                       olivetti-body-width)
-                                     window))
-             (width (cond ((integerp n) n)
-                          ((floatp n) (* (window-total-width window)
-                                         n))))
-             (margin (max (round (/ (- (window-total-width window)
-                                       width)
-                                    2))
-                          0)))
-        (set-window-margins window margin margin)))))
+  (dolist (window (get-buffer-window-list nil nil t))
+    (let* ((n (olivetti-safe-width (if (integerp olivetti-body-width)
+                                       (olivetti-scale-width 
olivetti-body-width)
+                                     olivetti-body-width)
+                                   window))
+           (width (cond ((integerp n) n)
+                        ((floatp n) (* (window-total-width window)
+                                       n))))
+           (margin (max (round (/ (- (window-total-width window)
+                                     width)
+                                  2))
+                        0)))
+      (set-window-parameter window 'split-window 'olivetti-split-window)
+      (set-window-margins window margin margin))))
+
+(defun olivetti-reset-environment ()
+  "Remove Olivetti's parameters and margins.
+
+Cycle through all windows displaying current buffer and reset
+window parameter `split-window' to nil. Then reset the window
+margins to nil."
+  (dolist (window (get-buffer-window-list nil nil t))
+    (set-window-parameter window 'split-window nil)
+    (set-window-margins window nil nil)))
+
+(defun olivetti-split-window (&optional window size side pixelwise)
+  "Safely split the window by first resetting the environment.
+
+First call `olivetti-reset-environment' then try
+`split-window'.
+
+If `olivetti-mode' is non-nil, call `olivetti-set-environment'."
+  (olivetti-reset-environment)
+  (split-window window size side pixelwise)
+  (if olivetti-mode (olivetti-set-environment)))
+
+(defun olivetti-split-window-sensibly (&optional window)
+  "Like `olivetti-split-window' but calls `split-window-sensibly'."
+  (olivetti-reset-environment)
+  (split-window-sensibly window)
+  (if olivetti-mode (olivetti-set-environment)))
 
 (defun olivetti-toggle-hide-mode-line ()
   "Toggle the visibility of the mode-line.
@@ -333,24 +356,23 @@ hidden."
                         text-scale-mode-hook))
           (add-hook hook 'olivetti-set-environment t t))
         (add-hook 'change-major-mode-hook
-                  '(lambda nil (olivetti-set-environment 'exit)) nil t)
+                  'olivetti-reset-environment nil t)
+        (setq-local split-window-preferred-function
+              'olivetti-split-window-sensibly)
         (setq olivetti--visual-line-mode visual-line-mode)
-        (unless olivetti--visual-line-mode
-          (visual-line-mode 1))
-        (if olivetti-hide-mode-line
-            (olivetti-set-mode-line))
-        (olivetti-patch-split-window)
+        (unless olivetti--visual-line-mode (visual-line-mode 1))
+        (if olivetti-hide-mode-line (olivetti-set-mode-line))
         (olivetti-set-environment))
     (dolist (hook '(window-configuration-change-hook
                     after-setting-font-hook
                     text-scale-mode-hook))
       (remove-hook hook 'olivetti-set-environment t))
-    (olivetti-patch-split-window 'remove)
     (olivetti-set-mode-line 'exit)
-    (olivetti-set-environment 'exit)
+    (olivetti-reset-environment)
     (if (and olivetti-recall-visual-line-mode-entry-state
              (not olivetti--visual-line-mode))
         (visual-line-mode 0))
+    (kill-local-variable 'split-window-preferred-function)
     (kill-local-variable 'olivetti--visual-line-mode)))
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]