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