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