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