New package cal-html.el.
[bpt/emacs.git] / src / window.c
CommitLineData
7ab12479
JB
1/* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
203eb0aa 3 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
aaef169d 4 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
7ab12479
JB
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
1113d9db 10the Free Software Foundation; either version 2, or (at your option)
7ab12479
JB
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
7ab12479 22
18160b98 23#include <config.h>
7ab12479
JB
24#include "lisp.h"
25#include "buffer.h"
3e4731a3 26#include "keyboard.h"
e35f6ff7 27#include "keymap.h"
44fa5b1e 28#include "frame.h"
7ab12479
JB
29#include "window.h"
30#include "commands.h"
31#include "indent.h"
32#include "termchar.h"
33#include "disptab.h"
dfcf069d 34#include "dispextern.h"
5500c422
GM
35#include "blockinput.h"
36#include "intervals.h"
37
6d55d620 38#ifdef HAVE_X_WINDOWS
dfcf069d 39#include "xterm.h"
5500c422 40#endif /* HAVE_X_WINDOWS */
8f23f280
AI
41#ifdef WINDOWSNT
42#include "w32term.h"
43#endif
1e3c8885
EZ
44#ifdef MSDOS
45#include "msdos.h"
46#endif
e0f712ba 47#ifdef MAC_OS
1a578e9b
AC
48#include "macterm.h"
49#endif
5500c422 50
5500c422 51
3f8ab7bd 52Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
c525d842 53Lisp_Object Qscroll_up, Qscroll_down;
90647b07 54Lisp_Object Qwindow_size_fixed;
49b996e7 55extern Lisp_Object Qleft_margin, Qright_margin;
7ab12479 56
521b203e 57static int displayed_window_lines P_ ((struct window *));
5e14b1fc 58static struct window *decode_window P_ ((Lisp_Object));
5500c422
GM
59static int count_windows P_ ((struct window *));
60static int get_leaf_windows P_ ((struct window *, struct window **, int));
61static void window_scroll P_ ((Lisp_Object, int, int, int));
62static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
63static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
233a4a2c 64static int window_min_size_1 P_ ((struct window *, int));
f984d4fc 65static int window_min_size P_ ((struct window *, int, int, int *));
5fe0b054 66static void size_window P_ ((Lisp_Object, int, int, int, int, int));
f95464e4 67static int freeze_window_start P_ ((struct window *, void *));
f984d4fc 68static int window_fixed_size_p P_ ((struct window *, int, int));
56ebfae2 69static void enlarge_window P_ ((Lisp_Object, int, int));
67492200 70static Lisp_Object window_list P_ ((void));
f95464e4 71static int add_window_to_list P_ ((struct window *, void *));
118ea242
GM
72static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
73 Lisp_Object));
67492200
GM
74static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
75 Lisp_Object, int));
76static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
77 Lisp_Object *));
f95464e4
GM
78static int foreach_window_1 P_ ((struct window *,
79 int (* fn) (struct window *, void *),
80 void *));
cd2904bd 81static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
b7354ddf 82
7ab12479
JB
83/* This is the window in which the terminal's cursor should
84 be left when nothing is being done with it. This must
85 always be a leaf window, and its buffer is selected by
86 the top level editing loop at the end of each command.
87
88 This value is always the same as
44fa5b1e 89 FRAME_SELECTED_WINDOW (selected_frame). */
7ab12479
JB
90
91Lisp_Object selected_window;
92
67492200
GM
93/* A list of all windows for use by next_window and Fwindow_list.
94 Functions creating or deleting windows should invalidate this cache
95 by setting it to nil. */
96
97Lisp_Object Vwindow_list;
98
5500c422
GM
99/* The mini-buffer window of the selected frame.
100 Note that you cannot test for mini-bufferness of an arbitrary window
101 by comparing against this; but you can test for mini-bufferness of
7ab12479 102 the selected window. */
5500c422 103
7ab12479
JB
104Lisp_Object minibuf_window;
105
3f49fddc
KS
106/* Non-nil means it is the window whose mode line should be
107 shown as the selected window when the minibuffer is selected. */
108
3dbab091 109Lisp_Object minibuf_selected_window;
3f49fddc 110
7ab12479 111/* Non-nil means it is the window for C-M-v to scroll
5500c422
GM
112 when the mini-buffer is selected. */
113
7ab12479
JB
114Lisp_Object Vminibuf_scroll_window;
115
116/* Non-nil means this is the buffer whose window C-M-v should scroll. */
5500c422 117
7ab12479
JB
118Lisp_Object Vother_window_scroll_buffer;
119
7ab12479 120/* Non-nil means it's function to call to display temp buffers. */
5500c422 121
7ab12479
JB
122Lisp_Object Vtemp_buffer_show_function;
123
e56263e5
KS
124/* Non-zero means line and page scrolling on tall lines (with images)
125 does partial scrolling by modifying window-vscroll. */
126
127int auto_window_vscroll_p;
128
cc91894c
KS
129/* Non-zero means to use mode-line-inactive face in all windows but the
130 selected-window and the minibuffer-scroll-window when the
131 minibuffer is active. */
132int mode_line_in_non_selected_windows;
26124d5e 133
7ab12479 134/* If a window gets smaller than either of these, it is removed. */
5500c422 135
31ade731
SM
136EMACS_INT window_min_height;
137EMACS_INT window_min_width;
7ab12479
JB
138
139/* Nonzero implies Fdisplay_buffer should create windows. */
5500c422 140
7ab12479
JB
141int pop_up_windows;
142
44fa5b1e 143/* Nonzero implies make new frames for Fdisplay_buffer. */
5500c422 144
44fa5b1e 145int pop_up_frames;
7ab12479 146
9c3da604
GM
147/* Nonzero means reuse existing frames for displaying buffers. */
148
149int display_buffer_reuse_frames;
150
7ab12479 151/* Non-nil means use this function instead of default */
5500c422 152
44fa5b1e 153Lisp_Object Vpop_up_frame_function;
7ab12479
JB
154
155/* Function to call to handle Fdisplay_buffer. */
5500c422 156
7ab12479
JB
157Lisp_Object Vdisplay_buffer_function;
158
6529ed87
GM
159/* Non-nil means that Fdisplay_buffer should even the heights of windows. */
160
161Lisp_Object Veven_window_heights;
162
a90712c2 163/* List of buffer *names* for buffers that should have their own frames. */
5500c422 164
a90712c2
RS
165Lisp_Object Vspecial_display_buffer_names;
166
167/* List of regexps for buffer names that should have their own frames. */
5500c422 168
a90712c2
RS
169Lisp_Object Vspecial_display_regexps;
170
171/* Function to pop up a special frame. */
5500c422 172
a90712c2
RS
173Lisp_Object Vspecial_display_function;
174
855d8627 175/* List of buffer *names* for buffers to appear in selected window. */
5500c422 176
855d8627
RS
177Lisp_Object Vsame_window_buffer_names;
178
179/* List of regexps for buffer names to appear in selected window. */
5500c422 180
855d8627
RS
181Lisp_Object Vsame_window_regexps;
182
a58ec57d 183/* Hook run at end of temp_output_buffer_show. */
5500c422 184
a58ec57d
RS
185Lisp_Object Qtemp_buffer_show_hook;
186
37962e60 187/* Fdisplay_buffer always splits the largest window
7ab12479 188 if that window is more than this high. */
5500c422 189
31ade731 190EMACS_INT split_height_threshold;
7ab12479
JB
191
192/* Number of lines of continuity in scrolling by screenfuls. */
5500c422 193
31ade731 194EMACS_INT next_screen_context_lines;
7ab12479
JB
195
196/* Incremented for each window created. */
5500c422 197
7ab12479
JB
198static int sequence_number;
199
5b03d3c0 200/* Nonzero after init_window_once has finished. */
5500c422 201
5b03d3c0
RS
202static int window_initialized;
203
543f5fb1 204/* Hook to run when window config changes. */
5500c422 205
543f5fb1
RS
206Lisp_Object Qwindow_configuration_change_hook;
207Lisp_Object Vwindow_configuration_change_hook;
208
d0c38d63 209/* Non-nil means scroll commands try to put point
9317a85d 210 at the same screen height as previously. */
9317a85d 211
5500c422
GM
212Lisp_Object Vscroll_preserve_screen_position;
213
0d384044
RS
214/* Incremented by 1 whenever a window is deleted. */
215
216int window_deletion_count;
217
66d43aea
LT
218/* Used by the function window_scroll_pixel_based */
219
66fe93d1 220static int window_scroll_pixel_based_preserve_y;
66d43aea 221
5500c422 222#if 0 /* This isn't used anywhere. */
91a78190 223/* Nonzero means we can split a frame even if it is "unsplittable". */
eddd51c2 224static int inhibit_frame_unsplittable;
5500c422 225#endif /* 0 */
7d601aaa 226
31ade731 227extern EMACS_INT scroll_margin;
9317a85d 228
dba06815 229extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
7ab12479
JB
230\f
231DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
fdb82f93
PJ
232 doc: /* Returns t if OBJECT is a window. */)
233 (object)
413430c5 234 Lisp_Object object;
7ab12479 235{
413430c5 236 return WINDOWP (object) ? Qt : Qnil;
7ab12479
JB
237}
238
806b4d9b 239DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
fdb82f93 240 doc: /* Returns t if OBJECT is a window which is currently visible. */)
413430c5
EN
241 (object)
242 Lisp_Object object;
605be8af 243{
9e571f49 244 return WINDOW_LIVE_P (object) ? Qt : Qnil;
605be8af
JB
245}
246
7ab12479
JB
247Lisp_Object
248make_window ()
249{
cffec418 250 Lisp_Object val;
7ab12479 251 register struct window *p;
cffec418 252
26605be9 253 p = allocate_window ();
2a1893f4
SM
254 ++sequence_number;
255 XSETFASTINT (p->sequence_number, sequence_number);
949cf20f
KS
256 XSETFASTINT (p->left_col, 0);
257 XSETFASTINT (p->top_line, 0);
258 XSETFASTINT (p->total_lines, 0);
259 XSETFASTINT (p->total_cols, 0);
d834a2e9 260 XSETFASTINT (p->hscroll, 0);
ea68264b 261 XSETFASTINT (p->min_hscroll, 0);
949cf20f 262 p->orig_top_line = p->orig_total_lines = Qnil;
7ab12479
JB
263 p->start = Fmake_marker ();
264 p->pointm = Fmake_marker ();
d834a2e9 265 XSETFASTINT (p->use_time, 0);
44fa5b1e 266 p->frame = Qnil;
7ab12479
JB
267 p->display_table = Qnil;
268 p->dedicated = Qnil;
5500c422
GM
269 p->pseudo_window_p = 0;
270 bzero (&p->cursor, sizeof (p->cursor));
271 bzero (&p->last_cursor, sizeof (p->last_cursor));
272 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
273 p->desired_matrix = p->current_matrix = 0;
7a146de2 274 p->nrows_scale_factor = p->ncols_scale_factor = 1;
5500c422 275 p->phys_cursor_type = -1;
4b26579e 276 p->phys_cursor_width = -1;
5500c422
GM
277 p->must_be_updated_p = 0;
278 XSETFASTINT (p->window_end_vpos, 0);
279 XSETFASTINT (p->window_end_pos, 0);
280 p->window_end_valid = Qnil;
281 p->vscroll = 0;
cffec418 282 XSETWINDOW (val, p);
5500c422 283 XSETFASTINT (p->last_point, 0);
7bbb5782 284 p->frozen_window_start_p = 0;
6b448755 285 p->last_cursor_off_p = p->cursor_off_p = 0;
949cf20f
KS
286 p->left_margin_cols = Qnil;
287 p->right_margin_cols = Qnil;
288 p->left_fringe_width = Qnil;
289 p->right_fringe_width = Qnil;
290 p->fringes_outside_margins = Qnil;
291 p->scroll_bar_width = Qnil;
292 p->vertical_scroll_bar_type = Qt;
67492200
GM
293
294 Vwindow_list = Qnil;
7ab12479
JB
295 return val;
296}
297
298DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
fdb82f93
PJ
299 doc: /* Return the window that the cursor now appears in and commands apply to. */)
300 ()
7ab12479
JB
301{
302 return selected_window;
303}
304
83762ba4 305DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
fdb82f93
PJ
306 doc: /* Return the window used now for minibuffers.
307If the optional argument FRAME is specified, return the minibuffer window
308used by that frame. */)
309 (frame)
83762ba4 310 Lisp_Object frame;
7ab12479 311{
83762ba4 312 if (NILP (frame))
1ae1a37d 313 frame = selected_frame;
b7826503 314 CHECK_LIVE_FRAME (frame);
83762ba4 315 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
7ab12479
JB
316}
317
605be8af 318DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
29a831f8
LT
319 doc: /* Returns non-nil if WINDOW is a minibuffer window.
320WINDOW defaults to the selected window. */)
fdb82f93 321 (window)
7ab12479
JB
322 Lisp_Object window;
323{
324 struct window *w = decode_window (window);
53bb6b99 325 return MINI_WINDOW_P (w) ? Qt : Qnil;
81e4d465
MB
326}
327
328
7ab12479 329DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
fdb82f93 330 Spos_visible_in_window_p, 0, 3, 0,
a0a37a6f 331 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
fdb82f93
PJ
332Return nil if that position is scrolled vertically out of view.
333If a character is only partially visible, nil is returned, unless the
334optional argument PARTIALLY is non-nil.
a0a37a6f 335If POS is only out of view because of horizontal scrolling, return non-nil.
120b7781 336If POS is t, it specifies the position of the last visible glyph in WINDOW.
0cc1039f
KS
337POS defaults to point in WINDOW; WINDOW defaults to the selected window.
338
339If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
5a857365
KS
340return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
341where X and Y are the pixel coordinates relative to the top left corner
342of the window. The remaining elements are omitted if the character after
343POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
120b7781 344off-window at the top and bottom of the row, ROWH is the height of the
7bbc67d2 345display row, and VPOS is the row number (0-based) containing POS. */)
fdb82f93 346 (pos, window, partially)
5cdb3cf3 347 Lisp_Object pos, window, partially;
7ab12479
JB
348{
349 register struct window *w;
7ab12479
JB
350 register int posint;
351 register struct buffer *buf;
9a132b1f 352 struct text_pos top;
0cc1039f 353 Lisp_Object in_window = Qnil;
5a857365 354 int rtop, rbot, rowh, vpos, fully_p = 1;
0cc1039f 355 int x, y;
7ab12479 356
f28b75a9
MB
357 w = decode_window (window);
358 buf = XBUFFER (w->buffer);
359 SET_TEXT_POS_FROM_MARKER (top, w->start);
360
120b7781
KS
361 if (EQ (pos, Qt))
362 posint = -1;
363 else if (!NILP (pos))
7ab12479 364 {
b7826503 365 CHECK_NUMBER_COERCE_MARKER (pos);
7ab12479
JB
366 posint = XINT (pos);
367 }
f28b75a9
MB
368 else if (w == XWINDOW (selected_window))
369 posint = PT;
370 else
371 posint = XMARKER (w->pointm)->charpos;
7ab12479 372
0cc1039f
KS
373 /* If position is above window start or outside buffer boundaries,
374 or if window start is out of range, position is not visible. */
120b7781
KS
375 if ((EQ (pos, Qt)
376 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
0cc1039f
KS
377 && CHARPOS (top) >= BUF_BEGV (buf)
378 && CHARPOS (top) <= BUF_ZV (buf)
5a857365 379 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
e56263e5 380 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
0cc1039f
KS
381 in_window = Qt;
382
383 if (!NILP (in_window) && !NILP (partially))
5a857365
KS
384 {
385 Lisp_Object part = Qnil;
386 if (!fully_p)
387 part = list4 (make_number (rtop), make_number (rbot),
388 make_number (rowh), make_number (vpos));
389 in_window = Fcons (make_number (x),
390 Fcons (make_number (y), part));
391 }
392
9a132b1f 393 return in_window;
7ab12479 394}
53bb6b99 395
536833ab 396DEFUN ("window-line-height", Fwindow_line_height,
7bbc67d2 397 Swindow_line_height, 0, 2, 0,
536833ab
KS
398 doc: /* Return height in pixels of text line LINE in window WINDOW.
399If WINDOW is nil or omitted, use selected window.
400
7bbc67d2
KS
401Return height of current line if LINE is omitted or nil. Return height of
402header or mode line if LINE is `header-line' and `mode-line'.
403Otherwise, LINE is a text line number starting from 0. A negative number
404counts from the end of the window.
536833ab 405
7bbc67d2 406Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
536833ab 407in pixels of the visible part of the line, VPOS and YPOS are the
68e67a4e 408vertical position in lines and pixels of the line, relative to the top
120b7781 409of the first text line, and OFFBOT is the number of off-window pixels at
68e67a4e
KS
410the bottom of the text line. If there are off-window pixels at the top
411of the (first) text line, YPOS is negative.
536833ab
KS
412
413Return nil if window display is not up-to-date. In that case, use
414`pos-visible-in-window-p' to obtain the information. */)
415 (line, window)
416 Lisp_Object line, window;
b3a10345
KS
417{
418 register struct window *w;
419 register struct buffer *b;
420 struct glyph_row *row, *end_row;
536833ab 421 int max_y, crop, i, n;
b3a10345
KS
422
423 w = decode_window (window);
424
425 if (noninteractive
b3a10345 426 || w->pseudo_window_p)
7bbc67d2 427 return Qnil;
b3a10345
KS
428
429 CHECK_BUFFER (w->buffer);
430 b = XBUFFER (w->buffer);
431
432 /* Fail if current matrix is not up-to-date. */
433 if (NILP (w->window_end_valid)
434 || current_buffer->clip_changed
435 || current_buffer->prevent_redisplay_optimizations_p
436 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
437 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
438 return Qnil;
439
7bbc67d2
KS
440 if (NILP (line))
441 {
442 i = w->cursor.vpos;
443 if (i < 0 || i >= w->current_matrix->nrows
444 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
445 return Qnil;
446 max_y = window_text_bottom_y (w);
447 goto found_row;
448 }
449
536833ab 450 if (EQ (line, Qheader_line))
b3a10345 451 {
536833ab
KS
452 if (!WINDOW_WANTS_HEADER_LINE_P (w))
453 return Qnil;
454 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
b3a10345
KS
455 if (!row->enabled_p)
456 return Qnil;
536833ab
KS
457 return list4 (make_number (row->height),
458 make_number (0), make_number (0),
459 make_number (0));
b3a10345 460 }
536833ab
KS
461
462 if (EQ (line, Qmode_line))
b3a10345 463 {
536833ab
KS
464 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
465 if (!row->enabled_p)
466 return Qnil;
467 return list4 (make_number (row->height),
468 make_number (0), /* not accurate */
469 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
470 + window_text_bottom_y (w)),
471 make_number (0));
472 }
b3a10345 473
536833ab 474 CHECK_NUMBER (line);
7bbc67d2 475 n = XINT (line);
536833ab
KS
476
477 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
478 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
479 max_y = window_text_bottom_y (w);
7bbc67d2 480 i = 0;
536833ab
KS
481
482 while ((n < 0 || i < n)
483 && row <= end_row && row->enabled_p
484 && row->y + row->height < max_y)
485 row++, i++;
b3a10345 486
536833ab
KS
487 if (row > end_row || !row->enabled_p)
488 return Qnil;
489
7bbc67d2 490 if (++n < 0)
536833ab
KS
491 {
492 if (-n > i)
493 return Qnil;
7bbc67d2
KS
494 row += n;
495 i += n;
b3a10345
KS
496 }
497
7bbc67d2 498 found_row:
536833ab
KS
499 crop = max (0, (row->y + row->height) - max_y);
500 return list4 (make_number (row->height + min (0, row->y) - crop),
7bbc67d2 501 make_number (i),
536833ab
KS
502 make_number (row->y),
503 make_number (crop));
b3a10345
KS
504}
505
536833ab 506
7ab12479
JB
507\f
508static struct window *
509decode_window (window)
510 register Lisp_Object window;
511{
265a9e55 512 if (NILP (window))
7ab12479
JB
513 return XWINDOW (selected_window);
514
b7826503 515 CHECK_LIVE_WINDOW (window);
7ab12479
JB
516 return XWINDOW (window);
517}
518
e995d23a
RS
519static struct window *
520decode_any_window (window)
521 register Lisp_Object window;
522{
523 if (NILP (window))
524 return XWINDOW (selected_window);
525
526 CHECK_WINDOW (window);
527 return XWINDOW (window);
528}
529
7ab12479 530DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
c2755926
LT
531 doc: /* Return the buffer that WINDOW is displaying.
532WINDOW defaults to the selected window. */)
fdb82f93 533 (window)
7ab12479
JB
534 Lisp_Object window;
535{
536 return decode_window (window)->buffer;
537}
538
539DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
fdb82f93
PJ
540 doc: /* Return the number of lines in WINDOW (including its mode line). */)
541 (window)
7ab12479
JB
542 Lisp_Object window;
543{
e995d23a 544 return decode_any_window (window)->total_lines;
7ab12479
JB
545}
546
547DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
fdb82f93
PJ
548 doc: /* Return the number of display columns in WINDOW.
549This is the width that is usable columns available for text in WINDOW.
550If you want to find out how many columns WINDOW takes up,
551use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
552 (window)
7ab12479
JB
553 Lisp_Object window;
554{
e995d23a 555 return make_number (window_box_text_cols (decode_any_window (window)));
7ab12479
JB
556}
557
558DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
fdb82f93
PJ
559 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
560 (window)
7ab12479
JB
561 Lisp_Object window;
562{
563 return decode_window (window)->hscroll;
564}
565
566DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
fdb82f93 567 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
a0a37a6f 568Return NCOL. NCOL should be zero or positive.
ebadb1e4
EZ
569
570Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
120b7781 571window so that the location of point moves off-window. */)
fdb82f93 572 (window, ncol)
ea68264b 573 Lisp_Object window, ncol;
7ab12479 574{
ea68264b
GM
575 struct window *w = decode_window (window);
576 int hscroll;
7ab12479 577
b7826503 578 CHECK_NUMBER (ncol);
ea68264b 579 hscroll = max (0, XINT (ncol));
177c0ea7 580
ea68264b
GM
581 /* Prevent redisplay shortcuts when changing the hscroll. */
582 if (XINT (w->hscroll) != hscroll)
b1599b4c 583 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
177c0ea7 584
c67fa410 585 w->hscroll = make_number (hscroll);
7ab12479
JB
586 return ncol;
587}
588
190eb263
RS
589DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
590 Swindow_redisplay_end_trigger, 0, 1, 0,
fdb82f93
PJ
591 doc: /* Return WINDOW's redisplay end trigger value.
592See `set-window-redisplay-end-trigger' for more information. */)
593 (window)
190eb263
RS
594 Lisp_Object window;
595{
596 return decode_window (window)->redisplay_end_trigger;
597}
598
599DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
600 Sset_window_redisplay_end_trigger, 2, 2, 0,
fdb82f93
PJ
601 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
602VALUE should be a buffer position (typically a marker) or nil.
603If it is a buffer position, then if redisplay in WINDOW reaches a position
604beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
605with two arguments: WINDOW, and the end trigger value.
606Afterwards the end-trigger value is reset to nil. */)
607 (window, value)
190eb263
RS
608 register Lisp_Object window, value;
609{
610 register struct window *w;
611
612 w = decode_window (window);
613 w->redisplay_end_trigger = value;
614 return value;
615}
616
7ab12479 617DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
fdb82f93
PJ
618 doc: /* Return a list of the edge coordinates of WINDOW.
619\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
c99a9eb3
RS
620RIGHT is one more than the rightmost column occupied by WINDOW,
621and BOTTOM is one more than the bottommost row occupied by WINDOW.
622The edges include the space used by the window's scroll bar,
623display margins, fringes, header line, and mode line, if it has them.
624To get the edges of the actual text area, use `window-inside-edges'. */)
fdb82f93 625 (window)
7ab12479
JB
626 Lisp_Object window;
627{
e995d23a 628 register struct window *w = decode_any_window (window);
7ab12479 629
949cf20f
KS
630 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
631 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
632 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
633 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
634 Qnil))));
7ab12479
JB
635}
636
c99a9eb3
RS
637DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
638 doc: /* Return a list of the edge pixel coordinates of WINDOW.
639\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
640RIGHT is one more than the rightmost x position occupied by WINDOW,
641and BOTTOM is one more than the bottommost y position occupied by WINDOW.
642The pixel edges include the space used by the window's scroll bar,
643display margins, fringes, header line, and mode line, if it has them.
644To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
645 (window)
646 Lisp_Object window;
647{
e995d23a 648 register struct window *w = decode_any_window (window);
c99a9eb3
RS
649
650 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
651 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
652 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
653 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
654 Qnil))));
655}
656
657DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
658 doc: /* Return a list of the edge coordinates of WINDOW.
659\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
660RIGHT is one more than the rightmost column used by text in WINDOW,
661and BOTTOM is one more than the bottommost row used by text in WINDOW.
662The inside edges do not include the space used by the window's scroll bar,
663display margins, fringes, header line, and/or mode line. */)
664 (window)
665 Lisp_Object window;
666{
e995d23a 667 register struct window *w = decode_any_window (window);
c99a9eb3
RS
668
669 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
670 + WINDOW_LEFT_MARGIN_COLS (w)
671 + WINDOW_LEFT_FRINGE_COLS (w)),
672 make_number (WINDOW_TOP_EDGE_LINE (w)
673 + WINDOW_HEADER_LINE_LINES (w)),
f3fbdb1f 674 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
c99a9eb3
RS
675 - WINDOW_RIGHT_MARGIN_COLS (w)
676 - WINDOW_RIGHT_FRINGE_COLS (w)),
677 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
678 - WINDOW_MODE_LINE_LINES (w)));
679}
680
681DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
a0a37a6f 682 doc: /* Return a list of the edge pixel coordinates of WINDOW.
c99a9eb3
RS
683\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
684RIGHT is one more than the rightmost x position used by text in WINDOW,
685and BOTTOM is one more than the bottommost y position used by text in WINDOW.
686The inside edges do not include the space used by the window's scroll bar,
687display margins, fringes, header line, and/or mode line. */)
688 (window)
689 Lisp_Object window;
690{
e995d23a 691 register struct window *w = decode_any_window (window);
c99a9eb3
RS
692
693 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
694 + WINDOW_LEFT_MARGIN_WIDTH (w)
695 + WINDOW_LEFT_FRINGE_WIDTH (w)),
696 make_number (WINDOW_TOP_EDGE_Y (w)
697 + WINDOW_HEADER_LINE_HEIGHT (w)),
f3fbdb1f 698 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
c99a9eb3
RS
699 - WINDOW_RIGHT_MARGIN_WIDTH (w)
700 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
701 make_number (WINDOW_BOTTOM_EDGE_Y (w)
702 - WINDOW_MODE_LINE_HEIGHT (w)));
703}
704
fbad6f9a 705/* Test if the character at column *X, row *Y is within window W.
341f3858 706 If it is not, return ON_NOTHING;
d5783c40
JB
707 if it is in the window's text area,
708 set *x and *y to its location relative to the upper left corner
709 of the window, and
341f3858
KS
710 return ON_TEXT;
711 if it is on the window's modeline, return ON_MODE_LINE;
d5783c40 712 if it is on the border between the window and its right sibling,
341f3858 713 return ON_VERTICAL_BORDER.
6487f669
RS
714 if it is on a scroll bar,
715 return ON_SCROLL_BAR.
341f3858 716 if it is on the window's top line, return ON_HEADER_LINE;
81d189fd 717 if it is in left or right fringe of the window,
341f3858
KS
718 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
719 to window-relative coordinates;
49b996e7 720 if it is in the marginal area to the left/right of the window,
341f3858
KS
721 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
722 to window-relative coordinates.
5500c422
GM
723
724 X and Y are frame relative pixel coordinates. */
725
7442878f 726static enum window_part
d5783c40
JB
727coordinates_in_window (w, x, y)
728 register struct window *w;
729 register int *x, *y;
730{
b0228ace
GM
731 struct frame *f = XFRAME (WINDOW_FRAME (w));
732 int left_x, right_x, top_y, bottom_y;
7442878f 733 enum window_part part;
949cf20f
KS
734 int ux = FRAME_COLUMN_WIDTH (f);
735 int x0 = WINDOW_LEFT_EDGE_X (w);
736 int x1 = WINDOW_RIGHT_EDGE_X (w);
dad75588
GM
737 /* The width of the area where the vertical line can be dragged.
738 (Between mode lines for instance. */
739 int grabbable_width = ux;
949cf20f 740 int lmargin_width, rmargin_width, text_left, text_right;
b0228ace 741
466539bc
EZ
742 /* In what's below, we subtract 1 when computing right_x because we
743 want the rightmost pixel, which is given by left_pixel+width-1. */
5500c422
GM
744 if (w->pseudo_window_p)
745 {
746 left_x = 0;
949cf20f
KS
747 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
748 top_y = WINDOW_TOP_EDGE_Y (w);
749 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
5500c422
GM
750 }
751 else
752 {
949cf20f
KS
753 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
754 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
755 top_y = WINDOW_TOP_EDGE_Y (w);
756 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
5500c422 757 }
37962e60 758
949cf20f
KS
759 /* Outside any interesting row? */
760 if (*y < top_y || *y >= bottom_y)
761 return ON_NOTHING;
762
1cb794ba
GM
763 /* On the mode line or header line? If it's near the start of
764 the mode or header line of window that's has a horizontal
765 sibling, say it's on the vertical line. That's to be able
766 to resize windows horizontally in case we're using toolkit
767 scroll bars. */
b0228ace 768
1cb794ba 769 if (WINDOW_WANTS_MODELINE_P (w)
0de01dfe 770 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
1cb794ba 771 {
949cf20f
KS
772 part = ON_MODE_LINE;
773
774 header_vertical_border_check:
b0228ace
GM
775 /* We're somewhere on the mode line. We consider the place
776 between mode lines of horizontally adjacent mode lines
777 as the vertical border. If scroll bars on the left,
778 return the right window. */
949cf20f
KS
779 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
780 || WINDOW_RIGHTMOST_P (w))
b0228ace 781 {
949cf20f 782 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
f8c62b70 783 {
3f489dc7
KS
784 /* Convert X and Y to window relative coordinates.
785 Vertical border is at the left edge of window. */
f8c62b70
NR
786 *x = max (0, *x - x0);
787 *y -= top_y;
788 return ON_VERTICAL_BORDER;
789 }
b0228ace 790 }
79fd290e 791 else
b0228ace 792 {
949cf20f 793 if (abs (*x - x1) < grabbable_width)
f8c62b70 794 {
3f489dc7
KS
795 /* Convert X and Y to window relative coordinates.
796 Vertical border is at the right edge of window. */
f8c62b70
NR
797 *x = min (x1, *x) - x0;
798 *y -= top_y;
799 return ON_VERTICAL_BORDER;
800 }
b0228ace 801 }
949cf20f 802
cb639b8f
KS
803 if (*x < x0 || *x >= x1)
804 return ON_NOTHING;
805
a5303820
KS
806 /* Convert X and Y to window relative coordinates.
807 Mode line starts at left edge of window. */
808 *x -= x0;
809 *y -= top_y;
949cf20f 810 return part;
b0228ace 811 }
949cf20f
KS
812
813 if (WINDOW_WANTS_HEADER_LINE_P (w)
949cf20f 814 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
1cb794ba 815 {
949cf20f
KS
816 part = ON_HEADER_LINE;
817 goto header_vertical_border_check;
1cb794ba 818 }
949cf20f 819
cb639b8f
KS
820 if (*x < x0 || *x >= x1)
821 return ON_NOTHING;
822
949cf20f
KS
823 /* Outside any interesting column? */
824 if (*x < left_x || *x > right_x)
4d1add72
KS
825 {
826 *y -= top_y;
827 return ON_SCROLL_BAR;
828 }
949cf20f
KS
829
830 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
831 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
79fd290e 832
949cf20f
KS
833 text_left = window_box_left (w, TEXT_AREA);
834 text_right = text_left + window_box_width (w, TEXT_AREA);
835
836 if (FRAME_WINDOW_P (f))
fbad6f9a 837 {
447e9da0 838 if (!w->pseudo_window_p
949cf20f 839 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
447e9da0 840 && !WINDOW_RIGHTMOST_P (w)
949cf20f 841 && (abs (*x - right_x) < grabbable_width))
f8c62b70 842 {
3f489dc7
KS
843 /* Convert X and Y to window relative coordinates.
844 Vertical border is at the right edge of window. */
f8c62b70
NR
845 *x = min (right_x, *x) - left_x;
846 *y -= top_y;
847 return ON_VERTICAL_BORDER;
848 }
fbad6f9a 849 }
447e9da0
GM
850 else
851 {
852 /* Need to say "*x > right_x" rather than >=, since on character
853 terminals, the vertical line's x coordinate is right_x. */
949cf20f
KS
854 if (!w->pseudo_window_p
855 && !WINDOW_RIGHTMOST_P (w)
856 && *x > right_x - ux)
b0228ace
GM
857 {
858 /* On the border on the right side of the window? Assume that
859 this area begins at RIGHT_X minus a canonical char width. */
f8c62b70
NR
860 *x = min (right_x, *x) - left_x;
861 *y -= top_y;
949cf20f 862 return ON_VERTICAL_BORDER;
b0228ace 863 }
447e9da0 864 }
b0228ace 865
949cf20f
KS
866 if (*x < text_left)
867 {
868 if (lmargin_width > 0
869 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
870 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
871 : (*x < left_x + lmargin_width)))
a5303820 872 {
0de01dfe 873 *x -= left_x;
a5303820
KS
874 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
875 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
876 *y -= top_y;
877 return ON_LEFT_MARGIN;
878 }
949cf20f
KS
879
880 /* Convert X and Y to window-relative pixel coordinates. */
881 *x -= left_x;
882 *y -= top_y;
883 return ON_LEFT_FRINGE;
884 }
885
886 if (*x >= text_right)
887 {
888 if (rmargin_width > 0
889 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
890 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
891 : (*x >= right_x - rmargin_width)))
a5303820 892 {
66d41723
KS
893 *x -= right_x - rmargin_width;
894 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
895 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
a5303820
KS
896 *y -= top_y;
897 return ON_RIGHT_MARGIN;
898 }
949cf20f
KS
899
900 /* Convert X and Y to window-relative pixel coordinates. */
901 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
902 *y -= top_y;
903 return ON_RIGHT_FRINGE;
904 }
905
906 /* Everything special ruled out - must be on text area */
66d41723 907 *x -= text_left;
949cf20f
KS
908 *y -= top_y;
909 return ON_TEXT;
d5783c40
JB
910}
911
b0228ace 912
d5783c40 913DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
fdb82f93
PJ
914 Scoordinates_in_window_p, 2, 2, 0,
915 doc: /* Return non-nil if COORDINATES are in WINDOW.
916COORDINATES is a cons of the form (X . Y), X and Y being distances
917measured in characters from the upper-left corner of the frame.
918\(0 . 0) denotes the character in the upper left corner of the
919frame.
920If COORDINATES are in the text portion of WINDOW,
921 the coordinates relative to the window are returned.
922If they are in the mode line of WINDOW, `mode-line' is returned.
923If they are in the top mode line of WINDOW, `header-line' is returned.
81d189fd
KS
924If they are in the left fringe of WINDOW, `left-fringe' is returned.
925If they are in the right fringe of WINDOW, `right-fringe' is returned.
fdb82f93 926If they are on the border between WINDOW and its right sibling,
49b996e7
GM
927 `vertical-line' is returned.
928If they are in the windows's left or right marginal areas, `left-margin'\n\
929 or `right-margin' is returned. */)
fdb82f93 930 (coordinates, window)
d5783c40
JB
931 register Lisp_Object coordinates, window;
932{
5500c422
GM
933 struct window *w;
934 struct frame *f;
d5783c40 935 int x, y;
5500c422 936 Lisp_Object lx, ly;
d5783c40 937
e995d23a 938 CHECK_WINDOW (window);
5500c422
GM
939 w = XWINDOW (window);
940 f = XFRAME (w->frame);
b7826503 941 CHECK_CONS (coordinates);
5500c422
GM
942 lx = Fcar (coordinates);
943 ly = Fcdr (coordinates);
b7826503
PJ
944 CHECK_NUMBER_OR_FLOAT (lx);
945 CHECK_NUMBER_OR_FLOAT (ly);
81159bb9
RS
946 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
947 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
5500c422
GM
948
949 switch (coordinates_in_window (w, &x, &y))
d5783c40 950 {
7442878f 951 case ON_NOTHING:
d5783c40
JB
952 return Qnil;
953
7442878f
GM
954 case ON_TEXT:
955 /* X and Y are now window relative pixel coordinates. Convert
956 them to canonical char units before returning them. */
949cf20f
KS
957 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
958 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
d5783c40 959
7442878f 960 case ON_MODE_LINE:
d5783c40 961 return Qmode_line;
37962e60 962
7442878f 963 case ON_VERTICAL_BORDER:
e5d77022 964 return Qvertical_line;
d5783c40 965
7442878f 966 case ON_HEADER_LINE:
045dee35 967 return Qheader_line;
5500c422 968
7442878f
GM
969 case ON_LEFT_FRINGE:
970 return Qleft_fringe;
177c0ea7 971
7442878f
GM
972 case ON_RIGHT_FRINGE:
973 return Qright_fringe;
fbad6f9a 974
49b996e7
GM
975 case ON_LEFT_MARGIN:
976 return Qleft_margin;
177c0ea7 977
49b996e7
GM
978 case ON_RIGHT_MARGIN:
979 return Qright_margin;
980
6487f669
RS
981 case ON_SCROLL_BAR:
982 /* Historically we are supposed to return nil in this case. */
983 return Qnil;
984
d5783c40
JB
985 default:
986 abort ();
987 }
988}
989
67492200
GM
990
991/* Callback for foreach_window, used in window_from_coordinates.
f95464e4
GM
992 Check if window W contains coordinates specified by USER_DATA which
993 is actually a pointer to a struct check_window_data CW.
994
995 Check if window W contains coordinates *CW->x and *CW->y. If it
996 does, return W in *CW->window, as Lisp_Object, and return in
5372262f 997 *CW->part the part of the window under coordinates *X,*Y. Return
f95464e4
GM
998 zero from this function to stop iterating over windows. */
999
1000struct check_window_data
1001{
1002 Lisp_Object *window;
341f3858
KS
1003 int *x, *y;
1004 enum window_part *part;
f95464e4 1005};
67492200
GM
1006
1007static int
f95464e4 1008check_window_containing (w, user_data)
67492200 1009 struct window *w;
f95464e4 1010 void *user_data;
67492200 1011{
f95464e4 1012 struct check_window_data *cw = (struct check_window_data *) user_data;
7442878f
GM
1013 enum window_part found;
1014 int continue_p = 1;
67492200 1015
f95464e4 1016 found = coordinates_in_window (w, cw->x, cw->y);
7442878f 1017 if (found != ON_NOTHING)
67492200 1018 {
341f3858 1019 *cw->part = found;
f95464e4 1020 XSETWINDOW (*cw->window, w);
7442878f 1021 continue_p = 0;
67492200 1022 }
177c0ea7 1023
7442878f 1024 return continue_p;
67492200
GM
1025}
1026
1027
5500c422 1028/* Find the window containing frame-relative pixel position X/Y and
949cf20f
KS
1029 return it as a Lisp_Object.
1030
1031 If X, Y is on one of the window's special `window_part' elements,
1032 set *PART to the id of that element, and return X and Y converted
1033 to window relative coordinates in WX and WY.
1034
341f3858 1035 If there is no window under X, Y return nil and leave *PART
67492200
GM
1036 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1037
1038 This function was previously implemented with a loop cycling over
1039 windows with Fnext_window, and starting with the frame's selected
1040 window. It turned out that this doesn't work with an
1041 implementation of next_window using Vwindow_list, because
1042 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1043 tree of F when this function is called asynchronously from
1044 note_mouse_highlight. The original loop didn't terminate in this
1045 case. */
5500c422 1046
7ab12479 1047Lisp_Object
949cf20f 1048window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
67492200 1049 struct frame *f;
7ab12479 1050 int x, y;
341f3858 1051 enum window_part *part;
949cf20f 1052 int *wx, *wy;
9ea173e8 1053 int tool_bar_p;
7ab12479 1054{
67492200 1055 Lisp_Object window;
f95464e4 1056 struct check_window_data cw;
341f3858
KS
1057 enum window_part dummy;
1058
1059 if (part == 0)
1060 part = &dummy;
5500c422 1061
67492200 1062 window = Qnil;
f95464e4
GM
1063 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
1064 foreach_window (f, check_window_containing, &cw);
177c0ea7 1065
67492200
GM
1066 /* If not found above, see if it's in the tool bar window, if a tool
1067 bar exists. */
1068 if (NILP (window)
1069 && tool_bar_p
1070 && WINDOWP (f->tool_bar_window)
949cf20f 1071 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
7442878f
GM
1072 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
1073 != ON_NOTHING))
5500c422 1074 {
341f3858 1075 *part = ON_TEXT;
67492200 1076 window = f->tool_bar_window;
5500c422 1077 }
37962e60 1078
949cf20f
KS
1079 if (wx) *wx = x;
1080 if (wy) *wy = y;
1081
67492200 1082 return window;
7ab12479
JB
1083}
1084
ab17c3f2 1085DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
fdb82f93
PJ
1086 doc: /* Return window containing coordinates X and Y on FRAME.
1087If omitted, FRAME defaults to the currently selected frame.
1088The top left corner of the frame is considered to be row 0,
1089column 0. */)
1090 (x, y, frame)
1091 Lisp_Object x, y, frame;
7ab12479 1092{
5500c422 1093 struct frame *f;
7ab12479 1094
44fa5b1e 1095 if (NILP (frame))
1ae1a37d 1096 frame = selected_frame;
b7826503 1097 CHECK_LIVE_FRAME (frame);
5500c422 1098 f = XFRAME (frame);
7ab12479 1099
5500c422 1100 /* Check that arguments are integers or floats. */
b7826503
PJ
1101 CHECK_NUMBER_OR_FLOAT (x);
1102 CHECK_NUMBER_OR_FLOAT (y);
5500c422 1103
177c0ea7 1104 return window_from_coordinates (f,
2c77cf3b
RS
1105 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1106 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1107 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1108 + FRAME_INTERNAL_BORDER_WIDTH (f)),
949cf20f 1109 0, 0, 0, 0);
7ab12479
JB
1110}
1111
1112DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
fdb82f93
PJ
1113 doc: /* Return current value of point in WINDOW.
1114For a nonselected window, this is the value point would have
1115if that window were selected.
1116
1117Note that, when WINDOW is the selected window and its buffer
1118is also currently selected, the value returned is the same as (point).
1119It would be more strictly correct to return the `top-level' value
1120of point, outside of any save-excursion forms.
1121But that is hard to define. */)
1122 (window)
7ab12479
JB
1123 Lisp_Object window;
1124{
1125 register struct window *w = decode_window (window);
1126
1127 if (w == XWINDOW (selected_window)
1128 && current_buffer == XBUFFER (w->buffer))
1129 return Fpoint ();
1130 return Fmarker_position (w->pointm);
1131}
1132
1133DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
fdb82f93
PJ
1134 doc: /* Return position at which display currently starts in WINDOW.
1135This is updated by redisplay or by calling `set-window-start'. */)
1136 (window)
7ab12479
JB
1137 Lisp_Object window;
1138{
1139 return Fmarker_position (decode_window (window)->start);
1140}
1141
8646118f
RS
1142/* This is text temporarily removed from the doc string below.
1143
fdb82f93
PJ
1144This function returns nil if the position is not currently known.
1145That happens when redisplay is preempted and doesn't finish.
1146If in that case you want to compute where the end of the window would
1147have been if redisplay had finished, do this:
1148 (save-excursion
1149 (goto-char (window-start window))
1150 (vertical-motion (1- (window-height window)) window)
8646118f
RS
1151 (point))") */
1152
478292ed 1153DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
fdb82f93
PJ
1154 doc: /* Return position at which display currently ends in WINDOW.
1155This is updated by redisplay, when it runs to completion.
1156Simply changing the buffer text or setting `window-start'
1157does not update this value.
a0a37a6f
LT
1158Return nil if there is no recorded value. \(This can happen if the
1159last redisplay of WINDOW was preempted, and did not finish.)
fdb82f93
PJ
1160If UPDATE is non-nil, compute the up-to-date position
1161if it isn't already recorded. */)
1162 (window, update)
478292ed 1163 Lisp_Object window, update;
7ab12479
JB
1164{
1165 Lisp_Object value;
1166 struct window *w = decode_window (window);
5a41ab94 1167 Lisp_Object buf;
31790df3 1168 struct buffer *b;
5a41ab94
RS
1169
1170 buf = w->buffer;
b7826503 1171 CHECK_BUFFER (buf);
b3a10345 1172 b = XBUFFER (buf);
5a41ab94 1173
8646118f 1174#if 0 /* This change broke some things. We should make it later. */
7250968e
RS
1175 /* If we don't know the end position, return nil.
1176 The user can compute it with vertical-motion if he wants to.
1177 It would be nicer to do it automatically,
1178 but that's so slow that it would probably bother people. */
1179 if (NILP (w->window_end_valid))
1180 return Qnil;
8646118f 1181#endif
7250968e 1182
478292ed
RS
1183 if (! NILP (update)
1184 && ! (! NILP (w->window_end_valid)
b3a10345 1185 && XFASTINT (w->last_modified) >= BUF_MODIFF (b))
41791a20 1186 && !noninteractive)
478292ed 1187 {
cbc099e5
GM
1188 struct text_pos startp;
1189 struct it it;
b3a10345
KS
1190 struct buffer *old_buffer = NULL;
1191
1192 /* Cannot use Fvertical_motion because that function doesn't
1193 cope with variable-height lines. */
1194 if (b != current_buffer)
1195 {
1196 old_buffer = current_buffer;
1197 set_buffer_internal (b);
1198 }
2d6d9df0
GM
1199
1200 /* In case W->start is out of the range, use something
cb795ec4 1201 reasonable. This situation occurred when loading a file with
2d6d9df0
GM
1202 `-l' containing a call to `rmail' with subsequent other
1203 commands. At the end, W->start happened to be BEG, while
cbc099e5 1204 rmail had already narrowed the buffer. */
2d6d9df0 1205 if (XMARKER (w->start)->charpos < BEGV)
cbc099e5 1206 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
2d6d9df0 1207 else if (XMARKER (w->start)->charpos > ZV)
cbc099e5 1208 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2d6d9df0 1209 else
cbc099e5
GM
1210 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1211
cbc099e5
GM
1212 start_display (&it, w, startp);
1213 move_it_vertically (&it, window_box_height (w));
c3c45f65
GM
1214 if (it.current_y < it.last_visible_y)
1215 move_it_past_eol (&it);
cbc099e5 1216 value = make_number (IT_CHARPOS (it));
177c0ea7 1217
99593a9d
GM
1218 if (old_buffer)
1219 set_buffer_internal (old_buffer);
478292ed
RS
1220 }
1221 else
b3a10345 1222 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
7ab12479
JB
1223
1224 return value;
1225}
1226
1227DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
a0a37a6f
LT
1228 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1229Return POS. */)
fdb82f93 1230 (window, pos)
7ab12479
JB
1231 Lisp_Object window, pos;
1232{
1233 register struct window *w = decode_window (window);
1234
b7826503 1235 CHECK_NUMBER_COERCE_MARKER (pos);
e90c4fe6
RS
1236 if (w == XWINDOW (selected_window)
1237 && XBUFFER (w->buffer) == current_buffer)
7ab12479
JB
1238 Fgoto_char (pos);
1239 else
1240 set_marker_restricted (w->pointm, pos, w->buffer);
799417bd 1241
0685cb3c
GM
1242 /* We have to make sure that redisplay updates the window to show
1243 the new value of point. */
1244 if (!EQ (window, selected_window))
799417bd 1245 ++windows_or_buffers_changed;
177c0ea7 1246
7ab12479
JB
1247 return pos;
1248}
1249
1250DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
fdb82f93 1251 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
a0a37a6f 1252Return POS.
fdb82f93
PJ
1253Optional third arg NOFORCE non-nil inhibits next redisplay
1254from overriding motion of point in order to display at this exact start. */)
1255 (window, pos, noforce)
7ab12479
JB
1256 Lisp_Object window, pos, noforce;
1257{
1258 register struct window *w = decode_window (window);
1259
b7826503 1260 CHECK_NUMBER_COERCE_MARKER (pos);
7ab12479
JB
1261 set_marker_restricted (w->start, pos, w->buffer);
1262 /* this is not right, but much easier than doing what is right. */
1263 w->start_at_line_beg = Qnil;
265a9e55 1264 if (NILP (noforce))
7ab12479
JB
1265 w->force_start = Qt;
1266 w->update_mode_line = Qt;
d834a2e9 1267 XSETFASTINT (w->last_modified, 0);
3cd21523 1268 XSETFASTINT (w->last_overlay_modified, 0);
62c07cc7
JB
1269 if (!EQ (window, selected_window))
1270 windows_or_buffers_changed++;
ce7fae7d 1271
7ab12479
JB
1272 return pos;
1273}
1274
1275DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1276 1, 1, 0,
fdb82f93
PJ
1277 doc: /* Return WINDOW's dedicated object, usually t or nil.
1278See also `set-window-dedicated-p'. */)
1279 (window)
7ab12479
JB
1280 Lisp_Object window;
1281{
1282 return decode_window (window)->dedicated;
1283}
1284
d207b766
RS
1285DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1286 Sset_window_dedicated_p, 2, 2, 0,
fdb82f93
PJ
1287 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1288If it is dedicated, Emacs will not automatically change
1289which buffer appears in it.
1290The second argument is the new value for the dedication flag;
1291non-nil means yes. */)
1292 (window, arg)
1293 Lisp_Object window, arg;
7ab12479
JB
1294{
1295 register struct window *w = decode_window (window);
1296
2d0834cc 1297 w->dedicated = arg;
7ab12479
JB
1298
1299 return w->dedicated;
1300}
1301
1302DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1303 0, 1, 0,
fdb82f93
PJ
1304 doc: /* Return the display-table that WINDOW is using. */)
1305 (window)
7ab12479
JB
1306 Lisp_Object window;
1307{
1308 return decode_window (window)->display_table;
1309}
1310
5500c422
GM
1311/* Get the display table for use on window W. This is either W's
1312 display table or W's buffer's display table. Ignore the specified
1313 tables if they are not valid; if no valid table is specified,
1314 return 0. */
7ab12479 1315
319315f1 1316struct Lisp_Char_Table *
7ab12479
JB
1317window_display_table (w)
1318 struct window *w;
1319{
c756cdbe
GM
1320 struct Lisp_Char_Table *dp = NULL;
1321
1322 if (DISP_TABLE_P (w->display_table))
1323 dp = XCHAR_TABLE (w->display_table);
1324 else if (BUFFERP (w->buffer))
1325 {
1326 struct buffer *b = XBUFFER (w->buffer);
177c0ea7 1327
c756cdbe
GM
1328 if (DISP_TABLE_P (b->display_table))
1329 dp = XCHAR_TABLE (b->display_table);
1330 else if (DISP_TABLE_P (Vstandard_display_table))
1331 dp = XCHAR_TABLE (Vstandard_display_table);
1332 }
171d003c 1333
c756cdbe 1334 return dp;
7ab12479
JB
1335}
1336
3a2712f9 1337DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
fdb82f93
PJ
1338 doc: /* Set WINDOW's display-table to TABLE. */)
1339 (window, table)
7ab12479
JB
1340 register Lisp_Object window, table;
1341{
1342 register struct window *w;
7ab12479
JB
1343
1344 w = decode_window (window);
1345 w->display_table = table;
1346 return table;
1347}
1348\f
1349/* Record info on buffer window w is displaying
1350 when it is about to cease to display that buffer. */
dfcf069d 1351static void
7ab12479
JB
1352unshow_buffer (w)
1353 register struct window *w;
1354{
12cae7c0 1355 Lisp_Object buf;
b73ea88e 1356 struct buffer *b;
7ab12479 1357
12cae7c0 1358 buf = w->buffer;
b73ea88e
RS
1359 b = XBUFFER (buf);
1360 if (b != XMARKER (w->pointm)->buffer)
7ab12479
JB
1361 abort ();
1362
573f41ab 1363#if 0
7ab12479
JB
1364 if (w == XWINDOW (selected_window)
1365 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1366 /* Do this except when the selected window's buffer
1367 is being removed from some other window. */
573f41ab
RS
1368#endif
1369 /* last_window_start records the start position that this buffer
1370 had in the last window to be disconnected from it.
1371 Now that this statement is unconditional,
1372 it is possible for the buffer to be displayed in the
1373 selected window, while last_window_start reflects another
1374 window which was recently showing the same buffer.
1375 Some people might say that might be a good thing. Let's see. */
b73ea88e 1376 b->last_window_start = marker_position (w->start);
7ab12479
JB
1377
1378 /* Point in the selected window's buffer
1379 is actually stored in that buffer, and the window's pointm isn't used.
1380 So don't clobber point in that buffer. */
719eaeb1
GM
1381 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1382 /* This line helps to fix Horsley's testbug.el bug. */
8801a864
KR
1383 && !(WINDOWP (b->last_selected_window)
1384 && w != XWINDOW (b->last_selected_window)
719eaeb1 1385 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
b73ea88e
RS
1386 temp_set_point_both (b,
1387 clip_to_bounds (BUF_BEGV (b),
1388 XMARKER (w->pointm)->charpos,
1389 BUF_ZV (b)),
1390 clip_to_bounds (BUF_BEGV_BYTE (b),
1391 marker_byte_position (w->pointm),
1392 BUF_ZV_BYTE (b)));
177c0ea7 1393
8801a864
KR
1394 if (WINDOWP (b->last_selected_window)
1395 && w == XWINDOW (b->last_selected_window))
719eaeb1 1396 b->last_selected_window = Qnil;
7ab12479
JB
1397}
1398
1399/* Put replacement into the window structure in place of old. */
dfcf069d 1400static void
7ab12479
JB
1401replace_window (old, replacement)
1402 Lisp_Object old, replacement;
1403{
1404 register Lisp_Object tem;
1405 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1406
44fa5b1e
JB
1407 /* If OLD is its frame's root_window, then replacement is the new
1408 root_window for that frame. */
7ab12479 1409
7f4161e0 1410 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
44fa5b1e 1411 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
7ab12479 1412
949cf20f
KS
1413 p->left_col = o->left_col;
1414 p->top_line = o->top_line;
1415 p->total_cols = o->total_cols;
1416 p->total_lines = o->total_lines;
5500c422
GM
1417 p->desired_matrix = p->current_matrix = 0;
1418 p->vscroll = 0;
1419 bzero (&p->cursor, sizeof (p->cursor));
1420 bzero (&p->last_cursor, sizeof (p->last_cursor));
1421 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1422 p->phys_cursor_type = -1;
4b26579e 1423 p->phys_cursor_width = -1;
5500c422
GM
1424 p->must_be_updated_p = 0;
1425 p->pseudo_window_p = 0;
1426 XSETFASTINT (p->window_end_vpos, 0);
1427 XSETFASTINT (p->window_end_pos, 0);
1428 p->window_end_valid = Qnil;
7bbb5782 1429 p->frozen_window_start_p = 0;
949cf20f 1430 p->orig_top_line = p->orig_total_lines = Qnil;
7ab12479
JB
1431
1432 p->next = tem = o->next;
265a9e55 1433 if (!NILP (tem))
7ab12479
JB
1434 XWINDOW (tem)->prev = replacement;
1435
1436 p->prev = tem = o->prev;
265a9e55 1437 if (!NILP (tem))
7ab12479
JB
1438 XWINDOW (tem)->next = replacement;
1439
1440 p->parent = tem = o->parent;
265a9e55 1441 if (!NILP (tem))
7ab12479
JB
1442 {
1443 if (EQ (XWINDOW (tem)->vchild, old))
1444 XWINDOW (tem)->vchild = replacement;
1445 if (EQ (XWINDOW (tem)->hchild, old))
1446 XWINDOW (tem)->hchild = replacement;
1447 }
1448
1449/*** Here, if replacement is a vertical combination
1450and so is its new parent, we should make replacement's
1451children be children of that parent instead. ***/
1452}
1453
1454DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
fdb82f93
PJ
1455 doc: /* Remove WINDOW from the display. Default is selected window. */)
1456 (window)
7ab12479 1457 register Lisp_Object window;
543f5fb1
RS
1458{
1459 delete_window (window);
1460
1461 if (! NILP (Vwindow_configuration_change_hook)
1462 && ! NILP (Vrun_hooks))
1463 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1464
1465 return Qnil;
1466}
1467
5e14b1fc 1468void
543f5fb1
RS
1469delete_window (window)
1470 register Lisp_Object window;
7ab12479
JB
1471{
1472 register Lisp_Object tem, parent, sib;
1473 register struct window *p;
1474 register struct window *par;
951f9df5 1475 struct frame *f;
7ab12479 1476
605be8af
JB
1477 /* Because this function is called by other C code on non-leaf
1478 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1479 so we can't decode_window here. */
265a9e55 1480 if (NILP (window))
7ab12479
JB
1481 window = selected_window;
1482 else
b7826503 1483 CHECK_WINDOW (window);
7ab12479 1484 p = XWINDOW (window);
605be8af 1485
0d384044 1486 /* It's a no-op to delete an already-deleted window. */
605be8af
JB
1487 if (NILP (p->buffer)
1488 && NILP (p->hchild)
1489 && NILP (p->vchild))
296b535c 1490 return;
605be8af 1491
7ab12479 1492 parent = p->parent;
265a9e55 1493 if (NILP (parent))
7ab12479
JB
1494 error ("Attempt to delete minibuffer or sole ordinary window");
1495 par = XWINDOW (parent);
1496
1497 windows_or_buffers_changed++;
67492200 1498 Vwindow_list = Qnil;
951f9df5
GM
1499 f = XFRAME (WINDOW_FRAME (p));
1500 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
7ab12479 1501
605be8af
JB
1502 /* Are we trying to delete any frame's selected window? */
1503 {
192c3131 1504 Lisp_Object swindow, pwindow;
605be8af 1505
0def0403
RS
1506 /* See if the frame's selected window is either WINDOW
1507 or any subwindow of it, by finding all that window's parents
1508 and comparing each one with WINDOW. */
192c3131 1509 swindow = FRAME_SELECTED_WINDOW (f);
0def0403 1510
192c3131 1511 while (1)
0def0403 1512 {
192c3131
RS
1513 pwindow = swindow;
1514 while (!NILP (pwindow))
1515 {
1516 if (EQ (window, pwindow))
1517 break;
1518 pwindow = XWINDOW (pwindow)->parent;
1519 }
1520
1521 /* If the window being deleted is not a parent of SWINDOW,
1522 then SWINDOW is ok as the new selected window. */
1523 if (!EQ (window, pwindow))
0def0403 1524 break;
192c3131
RS
1525 /* Otherwise, try another window for SWINDOW. */
1526 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1527
1528 /* If we get back to the frame's selected window,
1529 it means there was no acceptable alternative,
1530 so we cannot delete. */
1531 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1532 error ("Cannot delete window");
0def0403
RS
1533 }
1534
192c3131
RS
1535 /* If we need to change SWINDOW, do it. */
1536 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
605be8af 1537 {
605be8af
JB
1538 /* If we're about to delete the selected window on the
1539 selected frame, then we should use Fselect_window to select
1540 the new window. On the other hand, if we're about to
1541 delete the selected window on any other frame, we shouldn't do
1542 anything but set the frame's selected_window slot. */
192c3131 1543 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
14d87dc9 1544 Fselect_window (swindow, Qnil);
605be8af 1545 else
192c3131 1546 FRAME_SELECTED_WINDOW (f) = swindow;
605be8af
JB
1547 }
1548 }
7ab12479 1549
0d384044
RS
1550 /* Now we know we can delete this one. */
1551 window_deletion_count++;
1552
7ab12479
JB
1553 tem = p->buffer;
1554 /* tem is null for dummy parent windows
1555 (which have inferiors but not any contents themselves) */
265a9e55 1556 if (!NILP (tem))
7ab12479
JB
1557 {
1558 unshow_buffer (p);
2d0834cc
SM
1559 unchain_marker (XMARKER (p->pointm));
1560 unchain_marker (XMARKER (p->start));
7ab12479
JB
1561 }
1562
05e71564
GM
1563 /* Free window glyph matrices. It is sure that they are allocated
1564 again when ADJUST_GLYPHS is called. Block input so that expose
1565 events and other events that access glyph matrices are not
1566 processed while we are changing them. */
1567 BLOCK_INPUT;
951f9df5 1568 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
5500c422 1569
7ab12479 1570 tem = p->next;
265a9e55 1571 if (!NILP (tem))
7ab12479
JB
1572 XWINDOW (tem)->prev = p->prev;
1573
1574 tem = p->prev;
265a9e55 1575 if (!NILP (tem))
7ab12479
JB
1576 XWINDOW (tem)->next = p->next;
1577
1578 if (EQ (window, par->hchild))
1579 par->hchild = p->next;
1580 if (EQ (window, par->vchild))
1581 par->vchild = p->next;
1582
1583 /* Find one of our siblings to give our space to. */
1584 sib = p->prev;
265a9e55 1585 if (NILP (sib))
7ab12479
JB
1586 {
1587 /* If p gives its space to its next sibling, that sibling needs
1588 to have its top/left side pulled back to where p's is.
1589 set_window_{height,width} will re-position the sibling's
1590 children. */
1591 sib = p->next;
949cf20f
KS
1592 XWINDOW (sib)->top_line = p->top_line;
1593 XWINDOW (sib)->left_col = p->left_col;
7ab12479
JB
1594 }
1595
1596 /* Stretch that sibling. */
265a9e55 1597 if (!NILP (par->vchild))
7ab12479 1598 set_window_height (sib,
949cf20f 1599 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
7ab12479 1600 1);
265a9e55 1601 if (!NILP (par->hchild))
7ab12479 1602 set_window_width (sib,
949cf20f 1603 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
7ab12479
JB
1604 1);
1605
1606 /* If parent now has only one child,
1607 put the child into the parent's place. */
7ab12479 1608 tem = par->hchild;
265a9e55 1609 if (NILP (tem))
7ab12479 1610 tem = par->vchild;
5494d7bc 1611 if (NILP (XWINDOW (tem)->next)) {
7ab12479 1612 replace_window (parent, tem);
5494d7bc
JD
1613 par = XWINDOW (tem);
1614 }
605be8af
JB
1615
1616 /* Since we may be deleting combination windows, we must make sure that
1617 not only p but all its children have been marked as deleted. */
1618 if (! NILP (p->hchild))
1619 delete_all_subwindows (XWINDOW (p->hchild));
1620 else if (! NILP (p->vchild))
1621 delete_all_subwindows (XWINDOW (p->vchild));
1622
1623 /* Mark this window as deleted. */
1624 p->buffer = p->hchild = p->vchild = Qnil;
5500c422 1625
5494d7bc
JD
1626 if (! NILP (par->parent))
1627 par = XWINDOW (par->parent);
1628
1629 /* Check if we have a v/hchild with a v/hchild. In that case remove
1630 one of them. */
ae12ecd7 1631
5494d7bc
JD
1632 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1633 {
1634 p = XWINDOW (par->vchild);
1635 par->vchild = p->vchild;
1636 tem = p->vchild;
1637 }
1638 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1639 {
1640 p = XWINDOW (par->hchild);
1641 par->hchild = p->hchild;
1642 tem = p->hchild;
1643 }
1644 else
1645 p = 0;
1646
1647 if (p)
1648 {
1649 while (! NILP (tem)) {
1650 XWINDOW (tem)->parent = p->parent;
1651 if (NILP (XWINDOW (tem)->next))
1652 break;
1653 tem = XWINDOW (tem)->next;
1654 }
1655 if (! NILP (tem)) {
1656 /* The next of the v/hchild we are removing is now the next of the
1657 last child for the v/hchild:
1658 Before v/hchild -> v/hchild -> next1 -> next2
1659 |
1660 -> next3
1661 After: v/hchild -> next1 -> next2 -> next3
1662 */
1663 XWINDOW (tem)->next = p->next;
1664 if (! NILP (p->next))
1665 XWINDOW (p->next)->prev = tem;
1666 }
1667 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1668 }
1669
1670
5500c422 1671 /* Adjust glyph matrices. */
951f9df5 1672 adjust_glyphs (f);
05e71564 1673 UNBLOCK_INPUT;
7ab12479 1674}
67492200
GM
1675
1676
7ab12479 1677\f
67492200
GM
1678/***********************************************************************
1679 Window List
1680 ***********************************************************************/
1681
f95464e4
GM
1682/* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1683 pointer. This is a callback function for foreach_window, used in
1684 function window_list. */
67492200
GM
1685
1686static int
f95464e4 1687add_window_to_list (w, user_data)
67492200 1688 struct window *w;
f95464e4 1689 void *user_data;
67492200 1690{
f95464e4 1691 Lisp_Object *list = (Lisp_Object *) user_data;
67492200
GM
1692 Lisp_Object window;
1693 XSETWINDOW (window, w);
212116d6 1694 *list = Fcons (window, *list);
67492200
GM
1695 return 1;
1696}
1697
1698
1699/* Return a list of all windows, for use by next_window. If
1700 Vwindow_list is a list, return that list. Otherwise, build a new
1701 list, cache it in Vwindow_list, and return that. */
1702
1703static Lisp_Object
1704window_list ()
1705{
1706 if (!CONSP (Vwindow_list))
1707 {
1708 Lisp_Object tail;
212116d6 1709
67492200
GM
1710 Vwindow_list = Qnil;
1711 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
212116d6
GM
1712 {
1713 Lisp_Object args[2];
1714
1715 /* We are visiting windows in canonical order, and add
1716 new windows at the front of args[1], which means we
1717 have to reverse this list at the end. */
1718 args[1] = Qnil;
1719 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1720 args[0] = Vwindow_list;
1721 args[1] = Fnreverse (args[1]);
1722 Vwindow_list = Fnconc (2, args);
1723 }
67492200 1724 }
177c0ea7 1725
67492200
GM
1726 return Vwindow_list;
1727}
1728
1729
118ea242
GM
1730/* Value is non-zero if WINDOW satisfies the constraints given by
1731 OWINDOW, MINIBUF and ALL_FRAMES.
67492200 1732
118ea242
GM
1733 MINIBUF t means WINDOW may be minibuffer windows.
1734 `lambda' means WINDOW may not be a minibuffer window.
1735 a window means a specific minibuffer window
67492200 1736
118ea242
GM
1737 ALL_FRAMES t means search all frames,
1738 nil means search just current frame,
1739 `visible' means search just visible frames,
1740 0 means search visible and iconified frames,
1741 a window means search the frame that window belongs to,
1742 a frame means consider windows on that frame, only. */
67492200
GM
1743
1744static int
118ea242
GM
1745candidate_window_p (window, owindow, minibuf, all_frames)
1746 Lisp_Object window, owindow, minibuf, all_frames;
67492200
GM
1747{
1748 struct window *w = XWINDOW (window);
1749 struct frame *f = XFRAME (w->frame);
1750 int candidate_p = 1;
1751
1752 if (!BUFFERP (w->buffer))
1753 candidate_p = 0;
1754 else if (MINI_WINDOW_P (w)
1755 && (EQ (minibuf, Qlambda)
1756 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1757 {
1758 /* If MINIBUF is `lambda' don't consider any mini-windows.
1759 If it is a window, consider only that one. */
1760 candidate_p = 0;
1761 }
118ea242
GM
1762 else if (EQ (all_frames, Qt))
1763 candidate_p = 1;
67492200 1764 else if (NILP (all_frames))
118ea242
GM
1765 {
1766 xassert (WINDOWP (owindow));
1767 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1768 }
67492200
GM
1769 else if (EQ (all_frames, Qvisible))
1770 {
1771 FRAME_SAMPLE_VISIBILITY (f);
1772 candidate_p = FRAME_VISIBLE_P (f);
1773 }
1774 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1775 {
1776 FRAME_SAMPLE_VISIBILITY (f);
1777 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1778 }
67492200
GM
1779 else if (WINDOWP (all_frames))
1780 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1781 || EQ (XWINDOW (all_frames)->frame, w->frame)
1782 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
118ea242
GM
1783 else if (FRAMEP (all_frames))
1784 candidate_p = EQ (all_frames, w->frame);
67492200
GM
1785
1786 return candidate_p;
1787}
1788
1789
1790/* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1791 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1792 ALL_FRAMES. */
1793
1794static void
1795decode_next_window_args (window, minibuf, all_frames)
1796 Lisp_Object *window, *minibuf, *all_frames;
1797{
1798 if (NILP (*window))
1799 *window = selected_window;
1800 else
b7826503 1801 CHECK_LIVE_WINDOW (*window);
177c0ea7 1802
67492200
GM
1803 /* MINIBUF nil may or may not include minibuffers. Decide if it
1804 does. */
1805 if (NILP (*minibuf))
1806 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1807 else if (!EQ (*minibuf, Qt))
1808 *minibuf = Qlambda;
177c0ea7 1809
67492200
GM
1810 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1811 => count none of them, or a specific minibuffer window (the
1812 active one) to count. */
1813
1814 /* ALL_FRAMES nil doesn't specify which frames to include. */
1815 if (NILP (*all_frames))
1816 *all_frames = (!EQ (*minibuf, Qlambda)
1817 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1818 : Qnil);
1819 else if (EQ (*all_frames, Qvisible))
1820 ;
0f8fe9a2 1821 else if (EQ (*all_frames, make_number (0)))
67492200
GM
1822 ;
1823 else if (FRAMEP (*all_frames))
1824 ;
1825 else if (!EQ (*all_frames, Qt))
1826 *all_frames = Qnil;
177c0ea7 1827
67492200
GM
1828 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1829 search just current frame, `visible' meaning search just visible
1830 frames, 0 meaning search visible and iconified frames, or a
1831 window, meaning search the frame that window belongs to, or a
1832 frame, meaning consider windows on that frame, only. */
1833}
1834
1835
1836/* Return the next or previous window of WINDOW in canonical ordering
1837 of windows. NEXT_P non-zero means return the next window. See the
1838 documentation string of next-window for the meaning of MINIBUF and
1839 ALL_FRAMES. */
1840
1841static Lisp_Object
1842next_window (window, minibuf, all_frames, next_p)
1843 Lisp_Object window, minibuf, all_frames;
1844 int next_p;
1845{
1846 decode_next_window_args (&window, &minibuf, &all_frames);
177c0ea7 1847
67492200
GM
1848 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1849 return the first window on the frame. */
1850 if (FRAMEP (all_frames)
1851 && !EQ (all_frames, XWINDOW (window)->frame))
1852 return Fframe_first_window (all_frames);
177c0ea7 1853
212116d6 1854 if (next_p)
67492200
GM
1855 {
1856 Lisp_Object list;
177c0ea7 1857
67492200
GM
1858 /* Find WINDOW in the list of all windows. */
1859 list = Fmemq (window, window_list ());
1860
1861 /* Scan forward from WINDOW to the end of the window list. */
1862 if (CONSP (list))
1863 for (list = XCDR (list); CONSP (list); list = XCDR (list))
118ea242 1864 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
67492200
GM
1865 break;
1866
1867 /* Scan from the start of the window list up to WINDOW. */
1868 if (!CONSP (list))
1869 for (list = Vwindow_list;
1870 CONSP (list) && !EQ (XCAR (list), window);
1871 list = XCDR (list))
118ea242 1872 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
67492200
GM
1873 break;
1874
1875 if (CONSP (list))
1876 window = XCAR (list);
1877 }
1878 else
1879 {
1880 Lisp_Object candidate, list;
177c0ea7 1881
67492200
GM
1882 /* Scan through the list of windows for candidates. If there are
1883 candidate windows in front of WINDOW, the last one of these
1884 is the one we want. If there are candidates following WINDOW
1885 in the list, again the last one of these is the one we want. */
1886 candidate = Qnil;
1887 for (list = window_list (); CONSP (list); list = XCDR (list))
1888 {
1889 if (EQ (XCAR (list), window))
1890 {
1891 if (WINDOWP (candidate))
1892 break;
1893 }
118ea242
GM
1894 else if (candidate_window_p (XCAR (list), window, minibuf,
1895 all_frames))
67492200
GM
1896 candidate = XCAR (list);
1897 }
1898
1899 if (WINDOWP (candidate))
1900 window = candidate;
1901 }
1902
1903 return window;
1904}
7ab12479 1905
7ab12479 1906
26f6279d 1907DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
fdb82f93
PJ
1908 doc: /* Return next window after WINDOW in canonical ordering of windows.
1909If omitted, WINDOW defaults to the selected window.
1910
1911Optional second arg MINIBUF t means count the minibuffer window even
1912if not active. MINIBUF nil or omitted means count the minibuffer iff
1913it is active. MINIBUF neither t nor nil means not to count the
1914minibuffer even if it is active.
1915
1916Several frames may share a single minibuffer; if the minibuffer
1917counts, all windows on all frames that share that minibuffer count
1918too. Therefore, `next-window' can be used to iterate through the
1919set of windows even when the minibuffer is on another frame. If the
1920minibuffer does not count, only windows from WINDOW's frame count.
1921
1922Optional third arg ALL-FRAMES t means include windows on all frames.
1923ALL-FRAMES nil or omitted means cycle within the frames as specified
1924above. ALL-FRAMES = `visible' means include windows on all visible frames.
1925ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1926If ALL-FRAMES is a frame, restrict search to windows on that frame.
1927Anything else means restrict to WINDOW's frame.
1928
1929If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1930`next-window' to iterate through the entire cycle of acceptable
1931windows, eventually ending up back at the window you started with.
1932`previous-window' traverses the same cycle, in the reverse order. */)
1933 (window, minibuf, all_frames)
67492200 1934 Lisp_Object window, minibuf, all_frames;
7ab12479 1935{
67492200 1936 return next_window (window, minibuf, all_frames, 1);
7ab12479
JB
1937}
1938
67492200 1939
26f6279d 1940DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
fdb82f93
PJ
1941 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1942If omitted, WINDOW defaults to the selected window.
1943
1944Optional second arg MINIBUF t means count the minibuffer window even
1945if not active. MINIBUF nil or omitted means count the minibuffer iff
1946it is active. MINIBUF neither t nor nil means not to count the
1947minibuffer even if it is active.
1948
1949Several frames may share a single minibuffer; if the minibuffer
1950counts, all windows on all frames that share that minibuffer count
1951too. Therefore, `previous-window' can be used to iterate through
1952the set of windows even when the minibuffer is on another frame. If
1953the minibuffer does not count, only windows from WINDOW's frame count
1954
1955Optional third arg ALL-FRAMES t means include windows on all frames.
1956ALL-FRAMES nil or omitted means cycle within the frames as specified
1957above. ALL-FRAMES = `visible' means include windows on all visible frames.
1958ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1959If ALL-FRAMES is a frame, restrict search to windows on that frame.
1960Anything else means restrict to WINDOW's frame.
1961
1962If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1963`previous-window' to iterate through the entire cycle of acceptable
1964windows, eventually ending up back at the window you started with.
1965`next-window' traverses the same cycle, in the reverse order. */)
1966 (window, minibuf, all_frames)
67492200 1967 Lisp_Object window, minibuf, all_frames;
7ab12479 1968{
67492200 1969 return next_window (window, minibuf, all_frames, 0);
7ab12479
JB
1970}
1971
67492200 1972
62c07cc7 1973DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
fdb82f93
PJ
1974 doc: /* Select the ARG'th different window on this frame.
1975All windows on current frame are arranged in a cyclic order.
1976This command selects the window ARG steps away in that order.
c2755926 1977A negative ARG moves in the opposite order. The optional second
8cd0d661 1978argument ALL-FRAMES has the same meaning as in `next-window', which see. */)
fdb82f93 1979 (arg, all_frames)
67492200 1980 Lisp_Object arg, all_frames;
7ab12479 1981{
67492200
GM
1982 Lisp_Object window;
1983 int i;
7ab12479 1984
b7826503 1985 CHECK_NUMBER (arg);
67492200 1986 window = selected_window;
177c0ea7 1987
67492200
GM
1988 for (i = XINT (arg); i > 0; --i)
1989 window = Fnext_window (window, Qnil, all_frames);
1990 for (; i < 0; ++i)
1991 window = Fprevious_window (window, Qnil, all_frames);
50e88778 1992
14d87dc9 1993 Fselect_window (window, Qnil);
7ab12479
JB
1994 return Qnil;
1995}
67492200
GM
1996
1997
1998DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
fdb82f93
PJ
1999 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2000FRAME nil or omitted means use the selected frame.
2001WINDOW nil or omitted means use the selected window.
2002MINIBUF t means include the minibuffer window, even if it isn't active.
2003MINIBUF nil or omitted means include the minibuffer window only
2004if it's active.
2005MINIBUF neither nil nor t means never include the minibuffer window. */)
2006 (frame, minibuf, window)
cd2904bd
GM
2007 Lisp_Object frame, minibuf, window;
2008{
cd2904bd 2009 if (NILP (window))
acc6289a
SM
2010 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2011 CHECK_WINDOW (window);
cd2904bd
GM
2012 if (NILP (frame))
2013 frame = selected_frame;
2014
2015 if (!EQ (frame, XWINDOW (window)->frame))
2016 error ("Window is on a different frame");
2017
2018 return window_list_1 (window, minibuf, frame);
2019}
2020
2021
2022/* Return a list of windows in canonical ordering. Arguments are like
2023 for `next-window'. */
2024
2025static Lisp_Object
2026window_list_1 (window, minibuf, all_frames)
069f5950 2027 Lisp_Object window, minibuf, all_frames;
67492200 2028{
403de3b4 2029 Lisp_Object tail, list, rest;
67492200
GM
2030
2031 decode_next_window_args (&window, &minibuf, &all_frames);
2032 list = Qnil;
177c0ea7 2033
67492200 2034 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
118ea242 2035 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
67492200 2036 list = Fcons (XCAR (tail), list);
177c0ea7 2037
403de3b4
RS
2038 /* Rotate the list to start with WINDOW. */
2039 list = Fnreverse (list);
2040 rest = Fmemq (window, list);
2041 if (!NILP (rest) && !EQ (rest, list))
2042 {
1725a7c9 2043 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
403de3b4
RS
2044 ;
2045 XSETCDR (tail, Qnil);
2046 list = nconc2 (rest, list);
2047 }
2048 return list;
67492200
GM
2049}
2050
2051
7ab12479
JB
2052\f
2053/* Look at all windows, performing an operation specified by TYPE
2054 with argument OBJ.
75d8f668 2055 If FRAMES is Qt, look at all frames;
75d8f668 2056 Qnil, look at just the selected frame;
89bca612 2057 Qvisible, look at visible frames;
75d8f668 2058 a frame, just look at windows on that frame.
85fe3b5e 2059 If MINI is non-zero, perform the operation on minibuffer windows too. */
7ab12479
JB
2060
2061enum window_loop
2062{
2063 WINDOW_LOOP_UNUSED,
2064 GET_BUFFER_WINDOW, /* Arg is buffer */
2065 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2066 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2067 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2068 GET_LARGEST_WINDOW,
3f8ab7bd 2069 UNSHOW_BUFFER, /* Arg is buffer */
e661376d 2070 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
3f8ab7bd 2071 CHECK_ALL_WINDOWS
7ab12479
JB
2072};
2073
2074static Lisp_Object
44fa5b1e 2075window_loop (type, obj, mini, frames)
7ab12479 2076 enum window_loop type;
118ea242 2077 Lisp_Object obj, frames;
7ab12479
JB
2078 int mini;
2079{
118ea242
GM
2080 Lisp_Object window, windows, best_window, frame_arg;
2081 struct frame *f;
ffdc852d 2082 struct gcpro gcpro1;
177c0ea7 2083
44fa5b1e
JB
2084 /* If we're only looping through windows on a particular frame,
2085 frame points to that frame. If we're looping through windows
2086 on all frames, frame is 0. */
2087 if (FRAMEP (frames))
118ea242 2088 f = XFRAME (frames);
44fa5b1e 2089 else if (NILP (frames))
118ea242 2090 f = SELECTED_FRAME ();
7ab12479 2091 else
118ea242 2092 f = NULL;
177c0ea7 2093
118ea242 2094 if (f)
89bca612 2095 frame_arg = Qlambda;
0f8fe9a2 2096 else if (EQ (frames, make_number (0)))
f812f9c6 2097 frame_arg = frames;
89bca612
RS
2098 else if (EQ (frames, Qvisible))
2099 frame_arg = frames;
118ea242
GM
2100 else
2101 frame_arg = Qt;
7ab12479 2102
89bca612
RS
2103 /* frame_arg is Qlambda to stick to one frame,
2104 Qvisible to consider all visible frames,
2105 or Qt otherwise. */
2106
7ab12479 2107 /* Pick a window to start with. */
017b2bad 2108 if (WINDOWP (obj))
118ea242
GM
2109 window = obj;
2110 else if (f)
2111 window = FRAME_SELECTED_WINDOW (f);
7ab12479 2112 else
118ea242 2113 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
4b206065 2114
cd2904bd 2115 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
118ea242 2116 GCPRO1 (windows);
7ab12479 2117 best_window = Qnil;
118ea242 2118
e67a1dea 2119 for (; CONSP (windows); windows = XCDR (windows))
7ab12479 2120 {
118ea242 2121 struct window *w;
177c0ea7 2122
118ea242
GM
2123 window = XCAR (windows);
2124 w = XWINDOW (window);
177c0ea7 2125
118ea242
GM
2126 /* Note that we do not pay attention here to whether the frame
2127 is visible, since Fwindow_list skips non-visible frames if
2128 that is desired, under the control of frame_arg. */
2129 if (!MINI_WINDOW_P (w)
65a04b96
RS
2130 /* For UNSHOW_BUFFER, we must always consider all windows. */
2131 || type == UNSHOW_BUFFER
7ab12479
JB
2132 || (mini && minibuf_level > 0))
2133 switch (type)
2134 {
2135 case GET_BUFFER_WINDOW:
118ea242 2136 if (EQ (w->buffer, obj)
5c204627
RS
2137 /* Don't find any minibuffer window
2138 except the one that is currently in use. */
118ea242
GM
2139 && (MINI_WINDOW_P (w)
2140 ? EQ (window, minibuf_window)
2141 : 1))
2142 {
5ddc1b75
GM
2143 if (NILP (best_window))
2144 best_window = window;
2145 else if (EQ (window, selected_window))
2146 /* For compatibility with 20.x, prefer to return
2147 selected-window. */
2148 best_window = window;
118ea242 2149 }
7ab12479
JB
2150 break;
2151
2152 case GET_LRU_WINDOW:
20b69789
SM
2153 /* `obj' is an integer encoding a bitvector.
2154 `obj & 1' means consider only full-width windows.
2155 `obj & 2' means consider also dedicated windows. */
2156 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
3f489dc7 2157 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
20b69789
SM
2158 /* Minibuffer windows are always ignored. */
2159 || MINI_WINDOW_P (w))
7ab12479 2160 break;
265a9e55 2161 if (NILP (best_window)
7ab12479 2162 || (XFASTINT (XWINDOW (best_window)->use_time)
118ea242
GM
2163 > XFASTINT (w->use_time)))
2164 best_window = window;
7ab12479
JB
2165 break;
2166
2167 case DELETE_OTHER_WINDOWS:
118ea242
GM
2168 if (!EQ (window, obj))
2169 Fdelete_window (window);
7ab12479
JB
2170 break;
2171
2172 case DELETE_BUFFER_WINDOWS:
118ea242 2173 if (EQ (w->buffer, obj))
7ab12479 2174 {
118ea242 2175 struct frame *f = XFRAME (WINDOW_FRAME (w));
3548e138
RS
2176
2177 /* If this window is dedicated, and in a frame of its own,
2178 kill the frame. */
118ea242
GM
2179 if (EQ (window, FRAME_ROOT_WINDOW (f))
2180 && !NILP (w->dedicated)
3548e138 2181 && other_visible_frames (f))
7ab12479 2182 {
3548e138
RS
2183 /* Skip the other windows on this frame.
2184 There might be one, the minibuffer! */
118ea242
GM
2185 while (CONSP (XCDR (windows))
2186 && EQ (XWINDOW (XCAR (windows))->frame,
2187 XWINDOW (XCAR (XCDR (windows)))->frame))
2188 windows = XCDR (windows);
177c0ea7 2189
3548e138 2190 /* Now we can safely delete the frame. */
118ea242
GM
2191 Fdelete_frame (w->frame, Qnil);
2192 }
2193 else if (NILP (w->parent))
2194 {
2195 /* If we're deleting the buffer displayed in the
2196 only window on the frame, find a new buffer to
2197 display there. */
2198 Lisp_Object buffer;
2199 buffer = Fother_buffer (obj, Qnil, w->frame);
949cf20f 2200 Fset_window_buffer (window, buffer, Qnil);
118ea242
GM
2201 if (EQ (window, selected_window))
2202 Fset_buffer (w->buffer);
7ab12479
JB
2203 }
2204 else
118ea242 2205 Fdelete_window (window);
7ab12479
JB
2206 }
2207 break;
2208
2209 case GET_LARGEST_WINDOW:
20b69789 2210 { /* nil `obj' means to ignore dedicated windows. */
118ea242 2211 /* Ignore dedicated windows and minibuffers. */
3f489dc7 2212 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
118ea242 2213 break;
177c0ea7 2214
c930dfab 2215 if (NILP (best_window))
118ea242 2216 best_window = window;
c930dfab
GM
2217 else
2218 {
2219 struct window *b = XWINDOW (best_window);
949cf20f
KS
2220 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2221 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
c930dfab
GM
2222 best_window = window;
2223 }
7ab12479
JB
2224 }
2225 break;
2226
2227 case UNSHOW_BUFFER:
118ea242 2228 if (EQ (w->buffer, obj))
7ab12479 2229 {
118ea242
GM
2230 Lisp_Object buffer;
2231 struct frame *f = XFRAME (w->frame);
177c0ea7 2232
7ab12479 2233 /* Find another buffer to show in this window. */
118ea242 2234 buffer = Fother_buffer (obj, Qnil, w->frame);
177c0ea7 2235
38ab08d1
RS
2236 /* If this window is dedicated, and in a frame of its own,
2237 kill the frame. */
118ea242
GM
2238 if (EQ (window, FRAME_ROOT_WINDOW (f))
2239 && !NILP (w->dedicated)
38ab08d1 2240 && other_visible_frames (f))
45945a7b
RS
2241 {
2242 /* Skip the other windows on this frame.
2243 There might be one, the minibuffer! */
118ea242
GM
2244 while (CONSP (XCDR (windows))
2245 && EQ (XWINDOW (XCAR (windows))->frame,
2246 XWINDOW (XCAR (XCDR (windows)))->frame))
2247 windows = XCDR (windows);
177c0ea7 2248
45945a7b 2249 /* Now we can safely delete the frame. */
118ea242 2250 Fdelete_frame (w->frame, Qnil);
45945a7b 2251 }
a5731348
SM
2252 else if (!NILP (w->dedicated) && !NILP (w->parent))
2253 {
2254 Lisp_Object window;
2255 XSETWINDOW (window, w);
2256 /* If this window is dedicated and not the only window
2257 in its frame, then kill it. */
2258 Fdelete_window (window);
2259 }
38ab08d1 2260 else
38ab08d1
RS
2261 {
2262 /* Otherwise show a different buffer in the window. */
118ea242 2263 w->dedicated = Qnil;
949cf20f 2264 Fset_window_buffer (window, buffer, Qnil);
118ea242
GM
2265 if (EQ (window, selected_window))
2266 Fset_buffer (w->buffer);
38ab08d1 2267 }
7ab12479
JB
2268 }
2269 break;
3f8ab7bd 2270
e661376d
KS
2271 case REDISPLAY_BUFFER_WINDOWS:
2272 if (EQ (w->buffer, obj))
2273 {
2274 mark_window_display_accurate (window, 0);
40c6ee74
KS
2275 w->update_mode_line = Qt;
2276 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2277 ++update_mode_lines;
e661376d
KS
2278 best_window = window;
2279 }
2280 break;
2281
3f8ab7bd
RS
2282 /* Check for a window that has a killed buffer. */
2283 case CHECK_ALL_WINDOWS:
118ea242
GM
2284 if (! NILP (w->buffer)
2285 && NILP (XBUFFER (w->buffer)->name))
3f8ab7bd 2286 abort ();
118ea242 2287 break;
6bbd7a29
GM
2288
2289 case WINDOW_LOOP_UNUSED:
2290 break;
7ab12479 2291 }
7ab12479 2292 }
7ab12479 2293
118ea242 2294 UNGCPRO;
7ab12479 2295 return best_window;
37962e60 2296}
605be8af 2297
3f8ab7bd
RS
2298/* Used for debugging. Abort if any window has a dead buffer. */
2299
233a4a2c 2300void
3f8ab7bd
RS
2301check_all_windows ()
2302{
2303 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2304}
2305
3cbb13c8 2306DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
fdb82f93 2307 doc: /* Return the window least recently selected or used for display.
a899216f 2308Return a full-width window if possible.
c2755926 2309A minibuffer window is never a candidate.
3cbb13c8
SM
2310A dedicated window is never a candidate, unless DEDICATED is non-nil,
2311 so if all windows are dedicated, the value is nil.
fdb82f93
PJ
2312If optional argument FRAME is `visible', search all visible frames.
2313If FRAME is 0, search all visible and iconified frames.
2314If FRAME is t, search all frames.
2315If FRAME is nil, search only the selected frame.
2316If FRAME is a frame, search only that frame. */)
3cbb13c8
SM
2317 (frame, dedicated)
2318 Lisp_Object frame, dedicated;
7ab12479
JB
2319{
2320 register Lisp_Object w;
2321 /* First try for a window that is full-width */
20b69789
SM
2322 w = window_loop (GET_LRU_WINDOW,
2323 NILP (dedicated) ? make_number (1) : make_number (3),
2324 0, frame);
265a9e55 2325 if (!NILP (w) && !EQ (w, selected_window))
7ab12479
JB
2326 return w;
2327 /* If none of them, try the rest */
20b69789
SM
2328 return window_loop (GET_LRU_WINDOW,
2329 NILP (dedicated) ? make_number (0) : make_number (2),
2330 0, frame);
7ab12479
JB
2331}
2332
3cbb13c8 2333DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
fdb82f93 2334 doc: /* Return the largest window in area.
c2755926 2335A minibuffer window is never a candidate.
3cbb13c8
SM
2336A dedicated window is never a candidate unless DEDICATED is non-nil,
2337 so if all windows are dedicated, the value is nil.
fdb82f93
PJ
2338If optional argument FRAME is `visible', search all visible frames.
2339If FRAME is 0, search all visible and iconified frames.
2340If FRAME is t, search all frames.
2341If FRAME is nil, search only the selected frame.
2342If FRAME is a frame, search only that frame. */)
3cbb13c8
SM
2343 (frame, dedicated)
2344 Lisp_Object frame, dedicated;
7ab12479 2345{
20b69789 2346 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
44fa5b1e 2347 frame);
7ab12479
JB
2348}
2349
2350DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
fdb82f93 2351 doc: /* Return a window currently displaying BUFFER, or nil if none.
c2755926 2352BUFFER can be a buffer or a buffer name.
fdb82f93
PJ
2353If optional argument FRAME is `visible', search all visible frames.
2354If optional argument FRAME is 0, search all visible and iconified frames.
2355If FRAME is t, search all frames.
2356If FRAME is nil, search only the selected frame.
2357If FRAME is a frame, search only that frame. */)
2358 (buffer, frame)
2359 Lisp_Object buffer, frame;
7ab12479
JB
2360{
2361 buffer = Fget_buffer (buffer);
017b2bad 2362 if (BUFFERP (buffer))
44fa5b1e 2363 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
7ab12479
JB
2364 else
2365 return Qnil;
2366}
2367
2368DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
fdb82f93
PJ
2369 0, 1, "",
2370 doc: /* Make WINDOW (or the selected window) fill its frame.
2371Only the frame WINDOW is on is affected.
2372This function tries to reduce display jumps
2373by keeping the text previously visible in WINDOW
2374in the same place on the frame. Doing this depends on
2375the value of (window-start WINDOW), so if calling this function
2376in a program gives strange scrolling, make sure the window-start
2377value is reasonable when this function is called. */)
2378 (window)
7ab12479
JB
2379 Lisp_Object window;
2380{
2381 struct window *w;
00d3d838 2382 int startpos;
85fe3b5e 2383 int top, new_top;
7ab12479 2384
265a9e55 2385 if (NILP (window))
7ab12479
JB
2386 window = selected_window;
2387 else
b7826503 2388 CHECK_LIVE_WINDOW (window);
7ab12479 2389 w = XWINDOW (window);
a2b38b3c 2390
00d3d838 2391 startpos = marker_position (w->start);
949cf20f 2392 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
7ab12479 2393
a2b38b3c
RS
2394 if (MINI_WINDOW_P (w) && top > 0)
2395 error ("Can't expand minibuffer to full frame");
2396
70728a80 2397 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
7ab12479 2398
00d3d838
KH
2399 /* Try to minimize scrolling, by setting the window start to the point
2400 will cause the text at the old window start to be at the same place
2401 on the frame. But don't try to do this if the window start is
2402 outside the visible portion (as might happen when the display is
2403 not current, due to typeahead). */
949cf20f 2404 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
85fe3b5e
GM
2405 if (new_top != top
2406 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
00d3d838
KH
2407 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2408 {
2409 struct position pos;
2410 struct buffer *obuf = current_buffer;
2411
2412 Fset_buffer (w->buffer);
2413 /* This computation used to temporarily move point, but that can
2414 have unwanted side effects due to text properties. */
0383eb57 2415 pos = *vmotion (startpos, -top, w);
4d047f50 2416
b73ea88e 2417 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
85fe3b5e 2418 w->window_end_valid = Qnil;
b73ea88e
RS
2419 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2420 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
00d3d838 2421 : Qnil);
80622eec
RS
2422 /* We need to do this, so that the window-scroll-functions
2423 get called. */
4d047f50 2424 w->optional_new_start = Qt;
00d3d838
KH
2425
2426 set_buffer_internal (obuf);
2427 }
5500c422 2428
7ab12479
JB
2429 return Qnil;
2430}
2431
2432DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
fdb82f93
PJ
2433 1, 2, "bDelete windows on (buffer): ",
2434 doc: /* Delete all windows showing BUFFER.
c2755926 2435BUFFER must be a buffer or the name of an existing buffer.
fdb82f93
PJ
2436Optional second argument FRAME controls which frames are affected.
2437If optional argument FRAME is `visible', search all visible frames.
2438If FRAME is 0, search all visible and iconified frames.
2439If FRAME is nil, search all frames.
2440If FRAME is t, search only the selected frame.
2441If FRAME is a frame, search only that frame. */)
2442 (buffer, frame)
26f6279d 2443 Lisp_Object buffer, frame;
7ab12479 2444{
26f6279d
JB
2445 /* FRAME uses t and nil to mean the opposite of what window_loop
2446 expects. */
c520265e
RS
2447 if (NILP (frame))
2448 frame = Qt;
2449 else if (EQ (frame, Qt))
2450 frame = Qnil;
26f6279d 2451
265a9e55 2452 if (!NILP (buffer))
7ab12479
JB
2453 {
2454 buffer = Fget_buffer (buffer);
b7826503 2455 CHECK_BUFFER (buffer);
26f6279d 2456 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
7ab12479 2457 }
177c0ea7 2458
7ab12479
JB
2459 return Qnil;
2460}
2461
2462DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
fdb82f93
PJ
2463 Sreplace_buffer_in_windows,
2464 1, 1, "bReplace buffer in windows: ",
c2755926
LT
2465 doc: /* Replace BUFFER with some other buffer in all windows showing it.
2466BUFFER may be a buffer or the name of an existing buffer. */)
fdb82f93 2467 (buffer)
7ab12479
JB
2468 Lisp_Object buffer;
2469{
265a9e55 2470 if (!NILP (buffer))
7ab12479
JB
2471 {
2472 buffer = Fget_buffer (buffer);
b7826503 2473 CHECK_BUFFER (buffer);
7ab12479
JB
2474 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2475 }
2476 return Qnil;
2477}
ff58478b
RS
2478
2479/* Replace BUFFER with some other buffer in all windows
2480 of all frames, even those on other keyboards. */
2481
2482void
2483replace_buffer_in_all_windows (buffer)
2484 Lisp_Object buffer;
2485{
27abb84f 2486#ifdef MULTI_KBOARD
ff58478b
RS
2487 Lisp_Object tail, frame;
2488
ff58478b
RS
2489 /* A single call to window_loop won't do the job
2490 because it only considers frames on the current keyboard.
2491 So loop manually over frames, and handle each one. */
2492 FOR_EACH_FRAME (tail, frame)
db7f721d 2493 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
ff58478b 2494#else
db7f721d 2495 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
ff58478b
RS
2496#endif
2497}
7ab12479
JB
2498\f
2499/* Set the height of WINDOW and all its inferiors. */
a481b3ea
JB
2500
2501/* The smallest acceptable dimensions for a window. Anything smaller
2502 might crash Emacs. */
5500c422 2503
a481b3ea 2504#define MIN_SAFE_WINDOW_WIDTH (2)
dc1ab1ee 2505#define MIN_SAFE_WINDOW_HEIGHT (1)
a481b3ea
JB
2506
2507/* Make sure that window_min_height and window_min_width are
2508 not too small; if they are, set them to safe minima. */
2509
2510static void
2511check_min_window_sizes ()
2512{
2513 /* Smaller values might permit a crash. */
2514 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2515 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2516 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2517 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2518}
2519
2520/* If *ROWS or *COLS are too small a size for FRAME, set them to the
2521 minimum allowable size. */
5500c422 2522
605be8af 2523void
a481b3ea 2524check_frame_size (frame, rows, cols)
605be8af
JB
2525 FRAME_PTR frame;
2526 int *rows, *cols;
a481b3ea 2527{
628df3bf 2528 /* For height, we have to see:
54b8bcb5
RS
2529 how many windows the frame has at minimum (one or two),
2530 and whether it has a menu bar or other special stuff at the top. */
2531 int min_height
2532 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2533 ? MIN_SAFE_WINDOW_HEIGHT
2534 : 2 * MIN_SAFE_WINDOW_HEIGHT);
177c0ea7 2535
5500c422
GM
2536 if (FRAME_TOP_MARGIN (frame) > 0)
2537 min_height += FRAME_TOP_MARGIN (frame);
a481b3ea
JB
2538
2539 if (*rows < min_height)
2540 *rows = min_height;
2541 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2542 *cols = MIN_SAFE_WINDOW_WIDTH;
2543}
2544
c1636aa6 2545
233a4a2c
GM
2546/* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2547 check if W's width can be changed, otherwise check W's height.
2548 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2549 siblings, too. If none of the siblings is resizable, WINDOW isn't
2550 either. */
c1636aa6 2551
233a4a2c
GM
2552static int
2553window_fixed_size_p (w, width_p, check_siblings_p)
2554 struct window *w;
2555 int width_p, check_siblings_p;
2556{
2557 int fixed_p;
2558 struct window *c;
177c0ea7 2559
233a4a2c
GM
2560 if (!NILP (w->hchild))
2561 {
2562 c = XWINDOW (w->hchild);
177c0ea7 2563
233a4a2c
GM
2564 if (width_p)
2565 {
2566 /* A horiz. combination is fixed-width if all of if its
2567 children are. */
2568 while (c && window_fixed_size_p (c, width_p, 0))
2569 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2570 fixed_p = c == NULL;
2571 }
2572 else
2573 {
2574 /* A horiz. combination is fixed-height if one of if its
2575 children is. */
2576 while (c && !window_fixed_size_p (c, width_p, 0))
2577 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2578 fixed_p = c != NULL;
2579 }
2580 }
2581 else if (!NILP (w->vchild))
2582 {
2583 c = XWINDOW (w->vchild);
177c0ea7 2584
233a4a2c
GM
2585 if (width_p)
2586 {
2587 /* A vert. combination is fixed-width if one of if its
2588 children is. */
2589 while (c && !window_fixed_size_p (c, width_p, 0))
2590 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2591 fixed_p = c != NULL;
2592 }
2593 else
2594 {
2595 /* A vert. combination is fixed-height if all of if its
2596 children are. */
2597 while (c && window_fixed_size_p (c, width_p, 0))
2598 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2599 fixed_p = c == NULL;
2600 }
2601 }
2602 else if (BUFFERP (w->buffer))
2603 {
3df0580e
AS
2604 struct buffer *old = current_buffer;
2605 Lisp_Object val;
177c0ea7 2606
3df0580e
AS
2607 current_buffer = XBUFFER (w->buffer);
2608 val = find_symbol_value (Qwindow_size_fixed);
2609 current_buffer = old;
a34dfd12 2610
3df0580e
AS
2611 fixed_p = 0;
2612 if (!EQ (val, Qunbound))
2613 {
2614 fixed_p = !NILP (val);
177c0ea7 2615
3df0580e
AS
2616 if (fixed_p
2617 && ((EQ (val, Qheight) && width_p)
2618 || (EQ (val, Qwidth) && !width_p)))
2619 fixed_p = 0;
233a4a2c
GM
2620 }
2621
2622 /* Can't tell if this one is resizable without looking at
2623 siblings. If all siblings are fixed-size this one is too. */
2624 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2625 {
2626 Lisp_Object child;
177c0ea7 2627
233a4a2c
GM
2628 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2629 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2630 break;
2631
2632 if (NILP (child))
2633 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2634 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2635 break;
2636
2637 if (NILP (child))
2638 fixed_p = 1;
2639 }
2640 }
2641 else
2642 fixed_p = 1;
2643
2644 return fixed_p;
2645}
177c0ea7 2646
233a4a2c
GM
2647
2648/* Return the minimum size of window W, not taking fixed-width windows
2649 into account. WIDTH_P non-zero means return the minimum width,
2650 otherwise return the minimum height. If W is a combination window,
2651 compute the minimum size from the minimum sizes of W's children. */
2652
2653static int
2654window_min_size_1 (w, width_p)
c1636aa6
GM
2655 struct window *w;
2656 int width_p;
2657{
233a4a2c 2658 struct window *c;
c1636aa6 2659 int size;
177c0ea7 2660
233a4a2c
GM
2661 if (!NILP (w->hchild))
2662 {
2663 c = XWINDOW (w->hchild);
2664 size = 0;
177c0ea7 2665
233a4a2c
GM
2666 if (width_p)
2667 {
2668 /* The min width of a horizontal combination is
2669 the sum of the min widths of its children. */
2670 while (c)
2671 {
2672 size += window_min_size_1 (c, width_p);
2673 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2674 }
2675 }
2676 else
2677 {
2678 /* The min height a horizontal combination equals
2679 the maximum of all min height of its children. */
2680 while (c)
2681 {
2682 int min_size = window_min_size_1 (c, width_p);
2683 size = max (min_size, size);
2684 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2685 }
2686 }
2687 }
2688 else if (!NILP (w->vchild))
2689 {
2690 c = XWINDOW (w->vchild);
2691 size = 0;
177c0ea7 2692
233a4a2c
GM
2693 if (width_p)
2694 {
2695 /* The min width of a vertical combination is
2696 the maximum of the min widths of its children. */
2697 while (c)
2698 {
2699 int min_size = window_min_size_1 (c, width_p);
2700 size = max (min_size, size);
2701 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2702 }
2703 }
2704 else
2705 {
2706 /* The min height of a vertical combination equals
2707 the sum of the min height of its children. */
2708 while (c)
2709 {
2710 size += window_min_size_1 (c, width_p);
2711 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2712 }
2713 }
2714 }
c1636aa6
GM
2715 else
2716 {
233a4a2c 2717 if (width_p)
da7520de
CY
2718 size = max (window_min_width,
2719 (MIN_SAFE_WINDOW_WIDTH
2720 + WINDOW_FRINGE_COLS (w)
2721 + WINDOW_SCROLL_BAR_COLS (w)));
c1636aa6 2722 else
233a4a2c
GM
2723 {
2724 if (MINI_WINDOW_P (w)
2725 || (!WINDOW_WANTS_MODELINE_P (w)
045dee35 2726 && !WINDOW_WANTS_HEADER_LINE_P (w)))
233a4a2c
GM
2727 size = 1;
2728 else
2729 size = window_min_height;
2730 }
c1636aa6
GM
2731 }
2732
2733 return size;
2734}
2735
2736
233a4a2c
GM
2737/* Return the minimum size of window W, taking fixed-size windows into
2738 account. WIDTH_P non-zero means return the minimum width,
f984d4fc
GM
2739 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2740 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2741 unless FIXED is null. */
7ab12479 2742
233a4a2c 2743static int
f984d4fc 2744window_min_size (w, width_p, ignore_fixed_p, fixed)
233a4a2c 2745 struct window *w;
f984d4fc 2746 int width_p, ignore_fixed_p, *fixed;
233a4a2c
GM
2747{
2748 int size, fixed_p;
2749
f984d4fc
GM
2750 if (ignore_fixed_p)
2751 fixed_p = 0;
2752 else
2753 fixed_p = window_fixed_size_p (w, width_p, 1);
177c0ea7 2754
233a4a2c
GM
2755 if (fixed)
2756 *fixed = fixed_p;
177c0ea7 2757
233a4a2c 2758 if (fixed_p)
949cf20f 2759 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
233a4a2c
GM
2760 else
2761 size = window_min_size_1 (w, width_p);
177c0ea7 2762
233a4a2c
GM
2763 return size;
2764}
2765
2766
79fd290e 2767/* Adjust the margins of window W if text area is too small.
949cf20f
KS
2768 Return 1 if window width is ok after adjustment; 0 if window
2769 is still too narrow. */
2770
2771static int
2772adjust_window_margins (w)
2773 struct window *w;
2774{
2775 int box_cols = (WINDOW_TOTAL_COLS (w)
2776 - WINDOW_FRINGE_COLS (w)
2777 - WINDOW_SCROLL_BAR_COLS (w));
2778 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2779 + WINDOW_RIGHT_MARGIN_COLS (w));
2780
2781 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2782 return 1;
2783
2784 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2785 return 0;
2786
2787 /* Window's text area is too narrow, but reducing the window
2788 margins will fix that. */
2789 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2790 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2791 {
2792 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2793 w->left_margin_cols = w->right_margin_cols
2794 = make_number (margin_cols/2);
2795 else
2796 w->right_margin_cols = make_number (margin_cols);
2797 }
2798 else
2799 w->left_margin_cols = make_number (margin_cols);
2800 return 1;
2801}
2802
0aa513ed
JD
2803/* Calculate new sizes for windows in the list FORWARD when the window size
2804 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2805 The number of windows in FORWARD is NCHILDREN, and the number that
2806 can shrink is SHRINKABLE.
2807 The minimum size a window can have is MIN_SIZE.
2808 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2809 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2810 shrinking rows.
2811
2812 This function returns an allocated array of new sizes that the caller
2813 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2814 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2815 the second, and so on.
2816
2817 This function tries to keep windows at least at the minimum size
2818 and resize other windows before it resizes any window to zero (i.e.
2819 delete that window).
2820
2821 Windows are resized proportional to their size, so bigger windows
2822 shrink more than smaller windows. */
2823static int *
2824shrink_windows (total, size, nchildren, shrinkable,
2825 min_size, resize_fixed_p, forward, width_p)
2826 int total, size, nchildren, shrinkable, min_size;
2827 int resize_fixed_p, width_p;
2828 Lisp_Object forward;
2829{
2830 int available_resize = 0;
2831 int *new_sizes;
2832 struct window *c;
2833 Lisp_Object child;
2834 int smallest = total;
2835 int total_removed = 0;
2836 int total_shrink = total - size;
2837 int i;
2838
2839 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2840
2841 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2842 {
2843 int child_size;
2844
2845 c = XWINDOW (child);
2846 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2847
2848 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2849 new_sizes[i] = -1;
2850 else
2851 {
2852 new_sizes[i] = child_size;
2853 if (child_size > min_size)
2854 available_resize += child_size - min_size;
2855 }
2856 }
2857 /* We might need to shrink some windows to zero. Find the smallest
2858 windows and set them to 0 until we can fulfil the new size. */
2859
2860 while (shrinkable > 1 && size + available_resize < total)
2861 {
2862 for (i = 0; i < nchildren; ++i)
2863 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2864 smallest = new_sizes[i];
2865
2866 for (i = 0; i < nchildren; ++i)
2867 if (new_sizes[i] == smallest)
2868 {
2869 /* Resize this window down to zero. */
2870 new_sizes[i] = 0;
2871 if (smallest > min_size)
2872 available_resize -= smallest - min_size;
2873 available_resize += smallest;
2874 --shrinkable;
2875 total_removed += smallest;
2876
ef614e04
JD
2877 /* We don't know what the smallest is now. */
2878 smallest = total;
2879
0aa513ed
JD
2880 /* Out of for, just remove one window at the time and
2881 check again if we have enough space. */
2882 break;
2883 }
2884 }
2885
2886 /* Now, calculate the new sizes. Try to shrink each window
2887 proportional to its size. */
2888 for (i = 0; i < nchildren; ++i)
2889 {
2890 if (new_sizes[i] > min_size)
2891 {
2892 int to_shrink = total_shrink*new_sizes[i]/total;
2893 if (new_sizes[i] - to_shrink < min_size)
2894 to_shrink = new_sizes[i] - min_size;
2895 new_sizes[i] -= to_shrink;
2896 total_removed += to_shrink;
2897 }
2898 }
2899
2900 /* Any reminder due to rounding, we just subtract from windows
2901 that are left and still can be shrunk. */
2902 while (total_shrink > total_removed)
2903 {
ef614e04
JD
2904 int nonzero_sizes = 0;
2905 int nonzero_idx = -1;
2906
2907 for (i = 0; i < nchildren; ++i)
2908 if (new_sizes[i] > 0)
2909 {
2910 ++nonzero_sizes;
2911 nonzero_idx = i;
2912 }
c49a0495 2913
0aa513ed
JD
2914 for (i = 0; i < nchildren; ++i)
2915 if (new_sizes[i] > min_size)
2916 {
2917 --new_sizes[i];
2918 ++total_removed;
2919
2920 /* Out of for, just shrink one window at the time and
2921 check again if we have enough space. */
2922 break;
2923 }
ef614e04
JD
2924
2925
2926 /* Special case, only one window left. */
2927 if (nonzero_sizes == 1)
2928 break;
2929 }
2930
2931 /* Any surplus due to rounding, we add to windows that are left. */
2932 while (total_shrink < total_removed)
2933 {
2934 for (i = 0; i < nchildren; ++i)
2935 {
2936 if (new_sizes[i] != 0 && total_shrink < total_removed)
2937 {
2938 ++new_sizes[i];
2939 --total_removed;
2940 break;
2941 }
2942 }
0aa513ed
JD
2943 }
2944
2945 return new_sizes;
2946}
949cf20f 2947
233a4a2c
GM
2948/* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2949 WINDOW's width. Resize WINDOW's children, if any, so that they
5fe0b054
RS
2950 keep their proportionate size relative to WINDOW.
2951
2952 If FIRST_ONLY is 1, change only the first of WINDOW's children when
2953 they are in series. If LAST_ONLY is 1, change only the last of
2954 WINDOW's children when they are in series.
2955
2956 Propagate WINDOW's top or left edge position to children. Delete
2957 windows that become too small unless NODELETE_P is non-zero.
192c3131
RS
2958
2959 If NODELETE_P is 2, that means we do delete windows that are
2960 too small, even if they were too small before! */
233a4a2c
GM
2961
2962static void
5fe0b054 2963size_window (window, size, width_p, nodelete_p, first_only, last_only)
7ab12479 2964 Lisp_Object window;
233a4a2c 2965 int size, width_p, nodelete_p;
5fe0b054 2966 int first_only, last_only;
7ab12479 2967{
233a4a2c
GM
2968 struct window *w = XWINDOW (window);
2969 struct window *c;
2970 Lisp_Object child, *forward, *sideward;
949cf20f 2971 int old_size, min_size, safe_min_size;
7ab12479 2972
949cf20f
KS
2973 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2974 seems like it's too soon to do this here. ++KFS. */
192c3131
RS
2975 if (nodelete_p == 2)
2976 nodelete_p = 0;
2977
a481b3ea 2978 check_min_window_sizes ();
7ae2f10f 2979 size = max (0, size);
177c0ea7 2980
b5f05b50
GM
2981 /* If the window has been "too small" at one point,
2982 don't delete it for being "too small" in the future.
2983 Preserve it as long as that is at all possible. */
233a4a2c
GM
2984 if (width_p)
2985 {
949cf20f 2986 old_size = WINDOW_TOTAL_COLS (w);
233a4a2c 2987 min_size = window_min_width;
949cf20f
KS
2988 /* Ensure that there is room for the scroll bar and fringes!
2989 We may reduce display margins though. */
2990 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2991 + WINDOW_FRINGE_COLS (w)
2992 + WINDOW_SCROLL_BAR_COLS (w));
233a4a2c
GM
2993 }
2994 else
2995 {
949cf20f 2996 old_size = XINT (w->total_lines);
233a4a2c 2997 min_size = window_min_height;
949cf20f 2998 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
233a4a2c 2999 }
192c3131
RS
3000
3001 if (old_size < min_size && nodelete_p != 2)
b5f05b50
GM
3002 w->too_small_ok = Qt;
3003
233a4a2c 3004 /* Maybe delete WINDOW if it's too small. */
192c3131 3005 if (nodelete_p != 1 && !NILP (w->parent))
7ab12479 3006 {
b5f05b50 3007 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
233a4a2c 3008 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
949cf20f
KS
3009 if (min_size < safe_min_size)
3010 min_size = safe_min_size;
233a4a2c 3011 if (size < min_size)
c1636aa6
GM
3012 {
3013 delete_window (window);
3014 return;
3015 }
7ab12479
JB
3016 }
3017
233a4a2c 3018 /* Set redisplay hints. */
7ae2f10f
GM
3019 w->last_modified = make_number (0);
3020 w->last_overlay_modified = make_number (0);
7ab12479 3021 windows_or_buffers_changed++;
7ae2f10f 3022 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
29aeee73 3023
233a4a2c
GM
3024 if (width_p)
3025 {
3026 sideward = &w->vchild;
3027 forward = &w->hchild;
949cf20f
KS
3028 w->total_cols = make_number (size);
3029 adjust_window_margins (w);
233a4a2c
GM
3030 }
3031 else
3032 {
3033 sideward = &w->hchild;
3034 forward = &w->vchild;
949cf20f
KS
3035 w->total_lines = make_number (size);
3036 w->orig_total_lines = Qnil;
233a4a2c
GM
3037 }
3038
3039 if (!NILP (*sideward))
7ab12479 3040 {
5fe0b054 3041 /* We have a chain of parallel siblings whose size should all change. */
233a4a2c 3042 for (child = *sideward; !NILP (child); child = c->next)
7ab12479 3043 {
233a4a2c
GM
3044 c = XWINDOW (child);
3045 if (width_p)
949cf20f 3046 c->left_col = w->left_col;
233a4a2c 3047 else
949cf20f 3048 c->top_line = w->top_line;
5fe0b054
RS
3049 size_window (child, size, width_p, nodelete_p,
3050 first_only, last_only);
7ab12479
JB
3051 }
3052 }
5fe0b054
RS
3053 else if (!NILP (*forward) && last_only)
3054 {
3055 /* Change the last in a series of siblings. */
3056 Lisp_Object last_child;
3057 int child_size;
3058
3059 for (child = *forward; !NILP (child); child = c->next)
3060 {
3061 c = XWINDOW (child);
3062 last_child = child;
3063 }
3064
3065 child_size = XINT (width_p ? c->total_cols : c->total_lines);
3066 size_window (last_child,
3067 size - old_size + child_size,
3068 width_p, nodelete_p, first_only, last_only);
3069 }
3070 else if (!NILP (*forward) && first_only)
3071 {
3072 /* Change the first in a series of siblings. */
3073 int child_size;
3074
3075 child = *forward;
3076 c = XWINDOW (child);
3077
3078 if (width_p)
3079 c->left_col = w->left_col;
3080 else
3081 c->top_line = w->top_line;
3082
3083 child_size = XINT (width_p ? c->total_cols : c->total_lines);
3084 size_window (child,
3085 size - old_size + child_size,
3086 width_p, nodelete_p, first_only, last_only);
3087 }
233a4a2c 3088 else if (!NILP (*forward))
7ab12479 3089 {
233a4a2c
GM
3090 int fixed_size, each, extra, n;
3091 int resize_fixed_p, nfixed;
8b6d9dc9 3092 int last_pos, first_pos, nchildren, total;
0aa513ed 3093 int *new_sizes = NULL;
233a4a2c 3094
5fe0b054 3095 /* Determine the fixed-size portion of this window, and the
233a4a2c 3096 number of child windows. */
8b6d9dc9 3097 fixed_size = nchildren = nfixed = total = 0;
233a4a2c 3098 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
7ab12479 3099 {
8b6d9dc9 3100 int child_size;
177c0ea7 3101
7ab12479 3102 c = XWINDOW (child);
949cf20f 3103 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
8b6d9dc9 3104 total += child_size;
177c0ea7 3105
233a4a2c
GM
3106 if (window_fixed_size_p (c, width_p, 0))
3107 {
8b6d9dc9 3108 fixed_size += child_size;
233a4a2c
GM
3109 ++nfixed;
3110 }
3111 }
7ab12479 3112
233a4a2c
GM
3113 /* If the new size is smaller than fixed_size, or if there
3114 aren't any resizable windows, allow resizing fixed-size
3115 windows. */
3116 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3117
0aa513ed 3118 /* Compute how many lines/columns to add/remove to each child. The
233a4a2c
GM
3119 value of extra takes care of rounding errors. */
3120 n = resize_fixed_p ? nchildren : nchildren - nfixed;
0aa513ed
JD
3121 if (size < total && n > 1)
3122 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
3123 resize_fixed_p, *forward, width_p);
3124 else
3125 {
3126 each = (size - total) / n;
3127 extra = (size - total) - n * each;
3128 }
233a4a2c
GM
3129
3130 /* Compute new children heights and edge positions. */
949cf20f 3131 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
233a4a2c 3132 last_pos = first_pos;
0aa513ed 3133 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
233a4a2c
GM
3134 {
3135 int new_size, old_size;
177c0ea7 3136
233a4a2c 3137 c = XWINDOW (child);
949cf20f 3138 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
233a4a2c 3139 new_size = old_size;
7ab12479 3140
233a4a2c
GM
3141 /* The top or left edge position of this child equals the
3142 bottom or right edge of its predecessor. */
3143 if (width_p)
949cf20f 3144 c->left_col = make_number (last_pos);
233a4a2c 3145 else
949cf20f 3146 c->top_line = make_number (last_pos);
7ab12479 3147
233a4a2c
GM
3148 /* If this child can be resized, do it. */
3149 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3150 {
0aa513ed 3151 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
233a4a2c
GM
3152 extra = 0;
3153 }
177c0ea7 3154
233a4a2c
GM
3155 /* Set new height. Note that size_window also propagates
3156 edge positions to children, so it's not a no-op if we
3157 didn't change the child's size. */
5fe0b054 3158 size_window (child, new_size, width_p, 1, first_only, last_only);
233a4a2c
GM
3159
3160 /* Remember the bottom/right edge position of this child; it
3161 will be used to set the top/left edge of the next child. */
0aa513ed 3162 last_pos += new_size;
7ab12479 3163 }
233a4a2c 3164
0aa513ed
JD
3165 if (new_sizes) xfree (new_sizes);
3166
233a4a2c
GM
3167 /* We should have covered the parent exactly with child windows. */
3168 xassert (size == last_pos - first_pos);
177c0ea7 3169
7ab12479 3170 /* Now delete any children that became too small. */
233a4a2c
GM
3171 if (!nodelete_p)
3172 for (child = *forward; !NILP (child); child = c->next)
7ab12479 3173 {
233a4a2c
GM
3174 int child_size;
3175 c = XWINDOW (child);
949cf20f 3176 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
5fe0b054 3177 size_window (child, child_size, width_p, 2, first_only, last_only);
7ab12479
JB
3178 }
3179 }
3180}
3181
233a4a2c
GM
3182/* Set WINDOW's height to HEIGHT, and recursively change the height of
3183 WINDOW's children. NODELETE non-zero means don't delete windows
3184 that become too small in the process. (The caller should check
3185 later and do so if appropriate.) */
7ab12479 3186
5e14b1fc 3187void
233a4a2c 3188set_window_height (window, height, nodelete)
7ab12479 3189 Lisp_Object window;
233a4a2c 3190 int height;
7ab12479
JB
3191 int nodelete;
3192{
5fe0b054 3193 size_window (window, height, 0, nodelete, 0, 0);
233a4a2c 3194}
7ab12479 3195
7ab12479 3196
233a4a2c
GM
3197/* Set WINDOW's width to WIDTH, and recursively change the width of
3198 WINDOW's children. NODELETE non-zero means don't delete windows
3199 that become too small in the process. (The caller should check
3200 later and do so if appropriate.) */
7ab12479 3201
233a4a2c
GM
3202void
3203set_window_width (window, width, nodelete)
3204 Lisp_Object window;
3205 int width;
3206 int nodelete;
3207{
5fe0b054 3208 size_window (window, width, 1, nodelete, 0, 0);
7ab12479 3209}
233a4a2c 3210
cdbc7fec
KS
3211/* Change window heights in windows rooted in WINDOW by N lines. */
3212
3213void
3214change_window_heights (window, n)
3215 Lisp_Object window;
3216 int n;
3217{
3218 struct window *w = XWINDOW (window);
3219
949cf20f
KS
3220 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3221 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
cdbc7fec 3222
949cf20f
KS
3223 if (INTEGERP (w->orig_top_line))
3224 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3225 if (INTEGERP (w->orig_total_lines))
3226 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
cdbc7fec
KS
3227
3228 /* Handle just the top child in a vertical split. */
3229 if (!NILP (w->vchild))
3230 change_window_heights (w->vchild, n);
3231
3232 /* Adjust all children in a horizontal split. */
3233 for (window = w->hchild; !NILP (window); window = w->next)
3234 {
3235 w = XWINDOW (window);
3236 change_window_heights (window, n);
3237 }
3238}
3239
7ab12479 3240\f
1d8d96fa 3241int window_select_count;
7ab12479 3242
5b03d3c0
RS
3243Lisp_Object
3244Fset_window_buffer_unwind (obuf)
3245 Lisp_Object obuf;
3246{
3247 Fset_buffer (obuf);
3248 return Qnil;
3249}
3250
56613f06
SM
3251EXFUN (Fset_window_fringes, 4);
3252EXFUN (Fset_window_scroll_bars, 4);
7ab12479 3253
5500c422
GM
3254/* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3255 means it's allowed to run hooks. See make_frame for a case where
949cf20f
KS
3256 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3257 margins, fringes, and scroll-bar settings of the window are not
3258 reset from the buffer's local settings. */
7ab12479 3259
5500c422 3260void
949cf20f 3261set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
5500c422 3262 Lisp_Object window, buffer;
949cf20f 3263 int run_hooks_p, keep_margins_p;
5500c422
GM
3264{
3265 struct window *w = XWINDOW (window);
3266 struct buffer *b = XBUFFER (buffer);
aed13378 3267 int count = SPECPDL_INDEX ();
7ab12479
JB
3268
3269 w->buffer = buffer;
86e48436
RS
3270
3271 if (EQ (window, selected_window))
5500c422 3272 b->last_selected_window = window;
beb4e312 3273
c49a0495
KS
3274 /* Let redisplay errors through. */
3275 b->display_error_modiff = 0;
3276
beb4e312 3277 /* Update time stamps of buffer display. */
5500c422
GM
3278 if (INTEGERP (b->display_count))
3279 XSETINT (b->display_count, XINT (b->display_count) + 1);
3280 b->display_time = Fcurrent_time ();
86e48436 3281
d834a2e9 3282 XSETFASTINT (w->window_end_pos, 0);
5500c422
GM
3283 XSETFASTINT (w->window_end_vpos, 0);
3284 bzero (&w->last_cursor, sizeof w->last_cursor);
5a41ab94 3285 w->window_end_valid = Qnil;
1c686c99 3286 w->hscroll = w->min_hscroll = make_number (0);
224227d1 3287 w->vscroll = 0;
5500c422 3288 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
7ab12479 3289 set_marker_restricted (w->start,
5500c422 3290 make_number (b->last_window_start),
7ab12479
JB
3291 buffer);
3292 w->start_at_line_beg = Qnil;
e36ab06b 3293 w->force_start = Qnil;
d834a2e9 3294 XSETFASTINT (w->last_modified, 0);
3cd21523 3295 XSETFASTINT (w->last_overlay_modified, 0);
7ab12479 3296 windows_or_buffers_changed++;
5b03d3c0
RS
3297
3298 /* We must select BUFFER for running the window-scroll-functions.
3299 If WINDOW is selected, switch permanently.
3300 Otherwise, switch but go back to the ambient buffer afterward. */
7ab12479
JB
3301 if (EQ (window, selected_window))
3302 Fset_buffer (buffer);
5b03d3c0
RS
3303 /* We can't check ! NILP (Vwindow_scroll_functions) here
3304 because that might itself be a local variable. */
3305 else if (window_initialized)
3306 {
3307 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
3308 Fset_buffer (buffer);
3309 }
3310
949cf20f
KS
3311 if (!keep_margins_p)
3312 {
3313 /* Set left and right marginal area width etc. from buffer. */
3314
79fd290e 3315 /* This may call adjust_window_margins three times, so
949cf20f 3316 temporarily disable window margins. */
3834380d
KS
3317 Lisp_Object save_left = w->left_margin_cols;
3318 Lisp_Object save_right = w->right_margin_cols;
3319
949cf20f
KS
3320 w->left_margin_cols = w->right_margin_cols = Qnil;
3321
3322 Fset_window_fringes (window,
3323 b->left_fringe_width, b->right_fringe_width,
3324 b->fringes_outside_margins);
3325
3326 Fset_window_scroll_bars (window,
3327 b->scroll_bar_width,
3328 b->vertical_scroll_bar_type, Qnil);
3329
3834380d
KS
3330 w->left_margin_cols = save_left;
3331 w->right_margin_cols = save_right;
3332
949cf20f
KS
3333 Fset_window_margins (window,
3334 b->left_margin_cols, b->right_margin_cols);
3335 }
7ab12479 3336
5500c422
GM
3337 if (run_hooks_p)
3338 {
3339 if (! NILP (Vwindow_scroll_functions))
3340 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3341 Fmarker_position (w->start));
3342
3343 if (! NILP (Vwindow_configuration_change_hook)
3344 && ! NILP (Vrun_hooks))
3345 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3346 }
543f5fb1 3347
5b03d3c0 3348 unbind_to (count, Qnil);
5500c422 3349}
5b03d3c0 3350
5500c422 3351
949cf20f 3352DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
fdb82f93 3353 doc: /* Make WINDOW display BUFFER as its contents.
c2755926 3354BUFFER can be a buffer or the name of an existing buffer.
ab80314b 3355Optional third arg KEEP-MARGINS non-nil means that WINDOW's current
949cf20f
KS
3356display margins, fringe widths, and scroll bar settings are maintained;
3357the default is to reset these from BUFFER's local settings or the frame
6cb4a892
RS
3358defaults.
3359
3360This function runs the hook `window-scroll-functions'. */)
949cf20f 3361 (window, buffer, keep_margins)
56613f06 3362 register Lisp_Object window, buffer, keep_margins;
5500c422
GM
3363{
3364 register Lisp_Object tem;
3365 register struct window *w = decode_window (window);
5500c422 3366
bed0c171 3367 XSETWINDOW (window, w);
5500c422 3368 buffer = Fget_buffer (buffer);
b7826503 3369 CHECK_BUFFER (buffer);
5500c422
GM
3370
3371 if (NILP (XBUFFER (buffer)->name))
3372 error ("Attempt to display deleted buffer");
3373
3374 tem = w->buffer;
3375 if (NILP (tem))
3376 error ("Window is deleted");
3377 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3378 is first being set up. */
3379 {
3380 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3381 error ("Window is dedicated to `%s'",
d5db4077 3382 SDATA (XBUFFER (tem)->name));
5500c422
GM
3383
3384 unshow_buffer (w);
3385 }
3386
949cf20f 3387 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
7ab12479
JB
3388 return Qnil;
3389}
3390
2d0834cc
SM
3391/* Note that selected_window can be nil
3392 when this is called from Fset_window_configuration. */
3393
14d87dc9 3394DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
fdb82f93 3395 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
e963215c 3396If WINDOW is not already selected, make WINDOW's buffer current
c2755926 3397and make WINDOW the frame's selected window. Return WINDOW.
14d87dc9
SM
3398Optional second arg NORECORD non-nil means
3399do not put this buffer at the front of the list of recently selected ones.
282f7831 3400
fdb82f93
PJ
3401Note that the main editor command loop
3402selects the buffer of the selected window before each command. */)
14d87dc9
SM
3403 (window, norecord)
3404 register Lisp_Object window, norecord;
7ab12479
JB
3405{
3406 register struct window *w;
719eaeb1 3407 register struct window *ow;
1ae1a37d 3408 struct frame *sf;
7ab12479 3409
b7826503 3410 CHECK_LIVE_WINDOW (window);
7ab12479
JB
3411
3412 w = XWINDOW (window);
50e88778 3413 w->frozen_window_start_p = 0;
7ab12479 3414
2a1893f4
SM
3415 ++window_select_count;
3416 XSETFASTINT (w->use_time, window_select_count);
7ab12479
JB
3417 if (EQ (window, selected_window))
3418 return window;
3419
a46c0153
RS
3420 /* Store the current buffer's actual point into the
3421 old selected window. It belongs to that window,
3422 and when the window is not selected, must be in the window. */
719eaeb1
GM
3423 if (!NILP (selected_window))
3424 {
3425 ow = XWINDOW (selected_window);
3426 if (! NILP (ow->buffer))
3427 set_marker_both (ow->pointm, ow->buffer,
3428 BUF_PT (XBUFFER (ow->buffer)),
3429 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3430 }
7ab12479
JB
3431
3432 selected_window = window;
1ae1a37d 3433 sf = SELECTED_FRAME ();
d1755117
RS
3434 if (XFRAME (WINDOW_FRAME (w)) != sf)
3435 {
3436 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3437 /* Use this rather than Fhandle_switch_frame
3438 so that FRAME_FOCUS_FRAME is moved appropriately as we
3439 move around in the state where a minibuffer in a separate
3440 frame is active. */
c869384d 3441 Fselect_frame (WINDOW_FRAME (w));
d1755117
RS
3442 }
3443 else
1ae1a37d 3444 sf->selected_window = window;
7ab12479 3445
14d87dc9 3446 if (NILP (norecord))
b7354ddf 3447 record_buffer (w->buffer);
7ab12479
JB
3448 Fset_buffer (w->buffer);
3449
86e48436
RS
3450 XBUFFER (w->buffer)->last_selected_window = window;
3451
7ab12479
JB
3452 /* Go to the point recorded in the window.
3453 This is important when the buffer is in more
3454 than one window. It also matters when
3455 redisplay_window has altered point after scrolling,
3456 because it makes the change only in the window. */
3457 {
3458 register int new_point = marker_position (w->pointm);
3459 if (new_point < BEGV)
3460 SET_PT (BEGV);
a9c95e08 3461 else if (new_point > ZV)
7ab12479
JB
3462 SET_PT (ZV);
3463 else
3464 SET_PT (new_point);
3465 }
3466
3467 windows_or_buffers_changed++;
3468 return window;
3469}
14d87dc9
SM
3470
3471static Lisp_Object
3472select_window_norecord (window)
3473 Lisp_Object window;
3474{
3475 return Fselect_window (window, Qt);
3476}
b7354ddf 3477\f
441a127e
RS
3478/* Deiconify the frame containing the window WINDOW,
3479 unless it is the selected frame;
3480 then return WINDOW.
3481
3482 The reason for the exception for the selected frame
3483 is that it seems better not to change the selected frames visibility
3484 merely because of displaying a different buffer in it.
3485 The deiconification is useful when a buffer gets shown in
3486 another frame that you were not using lately. */
d07f802a
RS
3487
3488static Lisp_Object
3489display_buffer_1 (window)
3490 Lisp_Object window;
3491{
1ae1a37d
GM
3492 Lisp_Object frame = XWINDOW (window)->frame;
3493 FRAME_PTR f = XFRAME (frame);
177c0ea7 3494
d07f802a 3495 FRAME_SAMPLE_VISIBILITY (f);
177c0ea7 3496
2d0834cc
SM
3497 if (EQ (frame, selected_frame))
3498 ; /* Assume the selected frame is already visible enough. */
3499 else if (minibuf_level > 0
3500 && MINI_WINDOW_P (XWINDOW (selected_window))
3501 && WINDOW_LIVE_P (minibuf_selected_window)
3502 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3503 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3504 else
60117126
RS
3505 {
3506 if (FRAME_ICONIFIED_P (f))
1ae1a37d 3507 Fmake_frame_visible (frame);
37962e60 3508 else if (FRAME_VISIBLE_P (f))
1ae1a37d 3509 Fraise_frame (frame);
60117126 3510 }
177c0ea7 3511
d07f802a
RS
3512 return window;
3513}
3514
4628f7a4 3515DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
d99b4859 3516 doc: /* Returns non-nil if a buffer named BUFFER-NAME gets a special frame.
c019856e
RS
3517If the value is t, `display-buffer' or `pop-to-buffer' would create a
3518special frame for that buffer using the default frame parameters.
3519
3520If the value is a list, it is a list of frame parameters that would be used
d99b4859
RS
3521to make a frame for that buffer.
3522The variables `special-display-buffer-names'
3523and `special-display-regexps' control this. */)
fdb82f93 3524 (buffer_name)
4628f7a4
EN
3525 Lisp_Object buffer_name;
3526{
3527 Lisp_Object tem;
3528
b7826503 3529 CHECK_STRING (buffer_name);
4628f7a4
EN
3530
3531 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3532 if (!NILP (tem))
3533 return Qt;
3534
3535 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3536 if (!NILP (tem))
3537 return XCDR (tem);
3538
3539 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3540 {
3541 Lisp_Object car = XCAR (tem);
3542 if (STRINGP (car)
3543 && fast_string_match (car, buffer_name) >= 0)
3544 return Qt;
3545 else if (CONSP (car)
3546 && STRINGP (XCAR (car))
3547 && fast_string_match (XCAR (car), buffer_name) >= 0)
0057b00a 3548 return XCDR (car);
4628f7a4
EN
3549 }
3550 return Qnil;
177c0ea7 3551}
4628f7a4
EN
3552
3553DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
c019856e
RS
3554 doc: /* Returns non-nil if a buffer named BUFFER-NAME would use the same window.
3555More precisely, if `display-buffer' or `pop-to-buffer' would display
3556that buffer in the selected window rather than (as usual) in some other window.
fdb82f93
PJ
3557See `same-window-buffer-names' and `same-window-regexps'. */)
3558 (buffer_name)
4628f7a4
EN
3559 Lisp_Object buffer_name;
3560{
3561 Lisp_Object tem;
3562
b7826503 3563 CHECK_STRING (buffer_name);
4628f7a4
EN
3564
3565 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3566 if (!NILP (tem))
3567 return Qt;
3568
3569 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3570 if (!NILP (tem))
3571 return Qt;
3572
3573 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3574 {
3575 Lisp_Object car = XCAR (tem);
3576 if (STRINGP (car)
3577 && fast_string_match (car, buffer_name) >= 0)
3578 return Qt;
3579 else if (CONSP (car)
3580 && STRINGP (XCAR (car))
3581 && fast_string_match (XCAR (car), buffer_name) >= 0)
3582 return Qt;
3583 }
3584 return Qnil;
3585}
3586
fdb82f93 3587/* Use B so the default is (other-buffer). */
53f76081 3588DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
fdb82f93
PJ
3589 "BDisplay buffer: \nP",
3590 doc: /* Make BUFFER appear in some window but don't select it.
c2755926
LT
3591BUFFER must be the name of an existing buffer, or, when called from Lisp,
3592a buffer.
fdb82f93
PJ
3593If BUFFER is shown already in some window, just use that one,
3594unless the window is the selected window and the optional second
3595argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3596If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3597Returns the window displaying BUFFER.
079d288f 3598If `display-buffer-reuse-frames' is non-nil, and another frame is currently
fdb82f93
PJ
3599displaying BUFFER, then simply raise that frame.
3600
9a7e95f3
RS
3601The variables `special-display-buffer-names',
3602`special-display-regexps', `same-window-buffer-names', and
3603`same-window-regexps' customize how certain buffer names are handled.
9297079e 3604The latter two take effect only if NOT-THIS-WINDOW is nil.
fdb82f93
PJ
3605
3606If optional argument FRAME is `visible', search all visible frames.
3607If FRAME is 0, search all visible and iconified frames.
3608If FRAME is t, search all frames.
3609If FRAME is a frame, search only that frame.
3610If FRAME is nil, search only the selected frame
3611 (actually the last nonminibuffer frame),
3612 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3613 which means search visible and iconified frames.
3614
876e2665
GM
3615If a full-width window on a splittable frame is available to display
3616the buffer, it may be split, subject to the value of the variable
3617`split-height-threshold'.
3618
fdb82f93
PJ
3619If `even-window-heights' is non-nil, window heights will be evened out
3620if displaying the buffer causes two vertically adjacent windows to be
3621displayed. */)
3622 (buffer, not_this_window, frame)
53f76081 3623 register Lisp_Object buffer, not_this_window, frame;
7ab12479 3624{
aee631c2 3625 register Lisp_Object window, tem, swp;
1ae1a37d 3626 struct frame *f;
7ab12479 3627
aee631c2 3628 swp = Qnil;
7ab12479 3629 buffer = Fget_buffer (buffer);
b7826503 3630 CHECK_BUFFER (buffer);
7ab12479 3631
265a9e55 3632 if (!NILP (Vdisplay_buffer_function))
7ab12479
JB
3633 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3634
265a9e55 3635 if (NILP (not_this_window)
7ab12479 3636 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
d07f802a 3637 return display_buffer_1 (selected_window);
7ab12479 3638
855d8627
RS
3639 /* See if the user has specified this buffer should appear
3640 in the selected window. */
3641 if (NILP (not_this_window))
3642 {
aee631c2
RS
3643 swp = Fsame_window_p (XBUFFER (buffer)->name);
3644 if (!NILP (swp) && !no_switch_window (selected_window))
c63dc4a2
RS
3645 {
3646 Fswitch_to_buffer (buffer, Qnil);
d07f802a 3647 return display_buffer_1 (selected_window);
c63dc4a2 3648 }
855d8627
RS
3649 }
3650
079d288f 3651 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
73dc5198
KH
3652 look for a window showing BUFFER on any visible or iconified frame.
3653 Otherwise search only the current frame. */
53f76081
RS
3654 if (! NILP (frame))
3655 tem = frame;
9c3da604
GM
3656 else if (pop_up_frames
3657 || display_buffer_reuse_frames
3658 || last_nonminibuf_frame == 0)
73dc5198
KH
3659 XSETFASTINT (tem, 0);
3660 else
73dc5198 3661 XSETFRAME (tem, last_nonminibuf_frame);
177c0ea7 3662
73dc5198 3663 window = Fget_buffer_window (buffer, tem);
265a9e55
JB
3664 if (!NILP (window)
3665 && (NILP (not_this_window) || !EQ (window, selected_window)))
9c3da604 3666 return display_buffer_1 (window);
7ab12479 3667
a90712c2 3668 /* Certain buffer names get special handling. */
aee631c2 3669 if (!NILP (Vspecial_display_function) && NILP (swp))
a90712c2 3670 {
4628f7a4
EN
3671 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3672 if (EQ (tem, Qt))
a90712c2 3673 return call1 (Vspecial_display_function, buffer);
4628f7a4
EN
3674 if (CONSP (tem))
3675 return call2 (Vspecial_display_function, buffer, tem);
a90712c2
RS
3676 }
3677
44fa5b1e
JB
3678 /* If there are no frames open that have more than a minibuffer,
3679 we need to create a new frame. */
3680 if (pop_up_frames || last_nonminibuf_frame == 0)
7ab12479 3681 {
a90712c2 3682 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
949cf20f 3683 Fset_window_buffer (window, buffer, Qnil);
d07f802a 3684 return display_buffer_1 (window);
7ab12479 3685 }
7ab12479 3686
1ae1a37d 3687 f = SELECTED_FRAME ();
43bad991 3688 if (pop_up_windows
1ae1a37d 3689 || FRAME_MINIBUF_ONLY_P (f)
cee67da9
RS
3690 /* If the current frame is a special display frame,
3691 don't try to reuse its windows. */
1ae1a37d 3692 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
7ab12479 3693 {
12cae7c0
KH
3694 Lisp_Object frames;
3695
37962e60 3696 frames = Qnil;
1ae1a37d 3697 if (FRAME_MINIBUF_ONLY_P (f))
74112613 3698 XSETFRAME (frames, last_nonminibuf_frame);
a5731348 3699 /* Don't try to create a window if we would get an error. */
7ab12479
JB
3700 if (split_height_threshold < window_min_height << 1)
3701 split_height_threshold = window_min_height << 1;
3702
cee67da9
RS
3703 /* Note that both Fget_largest_window and Fget_lru_window
3704 ignore minibuffers and dedicated windows.
3705 This means they can return nil. */
7ab12479 3706
cee67da9
RS
3707 /* If the frame we would try to split cannot be split,
3708 try other frames. */
1ae1a37d 3709 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
cee67da9
RS
3710 {
3711 /* Try visible frames first. */
3cbb13c8 3712 window = Fget_largest_window (Qvisible, Qt);
cee67da9
RS
3713 /* If that didn't work, try iconified frames. */
3714 if (NILP (window))
3cbb13c8
SM
3715 window = Fget_largest_window (make_number (0), Qt);
3716#if 0 /* Don't try windows on other displays. */
cee67da9 3717 if (NILP (window))
3cbb13c8
SM
3718 window = Fget_largest_window (Qt, Qt);
3719#endif
cee67da9
RS
3720 }
3721 else
3cbb13c8 3722 window = Fget_largest_window (frames, Qt);
cee67da9 3723
92cca945
RS
3724 /* If we got a tall enough full-width window that can be split,
3725 split it. */
265a9e55 3726 if (!NILP (window)
92cca945 3727 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
7ab12479 3728 && window_height (window) >= split_height_threshold
111e5992 3729 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
7ab12479
JB
3730 window = Fsplit_window (window, Qnil, Qnil);
3731 else
3732 {
1942f68f
RS
3733 Lisp_Object upper, lower, other;
3734
3cbb13c8 3735 window = Fget_lru_window (frames, Qt);
92cca945
RS
3736 /* If the LRU window is selected, and big enough,
3737 and can be split, split it. */
cee67da9 3738 if (!NILP (window)
92cca945 3739 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
cee67da9
RS
3740 && (EQ (window, selected_window)
3741 || EQ (XWINDOW (window)->parent, Qnil))
7ab12479
JB
3742 && window_height (window) >= window_min_height << 1)
3743 window = Fsplit_window (window, Qnil, Qnil);
49e99345
SM
3744 else
3745 window = Fget_lru_window (frames, Qnil);
cee67da9 3746 /* If Fget_lru_window returned nil, try other approaches. */
48d9379d 3747
cee67da9 3748 /* Try visible frames first. */
48d9379d
RS
3749 if (NILP (window))
3750 window = Fget_buffer_window (buffer, Qvisible);
cee67da9 3751 if (NILP (window))
3cbb13c8 3752 window = Fget_largest_window (Qvisible, Qnil);
cee67da9 3753 /* If that didn't work, try iconified frames. */
48d9379d
RS
3754 if (NILP (window))
3755 window = Fget_buffer_window (buffer, make_number (0));
cee67da9 3756 if (NILP (window))
3cbb13c8
SM
3757 window = Fget_largest_window (make_number (0), Qnil);
3758
3759#if 0 /* Don't try frames on other displays. */
48d9379d
RS
3760 if (NILP (window))
3761 window = Fget_buffer_window (buffer, Qt);
cee67da9 3762 if (NILP (window))
3cbb13c8
SM
3763 window = Fget_largest_window (Qt, Qnil);
3764#endif
cee67da9
RS
3765 /* As a last resort, make a new frame. */
3766 if (NILP (window))
3767 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
1942f68f
RS
3768 /* If window appears above or below another,
3769 even out their heights. */
cac66e4f 3770 other = upper = lower = Qnil;
1942f68f
RS
3771 if (!NILP (XWINDOW (window)->prev))
3772 other = upper = XWINDOW (window)->prev, lower = window;
3773 if (!NILP (XWINDOW (window)->next))
3774 other = lower = XWINDOW (window)->next, upper = window;
3775 if (!NILP (other)
6529ed87 3776 && !NILP (Veven_window_heights)
1942f68f 3777 /* Check that OTHER and WINDOW are vertically arrayed. */
949cf20f
KS
3778 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3779 && (XFASTINT (XWINDOW (other)->total_lines)
3780 > XFASTINT (XWINDOW (window)->total_lines)))
1942f68f 3781 {
949cf20f
KS
3782 int total = (XFASTINT (XWINDOW (other)->total_lines)
3783 + XFASTINT (XWINDOW (window)->total_lines));
86c8e823 3784 enlarge_window (upper,
949cf20f 3785 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
56ebfae2 3786 0);
1942f68f 3787 }
7ab12479
JB
3788 }
3789 }
3790 else
3cbb13c8 3791 window = Fget_lru_window (Qnil, Qnil);
7ab12479 3792
949cf20f 3793 Fset_window_buffer (window, buffer, Qnil);
d07f802a 3794 return display_buffer_1 (window);
7ab12479
JB
3795}
3796
e661376d
KS
3797
3798DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3799 0, 1, 0,
5a1048a5 3800 doc: /* Force all windows to be updated on next redisplay.
e661376d 3801If optional arg OBJECT is a window, force redisplay of that window only.
0cc1039f 3802If OBJECT is a buffer or buffer name, force redisplay of all windows
e661376d
KS
3803displaying that buffer. */)
3804 (object)
3805 Lisp_Object object;
3806{
3807 if (NILP (object))
3808 {
3809 windows_or_buffers_changed++;
40c6ee74 3810 update_mode_lines++;
e661376d
KS
3811 return Qt;
3812 }
3813
3814 if (WINDOWP (object))
3815 {
40c6ee74 3816 struct window *w = XWINDOW (object);
e661376d 3817 mark_window_display_accurate (object, 0);
40c6ee74
KS
3818 w->update_mode_line = Qt;
3819 if (BUFFERP (w->buffer))
3820 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3821 ++update_mode_lines;
e661376d
KS
3822 return Qt;
3823 }
0cc1039f 3824
e661376d
KS
3825 if (STRINGP (object))
3826 object = Fget_buffer (object);
3827 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3828 {
3829 /* Walk all windows looking for buffer, and force update
3830 of each of those windows. */
3831
3832 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3833 return NILP (object) ? Qnil : Qt;
3834 }
3835
3836 /* If nothing suitable was found, just return.
3837 We could signal an error, but this feature will typically be used
3838 asynchronously in timers or process sentinels, so we don't. */
3839 return Qnil;
3840}
3841
3842
7ab12479
JB
3843void
3844temp_output_buffer_show (buf)
3845 register Lisp_Object buf;
3846{
3847 register struct buffer *old = current_buffer;
3848 register Lisp_Object window;
3849 register struct window *w;
3850
bccd3dd1
RS
3851 XBUFFER (buf)->directory = current_buffer->directory;
3852
7ab12479 3853 Fset_buffer (buf);
c6367666 3854 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
7ab12479
JB
3855 BEGV = BEG;
3856 ZV = Z;
3857 SET_PT (BEG);
06be4f85 3858#if 0 /* rms: there should be no reason for this. */
b1599b4c 3859 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
06be4f85 3860#endif
7ab12479
JB
3861 set_buffer_internal (old);
3862
e67a1dea 3863 if (!NILP (Vtemp_buffer_show_function))
7ab12479
JB
3864 call1 (Vtemp_buffer_show_function, buf);
3865 else
3866 {
53f76081 3867 window = Fdisplay_buffer (buf, Qnil, Qnil);
7ab12479 3868
1ae1a37d 3869 if (!EQ (XWINDOW (window)->frame, selected_frame))
44fa5b1e 3870 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
7ab12479
JB
3871 Vminibuf_scroll_window = window;
3872 w = XWINDOW (window);
d834a2e9 3873 XSETFASTINT (w->hscroll, 0);
ea68264b 3874 XSETFASTINT (w->min_hscroll, 0);
2d0834cc
SM
3875 set_marker_restricted_both (w->start, buf, BEG, BEG);
3876 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
a58ec57d 3877
beb4e312 3878 /* Run temp-buffer-show-hook, with the chosen window selected
a5731348 3879 and its buffer current. */
2d0834cc
SM
3880
3881 if (!NILP (Vrun_hooks)
3882 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3883 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
2cccc823 3884 {
2d0834cc
SM
3885 int count = SPECPDL_INDEX ();
3886 Lisp_Object prev_window, prev_buffer;
3887 prev_window = selected_window;
3888 XSETBUFFER (prev_buffer, old);
0cc1039f 3889
2d0834cc
SM
3890 /* Select the window that was chosen, for running the hook.
3891 Note: Both Fselect_window and select_window_norecord may
3892 set-buffer to the buffer displayed in the window,
3893 so we need to save the current buffer. --stef */
3894 record_unwind_protect (Fset_buffer, prev_buffer);
3895 record_unwind_protect (select_window_norecord, prev_window);
3896 Fselect_window (window, Qt);
3897 Fset_buffer (w->buffer);
3898 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3899 unbind_to (count, Qnil);
2cccc823
RS
3900 }
3901 }
7ab12479
JB
3902}
3903\f
dfcf069d 3904static void
7ab12479
JB
3905make_dummy_parent (window)
3906 Lisp_Object window;
3907{
cffec418 3908 Lisp_Object new;
7ab12479 3909 register struct window *o, *p;
cffec418 3910 int i;
7ab12479 3911
cffec418 3912 o = XWINDOW (window);
26605be9 3913 p = allocate_window ();
cffec418 3914 for (i = 0; i < VECSIZE (struct window); ++i)
26605be9
GM
3915 ((struct Lisp_Vector *) p)->contents[i]
3916 = ((struct Lisp_Vector *)o)->contents[i];
cffec418 3917 XSETWINDOW (new, p);
7ab12479 3918
2a1893f4
SM
3919 ++sequence_number;
3920 XSETFASTINT (p->sequence_number, sequence_number);
7ab12479
JB
3921
3922 /* Put new into window structure in place of window */
3923 replace_window (window, new);
3924
3925 o->next = Qnil;
3926 o->prev = Qnil;
3927 o->vchild = Qnil;
3928 o->hchild = Qnil;
3929 o->parent = new;
3930
3931 p->start = Qnil;
3932 p->pointm = Qnil;
3933 p->buffer = Qnil;
3934}
3935
3936DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
fdb82f93
PJ
3937 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3938WINDOW defaults to selected one and SIZE to half its size.
3939If optional third arg HORFLAG is non-nil, split side by side
3940and put SIZE columns in the first of the pair. In that case,
876e2665 3941SIZE includes that window's scroll bar, or the divider column to its right.
c2755926
LT
3942Interactively, all arguments are nil.
3943
3944Returns the newly created window (which is the lower or rightmost one).
19ca94ce
RS
3945The upper or leftmost window is the original one, and remains selected
3946if it was selected before.
3947
c2755926 3948See Info node `(elisp)Splitting Windows' for more details and examples.*/)
fdb82f93 3949 (window, size, horflag)
77ae0fe3 3950 Lisp_Object window, size, horflag;
7ab12479
JB
3951{
3952 register Lisp_Object new;
3953 register struct window *o, *p;
c0807608 3954 FRAME_PTR fo;
77ae0fe3 3955 register int size_int;
7ab12479 3956
265a9e55 3957 if (NILP (window))
7ab12479
JB
3958 window = selected_window;
3959 else
b7826503 3960 CHECK_LIVE_WINDOW (window);
7ab12479
JB
3961
3962 o = XWINDOW (window);
c0807608 3963 fo = XFRAME (WINDOW_FRAME (o));
7ab12479 3964
77ae0fe3 3965 if (NILP (size))
7ab12479 3966 {
265a9e55 3967 if (!NILP (horflag))
c0807608 3968 /* Calculate the size of the left-hand window, by dividing
25b33244
KH
3969 the usable space in columns by two.
3970 We round up, since the left-hand window may include
3971 a dividing line, while the right-hand may not. */
949cf20f 3972 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
7ab12479 3973 else
949cf20f 3974 size_int = XFASTINT (o->total_lines) >> 1;
7ab12479
JB
3975 }
3976 else
3977 {
b7826503 3978 CHECK_NUMBER (size);
77ae0fe3 3979 size_int = XINT (size);
7ab12479
JB
3980 }
3981
3982 if (MINI_WINDOW_P (o))
3983 error ("Attempt to split minibuffer window");
233a4a2c
GM
3984 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3985 error ("Attempt to split fixed-size window");
7ab12479 3986
a481b3ea 3987 check_min_window_sizes ();
7ab12479 3988
265a9e55 3989 if (NILP (horflag))
7ab12479 3990 {
77ae0fe3
KH
3991 if (size_int < window_min_height)
3992 error ("Window height %d too small (after splitting)", size_int);
949cf20f 3993 if (size_int + window_min_height > XFASTINT (o->total_lines))
37962e60 3994 error ("Window height %d too small (after splitting)",
949cf20f 3995 XFASTINT (o->total_lines) - size_int);
265a9e55
JB
3996 if (NILP (o->parent)
3997 || NILP (XWINDOW (o->parent)->vchild))
7ab12479
JB
3998 {
3999 make_dummy_parent (window);
4000 new = o->parent;
4001 XWINDOW (new)->vchild = window;
4002 }
4003 }
4004 else
4005 {
77ae0fe3
KH
4006 if (size_int < window_min_width)
4007 error ("Window width %d too small (after splitting)", size_int);
a59fed7e 4008
949cf20f 4009 if (size_int + window_min_width > XFASTINT (o->total_cols))
37962e60 4010 error ("Window width %d too small (after splitting)",
949cf20f 4011 XFASTINT (o->total_cols) - size_int);
265a9e55
JB
4012 if (NILP (o->parent)
4013 || NILP (XWINDOW (o->parent)->hchild))
7ab12479
JB
4014 {
4015 make_dummy_parent (window);
4016 new = o->parent;
4017 XWINDOW (new)->hchild = window;
4018 }
4019 }
4020
4021 /* Now we know that window's parent is a vertical combination
4022 if we are dividing vertically, or a horizontal combination
4023 if we are making side-by-side windows */
4024
4025 windows_or_buffers_changed++;
c0807608 4026 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
7ab12479
JB
4027 new = make_window ();
4028 p = XWINDOW (new);
4029
44fa5b1e 4030 p->frame = o->frame;
7ab12479 4031 p->next = o->next;
265a9e55 4032 if (!NILP (p->next))
7ab12479
JB
4033 XWINDOW (p->next)->prev = new;
4034 p->prev = window;
4035 o->next = new;
4036 p->parent = o->parent;
4037 p->buffer = Qt;
5500c422
GM
4038 p->window_end_valid = Qnil;
4039 bzero (&p->last_cursor, sizeof p->last_cursor);
7ab12479 4040
949cf20f
KS
4041 /* Duplicate special geometry settings. */
4042
4043 p->left_margin_cols = o->left_margin_cols;
4044 p->right_margin_cols = o->right_margin_cols;
4045 p->left_fringe_width = o->left_fringe_width;
4046 p->right_fringe_width = o->right_fringe_width;
4047 p->fringes_outside_margins = o->fringes_outside_margins;
4048 p->scroll_bar_width = o->scroll_bar_width;
4049 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
4050
44fa5b1e 4051 /* Apportion the available frame space among the two new windows */
7ab12479 4052
265a9e55 4053 if (!NILP (horflag))
7ab12479 4054 {
949cf20f
KS
4055 p->total_lines = o->total_lines;
4056 p->top_line = o->top_line;
4057 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
4058 XSETFASTINT (o->total_cols, size_int);
4059 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
4060 adjust_window_margins (p);
4061 adjust_window_margins (o);
7ab12479
JB
4062 }
4063 else
4064 {
949cf20f
KS
4065 p->left_col = o->left_col;
4066 p->total_cols = o->total_cols;
4067 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
4068 XSETFASTINT (o->total_lines, size_int);
4069 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
7ab12479
JB
4070 }
4071
5500c422
GM
4072 /* Adjust glyph matrices. */
4073 adjust_glyphs (fo);
949cf20f
KS
4074
4075 Fset_window_buffer (new, o->buffer, Qt);
7ab12479
JB
4076 return new;
4077}
4078\f
56ebfae2 4079DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
fdb82f93
PJ
4080 doc: /* Make current window ARG lines bigger.
4081From program, optional second arg non-nil means grow sideways ARG columns.
f95284d2 4082Interactively, if an argument is not given, make the window one line bigger.
5afc696a 4083If HORIZONTAL is non-nil, enlarge horizontally instead of vertically.
56ebfae2
RS
4084This function can delete windows, even the second window, if they get
4085too small. */)
4086 (arg, horizontal)
4087 Lisp_Object arg, horizontal;
7ab12479 4088{
b7826503 4089 CHECK_NUMBER (arg);
56ebfae2 4090 enlarge_window (selected_window, XINT (arg), !NILP (horizontal));
543f5fb1
RS
4091
4092 if (! NILP (Vwindow_configuration_change_hook))
4093 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4094
7ab12479
JB
4095 return Qnil;
4096}
4097
56ebfae2 4098DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
fdb82f93
PJ
4099 doc: /* Make current window ARG lines smaller.
4100From program, optional second arg non-nil means shrink sideways arg columns.
56ebfae2 4101Interactively, if an argument is not given, make the window one line smaller. Only
a5731348 4102siblings to the right or below are changed. */)
56ebfae2
RS
4103 (arg, side)
4104 Lisp_Object arg, side;
7ab12479 4105{
b7826503 4106 CHECK_NUMBER (arg);
56ebfae2 4107 enlarge_window (selected_window, -XINT (arg), !NILP (side));
543f5fb1
RS
4108
4109 if (! NILP (Vwindow_configuration_change_hook))
4110 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4111
7ab12479
JB
4112 return Qnil;
4113}
4114
4115int
4116window_height (window)
4117 Lisp_Object window;
4118{
4119 register struct window *p = XWINDOW (window);
949cf20f 4120 return WINDOW_TOTAL_LINES (p);
7ab12479
JB
4121}
4122
4123int
4124window_width (window)
4125 Lisp_Object window;
4126{
4127 register struct window *p = XWINDOW (window);
949cf20f 4128 return WINDOW_TOTAL_COLS (p);
7ab12479
JB
4129}
4130
177c0ea7 4131
7ab12479 4132#define CURBEG(w) \
5afc696a 4133 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
7ab12479
JB
4134
4135#define CURSIZE(w) \
5afc696a
RS
4136 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
4137
7ab12479 4138
5afc696a
RS
4139/* Enlarge WINDOW by DELTA.
4140 HORIZ_FLAG nonzero means enlarge it horizontally;
4141 zero means do it vertically.
233a4a2c 4142
5afc696a
RS
4143 Siblings of the selected window are resized to fulfill the size
4144 request. If they become too small in the process, they will be
56ebfae2 4145 deleted. */
7ab12479 4146
f984d4fc 4147static void
56ebfae2 4148enlarge_window (window, delta, horiz_flag)
86c8e823 4149 Lisp_Object window;
56ebfae2 4150 int delta, horiz_flag;
7ab12479 4151{
86c8e823 4152 Lisp_Object parent, next, prev;
233a4a2c 4153 struct window *p;
3578db3c
KR
4154 Lisp_Object *sizep;
4155 int maximum;
5e14b1fc 4156 int (*sizefun) P_ ((Lisp_Object))
5afc696a 4157 = horiz_flag ? window_width : window_height;
233a4a2c 4158 void (*setsizefun) P_ ((Lisp_Object, int, int))
5afc696a 4159 = (horiz_flag ? set_window_width : set_window_height);
7ab12479 4160
233a4a2c
GM
4161 /* Check values of window_min_width and window_min_height for
4162 validity. */
a481b3ea 4163 check_min_window_sizes ();
7ab12479 4164
233a4a2c 4165 /* Give up if this window cannot be resized. */
5afc696a 4166 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
233a4a2c
GM
4167 error ("Window is not resizable");
4168
4169 /* Find the parent of the selected window. */
7ab12479
JB
4170 while (1)
4171 {
4172 p = XWINDOW (window);
4173 parent = p->parent;
177c0ea7 4174
265a9e55 4175 if (NILP (parent))
7ab12479 4176 {
5afc696a 4177 if (horiz_flag)
7ab12479
JB
4178 error ("No other window to side of this one");
4179 break;
4180 }
177c0ea7 4181
5afc696a 4182 if (horiz_flag
233a4a2c 4183 ? !NILP (XWINDOW (parent)->hchild)
265a9e55 4184 : !NILP (XWINDOW (parent)->vchild))
7ab12479 4185 break;
177c0ea7 4186
7ab12479
JB
4187 window = parent;
4188 }
4189
05c2896a 4190 sizep = &CURSIZE (window);
7ab12479 4191
7ab12479
JB
4192 {
4193 register int maxdelta;
7ab12479 4194
f95284d2
RS
4195 /* Compute the maximum size increment this window can have. */
4196
56ebfae2
RS
4197 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4198 /* This is a main window followed by a minibuffer. */
4199 : !NILP (p->next) ? ((*sizefun) (p->next)
4200 - window_min_size (XWINDOW (p->next),
4201 horiz_flag, 0, 0))
4202 /* This is a minibuffer following a main window. */
4203 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4204 - window_min_size (XWINDOW (p->prev),
4205 horiz_flag, 0, 0))
4206 /* This is a frame with only one window, a minibuffer-only
4207 or a minibufferless frame. */
4208 : (delta = 0));
7ab12479
JB
4209
4210 if (delta > maxdelta)
4211 /* This case traps trying to make the minibuffer
44fa5b1e
JB
4212 the full frame, or make the only window aside from the
4213 minibuffer the full frame. */
7ab12479 4214 delta = maxdelta;
6b54027b 4215 }
d5783c40 4216
5afc696a 4217 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), horiz_flag, 0, 0))
6b54027b 4218 {
543f5fb1 4219 delete_window (window);
d5783c40 4220 return;
6b54027b
RS
4221 }
4222
4223 if (delta == 0)
4224 return;
7ab12479 4225
f95284d2 4226 /* Find the total we can get from other siblings without deleting them. */
db98a733
RS
4227 maximum = 0;
4228 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
c1636aa6 4229 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
5afc696a 4230 horiz_flag, 0, 0);
56ebfae2
RS
4231 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
4232 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4233 horiz_flag, 0, 0);
db98a733 4234
f95284d2 4235 /* If we can get it all from them without deleting them, do so. */
c6b530ed 4236 if (delta <= maximum)
7ab12479 4237 {
db98a733
RS
4238 Lisp_Object first_unaffected;
4239 Lisp_Object first_affected;
233a4a2c 4240 int fixed_p;
db98a733
RS
4241
4242 next = p->next;
4243 prev = p->prev;
4244 first_affected = window;
4245 /* Look at one sibling at a time,
4246 moving away from this window in both directions alternately,
4247 and take as much as we can get without deleting that sibling. */
f95284d2 4248 while (delta != 0
56ebfae2 4249 && (!NILP (next) || !NILP (prev)))
db98a733 4250 {
db98a733
RS
4251 if (! NILP (next))
4252 {
c1636aa6 4253 int this_one = ((*sizefun) (next)
233a4a2c 4254 - window_min_size (XWINDOW (next),
5afc696a 4255 horiz_flag, 0, &fixed_p));
233a4a2c
GM
4256 if (!fixed_p)
4257 {
4258 if (this_one > delta)
4259 this_one = delta;
177c0ea7 4260
233a4a2c 4261 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3578db3c 4262 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
db98a733 4263
233a4a2c
GM
4264 delta -= this_one;
4265 }
177c0ea7 4266
db98a733
RS
4267 next = XWINDOW (next)->next;
4268 }
177c0ea7 4269
db98a733
RS
4270 if (delta == 0)
4271 break;
177c0ea7 4272
56ebfae2 4273 if (! NILP (prev))
db98a733 4274 {
c1636aa6 4275 int this_one = ((*sizefun) (prev)
233a4a2c 4276 - window_min_size (XWINDOW (prev),
5afc696a 4277 horiz_flag, 0, &fixed_p));
233a4a2c
GM
4278 if (!fixed_p)
4279 {
4280 if (this_one > delta)
4281 this_one = delta;
177c0ea7 4282
233a4a2c 4283 first_affected = prev;
177c0ea7 4284
233a4a2c 4285 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3578db3c 4286 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
233a4a2c
GM
4287
4288 delta -= this_one;
4289 }
177c0ea7 4290
db98a733
RS
4291 prev = XWINDOW (prev)->prev;
4292 }
4293 }
4294
233a4a2c
GM
4295 xassert (delta == 0);
4296
db98a733
RS
4297 /* Now recalculate the edge positions of all the windows affected,
4298 based on the new sizes. */
4299 first_unaffected = next;
4300 prev = first_affected;
4301 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4302 prev = next, next = XWINDOW (next)->next)
4303 {
3578db3c 4304 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
db98a733
RS
4305 /* This does not change size of NEXT,
4306 but it propagates the new top edge to its children */
4307 (*setsizefun) (next, (*sizefun) (next), 0);
4308 }
7ab12479
JB
4309 }
4310 else
4311 {
4312 register int delta1;
4313 register int opht = (*sizefun) (parent);
4314
3578db3c 4315 if (opht <= XINT (*sizep) + delta)
daf516d3
RS
4316 {
4317 /* If trying to grow this window to or beyond size of the parent,
4318 just delete all the sibling windows. */
dc1ab1ee 4319 Lisp_Object start, tem, next;
daf516d3 4320
dc1ab1ee
RS
4321 start = XWINDOW (parent)->vchild;
4322 if (NILP (start))
4323 start = XWINDOW (parent)->hchild;
daf516d3 4324
dc1ab1ee
RS
4325 /* Delete any siblings that come after WINDOW. */
4326 tem = XWINDOW (window)->next;
daf516d3
RS
4327 while (! NILP (tem))
4328 {
4329 next = XWINDOW (tem)->next;
dc1ab1ee
RS
4330 delete_window (tem);
4331 tem = next;
4332 }
4333
4334 /* Delete any siblings that come after WINDOW.
4335 Note that if START is not WINDOW, then WINDOW still
4336 Fhas siblings, so WINDOW has not yet replaced its parent. */
4337 tem = start;
4338 while (! EQ (tem, window))
4339 {
4340 next = XWINDOW (tem)->next;
4341 delete_window (tem);
daf516d3
RS
4342 tem = next;
4343 }
4344 }
7ab12479 4345 else
233a4a2c
GM
4346 {
4347 /* Otherwise, make delta1 just right so that if we add
4348 delta1 lines to this window and to the parent, and then
4349 shrink the parent back to its original size, the new
4350 proportional size of this window will increase by delta.
4351
4352 The function size_window will compute the new height h'
4353 of the window from delta1 as:
177c0ea7 4354
233a4a2c
GM
4355 e = delta1/n
4356 x = delta1 - delta1/n * n for the 1st resizable child
4357 h' = h + e + x
4358
4359 where n is the number of children that can be resized.
4360 We can ignore x by choosing a delta1 that is a multiple of
4361 n. We want the height of this window to come out as
177c0ea7 4362
233a4a2c
GM
4363 h' = h + delta
4364
4365 So, delta1 must be
177c0ea7 4366
233a4a2c
GM
4367 h + e = h + delta
4368 delta1/n = delta
4369 delta1 = n * delta.
4370
a5731348 4371 The number of children n equals the number of resizable
233a4a2c 4372 children of this window + 1 because we know window itself
6e0dca3d 4373 is resizable (otherwise we would have signalled an error). */
233a4a2c
GM
4374
4375 struct window *w = XWINDOW (window);
4376 Lisp_Object s;
4377 int n = 1;
4378
4379 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
5afc696a 4380 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
233a4a2c
GM
4381 ++n;
4382 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
5afc696a 4383 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
233a4a2c
GM
4384 ++n;
4385
4386 delta1 = n * delta;
7ab12479 4387
daf516d3
RS
4388 /* Add delta1 lines or columns to this window, and to the parent,
4389 keeping things consistent while not affecting siblings. */
4390 XSETINT (CURSIZE (parent), opht + delta1);
4391 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4392
4393 /* Squeeze out delta1 lines or columns from our parent,
4394 shriking this window and siblings proportionately.
4395 This brings parent back to correct size.
4396 Delta1 was calculated so this makes this window the desired size,
4397 taking it all out of the siblings. */
4398 (*setsizefun) (parent, opht, 0);
4399
4400 }
7ab12479
JB
4401 }
4402
d834a2e9 4403 XSETFASTINT (p->last_modified, 0);
3cd21523 4404 XSETFASTINT (p->last_overlay_modified, 0);
5500c422
GM
4405
4406 /* Adjust glyph matrices. */
4407 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7ab12479 4408}
c1636aa6 4409
0d384044
RS
4410
4411/* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4412 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4413 zero means adjust the height, moving the bottom edge.
4414
4415 Following siblings of the selected window are resized to fulfill
4416 the size request. If they become too small in the process, they
4417 are not deleted; instead, we signal an error. */
4418
4419static void
4420adjust_window_trailing_edge (window, delta, horiz_flag)
4421 Lisp_Object window;
4422 int delta, horiz_flag;
4423{
4424 Lisp_Object parent, child;
4425 struct window *p;
4426 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4427 int delcount = window_deletion_count;
4428
4429 /* Check values of window_min_width and window_min_height for
4430 validity. */
4431 check_min_window_sizes ();
4432
4433 if (NILP (window))
4434 window = Fselected_window ();
4435
4436 CHECK_WINDOW (window);
4437
4438 /* Give up if this window cannot be resized. */
4439 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4440 error ("Window is not resizable");
4441
4442 while (1)
4443 {
bd99e242
RS
4444 Lisp_Object first_parallel = Qnil;
4445
c32de52a 4446 if (NILP (window))
0d384044 4447 {
5fe0b054
RS
4448 /* This happens if WINDOW on the previous iteration was
4449 at top level of the window tree. */
0d384044 4450 Fset_window_configuration (old_config);
c32de52a 4451 error ("Specified window edge is fixed");
0d384044
RS
4452 }
4453
c32de52a
RS
4454 p = XWINDOW (window);
4455 parent = p->parent;
4456
bd99e242
RS
4457 /* See if this level has windows in parallel in the specified
4458 direction. If so, set FIRST_PARALLEL to the first one. */
4459 if (horiz_flag)
4460 {
4461 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4462 first_parallel = XWINDOW (parent)->vchild;
5fe0b054
RS
4463 else if (NILP (parent) && !NILP (p->next))
4464 {
4465 /* Handle the vertical chain of main window and minibuffer
4466 which has no parent. */
4467 first_parallel = window;
4468 while (! NILP (XWINDOW (first_parallel)->prev))
4469 first_parallel = XWINDOW (first_parallel)->prev;
4470 }
bd99e242
RS
4471 }
4472 else
4473 {
4474 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4475 first_parallel = XWINDOW (parent)->hchild;
4476 }
4477
c32de52a 4478 /* If this level's succession is in the desired dimension,
5fe0b054
RS
4479 and this window is the last one, and there is no higher level,
4480 its trailing edge is fixed. */
4481 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4482 && NILP (parent))
c32de52a
RS
4483 {
4484 Fset_window_configuration (old_config);
4485 error ("Specified window edge is fixed");
4486 }
4487
0d384044
RS
4488 /* Don't make this window too small. */
4489 if (XINT (CURSIZE (window)) + delta
4490 < (horiz_flag ? window_min_width : window_min_height))
4491 {
4492 Fset_window_configuration (old_config);
4493 error ("Cannot adjust window size as specified");
4494 }
4495
4496 /* Clear out some redisplay caches. */
4497 XSETFASTINT (p->last_modified, 0);
4498 XSETFASTINT (p->last_overlay_modified, 0);
4499
4500 /* Adjust this window's edge. */
4501 XSETINT (CURSIZE (window),
4502 XINT (CURSIZE (window)) + delta);
4503
4504 /* If this window has following siblings in the desired dimension,
bd99e242
RS
4505 make them smaller, and exit the loop.
4506
0d384044
RS
4507 (If we reach the top of the tree and can never do this,
4508 we will fail and report an error, above.) */
bd99e242 4509 if (NILP (first_parallel))
0d384044 4510 {
c32de52a 4511 if (!NILP (p->next))
0d384044 4512 {
e99c7521
JD
4513 /* This may happen for the minibuffer. In that case
4514 the window_deletion_count check below does not work. */
a53d44a8 4515 if (XINT (CURSIZE (p->next)) - delta <= 0)
e99c7521
JD
4516 {
4517 Fset_window_configuration (old_config);
4518 error ("Cannot adjust window size as specified");
4519 }
4520
0d384044
RS
4521 XSETINT (CURBEG (p->next),
4522 XINT (CURBEG (p->next)) + delta);
4523 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
5fe0b054 4524 horiz_flag, 0, 1, 0);
0d384044
RS
4525 break;
4526 }
4527 }
4528 else
4529 /* Here we have a chain of parallel siblings, in the other dimension.
4530 Change the size of the other siblings. */
bd99e242 4531 for (child = first_parallel;
0d384044
RS
4532 ! NILP (child);
4533 child = XWINDOW (child)->next)
4534 if (! EQ (child, window))
4535 size_window (child, XINT (CURSIZE (child)) + delta,
5fe0b054 4536 horiz_flag, 0, 0, 1);
0d384044
RS
4537
4538 window = parent;
4539 }
4540
4541 /* If we made a window so small it got deleted,
4542 we failed. Report failure. */
4543 if (delcount != window_deletion_count)
4544 {
4545 Fset_window_configuration (old_config);
4546 error ("Cannot adjust window size as specified");
4547 }
4548
4549 /* Adjust glyph matrices. */
4550 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4551}
4552
7ab12479
JB
4553#undef CURBEG
4554#undef CURSIZE
4555
0d384044
RS
4556DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4557 Sadjust_window_trailing_edge, 3, 3, 0,
4558 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
56ebfae2 4559If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
0d384044
RS
4560Otherwise, adjust the height, moving the bottom edge.
4561
4562Following siblings of the selected window are resized to fulfill
4563the size request. If they become too small in the process, they
4564are not deleted; instead, we signal an error. */)
4565 (window, delta, horizontal)
4566 Lisp_Object window, delta, horizontal;
4567{
4568 CHECK_NUMBER (delta);
4569 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4570
4571 if (! NILP (Vwindow_configuration_change_hook))
4572 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4573
4574 return Qnil;
4575}
4576
5500c422 4577
f984d4fc
GM
4578\f
4579/***********************************************************************
4580 Resizing Mini-Windows
4581 ***********************************************************************/
4582
4583static void shrink_window_lowest_first P_ ((struct window *, int));
f984d4fc 4584
43b4a21f
GM
4585enum save_restore_action
4586{
4587 CHECK_ORIG_SIZES,
4588 SAVE_ORIG_SIZES,
4589 RESTORE_ORIG_SIZES
4590};
4591
177c0ea7 4592static int save_restore_orig_size P_ ((struct window *,
43b4a21f 4593 enum save_restore_action));
f984d4fc
GM
4594
4595/* Shrink windows rooted in window W to HEIGHT. Take the space needed
4596 from lowest windows first. */
4597
4598static void
4599shrink_window_lowest_first (w, height)
4600 struct window *w;
4601 int height;
4602{
4603 struct window *c;
4604 Lisp_Object child;
4605 int old_height;
4606
4607 xassert (!MINI_WINDOW_P (w));
4608
4609 /* Set redisplay hints. */
4610 XSETFASTINT (w->last_modified, 0);
4611 XSETFASTINT (w->last_overlay_modified, 0);
4612 windows_or_buffers_changed++;
4613 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4614
949cf20f
KS
4615 old_height = XFASTINT (w->total_lines);
4616 XSETFASTINT (w->total_lines, height);
f984d4fc
GM
4617
4618 if (!NILP (w->hchild))
4619 {
4620 for (child = w->hchild; !NILP (child); child = c->next)
4621 {
4622 c = XWINDOW (child);
949cf20f 4623 c->top_line = w->top_line;
f984d4fc
GM
4624 shrink_window_lowest_first (c, height);
4625 }
4626 }
4627 else if (!NILP (w->vchild))
4628 {
4629 Lisp_Object last_child;
4630 int delta = old_height - height;
4631 int last_top;
6bbd7a29
GM
4632
4633 last_child = Qnil;
177c0ea7 4634
f984d4fc
GM
4635 /* Find the last child. We are taking space from lowest windows
4636 first, so we iterate over children from the last child
4637 backwards. */
4638 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4639 last_child = child;
4640
4641 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4642 for (child = last_child; delta && !NILP (child); child = c->prev)
4643 {
4644 int this_one;
177c0ea7 4645
f984d4fc 4646 c = XWINDOW (child);
949cf20f 4647 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
f984d4fc
GM
4648
4649 if (this_one > delta)
4650 this_one = delta;
4651
949cf20f 4652 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
f984d4fc
GM
4653 delta -= this_one;
4654 }
4655
4656 /* Compute new positions. */
949cf20f 4657 last_top = XINT (w->top_line);
f984d4fc
GM
4658 for (child = w->vchild; !NILP (child); child = c->next)
4659 {
4660 c = XWINDOW (child);
949cf20f
KS
4661 c->top_line = make_number (last_top);
4662 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4663 last_top += XFASTINT (c->total_lines);
f984d4fc
GM
4664 }
4665 }
4666}
4667
4668
43b4a21f
GM
4669/* Save, restore, or check positions and sizes in the window tree
4670 rooted at W. ACTION says what to do.
f984d4fc 4671
949cf20f
KS
4672 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4673 orig_total_lines members are valid for all windows in the window
4674 tree. Value is non-zero if they are valid.
177c0ea7 4675
43b4a21f 4676 If ACTION is SAVE_ORIG_SIZES, save members top and height in
949cf20f 4677 orig_top_line and orig_total_lines for all windows in the tree.
43b4a21f 4678
949cf20f
KS
4679 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4680 stored in orig_top_line and orig_total_lines for all windows. */
43b4a21f
GM
4681
4682static int
4683save_restore_orig_size (w, action)
f984d4fc 4684 struct window *w;
43b4a21f 4685 enum save_restore_action action;
f984d4fc 4686{
43b4a21f
GM
4687 int success_p = 1;
4688
f984d4fc
GM
4689 while (w)
4690 {
4691 if (!NILP (w->hchild))
43b4a21f
GM
4692 {
4693 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4694 success_p = 0;
4695 }
f984d4fc 4696 else if (!NILP (w->vchild))
43b4a21f
GM
4697 {
4698 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4699 success_p = 0;
4700 }
177c0ea7 4701
43b4a21f 4702 switch (action)
f984d4fc 4703 {
43b4a21f 4704 case CHECK_ORIG_SIZES:
949cf20f 4705 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
43b4a21f
GM
4706 return 0;
4707 break;
4708
4709 case SAVE_ORIG_SIZES:
949cf20f
KS
4710 w->orig_top_line = w->top_line;
4711 w->orig_total_lines = w->total_lines;
43b4a21f
GM
4712 XSETFASTINT (w->last_modified, 0);
4713 XSETFASTINT (w->last_overlay_modified, 0);
4714 break;
4715
4716 case RESTORE_ORIG_SIZES:
949cf20f
KS
4717 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4718 w->top_line = w->orig_top_line;
4719 w->total_lines = w->orig_total_lines;
4720 w->orig_total_lines = w->orig_top_line = Qnil;
43b4a21f
GM
4721 XSETFASTINT (w->last_modified, 0);
4722 XSETFASTINT (w->last_overlay_modified, 0);
4723 break;
4724
4725 default:
4726 abort ();
f984d4fc 4727 }
43b4a21f 4728
f984d4fc
GM
4729 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4730 }
43b4a21f
GM
4731
4732 return success_p;
f984d4fc
GM
4733}
4734
4735
4736/* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4737 without deleting other windows. */
4738
4739void
4740grow_mini_window (w, delta)
4741 struct window *w;
4742 int delta;
4743{
4744 struct frame *f = XFRAME (w->frame);
4745 struct window *root;
177c0ea7 4746
f984d4fc
GM
4747 xassert (MINI_WINDOW_P (w));
4748 xassert (delta >= 0);
177c0ea7 4749
f984d4fc
GM
4750 /* Check values of window_min_width and window_min_height for
4751 validity. */
4752 check_min_window_sizes ();
4753
4754 /* Compute how much we can enlarge the mini-window without deleting
4755 other windows. */
4756 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4757 if (delta)
4758 {
4759 int min_height = window_min_size (root, 0, 0, 0);
949cf20f 4760 if (XFASTINT (root->total_lines) - delta < min_height)
8b8bd9c6 4761 /* Note that the root window may already be smaller than
eafa3196 4762 min_height. */
949cf20f 4763 delta = max (0, XFASTINT (root->total_lines) - min_height);
f984d4fc 4764 }
177c0ea7 4765
f984d4fc
GM
4766 if (delta)
4767 {
4768 /* Save original window sizes and positions, if not already done. */
43b4a21f
GM
4769 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4770 save_restore_orig_size (root, SAVE_ORIG_SIZES);
f984d4fc
GM
4771
4772 /* Shrink other windows. */
949cf20f 4773 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
f984d4fc
GM
4774
4775 /* Grow the mini-window. */
949cf20f
KS
4776 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4777 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
f984d4fc
GM
4778 XSETFASTINT (w->last_modified, 0);
4779 XSETFASTINT (w->last_overlay_modified, 0);
177c0ea7 4780
f984d4fc
GM
4781 adjust_glyphs (f);
4782 }
4783}
4784
4785
86c8e823
GM
4786/* Shrink mini-window W. If there is recorded info about window sizes
4787 before a call to grow_mini_window, restore recorded window sizes.
4788 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4789 line. */
f984d4fc
GM
4790
4791void
4792shrink_mini_window (w)
4793 struct window *w;
4794{
4795 struct frame *f = XFRAME (w->frame);
4796 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4797
43b4a21f 4798 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
f984d4fc 4799 {
43b4a21f 4800 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
f984d4fc
GM
4801 adjust_glyphs (f);
4802 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4803 windows_or_buffers_changed = 1;
4804 }
949cf20f 4805 else if (XFASTINT (w->total_lines) > 1)
86c8e823 4806 {
0130fe1a
GM
4807 /* Distribute the additional lines of the mini-window
4808 among the other windows. */
86c8e823
GM
4809 Lisp_Object window;
4810 XSETWINDOW (window, w);
56ebfae2 4811 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
86c8e823 4812 }
f984d4fc
GM
4813}
4814
4815
4816\f
5500c422
GM
4817/* Mark window cursors off for all windows in the window tree rooted
4818 at W by setting their phys_cursor_on_p flag to zero. Called from
4819 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4820 the frame are cleared. */
4821
4822void
4823mark_window_cursors_off (w)
4824 struct window *w;
4825{
4826 while (w)
4827 {
4828 if (!NILP (w->hchild))
4829 mark_window_cursors_off (XWINDOW (w->hchild));
4830 else if (!NILP (w->vchild))
4831 mark_window_cursors_off (XWINDOW (w->vchild));
4832 else
4833 w->phys_cursor_on_p = 0;
4834
4835 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4836 }
4837}
4838
4839
e9c195b1 4840/* Return number of lines of text (not counting mode lines) in W. */
7ab12479
JB
4841
4842int
4843window_internal_height (w)
4844 struct window *w;
4845{
949cf20f 4846 int ht = XFASTINT (w->total_lines);
7ab12479 4847
e9c195b1
GM
4848 if (!MINI_WINDOW_P (w))
4849 {
4850 if (!NILP (w->parent)
4851 || !NILP (w->vchild)
4852 || !NILP (w->hchild)
4853 || !NILP (w->next)
4854 || !NILP (w->prev)
4855 || WINDOW_WANTS_MODELINE_P (w))
4856 --ht;
7ab12479 4857
e9c195b1
GM
4858 if (WINDOW_WANTS_HEADER_LINE_P (w))
4859 --ht;
4860 }
7ab12479
JB
4861
4862 return ht;
4863}
4864
535e0b8e
JB
4865
4866/* Return the number of columns in W.
a3c87d4e 4867 Don't count columns occupied by scroll bars or the vertical bar
535e0b8e 4868 separating W from the sibling to its right. */
5500c422 4869
535e0b8e 4870int
949cf20f 4871window_box_text_cols (w)
535e0b8e
JB
4872 struct window *w;
4873{
5500c422 4874 struct frame *f = XFRAME (WINDOW_FRAME (w));
949cf20f 4875 int width = XINT (w->total_cols);
535e0b8e 4876
949cf20f 4877 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
5500c422 4878 /* Scroll bars occupy a few columns. */
949cf20f
KS
4879 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4880 else if (!FRAME_WINDOW_P (f)
4881 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
5500c422
GM
4882 /* The column of `|' characters separating side-by-side windows
4883 occupies one column only. */
4884 width -= 1;
4885
5500c422 4886 if (FRAME_WINDOW_P (f))
949cf20f
KS
4887 /* On window-systems, fringes and display margins cannot be
4888 used for normal text. */
4889 width -= (WINDOW_FRINGE_COLS (w)
4890 + WINDOW_LEFT_MARGIN_COLS (w)
4891 + WINDOW_RIGHT_MARGIN_COLS (w));
111e5992
RS
4892
4893 return width;
535e0b8e
JB
4894}
4895
5500c422
GM
4896\f
4897/************************************************************************
4898 Window Scrolling
4899 ***********************************************************************/
535e0b8e 4900
5500c422 4901/* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
d4e7cf01 4902 N screen-fulls, which is defined as the height of the window minus
5500c422
GM
4903 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4904 instead. Negative values of N mean scroll down. NOERROR non-zero
4905 means don't signal an error if we try to move over BEGV or ZV,
4906 respectively. */
7ab12479 4907
101d1605
RS
4908static void
4909window_scroll (window, n, whole, noerror)
7ab12479
JB
4910 Lisp_Object window;
4911 int n;
101d1605 4912 int whole;
f8026fd8 4913 int noerror;
5500c422 4914{
cba59f77
RS
4915 immediate_quit = 1;
4916
5500c422
GM
4917 /* If we must, use the pixel-based version which is much slower than
4918 the line-based one but can handle varying line heights. */
4919 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4920 window_scroll_pixel_based (window, n, whole, noerror);
4921 else
4922 window_scroll_line_based (window, n, whole, noerror);
cba59f77
RS
4923
4924 immediate_quit = 0;
5500c422
GM
4925}
4926
4927
4928/* Implementation of window_scroll that works based on pixel line
4929 heights. See the comment of window_scroll for parameter
4930 descriptions. */
4931
4932static void
4933window_scroll_pixel_based (window, n, whole, noerror)
4934 Lisp_Object window;
4935 int n;
4936 int whole;
4937 int noerror;
4938{
4939 struct it it;
4940 struct window *w = XWINDOW (window);
4941 struct text_pos start;
5500c422 4942 int this_scroll_margin;
5cdb3cf3
MB
4943 /* True if we fiddled the window vscroll field without really scrolling. */
4944 int vscrolled = 0;
5a857365 4945 int x, y, rtop, rbot, rowh, vpos;
5500c422
GM
4946
4947 SET_TEXT_POS_FROM_MARKER (start, w->start);
177c0ea7 4948
5500c422 4949 /* If PT is not visible in WINDOW, move back one half of
288d4e06
GM
4950 the screen. Allow PT to be partially visible, otherwise
4951 something like (scroll-down 1) with PT in the line before
4952 the partially visible one would recenter. */
5a857365
KS
4953
4954 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
5500c422
GM
4955 {
4956 /* Move backward half the height of the window. Performance note:
4957 vmotion used here is about 10% faster, but would give wrong
4958 results for variable height lines. */
4959 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4960 it.current_y = it.last_visible_y;
f204989e 4961 move_it_vertically_backward (&it, window_box_height (w) / 2);
177c0ea7 4962
5500c422
GM
4963 /* The function move_iterator_vertically may move over more than
4964 the specified y-distance. If it->w is small, e.g. a
4965 mini-buffer window, we may end up in front of the window's
4966 display area. This is the case when Start displaying at the
4967 start of the line containing PT in this case. */
4968 if (it.current_y <= 0)
4969 {
4970 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
f204989e 4971 move_it_vertically_backward (&it, 0);
5500c422
GM
4972 it.current_y = 0;
4973 }
4974
4975 start = it.current.pos;
4976 }
e56263e5
KS
4977 else if (auto_window_vscroll_p)
4978 {
5a857365 4979 if (rtop || rbot) /* partially visible */
e56263e5
KS
4980 {
4981 int px;
4982 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4983 if (whole)
e856c216
KS
4984 dy = max ((window_box_height (w)
4985 - next_screen_context_lines * dy),
4986 dy);
e56263e5
KS
4987 dy *= n;
4988
5a857365 4989 if (n < 0)
e56263e5 4990 {
5a857365
KS
4991 /* Only vscroll backwards if already vscrolled forwards. */
4992 if (w->vscroll < 0 && rtop > 0)
4993 {
4994 px = max (0, -w->vscroll - min (rtop, -dy));
4995 Fset_window_vscroll (window, make_number (px), Qt);
4996 return;
4997 }
e56263e5 4998 }
5a857365 4999 if (n > 0)
e56263e5 5000 {
5a857365
KS
5001 /* Do vscroll if already vscrolled or only display line. */
5002 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
5003 {
5004 px = max (0, -w->vscroll + min (rbot, dy));
5005 Fset_window_vscroll (window, make_number (px), Qt);
5006 return;
5007 }
5008
5009 /* Maybe modify window start instead of scrolling. */
5010 if (rbot > 0 || w->vscroll < 0)
5011 {
5012 int spos;
5013
5014 Fset_window_vscroll (window, make_number (0), Qt);
5015 /* If there are other text lines above the current row,
5016 move window start to current row. Else to next row. */
5017 if (rbot > 0)
5018 spos = XINT (Fline_beginning_position (Qnil));
5019 else
5020 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5021 set_marker_restricted (w->start, make_number (spos),
5022 w->buffer);
5023 w->start_at_line_beg = Qt;
5024 w->update_mode_line = Qt;
5025 XSETFASTINT (w->last_modified, 0);
5026 XSETFASTINT (w->last_overlay_modified, 0);
5027 /* Set force_start so that redisplay_window will run the
5028 window-scroll-functions. */
5029 w->force_start = Qt;
5030 return;
5031 }
e56263e5
KS
5032 }
5033 }
5a857365 5034 /* Cancel previous vscroll. */
e56263e5
KS
5035 Fset_window_vscroll (window, make_number (0), Qt);
5036 }
5500c422 5037
d0c38d63 5038 /* If scroll_preserve_screen_position is non-nil, we try to set
5500c422
GM
5039 point in the same window line as it is now, so get that line. */
5040 if (!NILP (Vscroll_preserve_screen_position))
5041 {
c525d842
CY
5042 /* We preserve the goal pixel coordinate across consecutive
5043 calls to scroll-up or scroll-down. This avoids the
5044 possibility of point becoming "stuck" on a tall line when
5045 scrolling by one line. */
66fe93d1 5046 if (window_scroll_pixel_based_preserve_y < 0
7d619454
SM
5047 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
5048 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
c525d842
CY
5049 {
5050 start_display (&it, w, start);
5051 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
66fe93d1 5052 window_scroll_pixel_based_preserve_y = it.current_y;
c525d842 5053 }
5500c422
GM
5054 }
5055 else
66fe93d1 5056 window_scroll_pixel_based_preserve_y = -1;
5500c422
GM
5057
5058 /* Move iterator it from start the specified distance forward or
5059 backward. The result is the new window start. */
5060 start_display (&it, w, start);
5061 if (whole)
5062 {
e856c216
KS
5063 int start_pos = IT_CHARPOS (it);
5064 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
5065 dy = max ((window_box_height (w)
5066 - next_screen_context_lines * dy),
5067 dy) * n;
d72340d4
GM
5068
5069 /* Note that move_it_vertically always moves the iterator to the
5070 start of a line. So, if the last line doesn't have a newline,
5071 we would end up at the start of the line ending at ZV. */
5072 if (dy <= 0)
e856c216
KS
5073 {
5074 move_it_vertically_backward (&it, -dy);
5a857365 5075 /* Ensure we actually do move, e.g. in case we are currently
e856c216
KS
5076 looking at an image that is taller that the window height. */
5077 while (start_pos == IT_CHARPOS (it)
5078 && start_pos > BEGV)
5079 move_it_by_lines (&it, -1, 1);
5080 }
d72340d4 5081 else if (dy > 0)
bed83ee4 5082 {
bed83ee4
KS
5083 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5084 MOVE_TO_POS | MOVE_TO_Y);
5a857365 5085 /* Ensure we actually do move, e.g. in case we are currently
bed83ee4
KS
5086 looking at an image that is taller that the window height. */
5087 while (start_pos == IT_CHARPOS (it)
5088 && start_pos < ZV)
5089 move_it_by_lines (&it, 1, 1);
5090 }
5500c422
GM
5091 }
5092 else
5093 move_it_by_lines (&it, n, 1);
5094
96ae58c8
RS
5095 /* We failed if we find ZV is already on the screen (scrolling up,
5096 means there's nothing past the end), or if we can't start any
5097 earlier (scrolling down, means there's nothing past the top). */
5500c422 5098 if ((n > 0 && IT_CHARPOS (it) == ZV)
96ae58c8 5099 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5500c422 5100 {
5cdb3cf3
MB
5101 if (IT_CHARPOS (it) == ZV)
5102 {
07ce8b53
RS
5103 if (it.current_y < it.last_visible_y
5104 && (it.current_y + it.max_ascent + it.max_descent
3f489dc7 5105 > it.last_visible_y))
a74eca50
GM
5106 {
5107 /* The last line was only partially visible, make it fully
5108 visible. */
5109 w->vscroll = (it.last_visible_y
5110 - it.current_y + it.max_ascent + it.max_descent);
5111 adjust_glyphs (it.f);
5112 }
5cdb3cf3
MB
5113 else if (noerror)
5114 return;
cf402f3f 5115 else if (n < 0) /* could happen with empty buffers */
ba96a5cf 5116 xsignal0 (Qbeginning_of_buffer);
5cdb3cf3 5117 else
ba96a5cf 5118 xsignal0 (Qend_of_buffer);
5cdb3cf3 5119 }
5500c422 5120 else
5cdb3cf3
MB
5121 {
5122 if (w->vscroll != 0)
5123 /* The first line was only partially visible, make it fully
5124 visible. */
5125 w->vscroll = 0;
5126 else if (noerror)
5127 return;
5128 else
ba96a5cf 5129 xsignal0 (Qbeginning_of_buffer);
5cdb3cf3
MB
5130 }
5131
5132 /* If control gets here, then we vscrolled. */
5133
5134 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5135
5136 /* Don't try to change the window start below. */
5137 vscrolled = 1;
5500c422
GM
5138 }
5139
5cdb3cf3
MB
5140 if (! vscrolled)
5141 {
dad67609
RS
5142 int pos = IT_CHARPOS (it);
5143 int bytepos;
e68def1e
AS
5144
5145 /* If in the middle of a multi-glyph character move forward to
5146 the next character. */
5147 if (in_display_vector_p (&it))
5148 {
5149 ++pos;
5150 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5151 }
5152
5cdb3cf3 5153 /* Set the window start, and set up the window for redisplay. */
dad67609 5154 set_marker_restricted (w->start, make_number (pos),
5cdb3cf3 5155 w->buffer);
dad67609
RS
5156 bytepos = XMARKER (w->start)->bytepos;
5157 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
5158 ? Qt : Qnil);
5cdb3cf3
MB
5159 w->update_mode_line = Qt;
5160 XSETFASTINT (w->last_modified, 0);
5161 XSETFASTINT (w->last_overlay_modified, 0);
5162 /* Set force_start so that redisplay_window will run the
5163 window-scroll-functions. */
5164 w->force_start = Qt;
5165 }
177c0ea7 5166
dc297565
RS
5167 /* The rest of this function uses current_y in a nonstandard way,
5168 not including the height of the header line if any. */
5500c422 5169 it.current_y = it.vpos = 0;
177c0ea7 5170
940f53e5
RS
5171 /* Move PT out of scroll margins.
5172 This code wants current_y to be zero at the window start position
5173 even if there is a header line. */
5174 this_scroll_margin = max (0, scroll_margin);
5175 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
5176 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5177
5178 if (n > 0)
5500c422 5179 {
940f53e5
RS
5180 /* We moved the window start towards ZV, so PT may be now
5181 in the scroll margin at the top. */
5182 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
bdf4ec93
RS
5183 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5184 && (NILP (Vscroll_preserve_screen_position)
5185 || EQ (Vscroll_preserve_screen_position, Qt)))
940f53e5
RS
5186 /* We found PT at a legitimate height. Leave it alone. */
5187 ;
66fe93d1 5188 else if (window_scroll_pixel_based_preserve_y >= 0)
940f53e5 5189 {
fa3c3426
RS
5190 /* If we have a header line, take account of it.
5191 This is necessary because we set it.current_y to 0, above. */
c525d842 5192 move_it_to (&it, -1, -1,
66fe93d1
LT
5193 window_scroll_pixel_based_preserve_y
5194 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
c525d842 5195 -1, MOVE_TO_Y);
940f53e5
RS
5196 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5197 }
5198 else
5500c422 5199 {
5500c422 5200 while (it.current_y < this_scroll_margin)
e9b2c961
RS
5201 {
5202 int prev = it.current_y;
5203 move_it_by_lines (&it, 1, 1);
5204 if (prev == it.current_y)
5205 break;
5206 }
5500c422
GM
5207 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5208 }
940f53e5
RS
5209 }
5210 else if (n < 0)
5211 {
5212 int charpos, bytepos;
aed328bf 5213 int partial_p;
940f53e5 5214
66fe93d1
LT
5215 /* Save our position, for the
5216 window_scroll_pixel_based_preserve_y case. */
940f53e5
RS
5217 charpos = IT_CHARPOS (it);
5218 bytepos = IT_BYTEPOS (it);
5cdb3cf3 5219
940f53e5
RS
5220 /* We moved the window start towards BEGV, so PT may be now
5221 in the scroll margin at the bottom. */
5222 move_it_to (&it, PT, -1,
7ad53239
RS
5223 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5224 - this_scroll_margin - 1),
5225 -1,
940f53e5
RS
5226 MOVE_TO_POS | MOVE_TO_Y);
5227
aed328bf
KS
5228 /* Save our position, in case it's correct. */
5229 charpos = IT_CHARPOS (it);
5230 bytepos = IT_BYTEPOS (it);
5231
5232 /* See if point is on a partially visible line at the end. */
5233 if (it.what == IT_EOB)
5234 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5235 else
5236 {
5237 move_it_by_lines (&it, 1, 1);
5238 partial_p = it.current_y > it.last_visible_y;
5239 }
5240
bdf4ec93
RS
5241 if (charpos == PT && !partial_p
5242 && (NILP (Vscroll_preserve_screen_position)
5243 || EQ (Vscroll_preserve_screen_position, Qt)))
940f53e5
RS
5244 /* We found PT before we found the display margin, so PT is ok. */
5245 ;
66fe93d1 5246 else if (window_scroll_pixel_based_preserve_y >= 0)
940f53e5
RS
5247 {
5248 SET_TEXT_POS_FROM_MARKER (start, w->start);
5249 start_display (&it, w, start);
c525d842
CY
5250 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5251 here because we called start_display again and did not
5252 alter it.current_y this time. */
66fe93d1
LT
5253 move_it_to (&it, -1, -1, window_scroll_pixel_based_preserve_y, -1,
5254 MOVE_TO_Y);
940f53e5
RS
5255 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5256 }
5257 else
5258 {
aed328bf 5259 if (partial_p)
5cdb3cf3
MB
5260 /* The last line was only partially visible, so back up two
5261 lines to make sure we're on a fully visible line. */
5262 {
5263 move_it_by_lines (&it, -2, 0);
5264 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5265 }
5266 else
5267 /* No, the position we saved is OK, so use it. */
5268 SET_PT_BOTH (charpos, bytepos);
5500c422
GM
5269 }
5270 }
5271}
5272
5273
5274/* Implementation of window_scroll that works based on screen lines.
5275 See the comment of window_scroll for parameter descriptions. */
5276
5277static void
5278window_scroll_line_based (window, n, whole, noerror)
5279 Lisp_Object window;
5280 int n;
5281 int whole;
5282 int noerror;
7ab12479
JB
5283{
5284 register struct window *w = XWINDOW (window);
5500c422 5285 register int opoint = PT, opoint_byte = PT_BYTE;
b73ea88e 5286 register int pos, pos_byte;
7ab12479
JB
5287 register int ht = window_internal_height (w);
5288 register Lisp_Object tem;
5289 int lose;
5500c422 5290 Lisp_Object bolp;
345d45b2 5291 int startpos;
101d1605
RS
5292 struct position posit;
5293 int original_vpos;
5294
d4e7cf01
GM
5295 /* If scrolling screen-fulls, compute the number of lines to
5296 scroll from the window's height. */
5297 if (whole)
5298 n *= max (1, ht - next_screen_context_lines);
5299
101d1605
RS
5300 startpos = marker_position (w->start);
5301
5302 posit = *compute_motion (startpos, 0, 0, 0,
5303 PT, ht, 0,
ec2b66c4 5304 -1, XINT (w->hscroll),
101d1605
RS
5305 0, w);
5306 original_vpos = posit.vpos;
0a1f771a 5307
d834a2e9 5308 XSETFASTINT (tem, PT);
6ffdb539 5309 tem = Fpos_visible_in_window_p (tem, window, Qnil);
7ab12479 5310
265a9e55 5311 if (NILP (tem))
7ab12479 5312 {
cd2be1dd 5313 Fvertical_motion (make_number (- (ht / 2)), window);
345d45b2 5314 startpos = PT;
7ab12479
JB
5315 }
5316
345d45b2 5317 SET_PT (startpos);
5ce7b543 5318 lose = n < 0 && PT == BEGV;
540b6aa0 5319 Fvertical_motion (make_number (n), window);
5ce7b543 5320 pos = PT;
b73ea88e 5321 pos_byte = PT_BYTE;
7ab12479 5322 bolp = Fbolp ();
b73ea88e 5323 SET_PT_BOTH (opoint, opoint_byte);
7ab12479
JB
5324
5325 if (lose)
f8026fd8
JB
5326 {
5327 if (noerror)
5328 return;
5329 else
ba96a5cf 5330 xsignal0 (Qbeginning_of_buffer);
f8026fd8 5331 }
7ab12479
JB
5332
5333 if (pos < ZV)
7ab12479 5334 {
0c7da84e
RS
5335 int this_scroll_margin = scroll_margin;
5336
5337 /* Don't use a scroll margin that is negative or too large. */
5338 if (this_scroll_margin < 0)
5339 this_scroll_margin = 0;
5340
949cf20f
KS
5341 if (XINT (w->total_lines) < 4 * scroll_margin)
5342 this_scroll_margin = XINT (w->total_lines) / 4;
0c7da84e 5343
b73ea88e 5344 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
7ab12479
JB
5345 w->start_at_line_beg = bolp;
5346 w->update_mode_line = Qt;
d834a2e9 5347 XSETFASTINT (w->last_modified, 0);
3cd21523 5348 XSETFASTINT (w->last_overlay_modified, 0);
345d45b2
RS
5349 /* Set force_start so that redisplay_window will run
5350 the window-scroll-functions. */
5351 w->force_start = Qt;
0c7da84e 5352
bdf4ec93
RS
5353 if (!NILP (Vscroll_preserve_screen_position)
5354 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
0c7da84e 5355 {
b73ea88e 5356 SET_PT_BOTH (pos, pos_byte);
101d1605 5357 Fvertical_motion (make_number (original_vpos), window);
0c7da84e 5358 }
101d1605
RS
5359 /* If we scrolled forward, put point enough lines down
5360 that it is outside the scroll margin. */
5361 else if (n > 0)
0c7da84e 5362 {
101d1605
RS
5363 int top_margin;
5364
5365 if (this_scroll_margin > 0)
5366 {
b73ea88e 5367 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
5368 Fvertical_motion (make_number (this_scroll_margin), window);
5369 top_margin = PT;
5370 }
5371 else
5372 top_margin = pos;
5373
5374 if (top_margin <= opoint)
b73ea88e 5375 SET_PT_BOTH (opoint, opoint_byte);
5500c422 5376 else if (!NILP (Vscroll_preserve_screen_position))
101d1605 5377 {
b73ea88e 5378 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
5379 Fvertical_motion (make_number (original_vpos), window);
5380 }
9317a85d 5381 else
335406fc 5382 SET_PT (top_margin);
0c7da84e 5383 }
101d1605 5384 else if (n < 0)
7ab12479 5385 {
101d1605
RS
5386 int bottom_margin;
5387
0c7da84e
RS
5388 /* If we scrolled backward, put point near the end of the window
5389 but not within the scroll margin. */
b73ea88e 5390 SET_PT_BOTH (pos, pos_byte);
0c7da84e 5391 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
101d1605
RS
5392 if (XFASTINT (tem) == ht - this_scroll_margin)
5393 bottom_margin = PT;
5394 else
5395 bottom_margin = PT + 1;
5396
5397 if (bottom_margin > opoint)
b73ea88e 5398 SET_PT_BOTH (opoint, opoint_byte);
7ab12479 5399 else
101d1605 5400 {
5500c422 5401 if (!NILP (Vscroll_preserve_screen_position))
9317a85d 5402 {
b73ea88e 5403 SET_PT_BOTH (pos, pos_byte);
9317a85d
RS
5404 Fvertical_motion (make_number (original_vpos), window);
5405 }
5406 else
5407 Fvertical_motion (make_number (-1), window);
101d1605 5408 }
7ab12479
JB
5409 }
5410 }
5411 else
f8026fd8
JB
5412 {
5413 if (noerror)
5414 return;
5415 else
ba96a5cf 5416 xsignal0 (Qend_of_buffer);
f8026fd8 5417 }
7ab12479 5418}
5500c422
GM
5419
5420
5421/* Scroll selected_window up or down. If N is nil, scroll a
5422 screen-full which is defined as the height of the window minus
5423 next_screen_context_lines. If N is the symbol `-', scroll.
5424 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5425 up. This is the guts of Fscroll_up and Fscroll_down. */
7ab12479
JB
5426
5427static void
5428scroll_command (n, direction)
5500c422 5429 Lisp_Object n;
7ab12479
JB
5430 int direction;
5431{
331379bf 5432 int count = SPECPDL_INDEX ();
7ab12479 5433
5500c422
GM
5434 xassert (abs (direction) == 1);
5435
5436 /* If selected window's buffer isn't current, make it current for
5437 the moment. But don't screw up if window_scroll gets an error. */
7ab12479 5438 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
95605e15
JB
5439 {
5440 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5441 Fset_buffer (XWINDOW (selected_window)->buffer);
5500c422
GM
5442
5443 /* Make redisplay consider other windows than just selected_window. */
5444 ++windows_or_buffers_changed;
95605e15 5445 }
7ab12479 5446
265a9e55 5447 if (NILP (n))
d4e7cf01 5448 window_scroll (selected_window, direction, 1, 0);
7ab12479 5449 else if (EQ (n, Qminus))
d4e7cf01 5450 window_scroll (selected_window, -direction, 1, 0);
7ab12479
JB
5451 else
5452 {
5453 n = Fprefix_numeric_value (n);
101d1605 5454 window_scroll (selected_window, XINT (n) * direction, 0, 0);
7ab12479 5455 }
95605e15
JB
5456
5457 unbind_to (count, Qnil);
7ab12479
JB
5458}
5459
5460DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
a0a37a6f
LT
5461 doc: /* Scroll text of current window upward ARG lines.
5462If ARG is omitted or nil, scroll upward by a near full screen.
fdb82f93
PJ
5463A near full screen is `next-screen-context-lines' less than a full screen.
5464Negative ARG means scroll downward.
5465If ARG is the atom `-', scroll downward by nearly full screen.
5466When calling from a program, supply as argument a number, nil, or `-'. */)
5467 (arg)
413430c5 5468 Lisp_Object arg;
7ab12479 5469{
413430c5 5470 scroll_command (arg, 1);
7ab12479
JB
5471 return Qnil;
5472}
5473
5474DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
a0a37a6f
LT
5475 doc: /* Scroll text of current window down ARG lines.
5476If ARG is omitted or nil, scroll down by a near full screen.
fdb82f93
PJ
5477A near full screen is `next-screen-context-lines' less than a full screen.
5478Negative ARG means scroll upward.
5479If ARG is the atom `-', scroll upward by nearly full screen.
5480When calling from a program, supply as argument a number, nil, or `-'. */)
5481 (arg)
413430c5 5482 Lisp_Object arg;
7ab12479 5483{
413430c5 5484 scroll_command (arg, -1);
7ab12479
JB
5485 return Qnil;
5486}
ccd0664b
RS
5487\f
5488DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
fdb82f93 5489 doc: /* Return the other window for \"other window scroll\" commands.
fdb82f93 5490If `other-window-scroll-buffer' is non-nil, a window
a0a37a6f
LT
5491showing that buffer is used.
5492If in the minibuffer, `minibuffer-scroll-window' if non-nil
5493specifies the window. This takes precedence over
5494`other-window-scroll-buffer'. */)
fdb82f93 5495 ()
7ab12479 5496{
ccd0664b 5497 Lisp_Object window;
7ab12479
JB
5498
5499 if (MINI_WINDOW_P (XWINDOW (selected_window))
265a9e55 5500 && !NILP (Vminibuf_scroll_window))
7ab12479
JB
5501 window = Vminibuf_scroll_window;
5502 /* If buffer is specified, scroll that buffer. */
265a9e55 5503 else if (!NILP (Vother_window_scroll_buffer))
7ab12479
JB
5504 {
5505 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
265a9e55 5506 if (NILP (window))
53f76081 5507 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
7ab12479
JB
5508 }
5509 else
dbc4e1c1
JB
5510 {
5511 /* Nothing specified; look for a neighboring window on the same
5512 frame. */
5513 window = Fnext_window (selected_window, Qnil, Qnil);
5514
5515 if (EQ (window, selected_window))
5516 /* That didn't get us anywhere; look for a window on another
5517 visible frame. */
5518 do
5519 window = Fnext_window (window, Qnil, Qt);
5520 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5521 && ! EQ (window, selected_window));
5522 }
5523
b7826503 5524 CHECK_LIVE_WINDOW (window);
7ab12479
JB
5525
5526 if (EQ (window, selected_window))
5527 error ("There is no other window");
5528
ccd0664b
RS
5529 return window;
5530}
5531
5532DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
fdb82f93
PJ
5533 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5534A near full screen is `next-screen-context-lines' less than a full screen.
5535The next window is the one below the current one; or the one at the top
5536if the current one is at the bottom. Negative ARG means scroll downward.
5537If ARG is the atom `-', scroll downward by nearly full screen.
5538When calling from a program, supply as argument a number, nil, or `-'.
5539
fdb82f93 5540If `other-window-scroll-buffer' is non-nil, scroll the window
a0a37a6f
LT
5541showing that buffer, popping the buffer up if necessary.
5542If in the minibuffer, `minibuffer-scroll-window' if non-nil
5543specifies the window to scroll. This takes precedence over
5544`other-window-scroll-buffer'. */)
fdb82f93 5545 (arg)
d4e7cf01 5546 Lisp_Object arg;
ccd0664b 5547{
d4e7cf01
GM
5548 Lisp_Object window;
5549 struct window *w;
331379bf 5550 int count = SPECPDL_INDEX ();
ccd0664b
RS
5551
5552 window = Fother_window_for_scrolling ();
7ab12479 5553 w = XWINDOW (window);
7ab12479
JB
5554
5555 /* Don't screw up if window_scroll gets an error. */
5556 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5500c422 5557 ++windows_or_buffers_changed;
7ab12479
JB
5558
5559 Fset_buffer (w->buffer);
5560 SET_PT (marker_position (w->pointm));
5561
413430c5 5562 if (NILP (arg))
d4e7cf01 5563 window_scroll (window, 1, 1, 1);
413430c5 5564 else if (EQ (arg, Qminus))
d4e7cf01 5565 window_scroll (window, -1, 1, 1);
7ab12479
JB
5566 else
5567 {
413430c5
EN
5568 if (CONSP (arg))
5569 arg = Fcar (arg);
b7826503 5570 CHECK_NUMBER (arg);
101d1605 5571 window_scroll (window, XINT (arg), 0, 1);
7ab12479
JB
5572 }
5573
b73ea88e 5574 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
f4e7b2c2 5575 unbind_to (count, Qnil);
7ab12479
JB
5576
5577 return Qnil;
5578}
5579\f
dc297565 5580DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "P\np",
fdb82f93 5581 doc: /* Scroll selected window display ARG columns left.
2fe41216
EZ
5582Default for ARG is window width minus 2.
5583Value is the total amount of leftward horizontal scrolling in
5584effect after the change.
dc297565
RS
5585If SET_MINIMUM is non-nil, the new scroll amount becomes the
5586lower bound for automatic scrolling, i.e. automatic scrolling
2fe41216 5587will not scroll a window to a column less than the value returned
dc297565
RS
5588by this function. This happens in an interactive call. */)
5589 (arg, set_minimum)
5590 register Lisp_Object arg, set_minimum;
7ab12479 5591{
c67fa410
GM
5592 Lisp_Object result;
5593 int hscroll;
5594 struct window *w = XWINDOW (selected_window);
177c0ea7 5595
265a9e55 5596 if (NILP (arg))
949cf20f 5597 XSETFASTINT (arg, window_box_text_cols (w) - 2);
7ab12479
JB
5598 else
5599 arg = Fprefix_numeric_value (arg);
5600
c67fa410
GM
5601 hscroll = XINT (w->hscroll) + XINT (arg);
5602 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5603
dc297565 5604 if (!NILP (set_minimum))
c67fa410
GM
5605 w->min_hscroll = w->hscroll;
5606
5607 return result;
7ab12479
JB
5608}
5609
dc297565 5610DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "P\np",
fdb82f93 5611 doc: /* Scroll selected window display ARG columns right.
2fe41216
EZ
5612Default for ARG is window width minus 2.
5613Value is the total amount of leftward horizontal scrolling in
5614effect after the change.
dc297565
RS
5615If SET_MINIMUM is non-nil, the new scroll amount becomes the
5616lower bound for automatic scrolling, i.e. automatic scrolling
2fe41216 5617will not scroll a window to a column less than the value returned
dc297565
RS
5618by this function. This happens in an interactive call. */)
5619 (arg, set_minimum)
a93563fd 5620 register Lisp_Object arg, set_minimum;
7ab12479 5621{
c67fa410
GM
5622 Lisp_Object result;
5623 int hscroll;
5624 struct window *w = XWINDOW (selected_window);
177c0ea7 5625
265a9e55 5626 if (NILP (arg))
949cf20f 5627 XSETFASTINT (arg, window_box_text_cols (w) - 2);
7ab12479
JB
5628 else
5629 arg = Fprefix_numeric_value (arg);
5630
c67fa410
GM
5631 hscroll = XINT (w->hscroll) - XINT (arg);
5632 result = Fset_window_hscroll (selected_window, make_number (hscroll));
177c0ea7 5633
dc297565 5634 if (!NILP (set_minimum))
c67fa410
GM
5635 w->min_hscroll = w->hscroll;
5636
5637 return result;
7ab12479
JB
5638}
5639
fa832261
KS
5640DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5641 doc: /* Return the window which was selected when entering the minibuffer.
5642Returns nil, if current window is not a minibuffer window. */)
5643 ()
5644{
5645 if (minibuf_level > 0
5646 && MINI_WINDOW_P (XWINDOW (selected_window))
fa832261
KS
5647 && WINDOW_LIVE_P (minibuf_selected_window))
5648 return minibuf_selected_window;
5649
5650 return Qnil;
5651}
5652
12c8b416
GM
5653/* Value is the number of lines actually displayed in window W,
5654 as opposed to its height. */
5655
5656static int
5657displayed_window_lines (w)
5658 struct window *w;
5659{
5660 struct it it;
5661 struct text_pos start;
5662 int height = window_box_height (w);
5663 struct buffer *old_buffer;
5664 int bottom_y;
5665
5666 if (XBUFFER (w->buffer) != current_buffer)
5667 {
5668 old_buffer = current_buffer;
5669 set_buffer_internal (XBUFFER (w->buffer));
5670 }
5671 else
5672 old_buffer = NULL;
5673
521b203e
GM
5674 /* In case W->start is out of the accessible range, do something
5675 reasonable. This happens in Info mode when Info-scroll-down
5676 calls (recenter -1) while W->start is 1. */
5677 if (XMARKER (w->start)->charpos < BEGV)
5678 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5679 else if (XMARKER (w->start)->charpos > ZV)
5680 SET_TEXT_POS (start, ZV, ZV_BYTE);
5681 else
5682 SET_TEXT_POS_FROM_MARKER (start, w->start);
5683
12c8b416
GM
5684 start_display (&it, w, start);
5685 move_it_vertically (&it, height);
c8bc6f65 5686 bottom_y = line_bottom_y (&it);
12c8b416 5687
1de65f51
RS
5688 /* rms: On a non-window display,
5689 the value of it.vpos at the bottom of the screen
5690 seems to be 1 larger than window_box_height (w).
5691 This kludge fixes a bug whereby (move-to-window-line -1)
5692 when ZV is on the last screen line
5693 moves to the previous screen line instead of the last one. */
5694 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5695 height++;
5696
12c8b416
GM
5697 /* Add in empty lines at the bottom of the window. */
5698 if (bottom_y < height)
5699 {
949cf20f 5700 int uy = FRAME_LINE_HEIGHT (it.f);
c8bc6f65 5701 it.vpos += (height - bottom_y + uy - 1) / uy;
12c8b416
GM
5702 }
5703
c8bc6f65
GM
5704 if (old_buffer)
5705 set_buffer_internal (old_buffer);
5706
12c8b416
GM
5707 return it.vpos;
5708}
5709
5710
7ab12479 5711DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
fdb82f93
PJ
5712 doc: /* Center point in window and redisplay frame.
5713With prefix argument ARG, recenter putting point on screen line ARG
5714relative to the current window. If ARG is negative, it counts up from the
5715bottom of the window. (ARG should be less than the height of the window.)
5716
5717If ARG is omitted or nil, erase the entire frame and then
5718redraw with point in the center of the current window.
5719Just C-u as prefix means put point in the center of the window
5720and redisplay normally--don't erase and redraw the frame. */)
5721 (arg)
413430c5 5722 register Lisp_Object arg;
7ab12479 5723{
6df47b59 5724 struct window *w = XWINDOW (selected_window);
478292ed
RS
5725 struct buffer *buf = XBUFFER (w->buffer);
5726 struct buffer *obuf = current_buffer;
6df47b59
GM
5727 int center_p = 0;
5728 int charpos, bytepos;
f6b43440
RS
5729 int iarg;
5730 int this_scroll_margin;
7ab12479 5731
0fa5d25b
RS
5732 /* If redisplay is suppressed due to an error, try again. */
5733 obuf->display_error_modiff = 0;
5734
413430c5 5735 if (NILP (arg))
7ab12479 5736 {
f02d6d5c
KH
5737 int i;
5738
5739 /* Invalidate pixel data calculated for all compositions. */
5740 for (i = 0; i < n_compositions; i++)
5741 composition_table[i]->font = NULL;
7ab12479 5742
527b6458 5743 Fredraw_frame (w->frame);
44fa5b1e 5744 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
6df47b59 5745 center_p = 1;
7ab12479 5746 }
413430c5 5747 else if (CONSP (arg)) /* Just C-u. */
6df47b59 5748 center_p = 1;
7ab12479
JB
5749 else
5750 {
413430c5 5751 arg = Fprefix_numeric_value (arg);
b7826503 5752 CHECK_NUMBER (arg);
ae12ecd7 5753 iarg = XINT (arg);
7ab12479
JB
5754 }
5755
478292ed 5756 set_buffer_internal (buf);
7ab12479 5757
f6b43440
RS
5758 /* Do this after making BUF current
5759 in case scroll_margin is buffer-local. */
5760 this_scroll_margin = max (0, scroll_margin);
5761 this_scroll_margin = min (this_scroll_margin,
5762 XFASTINT (w->total_lines) / 4);
5763
521b203e 5764 /* Handle centering on a graphical frame specially. Such frames can
6df47b59
GM
5765 have variable-height lines and centering point on the basis of
5766 line counts would lead to strange effects. */
521b203e 5767 if (FRAME_WINDOW_P (XFRAME (w->frame)))
6df47b59 5768 {
6df47b59
GM
5769 if (center_p)
5770 {
521b203e
GM
5771 struct it it;
5772 struct text_pos pt;
177c0ea7 5773
521b203e
GM
5774 SET_TEXT_POS (pt, PT, PT_BYTE);
5775 start_display (&it, w, pt);
f204989e 5776 move_it_vertically_backward (&it, window_box_height (w) / 2);
521b203e
GM
5777 charpos = IT_CHARPOS (it);
5778 bytepos = IT_BYTEPOS (it);
6df47b59 5779 }
f6b43440 5780 else if (iarg < 0)
6df47b59 5781 {
521b203e
GM
5782 struct it it;
5783 struct text_pos pt;
f6b43440 5784 int nlines = -iarg;
f204989e
KS
5785 int extra_line_spacing;
5786 int h = window_box_height (w);
177c0ea7 5787
f6b43440
RS
5788 iarg = - max (-iarg, this_scroll_margin);
5789
521b203e
GM
5790 SET_TEXT_POS (pt, PT, PT_BYTE);
5791 start_display (&it, w, pt);
f204989e
KS
5792
5793 /* Be sure we have the exact height of the full line containing PT. */
5794 move_it_by_lines (&it, 0, 1);
521b203e 5795
d466fa4d 5796 /* The amount of pixels we have to move back is the window
521b203e
GM
5797 height minus what's displayed in the line containing PT,
5798 and the lines below. */
f204989e
KS
5799 it.current_y = 0;
5800 it.vpos = 0;
d466fa4d
GM
5801 move_it_by_lines (&it, nlines, 1);
5802
f204989e
KS
5803 if (it.vpos == nlines)
5804 h -= it.current_y;
5805 else
5806 {
5807 /* Last line has no newline */
5808 h -= line_bottom_y (&it);
5809 it.vpos++;
5810 }
5811
5812 /* Don't reserve space for extra line spacing of last line. */
5813 extra_line_spacing = it.max_extra_line_spacing;
d466fa4d
GM
5814
5815 /* If we can't move down NLINES lines because we hit
5816 the end of the buffer, count in some empty lines. */
5817 if (it.vpos < nlines)
f204989e
KS
5818 {
5819 nlines -= it.vpos;
5820 extra_line_spacing = it.extra_line_spacing;
5821 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5822 }
5823 if (h <= 0)
5824 return Qnil;
201c831a 5825
f204989e 5826 /* Now find the new top line (starting position) of the window. */
521b203e 5827 start_display (&it, w, pt);
f204989e
KS
5828 it.current_y = 0;
5829 move_it_vertically_backward (&it, h);
5830
5831 /* If extra line spacing is present, we may move too far
5832 back. This causes the last line to be only partially
5833 visible (which triggers redisplay to recenter that line
5834 in the middle), so move forward.
5835 But ignore extra line spacing on last line, as it is not
5836 considered to be part of the visible height of the line.
5837 */
5838 h += extra_line_spacing;
5839 while (-it.current_y > h)
5840 move_it_by_lines (&it, 1, 1);
5841
521b203e
GM
5842 charpos = IT_CHARPOS (it);
5843 bytepos = IT_BYTEPOS (it);
6df47b59 5844 }
521b203e
GM
5845 else
5846 {
5847 struct position pos;
f6b43440 5848
f6b43440
RS
5849 iarg = max (iarg, this_scroll_margin);
5850
5851 pos = *vmotion (PT, -iarg, w);
521b203e
GM
5852 charpos = pos.bufpos;
5853 bytepos = pos.bytepos;
5854 }
5855 }
5856 else
5857 {
5858 struct position pos;
5859 int ht = window_internal_height (w);
5860
5861 if (center_p)
a4429c5b 5862 iarg = ht / 2;
f567c488
KS
5863 else if (iarg < 0)
5864 iarg += ht;
f6b43440
RS
5865
5866 /* Don't let it get into the margin at either top or bottom. */
5867 iarg = max (iarg, this_scroll_margin);
5868 iarg = min (iarg, ht - this_scroll_margin - 1);
177c0ea7 5869
f6b43440 5870 pos = *vmotion (PT, - iarg, w);
6df47b59
GM
5871 charpos = pos.bufpos;
5872 bytepos = pos.bytepos;
5873 }
5874
5875 /* Set the new window start. */
5876 set_marker_both (w->start, w->buffer, charpos, bytepos);
2f3cad6c 5877 w->window_end_valid = Qnil;
177c0ea7 5878
95605b1b
RS
5879 w->optional_new_start = Qt;
5880
6df47b59
GM
5881 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5882 w->start_at_line_beg = Qt;
5883 else
5884 w->start_at_line_beg = Qnil;
177c0ea7 5885
478292ed 5886 set_buffer_internal (obuf);
7ab12479
JB
5887 return Qnil;
5888}
b7617575
GM
5889
5890
81fe0836 5891DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
fdb82f93
PJ
5892 0, 1, 0,
5893 doc: /* Return the height in lines of the text display area of WINDOW.
5894This doesn't include the mode-line (or header-line if any) or any
5895partial-height lines in the text display area. */)
5896 (window)
81fe0836
MB
5897 Lisp_Object window;
5898{
5899 struct window *w = decode_window (window);
5900 int pixel_height = window_box_height (w);
949cf20f 5901 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
81fe0836
MB
5902 return make_number (line_height);
5903}
5904
5905
7ab12479
JB
5906\f
5907DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
fdb82f93
PJ
5908 1, 1, "P",
5909 doc: /* Position point relative to window.
5910With no argument, position point at center of window.
5911An argument specifies vertical position within the window;
5912zero means top of window, negative means relative to bottom of window. */)
5913 (arg)
b7617575 5914 Lisp_Object arg;
7ab12479 5915{
b7617575
GM
5916 struct window *w = XWINDOW (selected_window);
5917 int lines, start;
540b6aa0 5918 Lisp_Object window;
f6b43440
RS
5919#if 0
5920 int this_scroll_margin;
5921#endif
7ab12479 5922
b7617575 5923 window = selected_window;
7ab12479
JB
5924 start = marker_position (w->start);
5925 if (start < BEGV || start > ZV)
5926 {
b7617575 5927 int height = window_internal_height (w);
cd2be1dd 5928 Fvertical_motion (make_number (- (height / 2)), window);
b73ea88e 5929 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
7ab12479
JB
5930 w->start_at_line_beg = Fbolp ();
5931 w->force_start = Qt;
5932 }
5933 else
b73ea88e 5934 Fgoto_char (w->start);
7ab12479 5935
b7617575 5936 lines = displayed_window_lines (w);
f6b43440
RS
5937
5938#if 0
5939 this_scroll_margin = max (0, scroll_margin);
5940 this_scroll_margin = min (this_scroll_margin, lines / 4);
5941#endif
5942
b7617575
GM
5943 if (NILP (arg))
5944 XSETFASTINT (arg, lines / 2);
5945 else
5946 {
f6b43440
RS
5947 int iarg = XINT (Fprefix_numeric_value (arg));
5948
5949 if (iarg < 0)
5950 iarg = iarg + lines;
5951
5952#if 0 /* This code would prevent move-to-window-line from moving point
5953 to a place inside the scroll margins (which would cause the
5954 next redisplay to scroll). I wrote this code, but then concluded
5955 it is probably better not to install it. However, it is here
5956 inside #if 0 so as not to lose it. -- rms. */
5957
5958 /* Don't let it get into the margin at either top or bottom. */
5959 iarg = max (iarg, this_scroll_margin);
5960 iarg = min (iarg, lines - this_scroll_margin - 1);
5961#endif
5962
5963 arg = make_number (iarg);
b7617575
GM
5964 }
5965
c8bc6f65 5966 /* Skip past a partially visible first line. */
163784df 5967 if (w->vscroll)
163784df
MB
5968 XSETINT (arg, XINT (arg) + 1);
5969
540b6aa0 5970 return Fvertical_motion (arg, window);
7ab12479 5971}
5500c422
GM
5972
5973
7ab12479 5974\f
5500c422
GM
5975/***********************************************************************
5976 Window Configuration
5977 ***********************************************************************/
5978
7ab12479
JB
5979struct save_window_data
5980 {
f5ccc0cc 5981 EMACS_INT size_from_Lisp_Vector_struct;
7ab12479 5982 struct Lisp_Vector *next_from_Lisp_Vector_struct;
949cf20f 5983 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
9ea173e8 5984 Lisp_Object frame_tool_bar_lines;
bdc727bf 5985 Lisp_Object selected_frame;
7ab12479
JB
5986 Lisp_Object current_window;
5987 Lisp_Object current_buffer;
5988 Lisp_Object minibuf_scroll_window;
3f49fddc 5989 Lisp_Object minibuf_selected_window;
7ab12479 5990 Lisp_Object root_window;
bdc727bf 5991 Lisp_Object focus_frame;
756b6edc
RS
5992 /* Record the values of window-min-width and window-min-height
5993 so that window sizes remain consistent with them. */
5994 Lisp_Object min_width, min_height;
cbff28e8
RS
5995 /* A vector, each of whose elements is a struct saved_window
5996 for one window. */
7ab12479
JB
5997 Lisp_Object saved_windows;
5998 };
ff06df24 5999
cbff28e8 6000/* This is saved as a Lisp_Vector */
7ab12479 6001struct saved_window
ea68264b
GM
6002{
6003 /* these first two must agree with struct Lisp_Vector in lisp.h */
6004 EMACS_INT size_from_Lisp_Vector_struct;
6005 struct Lisp_Vector *next_from_Lisp_Vector_struct;
7ab12479 6006
ea68264b
GM
6007 Lisp_Object window;
6008 Lisp_Object buffer, start, pointm, mark;
949cf20f
KS
6009 Lisp_Object left_col, top_line, total_cols, total_lines;
6010 Lisp_Object hscroll, min_hscroll;
ea68264b
GM
6011 Lisp_Object parent, prev;
6012 Lisp_Object start_at_line_beg;
6013 Lisp_Object display_table;
949cf20f
KS
6014 Lisp_Object orig_top_line, orig_total_lines;
6015 Lisp_Object left_margin_cols, right_margin_cols;
6016 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6017 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
6ad0381c 6018 Lisp_Object dedicated;
ea68264b
GM
6019};
6020
7ab12479
JB
6021#define SAVED_WINDOW_N(swv,n) \
6022 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6023
6024DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
fdb82f93
PJ
6025 doc: /* Return t if OBJECT is a window-configuration object. */)
6026 (object)
413430c5 6027 Lisp_Object object;
7ab12479 6028{
6ad0381c 6029 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
7ab12479
JB
6030}
6031
3f8ab7bd 6032DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
fdb82f93
PJ
6033 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6034 (config)
3f8ab7bd
RS
6035 Lisp_Object config;
6036{
6037 register struct save_window_data *data;
6038 struct Lisp_Vector *saved_windows;
6039
663fbbba 6040 CHECK_WINDOW_CONFIGURATION (config);
3f8ab7bd
RS
6041
6042 data = (struct save_window_data *) XVECTOR (config);
6043 saved_windows = XVECTOR (data->saved_windows);
6044 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6045}
6046
d5b2799e 6047DEFUN ("set-window-configuration", Fset_window_configuration,
fdb82f93
PJ
6048 Sset_window_configuration, 1, 1, 0,
6049 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6050CONFIGURATION must be a value previously returned
6051by `current-window-configuration' (which see).
6052If CONFIGURATION was made from a frame that is now deleted,
6053only frame-independent values can be restored. In this case,
6054the return value is nil. Otherwise the value is t. */)
6055 (configuration)
2f83aebe 6056 Lisp_Object configuration;
7ab12479 6057{
7ab12479
JB
6058 register struct save_window_data *data;
6059 struct Lisp_Vector *saved_windows;
7ab12479 6060 Lisp_Object new_current_buffer;
fd482be5 6061 Lisp_Object frame;
44fa5b1e 6062 FRAME_PTR f;
72695e47 6063 int old_point = -1;
7ab12479 6064
663fbbba 6065 CHECK_WINDOW_CONFIGURATION (configuration);
7ab12479 6066
2f83aebe 6067 data = (struct save_window_data *) XVECTOR (configuration);
7ab12479
JB
6068 saved_windows = XVECTOR (data->saved_windows);
6069
7ab12479 6070 new_current_buffer = data->current_buffer;
265a9e55 6071 if (NILP (XBUFFER (new_current_buffer)->name))
7ab12479 6072 new_current_buffer = Qnil;
72695e47 6073 else
73cadfc1
DK
6074 {
6075 if (XBUFFER (new_current_buffer) == current_buffer)
e67a1dea
SM
6076 /* The code further down "preserves point" by saving here PT in
6077 old_point and then setting it later back into PT. When the
6078 current-selected-window and the final-selected-window both show
6079 the current buffer, this suffers from the problem that the
6080 current PT is the window-point of the current-selected-window,
6081 while the final PT is the point of the final-selected-window, so
6082 this copy from one PT to the other would end up moving the
6083 window-point of the final-selected-window to the window-point of
6084 the current-selected-window. So we have to be careful which
6085 point of the current-buffer we copy into old_point. */
6086 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6087 && WINDOWP (selected_window)
6088 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
6089 && !EQ (selected_window, data->current_window))
6090 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6091 else
6092 old_point = PT;
73cadfc1 6093 else
203eb0aa
SM
6094 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6095 point in new_current_buffer as of the last time this buffer was
6096 used. This can be non-deterministic since it can be changed by
6097 things like jit-lock by mere temporary selection of some random
6098 window that happens to show this buffer.
6099 So if possible we want this arbitrary choice of "which point" to
6100 be the one from the to-be-selected-window so as to prevent this
6101 window's cursor from being copied from another window. */
6102 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6103 /* If current_window = selected_window, its point is in BUF_PT. */
6104 && !EQ (selected_window, data->current_window))
6105 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6106 else
6107 old_point = BUF_PT (XBUFFER (new_current_buffer));
73cadfc1 6108 }
7ab12479 6109
fd482be5
JB
6110 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6111 f = XFRAME (frame);
177c0ea7 6112
fd482be5
JB
6113 /* If f is a dead frame, don't bother rebuilding its window tree.
6114 However, there is other stuff we should still try to do below. */
6115 if (FRAME_LIVE_P (f))
7ab12479 6116 {
fd482be5
JB
6117 register struct window *w;
6118 register struct saved_window *p;
5500c422
GM
6119 struct window *root_window;
6120 struct window **leaf_windows;
6121 int n_leaf_windows;
c4280705 6122 int k, i, n;
fd482be5
JB
6123
6124 /* If the frame has been resized since this window configuration was
6125 made, we change the frame to the size specified in the
6126 configuration, restore the configuration, and then resize it
6127 back. We keep track of the prevailing height in these variables. */
949cf20f
KS
6128 int previous_frame_lines = FRAME_LINES (f);
6129 int previous_frame_cols = FRAME_COLS (f);
8f6ea2e9 6130 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
9ea173e8 6131 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
fd482be5 6132
d2b35234
RS
6133 /* The mouse highlighting code could get screwed up
6134 if it runs during this. */
6135 BLOCK_INPUT;
6136
949cf20f
KS
6137 if (XFASTINT (data->frame_lines) != previous_frame_lines
6138 || XFASTINT (data->frame_cols) != previous_frame_cols)
6139 change_frame_size (f, XFASTINT (data->frame_lines),
6140 XFASTINT (data->frame_cols), 0, 0, 0);
e3678b64 6141#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9
KH
6142 if (XFASTINT (data->frame_menu_bar_lines)
6143 != previous_frame_menu_bar_lines)
f8ad443a 6144 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
4314246f 6145#ifdef HAVE_WINDOW_SYSTEM
9ea173e8
GM
6146 if (XFASTINT (data->frame_tool_bar_lines)
6147 != previous_frame_tool_bar_lines)
6148 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
4314246f 6149#endif
217f2871 6150#endif
fd482be5 6151
a46c0153
RS
6152 /* "Swap out" point from the selected window's buffer
6153 into the window itself. (Normally the pointm of the selected
6154 window holds garbage.) We do this now, before
719eaeb1
GM
6155 restoring the window contents, and prevent it from
6156 being done later on when we select a new window. */
596ae0cf
RS
6157 if (! NILP (XWINDOW (selected_window)->buffer))
6158 {
6159 w = XWINDOW (selected_window);
6160 set_marker_both (w->pointm,
6161 w->buffer,
6162 BUF_PT (XBUFFER (w->buffer)),
6163 BUF_PT_BYTE (XBUFFER (w->buffer)));
6164 }
6165
fd482be5 6166 windows_or_buffers_changed++;
29aeee73 6167 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
fd482be5 6168
5500c422 6169 /* Problem: Freeing all matrices and later allocating them again
177c0ea7 6170 is a serious redisplay flickering problem. What we would
5500c422
GM
6171 really like to do is to free only those matrices not reused
6172 below. */
6173 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6174 leaf_windows
6175 = (struct window **) alloca (count_windows (root_window)
6176 * sizeof (struct window *));
6177 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6178
756b6edc
RS
6179 /* Temporarily avoid any problems with windows that are smaller
6180 than they are supposed to be. */
6181 window_min_height = 1;
6182 window_min_width = 1;
6183
fd482be5
JB
6184 /* Kludge Alert!
6185 Mark all windows now on frame as "deleted".
6186 Restoring the new configuration "undeletes" any that are in it.
37962e60 6187
fd482be5
JB
6188 Save their current buffers in their height fields, since we may
6189 need it later, if a buffer saved in the configuration is now
6190 dead. */
6191 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6192
6193 for (k = 0; k < saved_windows->size; k++)
6194 {
6195 p = SAVED_WINDOW_N (saved_windows, k);
6196 w = XWINDOW (p->window);
6197 w->next = Qnil;
7ab12479 6198
fd482be5
JB
6199 if (!NILP (p->parent))
6200 w->parent = SAVED_WINDOW_N (saved_windows,
6201 XFASTINT (p->parent))->window;
6202 else
6203 w->parent = Qnil;
7ab12479 6204
fd482be5 6205 if (!NILP (p->prev))
7ab12479 6206 {
fd482be5
JB
6207 w->prev = SAVED_WINDOW_N (saved_windows,
6208 XFASTINT (p->prev))->window;
6209 XWINDOW (w->prev)->next = p->window;
6210 }
6211 else
6212 {
6213 w->prev = Qnil;
6214 if (!NILP (w->parent))
6215 {
949cf20f 6216 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
fd482be5
JB
6217 {
6218 XWINDOW (w->parent)->vchild = p->window;
6219 XWINDOW (w->parent)->hchild = Qnil;
6220 }
6221 else
6222 {
6223 XWINDOW (w->parent)->hchild = p->window;
6224 XWINDOW (w->parent)->vchild = Qnil;
6225 }
6226 }
6227 }
6228
6229 /* If we squirreled away the buffer in the window's height,
6230 restore it now. */
949cf20f
KS
6231 if (BUFFERP (w->total_lines))
6232 w->buffer = w->total_lines;
6233 w->left_col = p->left_col;
6234 w->top_line = p->top_line;
6235 w->total_cols = p->total_cols;
6236 w->total_lines = p->total_lines;
fd482be5 6237 w->hscroll = p->hscroll;
ea68264b 6238 w->min_hscroll = p->min_hscroll;
fd482be5 6239 w->display_table = p->display_table;
949cf20f
KS
6240 w->orig_top_line = p->orig_top_line;
6241 w->orig_total_lines = p->orig_total_lines;
6242 w->left_margin_cols = p->left_margin_cols;
6243 w->right_margin_cols = p->right_margin_cols;
6244 w->left_fringe_width = p->left_fringe_width;
6245 w->right_fringe_width = p->right_fringe_width;
6246 w->fringes_outside_margins = p->fringes_outside_margins;
6247 w->scroll_bar_width = p->scroll_bar_width;
6248 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6ad0381c 6249 w->dedicated = p->dedicated;
d834a2e9 6250 XSETFASTINT (w->last_modified, 0);
3cd21523 6251 XSETFASTINT (w->last_overlay_modified, 0);
fd482be5
JB
6252
6253 /* Reinstall the saved buffer and pointers into it. */
6254 if (NILP (p->buffer))
6255 w->buffer = p->buffer;
6256 else
6257 {
6258 if (!NILP (XBUFFER (p->buffer)->name))
6259 /* If saved buffer is alive, install it. */
6260 {
6261 w->buffer = p->buffer;
6262 w->start_at_line_beg = p->start_at_line_beg;
b73ea88e
RS
6263 set_marker_restricted (w->start, p->start, w->buffer);
6264 set_marker_restricted (w->pointm, p->pointm, w->buffer);
fd482be5 6265 Fset_marker (XBUFFER (w->buffer)->mark,
b73ea88e 6266 p->mark, w->buffer);
fd482be5
JB
6267
6268 /* As documented in Fcurrent_window_configuration, don't
243a5ce6
RS
6269 restore the location of point in the buffer which was
6270 current when the window configuration was recorded. */
6b54027b
RS
6271 if (!EQ (p->buffer, new_current_buffer)
6272 && XBUFFER (p->buffer) == current_buffer)
fd482be5
JB
6273 Fgoto_char (w->pointm);
6274 }
52a68e98
RS
6275 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6276 /* Else unless window has a live buffer, get one. */
7ab12479 6277 {
fd482be5
JB
6278 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6279 /* This will set the markers to beginning of visible
6280 range. */
6281 set_marker_restricted (w->start, make_number (0), w->buffer);
6282 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6283 w->start_at_line_beg = Qt;
7ab12479
JB
6284 }
6285 else
fd482be5 6286 /* Keeping window's old buffer; make sure the markers
52a68e98 6287 are real. */
7ab12479 6288 {
fd482be5
JB
6289 /* Set window markers at start of visible range. */
6290 if (XMARKER (w->start)->buffer == 0)
6291 set_marker_restricted (w->start, make_number (0),
6292 w->buffer);
6293 if (XMARKER (w->pointm)->buffer == 0)
b73ea88e
RS
6294 set_marker_restricted_both (w->pointm, w->buffer,
6295 BUF_PT (XBUFFER (w->buffer)),
6296 BUF_PT_BYTE (XBUFFER (w->buffer)));
fd482be5 6297 w->start_at_line_beg = Qt;
7ab12479
JB
6298 }
6299 }
6300 }
9ace597f 6301
fd482be5 6302 FRAME_ROOT_WINDOW (f) = data->root_window;
719eaeb1
GM
6303 /* Prevent "swapping out point" in the old selected window
6304 using the buffer that has been restored into it.
a46c0153 6305 We already swapped out point that from that window's old buffer. */
719eaeb1 6306 selected_window = Qnil;
a46c0153
RS
6307
6308 /* Arrange *not* to restore point in the buffer that was
6309 current when the window configuration was saved. */
243a5ce6
RS
6310 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6311 set_marker_restricted (XWINDOW (data->current_window)->pointm,
18787f5e 6312 make_number (old_point),
243a5ce6 6313 XWINDOW (data->current_window)->buffer);
177c0ea7 6314
14d87dc9 6315 Fselect_window (data->current_window, Qnil);
396a830c
RS
6316 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6317 = selected_window;
7ab12479 6318
db269683 6319 if (NILP (data->focus_frame)
017b2bad 6320 || (FRAMEP (data->focus_frame)
db269683
JB
6321 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6322 Fredirect_frame_focus (frame, data->focus_frame);
7ab12479 6323
fd482be5
JB
6324#if 0 /* I don't understand why this is needed, and it causes problems
6325 when the frame's old selected window has been deleted. */
e4e59717 6326 if (f != selected_frame && FRAME_WINDOW_P (f))
9a7c6fc3 6327 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
d12f6f83 6328 0, 0);
fd482be5
JB
6329#endif
6330
6331 /* Set the screen height to the value it had before this function. */
949cf20f
KS
6332 if (previous_frame_lines != FRAME_LINES (f)
6333 || previous_frame_cols != FRAME_COLS (f))
6334 change_frame_size (f, previous_frame_lines, previous_frame_cols,
2b653806 6335 0, 0, 0);
e3678b64 6336#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9 6337 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
f8ad443a
AS
6338 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6339 make_number (0));
4314246f 6340#ifdef HAVE_WINDOW_SYSTEM
9ea173e8
GM
6341 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6342 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6343 make_number (0));
4314246f 6344#endif
217f2871 6345#endif
d2b35234 6346
5500c422 6347 /* Now, free glyph matrices in windows that were not reused. */
c4280705
GM
6348 for (i = n = 0; i < n_leaf_windows; ++i)
6349 {
6350 if (NILP (leaf_windows[i]->buffer))
6351 {
6352 /* Assert it's not reused as a combination. */
177c0ea7 6353 xassert (NILP (leaf_windows[i]->hchild)
c4280705
GM
6354 && NILP (leaf_windows[i]->vchild));
6355 free_window_matrices (leaf_windows[i]);
c4280705
GM
6356 }
6357 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6358 ++n;
6359 }
177c0ea7 6360
5500c422
GM
6361 adjust_glyphs (f);
6362
d2b35234 6363 UNBLOCK_INPUT;
756b6edc 6364
478292ed
RS
6365 /* Fselect_window will have made f the selected frame, so we
6366 reselect the proper frame here. Fhandle_switch_frame will change the
6367 selected window too, but that doesn't make the call to
6368 Fselect_window above totally superfluous; it still sets f's
6369 selected window. */
6370 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
d12f6f83 6371 do_switch_frame (data->selected_frame, 0, 0);
478292ed
RS
6372
6373 if (! NILP (Vwindow_configuration_change_hook)
6374 && ! NILP (Vrun_hooks))
6375 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
6376 }
bdc727bf
JB
6377
6378 if (!NILP (new_current_buffer))
243a5ce6 6379 Fset_buffer (new_current_buffer);
bdc727bf 6380
478292ed
RS
6381 /* Restore the minimum heights recorded in the configuration. */
6382 window_min_height = XINT (data->min_height);
6383 window_min_width = XINT (data->min_width);
543f5fb1 6384
478292ed 6385 Vminibuf_scroll_window = data->minibuf_scroll_window;
3dbab091 6386 minibuf_selected_window = data->minibuf_selected_window;
543f5fb1 6387
3f8ab7bd 6388 return (FRAME_LIVE_P (f) ? Qt : Qnil);
7ab12479
JB
6389}
6390
44fa5b1e 6391/* Mark all windows now on frame as deleted
7ab12479
JB
6392 by setting their buffers to nil. */
6393
fd482be5 6394void
7ab12479
JB
6395delete_all_subwindows (w)
6396 register struct window *w;
6397{
265a9e55 6398 if (!NILP (w->next))
7ab12479 6399 delete_all_subwindows (XWINDOW (w->next));
265a9e55 6400 if (!NILP (w->vchild))
7ab12479 6401 delete_all_subwindows (XWINDOW (w->vchild));
265a9e55 6402 if (!NILP (w->hchild))
7ab12479 6403 delete_all_subwindows (XWINDOW (w->hchild));
605be8af 6404
949cf20f 6405 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
605be8af 6406
86e48436
RS
6407 if (!NILP (w->buffer))
6408 unshow_buffer (w);
6409
605be8af
JB
6410 /* We set all three of these fields to nil, to make sure that we can
6411 distinguish this dead window from any live window. Live leaf
6412 windows will have buffer set, and combination windows will have
6413 vchild or hchild set. */
6414 w->buffer = Qnil;
6415 w->vchild = Qnil;
6416 w->hchild = Qnil;
acf70840
GM
6417
6418 Vwindow_list = Qnil;
7ab12479
JB
6419}
6420\f
6421static int
6422count_windows (window)
6423 register struct window *window;
6424{
6425 register int count = 1;
265a9e55 6426 if (!NILP (window->next))
7ab12479 6427 count += count_windows (XWINDOW (window->next));
265a9e55 6428 if (!NILP (window->vchild))
7ab12479 6429 count += count_windows (XWINDOW (window->vchild));
265a9e55 6430 if (!NILP (window->hchild))
7ab12479
JB
6431 count += count_windows (XWINDOW (window->hchild));
6432 return count;
6433}
6434
5500c422 6435
177c0ea7 6436/* Fill vector FLAT with leaf windows under W, starting at index I.
5500c422
GM
6437 Value is last index + 1. */
6438
6439static int
6440get_leaf_windows (w, flat, i)
6441 struct window *w;
6442 struct window **flat;
6443 int i;
6444{
6445 while (w)
6446 {
6447 if (!NILP (w->hchild))
6448 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6449 else if (!NILP (w->vchild))
6450 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
177c0ea7 6451 else
5500c422
GM
6452 flat[i++] = w;
6453
6454 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6455 }
6456
6457 return i;
6458}
6459
6460
6461/* Return a pointer to the glyph W's physical cursor is on. Value is
6462 null if W's current matrix is invalid, so that no meaningfull glyph
6463 can be returned. */
6464
6465struct glyph *
6466get_phys_cursor_glyph (w)
6467 struct window *w;
6468{
6469 struct glyph_row *row;
6470 struct glyph *glyph;
6471
6472 if (w->phys_cursor.vpos >= 0
6473 && w->phys_cursor.vpos < w->current_matrix->nrows
6474 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6475 row->enabled_p)
6476 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6477 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6478 else
6479 glyph = NULL;
6480
6481 return glyph;
6482}
6483
6484
7ab12479
JB
6485static int
6486save_window_save (window, vector, i)
6487 Lisp_Object window;
6488 struct Lisp_Vector *vector;
6489 int i;
6490{
6491 register struct saved_window *p;
6492 register struct window *w;
6493 register Lisp_Object tem;
6494
265a9e55 6495 for (;!NILP (window); window = w->next)
7ab12479
JB
6496 {
6497 p = SAVED_WINDOW_N (vector, i);
6498 w = XWINDOW (window);
6499
2a1893f4 6500 XSETFASTINT (w->temslot, i); i++;
7ab12479
JB
6501 p->window = window;
6502 p->buffer = w->buffer;
949cf20f
KS
6503 p->left_col = w->left_col;
6504 p->top_line = w->top_line;
6505 p->total_cols = w->total_cols;
6506 p->total_lines = w->total_lines;
7ab12479 6507 p->hscroll = w->hscroll;
ea68264b 6508 p->min_hscroll = w->min_hscroll;
7ab12479 6509 p->display_table = w->display_table;
949cf20f
KS
6510 p->orig_top_line = w->orig_top_line;
6511 p->orig_total_lines = w->orig_total_lines;
6512 p->left_margin_cols = w->left_margin_cols;
6513 p->right_margin_cols = w->right_margin_cols;
6514 p->left_fringe_width = w->left_fringe_width;
6515 p->right_fringe_width = w->right_fringe_width;
6516 p->fringes_outside_margins = w->fringes_outside_margins;
6517 p->scroll_bar_width = w->scroll_bar_width;
6518 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6ad0381c 6519 p->dedicated = w->dedicated;
265a9e55 6520 if (!NILP (w->buffer))
7ab12479
JB
6521 {
6522 /* Save w's value of point in the window configuration.
6523 If w is the selected window, then get the value of point
6524 from the buffer; pointm is garbage in the selected window. */
6525 if (EQ (window, selected_window))
6526 {
6527 p->pointm = Fmake_marker ();
b73ea88e
RS
6528 set_marker_both (p->pointm, w->buffer,
6529 BUF_PT (XBUFFER (w->buffer)),
6530 BUF_PT_BYTE (XBUFFER (w->buffer)));
7ab12479
JB
6531 }
6532 else
eeb82665 6533 p->pointm = Fcopy_marker (w->pointm, Qnil);
7ab12479 6534
eeb82665 6535 p->start = Fcopy_marker (w->start, Qnil);
7ab12479
JB
6536 p->start_at_line_beg = w->start_at_line_beg;
6537
6538 tem = XBUFFER (w->buffer)->mark;
eeb82665 6539 p->mark = Fcopy_marker (tem, Qnil);
7ab12479
JB
6540 }
6541 else
6542 {
6543 p->pointm = Qnil;
6544 p->start = Qnil;
6545 p->mark = Qnil;
6546 p->start_at_line_beg = Qnil;
6547 }
6548
265a9e55 6549 if (NILP (w->parent))
7ab12479
JB
6550 p->parent = Qnil;
6551 else
6552 p->parent = XWINDOW (w->parent)->temslot;
6553
265a9e55 6554 if (NILP (w->prev))
7ab12479
JB
6555 p->prev = Qnil;
6556 else
6557 p->prev = XWINDOW (w->prev)->temslot;
6558
265a9e55 6559 if (!NILP (w->vchild))
7ab12479 6560 i = save_window_save (w->vchild, vector, i);
265a9e55 6561 if (!NILP (w->hchild))
7ab12479
JB
6562 i = save_window_save (w->hchild, vector, i);
6563 }
6564
6565 return i;
6566}
6567
a0d76c27 6568DEFUN ("current-window-configuration", Fcurrent_window_configuration,
fdb82f93
PJ
6569 Scurrent_window_configuration, 0, 1, 0,
6570 doc: /* Return an object representing the current window configuration of FRAME.
6571If FRAME is nil or omitted, use the selected frame.
6572This describes the number of windows, their sizes and current buffers,
6573and for each displayed buffer, where display starts, and the positions of
6574point and mark. An exception is made for point in the current buffer:
6575its value is -not- saved.
6576This also records the currently selected frame, and FRAME's focus
6577redirection (see `redirect-frame-focus'). */)
6578 (frame)
44fa5b1e 6579 Lisp_Object frame;
7ab12479
JB
6580{
6581 register Lisp_Object tem;
6582 register int n_windows;
6583 register struct save_window_data *data;
da2792e0 6584 register struct Lisp_Vector *vec;
7ab12479 6585 register int i;
44fa5b1e 6586 FRAME_PTR f;
43bad991 6587
44fa5b1e 6588 if (NILP (frame))
1ae1a37d 6589 frame = selected_frame;
b7826503 6590 CHECK_LIVE_FRAME (frame);
1ae1a37d 6591 f = XFRAME (frame);
7ab12479 6592
44fa5b1e 6593 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
26605be9 6594 vec = allocate_other_vector (VECSIZE (struct save_window_data));
da2792e0
KH
6595 data = (struct save_window_data *)vec;
6596
949cf20f
KS
6597 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
6598 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
d834a2e9 6599 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
9ea173e8 6600 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
1ae1a37d 6601 data->selected_frame = selected_frame;
44fa5b1e 6602 data->current_window = FRAME_SELECTED_WINDOW (f);
74112613 6603 XSETBUFFER (data->current_buffer, current_buffer);
3f49fddc 6604 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
3dbab091 6605 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
44fa5b1e 6606 data->root_window = FRAME_ROOT_WINDOW (f);
bdc727bf 6607 data->focus_frame = FRAME_FOCUS_FRAME (f);
74112613
KH
6608 XSETINT (data->min_height, window_min_height);
6609 XSETINT (data->min_width, window_min_width);
7ab12479
JB
6610 tem = Fmake_vector (make_number (n_windows), Qnil);
6611 data->saved_windows = tem;
6612 for (i = 0; i < n_windows; i++)
6613 XVECTOR (tem)->contents[i]
8a450f0a 6614 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
a1d58e5b 6615 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
74112613 6616 XSETWINDOW_CONFIGURATION (tem, data);
7ab12479
JB
6617 return (tem);
6618}
6619
6620DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
fdb82f93 6621 0, UNEVALLED, 0,
a0a37a6f
LT
6622 doc: /* Execute BODY, preserving window sizes and contents.
6623Return the value of the last form in BODY.
fdb82f93
PJ
6624Restore which buffer appears in which window, where display starts,
6625and the value of point and mark for each window.
6626Also restore the choice of selected window.
6627Also restore which buffer is current.
6924dda6
MB
6628Does not restore the value of point in current buffer.
6629usage: (save-window-excursion BODY ...) */)
fdb82f93 6630 (args)
7ab12479
JB
6631 Lisp_Object args;
6632{
6633 register Lisp_Object val;
aed13378 6634 register int count = SPECPDL_INDEX ();
7ab12479
JB
6635
6636 record_unwind_protect (Fset_window_configuration,
43bad991 6637 Fcurrent_window_configuration (Qnil));
7ab12479
JB
6638 val = Fprogn (args);
6639 return unbind_to (count, val);
6640}
5500c422 6641
94e16dd5
KS
6642
6643\f
6644/***********************************************************************
6645 Window Split Tree
6646 ***********************************************************************/
6647
6648static Lisp_Object
c16e1cc3 6649window_tree (w)
94e16dd5
KS
6650 struct window *w;
6651{
6652 Lisp_Object tail = Qnil;
6653 Lisp_Object result = Qnil;
6654
6655 while (w)
6656 {
6657 Lisp_Object wn;
6658
6659 XSETWINDOW (wn, w);
6660 if (!NILP (w->hchild))
6661 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
c16e1cc3 6662 window_tree (XWINDOW (w->hchild))));
94e16dd5
KS
6663 else if (!NILP (w->vchild))
6664 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
c16e1cc3 6665 window_tree (XWINDOW (w->vchild))));
94e16dd5
KS
6666
6667 if (NILP (result))
6668 {
6669 result = tail = Fcons (wn, Qnil);
6670 }
6671 else
6672 {
6673 XSETCDR (tail, Fcons (wn, Qnil));
6674 tail = XCDR (tail);
6675 }
6676
6677 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6678 }
6679
6680 return result;
6681}
6682
6683
6684
c16e1cc3 6685DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
94e16dd5 6686 0, 1, 0,
c16e1cc3 6687 doc: /* Return the window tree for frame FRAME.
94e16dd5
KS
6688
6689The return value is a list of the form (ROOT MINI), where ROOT
c16e1cc3 6690represents the window tree of the frame's root window, and MINI
94e16dd5
KS
6691is the frame's minibuffer window.
6692
6693If the root window is not split, ROOT is the root window itself.
6694Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
008171a4 6695horizontal split, and t for a vertical split, EDGES gives the combined
94e16dd5
KS
6696size and position of the subwindows in the split, and the rest of the
6697elements are the subwindows in the split. Each of the subwindows may
6698again be a window or a list representing a window split, and so on.
6699EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6700
6701If FRAME is nil or omitted, return information on the currently
6702selected frame. */)
6703 (frame)
6704 Lisp_Object frame;
6705{
94e16dd5
KS
6706 FRAME_PTR f;
6707
6708 if (NILP (frame))
6709 frame = selected_frame;
6710
6711 CHECK_FRAME (frame);
6712 f = XFRAME (frame);
6713
6714 if (!FRAME_LIVE_P (f))
6715 return Qnil;
6716
c16e1cc3 6717 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
94e16dd5
KS
6718}
6719
5500c422
GM
6720\f
6721/***********************************************************************
6722 Marginal Areas
6723 ***********************************************************************/
6724
6725DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
a7bdfc08 6726 2, 3, 0,
fdb82f93 6727 doc: /* Set width of marginal areas of window WINDOW.
e661376d
KS
6728If WINDOW is nil, set margins of the currently selected window.
6729Second arg LEFT-WIDTH specifies the number of character cells to
6730reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6731does the same for the right marginal area. A nil width parameter
6732means no margin. */)
c10ce82e
JB
6733 (window, left_width, right_width)
6734 Lisp_Object window, left_width, right_width;
5500c422
GM
6735{
6736 struct window *w = decode_window (window);
5500c422 6737
c2c3f202
KS
6738 /* Translate negative or zero widths to nil.
6739 Margins that are too wide have to be checked elsewhere. */
6740
c10ce82e 6741 if (!NILP (left_width))
c2c3f202 6742 {
c10ce82e
JB
6743 CHECK_NUMBER (left_width);
6744 if (XINT (left_width) <= 0)
6745 left_width = Qnil;
c2c3f202 6746 }
5500c422 6747
c10ce82e 6748 if (!NILP (right_width))
c2c3f202 6749 {
c10ce82e
JB
6750 CHECK_NUMBER (right_width);
6751 if (XINT (right_width) <= 0)
6752 right_width = Qnil;
c2c3f202 6753 }
5500c422 6754
c10ce82e
JB
6755 if (!EQ (w->left_margin_cols, left_width)
6756 || !EQ (w->right_margin_cols, right_width))
949cf20f 6757 {
c10ce82e
JB
6758 w->left_margin_cols = left_width;
6759 w->right_margin_cols = right_width;
949cf20f
KS
6760
6761 adjust_window_margins (w);
6762
6763 ++windows_or_buffers_changed;
6764 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6765 }
5500c422 6766
5500c422
GM
6767 return Qnil;
6768}
6769
6770
6771DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6772 0, 1, 0,
fdb82f93
PJ
6773 doc: /* Get width of marginal areas of window WINDOW.
6774If WINDOW is omitted or nil, use the currently selected window.
6775Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6776If a marginal area does not exist, its width will be returned
6777as nil. */)
6778 (window)
5500c422
GM
6779 Lisp_Object window;
6780{
6781 struct window *w = decode_window (window);
949cf20f
KS
6782 return Fcons (w->left_margin_cols, w->right_margin_cols);
6783}
6784
6785
6786\f
6787/***********************************************************************
6788 Fringes
6789 ***********************************************************************/
6790
6791DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6792 2, 4, 0,
62c2f759 6793 doc: /* Set the fringe widths of window WINDOW.
62c2f759
LK
6794If WINDOW is nil, set the fringe widths of the currently selected
6795window.
e661376d
KS
6796Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6797the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6798fringe width. If a fringe width arg is nil, that means to use the
6799frame's default fringe width. Default fringe widths can be set with
6800the command `set-fringe-style'.
6801If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
62c2f759
LK
6802outside of the display margins. By default, fringes are drawn between
6803display marginal areas and the text area. */)
c10ce82e
JB
6804 (window, left_width, right_width, outside_margins)
6805 Lisp_Object window, left_width, right_width, outside_margins;
949cf20f
KS
6806{
6807 struct window *w = decode_window (window);
6808
c10ce82e
JB
6809 if (!NILP (left_width))
6810 CHECK_NATNUM (left_width);
6811 if (!NILP (right_width))
6812 CHECK_NATNUM (right_width);
5a857365 6813
017e297e 6814 /* Do nothing on a tty. */
d46c6df5
NR
6815 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6816 && (!EQ (w->left_fringe_width, left_width)
6817 || !EQ (w->right_fringe_width, right_width)
6818 || !EQ (w->fringes_outside_margins, outside_margins)))
949cf20f 6819 {
c10ce82e
JB
6820 w->left_fringe_width = left_width;
6821 w->right_fringe_width = right_width;
949cf20f
KS
6822 w->fringes_outside_margins = outside_margins;
6823
6824 adjust_window_margins (w);
6825
6826 clear_glyph_matrix (w->current_matrix);
6827 w->window_end_valid = Qnil;
6828
6829 ++windows_or_buffers_changed;
6830 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6831 }
6832
6833 return Qnil;
6834}
6835
6836
6837DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6838 0, 1, 0,
6839 doc: /* Get width of fringes of window WINDOW.
6840If WINDOW is omitted or nil, use the currently selected window.
467e281a 6841Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
949cf20f
KS
6842 (window)
6843 Lisp_Object window;
6844{
6845 struct window *w = decode_window (window);
d46c6df5 6846
949cf20f
KS
6847 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6848 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
d46c6df5
NR
6849 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6850 ? Qt : Qnil), Qnil)));
949cf20f
KS
6851}
6852
6853
6854\f
6855/***********************************************************************
6856 Scroll bars
6857 ***********************************************************************/
6858
6859DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6860 2, 4, 0,
6861 doc: /* Set width and type of scroll bars of window WINDOW.
6862If window is nil, set scroll bars of the currently selected window.
6863Second parameter WIDTH specifies the pixel width for the scroll bar;
6864this is automatically adjusted to a multiple of the frame column width.
6865Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6866bar: left, right, or nil.
2f71b5ea 6867If WIDTH is nil, use the frame's scroll-bar width.
a53d44a8
JB
6868If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6869Fourth parameter HORIZONTAL-TYPE is currently unused. */)
79fd290e
JB
6870 (window, width, vertical_type, horizontal_type)
6871 Lisp_Object window, width, vertical_type, horizontal_type;
949cf20f
KS
6872{
6873 struct window *w = decode_window (window);
6874
6875 if (!NILP (width))
0f8fe9a2
SM
6876 {
6877 CHECK_NATNUM (width);
949cf20f 6878
0f8fe9a2
SM
6879 if (XINT (width) == 0)
6880 vertical_type = Qnil;
6881 }
949cf20f 6882
2f71b5ea 6883 if (!(EQ (vertical_type, Qnil)
0cc1039f 6884 || EQ (vertical_type, Qleft)
2f71b5ea
MY
6885 || EQ (vertical_type, Qright)
6886 || EQ (vertical_type, Qt)))
6887 error ("Invalid type of vertical scroll bar");
6888
949cf20f
KS
6889 if (!EQ (w->scroll_bar_width, width)
6890 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6891 {
6892 w->scroll_bar_width = width;
6893 w->vertical_scroll_bar_type = vertical_type;
6894
6895 adjust_window_margins (w);
6896
6897 clear_glyph_matrix (w->current_matrix);
6898 w->window_end_valid = Qnil;
6899
6900 ++windows_or_buffers_changed;
6901 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6902 }
6903
6904 return Qnil;
6905}
6906
6907
6908DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6909 0, 1, 0,
6910 doc: /* Get width and type of scroll bars of window WINDOW.
6911If WINDOW is omitted or nil, use the currently selected window.
12853c58
KS
6912Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6913If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6914value. */)
949cf20f
KS
6915 (window)
6916 Lisp_Object window;
6917{
6918 struct window *w = decode_window (window);
6919 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6920 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6921 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6922 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6923 Fcons (w->vertical_scroll_bar_type,
6924 Fcons (Qnil, Qnil))));
5500c422
GM
6925}
6926
6927
7ab12479 6928\f
5500c422
GM
6929/***********************************************************************
6930 Smooth scrolling
6931 ***********************************************************************/
6932
0cc1039f 6933DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
fdb82f93
PJ
6934 doc: /* Return the amount by which WINDOW is scrolled vertically.
6935Use the selected window if WINDOW is nil or omitted.
0cc1039f 6936Normally, value is a multiple of the canonical character height of WINDOW;
8cd0d661 6937optional second arg PIXELS-P means value is measured in pixels. */)
0cc1039f
KS
6938 (window, pixels_p)
6939 Lisp_Object window, pixels_p;
5500c422 6940{
47004952 6941 Lisp_Object result;
5500c422
GM
6942 struct frame *f;
6943 struct window *w;
177c0ea7 6944
5500c422
GM
6945 if (NILP (window))
6946 window = selected_window;
47004952 6947 else
b7826503 6948 CHECK_WINDOW (window);
5500c422
GM
6949 w = XWINDOW (window);
6950 f = XFRAME (w->frame);
177c0ea7 6951
5500c422 6952 if (FRAME_WINDOW_P (f))
0cc1039f
KS
6953 result = (NILP (pixels_p)
6954 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6955 : make_number (-w->vscroll));
5500c422 6956 else
47004952
GM
6957 result = make_number (0);
6958 return result;
5500c422
GM
6959}
6960
6961
6962DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
0cc1039f 6963 2, 3, 0,
fdb82f93 6964 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
0cc1039f
KS
6965WINDOW nil means use the selected window. Normally, VSCROLL is a
6966non-negative multiple of the canonical character height of WINDOW;
8cd0d661 6967optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
a0a37a6f
LT
6968If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6969corresponds to an integral number of pixels. The return value is the
6970result of this rounding.
6971If PIXELS-P is non-nil, the return value is VSCROLL. */)
0cc1039f
KS
6972 (window, vscroll, pixels_p)
6973 Lisp_Object window, vscroll, pixels_p;
5500c422
GM
6974{
6975 struct window *w;
6976 struct frame *f;
177c0ea7 6977
5500c422
GM
6978 if (NILP (window))
6979 window = selected_window;
47004952 6980 else
b7826503
PJ
6981 CHECK_WINDOW (window);
6982 CHECK_NUMBER_OR_FLOAT (vscroll);
177c0ea7 6983
5500c422
GM
6984 w = XWINDOW (window);
6985 f = XFRAME (w->frame);
6986
6987 if (FRAME_WINDOW_P (f))
6988 {
6989 int old_dy = w->vscroll;
177c0ea7 6990
0cc1039f
KS
6991 w->vscroll = - (NILP (pixels_p)
6992 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6993 : XFLOATINT (vscroll));
47004952 6994 w->vscroll = min (w->vscroll, 0);
5500c422 6995
e56263e5
KS
6996 if (w->vscroll != old_dy)
6997 {
6998 /* Adjust glyph matrix of the frame if the virtual display
6999 area becomes larger than before. */
7000 if (w->vscroll < 0 && w->vscroll < old_dy)
7001 adjust_glyphs (f);
177c0ea7 7002
e56263e5
KS
7003 /* Prevent redisplay shortcuts. */
7004 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7005 }
5500c422 7006 }
177c0ea7 7007
0cc1039f 7008 return Fwindow_vscroll (window, pixels_p);
5500c422 7009}
177c0ea7 7010
7bbb5782
GM
7011\f
7012/* Call FN for all leaf windows on frame F. FN is called with the
7013 first argument being a pointer to the leaf window, and with
f95464e4 7014 additional argument USER_DATA. Stops when FN returns 0. */
7bbb5782
GM
7015
7016void
f95464e4 7017foreach_window (f, fn, user_data)
7bbb5782 7018 struct frame *f;
f95464e4
GM
7019 int (* fn) P_ ((struct window *, void *));
7020 void *user_data;
7bbb5782 7021{
76fb556f
YM
7022 /* Fdelete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7023 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7024 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7bbb5782
GM
7025}
7026
7027
7028/* Helper function for foreach_window. Call FN for all leaf windows
7029 reachable from W. FN is called with the first argument being a
f95464e4 7030 pointer to the leaf window, and with additional argument USER_DATA.
67492200 7031 Stop when FN returns 0. Value is 0 if stopped by FN. */
7bbb5782 7032
67492200 7033static int
f95464e4 7034foreach_window_1 (w, fn, user_data)
7bbb5782 7035 struct window *w;
f95464e4
GM
7036 int (* fn) P_ ((struct window *, void *));
7037 void *user_data;
7bbb5782 7038{
67492200 7039 int cont;
177c0ea7 7040
67492200 7041 for (cont = 1; w && cont;)
7bbb5782
GM
7042 {
7043 if (!NILP (w->hchild))
f95464e4 7044 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
7bbb5782 7045 else if (!NILP (w->vchild))
f95464e4 7046 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
177c0ea7 7047 else
0f532a9a 7048 cont = fn (w, user_data);
177c0ea7 7049
7bbb5782
GM
7050 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7051 }
67492200
GM
7052
7053 return cont;
7bbb5782
GM
7054}
7055
7056
6d194a45 7057/* Freeze or unfreeze the window start of W unless it is a
f95464e4 7058 mini-window or the selected window. FREEZE_P non-null means freeze
7bbb5782
GM
7059 the window start. */
7060
67492200 7061static int
7bbb5782
GM
7062freeze_window_start (w, freeze_p)
7063 struct window *w;
f95464e4 7064 void *freeze_p;
7bbb5782
GM
7065{
7066 if (w == XWINDOW (selected_window)
7067 || MINI_WINDOW_P (w)
7068 || (MINI_WINDOW_P (XWINDOW (selected_window))
24d744ac 7069 && ! NILP (Vminibuf_scroll_window)
7bbb5782 7070 && w == XWINDOW (Vminibuf_scroll_window)))
f95464e4 7071 freeze_p = NULL;
177c0ea7 7072
f95464e4 7073 w->frozen_window_start_p = freeze_p != NULL;
67492200 7074 return 1;
7bbb5782
GM
7075}
7076
7077
7078/* Freeze or unfreeze the window starts of all leaf windows on frame
7079 F, except the selected window and a mini-window. FREEZE_P non-zero
7080 means freeze the window start. */
7081
7082void
7083freeze_window_starts (f, freeze_p)
7084 struct frame *f;
7085 int freeze_p;
7086{
cbccabec 7087 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7bbb5782 7088}
5500c422
GM
7089
7090\f
7091/***********************************************************************
7092 Initialization
7093 ***********************************************************************/
7094
cbff28e8
RS
7095/* Return 1 if window configurations C1 and C2
7096 describe the same state of affairs. This is used by Fequal. */
7097
7098int
2f8274be 7099compare_window_configurations (c1, c2, ignore_positions)
cbff28e8 7100 Lisp_Object c1, c2;
2f8274be 7101 int ignore_positions;
cbff28e8
RS
7102{
7103 register struct save_window_data *d1, *d2;
7104 struct Lisp_Vector *sw1, *sw2;
7105 int i;
7106
663fbbba
KS
7107 CHECK_WINDOW_CONFIGURATION (c1);
7108 CHECK_WINDOW_CONFIGURATION (c2);
177c0ea7 7109
cbff28e8
RS
7110 d1 = (struct save_window_data *) XVECTOR (c1);
7111 d2 = (struct save_window_data *) XVECTOR (c2);
7112 sw1 = XVECTOR (d1->saved_windows);
7113 sw2 = XVECTOR (d2->saved_windows);
7114
949cf20f 7115 if (! EQ (d1->frame_cols, d2->frame_cols))
cbff28e8 7116 return 0;
949cf20f 7117 if (! EQ (d1->frame_lines, d2->frame_lines))
cbff28e8
RS
7118 return 0;
7119 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
7120 return 0;
7121 if (! EQ (d1->selected_frame, d2->selected_frame))
7122 return 0;
7123 /* Don't compare the current_window field directly.
7124 Instead see w1_is_current and w2_is_current, below. */
7125 if (! EQ (d1->current_buffer, d2->current_buffer))
7126 return 0;
2f8274be 7127 if (! ignore_positions)
3f49fddc
KS
7128 {
7129 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
7130 return 0;
7131 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
7132 return 0;
7133 }
cbff28e8
RS
7134 /* Don't compare the root_window field.
7135 We don't require the two configurations
7136 to use the same window object,
7137 and the two root windows must be equivalent
7138 if everything else compares equal. */
7139 if (! EQ (d1->focus_frame, d2->focus_frame))
7140 return 0;
7141 if (! EQ (d1->min_width, d2->min_width))
7142 return 0;
7143 if (! EQ (d1->min_height, d2->min_height))
7144 return 0;
7145
7146 /* Verify that the two confis have the same number of windows. */
7147 if (sw1->size != sw2->size)
7148 return 0;
7149
7150 for (i = 0; i < sw1->size; i++)
7151 {
7152 struct saved_window *p1, *p2;
7153 int w1_is_current, w2_is_current;
7154
7155 p1 = SAVED_WINDOW_N (sw1, i);
7156 p2 = SAVED_WINDOW_N (sw2, i);
7157
7158 /* Verify that the current windows in the two
7159 configurations correspond to each other. */
7160 w1_is_current = EQ (d1->current_window, p1->window);
7161 w2_is_current = EQ (d2->current_window, p2->window);
7162
7163 if (w1_is_current != w2_is_current)
7164 return 0;
7165
7166 /* Verify that the corresponding windows do match. */
7167 if (! EQ (p1->buffer, p2->buffer))
7168 return 0;
949cf20f 7169 if (! EQ (p1->left_col, p2->left_col))
cbff28e8 7170 return 0;
949cf20f 7171 if (! EQ (p1->top_line, p2->top_line))
cbff28e8 7172 return 0;
949cf20f 7173 if (! EQ (p1->total_cols, p2->total_cols))
cbff28e8 7174 return 0;
949cf20f 7175 if (! EQ (p1->total_lines, p2->total_lines))
cbff28e8 7176 return 0;
cbff28e8
RS
7177 if (! EQ (p1->display_table, p2->display_table))
7178 return 0;
7179 if (! EQ (p1->parent, p2->parent))
7180 return 0;
7181 if (! EQ (p1->prev, p2->prev))
7182 return 0;
2f8274be
RS
7183 if (! ignore_positions)
7184 {
7185 if (! EQ (p1->hscroll, p2->hscroll))
7186 return 0;
ea68264b
GM
7187 if (!EQ (p1->min_hscroll, p2->min_hscroll))
7188 return 0;
2f8274be
RS
7189 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
7190 return 0;
7191 if (NILP (Fequal (p1->start, p2->start)))
7192 return 0;
7193 if (NILP (Fequal (p1->pointm, p2->pointm)))
7194 return 0;
7195 if (NILP (Fequal (p1->mark, p2->mark)))
7196 return 0;
7197 }
949cf20f
KS
7198 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
7199 return 0;
7200 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
7201 return 0;
7202 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
7203 return 0;
7204 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
7205 return 0;
7206 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
7207 return 0;
7208 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
7209 return 0;
7210 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
7211 return 0;
cbff28e8
RS
7212 }
7213
7214 return 1;
7215}
2f8274be
RS
7216
7217DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7218 Scompare_window_configurations, 2, 2, 0,
fdb82f93
PJ
7219 doc: /* Compare two window configurations as regards the structure of windows.
7220This function ignores details such as the values of point and mark
7221and scrolling positions. */)
7222 (x, y)
2f8274be
RS
7223 Lisp_Object x, y;
7224{
7225 if (compare_window_configurations (x, y, 1))
7226 return Qt;
7227 return Qnil;
7228}
cbff28e8 7229\f
dfcf069d 7230void
7ab12479
JB
7231init_window_once ()
7232{
1ae1a37d
GM
7233 struct frame *f = make_terminal_frame ();
7234 XSETFRAME (selected_frame, f);
7235 Vterminal_frame = selected_frame;
7236 minibuf_window = f->minibuffer_window;
7237 selected_window = f->selected_window;
7238 last_nonminibuf_frame = f;
5b03d3c0
RS
7239
7240 window_initialized = 1;
7ab12479
JB
7241}
7242
67492200
GM
7243void
7244init_window ()
7245{
7246 Vwindow_list = Qnil;
7247}
7248
dfcf069d 7249void
7ab12479
JB
7250syms_of_window ()
7251{
c525d842
CY
7252 Qscroll_up = intern ("scroll-up");
7253 staticpro (&Qscroll_up);
7254
7255 Qscroll_down = intern ("scroll-down");
7256 staticpro (&Qscroll_down);
7257
8a37516b
GM
7258 Qwindow_size_fixed = intern ("window-size-fixed");
7259 staticpro (&Qwindow_size_fixed);
c0e7ccd3 7260 Fset (Qwindow_size_fixed, Qnil);
177c0ea7 7261
543f5fb1
RS
7262 staticpro (&Qwindow_configuration_change_hook);
7263 Qwindow_configuration_change_hook
7264 = intern ("window-configuration-change-hook");
7265
7ab12479
JB
7266 Qwindowp = intern ("windowp");
7267 staticpro (&Qwindowp);
7268
3f8ab7bd
RS
7269 Qwindow_configuration_p = intern ("window-configuration-p");
7270 staticpro (&Qwindow_configuration_p);
7271
806b4d9b
JB
7272 Qwindow_live_p = intern ("window-live-p");
7273 staticpro (&Qwindow_live_p);
605be8af 7274
2cccc823 7275 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
a58ec57d
RS
7276 staticpro (&Qtemp_buffer_show_hook);
7277
67492200 7278 staticpro (&Vwindow_list);
8bfb170b
KS
7279
7280 minibuf_selected_window = Qnil;
3dbab091 7281 staticpro (&minibuf_selected_window);
67492200 7282
66fe93d1 7283 window_scroll_pixel_based_preserve_y = -1;
66d43aea 7284
7ab12479 7285 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
fdb82f93
PJ
7286 doc: /* Non-nil means call as function to display a help buffer.
7287The function is called with one argument, the buffer to be displayed.
7288Used by `with-output-to-temp-buffer'.
7289If this function is used, then it must do the entire job of showing
7290the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7ab12479
JB
7291 Vtemp_buffer_show_function = Qnil;
7292
7293 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
fdb82f93
PJ
7294 doc: /* If non-nil, function to call to handle `display-buffer'.
7295It will receive two args, the buffer and a flag which if non-nil means
c2755926
LT
7296that the currently selected window is not acceptable.
7297It should choose or create a window, display the specified buffer in it,
7298and return the window.
fdb82f93
PJ
7299Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
7300work using this function. */);
7ab12479
JB
7301 Vdisplay_buffer_function = Qnil;
7302
6529ed87 7303 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
fdb82f93
PJ
7304 doc: /* *If non-nil, `display-buffer' should even the window heights.
7305If nil, `display-buffer' will leave the window configuration alone. */);
6529ed87
GM
7306 Veven_window_heights = Qt;
7307
7ab12479 7308 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
fdb82f93 7309 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7ab12479
JB
7310 Vminibuf_scroll_window = Qnil;
7311
cc91894c 7312 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
26124d5e 7313 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
cc91894c
KS
7314If the minibuffer is active, the `minibuffer-scroll-window' mode line
7315is displayed in the `mode-line' face. */);
7316 mode_line_in_non_selected_windows = 1;
26124d5e 7317
7ab12479 7318 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
fdb82f93 7319 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7ab12479
JB
7320 Vother_window_scroll_buffer = Qnil;
7321
44fa5b1e 7322 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
fdb82f93 7323 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
44fa5b1e 7324 pop_up_frames = 0;
7ab12479 7325
e56263e5
KS
7326 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7327 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7328 auto_window_vscroll_p = 1;
7329
9c3da604 7330 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
fdb82f93
PJ
7331 doc: /* *Non-nil means `display-buffer' should reuse frames.
7332If the buffer in question is already displayed in a frame, raise that frame. */);
9c3da604
GM
7333 display_buffer_reuse_frames = 0;
7334
44fa5b1e 7335 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
fdb82f93
PJ
7336 doc: /* Function to call to handle automatic new frame creation.
7337It is called with no arguments and should return a newly created frame.
7338
7339A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
7340where `pop-up-frame-alist' would hold the default frame parameters. */);
44fa5b1e 7341 Vpop_up_frame_function = Qnil;
7ab12479 7342
a90712c2 7343 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
fdb82f93 7344 doc: /* *List of buffer names that should have their own special frames.
c019856e
RS
7345Displaying a buffer with `display-buffer' or `pop-to-buffer',
7346if its name is in this list, makes a special frame for it
fdb82f93
PJ
7347using `special-display-function'. See also `special-display-regexps'.
7348
7349An element of the list can be a list instead of just a string.
7350There are two ways to use a list as an element:
7351 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
3cb7143c
RS
7352In the first case, the FRAME-PARAMETERS are pairs of the form
7353\(PARAMETER . VALUE); these parameter values are used to create the frame.
7354In the second case, FUNCTION is called with BUFFER as the first argument,
7355followed by the OTHER-ARGS--it can display BUFFER in any way it likes.
fdb82f93
PJ
7356All this is done by the function found in `special-display-function'.
7357
499858c5
RS
7358If the specified frame parameters include (same-buffer . t), the
7359buffer is displayed in the currently selected window. Otherwise, if
7360they include (same-frame . t), the buffer is displayed in a new window
7361in the currently selected frame.
7362
fdb82f93
PJ
7363If this variable appears \"not to work\", because you add a name to it
7364but that buffer still appears in the selected window, look at the
7365values of `same-window-buffer-names' and `same-window-regexps'.
7366Those variables take precedence over this one. */);
a90712c2
RS
7367 Vspecial_display_buffer_names = Qnil;
7368
7369 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
fdb82f93 7370 doc: /* *List of regexps saying which buffers should have their own special frames.
c019856e
RS
7371When displaying a buffer with `display-buffer' or `pop-to-buffer',
7372if any regexp in this list matches the buffer name, it makes a
7373special frame for the buffer by calling `special-display-function'.
fdb82f93
PJ
7374
7375An element of the list can be a list instead of just a string.
7376There are two ways to use a list as an element:
7377 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
3cb7143c
RS
7378In the first case, the FRAME-PARAMETERS are pairs of the form
7379\(PARAMETER . VALUE); these parameter values are used to create the frame.
7380In the second case, FUNCTION is called with BUFFER as the first argument,
7381followed by the OTHER-ARGS--it can display the buffer in any way it likes.
fdb82f93
PJ
7382All this is done by the function found in `special-display-function'.
7383
499858c5
RS
7384If the specified frame parameters include (same-buffer . t), the
7385buffer is displayed in the currently selected window. Otherwise, if
7386they include (same-frame . t), the buffer is displayed in a new window
7387in the currently selected frame.
7388
fdb82f93
PJ
7389If this variable appears \"not to work\", because you add a regexp to it
7390but the matching buffers still appear in the selected window, look at the
7391values of `same-window-buffer-names' and `same-window-regexps'.
7392Those variables take precedence over this one. */);
a90712c2
RS
7393 Vspecial_display_regexps = Qnil;
7394
7395 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
fdb82f93
PJ
7396 doc: /* Function to call to make a new frame for a special buffer.
7397It is called with two arguments, the buffer and optional buffer specific
7398data, and should return a window displaying that buffer.
a5731348
SM
7399The default value normally makes a separate frame for the buffer,
7400 using `special-display-frame-alist' to specify the frame parameters.
7401But if the buffer specific data includes (same-buffer . t) then the
7402 buffer is displayed in the current selected window.
7403Otherwise if it includes (same-frame . t) then the buffer is displayed in
7404 a new window in the currently selected frame.
7405
7406A buffer is special if it is listed in `special-display-buffer-names'
fdb82f93 7407or matches a regexp in `special-display-regexps'. */);
a90712c2
RS
7408 Vspecial_display_function = Qnil;
7409
855d8627 7410 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
fdb82f93
PJ
7411 doc: /* *List of buffer names that should appear in the selected window.
7412Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
7413switches to it in the selected window, rather than making it appear
7414in some other window.
7415
7416An element of the list can be a cons cell instead of just a string.
7417Then the car must be a string, which specifies the buffer name.
7418This is for compatibility with `special-display-buffer-names';
7419the cdr of the cons cell is ignored.
7420
7421See also `same-window-regexps'. */);
855d8627
RS
7422 Vsame_window_buffer_names = Qnil;
7423
7424 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
fdb82f93
PJ
7425 doc: /* *List of regexps saying which buffers should appear in the selected window.
7426If a buffer name matches one of these regexps, then displaying it
7427using `display-buffer' or `pop-to-buffer' switches to it
7428in the selected window, rather than making it appear in some other window.
7429
7430An element of the list can be a cons cell instead of just a string.
7431Then the car must be a string, which specifies the buffer name.
7432This is for compatibility with `special-display-buffer-names';
7433the cdr of the cons cell is ignored.
7434
7435See also `same-window-buffer-names'. */);
855d8627
RS
7436 Vsame_window_regexps = Qnil;
7437
7ab12479 7438 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
fdb82f93 7439 doc: /* *Non-nil means display-buffer should make new windows. */);
7ab12479
JB
7440 pop_up_windows = 1;
7441
7442 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
fdb82f93 7443 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7ab12479
JB
7444 next_screen_context_lines = 2;
7445
7446 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
bdbae5b8
EZ
7447 doc: /* *A window must be at least this tall to be eligible for splitting
7448by `display-buffer'. The value is in line units.
fdb82f93 7449If there is only one window, it is split regardless of this value. */);
7ab12479
JB
7450 split_height_threshold = 500;
7451
7452 DEFVAR_INT ("window-min-height", &window_min_height,
bdbae5b8
EZ
7453 doc: /* *Delete any window less than this tall (including its mode line).
7454The value is in line units. */);
7ab12479
JB
7455 window_min_height = 4;
7456
7457 DEFVAR_INT ("window-min-width", &window_min_width,
bdbae5b8 7458 doc: /* *Delete any window less than this wide (measured in characters). */);
7ab12479
JB
7459 window_min_width = 10;
7460
5500c422
GM
7461 DEFVAR_LISP ("scroll-preserve-screen-position",
7462 &Vscroll_preserve_screen_position,
bdf4ec93
RS
7463 doc: /* *Controls if scroll commands move point to keep its screen line unchanged.
7464A value of nil means point does not keep its screen position except
7465at the scroll margin or window boundary respectively.
7466A value of t means point keeps its screen position if the scroll
7467command moved it vertically out of the window, e.g. when scrolling
7468by full screens.
7469Any other value means point always keeps its screen position. */);
5500c422 7470 Vscroll_preserve_screen_position = Qnil;
9317a85d 7471
543f5fb1
RS
7472 DEFVAR_LISP ("window-configuration-change-hook",
7473 &Vwindow_configuration_change_hook,
fdb82f93
PJ
7474 doc: /* Functions to call when window configuration changes.
7475The selected frame is the one whose configuration has changed. */);
543f5fb1
RS
7476 Vwindow_configuration_change_hook = Qnil;
7477
7ab12479
JB
7478 defsubr (&Sselected_window);
7479 defsubr (&Sminibuffer_window);
7480 defsubr (&Swindow_minibuffer_p);
7481 defsubr (&Swindowp);
806b4d9b 7482 defsubr (&Swindow_live_p);
7ab12479 7483 defsubr (&Spos_visible_in_window_p);
536833ab 7484 defsubr (&Swindow_line_height);
7ab12479
JB
7485 defsubr (&Swindow_buffer);
7486 defsubr (&Swindow_height);
7487 defsubr (&Swindow_width);
7488 defsubr (&Swindow_hscroll);
7489 defsubr (&Sset_window_hscroll);
190eb263
RS
7490 defsubr (&Swindow_redisplay_end_trigger);
7491 defsubr (&Sset_window_redisplay_end_trigger);
7ab12479 7492 defsubr (&Swindow_edges);
c99a9eb3
RS
7493 defsubr (&Swindow_pixel_edges);
7494 defsubr (&Swindow_inside_edges);
7495 defsubr (&Swindow_inside_pixel_edges);
d5783c40
JB
7496 defsubr (&Scoordinates_in_window_p);
7497 defsubr (&Swindow_at);
7ab12479
JB
7498 defsubr (&Swindow_point);
7499 defsubr (&Swindow_start);
7500 defsubr (&Swindow_end);
7501 defsubr (&Sset_window_point);
7502 defsubr (&Sset_window_start);
7503 defsubr (&Swindow_dedicated_p);
d207b766 7504 defsubr (&Sset_window_dedicated_p);
7ab12479
JB
7505 defsubr (&Swindow_display_table);
7506 defsubr (&Sset_window_display_table);
7507 defsubr (&Snext_window);
7508 defsubr (&Sprevious_window);
7509 defsubr (&Sother_window);
7510 defsubr (&Sget_lru_window);
7511 defsubr (&Sget_largest_window);
7512 defsubr (&Sget_buffer_window);
7513 defsubr (&Sdelete_other_windows);
7514 defsubr (&Sdelete_windows_on);
7515 defsubr (&Sreplace_buffer_in_windows);
7516 defsubr (&Sdelete_window);
7517 defsubr (&Sset_window_buffer);
7518 defsubr (&Sselect_window);
4628f7a4
EN
7519 defsubr (&Sspecial_display_p);
7520 defsubr (&Ssame_window_p);
7ab12479 7521 defsubr (&Sdisplay_buffer);
e661376d 7522 defsubr (&Sforce_window_update);
7ab12479
JB
7523 defsubr (&Ssplit_window);
7524 defsubr (&Senlarge_window);
7525 defsubr (&Sshrink_window);
0d384044 7526 defsubr (&Sadjust_window_trailing_edge);
7ab12479
JB
7527 defsubr (&Sscroll_up);
7528 defsubr (&Sscroll_down);
7529 defsubr (&Sscroll_left);
7530 defsubr (&Sscroll_right);
ccd0664b 7531 defsubr (&Sother_window_for_scrolling);
7ab12479 7532 defsubr (&Sscroll_other_window);
fa832261 7533 defsubr (&Sminibuffer_selected_window);
7ab12479 7534 defsubr (&Srecenter);
81fe0836 7535 defsubr (&Swindow_text_height);
7ab12479
JB
7536 defsubr (&Smove_to_window_line);
7537 defsubr (&Swindow_configuration_p);
3f8ab7bd 7538 defsubr (&Swindow_configuration_frame);
7ab12479
JB
7539 defsubr (&Sset_window_configuration);
7540 defsubr (&Scurrent_window_configuration);
7541 defsubr (&Ssave_window_excursion);
c16e1cc3 7542 defsubr (&Swindow_tree);
5500c422
GM
7543 defsubr (&Sset_window_margins);
7544 defsubr (&Swindow_margins);
949cf20f
KS
7545 defsubr (&Sset_window_fringes);
7546 defsubr (&Swindow_fringes);
7547 defsubr (&Sset_window_scroll_bars);
7548 defsubr (&Swindow_scroll_bars);
5500c422
GM
7549 defsubr (&Swindow_vscroll);
7550 defsubr (&Sset_window_vscroll);
2f8274be 7551 defsubr (&Scompare_window_configurations);
67492200 7552 defsubr (&Swindow_list);
7ab12479
JB
7553}
7554
dfcf069d 7555void
7ab12479
JB
7556keys_of_window ()
7557{
7558 initial_define_key (control_x_map, '1', "delete-other-windows");
7559 initial_define_key (control_x_map, '2', "split-window");
7560 initial_define_key (control_x_map, '0', "delete-window");
7561 initial_define_key (control_x_map, 'o', "other-window");
7562 initial_define_key (control_x_map, '^', "enlarge-window");
7563 initial_define_key (control_x_map, '<', "scroll-left");
7564 initial_define_key (control_x_map, '>', "scroll-right");
7565
7566 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7567 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7568 initial_define_key (meta_map, 'v', "scroll-down");
7569
7570 initial_define_key (global_map, Ctl('L'), "recenter");
7571 initial_define_key (meta_map, 'r', "move-to-window-line");
7572}
ab5796a9
MB
7573
7574/* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7575 (do not change this comment) */