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