help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: New balance-windows


From: Ehud Karni
Subject: Re: New balance-windows
Date: Sun, 7 Aug 2005 00:05:54 +0300

On Sat, 06 Aug 2005 18:39:14 +0200, Pascal Bourguignon wrote:
>
> "Ehud Karni" <ehud@unix.mvs.co.il> writes:
>
> > Following the recent discussion of `balance-windows' on help-gnu-emacs
> > I looked into it and wrote a replacement (new) function based on
> > somewhat different logic to achieve the balancing.
>

I think it is good as it can get until Emacs resizing is changed.

In Emacs 22.0.50 a 3rd argument was add to enlarge window:
    Optional third arg PRESERVE-BEFORE, if non-nil, means do not
    change the size of the siblings above or to the left of the
    selected window. Only siblings to the right or below are changed.

I tried using this argument but I found a problem (bug ?).
When you have a configuration like:

    +----------+----------+
    +   lw1    +   rw1    +
    +----------+          +
    +   lw2    +----------+
    +----------+   rw2    +
    +   lw3    +          +
    +----------+----------+
    +         fw6         +
    +----------+----------+

You can move the border between windows lw3/rw2 and fw6 with
enlarge-window command in lw3/rw2. i.e. the command
(enlarge-window 5 nil t) in lw3/rw2 does nothing.

Because the preserving is regarding only windows to the left and
above the current window (and there is no other way to specify the
size of a window) the "enlarging/shrinking" must be done from top
to bottom or left to right (which my function does).

Below is my latest version (using PRESERVE-BEFORE - available only
in 22.0) which is failing in such situation.

NOTE. This discussion really should be done in emacs-devel@gnu.org
list and not in help-gnu-emacs@gnu.org.

Ehud.


The lines with "^ ;; (" are for debugging, just drop the ";;" and
you'll see animation of the resizing with messages in the echo area.


(defun balance-windows (&optional horizontally)
  "Make all visible windows on the current frame the same size (approximately).
If optional prefix arg is not given, \"same size\" is same height.
When prefix arg is given,  \"same size\" is same width."
  (interactive "P")
  (let* (count size w cmjr resize
         (edge (if horizontally 0 1))  ;; Minor field to sort by 0=LEFT, 1=TOP
         (mjr (- 1 edge))              ;; Major field to sort
         (far (+ 2 edge))              ;; far edge (right/bottom) - for current 
size
         (windows nil)                 ;; list of windows
         (ix 0)
         nwin                          ;; number of windows
         (curw (selected-window))      ;; selected window (to return to)
         )
    ;; Build and sort list of all windows on frame
       (save-window-excursion
           (walk-windows (function (lambda (w)
                               (let ((ltrb (window-edges w)))
                                   (setq windows (cons (list
                                       (nth mjr  ltrb)
                                       (nth edge ltrb)
                                       (nth far  ltrb)
                                       w) windows)))))
                         'nomini)
           (setq windows (sort windows (lambda (e1 e2)
                                         (if (< (nth 0 e1) (nth 0 e2))
                                               t
                                           (if (= (nth 0 e1) (nth 0 e2))
                                               (if (< (nth 1 e1) (nth 1 e2))
                                                   t)))))))
       (setq nwin (length windows))
       ;; add 1 extra entry (for while check)
       (setq windows (append windows '((-1 -1 -1 nil))))

       (while (< ix nwin)                      ; walk on all (sorted) windows
           (setq count ix)                     ; count the windows in 1 column 
(or row)
           (setq cmjr (car (nth ix windows)))  ; column / raw identification
           (while (= cmjr (car (nth ix windows)))  ; same column / row
               (setq ix (1+ ix)))              ; next window
           (setq count (- ix count))
           (if (/= count 1)                    ; do only if more than one 
window in this column/row
               (let ((gix (- ix count)))
                 (setq size (- (nth far (window-edges (nth 3 (nth (1- ix) 
windows))))
                               (nth edge (window-edges (nth 3 (nth (- ix count) 
windows))))))
                 (setq size (/ (+ size count -1) count)) ; average window size

 ;; (message "Size=%d" size)

                 (while (< gix ix)
                   (setq w (nth 3 (nth gix windows)))
                   (setq resize (- size (- (nth far (window-edges w))
                                           (nth edge (window-edges w)))))

 ;; (message "Window=%s  resize=%d" w resize)
                   ; don't resize by 1 character/line
                   (if (or (> resize 1)
                           (< resize -1))
                       (progn

 ;; (sit-for 2)

                         (select-window w)       ; window to work on
                         (enlarge-window resize horizontally 'preserve)
 ;; (sit-for 2)
                         ))
                   (setq gix (1+ gix))))))

 ;; (message "")
       (select-window curw)))



--
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 GnuPG: 98EA398D <http://www.keyserver.net/>    Better Safe Than Sorry




reply via email to

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