branch: elpa/golden-ratio
commit 5a661d32df4e0c34108141d4b8dcd9858ff7b424
Author: Thierry Volpiatto <[email protected]>
Commit: Thierry Volpiatto <[email protected]>

    Fix usage of golden-ration in gnus buffers (#49).
    
    * golden-ratio.el: Fix issue by reverting PR (#48).
---
 golden-ratio.el | 104 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 61 insertions(+), 43 deletions(-)

diff --git a/golden-ratio.el b/golden-ratio.el
index 67970fe91e..d5a6b58d9c 100644
--- a/golden-ratio.el
+++ b/golden-ratio.el
@@ -26,12 +26,12 @@
 
 ;; Major modes that are exempt from being resized. An example of this
 ;; for users of Org-mode might be:
-;;  ("calendar-mode") or (calendar-mode)
+;;  ("calendar-mode")
 (defcustom golden-ratio-exclude-modes nil
-  "A list of symbols or strings naming major modes.
+  "An array of strings naming major modes.
 Switching to a buffer whose major mode is a member of this list
 will not cause the window to be resized to the golden ratio."
-  :type '(repeat (choice symbol string))
+  :type '(repeat string)
   :group 'golden-ratio)
 
 ;; Buffer names that are exempt from being resized. An example of this
@@ -115,58 +115,76 @@ will not cause the window to be resized to the golden 
ratio."
   (with-selected-window (or window (selected-window))
     (let ((nrow  (floor (- (first  dimensions) (window-height))))
           (ncol  (floor (- (second dimensions) (window-width)))))
-      (when (and
-             (window-resizable-p (selected-window) nrow)
-             ;; don't enlarge ignored windows
-             (> nrow 0))
+      (when (window-resizable-p (selected-window) nrow)
         (enlarge-window nrow))
-      (when (and (window-resizable-p (selected-window) ncol t)
-                 ;; don't enlarge ignored windows
-                 (> ncol 0))
+      (when (window-resizable-p (selected-window) ncol t)
         (enlarge-window ncol t)))))
 
-(defun golden-ratio-exclude-major-mode-p ()
-  "Returns non-nil if `major-mode' should not use golden-ratio."
-  (or (memq major-mode golden-ratio-exclude-modes)
-      (member (symbol-name major-mode)
-              golden-ratio-exclude-modes)))
-
-(defvar golden-ratio-in-progress nil
-  "Avoid recursive adjustment.")
-
 ;;;###autoload
 (defun golden-ratio ()
   "Resizes current window to the golden-ratio's size specs."
   (interactive)
-  (when (and golden-ratio-mode
-             (not golden-ratio-in-progress))
-    (let ((golden-ratio-in-progress t))
-      (unless (or (window-minibuffer-p)
-                  (one-window-p)
-                  (golden-ratio-exclude-major-mode-p)
-                  (member (buffer-name)
-                          golden-ratio-exclude-buffer-names)
-                  (and golden-ratio-inhibit-functions
-                       (loop for fun in golden-ratio-inhibit-functions
-                          thereis (funcall fun))))
-        (balance-windows)
-        (golden-ratio--resize-window (golden-ratio--dimensions))
-        (when golden-ratio-recenter
-          (scroll-right) (recenter))))))
-
-(defadvice select-window (after golden-ratio-select-window activate)
-  (when golden-ratio-mode
-    (golden-ratio)))
-
-(defadvice split-window (after golden-ratio-select-window activate)
-  (when golden-ratio-mode
-    (golden-ratio)))
+  (unless (or (not golden-ratio-mode)
+              (window-minibuffer-p)
+              (one-window-p)
+              (member (symbol-name major-mode)
+                      golden-ratio-exclude-modes)
+              (member (buffer-name)
+                      golden-ratio-exclude-buffer-names)
+              (and golden-ratio-inhibit-functions
+                   (loop for fun in golden-ratio-inhibit-functions
+                         thereis (funcall fun))))
+    (let ((dims (golden-ratio--dimensions))
+          (golden-ratio-mode nil))
+      ;; Always disable `golden-ratio-mode' to avoid
+      ;; infinite loop in `balance-windows'.
+      (balance-windows)
+      (golden-ratio--resize-window dims)
+      (when golden-ratio-recenter
+        (scroll-right) (recenter)))))
+
+;; Should return nil
+(defadvice other-window
+    (after golden-ratio-resize-window)
+  (golden-ratio) nil)
+
+;; Should return the buffer
+(defadvice pop-to-buffer
+    (around golden-ratio-resize-window)
+  (prog1 ad-do-it (golden-ratio)))
+
+(defun golden-ratio--post-command-hook ()
+  (when (or (memq this-command golden-ratio-extra-commands)
+            (and (consp this-command) ; A lambda form.
+                 (loop for com in golden-ratio-extra-commands
+                       thereis (or (memq com this-command)
+                                   (memq (car-safe com) this-command)))))
+    ;; This is needed in emacs-25 to avoid this error from `recenter':
+    ;; `recenter'ing a window that does not display current-buffer.
+    ;; This doesn't happen in emacs-24.4 and previous versions.
+    (run-with-idle-timer 0.01 nil (lambda () (golden-ratio)))))
+
+(defun golden-ratio--mouse-leave-buffer-hook ()
+  (run-at-time 0.1 nil (lambda ()
+                        (golden-ratio))))
 
 ;;;###autoload
 (define-minor-mode golden-ratio-mode
     "Enable automatic window resizing with golden ratio."
   :lighter " Golden"
-  :global t)
+  :global t
+  (if golden-ratio-mode
+      (progn
+        (add-hook 'window-configuration-change-hook 'golden-ratio)
+        (add-hook 'post-command-hook 'golden-ratio--post-command-hook)
+        (add-hook 'mouse-leave-buffer-hook 
'golden-ratio--mouse-leave-buffer-hook)
+        (ad-activate 'other-window)
+        (ad-activate 'pop-to-buffer))
+      (remove-hook 'window-configuration-change-hook 'golden-ratio)
+      (remove-hook 'post-command-hook 'golden-ratio--post-command-hook)
+      (remove-hook 'mouse-leave-buffer-hook 
'golden-ratio--mouse-leave-buffer-hook)
+      (ad-deactivate 'other-window)
+      (ad-deactivate 'pop-to-buffer)))
 
 
 (provide 'golden-ratio)

Reply via email to