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