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