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