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