unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Ehud Karni" <ehud@unix.mvs.co.il>
Cc: knipknap@jabber.org, help-gnu-emacs@gnu.org, spam@mouse-potato.com
Subject: New balance-windows (Was Re: Making the width of three windows equal)
Date: Sat, 6 Aug 2005 14:59:18 +0300	[thread overview]
Message-ID: <200508061159.j76BxILQ018985@beta.mvs.co.il> (raw)
In-Reply-To: <87fytr3ea2.fsf@thalassa.informatimago.com> (message from Pascal Bourguignon on Wed, 03 Aug 2005 05:11:49 +0200)

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.

The logic is this: group all windows that has the same "left" (or "top"
for horizontal) edge, and balance their height (width).

The actual resizing is dependent on the window structure.
e.g. the two symmetrical window structures below will be balanced
differently (the diagrams are after vertical balancing):

       +-------+--------+             +-------+--------+
       |       |        |             |       |        |
       | 1 / 3 |        |             |       | 1 / 4  |
       |       |        |             | 1 / 2 +--------+
       +-------|  2 / 3 |             |       |        |
       |       |        |             |       | 1 / 4  |
       | 1 / 3 |        |             +-------+--------+
       |       |        |             |                |
       +-------+--------+             |                |
       |                |             |     1 / 2      |
       |     1 / 3      |             |                |
       |                |             |                |
       +-------+--------+             +-------+--------+

On the other hand if the windows are more "normally" built it works
fine (diagrams after horizontal balancing):

    +-----+-----+------+----+    +-------+-------+-------+
    | 1/4 | 1/4 | 1/4 | 1/4 |    | 1 / 3 | 1 / 3 | 1 / 3 |
    |     |     |     |     |    |       |       |       |
    +-----+-+---+---+-+-----+    +-----+-+---+---+-+-----+
    |       |       |       |    |     |     |     |     |
    | 1 / 3 | 1 / 3 | 1 / 3 |    | 1/4 | 1/4 | 1/4 | 1/4 |
    +-------+-------+-------+    +-----+-----+------+----+

To balance horizontally just add a prefix argument (i.e. C-u).

Note. I don't have CVS write permission, so if some of the developers
find this as adequate replacement to `balance-windows' please add it.


Ehud.


(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 max rpt
         (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)
         (windows nil)                 ;; list of windows
         (ix 0)
         nwin                          ;; number of windows
         (pass 1)                      ;; pass number
         (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 2 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 0)                      ; number of windows in 1 column (or row)
           (setq cmjr (car (nth ix windows)))  ; column / raw identification
           (while (= cmjr (car (nth (+ count ix) windows)))    ; same
               (setq count (1+ count)))        ; count them
           (if (= count 1)                     ; only one window in this column/row
               (setq ix (1+ ix))               ; skip it
             ; compute and resize windows
             (setq size (if (= pass 1)         ; on pass 1 the saved edges have not changed
                          (- (nth 2 (nth (1- (+ count ix)) windows))
                             (nth 1 (nth ix windows)))
                        ;; previous changes may changed the window edges
                        (- (nth far (window-edges (nth 3 (nth (1- (+ count ix)) windows))))
                           (nth edge (window-edges (nth 3 (nth ix windows)))))))
             (setq size (/ (+ size count -1) count)) ; average window size

             (setq max (+ ix count))           ; index of next column/row
             ;; the resizing loop must be done twice
             ;; because later change may resize previous window
             (setq rpt 2)
             (while (> rpt 0)
               (setq rpt (1- rpt))
               (while (< ix max)
                 (setq w (nth 3 (nth ix windows)))
                 (setq resize (- size (- (nth far (window-edges w))
                                         (nth edge (window-edges w)))))
                 ; don't resize by 1 character
                 (if (or (> resize 1)
                         (< resize -1))
                     (progn
                       (select-window w)       ; window to work on
                       (enlarge-window resize horizontally)))
                 (setq ix (1+ ix)))
               (setq ix (- max count)))
             (setq ix max)
             (setq pass 2)))
           (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	other threads:[~2005-08-06 11:59 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-03  0:47 Making the width of three windows equal Samuel
2005-08-03  1:01 ` Pascal Bourguignon
2005-08-03  2:13   ` Samuel
2005-08-03  3:11     ` Pascal Bourguignon
2005-08-06 11:59       ` Ehud Karni [this message]
2005-08-06 13:27         ` New balance-windows Ehud Karni
     [not found]         ` <mailman.2758.1123335041.20277.help-gnu-emacs@gnu.org>
2005-08-06 16:42           ` Pascal Bourguignon
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.3158.1123633927.20277.help-gnu-emacs@gnu.org>
2005-08-21  0:33               ` David Combs
     [not found]           ` <mailman.2979.1123493766.20277.help-gnu-emacs@gnu.org>
2005-08-08 17:55             ` Pascal Bourguignon
     [not found]         ` <mailman.2888.1123436059.20277.help-gnu-emacs@gnu.org>
2005-08-07 18:31           ` Pascal Bourguignon
     [not found]       ` <mailman.2754.1123329756.20277.help-gnu-emacs@gnu.org>
2005-08-06 16:39         ` Pascal Bourguignon
2005-08-06 16:58           ` Lennart Borgman
     [not found]           ` <mailman.2773.1123347813.20277.help-gnu-emacs@gnu.org>
2005-08-06 20:45             ` Pascal Bourguignon
2005-08-06 21:14               ` Lennart Borgman
     [not found]               ` <mailman.2796.1123363907.20277.help-gnu-emacs@gnu.org>
2005-08-07 18:23                 ` Pascal Bourguignon
2005-08-07 18:59                   ` Lennart Borgman
     [not found]                   ` <mailman.2899.1123441421.20277.help-gnu-emacs@gnu.org>
2005-08-07 20:42                     ` Pascal Bourguignon
2005-08-06 21:05           ` Ehud Karni
2005-08-07  2:17           ` Lennart Borgman
2005-08-08  9:36             ` Ehud Karni
2005-08-08  9:47               ` Lennart Borgman
     [not found]             ` <mailman.2988.1123495783.20277.help-gnu-emacs@gnu.org>
2005-08-08 10:18               ` David Kastrup
2005-08-08 11:18                 ` Ehud Karni
2005-08-08 12:05                   ` Lennart Borgman
     [not found]                 ` <mailman.3002.1123500982.20277.help-gnu-emacs@gnu.org>
2005-08-08 18:11                   ` Pascal Bourguignon

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=200508061159.j76BxILQ018985@beta.mvs.co.il \
    --to=ehud@unix.mvs.co.il \
    --cc=help-gnu-emacs@gnu.org \
    --cc=knipknap@jabber.org \
    --cc=spam@mouse-potato.com \
    /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.
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).