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