upstream
[emacs.git] / src / window.c
blob33361b5ac1d7683cbdffb0df35ab1ad0cb5f6d55
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2011
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28 #include "keymap.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "commands.h"
32 #include "indent.h"
33 #include "termchar.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "blockinput.h"
37 #include "intervals.h"
38 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_X_WINDOWS
41 #include "xterm.h"
42 #endif /* HAVE_X_WINDOWS */
43 #ifdef WINDOWSNT
44 #include "w32term.h"
45 #endif
46 #ifdef MSDOS
47 #include "msdos.h"
48 #endif
49 #ifdef HAVE_NS
50 #include "nsterm.h"
51 #endif
52 #ifdef HAVE_XWIDGETS
53 #include "xwidget.h"
54 #endif
55 Lisp_Object Qwindowp, Qwindow_live_p;
56 static Lisp_Object Qwindow_configuration_p, Qrecord_window_buffer;
57 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
58 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
59 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
60 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
61 static Lisp_Object Qsafe, Qabove, Qbelow;
62 static Lisp_Object Qauto_buffer_name;
64 static int displayed_window_lines (struct window *);
65 static struct window *decode_window (Lisp_Object);
66 static int count_windows (struct window *);
67 static int get_leaf_windows (struct window *, struct window **, int);
68 static void window_scroll (Lisp_Object, int, int, int);
69 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
70 static void window_scroll_line_based (Lisp_Object, int, int, int);
71 static int freeze_window_start (struct window *, void *);
72 static Lisp_Object window_list (void);
73 static int add_window_to_list (struct window *, void *);
74 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
75 Lisp_Object);
76 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
77 Lisp_Object, int);
78 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
79 Lisp_Object *);
80 static void foreach_window (struct frame *,
81 int (* fn) (struct window *, void *),
82 void *);
83 static int foreach_window_1 (struct window *,
84 int (* fn) (struct window *, void *),
85 void *);
86 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
87 static int window_resize_check (struct window *, int);
88 static void window_resize_apply (struct window *, int);
89 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
91 /* This is the window in which the terminal's cursor should
92 be left when nothing is being done with it. This must
93 always be a leaf window, and its buffer is selected by
94 the top level editing loop at the end of each command.
96 This value is always the same as
97 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109 Lisp_Object minibuf_window;
111 /* Non-nil means it is the window whose mode line should be
112 shown as the selected window when the minibuffer is selected. */
113 Lisp_Object minibuf_selected_window;
115 /* Hook run at end of temp_output_buffer_show. */
116 static Lisp_Object Qtemp_buffer_show_hook;
118 /* Incremented for each window created. */
119 static int sequence_number;
121 /* Nonzero after init_window_once has finished. */
122 static int window_initialized;
124 /* Hook to run when window config changes. */
125 static Lisp_Object Qwindow_configuration_change_hook;
127 /* Used by the function window_scroll_pixel_based */
128 static int window_scroll_pixel_based_preserve_x;
129 static int window_scroll_pixel_based_preserve_y;
131 /* Same for window_scroll_line_based. */
132 static int window_scroll_preserve_hpos;
133 static int window_scroll_preserve_vpos;
135 static struct window *
136 decode_window (register Lisp_Object window)
138 if (NILP (window))
139 return XWINDOW (selected_window);
141 CHECK_LIVE_WINDOW (window);
142 return XWINDOW (window);
145 static struct window *
146 decode_any_window (register Lisp_Object window)
148 if (NILP (window))
149 return XWINDOW (selected_window);
151 CHECK_WINDOW (window);
152 return XWINDOW (window);
155 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
156 doc: /* Return t if OBJECT is a window and nil otherwise. */)
157 (Lisp_Object object)
159 return WINDOWP (object) ? Qt : Qnil;
162 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
163 doc: /* Return t if OBJECT is a live window and nil otherwise.
164 A live window is a window that displays a buffer. */)
165 (Lisp_Object object)
167 return WINDOW_LIVE_P (object) ? Qt : Qnil;
170 /* Frames and windows. */
171 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 1, 1, 0,
172 doc: /* Return the frame that window WINDOW is on.
173 WINDOW can be any window and defaults to the selected one. */)
174 (Lisp_Object window)
176 return decode_any_window (window)->frame;
179 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
180 doc: /* Return the root window of FRAME_OR_WINDOW.
181 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
182 Else if FRAME_OR_WINDOW denotes any window, return the root window of
183 that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
184 the root window of that frame. */)
185 (Lisp_Object frame_or_window)
187 Lisp_Object window;
189 if (NILP (frame_or_window))
190 window = SELECTED_FRAME ()->root_window;
191 else if (WINDOWP (frame_or_window))
192 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
193 else
195 CHECK_LIVE_FRAME (frame_or_window);
196 window = XFRAME (frame_or_window)->root_window;
199 return window;
202 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
203 doc: /* Return the window used now for minibuffers.
204 If the optional argument FRAME is specified, return the minibuffer window
205 used by that frame. */)
206 (Lisp_Object frame)
208 if (NILP (frame))
209 frame = selected_frame;
210 CHECK_LIVE_FRAME (frame);
211 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
214 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
215 Swindow_minibuffer_p, 0, 1, 0,
216 doc: /* Return non-nil if WINDOW is a minibuffer window.
217 WINDOW can be any window and defaults to the selected one. */)
218 (Lisp_Object window)
220 return MINI_WINDOW_P (decode_any_window (window)) ? Qt : Qnil;
223 /* Don't move this to window.el - this must be a safe routine. */
224 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
225 doc: /* Return the topmost, leftmost live window on FRAME_OR_WINDOW.
226 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
227 Else if FRAME_OR_WINDOW denotes any window, return the first window of
228 that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
229 the first window of that frame. */)
230 (Lisp_Object frame_or_window)
232 Lisp_Object window;
234 if (NILP (frame_or_window))
235 window = SELECTED_FRAME ()->root_window;
236 else if (WINDOWP (frame_or_window))
237 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
238 else
240 CHECK_LIVE_FRAME (frame_or_window);
241 window = XFRAME (frame_or_window)->root_window;
244 while (NILP (XWINDOW (window)->buffer))
246 if (! NILP (XWINDOW (window)->hchild))
247 window = XWINDOW (window)->hchild;
248 else if (! NILP (XWINDOW (window)->vchild))
249 window = XWINDOW (window)->vchild;
250 else
251 abort ();
254 return window;
257 DEFUN ("frame-selected-window", Fframe_selected_window,
258 Sframe_selected_window, 0, 1, 0,
259 doc: /* Return the selected window of FRAME_OR_WINDOW.
260 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
261 Else if FRAME_OR_WINDOW denotes any window, return the selected window
262 of that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
263 the selected window of that frame. */)
264 (Lisp_Object frame_or_window)
266 Lisp_Object window;
268 if (NILP (frame_or_window))
269 window = SELECTED_FRAME ()->selected_window;
270 else if (WINDOWP (frame_or_window))
271 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
272 else
274 CHECK_LIVE_FRAME (frame_or_window);
275 window = XFRAME (frame_or_window)->selected_window;
278 return window;
281 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
282 Sset_frame_selected_window, 2, 3, 0,
283 doc: /* Set selected window of FRAME to WINDOW.
284 FRAME must be a live frame and defaults to the selected one. If FRAME
285 is the selected frame, this makes WINDOW the selected window. Optional
286 argument NORECORD non-nil means to neither change the order of recently
287 selected windows nor the buffer list. WINDOW must denote a live window.
288 Return WINDOW. */)
289 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
291 if (NILP (frame))
292 frame = selected_frame;
294 CHECK_LIVE_FRAME (frame);
295 CHECK_LIVE_WINDOW (window);
297 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
298 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
300 if (EQ (frame, selected_frame))
301 return Fselect_window (window, norecord);
302 else
303 return XFRAME (frame)->selected_window = window;
306 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
307 doc: /* Return the selected window.
308 The selected window is the window in which the standard cursor for
309 selected windows appears and to which many commands apply. */)
310 (void)
312 return selected_window;
315 int window_select_count;
317 /* If select_window is called with inhibit_point_swap non-zero it will
318 not store point of the old selected window's buffer back into that
319 window's pointm slot. This is needed by Fset_window_configuration to
320 avoid that the display routine is called with selected_window set to
321 Qnil causing a subsequent crash. */
322 static Lisp_Object
323 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
325 register struct window *w;
326 register struct window *ow;
327 struct frame *sf;
329 CHECK_LIVE_WINDOW (window);
331 w = XWINDOW (window);
332 w->frozen_window_start_p = 0;
334 if (NILP (norecord))
336 ++window_select_count;
337 XSETFASTINT (w->use_time, window_select_count);
338 record_buffer (w->buffer);
341 if (EQ (window, selected_window) && !inhibit_point_swap)
342 return window;
344 sf = SELECTED_FRAME ();
345 if (XFRAME (WINDOW_FRAME (w)) != sf)
347 XFRAME (WINDOW_FRAME (w))->selected_window = window;
348 /* Use this rather than Fhandle_switch_frame
349 so that FRAME_FOCUS_FRAME is moved appropriately as we
350 move around in the state where a minibuffer in a separate
351 frame is active. */
352 Fselect_frame (WINDOW_FRAME (w), norecord);
353 /* Fselect_frame called us back so we've done all the work already. */
354 eassert (EQ (window, selected_window));
355 return window;
357 else
358 sf->selected_window = window;
360 /* Store the current buffer's actual point into the
361 old selected window. It belongs to that window,
362 and when the window is not selected, must be in the window. */
363 if (!inhibit_point_swap)
365 ow = XWINDOW (selected_window);
366 if (! NILP (ow->buffer))
367 set_marker_both (ow->pointm, ow->buffer,
368 BUF_PT (XBUFFER (ow->buffer)),
369 BUF_PT_BYTE (XBUFFER (ow->buffer)));
372 selected_window = window;
374 Fset_buffer (w->buffer);
376 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
378 /* Go to the point recorded in the window.
379 This is important when the buffer is in more
380 than one window. It also matters when
381 redisplay_window has altered point after scrolling,
382 because it makes the change only in the window. */
384 register EMACS_INT new_point = marker_position (w->pointm);
385 if (new_point < BEGV)
386 SET_PT (BEGV);
387 else if (new_point > ZV)
388 SET_PT (ZV);
389 else
390 SET_PT (new_point);
393 windows_or_buffers_changed++;
394 return window;
397 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
398 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
399 Also make WINDOW's buffer current and make WINDOW the frame's selected
400 window. Return WINDOW.
402 Optional second arg NORECORD non-nil means do not put this buffer at the
403 front of the buffer list and do not make this window the most recently
404 selected one.
406 Note that the main editor command loop sets the current buffer to the
407 buffer of the selected window before each command. */)
408 (register Lisp_Object window, Lisp_Object norecord)
410 return select_window (window, norecord, 0);
413 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
414 doc: /* Return the buffer that WINDOW is displaying.
415 WINDOW can be any window and defaults to the selected one.
416 If WINDOW is an internal window return nil. */)
417 (Lisp_Object window)
419 return decode_any_window (window)->buffer;
422 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
423 doc: /* Return WINDOW's parent window.
424 WINDOW can be any window and defaults to the selected one.
425 Return nil if WINDOW has no parent. */)
426 (Lisp_Object window)
428 return decode_any_window (window)->parent;
431 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
432 doc: /* Return WINDOW's topmost child window.
433 WINDOW can be any window and defaults to the selected one.
434 Return nil if WINDOW is not a vertical combination. */)
435 (Lisp_Object window)
437 return decode_any_window (window)->vchild;
440 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
441 doc: /* Return WINDOW's leftmost child window.
442 WINDOW can be any window and defaults to the selected one.
443 Return nil if WINDOW is not a horizontal combination. */)
444 (Lisp_Object window)
446 return decode_any_window (window)->hchild;
449 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
450 doc: /* Return WINDOW's next sibling window.
451 WINDOW can be any window and defaults to the selected one.
452 Return nil if WINDOW has no next sibling. */)
453 (Lisp_Object window)
455 return decode_any_window (window)->next;
458 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
459 doc: /* Return WINDOW's previous sibling window.
460 WINDOW can be any window and defaults to the selected one.
461 Return nil if WINDOW has no previous sibling. */)
462 (Lisp_Object window)
464 return decode_any_window (window)->prev;
467 DEFUN ("window-splits", Fwindow_splits, Swindow_splits, 0, 1, 0,
468 doc: /* Return splits status for WINDOW.
469 WINDOW can be any window and defaults to the selected one.
471 If the value returned by this function is nil and WINDOW is resized, the
472 corresponding space is preferably taken from (or given to) WINDOW's
473 right sibling. When WINDOW is deleted, its space is given to its left
474 sibling.
476 If the value returned by this function is non-nil, resizing and deleting
477 WINDOW may resize all windows in the same combination. */)
478 (Lisp_Object window)
480 return decode_any_window (window)->splits;
483 DEFUN ("set-window-splits", Fset_window_splits, Sset_window_splits, 2, 2, 0,
484 doc: /* Set splits status of WINDOW to STATUS.
485 WINDOW can be any window and defaults to the selected one. Return
486 STATUS.
488 If STATUS is nil and WINDOW is later resized, the corresponding space is
489 preferably taken from (or given to) WINDOW's right sibling. When WINDOW
490 is deleted, its space is given to its left sibling.
492 If STATUS is non-nil, resizing and deleting WINDOW may resize all
493 windows in the same combination. */)
494 (Lisp_Object window, Lisp_Object status)
496 register struct window *w = decode_any_window (window);
498 w->splits = status;
500 return w->splits;
503 DEFUN ("window-nest", Fwindow_nest, Swindow_nest, 0, 1, 0,
504 doc: /* Return nest status of WINDOW.
505 WINDOW can be any window and defaults to the selected one.
507 If the return value is nil, subwindows of WINDOW can be recombined with
508 WINDOW's siblings. A return value of non-nil means that subwindows of
509 WINDOW are never \(re-)combined with WINDOW's siblings. */)
510 (Lisp_Object window)
512 return decode_any_window (window)->nest;
515 DEFUN ("set-window-nest", Fset_window_nest, Sset_window_nest, 2, 2, 0,
516 doc: /* Set nest status of WINDOW to STATUS.
517 WINDOW can be any window and defaults to the selected one. Return
518 STATUS.
520 If STATUS is nil, subwindows of WINDOW can be recombined with WINDOW's
521 siblings. STATUS non-nil means that subwindows of WINDOW are never
522 \(re-)combined with WINDOW's siblings. */)
523 (Lisp_Object window, Lisp_Object status)
525 register struct window *w = decode_any_window (window);
527 w->nest = status;
529 return w->nest;
532 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
533 doc: /* Return WINDOW's use time.
534 WINDOW defaults to the selected window. The window with the highest use
535 time is the most recently selected one. The window with the lowest use
536 time is the least recently selected one. */)
537 (Lisp_Object window)
539 return decode_window (window)->use_time;
542 DEFUN ("window-total-size", Fwindow_total_size, Swindow_total_size, 0, 2, 0,
543 doc: /* Return the total number of lines of WINDOW.
544 WINDOW can be any window and defaults to the selected one. The return
545 value includes WINDOW's mode line and header line, if any. If WINDOW
546 is internal, the return value is the sum of the total number of lines
547 of WINDOW's child windows if these are vertically combined and the
548 height of WINDOW's first child otherwise.
550 Optional argument HORIZONTAL non-nil means return the total number of
551 columns of WINDOW. In this case the return value includes any vertical
552 dividers or scrollbars of WINDOW. If WINDOW is internal, the return
553 value is the sum of the total number of columns of WINDOW's child
554 windows if they are horizontally combined and the width of WINDOW's
555 first child otherwise. */)
556 (Lisp_Object window, Lisp_Object horizontal)
558 if (NILP (horizontal))
559 return decode_any_window (window)->total_lines;
560 else
561 return decode_any_window (window)->total_cols;
564 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
565 doc: /* Return new total size of WINDOW.
566 WINDOW defaults to the selected window. */)
567 (Lisp_Object window)
569 return decode_any_window (window)->new_total;
572 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
573 doc: /* Return normal height of WINDOW.
574 WINDOW can be any window and defaults to the selected one. Optional
575 argument HORIZONTAL non-nil means return normal width of WINDOW. */)
576 (Lisp_Object window, Lisp_Object horizontal)
578 if (NILP (horizontal))
579 return decode_any_window (window)->normal_lines;
580 else
581 return decode_any_window (window)->normal_cols;
584 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
585 doc: /* Return new normal size of WINDOW.
586 WINDOW can be any window and defaults to the selected one. */)
587 (Lisp_Object window)
589 return decode_any_window (window)->new_normal;
592 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
593 doc: /* Return left column of WINDOW.
594 WINDOW can be any window and defaults to the selected one. */)
595 (Lisp_Object window)
597 return decode_any_window (window)->left_col;
600 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
601 doc: /* Return top line of WINDOW.
602 WINDOW can be any window and defaults to the selected one. */)
603 (Lisp_Object window)
605 return decode_any_window (window)->top_line;
608 /* Return the number of lines of W's body. Don't count any mode or
609 header line of W. */
611 static int
612 window_body_lines (struct window *w)
614 int height = XFASTINT (w->total_lines);
616 if (!MINI_WINDOW_P (w))
618 if (WINDOW_WANTS_MODELINE_P (w))
619 --height;
620 if (WINDOW_WANTS_HEADER_LINE_P (w))
621 --height;
624 return height;
627 /* Return the number of columns of W's body. Don't count columns
628 occupied by the scroll bar or the vertical bar separating W from its
629 right sibling. On window-systems don't count fringes or display
630 margins either. */
633 window_body_cols (struct window *w)
635 struct frame *f = XFRAME (WINDOW_FRAME (w));
636 int width = XINT (w->total_cols);
638 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
639 /* Scroll bars occupy a few columns. */
640 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
641 else if (!FRAME_WINDOW_P (f)
642 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
643 /* The column of `|' characters separating side-by-side windows
644 occupies one column only. */
645 width -= 1;
647 if (FRAME_WINDOW_P (f))
648 /* On window-systems, fringes and display margins cannot be
649 used for normal text. */
650 width -= (WINDOW_FRINGE_COLS (w)
651 + WINDOW_LEFT_MARGIN_COLS (w)
652 + WINDOW_RIGHT_MARGIN_COLS (w));
654 return width;
657 DEFUN ("window-body-size", Fwindow_body_size, Swindow_body_size, 0, 2, 0,
658 doc: /* Return the number of lines of WINDOW's body.
659 WINDOW must be a live window and defaults to the selected one. The
660 return value does not include WINDOW's mode line and header line, if
661 any.
663 Optional argument HORIZONTAL non-nil means return the number of columns
664 of WINDOW's body. In this case, the return value does not include any
665 vertical dividers or scroll bars owned by WINDOW. On a window-system
666 the return value does not include the number of columns used for
667 WINDOW's fringes or display margins either. */)
668 (Lisp_Object window, Lisp_Object horizontal)
670 struct window *w = decode_any_window (window);
672 if (NILP (horizontal))
673 return make_number (window_body_lines (w));
674 else
675 return make_number (window_body_cols (w));
678 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
679 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
680 WINDOW must be a live window and defaults to the selected one. */)
681 (Lisp_Object window)
683 return decode_window (window)->hscroll;
686 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
687 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
688 If WINDOW is nil, the selected window is used.
689 Return NCOL. NCOL should be zero or positive.
691 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
692 window so that the location of point moves off-window. */)
693 (Lisp_Object window, Lisp_Object ncol)
695 struct window *w = decode_window (window);
696 int hscroll;
698 CHECK_NUMBER (ncol);
699 hscroll = max (0, XINT (ncol));
701 /* Prevent redisplay shortcuts when changing the hscroll. */
702 if (XINT (w->hscroll) != hscroll)
703 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
705 w->hscroll = make_number (hscroll);
706 return ncol;
709 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
710 Swindow_redisplay_end_trigger, 0, 1, 0,
711 doc: /* Return WINDOW's redisplay end trigger value.
712 WINDOW defaults to the selected window.
713 See `set-window-redisplay-end-trigger' for more information. */)
714 (Lisp_Object window)
716 return decode_window (window)->redisplay_end_trigger;
719 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
720 Sset_window_redisplay_end_trigger, 2, 2, 0,
721 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
722 VALUE should be a buffer position (typically a marker) or nil.
723 If it is a buffer position, then if redisplay in WINDOW reaches a position
724 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
725 with two arguments: WINDOW, and the end trigger value.
726 Afterwards the end-trigger value is reset to nil. */)
727 (register Lisp_Object window, Lisp_Object value)
729 register struct window *w;
731 w = decode_window (window);
732 w->redisplay_end_trigger = value;
733 return value;
736 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
737 doc: /* Return a list of the edge coordinates of WINDOW.
738 The list has the form (LEFT TOP RIGHT BOTTOM).
739 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
740 all relative to 0, 0 at top left corner of frame.
742 RIGHT is one more than the rightmost column occupied by WINDOW.
743 BOTTOM is one more than the bottommost row occupied by WINDOW.
744 The edges include the space used by WINDOW's scroll bar, display
745 margins, fringes, header line, and/or mode line. For the edges of
746 just the text area, use `window-inside-edges'. */)
747 (Lisp_Object window)
749 register struct window *w = decode_any_window (window);
751 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
752 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
753 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
754 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
755 Qnil))));
758 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
759 doc: /* Return a list of the edge pixel coordinates of WINDOW.
760 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
761 the top left corner of the frame.
763 RIGHT is one more than the rightmost x position occupied by WINDOW.
764 BOTTOM is one more than the bottommost y position occupied by WINDOW.
765 The pixel edges include the space used by WINDOW's scroll bar, display
766 margins, fringes, header line, and/or mode line. For the pixel edges
767 of just the text area, use `window-inside-pixel-edges'. */)
768 (Lisp_Object window)
770 register struct window *w = decode_any_window (window);
772 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
773 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
774 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
775 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
776 Qnil))));
779 static void
780 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
782 struct frame *f = XFRAME (w->frame);
783 *add_y = f->top_pos;
784 #ifdef FRAME_MENUBAR_HEIGHT
785 *add_y += FRAME_MENUBAR_HEIGHT (f);
786 #endif
787 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
788 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
789 #elif FRAME_TOOLBAR_HEIGHT
790 *add_y += FRAME_TOOLBAR_HEIGHT (f);
791 #endif
792 #ifdef FRAME_NS_TITLEBAR_HEIGHT
793 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
794 #endif
795 *add_x = f->left_pos;
796 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
797 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
798 #endif
801 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
802 Swindow_absolute_pixel_edges, 0, 1, 0,
803 doc: /* Return a list of the edge pixel coordinates of WINDOW.
804 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
805 the top left corner of the display.
807 RIGHT is one more than the rightmost x position occupied by WINDOW.
808 BOTTOM is one more than the bottommost y position occupied by WINDOW.
809 The pixel edges include the space used by WINDOW's scroll bar, display
810 margins, fringes, header line, and/or mode line. For the pixel edges
811 of just the text area, use `window-inside-absolute-pixel-edges'. */)
812 (Lisp_Object window)
814 register struct window *w = decode_any_window (window);
815 int add_x, add_y;
816 calc_absolute_offset (w, &add_x, &add_y);
818 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
819 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
820 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
821 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
822 Qnil))));
825 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
826 doc: /* Return a list of the edge coordinates of WINDOW.
827 The list has the form (LEFT TOP RIGHT BOTTOM).
828 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
829 all relative to 0, 0 at top left corner of frame.
831 RIGHT is one more than the rightmost column of WINDOW's text area.
832 BOTTOM is one more than the bottommost row of WINDOW's text area.
833 The inside edges do not include the space used by the WINDOW's scroll
834 bar, display margins, fringes, header line, and/or mode line. */)
835 (Lisp_Object window)
837 register struct window *w = decode_any_window (window);
839 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
840 + WINDOW_LEFT_MARGIN_COLS (w)
841 + WINDOW_LEFT_FRINGE_COLS (w)),
842 make_number (WINDOW_TOP_EDGE_LINE (w)
843 + WINDOW_HEADER_LINE_LINES (w)),
844 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
845 - WINDOW_RIGHT_MARGIN_COLS (w)
846 - WINDOW_RIGHT_FRINGE_COLS (w)),
847 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
848 - WINDOW_MODE_LINE_LINES (w)));
851 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
852 doc: /* Return a list of the edge pixel coordinates of WINDOW.
853 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
854 the top left corner of the frame.
856 RIGHT is one more than the rightmost x position of WINDOW's text area.
857 BOTTOM is one more than the bottommost y position of WINDOW's text area.
858 The inside edges do not include the space used by WINDOW's scroll bar,
859 display margins, fringes, header line, and/or mode line. */)
860 (Lisp_Object window)
862 register struct window *w = decode_any_window (window);
864 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
865 + WINDOW_LEFT_MARGIN_WIDTH (w)
866 + WINDOW_LEFT_FRINGE_WIDTH (w)),
867 make_number (WINDOW_TOP_EDGE_Y (w)
868 + WINDOW_HEADER_LINE_HEIGHT (w)),
869 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
870 - WINDOW_RIGHT_MARGIN_WIDTH (w)
871 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
872 make_number (WINDOW_BOTTOM_EDGE_Y (w)
873 - WINDOW_MODE_LINE_HEIGHT (w)));
876 DEFUN ("window-inside-absolute-pixel-edges",
877 Fwindow_inside_absolute_pixel_edges,
878 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
879 doc: /* Return a list of the edge pixel coordinates of WINDOW.
880 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
881 the top left corner of the display.
883 RIGHT is one more than the rightmost x position of WINDOW's text area.
884 BOTTOM is one more than the bottommost y position of WINDOW's text area.
885 The inside edges do not include the space used by WINDOW's scroll bar,
886 display margins, fringes, header line, and/or mode line. */)
887 (Lisp_Object window)
889 register struct window *w = decode_any_window (window);
890 int add_x, add_y;
891 calc_absolute_offset (w, &add_x, &add_y);
893 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
894 + WINDOW_LEFT_MARGIN_WIDTH (w)
895 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
896 make_number (WINDOW_TOP_EDGE_Y (w)
897 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
898 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
899 - WINDOW_RIGHT_MARGIN_WIDTH (w)
900 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
901 make_number (WINDOW_BOTTOM_EDGE_Y (w)
902 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
905 /* Test if the character at column X, row Y is within window W.
906 If it is not, return ON_NOTHING;
907 if it is in the window's text area, return ON_TEXT;
908 if it is on the window's modeline, return ON_MODE_LINE;
909 if it is on the border between the window and its right sibling,
910 return ON_VERTICAL_BORDER.
911 if it is on a scroll bar, return ON_SCROLL_BAR.
912 if it is on the window's top line, return ON_HEADER_LINE;
913 if it is in left or right fringe of the window,
914 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
915 if it is in the marginal area to the left/right of the window,
916 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
918 X and Y are frame relative pixel coordinates. */
920 static enum window_part
921 coordinates_in_window (register struct window *w, int x, int y)
923 struct frame *f = XFRAME (WINDOW_FRAME (w));
924 int left_x, right_x;
925 enum window_part part;
926 int ux = FRAME_COLUMN_WIDTH (f);
927 int x0 = WINDOW_LEFT_EDGE_X (w);
928 int x1 = WINDOW_RIGHT_EDGE_X (w);
929 /* The width of the area where the vertical line can be dragged.
930 (Between mode lines for instance. */
931 int grabbable_width = ux;
932 int lmargin_width, rmargin_width, text_left, text_right;
933 int top_y = WINDOW_TOP_EDGE_Y (w);
934 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
936 /* Outside any interesting row? */
937 if (y < top_y || y >= bottom_y)
938 return ON_NOTHING;
940 /* In what's below, we subtract 1 when computing right_x because we
941 want the rightmost pixel, which is given by left_pixel+width-1. */
942 if (w->pseudo_window_p)
944 left_x = 0;
945 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
947 else
949 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
950 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
953 /* On the mode line or header line? If it's near the start of
954 the mode or header line of window that's has a horizontal
955 sibling, say it's on the vertical line. That's to be able
956 to resize windows horizontally in case we're using toolkit
957 scroll bars. */
959 if (WINDOW_WANTS_MODELINE_P (w)
960 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
962 part = ON_MODE_LINE;
964 header_vertical_border_check:
965 /* We're somewhere on the mode line. We consider the place
966 between mode lines of horizontally adjacent mode lines
967 as the vertical border. If scroll bars on the left,
968 return the right window. */
969 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
970 || WINDOW_RIGHTMOST_P (w))
971 && !WINDOW_LEFTMOST_P (w)
972 && eabs (x - x0) < grabbable_width)
973 return ON_VERTICAL_BORDER;
975 /* Make sure we're not at the rightmost position of a
976 mode-/header-line and there's yet another window on the
977 right. (Bug#1372) */
978 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
979 && eabs (x - x1) < grabbable_width)
980 return ON_VERTICAL_BORDER;
982 if (x < x0 || x >= x1)
983 return ON_NOTHING;
985 return part;
988 if (WINDOW_WANTS_HEADER_LINE_P (w)
989 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
991 part = ON_HEADER_LINE;
992 goto header_vertical_border_check;
995 if (x < x0 || x >= x1) return ON_NOTHING;
997 /* Outside any interesting column? */
998 if (x < left_x || x > right_x)
999 return ON_SCROLL_BAR;
1001 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1002 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1004 text_left = window_box_left (w, TEXT_AREA);
1005 text_right = text_left + window_box_width (w, TEXT_AREA);
1007 if (FRAME_WINDOW_P (f))
1009 if (!w->pseudo_window_p
1010 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1011 && !WINDOW_RIGHTMOST_P (w)
1012 && (eabs (x - right_x) < grabbable_width))
1013 return ON_VERTICAL_BORDER;
1015 /* Need to say "x > right_x" rather than >=, since on character
1016 terminals, the vertical line's x coordinate is right_x. */
1017 else if (!w->pseudo_window_p
1018 && !WINDOW_RIGHTMOST_P (w)
1019 && x > right_x - ux)
1020 return ON_VERTICAL_BORDER;
1022 if (x < text_left)
1024 if (lmargin_width > 0
1025 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1026 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1027 : (x < left_x + lmargin_width)))
1028 return ON_LEFT_MARGIN;
1030 return ON_LEFT_FRINGE;
1033 if (x >= text_right)
1035 if (rmargin_width > 0
1036 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1037 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1038 : (x >= right_x - rmargin_width)))
1039 return ON_RIGHT_MARGIN;
1041 return ON_RIGHT_FRINGE;
1044 /* Everything special ruled out - must be on text area */
1045 return ON_TEXT;
1048 /* Take X is the frame-relative pixel x-coordinate, and return the
1049 x-coordinate relative to part PART of window W. */
1051 window_relative_x_coord (struct window *w, enum window_part part, int x)
1053 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1055 switch (part)
1057 case ON_TEXT:
1058 return x - window_box_left (w, TEXT_AREA);
1060 case ON_LEFT_FRINGE:
1061 return x - left_x;
1063 case ON_RIGHT_FRINGE:
1064 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1066 case ON_LEFT_MARGIN:
1067 return (x - left_x
1068 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1069 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1071 case ON_RIGHT_MARGIN:
1072 return (x + 1
1073 - ((w->pseudo_window_p)
1074 ? WINDOW_TOTAL_WIDTH (w)
1075 : WINDOW_BOX_RIGHT_EDGE_X (w))
1076 + window_box_width (w, RIGHT_MARGIN_AREA)
1077 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1078 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1081 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1082 return 0;
1086 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1087 Scoordinates_in_window_p, 2, 2, 0,
1088 doc: /* Return non-nil if COORDINATES are in WINDOW.
1089 COORDINATES is a cons of the form (X . Y), X and Y being distances
1090 measured in characters from the upper-left corner of the frame.
1091 \(0 . 0) denotes the character in the upper left corner of the
1092 frame.
1093 If COORDINATES are in the text portion of WINDOW,
1094 the coordinates relative to the window are returned.
1095 If they are in the mode line of WINDOW, `mode-line' is returned.
1096 If they are in the top mode line of WINDOW, `header-line' is returned.
1097 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1098 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1099 If they are on the border between WINDOW and its right sibling,
1100 `vertical-line' is returned.
1101 If they are in the windows's left or right marginal areas, `left-margin'\n\
1102 or `right-margin' is returned. */)
1103 (register Lisp_Object coordinates, Lisp_Object window)
1105 struct window *w;
1106 struct frame *f;
1107 int x, y;
1108 Lisp_Object lx, ly;
1110 CHECK_WINDOW (window);
1111 w = XWINDOW (window);
1112 f = XFRAME (w->frame);
1113 CHECK_CONS (coordinates);
1114 lx = Fcar (coordinates);
1115 ly = Fcdr (coordinates);
1116 CHECK_NUMBER_OR_FLOAT (lx);
1117 CHECK_NUMBER_OR_FLOAT (ly);
1118 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1119 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1121 switch (coordinates_in_window (w, x, y))
1123 case ON_NOTHING:
1124 return Qnil;
1126 case ON_TEXT:
1127 /* Convert X and Y to window relative pixel coordinates, and
1128 return the canonical char units. */
1129 x -= window_box_left (w, TEXT_AREA);
1130 y -= WINDOW_TOP_EDGE_Y (w);
1131 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1132 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1134 case ON_MODE_LINE:
1135 return Qmode_line;
1137 case ON_VERTICAL_BORDER:
1138 return Qvertical_line;
1140 case ON_HEADER_LINE:
1141 return Qheader_line;
1143 case ON_LEFT_FRINGE:
1144 return Qleft_fringe;
1146 case ON_RIGHT_FRINGE:
1147 return Qright_fringe;
1149 case ON_LEFT_MARGIN:
1150 return Qleft_margin;
1152 case ON_RIGHT_MARGIN:
1153 return Qright_margin;
1155 case ON_SCROLL_BAR:
1156 /* Historically we are supposed to return nil in this case. */
1157 return Qnil;
1159 default:
1160 abort ();
1165 /* Callback for foreach_window, used in window_from_coordinates.
1166 Check if window W contains coordinates specified by USER_DATA which
1167 is actually a pointer to a struct check_window_data CW.
1169 Check if window W contains coordinates *CW->x and *CW->y. If it
1170 does, return W in *CW->window, as Lisp_Object, and return in
1171 *CW->part the part of the window under coordinates *X,*Y. Return
1172 zero from this function to stop iterating over windows. */
1174 struct check_window_data
1176 Lisp_Object *window;
1177 int x, y;
1178 enum window_part *part;
1181 static int
1182 check_window_containing (struct window *w, void *user_data)
1184 struct check_window_data *cw = (struct check_window_data *) user_data;
1185 enum window_part found;
1186 int continue_p = 1;
1188 found = coordinates_in_window (w, cw->x, cw->y);
1189 if (found != ON_NOTHING)
1191 *cw->part = found;
1192 XSETWINDOW (*cw->window, w);
1193 continue_p = 0;
1196 return continue_p;
1200 /* Find the window containing frame-relative pixel position X/Y and
1201 return it as a Lisp_Object.
1203 If X, Y is on one of the window's special `window_part' elements,
1204 set *PART to the id of that element.
1206 If there is no window under X, Y return nil and leave *PART
1207 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1209 This function was previously implemented with a loop cycling over
1210 windows with Fnext_window, and starting with the frame's selected
1211 window. It turned out that this doesn't work with an
1212 implementation of next_window using Vwindow_list, because
1213 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1214 tree of F when this function is called asynchronously from
1215 note_mouse_highlight. The original loop didn't terminate in this
1216 case. */
1218 Lisp_Object
1219 window_from_coordinates (struct frame *f, int x, int y,
1220 enum window_part *part, int tool_bar_p)
1222 Lisp_Object window;
1223 struct check_window_data cw;
1224 enum window_part dummy;
1226 if (part == 0)
1227 part = &dummy;
1229 window = Qnil;
1230 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1231 foreach_window (f, check_window_containing, &cw);
1233 /* If not found above, see if it's in the tool bar window, if a tool
1234 bar exists. */
1235 if (NILP (window)
1236 && tool_bar_p
1237 && WINDOWP (f->tool_bar_window)
1238 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1239 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1240 != ON_NOTHING))
1242 *part = ON_TEXT;
1243 window = f->tool_bar_window;
1246 return window;
1249 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1250 doc: /* Return window containing coordinates X and Y on FRAME.
1251 FRAME must be a live frame and defaults to the selected one.
1252 The top left corner of the frame is considered to be row 0,
1253 column 0. */)
1254 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1256 struct frame *f;
1258 if (NILP (frame))
1259 frame = selected_frame;
1260 CHECK_LIVE_FRAME (frame);
1261 f = XFRAME (frame);
1263 /* Check that arguments are integers or floats. */
1264 CHECK_NUMBER_OR_FLOAT (x);
1265 CHECK_NUMBER_OR_FLOAT (y);
1267 return window_from_coordinates (f,
1268 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1269 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1270 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1271 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1272 0, 0);
1275 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1276 doc: /* Return current value of point in WINDOW.
1277 WINDOW must be a live window and defaults to the selected one.
1279 For a nonselected window, this is the value point would have
1280 if that window were selected.
1282 Note that, when WINDOW is the selected window and its buffer
1283 is also currently selected, the value returned is the same as (point).
1284 It would be more strictly correct to return the `top-level' value
1285 of point, outside of any save-excursion forms.
1286 But that is hard to define. */)
1287 (Lisp_Object window)
1289 register struct window *w = decode_window (window);
1291 if (w == XWINDOW (selected_window)
1292 && current_buffer == XBUFFER (w->buffer))
1293 return Fpoint ();
1294 return Fmarker_position (w->pointm);
1297 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1298 doc: /* Return position at which display currently starts in WINDOW.
1299 WINDOW must be a live window and defaults to the selected one.
1300 This is updated by redisplay or by calling `set-window-start'. */)
1301 (Lisp_Object window)
1303 return Fmarker_position (decode_window (window)->start);
1306 /* This is text temporarily removed from the doc string below.
1308 This function returns nil if the position is not currently known.
1309 That happens when redisplay is preempted and doesn't finish.
1310 If in that case you want to compute where the end of the window would
1311 have been if redisplay had finished, do this:
1312 (save-excursion
1313 (goto-char (window-start window))
1314 (vertical-motion (1- (window-height window)) window)
1315 (point))") */
1317 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1318 doc: /* Return position at which display currently ends in WINDOW.
1319 WINDOW must be a live window and defaults to the selected one.
1320 This is updated by redisplay, when it runs to completion.
1321 Simply changing the buffer text or setting `window-start'
1322 does not update this value.
1323 Return nil if there is no recorded value. \(This can happen if the
1324 last redisplay of WINDOW was preempted, and did not finish.)
1325 If UPDATE is non-nil, compute the up-to-date position
1326 if it isn't already recorded. */)
1327 (Lisp_Object window, Lisp_Object update)
1329 Lisp_Object value;
1330 struct window *w = decode_window (window);
1331 Lisp_Object buf;
1332 struct buffer *b;
1334 buf = w->buffer;
1335 CHECK_BUFFER (buf);
1336 b = XBUFFER (buf);
1338 #if 0 /* This change broke some things. We should make it later. */
1339 /* If we don't know the end position, return nil.
1340 The user can compute it with vertical-motion if he wants to.
1341 It would be nicer to do it automatically,
1342 but that's so slow that it would probably bother people. */
1343 if (NILP (w->window_end_valid))
1344 return Qnil;
1345 #endif
1347 if (! NILP (update)
1348 && ! (! NILP (w->window_end_valid)
1349 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1350 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1351 && !noninteractive)
1353 struct text_pos startp;
1354 struct it it;
1355 struct buffer *old_buffer = NULL;
1356 void *itdata = NULL;
1358 /* Cannot use Fvertical_motion because that function doesn't
1359 cope with variable-height lines. */
1360 if (b != current_buffer)
1362 old_buffer = current_buffer;
1363 set_buffer_internal (b);
1366 /* In case W->start is out of the range, use something
1367 reasonable. This situation occurred when loading a file with
1368 `-l' containing a call to `rmail' with subsequent other
1369 commands. At the end, W->start happened to be BEG, while
1370 rmail had already narrowed the buffer. */
1371 if (XMARKER (w->start)->charpos < BEGV)
1372 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1373 else if (XMARKER (w->start)->charpos > ZV)
1374 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1375 else
1376 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1378 itdata = bidi_shelve_cache ();
1379 start_display (&it, w, startp);
1380 move_it_vertically (&it, window_box_height (w));
1381 if (it.current_y < it.last_visible_y)
1382 move_it_past_eol (&it);
1383 value = make_number (IT_CHARPOS (it));
1384 bidi_unshelve_cache (itdata, 0);
1386 if (old_buffer)
1387 set_buffer_internal (old_buffer);
1389 else
1390 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1392 return value;
1395 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1396 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1397 Return POS. */)
1398 (Lisp_Object window, Lisp_Object pos)
1400 register struct window *w = decode_window (window);
1402 CHECK_NUMBER_COERCE_MARKER (pos);
1403 if (w == XWINDOW (selected_window)
1404 && XBUFFER (w->buffer) == current_buffer)
1405 Fgoto_char (pos);
1406 else
1407 set_marker_restricted (w->pointm, pos, w->buffer);
1409 /* We have to make sure that redisplay updates the window to show
1410 the new value of point. */
1411 if (!EQ (window, selected_window))
1412 ++windows_or_buffers_changed;
1414 return pos;
1417 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1418 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1419 If WINDOW is nil, the selected window is used. Return POS.
1420 Optional third arg NOFORCE non-nil inhibits next redisplay from
1421 overriding motion of point in order to display at this exact start. */)
1422 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1424 register struct window *w = decode_window (window);
1426 CHECK_NUMBER_COERCE_MARKER (pos);
1427 set_marker_restricted (w->start, pos, w->buffer);
1428 /* this is not right, but much easier than doing what is right. */
1429 w->start_at_line_beg = Qnil;
1430 if (NILP (noforce))
1431 w->force_start = Qt;
1432 w->update_mode_line = Qt;
1433 XSETFASTINT (w->last_modified, 0);
1434 XSETFASTINT (w->last_overlay_modified, 0);
1435 if (!EQ (window, selected_window))
1436 windows_or_buffers_changed++;
1438 return pos;
1441 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1442 Spos_visible_in_window_p, 0, 3, 0,
1443 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1444 Return nil if that position is scrolled vertically out of view.
1445 If a character is only partially visible, nil is returned, unless the
1446 optional argument PARTIALLY is non-nil.
1447 If POS is only out of view because of horizontal scrolling, return non-nil.
1448 If POS is t, it specifies the position of the last visible glyph in WINDOW.
1449 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
1451 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1452 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1453 where X and Y are the pixel coordinates relative to the top left corner
1454 of the window. The remaining elements are omitted if the character after
1455 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1456 off-window at the top and bottom of the row, ROWH is the height of the
1457 display row, and VPOS is the row number (0-based) containing POS. */)
1458 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1460 register struct window *w;
1461 register EMACS_INT posint;
1462 register struct buffer *buf;
1463 struct text_pos top;
1464 Lisp_Object in_window = Qnil;
1465 int rtop, rbot, rowh, vpos, fully_p = 1;
1466 int x, y;
1468 w = decode_window (window);
1469 buf = XBUFFER (w->buffer);
1470 SET_TEXT_POS_FROM_MARKER (top, w->start);
1472 if (EQ (pos, Qt))
1473 posint = -1;
1474 else if (!NILP (pos))
1476 CHECK_NUMBER_COERCE_MARKER (pos);
1477 posint = XINT (pos);
1479 else if (w == XWINDOW (selected_window))
1480 posint = PT;
1481 else
1482 posint = XMARKER (w->pointm)->charpos;
1484 /* If position is above window start or outside buffer boundaries,
1485 or if window start is out of range, position is not visible. */
1486 if ((EQ (pos, Qt)
1487 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1488 && CHARPOS (top) >= BUF_BEGV (buf)
1489 && CHARPOS (top) <= BUF_ZV (buf)
1490 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1491 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1492 in_window = Qt;
1494 if (!NILP (in_window) && !NILP (partially))
1496 Lisp_Object part = Qnil;
1497 if (!fully_p)
1498 part = list4 (make_number (rtop), make_number (rbot),
1499 make_number (rowh), make_number (vpos));
1500 in_window = Fcons (make_number (x),
1501 Fcons (make_number (y), part));
1504 return in_window;
1507 DEFUN ("window-line-height", Fwindow_line_height,
1508 Swindow_line_height, 0, 2, 0,
1509 doc: /* Return height in pixels of text line LINE in window WINDOW.
1510 WINDOW defaults to the selected window.
1512 Return height of current line if LINE is omitted or nil. Return height of
1513 header or mode line if LINE is `header-line' or `mode-line'.
1514 Otherwise, LINE is a text line number starting from 0. A negative number
1515 counts from the end of the window.
1517 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1518 in pixels of the visible part of the line, VPOS and YPOS are the
1519 vertical position in lines and pixels of the line, relative to the top
1520 of the first text line, and OFFBOT is the number of off-window pixels at
1521 the bottom of the text line. If there are off-window pixels at the top
1522 of the (first) text line, YPOS is negative.
1524 Return nil if window display is not up-to-date. In that case, use
1525 `pos-visible-in-window-p' to obtain the information. */)
1526 (Lisp_Object line, Lisp_Object window)
1528 register struct window *w;
1529 register struct buffer *b;
1530 struct glyph_row *row, *end_row;
1531 int max_y, crop, i, n;
1533 w = decode_window (window);
1535 if (noninteractive || w->pseudo_window_p)
1536 return Qnil;
1538 CHECK_BUFFER (w->buffer);
1539 b = XBUFFER (w->buffer);
1541 /* Fail if current matrix is not up-to-date. */
1542 if (NILP (w->window_end_valid)
1543 || current_buffer->clip_changed
1544 || current_buffer->prevent_redisplay_optimizations_p
1545 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
1546 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
1547 return Qnil;
1549 if (NILP (line))
1551 i = w->cursor.vpos;
1552 if (i < 0 || i >= w->current_matrix->nrows
1553 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1554 return Qnil;
1555 max_y = window_text_bottom_y (w);
1556 goto found_row;
1559 if (EQ (line, Qheader_line))
1561 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1562 return Qnil;
1563 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1564 if (!row->enabled_p)
1565 return Qnil;
1566 return list4 (make_number (row->height),
1567 make_number (0), make_number (0),
1568 make_number (0));
1571 if (EQ (line, Qmode_line))
1573 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1574 if (!row->enabled_p)
1575 return Qnil;
1576 return list4 (make_number (row->height),
1577 make_number (0), /* not accurate */
1578 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
1579 + window_text_bottom_y (w)),
1580 make_number (0));
1583 CHECK_NUMBER (line);
1584 n = XINT (line);
1586 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1587 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1588 max_y = window_text_bottom_y (w);
1589 i = 0;
1591 while ((n < 0 || i < n)
1592 && row <= end_row && row->enabled_p
1593 && row->y + row->height < max_y)
1594 row++, i++;
1596 if (row > end_row || !row->enabled_p)
1597 return Qnil;
1599 if (++n < 0)
1601 if (-n > i)
1602 return Qnil;
1603 row += n;
1604 i += n;
1607 found_row:
1608 crop = max (0, (row->y + row->height) - max_y);
1609 return list4 (make_number (row->height + min (0, row->y) - crop),
1610 make_number (i),
1611 make_number (row->y),
1612 make_number (crop));
1615 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1616 0, 1, 0,
1617 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1618 More precisely, return the value assigned by the last call of
1619 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1620 never called with WINDOW as its argument, or the value set by that
1621 function was internally reset since its last call. WINDOW defaults to
1622 the selected window.
1624 When a window is dedicated to its buffer, `display-buffer' will refrain
1625 from displaying another buffer in it. `get-lru-window' and
1626 `get-largest-window' treat dedicated windows specially.
1627 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1628 `kill-buffer' can delete a dedicated window and the containing frame.
1630 Functions like `set-window-buffer' may change the buffer displayed by a
1631 window, unless that window is "strongly" dedicated to its buffer, that
1632 is the value returned by `window-dedicated-p' is t. */)
1633 (Lisp_Object window)
1635 return decode_window (window)->dedicated;
1638 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1639 Sset_window_dedicated_p, 2, 2, 0,
1640 doc: /* Mark WINDOW as dedicated according to FLAG.
1641 WINDOW must be a live window and defaults to the selected one. FLAG
1642 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1643 mark WINDOW as non-dedicated. Return FLAG.
1645 When a window is dedicated to its buffer, `display-buffer' will refrain
1646 from displaying another buffer in it. `get-lru-window' and
1647 `get-largest-window' treat dedicated windows specially.
1648 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1649 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1650 and the containing frame.
1652 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1653 its buffer. Functions like `set-window-buffer' may change the buffer
1654 displayed by a window, unless that window is strongly dedicated to its
1655 buffer. If and when `set-window-buffer' displays another buffer in a
1656 window, it also makes sure that the window is no more dedicated. */)
1657 (Lisp_Object window, Lisp_Object flag)
1659 register struct window *w = decode_window (window);
1661 w->dedicated = flag;
1662 return w->dedicated;
1665 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1666 0, 1, 0,
1667 doc: /* Return buffers previously shown in WINDOW.
1668 WINDOW must be a live window and defaults to the selected one.
1670 The return value is either nil or a list of <buffer, window-start,
1671 window-point> triples where buffer was previously shown in WINDOW. */)
1672 (Lisp_Object window)
1674 return decode_window (window)->prev_buffers;
1677 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1678 Sset_window_prev_buffers, 2, 2, 0,
1679 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1680 WINDOW must be a live window and defaults to the selected one. Return
1681 PREV-BUFFERS.
1683 PREV-BUFFERS should be either nil or a list of <buffer, window-start,
1684 window-point> triples where buffer was previously shown in WINDOW. */)
1685 (Lisp_Object window, Lisp_Object prev_buffers)
1687 return decode_any_window (window)->prev_buffers = prev_buffers;
1690 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1691 0, 1, 0,
1692 doc: /* Return list of buffers recently re-shown in WINDOW.
1693 WINDOW must be a live window and defaults to the selected one. */)
1694 (Lisp_Object window)
1696 return decode_window (window)->next_buffers;
1699 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1700 Sset_window_next_buffers, 2, 2, 0,
1701 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1702 WINDOW must be a live window and defaults to the selected one. Return
1703 NEXT-BUFFERS.
1705 NEXT-BUFFERS should be either nil or a list of buffers that have been
1706 recently re-shown in WINDOW. */)
1707 (Lisp_Object window, Lisp_Object next_buffers)
1709 return decode_any_window (window)->next_buffers = next_buffers;
1712 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1713 0, 1, 0,
1714 doc: /* Return the parameters of WINDOW and their values.
1715 WINDOW defaults to the selected window. The return value is a list of
1716 elements of the form (PARAMETER . VALUE). */)
1717 (Lisp_Object window)
1719 return Fcopy_alist (decode_any_window (window)->window_parameters);
1722 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1723 2, 2, 0,
1724 doc: /* Return WINDOW's value for PARAMETER.
1725 WINDOW defaults to the selected window. */)
1726 (Lisp_Object window, Lisp_Object parameter)
1728 Lisp_Object result;
1730 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1731 return CDR_SAFE (result);
1734 DEFUN ("set-window-parameter", Fset_window_parameter,
1735 Sset_window_parameter, 3, 3, 0,
1736 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1737 WINDOW defaults to the selected window. Return VALUE. */)
1738 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1740 register struct window *w = decode_any_window (window);
1741 Lisp_Object old_alist_elt;
1743 old_alist_elt = Fassq (parameter, w->window_parameters);
1744 if (NILP (old_alist_elt))
1745 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1746 else
1747 Fsetcdr (old_alist_elt, value);
1748 return value;
1751 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1752 0, 1, 0,
1753 doc: /* Return the display-table that WINDOW is using.
1754 WINDOW defaults to the selected window. */)
1755 (Lisp_Object window)
1757 return decode_window (window)->display_table;
1760 /* Get the display table for use on window W. This is either W's
1761 display table or W's buffer's display table. Ignore the specified
1762 tables if they are not valid; if no valid table is specified,
1763 return 0. */
1765 struct Lisp_Char_Table *
1766 window_display_table (struct window *w)
1768 struct Lisp_Char_Table *dp = NULL;
1770 if (DISP_TABLE_P (w->display_table))
1771 dp = XCHAR_TABLE (w->display_table);
1772 else if (BUFFERP (w->buffer))
1774 struct buffer *b = XBUFFER (w->buffer);
1776 if (DISP_TABLE_P (BVAR (b, display_table)))
1777 dp = XCHAR_TABLE (BVAR (b, display_table));
1778 else if (DISP_TABLE_P (Vstandard_display_table))
1779 dp = XCHAR_TABLE (Vstandard_display_table);
1782 return dp;
1785 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1786 doc: /* Set WINDOW's display-table to TABLE. */)
1787 (register Lisp_Object window, Lisp_Object table)
1789 register struct window *w;
1791 w = decode_window (window);
1792 w->display_table = table;
1793 return table;
1796 /* Record info on buffer window W is displaying
1797 when it is about to cease to display that buffer. */
1798 static void
1799 unshow_buffer (register struct window *w)
1801 Lisp_Object buf;
1802 struct buffer *b;
1804 buf = w->buffer;
1805 b = XBUFFER (buf);
1806 if (b != XMARKER (w->pointm)->buffer)
1807 abort ();
1809 #if 0
1810 if (w == XWINDOW (selected_window)
1811 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1812 /* Do this except when the selected window's buffer
1813 is being removed from some other window. */
1814 #endif
1815 /* last_window_start records the start position that this buffer
1816 had in the last window to be disconnected from it.
1817 Now that this statement is unconditional,
1818 it is possible for the buffer to be displayed in the
1819 selected window, while last_window_start reflects another
1820 window which was recently showing the same buffer.
1821 Some people might say that might be a good thing. Let's see. */
1822 b->last_window_start = marker_position (w->start);
1824 /* Point in the selected window's buffer
1825 is actually stored in that buffer, and the window's pointm isn't used.
1826 So don't clobber point in that buffer. */
1827 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1828 /* This line helps to fix Horsley's testbug.el bug. */
1829 && !(WINDOWP (BVAR (b, last_selected_window))
1830 && w != XWINDOW (BVAR (b, last_selected_window))
1831 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1832 temp_set_point_both (b,
1833 clip_to_bounds (BUF_BEGV (b),
1834 XMARKER (w->pointm)->charpos,
1835 BUF_ZV (b)),
1836 clip_to_bounds (BUF_BEGV_BYTE (b),
1837 marker_byte_position (w->pointm),
1838 BUF_ZV_BYTE (b)));
1840 if (WINDOWP (BVAR (b, last_selected_window))
1841 && w == XWINDOW (BVAR (b, last_selected_window)))
1842 BVAR (b, last_selected_window) = Qnil;
1845 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1846 means change window structure only. Otherwise store geometry and
1847 other settings as well. */
1848 static void
1849 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1851 register Lisp_Object tem;
1852 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1854 /* If OLD is its frame's root window, then NEW is the new
1855 root window for that frame. */
1856 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1857 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = new;
1859 if (setflag)
1861 n->left_col = o->left_col;
1862 n->top_line = o->top_line;
1863 n->total_cols = o->total_cols;
1864 n->total_lines = o->total_lines;
1865 n->normal_cols = o->normal_cols;
1866 o->normal_cols = make_float (1.0);
1867 n->normal_lines = o->normal_lines;
1868 o->normal_lines = make_float (1.0);
1869 n->desired_matrix = n->current_matrix = 0;
1870 n->vscroll = 0;
1871 memset (&n->cursor, 0, sizeof (n->cursor));
1872 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
1873 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
1874 n->phys_cursor_type = -1;
1875 n->phys_cursor_width = -1;
1876 n->must_be_updated_p = 0;
1877 n->pseudo_window_p = 0;
1878 XSETFASTINT (n->window_end_vpos, 0);
1879 XSETFASTINT (n->window_end_pos, 0);
1880 n->window_end_valid = Qnil;
1881 n->frozen_window_start_p = 0;
1884 n->next = tem = o->next;
1885 if (!NILP (tem))
1886 XWINDOW (tem)->prev = new;
1888 n->prev = tem = o->prev;
1889 if (!NILP (tem))
1890 XWINDOW (tem)->next = new;
1892 n->parent = tem = o->parent;
1893 if (!NILP (tem))
1895 if (EQ (XWINDOW (tem)->vchild, old))
1896 XWINDOW (tem)->vchild = new;
1897 if (EQ (XWINDOW (tem)->hchild, old))
1898 XWINDOW (tem)->hchild = new;
1902 /* If window WINDOW and its parent window are iso-combined, merge
1903 WINDOW's children into those of its parent window and mark WINDOW as
1904 deleted. */
1906 static void
1907 recombine_windows (Lisp_Object window)
1909 struct window *w, *p, *c;
1910 Lisp_Object parent, child;
1911 int horflag;
1913 w = XWINDOW (window);
1914 parent = w->parent;
1915 if (!NILP (parent) && NILP (w->nest))
1917 p = XWINDOW (parent);
1918 if (((!NILP (p->vchild) && !NILP (w->vchild))
1919 || (!NILP (p->hchild) && !NILP (w->hchild))))
1920 /* WINDOW and PARENT are both either a vertical or a horizontal
1921 combination. */
1923 horflag = NILP (w->vchild);
1924 child = horflag ? w->hchild : w->vchild;
1925 c = XWINDOW (child);
1927 /* Splice WINDOW's children into its parent's children and
1928 assign new normal sizes. */
1929 if (NILP (w->prev))
1930 if (horflag)
1931 p->hchild = child;
1932 else
1933 p->vchild = child;
1934 else
1936 c->prev = w->prev;
1937 XWINDOW (w->prev)->next = child;
1940 while (c)
1942 c->parent = parent;
1944 if (horflag)
1945 c->normal_cols
1946 = make_float (XFLOATINT (c->total_cols)
1947 / XFLOATINT (p->total_cols));
1948 else
1949 c->normal_lines
1950 = make_float (XFLOATINT (c->total_lines)
1951 / XFLOATINT (p->total_lines));
1953 if (NILP (c->next))
1955 if (!NILP (w->next))
1957 c->next = w->next;
1958 XWINDOW (c->next)->prev = child;
1961 c = 0;
1963 else
1965 child = c->next;
1966 c = XWINDOW (child);
1970 /* WINDOW can be deleted now. */
1971 w->vchild = w->hchild = Qnil;
1976 /* If WINDOW can be deleted, delete it. */
1977 static void
1978 delete_deletable_window (Lisp_Object window)
1980 if (!NILP (call1 (Qwindow_deletable_p, window)))
1981 call1 (Qdelete_window, window);
1984 /***********************************************************************
1985 Window List
1986 ***********************************************************************/
1988 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1989 pointer. This is a callback function for foreach_window, used in
1990 the window_list function. */
1992 static int
1993 add_window_to_list (struct window *w, void *user_data)
1995 Lisp_Object *list = (Lisp_Object *) user_data;
1996 Lisp_Object window;
1997 XSETWINDOW (window, w);
1998 *list = Fcons (window, *list);
1999 return 1;
2003 /* Return a list of all windows, for use by next_window. If
2004 Vwindow_list is a list, return that list. Otherwise, build a new
2005 list, cache it in Vwindow_list, and return that. */
2007 static Lisp_Object
2008 window_list (void)
2010 if (!CONSP (Vwindow_list))
2012 Lisp_Object tail;
2014 Vwindow_list = Qnil;
2015 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
2017 Lisp_Object args[2];
2019 /* We are visiting windows in canonical order, and add
2020 new windows at the front of args[1], which means we
2021 have to reverse this list at the end. */
2022 args[1] = Qnil;
2023 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
2024 args[0] = Vwindow_list;
2025 args[1] = Fnreverse (args[1]);
2026 Vwindow_list = Fnconc (2, args);
2030 return Vwindow_list;
2034 /* Value is non-zero if WINDOW satisfies the constraints given by
2035 OWINDOW, MINIBUF and ALL_FRAMES.
2037 MINIBUF t means WINDOW may be minibuffer windows.
2038 `lambda' means WINDOW may not be a minibuffer window.
2039 a window means a specific minibuffer window
2041 ALL_FRAMES t means search all frames,
2042 nil means search just current frame,
2043 `visible' means search just visible frames on the
2044 current terminal,
2045 0 means search visible and iconified frames on the
2046 current terminal,
2047 a window means search the frame that window belongs to,
2048 a frame means consider windows on that frame, only. */
2050 static int
2051 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2053 struct window *w = XWINDOW (window);
2054 struct frame *f = XFRAME (w->frame);
2055 int candidate_p = 1;
2057 if (!BUFFERP (w->buffer))
2058 candidate_p = 0;
2059 else if (MINI_WINDOW_P (w)
2060 && (EQ (minibuf, Qlambda)
2061 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2063 /* If MINIBUF is `lambda' don't consider any mini-windows.
2064 If it is a window, consider only that one. */
2065 candidate_p = 0;
2067 else if (EQ (all_frames, Qt))
2068 candidate_p = 1;
2069 else if (NILP (all_frames))
2071 xassert (WINDOWP (owindow));
2072 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2074 else if (EQ (all_frames, Qvisible))
2076 FRAME_SAMPLE_VISIBILITY (f);
2077 candidate_p = FRAME_VISIBLE_P (f)
2078 && (FRAME_TERMINAL (XFRAME (w->frame))
2079 == FRAME_TERMINAL (XFRAME (selected_frame)));
2082 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2084 FRAME_SAMPLE_VISIBILITY (f);
2085 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2086 #ifdef HAVE_X_WINDOWS
2087 /* Yuck!! If we've just created the frame and the
2088 window-manager requested the user to place it
2089 manually, the window may still not be considered
2090 `visible'. I'd argue it should be at least
2091 something like `iconified', but don't know how to do
2092 that yet. --Stef */
2093 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2094 && !f->output_data.x->has_been_visible)
2095 #endif
2097 && (FRAME_TERMINAL (XFRAME (w->frame))
2098 == FRAME_TERMINAL (XFRAME (selected_frame)));
2100 else if (WINDOWP (all_frames))
2101 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2102 || EQ (XWINDOW (all_frames)->frame, w->frame)
2103 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2104 else if (FRAMEP (all_frames))
2105 candidate_p = EQ (all_frames, w->frame);
2107 return candidate_p;
2111 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2112 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2113 MINIBUF, and ALL_FRAMES. */
2115 static void
2116 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2118 if (NILP (*window))
2119 *window = selected_window;
2120 else
2121 CHECK_LIVE_WINDOW (*window);
2123 /* MINIBUF nil may or may not include minibuffers. Decide if it
2124 does. */
2125 if (NILP (*minibuf))
2126 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2127 else if (!EQ (*minibuf, Qt))
2128 *minibuf = Qlambda;
2130 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2131 => count none of them, or a specific minibuffer window (the
2132 active one) to count. */
2134 /* ALL_FRAMES nil doesn't specify which frames to include. */
2135 if (NILP (*all_frames))
2136 *all_frames = (!EQ (*minibuf, Qlambda)
2137 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
2138 : Qnil);
2139 else if (EQ (*all_frames, Qvisible))
2141 else if (EQ (*all_frames, make_number (0)))
2143 else if (FRAMEP (*all_frames))
2145 else if (!EQ (*all_frames, Qt))
2146 *all_frames = Qnil;
2150 /* Return the next or previous window of WINDOW in cyclic ordering
2151 of windows. NEXT_P non-zero means return the next window. See the
2152 documentation string of next-window for the meaning of MINIBUF and
2153 ALL_FRAMES. */
2155 static Lisp_Object
2156 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2158 decode_next_window_args (&window, &minibuf, &all_frames);
2160 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2161 return the first window on the frame. */
2162 if (FRAMEP (all_frames)
2163 && !EQ (all_frames, XWINDOW (window)->frame))
2164 return Fframe_first_window (all_frames);
2166 if (next_p)
2168 Lisp_Object list;
2170 /* Find WINDOW in the list of all windows. */
2171 list = Fmemq (window, window_list ());
2173 /* Scan forward from WINDOW to the end of the window list. */
2174 if (CONSP (list))
2175 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2176 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2177 break;
2179 /* Scan from the start of the window list up to WINDOW. */
2180 if (!CONSP (list))
2181 for (list = Vwindow_list;
2182 CONSP (list) && !EQ (XCAR (list), window);
2183 list = XCDR (list))
2184 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2185 break;
2187 if (CONSP (list))
2188 window = XCAR (list);
2190 else
2192 Lisp_Object candidate, list;
2194 /* Scan through the list of windows for candidates. If there are
2195 candidate windows in front of WINDOW, the last one of these
2196 is the one we want. If there are candidates following WINDOW
2197 in the list, again the last one of these is the one we want. */
2198 candidate = Qnil;
2199 for (list = window_list (); CONSP (list); list = XCDR (list))
2201 if (EQ (XCAR (list), window))
2203 if (WINDOWP (candidate))
2204 break;
2206 else if (candidate_window_p (XCAR (list), window, minibuf,
2207 all_frames))
2208 candidate = XCAR (list);
2211 if (WINDOWP (candidate))
2212 window = candidate;
2215 return window;
2219 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2220 doc: /* Return window following WINDOW in cyclic ordering of windows.
2221 WINDOW must be a live window and defaults to the selected one. The
2222 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2223 consider.
2225 MINIBUF nil or omitted means consider the minibuffer window only if the
2226 minibuffer is active. MINIBUF t means consider the minibuffer window
2227 even if the minibuffer is not active. Any other value means do not
2228 consider the minibuffer window even if the minibuffer is active.
2230 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2231 plus the minibuffer window if specified by the MINIBUF argument. If the
2232 minibuffer counts, consider all windows on all frames that share that
2233 minibuffer too. The following non-nil values of ALL-FRAMES have special
2234 meanings:
2236 - t means consider all windows on all existing frames.
2238 - `visible' means consider all windows on all visible frames.
2240 - 0 (the number zero) means consider all windows on all visible and
2241 iconified frames.
2243 - A frame means consider all windows on that frame only.
2245 Anything else means consider all windows on WINDOW's frame and no
2246 others.
2248 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2249 `next-window' to iterate through the entire cycle of acceptable
2250 windows, eventually ending up back at the window you started with.
2251 `previous-window' traverses the same cycle, in the reverse order. */)
2252 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2254 return next_window (window, minibuf, all_frames, 1);
2258 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2259 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2260 WINDOW must be a live window and defaults to the selected one. The
2261 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2262 consider.
2264 MINIBUF nil or omitted means consider the minibuffer window only if the
2265 minibuffer is active. MINIBUF t means consider the minibuffer window
2266 even if the minibuffer is not active. Any other value means do not
2267 consider the minibuffer window even if the minibuffer is active.
2269 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2270 plus the minibuffer window if specified by the MINIBUF argument. If the
2271 minibuffer counts, consider all windows on all frames that share that
2272 minibuffer too. The following non-nil values of ALL-FRAMES have special
2273 meanings:
2275 - t means consider all windows on all existing frames.
2277 - `visible' means consider all windows on all visible frames.
2279 - 0 (the number zero) means consider all windows on all visible and
2280 iconified frames.
2282 - A frame means consider all windows on that frame only.
2284 Anything else means consider all windows on WINDOW's frame and no
2285 others.
2287 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2288 use `previous-window' to iterate through the entire cycle of
2289 acceptable windows, eventually ending up back at the window you
2290 started with. `next-window' traverses the same cycle, in the
2291 reverse order. */)
2292 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2294 return next_window (window, minibuf, all_frames, 0);
2298 /* Return a list of windows in cyclic ordering. Arguments are like
2299 for `next-window'. */
2301 static Lisp_Object
2302 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2304 Lisp_Object tail, list, rest;
2306 decode_next_window_args (&window, &minibuf, &all_frames);
2307 list = Qnil;
2309 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2310 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2311 list = Fcons (XCAR (tail), list);
2313 /* Rotate the list to start with WINDOW. */
2314 list = Fnreverse (list);
2315 rest = Fmemq (window, list);
2316 if (!NILP (rest) && !EQ (rest, list))
2318 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2320 XSETCDR (tail, Qnil);
2321 list = nconc2 (rest, list);
2323 return list;
2327 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2328 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2329 FRAME nil or omitted means use the selected frame.
2330 WINDOW nil or omitted means use the selected window.
2331 MINIBUF t means include the minibuffer window, even if it isn't active.
2332 MINIBUF nil or omitted means include the minibuffer window only
2333 if it's active.
2334 MINIBUF neither nil nor t means never include the minibuffer window. */)
2335 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2337 if (NILP (window))
2338 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2339 CHECK_WINDOW (window);
2340 if (NILP (frame))
2341 frame = selected_frame;
2343 if (!EQ (frame, XWINDOW (window)->frame))
2344 error ("Window is on a different frame");
2346 return window_list_1 (window, minibuf, frame);
2350 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2351 doc: /* Return a list of all live windows.
2352 WINDOW specifies the first window to list and defaults to the selected
2353 window.
2355 Optional argument MINIBUF nil or omitted means consider the minibuffer
2356 window only if the minibuffer is active. MINIBUF t means consider the
2357 minibuffer window even if the minibuffer is not active. Any other value
2358 means do not consider the minibuffer window even if the minibuffer is
2359 active.
2361 Optional argument ALL-FRAMES nil or omitted means consider all windows
2362 on WINDOW's frame, plus the minibuffer window if specified by the
2363 MINIBUF argument. If the minibuffer counts, consider all windows on all
2364 frames that share that minibuffer too. The following non-nil values of
2365 ALL-FRAMES have special meanings:
2367 - t means consider all windows on all existing frames.
2369 - `visible' means consider all windows on all visible frames.
2371 - 0 (the number zero) means consider all windows on all visible and
2372 iconified frames.
2374 - A frame means consider all windows on that frame only.
2376 Anything else means consider all windows on WINDOW's frame and no
2377 others.
2379 If WINDOW is not on the list of windows returned, some other window will
2380 be listed first but no error is signalled. */)
2381 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2383 return window_list_1 (window, minibuf, all_frames);
2386 /* Look at all windows, performing an operation specified by TYPE
2387 with argument OBJ.
2388 If FRAMES is Qt, look at all frames;
2389 Qnil, look at just the selected frame;
2390 Qvisible, look at visible frames;
2391 a frame, just look at windows on that frame.
2392 If MINI is non-zero, perform the operation on minibuffer windows too. */
2394 enum window_loop
2396 WINDOW_LOOP_UNUSED,
2397 GET_BUFFER_WINDOW, /* Arg is buffer */
2398 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2399 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2400 CHECK_ALL_WINDOWS
2403 static Lisp_Object
2404 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2406 Lisp_Object window, windows, best_window, frame_arg;
2407 int frame_best_window_flag = 0;
2408 struct frame *f;
2409 struct gcpro gcpro1;
2411 /* If we're only looping through windows on a particular frame,
2412 frame points to that frame. If we're looping through windows
2413 on all frames, frame is 0. */
2414 if (FRAMEP (frames))
2415 f = XFRAME (frames);
2416 else if (NILP (frames))
2417 f = SELECTED_FRAME ();
2418 else
2419 f = NULL;
2421 if (f)
2422 frame_arg = Qlambda;
2423 else if (EQ (frames, make_number (0)))
2424 frame_arg = frames;
2425 else if (EQ (frames, Qvisible))
2426 frame_arg = frames;
2427 else
2428 frame_arg = Qt;
2430 /* frame_arg is Qlambda to stick to one frame,
2431 Qvisible to consider all visible frames,
2432 or Qt otherwise. */
2434 /* Pick a window to start with. */
2435 if (WINDOWP (obj))
2436 window = obj;
2437 else if (f)
2438 window = FRAME_SELECTED_WINDOW (f);
2439 else
2440 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2442 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2443 GCPRO1 (windows);
2444 best_window = Qnil;
2446 for (; CONSP (windows); windows = XCDR (windows))
2448 struct window *w;
2450 window = XCAR (windows);
2451 w = XWINDOW (window);
2453 /* Note that we do not pay attention here to whether the frame
2454 is visible, since Fwindow_list skips non-visible frames if
2455 that is desired, under the control of frame_arg. */
2456 if (!MINI_WINDOW_P (w)
2457 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2458 consider all windows. */
2459 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2460 || (mini && minibuf_level > 0))
2461 switch (type)
2463 case GET_BUFFER_WINDOW:
2464 if (EQ (w->buffer, obj)
2465 /* Don't find any minibuffer window except the one that
2466 is currently in use. */
2467 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2469 if (EQ (window, selected_window))
2470 /* Preferably return the selected window. */
2471 RETURN_UNGCPRO (window);
2472 else if (EQ (XWINDOW (window)->frame, selected_frame)
2473 && !frame_best_window_flag)
2474 /* Prefer windows on the current frame (but don't
2475 choose another one if we have one already). */
2477 best_window = window;
2478 frame_best_window_flag = 1;
2480 else if (NILP (best_window))
2481 best_window = window;
2483 break;
2485 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2486 /* We could simply check whether the buffer shown by window
2487 is live, and show another buffer in case it isn't. */
2488 if (EQ (w->buffer, obj))
2490 /* Undedicate WINDOW. */
2491 w->dedicated = Qnil;
2492 /* Make WINDOW show the buffer returned by
2493 other_buffer_safely, don't run any hooks. */
2494 set_window_buffer
2495 (window, other_buffer_safely (w->buffer), 0, 0);
2496 /* If WINDOW is the selected window, make its buffer
2497 current. But do so only if the window shows the
2498 current buffer (Bug#6454). */
2499 if (EQ (window, selected_window)
2500 && XBUFFER (w->buffer) == current_buffer)
2501 Fset_buffer (w->buffer);
2503 break;
2505 case REDISPLAY_BUFFER_WINDOWS:
2506 if (EQ (w->buffer, obj))
2508 mark_window_display_accurate (window, 0);
2509 w->update_mode_line = Qt;
2510 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2511 ++update_mode_lines;
2512 best_window = window;
2514 break;
2516 /* Check for a window that has a killed buffer. */
2517 case CHECK_ALL_WINDOWS:
2518 if (! NILP (w->buffer)
2519 && NILP (BVAR (XBUFFER (w->buffer), name)))
2520 abort ();
2521 break;
2523 case WINDOW_LOOP_UNUSED:
2524 break;
2528 UNGCPRO;
2529 return best_window;
2532 /* Used for debugging. Abort if any window has a dead buffer. */
2534 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2535 void
2536 check_all_windows (void)
2538 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2541 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2542 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2543 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2544 the current buffer.
2546 The optional argument ALL-FRAMES specifies the frames to consider:
2548 - t means consider all windows on all existing frames.
2550 - `visible' means consider all windows on all visible frames.
2552 - 0 (the number zero) means consider all windows on all visible
2553 and iconified frames.
2555 - A frame means consider all windows on that frame only.
2557 Any other value of ALL-FRAMES means consider all windows on the
2558 selected frame and no others. */)
2559 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2561 Lisp_Object buffer;
2563 if (NILP (buffer_or_name))
2564 buffer = Fcurrent_buffer ();
2565 else
2566 buffer = Fget_buffer (buffer_or_name);
2568 if (BUFFERP (buffer))
2569 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2570 else
2571 return Qnil;
2574 static Lisp_Object
2575 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2577 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2581 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2582 Sdelete_other_windows_internal, 0, 2, "",
2583 doc: /* Make WINDOW fill its frame.
2584 Only the frame WINDOW is on is affected. WINDOW may be any window and
2585 defaults to the selected one.
2587 Optional argument ROOT, if non-nil, must specify an internal window
2588 containing WINDOW as a subwindow. If this is the case, replace ROOT by
2589 WINDOW and leave alone any windows not contained in ROOT.
2591 When WINDOW is live try to reduce display jumps by keeping the text
2592 previously visible in WINDOW in the same place on the frame. Doing this
2593 depends on the value of (window-start WINDOW), so if calling this
2594 function in a program gives strange scrolling, make sure the
2595 window-start value is reasonable when this function is called. */)
2596 (Lisp_Object window, Lisp_Object root)
2598 struct window *w, *r, *s;
2599 struct frame *f;
2600 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2601 EMACS_INT startpos IF_LINT (= 0);
2602 int top IF_LINT (= 0), new_top, resize_failed;
2604 w = decode_any_window (window);
2605 XSETWINDOW (window, w);
2606 f = XFRAME (w->frame);
2608 if (NILP (root))
2609 /* ROOT is the frame's root window. */
2611 root = FRAME_ROOT_WINDOW (f);
2612 r = XWINDOW (root);
2614 else
2615 /* ROOT must be an ancestor of WINDOW. */
2617 r = decode_any_window (root);
2618 pwindow = XWINDOW (window)->parent;
2619 while (!NILP (pwindow))
2620 if (EQ (pwindow, root))
2621 break;
2622 else
2623 pwindow = XWINDOW (pwindow)->parent;
2624 if (!EQ (pwindow, root))
2625 error ("Specified root is not an ancestor of specified window");
2628 if (EQ (window, root))
2629 /* A noop. */
2630 return Qnil;
2631 /* I don't understand the "top > 0" part below. If we deal with a
2632 standalone minibuffer it would have been caught by the preceding
2633 test. */
2634 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2635 error ("Can't expand minibuffer to full frame");
2637 if (!NILP (w->buffer))
2639 startpos = marker_position (w->start);
2640 top = WINDOW_TOP_EDGE_LINE (w)
2641 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2642 /* Make sure WINDOW is the frame's selected window. */
2643 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2645 if (EQ (selected_frame, w->frame))
2646 Fselect_window (window, Qnil);
2647 else
2648 FRAME_SELECTED_WINDOW (f) = window;
2651 else
2653 /* See if the frame's selected window is a subwindow of WINDOW, by
2654 finding all the selected window's parents and comparing each
2655 one with WINDOW. If it isn't we need a new selected window for
2656 this frame. */
2657 swindow = FRAME_SELECTED_WINDOW (f);
2658 while (1)
2660 pwindow = swindow;
2661 while (!NILP (pwindow) && !EQ (window, pwindow))
2662 pwindow = XWINDOW (pwindow)->parent;
2664 if (EQ (window, pwindow))
2665 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2666 as the new selected window. */
2667 break;
2668 else
2669 /* Else try the previous window of SWINDOW. */
2670 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2673 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2675 if (EQ (selected_frame, w->frame))
2676 Fselect_window (swindow, Qnil);
2677 else
2678 FRAME_SELECTED_WINDOW (f) = swindow;
2682 BLOCK_INPUT;
2683 free_window_matrices (r);
2685 windows_or_buffers_changed++;
2686 Vwindow_list = Qnil;
2687 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2688 resize_failed = 0;
2690 if (NILP (w->buffer))
2692 /* Resize subwindows vertically. */
2693 XSETINT (delta, XINT (r->total_lines) - XINT (w->total_lines));
2694 w->top_line = r->top_line;
2695 resize_root_window (window, delta, Qnil, Qnil);
2696 if (window_resize_check (w, 0))
2697 window_resize_apply (w, 0);
2698 else
2700 resize_root_window (window, delta, Qnil, Qt);
2701 if (window_resize_check (w, 0))
2702 window_resize_apply (w, 0);
2703 else
2704 resize_failed = 1;
2707 /* Resize subwindows horizontally. */
2708 if (!resize_failed)
2710 w->left_col = r->left_col;
2711 XSETINT (delta, XINT (r->total_cols) - XINT (w->total_cols));
2712 w->left_col = r->left_col;
2713 resize_root_window (window, delta, Qt, Qnil);
2714 if (window_resize_check (w, 1))
2715 window_resize_apply (w, 1);
2716 else
2718 resize_root_window (window, delta, Qt, Qt);
2719 if (window_resize_check (w, 1))
2720 window_resize_apply (w, 1);
2721 else
2722 resize_failed = 1;
2726 if (resize_failed)
2727 /* Play safe, if we still can ... */
2729 window = swindow;
2730 w = XWINDOW (window);
2734 /* Cleanly unlink WINDOW from window-tree. */
2735 if (!NILP (w->prev))
2736 /* Get SIBLING above (on the left of) WINDOW. */
2738 sibling = w->prev;
2739 s = XWINDOW (sibling);
2740 s->next = w->next;
2741 if (!NILP (s->next))
2742 XWINDOW (s->next)->prev = sibling;
2744 else
2745 /* Get SIBLING below (on the right of) WINDOW. */
2747 sibling = w->next;
2748 s = XWINDOW (sibling);
2749 s->prev = Qnil;
2750 if (!NILP (XWINDOW (w->parent)->vchild))
2751 XWINDOW (w->parent)->vchild = sibling;
2752 else
2753 XWINDOW (w->parent)->hchild = sibling;
2756 /* Delete ROOT and all subwindows of ROOT. */
2757 if (!NILP (r->vchild))
2759 delete_all_subwindows (r->vchild);
2760 r->vchild = Qnil;
2762 else if (!NILP (r->hchild))
2764 delete_all_subwindows (r->hchild);
2765 r->hchild = Qnil;
2768 replace_window (root, window, 1);
2770 /* Reset WINDOW's splits status. */
2771 w->splits = Qnil;
2773 /* This must become SWINDOW anyway ....... */
2774 if (!NILP (w->buffer) && !resize_failed)
2776 /* Try to minimize scrolling, by setting the window start to the
2777 point will cause the text at the old window start to be at the
2778 same place on the frame. But don't try to do this if the
2779 window start is outside the visible portion (as might happen
2780 when the display is not current, due to typeahead). */
2781 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2782 if (new_top != top
2783 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2784 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2786 struct position pos;
2787 struct buffer *obuf = current_buffer;
2789 Fset_buffer (w->buffer);
2790 /* This computation used to temporarily move point, but that
2791 can have unwanted side effects due to text properties. */
2792 pos = *vmotion (startpos, -top, w);
2794 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2795 w->window_end_valid = Qnil;
2796 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2797 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2798 : Qnil);
2799 /* We need to do this, so that the window-scroll-functions
2800 get called. */
2801 w->optional_new_start = Qt;
2803 set_buffer_internal (obuf);
2807 adjust_glyphs (f);
2808 UNBLOCK_INPUT;
2810 run_window_configuration_change_hook (f);
2812 return Qnil;
2816 void
2817 replace_buffer_in_windows (Lisp_Object buffer)
2819 call1 (Qreplace_buffer_in_windows, buffer);
2823 /* Safely replace BUFFER with some other buffer in all windows of all
2824 frames, even those on other keyboards. */
2826 void
2827 replace_buffer_in_windows_safely (Lisp_Object buffer)
2829 Lisp_Object tail, frame;
2831 /* A single call to window_loop won't do the job because it only
2832 considers frames on the current keyboard. So loop manually over
2833 frames, and handle each one. */
2834 FOR_EACH_FRAME (tail, frame)
2835 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2838 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2839 minimum allowable size. */
2841 void
2842 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2844 /* For height, we have to see:
2845 how many windows the frame has at minimum (one or two),
2846 and whether it has a menu bar or other special stuff at the top. */
2847 int min_height
2848 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2849 ? MIN_SAFE_WINDOW_HEIGHT
2850 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2852 if (FRAME_TOP_MARGIN (frame) > 0)
2853 min_height += FRAME_TOP_MARGIN (frame);
2855 if (*rows < min_height)
2856 *rows = min_height;
2857 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2858 *cols = MIN_SAFE_WINDOW_WIDTH;
2861 /* Adjust the margins of window W if text area is too small.
2862 Return 1 if window width is ok after adjustment; 0 if window
2863 is still too narrow. */
2865 static int
2866 adjust_window_margins (struct window *w)
2868 int box_cols = (WINDOW_TOTAL_COLS (w)
2869 - WINDOW_FRINGE_COLS (w)
2870 - WINDOW_SCROLL_BAR_COLS (w));
2871 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2872 + WINDOW_RIGHT_MARGIN_COLS (w));
2874 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2875 return 1;
2877 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2878 return 0;
2880 /* Window's text area is too narrow, but reducing the window
2881 margins will fix that. */
2882 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2883 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2885 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2886 w->left_margin_cols = w->right_margin_cols
2887 = make_number (margin_cols/2);
2888 else
2889 w->right_margin_cols = make_number (margin_cols);
2891 else
2892 w->left_margin_cols = make_number (margin_cols);
2893 return 1;
2896 static Lisp_Object Fset_window_margins (Lisp_Object, Lisp_Object, Lisp_Object);
2897 static Lisp_Object Fset_window_fringes (Lisp_Object, Lisp_Object, Lisp_Object,
2898 Lisp_Object);
2899 static Lisp_Object Fset_window_scroll_bars (Lisp_Object, Lisp_Object,
2900 Lisp_Object, Lisp_Object);
2901 static Lisp_Object Fset_window_vscroll (Lisp_Object, Lisp_Object, Lisp_Object);
2903 /* The following three routines are needed for running a window's
2904 configuration change hook. */
2905 static void
2906 run_funs (Lisp_Object funs)
2908 for (; CONSP (funs); funs = XCDR (funs))
2909 if (!EQ (XCAR (funs), Qt))
2910 call0 (XCAR (funs));
2913 static Lisp_Object
2914 select_window_norecord (Lisp_Object window)
2916 return WINDOW_LIVE_P (window)
2917 ? Fselect_window (window, Qt) : selected_window;
2920 static Lisp_Object
2921 select_frame_norecord (Lisp_Object frame)
2923 return FRAME_LIVE_P (XFRAME (frame))
2924 ? Fselect_frame (frame, Qt) : selected_frame;
2927 void
2928 run_window_configuration_change_hook (struct frame *f)
2930 int count = SPECPDL_INDEX ();
2931 Lisp_Object frame, global_wcch
2932 = Fdefault_value (Qwindow_configuration_change_hook);
2933 XSETFRAME (frame, f);
2935 if (NILP (Vrun_hooks))
2936 return;
2938 /* Use the right buffer. Matters when running the local hooks. */
2939 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
2941 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2942 Fset_buffer (Fwindow_buffer (Qnil));
2945 if (SELECTED_FRAME () != f)
2947 record_unwind_protect (select_frame_norecord, Fselected_frame ());
2948 select_frame_norecord (frame);
2951 /* Look for buffer-local values. */
2953 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
2954 for (; CONSP (windows); windows = XCDR (windows))
2956 Lisp_Object window = XCAR (windows);
2957 Lisp_Object buffer = Fwindow_buffer (window);
2958 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
2959 buffer)))
2961 int inner_count = SPECPDL_INDEX ();
2962 record_unwind_protect (select_window_norecord, Fselected_window ());
2963 select_window_norecord (window);
2964 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
2965 buffer));
2966 unbind_to (inner_count, Qnil);
2971 run_funs (global_wcch);
2972 unbind_to (count, Qnil);
2975 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
2976 Srun_window_configuration_change_hook, 1, 1, 0,
2977 doc: /* Run `window-configuration-change-hook' for FRAME. */)
2978 (Lisp_Object frame)
2980 CHECK_LIVE_FRAME (frame);
2981 run_window_configuration_change_hook (XFRAME (frame));
2982 return Qnil;
2985 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2986 means it's allowed to run hooks. See make_frame for a case where
2987 it's not allowed. KEEP_MARGINS_P non-zero means that the current
2988 margins, fringes, and scroll-bar settings of the window are not
2989 reset from the buffer's local settings. */
2991 void
2992 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
2994 struct window *w = XWINDOW (window);
2995 struct buffer *b = XBUFFER (buffer);
2996 int count = SPECPDL_INDEX ();
2997 int samebuf = EQ (buffer, w->buffer);
2999 w->buffer = buffer;
3001 if (EQ (window, selected_window))
3002 BVAR (b, last_selected_window) = window;
3004 /* Let redisplay errors through. */
3005 b->display_error_modiff = 0;
3007 /* Update time stamps of buffer display. */
3008 if (INTEGERP (BVAR (b, display_count)))
3009 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
3010 BVAR (b, display_time) = Fcurrent_time ();
3012 XSETFASTINT (w->window_end_pos, 0);
3013 XSETFASTINT (w->window_end_vpos, 0);
3014 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3015 w->window_end_valid = Qnil;
3016 if (!(keep_margins_p && samebuf))
3017 { /* If we're not actually changing the buffer, don't reset hscroll and
3018 vscroll. This case happens for example when called from
3019 change_frame_size_1, where we use a dummy call to
3020 Fset_window_buffer on the frame's selected window (and no other)
3021 just in order to run window-configuration-change-hook.
3022 Resetting hscroll and vscroll here is problematic for things like
3023 image-mode and doc-view-mode since it resets the image's position
3024 whenever we resize the frame. */
3025 w->hscroll = w->min_hscroll = make_number (0);
3026 w->vscroll = 0;
3027 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3028 set_marker_restricted (w->start,
3029 make_number (b->last_window_start),
3030 buffer);
3031 w->start_at_line_beg = Qnil;
3032 w->force_start = Qnil;
3033 XSETFASTINT (w->last_modified, 0);
3034 XSETFASTINT (w->last_overlay_modified, 0);
3036 /* Maybe we could move this into the `if' but it's not obviously safe and
3037 I doubt it's worth the trouble. */
3038 windows_or_buffers_changed++;
3040 /* We must select BUFFER for running the window-scroll-functions. */
3041 /* We can't check ! NILP (Vwindow_scroll_functions) here
3042 because that might itself be a local variable. */
3043 if (window_initialized)
3045 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3046 Fset_buffer (buffer);
3049 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3051 if (!keep_margins_p)
3053 /* Set left and right marginal area width etc. from buffer. */
3055 /* This may call adjust_window_margins three times, so
3056 temporarily disable window margins. */
3057 Lisp_Object save_left = w->left_margin_cols;
3058 Lisp_Object save_right = w->right_margin_cols;
3060 w->left_margin_cols = w->right_margin_cols = Qnil;
3062 Fset_window_fringes (window,
3063 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3064 BVAR (b, fringes_outside_margins));
3066 Fset_window_scroll_bars (window,
3067 BVAR (b, scroll_bar_width),
3068 BVAR (b, vertical_scroll_bar_type), Qnil);
3070 w->left_margin_cols = save_left;
3071 w->right_margin_cols = save_right;
3073 Fset_window_margins (window,
3074 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3077 if (run_hooks_p)
3079 if (! NILP (Vwindow_scroll_functions))
3080 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3081 Fmarker_position (w->start));
3082 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3085 unbind_to (count, Qnil);
3088 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3089 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3090 WINDOW has to be a live window and defaults to the selected one.
3091 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3093 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3094 display margins, fringe widths, and scroll bar settings are preserved;
3095 the default is to reset these from the local settings for BUFFER-OR-NAME
3096 or the frame defaults. Return nil.
3098 This function throws an error when WINDOW is strongly dedicated to its
3099 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3100 already display BUFFER-OR-NAME.
3102 This function runs `window-scroll-functions' before running
3103 `window-configuration-change-hook'. */)
3104 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3106 register Lisp_Object tem, buffer;
3107 register struct window *w = decode_window (window);
3109 XSETWINDOW (window, w);
3110 buffer = Fget_buffer (buffer_or_name);
3111 CHECK_BUFFER (buffer);
3112 if (NILP (BVAR (XBUFFER (buffer), name)))
3113 error ("Attempt to display deleted buffer");
3115 tem = w->buffer;
3116 if (NILP (tem))
3117 error ("Window is deleted");
3118 else if (!EQ (tem, Qt))
3119 /* w->buffer is t when the window is first being set up. */
3121 if (!EQ (tem, buffer))
3123 if (EQ (w->dedicated, Qt))
3124 /* WINDOW is strongly dedicated to its buffer, signal an
3125 error. */
3126 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3127 else
3128 /* WINDOW is weakly dedicated to its buffer, reset
3129 dedicatedness. */
3130 w->dedicated = Qnil;
3132 call1 (Qrecord_window_buffer, window);
3135 unshow_buffer (w);
3138 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3140 return Qnil;
3143 static Lisp_Object
3144 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3146 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3149 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3150 0, 1, 0,
3151 doc: /* Force all windows to be updated on next redisplay.
3152 If optional arg OBJECT is a window, force redisplay of that window only.
3153 If OBJECT is a buffer or buffer name, force redisplay of all windows
3154 displaying that buffer. */)
3155 (Lisp_Object object)
3157 if (NILP (object))
3159 windows_or_buffers_changed++;
3160 update_mode_lines++;
3161 return Qt;
3164 if (WINDOWP (object))
3166 struct window *w = XWINDOW (object);
3167 mark_window_display_accurate (object, 0);
3168 w->update_mode_line = Qt;
3169 if (BUFFERP (w->buffer))
3170 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3171 ++update_mode_lines;
3172 return Qt;
3175 if (STRINGP (object))
3176 object = Fget_buffer (object);
3177 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3179 /* Walk all windows looking for buffer, and force update
3180 of each of those windows. */
3182 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3183 return NILP (object) ? Qnil : Qt;
3186 /* If nothing suitable was found, just return.
3187 We could signal an error, but this feature will typically be used
3188 asynchronously in timers or process sentinels, so we don't. */
3189 return Qnil;
3193 void
3194 temp_output_buffer_show (register Lisp_Object buf)
3196 register struct buffer *old = current_buffer;
3197 register Lisp_Object window;
3198 register struct window *w;
3200 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3202 Fset_buffer (buf);
3203 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3204 BEGV = BEG;
3205 ZV = Z;
3206 SET_PT (BEG);
3207 set_buffer_internal (old);
3209 if (!NILP (Vtemp_buffer_show_function))
3210 call1 (Vtemp_buffer_show_function, buf);
3211 else
3213 window = display_buffer (buf, Vtemp_buffer_show_specifiers, Qnil);
3214 /* Reset Vtemp_buffer_show_specifiers immediately so it won't
3215 affect subsequent calls. */
3216 Vtemp_buffer_show_specifiers = Qnil;
3218 if (!EQ (XWINDOW (window)->frame, selected_frame))
3219 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3220 Vminibuf_scroll_window = window;
3221 w = XWINDOW (window);
3222 XSETFASTINT (w->hscroll, 0);
3223 XSETFASTINT (w->min_hscroll, 0);
3224 set_marker_restricted_both (w->start, buf, BEG, BEG);
3225 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3227 /* Run temp-buffer-show-hook, with the chosen window selected
3228 and its buffer current. */
3230 int count = SPECPDL_INDEX ();
3231 Lisp_Object prev_window, prev_buffer;
3232 prev_window = selected_window;
3233 XSETBUFFER (prev_buffer, old);
3235 /* Select the window that was chosen, for running the hook.
3236 Note: Both Fselect_window and select_window_norecord may
3237 set-buffer to the buffer displayed in the window,
3238 so we need to save the current buffer. --stef */
3239 record_unwind_protect (Fset_buffer, prev_buffer);
3240 record_unwind_protect (select_window_norecord, prev_window);
3241 Fselect_window (window, Qt);
3242 Fset_buffer (w->buffer);
3243 Frun_hooks (1, &Qtemp_buffer_show_hook);
3244 unbind_to (count, Qnil);
3249 DEFUN ("internal-temp-output-buffer-show",
3250 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3251 1, 1, 0,
3252 doc: /* Internal function for `with-output-to-temp-buffer''. */)
3253 (Lisp_Object buf)
3255 temp_output_buffer_show (buf);
3256 return Qnil;
3259 /* Make new window, have it replace WINDOW in window-tree, and make
3260 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3261 horizontal child). */
3262 static void
3263 make_parent_window (Lisp_Object window, int horflag)
3265 Lisp_Object parent;
3266 register struct window *o, *p;
3267 int i;
3269 o = XWINDOW (window);
3270 p = allocate_window ();
3271 for (i = 0; i < VECSIZE (struct window); ++i)
3272 ((struct Lisp_Vector *) p)->contents[i]
3273 = ((struct Lisp_Vector *) o)->contents[i];
3274 XSETWINDOW (parent, p);
3276 ++sequence_number;
3277 XSETFASTINT (p->sequence_number, sequence_number);
3279 replace_window (window, parent, 1);
3281 o->next = Qnil;
3282 o->prev = Qnil;
3283 o->parent = parent;
3285 p->hchild = horflag ? window : Qnil;
3286 p->vchild = horflag ? Qnil : window;
3287 p->start = Qnil;
3288 p->pointm = Qnil;
3289 p->buffer = Qnil;
3290 p->splits = Qnil;
3291 p->nest = Qnil;
3292 p->window_parameters = Qnil;
3295 /* Make new window from scratch. */
3296 Lisp_Object
3297 make_window (void)
3299 Lisp_Object window;
3300 register struct window *w;
3302 w = allocate_window ();
3303 /* Initialize all Lisp data. */
3304 w->frame = w->mini_p = Qnil;
3305 w->next = w->prev = w->hchild = w->vchild = w->parent = Qnil;
3306 XSETFASTINT (w->left_col, 0);
3307 XSETFASTINT (w->top_line, 0);
3308 XSETFASTINT (w->total_lines, 0);
3309 XSETFASTINT (w->total_cols, 0);
3310 w->normal_lines = make_float (1.0);
3311 w->normal_cols = make_float (1.0);
3312 XSETFASTINT (w->new_total, 0);
3313 XSETFASTINT (w->new_normal, 0);
3314 w->buffer = Qnil;
3315 w->start = Fmake_marker ();
3316 w->pointm = Fmake_marker ();
3317 w->force_start = w->optional_new_start = Qnil;
3318 XSETFASTINT (w->hscroll, 0);
3319 XSETFASTINT (w->min_hscroll, 0);
3320 XSETFASTINT (w->use_time, 0);
3321 ++sequence_number;
3322 XSETFASTINT (w->sequence_number, sequence_number);
3323 w->temslot = w->last_modified = w->last_overlay_modified = Qnil;
3324 XSETFASTINT (w->last_point, 0);
3325 w->last_had_star = w->vertical_scroll_bar = Qnil;
3326 w->left_margin_cols = w->right_margin_cols = Qnil;
3327 w->left_fringe_width = w->right_fringe_width = Qnil;
3328 w->fringes_outside_margins = Qnil;
3329 w->scroll_bar_width = Qnil;
3330 w->vertical_scroll_bar_type = Qt;
3331 w->last_mark_x = w->last_mark_y = Qnil;
3332 XSETFASTINT (w->window_end_pos, 0);
3333 XSETFASTINT (w->window_end_vpos, 0);
3334 w->window_end_valid = w->update_mode_line = Qnil;
3335 w->start_at_line_beg = w->display_table = w->dedicated = Qnil;
3336 w->base_line_number = w->base_line_pos = w->region_showing = Qnil;
3337 w->column_number_displayed = w->redisplay_end_trigger = Qnil;
3338 w->splits = w->nest = w->window_parameters = Qnil;
3339 w->prev_buffers = w->next_buffers = Qnil;
3340 /* Initialize non-Lisp data. */
3341 w->desired_matrix = w->current_matrix = 0;
3342 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3343 memset (&w->cursor, 0, sizeof (w->cursor));
3344 memset (&w->last_cursor, 0, sizeof (w->last_cursor));
3345 memset (&w->phys_cursor, 0, sizeof (w->phys_cursor));
3346 w->phys_cursor_type = -1;
3347 w->phys_cursor_width = -1;
3348 w->last_cursor_off_p = w->cursor_off_p = 0;
3349 w->must_be_updated_p = 0;
3350 w->pseudo_window_p = 0;
3351 w->frozen_window_start_p = 0;
3352 w->vscroll = 0;
3353 /* Reset window_list. */
3354 Vwindow_list = Qnil;
3355 /* Return window. */
3356 XSETWINDOW (window, w);
3357 return window;
3360 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3361 doc: /* Set new total size of WINDOW to SIZE.
3362 Return SIZE.
3364 Optional argument ADD non-nil means add SIZE to the new total size of
3365 WINDOW and return the sum.
3367 Note: This function does not operate on any subwindows of WINDOW. */)
3368 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3370 struct window *w = decode_any_window (window);
3372 CHECK_NUMBER (size);
3373 if (NILP (add))
3374 XSETINT (w->new_total, XINT (size));
3375 else
3376 XSETINT (w->new_total, XINT (w->new_total) + XINT (size));
3378 return w->new_total;
3381 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3382 doc: /* Set new normal size of WINDOW to SIZE.
3383 Return SIZE.
3385 Note: This function does not operate on any subwindows of WINDOW. */)
3386 (Lisp_Object window, Lisp_Object size)
3388 struct window *w = decode_any_window (window);
3390 w->new_normal = size;
3391 return w->new_normal;
3394 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3395 non-zero) to w->new_total would result in correct heights (widths)
3396 for window W and recursively all subwindows of W.
3398 Note: This function does not check any of `window-fixed-size-p',
3399 `window-min-height' or `window-min-width'. It does check that window
3400 sizes do not drop below one line (two columns). */
3401 static int
3402 window_resize_check (struct window *w, int horflag)
3404 struct window *c;
3406 if (!NILP (w->vchild))
3407 /* W is a vertical combination. */
3409 c = XWINDOW (w->vchild);
3410 if (horflag)
3411 /* All subwindows of W must have the same width as W. */
3413 while (c)
3415 if ((XINT (c->new_total) != XINT (w->new_total))
3416 || !window_resize_check (c, horflag))
3417 return 0;
3418 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3420 return 1;
3422 else
3423 /* The sum of the heights of the subwindows of W must equal W's
3424 height. */
3426 int sum_of_sizes = 0;
3427 while (c)
3429 if (!window_resize_check (c, horflag))
3430 return 0;
3431 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3432 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3434 return (sum_of_sizes == XINT (w->new_total));
3437 else if (!NILP (w->hchild))
3438 /* W is a horizontal combination. */
3440 c = XWINDOW (w->hchild);
3441 if (horflag)
3442 /* The sum of the widths of the subwindows of W must equal W's
3443 width. */
3445 int sum_of_sizes = 0;
3446 while (c)
3448 if (!window_resize_check (c, horflag))
3449 return 0;
3450 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3451 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3453 return (sum_of_sizes == XINT (w->new_total));
3455 else
3456 /* All subwindows of W must have the same height as W. */
3458 while (c)
3460 if ((XINT (c->new_total) != XINT (w->new_total))
3461 || !window_resize_check (c, horflag))
3462 return 0;
3463 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3465 return 1;
3468 else
3469 /* A leaf window. Make sure it's not too small. The following
3470 hardcodes the values of `window-safe-min-width' (2) and
3471 `window-safe-min-height' (1) which are defined in window.el. */
3472 return XINT (w->new_total) >= (horflag ? 2 : 1);
3475 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3476 w->new_total for window W and recursively all subwindows of W. Also
3477 calculate and assign the new vertical (horizontal) start positions of
3478 each of these windows.
3480 This function does not perform any error checks. Make sure you have
3481 run window_resize_check on W before applying this function. */
3482 static void
3483 window_resize_apply (struct window *w, int horflag)
3485 struct window *c;
3486 int pos;
3488 /* Note: Assigning new_normal requires that the new total size of the
3489 parent window has been set *before*. */
3490 if (horflag)
3492 w->total_cols = w->new_total;
3493 if (NUMBERP (w->new_normal))
3494 w->normal_cols = w->new_normal;
3496 pos = XINT (w->left_col);
3498 else
3500 w->total_lines = w->new_total;
3501 if (NUMBERP (w->new_normal))
3502 w->normal_lines = w->new_normal;
3504 pos = XINT (w->top_line);
3507 if (!NILP (w->vchild))
3508 /* W is a vertical combination. */
3510 c = XWINDOW (w->vchild);
3511 while (c)
3513 if (horflag)
3514 XSETFASTINT (c->left_col, pos);
3515 else
3516 XSETFASTINT (c->top_line, pos);
3517 window_resize_apply (c, horflag);
3518 if (!horflag)
3519 pos = pos + XINT (c->total_lines);
3520 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3523 else if (!NILP (w->hchild))
3524 /* W is a horizontal combination. */
3526 c = XWINDOW (w->hchild);
3527 while (c)
3529 if (horflag)
3530 XSETFASTINT (c->left_col, pos);
3531 else
3532 XSETFASTINT (c->top_line, pos);
3533 window_resize_apply (c, horflag);
3534 if (horflag)
3535 pos = pos + XINT (c->total_cols);
3536 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3540 /* Clear out some redisplay caches. */
3541 XSETFASTINT (w->last_modified, 0);
3542 XSETFASTINT (w->last_overlay_modified, 0);
3546 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 1, 2, 0,
3547 doc: /* Apply requested size values for window-tree of FRAME.
3548 Optional argument HORIZONTAL omitted or nil means apply requested height
3549 values. HORIZONTAL non-nil means apply requested width values.
3551 This function checks whether the requested values sum up to a valid
3552 window layout, recursively assigns the new sizes of all subwindows and
3553 calculates and assigns the new start positions of these windows.
3555 Note: This function does not check any of `window-fixed-size-p',
3556 `window-min-height' or `window-min-width'. All these checks have to
3557 be applied on the Elisp level. */)
3558 (Lisp_Object frame, Lisp_Object horizontal)
3560 struct frame *f;
3561 struct window *r;
3562 int horflag = !NILP (horizontal);
3564 if (NILP (frame))
3565 frame = selected_frame;
3566 CHECK_LIVE_FRAME (frame);
3568 f = XFRAME (frame);
3569 r = XWINDOW (FRAME_ROOT_WINDOW (f));
3571 if (!window_resize_check (r, horflag)
3572 || ! EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3573 return Qnil;
3575 BLOCK_INPUT;
3576 window_resize_apply (r, horflag);
3578 windows_or_buffers_changed++;
3579 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3581 adjust_glyphs (f);
3582 UNBLOCK_INPUT;
3584 run_window_configuration_change_hook (f);
3586 return Qt;
3590 /* Resize frame F's windows when number of lines of F is set to SIZE.
3591 HORFLAG 1 means resize windows when number of columns of F is set to
3592 SIZE.
3594 This function can delete all windows but the selected one in order to
3595 satisfy the request. The result will be meaningful if and only if
3596 F's windows have meaningful sizes when you call this. */
3597 void
3598 resize_frame_windows (struct frame *f, int size, int horflag)
3600 Lisp_Object root = f->root_window;
3601 struct window *r = XWINDOW (root);
3602 Lisp_Object mini = f->minibuffer_window;
3603 struct window *m;
3604 /* new_size is the new size of the frame's root window. */
3605 int new_size = (horflag
3606 ? size
3607 : (size
3608 - FRAME_TOP_MARGIN (f)
3609 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3610 ? 1 : 0)));
3612 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
3613 if (NILP (r->vchild) && NILP (r->hchild))
3614 /* For a leaf root window just set the size. */
3615 if (horflag)
3616 XSETFASTINT (r->total_cols, new_size);
3617 else
3618 XSETFASTINT (r->total_lines, new_size);
3619 else
3621 /* old_size is the old size of the frame's root window. */
3622 int old_size = XFASTINT (horflag ? r->total_cols : r->total_lines);
3623 Lisp_Object delta;
3625 XSETINT (delta, new_size - old_size);
3626 /* Try a "normal" resize first. */
3627 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3628 if (window_resize_check (r, horflag) && new_size == XINT (r->new_total))
3629 window_resize_apply (r, horflag);
3630 else
3632 /* Try with "reasonable" minimum sizes next. */
3633 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3634 if (window_resize_check (r, horflag)
3635 && new_size == XINT (r->new_total))
3636 window_resize_apply (r, horflag);
3637 else
3639 /* Finally, try with "safe" minimum sizes. */
3640 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3641 if (window_resize_check (r, horflag)
3642 && new_size == XINT (r->new_total))
3643 window_resize_apply (r, horflag);
3644 else
3646 /* We lost. Delete all windows but the frame's
3647 selected one. */
3648 root = f->selected_window;
3649 Fdelete_other_windows_internal (root, Qnil);
3650 if (horflag)
3651 XSETFASTINT (XWINDOW (root)->total_cols, new_size);
3652 else
3653 XSETFASTINT (XWINDOW (root)->total_lines, new_size);
3659 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3661 m = XWINDOW (mini);
3662 if (horflag)
3663 XSETFASTINT (m->total_cols, size);
3664 else
3666 /* Are we sure we always want 1 line here? */
3667 XSETFASTINT (m->total_lines, 1);
3668 XSETFASTINT (m->top_line, XINT (r->top_line) + XINT (r->total_lines));
3674 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3675 doc: /* Split window OLD.
3676 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3677 new window. In any case TOTAL-SIZE must be a positive integer.
3679 Third argument SIDE nil (or `below') specifies that the new window shall
3680 be located below WINDOW. SIDE `above' means the new window shall be
3681 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3682 lines of the new window including space reserved for the mode and/or
3683 header line.
3685 SIDE t (or `right') specifies that the new window shall be located on
3686 the right side of WINDOW. SIDE `left' means the new window shall be
3687 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3688 number of columns of the new window including space reserved for fringes
3689 and the scrollbar or a divider column.
3691 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3692 according to the SIDE argument.
3694 The new total and normal sizes of all involved windows must have been
3695 set correctly. See the code of `split-window' for how this is done. */)
3696 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3698 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3699 parent window or an internal window we have to install as OLD's new
3700 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3701 provided OLD is a leaf window, or to the frame's selected window.
3702 NEW (*n) is the new window created with some parameters taken from
3703 REFERENCE (*r). */
3704 register Lisp_Object new, frame, reference;
3705 register struct window *o, *p, *n, *r;
3706 struct frame *f;
3707 int horflag
3708 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3709 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3710 int do_nest = 0;
3712 CHECK_WINDOW (old);
3713 o = XWINDOW (old);
3714 frame = WINDOW_FRAME (o);
3715 f = XFRAME (frame);
3717 CHECK_NUMBER (total_size);
3719 /* Set do_nest to 1 if we have to make a new parent window. We do
3720 that if either `window-nest' is non-nil, or OLD has no parent, or
3721 OLD is ortho-combined. */
3722 do_nest =
3723 !NILP (Vwindow_nest)
3724 || NILP (o->parent)
3725 || NILP (horflag
3726 ? (XWINDOW (o->parent)->hchild)
3727 : (XWINDOW (o->parent)->vchild));
3729 /* We need a live reference window to initialize some parameters. */
3730 if (WINDOW_LIVE_P (old))
3731 /* OLD is live, use it as reference window. */
3732 reference = old;
3733 else
3734 /* Use the frame's selected window as reference window. */
3735 reference = FRAME_SELECTED_WINDOW (f);
3736 r = XWINDOW (reference);
3738 /* The following bugs are caught by `split-window'. */
3739 if (MINI_WINDOW_P (o))
3740 error ("Attempt to split minibuffer window");
3741 else if (XINT (total_size) < (horflag ? 2 : 1))
3742 error ("Size of new window too small (after split)");
3743 else if (!do_nest && !NILP (Vwindow_splits))
3744 /* `window-splits' non-nil means try to resize OLD's siblings
3745 proportionally. */
3747 p = XWINDOW (o->parent);
3748 /* Temporarily pretend we split the parent window. */
3749 XSETINT (p->new_total,
3750 XINT (horflag ? p->total_cols : p->total_lines)
3751 - XINT (total_size));
3752 if (!window_resize_check (p, horflag))
3753 error ("Window sizes don't fit");
3754 else
3755 /* Undo the temporary pretension. */
3756 p->new_total = horflag ? p->total_cols : p->total_lines;
3758 else
3760 if (!window_resize_check (o, horflag))
3761 error ("Resizing old window failed");
3762 else if (XINT (total_size) + XINT (o->new_total)
3763 != XINT (horflag ? o->total_cols : o->total_lines))
3764 error ("Sum of sizes of old and new window don't fit");
3767 /* This is our point of no return. */
3768 if (do_nest)
3770 /* Save the old value of o->normal_cols/lines. It gets corrupted
3771 by make_parent_window and we need it below for assigning it to
3772 p->new_normal. */
3773 Lisp_Object new_normal = horflag ? o->normal_cols : o->normal_lines;
3775 make_parent_window (old, horflag);
3776 p = XWINDOW (o->parent);
3777 /* Store value of `window-nest' in new parent's nest slot. */
3778 p->nest = Vwindow_nest;
3779 /* Have PARENT inherit splits slot value from OLD. */
3780 p->splits = o->splits;
3781 /* Store value of `window-splits' in OLD's splits slot. */
3782 o->splits = Vwindow_splits;
3783 /* These get applied below. */
3784 p->new_total = horflag ? o->total_cols : o->total_lines;
3785 p->new_normal = new_normal;
3787 else
3788 p = XWINDOW (o->parent);
3790 windows_or_buffers_changed++;
3791 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3792 new = make_window ();
3793 n = XWINDOW (new);
3794 n->frame = frame;
3795 n->parent = o->parent;
3796 n->vchild = n->hchild = Qnil;
3798 if (EQ (side, Qabove) || EQ (side, Qleft))
3800 n->prev = o->prev;
3801 if (NILP (n->prev))
3802 if (horflag)
3803 p->hchild = new;
3804 else
3805 p->vchild = new;
3806 else
3807 XWINDOW (n->prev)->next = new;
3808 n->next = old;
3809 o->prev = new;
3811 else
3813 n->next = o->next;
3814 if (!NILP (n->next))
3815 XWINDOW (n->next)->prev = new;
3816 n->prev = old;
3817 o->next = new;
3820 n->buffer = Qt;
3821 n->window_end_valid = Qnil;
3822 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3824 /* Get special geometry settings from reference window. */
3825 n->left_margin_cols = r->left_margin_cols;
3826 n->right_margin_cols = r->right_margin_cols;
3827 n->left_fringe_width = r->left_fringe_width;
3828 n->right_fringe_width = r->right_fringe_width;
3829 n->fringes_outside_margins = r->fringes_outside_margins;
3830 n->scroll_bar_width = r->scroll_bar_width;
3831 n->vertical_scroll_bar_type = r->vertical_scroll_bar_type;
3833 /* Store `window-splits' in NEW's splits slot. */
3834 n->splits = Vwindow_splits;
3836 /* Directly assign orthogonal coordinates and sizes. */
3837 if (horflag)
3839 n->top_line = o->top_line;
3840 n->total_lines = o->total_lines;
3842 else
3844 n->left_col = o->left_col;
3845 n->total_cols = o->total_cols;
3848 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3849 get them ready here. */
3850 n->new_total = total_size;
3851 n->new_normal = normal_size;
3853 BLOCK_INPUT;
3854 window_resize_apply (p, horflag);
3855 adjust_glyphs (f);
3856 /* Set buffer of NEW to buffer of reference window. Don't run
3857 any hooks. */
3858 set_window_buffer (new, r->buffer, 0, 1);
3859 UNBLOCK_INPUT;
3861 /* Maybe we should run the scroll functions in Elisp (which already
3862 runs the configuration change hook). */
3863 if (! NILP (Vwindow_scroll_functions))
3864 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3865 Fmarker_position (n->start));
3866 /* Return NEW. */
3867 return new;
3871 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3872 doc: /* Remove WINDOW from its frame.
3873 WINDOW defaults to the selected window. Return nil. Signal an error
3874 when WINDOW is the only window on its frame. */)
3875 (register Lisp_Object window)
3877 register Lisp_Object parent, sibling, frame, root;
3878 struct window *w, *p, *s, *r;
3879 struct frame *f;
3880 int horflag;
3881 int before_sibling = 0;
3883 w = decode_any_window (window);
3884 XSETWINDOW (window, w);
3885 if (NILP (w->buffer) && NILP (w->hchild) && NILP (w->vchild))
3886 /* It's a no-op to delete an already deleted window. */
3887 return Qnil;
3889 parent = w->parent;
3890 if (NILP (parent))
3891 /* Never delete a minibuffer or frame root window. */
3892 error ("Attempt to delete minibuffer or sole ordinary window");
3893 else if (NILP (w->prev) && NILP (w->next))
3894 /* Rather bow out here, this case should be handled on the Elisp
3895 level. */
3896 error ("Attempt to delete sole window of parent");
3898 p = XWINDOW (parent);
3899 horflag = NILP (p->vchild);
3901 frame = WINDOW_FRAME (w);
3902 f = XFRAME (frame);
3904 root = FRAME_ROOT_WINDOW (f);
3905 r = XWINDOW (root);
3907 /* Unlink WINDOW from window tree. */
3908 if (NILP (w->prev))
3909 /* Get SIBLING below (on the right of) WINDOW. */
3911 /* before_sibling 1 means WINDOW is the first child of its
3912 parent and thus before the sibling. */
3913 before_sibling = 1;
3914 sibling = w->next;
3915 s = XWINDOW (sibling);
3916 s->prev = Qnil;
3917 if (horflag)
3918 p->hchild = sibling;
3919 else
3920 p->vchild = sibling;
3922 else
3923 /* Get SIBLING above (on the left of) WINDOW. */
3925 sibling = w->prev;
3926 s = XWINDOW (sibling);
3927 s->next = w->next;
3928 if (!NILP (s->next))
3929 XWINDOW (s->next)->prev = sibling;
3932 if (window_resize_check (r, horflag)
3933 && EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3934 /* We can delete WINDOW now. */
3936 /* Block input. */
3937 BLOCK_INPUT;
3938 #ifdef HAVE_XWIDGETS
3939 xwidget_view_delete_all_in_window(w);
3940 #endif
3941 window_resize_apply (p, horflag);
3943 windows_or_buffers_changed++;
3944 Vwindow_list = Qnil;
3945 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3947 w->next = Qnil; /* Don't delete w->next too. */
3948 free_window_matrices (w);
3950 if (!NILP (w->vchild))
3952 delete_all_subwindows (w->vchild);
3953 w->vchild = Qnil;
3955 else if (!NILP (w->hchild))
3957 delete_all_subwindows (w->hchild);
3958 w->hchild = Qnil;
3960 else if (!NILP (w->buffer))
3962 unshow_buffer (w);
3963 unchain_marker (XMARKER (w->pointm));
3964 unchain_marker (XMARKER (w->start));
3965 w->buffer = Qnil;
3968 if (NILP (s->prev) && NILP (s->next))
3969 /* A matrjoshka where SIBLING has become the only child of
3970 PARENT. */
3972 /* Put SIBLING into PARENT's place. */
3973 replace_window (parent, sibling, 0);
3974 /* Have SIBLING inherit the following three slot values from
3975 PARENT (the nest slot is not inherited). */
3976 s->normal_cols = p->normal_cols;
3977 s->normal_lines = p->normal_lines;
3978 s->splits = p->splits;
3979 /* Mark PARENT as deleted. */
3980 p->vchild = p->hchild = Qnil;
3981 /* Try to merge SIBLING into its new parent. */
3982 recombine_windows (sibling);
3985 adjust_glyphs (f);
3987 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
3988 /* We deleted the frame's selected window. */
3990 /* Use the frame's first window as fallback ... */
3991 Lisp_Object new_selected_window = Fframe_first_window (frame);
3992 /* ... but preferably use its most recently used window. */
3993 Lisp_Object mru_window;
3995 /* `get-mru-window' might fail for some reason so play it safe
3996 - promote the first window _without recording it_ first. */
3997 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
3998 Fselect_window (new_selected_window, Qt);
3999 else
4000 FRAME_SELECTED_WINDOW (f) = new_selected_window;
4002 UNBLOCK_INPUT;
4004 /* Now look whether `get-mru-window' gets us something. */
4005 mru_window = call1 (Qget_mru_window, frame);
4006 if (WINDOW_LIVE_P (mru_window)
4007 && EQ (XWINDOW (mru_window)->frame, frame))
4008 new_selected_window = mru_window;
4010 /* If all ended up well, we now promote the mru window. */
4011 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4012 Fselect_window (new_selected_window, Qnil);
4013 else
4014 FRAME_SELECTED_WINDOW (f) = new_selected_window;
4016 else
4017 UNBLOCK_INPUT;
4019 /* Must be run by the caller:
4020 run_window_configuration_change_hook (f); */
4022 else
4023 /* We failed: Relink WINDOW into window tree. */
4025 if (before_sibling)
4027 s->prev = window;
4028 if (horflag)
4029 p->hchild = window;
4030 else
4031 p->vchild = window;
4033 else
4035 s->next = window;
4036 if (!NILP (w->next))
4037 XWINDOW (w->next)->prev = window;
4039 error ("Deletion failed");
4042 return Qnil;
4045 /***********************************************************************
4046 Resizing Mini-Windows
4047 ***********************************************************************/
4049 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4050 can. */
4051 void
4052 grow_mini_window (struct window *w, int delta)
4054 struct frame *f = XFRAME (w->frame);
4055 struct window *r;
4056 Lisp_Object root, value;
4058 xassert (MINI_WINDOW_P (w));
4059 xassert (delta >= 0);
4061 root = FRAME_ROOT_WINDOW (f);
4062 r = XWINDOW (root);
4063 value = call2 (Qwindow_resize_root_window_vertically,
4064 root, make_number (- delta));
4065 if (INTEGERP (value) && window_resize_check (r, 0))
4067 BLOCK_INPUT;
4068 window_resize_apply (r, 0);
4070 /* Grow the mini-window. */
4071 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4072 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - XINT (value));
4073 XSETFASTINT (w->last_modified, 0);
4074 XSETFASTINT (w->last_overlay_modified, 0);
4076 adjust_glyphs (f);
4077 UNBLOCK_INPUT;
4082 /* Shrink mini-window W. */
4083 void
4084 shrink_mini_window (struct window *w)
4086 struct frame *f = XFRAME (w->frame);
4087 struct window *r;
4088 Lisp_Object root, value;
4089 EMACS_INT size;
4091 xassert (MINI_WINDOW_P (w));
4093 size = XINT (w->total_lines);
4094 if (size > 1)
4096 root = FRAME_ROOT_WINDOW (f);
4097 r = XWINDOW (root);
4098 value = call2 (Qwindow_resize_root_window_vertically,
4099 root, make_number (size - 1));
4100 if (INTEGERP (value) && window_resize_check (r, 0))
4102 BLOCK_INPUT;
4103 window_resize_apply (r, 0);
4105 /* Shrink the mini-window. */
4106 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4107 XSETFASTINT (w->total_lines, 1);
4109 XSETFASTINT (w->last_modified, 0);
4110 XSETFASTINT (w->last_overlay_modified, 0);
4112 adjust_glyphs (f);
4113 UNBLOCK_INPUT;
4115 /* If the above failed for whatever strange reason we must make a
4116 one window frame here. The same routine will be needed when
4117 shrinking the frame (and probably when making the initial
4118 *scratch* window). For the moment leave things as they are. */
4122 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4123 doc: /* Resize minibuffer window WINDOW. */)
4124 (Lisp_Object window)
4126 struct window *w = XWINDOW (window);
4127 struct window *r;
4128 struct frame *f;
4129 int height;
4131 CHECK_WINDOW (window);
4132 f = XFRAME (w->frame);
4134 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4135 error ("Not a valid minibuffer window");
4136 else if (FRAME_MINIBUF_ONLY_P (f))
4137 error ("Cannot resize a minibuffer-only frame");
4139 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4140 height = XINT (r->total_lines) + XINT (w->total_lines);
4141 if (window_resize_check (r, 0)
4142 && XINT (w->new_total) > 0
4143 && height == XINT (r->new_total) + XINT (w->new_total))
4145 BLOCK_INPUT;
4146 window_resize_apply (r, 0);
4148 w->total_lines = w->new_total;
4149 XSETFASTINT (w->top_line, XINT (r->top_line) + XINT (r->total_lines));
4151 windows_or_buffers_changed++;
4152 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4153 adjust_glyphs (f);
4154 UNBLOCK_INPUT;
4156 run_window_configuration_change_hook (f);
4157 return Qt;
4159 else error ("Failed to resize minibuffer window");
4162 /* Mark window cursors off for all windows in the window tree rooted
4163 at W by setting their phys_cursor_on_p flag to zero. Called from
4164 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4165 the frame are cleared. */
4167 void
4168 mark_window_cursors_off (struct window *w)
4170 while (w)
4172 if (!NILP (w->hchild))
4173 mark_window_cursors_off (XWINDOW (w->hchild));
4174 else if (!NILP (w->vchild))
4175 mark_window_cursors_off (XWINDOW (w->vchild));
4176 else
4177 w->phys_cursor_on_p = 0;
4179 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4184 /* Return number of lines of text (not counting mode lines) in W. */
4187 window_internal_height (struct window *w)
4189 int ht = XFASTINT (w->total_lines);
4191 if (!MINI_WINDOW_P (w))
4193 if (!NILP (w->parent)
4194 || !NILP (w->vchild)
4195 || !NILP (w->hchild)
4196 || !NILP (w->next)
4197 || !NILP (w->prev)
4198 || WINDOW_WANTS_MODELINE_P (w))
4199 --ht;
4201 if (WINDOW_WANTS_HEADER_LINE_P (w))
4202 --ht;
4205 return ht;
4208 /************************************************************************
4209 Window Scrolling
4210 ***********************************************************************/
4212 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4213 N screen-fulls, which is defined as the height of the window minus
4214 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4215 instead. Negative values of N mean scroll down. NOERROR non-zero
4216 means don't signal an error if we try to move over BEGV or ZV,
4217 respectively. */
4219 static void
4220 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4222 immediate_quit = 1;
4224 /* If we must, use the pixel-based version which is much slower than
4225 the line-based one but can handle varying line heights. */
4226 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4227 window_scroll_pixel_based (window, n, whole, noerror);
4228 else
4229 window_scroll_line_based (window, n, whole, noerror);
4231 immediate_quit = 0;
4235 /* Implementation of window_scroll that works based on pixel line
4236 heights. See the comment of window_scroll for parameter
4237 descriptions. */
4239 static void
4240 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4242 struct it it;
4243 struct window *w = XWINDOW (window);
4244 struct text_pos start;
4245 int this_scroll_margin;
4246 /* True if we fiddled the window vscroll field without really scrolling. */
4247 int vscrolled = 0;
4248 int x, y, rtop, rbot, rowh, vpos;
4249 void *itdata = NULL;
4251 SET_TEXT_POS_FROM_MARKER (start, w->start);
4253 /* If PT is not visible in WINDOW, move back one half of
4254 the screen. Allow PT to be partially visible, otherwise
4255 something like (scroll-down 1) with PT in the line before
4256 the partially visible one would recenter. */
4258 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4260 itdata = bidi_shelve_cache ();
4261 /* Move backward half the height of the window. Performance note:
4262 vmotion used here is about 10% faster, but would give wrong
4263 results for variable height lines. */
4264 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4265 it.current_y = it.last_visible_y;
4266 move_it_vertically_backward (&it, window_box_height (w) / 2);
4268 /* The function move_iterator_vertically may move over more than
4269 the specified y-distance. If it->w is small, e.g. a
4270 mini-buffer window, we may end up in front of the window's
4271 display area. This is the case when Start displaying at the
4272 start of the line containing PT in this case. */
4273 if (it.current_y <= 0)
4275 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4276 move_it_vertically_backward (&it, 0);
4277 it.current_y = 0;
4280 start = it.current.pos;
4281 bidi_unshelve_cache (itdata, 0);
4283 else if (auto_window_vscroll_p)
4285 if (rtop || rbot) /* partially visible */
4287 int px;
4288 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4289 if (whole)
4290 dy = max ((window_box_height (w)
4291 - next_screen_context_lines * dy),
4292 dy);
4293 dy *= n;
4295 if (n < 0)
4297 /* Only vscroll backwards if already vscrolled forwards. */
4298 if (w->vscroll < 0 && rtop > 0)
4300 px = max (0, -w->vscroll - min (rtop, -dy));
4301 Fset_window_vscroll (window, make_number (px), Qt);
4302 return;
4305 if (n > 0)
4307 /* Do vscroll if already vscrolled or only display line. */
4308 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4310 px = max (0, -w->vscroll + min (rbot, dy));
4311 Fset_window_vscroll (window, make_number (px), Qt);
4312 return;
4315 /* Maybe modify window start instead of scrolling. */
4316 if (rbot > 0 || w->vscroll < 0)
4318 EMACS_INT spos;
4320 Fset_window_vscroll (window, make_number (0), Qt);
4321 /* If there are other text lines above the current row,
4322 move window start to current row. Else to next row. */
4323 if (rbot > 0)
4324 spos = XINT (Fline_beginning_position (Qnil));
4325 else
4326 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4327 set_marker_restricted (w->start, make_number (spos),
4328 w->buffer);
4329 w->start_at_line_beg = Qt;
4330 w->update_mode_line = Qt;
4331 XSETFASTINT (w->last_modified, 0);
4332 XSETFASTINT (w->last_overlay_modified, 0);
4333 /* Set force_start so that redisplay_window will run the
4334 window-scroll-functions. */
4335 w->force_start = Qt;
4336 return;
4340 /* Cancel previous vscroll. */
4341 Fset_window_vscroll (window, make_number (0), Qt);
4344 itdata = bidi_shelve_cache ();
4345 /* If scroll_preserve_screen_position is non-nil, we try to set
4346 point in the same window line as it is now, so get that line. */
4347 if (!NILP (Vscroll_preserve_screen_position))
4349 /* We preserve the goal pixel coordinate across consecutive
4350 calls to scroll-up, scroll-down and other commands that
4351 have the `scroll-command' property. This avoids the
4352 possibility of point becoming "stuck" on a tall line when
4353 scrolling by one line. */
4354 if (window_scroll_pixel_based_preserve_y < 0
4355 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4356 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4358 start_display (&it, w, start);
4359 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4360 window_scroll_pixel_based_preserve_y = it.current_y;
4361 window_scroll_pixel_based_preserve_x = it.current_x;
4364 else
4365 window_scroll_pixel_based_preserve_y
4366 = window_scroll_pixel_based_preserve_x = -1;
4368 /* Move iterator it from start the specified distance forward or
4369 backward. The result is the new window start. */
4370 start_display (&it, w, start);
4371 if (whole)
4373 EMACS_INT start_pos = IT_CHARPOS (it);
4374 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4375 dy = max ((window_box_height (w)
4376 - next_screen_context_lines * dy),
4377 dy) * n;
4379 /* Note that move_it_vertically always moves the iterator to the
4380 start of a line. So, if the last line doesn't have a newline,
4381 we would end up at the start of the line ending at ZV. */
4382 if (dy <= 0)
4384 move_it_vertically_backward (&it, -dy);
4385 /* Ensure we actually do move, e.g. in case we are currently
4386 looking at an image that is taller that the window height. */
4387 while (start_pos == IT_CHARPOS (it)
4388 && start_pos > BEGV)
4389 move_it_by_lines (&it, -1);
4391 else if (dy > 0)
4393 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4394 MOVE_TO_POS | MOVE_TO_Y);
4395 /* Ensure we actually do move, e.g. in case we are currently
4396 looking at an image that is taller that the window height. */
4397 while (start_pos == IT_CHARPOS (it)
4398 && start_pos < ZV)
4399 move_it_by_lines (&it, 1);
4402 else
4403 move_it_by_lines (&it, n);
4405 /* We failed if we find ZV is already on the screen (scrolling up,
4406 means there's nothing past the end), or if we can't start any
4407 earlier (scrolling down, means there's nothing past the top). */
4408 if ((n > 0 && IT_CHARPOS (it) == ZV)
4409 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4411 if (IT_CHARPOS (it) == ZV)
4413 if (it.current_y < it.last_visible_y
4414 && (it.current_y + it.max_ascent + it.max_descent
4415 > it.last_visible_y))
4417 /* The last line was only partially visible, make it fully
4418 visible. */
4419 w->vscroll = (it.last_visible_y
4420 - it.current_y + it.max_ascent + it.max_descent);
4421 adjust_glyphs (it.f);
4423 else
4425 bidi_unshelve_cache (itdata, 0);
4426 if (noerror)
4427 return;
4428 else if (n < 0) /* could happen with empty buffers */
4429 xsignal0 (Qbeginning_of_buffer);
4430 else
4431 xsignal0 (Qend_of_buffer);
4434 else
4436 if (w->vscroll != 0)
4437 /* The first line was only partially visible, make it fully
4438 visible. */
4439 w->vscroll = 0;
4440 else
4442 bidi_unshelve_cache (itdata, 0);
4443 if (noerror)
4444 return;
4445 else
4446 xsignal0 (Qbeginning_of_buffer);
4450 /* If control gets here, then we vscrolled. */
4452 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4454 /* Don't try to change the window start below. */
4455 vscrolled = 1;
4458 if (! vscrolled)
4460 EMACS_INT pos = IT_CHARPOS (it);
4461 EMACS_INT bytepos;
4463 /* If in the middle of a multi-glyph character move forward to
4464 the next character. */
4465 if (in_display_vector_p (&it))
4467 ++pos;
4468 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4471 /* Set the window start, and set up the window for redisplay. */
4472 set_marker_restricted (w->start, make_number (pos),
4473 w->buffer);
4474 bytepos = XMARKER (w->start)->bytepos;
4475 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4476 ? Qt : Qnil);
4477 w->update_mode_line = Qt;
4478 XSETFASTINT (w->last_modified, 0);
4479 XSETFASTINT (w->last_overlay_modified, 0);
4480 /* Set force_start so that redisplay_window will run the
4481 window-scroll-functions. */
4482 w->force_start = Qt;
4485 /* The rest of this function uses current_y in a nonstandard way,
4486 not including the height of the header line if any. */
4487 it.current_y = it.vpos = 0;
4489 /* Move PT out of scroll margins.
4490 This code wants current_y to be zero at the window start position
4491 even if there is a header line. */
4492 this_scroll_margin = max (0, scroll_margin);
4493 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4494 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4496 if (n > 0)
4498 /* We moved the window start towards ZV, so PT may be now
4499 in the scroll margin at the top. */
4500 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4501 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4502 && (NILP (Vscroll_preserve_screen_position)
4503 || EQ (Vscroll_preserve_screen_position, Qt)))
4504 /* We found PT at a legitimate height. Leave it alone. */
4506 else if (window_scroll_pixel_based_preserve_y >= 0)
4508 /* If we have a header line, take account of it.
4509 This is necessary because we set it.current_y to 0, above. */
4510 move_it_to (&it, -1,
4511 window_scroll_pixel_based_preserve_x,
4512 window_scroll_pixel_based_preserve_y
4513 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4514 -1, MOVE_TO_Y | MOVE_TO_X);
4515 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4517 else
4519 while (it.current_y < this_scroll_margin)
4521 int prev = it.current_y;
4522 move_it_by_lines (&it, 1);
4523 if (prev == it.current_y)
4524 break;
4526 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4529 else if (n < 0)
4531 EMACS_INT charpos, bytepos;
4532 int partial_p;
4534 /* Save our position, for the
4535 window_scroll_pixel_based_preserve_y case. */
4536 charpos = IT_CHARPOS (it);
4537 bytepos = IT_BYTEPOS (it);
4539 /* We moved the window start towards BEGV, so PT may be now
4540 in the scroll margin at the bottom. */
4541 move_it_to (&it, PT, -1,
4542 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4543 - this_scroll_margin - 1),
4545 MOVE_TO_POS | MOVE_TO_Y);
4547 /* Save our position, in case it's correct. */
4548 charpos = IT_CHARPOS (it);
4549 bytepos = IT_BYTEPOS (it);
4551 /* See if point is on a partially visible line at the end. */
4552 if (it.what == IT_EOB)
4553 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4554 else
4556 move_it_by_lines (&it, 1);
4557 partial_p = it.current_y > it.last_visible_y;
4560 if (charpos == PT && !partial_p
4561 && (NILP (Vscroll_preserve_screen_position)
4562 || EQ (Vscroll_preserve_screen_position, Qt)))
4563 /* We found PT before we found the display margin, so PT is ok. */
4565 else if (window_scroll_pixel_based_preserve_y >= 0)
4567 SET_TEXT_POS_FROM_MARKER (start, w->start);
4568 start_display (&it, w, start);
4569 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4570 here because we called start_display again and did not
4571 alter it.current_y this time. */
4572 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4573 window_scroll_pixel_based_preserve_y, -1,
4574 MOVE_TO_Y | MOVE_TO_X);
4575 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4577 else
4579 if (partial_p)
4580 /* The last line was only partially visible, so back up two
4581 lines to make sure we're on a fully visible line. */
4583 move_it_by_lines (&it, -2);
4584 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4586 else
4587 /* No, the position we saved is OK, so use it. */
4588 SET_PT_BOTH (charpos, bytepos);
4591 bidi_unshelve_cache (itdata, 0);
4595 /* Implementation of window_scroll that works based on screen lines.
4596 See the comment of window_scroll for parameter descriptions. */
4598 static void
4599 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4601 register struct window *w = XWINDOW (window);
4602 /* Fvertical_motion enters redisplay, which can trigger
4603 fontification, which in turn can modify buffer text (e.g., if the
4604 fontification functions replace escape sequences with faces, as
4605 in `grep-mode-font-lock-keywords'). So we use a marker to record
4606 the old point position, to prevent crashes in SET_PT_BOTH. */
4607 Lisp_Object opoint_marker = Fpoint_marker ();
4608 register EMACS_INT pos, pos_byte;
4609 register int ht = window_internal_height (w);
4610 register Lisp_Object tem;
4611 int lose;
4612 Lisp_Object bolp;
4613 EMACS_INT startpos;
4614 Lisp_Object original_pos = Qnil;
4616 /* If scrolling screen-fulls, compute the number of lines to
4617 scroll from the window's height. */
4618 if (whole)
4619 n *= max (1, ht - next_screen_context_lines);
4621 startpos = marker_position (w->start);
4623 if (!NILP (Vscroll_preserve_screen_position))
4625 if (window_scroll_preserve_vpos <= 0
4626 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4627 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4629 struct position posit
4630 = *compute_motion (startpos, 0, 0, 0,
4631 PT, ht, 0,
4632 -1, XINT (w->hscroll),
4633 0, w);
4634 window_scroll_preserve_vpos = posit.vpos;
4635 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
4638 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4639 make_number (window_scroll_preserve_vpos));
4642 XSETFASTINT (tem, PT);
4643 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4645 if (NILP (tem))
4647 Fvertical_motion (make_number (- (ht / 2)), window);
4648 startpos = PT;
4651 SET_PT (startpos);
4652 lose = n < 0 && PT == BEGV;
4653 Fvertical_motion (make_number (n), window);
4654 pos = PT;
4655 pos_byte = PT_BYTE;
4656 bolp = Fbolp ();
4657 SET_PT_BOTH (marker_position (opoint_marker),
4658 marker_byte_position (opoint_marker));
4660 if (lose)
4662 if (noerror)
4663 return;
4664 else
4665 xsignal0 (Qbeginning_of_buffer);
4668 if (pos < ZV)
4670 /* Don't use a scroll margin that is negative or too large. */
4671 int this_scroll_margin =
4672 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4674 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4675 w->start_at_line_beg = bolp;
4676 w->update_mode_line = Qt;
4677 XSETFASTINT (w->last_modified, 0);
4678 XSETFASTINT (w->last_overlay_modified, 0);
4679 /* Set force_start so that redisplay_window will run
4680 the window-scroll-functions. */
4681 w->force_start = Qt;
4683 if (!NILP (Vscroll_preserve_screen_position)
4684 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4686 SET_PT_BOTH (pos, pos_byte);
4687 Fvertical_motion (original_pos, window);
4689 /* If we scrolled forward, put point enough lines down
4690 that it is outside the scroll margin. */
4691 else if (n > 0)
4693 int top_margin;
4695 if (this_scroll_margin > 0)
4697 SET_PT_BOTH (pos, pos_byte);
4698 Fvertical_motion (make_number (this_scroll_margin), window);
4699 top_margin = PT;
4701 else
4702 top_margin = pos;
4704 if (top_margin <= marker_position (opoint_marker))
4705 SET_PT_BOTH (marker_position (opoint_marker),
4706 marker_byte_position (opoint_marker));
4707 else if (!NILP (Vscroll_preserve_screen_position))
4709 SET_PT_BOTH (pos, pos_byte);
4710 Fvertical_motion (original_pos, window);
4712 else
4713 SET_PT (top_margin);
4715 else if (n < 0)
4717 int bottom_margin;
4719 /* If we scrolled backward, put point near the end of the window
4720 but not within the scroll margin. */
4721 SET_PT_BOTH (pos, pos_byte);
4722 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4723 if (XFASTINT (tem) == ht - this_scroll_margin)
4724 bottom_margin = PT;
4725 else
4726 bottom_margin = PT + 1;
4728 if (bottom_margin > marker_position (opoint_marker))
4729 SET_PT_BOTH (marker_position (opoint_marker),
4730 marker_byte_position (opoint_marker));
4731 else
4733 if (!NILP (Vscroll_preserve_screen_position))
4735 SET_PT_BOTH (pos, pos_byte);
4736 Fvertical_motion (original_pos, window);
4738 else
4739 Fvertical_motion (make_number (-1), window);
4743 else
4745 if (noerror)
4746 return;
4747 else
4748 xsignal0 (Qend_of_buffer);
4753 /* Scroll selected_window up or down. If N is nil, scroll a
4754 screen-full which is defined as the height of the window minus
4755 next_screen_context_lines. If N is the symbol `-', scroll.
4756 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4757 up. This is the guts of Fscroll_up and Fscroll_down. */
4759 static void
4760 scroll_command (Lisp_Object n, int direction)
4762 int count = SPECPDL_INDEX ();
4764 xassert (eabs (direction) == 1);
4766 /* If selected window's buffer isn't current, make it current for
4767 the moment. But don't screw up if window_scroll gets an error. */
4768 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4770 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4771 Fset_buffer (XWINDOW (selected_window)->buffer);
4773 /* Make redisplay consider other windows than just selected_window. */
4774 ++windows_or_buffers_changed;
4777 if (NILP (n))
4778 window_scroll (selected_window, direction, 1, 0);
4779 else if (EQ (n, Qminus))
4780 window_scroll (selected_window, -direction, 1, 0);
4781 else
4783 n = Fprefix_numeric_value (n);
4784 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4787 unbind_to (count, Qnil);
4790 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4791 doc: /* Scroll text of selected window upward ARG lines.
4792 If ARG is omitted or nil, scroll upward by a near full screen.
4793 A near full screen is `next-screen-context-lines' less than a full screen.
4794 Negative ARG means scroll downward.
4795 If ARG is the atom `-', scroll downward by nearly full screen.
4796 When calling from a program, supply as argument a number, nil, or `-'. */)
4797 (Lisp_Object arg)
4799 scroll_command (arg, 1);
4800 return Qnil;
4803 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4804 doc: /* Scroll text of selected window down ARG lines.
4805 If ARG is omitted or nil, scroll down by a near full screen.
4806 A near full screen is `next-screen-context-lines' less than a full screen.
4807 Negative ARG means scroll upward.
4808 If ARG is the atom `-', scroll upward by nearly full screen.
4809 When calling from a program, supply as argument a number, nil, or `-'. */)
4810 (Lisp_Object arg)
4812 scroll_command (arg, -1);
4813 return Qnil;
4816 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4817 doc: /* Return the other window for \"other window scroll\" commands.
4818 If `other-window-scroll-buffer' is non-nil, a window
4819 showing that buffer is used.
4820 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4821 specifies the window. This takes precedence over
4822 `other-window-scroll-buffer'. */)
4823 (void)
4825 Lisp_Object window;
4827 if (MINI_WINDOW_P (XWINDOW (selected_window))
4828 && !NILP (Vminibuf_scroll_window))
4829 window = Vminibuf_scroll_window;
4830 /* If buffer is specified, scroll that buffer. */
4831 else if (!NILP (Vother_window_scroll_buffer))
4833 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4834 if (NILP (window))
4835 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4837 else
4839 /* Nothing specified; look for a neighboring window on the same
4840 frame. */
4841 window = Fnext_window (selected_window, Qnil, Qnil);
4843 if (EQ (window, selected_window))
4844 /* That didn't get us anywhere; look for a window on another
4845 visible frame. */
4847 window = Fnext_window (window, Qnil, Qt);
4848 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4849 && ! EQ (window, selected_window));
4852 CHECK_LIVE_WINDOW (window);
4854 if (EQ (window, selected_window))
4855 error ("There is no other window");
4857 return window;
4860 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4861 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4862 A near full screen is `next-screen-context-lines' less than a full screen.
4863 The next window is the one below the current one; or the one at the top
4864 if the current one is at the bottom. Negative ARG means scroll downward.
4865 If ARG is the atom `-', scroll downward by nearly full screen.
4866 When calling from a program, supply as argument a number, nil, or `-'.
4868 If `other-window-scroll-buffer' is non-nil, scroll the window
4869 showing that buffer, popping the buffer up if necessary.
4870 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4871 specifies the window to scroll. This takes precedence over
4872 `other-window-scroll-buffer'. */)
4873 (Lisp_Object arg)
4875 Lisp_Object window;
4876 struct window *w;
4877 int count = SPECPDL_INDEX ();
4879 window = Fother_window_for_scrolling ();
4880 w = XWINDOW (window);
4882 /* Don't screw up if window_scroll gets an error. */
4883 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4884 ++windows_or_buffers_changed;
4886 Fset_buffer (w->buffer);
4887 SET_PT (marker_position (w->pointm));
4889 if (NILP (arg))
4890 window_scroll (window, 1, 1, 1);
4891 else if (EQ (arg, Qminus))
4892 window_scroll (window, -1, 1, 1);
4893 else
4895 if (CONSP (arg))
4896 arg = Fcar (arg);
4897 CHECK_NUMBER (arg);
4898 window_scroll (window, XINT (arg), 0, 1);
4901 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4902 unbind_to (count, Qnil);
4904 return Qnil;
4907 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4908 doc: /* Scroll selected window display ARG columns left.
4909 Default for ARG is window width minus 2.
4910 Value is the total amount of leftward horizontal scrolling in
4911 effect after the change.
4912 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4913 lower bound for automatic scrolling, i.e. automatic scrolling
4914 will not scroll a window to a column less than the value returned
4915 by this function. This happens in an interactive call. */)
4916 (register Lisp_Object arg, Lisp_Object set_minimum)
4918 Lisp_Object result;
4919 int hscroll;
4920 struct window *w = XWINDOW (selected_window);
4922 if (NILP (arg))
4923 XSETFASTINT (arg, window_body_cols (w) - 2);
4924 else
4925 arg = Fprefix_numeric_value (arg);
4927 hscroll = XINT (w->hscroll) + XINT (arg);
4928 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4930 if (!NILP (set_minimum))
4931 w->min_hscroll = w->hscroll;
4933 return result;
4936 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4937 doc: /* Scroll selected window display ARG columns right.
4938 Default for ARG is window width minus 2.
4939 Value is the total amount of leftward horizontal scrolling in
4940 effect after the change.
4941 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4942 lower bound for automatic scrolling, i.e. automatic scrolling
4943 will not scroll a window to a column less than the value returned
4944 by this function. This happens in an interactive call. */)
4945 (register Lisp_Object arg, Lisp_Object set_minimum)
4947 Lisp_Object result;
4948 int hscroll;
4949 struct window *w = XWINDOW (selected_window);
4951 if (NILP (arg))
4952 XSETFASTINT (arg, window_body_cols (w) - 2);
4953 else
4954 arg = Fprefix_numeric_value (arg);
4956 hscroll = XINT (w->hscroll) - XINT (arg);
4957 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4959 if (!NILP (set_minimum))
4960 w->min_hscroll = w->hscroll;
4962 return result;
4965 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4966 doc: /* Return the window which was selected when entering the minibuffer.
4967 Returns nil, if selected window is not a minibuffer window. */)
4968 (void)
4970 if (minibuf_level > 0
4971 && MINI_WINDOW_P (XWINDOW (selected_window))
4972 && WINDOW_LIVE_P (minibuf_selected_window))
4973 return minibuf_selected_window;
4975 return Qnil;
4978 /* Value is the number of lines actually displayed in window W,
4979 as opposed to its height. */
4981 static int
4982 displayed_window_lines (struct window *w)
4984 struct it it;
4985 struct text_pos start;
4986 int height = window_box_height (w);
4987 struct buffer *old_buffer;
4988 int bottom_y;
4989 void *itdata = NULL;
4991 if (XBUFFER (w->buffer) != current_buffer)
4993 old_buffer = current_buffer;
4994 set_buffer_internal (XBUFFER (w->buffer));
4996 else
4997 old_buffer = NULL;
4999 /* In case W->start is out of the accessible range, do something
5000 reasonable. This happens in Info mode when Info-scroll-down
5001 calls (recenter -1) while W->start is 1. */
5002 if (XMARKER (w->start)->charpos < BEGV)
5003 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5004 else if (XMARKER (w->start)->charpos > ZV)
5005 SET_TEXT_POS (start, ZV, ZV_BYTE);
5006 else
5007 SET_TEXT_POS_FROM_MARKER (start, w->start);
5009 itdata = bidi_shelve_cache ();
5010 start_display (&it, w, start);
5011 move_it_vertically (&it, height);
5012 bottom_y = line_bottom_y (&it);
5013 bidi_unshelve_cache (itdata, 0);
5015 /* rms: On a non-window display,
5016 the value of it.vpos at the bottom of the screen
5017 seems to be 1 larger than window_box_height (w).
5018 This kludge fixes a bug whereby (move-to-window-line -1)
5019 when ZV is on the last screen line
5020 moves to the previous screen line instead of the last one. */
5021 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5022 height++;
5024 /* Add in empty lines at the bottom of the window. */
5025 if (bottom_y < height)
5027 int uy = FRAME_LINE_HEIGHT (it.f);
5028 it.vpos += (height - bottom_y + uy - 1) / uy;
5031 if (old_buffer)
5032 set_buffer_internal (old_buffer);
5034 return it.vpos;
5038 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5039 doc: /* Center point in selected window and maybe redisplay frame.
5040 With prefix argument ARG, recenter putting point on screen line ARG
5041 relative to the selected window. If ARG is negative, it counts up from the
5042 bottom of the window. (ARG should be less than the height of the window.)
5044 If ARG is omitted or nil, then recenter with point on the middle line of
5045 the selected window; if the variable `recenter-redisplay' is non-nil,
5046 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5047 is set to `grow-only', this resets the tool-bar's height to the minimum
5048 height needed); if `recenter-redisplay' has the special value `tty',
5049 then only tty frame are redrawn.
5051 Just C-u as prefix means put point in the center of the window
5052 and redisplay normally--don't erase and redraw the frame. */)
5053 (register Lisp_Object arg)
5055 struct window *w = XWINDOW (selected_window);
5056 struct buffer *buf = XBUFFER (w->buffer);
5057 struct buffer *obuf = current_buffer;
5058 int center_p = 0;
5059 EMACS_INT charpos, bytepos;
5060 EMACS_INT iarg IF_LINT (= 0);
5061 int this_scroll_margin;
5063 /* If redisplay is suppressed due to an error, try again. */
5064 obuf->display_error_modiff = 0;
5066 if (NILP (arg))
5068 if (!NILP (Vrecenter_redisplay)
5069 && (!EQ (Vrecenter_redisplay, Qtty)
5070 || !NILP (Ftty_type (selected_frame))))
5072 ptrdiff_t i;
5074 /* Invalidate pixel data calculated for all compositions. */
5075 for (i = 0; i < n_compositions; i++)
5076 composition_table[i]->font = NULL;
5078 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5080 Fredraw_frame (WINDOW_FRAME (w));
5081 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5084 center_p = 1;
5086 else if (CONSP (arg)) /* Just C-u. */
5087 center_p = 1;
5088 else
5090 arg = Fprefix_numeric_value (arg);
5091 CHECK_NUMBER (arg);
5092 iarg = XINT (arg);
5095 set_buffer_internal (buf);
5097 /* Do this after making BUF current
5098 in case scroll_margin is buffer-local. */
5099 this_scroll_margin =
5100 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5102 /* Handle centering on a graphical frame specially. Such frames can
5103 have variable-height lines and centering point on the basis of
5104 line counts would lead to strange effects. */
5105 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5107 if (center_p)
5109 struct it it;
5110 struct text_pos pt;
5111 void *itdata = bidi_shelve_cache ();
5113 SET_TEXT_POS (pt, PT, PT_BYTE);
5114 start_display (&it, w, pt);
5115 move_it_vertically_backward (&it, window_box_height (w) / 2);
5116 charpos = IT_CHARPOS (it);
5117 bytepos = IT_BYTEPOS (it);
5118 bidi_unshelve_cache (itdata, 0);
5120 else if (iarg < 0)
5122 struct it it;
5123 struct text_pos pt;
5124 int nlines = min (INT_MAX, -iarg);
5125 int extra_line_spacing;
5126 int h = window_box_height (w);
5127 void *itdata = bidi_shelve_cache ();
5129 iarg = - max (-iarg, this_scroll_margin);
5131 SET_TEXT_POS (pt, PT, PT_BYTE);
5132 start_display (&it, w, pt);
5134 /* Be sure we have the exact height of the full line containing PT. */
5135 move_it_by_lines (&it, 0);
5137 /* The amount of pixels we have to move back is the window
5138 height minus what's displayed in the line containing PT,
5139 and the lines below. */
5140 it.current_y = 0;
5141 it.vpos = 0;
5142 move_it_by_lines (&it, nlines);
5144 if (it.vpos == nlines)
5145 h -= it.current_y;
5146 else
5148 /* Last line has no newline */
5149 h -= line_bottom_y (&it);
5150 it.vpos++;
5153 /* Don't reserve space for extra line spacing of last line. */
5154 extra_line_spacing = it.max_extra_line_spacing;
5156 /* If we can't move down NLINES lines because we hit
5157 the end of the buffer, count in some empty lines. */
5158 if (it.vpos < nlines)
5160 nlines -= it.vpos;
5161 extra_line_spacing = it.extra_line_spacing;
5162 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5164 if (h <= 0)
5166 bidi_unshelve_cache (itdata, 0);
5167 return Qnil;
5170 /* Now find the new top line (starting position) of the window. */
5171 start_display (&it, w, pt);
5172 it.current_y = 0;
5173 move_it_vertically_backward (&it, h);
5175 /* If extra line spacing is present, we may move too far
5176 back. This causes the last line to be only partially
5177 visible (which triggers redisplay to recenter that line
5178 in the middle), so move forward.
5179 But ignore extra line spacing on last line, as it is not
5180 considered to be part of the visible height of the line.
5182 h += extra_line_spacing;
5183 while (-it.current_y > h)
5184 move_it_by_lines (&it, 1);
5186 charpos = IT_CHARPOS (it);
5187 bytepos = IT_BYTEPOS (it);
5189 bidi_unshelve_cache (itdata, 0);
5191 else
5193 struct position pos;
5195 iarg = max (iarg, this_scroll_margin);
5197 pos = *vmotion (PT, -iarg, w);
5198 charpos = pos.bufpos;
5199 bytepos = pos.bytepos;
5202 else
5204 struct position pos;
5205 int ht = window_internal_height (w);
5207 if (center_p)
5208 iarg = ht / 2;
5209 else if (iarg < 0)
5210 iarg += ht;
5212 /* Don't let it get into the margin at either top or bottom. */
5213 iarg = max (iarg, this_scroll_margin);
5214 iarg = min (iarg, ht - this_scroll_margin - 1);
5216 pos = *vmotion (PT, - iarg, w);
5217 charpos = pos.bufpos;
5218 bytepos = pos.bytepos;
5221 /* Set the new window start. */
5222 set_marker_both (w->start, w->buffer, charpos, bytepos);
5223 w->window_end_valid = Qnil;
5225 w->optional_new_start = Qt;
5227 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5228 w->start_at_line_beg = Qt;
5229 else
5230 w->start_at_line_beg = Qnil;
5232 set_buffer_internal (obuf);
5233 return Qnil;
5236 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5237 0, 1, 0,
5238 doc: /* Return the height in lines of the text display area of WINDOW.
5239 WINDOW defaults to the selected window.
5241 The return value does not include the mode line, any header line, nor
5242 any partial-height lines in the text display area. */)
5243 (Lisp_Object window)
5245 struct window *w = decode_window (window);
5246 int pixel_height = window_box_height (w);
5247 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5248 return make_number (line_height);
5253 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5254 1, 1, "P",
5255 doc: /* Position point relative to window.
5256 With no argument, position point at center of window.
5257 An argument specifies vertical position within the window;
5258 zero means top of window, negative means relative to bottom of window. */)
5259 (Lisp_Object arg)
5261 struct window *w = XWINDOW (selected_window);
5262 int lines, start;
5263 Lisp_Object window;
5264 #if 0
5265 int this_scroll_margin;
5266 #endif
5268 if (!(BUFFERP (w->buffer)
5269 && XBUFFER (w->buffer) == current_buffer))
5270 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5271 when passed below to set_marker_both. */
5272 error ("move-to-window-line called from unrelated buffer");
5274 window = selected_window;
5275 start = marker_position (w->start);
5276 if (start < BEGV || start > ZV)
5278 int height = window_internal_height (w);
5279 Fvertical_motion (make_number (- (height / 2)), window);
5280 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5281 w->start_at_line_beg = Fbolp ();
5282 w->force_start = Qt;
5284 else
5285 Fgoto_char (w->start);
5287 lines = displayed_window_lines (w);
5289 #if 0
5290 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5291 #endif
5293 if (NILP (arg))
5294 XSETFASTINT (arg, lines / 2);
5295 else
5297 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5299 if (iarg < 0)
5300 iarg = iarg + lines;
5302 #if 0 /* This code would prevent move-to-window-line from moving point
5303 to a place inside the scroll margins (which would cause the
5304 next redisplay to scroll). I wrote this code, but then concluded
5305 it is probably better not to install it. However, it is here
5306 inside #if 0 so as not to lose it. -- rms. */
5308 /* Don't let it get into the margin at either top or bottom. */
5309 iarg = max (iarg, this_scroll_margin);
5310 iarg = min (iarg, lines - this_scroll_margin - 1);
5311 #endif
5313 arg = make_number (iarg);
5316 /* Skip past a partially visible first line. */
5317 if (w->vscroll)
5318 XSETINT (arg, XINT (arg) + 1);
5320 return Fvertical_motion (arg, window);
5325 /***********************************************************************
5326 Window Configuration
5327 ***********************************************************************/
5329 struct save_window_data
5331 struct vectorlike_header header;
5332 Lisp_Object selected_frame;
5333 Lisp_Object current_window;
5334 Lisp_Object current_buffer;
5335 Lisp_Object minibuf_scroll_window;
5336 Lisp_Object minibuf_selected_window;
5337 Lisp_Object root_window;
5338 Lisp_Object focus_frame;
5339 /* A vector, each of whose elements is a struct saved_window
5340 for one window. */
5341 Lisp_Object saved_windows;
5343 /* All fields above are traced by the GC.
5344 From `fame-cols' down, the fields are ignored by the GC. */
5346 int frame_cols, frame_lines, frame_menu_bar_lines;
5347 int frame_tool_bar_lines;
5350 /* This is saved as a Lisp_Vector */
5351 struct saved_window
5353 struct vectorlike_header header;
5355 Lisp_Object window, buffer, start, pointm, mark;
5356 Lisp_Object left_col, top_line, total_cols, total_lines;
5357 Lisp_Object normal_cols, normal_lines;
5358 Lisp_Object hscroll, min_hscroll;
5359 Lisp_Object parent, prev;
5360 Lisp_Object start_at_line_beg;
5361 Lisp_Object display_table;
5362 Lisp_Object left_margin_cols, right_margin_cols;
5363 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5364 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5365 Lisp_Object splits, nest, window_parameters;
5368 #define SAVED_WINDOW_N(swv,n) \
5369 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5371 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5372 doc: /* Return t if OBJECT is a window-configuration object. */)
5373 (Lisp_Object object)
5375 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5378 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5379 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5380 (Lisp_Object config)
5382 register struct save_window_data *data;
5383 struct Lisp_Vector *saved_windows;
5385 CHECK_WINDOW_CONFIGURATION (config);
5387 data = (struct save_window_data *) XVECTOR (config);
5388 saved_windows = XVECTOR (data->saved_windows);
5389 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5392 DEFUN ("set-window-configuration", Fset_window_configuration,
5393 Sset_window_configuration, 1, 1, 0,
5394 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5395 CONFIGURATION must be a value previously returned
5396 by `current-window-configuration' (which see).
5397 If CONFIGURATION was made from a frame that is now deleted,
5398 only frame-independent values can be restored. In this case,
5399 the return value is nil. Otherwise the value is t. */)
5400 (Lisp_Object configuration)
5402 register struct save_window_data *data;
5403 struct Lisp_Vector *saved_windows;
5404 Lisp_Object new_current_buffer;
5405 Lisp_Object frame;
5406 Lisp_Object auto_buffer_name;
5407 FRAME_PTR f;
5408 EMACS_INT old_point = -1;
5410 CHECK_WINDOW_CONFIGURATION (configuration);
5412 data = (struct save_window_data *) XVECTOR (configuration);
5413 saved_windows = XVECTOR (data->saved_windows);
5415 new_current_buffer = data->current_buffer;
5416 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5417 new_current_buffer = Qnil;
5418 else
5420 if (XBUFFER (new_current_buffer) == current_buffer)
5421 /* The code further down "preserves point" by saving here PT in
5422 old_point and then setting it later back into PT. When the
5423 current-selected-window and the final-selected-window both show
5424 the current buffer, this suffers from the problem that the
5425 current PT is the window-point of the current-selected-window,
5426 while the final PT is the point of the final-selected-window, so
5427 this copy from one PT to the other would end up moving the
5428 window-point of the final-selected-window to the window-point of
5429 the current-selected-window. So we have to be careful which
5430 point of the current-buffer we copy into old_point. */
5431 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5432 && WINDOWP (selected_window)
5433 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5434 && !EQ (selected_window, data->current_window))
5435 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5436 else
5437 old_point = PT;
5438 else
5439 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5440 point in new_current_buffer as of the last time this buffer was
5441 used. This can be non-deterministic since it can be changed by
5442 things like jit-lock by mere temporary selection of some random
5443 window that happens to show this buffer.
5444 So if possible we want this arbitrary choice of "which point" to
5445 be the one from the to-be-selected-window so as to prevent this
5446 window's cursor from being copied from another window. */
5447 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5448 /* If current_window = selected_window, its point is in BUF_PT. */
5449 && !EQ (selected_window, data->current_window))
5450 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5451 else
5452 old_point = BUF_PT (XBUFFER (new_current_buffer));
5455 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5456 f = XFRAME (frame);
5458 /* If f is a dead frame, don't bother rebuilding its window tree.
5459 However, there is other stuff we should still try to do below. */
5460 if (FRAME_LIVE_P (f))
5462 Lisp_Object window;
5463 Lisp_Object dead_windows = Qnil;
5464 register struct window *w;
5465 register struct saved_window *p;
5466 struct window *root_window;
5467 struct window **leaf_windows;
5468 int n_leaf_windows;
5469 ptrdiff_t k;
5470 int i, n;
5472 /* If the frame has been resized since this window configuration was
5473 made, we change the frame to the size specified in the
5474 configuration, restore the configuration, and then resize it
5475 back. We keep track of the prevailing height in these variables. */
5476 int previous_frame_lines = FRAME_LINES (f);
5477 int previous_frame_cols = FRAME_COLS (f);
5478 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5479 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5481 /* The mouse highlighting code could get screwed up
5482 if it runs during this. */
5483 BLOCK_INPUT;
5485 if (data->frame_lines != previous_frame_lines
5486 || data->frame_cols != previous_frame_cols)
5487 change_frame_size (f, data->frame_lines,
5488 data->frame_cols, 0, 0, 0);
5489 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5490 if (data->frame_menu_bar_lines
5491 != previous_frame_menu_bar_lines)
5492 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5493 make_number (0));
5494 #ifdef HAVE_WINDOW_SYSTEM
5495 if (data->frame_tool_bar_lines
5496 != previous_frame_tool_bar_lines)
5497 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5498 make_number (0));
5499 #endif
5500 #endif
5502 /* "Swap out" point from the selected window's buffer
5503 into the window itself. (Normally the pointm of the selected
5504 window holds garbage.) We do this now, before
5505 restoring the window contents, and prevent it from
5506 being done later on when we select a new window. */
5507 if (! NILP (XWINDOW (selected_window)->buffer))
5509 w = XWINDOW (selected_window);
5510 set_marker_both (w->pointm,
5511 w->buffer,
5512 BUF_PT (XBUFFER (w->buffer)),
5513 BUF_PT_BYTE (XBUFFER (w->buffer)));
5516 windows_or_buffers_changed++;
5517 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5519 /* Problem: Freeing all matrices and later allocating them again
5520 is a serious redisplay flickering problem. What we would
5521 really like to do is to free only those matrices not reused
5522 below. */
5523 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5524 leaf_windows
5525 = (struct window **) alloca (count_windows (root_window)
5526 * sizeof (struct window *));
5527 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5529 /* Kludge Alert!
5530 Mark all windows now on frame as "deleted".
5531 Restoring the new configuration "undeletes" any that are in it.
5533 Save their current buffers in their height fields, since we may
5534 need it later, if a buffer saved in the configuration is now
5535 dead. */
5536 delete_all_subwindows (FRAME_ROOT_WINDOW (f));
5538 for (k = 0; k < saved_windows->header.size; k++)
5540 p = SAVED_WINDOW_N (saved_windows, k);
5541 window = p->window;
5542 w = XWINDOW (window);
5543 w->next = Qnil;
5545 if (!NILP (p->parent))
5546 w->parent = SAVED_WINDOW_N (saved_windows,
5547 XFASTINT (p->parent))->window;
5548 else
5549 w->parent = Qnil;
5551 if (!NILP (p->prev))
5553 w->prev = SAVED_WINDOW_N (saved_windows,
5554 XFASTINT (p->prev))->window;
5555 XWINDOW (w->prev)->next = p->window;
5557 else
5559 w->prev = Qnil;
5560 if (!NILP (w->parent))
5562 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5564 XWINDOW (w->parent)->vchild = p->window;
5565 XWINDOW (w->parent)->hchild = Qnil;
5567 else
5569 XWINDOW (w->parent)->hchild = p->window;
5570 XWINDOW (w->parent)->vchild = Qnil;
5575 /* If we squirreled away the buffer in the window's height,
5576 restore it now. */
5577 if (BUFFERP (w->total_lines))
5578 w->buffer = w->total_lines;
5579 w->left_col = p->left_col;
5580 w->top_line = p->top_line;
5581 w->total_cols = p->total_cols;
5582 w->total_lines = p->total_lines;
5583 w->normal_cols = p->normal_cols;
5584 w->normal_lines = p->normal_lines;
5585 w->hscroll = p->hscroll;
5586 w->min_hscroll = p->min_hscroll;
5587 w->display_table = p->display_table;
5588 w->left_margin_cols = p->left_margin_cols;
5589 w->right_margin_cols = p->right_margin_cols;
5590 w->left_fringe_width = p->left_fringe_width;
5591 w->right_fringe_width = p->right_fringe_width;
5592 w->fringes_outside_margins = p->fringes_outside_margins;
5593 w->scroll_bar_width = p->scroll_bar_width;
5594 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5595 w->dedicated = p->dedicated;
5596 w->splits = p->splits;
5597 w->nest = p->nest;
5598 w->window_parameters = p->window_parameters;
5599 XSETFASTINT (w->last_modified, 0);
5600 XSETFASTINT (w->last_overlay_modified, 0);
5602 /* Reinstall the saved buffer and pointers into it. */
5603 if (NILP (p->buffer))
5604 /* An internal window. */
5605 w->buffer = p->buffer;
5606 else if (!NILP (BVAR (XBUFFER (p->buffer), name)))
5607 /* If saved buffer is alive, install it. */
5609 w->buffer = p->buffer;
5610 w->start_at_line_beg = p->start_at_line_beg;
5611 set_marker_restricted (w->start, p->start, w->buffer);
5612 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5613 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5614 p->mark, w->buffer);
5616 /* As documented in Fcurrent_window_configuration, don't
5617 restore the location of point in the buffer which was
5618 current when the window configuration was recorded. */
5619 if (!EQ (p->buffer, new_current_buffer)
5620 && XBUFFER (p->buffer) == current_buffer)
5621 Fgoto_char (w->pointm);
5623 else if (!NILP (w->buffer) && !NILP (BVAR (XBUFFER (w->buffer), name)))
5624 /* Keep window's old buffer; make sure the markers are
5625 real. */
5627 /* Set window markers at start of visible range. */
5628 if (XMARKER (w->start)->buffer == 0)
5629 set_marker_restricted (w->start, make_number (0),
5630 w->buffer);
5631 if (XMARKER (w->pointm)->buffer == 0)
5632 set_marker_restricted_both (w->pointm, w->buffer,
5633 BUF_PT (XBUFFER (w->buffer)),
5634 BUF_PT_BYTE (XBUFFER (w->buffer)));
5635 w->start_at_line_beg = Qt;
5637 else if (STRINGP (auto_buffer_name =
5638 Fwindow_parameter (window, Qauto_buffer_name))
5639 && SCHARS (auto_buffer_name) != 0
5640 && !NILP (w->buffer = Fget_buffer_create (auto_buffer_name)))
5642 set_marker_restricted (w->start, make_number (0), w->buffer);
5643 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5644 w->start_at_line_beg = Qt;
5646 else
5647 /* Window has no live buffer, get one. */
5649 /* Get the buffer via other_buffer_safely in order to
5650 avoid showing an unimportant buffer and, if necessary, to
5651 recreate *scratch* in the course (part of Juanma's bs-show
5652 scenario from March 2011). */
5653 w->buffer = other_buffer_safely (Fcurrent_buffer ());
5654 /* This will set the markers to beginning of visible
5655 range. */
5656 set_marker_restricted (w->start, make_number (0), w->buffer);
5657 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5658 w->start_at_line_beg = Qt;
5659 if (!NILP (w->dedicated))
5660 /* Record this window as dead. */
5661 dead_windows = Fcons (window, dead_windows);
5662 /* Make sure window is no more dedicated. */
5663 w->dedicated = Qnil;
5667 FRAME_ROOT_WINDOW (f) = data->root_window;
5668 /* Arrange *not* to restore point in the buffer that was
5669 current when the window configuration was saved. */
5670 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5671 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5672 make_number (old_point),
5673 XWINDOW (data->current_window)->buffer);
5675 /* In the following call to `select-window', prevent "swapping out
5676 point" in the old selected window using the buffer that has
5677 been restored into it. We already swapped out that point from
5678 that window's old buffer. */
5679 select_window (data->current_window, Qnil, 1);
5680 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5681 = selected_window;
5683 if (NILP (data->focus_frame)
5684 || (FRAMEP (data->focus_frame)
5685 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5686 Fredirect_frame_focus (frame, data->focus_frame);
5688 /* Set the screen height to the value it had before this function. */
5689 if (previous_frame_lines != FRAME_LINES (f)
5690 || previous_frame_cols != FRAME_COLS (f))
5691 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5692 0, 0, 0);
5693 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5694 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5695 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5696 make_number (0));
5697 #ifdef HAVE_WINDOW_SYSTEM
5698 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5699 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5700 make_number (0));
5701 #endif
5702 #endif
5704 /* Now, free glyph matrices in windows that were not reused. */
5705 for (i = n = 0; i < n_leaf_windows; ++i)
5707 if (NILP (leaf_windows[i]->buffer))
5709 /* Assert it's not reused as a combination. */
5710 xassert (NILP (leaf_windows[i]->hchild)
5711 && NILP (leaf_windows[i]->vchild));
5712 free_window_matrices (leaf_windows[i]);
5714 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5715 ++n;
5718 adjust_glyphs (f);
5719 UNBLOCK_INPUT;
5721 /* Scan dead buffer windows. */
5722 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5724 window = XCAR (dead_windows);
5725 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5726 delete_deletable_window (window);
5729 /* Fselect_window will have made f the selected frame, so we
5730 reselect the proper frame here. Fhandle_switch_frame will change the
5731 selected window too, but that doesn't make the call to
5732 Fselect_window above totally superfluous; it still sets f's
5733 selected window. */
5734 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5735 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5737 run_window_configuration_change_hook (f);
5740 if (!NILP (new_current_buffer))
5741 Fset_buffer (new_current_buffer);
5743 Vminibuf_scroll_window = data->minibuf_scroll_window;
5744 minibuf_selected_window = data->minibuf_selected_window;
5746 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5750 /* Delete all subwindows reachable via the next, vchild, and hchild
5751 slots of WINDOW. */
5752 void
5753 delete_all_subwindows (Lisp_Object window)
5755 register struct window *w;
5757 w = XWINDOW (window);
5759 if (!NILP (w->next))
5760 /* Delete WINDOW's siblings (we traverse postorderly). */
5761 delete_all_subwindows (w->next);
5763 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5765 if (!NILP (w->vchild))
5767 delete_all_subwindows (w->vchild);
5768 w->vchild = Qnil;
5770 else if (!NILP (w->hchild))
5772 delete_all_subwindows (w->hchild);
5773 w->hchild = Qnil;
5775 else if (!NILP (w->buffer))
5777 unshow_buffer (w);
5778 unchain_marker (XMARKER (w->pointm));
5779 unchain_marker (XMARKER (w->start));
5780 w->buffer = Qnil;
5783 Vwindow_list = Qnil;
5786 static int
5787 count_windows (register struct window *window)
5789 register int count = 1;
5790 if (!NILP (window->next))
5791 count += count_windows (XWINDOW (window->next));
5792 if (!NILP (window->vchild))
5793 count += count_windows (XWINDOW (window->vchild));
5794 if (!NILP (window->hchild))
5795 count += count_windows (XWINDOW (window->hchild));
5796 return count;
5800 /* Fill vector FLAT with leaf windows under W, starting at index I.
5801 Value is last index + 1. */
5802 static int
5803 get_leaf_windows (struct window *w, struct window **flat, int i)
5805 while (w)
5807 if (!NILP (w->hchild))
5808 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5809 else if (!NILP (w->vchild))
5810 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5811 else
5812 flat[i++] = w;
5814 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5817 return i;
5821 /* Return a pointer to the glyph W's physical cursor is on. Value is
5822 null if W's current matrix is invalid, so that no meaningfull glyph
5823 can be returned. */
5824 struct glyph *
5825 get_phys_cursor_glyph (struct window *w)
5827 struct glyph_row *row;
5828 struct glyph *glyph;
5830 if (w->phys_cursor.vpos >= 0
5831 && w->phys_cursor.vpos < w->current_matrix->nrows
5832 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5833 row->enabled_p)
5834 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5835 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5836 else
5837 glyph = NULL;
5839 return glyph;
5843 static int
5844 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5846 register struct saved_window *p;
5847 register struct window *w;
5848 register Lisp_Object tem;
5850 for (;!NILP (window); window = w->next)
5852 p = SAVED_WINDOW_N (vector, i);
5853 w = XWINDOW (window);
5855 XSETFASTINT (w->temslot, i); i++;
5856 p->window = window;
5857 p->buffer = w->buffer;
5858 p->left_col = w->left_col;
5859 p->top_line = w->top_line;
5860 p->total_cols = w->total_cols;
5861 p->total_lines = w->total_lines;
5862 p->normal_cols = w->normal_cols;
5863 p->normal_lines = w->normal_lines;
5864 p->hscroll = w->hscroll;
5865 p->min_hscroll = w->min_hscroll;
5866 p->display_table = w->display_table;
5867 p->left_margin_cols = w->left_margin_cols;
5868 p->right_margin_cols = w->right_margin_cols;
5869 p->left_fringe_width = w->left_fringe_width;
5870 p->right_fringe_width = w->right_fringe_width;
5871 p->fringes_outside_margins = w->fringes_outside_margins;
5872 p->scroll_bar_width = w->scroll_bar_width;
5873 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5874 p->dedicated = w->dedicated;
5875 p->splits = w->splits;
5876 p->nest = w->nest;
5877 p->window_parameters = w->window_parameters;
5878 if (!NILP (w->buffer))
5880 /* Save w's value of point in the window configuration.
5881 If w is the selected window, then get the value of point
5882 from the buffer; pointm is garbage in the selected window. */
5883 if (EQ (window, selected_window))
5885 p->pointm = Fmake_marker ();
5886 set_marker_both (p->pointm, w->buffer,
5887 BUF_PT (XBUFFER (w->buffer)),
5888 BUF_PT_BYTE (XBUFFER (w->buffer)));
5890 else
5891 p->pointm = Fcopy_marker (w->pointm, Qnil);
5893 p->start = Fcopy_marker (w->start, Qnil);
5894 p->start_at_line_beg = w->start_at_line_beg;
5896 tem = BVAR (XBUFFER (w->buffer), mark);
5897 p->mark = Fcopy_marker (tem, Qnil);
5899 else
5901 p->pointm = Qnil;
5902 p->start = Qnil;
5903 p->mark = Qnil;
5904 p->start_at_line_beg = Qnil;
5907 if (NILP (w->parent))
5908 p->parent = Qnil;
5909 else
5910 p->parent = XWINDOW (w->parent)->temslot;
5912 if (NILP (w->prev))
5913 p->prev = Qnil;
5914 else
5915 p->prev = XWINDOW (w->prev)->temslot;
5917 if (!NILP (w->vchild))
5918 i = save_window_save (w->vchild, vector, i);
5919 if (!NILP (w->hchild))
5920 i = save_window_save (w->hchild, vector, i);
5923 return i;
5926 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5927 Scurrent_window_configuration, 0, 1, 0,
5928 doc: /* Return an object representing the current window configuration of FRAME.
5929 If FRAME is nil or omitted, use the selected frame.
5930 This describes the number of windows, their sizes and current buffers,
5931 and for each displayed buffer, where display starts, and the positions of
5932 point and mark. An exception is made for point in the current buffer:
5933 its value is -not- saved.
5934 This also records the currently selected frame, and FRAME's focus
5935 redirection (see `redirect-frame-focus'). */)
5936 (Lisp_Object frame)
5938 register Lisp_Object tem;
5939 register int n_windows;
5940 register struct save_window_data *data;
5941 register int i;
5942 FRAME_PTR f;
5944 if (NILP (frame))
5945 frame = selected_frame;
5946 CHECK_LIVE_FRAME (frame);
5947 f = XFRAME (frame);
5949 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5950 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
5951 PVEC_WINDOW_CONFIGURATION);
5953 data->frame_cols = FRAME_COLS (f);
5954 data->frame_lines = FRAME_LINES (f);
5955 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5956 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5957 data->selected_frame = selected_frame;
5958 data->current_window = FRAME_SELECTED_WINDOW (f);
5959 XSETBUFFER (data->current_buffer, current_buffer);
5960 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
5961 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
5962 data->root_window = FRAME_ROOT_WINDOW (f);
5963 data->focus_frame = FRAME_FOCUS_FRAME (f);
5964 tem = Fmake_vector (make_number (n_windows), Qnil);
5965 data->saved_windows = tem;
5966 for (i = 0; i < n_windows; i++)
5967 XVECTOR (tem)->contents[i]
5968 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
5969 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
5970 XSETWINDOW_CONFIGURATION (tem, data);
5971 return (tem);
5974 /***********************************************************************
5975 Marginal Areas
5976 ***********************************************************************/
5978 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
5979 2, 3, 0,
5980 doc: /* Set width of marginal areas of window WINDOW.
5981 If WINDOW is nil, set margins of the currently selected window.
5982 Second arg LEFT-WIDTH specifies the number of character cells to
5983 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
5984 does the same for the right marginal area. A nil width parameter
5985 means no margin. */)
5986 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
5988 struct window *w = decode_window (window);
5990 /* Translate negative or zero widths to nil.
5991 Margins that are too wide have to be checked elsewhere. */
5993 if (!NILP (left_width))
5995 CHECK_NUMBER (left_width);
5996 if (XINT (left_width) <= 0)
5997 left_width = Qnil;
6000 if (!NILP (right_width))
6002 CHECK_NUMBER (right_width);
6003 if (XINT (right_width) <= 0)
6004 right_width = Qnil;
6007 if (!EQ (w->left_margin_cols, left_width)
6008 || !EQ (w->right_margin_cols, right_width))
6010 w->left_margin_cols = left_width;
6011 w->right_margin_cols = right_width;
6013 adjust_window_margins (w);
6015 ++windows_or_buffers_changed;
6016 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6019 return Qnil;
6023 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6024 0, 1, 0,
6025 doc: /* Get width of marginal areas of window WINDOW.
6026 If WINDOW is omitted or nil, use the currently selected window.
6027 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6028 If a marginal area does not exist, its width will be returned
6029 as nil. */)
6030 (Lisp_Object window)
6032 struct window *w = decode_window (window);
6033 return Fcons (w->left_margin_cols, w->right_margin_cols);
6038 /***********************************************************************
6039 Fringes
6040 ***********************************************************************/
6042 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6043 2, 4, 0,
6044 doc: /* Set the fringe widths of window WINDOW.
6045 If WINDOW is nil, set the fringe widths of the currently selected
6046 window.
6047 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6048 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6049 fringe width. If a fringe width arg is nil, that means to use the
6050 frame's default fringe width. Default fringe widths can be set with
6051 the command `set-fringe-style'.
6052 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6053 outside of the display margins. By default, fringes are drawn between
6054 display marginal areas and the text area. */)
6055 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6057 struct window *w = decode_window (window);
6059 if (!NILP (left_width))
6060 CHECK_NATNUM (left_width);
6061 if (!NILP (right_width))
6062 CHECK_NATNUM (right_width);
6064 /* Do nothing on a tty. */
6065 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6066 && (!EQ (w->left_fringe_width, left_width)
6067 || !EQ (w->right_fringe_width, right_width)
6068 || !EQ (w->fringes_outside_margins, outside_margins)))
6070 w->left_fringe_width = left_width;
6071 w->right_fringe_width = right_width;
6072 w->fringes_outside_margins = outside_margins;
6074 adjust_window_margins (w);
6076 clear_glyph_matrix (w->current_matrix);
6077 w->window_end_valid = Qnil;
6079 ++windows_or_buffers_changed;
6080 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6083 return Qnil;
6087 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6088 0, 1, 0,
6089 doc: /* Get width of fringes of window WINDOW.
6090 If WINDOW is omitted or nil, use the currently selected window.
6091 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6092 (Lisp_Object window)
6094 struct window *w = decode_window (window);
6096 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6097 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6098 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6099 ? Qt : Qnil), Qnil)));
6104 /***********************************************************************
6105 Scroll bars
6106 ***********************************************************************/
6108 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6109 Sset_window_scroll_bars, 2, 4, 0,
6110 doc: /* Set width and type of scroll bars of window WINDOW.
6111 If window is nil, set scroll bars of the currently selected window.
6112 Second parameter WIDTH specifies the pixel width for the scroll bar;
6113 this is automatically adjusted to a multiple of the frame column width.
6114 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6115 bar: left, right, or nil.
6116 If WIDTH is nil, use the frame's scroll-bar width.
6117 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6118 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6119 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6121 struct window *w = decode_window (window);
6123 if (!NILP (width))
6125 CHECK_NATNUM (width);
6127 if (XINT (width) == 0)
6128 vertical_type = Qnil;
6131 if (!(NILP (vertical_type)
6132 || EQ (vertical_type, Qleft)
6133 || EQ (vertical_type, Qright)
6134 || EQ (vertical_type, Qt)))
6135 error ("Invalid type of vertical scroll bar");
6137 if (!EQ (w->scroll_bar_width, width)
6138 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6140 w->scroll_bar_width = width;
6141 w->vertical_scroll_bar_type = vertical_type;
6143 adjust_window_margins (w);
6145 clear_glyph_matrix (w->current_matrix);
6146 w->window_end_valid = Qnil;
6148 ++windows_or_buffers_changed;
6149 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6152 return Qnil;
6156 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6157 0, 1, 0,
6158 doc: /* Get width and type of scroll bars of window WINDOW.
6159 If WINDOW is omitted or nil, use the currently selected window.
6160 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6161 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6162 value. */)
6163 (Lisp_Object window)
6165 struct window *w = decode_window (window);
6166 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6167 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6168 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6169 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6170 Fcons (w->vertical_scroll_bar_type,
6171 Fcons (Qnil, Qnil))));
6176 /***********************************************************************
6177 Smooth scrolling
6178 ***********************************************************************/
6180 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6181 doc: /* Return the amount by which WINDOW is scrolled vertically.
6182 Use the selected window if WINDOW is nil or omitted.
6183 Normally, value is a multiple of the canonical character height of WINDOW;
6184 optional second arg PIXELS-P means value is measured in pixels. */)
6185 (Lisp_Object window, Lisp_Object pixels_p)
6187 Lisp_Object result;
6188 struct frame *f;
6189 struct window *w;
6191 if (NILP (window))
6192 window = selected_window;
6193 else
6194 CHECK_WINDOW (window);
6195 w = XWINDOW (window);
6196 f = XFRAME (w->frame);
6198 if (FRAME_WINDOW_P (f))
6199 result = (NILP (pixels_p)
6200 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6201 : make_number (-w->vscroll));
6202 else
6203 result = make_number (0);
6204 return result;
6208 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6209 2, 3, 0,
6210 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6211 WINDOW nil means use the selected window. Normally, VSCROLL is a
6212 non-negative multiple of the canonical character height of WINDOW;
6213 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6214 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6215 corresponds to an integral number of pixels. The return value is the
6216 result of this rounding.
6217 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6218 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6220 struct window *w;
6221 struct frame *f;
6223 if (NILP (window))
6224 window = selected_window;
6225 else
6226 CHECK_WINDOW (window);
6227 CHECK_NUMBER_OR_FLOAT (vscroll);
6229 w = XWINDOW (window);
6230 f = XFRAME (w->frame);
6232 if (FRAME_WINDOW_P (f))
6234 int old_dy = w->vscroll;
6236 w->vscroll = - (NILP (pixels_p)
6237 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6238 : XFLOATINT (vscroll));
6239 w->vscroll = min (w->vscroll, 0);
6241 if (w->vscroll != old_dy)
6243 /* Adjust glyph matrix of the frame if the virtual display
6244 area becomes larger than before. */
6245 if (w->vscroll < 0 && w->vscroll < old_dy)
6246 adjust_glyphs (f);
6248 /* Prevent redisplay shortcuts. */
6249 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6253 return Fwindow_vscroll (window, pixels_p);
6257 /* Call FN for all leaf windows on frame F. FN is called with the
6258 first argument being a pointer to the leaf window, and with
6259 additional argument USER_DATA. Stops when FN returns 0. */
6261 static void
6262 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6263 void *user_data)
6265 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6266 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6267 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6271 /* Helper function for foreach_window. Call FN for all leaf windows
6272 reachable from W. FN is called with the first argument being a
6273 pointer to the leaf window, and with additional argument USER_DATA.
6274 Stop when FN returns 0. Value is 0 if stopped by FN. */
6276 static int
6277 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6279 int cont;
6281 for (cont = 1; w && cont;)
6283 if (!NILP (w->hchild))
6284 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6285 else if (!NILP (w->vchild))
6286 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6287 else
6288 cont = fn (w, user_data);
6290 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6293 return cont;
6297 /* Freeze or unfreeze the window start of W unless it is a
6298 mini-window or the selected window. FREEZE_P non-null means freeze
6299 the window start. */
6301 static int
6302 freeze_window_start (struct window *w, void *freeze_p)
6304 if (MINI_WINDOW_P (w)
6305 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6306 && (w == XWINDOW (selected_window)
6307 || (MINI_WINDOW_P (XWINDOW (selected_window))
6308 && ! NILP (Vminibuf_scroll_window)
6309 && w == XWINDOW (Vminibuf_scroll_window)))))
6310 freeze_p = NULL;
6312 w->frozen_window_start_p = freeze_p != NULL;
6313 return 1;
6317 /* Freeze or unfreeze the window starts of all leaf windows on frame
6318 F, except the selected window and a mini-window. FREEZE_P non-zero
6319 means freeze the window start. */
6321 void
6322 freeze_window_starts (struct frame *f, int freeze_p)
6324 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6328 /***********************************************************************
6329 Initialization
6330 ***********************************************************************/
6332 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6333 describe the same state of affairs. This is used by Fequal.
6335 ignore_positions non-zero means ignore non-matching scroll positions
6336 and the like.
6338 This ignores a couple of things like the dedicatedness status of
6339 window, splits, nest and the like. This might have to be fixed. */
6342 compare_window_configurations (Lisp_Object configuration1, Lisp_Object configuration2, int ignore_positions)
6344 register struct save_window_data *d1, *d2;
6345 struct Lisp_Vector *sws1, *sws2;
6346 ptrdiff_t i;
6348 CHECK_WINDOW_CONFIGURATION (configuration1);
6349 CHECK_WINDOW_CONFIGURATION (configuration2);
6351 d1 = (struct save_window_data *) XVECTOR (configuration1);
6352 d2 = (struct save_window_data *) XVECTOR (configuration2);
6353 sws1 = XVECTOR (d1->saved_windows);
6354 sws2 = XVECTOR (d2->saved_windows);
6356 /* Frame settings must match. */
6357 if (d1->frame_cols != d2->frame_cols
6358 || d1->frame_lines != d2->frame_lines
6359 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6360 || !EQ (d1->selected_frame, d2->selected_frame)
6361 || !EQ (d1->current_buffer, d2->current_buffer)
6362 || (!ignore_positions
6363 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6364 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6365 || !EQ (d1->focus_frame, d2->focus_frame)
6366 /* Verify that the two configurations have the same number of windows. */
6367 || sws1->header.size != sws2->header.size)
6368 return 0;
6370 for (i = 0; i < sws1->header.size; i++)
6372 struct saved_window *sw1, *sw2;
6374 sw1 = SAVED_WINDOW_N (sws1, i);
6375 sw2 = SAVED_WINDOW_N (sws2, i);
6377 if (
6378 /* The "current" windows in the two configurations must
6379 correspond to each other. */
6380 EQ (d1->current_window, sw1->window)
6381 != EQ (d2->current_window, sw2->window)
6382 /* Windows' buffers must match. */
6383 || !EQ (sw1->buffer, sw2->buffer)
6384 || !EQ (sw1->left_col, sw2->left_col)
6385 || !EQ (sw1->top_line, sw2->top_line)
6386 || !EQ (sw1->total_cols, sw2->total_cols)
6387 || !EQ (sw1->total_lines, sw2->total_lines)
6388 || !EQ (sw1->display_table, sw2->display_table)
6389 /* The next two disjuncts check the window structure for
6390 equality. */
6391 || !EQ (sw1->parent, sw2->parent)
6392 || !EQ (sw1->prev, sw2->prev)
6393 || (!ignore_positions
6394 && (!EQ (sw1->hscroll, sw2->hscroll)
6395 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6396 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6397 || NILP (Fequal (sw1->start, sw2->start))
6398 || NILP (Fequal (sw1->pointm, sw2->pointm))
6399 || NILP (Fequal (sw1->mark, sw2->mark))))
6400 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6401 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6402 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6403 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6404 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6405 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6406 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6407 return 0;
6410 return 1;
6413 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6414 Scompare_window_configurations, 2, 2, 0,
6415 doc: /* Compare two window configurations as regards the structure of windows.
6416 This function ignores details such as the values of point and mark
6417 and scrolling positions. */)
6418 (Lisp_Object x, Lisp_Object y)
6420 if (compare_window_configurations (x, y, 1))
6421 return Qt;
6422 return Qnil;
6425 void
6426 init_window_once (void)
6428 struct frame *f = make_initial_frame ();
6429 XSETFRAME (selected_frame, f);
6430 Vterminal_frame = selected_frame;
6431 minibuf_window = f->minibuffer_window;
6432 selected_window = f->selected_window;
6433 last_nonminibuf_frame = f;
6435 window_initialized = 1;
6438 void
6439 init_window (void)
6441 Vwindow_list = Qnil;
6444 void
6445 syms_of_window (void)
6447 DEFSYM (Qscroll_up, "scroll-up");
6448 DEFSYM (Qscroll_down, "scroll-down");
6449 DEFSYM (Qscroll_command, "scroll-command");
6451 Fput (Qscroll_up, Qscroll_command, Qt);
6452 Fput (Qscroll_down, Qscroll_command, Qt);
6454 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6455 DEFSYM (Qwindowp, "windowp");
6456 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6457 DEFSYM (Qwindow_live_p, "window-live-p");
6458 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6459 DEFSYM (Qdelete_window, "delete-window");
6460 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6461 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6462 DEFSYM (Qsafe, "safe");
6463 DEFSYM (Qdisplay_buffer, "display-buffer");
6464 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6465 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6466 DEFSYM (Qget_mru_window, "get-mru-window");
6467 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6468 DEFSYM (Qabove, "above");
6469 DEFSYM (Qbelow, "below");
6470 DEFSYM (Qauto_buffer_name, "auto-buffer-name");
6472 staticpro (&Vwindow_list);
6474 minibuf_selected_window = Qnil;
6475 staticpro (&minibuf_selected_window);
6477 window_scroll_pixel_based_preserve_x = -1;
6478 window_scroll_pixel_based_preserve_y = -1;
6479 window_scroll_preserve_hpos = -1;
6480 window_scroll_preserve_vpos = -1;
6482 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6483 doc: /* Non-nil means call as function to display a help buffer.
6484 The function is called with one argument, the buffer to be displayed.
6485 Used by `with-output-to-temp-buffer'.
6486 If this function is used, then it must do the entire job of showing
6487 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6488 Vtemp_buffer_show_function = Qnil;
6490 DEFVAR_LISP ("temp-buffer-show-specifiers", Vtemp_buffer_show_specifiers,
6491 doc: /* Buffer display specifiers used by `with-output-to-temp-buffer'.
6492 These specifiers are passed by `with-output-to-temp-buffer' as second
6493 argument to `display-buffer'. Applications should only let-bind this
6494 around a call to `with-output-to-temp-buffer'.
6496 For a description of buffer display specifiers see the variable
6497 `display-buffer-alist'. */);
6498 Vtemp_buffer_show_specifiers = Qnil;
6500 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6501 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6502 Vminibuf_scroll_window = Qnil;
6504 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6505 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6506 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6507 is displayed in the `mode-line' face. */);
6508 mode_line_in_non_selected_windows = 1;
6510 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6511 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6512 Vother_window_scroll_buffer = Qnil;
6514 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6515 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6516 auto_window_vscroll_p = 1;
6518 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6519 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6520 next_screen_context_lines = 2;
6522 DEFVAR_LISP ("scroll-preserve-screen-position",
6523 Vscroll_preserve_screen_position,
6524 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6525 A value of nil means point does not keep its screen position except
6526 at the scroll margin or window boundary respectively.
6527 A value of t means point keeps its screen position if the scroll
6528 command moved it vertically out of the window, e.g. when scrolling
6529 by full screens.
6530 Any other value means point always keeps its screen position.
6531 Scroll commands should have the `scroll-command' property
6532 on their symbols to be controlled by this variable. */);
6533 Vscroll_preserve_screen_position = Qnil;
6535 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6536 doc: /* Type of marker to use for `window-point'. */);
6537 Vwindow_point_insertion_type = Qnil;
6539 DEFVAR_LISP ("window-configuration-change-hook",
6540 Vwindow_configuration_change_hook,
6541 doc: /* Functions to call when window configuration changes.
6542 The buffer-local part is run once per window, with the relevant window
6543 selected; while the global part is run only once for the modified frame,
6544 with the relevant frame selected. */);
6545 Vwindow_configuration_change_hook = Qnil;
6547 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6548 doc: /* If non-nil, then the `recenter' command with a nil argument
6549 will redraw the entire frame; the special value `tty' causes the
6550 frame to be redrawn only if it is a tty frame. */);
6551 Vrecenter_redisplay = Qtty;
6553 DEFVAR_LISP ("window-splits", Vwindow_splits,
6554 doc: /* Non-nil means splitting windows is handled specially.
6555 If this variable is nil, splitting a window gets the entire screen space
6556 for displaying the new window from the window to split. If this
6557 variable is non-nil, splitting a window may resize all windows in the
6558 same combination. This also allows to split a window that is otherwise
6559 too small or of fixed size.
6561 The value of this variable is also assigned to the split status of the
6562 new window and, provided the old and new window form a new combination,
6563 to the window that was split as well. The split status of a window can
6564 be retrieved with the function `window-splits' and altered by the
6565 function `set-window-splits'.
6567 If the value of the variable `window-nest' is non-nil, the space for the
6568 new window is exclusively taken from the window that shall be split, but
6569 the split status of the window that is split as well as that of the new
6570 window are still set to the value of this variable. */);
6571 Vwindow_splits = Qnil;
6573 DEFVAR_LISP ("window-nest", Vwindow_nest,
6574 doc: /* Non-nil means splitting a window makes a new parent window.
6575 If this variable is nil, splitting a window will create a new parent
6576 window only if the window has no parent window or the window shall
6577 become a combination orthogonal to the one it it is part of.
6579 If this variable is non-nil, splitting a window always creates a new
6580 parent window. If all splits behave this way, each frame's window tree
6581 is a binary tree and every window but the frame's root window has
6582 exactly one sibling.
6584 The value of this variable is also assigned to the nest status of the
6585 new parent window. The nest status of a window can be retrieved via the
6586 function `window-nest' and altered by the function `set-window-nest'. */);
6587 Vwindow_nest = Qnil;
6589 defsubr (&Sselected_window);
6590 defsubr (&Sminibuffer_window);
6591 defsubr (&Swindow_minibuffer_p);
6592 defsubr (&Swindowp);
6593 defsubr (&Swindow_live_p);
6594 defsubr (&Swindow_frame);
6595 defsubr (&Sframe_root_window);
6596 defsubr (&Sframe_first_window);
6597 defsubr (&Sframe_selected_window);
6598 defsubr (&Sset_frame_selected_window);
6599 defsubr (&Spos_visible_in_window_p);
6600 defsubr (&Swindow_line_height);
6601 defsubr (&Swindow_buffer);
6602 defsubr (&Swindow_parent);
6603 defsubr (&Swindow_top_child);
6604 defsubr (&Swindow_left_child);
6605 defsubr (&Swindow_next_sibling);
6606 defsubr (&Swindow_prev_sibling);
6607 defsubr (&Swindow_splits);
6608 defsubr (&Sset_window_splits);
6609 defsubr (&Swindow_nest);
6610 defsubr (&Sset_window_nest);
6611 defsubr (&Swindow_use_time);
6612 defsubr (&Swindow_top_line);
6613 defsubr (&Swindow_left_column);
6614 defsubr (&Swindow_total_size);
6615 defsubr (&Swindow_normal_size);
6616 defsubr (&Swindow_new_total);
6617 defsubr (&Swindow_new_normal);
6618 defsubr (&Sset_window_new_total);
6619 defsubr (&Sset_window_new_normal);
6620 defsubr (&Swindow_resize_apply);
6621 defsubr (&Swindow_body_size);
6622 defsubr (&Swindow_hscroll);
6623 defsubr (&Sset_window_hscroll);
6624 defsubr (&Swindow_redisplay_end_trigger);
6625 defsubr (&Sset_window_redisplay_end_trigger);
6626 defsubr (&Swindow_edges);
6627 defsubr (&Swindow_pixel_edges);
6628 defsubr (&Swindow_absolute_pixel_edges);
6629 defsubr (&Swindow_inside_edges);
6630 defsubr (&Swindow_inside_pixel_edges);
6631 defsubr (&Swindow_inside_absolute_pixel_edges);
6632 defsubr (&Scoordinates_in_window_p);
6633 defsubr (&Swindow_at);
6634 defsubr (&Swindow_point);
6635 defsubr (&Swindow_start);
6636 defsubr (&Swindow_end);
6637 defsubr (&Sset_window_point);
6638 defsubr (&Sset_window_start);
6639 defsubr (&Swindow_dedicated_p);
6640 defsubr (&Sset_window_dedicated_p);
6641 defsubr (&Swindow_display_table);
6642 defsubr (&Sset_window_display_table);
6643 defsubr (&Snext_window);
6644 defsubr (&Sprevious_window);
6645 defsubr (&Sget_buffer_window);
6646 defsubr (&Sdelete_other_windows_internal);
6647 defsubr (&Sdelete_window_internal);
6648 defsubr (&Sresize_mini_window_internal);
6649 defsubr (&Sset_window_buffer);
6650 defsubr (&Srun_window_configuration_change_hook);
6651 defsubr (&Sselect_window);
6652 defsubr (&Sforce_window_update);
6653 defsubr (&Stemp_output_buffer_show);
6654 defsubr (&Ssplit_window_internal);
6655 defsubr (&Sscroll_up);
6656 defsubr (&Sscroll_down);
6657 defsubr (&Sscroll_left);
6658 defsubr (&Sscroll_right);
6659 defsubr (&Sother_window_for_scrolling);
6660 defsubr (&Sscroll_other_window);
6661 defsubr (&Sminibuffer_selected_window);
6662 defsubr (&Srecenter);
6663 defsubr (&Swindow_text_height);
6664 defsubr (&Smove_to_window_line);
6665 defsubr (&Swindow_configuration_p);
6666 defsubr (&Swindow_configuration_frame);
6667 defsubr (&Sset_window_configuration);
6668 defsubr (&Scurrent_window_configuration);
6669 defsubr (&Sset_window_margins);
6670 defsubr (&Swindow_margins);
6671 defsubr (&Sset_window_fringes);
6672 defsubr (&Swindow_fringes);
6673 defsubr (&Sset_window_scroll_bars);
6674 defsubr (&Swindow_scroll_bars);
6675 defsubr (&Swindow_vscroll);
6676 defsubr (&Sset_window_vscroll);
6677 defsubr (&Scompare_window_configurations);
6678 defsubr (&Swindow_list);
6679 defsubr (&Swindow_list_1);
6680 defsubr (&Swindow_prev_buffers);
6681 defsubr (&Sset_window_prev_buffers);
6682 defsubr (&Swindow_next_buffers);
6683 defsubr (&Sset_window_next_buffers);
6684 defsubr (&Swindow_parameters);
6685 defsubr (&Swindow_parameter);
6686 defsubr (&Sset_window_parameter);
6689 void
6690 keys_of_window (void)
6692 initial_define_key (control_x_map, '<', "scroll-left");
6693 initial_define_key (control_x_map, '>', "scroll-right");
6695 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6696 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6697 initial_define_key (meta_map, 'v', "scroll-down-command");