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