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