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