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