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