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