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