roman / golden-ratio.el

Automatic resizing of Emacs windows to the golden ratio
MIT License
590 stars 38 forks source link

Erratic horizontal resizing behavior #14

Closed zane closed 10 years ago

zane commented 11 years ago

If the intention is for horizontal siblings of the focused window to all have the same width that behavior appears to be broken for certain frame width / window combinations. Here we have three windows in a frame with a width of 239 (focused window on the right):

Note that the focused window is the right size, but the middle window is abnormally small.

This function and advice appears to fix the behavior by balancing the widths of all the focused window's siblings from the outside in (order matters, otherwise you wind up resizing the focused window as well). I imagine there's a more elegant solution, but this is what worked for me:

(defun zane/window-siblings-at-side-list (side &optional window)
  (let ((window (or window (selected-window))))
    (let ((next (cond ((eq side 'left)
                       (window-left window))
                      ((eq side 'right)
                       (window-right window)))))
      (if (eq next nil)
          nil
        (cons next
              (zane/window-siblings-at-side-list side next))))))

(defun zane/balance-sibling-widths (&optional window)
  "Even widths of all provided windows other than the selected window."
  (let* ((window (or window (selected-window)))
         (siblings (append (reverse (zane/window-siblings-at-side-list 'left window))
                           (reverse (zane/window-siblings-at-side-list 'right window))))
         (total-width (apply '+ (mapcar 'window-width siblings))))
    (dolist (sibling siblings)
      (condition-case nil
          (window-resize sibling
                         (- (/ total-width (length siblings))
                            (window-width sibling))
                         t)
        (error nil)))))

(defadvice golden-ratio
  (after horizontally-balance-after-golden)
  (zane/balance-sibling-widths))

(ad-activate 'golden-ratio)

Here is the desired effect of the above advice on a frame with the same width/number of windows:

roman commented 11 years ago

@zane, Uhmm annoying... In my current version of emacs (24.1.1) this doesn't happen, I tested on a different box with emacs (23.3.1) and the issue is there, which version of emacs are you using? Your solution works really well for simple setups (3 windows or so), have you tried with more complex ones.

zane commented 11 years ago

@roman I'm running 24.2.1. The hack above only really works for fairly trivial window arrangements, sadly. I think the issue has something to do with the order in which the windows are resized, but I haven't figured out the details yet.

zane commented 11 years ago

@roman I don't think I communicated this well, but it's very much dependent on the width of the frame. Works fine for me even on 24.2.1 for most widths.

syl20bnr commented 10 years ago

@zane I'm not sure if this can fix your issue (screenshots of OP are not available anymore) but you can take a look at #25.

zane commented 10 years ago

@syl20bnr Seems to! Let's close this.

syl20bnr commented 10 years ago

@zane see #25 updates. There is a limitation with restored sessions...