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