unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ehud Karni" <ehud@unix.mvs.co.il>
Cc: help-gnu-emacs@gnu.org
Subject: Re: New balance-windows
Date: Sun, 7 Aug 2005 00:05:54 +0300	[thread overview]
Message-ID: <200508062105.j76L5sj4006926@beta.mvs.co.il> (raw)
In-Reply-To: <87vf2juij1.fsf@thalassa.informatimago.com> (message from Pascal Bourguignon on Sat, 06 Aug 2005 18:39:14 +0200)

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

  parent reply	other threads:[~2005-08-06 21:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <pan.2005.08.03.00.47.36.497219@debain.org>
     [not found] ` <87pssv3kai.fsf@thalassa.informatimago.com>
     [not found]   ` <1123035204.009217.187300@g14g2000cwa.googlegroups.com>
     [not found]     ` <87fytr3ea2.fsf@thalassa.informatimago.com>
2005-08-06 11:59       ` New balance-windows (Was Re: Making the width of three windows equal) Ehud Karni
2005-08-06 13:27         ` New balance-windows Ehud Karni
2005-08-07 17:15         ` New balance-windows (Was Re: Making the width of three windows equal) Richard M. Stallman
2005-08-08  9:27           ` Ehud Karni
2005-08-09  0:27             ` Richard M. Stallman
2005-08-10  0:05             ` New balance-windows Stefan Monnier
2005-08-10  1:48               ` Stefan Monnier
     [not found]       ` <mailman.2754.1123329756.20277.help-gnu-emacs@gnu.org>
     [not found]         ` <87vf2juij1.fsf@thalassa.informatimago.com>
2005-08-06 21:05           ` Ehud Karni [this message]
2005-08-06 23:22             ` Lennart Borgman
2005-08-07  2:17           ` Lennart Borgman
2005-08-08  9:36             ` Ehud Karni
     [not found]             ` <mailman.2988.1123495783.20277.help-gnu-emacs@gnu.org>
     [not found]               ` <85wtmw4tpu.fsf@lola.goethe.zz>
2005-08-08 11:18                 ` Ehud Karni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200508062105.j76L5sj4006926@beta.mvs.co.il \
    --to=ehud@unix.mvs.co.il \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).