home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-29  |  78.8 KB  |  2,796 lines

  1. /* Window creation, deletion and examination for GNU Emacs.
  2.    Does not include redisplay.
  3.    Copyright (C) 1985-1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "lisp.h"
  24. #include "buffer.h"
  25. #include "screen.h"
  26. #include "window.h"
  27. #include "commands.h"
  28. #include "indent.h"
  29. #include "termchar.h"
  30. #include "disptab.h"
  31.  
  32. Lisp_Object Qwindowp;
  33.  
  34. static void delete_all_subwindows ();
  35. static struct window *decode_window();
  36.  
  37. #ifdef ENERGIZE
  38. extern void energize_buffer_shown_hook ();
  39. extern void energize_buffer_hidden_hook ();
  40. extern void energize_window_selected_hook ();
  41. extern void energize_window_deselected_hook ();
  42. #endif
  43.  
  44. extern int zmacs_region_stays;
  45.  
  46.  
  47. /* This is the window in which the terminal's cursor should
  48.    be left when nothing is being done with it.  This must
  49.    always be a leaf window, and its buffer is selected by
  50.    the top level editing loop at the end of each command.
  51.  
  52.    This value is always the same as
  53.     SCREEN_SELECTED_WINDOW (selected_screen).  */
  54.  
  55. Lisp_Object selected_window;
  56.  
  57. #ifndef MULTI_SCREEN
  58.  
  59. /* The root window for the screen.
  60.    This is accessed via SCREEN_ROOT_WINDOW (selected_screen).  */
  61. Lisp_Object root_window;
  62.  
  63. #endif
  64.  
  65. /* The minibuffer window of the selected screen.
  66.    Note that you cannot test for minibufferness of an arbitrary window
  67.    by comparing against this; but you can test for minibufferness of
  68.    the selected window.  */
  69. Lisp_Object minibuf_window;
  70.  
  71. /* Non-nil means it is the window for C-M-v to scroll
  72.    when the minibuffer is selected.  */
  73. Lisp_Object Vminibuf_scroll_window;
  74.  
  75. /* Non-nil means this is the buffer whose window C-M-v should scroll.  */
  76. Lisp_Object Vother_window_scroll_buffer;
  77.  
  78. /* Non-nil means it's function to call to display temp buffers.  */
  79. Lisp_Object Vtemp_buffer_show_function;
  80.  
  81. /* If a window gets smaller than either of these, it is removed. */
  82. int window_min_height;
  83. int window_min_width;
  84.  
  85. /* Nonzero implies Fdisplay_buffer should create windows. */
  86. int pop_up_windows;
  87.  
  88. /* Function to call *instead of* Fdisplay_buffer.  */
  89. Lisp_Object Vdisplay_buffer_function;
  90.  
  91. /* Function to call as the first thing that Fdisplay_buffer does (can be
  92.    used to select an appropriate screen, for example.)  */
  93. Lisp_Object Vpre_display_buffer_function;
  94.  
  95. /* Fdisplay_buffer always splits the largest window 
  96.    if that window is more than this high.  */
  97. int split_height_threshold;
  98.  
  99. /* Number of lines of continuity in scrolling by screenfuls.  */
  100. int next_screen_context_lines;
  101.  
  102. /* Incremented for each window created.  */
  103. static int sequence_number;
  104.  
  105. DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
  106.   "Returns t if OBJ is a window.")
  107.   (obj)
  108.      Lisp_Object obj;
  109. {
  110.   return WINDOWP (obj) ? Qt : Qnil;
  111. }
  112.  
  113. Lisp_Object
  114. make_window ()
  115. {
  116.   register Lisp_Object val;
  117.   register struct window *p;
  118.  
  119.   /* Add sizeof (Lisp_Object) here because sizeof (struct Lisp_Vector)
  120.      includes the first element.  */
  121.   val = Fmake_vector (
  122.     make_number ((sizeof (struct window) - sizeof (struct Lisp_Vector)
  123.           + sizeof (Lisp_Object))
  124.          / sizeof (Lisp_Object)),
  125.     Qnil);
  126.   XSETTYPE (val, Lisp_Window);
  127.   p = XWINDOW (val);
  128.   XFASTINT (p->sequence_number) = ++sequence_number;
  129.   XFASTINT (p->left) = XFASTINT (p->top)
  130.     = XFASTINT (p->height) = XFASTINT (p->width)
  131.       = XFASTINT (p->hscroll) = 0;
  132.   XFASTINT (p->last_point_x) = XFASTINT (p->last_point_y) = 0;
  133.   p->start = Fmake_marker ();
  134.   p->pointm = Fmake_marker ();
  135.   XFASTINT (p->use_time) = 0;
  136.   p->screen = Qnil;
  137.   p->display_table = Qnil;
  138.   p->dedicated = Qnil;
  139.   return val;
  140. }
  141.  
  142. DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
  143.   "Return the window that the cursor now appears in and commands apply to.")
  144.   ()
  145. {
  146.   return selected_window;
  147. }
  148.  
  149. DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 0, 0,
  150.   "Return the window used now for minibuffers.")
  151.   ()
  152. {
  153. #ifdef MULTI_SCREEN
  154.   if (minibuf_level == 0
  155.       && !EQ (minibuf_window, selected_screen->minibuffer_window)
  156.       && !EQ (Qnil, selected_screen->minibuffer_window))
  157.     {
  158.       Fset_window_buffer (selected_screen->minibuffer_window,
  159.               XWINDOW (minibuf_window)->buffer);
  160.       minibuf_window = selected_screen->minibuffer_window;
  161.     }
  162.  
  163.   if (SCREENP (Vglobal_minibuffer_screen))
  164.     minibuf_window = XSCREEN (Vglobal_minibuffer_screen)->minibuffer_window;
  165.   else
  166.     minibuf_window = selected_screen->minibuffer_window;
  167.  
  168. #endif /* MULTI_SCREEN */
  169.   return minibuf_window;
  170. }
  171.  
  172. DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 1, 1, 0,
  173.   "Returns non-nil if WINDOW is a minibuffer window.")
  174.   (window)
  175.      Lisp_Object window;
  176. {
  177.   struct window *w = decode_window (window);
  178.   return (MINI_WINDOW_P (w) ? Qt : Qnil);
  179. }
  180.  
  181. DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
  182.   Spos_visible_in_window_p, 0, 2, 0,
  183.   "Return t if position POS is currently on the screen in WINDOW.\n\
  184. Returns nil if that position is scrolled vertically out of view.\n\
  185. POS defaults to point; WINDOW, to the selected window.")
  186.   (pos, window)
  187.      Lisp_Object pos, window;
  188. {
  189.   register struct window *w;
  190.   register int top;
  191.   register int height;
  192.   register int posint;
  193.   register struct buffer *buf;
  194.   struct position posval;
  195.  
  196.   if (NILP (pos))
  197.     posint = point;
  198.   else
  199.     {
  200.       CHECK_FIXNUM_COERCE_MARKER (pos, 0);
  201.       posint = XINT (pos);
  202.     }
  203.  
  204.   if (NILP (window))
  205.     window = selected_window;
  206.   else
  207.     CHECK_WINDOW (window, 1);
  208.   w = XWINDOW (window);
  209.   top = marker_position (w->start);
  210.  
  211.   if (posint < top)
  212.     return Qnil;
  213.  
  214.   height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
  215.  
  216.   buf = XBUFFER (w->buffer);
  217.   if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
  218.       && XFASTINT (w->last_facechange) >= BUF_FACECHANGE (buf))
  219.     {
  220.       /* If screen is up to date,
  221.      use the info recorded about how much text fit on it. */
  222.       if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
  223.       || (XFASTINT (w->window_end_vpos) < height))
  224.     return Qt;
  225.       return Qnil;
  226.     }
  227.   else
  228.     {
  229.       if (posint > BUF_Z (buf))
  230.     return Qnil;
  231.  
  232.       /* If that info is not correct, calculate afresh */
  233.       posval = *compute_motion (window,
  234.                 top, 0, 0, posint, height, 0,
  235.                    XFASTINT (w->width) - 1
  236.                 - (XFASTINT (w->width) + XFASTINT (w->left)
  237.                    != XSCREEN (w->screen)->width),
  238.                 XINT (w->hscroll), 0, 0, 0);
  239.  
  240.       return posval.vpos < height ? Qt : Qnil;
  241.     }
  242. }
  243.  
  244. static struct window *
  245. decode_window (window)
  246.      register Lisp_Object window;
  247. {
  248.   if (NILP (window))
  249.     return XWINDOW (selected_window);
  250.  
  251.   CHECK_WINDOW (window, 0);
  252.   return XWINDOW (window);
  253. }
  254.  
  255. DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
  256.   "Return the buffer that WINDOW is displaying.")
  257.   (window)
  258.      Lisp_Object window;
  259. {
  260.   return decode_window (window)->buffer;
  261. }
  262.  
  263. DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
  264.   "Return the number of lines in WINDOW (including its mode line).")
  265.   (window)
  266.      Lisp_Object window;
  267. {
  268.   return decode_window (window)->height;
  269. }
  270.  
  271. DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
  272.   "Return the number of columns in WINDOW.")
  273.   (window)
  274.      Lisp_Object window;
  275. {
  276.   register struct window *w = decode_window (window);
  277.   register int width = w->width;
  278.  
  279.   /* If this window does not end at the right margin,
  280.      must deduct one column for the border */
  281.   if ((width + w->left) == SCREEN_WIDTH (XSCREEN (WINDOW_SCREEN (w))))
  282.     return width;
  283.   return width - 1;
  284. }
  285.  
  286. DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
  287.   "Return the number of columns by which WINDOW is scrolled from left margin.")
  288.   (window)
  289.      Lisp_Object window;
  290. {
  291.   return decode_window (window)->hscroll;
  292. }
  293.  
  294. DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
  295.   "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
  296. NCOL should be zero or positive.")
  297.   (window, ncol)
  298.      register Lisp_Object window, ncol;
  299. {
  300.   register struct window *w;
  301.  
  302.   CHECK_FIXNUM (ncol, 1);
  303.   if (XINT (ncol) < 0) XFASTINT (ncol) = 0;
  304.   if (XFASTINT (ncol) >= (1 << (SHORTBITS - 1)))
  305.     args_out_of_range (ncol, Qnil);
  306.   w = decode_window (window);
  307.   if (w->hscroll != ncol)
  308.     clip_changed = 1;        /* Prevent redisplay shortcuts */
  309.   w->hscroll = ncol;
  310.   return ncol;
  311. }
  312.  
  313. DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
  314.   "Return a list of the edge coordinates of WINDOW.\n\
  315. \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of screen.\n\
  316. RIGHT is one more than the rightmost column used by WINDOW,\n\
  317. and BOTTOM is one more than the bottommost row used by WINDOW\n\
  318.  and its mode-line.")
  319.   (window)
  320.      Lisp_Object window;
  321. {
  322.   register struct window *w = decode_window (window);
  323.  
  324.   return Fcons (w->left, Fcons (w->top,
  325.            Fcons (make_number (XFASTINT (w->left) + XFASTINT (w->width)),
  326.           Fcons (make_number (XFASTINT (w->top)
  327.                       + XFASTINT (w->height)),
  328.              Qnil))));
  329. }
  330.  
  331. DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
  332.   "Return current value of point in WINDOW.\n\
  333. For a nonselected window, this is the value point would have\n\
  334. if that window were selected.\n\
  335. \n\
  336. Note that, when WINDOW is the selected window and its buffer\n\
  337. is also currently selected, the value returned is the same as (point).\n\
  338. It would be more strictly correct to return the `top-level' value\n\
  339. of point, outside of any save-excursion forms.\n\
  340. But that is hard to define.")
  341.   (window)
  342.      Lisp_Object window;
  343. {
  344.   register struct window *w = decode_window (window);
  345.  
  346.   if (w == XWINDOW (selected_window)
  347.       && current_buffer == XBUFFER (w->buffer))
  348.     return Fpoint ();
  349.   return Fmarker_position (w->pointm);
  350. }
  351.  
  352. DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
  353.   "Return position at which display currently starts in WINDOW.")
  354.   (window)
  355.      Lisp_Object window;
  356. {
  357.   return Fmarker_position (decode_window (window)->start);
  358. }
  359.  
  360. DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
  361.   "Return position at which display currently ends in WINDOW.")
  362.   (window)
  363.      Lisp_Object window;
  364. {
  365.   Lisp_Object value;
  366.   struct window *w = decode_window (window);
  367.   
  368.   XSET (value, Lisp_Int,
  369.     BUF_Z (current_buffer) - XFASTINT (w->window_end_pos));
  370.  
  371.   return value;
  372. }
  373.  
  374. DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
  375.   "Make point value in WINDOW be at position POS in WINDOW's buffer.")
  376.   (window, pos)
  377.      Lisp_Object window, pos;
  378. {
  379.   register struct window *w = decode_window (window);
  380.  
  381.   CHECK_FIXNUM_COERCE_MARKER (pos, 1);
  382.   if (w == XWINDOW (selected_window))
  383.     Fgoto_char (pos);
  384.   else
  385.     set_marker_restricted (w->pointm, pos, w->buffer);
  386.  
  387.   return pos;
  388. }
  389.  
  390. DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
  391.   "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
  392. Optional third arg NOFORCE non-nil inhibits next redisplay\n\
  393. from overriding motion of point in order to display at this exact start.")
  394.   (window, pos, noforce)
  395.      Lisp_Object window, pos, noforce;
  396. {
  397.   register struct window *w = decode_window (window);
  398.  
  399.   CHECK_FIXNUM_COERCE_MARKER (pos, 1);
  400.   set_marker_restricted (w->start, pos, w->buffer);
  401.   /* this is not right, but much easier than doing what is right. */
  402.   w->start_at_line_beg = Qnil;
  403.   if (NILP (noforce))
  404.     w->force_start = Qt;
  405.   w->redo_mode_line = Qt;
  406.   XFASTINT (w->last_modified) = 0;
  407.   XFASTINT (w->last_facechange) = 0;
  408.   /* Lucid fix: added this from Darryl Okahata <darrylo@HPNMXX.SR.HP.COM> */
  409.   windows_or_buffers_changed++;
  410.   return pos;
  411. }
  412.  
  413. DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
  414.        1, 1, 0,
  415.   "Return WINDOW's dedicated object, usually t or nil.\n\
  416. See also `set-window-buffer-dedicated'.")
  417.   (window)
  418.      Lisp_Object window;
  419. {
  420.   return decode_window (window)->dedicated;
  421. }
  422.  
  423. DEFUN ("set-window-buffer-dedicated", Fset_window_buffer_dedicated,
  424.        Sset_window_buffer_dedicated, 2, 2, 0,
  425.   "Make WINDOW display BUFFER and be dedicated to that buffer.\n\
  426. Then Emacs will not automatically change which buffer appears in WINDOW.\n\
  427. If BUFFER is nil, make WINDOW not be dedicated (but don't change which\n\
  428. buffer appears in it currently).")
  429.   (window, arg)
  430.        Lisp_Object window, arg;
  431. {
  432.   register struct window *w = decode_window (window);
  433.  
  434.   if (NILP (arg))
  435.     w->dedicated = Qnil;
  436.   else
  437.     {
  438.       Fset_window_buffer (window, Fget_buffer_create (arg));
  439.       w->dedicated = Qt;
  440.     }
  441.  
  442.   return w->dedicated;
  443. }
  444.  
  445. DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
  446.        0, 1, 0,
  447.   "Return the display-table that WINDOW is using.")
  448.   (window)
  449.      Lisp_Object window;
  450. {
  451.   return decode_window (window)->display_table;
  452. }
  453.  
  454. /* Get the display table for use currently on window W.
  455.    This is either W's display table or W's buffer's display table.
  456.    Ignore the specified tables if they are not valid;
  457.    if no valid table is specified, return 0.  */
  458.  
  459. struct Lisp_Vector *
  460. window_display_table (w)
  461.      struct window *w;
  462. {
  463.   Lisp_Object tem;
  464.   tem = w->display_table;
  465.   if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  466.     return XVECTOR (tem);
  467.   tem = XBUFFER (w->buffer)->display_table;
  468.   if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  469.     return XVECTOR (tem);
  470.   tem = Vstandard_display_table;
  471.   if (VECTORP (tem) && XVECTOR (tem)->size == DISP_TABLE_SIZE)
  472.     return XVECTOR (tem);
  473.   return 0;
  474. }
  475.  
  476. DEFUN ("set-window-display-table",
  477.        Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
  478.   "Set WINDOW's display-table to TABLE.")
  479.   (window, table)
  480.      register Lisp_Object window, table;
  481. {
  482.   register struct window *w;
  483.   w = decode_window (window);
  484.   w->display_table = table;
  485.   return table;
  486. }
  487.  
  488. /* Record info on buffer window w is displaying
  489.    when it is about to cease to display that buffer.  */
  490. static void
  491. unshow_buffer (w)
  492.      register struct window *w;
  493. {
  494.   Lisp_Object buf = w->buffer;
  495.  
  496.   if (XBUFFER (buf) != XMARKER (w->pointm)->buffer)
  497.     abort ();
  498.  
  499. #ifdef ENERGIZE
  500.     energize_buffer_hidden_hook (w);
  501. #endif
  502.  
  503.   if (w == XWINDOW (selected_window)
  504.       || ! EQ (buf, XWINDOW (selected_window)->buffer))
  505.     /* Do this except when the selected window's buffer
  506.        is being removed from some other window.  */
  507.     XBUFFER (buf)->last_window_start = marker_position (w->start);
  508.  
  509.   /* Point in the selected window's buffer
  510.      is actually stored in that buffer, and the window's pointm isn't used.
  511.      So don't clobber point in that buffer.  */
  512.   if (! EQ (buf, XWINDOW (selected_window)->buffer))
  513.     {
  514.       struct buffer *b = XBUFFER (buf);
  515.       int p = marker_position (w->pointm);
  516.       if (p < BUF_BEGV (b))
  517.     p = BUF_BEGV (b);
  518.       else if (p > BUF_ZV (b))
  519.     p = BUF_ZV (b);
  520.       SET_BUF_PT (b, p);
  521.     }
  522. }
  523.  
  524. /* Put replacement into the window structure in place of old. */
  525. static void
  526. replace_window (old, replacement)
  527.      Lisp_Object old, replacement;
  528. {
  529.   register Lisp_Object tem;
  530.   register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
  531.  
  532.   /* If OLD is its screen's root_window, then replacement is the new
  533.      root_window for that screen.  */
  534.  
  535.   if (old == XSCREEN (o->screen)->root_window)
  536.     XSCREEN (o->screen)->root_window = replacement;
  537.  
  538.   p->left = o->left;
  539.   p->top = o->top;
  540.   p->width = o->width;
  541.   p->height = o->height;
  542.  
  543.   p->next = tem = o->next;
  544.   if (!NILP (tem))
  545.     XWINDOW (tem)->prev = replacement;
  546.  
  547.   p->prev = tem = o->prev;
  548.   if (!NILP (tem))
  549.     XWINDOW (tem)->next = replacement;
  550.  
  551.   p->parent = tem = o->parent;
  552.   if (!NILP (tem))
  553.     {
  554.       if (EQ (XWINDOW (tem)->vchild, old))
  555.     XWINDOW (tem)->vchild = replacement;
  556.       if (EQ (XWINDOW (tem)->hchild, old))
  557.     XWINDOW (tem)->hchild = replacement;
  558.     }
  559.  
  560. /*** Here, if replacement is a vertical combination
  561. and so is its new parent, we should make replacement's
  562. children be children of that parent instead.  ***/
  563. }
  564.  
  565. extern int allow_deletion_of_last_visible_screen;
  566.  
  567. DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
  568.   "Remove WINDOW from the display.  Default is selected window.\n\
  569. If window is the only one on the screen, the screen is destroyed.")
  570.   (window)
  571.      register Lisp_Object window;
  572. {
  573.   register Lisp_Object tem, parent, sib;
  574.   register struct window *p;
  575.   register struct window *par;
  576.  
  577.   if (NILP (window))
  578.     window = selected_window;
  579.   else
  580.     CHECK_WINDOW (window, 0);
  581.  
  582.   p = XWINDOW (window);
  583.   parent = p->parent;
  584. #ifdef MULTI_SCREEN
  585.   if (ONLY_WINDOW_P (p))
  586.     {
  587.       windows_or_buffers_changed++;
  588.       if (NILP (memq_no_quit (p->screen, Vscreen_list)))
  589.     /* this screen isn't fully initialized yet; don't blow up. */
  590.     return Qnil;
  591.       if (EQ (p->screen, next_screen (p->screen, 0, 1)))
  592.     {
  593.       if (EQ (p->screen, next_screen (p->screen, 0, 0)))
  594.             error ("Attempt to delete the only window on the only screen");
  595.           else if (!allow_deletion_of_last_visible_screen)
  596.             error ("Attempt to delete the only window on the only visible screen");
  597.     }
  598.       Fdelete_screen (p->screen);
  599.       return Qnil;
  600.     }
  601. #else
  602.   if (NILP (parent))
  603.     error ("Attempt to delete minibuffer or sole ordinary window");
  604. #endif
  605.  
  606.   par = XWINDOW (parent);
  607.  
  608.   windows_or_buffers_changed++;
  609.  
  610.   if (EQ (window, selected_window))
  611.     Fselect_window (Fnext_window (window, Qnil, Qnil, Qnil));
  612.  
  613.   tem = p->buffer;
  614.   /* tem is null for dummy parent windows
  615.      (which have inferiors but not any contents themselves) */
  616.   if (!NILP (tem))
  617.     {
  618.       unshow_buffer (p);
  619.       unchain_marker (p->pointm);
  620.       unchain_marker (p->start);
  621.       /* This breaks set-window-configuration if windows in the saved
  622.      configuration get deleted and multiple screens are in use.
  623.      Consequently Fselect_window needs to check the validity of
  624.      the markers instead.
  625.        */
  626.       /*      p->buffer = Qnil; */
  627.     }
  628.  
  629.   tem = p->next;
  630.   if (!NILP (tem))
  631.     XWINDOW (tem)->prev = p->prev;
  632.  
  633.   tem = p->prev;
  634.   if (!NILP (tem))
  635.     XWINDOW (tem)->next = p->next;
  636.  
  637.   if (EQ (window, par->hchild))
  638.     par->hchild = p->next;
  639.   if (EQ (window, par->vchild))
  640.     par->vchild = p->next;
  641.  
  642.   /* Find one of our siblings to give our space to.  */
  643.   sib = p->prev;
  644.   if (NILP (sib))
  645.     sib = p->next;
  646.  
  647.   /* Stretch that sibling.  */
  648.   if (!NILP (par->vchild)) {
  649.     set_window_height (sib,
  650.                XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
  651.                1);
  652.     if (sib == p->next)    /* Lucid fix */
  653.       XFASTINT (XWINDOW(sib)->top) -= XFASTINT (p->height);
  654.   }
  655.   if (!NILP (par->hchild)) {
  656.     set_window_width (sib,
  657.               XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
  658.               1);
  659.     if (sib == p->next)    /* Lucid fix */
  660.       XFASTINT (XWINDOW(sib)->left) -= XFASTINT (p->width);
  661.   }
  662.  
  663.   /* If parent now has only one child,
  664.      put the child into the parent's place.  */
  665.  
  666.   tem = par->hchild;
  667.   if (NILP (tem))
  668.     tem = par->vchild;
  669.   if (NILP (XWINDOW (tem)->next))
  670.     replace_window (parent, tem);
  671.   return Qnil;
  672. }
  673.  
  674. #ifdef MULTI_SCREEN
  675. Lisp_Object
  676. next_screen_window (screen, window, mini)
  677.      Lisp_Object window, mini;
  678.      SCREEN_PTR screen;
  679. {
  680.   Lisp_Object tem;
  681.  
  682.   if (NILP (window))
  683.     window = SCREEN_SELECTED_WINDOW (screen);
  684.  
  685.   do
  686.     {
  687.       while (tem = XWINDOW (window)->next, NILP (tem))
  688.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  689.       window = tem;
  690.         else  /* window must be minibuffer window now */
  691.       {
  692.         tem = SCREEN_ROOT_WINDOW (screen);
  693.         break;
  694.       }
  695.  
  696.       window = tem;
  697.       while (1)
  698.     {
  699.       if (!NILP (XWINDOW (window)->hchild))
  700.         window = XWINDOW (window)->hchild;
  701.       else if (!NILP (XWINDOW (window)->vchild))
  702.         window = XWINDOW (window)->vchild;
  703.       else break;
  704.     }
  705.     }
  706.   while (MINI_WINDOW_P (XWINDOW (window))
  707.      && !EQ (mini, Qt)
  708.      && (!NILP (mini) || !minibuf_level));
  709.  
  710.   return window;
  711. }
  712. #endif
  713.  
  714. DEFUN ("next-window", Fnext_window, Snext_window, 0, 4, 0,
  715.   "Return next window after WINDOW in canonical ordering of windows.\n\
  716. Optional second arg MINIBUF t means count the minibuffer window\n\
  717. even if not active.  If MINIBUF is neither t nor nil it means\n\
  718. not to count the minibuffer even if it is active.\n\
  719. Optional third arg ALL-SCREENS t means include all windows in all visible\n\
  720. screens; otherwise cycle within the selected screen, with the exception that\n\
  721. if a global minibuffer screen is in use, all screens are used.\n\
  722. Optional fourth argument INVISIBLE-TOO t means also visit invisible screens.")
  723.   (window, mini, all_screens, invisible_too)
  724.      register Lisp_Object window, mini, all_screens, invisible_too;
  725. {
  726.   register Lisp_Object tem;
  727.  
  728.   if (NILP (window))
  729.     window = selected_window;
  730.   else
  731.     CHECK_WINDOW (window, 0);
  732.  
  733. #ifdef MULTI_SCREEN
  734.   if (EQ (mini, Qt)
  735.       || (! NILP (mini) && minibuf_level))
  736.     {
  737.       if (SCREENP (Vglobal_minibuffer_screen))
  738.     all_screens = Qt;
  739.     }
  740. #endif
  741.  
  742.   do
  743.     {
  744.       while (tem = XWINDOW (window)->next, NILP (tem))
  745.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  746.       window = tem;
  747.         else  /* window must be minibuffer window now */
  748.       {
  749.         tem = WINDOW_SCREEN (XWINDOW (window));
  750. #ifdef MULTI_SCREEN
  751.         if (! NILP (all_screens))
  752.           tem = next_screen (tem,
  753.                  (NILP (mini) ? 0 : 1),
  754.                  (NILP (invisible_too) ? 1 : 0));
  755. #endif
  756.         tem = SCREEN_ROOT_WINDOW (XSCREEN (tem));
  757.         break;
  758.       }
  759.  
  760.       window = tem;
  761.       while (1)
  762.     {
  763.       if (!NILP (XWINDOW (window)->hchild))
  764.         window = XWINDOW (window)->hchild;
  765.       else if (!NILP (XWINDOW (window)->vchild))
  766.         window = XWINDOW (window)->vchild;
  767.       else break;
  768.     }
  769.     }
  770.   while (MINI_WINDOW_P (XWINDOW (window))
  771.      && !EQ (mini, Qt)
  772.      && (! NILP (mini) || !minibuf_level)
  773.      && !EQ (SCREEN_ROOT_WINDOW (XSCREEN (XWINDOW (window)->screen)),
  774.          SCREEN_MINIBUF_WINDOW (XSCREEN (XWINDOW (window)->screen))));
  775.  
  776.   return window;
  777. }
  778.  
  779. DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 4, 0,
  780.   "Return previous window before WINDOW in canonical ordering of windows.\n\
  781. Optional second arg MINIBUF t means count the minibuffer window\n\
  782. even if not active.  If MINIBUF is neither t nor nil it means\n\
  783. not to count the minibuffer even if it is active.\n\
  784. Optional third arg ALL-SCREENS t means include all windows in all visible\n\
  785. screens; otherwise cycle within the selected screen, with the exception\n\
  786. that if a global minibuffer screen is in use, all visible screens are used.\n\
  787. If optional fourth argument INVISIBLE-TOO is t also visit invisible screens.")
  788.   (window, mini, all_screens, invisible_too)
  789.      register Lisp_Object window, mini, all_screens, invisible_too;
  790. {
  791.   register Lisp_Object tem;
  792.   register Lisp_Object screen_root, screen_mini;
  793.  
  794.   if (NILP (window))
  795.     window = selected_window;
  796.   else
  797.     CHECK_WINDOW (window, 0);
  798.  
  799. #ifdef MULTI_SCREEN
  800.   if (! NILP (mini))
  801.     {
  802.       if (SCREENP (Vglobal_minibuffer_screen))
  803.     all_screens = Qt;
  804.       else
  805.     all_screens = Qnil;
  806.     }
  807. #endif
  808.  
  809.   do  /* at least once, and until not the minibuffer */
  810.     {
  811.       while (tem = XWINDOW (window)->prev, NILP (tem))
  812.     if (tem = XWINDOW (window)->parent, !NILP (tem))
  813.       window = tem;
  814.         else  /* window must be the root window now */
  815.       {
  816.         tem = WINDOW_SCREEN (XWINDOW (window));
  817. #ifdef MULTI_SCREEN
  818.         if (! NILP (all_screens))
  819.           tem = prev_screen (tem, (NILP (mini) ? 0 : 1),
  820.                  (NILP (invisible_too) ? 1 : 0));
  821. #endif
  822. #if 0
  823.         tem = XWINDOW (XSCREEN (tem)->minibuffer_window)->prev;
  824. #endif
  825.         tem = SCREEN_ROOT_WINDOW (XSCREEN (tem));
  826.         break;
  827.       }
  828.  
  829.       window = tem;
  830.       while (1)
  831.     {
  832.       if (!NILP (XWINDOW (window)->hchild))
  833.         window = XWINDOW (window)->hchild;
  834.       else if (!NILP (XWINDOW (window)->vchild))
  835.         window = XWINDOW (window)->vchild;
  836.       else break;
  837.       while (tem = XWINDOW (window)->next, !NILP (tem))
  838.         window = tem;
  839.     }
  840.  
  841.       screen_root = SCREEN_ROOT_WINDOW (XSCREEN (XWINDOW (window)->screen));
  842.       screen_mini = SCREEN_MINIBUF_WINDOW (XSCREEN (XWINDOW (window)->screen));
  843.     }
  844.   while (MINI_WINDOW_P (XWINDOW (window))
  845.      && !EQ (mini, Qt)
  846.      && (!NILP (mini) || !minibuf_level)
  847.      && !EQ (screen_root, screen_mini));
  848.  
  849.   return window;
  850. }
  851.  
  852. DEFUN ("other-window", Fother_window, Sother_window, 1, 3, "p",
  853.   "Select the ARG'th different window on this screen.\n\
  854. All windows on current screen are arranged in a cyclic order.\n\
  855. This command selects the window ARG steps away in that order.\n\
  856. A negative ARG moves in the opposite order.  If the optional second\n\
  857. argument ALL_SCREENS is non-nil, cycle through all visible screens.\n\
  858. If optional third argument INVISIBLE-TOO is t also search invisible screens.")
  859.   (n, all_screens, invisible_too)
  860.      register Lisp_Object n, all_screens, invisible_too;
  861. {
  862.   register int i;
  863.   register Lisp_Object w;
  864.  
  865.   CHECK_FIXNUM (n, 0);
  866.   w = selected_window;
  867.   i = XINT (n);
  868.  
  869.   while (i > 0)
  870.     {
  871.       w = Fnext_window (w, Qnil, all_screens, invisible_too);
  872.       i--;
  873.     }
  874.   while (i < 0)
  875.     {
  876.       w = Fprevious_window (w, Qnil, all_screens, invisible_too);
  877.       i++;
  878.     }
  879.   Fselect_window (w);
  880.   return Qnil;
  881. }
  882.  
  883. /* Look at all windows, performing an operation specified by TYPE
  884.    with argument OBJ.
  885.  
  886.    If SCREENS is Qt, look at all screens, if Qnil, look at just the selected
  887.    screen.  If SCREENS is a screen, just look at windows on that screen.
  888.    If force it 1 and SCREENS is Qt also look at the invisible screens.
  889. */
  890.  
  891. enum window_loop
  892. {
  893.   WINDOW_LOOP_UNUSED,
  894.   GET_BUFFER_WINDOW,        /* Arg is buffer */
  895.   GET_LRU_WINDOW,        /* Arg is t for full-width windows only */
  896.   DELETE_OTHER_WINDOWS,        /* Arg is window not to delete */
  897.   DELETE_BUFFER_WINDOWS,    /* Arg is buffer */
  898.   GET_LARGEST_WINDOW,
  899.   UNSHOW_BUFFER            /* Arg is buffer */
  900. };
  901.  
  902. static Lisp_Object
  903. window_loop (type, obj, mini, this_screen, force)
  904.      enum window_loop type;
  905.      register Lisp_Object obj, this_screen;
  906.      int mini;
  907.      int force;
  908. {
  909.   register Lisp_Object w, tem;
  910.   register struct window *p, *q;
  911.   register Lisp_Object ret_w = Qnil;
  912.   register SCREEN_PTR screen;
  913.   int lose_lose = 0;
  914.  
  915.   if (NILP (this_screen) || EQ (this_screen, Qt))
  916.     screen = selected_screen;
  917.   else
  918.     screen = XSCREEN (this_screen);
  919.  
  920.   if (WINDOWP (obj))
  921.     w = obj;
  922.   else
  923.     w = SCREEN_SELECTED_WINDOW (screen);
  924.   ret_w = Qnil;
  925.  
  926.   while (1)
  927.     {
  928.  
  929.       /* Given the outstanding quality of the rest of this code, 
  930.      I feel no shame about putting this piece of shit in. */
  931.       if (++lose_lose >= 500)
  932.     return Qnil;
  933.  
  934.       p = XWINDOW (w);
  935.  
  936.       if (!MINI_WINDOW_P (XWINDOW (w)))
  937.     switch (type)
  938.       {
  939.       case GET_BUFFER_WINDOW:
  940.         if (XBUFFER (p->buffer) == XBUFFER (obj))
  941.           return w;
  942.         break;
  943.  
  944.       case GET_LRU_WINDOW:
  945.         /* t as arg means consider only full-width windows */
  946.         if (!NILP (obj) && XFASTINT (p->width) != screen->width)
  947.           break;
  948.  
  949.         /* Ignore dedicated windows and minibuffers.  */
  950.         if (MINI_WINDOW_P (XWINDOW (w))
  951.         || !NILP (XWINDOW (w)->dedicated))
  952.           break;
  953.         if (NILP (ret_w) ||
  954.         XFASTINT (XWINDOW (ret_w)->use_time) > XFASTINT (p->use_time))
  955.           ret_w = w;
  956.         break;
  957.  
  958.       case DELETE_OTHER_WINDOWS:
  959.         if (p != XWINDOW (obj))
  960.           Fdelete_window (w);
  961.         break;
  962.  
  963.       case DELETE_BUFFER_WINDOWS:
  964.         if (EQ (p->buffer, obj))
  965.           {
  966.         if (NILP (p->parent))
  967.           {
  968.             tem = Fother_buffer (obj, Qnil);
  969.             if (NILP (tem))
  970.               tem = Fget_buffer_create (build_string ("*scratch*"));
  971.             Fset_window_buffer (w, tem);
  972.             Fset_buffer (p->buffer);
  973.           }
  974.         else
  975.           Fdelete_window (w);
  976.           }
  977.         break;
  978.  
  979.       case GET_LARGEST_WINDOW:
  980.         /* Ignore dedicated windows and minibuffers.  */
  981.         if (MINI_WINDOW_P (XWINDOW (w))
  982.         || !NILP (XWINDOW (w)->dedicated))
  983.           break;
  984.         q = XWINDOW (ret_w);
  985.         if (NILP (ret_w) ||
  986.         (XFASTINT (p->height) * XFASTINT (p->width))
  987.         >
  988.         (XFASTINT (q->height) * XFASTINT (q->width)))
  989.           ret_w = w;
  990.         break;
  991.  
  992.       case UNSHOW_BUFFER:
  993.         if (EQ (p->buffer, obj))
  994.           {
  995.         tem = Fother_buffer (obj, Qnil);
  996.         if (NILP (tem))
  997.           tem = Fget_buffer_create (build_string ("*scratch*"));
  998.         Fset_window_buffer (w, tem);
  999.         if (EQ (w, selected_window))
  1000.           Fset_buffer (p->buffer);
  1001.           }
  1002.         break;
  1003.  
  1004.       case WINDOW_LOOP_UNUSED:
  1005.         abort ();
  1006.       }
  1007.  
  1008.       if (EQ (this_screen, Qt))
  1009.     w = Fnext_window (w, mini ? Qt : Qnil, Qt, force ? Qt : Qnil);
  1010.       else
  1011.     w = next_screen_window (screen, w, mini ? Qt : Qnil);
  1012.  
  1013.       if (EQ (w, SCREEN_SELECTED_WINDOW (screen)))
  1014.     return ret_w;
  1015.     }
  1016. }
  1017.  
  1018. DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
  1019.   "Return the window least recently selected or used for display.\n\
  1020. If optional argument SCREENS is non-nil, search only that screen.")
  1021.   (screens)
  1022.   Lisp_Object screens;
  1023. {
  1024.   register Lisp_Object w;
  1025.   /* First try for a window that is full-width */
  1026.   w = window_loop (GET_LRU_WINDOW, Qt, 0, screens, 0);
  1027.   if (!NILP (w) && !EQ (w, selected_window))
  1028.     return w;
  1029.   /* If none of them, try the rest */
  1030.   return window_loop (GET_LRU_WINDOW, Qnil, 0, screens, 0);
  1031. }
  1032.  
  1033. DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
  1034.   "Return the window largest in area.  If optional argument SCREENS\n\
  1035. is non-nil, search only that screen.")
  1036.   (screens)
  1037.   Lisp_Object screens;
  1038. {
  1039.   return window_loop (GET_LARGEST_WINDOW, Qnil, 0, screens, 0);
  1040. }
  1041.  
  1042. DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 3, 0,
  1043.   "Return a window currently displaying BUFFER, or nil if none.\n\
  1044. Only the selected screen is searched; if the optional second argument\n\
  1045. SCREEN is non-nil, then that screen is searched instead.  If SCREEN is t,\n\
  1046. then all visible screens are searched.  If FORCE is t invisible screens\n\
  1047. are searched too.")
  1048.   (buffer, screen, force)
  1049.      Lisp_Object buffer, screen, force;
  1050. {
  1051.   buffer = Fget_buffer (buffer);
  1052.   if (BUFFERP (buffer))
  1053.     return window_loop (GET_BUFFER_WINDOW, buffer, 1, screen,
  1054.             NILP (force) ? 0 : 1);
  1055.   else return Qnil;
  1056. }
  1057.  
  1058. DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
  1059.   0, 1, "",
  1060.   "Make WINDOW (or the selected window) fill its screen.")
  1061.   (window)
  1062.      Lisp_Object window;
  1063. {
  1064.   struct window *w;
  1065.   int opoint, owpoint;
  1066.   struct buffer *obuf, *owbuf;
  1067.   int top, start;
  1068.  
  1069.   if (NILP (window))
  1070.     window = selected_window;
  1071.   else
  1072.     CHECK_WINDOW (window, 0);
  1073.  
  1074.   w = XWINDOW (window);
  1075.   top = XFASTINT (w->top);
  1076.  
  1077.   obuf = current_buffer;
  1078.   opoint = point;
  1079.  
  1080.   owbuf = XBUFFER (w->buffer);
  1081.   owpoint = BUF_PT (owbuf);
  1082.  
  1083.   window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_SCREEN (w), 0);
  1084.  
  1085.   Fset_buffer (w->buffer); /* owbuf */
  1086.   
  1087.   start = marker_position (w->start);
  1088.  
  1089.   if (start < BEGV)
  1090.     start = BEGV;
  1091.   else if (start > ZV)
  1092.     start = ZV;
  1093.  
  1094.   SET_PT (start);
  1095.   Frecenter (make_number (top));
  1096.   SET_PT (owpoint);
  1097.  
  1098.   internal_set_buffer (obuf);
  1099.   SET_PT (opoint);
  1100.   return Qnil;
  1101. }
  1102.  
  1103. DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
  1104.   1, 1, "bDelete windows on (buffer): ",
  1105.   "Delete all windows showing BUFFER.")
  1106.   (buffer)
  1107.      Lisp_Object buffer;
  1108. {
  1109.   if (!NILP (buffer))
  1110.     {
  1111.       buffer = Fget_buffer (buffer);
  1112.       CHECK_BUFFER (buffer, 0);
  1113.       window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, Qt, 1);
  1114.     }
  1115.   return Qnil;
  1116. }
  1117.  
  1118. DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
  1119.   Sreplace_buffer_in_windows,
  1120.   1, 1, "bReplace buffer in windows: ",
  1121.   "Replace BUFFER with some other buffer in all windows showing it.")
  1122.   (buffer)
  1123.      Lisp_Object buffer;
  1124. {
  1125.   if (!NILP (buffer))
  1126.     {
  1127.       buffer = Fget_buffer (buffer);
  1128.       CHECK_BUFFER (buffer, 0);
  1129.       window_loop (UNSHOW_BUFFER, buffer, 0, Qt, 1);
  1130.     }
  1131.   return Qnil;
  1132. }
  1133.  
  1134. /* Set the height of WINDOW and all its inferiors.  */
  1135. /* Normally the window is deleted if it gets too small.
  1136.    nodelete nonzero means do not do this.
  1137.    (The caller should check later and do so if appropriate)  */
  1138.  
  1139. void
  1140. set_window_height (window, height, nodelete)
  1141.      Lisp_Object window;
  1142.      int height;
  1143.      int nodelete;
  1144. {
  1145.   register struct window *w = XWINDOW (window);
  1146.   register struct window *c;
  1147.   int oheight = XFASTINT (w->height);
  1148.   int top, pos, lastbot, opos, lastobot;
  1149.   Lisp_Object child;
  1150.  
  1151.   if (!nodelete
  1152.       && ! NILP (w->parent)
  1153.       && height < window_min_height)
  1154.     {
  1155.       Fdelete_window (window);
  1156.       return;
  1157.     }
  1158.  
  1159.   XFASTINT (w->last_modified) = 0;
  1160.   XFASTINT (w->last_facechange) = 0;
  1161.   windows_or_buffers_changed++;
  1162.   XFASTINT (w->height) = height;
  1163.   if (!NILP (w->hchild))
  1164.     {
  1165.       for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1166.     {
  1167.       XWINDOW (child)->top = w->top;
  1168.       set_window_height (child, height, nodelete);
  1169.     }
  1170.     }
  1171.   else if (!NILP (w->vchild))
  1172.     {
  1173.       lastbot = top = XFASTINT (w->top);
  1174.       lastobot = 0;
  1175.       for (child = w->vchild; !NILP (child); child = c->next)
  1176.     {
  1177.       c = XWINDOW (child);
  1178.  
  1179.       opos = lastobot + XFASTINT (c->height);
  1180.  
  1181.       XFASTINT (c->top) = lastbot;
  1182.  
  1183.       pos = (((opos * height) << 1) + oheight) / (oheight << 1);
  1184.  
  1185.       /* Avoid confusion: inhibit deletion of child if becomes too small */
  1186.       set_window_height (child, pos + top - lastbot, 1);
  1187.  
  1188.       /* Now advance child to next window,
  1189.          and set lastbot if child was not just deleted.  */
  1190.       lastbot = pos + top, lastobot = opos;
  1191.     }
  1192.       /* Now delete any children that became too small.  */
  1193.       if (!nodelete)
  1194.     for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1195.       {
  1196.         set_window_height (child, XINT (XWINDOW (child)->height), 0);
  1197.       }
  1198.     }
  1199. }
  1200.  
  1201. /* Recursively set width of WINDOW and its inferiors. */
  1202.  
  1203. void
  1204. set_window_width (window, width, nodelete)
  1205.      Lisp_Object window;
  1206.      int width;
  1207.      int nodelete;
  1208. {
  1209.   register struct window *w = XWINDOW (window);
  1210.   register struct window *c;
  1211.   int owidth = XFASTINT (w->width);
  1212.   int left, pos, lastright, opos, lastoright;
  1213.   Lisp_Object child;
  1214.  
  1215.   if (!nodelete && width < window_min_width)
  1216.     {
  1217.       Fdelete_window (window);
  1218.       return;
  1219.     }
  1220.  
  1221.   XFASTINT (w->last_modified) = 0;
  1222.   XFASTINT (w->last_facechange) = 0;
  1223.   windows_or_buffers_changed++;
  1224.   XFASTINT (w->width) = width;
  1225.   if (!NILP (w->vchild))
  1226.     {
  1227.       for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
  1228.     {
  1229.       XWINDOW (child)->left = w->left;
  1230.       set_window_width (child, width, nodelete);
  1231.     }
  1232.     }
  1233.   else if (!NILP (w->hchild))
  1234.     {
  1235.       lastright = left = XFASTINT (w->left);
  1236.       lastoright = 0;
  1237.       for (child = w->hchild; !NILP (child); child = c->next)
  1238.     {
  1239.       c = XWINDOW (child);
  1240.  
  1241.       opos = lastoright + XFASTINT (c->width);
  1242.  
  1243.       XFASTINT (c->left) = lastright;
  1244.  
  1245.       pos = (((opos * width) << 1) + owidth) / (owidth << 1);
  1246.  
  1247.       /* Inhibit deletion for becoming too small */
  1248.       set_window_width (child, pos + left - lastright, 1);
  1249.  
  1250.       /* Now advance child to next window,
  1251.          and set lastright if child was not just deleted.  */
  1252.       lastright = pos + left, lastoright = opos;
  1253.     }
  1254.       /* Delete children that became too small */
  1255.       if (!nodelete)
  1256.     for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
  1257.       {
  1258.         set_window_width (child, XINT (XWINDOW (child)->width), 0);
  1259.       }
  1260.     }
  1261. }
  1262.  
  1263. static int window_select_count;
  1264.  
  1265. DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
  1266.   "Make WINDOW display BUFFER as its contents.\n\
  1267. BUFFER can be a buffer or buffer name.")
  1268.   (window, buffer)
  1269.      register Lisp_Object window, buffer;
  1270. {
  1271.   register Lisp_Object tem;
  1272.   register struct window *w = decode_window (window);
  1273.  
  1274.   buffer = Fget_buffer (buffer);
  1275.   CHECK_BUFFER (buffer, 1);
  1276.  
  1277.   if (NILP (XBUFFER (buffer)->name))
  1278.     error ("Attempt to display deleted buffer");
  1279.  
  1280.   tem = w->buffer;
  1281.   if (NILP (tem))
  1282.     error ("Window is deleted");
  1283.   else if (! EQ (tem, Qt))    /* w->buffer is t when the window
  1284.                    is first being set up.  */
  1285.     {
  1286.       if (!NILP (w->dedicated) && !EQ (tem, buffer))
  1287.     /* Lucid fix */
  1288.     error ("Window is dedicated to buffer %s",
  1289.            XSTRING(XBUFFER(tem)->name)->data);
  1290.  
  1291.       unshow_buffer (w);
  1292.     }
  1293.  
  1294.   w->buffer = buffer;
  1295.   Fset_marker (w->pointm,
  1296.            make_number (BUF_PT (XBUFFER (buffer))),
  1297.            buffer);
  1298.   set_marker_restricted (w->start,
  1299.              make_number (XBUFFER (buffer)->last_window_start),
  1300.              buffer);
  1301.   w->start_at_line_beg = Qnil;
  1302.   w->force_start = Qnil; /* Lucid fix */
  1303.   XFASTINT (w->last_modified) = 0;
  1304.   XFASTINT (w->last_facechange) = 0;
  1305.   windows_or_buffers_changed++;
  1306.   if (EQ (window, selected_window)) {
  1307.     Fset_buffer (buffer);
  1308. #ifdef ENERGIZE
  1309.     energize_buffer_shown_hook (w);
  1310. #endif
  1311.   }
  1312.   return Qnil;
  1313. }
  1314.  
  1315. DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
  1316.   "Select WINDOW.  Most editing will apply to WINDOW's buffer.\n\
  1317. The main editor command loop selects the buffer of the selected window\n\
  1318. before each command.")
  1319.   (window)
  1320.      register Lisp_Object window;
  1321. {
  1322.   register struct window *w;
  1323.   register struct window *ow = XWINDOW (selected_window);
  1324.   register int new_point;
  1325.  
  1326.   CHECK_WINDOW (window, 0);
  1327.  
  1328.   w = XWINDOW (window);
  1329.  
  1330.   if (NILP (w->buffer) ||  /* no longer enough - see Fdelete_window() */
  1331.       NILP (w->start) || NILP (w->pointm) ||
  1332.       XMARKER (w->start)->buffer == 0 ||
  1333.       XMARKER (w->pointm)->buffer == 0)
  1334.     error ("Trying to select deleted window or non-leaf window");
  1335.  
  1336.   XFASTINT (w->use_time) = ++window_select_count;
  1337.   if (EQ (window, selected_window))
  1338.     return window;
  1339.  
  1340. #ifdef ENERGIZE
  1341.   if (! MINI_WINDOW_P (w))
  1342.     energize_window_deselected_hook (XWINDOW(selected_window));
  1343. #endif
  1344.  
  1345.   Fset_marker (ow->pointm, make_number (BUF_PT (XBUFFER (ow->buffer))),
  1346.            ow->buffer);
  1347.  
  1348.   selected_window = window;
  1349. #ifdef MULTI_SCREEN
  1350.   if (XSCREEN (WINDOW_SCREEN (w)) != selected_screen)
  1351.     {
  1352.       XSCREEN (WINDOW_SCREEN (w))->selected_window = window;
  1353.       Fselect_screen (WINDOW_SCREEN (w));
  1354.     }
  1355.   else
  1356.     selected_screen->selected_window = window;
  1357. #endif
  1358.  
  1359.   record_buffer (w->buffer);
  1360.   Fset_buffer (w->buffer);
  1361.  
  1362.   /* Go to the point recorded in the window.
  1363.      This is important when the buffer is in more
  1364.      than one window.  It also matters when
  1365.      redisplay_window has altered point after scrolling,
  1366.      because it makes the change only in the window.  */
  1367.   new_point = (marker_position (w->pointm));
  1368.  
  1369.   if (new_point < BEGV)
  1370.     new_point = BEGV;
  1371.   else if (new_point > ZV)
  1372.     new_point = ZV;
  1373.  
  1374.   SET_PT (new_point);
  1375.  
  1376.   windows_or_buffers_changed++;
  1377.  
  1378. #ifdef ENERGIZE
  1379.   if (! MINI_WINDOW_P (w))
  1380.     energize_window_selected_hook (w);
  1381. #endif
  1382.  
  1383.   return window;
  1384. }
  1385.  
  1386. /* if global-minibuffer-mode, this is the screen which invoked the minibuf
  1387.    #### the global-minibuffer code doesn't really work, and I think it might
  1388.    be simpler if there was no "active" screen, but we arranged for the 
  1389.    minibuffer screen to never be the selected-screen. */
  1390. extern struct screen *active_screen;
  1391.  
  1392. DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3, 0,
  1393.        "Make BUFFER appear in some window on the current screen, but don't select it.\n\
  1394. BUFFER can be a buffer or a buffer name.\n\
  1395. If BUFFER is shown already in some window in the current screen, just uses\n\
  1396.  that one, unless the window is the selected window and NOT_THIS_WINDOW_P\n\
  1397.  is non-nil.\n\
  1398. If ON_SCREEN is non-nil, display on that screen instead of the current\n\
  1399.  screen (or the dedicated screen).\n\
  1400. If BUFFER has a dedicated screen, display on that screen instead of the\n\
  1401.  current screen, unless ON_SCREEN was specified.\n\
  1402. If pop-up-windows is non-nil, and the buffer is not visible in some window\n\
  1403.  on the target screen, then windows will be split (a new window will be\n\
  1404.  created) if there is room.  Otherwise, the least-recently-used window will\n\
  1405.  be reused.\n\
  1406. Returns the window displaying BUFFER.")
  1407.   (buffer, not_this_window_p, on_screen)
  1408.      register Lisp_Object buffer, not_this_window_p, on_screen;
  1409. {
  1410.   Lisp_Object window;
  1411.   Lisp_Object old_screen, target_screen;
  1412. #ifdef MULTI_SCREEN
  1413. # if 0
  1414.   /* if global-minibuffer-mode, this is the screen which invoked the minibuf */
  1415.   if (active_screen)
  1416.     XSET (old_screen, Lisp_Screen, active_screen);
  1417.   else
  1418. # endif
  1419.     XSET (old_screen, Lisp_Screen, selected_screen);
  1420. #endif
  1421.  
  1422.   buffer = Fget_buffer (buffer);
  1423.   CHECK_BUFFER (buffer, 0);
  1424.  
  1425.   /* Give the user the ability to completely reimplement this function in
  1426.      lisp via the `display-buffer-function'.
  1427.    */
  1428.   if (!NILP (Vdisplay_buffer_function))
  1429.     {
  1430.       if (!NILP (Vpre_display_buffer_function))
  1431.     call3 (Vpre_display_buffer_function,
  1432.            buffer, not_this_window_p, on_screen);
  1433.       return
  1434.     call3 (Vdisplay_buffer_function,
  1435.            buffer, not_this_window_p, on_screen);
  1436.     }
  1437.  
  1438.   /* else... */
  1439.  
  1440.   target_screen = old_screen;
  1441.  
  1442.   if (!NILP (Vpre_display_buffer_function))
  1443.     {
  1444.       /* this hook may select some other screen, so take notice. */
  1445.       call3 (Vpre_display_buffer_function,
  1446.          buffer, not_this_window_p, on_screen);
  1447.       XSET (target_screen, Lisp_Screen, selected_screen);
  1448.     }
  1449.  
  1450.   /* If the buffer has a dedicated screen, that takes precedence over the
  1451.      current screen, and over what the pre-display-buffer-function did.
  1452.    */
  1453.   if (!NILP (XBUFFER (buffer)->dedicated_screen))
  1454.     {
  1455.       target_screen = XBUFFER (buffer)->dedicated_screen;
  1456.       CHECK_SCREEN (target_screen, 0);
  1457.     }
  1458.  
  1459.   /* if on_screen is supplied, that takes precedence over everything.
  1460.      (This is gonna look bad if the pre-display-buffer-function raised
  1461.      some other screen already.)
  1462.    */
  1463.   if (!NILP (on_screen))
  1464.     {
  1465.       target_screen = on_screen;
  1466.       CHECK_SCREEN (target_screen, 0);
  1467.     }
  1468.  
  1469.   /* If we have switched screens, then set not_this_window_p to false.
  1470.      Switching screens means that selected_window is no longer the same
  1471.      as it was on entry -- it's the selected_window of target_screen
  1472.      instead of old_screen, so it's a fine candidate for display. */
  1473.   if (!EQ (old_screen, target_screen))
  1474.     not_this_window_p = Qnil;
  1475.  
  1476.   /* if it's in the selected window, and that's ok, then we're done. */
  1477.   if (NILP (not_this_window_p)
  1478.       && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
  1479.     return selected_window;
  1480.  
  1481.   /* Otherwise, find some window that it's already in, and return that,
  1482.      unless that window is the selected window and that isn't ok.
  1483.      What a contorted mess! */
  1484.   window = Fget_buffer_window (buffer, target_screen, Qnil);
  1485.   if (!NILP (window)
  1486.       && (NILP (not_this_window_p) || !EQ (window, selected_window)))
  1487.     return window;
  1488.  
  1489.   /* Otherwise, make it be in some window, splitting if appropriate/possible.
  1490.      Do not split a window if we are displaying the buffer in a different
  1491.      screen than that which was current when we were called.  (It is already
  1492.      in a different window by virtue of being in another screen.)
  1493.    */
  1494.   if (pop_up_windows && EQ (target_screen, old_screen))
  1495.     {
  1496.       /* Don't try to create a window if would get an error */
  1497.       if (split_height_threshold < window_min_height << 1)
  1498.     split_height_threshold = window_min_height << 1;
  1499.  
  1500.       window = Fget_largest_window (target_screen);
  1501.  
  1502.       if (!NILP (window)
  1503.       && window_height (window) >= split_height_threshold
  1504.       &&
  1505.       (XFASTINT (XWINDOW (window)->width)
  1506.        == SCREEN_WIDTH (XSCREEN (WINDOW_SCREEN (XWINDOW (window))))))
  1507.     window = Fsplit_window (window, Qnil, Qnil);
  1508.       else
  1509.     {
  1510.       window = Fget_lru_window (target_screen);
  1511.       if ((EQ (window, selected_window)
  1512.            || EQ (XWINDOW (window)->parent, Qnil))
  1513.           && window_height (window) >= window_min_height << 1)
  1514.         window = Fsplit_window (window, Qnil, Qnil);
  1515.     }
  1516.     }
  1517.   else
  1518.     window = Fget_lru_window (target_screen);
  1519.  
  1520.   Fset_window_buffer (window, buffer);
  1521.   return window;
  1522. }
  1523.  
  1524. void
  1525. temp_output_buffer_show (buf, on_screen)
  1526.      register Lisp_Object buf, on_screen;
  1527. {
  1528.   register struct buffer *old = current_buffer;
  1529.   register Lisp_Object window;
  1530.   register struct window *w;
  1531.  
  1532.   Fset_buffer (buf);
  1533.   XBUFFER (buf)->save_modified = MODIFF;
  1534.   BEGV = BEG;
  1535.   ZV = Z;
  1536.   SET_PT (BEG);
  1537.   clip_changed = 1;
  1538.   internal_set_buffer (old);
  1539.  
  1540.   if (!EQ (Vtemp_buffer_show_function, Qnil))
  1541.     call1 (Vtemp_buffer_show_function, buf);
  1542.   else
  1543.     {
  1544.       window = Fdisplay_buffer (buf, Qnil, on_screen);
  1545.  
  1546. #ifdef MULTI_SCREEN
  1547.       if (XSCREEN (XWINDOW (window)->screen) != selected_screen)
  1548.     Fmake_screen_visible (XWINDOW (window)->screen);
  1549. #endif /* MULTI_SCREEN */
  1550.       Vminibuf_scroll_window = window;
  1551.       w = XWINDOW (window);
  1552.       XFASTINT (w->hscroll) = 0;
  1553.       set_marker_restricted (w->start, make_number (1), buf);
  1554.       set_marker_restricted (w->pointm, make_number (1), buf);
  1555.     }
  1556. }
  1557.  
  1558. static void
  1559. make_dummy_parent (window)
  1560.      Lisp_Object window;
  1561. {
  1562.   register Lisp_Object old, new;
  1563.   register struct window *o, *p;
  1564.  
  1565.   old = window;
  1566.   XSETTYPE (old, Lisp_Vector);
  1567.   new = Fcopy_sequence (old);
  1568.   XSETTYPE (new, Lisp_Window);
  1569.  
  1570.   o = XWINDOW (old);
  1571.   p = XWINDOW (new);
  1572.   XFASTINT (p->sequence_number) = ++sequence_number;
  1573.  
  1574.   /* Put new into window structure in place of window */
  1575.   replace_window (window, new);
  1576.  
  1577.   o->next = Qnil;
  1578.   o->prev = Qnil;
  1579.   o->vchild = Qnil;
  1580.   o->hchild = Qnil;
  1581.   o->parent = new;
  1582.  
  1583.   p->start = Qnil;
  1584.   p->pointm = Qnil;
  1585.   p->buffer = Qnil;
  1586. }
  1587.  
  1588. DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
  1589.   "Split WINDOW, putting SIZE lines in the first of the pair.\n\
  1590. WINDOW defaults to selected one and SIZE to half its size.\n\
  1591. If optional third arg HOR-FLAG is non-nil, split side by side\n\
  1592. and put SIZE columns in the first of the pair.")
  1593.   (window, chsize, horflag)
  1594.      Lisp_Object window, chsize, horflag;
  1595. {
  1596.   register Lisp_Object new;
  1597.   register struct window *o, *p;
  1598.   register int size;
  1599.  
  1600.   if (NILP (window))
  1601.     window = selected_window;
  1602.   else
  1603.     CHECK_WINDOW (window, 0);
  1604.  
  1605.   o = XWINDOW (window);
  1606.  
  1607.   if (NILP (chsize))
  1608.     {
  1609.       if (!NILP (horflag))
  1610.     /* Round odd size up, since this is for the left-hand window,
  1611.        and it will lose a column for the separators.  */
  1612.     size = ((XFASTINT (o->width) + 1) & -2) >> 1;
  1613.       else
  1614.     size = XFASTINT (o->height) >> 1;
  1615.     }
  1616.   else
  1617.     {
  1618.       CHECK_FIXNUM (chsize, 1);
  1619.       size = XINT (chsize);
  1620.     }
  1621.  
  1622.   if (MINI_WINDOW_P (o))
  1623.     error ("Attempt to split minibuffer window");
  1624.   else if (SCREEN_NO_SPLIT_P (XSCREEN (WINDOW_SCREEN (o))))
  1625.     error ("Attempt to split unsplittable screen");
  1626.  
  1627.   /* Smaller values might permit a crash.  */
  1628.   if (window_min_width < 2)
  1629.     window_min_width = 2;
  1630.   if (window_min_height < 2)
  1631.     window_min_height = 2;
  1632.  
  1633.   if (NILP (horflag))
  1634.     {
  1635.       if (size < window_min_height
  1636.       || size + window_min_height > XFASTINT (o->height))
  1637.     args_out_of_range_3 (window, chsize, horflag);
  1638.       if (NILP (o->parent)
  1639.       || NILP (XWINDOW (o->parent)->vchild))
  1640.     {
  1641.       make_dummy_parent (window);
  1642.       new = o->parent;
  1643.       XWINDOW (new)->vchild = window;
  1644.     }
  1645.     }
  1646.   else
  1647.     {
  1648.       if (size < window_min_width
  1649.       || size + window_min_width > XFASTINT (o->width))
  1650.     args_out_of_range_3 (window, chsize, horflag);
  1651.       if (NILP (o->parent)
  1652.       || NILP (XWINDOW (o->parent)->hchild))
  1653.     {
  1654.       make_dummy_parent (window);
  1655.       new = o->parent;
  1656.       XWINDOW (new)->hchild = window;
  1657.     }
  1658.     }
  1659.  
  1660.   /* Now we know that window's parent is a vertical combination
  1661.      if we are dividing vertically, or a horizontal combination
  1662.      if we are making side-by-side windows */
  1663.  
  1664.   windows_or_buffers_changed++;
  1665.   new = make_window ();
  1666.   p = XWINDOW (new);
  1667.  
  1668.   p->screen = o->screen;
  1669.   p->next = o->next;
  1670.   if (!NILP (p->next))
  1671.     XWINDOW (p->next)->prev = new;
  1672.   p->prev = window;
  1673.   o->next = new;
  1674.   p->parent = o->parent;
  1675.   p->buffer = Qt;
  1676.  
  1677.   Fset_window_buffer (new, o->buffer);
  1678.  
  1679.   /* Apportion the available screen space among the two new windows */
  1680.  
  1681.   if (!NILP (horflag))
  1682.     {
  1683.       p->height = o->height;
  1684.       p->top = o->top;
  1685.       XFASTINT (p->width) = XFASTINT (o->width) - size;
  1686.       XFASTINT (o->width) = size;
  1687.       XFASTINT (p->left) = XFASTINT (o->left) + size;
  1688.     }
  1689.   else
  1690.     {
  1691.       p->left = o->left;
  1692.       p->width = o->width;
  1693.       XFASTINT (p->height) = XFASTINT (o->height) - size;
  1694.       XFASTINT (o->height) = size;
  1695.       XFASTINT (p->top) = XFASTINT (o->top) + size;
  1696.     }
  1697.  
  1698.   return new;
  1699. }
  1700.  
  1701. DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
  1702.   "Make current window ARG lines bigger.\n\
  1703. From program, optional second arg non-nil means grow sideways ARG columns.")
  1704.   (n, side)
  1705.      register Lisp_Object n, side;
  1706. {
  1707.   CHECK_FIXNUM (n, 0);
  1708.   change_window_height (XINT (n), !NILP (side));
  1709.   zmacs_region_stays = 1;
  1710.   return Qnil;
  1711. }
  1712.  
  1713. DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
  1714.   "Make current window ARG lines smaller.\n\
  1715. From program, optional second arg non-nil means shrink sideways ARG columns.")
  1716.   (n, side)
  1717.      register Lisp_Object n, side;
  1718. {
  1719.   CHECK_FIXNUM (n, 0);
  1720.   change_window_height (-XINT (n), !NILP (side));
  1721.   zmacs_region_stays = 1;
  1722.   return Qnil;
  1723. }
  1724.  
  1725. int
  1726. window_height (window)
  1727.      Lisp_Object window;
  1728. {
  1729.   register struct window *p = XWINDOW (window);
  1730.   return XFASTINT (p->height);
  1731. }
  1732.  
  1733. int
  1734. window_width (window)
  1735.      Lisp_Object window;
  1736. {
  1737.   register struct window *p = XWINDOW (window);
  1738.   return XFASTINT (p->width);
  1739. }
  1740.  
  1741. #define MINSIZE(w) \
  1742.   (widthflag ? window_min_width : window_min_height)
  1743.  
  1744. #define CURBEG(w) \
  1745.   *(widthflag ? (int *) &(w)->left : (int *) &(w)->top)
  1746.  
  1747. #define CURSIZE(w) \
  1748.   *(widthflag ? (int *) &(w)->width : (int *) &(w)->height)
  1749.  
  1750. /* Unlike set_window_height, this function
  1751.    also changes the heights of the siblings so as to
  1752.    keep everything consistent. */
  1753.  
  1754. void
  1755. change_window_height (delta, widthflag)
  1756.      register int delta;
  1757.      int widthflag;
  1758. {
  1759.   register Lisp_Object parent;
  1760.   Lisp_Object window;
  1761.   register struct window *p;
  1762.   int *sizep;
  1763.   int (*sizefun) () = widthflag ? window_width : window_height;
  1764.   register void (*setsizefun) () = (widthflag
  1765.                     ? set_window_width
  1766.                     : set_window_height);
  1767.   if (delta == 0)
  1768.     return;
  1769.  
  1770.   if (EQ (selected_window,
  1771.       SCREEN_ROOT_WINDOW (XSCREEN (XWINDOW (selected_window)->screen))))
  1772.     error ("Won't change only window");
  1773.  
  1774.   /* Smaller values might permit a crash.  */
  1775.   if (window_min_width < 2)
  1776.     window_min_width = 2;
  1777.   if (window_min_height < 2)
  1778.     window_min_height = 2;
  1779.  
  1780.   window = selected_window;
  1781.   while (1)
  1782.     {
  1783.       p = XWINDOW (window);
  1784.       parent = p->parent;
  1785.       if (NILP (parent))
  1786.     {
  1787.       if (widthflag)
  1788.         error ("No other window to side of this one");
  1789.       break;
  1790.     }
  1791.       if (widthflag ? !NILP (XWINDOW (parent)->hchild)
  1792.       : !NILP (XWINDOW (parent)->vchild))
  1793.     break;
  1794.       window = parent;
  1795.     }
  1796.  
  1797.   sizep = &CURSIZE (p);
  1798.  
  1799.   if (*sizep + delta < MINSIZE (p)
  1800.       && !NILP (XWINDOW (window)->parent))
  1801.     {
  1802.       Fdelete_window (window);
  1803.       return;
  1804.     }
  1805.  
  1806.   {
  1807.     register int maxdelta;
  1808.     register Lisp_Object tem;
  1809.  
  1810.     maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
  1811.         : (tem = (!NILP (p->next) ? p->next : p->prev),
  1812.            (*sizefun) (tem) - MINSIZE (tem)));
  1813.  
  1814.     if (delta > maxdelta)
  1815.       /* This case traps trying to make the minibuffer
  1816.      the full screen, or make the only window aside from the
  1817.      minibuffer the full screen.  */
  1818.       delta = maxdelta;
  1819.   }
  1820.  
  1821.   if (!NILP (p->next) &&
  1822.       (*sizefun) (p->next) - delta >= MINSIZE (p->next))
  1823.     {
  1824.       (*setsizefun) (p->next, (*sizefun) (p->next) - delta, 0);
  1825.       (*setsizefun) (window, *sizep + delta, 0);
  1826.       CURBEG (XWINDOW (p->next)) += delta;
  1827.       /* This does not change size of p->next,
  1828.      but it propagates the new top edge to its children */
  1829.       (*setsizefun) (p->next, (*sizefun) (p->next), 0);
  1830.     }
  1831.   else if (!NILP (p->prev) &&
  1832.        (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
  1833.     {
  1834.       (*setsizefun) (p->prev, (*sizefun) (p->prev) - delta, 0);
  1835.       CURBEG (p) -= delta;
  1836.       (*setsizefun) (window, *sizep + delta, 0);
  1837.     }
  1838.   else
  1839.     {
  1840.       register int delta1;
  1841.       register int opht = (*sizefun) (parent);
  1842.  
  1843.       /* If trying to grow this window to or beyond size of the parent,
  1844.      make delta1 so big that, on shrinking back down,
  1845.      all the siblings end up with less than one line and are deleted.  */
  1846.       if (opht <= *sizep + delta)
  1847.     delta1 = opht * opht * 2;
  1848.       /* Otherwise, make delta1 just right so that if we add delta1
  1849.      lines to this window and to the parent, and then shrink
  1850.      the parent back to its original size, the new proportional
  1851.      size of this window will increase by delta.  */
  1852.       else
  1853.     delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
  1854.  
  1855.       /* Add delta1 lines or columns to this window, and to the parent,
  1856.      keeping things consistent while not affecting siblings.  */
  1857.       CURSIZE (XWINDOW (parent)) = opht + delta1;
  1858.       (*setsizefun) (window, *sizep + delta1, 0);
  1859.  
  1860.       /* Squeeze out delta1 lines or columns from our parent,
  1861.      shriking this window and siblings proportionately.
  1862.      This brings parent back to correct size.
  1863.      Delta1 was calculated so this makes this window the desired size,
  1864.      taking it all out of the siblings.  */
  1865.       (*setsizefun) (parent, opht, 0);
  1866.     }
  1867.  
  1868.   XFASTINT (p->last_modified) = 0;
  1869.   XFASTINT (p->last_facechange) = 0;
  1870. }
  1871. #undef MINSIZE
  1872. #undef CURBEG
  1873. #undef CURSIZE
  1874.  
  1875.  
  1876. /* Return number of lines of text (not counting mode line) in W.  */
  1877.  
  1878. int
  1879. window_internal_height (w)
  1880.      struct window *w;
  1881. {
  1882.   int ht = XFASTINT (w->height);
  1883.  
  1884.   if (MINI_WINDOW_P (w))
  1885.     return ht;
  1886.  
  1887.   if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
  1888.       || !NILP (w->next) || !NILP (w->prev)
  1889.       || SCREEN_WANTS_MODELINE_P (XSCREEN (WINDOW_SCREEN (w))))
  1890.     return ht - 1;
  1891.  
  1892.   return ht;
  1893. }
  1894.  
  1895. /* Scroll contents of window WINDOW up N lines.  */
  1896.  
  1897. void
  1898. window_scroll (window, n)
  1899.      Lisp_Object window;
  1900.      int n;
  1901. {
  1902.   register struct window *w = XWINDOW (window);
  1903.   register int opoint = point;
  1904.   register int pos;
  1905.   register int ht = window_internal_height (w);
  1906.   register Lisp_Object tem;
  1907.   int lose;
  1908.   Lisp_Object bolp;
  1909.  
  1910.   XFASTINT (tem) = point;
  1911.   tem = Fpos_visible_in_window_p (tem, window);
  1912.  
  1913.   if (NILP (tem))
  1914.     {
  1915.       Fvertical_motion (make_number (- ht / 2), window);
  1916.       XFASTINT (tem) = point;
  1917.       Fset_marker (w->start, tem, w->buffer);
  1918.       w->force_start = Qt;
  1919.     }
  1920.  
  1921.   pos = marker_position (w->start);
  1922.  
  1923.   if (pos < BEGV)
  1924.     pos = BEGV;
  1925.   else if (pos > ZV)
  1926.     pos = ZV;
  1927.  
  1928.   SET_PT (pos);
  1929.   lose = n < 0 && point == BEGV;
  1930.   Fvertical_motion (make_number (n), window);
  1931.   pos = point;
  1932.   bolp = Fbolp ();
  1933.   SET_PT (opoint);
  1934.  
  1935.   if (lose)
  1936.     Fsignal (Qbeginning_of_buffer, Qnil);
  1937.  
  1938.   if (pos < ZV)
  1939. #if 0
  1940.       /* Allow scrolling to an empty screen (end of buffer)
  1941.      if that is exactly how far we wanted to go.  */
  1942.       || XINT (nmoved) == n)
  1943. #endif
  1944.     {
  1945.       set_marker_restricted (w->start, make_number (pos), w->buffer);
  1946.       w->start_at_line_beg = bolp;
  1947.       w->redo_mode_line = Qt;
  1948.       XFASTINT (w->last_modified) = 0;
  1949.       XFASTINT (w->last_facechange) = 0;
  1950.       if (pos > opoint)
  1951.     SET_PT (pos);
  1952.       if (n < 0)
  1953.     {
  1954.       SET_PT (pos);
  1955.       tem = Fvertical_motion (make_number (ht), window);
  1956.       if (point > opoint || XFASTINT (tem) < ht)
  1957.         SET_PT (opoint);
  1958.       else
  1959.         Fvertical_motion (make_number (-1), window);
  1960.     }
  1961.     }
  1962.   else
  1963.     Fsignal (Qend_of_buffer, Qnil);
  1964. }
  1965.  
  1966. /* This is the guts of Fscroll_up and Fscroll_down.  */
  1967.  
  1968. static void
  1969. scroll_command (n, direction)
  1970.      register Lisp_Object n;
  1971.      int direction;
  1972. {
  1973.   register int defalt;
  1974.   int count = specpdl_ptr - specpdl;
  1975.  
  1976.   /* If selected window's buffer isn't current, make it current for the moment.
  1977.      But don't screw up if window_scroll gets an error.  */
  1978.   if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
  1979.     {
  1980.       record_unwind_protect (save_excursion_restore, save_excursion_save ());
  1981.       Fset_buffer (XWINDOW (selected_window)->buffer);
  1982.     }
  1983.  
  1984.   defalt = (window_internal_height (XWINDOW (selected_window))
  1985.         - next_screen_context_lines);
  1986.   defalt = direction * (defalt < 1 ? 1 : defalt);
  1987.  
  1988.   if (NILP (n))
  1989.     window_scroll (selected_window, defalt);
  1990.   else if (EQ (n, Qminus))
  1991.     window_scroll (selected_window, - defalt);
  1992.   else
  1993.     {
  1994.       n = Fprefix_numeric_value (n);
  1995.       window_scroll (selected_window, XINT (n) * direction);
  1996.     }
  1997.  
  1998.   zmacs_region_stays = 1;
  1999.   unbind_to (count, Qnil);
  2000. }
  2001.  
  2002. DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
  2003.   "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
  2004. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  2005. When calling from a program, supply a number as argument or nil.")
  2006.   (n)
  2007.      Lisp_Object n;
  2008. {
  2009.   scroll_command (n, 1);
  2010.   return Qnil;
  2011. }
  2012.  
  2013. DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
  2014.   "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
  2015. A near full screen is `next-screen-context-lines' less than a full screen.\n\
  2016. When calling from a program, supply a number as argument or nil.")
  2017.   (n)
  2018.      Lisp_Object n;
  2019. {
  2020.   scroll_command (n, -1);
  2021.   return Qnil;
  2022. }
  2023.  
  2024. DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
  2025.   "Scroll text of next window upward ARG lines; or near full screen if no ARG.\n\
  2026. The next window is the one below the current one; or the one at the top\n\
  2027. if the current one is at the bottom.\n\
  2028. When calling from a program, supply a number as argument or nil.\n\
  2029. \n\
  2030. If in the minibuffer, `minibuf-scroll-window' if non-nil\n\
  2031. specifies the window to scroll.\n\
  2032. If `other-window-scroll-buffer' is non-nil, scroll the window\n\
  2033. showing that buffer, popping the buffer up if necessary.")
  2034.   (n)
  2035.      register Lisp_Object n;
  2036. {
  2037.   register Lisp_Object window;
  2038.   register int ht, pos;
  2039.   register struct window *w;
  2040.   register int count = specpdl_ptr - specpdl;
  2041.  
  2042.   if (MINI_WINDOW_P (XWINDOW (selected_window))
  2043.       && !NILP (Vminibuf_scroll_window))
  2044.     window = Vminibuf_scroll_window;
  2045.   /* If buffer is specified, scroll that buffer.  */
  2046.   else if (!NILP (Vother_window_scroll_buffer))
  2047.     {
  2048.       window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil, Qnil);
  2049.       if (NILP (window))
  2050.     window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
  2051.     }
  2052.   else
  2053.     /* Nothing specified; pick a neighboring window in this screen.  */
  2054.     window = Fnext_window (selected_window, Qnil, Qnil, Qnil);
  2055.   CHECK_WINDOW (window, 0);
  2056.  
  2057.   if (EQ (window, selected_window))
  2058.     error ("There is no other window");
  2059.  
  2060.   w = XWINDOW (window);
  2061.   ht = window_internal_height (w);
  2062.  
  2063.   /* Don't screw up if window_scroll gets an error.  */
  2064.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  2065.  
  2066.   Fset_buffer (w->buffer);
  2067.  
  2068.   pos = marker_position (w->pointm);
  2069.  
  2070.   if (pos < BEGV)
  2071.     pos = BEGV;
  2072.   else if (pos > ZV)
  2073.     pos = ZV;
  2074.  
  2075.   SET_PT (pos);
  2076.  
  2077.   if (NILP (n))
  2078.     window_scroll (window, ht - next_screen_context_lines);
  2079.   else if (EQ (n, Qminus))
  2080.     window_scroll (window, next_screen_context_lines - ht);
  2081.   else
  2082.     {
  2083.       if (CONSP (n))
  2084.     n = Fcar (n);
  2085.       CHECK_FIXNUM (n, 0);
  2086.       window_scroll (window, XINT (n));
  2087.     }
  2088.   Fset_marker (w->pointm, make_number (point), Qnil);
  2089.  
  2090.   zmacs_region_stays = 1;
  2091.   return unbind_to (count, Qnil);
  2092. }
  2093.  
  2094. DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 1, 1, "P",
  2095.   "Scroll selected window display ARG columns left.\n\
  2096. Default for ARG is window width minus 2.")
  2097.   (arg)
  2098.      register Lisp_Object arg;
  2099. {
  2100.   zmacs_region_stays = 1;
  2101.   if (NILP (arg))
  2102.     XFASTINT (arg) = XFASTINT (XWINDOW (selected_window)->width) - 2;
  2103.   else
  2104.     arg = Fprefix_numeric_value (arg);
  2105.  
  2106.   return
  2107.     Fset_window_hscroll (selected_window,
  2108.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  2109.                       + XINT (arg)));
  2110. }
  2111.  
  2112. DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 1, 1, "P",
  2113.   "Scroll selected window display ARG columns right.\n\
  2114. Default for ARG is window width minus 2.")
  2115.   (arg)
  2116.      register Lisp_Object arg;
  2117. {
  2118.   zmacs_region_stays = 1;
  2119.   if (NILP (arg))
  2120.     XFASTINT (arg) = XFASTINT (XWINDOW (selected_window)->width) - 2;
  2121.   else
  2122.     arg = Fprefix_numeric_value (arg);
  2123.  
  2124.   return
  2125.     Fset_window_hscroll (selected_window,
  2126.              make_number (XINT (XWINDOW (selected_window)->hscroll)
  2127.                       - XINT (arg)));
  2128. }
  2129.  
  2130. DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
  2131.   "Center point in window and redisplay screen.  With ARG, put point on line ARG.\n\
  2132. The desired position of point is always relative to the current window.\n\
  2133. Just C-u as prefix means put point in the center of the screen.\n\
  2134. No arg (i.e., it is nil) erases the entire screen and then\n\
  2135. redraws with point in the center.")
  2136.   (n)
  2137.      register Lisp_Object n;
  2138. {
  2139.   register struct window *w = XWINDOW (selected_window);
  2140.   register int ht = window_internal_height (w);
  2141.   register int opoint = point;
  2142.  
  2143.   if (NILP (n))
  2144.     {
  2145.       extern int screen_garbaged;
  2146.  
  2147.       SET_SCREEN_GARBAGED (XSCREEN (WINDOW_SCREEN (w)));
  2148.       XFASTINT (n) = ht / 2;
  2149.     }
  2150.   else if (CONSP (n)) /* Just C-u. */
  2151.     {
  2152.       XFASTINT (n) = ht / 2;
  2153.     }
  2154.   else
  2155.     {
  2156.       n = Fprefix_numeric_value (n);
  2157.       CHECK_FIXNUM (n, 0);
  2158.     }
  2159.  
  2160.   if (XINT (n) < 0)
  2161.     XSETINT (n, XINT (n) + ht);
  2162.  
  2163.   XSETINT (n, - XINT (n));
  2164.  
  2165.   Fvertical_motion (n, selected_window);
  2166.   Fset_marker (w->start, make_number (point), w->buffer);
  2167.   w->start_at_line_beg = Fbolp ();
  2168.  
  2169.   SET_PT (opoint);
  2170.   w->force_start = Qt;
  2171.   zmacs_region_stays = 1;
  2172.   return Qnil;
  2173. }
  2174.  
  2175. DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
  2176.   1, 1, "P",
  2177.   "Position point relative to window.\n\
  2178. With no argument, position text at center of window.\n\
  2179. An argument specifies screen line; zero means top of window,\n\
  2180. negative means relative to bottom of window.")
  2181.   (arg)
  2182.      register Lisp_Object arg;
  2183. {
  2184.   register struct window *w = XWINDOW (selected_window);
  2185.   register int height = window_internal_height (w);
  2186.   register int start;
  2187.  
  2188.   if (NILP (arg))
  2189.     XFASTINT (arg) = height / 2;
  2190.   else
  2191.     {
  2192.       arg = Fprefix_numeric_value (arg);
  2193.       if (XINT (arg) < 0)
  2194.     XSETINT (arg, XINT (arg) + height);
  2195.     }
  2196.  
  2197.   start = marker_position (w->start);
  2198.   if (start < BEGV || start > ZV)
  2199.     {
  2200.       Fvertical_motion (make_number (- height / 2), selected_window);
  2201.       Fset_marker (w->start, make_number (point), w->buffer);
  2202.       w->start_at_line_beg = Fbolp ();
  2203.       w->force_start = Qt;
  2204.     }
  2205.   else
  2206.     SET_PT (start);
  2207.  
  2208.   zmacs_region_stays = 1;
  2209.   return Fvertical_motion (arg, selected_window);
  2210. }
  2211.  
  2212. /* #### This window configuration stuff has had serious bugs lurking in it
  2213.    for years; it would be a -huge- win if this was reimplemented in lisp.
  2214.  */
  2215.  
  2216. struct save_window_data
  2217.   {
  2218.     int size_from_Lisp_Vector_struct;
  2219.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  2220.     Lisp_Object screen_width, screen_height;
  2221.     Lisp_Object current_window;
  2222.     Lisp_Object current_buffer;
  2223.     Lisp_Object minibuf_scroll_window;
  2224.     Lisp_Object root_window;
  2225.     /* A vector, interpreted as a struct saved_window */
  2226.     Lisp_Object saved_windows;
  2227. #ifdef ENERGIZE
  2228.     /* The buffer whose p_sheets are visible */
  2229.     Lisp_Object p_sheet_buffer;
  2230. #endif
  2231.   };
  2232.  
  2233. #ifndef ENERGIZE
  2234. #define SAVE_WINDOW_DATA_SIZE 7 /* Arg to Fmake_vector */
  2235. #else
  2236. #define SAVE_WINDOW_DATA_SIZE 8 /* Arg to Fmake_vector */
  2237. #endif
  2238.  
  2239. /* This is saved as a Lisp_Vector */
  2240. struct saved_window
  2241.   {
  2242.     /* these first two must agree with struct Lisp_Vector in lisp.h */
  2243.     int size_from_Lisp_Vector_struct;
  2244.     struct Lisp_Vector *next_from_Lisp_Vector_struct;
  2245.  
  2246.     Lisp_Object window;
  2247.     Lisp_Object buffer, start, pointm, mark;
  2248.     Lisp_Object left, top, width, height, hscroll;
  2249.     Lisp_Object parent, prev;
  2250.     Lisp_Object start_at_line_beg;
  2251.     Lisp_Object display_table;
  2252.     Lisp_Object    dedicated;
  2253.   };
  2254.  
  2255. #define SAVED_WINDOW_VECTOR_SIZE 15 /* Arg to Fmake_vector */
  2256.  
  2257. #define SAVED_WINDOW_N(swv,n) \
  2258.   ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
  2259.  
  2260. DEFUN ("set-window-configuration",
  2261.        Fset_window_configuration, Sset_window_configuration,
  2262.        1, 1, 0,
  2263.   "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
  2264. CONFIGURATION must be a value previously returned\n\
  2265. by `current-window-configuration' (which see).")
  2266.      (arg)
  2267.      Lisp_Object arg;
  2268. {
  2269.   register struct window *w;
  2270.   register struct save_window_data *data;
  2271.   struct Lisp_Vector *saved_windows;
  2272.   register struct saved_window *p;
  2273.   Lisp_Object new_current_buffer;
  2274.   int k;
  2275.   Lisp_Object screen;
  2276.   SCREEN_PTR s;
  2277.   struct gcpro gcpro1;
  2278.  
  2279.   GCPRO1 (arg);
  2280.  
  2281.   while (XTYPE (arg) != Lisp_Window_Configuration)
  2282.     {
  2283.       /* the function window-configuration-p isn't actually defined
  2284.      at present --- is there a need for it? */
  2285.       arg = wrong_type_argument (intern ("window-configuration-p"), arg);
  2286.     }
  2287.  
  2288.   data = (struct save_window_data *) XVECTOR (arg);
  2289.   saved_windows = XVECTOR (data->saved_windows);
  2290.  
  2291.   screen = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->screen;
  2292.   s = XSCREEN (screen);
  2293.  
  2294.   /* If the screen was deleted you can't restore the windows as they
  2295.      were deleted too.  Raise an error.  */
  2296.   if (s->display.nothing == 0)
  2297.     error ("Cannot set-window-configuration for a deleted screen");
  2298.  
  2299.     /* restore the screen characteristics */
  2300. #ifdef ENERGIZE
  2301.   if (SCREEN_IS_X (s))
  2302.     {
  2303.       extern Lisp_Object desired_psheet_buffer();
  2304.       extern void make_psheets_desired ();
  2305.     
  2306.       Lisp_Object new_desired = data->p_sheet_buffer;
  2307.     
  2308.       if (BUFFERP (new_desired) &&
  2309.       NILP (XBUFFER (new_desired)->name))
  2310.     new_desired = Qnil;    /* the desired buffer was killed */
  2311.     
  2312.       /* need to restore the desired buffer */
  2313.       if (new_desired != desired_psheet_buffer (s))
  2314.     make_psheets_desired (s, new_desired);
  2315.     }
  2316. #endif
  2317.     
  2318.   windows_or_buffers_changed++;
  2319.   new_current_buffer = data->current_buffer;
  2320.   if (NILP (XBUFFER (new_current_buffer)->name))
  2321.     new_current_buffer = Qnil;
  2322.  
  2323. #if 0
  2324.  *  /* This was NOT in 18 and I think it's useless --Matthieu. */
  2325.  *  /* Mark all windows now on screen as "deleted".
  2326.  *     Restoring the new configuration "undeletes" any that are in it.  */
  2327.  *
  2328.  *  delete_all_subwindows (XWINDOW (s->root_window));
  2329. #endif
  2330. #if 0
  2331.  *  /* This loses when the minibuf screen is not s. */
  2332.  *  delete_all_subwindows (XWINDOW (XWINDOW (minibuf_window)->prev));
  2333. #endif
  2334.  
  2335.   for (k = 0; k < saved_windows->size; k++)
  2336.     {
  2337.       p = SAVED_WINDOW_N (saved_windows, k);
  2338.       w = XWINDOW (p->window);
  2339.       w->next = Qnil;
  2340.  
  2341.       if (!NILP (p->parent))
  2342.     w->parent =
  2343.       SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window;
  2344.       else
  2345.     w->parent = Qnil;
  2346.  
  2347.       if (!NILP (p->prev))
  2348.     {
  2349.       w->prev = SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window;
  2350. #ifdef MULTI_SCREEN
  2351.       /* This is true for a minibuffer-only screen. */
  2352.       if (w->mini_p && EQ (w->prev, p->window))
  2353.         w->next = Qnil;
  2354.       else
  2355. #endif    /* MULTI_SCREEN */
  2356.         XWINDOW (w->prev)->next = p->window;
  2357.     }
  2358.       else
  2359.     {
  2360.       w->prev = Qnil;
  2361.       if (!NILP (w->parent))
  2362.         {
  2363.           if (EQ (p->width, XWINDOW (w->parent)->width))
  2364.         {
  2365.           XWINDOW (w->parent)->vchild = p->window;
  2366.           XWINDOW (w->parent)->hchild = Qnil;
  2367.         }
  2368.           else
  2369.         {
  2370.           XWINDOW (w->parent)->hchild = p->window;
  2371.           XWINDOW (w->parent)->vchild = Qnil;
  2372.         }
  2373.         }
  2374.     }
  2375.       w->left = p->left;
  2376.       w->top = p->top;
  2377.       w->width = p->width;
  2378.       w->height = p->height;
  2379.       w->hscroll = p->hscroll;
  2380.       w->display_table = p->display_table;
  2381.       w->dedicated = p->dedicated;
  2382.       XFASTINT (w->last_modified) = 0;
  2383.       XFASTINT (w->last_facechange) = 0;
  2384.  
  2385.       /* Reinstall the saved buffer and pointers into it.  */
  2386.       if (NILP (p->buffer))
  2387.     w->buffer = p->buffer;
  2388.       else
  2389.     {
  2390.       if (!NILP (XBUFFER (p->buffer)->name))
  2391.         /* If saved buffer is alive, install it.  */
  2392.         {
  2393.           w->buffer = p->buffer;
  2394.           w->start_at_line_beg = p->start_at_line_beg;
  2395.           set_marker_restricted (w->start, Fmarker_position (p->start), w->buffer);
  2396.           set_marker_restricted (w->pointm, Fmarker_position (p->pointm), w->buffer);
  2397.           Fset_marker (XBUFFER (w->buffer)->mark,
  2398.                Fmarker_position (p->mark), w->buffer);
  2399.  
  2400.           if (!EQ (p->buffer, new_current_buffer) &&
  2401.           XBUFFER (p->buffer) == current_buffer)
  2402.         Fgoto_char (w->pointm);
  2403.         }
  2404.       else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
  2405.         /* Else if window's old buffer is dead too, get a live one.  */
  2406.         {
  2407.           /* ## The following line makes me nervous... */
  2408.            /* w->buffer = Fcdr (Fcar (XSCREEN (w->screen)->buffer_alist));*/
  2409.           w->buffer = Fget_buffer_create (build_string ("*scratch*"));
  2410.            /* w->buffer = Fother_buffer (Qnil, w->screen); */
  2411.           /* This will set the markers to beginning of visible range.  */
  2412.           set_marker_restricted (w->start, make_number (0), w->buffer);
  2413.           set_marker_restricted (w->pointm, make_number (0), w->buffer);
  2414.           w->start_at_line_beg = Qt;
  2415.         }
  2416.       else
  2417.         /* Keeping window's old buffer; make sure the markers are real.  */
  2418.         {
  2419.           /* Set window markers at start of visible range.  */
  2420.           if (XMARKER (w->start)->buffer == 0)
  2421.         set_marker_restricted (w->start, make_number (0), w->buffer);
  2422.           if (XMARKER (w->pointm)->buffer == 0)
  2423.         set_marker_restricted (w->pointm,
  2424.                        make_number (BUF_PT (XBUFFER (w->buffer))),
  2425.                        w->buffer);
  2426.           w->start_at_line_beg = Qt;
  2427.         }
  2428.     }
  2429.     }
  2430.  
  2431.  
  2432.   SCREEN_ROOT_WINDOW (s) = data->root_window;
  2433.  
  2434.   if (XFASTINT (data->screen_height) != SCREEN_HEIGHT (s)
  2435.       || XFASTINT (data->screen_width) != SCREEN_WIDTH (s))
  2436.     {
  2437.       int current_height = SCREEN_HEIGHT (s);
  2438.       int current_width = SCREEN_WIDTH (s);
  2439.       SCREEN_HEIGHT (s) = 0;
  2440.       SCREEN_WIDTH (s) = 0;
  2441.       change_screen_size (s, current_height, current_width, 0);
  2442.     }
  2443.  
  2444. #if 0
  2445.  * /* Selecting the window (below) will also select the screen.  If we call
  2446.  *    Fselect_screen now it will try to select whatever is in the 
  2447.  *    selected_window slot of the screen, which most likely is a deleted window
  2448.  *    which would be pretty bad. */
  2449.  * #ifdef MULTI_SCREEN
  2450.  *  if (s != selected_screen && ! SCREEN_IS_TERMCAP (s))
  2451.  *    Fselect_screen (screen, Qnil);
  2452.  *#endif
  2453. #endif
  2454.  
  2455.    /* If restoring in the current screen make the window current, otherwise
  2456.       just update the screen selected_window slot to be the restored
  2457.       current_window. */
  2458.   if (s == selected_screen)
  2459.     {
  2460.       Fselect_window (data->current_window);
  2461.       if (!NILP (new_current_buffer))
  2462.     Fset_buffer (new_current_buffer);
  2463.       else
  2464.     Fset_buffer (XWINDOW (selected_window)->buffer);
  2465. #ifdef ENERGIZE
  2466.       energize_buffer_shown_hook (XWINDOW(selected_window));
  2467. #endif
  2468.     }
  2469.   else
  2470.     s->selected_window = data->current_window;
  2471.  
  2472.   Vminibuf_scroll_window = data->minibuf_scroll_window;
  2473.  
  2474.   UNGCPRO;
  2475.  
  2476.   return (Qnil);
  2477. }
  2478.  
  2479. /* Mark all windows now on screen as deleted
  2480.    by setting their buffers to nil.  */
  2481.  
  2482. static void
  2483. delete_all_subwindows (w)
  2484.      register struct window *w;
  2485. {
  2486.   w->buffer = Qnil;
  2487.   if (!NILP (w->next))
  2488.     delete_all_subwindows (XWINDOW (w->next));
  2489.   if (!NILP (w->vchild))
  2490.     delete_all_subwindows (XWINDOW (w->vchild));
  2491.   if (!NILP (w->hchild))
  2492.     delete_all_subwindows (XWINDOW (w->hchild));
  2493. }
  2494.  
  2495. static int
  2496. count_windows (window)
  2497.      register struct window *window;
  2498. {
  2499.   register int count = 1;
  2500.   if (!NILP (window->next))
  2501.     count += count_windows (XWINDOW (window->next));
  2502.   if (!NILP (window->vchild))
  2503.     count += count_windows (XWINDOW (window->vchild));
  2504.   if (!NILP (window->hchild))
  2505.     count += count_windows (XWINDOW (window->hchild));
  2506.   return count;
  2507. }
  2508.  
  2509. static int
  2510. save_window_save (window, vector, i)
  2511.      Lisp_Object window;
  2512.      struct Lisp_Vector *vector;
  2513.      int i;
  2514. {
  2515.   register struct saved_window *p;
  2516.   register struct window *w;
  2517.   register Lisp_Object tem;
  2518.  
  2519.   for (;!NILP (window); window = w->next)
  2520.     {
  2521.       p = SAVED_WINDOW_N (vector, i);
  2522.       w = XWINDOW (window);
  2523.  
  2524.       XFASTINT (w->temslot) = i++;
  2525.       p->window = window;
  2526.       p->buffer = w->buffer;
  2527.       p->left = w->left;
  2528.       p->top = w->top;
  2529.       p->width = w->width;
  2530.       p->height = w->height;
  2531.       p->hscroll = w->hscroll;
  2532.       p->display_table = w->display_table;
  2533.       if (!NILP (w->buffer))
  2534.     {
  2535. /* jwz: installed this patch from Jim Blandy */
  2536. #if 0
  2537.       if (EQ (window, selected_window)
  2538.           && XBUFFER (w->buffer) == current_buffer)
  2539.         p->pointm = Fpoint_marker ();
  2540. #else
  2541.       /* Save w's value of point in the window configuration.
  2542.          If w is the selected window, then get the value of point
  2543.          from the buffer; pointm is garbage in the selected window.  */
  2544.       if (EQ (window, selected_window))
  2545.         {
  2546.           p->pointm = Fmake_marker ();
  2547.           Fset_marker (p->pointm, BUF_PT (XBUFFER (w->buffer)),
  2548.                w->buffer);
  2549.         }
  2550. #endif
  2551.       else
  2552.         p->pointm = Fcopy_marker (w->pointm);
  2553.  
  2554.       p->start = Fcopy_marker (w->start);
  2555.       p->start_at_line_beg = w->start_at_line_beg;
  2556.  
  2557.       tem = XBUFFER (w->buffer)->mark;
  2558.       p->mark = Fcopy_marker (tem);
  2559.     }
  2560.       else
  2561.     {
  2562.       p->pointm = Qnil;
  2563.       p->start = Qnil;
  2564.       p->mark = Qnil;
  2565.       p->start_at_line_beg = Qnil;
  2566.     }
  2567.  
  2568.       if (NILP (w->parent))
  2569.     p->parent = Qnil;
  2570.       else
  2571.     p->parent = XWINDOW (w->parent)->temslot;
  2572.  
  2573.       if (NILP (w->prev))
  2574.     p->prev = Qnil;
  2575.       else
  2576.     p->prev = XWINDOW (w->prev)->temslot;
  2577.  
  2578.       p->dedicated = w->dedicated;
  2579.       if (!NILP (w->vchild))
  2580.     i = save_window_save (w->vchild, vector, i);
  2581.       if (!NILP (w->hchild))
  2582.     i = save_window_save (w->hchild, vector, i);
  2583.     }
  2584.  
  2585.   return i;
  2586. }
  2587.  
  2588. DEFUN ("current-window-configuration",
  2589.     Fcurrent_window_configuration, Scurrent_window_configuration, 0, 0, 0,
  2590.        "Return an object representing Emacs' current window configuration.\n\
  2591. This describes the number of windows, their sizes and current buffers,\n\
  2592. and for each displayed buffer, where display starts, and the positions of\n\
  2593. point and mark.  An exception is made for point in the current buffer:\n\
  2594. its value is -not- saved.")
  2595.   ()
  2596. {
  2597.   register Lisp_Object tem;
  2598.   register int n_windows;
  2599.   register struct save_window_data *data;
  2600.   register int i;
  2601.  
  2602.   n_windows = count_windows (XWINDOW (SCREEN_ROOT_WINDOW (selected_screen)));
  2603.   data = (struct save_window_data *)
  2604.            XVECTOR (Fmake_vector (make_number (SAVE_WINDOW_DATA_SIZE),
  2605.                   Qnil));
  2606.   XFASTINT (data->screen_width) = SCREEN_WIDTH (selected_screen);
  2607.   XFASTINT (data->screen_height) = SCREEN_HEIGHT (selected_screen);
  2608.   data->current_window = selected_window;
  2609.   XSET (data->current_buffer, Lisp_Buffer, current_buffer);
  2610.   data->minibuf_scroll_window = Vminibuf_scroll_window;
  2611.   data->root_window = SCREEN_ROOT_WINDOW (selected_screen);
  2612.   tem = Fmake_vector (make_number (n_windows), Qnil);
  2613.   data->saved_windows = tem;
  2614.   for (i = 0; i < n_windows; i++)
  2615.     XVECTOR (tem)->contents[i]
  2616.       = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
  2617.   save_window_save (SCREEN_ROOT_WINDOW (selected_screen),
  2618.             XVECTOR (tem), 0);
  2619. #ifdef ENERGIZE
  2620.   {
  2621.     extern Lisp_Object desired_psheet_buffer();
  2622.     data->p_sheet_buffer = desired_psheet_buffer (selected_screen);
  2623.     if (!data->p_sheet_buffer)
  2624.       data->p_sheet_buffer = Qnil;
  2625.   }
  2626. #endif
  2627.   XSET (tem, Lisp_Window_Configuration, data);
  2628.   return (tem);
  2629. }
  2630.  
  2631. DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
  2632.   0, UNEVALLED, 0,
  2633.   "Execute body, preserving window sizes and contents.\n\
  2634. Restores which buffer appears in which window, where display starts,\n\
  2635. as well as the current buffer.\n\
  2636. Does not restore the value of point in current buffer.")
  2637.   (args)
  2638.      Lisp_Object args;
  2639. {
  2640.   register Lisp_Object val;
  2641.   register int count = specpdl_depth;
  2642.  
  2643.   record_unwind_protect (Fset_window_configuration,
  2644.              Fcurrent_window_configuration ());
  2645.   val = Fprogn (args);
  2646.   return unbind_to (count, val);
  2647. }
  2648.  
  2649. void
  2650. init_window_once ()
  2651. {
  2652. #ifdef MULTI_SCREEN
  2653.   selected_screen = make_terminal_screen ();
  2654.   XSET (Vterminal_screen, Lisp_Screen, selected_screen);
  2655.   minibuf_window = selected_screen->minibuffer_window;
  2656.   selected_window = selected_screen->selected_window;
  2657.   Vscreen_list = Fcons (Vterminal_screen, Qnil);
  2658. #else /* not MULTI_SCREEN */
  2659.   extern Lisp_Object get_minibuffer ();
  2660.  
  2661.   root_window = make_window (0);
  2662.   minibuf_window = make_window (0);
  2663.  
  2664.   XWINDOW (root_window)->next = minibuf_window;
  2665.   XWINDOW (minibuf_window)->prev = root_window;
  2666.  
  2667.   /* These values 9 and 10 are arbitrary,
  2668.      just so that there is "something there."
  2669.      Correct values are put in in init_xdisp */
  2670.  
  2671.   XFASTINT (XWINDOW (root_window)->width) = 10;
  2672.   XFASTINT (XWINDOW (minibuf_window)->width) = 10;
  2673.  
  2674.   XFASTINT (XWINDOW (root_window)->height) = 9;
  2675.   XFASTINT (XWINDOW (minibuf_window)->top) = 9;
  2676.   XFASTINT (XWINDOW (minibuf_window)->height) = 1;
  2677.  
  2678.   /* Prevent error in Fset_window_buffer.  */
  2679.   XWINDOW (root_window)->buffer = Qt;
  2680.   XWINDOW (minibuf_window)->buffer = Qt;
  2681.  
  2682.   /* Now set them up for real.  */
  2683.   Fset_window_buffer (root_window, Fcurrent_buffer ());
  2684.   Fset_window_buffer (minibuf_window, get_minibuffer (0));
  2685.  
  2686.   selected_window = root_window;
  2687. #endif /* not MULTI_SCREEN */
  2688. }
  2689.  
  2690. void
  2691. syms_of_window ()
  2692. {
  2693.   Qwindowp = intern ("windowp");
  2694.   staticpro (&Qwindowp);
  2695.  
  2696.   /* Make sure all windows get marked */
  2697.   staticpro (&minibuf_window);
  2698.  
  2699.   DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
  2700.     "Non-nil means call as function to display a help buffer.\n\
  2701. Used by `with-output-to-temp-buffer'.");
  2702.   Vtemp_buffer_show_function = Qnil;
  2703.  
  2704.   DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
  2705.     "If non-nil, function to call to handle `display-buffer'.\n\
  2706. It will receive three args, the same args as display-buffer.");
  2707.   Vdisplay_buffer_function = Qnil;
  2708.  
  2709.   DEFVAR_LISP ("pre-display-buffer-function", &Vpre_display_buffer_function,
  2710.     "If non-nil, function that will be called from `display-buffer' as the\n\
  2711. first action.  It will receive three args, the same arguments as \n\
  2712. `display-buffer' -- the buffer, a flag which if non-nil means that the\n\
  2713. currently selected window is not acceptable, and a screen to use (or nil,\n\
  2714. meaning unspecified.)  This function may be used to select an appropriate\n\
  2715. screen for the buffer, for example.  See also the variable\n\
  2716. `display-buffer-function', which may be used to completely replace\n\
  2717. display-buffer.");
  2718.   Vpre_display_buffer_function = Qnil;
  2719.  
  2720.   DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
  2721.     "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
  2722.   Vminibuf_scroll_window = Qnil;
  2723.  
  2724.   DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
  2725.     "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
  2726.   Vother_window_scroll_buffer = Qnil;
  2727.  
  2728.   DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
  2729.     "*Non-nil means display-buffer should make new windows.");
  2730.   pop_up_windows = 1;
  2731.  
  2732.   DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
  2733.     "*Number of lines of continuity when scrolling by screenfuls.");
  2734.   next_screen_context_lines = 2;
  2735.  
  2736.   DEFVAR_INT ("split-height-threshold", &split_height_threshold,
  2737.     "*display-buffer would prefer to split the largest window if this large.\n\
  2738. If there is only one window, it is split regardless of this value.");
  2739.   split_height_threshold = 500;
  2740.  
  2741.   DEFVAR_INT ("window-min-height", &window_min_height,
  2742.     "*Delete any window less than this tall (including its mode line).");
  2743.   window_min_height = 4;
  2744.  
  2745.   DEFVAR_INT ("window-min-width", &window_min_width,
  2746.     "*Delete any window less than this wide.");
  2747.   window_min_width = 10;
  2748.  
  2749.   defsubr (&Sselected_window);
  2750.   defsubr (&Sminibuffer_window);
  2751.   defsubr (&Swindow_minibuffer_p);
  2752.   defsubr (&Swindowp);
  2753.   defsubr (&Spos_visible_in_window_p);
  2754.   defsubr (&Swindow_buffer);
  2755.   defsubr (&Swindow_height);
  2756.   defsubr (&Swindow_width);
  2757.   defsubr (&Swindow_hscroll);
  2758.   defsubr (&Sset_window_hscroll);
  2759.   defsubr (&Swindow_edges);
  2760.   defsubr (&Swindow_point);
  2761.   defsubr (&Swindow_start);
  2762.   defsubr (&Swindow_end);
  2763.   defsubr (&Sset_window_point);
  2764.   defsubr (&Sset_window_start);
  2765.   defsubr (&Swindow_dedicated_p);
  2766.   defsubr (&Sset_window_buffer_dedicated);
  2767.   defsubr (&Swindow_display_table);
  2768.   defsubr (&Sset_window_display_table);
  2769.   defsubr (&Snext_window);
  2770.   defsubr (&Sprevious_window);
  2771.   defsubr (&Sother_window);
  2772.   defsubr (&Sget_lru_window);
  2773.   defsubr (&Sget_largest_window);
  2774.   defsubr (&Sget_buffer_window);
  2775.   defsubr (&Sdelete_other_windows);
  2776.   defsubr (&Sdelete_windows_on);
  2777.   defsubr (&Sreplace_buffer_in_windows);
  2778.   defsubr (&Sdelete_window);
  2779.   defsubr (&Sset_window_buffer);
  2780.   defsubr (&Sselect_window);
  2781.   defsubr (&Sdisplay_buffer);
  2782.   defsubr (&Ssplit_window);
  2783.   defsubr (&Senlarge_window);
  2784.   defsubr (&Sshrink_window);
  2785.   defsubr (&Sscroll_up);
  2786.   defsubr (&Sscroll_down);
  2787.   defsubr (&Sscroll_left);
  2788.   defsubr (&Sscroll_right);
  2789.   defsubr (&Sscroll_other_window);
  2790.   defsubr (&Srecenter);
  2791.   defsubr (&Smove_to_window_line);
  2792.   defsubr (&Sset_window_configuration);
  2793.   defsubr (&Scurrent_window_configuration);
  2794.   defsubr (&Ssave_window_excursion);
  2795. }
  2796.