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