3

How do I tell if more than one window is open programmatically? (I am looking for an elisp function/snippet or variable I can check)

(I want to open a REPL in another other window if one exists, otherwise create a new window and open it there.)

  • I mean an emacs window, not an emacs frame. They mean the opposite of what they usually do (in eg webbrowsers). – 24HrRevengeTherapist Oct 18 '15 at 13:33
  • 2
    I'd love to see a patch to Emacs which adds support for wind sensors to detect when more than one window is open. – Stefan Nov 25 '15 at 19:50
9

You can use the built-in function one-window-p.


From C-h S one-window-p:

 -- Function: one-window-p &optional no-mini all-frames
     This function returns ‘t’ if the selected window is the only live
     window, and ‘nil’ otherwise.

     If the minibuffer window is active, it is normally considered (so
     that this function returns ‘nil’).  However, if the optional
     argument NO-MINI is non-‘nil’, the minibuffer window is ignored
     even if active.  The optional argument ALL-FRAMES has the same
     meaning as for ‘next-window’.
1

You can use frame-first-window' andnext-window', for example:

(let ((first (frame-first-window)))
  (eq first (next-window first)))
  • 2
    or built-in function one-window-p. – xuchunyang Oct 18 '15 at 14:29
  • @xuchunyang, post it as an answer, this should be the accepted one. – Mark Karpov Oct 18 '15 at 14:38
  • This does not really answer the question as posed, which does not ask whether the selected frame has only one window, but rather (presumably) whether the Emacs session has only one window. one-window-p is a good answer because its second argument lets you choose which frames to check, including all. – Drew Oct 18 '15 at 16:09
  • @xuchunyang, Ohh, I didn't know about that one -- it's a way better answer than the one I gave. Drew, from the comments I think it's clear that the user wants to work in the same frame. If not, it's trivial to rework the posted code to work with multiple frames. – Lindydancer Oct 19 '15 at 7:52
1

This might work:

(defun more-than-one-window-p ()
  (cdr (window-list)))

This will return non-nil value if there are more than one window (in selected frame).

  • 1
    This does not really answer the question as posed, which does not say anything about checking only the selected frame. If you provide an arg to your function and pass it to window-list then it can provide an answer to the question as posed. – Drew Oct 18 '15 at 16:11
  • @Drew, Well, as posted, the question says nothing about frames at all, re-read it. So, to answer the question, we can assume selected frame. – Mark Karpov Oct 18 '15 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.