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