Merge from trunk
[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 {
f5385255
SM
3525 if (!EQ (tem, buffer))
3526 if (EQ (w->dedicated, Qt))
3527 error ("Window is dedicated to `%s'", SDATA (XBUFFER (tem)->name));
3528 else
3529 w->dedicated = Qnil;
5500c422
GM
3530
3531 unshow_buffer (w);
3532 }
3533
949cf20f 3534 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
7ab12479
JB
3535 return Qnil;
3536}
3537
d653c8cc
MR
3538/* Note that selected_window can be nil when this is called from
3539 Fset_window_configuration. */
2d0834cc 3540
14d87dc9 3541DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
fdb82f93 3542 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
e963215c 3543If WINDOW is not already selected, make WINDOW's buffer current
c2755926 3544and make WINDOW the frame's selected window. Return WINDOW.
4e822bdc 3545Optional second arg NORECORD non-nil means do not put this buffer
c6932ecd
MR
3546at the front of the list of recently selected ones and do not
3547make this window the most recently selected one.
282f7831 3548
4e822bdc
MR
3549Note that the main editor command loop selects the buffer of the
3550selected window before each command. */)
5842a27b 3551 (register Lisp_Object window, Lisp_Object norecord)
7ab12479
JB
3552{
3553 register struct window *w;
719eaeb1 3554 register struct window *ow;
1ae1a37d 3555 struct frame *sf;
7ab12479 3556
b7826503 3557 CHECK_LIVE_WINDOW (window);
7ab12479
JB
3558
3559 w = XWINDOW (window);
50e88778 3560 w->frozen_window_start_p = 0;
7ab12479 3561
4e822bdc
MR
3562 if (NILP (norecord))
3563 {
3564 ++window_select_count;
3565 XSETFASTINT (w->use_time, window_select_count);
13b5221f 3566 record_buffer (w->buffer);
4e822bdc
MR
3567 }
3568
7ab12479
JB
3569 if (EQ (window, selected_window))
3570 return window;
3571
ca2d5566
SM
3572 sf = SELECTED_FRAME ();
3573 if (XFRAME (WINDOW_FRAME (w)) != sf)
3574 {
3575 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3576 /* Use this rather than Fhandle_switch_frame
3577 so that FRAME_FOCUS_FRAME is moved appropriately as we
3578 move around in the state where a minibuffer in a separate
3579 frame is active. */
c6932ecd 3580 Fselect_frame (WINDOW_FRAME (w), norecord);
ca2d5566
SM
3581 /* Fselect_frame called us back so we've done all the work already. */
3582 eassert (EQ (window, selected_window));
3583 return window;
3584 }
3585 else
3586 sf->selected_window = window;
3587
a46c0153
RS
3588 /* Store the current buffer's actual point into the
3589 old selected window. It belongs to that window,
3590 and when the window is not selected, must be in the window. */
719eaeb1
GM
3591 if (!NILP (selected_window))
3592 {
3593 ow = XWINDOW (selected_window);
3594 if (! NILP (ow->buffer))
3595 set_marker_both (ow->pointm, ow->buffer,
3596 BUF_PT (XBUFFER (ow->buffer)),
3597 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3598 }
7ab12479
JB
3599
3600 selected_window = window;
7ab12479 3601
7ab12479
JB
3602 Fset_buffer (w->buffer);
3603
86e48436
RS
3604 XBUFFER (w->buffer)->last_selected_window = window;
3605
7ab12479
JB
3606 /* Go to the point recorded in the window.
3607 This is important when the buffer is in more
3608 than one window. It also matters when
3609 redisplay_window has altered point after scrolling,
3610 because it makes the change only in the window. */
3611 {
2452438f 3612 register EMACS_INT new_point = marker_position (w->pointm);
7ab12479
JB
3613 if (new_point < BEGV)
3614 SET_PT (BEGV);
a9c95e08 3615 else if (new_point > ZV)
7ab12479
JB
3616 SET_PT (ZV);
3617 else
3618 SET_PT (new_point);
3619 }
3620
3621 windows_or_buffers_changed++;
3622 return window;
3623}
14d87dc9
SM
3624
3625static Lisp_Object
971de7fb 3626select_window_norecord (Lisp_Object window)
14d87dc9 3627{
ab6d1131
MR
3628 return WINDOW_LIVE_P (window)
3629 ? Fselect_window (window, Qt) : selected_window;
14d87dc9 3630}
c6932ecd
MR
3631
3632static Lisp_Object
971de7fb 3633select_frame_norecord (Lisp_Object frame)
c6932ecd
MR
3634{
3635 return FRAME_LIVE_P (XFRAME (frame))
3636 ? Fselect_frame (frame, Qt) : selected_frame;
3637}
b7354ddf 3638\f
87478b52 3639Lisp_Object
971de7fb 3640display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
d07f802a 3641{
87478b52 3642 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
d07f802a
RS
3643}
3644
6b61353c
KH
3645DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3646 0, 1, 0,
5a1048a5 3647 doc: /* Force all windows to be updated on next redisplay.
6b61353c 3648If optional arg OBJECT is a window, force redisplay of that window only.
0cc1039f 3649If OBJECT is a buffer or buffer name, force redisplay of all windows
6b61353c 3650displaying that buffer. */)
5842a27b 3651 (Lisp_Object object)
6b61353c
KH
3652{
3653 if (NILP (object))
3654 {
3655 windows_or_buffers_changed++;
3656 update_mode_lines++;
3657 return Qt;
3658 }
3659
3660 if (WINDOWP (object))
3661 {
3662 struct window *w = XWINDOW (object);
3663 mark_window_display_accurate (object, 0);
3664 w->update_mode_line = Qt;
3665 if (BUFFERP (w->buffer))
3666 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3667 ++update_mode_lines;
3668 return Qt;
3669 }
0cc1039f 3670
6b61353c
KH
3671 if (STRINGP (object))
3672 object = Fget_buffer (object);
3673 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3674 {
3675 /* Walk all windows looking for buffer, and force update
3676 of each of those windows. */
3677
3678 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3679 return NILP (object) ? Qnil : Qt;
3680 }
3681
3682 /* If nothing suitable was found, just return.
3683 We could signal an error, but this feature will typically be used
3684 asynchronously in timers or process sentinels, so we don't. */
3685 return Qnil;
3686}
3687
3688
7ab12479 3689void
971de7fb 3690temp_output_buffer_show (register Lisp_Object buf)
7ab12479
JB
3691{
3692 register struct buffer *old = current_buffer;
3693 register Lisp_Object window;
3694 register struct window *w;
3695
bccd3dd1
RS
3696 XBUFFER (buf)->directory = current_buffer->directory;
3697
7ab12479 3698 Fset_buffer (buf);
c6367666 3699 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
7ab12479
JB
3700 BEGV = BEG;
3701 ZV = Z;
3702 SET_PT (BEG);
06be4f85 3703#if 0 /* rms: there should be no reason for this. */
b1599b4c 3704 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
06be4f85 3705#endif
7ab12479
JB
3706 set_buffer_internal (old);
3707
e67a1dea 3708 if (!NILP (Vtemp_buffer_show_function))
7ab12479
JB
3709 call1 (Vtemp_buffer_show_function, buf);
3710 else
3711 {
87478b52 3712 window = display_buffer (buf, Qnil, Qnil);
7ab12479 3713
1ae1a37d 3714 if (!EQ (XWINDOW (window)->frame, selected_frame))
44fa5b1e 3715 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
7ab12479
JB
3716 Vminibuf_scroll_window = window;
3717 w = XWINDOW (window);
d834a2e9 3718 XSETFASTINT (w->hscroll, 0);
ea68264b 3719 XSETFASTINT (w->min_hscroll, 0);
2d0834cc
SM
3720 set_marker_restricted_both (w->start, buf, BEG, BEG);
3721 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
a58ec57d 3722
beb4e312 3723 /* Run temp-buffer-show-hook, with the chosen window selected
a5731348 3724 and its buffer current. */
2d0834cc
SM
3725
3726 if (!NILP (Vrun_hooks)
3727 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3728 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
2cccc823 3729 {
2d0834cc
SM
3730 int count = SPECPDL_INDEX ();
3731 Lisp_Object prev_window, prev_buffer;
3732 prev_window = selected_window;
3733 XSETBUFFER (prev_buffer, old);
0cc1039f 3734
2d0834cc
SM
3735 /* Select the window that was chosen, for running the hook.
3736 Note: Both Fselect_window and select_window_norecord may
3737 set-buffer to the buffer displayed in the window,
3738 so we need to save the current buffer. --stef */
3739 record_unwind_protect (Fset_buffer, prev_buffer);
3740 record_unwind_protect (select_window_norecord, prev_window);
3741 Fselect_window (window, Qt);
3742 Fset_buffer (w->buffer);
3743 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3744 unbind_to (count, Qnil);
2cccc823
RS
3745 }
3746 }
7ab12479
JB
3747}
3748\f
dfcf069d 3749static void
971de7fb 3750make_dummy_parent (Lisp_Object window)
7ab12479 3751{
cffec418 3752 Lisp_Object new;
7ab12479 3753 register struct window *o, *p;
cffec418 3754 int i;
7ab12479 3755
cffec418 3756 o = XWINDOW (window);
26605be9 3757 p = allocate_window ();
cffec418 3758 for (i = 0; i < VECSIZE (struct window); ++i)
26605be9
GM
3759 ((struct Lisp_Vector *) p)->contents[i]
3760 = ((struct Lisp_Vector *)o)->contents[i];
cffec418 3761 XSETWINDOW (new, p);
7ab12479 3762
2a1893f4
SM
3763 ++sequence_number;
3764 XSETFASTINT (p->sequence_number, sequence_number);
7ab12479
JB
3765
3766 /* Put new into window structure in place of window */
3767 replace_window (window, new);
3768
3769 o->next = Qnil;
3770 o->prev = Qnil;
3771 o->vchild = Qnil;
3772 o->hchild = Qnil;
3773 o->parent = new;
3774
3775 p->start = Qnil;
3776 p->pointm = Qnil;
3777 p->buffer = Qnil;
3778}
3779
3780DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
fdb82f93
PJ
3781 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3782WINDOW defaults to selected one and SIZE to half its size.
87527026
MR
3783If optional third arg HORIZONTAL is non-nil, split side by side and put
3784SIZE columns in the first of the pair. In that case, SIZE includes that
3785window's scroll bar, or the divider column to its right.
c2755926 3786Interactively, all arguments are nil.
c2755926 3787Returns the newly created window (which is the lower or rightmost one).
19ca94ce
RS
3788The upper or leftmost window is the original one, and remains selected
3789if it was selected before.
3790
87527026 3791See Info node `(elisp)Splitting Windows' for more details and examples. */)
5842a27b 3792 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
7ab12479
JB
3793{
3794 register Lisp_Object new;
3795 register struct window *o, *p;
c0807608 3796 FRAME_PTR fo;
77ae0fe3 3797 register int size_int;
7ab12479 3798
265a9e55 3799 if (NILP (window))
7ab12479
JB
3800 window = selected_window;
3801 else
b7826503 3802 CHECK_LIVE_WINDOW (window);
7ab12479
JB
3803
3804 o = XWINDOW (window);
c0807608 3805 fo = XFRAME (WINDOW_FRAME (o));
7ab12479 3806
77ae0fe3 3807 if (NILP (size))
7ab12479 3808 {
87527026 3809 if (!NILP (horizontal))
c0807608 3810 /* Calculate the size of the left-hand window, by dividing
25b33244
KH
3811 the usable space in columns by two.
3812 We round up, since the left-hand window may include
3813 a dividing line, while the right-hand may not. */
949cf20f 3814 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
7ab12479 3815 else
949cf20f 3816 size_int = XFASTINT (o->total_lines) >> 1;
7ab12479
JB
3817 }
3818 else
3819 {
b7826503 3820 CHECK_NUMBER (size);
77ae0fe3 3821 size_int = XINT (size);
7ab12479
JB
3822 }
3823
3824 if (MINI_WINDOW_P (o))
3825 error ("Attempt to split minibuffer window");
87527026 3826 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
233a4a2c 3827 error ("Attempt to split fixed-size window");
7ab12479 3828
87527026 3829 if (NILP (horizontal))
7ab12479 3830 {
047aaeb9
MR
3831 int window_safe_height = window_min_size_2 (o, 0, 0);
3832
f1de8c77 3833 if (size_int < window_safe_height)
77ae0fe3 3834 error ("Window height %d too small (after splitting)", size_int);
f1de8c77 3835 if (size_int + window_safe_height > XFASTINT (o->total_lines))
37962e60 3836 error ("Window height %d too small (after splitting)",
949cf20f 3837 XFASTINT (o->total_lines) - size_int);
265a9e55
JB
3838 if (NILP (o->parent)
3839 || NILP (XWINDOW (o->parent)->vchild))
7ab12479
JB
3840 {
3841 make_dummy_parent (window);
3842 new = o->parent;
3843 XWINDOW (new)->vchild = window;
3844 }
3845 }
3846 else
3847 {
047aaeb9
MR
3848 int window_safe_width = window_min_size_2 (o, 1, 0);
3849
f1de8c77 3850 if (size_int < window_safe_width)
77ae0fe3 3851 error ("Window width %d too small (after splitting)", size_int);
f1de8c77 3852 if (size_int + window_safe_width > XFASTINT (o->total_cols))
37962e60 3853 error ("Window width %d too small (after splitting)",
949cf20f 3854 XFASTINT (o->total_cols) - size_int);
265a9e55
JB
3855 if (NILP (o->parent)
3856 || NILP (XWINDOW (o->parent)->hchild))
7ab12479
JB
3857 {
3858 make_dummy_parent (window);
3859 new = o->parent;
3860 XWINDOW (new)->hchild = window;
3861 }
3862 }
3863
3864 /* Now we know that window's parent is a vertical combination
3865 if we are dividing vertically, or a horizontal combination
3866 if we are making side-by-side windows */
3867
3868 windows_or_buffers_changed++;
c0807608 3869 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
7ab12479
JB
3870 new = make_window ();
3871 p = XWINDOW (new);
3872
44fa5b1e 3873 p->frame = o->frame;
7ab12479 3874 p->next = o->next;
265a9e55 3875 if (!NILP (p->next))
7ab12479
JB
3876 XWINDOW (p->next)->prev = new;
3877 p->prev = window;
3878 o->next = new;
3879 p->parent = o->parent;
3880 p->buffer = Qt;
5500c422 3881 p->window_end_valid = Qnil;
72af86bd 3882 memset (&p->last_cursor, 0, sizeof p->last_cursor);
7ab12479 3883
949cf20f
KS
3884 /* Duplicate special geometry settings. */
3885
3886 p->left_margin_cols = o->left_margin_cols;
3887 p->right_margin_cols = o->right_margin_cols;
3888 p->left_fringe_width = o->left_fringe_width;
3889 p->right_fringe_width = o->right_fringe_width;
3890 p->fringes_outside_margins = o->fringes_outside_margins;
3891 p->scroll_bar_width = o->scroll_bar_width;
3892 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3893
44fa5b1e 3894 /* Apportion the available frame space among the two new windows */
7ab12479 3895
87527026 3896 if (!NILP (horizontal))
7ab12479 3897 {
949cf20f
KS
3898 p->total_lines = o->total_lines;
3899 p->top_line = o->top_line;
3900 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3901 XSETFASTINT (o->total_cols, size_int);
3902 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3903 adjust_window_margins (p);
3904 adjust_window_margins (o);
7ab12479
JB
3905 }
3906 else
3907 {
949cf20f
KS
3908 p->left_col = o->left_col;
3909 p->total_cols = o->total_cols;
3910 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3911 XSETFASTINT (o->total_lines, size_int);
3912 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
7ab12479
JB
3913 }
3914
5500c422
GM
3915 /* Adjust glyph matrices. */
3916 adjust_glyphs (fo);
949cf20f
KS
3917
3918 Fset_window_buffer (new, o->buffer, Qt);
7ab12479
JB
3919 return new;
3920}
3921\f
56ebfae2 3922DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
aac0c6e3
MR
3923 doc: /* Make selected window SIZE lines taller.
3924Interactively, if no argument is given, make the selected window one
3925line taller. If optional argument HORIZONTAL is non-nil, make selected
3926window wider by SIZE columns. If SIZE is negative, shrink the window by
3927-SIZE lines or columns. Return nil.
3928
3929This function can delete windows if they get too small. The size of
3930fixed size windows is not altered by this function. */)
5842a27b 3931 (Lisp_Object size, Lisp_Object horizontal)
7ab12479 3932{
aac0c6e3
MR
3933 CHECK_NUMBER (size);
3934 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
543f5fb1 3935
eeca6f6f 3936 run_window_configuration_change_hook (SELECTED_FRAME ());
543f5fb1 3937
7ab12479
JB
3938 return Qnil;
3939}
3940
56ebfae2 3941DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
aac0c6e3
MR
3942 doc: /* Make selected window SIZE lines smaller.
3943Interactively, if no argument is given, make the selected window one
3944line smaller. If optional argument HORIZONTAL is non-nil, make the
3945window narrower by SIZE columns. If SIZE is negative, enlarge selected
3946window by -SIZE lines or columns. Return nil.
3947
3948This function can delete windows if they get too small. The size of
3949fixed size windows is not altered by this function. */)
5842a27b 3950 (Lisp_Object size, Lisp_Object horizontal)
aac0c6e3
MR
3951{
3952 CHECK_NUMBER (size);
3953 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
543f5fb1 3954
eeca6f6f 3955 run_window_configuration_change_hook (SELECTED_FRAME ());
543f5fb1 3956
7ab12479
JB
3957 return Qnil;
3958}
3959
3960int
971de7fb 3961window_height (Lisp_Object window)
7ab12479
JB
3962{
3963 register struct window *p = XWINDOW (window);
949cf20f 3964 return WINDOW_TOTAL_LINES (p);
7ab12479
JB
3965}
3966
3967int
971de7fb 3968window_width (Lisp_Object window)
7ab12479
JB
3969{
3970 register struct window *p = XWINDOW (window);
949cf20f 3971 return WINDOW_TOTAL_COLS (p);
7ab12479
JB
3972}
3973
177c0ea7 3974
7ab12479 3975#define CURBEG(w) \
5afc696a 3976 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
7ab12479
JB
3977
3978#define CURSIZE(w) \
5afc696a 3979 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
7ab12479 3980
233a4a2c 3981
047aaeb9
MR
3982/* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
3983 horizontally; zero means do it vertically.
f95284d2 3984
5afc696a 3985 Siblings of the selected window are resized to fulfill the size
047aaeb9 3986 request. If they become too small in the process, they may be
56ebfae2 3987 deleted. */
7ab12479 3988
f984d4fc 3989static void
971de7fb 3990enlarge_window (Lisp_Object window, int delta, int horiz_flag)
7ab12479 3991{
86c8e823 3992 Lisp_Object parent, next, prev;
233a4a2c 3993 struct window *p;
3578db3c
KR
3994 Lisp_Object *sizep;
3995 int maximum;
f57e2426 3996 int (*sizefun) (Lisp_Object)
5afc696a 3997 = horiz_flag ? window_width : window_height;
f57e2426 3998 void (*setsizefun) (Lisp_Object, int, int)
5afc696a 3999 = (horiz_flag ? set_window_width : set_window_height);
7ab12479 4000
233a4a2c 4001 /* Give up if this window cannot be resized. */
5afc696a 4002 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
233a4a2c
GM
4003 error ("Window is not resizable");
4004
4005 /* Find the parent of the selected window. */
7ab12479
JB
4006 while (1)
4007 {
4008 p = XWINDOW (window);
4009 parent = p->parent;
177c0ea7 4010
265a9e55 4011 if (NILP (parent))
7ab12479 4012 {
5afc696a 4013 if (horiz_flag)
7ab12479
JB
4014 error ("No other window to side of this one");
4015 break;
4016 }
177c0ea7 4017
5afc696a 4018 if (horiz_flag
233a4a2c 4019 ? !NILP (XWINDOW (parent)->hchild)
265a9e55 4020 : !NILP (XWINDOW (parent)->vchild))
7ab12479 4021 break;
177c0ea7 4022
7ab12479
JB
4023 window = parent;
4024 }
4025
05c2896a 4026 sizep = &CURSIZE (window);
7ab12479 4027
7ab12479
JB
4028 {
4029 register int maxdelta;
7ab12479 4030
f95284d2
RS
4031 /* Compute the maximum size increment this window can have. */
4032
56ebfae2
RS
4033 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4034 /* This is a main window followed by a minibuffer. */
4035 : !NILP (p->next) ? ((*sizefun) (p->next)
4036 - window_min_size (XWINDOW (p->next),
047aaeb9 4037 horiz_flag, 0, 0, 0))
56ebfae2
RS
4038 /* This is a minibuffer following a main window. */
4039 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4040 - window_min_size (XWINDOW (p->prev),
047aaeb9 4041 horiz_flag, 0, 0, 0))
56ebfae2
RS
4042 /* This is a frame with only one window, a minibuffer-only
4043 or a minibufferless frame. */
4044 : (delta = 0));
7ab12479
JB
4045
4046 if (delta > maxdelta)
4047 /* This case traps trying to make the minibuffer
44fa5b1e
JB
4048 the full frame, or make the only window aside from the
4049 minibuffer the full frame. */
7ab12479 4050 delta = maxdelta;
6b54027b 4051 }
d5783c40 4052
047aaeb9
MR
4053 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4054 horiz_flag, 0, 0, 0))
6b54027b 4055 {
543f5fb1 4056 delete_window (window);
d5783c40 4057 return;
6b54027b
RS
4058 }
4059
4060 if (delta == 0)
4061 return;
7ab12479 4062
f95284d2 4063 /* Find the total we can get from other siblings without deleting them. */
db98a733 4064 maximum = 0;
9beb8baa 4065 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
c1636aa6 4066 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
047aaeb9 4067 horiz_flag, 0, 0, 0);
9beb8baa 4068 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
56ebfae2 4069 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
047aaeb9 4070 horiz_flag, 0, 0, 0);
db98a733 4071
f95284d2 4072 /* If we can get it all from them without deleting them, do so. */
c6b530ed 4073 if (delta <= maximum)
7ab12479 4074 {
db98a733
RS
4075 Lisp_Object first_unaffected;
4076 Lisp_Object first_affected;
233a4a2c 4077 int fixed_p;
db98a733
RS
4078
4079 next = p->next;
4080 prev = p->prev;
4081 first_affected = window;
4082 /* Look at one sibling at a time,
4083 moving away from this window in both directions alternately,
4084 and take as much as we can get without deleting that sibling. */
f95284d2 4085 while (delta != 0
56ebfae2 4086 && (!NILP (next) || !NILP (prev)))
db98a733 4087 {
db98a733
RS
4088 if (! NILP (next))
4089 {
c1636aa6 4090 int this_one = ((*sizefun) (next)
047aaeb9
MR
4091 - window_min_size (XWINDOW (next), horiz_flag,
4092 0, 0, &fixed_p));
233a4a2c
GM
4093 if (!fixed_p)
4094 {
4095 if (this_one > delta)
4096 this_one = delta;
177c0ea7 4097
233a4a2c 4098 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3578db3c 4099 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
db98a733 4100
233a4a2c
GM
4101 delta -= this_one;
4102 }
177c0ea7 4103
db98a733
RS
4104 next = XWINDOW (next)->next;
4105 }
177c0ea7 4106
db98a733
RS
4107 if (delta == 0)
4108 break;
177c0ea7 4109
56ebfae2 4110 if (! NILP (prev))
db98a733 4111 {
c1636aa6 4112 int this_one = ((*sizefun) (prev)
047aaeb9
MR
4113 - window_min_size (XWINDOW (prev), horiz_flag,
4114 0, 0, &fixed_p));
233a4a2c
GM
4115 if (!fixed_p)
4116 {
4117 if (this_one > delta)
4118 this_one = delta;
177c0ea7 4119
233a4a2c 4120 first_affected = prev;
177c0ea7 4121
233a4a2c 4122 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3578db3c 4123 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
233a4a2c
GM
4124
4125 delta -= this_one;
4126 }
177c0ea7 4127
db98a733
RS
4128 prev = XWINDOW (prev)->prev;
4129 }
4130 }
4131
233a4a2c
GM
4132 xassert (delta == 0);
4133
db98a733
RS
4134 /* Now recalculate the edge positions of all the windows affected,
4135 based on the new sizes. */
4136 first_unaffected = next;
4137 prev = first_affected;
4138 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4139 prev = next, next = XWINDOW (next)->next)
4140 {
3578db3c 4141 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
db98a733
RS
4142 /* This does not change size of NEXT,
4143 but it propagates the new top edge to its children */
4144 (*setsizefun) (next, (*sizefun) (next), 0);
4145 }
7ab12479
JB
4146 }
4147 else
4148 {
4149 register int delta1;
4150 register int opht = (*sizefun) (parent);
4151
3578db3c 4152 if (opht <= XINT (*sizep) + delta)
daf516d3
RS
4153 {
4154 /* If trying to grow this window to or beyond size of the parent,
4155 just delete all the sibling windows. */
dc1ab1ee 4156 Lisp_Object start, tem, next;
daf516d3 4157
dc1ab1ee
RS
4158 start = XWINDOW (parent)->vchild;
4159 if (NILP (start))
4160 start = XWINDOW (parent)->hchild;
daf516d3 4161
dc1ab1ee
RS
4162 /* Delete any siblings that come after WINDOW. */
4163 tem = XWINDOW (window)->next;
daf516d3
RS
4164 while (! NILP (tem))
4165 {
4166 next = XWINDOW (tem)->next;
dc1ab1ee
RS
4167 delete_window (tem);
4168 tem = next;
4169 }
4170
4171 /* Delete any siblings that come after WINDOW.
4172 Note that if START is not WINDOW, then WINDOW still
047aaeb9 4173 has siblings, so WINDOW has not yet replaced its parent. */
dc1ab1ee
RS
4174 tem = start;
4175 while (! EQ (tem, window))
4176 {
4177 next = XWINDOW (tem)->next;
4178 delete_window (tem);
daf516d3
RS
4179 tem = next;
4180 }
4181 }
7ab12479 4182 else
233a4a2c
GM
4183 {
4184 /* Otherwise, make delta1 just right so that if we add
4185 delta1 lines to this window and to the parent, and then
4186 shrink the parent back to its original size, the new
4187 proportional size of this window will increase by delta.
4188
4189 The function size_window will compute the new height h'
4190 of the window from delta1 as:
177c0ea7 4191
233a4a2c
GM
4192 e = delta1/n
4193 x = delta1 - delta1/n * n for the 1st resizable child
4194 h' = h + e + x
4195
4196 where n is the number of children that can be resized.
4197 We can ignore x by choosing a delta1 that is a multiple of
4198 n. We want the height of this window to come out as
177c0ea7 4199
233a4a2c
GM
4200 h' = h + delta
4201
4202 So, delta1 must be
177c0ea7 4203
233a4a2c
GM
4204 h + e = h + delta
4205 delta1/n = delta
4206 delta1 = n * delta.
4207
a5731348 4208 The number of children n equals the number of resizable
233a4a2c 4209 children of this window + 1 because we know window itself
04bf5b65 4210 is resizable (otherwise we would have signaled an error).
047aaeb9
MR
4211
4212 This reasoning is not correct when other windows become too
4213 small and shrink_windows refuses to delete them. Below we
4214 use resize_proportionally to work around this problem. */
233a4a2c
GM
4215
4216 struct window *w = XWINDOW (window);
4217 Lisp_Object s;
4218 int n = 1;
4219
9beb8baa 4220 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
5afc696a 4221 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
233a4a2c 4222 ++n;
9beb8baa 4223 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
5afc696a 4224 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
233a4a2c
GM
4225 ++n;
4226
4227 delta1 = n * delta;
7ab12479 4228
daf516d3
RS
4229 /* Add delta1 lines or columns to this window, and to the parent,
4230 keeping things consistent while not affecting siblings. */
4231 XSETINT (CURSIZE (parent), opht + delta1);
4232 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4233
4234 /* Squeeze out delta1 lines or columns from our parent,
047aaeb9
MR
4235 shrinking this window and siblings proportionately. This
4236 brings parent back to correct size. Delta1 was calculated
4237 so this makes this window the desired size, taking it all
4238 out of the siblings.
4239
4240 Temporarily set resize_proportionally to Qt to assure that,
4241 if necessary, shrink_windows deletes smaller windows rather
4242 than shrink this window. */
4243 w->resize_proportionally = Qt;
daf516d3 4244 (*setsizefun) (parent, opht, 0);
047aaeb9 4245 w->resize_proportionally = Qnil;
daf516d3 4246 }
7ab12479
JB
4247 }
4248
d834a2e9 4249 XSETFASTINT (p->last_modified, 0);
3cd21523 4250 XSETFASTINT (p->last_overlay_modified, 0);
5500c422
GM
4251
4252 /* Adjust glyph matrices. */
4253 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7ab12479 4254}
c1636aa6 4255
0d384044
RS
4256
4257/* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4258 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4259 zero means adjust the height, moving the bottom edge.
4260
4261 Following siblings of the selected window are resized to fulfill
4262 the size request. If they become too small in the process, they
4263 are not deleted; instead, we signal an error. */
4264
4265static void
971de7fb 4266adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
0d384044
RS
4267{
4268 Lisp_Object parent, child;
4269 struct window *p;
4270 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4271 int delcount = window_deletion_count;
4272
0d384044
RS
4273 CHECK_WINDOW (window);
4274
4275 /* Give up if this window cannot be resized. */
4276 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4277 error ("Window is not resizable");
4278
4279 while (1)
4280 {
bd99e242
RS
4281 Lisp_Object first_parallel = Qnil;
4282
c32de52a 4283 if (NILP (window))
0d384044 4284 {
5fe0b054
RS
4285 /* This happens if WINDOW on the previous iteration was
4286 at top level of the window tree. */
0d384044 4287 Fset_window_configuration (old_config);
c32de52a 4288 error ("Specified window edge is fixed");
0d384044
RS
4289 }
4290
c32de52a
RS
4291 p = XWINDOW (window);
4292 parent = p->parent;
4293
bd99e242
RS
4294 /* See if this level has windows in parallel in the specified
4295 direction. If so, set FIRST_PARALLEL to the first one. */
4296 if (horiz_flag)
4297 {
4298 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4299 first_parallel = XWINDOW (parent)->vchild;
5fe0b054
RS
4300 else if (NILP (parent) && !NILP (p->next))
4301 {
4302 /* Handle the vertical chain of main window and minibuffer
4303 which has no parent. */
4304 first_parallel = window;
4305 while (! NILP (XWINDOW (first_parallel)->prev))
4306 first_parallel = XWINDOW (first_parallel)->prev;
4307 }
bd99e242
RS
4308 }
4309 else
4310 {
4311 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4312 first_parallel = XWINDOW (parent)->hchild;
4313 }
4314
c32de52a 4315 /* If this level's succession is in the desired dimension,
5fe0b054
RS
4316 and this window is the last one, and there is no higher level,
4317 its trailing edge is fixed. */
4318 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4319 && NILP (parent))
c32de52a
RS
4320 {
4321 Fset_window_configuration (old_config);
4322 error ("Specified window edge is fixed");
4323 }
4324
0d384044
RS
4325 /* Don't make this window too small. */
4326 if (XINT (CURSIZE (window)) + delta
047aaeb9 4327 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
0d384044
RS
4328 {
4329 Fset_window_configuration (old_config);
4330 error ("Cannot adjust window size as specified");
4331 }
4332
4333 /* Clear out some redisplay caches. */
4334 XSETFASTINT (p->last_modified, 0);
4335 XSETFASTINT (p->last_overlay_modified, 0);
4336
4337 /* Adjust this window's edge. */
4338 XSETINT (CURSIZE (window),
4339 XINT (CURSIZE (window)) + delta);
4340
4341 /* If this window has following siblings in the desired dimension,
bd99e242
RS
4342 make them smaller, and exit the loop.
4343
0d384044
RS
4344 (If we reach the top of the tree and can never do this,
4345 we will fail and report an error, above.) */
bd99e242 4346 if (NILP (first_parallel))
0d384044 4347 {
c32de52a 4348 if (!NILP (p->next))
0d384044 4349 {
e99c7521
JD
4350 /* This may happen for the minibuffer. In that case
4351 the window_deletion_count check below does not work. */
a53d44a8 4352 if (XINT (CURSIZE (p->next)) - delta <= 0)
e99c7521
JD
4353 {
4354 Fset_window_configuration (old_config);
4355 error ("Cannot adjust window size as specified");
4356 }
4357
0d384044
RS
4358 XSETINT (CURBEG (p->next),
4359 XINT (CURBEG (p->next)) + delta);
4360 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
5fe0b054 4361 horiz_flag, 0, 1, 0);
0d384044
RS
4362 break;
4363 }
4364 }
4365 else
4366 /* Here we have a chain of parallel siblings, in the other dimension.
4367 Change the size of the other siblings. */
bd99e242 4368 for (child = first_parallel;
0d384044
RS
4369 ! NILP (child);
4370 child = XWINDOW (child)->next)
4371 if (! EQ (child, window))
4372 size_window (child, XINT (CURSIZE (child)) + delta,
5fe0b054 4373 horiz_flag, 0, 0, 1);
0d384044
RS
4374
4375 window = parent;
4376 }
4377
4378 /* If we made a window so small it got deleted,
4379 we failed. Report failure. */
4380 if (delcount != window_deletion_count)
4381 {
4382 Fset_window_configuration (old_config);
4383 error ("Cannot adjust window size as specified");
4384 }
4385
4386 /* Adjust glyph matrices. */
4387 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4388}
4389
7ab12479
JB
4390#undef CURBEG
4391#undef CURSIZE
4392
0d384044
RS
4393DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4394 Sadjust_window_trailing_edge, 3, 3, 0,
4395 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
56ebfae2 4396If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
0d384044
RS
4397Otherwise, adjust the height, moving the bottom edge.
4398
4399Following siblings of the selected window are resized to fulfill
4400the size request. If they become too small in the process, they
4401are not deleted; instead, we signal an error. */)
5842a27b 4402 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
0d384044
RS
4403{
4404 CHECK_NUMBER (delta);
eeca6f6f
SM
4405 if (NILP (window))
4406 window = selected_window;
0d384044
RS
4407 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4408
eeca6f6f
SM
4409 run_window_configuration_change_hook
4410 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
0d384044
RS
4411
4412 return Qnil;
4413}
4414
5500c422 4415
f984d4fc
GM
4416\f
4417/***********************************************************************
4418 Resizing Mini-Windows
4419 ***********************************************************************/
4420
f57e2426 4421static void shrink_window_lowest_first (struct window *, int);
f984d4fc 4422
43b4a21f
GM
4423enum save_restore_action
4424{
4425 CHECK_ORIG_SIZES,
4426 SAVE_ORIG_SIZES,
4427 RESTORE_ORIG_SIZES
4428};
4429
f57e2426
J
4430static int save_restore_orig_size (struct window *,
4431 enum save_restore_action);
f984d4fc
GM
4432
4433/* Shrink windows rooted in window W to HEIGHT. Take the space needed
4434 from lowest windows first. */
4435
4436static void
971de7fb 4437shrink_window_lowest_first (struct window *w, int height)
f984d4fc
GM
4438{
4439 struct window *c;
4440 Lisp_Object child;
4441 int old_height;
4442
4443 xassert (!MINI_WINDOW_P (w));
4444
4445 /* Set redisplay hints. */
4446 XSETFASTINT (w->last_modified, 0);
4447 XSETFASTINT (w->last_overlay_modified, 0);
4448 windows_or_buffers_changed++;
4449 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4450
949cf20f
KS
4451 old_height = XFASTINT (w->total_lines);
4452 XSETFASTINT (w->total_lines, height);
f984d4fc
GM
4453
4454 if (!NILP (w->hchild))
4455 {
4456 for (child = w->hchild; !NILP (child); child = c->next)
4457 {
4458 c = XWINDOW (child);
949cf20f 4459 c->top_line = w->top_line;
f984d4fc
GM
4460 shrink_window_lowest_first (c, height);
4461 }
4462 }
4463 else if (!NILP (w->vchild))
4464 {
4465 Lisp_Object last_child;
4466 int delta = old_height - height;
4467 int last_top;
6bbd7a29
GM
4468
4469 last_child = Qnil;
177c0ea7 4470
f984d4fc
GM
4471 /* Find the last child. We are taking space from lowest windows
4472 first, so we iterate over children from the last child
4473 backwards. */
9beb8baa 4474 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
f984d4fc
GM
4475 last_child = child;
4476
047aaeb9 4477 /* Size children down to their safe heights. */
f984d4fc
GM
4478 for (child = last_child; delta && !NILP (child); child = c->prev)
4479 {
4480 int this_one;
177c0ea7 4481
f984d4fc 4482 c = XWINDOW (child);
047aaeb9 4483 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
f984d4fc
GM
4484
4485 if (this_one > delta)
4486 this_one = delta;
4487
949cf20f 4488 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
f984d4fc
GM
4489 delta -= this_one;
4490 }
4491
4492 /* Compute new positions. */
949cf20f 4493 last_top = XINT (w->top_line);
f984d4fc
GM
4494 for (child = w->vchild; !NILP (child); child = c->next)
4495 {
4496 c = XWINDOW (child);
949cf20f
KS
4497 c->top_line = make_number (last_top);
4498 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4499 last_top += XFASTINT (c->total_lines);
f984d4fc
GM
4500 }
4501 }
4502}
4503
4504
43b4a21f
GM
4505/* Save, restore, or check positions and sizes in the window tree
4506 rooted at W. ACTION says what to do.
f984d4fc 4507
949cf20f
KS
4508 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4509 orig_total_lines members are valid for all windows in the window
4510 tree. Value is non-zero if they are valid.
177c0ea7 4511
43b4a21f 4512 If ACTION is SAVE_ORIG_SIZES, save members top and height in
949cf20f 4513 orig_top_line and orig_total_lines for all windows in the tree.
43b4a21f 4514
949cf20f
KS
4515 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4516 stored in orig_top_line and orig_total_lines for all windows. */
43b4a21f
GM
4517
4518static int
971de7fb 4519save_restore_orig_size (struct window *w, enum save_restore_action action)
f984d4fc 4520{
43b4a21f
GM
4521 int success_p = 1;
4522
f984d4fc
GM
4523 while (w)
4524 {
4525 if (!NILP (w->hchild))
43b4a21f
GM
4526 {
4527 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4528 success_p = 0;
4529 }
f984d4fc 4530 else if (!NILP (w->vchild))
43b4a21f
GM
4531 {
4532 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4533 success_p = 0;
4534 }
177c0ea7 4535
43b4a21f 4536 switch (action)
f984d4fc 4537 {
43b4a21f 4538 case CHECK_ORIG_SIZES:
949cf20f 4539 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
43b4a21f
GM
4540 return 0;
4541 break;
4542
4543 case SAVE_ORIG_SIZES:
949cf20f
KS
4544 w->orig_top_line = w->top_line;
4545 w->orig_total_lines = w->total_lines;
43b4a21f
GM
4546 XSETFASTINT (w->last_modified, 0);
4547 XSETFASTINT (w->last_overlay_modified, 0);
4548 break;
4549
4550 case RESTORE_ORIG_SIZES:
949cf20f
KS
4551 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4552 w->top_line = w->orig_top_line;
4553 w->total_lines = w->orig_total_lines;
4554 w->orig_total_lines = w->orig_top_line = Qnil;
43b4a21f
GM
4555 XSETFASTINT (w->last_modified, 0);
4556 XSETFASTINT (w->last_overlay_modified, 0);
4557 break;
4558
4559 default:
4560 abort ();
f984d4fc 4561 }
43b4a21f 4562
f984d4fc
GM
4563 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4564 }
43b4a21f
GM
4565
4566 return success_p;
f984d4fc
GM
4567}
4568
4569
4570/* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4571 without deleting other windows. */
4572
4573void
971de7fb 4574grow_mini_window (struct window *w, int delta)
f984d4fc
GM
4575{
4576 struct frame *f = XFRAME (w->frame);
4577 struct window *root;
177c0ea7 4578
f984d4fc 4579 xassert (MINI_WINDOW_P (w));
522d013a
JB
4580 /* Commenting out the following assertion goes against the stated interface
4581 of the function, but it currently does not seem to do anything useful.
4582 See discussion of this issue in the thread for bug#4534.
4583 xassert (delta >= 0); */
177c0ea7 4584
f984d4fc
GM
4585 /* Compute how much we can enlarge the mini-window without deleting
4586 other windows. */
4587 root = XWINDOW (FRAME_ROOT_WINDOW (f));
522d013a 4588 if (delta > 0)
f984d4fc 4589 {
047aaeb9 4590 int min_height = window_min_size (root, 0, 0, 0, 0);
949cf20f 4591 if (XFASTINT (root->total_lines) - delta < min_height)
8b8bd9c6 4592 /* Note that the root window may already be smaller than
eafa3196 4593 min_height. */
949cf20f 4594 delta = max (0, XFASTINT (root->total_lines) - min_height);
f984d4fc 4595 }
177c0ea7 4596
f984d4fc
GM
4597 if (delta)
4598 {
4599 /* Save original window sizes and positions, if not already done. */
43b4a21f
GM
4600 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4601 save_restore_orig_size (root, SAVE_ORIG_SIZES);
f984d4fc
GM
4602
4603 /* Shrink other windows. */
949cf20f 4604 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
f984d4fc
GM
4605
4606 /* Grow the mini-window. */
949cf20f
KS
4607 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4608 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
f984d4fc
GM
4609 XSETFASTINT (w->last_modified, 0);
4610 XSETFASTINT (w->last_overlay_modified, 0);
177c0ea7 4611
f984d4fc
GM
4612 adjust_glyphs (f);
4613 }
4614}
4615
4616
86c8e823
GM
4617/* Shrink mini-window W. If there is recorded info about window sizes
4618 before a call to grow_mini_window, restore recorded window sizes.
4619 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4620 line. */
f984d4fc
GM
4621
4622void
971de7fb 4623shrink_mini_window (struct window *w)
f984d4fc
GM
4624{
4625 struct frame *f = XFRAME (w->frame);
4626 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4627
43b4a21f 4628 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
f984d4fc 4629 {
43b4a21f 4630 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
f984d4fc
GM
4631 adjust_glyphs (f);
4632 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4633 windows_or_buffers_changed = 1;
4634 }
949cf20f 4635 else if (XFASTINT (w->total_lines) > 1)
86c8e823 4636 {
0130fe1a
GM
4637 /* Distribute the additional lines of the mini-window
4638 among the other windows. */
86c8e823
GM
4639 Lisp_Object window;
4640 XSETWINDOW (window, w);
56ebfae2 4641 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
86c8e823 4642 }
f984d4fc
GM
4643}
4644
4645
4646\f
5500c422
GM
4647/* Mark window cursors off for all windows in the window tree rooted
4648 at W by setting their phys_cursor_on_p flag to zero. Called from
4649 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4650 the frame are cleared. */
4651
4652void
971de7fb 4653mark_window_cursors_off (struct window *w)
5500c422
GM
4654{
4655 while (w)
4656 {
4657 if (!NILP (w->hchild))
4658 mark_window_cursors_off (XWINDOW (w->hchild));
4659 else if (!NILP (w->vchild))
4660 mark_window_cursors_off (XWINDOW (w->vchild));
4661 else
4662 w->phys_cursor_on_p = 0;
4663
4664 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4665 }
4666}
4667
4668
e9c195b1 4669/* Return number of lines of text (not counting mode lines) in W. */
7ab12479
JB
4670
4671int
971de7fb 4672window_internal_height (struct window *w)
7ab12479 4673{
949cf20f 4674 int ht = XFASTINT (w->total_lines);
7ab12479 4675
e9c195b1
GM
4676 if (!MINI_WINDOW_P (w))
4677 {
4678 if (!NILP (w->parent)
4679 || !NILP (w->vchild)
4680 || !NILP (w->hchild)
4681 || !NILP (w->next)
4682 || !NILP (w->prev)
4683 || WINDOW_WANTS_MODELINE_P (w))
4684 --ht;
7ab12479 4685
e9c195b1
GM
4686 if (WINDOW_WANTS_HEADER_LINE_P (w))
4687 --ht;
4688 }
7ab12479
JB
4689
4690 return ht;
4691}
4692
535e0b8e
JB
4693
4694/* Return the number of columns in W.
a3c87d4e 4695 Don't count columns occupied by scroll bars or the vertical bar
535e0b8e 4696 separating W from the sibling to its right. */
5500c422 4697
535e0b8e 4698int
971de7fb 4699window_box_text_cols (struct window *w)
535e0b8e 4700{
5500c422 4701 struct frame *f = XFRAME (WINDOW_FRAME (w));
949cf20f 4702 int width = XINT (w->total_cols);
535e0b8e 4703
949cf20f 4704 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
5500c422 4705 /* Scroll bars occupy a few columns. */
949cf20f
KS
4706 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4707 else if (!FRAME_WINDOW_P (f)
4708 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
5500c422
GM
4709 /* The column of `|' characters separating side-by-side windows
4710 occupies one column only. */
4711 width -= 1;
4712
5500c422 4713 if (FRAME_WINDOW_P (f))
949cf20f
KS
4714 /* On window-systems, fringes and display margins cannot be
4715 used for normal text. */
4716 width -= (WINDOW_FRINGE_COLS (w)
4717 + WINDOW_LEFT_MARGIN_COLS (w)
4718 + WINDOW_RIGHT_MARGIN_COLS (w));
111e5992
RS
4719
4720 return width;
535e0b8e
JB
4721}
4722
5500c422
GM
4723\f
4724/************************************************************************
4725 Window Scrolling
4726 ***********************************************************************/
535e0b8e 4727
5500c422 4728/* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
d4e7cf01 4729 N screen-fulls, which is defined as the height of the window minus
5500c422
GM
4730 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4731 instead. Negative values of N mean scroll down. NOERROR non-zero
4732 means don't signal an error if we try to move over BEGV or ZV,
4733 respectively. */
7ab12479 4734
101d1605 4735static void
971de7fb 4736window_scroll (Lisp_Object window, int n, int whole, int noerror)
5500c422 4737{
cba59f77
RS
4738 immediate_quit = 1;
4739
5500c422
GM
4740 /* If we must, use the pixel-based version which is much slower than
4741 the line-based one but can handle varying line heights. */
4742 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4743 window_scroll_pixel_based (window, n, whole, noerror);
4744 else
4745 window_scroll_line_based (window, n, whole, noerror);
cba59f77
RS
4746
4747 immediate_quit = 0;
5500c422
GM
4748}
4749
4750
4751/* Implementation of window_scroll that works based on pixel line
4752 heights. See the comment of window_scroll for parameter
4753 descriptions. */
4754
4755static void
971de7fb 4756window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
5500c422
GM
4757{
4758 struct it it;
4759 struct window *w = XWINDOW (window);
4760 struct text_pos start;
5500c422 4761 int this_scroll_margin;
9d14503e 4762 /* True if we fiddled the window vscroll field without really scrolling. */
5cdb3cf3 4763 int vscrolled = 0;
5a857365 4764 int x, y, rtop, rbot, rowh, vpos;
5500c422
GM
4765
4766 SET_TEXT_POS_FROM_MARKER (start, w->start);
177c0ea7 4767
5500c422 4768 /* If PT is not visible in WINDOW, move back one half of
288d4e06
GM
4769 the screen. Allow PT to be partially visible, otherwise
4770 something like (scroll-down 1) with PT in the line before
4771 the partially visible one would recenter. */
5a857365
KS
4772
4773 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
5500c422
GM
4774 {
4775 /* Move backward half the height of the window. Performance note:
4776 vmotion used here is about 10% faster, but would give wrong
4777 results for variable height lines. */
4778 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4779 it.current_y = it.last_visible_y;
f204989e 4780 move_it_vertically_backward (&it, window_box_height (w) / 2);
177c0ea7 4781
5500c422
GM
4782 /* The function move_iterator_vertically may move over more than
4783 the specified y-distance. If it->w is small, e.g. a
4784 mini-buffer window, we may end up in front of the window's
4785 display area. This is the case when Start displaying at the
4786 start of the line containing PT in this case. */
4787 if (it.current_y <= 0)
4788 {
4789 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
f204989e 4790 move_it_vertically_backward (&it, 0);
5500c422
GM
4791 it.current_y = 0;
4792 }
4793
4794 start = it.current.pos;
4795 }
e56263e5
KS
4796 else if (auto_window_vscroll_p)
4797 {
5a857365 4798 if (rtop || rbot) /* partially visible */
e56263e5
KS
4799 {
4800 int px;
4801 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4802 if (whole)
e856c216
KS
4803 dy = max ((window_box_height (w)
4804 - next_screen_context_lines * dy),
4805 dy);
e56263e5
KS
4806 dy *= n;
4807
5a857365 4808 if (n < 0)
e56263e5 4809 {
5a857365
KS
4810 /* Only vscroll backwards if already vscrolled forwards. */
4811 if (w->vscroll < 0 && rtop > 0)
4812 {
4813 px = max (0, -w->vscroll - min (rtop, -dy));
4814 Fset_window_vscroll (window, make_number (px), Qt);
4815 return;
4816 }
e56263e5 4817 }
5a857365 4818 if (n > 0)
e56263e5 4819 {
5a857365
KS
4820 /* Do vscroll if already vscrolled or only display line. */
4821 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4822 {
4823 px = max (0, -w->vscroll + min (rbot, dy));
4824 Fset_window_vscroll (window, make_number (px), Qt);
4825 return;
4826 }
4827
4828 /* Maybe modify window start instead of scrolling. */
4829 if (rbot > 0 || w->vscroll < 0)
4830 {
2452438f 4831 EMACS_INT spos;
5a857365
KS
4832
4833 Fset_window_vscroll (window, make_number (0), Qt);
4834 /* If there are other text lines above the current row,
4835 move window start to current row. Else to next row. */
4836 if (rbot > 0)
4837 spos = XINT (Fline_beginning_position (Qnil));
4838 else
4839 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4840 set_marker_restricted (w->start, make_number (spos),
4841 w->buffer);
4842 w->start_at_line_beg = Qt;
4843 w->update_mode_line = Qt;
4844 XSETFASTINT (w->last_modified, 0);
4845 XSETFASTINT (w->last_overlay_modified, 0);
4846 /* Set force_start so that redisplay_window will run the
4847 window-scroll-functions. */
4848 w->force_start = Qt;
4849 return;
4850 }
e56263e5
KS
4851 }
4852 }
5a857365 4853 /* Cancel previous vscroll. */
e56263e5
KS
4854 Fset_window_vscroll (window, make_number (0), Qt);
4855 }
5500c422 4856
d0c38d63 4857 /* If scroll_preserve_screen_position is non-nil, we try to set
5500c422
GM
4858 point in the same window line as it is now, so get that line. */
4859 if (!NILP (Vscroll_preserve_screen_position))
4860 {
c525d842 4861 /* We preserve the goal pixel coordinate across consecutive
a4b000fb
JL
4862 calls to scroll-up, scroll-down and other commands that
4863 have the `scroll-command' property. This avoids the
c525d842
CY
4864 possibility of point becoming "stuck" on a tall line when
4865 scrolling by one line. */
66fe93d1 4866 if (window_scroll_pixel_based_preserve_y < 0
a4b000fb
JL
4867 || !SYMBOLP (current_kboard->Vlast_command)
4868 || NILP (Fget (current_kboard->Vlast_command, Qscroll_command)))
c525d842
CY
4869 {
4870 start_display (&it, w, start);
4871 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
66fe93d1 4872 window_scroll_pixel_based_preserve_y = it.current_y;
c876b227 4873 window_scroll_pixel_based_preserve_x = it.current_x;
c525d842 4874 }
5500c422
GM
4875 }
4876 else
c876b227
SM
4877 window_scroll_pixel_based_preserve_y
4878 = window_scroll_pixel_based_preserve_x = -1;
5500c422
GM
4879
4880 /* Move iterator it from start the specified distance forward or
4881 backward. The result is the new window start. */
4882 start_display (&it, w, start);
4883 if (whole)
4884 {
2452438f 4885 EMACS_INT start_pos = IT_CHARPOS (it);
e856c216
KS
4886 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4887 dy = max ((window_box_height (w)
4888 - next_screen_context_lines * dy),
4889 dy) * n;
d72340d4
GM
4890
4891 /* Note that move_it_vertically always moves the iterator to the
4892 start of a line. So, if the last line doesn't have a newline,
4893 we would end up at the start of the line ending at ZV. */
4894 if (dy <= 0)
e856c216
KS
4895 {
4896 move_it_vertically_backward (&it, -dy);
5a857365 4897 /* Ensure we actually do move, e.g. in case we are currently
e856c216
KS
4898 looking at an image that is taller that the window height. */
4899 while (start_pos == IT_CHARPOS (it)
4900 && start_pos > BEGV)
4901 move_it_by_lines (&it, -1, 1);
4902 }
d72340d4 4903 else if (dy > 0)
bed83ee4 4904 {
bed83ee4
KS
4905 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4906 MOVE_TO_POS | MOVE_TO_Y);
5a857365 4907 /* Ensure we actually do move, e.g. in case we are currently
bed83ee4
KS
4908 looking at an image that is taller that the window height. */
4909 while (start_pos == IT_CHARPOS (it)
4910 && start_pos < ZV)
4911 move_it_by_lines (&it, 1, 1);
4912 }
5500c422
GM
4913 }
4914 else
4915 move_it_by_lines (&it, n, 1);
4916
96ae58c8
RS
4917 /* We failed if we find ZV is already on the screen (scrolling up,
4918 means there's nothing past the end), or if we can't start any
4919 earlier (scrolling down, means there's nothing past the top). */
5500c422 4920 if ((n > 0 && IT_CHARPOS (it) == ZV)
96ae58c8 4921 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5500c422 4922 {
5cdb3cf3
MB
4923 if (IT_CHARPOS (it) == ZV)
4924 {
07ce8b53
RS
4925 if (it.current_y < it.last_visible_y
4926 && (it.current_y + it.max_ascent + it.max_descent
3f489dc7 4927 > it.last_visible_y))
a74eca50
GM
4928 {
4929 /* The last line was only partially visible, make it fully
4930 visible. */
4931 w->vscroll = (it.last_visible_y
4932 - it.current_y + it.max_ascent + it.max_descent);
4933 adjust_glyphs (it.f);
4934 }
5cdb3cf3
MB
4935 else if (noerror)
4936 return;
cf402f3f 4937 else if (n < 0) /* could happen with empty buffers */
ba96a5cf 4938 xsignal0 (Qbeginning_of_buffer);
5cdb3cf3 4939 else
ba96a5cf 4940 xsignal0 (Qend_of_buffer);
5cdb3cf3 4941 }
5500c422 4942 else
5cdb3cf3
MB
4943 {
4944 if (w->vscroll != 0)
4945 /* The first line was only partially visible, make it fully
4946 visible. */
4947 w->vscroll = 0;
4948 else if (noerror)
4949 return;
4950 else
ba96a5cf 4951 xsignal0 (Qbeginning_of_buffer);
5cdb3cf3
MB
4952 }
4953
4954 /* If control gets here, then we vscrolled. */
4955
4956 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4957
4958 /* Don't try to change the window start below. */
4959 vscrolled = 1;
5500c422
GM
4960 }
4961
5cdb3cf3
MB
4962 if (! vscrolled)
4963 {
2452438f
EZ
4964 EMACS_INT pos = IT_CHARPOS (it);
4965 EMACS_INT bytepos;
e68def1e
AS
4966
4967 /* If in the middle of a multi-glyph character move forward to
4968 the next character. */
4969 if (in_display_vector_p (&it))
4970 {
4971 ++pos;
4972 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4973 }
4974
5cdb3cf3 4975 /* Set the window start, and set up the window for redisplay. */
dad67609 4976 set_marker_restricted (w->start, make_number (pos),
5cdb3cf3 4977 w->buffer);
dad67609
RS
4978 bytepos = XMARKER (w->start)->bytepos;
4979 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4980 ? Qt : Qnil);
5cdb3cf3
MB
4981 w->update_mode_line = Qt;
4982 XSETFASTINT (w->last_modified, 0);
4983 XSETFASTINT (w->last_overlay_modified, 0);
4984 /* Set force_start so that redisplay_window will run the
4985 window-scroll-functions. */
4986 w->force_start = Qt;
4987 }
177c0ea7 4988
dc297565
RS
4989 /* The rest of this function uses current_y in a nonstandard way,
4990 not including the height of the header line if any. */
5500c422 4991 it.current_y = it.vpos = 0;
177c0ea7 4992
940f53e5
RS
4993 /* Move PT out of scroll margins.
4994 This code wants current_y to be zero at the window start position
4995 even if there is a header line. */
4996 this_scroll_margin = max (0, scroll_margin);
4997 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4998 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4999
5000 if (n > 0)
5500c422 5001 {
940f53e5
RS
5002 /* We moved the window start towards ZV, so PT may be now
5003 in the scroll margin at the top. */
5004 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
bdf4ec93
RS
5005 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5006 && (NILP (Vscroll_preserve_screen_position)
5007 || EQ (Vscroll_preserve_screen_position, Qt)))
940f53e5
RS
5008 /* We found PT at a legitimate height. Leave it alone. */
5009 ;
66fe93d1 5010 else if (window_scroll_pixel_based_preserve_y >= 0)
940f53e5 5011 {
fa3c3426
RS
5012 /* If we have a header line, take account of it.
5013 This is necessary because we set it.current_y to 0, above. */
c876b227
SM
5014 move_it_to (&it, -1,
5015 window_scroll_pixel_based_preserve_x,
66fe93d1
LT
5016 window_scroll_pixel_based_preserve_y
5017 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
c876b227 5018 -1, MOVE_TO_Y | MOVE_TO_X);
940f53e5
RS
5019 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5020 }
5021 else
5500c422 5022 {
5500c422 5023 while (it.current_y < this_scroll_margin)
e9b2c961
RS
5024 {
5025 int prev = it.current_y;
5026 move_it_by_lines (&it, 1, 1);
5027 if (prev == it.current_y)
5028 break;
5029 }
5500c422
GM
5030 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5031 }
940f53e5
RS
5032 }
5033 else if (n < 0)
5034 {
2452438f 5035 EMACS_INT charpos, bytepos;
aed328bf 5036 int partial_p;
940f53e5 5037
66fe93d1
LT
5038 /* Save our position, for the
5039 window_scroll_pixel_based_preserve_y case. */
940f53e5
RS
5040 charpos = IT_CHARPOS (it);
5041 bytepos = IT_BYTEPOS (it);
5cdb3cf3 5042
940f53e5
RS
5043 /* We moved the window start towards BEGV, so PT may be now
5044 in the scroll margin at the bottom. */
5045 move_it_to (&it, PT, -1,
7ad53239
RS
5046 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5047 - this_scroll_margin - 1),
5048 -1,
940f53e5
RS
5049 MOVE_TO_POS | MOVE_TO_Y);
5050
aed328bf
KS
5051 /* Save our position, in case it's correct. */
5052 charpos = IT_CHARPOS (it);
5053 bytepos = IT_BYTEPOS (it);
5054
5055 /* See if point is on a partially visible line at the end. */
5056 if (it.what == IT_EOB)
5057 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5058 else
5059 {
5060 move_it_by_lines (&it, 1, 1);
5061 partial_p = it.current_y > it.last_visible_y;
5062 }
5063
bdf4ec93
RS
5064 if (charpos == PT && !partial_p
5065 && (NILP (Vscroll_preserve_screen_position)
5066 || EQ (Vscroll_preserve_screen_position, Qt)))
940f53e5
RS
5067 /* We found PT before we found the display margin, so PT is ok. */
5068 ;
66fe93d1 5069 else if (window_scroll_pixel_based_preserve_y >= 0)
940f53e5
RS
5070 {
5071 SET_TEXT_POS_FROM_MARKER (start, w->start);
5072 start_display (&it, w, start);
c525d842
CY
5073 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5074 here because we called start_display again and did not
5075 alter it.current_y this time. */
c876b227
SM
5076 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5077 window_scroll_pixel_based_preserve_y, -1,
5078 MOVE_TO_Y | MOVE_TO_X);
940f53e5
RS
5079 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5080 }
5081 else
5082 {
aed328bf 5083 if (partial_p)
5cdb3cf3
MB
5084 /* The last line was only partially visible, so back up two
5085 lines to make sure we're on a fully visible line. */
5086 {
5087 move_it_by_lines (&it, -2, 0);
5088 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5089 }
5090 else
5091 /* No, the position we saved is OK, so use it. */
5092 SET_PT_BOTH (charpos, bytepos);
5500c422
GM
5093 }
5094 }
5095}
5096
5097
5098/* Implementation of window_scroll that works based on screen lines.
5099 See the comment of window_scroll for parameter descriptions. */
5100
5101static void
971de7fb 5102window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
7ab12479
JB
5103{
5104 register struct window *w = XWINDOW (window);
2452438f
EZ
5105 register EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
5106 register EMACS_INT pos, pos_byte;
7ab12479
JB
5107 register int ht = window_internal_height (w);
5108 register Lisp_Object tem;
5109 int lose;
5500c422 5110 Lisp_Object bolp;
2452438f 5111 EMACS_INT startpos;
c876b227 5112 Lisp_Object original_pos = Qnil;
101d1605 5113
d4e7cf01
GM
5114 /* If scrolling screen-fulls, compute the number of lines to
5115 scroll from the window's height. */
5116 if (whole)
5117 n *= max (1, ht - next_screen_context_lines);
5118
101d1605
RS
5119 startpos = marker_position (w->start);
5120
c876b227
SM
5121 if (!NILP (Vscroll_preserve_screen_position))
5122 {
5123 if (window_scroll_preserve_vpos <= 0
a4b000fb
JL
5124 || !SYMBOLP (current_kboard->Vlast_command)
5125 || NILP (Fget (current_kboard->Vlast_command, Qscroll_command)))
c876b227
SM
5126 {
5127 struct position posit
5128 = *compute_motion (startpos, 0, 0, 0,
5129 PT, ht, 0,
5130 -1, XINT (w->hscroll),
5131 0, w);
5132 window_scroll_preserve_vpos = posit.vpos;
5133 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5134 }
5135
5136 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5137 make_number (window_scroll_preserve_vpos));
5138 }
0a1f771a 5139
d834a2e9 5140 XSETFASTINT (tem, PT);
6ffdb539 5141 tem = Fpos_visible_in_window_p (tem, window, Qnil);
7ab12479 5142
265a9e55 5143 if (NILP (tem))
7ab12479 5144 {
cd2be1dd 5145 Fvertical_motion (make_number (- (ht / 2)), window);
345d45b2 5146 startpos = PT;
7ab12479
JB
5147 }
5148
345d45b2 5149 SET_PT (startpos);
5ce7b543 5150 lose = n < 0 && PT == BEGV;
540b6aa0 5151 Fvertical_motion (make_number (n), window);
5ce7b543 5152 pos = PT;
b73ea88e 5153 pos_byte = PT_BYTE;
7ab12479 5154 bolp = Fbolp ();
b73ea88e 5155 SET_PT_BOTH (opoint, opoint_byte);
7ab12479
JB
5156
5157 if (lose)
f8026fd8
JB
5158 {
5159 if (noerror)
5160 return;
5161 else
ba96a5cf 5162 xsignal0 (Qbeginning_of_buffer);
f8026fd8 5163 }
7ab12479
JB
5164
5165 if (pos < ZV)
7ab12479 5166 {
0c7da84e
RS
5167 int this_scroll_margin = scroll_margin;
5168
5169 /* Don't use a scroll margin that is negative or too large. */
5170 if (this_scroll_margin < 0)
5171 this_scroll_margin = 0;
5172
949cf20f
KS
5173 if (XINT (w->total_lines) < 4 * scroll_margin)
5174 this_scroll_margin = XINT (w->total_lines) / 4;
0c7da84e 5175
b73ea88e 5176 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
7ab12479
JB
5177 w->start_at_line_beg = bolp;
5178 w->update_mode_line = Qt;
d834a2e9 5179 XSETFASTINT (w->last_modified, 0);
3cd21523 5180 XSETFASTINT (w->last_overlay_modified, 0);
345d45b2
RS
5181 /* Set force_start so that redisplay_window will run
5182 the window-scroll-functions. */
5183 w->force_start = Qt;
0c7da84e 5184
bdf4ec93
RS
5185 if (!NILP (Vscroll_preserve_screen_position)
5186 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
0c7da84e 5187 {
b73ea88e 5188 SET_PT_BOTH (pos, pos_byte);
c876b227 5189 Fvertical_motion (original_pos, window);
0c7da84e 5190 }
101d1605
RS
5191 /* If we scrolled forward, put point enough lines down
5192 that it is outside the scroll margin. */
5193 else if (n > 0)
0c7da84e 5194 {
101d1605
RS
5195 int top_margin;
5196
5197 if (this_scroll_margin > 0)
5198 {
b73ea88e 5199 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
5200 Fvertical_motion (make_number (this_scroll_margin), window);
5201 top_margin = PT;
5202 }
5203 else
5204 top_margin = pos;
5205
5206 if (top_margin <= opoint)
b73ea88e 5207 SET_PT_BOTH (opoint, opoint_byte);
5500c422 5208 else if (!NILP (Vscroll_preserve_screen_position))
101d1605 5209 {
b73ea88e 5210 SET_PT_BOTH (pos, pos_byte);
c876b227 5211 Fvertical_motion (original_pos, window);
101d1605 5212 }
9317a85d 5213 else
335406fc 5214 SET_PT (top_margin);
0c7da84e 5215 }
101d1605 5216 else if (n < 0)
7ab12479 5217 {
101d1605
RS
5218 int bottom_margin;
5219
0c7da84e
RS
5220 /* If we scrolled backward, put point near the end of the window
5221 but not within the scroll margin. */
b73ea88e 5222 SET_PT_BOTH (pos, pos_byte);
0c7da84e 5223 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
101d1605
RS
5224 if (XFASTINT (tem) == ht - this_scroll_margin)
5225 bottom_margin = PT;
5226 else
5227 bottom_margin = PT + 1;
5228
5229 if (bottom_margin > opoint)
b73ea88e 5230 SET_PT_BOTH (opoint, opoint_byte);
7ab12479 5231 else
101d1605 5232 {
5500c422 5233 if (!NILP (Vscroll_preserve_screen_position))
9317a85d 5234 {
b73ea88e 5235 SET_PT_BOTH (pos, pos_byte);
c876b227 5236 Fvertical_motion (original_pos, window);
9317a85d
RS
5237 }
5238 else
5239 Fvertical_motion (make_number (-1), window);
101d1605 5240 }
7ab12479
JB
5241 }
5242 }
5243 else
f8026fd8
JB
5244 {
5245 if (noerror)
5246 return;
5247 else
ba96a5cf 5248 xsignal0 (Qend_of_buffer);
f8026fd8 5249 }
7ab12479 5250}
5500c422
GM
5251
5252
5253/* Scroll selected_window up or down. If N is nil, scroll a
5254 screen-full which is defined as the height of the window minus
5255 next_screen_context_lines. If N is the symbol `-', scroll.
5256 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5257 up. This is the guts of Fscroll_up and Fscroll_down. */
7ab12479
JB
5258
5259static void
971de7fb 5260scroll_command (Lisp_Object n, int direction)
7ab12479 5261{
331379bf 5262 int count = SPECPDL_INDEX ();
7ab12479 5263
1ea40aa2 5264 xassert (eabs (direction) == 1);
5500c422
GM
5265
5266 /* If selected window's buffer isn't current, make it current for
5267 the moment. But don't screw up if window_scroll gets an error. */
7ab12479 5268 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
95605e15
JB
5269 {
5270 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5271 Fset_buffer (XWINDOW (selected_window)->buffer);
5500c422
GM
5272
5273 /* Make redisplay consider other windows than just selected_window. */
5274 ++windows_or_buffers_changed;
95605e15 5275 }
7ab12479 5276
265a9e55 5277 if (NILP (n))
d4e7cf01 5278 window_scroll (selected_window, direction, 1, 0);
7ab12479 5279 else if (EQ (n, Qminus))
d4e7cf01 5280 window_scroll (selected_window, -direction, 1, 0);
7ab12479
JB
5281 else
5282 {
5283 n = Fprefix_numeric_value (n);
101d1605 5284 window_scroll (selected_window, XINT (n) * direction, 0, 0);
7ab12479 5285 }
95605e15
JB
5286
5287 unbind_to (count, Qnil);
7ab12479
JB
5288}
5289
e0965597 5290DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
12bb3111 5291 doc: /* Scroll text of selected window upward ARG lines.
a0a37a6f 5292If ARG is omitted or nil, scroll upward by a near full screen.
fdb82f93
PJ
5293A near full screen is `next-screen-context-lines' less than a full screen.
5294Negative ARG means scroll downward.
5295If ARG is the atom `-', scroll downward by nearly full screen.
5296When calling from a program, supply as argument a number, nil, or `-'. */)
5842a27b 5297 (Lisp_Object arg)
7ab12479 5298{
413430c5 5299 scroll_command (arg, 1);
7ab12479
JB
5300 return Qnil;
5301}
5302
e0965597 5303DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
12bb3111 5304 doc: /* Scroll text of selected window down ARG lines.
a0a37a6f 5305If ARG is omitted or nil, scroll down by a near full screen.
fdb82f93
PJ
5306A near full screen is `next-screen-context-lines' less than a full screen.
5307Negative ARG means scroll upward.
5308If ARG is the atom `-', scroll upward by nearly full screen.
5309When calling from a program, supply as argument a number, nil, or `-'. */)
5842a27b 5310 (Lisp_Object arg)
7ab12479 5311{
413430c5 5312 scroll_command (arg, -1);
7ab12479
JB
5313 return Qnil;
5314}
ccd0664b
RS
5315\f
5316DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
fdb82f93 5317 doc: /* Return the other window for \"other window scroll\" commands.
fdb82f93 5318If `other-window-scroll-buffer' is non-nil, a window
a0a37a6f
LT
5319showing that buffer is used.
5320If in the minibuffer, `minibuffer-scroll-window' if non-nil
5321specifies the window. This takes precedence over
5322`other-window-scroll-buffer'. */)
5842a27b 5323 (void)
7ab12479 5324{
ccd0664b 5325 Lisp_Object window;
7ab12479
JB
5326
5327 if (MINI_WINDOW_P (XWINDOW (selected_window))
265a9e55 5328 && !NILP (Vminibuf_scroll_window))
7ab12479
JB
5329 window = Vminibuf_scroll_window;
5330 /* If buffer is specified, scroll that buffer. */
265a9e55 5331 else if (!NILP (Vother_window_scroll_buffer))
7ab12479
JB
5332 {
5333 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
265a9e55 5334 if (NILP (window))
87478b52 5335 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
7ab12479
JB
5336 }
5337 else
dbc4e1c1
JB
5338 {
5339 /* Nothing specified; look for a neighboring window on the same
5340 frame. */
5341 window = Fnext_window (selected_window, Qnil, Qnil);
5342
5343 if (EQ (window, selected_window))
5344 /* That didn't get us anywhere; look for a window on another
5345 visible frame. */
5346 do
5347 window = Fnext_window (window, Qnil, Qt);
5348 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5349 && ! EQ (window, selected_window));
5350 }
5351
b7826503 5352 CHECK_LIVE_WINDOW (window);
7ab12479
JB
5353
5354 if (EQ (window, selected_window))
5355 error ("There is no other window");
5356
ccd0664b
RS
5357 return window;
5358}
5359
5360DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
fdb82f93
PJ
5361 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5362A near full screen is `next-screen-context-lines' less than a full screen.
5363The next window is the one below the current one; or the one at the top
5364if the current one is at the bottom. Negative ARG means scroll downward.
5365If ARG is the atom `-', scroll downward by nearly full screen.
5366When calling from a program, supply as argument a number, nil, or `-'.
5367
fdb82f93 5368If `other-window-scroll-buffer' is non-nil, scroll the window
a0a37a6f
LT
5369showing that buffer, popping the buffer up if necessary.
5370If in the minibuffer, `minibuffer-scroll-window' if non-nil
5371specifies the window to scroll. This takes precedence over
5372`other-window-scroll-buffer'. */)
5842a27b 5373 (Lisp_Object arg)
ccd0664b 5374{
d4e7cf01
GM
5375 Lisp_Object window;
5376 struct window *w;
331379bf 5377 int count = SPECPDL_INDEX ();
ccd0664b
RS
5378
5379 window = Fother_window_for_scrolling ();
7ab12479 5380 w = XWINDOW (window);
7ab12479
JB
5381
5382 /* Don't screw up if window_scroll gets an error. */
5383 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5500c422 5384 ++windows_or_buffers_changed;
7ab12479
JB
5385
5386 Fset_buffer (w->buffer);
5387 SET_PT (marker_position (w->pointm));
5388
413430c5 5389 if (NILP (arg))
d4e7cf01 5390 window_scroll (window, 1, 1, 1);
413430c5 5391 else if (EQ (arg, Qminus))
d4e7cf01 5392 window_scroll (window, -1, 1, 1);
7ab12479
JB
5393 else
5394 {
413430c5
EN
5395 if (CONSP (arg))
5396 arg = Fcar (arg);
b7826503 5397 CHECK_NUMBER (arg);
101d1605 5398 window_scroll (window, XINT (arg), 0, 1);
7ab12479
JB
5399 }
5400
b73ea88e 5401 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
f4e7b2c2 5402 unbind_to (count, Qnil);
7ab12479
JB
5403
5404 return Qnil;
5405}
5406\f
e0965597 5407DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
fdb82f93 5408 doc: /* Scroll selected window display ARG columns left.
2fe41216
EZ
5409Default for ARG is window width minus 2.
5410Value is the total amount of leftward horizontal scrolling in
5411effect after the change.
23fe745a 5412If SET-MINIMUM is non-nil, the new scroll amount becomes the
dc297565 5413lower bound for automatic scrolling, i.e. automatic scrolling
2fe41216 5414will not scroll a window to a column less than the value returned
dc297565 5415by this function. This happens in an interactive call. */)
5842a27b 5416 (register Lisp_Object arg, Lisp_Object set_minimum)
7ab12479 5417{
c67fa410
GM
5418 Lisp_Object result;
5419 int hscroll;
5420 struct window *w = XWINDOW (selected_window);
177c0ea7 5421
265a9e55 5422 if (NILP (arg))
949cf20f 5423 XSETFASTINT (arg, window_box_text_cols (w) - 2);
7ab12479
JB
5424 else
5425 arg = Fprefix_numeric_value (arg);
5426
c67fa410
GM
5427 hscroll = XINT (w->hscroll) + XINT (arg);
5428 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5429
dc297565 5430 if (!NILP (set_minimum))
c67fa410
GM
5431 w->min_hscroll = w->hscroll;
5432
5433 return result;
7ab12479
JB
5434}
5435
e0965597 5436DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
fdb82f93 5437 doc: /* Scroll selected window display ARG columns right.
2fe41216
EZ
5438Default for ARG is window width minus 2.
5439Value is the total amount of leftward horizontal scrolling in
5440effect after the change.
23fe745a 5441If SET-MINIMUM is non-nil, the new scroll amount becomes the
dc297565 5442lower bound for automatic scrolling, i.e. automatic scrolling
2fe41216 5443will not scroll a window to a column less than the value returned
dc297565 5444by this function. This happens in an interactive call. */)
5842a27b 5445 (register Lisp_Object arg, Lisp_Object set_minimum)
7ab12479 5446{
c67fa410
GM
5447 Lisp_Object result;
5448 int hscroll;
5449 struct window *w = XWINDOW (selected_window);
177c0ea7 5450
265a9e55 5451 if (NILP (arg))
949cf20f 5452 XSETFASTINT (arg, window_box_text_cols (w) - 2);
7ab12479
JB
5453 else
5454 arg = Fprefix_numeric_value (arg);
5455
c67fa410
GM
5456 hscroll = XINT (w->hscroll) - XINT (arg);
5457 result = Fset_window_hscroll (selected_window, make_number (hscroll));
177c0ea7 5458
dc297565 5459 if (!NILP (set_minimum))
c67fa410
GM
5460 w->min_hscroll = w->hscroll;
5461
5462 return result;
7ab12479
JB
5463}
5464
fa832261
KS
5465DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5466 doc: /* Return the window which was selected when entering the minibuffer.
12bb3111 5467Returns nil, if selected window is not a minibuffer window. */)
5842a27b 5468 (void)
fa832261
KS
5469{
5470 if (minibuf_level > 0
5471 && MINI_WINDOW_P (XWINDOW (selected_window))
fa832261
KS
5472 && WINDOW_LIVE_P (minibuf_selected_window))
5473 return minibuf_selected_window;
5474
5475 return Qnil;
5476}
5477
12c8b416
GM
5478/* Value is the number of lines actually displayed in window W,
5479 as opposed to its height. */
5480
5481static int
971de7fb 5482displayed_window_lines (struct window *w)
12c8b416
GM
5483{
5484 struct it it;
5485 struct text_pos start;
5486 int height = window_box_height (w);
5487 struct buffer *old_buffer;
5488 int bottom_y;
5489
5490 if (XBUFFER (w->buffer) != current_buffer)
5491 {
5492 old_buffer = current_buffer;
5493 set_buffer_internal (XBUFFER (w->buffer));
5494 }
5495 else
5496 old_buffer = NULL;
5497
521b203e
GM
5498 /* In case W->start is out of the accessible range, do something
5499 reasonable. This happens in Info mode when Info-scroll-down
5500 calls (recenter -1) while W->start is 1. */
5501 if (XMARKER (w->start)->charpos < BEGV)
5502 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5503 else if (XMARKER (w->start)->charpos > ZV)
5504 SET_TEXT_POS (start, ZV, ZV_BYTE);
5505 else
5506 SET_TEXT_POS_FROM_MARKER (start, w->start);
5507
12c8b416
GM
5508 start_display (&it, w, start);
5509 move_it_vertically (&it, height);
c8bc6f65 5510 bottom_y = line_bottom_y (&it);
12c8b416 5511
1de65f51
RS
5512 /* rms: On a non-window display,
5513 the value of it.vpos at the bottom of the screen
5514 seems to be 1 larger than window_box_height (w).
5515 This kludge fixes a bug whereby (move-to-window-line -1)
5516 when ZV is on the last screen line
5517 moves to the previous screen line instead of the last one. */
5518 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5519 height++;
5520
12c8b416
GM
5521 /* Add in empty lines at the bottom of the window. */
5522 if (bottom_y < height)
5523 {
949cf20f 5524 int uy = FRAME_LINE_HEIGHT (it.f);
c8bc6f65 5525 it.vpos += (height - bottom_y + uy - 1) / uy;
12c8b416
GM
5526 }
5527
c8bc6f65
GM
5528 if (old_buffer)
5529 set_buffer_internal (old_buffer);
5530
12c8b416
GM
5531 return it.vpos;
5532}
5533
5534
7ab12479 5535DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
666e158e 5536 doc: /* Center point in selected window and maybe redisplay frame.
fdb82f93 5537With prefix argument ARG, recenter putting point on screen line ARG
12bb3111 5538relative to the selected window. If ARG is negative, it counts up from the
fdb82f93
PJ
5539bottom of the window. (ARG should be less than the height of the window.)
5540
666e158e
MB
5541If ARG is omitted or nil, then recenter with point on the middle line of
5542the selected window; if the variable `recenter-redisplay' is non-nil,
5543also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5544is set to `grow-only', this resets the tool-bar's height to the minimum
5545height needed); if `recenter-redisplay' has the special value `tty',
5546then only tty frame are redrawn.
7d1d98ee 5547
fdb82f93
PJ
5548Just C-u as prefix means put point in the center of the window
5549and redisplay normally--don't erase and redraw the frame. */)
5842a27b 5550 (register Lisp_Object arg)
7ab12479 5551{
6df47b59 5552 struct window *w = XWINDOW (selected_window);
478292ed
RS
5553 struct buffer *buf = XBUFFER (w->buffer);
5554 struct buffer *obuf = current_buffer;
6df47b59 5555 int center_p = 0;
2452438f 5556 EMACS_INT charpos, bytepos;
f6b43440
RS
5557 int iarg;
5558 int this_scroll_margin;
7ab12479 5559
0fa5d25b
RS
5560 /* If redisplay is suppressed due to an error, try again. */
5561 obuf->display_error_modiff = 0;
5562
413430c5 5563 if (NILP (arg))
7ab12479 5564 {
666e158e
MB
5565 if (!NILP (Vrecenter_redisplay)
5566 && (!EQ (Vrecenter_redisplay, Qtty)
5567 || !NILP (Ftty_type (selected_frame))))
5568 {
5569 int i;
f02d6d5c 5570
666e158e
MB
5571 /* Invalidate pixel data calculated for all compositions. */
5572 for (i = 0; i < n_compositions; i++)
5573 composition_table[i]->font = NULL;
7ab12479 5574
666e158e
MB
5575 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5576
5577 Fredraw_frame (WINDOW_FRAME (w));
5578 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5579 }
7d1d98ee 5580
6df47b59 5581 center_p = 1;
7ab12479 5582 }
413430c5 5583 else if (CONSP (arg)) /* Just C-u. */
6df47b59 5584 center_p = 1;
7ab12479
JB
5585 else
5586 {
413430c5 5587 arg = Fprefix_numeric_value (arg);
b7826503 5588 CHECK_NUMBER (arg);
ae12ecd7 5589 iarg = XINT (arg);
7ab12479
JB
5590 }
5591
478292ed 5592 set_buffer_internal (buf);
7ab12479 5593
f6b43440
RS
5594 /* Do this after making BUF current
5595 in case scroll_margin is buffer-local. */
5596 this_scroll_margin = max (0, scroll_margin);
5597 this_scroll_margin = min (this_scroll_margin,
5598 XFASTINT (w->total_lines) / 4);
5599
521b203e 5600 /* Handle centering on a graphical frame specially. Such frames can
6df47b59
GM
5601 have variable-height lines and centering point on the basis of
5602 line counts would lead to strange effects. */
521b203e 5603 if (FRAME_WINDOW_P (XFRAME (w->frame)))
6df47b59 5604 {
6df47b59
GM
5605 if (center_p)
5606 {
521b203e
GM
5607 struct it it;
5608 struct text_pos pt;
177c0ea7 5609
521b203e
GM
5610 SET_TEXT_POS (pt, PT, PT_BYTE);
5611 start_display (&it, w, pt);
f204989e 5612 move_it_vertically_backward (&it, window_box_height (w) / 2);
521b203e
GM
5613 charpos = IT_CHARPOS (it);
5614 bytepos = IT_BYTEPOS (it);
6df47b59 5615 }
f6b43440 5616 else if (iarg < 0)
6df47b59 5617 {
521b203e
GM
5618 struct it it;
5619 struct text_pos pt;
f6b43440 5620 int nlines = -iarg;
f204989e
KS
5621 int extra_line_spacing;
5622 int h = window_box_height (w);
177c0ea7 5623
f6b43440
RS
5624 iarg = - max (-iarg, this_scroll_margin);
5625
521b203e
GM
5626 SET_TEXT_POS (pt, PT, PT_BYTE);
5627 start_display (&it, w, pt);
f204989e
KS
5628
5629 /* Be sure we have the exact height of the full line containing PT. */
5630 move_it_by_lines (&it, 0, 1);
521b203e 5631
d466fa4d 5632 /* The amount of pixels we have to move back is the window
521b203e
GM
5633 height minus what's displayed in the line containing PT,
5634 and the lines below. */
f204989e
KS
5635 it.current_y = 0;
5636 it.vpos = 0;
d466fa4d
GM
5637 move_it_by_lines (&it, nlines, 1);
5638
f204989e
KS
5639 if (it.vpos == nlines)
5640 h -= it.current_y;
5641 else
5642 {
5643 /* Last line has no newline */
5644 h -= line_bottom_y (&it);
5645 it.vpos++;
5646 }
5647
5648 /* Don't reserve space for extra line spacing of last line. */
5649 extra_line_spacing = it.max_extra_line_spacing;
d466fa4d
GM
5650
5651 /* If we can't move down NLINES lines because we hit
5652 the end of the buffer, count in some empty lines. */
5653 if (it.vpos < nlines)
f204989e
KS
5654 {
5655 nlines -= it.vpos;
5656 extra_line_spacing = it.extra_line_spacing;
5657 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5658 }
5659 if (h <= 0)
5660 return Qnil;
201c831a 5661
f204989e 5662 /* Now find the new top line (starting position) of the window. */
521b203e 5663 start_display (&it, w, pt);
f204989e
KS
5664 it.current_y = 0;
5665 move_it_vertically_backward (&it, h);
5666
5667 /* If extra line spacing is present, we may move too far
5668 back. This causes the last line to be only partially
5669 visible (which triggers redisplay to recenter that line
5670 in the middle), so move forward.
5671 But ignore extra line spacing on last line, as it is not
5672 considered to be part of the visible height of the line.
5673 */
5674 h += extra_line_spacing;
5675 while (-it.current_y > h)
5676 move_it_by_lines (&it, 1, 1);
5677
521b203e
GM
5678 charpos = IT_CHARPOS (it);
5679 bytepos = IT_BYTEPOS (it);
6df47b59 5680 }
521b203e
GM
5681 else
5682 {
5683 struct position pos;
f6b43440 5684
f6b43440
RS
5685 iarg = max (iarg, this_scroll_margin);
5686
5687 pos = *vmotion (PT, -iarg, w);
521b203e
GM
5688 charpos = pos.bufpos;
5689 bytepos = pos.bytepos;
5690 }
5691 }
5692 else
5693 {
5694 struct position pos;
5695 int ht = window_internal_height (w);
5696
5697 if (center_p)
a4429c5b 5698 iarg = ht / 2;
f567c488
KS
5699 else if (iarg < 0)
5700 iarg += ht;
f6b43440
RS
5701
5702 /* Don't let it get into the margin at either top or bottom. */
5703 iarg = max (iarg, this_scroll_margin);
5704 iarg = min (iarg, ht - this_scroll_margin - 1);
177c0ea7 5705
f6b43440 5706 pos = *vmotion (PT, - iarg, w);
6df47b59
GM
5707 charpos = pos.bufpos;
5708 bytepos = pos.bytepos;
5709 }
5710
5711 /* Set the new window start. */
5712 set_marker_both (w->start, w->buffer, charpos, bytepos);
2f3cad6c 5713 w->window_end_valid = Qnil;
177c0ea7 5714
95605b1b
RS
5715 w->optional_new_start = Qt;
5716
6df47b59
GM
5717 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5718 w->start_at_line_beg = Qt;
5719 else
5720 w->start_at_line_beg = Qnil;
177c0ea7 5721
478292ed 5722 set_buffer_internal (obuf);
7ab12479
JB
5723 return Qnil;
5724}
b7617575
GM
5725
5726
81fe0836 5727DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
fdb82f93
PJ
5728 0, 1, 0,
5729 doc: /* Return the height in lines of the text display area of WINDOW.
c85322d0 5730WINDOW defaults to the selected window.
8fef9de1 5731
f039b2d2
CY
5732The return value does not include the mode line, any header line, nor
5733any partial-height lines in the text display area. */)
5842a27b 5734 (Lisp_Object window)
81fe0836
MB
5735{
5736 struct window *w = decode_window (window);
5737 int pixel_height = window_box_height (w);
949cf20f 5738 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
81fe0836
MB
5739 return make_number (line_height);
5740}
5741
5742
7ab12479
JB
5743\f
5744DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
fdb82f93
PJ
5745 1, 1, "P",
5746 doc: /* Position point relative to window.
5747With no argument, position point at center of window.
5748An argument specifies vertical position within the window;
5749zero means top of window, negative means relative to bottom of window. */)
5842a27b 5750 (Lisp_Object arg)
7ab12479 5751{
b7617575
GM
5752 struct window *w = XWINDOW (selected_window);
5753 int lines, start;
540b6aa0 5754 Lisp_Object window;
f6b43440
RS
5755#if 0
5756 int this_scroll_margin;
5757#endif
7ab12479 5758
b7617575 5759 window = selected_window;
7ab12479
JB
5760 start = marker_position (w->start);
5761 if (start < BEGV || start > ZV)
5762 {
b7617575 5763 int height = window_internal_height (w);
cd2be1dd 5764 Fvertical_motion (make_number (- (height / 2)), window);
b73ea88e 5765 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
7ab12479
JB
5766 w->start_at_line_beg = Fbolp ();
5767 w->force_start = Qt;
5768 }
5769 else
b73ea88e 5770 Fgoto_char (w->start);
7ab12479 5771
b7617575 5772 lines = displayed_window_lines (w);
f6b43440
RS
5773
5774#if 0
5775 this_scroll_margin = max (0, scroll_margin);
5776 this_scroll_margin = min (this_scroll_margin, lines / 4);
5777#endif
5778
b7617575
GM
5779 if (NILP (arg))
5780 XSETFASTINT (arg, lines / 2);
5781 else
5782 {
f6b43440
RS
5783 int iarg = XINT (Fprefix_numeric_value (arg));
5784
5785 if (iarg < 0)
5786 iarg = iarg + lines;
5787
5788#if 0 /* This code would prevent move-to-window-line from moving point
5789 to a place inside the scroll margins (which would cause the
5790 next redisplay to scroll). I wrote this code, but then concluded
5791 it is probably better not to install it. However, it is here
5792 inside #if 0 so as not to lose it. -- rms. */
5793
5794 /* Don't let it get into the margin at either top or bottom. */
5795 iarg = max (iarg, this_scroll_margin);
5796 iarg = min (iarg, lines - this_scroll_margin - 1);
5797#endif
5798
5799 arg = make_number (iarg);
b7617575
GM
5800 }
5801
c8bc6f65 5802 /* Skip past a partially visible first line. */
163784df 5803 if (w->vscroll)
163784df
MB
5804 XSETINT (arg, XINT (arg) + 1);
5805
540b6aa0 5806 return Fvertical_motion (arg, window);
7ab12479 5807}
5500c422
GM
5808
5809
7ab12479 5810\f
5500c422
GM
5811/***********************************************************************
5812 Window Configuration
5813 ***********************************************************************/
5814
7ab12479
JB
5815struct save_window_data
5816 {
d0fdb6da 5817 EMACS_UINT size;
7ab12479 5818 struct Lisp_Vector *next_from_Lisp_Vector_struct;
bdc727bf 5819 Lisp_Object selected_frame;
7ab12479
JB
5820 Lisp_Object current_window;
5821 Lisp_Object current_buffer;
5822 Lisp_Object minibuf_scroll_window;
3f49fddc 5823 Lisp_Object minibuf_selected_window;
7ab12479 5824 Lisp_Object root_window;
bdc727bf 5825 Lisp_Object focus_frame;
cbff28e8
RS
5826 /* A vector, each of whose elements is a struct saved_window
5827 for one window. */
7ab12479 5828 Lisp_Object saved_windows;
b05b4e27
SM
5829
5830 /* All fields above are traced by the GC.
5831 From `fame-cols' down, the fields are ignored by the GC. */
5832
5833 int frame_cols, frame_lines, frame_menu_bar_lines;
5834 int frame_tool_bar_lines;
7ab12479 5835 };
ff06df24 5836
cbff28e8 5837/* This is saved as a Lisp_Vector */
7ab12479 5838struct saved_window
ea68264b
GM
5839{
5840 /* these first two must agree with struct Lisp_Vector in lisp.h */
d0fdb6da 5841 EMACS_UINT size;
ea68264b 5842 struct Lisp_Vector *next_from_Lisp_Vector_struct;
7ab12479 5843
ea68264b
GM
5844 Lisp_Object window;
5845 Lisp_Object buffer, start, pointm, mark;
949cf20f
KS
5846 Lisp_Object left_col, top_line, total_cols, total_lines;
5847 Lisp_Object hscroll, min_hscroll;
ea68264b
GM
5848 Lisp_Object parent, prev;
5849 Lisp_Object start_at_line_beg;
5850 Lisp_Object display_table;
949cf20f
KS
5851 Lisp_Object orig_top_line, orig_total_lines;
5852 Lisp_Object left_margin_cols, right_margin_cols;
5853 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5854 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
047aaeb9 5855 Lisp_Object dedicated, resize_proportionally;
ea68264b
GM
5856};
5857
7ab12479
JB
5858#define SAVED_WINDOW_N(swv,n) \
5859 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5860
5861DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
fdb82f93 5862 doc: /* Return t if OBJECT is a window-configuration object. */)
5842a27b 5863 (Lisp_Object object)
7ab12479 5864{
6ad0381c 5865 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
7ab12479
JB
5866}
5867
3f8ab7bd 5868DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
fdb82f93 5869 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5842a27b 5870 (Lisp_Object config)
3f8ab7bd
RS
5871{
5872 register struct save_window_data *data;
5873 struct Lisp_Vector *saved_windows;
5874
663fbbba 5875 CHECK_WINDOW_CONFIGURATION (config);
3f8ab7bd
RS
5876
5877 data = (struct save_window_data *) XVECTOR (config);
5878 saved_windows = XVECTOR (data->saved_windows);
5879 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5880}
5881
d5b2799e 5882DEFUN ("set-window-configuration", Fset_window_configuration,
fdb82f93
PJ
5883 Sset_window_configuration, 1, 1, 0,
5884 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5885CONFIGURATION must be a value previously returned
5886by `current-window-configuration' (which see).
5887If CONFIGURATION was made from a frame that is now deleted,
5888only frame-independent values can be restored. In this case,
5889the return value is nil. Otherwise the value is t. */)
5842a27b 5890 (Lisp_Object configuration)
7ab12479 5891{
7ab12479
JB
5892 register struct save_window_data *data;
5893 struct Lisp_Vector *saved_windows;
7ab12479 5894 Lisp_Object new_current_buffer;
fd482be5 5895 Lisp_Object frame;
44fa5b1e 5896 FRAME_PTR f;
2452438f 5897 EMACS_INT old_point = -1;
7ab12479 5898
663fbbba 5899 CHECK_WINDOW_CONFIGURATION (configuration);
7ab12479 5900
2f83aebe 5901 data = (struct save_window_data *) XVECTOR (configuration);
7ab12479
JB
5902 saved_windows = XVECTOR (data->saved_windows);
5903
7ab12479 5904 new_current_buffer = data->current_buffer;
265a9e55 5905 if (NILP (XBUFFER (new_current_buffer)->name))
7ab12479 5906 new_current_buffer = Qnil;
72695e47 5907 else
73cadfc1
DK
5908 {
5909 if (XBUFFER (new_current_buffer) == current_buffer)
e67a1dea
SM
5910 /* The code further down "preserves point" by saving here PT in
5911 old_point and then setting it later back into PT. When the
5912 current-selected-window and the final-selected-window both show
5913 the current buffer, this suffers from the problem that the
5914 current PT is the window-point of the current-selected-window,
5915 while the final PT is the point of the final-selected-window, so
5916 this copy from one PT to the other would end up moving the
5917 window-point of the final-selected-window to the window-point of
5918 the current-selected-window. So we have to be careful which
5919 point of the current-buffer we copy into old_point. */
5920 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5921 && WINDOWP (selected_window)
5922 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5923 && !EQ (selected_window, data->current_window))
5924 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5925 else
5926 old_point = PT;
73cadfc1 5927 else
203eb0aa
SM
5928 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5929 point in new_current_buffer as of the last time this buffer was
5930 used. This can be non-deterministic since it can be changed by
5931 things like jit-lock by mere temporary selection of some random
5932 window that happens to show this buffer.
5933 So if possible we want this arbitrary choice of "which point" to
5934 be the one from the to-be-selected-window so as to prevent this
5935 window's cursor from being copied from another window. */
5936 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5937 /* If current_window = selected_window, its point is in BUF_PT. */
5938 && !EQ (selected_window, data->current_window))
5939 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5940 else
5941 old_point = BUF_PT (XBUFFER (new_current_buffer));
73cadfc1 5942 }
7ab12479 5943
fd482be5
JB
5944 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5945 f = XFRAME (frame);
177c0ea7 5946
fd482be5
JB
5947 /* If f is a dead frame, don't bother rebuilding its window tree.
5948 However, there is other stuff we should still try to do below. */
5949 if (FRAME_LIVE_P (f))
7ab12479 5950 {
fd482be5
JB
5951 register struct window *w;
5952 register struct saved_window *p;
5500c422
GM
5953 struct window *root_window;
5954 struct window **leaf_windows;
5955 int n_leaf_windows;
c4280705 5956 int k, i, n;
fd482be5
JB
5957
5958 /* If the frame has been resized since this window configuration was
5959 made, we change the frame to the size specified in the
5960 configuration, restore the configuration, and then resize it
5961 back. We keep track of the prevailing height in these variables. */
949cf20f
KS
5962 int previous_frame_lines = FRAME_LINES (f);
5963 int previous_frame_cols = FRAME_COLS (f);
8f6ea2e9 5964 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
9ea173e8 5965 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
fd482be5 5966
d2b35234
RS
5967 /* The mouse highlighting code could get screwed up
5968 if it runs during this. */
5969 BLOCK_INPUT;
5970
b05b4e27
SM
5971 if (data->frame_lines != previous_frame_lines
5972 || data->frame_cols != previous_frame_cols)
5973 change_frame_size (f, data->frame_lines,
5974 data->frame_cols, 0, 0, 0);
e3678b64 5975#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
b05b4e27 5976 if (data->frame_menu_bar_lines
8f6ea2e9 5977 != previous_frame_menu_bar_lines)
b05b4e27
SM
5978 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5979 make_number (0));
4314246f 5980#ifdef HAVE_WINDOW_SYSTEM
b05b4e27 5981 if (data->frame_tool_bar_lines
9ea173e8 5982 != previous_frame_tool_bar_lines)
b05b4e27
SM
5983 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5984 make_number (0));
4314246f 5985#endif
217f2871 5986#endif
fd482be5 5987
a46c0153
RS
5988 /* "Swap out" point from the selected window's buffer
5989 into the window itself. (Normally the pointm of the selected
5990 window holds garbage.) We do this now, before
719eaeb1
GM
5991 restoring the window contents, and prevent it from
5992 being done later on when we select a new window. */
596ae0cf
RS
5993 if (! NILP (XWINDOW (selected_window)->buffer))
5994 {
5995 w = XWINDOW (selected_window);
5996 set_marker_both (w->pointm,
5997 w->buffer,
5998 BUF_PT (XBUFFER (w->buffer)),
5999 BUF_PT_BYTE (XBUFFER (w->buffer)));
6000 }
6001
fd482be5 6002 windows_or_buffers_changed++;
29aeee73 6003 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
fd482be5 6004
5500c422 6005 /* Problem: Freeing all matrices and later allocating them again
177c0ea7 6006 is a serious redisplay flickering problem. What we would
5500c422 6007 really like to do is to free only those matrices not reused
9d14503e 6008 below. */
5500c422
GM
6009 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6010 leaf_windows
6011 = (struct window **) alloca (count_windows (root_window)
6012 * sizeof (struct window *));
6013 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6014
fd482be5
JB
6015 /* Kludge Alert!
6016 Mark all windows now on frame as "deleted".
6017 Restoring the new configuration "undeletes" any that are in it.
37962e60 6018
fd482be5
JB
6019 Save their current buffers in their height fields, since we may
6020 need it later, if a buffer saved in the configuration is now
6021 dead. */
6022 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6023
6024 for (k = 0; k < saved_windows->size; k++)
6025 {
6026 p = SAVED_WINDOW_N (saved_windows, k);
6027 w = XWINDOW (p->window);
6028 w->next = Qnil;
7ab12479 6029
fd482be5
JB
6030 if (!NILP (p->parent))
6031 w->parent = SAVED_WINDOW_N (saved_windows,
6032 XFASTINT (p->parent))->window;
6033 else
6034 w->parent = Qnil;
7ab12479 6035
fd482be5 6036 if (!NILP (p->prev))
7ab12479 6037 {
fd482be5
JB
6038 w->prev = SAVED_WINDOW_N (saved_windows,
6039 XFASTINT (p->prev))->window;
6040 XWINDOW (w->prev)->next = p->window;
6041 }
6042 else
6043 {
6044 w->prev = Qnil;
6045 if (!NILP (w->parent))
6046 {
949cf20f 6047 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
fd482be5
JB
6048 {
6049 XWINDOW (w->parent)->vchild = p->window;
6050 XWINDOW (w->parent)->hchild = Qnil;
6051 }
6052 else
6053 {
6054 XWINDOW (w->parent)->hchild = p->window;
6055 XWINDOW (w->parent)->vchild = Qnil;
6056 }
6057 }
6058 }
6059
6060 /* If we squirreled away the buffer in the window's height,
6061 restore it now. */
949cf20f
KS
6062 if (BUFFERP (w->total_lines))
6063 w->buffer = w->total_lines;
6064 w->left_col = p->left_col;
6065 w->top_line = p->top_line;
6066 w->total_cols = p->total_cols;
6067 w->total_lines = p->total_lines;
fd482be5 6068 w->hscroll = p->hscroll;
ea68264b 6069 w->min_hscroll = p->min_hscroll;
fd482be5 6070 w->display_table = p->display_table;
949cf20f
KS
6071 w->orig_top_line = p->orig_top_line;
6072 w->orig_total_lines = p->orig_total_lines;
6073 w->left_margin_cols = p->left_margin_cols;
6074 w->right_margin_cols = p->right_margin_cols;
6075 w->left_fringe_width = p->left_fringe_width;
6076 w->right_fringe_width = p->right_fringe_width;
6077 w->fringes_outside_margins = p->fringes_outside_margins;
6078 w->scroll_bar_width = p->scroll_bar_width;
6079 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6ad0381c 6080 w->dedicated = p->dedicated;
047aaeb9 6081 w->resize_proportionally = p->resize_proportionally;
d834a2e9 6082 XSETFASTINT (w->last_modified, 0);
3cd21523 6083 XSETFASTINT (w->last_overlay_modified, 0);
fd482be5
JB
6084
6085 /* Reinstall the saved buffer and pointers into it. */
6086 if (NILP (p->buffer))
6087 w->buffer = p->buffer;
6088 else
6089 {
6090 if (!NILP (XBUFFER (p->buffer)->name))
6091 /* If saved buffer is alive, install it. */
6092 {
6093 w->buffer = p->buffer;
6094 w->start_at_line_beg = p->start_at_line_beg;
b73ea88e
RS
6095 set_marker_restricted (w->start, p->start, w->buffer);
6096 set_marker_restricted (w->pointm, p->pointm, w->buffer);
fd482be5 6097 Fset_marker (XBUFFER (w->buffer)->mark,
b73ea88e 6098 p->mark, w->buffer);
fd482be5
JB
6099
6100 /* As documented in Fcurrent_window_configuration, don't
243a5ce6
RS
6101 restore the location of point in the buffer which was
6102 current when the window configuration was recorded. */
6b54027b
RS
6103 if (!EQ (p->buffer, new_current_buffer)
6104 && XBUFFER (p->buffer) == current_buffer)
fd482be5
JB
6105 Fgoto_char (w->pointm);
6106 }
52a68e98
RS
6107 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6108 /* Else unless window has a live buffer, get one. */
7ab12479 6109 {
fd482be5
JB
6110 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6111 /* This will set the markers to beginning of visible
6112 range. */
6113 set_marker_restricted (w->start, make_number (0), w->buffer);
6114 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6115 w->start_at_line_beg = Qt;
7ab12479
JB
6116 }
6117 else
fd482be5 6118 /* Keeping window's old buffer; make sure the markers
52a68e98 6119 are real. */
7ab12479 6120 {
fd482be5
JB
6121 /* Set window markers at start of visible range. */
6122 if (XMARKER (w->start)->buffer == 0)
6123 set_marker_restricted (w->start, make_number (0),
6124 w->buffer);
6125 if (XMARKER (w->pointm)->buffer == 0)
b73ea88e
RS
6126 set_marker_restricted_both (w->pointm, w->buffer,
6127 BUF_PT (XBUFFER (w->buffer)),
6128 BUF_PT_BYTE (XBUFFER (w->buffer)));
fd482be5 6129 w->start_at_line_beg = Qt;
7ab12479
JB
6130 }
6131 }
6132 }
9ace597f 6133
fd482be5 6134 FRAME_ROOT_WINDOW (f) = data->root_window;
719eaeb1
GM
6135 /* Prevent "swapping out point" in the old selected window
6136 using the buffer that has been restored into it.
a46c0153 6137 We already swapped out point that from that window's old buffer. */
719eaeb1 6138 selected_window = Qnil;
a46c0153
RS
6139
6140 /* Arrange *not* to restore point in the buffer that was
6141 current when the window configuration was saved. */
243a5ce6
RS
6142 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6143 set_marker_restricted (XWINDOW (data->current_window)->pointm,
18787f5e 6144 make_number (old_point),
243a5ce6 6145 XWINDOW (data->current_window)->buffer);
177c0ea7 6146
14d87dc9 6147 Fselect_window (data->current_window, Qnil);
396a830c
RS
6148 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6149 = selected_window;
7ab12479 6150
db269683 6151 if (NILP (data->focus_frame)
017b2bad 6152 || (FRAMEP (data->focus_frame)
db269683
JB
6153 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6154 Fredirect_frame_focus (frame, data->focus_frame);
7ab12479 6155
fd482be5
JB
6156#if 0 /* I don't understand why this is needed, and it causes problems
6157 when the frame's old selected window has been deleted. */
e4e59717 6158 if (f != selected_frame && FRAME_WINDOW_P (f))
9a7c6fc3 6159 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
c6932ecd 6160 0, 0, Qnil);
fd482be5
JB
6161#endif
6162
6163 /* Set the screen height to the value it had before this function. */
949cf20f
KS
6164 if (previous_frame_lines != FRAME_LINES (f)
6165 || previous_frame_cols != FRAME_COLS (f))
6166 change_frame_size (f, previous_frame_lines, previous_frame_cols,
2b653806 6167 0, 0, 0);
e3678b64 6168#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9 6169 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
f8ad443a
AS
6170 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6171 make_number (0));
4314246f 6172#ifdef HAVE_WINDOW_SYSTEM
9ea173e8
GM
6173 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6174 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6175 make_number (0));
4314246f 6176#endif
217f2871 6177#endif
d2b35234 6178
5500c422 6179 /* Now, free glyph matrices in windows that were not reused. */
c4280705
GM
6180 for (i = n = 0; i < n_leaf_windows; ++i)
6181 {
6182 if (NILP (leaf_windows[i]->buffer))
6183 {
6184 /* Assert it's not reused as a combination. */
177c0ea7 6185 xassert (NILP (leaf_windows[i]->hchild)
c4280705
GM
6186 && NILP (leaf_windows[i]->vchild));
6187 free_window_matrices (leaf_windows[i]);
c4280705
GM
6188 }
6189 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6190 ++n;
6191 }
177c0ea7 6192
5500c422
GM
6193 adjust_glyphs (f);
6194
d2b35234 6195 UNBLOCK_INPUT;
756b6edc 6196
478292ed
RS
6197 /* Fselect_window will have made f the selected frame, so we
6198 reselect the proper frame here. Fhandle_switch_frame will change the
6199 selected window too, but that doesn't make the call to
6200 Fselect_window above totally superfluous; it still sets f's
6201 selected window. */
6202 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
c6932ecd 6203 do_switch_frame (data->selected_frame, 0, 0, Qnil);
478292ed 6204
eeca6f6f 6205 run_window_configuration_change_hook (f);
478292ed 6206 }
bdc727bf
JB
6207
6208 if (!NILP (new_current_buffer))
243a5ce6 6209 Fset_buffer (new_current_buffer);
bdc727bf 6210
478292ed 6211 Vminibuf_scroll_window = data->minibuf_scroll_window;
3dbab091 6212 minibuf_selected_window = data->minibuf_selected_window;
543f5fb1 6213
3f8ab7bd 6214 return (FRAME_LIVE_P (f) ? Qt : Qnil);
7ab12479
JB
6215}
6216
44fa5b1e 6217/* Mark all windows now on frame as deleted
7ab12479
JB
6218 by setting their buffers to nil. */
6219
fd482be5 6220void
971de7fb 6221delete_all_subwindows (register struct window *w)
7ab12479 6222{
265a9e55 6223 if (!NILP (w->next))
7ab12479 6224 delete_all_subwindows (XWINDOW (w->next));
265a9e55 6225 if (!NILP (w->vchild))
7ab12479 6226 delete_all_subwindows (XWINDOW (w->vchild));
265a9e55 6227 if (!NILP (w->hchild))
7ab12479 6228 delete_all_subwindows (XWINDOW (w->hchild));
605be8af 6229
949cf20f 6230 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
605be8af 6231
86e48436
RS
6232 if (!NILP (w->buffer))
6233 unshow_buffer (w);
6234
605be8af
JB
6235 /* We set all three of these fields to nil, to make sure that we can
6236 distinguish this dead window from any live window. Live leaf
6237 windows will have buffer set, and combination windows will have
6238 vchild or hchild set. */
6239 w->buffer = Qnil;
6240 w->vchild = Qnil;
6241 w->hchild = Qnil;
acf70840
GM
6242
6243 Vwindow_list = Qnil;
7ab12479
JB
6244}
6245\f
6246static int
971de7fb 6247count_windows (register struct window *window)
7ab12479
JB
6248{
6249 register int count = 1;
265a9e55 6250 if (!NILP (window->next))
7ab12479 6251 count += count_windows (XWINDOW (window->next));
265a9e55 6252 if (!NILP (window->vchild))
7ab12479 6253 count += count_windows (XWINDOW (window->vchild));
265a9e55 6254 if (!NILP (window->hchild))
7ab12479
JB
6255 count += count_windows (XWINDOW (window->hchild));
6256 return count;
6257}
6258
5500c422 6259
177c0ea7 6260/* Fill vector FLAT with leaf windows under W, starting at index I.
5500c422
GM
6261 Value is last index + 1. */
6262
6263static int
971de7fb 6264get_leaf_windows (struct window *w, struct window **flat, int i)
5500c422
GM
6265{
6266 while (w)
6267 {
6268 if (!NILP (w->hchild))
6269 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6270 else if (!NILP (w->vchild))
6271 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
177c0ea7 6272 else
5500c422
GM
6273 flat[i++] = w;
6274
6275 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6276 }
6277
6278 return i;
6279}
6280
6281
6282/* Return a pointer to the glyph W's physical cursor is on. Value is
6283 null if W's current matrix is invalid, so that no meaningfull glyph
6284 can be returned. */
6285
6286struct glyph *
971de7fb 6287get_phys_cursor_glyph (struct window *w)
5500c422
GM
6288{
6289 struct glyph_row *row;
6290 struct glyph *glyph;
6291
6292 if (w->phys_cursor.vpos >= 0
6293 && w->phys_cursor.vpos < w->current_matrix->nrows
6294 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6295 row->enabled_p)
6296 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6297 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6298 else
6299 glyph = NULL;
6300
6301 return glyph;
6302}
6303
6304
7ab12479 6305static int
971de7fb 6306save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
7ab12479
JB
6307{
6308 register struct saved_window *p;
6309 register struct window *w;
6310 register Lisp_Object tem;
6311
265a9e55 6312 for (;!NILP (window); window = w->next)
7ab12479
JB
6313 {
6314 p = SAVED_WINDOW_N (vector, i);
6315 w = XWINDOW (window);
6316
2a1893f4 6317 XSETFASTINT (w->temslot, i); i++;
7ab12479
JB
6318 p->window = window;
6319 p->buffer = w->buffer;
949cf20f
KS
6320 p->left_col = w->left_col;
6321 p->top_line = w->top_line;
6322 p->total_cols = w->total_cols;
6323 p->total_lines = w->total_lines;
7ab12479 6324 p->hscroll = w->hscroll;
ea68264b 6325 p->min_hscroll = w->min_hscroll;
7ab12479 6326 p->display_table = w->display_table;
949cf20f
KS
6327 p->orig_top_line = w->orig_top_line;
6328 p->orig_total_lines = w->orig_total_lines;
6329 p->left_margin_cols = w->left_margin_cols;
6330 p->right_margin_cols = w->right_margin_cols;
6331 p->left_fringe_width = w->left_fringe_width;
6332 p->right_fringe_width = w->right_fringe_width;
6333 p->fringes_outside_margins = w->fringes_outside_margins;
6334 p->scroll_bar_width = w->scroll_bar_width;
6335 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6ad0381c 6336 p->dedicated = w->dedicated;
047aaeb9 6337 p->resize_proportionally = w->resize_proportionally;
265a9e55 6338 if (!NILP (w->buffer))
7ab12479
JB
6339 {
6340 /* Save w's value of point in the window configuration.
6341 If w is the selected window, then get the value of point
6342 from the buffer; pointm is garbage in the selected window. */
6343 if (EQ (window, selected_window))
6344 {
6345 p->pointm = Fmake_marker ();
b73ea88e
RS
6346 set_marker_both (p->pointm, w->buffer,
6347 BUF_PT (XBUFFER (w->buffer)),
6348 BUF_PT_BYTE (XBUFFER (w->buffer)));
7ab12479
JB
6349 }
6350 else
eeb82665 6351 p->pointm = Fcopy_marker (w->pointm, Qnil);
7ab12479 6352
eeb82665 6353 p->start = Fcopy_marker (w->start, Qnil);
7ab12479
JB
6354 p->start_at_line_beg = w->start_at_line_beg;
6355
6356 tem = XBUFFER (w->buffer)->mark;
eeb82665 6357 p->mark = Fcopy_marker (tem, Qnil);
7ab12479
JB
6358 }
6359 else
6360 {
6361 p->pointm = Qnil;
6362 p->start = Qnil;
6363 p->mark = Qnil;
6364 p->start_at_line_beg = Qnil;
6365 }
6366
265a9e55 6367 if (NILP (w->parent))
7ab12479
JB
6368 p->parent = Qnil;
6369 else
6370 p->parent = XWINDOW (w->parent)->temslot;
6371
265a9e55 6372 if (NILP (w->prev))
7ab12479
JB
6373 p->prev = Qnil;
6374 else
6375 p->prev = XWINDOW (w->prev)->temslot;
6376
265a9e55 6377 if (!NILP (w->vchild))
7ab12479 6378 i = save_window_save (w->vchild, vector, i);
265a9e55 6379 if (!NILP (w->hchild))
7ab12479
JB
6380 i = save_window_save (w->hchild, vector, i);
6381 }
6382
6383 return i;
6384}
6385
a0d76c27 6386DEFUN ("current-window-configuration", Fcurrent_window_configuration,
fdb82f93
PJ
6387 Scurrent_window_configuration, 0, 1, 0,
6388 doc: /* Return an object representing the current window configuration of FRAME.
6389If FRAME is nil or omitted, use the selected frame.
6390This describes the number of windows, their sizes and current buffers,
6391and for each displayed buffer, where display starts, and the positions of
6392point and mark. An exception is made for point in the current buffer:
6393its value is -not- saved.
6394This also records the currently selected frame, and FRAME's focus
6395redirection (see `redirect-frame-focus'). */)
5842a27b 6396 (Lisp_Object frame)
7ab12479
JB
6397{
6398 register Lisp_Object tem;
6399 register int n_windows;
6400 register struct save_window_data *data;
6401 register int i;
44fa5b1e 6402 FRAME_PTR f;
43bad991 6403
44fa5b1e 6404 if (NILP (frame))
1ae1a37d 6405 frame = selected_frame;
b7826503 6406 CHECK_LIVE_FRAME (frame);
1ae1a37d 6407 f = XFRAME (frame);
7ab12479 6408
44fa5b1e 6409 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
b05b4e27
SM
6410 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6411 PVEC_WINDOW_CONFIGURATION);
6412
6413 data->frame_cols = FRAME_COLS (f);
6414 data->frame_lines = FRAME_LINES (f);
6415 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6416 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
1ae1a37d 6417 data->selected_frame = selected_frame;
44fa5b1e 6418 data->current_window = FRAME_SELECTED_WINDOW (f);
74112613 6419 XSETBUFFER (data->current_buffer, current_buffer);
3f49fddc 6420 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
3dbab091 6421 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
44fa5b1e 6422 data->root_window = FRAME_ROOT_WINDOW (f);
bdc727bf 6423 data->focus_frame = FRAME_FOCUS_FRAME (f);
7ab12479
JB
6424 tem = Fmake_vector (make_number (n_windows), Qnil);
6425 data->saved_windows = tem;
6426 for (i = 0; i < n_windows; i++)
6427 XVECTOR (tem)->contents[i]
8a450f0a 6428 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
a1d58e5b 6429 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
74112613 6430 XSETWINDOW_CONFIGURATION (tem, data);
7ab12479
JB
6431 return (tem);
6432}
6433
6434DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
fdb82f93 6435 0, UNEVALLED, 0,
a0a37a6f
LT
6436 doc: /* Execute BODY, preserving window sizes and contents.
6437Return the value of the last form in BODY.
fdb82f93
PJ
6438Restore which buffer appears in which window, where display starts,
6439and the value of point and mark for each window.
6440Also restore the choice of selected window.
6441Also restore which buffer is current.
6924dda6 6442Does not restore the value of point in current buffer.
7254a30c 6443usage: (save-window-excursion BODY...) */)
5842a27b 6444 (Lisp_Object args)
7ab12479
JB
6445{
6446 register Lisp_Object val;
aed13378 6447 register int count = SPECPDL_INDEX ();
7ab12479
JB
6448
6449 record_unwind_protect (Fset_window_configuration,
43bad991 6450 Fcurrent_window_configuration (Qnil));
7ab12479
JB
6451 val = Fprogn (args);
6452 return unbind_to (count, val);
6453}
5500c422 6454
94e16dd5
KS
6455
6456\f
6457/***********************************************************************
6458 Window Split Tree
6459 ***********************************************************************/
6460
6461static Lisp_Object
971de7fb 6462window_tree (struct window *w)
94e16dd5
KS
6463{
6464 Lisp_Object tail = Qnil;
6465 Lisp_Object result = Qnil;
6466
6467 while (w)
6468 {
6469 Lisp_Object wn;
6470
6471 XSETWINDOW (wn, w);
6472 if (!NILP (w->hchild))
6473 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
c16e1cc3 6474 window_tree (XWINDOW (w->hchild))));
94e16dd5
KS
6475 else if (!NILP (w->vchild))
6476 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
c16e1cc3 6477 window_tree (XWINDOW (w->vchild))));
94e16dd5
KS
6478
6479 if (NILP (result))
6480 {
6481 result = tail = Fcons (wn, Qnil);
6482 }
6483 else
6484 {
6485 XSETCDR (tail, Fcons (wn, Qnil));
6486 tail = XCDR (tail);
6487 }
6488
6489 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6490 }
6491
6492 return result;
6493}
6494
6495
6496
c16e1cc3 6497DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
94e16dd5 6498 0, 1, 0,
c16e1cc3 6499 doc: /* Return the window tree for frame FRAME.
94e16dd5
KS
6500
6501The return value is a list of the form (ROOT MINI), where ROOT
c16e1cc3 6502represents the window tree of the frame's root window, and MINI
94e16dd5
KS
6503is the frame's minibuffer window.
6504
6505If the root window is not split, ROOT is the root window itself.
6506Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
008171a4 6507horizontal split, and t for a vertical split, EDGES gives the combined
94e16dd5
KS
6508size and position of the subwindows in the split, and the rest of the
6509elements are the subwindows in the split. Each of the subwindows may
6510again be a window or a list representing a window split, and so on.
6511EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6512
6513If FRAME is nil or omitted, return information on the currently
6514selected frame. */)
5842a27b 6515 (Lisp_Object frame)
94e16dd5 6516{
94e16dd5
KS
6517 FRAME_PTR f;
6518
6519 if (NILP (frame))
6520 frame = selected_frame;
6521
6522 CHECK_FRAME (frame);
6523 f = XFRAME (frame);
6524
6525 if (!FRAME_LIVE_P (f))
6526 return Qnil;
6527
c16e1cc3 6528 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
94e16dd5
KS
6529}
6530
5500c422
GM
6531\f
6532/***********************************************************************
6533 Marginal Areas
6534 ***********************************************************************/
6535
6536DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
a7bdfc08 6537 2, 3, 0,
fdb82f93 6538 doc: /* Set width of marginal areas of window WINDOW.
6b61353c
KH
6539If WINDOW is nil, set margins of the currently selected window.
6540Second arg LEFT-WIDTH specifies the number of character cells to
6541reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6542does the same for the right marginal area. A nil width parameter
6543means no margin. */)
5842a27b 6544 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
5500c422
GM
6545{
6546 struct window *w = decode_window (window);
5500c422 6547
6b61353c
KH
6548 /* Translate negative or zero widths to nil.
6549 Margins that are too wide have to be checked elsewhere. */
949cf20f 6550
c10ce82e 6551 if (!NILP (left_width))
6b61353c 6552 {
c10ce82e
JB
6553 CHECK_NUMBER (left_width);
6554 if (XINT (left_width) <= 0)
6555 left_width = Qnil;
6b61353c 6556 }
5500c422 6557
c10ce82e 6558 if (!NILP (right_width))
6b61353c 6559 {
c10ce82e
JB
6560 CHECK_NUMBER (right_width);
6561 if (XINT (right_width) <= 0)
6562 right_width = Qnil;
6b61353c 6563 }
5500c422 6564
c10ce82e
JB
6565 if (!EQ (w->left_margin_cols, left_width)
6566 || !EQ (w->right_margin_cols, right_width))
949cf20f 6567 {
c10ce82e
JB
6568 w->left_margin_cols = left_width;
6569 w->right_margin_cols = right_width;
949cf20f
KS
6570
6571 adjust_window_margins (w);
6572
6573 ++windows_or_buffers_changed;
6574 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6575 }
5500c422 6576
5500c422
GM
6577 return Qnil;
6578}
6579
6580
6581DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6582 0, 1, 0,
fdb82f93
PJ
6583 doc: /* Get width of marginal areas of window WINDOW.
6584If WINDOW is omitted or nil, use the currently selected window.
6585Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6586If a marginal area does not exist, its width will be returned
6587as nil. */)
5842a27b 6588 (Lisp_Object window)
5500c422
GM
6589{
6590 struct window *w = decode_window (window);
949cf20f
KS
6591 return Fcons (w->left_margin_cols, w->right_margin_cols);
6592}
6593
6594
6595\f
6596/***********************************************************************
6597 Fringes
6598 ***********************************************************************/
6599
6600DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6601 2, 4, 0,
6b61353c
KH
6602 doc: /* Set the fringe widths of window WINDOW.
6603If WINDOW is nil, set the fringe widths of the currently selected
6604window.
6605Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6606the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6607fringe width. If a fringe width arg is nil, that means to use the
6608frame's default fringe width. Default fringe widths can be set with
6609the command `set-fringe-style'.
6610If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6611outside of the display margins. By default, fringes are drawn between
6612display marginal areas and the text area. */)
5842a27b 6613 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
949cf20f
KS
6614{
6615 struct window *w = decode_window (window);
6616
c10ce82e
JB
6617 if (!NILP (left_width))
6618 CHECK_NATNUM (left_width);
6619 if (!NILP (right_width))
6620 CHECK_NATNUM (right_width);
5a857365 6621
017e297e 6622 /* Do nothing on a tty. */
d46c6df5
NR
6623 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6624 && (!EQ (w->left_fringe_width, left_width)
6625 || !EQ (w->right_fringe_width, right_width)
6626 || !EQ (w->fringes_outside_margins, outside_margins)))
949cf20f 6627 {
c10ce82e
JB
6628 w->left_fringe_width = left_width;
6629 w->right_fringe_width = right_width;
949cf20f
KS
6630 w->fringes_outside_margins = outside_margins;
6631
6632 adjust_window_margins (w);
6633
6634 clear_glyph_matrix (w->current_matrix);
6635 w->window_end_valid = Qnil;
6636
6637 ++windows_or_buffers_changed;
6638 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6639 }
6640
6641 return Qnil;
6642}
6643
6644
6645DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6646 0, 1, 0,
6647 doc: /* Get width of fringes of window WINDOW.
6648If WINDOW is omitted or nil, use the currently selected window.
6b61353c 6649Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
5842a27b 6650 (Lisp_Object window)
949cf20f
KS
6651{
6652 struct window *w = decode_window (window);
d46c6df5 6653
949cf20f
KS
6654 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6655 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
d46c6df5
NR
6656 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6657 ? Qt : Qnil), Qnil)));
949cf20f
KS
6658}
6659
6660
6661\f
6662/***********************************************************************
6663 Scroll bars
6664 ***********************************************************************/
6665
6666DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6667 2, 4, 0,
6668 doc: /* Set width and type of scroll bars of window WINDOW.
6669If window is nil, set scroll bars of the currently selected window.
6670Second parameter WIDTH specifies the pixel width for the scroll bar;
6671this is automatically adjusted to a multiple of the frame column width.
6672Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6673bar: left, right, or nil.
6b61353c 6674If WIDTH is nil, use the frame's scroll-bar width.
a53d44a8
JB
6675If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6676Fourth parameter HORIZONTAL-TYPE is currently unused. */)
5842a27b 6677 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
949cf20f
KS
6678{
6679 struct window *w = decode_window (window);
6680
6681 if (!NILP (width))
0f8fe9a2
SM
6682 {
6683 CHECK_NATNUM (width);
949cf20f 6684
0f8fe9a2
SM
6685 if (XINT (width) == 0)
6686 vertical_type = Qnil;
6687 }
949cf20f 6688
413a79ad 6689 if (!(NILP (vertical_type)
0cc1039f 6690 || EQ (vertical_type, Qleft)
6b61353c
KH
6691 || EQ (vertical_type, Qright)
6692 || EQ (vertical_type, Qt)))
6693 error ("Invalid type of vertical scroll bar");
6694
949cf20f
KS
6695 if (!EQ (w->scroll_bar_width, width)
6696 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6697 {
6698 w->scroll_bar_width = width;
6699 w->vertical_scroll_bar_type = vertical_type;
6700
6701 adjust_window_margins (w);
6702
6703 clear_glyph_matrix (w->current_matrix);
6704 w->window_end_valid = Qnil;
6705
6706 ++windows_or_buffers_changed;
6707 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6708 }
6709
6710 return Qnil;
6711}
6712
6713
6714DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6715 0, 1, 0,
6716 doc: /* Get width and type of scroll bars of window WINDOW.
6717If WINDOW is omitted or nil, use the currently selected window.
6b61353c
KH
6718Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6719If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6720value. */)
5842a27b 6721 (Lisp_Object window)
949cf20f
KS
6722{
6723 struct window *w = decode_window (window);
6724 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6725 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6726 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6727 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6728 Fcons (w->vertical_scroll_bar_type,
6729 Fcons (Qnil, Qnil))));
5500c422
GM
6730}
6731
6732
7ab12479 6733\f
5500c422
GM
6734/***********************************************************************
6735 Smooth scrolling
6736 ***********************************************************************/
6737
0cc1039f 6738DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
fdb82f93
PJ
6739 doc: /* Return the amount by which WINDOW is scrolled vertically.
6740Use the selected window if WINDOW is nil or omitted.
0cc1039f 6741Normally, value is a multiple of the canonical character height of WINDOW;
8cd0d661 6742optional second arg PIXELS-P means value is measured in pixels. */)
5842a27b 6743 (Lisp_Object window, Lisp_Object pixels_p)
5500c422 6744{
47004952 6745 Lisp_Object result;
5500c422
GM
6746 struct frame *f;
6747 struct window *w;
177c0ea7 6748
5500c422
GM
6749 if (NILP (window))
6750 window = selected_window;
47004952 6751 else
b7826503 6752 CHECK_WINDOW (window);
5500c422
GM
6753 w = XWINDOW (window);
6754 f = XFRAME (w->frame);
177c0ea7 6755
5500c422 6756 if (FRAME_WINDOW_P (f))
0cc1039f
KS
6757 result = (NILP (pixels_p)
6758 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6759 : make_number (-w->vscroll));
5500c422 6760 else
47004952
GM
6761 result = make_number (0);
6762 return result;
5500c422
GM
6763}
6764
6765
6766DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
0cc1039f 6767 2, 3, 0,
fdb82f93 6768 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
0cc1039f
KS
6769WINDOW nil means use the selected window. Normally, VSCROLL is a
6770non-negative multiple of the canonical character height of WINDOW;
8cd0d661 6771optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
a0a37a6f
LT
6772If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6773corresponds to an integral number of pixels. The return value is the
6774result of this rounding.
6775If PIXELS-P is non-nil, the return value is VSCROLL. */)
5842a27b 6776 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
5500c422
GM
6777{
6778 struct window *w;
6779 struct frame *f;
177c0ea7 6780
5500c422
GM
6781 if (NILP (window))
6782 window = selected_window;
47004952 6783 else
b7826503
PJ
6784 CHECK_WINDOW (window);
6785 CHECK_NUMBER_OR_FLOAT (vscroll);
177c0ea7 6786
5500c422
GM
6787 w = XWINDOW (window);
6788 f = XFRAME (w->frame);
6789
6790 if (FRAME_WINDOW_P (f))
6791 {
6792 int old_dy = w->vscroll;
177c0ea7 6793
0cc1039f
KS
6794 w->vscroll = - (NILP (pixels_p)
6795 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6796 : XFLOATINT (vscroll));
47004952 6797 w->vscroll = min (w->vscroll, 0);
5500c422 6798
e56263e5
KS
6799 if (w->vscroll != old_dy)
6800 {
6801 /* Adjust glyph matrix of the frame if the virtual display
6802 area becomes larger than before. */
6803 if (w->vscroll < 0 && w->vscroll < old_dy)
6804 adjust_glyphs (f);
177c0ea7 6805
e56263e5
KS
6806 /* Prevent redisplay shortcuts. */
6807 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6808 }
5500c422 6809 }
177c0ea7 6810
0cc1039f 6811 return Fwindow_vscroll (window, pixels_p);
5500c422 6812}
177c0ea7 6813
7bbb5782
GM
6814\f
6815/* Call FN for all leaf windows on frame F. FN is called with the
6816 first argument being a pointer to the leaf window, and with
f95464e4 6817 additional argument USER_DATA. Stops when FN returns 0. */
7bbb5782
GM
6818
6819void
971de7fb 6820foreach_window (struct frame *f, int (*fn) (struct window *, void *), void *user_data)
7bbb5782 6821{
56f2de10 6822 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
76fb556f
YM
6823 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6824 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7bbb5782
GM
6825}
6826
6827
6828/* Helper function for foreach_window. Call FN for all leaf windows
6829 reachable from W. FN is called with the first argument being a
f95464e4 6830 pointer to the leaf window, and with additional argument USER_DATA.
67492200 6831 Stop when FN returns 0. Value is 0 if stopped by FN. */
7bbb5782 6832
67492200 6833static int
971de7fb 6834foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7bbb5782 6835{
67492200 6836 int cont;
177c0ea7 6837
67492200 6838 for (cont = 1; w && cont;)
7bbb5782
GM
6839 {
6840 if (!NILP (w->hchild))
f95464e4 6841 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
7bbb5782 6842 else if (!NILP (w->vchild))
f95464e4 6843 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
177c0ea7 6844 else
0f532a9a 6845 cont = fn (w, user_data);
177c0ea7 6846
7bbb5782
GM
6847 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6848 }
67492200
GM
6849
6850 return cont;
7bbb5782
GM
6851}
6852
6853
6d194a45 6854/* Freeze or unfreeze the window start of W unless it is a
f95464e4 6855 mini-window or the selected window. FREEZE_P non-null means freeze
7bbb5782
GM
6856 the window start. */
6857
67492200 6858static int
971de7fb 6859freeze_window_start (struct window *w, void *freeze_p)
7bbb5782 6860{
08c1d235
SM
6861 if (MINI_WINDOW_P (w)
6862 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6863 && (w == XWINDOW (selected_window)
6864 || (MINI_WINDOW_P (XWINDOW (selected_window))
6865 && ! NILP (Vminibuf_scroll_window)
6866 && w == XWINDOW (Vminibuf_scroll_window)))))
f95464e4 6867 freeze_p = NULL;
177c0ea7 6868
f95464e4 6869 w->frozen_window_start_p = freeze_p != NULL;
67492200 6870 return 1;
7bbb5782
GM
6871}
6872
6873
6874/* Freeze or unfreeze the window starts of all leaf windows on frame
6875 F, except the selected window and a mini-window. FREEZE_P non-zero
6876 means freeze the window start. */
6877
6878void
971de7fb 6879freeze_window_starts (struct frame *f, int freeze_p)
7bbb5782 6880{
cbccabec 6881 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7bbb5782 6882}
5500c422
GM
6883
6884\f
6885/***********************************************************************
6886 Initialization
6887 ***********************************************************************/
6888
cbff28e8 6889/* Return 1 if window configurations C1 and C2
9d14503e 6890 describe the same state of affairs. This is used by Fequal. */
cbff28e8
RS
6891
6892int
971de7fb 6893compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
cbff28e8
RS
6894{
6895 register struct save_window_data *d1, *d2;
6896 struct Lisp_Vector *sw1, *sw2;
6897 int i;
6898
663fbbba
KS
6899 CHECK_WINDOW_CONFIGURATION (c1);
6900 CHECK_WINDOW_CONFIGURATION (c2);
177c0ea7 6901
cbff28e8
RS
6902 d1 = (struct save_window_data *) XVECTOR (c1);
6903 d2 = (struct save_window_data *) XVECTOR (c2);
6904 sw1 = XVECTOR (d1->saved_windows);
6905 sw2 = XVECTOR (d2->saved_windows);
6906
b05b4e27 6907 if (d1->frame_cols != d2->frame_cols)
cbff28e8 6908 return 0;
b05b4e27 6909 if (d1->frame_lines != d2->frame_lines)
cbff28e8 6910 return 0;
b05b4e27 6911 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
cbff28e8
RS
6912 return 0;
6913 if (! EQ (d1->selected_frame, d2->selected_frame))
6914 return 0;
6915 /* Don't compare the current_window field directly.
6916 Instead see w1_is_current and w2_is_current, below. */
6917 if (! EQ (d1->current_buffer, d2->current_buffer))
6918 return 0;
2f8274be 6919 if (! ignore_positions)
3f49fddc
KS
6920 {
6921 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6922 return 0;
6923 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6924 return 0;
6925 }
cbff28e8
RS
6926 /* Don't compare the root_window field.
6927 We don't require the two configurations
6928 to use the same window object,
6929 and the two root windows must be equivalent
6930 if everything else compares equal. */
6931 if (! EQ (d1->focus_frame, d2->focus_frame))
6932 return 0;
cbff28e8
RS
6933
6934 /* Verify that the two confis have the same number of windows. */
6935 if (sw1->size != sw2->size)
6936 return 0;
6937
6938 for (i = 0; i < sw1->size; i++)
6939 {
6940 struct saved_window *p1, *p2;
6941 int w1_is_current, w2_is_current;
6942
6943 p1 = SAVED_WINDOW_N (sw1, i);
6944 p2 = SAVED_WINDOW_N (sw2, i);
6945
6946 /* Verify that the current windows in the two
6947 configurations correspond to each other. */
6948 w1_is_current = EQ (d1->current_window, p1->window);
6949 w2_is_current = EQ (d2->current_window, p2->window);
6950
6951 if (w1_is_current != w2_is_current)
6952 return 0;
6953
6954 /* Verify that the corresponding windows do match. */
6955 if (! EQ (p1->buffer, p2->buffer))
6956 return 0;
949cf20f 6957 if (! EQ (p1->left_col, p2->left_col))
cbff28e8 6958 return 0;
949cf20f 6959 if (! EQ (p1->top_line, p2->top_line))
cbff28e8 6960 return 0;
949cf20f 6961 if (! EQ (p1->total_cols, p2->total_cols))
cbff28e8 6962 return 0;
949cf20f 6963 if (! EQ (p1->total_lines, p2->total_lines))
cbff28e8 6964 return 0;
cbff28e8
RS
6965 if (! EQ (p1->display_table, p2->display_table))
6966 return 0;
6967 if (! EQ (p1->parent, p2->parent))
6968 return 0;
6969 if (! EQ (p1->prev, p2->prev))
6970 return 0;
2f8274be
RS
6971 if (! ignore_positions)
6972 {
6973 if (! EQ (p1->hscroll, p2->hscroll))
6974 return 0;
ea68264b
GM
6975 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6976 return 0;
2f8274be
RS
6977 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6978 return 0;
6979 if (NILP (Fequal (p1->start, p2->start)))
6980 return 0;
6981 if (NILP (Fequal (p1->pointm, p2->pointm)))
6982 return 0;
6983 if (NILP (Fequal (p1->mark, p2->mark)))
6984 return 0;
6985 }
949cf20f
KS
6986 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6987 return 0;
6988 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6989 return 0;
6990 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6991 return 0;
6992 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6993 return 0;
6994 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6995 return 0;
6996 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6997 return 0;
6998 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6999 return 0;
cbff28e8
RS
7000 }
7001
7002 return 1;
7003}
2f8274be
RS
7004
7005DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7006 Scompare_window_configurations, 2, 2, 0,
fdb82f93
PJ
7007 doc: /* Compare two window configurations as regards the structure of windows.
7008This function ignores details such as the values of point and mark
7009and scrolling positions. */)
5842a27b 7010 (Lisp_Object x, Lisp_Object y)
2f8274be
RS
7011{
7012 if (compare_window_configurations (x, y, 1))
7013 return Qt;
7014 return Qnil;
7015}
cbff28e8 7016\f
dfcf069d 7017void
971de7fb 7018init_window_once (void)
7ab12479 7019{
3224dac1 7020 struct frame *f = make_initial_frame ();
1ae1a37d
GM
7021 XSETFRAME (selected_frame, f);
7022 Vterminal_frame = selected_frame;
7023 minibuf_window = f->minibuffer_window;
7024 selected_window = f->selected_window;
7025 last_nonminibuf_frame = f;
5b03d3c0
RS
7026
7027 window_initialized = 1;
7ab12479
JB
7028}
7029
67492200 7030void
971de7fb 7031init_window (void)
67492200
GM
7032{
7033 Vwindow_list = Qnil;
7034}
7035
dfcf069d 7036void
971de7fb 7037syms_of_window (void)
7ab12479 7038{
d67b4f80 7039 Qscroll_up = intern_c_string ("scroll-up");
c525d842
CY
7040 staticpro (&Qscroll_up);
7041
d67b4f80 7042 Qscroll_down = intern_c_string ("scroll-down");
c525d842
CY
7043 staticpro (&Qscroll_down);
7044
a4b000fb
JL
7045 Qscroll_command = intern_c_string ("scroll-command");
7046 staticpro (&Qscroll_command);
7047
7048 Fput (Qscroll_up, Qscroll_command, Qt);
7049 Fput (Qscroll_down, Qscroll_command, Qt);
7050
d67b4f80 7051 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
8a37516b 7052 staticpro (&Qwindow_size_fixed);
c0e7ccd3 7053 Fset (Qwindow_size_fixed, Qnil);
177c0ea7 7054
543f5fb1
RS
7055 staticpro (&Qwindow_configuration_change_hook);
7056 Qwindow_configuration_change_hook
d67b4f80 7057 = intern_c_string ("window-configuration-change-hook");
543f5fb1 7058
d67b4f80 7059 Qwindowp = intern_c_string ("windowp");
7ab12479
JB
7060 staticpro (&Qwindowp);
7061
d67b4f80 7062 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
3f8ab7bd
RS
7063 staticpro (&Qwindow_configuration_p);
7064
d67b4f80 7065 Qwindow_live_p = intern_c_string ("window-live-p");
806b4d9b 7066 staticpro (&Qwindow_live_p);
605be8af 7067
d67b4f80 7068 Qdisplay_buffer = intern_c_string ("display-buffer");
87478b52
SM
7069 staticpro (&Qdisplay_buffer);
7070
d67b4f80 7071 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
a58ec57d
RS
7072 staticpro (&Qtemp_buffer_show_hook);
7073
67492200 7074 staticpro (&Vwindow_list);
8bfb170b
KS
7075
7076 minibuf_selected_window = Qnil;
3dbab091 7077 staticpro (&minibuf_selected_window);
67492200 7078
c876b227 7079 window_scroll_pixel_based_preserve_x = -1;
66fe93d1 7080 window_scroll_pixel_based_preserve_y = -1;
c876b227
SM
7081 window_scroll_preserve_hpos = -1;
7082 window_scroll_preserve_vpos = -1;
66d43aea 7083
7ab12479 7084 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
fdb82f93
PJ
7085 doc: /* Non-nil means call as function to display a help buffer.
7086The function is called with one argument, the buffer to be displayed.
7087Used by `with-output-to-temp-buffer'.
7088If this function is used, then it must do the entire job of showing
7089the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7ab12479
JB
7090 Vtemp_buffer_show_function = Qnil;
7091
7ab12479 7092 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
fdb82f93 7093 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7ab12479
JB
7094 Vminibuf_scroll_window = Qnil;
7095
cc91894c 7096 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
26124d5e 7097 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
cc91894c
KS
7098If the minibuffer is active, the `minibuffer-scroll-window' mode line
7099is displayed in the `mode-line' face. */);
7100 mode_line_in_non_selected_windows = 1;
26124d5e 7101
7ab12479 7102 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
fdb82f93 7103 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7ab12479
JB
7104 Vother_window_scroll_buffer = Qnil;
7105
e56263e5
KS
7106 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7107 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7108 auto_window_vscroll_p = 1;
7109
7ab12479 7110 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
fdb82f93 7111 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7ab12479
JB
7112 next_screen_context_lines = 2;
7113
7ab12479 7114 DEFVAR_INT ("window-min-height", &window_min_height,
047aaeb9
MR
7115 doc: /* Allow deleting windows less than this tall.
7116The value is measured in line units. If a window wants a modeline it
7117is counted as one line.
7118
7119Emacs honors settings of this variable when enlarging or shrinking
7120windows vertically. A value less than 1 is invalid. */);
7ab12479
JB
7121 window_min_height = 4;
7122
7123 DEFVAR_INT ("window-min-width", &window_min_width,
047aaeb9
MR
7124 doc: /* Allow deleting windows less than this wide.
7125The value is measured in characters and includes any fringes or
7126the scrollbar.
7127
7128Emacs honors settings of this variable when enlarging or shrinking
7129windows horizontally. A value less than 2 is invalid. */);
7ab12479
JB
7130 window_min_width = 10;
7131
5500c422
GM
7132 DEFVAR_LISP ("scroll-preserve-screen-position",
7133 &Vscroll_preserve_screen_position,
c876b227 7134 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
bdf4ec93
RS
7135A value of nil means point does not keep its screen position except
7136at the scroll margin or window boundary respectively.
7137A value of t means point keeps its screen position if the scroll
7138command moved it vertically out of the window, e.g. when scrolling
7139by full screens.
9013a7f8 7140Any other value means point always keeps its screen position.
a4b000fb
JL
7141Scroll commands should have the `scroll-command' property
7142on their symbols to be controlled by this variable. */);
5500c422 7143 Vscroll_preserve_screen_position = Qnil;
9317a85d 7144
a1562258
SM
7145 DEFVAR_LISP ("window-point-insertion-type", &Vwindow_point_insertion_type,
7146 doc: /* Type of marker to use for `window-point'. */);
7147 Vwindow_point_insertion_type = Qnil;
7148
543f5fb1
RS
7149 DEFVAR_LISP ("window-configuration-change-hook",
7150 &Vwindow_configuration_change_hook,
fdb82f93 7151 doc: /* Functions to call when window configuration changes.
6a44ffb3
SM
7152The buffer-local part is run once per window, with the relevant window
7153selected; while the global part is run only once for the modified frame,
7154with the relevant frame selected. */);
543f5fb1
RS
7155 Vwindow_configuration_change_hook = Qnil;
7156
666e158e
MB
7157 DEFVAR_LISP ("recenter-redisplay", &Vrecenter_redisplay,
7158 doc: /* If non-nil, then the `recenter' command with a nil argument
6e6e5760 7159will redraw the entire frame; the special value `tty' causes the
666e158e
MB
7160frame to be redrawn only if it is a tty frame. */);
7161 Vrecenter_redisplay = Qtty;
7162
7163
7ab12479
JB
7164 defsubr (&Sselected_window);
7165 defsubr (&Sminibuffer_window);
7166 defsubr (&Swindow_minibuffer_p);
7167 defsubr (&Swindowp);
806b4d9b 7168 defsubr (&Swindow_live_p);
7ab12479 7169 defsubr (&Spos_visible_in_window_p);
536833ab 7170 defsubr (&Swindow_line_height);
7ab12479
JB
7171 defsubr (&Swindow_buffer);
7172 defsubr (&Swindow_height);
7173 defsubr (&Swindow_width);
35ea56c9 7174 defsubr (&Swindow_full_width_p);
7ab12479
JB
7175 defsubr (&Swindow_hscroll);
7176 defsubr (&Sset_window_hscroll);
190eb263
RS
7177 defsubr (&Swindow_redisplay_end_trigger);
7178 defsubr (&Sset_window_redisplay_end_trigger);
7ab12479 7179 defsubr (&Swindow_edges);
c99a9eb3 7180 defsubr (&Swindow_pixel_edges);
9d5405ec 7181 defsubr (&Swindow_absolute_pixel_edges);
c99a9eb3
RS
7182 defsubr (&Swindow_inside_edges);
7183 defsubr (&Swindow_inside_pixel_edges);
9d5405ec 7184 defsubr (&Swindow_inside_absolute_pixel_edges);
d5783c40
JB
7185 defsubr (&Scoordinates_in_window_p);
7186 defsubr (&Swindow_at);
7ab12479
JB
7187 defsubr (&Swindow_point);
7188 defsubr (&Swindow_start);
7189 defsubr (&Swindow_end);
7190 defsubr (&Sset_window_point);
7191 defsubr (&Sset_window_start);
7192 defsubr (&Swindow_dedicated_p);
d207b766 7193 defsubr (&Sset_window_dedicated_p);
7ab12479
JB
7194 defsubr (&Swindow_display_table);
7195 defsubr (&Sset_window_display_table);
7196 defsubr (&Snext_window);
7197 defsubr (&Sprevious_window);
7198 defsubr (&Sother_window);
7199 defsubr (&Sget_lru_window);
7200 defsubr (&Sget_largest_window);
7201 defsubr (&Sget_buffer_window);
7202 defsubr (&Sdelete_other_windows);
7203 defsubr (&Sdelete_windows_on);
7204 defsubr (&Sreplace_buffer_in_windows);
7205 defsubr (&Sdelete_window);
7206 defsubr (&Sset_window_buffer);
7207 defsubr (&Sselect_window);
6b61353c 7208 defsubr (&Sforce_window_update);
7ab12479
JB
7209 defsubr (&Ssplit_window);
7210 defsubr (&Senlarge_window);
7211 defsubr (&Sshrink_window);
0d384044 7212 defsubr (&Sadjust_window_trailing_edge);
7ab12479
JB
7213 defsubr (&Sscroll_up);
7214 defsubr (&Sscroll_down);
7215 defsubr (&Sscroll_left);
7216 defsubr (&Sscroll_right);
ccd0664b 7217 defsubr (&Sother_window_for_scrolling);
7ab12479 7218 defsubr (&Sscroll_other_window);
fa832261 7219 defsubr (&Sminibuffer_selected_window);
7ab12479 7220 defsubr (&Srecenter);
81fe0836 7221 defsubr (&Swindow_text_height);
7ab12479
JB
7222 defsubr (&Smove_to_window_line);
7223 defsubr (&Swindow_configuration_p);
3f8ab7bd 7224 defsubr (&Swindow_configuration_frame);
7ab12479
JB
7225 defsubr (&Sset_window_configuration);
7226 defsubr (&Scurrent_window_configuration);
7227 defsubr (&Ssave_window_excursion);
c16e1cc3 7228 defsubr (&Swindow_tree);
5500c422
GM
7229 defsubr (&Sset_window_margins);
7230 defsubr (&Swindow_margins);
949cf20f
KS
7231 defsubr (&Sset_window_fringes);
7232 defsubr (&Swindow_fringes);
7233 defsubr (&Sset_window_scroll_bars);
7234 defsubr (&Swindow_scroll_bars);
5500c422
GM
7235 defsubr (&Swindow_vscroll);
7236 defsubr (&Sset_window_vscroll);
2f8274be 7237 defsubr (&Scompare_window_configurations);
67492200 7238 defsubr (&Swindow_list);
cfbb2395
JB
7239 defsubr (&Swindow_parameters);
7240 defsubr (&Swindow_parameter);
7241 defsubr (&Sset_window_parameter);
7242
7ab12479
JB
7243}
7244
dfcf069d 7245void
971de7fb 7246keys_of_window (void)
7ab12479
JB
7247{
7248 initial_define_key (control_x_map, '1', "delete-other-windows");
7249 initial_define_key (control_x_map, '2', "split-window");
7250 initial_define_key (control_x_map, '0', "delete-window");
7251 initial_define_key (control_x_map, 'o', "other-window");
7252 initial_define_key (control_x_map, '^', "enlarge-window");
7253 initial_define_key (control_x_map, '<', "scroll-left");
7254 initial_define_key (control_x_map, '>', "scroll-right");
7255
32129746 7256 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7ab12479 7257 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
32129746 7258 initial_define_key (meta_map, 'v', "scroll-down-command");
7ab12479 7259}
6b61353c
KH
7260
7261/* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
2c2cd782 7262 (do not change this comment) */