(one-window-p, walk-windows, minibuffer-window-active-p):
[bpt/emacs.git] / src / window.c
index 0f00ae6..ec561e5 100644 (file)
@@ -1,6 +1,6 @@
 /* Window creation, deletion and examination for GNU Emacs.
    Does not include redisplay.
-   Copyright (C) 1985, 1986, 1987, 1992, 1993 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,7 +29,7 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "disptab.h"
 #include "keyboard.h"
 
-Lisp_Object Qwindowp, Qlive_window_p;
+Lisp_Object Qwindowp, Qwindow_live_p;
 
 Lisp_Object Fnext_window (), Fdelete_window (), Fselect_window ();
 Lisp_Object Fset_window_buffer (), Fsplit_window (), Frecenter ();
@@ -60,9 +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;
 
-/* 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;
 
@@ -82,6 +79,24 @@ Lisp_Object Vpop_up_frame_function;
 /* Function to call to handle Fdisplay_buffer.  */
 Lisp_Object Vdisplay_buffer_function;
 
+/* List of buffer *names* for buffers that should have their own frames.  */
+Lisp_Object Vspecial_display_buffer_names;
+
+/* List of regexps for buffer names that should have their own frames.  */
+Lisp_Object Vspecial_display_regexps;
+
+/* Function to pop up a special frame.  */
+Lisp_Object Vspecial_display_function;
+
+/* List of buffer *names* for buffers to appear in selected window.  */
+Lisp_Object Vsame_window_buffer_names;
+
+/* List of regexps for buffer names to appear in selected window.  */
+Lisp_Object Vsame_window_regexps;
+
+/* Hook run at end of temp_output_buffer_show.  */
+Lisp_Object Qtemp_buffer_show_hook;
+
 /* Fdisplay_buffer always splits the largest window 
    if that window is more than this high.  */
 int split_height_threshold;
@@ -99,45 +114,45 @@ DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
   (obj)
      Lisp_Object obj;
 {
-  return XTYPE (obj) == Lisp_Window ? Qt : Qnil;
+  return WINDOWP (obj) ? Qt : Qnil;
 }
 
-DEFUN ("live-window-p", Flive_window_p, Slive_window_p, 1, 1, 0,
+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);
+  return (WINDOWP (obj) && ! NILP (XWINDOW (obj)->buffer) ? Qt : Qnil);
 }
 
 Lisp_Object
 make_window ()
 {
-  register Lisp_Object val;
+  Lisp_Object val;
   register struct window *p;
-
-  /* Add sizeof (Lisp_Object) here because sizeof (struct Lisp_Vector)
-     includes the first element.  */
-  val = Fmake_vector (
-    make_number ((sizeof (struct window) - sizeof (struct Lisp_Vector)
-                 + sizeof (Lisp_Object))
-                / sizeof (Lisp_Object)),
-    Qnil);
-  XSETTYPE (val, Lisp_Window);
-  p = XWINDOW (val);
-  XFASTINT (p->sequence_number) = ++sequence_number;
-  XFASTINT (p->left) = XFASTINT (p->top)
-    = XFASTINT (p->height) = XFASTINT (p->width)
-      = XFASTINT (p->hscroll) = 0;
-  XFASTINT (p->last_point_x) = XFASTINT (p->last_point_y) = 0;
+  register struct Lisp_Vector *vec;
+  int i;
+
+  vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
+  for (i = 0; i < VECSIZE (struct window); i++)
+    vec->contents[i] = Qnil;
+  vec->size = VECSIZE (struct window);
+  p = (struct window *)vec;
+  XSETFASTINT (p->sequence_number, ++sequence_number);
+  XSETFASTINT (p->left, 0);
+  XSETFASTINT (p->top, 0);
+  XSETFASTINT (p->height, 0);
+  XSETFASTINT (p->width, 0);
+  XSETFASTINT (p->hscroll, 0);
+  XSETFASTINT (p->last_point_x, 0);
+  XSETFASTINT (p->last_point_y, 0);
   p->start = Fmake_marker ();
   p->pointm = Fmake_marker ();
-  XFASTINT (p->use_time) = 0;
+  XSETFASTINT (p->use_time, 0);
   p->frame = Qnil;
   p->display_table = Qnil;
   p->dedicated = Qnil;
+  XSETWINDOW (val, p);
   return val;
 }
 
@@ -157,7 +172,7 @@ used by that frame.")
 {
 #ifdef MULTI_FRAME
   if (NILP (frame))
-    XSET (frame, Lisp_Frame, selected_frame);
+    XSETFRAME (frame, selected_frame);
   else
     CHECK_LIVE_FRAME (frame, 0);
 #endif
@@ -188,9 +203,10 @@ 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;
+    posint = PT;
   else
     {
       CHECK_NUMBER_COERCE_MARKER (pos, 0);
@@ -199,6 +215,7 @@ POS defaults to point; WINDOW, to the selected window.")
 
   w = decode_window (window);
   top = marker_position (w->start);
+  hscroll = XINT (w->hscroll);
 
   if (posint < top)
     return Qnil;
@@ -217,13 +234,18 @@ POS defaults to point; WINDOW, to the selected window.")
     }
   else
     {
-      if (posint > BUF_Z (buf))
+      if (posint > BUF_ZV (buf))
+       return Qnil;
+
+      /* w->start can be out of range.  If it is, do something reasonable.  */
+      if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
        return Qnil;
 
       /* If that info is not correct, calculate afresh */
-      posval = *compute_motion (top, 0, 0, posint, height, 0,
+      posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0),
+                               posint, height, 0,
                                window_internal_width (w) - 1,
-                               XINT (w->hscroll), 0);
+                               hscroll, 0, w);
 
       return posval.vpos < height ? Qt : Qnil;
     }
@@ -257,14 +279,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 = XFASTINT (w->width);
-
-  return make_number (window_internal_width (w));
+  return make_number (window_internal_width (decode_window (window)));
 }
 
 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
@@ -284,7 +306,7 @@ NCOL should be zero or positive.")
   register struct window *w;
 
   CHECK_NUMBER (ncol, 1);
-  if (XINT (ncol) < 0) XFASTINT (ncol) = 0;
+  if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
   if (XFASTINT (ncol) >= (1 << (SHORTBITS - 1)))
     args_out_of_range (ncol, Qnil);
   w = decode_window (window);
@@ -435,7 +457,7 @@ column 0.")
 
 #ifdef MULTI_FRAME
   if (NILP (frame))
-    XSET (frame, Lisp_Frame, selected_frame);
+    XSETFRAME (frame, selected_frame);
   else
     CHECK_LIVE_FRAME (frame, 2);
 #endif
@@ -476,16 +498,43 @@ DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
   return Fmarker_position (decode_window (window)->start);
 }
 
+/* This is text temporarily removed from the doc string below.
+
+This function returns nil if the position is not currently known.\n\
+That happens when redisplay is preempted and doesn't finish.\n\
+If in that case you want to compute where the end of the window would\n\
+have been if redisplay had finished, do this:\n\
+    (save-excursion\n\
+      (goto-char (window-start window))\n\
+      (vertical-motion (1- (window-height window)) window)\n\
+      (point))")  */
+
 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
-  "Return position at which display currently ends in WINDOW.")
+  "Return position at which display currently ends in WINDOW.\n\
+This is updated by redisplay, when it runs to completion.\n\
+Simply changing the buffer text or setting `window-start'\n\
+does not update this value.")
   (window)
      Lisp_Object window;
 {
   Lisp_Object value;
   struct window *w = decode_window (window);
-  
-  XSET (value, Lisp_Int,
-       BUF_Z (current_buffer) - XFASTINT (w->window_end_pos));
+  Lisp_Object buf;
+
+  buf = w->buffer;
+  CHECK_BUFFER (buf, 0);
+
+#if 0 /* This change broke some things.  We should make it later.  */
+  /* If we don't know the end position, return nil.
+     The user can compute it with vertical-motion if he wants to.
+     It would be nicer to do it automatically,
+     but that's so slow that it would probably bother people.  */
+  if (NILP (w->window_end_valid))
+    return Qnil;
+#endif
+
+  XSETINT (value,
+          BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
 
   return value;
 }
@@ -522,7 +571,7 @@ from overriding motion of point in order to display at this exact start.")
   if (NILP (noforce))
     w->force_start = Qt;
   w->update_mode_line = Qt;
-  XFASTINT (w->last_modified) = 0;
+  XSETFASTINT (w->last_modified, 0);
   if (!EQ (window, selected_window))
     windows_or_buffers_changed++;
   return pos;
@@ -531,7 +580,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;
 {
@@ -578,13 +627,13 @@ window_display_table (w)
 {
   Lisp_Object tem;
   tem = w->display_table;
-  if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
+  if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
     return XVECTOR (tem);
   tem = XBUFFER (w->buffer)->display_table;
-  if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
+  if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
     return XVECTOR (tem);
   tem = Vstandard_display_table;
-  if (XTYPE (tem) == Lisp_Vector && XVECTOR (tem)->size == DISP_TABLE_SIZE)
+  if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
     return XVECTOR (tem);
   return 0;
 }
@@ -608,15 +657,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
@@ -700,14 +759,29 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
   par = XWINDOW (parent);
 
   windows_or_buffers_changed++;
+  FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
 
   /* Are we trying to delete any frame's selected window?  */
   {
-    Lisp_Object frame = WINDOW_FRAME (XWINDOW (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));
 
-    if (EQ (window, FRAME_SELECTED_WINDOW (XFRAME (frame))))
+    while (!NILP (pwindow))
       {
-       Lisp_Object alternative = Fnext_window (window, Qlambda, Qnil);
+       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
@@ -791,7 +865,11 @@ DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
 
 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\
@@ -802,18 +880,24 @@ minibuffer even if it is active.\n\
 \n\
 Several frames may share a single minibuffer; if the minibuffer\n\
 counts, all windows on all frames that share that minibuffer count\n\
-too.  This means that next-window may be used to iterate through the\n\
+too.  Therefore, `next-window' can be used to iterate through the\n\
 set of windows even when the minibuffer is on another frame.  If the\n\
 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.\n\
+above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
+ALL-FRAMES = 0 means include windows on all visible and iconified 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;
 {
@@ -832,17 +916,27 @@ windows, eventually ending up back at the window you started with.\n\
   if (NILP (minibuf))
     minibuf = (minibuf_level ? Qt : Qlambda);
 
-  /* all_frames == nil doesn't specify which frames to include.
-     Decide which frames it includes.  */
+#ifdef MULTI_FRAME
+  /* all_frames == nil doesn't specify which frames to include.  */
   if (NILP (all_frames))
     all_frames = (EQ (minibuf, Qt)
-                  ? (FRAME_MINIBUF_WINDOW
-                     (XFRAME
-                      (WINDOW_FRAME
-                       (XWINDOW (window)))))
-                  : Qnil);
+                 ? (FRAME_MINIBUF_WINDOW
+                    (XFRAME
+                     (WINDOW_FRAME
+                      (XWINDOW (window)))))
+                 : Qnil);
+  else if (EQ (all_frames, Qvisible))
+    ;
+  else if (XFASTINT (all_frames) == 0)
+    ;
   else if (! EQ (all_frames, Qt))
     all_frames = Qnil;
+  /* Now all_frames is t meaning search all frames,
+     nil meaning search just current frame,
+     visible meaning search just visible frames,
+     0 meaning search visible and iconified frames,
+     or a window, meaning search the frame that window belongs to.  */
+#endif
 
   /* Do this loop at least once, to get the next window, and perhaps
      again, if we hit the minibuffer and that is not acceptable.  */
@@ -860,7 +954,19 @@ windows, eventually ending up back at the window you started with.\n\
            tem = WINDOW_FRAME (XWINDOW (window));
 #ifdef MULTI_FRAME
            if (! NILP (all_frames))
-             tem = next_frame (tem, all_frames);
+             {
+               Lisp_Object tem1;
+
+               tem1 = tem;
+               tem = next_frame (tem, all_frames);
+               /* In the case where the minibuffer is active,
+                  and we include its frame as well as the selected one,
+                  next_frame may get stuck in that frame.
+                  If that happens, go back to the selected frame
+                  so we can complete the cycle.  */
+               if (EQ (tem, tem1))
+                 XSETFRAME (tem, selected_frame);
+             }
 #endif
            tem = FRAME_ROOT_WINDOW (XFRAME (tem));
 
@@ -892,7 +998,11 @@ windows, eventually ending up back at the window you started with.\n\
   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\
@@ -903,19 +1013,25 @@ minibuffer even if it is active.\n\
 \n\
 Several frames may share a single minibuffer; if the minibuffer\n\
 counts, all windows on all frames that share that minibuffer count\n\
-too.  This means that previous-window may be used to iterate through\n\
+too.  Therefore, `previous-window' can be used to iterate through\n\
 the set of windows even when the minibuffer is on another frame.  If\n\
-the minibuffer does not count, only windows from WINDOW's frame\n\
-count.\n\
+the 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.\n\
+above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
+ALL-FRAMES = 0 means include windows on all visible and iconified 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;
 {
@@ -934,6 +1050,7 @@ windows, eventually ending up back at the window you started with.\n\
   if (NILP (minibuf))
     minibuf = (minibuf_level ? Qt : Qlambda);
 
+#ifdef MULTI_FRAME
   /* all_frames == nil doesn't specify which frames to include.
      Decide which frames it includes.  */
   if (NILP (all_frames))
@@ -943,8 +1060,18 @@ windows, eventually ending up back at the window you started with.\n\
                       (WINDOW_FRAME
                        (XWINDOW (window)))))
                   : Qnil);
+  else if (EQ (all_frames, Qvisible))
+    ;
+  else if (XFASTINT (all_frames) == 0)
+    ;
   else if (! EQ (all_frames, Qt))
     all_frames = Qnil;
+  /* Now all_frames is t meaning search all frames,
+     nil meaning search just current frame,
+     visible meaning search just visible frames,
+     0 meaning search visible and iconified frames,
+     or a window, meaning search the frame that window belongs to.  */
+#endif
 
   /* Do this loop at least once, to get the previous window, and perhaps
      again, if we hit the minibuffer and that is not acceptable.  */
@@ -971,9 +1098,26 @@ windows, eventually ending up back at the window you started with.\n\
                 paths through the set of acceptable windows.
                 window_loop assumes that these `ring' requirement are
                 met.  */
-             tem = prev_frame (tem, all_frames);
+             {
+               Lisp_Object tem1;
+
+               tem1 = tem;
+               tem = prev_frame (tem, all_frames);
+               /* In the case where the minibuffer is active,
+                  and we include its frame as well as the selected one,
+                  next_frame may get stuck in that frame.
+                  If that happens, go back to the selected frame
+                  so we can complete the cycle.  */
+               if (EQ (tem, tem1))
+                 XSETFRAME (tem, selected_frame);
+             }
 #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;
          }
@@ -1036,8 +1180,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.
 */
 
@@ -1049,7 +1195,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
@@ -1063,6 +1209,8 @@ window_loop (type, obj, mini, frames)
   register Lisp_Object next_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,
@@ -1074,12 +1222,22 @@ window_loop (type, obj, mini, frames)
     frame = selected_frame;
   else
     frame = 0;
+  if (frame)
+    frame_arg = Qlambda;
+  else if (XFASTINT (frames) == 0)
+    frame_arg = frames;
+  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)
+  if (WINDOWP (obj))
     w = obj;
   else if (frame)
     w = FRAME_SELECTED_WINDOW (frame);
@@ -1093,40 +1251,25 @@ window_loop (type, obj, mini, frames)
      We can't just wait until we hit the first window again, because
      it might be deleted.  */
 
-#ifdef MULTI_FRAME
-  if (frame)
-    last_window = Fprevious_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?  */
-    last_window = Fprevious_window (w, mini ? Qt : Qnil, Qt);
+  last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
 
   best_window = Qnil;
   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;
@@ -1134,14 +1277,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))
+               != FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (w)))))
              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)))))
-             break;
-#endif
            /* Ignore dedicated windows and minibuffers.  */
            if (MINI_WINDOW_P (XWINDOW (w))
                || !NILP (XWINDOW (w)->dedicated))
@@ -1160,29 +1297,55 @@ window_loop (type, obj, mini, frames)
          case DELETE_BUFFER_WINDOWS:
            if (EQ (XWINDOW (w)->buffer, obj))
              {
-               /* If we're deleting the buffer displayed in the only window
-                  on the frame, find a new buffer to display there.  */
-               if (NILP (XWINDOW (w)->parent))
+#ifdef MULTI_FRAME
+               FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
+
+               /* If this window is dedicated, and in a frame of its own,
+                  kill the frame.  */
+               if (EQ (w, FRAME_ROOT_WINDOW (f))
+                   && !NILP (XWINDOW (w)->dedicated)
+                   && other_visible_frames (f))
                  {
-                   Lisp_Object 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);
+                   /* Skip the other windows on this frame.
+                      There might be one, the minibuffer!  */
+                   if (! EQ (w, last_window))
+                     while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
+                       {
+                         /* As we go, check for the end of the loop.
+                            We mustn't start going around a second time.  */
+                         if (EQ (next_window, last_window))
+                           {
+                             last_window = w;
+                             break;
+                           }
+                         next_window = Fnext_window (next_window,
+                                                     mini ? Qt : Qnil,
+                                                     frame_arg);
+                       }
+                   /* Now we can safely delete the frame.  */
+                   Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
                  }
                else
-                 Fdelete_window (w);
+#endif
+                 /* If we're deleting the buffer displayed in the only window
+                    on the frame, find a new buffer to display there.  */
+                 if (NILP (XWINDOW (w)->parent))
+                   {
+                     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);
+                     if (EQ (w, selected_window))
+                       Fset_buffer (XWINDOW (w)->buffer);
+                   }
+                 else
+                   Fdelete_window (w);
              }
            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))
@@ -1190,10 +1353,10 @@ window_loop (type, obj, mini, frames)
            {
              struct window *best_window_ptr = XWINDOW (best_window);
              struct window *w_ptr = XWINDOW (w);
-             if (NILP (best_window) ||
-                 (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width))
-                 > (XFASTINT (best_window_ptr->height)
-                    * XFASTINT (best_window_ptr->width)))
+             if (NILP (best_window)
+                 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
+                     > (XFASTINT (best_window_ptr->height)
+                        * XFASTINT (best_window_ptr->width))))
                best_window = w;
            }
            break;
@@ -1202,13 +1365,47 @@ 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, Qnil);
+               Lisp_Object another_buffer;
+               FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
+               another_buffer = Fother_buffer (obj, Qnil);
                if (NILP (another_buffer))
                  another_buffer
                    = Fget_buffer_create (build_string ("*scratch*"));
-               Fset_window_buffer (w, another_buffer);
-               if (EQ (w, selected_window))
-                 Fset_buffer (XWINDOW (w)->buffer);
+#ifdef MULTI_FRAME
+               /* If this window is dedicated, and in a frame of its own,
+                  kill the frame.  */
+               if (EQ (w, FRAME_ROOT_WINDOW (f))
+                   && !NILP (XWINDOW (w)->dedicated)
+                   && other_visible_frames (f))
+                 {
+                   /* Skip the other windows on this frame.
+                      There might be one, the minibuffer!  */
+                   if (! EQ (w, last_window))
+                     while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
+                       {
+                         /* As we go, check for the end of the loop.
+                            We mustn't start going around a second time.  */
+                         if (EQ (next_window, last_window))
+                           {
+                             last_window = w;
+                             break;
+                           }
+                         next_window = Fnext_window (next_window,
+                                                     mini ? Qt : Qnil,
+                                                     frame_arg);
+                       }
+                   /* Now we can safely delete the frame.  */
+                   Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
+                 }
+               else
+#endif
+                 {
+                   /* Otherwise show a different buffer in the window.  */
+                   XWINDOW (w)->dedicated = Qnil;
+                   Fset_window_buffer (w, another_buffer);
+                   if (EQ (w, selected_window))
+                     Fset_buffer (XWINDOW (w)->buffer);
+                 }
              }
            break;
          }
@@ -1224,24 +1421,30 @@ window_loop (type, obj, mini, frames)
 
 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 0, search all visible and iconified 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 0, search all visible and iconified 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;
 {
@@ -1251,13 +1454,16 @@ 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 optional argument FRAME is 0, search all visible and iconified 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;
 {
   buffer = Fget_buffer (buffer);
-  if (XTYPE (buffer) == Lisp_Buffer)
+  if (BUFFERP (buffer))
     return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
   else
     return Qnil;
@@ -1266,13 +1472,18 @@ 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 startpos;
   int top;
 
   if (NILP (window))
@@ -1281,30 +1492,66 @@ Only the frame WINDOW is on is affected.")
     CHECK_LIVE_WINDOW (window, 0);
 
   w = XWINDOW (window);
-  top = XFASTINT (w->top);
 
-  window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
+  startpos = marker_position (w->start);
+  top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
 
-  Fset_buffer (w->buffer);
-  SET_PT (marker_position (w->start));
-  Frecenter (make_number (top - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)))));
+  if (MINI_WINDOW_P (w) && top > 0)
+    error ("Can't expand minibuffer to full frame");
 
-  set_buffer_internal (obuf);
-  SET_PT (opoint);
+  window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
+
+  /* Try to minimize scrolling, by setting the window start to the point
+     will cause the text at the old window start to be at the same place
+     on the frame.  But don't try to do this if the window start is
+     outside the visible portion (as might happen when the display is
+     not current, due to typeahead).  */
+  if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
+      && startpos <= BUF_ZV (XBUFFER (w->buffer)))
+    {
+      struct position pos;
+      struct buffer *obuf = current_buffer;
+
+      Fset_buffer (w->buffer);
+      /* This computation used to temporarily move point, but that can
+        have unwanted side effects due to text properties.  */
+      pos = *vmotion (startpos, -top, window_internal_width (w) - 1,
+                     XINT (w->hscroll), window);
+      Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
+      w->start_at_line_beg = ((pos.bufpos == BEGV
+                              || FETCH_CHAR (pos.bufpos - 1) == '\n') ? Qt
+                             : Qnil);
+
+      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;
 }
@@ -1352,13 +1599,16 @@ check_frame_size (frame, rows, cols)
      FRAME_PTR frame;
      int *rows, *cols;
 {
-  /* For height, we have to see whether the frame has a minibuffer, and
-     whether it wants a mode line.  */
+  /* 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)
-      || ! FRAME_HAS_MINIBUF_P (frame))
-     ? MIN_SAFE_WINDOW_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;
@@ -1391,9 +1641,11 @@ set_window_height (window, height, nodelete)
       return;
     }
 
-  XFASTINT (w->last_modified) = 0;
+  XSETFASTINT (w->last_modified, 0);
   windows_or_buffers_changed++;
-  XFASTINT (w->height) = height;
+  FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
+
+  XSETFASTINT (w->height, height);
   if (!NILP (w->hchild))
     {
       for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
@@ -1412,7 +1664,7 @@ set_window_height (window, height, nodelete)
 
          opos = lastobot + XFASTINT (c->height);
 
-         XFASTINT (c->top) = lastbot;
+         XSETFASTINT (c->top, lastbot);
 
          pos = (((opos * height) << 1) + oheight) / (oheight << 1);
 
@@ -1446,15 +1698,17 @@ set_window_width (window, width, nodelete)
   int left, pos, lastright, opos, lastoright;
   Lisp_Object child;
 
-  if (!nodelete && width < window_min_width)
+  if (!nodelete && width < window_min_width && !NILP (w->parent))
     {
       Fdelete_window (window);
       return;
     }
 
-  XFASTINT (w->last_modified) = 0;
+  XSETFASTINT (w->last_modified, 0);
   windows_or_buffers_changed++;
-  XFASTINT (w->width) = width;
+  FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
+
+  XSETFASTINT (w->width, width);
   if (!NILP (w->vchild))
     {
       for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
@@ -1473,7 +1727,7 @@ set_window_width (window, width, nodelete)
 
          opos = lastoright + XFASTINT (c->width);
 
-         XFASTINT (c->left) = lastright;
+         XSETFASTINT (c->left, lastright);
 
          pos = (((opos * width) << 1) + owidth) / (owidth << 1);
 
@@ -1517,12 +1771,16 @@ BUFFER can be a buffer or buffer name.")
                                   is first being set up.  */
     {
       if (!NILP (w->dedicated) && !EQ (tem, buffer))
-       error ("Window is dedicated to %s\n", tem);
+       error ("Window is dedicated to `%s'",
+              XSTRING (XBUFFER (tem)->name)->data);
 
       unshow_buffer (w);
     }
 
   w->buffer = buffer;
+  XSETFASTINT (w->window_end_pos, 0);
+  w->window_end_valid = Qnil;
+  XSETFASTINT(w->hscroll, 0);
   Fset_marker (w->pointm,
               make_number (BUF_PT (XBUFFER (buffer))),
               buffer);
@@ -1530,7 +1788,8 @@ BUFFER can be a buffer or buffer name.")
                         make_number (XBUFFER (buffer)->last_window_start),
                         buffer);
   w->start_at_line_beg = Qnil;
-  XFASTINT (w->last_modified) = 0;
+  w->force_start = Qnil;
+  XSETFASTINT (w->last_modified, 0);
   windows_or_buffers_changed++;
   if (EQ (window, selected_window))
     Fset_buffer (buffer);
@@ -1555,7 +1814,7 @@ before each command.")
   if (NILP (w->buffer))
     error ("Trying to select deleted window or non-leaf window");
 
-  XFASTINT (w->use_time) = ++window_select_count;
+  XSETFASTINT (w->use_time, ++window_select_count);
   if (EQ (window, selected_window))
     return window;
 
@@ -1567,6 +1826,10 @@ before each command.")
   if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
     {
       XFRAME (WINDOW_FRAME (w))->selected_window = window;
+      /* Use this rather than Fhandle_switch_frame
+        so that FRAME_FOCUS_FRAME is moved appropriately as we
+        move around in the state where a minibuffer in a separate
+        frame is active.  */
       Fselect_frame (WINDOW_FRAME (w), Qnil);
     }
   else
@@ -1585,7 +1848,7 @@ before each command.")
     register int new_point = marker_position (w->pointm);
     if (new_point < BEGV)
       SET_PT (BEGV);
-    if (new_point > ZV)
+    else if (new_point > ZV)
       SET_PT (ZV);
     else
       SET_PT (new_point);
@@ -1596,17 +1859,18 @@ 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 (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;
 {
-  register Lisp_Object window;
+  register Lisp_Object window, tem;
 
   buffer = Fget_buffer (buffer);
   CHECK_BUFFER (buffer, 0);
@@ -1618,22 +1882,95 @@ Returns the window displaying BUFFER.")
       && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
     return selected_window;
 
+  /* See if the user has specified this buffer should appear
+     in the selected window.  */
+  if (NILP (not_this_window))
+    {
+      tem = Fmember (XBUFFER (buffer)->name, Vsame_window_buffer_names);
+      if (!NILP (tem))
+       {
+         Fswitch_to_buffer (buffer, Qnil);
+         return selected_window;
+       }
+
+      tem = Fassoc (XBUFFER (buffer)->name, Vsame_window_buffer_names);
+      if (!NILP (tem))
+       {
+         Fswitch_to_buffer (buffer, Qnil);
+         return selected_window;
+       }
+
+      for (tem = Vsame_window_regexps; CONSP (tem); tem = XCONS (tem)->cdr)
+       {
+         Lisp_Object car = XCONS (tem)->car;
+         if (STRINGP (car)
+             && fast_string_match (car, XBUFFER (buffer)->name) >= 0)
+           {
+             Fswitch_to_buffer (buffer, Qnil);
+             return selected_window;
+           }
+         else if (CONSP (car)
+                  && STRINGP (XCONS (car)->car)
+                  && fast_string_match (XCONS (car)->car,
+                                        XBUFFER (buffer)->name) >= 0)
+           {
+             Fswitch_to_buffer (buffer, Qnil);
+             return selected_window;
+           }
+       }
+    }
+
+#ifdef MULTI_FRAME
+  /* If pop_up_frames,
+     look for a window showing BUFFER on any visible or iconified frame.  */
+  window = Fget_buffer_window (buffer, pop_up_frames ? make_number (0) : Qnil);
+#else
   window = Fget_buffer_window (buffer, Qnil);
+#endif
   if (!NILP (window)
       && (NILP (not_this_window) || !EQ (window, selected_window)))
-    return window;
+    {
+#ifdef MULTI_FRAME
+      if (FRAME_ICONIFIED_P (XFRAME (WINDOW_FRAME (XWINDOW (window)))))
+       Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
+#endif
+      return window;
+    }
+
+  /* Certain buffer names get special handling.  */
+  if (! NILP (Vspecial_display_function))
+    {
+      tem = Fmember (XBUFFER (buffer)->name, Vspecial_display_buffer_names);
+      if (!NILP (tem))
+       return call1 (Vspecial_display_function, buffer);
+
+      tem = Fassoc (XBUFFER (buffer)->name, Vspecial_display_buffer_names);
+      if (!NILP (tem))
+       return call2 (Vspecial_display_function, buffer, XCONS (tem)->cdr);
+      
+      for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCONS (tem)->cdr)
+       {
+         Lisp_Object car = XCONS (tem)->car;
+         if (STRINGP (car)
+             && fast_string_match (car, XBUFFER (buffer)->name) >= 0)
+           return call1 (Vspecial_display_function, buffer);
+         else if (CONSP (car)
+                  && STRINGP (XCONS (car)->car)
+                  && fast_string_match (XCONS (car)->car,
+                                        XBUFFER (buffer)->name) >= 0)
+           return call2 (Vspecial_display_function,
+                         buffer,
+                         XCONS (car)->cdr);
+       }
+    }
 
 #ifdef MULTI_FRAME
   /* If there are no frames open that have more than a minibuffer,
      we need to create a new frame.  */
   if (pop_up_frames || last_nonminibuf_frame == 0)
     {
-      window
-       = Fframe_selected_window (call0 (Vpop_up_frame_function));
+      window = Fframe_selected_window (call0 (Vpop_up_frame_function));
       Fset_window_buffer (window, buffer);
-#if 0
-      Fselect_frame (XWINDOW (window)->frame, Qnil);
-#endif
       return window;
     }
 #endif /* MULTI_FRAME */
@@ -1641,33 +1978,104 @@ Returns the window displaying BUFFER.")
   if (pop_up_windows
 #ifdef MULTI_FRAME
       || FRAME_MINIBUF_ONLY_P (selected_frame)
+      /* If the current frame is a special display frame,
+        don't try to reuse its windows.  */
+      || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
 #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);
+       XSETFRAME (frames, last_nonminibuf_frame);
 #endif
       /* Don't try to create a window if would get an error */
       if (split_height_threshold < window_min_height << 1)
        split_height_threshold = window_min_height << 1;
 
-      window = Fget_largest_window (frames);
+      /* Note that both Fget_largest_window and Fget_lru_window
+        ignore minibuffers and dedicated windows.
+        This means they can return nil.  */
 
+#ifdef MULTI_FRAME
+      /* If the frame we would try to split cannot be split,
+        try other frames.  */
+      if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
+                           : last_nonminibuf_frame))
+       {
+         /* Try visible frames first.  */
+         window = Fget_largest_window (Qvisible);
+         /* If that didn't work, try iconified frames.  */
+         if (NILP (window))
+           window = Fget_largest_window (make_number (0));
+         if (NILP (window))
+           window = Fget_largest_window (Qt);
+       }
+      else
+#endif
+       window = Fget_largest_window (frames);
+
+      /* If we got a tall enough full-width window that can be split,
+        split it.  */
       if (!NILP (window)
+         && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
          && window_height (window) >= split_height_threshold
          && (XFASTINT (XWINDOW (window)->width)
              == FRAME_WIDTH (XFRAME (WINDOW_FRAME (XWINDOW (window))))))
        window = Fsplit_window (window, Qnil, Qnil);
       else
        {
+         Lisp_Object upper, lower, other;
+
          window = Fget_lru_window (frames);
-         if ((EQ (window, selected_window)
-              || EQ (XWINDOW (window)->parent, Qnil))
+         /* If the LRU window is selected, and big enough,
+            and can be split, split it.  */
+         if (!NILP (window)
+             && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
+             && (EQ (window, selected_window)
+                 || EQ (XWINDOW (window)->parent, Qnil))
              && window_height (window) >= window_min_height << 1)
            window = Fsplit_window (window, Qnil, Qnil);
+#ifdef MULTI_FRAME
+         /* If Fget_lru_window returned nil, try other approaches.  */
+         /* Try visible frames first.  */
+         if (NILP (window))
+           window = Fget_largest_window (Qvisible);
+         /* If that didn't work, try iconified frames.  */
+         if (NILP (window))
+           window = Fget_largest_window (make_number (0));
+         /* Try invisible frames.  */
+         if (NILP (window))
+           window = Fget_largest_window (Qt);
+         /* As a last resort, make a new frame.  */
+         if (NILP (window))
+           window = Fframe_selected_window (call0 (Vpop_up_frame_function));
+#else
+         /* As a last resort, use a non minibuffer window.  */
+         if (NILP (window))
+           window = Fframe_first_window (Fselected_frame ());
+#endif
+         /* If window appears above or below another,
+            even out their heights.  */
+         if (!NILP (XWINDOW (window)->prev))
+           other = upper = XWINDOW (window)->prev, lower = window;
+         if (!NILP (XWINDOW (window)->next))
+           other = lower = XWINDOW (window)->next, upper = window;
+         if (!NILP (other)
+             /* Check that OTHER and WINDOW are vertically arrayed.  */
+             && XWINDOW (other)->top != XWINDOW (window)->top
+             && XWINDOW (other)->height > XWINDOW (window)->height)
+           {
+             int total = XWINDOW (other)->height + XWINDOW (window)->height;
+             Lisp_Object old_selected_window;
+             old_selected_window = selected_window;
+
+             selected_window = upper;
+             change_window_height (total / 2 - XWINDOW (upper)->height, 0);
+             selected_window = old_selected_window;
+           }
        }
     }
   else
@@ -1686,7 +2094,7 @@ temp_output_buffer_show (buf)
   register struct window *w;
 
   Fset_buffer (buf);
-  XBUFFER (buf)->save_modified = MODIFF;
+  BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
   BEGV = BEG;
   ZV = Z;
   SET_PT (BEG);
@@ -1705,27 +2113,33 @@ temp_output_buffer_show (buf)
 #endif /* MULTI_FRAME */
       Vminibuf_scroll_window = window;
       w = XWINDOW (window);
-      XFASTINT (w->hscroll) = 0;
+      XSETFASTINT (w->hscroll, 0);
       set_marker_restricted (w->start, make_number (1), buf);
       set_marker_restricted (w->pointm, make_number (1), buf);
     }
+
+  if (!NILP (Vrun_hooks))
+    call1 (Vrun_hooks, Qtemp_buffer_show_hook);
 }
 \f
 static
 make_dummy_parent (window)
      Lisp_Object window;
 {
-  register Lisp_Object old, new;
+  Lisp_Object new;
   register struct window *o, *p;
+  register struct Lisp_Vector *vec;
+  int i;
 
-  old = window;
-  XSETTYPE (old, Lisp_Vector);
-  new = Fcopy_sequence (old);
-  XSETTYPE (new, Lisp_Window);
+  o = XWINDOW (window);
+  vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
+  for (i = 0; i < VECSIZE (struct window); ++i)
+    vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
+  vec->size = VECSIZE (struct window);
+  p = (struct window *)vec;
+  XSETWINDOW (new, p);
 
-  o = XWINDOW (old);
-  p = XWINDOW (new);
-  XFASTINT (p->sequence_number) = ++sequence_number;
+  XSETFASTINT (p->sequence_number, ++sequence_number);
 
   /* Put new into window structure in place of window */
   replace_window (window, new);
@@ -1814,6 +2228,7 @@ and put SIZE columns in the first of the pair.")
      if we are making side-by-side windows */
 
   windows_or_buffers_changed++;
+  FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (o))) = 1;
   new = make_window ();
   p = XWINDOW (new);
 
@@ -1834,17 +2249,17 @@ and put SIZE columns in the first of the pair.")
     {
       p->height = o->height;
       p->top = o->top;
-      XFASTINT (p->width) = XFASTINT (o->width) - size;
-      XFASTINT (o->width) = size;
-      XFASTINT (p->left) = XFASTINT (o->left) + size;
+      XSETFASTINT (p->width, XFASTINT (o->width) - size);
+      XSETFASTINT (o->width, size);
+      XSETFASTINT (p->left, XFASTINT (o->left) + size);
     }
   else
     {
       p->left = o->left;
       p->width = o->width;
-      XFASTINT (p->height) = XFASTINT (o->height) - size;
-      XFASTINT (o->height) = size;
-      XFASTINT (p->top) = XFASTINT (o->top) + size;
+      XSETFASTINT (p->height, XFASTINT (o->height) - size);
+      XSETFASTINT (o->height, size);
+      XSETFASTINT (p->top, XFASTINT (o->top) + size);
     }
 
   return new;
@@ -1937,12 +2352,6 @@ change_window_height (delta, widthflag)
 
   sizep = &CURSIZE (window);
 
-  if (*sizep + delta < MINSIZE (window))
-    {
-      Fdelete_window (window);
-      return;
-    }
-
   {
     register int maxdelta;
 
@@ -1958,13 +2367,19 @@ change_window_height (delta, widthflag)
         the full frame, or make the only window aside from the
         minibuffer the full frame.  */
       delta = maxdelta;
+  }
 
-    if (delta == 0)
+  if (*sizep + delta < MINSIZE (window))
+    {
+      Fdelete_window (window);
       return;
-  }
+    }
+
+  if (delta == 0)
+    return;
 
-  if (!NILP (p->next) &&
-      (*sizefun) (p->next) - delta >= MINSIZE (p->next))
+  if (!NILP (p->next)
+      && (*sizefun) (p->next) - delta >= MINSIZE (p->next))
     {
       (*setsizefun) (p->next, (*sizefun) (p->next) - delta, 0);
       (*setsizefun) (window, *sizep + delta, 0);
@@ -1973,8 +2388,8 @@ change_window_height (delta, widthflag)
         but it propagates the new top edge to its children */
       (*setsizefun) (p->next, (*sizefun) (p->next), 0);
     }
-  else if (!NILP (p->prev) &&
-          (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
+  else if (!NILP (p->prev)
+          && (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
     {
       (*setsizefun) (p->prev, (*sizefun) (p->prev) - delta, 0);
       CURBEG (window) -= delta;
@@ -2010,7 +2425,7 @@ change_window_height (delta, widthflag)
       (*setsizefun) (parent, opht, 0);
     }
 
-  XFASTINT (p->last_modified) = 0;
+  XSETFASTINT (p->last_modified, 0);
 }
 #undef MINSIZE
 #undef CURBEG
@@ -2058,7 +2473,7 @@ window_internal_width (w)
 
   /* Scroll bars occupy a few columns.  */
   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
-    return width - VERTICAL_SCROLL_BAR_WIDTH;
+    return width - FRAME_SCROLL_BAR_COLS (f);
 
   /* The column of `|' characters separating side-by-side windows
      occupies one column only.  */
@@ -2075,28 +2490,28 @@ window_scroll (window, n, noerror)
      int noerror;
 {
   register struct window *w = XWINDOW (window);
-  register int opoint = point;
+  register int opoint = PT;
   register int pos;
   register int ht = window_internal_height (w);
   register Lisp_Object tem;
   int lose;
   Lisp_Object bolp, nmoved;
 
-  XFASTINT (tem) = point;
+  XSETFASTINT (tem, PT);
   tem = Fpos_visible_in_window_p (tem, window);
 
   if (NILP (tem))
     {
-      Fvertical_motion (make_number (- ht / 2));
-      XFASTINT (tem) = point;
+      Fvertical_motion (make_number (- (ht / 2)), window);
+      XSETFASTINT (tem, PT);
       Fset_marker (w->start, tem, w->buffer);
       w->force_start = Qt;
     }
 
   SET_PT (marker_position (w->start));
-  lose = n < 0 && point == BEGV;
-  Fvertical_motion (make_number (n));
-  pos = point;
+  lose = n < 0 && PT == BEGV;
+  Fvertical_motion (make_number (n), window);
+  pos = PT;
   bolp = Fbolp ();
   SET_PT (opoint);
 
@@ -2113,17 +2528,17 @@ window_scroll (window, n, noerror)
       set_marker_restricted (w->start, make_number (pos), w->buffer);
       w->start_at_line_beg = bolp;
       w->update_mode_line = Qt;
-      XFASTINT (w->last_modified) = 0;
+      XSETFASTINT (w->last_modified, 0);
       if (pos > opoint)
        SET_PT (pos);
       if (n < 0)
        {
          SET_PT (pos);
-         tem = Fvertical_motion (make_number (ht));
-         if (point > opoint || XFASTINT (tem) < ht)
+         tem = Fvertical_motion (make_number (ht), window);
+         if (PT > opoint || XFASTINT (tem) < ht)
            SET_PT (opoint);
          else
-           Fvertical_motion (make_number (-1));
+           Fvertical_motion (make_number (-1), window);
        }
     }
   else
@@ -2173,6 +2588,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;
@@ -2184,6 +2600,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;
@@ -2191,24 +2608,16 @@ When calling from a program, supply a number as argument or nil.")
   scroll_command (n, -1);
   return Qnil;
 }
-
-DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
-  "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\
-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\
-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.")
-  (n)
-     register Lisp_Object n;
+\f
+DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
+  "Return the other window for \"other window scroll\" commands.\n\
+If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
+specifies the window.\n\
+If `other-window-scroll-buffer' is non-nil, a window\n\
+showing that buffer is used.")
+  ()
 {
-  register Lisp_Object window;
-  register int ht;
-  register struct window *w;
-  register int count = specpdl_ptr - specpdl;
+  Lisp_Object window;
 
   if (MINI_WINDOW_P (XWINDOW (selected_window))
       && !NILP (Vminibuf_scroll_window))
@@ -2240,8 +2649,32 @@ showing that buffer, popping the buffer up if necessary.")
   if (EQ (window, selected_window))
     error ("There is no other window");
 
+  return window;
+}
+
+DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
+  "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.  Negative ARG means scroll downward.\n\
+When calling from a program, supply a number as argument or nil.\n\
+\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.")
+  (n)
+     register Lisp_Object n;
+{
+  register Lisp_Object window;
+  register int defalt;
+  register struct window *w;
+  register int count = specpdl_ptr - specpdl;
+
+  window = Fother_window_for_scrolling ();
+
   w = XWINDOW (window);
-  ht = window_internal_height (w);
+  defalt = window_internal_height (w) - next_screen_context_lines;
+  if (defalt < 1) defalt = 1;
 
   /* Don't screw up if window_scroll gets an error.  */
   record_unwind_protect (save_excursion_restore, save_excursion_save ());
@@ -2250,24 +2683,24 @@ showing that buffer, popping the buffer up if necessary.")
   SET_PT (marker_position (w->pointm));
 
   if (NILP (n))
-    window_scroll (window, ht - next_screen_context_lines, 1);
+    window_scroll (window, defalt, 1);
   else if (EQ (n, Qminus))
-    window_scroll (window, next_screen_context_lines - ht, 1);
+    window_scroll (window, -defalt, 1);
   else
     {
-      if (XTYPE (n) == Lisp_Cons)
+      if (CONSP (n))
        n = Fcar (n);
       CHECK_NUMBER (n, 0);
       window_scroll (window, XINT (n), 1);
     }
 
-  Fset_marker (w->pointm, make_number (point), Qnil);
+  Fset_marker (w->pointm, make_number (PT), Qnil);
   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)
@@ -2275,7 +2708,7 @@ Default for ARG is window width minus 2.")
 {
 
   if (NILP (arg))
-    XFASTINT (arg) = window_internal_width (XWINDOW (selected_window)) - 2;
+    XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
   else
     arg = Fprefix_numeric_value (arg);
 
@@ -2285,14 +2718,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) = window_internal_width (XWINDOW (selected_window)) - 2;
+    XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
   else
     arg = Fprefix_numeric_value (arg);
 
@@ -2313,18 +2746,19 @@ 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;
+  struct position pos;
+  Lisp_Object window;
 
   if (NILP (n))
     {
       extern int frame_garbaged;
 
       SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
-      XFASTINT (n) = ht / 2;
+      XSETFASTINT (n, ht / 2);
     }
-  else if (XTYPE (n) == Lisp_Cons) /* Just C-u. */
+  else if (CONSP (n)) /* Just C-u. */
     {
-      XFASTINT (n) = ht / 2;
+      XSETFASTINT (n, ht / 2);
     }
   else
     {
@@ -2335,13 +2769,14 @@ redraws with point in the center of the current window.")
   if (XINT (n) < 0)
     XSETINT (n, XINT (n) + ht);
 
-  XSETINT (n, - XINT (n));
-
-  Fvertical_motion (n);
-  Fset_marker (w->start, make_number (point), w->buffer);
-  w->start_at_line_beg = Fbolp ();
+  XSETWINDOW (window, w);
+  pos = *vmotion (point, - XINT (n), window_internal_width (w) - 1,
+                 XINT (w->hscroll), window);
 
-  SET_PT (opoint);
+  Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
+  w->start_at_line_beg = ((pos.bufpos == BEGV
+                          || FETCH_CHAR (pos.bufpos - 1) == '\n')
+                         ? Qt : Qnil);
   w->force_start = Qt;
 
   return Qnil;
@@ -2350,7 +2785,7 @@ redraws with point in the center of the current window.")
 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
   1, 1, "P",
   "Position point relative to window.\n\
-With no argument, position text at center of window.\n\
+With no argument, position point at center of window.\n\
 An argument specifies frame line; zero means top of window,\n\
 negative means relative to bottom of window.")
   (arg)
@@ -2359,9 +2794,10 @@ 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;
+    XSETFASTINT (arg, height / 2);
   else
     {
       arg = Fprefix_numeric_value (arg);
@@ -2370,42 +2806,38 @@ negative means relative to bottom of window.")
     }
 
   start = marker_position (w->start);
+  XSETWINDOW (window, w);
   if (start < BEGV || start > ZV)
     {
-      Fvertical_motion (make_number (- height / 2));
-      Fset_marker (w->start, make_number (point), w->buffer);
+      Fvertical_motion (make_number (- (height / 2)), window);
+      Fset_marker (w->start, make_number (PT), w->buffer);
       w->start_at_line_beg = Fbolp ();
       w->force_start = Qt;
     }
   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;
+    /* Record the values of window-min-width and window-min-height
+       so that window sizes remain consistent with them.  */
+    Lisp_Object min_width, min_height;
     /* A vector, interpreted as a struct saved_window */
     Lisp_Object saved_windows;
   };
 
-/* 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
   {
@@ -2430,15 +2862,14 @@ DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_
   (obj)
      Lisp_Object obj;
 {
-  if (XTYPE (obj) == Lisp_Window_Configuration)
+  if (WINDOW_CONFIGURATIONP (obj))
     return Qt;
   return Qnil;
 }
 
 
-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).")
@@ -2451,7 +2882,7 @@ by `current-window-configuration' (which see).")
   Lisp_Object frame;
   FRAME_PTR f;
 
-  while (XTYPE (configuration) != Lisp_Window_Configuration)
+  while (!WINDOW_CONFIGURATIONP (configuration))
     {
       configuration = wrong_type_argument (intern ("window-configuration-p"),
                                           configuration);
@@ -2481,12 +2912,24 @@ by `current-window-configuration' (which see).")
         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);
+#ifdef HAVE_X_WINDOWS
+      if (XFASTINT (data->frame_menu_bar_lines)
+         != previous_frame_menu_bar_lines)
+       x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
+#endif
 
       windows_or_buffers_changed++;
+      FRAME_WINDOW_SIZES_CHANGED (f) = 1;
+
+      /* Temporarily avoid any problems with windows that are smaller
+        than they are supposed to be.  */
+      window_min_height = 1;
+      window_min_width = 1;
 
       /* Kludge Alert!
         Mark all windows now on frame as "deleted".
@@ -2535,7 +2978,7 @@ by `current-window-configuration' (which see).")
 
          /* If we squirreled away the buffer in the window's height,
             restore it now.  */
-         if (XTYPE (w->height) == Lisp_Buffer)
+         if (BUFFERP (w->height))
            w->buffer = w->height;
          w->left = p->left;
          w->top = p->top;
@@ -2543,7 +2986,7 @@ by `current-window-configuration' (which see).")
          w->height = p->height;
          w->hscroll = p->hscroll;
          w->display_table = p->display_table;
-         XFASTINT (w->last_modified) = 0;
+         XSETFASTINT (w->last_modified, 0);
 
          /* Reinstall the saved buffer and pointers into it.  */
          if (NILP (p->buffer))
@@ -2567,12 +3010,12 @@ by `current-window-configuration' (which see).")
                  /* 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)
+                 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.  */
+             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
@@ -2583,8 +3026,7 @@ by `current-window-configuration' (which see).")
                }
              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.  */
+                  are real.  */
                {
                  /* Set window markers at start of visible range.  */
                  if (XMARKER (w->start)->buffer == 0)
@@ -2605,7 +3047,7 @@ by `current-window-configuration' (which see).")
 
 #ifdef MULTI_FRAME
       if (NILP (data->focus_frame)
-         || (XTYPE (data->focus_frame) == Lisp_Frame
+         || (FRAMEP (data->focus_frame)
              && FRAME_LIVE_P (XFRAME (data->focus_frame))))
        Fredirect_frame_focus (frame, data->focus_frame);
 #endif
@@ -2614,7 +3056,7 @@ by `current-window-configuration' (which see).")
          when the frame's old selected window has been deleted.  */
 #ifdef MULTI_FRAME
       if (f != selected_frame && ! FRAME_TERMCAP_P (f))
-       Fselect_frame (WINDOW_FRAME (XWINDOW (data->root_window)), Qnil);
+       Fhandle_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)), Qnil);
 #endif
 #endif
 
@@ -2623,16 +3065,24 @@ by `current-window-configuration' (which see).")
          || previous_frame_width != FRAME_WIDTH (f))
        change_frame_size (f, previous_frame_height, previous_frame_width,
                           0, 0);
+#ifdef HAVE_X_WINDOWS
+      if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
+       x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
+#endif
     }
 
+  /* Restore the minimum heights recorded in the configuration.  */
+  window_min_height = XINT (data->min_height);
+  window_min_width = XINT (data->min_width);
+
 #ifdef MULTI_FRAME
   /* Fselect_window will have made f the selected frame, so we
-     reselect the proper frame here.  Fselect_frame will change the
+     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)))
-    Fselect_frame (data->selected_frame, Qnil);
+    Fhandle_switch_frame (data->selected_frame, Qnil);
 #endif
 
   if (!NILP (new_current_buffer))
@@ -2696,7 +3146,7 @@ save_window_save (window, vector, i)
       p = SAVED_WINDOW_N (vector, i);
       w = XWINDOW (window);
 
-      XFASTINT (w->temslot) = i++;
+      XSETFASTINT (w->temslot, i++);
       p->window = window;
       p->buffer = w->buffer;
       p->left = w->left;
@@ -2768,6 +3218,7 @@ redirection (see `redirect-frame-focus').")
   register Lisp_Object tem;
   register int n_windows;
   register struct save_window_data *data;
+  register struct Lisp_Vector *vec;
   register int i;
   FRAME_PTR f;
 
@@ -2780,19 +3231,25 @@ redirection (see `redirect-frame-focus').")
     }
 
   n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
-  data = (struct save_window_data *)
-           XVECTOR (Fmake_vector (make_number (SAVE_WINDOW_DATA_SIZE),
-                                 Qnil));
-  XFASTINT (data->frame_width) = FRAME_WIDTH (f);
-  XFASTINT (data->frame_height) = FRAME_HEIGHT (f);
+  vec = allocate_vectorlike (VECSIZE (struct save_window_data));
+  for (i = 0; i < VECSIZE (struct save_window_data); i++)
+    vec->contents[i] = Qnil;
+  vec->size = VECSIZE (struct save_window_data);
+  data = (struct save_window_data *)vec;
+
+  XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
+  XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
+  XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
 #ifdef MULTI_FRAME
-  XSET (data->selected_frame, Lisp_Frame, selected_frame);
+  XSETFRAME (data->selected_frame, selected_frame);
 #endif
   data->current_window = FRAME_SELECTED_WINDOW (f);
-  XSET (data->current_buffer, Lisp_Buffer, current_buffer);
+  XSETBUFFER (data->current_buffer, current_buffer);
   data->minibuf_scroll_window = Vminibuf_scroll_window;
   data->root_window = FRAME_ROOT_WINDOW (f);
   data->focus_frame = FRAME_FOCUS_FRAME (f);
+  XSETINT (data->min_height, window_min_height);
+  XSETINT (data->min_width, window_min_width);
   tem = Fmake_vector (make_number (n_windows), Qnil);
   data->saved_windows = tem;
   for (i = 0; i < n_windows; i++)
@@ -2800,15 +3257,17 @@ redirection (see `redirect-frame-focus').")
       = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
   save_window_save (FRAME_ROOT_WINDOW (f),
                    XVECTOR (tem), 0);
-  XSET (tem, Lisp_Window_Configuration, data);
+  XSETWINDOW_CONFIGURATION (tem, data);
   return (tem);
 }
 
 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
   0, UNEVALLED, 0,
   "Execute body, preserving window sizes and contents.\n\
-Restores which buffer appears in which window, where display starts,\n\
-as well as the current buffer.\n\
+Restore which buffer appears in which window, where display starts,\n\
+and the value of point and mark for each window.\n\
+Also restore which buffer is current.\n\
+But do not preserve point in the current buffer.\n\
 Does not restore the value of point in current buffer.")
   (args)
      Lisp_Object args;
@@ -2832,6 +3291,8 @@ init_window_once ()
 #else /* not MULTI_FRAME */
   extern Lisp_Object get_minibuffer ();
 
+  selected_frame = last_nonminibuf_frame = &the_only_frame;
+
   minibuf_window = make_window ();
   FRAME_ROOT_WINDOW (selected_frame) = make_window ();
 
@@ -2843,12 +3304,12 @@ init_window_once ()
      just so that there is "something there."
      Correct values are put in in init_xdisp */
 
-  XFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->width) = 10;
-  XFASTINT (XWINDOW (minibuf_window)->width) = 10;
+  XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->width, 10);
+  XSETFASTINT (XWINDOW (minibuf_window)->width, 10);
 
-  XFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->height) = 9;
-  XFASTINT (XWINDOW (minibuf_window)->top) = 9;
-  XFASTINT (XWINDOW (minibuf_window)->height) = 1;
+  XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->height, 9);
+  XSETFASTINT (XWINDOW (minibuf_window)->top, 9);
+  XSETFASTINT (XWINDOW (minibuf_window)->height, 1);
 
   /* Prevent error in Fset_window_buffer.  */
   XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->buffer = Qt;
@@ -2864,7 +3325,7 @@ init_window_once ()
      a newly-created, never-selected window.  Increment
      window_select_count so the first selection ever will get
      something newer than this.  */
-  XFASTINT (XWINDOW (selected_window)->use_time) = ++window_select_count;
+  XSETFASTINT (XWINDOW (selected_window)->use_time, ++window_select_count);
 #endif /* not MULTI_FRAME */
 }
 
@@ -2873,8 +3334,11 @@ syms_of_window ()
   Qwindowp = intern ("windowp");
   staticpro (&Qwindowp);
 
-  Qlive_window_p = intern ("live-window-p");
-  staticpro (&Qlive_window_p);
+  Qwindow_live_p = intern ("window-live-p");
+  staticpro (&Qwindow_live_p);
+
+  Qtemp_buffer_show_hook = intern ("Qtemp-buffer-show-hook");
+  staticpro (&Qtemp_buffer_show_hook);
 
 #ifndef MULTI_FRAME
   /* Make sure all windows get marked */
@@ -2883,6 +3347,7 @@ syms_of_window ()
 
   DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
     "Non-nil means call as function to display a help buffer.\n\
+The function is called with one argument, the buffer to be displayed.\n\
 Used by `with-output-to-temp-buffer'.");
   Vtemp_buffer_show_function = Qnil;
 
@@ -2894,21 +3359,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-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 scroll bar;\n\
-then Y-POS is really the total length of the scroll bar, while X-POS is\n\
-the relative position of the scroll bar's value within that total length.\n\
-FRAME-PART is one of the following symbols:\n\
- `vertical-scroll-bar', `vertical-slider',\n\
- `vertical-thumbup', `vertical-thumbdown',\n\
- `horizontal-scroll-bar', `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;
@@ -2917,19 +3367,83 @@ 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 frame.");
   pop_up_frames = 0;
 
   DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
-    "*If non-nil, function to call to handle automatic new frame creation.\n\
+    "Function to call to handle automatic new frame creation.\n\
 It is called with no arguments and should return a newly created frame.\n\
 \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_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
+    "*List of buffer names that should have their own special frames.\n\
+Displaying a buffer whose name is in this list makes a special frame for it\n\
+using `special-display-function'.\n\
+\n\
+An element of the list can be a cons cell instead of just a string.\n\
+Then the car should be a buffer name, and the cdr specifies frame\n\
+parameters for creating the frame for that buffer.\n\
+More precisely, the cdr is passed as the second argument to\n\
+the function found in `special-display-function', when making that frame.\n\
+See also `special-display-regexps'.");
+  Vspecial_display_buffer_names = Qnil;
+
+  DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
+    "*List of regexps saying which buffers should have their own special frames.\n\
+If a buffer name matches one of these regexps, it gets its own frame.\n\
+Displaying a buffer whose name is in this list makes a special frame for it\n\
+using `special-display-function'.\n\
+\n\
+An element of the list can be a cons cell instead of just a string.\n\
+Then the car should be the regexp, and the cdr specifies frame\n\
+parameters for creating the frame for buffers that match.\n\
+More precisely, the cdr is passed as the second argument to\n\
+the function found in `special-display-function', when making that frame.\n\
+See also `special-display-buffer-names'.");
+  Vspecial_display_regexps = Qnil;
+
+  DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
+    "Function to call to make a new frame for a special buffer.\n\
+It is called with two arguments, the buffer and optional buffer specific\n\
+data, and should return a window displaying that buffer.\n\
+The default value makes a separate frame for the buffer,\n\
+using `special-display-frame-alist' to specify the frame parameters.\n\
+\n\
+A buffer is special if its is listed in `special-display-buffer-names'\n\
+or matches a regexp in `special-display-regexps'.");
+  Vspecial_display_function = Qnil;
+
+  DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
+    "*List of buffer names that should appear in the selected window.\n\
+Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
+switches to it in the selected window, rather than making it appear\n\
+in some other window.
+\n\
+An element of the list can be a cons cell instead of just a string.\n\
+Then the car must be a string, which specifies the buffer name.\n\
+This is for compatibility with `special-display-buffer-names';\n\
+the cdr of the cons cell is ignored.\n\
+\n\
+See also `same-window-regexps'.");
+  Vsame_window_buffer_names = Qnil;
+
+  DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
+    "*List of regexps saying which buffers should appear in the selected window.\n\
+If a buffer name matches one of these regexps, then displaying it\n\
+using `display-buffer' or `pop-to-buffer' switches to it\n\
+in the selected window, rather than making it appear in some other window.\n\
+\n\
+An element of the list can be a cons cell instead of just a string.\n\
+Then the car must be a string, which specifies the buffer name.\n\
+This is for compatibility with `special-display-buffer-names';\n\
+the cdr of the cons cell is ignored.\n\
+\n\
+See also `same-window-buffer-names'.");
+  Vsame_window_regexps = Qnil;
 
   DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
     "*Non-nil means display-buffer should make new windows.");
@@ -2956,7 +3470,7 @@ If there is only one window, it is split regardless of this value.");
   defsubr (&Sminibuffer_window);
   defsubr (&Swindow_minibuffer_p);
   defsubr (&Swindowp);
-  defsubr (&Slive_window_p);
+  defsubr (&Swindow_live_p);
   defsubr (&Spos_visible_in_window_p);
   defsubr (&Swindow_buffer);
   defsubr (&Swindow_height);
@@ -2995,6 +3509,7 @@ If there is only one window, it is split regardless of this value.");
   defsubr (&Sscroll_down);
   defsubr (&Sscroll_left);
   defsubr (&Sscroll_right);
+  defsubr (&Sother_window_for_scrolling);
   defsubr (&Sscroll_other_window);
   defsubr (&Srecenter);
   defsubr (&Smove_to_window_line);