(mail-setup): Call set-buffer-multibyte because
[bpt/emacs.git] / src / window.c
index b90d8b4..baa55a5 100644 (file)
@@ -1,6 +1,7 @@
 /* Window creation, deletion and examination for GNU Emacs.
    Does not include redisplay.
-   Copyright (C) 1985,86,87,93,94,95,96,97,1998 Free Software Foundation, Inc.
+   Copyright (C) 1985,86,87,93,94,95,96,97,1998,2000
+   Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -22,13 +23,13 @@ Boston, MA 02111-1307, USA.  */
 #include <config.h>
 #include "lisp.h"
 #include "buffer.h"
+#include "keyboard.h"
 #include "frame.h"
 #include "window.h"
 #include "commands.h"
 #include "indent.h"
 #include "termchar.h"
 #include "disptab.h"
-#include "keyboard.h"
 #include "dispextern.h"
 #include "blockinput.h"
 #include "intervals.h"
@@ -42,6 +43,9 @@ Boston, MA 02111-1307, USA.  */
 #ifdef MSDOS
 #include "msdos.h"
 #endif
+#ifdef macintosh
+#include "macterm.h"
+#endif
 
 #ifndef max
 #define max(a, b) ((a) < (b) ? (b) : (a))
@@ -76,6 +80,7 @@ static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
 static int foreach_window_1 P_ ((struct window *,
                                 int (* fn) (struct window *, void *),
                                 void *));
+static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
 
 /* This is the window in which the terminal's cursor should
    be left when nothing is being done with it.  This must
@@ -126,6 +131,10 @@ int pop_up_windows;
 
 int pop_up_frames;
 
+/* Nonzero means reuse existing frames for displaying buffers.  */
+
+int display_buffer_reuse_frames;
+
 /* Non-nil means use this function instead of default */
 
 Lisp_Object Vpop_up_frame_function;
@@ -134,6 +143,10 @@ Lisp_Object Vpop_up_frame_function;
 
 Lisp_Object Vdisplay_buffer_function;
 
+/* Non-nil means that Fdisplay_buffer should even the heights of windows.  */
+
+Lisp_Object Veven_window_heights;
+
 /* List of buffer *names* for buffers that should have their own frames.  */
 
 Lisp_Object Vspecial_display_buffer_names;
@@ -231,6 +244,7 @@ make_window ()
   XSETFASTINT (p->height, 0);
   XSETFASTINT (p->width, 0);
   XSETFASTINT (p->hscroll, 0);
+  XSETFASTINT (p->min_hscroll, 0);
   p->orig_top = p->orig_height = Qnil;
   p->start = Fmake_marker ();
   p->pointm = Fmake_marker ();
@@ -283,46 +297,60 @@ DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1,
      Lisp_Object window;
 {
   struct window *w = decode_window (window);
-  return (MINI_WINDOW_P (w) ? Qt : Qnil);
+  return MINI_WINDOW_P (w) ? Qt : Qnil;
 }
 
+
 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
-  Spos_visible_in_window_p, 0, 2, 0,
+  Spos_visible_in_window_p, 0, 3, 0,
   "Return t if position POS is currently on the frame in WINDOW.\n\
-Returns nil if that position is scrolled vertically out of view.\n\
-POS defaults to point; WINDOW, to the selected window.")
-  (pos, window)
-     Lisp_Object pos, window;
+Return nil if that position is scrolled vertically out of view.\n\
+If a character is only partially visible, nil is returned, unless the\n\
+optional argument PARTIALLY is non-nil.\n\
+POS defaults to point in WINDOW; WINDOW defaults to the selected window.")
+  (pos, window, partially)
+     Lisp_Object pos, window, partially;
 {
   register struct window *w;
   register int posint;
   register struct buffer *buf;
   struct text_pos top;
   Lisp_Object in_window;
+  int fully_p;
 
-  if (NILP (pos))
-    posint = PT;
-  else
+  w = decode_window (window);
+  buf = XBUFFER (w->buffer);
+  SET_TEXT_POS_FROM_MARKER (top, w->start);
+
+  if (!NILP (pos))
     {
       CHECK_NUMBER_COERCE_MARKER (pos, 0);
       posint = XINT (pos);
     }
+  else if (w == XWINDOW (selected_window))
+    posint = PT;
+  else
+    posint = XMARKER (w->pointm)->charpos;
 
-  w = decode_window (window);
-  buf = XBUFFER (w->buffer);
-  SET_TEXT_POS_FROM_MARKER (top, w->start);
-
-  /* If position above window, it's not visible.  */
+  /* If position is above window start, it's not visible.  */
   if (posint < CHARPOS (top))
     in_window = Qnil;
   else if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
-      && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
-      && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
-    /* If frame is up to date, and POSINT is < window end pos, use
-       that info.  This doesn't work for POSINT == end pos, because
-       the window end pos is actually the position _after_ the last
-       char in the window.  */
-    in_window = Qt;
+          && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
+          && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
+    {
+      /* If frame is up-to-date, and POSINT is < window end pos, use
+        that info.  This doesn't work for POSINT == end pos, because
+        the window end pos is actually the position _after_ the last
+        char in the window.  */
+      if (NILP (partially))
+       {
+         pos_visible_p (w, posint, &fully_p, NILP (partially));
+         in_window = fully_p ? Qt : Qnil;
+       }
+      else
+       in_window = Qt;
+    }
   else if (posint > BUF_ZV (buf))
     in_window = Qnil;
   else if (CHARPOS (top) < BUF_BEGV (buf) || CHARPOS (top) > BUF_ZV (buf))
@@ -330,15 +358,15 @@ POS defaults to point; WINDOW, to the selected window.")
     in_window = Qnil;
   else
     {
-      struct it it;
-      start_display (&it, w, top);
-      move_it_to (&it, posint, 0, it.last_visible_y, -1,
-                 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
-      in_window = IT_CHARPOS (it) == posint ? Qt : Qnil;
+      if (pos_visible_p (w, posint, &fully_p, NILP (partially)))
+       in_window = !NILP (partially) || fully_p ? Qt : Qnil;
+      else
+       in_window = Qnil;
     }
 
   return in_window;
 }
+
 \f
 static struct window *
 decode_window (window)
@@ -390,17 +418,19 @@ DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
   "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
 NCOL should be zero or positive.")
   (window, ncol)
-     register Lisp_Object window, ncol;
+     Lisp_Object window, ncol;
 {
-  register struct window *w;
+  struct window *w = decode_window (window);
+  int hscroll;
 
   CHECK_NUMBER (ncol, 1);
-  if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
-  w = decode_window (window);
-  if (XINT (w->hscroll) != XINT (ncol))
-    /* Prevent redisplay shortcuts */
+  hscroll = max (0, XINT (ncol));
+  
+  /* Prevent redisplay shortcuts when changing the hscroll.  */
+  if (XINT (w->hscroll) != hscroll)
     XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
-  w->hscroll = ncol;
+  
+  w->hscroll = make_number (hscroll);
   return ncol;
 }
 
@@ -470,16 +500,28 @@ coordinates_in_window (w, x, y)
      register struct window *w;
      register int *x, *y;
 {
+  /* Let's make this a global enum later, instead of using numbers
+     everywhere.  */
+  enum {ON_NOTHING, ON_TEXT, ON_MODE_LINE, ON_VERTICAL_BORDER,
+       ON_HEADER_LINE, ON_LEFT_FRINGE, ON_RIGHT_FRINGE};
+
   struct frame *f = XFRAME (WINDOW_FRAME (w));
   int left_x, right_x, top_y, bottom_y;
   int flags_area_width = FRAME_LEFT_FLAGS_AREA_WIDTH (f);
+  int part;
+  int ux = CANON_X_UNIT (f), uy = CANON_Y_UNIT (f);
+  int x0 = XFASTINT (w->left) * ux;
+  int x1 = x0 + XFASTINT (w->width) * ux;
 
+  if (*x < x0 || *x >= x1)
+    return ON_NOTHING;
+  
   /* In what's below, we subtract 1 when computing right_x because we
      want the rightmost pixel, which is given by left_pixel+width-1.  */
   if (w->pseudo_window_p)
     {
       left_x = 0;
-      right_x = XFASTINT (w->width) * CANON_Y_UNIT (f) - 1;
+      right_x = XFASTINT (w->width) * CANON_X_UNIT (f) - 1;
       top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
       bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
     }
@@ -493,48 +535,116 @@ coordinates_in_window (w, x, y)
       bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
     }
 
-  if (*y < top_y
-      || *y >= bottom_y
-      || *x < (left_x
-              - flags_area_width
-              - (FRAME_LEFT_SCROLL_BAR_WIDTH (f)
-                 * CANON_X_UNIT (f)))
-      || *x > right_x + flags_area_width)
-    /* Completely outside anything interesting.  */
-    return 0;
-  else if (WINDOW_WANTS_MODELINE_P (w)
-          && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
-    /* On the mode line.  */
-    return 2;
+  /* On the mode line or header line?  If it's near the start of
+     the mode or header line of window that's has a horizontal
+     sibling, say it's on the vertical line.  That's to be able
+     to resize windows horizontally in case we're using toolkit
+     scroll bars.  */
+
+  if (WINDOW_WANTS_MODELINE_P (w)
+      && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
+      && *y < bottom_y)
+    {
+      /* We're somewhere on the mode line.  We consider the place
+        between mode lines of horizontally adjacent mode lines
+        as the vertical border.    If scroll bars on the left,
+        return the right window.  */
+      part = ON_MODE_LINE;
+      
+      if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
+       {
+         if (abs (*x - x0) < ux / 2)
+           part = ON_VERTICAL_BORDER;
+       }
+      else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < ux / 2)
+       part = ON_VERTICAL_BORDER;
+    }
   else if (WINDOW_WANTS_HEADER_LINE_P (w)
-          && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
-    /* On the top line.  */
-    return 4;
-  else if (*x < left_x || *x >= right_x)
+          && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
+          && *y >= top_y)
     {
-      /* Other lines than the mode line don't include flags areas and
-        scroll bars on the left.  */
+      part = ON_HEADER_LINE;
       
-      /* Convert X and Y to window-relative pixel coordinates.  */
-      *x -= left_x;
-      *y -= top_y;
-      return *x < left_x ? 5 : 6;
+      if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
+       {
+         if (abs (*x - x0) < ux / 2)
+           part = ON_VERTICAL_BORDER;
+       }
+      else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < ux / 2)
+       part = ON_VERTICAL_BORDER;
+    }
+  /* Outside anything interesting?  */
+  else if (*y < top_y
+          || *y >= bottom_y
+          || *x < (left_x
+                   - flags_area_width
+                   - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * ux)
+          || *x > right_x + flags_area_width)
+    {
+      part = ON_NOTHING;
+    }
+  else if (FRAME_WINDOW_P (f))
+    {
+      if (!w->pseudo_window_p
+         && !FRAME_HAS_VERTICAL_SCROLL_BARS (f)
+         && !WINDOW_RIGHTMOST_P (w)
+         && (abs (*x - right_x - flags_area_width) < ux / 2))
+       {
+         part = ON_VERTICAL_BORDER;
+       }
+      else if (*x < left_x || *x > right_x)
+       {
+         /* Other lines than the mode line don't include flags areas and
+            scroll bars on the left.  */
+      
+         /* Convert X and Y to window-relative pixel coordinates.  */
+         *x -= left_x;
+         *y -= top_y;
+         part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
+       }
+      else
+       {
+         *x -= left_x;
+         *y -= top_y;
+         part = ON_TEXT;
+       }
     }
-  else if (!w->pseudo_window_p
-          && !WINDOW_RIGHTMOST_P (w)
-          && *x >= right_x - CANON_X_UNIT (f))
-    /* On the border on the right side of the window?  Assume that
-       this area begins at RIGHT_X minus a canonical char width.  */
-    return 3;
   else
     {
-      /* Convert X and Y to window-relative pixel coordinates.  */
-      *x -= left_x;
-      *y -= top_y;
-      return 1;
+      /* Need to say "*x > right_x" rather than >=, since on character
+        terminals, the vertical line's x coordinate is right_x.  */
+      if (*x < left_x || *x > right_x)
+       {
+         /* Other lines than the mode line don't include flags areas and
+            scroll bars on the left.  */
+      
+         /* Convert X and Y to window-relative pixel coordinates.  */
+         *x -= left_x;
+         *y -= top_y;
+         part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
+       }
+      /* Here, too, "*x > right_x" is because of character terminals.  */
+      else if (!w->pseudo_window_p
+              && !WINDOW_RIGHTMOST_P (w)
+              && *x > right_x - ux)
+       {
+         /* On the border on the right side of the window?  Assume that
+            this area begins at RIGHT_X minus a canonical char width.  */
+         part = ON_VERTICAL_BORDER;
+       }
+      else
+       {
+         /* Convert X and Y to window-relative pixel coordinates.  */
+         *x -= left_x;
+         *y -= top_y;
+         part = ON_TEXT;
+       }
     }
+
+  return part;
 }
 
+
 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
   Scoordinates_in_window_p, 2, 2, 0,
   "Return non-nil if COORDINATES are in WINDOW.\n\
@@ -609,7 +719,7 @@ If they are on the border between WINDOW and its right sibling,\n\
 
    Check if window W contains coordinates *CW->x and *CW->y.  If it
    does, return W in *CW->window, as Lisp_Object, and return in
-   *CW->part the part of the window under coordinates *X/*Y.  Return
+   *CW->part the part of the window under coordinates *X,*Y.  Return
    zero from this function to stop iterating over windows.  */
 
 struct check_window_data
@@ -754,7 +864,7 @@ DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
 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.\n\
-If UP-TO-DATE is non-nil, compute the up-to-date position\n\
+If UPDATE is non-nil, compute the up-to-date position\n\
 if it isn't already recorded.")
   (window, update)
      Lisp_Object window, update;
@@ -779,29 +889,30 @@ if it isn't already recorded.")
       && ! (! NILP (w->window_end_valid)
            && XFASTINT (w->last_modified) >= MODIFF))
     {
-      int opoint = PT, opoint_byte = PT_BYTE;
+      struct text_pos startp;
+      struct it it;
 
       /* In case W->start is out of the range, use something
          reasonable.  This situation occured when loading a file with
          `-l' containing a call to `rmail' with subsequent other
          commands.  At the end, W->start happened to be BEG, while
-         rmail had already narrowed the buffer.  This leads to an
-         abort in temp_set_pt_both.  */
+         rmail had already narrowed the buffer.  */
       if (XMARKER (w->start)->charpos < BEGV)
-       TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
+       SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
       else if (XMARKER (w->start)->charpos > ZV)
-       TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
+       SET_TEXT_POS (startp, ZV, ZV_BYTE);
       else
-       TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
-                         XMARKER (w->start)->bytepos);
-      
-      Fvertical_motion (make_number (window_internal_height (w)), Qnil);
-      XSETINT (value, PT);
-      TEMP_SET_PT_BOTH (opoint, opoint_byte);
+       SET_TEXT_POS_FROM_MARKER (startp, w->start);
+
+      /* Cannot use Fvertical_motion because that function doesn't
+        cope with variable-height lines.  */
+      start_display (&it, w, startp);
+      move_it_vertically (&it, window_box_height (w));
+      move_it_past_eol (&it);
+      value = make_number (IT_CHARPOS (it));
     }
   else
-    XSETINT (value,
-            BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
+    XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
 
   return value;
 }
@@ -819,6 +930,11 @@ DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
     Fgoto_char (pos);
   else
     set_marker_restricted (w->pointm, pos, w->buffer);
+
+  /* We have to make sure that redisplay updates the window to show
+     the new value of point.  */
+  if (!EQ (window, selected_window))
+    ++windows_or_buffers_changed;
   
   return pos;
 }
@@ -1049,7 +1165,7 @@ delete_window (window)
   register Lisp_Object tem, parent, sib;
   register struct window *p;
   register struct window *par;
-  FRAME_PTR frame;
+  struct frame *f;
 
   /* Because this function is called by other C code on non-leaf
      windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
@@ -1073,18 +1189,17 @@ delete_window (window)
 
   windows_or_buffers_changed++;
   Vwindow_list = Qnil;
-  frame = XFRAME (WINDOW_FRAME (p));
-  FRAME_WINDOW_SIZES_CHANGED (frame) = 1;
+  f = XFRAME (WINDOW_FRAME (p));
+  FRAME_WINDOW_SIZES_CHANGED (f) = 1;
 
   /* Are we trying to delete any frame's selected window?  */
   {
-    Lisp_Object frame, pwindow;
+    Lisp_Object 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));
+    pwindow = FRAME_SELECTED_WINDOW (f);
 
     while (!NILP (pwindow))
       {
@@ -1106,7 +1221,7 @@ delete_window (window)
        if (EQ (window, selected_window))
          Fselect_window (alternative);
        else
-         FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
+         FRAME_SELECTED_WINDOW (f) = alternative;
       }
   }
 
@@ -1125,7 +1240,7 @@ delete_window (window)
      events and other events that access glyph matrices are not
      processed while we are changing them.  */
   BLOCK_INPUT;
-  free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (frame)));
+  free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
 
   tem = p->next;
   if (!NILP (tem))
@@ -1182,7 +1297,7 @@ delete_window (window)
   p->buffer = p->hchild = p->vchild = Qnil;
 
   /* Adjust glyph matrices. */
-  adjust_glyphs (frame);
+  adjust_glyphs (f);
   UNBLOCK_INPUT;
 }
 
@@ -1526,9 +1641,34 @@ argument ALL_FRAMES is non-nil, cycle through all frames.")
 
 
 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
-  "Return a list of windows in canonical ordering.\n\
-Arguments are like for `next-window'.")
-  (window, minibuf, all_frames)
+  "Return a list of windows on FRAME, starting with WINDOW.\n\
+FRAME nil or omitted means use the selected frame.\n\
+WINDOW nil or omitted means use the selected window.\n\
+MINIBUF t means include the minibuffer window, even if it isn't active.\n\
+MINIBUF nil or omitted means include the minibuffer window only\n\
+if it's active.\n\
+MINIBUF neither nil nor t means never include the minibuffer window.")
+  (frame, minibuf, window)
+     Lisp_Object frame, minibuf, window;
+{
+
+  if (NILP (window))
+    window = selected_window;
+  if (NILP (frame))
+    frame = selected_frame;
+
+  if (!EQ (frame, XWINDOW (window)->frame))
+    error ("Window is on a different frame");
+
+  return window_list_1 (window, minibuf, frame);
+}
+
+
+/* Return a list of windows in canonical ordering.  Arguments are like
+   for `next-window'.  */
+
+static Lisp_Object
+window_list_1 (window, minibuf, all_frames)
      Lisp_Object window, minibuf, all_frames;
 {
   Lisp_Object tail, list;
@@ -1551,8 +1691,7 @@ Arguments are like for `next-window'.")
                 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.
-*/
+   If MINI is non-zero, perform the operation on minibuffer windows too.  */
 
 enum window_loop
 {
@@ -1614,7 +1753,7 @@ window_loop (type, obj, mini, frames)
      We can't just wait until we hit the first window again, because
      it might be deleted.  */
 
-  windows = Fwindow_list (window, mini ? Qt : Qnil, frame_arg);
+  windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
   GCPRO1 (windows);
   best_window = Qnil;
 
@@ -1706,19 +1845,19 @@ window_loop (type, obj, mini, frames)
 
          case GET_LARGEST_WINDOW:
            {
-             struct window *b;
-             
              /* Ignore dedicated windows and minibuffers.  */
-             if (MINI_WINDOW_P (w)
-                 || !NILP (w->dedicated)
-                 || NILP (best_window))
+             if (MINI_WINDOW_P (w) || !NILP (w->dedicated))
                break;
              
-             b = XWINDOW (best_window);
-             if (NILP (best_window)
-                 || (XFASTINT (w->height) * XFASTINT (w->width)
-                     > (XFASTINT (b->height) * XFASTINT (b->width))))
+             if (NILP (best_window))
                best_window = window;
+             else
+               {
+                 struct window *b = XWINDOW (best_window);
+                 if (XFASTINT (w->height) * XFASTINT (w->width)
+                     > XFASTINT (b->height) * XFASTINT (b->width))
+                   best_window = window;
+               }
            }
            break;
 
@@ -1766,6 +1905,9 @@ window_loop (type, obj, mini, frames)
                && NILP (XBUFFER (w->buffer)->name))
              abort ();
            break;
+
+         case WINDOW_LOOP_UNUSED:
+           break;
          }
     }
 
@@ -1846,13 +1988,12 @@ value is reasonable when this function is called.")
 {
   struct window *w;
   int startpos;
-  int top;
+  int top, new_top;
 
   if (NILP (window))
     window = selected_window;
   else
     CHECK_LIVE_WINDOW (window, 0);
-
   w = XWINDOW (window);
 
   startpos = marker_position (w->start);
@@ -1868,7 +2009,9 @@ value is reasonable when this function is called.")
      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))
+  new_top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
+  if (new_top != top
+      && startpos >= BUF_BEGV (XBUFFER (w->buffer))
       && startpos <= BUF_ZV (XBUFFER (w->buffer)))
     {
       struct position pos;
@@ -1880,6 +2023,7 @@ value is reasonable when this function is called.")
       pos = *vmotion (startpos, -top, w);
 
       set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
+      w->window_end_valid = Qnil;
       w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
                               || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
                              : Qnil);
@@ -2246,29 +2390,28 @@ size_window (window, size, width_p, nodelete_p)
   int old_size, min_size;
 
   check_min_window_sizes ();
+  size = max (0, size);
   
   /* If the window has been "too small" at one point,
      don't delete it for being "too small" in the future.
      Preserve it as long as that is at all possible.  */
   if (width_p)
     {
-      old_size = XFASTINT (w->width);
+      old_size = XINT (w->width);
       min_size = window_min_width;
     }
   else
     {
-      old_size = XFASTINT (w->height);
+      old_size = XINT (w->height);
       min_size = window_min_height;
     }
   
-  if (old_size < window_min_width)
+  if (old_size < min_size)
     w->too_small_ok = Qt;
 
   /* Maybe delete WINDOW if it's too small.  */
   if (!nodelete_p && !NILP (w->parent))
     {
-      int min_size;
-
       if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
        min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
       else
@@ -2282,22 +2425,22 @@ size_window (window, size, width_p, nodelete_p)
     }
 
   /* Set redisplay hints.  */
-  XSETFASTINT (w->last_modified, 0);
-  XSETFASTINT (w->last_overlay_modified, 0);
+  w->last_modified = make_number (0);
+  w->last_overlay_modified = make_number (0);
   windows_or_buffers_changed++;
-  FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
+  FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
 
   if (width_p)
     {
       sideward = &w->vchild;
       forward = &w->hchild;
-      XSETFASTINT (w->width, size);
+      w->width = make_number (size);
     }
   else
     {
       sideward = &w->hchild;
       forward = &w->vchild;
-      XSETFASTINT (w->height, size);
+      w->height = make_number (size);
     }
 
   if (!NILP (*sideward))
@@ -2316,18 +2459,22 @@ size_window (window, size, width_p, nodelete_p)
     {
       int fixed_size, each, extra, n;
       int resize_fixed_p, nfixed;
-      int last_pos, first_pos, nchildren;
+      int last_pos, first_pos, nchildren, total;
 
       /* Determine the fixed-size portion of the this window, and the
         number of child windows.  */
-      fixed_size = nchildren = nfixed = 0;
+      fixed_size = nchildren = nfixed = total = 0;
       for (child = *forward; !NILP (child); child = c->next, ++nchildren)
        {
+         int child_size;
+         
          c = XWINDOW (child);
+         child_size = width_p ? XINT (c->width) : XINT (c->height);
+         total += child_size;
+         
          if (window_fixed_size_p (c, width_p, 0))
            {
-             fixed_size += (width_p
-                            ? XFASTINT (c->width) : XFASTINT (c->height));
+             fixed_size += child_size;
              ++nfixed;
            }
        }
@@ -2340,11 +2487,11 @@ size_window (window, size, width_p, nodelete_p)
       /* Compute how many lines/columns to add to each child.  The
         value of extra takes care of rounding errors.  */
       n = resize_fixed_p ? nchildren : nchildren - nfixed;
-      each = (size - old_size) / n;
-      extra = (size - old_size) - n * each;
+      each = (size - total) / n;
+      extra = (size - total) - n * each;
 
       /* Compute new children heights and edge positions.  */
-      first_pos = width_p ? XFASTINT (w->left) : XFASTINT (w->top);
+      first_pos = width_p ? XINT (w->left) : XINT (w->top);
       last_pos = first_pos;
       for (child = *forward; !NILP (child); child = c->next)
        {
@@ -2387,7 +2534,7 @@ size_window (window, size, width_p, nodelete_p)
          {
            int child_size;
            c = XWINDOW (child);
-           child_size = width_p ? XFASTINT (c->width) : XFASTINT (c->height);
+           child_size = width_p ? XINT (c->width) : XINT (c->height);
            size_window (child, child_size, width_p, 0);
          }
     }
@@ -2462,6 +2609,7 @@ set_window_buffer (window, buffer, run_hooks_p)
   bzero (&w->last_cursor, sizeof w->last_cursor);
   w->window_end_valid = Qnil;
   XSETFASTINT (w->hscroll, 0);
+  XSETFASTINT (w->min_hscroll, 0);
   set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
   set_marker_restricted (w->start,
                         make_number (b->last_window_start),
@@ -2724,6 +2872,8 @@ 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.\n\
+If `display-reuse-frames' is non-nil, and another frame is currently\n\
+displaying BUFFER, then simply raise that frame.\n\
 \n\
 The variables `special-display-buffer-names', `special-display-regexps',\n\
 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
@@ -2735,8 +2885,12 @@ If FRAME is t, search all frames.\n\
 If FRAME is a frame, search only that frame.\n\
 If FRAME is nil, search only the selected frame\n\
  (actually the last nonminibuffer frame),\n\
- unless `pop-up-frames' is non-nil,\n\
- which means search visible and iconified frames.")
+ unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,\n\
+ which means search visible and iconified frames.\n\
+\n\
+If `even-window-heights' is non-nil, window heights will be evened out\n\
+if displaying the buffer causes two vertically adjacent windows to be\n\
+displayed.")
   (buffer, not_this_window, frame)
      register Lisp_Object buffer, not_this_window, frame;
 {
@@ -2766,21 +2920,22 @@ If FRAME is nil, search only the selected frame\n\
        }
     }
 
-  /* If pop_up_frames,
+  /* If the user wants pop-up-frames or display-reuse-frames, then
      look for a window showing BUFFER on any visible or iconified frame.
      Otherwise search only the current frame.  */
   if (! NILP (frame))
     tem = frame;
-  else if (pop_up_frames || last_nonminibuf_frame == 0)
+  else if (pop_up_frames
+          || display_buffer_reuse_frames
+          || last_nonminibuf_frame == 0)
     XSETFASTINT (tem, 0);
   else
     XSETFRAME (tem, last_nonminibuf_frame);
+  
   window = Fget_buffer_window (buffer, tem);
   if (!NILP (window)
       && (NILP (not_this_window) || !EQ (window, selected_window)))
-    {
-      return display_buffer_1 (window);
-    }
+    return display_buffer_1 (window);
 
   /* Certain buffer names get special handling.  */
   if (!NILP (Vspecial_display_function) && NILP (swp))
@@ -2884,6 +3039,7 @@ If FRAME is nil, search only the selected frame\n\
          if (!NILP (XWINDOW (window)->next))
            other = lower = XWINDOW (window)->next, upper = window;
          if (!NILP (other)
+             && !NILP (Veven_window_heights)
              /* Check that OTHER and WINDOW are vertically arrayed.  */
              && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
              && (XFASTINT (XWINDOW (other)->height)
@@ -2933,6 +3089,7 @@ temp_output_buffer_show (buf)
       Vminibuf_scroll_window = window;
       w = XWINDOW (window);
       XSETFASTINT (w->hscroll, 0);
+      XSETFASTINT (w->min_hscroll, 0);
       set_marker_restricted_both (w->start, buf, 1, 1);
       set_marker_restricted_both (w->pointm, buf, 1, 1);
 
@@ -3469,6 +3626,8 @@ shrink_window_lowest_first (w, height)
       Lisp_Object last_child;
       int delta = old_height - height;
       int last_top;
+
+      last_child = Qnil;
       
       /* Find the last child.  We are taking space from lowest windows
         first, so we iterate over children from the last child
@@ -3763,13 +3922,14 @@ window_scroll_pixel_based (window, n, whole, noerror)
   Lisp_Object tem;
   int this_scroll_margin;
   int preserve_y;
+  /* True if we fiddled the window vscroll field without really scrolling.   */
+  int vscrolled = 0;
 
   SET_TEXT_POS_FROM_MARKER (start, w->start);
   
   /* If PT is not visible in WINDOW, move back one half of
      the screen.  */
-  XSETFASTINT (tem, PT);
-  tem = Fpos_visible_in_window_p (tem, window);
+  tem = Fpos_visible_in_window_p (make_number (PT), window, Qnil);
   if (NILP (tem))
     {
       /* Move backward half the height of the window.  Performance note:
@@ -3813,7 +3973,16 @@ window_scroll_pixel_based (window, n, whole, noerror)
       int screen_full = (it.last_visible_y
                         - next_screen_context_lines * CANON_Y_UNIT (it.f));
       int direction = n < 0 ? -1 : 1;
-      move_it_vertically (&it, direction * screen_full);
+      int dy = direction * screen_full;
+
+      /* Note that move_it_vertically always moves the iterator to the
+         start of a line.  So, if the last line doesn't have a newline,
+        we would end up at the start of the line ending at ZV.  */
+      if (dy <= 0)
+       move_it_vertically_backward (&it, -dy);
+      else if (dy > 0)
+       move_it_to (&it, ZV, -1, it.current_y + dy, -1,
+                   MOVE_TO_POS | MOVE_TO_Y);
     }
   else
     move_it_by_lines (&it, n, 1);
@@ -3822,23 +3991,55 @@ window_scroll_pixel_based (window, n, whole, noerror)
   if ((n > 0 && IT_CHARPOS (it) == ZV)
       || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
     {
-      if (noerror)
-       return;
-      else if (IT_CHARPOS (it) == ZV)
-       Fsignal (Qend_of_buffer, Qnil);
+      if (IT_CHARPOS (it) == ZV)
+       {
+         if (it.current_y + it.max_ascent + it.max_descent
+             > it.last_visible_y)
+           {
+             /* The last line was only partially visible, make it fully
+                visible.  */
+             w->vscroll = (it.last_visible_y
+                           - it.current_y + it.max_ascent + it.max_descent);
+             adjust_glyphs (it.f);
+           }
+         else if (noerror)
+           return;
+         else
+           Fsignal (Qend_of_buffer, Qnil);
+       }
       else
-       Fsignal (Qbeginning_of_buffer, Qnil);
+       {
+         if (w->vscroll != 0)
+           /* The first line was only partially visible, make it fully
+              visible. */
+           w->vscroll = 0;
+         else if (noerror)
+           return;
+         else
+           Fsignal (Qbeginning_of_buffer, Qnil);
+       }
+
+      /* If control gets here, then we vscrolled.  */
+
+      XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
+
+      /* Don't try to change the window start below.  */
+      vscrolled = 1;
     }
 
-  /* Set the window start, and set up the window for redisplay.  */
-  set_marker_restricted (w->start, make_number (IT_CHARPOS (it)), w->buffer);
-  w->start_at_line_beg = Fbolp ();
-  w->update_mode_line = Qt;
-  XSETFASTINT (w->last_modified, 0);
-  XSETFASTINT (w->last_overlay_modified, 0);
-  /* Set force_start so that redisplay_window will run the
-     window-scroll-functions.  */
-  w->force_start = Qt;
+  if (! vscrolled)
+    {
+      /* Set the window start, and set up the window for redisplay.  */
+      set_marker_restricted (w->start, make_number (IT_CHARPOS (it)),
+                            w->buffer);
+      w->start_at_line_beg = Fbolp ();
+      w->update_mode_line = Qt;
+      XSETFASTINT (w->last_modified, 0);
+      XSETFASTINT (w->last_overlay_modified, 0);
+      /* Set force_start so that redisplay_window will run the
+        window-scroll-functions.  */
+      w->force_start = Qt;
+    }
   
   it.current_y = it.vpos = 0;
   
@@ -3866,18 +4067,30 @@ window_scroll_pixel_based (window, n, whole, noerror)
        }
       else if (n < 0)
        {
+         int charpos, bytepos;
+
          /* We moved the window start towards BEGV, so PT may be now
             in the scroll margin at the bottom.  */
          move_it_to (&it, PT, -1,
                      it.last_visible_y - this_scroll_margin - 1, -1,
                      MOVE_TO_POS | MOVE_TO_Y);
+
+         /* Save our position, in case it's correct.  */
+         charpos = IT_CHARPOS (it);
+         bytepos = IT_BYTEPOS (it);
       
-         /* Don't put point on a partially visible line at the end.  */
-         if (it.current_y + it.max_ascent + it.max_descent
-             > it.last_visible_y)
-           move_it_by_lines (&it, -1, 0);
-      
-         SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
+         /* See if point is on a partially visible line at the end.  */
+         move_it_by_lines (&it, 1, 1);
+         if (it.current_y > it.last_visible_y)
+           /* The last line was only partially visible, so back up two
+              lines to make sure we're on a fully visible line.  */
+           {
+             move_it_by_lines (&it, -2, 0);
+             SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
+           }
+         else
+           /* No, the position we saved is OK, so use it.  */
+           SET_PT_BOTH (charpos, bytepos);
        }
     }
 }
@@ -3913,7 +4126,7 @@ window_scroll_line_based (window, n, whole, noerror)
   original_vpos = posit.vpos;
 
   XSETFASTINT (tem, PT);
-  tem = Fpos_visible_in_window_p (tem, window);
+  tem = Fpos_visible_in_window_p (tem, window, Qnil);
 
   if (NILP (tem))
     {
@@ -4194,16 +4407,22 @@ Default for ARG is window width minus 2.")
   (arg)
      register Lisp_Object arg;
 {
-
+  Lisp_Object result;
+  int hscroll;
+  struct window *w = XWINDOW (selected_window);
+  
   if (NILP (arg))
-    XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
+    XSETFASTINT (arg, window_internal_width (w) - 2);
   else
     arg = Fprefix_numeric_value (arg);
 
-  return
-    Fset_window_hscroll (selected_window,
-                        make_number (XINT (XWINDOW (selected_window)->hscroll)
-                                     + XINT (arg)));
+  hscroll = XINT (w->hscroll) + XINT (arg);
+  result = Fset_window_hscroll (selected_window, make_number (hscroll));
+
+  if (!NILP (Finteractive_p ()))
+    w->min_hscroll = w->hscroll;
+
+  return result;
 }
 
 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
@@ -4212,35 +4431,92 @@ Default for ARG is window width minus 2.")
   (arg)
      register Lisp_Object arg;
 {
+  Lisp_Object result;
+  int hscroll;
+  struct window *w = XWINDOW (selected_window);
+  
   if (NILP (arg))
-    XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
+    XSETFASTINT (arg, window_internal_width (w) - 2);
   else
     arg = Fprefix_numeric_value (arg);
 
-  return
-    Fset_window_hscroll (selected_window,
-                        make_number (XINT (XWINDOW (selected_window)->hscroll)
-                                     - XINT (arg)));
+  hscroll = XINT (w->hscroll) - XINT (arg);
+  result = Fset_window_hscroll (selected_window, make_number (hscroll));
+  
+  if (!NILP (Finteractive_p ()))
+    w->min_hscroll = w->hscroll;
+
+  return result;
 }
 
+/* Value is the number of lines actually displayed in window W,
+   as opposed to its height.  */
+
+static int
+displayed_window_lines (w)
+     struct window *w;
+{
+  struct it it;
+  struct text_pos start;
+  int height = window_box_height (w);
+  struct buffer *old_buffer;
+  int bottom_y;
+
+  if (XBUFFER (w->buffer) != current_buffer)
+    {
+      old_buffer = current_buffer;
+      set_buffer_internal (XBUFFER (w->buffer));
+    }
+  else
+    old_buffer = NULL;
+
+  SET_TEXT_POS_FROM_MARKER (start, w->start);
+  start_display (&it, w, start);
+  move_it_vertically (&it, height);
+
+  if (old_buffer)
+    set_buffer_internal (old_buffer);
+
+  bottom_y = it.current_y + it.max_ascent + it.max_descent;
+
+  if (bottom_y > it.current_y && bottom_y <= it.last_visible_y)
+    /* Hit a line without a terminating newline.  */
+    it.vpos++;
+
+  /* Add in empty lines at the bottom of the window.  */
+  if (bottom_y < height)
+    {
+      struct frame *f = XFRAME (w->frame);
+      int rest = height - bottom_y;
+      int lines = rest / CANON_Y_UNIT (f);
+      it.vpos += lines;
+    }
+
+  return it.vpos;
+}
+
+
 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
-  "Center point in window and redisplay frame.  With ARG, put point on line ARG.\n\
-The desired position of point is always relative to the current window.\n\
-Just C-u as prefix means put point in the center of the window.\n\
-If ARG is omitted or nil, erases the entire frame and then\n\
-redraws with point in the center of the current window.")
+  "Center point in window and redisplay frame.\n\
+With prefix argument ARG, recenter putting point on screen line ARG\n\
+relative to the current window.  If ARG is negative, it counts up from the\n\
+bottom of the window.  (ARG should be less than the height of the window.)\n\
+\n\
+If ARG is omitted or nil, erase the entire frame and then\n\
+redraw with point in the center of the current window.\n\
+Just C-u as prefix means put point in the center of the window\n\
+and redisplay normally--don't erase and redraw the frame.")
   (arg)
      register Lisp_Object arg;
 {
-  register struct window *w = XWINDOW (selected_window);
-  register int ht = window_internal_height (w);
-  struct position pos;
+  struct window *w = XWINDOW (selected_window);
   struct buffer *buf = XBUFFER (w->buffer);
   struct buffer *obuf = current_buffer;
+  int center_p = 0;
+  int charpos, bytepos;
 
   if (NILP (arg))
     {
-      extern int frame_garbaged;
       int i;
 
       /* Invalidate pixel data calculated for all compositions.  */
@@ -4249,60 +4525,78 @@ redraws with point in the center of the current window.")
 
       Fredraw_frame (w->frame);
       SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
-      XSETFASTINT (arg, ht / 2);
+      center_p = 1;
     }
   else if (CONSP (arg)) /* Just C-u. */
-    {
-      XSETFASTINT (arg, ht / 2);
-    }
+    center_p = 1;
   else
     {
       arg = Fprefix_numeric_value (arg);
       CHECK_NUMBER (arg, 0);
     }
 
-  if (XINT (arg) < 0)
-    XSETINT (arg, XINT (arg) + ht);
-
   set_buffer_internal (buf);
-  pos = *vmotion (PT, - XINT (arg), w);
 
-  set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
-  w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
-                          || FETCH_BYTE (pos.bytepos - 1) == '\n')
-                         ? Qt : Qnil);
+  /* Handle centering on a gfaphical frame specially.  Such frames can
+     have variable-height lines and centering point on the basis of
+     line counts would lead to strange effects.  */
+  if (center_p && FRAME_WINDOW_P (XFRAME (w->frame)))
+    {
+      struct it it;
+      struct text_pos pt;
+      
+      SET_TEXT_POS (pt, PT, PT_BYTE);
+      start_display (&it, w, pt);
+      move_it_vertically (&it, - it.last_visible_y / 2);
+      charpos = IT_CHARPOS (it);
+      bytepos = IT_BYTEPOS (it);
+    }
+  else
+    {
+      struct position pos;
+      
+      if (center_p)
+       {
+         int ht = displayed_window_lines (w);
+         arg = make_number (ht / 2);
+       }
+      else if (XINT (arg) < 0)
+       {
+         int ht = displayed_window_lines (w);
+         XSETINT (arg, XINT (arg) + ht);
+       }
+      
+      pos = *vmotion (PT, - XINT (arg), w);
+      charpos = pos.bufpos;
+      bytepos = pos.bytepos;
+    }
+
+  /* Set the new window start.  */
+  set_marker_both (w->start, w->buffer, charpos, bytepos);
+  w->window_end_valid = Qnil;
   w->force_start = Qt;
+  if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
+    w->start_at_line_beg = Qt;
+  else
+    w->start_at_line_beg = Qnil;
+  
   set_buffer_internal (obuf);
-
   return Qnil;
 }
 
 
-/* Value is the number of lines actually displayed in window W,
-   as opposed to its height.  */
-
-static int
-displayed_window_lines (w)
-     struct window *w;
+DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
+  0, 1, 0,
+  "Return the height in lines of the text display area of WINDOW.\n\
+This doesn't include the mode-line (or header-line if any) or any\n\
+partial-height lines in the text display area.")
+  (window)
+     Lisp_Object window;
 {
-  struct it it;
-  struct text_pos start;
-  int height = window_box_height (w);
-
-  SET_TEXT_POS_FROM_MARKER (start, w->start);
-  start_display (&it, w, start);
-  move_it_vertically (&it, height);
-
-  /* Add in empty lines at the bottom of the window.  */
-  if (it.current_y < height)
-    {
-      struct frame *f = XFRAME (w->frame);
-      int rest = height - it.current_y;
-      int lines = (rest + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
-      it.vpos += lines;
-    }
-  
-  return it.vpos;
+  struct window *w = decode_window (window);
+  int pixel_height = window_box_height (w);
+  int line_height = pixel_height / CANON_Y_UNIT (XFRAME (w->frame));
+  return make_number (line_height);
 }
 
 
@@ -4343,6 +4637,10 @@ zero means top of window, negative means relative to bottom of window.")
        XSETINT (arg, XINT (arg) + lines);
     }
 
+  if (w->vscroll)
+    /* Skip past a partially visible first line.  */
+    XSETINT (arg, XINT (arg) + 1);
+
   return Fvertical_motion (arg, window);
 }
 
@@ -4374,19 +4672,21 @@ struct save_window_data
 
 /* This is saved as a Lisp_Vector  */
 struct saved_window
-  {
-    /* these first two must agree with struct Lisp_Vector in lisp.h */
-    EMACS_INT size_from_Lisp_Vector_struct;
-    struct Lisp_Vector *next_from_Lisp_Vector_struct;
+{
+  /* these first two must agree with struct Lisp_Vector in lisp.h */
+  EMACS_INT size_from_Lisp_Vector_struct;
+  struct Lisp_Vector *next_from_Lisp_Vector_struct;
 
-    Lisp_Object window;
-    Lisp_Object buffer, start, pointm, mark;
-    Lisp_Object left, top, width, height, hscroll;
-    Lisp_Object parent, prev;
-    Lisp_Object start_at_line_beg;
-    Lisp_Object display_table;
-  };
-#define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
+  Lisp_Object window;
+  Lisp_Object buffer, start, pointm, mark;
+  Lisp_Object left, top, width, height, hscroll, min_hscroll;
+  Lisp_Object parent, prev;
+  Lisp_Object start_at_line_beg;
+  Lisp_Object display_table;
+  Lisp_Object orig_top, orig_height;
+};
+
+#define SAVED_WINDOW_VECTOR_SIZE 17 /* Arg to Fmake_vector */
 
 #define SAVED_WINDOW_N(swv,n) \
   ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
@@ -4448,12 +4748,11 @@ the return value is nil.  Otherwise the value is t.")
     {
       if (XBUFFER (new_current_buffer) == current_buffer)
        old_point = PT;
-
     }
 
   frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
   f = XFRAME (frame);
-
+  
   /* 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))
@@ -4463,7 +4762,7 @@ the return value is nil.  Otherwise the value is t.")
       struct window *root_window;
       struct window **leaf_windows;
       int n_leaf_windows;
-      int k, i;
+      int k, i, n;
 
       /* If the frame has been resized since this window configuration was
         made, we change the frame to the size specified in the
@@ -4578,7 +4877,10 @@ the return value is nil.  Otherwise the value is t.")
          w->width = p->width;
          w->height = p->height;
          w->hscroll = p->hscroll;
+         w->min_hscroll = p->min_hscroll;
          w->display_table = p->display_table;
+         w->orig_top = p->orig_top;
+         w->orig_height = p->orig_height;
          XSETFASTINT (w->last_modified, 0);
          XSETFASTINT (w->last_overlay_modified, 0);
 
@@ -4670,15 +4972,23 @@ the return value is nil.  Otherwise the value is t.")
 #endif
 
       /* Now, free glyph matrices in windows that were not reused.  */
-      for (i = 0; i < n_leaf_windows; ++i)
-       if (NILP (leaf_windows[i]->buffer))
-         {
-           /* Assert it's not reused as a combination.  */
-           xassert (NILP (leaf_windows[i]->hchild) 
-                    && NILP (leaf_windows[i]->vchild));
-           free_window_matrices (leaf_windows[i]);
-           SET_FRAME_GARBAGED (f);
-         }
+      for (i = n = 0; i < n_leaf_windows; ++i)
+       {
+         if (NILP (leaf_windows[i]->buffer))
+           {
+             /* Assert it's not reused as a combination.  */
+             xassert (NILP (leaf_windows[i]->hchild) 
+                      && NILP (leaf_windows[i]->vchild));
+             free_window_matrices (leaf_windows[i]);
+           }
+         else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
+           ++n;
+       }
+
+      /* If more than one window shows the new and old current buffer,
+        don't try to preserve point in that buffer.  */
+      if (old_point > 0 && n > 1)
+       old_point = -1;
       
       adjust_glyphs (f);
 
@@ -4834,7 +5144,10 @@ save_window_save (window, vector, i)
       p->width = w->width;
       p->height = w->height;
       p->hscroll = w->hscroll;
+      p->min_hscroll = w->min_hscroll;
       p->display_table = w->display_table;
+      p->orig_top = w->orig_top;
+      p->orig_height = w->orig_height;
       if (!NILP (w->buffer))
        {
          /* Save w's value of point in the window configuration.
@@ -4932,8 +5245,7 @@ redirection (see `redirect-frame-focus').")
   for (i = 0; i < n_windows; i++)
     XVECTOR (tem)->contents[i]
       = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
-  save_window_save (FRAME_ROOT_WINDOW (f),
-                   XVECTOR (tem), 0);
+  save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
   XSETWINDOW_CONFIGURATION (tem, data);
   return (tem);
 }
@@ -5163,7 +5475,7 @@ freeze_window_starts (f, freeze_p)
      struct frame *f;
      int freeze_p;
 {
-  foreach_window (f, freeze_window_start, (void *) freeze_p);
+  foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
 }
 
 \f
@@ -5261,6 +5573,8 @@ compare_window_configurations (c1, c2, ignore_positions)
        {
          if (! EQ (p1->hscroll, p2->hscroll))
            return 0;
+         if (!EQ (p1->min_hscroll, p2->min_hscroll))
+           return 0;
          if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
            return 0;
          if (NILP (Fequal (p1->start, p2->start)))
@@ -5352,6 +5666,11 @@ Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
 work using this function.");
   Vdisplay_buffer_function = Qnil;
 
+  DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
+    "*If non-nil, `display-buffer' should even the window heights.\n\
+If nil, `display-buffer' will leave the window configuration alone.");
+  Veven_window_heights = Qt;
+
   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;
@@ -5364,6 +5683,11 @@ work using this function.");
     "*Non-nil means `display-buffer' should make a separate frame.");
   pop_up_frames = 0;
 
+  DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
+    "*Non-nil means `display-buffer' should reuse frames.\n\
+If the buffer in question is already displayed in a frame, raise that frame.");
+  display_buffer_reuse_frames = 0;
+
   DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
     "Function to call to handle automatic new frame creation.\n\
 It is called with no arguments and should return a newly created frame.\n\
@@ -5532,6 +5856,7 @@ The selected frame is the one whose configuration has changed.");
   defsubr (&Sother_window_for_scrolling);
   defsubr (&Sscroll_other_window);
   defsubr (&Srecenter);
+  defsubr (&Swindow_text_height);
   defsubr (&Smove_to_window_line);
   defsubr (&Swindow_configuration_p);
   defsubr (&Swindow_configuration_frame);