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