Improve explanation of special handling of point in current buffer.
[bpt/emacs.git] / src / window.c
CommitLineData
7ab12479
JB
1/* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
49b996e7 3 Copyright (C) 1985,86,87,93,94,95,96,97,1998,2000, 2001, 2002
6529ed87 4 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
3b7ad313
EN
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, 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
7442878f
GM
51/* Values returned from coordinates_in_window. */
52
53enum window_part
54{
55 ON_NOTHING,
56 ON_TEXT,
57 ON_MODE_LINE,
58 ON_VERTICAL_BORDER,
59 ON_HEADER_LINE,
60 ON_LEFT_FRINGE,
49b996e7
GM
61 ON_RIGHT_FRINGE,
62 ON_LEFT_MARGIN,
63 ON_RIGHT_MARGIN
7442878f
GM
64};
65
5500c422 66
3f8ab7bd 67Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
90647b07 68Lisp_Object Qwindow_size_fixed;
49b996e7 69extern Lisp_Object Qleft_margin, Qright_margin;
233a4a2c 70extern Lisp_Object Qheight, Qwidth;
7ab12479 71
521b203e 72static int displayed_window_lines P_ ((struct window *));
5e14b1fc 73static struct window *decode_window P_ ((Lisp_Object));
b7354ddf 74static Lisp_Object select_window_1 P_ ((Lisp_Object, int));
5500c422
GM
75static int count_windows P_ ((struct window *));
76static int get_leaf_windows P_ ((struct window *, struct window **, int));
77static void window_scroll P_ ((Lisp_Object, int, int, int));
78static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
79static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
233a4a2c 80static int window_min_size_1 P_ ((struct window *, int));
f984d4fc 81static int window_min_size P_ ((struct window *, int, int, int *));
233a4a2c 82static void size_window P_ ((Lisp_Object, int, int, int));
f95464e4 83static int freeze_window_start P_ ((struct window *, void *));
f984d4fc 84static int window_fixed_size_p P_ ((struct window *, int, int));
f95284d2 85static void enlarge_window P_ ((Lisp_Object, int, int, int));
67492200 86static Lisp_Object window_list P_ ((void));
f95464e4 87static int add_window_to_list P_ ((struct window *, void *));
118ea242
GM
88static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
89 Lisp_Object));
67492200
GM
90static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
91 Lisp_Object, int));
92static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
93 Lisp_Object *));
f95464e4
GM
94static int foreach_window_1 P_ ((struct window *,
95 int (* fn) (struct window *, void *),
96 void *));
cd2904bd 97static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
b7354ddf 98
b0b7ed0e
GM
99/* The value of `window-size-fixed'. */
100
101int window_size_fixed;
102
7ab12479
JB
103/* This is the window in which the terminal's cursor should
104 be left when nothing is being done with it. This must
105 always be a leaf window, and its buffer is selected by
106 the top level editing loop at the end of each command.
107
108 This value is always the same as
44fa5b1e 109 FRAME_SELECTED_WINDOW (selected_frame). */
7ab12479
JB
110
111Lisp_Object selected_window;
112
67492200
GM
113/* A list of all windows for use by next_window and Fwindow_list.
114 Functions creating or deleting windows should invalidate this cache
115 by setting it to nil. */
116
117Lisp_Object Vwindow_list;
118
5500c422
GM
119/* The mini-buffer window of the selected frame.
120 Note that you cannot test for mini-bufferness of an arbitrary window
121 by comparing against this; but you can test for mini-bufferness of
7ab12479 122 the selected window. */
5500c422 123
7ab12479
JB
124Lisp_Object minibuf_window;
125
3f49fddc
KS
126/* Non-nil means it is the window whose mode line should be
127 shown as the selected window when the minibuffer is selected. */
128
3dbab091 129Lisp_Object minibuf_selected_window;
3f49fddc 130
7ab12479 131/* Non-nil means it is the window for C-M-v to scroll
5500c422
GM
132 when the mini-buffer is selected. */
133
7ab12479
JB
134Lisp_Object Vminibuf_scroll_window;
135
136/* Non-nil means this is the buffer whose window C-M-v should scroll. */
5500c422 137
7ab12479
JB
138Lisp_Object Vother_window_scroll_buffer;
139
7ab12479 140/* Non-nil means it's function to call to display temp buffers. */
5500c422 141
7ab12479
JB
142Lisp_Object Vtemp_buffer_show_function;
143
cc91894c
KS
144/* Non-zero means to use mode-line-inactive face in all windows but the
145 selected-window and the minibuffer-scroll-window when the
146 minibuffer is active. */
147int mode_line_in_non_selected_windows;
26124d5e 148
7ab12479 149/* If a window gets smaller than either of these, it is removed. */
5500c422 150
31ade731
SM
151EMACS_INT window_min_height;
152EMACS_INT window_min_width;
7ab12479
JB
153
154/* Nonzero implies Fdisplay_buffer should create windows. */
5500c422 155
7ab12479
JB
156int pop_up_windows;
157
44fa5b1e 158/* Nonzero implies make new frames for Fdisplay_buffer. */
5500c422 159
44fa5b1e 160int pop_up_frames;
7ab12479 161
9c3da604
GM
162/* Nonzero means reuse existing frames for displaying buffers. */
163
164int display_buffer_reuse_frames;
165
7ab12479 166/* Non-nil means use this function instead of default */
5500c422 167
44fa5b1e 168Lisp_Object Vpop_up_frame_function;
7ab12479
JB
169
170/* Function to call to handle Fdisplay_buffer. */
5500c422 171
7ab12479
JB
172Lisp_Object Vdisplay_buffer_function;
173
6529ed87
GM
174/* Non-nil means that Fdisplay_buffer should even the heights of windows. */
175
176Lisp_Object Veven_window_heights;
177
a90712c2 178/* List of buffer *names* for buffers that should have their own frames. */
5500c422 179
a90712c2
RS
180Lisp_Object Vspecial_display_buffer_names;
181
182/* List of regexps for buffer names that should have their own frames. */
5500c422 183
a90712c2
RS
184Lisp_Object Vspecial_display_regexps;
185
186/* Function to pop up a special frame. */
5500c422 187
a90712c2
RS
188Lisp_Object Vspecial_display_function;
189
855d8627 190/* List of buffer *names* for buffers to appear in selected window. */
5500c422 191
855d8627
RS
192Lisp_Object Vsame_window_buffer_names;
193
194/* List of regexps for buffer names to appear in selected window. */
5500c422 195
855d8627
RS
196Lisp_Object Vsame_window_regexps;
197
a58ec57d 198/* Hook run at end of temp_output_buffer_show. */
5500c422 199
a58ec57d
RS
200Lisp_Object Qtemp_buffer_show_hook;
201
37962e60 202/* Fdisplay_buffer always splits the largest window
7ab12479 203 if that window is more than this high. */
5500c422 204
31ade731 205EMACS_INT split_height_threshold;
7ab12479
JB
206
207/* Number of lines of continuity in scrolling by screenfuls. */
5500c422 208
31ade731 209EMACS_INT next_screen_context_lines;
7ab12479
JB
210
211/* Incremented for each window created. */
5500c422 212
7ab12479
JB
213static int sequence_number;
214
5b03d3c0 215/* Nonzero after init_window_once has finished. */
5500c422 216
5b03d3c0
RS
217static int window_initialized;
218
543f5fb1 219/* Hook to run when window config changes. */
5500c422 220
543f5fb1
RS
221Lisp_Object Qwindow_configuration_change_hook;
222Lisp_Object Vwindow_configuration_change_hook;
223
9317a85d
RS
224/* Nonzero means scroll commands try to put point
225 at the same screen height as previously. */
9317a85d 226
5500c422
GM
227Lisp_Object Vscroll_preserve_screen_position;
228
229#if 0 /* This isn't used anywhere. */
91a78190 230/* Nonzero means we can split a frame even if it is "unsplittable". */
eddd51c2 231static int inhibit_frame_unsplittable;
5500c422 232#endif /* 0 */
7d601aaa 233
31ade731 234extern EMACS_INT scroll_margin;
9317a85d 235
dba06815 236extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
7ab12479
JB
237\f
238DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
fdb82f93
PJ
239 doc: /* Returns t if OBJECT is a window. */)
240 (object)
413430c5 241 Lisp_Object object;
7ab12479 242{
413430c5 243 return WINDOWP (object) ? Qt : Qnil;
7ab12479
JB
244}
245
806b4d9b 246DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
fdb82f93 247 doc: /* Returns t if OBJECT is a window which is currently visible. */)
413430c5
EN
248 (object)
249 Lisp_Object object;
605be8af 250{
9e571f49 251 return WINDOW_LIVE_P (object) ? Qt : Qnil;
605be8af
JB
252}
253
7ab12479
JB
254Lisp_Object
255make_window ()
256{
cffec418 257 Lisp_Object val;
7ab12479 258 register struct window *p;
cffec418 259
26605be9 260 p = allocate_window ();
d834a2e9
KH
261 XSETFASTINT (p->sequence_number, ++sequence_number);
262 XSETFASTINT (p->left, 0);
263 XSETFASTINT (p->top, 0);
264 XSETFASTINT (p->height, 0);
265 XSETFASTINT (p->width, 0);
266 XSETFASTINT (p->hscroll, 0);
ea68264b 267 XSETFASTINT (p->min_hscroll, 0);
f984d4fc 268 p->orig_top = p->orig_height = Qnil;
7ab12479
JB
269 p->start = Fmake_marker ();
270 p->pointm = Fmake_marker ();
d834a2e9 271 XSETFASTINT (p->use_time, 0);
44fa5b1e 272 p->frame = Qnil;
7ab12479
JB
273 p->display_table = Qnil;
274 p->dedicated = Qnil;
5500c422
GM
275 p->pseudo_window_p = 0;
276 bzero (&p->cursor, sizeof (p->cursor));
277 bzero (&p->last_cursor, sizeof (p->last_cursor));
278 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
279 p->desired_matrix = p->current_matrix = 0;
280 p->phys_cursor_type = -1;
4b26579e 281 p->phys_cursor_width = -1;
5500c422
GM
282 p->must_be_updated_p = 0;
283 XSETFASTINT (p->window_end_vpos, 0);
284 XSETFASTINT (p->window_end_pos, 0);
285 p->window_end_valid = Qnil;
286 p->vscroll = 0;
cffec418 287 XSETWINDOW (val, p);
5500c422 288 XSETFASTINT (p->last_point, 0);
7bbb5782 289 p->frozen_window_start_p = 0;
6b448755
RS
290 p->height_fixed_p = 0;
291 p->last_cursor_off_p = p->cursor_off_p = 0;
67492200
GM
292
293 Vwindow_list = Qnil;
7ab12479
JB
294 return val;
295}
296
297DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
fdb82f93
PJ
298 doc: /* Return the window that the cursor now appears in and commands apply to. */)
299 ()
7ab12479
JB
300{
301 return selected_window;
302}
303
83762ba4 304DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
fdb82f93
PJ
305 doc: /* Return the window used now for minibuffers.
306If the optional argument FRAME is specified, return the minibuffer window
307used by that frame. */)
308 (frame)
83762ba4 309 Lisp_Object frame;
7ab12479 310{
83762ba4 311 if (NILP (frame))
1ae1a37d 312 frame = selected_frame;
b7826503 313 CHECK_LIVE_FRAME (frame);
83762ba4 314 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
7ab12479
JB
315}
316
605be8af 317DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
fdb82f93
PJ
318 doc: /* Returns non-nil if WINDOW is a minibuffer window. */)
319 (window)
7ab12479
JB
320 Lisp_Object window;
321{
322 struct window *w = decode_window (window);
53bb6b99 323 return MINI_WINDOW_P (w) ? Qt : Qnil;
81e4d465
MB
324}
325
326
7ab12479 327DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
fdb82f93
PJ
328 Spos_visible_in_window_p, 0, 3, 0,
329 doc: /* Return t if position POS is currently on the frame in WINDOW.
330Return nil if that position is scrolled vertically out of view.
331If a character is only partially visible, nil is returned, unless the
332optional argument PARTIALLY is non-nil.
333POS defaults to point in WINDOW; WINDOW defaults to the selected window. */)
334 (pos, window, partially)
5cdb3cf3 335 Lisp_Object pos, window, partially;
7ab12479
JB
336{
337 register struct window *w;
7ab12479
JB
338 register int posint;
339 register struct buffer *buf;
9a132b1f
GM
340 struct text_pos top;
341 Lisp_Object in_window;
53bb6b99 342 int fully_p;
7ab12479 343
f28b75a9
MB
344 w = decode_window (window);
345 buf = XBUFFER (w->buffer);
346 SET_TEXT_POS_FROM_MARKER (top, w->start);
347
348 if (!NILP (pos))
7ab12479 349 {
b7826503 350 CHECK_NUMBER_COERCE_MARKER (pos);
7ab12479
JB
351 posint = XINT (pos);
352 }
f28b75a9
MB
353 else if (w == XWINDOW (selected_window))
354 posint = PT;
355 else
356 posint = XMARKER (w->pointm)->charpos;
7ab12479 357
53bb6b99 358 /* If position is above window start, it's not visible. */
5500c422 359 if (posint < CHARPOS (top))
9a132b1f
GM
360 in_window = Qnil;
361 else if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
81e4d465
MB
362 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
363 && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
81e4d465 364 {
53bb6b99
GM
365 /* If frame is up-to-date, and POSINT is < window end pos, use
366 that info. This doesn't work for POSINT == end pos, because
367 the window end pos is actually the position _after_ the last
368 char in the window. */
5cdb3cf3 369 if (NILP (partially))
53bb6b99 370 {
5cdb3cf3 371 pos_visible_p (w, posint, &fully_p, NILP (partially));
53bb6b99
GM
372 in_window = fully_p ? Qt : Qnil;
373 }
81e4d465 374 else
53bb6b99 375 in_window = Qt;
81e4d465 376 }
9a132b1f
GM
377 else if (posint > BUF_ZV (buf))
378 in_window = Qnil;
379 else if (CHARPOS (top) < BUF_BEGV (buf) || CHARPOS (top) > BUF_ZV (buf))
380 /* If window start is out of range, do something reasonable. */
381 in_window = Qnil;
7ab12479
JB
382 else
383 {
5cdb3cf3
MB
384 if (pos_visible_p (w, posint, &fully_p, NILP (partially)))
385 in_window = !NILP (partially) || fully_p ? Qt : Qnil;
81e4d465
MB
386 else
387 in_window = Qnil;
7ab12479 388 }
9a132b1f
GM
389
390 return in_window;
7ab12479 391}
53bb6b99 392
7ab12479
JB
393\f
394static struct window *
395decode_window (window)
396 register Lisp_Object window;
397{
265a9e55 398 if (NILP (window))
7ab12479
JB
399 return XWINDOW (selected_window);
400
b7826503 401 CHECK_LIVE_WINDOW (window);
7ab12479
JB
402 return XWINDOW (window);
403}
404
405DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
fdb82f93
PJ
406 doc: /* Return the buffer that WINDOW is displaying. */)
407 (window)
7ab12479
JB
408 Lisp_Object window;
409{
410 return decode_window (window)->buffer;
411}
412
413DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
fdb82f93
PJ
414 doc: /* Return the number of lines in WINDOW (including its mode line). */)
415 (window)
7ab12479
JB
416 Lisp_Object window;
417{
418 return decode_window (window)->height;
419}
420
421DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
fdb82f93
PJ
422 doc: /* Return the number of display columns in WINDOW.
423This is the width that is usable columns available for text in WINDOW.
424If you want to find out how many columns WINDOW takes up,
425use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
426 (window)
7ab12479
JB
427 Lisp_Object window;
428{
ee61d94e 429 return make_number (window_internal_width (decode_window (window)));
7ab12479
JB
430}
431
432DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
fdb82f93
PJ
433 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
434 (window)
7ab12479
JB
435 Lisp_Object window;
436{
437 return decode_window (window)->hscroll;
438}
439
440DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
fdb82f93
PJ
441 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
442NCOL should be zero or positive. */)
443 (window, ncol)
ea68264b 444 Lisp_Object window, ncol;
7ab12479 445{
ea68264b
GM
446 struct window *w = decode_window (window);
447 int hscroll;
7ab12479 448
b7826503 449 CHECK_NUMBER (ncol);
ea68264b
GM
450 hscroll = max (0, XINT (ncol));
451
452 /* Prevent redisplay shortcuts when changing the hscroll. */
453 if (XINT (w->hscroll) != hscroll)
b1599b4c 454 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
ea68264b 455
c67fa410 456 w->hscroll = make_number (hscroll);
7ab12479
JB
457 return ncol;
458}
459
190eb263
RS
460DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
461 Swindow_redisplay_end_trigger, 0, 1, 0,
fdb82f93
PJ
462 doc: /* Return WINDOW's redisplay end trigger value.
463See `set-window-redisplay-end-trigger' for more information. */)
464 (window)
190eb263
RS
465 Lisp_Object window;
466{
467 return decode_window (window)->redisplay_end_trigger;
468}
469
470DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
471 Sset_window_redisplay_end_trigger, 2, 2, 0,
fdb82f93
PJ
472 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
473VALUE should be a buffer position (typically a marker) or nil.
474If it is a buffer position, then if redisplay in WINDOW reaches a position
475beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
476with two arguments: WINDOW, and the end trigger value.
477Afterwards the end-trigger value is reset to nil. */)
478 (window, value)
190eb263
RS
479 register Lisp_Object window, value;
480{
481 register struct window *w;
482
483 w = decode_window (window);
484 w->redisplay_end_trigger = value;
485 return value;
486}
487
7ab12479 488DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
fdb82f93
PJ
489 doc: /* Return a list of the edge coordinates of WINDOW.
490\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
491RIGHT is one more than the rightmost column used by WINDOW,
492and BOTTOM is one more than the bottommost row used by WINDOW
493 and its mode-line. */)
494 (window)
7ab12479
JB
495 Lisp_Object window;
496{
497 register struct window *w = decode_window (window);
498
499 return Fcons (w->left, Fcons (w->top,
111e5992 500 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
7ab12479
JB
501 Fcons (make_number (XFASTINT (w->top)
502 + XFASTINT (w->height)),
503 Qnil))));
504}
505
fbad6f9a 506/* Test if the character at column *X, row *Y is within window W.
d5783c40
JB
507 If it is not, return 0;
508 if it is in the window's text area,
509 set *x and *y to its location relative to the upper left corner
510 of the window, and
511 return 1;
512 if it is on the window's modeline, return 2;
513 if it is on the border between the window and its right sibling,
5500c422
GM
514 return 3.
515 if it is on the window's top line, return 4;
81d189fd 516 if it is in left or right fringe of the window,
49b996e7
GM
517 return 5 or 6, and convert *X and *Y to window-relative coordinates;
518 if it is in the marginal area to the left/right of the window,
519 return 7 or 8, and convert *X and *Y to window-relative coordinates.
5500c422
GM
520
521 X and Y are frame relative pixel coordinates. */
522
7442878f 523static enum window_part
d5783c40
JB
524coordinates_in_window (w, x, y)
525 register struct window *w;
526 register int *x, *y;
527{
1cb794ba
GM
528 /* Let's make this a global enum later, instead of using numbers
529 everywhere. */
b0228ace
GM
530 struct frame *f = XFRAME (WINDOW_FRAME (w));
531 int left_x, right_x, top_y, bottom_y;
7442878f 532 enum window_part part;
15c565ac 533 int ux = CANON_X_UNIT (f);
b0228ace
GM
534 int x0 = XFASTINT (w->left) * ux;
535 int x1 = x0 + XFASTINT (w->width) * ux;
dad75588
GM
536 /* The width of the area where the vertical line can be dragged.
537 (Between mode lines for instance. */
538 int grabbable_width = ux;
ac431108 539 int lmargin_width = 0, rmargin_width = 0;
b0228ace
GM
540
541 if (*x < x0 || *x >= x1)
542 return ON_NOTHING;
543
466539bc
EZ
544 /* In what's below, we subtract 1 when computing right_x because we
545 want the rightmost pixel, which is given by left_pixel+width-1. */
5500c422
GM
546 if (w->pseudo_window_p)
547 {
548 left_x = 0;
b0228ace 549 right_x = XFASTINT (w->width) * CANON_X_UNIT (f) - 1;
5500c422
GM
550 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
551 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
552 }
553 else
554 {
2add4349 555 left_x = (WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w)
50df5e5a 556 - FRAME_INTERNAL_BORDER_WIDTH_SAFE (f));
466539bc 557 right_x = WINDOW_DISPLAY_RIGHT_EDGE_PIXEL_X (w) - 1;
2add4349 558 top_y = (WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w)
50df5e5a 559 - FRAME_INTERNAL_BORDER_WIDTH_SAFE (f));
5500c422
GM
560 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
561 }
37962e60 562
1cb794ba
GM
563 /* On the mode line or header line? If it's near the start of
564 the mode or header line of window that's has a horizontal
565 sibling, say it's on the vertical line. That's to be able
566 to resize windows horizontally in case we're using toolkit
567 scroll bars. */
b0228ace 568
1cb794ba 569 if (WINDOW_WANTS_MODELINE_P (w)
b0228ace
GM
570 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
571 && *y < bottom_y)
1cb794ba 572 {
b0228ace
GM
573 /* We're somewhere on the mode line. We consider the place
574 between mode lines of horizontally adjacent mode lines
575 as the vertical border. If scroll bars on the left,
576 return the right window. */
577 part = ON_MODE_LINE;
578
579 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
580 {
dad75588 581 if (abs (*x - x0) < grabbable_width)
b0228ace
GM
582 part = ON_VERTICAL_BORDER;
583 }
dad75588 584 else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < grabbable_width)
b0228ace 585 part = ON_VERTICAL_BORDER;
1cb794ba 586 }
b0228ace
GM
587 else if (WINDOW_WANTS_HEADER_LINE_P (w)
588 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
589 && *y >= top_y)
590 {
591 part = ON_HEADER_LINE;
592
593 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
594 {
dad75588 595 if (abs (*x - x0) < grabbable_width)
b0228ace
GM
596 part = ON_VERTICAL_BORDER;
597 }
dad75588 598 else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < grabbable_width)
b0228ace
GM
599 part = ON_VERTICAL_BORDER;
600 }
601 /* Outside anything interesting? */
602 else if (*y < top_y
603 || *y >= bottom_y
604 || *x < (left_x
c13418ac 605 - FRAME_LEFT_FRINGE_WIDTH (f)
b0228ace 606 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * ux)
7442878f 607 || *x > (right_x
c13418ac 608 + FRAME_RIGHT_FRINGE_WIDTH (f)
7442878f 609 + FRAME_RIGHT_SCROLL_BAR_WIDTH (f) * ux))
1cb794ba 610 {
b0228ace 611 part = ON_NOTHING;
1cb794ba 612 }
b0228ace 613 else if (FRAME_WINDOW_P (f))
fbad6f9a 614 {
447e9da0
GM
615 if (!w->pseudo_window_p
616 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f)
617 && !WINDOW_RIGHTMOST_P (w)
c13418ac 618 && (abs (*x - right_x - FRAME_RIGHT_FRINGE_WIDTH (f)) < grabbable_width))
b0228ace
GM
619 {
620 part = ON_VERTICAL_BORDER;
621 }
622 else if (*x < left_x || *x > right_x)
447e9da0 623 {
81d189fd 624 /* Other lines than the mode line don't include fringes and
447e9da0 625 scroll bars on the left. */
fbad6f9a 626
447e9da0
GM
627 /* Convert X and Y to window-relative pixel coordinates. */
628 *x -= left_x;
629 *y -= top_y;
b0228ace
GM
630 part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
631 }
632 else
633 {
ac431108
EZ
634 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
635 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
636 /* You can never be on a margin area if its width is zero. */
637 if (lmargin_width
638 && *x <= window_box_right (w, LEFT_MARGIN_AREA))
49b996e7 639 part = ON_LEFT_MARGIN;
ac431108
EZ
640 else if (rmargin_width
641 && *x >= window_box_left (w, RIGHT_MARGIN_AREA))
49b996e7
GM
642 part = ON_RIGHT_MARGIN;
643 else
644 {
645 part = ON_TEXT;
646 *x -= left_x;
647 *y -= top_y;
648 }
447e9da0 649 }
fbad6f9a 650 }
447e9da0
GM
651 else
652 {
653 /* Need to say "*x > right_x" rather than >=, since on character
654 terminals, the vertical line's x coordinate is right_x. */
655 if (*x < left_x || *x > right_x)
656 {
81d189fd 657 /* Other lines than the mode line don't include fringes and
447e9da0
GM
658 scroll bars on the left. */
659
660 /* Convert X and Y to window-relative pixel coordinates. */
661 *x -= left_x;
662 *y -= top_y;
b0228ace 663 part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
447e9da0 664 }
447e9da0 665 /* Here, too, "*x > right_x" is because of character terminals. */
b0228ace
GM
666 else if (!w->pseudo_window_p
667 && !WINDOW_RIGHTMOST_P (w)
668 && *x > right_x - ux)
669 {
670 /* On the border on the right side of the window? Assume that
671 this area begins at RIGHT_X minus a canonical char width. */
672 part = ON_VERTICAL_BORDER;
673 }
674 else
675 {
ac431108
EZ
676 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
677 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
678 /* You can never be on a margin area if its width is zero.
679 This is especially important for character terminals. */
680 if (lmargin_width
681 && *x <= window_box_right (w, LEFT_MARGIN_AREA))
49b996e7 682 part = ON_LEFT_MARGIN;
ac431108
EZ
683 else if (rmargin_width
684 && *x >= window_box_left (w, RIGHT_MARGIN_AREA))
49b996e7
GM
685 part = ON_RIGHT_MARGIN;
686 else
687 {
688 part = ON_TEXT;
689 /* Convert X and Y to window-relative pixel coordinates. */
690 *x -= left_x;
691 *y -= top_y;
692 }
b0228ace 693 }
447e9da0 694 }
b0228ace
GM
695
696 return part;
d5783c40
JB
697}
698
b0228ace 699
d5783c40 700DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
fdb82f93
PJ
701 Scoordinates_in_window_p, 2, 2, 0,
702 doc: /* Return non-nil if COORDINATES are in WINDOW.
703COORDINATES is a cons of the form (X . Y), X and Y being distances
704measured in characters from the upper-left corner of the frame.
705\(0 . 0) denotes the character in the upper left corner of the
706frame.
707If COORDINATES are in the text portion of WINDOW,
708 the coordinates relative to the window are returned.
709If they are in the mode line of WINDOW, `mode-line' is returned.
710If they are in the top mode line of WINDOW, `header-line' is returned.
81d189fd
KS
711If they are in the left fringe of WINDOW, `left-fringe' is returned.
712If they are in the right fringe of WINDOW, `right-fringe' is returned.
fdb82f93 713If they are on the border between WINDOW and its right sibling,
49b996e7
GM
714 `vertical-line' is returned.
715If they are in the windows's left or right marginal areas, `left-margin'\n\
716 or `right-margin' is returned. */)
fdb82f93 717 (coordinates, window)
d5783c40
JB
718 register Lisp_Object coordinates, window;
719{
5500c422
GM
720 struct window *w;
721 struct frame *f;
d5783c40 722 int x, y;
5500c422 723 Lisp_Object lx, ly;
d5783c40 724
b7826503 725 CHECK_LIVE_WINDOW (window);
5500c422
GM
726 w = XWINDOW (window);
727 f = XFRAME (w->frame);
b7826503 728 CHECK_CONS (coordinates);
5500c422
GM
729 lx = Fcar (coordinates);
730 ly = Fcdr (coordinates);
b7826503
PJ
731 CHECK_NUMBER_OR_FLOAT (lx);
732 CHECK_NUMBER_OR_FLOAT (ly);
5500c422
GM
733 x = PIXEL_X_FROM_CANON_X (f, lx);
734 y = PIXEL_Y_FROM_CANON_Y (f, ly);
735
736 switch (coordinates_in_window (w, &x, &y))
d5783c40 737 {
7442878f 738 case ON_NOTHING:
d5783c40
JB
739 return Qnil;
740
7442878f
GM
741 case ON_TEXT:
742 /* X and Y are now window relative pixel coordinates. Convert
743 them to canonical char units before returning them. */
5500c422
GM
744 return Fcons (CANON_X_FROM_PIXEL_X (f, x),
745 CANON_Y_FROM_PIXEL_Y (f, y));
d5783c40 746
7442878f 747 case ON_MODE_LINE:
d5783c40 748 return Qmode_line;
37962e60 749
7442878f 750 case ON_VERTICAL_BORDER:
e5d77022 751 return Qvertical_line;
d5783c40 752
7442878f 753 case ON_HEADER_LINE:
045dee35 754 return Qheader_line;
5500c422 755
7442878f
GM
756 case ON_LEFT_FRINGE:
757 return Qleft_fringe;
fbad6f9a 758
7442878f
GM
759 case ON_RIGHT_FRINGE:
760 return Qright_fringe;
fbad6f9a 761
49b996e7
GM
762 case ON_LEFT_MARGIN:
763 return Qleft_margin;
764
765 case ON_RIGHT_MARGIN:
766 return Qright_margin;
767
d5783c40
JB
768 default:
769 abort ();
770 }
771}
772
67492200
GM
773
774/* Callback for foreach_window, used in window_from_coordinates.
f95464e4
GM
775 Check if window W contains coordinates specified by USER_DATA which
776 is actually a pointer to a struct check_window_data CW.
777
778 Check if window W contains coordinates *CW->x and *CW->y. If it
779 does, return W in *CW->window, as Lisp_Object, and return in
5372262f 780 *CW->part the part of the window under coordinates *X,*Y. Return
f95464e4
GM
781 zero from this function to stop iterating over windows. */
782
783struct check_window_data
784{
785 Lisp_Object *window;
786 int *x, *y, *part;
787};
67492200
GM
788
789static int
f95464e4 790check_window_containing (w, user_data)
67492200 791 struct window *w;
f95464e4 792 void *user_data;
67492200 793{
f95464e4 794 struct check_window_data *cw = (struct check_window_data *) user_data;
7442878f
GM
795 enum window_part found;
796 int continue_p = 1;
67492200 797
f95464e4 798 found = coordinates_in_window (w, cw->x, cw->y);
7442878f 799 if (found != ON_NOTHING)
67492200 800 {
f95464e4
GM
801 *cw->part = found - 1;
802 XSETWINDOW (*cw->window, w);
7442878f 803 continue_p = 0;
67492200
GM
804 }
805
7442878f 806 return continue_p;
67492200
GM
807}
808
809
5500c422
GM
810/* Find the window containing frame-relative pixel position X/Y and
811 return it as a Lisp_Object. If X, Y is on the window's modeline,
812 set *PART to 1; if it is on the separating line between the window
813 and its right sibling, set it to 2; otherwise set it to 0. If
814 there is no window under X, Y return nil and leave *PART
67492200
GM
815 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
816
817 This function was previously implemented with a loop cycling over
818 windows with Fnext_window, and starting with the frame's selected
819 window. It turned out that this doesn't work with an
820 implementation of next_window using Vwindow_list, because
821 FRAME_SELECTED_WINDOW (F) is not always contained in the window
822 tree of F when this function is called asynchronously from
823 note_mouse_highlight. The original loop didn't terminate in this
824 case. */
5500c422 825
7ab12479 826Lisp_Object
67492200
GM
827window_from_coordinates (f, x, y, part, tool_bar_p)
828 struct frame *f;
7ab12479 829 int x, y;
d5783c40 830 int *part;
9ea173e8 831 int tool_bar_p;
7ab12479 832{
67492200 833 Lisp_Object window;
f95464e4 834 struct check_window_data cw;
5500c422 835
67492200 836 window = Qnil;
f95464e4
GM
837 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
838 foreach_window (f, check_window_containing, &cw);
67492200
GM
839
840 /* If not found above, see if it's in the tool bar window, if a tool
841 bar exists. */
842 if (NILP (window)
843 && tool_bar_p
844 && WINDOWP (f->tool_bar_window)
845 && XINT (XWINDOW (f->tool_bar_window)->height) > 0
7442878f
GM
846 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
847 != ON_NOTHING))
5500c422
GM
848 {
849 *part = 0;
67492200 850 window = f->tool_bar_window;
5500c422 851 }
37962e60 852
67492200 853 return window;
7ab12479
JB
854}
855
ab17c3f2 856DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
fdb82f93
PJ
857 doc: /* Return window containing coordinates X and Y on FRAME.
858If omitted, FRAME defaults to the currently selected frame.
859The top left corner of the frame is considered to be row 0,
860column 0. */)
861 (x, y, frame)
862 Lisp_Object x, y, frame;
7ab12479
JB
863{
864 int part;
5500c422 865 struct frame *f;
7ab12479 866
44fa5b1e 867 if (NILP (frame))
1ae1a37d 868 frame = selected_frame;
b7826503 869 CHECK_LIVE_FRAME (frame);
5500c422 870 f = XFRAME (frame);
7ab12479 871
5500c422 872 /* Check that arguments are integers or floats. */
b7826503
PJ
873 CHECK_NUMBER_OR_FLOAT (x);
874 CHECK_NUMBER_OR_FLOAT (y);
5500c422
GM
875
876 return window_from_coordinates (f,
877 PIXEL_X_FROM_CANON_X (f, x),
878 PIXEL_Y_FROM_CANON_Y (f, y),
879 &part, 0);
7ab12479
JB
880}
881
882DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
fdb82f93
PJ
883 doc: /* Return current value of point in WINDOW.
884For a nonselected window, this is the value point would have
885if that window were selected.
886
887Note that, when WINDOW is the selected window and its buffer
888is also currently selected, the value returned is the same as (point).
889It would be more strictly correct to return the `top-level' value
890of point, outside of any save-excursion forms.
891But that is hard to define. */)
892 (window)
7ab12479
JB
893 Lisp_Object window;
894{
895 register struct window *w = decode_window (window);
896
897 if (w == XWINDOW (selected_window)
898 && current_buffer == XBUFFER (w->buffer))
899 return Fpoint ();
900 return Fmarker_position (w->pointm);
901}
902
903DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
fdb82f93
PJ
904 doc: /* Return position at which display currently starts in WINDOW.
905This is updated by redisplay or by calling `set-window-start'. */)
906 (window)
7ab12479
JB
907 Lisp_Object window;
908{
909 return Fmarker_position (decode_window (window)->start);
910}
911
8646118f
RS
912/* This is text temporarily removed from the doc string below.
913
fdb82f93
PJ
914This function returns nil if the position is not currently known.
915That happens when redisplay is preempted and doesn't finish.
916If in that case you want to compute where the end of the window would
917have been if redisplay had finished, do this:
918 (save-excursion
919 (goto-char (window-start window))
920 (vertical-motion (1- (window-height window)) window)
8646118f
RS
921 (point))") */
922
478292ed 923DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
fdb82f93
PJ
924 doc: /* Return position at which display currently ends in WINDOW.
925This is updated by redisplay, when it runs to completion.
926Simply changing the buffer text or setting `window-start'
927does not update this value.
928If UPDATE is non-nil, compute the up-to-date position
929if it isn't already recorded. */)
930 (window, update)
478292ed 931 Lisp_Object window, update;
7ab12479
JB
932{
933 Lisp_Object value;
934 struct window *w = decode_window (window);
5a41ab94
RS
935 Lisp_Object buf;
936
937 buf = w->buffer;
b7826503 938 CHECK_BUFFER (buf);
5a41ab94 939
8646118f 940#if 0 /* This change broke some things. We should make it later. */
7250968e
RS
941 /* If we don't know the end position, return nil.
942 The user can compute it with vertical-motion if he wants to.
943 It would be nicer to do it automatically,
944 but that's so slow that it would probably bother people. */
945 if (NILP (w->window_end_valid))
946 return Qnil;
8646118f 947#endif
7250968e 948
478292ed
RS
949 if (! NILP (update)
950 && ! (! NILP (w->window_end_valid)
951 && XFASTINT (w->last_modified) >= MODIFF))
952 {
cbc099e5
GM
953 struct text_pos startp;
954 struct it it;
99593a9d 955 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
2d6d9df0
GM
956
957 /* In case W->start is out of the range, use something
958 reasonable. This situation occured when loading a file with
959 `-l' containing a call to `rmail' with subsequent other
960 commands. At the end, W->start happened to be BEG, while
cbc099e5 961 rmail had already narrowed the buffer. */
2d6d9df0 962 if (XMARKER (w->start)->charpos < BEGV)
cbc099e5 963 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
2d6d9df0 964 else if (XMARKER (w->start)->charpos > ZV)
cbc099e5 965 SET_TEXT_POS (startp, ZV, ZV_BYTE);
2d6d9df0 966 else
cbc099e5
GM
967 SET_TEXT_POS_FROM_MARKER (startp, w->start);
968
969 /* Cannot use Fvertical_motion because that function doesn't
970 cope with variable-height lines. */
99593a9d
GM
971 if (b != current_buffer)
972 {
973 old_buffer = current_buffer;
974 set_buffer_internal (b);
975 }
976
cbc099e5
GM
977 start_display (&it, w, startp);
978 move_it_vertically (&it, window_box_height (w));
c3c45f65
GM
979 if (it.current_y < it.last_visible_y)
980 move_it_past_eol (&it);
cbc099e5 981 value = make_number (IT_CHARPOS (it));
99593a9d
GM
982
983 if (old_buffer)
984 set_buffer_internal (old_buffer);
478292ed
RS
985 }
986 else
cbc099e5 987 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
7ab12479
JB
988
989 return value;
990}
991
992DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
fdb82f93
PJ
993 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer. */)
994 (window, pos)
7ab12479
JB
995 Lisp_Object window, pos;
996{
997 register struct window *w = decode_window (window);
998
b7826503 999 CHECK_NUMBER_COERCE_MARKER (pos);
e90c4fe6
RS
1000 if (w == XWINDOW (selected_window)
1001 && XBUFFER (w->buffer) == current_buffer)
7ab12479
JB
1002 Fgoto_char (pos);
1003 else
1004 set_marker_restricted (w->pointm, pos, w->buffer);
799417bd 1005
0685cb3c
GM
1006 /* We have to make sure that redisplay updates the window to show
1007 the new value of point. */
1008 if (!EQ (window, selected_window))
799417bd 1009 ++windows_or_buffers_changed;
ce7fae7d 1010
7ab12479
JB
1011 return pos;
1012}
1013
1014DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
fdb82f93
PJ
1015 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1016Optional third arg NOFORCE non-nil inhibits next redisplay
1017from overriding motion of point in order to display at this exact start. */)
1018 (window, pos, noforce)
7ab12479
JB
1019 Lisp_Object window, pos, noforce;
1020{
1021 register struct window *w = decode_window (window);
1022
b7826503 1023 CHECK_NUMBER_COERCE_MARKER (pos);
7ab12479
JB
1024 set_marker_restricted (w->start, pos, w->buffer);
1025 /* this is not right, but much easier than doing what is right. */
1026 w->start_at_line_beg = Qnil;
265a9e55 1027 if (NILP (noforce))
7ab12479
JB
1028 w->force_start = Qt;
1029 w->update_mode_line = Qt;
d834a2e9 1030 XSETFASTINT (w->last_modified, 0);
3cd21523 1031 XSETFASTINT (w->last_overlay_modified, 0);
62c07cc7
JB
1032 if (!EQ (window, selected_window))
1033 windows_or_buffers_changed++;
ce7fae7d 1034
7ab12479
JB
1035 return pos;
1036}
1037
1038DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1039 1, 1, 0,
fdb82f93
PJ
1040 doc: /* Return WINDOW's dedicated object, usually t or nil.
1041See also `set-window-dedicated-p'. */)
1042 (window)
7ab12479
JB
1043 Lisp_Object window;
1044{
1045 return decode_window (window)->dedicated;
1046}
1047
d207b766
RS
1048DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1049 Sset_window_dedicated_p, 2, 2, 0,
fdb82f93
PJ
1050 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1051If it is dedicated, Emacs will not automatically change
1052which buffer appears in it.
1053The second argument is the new value for the dedication flag;
1054non-nil means yes. */)
1055 (window, arg)
1056 Lisp_Object window, arg;
7ab12479
JB
1057{
1058 register struct window *w = decode_window (window);
1059
265a9e55 1060 if (NILP (arg))
7ab12479
JB
1061 w->dedicated = Qnil;
1062 else
d207b766 1063 w->dedicated = Qt;
7ab12479
JB
1064
1065 return w->dedicated;
1066}
1067
1068DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1069 0, 1, 0,
fdb82f93
PJ
1070 doc: /* Return the display-table that WINDOW is using. */)
1071 (window)
7ab12479
JB
1072 Lisp_Object window;
1073{
1074 return decode_window (window)->display_table;
1075}
1076
5500c422
GM
1077/* Get the display table for use on window W. This is either W's
1078 display table or W's buffer's display table. Ignore the specified
1079 tables if they are not valid; if no valid table is specified,
1080 return 0. */
7ab12479 1081
319315f1 1082struct Lisp_Char_Table *
7ab12479
JB
1083window_display_table (w)
1084 struct window *w;
1085{
c756cdbe
GM
1086 struct Lisp_Char_Table *dp = NULL;
1087
1088 if (DISP_TABLE_P (w->display_table))
1089 dp = XCHAR_TABLE (w->display_table);
1090 else if (BUFFERP (w->buffer))
1091 {
1092 struct buffer *b = XBUFFER (w->buffer);
1093
1094 if (DISP_TABLE_P (b->display_table))
1095 dp = XCHAR_TABLE (b->display_table);
1096 else if (DISP_TABLE_P (Vstandard_display_table))
1097 dp = XCHAR_TABLE (Vstandard_display_table);
1098 }
171d003c 1099
c756cdbe 1100 return dp;
7ab12479
JB
1101}
1102
3a2712f9 1103DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
fdb82f93
PJ
1104 doc: /* Set WINDOW's display-table to TABLE. */)
1105 (window, table)
7ab12479
JB
1106 register Lisp_Object window, table;
1107{
1108 register struct window *w;
7ab12479
JB
1109
1110 w = decode_window (window);
1111 w->display_table = table;
1112 return table;
1113}
1114\f
1115/* Record info on buffer window w is displaying
1116 when it is about to cease to display that buffer. */
dfcf069d 1117static void
7ab12479
JB
1118unshow_buffer (w)
1119 register struct window *w;
1120{
12cae7c0 1121 Lisp_Object buf;
b73ea88e 1122 struct buffer *b;
7ab12479 1123
12cae7c0 1124 buf = w->buffer;
b73ea88e
RS
1125 b = XBUFFER (buf);
1126 if (b != XMARKER (w->pointm)->buffer)
7ab12479
JB
1127 abort ();
1128
573f41ab 1129#if 0
7ab12479
JB
1130 if (w == XWINDOW (selected_window)
1131 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1132 /* Do this except when the selected window's buffer
1133 is being removed from some other window. */
573f41ab
RS
1134#endif
1135 /* last_window_start records the start position that this buffer
1136 had in the last window to be disconnected from it.
1137 Now that this statement is unconditional,
1138 it is possible for the buffer to be displayed in the
1139 selected window, while last_window_start reflects another
1140 window which was recently showing the same buffer.
1141 Some people might say that might be a good thing. Let's see. */
b73ea88e 1142 b->last_window_start = marker_position (w->start);
7ab12479
JB
1143
1144 /* Point in the selected window's buffer
1145 is actually stored in that buffer, and the window's pointm isn't used.
1146 So don't clobber point in that buffer. */
719eaeb1
GM
1147 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1148 /* This line helps to fix Horsley's testbug.el bug. */
8801a864
KR
1149 && !(WINDOWP (b->last_selected_window)
1150 && w != XWINDOW (b->last_selected_window)
719eaeb1 1151 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
b73ea88e
RS
1152 temp_set_point_both (b,
1153 clip_to_bounds (BUF_BEGV (b),
1154 XMARKER (w->pointm)->charpos,
1155 BUF_ZV (b)),
1156 clip_to_bounds (BUF_BEGV_BYTE (b),
1157 marker_byte_position (w->pointm),
1158 BUF_ZV_BYTE (b)));
719eaeb1 1159
8801a864
KR
1160 if (WINDOWP (b->last_selected_window)
1161 && w == XWINDOW (b->last_selected_window))
719eaeb1 1162 b->last_selected_window = Qnil;
7ab12479
JB
1163}
1164
1165/* Put replacement into the window structure in place of old. */
dfcf069d 1166static void
7ab12479
JB
1167replace_window (old, replacement)
1168 Lisp_Object old, replacement;
1169{
1170 register Lisp_Object tem;
1171 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1172
44fa5b1e
JB
1173 /* If OLD is its frame's root_window, then replacement is the new
1174 root_window for that frame. */
7ab12479 1175
7f4161e0 1176 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
44fa5b1e 1177 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
7ab12479
JB
1178
1179 p->left = o->left;
1180 p->top = o->top;
1181 p->width = o->width;
1182 p->height = o->height;
5500c422
GM
1183 p->desired_matrix = p->current_matrix = 0;
1184 p->vscroll = 0;
1185 bzero (&p->cursor, sizeof (p->cursor));
1186 bzero (&p->last_cursor, sizeof (p->last_cursor));
1187 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1188 p->phys_cursor_type = -1;
4b26579e 1189 p->phys_cursor_width = -1;
5500c422
GM
1190 p->must_be_updated_p = 0;
1191 p->pseudo_window_p = 0;
1192 XSETFASTINT (p->window_end_vpos, 0);
1193 XSETFASTINT (p->window_end_pos, 0);
1194 p->window_end_valid = Qnil;
7bbb5782 1195 p->frozen_window_start_p = 0;
f984d4fc 1196 p->orig_top = p->orig_height = Qnil;
7ab12479
JB
1197
1198 p->next = tem = o->next;
265a9e55 1199 if (!NILP (tem))
7ab12479
JB
1200 XWINDOW (tem)->prev = replacement;
1201
1202 p->prev = tem = o->prev;
265a9e55 1203 if (!NILP (tem))
7ab12479
JB
1204 XWINDOW (tem)->next = replacement;
1205
1206 p->parent = tem = o->parent;
265a9e55 1207 if (!NILP (tem))
7ab12479
JB
1208 {
1209 if (EQ (XWINDOW (tem)->vchild, old))
1210 XWINDOW (tem)->vchild = replacement;
1211 if (EQ (XWINDOW (tem)->hchild, old))
1212 XWINDOW (tem)->hchild = replacement;
1213 }
1214
1215/*** Here, if replacement is a vertical combination
1216and so is its new parent, we should make replacement's
1217children be children of that parent instead. ***/
1218}
1219
1220DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
fdb82f93
PJ
1221 doc: /* Remove WINDOW from the display. Default is selected window. */)
1222 (window)
7ab12479 1223 register Lisp_Object window;
543f5fb1
RS
1224{
1225 delete_window (window);
1226
1227 if (! NILP (Vwindow_configuration_change_hook)
1228 && ! NILP (Vrun_hooks))
1229 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1230
1231 return Qnil;
1232}
1233
5e14b1fc 1234void
543f5fb1
RS
1235delete_window (window)
1236 register Lisp_Object window;
7ab12479
JB
1237{
1238 register Lisp_Object tem, parent, sib;
1239 register struct window *p;
1240 register struct window *par;
951f9df5 1241 struct frame *f;
7ab12479 1242
605be8af
JB
1243 /* Because this function is called by other C code on non-leaf
1244 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1245 so we can't decode_window here. */
265a9e55 1246 if (NILP (window))
7ab12479
JB
1247 window = selected_window;
1248 else
b7826503 1249 CHECK_WINDOW (window);
7ab12479 1250 p = XWINDOW (window);
605be8af
JB
1251
1252 /* It's okay to delete an already-deleted window. */
1253 if (NILP (p->buffer)
1254 && NILP (p->hchild)
1255 && NILP (p->vchild))
296b535c 1256 return;
605be8af 1257
7ab12479 1258 parent = p->parent;
265a9e55 1259 if (NILP (parent))
7ab12479
JB
1260 error ("Attempt to delete minibuffer or sole ordinary window");
1261 par = XWINDOW (parent);
1262
1263 windows_or_buffers_changed++;
67492200 1264 Vwindow_list = Qnil;
951f9df5
GM
1265 f = XFRAME (WINDOW_FRAME (p));
1266 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
7ab12479 1267
605be8af
JB
1268 /* Are we trying to delete any frame's selected window? */
1269 {
192c3131 1270 Lisp_Object swindow, pwindow;
605be8af 1271
0def0403
RS
1272 /* See if the frame's selected window is either WINDOW
1273 or any subwindow of it, by finding all that window's parents
1274 and comparing each one with WINDOW. */
192c3131 1275 swindow = FRAME_SELECTED_WINDOW (f);
0def0403 1276
192c3131 1277 while (1)
0def0403 1278 {
192c3131
RS
1279 pwindow = swindow;
1280 while (!NILP (pwindow))
1281 {
1282 if (EQ (window, pwindow))
1283 break;
1284 pwindow = XWINDOW (pwindow)->parent;
1285 }
1286
1287 /* If the window being deleted is not a parent of SWINDOW,
1288 then SWINDOW is ok as the new selected window. */
1289 if (!EQ (window, pwindow))
0def0403 1290 break;
192c3131
RS
1291 /* Otherwise, try another window for SWINDOW. */
1292 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1293
1294 /* If we get back to the frame's selected window,
1295 it means there was no acceptable alternative,
1296 so we cannot delete. */
1297 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1298 error ("Cannot delete window");
0def0403
RS
1299 }
1300
192c3131
RS
1301 /* If we need to change SWINDOW, do it. */
1302 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
605be8af 1303 {
605be8af
JB
1304 /* If we're about to delete the selected window on the
1305 selected frame, then we should use Fselect_window to select
1306 the new window. On the other hand, if we're about to
1307 delete the selected window on any other frame, we shouldn't do
1308 anything but set the frame's selected_window slot. */
192c3131
RS
1309 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1310 Fselect_window (swindow);
605be8af 1311 else
192c3131 1312 FRAME_SELECTED_WINDOW (f) = swindow;
605be8af
JB
1313 }
1314 }
7ab12479
JB
1315
1316 tem = p->buffer;
1317 /* tem is null for dummy parent windows
1318 (which have inferiors but not any contents themselves) */
265a9e55 1319 if (!NILP (tem))
7ab12479
JB
1320 {
1321 unshow_buffer (p);
1322 unchain_marker (p->pointm);
1323 unchain_marker (p->start);
7ab12479
JB
1324 }
1325
05e71564
GM
1326 /* Free window glyph matrices. It is sure that they are allocated
1327 again when ADJUST_GLYPHS is called. Block input so that expose
1328 events and other events that access glyph matrices are not
1329 processed while we are changing them. */
1330 BLOCK_INPUT;
951f9df5 1331 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
5500c422 1332
7ab12479 1333 tem = p->next;
265a9e55 1334 if (!NILP (tem))
7ab12479
JB
1335 XWINDOW (tem)->prev = p->prev;
1336
1337 tem = p->prev;
265a9e55 1338 if (!NILP (tem))
7ab12479
JB
1339 XWINDOW (tem)->next = p->next;
1340
1341 if (EQ (window, par->hchild))
1342 par->hchild = p->next;
1343 if (EQ (window, par->vchild))
1344 par->vchild = p->next;
1345
1346 /* Find one of our siblings to give our space to. */
1347 sib = p->prev;
265a9e55 1348 if (NILP (sib))
7ab12479
JB
1349 {
1350 /* If p gives its space to its next sibling, that sibling needs
1351 to have its top/left side pulled back to where p's is.
1352 set_window_{height,width} will re-position the sibling's
1353 children. */
1354 sib = p->next;
7f4161e0
JB
1355 XWINDOW (sib)->top = p->top;
1356 XWINDOW (sib)->left = p->left;
7ab12479
JB
1357 }
1358
1359 /* Stretch that sibling. */
265a9e55 1360 if (!NILP (par->vchild))
7ab12479
JB
1361 set_window_height (sib,
1362 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
1363 1);
265a9e55 1364 if (!NILP (par->hchild))
7ab12479
JB
1365 set_window_width (sib,
1366 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
1367 1);
1368
1369 /* If parent now has only one child,
1370 put the child into the parent's place. */
7ab12479 1371 tem = par->hchild;
265a9e55 1372 if (NILP (tem))
7ab12479 1373 tem = par->vchild;
265a9e55 1374 if (NILP (XWINDOW (tem)->next))
7ab12479 1375 replace_window (parent, tem);
605be8af
JB
1376
1377 /* Since we may be deleting combination windows, we must make sure that
1378 not only p but all its children have been marked as deleted. */
1379 if (! NILP (p->hchild))
1380 delete_all_subwindows (XWINDOW (p->hchild));
1381 else if (! NILP (p->vchild))
1382 delete_all_subwindows (XWINDOW (p->vchild));
1383
1384 /* Mark this window as deleted. */
1385 p->buffer = p->hchild = p->vchild = Qnil;
5500c422
GM
1386
1387 /* Adjust glyph matrices. */
951f9df5 1388 adjust_glyphs (f);
05e71564 1389 UNBLOCK_INPUT;
7ab12479 1390}
67492200
GM
1391
1392
7ab12479 1393\f
67492200
GM
1394/***********************************************************************
1395 Window List
1396 ***********************************************************************/
1397
f95464e4
GM
1398/* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1399 pointer. This is a callback function for foreach_window, used in
1400 function window_list. */
67492200
GM
1401
1402static int
f95464e4 1403add_window_to_list (w, user_data)
67492200 1404 struct window *w;
f95464e4 1405 void *user_data;
67492200 1406{
f95464e4 1407 Lisp_Object *list = (Lisp_Object *) user_data;
67492200
GM
1408 Lisp_Object window;
1409 XSETWINDOW (window, w);
212116d6 1410 *list = Fcons (window, *list);
67492200
GM
1411 return 1;
1412}
1413
1414
1415/* Return a list of all windows, for use by next_window. If
1416 Vwindow_list is a list, return that list. Otherwise, build a new
1417 list, cache it in Vwindow_list, and return that. */
1418
1419static Lisp_Object
1420window_list ()
1421{
1422 if (!CONSP (Vwindow_list))
1423 {
1424 Lisp_Object tail;
212116d6 1425
67492200
GM
1426 Vwindow_list = Qnil;
1427 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
212116d6
GM
1428 {
1429 Lisp_Object args[2];
1430
1431 /* We are visiting windows in canonical order, and add
1432 new windows at the front of args[1], which means we
1433 have to reverse this list at the end. */
1434 args[1] = Qnil;
1435 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1436 args[0] = Vwindow_list;
1437 args[1] = Fnreverse (args[1]);
1438 Vwindow_list = Fnconc (2, args);
1439 }
67492200
GM
1440 }
1441
1442 return Vwindow_list;
1443}
1444
1445
118ea242
GM
1446/* Value is non-zero if WINDOW satisfies the constraints given by
1447 OWINDOW, MINIBUF and ALL_FRAMES.
67492200 1448
118ea242
GM
1449 MINIBUF t means WINDOW may be minibuffer windows.
1450 `lambda' means WINDOW may not be a minibuffer window.
1451 a window means a specific minibuffer window
67492200 1452
118ea242
GM
1453 ALL_FRAMES t means search all frames,
1454 nil means search just current frame,
1455 `visible' means search just visible frames,
1456 0 means search visible and iconified frames,
1457 a window means search the frame that window belongs to,
1458 a frame means consider windows on that frame, only. */
67492200
GM
1459
1460static int
118ea242
GM
1461candidate_window_p (window, owindow, minibuf, all_frames)
1462 Lisp_Object window, owindow, minibuf, all_frames;
67492200
GM
1463{
1464 struct window *w = XWINDOW (window);
1465 struct frame *f = XFRAME (w->frame);
1466 int candidate_p = 1;
1467
1468 if (!BUFFERP (w->buffer))
1469 candidate_p = 0;
1470 else if (MINI_WINDOW_P (w)
1471 && (EQ (minibuf, Qlambda)
1472 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1473 {
1474 /* If MINIBUF is `lambda' don't consider any mini-windows.
1475 If it is a window, consider only that one. */
1476 candidate_p = 0;
1477 }
118ea242
GM
1478 else if (EQ (all_frames, Qt))
1479 candidate_p = 1;
67492200 1480 else if (NILP (all_frames))
118ea242
GM
1481 {
1482 xassert (WINDOWP (owindow));
1483 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1484 }
67492200
GM
1485 else if (EQ (all_frames, Qvisible))
1486 {
1487 FRAME_SAMPLE_VISIBILITY (f);
1488 candidate_p = FRAME_VISIBLE_P (f);
1489 }
1490 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1491 {
1492 FRAME_SAMPLE_VISIBILITY (f);
1493 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1494 }
67492200
GM
1495 else if (WINDOWP (all_frames))
1496 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1497 || EQ (XWINDOW (all_frames)->frame, w->frame)
1498 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
118ea242
GM
1499 else if (FRAMEP (all_frames))
1500 candidate_p = EQ (all_frames, w->frame);
67492200
GM
1501
1502 return candidate_p;
1503}
1504
1505
1506/* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1507 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1508 ALL_FRAMES. */
1509
1510static void
1511decode_next_window_args (window, minibuf, all_frames)
1512 Lisp_Object *window, *minibuf, *all_frames;
1513{
1514 if (NILP (*window))
1515 *window = selected_window;
1516 else
b7826503 1517 CHECK_LIVE_WINDOW (*window);
67492200
GM
1518
1519 /* MINIBUF nil may or may not include minibuffers. Decide if it
1520 does. */
1521 if (NILP (*minibuf))
1522 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1523 else if (!EQ (*minibuf, Qt))
1524 *minibuf = Qlambda;
1525
1526 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1527 => count none of them, or a specific minibuffer window (the
1528 active one) to count. */
1529
1530 /* ALL_FRAMES nil doesn't specify which frames to include. */
1531 if (NILP (*all_frames))
1532 *all_frames = (!EQ (*minibuf, Qlambda)
1533 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1534 : Qnil);
1535 else if (EQ (*all_frames, Qvisible))
1536 ;
1537 else if (XFASTINT (*all_frames) == 0)
1538 ;
1539 else if (FRAMEP (*all_frames))
1540 ;
1541 else if (!EQ (*all_frames, Qt))
1542 *all_frames = Qnil;
1543
1544 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1545 search just current frame, `visible' meaning search just visible
1546 frames, 0 meaning search visible and iconified frames, or a
1547 window, meaning search the frame that window belongs to, or a
1548 frame, meaning consider windows on that frame, only. */
1549}
1550
1551
1552/* Return the next or previous window of WINDOW in canonical ordering
1553 of windows. NEXT_P non-zero means return the next window. See the
1554 documentation string of next-window for the meaning of MINIBUF and
1555 ALL_FRAMES. */
1556
1557static Lisp_Object
1558next_window (window, minibuf, all_frames, next_p)
1559 Lisp_Object window, minibuf, all_frames;
1560 int next_p;
1561{
1562 decode_next_window_args (&window, &minibuf, &all_frames);
1563
1564 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1565 return the first window on the frame. */
1566 if (FRAMEP (all_frames)
1567 && !EQ (all_frames, XWINDOW (window)->frame))
1568 return Fframe_first_window (all_frames);
1569
212116d6 1570 if (next_p)
67492200
GM
1571 {
1572 Lisp_Object list;
1573
1574 /* Find WINDOW in the list of all windows. */
1575 list = Fmemq (window, window_list ());
1576
1577 /* Scan forward from WINDOW to the end of the window list. */
1578 if (CONSP (list))
1579 for (list = XCDR (list); CONSP (list); list = XCDR (list))
118ea242 1580 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
67492200
GM
1581 break;
1582
1583 /* Scan from the start of the window list up to WINDOW. */
1584 if (!CONSP (list))
1585 for (list = Vwindow_list;
1586 CONSP (list) && !EQ (XCAR (list), window);
1587 list = XCDR (list))
118ea242 1588 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
67492200
GM
1589 break;
1590
1591 if (CONSP (list))
1592 window = XCAR (list);
1593 }
1594 else
1595 {
1596 Lisp_Object candidate, list;
1597
1598 /* Scan through the list of windows for candidates. If there are
1599 candidate windows in front of WINDOW, the last one of these
1600 is the one we want. If there are candidates following WINDOW
1601 in the list, again the last one of these is the one we want. */
1602 candidate = Qnil;
1603 for (list = window_list (); CONSP (list); list = XCDR (list))
1604 {
1605 if (EQ (XCAR (list), window))
1606 {
1607 if (WINDOWP (candidate))
1608 break;
1609 }
118ea242
GM
1610 else if (candidate_window_p (XCAR (list), window, minibuf,
1611 all_frames))
67492200
GM
1612 candidate = XCAR (list);
1613 }
1614
1615 if (WINDOWP (candidate))
1616 window = candidate;
1617 }
1618
1619 return window;
1620}
7ab12479 1621
7ab12479 1622
26f6279d 1623DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
fdb82f93
PJ
1624 doc: /* Return next window after WINDOW in canonical ordering of windows.
1625If omitted, WINDOW defaults to the selected window.
1626
1627Optional second arg MINIBUF t means count the minibuffer window even
1628if not active. MINIBUF nil or omitted means count the minibuffer iff
1629it is active. MINIBUF neither t nor nil means not to count the
1630minibuffer even if it is active.
1631
1632Several frames may share a single minibuffer; if the minibuffer
1633counts, all windows on all frames that share that minibuffer count
1634too. Therefore, `next-window' can be used to iterate through the
1635set of windows even when the minibuffer is on another frame. If the
1636minibuffer does not count, only windows from WINDOW's frame count.
1637
1638Optional third arg ALL-FRAMES t means include windows on all frames.
1639ALL-FRAMES nil or omitted means cycle within the frames as specified
1640above. ALL-FRAMES = `visible' means include windows on all visible frames.
1641ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1642If ALL-FRAMES is a frame, restrict search to windows on that frame.
1643Anything else means restrict to WINDOW's frame.
1644
1645If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1646`next-window' to iterate through the entire cycle of acceptable
1647windows, eventually ending up back at the window you started with.
1648`previous-window' traverses the same cycle, in the reverse order. */)
1649 (window, minibuf, all_frames)
67492200 1650 Lisp_Object window, minibuf, all_frames;
7ab12479 1651{
67492200 1652 return next_window (window, minibuf, all_frames, 1);
7ab12479
JB
1653}
1654
67492200 1655
26f6279d 1656DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
fdb82f93
PJ
1657 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1658If omitted, WINDOW defaults to the selected window.
1659
1660Optional second arg MINIBUF t means count the minibuffer window even
1661if not active. MINIBUF nil or omitted means count the minibuffer iff
1662it is active. MINIBUF neither t nor nil means not to count the
1663minibuffer even if it is active.
1664
1665Several frames may share a single minibuffer; if the minibuffer
1666counts, all windows on all frames that share that minibuffer count
1667too. Therefore, `previous-window' can be used to iterate through
1668the set of windows even when the minibuffer is on another frame. If
1669the minibuffer does not count, only windows from WINDOW's frame count
1670
1671Optional third arg ALL-FRAMES t means include windows on all frames.
1672ALL-FRAMES nil or omitted means cycle within the frames as specified
1673above. ALL-FRAMES = `visible' means include windows on all visible frames.
1674ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1675If ALL-FRAMES is a frame, restrict search to windows on that frame.
1676Anything else means restrict to WINDOW's frame.
1677
1678If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1679`previous-window' to iterate through the entire cycle of acceptable
1680windows, eventually ending up back at the window you started with.
1681`next-window' traverses the same cycle, in the reverse order. */)
1682 (window, minibuf, all_frames)
67492200 1683 Lisp_Object window, minibuf, all_frames;
7ab12479 1684{
67492200 1685 return next_window (window, minibuf, all_frames, 0);
7ab12479
JB
1686}
1687
67492200 1688
62c07cc7 1689DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
fdb82f93
PJ
1690 doc: /* Select the ARG'th different window on this frame.
1691All windows on current frame are arranged in a cyclic order.
1692This command selects the window ARG steps away in that order.
1693A negative ARG moves in the opposite order. If the optional second
1694argument ALL_FRAMES is non-nil, cycle through all frames. */)
1695 (arg, all_frames)
67492200 1696 Lisp_Object arg, all_frames;
7ab12479 1697{
67492200
GM
1698 Lisp_Object window;
1699 int i;
7ab12479 1700
b7826503 1701 CHECK_NUMBER (arg);
67492200
GM
1702 window = selected_window;
1703
1704 for (i = XINT (arg); i > 0; --i)
1705 window = Fnext_window (window, Qnil, all_frames);
1706 for (; i < 0; ++i)
1707 window = Fprevious_window (window, Qnil, all_frames);
50e88778 1708
67492200 1709 Fselect_window (window);
7ab12479
JB
1710 return Qnil;
1711}
67492200
GM
1712
1713
1714DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
fdb82f93
PJ
1715 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1716FRAME nil or omitted means use the selected frame.
1717WINDOW nil or omitted means use the selected window.
1718MINIBUF t means include the minibuffer window, even if it isn't active.
1719MINIBUF nil or omitted means include the minibuffer window only
1720if it's active.
1721MINIBUF neither nil nor t means never include the minibuffer window. */)
1722 (frame, minibuf, window)
cd2904bd
GM
1723 Lisp_Object frame, minibuf, window;
1724{
cd2904bd
GM
1725 if (NILP (window))
1726 window = selected_window;
1727 if (NILP (frame))
1728 frame = selected_frame;
1729
1730 if (!EQ (frame, XWINDOW (window)->frame))
1731 error ("Window is on a different frame");
1732
1733 return window_list_1 (window, minibuf, frame);
1734}
1735
1736
1737/* Return a list of windows in canonical ordering. Arguments are like
1738 for `next-window'. */
1739
1740static Lisp_Object
1741window_list_1 (window, minibuf, all_frames)
069f5950 1742 Lisp_Object window, minibuf, all_frames;
67492200
GM
1743{
1744 Lisp_Object tail, list;
1745
1746 decode_next_window_args (&window, &minibuf, &all_frames);
1747 list = Qnil;
1748
1749 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
118ea242 1750 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
67492200
GM
1751 list = Fcons (XCAR (tail), list);
1752
118ea242 1753 return Fnreverse (list);
67492200
GM
1754}
1755
1756
7ab12479
JB
1757\f
1758/* Look at all windows, performing an operation specified by TYPE
1759 with argument OBJ.
75d8f668 1760 If FRAMES is Qt, look at all frames;
75d8f668 1761 Qnil, look at just the selected frame;
89bca612 1762 Qvisible, look at visible frames;
75d8f668 1763 a frame, just look at windows on that frame.
85fe3b5e 1764 If MINI is non-zero, perform the operation on minibuffer windows too. */
7ab12479
JB
1765
1766enum window_loop
1767{
1768 WINDOW_LOOP_UNUSED,
1769 GET_BUFFER_WINDOW, /* Arg is buffer */
1770 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1771 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1772 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1773 GET_LARGEST_WINDOW,
3f8ab7bd
RS
1774 UNSHOW_BUFFER, /* Arg is buffer */
1775 CHECK_ALL_WINDOWS
7ab12479
JB
1776};
1777
1778static Lisp_Object
44fa5b1e 1779window_loop (type, obj, mini, frames)
7ab12479 1780 enum window_loop type;
118ea242 1781 Lisp_Object obj, frames;
7ab12479
JB
1782 int mini;
1783{
118ea242
GM
1784 Lisp_Object window, windows, best_window, frame_arg;
1785 struct frame *f;
ffdc852d 1786 struct gcpro gcpro1;
118ea242 1787
44fa5b1e
JB
1788 /* If we're only looping through windows on a particular frame,
1789 frame points to that frame. If we're looping through windows
1790 on all frames, frame is 0. */
1791 if (FRAMEP (frames))
118ea242 1792 f = XFRAME (frames);
44fa5b1e 1793 else if (NILP (frames))
118ea242 1794 f = SELECTED_FRAME ();
7ab12479 1795 else
118ea242
GM
1796 f = NULL;
1797
1798 if (f)
89bca612 1799 frame_arg = Qlambda;
f812f9c6
RS
1800 else if (XFASTINT (frames) == 0)
1801 frame_arg = frames;
89bca612
RS
1802 else if (EQ (frames, Qvisible))
1803 frame_arg = frames;
118ea242
GM
1804 else
1805 frame_arg = Qt;
7ab12479 1806
89bca612
RS
1807 /* frame_arg is Qlambda to stick to one frame,
1808 Qvisible to consider all visible frames,
1809 or Qt otherwise. */
1810
7ab12479 1811 /* Pick a window to start with. */
017b2bad 1812 if (WINDOWP (obj))
118ea242
GM
1813 window = obj;
1814 else if (f)
1815 window = FRAME_SELECTED_WINDOW (f);
7ab12479 1816 else
118ea242 1817 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
4b206065 1818
cd2904bd 1819 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
118ea242 1820 GCPRO1 (windows);
7ab12479 1821 best_window = Qnil;
118ea242
GM
1822
1823 for (; CONSP (windows); windows = CDR (windows))
7ab12479 1824 {
118ea242
GM
1825 struct window *w;
1826
1827 window = XCAR (windows);
1828 w = XWINDOW (window);
1829
1830 /* Note that we do not pay attention here to whether the frame
1831 is visible, since Fwindow_list skips non-visible frames if
1832 that is desired, under the control of frame_arg. */
1833 if (!MINI_WINDOW_P (w)
65a04b96
RS
1834 /* For UNSHOW_BUFFER, we must always consider all windows. */
1835 || type == UNSHOW_BUFFER
7ab12479
JB
1836 || (mini && minibuf_level > 0))
1837 switch (type)
1838 {
1839 case GET_BUFFER_WINDOW:
118ea242 1840 if (EQ (w->buffer, obj)
5c204627
RS
1841 /* Don't find any minibuffer window
1842 except the one that is currently in use. */
118ea242
GM
1843 && (MINI_WINDOW_P (w)
1844 ? EQ (window, minibuf_window)
1845 : 1))
1846 {
5ddc1b75
GM
1847 if (NILP (best_window))
1848 best_window = window;
1849 else if (EQ (window, selected_window))
1850 /* For compatibility with 20.x, prefer to return
1851 selected-window. */
1852 best_window = window;
118ea242 1853 }
7ab12479
JB
1854 break;
1855
1856 case GET_LRU_WINDOW:
1857 /* t as arg means consider only full-width windows */
118ea242 1858 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (w))
7ab12479 1859 break;
7ab12479 1860 /* Ignore dedicated windows and minibuffers. */
118ea242 1861 if (MINI_WINDOW_P (w) || !NILP (w->dedicated))
7ab12479 1862 break;
265a9e55 1863 if (NILP (best_window)
7ab12479 1864 || (XFASTINT (XWINDOW (best_window)->use_time)
118ea242
GM
1865 > XFASTINT (w->use_time)))
1866 best_window = window;
7ab12479
JB
1867 break;
1868
1869 case DELETE_OTHER_WINDOWS:
118ea242
GM
1870 if (!EQ (window, obj))
1871 Fdelete_window (window);
7ab12479
JB
1872 break;
1873
1874 case DELETE_BUFFER_WINDOWS:
118ea242 1875 if (EQ (w->buffer, obj))
7ab12479 1876 {
118ea242 1877 struct frame *f = XFRAME (WINDOW_FRAME (w));
3548e138
RS
1878
1879 /* If this window is dedicated, and in a frame of its own,
1880 kill the frame. */
118ea242
GM
1881 if (EQ (window, FRAME_ROOT_WINDOW (f))
1882 && !NILP (w->dedicated)
3548e138 1883 && other_visible_frames (f))
7ab12479 1884 {
3548e138
RS
1885 /* Skip the other windows on this frame.
1886 There might be one, the minibuffer! */
118ea242
GM
1887 while (CONSP (XCDR (windows))
1888 && EQ (XWINDOW (XCAR (windows))->frame,
1889 XWINDOW (XCAR (XCDR (windows)))->frame))
1890 windows = XCDR (windows);
1891
3548e138 1892 /* Now we can safely delete the frame. */
118ea242
GM
1893 Fdelete_frame (w->frame, Qnil);
1894 }
1895 else if (NILP (w->parent))
1896 {
1897 /* If we're deleting the buffer displayed in the
1898 only window on the frame, find a new buffer to
1899 display there. */
1900 Lisp_Object buffer;
1901 buffer = Fother_buffer (obj, Qnil, w->frame);
118ea242
GM
1902 Fset_window_buffer (window, buffer);
1903 if (EQ (window, selected_window))
1904 Fset_buffer (w->buffer);
7ab12479
JB
1905 }
1906 else
118ea242 1907 Fdelete_window (window);
7ab12479
JB
1908 }
1909 break;
1910
1911 case GET_LARGEST_WINDOW:
7ab12479 1912 {
118ea242 1913 /* Ignore dedicated windows and minibuffers. */
c930dfab 1914 if (MINI_WINDOW_P (w) || !NILP (w->dedicated))
118ea242
GM
1915 break;
1916
c930dfab 1917 if (NILP (best_window))
118ea242 1918 best_window = window;
c930dfab
GM
1919 else
1920 {
1921 struct window *b = XWINDOW (best_window);
1922 if (XFASTINT (w->height) * XFASTINT (w->width)
1923 > XFASTINT (b->height) * XFASTINT (b->width))
1924 best_window = window;
1925 }
7ab12479
JB
1926 }
1927 break;
1928
1929 case UNSHOW_BUFFER:
118ea242 1930 if (EQ (w->buffer, obj))
7ab12479 1931 {
118ea242
GM
1932 Lisp_Object buffer;
1933 struct frame *f = XFRAME (w->frame);
1934
7ab12479 1935 /* Find another buffer to show in this window. */
118ea242 1936 buffer = Fother_buffer (obj, Qnil, w->frame);
118ea242 1937
38ab08d1
RS
1938 /* If this window is dedicated, and in a frame of its own,
1939 kill the frame. */
118ea242
GM
1940 if (EQ (window, FRAME_ROOT_WINDOW (f))
1941 && !NILP (w->dedicated)
38ab08d1 1942 && other_visible_frames (f))
45945a7b
RS
1943 {
1944 /* Skip the other windows on this frame.
1945 There might be one, the minibuffer! */
118ea242
GM
1946 while (CONSP (XCDR (windows))
1947 && EQ (XWINDOW (XCAR (windows))->frame,
1948 XWINDOW (XCAR (XCDR (windows)))->frame))
1949 windows = XCDR (windows);
1950
45945a7b 1951 /* Now we can safely delete the frame. */
118ea242 1952 Fdelete_frame (w->frame, Qnil);
45945a7b 1953 }
38ab08d1 1954 else
38ab08d1
RS
1955 {
1956 /* Otherwise show a different buffer in the window. */
118ea242
GM
1957 w->dedicated = Qnil;
1958 Fset_window_buffer (window, buffer);
1959 if (EQ (window, selected_window))
1960 Fset_buffer (w->buffer);
38ab08d1 1961 }
7ab12479
JB
1962 }
1963 break;
3f8ab7bd
RS
1964
1965 /* Check for a window that has a killed buffer. */
1966 case CHECK_ALL_WINDOWS:
118ea242
GM
1967 if (! NILP (w->buffer)
1968 && NILP (XBUFFER (w->buffer)->name))
3f8ab7bd 1969 abort ();
118ea242 1970 break;
6bbd7a29
GM
1971
1972 case WINDOW_LOOP_UNUSED:
1973 break;
7ab12479 1974 }
7ab12479 1975 }
7ab12479 1976
118ea242 1977 UNGCPRO;
7ab12479 1978 return best_window;
37962e60 1979}
605be8af 1980
3f8ab7bd
RS
1981/* Used for debugging. Abort if any window has a dead buffer. */
1982
233a4a2c 1983void
3f8ab7bd
RS
1984check_all_windows ()
1985{
1986 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
1987}
1988
7ab12479 1989DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
fdb82f93
PJ
1990 doc: /* Return the window least recently selected or used for display.
1991If optional argument FRAME is `visible', search all visible frames.
1992If FRAME is 0, search all visible and iconified frames.
1993If FRAME is t, search all frames.
1994If FRAME is nil, search only the selected frame.
1995If FRAME is a frame, search only that frame. */)
1996 (frame)
1997 Lisp_Object frame;
7ab12479
JB
1998{
1999 register Lisp_Object w;
2000 /* First try for a window that is full-width */
89bca612 2001 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
265a9e55 2002 if (!NILP (w) && !EQ (w, selected_window))
7ab12479
JB
2003 return w;
2004 /* If none of them, try the rest */
89bca612 2005 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
7ab12479
JB
2006}
2007
2008DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
fdb82f93
PJ
2009 doc: /* Return the largest window in area.
2010If optional argument FRAME is `visible', search all visible frames.
2011If FRAME is 0, search all visible and iconified frames.
2012If FRAME is t, search all frames.
2013If FRAME is nil, search only the selected frame.
2014If FRAME is a frame, search only that frame. */)
2015 (frame)
2016 Lisp_Object frame;
7ab12479
JB
2017{
2018 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
44fa5b1e 2019 frame);
7ab12479
JB
2020}
2021
2022DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
fdb82f93
PJ
2023 doc: /* Return a window currently displaying BUFFER, or nil if none.
2024If optional argument FRAME is `visible', search all visible frames.
2025If optional argument FRAME is 0, search all visible and iconified frames.
2026If FRAME is t, search all frames.
2027If FRAME is nil, search only the selected frame.
2028If FRAME is a frame, search only that frame. */)
2029 (buffer, frame)
2030 Lisp_Object buffer, frame;
7ab12479
JB
2031{
2032 buffer = Fget_buffer (buffer);
017b2bad 2033 if (BUFFERP (buffer))
44fa5b1e 2034 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
7ab12479
JB
2035 else
2036 return Qnil;
2037}
2038
2039DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
fdb82f93
PJ
2040 0, 1, "",
2041 doc: /* Make WINDOW (or the selected window) fill its frame.
2042Only the frame WINDOW is on is affected.
2043This function tries to reduce display jumps
2044by keeping the text previously visible in WINDOW
2045in the same place on the frame. Doing this depends on
2046the value of (window-start WINDOW), so if calling this function
2047in a program gives strange scrolling, make sure the window-start
2048value is reasonable when this function is called. */)
2049 (window)
7ab12479
JB
2050 Lisp_Object window;
2051{
2052 struct window *w;
00d3d838 2053 int startpos;
85fe3b5e 2054 int top, new_top;
7ab12479 2055
265a9e55 2056 if (NILP (window))
7ab12479
JB
2057 window = selected_window;
2058 else
b7826503 2059 CHECK_LIVE_WINDOW (window);
7ab12479 2060 w = XWINDOW (window);
a2b38b3c 2061
00d3d838 2062 startpos = marker_position (w->start);
5500c422 2063 top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
7ab12479 2064
a2b38b3c
RS
2065 if (MINI_WINDOW_P (w) && top > 0)
2066 error ("Can't expand minibuffer to full frame");
2067
70728a80 2068 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
7ab12479 2069
00d3d838
KH
2070 /* Try to minimize scrolling, by setting the window start to the point
2071 will cause the text at the old window start to be at the same place
2072 on the frame. But don't try to do this if the window start is
2073 outside the visible portion (as might happen when the display is
2074 not current, due to typeahead). */
85fe3b5e
GM
2075 new_top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2076 if (new_top != top
2077 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
00d3d838
KH
2078 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2079 {
2080 struct position pos;
2081 struct buffer *obuf = current_buffer;
2082
2083 Fset_buffer (w->buffer);
2084 /* This computation used to temporarily move point, but that can
2085 have unwanted side effects due to text properties. */
0383eb57 2086 pos = *vmotion (startpos, -top, w);
4d047f50 2087
b73ea88e 2088 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
85fe3b5e 2089 w->window_end_valid = Qnil;
b73ea88e
RS
2090 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2091 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
00d3d838 2092 : Qnil);
80622eec
RS
2093 /* We need to do this, so that the window-scroll-functions
2094 get called. */
4d047f50 2095 w->optional_new_start = Qt;
00d3d838
KH
2096
2097 set_buffer_internal (obuf);
2098 }
5500c422 2099
7ab12479
JB
2100 return Qnil;
2101}
2102
2103DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
fdb82f93
PJ
2104 1, 2, "bDelete windows on (buffer): ",
2105 doc: /* Delete all windows showing BUFFER.
2106Optional second argument FRAME controls which frames are affected.
2107If optional argument FRAME is `visible', search all visible frames.
2108If FRAME is 0, search all visible and iconified frames.
2109If FRAME is nil, search all frames.
2110If FRAME is t, search only the selected frame.
2111If FRAME is a frame, search only that frame. */)
2112 (buffer, frame)
26f6279d 2113 Lisp_Object buffer, frame;
7ab12479 2114{
26f6279d
JB
2115 /* FRAME uses t and nil to mean the opposite of what window_loop
2116 expects. */
c520265e
RS
2117 if (NILP (frame))
2118 frame = Qt;
2119 else if (EQ (frame, Qt))
2120 frame = Qnil;
26f6279d 2121
265a9e55 2122 if (!NILP (buffer))
7ab12479
JB
2123 {
2124 buffer = Fget_buffer (buffer);
b7826503 2125 CHECK_BUFFER (buffer);
26f6279d 2126 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
7ab12479 2127 }
5500c422 2128
7ab12479
JB
2129 return Qnil;
2130}
2131
2132DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
fdb82f93
PJ
2133 Sreplace_buffer_in_windows,
2134 1, 1, "bReplace buffer in windows: ",
2135 doc: /* Replace BUFFER with some other buffer in all windows showing it. */)
2136 (buffer)
7ab12479
JB
2137 Lisp_Object buffer;
2138{
265a9e55 2139 if (!NILP (buffer))
7ab12479
JB
2140 {
2141 buffer = Fget_buffer (buffer);
b7826503 2142 CHECK_BUFFER (buffer);
7ab12479
JB
2143 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2144 }
2145 return Qnil;
2146}
ff58478b
RS
2147
2148/* Replace BUFFER with some other buffer in all windows
2149 of all frames, even those on other keyboards. */
2150
2151void
2152replace_buffer_in_all_windows (buffer)
2153 Lisp_Object buffer;
2154{
27abb84f 2155#ifdef MULTI_KBOARD
ff58478b
RS
2156 Lisp_Object tail, frame;
2157
ff58478b
RS
2158 /* A single call to window_loop won't do the job
2159 because it only considers frames on the current keyboard.
2160 So loop manually over frames, and handle each one. */
2161 FOR_EACH_FRAME (tail, frame)
db7f721d 2162 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
ff58478b 2163#else
db7f721d 2164 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
ff58478b
RS
2165#endif
2166}
7ab12479
JB
2167\f
2168/* Set the height of WINDOW and all its inferiors. */
a481b3ea
JB
2169
2170/* The smallest acceptable dimensions for a window. Anything smaller
2171 might crash Emacs. */
5500c422 2172
a481b3ea 2173#define MIN_SAFE_WINDOW_WIDTH (2)
dc1ab1ee 2174#define MIN_SAFE_WINDOW_HEIGHT (1)
a481b3ea
JB
2175
2176/* Make sure that window_min_height and window_min_width are
2177 not too small; if they are, set them to safe minima. */
2178
2179static void
2180check_min_window_sizes ()
2181{
2182 /* Smaller values might permit a crash. */
2183 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2184 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2185 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2186 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2187}
2188
2189/* If *ROWS or *COLS are too small a size for FRAME, set them to the
2190 minimum allowable size. */
5500c422 2191
605be8af 2192void
a481b3ea 2193check_frame_size (frame, rows, cols)
605be8af
JB
2194 FRAME_PTR frame;
2195 int *rows, *cols;
a481b3ea 2196{
628df3bf 2197 /* For height, we have to see:
54b8bcb5
RS
2198 how many windows the frame has at minimum (one or two),
2199 and whether it has a menu bar or other special stuff at the top. */
2200 int min_height
2201 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2202 ? MIN_SAFE_WINDOW_HEIGHT
2203 : 2 * MIN_SAFE_WINDOW_HEIGHT);
5500c422
GM
2204
2205 if (FRAME_TOP_MARGIN (frame) > 0)
2206 min_height += FRAME_TOP_MARGIN (frame);
a481b3ea
JB
2207
2208 if (*rows < min_height)
2209 *rows = min_height;
2210 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2211 *cols = MIN_SAFE_WINDOW_WIDTH;
2212}
2213
c1636aa6 2214
233a4a2c
GM
2215/* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2216 check if W's width can be changed, otherwise check W's height.
2217 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2218 siblings, too. If none of the siblings is resizable, WINDOW isn't
2219 either. */
c1636aa6 2220
233a4a2c
GM
2221static int
2222window_fixed_size_p (w, width_p, check_siblings_p)
2223 struct window *w;
2224 int width_p, check_siblings_p;
2225{
2226 int fixed_p;
2227 struct window *c;
2228
2229 if (!NILP (w->hchild))
2230 {
2231 c = XWINDOW (w->hchild);
2232
2233 if (width_p)
2234 {
2235 /* A horiz. combination is fixed-width if all of if its
2236 children are. */
2237 while (c && window_fixed_size_p (c, width_p, 0))
2238 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2239 fixed_p = c == NULL;
2240 }
2241 else
2242 {
2243 /* A horiz. combination is fixed-height if one of if its
2244 children is. */
2245 while (c && !window_fixed_size_p (c, width_p, 0))
2246 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2247 fixed_p = c != NULL;
2248 }
2249 }
2250 else if (!NILP (w->vchild))
2251 {
2252 c = XWINDOW (w->vchild);
2253
2254 if (width_p)
2255 {
2256 /* A vert. combination is fixed-width if one of if its
2257 children is. */
2258 while (c && !window_fixed_size_p (c, width_p, 0))
2259 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2260 fixed_p = c != NULL;
2261 }
2262 else
2263 {
2264 /* A vert. combination is fixed-height if all of if its
2265 children are. */
2266 while (c && window_fixed_size_p (c, width_p, 0))
2267 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2268 fixed_p = c == NULL;
2269 }
2270 }
2271 else if (BUFFERP (w->buffer))
2272 {
a34dfd12
GM
2273 if (w->height_fixed_p && !width_p)
2274 fixed_p = 1;
2275 else
233a4a2c 2276 {
a34dfd12
GM
2277 struct buffer *old = current_buffer;
2278 Lisp_Object val;
2279
2280 current_buffer = XBUFFER (w->buffer);
2281 val = find_symbol_value (Qwindow_size_fixed);
2282 current_buffer = old;
2283
2284 fixed_p = 0;
2285 if (!EQ (val, Qunbound))
2286 {
2287 fixed_p = !NILP (val);
2288
2289 if (fixed_p
2290 && ((EQ (val, Qheight) && width_p)
2291 || (EQ (val, Qwidth) && !width_p)))
2292 fixed_p = 0;
2293 }
233a4a2c
GM
2294 }
2295
2296 /* Can't tell if this one is resizable without looking at
2297 siblings. If all siblings are fixed-size this one is too. */
2298 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2299 {
2300 Lisp_Object child;
2301
2302 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2303 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2304 break;
2305
2306 if (NILP (child))
2307 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2308 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2309 break;
2310
2311 if (NILP (child))
2312 fixed_p = 1;
2313 }
2314 }
2315 else
2316 fixed_p = 1;
2317
2318 return fixed_p;
2319}
2320
2321
2322/* Return the minimum size of window W, not taking fixed-width windows
2323 into account. WIDTH_P non-zero means return the minimum width,
2324 otherwise return the minimum height. If W is a combination window,
2325 compute the minimum size from the minimum sizes of W's children. */
2326
2327static int
2328window_min_size_1 (w, width_p)
c1636aa6
GM
2329 struct window *w;
2330 int width_p;
2331{
233a4a2c 2332 struct window *c;
c1636aa6
GM
2333 int size;
2334
233a4a2c
GM
2335 if (!NILP (w->hchild))
2336 {
2337 c = XWINDOW (w->hchild);
2338 size = 0;
2339
2340 if (width_p)
2341 {
2342 /* The min width of a horizontal combination is
2343 the sum of the min widths of its children. */
2344 while (c)
2345 {
2346 size += window_min_size_1 (c, width_p);
2347 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2348 }
2349 }
2350 else
2351 {
2352 /* The min height a horizontal combination equals
2353 the maximum of all min height of its children. */
2354 while (c)
2355 {
2356 int min_size = window_min_size_1 (c, width_p);
2357 size = max (min_size, size);
2358 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2359 }
2360 }
2361 }
2362 else if (!NILP (w->vchild))
2363 {
2364 c = XWINDOW (w->vchild);
2365 size = 0;
2366
2367 if (width_p)
2368 {
2369 /* The min width of a vertical combination is
2370 the maximum of the min widths of its children. */
2371 while (c)
2372 {
2373 int min_size = window_min_size_1 (c, width_p);
2374 size = max (min_size, size);
2375 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2376 }
2377 }
2378 else
2379 {
2380 /* The min height of a vertical combination equals
2381 the sum of the min height of its children. */
2382 while (c)
2383 {
2384 size += window_min_size_1 (c, width_p);
2385 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2386 }
2387 }
2388 }
c1636aa6
GM
2389 else
2390 {
233a4a2c
GM
2391 if (width_p)
2392 size = window_min_width;
c1636aa6 2393 else
233a4a2c
GM
2394 {
2395 if (MINI_WINDOW_P (w)
2396 || (!WINDOW_WANTS_MODELINE_P (w)
045dee35 2397 && !WINDOW_WANTS_HEADER_LINE_P (w)))
233a4a2c
GM
2398 size = 1;
2399 else
2400 size = window_min_height;
2401 }
c1636aa6
GM
2402 }
2403
2404 return size;
2405}
2406
2407
233a4a2c
GM
2408/* Return the minimum size of window W, taking fixed-size windows into
2409 account. WIDTH_P non-zero means return the minimum width,
f984d4fc
GM
2410 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2411 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2412 unless FIXED is null. */
7ab12479 2413
233a4a2c 2414static int
f984d4fc 2415window_min_size (w, width_p, ignore_fixed_p, fixed)
233a4a2c 2416 struct window *w;
f984d4fc 2417 int width_p, ignore_fixed_p, *fixed;
233a4a2c
GM
2418{
2419 int size, fixed_p;
2420
f984d4fc
GM
2421 if (ignore_fixed_p)
2422 fixed_p = 0;
2423 else
2424 fixed_p = window_fixed_size_p (w, width_p, 1);
2425
233a4a2c
GM
2426 if (fixed)
2427 *fixed = fixed_p;
2428
2429 if (fixed_p)
2430 size = width_p ? XFASTINT (w->width) : XFASTINT (w->height);
2431 else
2432 size = window_min_size_1 (w, width_p);
f984d4fc 2433
233a4a2c
GM
2434 return size;
2435}
2436
2437
2438/* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2439 WINDOW's width. Resize WINDOW's children, if any, so that they
2440 keep their proportionate size relative to WINDOW. Propagate
2441 WINDOW's top or left edge position to children. Delete windows
192c3131
RS
2442 that become too small unless NODELETE_P is non-zero.
2443
2444 If NODELETE_P is 2, that means we do delete windows that are
2445 too small, even if they were too small before! */
233a4a2c
GM
2446
2447static void
2448size_window (window, size, width_p, nodelete_p)
7ab12479 2449 Lisp_Object window;
233a4a2c 2450 int size, width_p, nodelete_p;
7ab12479 2451{
233a4a2c
GM
2452 struct window *w = XWINDOW (window);
2453 struct window *c;
2454 Lisp_Object child, *forward, *sideward;
2455 int old_size, min_size;
7ab12479 2456
192c3131
RS
2457 if (nodelete_p == 2)
2458 nodelete_p = 0;
2459
a481b3ea 2460 check_min_window_sizes ();
7ae2f10f 2461 size = max (0, size);
233a4a2c 2462
b5f05b50
GM
2463 /* If the window has been "too small" at one point,
2464 don't delete it for being "too small" in the future.
2465 Preserve it as long as that is at all possible. */
233a4a2c
GM
2466 if (width_p)
2467 {
8b6d9dc9 2468 old_size = XINT (w->width);
233a4a2c
GM
2469 min_size = window_min_width;
2470 }
2471 else
2472 {
8b6d9dc9 2473 old_size = XINT (w->height);
233a4a2c
GM
2474 min_size = window_min_height;
2475 }
192c3131
RS
2476
2477 if (old_size < min_size && nodelete_p != 2)
b5f05b50
GM
2478 w->too_small_ok = Qt;
2479
233a4a2c 2480 /* Maybe delete WINDOW if it's too small. */
192c3131 2481 if (nodelete_p != 1 && !NILP (w->parent))
7ab12479 2482 {
b5f05b50 2483 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
233a4a2c 2484 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
b5f05b50 2485 else
233a4a2c 2486 min_size = width_p ? window_min_width : window_min_height;
b5f05b50 2487
233a4a2c 2488 if (size < min_size)
c1636aa6
GM
2489 {
2490 delete_window (window);
2491 return;
2492 }
7ab12479
JB
2493 }
2494
233a4a2c 2495 /* Set redisplay hints. */
7ae2f10f
GM
2496 w->last_modified = make_number (0);
2497 w->last_overlay_modified = make_number (0);
7ab12479 2498 windows_or_buffers_changed++;
7ae2f10f 2499 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
29aeee73 2500
233a4a2c
GM
2501 if (width_p)
2502 {
2503 sideward = &w->vchild;
2504 forward = &w->hchild;
7ae2f10f 2505 w->width = make_number (size);
233a4a2c
GM
2506 }
2507 else
2508 {
2509 sideward = &w->hchild;
2510 forward = &w->vchild;
7ae2f10f 2511 w->height = make_number (size);
0130fe1a 2512 w->orig_height = Qnil;
233a4a2c
GM
2513 }
2514
2515 if (!NILP (*sideward))
7ab12479 2516 {
233a4a2c 2517 for (child = *sideward; !NILP (child); child = c->next)
7ab12479 2518 {
233a4a2c
GM
2519 c = XWINDOW (child);
2520 if (width_p)
2521 c->left = w->left;
2522 else
2523 c->top = w->top;
2524 size_window (child, size, width_p, nodelete_p);
7ab12479
JB
2525 }
2526 }
233a4a2c 2527 else if (!NILP (*forward))
7ab12479 2528 {
233a4a2c
GM
2529 int fixed_size, each, extra, n;
2530 int resize_fixed_p, nfixed;
8b6d9dc9 2531 int last_pos, first_pos, nchildren, total;
233a4a2c
GM
2532
2533 /* Determine the fixed-size portion of the this window, and the
2534 number of child windows. */
8b6d9dc9 2535 fixed_size = nchildren = nfixed = total = 0;
233a4a2c 2536 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
7ab12479 2537 {
8b6d9dc9
GM
2538 int child_size;
2539
7ab12479 2540 c = XWINDOW (child);
8b6d9dc9
GM
2541 child_size = width_p ? XINT (c->width) : XINT (c->height);
2542 total += child_size;
2543
233a4a2c
GM
2544 if (window_fixed_size_p (c, width_p, 0))
2545 {
8b6d9dc9 2546 fixed_size += child_size;
233a4a2c
GM
2547 ++nfixed;
2548 }
2549 }
7ab12479 2550
233a4a2c
GM
2551 /* If the new size is smaller than fixed_size, or if there
2552 aren't any resizable windows, allow resizing fixed-size
2553 windows. */
2554 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2555
2556 /* Compute how many lines/columns to add to each child. The
2557 value of extra takes care of rounding errors. */
2558 n = resize_fixed_p ? nchildren : nchildren - nfixed;
8b6d9dc9
GM
2559 each = (size - total) / n;
2560 extra = (size - total) - n * each;
233a4a2c
GM
2561
2562 /* Compute new children heights and edge positions. */
8b6d9dc9 2563 first_pos = width_p ? XINT (w->left) : XINT (w->top);
233a4a2c
GM
2564 last_pos = first_pos;
2565 for (child = *forward; !NILP (child); child = c->next)
2566 {
2567 int new_size, old_size;
2568
2569 c = XWINDOW (child);
2570 old_size = width_p ? XFASTINT (c->width) : XFASTINT (c->height);
2571 new_size = old_size;
7ab12479 2572
233a4a2c
GM
2573 /* The top or left edge position of this child equals the
2574 bottom or right edge of its predecessor. */
2575 if (width_p)
2576 c->left = make_number (last_pos);
2577 else
2578 c->top = make_number (last_pos);
7ab12479 2579
233a4a2c
GM
2580 /* If this child can be resized, do it. */
2581 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2582 {
2583 new_size = old_size + each + extra;
2584 extra = 0;
2585 }
2586
2587 /* Set new height. Note that size_window also propagates
2588 edge positions to children, so it's not a no-op if we
2589 didn't change the child's size. */
2590 size_window (child, new_size, width_p, 1);
2591
2592 /* Remember the bottom/right edge position of this child; it
2593 will be used to set the top/left edge of the next child. */
2594 last_pos += new_size;
7ab12479 2595 }
233a4a2c
GM
2596
2597 /* We should have covered the parent exactly with child windows. */
2598 xassert (size == last_pos - first_pos);
2599
7ab12479 2600 /* Now delete any children that became too small. */
233a4a2c
GM
2601 if (!nodelete_p)
2602 for (child = *forward; !NILP (child); child = c->next)
7ab12479 2603 {
233a4a2c
GM
2604 int child_size;
2605 c = XWINDOW (child);
8b6d9dc9 2606 child_size = width_p ? XINT (c->width) : XINT (c->height);
192c3131 2607 size_window (child, child_size, width_p, 2);
7ab12479
JB
2608 }
2609 }
2610}
2611
233a4a2c
GM
2612/* Set WINDOW's height to HEIGHT, and recursively change the height of
2613 WINDOW's children. NODELETE non-zero means don't delete windows
2614 that become too small in the process. (The caller should check
2615 later and do so if appropriate.) */
7ab12479 2616
5e14b1fc 2617void
233a4a2c 2618set_window_height (window, height, nodelete)
7ab12479 2619 Lisp_Object window;
233a4a2c 2620 int height;
7ab12479
JB
2621 int nodelete;
2622{
233a4a2c
GM
2623 size_window (window, height, 0, nodelete);
2624}
7ab12479 2625
7ab12479 2626
233a4a2c
GM
2627/* Set WINDOW's width to WIDTH, and recursively change the width of
2628 WINDOW's children. NODELETE non-zero means don't delete windows
2629 that become too small in the process. (The caller should check
2630 later and do so if appropriate.) */
7ab12479 2631
233a4a2c
GM
2632void
2633set_window_width (window, width, nodelete)
2634 Lisp_Object window;
2635 int width;
2636 int nodelete;
2637{
2638 size_window (window, width, 1, nodelete);
7ab12479 2639}
233a4a2c 2640
7ab12479 2641\f
1d8d96fa 2642int window_select_count;
7ab12479 2643
5b03d3c0
RS
2644Lisp_Object
2645Fset_window_buffer_unwind (obuf)
2646 Lisp_Object obuf;
2647{
2648 Fset_buffer (obuf);
2649 return Qnil;
2650}
2651
7ab12479 2652
5500c422
GM
2653/* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2654 means it's allowed to run hooks. See make_frame for a case where
2655 it's not allowed. */
7ab12479 2656
5500c422
GM
2657void
2658set_window_buffer (window, buffer, run_hooks_p)
2659 Lisp_Object window, buffer;
2660 int run_hooks_p;
2661{
2662 struct window *w = XWINDOW (window);
2663 struct buffer *b = XBUFFER (buffer);
2664 int count = specpdl_ptr - specpdl;
7ab12479
JB
2665
2666 w->buffer = buffer;
86e48436
RS
2667
2668 if (EQ (window, selected_window))
5500c422 2669 b->last_selected_window = window;
beb4e312
RS
2670
2671 /* Update time stamps of buffer display. */
5500c422
GM
2672 if (INTEGERP (b->display_count))
2673 XSETINT (b->display_count, XINT (b->display_count) + 1);
2674 b->display_time = Fcurrent_time ();
86e48436 2675
d834a2e9 2676 XSETFASTINT (w->window_end_pos, 0);
5500c422
GM
2677 XSETFASTINT (w->window_end_vpos, 0);
2678 bzero (&w->last_cursor, sizeof w->last_cursor);
5a41ab94 2679 w->window_end_valid = Qnil;
1c686c99 2680 w->hscroll = w->min_hscroll = make_number (0);
224227d1 2681 w->vscroll = 0;
5500c422 2682 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
7ab12479 2683 set_marker_restricted (w->start,
5500c422 2684 make_number (b->last_window_start),
7ab12479
JB
2685 buffer);
2686 w->start_at_line_beg = Qnil;
e36ab06b 2687 w->force_start = Qnil;
d834a2e9 2688 XSETFASTINT (w->last_modified, 0);
3cd21523 2689 XSETFASTINT (w->last_overlay_modified, 0);
7ab12479 2690 windows_or_buffers_changed++;
5b03d3c0
RS
2691
2692 /* We must select BUFFER for running the window-scroll-functions.
2693 If WINDOW is selected, switch permanently.
2694 Otherwise, switch but go back to the ambient buffer afterward. */
7ab12479
JB
2695 if (EQ (window, selected_window))
2696 Fset_buffer (buffer);
5b03d3c0
RS
2697 /* We can't check ! NILP (Vwindow_scroll_functions) here
2698 because that might itself be a local variable. */
2699 else if (window_initialized)
2700 {
2701 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2702 Fset_buffer (buffer);
2703 }
2704
5500c422 2705 /* Set left and right marginal area width from buffer. */
cfa22082 2706 Fset_window_margins (window, b->left_margin_width, b->right_margin_width);
7ab12479 2707
5500c422
GM
2708 if (run_hooks_p)
2709 {
2710 if (! NILP (Vwindow_scroll_functions))
2711 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2712 Fmarker_position (w->start));
2713
2714 if (! NILP (Vwindow_configuration_change_hook)
2715 && ! NILP (Vrun_hooks))
2716 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2717 }
543f5fb1 2718
5b03d3c0 2719 unbind_to (count, Qnil);
5500c422 2720}
5b03d3c0 2721
5500c422
GM
2722
2723DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
fdb82f93
PJ
2724 doc: /* Make WINDOW display BUFFER as its contents.
2725BUFFER can be a buffer or buffer name. */)
2726 (window, buffer)
5500c422
GM
2727 register Lisp_Object window, buffer;
2728{
2729 register Lisp_Object tem;
2730 register struct window *w = decode_window (window);
5500c422 2731
bed0c171 2732 XSETWINDOW (window, w);
5500c422 2733 buffer = Fget_buffer (buffer);
b7826503 2734 CHECK_BUFFER (buffer);
5500c422
GM
2735
2736 if (NILP (XBUFFER (buffer)->name))
2737 error ("Attempt to display deleted buffer");
2738
2739 tem = w->buffer;
2740 if (NILP (tem))
2741 error ("Window is deleted");
2742 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
2743 is first being set up. */
2744 {
2745 if (!NILP (w->dedicated) && !EQ (tem, buffer))
2746 error ("Window is dedicated to `%s'",
2747 XSTRING (XBUFFER (tem)->name)->data);
2748
2749 unshow_buffer (w);
2750 }
2751
2752 set_window_buffer (window, buffer, 1);
7ab12479
JB
2753 return Qnil;
2754}
2755
2756DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
fdb82f93
PJ
2757 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
2758If WINDOW is not already selected, also make WINDOW's buffer current.
2759Note that the main editor command loop
2760selects the buffer of the selected window before each command. */)
2761 (window)
7ab12479 2762 register Lisp_Object window;
b7354ddf
RS
2763{
2764 return select_window_1 (window, 1);
2765}
2766\f
719eaeb1
GM
2767/* Note that selected_window can be nil
2768 when this is called from Fset_window_configuration. */
2769
b7354ddf
RS
2770static Lisp_Object
2771select_window_1 (window, recordflag)
2772 register Lisp_Object window;
2773 int recordflag;
7ab12479
JB
2774{
2775 register struct window *w;
719eaeb1 2776 register struct window *ow;
1ae1a37d 2777 struct frame *sf;
7ab12479 2778
b7826503 2779 CHECK_LIVE_WINDOW (window);
7ab12479
JB
2780
2781 w = XWINDOW (window);
50e88778 2782 w->frozen_window_start_p = 0;
7ab12479 2783
d834a2e9 2784 XSETFASTINT (w->use_time, ++window_select_count);
7ab12479
JB
2785 if (EQ (window, selected_window))
2786 return window;
2787
719eaeb1
GM
2788 if (!NILP (selected_window))
2789 {
2790 ow = XWINDOW (selected_window);
2791 if (! NILP (ow->buffer))
2792 set_marker_both (ow->pointm, ow->buffer,
2793 BUF_PT (XBUFFER (ow->buffer)),
2794 BUF_PT_BYTE (XBUFFER (ow->buffer)));
2795 }
7ab12479
JB
2796
2797 selected_window = window;
1ae1a37d
GM
2798 sf = SELECTED_FRAME ();
2799 if (XFRAME (WINDOW_FRAME (w)) != sf)
7ab12479 2800 {
44fa5b1e 2801 XFRAME (WINDOW_FRAME (w))->selected_window = window;
147a6615
RS
2802 /* Use this rather than Fhandle_switch_frame
2803 so that FRAME_FOCUS_FRAME is moved appropriately as we
2804 move around in the state where a minibuffer in a separate
2805 frame is active. */
2806 Fselect_frame (WINDOW_FRAME (w), Qnil);
7ab12479
JB
2807 }
2808 else
1ae1a37d 2809 sf->selected_window = window;
7ab12479 2810
b7354ddf
RS
2811 if (recordflag)
2812 record_buffer (w->buffer);
7ab12479
JB
2813 Fset_buffer (w->buffer);
2814
86e48436
RS
2815 XBUFFER (w->buffer)->last_selected_window = window;
2816
7ab12479
JB
2817 /* Go to the point recorded in the window.
2818 This is important when the buffer is in more
2819 than one window. It also matters when
2820 redisplay_window has altered point after scrolling,
2821 because it makes the change only in the window. */
2822 {
2823 register int new_point = marker_position (w->pointm);
2824 if (new_point < BEGV)
2825 SET_PT (BEGV);
a9c95e08 2826 else if (new_point > ZV)
7ab12479
JB
2827 SET_PT (ZV);
2828 else
2829 SET_PT (new_point);
2830 }
2831
2832 windows_or_buffers_changed++;
2833 return window;
2834}
b7354ddf 2835\f
441a127e
RS
2836/* Deiconify the frame containing the window WINDOW,
2837 unless it is the selected frame;
2838 then return WINDOW.
2839
2840 The reason for the exception for the selected frame
2841 is that it seems better not to change the selected frames visibility
2842 merely because of displaying a different buffer in it.
2843 The deiconification is useful when a buffer gets shown in
2844 another frame that you were not using lately. */
d07f802a
RS
2845
2846static Lisp_Object
2847display_buffer_1 (window)
2848 Lisp_Object window;
2849{
1ae1a37d
GM
2850 Lisp_Object frame = XWINDOW (window)->frame;
2851 FRAME_PTR f = XFRAME (frame);
2852
d07f802a 2853 FRAME_SAMPLE_VISIBILITY (f);
1ae1a37d
GM
2854
2855 if (!EQ (frame, selected_frame))
60117126
RS
2856 {
2857 if (FRAME_ICONIFIED_P (f))
1ae1a37d 2858 Fmake_frame_visible (frame);
37962e60 2859 else if (FRAME_VISIBLE_P (f))
1ae1a37d 2860 Fraise_frame (frame);
60117126 2861 }
1ae1a37d 2862
d07f802a
RS
2863 return window;
2864}
2865
4628f7a4 2866DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
fdb82f93
PJ
2867 doc: /* Returns non-nil if a buffer named BUFFER-NAME would be created specially.
2868The value is actually t if the frame should be called with default frame
2869parameters, and a list of frame parameters if they were specified.
2870See `special-display-buffer-names', and `special-display-regexps'. */)
2871 (buffer_name)
4628f7a4
EN
2872 Lisp_Object buffer_name;
2873{
2874 Lisp_Object tem;
2875
b7826503 2876 CHECK_STRING (buffer_name);
4628f7a4
EN
2877
2878 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2879 if (!NILP (tem))
2880 return Qt;
2881
2882 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2883 if (!NILP (tem))
2884 return XCDR (tem);
2885
2886 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2887 {
2888 Lisp_Object car = XCAR (tem);
2889 if (STRINGP (car)
2890 && fast_string_match (car, buffer_name) >= 0)
2891 return Qt;
2892 else if (CONSP (car)
2893 && STRINGP (XCAR (car))
2894 && fast_string_match (XCAR (car), buffer_name) >= 0)
0057b00a 2895 return XCDR (car);
4628f7a4
EN
2896 }
2897 return Qnil;
2898}
2899
2900DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
fdb82f93
PJ
2901 doc: /* Returns non-nil if a new buffer named BUFFER-NAME would use the same window.
2902See `same-window-buffer-names' and `same-window-regexps'. */)
2903 (buffer_name)
4628f7a4
EN
2904 Lisp_Object buffer_name;
2905{
2906 Lisp_Object tem;
2907
b7826503 2908 CHECK_STRING (buffer_name);
4628f7a4
EN
2909
2910 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2911 if (!NILP (tem))
2912 return Qt;
2913
2914 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2915 if (!NILP (tem))
2916 return Qt;
2917
2918 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2919 {
2920 Lisp_Object car = XCAR (tem);
2921 if (STRINGP (car)
2922 && fast_string_match (car, buffer_name) >= 0)
2923 return Qt;
2924 else if (CONSP (car)
2925 && STRINGP (XCAR (car))
2926 && fast_string_match (XCAR (car), buffer_name) >= 0)
2927 return Qt;
2928 }
2929 return Qnil;
2930}
2931
fdb82f93 2932/* Use B so the default is (other-buffer). */
53f76081 2933DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
fdb82f93
PJ
2934 "BDisplay buffer: \nP",
2935 doc: /* Make BUFFER appear in some window but don't select it.
2936BUFFER can be a buffer or a buffer name.
2937If BUFFER is shown already in some window, just use that one,
2938unless the window is the selected window and the optional second
2939argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
2940If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
2941Returns the window displaying BUFFER.
079d288f 2942If `display-buffer-reuse-frames' is non-nil, and another frame is currently
fdb82f93
PJ
2943displaying BUFFER, then simply raise that frame.
2944
2945The variables `special-display-buffer-names', `special-display-regexps',
2946`same-window-buffer-names', and `same-window-regexps' customize how certain
2947buffer names are handled.
2948
2949If optional argument FRAME is `visible', search all visible frames.
2950If FRAME is 0, search all visible and iconified frames.
2951If FRAME is t, search all frames.
2952If FRAME is a frame, search only that frame.
2953If FRAME is nil, search only the selected frame
2954 (actually the last nonminibuffer frame),
2955 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
2956 which means search visible and iconified frames.
2957
2958If `even-window-heights' is non-nil, window heights will be evened out
2959if displaying the buffer causes two vertically adjacent windows to be
2960displayed. */)
2961 (buffer, not_this_window, frame)
53f76081 2962 register Lisp_Object buffer, not_this_window, frame;
7ab12479 2963{
aee631c2 2964 register Lisp_Object window, tem, swp;
1ae1a37d 2965 struct frame *f;
7ab12479 2966
aee631c2 2967 swp = Qnil;
7ab12479 2968 buffer = Fget_buffer (buffer);
b7826503 2969 CHECK_BUFFER (buffer);
7ab12479 2970
265a9e55 2971 if (!NILP (Vdisplay_buffer_function))
7ab12479
JB
2972 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2973
265a9e55 2974 if (NILP (not_this_window)
7ab12479 2975 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
d07f802a 2976 return display_buffer_1 (selected_window);
7ab12479 2977
855d8627
RS
2978 /* See if the user has specified this buffer should appear
2979 in the selected window. */
2980 if (NILP (not_this_window))
2981 {
aee631c2
RS
2982 swp = Fsame_window_p (XBUFFER (buffer)->name);
2983 if (!NILP (swp) && !no_switch_window (selected_window))
c63dc4a2
RS
2984 {
2985 Fswitch_to_buffer (buffer, Qnil);
d07f802a 2986 return display_buffer_1 (selected_window);
c63dc4a2 2987 }
855d8627
RS
2988 }
2989
079d288f 2990 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
73dc5198
KH
2991 look for a window showing BUFFER on any visible or iconified frame.
2992 Otherwise search only the current frame. */
53f76081
RS
2993 if (! NILP (frame))
2994 tem = frame;
9c3da604
GM
2995 else if (pop_up_frames
2996 || display_buffer_reuse_frames
2997 || last_nonminibuf_frame == 0)
73dc5198
KH
2998 XSETFASTINT (tem, 0);
2999 else
73dc5198 3000 XSETFRAME (tem, last_nonminibuf_frame);
9c3da604 3001
73dc5198 3002 window = Fget_buffer_window (buffer, tem);
265a9e55
JB
3003 if (!NILP (window)
3004 && (NILP (not_this_window) || !EQ (window, selected_window)))
9c3da604 3005 return display_buffer_1 (window);
7ab12479 3006
a90712c2 3007 /* Certain buffer names get special handling. */
aee631c2 3008 if (!NILP (Vspecial_display_function) && NILP (swp))
a90712c2 3009 {
4628f7a4
EN
3010 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3011 if (EQ (tem, Qt))
a90712c2 3012 return call1 (Vspecial_display_function, buffer);
4628f7a4
EN
3013 if (CONSP (tem))
3014 return call2 (Vspecial_display_function, buffer, tem);
a90712c2
RS
3015 }
3016
44fa5b1e
JB
3017 /* If there are no frames open that have more than a minibuffer,
3018 we need to create a new frame. */
3019 if (pop_up_frames || last_nonminibuf_frame == 0)
7ab12479 3020 {
a90712c2 3021 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
7ab12479 3022 Fset_window_buffer (window, buffer);
d07f802a 3023 return display_buffer_1 (window);
7ab12479 3024 }
7ab12479 3025
1ae1a37d 3026 f = SELECTED_FRAME ();
43bad991 3027 if (pop_up_windows
1ae1a37d 3028 || FRAME_MINIBUF_ONLY_P (f)
cee67da9
RS
3029 /* If the current frame is a special display frame,
3030 don't try to reuse its windows. */
1ae1a37d 3031 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
7ab12479 3032 {
12cae7c0
KH
3033 Lisp_Object frames;
3034
37962e60 3035 frames = Qnil;
1ae1a37d 3036 if (FRAME_MINIBUF_ONLY_P (f))
74112613 3037 XSETFRAME (frames, last_nonminibuf_frame);
7ab12479
JB
3038 /* Don't try to create a window if would get an error */
3039 if (split_height_threshold < window_min_height << 1)
3040 split_height_threshold = window_min_height << 1;
3041
cee67da9
RS
3042 /* Note that both Fget_largest_window and Fget_lru_window
3043 ignore minibuffers and dedicated windows.
3044 This means they can return nil. */
7ab12479 3045
cee67da9
RS
3046 /* If the frame we would try to split cannot be split,
3047 try other frames. */
1ae1a37d 3048 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
cee67da9
RS
3049 {
3050 /* Try visible frames first. */
3051 window = Fget_largest_window (Qvisible);
3052 /* If that didn't work, try iconified frames. */
3053 if (NILP (window))
3054 window = Fget_largest_window (make_number (0));
3055 if (NILP (window))
3056 window = Fget_largest_window (Qt);
3057 }
3058 else
3059 window = Fget_largest_window (frames);
3060
92cca945
RS
3061 /* If we got a tall enough full-width window that can be split,
3062 split it. */
265a9e55 3063 if (!NILP (window)
92cca945 3064 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
7ab12479 3065 && window_height (window) >= split_height_threshold
111e5992 3066 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
7ab12479
JB
3067 window = Fsplit_window (window, Qnil, Qnil);
3068 else
3069 {
1942f68f
RS
3070 Lisp_Object upper, lower, other;
3071
44fa5b1e 3072 window = Fget_lru_window (frames);
92cca945
RS
3073 /* If the LRU window is selected, and big enough,
3074 and can be split, split it. */
cee67da9 3075 if (!NILP (window)
92cca945 3076 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
cee67da9
RS
3077 && (EQ (window, selected_window)
3078 || EQ (XWINDOW (window)->parent, Qnil))
7ab12479
JB
3079 && window_height (window) >= window_min_height << 1)
3080 window = Fsplit_window (window, Qnil, Qnil);
cee67da9 3081 /* If Fget_lru_window returned nil, try other approaches. */
48d9379d 3082
cee67da9 3083 /* Try visible frames first. */
48d9379d
RS
3084 if (NILP (window))
3085 window = Fget_buffer_window (buffer, Qvisible);
cee67da9
RS
3086 if (NILP (window))
3087 window = Fget_largest_window (Qvisible);
3088 /* If that didn't work, try iconified frames. */
48d9379d
RS
3089 if (NILP (window))
3090 window = Fget_buffer_window (buffer, make_number (0));
cee67da9
RS
3091 if (NILP (window))
3092 window = Fget_largest_window (make_number (0));
3093 /* Try invisible frames. */
48d9379d
RS
3094 if (NILP (window))
3095 window = Fget_buffer_window (buffer, Qt);
cee67da9
RS
3096 if (NILP (window))
3097 window = Fget_largest_window (Qt);
3098 /* As a last resort, make a new frame. */
3099 if (NILP (window))
3100 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
1942f68f
RS
3101 /* If window appears above or below another,
3102 even out their heights. */
cac66e4f 3103 other = upper = lower = Qnil;
1942f68f
RS
3104 if (!NILP (XWINDOW (window)->prev))
3105 other = upper = XWINDOW (window)->prev, lower = window;
3106 if (!NILP (XWINDOW (window)->next))
3107 other = lower = XWINDOW (window)->next, upper = window;
3108 if (!NILP (other)
6529ed87 3109 && !NILP (Veven_window_heights)
1942f68f 3110 /* Check that OTHER and WINDOW are vertically arrayed. */
296b535c
KH
3111 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
3112 && (XFASTINT (XWINDOW (other)->height)
3113 > XFASTINT (XWINDOW (window)->height)))
1942f68f 3114 {
296b535c
KH
3115 int total = (XFASTINT (XWINDOW (other)->height)
3116 + XFASTINT (XWINDOW (window)->height));
86c8e823
GM
3117 enlarge_window (upper,
3118 total / 2 - XFASTINT (XWINDOW (upper)->height),
f95284d2 3119 0, 0);
1942f68f 3120 }
7ab12479
JB
3121 }
3122 }
3123 else
3124 window = Fget_lru_window (Qnil);
3125
3126 Fset_window_buffer (window, buffer);
d07f802a 3127 return display_buffer_1 (window);
7ab12479
JB
3128}
3129
3130void
3131temp_output_buffer_show (buf)
3132 register Lisp_Object buf;
3133{
3134 register struct buffer *old = current_buffer;
3135 register Lisp_Object window;
3136 register struct window *w;
3137
bccd3dd1
RS
3138 XBUFFER (buf)->directory = current_buffer->directory;
3139
7ab12479 3140 Fset_buffer (buf);
c6367666 3141 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
7ab12479
JB
3142 BEGV = BEG;
3143 ZV = Z;
3144 SET_PT (BEG);
b1599b4c 3145 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
7ab12479
JB
3146 set_buffer_internal (old);
3147
3148 if (!EQ (Vtemp_buffer_show_function, Qnil))
3149 call1 (Vtemp_buffer_show_function, buf);
3150 else
3151 {
53f76081 3152 window = Fdisplay_buffer (buf, Qnil, Qnil);
7ab12479 3153
1ae1a37d 3154 if (!EQ (XWINDOW (window)->frame, selected_frame))
44fa5b1e 3155 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
7ab12479
JB
3156 Vminibuf_scroll_window = window;
3157 w = XWINDOW (window);
d834a2e9 3158 XSETFASTINT (w->hscroll, 0);
ea68264b 3159 XSETFASTINT (w->min_hscroll, 0);
b73ea88e
RS
3160 set_marker_restricted_both (w->start, buf, 1, 1);
3161 set_marker_restricted_both (w->pointm, buf, 1, 1);
a58ec57d 3162
beb4e312
RS
3163 /* Run temp-buffer-show-hook, with the chosen window selected
3164 and it sbuffer current. */
f52cca03 3165 if (!NILP (Vrun_hooks))
2cccc823 3166 {
f52cca03
RS
3167 Lisp_Object tem;
3168 tem = Fboundp (Qtemp_buffer_show_hook);
2cccc823
RS
3169 if (!NILP (tem))
3170 {
f52cca03
RS
3171 tem = Fsymbol_value (Qtemp_buffer_show_hook);
3172 if (!NILP (tem))
3173 {
3174 int count = specpdl_ptr - specpdl;
b7354ddf
RS
3175 Lisp_Object prev_window;
3176 prev_window = selected_window;
2cccc823 3177
f52cca03 3178 /* Select the window that was chosen, for running the hook. */
65a04b96 3179 record_unwind_protect (Fselect_window, prev_window);
b7354ddf 3180 select_window_1 (window, 0);
beb4e312 3181 Fset_buffer (w->buffer);
f52cca03 3182 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
b7354ddf 3183 select_window_1 (prev_window, 0);
f52cca03
RS
3184 unbind_to (count, Qnil);
3185 }
2cccc823
RS
3186 }
3187 }
3188 }
7ab12479
JB
3189}
3190\f
dfcf069d 3191static void
7ab12479
JB
3192make_dummy_parent (window)
3193 Lisp_Object window;
3194{
cffec418 3195 Lisp_Object new;
7ab12479 3196 register struct window *o, *p;
cffec418 3197 int i;
7ab12479 3198
cffec418 3199 o = XWINDOW (window);
26605be9 3200 p = allocate_window ();
cffec418 3201 for (i = 0; i < VECSIZE (struct window); ++i)
26605be9
GM
3202 ((struct Lisp_Vector *) p)->contents[i]
3203 = ((struct Lisp_Vector *)o)->contents[i];
cffec418 3204 XSETWINDOW (new, p);
7ab12479 3205
d834a2e9 3206 XSETFASTINT (p->sequence_number, ++sequence_number);
7ab12479
JB
3207
3208 /* Put new into window structure in place of window */
3209 replace_window (window, new);
3210
3211 o->next = Qnil;
3212 o->prev = Qnil;
3213 o->vchild = Qnil;
3214 o->hchild = Qnil;
3215 o->parent = new;
3216
3217 p->start = Qnil;
3218 p->pointm = Qnil;
3219 p->buffer = Qnil;
3220}
3221
3222DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
fdb82f93
PJ
3223 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3224WINDOW defaults to selected one and SIZE to half its size.
3225If optional third arg HORFLAG is non-nil, split side by side
3226and put SIZE columns in the first of the pair. In that case,
3227SIZE includes that window's scroll bar, or the divider column to its right. */)
3228 (window, size, horflag)
77ae0fe3 3229 Lisp_Object window, size, horflag;
7ab12479
JB
3230{
3231 register Lisp_Object new;
3232 register struct window *o, *p;
c0807608 3233 FRAME_PTR fo;
77ae0fe3 3234 register int size_int;
7ab12479 3235
265a9e55 3236 if (NILP (window))
7ab12479
JB
3237 window = selected_window;
3238 else
b7826503 3239 CHECK_LIVE_WINDOW (window);
7ab12479
JB
3240
3241 o = XWINDOW (window);
c0807608 3242 fo = XFRAME (WINDOW_FRAME (o));
7ab12479 3243
77ae0fe3 3244 if (NILP (size))
7ab12479 3245 {
265a9e55 3246 if (!NILP (horflag))
c0807608 3247 /* Calculate the size of the left-hand window, by dividing
25b33244
KH
3248 the usable space in columns by two.
3249 We round up, since the left-hand window may include
3250 a dividing line, while the right-hand may not. */
3251 size_int = (XFASTINT (o->width) + 1) >> 1;
7ab12479 3252 else
77ae0fe3 3253 size_int = XFASTINT (o->height) >> 1;
7ab12479
JB
3254 }
3255 else
3256 {
b7826503 3257 CHECK_NUMBER (size);
77ae0fe3 3258 size_int = XINT (size);
7ab12479
JB
3259 }
3260
3261 if (MINI_WINDOW_P (o))
3262 error ("Attempt to split minibuffer window");
233a4a2c
GM
3263 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3264 error ("Attempt to split fixed-size window");
7ab12479 3265
a481b3ea 3266 check_min_window_sizes ();
7ab12479 3267
265a9e55 3268 if (NILP (horflag))
7ab12479 3269 {
77ae0fe3
KH
3270 if (size_int < window_min_height)
3271 error ("Window height %d too small (after splitting)", size_int);
3272 if (size_int + window_min_height > XFASTINT (o->height))
37962e60 3273 error ("Window height %d too small (after splitting)",
77ae0fe3 3274 XFASTINT (o->height) - size_int);
265a9e55
JB
3275 if (NILP (o->parent)
3276 || NILP (XWINDOW (o->parent)->vchild))
7ab12479
JB
3277 {
3278 make_dummy_parent (window);
3279 new = o->parent;
3280 XWINDOW (new)->vchild = window;
3281 }
3282 }
3283 else
3284 {
77ae0fe3
KH
3285 if (size_int < window_min_width)
3286 error ("Window width %d too small (after splitting)", size_int);
a59fed7e
RS
3287
3288 if (size_int + window_min_width > XFASTINT (o->width))
37962e60 3289 error ("Window width %d too small (after splitting)",
a59fed7e 3290 XFASTINT (o->width) - size_int);
265a9e55
JB
3291 if (NILP (o->parent)
3292 || NILP (XWINDOW (o->parent)->hchild))
7ab12479
JB
3293 {
3294 make_dummy_parent (window);
3295 new = o->parent;
3296 XWINDOW (new)->hchild = window;
3297 }
3298 }
3299
3300 /* Now we know that window's parent is a vertical combination
3301 if we are dividing vertically, or a horizontal combination
3302 if we are making side-by-side windows */
3303
3304 windows_or_buffers_changed++;
c0807608 3305 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
7ab12479
JB
3306 new = make_window ();
3307 p = XWINDOW (new);
3308
44fa5b1e 3309 p->frame = o->frame;
7ab12479 3310 p->next = o->next;
265a9e55 3311 if (!NILP (p->next))
7ab12479
JB
3312 XWINDOW (p->next)->prev = new;
3313 p->prev = window;
3314 o->next = new;
3315 p->parent = o->parent;
3316 p->buffer = Qt;
5500c422
GM
3317 p->window_end_valid = Qnil;
3318 bzero (&p->last_cursor, sizeof p->last_cursor);
7ab12479 3319
44fa5b1e 3320 /* Apportion the available frame space among the two new windows */
7ab12479 3321
265a9e55 3322 if (!NILP (horflag))
7ab12479
JB
3323 {
3324 p->height = o->height;
3325 p->top = o->top;
a59fed7e 3326 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
77ae0fe3
KH
3327 XSETFASTINT (o->width, size_int);
3328 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
7ab12479
JB
3329 }
3330 else
3331 {
3332 p->left = o->left;
3333 p->width = o->width;
77ae0fe3
KH
3334 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
3335 XSETFASTINT (o->height, size_int);
3336 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
7ab12479
JB
3337 }
3338
5500c422
GM
3339 /* Adjust glyph matrices. */
3340 adjust_glyphs (fo);
543f5fb1 3341 Fset_window_buffer (new, o->buffer);
7ab12479
JB
3342 return new;
3343}
3344\f
f95284d2 3345DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 3, "p",
fdb82f93
PJ
3346 doc: /* Make current window ARG lines bigger.
3347From program, optional second arg non-nil means grow sideways ARG columns.
f95284d2
RS
3348Interactively, if an argument is not given, make the window one line bigger.
3349
3350Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3351of the siblings above or to the left of the selected window. Only
3352siblings to the right or below are changed. */)
3353 (arg, side, preserve_before)
3354 register Lisp_Object arg, side, preserve_before;
7ab12479 3355{
b7826503 3356 CHECK_NUMBER (arg);
f95284d2
RS
3357 enlarge_window (selected_window, XINT (arg), !NILP (side),
3358 !NILP (preserve_before));
543f5fb1
RS
3359
3360 if (! NILP (Vwindow_configuration_change_hook))
3361 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3362
7ab12479
JB
3363 return Qnil;
3364}
3365
3366DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
fdb82f93
PJ
3367 doc: /* Make current window ARG lines smaller.
3368From program, optional second arg non-nil means shrink sideways arg columns.
3369Interactively, if an argument is not given, make the window one line smaller. */)
3370 (arg, side)
413430c5 3371 register Lisp_Object arg, side;
7ab12479 3372{
b7826503 3373 CHECK_NUMBER (arg);
f95284d2 3374 enlarge_window (selected_window, -XINT (arg), !NILP (side), 0);
543f5fb1
RS
3375
3376 if (! NILP (Vwindow_configuration_change_hook))
3377 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3378
7ab12479
JB
3379 return Qnil;
3380}
3381
3382int
3383window_height (window)
3384 Lisp_Object window;
3385{
3386 register struct window *p = XWINDOW (window);
3387 return XFASTINT (p->height);
3388}
3389
3390int
3391window_width (window)
3392 Lisp_Object window;
3393{
3394 register struct window *p = XWINDOW (window);
3395 return XFASTINT (p->width);
3396}
3397
c1636aa6 3398
7ab12479 3399#define CURBEG(w) \
3578db3c 3400 *(widthflag ? &(XWINDOW (w)->left) : &(XWINDOW (w)->top))
7ab12479
JB
3401
3402#define CURSIZE(w) \
3578db3c 3403 *(widthflag ? &(XWINDOW (w)->width) : &(XWINDOW (w)->height))
7ab12479 3404
233a4a2c 3405
f95284d2 3406/* Enlarge WINDOW by DELTA. WIDTHFLAG non-zero means
233a4a2c 3407 increase its width. Siblings of the selected window are resized to
f95284d2
RS
3408 fulfill the size request. If they become too small in the process,
3409 they will be deleted.
3410
3411 If PRESERVE_BEFORE is nonzero, that means don't alter
3412 the siblings to the left or above WINDOW. */
7ab12479 3413
f984d4fc 3414static void
f95284d2 3415enlarge_window (window, delta, widthflag, preserve_before)
86c8e823 3416 Lisp_Object window;
f95284d2 3417 int delta, widthflag, preserve_before;
7ab12479 3418{
86c8e823 3419 Lisp_Object parent, next, prev;
233a4a2c 3420 struct window *p;
3578db3c
KR
3421 Lisp_Object *sizep;
3422 int maximum;
5e14b1fc
AS
3423 int (*sizefun) P_ ((Lisp_Object))
3424 = widthflag ? window_width : window_height;
233a4a2c 3425 void (*setsizefun) P_ ((Lisp_Object, int, int))
5e14b1fc 3426 = (widthflag ? set_window_width : set_window_height);
7ab12479 3427
233a4a2c
GM
3428 /* Check values of window_min_width and window_min_height for
3429 validity. */
a481b3ea 3430 check_min_window_sizes ();
7ab12479 3431
233a4a2c 3432 /* Give up if this window cannot be resized. */
233a4a2c
GM
3433 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3434 error ("Window is not resizable");
3435
3436 /* Find the parent of the selected window. */
7ab12479
JB
3437 while (1)
3438 {
3439 p = XWINDOW (window);
3440 parent = p->parent;
233a4a2c 3441
265a9e55 3442 if (NILP (parent))
7ab12479
JB
3443 {
3444 if (widthflag)
3445 error ("No other window to side of this one");
3446 break;
3447 }
233a4a2c
GM
3448
3449 if (widthflag
3450 ? !NILP (XWINDOW (parent)->hchild)
265a9e55 3451 : !NILP (XWINDOW (parent)->vchild))
7ab12479 3452 break;
233a4a2c 3453
7ab12479
JB
3454 window = parent;
3455 }
3456
05c2896a 3457 sizep = &CURSIZE (window);
7ab12479 3458
7ab12479
JB
3459 {
3460 register int maxdelta;
7ab12479 3461
f95284d2
RS
3462 /* Compute the maximum size increment this window can have. */
3463
3464 if (preserve_before)
3465 {
3466 if (!NILP (parent))
3467 {
3468 maxdelta = (*sizefun) (parent) - XINT (*sizep);
3469 /* Subtract size of siblings before, since we can't take that. */
1ab964d7 3470 maxdelta -= XINT (CURBEG (window)) - XINT (CURBEG (parent));
f95284d2
RS
3471 }
3472 else
3473 maxdelta = (!NILP (p->next) ? ((*sizefun) (p->next)
3474 - window_min_size (XWINDOW (p->next),
3475 widthflag, 0, 0))
3476 : (delta = 0));
3477 }
3478 else
3479 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
3480 /* This is a main window followed by a minibuffer. */
3481 : !NILP (p->next) ? ((*sizefun) (p->next)
3482 - window_min_size (XWINDOW (p->next),
3483 widthflag, 0, 0))
3484 /* This is a minibuffer following a main window. */
3485 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3486 - window_min_size (XWINDOW (p->prev),
3487 widthflag, 0, 0))
3488 /* This is a frame with only one window, a minibuffer-only
3489 or a minibufferless frame. */
3490 : (delta = 0));
7ab12479
JB
3491
3492 if (delta > maxdelta)
3493 /* This case traps trying to make the minibuffer
44fa5b1e
JB
3494 the full frame, or make the only window aside from the
3495 minibuffer the full frame. */
7ab12479 3496 delta = maxdelta;
6b54027b 3497 }
d5783c40 3498
3578db3c 3499 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
6b54027b 3500 {
543f5fb1 3501 delete_window (window);
d5783c40 3502 return;
6b54027b
RS
3503 }
3504
3505 if (delta == 0)
3506 return;
7ab12479 3507
f95284d2 3508 /* Find the total we can get from other siblings without deleting them. */
db98a733
RS
3509 maximum = 0;
3510 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
c1636aa6 3511 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
f984d4fc 3512 widthflag, 0, 0);
f95284d2
RS
3513 if (! preserve_before)
3514 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3515 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3516 widthflag, 0, 0);
db98a733 3517
f95284d2 3518 /* If we can get it all from them without deleting them, do so. */
c6b530ed 3519 if (delta <= maximum)
7ab12479 3520 {
db98a733
RS
3521 Lisp_Object first_unaffected;
3522 Lisp_Object first_affected;
233a4a2c 3523 int fixed_p;
db98a733
RS
3524
3525 next = p->next;
3526 prev = p->prev;
3527 first_affected = window;
3528 /* Look at one sibling at a time,
3529 moving away from this window in both directions alternately,
3530 and take as much as we can get without deleting that sibling. */
f95284d2
RS
3531 while (delta != 0
3532 && (!NILP (next) || (!preserve_before && !NILP (prev))))
db98a733 3533 {
db98a733
RS
3534 if (! NILP (next))
3535 {
c1636aa6 3536 int this_one = ((*sizefun) (next)
233a4a2c 3537 - window_min_size (XWINDOW (next),
f984d4fc 3538 widthflag, 0, &fixed_p));
233a4a2c
GM
3539 if (!fixed_p)
3540 {
3541 if (this_one > delta)
3542 this_one = delta;
3543
3544 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3578db3c 3545 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
db98a733 3546
233a4a2c
GM
3547 delta -= this_one;
3548 }
3549
db98a733
RS
3550 next = XWINDOW (next)->next;
3551 }
233a4a2c 3552
db98a733
RS
3553 if (delta == 0)
3554 break;
233a4a2c 3555
f95284d2 3556 if (!preserve_before && ! NILP (prev))
db98a733 3557 {
c1636aa6 3558 int this_one = ((*sizefun) (prev)
233a4a2c 3559 - window_min_size (XWINDOW (prev),
f984d4fc 3560 widthflag, 0, &fixed_p));
233a4a2c
GM
3561 if (!fixed_p)
3562 {
3563 if (this_one > delta)
3564 this_one = delta;
3565
3566 first_affected = prev;
3567
3568 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3578db3c 3569 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
233a4a2c
GM
3570
3571 delta -= this_one;
3572 }
3573
db98a733
RS
3574 prev = XWINDOW (prev)->prev;
3575 }
3576 }
3577
233a4a2c
GM
3578 xassert (delta == 0);
3579
db98a733
RS
3580 /* Now recalculate the edge positions of all the windows affected,
3581 based on the new sizes. */
3582 first_unaffected = next;
3583 prev = first_affected;
3584 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3585 prev = next, next = XWINDOW (next)->next)
3586 {
3578db3c 3587 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
db98a733
RS
3588 /* This does not change size of NEXT,
3589 but it propagates the new top edge to its children */
3590 (*setsizefun) (next, (*sizefun) (next), 0);
3591 }
7ab12479
JB
3592 }
3593 else
3594 {
3595 register int delta1;
3596 register int opht = (*sizefun) (parent);
3597
3578db3c 3598 if (opht <= XINT (*sizep) + delta)
daf516d3
RS
3599 {
3600 /* If trying to grow this window to or beyond size of the parent,
3601 just delete all the sibling windows. */
dc1ab1ee 3602 Lisp_Object start, tem, next;
daf516d3 3603
dc1ab1ee
RS
3604 start = XWINDOW (parent)->vchild;
3605 if (NILP (start))
3606 start = XWINDOW (parent)->hchild;
daf516d3 3607
dc1ab1ee
RS
3608 /* Delete any siblings that come after WINDOW. */
3609 tem = XWINDOW (window)->next;
daf516d3
RS
3610 while (! NILP (tem))
3611 {
3612 next = XWINDOW (tem)->next;
dc1ab1ee
RS
3613 delete_window (tem);
3614 tem = next;
3615 }
3616
3617 /* Delete any siblings that come after WINDOW.
3618 Note that if START is not WINDOW, then WINDOW still
3619 Fhas siblings, so WINDOW has not yet replaced its parent. */
3620 tem = start;
3621 while (! EQ (tem, window))
3622 {
3623 next = XWINDOW (tem)->next;
3624 delete_window (tem);
daf516d3
RS
3625 tem = next;
3626 }
3627 }
7ab12479 3628 else
233a4a2c
GM
3629 {
3630 /* Otherwise, make delta1 just right so that if we add
3631 delta1 lines to this window and to the parent, and then
3632 shrink the parent back to its original size, the new
3633 proportional size of this window will increase by delta.
3634
3635 The function size_window will compute the new height h'
3636 of the window from delta1 as:
3637
3638 e = delta1/n
3639 x = delta1 - delta1/n * n for the 1st resizable child
3640 h' = h + e + x
3641
3642 where n is the number of children that can be resized.
3643 We can ignore x by choosing a delta1 that is a multiple of
3644 n. We want the height of this window to come out as
3645
3646 h' = h + delta
3647
3648 So, delta1 must be
3649
3650 h + e = h + delta
3651 delta1/n = delta
3652 delta1 = n * delta.
3653
3654 The number of children n rquals the number of resizable
3655 children of this window + 1 because we know window itself
3656 is resizable (otherwise we would have signalled an error. */
3657
3658 struct window *w = XWINDOW (window);
3659 Lisp_Object s;
3660 int n = 1;
3661
3662 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
3663 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3664 ++n;
3665 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
3666 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3667 ++n;
3668
3669 delta1 = n * delta;
7ab12479 3670
daf516d3
RS
3671 /* Add delta1 lines or columns to this window, and to the parent,
3672 keeping things consistent while not affecting siblings. */
3673 XSETINT (CURSIZE (parent), opht + delta1);
3674 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
3675
3676 /* Squeeze out delta1 lines or columns from our parent,
3677 shriking this window and siblings proportionately.
3678 This brings parent back to correct size.
3679 Delta1 was calculated so this makes this window the desired size,
3680 taking it all out of the siblings. */
3681 (*setsizefun) (parent, opht, 0);
3682
3683 }
7ab12479
JB
3684 }
3685
d834a2e9 3686 XSETFASTINT (p->last_modified, 0);
3cd21523 3687 XSETFASTINT (p->last_overlay_modified, 0);
5500c422
GM
3688
3689 /* Adjust glyph matrices. */
3690 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7ab12479 3691}
c1636aa6 3692
7ab12479
JB
3693#undef CURBEG
3694#undef CURSIZE
3695
5500c422 3696
f984d4fc
GM
3697\f
3698/***********************************************************************
3699 Resizing Mini-Windows
3700 ***********************************************************************/
3701
3702static void shrink_window_lowest_first P_ ((struct window *, int));
f984d4fc 3703
43b4a21f
GM
3704enum save_restore_action
3705{
3706 CHECK_ORIG_SIZES,
3707 SAVE_ORIG_SIZES,
3708 RESTORE_ORIG_SIZES
3709};
3710
3711static int save_restore_orig_size P_ ((struct window *,
3712 enum save_restore_action));
f984d4fc
GM
3713
3714/* Shrink windows rooted in window W to HEIGHT. Take the space needed
3715 from lowest windows first. */
3716
3717static void
3718shrink_window_lowest_first (w, height)
3719 struct window *w;
3720 int height;
3721{
3722 struct window *c;
3723 Lisp_Object child;
3724 int old_height;
3725
3726 xassert (!MINI_WINDOW_P (w));
3727
3728 /* Set redisplay hints. */
3729 XSETFASTINT (w->last_modified, 0);
3730 XSETFASTINT (w->last_overlay_modified, 0);
3731 windows_or_buffers_changed++;
3732 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
3733
3734 old_height = XFASTINT (w->height);
3735 XSETFASTINT (w->height, height);
3736
3737 if (!NILP (w->hchild))
3738 {
3739 for (child = w->hchild; !NILP (child); child = c->next)
3740 {
3741 c = XWINDOW (child);
3742 c->top = w->top;
3743 shrink_window_lowest_first (c, height);
3744 }
3745 }
3746 else if (!NILP (w->vchild))
3747 {
3748 Lisp_Object last_child;
3749 int delta = old_height - height;
3750 int last_top;
6bbd7a29
GM
3751
3752 last_child = Qnil;
f984d4fc
GM
3753
3754 /* Find the last child. We are taking space from lowest windows
3755 first, so we iterate over children from the last child
3756 backwards. */
3757 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
3758 last_child = child;
3759
3760 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
3761 for (child = last_child; delta && !NILP (child); child = c->prev)
3762 {
3763 int this_one;
3764
3765 c = XWINDOW (child);
3766 this_one = XFASTINT (c->height) - MIN_SAFE_WINDOW_HEIGHT;
3767
3768 if (this_one > delta)
3769 this_one = delta;
3770
3771 shrink_window_lowest_first (c, XFASTINT (c->height) - this_one);
3772 delta -= this_one;
3773 }
3774
3775 /* Compute new positions. */
3578db3c 3776 last_top = XINT (w->top);
f984d4fc
GM
3777 for (child = w->vchild; !NILP (child); child = c->next)
3778 {
3779 c = XWINDOW (child);
3780 c->top = make_number (last_top);
3781 shrink_window_lowest_first (c, XFASTINT (c->height));
3782 last_top += XFASTINT (c->height);
3783 }
3784 }
3785}
3786
3787
43b4a21f
GM
3788/* Save, restore, or check positions and sizes in the window tree
3789 rooted at W. ACTION says what to do.
f984d4fc 3790
43b4a21f
GM
3791 If ACTION is CHECK_ORIG_SIZES, check if orig_top and orig_height
3792 members are valid for all windows in the window tree. Value is
3793 non-zero if they are valid.
3794
3795 If ACTION is SAVE_ORIG_SIZES, save members top and height in
3796 orig_top and orig_height for all windows in the tree.
3797
3798 If ACTION is RESTORE_ORIG_SIZES, restore top and height from
3799 values stored in orig_top and orig_height for all windows. */
3800
3801static int
3802save_restore_orig_size (w, action)
f984d4fc 3803 struct window *w;
43b4a21f 3804 enum save_restore_action action;
f984d4fc 3805{
43b4a21f
GM
3806 int success_p = 1;
3807
f984d4fc
GM
3808 while (w)
3809 {
3810 if (!NILP (w->hchild))
43b4a21f
GM
3811 {
3812 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
3813 success_p = 0;
3814 }
f984d4fc 3815 else if (!NILP (w->vchild))
43b4a21f
GM
3816 {
3817 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
3818 success_p = 0;
3819 }
f984d4fc 3820
43b4a21f 3821 switch (action)
f984d4fc 3822 {
43b4a21f
GM
3823 case CHECK_ORIG_SIZES:
3824 if (!INTEGERP (w->orig_top) || !INTEGERP (w->orig_height))
3825 return 0;
3826 break;
3827
3828 case SAVE_ORIG_SIZES:
f984d4fc
GM
3829 w->orig_top = w->top;
3830 w->orig_height = w->height;
43b4a21f
GM
3831 XSETFASTINT (w->last_modified, 0);
3832 XSETFASTINT (w->last_overlay_modified, 0);
3833 break;
3834
3835 case RESTORE_ORIG_SIZES:
f984d4fc
GM
3836 xassert (INTEGERP (w->orig_top) && INTEGERP (w->orig_height));
3837 w->top = w->orig_top;
3838 w->height = w->orig_height;
3839 w->orig_height = w->orig_top = Qnil;
43b4a21f
GM
3840 XSETFASTINT (w->last_modified, 0);
3841 XSETFASTINT (w->last_overlay_modified, 0);
3842 break;
3843
3844 default:
3845 abort ();
f984d4fc 3846 }
43b4a21f 3847
f984d4fc
GM
3848 w = NILP (w->next) ? NULL : XWINDOW (w->next);
3849 }
43b4a21f
GM
3850
3851 return success_p;
f984d4fc
GM
3852}
3853
3854
3855/* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
3856 without deleting other windows. */
3857
3858void
3859grow_mini_window (w, delta)
3860 struct window *w;
3861 int delta;
3862{
3863 struct frame *f = XFRAME (w->frame);
3864 struct window *root;
3865
3866 xassert (MINI_WINDOW_P (w));
3867 xassert (delta >= 0);
3868
3869 /* Check values of window_min_width and window_min_height for
3870 validity. */
3871 check_min_window_sizes ();
3872
3873 /* Compute how much we can enlarge the mini-window without deleting
3874 other windows. */
3875 root = XWINDOW (FRAME_ROOT_WINDOW (f));
3876 if (delta)
3877 {
3878 int min_height = window_min_size (root, 0, 0, 0);
3879 if (XFASTINT (root->height) - delta < min_height)
8b8bd9c6 3880 /* Note that the root window may already be smaller than
eafa3196
GM
3881 min_height. */
3882 delta = max (0, XFASTINT (root->height) - min_height);
f984d4fc
GM
3883 }
3884
3885 if (delta)
3886 {
3887 /* Save original window sizes and positions, if not already done. */
43b4a21f
GM
3888 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
3889 save_restore_orig_size (root, SAVE_ORIG_SIZES);
f984d4fc
GM
3890
3891 /* Shrink other windows. */
3892 shrink_window_lowest_first (root, XFASTINT (root->height) - delta);
3893
3894 /* Grow the mini-window. */
3578db3c 3895 w->top = make_number (XFASTINT (root->top) + XFASTINT (root->height));
f984d4fc
GM
3896 w->height = make_number (XFASTINT (w->height) + delta);
3897 XSETFASTINT (w->last_modified, 0);
3898 XSETFASTINT (w->last_overlay_modified, 0);
3899
3900 adjust_glyphs (f);
3901 }
3902}
3903
3904
86c8e823
GM
3905/* Shrink mini-window W. If there is recorded info about window sizes
3906 before a call to grow_mini_window, restore recorded window sizes.
3907 Otherwise, if the mini-window is higher than 1 line, resize it to 1
3908 line. */
f984d4fc
GM
3909
3910void
3911shrink_mini_window (w)
3912 struct window *w;
3913{
3914 struct frame *f = XFRAME (w->frame);
3915 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3916
43b4a21f 3917 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
f984d4fc 3918 {
43b4a21f 3919 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
f984d4fc
GM
3920 adjust_glyphs (f);
3921 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3922 windows_or_buffers_changed = 1;
3923 }
86c8e823
GM
3924 else if (XFASTINT (w->height) > 1)
3925 {
0130fe1a
GM
3926 /* Distribute the additional lines of the mini-window
3927 among the other windows. */
86c8e823
GM
3928 Lisp_Object window;
3929 XSETWINDOW (window, w);
f95284d2 3930 enlarge_window (window, 1 - XFASTINT (w->height), 0, 0);
86c8e823 3931 }
f984d4fc
GM
3932}
3933
3934
3935\f
5500c422
GM
3936/* Mark window cursors off for all windows in the window tree rooted
3937 at W by setting their phys_cursor_on_p flag to zero. Called from
3938 xterm.c, e.g. when a frame is cleared and thereby all cursors on
3939 the frame are cleared. */
3940
3941void
3942mark_window_cursors_off (w)
3943 struct window *w;
3944{
3945 while (w)
3946 {
3947 if (!NILP (w->hchild))
3948 mark_window_cursors_off (XWINDOW (w->hchild));
3949 else if (!NILP (w->vchild))
3950 mark_window_cursors_off (XWINDOW (w->vchild));
3951 else
3952 w->phys_cursor_on_p = 0;
3953
3954 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3955 }
3956}
3957
3958
e9c195b1 3959/* Return number of lines of text (not counting mode lines) in W. */
7ab12479
JB
3960
3961int
3962window_internal_height (w)
3963 struct window *w;
3964{
3965 int ht = XFASTINT (w->height);
3966
e9c195b1
GM
3967 if (!MINI_WINDOW_P (w))
3968 {
3969 if (!NILP (w->parent)
3970 || !NILP (w->vchild)
3971 || !NILP (w->hchild)
3972 || !NILP (w->next)
3973 || !NILP (w->prev)
3974 || WINDOW_WANTS_MODELINE_P (w))
3975 --ht;
7ab12479 3976
e9c195b1
GM
3977 if (WINDOW_WANTS_HEADER_LINE_P (w))
3978 --ht;
3979 }
7ab12479
JB
3980
3981 return ht;
3982}
3983
535e0b8e
JB
3984
3985/* Return the number of columns in W.
a3c87d4e 3986 Don't count columns occupied by scroll bars or the vertical bar
535e0b8e 3987 separating W from the sibling to its right. */
5500c422 3988
535e0b8e
JB
3989int
3990window_internal_width (w)
3991 struct window *w;
3992{
5500c422 3993 struct frame *f = XFRAME (WINDOW_FRAME (w));
535e0b8e
JB
3994 int width = XINT (w->width);
3995
a3c87d4e 3996 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5500c422
GM
3997 /* Scroll bars occupy a few columns. */
3998 width -= FRAME_SCROLL_BAR_COLS (f);
3999 else if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4000 /* The column of `|' characters separating side-by-side windows
4001 occupies one column only. */
4002 width -= 1;
4003
4004 /* On window-systems, areas to the left and right of the window
81d189fd 4005 are used as fringes. */
5500c422 4006 if (FRAME_WINDOW_P (f))
81d189fd 4007 width -= FRAME_FRINGE_COLS (f);
111e5992
RS
4008
4009 return width;
535e0b8e
JB
4010}
4011
5500c422
GM
4012\f
4013/************************************************************************
4014 Window Scrolling
4015 ***********************************************************************/
535e0b8e 4016
5500c422 4017/* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
d4e7cf01 4018 N screen-fulls, which is defined as the height of the window minus
5500c422
GM
4019 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4020 instead. Negative values of N mean scroll down. NOERROR non-zero
4021 means don't signal an error if we try to move over BEGV or ZV,
4022 respectively. */
7ab12479 4023
101d1605
RS
4024static void
4025window_scroll (window, n, whole, noerror)
7ab12479
JB
4026 Lisp_Object window;
4027 int n;
101d1605 4028 int whole;
f8026fd8 4029 int noerror;
5500c422
GM
4030{
4031 /* If we must, use the pixel-based version which is much slower than
4032 the line-based one but can handle varying line heights. */
4033 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4034 window_scroll_pixel_based (window, n, whole, noerror);
4035 else
4036 window_scroll_line_based (window, n, whole, noerror);
4037}
4038
4039
4040/* Implementation of window_scroll that works based on pixel line
4041 heights. See the comment of window_scroll for parameter
4042 descriptions. */
4043
4044static void
4045window_scroll_pixel_based (window, n, whole, noerror)
4046 Lisp_Object window;
4047 int n;
4048 int whole;
4049 int noerror;
4050{
4051 struct it it;
4052 struct window *w = XWINDOW (window);
4053 struct text_pos start;
4054 Lisp_Object tem;
4055 int this_scroll_margin;
4056 int preserve_y;
5cdb3cf3
MB
4057 /* True if we fiddled the window vscroll field without really scrolling. */
4058 int vscrolled = 0;
5500c422
GM
4059
4060 SET_TEXT_POS_FROM_MARKER (start, w->start);
4061
4062 /* If PT is not visible in WINDOW, move back one half of
288d4e06
GM
4063 the screen. Allow PT to be partially visible, otherwise
4064 something like (scroll-down 1) with PT in the line before
4065 the partially visible one would recenter. */
4066 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
5500c422
GM
4067 if (NILP (tem))
4068 {
4069 /* Move backward half the height of the window. Performance note:
4070 vmotion used here is about 10% faster, but would give wrong
4071 results for variable height lines. */
4072 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4073 it.current_y = it.last_visible_y;
202379cf 4074 move_it_vertically (&it, - window_box_height (w) / 2);
5500c422
GM
4075
4076 /* The function move_iterator_vertically may move over more than
4077 the specified y-distance. If it->w is small, e.g. a
4078 mini-buffer window, we may end up in front of the window's
4079 display area. This is the case when Start displaying at the
4080 start of the line containing PT in this case. */
4081 if (it.current_y <= 0)
4082 {
4083 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4084 move_it_vertically (&it, 0);
4085 it.current_y = 0;
4086 }
4087
4088 start = it.current.pos;
4089 }
4090
4091 /* If scroll_preserve_screen_position is non-zero, we try to set
4092 point in the same window line as it is now, so get that line. */
4093 if (!NILP (Vscroll_preserve_screen_position))
4094 {
4095 start_display (&it, w, start);
4096 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4097 preserve_y = it.current_y;
4098 }
4099 else
4100 preserve_y = -1;
4101
4102 /* Move iterator it from start the specified distance forward or
4103 backward. The result is the new window start. */
4104 start_display (&it, w, start);
4105 if (whole)
4106 {
202379cf 4107 int screen_full = (window_box_height (w)
5500c422 4108 - next_screen_context_lines * CANON_Y_UNIT (it.f));
d4e7cf01 4109 int dy = n * screen_full;
d72340d4
GM
4110
4111 /* Note that move_it_vertically always moves the iterator to the
4112 start of a line. So, if the last line doesn't have a newline,
4113 we would end up at the start of the line ending at ZV. */
4114 if (dy <= 0)
4115 move_it_vertically_backward (&it, -dy);
4116 else if (dy > 0)
4117 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4118 MOVE_TO_POS | MOVE_TO_Y);
5500c422
GM
4119 }
4120 else
4121 move_it_by_lines (&it, n, 1);
4122
4123 /* End if we end up at ZV or BEGV. */
4124 if ((n > 0 && IT_CHARPOS (it) == ZV)
4125 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4126 {
5cdb3cf3
MB
4127 if (IT_CHARPOS (it) == ZV)
4128 {
4129 if (it.current_y + it.max_ascent + it.max_descent
4130 > it.last_visible_y)
a74eca50
GM
4131 {
4132 /* The last line was only partially visible, make it fully
4133 visible. */
4134 w->vscroll = (it.last_visible_y
4135 - it.current_y + it.max_ascent + it.max_descent);
4136 adjust_glyphs (it.f);
4137 }
5cdb3cf3
MB
4138 else if (noerror)
4139 return;
4140 else
4141 Fsignal (Qend_of_buffer, Qnil);
4142 }
5500c422 4143 else
5cdb3cf3
MB
4144 {
4145 if (w->vscroll != 0)
4146 /* The first line was only partially visible, make it fully
4147 visible. */
4148 w->vscroll = 0;
4149 else if (noerror)
4150 return;
4151 else
4152 Fsignal (Qbeginning_of_buffer, Qnil);
4153 }
4154
4155 /* If control gets here, then we vscrolled. */
4156
4157 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4158
4159 /* Don't try to change the window start below. */
4160 vscrolled = 1;
5500c422
GM
4161 }
4162
5cdb3cf3
MB
4163 if (! vscrolled)
4164 {
dad67609
RS
4165 int pos = IT_CHARPOS (it);
4166 int bytepos;
5cdb3cf3 4167 /* Set the window start, and set up the window for redisplay. */
dad67609 4168 set_marker_restricted (w->start, make_number (pos),
5cdb3cf3 4169 w->buffer);
dad67609
RS
4170 bytepos = XMARKER (w->start)->bytepos;
4171 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4172 ? Qt : Qnil);
5cdb3cf3
MB
4173 w->update_mode_line = Qt;
4174 XSETFASTINT (w->last_modified, 0);
4175 XSETFASTINT (w->last_overlay_modified, 0);
4176 /* Set force_start so that redisplay_window will run the
4177 window-scroll-functions. */
4178 w->force_start = Qt;
4179 }
5500c422
GM
4180
4181 it.current_y = it.vpos = 0;
4182
4183 /* Preserve the screen position if we must. */
4184 if (preserve_y >= 0)
4185 {
4186 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4187 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4188 }
4189 else
4190 {
4191 /* Move PT out of scroll margins. */
4192 this_scroll_margin = max (0, scroll_margin);
4193 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
4194 this_scroll_margin *= CANON_Y_UNIT (it.f);
4195
4196 if (n > 0)
4197 {
4198 /* We moved the window start towards ZV, so PT may be now
4199 in the scroll margin at the top. */
4200 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4201 while (it.current_y < this_scroll_margin)
e9b2c961
RS
4202 {
4203 int prev = it.current_y;
4204 move_it_by_lines (&it, 1, 1);
4205 if (prev == it.current_y)
4206 break;
4207 }
5500c422
GM
4208 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4209 }
4210 else if (n < 0)
4211 {
5cdb3cf3
MB
4212 int charpos, bytepos;
4213
5500c422
GM
4214 /* We moved the window start towards BEGV, so PT may be now
4215 in the scroll margin at the bottom. */
4216 move_it_to (&it, PT, -1,
4217 it.last_visible_y - this_scroll_margin - 1, -1,
4218 MOVE_TO_POS | MOVE_TO_Y);
5cdb3cf3
MB
4219
4220 /* Save our position, in case it's correct. */
4221 charpos = IT_CHARPOS (it);
4222 bytepos = IT_BYTEPOS (it);
5500c422 4223
5cdb3cf3
MB
4224 /* See if point is on a partially visible line at the end. */
4225 move_it_by_lines (&it, 1, 1);
4226 if (it.current_y > it.last_visible_y)
4227 /* The last line was only partially visible, so back up two
4228 lines to make sure we're on a fully visible line. */
4229 {
4230 move_it_by_lines (&it, -2, 0);
4231 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4232 }
4233 else
4234 /* No, the position we saved is OK, so use it. */
4235 SET_PT_BOTH (charpos, bytepos);
5500c422
GM
4236 }
4237 }
4238}
4239
4240
4241/* Implementation of window_scroll that works based on screen lines.
4242 See the comment of window_scroll for parameter descriptions. */
4243
4244static void
4245window_scroll_line_based (window, n, whole, noerror)
4246 Lisp_Object window;
4247 int n;
4248 int whole;
4249 int noerror;
7ab12479
JB
4250{
4251 register struct window *w = XWINDOW (window);
5500c422 4252 register int opoint = PT, opoint_byte = PT_BYTE;
b73ea88e 4253 register int pos, pos_byte;
7ab12479
JB
4254 register int ht = window_internal_height (w);
4255 register Lisp_Object tem;
4256 int lose;
5500c422 4257 Lisp_Object bolp;
345d45b2 4258 int startpos;
101d1605
RS
4259 struct position posit;
4260 int original_vpos;
4261
d4e7cf01
GM
4262 /* If scrolling screen-fulls, compute the number of lines to
4263 scroll from the window's height. */
4264 if (whole)
4265 n *= max (1, ht - next_screen_context_lines);
4266
101d1605
RS
4267 startpos = marker_position (w->start);
4268
4269 posit = *compute_motion (startpos, 0, 0, 0,
4270 PT, ht, 0,
4271 window_internal_width (w), XINT (w->hscroll),
4272 0, w);
4273 original_vpos = posit.vpos;
0a1f771a 4274
d834a2e9 4275 XSETFASTINT (tem, PT);
6ffdb539 4276 tem = Fpos_visible_in_window_p (tem, window, Qnil);
7ab12479 4277
265a9e55 4278 if (NILP (tem))
7ab12479 4279 {
cd2be1dd 4280 Fvertical_motion (make_number (- (ht / 2)), window);
345d45b2 4281 startpos = PT;
7ab12479
JB
4282 }
4283
345d45b2 4284 SET_PT (startpos);
5ce7b543 4285 lose = n < 0 && PT == BEGV;
540b6aa0 4286 Fvertical_motion (make_number (n), window);
5ce7b543 4287 pos = PT;
b73ea88e 4288 pos_byte = PT_BYTE;
7ab12479 4289 bolp = Fbolp ();
b73ea88e 4290 SET_PT_BOTH (opoint, opoint_byte);
7ab12479
JB
4291
4292 if (lose)
f8026fd8
JB
4293 {
4294 if (noerror)
4295 return;
4296 else
4297 Fsignal (Qbeginning_of_buffer, Qnil);
4298 }
7ab12479
JB
4299
4300 if (pos < ZV)
7ab12479 4301 {
0c7da84e
RS
4302 int this_scroll_margin = scroll_margin;
4303
4304 /* Don't use a scroll margin that is negative or too large. */
4305 if (this_scroll_margin < 0)
4306 this_scroll_margin = 0;
4307
4308 if (XINT (w->height) < 4 * scroll_margin)
4309 this_scroll_margin = XINT (w->height) / 4;
4310
b73ea88e 4311 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
7ab12479
JB
4312 w->start_at_line_beg = bolp;
4313 w->update_mode_line = Qt;
d834a2e9 4314 XSETFASTINT (w->last_modified, 0);
3cd21523 4315 XSETFASTINT (w->last_overlay_modified, 0);
345d45b2
RS
4316 /* Set force_start so that redisplay_window will run
4317 the window-scroll-functions. */
4318 w->force_start = Qt;
0c7da84e 4319
5500c422 4320 if (whole && !NILP (Vscroll_preserve_screen_position))
0c7da84e 4321 {
b73ea88e 4322 SET_PT_BOTH (pos, pos_byte);
101d1605 4323 Fvertical_motion (make_number (original_vpos), window);
0c7da84e 4324 }
101d1605
RS
4325 /* If we scrolled forward, put point enough lines down
4326 that it is outside the scroll margin. */
4327 else if (n > 0)
0c7da84e 4328 {
101d1605
RS
4329 int top_margin;
4330
4331 if (this_scroll_margin > 0)
4332 {
b73ea88e 4333 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
4334 Fvertical_motion (make_number (this_scroll_margin), window);
4335 top_margin = PT;
4336 }
4337 else
4338 top_margin = pos;
4339
4340 if (top_margin <= opoint)
b73ea88e 4341 SET_PT_BOTH (opoint, opoint_byte);
5500c422 4342 else if (!NILP (Vscroll_preserve_screen_position))
101d1605 4343 {
b73ea88e 4344 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
4345 Fvertical_motion (make_number (original_vpos), window);
4346 }
9317a85d 4347 else
335406fc 4348 SET_PT (top_margin);
0c7da84e 4349 }
101d1605 4350 else if (n < 0)
7ab12479 4351 {
101d1605
RS
4352 int bottom_margin;
4353
0c7da84e
RS
4354 /* If we scrolled backward, put point near the end of the window
4355 but not within the scroll margin. */
b73ea88e 4356 SET_PT_BOTH (pos, pos_byte);
0c7da84e 4357 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
101d1605
RS
4358 if (XFASTINT (tem) == ht - this_scroll_margin)
4359 bottom_margin = PT;
4360 else
4361 bottom_margin = PT + 1;
4362
4363 if (bottom_margin > opoint)
b73ea88e 4364 SET_PT_BOTH (opoint, opoint_byte);
7ab12479 4365 else
101d1605 4366 {
5500c422 4367 if (!NILP (Vscroll_preserve_screen_position))
9317a85d 4368 {
b73ea88e 4369 SET_PT_BOTH (pos, pos_byte);
9317a85d
RS
4370 Fvertical_motion (make_number (original_vpos), window);
4371 }
4372 else
4373 Fvertical_motion (make_number (-1), window);
101d1605 4374 }
7ab12479
JB
4375 }
4376 }
4377 else
f8026fd8
JB
4378 {
4379 if (noerror)
4380 return;
4381 else
4382 Fsignal (Qend_of_buffer, Qnil);
4383 }
7ab12479 4384}
5500c422
GM
4385
4386
4387/* Scroll selected_window up or down. If N is nil, scroll a
4388 screen-full which is defined as the height of the window minus
4389 next_screen_context_lines. If N is the symbol `-', scroll.
4390 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4391 up. This is the guts of Fscroll_up and Fscroll_down. */
7ab12479
JB
4392
4393static void
4394scroll_command (n, direction)
5500c422 4395 Lisp_Object n;
7ab12479
JB
4396 int direction;
4397{
d4e7cf01 4398 int count = BINDING_STACK_SIZE ();
7ab12479 4399
5500c422
GM
4400 xassert (abs (direction) == 1);
4401
4402 /* If selected window's buffer isn't current, make it current for
4403 the moment. But don't screw up if window_scroll gets an error. */
7ab12479 4404 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
95605e15
JB
4405 {
4406 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4407 Fset_buffer (XWINDOW (selected_window)->buffer);
5500c422
GM
4408
4409 /* Make redisplay consider other windows than just selected_window. */
4410 ++windows_or_buffers_changed;
95605e15 4411 }
7ab12479 4412
265a9e55 4413 if (NILP (n))
d4e7cf01 4414 window_scroll (selected_window, direction, 1, 0);
7ab12479 4415 else if (EQ (n, Qminus))
d4e7cf01 4416 window_scroll (selected_window, -direction, 1, 0);
7ab12479
JB
4417 else
4418 {
4419 n = Fprefix_numeric_value (n);
101d1605 4420 window_scroll (selected_window, XINT (n) * direction, 0, 0);
7ab12479 4421 }
95605e15
JB
4422
4423 unbind_to (count, Qnil);
7ab12479
JB
4424}
4425
4426DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
fdb82f93
PJ
4427 doc: /* Scroll text of current window upward ARG lines; or near full screen if no ARG.
4428A near full screen is `next-screen-context-lines' less than a full screen.
4429Negative ARG means scroll downward.
4430If ARG is the atom `-', scroll downward by nearly full screen.
4431When calling from a program, supply as argument a number, nil, or `-'. */)
4432 (arg)
413430c5 4433 Lisp_Object arg;
7ab12479 4434{
413430c5 4435 scroll_command (arg, 1);
7ab12479
JB
4436 return Qnil;
4437}
4438
4439DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
fdb82f93
PJ
4440 doc: /* Scroll text of current window down ARG lines; or near full screen if no ARG.
4441A near full screen is `next-screen-context-lines' less than a full screen.
4442Negative ARG means scroll upward.
4443If ARG is the atom `-', scroll upward by nearly full screen.
4444When calling from a program, supply as argument a number, nil, or `-'. */)
4445 (arg)
413430c5 4446 Lisp_Object arg;
7ab12479 4447{
413430c5 4448 scroll_command (arg, -1);
7ab12479
JB
4449 return Qnil;
4450}
ccd0664b
RS
4451\f
4452DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
fdb82f93
PJ
4453 doc: /* Return the other window for \"other window scroll\" commands.
4454If in the minibuffer, `minibuffer-scroll-window' if non-nil
4455specifies the window.
4456If `other-window-scroll-buffer' is non-nil, a window
4457showing that buffer is used. */)
4458 ()
7ab12479 4459{
ccd0664b 4460 Lisp_Object window;
7ab12479
JB
4461
4462 if (MINI_WINDOW_P (XWINDOW (selected_window))
265a9e55 4463 && !NILP (Vminibuf_scroll_window))
7ab12479
JB
4464 window = Vminibuf_scroll_window;
4465 /* If buffer is specified, scroll that buffer. */
265a9e55 4466 else if (!NILP (Vother_window_scroll_buffer))
7ab12479
JB
4467 {
4468 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
265a9e55 4469 if (NILP (window))
53f76081 4470 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
7ab12479
JB
4471 }
4472 else
dbc4e1c1
JB
4473 {
4474 /* Nothing specified; look for a neighboring window on the same
4475 frame. */
4476 window = Fnext_window (selected_window, Qnil, Qnil);
4477
4478 if (EQ (window, selected_window))
4479 /* That didn't get us anywhere; look for a window on another
4480 visible frame. */
4481 do
4482 window = Fnext_window (window, Qnil, Qt);
4483 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4484 && ! EQ (window, selected_window));
4485 }
4486
b7826503 4487 CHECK_LIVE_WINDOW (window);
7ab12479
JB
4488
4489 if (EQ (window, selected_window))
4490 error ("There is no other window");
4491
ccd0664b
RS
4492 return window;
4493}
4494
4495DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
fdb82f93
PJ
4496 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4497A near full screen is `next-screen-context-lines' less than a full screen.
4498The next window is the one below the current one; or the one at the top
4499if the current one is at the bottom. Negative ARG means scroll downward.
4500If ARG is the atom `-', scroll downward by nearly full screen.
4501When calling from a program, supply as argument a number, nil, or `-'.
4502
4503If in the minibuffer, `minibuffer-scroll-window' if non-nil
4504specifies the window to scroll.
4505If `other-window-scroll-buffer' is non-nil, scroll the window
4506showing that buffer, popping the buffer up if necessary. */)
4507 (arg)
d4e7cf01 4508 Lisp_Object arg;
ccd0664b 4509{
d4e7cf01
GM
4510 Lisp_Object window;
4511 struct window *w;
4512 int count = BINDING_STACK_SIZE ();
ccd0664b
RS
4513
4514 window = Fother_window_for_scrolling ();
7ab12479 4515 w = XWINDOW (window);
7ab12479
JB
4516
4517 /* Don't screw up if window_scroll gets an error. */
4518 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5500c422 4519 ++windows_or_buffers_changed;
7ab12479
JB
4520
4521 Fset_buffer (w->buffer);
4522 SET_PT (marker_position (w->pointm));
4523
413430c5 4524 if (NILP (arg))
d4e7cf01 4525 window_scroll (window, 1, 1, 1);
413430c5 4526 else if (EQ (arg, Qminus))
d4e7cf01 4527 window_scroll (window, -1, 1, 1);
7ab12479
JB
4528 else
4529 {
413430c5
EN
4530 if (CONSP (arg))
4531 arg = Fcar (arg);
b7826503 4532 CHECK_NUMBER (arg);
101d1605 4533 window_scroll (window, XINT (arg), 0, 1);
7ab12479
JB
4534 }
4535
b73ea88e 4536 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
f4e7b2c2 4537 unbind_to (count, Qnil);
7ab12479
JB
4538
4539 return Qnil;
4540}
4541\f
644b477c 4542DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
fdb82f93 4543 doc: /* Scroll selected window display ARG columns left.
2fe41216
EZ
4544Default for ARG is window width minus 2.
4545Value is the total amount of leftward horizontal scrolling in
4546effect after the change.
4547If `automatic-hscrolling' is non-nil, the argument ARG modifies
4548a lower bound for automatic scrolling, i.e. automatic scrolling
4549will not scroll a window to a column less than the value returned
4550by this function. */)
fdb82f93 4551 (arg)
7ab12479
JB
4552 register Lisp_Object arg;
4553{
c67fa410
GM
4554 Lisp_Object result;
4555 int hscroll;
4556 struct window *w = XWINDOW (selected_window);
4557
265a9e55 4558 if (NILP (arg))
c67fa410 4559 XSETFASTINT (arg, window_internal_width (w) - 2);
7ab12479
JB
4560 else
4561 arg = Fprefix_numeric_value (arg);
4562
c67fa410
GM
4563 hscroll = XINT (w->hscroll) + XINT (arg);
4564 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4565
f5686fbd 4566 if (interactive_p (0))
c67fa410
GM
4567 w->min_hscroll = w->hscroll;
4568
4569 return result;
7ab12479
JB
4570}
4571
644b477c 4572DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
fdb82f93 4573 doc: /* Scroll selected window display ARG columns right.
2fe41216
EZ
4574Default for ARG is window width minus 2.
4575Value is the total amount of leftward horizontal scrolling in
4576effect after the change.
4577If `automatic-hscrolling' is non-nil, the argument ARG modifies
4578a lower bound for automatic scrolling, i.e. automatic scrolling
4579will not scroll a window to a column less than the value returned
4580by this function. */)
fdb82f93 4581 (arg)
7ab12479
JB
4582 register Lisp_Object arg;
4583{
c67fa410
GM
4584 Lisp_Object result;
4585 int hscroll;
4586 struct window *w = XWINDOW (selected_window);
4587
265a9e55 4588 if (NILP (arg))
c67fa410 4589 XSETFASTINT (arg, window_internal_width (w) - 2);
7ab12479
JB
4590 else
4591 arg = Fprefix_numeric_value (arg);
4592
c67fa410
GM
4593 hscroll = XINT (w->hscroll) - XINT (arg);
4594 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4595
f5686fbd 4596 if (interactive_p (0))
c67fa410
GM
4597 w->min_hscroll = w->hscroll;
4598
4599 return result;
7ab12479
JB
4600}
4601
fa832261
KS
4602DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4603 doc: /* Return the window which was selected when entering the minibuffer.
4604Returns nil, if current window is not a minibuffer window. */)
4605 ()
4606{
4607 if (minibuf_level > 0
4608 && MINI_WINDOW_P (XWINDOW (selected_window))
4609 && !NILP (minibuf_selected_window)
4610 && WINDOW_LIVE_P (minibuf_selected_window))
4611 return minibuf_selected_window;
4612
4613 return Qnil;
4614}
4615
12c8b416
GM
4616/* Value is the number of lines actually displayed in window W,
4617 as opposed to its height. */
4618
4619static int
4620displayed_window_lines (w)
4621 struct window *w;
4622{
4623 struct it it;
4624 struct text_pos start;
4625 int height = window_box_height (w);
4626 struct buffer *old_buffer;
4627 int bottom_y;
4628
4629 if (XBUFFER (w->buffer) != current_buffer)
4630 {
4631 old_buffer = current_buffer;
4632 set_buffer_internal (XBUFFER (w->buffer));
4633 }
4634 else
4635 old_buffer = NULL;
4636
521b203e
GM
4637 /* In case W->start is out of the accessible range, do something
4638 reasonable. This happens in Info mode when Info-scroll-down
4639 calls (recenter -1) while W->start is 1. */
4640 if (XMARKER (w->start)->charpos < BEGV)
4641 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4642 else if (XMARKER (w->start)->charpos > ZV)
4643 SET_TEXT_POS (start, ZV, ZV_BYTE);
4644 else
4645 SET_TEXT_POS_FROM_MARKER (start, w->start);
4646
12c8b416
GM
4647 start_display (&it, w, start);
4648 move_it_vertically (&it, height);
c8bc6f65 4649 bottom_y = line_bottom_y (&it);
12c8b416
GM
4650
4651 /* Add in empty lines at the bottom of the window. */
4652 if (bottom_y < height)
4653 {
c8bc6f65
GM
4654 int uy = CANON_Y_UNIT (it.f);
4655 it.vpos += (height - bottom_y + uy - 1) / uy;
12c8b416
GM
4656 }
4657
c8bc6f65
GM
4658 if (old_buffer)
4659 set_buffer_internal (old_buffer);
4660
12c8b416
GM
4661 return it.vpos;
4662}
4663
4664
7ab12479 4665DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
fdb82f93
PJ
4666 doc: /* Center point in window and redisplay frame.
4667With prefix argument ARG, recenter putting point on screen line ARG
4668relative to the current window. If ARG is negative, it counts up from the
4669bottom of the window. (ARG should be less than the height of the window.)
4670
4671If ARG is omitted or nil, erase the entire frame and then
4672redraw with point in the center of the current window.
4673Just C-u as prefix means put point in the center of the window
4674and redisplay normally--don't erase and redraw the frame. */)
4675 (arg)
413430c5 4676 register Lisp_Object arg;
7ab12479 4677{
6df47b59 4678 struct window *w = XWINDOW (selected_window);
478292ed
RS
4679 struct buffer *buf = XBUFFER (w->buffer);
4680 struct buffer *obuf = current_buffer;
6df47b59
GM
4681 int center_p = 0;
4682 int charpos, bytepos;
7ab12479 4683
0fa5d25b
RS
4684 /* If redisplay is suppressed due to an error, try again. */
4685 obuf->display_error_modiff = 0;
4686
413430c5 4687 if (NILP (arg))
7ab12479 4688 {
f02d6d5c
KH
4689 int i;
4690
4691 /* Invalidate pixel data calculated for all compositions. */
4692 for (i = 0; i < n_compositions; i++)
4693 composition_table[i]->font = NULL;
7ab12479 4694
527b6458 4695 Fredraw_frame (w->frame);
44fa5b1e 4696 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
6df47b59 4697 center_p = 1;
7ab12479 4698 }
413430c5 4699 else if (CONSP (arg)) /* Just C-u. */
6df47b59 4700 center_p = 1;
7ab12479
JB
4701 else
4702 {
413430c5 4703 arg = Fprefix_numeric_value (arg);
b7826503 4704 CHECK_NUMBER (arg);
7ab12479
JB
4705 }
4706
478292ed 4707 set_buffer_internal (buf);
7ab12479 4708
521b203e 4709 /* Handle centering on a graphical frame specially. Such frames can
6df47b59
GM
4710 have variable-height lines and centering point on the basis of
4711 line counts would lead to strange effects. */
521b203e 4712 if (FRAME_WINDOW_P (XFRAME (w->frame)))
6df47b59 4713 {
6df47b59
GM
4714 if (center_p)
4715 {
521b203e
GM
4716 struct it it;
4717 struct text_pos pt;
4718
4719 SET_TEXT_POS (pt, PT, PT_BYTE);
4720 start_display (&it, w, pt);
202379cf 4721 move_it_vertically (&it, - window_box_height (w) / 2);
521b203e
GM
4722 charpos = IT_CHARPOS (it);
4723 bytepos = IT_BYTEPOS (it);
6df47b59
GM
4724 }
4725 else if (XINT (arg) < 0)
4726 {
521b203e
GM
4727 struct it it;
4728 struct text_pos pt;
d466fa4d 4729 int y0, y1, h, nlines;
521b203e
GM
4730
4731 SET_TEXT_POS (pt, PT, PT_BYTE);
4732 start_display (&it, w, pt);
4733 y0 = it.current_y;
4734
d466fa4d 4735 /* The amount of pixels we have to move back is the window
521b203e
GM
4736 height minus what's displayed in the line containing PT,
4737 and the lines below. */
d466fa4d
GM
4738 nlines = - XINT (arg) - 1;
4739 move_it_by_lines (&it, nlines, 1);
4740
201c831a 4741 y1 = line_bottom_y (&it);
d466fa4d
GM
4742
4743 /* If we can't move down NLINES lines because we hit
4744 the end of the buffer, count in some empty lines. */
4745 if (it.vpos < nlines)
4746 y1 += (nlines - it.vpos) * CANON_Y_UNIT (it.f);
4747
201c831a
GM
4748 h = window_box_height (w) - (y1 - y0);
4749
521b203e 4750 start_display (&it, w, pt);
201c831a 4751 move_it_vertically (&it, - h);
521b203e
GM
4752 charpos = IT_CHARPOS (it);
4753 bytepos = IT_BYTEPOS (it);
6df47b59 4754 }
521b203e
GM
4755 else
4756 {
4757 struct position pos;
4758 pos = *vmotion (PT, - XINT (arg), w);
4759 charpos = pos.bufpos;
4760 bytepos = pos.bytepos;
4761 }
4762 }
4763 else
4764 {
4765 struct position pos;
4766 int ht = window_internal_height (w);
4767
4768 if (center_p)
4769 arg = make_number (ht / 2);
4770 else if (XINT (arg) < 0)
4771 arg = make_number (XINT (arg) + ht);
6df47b59
GM
4772
4773 pos = *vmotion (PT, - XINT (arg), w);
4774 charpos = pos.bufpos;
4775 bytepos = pos.bytepos;
4776 }
4777
4778 /* Set the new window start. */
4779 set_marker_both (w->start, w->buffer, charpos, bytepos);
2f3cad6c 4780 w->window_end_valid = Qnil;
7ab12479 4781 w->force_start = Qt;
6df47b59
GM
4782 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
4783 w->start_at_line_beg = Qt;
4784 else
4785 w->start_at_line_beg = Qnil;
4786
478292ed 4787 set_buffer_internal (obuf);
7ab12479
JB
4788 return Qnil;
4789}
b7617575
GM
4790
4791
81fe0836 4792DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
fdb82f93
PJ
4793 0, 1, 0,
4794 doc: /* Return the height in lines of the text display area of WINDOW.
4795This doesn't include the mode-line (or header-line if any) or any
4796partial-height lines in the text display area. */)
4797 (window)
81fe0836
MB
4798 Lisp_Object window;
4799{
4800 struct window *w = decode_window (window);
4801 int pixel_height = window_box_height (w);
4802 int line_height = pixel_height / CANON_Y_UNIT (XFRAME (w->frame));
4803 return make_number (line_height);
4804}
4805
4806
7ab12479
JB
4807\f
4808DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
fdb82f93
PJ
4809 1, 1, "P",
4810 doc: /* Position point relative to window.
4811With no argument, position point at center of window.
4812An argument specifies vertical position within the window;
4813zero means top of window, negative means relative to bottom of window. */)
4814 (arg)
b7617575 4815 Lisp_Object arg;
7ab12479 4816{
b7617575
GM
4817 struct window *w = XWINDOW (selected_window);
4818 int lines, start;
540b6aa0 4819 Lisp_Object window;
7ab12479 4820
b7617575 4821 window = selected_window;
7ab12479
JB
4822 start = marker_position (w->start);
4823 if (start < BEGV || start > ZV)
4824 {
b7617575 4825 int height = window_internal_height (w);
cd2be1dd 4826 Fvertical_motion (make_number (- (height / 2)), window);
b73ea88e 4827 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
7ab12479
JB
4828 w->start_at_line_beg = Fbolp ();
4829 w->force_start = Qt;
4830 }
4831 else
b73ea88e 4832 Fgoto_char (w->start);
7ab12479 4833
b7617575
GM
4834 lines = displayed_window_lines (w);
4835 if (NILP (arg))
4836 XSETFASTINT (arg, lines / 2);
4837 else
4838 {
4839 arg = Fprefix_numeric_value (arg);
4840 if (XINT (arg) < 0)
4841 XSETINT (arg, XINT (arg) + lines);
4842 }
4843
c8bc6f65 4844 /* Skip past a partially visible first line. */
163784df 4845 if (w->vscroll)
163784df
MB
4846 XSETINT (arg, XINT (arg) + 1);
4847
540b6aa0 4848 return Fvertical_motion (arg, window);
7ab12479 4849}
5500c422
GM
4850
4851
7ab12479 4852\f
5500c422
GM
4853/***********************************************************************
4854 Window Configuration
4855 ***********************************************************************/
4856
7ab12479
JB
4857struct save_window_data
4858 {
f5ccc0cc 4859 EMACS_INT size_from_Lisp_Vector_struct;
7ab12479 4860 struct Lisp_Vector *next_from_Lisp_Vector_struct;
8f6ea2e9 4861 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
9ea173e8 4862 Lisp_Object frame_tool_bar_lines;
bdc727bf 4863 Lisp_Object selected_frame;
7ab12479
JB
4864 Lisp_Object current_window;
4865 Lisp_Object current_buffer;
4866 Lisp_Object minibuf_scroll_window;
3f49fddc 4867 Lisp_Object minibuf_selected_window;
7ab12479 4868 Lisp_Object root_window;
bdc727bf 4869 Lisp_Object focus_frame;
756b6edc
RS
4870 /* Record the values of window-min-width and window-min-height
4871 so that window sizes remain consistent with them. */
4872 Lisp_Object min_width, min_height;
cbff28e8
RS
4873 /* A vector, each of whose elements is a struct saved_window
4874 for one window. */
7ab12479
JB
4875 Lisp_Object saved_windows;
4876 };
ff06df24 4877
cbff28e8 4878/* This is saved as a Lisp_Vector */
7ab12479 4879struct saved_window
ea68264b
GM
4880{
4881 /* these first two must agree with struct Lisp_Vector in lisp.h */
4882 EMACS_INT size_from_Lisp_Vector_struct;
4883 struct Lisp_Vector *next_from_Lisp_Vector_struct;
7ab12479 4884
ea68264b
GM
4885 Lisp_Object window;
4886 Lisp_Object buffer, start, pointm, mark;
4887 Lisp_Object left, top, width, height, hscroll, min_hscroll;
4888 Lisp_Object parent, prev;
4889 Lisp_Object start_at_line_beg;
4890 Lisp_Object display_table;
4891 Lisp_Object orig_top, orig_height;
4892};
4893
4894#define SAVED_WINDOW_VECTOR_SIZE 17 /* Arg to Fmake_vector */
7ab12479
JB
4895
4896#define SAVED_WINDOW_N(swv,n) \
4897 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
4898
4899DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
fdb82f93
PJ
4900 doc: /* Return t if OBJECT is a window-configuration object. */)
4901 (object)
413430c5 4902 Lisp_Object object;
7ab12479 4903{
413430c5 4904 if (WINDOW_CONFIGURATIONP (object))
7ab12479
JB
4905 return Qt;
4906 return Qnil;
4907}
4908
3f8ab7bd 4909DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
fdb82f93
PJ
4910 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
4911 (config)
3f8ab7bd
RS
4912 Lisp_Object config;
4913{
4914 register struct save_window_data *data;
4915 struct Lisp_Vector *saved_windows;
4916
4917 if (! WINDOW_CONFIGURATIONP (config))
4918 wrong_type_argument (Qwindow_configuration_p, config);
4919
4920 data = (struct save_window_data *) XVECTOR (config);
4921 saved_windows = XVECTOR (data->saved_windows);
4922 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4923}
4924
d5b2799e 4925DEFUN ("set-window-configuration", Fset_window_configuration,
fdb82f93
PJ
4926 Sset_window_configuration, 1, 1, 0,
4927 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
4928CONFIGURATION must be a value previously returned
4929by `current-window-configuration' (which see).
4930If CONFIGURATION was made from a frame that is now deleted,
4931only frame-independent values can be restored. In this case,
4932the return value is nil. Otherwise the value is t. */)
4933 (configuration)
2f83aebe 4934 Lisp_Object configuration;
7ab12479 4935{
7ab12479
JB
4936 register struct save_window_data *data;
4937 struct Lisp_Vector *saved_windows;
7ab12479 4938 Lisp_Object new_current_buffer;
fd482be5 4939 Lisp_Object frame;
44fa5b1e 4940 FRAME_PTR f;
d2b35234 4941 int old_point = -1;
7ab12479 4942
017b2bad 4943 while (!WINDOW_CONFIGURATIONP (configuration))
3f8ab7bd 4944 wrong_type_argument (Qwindow_configuration_p, configuration);
7ab12479 4945
2f83aebe 4946 data = (struct save_window_data *) XVECTOR (configuration);
7ab12479
JB
4947 saved_windows = XVECTOR (data->saved_windows);
4948
7ab12479 4949 new_current_buffer = data->current_buffer;
265a9e55 4950 if (NILP (XBUFFER (new_current_buffer)->name))
7ab12479 4951 new_current_buffer = Qnil;
d2b35234
RS
4952 else
4953 {
4954 if (XBUFFER (new_current_buffer) == current_buffer)
4955 old_point = PT;
4956 }
7ab12479 4957
fd482be5
JB
4958 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4959 f = XFRAME (frame);
aa35ba9e 4960
fd482be5
JB
4961 /* If f is a dead frame, don't bother rebuilding its window tree.
4962 However, there is other stuff we should still try to do below. */
4963 if (FRAME_LIVE_P (f))
7ab12479 4964 {
fd482be5
JB
4965 register struct window *w;
4966 register struct saved_window *p;
5500c422
GM
4967 struct window *root_window;
4968 struct window **leaf_windows;
4969 int n_leaf_windows;
c4280705 4970 int k, i, n;
fd482be5
JB
4971
4972 /* If the frame has been resized since this window configuration was
4973 made, we change the frame to the size specified in the
4974 configuration, restore the configuration, and then resize it
4975 back. We keep track of the prevailing height in these variables. */
4976 int previous_frame_height = FRAME_HEIGHT (f);
4977 int previous_frame_width = FRAME_WIDTH (f);
8f6ea2e9 4978 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
9ea173e8 4979 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
fd482be5 4980
d2b35234
RS
4981 /* The mouse highlighting code could get screwed up
4982 if it runs during this. */
4983 BLOCK_INPUT;
4984
fd482be5
JB
4985 if (XFASTINT (data->frame_height) != previous_frame_height
4986 || XFASTINT (data->frame_width) != previous_frame_width)
f8ad443a 4987 change_frame_size (f, XFASTINT (data->frame_height),
2b653806 4988 XFASTINT (data->frame_width), 0, 0, 0);
e3678b64 4989#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9
KH
4990 if (XFASTINT (data->frame_menu_bar_lines)
4991 != previous_frame_menu_bar_lines)
f8ad443a 4992 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
4314246f 4993#ifdef HAVE_WINDOW_SYSTEM
9ea173e8
GM
4994 if (XFASTINT (data->frame_tool_bar_lines)
4995 != previous_frame_tool_bar_lines)
4996 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
4314246f 4997#endif
217f2871 4998#endif
fd482be5 4999
719eaeb1
GM
5000 /* "Swap out" point from the selected window
5001 into its buffer. We do this now, before
5002 restoring the window contents, and prevent it from
5003 being done later on when we select a new window. */
596ae0cf
RS
5004 if (! NILP (XWINDOW (selected_window)->buffer))
5005 {
5006 w = XWINDOW (selected_window);
5007 set_marker_both (w->pointm,
5008 w->buffer,
5009 BUF_PT (XBUFFER (w->buffer)),
5010 BUF_PT_BYTE (XBUFFER (w->buffer)));
5011 }
5012
fd482be5 5013 windows_or_buffers_changed++;
29aeee73 5014 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
fd482be5 5015
5500c422
GM
5016 /* Problem: Freeing all matrices and later allocating them again
5017 is a serious redisplay flickering problem. What we would
5018 really like to do is to free only those matrices not reused
5019 below. */
5020 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5021 leaf_windows
5022 = (struct window **) alloca (count_windows (root_window)
5023 * sizeof (struct window *));
5024 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5025
756b6edc
RS
5026 /* Temporarily avoid any problems with windows that are smaller
5027 than they are supposed to be. */
5028 window_min_height = 1;
5029 window_min_width = 1;
5030
fd482be5
JB
5031 /* Kludge Alert!
5032 Mark all windows now on frame as "deleted".
5033 Restoring the new configuration "undeletes" any that are in it.
37962e60 5034
fd482be5
JB
5035 Save their current buffers in their height fields, since we may
5036 need it later, if a buffer saved in the configuration is now
5037 dead. */
5038 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5039
5040 for (k = 0; k < saved_windows->size; k++)
5041 {
5042 p = SAVED_WINDOW_N (saved_windows, k);
5043 w = XWINDOW (p->window);
5044 w->next = Qnil;
7ab12479 5045
fd482be5
JB
5046 if (!NILP (p->parent))
5047 w->parent = SAVED_WINDOW_N (saved_windows,
5048 XFASTINT (p->parent))->window;
5049 else
5050 w->parent = Qnil;
7ab12479 5051
fd482be5 5052 if (!NILP (p->prev))
7ab12479 5053 {
fd482be5
JB
5054 w->prev = SAVED_WINDOW_N (saved_windows,
5055 XFASTINT (p->prev))->window;
5056 XWINDOW (w->prev)->next = p->window;
5057 }
5058 else
5059 {
5060 w->prev = Qnil;
5061 if (!NILP (w->parent))
5062 {
5063 if (EQ (p->width, XWINDOW (w->parent)->width))
5064 {
5065 XWINDOW (w->parent)->vchild = p->window;
5066 XWINDOW (w->parent)->hchild = Qnil;
5067 }
5068 else
5069 {
5070 XWINDOW (w->parent)->hchild = p->window;
5071 XWINDOW (w->parent)->vchild = Qnil;
5072 }
5073 }
5074 }
5075
5076 /* If we squirreled away the buffer in the window's height,
5077 restore it now. */
017b2bad 5078 if (BUFFERP (w->height))
fd482be5
JB
5079 w->buffer = w->height;
5080 w->left = p->left;
5081 w->top = p->top;
5082 w->width = p->width;
5083 w->height = p->height;
5084 w->hscroll = p->hscroll;
ea68264b 5085 w->min_hscroll = p->min_hscroll;
fd482be5 5086 w->display_table = p->display_table;
a1d58e5b
GM
5087 w->orig_top = p->orig_top;
5088 w->orig_height = p->orig_height;
d834a2e9 5089 XSETFASTINT (w->last_modified, 0);
3cd21523 5090 XSETFASTINT (w->last_overlay_modified, 0);
fd482be5
JB
5091
5092 /* Reinstall the saved buffer and pointers into it. */
5093 if (NILP (p->buffer))
5094 w->buffer = p->buffer;
5095 else
5096 {
5097 if (!NILP (XBUFFER (p->buffer)->name))
5098 /* If saved buffer is alive, install it. */
5099 {
5100 w->buffer = p->buffer;
5101 w->start_at_line_beg = p->start_at_line_beg;
b73ea88e
RS
5102 set_marker_restricted (w->start, p->start, w->buffer);
5103 set_marker_restricted (w->pointm, p->pointm, w->buffer);
fd482be5 5104 Fset_marker (XBUFFER (w->buffer)->mark,
b73ea88e 5105 p->mark, w->buffer);
fd482be5
JB
5106
5107 /* As documented in Fcurrent_window_configuration, don't
5108 save the location of point in the buffer which was current
5109 when the window configuration was recorded. */
6b54027b
RS
5110 if (!EQ (p->buffer, new_current_buffer)
5111 && XBUFFER (p->buffer) == current_buffer)
fd482be5
JB
5112 Fgoto_char (w->pointm);
5113 }
52a68e98
RS
5114 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
5115 /* Else unless window has a live buffer, get one. */
7ab12479 5116 {
fd482be5
JB
5117 w->buffer = Fcdr (Fcar (Vbuffer_alist));
5118 /* This will set the markers to beginning of visible
5119 range. */
5120 set_marker_restricted (w->start, make_number (0), w->buffer);
5121 set_marker_restricted (w->pointm, make_number (0),w->buffer);
5122 w->start_at_line_beg = Qt;
7ab12479
JB
5123 }
5124 else
fd482be5 5125 /* Keeping window's old buffer; make sure the markers
52a68e98 5126 are real. */
7ab12479 5127 {
fd482be5
JB
5128 /* Set window markers at start of visible range. */
5129 if (XMARKER (w->start)->buffer == 0)
5130 set_marker_restricted (w->start, make_number (0),
5131 w->buffer);
5132 if (XMARKER (w->pointm)->buffer == 0)
b73ea88e
RS
5133 set_marker_restricted_both (w->pointm, w->buffer,
5134 BUF_PT (XBUFFER (w->buffer)),
5135 BUF_PT_BYTE (XBUFFER (w->buffer)));
fd482be5 5136 w->start_at_line_beg = Qt;
7ab12479
JB
5137 }
5138 }
5139 }
9ace597f 5140
fd482be5 5141 FRAME_ROOT_WINDOW (f) = data->root_window;
719eaeb1
GM
5142 /* Prevent "swapping out point" in the old selected window
5143 using the buffer that has been restored into it.
5144 That swapping out has already been done,
5145 near the beginning of this function. */
5146 selected_window = Qnil;
fd482be5 5147 Fselect_window (data->current_window);
396a830c
RS
5148 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
5149 = selected_window;
7ab12479 5150
db269683 5151 if (NILP (data->focus_frame)
017b2bad 5152 || (FRAMEP (data->focus_frame)
db269683
JB
5153 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5154 Fredirect_frame_focus (frame, data->focus_frame);
7ab12479 5155
fd482be5
JB
5156#if 0 /* I don't understand why this is needed, and it causes problems
5157 when the frame's old selected window has been deleted. */
e4e59717 5158 if (f != selected_frame && FRAME_WINDOW_P (f))
9a7c6fc3 5159 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
d12f6f83 5160 0, 0);
fd482be5
JB
5161#endif
5162
5163 /* Set the screen height to the value it had before this function. */
5164 if (previous_frame_height != FRAME_HEIGHT (f)
5165 || previous_frame_width != FRAME_WIDTH (f))
5166 change_frame_size (f, previous_frame_height, previous_frame_width,
2b653806 5167 0, 0, 0);
e3678b64 5168#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9 5169 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
f8ad443a
AS
5170 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5171 make_number (0));
4314246f 5172#ifdef HAVE_WINDOW_SYSTEM
9ea173e8
GM
5173 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5174 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5175 make_number (0));
4314246f 5176#endif
217f2871 5177#endif
d2b35234 5178
5500c422 5179 /* Now, free glyph matrices in windows that were not reused. */
c4280705
GM
5180 for (i = n = 0; i < n_leaf_windows; ++i)
5181 {
5182 if (NILP (leaf_windows[i]->buffer))
5183 {
5184 /* Assert it's not reused as a combination. */
5185 xassert (NILP (leaf_windows[i]->hchild)
5186 && NILP (leaf_windows[i]->vchild));
5187 free_window_matrices (leaf_windows[i]);
c4280705
GM
5188 }
5189 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5190 ++n;
5191 }
5192
5193 /* If more than one window shows the new and old current buffer,
5194 don't try to preserve point in that buffer. */
5195 if (old_point > 0 && n > 1)
5196 old_point = -1;
5500c422
GM
5197
5198 adjust_glyphs (f);
5199
d2b35234 5200 UNBLOCK_INPUT;
756b6edc 5201
478292ed
RS
5202 /* Fselect_window will have made f the selected frame, so we
5203 reselect the proper frame here. Fhandle_switch_frame will change the
5204 selected window too, but that doesn't make the call to
5205 Fselect_window above totally superfluous; it still sets f's
5206 selected window. */
5207 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
d12f6f83 5208 do_switch_frame (data->selected_frame, 0, 0);
478292ed
RS
5209
5210 if (! NILP (Vwindow_configuration_change_hook)
5211 && ! NILP (Vrun_hooks))
5212 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
5213 }
bdc727bf
JB
5214
5215 if (!NILP (new_current_buffer))
d2b35234
RS
5216 {
5217 Fset_buffer (new_current_buffer);
5218
5219 /* If the buffer that is current now is the same
5220 that was current before setting the window configuration,
5221 don't alter its PT. */
5222 if (old_point >= 0)
5223 SET_PT (old_point);
5224 }
bdc727bf 5225
478292ed
RS
5226 /* Restore the minimum heights recorded in the configuration. */
5227 window_min_height = XINT (data->min_height);
5228 window_min_width = XINT (data->min_width);
543f5fb1 5229
478292ed 5230 Vminibuf_scroll_window = data->minibuf_scroll_window;
3dbab091 5231 minibuf_selected_window = data->minibuf_selected_window;
543f5fb1 5232
3f8ab7bd 5233 return (FRAME_LIVE_P (f) ? Qt : Qnil);
7ab12479
JB
5234}
5235
44fa5b1e 5236/* Mark all windows now on frame as deleted
7ab12479
JB
5237 by setting their buffers to nil. */
5238
fd482be5 5239void
7ab12479
JB
5240delete_all_subwindows (w)
5241 register struct window *w;
5242{
265a9e55 5243 if (!NILP (w->next))
7ab12479 5244 delete_all_subwindows (XWINDOW (w->next));
265a9e55 5245 if (!NILP (w->vchild))
7ab12479 5246 delete_all_subwindows (XWINDOW (w->vchild));
265a9e55 5247 if (!NILP (w->hchild))
7ab12479 5248 delete_all_subwindows (XWINDOW (w->hchild));
605be8af
JB
5249
5250 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
5251
86e48436
RS
5252 if (!NILP (w->buffer))
5253 unshow_buffer (w);
5254
605be8af
JB
5255 /* We set all three of these fields to nil, to make sure that we can
5256 distinguish this dead window from any live window. Live leaf
5257 windows will have buffer set, and combination windows will have
5258 vchild or hchild set. */
5259 w->buffer = Qnil;
5260 w->vchild = Qnil;
5261 w->hchild = Qnil;
acf70840
GM
5262
5263 Vwindow_list = Qnil;
7ab12479
JB
5264}
5265\f
5266static int
5267count_windows (window)
5268 register struct window *window;
5269{
5270 register int count = 1;
265a9e55 5271 if (!NILP (window->next))
7ab12479 5272 count += count_windows (XWINDOW (window->next));
265a9e55 5273 if (!NILP (window->vchild))
7ab12479 5274 count += count_windows (XWINDOW (window->vchild));
265a9e55 5275 if (!NILP (window->hchild))
7ab12479
JB
5276 count += count_windows (XWINDOW (window->hchild));
5277 return count;
5278}
5279
5500c422
GM
5280
5281/* Fill vector FLAT with leaf windows under W, starting at index I.
5282 Value is last index + 1. */
5283
5284static int
5285get_leaf_windows (w, flat, i)
5286 struct window *w;
5287 struct window **flat;
5288 int i;
5289{
5290 while (w)
5291 {
5292 if (!NILP (w->hchild))
5293 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5294 else if (!NILP (w->vchild))
5295 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5296 else
5297 flat[i++] = w;
5298
5299 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5300 }
5301
5302 return i;
5303}
5304
5305
5306/* Return a pointer to the glyph W's physical cursor is on. Value is
5307 null if W's current matrix is invalid, so that no meaningfull glyph
5308 can be returned. */
5309
5310struct glyph *
5311get_phys_cursor_glyph (w)
5312 struct window *w;
5313{
5314 struct glyph_row *row;
5315 struct glyph *glyph;
5316
5317 if (w->phys_cursor.vpos >= 0
5318 && w->phys_cursor.vpos < w->current_matrix->nrows
5319 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5320 row->enabled_p)
5321 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5322 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5323 else
5324 glyph = NULL;
5325
5326 return glyph;
5327}
5328
5329
7ab12479
JB
5330static int
5331save_window_save (window, vector, i)
5332 Lisp_Object window;
5333 struct Lisp_Vector *vector;
5334 int i;
5335{
5336 register struct saved_window *p;
5337 register struct window *w;
5338 register Lisp_Object tem;
5339
265a9e55 5340 for (;!NILP (window); window = w->next)
7ab12479
JB
5341 {
5342 p = SAVED_WINDOW_N (vector, i);
5343 w = XWINDOW (window);
5344
d834a2e9 5345 XSETFASTINT (w->temslot, i++);
7ab12479
JB
5346 p->window = window;
5347 p->buffer = w->buffer;
5348 p->left = w->left;
5349 p->top = w->top;
5350 p->width = w->width;
5351 p->height = w->height;
5352 p->hscroll = w->hscroll;
ea68264b 5353 p->min_hscroll = w->min_hscroll;
7ab12479 5354 p->display_table = w->display_table;
a1d58e5b
GM
5355 p->orig_top = w->orig_top;
5356 p->orig_height = w->orig_height;
265a9e55 5357 if (!NILP (w->buffer))
7ab12479
JB
5358 {
5359 /* Save w's value of point in the window configuration.
5360 If w is the selected window, then get the value of point
5361 from the buffer; pointm is garbage in the selected window. */
5362 if (EQ (window, selected_window))
5363 {
5364 p->pointm = Fmake_marker ();
b73ea88e
RS
5365 set_marker_both (p->pointm, w->buffer,
5366 BUF_PT (XBUFFER (w->buffer)),
5367 BUF_PT_BYTE (XBUFFER (w->buffer)));
7ab12479
JB
5368 }
5369 else
eeb82665 5370 p->pointm = Fcopy_marker (w->pointm, Qnil);
7ab12479 5371
eeb82665 5372 p->start = Fcopy_marker (w->start, Qnil);
7ab12479
JB
5373 p->start_at_line_beg = w->start_at_line_beg;
5374
5375 tem = XBUFFER (w->buffer)->mark;
eeb82665 5376 p->mark = Fcopy_marker (tem, Qnil);
7ab12479
JB
5377 }
5378 else
5379 {
5380 p->pointm = Qnil;
5381 p->start = Qnil;
5382 p->mark = Qnil;
5383 p->start_at_line_beg = Qnil;
5384 }
5385
265a9e55 5386 if (NILP (w->parent))
7ab12479
JB
5387 p->parent = Qnil;
5388 else
5389 p->parent = XWINDOW (w->parent)->temslot;
5390
265a9e55 5391 if (NILP (w->prev))
7ab12479
JB
5392 p->prev = Qnil;
5393 else
5394 p->prev = XWINDOW (w->prev)->temslot;
5395
265a9e55 5396 if (!NILP (w->vchild))
7ab12479 5397 i = save_window_save (w->vchild, vector, i);
265a9e55 5398 if (!NILP (w->hchild))
7ab12479
JB
5399 i = save_window_save (w->hchild, vector, i);
5400 }
5401
5402 return i;
5403}
5404
a0d76c27 5405DEFUN ("current-window-configuration", Fcurrent_window_configuration,
fdb82f93
PJ
5406 Scurrent_window_configuration, 0, 1, 0,
5407 doc: /* Return an object representing the current window configuration of FRAME.
5408If FRAME is nil or omitted, use the selected frame.
5409This describes the number of windows, their sizes and current buffers,
5410and for each displayed buffer, where display starts, and the positions of
5411point and mark. An exception is made for point in the current buffer:
5412its value is -not- saved.
5413This also records the currently selected frame, and FRAME's focus
5414redirection (see `redirect-frame-focus'). */)
5415 (frame)
44fa5b1e 5416 Lisp_Object frame;
7ab12479
JB
5417{
5418 register Lisp_Object tem;
5419 register int n_windows;
5420 register struct save_window_data *data;
da2792e0 5421 register struct Lisp_Vector *vec;
7ab12479 5422 register int i;
44fa5b1e 5423 FRAME_PTR f;
43bad991 5424
44fa5b1e 5425 if (NILP (frame))
1ae1a37d 5426 frame = selected_frame;
b7826503 5427 CHECK_LIVE_FRAME (frame);
1ae1a37d 5428 f = XFRAME (frame);
7ab12479 5429
44fa5b1e 5430 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
26605be9 5431 vec = allocate_other_vector (VECSIZE (struct save_window_data));
da2792e0
KH
5432 data = (struct save_window_data *)vec;
5433
d834a2e9
KH
5434 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
5435 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
5436 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
9ea173e8 5437 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
1ae1a37d 5438 data->selected_frame = selected_frame;
44fa5b1e 5439 data->current_window = FRAME_SELECTED_WINDOW (f);
74112613 5440 XSETBUFFER (data->current_buffer, current_buffer);
3f49fddc 5441 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
3dbab091 5442 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
44fa5b1e 5443 data->root_window = FRAME_ROOT_WINDOW (f);
bdc727bf 5444 data->focus_frame = FRAME_FOCUS_FRAME (f);
74112613
KH
5445 XSETINT (data->min_height, window_min_height);
5446 XSETINT (data->min_width, window_min_width);
7ab12479
JB
5447 tem = Fmake_vector (make_number (n_windows), Qnil);
5448 data->saved_windows = tem;
5449 for (i = 0; i < n_windows; i++)
5450 XVECTOR (tem)->contents[i]
5451 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
a1d58e5b 5452 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
74112613 5453 XSETWINDOW_CONFIGURATION (tem, data);
7ab12479
JB
5454 return (tem);
5455}
5456
5457DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
fdb82f93
PJ
5458 0, UNEVALLED, 0,
5459 doc: /* Execute body, preserving window sizes and contents.
5460Restore which buffer appears in which window, where display starts,
5461and the value of point and mark for each window.
5462Also restore the choice of selected window.
5463Also restore which buffer is current.
6924dda6
MB
5464Does not restore the value of point in current buffer.
5465usage: (save-window-excursion BODY ...) */)
fdb82f93 5466 (args)
7ab12479
JB
5467 Lisp_Object args;
5468{
5469 register Lisp_Object val;
5470 register int count = specpdl_ptr - specpdl;
5471
5472 record_unwind_protect (Fset_window_configuration,
43bad991 5473 Fcurrent_window_configuration (Qnil));
7ab12479
JB
5474 val = Fprogn (args);
5475 return unbind_to (count, val);
5476}
5500c422
GM
5477
5478\f
5479/***********************************************************************
5480 Marginal Areas
5481 ***********************************************************************/
5482
5483DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
a7bdfc08 5484 2, 3, 0,
fdb82f93
PJ
5485 doc: /* Set width of marginal areas of window WINDOW.
5486If window is nil, set margins of the currently selected window.
5487First parameter LEFT-WIDTH specifies the number of character
5488cells to reserve for the left marginal area. Second parameter
5489RIGHT-WIDTH does the same for the right marginal area.
5490A nil width parameter means no margin. */)
5491 (window, left, right)
5500c422
GM
5492 Lisp_Object window, left, right;
5493{
5494 struct window *w = decode_window (window);
5500c422
GM
5495
5496 if (!NILP (left))
b7826503 5497 CHECK_NUMBER_OR_FLOAT (left);
5500c422 5498 if (!NILP (right))
b7826503 5499 CHECK_NUMBER_OR_FLOAT (right);
5500c422
GM
5500
5501 /* Check widths < 0 and translate a zero width to nil.
5502 Margins that are too wide have to be checked elsewhere. */
5503 if ((INTEGERP (left) && XINT (left) < 0)
7539e11f 5504 || (FLOATP (left) && XFLOAT_DATA (left) <= 0))
5500c422
GM
5505 XSETFASTINT (left, 0);
5506 if (INTEGERP (left) && XFASTINT (left) == 0)
5507 left = Qnil;
5508
5509 if ((INTEGERP (right) && XINT (right) < 0)
7539e11f 5510 || (FLOATP (right) && XFLOAT_DATA (right) <= 0))
5500c422
GM
5511 XSETFASTINT (right, 0);
5512 if (INTEGERP (right) && XFASTINT (right) == 0)
5513 right = Qnil;
5514
5515 w->left_margin_width = left;
5516 w->right_margin_width = right;
5517
5518 ++windows_or_buffers_changed;
5519 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
5520 return Qnil;
5521}
5522
5523
5524DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
5525 0, 1, 0,
fdb82f93
PJ
5526 doc: /* Get width of marginal areas of window WINDOW.
5527If WINDOW is omitted or nil, use the currently selected window.
5528Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
5529If a marginal area does not exist, its width will be returned
5530as nil. */)
5531 (window)
5500c422
GM
5532 Lisp_Object window;
5533{
5534 struct window *w = decode_window (window);
5535 return Fcons (w->left_margin_width, w->right_margin_width);
5536}
5537
5538
7ab12479 5539\f
5500c422
GM
5540/***********************************************************************
5541 Smooth scrolling
5542 ***********************************************************************/
5543
5544DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
fdb82f93
PJ
5545 doc: /* Return the amount by which WINDOW is scrolled vertically.
5546Use the selected window if WINDOW is nil or omitted.
5547Value is a multiple of the canonical character height of WINDOW. */)
5548 (window)
5500c422
GM
5549 Lisp_Object window;
5550{
47004952 5551 Lisp_Object result;
5500c422
GM
5552 struct frame *f;
5553 struct window *w;
5554
5555 if (NILP (window))
5556 window = selected_window;
47004952 5557 else
b7826503 5558 CHECK_WINDOW (window);
5500c422
GM
5559 w = XWINDOW (window);
5560 f = XFRAME (w->frame);
5561
5562 if (FRAME_WINDOW_P (f))
47004952 5563 result = CANON_Y_FROM_PIXEL_Y (f, -w->vscroll);
5500c422 5564 else
47004952
GM
5565 result = make_number (0);
5566 return result;
5500c422
GM
5567}
5568
5569
5570DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
47004952 5571 2, 2, 0,
fdb82f93 5572 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
b0f906c8
EZ
5573WINDOW nil means use the selected window. VSCROLL is a non-negative
5574multiple of the canonical character height of WINDOW. */)
fdb82f93 5575 (window, vscroll)
47004952 5576 Lisp_Object window, vscroll;
5500c422
GM
5577{
5578 struct window *w;
5579 struct frame *f;
5580
5500c422
GM
5581 if (NILP (window))
5582 window = selected_window;
47004952 5583 else
b7826503
PJ
5584 CHECK_WINDOW (window);
5585 CHECK_NUMBER_OR_FLOAT (vscroll);
47004952 5586
5500c422
GM
5587 w = XWINDOW (window);
5588 f = XFRAME (w->frame);
5589
5590 if (FRAME_WINDOW_P (f))
5591 {
5592 int old_dy = w->vscroll;
47004952
GM
5593
5594 w->vscroll = - CANON_Y_UNIT (f) * XFLOATINT (vscroll);
5595 w->vscroll = min (w->vscroll, 0);
5500c422
GM
5596
5597 /* Adjust glyph matrix of the frame if the virtual display
5598 area becomes larger than before. */
5599 if (w->vscroll < 0 && w->vscroll < old_dy)
5600 adjust_glyphs (f);
5601
5602 /* Prevent redisplay shortcuts. */
b1599b4c 5603 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5500c422
GM
5604 }
5605
47004952 5606 return Fwindow_vscroll (window);
5500c422
GM
5607}
5608
7bbb5782
GM
5609\f
5610/* Call FN for all leaf windows on frame F. FN is called with the
5611 first argument being a pointer to the leaf window, and with
f95464e4 5612 additional argument USER_DATA. Stops when FN returns 0. */
7bbb5782
GM
5613
5614void
f95464e4 5615foreach_window (f, fn, user_data)
7bbb5782 5616 struct frame *f;
f95464e4
GM
5617 int (* fn) P_ ((struct window *, void *));
5618 void *user_data;
7bbb5782 5619{
f95464e4 5620 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7bbb5782
GM
5621}
5622
5623
5624/* Helper function for foreach_window. Call FN for all leaf windows
5625 reachable from W. FN is called with the first argument being a
f95464e4 5626 pointer to the leaf window, and with additional argument USER_DATA.
67492200 5627 Stop when FN returns 0. Value is 0 if stopped by FN. */
7bbb5782 5628
67492200 5629static int
f95464e4 5630foreach_window_1 (w, fn, user_data)
7bbb5782 5631 struct window *w;
f95464e4
GM
5632 int (* fn) P_ ((struct window *, void *));
5633 void *user_data;
7bbb5782 5634{
67492200
GM
5635 int cont;
5636
5637 for (cont = 1; w && cont;)
7bbb5782
GM
5638 {
5639 if (!NILP (w->hchild))
f95464e4 5640 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
7bbb5782 5641 else if (!NILP (w->vchild))
f95464e4 5642 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
0f532a9a
GM
5643 else
5644 cont = fn (w, user_data);
7bbb5782
GM
5645
5646 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5647 }
67492200
GM
5648
5649 return cont;
7bbb5782
GM
5650}
5651
5652
6d194a45 5653/* Freeze or unfreeze the window start of W unless it is a
f95464e4 5654 mini-window or the selected window. FREEZE_P non-null means freeze
7bbb5782
GM
5655 the window start. */
5656
67492200 5657static int
7bbb5782
GM
5658freeze_window_start (w, freeze_p)
5659 struct window *w;
f95464e4 5660 void *freeze_p;
7bbb5782
GM
5661{
5662 if (w == XWINDOW (selected_window)
5663 || MINI_WINDOW_P (w)
5664 || (MINI_WINDOW_P (XWINDOW (selected_window))
24d744ac 5665 && ! NILP (Vminibuf_scroll_window)
7bbb5782 5666 && w == XWINDOW (Vminibuf_scroll_window)))
f95464e4 5667 freeze_p = NULL;
7bbb5782 5668
f95464e4 5669 w->frozen_window_start_p = freeze_p != NULL;
67492200 5670 return 1;
7bbb5782
GM
5671}
5672
5673
5674/* Freeze or unfreeze the window starts of all leaf windows on frame
5675 F, except the selected window and a mini-window. FREEZE_P non-zero
5676 means freeze the window start. */
5677
5678void
5679freeze_window_starts (f, freeze_p)
5680 struct frame *f;
5681 int freeze_p;
5682{
cbccabec 5683 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7bbb5782 5684}
5500c422
GM
5685
5686\f
5687/***********************************************************************
5688 Initialization
5689 ***********************************************************************/
5690
cbff28e8
RS
5691/* Return 1 if window configurations C1 and C2
5692 describe the same state of affairs. This is used by Fequal. */
5693
5694int
2f8274be 5695compare_window_configurations (c1, c2, ignore_positions)
cbff28e8 5696 Lisp_Object c1, c2;
2f8274be 5697 int ignore_positions;
cbff28e8
RS
5698{
5699 register struct save_window_data *d1, *d2;
5700 struct Lisp_Vector *sw1, *sw2;
5701 int i;
5702
4d3edcb4
GM
5703 if (!WINDOW_CONFIGURATIONP (c1))
5704 wrong_type_argument (Qwindow_configuration_p, c1);
5705 if (!WINDOW_CONFIGURATIONP (c2))
5706 wrong_type_argument (Qwindow_configuration_p, c2);
5707
cbff28e8
RS
5708 d1 = (struct save_window_data *) XVECTOR (c1);
5709 d2 = (struct save_window_data *) XVECTOR (c2);
5710 sw1 = XVECTOR (d1->saved_windows);
5711 sw2 = XVECTOR (d2->saved_windows);
5712
5713 if (! EQ (d1->frame_width, d2->frame_width))
5714 return 0;
5715 if (! EQ (d1->frame_height, d2->frame_height))
5716 return 0;
5717 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
5718 return 0;
5719 if (! EQ (d1->selected_frame, d2->selected_frame))
5720 return 0;
5721 /* Don't compare the current_window field directly.
5722 Instead see w1_is_current and w2_is_current, below. */
5723 if (! EQ (d1->current_buffer, d2->current_buffer))
5724 return 0;
2f8274be 5725 if (! ignore_positions)
3f49fddc
KS
5726 {
5727 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
5728 return 0;
5729 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
5730 return 0;
5731 }
cbff28e8
RS
5732 /* Don't compare the root_window field.
5733 We don't require the two configurations
5734 to use the same window object,
5735 and the two root windows must be equivalent
5736 if everything else compares equal. */
5737 if (! EQ (d1->focus_frame, d2->focus_frame))
5738 return 0;
5739 if (! EQ (d1->min_width, d2->min_width))
5740 return 0;
5741 if (! EQ (d1->min_height, d2->min_height))
5742 return 0;
5743
5744 /* Verify that the two confis have the same number of windows. */
5745 if (sw1->size != sw2->size)
5746 return 0;
5747
5748 for (i = 0; i < sw1->size; i++)
5749 {
5750 struct saved_window *p1, *p2;
5751 int w1_is_current, w2_is_current;
5752
5753 p1 = SAVED_WINDOW_N (sw1, i);
5754 p2 = SAVED_WINDOW_N (sw2, i);
5755
5756 /* Verify that the current windows in the two
5757 configurations correspond to each other. */
5758 w1_is_current = EQ (d1->current_window, p1->window);
5759 w2_is_current = EQ (d2->current_window, p2->window);
5760
5761 if (w1_is_current != w2_is_current)
5762 return 0;
5763
5764 /* Verify that the corresponding windows do match. */
5765 if (! EQ (p1->buffer, p2->buffer))
5766 return 0;
5767 if (! EQ (p1->left, p2->left))
5768 return 0;
5769 if (! EQ (p1->top, p2->top))
5770 return 0;
5771 if (! EQ (p1->width, p2->width))
5772 return 0;
5773 if (! EQ (p1->height, p2->height))
5774 return 0;
cbff28e8
RS
5775 if (! EQ (p1->display_table, p2->display_table))
5776 return 0;
5777 if (! EQ (p1->parent, p2->parent))
5778 return 0;
5779 if (! EQ (p1->prev, p2->prev))
5780 return 0;
2f8274be
RS
5781 if (! ignore_positions)
5782 {
5783 if (! EQ (p1->hscroll, p2->hscroll))
5784 return 0;
ea68264b
GM
5785 if (!EQ (p1->min_hscroll, p2->min_hscroll))
5786 return 0;
2f8274be
RS
5787 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
5788 return 0;
5789 if (NILP (Fequal (p1->start, p2->start)))
5790 return 0;
5791 if (NILP (Fequal (p1->pointm, p2->pointm)))
5792 return 0;
5793 if (NILP (Fequal (p1->mark, p2->mark)))
5794 return 0;
5795 }
cbff28e8
RS
5796 }
5797
5798 return 1;
5799}
2f8274be
RS
5800
5801DEFUN ("compare-window-configurations", Fcompare_window_configurations,
5802 Scompare_window_configurations, 2, 2, 0,
fdb82f93
PJ
5803 doc: /* Compare two window configurations as regards the structure of windows.
5804This function ignores details such as the values of point and mark
5805and scrolling positions. */)
5806 (x, y)
2f8274be
RS
5807 Lisp_Object x, y;
5808{
5809 if (compare_window_configurations (x, y, 1))
5810 return Qt;
5811 return Qnil;
5812}
cbff28e8 5813\f
dfcf069d 5814void
7ab12479
JB
5815init_window_once ()
5816{
1ae1a37d
GM
5817 struct frame *f = make_terminal_frame ();
5818 XSETFRAME (selected_frame, f);
5819 Vterminal_frame = selected_frame;
5820 minibuf_window = f->minibuffer_window;
5821 selected_window = f->selected_window;
5822 last_nonminibuf_frame = f;
5b03d3c0
RS
5823
5824 window_initialized = 1;
7ab12479
JB
5825}
5826
67492200
GM
5827void
5828init_window ()
5829{
5830 Vwindow_list = Qnil;
5831}
5832
dfcf069d 5833void
7ab12479
JB
5834syms_of_window ()
5835{
8a37516b
GM
5836 Qwindow_size_fixed = intern ("window-size-fixed");
5837 staticpro (&Qwindow_size_fixed);
233a4a2c 5838
543f5fb1
RS
5839 staticpro (&Qwindow_configuration_change_hook);
5840 Qwindow_configuration_change_hook
5841 = intern ("window-configuration-change-hook");
5842
7ab12479
JB
5843 Qwindowp = intern ("windowp");
5844 staticpro (&Qwindowp);
5845
3f8ab7bd
RS
5846 Qwindow_configuration_p = intern ("window-configuration-p");
5847 staticpro (&Qwindow_configuration_p);
5848
806b4d9b
JB
5849 Qwindow_live_p = intern ("window-live-p");
5850 staticpro (&Qwindow_live_p);
605be8af 5851
2cccc823 5852 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
a58ec57d
RS
5853 staticpro (&Qtemp_buffer_show_hook);
5854
67492200 5855 staticpro (&Vwindow_list);
8bfb170b
KS
5856
5857 minibuf_selected_window = Qnil;
3dbab091 5858 staticpro (&minibuf_selected_window);
67492200 5859
7ab12479 5860 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
fdb82f93
PJ
5861 doc: /* Non-nil means call as function to display a help buffer.
5862The function is called with one argument, the buffer to be displayed.
5863Used by `with-output-to-temp-buffer'.
5864If this function is used, then it must do the entire job of showing
5865the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7ab12479
JB
5866 Vtemp_buffer_show_function = Qnil;
5867
5868 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
fdb82f93
PJ
5869 doc: /* If non-nil, function to call to handle `display-buffer'.
5870It will receive two args, the buffer and a flag which if non-nil means
5871 that the currently selected window is not acceptable.
5872Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
5873work using this function. */);
7ab12479
JB
5874 Vdisplay_buffer_function = Qnil;
5875
6529ed87 5876 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
fdb82f93
PJ
5877 doc: /* *If non-nil, `display-buffer' should even the window heights.
5878If nil, `display-buffer' will leave the window configuration alone. */);
6529ed87
GM
5879 Veven_window_heights = Qt;
5880
7ab12479 5881 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
fdb82f93 5882 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7ab12479
JB
5883 Vminibuf_scroll_window = Qnil;
5884
cc91894c 5885 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
26124d5e 5886 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
cc91894c
KS
5887If the minibuffer is active, the `minibuffer-scroll-window' mode line
5888is displayed in the `mode-line' face. */);
5889 mode_line_in_non_selected_windows = 1;
26124d5e 5890
7ab12479 5891 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
fdb82f93 5892 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7ab12479
JB
5893 Vother_window_scroll_buffer = Qnil;
5894
44fa5b1e 5895 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
fdb82f93 5896 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
44fa5b1e 5897 pop_up_frames = 0;
7ab12479 5898
9c3da604 5899 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
fdb82f93
PJ
5900 doc: /* *Non-nil means `display-buffer' should reuse frames.
5901If the buffer in question is already displayed in a frame, raise that frame. */);
9c3da604
GM
5902 display_buffer_reuse_frames = 0;
5903
44fa5b1e 5904 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
fdb82f93
PJ
5905 doc: /* Function to call to handle automatic new frame creation.
5906It is called with no arguments and should return a newly created frame.
5907
5908A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
5909where `pop-up-frame-alist' would hold the default frame parameters. */);
44fa5b1e 5910 Vpop_up_frame_function = Qnil;
7ab12479 5911
a90712c2 5912 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
fdb82f93
PJ
5913 doc: /* *List of buffer names that should have their own special frames.
5914Displaying a buffer whose name is in this list makes a special frame for it
5915using `special-display-function'. See also `special-display-regexps'.
5916
5917An element of the list can be a list instead of just a string.
5918There are two ways to use a list as an element:
5919 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
5920In the first case, FRAME-PARAMETERS are used to create the frame.
5921In the latter case, FUNCTION is called with BUFFER as the first argument,
5922followed by OTHER-ARGS--it can display BUFFER in any way it likes.
5923All this is done by the function found in `special-display-function'.
5924
5925If this variable appears \"not to work\", because you add a name to it
5926but that buffer still appears in the selected window, look at the
5927values of `same-window-buffer-names' and `same-window-regexps'.
5928Those variables take precedence over this one. */);
a90712c2
RS
5929 Vspecial_display_buffer_names = Qnil;
5930
5931 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
fdb82f93
PJ
5932 doc: /* *List of regexps saying which buffers should have their own special frames.
5933If a buffer name matches one of these regexps, it gets its own frame.
5934Displaying a buffer whose name is in this list makes a special frame for it
5935using `special-display-function'.
5936
5937An element of the list can be a list instead of just a string.
5938There are two ways to use a list as an element:
5939 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
5940In the first case, FRAME-PARAMETERS are used to create the frame.
5941In the latter case, FUNCTION is called with the buffer as first argument,
5942followed by OTHER-ARGS--it can display the buffer in any way it likes.
5943All this is done by the function found in `special-display-function'.
5944
5945If this variable appears \"not to work\", because you add a regexp to it
5946but the matching buffers still appear in the selected window, look at the
5947values of `same-window-buffer-names' and `same-window-regexps'.
5948Those variables take precedence over this one. */);
a90712c2
RS
5949 Vspecial_display_regexps = Qnil;
5950
5951 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
fdb82f93
PJ
5952 doc: /* Function to call to make a new frame for a special buffer.
5953It is called with two arguments, the buffer and optional buffer specific
5954data, and should return a window displaying that buffer.
5955The default value makes a separate frame for the buffer,
5956using `special-display-frame-alist' to specify the frame parameters.
5957
5958A buffer is special if its is listed in `special-display-buffer-names'
5959or matches a regexp in `special-display-regexps'. */);
a90712c2
RS
5960 Vspecial_display_function = Qnil;
5961
855d8627 5962 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
fdb82f93
PJ
5963 doc: /* *List of buffer names that should appear in the selected window.
5964Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
5965switches to it in the selected window, rather than making it appear
5966in some other window.
5967
5968An element of the list can be a cons cell instead of just a string.
5969Then the car must be a string, which specifies the buffer name.
5970This is for compatibility with `special-display-buffer-names';
5971the cdr of the cons cell is ignored.
5972
5973See also `same-window-regexps'. */);
855d8627
RS
5974 Vsame_window_buffer_names = Qnil;
5975
5976 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
fdb82f93
PJ
5977 doc: /* *List of regexps saying which buffers should appear in the selected window.
5978If a buffer name matches one of these regexps, then displaying it
5979using `display-buffer' or `pop-to-buffer' switches to it
5980in the selected window, rather than making it appear in some other window.
5981
5982An element of the list can be a cons cell instead of just a string.
5983Then the car must be a string, which specifies the buffer name.
5984This is for compatibility with `special-display-buffer-names';
5985the cdr of the cons cell is ignored.
5986
5987See also `same-window-buffer-names'. */);
855d8627
RS
5988 Vsame_window_regexps = Qnil;
5989
7ab12479 5990 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
fdb82f93 5991 doc: /* *Non-nil means display-buffer should make new windows. */);
7ab12479
JB
5992 pop_up_windows = 1;
5993
5994 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
fdb82f93 5995 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7ab12479
JB
5996 next_screen_context_lines = 2;
5997
5998 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
fdb82f93
PJ
5999 doc: /* *display-buffer would prefer to split the largest window if this large.
6000If there is only one window, it is split regardless of this value. */);
7ab12479
JB
6001 split_height_threshold = 500;
6002
6003 DEFVAR_INT ("window-min-height", &window_min_height,
fdb82f93 6004 doc: /* *Delete any window less than this tall (including its mode line). */);
7ab12479
JB
6005 window_min_height = 4;
6006
6007 DEFVAR_INT ("window-min-width", &window_min_width,
fdb82f93 6008 doc: /* *Delete any window less than this wide. */);
7ab12479
JB
6009 window_min_width = 10;
6010
5500c422
GM
6011 DEFVAR_LISP ("scroll-preserve-screen-position",
6012 &Vscroll_preserve_screen_position,
fdb82f93 6013 doc: /* *Non-nil means scroll commands move point to keep its screen line unchanged. */);
5500c422 6014 Vscroll_preserve_screen_position = Qnil;
9317a85d 6015
543f5fb1
RS
6016 DEFVAR_LISP ("window-configuration-change-hook",
6017 &Vwindow_configuration_change_hook,
fdb82f93
PJ
6018 doc: /* Functions to call when window configuration changes.
6019The selected frame is the one whose configuration has changed. */);
543f5fb1
RS
6020 Vwindow_configuration_change_hook = Qnil;
6021
b0b7ed0e 6022 DEFVAR_BOOL ("window-size-fixed", &window_size_fixed,
fdb82f93
PJ
6023 doc: /* Non-nil in a buffer means windows displaying the buffer are fixed-size.
6024Emacs won't change the size of any window displaying that buffer,
6025unless you explicitly change the size, or Emacs has no other choice.
6026This variable automatically becomes buffer-local when set. */);
b0b7ed0e
GM
6027 Fmake_variable_buffer_local (Qwindow_size_fixed);
6028 window_size_fixed = 0;
6029
7ab12479
JB
6030 defsubr (&Sselected_window);
6031 defsubr (&Sminibuffer_window);
6032 defsubr (&Swindow_minibuffer_p);
6033 defsubr (&Swindowp);
806b4d9b 6034 defsubr (&Swindow_live_p);
7ab12479
JB
6035 defsubr (&Spos_visible_in_window_p);
6036 defsubr (&Swindow_buffer);
6037 defsubr (&Swindow_height);
6038 defsubr (&Swindow_width);
6039 defsubr (&Swindow_hscroll);
6040 defsubr (&Sset_window_hscroll);
190eb263
RS
6041 defsubr (&Swindow_redisplay_end_trigger);
6042 defsubr (&Sset_window_redisplay_end_trigger);
7ab12479 6043 defsubr (&Swindow_edges);
d5783c40
JB
6044 defsubr (&Scoordinates_in_window_p);
6045 defsubr (&Swindow_at);
7ab12479
JB
6046 defsubr (&Swindow_point);
6047 defsubr (&Swindow_start);
6048 defsubr (&Swindow_end);
6049 defsubr (&Sset_window_point);
6050 defsubr (&Sset_window_start);
6051 defsubr (&Swindow_dedicated_p);
d207b766 6052 defsubr (&Sset_window_dedicated_p);
7ab12479
JB
6053 defsubr (&Swindow_display_table);
6054 defsubr (&Sset_window_display_table);
6055 defsubr (&Snext_window);
6056 defsubr (&Sprevious_window);
6057 defsubr (&Sother_window);
6058 defsubr (&Sget_lru_window);
6059 defsubr (&Sget_largest_window);
6060 defsubr (&Sget_buffer_window);
6061 defsubr (&Sdelete_other_windows);
6062 defsubr (&Sdelete_windows_on);
6063 defsubr (&Sreplace_buffer_in_windows);
6064 defsubr (&Sdelete_window);
6065 defsubr (&Sset_window_buffer);
6066 defsubr (&Sselect_window);
4628f7a4
EN
6067 defsubr (&Sspecial_display_p);
6068 defsubr (&Ssame_window_p);
7ab12479
JB
6069 defsubr (&Sdisplay_buffer);
6070 defsubr (&Ssplit_window);
6071 defsubr (&Senlarge_window);
6072 defsubr (&Sshrink_window);
6073 defsubr (&Sscroll_up);
6074 defsubr (&Sscroll_down);
6075 defsubr (&Sscroll_left);
6076 defsubr (&Sscroll_right);
ccd0664b 6077 defsubr (&Sother_window_for_scrolling);
7ab12479 6078 defsubr (&Sscroll_other_window);
fa832261 6079 defsubr (&Sminibuffer_selected_window);
7ab12479 6080 defsubr (&Srecenter);
81fe0836 6081 defsubr (&Swindow_text_height);
7ab12479
JB
6082 defsubr (&Smove_to_window_line);
6083 defsubr (&Swindow_configuration_p);
3f8ab7bd 6084 defsubr (&Swindow_configuration_frame);
7ab12479
JB
6085 defsubr (&Sset_window_configuration);
6086 defsubr (&Scurrent_window_configuration);
6087 defsubr (&Ssave_window_excursion);
5500c422
GM
6088 defsubr (&Sset_window_margins);
6089 defsubr (&Swindow_margins);
6090 defsubr (&Swindow_vscroll);
6091 defsubr (&Sset_window_vscroll);
2f8274be 6092 defsubr (&Scompare_window_configurations);
67492200 6093 defsubr (&Swindow_list);
7ab12479
JB
6094}
6095
dfcf069d 6096void
7ab12479
JB
6097keys_of_window ()
6098{
6099 initial_define_key (control_x_map, '1', "delete-other-windows");
6100 initial_define_key (control_x_map, '2', "split-window");
6101 initial_define_key (control_x_map, '0', "delete-window");
6102 initial_define_key (control_x_map, 'o', "other-window");
6103 initial_define_key (control_x_map, '^', "enlarge-window");
6104 initial_define_key (control_x_map, '<', "scroll-left");
6105 initial_define_key (control_x_map, '>', "scroll-right");
6106
6107 initial_define_key (global_map, Ctl ('V'), "scroll-up");
6108 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6109 initial_define_key (meta_map, 'v', "scroll-down");
6110
6111 initial_define_key (global_map, Ctl('L'), "recenter");
6112 initial_define_key (meta_map, 'r', "move-to-window-line");
6113}