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