(construct_menu_click, construct_mouse_click):
[bpt/emacs.git] / src / window.c
index 3b2c123..b253f7d 100644 (file)
@@ -1,6 +1,6 @@
 /* Window creation, deletion and examination for GNU Emacs.
    Does not include redisplay.
-   Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1993, 1994 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-#include "config.h"
+#include <config.h>
 #include "lisp.h"
 #include "buffer.h"
 #include "frame.h"
@@ -29,12 +29,12 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "disptab.h"
 #include "keyboard.h"
 
-Lisp_Object Qwindowp;
+Lisp_Object Qwindowp, Qwindow_live_p;
 
 Lisp_Object Fnext_window (), Fdelete_window (), Fselect_window ();
 Lisp_Object Fset_window_buffer (), Fsplit_window (), Frecenter ();
 
-static void delete_all_subwindows ();
+void delete_all_subwindows ();
 static struct window *decode_window();
 
 /* This is the window in which the terminal's cursor should
@@ -60,12 +60,6 @@ Lisp_Object Vminibuf_scroll_window;
 /* Non-nil means this is the buffer whose window C-M-v should scroll.  */
 Lisp_Object Vother_window_scroll_buffer;
 
-/* Window that the mouse is over (nil if no mouse support).  */
-Lisp_Object Vmouse_window;
-
-/* Last mouse click data structure (nil if no mouse support).  */
-Lisp_Object Vmouse_event;
-
 /* Non-nil means it's function to call to display temp buffers.  */
 Lisp_Object Vtemp_buffer_show_function;
 
@@ -105,6 +99,16 @@ DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
   return XTYPE (obj) == Lisp_Window ? Qt : Qnil;
 }
 
+DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
+  "Returns t if OBJ is a window which is currently visible.")
+     (obj)
+     Lisp_Object obj;
+{
+  return ((XTYPE (obj) == Lisp_Window
+          && ! NILP (XWINDOW (obj)->buffer))
+         ? Qt : Qnil);
+}
+
 Lisp_Object
 make_window ()
 {
@@ -141,17 +145,24 @@ DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
   return selected_window;
 }
 
-DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 0, 0,
-  "Return the window used now for minibuffers.")
-  ()
+DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
+  "Return the window used now for minibuffers.\n\
+If the optional argument FRAME is specified, return the minibuffer window\n\
+used by that frame.")
+  (frame)
+    Lisp_Object frame;
 {
 #ifdef MULTI_FRAME
-  choose_minibuf_frame ();
-#endif /* MULTI_FRAME */
-  return minibuf_window;
+  if (NILP (frame))
+    XSET (frame, Lisp_Frame, selected_frame);
+  else
+    CHECK_LIVE_FRAME (frame, 0);
+#endif
+
+  return FRAME_MINIBUF_WINDOW (XFRAME (frame));
 }
 
-DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 1, 1, 0,
+DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
   "Returns non-nil if WINDOW is a minibuffer window.")
   (window)
      Lisp_Object window;
@@ -174,6 +185,7 @@ POS defaults to point; WINDOW, to the selected window.")
   register int posint;
   register struct buffer *buf;
   struct position posval;
+  int hscroll;
 
   if (NILP (pos))
     posint = point;
@@ -183,12 +195,9 @@ POS defaults to point; WINDOW, to the selected window.")
       posint = XINT (pos);
     }
 
-  if (NILP (window))
-    window = selected_window;
-  else
-    CHECK_WINDOW (window, 1);
-  w = XWINDOW (window);
+  w = decode_window (window);
   top = marker_position (w->start);
+  hscroll = XINT (w->hscroll);
 
   if (posint < top)
     return Qnil;
@@ -207,15 +216,14 @@ POS defaults to point; WINDOW, to the selected window.")
     }
   else
     {
-      if (posint > BUF_Z (buf))
+      if (posint > BUF_ZV (buf))
        return Qnil;
 
       /* If that info is not correct, calculate afresh */
-      posval = *compute_motion (top, 0, 0, posint, height, 0,
-                               XFASTINT (w->width) - 1
-                               - (XFASTINT (w->width) + XFASTINT (w->left)
-                                  != FRAME_WIDTH (XFRAME (w->frame))),
-                               XINT (w->hscroll), 0);
+      posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0),
+                               posint, height, 0,
+                               window_internal_width (w) - 1,
+                               hscroll, 0);
 
       return posval.vpos < height ? Qt : Qnil;
     }
@@ -228,7 +236,7 @@ decode_window (window)
   if (NILP (window))
     return XWINDOW (selected_window);
 
-  CHECK_WINDOW (window, 0);
+  CHECK_LIVE_WINDOW (window, 0);
   return XWINDOW (window);
 }
 
@@ -249,18 +257,14 @@ DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
 }
 
 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
-  "Return the number of columns in WINDOW.")
+  "Return the number of display columns in WINDOW.\n\
+This is the width that is usable columns available for text in WINDOW.\n\
+If you want to find out how many columns WINDOW takes up,\n\
+use  (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
   (window)
      Lisp_Object window;
 {
-  register struct window *w = decode_window (window);
-  register int width = w->width;
-
-  /* If this window does not end at the right margin,
-     must deduct one column for the border */
-  if ((width + w->left) == FRAME_WIDTH (XFRAME (WINDOW_FRAME (w))))
-    return width;
-  return width - 1;
+  return make_number (window_internal_width (decode_window (window)));
 }
 
 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
@@ -284,7 +288,7 @@ NCOL should be zero or positive.")
   if (XFASTINT (ncol) >= (1 << (SHORTBITS - 1)))
     args_out_of_range (ncol, Qnil);
   w = decode_window (window);
-  if (w->hscroll != ncol)
+  if (XINT (w->hscroll) != XINT (ncol))
     clip_changed = 1;          /* Prevent redisplay shortcuts */
   w->hscroll = ncol;
   return ncol;
@@ -333,7 +337,7 @@ coordinates_in_window (w, x, y)
 
   /* Is the character is the mode line?  */
   if (*y == top + window_height - 1
-      && window_height > 1)    /* 1 line => minibuffer */
+      && ! MINI_WINDOW_P (w))
     return 2;
   
   /* Is the character in the right border?  */
@@ -363,7 +367,7 @@ If they are on the border between WINDOW and its right sibling,\n\
 {
   int x, y;
 
-  CHECK_WINDOW (window, 0);
+  CHECK_LIVE_WINDOW (window, 0);
   CHECK_CONS (coordinates, 1);
   x = XINT (Fcar (coordinates));
   y = XINT (Fcdr (coordinates));
@@ -420,24 +424,26 @@ window_from_coordinates (frame, x, y, part)
 }
 
 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
-  "Return window containing row ROW, column COLUMN on FRAME.\n\
+  "Return window containing coordinates X and Y on FRAME.\n\
 If omitted, FRAME defaults to the currently selected frame.\n\
 The top left corner of the frame is considered to be row 0,\n\
 column 0.")
-  (row, column, frame)
-      Lisp_Object row, column, frame;
+  (x, y, frame)
+      Lisp_Object x, y, frame;
 {
   int part;
 
+#ifdef MULTI_FRAME
   if (NILP (frame))
     XSET (frame, Lisp_Frame, selected_frame);
   else
     CHECK_LIVE_FRAME (frame, 2);
-  CHECK_NUMBER (row, 0);
-  CHECK_NUMBER (column, 1);
+#endif
+  CHECK_NUMBER (x, 0);
+  CHECK_NUMBER (y, 1);
 
   return window_from_coordinates (XFRAME (frame),
-                                 XINT (row), XINT (column),
+                                 XINT (x), XINT (y),
                                  &part);
 }
 
@@ -477,9 +483,13 @@ DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
 {
   Lisp_Object value;
   struct window *w = decode_window (window);
-  
+  Lisp_Object buf;
+
+  buf = w->buffer;
+  CHECK_BUFFER (buf, 0);
+
   XSET (value, Lisp_Int,
-       BUF_Z (current_buffer) - XFASTINT (w->window_end_pos));
+       BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
 
   return value;
 }
@@ -525,7 +535,7 @@ from overriding motion of point in order to display at this exact start.")
 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
        1, 1, 0,
   "Return WINDOW's dedicated object, usually t or nil.\n\
-See also `set-window-buffer-dedicated'.")
+See also `set-window-dedicated-p'.")
   (window)
      Lisp_Object window;
 {
@@ -602,15 +612,25 @@ static
 unshow_buffer (w)
      register struct window *w;
 {
-  Lisp_Object buf = w->buffer;
+  Lisp_Object buf;
 
+  buf = w->buffer;
   if (XBUFFER (buf) != XMARKER (w->pointm)->buffer)
     abort ();
 
+#if 0
   if (w == XWINDOW (selected_window)
       || ! EQ (buf, XWINDOW (selected_window)->buffer))
     /* Do this except when the selected window's buffer
        is being removed from some other window.  */
+#endif
+    /* last_window_start records the start position that this buffer
+       had in the last window to be disconnected from it.
+       Now that this statement is unconditional,
+       it is possible for the buffer to be displayed in the
+       selected window, while last_window_start reflects another
+       window which was recently showing the same buffer.
+       Some people might say that might be a good thing.  Let's see.  */
     XBUFFER (buf)->last_window_start = marker_position (w->start);
 
   /* Point in the selected window's buffer
@@ -634,7 +654,7 @@ replace_window (old, replacement)
   /* If OLD is its frame's root_window, then replacement is the new
      root_window for that frame.  */
 
-  if (old == FRAME_ROOT_WINDOW (XFRAME (o->frame)))
+  if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
     FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
 
   p->left = o->left;
@@ -673,12 +693,21 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
   register struct window *p;
   register struct window *par;
 
+  /* Because this function is called by other C code on non-leaf
+     windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
+     so we can't decode_window here.  */
   if (NILP (window))
     window = selected_window;
   else
     CHECK_WINDOW (window, 0);
-
   p = XWINDOW (window);
+
+  /* It's okay to delete an already-deleted window.  */
+  if (NILP (p->buffer)
+      && NILP (p->hchild)
+      && NILP (p->vchild))
+    return Qnil;
+
   parent = p->parent;
   if (NILP (parent))
     error ("Attempt to delete minibuffer or sole ordinary window");
@@ -686,8 +715,39 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
 
   windows_or_buffers_changed++;
 
-  if (EQ (window, selected_window))
-    Fselect_window (Fnext_window (window, Qnil, Qnil));
+  /* Are we trying to delete any frame's selected window?  */
+  {
+    Lisp_Object frame, pwindow;
+
+    /* See if the frame's selected window is either WINDOW
+       or any subwindow of it, by finding all that window's parents
+       and comparing each one with WINDOW.  */
+    frame = WINDOW_FRAME (XWINDOW (window));
+    pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
+
+    while (!NILP (pwindow))
+      {
+       if (EQ (window, pwindow))
+         break;
+       pwindow = XWINDOW (pwindow)->parent;
+      }
+
+    if (EQ (window, pwindow))
+      {
+       Lisp_Object alternative;
+       alternative = Fnext_window (window, Qlambda, Qnil);
+
+       /* If we're about to delete the selected window on the
+          selected frame, then we should use Fselect_window to select
+          the new window.  On the other hand, if we're about to
+          delete the selected window on any other frame, we shouldn't do
+          anything but set the frame's selected_window slot.  */
+       if (EQ (window, selected_window))
+         Fselect_window (alternative);
+       else
+         FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
+      }
+  }
 
   tem = p->buffer;
   /* tem is null for dummy parent windows
@@ -697,7 +757,6 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
       unshow_buffer (p);
       unchain_marker (p->pointm);
       unchain_marker (p->start);
-      p->buffer = Qnil;
     }
 
   tem = p->next;
@@ -722,8 +781,8 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
         set_window_{height,width} will re-position the sibling's
         children.  */
       sib = p->next;
-      XFASTINT (XWINDOW (sib)->top) = p->top;
-      XFASTINT (XWINDOW (sib)->left) = p->left;
+      XWINDOW (sib)->top = p->top;
+      XWINDOW (sib)->left = p->left;
     }
 
   /* Stretch that sibling.  */
@@ -738,19 +797,33 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
 
   /* If parent now has only one child,
      put the child into the parent's place.  */
-
   tem = par->hchild;
   if (NILP (tem))
     tem = par->vchild;
   if (NILP (XWINDOW (tem)->next))
     replace_window (parent, tem);
+
+  /* Since we may be deleting combination windows, we must make sure that
+     not only p but all its children have been marked as deleted.  */
+  if (! NILP (p->hchild))
+    delete_all_subwindows (XWINDOW (p->hchild));
+  else if (! NILP (p->vchild))
+    delete_all_subwindows (XWINDOW (p->vchild));
+
+  /* Mark this window as deleted.  */
+  p->buffer = p->hchild = p->vchild = Qnil;
+
   return Qnil;
 }
 \f
 
 extern Lisp_Object next_frame (), prev_frame ();
 
-DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
+/* This comment supplies the doc string for `next-window',
+   for make-docfile to see.  We cannot put this in the real DEFUN
+   due to limits in the Unix cpp.
+
+DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
   "Return next window after WINDOW in canonical ordering of windows.\n\
 If omitted, WINDOW defaults to the selected window.\n\
 \n\
@@ -767,7 +840,17 @@ minibuffer does not count, only windows from WINDOW's frame count.\n\
 \n\
 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
-above.  If neither nil nor t, restrict to WINDOW's frame.")
+above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
+Anything else means restrict to WINDOW's frame.\n\
+\n\
+If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
+`next-window' to iterate through the entire cycle of acceptable\n\
+windows, eventually ending up back at the window you started with.\n\
+`previous-window' traverses the same cycle, in the reverse order.")
+  (window, minibuf, all_frames) */
+
+DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
+       0)
   (window, minibuf, all_frames)
      register Lisp_Object window, minibuf, all_frames;
 {
@@ -777,7 +860,7 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
   if (NILP (window))
     window = selected_window;
   else
-    CHECK_WINDOW (window, 0);
+    CHECK_LIVE_WINDOW (window, 0);
 
   start_window = window;
 
@@ -795,8 +878,13 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
                       (WINDOW_FRAME
                        (XWINDOW (window)))))
                   : Qnil);
+  else if (EQ (all_frames, Qvisible))
+    ;
   else if (! EQ (all_frames, Qt))
     all_frames = Qnil;
+  /* Now all_frames is t meaning search all frames,
+     nil meaning search just current frame,
+     or a window, meaning search the frame that window belongs to.  */
 
   /* Do this loop at least once, to get the next window, and perhaps
      again, if we hit the minibuffer and that is not acceptable.  */
@@ -841,12 +929,16 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
      we've come all the way around and we're back at the original window.  */
   while (MINI_WINDOW_P (XWINDOW (window))
         && ! EQ (minibuf, Qt)
-        && window != start_window);
+        && ! EQ (window, start_window));
 
   return window;
 }
 
-DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
+/* This comment supplies the doc string for `previous-window',
+   for make-docfile to see.  We cannot put this in the real DEFUN
+   due to limits in the Unix cpp.
+
+DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
   "Return the window preceeding WINDOW in canonical ordering of windows.\n\
 If omitted, WINDOW defaults to the selected window.\n\
 \n\
@@ -864,7 +956,18 @@ count.\n\
 \n\
 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
-above.  If neither nil nor t, restrict to WINDOW's frame.")
+above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
+Anything else means restrict to WINDOW's frame.\n\
+\n\
+If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
+`previous-window' to iterate through the entire cycle of acceptable\n\
+windows, eventually ending up back at the window you started with.\n\
+`next-window' traverses the same cycle, in the reverse order.")
+  (window, minibuf, all_frames)  */
+
+
+DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
+       0)
   (window, minibuf, all_frames)
      register Lisp_Object window, minibuf, all_frames;
 {
@@ -874,7 +977,7 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
   if (NILP (window))
     window = selected_window;
   else
-    CHECK_WINDOW (window, 0);
+    CHECK_LIVE_WINDOW (window, 0);
 
   start_window = window;
 
@@ -892,8 +995,13 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
                       (WINDOW_FRAME
                        (XWINDOW (window)))))
                   : Qnil);
+  else if (EQ (all_frames, Qvisible))
+    ;
   else if (! EQ (all_frames, Qt))
     all_frames = Qnil;
+  /* Now all_frames is t meaning search all frames,
+     nil meaning search just current frame,
+     or a window, meaning search the frame that window belongs to.  */
 
   /* Do this loop at least once, to get the previous window, and perhaps
      again, if we hit the minibuffer and that is not acceptable.  */
@@ -911,9 +1019,23 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
            tem = WINDOW_FRAME (XWINDOW (window));
 #ifdef MULTI_FRAME
            if (! NILP (all_frames))
-             tem = next_frame (tem, all_frames);
+             /* It's actually important that we use prev_frame here,
+                rather than next_frame.  All the windows acceptable
+                according to the given parameters should form a ring;
+                Fnext_window and Fprevious_window should go back and
+                forth around the ring.  If we use next_frame here,
+                then Fnext_window and Fprevious_window take different
+                paths through the set of acceptable windows.
+                window_loop assumes that these `ring' requirement are
+                met.  */
+             tem = prev_frame (tem, all_frames);
 #endif
-           tem = FRAME_ROOT_WINDOW (XFRAME (tem));
+           /* If this frame has a minibuffer, find that window first,
+              because it is conceptually the last window in that frame.  */
+           if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
+             tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
+           else
+             tem = FRAME_ROOT_WINDOW (XFRAME (tem));
 
            break;
          }
@@ -939,7 +1061,7 @@ above.  If neither nil nor t, restrict to WINDOW's frame.")
      we've come all the way around and we're back at the original window.  */
   while (MINI_WINDOW_P (XWINDOW (window))
         && !EQ (minibuf, Qt)
-        && window != start_window);
+        && !EQ (window, start_window));
 
   return window;
 }
@@ -976,8 +1098,10 @@ argument ALL_FRAMES is non-nil, cycle through all frames.")
 \f
 /* Look at all windows, performing an operation specified by TYPE
    with argument OBJ.
-   If FRAMES is Qt, look at all frames, if Qnil, look at just the selected
-   frame.  If FRAMES is a frame, just look at windows on that frame.
+   If FRAMES is Qt, look at all frames;
+                Qnil, look at just the selected frame;
+               Qvisible, look at visible frames;
+               a frame, just look at windows on that frame.
    If MINI is non-zero, perform the operation on minibuffer windows too.
 */
 
@@ -989,7 +1113,7 @@ enum window_loop
   DELETE_OTHER_WINDOWS,                /* Arg is window not to delete */
   DELETE_BUFFER_WINDOWS,       /* Arg is buffer */
   GET_LARGEST_WINDOW,
-  UNSHOW_BUFFER,               /* Arg is buffer */
+  UNSHOW_BUFFER                /* Arg is buffer */
 };
 
 static Lisp_Object
@@ -1001,9 +1125,12 @@ window_loop (type, obj, mini, frames)
   register Lisp_Object w;
   register Lisp_Object best_window;
   register Lisp_Object next_window;
-  register Lisp_Object first_window;
+  register Lisp_Object last_window;
   FRAME_PTR frame;
+  Lisp_Object frame_arg;
+  frame_arg = Qt;
 
+#ifdef MULTI_FRAME
   /* If we're only looping through windows on a particular frame,
      frame points to that frame.  If we're looping through windows
      on all frames, frame is 0.  */
@@ -1013,41 +1140,52 @@ window_loop (type, obj, mini, frames)
     frame = selected_frame;
   else
     frame = 0;
+  if (frame)
+    frame_arg = Qlambda;
+  else if (EQ (frames, Qvisible))
+    frame_arg = frames;
+#else
+  frame = 0;
+#endif
+
+  /* frame_arg is Qlambda to stick to one frame,
+     Qvisible to consider all visible frames,
+     or Qt otherwise.  */
 
   /* Pick a window to start with.  */
   if (XTYPE (obj) == Lisp_Window)
-    first_window = obj;
+    w = obj;
   else if (frame)
-    first_window = FRAME_SELECTED_WINDOW (frame);
+    w = FRAME_SELECTED_WINDOW (frame);
   else
-    first_window = FRAME_SELECTED_WINDOW (selected_frame);
+    w = FRAME_SELECTED_WINDOW (selected_frame);
+
+  /* Figure out the last window we're going to mess with.  Since
+     Fnext_window, given the same options, is guaranteed to go in a
+     ring, we can just use Fprevious_window to find the last one.
+
+     We can't just wait until we hit the first window again, because
+     it might be deleted.  */
+
+  last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
 
-  w = first_window;
   best_window = Qnil;
-  do
+  for (;;)
     {
+      FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
+
       /* Pick the next window now, since some operations will delete
         the current window.  */
-#ifdef MULTI_FRAME
-      if (frame)
-       next_window = Fnext_window (w, (mini ? Qt : Qnil), Qlambda);
-      else
-#endif /* MULTI_FRAME */
-       /* We know frame is 0, so we're looping through all frames.
-          Or we know this isn't a MULTI_FRAME Emacs, so who cares?  */
-       next_window = Fnext_window (w, mini ? Qt : Qnil, Qt);
+      next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
 
-      if (!MINI_WINDOW_P (XWINDOW (w))
+      /* Note that we do not pay attention here to whether
+        the frame is visible, since Fnext_window skips non-visible frames
+        if that is desired, under the control of frame_arg.  */
+      if (! MINI_WINDOW_P (XWINDOW (w))
          || (mini && minibuf_level > 0))
        switch (type)
          {
          case GET_BUFFER_WINDOW:
-#if 0
-           /* Ignore invisible and iconified frames.  */
-           if (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (w))))
-               || FRAME_ICONIFIED_P (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
-             break;
-#endif
            if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj))
              return w;
            break;
@@ -1055,14 +1193,8 @@ window_loop (type, obj, mini, frames)
          case GET_LRU_WINDOW:
            /* t as arg means consider only full-width windows */
            if (!NILP (obj) && XFASTINT (XWINDOW (w)->width)
-               != FRAME_WIDTH (frame))
-             break;
-#if 0
-           /* Ignore invisible and iconified frames.  */
-           if (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (w))))
-               || FRAME_ICONIFIED_P (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
+               != FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
              break;
-#endif
            /* Ignore dedicated windows and minibuffers.  */
            if (MINI_WINDOW_P (XWINDOW (w))
                || !NILP (XWINDOW (w)->dedicated))
@@ -1085,12 +1217,14 @@ window_loop (type, obj, mini, frames)
                   on the frame, find a new buffer to display there.  */
                if (NILP (XWINDOW (w)->parent))
                  {
-                   Lisp_Object new_buffer = Fother_buffer (obj);
+                   Lisp_Object new_buffer;
+                   new_buffer = Fother_buffer (obj, Qnil);
                    if (NILP (new_buffer))
                      new_buffer
                        = Fget_buffer_create (build_string ("*scratch*"));
                    Fset_window_buffer (w, new_buffer);
-                   Fset_buffer (XWINDOW (w)->buffer);
+                   if (EQ (w, selected_window))
+                     Fset_buffer (XWINDOW (w)->buffer);
                  }
                else
                  Fdelete_window (w);
@@ -1098,12 +1232,6 @@ window_loop (type, obj, mini, frames)
            break;
 
          case GET_LARGEST_WINDOW:
-#if 0
-           /* Ignore invisible and iconified frames.  */
-           if (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (w))))
-               || FRAME_ICONIFIED_P (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
-             break;
-#endif
            /* Ignore dedicated windows and minibuffers.  */
            if (MINI_WINDOW_P (XWINDOW (w))
                || !NILP (XWINDOW (w)->dedicated))
@@ -1123,7 +1251,8 @@ window_loop (type, obj, mini, frames)
            if (EQ (XWINDOW (w)->buffer, obj))
              {
                /* Find another buffer to show in this window.  */
-               Lisp_Object another_buffer = Fother_buffer (obj);
+               Lisp_Object another_buffer;
+               another_buffer = Fother_buffer (obj, Qnil);
                if (NILP (another_buffer))
                  another_buffer
                    = Fget_buffer_create (build_string ("*scratch*"));
@@ -1133,33 +1262,40 @@ window_loop (type, obj, mini, frames)
              }
            break;
          }
+
+      if (EQ (w, last_window))
+       break;
+
       w = next_window;
     }
-  while (! EQ (w, first_window));
 
   return best_window;
 }     
-\f
+
 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
   "Return the window least recently selected or used for display.\n\
-If optional argument FRAMES is t, search all frames.  If FRAME is a\n\
-frame, search only that frame.\n")
-  (frames)
-    Lisp_Object frames;
+If optional argument FRAME is `visible', search all visible frames.\n\
+If FRAME is t, search all frames.\n\
+If FRAME is nil, search only the selected frame.\n\
+If FRAME is a frame, search only that frame.")
+  (frame)
+    Lisp_Object frame;
 {
   register Lisp_Object w;
   /* First try for a window that is full-width */
-  w = window_loop (GET_LRU_WINDOW, Qt, 0, frames);
+  w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
   if (!NILP (w) && !EQ (w, selected_window))
     return w;
   /* If none of them, try the rest */
-  return window_loop (GET_LRU_WINDOW, Qnil, 0, frames);
+  return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
 }
 
 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
   "Return the largest window in area.\n\
-If optional argument FRAMES is t, search all frames.  If FRAME is a\n\
-frame, search only that frame.\n")
+If optional argument FRAME is `visible', search all visible frames.\n\
+If FRAME is t, search all frames.\n\
+If FRAME is nil, search only the selected frame.\n\
+If FRAME is a frame, search only that frame.")
   (frame)
     Lisp_Object frame;
 {
@@ -1169,8 +1305,10 @@ frame, search only that frame.\n")
 
 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
   "Return a window currently displaying BUFFER, or nil if none.\n\
-If optional argument FRAMES is t, search all frames.  If FRAME is a\n\
-frame, search only that frame.\n")
+If optional argument FRAME is `visible', search all visible frames.\n\
+If FRAME is t, search all frames.\n\
+If FRAME is nil, search only the selected frame.\n\
+If FRAME is a frame, search only that frame.")
   (buffer, frame)
     Lisp_Object buffer, frame;
 {
@@ -1184,45 +1322,70 @@ frame, search only that frame.\n")
 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
   0, 1, "",
   "Make WINDOW (or the selected window) fill its frame.\n\
-Only the frame WINDOW is on is affected.")
+Only the frame WINDOW is on is affected.\n\
+This function tries to reduce display jumps\n\
+by keeping the text previously visible in WINDOW\n\
+in the same place on the frame.  Doing this depends on\n\
+the value of (window-start WINDOW), so if calling this function\n\
+in a program gives strange scrolling, make sure the window-start\n\
+value is reasonable when this function is called.")
   (window)
      Lisp_Object window;
 {
   struct window *w;
-  int opoint = point;
   struct buffer *obuf = current_buffer;
+  int opoint;
   int top;
 
   if (NILP (window))
     window = selected_window;
   else
-    CHECK_WINDOW (window, 0);
+    CHECK_LIVE_WINDOW (window, 0);
 
   w = XWINDOW (window);
   top = XFASTINT (w->top);
 
-  window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME(w));
+  window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
 
   Fset_buffer (w->buffer);
+  opoint = point;
   SET_PT (marker_position (w->start));
-  Frecenter (make_number (top));
+  /* Like Frecenter but avoid setting w->force_start.  */
+  Fvertical_motion (make_number (- (top - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w))))),
+                   window);
+  Fset_marker (w->start, make_number (PT), w->buffer);
+  w->start_at_line_beg = Fbolp ();
 
-  set_buffer_internal (obuf);
   SET_PT (opoint);
+  set_buffer_internal (obuf);
   return Qnil;
 }
 
 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
-  1, 1, "bDelete windows on (buffer): ",
-  "Delete all windows showing BUFFER.")
-  (buffer)
-     Lisp_Object buffer;
+  1, 2, "bDelete windows on (buffer): ",
+  "Delete all windows showing BUFFER.\n\
+Optional second argument FRAME controls which frames are affected.\n\
+If nil or omitted, delete all windows showing BUFFER in any frame.\n\
+If t, delete only windows showing BUFFER in the selected frame.\n\
+If `visible', delete all windows showing BUFFER in any visible frame.\n\
+If a frame, delete only windows showing BUFFER in that frame.")
+  (buffer, frame)
+     Lisp_Object buffer, frame;
 {
+#ifdef MULTI_FRAME
+  /* FRAME uses t and nil to mean the opposite of what window_loop
+     expects.  */
+  if (! FRAMEP (frame))
+    frame = NILP (frame) ? Qt : Qnil;
+#else
+  frame = Qt;
+#endif
+
   if (!NILP (buffer))
     {
       buffer = Fget_buffer (buffer);
       CHECK_BUFFER (buffer, 0);
-      window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, Qt);
+      window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
     }
   return Qnil;
 }
@@ -1244,6 +1407,49 @@ DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
 }
 \f
 /* Set the height of WINDOW and all its inferiors.  */
+
+/* The smallest acceptable dimensions for a window.  Anything smaller
+   might crash Emacs.  */
+#define MIN_SAFE_WINDOW_WIDTH  (2)
+#define MIN_SAFE_WINDOW_HEIGHT (2)
+
+/* Make sure that window_min_height and window_min_width are
+   not too small; if they are, set them to safe minima.  */
+
+static void
+check_min_window_sizes ()
+{
+  /* Smaller values might permit a crash.  */
+  if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
+    window_min_width = MIN_SAFE_WINDOW_WIDTH;
+  if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
+    window_min_height = MIN_SAFE_WINDOW_HEIGHT;
+}
+
+/* If *ROWS or *COLS are too small a size for FRAME, set them to the
+   minimum allowable size.  */
+void
+check_frame_size (frame, rows, cols)
+     FRAME_PTR frame;
+     int *rows, *cols;
+{
+  /* For height, we have to see:
+     whether the frame has a minibuffer, 
+     whether it wants a mode line, and
+     whether it has a menu bar.  */
+  int min_height =
+    (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
+     : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
+     : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
+  if (FRAME_MENU_BAR_LINES (frame) > 0)
+    min_height += FRAME_MENU_BAR_LINES (frame);
+
+  if (*rows < min_height)
+    *rows = min_height;
+  if (*cols  < MIN_SAFE_WINDOW_WIDTH)
+    *cols = MIN_SAFE_WINDOW_WIDTH;
+}
+
 /* Normally the window is deleted if it gets too small.
    nodelete nonzero means do not do this.
    (The caller should check later and do so if appropriate)  */
@@ -1259,6 +1465,8 @@ set_window_height (window, height, nodelete)
   int top, pos, lastbot, opos, lastobot;
   Lisp_Object child;
 
+  check_min_window_sizes ();
+
   if (!nodelete
       && ! NILP (w->parent)
       && height < window_min_height)
@@ -1399,6 +1607,9 @@ BUFFER can be a buffer or buffer name.")
     }
 
   w->buffer = buffer;
+  w->window_end_pos = 0;
+  w->window_end_valid = Qnil;
+  w->hscroll = 0;
   Fset_marker (w->pointm,
               make_number (BUF_PT (XBUFFER (buffer))),
               buffer);
@@ -1406,6 +1617,7 @@ BUFFER can be a buffer or buffer name.")
                         make_number (XBUFFER (buffer)->last_window_start),
                         buffer);
   w->start_at_line_beg = Qnil;
+  w->force_start = Qnil;
   XFASTINT (w->last_modified) = 0;
   windows_or_buffers_changed++;
   if (EQ (window, selected_window))
@@ -1424,7 +1636,7 @@ before each command.")
   register struct window *w;
   register struct window *ow = XWINDOW (selected_window);
 
-  CHECK_WINDOW (window, 0);
+  CHECK_LIVE_WINDOW (window, 0);
 
   w = XWINDOW (window);
 
@@ -1443,7 +1655,7 @@ before each command.")
   if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
     {
       XFRAME (WINDOW_FRAME (w))->selected_window = window;
-      Fselect_frame (WINDOW_FRAME (w), Qnil);
+      Fhandle_switch_frame (WINDOW_FRAME (w), Qnil);
     }
   else
     selected_frame->selected_window = window;
@@ -1472,12 +1684,13 @@ before each command.")
 }
 
 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 2,
-       "BDisplay buffer:\nP",
+       "BDisplay buffer: \nP",
   "Make BUFFER appear in some window but don't select it.\n\
 BUFFER can be a buffer or a buffer name.\n\
 If BUFFER is shown already in some window, just use that one,\n\
 unless the window is the selected window and the optional second\n\
-argument NOT_THIS_WINDOW is non-nil.\n\
+argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
+If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
 Returns the window displaying BUFFER.")
   (buffer, not_this_window)
      register Lisp_Object buffer, not_this_window;
@@ -1494,7 +1707,9 @@ Returns the window displaying BUFFER.")
       && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
     return selected_window;
 
-  window = Fget_buffer_window (buffer, Qnil);
+  /* If pop_up_frames,
+     look for a window showing BUFFER on any visible frame.  */
+  window = Fget_buffer_window (buffer, pop_up_frames ? Qvisible : Qnil);
   if (!NILP (window)
       && (NILP (not_this_window) || !EQ (window, selected_window)))
     return window;
@@ -1508,7 +1723,7 @@ Returns the window displaying BUFFER.")
        = Fframe_selected_window (call0 (Vpop_up_frame_function));
       Fset_window_buffer (window, buffer);
 #if 0
-      Fselect_frame (XWINDOW (window)->frame, Qnil);
+      Fhandle_switch_frame (XWINDOW (window)->frame, Qnil);
 #endif
       return window;
     }
@@ -1520,8 +1735,9 @@ Returns the window displaying BUFFER.")
 #endif
       )
     {
-      Lisp_Object frames = Qnil;
-      
+      Lisp_Object frames;
+
+      frames = Qnil;      
 #ifdef MULTI_FRAME
       if (FRAME_MINIBUF_ONLY_P (selected_frame))
        XSET (frames, Lisp_Frame, last_nonminibuf_frame);
@@ -1534,9 +1750,8 @@ Returns the window displaying BUFFER.")
 
       if (!NILP (window)
          && window_height (window) >= split_height_threshold
-         &&
-         (XFASTINT (XWINDOW (window)->width)
-          == FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (window))))))
+         && (XFASTINT (XWINDOW (window)->width)
+             == FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (window))))))
        window = Fsplit_window (window, Qnil, Qnil);
       else
        {
@@ -1633,7 +1848,7 @@ and put SIZE columns in the first of the pair.")
   if (NILP (window))
     window = selected_window;
   else
-    CHECK_WINDOW (window, 0);
+    CHECK_LIVE_WINDOW (window, 0);
 
   o = XWINDOW (window);
 
@@ -1657,11 +1872,7 @@ and put SIZE columns in the first of the pair.")
   else if (FRAME_NO_SPLIT_P (XFRAME (WINDOW_FRAME (o))))
     error ("Attempt to split unsplittable frame");
 
-  /* Smaller values might permit a crash.  */
-  if (window_min_width < 2)
-    window_min_width = 2;
-  if (window_min_height < 2)
-    window_min_height = 2;
+  check_min_window_sizes ();
 
   if (NILP (horflag))
     {
@@ -1769,14 +1980,16 @@ window_width (window)
   return XFASTINT (p->width);
 }
 
-#define MINSIZE(w) \
-  (widthflag ? window_min_width : window_min_height)
+#define MINSIZE(w)                                             \
+  (widthflag                                                   \
+   ? window_min_width                                          \
+   : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
 
 #define CURBEG(w) \
-  *(widthflag ? (int *) &(w)->left : (int *) &(w)->top)
+  *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
 
 #define CURSIZE(w) \
-  *(widthflag ? (int *) &(w)->width : (int *) &(w)->height)
+  *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
 
 /* Unlike set_window_height, this function
    also changes the heights of the siblings so as to
@@ -1795,11 +2008,7 @@ change_window_height (delta, widthflag)
                                   ? set_window_width
                                   : set_window_height);
 
-  /* Smaller values might permit a crash.  */
-  if (window_min_width < 2)
-    window_min_width = 2;
-  if (window_min_height < 2)
-    window_min_height = 2;
+  check_min_window_sizes ();
 
   window = selected_window;
   while (1)
@@ -1818,10 +2027,9 @@ change_window_height (delta, widthflag)
       window = parent;
     }
 
-  sizep = &CURSIZE (p);
+  sizep = &CURSIZE (window);
 
-  if (*sizep + delta < MINSIZE (p)
-      && !NILP (XWINDOW (window)->parent))
+  if (*sizep + delta < MINSIZE (window))
     {
       Fdelete_window (window);
       return;
@@ -1852,7 +2060,7 @@ change_window_height (delta, widthflag)
     {
       (*setsizefun) (p->next, (*sizefun) (p->next) - delta, 0);
       (*setsizefun) (window, *sizep + delta, 0);
-      CURBEG (XWINDOW (p->next)) += delta;
+      CURBEG (p->next) += delta;
       /* This does not change size of p->next,
         but it propagates the new top edge to its children */
       (*setsizefun) (p->next, (*sizefun) (p->next), 0);
@@ -1861,7 +2069,7 @@ change_window_height (delta, widthflag)
           (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
     {
       (*setsizefun) (p->prev, (*sizefun) (p->prev) - delta, 0);
-      CURBEG (p) -= delta;
+      CURBEG (window) -= delta;
       (*setsizefun) (window, *sizep + delta, 0);
     }
   else
@@ -1883,7 +2091,7 @@ change_window_height (delta, widthflag)
 
       /* Add delta1 lines or columns to this window, and to the parent,
         keeping things consistent while not affecting siblings.  */
-      CURSIZE (XWINDOW (parent)) = opht + delta1;
+      CURSIZE (parent) = opht + delta1;
       (*setsizefun) (window, *sizep + delta1, 0);
 
       /* Squeeze out delta1 lines or columns from our parent,
@@ -1920,6 +2128,36 @@ window_internal_height (w)
   return ht;
 }
 
+
+/* Return the number of columns in W.
+   Don't count columns occupied by scroll bars or the vertical bar
+   separating W from the sibling to its right.  */
+int
+window_internal_width (w)
+     struct window *w;
+{
+  FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
+  int left = XINT (w->left);
+  int width = XINT (w->width);
+
+  /* If this window is flush against the right edge of the frame, its
+     internal width is its full width.  */
+  if (left + width >= FRAME_WIDTH (f))
+    return width;
+
+  /* If we are not flush right, then our rightmost columns are
+     occupied by some sort of separator.  */
+
+  /* Scroll bars occupy a few columns.  */
+  if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
+    return width - VERTICAL_SCROLL_BAR_WIDTH;
+
+  /* The column of `|' characters separating side-by-side windows
+     occupies one column only.  */
+  return width - 1;
+}
+
+
 /* Scroll contents of window WINDOW up N lines.  */
 
 void
@@ -1941,7 +2179,7 @@ window_scroll (window, n, noerror)
 
   if (NILP (tem))
     {
-      Fvertical_motion (make_number (- ht / 2));
+      Fvertical_motion (make_number (- (ht / 2)), window);
       XFASTINT (tem) = point;
       Fset_marker (w->start, tem, w->buffer);
       w->force_start = Qt;
@@ -1949,7 +2187,7 @@ window_scroll (window, n, noerror)
 
   SET_PT (marker_position (w->start));
   lose = n < 0 && point == BEGV;
-  Fvertical_motion (make_number (n));
+  Fvertical_motion (make_number (n), window);
   pos = point;
   bolp = Fbolp ();
   SET_PT (opoint);
@@ -1973,11 +2211,11 @@ window_scroll (window, n, noerror)
       if (n < 0)
        {
          SET_PT (pos);
-         tem = Fvertical_motion (make_number (ht));
+         tem = Fvertical_motion (make_number (ht), window);
          if (point > opoint || XFASTINT (tem) < ht)
            SET_PT (opoint);
          else
-           Fvertical_motion (make_number (-1));
+           Fvertical_motion (make_number (-1), window);
        }
     }
   else
@@ -2027,6 +2265,7 @@ scroll_command (n, direction)
 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
   "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
 A near full screen is `next-screen-context-lines' less than a full screen.\n\
+Negative ARG means scroll downward.\n\
 When calling from a program, supply a number as argument or nil.")
   (n)
      Lisp_Object n;
@@ -2038,6 +2277,7 @@ When calling from a program, supply a number as argument or nil.")
 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
   "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
 A near full screen is `next-screen-context-lines' less than a full screen.\n\
+Negative ARG means scroll upward.\n\
 When calling from a program, supply a number as argument or nil.")
   (n)
      Lisp_Object n;
@@ -2047,12 +2287,12 @@ When calling from a program, supply a number as argument or nil.")
 }
 
 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
-  "Scroll text of next window upward ARG lines; or near full screen if no ARG.\n\
+  "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
 The next window is the one below the current one; or the one at the top\n\
-if the current one is at the bottom.\n\
+if the current one is at the bottom.  Negative ARG means scroll downward.\n\
 When calling from a program, supply a number as argument or nil.\n\
 \n\
-If in the minibuffer, `minibuf-scroll-window' if non-nil\n\
+If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
 specifies the window to scroll.\n\
 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
 showing that buffer, popping the buffer up if necessary.")
@@ -2075,9 +2315,21 @@ showing that buffer, popping the buffer up if necessary.")
        window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt);
     }
   else
-    /* Nothing specified; pick a neighboring window.  */
-    window = Fnext_window (selected_window, Qnil, Qt);
-  CHECK_WINDOW (window, 0);
+    {
+      /* Nothing specified; look for a neighboring window on the same
+        frame.  */
+      window = Fnext_window (selected_window, Qnil, Qnil);
+
+      if (EQ (window, selected_window))
+       /* That didn't get us anywhere; look for a window on another
+           visible frame.  */
+       do
+         window = Fnext_window (window, Qnil, Qt);
+       while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
+              && ! EQ (window, selected_window));
+    }
+
+  CHECK_LIVE_WINDOW (window, 0);
 
   if (EQ (window, selected_window))
     error ("There is no other window");
@@ -2104,12 +2356,12 @@ showing that buffer, popping the buffer up if necessary.")
     }
 
   Fset_marker (w->pointm, make_number (point), Qnil);
-  unbind_to (count);
+  unbind_to (count, Qnil);
 
   return Qnil;
 }
 \f
-DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 1, 1, "P",
+DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
   "Scroll selected window display ARG columns left.\n\
 Default for ARG is window width minus 2.")
   (arg)
@@ -2117,7 +2369,7 @@ Default for ARG is window width minus 2.")
 {
 
   if (NILP (arg))
-    XFASTINT (arg) = XFASTINT (XWINDOW (selected_window)->width) - 2;
+    XFASTINT (arg) = window_internal_width (XWINDOW (selected_window)) - 2;
   else
     arg = Fprefix_numeric_value (arg);
 
@@ -2127,14 +2379,14 @@ Default for ARG is window width minus 2.")
                                      + XINT (arg)));
 }
 
-DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 1, 1, "P",
+DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
   "Scroll selected window display ARG columns right.\n\
 Default for ARG is window width minus 2.")
   (arg)
      register Lisp_Object arg;
 {
   if (NILP (arg))
-    XFASTINT (arg) = XFASTINT (XWINDOW (selected_window)->width) - 2;
+    XFASTINT (arg) = window_internal_width (XWINDOW (selected_window)) - 2;
   else
     arg = Fprefix_numeric_value (arg);
 
@@ -2156,6 +2408,7 @@ redraws with point in the center of the current window.")
   register struct window *w = XWINDOW (selected_window);
   register int ht = window_internal_height (w);
   register int opoint = point;
+  Lisp_Object window;
 
   if (NILP (n))
     {
@@ -2179,7 +2432,8 @@ redraws with point in the center of the current window.")
 
   XSETINT (n, - XINT (n));
 
-  Fvertical_motion (n);
+  XSET (window, Lisp_Window, w);
+  Fvertical_motion (n, window);
   Fset_marker (w->start, make_number (point), w->buffer);
   w->start_at_line_beg = Fbolp ();
 
@@ -2201,6 +2455,7 @@ negative means relative to bottom of window.")
   register struct window *w = XWINDOW (selected_window);
   register int height = window_internal_height (w);
   register int start;
+  Lisp_Object window;
 
   if (NILP (arg))
     XFASTINT (arg) = height / 2;
@@ -2212,9 +2467,10 @@ negative means relative to bottom of window.")
     }
 
   start = marker_position (w->start);
+  XSET (window, Lisp_Window, w);
   if (start < BEGV || start > ZV)
     {
-      Fvertical_motion (make_number (- height / 2));
+      Fvertical_motion (make_number (- (height / 2)), window);
       Fset_marker (w->start, make_number (point), w->buffer);
       w->start_at_line_beg = Fbolp ();
       w->force_start = Qt;
@@ -2222,22 +2478,31 @@ negative means relative to bottom of window.")
   else
     SET_PT (start);
 
-  return Fvertical_motion (arg);
+  return Fvertical_motion (arg, window);
 }
 \f
 struct save_window_data
   {
     int size_from_Lisp_Vector_struct;
     struct Lisp_Vector *next_from_Lisp_Vector_struct;
-    Lisp_Object frame_width, frame_height;
+    Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
+    Lisp_Object selected_frame;
     Lisp_Object current_window;
     Lisp_Object current_buffer;
     Lisp_Object minibuf_scroll_window;
     Lisp_Object root_window;
+    Lisp_Object focus_frame;
     /* A vector, interpreted as a struct saved_window */
     Lisp_Object saved_windows;
   };
-#define SAVE_WINDOW_DATA_SIZE 7 /* Arg to Fmake_vector */
+
+/* Arg to Fmake_vector */
+#define SAVE_WINDOW_DATA_SIZE                                          \
+  ((sizeof (struct save_window_data)                                   \
+    - (sizeof (struct Lisp_Vector)                                     \
+       /* Don't count the contents member of the struct Lisp_Vector */ \
+       - sizeof (Lisp_Object)))                                                \
+   / sizeof (Lisp_Object))
 
 /* This is saved as a Lisp_Vector */
 struct saved_window
@@ -2269,165 +2534,212 @@ DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_
 }
 
 
-DEFUN ("set-window-configuration",
-       Fset_window_configuration, Sset_window_configuration,
-       1, 1, 0,
+DEFUN ("set-window-configuration", Fset_window_configuration,
+  Sset_window_configuration, 1, 1, 0,
   "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
 CONFIGURATION must be a value previously returned\n\
 by `current-window-configuration' (which see).")
-     (arg)
-     Lisp_Object arg;
+     (configuration)
+     Lisp_Object configuration;
 {
-  register struct window *w;
   register struct save_window_data *data;
   struct Lisp_Vector *saved_windows;
-  register struct saved_window *p;
-  register Lisp_Object tem;
   Lisp_Object new_current_buffer;
-  int k;
+  Lisp_Object frame;
   FRAME_PTR f;
 
-  while (XTYPE (arg) != Lisp_Window_Configuration)
+  while (XTYPE (configuration) != Lisp_Window_Configuration)
     {
-      arg = wrong_type_argument (intern ("window-configuration-p"), arg);
+      configuration = wrong_type_argument (intern ("window-configuration-p"),
+                                          configuration);
     }
 
-  data = (struct save_window_data *) XVECTOR (arg);
+  data = (struct save_window_data *) XVECTOR (configuration);
   saved_windows = XVECTOR (data->saved_windows);
 
-  f = XFRAME (XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame);
-
-  if (XFASTINT (data->frame_height) != FRAME_HEIGHT (f)
-      || XFASTINT (data->frame_width) != FRAME_WIDTH (f))
-    {
-      /* Presumably something clever could be done.
-        However, it doesn't seem worth the effort */
-      error ("Frame size %dx%d in saved window configuration mismatches frame.",
-            XFASTINT (data->frame_height),
-            XFASTINT (data->frame_width));
-    }
-
-  windows_or_buffers_changed++;
   new_current_buffer = data->current_buffer;
   if (NILP (XBUFFER (new_current_buffer)->name))
     new_current_buffer = Qnil;
 
-  /* Mark all windows now on frame as "deleted".
-     Restoring the new configuration "undeletes" any that are in it.  */
-
-  delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
-#if 0
-  /* This loses when the minibuf frame is not f. */
-  delete_all_subwindows (XWINDOW (XWINDOW (minibuf_window)->prev));
-#endif
+  frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
+  f = XFRAME (frame);
 
-  for (k = 0; k < saved_windows->size; k++)
+  /* If f is a dead frame, don't bother rebuilding its window tree.
+     However, there is other stuff we should still try to do below.  */
+  if (FRAME_LIVE_P (f))
     {
-      p = SAVED_WINDOW_N (saved_windows, k);
-      w = XWINDOW (p->window);
-      w->next = Qnil;
+      register struct window *w;
+      register struct saved_window *p;
+      int k;
+
+      /* If the frame has been resized since this window configuration was
+        made, we change the frame to the size specified in the
+        configuration, restore the configuration, and then resize it
+        back.  We keep track of the prevailing height in these variables.  */
+      int previous_frame_height = FRAME_HEIGHT (f);
+      int previous_frame_width =  FRAME_WIDTH  (f);
+      int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
+
+      if (XFASTINT (data->frame_height) != previous_frame_height
+         || XFASTINT (data->frame_width) != previous_frame_width)
+       change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
+      if (XFASTINT (data->frame_menu_bar_lines)
+         != previous_frame_menu_bar_lines)
+       x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
+
+      windows_or_buffers_changed++;
+
+      /* Kludge Alert!
+        Mark all windows now on frame as "deleted".
+        Restoring the new configuration "undeletes" any that are in it.
+        
+        Save their current buffers in their height fields, since we may
+        need it later, if a buffer saved in the configuration is now
+        dead.  */
+      delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
+
+      for (k = 0; k < saved_windows->size; k++)
+       {
+         p = SAVED_WINDOW_N (saved_windows, k);
+         w = XWINDOW (p->window);
+         w->next = Qnil;
 
-      if (!NILP (p->parent))
-       w->parent = SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window;
-      else
-       w->parent = Qnil;
+         if (!NILP (p->parent))
+           w->parent = SAVED_WINDOW_N (saved_windows,
+                                       XFASTINT (p->parent))->window;
+         else
+           w->parent = Qnil;
 
-      if (!NILP (p->prev))
-       {
-         w->prev = SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window;
-#ifdef MULTI_FRAME
-         /* This is true for a minibuffer-only frame. */
-         if (w->mini_p && EQ (w->prev, p->window))
-           w->next = Qnil;
+         if (!NILP (p->prev))
+           {
+             w->prev = SAVED_WINDOW_N (saved_windows,
+                                       XFASTINT (p->prev))->window;
+             XWINDOW (w->prev)->next = p->window;
+           }
          else
-#endif /* MULTI_FRAME */
-           XWINDOW (w->prev)->next = p->window;
-       }
-      else
-       {
-         w->prev = Qnil;
-         if (!NILP (w->parent))
            {
-             if (EQ (p->width, XWINDOW (w->parent)->width))
-               {
-                 XWINDOW (w->parent)->vchild = p->window;
-                 XWINDOW (w->parent)->hchild = Qnil;
-               }
-             else
+             w->prev = Qnil;
+             if (!NILP (w->parent))
                {
-                 XWINDOW (w->parent)->hchild = p->window;
-                 XWINDOW (w->parent)->vchild = Qnil;
+                 if (EQ (p->width, XWINDOW (w->parent)->width))
+                   {
+                     XWINDOW (w->parent)->vchild = p->window;
+                     XWINDOW (w->parent)->hchild = Qnil;
+                   }
+                 else
+                   {
+                     XWINDOW (w->parent)->hchild = p->window;
+                     XWINDOW (w->parent)->vchild = Qnil;
+                   }
                }
            }
-       }
-      w->left = p->left;
-      w->top = p->top;
-      w->width = p->width;
-      w->height = p->height;
-      w->hscroll = p->hscroll;
-      w->display_table = p->display_table;
-      XFASTINT (w->last_modified) = 0;
 
-      /* Reinstall the saved buffer and pointers into it.  */
-      if (NILP (p->buffer))
-       w->buffer = p->buffer;
-      else
-       {
-         if (!NILP (XBUFFER (p->buffer)->name))
-           /* If saved buffer is alive, install it.  */
-           {
-             w->buffer = p->buffer;
-             w->start_at_line_beg = p->start_at_line_beg;
-             set_marker_restricted (w->start, Fmarker_position (p->start), w->buffer);
-             set_marker_restricted (w->pointm, Fmarker_position (p->pointm), w->buffer);
-             Fset_marker (XBUFFER (w->buffer)->mark,
-                          Fmarker_position (p->mark), w->buffer);
-
-             if (!EQ (p->buffer, new_current_buffer) &&
-                 XBUFFER (p->buffer) == current_buffer)
-               Fgoto_char (w->pointm);
-           }
-         else if (NILP (XBUFFER (w->buffer)->name))
-           /* Else if window's old buffer is dead too, get a live one.  */
-           {
-             w->buffer = Fcdr (Fcar (Vbuffer_alist));
-             /* This will set the markers to beginning of visible range.  */
-             set_marker_restricted (w->start, make_number (0), w->buffer);
-             set_marker_restricted (w->pointm, make_number (0), w->buffer);
-             w->start_at_line_beg = Qt;
-           }
+         /* If we squirreled away the buffer in the window's height,
+            restore it now.  */
+         if (XTYPE (w->height) == Lisp_Buffer)
+           w->buffer = w->height;
+         w->left = p->left;
+         w->top = p->top;
+         w->width = p->width;
+         w->height = p->height;
+         w->hscroll = p->hscroll;
+         w->display_table = p->display_table;
+         XFASTINT (w->last_modified) = 0;
+
+         /* Reinstall the saved buffer and pointers into it.  */
+         if (NILP (p->buffer))
+           w->buffer = p->buffer;
          else
-           /* Keeping window's old buffer; make sure the markers are real.  */
-           /* Else if window's old buffer is dead too, get a live one.  */
            {
-             /* Set window markers at start of visible range.  */
-             if (XMARKER (w->start)->buffer == 0)
-               set_marker_restricted (w->start, make_number (0), w->buffer);
-             if (XMARKER (w->pointm)->buffer == 0)
-               set_marker_restricted (w->pointm,
-                                      make_number (BUF_PT (XBUFFER (w->buffer))),
-                                      w->buffer);
-             w->start_at_line_beg = Qt;
+             if (!NILP (XBUFFER (p->buffer)->name))
+               /* If saved buffer is alive, install it.  */
+               {
+                 w->buffer = p->buffer;
+                 w->start_at_line_beg = p->start_at_line_beg;
+                 set_marker_restricted (w->start,
+                                        Fmarker_position (p->start),
+                                        w->buffer);
+                 set_marker_restricted (w->pointm,
+                                        Fmarker_position (p->pointm),
+                                        w->buffer);
+                 Fset_marker (XBUFFER (w->buffer)->mark,
+                              Fmarker_position (p->mark), w->buffer);
+
+                 /* As documented in Fcurrent_window_configuration, don't
+                    save the location of point in the buffer which was current
+                    when the window configuration was recorded.  */
+                 if (!EQ (p->buffer, new_current_buffer) &&
+                     XBUFFER (p->buffer) == current_buffer)
+                   Fgoto_char (w->pointm);
+               }
+             else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
+               /* Else unless window has a live buffer, get one.  */
+               {
+                 w->buffer = Fcdr (Fcar (Vbuffer_alist));
+                 /* This will set the markers to beginning of visible
+                    range.  */
+                 set_marker_restricted (w->start, make_number (0), w->buffer);
+                 set_marker_restricted (w->pointm, make_number (0),w->buffer);
+                 w->start_at_line_beg = Qt;
+               }
+             else
+               /* Keeping window's old buffer; make sure the markers
+                  are real.  */
+               {
+                 /* Set window markers at start of visible range.  */
+                 if (XMARKER (w->start)->buffer == 0)
+                   set_marker_restricted (w->start, make_number (0),
+                                          w->buffer);
+                 if (XMARKER (w->pointm)->buffer == 0)
+                   set_marker_restricted (w->pointm,
+                                          (make_number
+                                           (BUF_PT (XBUFFER (w->buffer)))),
+                                          w->buffer);
+                 w->start_at_line_beg = Qt;
+               }
            }
        }
-    }
 
-  FRAME_ROOT_WINDOW (f) = data->root_window;
+      FRAME_ROOT_WINDOW (f) = data->root_window;
+      Fselect_window (data->current_window);
 
 #ifdef MULTI_FRAME
-  if (f != selected_frame && ! FRAME_IS_TERMCAP (f))
-    Fselect_frame (WINDOW_FRAME (XWINDOW (data->root_window)), Qnil);
+      if (NILP (data->focus_frame)
+         || (XTYPE (data->focus_frame) == Lisp_Frame
+             && FRAME_LIVE_P (XFRAME (data->focus_frame))))
+       Fredirect_frame_focus (frame, data->focus_frame);
 #endif
 
-  if (f == selected_frame)
-    {
-      Fselect_window (data->current_window);
-      if (!NILP (new_current_buffer))
-       Fset_buffer (new_current_buffer);
-      else
-       Fset_buffer (XWINDOW (selected_window)->buffer);
+#if 0 /* I don't understand why this is needed, and it causes problems
+         when the frame's old selected window has been deleted.  */
+#ifdef MULTI_FRAME
+      if (f != selected_frame && ! FRAME_TERMCAP_P (f))
+       Fhandle_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)), Qnil);
+#endif
+#endif
+
+      /* Set the screen height to the value it had before this function.  */
+      if (previous_frame_height != FRAME_HEIGHT (f)
+         || previous_frame_width != FRAME_WIDTH (f))
+       change_frame_size (f, previous_frame_height, previous_frame_width,
+                          0, 0);
+      if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
+       x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
     }
 
+#ifdef MULTI_FRAME
+  /* Fselect_window will have made f the selected frame, so we
+     reselect the proper frame here.  Fhandle_switch_frame will change the
+     selected window too, but that doesn't make the call to
+     Fselect_window above totally superfluous; it still sets f's
+     selected window.  */
+  if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
+    Fhandle_switch_frame (data->selected_frame, Qnil);
+#endif
+
+  if (!NILP (new_current_buffer))
+    Fset_buffer (new_current_buffer);
+
   Vminibuf_scroll_window = data->minibuf_scroll_window;
   return (Qnil);
 }
@@ -2435,18 +2747,26 @@ by `current-window-configuration' (which see).")
 /* Mark all windows now on frame as deleted
    by setting their buffers to nil.  */
 
-static void
+void
 delete_all_subwindows (w)
      register struct window *w;
 {
-  register int count = 1;
-  w->buffer = Qnil;
   if (!NILP (w->next))
     delete_all_subwindows (XWINDOW (w->next));
   if (!NILP (w->vchild))
     delete_all_subwindows (XWINDOW (w->vchild));
   if (!NILP (w->hchild))
     delete_all_subwindows (XWINDOW (w->hchild));
+
+  w->height = w->buffer;       /* See Fset_window_configuration for excuse.  */
+
+  /* We set all three of these fields to nil, to make sure that we can
+     distinguish this dead window from any live window.  Live leaf
+     windows will have buffer set, and combination windows will have
+     vchild or hchild set.  */
+  w->buffer = Qnil;
+  w->vchild = Qnil;
+  w->hchild = Qnil;
 }
 \f
 static int
@@ -2541,7 +2861,9 @@ If FRAME is nil or omitted, use the selected frame.\n\
 This describes the number of windows, their sizes and current buffers,\n\
 and for each displayed buffer, where display starts, and the positions of\n\
 point and mark.  An exception is made for point in the current buffer:\n\
-its value is -not- saved.")
+its value is -not- saved.\n\
+This also records the currently selected frame, and FRAME's focus\n\
+redirection (see `redirect-frame-focus').")
   (frame)
      Lisp_Object frame;
 {
@@ -2565,10 +2887,15 @@ its value is -not- saved.")
                                  Qnil));
   XFASTINT (data->frame_width) = FRAME_WIDTH (f);
   XFASTINT (data->frame_height) = FRAME_HEIGHT (f);
+  XFASTINT (data->frame_menu_bar_lines) = FRAME_MENU_BAR_LINES (f);
+#ifdef MULTI_FRAME
+  XSET (data->selected_frame, Lisp_Frame, selected_frame);
+#endif
   data->current_window = FRAME_SELECTED_WINDOW (f);
   XSET (data->current_buffer, Lisp_Buffer, current_buffer);
   data->minibuf_scroll_window = Vminibuf_scroll_window;
   data->root_window = FRAME_ROOT_WINDOW (f);
+  data->focus_frame = FRAME_FOCUS_FRAME (f);
   tem = Fmake_vector (make_number (n_windows), Qnil);
   data->saved_windows = tem;
   for (i = 0; i < n_windows; i++)
@@ -2608,11 +2935,12 @@ init_window_once ()
 #else /* not MULTI_FRAME */
   extern Lisp_Object get_minibuffer ();
 
-  FRAME_ROOT_WINDOW (selected_frame) = make_window ();
   minibuf_window = make_window ();
+  FRAME_ROOT_WINDOW (selected_frame) = make_window ();
 
   XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->next = minibuf_window;
   XWINDOW (minibuf_window)->prev = FRAME_ROOT_WINDOW (selected_frame);
+  XWINDOW (minibuf_window)->mini_p = Qt;
 
   /* These values 9 and 10 are arbitrary,
      just so that there is "something there."
@@ -2648,8 +2976,13 @@ syms_of_window ()
   Qwindowp = intern ("windowp");
   staticpro (&Qwindowp);
 
+  Qwindow_live_p = intern ("window-live-p");
+  staticpro (&Qwindow_live_p);
+
+#ifndef MULTI_FRAME
   /* Make sure all windows get marked */
   staticpro (&minibuf_window);
+#endif
 
   DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
     "Non-nil means call as function to display a help buffer.\n\
@@ -2664,25 +2997,6 @@ Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
 work using this function.");
   Vdisplay_buffer_function = Qnil;
 
-  DEFVAR_LISP ("mouse-window", &Vmouse_window,
-     "Window that the last mouse click occurred on.");
-  Vmouse_window = Qnil;
-
-  DEFVAR_LISP ("mouse-event", &Vmouse_event,
-     "The last mouse-event object.  A list of four elements:\n\
-  ((X-POS Y-POS) WINDOW FRAME-PART KEYSEQ).\n\
-KEYSEQ is a string, the key sequence to be looked up in the mouse maps.\n\
-WINDOW is the window that the click applies do.\n\
-If FRAME-PART is non-nil, the event was on a scrollbar;\n\
-then Y-POS is really the total length of the scrollbar, while X-POS is\n\
-the relative position of the scrollbar's value within that total length.\n\
-FRAME-PART is one of the following symbols:\n\
- `vertical-scrollbar', `vertical-slider',\n\
- `vertical-thumbup', `vertical-thumbdown',\n\
- `horizontal-scrollbar', `horizontal-slider',\n\
- `horizontal-thumbleft', `horizontal-thumbright'");
-  Vmouse_event = Qnil;
-
   DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
     "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
   Vminibuf_scroll_window = Qnil;
@@ -2691,9 +3005,8 @@ FRAME-PART is one of the following symbols:\n\
     "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
   Vother_window_scroll_buffer = Qnil;
 
-#ifdef MULTI_FRAME
   DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
-    "*Non-nil means `display-buffer' should make a separate X-window.");
+    "*Non-nil means `display-buffer' should make a separate frame.");
   pop_up_frames = 0;
 
   DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
@@ -2703,7 +3016,6 @@ It is called with no arguments and should return a newly created frame.\n\
 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
 where `pop-up-frame-alist' would hold the default frame parameters.");
   Vpop_up_frame_function = Qnil;
-#endif
 
   DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
     "*Non-nil means display-buffer should make new windows.");
@@ -2730,6 +3042,7 @@ If there is only one window, it is split regardless of this value.");
   defsubr (&Sminibuffer_window);
   defsubr (&Swindow_minibuffer_p);
   defsubr (&Swindowp);
+  defsubr (&Swindow_live_p);
   defsubr (&Spos_visible_in_window_p);
   defsubr (&Swindow_buffer);
   defsubr (&Swindow_height);