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