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