home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / window.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  131KB  |  4,095 lines

  1. /* Window creation, deletion and examination for GNU Emacs.
  2.    Does not include redisplay.
  3.    Copyright (C) 1985,86,87,93,94,95,96,97,1998 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. /* Modified for emx by Jeremy Bowen, May 1999 */
  23.  
  24. #include <config.h>
  25. #include "lisp.h"
  26. #include "buffer.h"
  27. #include "frame.h"
  28. #include "window.h"
  29. #include "commands.h"
  30. #include "indent.h"
  31. #include "termchar.h"
  32. #include "disptab.h"
  33. #include "keyboard.h"
  34. #include "blockinput.h"
  35. #include "dispextern.h"
  36.  
  37. #ifdef HAVE_X_WINDOWS
  38. #ifndef HAVE_PM
  39. #include "xterm.h"
  40. #else /* HAVE_PM */
  41. #include "pmterm.h"
  42. #endif /* HAVE_PM */
  43. #endif    /* HAVE_X_WINDOWS */
  44.  
  45. Lisp_Object Qwindowp, Qwindow_live_p;
  46.  
  47. static struct window *decode_window P_ ((Lisp_Object));
  48.  
  49. static Lisp_Object select_window_1 P_ ((Lisp_Object, int));
  50.  
  51. /* This is the window in which the terminal's cursor should
  52.    be left when nothing is being done with it.  This must
  53.    always be a leaf window, and its buffer is selected by
  54.    the top level editing loop at the end of each command.
  55.  
  56.    This value is always the same as
  57.    FRAME_SELECTED_WINDOW (selected_frame).  */
  58.  
  59. Lisp_Object selected_window;
  60.  
  61. /* The minibuffer window of the selected frame.
  62.    Note that you cannot test for minibufferness of an arbitrary window
  63.    by comparing against this; but you can test for minibufferness of
  64.    the selected window.  */
  65. Lisp_Object minibuf_window;
  66.  
  67. /* Non-nil means it is the window for C-M-v to scroll
  68.    when the minibuffer is selected.  */
  69. Lisp_Object Vminibuf_scroll_window;
  70.  
  71. /* Non-nil means this is the buffer whose window C-M-v should scroll.  */
  72. Lisp_Object Vother_window_scroll_buffer;
  73.  
  74. /* Non-nil means it's function to call to display temp buffers.  */
  75. Lisp_Object Vtemp_buffer_show_function;
  76.  
  77. /* If a window gets smaller than either of these, it is removed. */
  78. int window_min_height;
  79. int window_min_width;
  80.  
  81. /* Nonzero implies Fdisplay_buffer should create windows. */
  82. int pop_up_windows;
  83.  
  84. /* Nonzero implies make new frames for Fdisplay_buffer.  */
  85. int pop_up_frames;
  86.  
  87. /* Non-nil means use this function instead of default */
  88. Lisp_Object Vpop_up_frame_function;
  89.  
  90. /* Function to call to handle Fdisplay_buffer.  */
  91. Lisp_Object Vdisplay_buffer_function;
  92.  
  93. /* List of buffer *names* for buffers that should have their own frames.  */
  94. Lisp_Object Vspecial_display_buffer_names;
  95.  
  96. /* List of regexps for buffer names that should have their own frames.  */
  97. Lisp_Object Vspecial_display_regexps;
  98.  
  99. /* Function to pop up a special frame.  */
  100. Lisp_Object Vspecial_display_function;
  101.  
  102. /* List of buffer *names* for buffers to appear in selected window.  */
  103. Lisp_Object Vsame_window_buffer_names;
  104.  
  105. /* List of regexps for buffer names to appear in selected window.  */
  106. Lisp_Object Vsame_window_regexps;
  107.  
  108. /* Hook run at end of temp_output_buffer_show.  */
  109. Lisp_Object Qtemp_buffer_show_hook;
  110.  
  111. /* Fdisplay_buffer always splits the largest window
  112.    if that window is more than this high.  */
  113. int split_height_threshold;
  114.  
  115. /* Number of lines of continuity in scrolling by screenfuls.  */
  116. int next_screen_context_lines;
  117.  
  118. /* Incremented for each window created.  */
  119. static int sequence_number;
  120.  
  121. /* Nonzero after init_window_once has finished.  */
  122. static int window_initialized;
  123.  
  124. /* Hook to run when window config changes.  */
  125. Lisp_Object Qwindow_configuration_change_hook;
  126. Lisp_Object Vwindow_configuration_change_hook;
  127.  
  128. /* Nonzero means scroll commands try to put point
  129.    at the same screen height as previously.  */
  130. static int scroll_preserve_screen_position;
  131.  
  132. /* Nonzero means we can split a frame even if it is "unsplittable".  */
  133. static int inhibit_frame_unsplittable;
  134.  
  135. #define min(a, b) ((a) < (b) ? (a) : (b))
  136.  
  137. extern int scroll_margin;
  138.  
  139. extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
  140.  
  141. DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
  142.   "Returns t if OBJECT is a window.")
  143.   (object)
  144.      Lisp_Object object;
  145. {
  146.   return WINDOWP (object) ? Qt : Qnil;
  147. }
  148.  
  149. DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
  150.   "Returns t if OBJECT is a window which is currently visible.")
  151.      (object)
  152.      Lisp_Object object;
  153. {
  154.   return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
  155. }
  156.  
  157. Lisp_Object
  158. make_window ()
  159. {
  160.   Lisp_Object val;
  161.   register struct window *p;
  162.   register struct Lisp_Vector *vec;
  163.   int i;
  164.  
  165.   vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
  166.   for (i = 0; i < VECSIZE (struct window); i++)
  167.     vec->contents[i] = Qnil;
  168.   vec->size = VECSIZE (struct window);
  169.   p = (struct window *)vec;
  170.   XSETFASTINT (p->sequence_number, ++sequence_number);
  171.   XSETFASTINT (p->left, 0);
  172.   XSETFASTINT (p->top, 0);
  173.   XSETFASTINT (p->height, 0);
  174.   XSETFASTINT (p->width, 0);
  175.   XSETFASTINT (p->hscroll, 0);
  176.   XSETFASTINT (p->last_point, 0);
  177.   XSETFASTINT (p->last_point_x, 0);
  178.   XSETFASTINT (p->last_point_y, 0);
  179.   p->start = Fmake_marker ();
  180.   p->pointm = Fmake_marker ();
  181.   XSETFASTINT (p->use_time, 0);
  182.   p->frame = Qnil;
  183.   p->display_table = Qnil;
  184.   p->dedicated = Qnil;
  185.   XSETWINDOW (val, p);
  186.   return val;
  187. }
  188.  
  189. DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
  190.   "Return the window that the cursor now appears in and commands apply to.")
  191.   ()
  192. {
  193.   return selected_window;
  194. }
  195.  
  196. DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
  197.   "Return the window used now for minibuffers.\n\
  198. If the optional argument FRAME is specified, return the minibuffer window\n\
  199. used by that frame.")
  200.   (frame)
  201.     Lisp_Object frame;
  202. {
  203.   if (NILP (frame))
  204.     XSETFRAME (frame, selected_frame);
  205.   else
  206.     CHECK_LIVE_FRAME (frame, 0);
  207.  
  208.   return FRAME_MINIBUF_WINDOW (XFRAME (frame));
  209. }
  210.  
  211. DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
  212.   "Returns non-nil if WINDOW is a minibuffer window.")
  213.   (window)
  214.      Lisp_Object window;
  215. {
  216.   struct window *w = decode_window (window);
  217.   return (MINI_WINDOW_P (w) ? Qt : Qnil);
  218. }
  219.  
  220. DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
  221.   Spos_visible_in_window_p, 0, 2, 0,
  222.   "Return t if position POS is currently on the frame in WINDOW.\n\
  223. Returns nil if that position is scrolled vertically out of view.\n\
  224. POS defaults to point; WINDOW, to the selected window.")
  225.   (pos, window)
  226.      Lisp_Object pos, window;
  227. {
  228.   register struct window *w;
  229.   register int top;
  230.   register int height;
  231.   register int posint;
  232.   register struct buffer *buf;
  233.   struct position posval;
  234.   int hscroll;
  235.  
  236.   if (NILP (pos))
  237.     posint = PT;
  238.   else
  239.     {
  240.       CHECK_NUMBER_COERCE_MARKER (pos, 0);
  241.       posint = XINT (pos);
  242.     }
  243.  
  244.   w = decode_window (window);
  245.   top = marker_position (w->start);
  246.   hscroll = XINT (w->hscroll);
  247.  
  248.   if (posint < top)
  249.     return Qnil;
  250.  
  251.   height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
  252.  
  253.   buf = XBUFFER (w->buffer);
  254.   if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
  255.       && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
  256.     {
  257.       /* If frame is up to date,
  258.      use the info recorded about how much text fit on it. */
  259.       if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
  260.       || (XFASTINT (w->window_end_vpos) < height))
  261.     return Qt;
  262.       return Qnil;
  263.     }
  264.   else
  265.     {
  266.       if (posint > BUF_ZV (buf))
  267.     return Qnil;
  268.  
  269.       /* w->start can be out of range.  If it is, do something reasonable.  */
  270.       if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
  271.     return Qnil;
  272.  
  273.       /* If that info is not correct, calculate afresh */
  274.       /* BUG FIX for the 7th arg (TOHPOS).
  275.  
  276.      '0' is harmless, however, ' - (1 << (BITS_PER_SHORT - 1))' is
  277.      more appropriate here.  In case of HSCROLL > 0, this can avoid
  278.      needless calculation done until (HPOS == 0).
  279.  
  280.      We want to determine if the position POSINT is in HEIGHT or
  281.      not.  We don't have to do calculation until (HPOS == 0).  We
  282.      can stop it when VPOS goes beyond HEIGHT.  */
  283.       posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0), 0,
  284.                 posint, height, - (1 << (BITS_PER_SHORT - 1)),
  285.                 window_internal_width (w) - 1,
  286.                 hscroll, 0, w);
  287.  
  288.       return posval.vpos < height ? Qt : Qnil;
  289.     }
  290. }
  291.  
  292. static struct window *
  293. decode_window (window)
  294.      register Lisp_Object window;
  295. {
  296.   if (NILP (window))
  297.     return XWINDOW (selected_window);
  298.  
  299.   CHECK_LIVE_WINDOW (window, 0);
  300.   return XWINDOW (window);
  301. }
  302.  
  303. DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
  304.   "Return the buffer that WINDOW is displaying.")
  305.   (window)
  306.      Lisp_Object window;
  307. {
  308.   return decode_window (window)->buffer;
  309. }
  310.  
  311. DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
  312.   "Return the number of lines in WINDOW (including its mode line).")
  313.   (window)
  314.      Lisp_Object window;
  315. {
  316.   return decode_window (window)->height;
  317. }
  318.  
  319. DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
  320.   "Return the number of display columns in WINDOW.\n\
  321. This is the width that is usable columns available for text in WINDOW.\n\
  322. If you want to find out how many columns WINDOW takes up,\n\
  323. use  (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
  324.   (window)
  325.      Lisp_Object window;
  326. {
  327.   return make_number (window_internal_width (decode_window (window)));
  328. }
  329.  
  330. DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
  331.   "Return the number of columns by which WINDOW is scrolled from left margin.")
  332.   (window)
  333.      Lisp_Object window;
  334. {
  335.   return decode_window (window)->hscroll;
  336. }
  337.  
  338. DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
  339.   "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
  340. NCOL should be zero or positive.")
  341.   (window, ncol)
  342.      register Lisp_Object window, ncol;
  343. {
  344.   register struct window *w;
  345.  
  346.   CHECK_NUMBER (ncol, 1);
  347.   if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
  348.   w = decode_window (window);
  349.   if (XINT (w->hscroll) != XINT (ncol))
  350.     XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
  351.   w->hscroll = ncol;
  352.   return ncol;
  353. }
  354.  
  355. DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
  356.        Swindow_redisplay_end_trigger, 0, 1, 0,
  357.   "Return WINDOW's redisplay end trigger value.\n\
  358. See `set-window-redisplay-end-trigger' for more information.")
  359.   (window)
  360.      Lisp_Object window;
  361. {
  362.   return decode_window (window)->redisplay_end_trigger;
  363. }
  364.  
  365. DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
  366.        Sset_window_redisplay_end_trigger, 2, 2, 0,
  367.   "Set WINDOW's redisplay end trigger value to VALUE.\n\
  368. VALUE should be a buffer position (typically a marker) or nil.\n\
  369. If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
  370. beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
  371. with two arguments: WINDOW, and the end trigger value.\n\
  372. Afterwards the end-trigger value is reset to nil.")
  373.   (window, value)
  374.      register Lisp_Object window, value;
  375. {
  376.   register struct window *w;
  377.  
  378.   w = decode_window (window);
  379.   w->redisplay_end_trigger = value;
  380.   return value;
  381. }
  382.  
  383. DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
  384.   "Return a list of the edge coordinates of WINDOW.\n\
  385. \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
  386. RIGHT is one more than the rightmost column used by WINDOW,\n\
  387. and BOTTOM is one more than the bottommost row used by WINDOW\n\
  388.  and its mode-line.")
  389.   (window)
  390.      Lisp_Object window;
  391. {
  392.   register struct window *w = decode_window (window);
  393.  
  394.   return Fcons (w->left, Fcons (w->top,
  395.            Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
  396.           Fcons (make_number (XFASTINT (w->top)
  397.                       + XFASTINT (w->height)),
  398.              Qnil))));
  399. }
  400.  
  401. /* Test if the character at column *x, row *y is within window *w.
  402.    If it is not, return 0;
  403.    if it is in the window's text area,
  404.       set *x and *y to its location relative to the upper left corner
  405.          of the window, and
  406.       return 1;
  407.    if it is on the window's modeline, return 2;
  408.    if it is on the border between the window and its right sibling,
  409.       return 3.  */
  410. static int
  411. coordinates_in_window (w, x, y)
  412.      register struct window *w;
  413.      register int *x, *y;
  414. {
  415.   register int left = XINT (w->left);
  416.   register int right_edge = WINDOW_RIGHT_EDGE (w);
  417.   register int left_margin = WINDOW_LEFT_MARGIN (w);
  418.   register int right_margin = WINDOW_RIGHT_MARGIN (w);
  419.   register int window_height = XINT (w->height);
  420.   register int top = XFASTINT (w->top);
  421.   
  422.   if (   *x < left || *x >= right_edge
  423.       || *y < top  || *y >= top  + window_height)
  424.     return 0;
  425.  
  426.   if (left_margin != left && *x < left_margin && *x >= left)
  427.     return 3;
  428.   
  429.   if (right_margin != right_edge && *x >= right_margin && *x < right_edge)
  430.     return 3;
  431.   
  432.   /* Is the character is the mode line?  */
  433.   if (*y == top + window_height - 1
  434.       && ! MINI_WINDOW_P (w))
  435.     return 2;
  436.  
  437.   *x -= WINDOW_LEFT_MARGIN (w);
  438.   *y -= top;
  439.   return 1;
  440. }
  441.  
  442. DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
  443.   Scoordinates_in_window_p, 2, 2, 0,
  444.   "Return non-nil if COORDINATES are in WINDOW.\n\
  445. COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
  446. measured in characters from the upper-left corner of the frame.\n\
  447. (0 .  0) denotes the character in the upper left corner of the\n\
  448. frame.\n\
  449. If COORDINATES are in the text portion of WINDOW,\n\
  450.    the coordinates relative to the window are returned.\n\
  451. If they are in the mode line of WINDOW, `mode-line' is returned.\n\
  452. If they are on the border between WINDOW and its right sibling,\n\
  453.    `vertical-line' is returned.")
  454.   (coordinates, window)
  455.      register Lisp_Object coordinates, window;
  456. {
  457.   int x, y;
  458.  
  459.   CHECK_LIVE_WINDOW (window, 0);
  460.   CHECK_CONS (coordinates, 1);
  461.   x = XINT (Fcar (coordinates));
  462.   y = XINT (Fcdr (coordinates));
  463.  
  464.   switch (coordinates_in_window (XWINDOW (window), &x, &y))
  465.     {
  466.     case 0:            /* NOT in window at all. */
  467.       return Qnil;
  468.  
  469.     case 1:            /* In text part of window. */
  470.       return Fcons (make_number (x), make_number (y));
  471.  
  472.     case 2:            /* In mode line of window. */
  473.       return Qmode_line;
  474.  
  475.     case 3:            /* On right border of window.  */
  476.       return Qvertical_line;
  477.  
  478.     default:
  479.       abort ();
  480.     }
  481. }
  482.  
  483. /* Find the window containing column x, row y, and return it as a
  484.    Lisp_Object.  If x, y is on the window's modeline, set *part
  485.    to 1; if it is on the separating line between the window and its
  486.    right sibling, set it to 2; otherwise set it to 0.  If there is no
  487.    window under x, y return nil and leave *part unmodified.  */
  488. Lisp_Object
  489. window_from_coordinates (frame, x, y, part)
  490.      FRAME_PTR frame;
  491.      int x, y;
  492.      int *part;
  493. {
  494.   register Lisp_Object tem, first;
  495.  
  496.   tem = first = FRAME_SELECTED_WINDOW (frame);
  497.  
  498.   do
  499.     {
  500.       int found = coordinates_in_window (XWINDOW (tem), &x, &y);
  501.  
  502.       if (found)
  503.     {
  504.       *part = found - 1;
  505.       return tem;
  506.     }
  507.  
  508.       tem = Fnext_window (tem, Qt, Qlambda);
  509.     }
  510.   while (! EQ (tem, first));
  511.  
  512.   return Qnil;
  513. }
  514.  
  515. DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
  516.   "Return window containing coordinates X and Y on FRAME.\n\
  517. If omitted, FRAME defaults to the currently selected frame.\n\
  518. The top left corner of the frame is considered to be row 0,\n\
  519. column 0.")
  520.   (x, y, frame)
  521.       Lisp_Object x, y, frame;
  522. {
  523.   int part;
  524.  
  525.   if (NILP (frame))
  526.     XSETFRAME (frame, selected_frame);
  527.   else
  528.     CHECK_LIVE_FRAME (frame, 2);
  529.   CHECK_NUMBER (x, 0);
  530.   CHECK_NUMBER (y, 1);
  531.  
  532.   return window_from_coordinates (XFRAME (frame),
  533.                   XINT (x), XINT (y),
  534.                   &part);
  535. }
  536.  
  537. DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
  538.   "Return current value of point in WINDOW.\n\
  539. For a nonselected window, this is the value point would have\n\
  540. if that window were selected.\n\
  541. \n\
  542. Note that, when WINDOW is the selected window and its buffer\n\
  543. is also currently selected, the value returned is the same as (point).\n\
  544. It would be more strictly correct to return the `top-level' value\n\
  545. of point, outside of any save-excursion forms.\n\
  546. But that is hard to define.")
  547.   (window)
  548.      Lisp_Object window;
  549. {
  550.   register struct window *w = decode_window (window);
  551.  
  552.   if (w == XWINDOW (selected_window)
  553.       && current_buffer == XBUFFER (w->buffer))
  554.     return Fpoint ();
  555.   return Fmarker_position (w->pointm);
  556. }
  557.  
  558. DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
  559.   "Return position at which display currently starts in WINDOW.\n\
  560. This is updated by redisplay or by calling `set-window-start'.")
  561.   (window)
  562.      Lisp_Object window;
  563. {
  564.   return Fmarker_position (decode_window (window)->start);
  565. }
  566.  
  567. /* This is text temporarily removed from the doc string below.
  568.  
  569. This function returns nil if the position is not currently known.\n\
  570. That happens when redisplay is preempted and doesn't finish.\n\
  571. If in that case you want to compute where the end of the window would\n\
  572. have been if redisplay had finished, do this:\n\
  573.     (save-excursion\n\
  574.       (goto-char (window-start window))\n\
  575.       (vertical-motion (1- (window-height window)) window)\n\
  576.       (point))")  */
  577.  
  578. DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
  579.   "Return position at which display currently ends in WINDOW.\n\
  580. This is updated by redisplay, when it runs to completion.\n\
  581. Simply changing the buffer text or setting `window-start'\n\
  582. does not update this value.\n\
  583. If UP-TO-DATE is non-nil, compute the up-to-date position\n\
  584. if it isn't already recorded.")
  585.   (window, update)
  586.      Lisp_Object window, update;
  587. {
  588.   Lisp_Object value;
  589.   struct window *w = decode_window (window);
  590.   Lisp_Object buf;
  591.  
  592.   buf = w->buffer;
  593.   CHECK_BUFFER (buf, 0);
  594.  
  595. #if 0 /* This change broke some things.  We should make it later.  */
  596.   /* If we don't know the end position, return nil.
  597.      The user can compute it with vertical-motion if he wants to.
  598.      It would be nicer to do it automatically,
  599.      but that's so slow that it would probably bother people.  */
  600.   if (NILP (w->window_end_valid))
  601.     return Qnil;
  602. #endif
  603.  
  604.   if (! NILP (update)
  605.       && ! (! NILP (w->window_end_valid)
  606.         && XFASTINT (w->last_modified) >= MODIFF))
  607.     {
  608.       int opoint = PT, opoint_byte = PT_BYTE;
  609.       TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
  610.             XMARKER (w->start)->bytepos);
  611.       Fvertical_motion (make_number (window_internal_height (w)), Qnil);
  612.       XSETINT (value, PT);
  613.       TEMP_SET_PT_BOTH (opoint, opoint_byte);
  614.     }
  615.   else
  616.     XSETINT (value,
  617.          BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
  618.  
  619.   return value;
  620. }
  621.  
  622. DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
  623.   "Make point value in WINDOW be at position POS in WINDOW's buffer.")
  624.   (window, pos)
  625.      Lisp_Object window, pos;
  626. {
  627.   register struct window *w = decode_window (window);
  628.  
  629.   CHECK_NUMBER_COERCE_MARKER (pos, 1);
  630.   if (w == XWINDOW (selected_window))
  631.     Fgoto_char (pos);
  632.   else
  633.     set_marker_restricted (w->pointm, pos, w->buffer);
  634.  
  635.   return pos;
  636. }
  637.  
  638. DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
  639.   "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
  640. Optional third arg NOFORCE non-nil inhibits next redisplay\n\
  641. from overriding motion of point in order to display at this exact start.")
  642.   (window, pos, noforce)
  643.      Lisp_Object window, pos, noforce;
  644. {
  645.   register struct window *w = decode_window (window);
  646.  
  647.   CHECK_NUMBER_COERCE_MARKER (pos, 1);
  648.   set_marker_restricted (w->start, pos, w->buffer);
  649.   /* this is not right, but much easier than doing what is right. */
  650.   w->start_at_line_beg = Qnil;
  651.   if (NILP (noforce))
  652.     w->force_start = Qt;
  653.   w->update_mode_line = Qt;
  654.   XSETFASTINT (w->last_modified, 0);
  655.   XSETFASTINT (w->last_overlay_modified, 0);
  656.   if (!EQ (window, selected_window))
  657.     windows_or_buffers_changed++;
  658.   return pos;
  659. }
  660.  
  661. DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
  662.        1, 1, 0,
  663.   "Return WINDOW's dedicated object, usually t or nil.\n\
  664. See also `set-window-dedicated-p'.")
  665.   (window)
  666.      Lisp_Object window;
  667. {
  668.   return decode_window (window)->dedicated;
  669. }
  670.  
  671. DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
  672.        Sset_window_dedicated_p, 2, 2, 0,
  673.   "Control whether WINDOW is dedicated to the buffer it displays.\n\
  674. If it is dedicated, Emacs will not automatically change\n\
  675. which buffer appears in it.\n\
  676. The second argument is the new value for the dedication flag;\n\
  677. non-nil means yes.")
  678.   (window, arg)
  679.        Lisp_Object window, arg;
  680. {
  681.   register struct window *w = decode_window (window);
  682.  
  683.   if (NILP (arg))
  684.     w->dedicated = Qnil;
  685.   else
  686.     w->dedicated = Qt;
  687.  
  688.   return w->dedicated;
  689. }
  690.  
  691. DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
  692.        0, 1, 0,
  693.   "Return the display-table that WINDOW is using.")
  694.   (window)
  695.      Lisp_Object window;
  696. {
  697.   return decode_window (window)->display_table;
  698. }
  699.  
  700. /* Get the display table for use currently on window W.
  701.    This is either W's display table or W's buffer's display table.
  702.    Ignore the specified tables if they are not valid;
  703.    if no valid table is specified, return 0.  */
  704.  
  705. struct Lisp_Char_Table *
  706. window_display_table (w)
  707.      struct window *w;
  708. {
  709.   Lisp_Object tem;
  710.   tem = w->display_table;
  711.   if (DISP_TABLE_P (tem))
  712.     return XCHAR_TABLE (tem);
  713.   if (NILP (w->buffer))
  714.     return 0;
  715.  
  716.   tem = XBUFFER (w->buffer)->display_table;
  717.   if (DISP_TABLE_P (tem))
  718.     return XCHAR_TABLE (tem);
  719.   tem = Vstandard_display_table;
  720.   if (DISP_TABLE_P (tem))
  721.     return XCHAR_TABLE (tem);
  722.   return 0;
  723. }
  724.  
  725. DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
  726.   "Set WINDOW's display-table to TABLE.")
  727.   (window, table)
  728.      register Lisp_Object window, table;
  729. {
  730.   register struct window *w;
  731.   register Lisp_Object z;    /* Return value. */
  732.  
  733.   w = decode_window (window);
  734.   w->display_table = table;
  735.   return table;
  736. }
  737.  
  738. /* Record info on buffer window w is displaying
  739.    when it is about to cease to display that buffer.  */
  740. static void
  741. unshow_buffer (w)
  742.      register struct window *w;
  743. {
  744.   Lisp_Object buf;
  745.   struct buffer *b;
  746.  
  747.   buf = w->buffer;
  748.   b = XBUFFER (buf);
  749.   if (b != XMARKER (w->pointm)->buffer)
  750.     abort ();
  751.  
  752.   if (w == XWINDOW (b->last_selected_window))
  753.     b->last_selected_window = Qnil;
  754.  
  755. #if 0
  756.   if (w == XWINDOW (selected_window)
  757.       || ! EQ (buf, XWINDOW (selected_window)->buffer))
  758.     /* Do this except when the selected window's buffer
  759.        is being removed from some other window.  */
  760. #endif
  761.     /* last_window_start records the start position that this buffer
  762.        had in the last window to be disconnected from it.
  763.        Now that this statement is unconditional,
  764.        it is possible for the buffer to be displayed in the
  765.        selected window, while last_window_start reflects another
  766.        window which was recently showing the same buffer.
  767.        Some people might say that might be a good thing.  Let's see.  */
  768.     b->last_window_start = marker_position (w->start);
  769.  
  770.   /* Point in the selected window's buffer
  771.      is actually stored in that buffer, and the window's pointm isn't used.
  772.      So don't clobber point in that buffer.  */
  773.   if (! EQ (buf, XWINDOW (selected_window)->buffer))
  774.     temp_set_point_both (b,
  775.              clip_to_bounds (BUF_BEGV (b),
  776.                      XMARKER (w->pointm)->charpos,
  777.                      BUF_ZV (b)),
  778.              clip_to_bounds (BUF_BEGV_BYTE (b),
  779.                      marker_byte_position (w->pointm),
  780.                      BUF_ZV_BYTE (b)));
  781. }
  782.  
  783. /* Put replacement into the window structure in place of old. */
  784. static void
  785. replace_window (old, replacement)
  786.      Lisp_Object old, replacement;
  787. {
  788.   register Lisp_Object tem;
  789.   register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
  790.  
  791.   /* If OLD is its frame's root_window, then replacement is the new
  792.      root_window for that frame.  */
  793.  
  794.   if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
  795.     FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
  796.  
  797.   p->left = o->left;
  798.   p->top = o->top;
  799.   p->width = o->width;
  800.   p->height = o->height;
  801.  
  802.   p->next = tem = o->next;
  803.   if (!NILP (tem))
  804.     XWINDOW (tem)->prev = replacement;
  805.  
  806.   p->prev = tem = o->prev;
  807.   if (!NILP (tem))
  808.     XWINDOW (tem)->next = replacement;
  809.  
  810.   p->parent = tem = o->parent;
  811.   if (!NILP (tem))
  812.     {
  813.       if (EQ (XWINDOW (tem)->vchild, old))
  814.     XWINDOW (tem)->vchild = replacement;
  815.       if (EQ (XWINDOW (tem)->hchild, old))
  816.     XWINDOW (tem)->hchild = replacement;
  817.     }
  818.  
  819. /*** Here, if replacement is a vertical combination
  820. and so is its new parent, we should make replacement's
  821. children be children of that parent instead.  ***/
  822. }
  823.  
  824. DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
  825.   "Remove WINDOW from the display.  Default is selected window.")
  826.   (window)
  827.      register Lisp_Object window;
  828. {
  829.   delete_window (window);
  830.  
  831.   if (! NILP (Vwindow_configuration_change_hook)
  832.       && ! NILP (Vrun_hooks))
  833.     call1 (Vrun_hooks, Qwindow_configuration_change_hook);
  834.  
  835.   return Qnil;
  836. }
  837.  
  838. void
  839. delete_window (window)
  840.      register Lisp_Object window;
  841. {
  842.   register Lisp_Object tem, parent, sib;
  843.   register struct window *p;
  844.   register struct window *par;
  845.  
  846.   /* Because this function is called by other C code on non-leaf
  847.      windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
  848.      so we can't decode_window here.  */
  849.   if (NILP (window))
  850.     window = selected_window;
  851.   else
  852.     CHECK_WINDOW (window, 0);
  853.   p = XWINDOW (window);
  854.  
  855.   /* It's okay to delete an already-deleted window.  */
  856.   if (NILP (p->buffer)
  857.       && NILP (p->hchild)
  858.       && NILP (p->vchild))
  859.     return;
  860.  
  861.   parent = p->parent;
  862.   if (NILP (parent))
  863.     error ("Attempt to delete minibuffer or sole ordinary window");
  864.   par = XWINDOW (parent);
  865.  
  866.   windows_or_buffers_changed++;
  867.   FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
  868.  
  869.   /* Are we trying to delete any frame's selected window?  */
  870.   {
  871.     Lisp_Object frame, pwindow;
  872.  
  873.     /* See if the frame's selected window is either WINDOW
  874.        or any subwindow of it, by finding all that window's parents
  875.        and comparing each one with WINDOW.  */
  876.     frame = WINDOW_FRAME (XWINDOW (window));
  877.     pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
  878.  
  879.     while (!NILP (pwindow))
  880.       {
  881.     if (EQ (window, pwindow))
  882.       break;
  883.     pwindow = XWINDOW (pwindow)->parent;
  884.       }
  885.  
  886.     if (EQ (window, pwindow))
  887.       {
  888.     Lisp_Object alternative;
  889.     alternative = Fnext_window (window, Qlambda, Qnil);
  890.  
  891.     /* If we're about to delete the selected window on the
  892.        selected frame, then we should use Fselect_window to select
  893.        the new window.  On the other hand, if we're about to
  894.        delete the selected window on any other frame, we shouldn't do
  895.        anything but set the frame's selected_window slot.  */
  896.     if (EQ (window, selected_window))
  897.       Fselect_window (alternative);
  898.     else
  899.       FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
  900.       }
  901.   }
  902.  
  903.   tem = p->buffer;
  904.   /* tem is null for dummy parent windows
  905.      (which have inferiors but not any contents themselves) */
  906.   if (!NILP (tem))
  907.     {
  908.       unshow_buffer (p);
  909.       unchain_marker (p->pointm);
  910.       unchain_marker (p->start);
  911.     }
  912.  
  913.   tem = p->next;
  914.   if (!NILP (tem))
  915.     XWINDOW (tem)->prev = p->prev;
  916.  
  917.   tem = p->prev;
  918.   if (!NILP (tem))
  919.     XWINDOW (tem)->next = p->next;
  920.  
  921.   if (EQ (window, par->hchild))
  922.     par->hchild = p->next;
  923.   if (EQ (window, par->vchild))
  924.     par->vchild = p->next;
  925.  
  926.   /* Find one of our siblings to give our space to.  */
  927.   sib = p->prev;
  928.   if (NILP (sib))
  929.     {
  930.       /* If p gives its space to its next sibling, that sibling needs
  931.      to have its top/left side pulled back to where p's is.
  932.      set_window_{height,width} will re-position the sibling's
  933.      children.  */
  934.       sib = p->next;
  935.       XWINDOW (sib)->top = p->top;
  936.       XWINDOW (sib)->left = p->left;
  937.     }
  938.  
  939.   /* Stretch that sibling.  */
  940.   if (!NILP (par->vchild))
  941.     set_window_height (sib,
  942.                XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
  943.                1);
  944.   if (!NILP (par->hchild))
  945.     set_window_width (sib,
  946.               XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
  947.               1);
  948.  
  949.   /* If parent now has only one child,
  950.      put the child into the parent's place.  */
  951.   tem = par->hchild;
  952.   if (NILP (tem))
  953.     tem = par->vchild;
  954.   if (NILP (XWINDOW (tem)->next))
  955.     replace_window (parent, tem);
  956.  
  957.   /* Since we may be deleting combination windows, we must make sure that
  958.      not only p but all its children have been marked as deleted.  */
  959.   if (! NILP (p->hchild))
  960.     delete_all_subwindows (XWINDOW (p->hchild));
  961.   else if (! NILP (p->vchild))
  962.     delete_all_subwindows (XWINDOW (p->vchild));
  963.  
  964.   /* Mark this window as deleted.  */
  965.   p->buffer = p->hchild = p->vchild = Qnil;
  966. }
  967.  
  968.  
  969. extern Lisp_Object next_frame (), prev_frame ();
  970.  
  971. /* This comment supplies the doc string for `next-window',
  972.    for make-docfile to see.  We cannot put this in the real DEFUN
  973.    due to limits in the Unix cpp.
  974.  
  975. DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
  976.   "Return next window after WINDOW in canonical ordering of windows.\n\
  977. If omitted, WINDOW defaults to the selected window.\n\
  978. \n\
  979. Optional second arg MINIBUF t means count the minibuffer window even\n\
  980. if not active.  MINIBUF nil or omitted means count the minibuffer iff\n\
  981. it is active.  MINIBUF neither t nor nil means not to count the\n\
  982. minibuffer even if it is active.\n\
  983. \n\
  984. Several frames may share a single minibuffer; if the minibuffer\n\
  985. counts, all windows on all frames that share that minibuffer count\n\
  986. too.  Therefore, `next-window' can be used to iterate through the\n\
  987. set of windows even when the minibuffer is on another frame.  If the\n\
  988. minibuffer does not count, only windows from WINDOW's frame count.\n\
  989. \n\
  990. Optional third arg ALL-FRAMES t means include windows on all frames.\n\
  991. ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
  992. above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
  993. ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
  994. If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
  995. Anything else means restrict to WINDOW's frame.\n\
  996. \n\
  997. If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
  998. `next-window' to iterate through the entire cycle of acceptable\n\
  999. windows, eventually ending up back at the window you started with.\n\
  1000. `previous-window' traverses the same cycle, in the reverse order.")
  1001.   (window, minibuf, all_frames) */
  1002.  
  1003. DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
  1004.        0)
  1005.   (window, minibuf, all_frames)
  1006.      register Lisp_Object window, minibuf, all_frames;
  1007. {
  1008.   register Lisp_Object tem;
  1009.   Lisp_Object start_window;
  1010.  
  1011.   if (NILP (window))
  1012.     window = selected_window;
  1013.   else
  1014.     CHECK_LIVE_WINDOW (window, 0);
  1015.  
  1016.   start_window = window;
  1017.  
  1018.   /* minibuf == nil may or may not include minibuffers.
  1019.      Decide if it does.  */
  1020.   if (NILP (minibuf))
  1021.     minibuf = (minibuf_level ? minibuf_window : Qlambda);
  1022.   else if (! EQ (minibuf, Qt))
  1023.     minibuf = Qlambda;
  1024.   /* Now minibuf can be t => count all minibuffer windows,
  1025.      lambda => count none of them,
  1026.      or a specific minibuffer window (the active one) to count.  */
  1027.  
  1028.   /* all_frames == nil doesn't specify which frames to include.  */
  1029.   if (NILP (all_frames))
  1030.     all_frames = (! EQ (minibuf, Qlambda)
  1031.           ? (FRAME_MINIBUF_WINDOW
  1032.              (XFRAME
  1033.               (WINDOW_FRAME
  1034.                (XWINDOW (window)))))
  1035.           : Qnil);
  1036.   else if (EQ (all_frames, Qvisible))
  1037.     ;
  1038.   else if (XFASTINT (all_frames) == 0)
  1039.     ;
  1040.   else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
  1041.     /* If all_frames is a frame and window arg isn't on that frame, just
  1042.        return the first window on the frame.  */
  1043.     return Fframe_first_window (all_frames);
  1044.   else if (! EQ (all_frames, Qt))
  1045.     all_frames = Qnil;
  1046.   /* Now all_frames is t meaning search all frames,
  1047.      nil meaning search just current frame,
  1048.      visible meaning search just visible frames,
  1049.      0 meaning search visible and iconified frames,
  1050.      or a window, meaning search the frame that window belongs to.  */
  1051.  
  1052.   /* Do this loop at least once, to get the next window, and perhaps
  1053.      again, if we hit the minibuffer and that is not acceptable.  */
  1054.   do
  1055.     {
  1056.       /* Find a window that actually has a next one.  This loop
  1057.      climbs up the tree.  */
  1058.       while (tem = XWINDOW (window)->next, NILP (tem))
  1059.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  1060.       window = tem;
  1061.     else
  1062.       {
  1063.         /* We've reached the end of this frame.
  1064.            Which other frames are acceptable?  */
  1065.         tem = WINDOW_FRAME (XWINDOW (window));
  1066.         if (! NILP (all_frames))
  1067.           {
  1068.         Lisp_Object tem1;
  1069.  
  1070.         tem1 = tem;
  1071.         tem = next_frame (tem, all_frames);
  1072.         /* In the case where the minibuffer is active,
  1073.            and we include its frame as well as the selected one,
  1074.            next_frame may get stuck in that frame.
  1075.            If that happens, go back to the selected frame
  1076.            so we can complete the cycle.  */
  1077.         if (EQ (tem, tem1))
  1078.           XSETFRAME (tem, selected_frame);
  1079.           }
  1080.         tem = FRAME_ROOT_WINDOW (XFRAME (tem));
  1081.  
  1082.         break;
  1083.       }
  1084.  
  1085.       window = tem;
  1086.  
  1087.       /* If we're in a combination window, find its first child and
  1088.      recurse on that.  Otherwise, we've found the window we want.  */
  1089.       while (1)
  1090.     {
  1091.       if (!NILP (XWINDOW (window)->hchild))
  1092.         window = XWINDOW (window)->hchild;
  1093.       else if (!NILP (XWINDOW (window)->vchild))
  1094.         window = XWINDOW (window)->vchild;
  1095.       else break;
  1096.     }
  1097.     }
  1098.   /* Which windows are acceptable?
  1099.      Exit the loop and accept this window if
  1100.      this isn't a minibuffer window,
  1101.      or we're accepting all minibuffer windows,
  1102.      or this is the active minibuffer and we are accepting that one, or
  1103.      we've come all the way around and we're back at the original window.  */
  1104.   while (MINI_WINDOW_P (XWINDOW (window))
  1105.      && ! EQ (minibuf, Qt)
  1106.      && ! EQ (minibuf, window)
  1107.      && ! EQ (window, start_window));
  1108.  
  1109.   return window;
  1110. }
  1111.  
  1112. /* This comment supplies the doc string for `previous-window',
  1113.    for make-docfile to see.  We cannot put this in the real DEFUN
  1114.    due to limits in the Unix cpp.
  1115.  
  1116. DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
  1117.   "Return the window preceding WINDOW in canonical ordering of windows.\n\
  1118. If omitted, WINDOW defaults to the selected window.\n\
  1119. \n\
  1120. Optional second arg MINIBUF t means count the minibuffer window even\n\
  1121. if not active.  MINIBUF nil or omitted means count the minibuffer iff\n\
  1122. it is active.  MINIBUF neither t nor nil means not to count the\n\
  1123. minibuffer even if it is active.\n\
  1124. \n\
  1125. Several frames may share a single minibuffer; if the minibuffer\n\
  1126. counts, all windows on all frames that share that minibuffer count\n\
  1127. too.  Therefore, `previous-window' can be used to iterate through\n\
  1128. the set of windows even when the minibuffer is on another frame.  If\n\
  1129. the minibuffer does not count, only windows from WINDOW's frame count\n\
  1130. \n\
  1131. Optional third arg ALL-FRAMES t means include windows on all frames.\n\
  1132. ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
  1133. above.  ALL-FRAMES = `visible' means include windows on all visible frames.\n\
  1134. ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
  1135. If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
  1136. Anything else means restrict to WINDOW's frame.\n\
  1137. \n\
  1138. If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
  1139. `previous-window' to iterate through the entire cycle of acceptable\n\
  1140. windows, eventually ending up back at the window you started with.\n\
  1141. `next-window' traverses the same cycle, in the reverse order.")
  1142.   (window, minibuf, all_frames)  */
  1143.  
  1144.  
  1145. DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
  1146.        0)
  1147.   (window, minibuf, all_frames)
  1148.      register Lisp_Object window, minibuf, all_frames;
  1149. {
  1150.   register Lisp_Object tem;
  1151.   Lisp_Object start_window;
  1152.  
  1153.   if (NILP (window))
  1154.     window = selected_window;
  1155.   else
  1156.     CHECK_LIVE_WINDOW (window, 0);
  1157.  
  1158.   start_window = window;
  1159.  
  1160.   /* minibuf == nil may or may not include minibuffers.
  1161.      Decide if it does.  */
  1162.   if (NILP (minibuf))
  1163.     minibuf = (minibuf_level ? minibuf_window : Qlambda);
  1164.   else if (! EQ (minibuf, Qt))
  1165.     minibuf = Qlambda;
  1166.   /* Now minibuf can be t => count all minibuffer windows,
  1167.      lambda => count none of them,
  1168.      or a specific minibuffer window (the active one) to count.  */
  1169.  
  1170.   /* all_frames == nil doesn't specify which frames to include.
  1171.      Decide which frames it includes.  */
  1172.   if (NILP (all_frames))
  1173.     all_frames = (! EQ (minibuf, Qlambda)
  1174.            ? (FRAME_MINIBUF_WINDOW
  1175.               (XFRAME
  1176.                (WINDOW_FRAME
  1177.             (XWINDOW (window)))))
  1178.            : Qnil);
  1179.   else if (EQ (all_frames, Qvisible))
  1180.     ;
  1181.   else if (XFASTINT (all_frames) == 0)
  1182.     ;
  1183.   else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
  1184.     /* If all_frames is a frame and window arg isn't on that frame, just
  1185.        return the first window on the frame.  */
  1186.     return Fframe_first_window (all_frames);
  1187.   else if (! EQ (all_frames, Qt))
  1188.     all_frames = Qnil;
  1189.   /* Now all_frames is t meaning search all frames,
  1190.      nil meaning search just current frame,
  1191.      visible meaning search just visible frames,
  1192.      0 meaning search visible and iconified frames,
  1193.      or a window, meaning search the frame that window belongs to.  */
  1194.  
  1195.   /* Do this loop at least once, to get the previous window, and perhaps
  1196.      again, if we hit the minibuffer and that is not acceptable.  */
  1197.   do
  1198.     {
  1199.       /* Find a window that actually has a previous one.  This loop
  1200.      climbs up the tree.  */
  1201.       while (tem = XWINDOW (window)->prev, NILP (tem))
  1202.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  1203.       window = tem;
  1204.     else
  1205.       {
  1206.         /* We have found the top window on the frame.
  1207.            Which frames are acceptable?  */
  1208.         tem = WINDOW_FRAME (XWINDOW (window));
  1209.         if (! NILP (all_frames))
  1210.           /* It's actually important that we use prev_frame here,
  1211.          rather than next_frame.  All the windows acceptable
  1212.          according to the given parameters should form a ring;
  1213.          Fnext_window and Fprevious_window should go back and
  1214.          forth around the ring.  If we use next_frame here,
  1215.          then Fnext_window and Fprevious_window take different
  1216.          paths through the set of acceptable windows.
  1217.          window_loop assumes that these `ring' requirement are
  1218.          met.  */
  1219.           {
  1220.         Lisp_Object tem1;
  1221.  
  1222.         tem1 = tem;
  1223.         tem = prev_frame (tem, all_frames);
  1224.         /* In the case where the minibuffer is active,
  1225.            and we include its frame as well as the selected one,
  1226.            next_frame may get stuck in that frame.
  1227.            If that happens, go back to the selected frame
  1228.            so we can complete the cycle.  */
  1229.         if (EQ (tem, tem1))
  1230.           XSETFRAME (tem, selected_frame);
  1231.           }
  1232.         /* If this frame has a minibuffer, find that window first,
  1233.            because it is conceptually the last window in that frame.  */
  1234.         if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
  1235.           tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
  1236.         else
  1237.           tem = FRAME_ROOT_WINDOW (XFRAME (tem));
  1238.  
  1239.         break;
  1240.       }
  1241.  
  1242.       window = tem;
  1243.       /* If we're in a combination window, find its last child and
  1244.      recurse on that.  Otherwise, we've found the window we want.  */
  1245.       while (1)
  1246.     {
  1247.       if (!NILP (XWINDOW (window)->hchild))
  1248.         window = XWINDOW (window)->hchild;
  1249.       else if (!NILP (XWINDOW (window)->vchild))
  1250.         window = XWINDOW (window)->vchild;
  1251.       else break;
  1252.       while (tem = XWINDOW (window)->next, !NILP (tem))
  1253.         window = tem;
  1254.     }
  1255.     }
  1256.   /* Which windows are acceptable?
  1257.      Exit the loop and accept this window if
  1258.      this isn't a minibuffer window,
  1259.      or we're accepting all minibuffer windows,
  1260.      or this is the active minibuffer and we are accepting that one, or
  1261.      we've come all the way around and we're back at the original window.  */
  1262.   while (MINI_WINDOW_P (XWINDOW (window))
  1263.      && ! EQ (minibuf, Qt)
  1264.      && ! EQ (minibuf, window)
  1265.      && ! EQ (window, start_window));
  1266.  
  1267.   return window;
  1268. }
  1269.  
  1270. DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
  1271.   "Select the ARG'th different window on this frame.\n\
  1272. All windows on current frame are arranged in a cyclic order.\n\
  1273. This command selects the window ARG steps away in that order.\n\
  1274. A negative ARG moves in the opposite order.  If the optional second\n\
  1275. argument ALL_FRAMES is non-nil, cycle through all frames.")
  1276.   (arg, all_frames)
  1277.      register Lisp_Object arg, all_frames;
  1278. {
  1279.   register int i;
  1280.   register Lisp_Object w;
  1281.  
  1282.   CHECK_NUMBER (arg, 0);
  1283.   w = selected_window;
  1284.   i = XINT (arg);
  1285.  
  1286.   while (i > 0)
  1287.     {
  1288.       w = Fnext_window (w, Qnil, all_frames);
  1289.       i--;
  1290.     }
  1291.   while (i < 0)
  1292.     {
  1293.       w = Fprevious_window (w, Qnil, all_frames);
  1294.       i++;
  1295.     }
  1296.   Fselect_window (w);
  1297.   return Qnil;
  1298. }
  1299.  
  1300. /* Look at all windows, performing an operation specified by TYPE
  1301.    with argument OBJ.
  1302.    If FRAMES is Qt, look at all frames;
  1303.                 Qnil, look at just the selected frame;
  1304.         Qvisible, look at visible frames;
  1305.             a frame, just look at windows on that frame.
  1306.    If MINI is non-zero, perform the operation on minibuffer windows too.
  1307. */
  1308.  
  1309. enum window_loop
  1310. {
  1311.   WINDOW_LOOP_UNUSED,
  1312.   GET_BUFFER_WINDOW,        /* Arg is buffer */
  1313.   GET_LRU_WINDOW,        /* Arg is t for full-width windows only */
  1314.   DELETE_OTHER_WINDOWS,        /* Arg is window not to delete */
  1315.   DELETE_BUFFER_WINDOWS,    /* Arg is buffer */
  1316.   GET_LARGEST_WINDOW,
  1317.   UNSHOW_BUFFER        /* Arg is buffer */
  1318. };
  1319.  
  1320. static Lisp_Object
  1321. window_loop (type, obj, mini, frames)
  1322.      enum window_loop type;
  1323.      register Lisp_Object obj, frames;
  1324.      int mini;
  1325. {
  1326.   register Lisp_Object w;
  1327.   register Lisp_Object best_window;
  1328.   register Lisp_Object next_window;
  1329.   register Lisp_Object last_window;
  1330.   FRAME_PTR frame;
  1331.   Lisp_Object frame_arg;
  1332.   frame_arg = Qt;
  1333.  
  1334.   /* If we're only looping through windows on a particular frame,
  1335.      frame points to that frame.  If we're looping through windows
  1336.      on all frames, frame is 0.  */
  1337.   if (FRAMEP (frames))
  1338.     frame = XFRAME (frames);
  1339.   else if (NILP (frames))
  1340.     frame = selected_frame;
  1341.   else
  1342.     frame = 0;
  1343.   if (frame)
  1344.     frame_arg = Qlambda;
  1345.   else if (XFASTINT (frames) == 0)
  1346.     frame_arg = frames;
  1347.   else if (EQ (frames, Qvisible))
  1348.     frame_arg = frames;
  1349.  
  1350.   /* frame_arg is Qlambda to stick to one frame,
  1351.      Qvisible to consider all visible frames,
  1352.      or Qt otherwise.  */
  1353.  
  1354.   /* Pick a window to start with.  */
  1355.   if (WINDOWP (obj))
  1356.     w = obj;
  1357.   else if (frame)
  1358.     w = FRAME_SELECTED_WINDOW (frame);
  1359.   else
  1360.     w = FRAME_SELECTED_WINDOW (selected_frame);
  1361.  
  1362.   /* Figure out the last window we're going to mess with.  Since
  1363.      Fnext_window, given the same options, is guaranteed to go in a
  1364.      ring, we can just use Fprevious_window to find the last one.
  1365.  
  1366.      We can't just wait until we hit the first window again, because
  1367.      it might be deleted.  */
  1368.  
  1369.   last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
  1370.  
  1371.   best_window = Qnil;
  1372.   for (;;)
  1373.     {
  1374.       FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
  1375.  
  1376.       /* Pick the next window now, since some operations will delete
  1377.      the current window.  */
  1378.       next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
  1379.  
  1380.       /* Note that we do not pay attention here to whether
  1381.      the frame is visible, since Fnext_window skips non-visible frames
  1382.      if that is desired, under the control of frame_arg.  */
  1383.       if (! MINI_WINDOW_P (XWINDOW (w))
  1384.       || (mini && minibuf_level > 0))
  1385.     switch (type)
  1386.       {
  1387.       case GET_BUFFER_WINDOW:
  1388.         if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
  1389.         /* Don't find any minibuffer window
  1390.            except the one that is currently in use.  */
  1391.         && (MINI_WINDOW_P (XWINDOW (w))
  1392.             ? EQ (w, minibuf_window) : 1))
  1393.           return w;
  1394.         break;
  1395.  
  1396.       case GET_LRU_WINDOW:
  1397.         /* t as arg means consider only full-width windows */
  1398.         if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
  1399.           break;
  1400.         /* Ignore dedicated windows and minibuffers.  */
  1401.         if (MINI_WINDOW_P (XWINDOW (w))
  1402.         || !NILP (XWINDOW (w)->dedicated))
  1403.           break;
  1404.         if (NILP (best_window)
  1405.         || (XFASTINT (XWINDOW (best_window)->use_time)
  1406.             > XFASTINT (XWINDOW (w)->use_time)))
  1407.           best_window = w;
  1408.         break;
  1409.  
  1410.       case DELETE_OTHER_WINDOWS:
  1411.         if (XWINDOW (w) != XWINDOW (obj))
  1412.           Fdelete_window (w);
  1413.         break;
  1414.  
  1415.       case DELETE_BUFFER_WINDOWS:
  1416.         if (EQ (XWINDOW (w)->buffer, obj))
  1417.           {
  1418.         FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
  1419.  
  1420.         /* If this window is dedicated, and in a frame of its own,
  1421.            kill the frame.  */
  1422.         if (EQ (w, FRAME_ROOT_WINDOW (f))
  1423.             && !NILP (XWINDOW (w)->dedicated)
  1424.             && other_visible_frames (f))
  1425.           {
  1426.             /* Skip the other windows on this frame.
  1427.                There might be one, the minibuffer!  */
  1428.             if (! EQ (w, last_window))
  1429.               while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
  1430.             {
  1431.               /* As we go, check for the end of the loop.
  1432.                  We mustn't start going around a second time.  */
  1433.               if (EQ (next_window, last_window))
  1434.                 {
  1435.                   last_window = w;
  1436.                   break;
  1437.                 }
  1438.               next_window = Fnext_window (next_window,
  1439.                               mini ? Qt : Qnil,
  1440.                               frame_arg);
  1441.             }
  1442.             /* Now we can safely delete the frame.  */
  1443.             Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
  1444.           }
  1445.         else
  1446.           /* If we're deleting the buffer displayed in the only window
  1447.              on the frame, find a new buffer to display there.  */
  1448.           if (NILP (XWINDOW (w)->parent))
  1449.             {
  1450.               Lisp_Object new_buffer;
  1451.               new_buffer = Fother_buffer (obj, Qnil,
  1452.                           XWINDOW (w)->frame);
  1453.               if (NILP (new_buffer))
  1454.             new_buffer
  1455.               = Fget_buffer_create (build_string ("*scratch*"));
  1456.               Fset_window_buffer (w, new_buffer);
  1457.               if (EQ (w, selected_window))
  1458.             Fset_buffer (XWINDOW (w)->buffer);
  1459.             }
  1460.           else
  1461.             Fdelete_window (w);
  1462.           }
  1463.         break;
  1464.  
  1465.       case GET_LARGEST_WINDOW:
  1466.         /* Ignore dedicated windows and minibuffers.  */
  1467.         if (MINI_WINDOW_P (XWINDOW (w))
  1468.         || !NILP (XWINDOW (w)->dedicated))
  1469.           break;
  1470.         {
  1471.           struct window *best_window_ptr = XWINDOW (best_window);
  1472.           struct window *w_ptr = XWINDOW (w);
  1473.           if (NILP (best_window)
  1474.           || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
  1475.               > (XFASTINT (best_window_ptr->height)
  1476.              * XFASTINT (best_window_ptr->width))))
  1477.         best_window = w;
  1478.         }
  1479.         break;
  1480.  
  1481.       case UNSHOW_BUFFER:
  1482.         if (EQ (XWINDOW (w)->buffer, obj))
  1483.           {
  1484.         /* Find another buffer to show in this window.  */
  1485.         Lisp_Object another_buffer;
  1486.         FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
  1487.         another_buffer = Fother_buffer (obj, Qnil, XWINDOW (w)->frame);
  1488.         if (NILP (another_buffer))
  1489.           another_buffer
  1490.             = Fget_buffer_create (build_string ("*scratch*"));
  1491.         /* If this window is dedicated, and in a frame of its own,
  1492.            kill the frame.  */
  1493.         if (EQ (w, FRAME_ROOT_WINDOW (f))
  1494.             && !NILP (XWINDOW (w)->dedicated)
  1495.             && other_visible_frames (f))
  1496.           {
  1497.             /* Skip the other windows on this frame.
  1498.                There might be one, the minibuffer!  */
  1499.             if (! EQ (w, last_window))
  1500.               while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
  1501.             {
  1502.               /* As we go, check for the end of the loop.
  1503.                  We mustn't start going around a second time.  */
  1504.               if (EQ (next_window, last_window))
  1505.                 {
  1506.                   last_window = w;
  1507.                   break;
  1508.                 }
  1509.               next_window = Fnext_window (next_window,
  1510.                               mini ? Qt : Qnil,
  1511.                               frame_arg);
  1512.             }
  1513.             /* Now we can safely delete the frame.  */
  1514.             Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
  1515.           }
  1516.         else
  1517.           {
  1518.             /* Otherwise show a different buffer in the window.  */
  1519.             XWINDOW (w)->dedicated = Qnil;
  1520.             Fset_window_buffer (w, another_buffer);
  1521.             if (EQ (w, selected_window))
  1522.               Fset_buffer (XWINDOW (w)->buffer);
  1523.           }
  1524.           }
  1525.         break;
  1526.       }
  1527.  
  1528.       if (EQ (w, last_window))
  1529.     break;
  1530.  
  1531.       w = next_window;
  1532.     }
  1533.  
  1534.   return best_window;
  1535. }
  1536.  
  1537. DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
  1538.   "Return the window least recently selected or used for display.\n\
  1539. If optional argument FRAME is `visible', search all visible frames.\n\
  1540. If FRAME is 0, search all visible and iconified frames.\n\
  1541. If FRAME is t, search all frames.\n\
  1542. If FRAME is nil, search only the selected frame.\n\
  1543. If FRAME is a frame, search only that frame.")
  1544.   (frame)
  1545.     Lisp_Object frame;
  1546. {
  1547.   register Lisp_Object w;
  1548.   /* First try for a window that is full-width */
  1549.   w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
  1550.   if (!NILP (w) && !EQ (w, selected_window))
  1551.     return w;
  1552.   /* If none of them, try the rest */
  1553.   return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
  1554. }
  1555.  
  1556. DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
  1557.   "Return the largest window in area.\n\
  1558. If optional argument FRAME is `visible', search all visible frames.\n\
  1559. If FRAME is 0, search all visible and iconified frames.\n\
  1560. If FRAME is t, search all frames.\n\
  1561. If FRAME is nil, search only the selected frame.\n\
  1562. If FRAME is a frame, search only that frame.")
  1563.   (frame)
  1564.     Lisp_Object frame;
  1565. {
  1566.   return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
  1567.               frame);
  1568. }
  1569.  
  1570. DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
  1571.   "Return a window currently displaying BUFFER, or nil if none.\n\
  1572. If optional argument FRAME is `visible', search all visible frames.\n\
  1573. If optional argument FRAME is 0, search all visible and iconified frames.\n\
  1574. If FRAME is t, search all frames.\n\
  1575. If FRAME is nil, search only the selected frame.\n\
  1576. If FRAME is a frame, search only that frame.")
  1577.   (buffer, frame)
  1578.     Lisp_Object buffer, frame;
  1579. {
  1580.   buffer = Fget_buffer (buffer);
  1581.   if (BUFFERP (buffer))
  1582.     return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
  1583.   else
  1584.     return Qnil;
  1585. }
  1586.  
  1587. DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
  1588.   0, 1, "",
  1589.   "Make WINDOW (or the selected window) fill its frame.\n\
  1590. Only the frame WINDOW is on is affected.\n\
  1591. This function tries to reduce display jumps\n\
  1592. by keeping the text previously visible in WINDOW\n\
  1593. in the same place on the frame.  Doing this depends on\n\
  1594. the value of (window-start WINDOW), so if calling this function\n\
  1595. in a program gives strange scrolling, make sure the window-start\n\
  1596. value is reasonable when this function is called.")
  1597.   (window)
  1598.      Lisp_Object window;
  1599. {
  1600.   struct window *w;
  1601.   int startpos;
  1602.   int top;
  1603.  
  1604.   if (NILP (window))
  1605.     window = selected_window;
  1606.   else
  1607.     CHECK_LIVE_WINDOW (window, 0);
  1608.  
  1609.   w = XWINDOW (window);
  1610.  
  1611.   startpos = marker_position (w->start);
  1612.   top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
  1613.  
  1614.   if (MINI_WINDOW_P (w) && top > 0)
  1615.     error ("Can't expand minibuffer to full frame");
  1616.  
  1617.   window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
  1618.  
  1619.   /* Try to minimize scrolling, by setting the window start to the point
  1620.      will cause the text at the old window start to be at the same place
  1621.      on the frame.  But don't try to do this if the window start is
  1622.      outside the visible portion (as might happen when the display is
  1623.      not current, due to typeahead).  */
  1624.   if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
  1625.       && startpos <= BUF_ZV (XBUFFER (w->buffer)))
  1626.     {
  1627.       struct position pos;
  1628.       struct buffer *obuf = current_buffer;
  1629.  
  1630.       Fset_buffer (w->buffer);
  1631.       /* This computation used to temporarily move point, but that can
  1632.      have unwanted side effects due to text properties.  */
  1633.       pos = *vmotion (startpos, -top, w);
  1634.  
  1635.       set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
  1636.       w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
  1637.                    || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
  1638.                   : Qnil);
  1639.       /* We need to do this, so that the window-scroll-functions
  1640.      get called.  */
  1641.       w->optional_new_start = Qt;
  1642.  
  1643.       set_buffer_internal (obuf);
  1644.     }
  1645.   return Qnil;
  1646. }
  1647.  
  1648. DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
  1649.   1, 2, "bDelete windows on (buffer): ",
  1650.   "Delete all windows showing BUFFER.\n\
  1651. Optional second argument FRAME controls which frames are affected.\n\
  1652. If optional argument FRAME is `visible', search all visible frames.\n\
  1653. If FRAME is 0, search all visible and iconified frames.\n\
  1654. If FRAME is nil, search all frames.\n\
  1655. If FRAME is t, search only the selected frame.\n\
  1656. If FRAME is a frame, search only that frame.")
  1657.   (buffer, frame)
  1658.      Lisp_Object buffer, frame;
  1659. {
  1660.   /* FRAME uses t and nil to mean the opposite of what window_loop
  1661.      expects.  */
  1662.   if (NILP (frame))
  1663.     frame = Qt;
  1664.   else if (EQ (frame, Qt))
  1665.     frame = Qnil;
  1666.  
  1667.   if (!NILP (buffer))
  1668.     {
  1669.       buffer = Fget_buffer (buffer);
  1670.       CHECK_BUFFER (buffer, 0);
  1671.       window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
  1672.     }
  1673.   return Qnil;
  1674. }
  1675.  
  1676. DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
  1677.   Sreplace_buffer_in_windows,
  1678.   1, 1, "bReplace buffer in windows: ",
  1679.   "Replace BUFFER with some other buffer in all windows showing it.")
  1680.   (buffer)
  1681.      Lisp_Object buffer;
  1682. {
  1683.   if (!NILP (buffer))
  1684.     {
  1685.       buffer = Fget_buffer (buffer);
  1686.       CHECK_BUFFER (buffer, 0);
  1687.       window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
  1688.     }
  1689.   return Qnil;
  1690. }
  1691.  
  1692. /* Replace BUFFER with some other buffer in all windows
  1693.    of all frames, even those on other keyboards.  */
  1694.  
  1695. void
  1696. replace_buffer_in_all_windows (buffer)
  1697.      Lisp_Object buffer;
  1698. {
  1699. #ifdef MULTI_KBOARD
  1700.   Lisp_Object tail, frame;
  1701.  
  1702.   /* A single call to window_loop won't do the job
  1703.      because it only considers frames on the current keyboard.
  1704.      So loop manually over frames, and handle each one.  */
  1705.   FOR_EACH_FRAME (tail, frame)
  1706.     window_loop (UNSHOW_BUFFER, buffer, 1, frame);
  1707. #else
  1708.   window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
  1709. #endif
  1710. }
  1711.  
  1712. /* Set the height of WINDOW and all its inferiors.  */
  1713.  
  1714. /* The smallest acceptable dimensions for a window.  Anything smaller
  1715.    might crash Emacs.  */
  1716. #define MIN_SAFE_WINDOW_WIDTH  (2)
  1717. #define MIN_SAFE_WINDOW_HEIGHT (2)
  1718.  
  1719. /* Make sure that window_min_height and window_min_width are
  1720.    not too small; if they are, set them to safe minima.  */
  1721.  
  1722. static void
  1723. check_min_window_sizes ()
  1724. {
  1725.   /* Smaller values might permit a crash.  */
  1726.   if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
  1727.     window_min_width = MIN_SAFE_WINDOW_WIDTH;
  1728.   if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
  1729.     window_min_height = MIN_SAFE_WINDOW_HEIGHT;
  1730. }
  1731.  
  1732. /* If *ROWS or *COLS are too small a size for FRAME, set them to the
  1733.    minimum allowable size.  */
  1734. void
  1735. check_frame_size (frame, rows, cols)
  1736.      FRAME_PTR frame;
  1737.      int *rows, *cols;
  1738. {
  1739.   /* For height, we have to see:
  1740.      whether the frame has a minibuffer,
  1741.      whether it wants a mode line, and
  1742.      whether it has a menu bar.  */
  1743.   int min_height =
  1744.     (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
  1745.      : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
  1746.      : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
  1747.   if (FRAME_MENU_BAR_LINES (frame) > 0)
  1748.     min_height += FRAME_MENU_BAR_LINES (frame);
  1749.  
  1750.   if (*rows < min_height)
  1751.     *rows = min_height;
  1752.   if (*cols  < MIN_SAFE_WINDOW_WIDTH)
  1753.     *cols = MIN_SAFE_WINDOW_WIDTH;
  1754. }
  1755.  
  1756. /* Normally the window is deleted if it gets too small.
  1757.    nodelete nonzero means do not do this.
  1758.    (The caller should check later and do so if appropriate)  */
  1759.  
  1760. void
  1761. set_window_height (window, height, nodelete)
  1762.      Lisp_Object window;
  1763.      int height;
  1764.      int nodelete;
  1765. {
  1766.   register struct window *w = XWINDOW (window);
  1767.   register struct window *c;
  1768.   int oheight = XFASTINT (w->height);
  1769.   int top, pos, lastbot, opos, lastobot;
  1770.   Lisp_Object child;
  1771.  
  1772.   check_min_window_sizes ();
  1773.  
  1774.   if (!nodelete
  1775.       && ! NILP (w->parent)
  1776.       && (MINI_WINDOW_P (w)
  1777.       ? height < 1
  1778.       : height < window_min_height))
  1779.     {
  1780.       delete_window (window);
  1781.       return;
  1782.     }
  1783.  
  1784.   XSETFASTINT (w->last_modified, 0);
  1785.   XSETFASTINT (w->last_overlay_modified, 0);
  1786.   windows_or_buffers_changed++;
  1787.   FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
  1788.  
  1789.   XSETFASTINT (w->height, height);
  1790.   if (!NILP (w->hchild))
  1791.     {
  1792.       for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1793.     {
  1794.       XWINDOW (child)->top = w->top;
  1795.       set_window_height (child, height, nodelete);
  1796.     }
  1797.     }
  1798.   else if (!NILP (w->vchild))
  1799.     {
  1800.       lastbot = top = XFASTINT (w->top);
  1801.       lastobot = 0;
  1802.       for (child = w->vchild; !NILP (child); child = c->next)
  1803.     {
  1804.       c = XWINDOW (child);
  1805.  
  1806.       opos = lastobot + XFASTINT (c->height);
  1807.  
  1808.       XSETFASTINT (c->top, lastbot);
  1809.  
  1810.       pos = (((opos * height) << 1) + oheight) / (oheight << 1);
  1811.  
  1812.       /* Avoid confusion: inhibit deletion of child if becomes too small */
  1813.       set_window_height (child, pos + top - lastbot, 1);
  1814.  
  1815.       /* Now advance child to next window,
  1816.          and set lastbot if child was not just deleted.  */
  1817.       lastbot = pos + top;
  1818.       lastobot = opos;
  1819.     }
  1820.       /* Now delete any children that became too small.  */
  1821.       if (!nodelete)
  1822.     for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1823.       {
  1824.         set_window_height (child, XINT (XWINDOW (child)->height), 0);
  1825.       }
  1826.     }
  1827. }
  1828.  
  1829. /* Recursively set width of WINDOW and its inferiors. */
  1830.  
  1831. void
  1832. set_window_width (window, width, nodelete)
  1833.      Lisp_Object window;
  1834.      int width;
  1835.      int nodelete;
  1836. {
  1837.   register struct window *w = XWINDOW (window);
  1838.   register struct window *c;
  1839.   int owidth = XFASTINT (w->width);
  1840.   int left, pos, lastright, opos, lastoright;
  1841.   Lisp_Object child;
  1842.  
  1843.   if (!nodelete && width < window_min_width && !NILP (w->parent))
  1844.     {
  1845.       delete_window (window);
  1846.       return;
  1847.     }
  1848.  
  1849.   XSETFASTINT (w->last_modified, 0);
  1850.   XSETFASTINT (w->last_overlay_modified, 0);
  1851.   windows_or_buffers_changed++;
  1852.   FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
  1853.  
  1854.   XSETFASTINT (w->width, width);
  1855.   if (!NILP (w->vchild))
  1856.     {
  1857.       for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1858.     {
  1859.       XWINDOW (child)->left = w->left;
  1860.       set_window_width (child, width, nodelete);
  1861.     }
  1862.     }
  1863.   else if (!NILP (w->hchild))
  1864.     {
  1865.       lastright = left = XFASTINT (w->left);
  1866.       lastoright = 0;
  1867.       for (child = w->hchild; !NILP (child); child = c->next)
  1868.     {
  1869.       c = XWINDOW (child);
  1870.  
  1871.       opos = lastoright + XFASTINT (c->width);
  1872.  
  1873.       XSETFASTINT (c->left, lastright);
  1874.  
  1875.       pos = (((opos * width) << 1) + owidth) / (owidth << 1);
  1876.  
  1877.       /* Inhibit deletion for becoming too small */
  1878.       set_window_width (child, pos + left - lastright, 1);
  1879.  
  1880.       /* Now advance child to next window,
  1881.          and set lastright if child was not just deleted.  */
  1882.       lastright = pos + left, lastoright = opos;
  1883.     }
  1884.       /* Delete children that became too small */
  1885.       if (!nodelete)
  1886.     for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1887.       {
  1888.         set_window_width (child, XINT (XWINDOW (child)->width), 0);
  1889.       }
  1890.     }
  1891. }
  1892.  
  1893. int window_select_count;
  1894.  
  1895. Lisp_Object
  1896. Fset_window_buffer_unwind (obuf)
  1897.      Lisp_Object obuf;
  1898. {
  1899.   Fset_buffer (obuf);
  1900.   return Qnil;
  1901. }
  1902.  
  1903. DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
  1904.   "Make WINDOW display BUFFER as its contents.\n\
  1905. BUFFER can be a buffer or buffer name.")
  1906.   (window, buffer)
  1907.      register Lisp_Object window, buffer;
  1908. {
  1909.   register Lisp_Object tem;
  1910.   register struct window *w = decode_window (window);
  1911.   int count = specpdl_ptr - specpdl;
  1912.  
  1913.   buffer = Fget_buffer (buffer);
  1914.   CHECK_BUFFER (buffer, 1);
  1915.  
  1916.   if (NILP (XBUFFER (buffer)->name))
  1917.     error ("Attempt to display deleted buffer");
  1918.  
  1919.   tem = w->buffer;
  1920.   if (NILP (tem))
  1921.     error ("Window is deleted");
  1922.   else if (! EQ (tem, Qt))    /* w->buffer is t when the window
  1923.                    is first being set up.  */
  1924.     {
  1925.       if (!NILP (w->dedicated) && !EQ (tem, buffer))
  1926.     error ("Window is dedicated to `%s'",
  1927.            XSTRING (XBUFFER (tem)->name)->data);
  1928.  
  1929.       unshow_buffer (w);
  1930.     }
  1931.  
  1932.   w->buffer = buffer;
  1933.  
  1934.   if (EQ (window, selected_window))
  1935.     XBUFFER (w->buffer)->last_selected_window = window;
  1936.  
  1937.   /* Update time stamps of buffer display.  */
  1938.   if (INTEGERP (XBUFFER (buffer)->display_count))
  1939.     XSETINT (XBUFFER (buffer)->display_count,
  1940.          XINT (XBUFFER (buffer)->display_count) + 1);
  1941.   XBUFFER (buffer)->display_time = Fcurrent_time ();
  1942.  
  1943.   XSETFASTINT (w->window_end_pos, 0);
  1944.   w->window_end_valid = Qnil;
  1945.   XSETFASTINT (w->hscroll, 0);
  1946.   set_marker_both (w->pointm, buffer,
  1947.            BUF_PT (XBUFFER (buffer)), BUF_PT_BYTE (XBUFFER (buffer)));
  1948.   set_marker_restricted (w->start,
  1949.              make_number (XBUFFER (buffer)->last_window_start),
  1950.              buffer);
  1951.   w->start_at_line_beg = Qnil;
  1952.   w->force_start = Qnil;
  1953.   XSETFASTINT (w->last_modified, 0);
  1954.   XSETFASTINT (w->last_overlay_modified, 0);
  1955.   windows_or_buffers_changed++;
  1956.  
  1957.   /* We must select BUFFER for running the window-scroll-functions.
  1958.      If WINDOW is selected, switch permanently.
  1959.      Otherwise, switch but go back to the ambient buffer afterward.  */
  1960.   if (EQ (window, selected_window))
  1961.     Fset_buffer (buffer);
  1962.   /* We can't check ! NILP (Vwindow_scroll_functions) here
  1963.      because that might itself be a local variable.  */
  1964.   else if (window_initialized)
  1965.     {
  1966.       record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
  1967.       Fset_buffer (buffer);
  1968.     }
  1969.  
  1970.   if (! NILP (Vwindow_scroll_functions))
  1971.     run_hook_with_args_2 (Qwindow_scroll_functions, window,
  1972.               Fmarker_position (w->start));
  1973.  
  1974.   if (! NILP (Vwindow_configuration_change_hook)
  1975.       && ! NILP (Vrun_hooks))
  1976.     call1 (Vrun_hooks, Qwindow_configuration_change_hook);
  1977.  
  1978.   unbind_to (count, Qnil);
  1979.  
  1980.   return Qnil;
  1981. }
  1982.  
  1983. DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
  1984.   "Select WINDOW.  Most editing will apply to WINDOW's buffer.\n\
  1985. If WINDOW is not already selected, also make WINDOW's buffer current.\n\
  1986. Note that the main editor command loop\n\
  1987. selects the buffer of the selected window before each command.")
  1988.   (window)
  1989.      register Lisp_Object window;
  1990. {
  1991.   return select_window_1 (window, 1);
  1992. }
  1993.  
  1994. static Lisp_Object
  1995. select_window_1 (window, recordflag)
  1996.      register Lisp_Object window;
  1997.      int recordflag;
  1998. {
  1999.   register struct window *w;
  2000.   register struct window *ow = XWINDOW (selected_window);
  2001.  
  2002.   CHECK_LIVE_WINDOW (window, 0);
  2003.  
  2004.   w = XWINDOW (window);
  2005.  
  2006.   if (NILP (w->buffer))
  2007.     error ("Trying to select deleted window or non-leaf window");
  2008.  
  2009.   XSETFASTINT (w->use_time, ++window_select_count);
  2010.   if (EQ (window, selected_window))
  2011.     return window;
  2012.  
  2013.   if (! NILP (ow->buffer))
  2014.     set_marker_both (ow->pointm, ow->buffer,
  2015.              BUF_PT (XBUFFER (ow->buffer)),
  2016.              BUF_PT_BYTE (XBUFFER (ow->buffer)));
  2017.  
  2018.   selected_window = window;
  2019.   if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
  2020.     {
  2021.       XFRAME (WINDOW_FRAME (w))->selected_window = window;
  2022.       /* Use this rather than Fhandle_switch_frame
  2023.      so that FRAME_FOCUS_FRAME is moved appropriately as we
  2024.      move around in the state where a minibuffer in a separate
  2025.      frame is active.  */
  2026.       Fselect_frame (WINDOW_FRAME (w), Qnil);
  2027.     }
  2028.   else
  2029.     selected_frame->selected_window = window;
  2030.  
  2031.   if (recordflag)
  2032.     record_buffer (w->buffer);
  2033.   Fset_buffer (w->buffer);
  2034.  
  2035.   XBUFFER (w->buffer)->last_selected_window = window;
  2036.  
  2037.   /* Go to the point recorded in the window.
  2038.      This is important when the buffer is in more
  2039.      than one window.  It also matters when
  2040.      redisplay_window has altered point after scrolling,
  2041.      because it makes the change only in the window.  */
  2042.   {
  2043.     register int new_point = marker_position (w->pointm);
  2044.     if (new_point < BEGV)
  2045.       SET_PT (BEGV);
  2046.     else if (new_point > ZV)
  2047.       SET_PT (ZV);
  2048.     else
  2049.       SET_PT (new_point);
  2050.   }
  2051.  
  2052.   windows_or_buffers_changed++;
  2053.   return window;
  2054. }
  2055.  
  2056. /* Deiconify the frame containing the window WINDOW,
  2057.    unless it is the selected frame;
  2058.    then return WINDOW.
  2059.  
  2060.    The reason for the exception for the selected frame
  2061.    is that it seems better not to change the selected frames visibility
  2062.    merely because of displaying a different buffer in it.
  2063.    The deiconification is useful when a buffer gets shown in
  2064.    another frame that you were not using lately.  */
  2065.  
  2066. static Lisp_Object
  2067. display_buffer_1 (window)
  2068.      Lisp_Object window;
  2069. {
  2070.   FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
  2071.   FRAME_SAMPLE_VISIBILITY (f);
  2072.   if (f != selected_frame)
  2073.     {
  2074.       if (FRAME_ICONIFIED_P (f))
  2075.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
  2076.       else if (FRAME_VISIBLE_P (f))
  2077.     Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
  2078.     }
  2079.   return window;
  2080. }
  2081.  
  2082. DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
  2083.   "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
  2084. The value is actually t if the frame should be called with default frame\n\
  2085. parameters, and a list of frame parameters if they were specified.\n\
  2086. See `special-display-buffer-names', and `special-display-regexps'.")
  2087.   (buffer_name)
  2088.      Lisp_Object buffer_name;
  2089. {
  2090.   Lisp_Object tem;
  2091.  
  2092.   CHECK_STRING (buffer_name, 1);
  2093.  
  2094.   tem = Fmember (buffer_name, Vspecial_display_buffer_names);
  2095.   if (!NILP (tem))
  2096.     return Qt;
  2097.  
  2098.   tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
  2099.   if (!NILP (tem))
  2100.     return XCDR (tem);
  2101.  
  2102.   for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
  2103.     {
  2104.       Lisp_Object car = XCAR (tem);
  2105.       if (STRINGP (car)
  2106.       && fast_string_match (car, buffer_name) >= 0)
  2107.     return Qt;
  2108.       else if (CONSP (car)
  2109.            && STRINGP (XCAR (car))
  2110.            && fast_string_match (XCAR (car), buffer_name) >= 0)
  2111.     return XCDR (car);
  2112.     }
  2113.   return Qnil;
  2114. }  
  2115.  
  2116. DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
  2117.   "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
  2118. See `same-window-buffer-names' and `same-window-regexps'.")
  2119.   (buffer_name)
  2120.      Lisp_Object buffer_name;
  2121. {
  2122.   Lisp_Object tem;
  2123.  
  2124.   CHECK_STRING (buffer_name, 1);
  2125.  
  2126.   tem = Fmember (buffer_name, Vsame_window_buffer_names);
  2127.   if (!NILP (tem))
  2128.     return Qt;
  2129.  
  2130.   tem = Fassoc (buffer_name, Vsame_window_buffer_names);
  2131.   if (!NILP (tem))
  2132.     return Qt;
  2133.  
  2134.   for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
  2135.     {
  2136.       Lisp_Object car = XCAR (tem);
  2137.       if (STRINGP (car)
  2138.       && fast_string_match (car, buffer_name) >= 0)
  2139.     return Qt;
  2140.       else if (CONSP (car)
  2141.            && STRINGP (XCAR (car))
  2142.            && fast_string_match (XCAR (car), buffer_name) >= 0)
  2143.     return Qt;
  2144.     }
  2145.   return Qnil;
  2146. }
  2147.  
  2148.    /* Use B so the default is (other-buffer).  */
  2149. DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
  2150.      "BDisplay buffer: \nP",
  2151.   "Make BUFFER appear in some window but don't select it.\n\
  2152. BUFFER can be a buffer or a buffer name.\n\
  2153. If BUFFER is shown already in some window, just use that one,\n\
  2154. unless the window is the selected window and the optional second\n\
  2155. argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
  2156. If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
  2157. Returns the window displaying BUFFER.\n\
  2158. \n\
  2159. The variables `special-display-buffer-names', `special-display-regexps',\n\
  2160. `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
  2161. buffer names are handled.\n\
  2162. \n\
  2163. If optional argument FRAME is `visible', search all visible frames.\n\
  2164. If FRAME is 0, search all visible and iconified frames.\n\
  2165. If FRAME is t, search all frames.\n\
  2166. If FRAME is a frame, search only that frame.\n\
  2167. If FRAME is nil, search only the selected frame\n\
  2168.  (actually the last nonminibuffer frame),\n\
  2169.  unless `pop-up-frames' is non-nil,\n\
  2170.  which means search visible and iconified frames.")
  2171.   (buffer, not_this_window, frame)
  2172.      register Lisp_Object buffer, not_this_window, frame;
  2173. {
  2174.   register Lisp_Object window, tem;
  2175.  
  2176.   buffer = Fget_buffer (buffer);
  2177.   CHECK_BUFFER (buffer, 0);
  2178.  
  2179.   if (!NILP (Vdisplay_buffer_function))
  2180.     return call2 (Vdisplay_buffer_function, buffer, not_this_window);
  2181.  
  2182.   if (NILP (not_this_window)
  2183.       && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
  2184.     return display_buffer_1 (selected_window);
  2185.  
  2186.   /* See if the user has specified this buffer should appear
  2187.      in the selected window.  */
  2188.   if (NILP (not_this_window))
  2189.     {
  2190.       tem = Fsame_window_p (XBUFFER (buffer)->name);
  2191.       if (!NILP (tem))
  2192.     {
  2193.       Fswitch_to_buffer (buffer, Qnil);
  2194.       return display_buffer_1 (selected_window);
  2195.     }
  2196.     }
  2197.  
  2198.   /* If pop_up_frames,
  2199.      look for a window showing BUFFER on any visible or iconified frame.
  2200.      Otherwise search only the current frame.  */
  2201.   if (! NILP (frame))
  2202.     tem = frame;
  2203.   else if (pop_up_frames || last_nonminibuf_frame == 0)
  2204.     XSETFASTINT (tem, 0);
  2205.   else
  2206.     XSETFRAME (tem, last_nonminibuf_frame);
  2207.   window = Fget_buffer_window (buffer, tem);
  2208.   if (!NILP (window)
  2209.       && (NILP (not_this_window) || !EQ (window, selected_window)))
  2210.     {
  2211.       return display_buffer_1 (window);
  2212.     }
  2213.  
  2214.   /* Certain buffer names get special handling.  */
  2215.   if (!NILP (Vspecial_display_function))
  2216.     {
  2217.       tem = Fspecial_display_p (XBUFFER (buffer)->name);
  2218.       if (EQ (tem, Qt))
  2219.     return call1 (Vspecial_display_function, buffer);
  2220.       if (CONSP (tem))
  2221.     return call2 (Vspecial_display_function, buffer, tem);
  2222.     }
  2223.  
  2224.   /* If there are no frames open that have more than a minibuffer,
  2225.      we need to create a new frame.  */
  2226.   if (pop_up_frames || last_nonminibuf_frame == 0)
  2227.     {
  2228.       window = Fframe_selected_window (call0 (Vpop_up_frame_function));
  2229.       Fset_window_buffer (window, buffer);
  2230.       return display_buffer_1 (window);
  2231.     }
  2232.  
  2233.   if (pop_up_windows
  2234.       || FRAME_MINIBUF_ONLY_P (selected_frame)
  2235.       /* If the current frame is a special display frame,
  2236.      don't try to reuse its windows.  */
  2237.       || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
  2238.       )
  2239.     {
  2240.       Lisp_Object frames;
  2241.  
  2242.       frames = Qnil;
  2243.       if (FRAME_MINIBUF_ONLY_P (selected_frame))
  2244.     XSETFRAME (frames, last_nonminibuf_frame);
  2245.       /* Don't try to create a window if would get an error */
  2246.       if (split_height_threshold < window_min_height << 1)
  2247.     split_height_threshold = window_min_height << 1;
  2248.  
  2249.       /* Note that both Fget_largest_window and Fget_lru_window
  2250.      ignore minibuffers and dedicated windows.
  2251.      This means they can return nil.  */
  2252.  
  2253.       /* If the frame we would try to split cannot be split,
  2254.      try other frames.  */
  2255.       if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
  2256.                 : last_nonminibuf_frame))
  2257.     {
  2258.       /* Try visible frames first.  */
  2259.       window = Fget_largest_window (Qvisible);
  2260.       /* If that didn't work, try iconified frames.  */
  2261.       if (NILP (window))
  2262.         window = Fget_largest_window (make_number (0));
  2263.       if (NILP (window))
  2264.         window = Fget_largest_window (Qt);
  2265.     }
  2266.       else
  2267.     window = Fget_largest_window (frames);
  2268.  
  2269.       /* If we got a tall enough full-width window that can be split,
  2270.      split it.  */
  2271.       if (!NILP (window)
  2272.       && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
  2273.       && window_height (window) >= split_height_threshold
  2274.       && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
  2275.     window = Fsplit_window (window, Qnil, Qnil);
  2276.       else
  2277.     {
  2278.       Lisp_Object upper, lower, other;
  2279.  
  2280.       window = Fget_lru_window (frames);
  2281.       /* If the LRU window is selected, and big enough,
  2282.          and can be split, split it.  */
  2283.       if (!NILP (window)
  2284.           && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
  2285.           && (EQ (window, selected_window)
  2286.           || EQ (XWINDOW (window)->parent, Qnil))
  2287.           && window_height (window) >= window_min_height << 1)
  2288.         window = Fsplit_window (window, Qnil, Qnil);
  2289.       /* If Fget_lru_window returned nil, try other approaches.  */
  2290.  
  2291.       /* Try visible frames first.  */
  2292.       if (NILP (window))
  2293.         window = Fget_buffer_window (buffer, Qvisible);
  2294.       if (NILP (window))
  2295.         window = Fget_largest_window (Qvisible);
  2296.       /* If that didn't work, try iconified frames.  */
  2297.       if (NILP (window))
  2298.         window = Fget_buffer_window (buffer, make_number (0));
  2299.       if (NILP (window))
  2300.         window = Fget_largest_window (make_number (0));
  2301.       /* Try invisible frames.  */
  2302.       if (NILP (window))
  2303.         window = Fget_buffer_window (buffer, Qt);
  2304.       if (NILP (window))
  2305.         window = Fget_largest_window (Qt);
  2306.       /* As a last resort, make a new frame.  */
  2307.       if (NILP (window))
  2308.         window = Fframe_selected_window (call0 (Vpop_up_frame_function));
  2309.       /* If window appears above or below another,
  2310.          even out their heights.  */
  2311.       other = upper = lower = Qnil;
  2312.       if (!NILP (XWINDOW (window)->prev))
  2313.         other = upper = XWINDOW (window)->prev, lower = window;
  2314.       if (!NILP (XWINDOW (window)->next))
  2315.         other = lower = XWINDOW (window)->next, upper = window;
  2316.       if (!NILP (other)
  2317.           /* Check that OTHER and WINDOW are vertically arrayed.  */
  2318.           && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
  2319.           && (XFASTINT (XWINDOW (other)->height)
  2320.           > XFASTINT (XWINDOW (window)->height)))
  2321.         {
  2322.           int total = (XFASTINT (XWINDOW (other)->height)
  2323.                + XFASTINT (XWINDOW (window)->height));
  2324.           Lisp_Object old_selected_window;
  2325.           old_selected_window = selected_window;
  2326.  
  2327.           selected_window = upper;
  2328.           change_window_height ((total / 2
  2329.                      - XFASTINT (XWINDOW (upper)->height)),
  2330.                     0);
  2331.           selected_window = old_selected_window;
  2332.         }
  2333.     }
  2334.     }
  2335.   else
  2336.     window = Fget_lru_window (Qnil);
  2337.  
  2338.   Fset_window_buffer (window, buffer);
  2339.   return display_buffer_1 (window);
  2340. }
  2341.  
  2342. void
  2343. temp_output_buffer_show (buf)
  2344.      register Lisp_Object buf;
  2345. {
  2346.   register struct buffer *old = current_buffer;
  2347.   register Lisp_Object window;
  2348.   register struct window *w;
  2349.  
  2350.   XBUFFER (buf)->directory = current_buffer->directory;
  2351.  
  2352.   Fset_buffer (buf);
  2353.   BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
  2354.   BEGV = BEG;
  2355.   ZV = Z;
  2356.   SET_PT (BEG);
  2357.   XBUFFER (buf)->clip_changed = 1;
  2358.   set_buffer_internal (old);
  2359.  
  2360.   if (!EQ (Vtemp_buffer_show_function, Qnil))
  2361.     call1 (Vtemp_buffer_show_function, buf);
  2362.   else
  2363.     {
  2364.       window = Fdisplay_buffer (buf, Qnil, Qnil);
  2365.  
  2366.       if (XFRAME (XWINDOW (window)->frame) != selected_frame)
  2367.     Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
  2368.       Vminibuf_scroll_window = window;
  2369.       w = XWINDOW (window);
  2370.       XSETFASTINT (w->hscroll, 0);
  2371.       set_marker_restricted_both (w->start, buf, 1, 1);
  2372.       set_marker_restricted_both (w->pointm, buf, 1, 1);
  2373.  
  2374.       /* Run temp-buffer-show-hook, with the chosen window selected
  2375.      and it sbuffer current.  */
  2376.       if (!NILP (Vrun_hooks))
  2377.     {
  2378.       Lisp_Object tem;
  2379.       tem = Fboundp (Qtemp_buffer_show_hook);
  2380.       if (!NILP (tem))
  2381.         {
  2382.           tem = Fsymbol_value (Qtemp_buffer_show_hook);
  2383.           if (!NILP (tem))
  2384.         {
  2385.           int count = specpdl_ptr - specpdl;
  2386.           Lisp_Object prev_window;
  2387.           prev_window = selected_window;
  2388.  
  2389.           /* Select the window that was chosen, for running the hook.  */
  2390.           record_unwind_protect (Fset_window_configuration,
  2391.                      Fcurrent_window_configuration (Qnil));
  2392.  
  2393.           select_window_1 (window, 0);
  2394.           Fset_buffer (w->buffer);
  2395.           call1 (Vrun_hooks, Qtemp_buffer_show_hook);
  2396.           select_window_1 (prev_window, 0);
  2397.           unbind_to (count, Qnil);
  2398.         }
  2399.         }
  2400.     }
  2401.     }
  2402. }
  2403.  
  2404. static void
  2405. make_dummy_parent (window)
  2406.      Lisp_Object window;
  2407. {
  2408.   Lisp_Object new;
  2409.   register struct window *o, *p;
  2410.   register struct Lisp_Vector *vec;
  2411.   int i;
  2412.  
  2413.   o = XWINDOW (window);
  2414.   vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
  2415.   for (i = 0; i < VECSIZE (struct window); ++i)
  2416.     vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
  2417.   vec->size = VECSIZE (struct window);
  2418.   p = (struct window *)vec;
  2419.   XSETWINDOW (new, p);
  2420.  
  2421.   XSETFASTINT (p->sequence_number, ++sequence_number);
  2422.  
  2423.   /* Put new into window structure in place of window */
  2424.   replace_window (window, new);
  2425.  
  2426.   o->next = Qnil;
  2427.   o->prev = Qnil;
  2428.   o->vchild = Qnil;
  2429.   o->hchild = Qnil;
  2430.   o->parent = new;
  2431.  
  2432.   p->start = Qnil;
  2433.   p->pointm = Qnil;
  2434.   p->buffer = Qnil;
  2435. }
  2436.  
  2437. DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
  2438.   "Split WINDOW, putting SIZE lines in the first of the pair.\n\
  2439. WINDOW defaults to selected one and SIZE to half its size.\n\
  2440. If optional third arg HORFLAG is non-nil, split side by side\n\
  2441. and put SIZE columns in the first of the pair.  In that case,\n\
  2442. SIZE includes that window's scroll bar, or the divider column to its right.")
  2443.   (window, size, horflag)
  2444.      Lisp_Object window, size, horflag;
  2445. {
  2446.   register Lisp_Object new;
  2447.   register struct window *o, *p;
  2448.   FRAME_PTR fo;
  2449.   register int size_int;
  2450.  
  2451.   if (NILP (window))
  2452.     window = selected_window;
  2453.   else
  2454.     CHECK_LIVE_WINDOW (window, 0);
  2455.  
  2456.   o = XWINDOW (window);
  2457.   fo = XFRAME (WINDOW_FRAME (o));
  2458.  
  2459.   if (NILP (size))
  2460.     {
  2461.       if (!NILP (horflag))
  2462.     /* Calculate the size of the left-hand window, by dividing
  2463.        the usable space in columns by two. */
  2464.     size_int = XFASTINT (o->width) >> 1;
  2465.       else
  2466.     size_int = XFASTINT (o->height) >> 1;
  2467.     }
  2468.   else
  2469.     {
  2470.       CHECK_NUMBER (size, 1);
  2471.       size_int = XINT (size);
  2472.     }
  2473.  
  2474.   if (MINI_WINDOW_P (o))
  2475.     error ("Attempt to split minibuffer window");
  2476.  
  2477.   check_min_window_sizes ();
  2478.  
  2479.   if (NILP (horflag))
  2480.     {
  2481.       if (size_int < window_min_height)
  2482.     error ("Window height %d too small (after splitting)", size_int);
  2483.       if (size_int + window_min_height > XFASTINT (o->height))
  2484.     error ("Window height %d too small (after splitting)",
  2485.            XFASTINT (o->height) - size_int);
  2486.       if (NILP (o->parent)
  2487.       || NILP (XWINDOW (o->parent)->vchild))
  2488.     {
  2489.       make_dummy_parent (window);
  2490.       new = o->parent;
  2491.       XWINDOW (new)->vchild = window;
  2492.     }
  2493.     }
  2494.   else
  2495.     {
  2496.       if (size_int < window_min_width)
  2497.     error ("Window width %d too small (after splitting)", size_int);
  2498.  
  2499.       if (size_int + window_min_width > XFASTINT (o->width))
  2500.     error ("Window width %d too small (after splitting)",
  2501.            XFASTINT (o->width) - size_int);
  2502.       if (NILP (o->parent)
  2503.       || NILP (XWINDOW (o->parent)->hchild))
  2504.     {
  2505.       make_dummy_parent (window);
  2506.       new = o->parent;
  2507.       XWINDOW (new)->hchild = window;
  2508.     }
  2509.     }
  2510.  
  2511.   /* Now we know that window's parent is a vertical combination
  2512.      if we are dividing vertically, or a horizontal combination
  2513.      if we are making side-by-side windows */
  2514.  
  2515.   windows_or_buffers_changed++;
  2516.   FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
  2517.   new = make_window ();
  2518.   p = XWINDOW (new);
  2519.  
  2520.   p->frame = o->frame;
  2521.   p->next = o->next;
  2522.   if (!NILP (p->next))
  2523.     XWINDOW (p->next)->prev = new;
  2524.   p->prev = window;
  2525.   o->next = new;
  2526.   p->parent = o->parent;
  2527.   p->buffer = Qt;
  2528.  
  2529.   /* Apportion the available frame space among the two new windows */
  2530.  
  2531.   if (!NILP (horflag))
  2532.     {
  2533.       p->height = o->height;
  2534.       p->top = o->top;
  2535.       XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
  2536.       XSETFASTINT (o->width, size_int);
  2537.       XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
  2538.     }
  2539.   else
  2540.     {
  2541.       p->left = o->left;
  2542.       p->width = o->width;
  2543.       XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
  2544.       XSETFASTINT (o->height, size_int);
  2545.       XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
  2546.     }
  2547.  
  2548.   Fset_window_buffer (new, o->buffer);
  2549.  
  2550.   return new;
  2551. }
  2552.  
  2553. DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
  2554.   "Make current window ARG lines bigger.\n\
  2555. From program, optional second arg non-nil means grow sideways ARG columns.")
  2556.   (arg, side)
  2557.      register Lisp_Object arg, side;
  2558. {
  2559.   CHECK_NUMBER (arg, 0);
  2560.   change_window_height (XINT (arg), !NILP (side));
  2561.  
  2562.   if (! NILP (Vwindow_configuration_change_hook))
  2563.     call1 (Vrun_hooks, Qwindow_configuration_change_hook);
  2564.  
  2565.   return Qnil;
  2566. }
  2567.  
  2568. DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
  2569.   "Make current window ARG lines smaller.\n\
  2570. From program, optional second arg non-nil means shrink sideways arg columns.")
  2571.   (arg, side)
  2572.      register Lisp_Object arg, side;
  2573. {
  2574.   CHECK_NUMBER (arg, 0);
  2575.   change_window_height (-XINT (arg), !NILP (side));
  2576.  
  2577.   if (! NILP (Vwindow_configuration_change_hook))
  2578.     call1 (Vrun_hooks, Qwindow_configuration_change_hook);
  2579.  
  2580.   return Qnil;
  2581. }
  2582.  
  2583. int
  2584. window_height (window)
  2585.      Lisp_Object window;
  2586. {
  2587.   register struct window *p = XWINDOW (window);
  2588.   return XFASTINT (p->height);
  2589. }
  2590.  
  2591. int
  2592. window_width (window)
  2593.      Lisp_Object window;
  2594. {
  2595.   register struct window *p = XWINDOW (window);
  2596.   return XFASTINT (p->width);
  2597. }
  2598.  
  2599. #define MINSIZE(w)                        \
  2600.   (widthflag                            \
  2601.    ? window_min_width                        \
  2602.    : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
  2603.  
  2604. #define CURBEG(w) \
  2605.   *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
  2606.  
  2607. #define CURSIZE(w) \
  2608.   *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
  2609.  
  2610. /* Unlike set_window_height, this function
  2611.    also changes the heights of the siblings so as to
  2612.    keep everything consistent. */
  2613.  
  2614. void
  2615. change_window_height (delta, widthflag)
  2616.      register int delta;
  2617.      int widthflag;
  2618. {
  2619.   register Lisp_Object parent;
  2620.   Lisp_Object window;
  2621.   register struct window *p;
  2622.   int *sizep;
  2623.   int (*sizefun) P_ ((Lisp_Object))
  2624.     = widthflag ? window_width : window_height;
  2625.   register void (*setsizefun) P_ ((Lisp_Object, int, int))
  2626.     = (widthflag ? set_window_width : set_window_height);
  2627.   int maximum;
  2628.   Lisp_Object next, prev;
  2629.  
  2630.   check_min_window_sizes ();
  2631.  
  2632.   window = selected_window;
  2633.   while (1)
  2634.     {
  2635.       p = XWINDOW (window);
  2636.       parent = p->parent;
  2637.       if (NILP (parent))
  2638.     {
  2639.       if (widthflag)
  2640.         error ("No other window to side of this one");
  2641.       break;
  2642.     }
  2643.       if (widthflag ? !NILP (XWINDOW (parent)->hchild)
  2644.       : !NILP (XWINDOW (parent)->vchild))
  2645.     break;
  2646.       window = parent;
  2647.     }
  2648.  
  2649.   sizep = &CURSIZE (window);
  2650.  
  2651.   {
  2652.     register int maxdelta;
  2653.  
  2654.     maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
  2655.         : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
  2656.         : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
  2657.         /* This is a frame with only one window, a minibuffer-only
  2658.            or a minibufferless frame.  */
  2659.         : (delta = 0));
  2660.  
  2661.     if (delta > maxdelta)
  2662.       /* This case traps trying to make the minibuffer
  2663.      the full frame, or make the only window aside from the
  2664.      minibuffer the full frame.  */
  2665.       delta = maxdelta;
  2666.   }
  2667.  
  2668.   if (*sizep + delta < MINSIZE (window))
  2669.     {
  2670.       delete_window (window);
  2671.       return;
  2672.     }
  2673.  
  2674.   if (delta == 0)
  2675.     return;
  2676.  
  2677.   /* Find the total we can get from other siblings.  */
  2678.   maximum = 0;
  2679.   for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
  2680.     maximum += (*sizefun) (next) - MINSIZE (next);
  2681.   for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
  2682.     maximum += (*sizefun) (prev) - MINSIZE (prev);
  2683.  
  2684.   /* If we can get it all from them, do so.  */
  2685.   if (delta <= maximum)
  2686.     {
  2687.       Lisp_Object first_unaffected;
  2688.       Lisp_Object first_affected;
  2689.  
  2690.       next = p->next;
  2691.       prev = p->prev;
  2692.       first_affected = window;
  2693.       /* Look at one sibling at a time,
  2694.      moving away from this window in both directions alternately,
  2695.      and take as much as we can get without deleting that sibling.  */
  2696.       while (delta != 0)
  2697.     {
  2698.       if (delta == 0)
  2699.         break;
  2700.       if (! NILP (next))
  2701.         {
  2702.           int this_one = (*sizefun) (next) - MINSIZE (next);
  2703.           if (this_one > delta)
  2704.         this_one = delta;
  2705.  
  2706.           (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
  2707.           (*setsizefun) (window, *sizep + this_one, 0);
  2708.  
  2709.           delta -= this_one;
  2710.           next = XWINDOW (next)->next;
  2711.         }
  2712.       if (delta == 0)
  2713.         break;
  2714.       if (! NILP (prev))
  2715.         {
  2716.           int this_one = (*sizefun) (prev) - MINSIZE (prev);
  2717.           if (this_one > delta)
  2718.         this_one = delta;
  2719.  
  2720.           first_affected = prev;
  2721.  
  2722.           (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
  2723.           (*setsizefun) (window, *sizep + this_one, 0);
  2724.  
  2725.           delta -= this_one;
  2726.           prev = XWINDOW (prev)->prev;
  2727.         }
  2728.     }
  2729.  
  2730.       /* Now recalculate the edge positions of all the windows affected,
  2731.      based on the new sizes.  */
  2732.       first_unaffected = next;
  2733.       prev = first_affected;
  2734.       for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
  2735.        prev = next, next = XWINDOW (next)->next)
  2736.     {
  2737.       CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
  2738.       /* This does not change size of NEXT,
  2739.          but it propagates the new top edge to its children */
  2740.       (*setsizefun) (next, (*sizefun) (next), 0);
  2741.     }
  2742.     }
  2743.   else
  2744.     {
  2745.       register int delta1;
  2746.       register int opht = (*sizefun) (parent);
  2747.  
  2748.       /* If trying to grow this window to or beyond size of the parent,
  2749.      make delta1 so big that, on shrinking back down,
  2750.      all the siblings end up with less than one line and are deleted.  */
  2751.       if (opht <= *sizep + delta)
  2752.     delta1 = opht * opht * 2;
  2753.       /* Otherwise, make delta1 just right so that if we add delta1
  2754.      lines to this window and to the parent, and then shrink
  2755.      the parent back to its original size, the new proportional
  2756.      size of this window will increase by delta.  */
  2757.       else
  2758.     delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
  2759.  
  2760.       /* Add delta1 lines or columns to this window, and to the parent,
  2761.      keeping things consistent while not affecting siblings.  */
  2762.       CURSIZE (parent) = opht + delta1;
  2763.       (*setsizefun) (window, *sizep + delta1, 0);
  2764.  
  2765.       /* Squeeze out delta1 lines or columns from our parent,
  2766.      shriking this window and siblings proportionately.
  2767.      This brings parent back to correct size.
  2768.      Delta1 was calculated so this makes this window the desired size,
  2769.      taking it all out of the siblings.  */
  2770.       (*setsizefun) (parent, opht, 0);
  2771.     }
  2772.  
  2773.   XSETFASTINT (p->last_modified, 0);
  2774.   XSETFASTINT (p->last_overlay_modified, 0);
  2775. }
  2776. #undef MINSIZE
  2777. #undef CURBEG
  2778. #undef CURSIZE
  2779.  
  2780.  
  2781. /* Return number of lines of text (not counting mode line) in W.  */
  2782.  
  2783. int
  2784. window_internal_height (w)
  2785.      struct window *w;
  2786. {
  2787.   int ht = XFASTINT (w->height);
  2788.  
  2789.   if (MINI_WINDOW_P (w))
  2790.     return ht;
  2791.  
  2792.   if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
  2793.       || !NILP (w->next) || !NILP (w->prev)
  2794.       || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
  2795.     return ht - 1;
  2796.  
  2797.   return ht;
  2798. }
  2799.  
  2800.  
  2801. /* Return the number of columns in W.
  2802.    Don't count columns occupied by scroll bars or the vertical bar
  2803.    separating W from the sibling to its right.  */
  2804. int
  2805. window_internal_width (w)
  2806.      struct window *w;
  2807. {
  2808.   FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
  2809.   int width = XINT (w->width);
  2810.  
  2811.   /* Scroll bars occupy a few columns.  */
  2812.   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
  2813.     return width - FRAME_SCROLL_BAR_COLS (f);
  2814.  
  2815.   /* The column of `|' characters separating side-by-side windows
  2816.      occupies one column only.  */
  2817.   if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
  2818.     return width - 1;
  2819.  
  2820.   return width;
  2821. }
  2822.  
  2823.  
  2824. /* Scroll contents of window WINDOW up N lines.
  2825.    If WHOLE is nonzero, it means scroll N screenfuls instead.  */
  2826.  
  2827. static void
  2828. window_scroll (window, n, whole, noerror)
  2829.      Lisp_Object window;
  2830.      int n;
  2831.      int whole;
  2832.      int noerror;
  2833. {
  2834.   register struct window *w = XWINDOW (window);
  2835.   register int opoint = PT;
  2836.   register int opoint_byte = PT_BYTE;
  2837.   register int pos, pos_byte;
  2838.   register int ht = window_internal_height (w);
  2839.   register Lisp_Object tem;
  2840.   int lose;
  2841.   Lisp_Object bolp, nmoved;
  2842.   int startpos;
  2843.   struct position posit;
  2844.   int original_vpos;
  2845.  
  2846.   startpos = marker_position (w->start);
  2847.  
  2848.   posit = *compute_motion (startpos, 0, 0, 0,
  2849.                PT, ht, 0,
  2850.                window_internal_width (w), XINT (w->hscroll),
  2851.                0, w);
  2852.   original_vpos = posit.vpos;
  2853.  
  2854.   XSETFASTINT (tem, PT);
  2855.   tem = Fpos_visible_in_window_p (tem, window);
  2856.  
  2857.   if (NILP (tem))
  2858.     {
  2859.       Fvertical_motion (make_number (- (ht / 2)), window);
  2860.       startpos = PT;
  2861.     }
  2862.  
  2863.   SET_PT (startpos);
  2864.   lose = n < 0 && PT == BEGV;
  2865.   Fvertical_motion (make_number (n), window);
  2866.   pos = PT;
  2867.   pos_byte = PT_BYTE;
  2868.   bolp = Fbolp ();
  2869.   SET_PT_BOTH (opoint, opoint_byte);
  2870.  
  2871.   if (lose)
  2872.     {
  2873.       if (noerror)
  2874.     return;
  2875.       else
  2876.     Fsignal (Qbeginning_of_buffer, Qnil);
  2877.     }
  2878.  
  2879.   if (pos < ZV)
  2880.     {
  2881.       int this_scroll_margin = scroll_margin;
  2882.  
  2883.       /* Don't use a scroll margin that is negative or too large.  */
  2884.       if (this_scroll_margin < 0)
  2885.     this_scroll_margin = 0;
  2886.  
  2887.       if (XINT (w->height) < 4 * scroll_margin)
  2888.     this_scroll_margin = XINT (w->height) / 4;
  2889.  
  2890.       set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
  2891.       w->start_at_line_beg = bolp;
  2892.       w->update_mode_line = Qt;
  2893.       XSETFASTINT (w->last_modified, 0);
  2894.       XSETFASTINT (w->last_overlay_modified, 0);
  2895.       /* Set force_start so that redisplay_window will run
  2896.      the window-scroll-functions.  */
  2897.       w->force_start = Qt;
  2898.  
  2899.       if (whole && scroll_preserve_screen_position)
  2900.     {
  2901.       SET_PT_BOTH (pos, pos_byte);
  2902.       Fvertical_motion (make_number (original_vpos), window);
  2903.     }
  2904.       /* If we scrolled forward, put point enough lines down
  2905.      that it is outside the scroll margin.  */
  2906.       else if (n > 0)
  2907.     {
  2908.       int top_margin;
  2909.  
  2910.       if (this_scroll_margin > 0)
  2911.         {
  2912.           SET_PT_BOTH (pos, pos_byte);
  2913.           Fvertical_motion (make_number (this_scroll_margin), window);
  2914.           top_margin = PT;
  2915.         }
  2916.       else
  2917.         top_margin = pos;
  2918.  
  2919.       if (top_margin <= opoint)
  2920.         SET_PT_BOTH (opoint, opoint_byte);
  2921.       else if (scroll_preserve_screen_position)
  2922.         {
  2923.           SET_PT_BOTH (pos, pos_byte);
  2924.           Fvertical_motion (make_number (original_vpos), window);
  2925.         }
  2926.       else
  2927.         SET_PT (top_margin);
  2928.     }
  2929.       else if (n < 0)
  2930.     {
  2931.       int bottom_margin;
  2932.  
  2933.       /* If we scrolled backward, put point near the end of the window
  2934.          but not within the scroll margin.  */
  2935.       SET_PT_BOTH (pos, pos_byte);
  2936.       tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
  2937.       if (XFASTINT (tem) == ht - this_scroll_margin)
  2938.         bottom_margin = PT;
  2939.       else
  2940.         bottom_margin = PT + 1;
  2941.  
  2942.       if (bottom_margin > opoint)
  2943.         SET_PT_BOTH (opoint, opoint_byte);
  2944.       else
  2945.         {
  2946.           if (scroll_preserve_screen_position)
  2947.         {
  2948.           SET_PT_BOTH (pos, pos_byte);
  2949.           Fvertical_motion (make_number (original_vpos), window);
  2950.         }
  2951.           else
  2952.         Fvertical_motion (make_number (-1), window);
  2953.         }
  2954.     }
  2955.     }
  2956.   else
  2957.     {
  2958.       if (noerror)
  2959.     return;
  2960.       else
  2961.     Fsignal (Qend_of_buffer, Qnil);
  2962.     }
  2963. }
  2964.  
  2965. /* This is the guts of Fscroll_up and Fscroll_down.  */
  2966.  
  2967. static void
  2968. scroll_command (n, direction)
  2969.      register Lisp_Object n;
  2970.      int direction;
  2971. {
  2972.   register int defalt;
  2973.   int count = specpdl_ptr - specpdl;
  2974.  
  2975.   /* If selected window's buffer isn't current, make it current for the moment.
  2976.      But don't screw up if window_scroll gets an error.  */
  2977.   if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  2978.     {
  2979.       record_unwind_protect (save_excursion_restore, save_excursion_save ());
  2980.       Fset_buffer (XWINDOW (selected_window)->buffer);
  2981.     }
  2982.  
  2983.   defalt = (window_internal_height (XWINDOW (selected_window))
  2984.         - next_screen_context_lines);
  2985.   defalt = direction * (defalt < 1 ? 1 : defalt);
  2986.  
  2987.   if (NILP (n))
  2988.     window_scroll (selected_window, defalt, 1, 0);
  2989.   else if (EQ (n, Qminus))
  2990.     window_scroll (selected_window, - defalt, 1, 0);
  2991.   else
  2992.     {
  2993.       n = Fprefix_numeric_value (n);
  2994.       window_scroll (selected_window, XINT (n) * direction, 0, 0);
  2995.     }
  2996.  
  2997.   unbind_to (count, Qnil);
  2998. }
  2999.  
  3000. DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
  3001.   "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
  3002. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  3003. Negative ARG means scroll downward.\n\
  3004. If ARG is the atom `-', scroll downward by nearly full screen.\n\
  3005. When calling from a program, supply as argument a number, nil, or `-'.")
  3006.   (arg)
  3007.      Lisp_Object arg;
  3008. {
  3009.   scroll_command (arg, 1);
  3010.   return Qnil;
  3011. }
  3012.  
  3013. DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
  3014.   "Scroll text of current window down ARG lines; or near full screen if no ARG.\n\
  3015. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  3016. Negative ARG means scroll upward.\n\
  3017. If ARG is the atom `-', scroll upward by nearly full screen.\n\
  3018. When calling from a program, supply as argument a number, nil, or `-'.")
  3019.   (arg)
  3020.      Lisp_Object arg;
  3021. {
  3022.   scroll_command (arg, -1);
  3023.   return Qnil;
  3024. }
  3025.  
  3026. DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
  3027.   "Return the other window for \"other window scroll\" commands.\n\
  3028. If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
  3029. specifies the window.\n\
  3030. If `other-window-scroll-buffer' is non-nil, a window\n\
  3031. showing that buffer is used.")
  3032.   ()
  3033. {
  3034.   Lisp_Object window;
  3035.  
  3036.   if (MINI_WINDOW_P (XWINDOW (selected_window))
  3037.       && !NILP (Vminibuf_scroll_window))
  3038.     window = Vminibuf_scroll_window;
  3039.   /* If buffer is specified, scroll that buffer.  */
  3040.   else if (!NILP (Vother_window_scroll_buffer))
  3041.     {
  3042.       window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
  3043.       if (NILP (window))
  3044.     window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
  3045.     }
  3046.   else
  3047.     {
  3048.       /* Nothing specified; look for a neighboring window on the same
  3049.      frame.  */
  3050.       window = Fnext_window (selected_window, Qnil, Qnil);
  3051.  
  3052.       if (EQ (window, selected_window))
  3053.     /* That didn't get us anywhere; look for a window on another
  3054.            visible frame.  */
  3055.     do
  3056.       window = Fnext_window (window, Qnil, Qt);
  3057.     while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
  3058.            && ! EQ (window, selected_window));
  3059.     }
  3060.  
  3061.   CHECK_LIVE_WINDOW (window, 0);
  3062.  
  3063.   if (EQ (window, selected_window))
  3064.     error ("There is no other window");
  3065.  
  3066.   return window;
  3067. }
  3068.  
  3069. DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
  3070.   "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
  3071. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  3072. The next window is the one below the current one; or the one at the top\n\
  3073. if the current one is at the bottom.  Negative ARG means scroll downward.\n\
  3074. If ARG is the atom `-', scroll downward by nearly full screen.\n\
  3075. When calling from a program, supply as argument a number, nil, or `-'.\n\
  3076. \n\
  3077. If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
  3078. specifies the window to scroll.\n\
  3079. If `other-window-scroll-buffer' is non-nil, scroll the window\n\
  3080. showing that buffer, popping the buffer up if necessary.")
  3081.   (arg)
  3082.      register Lisp_Object arg;
  3083. {
  3084.   register Lisp_Object window;
  3085.   register int defalt;
  3086.   register struct window *w;
  3087.   register int count = specpdl_ptr - specpdl;
  3088.  
  3089.   window = Fother_window_for_scrolling ();
  3090.  
  3091.   w = XWINDOW (window);
  3092.   defalt = window_internal_height (w) - next_screen_context_lines;
  3093.   if (defalt < 1) defalt = 1;
  3094.  
  3095.   /* Don't screw up if window_scroll gets an error.  */
  3096.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  3097.  
  3098.   Fset_buffer (w->buffer);
  3099.   SET_PT (marker_position (w->pointm));
  3100.  
  3101.   if (NILP (arg))
  3102.     window_scroll (window, defalt, 1, 1);
  3103.   else if (EQ (arg, Qminus))
  3104.     window_scroll (window, -defalt, 1, 1);
  3105.   else
  3106.     {
  3107.       if (CONSP (arg))
  3108.     arg = Fcar (arg);
  3109.       CHECK_NUMBER (arg, 0);
  3110.       window_scroll (window, XINT (arg), 0, 1);
  3111.     }
  3112.  
  3113.   set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
  3114.   unbind_to (count, Qnil);
  3115.  
  3116.   return Qnil;
  3117. }
  3118.  
  3119. DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
  3120.   "Scroll selected window display ARG columns left.\n\
  3121. Default for ARG is window width minus 2.")
  3122.   (arg)
  3123.      register Lisp_Object arg;
  3124. {
  3125.  
  3126.   if (NILP (arg))
  3127.     XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
  3128.   else
  3129.     arg = Fprefix_numeric_value (arg);
  3130.  
  3131.   return
  3132.     Fset_window_hscroll (selected_window,
  3133.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  3134.                       + XINT (arg)));
  3135. }
  3136.  
  3137. DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
  3138.   "Scroll selected window display ARG columns right.\n\
  3139. Default for ARG is window width minus 2.")
  3140.   (arg)
  3141.      register Lisp_Object arg;
  3142. {
  3143.   if (NILP (arg))
  3144.     XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
  3145.   else
  3146.     arg = Fprefix_numeric_value (arg);
  3147.  
  3148.   return
  3149.     Fset_window_hscroll (selected_window,
  3150.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  3151.                       - XINT (arg)));
  3152. }
  3153.  
  3154. DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
  3155.   "Center point in window and redisplay frame.  With ARG, put point on line ARG.\n\
  3156. The desired position of point is always relative to the current window.\n\
  3157. Just C-u as prefix means put point in the center of the window.\n\
  3158. If ARG is omitted or nil, erases the entire frame and then\n\
  3159. redraws with point in the center of the current window.")
  3160.   (arg)
  3161.      register Lisp_Object arg;
  3162. {
  3163.   register struct window *w = XWINDOW (selected_window);
  3164.   register int ht = window_internal_height (w);
  3165.   struct position pos;
  3166.   struct buffer *buf = XBUFFER (w->buffer);
  3167.   struct buffer *obuf = current_buffer;
  3168.  
  3169.   if (NILP (arg))
  3170.     {
  3171.       extern int frame_garbaged;
  3172.  
  3173.       SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
  3174.       XSETFASTINT (arg, ht / 2);
  3175.     }
  3176.   else if (CONSP (arg)) /* Just C-u. */
  3177.     {
  3178.       XSETFASTINT (arg, ht / 2);
  3179.     }
  3180.   else
  3181.     {
  3182.       arg = Fprefix_numeric_value (arg);
  3183.       CHECK_NUMBER (arg, 0);
  3184.     }
  3185.  
  3186.   if (XINT (arg) < 0)
  3187.     XSETINT (arg, XINT (arg) + ht);
  3188.  
  3189.   set_buffer_internal (buf);
  3190.   pos = *vmotion (PT, - XINT (arg), w);
  3191.  
  3192.   set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
  3193.   w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
  3194.                || FETCH_BYTE (pos.bytepos - 1) == '\n')
  3195.               ? Qt : Qnil);
  3196.   w->force_start = Qt;
  3197.   set_buffer_internal (obuf);
  3198.  
  3199.   return Qnil;
  3200. }
  3201.  
  3202. DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
  3203.   1, 1, "P",
  3204.   "Position point relative to window.\n\
  3205. With no argument, position point at center of window.\n\
  3206. An argument specifies vertical position within the window;\n\
  3207. zero means top of window, negative means relative to bottom of window.")
  3208.   (arg)
  3209.      register Lisp_Object arg;
  3210. {
  3211.   register struct window *w = XWINDOW (selected_window);
  3212.   register int height = window_internal_height (w);
  3213.   register int start;
  3214.   Lisp_Object window;
  3215.  
  3216.   if (NILP (arg))
  3217.     XSETFASTINT (arg, height / 2);
  3218.   else
  3219.     {
  3220.       arg = Fprefix_numeric_value (arg);
  3221.       if (XINT (arg) < 0)
  3222.     XSETINT (arg, XINT (arg) + height);
  3223.     }
  3224.  
  3225.   start = marker_position (w->start);
  3226.   XSETWINDOW (window, w);
  3227.   if (start < BEGV || start > ZV)
  3228.     {
  3229.       Fvertical_motion (make_number (- (height / 2)), window);
  3230.       set_marker_both (w->start, w->buffer, PT, PT_BYTE);
  3231.       w->start_at_line_beg = Fbolp ();
  3232.       w->force_start = Qt;
  3233.     }
  3234.   else
  3235.     Fgoto_char (w->start);
  3236.  
  3237.   return Fvertical_motion (arg, window);
  3238. }
  3239.  
  3240. struct save_window_data
  3241.   {
  3242.     EMACS_INT size_from_Lisp_Vector_struct;
  3243.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  3244.     Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
  3245.     Lisp_Object selected_frame;
  3246.     Lisp_Object current_window;
  3247.     Lisp_Object current_buffer;
  3248.     Lisp_Object minibuf_scroll_window;
  3249.     Lisp_Object root_window;
  3250.     Lisp_Object focus_frame;
  3251.     /* Record the values of window-min-width and window-min-height
  3252.        so that window sizes remain consistent with them.  */
  3253.     Lisp_Object min_width, min_height;
  3254.     /* A vector, each of whose elements is a struct saved_window
  3255.        for one window.  */
  3256.     Lisp_Object saved_windows;
  3257.   };
  3258.  
  3259. /* This is saved as a Lisp_Vector  */
  3260. struct saved_window
  3261.   {
  3262.     /* these first two must agree with struct Lisp_Vector in lisp.h */
  3263.     EMACS_INT size_from_Lisp_Vector_struct;
  3264.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  3265.  
  3266.     Lisp_Object window;
  3267.     Lisp_Object buffer, start, pointm, mark;
  3268.     Lisp_Object left, top, width, height, hscroll;
  3269.     Lisp_Object parent, prev;
  3270.     Lisp_Object start_at_line_beg;
  3271.     Lisp_Object display_table;
  3272.   };
  3273. #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
  3274.  
  3275. #define SAVED_WINDOW_N(swv,n) \
  3276.   ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
  3277.  
  3278. DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
  3279.   "Return t if OBJECT is a window-configuration object.")
  3280.   (object)
  3281.      Lisp_Object object;
  3282. {
  3283.   if (WINDOW_CONFIGURATIONP (object))
  3284.     return Qt;
  3285.   return Qnil;
  3286. }
  3287.  
  3288. DEFUN ("set-window-configuration", Fset_window_configuration,
  3289.   Sset_window_configuration, 1, 1, 0,
  3290.   "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
  3291. CONFIGURATION must be a value previously returned\n\
  3292. by `current-window-configuration' (which see).")
  3293.      (configuration)
  3294.      Lisp_Object configuration;
  3295. {
  3296.   register struct save_window_data *data;
  3297.   struct Lisp_Vector *saved_windows;
  3298.   Lisp_Object new_current_buffer;
  3299.   Lisp_Object frame;
  3300.   FRAME_PTR f;
  3301.   int old_point = -1;
  3302.  
  3303.   while (!WINDOW_CONFIGURATIONP (configuration))
  3304.     {
  3305.       configuration = wrong_type_argument (intern ("window-configuration-p"),
  3306.                        configuration);
  3307.     }
  3308.  
  3309.   data = (struct save_window_data *) XVECTOR (configuration);
  3310.   saved_windows = XVECTOR (data->saved_windows);
  3311.  
  3312.   new_current_buffer = data->current_buffer;
  3313.   if (NILP (XBUFFER (new_current_buffer)->name))
  3314.     new_current_buffer = Qnil;
  3315.   else
  3316.     {
  3317.       if (XBUFFER (new_current_buffer) == current_buffer)
  3318.     old_point = PT;
  3319.  
  3320.     }
  3321.  
  3322.   frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
  3323.   f = XFRAME (frame);
  3324.  
  3325.   /* If f is a dead frame, don't bother rebuilding its window tree.
  3326.      However, there is other stuff we should still try to do below.  */
  3327.   if (FRAME_LIVE_P (f))
  3328.     {
  3329.       register struct window *w;
  3330.       register struct saved_window *p;
  3331.       int k;
  3332.  
  3333.       /* If the frame has been resized since this window configuration was
  3334.      made, we change the frame to the size specified in the
  3335.      configuration, restore the configuration, and then resize it
  3336.      back.  We keep track of the prevailing height in these variables.  */
  3337.       int previous_frame_height = FRAME_HEIGHT (f);
  3338.       int previous_frame_width =  FRAME_WIDTH  (f);
  3339.       int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
  3340.  
  3341.       /* The mouse highlighting code could get screwed up
  3342.      if it runs during this.  */
  3343.       BLOCK_INPUT;
  3344.  
  3345.       if (XFASTINT (data->frame_height) != previous_frame_height
  3346.       || XFASTINT (data->frame_width) != previous_frame_width)
  3347.     change_frame_size (f, XFASTINT (data->frame_height),
  3348.                XFASTINT (data->frame_width), 0, 0);
  3349. #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
  3350.       if (XFASTINT (data->frame_menu_bar_lines)
  3351.       != previous_frame_menu_bar_lines)
  3352.     x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
  3353. #endif
  3354.  
  3355.       if (! NILP (XWINDOW (selected_window)->buffer))
  3356.     {
  3357.       w = XWINDOW (selected_window);
  3358.       set_marker_both (w->pointm,
  3359.                w->buffer,
  3360.                BUF_PT (XBUFFER (w->buffer)),
  3361.                BUF_PT_BYTE (XBUFFER (w->buffer)));
  3362.     }
  3363.  
  3364.       windows_or_buffers_changed++;
  3365.       FRAME_WINDOW_SIZES_CHANGED (f) = 1;
  3366.  
  3367.       /* Temporarily avoid any problems with windows that are smaller
  3368.      than they are supposed to be.  */
  3369.       window_min_height = 1;
  3370.       window_min_width = 1;
  3371.  
  3372.       /* Kludge Alert!
  3373.      Mark all windows now on frame as "deleted".
  3374.      Restoring the new configuration "undeletes" any that are in it.
  3375.  
  3376.      Save their current buffers in their height fields, since we may
  3377.      need it later, if a buffer saved in the configuration is now
  3378.      dead.  */
  3379.       delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
  3380.  
  3381.       for (k = 0; k < saved_windows->size; k++)
  3382.     {
  3383.       p = SAVED_WINDOW_N (saved_windows, k);
  3384.       w = XWINDOW (p->window);
  3385.       w->next = Qnil;
  3386.  
  3387.       if (!NILP (p->parent))
  3388.         w->parent = SAVED_WINDOW_N (saved_windows,
  3389.                     XFASTINT (p->parent))->window;
  3390.       else
  3391.         w->parent = Qnil;
  3392.  
  3393.       if (!NILP (p->prev))
  3394.         {
  3395.           w->prev = SAVED_WINDOW_N (saved_windows,
  3396.                     XFASTINT (p->prev))->window;
  3397.           XWINDOW (w->prev)->next = p->window;
  3398.         }
  3399.       else
  3400.         {
  3401.           w->prev = Qnil;
  3402.           if (!NILP (w->parent))
  3403.         {
  3404.           if (EQ (p->width, XWINDOW (w->parent)->width))
  3405.             {
  3406.               XWINDOW (w->parent)->vchild = p->window;
  3407.               XWINDOW (w->parent)->hchild = Qnil;
  3408.             }
  3409.           else
  3410.             {
  3411.               XWINDOW (w->parent)->hchild = p->window;
  3412.               XWINDOW (w->parent)->vchild = Qnil;
  3413.             }
  3414.         }
  3415.         }
  3416.  
  3417.       /* If we squirreled away the buffer in the window's height,
  3418.          restore it now.  */
  3419.       if (BUFFERP (w->height))
  3420.         w->buffer = w->height;
  3421.       w->left = p->left;
  3422.       w->top = p->top;
  3423.       w->width = p->width;
  3424.       w->height = p->height;
  3425.       w->hscroll = p->hscroll;
  3426.       w->display_table = p->display_table;
  3427.       XSETFASTINT (w->last_modified, 0);
  3428.       XSETFASTINT (w->last_overlay_modified, 0);
  3429.  
  3430.       /* Reinstall the saved buffer and pointers into it.  */
  3431.       if (NILP (p->buffer))
  3432.         w->buffer = p->buffer;
  3433.       else
  3434.         {
  3435.           if (!NILP (XBUFFER (p->buffer)->name))
  3436.         /* If saved buffer is alive, install it.  */
  3437.         {
  3438.           w->buffer = p->buffer;
  3439.           w->start_at_line_beg = p->start_at_line_beg;
  3440.           set_marker_restricted (w->start, p->start, w->buffer);
  3441.           set_marker_restricted (w->pointm, p->pointm, w->buffer);
  3442.           Fset_marker (XBUFFER (w->buffer)->mark,
  3443.                    p->mark, w->buffer);
  3444.  
  3445.           /* As documented in Fcurrent_window_configuration, don't
  3446.              save the location of point in the buffer which was current
  3447.              when the window configuration was recorded.  */
  3448.           if (!EQ (p->buffer, new_current_buffer)
  3449.               && XBUFFER (p->buffer) == current_buffer)
  3450.             Fgoto_char (w->pointm);
  3451.         }
  3452.           else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
  3453.         /* Else unless window has a live buffer, get one.  */
  3454.         {
  3455.           w->buffer = Fcdr (Fcar (Vbuffer_alist));
  3456.           /* This will set the markers to beginning of visible
  3457.              range.  */
  3458.           set_marker_restricted (w->start, make_number (0), w->buffer);
  3459.           set_marker_restricted (w->pointm, make_number (0),w->buffer);
  3460.           w->start_at_line_beg = Qt;
  3461.         }
  3462.           else
  3463.         /* Keeping window's old buffer; make sure the markers
  3464.            are real.  */
  3465.         {
  3466.           /* Set window markers at start of visible range.  */
  3467.           if (XMARKER (w->start)->buffer == 0)
  3468.             set_marker_restricted (w->start, make_number (0),
  3469.                        w->buffer);
  3470.           if (XMARKER (w->pointm)->buffer == 0)
  3471.             set_marker_restricted_both (w->pointm, w->buffer,
  3472.                         BUF_PT (XBUFFER (w->buffer)),
  3473.                         BUF_PT_BYTE (XBUFFER (w->buffer)));
  3474.           w->start_at_line_beg = Qt;
  3475.         }
  3476.         }
  3477.     }
  3478.  
  3479.       FRAME_ROOT_WINDOW (f) = data->root_window;
  3480.       Fselect_window (data->current_window);
  3481.       XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
  3482.     = selected_window;
  3483.  
  3484.       if (NILP (data->focus_frame)
  3485.       || (FRAMEP (data->focus_frame)
  3486.           && FRAME_LIVE_P (XFRAME (data->focus_frame))))
  3487.     Fredirect_frame_focus (frame, data->focus_frame);
  3488.  
  3489. #if 0 /* I don't understand why this is needed, and it causes problems
  3490.          when the frame's old selected window has been deleted.  */
  3491.       if (f != selected_frame && FRAME_WINDOW_P (f))
  3492.     do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
  3493.              Qnil, 0);
  3494. #endif
  3495.  
  3496.       /* Set the screen height to the value it had before this function.  */
  3497.       if (previous_frame_height != FRAME_HEIGHT (f)
  3498.       || previous_frame_width != FRAME_WIDTH (f))
  3499.     change_frame_size (f, previous_frame_height, previous_frame_width,
  3500.                0, 0);
  3501. #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
  3502.       if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
  3503.     x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
  3504.                   make_number (0));
  3505. #endif
  3506.  
  3507.       UNBLOCK_INPUT;
  3508.  
  3509.       /* Fselect_window will have made f the selected frame, so we
  3510.      reselect the proper frame here.  Fhandle_switch_frame will change the
  3511.      selected window too, but that doesn't make the call to
  3512.      Fselect_window above totally superfluous; it still sets f's
  3513.      selected window.  */
  3514.       if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
  3515.     do_switch_frame (data->selected_frame, Qnil, 0);
  3516.  
  3517.       if (! NILP (Vwindow_configuration_change_hook)
  3518.       && ! NILP (Vrun_hooks))
  3519.     call1 (Vrun_hooks, Qwindow_configuration_change_hook);
  3520.     }
  3521.  
  3522.   if (!NILP (new_current_buffer))
  3523.     {
  3524.       Fset_buffer (new_current_buffer);
  3525.  
  3526.       /* If the buffer that is current now is the same
  3527.      that was current before setting the window configuration,
  3528.      don't alter its PT.  */
  3529.       if (old_point >= 0)
  3530.     SET_PT (old_point);
  3531.     }
  3532.  
  3533.   /* Restore the minimum heights recorded in the configuration.  */
  3534.   window_min_height = XINT (data->min_height);
  3535.   window_min_width = XINT (data->min_width);
  3536.  
  3537.   Vminibuf_scroll_window = data->minibuf_scroll_window;
  3538.  
  3539.   return Qnil;
  3540. }
  3541.  
  3542. /* Mark all windows now on frame as deleted
  3543.    by setting their buffers to nil.  */
  3544.  
  3545. void
  3546. delete_all_subwindows (w)
  3547.      register struct window *w;
  3548. {
  3549.   if (!NILP (w->next))
  3550.     delete_all_subwindows (XWINDOW (w->next));
  3551.   if (!NILP (w->vchild))
  3552.     delete_all_subwindows (XWINDOW (w->vchild));
  3553.   if (!NILP (w->hchild))
  3554.     delete_all_subwindows (XWINDOW (w->hchild));
  3555.  
  3556.   w->height = w->buffer;       /* See Fset_window_configuration for excuse.  */
  3557.  
  3558.   if (!NILP (w->buffer))
  3559.     unshow_buffer (w);
  3560.  
  3561.   /* We set all three of these fields to nil, to make sure that we can
  3562.      distinguish this dead window from any live window.  Live leaf
  3563.      windows will have buffer set, and combination windows will have
  3564.      vchild or hchild set.  */
  3565.   w->buffer = Qnil;
  3566.   w->vchild = Qnil;
  3567.   w->hchild = Qnil;
  3568. }
  3569.  
  3570. static int
  3571. count_windows (window)
  3572.      register struct window *window;
  3573. {
  3574.   register int count = 1;
  3575.   if (!NILP (window->next))
  3576.     count += count_windows (XWINDOW (window->next));
  3577.   if (!NILP (window->vchild))
  3578.     count += count_windows (XWINDOW (window->vchild));
  3579.   if (!NILP (window->hchild))
  3580.     count += count_windows (XWINDOW (window->hchild));
  3581.   return count;
  3582. }
  3583.  
  3584. static int
  3585. save_window_save (window, vector, i)
  3586.      Lisp_Object window;
  3587.      struct Lisp_Vector *vector;
  3588.      int i;
  3589. {
  3590.   register struct saved_window *p;
  3591.   register struct window *w;
  3592.   register Lisp_Object tem;
  3593.  
  3594.   for (;!NILP (window); window = w->next)
  3595.     {
  3596.       p = SAVED_WINDOW_N (vector, i);
  3597.       w = XWINDOW (window);
  3598.  
  3599.       XSETFASTINT (w->temslot, i++);
  3600.       p->window = window;
  3601.       p->buffer = w->buffer;
  3602.       p->left = w->left;
  3603.       p->top = w->top;
  3604.       p->width = w->width;
  3605.       p->height = w->height;
  3606.       p->hscroll = w->hscroll;
  3607.       p->display_table = w->display_table;
  3608.       if (!NILP (w->buffer))
  3609.     {
  3610.       /* Save w's value of point in the window configuration.
  3611.          If w is the selected window, then get the value of point
  3612.          from the buffer; pointm is garbage in the selected window.  */
  3613.       if (EQ (window, selected_window))
  3614.         {
  3615.           p->pointm = Fmake_marker ();
  3616.           set_marker_both (p->pointm, w->buffer,
  3617.                    BUF_PT (XBUFFER (w->buffer)),
  3618.                    BUF_PT_BYTE (XBUFFER (w->buffer)));
  3619.         }
  3620.       else
  3621.         p->pointm = Fcopy_marker (w->pointm, Qnil);
  3622.  
  3623.       p->start = Fcopy_marker (w->start, Qnil);
  3624.       p->start_at_line_beg = w->start_at_line_beg;
  3625.  
  3626.       tem = XBUFFER (w->buffer)->mark;
  3627.       p->mark = Fcopy_marker (tem, Qnil);
  3628.     }
  3629.       else
  3630.     {
  3631.       p->pointm = Qnil;
  3632.       p->start = Qnil;
  3633.       p->mark = Qnil;
  3634.       p->start_at_line_beg = Qnil;
  3635.     }
  3636.  
  3637.       if (NILP (w->parent))
  3638.     p->parent = Qnil;
  3639.       else
  3640.     p->parent = XWINDOW (w->parent)->temslot;
  3641.  
  3642.       if (NILP (w->prev))
  3643.     p->prev = Qnil;
  3644.       else
  3645.     p->prev = XWINDOW (w->prev)->temslot;
  3646.  
  3647.       if (!NILP (w->vchild))
  3648.     i = save_window_save (w->vchild, vector, i);
  3649.       if (!NILP (w->hchild))
  3650.     i = save_window_save (w->hchild, vector, i);
  3651.     }
  3652.  
  3653.   return i;
  3654. }
  3655.  
  3656. DEFUN ("current-window-configuration", Fcurrent_window_configuration,
  3657.   Scurrent_window_configuration, 0, 1, 0,
  3658.   "Return an object representing the current window configuration of FRAME.\n\
  3659. If FRAME is nil or omitted, use the selected frame.\n\
  3660. This describes the number of windows, their sizes and current buffers,\n\
  3661. and for each displayed buffer, where display starts, and the positions of\n\
  3662. point and mark.  An exception is made for point in the current buffer:\n\
  3663. its value is -not- saved.\n\
  3664. This also records the currently selected frame, and FRAME's focus\n\
  3665. redirection (see `redirect-frame-focus').")
  3666.   (frame)
  3667.      Lisp_Object frame;
  3668. {
  3669.   register Lisp_Object tem;
  3670.   register int n_windows;
  3671.   register struct save_window_data *data;
  3672.   register struct Lisp_Vector *vec;
  3673.   register int i;
  3674.   FRAME_PTR f;
  3675.  
  3676.   if (NILP (frame))
  3677.     f = selected_frame;
  3678.   else
  3679.     {
  3680.       CHECK_LIVE_FRAME (frame, 0);
  3681.       f = XFRAME (frame);
  3682.     }
  3683.  
  3684.   n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
  3685.   vec = allocate_vectorlike (VECSIZE (struct save_window_data));
  3686.   for (i = 0; i < VECSIZE (struct save_window_data); i++)
  3687.     vec->contents[i] = Qnil;
  3688.   vec->size = VECSIZE (struct save_window_data);
  3689.   data = (struct save_window_data *)vec;
  3690.  
  3691.   XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
  3692.   XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
  3693.   XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
  3694.   XSETFRAME (data->selected_frame, selected_frame);
  3695.   data->current_window = FRAME_SELECTED_WINDOW (f);
  3696.   XSETBUFFER (data->current_buffer, current_buffer);
  3697.   data->minibuf_scroll_window = Vminibuf_scroll_window;
  3698.   data->root_window = FRAME_ROOT_WINDOW (f);
  3699.   data->focus_frame = FRAME_FOCUS_FRAME (f);
  3700.   XSETINT (data->min_height, window_min_height);
  3701.   XSETINT (data->min_width, window_min_width);
  3702.   tem = Fmake_vector (make_number (n_windows), Qnil);
  3703.   data->saved_windows = tem;
  3704.   for (i = 0; i < n_windows; i++)
  3705.     XVECTOR (tem)->contents[i]
  3706.       = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
  3707.   save_window_save (FRAME_ROOT_WINDOW (f),
  3708.             XVECTOR (tem), 0);
  3709.   XSETWINDOW_CONFIGURATION (tem, data);
  3710.   return (tem);
  3711. }
  3712.  
  3713. DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
  3714.   0, UNEVALLED, 0,
  3715.   "Execute body, preserving window sizes and contents.\n\
  3716. Restore which buffer appears in which window, where display starts,\n\
  3717. and the value of point and mark for each window.\n\
  3718. Also restore which buffer is current.\n\
  3719. But do not preserve point in the current buffer.\n\
  3720. Does not restore the value of point in current buffer.")
  3721.   (args)
  3722.      Lisp_Object args;
  3723. {
  3724.   register Lisp_Object val;
  3725.   register int count = specpdl_ptr - specpdl;
  3726.  
  3727.   record_unwind_protect (Fset_window_configuration,
  3728.              Fcurrent_window_configuration (Qnil));
  3729.   val = Fprogn (args);
  3730.   return unbind_to (count, val);
  3731. }
  3732.  
  3733. /* Return 1 if window configurations C1 and C2
  3734.    describe the same state of affairs.  This is used by Fequal.   */
  3735.  
  3736. int
  3737. compare_window_configurations (c1, c2, ignore_positions)
  3738.      Lisp_Object c1, c2;
  3739.      int ignore_positions;
  3740. {
  3741.   register struct save_window_data *d1, *d2;
  3742.   struct Lisp_Vector *sw1, *sw2;
  3743.   int i;
  3744.  
  3745.   d1 = (struct save_window_data *) XVECTOR (c1);
  3746.   d2 = (struct save_window_data *) XVECTOR (c2);
  3747.   sw1 = XVECTOR (d1->saved_windows);
  3748.   sw2 = XVECTOR (d2->saved_windows);
  3749.  
  3750.   if (! EQ (d1->frame_width, d2->frame_width))
  3751.     return 0;
  3752.   if (! EQ (d1->frame_height, d2->frame_height))
  3753.     return 0;
  3754.   if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
  3755.     return 0;
  3756.   if (! EQ (d1->selected_frame, d2->selected_frame))
  3757.     return 0;
  3758.   /* Don't compare the current_window field directly.
  3759.      Instead see w1_is_current and w2_is_current, below.  */
  3760.   if (! EQ (d1->current_buffer, d2->current_buffer))
  3761.     return 0;
  3762.   if (! ignore_positions)
  3763.     if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
  3764.       return 0;
  3765.   /* Don't compare the root_window field.
  3766.      We don't require the two configurations
  3767.      to use the same window object,
  3768.      and the two root windows must be equivalent
  3769.      if everything else compares equal.  */
  3770.   if (! EQ (d1->focus_frame, d2->focus_frame))
  3771.     return 0;
  3772.   if (! EQ (d1->min_width, d2->min_width))
  3773.     return 0;
  3774.   if (! EQ (d1->min_height, d2->min_height))
  3775.     return 0;
  3776.  
  3777.   /* Verify that the two confis have the same number of windows.  */
  3778.   if (sw1->size != sw2->size)
  3779.     return 0;
  3780.  
  3781.   for (i = 0; i < sw1->size; i++)
  3782.     {
  3783.       struct saved_window *p1, *p2;
  3784.       int w1_is_current, w2_is_current;
  3785.  
  3786.       p1 = SAVED_WINDOW_N (sw1, i);
  3787.       p2 = SAVED_WINDOW_N (sw2, i);
  3788.  
  3789.       /* Verify that the current windows in the two
  3790.      configurations correspond to each other.  */
  3791.       w1_is_current = EQ (d1->current_window, p1->window);
  3792.       w2_is_current = EQ (d2->current_window, p2->window);
  3793.  
  3794.       if (w1_is_current != w2_is_current)
  3795.     return 0;
  3796.  
  3797.       /* Verify that the corresponding windows do match.  */
  3798.       if (! EQ (p1->buffer, p2->buffer))
  3799.     return 0;
  3800.       if (! EQ (p1->left, p2->left))
  3801.     return 0;
  3802.       if (! EQ (p1->top, p2->top))
  3803.     return 0;
  3804.       if (! EQ (p1->width, p2->width))
  3805.     return 0;
  3806.       if (! EQ (p1->height, p2->height))
  3807.     return 0;
  3808.       if (! EQ (p1->display_table, p2->display_table))
  3809.     return 0;
  3810.       if (! EQ (p1->parent, p2->parent))
  3811.     return 0;
  3812.       if (! EQ (p1->prev, p2->prev))
  3813.     return 0;
  3814.       if (! ignore_positions)
  3815.     {
  3816.       if (! EQ (p1->hscroll, p2->hscroll))
  3817.         return 0;
  3818.       if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
  3819.         return 0;
  3820.       if (NILP (Fequal (p1->start, p2->start)))
  3821.         return 0;
  3822.       if (NILP (Fequal (p1->pointm, p2->pointm)))
  3823.         return 0;
  3824.       if (NILP (Fequal (p1->mark, p2->mark)))
  3825.         return 0;
  3826.     }
  3827.     }
  3828.  
  3829.   return 1;
  3830. }
  3831.  
  3832. DEFUN ("compare-window-configurations", Fcompare_window_configurations,
  3833.        Scompare_window_configurations, 2, 2, 0,
  3834.   "Compare two window configurations as regards the structure of windows.\n\
  3835. This function ignores details such as the values of point and mark\n\
  3836. and scrolling positions.")
  3837.   (x, y)
  3838.      Lisp_Object x, y;
  3839. {
  3840.   if (compare_window_configurations (x, y, 1))
  3841.     return Qt;
  3842.   return Qnil;
  3843. }
  3844.  
  3845. void
  3846. init_window_once ()
  3847. {
  3848.   selected_frame = make_terminal_frame ();
  3849.   XSETFRAME (Vterminal_frame, selected_frame);
  3850.   minibuf_window = selected_frame->minibuffer_window;
  3851.   selected_window = selected_frame->selected_window;
  3852.   last_nonminibuf_frame = selected_frame;
  3853.  
  3854.   window_initialized = 1;
  3855. }
  3856.  
  3857. void
  3858. syms_of_window ()
  3859. {
  3860.   staticpro (&Qwindow_configuration_change_hook);
  3861.   Qwindow_configuration_change_hook
  3862.     = intern ("window-configuration-change-hook");
  3863.  
  3864.   Qwindowp = intern ("windowp");
  3865.   staticpro (&Qwindowp);
  3866.  
  3867.   Qwindow_live_p = intern ("window-live-p");
  3868.   staticpro (&Qwindow_live_p);
  3869.  
  3870.   Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
  3871.   staticpro (&Qtemp_buffer_show_hook);
  3872.  
  3873.   DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
  3874.     "Non-nil means call as function to display a help buffer.\n\
  3875. The function is called with one argument, the buffer to be displayed.\n\
  3876. Used by `with-output-to-temp-buffer'.\n\
  3877. If this function is used, then it must do the entire job of showing\n\
  3878. the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
  3879.   Vtemp_buffer_show_function = Qnil;
  3880.  
  3881.   DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
  3882.     "If non-nil, function to call to handle `display-buffer'.\n\
  3883. It will receive two args, the buffer and a flag which if non-nil means\n\
  3884.  that the currently selected window is not acceptable.\n\
  3885. Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
  3886. work using this function.");
  3887.   Vdisplay_buffer_function = Qnil;
  3888.  
  3889.   DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
  3890.     "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
  3891.   Vminibuf_scroll_window = Qnil;
  3892.  
  3893.   DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
  3894.     "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
  3895.   Vother_window_scroll_buffer = Qnil;
  3896.  
  3897.   DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
  3898.     "*Non-nil means `display-buffer' should make a separate frame.");
  3899.   pop_up_frames = 0;
  3900.  
  3901.   DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
  3902.     "Function to call to handle automatic new frame creation.\n\
  3903. It is called with no arguments and should return a newly created frame.\n\
  3904. \n\
  3905. A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
  3906. where `pop-up-frame-alist' would hold the default frame parameters.");
  3907.   Vpop_up_frame_function = Qnil;
  3908.  
  3909.   DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
  3910.     "*List of buffer names that should have their own special frames.\n\
  3911. Displaying a buffer whose name is in this list makes a special frame for it\n\
  3912. using `special-display-function'.  See also `special-display-regexps'.\n\
  3913. \n\
  3914. An element of the list can be a list instead of just a string.\n\
  3915. There are two ways to use a list as an element:\n\
  3916.   (BUFFER FRAME-PARAMETERS...)   (BUFFER FUNCTION OTHER-ARGS...)\n\
  3917. In the first case, FRAME-PARAMETERS are used to create the frame.\n\
  3918. In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
  3919. followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
  3920. All this is done by the function found in `special-display-function'.\n\
  3921. \n\
  3922. If this variable appears \"not to work\", because you add a name to it\n\
  3923. but that buffer still appears in the selected window, look at the\n\
  3924. values of `same-window-buffer-names' and `same-window-regexps'.\n\
  3925. Those variables take precedence over this one.");
  3926.   Vspecial_display_buffer_names = Qnil;
  3927.  
  3928.   DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
  3929.     "*List of regexps saying which buffers should have their own special frames.\n\
  3930. If a buffer name matches one of these regexps, it gets its own frame.\n\
  3931. Displaying a buffer whose name is in this list makes a special frame for it\n\
  3932. using `special-display-function'.\n\
  3933. \n\
  3934. An element of the list can be a list instead of just a string.\n\
  3935. There are two ways to use a list as an element:\n\
  3936.   (REGEXP FRAME-PARAMETERS...)   (REGEXP FUNCTION OTHER-ARGS...)\n\
  3937. In the first case, FRAME-PARAMETERS are used to create the frame.\n\
  3938. In the latter case, FUNCTION is called with the buffer as first argument,\n\
  3939. followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
  3940. All this is done by the function found in `special-display-function'.\n\
  3941. \n\
  3942. If this variable appears \"not to work\", because you add a regexp to it\n\
  3943. but the matching buffers still appear in the selected window, look at the\n\
  3944. values of `same-window-buffer-names' and `same-window-regexps'.\n\
  3945. Those variables take precedence over this one.");
  3946.   Vspecial_display_regexps = Qnil;
  3947.  
  3948.   DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
  3949.     "Function to call to make a new frame for a special buffer.\n\
  3950. It is called with two arguments, the buffer and optional buffer specific\n\
  3951. data, and should return a window displaying that buffer.\n\
  3952. The default value makes a separate frame for the buffer,\n\
  3953. using `special-display-frame-alist' to specify the frame parameters.\n\
  3954. \n\
  3955. A buffer is special if its is listed in `special-display-buffer-names'\n\
  3956. or matches a regexp in `special-display-regexps'.");
  3957.   Vspecial_display_function = Qnil;
  3958.  
  3959.   DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
  3960.     "*List of buffer names that should appear in the selected window.\n\
  3961. Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
  3962. switches to it in the selected window, rather than making it appear\n\
  3963. in some other window.\n\
  3964. \n\
  3965. An element of the list can be a cons cell instead of just a string.\n\
  3966. Then the car must be a string, which specifies the buffer name.\n\
  3967. This is for compatibility with `special-display-buffer-names';\n\
  3968. the cdr of the cons cell is ignored.\n\
  3969. \n\
  3970. See also `same-window-regexps'.");
  3971.   Vsame_window_buffer_names = Qnil;
  3972.  
  3973.   DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
  3974.     "*List of regexps saying which buffers should appear in the selected window.\n\
  3975. If a buffer name matches one of these regexps, then displaying it\n\
  3976. using `display-buffer' or `pop-to-buffer' switches to it\n\
  3977. in the selected window, rather than making it appear in some other window.\n\
  3978. \n\
  3979. An element of the list can be a cons cell instead of just a string.\n\
  3980. Then the car must be a string, which specifies the buffer name.\n\
  3981. This is for compatibility with `special-display-buffer-names';\n\
  3982. the cdr of the cons cell is ignored.\n\
  3983. \n\
  3984. See also `same-window-buffer-names'.");
  3985.   Vsame_window_regexps = Qnil;
  3986.  
  3987.   DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
  3988.     "*Non-nil means display-buffer should make new windows.");
  3989.   pop_up_windows = 1;
  3990.  
  3991.   DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
  3992.     "*Number of lines of continuity when scrolling by screenfuls.");
  3993.   next_screen_context_lines = 2;
  3994.  
  3995.   DEFVAR_INT ("split-height-threshold", &split_height_threshold,
  3996.     "*display-buffer would prefer to split the largest window if this large.\n\
  3997. If there is only one window, it is split regardless of this value.");
  3998.   split_height_threshold = 500;
  3999.  
  4000.   DEFVAR_INT ("window-min-height", &window_min_height,
  4001.     "*Delete any window less than this tall (including its mode line).");
  4002.   window_min_height = 4;
  4003.  
  4004.   DEFVAR_INT ("window-min-width", &window_min_width,
  4005.     "*Delete any window less than this wide.");
  4006.   window_min_width = 10;
  4007.  
  4008.   DEFVAR_BOOL ("scroll-preserve-screen-position",
  4009.            &scroll_preserve_screen_position,
  4010.     "*Nonzero means scroll commands move point to keep its screen line unchanged.");
  4011.   scroll_preserve_screen_position = 0;
  4012.  
  4013.   DEFVAR_LISP ("window-configuration-change-hook",
  4014.            &Vwindow_configuration_change_hook,
  4015.     "Functions to call when window configuration changes.\n\
  4016. The selected frame is the one whose configuration has changed.");
  4017.   Vwindow_configuration_change_hook = Qnil;
  4018.  
  4019.   defsubr (&Sselected_window);
  4020.   defsubr (&Sminibuffer_window);
  4021.   defsubr (&Swindow_minibuffer_p);
  4022.   defsubr (&Swindowp);
  4023.   defsubr (&Swindow_live_p);
  4024.   defsubr (&Spos_visible_in_window_p);
  4025.   defsubr (&Swindow_buffer);
  4026.   defsubr (&Swindow_height);
  4027.   defsubr (&Swindow_width);
  4028.   defsubr (&Swindow_hscroll);
  4029.   defsubr (&Sset_window_hscroll);
  4030.   defsubr (&Swindow_redisplay_end_trigger);
  4031.   defsubr (&Sset_window_redisplay_end_trigger);
  4032.   defsubr (&Swindow_edges);
  4033.   defsubr (&Scoordinates_in_window_p);
  4034.   defsubr (&Swindow_at);
  4035.   defsubr (&Swindow_point);
  4036.   defsubr (&Swindow_start);
  4037.   defsubr (&Swindow_end);
  4038.   defsubr (&Sset_window_point);
  4039.   defsubr (&Sset_window_start);
  4040.   defsubr (&Swindow_dedicated_p);
  4041.   defsubr (&Sset_window_dedicated_p);
  4042.   defsubr (&Swindow_display_table);
  4043.   defsubr (&Sset_window_display_table);
  4044.   defsubr (&Snext_window);
  4045.   defsubr (&Sprevious_window);
  4046.   defsubr (&Sother_window);
  4047.   defsubr (&Sget_lru_window);
  4048.   defsubr (&Sget_largest_window);
  4049.   defsubr (&Sget_buffer_window);
  4050.   defsubr (&Sdelete_other_windows);
  4051.   defsubr (&Sdelete_windows_on);
  4052.   defsubr (&Sreplace_buffer_in_windows);
  4053.   defsubr (&Sdelete_window);
  4054.   defsubr (&Sset_window_buffer);
  4055.   defsubr (&Sselect_window);
  4056.   defsubr (&Sspecial_display_p);
  4057.   defsubr (&Ssame_window_p);
  4058.   defsubr (&Sdisplay_buffer);
  4059.   defsubr (&Ssplit_window);
  4060.   defsubr (&Senlarge_window);
  4061.   defsubr (&Sshrink_window);
  4062.   defsubr (&Sscroll_up);
  4063.   defsubr (&Sscroll_down);
  4064.   defsubr (&Sscroll_left);
  4065.   defsubr (&Sscroll_right);
  4066.   defsubr (&Sother_window_for_scrolling);
  4067.   defsubr (&Sscroll_other_window);
  4068.   defsubr (&Srecenter);
  4069.   defsubr (&Smove_to_window_line);
  4070.   defsubr (&Swindow_configuration_p);
  4071.   defsubr (&Sset_window_configuration);
  4072.   defsubr (&Scurrent_window_configuration);
  4073.   defsubr (&Ssave_window_excursion);
  4074.   defsubr (&Scompare_window_configurations);
  4075. }
  4076.  
  4077. void
  4078. keys_of_window ()
  4079. {
  4080.   initial_define_key (control_x_map, '1', "delete-other-windows");
  4081.   initial_define_key (control_x_map, '2', "split-window");
  4082.   initial_define_key (control_x_map, '0', "delete-window");
  4083.   initial_define_key (control_x_map, 'o', "other-window");
  4084.   initial_define_key (control_x_map, '^', "enlarge-window");
  4085.   initial_define_key (control_x_map, '<', "scroll-left");
  4086.   initial_define_key (control_x_map, '>', "scroll-right");
  4087.  
  4088.   initial_define_key (global_map, Ctl ('V'), "scroll-up");
  4089.   initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
  4090.   initial_define_key (meta_map, 'v', "scroll-down");
  4091.  
  4092.   initial_define_key (global_map, Ctl('L'), "recenter");
  4093.   initial_define_key (meta_map, 'r', "move-to-window-line");
  4094. }
  4095.