[comp.emacs] How many windows?

jpdres10@usl-pc.UUCP (Green Eric Lee) (11/04/87)

Is there any way to find out how many windows are on the screen in a
Elisp program (GNU Emacs version 18.??). I have a program that I want
to automatically split the screen into two windows, ONLY two windows,
that is, if we already have two windows on the screen, we DON'T want
to split into THREE windows.

--
Eric Green  elg@usl.CSNET       from BEYOND nowhere:
{ihnp4,cbosgd}!killer!elg,      P.O. Box 92191, Lafayette, LA 70509
{ut-sally,killer}!usl!elg     "there's someone in my head, but it's not me..."

jr@LF-SERVER-2.BBN.COM (John Robinson) (11/09/87)

>> Is there any way to find out how many windows are on the screen in a
>> Elisp program (GNU Emacs version 18.??).

Yes. There is a lisp structure that tracks what buffers are visible.

>> I have a program that I want
>> to automatically split the screen into two windows, ONLY two windows,
>> that is, if we already have two windows on the screen, we DON'T want
>> to split into THREE windows.

Emacs' functions that pop up windows generally won't go from two to
three windows.  For example:

 odisplay-buffer:
 Make BUFFER appear in some window but don't select it.
 BUFFER can be a buffer or a buffer name.
 If BUFFER is shown already in some window, just uses that one,
 unless the window is the selected window and NOTTHISWINDOW is non-nil.
 Returns the window displaying BUFFER.

and

 pop-to-buffer:
 Select buffer BUFFER in some window, preferably a different one.
 If  pop-up-windows  is non-nil, windows can be split to do this.
 If second arg  OTHER-WINDOW is non-nil, insist on finding another
 window even if BUFFER is already visible in the selected window.

The mh-e package (lisp/mh-e.el) has a fairly crufty example of how to
arrange the screen to have a particular appearance:

    ;; These contortions are to force the summary line to be the top window.
    (switch-to-buffer-other-window folder)
    (delete-other-windows)
    (switch-to-buffer-other-window mh-show-buffer)
    (switch-to-buffer-other-window folder)
    (shrink-window (- (window-height) mh-summary-height))
    (recenter 1)

which makes the top window be mh-summary-height on buffer "folder",
and the rest of the screen (except the minibuffer) be on the buffer
"mh-show-buffer".

/jr
jr@bbn.com or jr@bbn.nLro

jr@LF-SERVER-2.BBN.COM (John Robinson) (11/09/87)

>> Emacs' functions that pop up windows generally won't go from two to
>> three windows.  For example:
>> 
>>  odisplay-buffer:
>>  Make BUFFER appear in some window but don't select it.
>>  BUFFER can be a buffer or a buffer name.

Of course, that should have been "display-buffer".  I hate Mondays.

/jr
jr@bbn.com or jr@bbn.uucp

kyle@xanth.UUCP (Kyle Jones) (11/10/87)

In article <231@usl-pc.UUCP>, jpdres10@usl-pc.UUCP (Green Eric Lee) writes:
> Is there any way to find out how many windows are on the screen in a
> Elisp program (GNU Emacs version 18.??).

Yes, you could iterate through all the windows with
   (select-window (next-window))
and count them, but that's too much work for what you're trying
to do...

> I have a program that I want to automatically split the screen
> into two windows, ONLY two windows, that is, if we already have two
> windows on the screen, we DON'T want to split into THREE windows.

Use (one-window-p):

one-window-p:
Returns non-nil if there is only one window.
Optional arg NOMINI non-nil means don't count the minibuffer
even if it is active.

So if (one-window-p) returns non-nil, split the window, otherwise don't.

You also might want to look at the documentation for the `pop-up-windows' and
`split-height-threshold' variables.

kyle jones  <kyle@odu.edu>  old dominion university, norfolk, va  usa

beede@hubcap.UUCP (Mike Beede) (11/10/87)

in article <231@usl-pc.UUCP>, jpdres10@usl-pc.UUCP (Green Eric Lee) says:
> 
> Is there any way to find out how many windows are on the screen in a
> Elisp program (GNU Emacs version 18.??). I have a program that I want
> to automatically split the screen into two windows, ONLY two windows,
> that is, if we already have two windows on the screen, we DON'T want
> to split into THREE windows.

Since there is probably a more elegant (or maybe even built-in) way
to do this, I post this code for comment/correction/flaming . . .

(defun num-windows nil
 "Return number of open windows"
 (save-window-excursion
   (num-w)
))

(defune num-w nil "Don't call this!"
 (cond ((one-window-p) 1)
     (t (delete-window) (1+ (num-w)))
))

Another way to approach this would be to step through the windows
until arriving back at the start.

And while I'm at it, this is to balance the height of open windows, since
I needed it for my program, where I didn't want more than n open windows--
all the same size (roughly):

(defun balance-windows nil
 "Make windows' heights balanced"
 (let ((size (/ (screen-height) (num-windows))
       (n (num-windows)) (i 1))
   (while (<= i n)
     (if (< (window-height) size)
         (enlarge-window (- size (window-height)))
         (if (> (window-height) size)
             (shrink-window (- (window-height) size))
         )
     )
     (setq i (1+ i))
     (other-window 1)
   )
))

-- 
Mike Beede                      
Computer Science Dept.          UUCP: . . . !hubcap!beede
Clemson University              INET: beede@hubcap.clemson.edu
Clemson SC 29634-1906           YOUR DIME: (803)656-{2845,3444}

ram-ashwin@YALE.ARPA (Ashwin Ram) (11/11/87)

>                                             I have a program that I want
>    to automatically split the screen into two windows, ONLY two windows,
>    that is, if we already have two windows on the screen, we DON'T want
>    to split into THREE windows.

The function one-window-p returns t iff there is only one window on the
screen.

-- Ashwin Ram --

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,linus,seismo}!yale!Ram-Ashwin
BITNET:  Ram@yalecs