home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / alt / lucidem / bug / 324 < prev    next >
Encoding:
Text File  |  1992-11-06  |  78.3 KB  |  2,781 lines

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