Consistently check windows for validity/liveness (Bug#11984, Bug#12025, Bug#12026).
[emacs.git] / src / window.c
blob7874105df1ef5d0f3eb1a0efb4fed847ad5d5ab5
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2012
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 "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
41 #ifdef HAVE_X_WINDOWS
42 #include "xterm.h"
43 #endif /* HAVE_X_WINDOWS */
44 #ifdef WINDOWSNT
45 #include "w32term.h"
46 #endif
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
50 #ifdef HAVE_NS
51 #include "nsterm.h"
52 #endif
54 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_valid_p;
55 static Lisp_Object Qwindow_configuration_p, Qrecord_window_buffer;
56 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
57 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
58 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
59 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
60 static Lisp_Object Qsafe, Qabove, Qbelow;
61 static Lisp_Object Qauto_buffer_name, Qclone_of;
63 static int displayed_window_lines (struct window *);
64 static int count_windows (struct window *);
65 static int get_leaf_windows (struct window *, struct window **, int);
66 static void window_scroll (Lisp_Object, EMACS_INT, int, int);
67 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
68 static void window_scroll_line_based (Lisp_Object, int, int, int);
69 static int freeze_window_start (struct window *, void *);
70 static Lisp_Object window_list (void);
71 static int add_window_to_list (struct window *, void *);
72 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
73 Lisp_Object);
74 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
75 Lisp_Object, int);
76 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
77 Lisp_Object *);
78 static void foreach_window (struct frame *,
79 int (* fn) (struct window *, void *),
80 void *);
81 static int foreach_window_1 (struct window *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
85 static int window_resize_check (struct window *, int);
86 static void window_resize_apply (struct window *, int);
87 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
89 /* This is the window in which the terminal's cursor should
90 be left when nothing is being done with it. This must
91 always be a leaf window, and its buffer is selected by
92 the top level editing loop at the end of each command.
94 This value is always the same as
95 FRAME_SELECTED_WINDOW (selected_frame). */
96 Lisp_Object selected_window;
98 /* A list of all windows for use by next_window and Fwindow_list.
99 Functions creating or deleting windows should invalidate this cache
100 by setting it to nil. */
101 Lisp_Object Vwindow_list;
103 /* The mini-buffer window of the selected frame.
104 Note that you cannot test for mini-bufferness of an arbitrary window
105 by comparing against this; but you can test for mini-bufferness of
106 the selected window. */
107 Lisp_Object minibuf_window;
109 /* Non-nil means it is the window whose mode line should be
110 shown as the selected window when the minibuffer is selected. */
111 Lisp_Object minibuf_selected_window;
113 /* Hook run at end of temp_output_buffer_show. */
114 static Lisp_Object Qtemp_buffer_show_hook;
116 /* Incremented for each window created. */
117 static int sequence_number;
119 /* Nonzero after init_window_once has finished. */
120 static int window_initialized;
122 /* Hook to run when window config changes. */
123 static Lisp_Object Qwindow_configuration_change_hook;
125 /* Used by the function window_scroll_pixel_based */
126 static int window_scroll_pixel_based_preserve_x;
127 static int window_scroll_pixel_based_preserve_y;
129 /* Same for window_scroll_line_based. */
130 static EMACS_INT window_scroll_preserve_hpos;
131 static EMACS_INT window_scroll_preserve_vpos;
133 struct window *
134 decode_live_window (register Lisp_Object window)
136 if (NILP (window))
137 return XWINDOW (selected_window);
139 CHECK_LIVE_WINDOW (window);
140 return XWINDOW (window);
143 static struct window *
144 decode_any_window (register Lisp_Object window)
146 struct window *w;
148 if (NILP (window))
149 return XWINDOW (selected_window);
151 CHECK_WINDOW (window);
152 w = XWINDOW (window);
153 return w;
156 struct window *
157 decode_valid_window (register Lisp_Object window)
159 struct window *w;
161 if (NILP (window))
162 return XWINDOW (selected_window);
164 CHECK_VALID_WINDOW (window);
165 w = XWINDOW (window);
166 return w;
169 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
170 doc: /* Return t if OBJECT is a window and nil otherwise. */)
171 (Lisp_Object object)
173 return WINDOWP (object) ? Qt : Qnil;
176 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
177 doc: /* Return t if OBJECT is a valid window and nil otherwise.
178 A valid window is either a window that displays a buffer or an internal
179 window. Deleted windows are not live. */)
180 (Lisp_Object object)
182 return WINDOW_VALID_P (object) ? Qt : Qnil;
185 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
186 doc: /* Return t if OBJECT is a live window and nil otherwise.
187 A live window is a window that displays a buffer.
188 Internal windows and deleted windows are not live. */)
189 (Lisp_Object object)
191 return WINDOW_LIVE_P (object) ? Qt : Qnil;
194 /* Frames and windows. */
195 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 1, 1, 0,
196 doc: /* Return the frame that window WINDOW is on.
197 WINDOW must be a valid window and defaults to the selected one. */)
198 (Lisp_Object window)
200 return decode_valid_window (window)->frame;
203 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
204 doc: /* Return the root window of FRAME-OR-WINDOW.
205 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
206 With a frame argument, return that frame's root window.
207 With a window argument, return the root window of that window's frame. */)
208 (Lisp_Object frame_or_window)
210 Lisp_Object window;
212 if (NILP (frame_or_window))
213 window = SELECTED_FRAME ()->root_window;
214 else if (WINDOW_VALID_P (frame_or_window))
215 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
216 else
218 CHECK_LIVE_FRAME (frame_or_window);
219 window = XFRAME (frame_or_window)->root_window;
222 return window;
225 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
226 doc: /* Return the minibuffer window for frame FRAME.
227 If FRAME is omitted or nil, it defaults to the selected frame. */)
228 (Lisp_Object frame)
230 if (NILP (frame))
231 frame = selected_frame;
232 CHECK_LIVE_FRAME (frame);
233 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
236 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
237 Swindow_minibuffer_p, 0, 1, 0,
238 doc: /* Return non-nil if WINDOW is a minibuffer window.
239 WINDOW must be a valid window and defaults to the selected one. */)
240 (Lisp_Object window)
242 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
245 /* Don't move this to window.el - this must be a safe routine. */
246 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
247 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
248 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
249 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
250 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
251 the first window of that frame. */)
252 (Lisp_Object frame_or_window)
254 Lisp_Object window;
256 if (NILP (frame_or_window))
257 window = SELECTED_FRAME ()->root_window;
258 else if (WINDOW_VALID_P (frame_or_window))
259 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
260 else
262 CHECK_LIVE_FRAME (frame_or_window);
263 window = XFRAME (frame_or_window)->root_window;
266 while (NILP (XWINDOW (window)->buffer))
268 if (! NILP (XWINDOW (window)->hchild))
269 window = XWINDOW (window)->hchild;
270 else if (! NILP (XWINDOW (window)->vchild))
271 window = XWINDOW (window)->vchild;
272 else
273 abort ();
276 return window;
279 DEFUN ("frame-selected-window", Fframe_selected_window,
280 Sframe_selected_window, 0, 1, 0,
281 doc: /* Return the selected window of FRAME-OR-WINDOW.
282 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
283 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
284 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
285 return the selected window of that frame. */)
286 (Lisp_Object frame_or_window)
288 Lisp_Object window;
290 if (NILP (frame_or_window))
291 window = SELECTED_FRAME ()->selected_window;
292 else if (WINDOW_VALID_P (frame_or_window))
293 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
294 else
296 CHECK_LIVE_FRAME (frame_or_window);
297 window = XFRAME (frame_or_window)->selected_window;
300 return window;
303 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
304 Sset_frame_selected_window, 2, 3, 0,
305 doc: /* Set selected window of FRAME to WINDOW.
306 FRAME must be a live frame and defaults to the selected one. If FRAME
307 is the selected frame, this makes WINDOW the selected window. Optional
308 argument NORECORD non-nil means to neither change the order of recently
309 selected windows nor the buffer list. WINDOW must denote a live window.
310 Return WINDOW. */)
311 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
313 if (NILP (frame))
314 frame = selected_frame;
316 CHECK_LIVE_FRAME (frame);
317 CHECK_LIVE_WINDOW (window);
319 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
320 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
322 if (EQ (frame, selected_frame))
323 return Fselect_window (window, norecord);
324 else
325 return FSET (XFRAME (frame), selected_window, window);
328 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
329 doc: /* Return the selected window.
330 The selected window is the window in which the standard cursor for
331 selected windows appears and to which many commands apply. */)
332 (void)
334 return selected_window;
337 int window_select_count;
339 /* If select_window is called with inhibit_point_swap non-zero it will
340 not store point of the old selected window's buffer back into that
341 window's pointm slot. This is needed by Fset_window_configuration to
342 avoid that the display routine is called with selected_window set to
343 Qnil causing a subsequent crash. */
344 static Lisp_Object
345 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
347 register struct window *w;
348 register struct window *ow;
349 struct frame *sf;
351 CHECK_LIVE_WINDOW (window);
353 w = XWINDOW (window);
354 w->frozen_window_start_p = 0;
356 if (NILP (norecord))
358 w->use_time = ++window_select_count;
359 record_buffer (w->buffer);
362 if (EQ (window, selected_window) && !inhibit_point_swap)
363 return window;
365 sf = SELECTED_FRAME ();
366 if (XFRAME (WINDOW_FRAME (w)) != sf)
368 FSET (XFRAME (WINDOW_FRAME (w)), selected_window, window);
369 /* Use this rather than Fhandle_switch_frame
370 so that FRAME_FOCUS_FRAME is moved appropriately as we
371 move around in the state where a minibuffer in a separate
372 frame is active. */
373 Fselect_frame (WINDOW_FRAME (w), norecord);
374 /* Fselect_frame called us back so we've done all the work already. */
375 eassert (EQ (window, selected_window));
376 return window;
378 else
379 FSET (sf, selected_window, window);
381 /* Store the current buffer's actual point into the
382 old selected window. It belongs to that window,
383 and when the window is not selected, must be in the window. */
384 if (!inhibit_point_swap)
386 ow = XWINDOW (selected_window);
387 if (! NILP (ow->buffer))
388 set_marker_both (ow->pointm, ow->buffer,
389 BUF_PT (XBUFFER (ow->buffer)),
390 BUF_PT_BYTE (XBUFFER (ow->buffer)));
393 selected_window = window;
395 Fset_buffer (w->buffer);
397 BSET (XBUFFER (w->buffer), last_selected_window, window);
399 /* Go to the point recorded in the window.
400 This is important when the buffer is in more
401 than one window. It also matters when
402 redisplay_window has altered point after scrolling,
403 because it makes the change only in the window. */
405 register ptrdiff_t new_point = marker_position (w->pointm);
406 if (new_point < BEGV)
407 SET_PT (BEGV);
408 else if (new_point > ZV)
409 SET_PT (ZV);
410 else
411 SET_PT (new_point);
414 windows_or_buffers_changed++;
415 return window;
418 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
419 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
420 Also make WINDOW's buffer current and make WINDOW the frame's selected
421 window. Return WINDOW.
423 Optional second arg NORECORD non-nil means do not put this buffer at the
424 front of the buffer list and do not make this window the most recently
425 selected one.
427 Note that the main editor command loop sets the current buffer to the
428 buffer of the selected window before each command. */)
429 (register Lisp_Object window, Lisp_Object norecord)
431 return select_window (window, norecord, 0);
434 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
435 doc: /* Return the buffer displayed in window WINDOW.
436 If WINDOW is omitted or nil, it defaults to the selected window.
437 Return nil for an internal window or a deleted window. */)
438 (Lisp_Object window)
440 return decode_any_window (window)->buffer;
443 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
444 doc: /* Return the parent window of window WINDOW.
445 WINDOW must be a valid window and defaults to the selected one.
446 Return nil for a window with no parent (e.g. a root window). */)
447 (Lisp_Object window)
449 return decode_valid_window (window)->parent;
452 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 1, 1, 0,
453 doc: /* Return the topmost child window of window WINDOW.
454 WINDOW must be a valid window and defaults to the selected one.
455 Return nil if WINDOW is a live window (live windows have no children).
456 Return nil if WINDOW is an internal window whose children form a
457 horizontal combination. */)
458 (Lisp_Object window)
460 CHECK_WINDOW (window);
461 return decode_valid_window (window)->vchild;
464 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 1, 1, 0,
465 doc: /* Return the leftmost child window of window WINDOW.
466 WINDOW must be a valid window and defaults to the selected one.
467 Return nil if WINDOW is a live window (live windows have no children).
468 Return nil if WINDOW is an internal window whose children form a
469 vertical combination. */)
470 (Lisp_Object window)
472 CHECK_WINDOW (window);
473 return decode_valid_window (window)->hchild;
476 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
477 doc: /* Return the next sibling window of window WINDOW.
478 WINDOW must be a valid window and defaults to the selected one.
479 Return nil if WINDOW has no next sibling. */)
480 (Lisp_Object window)
482 return decode_valid_window (window)->next;
485 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
486 doc: /* Return the previous sibling window of window WINDOW.
487 WINDOW must be a valid window and defaults to the selected one.
488 Return nil if WINDOW has no previous sibling. */)
489 (Lisp_Object window)
491 return decode_valid_window (window)->prev;
494 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
495 doc: /* Return combination limit of window WINDOW.
496 WINDOW must be a valid window and defaults to the selected one.
497 If the return value is nil, child windows of WINDOW can be recombined with
498 WINDOW's siblings. A return value of t means that child windows of
499 WINDOW are never \(re-)combined with WINDOW's siblings. */)
500 (Lisp_Object window)
502 return decode_valid_window (window)->combination_limit;
505 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
506 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
507 WINDOW must be a valid window and defaults to the selected one.
508 If LIMIT is nil, child windows of WINDOW can be recombined with
509 WINDOW's siblings. LIMIT t means that child windows of WINDOW are
510 never \(re-)combined with WINDOW's siblings. Other values are reserved
511 for future use. */)
512 (Lisp_Object window, Lisp_Object limit)
514 return WSET (decode_valid_window (window), combination_limit, limit);
517 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
518 doc: /* Return the use time of window WINDOW.
519 WINDOW must be a live window and defaults to the selected one.
520 The window with the highest use time is the most recently selected
521 one. The window with the lowest use time is the least recently
522 selected one. */)
523 (Lisp_Object window)
525 return make_number (decode_live_window (window)->use_time);
528 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
529 doc: /* Return the total height, in lines, of window WINDOW.
530 WINDOW must be a valid window and defaults to the selected one.
532 The return value includes the mode line and header line, if any.
533 If WINDOW is an internal window, the total height is the height
534 of the screen areas spanned by its children.
536 On a graphical display, this total height is reported as an
537 integer multiple of the default character height. */)
538 (Lisp_Object window)
540 return decode_valid_window (window)->total_lines;
543 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
544 doc: /* Return the total width, in columns, of window WINDOW.
545 WINDOW must be a valid window and defaults to the selected one.
547 The return value includes any vertical dividers or scroll bars
548 belonging to WINDOW. If WINDOW is an internal window, the total width
549 is the width of the screen areas spanned by its children.
551 On a graphical display, this total width is reported as an
552 integer multiple of the default character width. */)
553 (Lisp_Object window)
555 return decode_valid_window (window)->total_cols;
558 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
559 doc: /* Return the new total size of window WINDOW.
560 WINDOW must be a valid window and defaults to the selected one. */)
561 (Lisp_Object window)
563 return decode_valid_window (window)->new_total;
566 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
567 doc: /* Return the normal height of window WINDOW.
568 WINDOW must be a valid window and defaults to the selected one.
569 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
570 (Lisp_Object window, Lisp_Object horizontal)
572 struct window *w = decode_valid_window (window);
574 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
577 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
578 doc: /* Return new normal size of window WINDOW.
579 WINDOW must be a valid window and defaults to the selected one. */)
580 (Lisp_Object window)
582 return decode_valid_window (window)->new_normal;
585 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
586 doc: /* Return left column of window WINDOW.
587 This is the distance, in columns, between the left edge of WINDOW and
588 the left edge of the frame's window area. For instance, the return
589 value is 0 if there is no window to the left of WINDOW.
591 WINDOW must be a valid window and defaults to the selected one. */)
592 (Lisp_Object window)
594 return decode_valid_window (window)->left_col;
597 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
598 doc: /* Return top line of window WINDOW.
599 This is the distance, in lines, between the top of WINDOW and the top
600 of the frame's window area. For instance, the return value is 0 if
601 there is no window above WINDOW.
603 WINDOW must be a valid window and defaults to the selected one. */)
604 (Lisp_Object window)
606 return decode_valid_window (window)->top_line;
609 /* Return the number of lines of W's body. Don't count any mode or
610 header line of W. */
612 static int
613 window_body_lines (struct window *w)
615 int height = XFASTINT (w->total_lines);
617 if (!MINI_WINDOW_P (w))
619 if (WINDOW_WANTS_MODELINE_P (w))
620 --height;
621 if (WINDOW_WANTS_HEADER_LINE_P (w))
622 --height;
625 return height;
628 /* Return the number of columns of W's body. Don't count columns
629 occupied by the scroll bar or the vertical bar separating W from its
630 right sibling. On window-systems don't count fringes or display
631 margins either. */
634 window_body_cols (struct window *w)
636 struct frame *f = XFRAME (WINDOW_FRAME (w));
637 int width = XINT (w->total_cols);
639 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
640 /* Scroll bars occupy a few columns. */
641 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
642 else if (!FRAME_WINDOW_P (f)
643 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
644 /* The column of `|' characters separating side-by-side windows
645 occupies one column only. */
646 width -= 1;
648 if (FRAME_WINDOW_P (f))
649 /* On window-systems, fringes and display margins cannot be
650 used for normal text. */
651 width -= (WINDOW_FRINGE_COLS (w)
652 + WINDOW_LEFT_MARGIN_COLS (w)
653 + WINDOW_RIGHT_MARGIN_COLS (w));
655 return width;
658 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
659 doc: /* Return the height, in lines, of WINDOW's text area.
660 WINDOW must be a live window and defaults to the selected one.
662 The returned height does not include the mode line or header line.
663 On a graphical display, the height is expressed as an integer multiple
664 of the default character height. If a line at the bottom of the text
665 area is only partially visible, that counts as a whole line; to
666 exclude partially-visible lines, use `window-text-height'. */)
667 (Lisp_Object window)
669 struct window *w = decode_live_window (window);
670 return make_number (window_body_lines (w));
673 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
674 doc: /* Return the width, in columns, of WINDOW's text area.
675 WINDOW must be a live window and defaults to the selected one.
677 The return value does not include any vertical dividers, fringe or
678 marginal areas, or scroll bars. On a graphical display, the width is
679 expressed as an integer multiple of the default character width. */)
680 (Lisp_Object window)
682 struct window *w = decode_live_window (window);
683 return make_number (window_body_cols (w));
686 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
687 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
688 WINDOW must be a live window and defaults to the selected one. */)
689 (Lisp_Object window)
691 return make_number (decode_live_window (window)->hscroll);
694 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
695 range, returning the new amount as a fixnum. */
696 static Lisp_Object
697 set_window_hscroll (struct window *w, EMACS_INT hscroll)
699 /* Horizontal scrolling has problems with large scroll amounts.
700 It's too slow with long lines, and even with small lines the
701 display can be messed up. For now, though, impose only the limits
702 required by the internal representation: horizontal scrolling must
703 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
704 (since it's stored in a ptrdiff_t). */
705 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
706 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
708 /* Prevent redisplay shortcuts when changing the hscroll. */
709 if (w->hscroll != new_hscroll)
710 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
712 w->hscroll = new_hscroll;
713 return make_number (new_hscroll);
716 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
717 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
718 WINDOW must be a live window and defaults to the selected one.
719 Clip the number to a reasonable value if out of range.
720 Return the new number. NCOL should be zero or positive.
722 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
723 window so that the location of point moves off-window. */)
724 (Lisp_Object window, Lisp_Object ncol)
726 struct window *w = decode_live_window (window);
728 CHECK_NUMBER (ncol);
729 return set_window_hscroll (w, XINT (ncol));
732 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
733 Swindow_redisplay_end_trigger, 0, 1, 0,
734 doc: /* Return WINDOW's redisplay end trigger value.
735 WINDOW must be a live window and defaults to the selected one.
736 See `set-window-redisplay-end-trigger' for more information. */)
737 (Lisp_Object window)
739 return decode_live_window (window)->redisplay_end_trigger;
742 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
743 Sset_window_redisplay_end_trigger, 2, 2, 0,
744 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
745 WINDOW must be a live window and defaults to the selected one. VALUE
746 should be a buffer position (typically a marker) or nil. If it is a
747 buffer position, then if redisplay in WINDOW reaches a position beyond
748 VALUE, the functions in `redisplay-end-trigger-functions' are called
749 with two arguments: WINDOW, and the end trigger value. Afterwards the
750 end-trigger value is reset to nil. */)
751 (register Lisp_Object window, Lisp_Object value)
753 return WSET (decode_live_window (window), redisplay_end_trigger, value);
756 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
757 doc: /* Return a list of the edge coordinates of WINDOW.
758 WINDOW must be a valid window and defaults to the selected one.
760 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
761 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
762 0 at top left corner of frame.
764 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
765 is one more than the bottommost row occupied by WINDOW. The edges
766 include the space used by WINDOW's scroll bar, display margins, fringes,
767 header line, and/or mode line. For the edges of just the text area, use
768 `window-inside-edges'. */)
769 (Lisp_Object window)
771 register struct window *w = decode_valid_window (window);
773 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
774 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
775 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
776 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
777 Qnil))));
780 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
781 doc: /* Return a list of the edge pixel coordinates of WINDOW.
782 WINDOW must be a valid window and defaults to the selected one.
784 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
785 0, 0 at the top left corner of the frame.
787 RIGHT is one more than the rightmost x position occupied by WINDOW.
788 BOTTOM is one more than the bottommost y position occupied by WINDOW.
789 The pixel edges include the space used by WINDOW's scroll bar, display
790 margins, fringes, header line, and/or mode line. For the pixel edges
791 of just the text area, use `window-inside-pixel-edges'. */)
792 (Lisp_Object window)
794 register struct window *w = decode_valid_window (window);
796 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
797 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
798 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
799 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
800 Qnil))));
803 static void
804 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
806 struct frame *f = XFRAME (w->frame);
807 *add_y = f->top_pos;
808 #ifdef FRAME_MENUBAR_HEIGHT
809 *add_y += FRAME_MENUBAR_HEIGHT (f);
810 #endif
811 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
812 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
813 #elif FRAME_TOOLBAR_HEIGHT
814 *add_y += FRAME_TOOLBAR_HEIGHT (f);
815 #endif
816 #ifdef FRAME_NS_TITLEBAR_HEIGHT
817 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
818 #endif
819 *add_x = f->left_pos;
820 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
821 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
822 #endif
825 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
826 Swindow_absolute_pixel_edges, 0, 1, 0,
827 doc: /* Return a list of the edge pixel coordinates of WINDOW.
828 WINDOW must be a valid window and defaults to the selected one.
830 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
831 0, 0 at the top left corner of the display.
833 RIGHT is one more than the rightmost x position occupied by WINDOW.
834 BOTTOM is one more than the bottommost y position occupied by WINDOW.
835 The pixel edges include the space used by WINDOW's scroll bar, display
836 margins, fringes, header line, and/or mode line. For the pixel edges
837 of just the text area, use `window-inside-absolute-pixel-edges'. */)
838 (Lisp_Object window)
840 register struct window *w = decode_valid_window (window);
841 int add_x, add_y;
842 calc_absolute_offset (w, &add_x, &add_y);
844 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
845 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
846 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
847 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
848 Qnil))));
851 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
852 doc: /* Return a list of the edge coordinates of WINDOW.
853 WINDOW must be a live window and defaults to the selected one.
855 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
856 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
857 0 at top left corner of frame.
859 RIGHT is one more than the rightmost column of WINDOW's text area.
860 BOTTOM is one more than the bottommost row of WINDOW's text area. The
861 inside edges do not include the space used by the WINDOW's scroll bar,
862 display margins, fringes, header line, and/or mode line. */)
863 (Lisp_Object window)
865 register struct window *w = decode_live_window (window);
867 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
868 + WINDOW_LEFT_MARGIN_COLS (w)
869 + WINDOW_LEFT_FRINGE_COLS (w)),
870 make_number (WINDOW_TOP_EDGE_LINE (w)
871 + WINDOW_HEADER_LINE_LINES (w)),
872 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
873 - WINDOW_RIGHT_MARGIN_COLS (w)
874 - WINDOW_RIGHT_FRINGE_COLS (w)),
875 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
876 - WINDOW_MODE_LINE_LINES (w)));
879 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
880 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
881 WINDOW must be a live window and defaults to the selected one.
883 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
884 (0,0) at the top left corner of the frame's window area.
886 RIGHT is one more than the rightmost x position of WINDOW's text area.
887 BOTTOM is one more than the bottommost y position of WINDOW's text area.
888 The inside edges do not include the space used by WINDOW's scroll bar,
889 display margins, fringes, header line, and/or mode line. */)
890 (Lisp_Object window)
892 register struct window *w = decode_live_window (window);
894 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
895 + WINDOW_LEFT_MARGIN_WIDTH (w)
896 + WINDOW_LEFT_FRINGE_WIDTH (w)),
897 make_number (WINDOW_TOP_EDGE_Y (w)
898 + WINDOW_HEADER_LINE_HEIGHT (w)),
899 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
900 - WINDOW_RIGHT_MARGIN_WIDTH (w)
901 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
902 make_number (WINDOW_BOTTOM_EDGE_Y (w)
903 - WINDOW_MODE_LINE_HEIGHT (w)));
906 DEFUN ("window-inside-absolute-pixel-edges",
907 Fwindow_inside_absolute_pixel_edges,
908 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
909 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
910 WINDOW must be a live window and defaults to the selected one.
912 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
913 (0,0) at the top left corner of the frame's window area.
915 RIGHT is one more than the rightmost x position of WINDOW's text area.
916 BOTTOM is one more than the bottommost y position of WINDOW's text area.
917 The inside edges do not include the space used by WINDOW's scroll bar,
918 display margins, fringes, header line, and/or mode line. */)
919 (Lisp_Object window)
921 register struct window *w = decode_live_window (window);
922 int add_x, add_y;
923 calc_absolute_offset (w, &add_x, &add_y);
925 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
926 + WINDOW_LEFT_MARGIN_WIDTH (w)
927 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
928 make_number (WINDOW_TOP_EDGE_Y (w)
929 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
930 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
931 - WINDOW_RIGHT_MARGIN_WIDTH (w)
932 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
933 make_number (WINDOW_BOTTOM_EDGE_Y (w)
934 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
937 /* Test if the character at column X, row Y is within window W.
938 If it is not, return ON_NOTHING;
939 if it is in the window's text area, return ON_TEXT;
940 if it is on the window's modeline, return ON_MODE_LINE;
941 if it is on the border between the window and its right sibling,
942 return ON_VERTICAL_BORDER.
943 if it is on a scroll bar, return ON_SCROLL_BAR.
944 if it is on the window's top line, return ON_HEADER_LINE;
945 if it is in left or right fringe of the window,
946 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
947 if it is in the marginal area to the left/right of the window,
948 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
950 X and Y are frame relative pixel coordinates. */
952 static enum window_part
953 coordinates_in_window (register struct window *w, int x, int y)
955 struct frame *f = XFRAME (WINDOW_FRAME (w));
956 enum window_part part;
957 int ux = FRAME_COLUMN_WIDTH (f);
958 int left_x = WINDOW_LEFT_EDGE_X (w);
959 int right_x = WINDOW_RIGHT_EDGE_X (w);
960 int top_y = WINDOW_TOP_EDGE_Y (w);
961 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
962 /* The width of the area where the vertical line can be dragged.
963 (Between mode lines for instance. */
964 int grabbable_width = ux;
965 int lmargin_width, rmargin_width, text_left, text_right;
967 /* Outside any interesting row or column? */
968 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
969 return ON_NOTHING;
971 /* On the mode line or header line? */
972 if ((WINDOW_WANTS_MODELINE_P (w)
973 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
974 && (part = ON_MODE_LINE))
975 || (WINDOW_WANTS_HEADER_LINE_P (w)
976 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
977 && (part = ON_HEADER_LINE)))
979 /* If it's under/over the scroll bar portion of the mode/header
980 line, say it's on the vertical line. That's to be able to
981 resize windows horizontally in case we're using toolkit scroll
982 bars. Note: If scrollbars are on the left, the window that
983 must be eventually resized is that on the left of WINDOW. */
984 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
985 && !WINDOW_LEFTMOST_P (w)
986 && eabs (x - left_x) < grabbable_width)
987 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
988 && !WINDOW_RIGHTMOST_P (w)
989 && eabs (x - right_x) < grabbable_width))
990 return ON_VERTICAL_BORDER;
991 else
992 return part;
995 /* In what's below, we subtract 1 when computing right_x because we
996 want the rightmost pixel, which is given by left_pixel+width-1. */
997 if (w->pseudo_window_p)
999 left_x = 0;
1000 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1002 else
1004 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1005 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1008 /* Outside any interesting column? */
1009 if (x < left_x || x > right_x)
1010 return ON_SCROLL_BAR;
1012 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1013 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1015 text_left = window_box_left (w, TEXT_AREA);
1016 text_right = text_left + window_box_width (w, TEXT_AREA);
1018 if (FRAME_WINDOW_P (f))
1020 if (!w->pseudo_window_p
1021 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1022 && !WINDOW_RIGHTMOST_P (w)
1023 && (eabs (x - right_x) < grabbable_width))
1024 return ON_VERTICAL_BORDER;
1026 /* Need to say "x > right_x" rather than >=, since on character
1027 terminals, the vertical line's x coordinate is right_x. */
1028 else if (!w->pseudo_window_p
1029 && !WINDOW_RIGHTMOST_P (w)
1030 && x > right_x - ux)
1031 return ON_VERTICAL_BORDER;
1033 if (x < text_left)
1035 if (lmargin_width > 0
1036 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1037 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1038 : (x < left_x + lmargin_width)))
1039 return ON_LEFT_MARGIN;
1041 return ON_LEFT_FRINGE;
1044 if (x >= text_right)
1046 if (rmargin_width > 0
1047 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1048 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1049 : (x >= right_x - rmargin_width)))
1050 return ON_RIGHT_MARGIN;
1052 return ON_RIGHT_FRINGE;
1055 /* Everything special ruled out - must be on text area */
1056 return ON_TEXT;
1059 /* Take X is the frame-relative pixel x-coordinate, and return the
1060 x-coordinate relative to part PART of window W. */
1062 window_relative_x_coord (struct window *w, enum window_part part, int x)
1064 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1066 switch (part)
1068 case ON_TEXT:
1069 return x - window_box_left (w, TEXT_AREA);
1071 case ON_LEFT_FRINGE:
1072 return x - left_x;
1074 case ON_RIGHT_FRINGE:
1075 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1077 case ON_LEFT_MARGIN:
1078 return (x - left_x
1079 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1080 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1082 case ON_RIGHT_MARGIN:
1083 return (x + 1
1084 - ((w->pseudo_window_p)
1085 ? WINDOW_TOTAL_WIDTH (w)
1086 : WINDOW_BOX_RIGHT_EDGE_X (w))
1087 + window_box_width (w, RIGHT_MARGIN_AREA)
1088 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1089 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1092 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1093 return 0;
1097 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1098 Scoordinates_in_window_p, 2, 2, 0,
1099 doc: /* Return non-nil if COORDINATES are in WINDOW.
1100 WINDOW must be a live window and defaults to the selected one.
1101 COORDINATES is a cons of the form (X . Y), X and Y being distances
1102 measured in characters from the upper-left corner of the frame.
1103 \(0 . 0) denotes the character in the upper left corner of the
1104 frame.
1105 If COORDINATES are in the text portion of WINDOW,
1106 the coordinates relative to the window are returned.
1107 If they are in the mode line of WINDOW, `mode-line' is returned.
1108 If they are in the top mode line of WINDOW, `header-line' is returned.
1109 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1110 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1111 If they are on the border between WINDOW and its right sibling,
1112 `vertical-line' is returned.
1113 If they are in the windows's left or right marginal areas, `left-margin'\n\
1114 or `right-margin' is returned. */)
1115 (register Lisp_Object coordinates, Lisp_Object window)
1117 struct window *w;
1118 struct frame *f;
1119 int x, y;
1120 Lisp_Object lx, ly;
1122 w = decode_live_window (window);
1123 f = XFRAME (w->frame);
1124 CHECK_CONS (coordinates);
1125 lx = Fcar (coordinates);
1126 ly = Fcdr (coordinates);
1127 CHECK_NUMBER_OR_FLOAT (lx);
1128 CHECK_NUMBER_OR_FLOAT (ly);
1129 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1130 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1132 switch (coordinates_in_window (w, x, y))
1134 case ON_NOTHING:
1135 return Qnil;
1137 case ON_TEXT:
1138 /* Convert X and Y to window relative pixel coordinates, and
1139 return the canonical char units. */
1140 x -= window_box_left (w, TEXT_AREA);
1141 y -= WINDOW_TOP_EDGE_Y (w);
1142 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1143 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1145 case ON_MODE_LINE:
1146 return Qmode_line;
1148 case ON_VERTICAL_BORDER:
1149 return Qvertical_line;
1151 case ON_HEADER_LINE:
1152 return Qheader_line;
1154 case ON_LEFT_FRINGE:
1155 return Qleft_fringe;
1157 case ON_RIGHT_FRINGE:
1158 return Qright_fringe;
1160 case ON_LEFT_MARGIN:
1161 return Qleft_margin;
1163 case ON_RIGHT_MARGIN:
1164 return Qright_margin;
1166 case ON_SCROLL_BAR:
1167 /* Historically we are supposed to return nil in this case. */
1168 return Qnil;
1170 default:
1171 abort ();
1176 /* Callback for foreach_window, used in window_from_coordinates.
1177 Check if window W contains coordinates specified by USER_DATA which
1178 is actually a pointer to a struct check_window_data CW.
1180 Check if window W contains coordinates *CW->x and *CW->y. If it
1181 does, return W in *CW->window, as Lisp_Object, and return in
1182 *CW->part the part of the window under coordinates *X,*Y. Return
1183 zero from this function to stop iterating over windows. */
1185 struct check_window_data
1187 Lisp_Object *window;
1188 int x, y;
1189 enum window_part *part;
1192 static int
1193 check_window_containing (struct window *w, void *user_data)
1195 struct check_window_data *cw = (struct check_window_data *) user_data;
1196 enum window_part found;
1197 int continue_p = 1;
1199 found = coordinates_in_window (w, cw->x, cw->y);
1200 if (found != ON_NOTHING)
1202 *cw->part = found;
1203 XSETWINDOW (*cw->window, w);
1204 continue_p = 0;
1207 return continue_p;
1211 /* Find the window containing frame-relative pixel position X/Y and
1212 return it as a Lisp_Object.
1214 If X, Y is on one of the window's special `window_part' elements,
1215 set *PART to the id of that element.
1217 If there is no window under X, Y return nil and leave *PART
1218 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1220 This function was previously implemented with a loop cycling over
1221 windows with Fnext_window, and starting with the frame's selected
1222 window. It turned out that this doesn't work with an
1223 implementation of next_window using Vwindow_list, because
1224 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1225 tree of F when this function is called asynchronously from
1226 note_mouse_highlight. The original loop didn't terminate in this
1227 case. */
1229 Lisp_Object
1230 window_from_coordinates (struct frame *f, int x, int y,
1231 enum window_part *part, int tool_bar_p)
1233 Lisp_Object window;
1234 struct check_window_data cw;
1235 enum window_part dummy;
1237 if (part == 0)
1238 part = &dummy;
1240 window = Qnil;
1241 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1242 foreach_window (f, check_window_containing, &cw);
1244 /* If not found above, see if it's in the tool bar window, if a tool
1245 bar exists. */
1246 if (NILP (window)
1247 && tool_bar_p
1248 && WINDOWP (f->tool_bar_window)
1249 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1250 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1251 != ON_NOTHING))
1253 *part = ON_TEXT;
1254 window = f->tool_bar_window;
1257 return window;
1260 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1261 doc: /* Return window containing coordinates X and Y on FRAME.
1262 FRAME must be a live frame and defaults to the selected one.
1263 The top left corner of the frame is considered to be row 0,
1264 column 0. */)
1265 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1267 struct frame *f;
1269 if (NILP (frame))
1270 frame = selected_frame;
1271 CHECK_LIVE_FRAME (frame);
1272 f = XFRAME (frame);
1274 /* Check that arguments are integers or floats. */
1275 CHECK_NUMBER_OR_FLOAT (x);
1276 CHECK_NUMBER_OR_FLOAT (y);
1278 return window_from_coordinates (f,
1279 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1280 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1281 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1282 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1283 0, 0);
1286 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1287 doc: /* Return current value of point in WINDOW.
1288 WINDOW must be a live window and defaults to the selected one.
1290 For a nonselected window, this is the value point would have
1291 if that window were selected.
1293 Note that, when WINDOW is the selected window and its buffer
1294 is also currently selected, the value returned is the same as (point).
1295 It would be more strictly correct to return the `top-level' value
1296 of point, outside of any save-excursion forms.
1297 But that is hard to define. */)
1298 (Lisp_Object window)
1300 register struct window *w = decode_live_window (window);
1302 if (w == XWINDOW (selected_window)
1303 && current_buffer == XBUFFER (w->buffer))
1304 return Fpoint ();
1305 return Fmarker_position (w->pointm);
1308 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1309 doc: /* Return position at which display currently starts in WINDOW.
1310 WINDOW must be a live window and defaults to the selected one.
1311 This is updated by redisplay or by calling `set-window-start'. */)
1312 (Lisp_Object window)
1314 return Fmarker_position (decode_live_window (window)->start);
1317 /* This is text temporarily removed from the doc string below.
1319 This function returns nil if the position is not currently known.
1320 That happens when redisplay is preempted and doesn't finish.
1321 If in that case you want to compute where the end of the window would
1322 have been if redisplay had finished, do this:
1323 (save-excursion
1324 (goto-char (window-start window))
1325 (vertical-motion (1- (window-height window)) window)
1326 (point))") */
1328 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1329 doc: /* Return position at which display currently ends in WINDOW.
1330 WINDOW must be a live window and defaults to the selected one.
1331 This is updated by redisplay, when it runs to completion.
1332 Simply changing the buffer text or setting `window-start'
1333 does not update this value.
1334 Return nil if there is no recorded value. (This can happen if the
1335 last redisplay of WINDOW was preempted, and did not finish.)
1336 If UPDATE is non-nil, compute the up-to-date position
1337 if it isn't already recorded. */)
1338 (Lisp_Object window, Lisp_Object update)
1340 Lisp_Object value;
1341 struct window *w = decode_live_window (window);
1342 Lisp_Object buf;
1343 struct buffer *b;
1345 buf = w->buffer;
1346 CHECK_BUFFER (buf);
1347 b = XBUFFER (buf);
1349 #if 0 /* This change broke some things. We should make it later. */
1350 /* If we don't know the end position, return nil.
1351 The user can compute it with vertical-motion if he wants to.
1352 It would be nicer to do it automatically,
1353 but that's so slow that it would probably bother people. */
1354 if (NILP (w->window_end_valid))
1355 return Qnil;
1356 #endif
1358 if (! NILP (update)
1359 && ! (! NILP (w->window_end_valid)
1360 && w->last_modified >= BUF_MODIFF (b)
1361 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b))
1362 && !noninteractive)
1364 struct text_pos startp;
1365 struct it it;
1366 struct buffer *old_buffer = NULL;
1367 void *itdata = NULL;
1369 /* Cannot use Fvertical_motion because that function doesn't
1370 cope with variable-height lines. */
1371 if (b != current_buffer)
1373 old_buffer = current_buffer;
1374 set_buffer_internal (b);
1377 /* In case W->start is out of the range, use something
1378 reasonable. This situation occurred when loading a file with
1379 `-l' containing a call to `rmail' with subsequent other
1380 commands. At the end, W->start happened to be BEG, while
1381 rmail had already narrowed the buffer. */
1382 if (XMARKER (w->start)->charpos < BEGV)
1383 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1384 else if (XMARKER (w->start)->charpos > ZV)
1385 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1386 else
1387 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1389 itdata = bidi_shelve_cache ();
1390 start_display (&it, w, startp);
1391 move_it_vertically (&it, window_box_height (w));
1392 if (it.current_y < it.last_visible_y)
1393 move_it_past_eol (&it);
1394 value = make_number (IT_CHARPOS (it));
1395 bidi_unshelve_cache (itdata, 0);
1397 if (old_buffer)
1398 set_buffer_internal (old_buffer);
1400 else
1401 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1403 return value;
1406 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1407 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1408 WINDOW must be a live window and defaults to the selected one.
1409 Return POS. */)
1410 (Lisp_Object window, Lisp_Object pos)
1412 register struct window *w = decode_live_window (window);
1414 CHECK_NUMBER_COERCE_MARKER (pos);
1415 if (w == XWINDOW (selected_window)
1416 && XBUFFER (w->buffer) == current_buffer)
1417 Fgoto_char (pos);
1418 else
1419 set_marker_restricted (w->pointm, pos, w->buffer);
1421 /* We have to make sure that redisplay updates the window to show
1422 the new value of point. */
1423 if (!EQ (window, selected_window))
1424 ++windows_or_buffers_changed;
1426 return pos;
1429 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1430 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1431 WINDOW must be a live window and defaults to the selected one. Return
1432 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1433 overriding motion of point in order to display at this exact start. */)
1434 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1436 register struct window *w = decode_live_window (window);
1438 CHECK_NUMBER_COERCE_MARKER (pos);
1439 set_marker_restricted (w->start, pos, w->buffer);
1440 /* this is not right, but much easier than doing what is right. */
1441 w->start_at_line_beg = 0;
1442 if (NILP (noforce))
1443 w->force_start = 1;
1444 w->update_mode_line = 1;
1445 w->last_modified = 0;
1446 w->last_overlay_modified = 0;
1447 if (!EQ (window, selected_window))
1448 windows_or_buffers_changed++;
1450 return pos;
1453 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1454 Spos_visible_in_window_p, 0, 3, 0,
1455 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1456 WINDOW must be a live window and defaults to the selected one.
1458 Return nil if that position is scrolled vertically out of view. If a
1459 character is only partially visible, nil is returned, unless the
1460 optional argument PARTIALLY is non-nil. If POS is only out of view
1461 because of horizontal scrolling, return non-nil. If POS is t, it
1462 specifies the position of the last visible glyph in WINDOW. POS
1463 defaults to point in WINDOW; WINDOW defaults to the selected window.
1465 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1466 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1467 where X and Y are the pixel coordinates relative to the top left corner
1468 of the window. The remaining elements are omitted if the character after
1469 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1470 off-window at the top and bottom of the row, ROWH is the height of the
1471 display row, and VPOS is the row number (0-based) containing POS. */)
1472 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1474 register struct window *w;
1475 register EMACS_INT posint;
1476 register struct buffer *buf;
1477 struct text_pos top;
1478 Lisp_Object in_window = Qnil;
1479 int rtop, rbot, rowh, vpos, fully_p = 1;
1480 int x, y;
1482 w = decode_live_window (window);
1483 buf = XBUFFER (w->buffer);
1484 SET_TEXT_POS_FROM_MARKER (top, w->start);
1486 if (EQ (pos, Qt))
1487 posint = -1;
1488 else if (!NILP (pos))
1490 CHECK_NUMBER_COERCE_MARKER (pos);
1491 posint = XINT (pos);
1493 else if (w == XWINDOW (selected_window))
1494 posint = PT;
1495 else
1496 posint = XMARKER (w->pointm)->charpos;
1498 /* If position is above window start or outside buffer boundaries,
1499 or if window start is out of range, position is not visible. */
1500 if ((EQ (pos, Qt)
1501 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1502 && CHARPOS (top) >= BUF_BEGV (buf)
1503 && CHARPOS (top) <= BUF_ZV (buf)
1504 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1505 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1506 in_window = Qt;
1508 if (!NILP (in_window) && !NILP (partially))
1510 Lisp_Object part = Qnil;
1511 if (!fully_p)
1512 part = list4 (make_number (rtop), make_number (rbot),
1513 make_number (rowh), make_number (vpos));
1514 in_window = Fcons (make_number (x),
1515 Fcons (make_number (y), part));
1518 return in_window;
1521 DEFUN ("window-line-height", Fwindow_line_height,
1522 Swindow_line_height, 0, 2, 0,
1523 doc: /* Return height in pixels of text line LINE in window WINDOW.
1524 WINDOW must be a live window and defaults to the selected one.
1526 Return height of current line if LINE is omitted or nil. Return height of
1527 header or mode line if LINE is `header-line' or `mode-line'.
1528 Otherwise, LINE is a text line number starting from 0. A negative number
1529 counts from the end of the window.
1531 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1532 in pixels of the visible part of the line, VPOS and YPOS are the
1533 vertical position in lines and pixels of the line, relative to the top
1534 of the first text line, and OFFBOT is the number of off-window pixels at
1535 the bottom of the text line. If there are off-window pixels at the top
1536 of the (first) text line, YPOS is negative.
1538 Return nil if window display is not up-to-date. In that case, use
1539 `pos-visible-in-window-p' to obtain the information. */)
1540 (Lisp_Object line, Lisp_Object window)
1542 register struct window *w;
1543 register struct buffer *b;
1544 struct glyph_row *row, *end_row;
1545 int max_y, crop, i;
1546 EMACS_INT n;
1548 w = decode_live_window (window);
1550 if (noninteractive || w->pseudo_window_p)
1551 return Qnil;
1553 CHECK_BUFFER (w->buffer);
1554 b = XBUFFER (w->buffer);
1556 /* Fail if current matrix is not up-to-date. */
1557 if (NILP (w->window_end_valid)
1558 || current_buffer->clip_changed
1559 || current_buffer->prevent_redisplay_optimizations_p
1560 || w->last_modified < BUF_MODIFF (b)
1561 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b))
1562 return Qnil;
1564 if (NILP (line))
1566 i = w->cursor.vpos;
1567 if (i < 0 || i >= w->current_matrix->nrows
1568 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1569 return Qnil;
1570 max_y = window_text_bottom_y (w);
1571 goto found_row;
1574 if (EQ (line, Qheader_line))
1576 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1577 return Qnil;
1578 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1579 if (!row->enabled_p)
1580 return Qnil;
1581 return list4 (make_number (row->height),
1582 make_number (0), make_number (0),
1583 make_number (0));
1586 if (EQ (line, Qmode_line))
1588 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1589 if (!row->enabled_p)
1590 return Qnil;
1591 return list4 (make_number (row->height),
1592 make_number (0), /* not accurate */
1593 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
1594 + window_text_bottom_y (w)),
1595 make_number (0));
1598 CHECK_NUMBER (line);
1599 n = XINT (line);
1601 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1602 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1603 max_y = window_text_bottom_y (w);
1604 i = 0;
1606 while ((n < 0 || i < n)
1607 && row <= end_row && row->enabled_p
1608 && row->y + row->height < max_y)
1609 row++, i++;
1611 if (row > end_row || !row->enabled_p)
1612 return Qnil;
1614 if (++n < 0)
1616 if (-n > i)
1617 return Qnil;
1618 row += n;
1619 i += n;
1622 found_row:
1623 crop = max (0, (row->y + row->height) - max_y);
1624 return list4 (make_number (row->height + min (0, row->y) - crop),
1625 make_number (i),
1626 make_number (row->y),
1627 make_number (crop));
1630 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1631 0, 1, 0,
1632 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1633 More precisely, return the value assigned by the last call of
1634 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1635 never called with WINDOW as its argument, or the value set by that
1636 function was internally reset since its last call. WINDOW must be a
1637 live window and defaults to the selected one.
1639 When a window is dedicated to its buffer, `display-buffer' will refrain
1640 from displaying another buffer in it. `get-lru-window' and
1641 `get-largest-window' treat dedicated windows specially.
1642 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1643 `kill-buffer' can delete a dedicated window and the containing frame.
1645 Functions like `set-window-buffer' may change the buffer displayed by a
1646 window, unless that window is "strongly" dedicated to its buffer, that
1647 is the value returned by `window-dedicated-p' is t. */)
1648 (Lisp_Object window)
1650 return decode_live_window (window)->dedicated;
1653 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1654 Sset_window_dedicated_p, 2, 2, 0,
1655 doc: /* Mark WINDOW as dedicated according to FLAG.
1656 WINDOW must be a live window and defaults to the selected one. FLAG
1657 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1658 mark WINDOW as non-dedicated. Return FLAG.
1660 When a window is dedicated to its buffer, `display-buffer' will refrain
1661 from displaying another buffer in it. `get-lru-window' and
1662 `get-largest-window' treat dedicated windows specially.
1663 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1664 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1665 and the containing frame.
1667 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1668 its buffer. Functions like `set-window-buffer' may change the buffer
1669 displayed by a window, unless that window is strongly dedicated to its
1670 buffer. If and when `set-window-buffer' displays another buffer in a
1671 window, it also makes sure that the window is no more dedicated. */)
1672 (Lisp_Object window, Lisp_Object flag)
1674 return WSET (decode_live_window (window), dedicated, flag);
1677 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1678 0, 1, 0,
1679 doc: /* Return buffers previously shown in WINDOW.
1680 WINDOW must be a live window and defaults to the selected one.
1682 The return value is a list of elements (BUFFER WINDOW-START POS),
1683 where BUFFER is a buffer, WINDOW-START is the start position of the
1684 window for that buffer, and POS is a window-specific point value. */)
1685 (Lisp_Object window)
1687 return decode_live_window (window)->prev_buffers;
1690 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1691 Sset_window_prev_buffers, 2, 2, 0,
1692 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1693 WINDOW must be a live window and defaults to the selected one.
1695 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1696 where BUFFER is a buffer, WINDOW-START is the start position of the
1697 window for that buffer, and POS is a window-specific point value. */)
1698 (Lisp_Object window, Lisp_Object prev_buffers)
1700 return WSET (decode_live_window (window), prev_buffers, prev_buffers);
1703 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1704 0, 1, 0,
1705 doc: /* Return list of buffers recently re-shown in WINDOW.
1706 WINDOW must be a live window and defaults to the selected one. */)
1707 (Lisp_Object window)
1709 return decode_live_window (window)->next_buffers;
1712 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1713 Sset_window_next_buffers, 2, 2, 0,
1714 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1715 WINDOW must be a live window and defaults to the selected one.
1716 NEXT-BUFFERS should be a list of buffers. */)
1717 (Lisp_Object window, Lisp_Object next_buffers)
1719 return WSET (decode_live_window (window), next_buffers, next_buffers);
1722 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1723 0, 1, 0,
1724 doc: /* Return the parameters of WINDOW and their values.
1725 WINDOW must be a valid window and defaults to the selected one. The
1726 return value is a list of elements of the form (PARAMETER . VALUE). */)
1727 (Lisp_Object window)
1729 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1732 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1733 2, 2, 0,
1734 doc: /* Return WINDOW's value for PARAMETER.
1735 WINDOW must be a valid window and defaults to the selected one. */)
1736 (Lisp_Object window, Lisp_Object parameter)
1738 Lisp_Object result;
1740 result = Fassq (parameter, decode_valid_window (window)->window_parameters);
1741 return CDR_SAFE (result);
1744 DEFUN ("set-window-parameter", Fset_window_parameter,
1745 Sset_window_parameter, 3, 3, 0,
1746 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1747 WINDOW must be a valid window and defaults to the selected one.
1748 Return VALUE. */)
1749 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1751 register struct window *w = decode_valid_window (window);
1752 Lisp_Object old_alist_elt;
1754 old_alist_elt = Fassq (parameter, w->window_parameters);
1755 if (NILP (old_alist_elt))
1756 WSET (w, window_parameters,
1757 Fcons (Fcons (parameter, value), w->window_parameters));
1758 else
1759 Fsetcdr (old_alist_elt, value);
1760 return value;
1763 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1764 0, 1, 0,
1765 doc: /* Return the display-table that WINDOW is using.
1766 WINDOW must be a live window and defaults to the selected one. */)
1767 (Lisp_Object window)
1769 return decode_live_window (window)->display_table;
1772 /* Get the display table for use on window W. This is either W's
1773 display table or W's buffer's display table. Ignore the specified
1774 tables if they are not valid; if no valid table is specified,
1775 return 0. */
1777 struct Lisp_Char_Table *
1778 window_display_table (struct window *w)
1780 struct Lisp_Char_Table *dp = NULL;
1782 if (DISP_TABLE_P (w->display_table))
1783 dp = XCHAR_TABLE (w->display_table);
1784 else if (BUFFERP (w->buffer))
1786 struct buffer *b = XBUFFER (w->buffer);
1788 if (DISP_TABLE_P (BVAR (b, display_table)))
1789 dp = XCHAR_TABLE (BVAR (b, display_table));
1790 else if (DISP_TABLE_P (Vstandard_display_table))
1791 dp = XCHAR_TABLE (Vstandard_display_table);
1794 return dp;
1797 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1798 doc: /* Set WINDOW's display-table to TABLE.
1799 WINDOW must be a live window and defaults to the selected one. */)
1800 (register Lisp_Object window, Lisp_Object table)
1802 return WSET (decode_live_window (window), display_table, table);
1805 /* Record info on buffer window W is displaying
1806 when it is about to cease to display that buffer. */
1807 static void
1808 unshow_buffer (register struct window *w)
1810 Lisp_Object buf;
1811 struct buffer *b;
1813 buf = w->buffer;
1814 b = XBUFFER (buf);
1815 if (b != XMARKER (w->pointm)->buffer)
1816 abort ();
1818 #if 0
1819 if (w == XWINDOW (selected_window)
1820 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1821 /* Do this except when the selected window's buffer
1822 is being removed from some other window. */
1823 #endif
1824 /* last_window_start records the start position that this buffer
1825 had in the last window to be disconnected from it.
1826 Now that this statement is unconditional,
1827 it is possible for the buffer to be displayed in the
1828 selected window, while last_window_start reflects another
1829 window which was recently showing the same buffer.
1830 Some people might say that might be a good thing. Let's see. */
1831 b->last_window_start = marker_position (w->start);
1833 /* Point in the selected window's buffer
1834 is actually stored in that buffer, and the window's pointm isn't used.
1835 So don't clobber point in that buffer. */
1836 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1837 /* This line helps to fix Horsley's testbug.el bug. */
1838 && !(WINDOWP (BVAR (b, last_selected_window))
1839 && w != XWINDOW (BVAR (b, last_selected_window))
1840 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1841 temp_set_point_both (b,
1842 clip_to_bounds (BUF_BEGV (b),
1843 XMARKER (w->pointm)->charpos,
1844 BUF_ZV (b)),
1845 clip_to_bounds (BUF_BEGV_BYTE (b),
1846 marker_byte_position (w->pointm),
1847 BUF_ZV_BYTE (b)));
1849 if (WINDOWP (BVAR (b, last_selected_window))
1850 && w == XWINDOW (BVAR (b, last_selected_window)))
1851 BSET (b, last_selected_window, Qnil);
1854 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1855 means change window structure only. Otherwise store geometry and
1856 other settings as well. */
1857 static void
1858 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1860 register Lisp_Object tem;
1861 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1863 /* If OLD is its frame's root window, then NEW is the new
1864 root window for that frame. */
1865 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1866 FSET (XFRAME (o->frame), root_window, new);
1868 if (setflag)
1870 WSET (n, left_col, o->left_col);
1871 WSET (n, top_line, o->top_line);
1872 WSET (n, total_cols, o->total_cols);
1873 WSET (n, total_lines, o->total_lines);
1874 WSET (n, normal_cols, o->normal_cols);
1875 WSET (o, normal_cols, make_float (1.0));
1876 WSET (n, normal_lines, o->normal_lines);
1877 WSET (o, normal_lines, make_float (1.0));
1878 n->desired_matrix = n->current_matrix = 0;
1879 n->vscroll = 0;
1880 memset (&n->cursor, 0, sizeof (n->cursor));
1881 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
1882 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
1883 n->phys_cursor_type = -1;
1884 n->phys_cursor_width = -1;
1885 n->must_be_updated_p = 0;
1886 n->pseudo_window_p = 0;
1887 WSET (n, window_end_vpos, make_number (0));
1888 WSET (n, window_end_pos, make_number (0));
1889 WSET (n, window_end_valid, Qnil);
1890 n->frozen_window_start_p = 0;
1893 tem = o->next;
1894 WSET (n, next, tem);
1895 if (!NILP (tem))
1896 WSET (XWINDOW (tem), prev, new);
1898 tem = o->prev;
1899 WSET (n, prev, tem);
1900 if (!NILP (tem))
1901 WSET (XWINDOW (tem), next, new);
1903 tem = o->parent;
1904 WSET (n, parent, tem);
1905 if (!NILP (tem))
1907 if (EQ (XWINDOW (tem)->vchild, old))
1908 WSET (XWINDOW (tem), vchild, new);
1909 if (EQ (XWINDOW (tem)->hchild, old))
1910 WSET (XWINDOW (tem), hchild, new);
1914 /* If window WINDOW and its parent window are iso-combined, merge
1915 WINDOW's children into those of its parent window and mark WINDOW as
1916 deleted. */
1918 static void
1919 recombine_windows (Lisp_Object window)
1921 struct window *w, *p, *c;
1922 Lisp_Object parent, child;
1923 int horflag;
1925 w = XWINDOW (window);
1926 parent = w->parent;
1927 if (!NILP (parent) && NILP (w->combination_limit))
1929 p = XWINDOW (parent);
1930 if (((!NILP (p->vchild) && !NILP (w->vchild))
1931 || (!NILP (p->hchild) && !NILP (w->hchild))))
1932 /* WINDOW and PARENT are both either a vertical or a horizontal
1933 combination. */
1935 horflag = NILP (w->vchild);
1936 child = horflag ? w->hchild : w->vchild;
1937 c = XWINDOW (child);
1939 /* Splice WINDOW's children into its parent's children and
1940 assign new normal sizes. */
1941 if (NILP (w->prev))
1942 if (horflag)
1943 WSET (p, hchild, child);
1944 else
1945 WSET (p, vchild, child);
1946 else
1948 WSET (c, prev, w->prev);
1949 WSET (XWINDOW (w->prev), next, child);
1952 while (c)
1954 WSET (c, parent, parent);
1956 if (horflag)
1957 WSET (c, normal_cols,
1958 make_float (XFLOATINT (c->total_cols)
1959 / XFLOATINT (p->total_cols)));
1960 else
1961 WSET (c, normal_lines,
1962 make_float (XFLOATINT (c->total_lines)
1963 / XFLOATINT (p->total_lines)));
1965 if (NILP (c->next))
1967 if (!NILP (w->next))
1969 WSET (c, next, w->next);
1970 WSET (XWINDOW (c->next), prev, child);
1973 c = 0;
1975 else
1977 child = c->next;
1978 c = XWINDOW (child);
1982 /* WINDOW can be deleted now. */
1983 WSET (w, vchild, Qnil);
1984 WSET (w, hchild, Qnil);
1989 /* If WINDOW can be deleted, delete it. */
1990 static void
1991 delete_deletable_window (Lisp_Object window)
1993 if (!NILP (call1 (Qwindow_deletable_p, window)))
1994 call1 (Qdelete_window, window);
1997 /***********************************************************************
1998 Window List
1999 ***********************************************************************/
2001 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2002 pointer. This is a callback function for foreach_window, used in
2003 the window_list function. */
2005 static int
2006 add_window_to_list (struct window *w, void *user_data)
2008 Lisp_Object *list = (Lisp_Object *) user_data;
2009 Lisp_Object window;
2010 XSETWINDOW (window, w);
2011 *list = Fcons (window, *list);
2012 return 1;
2016 /* Return a list of all windows, for use by next_window. If
2017 Vwindow_list is a list, return that list. Otherwise, build a new
2018 list, cache it in Vwindow_list, and return that. */
2020 static Lisp_Object
2021 window_list (void)
2023 if (!CONSP (Vwindow_list))
2025 Lisp_Object tail;
2027 Vwindow_list = Qnil;
2028 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
2030 Lisp_Object args[2];
2032 /* We are visiting windows in canonical order, and add
2033 new windows at the front of args[1], which means we
2034 have to reverse this list at the end. */
2035 args[1] = Qnil;
2036 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
2037 args[0] = Vwindow_list;
2038 args[1] = Fnreverse (args[1]);
2039 Vwindow_list = Fnconc (2, args);
2043 return Vwindow_list;
2047 /* Value is non-zero if WINDOW satisfies the constraints given by
2048 OWINDOW, MINIBUF and ALL_FRAMES.
2050 MINIBUF t means WINDOW may be minibuffer windows.
2051 `lambda' means WINDOW may not be a minibuffer window.
2052 a window means a specific minibuffer window
2054 ALL_FRAMES t means search all frames,
2055 nil means search just current frame,
2056 `visible' means search just visible frames on the
2057 current terminal,
2058 0 means search visible and iconified frames on the
2059 current terminal,
2060 a window means search the frame that window belongs to,
2061 a frame means consider windows on that frame, only. */
2063 static int
2064 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2066 struct window *w = XWINDOW (window);
2067 struct frame *f = XFRAME (w->frame);
2068 int candidate_p = 1;
2070 if (!BUFFERP (w->buffer))
2071 candidate_p = 0;
2072 else if (MINI_WINDOW_P (w)
2073 && (EQ (minibuf, Qlambda)
2074 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2076 /* If MINIBUF is `lambda' don't consider any mini-windows.
2077 If it is a window, consider only that one. */
2078 candidate_p = 0;
2080 else if (EQ (all_frames, Qt))
2081 candidate_p = 1;
2082 else if (NILP (all_frames))
2084 eassert (WINDOWP (owindow));
2085 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2087 else if (EQ (all_frames, Qvisible))
2089 FRAME_SAMPLE_VISIBILITY (f);
2090 candidate_p = FRAME_VISIBLE_P (f)
2091 && (FRAME_TERMINAL (XFRAME (w->frame))
2092 == FRAME_TERMINAL (XFRAME (selected_frame)));
2095 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2097 FRAME_SAMPLE_VISIBILITY (f);
2098 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2099 #ifdef HAVE_X_WINDOWS
2100 /* Yuck!! If we've just created the frame and the
2101 window-manager requested the user to place it
2102 manually, the window may still not be considered
2103 `visible'. I'd argue it should be at least
2104 something like `iconified', but don't know how to do
2105 that yet. --Stef */
2106 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2107 && !f->output_data.x->has_been_visible)
2108 #endif
2110 && (FRAME_TERMINAL (XFRAME (w->frame))
2111 == FRAME_TERMINAL (XFRAME (selected_frame)));
2113 else if (WINDOWP (all_frames))
2114 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2115 || EQ (XWINDOW (all_frames)->frame, w->frame)
2116 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2117 else if (FRAMEP (all_frames))
2118 candidate_p = EQ (all_frames, w->frame);
2120 return candidate_p;
2124 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2125 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2126 MINIBUF, and ALL_FRAMES. */
2128 static void
2129 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2131 if (NILP (*window))
2132 *window = selected_window;
2133 else
2134 CHECK_LIVE_WINDOW (*window);
2136 /* MINIBUF nil may or may not include minibuffers. Decide if it
2137 does. */
2138 if (NILP (*minibuf))
2139 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2140 else if (!EQ (*minibuf, Qt))
2141 *minibuf = Qlambda;
2143 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2144 => count none of them, or a specific minibuffer window (the
2145 active one) to count. */
2147 /* ALL_FRAMES nil doesn't specify which frames to include. */
2148 if (NILP (*all_frames))
2149 *all_frames
2150 = (!EQ (*minibuf, Qlambda)
2151 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
2152 : Qnil);
2153 else if (EQ (*all_frames, Qvisible))
2155 else if (EQ (*all_frames, make_number (0)))
2157 else if (FRAMEP (*all_frames))
2159 else if (!EQ (*all_frames, Qt))
2160 *all_frames = Qnil;
2164 /* Return the next or previous window of WINDOW in cyclic ordering
2165 of windows. NEXT_P non-zero means return the next window. See the
2166 documentation string of next-window for the meaning of MINIBUF and
2167 ALL_FRAMES. */
2169 static Lisp_Object
2170 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2172 decode_next_window_args (&window, &minibuf, &all_frames);
2174 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2175 return the first window on the frame. */
2176 if (FRAMEP (all_frames)
2177 && !EQ (all_frames, XWINDOW (window)->frame))
2178 return Fframe_first_window (all_frames);
2180 if (next_p)
2182 Lisp_Object list;
2184 /* Find WINDOW in the list of all windows. */
2185 list = Fmemq (window, window_list ());
2187 /* Scan forward from WINDOW to the end of the window list. */
2188 if (CONSP (list))
2189 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2190 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2191 break;
2193 /* Scan from the start of the window list up to WINDOW. */
2194 if (!CONSP (list))
2195 for (list = Vwindow_list;
2196 CONSP (list) && !EQ (XCAR (list), window);
2197 list = XCDR (list))
2198 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2199 break;
2201 if (CONSP (list))
2202 window = XCAR (list);
2204 else
2206 Lisp_Object candidate, list;
2208 /* Scan through the list of windows for candidates. If there are
2209 candidate windows in front of WINDOW, the last one of these
2210 is the one we want. If there are candidates following WINDOW
2211 in the list, again the last one of these is the one we want. */
2212 candidate = Qnil;
2213 for (list = window_list (); CONSP (list); list = XCDR (list))
2215 if (EQ (XCAR (list), window))
2217 if (WINDOWP (candidate))
2218 break;
2220 else if (candidate_window_p (XCAR (list), window, minibuf,
2221 all_frames))
2222 candidate = XCAR (list);
2225 if (WINDOWP (candidate))
2226 window = candidate;
2229 return window;
2233 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2234 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2235 WINDOW must be a live window and defaults to the selected one. The
2236 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2237 consider.
2239 MINIBUF nil or omitted means consider the minibuffer window only if the
2240 minibuffer is active. MINIBUF t means consider the minibuffer window
2241 even if the minibuffer is not active. Any other value means do not
2242 consider the minibuffer window even if the minibuffer is active.
2244 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2245 plus the minibuffer window if specified by the MINIBUF argument. If the
2246 minibuffer counts, consider all windows on all frames that share that
2247 minibuffer too. The following non-nil values of ALL-FRAMES have special
2248 meanings:
2250 - t means consider all windows on all existing frames.
2252 - `visible' means consider all windows on all visible frames.
2254 - 0 (the number zero) means consider all windows on all visible and
2255 iconified frames.
2257 - A frame means consider all windows on that frame only.
2259 Anything else means consider all windows on WINDOW's frame and no
2260 others.
2262 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2263 `next-window' to iterate through the entire cycle of acceptable
2264 windows, eventually ending up back at the window you started with.
2265 `previous-window' traverses the same cycle, in the reverse order. */)
2266 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2268 return next_window (window, minibuf, all_frames, 1);
2272 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2273 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2274 WINDOW must be a live window and defaults to the selected one. The
2275 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2276 consider.
2278 MINIBUF nil or omitted means consider the minibuffer window only if the
2279 minibuffer is active. MINIBUF t means consider the minibuffer window
2280 even if the minibuffer is not active. Any other value means do not
2281 consider the minibuffer window even if the minibuffer is active.
2283 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2284 plus the minibuffer window if specified by the MINIBUF argument. If the
2285 minibuffer counts, consider all windows on all frames that share that
2286 minibuffer too. The following non-nil values of ALL-FRAMES have special
2287 meanings:
2289 - t means consider all windows on all existing frames.
2291 - `visible' means consider all windows on all visible frames.
2293 - 0 (the number zero) means consider all windows on all visible and
2294 iconified frames.
2296 - A frame means consider all windows on that frame only.
2298 Anything else means consider all windows on WINDOW's frame and no
2299 others.
2301 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2302 use `previous-window' to iterate through the entire cycle of
2303 acceptable windows, eventually ending up back at the window you
2304 started with. `next-window' traverses the same cycle, in the
2305 reverse order. */)
2306 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2308 return next_window (window, minibuf, all_frames, 0);
2312 /* Return a list of windows in cyclic ordering. Arguments are like
2313 for `next-window'. */
2315 static Lisp_Object
2316 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2318 Lisp_Object tail, list, rest;
2320 decode_next_window_args (&window, &minibuf, &all_frames);
2321 list = Qnil;
2323 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2324 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2325 list = Fcons (XCAR (tail), list);
2327 /* Rotate the list to start with WINDOW. */
2328 list = Fnreverse (list);
2329 rest = Fmemq (window, list);
2330 if (!NILP (rest) && !EQ (rest, list))
2332 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2334 XSETCDR (tail, Qnil);
2335 list = nconc2 (rest, list);
2337 return list;
2341 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2342 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2343 FRAME nil or omitted means use the selected frame.
2344 WINDOW nil or omitted means use the window selected within FRAME.
2345 MINIBUF t means include the minibuffer window, even if it isn't active.
2346 MINIBUF nil or omitted means include the minibuffer window only
2347 if it's active.
2348 MINIBUF neither nil nor t means never include the minibuffer window. */)
2349 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2351 if (NILP (window))
2352 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2353 CHECK_WINDOW (window);
2354 if (NILP (frame))
2355 frame = selected_frame;
2357 if (!EQ (frame, XWINDOW (window)->frame))
2358 error ("Window is on a different frame");
2360 return window_list_1 (window, minibuf, frame);
2364 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2365 doc: /* Return a list of all live windows.
2366 WINDOW specifies the first window to list and defaults to the selected
2367 window.
2369 Optional argument MINIBUF nil or omitted means consider the minibuffer
2370 window only if the minibuffer is active. MINIBUF t means consider the
2371 minibuffer window even if the minibuffer is not active. Any other value
2372 means do not consider the minibuffer window even if the minibuffer is
2373 active.
2375 Optional argument ALL-FRAMES nil or omitted means consider all windows
2376 on WINDOW's frame, plus the minibuffer window if specified by the
2377 MINIBUF argument. If the minibuffer counts, consider all windows on all
2378 frames that share that minibuffer too. The following non-nil values of
2379 ALL-FRAMES have special meanings:
2381 - t means consider all windows on all existing frames.
2383 - `visible' means consider all windows on all visible frames.
2385 - 0 (the number zero) means consider all windows on all visible and
2386 iconified frames.
2388 - A frame means consider all windows on that frame only.
2390 Anything else means consider all windows on WINDOW's frame and no
2391 others.
2393 If WINDOW is not on the list of windows returned, some other window will
2394 be listed first but no error is signaled. */)
2395 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2397 return window_list_1 (window, minibuf, all_frames);
2400 /* Look at all windows, performing an operation specified by TYPE
2401 with argument OBJ.
2402 If FRAMES is Qt, look at all frames;
2403 Qnil, look at just the selected frame;
2404 Qvisible, look at visible frames;
2405 a frame, just look at windows on that frame.
2406 If MINI is non-zero, perform the operation on minibuffer windows too. */
2408 enum window_loop
2410 WINDOW_LOOP_UNUSED,
2411 GET_BUFFER_WINDOW, /* Arg is buffer */
2412 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2413 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2414 CHECK_ALL_WINDOWS
2417 static Lisp_Object
2418 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2420 Lisp_Object window, windows, best_window, frame_arg;
2421 int frame_best_window_flag = 0;
2422 struct frame *f;
2423 struct gcpro gcpro1;
2425 /* If we're only looping through windows on a particular frame,
2426 frame points to that frame. If we're looping through windows
2427 on all frames, frame is 0. */
2428 if (FRAMEP (frames))
2429 f = XFRAME (frames);
2430 else if (NILP (frames))
2431 f = SELECTED_FRAME ();
2432 else
2433 f = NULL;
2435 if (f)
2436 frame_arg = Qlambda;
2437 else if (EQ (frames, make_number (0)))
2438 frame_arg = frames;
2439 else if (EQ (frames, Qvisible))
2440 frame_arg = frames;
2441 else
2442 frame_arg = Qt;
2444 /* frame_arg is Qlambda to stick to one frame,
2445 Qvisible to consider all visible frames,
2446 or Qt otherwise. */
2448 /* Pick a window to start with. */
2449 if (WINDOWP (obj))
2450 window = obj;
2451 else if (f)
2452 window = FRAME_SELECTED_WINDOW (f);
2453 else
2454 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2456 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2457 GCPRO1 (windows);
2458 best_window = Qnil;
2460 for (; CONSP (windows); windows = XCDR (windows))
2462 struct window *w;
2464 window = XCAR (windows);
2465 w = XWINDOW (window);
2467 /* Note that we do not pay attention here to whether the frame
2468 is visible, since Fwindow_list skips non-visible frames if
2469 that is desired, under the control of frame_arg. */
2470 if (!MINI_WINDOW_P (w)
2471 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2472 consider all windows. */
2473 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2474 || (mini && minibuf_level > 0))
2475 switch (type)
2477 case GET_BUFFER_WINDOW:
2478 if (EQ (w->buffer, obj)
2479 /* Don't find any minibuffer window except the one that
2480 is currently in use. */
2481 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2483 if (EQ (window, selected_window))
2484 /* Preferably return the selected window. */
2485 RETURN_UNGCPRO (window);
2486 else if (EQ (XWINDOW (window)->frame, selected_frame)
2487 && !frame_best_window_flag)
2488 /* Prefer windows on the current frame (but don't
2489 choose another one if we have one already). */
2491 best_window = window;
2492 frame_best_window_flag = 1;
2494 else if (NILP (best_window))
2495 best_window = window;
2497 break;
2499 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2500 /* We could simply check whether the buffer shown by window
2501 is live, and show another buffer in case it isn't. */
2502 if (EQ (w->buffer, obj))
2504 /* Undedicate WINDOW. */
2505 WSET (w, dedicated, Qnil);
2506 /* Make WINDOW show the buffer returned by
2507 other_buffer_safely, don't run any hooks. */
2508 set_window_buffer
2509 (window, other_buffer_safely (w->buffer), 0, 0);
2510 /* If WINDOW is the selected window, make its buffer
2511 current. But do so only if the window shows the
2512 current buffer (Bug#6454). */
2513 if (EQ (window, selected_window)
2514 && XBUFFER (w->buffer) == current_buffer)
2515 Fset_buffer (w->buffer);
2517 break;
2519 case REDISPLAY_BUFFER_WINDOWS:
2520 if (EQ (w->buffer, obj))
2522 mark_window_display_accurate (window, 0);
2523 w->update_mode_line = 1;
2524 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2525 ++update_mode_lines;
2526 best_window = window;
2528 break;
2530 /* Check for a window that has a killed buffer. */
2531 case CHECK_ALL_WINDOWS:
2532 if (! NILP (w->buffer)
2533 && NILP (BVAR (XBUFFER (w->buffer), name)))
2534 abort ();
2535 break;
2537 case WINDOW_LOOP_UNUSED:
2538 break;
2542 UNGCPRO;
2543 return best_window;
2546 /* Used for debugging. Abort if any window has a dead buffer. */
2548 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2549 void
2550 check_all_windows (void)
2552 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2555 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2556 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2557 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2558 the current buffer.
2560 The optional argument ALL-FRAMES specifies the frames to consider:
2562 - t means consider all windows on all existing frames.
2564 - `visible' means consider all windows on all visible frames.
2566 - 0 (the number zero) means consider all windows on all visible
2567 and iconified frames.
2569 - A frame means consider all windows on that frame only.
2571 Any other value of ALL-FRAMES means consider all windows on the
2572 selected frame and no others. */)
2573 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2575 Lisp_Object buffer;
2577 if (NILP (buffer_or_name))
2578 buffer = Fcurrent_buffer ();
2579 else
2580 buffer = Fget_buffer (buffer_or_name);
2582 if (BUFFERP (buffer))
2583 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2584 else
2585 return Qnil;
2588 static Lisp_Object
2589 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2591 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2595 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2596 Sdelete_other_windows_internal, 0, 2, "",
2597 doc: /* Make WINDOW fill its frame.
2598 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2599 and defaults to the selected one.
2601 Optional argument ROOT, if non-nil, must specify an internal window such
2602 that WINDOW is in its window subtree. If this is the case, replace ROOT
2603 by WINDOW and leave alone any windows not part of ROOT's subtree.
2605 When WINDOW is live try to reduce display jumps by keeping the text
2606 previously visible in WINDOW in the same place on the frame. Doing this
2607 depends on the value of (window-start WINDOW), so if calling this
2608 function in a program gives strange scrolling, make sure the
2609 window-start value is reasonable when this function is called. */)
2610 (Lisp_Object window, Lisp_Object root)
2612 struct window *w, *r, *s;
2613 struct frame *f;
2614 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2615 ptrdiff_t startpos IF_LINT (= 0);
2616 int top IF_LINT (= 0), new_top, resize_failed;
2618 w = decode_valid_window (window);
2619 XSETWINDOW (window, w);
2620 f = XFRAME (w->frame);
2622 if (NILP (root))
2623 /* ROOT is the frame's root window. */
2625 root = FRAME_ROOT_WINDOW (f);
2626 r = XWINDOW (root);
2628 else
2629 /* ROOT must be an ancestor of WINDOW. */
2631 r = decode_valid_window (root);
2632 pwindow = XWINDOW (window)->parent;
2633 while (!NILP (pwindow))
2634 if (EQ (pwindow, root))
2635 break;
2636 else
2637 pwindow = XWINDOW (pwindow)->parent;
2638 if (!EQ (pwindow, root))
2639 error ("Specified root is not an ancestor of specified window");
2642 if (EQ (window, root))
2643 /* A noop. */
2644 return Qnil;
2645 /* I don't understand the "top > 0" part below. If we deal with a
2646 standalone minibuffer it would have been caught by the preceding
2647 test. */
2648 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2649 error ("Can't expand minibuffer to full frame");
2651 if (!NILP (w->buffer))
2653 startpos = marker_position (w->start);
2654 top = WINDOW_TOP_EDGE_LINE (w)
2655 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2656 /* Make sure WINDOW is the frame's selected window. */
2657 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2659 if (EQ (selected_frame, w->frame))
2660 Fselect_window (window, Qnil);
2661 else
2662 FSET (f, selected_window, window);
2665 else
2667 /* See if the frame's selected window is a part of the window
2668 subtree rooted at WINDOW, by finding all the selected window's
2669 parents and comparing each one with WINDOW. If it isn't we
2670 need a new selected window for this frame. */
2671 swindow = FRAME_SELECTED_WINDOW (f);
2672 while (1)
2674 pwindow = swindow;
2675 while (!NILP (pwindow) && !EQ (window, pwindow))
2676 pwindow = XWINDOW (pwindow)->parent;
2678 if (EQ (window, pwindow))
2679 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2680 as the new selected window. */
2681 break;
2682 else
2683 /* Else try the previous window of SWINDOW. */
2684 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2687 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2689 if (EQ (selected_frame, w->frame))
2690 Fselect_window (swindow, Qnil);
2691 else
2692 FSET (f, selected_window, swindow);
2696 BLOCK_INPUT;
2697 if (!FRAME_INITIAL_P (f))
2699 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2701 /* We are going to free the glyph matrices of WINDOW, and with
2702 that we might lose any information about glyph rows that have
2703 some of their glyphs highlighted in mouse face. (These rows
2704 are marked with a non-zero mouse_face_p flag.) If WINDOW
2705 indeed has some glyphs highlighted in mouse face, signal to
2706 frame's up-to-date hook that mouse highlight was overwritten,
2707 so that it will arrange for redisplaying the highlight. */
2708 if (EQ (hlinfo->mouse_face_window, window))
2710 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2711 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2712 hlinfo->mouse_face_window = Qnil;
2715 free_window_matrices (r);
2717 windows_or_buffers_changed++;
2718 Vwindow_list = Qnil;
2719 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2720 resize_failed = 0;
2722 if (NILP (w->buffer))
2724 /* Resize child windows vertically. */
2725 XSETINT (delta, XINT (r->total_lines)
2726 - XINT (w->total_lines));
2727 WSET (w, top_line, r->top_line);
2728 resize_root_window (window, delta, Qnil, Qnil);
2729 if (window_resize_check (w, 0))
2730 window_resize_apply (w, 0);
2731 else
2733 resize_root_window (window, delta, Qnil, Qt);
2734 if (window_resize_check (w, 0))
2735 window_resize_apply (w, 0);
2736 else
2737 resize_failed = 1;
2740 /* Resize child windows horizontally. */
2741 if (!resize_failed)
2743 WSET (w, left_col, r->left_col);
2744 XSETINT (delta, XINT (r->total_cols)
2745 - XINT (w->total_cols));
2746 WSET (w, left_col, r->left_col);
2747 resize_root_window (window, delta, Qt, Qnil);
2748 if (window_resize_check (w, 1))
2749 window_resize_apply (w, 1);
2750 else
2752 resize_root_window (window, delta, Qt, Qt);
2753 if (window_resize_check (w, 1))
2754 window_resize_apply (w, 1);
2755 else
2756 resize_failed = 1;
2760 if (resize_failed)
2761 /* Play safe, if we still can ... */
2763 window = swindow;
2764 w = XWINDOW (window);
2768 /* Cleanly unlink WINDOW from window-tree. */
2769 if (!NILP (w->prev))
2770 /* Get SIBLING above (on the left of) WINDOW. */
2772 sibling = w->prev;
2773 s = XWINDOW (sibling);
2774 WSET (s, next, w->next);
2775 if (!NILP (s->next))
2776 WSET (XWINDOW (s->next), prev, sibling);
2778 else
2779 /* Get SIBLING below (on the right of) WINDOW. */
2781 sibling = w->next;
2782 s = XWINDOW (sibling);
2783 WSET (s, prev, Qnil);
2784 if (!NILP (XWINDOW (w->parent)->vchild))
2785 WSET (XWINDOW (w->parent), vchild, sibling);
2786 else
2787 WSET (XWINDOW (w->parent), hchild, sibling);
2790 /* Delete ROOT and all child windows of ROOT. */
2791 if (!NILP (r->vchild))
2793 delete_all_child_windows (r->vchild);
2794 WSET (r, vchild, Qnil);
2796 else if (!NILP (r->hchild))
2798 delete_all_child_windows (r->hchild);
2799 WSET (r, hchild, Qnil);
2802 replace_window (root, window, 1);
2804 /* This must become SWINDOW anyway ....... */
2805 if (!NILP (w->buffer) && !resize_failed)
2807 /* Try to minimize scrolling, by setting the window start to the
2808 point will cause the text at the old window start to be at the
2809 same place on the frame. But don't try to do this if the
2810 window start is outside the visible portion (as might happen
2811 when the display is not current, due to typeahead). */
2812 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2813 if (new_top != top
2814 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2815 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2817 struct position pos;
2818 struct buffer *obuf = current_buffer;
2820 Fset_buffer (w->buffer);
2821 /* This computation used to temporarily move point, but that
2822 can have unwanted side effects due to text properties. */
2823 pos = *vmotion (startpos, -top, w);
2825 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2826 WSET (w, window_end_valid, Qnil);
2827 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2828 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2829 /* We need to do this, so that the window-scroll-functions
2830 get called. */
2831 w->optional_new_start = 1;
2833 set_buffer_internal (obuf);
2837 adjust_glyphs (f);
2838 UNBLOCK_INPUT;
2840 run_window_configuration_change_hook (f);
2842 return Qnil;
2846 void
2847 replace_buffer_in_windows (Lisp_Object buffer)
2849 call1 (Qreplace_buffer_in_windows, buffer);
2853 /* Safely replace BUFFER with some other buffer in all windows of all
2854 frames, even those on other keyboards. */
2856 void
2857 replace_buffer_in_windows_safely (Lisp_Object buffer)
2859 Lisp_Object tail, frame;
2861 /* A single call to window_loop won't do the job because it only
2862 considers frames on the current keyboard. So loop manually over
2863 frames, and handle each one. */
2864 FOR_EACH_FRAME (tail, frame)
2865 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2868 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2869 minimum allowable size. */
2871 void
2872 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2874 /* For height, we have to see:
2875 how many windows the frame has at minimum (one or two),
2876 and whether it has a menu bar or other special stuff at the top. */
2877 int min_height
2878 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2879 ? MIN_SAFE_WINDOW_HEIGHT
2880 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2882 if (FRAME_TOP_MARGIN (frame) > 0)
2883 min_height += FRAME_TOP_MARGIN (frame);
2885 if (*rows < min_height)
2886 *rows = min_height;
2887 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2888 *cols = MIN_SAFE_WINDOW_WIDTH;
2891 /* Adjust the margins of window W if text area is too small.
2892 Return 1 if window width is ok after adjustment; 0 if window
2893 is still too narrow. */
2895 static int
2896 adjust_window_margins (struct window *w)
2898 int box_cols = (WINDOW_TOTAL_COLS (w)
2899 - WINDOW_FRINGE_COLS (w)
2900 - WINDOW_SCROLL_BAR_COLS (w));
2901 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2902 + WINDOW_RIGHT_MARGIN_COLS (w));
2904 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2905 return 1;
2907 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2908 return 0;
2910 /* Window's text area is too narrow, but reducing the window
2911 margins will fix that. */
2912 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2913 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2915 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2917 WSET (w, left_margin_cols, make_number (margin_cols / 2));
2918 WSET (w, right_margin_cols, make_number (margin_cols / 2));
2920 else
2921 WSET (w, right_margin_cols, make_number (margin_cols));
2923 else
2924 WSET (w, left_margin_cols, make_number (margin_cols));
2925 return 1;
2928 /* The following three routines are needed for running a window's
2929 configuration change hook. */
2930 static void
2931 run_funs (Lisp_Object funs)
2933 for (; CONSP (funs); funs = XCDR (funs))
2934 if (!EQ (XCAR (funs), Qt))
2935 call0 (XCAR (funs));
2938 static Lisp_Object
2939 select_window_norecord (Lisp_Object window)
2941 return WINDOW_LIVE_P (window)
2942 ? Fselect_window (window, Qt) : selected_window;
2945 static Lisp_Object
2946 select_frame_norecord (Lisp_Object frame)
2948 return FRAME_LIVE_P (XFRAME (frame))
2949 ? Fselect_frame (frame, Qt) : selected_frame;
2952 void
2953 run_window_configuration_change_hook (struct frame *f)
2955 ptrdiff_t count = SPECPDL_INDEX ();
2956 Lisp_Object frame, global_wcch
2957 = Fdefault_value (Qwindow_configuration_change_hook);
2958 XSETFRAME (frame, f);
2960 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
2961 return;
2963 /* Use the right buffer. Matters when running the local hooks. */
2964 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
2966 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2967 Fset_buffer (Fwindow_buffer (Qnil));
2970 if (SELECTED_FRAME () != f)
2972 record_unwind_protect (select_frame_norecord, Fselected_frame ());
2973 select_frame_norecord (frame);
2976 /* Look for buffer-local values. */
2978 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
2979 for (; CONSP (windows); windows = XCDR (windows))
2981 Lisp_Object window = XCAR (windows);
2982 Lisp_Object buffer = Fwindow_buffer (window);
2983 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
2984 buffer)))
2986 ptrdiff_t inner_count = SPECPDL_INDEX ();
2987 record_unwind_protect (select_window_norecord, Fselected_window ());
2988 select_window_norecord (window);
2989 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
2990 buffer));
2991 unbind_to (inner_count, Qnil);
2996 run_funs (global_wcch);
2997 unbind_to (count, Qnil);
3000 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3001 Srun_window_configuration_change_hook, 1, 1, 0,
3002 doc: /* Run `window-configuration-change-hook' for FRAME. */)
3003 (Lisp_Object frame)
3005 CHECK_LIVE_FRAME (frame);
3006 run_window_configuration_change_hook (XFRAME (frame));
3007 return Qnil;
3010 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3011 means it's allowed to run hooks. See make_frame for a case where
3012 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3013 margins, fringes, and scroll-bar settings of the window are not
3014 reset from the buffer's local settings. */
3016 void
3017 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3019 struct window *w = XWINDOW (window);
3020 struct buffer *b = XBUFFER (buffer);
3021 ptrdiff_t count = SPECPDL_INDEX ();
3022 int samebuf = EQ (buffer, w->buffer);
3024 WSET (w, buffer, buffer);
3026 if (EQ (window, selected_window))
3027 BSET (b, last_selected_window, window);
3029 /* Let redisplay errors through. */
3030 b->display_error_modiff = 0;
3032 /* Update time stamps of buffer display. */
3033 if (INTEGERP (BVAR (b, display_count)))
3034 BSET (b, display_count, make_number (XINT (BVAR (b, display_count)) + 1));
3035 BSET (b, display_time, Fcurrent_time ());
3037 WSET (w, window_end_pos, make_number (0));
3038 WSET (w, window_end_vpos, make_number (0));
3039 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3040 WSET (w, window_end_valid, Qnil);
3041 if (!(keep_margins_p && samebuf))
3042 { /* If we're not actually changing the buffer, don't reset hscroll and
3043 vscroll. This case happens for example when called from
3044 change_frame_size_1, where we use a dummy call to
3045 Fset_window_buffer on the frame's selected window (and no other)
3046 just in order to run window-configuration-change-hook.
3047 Resetting hscroll and vscroll here is problematic for things like
3048 image-mode and doc-view-mode since it resets the image's position
3049 whenever we resize the frame. */
3050 w->hscroll = w->min_hscroll = 0;
3051 w->vscroll = 0;
3052 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3053 set_marker_restricted (w->start,
3054 make_number (b->last_window_start),
3055 buffer);
3056 w->start_at_line_beg = 0;
3057 w->force_start = 0;
3058 w->last_modified = 0;
3059 w->last_overlay_modified = 0;
3061 /* Maybe we could move this into the `if' but it's not obviously safe and
3062 I doubt it's worth the trouble. */
3063 windows_or_buffers_changed++;
3065 /* We must select BUFFER for running the window-scroll-functions. */
3066 /* We can't check ! NILP (Vwindow_scroll_functions) here
3067 because that might itself be a local variable. */
3068 if (window_initialized)
3070 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3071 Fset_buffer (buffer);
3074 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3076 if (!keep_margins_p)
3078 /* Set left and right marginal area width etc. from buffer. */
3080 /* This may call adjust_window_margins three times, so
3081 temporarily disable window margins. */
3082 Lisp_Object save_left = w->left_margin_cols;
3083 Lisp_Object save_right = w->right_margin_cols;
3085 WSET (w, left_margin_cols, Qnil);
3086 WSET (w, right_margin_cols, Qnil);
3088 Fset_window_fringes (window,
3089 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3090 BVAR (b, fringes_outside_margins));
3092 Fset_window_scroll_bars (window,
3093 BVAR (b, scroll_bar_width),
3094 BVAR (b, vertical_scroll_bar_type), Qnil);
3096 WSET (w, left_margin_cols, save_left);
3097 WSET (w, right_margin_cols, save_right);
3099 Fset_window_margins (window,
3100 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3103 if (run_hooks_p)
3105 if (! NILP (Vwindow_scroll_functions))
3106 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3107 Fmarker_position (w->start));
3108 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3111 unbind_to (count, Qnil);
3114 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3115 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3116 WINDOW must be a live window and defaults to the selected one.
3117 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3119 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3120 display margins, fringe widths, and scroll bar settings are preserved;
3121 the default is to reset these from the local settings for BUFFER-OR-NAME
3122 or the frame defaults. Return nil.
3124 This function throws an error when WINDOW is strongly dedicated to its
3125 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3126 already display BUFFER-OR-NAME.
3128 This function runs `window-scroll-functions' before running
3129 `window-configuration-change-hook'. */)
3130 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3132 register Lisp_Object tem, buffer;
3133 register struct window *w = decode_live_window (window);
3135 XSETWINDOW (window, w);
3136 buffer = Fget_buffer (buffer_or_name);
3137 CHECK_BUFFER (buffer);
3138 if (NILP (BVAR (XBUFFER (buffer), name)))
3139 error ("Attempt to display deleted buffer");
3141 tem = w->buffer;
3142 if (NILP (tem))
3143 error ("Window is deleted");
3144 else if (!EQ (tem, Qt))
3145 /* w->buffer is t when the window is first being set up. */
3147 if (!EQ (tem, buffer))
3149 if (EQ (w->dedicated, Qt))
3150 /* WINDOW is strongly dedicated to its buffer, signal an
3151 error. */
3152 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3153 else
3154 /* WINDOW is weakly dedicated to its buffer, reset
3155 dedication. */
3156 WSET (w, dedicated, Qnil);
3158 call1 (Qrecord_window_buffer, window);
3161 unshow_buffer (w);
3164 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3166 return Qnil;
3169 static Lisp_Object
3170 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3172 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3175 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3176 0, 1, 0,
3177 doc: /* Force all windows to be updated on next redisplay.
3178 If optional arg OBJECT is a window, force redisplay of that window only.
3179 If OBJECT is a buffer or buffer name, force redisplay of all windows
3180 displaying that buffer. */)
3181 (Lisp_Object object)
3183 if (NILP (object))
3185 windows_or_buffers_changed++;
3186 update_mode_lines++;
3187 return Qt;
3190 if (WINDOWP (object))
3192 struct window *w = XWINDOW (object);
3193 mark_window_display_accurate (object, 0);
3194 w->update_mode_line = 1;
3195 if (BUFFERP (w->buffer))
3196 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3197 ++update_mode_lines;
3198 return Qt;
3201 if (STRINGP (object))
3202 object = Fget_buffer (object);
3203 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3205 /* Walk all windows looking for buffer, and force update
3206 of each of those windows. */
3208 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3209 return NILP (object) ? Qnil : Qt;
3212 /* If nothing suitable was found, just return.
3213 We could signal an error, but this feature will typically be used
3214 asynchronously in timers or process sentinels, so we don't. */
3215 return Qnil;
3219 void
3220 temp_output_buffer_show (register Lisp_Object buf)
3222 register struct buffer *old = current_buffer;
3223 register Lisp_Object window;
3224 register struct window *w;
3226 BSET (XBUFFER (buf), directory, BVAR (current_buffer, directory));
3228 Fset_buffer (buf);
3229 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3230 BEGV = BEG;
3231 ZV = Z;
3232 SET_PT (BEG);
3233 set_buffer_internal (old);
3235 if (!NILP (Vtemp_buffer_show_function))
3236 call1 (Vtemp_buffer_show_function, buf);
3237 else
3239 window = display_buffer (buf, Qnil, Qnil);
3241 if (!EQ (XWINDOW (window)->frame, selected_frame))
3242 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3243 Vminibuf_scroll_window = window;
3244 w = XWINDOW (window);
3245 w->hscroll = 0;
3246 w->min_hscroll = 0;
3247 set_marker_restricted_both (w->start, buf, BEG, BEG);
3248 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3250 /* Run temp-buffer-show-hook, with the chosen window selected
3251 and its buffer current. */
3253 ptrdiff_t count = SPECPDL_INDEX ();
3254 Lisp_Object prev_window, prev_buffer;
3255 prev_window = selected_window;
3256 XSETBUFFER (prev_buffer, old);
3258 /* Select the window that was chosen, for running the hook.
3259 Note: Both Fselect_window and select_window_norecord may
3260 set-buffer to the buffer displayed in the window,
3261 so we need to save the current buffer. --stef */
3262 record_unwind_protect (Fset_buffer, prev_buffer);
3263 record_unwind_protect (select_window_norecord, prev_window);
3264 Fselect_window (window, Qt);
3265 Fset_buffer (w->buffer);
3266 Frun_hooks (1, &Qtemp_buffer_show_hook);
3267 unbind_to (count, Qnil);
3272 DEFUN ("internal-temp-output-buffer-show",
3273 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3274 1, 1, 0,
3275 doc: /* Internal function for `with-output-to-temp-buffer'. */)
3276 (Lisp_Object buf)
3278 temp_output_buffer_show (buf);
3279 return Qnil;
3282 /* Make new window, have it replace WINDOW in window-tree, and make
3283 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3284 horizontal child). */
3285 static void
3286 make_parent_window (Lisp_Object window, int horflag)
3288 Lisp_Object parent;
3289 register struct window *o, *p;
3291 o = XWINDOW (window);
3292 p = allocate_window ();
3293 memcpy ((char *) p + sizeof (struct vectorlike_header),
3294 (char *) o + sizeof (struct vectorlike_header),
3295 word_size * VECSIZE (struct window));
3296 XSETWINDOW (parent, p);
3298 p->sequence_number = ++sequence_number;
3300 replace_window (window, parent, 1);
3302 WSET (o, next, Qnil);
3303 WSET (o, prev, Qnil);
3304 WSET (o, parent, parent);
3306 WSET (p, hchild, horflag ? window : Qnil);
3307 WSET (p, vchild, horflag ? Qnil : window);
3308 WSET (p, start, Qnil);
3309 WSET (p, pointm, Qnil);
3310 WSET (p, buffer, Qnil);
3311 WSET (p, combination_limit, Qnil);
3312 WSET (p, window_parameters, Qnil);
3315 /* Make new window from scratch. */
3316 Lisp_Object
3317 make_window (void)
3319 Lisp_Object window;
3320 register struct window *w;
3322 w = allocate_window ();
3323 /* Initialize Lisp data. Note that allocate_window initializes all
3324 Lisp data to nil, so do it only for slots which should not be nil. */
3325 WSET (w, left_col, make_number (0));
3326 WSET (w, top_line, make_number (0));
3327 WSET (w, total_lines, make_number (0));
3328 WSET (w, total_cols, make_number (0));
3329 WSET (w, normal_lines, make_float (1.0));
3330 WSET (w, normal_cols, make_float (1.0));
3331 WSET (w, new_total, make_number (0));
3332 WSET (w, new_normal, make_number (0));
3333 WSET (w, start, Fmake_marker ());
3334 WSET (w, pointm, Fmake_marker ());
3335 WSET (w, vertical_scroll_bar_type, Qt);
3336 WSET (w, window_end_pos, make_number (0));
3337 WSET (w, window_end_vpos, make_number (0));
3339 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3340 non-Lisp data, so do it only for slots which should not be zero. */
3341 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3342 w->phys_cursor_type = -1;
3343 w->phys_cursor_width = -1;
3344 w->sequence_number = ++sequence_number;
3346 /* Reset window_list. */
3347 Vwindow_list = Qnil;
3348 /* Return window. */
3349 XSETWINDOW (window, w);
3350 return window;
3353 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3354 doc: /* Set new total size of WINDOW to SIZE.
3355 WINDOW must be a valid window and defaults to the selected one.
3356 Return SIZE.
3358 Optional argument ADD non-nil means add SIZE to the new total size of
3359 WINDOW and return the sum.
3361 Note: This function does not operate on any child windows of WINDOW. */)
3362 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3364 struct window *w = decode_valid_window (window);
3366 CHECK_NUMBER (size);
3367 if (NILP (add))
3368 WSET (w, new_total, size);
3369 else
3370 WSET (w, new_total, make_number (XINT (w->new_total) + XINT (size)));
3372 return w->new_total;
3375 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3376 doc: /* Set new normal size of WINDOW to SIZE.
3377 WINDOW must be a valid window and defaults to the selected one.
3378 Return SIZE.
3380 Note: This function does not operate on any child windows of WINDOW. */)
3381 (Lisp_Object window, Lisp_Object size)
3383 return WSET (decode_valid_window (window), new_normal, size);
3386 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3387 non-zero) to w->new_total would result in correct heights (widths)
3388 for window W and recursively all child windows of W.
3390 Note: This function does not check any of `window-fixed-size-p',
3391 `window-min-height' or `window-min-width'. It does check that window
3392 sizes do not drop below one line (two columns). */
3393 static int
3394 window_resize_check (struct window *w, int horflag)
3396 struct window *c;
3398 if (!NILP (w->vchild))
3399 /* W is a vertical combination. */
3401 c = XWINDOW (w->vchild);
3402 if (horflag)
3403 /* All child windows of W must have the same width as W. */
3405 while (c)
3407 if ((XINT (c->new_total) != XINT (w->new_total))
3408 || !window_resize_check (c, horflag))
3409 return 0;
3410 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3412 return 1;
3414 else
3415 /* The sum of the heights of the child windows of W must equal
3416 W's height. */
3418 int sum_of_sizes = 0;
3419 while (c)
3421 if (!window_resize_check (c, horflag))
3422 return 0;
3423 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3424 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3426 return (sum_of_sizes == XINT (w->new_total));
3429 else if (!NILP (w->hchild))
3430 /* W is a horizontal combination. */
3432 c = XWINDOW (w->hchild);
3433 if (horflag)
3434 /* The sum of the widths of the child windows of W must equal W's
3435 width. */
3437 int sum_of_sizes = 0;
3438 while (c)
3440 if (!window_resize_check (c, horflag))
3441 return 0;
3442 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3443 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3445 return (sum_of_sizes == XINT (w->new_total));
3447 else
3448 /* All child windows of W must have the same height as W. */
3450 while (c)
3452 if ((XINT (c->new_total) != XINT (w->new_total))
3453 || !window_resize_check (c, horflag))
3454 return 0;
3455 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3457 return 1;
3460 else
3461 /* A leaf window. Make sure it's not too small. The following
3462 hardcodes the values of `window-safe-min-width' (2) and
3463 `window-safe-min-height' (1) which are defined in window.el. */
3464 return XINT (w->new_total) >= (horflag ? 2 : 1);
3467 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3468 w->new_total for window W and recursively all child windows of W.
3469 Also calculate and assign the new vertical (horizontal) start
3470 positions of each of these windows.
3472 This function does not perform any error checks. Make sure you have
3473 run window_resize_check on W before applying this function. */
3474 static void
3475 window_resize_apply (struct window *w, int horflag)
3477 struct window *c;
3478 int pos;
3480 /* Note: Assigning new_normal requires that the new total size of the
3481 parent window has been set *before*. */
3482 if (horflag)
3484 WSET (w, total_cols, w->new_total);
3485 if (NUMBERP (w->new_normal))
3486 WSET (w, normal_cols, w->new_normal);
3488 pos = XINT (w->left_col);
3490 else
3492 WSET (w, total_lines, w->new_total);
3493 if (NUMBERP (w->new_normal))
3494 WSET (w, normal_lines, w->new_normal);
3496 pos = XINT (w->top_line);
3499 if (!NILP (w->vchild))
3500 /* W is a vertical combination. */
3502 c = XWINDOW (w->vchild);
3503 while (c)
3505 if (horflag)
3506 WSET (c, left_col, make_number (pos));
3507 else
3508 WSET (c, top_line, make_number (pos));
3509 window_resize_apply (c, horflag);
3510 if (!horflag)
3511 pos = pos + XINT (c->total_lines);
3512 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3515 else if (!NILP (w->hchild))
3516 /* W is a horizontal combination. */
3518 c = XWINDOW (w->hchild);
3519 while (c)
3521 if (horflag)
3522 WSET (c, left_col, make_number (pos));
3523 else
3524 WSET (c, top_line, make_number (pos));
3525 window_resize_apply (c, horflag);
3526 if (horflag)
3527 pos = pos + XINT (c->total_cols);
3528 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3532 /* Clear out some redisplay caches. */
3533 w->last_modified = 0;
3534 w->last_overlay_modified = 0;
3538 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 1, 2, 0,
3539 doc: /* Apply requested size values for window-tree of FRAME.
3540 Optional argument HORIZONTAL omitted or nil means apply requested height
3541 values. HORIZONTAL non-nil means apply requested width values.
3543 This function checks whether the requested values sum up to a valid
3544 window layout, recursively assigns the new sizes of all child windows
3545 and calculates and assigns the new start positions of these windows.
3547 Note: This function does not check any of `window-fixed-size-p',
3548 `window-min-height' or `window-min-width'. All these checks have to
3549 be applied on the Elisp level. */)
3550 (Lisp_Object frame, Lisp_Object horizontal)
3552 struct frame *f;
3553 struct window *r;
3554 int horflag = !NILP (horizontal);
3556 if (NILP (frame))
3557 frame = selected_frame;
3558 CHECK_LIVE_FRAME (frame);
3560 f = XFRAME (frame);
3561 r = XWINDOW (FRAME_ROOT_WINDOW (f));
3563 if (!window_resize_check (r, horflag)
3564 || ! EQ (r->new_total,
3565 (horflag ? r->total_cols : r->total_lines)))
3566 return Qnil;
3568 BLOCK_INPUT;
3569 window_resize_apply (r, horflag);
3571 windows_or_buffers_changed++;
3572 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3574 adjust_glyphs (f);
3575 UNBLOCK_INPUT;
3577 run_window_configuration_change_hook (f);
3579 return Qt;
3583 /* Resize frame F's windows when number of lines of F is set to SIZE.
3584 HORFLAG 1 means resize windows when number of columns of F is set to
3585 SIZE.
3587 This function can delete all windows but the selected one in order to
3588 satisfy the request. The result will be meaningful if and only if
3589 F's windows have meaningful sizes when you call this. */
3590 void
3591 resize_frame_windows (struct frame *f, int size, int horflag)
3593 Lisp_Object root = f->root_window;
3594 struct window *r = XWINDOW (root);
3595 Lisp_Object mini = f->minibuffer_window;
3596 struct window *m;
3597 /* new_size is the new size of the frame's root window. */
3598 int new_size = (horflag
3599 ? size
3600 : (size
3601 - FRAME_TOP_MARGIN (f)
3602 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3603 ? 1 : 0)));
3605 WSET (r, top_line, make_number (FRAME_TOP_MARGIN (f)));
3606 if (NILP (r->vchild) && NILP (r->hchild))
3607 /* For a leaf root window just set the size. */
3608 if (horflag)
3609 WSET (r, total_cols, make_number (new_size));
3610 else
3611 WSET (r, total_lines, make_number (new_size));
3612 else
3614 /* old_size is the old size of the frame's root window. */
3615 int old_size = XFASTINT (horflag ? r->total_cols
3616 : r->total_lines);
3617 Lisp_Object delta;
3619 XSETINT (delta, new_size - old_size);
3620 /* Try a "normal" resize first. */
3621 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3622 if (window_resize_check (r, horflag)
3623 && new_size == XINT (r->new_total))
3624 window_resize_apply (r, horflag);
3625 else
3627 /* Try with "reasonable" minimum sizes next. */
3628 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3629 if (window_resize_check (r, horflag)
3630 && new_size == XINT (r->new_total))
3631 window_resize_apply (r, horflag);
3632 else
3634 /* Finally, try with "safe" minimum sizes. */
3635 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3636 if (window_resize_check (r, horflag)
3637 && new_size == XINT (r->new_total))
3638 window_resize_apply (r, horflag);
3639 else
3641 /* We lost. Delete all windows but the frame's
3642 selected one. */
3643 root = f->selected_window;
3644 Fdelete_other_windows_internal (root, Qnil);
3645 if (horflag)
3646 WSET (XWINDOW (root), total_cols, make_number (new_size));
3647 else
3648 WSET (XWINDOW (root), total_lines, make_number (new_size));
3654 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3656 m = XWINDOW (mini);
3657 if (horflag)
3658 WSET (m, total_cols, make_number (size));
3659 else
3661 /* Are we sure we always want 1 line here? */
3662 WSET (m, total_lines, make_number (1));
3663 WSET (m, top_line,
3664 make_number (XINT (r->top_line) + XINT (r->total_lines)));
3670 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3671 doc: /* Split window OLD.
3672 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3673 new window. In any case TOTAL-SIZE must be a positive integer.
3675 Third argument SIDE nil (or `below') specifies that the new window shall
3676 be located below WINDOW. SIDE `above' means the new window shall be
3677 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3678 lines of the new window including space reserved for the mode and/or
3679 header line.
3681 SIDE t (or `right') specifies that the new window shall be located on
3682 the right side of WINDOW. SIDE `left' means the new window shall be
3683 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3684 number of columns of the new window including space reserved for fringes
3685 and the scrollbar or a divider column.
3687 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3688 according to the SIDE argument.
3690 The new total and normal sizes of all involved windows must have been
3691 set correctly. See the code of `split-window' for how this is done. */)
3692 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3694 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3695 parent window or an internal window we have to install as OLD's new
3696 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3697 provided OLD is a leaf window, or to the frame's selected window.
3698 NEW (*n) is the new window created with some parameters taken from
3699 REFERENCE (*r). */
3700 register Lisp_Object new, frame, reference;
3701 register struct window *o, *p, *n, *r;
3702 struct frame *f;
3703 int horflag
3704 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3705 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3706 int combination_limit = 0;
3708 CHECK_WINDOW (old);
3709 o = XWINDOW (old);
3710 frame = WINDOW_FRAME (o);
3711 f = XFRAME (frame);
3713 CHECK_NUMBER (total_size);
3715 /* Set combination_limit to 1 if we have to make a new parent window.
3716 We do that if either `window-combination-limit' is t, or OLD has no
3717 parent, or OLD is ortho-combined. */
3718 combination_limit =
3719 !NILP (Vwindow_combination_limit)
3720 || NILP (o->parent)
3721 || NILP (horflag
3722 ? (XWINDOW (o->parent)->hchild)
3723 : (XWINDOW (o->parent)->vchild));
3725 /* We need a live reference window to initialize some parameters. */
3726 if (WINDOW_LIVE_P (old))
3727 /* OLD is live, use it as reference window. */
3728 reference = old;
3729 else
3730 /* Use the frame's selected window as reference window. */
3731 reference = FRAME_SELECTED_WINDOW (f);
3732 r = XWINDOW (reference);
3734 /* The following bugs are caught by `split-window'. */
3735 if (MINI_WINDOW_P (o))
3736 error ("Attempt to split minibuffer window");
3737 else if (XINT (total_size) < (horflag ? 2 : 1))
3738 error ("Size of new window too small (after split)");
3739 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3740 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3741 proportionally. */
3743 p = XWINDOW (o->parent);
3744 /* Temporarily pretend we split the parent window. */
3745 WSET (p, new_total,
3746 make_number (XINT (horflag ? p->total_cols : p->total_lines)
3747 - XINT (total_size)));
3748 if (!window_resize_check (p, horflag))
3749 error ("Window sizes don't fit");
3750 else
3751 /* Undo the temporary pretension. */
3752 WSET (p, new_total,
3753 horflag ? p->total_cols : p->total_lines);
3755 else
3757 if (!window_resize_check (o, horflag))
3758 error ("Resizing old window failed");
3759 else if (XINT (total_size) + XINT (o->new_total)
3760 != XINT (horflag ? o->total_cols : o->total_lines))
3761 error ("Sum of sizes of old and new window don't fit");
3764 /* This is our point of no return. */
3765 if (combination_limit)
3767 /* Save the old value of o->normal_cols/lines. It gets corrupted
3768 by make_parent_window and we need it below for assigning it to
3769 p->new_normal. */
3770 Lisp_Object new_normal
3771 = horflag ? o->normal_cols : o->normal_lines;
3773 make_parent_window (old, horflag);
3774 p = XWINDOW (o->parent);
3775 /* Store value of `window-combination-limit' in new parent's
3776 combination_limit slot. */
3777 WSET (p, combination_limit, Vwindow_combination_limit);
3778 /* These get applied below. */
3779 WSET (p, new_total, horflag ? o->total_cols : o->total_lines);
3780 WSET (p, new_normal, new_normal);
3782 else
3783 p = XWINDOW (o->parent);
3785 windows_or_buffers_changed++;
3786 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3787 new = make_window ();
3788 n = XWINDOW (new);
3789 WSET (n, frame, frame);
3790 WSET (n, parent, o->parent);
3791 WSET (n, vchild, Qnil);
3792 WSET (n, hchild, Qnil);
3794 if (EQ (side, Qabove) || EQ (side, Qleft))
3796 WSET (n, prev, o->prev);
3797 if (NILP (n->prev))
3798 if (horflag)
3799 WSET (p, hchild, new);
3800 else
3801 WSET (p, vchild, new);
3802 else
3803 WSET (XWINDOW (n->prev), next, new);
3804 WSET (n, next, old);
3805 WSET (o, prev, new);
3807 else
3809 WSET (n, next, o->next);
3810 if (!NILP (n->next))
3811 WSET (XWINDOW (n->next), prev, new);
3812 WSET (n, prev, old);
3813 WSET (o, next, new);
3816 WSET (n, buffer, Qt);
3817 WSET (n, window_end_valid, Qnil);
3818 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3820 /* Get special geometry settings from reference window. */
3821 WSET (n, left_margin_cols, r->left_margin_cols);
3822 WSET (n, right_margin_cols, r->right_margin_cols);
3823 WSET (n, left_fringe_width, r->left_fringe_width);
3824 WSET (n, right_fringe_width, r->right_fringe_width);
3825 n->fringes_outside_margins = r->fringes_outside_margins;
3826 WSET (n, scroll_bar_width, r->scroll_bar_width);
3827 WSET (n, vertical_scroll_bar_type, r->vertical_scroll_bar_type);
3829 /* Directly assign orthogonal coordinates and sizes. */
3830 if (horflag)
3832 WSET (n, top_line, o->top_line);
3833 WSET (n, total_lines, o->total_lines);
3835 else
3837 WSET (n, left_col, o->left_col);
3838 WSET (n, total_cols, o->total_cols);
3841 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3842 get them ready here. */
3843 WSET (n, new_total, total_size);
3844 WSET (n, new_normal, normal_size);
3846 BLOCK_INPUT;
3847 window_resize_apply (p, horflag);
3848 adjust_glyphs (f);
3849 /* Set buffer of NEW to buffer of reference window. Don't run
3850 any hooks. */
3851 set_window_buffer (new, r->buffer, 0, 1);
3852 UNBLOCK_INPUT;
3854 /* Maybe we should run the scroll functions in Elisp (which already
3855 runs the configuration change hook). */
3856 if (! NILP (Vwindow_scroll_functions))
3857 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3858 Fmarker_position (n->start));
3859 /* Return NEW. */
3860 return new;
3864 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3865 doc: /* Remove WINDOW from its frame.
3866 WINDOW defaults to the selected window. Return nil.
3867 Signal an error when WINDOW is the only window on its frame. */)
3868 (register Lisp_Object window)
3870 register Lisp_Object parent, sibling, frame, root;
3871 struct window *w, *p, *s, *r;
3872 struct frame *f;
3873 int horflag;
3874 int before_sibling = 0;
3876 w = decode_any_window (window);
3877 XSETWINDOW (window, w);
3878 if (NILP (w->buffer)
3879 && NILP (w->hchild) && NILP (w->vchild))
3880 /* It's a no-op to delete an already deleted window. */
3881 return Qnil;
3883 parent = w->parent;
3884 if (NILP (parent))
3885 /* Never delete a minibuffer or frame root window. */
3886 error ("Attempt to delete minibuffer or sole ordinary window");
3887 else if (NILP (w->prev) && NILP (w->next))
3888 /* Rather bow out here, this case should be handled on the Elisp
3889 level. */
3890 error ("Attempt to delete sole window of parent");
3892 p = XWINDOW (parent);
3893 horflag = NILP (p->vchild);
3895 frame = WINDOW_FRAME (w);
3896 f = XFRAME (frame);
3898 root = FRAME_ROOT_WINDOW (f);
3899 r = XWINDOW (root);
3901 /* Unlink WINDOW from window tree. */
3902 if (NILP (w->prev))
3903 /* Get SIBLING below (on the right of) WINDOW. */
3905 /* before_sibling 1 means WINDOW is the first child of its
3906 parent and thus before the sibling. */
3907 before_sibling = 1;
3908 sibling = w->next;
3909 s = XWINDOW (sibling);
3910 WSET (s, prev, Qnil);
3911 if (horflag)
3912 WSET (p, hchild, sibling);
3913 else
3914 WSET (p, vchild, sibling);
3916 else
3917 /* Get SIBLING above (on the left of) WINDOW. */
3919 sibling = w->prev;
3920 s = XWINDOW (sibling);
3921 WSET (s, next, w->next);
3922 if (!NILP (s->next))
3923 WSET (XWINDOW (s->next), prev, sibling);
3926 if (window_resize_check (r, horflag)
3927 && EQ (r->new_total,
3928 (horflag ? r->total_cols : r->total_lines)))
3929 /* We can delete WINDOW now. */
3932 /* Block input. */
3933 BLOCK_INPUT;
3934 window_resize_apply (p, horflag);
3936 /* If this window is referred to by the dpyinfo's mouse
3937 highlight, invalidate that slot to be safe (Bug#9904). */
3938 if (!FRAME_INITIAL_P (f))
3940 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3942 if (EQ (hlinfo->mouse_face_window, window))
3943 hlinfo->mouse_face_window = Qnil;
3946 windows_or_buffers_changed++;
3947 Vwindow_list = Qnil;
3948 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3950 WSET (w, next, Qnil); /* Don't delete w->next too. */
3951 free_window_matrices (w);
3953 if (!NILP (w->vchild))
3955 delete_all_child_windows (w->vchild);
3956 WSET (w, vchild, Qnil);
3958 else if (!NILP (w->hchild))
3960 delete_all_child_windows (w->hchild);
3961 WSET (w, hchild, Qnil);
3963 else if (!NILP (w->buffer))
3965 unshow_buffer (w);
3966 unchain_marker (XMARKER (w->pointm));
3967 unchain_marker (XMARKER (w->start));
3968 WSET (w, buffer, Qnil);
3971 if (NILP (s->prev) && NILP (s->next))
3972 /* A matrjoshka where SIBLING has become the only child of
3973 PARENT. */
3975 /* Put SIBLING into PARENT's place. */
3976 replace_window (parent, sibling, 0);
3977 /* Have SIBLING inherit the following three slot values from
3978 PARENT (the combination_limit slot is not inherited). */
3979 WSET (s, normal_cols, p->normal_cols);
3980 WSET (s, normal_lines, p->normal_lines);
3981 /* Mark PARENT as deleted. */
3982 WSET (p, vchild, Qnil);
3983 WSET (p, hchild, Qnil);
3984 /* Try to merge SIBLING into its new parent. */
3985 recombine_windows (sibling);
3988 adjust_glyphs (f);
3990 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
3991 /* We deleted the frame's selected window. */
3993 /* Use the frame's first window as fallback ... */
3994 Lisp_Object new_selected_window = Fframe_first_window (frame);
3995 /* ... but preferably use its most recently used window. */
3996 Lisp_Object mru_window;
3998 /* `get-mru-window' might fail for some reason so play it safe
3999 - promote the first window _without recording it_ first. */
4000 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4001 Fselect_window (new_selected_window, Qt);
4002 else
4003 FSET (f, selected_window, new_selected_window);
4005 UNBLOCK_INPUT;
4007 /* Now look whether `get-mru-window' gets us something. */
4008 mru_window = call1 (Qget_mru_window, frame);
4009 if (WINDOW_LIVE_P (mru_window)
4010 && EQ (XWINDOW (mru_window)->frame, frame))
4011 new_selected_window = mru_window;
4013 /* If all ended up well, we now promote the mru window. */
4014 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4015 Fselect_window (new_selected_window, Qnil);
4016 else
4017 FSET (f, selected_window, new_selected_window);
4019 else
4020 UNBLOCK_INPUT;
4022 /* Must be run by the caller:
4023 run_window_configuration_change_hook (f); */
4025 else
4026 /* We failed: Relink WINDOW into window tree. */
4028 if (before_sibling)
4030 WSET (s, prev, window);
4031 if (horflag)
4032 WSET (p, hchild, window);
4033 else
4034 WSET (p, vchild, window);
4036 else
4038 WSET (s, next, window);
4039 if (!NILP (w->next))
4040 WSET (XWINDOW (w->next), prev, window);
4042 error ("Deletion failed");
4045 return Qnil;
4048 /***********************************************************************
4049 Resizing Mini-Windows
4050 ***********************************************************************/
4052 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4053 can. */
4054 void
4055 grow_mini_window (struct window *w, int delta)
4057 struct frame *f = XFRAME (w->frame);
4058 struct window *r;
4059 Lisp_Object root, value;
4061 eassert (MINI_WINDOW_P (w));
4062 eassert (delta >= 0);
4064 root = FRAME_ROOT_WINDOW (f);
4065 r = XWINDOW (root);
4066 value = call2 (Qwindow_resize_root_window_vertically,
4067 root, make_number (- delta));
4068 if (INTEGERP (value) && window_resize_check (r, 0))
4070 BLOCK_INPUT;
4071 window_resize_apply (r, 0);
4073 /* Grow the mini-window. */
4074 WSET (w, top_line,
4075 make_number (XFASTINT (r->top_line) + XFASTINT (r->total_lines)));
4076 WSET (w, total_lines,
4077 make_number (XFASTINT (w->total_lines) - XINT (value)));
4078 w->last_modified = 0;
4079 w->last_overlay_modified = 0;
4081 adjust_glyphs (f);
4082 UNBLOCK_INPUT;
4087 /* Shrink mini-window W. */
4088 void
4089 shrink_mini_window (struct window *w)
4091 struct frame *f = XFRAME (w->frame);
4092 struct window *r;
4093 Lisp_Object root, value;
4094 EMACS_INT size;
4096 eassert (MINI_WINDOW_P (w));
4098 size = XINT (w->total_lines);
4099 if (size > 1)
4101 root = FRAME_ROOT_WINDOW (f);
4102 r = XWINDOW (root);
4103 value = call2 (Qwindow_resize_root_window_vertically,
4104 root, make_number (size - 1));
4105 if (INTEGERP (value) && window_resize_check (r, 0))
4107 BLOCK_INPUT;
4108 window_resize_apply (r, 0);
4110 /* Shrink the mini-window. */
4111 WSET (w, top_line,
4112 make_number (XFASTINT (r->top_line) + XFASTINT (r->total_lines)));
4113 WSET (w, total_lines, make_number (1));
4115 w->last_modified = 0;
4116 w->last_overlay_modified = 0;
4118 adjust_glyphs (f);
4119 UNBLOCK_INPUT;
4121 /* If the above failed for whatever strange reason we must make a
4122 one window frame here. The same routine will be needed when
4123 shrinking the frame (and probably when making the initial
4124 *scratch* window). For the moment leave things as they are. */
4128 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4129 doc: /* Resize minibuffer window WINDOW. */)
4130 (Lisp_Object window)
4132 struct window *w = XWINDOW (window);
4133 struct window *r;
4134 struct frame *f;
4135 int height;
4137 CHECK_WINDOW (window);
4138 f = XFRAME (w->frame);
4140 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4141 error ("Not a valid minibuffer window");
4142 else if (FRAME_MINIBUF_ONLY_P (f))
4143 error ("Cannot resize a minibuffer-only frame");
4145 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4146 height = XINT (r->total_lines) + XINT (w->total_lines);
4147 if (window_resize_check (r, 0)
4148 && XINT (w->new_total) > 0
4149 && height == XINT (r->new_total) + XINT (w->new_total))
4151 BLOCK_INPUT;
4152 window_resize_apply (r, 0);
4154 WSET (w, total_lines, w->new_total);
4155 WSET (w, top_line,
4156 make_number (XINT (r->top_line) + XINT (r->total_lines)));
4158 windows_or_buffers_changed++;
4159 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4160 adjust_glyphs (f);
4161 UNBLOCK_INPUT;
4163 run_window_configuration_change_hook (f);
4164 return Qt;
4166 else error ("Failed to resize minibuffer window");
4169 /* Mark window cursors off for all windows in the window tree rooted
4170 at W by setting their phys_cursor_on_p flag to zero. Called from
4171 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4172 the frame are cleared. */
4174 void
4175 mark_window_cursors_off (struct window *w)
4177 while (w)
4179 if (!NILP (w->hchild))
4180 mark_window_cursors_off (XWINDOW (w->hchild));
4181 else if (!NILP (w->vchild))
4182 mark_window_cursors_off (XWINDOW (w->vchild));
4183 else
4184 w->phys_cursor_on_p = 0;
4186 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4191 /* Return number of lines of text (not counting mode lines) in W. */
4194 window_internal_height (struct window *w)
4196 int ht = XFASTINT (w->total_lines);
4198 if (!MINI_WINDOW_P (w))
4200 if (!NILP (w->parent)
4201 || !NILP (w->vchild)
4202 || !NILP (w->hchild)
4203 || !NILP (w->next)
4204 || !NILP (w->prev)
4205 || WINDOW_WANTS_MODELINE_P (w))
4206 --ht;
4208 if (WINDOW_WANTS_HEADER_LINE_P (w))
4209 --ht;
4212 return ht;
4215 /************************************************************************
4216 Window Scrolling
4217 ***********************************************************************/
4219 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4220 N screen-fulls, which is defined as the height of the window minus
4221 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4222 instead. Negative values of N mean scroll down. NOERROR non-zero
4223 means don't signal an error if we try to move over BEGV or ZV,
4224 respectively. */
4226 static void
4227 window_scroll (Lisp_Object window, EMACS_INT n, int whole, int noerror)
4229 immediate_quit = 1;
4230 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4232 /* If we must, use the pixel-based version which is much slower than
4233 the line-based one but can handle varying line heights. */
4234 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4235 window_scroll_pixel_based (window, n, whole, noerror);
4236 else
4237 window_scroll_line_based (window, n, whole, noerror);
4239 immediate_quit = 0;
4243 /* Implementation of window_scroll that works based on pixel line
4244 heights. See the comment of window_scroll for parameter
4245 descriptions. */
4247 static void
4248 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4250 struct it it;
4251 struct window *w = XWINDOW (window);
4252 struct text_pos start;
4253 int this_scroll_margin;
4254 /* True if we fiddled the window vscroll field without really scrolling. */
4255 int vscrolled = 0;
4256 int x, y, rtop, rbot, rowh, vpos;
4257 void *itdata = NULL;
4259 SET_TEXT_POS_FROM_MARKER (start, w->start);
4260 /* Scrolling a minibuffer window via scroll bar when the echo area
4261 shows long text sometimes resets the minibuffer contents behind
4262 our backs. */
4263 if (CHARPOS (start) > ZV)
4264 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4266 /* If PT is not visible in WINDOW, move back one half of
4267 the screen. Allow PT to be partially visible, otherwise
4268 something like (scroll-down 1) with PT in the line before
4269 the partially visible one would recenter. */
4271 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4273 itdata = bidi_shelve_cache ();
4274 /* Move backward half the height of the window. Performance note:
4275 vmotion used here is about 10% faster, but would give wrong
4276 results for variable height lines. */
4277 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4278 it.current_y = it.last_visible_y;
4279 move_it_vertically_backward (&it, window_box_height (w) / 2);
4281 /* The function move_iterator_vertically may move over more than
4282 the specified y-distance. If it->w is small, e.g. a
4283 mini-buffer window, we may end up in front of the window's
4284 display area. This is the case when Start displaying at the
4285 start of the line containing PT in this case. */
4286 if (it.current_y <= 0)
4288 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4289 move_it_vertically_backward (&it, 0);
4290 it.current_y = 0;
4293 start = it.current.pos;
4294 bidi_unshelve_cache (itdata, 0);
4296 else if (auto_window_vscroll_p)
4298 if (rtop || rbot) /* partially visible */
4300 int px;
4301 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4302 if (whole)
4303 dy = max ((window_box_height (w)
4304 - next_screen_context_lines * dy),
4305 dy);
4306 dy *= n;
4308 if (n < 0)
4310 /* Only vscroll backwards if already vscrolled forwards. */
4311 if (w->vscroll < 0 && rtop > 0)
4313 px = max (0, -w->vscroll - min (rtop, -dy));
4314 Fset_window_vscroll (window, make_number (px), Qt);
4315 return;
4318 if (n > 0)
4320 /* Do vscroll if already vscrolled or only display line. */
4321 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4323 px = max (0, -w->vscroll + min (rbot, dy));
4324 Fset_window_vscroll (window, make_number (px), Qt);
4325 return;
4328 /* Maybe modify window start instead of scrolling. */
4329 if (rbot > 0 || w->vscroll < 0)
4331 ptrdiff_t spos;
4333 Fset_window_vscroll (window, make_number (0), Qt);
4334 /* If there are other text lines above the current row,
4335 move window start to current row. Else to next row. */
4336 if (rbot > 0)
4337 spos = XINT (Fline_beginning_position (Qnil));
4338 else
4339 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4340 set_marker_restricted (w->start, make_number (spos),
4341 w->buffer);
4342 w->start_at_line_beg = 1;
4343 w->update_mode_line = 1;
4344 w->last_modified = 0;
4345 w->last_overlay_modified = 0;
4346 /* Set force_start so that redisplay_window will run the
4347 window-scroll-functions. */
4348 w->force_start = 1;
4349 return;
4353 /* Cancel previous vscroll. */
4354 Fset_window_vscroll (window, make_number (0), Qt);
4357 itdata = bidi_shelve_cache ();
4358 /* If scroll_preserve_screen_position is non-nil, we try to set
4359 point in the same window line as it is now, so get that line. */
4360 if (!NILP (Vscroll_preserve_screen_position))
4362 /* We preserve the goal pixel coordinate across consecutive
4363 calls to scroll-up, scroll-down and other commands that
4364 have the `scroll-command' property. This avoids the
4365 possibility of point becoming "stuck" on a tall line when
4366 scrolling by one line. */
4367 if (window_scroll_pixel_based_preserve_y < 0
4368 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4369 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4371 start_display (&it, w, start);
4372 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4373 window_scroll_pixel_based_preserve_y = it.current_y;
4374 window_scroll_pixel_based_preserve_x = it.current_x;
4377 else
4378 window_scroll_pixel_based_preserve_y
4379 = window_scroll_pixel_based_preserve_x = -1;
4381 /* Move iterator it from start the specified distance forward or
4382 backward. The result is the new window start. */
4383 start_display (&it, w, start);
4384 if (whole)
4386 ptrdiff_t start_pos = IT_CHARPOS (it);
4387 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4388 dy = max ((window_box_height (w)
4389 - next_screen_context_lines * dy),
4390 dy) * n;
4392 /* Note that move_it_vertically always moves the iterator to the
4393 start of a line. So, if the last line doesn't have a newline,
4394 we would end up at the start of the line ending at ZV. */
4395 if (dy <= 0)
4397 move_it_vertically_backward (&it, -dy);
4398 /* Ensure we actually do move, e.g. in case we are currently
4399 looking at an image that is taller that the window height. */
4400 while (start_pos == IT_CHARPOS (it)
4401 && start_pos > BEGV)
4402 move_it_by_lines (&it, -1);
4404 else if (dy > 0)
4406 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4407 MOVE_TO_POS | MOVE_TO_Y);
4408 /* Ensure we actually do move, e.g. in case we are currently
4409 looking at an image that is taller that the window height. */
4410 while (start_pos == IT_CHARPOS (it)
4411 && start_pos < ZV)
4412 move_it_by_lines (&it, 1);
4415 else
4416 move_it_by_lines (&it, n);
4418 /* We failed if we find ZV is already on the screen (scrolling up,
4419 means there's nothing past the end), or if we can't start any
4420 earlier (scrolling down, means there's nothing past the top). */
4421 if ((n > 0 && IT_CHARPOS (it) == ZV)
4422 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4424 if (IT_CHARPOS (it) == ZV)
4426 if (it.current_y < it.last_visible_y
4427 && (it.current_y + it.max_ascent + it.max_descent
4428 > it.last_visible_y))
4430 /* The last line was only partially visible, make it fully
4431 visible. */
4432 w->vscroll = (it.last_visible_y
4433 - it.current_y + it.max_ascent + it.max_descent);
4434 adjust_glyphs (it.f);
4436 else
4438 bidi_unshelve_cache (itdata, 0);
4439 if (noerror)
4440 return;
4441 else if (n < 0) /* could happen with empty buffers */
4442 xsignal0 (Qbeginning_of_buffer);
4443 else
4444 xsignal0 (Qend_of_buffer);
4447 else
4449 if (w->vscroll != 0)
4450 /* The first line was only partially visible, make it fully
4451 visible. */
4452 w->vscroll = 0;
4453 else
4455 bidi_unshelve_cache (itdata, 0);
4456 if (noerror)
4457 return;
4458 else
4459 xsignal0 (Qbeginning_of_buffer);
4463 /* If control gets here, then we vscrolled. */
4465 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4467 /* Don't try to change the window start below. */
4468 vscrolled = 1;
4471 if (! vscrolled)
4473 ptrdiff_t pos = IT_CHARPOS (it);
4474 ptrdiff_t bytepos;
4476 /* If in the middle of a multi-glyph character move forward to
4477 the next character. */
4478 if (in_display_vector_p (&it))
4480 ++pos;
4481 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4484 /* Set the window start, and set up the window for redisplay. */
4485 set_marker_restricted (w->start, make_number (pos),
4486 w->buffer);
4487 bytepos = XMARKER (w->start)->bytepos;
4488 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4489 w->update_mode_line = 1;
4490 w->last_modified = 0;
4491 w->last_overlay_modified = 0;
4492 /* Set force_start so that redisplay_window will run the
4493 window-scroll-functions. */
4494 w->force_start = 1;
4497 /* The rest of this function uses current_y in a nonstandard way,
4498 not including the height of the header line if any. */
4499 it.current_y = it.vpos = 0;
4501 /* Move PT out of scroll margins.
4502 This code wants current_y to be zero at the window start position
4503 even if there is a header line. */
4504 this_scroll_margin = max (0, scroll_margin);
4505 this_scroll_margin
4506 = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4507 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4509 if (n > 0)
4511 /* We moved the window start towards ZV, so PT may be now
4512 in the scroll margin at the top. */
4513 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4514 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4515 && (NILP (Vscroll_preserve_screen_position)
4516 || EQ (Vscroll_preserve_screen_position, Qt)))
4517 /* We found PT at a legitimate height. Leave it alone. */
4519 else if (window_scroll_pixel_based_preserve_y >= 0)
4521 /* If we have a header line, take account of it.
4522 This is necessary because we set it.current_y to 0, above. */
4523 move_it_to (&it, -1,
4524 window_scroll_pixel_based_preserve_x,
4525 window_scroll_pixel_based_preserve_y
4526 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4527 -1, MOVE_TO_Y | MOVE_TO_X);
4528 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4530 else
4532 while (it.current_y < this_scroll_margin)
4534 int prev = it.current_y;
4535 move_it_by_lines (&it, 1);
4536 if (prev == it.current_y)
4537 break;
4539 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4542 else if (n < 0)
4544 ptrdiff_t charpos, bytepos;
4545 int partial_p;
4547 /* Save our position, for the
4548 window_scroll_pixel_based_preserve_y case. */
4549 charpos = IT_CHARPOS (it);
4550 bytepos = IT_BYTEPOS (it);
4552 /* We moved the window start towards BEGV, so PT may be now
4553 in the scroll margin at the bottom. */
4554 move_it_to (&it, PT, -1,
4555 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4556 - this_scroll_margin - 1),
4558 MOVE_TO_POS | MOVE_TO_Y);
4560 /* Save our position, in case it's correct. */
4561 charpos = IT_CHARPOS (it);
4562 bytepos = IT_BYTEPOS (it);
4564 /* See if point is on a partially visible line at the end. */
4565 if (it.what == IT_EOB)
4566 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4567 else
4569 move_it_by_lines (&it, 1);
4570 partial_p = it.current_y > it.last_visible_y;
4573 if (charpos == PT && !partial_p
4574 && (NILP (Vscroll_preserve_screen_position)
4575 || EQ (Vscroll_preserve_screen_position, Qt)))
4576 /* We found PT before we found the display margin, so PT is ok. */
4578 else if (window_scroll_pixel_based_preserve_y >= 0)
4580 SET_TEXT_POS_FROM_MARKER (start, w->start);
4581 start_display (&it, w, start);
4582 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4583 here because we called start_display again and did not
4584 alter it.current_y this time. */
4585 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4586 window_scroll_pixel_based_preserve_y, -1,
4587 MOVE_TO_Y | MOVE_TO_X);
4588 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4590 else
4592 if (partial_p)
4593 /* The last line was only partially visible, so back up two
4594 lines to make sure we're on a fully visible line. */
4596 move_it_by_lines (&it, -2);
4597 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4599 else
4600 /* No, the position we saved is OK, so use it. */
4601 SET_PT_BOTH (charpos, bytepos);
4604 bidi_unshelve_cache (itdata, 0);
4608 /* Implementation of window_scroll that works based on screen lines.
4609 See the comment of window_scroll for parameter descriptions. */
4611 static void
4612 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4614 register struct window *w = XWINDOW (window);
4615 /* Fvertical_motion enters redisplay, which can trigger
4616 fontification, which in turn can modify buffer text (e.g., if the
4617 fontification functions replace escape sequences with faces, as
4618 in `grep-mode-font-lock-keywords'). So we use a marker to record
4619 the old point position, to prevent crashes in SET_PT_BOTH. */
4620 Lisp_Object opoint_marker = Fpoint_marker ();
4621 register ptrdiff_t pos, pos_byte;
4622 register int ht = window_internal_height (w);
4623 register Lisp_Object tem;
4624 int lose;
4625 Lisp_Object bolp;
4626 ptrdiff_t startpos;
4627 Lisp_Object original_pos = Qnil;
4629 /* If scrolling screen-fulls, compute the number of lines to
4630 scroll from the window's height. */
4631 if (whole)
4632 n *= max (1, ht - next_screen_context_lines);
4634 startpos = marker_position (w->start);
4636 if (!NILP (Vscroll_preserve_screen_position))
4638 if (window_scroll_preserve_vpos <= 0
4639 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4640 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4642 struct position posit
4643 = *compute_motion (startpos, 0, 0, 0,
4644 PT, ht, 0,
4645 -1, w->hscroll,
4646 0, w);
4647 window_scroll_preserve_vpos = posit.vpos;
4648 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4651 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4652 make_number (window_scroll_preserve_vpos));
4655 XSETFASTINT (tem, PT);
4656 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4658 if (NILP (tem))
4660 Fvertical_motion (make_number (- (ht / 2)), window);
4661 startpos = PT;
4664 SET_PT (startpos);
4665 lose = n < 0 && PT == BEGV;
4666 Fvertical_motion (make_number (n), window);
4667 pos = PT;
4668 pos_byte = PT_BYTE;
4669 bolp = Fbolp ();
4670 SET_PT_BOTH (marker_position (opoint_marker),
4671 marker_byte_position (opoint_marker));
4673 if (lose)
4675 if (noerror)
4676 return;
4677 else
4678 xsignal0 (Qbeginning_of_buffer);
4681 if (pos < ZV)
4683 /* Don't use a scroll margin that is negative or too large. */
4684 int this_scroll_margin =
4685 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4687 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4688 w->start_at_line_beg = !NILP (bolp);
4689 w->update_mode_line = 1;
4690 w->last_modified = 0;
4691 w->last_overlay_modified = 0;
4692 /* Set force_start so that redisplay_window will run
4693 the window-scroll-functions. */
4694 w->force_start = 1;
4696 if (!NILP (Vscroll_preserve_screen_position)
4697 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4699 SET_PT_BOTH (pos, pos_byte);
4700 Fvertical_motion (original_pos, window);
4702 /* If we scrolled forward, put point enough lines down
4703 that it is outside the scroll margin. */
4704 else if (n > 0)
4706 int top_margin;
4708 if (this_scroll_margin > 0)
4710 SET_PT_BOTH (pos, pos_byte);
4711 Fvertical_motion (make_number (this_scroll_margin), window);
4712 top_margin = PT;
4714 else
4715 top_margin = pos;
4717 if (top_margin <= marker_position (opoint_marker))
4718 SET_PT_BOTH (marker_position (opoint_marker),
4719 marker_byte_position (opoint_marker));
4720 else if (!NILP (Vscroll_preserve_screen_position))
4722 SET_PT_BOTH (pos, pos_byte);
4723 Fvertical_motion (original_pos, window);
4725 else
4726 SET_PT (top_margin);
4728 else if (n < 0)
4730 int bottom_margin;
4732 /* If we scrolled backward, put point near the end of the window
4733 but not within the scroll margin. */
4734 SET_PT_BOTH (pos, pos_byte);
4735 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4736 if (XFASTINT (tem) == ht - this_scroll_margin)
4737 bottom_margin = PT;
4738 else
4739 bottom_margin = PT + 1;
4741 if (bottom_margin > marker_position (opoint_marker))
4742 SET_PT_BOTH (marker_position (opoint_marker),
4743 marker_byte_position (opoint_marker));
4744 else
4746 if (!NILP (Vscroll_preserve_screen_position))
4748 SET_PT_BOTH (pos, pos_byte);
4749 Fvertical_motion (original_pos, window);
4751 else
4752 Fvertical_motion (make_number (-1), window);
4756 else
4758 if (noerror)
4759 return;
4760 else
4761 xsignal0 (Qend_of_buffer);
4766 /* Scroll selected_window up or down. If N is nil, scroll a
4767 screen-full which is defined as the height of the window minus
4768 next_screen_context_lines. If N is the symbol `-', scroll.
4769 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4770 up. This is the guts of Fscroll_up and Fscroll_down. */
4772 static void
4773 scroll_command (Lisp_Object n, int direction)
4775 ptrdiff_t count = SPECPDL_INDEX ();
4777 eassert (eabs (direction) == 1);
4779 /* If selected window's buffer isn't current, make it current for
4780 the moment. But don't screw up if window_scroll gets an error. */
4781 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4783 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4784 Fset_buffer (XWINDOW (selected_window)->buffer);
4786 /* Make redisplay consider other windows than just selected_window. */
4787 ++windows_or_buffers_changed;
4790 if (NILP (n))
4791 window_scroll (selected_window, direction, 1, 0);
4792 else if (EQ (n, Qminus))
4793 window_scroll (selected_window, -direction, 1, 0);
4794 else
4796 n = Fprefix_numeric_value (n);
4797 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4800 unbind_to (count, Qnil);
4803 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4804 doc: /* Scroll text of selected window upward ARG lines.
4805 If ARG is omitted or nil, scroll upward 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 downward.
4808 If ARG is the atom `-', scroll downward 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 ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4817 doc: /* Scroll text of selected window down ARG lines.
4818 If ARG is omitted or nil, scroll down by a near full screen.
4819 A near full screen is `next-screen-context-lines' less than a full screen.
4820 Negative ARG means scroll upward.
4821 If ARG is the atom `-', scroll upward by nearly full screen.
4822 When calling from a program, supply as argument a number, nil, or `-'. */)
4823 (Lisp_Object arg)
4825 scroll_command (arg, -1);
4826 return Qnil;
4829 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4830 doc: /* Return the other window for \"other window scroll\" commands.
4831 If `other-window-scroll-buffer' is non-nil, a window
4832 showing that buffer is used.
4833 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4834 specifies the window. This takes precedence over
4835 `other-window-scroll-buffer'. */)
4836 (void)
4838 Lisp_Object window;
4840 if (MINI_WINDOW_P (XWINDOW (selected_window))
4841 && !NILP (Vminibuf_scroll_window))
4842 window = Vminibuf_scroll_window;
4843 /* If buffer is specified, scroll that buffer. */
4844 else if (!NILP (Vother_window_scroll_buffer))
4846 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4847 if (NILP (window))
4848 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4850 else
4852 /* Nothing specified; look for a neighboring window on the same
4853 frame. */
4854 window = Fnext_window (selected_window, Qnil, Qnil);
4856 if (EQ (window, selected_window))
4857 /* That didn't get us anywhere; look for a window on another
4858 visible frame. */
4860 window = Fnext_window (window, Qnil, Qt);
4861 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4862 && ! EQ (window, selected_window));
4865 CHECK_LIVE_WINDOW (window);
4867 if (EQ (window, selected_window))
4868 error ("There is no other window");
4870 return window;
4873 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4874 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4875 A near full screen is `next-screen-context-lines' less than a full screen.
4876 The next window is the one below the current one; or the one at the top
4877 if the current one is at the bottom. Negative ARG means scroll downward.
4878 If ARG is the atom `-', scroll downward by nearly full screen.
4879 When calling from a program, supply as argument a number, nil, or `-'.
4881 If `other-window-scroll-buffer' is non-nil, scroll the window
4882 showing that buffer, popping the buffer up if necessary.
4883 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4884 specifies the window to scroll. This takes precedence over
4885 `other-window-scroll-buffer'. */)
4886 (Lisp_Object arg)
4888 Lisp_Object window;
4889 struct window *w;
4890 ptrdiff_t count = SPECPDL_INDEX ();
4892 window = Fother_window_for_scrolling ();
4893 w = XWINDOW (window);
4895 /* Don't screw up if window_scroll gets an error. */
4896 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4897 ++windows_or_buffers_changed;
4899 Fset_buffer (w->buffer);
4900 SET_PT (marker_position (w->pointm));
4902 if (NILP (arg))
4903 window_scroll (window, 1, 1, 1);
4904 else if (EQ (arg, Qminus))
4905 window_scroll (window, -1, 1, 1);
4906 else
4908 if (CONSP (arg))
4909 arg = XCAR (arg);
4910 CHECK_NUMBER (arg);
4911 window_scroll (window, XINT (arg), 0, 1);
4914 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4915 unbind_to (count, Qnil);
4917 return Qnil;
4920 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4921 doc: /* Scroll selected window display ARG columns left.
4922 Default for ARG is window width minus 2.
4923 Value is the total amount of leftward horizontal scrolling in
4924 effect after the change.
4925 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4926 lower bound for automatic scrolling, i.e. automatic scrolling
4927 will not scroll a window to a column less than the value returned
4928 by this function. This happens in an interactive call. */)
4929 (register Lisp_Object arg, Lisp_Object set_minimum)
4931 struct window *w = XWINDOW (selected_window);
4932 EMACS_INT requested_arg = (NILP (arg)
4933 ? window_body_cols (w) - 2
4934 : XINT (Fprefix_numeric_value (arg)));
4935 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
4937 if (!NILP (set_minimum))
4938 w->min_hscroll = w->hscroll;
4940 return result;
4943 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4944 doc: /* Scroll selected window display ARG columns right.
4945 Default for ARG is window width minus 2.
4946 Value is the total amount of leftward horizontal scrolling in
4947 effect after the change.
4948 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4949 lower bound for automatic scrolling, i.e. automatic scrolling
4950 will not scroll a window to a column less than the value returned
4951 by this function. This happens in an interactive call. */)
4952 (register Lisp_Object arg, Lisp_Object set_minimum)
4954 struct window *w = XWINDOW (selected_window);
4955 EMACS_INT requested_arg = (NILP (arg)
4956 ? window_body_cols (w) - 2
4957 : XINT (Fprefix_numeric_value (arg)));
4958 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
4960 if (!NILP (set_minimum))
4961 w->min_hscroll = w->hscroll;
4963 return result;
4966 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4967 doc: /* Return the window which was selected when entering the minibuffer.
4968 Returns nil, if selected window is not a minibuffer window. */)
4969 (void)
4971 if (minibuf_level > 0
4972 && MINI_WINDOW_P (XWINDOW (selected_window))
4973 && WINDOW_LIVE_P (minibuf_selected_window))
4974 return minibuf_selected_window;
4976 return Qnil;
4979 /* Value is the number of lines actually displayed in window W,
4980 as opposed to its height. */
4982 static int
4983 displayed_window_lines (struct window *w)
4985 struct it it;
4986 struct text_pos start;
4987 int height = window_box_height (w);
4988 struct buffer *old_buffer;
4989 int bottom_y;
4990 void *itdata = NULL;
4992 if (XBUFFER (w->buffer) != current_buffer)
4994 old_buffer = current_buffer;
4995 set_buffer_internal (XBUFFER (w->buffer));
4997 else
4998 old_buffer = NULL;
5000 /* In case W->start is out of the accessible range, do something
5001 reasonable. This happens in Info mode when Info-scroll-down
5002 calls (recenter -1) while W->start is 1. */
5003 if (XMARKER (w->start)->charpos < BEGV)
5004 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5005 else if (XMARKER (w->start)->charpos > ZV)
5006 SET_TEXT_POS (start, ZV, ZV_BYTE);
5007 else
5008 SET_TEXT_POS_FROM_MARKER (start, w->start);
5010 itdata = bidi_shelve_cache ();
5011 start_display (&it, w, start);
5012 move_it_vertically (&it, height);
5013 bottom_y = line_bottom_y (&it);
5014 bidi_unshelve_cache (itdata, 0);
5016 /* rms: On a non-window display,
5017 the value of it.vpos at the bottom of the screen
5018 seems to be 1 larger than window_box_height (w).
5019 This kludge fixes a bug whereby (move-to-window-line -1)
5020 when ZV is on the last screen line
5021 moves to the previous screen line instead of the last one. */
5022 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5023 height++;
5025 /* Add in empty lines at the bottom of the window. */
5026 if (bottom_y < height)
5028 int uy = FRAME_LINE_HEIGHT (it.f);
5029 it.vpos += (height - bottom_y + uy - 1) / uy;
5032 if (old_buffer)
5033 set_buffer_internal (old_buffer);
5035 return it.vpos;
5039 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5040 doc: /* Center point in selected window and maybe redisplay frame.
5041 With prefix argument ARG, recenter putting point on screen line ARG
5042 relative to the selected window. If ARG is negative, it counts up from the
5043 bottom of the window. (ARG should be less than the height of the window.)
5045 If ARG is omitted or nil, then recenter with point on the middle line of
5046 the selected window; if the variable `recenter-redisplay' is non-nil,
5047 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5048 is set to `grow-only', this resets the tool-bar's height to the minimum
5049 height needed); if `recenter-redisplay' has the special value `tty',
5050 then only tty frames are redrawn.
5052 Just C-u as prefix means put point in the center of the window
5053 and redisplay normally--don't erase and redraw the frame. */)
5054 (register Lisp_Object arg)
5056 struct window *w = XWINDOW (selected_window);
5057 struct buffer *buf = XBUFFER (w->buffer);
5058 struct buffer *obuf = current_buffer;
5059 int center_p = 0;
5060 ptrdiff_t charpos, bytepos;
5061 EMACS_INT iarg IF_LINT (= 0);
5062 int this_scroll_margin;
5064 /* If redisplay is suppressed due to an error, try again. */
5065 obuf->display_error_modiff = 0;
5067 if (NILP (arg))
5069 if (!NILP (Vrecenter_redisplay)
5070 && (!EQ (Vrecenter_redisplay, Qtty)
5071 || !NILP (Ftty_type (selected_frame))))
5073 ptrdiff_t i;
5075 /* Invalidate pixel data calculated for all compositions. */
5076 for (i = 0; i < n_compositions; i++)
5077 composition_table[i]->font = NULL;
5079 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5081 Fredraw_frame (WINDOW_FRAME (w));
5082 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5085 center_p = 1;
5087 else if (CONSP (arg)) /* Just C-u. */
5088 center_p = 1;
5089 else
5091 arg = Fprefix_numeric_value (arg);
5092 CHECK_NUMBER (arg);
5093 iarg = XINT (arg);
5096 set_buffer_internal (buf);
5098 /* Do this after making BUF current
5099 in case scroll_margin is buffer-local. */
5100 this_scroll_margin =
5101 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5103 /* Handle centering on a graphical frame specially. Such frames can
5104 have variable-height lines and centering point on the basis of
5105 line counts would lead to strange effects. */
5106 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5108 if (center_p)
5110 struct it it;
5111 struct text_pos pt;
5112 void *itdata = bidi_shelve_cache ();
5114 SET_TEXT_POS (pt, PT, PT_BYTE);
5115 start_display (&it, w, pt);
5116 move_it_vertically_backward (&it, window_box_height (w) / 2);
5117 charpos = IT_CHARPOS (it);
5118 bytepos = IT_BYTEPOS (it);
5119 bidi_unshelve_cache (itdata, 0);
5121 else if (iarg < 0)
5123 struct it it;
5124 struct text_pos pt;
5125 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5126 int extra_line_spacing;
5127 int h = window_box_height (w);
5128 void *itdata = bidi_shelve_cache ();
5130 iarg = - max (-iarg, this_scroll_margin);
5132 SET_TEXT_POS (pt, PT, PT_BYTE);
5133 start_display (&it, w, pt);
5135 /* Be sure we have the exact height of the full line containing PT. */
5136 move_it_by_lines (&it, 0);
5138 /* The amount of pixels we have to move back is the window
5139 height minus what's displayed in the line containing PT,
5140 and the lines below. */
5141 it.current_y = 0;
5142 it.vpos = 0;
5143 move_it_by_lines (&it, nlines);
5145 if (it.vpos == nlines)
5146 h -= it.current_y;
5147 else
5149 /* Last line has no newline */
5150 h -= line_bottom_y (&it);
5151 it.vpos++;
5154 /* Don't reserve space for extra line spacing of last line. */
5155 extra_line_spacing = it.max_extra_line_spacing;
5157 /* If we can't move down NLINES lines because we hit
5158 the end of the buffer, count in some empty lines. */
5159 if (it.vpos < nlines)
5161 nlines -= it.vpos;
5162 extra_line_spacing = it.extra_line_spacing;
5163 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5165 if (h <= 0)
5167 bidi_unshelve_cache (itdata, 0);
5168 return Qnil;
5171 /* Now find the new top line (starting position) of the window. */
5172 start_display (&it, w, pt);
5173 it.current_y = 0;
5174 move_it_vertically_backward (&it, h);
5176 /* If extra line spacing is present, we may move too far
5177 back. This causes the last line to be only partially
5178 visible (which triggers redisplay to recenter that line
5179 in the middle), so move forward.
5180 But ignore extra line spacing on last line, as it is not
5181 considered to be part of the visible height of the line.
5183 h += extra_line_spacing;
5184 while (-it.current_y > h)
5185 move_it_by_lines (&it, 1);
5187 charpos = IT_CHARPOS (it);
5188 bytepos = IT_BYTEPOS (it);
5190 bidi_unshelve_cache (itdata, 0);
5192 else
5194 struct position pos;
5196 iarg = max (iarg, this_scroll_margin);
5198 pos = *vmotion (PT, -iarg, w);
5199 charpos = pos.bufpos;
5200 bytepos = pos.bytepos;
5203 else
5205 struct position pos;
5206 int ht = window_internal_height (w);
5208 if (center_p)
5209 iarg = ht / 2;
5210 else if (iarg < 0)
5211 iarg += ht;
5213 /* Don't let it get into the margin at either top or bottom. */
5214 iarg = max (iarg, this_scroll_margin);
5215 iarg = min (iarg, ht - this_scroll_margin - 1);
5217 pos = *vmotion (PT, - iarg, w);
5218 charpos = pos.bufpos;
5219 bytepos = pos.bytepos;
5222 /* Set the new window start. */
5223 set_marker_both (w->start, w->buffer, charpos, bytepos);
5224 WSET (w, window_end_valid, Qnil);
5226 w->optional_new_start = 1;
5228 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5229 FETCH_BYTE (bytepos - 1) == '\n');
5231 set_buffer_internal (obuf);
5232 return Qnil;
5235 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5236 0, 1, 0,
5237 doc: /* Return the height in lines of the text display area of WINDOW.
5238 WINDOW must be a live window and defaults to the selected one.
5240 The returned height does not include the mode line, any header line,
5241 nor any partial-height lines at the bottom of the text area. */)
5242 (Lisp_Object window)
5244 struct window *w = decode_live_window (window);
5245 int pixel_height = window_box_height (w);
5246 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5247 return make_number (line_height);
5252 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5253 1, 1, "P",
5254 doc: /* Position point relative to window.
5255 ARG nil means position point at center of window.
5256 Else, ARG specifies vertical position within the window;
5257 zero means top of window, negative means relative to bottom of window. */)
5258 (Lisp_Object arg)
5260 struct window *w = XWINDOW (selected_window);
5261 int lines, start;
5262 Lisp_Object window;
5263 #if 0
5264 int this_scroll_margin;
5265 #endif
5267 if (!(BUFFERP (w->buffer)
5268 && XBUFFER (w->buffer) == current_buffer))
5269 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5270 when passed below to set_marker_both. */
5271 error ("move-to-window-line called from unrelated buffer");
5273 window = selected_window;
5274 start = marker_position (w->start);
5275 if (start < BEGV || start > ZV)
5277 int height = window_internal_height (w);
5278 Fvertical_motion (make_number (- (height / 2)), window);
5279 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5280 w->start_at_line_beg = !NILP (Fbolp ());
5281 w->force_start = 1;
5283 else
5284 Fgoto_char (w->start);
5286 lines = displayed_window_lines (w);
5288 #if 0
5289 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5290 #endif
5292 if (NILP (arg))
5293 XSETFASTINT (arg, lines / 2);
5294 else
5296 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5298 if (iarg < 0)
5299 iarg = iarg + lines;
5301 #if 0 /* This code would prevent move-to-window-line from moving point
5302 to a place inside the scroll margins (which would cause the
5303 next redisplay to scroll). I wrote this code, but then concluded
5304 it is probably better not to install it. However, it is here
5305 inside #if 0 so as not to lose it. -- rms. */
5307 /* Don't let it get into the margin at either top or bottom. */
5308 iarg = max (iarg, this_scroll_margin);
5309 iarg = min (iarg, lines - this_scroll_margin - 1);
5310 #endif
5312 arg = make_number (iarg);
5315 /* Skip past a partially visible first line. */
5316 if (w->vscroll)
5317 XSETINT (arg, XINT (arg) + 1);
5319 return Fvertical_motion (arg, window);
5324 /***********************************************************************
5325 Window Configuration
5326 ***********************************************************************/
5328 struct save_window_data
5330 struct vectorlike_header header;
5331 Lisp_Object selected_frame;
5332 Lisp_Object current_window;
5333 Lisp_Object current_buffer;
5334 Lisp_Object minibuf_scroll_window;
5335 Lisp_Object minibuf_selected_window;
5336 Lisp_Object root_window;
5337 Lisp_Object focus_frame;
5338 /* A vector, each of whose elements is a struct saved_window
5339 for one window. */
5340 Lisp_Object saved_windows;
5342 /* All fields above are traced by the GC.
5343 From `fame-cols' down, the fields are ignored by the GC. */
5345 int frame_cols, frame_lines, frame_menu_bar_lines;
5346 int frame_tool_bar_lines;
5349 /* This is saved as a Lisp_Vector */
5350 struct saved_window
5352 struct vectorlike_header header;
5354 Lisp_Object window, buffer, start, pointm, mark;
5355 Lisp_Object left_col, top_line, total_cols, total_lines;
5356 Lisp_Object normal_cols, normal_lines;
5357 Lisp_Object hscroll, min_hscroll;
5358 Lisp_Object parent, prev;
5359 Lisp_Object start_at_line_beg;
5360 Lisp_Object display_table;
5361 Lisp_Object left_margin_cols, right_margin_cols;
5362 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5363 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5364 Lisp_Object combination_limit, window_parameters;
5367 #define SAVED_WINDOW_N(swv,n) \
5368 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5370 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5371 doc: /* Return t if OBJECT is a window-configuration object. */)
5372 (Lisp_Object object)
5374 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5377 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5378 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5379 (Lisp_Object config)
5381 register struct save_window_data *data;
5382 struct Lisp_Vector *saved_windows;
5384 CHECK_WINDOW_CONFIGURATION (config);
5386 data = (struct save_window_data *) XVECTOR (config);
5387 saved_windows = XVECTOR (data->saved_windows);
5388 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5391 DEFUN ("set-window-configuration", Fset_window_configuration,
5392 Sset_window_configuration, 1, 1, 0,
5393 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5394 CONFIGURATION must be a value previously returned
5395 by `current-window-configuration' (which see).
5396 If CONFIGURATION was made from a frame that is now deleted,
5397 only frame-independent values can be restored. In this case,
5398 the return value is nil. Otherwise the value is t. */)
5399 (Lisp_Object configuration)
5401 register struct save_window_data *data;
5402 struct Lisp_Vector *saved_windows;
5403 Lisp_Object new_current_buffer;
5404 Lisp_Object frame;
5405 Lisp_Object auto_buffer_name;
5406 FRAME_PTR f;
5407 ptrdiff_t old_point = -1;
5409 CHECK_WINDOW_CONFIGURATION (configuration);
5411 data = (struct save_window_data *) XVECTOR (configuration);
5412 saved_windows = XVECTOR (data->saved_windows);
5414 new_current_buffer = data->current_buffer;
5415 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5416 new_current_buffer = Qnil;
5417 else
5419 if (XBUFFER (new_current_buffer) == current_buffer)
5420 /* The code further down "preserves point" by saving here PT in
5421 old_point and then setting it later back into PT. When the
5422 current-selected-window and the final-selected-window both show
5423 the current buffer, this suffers from the problem that the
5424 current PT is the window-point of the current-selected-window,
5425 while the final PT is the point of the final-selected-window, so
5426 this copy from one PT to the other would end up moving the
5427 window-point of the final-selected-window to the window-point of
5428 the current-selected-window. So we have to be careful which
5429 point of the current-buffer we copy into old_point. */
5430 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5431 && WINDOWP (selected_window)
5432 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5433 && !EQ (selected_window, data->current_window))
5434 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5435 else
5436 old_point = PT;
5437 else
5438 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5439 point in new_current_buffer as of the last time this buffer was
5440 used. This can be non-deterministic since it can be changed by
5441 things like jit-lock by mere temporary selection of some random
5442 window that happens to show this buffer.
5443 So if possible we want this arbitrary choice of "which point" to
5444 be the one from the to-be-selected-window so as to prevent this
5445 window's cursor from being copied from another window. */
5446 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5447 /* If current_window = selected_window, its point is in BUF_PT. */
5448 && !EQ (selected_window, data->current_window))
5449 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5450 else
5451 old_point = BUF_PT (XBUFFER (new_current_buffer));
5454 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5455 f = XFRAME (frame);
5457 /* If f is a dead frame, don't bother rebuilding its window tree.
5458 However, there is other stuff we should still try to do below. */
5459 if (FRAME_LIVE_P (f))
5461 Lisp_Object window;
5462 Lisp_Object dead_windows = Qnil;
5463 register Lisp_Object tem, par, pers;
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 = alloca (count_windows (root_window)
5525 * sizeof *leaf_windows);
5526 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5528 /* Kludge Alert!
5529 Mark all windows now on frame as "deleted".
5530 Restoring the new configuration "undeletes" any that are in it.
5532 Save their current buffers in their height fields, since we may
5533 need it later, if a buffer saved in the configuration is now
5534 dead. */
5535 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5537 for (k = 0; k < saved_windows->header.size; k++)
5539 p = SAVED_WINDOW_N (saved_windows, k);
5540 window = p->window;
5541 w = XWINDOW (window);
5542 WSET (w, next, Qnil);
5544 if (!NILP (p->parent))
5545 WSET (w, parent, SAVED_WINDOW_N
5546 (saved_windows, XFASTINT (p->parent))->window);
5547 else
5548 WSET (w, parent, Qnil);
5550 if (!NILP (p->prev))
5552 WSET (w, prev, SAVED_WINDOW_N
5553 (saved_windows, XFASTINT (p->prev))->window);
5554 WSET (XWINDOW (w->prev), next, p->window);
5556 else
5558 WSET (w, prev, Qnil);
5559 if (!NILP (w->parent))
5561 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5563 WSET (XWINDOW (w->parent), vchild, p->window);
5564 WSET (XWINDOW (w->parent), hchild, Qnil);
5566 else
5568 WSET (XWINDOW (w->parent), hchild, p->window);
5569 WSET (XWINDOW (w->parent), vchild, Qnil);
5574 /* If we squirreled away the buffer in the window's height,
5575 restore it now. */
5576 if (BUFFERP (w->total_lines))
5577 WSET (w, buffer, w->total_lines);
5578 WSET (w, left_col, p->left_col);
5579 WSET (w, top_line, p->top_line);
5580 WSET (w, total_cols, p->total_cols);
5581 WSET (w, total_lines, p->total_lines);
5582 WSET (w, normal_cols, p->normal_cols);
5583 WSET (w, normal_lines, p->normal_lines);
5584 w->hscroll = XFASTINT (p->hscroll);
5585 w->min_hscroll = XFASTINT (p->min_hscroll);
5586 WSET (w, display_table, p->display_table);
5587 WSET (w, left_margin_cols, p->left_margin_cols);
5588 WSET (w, right_margin_cols, p->right_margin_cols);
5589 WSET (w, left_fringe_width, p->left_fringe_width);
5590 WSET (w, right_fringe_width, p->right_fringe_width);
5591 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5592 WSET (w, scroll_bar_width, p->scroll_bar_width);
5593 WSET (w, vertical_scroll_bar_type, p->vertical_scroll_bar_type);
5594 WSET (w, dedicated, p->dedicated);
5595 WSET (w, combination_limit, p->combination_limit);
5596 /* Restore any window parameters that have been saved.
5597 Parameters that have not been saved are left alone. */
5598 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5600 pers = XCAR (tem);
5601 if (CONSP (pers))
5603 if (NILP (XCDR (pers)))
5605 par = Fassq (XCAR (pers), w->window_parameters);
5606 if (CONSP (par) && !NILP (XCDR (par)))
5607 /* Reset a parameter to nil if and only if it
5608 has a non-nil association. Don't make new
5609 associations. */
5610 Fsetcdr (par, Qnil);
5612 else
5613 /* Always restore a non-nil value. */
5614 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5618 w->last_modified = 0;
5619 w->last_overlay_modified = 0;
5621 /* Reinstall the saved buffer and pointers into it. */
5622 if (NILP (p->buffer))
5623 /* An internal window. */
5624 WSET (w, buffer, p->buffer);
5625 else if (!NILP (BVAR (XBUFFER (p->buffer), name)))
5626 /* If saved buffer is alive, install it. */
5628 WSET (w, buffer, p->buffer);
5629 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5630 set_marker_restricted (w->start, p->start, w->buffer);
5631 set_marker_restricted (w->pointm, p->pointm,
5632 w->buffer);
5633 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5634 p->mark, w->buffer);
5636 /* As documented in Fcurrent_window_configuration, don't
5637 restore the location of point in the buffer which was
5638 current when the window configuration was recorded. */
5639 if (!EQ (p->buffer, new_current_buffer)
5640 && XBUFFER (p->buffer) == current_buffer)
5641 Fgoto_char (w->pointm);
5643 else if (!NILP (w->buffer)
5644 && !NILP (BVAR (XBUFFER (w->buffer), name)))
5645 /* Keep window's old buffer; make sure the markers are
5646 real. */
5648 /* Set window markers at start of visible range. */
5649 if (XMARKER (w->start)->buffer == 0)
5650 set_marker_restricted (w->start, make_number (0),
5651 w->buffer);
5652 if (XMARKER (w->pointm)->buffer == 0)
5653 set_marker_restricted_both
5654 (w->pointm, w->buffer,
5655 BUF_PT (XBUFFER (w->buffer)),
5656 BUF_PT_BYTE (XBUFFER (w->buffer)));
5657 w->start_at_line_beg = 1;
5659 else if (STRINGP (auto_buffer_name =
5660 Fwindow_parameter (window, Qauto_buffer_name))
5661 && SCHARS (auto_buffer_name) != 0
5662 && !NILP (WSET (w, buffer, Fget_buffer_create (auto_buffer_name))))
5664 set_marker_restricted (w->start,
5665 make_number (0), w->buffer);
5666 set_marker_restricted (w->pointm,
5667 make_number (0), w->buffer);
5668 w->start_at_line_beg = 1;
5670 else
5671 /* Window has no live buffer, get one. */
5673 /* Get the buffer via other_buffer_safely in order to
5674 avoid showing an unimportant buffer and, if necessary, to
5675 recreate *scratch* in the course (part of Juanma's bs-show
5676 scenario from March 2011). */
5677 WSET (w, buffer, other_buffer_safely (Fcurrent_buffer ()));
5678 /* This will set the markers to beginning of visible
5679 range. */
5680 set_marker_restricted (w->start,
5681 make_number (0), w->buffer);
5682 set_marker_restricted (w->pointm,
5683 make_number (0), w->buffer);
5684 w->start_at_line_beg = 1;
5685 if (!NILP (w->dedicated))
5686 /* Record this window as dead. */
5687 dead_windows = Fcons (window, dead_windows);
5688 /* Make sure window is no more dedicated. */
5689 WSET (w, dedicated, Qnil);
5693 FSET (f, root_window, data->root_window);
5694 /* Arrange *not* to restore point in the buffer that was
5695 current when the window configuration was saved. */
5696 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5697 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5698 make_number (old_point),
5699 XWINDOW (data->current_window)->buffer);
5701 /* In the following call to `select-window', prevent "swapping out
5702 point" in the old selected window using the buffer that has
5703 been restored into it. We already swapped out that point from
5704 that window's old buffer. */
5705 select_window (data->current_window, Qnil, 1);
5706 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5707 = selected_window;
5709 if (NILP (data->focus_frame)
5710 || (FRAMEP (data->focus_frame)
5711 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5712 Fredirect_frame_focus (frame, data->focus_frame);
5714 /* Set the screen height to the value it had before this function. */
5715 if (previous_frame_lines != FRAME_LINES (f)
5716 || previous_frame_cols != FRAME_COLS (f))
5717 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5718 0, 0, 0);
5719 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5720 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5721 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5722 make_number (0));
5723 #ifdef HAVE_WINDOW_SYSTEM
5724 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5725 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5726 make_number (0));
5727 #endif
5728 #endif
5730 /* Now, free glyph matrices in windows that were not reused. */
5731 for (i = n = 0; i < n_leaf_windows; ++i)
5733 if (NILP (leaf_windows[i]->buffer))
5735 /* Assert it's not reused as a combination. */
5736 eassert (NILP (leaf_windows[i]->hchild)
5737 && NILP (leaf_windows[i]->vchild));
5738 free_window_matrices (leaf_windows[i]);
5740 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5741 ++n;
5744 adjust_glyphs (f);
5745 UNBLOCK_INPUT;
5747 /* Scan dead buffer windows. */
5748 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5750 window = XCAR (dead_windows);
5751 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5752 delete_deletable_window (window);
5755 /* Fselect_window will have made f the selected frame, so we
5756 reselect the proper frame here. Fhandle_switch_frame will change the
5757 selected window too, but that doesn't make the call to
5758 Fselect_window above totally superfluous; it still sets f's
5759 selected window. */
5760 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5761 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5763 run_window_configuration_change_hook (f);
5766 if (!NILP (new_current_buffer))
5767 Fset_buffer (new_current_buffer);
5769 Vminibuf_scroll_window = data->minibuf_scroll_window;
5770 minibuf_selected_window = data->minibuf_selected_window;
5772 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5776 /* Recursively delete all child windows reachable via the next, vchild,
5777 and hchild slots of WINDOW. */
5778 void
5779 delete_all_child_windows (Lisp_Object window)
5781 register struct window *w;
5783 w = XWINDOW (window);
5785 if (!NILP (w->next))
5786 /* Delete WINDOW's siblings (we traverse postorderly). */
5787 delete_all_child_windows (w->next);
5789 WSET (w, total_lines, w->buffer); /* See Fset_window_configuration for excuse. */
5791 if (!NILP (w->vchild))
5793 delete_all_child_windows (w->vchild);
5794 WSET (w, vchild, Qnil);
5796 else if (!NILP (w->hchild))
5798 delete_all_child_windows (w->hchild);
5799 WSET (w, hchild, Qnil);
5801 else if (!NILP (w->buffer))
5803 unshow_buffer (w);
5804 unchain_marker (XMARKER (w->pointm));
5805 unchain_marker (XMARKER (w->start));
5806 WSET (w, buffer, Qnil);
5809 Vwindow_list = Qnil;
5812 static int
5813 count_windows (register struct window *window)
5815 register int count = 1;
5816 if (!NILP (window->next))
5817 count += count_windows (XWINDOW (window->next));
5818 if (!NILP (window->vchild))
5819 count += count_windows (XWINDOW (window->vchild));
5820 if (!NILP (window->hchild))
5821 count += count_windows (XWINDOW (window->hchild));
5822 return count;
5826 /* Fill vector FLAT with leaf windows under W, starting at index I.
5827 Value is last index + 1. */
5828 static int
5829 get_leaf_windows (struct window *w, struct window **flat, int i)
5831 while (w)
5833 if (!NILP (w->hchild))
5834 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5835 else if (!NILP (w->vchild))
5836 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5837 else
5838 flat[i++] = w;
5840 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5843 return i;
5847 /* Return a pointer to the glyph W's physical cursor is on. Value is
5848 null if W's current matrix is invalid, so that no meaningful glyph
5849 can be returned. */
5850 struct glyph *
5851 get_phys_cursor_glyph (struct window *w)
5853 struct glyph_row *row;
5854 struct glyph *glyph;
5855 int hpos = w->phys_cursor.hpos;
5857 if (!(w->phys_cursor.vpos >= 0
5858 && w->phys_cursor.vpos < w->current_matrix->nrows))
5859 return NULL;
5861 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5862 if (!row->enabled_p)
5863 return NULL;
5865 if (w->hscroll)
5867 /* When the window is hscrolled, cursor hpos can legitimately be
5868 out of bounds, but we draw the cursor at the corresponding
5869 window margin in that case. */
5870 if (!row->reversed_p && hpos < 0)
5871 hpos = 0;
5872 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5873 hpos = row->used[TEXT_AREA] - 1;
5876 if (row->used[TEXT_AREA] > hpos
5877 && 0 <= hpos)
5878 glyph = row->glyphs[TEXT_AREA] + hpos;
5879 else
5880 glyph = NULL;
5882 return glyph;
5886 static int
5887 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5889 register struct saved_window *p;
5890 register struct window *w;
5891 register Lisp_Object tem, pers, par;
5893 for (;!NILP (window); window = w->next)
5895 p = SAVED_WINDOW_N (vector, i);
5896 w = XWINDOW (window);
5898 WSET (w, temslot, make_number (i)); i++;
5899 p->window = window;
5900 p->buffer = w->buffer;
5901 p->left_col = w->left_col;
5902 p->top_line = w->top_line;
5903 p->total_cols = w->total_cols;
5904 p->total_lines = w->total_lines;
5905 p->normal_cols = w->normal_cols;
5906 p->normal_lines = w->normal_lines;
5907 XSETFASTINT (p->hscroll, w->hscroll);
5908 XSETFASTINT (p->min_hscroll, w->min_hscroll);
5909 p->display_table = w->display_table;
5910 p->left_margin_cols = w->left_margin_cols;
5911 p->right_margin_cols = w->right_margin_cols;
5912 p->left_fringe_width = w->left_fringe_width;
5913 p->right_fringe_width = w->right_fringe_width;
5914 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
5915 p->scroll_bar_width = w->scroll_bar_width;
5916 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5917 p->dedicated = w->dedicated;
5918 p->combination_limit = w->combination_limit;
5919 p->window_parameters = Qnil;
5921 if (!NILP (Vwindow_persistent_parameters))
5923 /* Run cycle detection on Vwindow_persistent_parameters. */
5924 Lisp_Object tortoise, hare;
5926 hare = tortoise = Vwindow_persistent_parameters;
5927 while (CONSP (hare))
5929 hare = XCDR (hare);
5930 if (!CONSP (hare))
5931 break;
5933 hare = XCDR (hare);
5934 tortoise = XCDR (tortoise);
5936 if (EQ (hare, tortoise))
5937 /* Reset Vwindow_persistent_parameters to Qnil. */
5939 Vwindow_persistent_parameters = Qnil;
5940 break;
5944 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5945 tem = XCDR (tem))
5947 pers = XCAR (tem);
5948 /* Save values for persistent window parameters. */
5949 if (CONSP (pers) && !NILP (XCDR (pers)))
5951 par = Fassq (XCAR (pers), w->window_parameters);
5952 if (NILP (par))
5953 /* If the window has no value for the parameter,
5954 make one. */
5955 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
5956 p->window_parameters);
5957 else
5958 /* If the window has a value for the parameter,
5959 save it. */
5960 p->window_parameters = Fcons (Fcons (XCAR (par),
5961 XCDR (par)),
5962 p->window_parameters);
5967 if (!NILP (w->buffer))
5969 /* Save w's value of point in the window configuration. If w
5970 is the selected window, then get the value of point from
5971 the buffer; pointm is garbage in the selected window. */
5972 if (EQ (window, selected_window))
5973 p->pointm = build_marker (XBUFFER (w->buffer),
5974 BUF_PT (XBUFFER (w->buffer)),
5975 BUF_PT_BYTE (XBUFFER (w->buffer)));
5976 else
5977 p->pointm = Fcopy_marker (w->pointm, Qnil);
5978 XMARKER (p->pointm)->insertion_type
5979 = !NILP (Vwindow_point_insertion_type);
5981 p->start = Fcopy_marker (w->start, Qnil);
5982 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
5984 tem = BVAR (XBUFFER (w->buffer), mark);
5985 p->mark = Fcopy_marker (tem, Qnil);
5987 else
5989 p->pointm = Qnil;
5990 p->start = Qnil;
5991 p->mark = Qnil;
5992 p->start_at_line_beg = Qnil;
5995 if (NILP (w->parent))
5996 p->parent = Qnil;
5997 else
5998 p->parent = XWINDOW (w->parent)->temslot;
6000 if (NILP (w->prev))
6001 p->prev = Qnil;
6002 else
6003 p->prev = XWINDOW (w->prev)->temslot;
6005 if (!NILP (w->vchild))
6006 i = save_window_save (w->vchild, vector, i);
6007 if (!NILP (w->hchild))
6008 i = save_window_save (w->hchild, vector, i);
6011 return i;
6014 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6015 Scurrent_window_configuration, 0, 1, 0,
6016 doc: /* Return an object representing the current window configuration of FRAME.
6017 If FRAME is nil or omitted, use the selected frame.
6018 This describes the number of windows, their sizes and current buffers,
6019 and for each displayed buffer, where display starts, and the positions of
6020 point and mark. An exception is made for point in the current buffer:
6021 its value is -not- saved.
6022 This also records the currently selected frame, and FRAME's focus
6023 redirection (see `redirect-frame-focus'). The variable
6024 `window-persistent-parameters' specifies which window parameters are
6025 saved by this function. */)
6026 (Lisp_Object frame)
6028 register Lisp_Object tem;
6029 register int n_windows;
6030 register struct save_window_data *data;
6031 register int i;
6032 FRAME_PTR f;
6034 if (NILP (frame))
6035 frame = selected_frame;
6036 CHECK_LIVE_FRAME (frame);
6037 f = XFRAME (frame);
6039 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6040 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6041 PVEC_WINDOW_CONFIGURATION);
6043 data->frame_cols = FRAME_COLS (f);
6044 data->frame_lines = FRAME_LINES (f);
6045 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6046 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6047 data->selected_frame = selected_frame;
6048 data->current_window = FRAME_SELECTED_WINDOW (f);
6049 XSETBUFFER (data->current_buffer, current_buffer);
6050 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6051 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6052 data->root_window = FRAME_ROOT_WINDOW (f);
6053 data->focus_frame = FRAME_FOCUS_FRAME (f);
6054 tem = Fmake_vector (make_number (n_windows), Qnil);
6055 data->saved_windows = tem;
6056 for (i = 0; i < n_windows; i++)
6057 ASET (tem, i,
6058 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6059 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6060 XSETWINDOW_CONFIGURATION (tem, data);
6061 return (tem);
6064 /***********************************************************************
6065 Marginal Areas
6066 ***********************************************************************/
6068 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6069 2, 3, 0,
6070 doc: /* Set width of marginal areas of window WINDOW.
6071 WINDOW must be a live window and defaults to the selected one.
6073 Second arg LEFT-WIDTH specifies the number of character cells to
6074 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6075 does the same for the right marginal area. A nil width parameter
6076 means no margin. */)
6077 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6079 struct window *w = decode_live_window (window);
6081 /* Translate negative or zero widths to nil.
6082 Margins that are too wide have to be checked elsewhere. */
6084 if (!NILP (left_width))
6086 CHECK_NUMBER (left_width);
6087 if (XINT (left_width) <= 0)
6088 left_width = Qnil;
6091 if (!NILP (right_width))
6093 CHECK_NUMBER (right_width);
6094 if (XINT (right_width) <= 0)
6095 right_width = Qnil;
6098 if (!EQ (w->left_margin_cols, left_width)
6099 || !EQ (w->right_margin_cols, right_width))
6101 WSET (w, left_margin_cols, left_width);
6102 WSET (w, right_margin_cols, right_width);
6104 adjust_window_margins (w);
6106 ++windows_or_buffers_changed;
6107 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6110 return Qnil;
6114 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6115 0, 1, 0,
6116 doc: /* Get width of marginal areas of window WINDOW.
6117 WINDOW must be a live window and defaults to the selected one.
6119 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6120 If a marginal area does not exist, its width will be returned
6121 as nil. */)
6122 (Lisp_Object window)
6124 struct window *w = decode_live_window (window);
6125 return Fcons (w->left_margin_cols, w->right_margin_cols);
6130 /***********************************************************************
6131 Fringes
6132 ***********************************************************************/
6134 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6135 2, 4, 0,
6136 doc: /* Set the fringe widths of window WINDOW.
6137 WINDOW must be a live window and defaults to the selected one.
6139 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6140 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6141 fringe width. If a fringe width arg is nil, that means to use the
6142 frame's default fringe width. Default fringe widths can be set with
6143 the command `set-fringe-style'.
6144 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6145 outside of the display margins. By default, fringes are drawn between
6146 display marginal areas and the text area. */)
6147 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6149 struct window *w = decode_live_window (window);
6150 int outside = !NILP (outside_margins);
6152 if (!NILP (left_width))
6153 CHECK_NATNUM (left_width);
6154 if (!NILP (right_width))
6155 CHECK_NATNUM (right_width);
6157 /* Do nothing on a tty. */
6158 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6159 && (!EQ (w->left_fringe_width, left_width)
6160 || !EQ (w->right_fringe_width, right_width)
6161 || w->fringes_outside_margins != outside))
6163 WSET (w, left_fringe_width, left_width);
6164 WSET (w, right_fringe_width, right_width);
6165 w->fringes_outside_margins = outside;
6167 adjust_window_margins (w);
6169 clear_glyph_matrix (w->current_matrix);
6170 WSET (w, window_end_valid, Qnil);
6172 ++windows_or_buffers_changed;
6173 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6176 return Qnil;
6180 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6181 0, 1, 0,
6182 doc: /* Get width of fringes of window WINDOW.
6183 WINDOW must be a live window and defaults to the selected one.
6185 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6186 (Lisp_Object window)
6188 struct window *w = decode_live_window (window);
6190 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6191 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6192 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6193 ? Qt : Qnil), Qnil)));
6198 /***********************************************************************
6199 Scroll bars
6200 ***********************************************************************/
6202 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6203 Sset_window_scroll_bars, 2, 4, 0,
6204 doc: /* Set width and type of scroll bars of window WINDOW.
6205 WINDOW must be a live window and defaults to the selected one.
6207 Second parameter WIDTH specifies the pixel width for the scroll bar;
6208 this is automatically adjusted to a multiple of the frame column width.
6209 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6210 bar: left, right, or nil.
6211 If WIDTH is nil, use the frame's scroll-bar width.
6212 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6213 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6214 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6216 struct window *w = decode_live_window (window);
6218 if (!NILP (width))
6220 CHECK_RANGED_INTEGER (width, 0, INT_MAX);
6222 if (XINT (width) == 0)
6223 vertical_type = Qnil;
6226 if (!(NILP (vertical_type)
6227 || EQ (vertical_type, Qleft)
6228 || EQ (vertical_type, Qright)
6229 || EQ (vertical_type, Qt)))
6230 error ("Invalid type of vertical scroll bar");
6232 if (!EQ (w->scroll_bar_width, width)
6233 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6235 WSET (w, scroll_bar_width, width);
6236 WSET (w, vertical_scroll_bar_type, vertical_type);
6238 adjust_window_margins (w);
6240 clear_glyph_matrix (w->current_matrix);
6241 WSET (w, window_end_valid, Qnil);
6243 ++windows_or_buffers_changed;
6244 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6247 return Qnil;
6251 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6252 0, 1, 0,
6253 doc: /* Get width and type of scroll bars of window WINDOW.
6254 WINDOW must be a live window and defaults to the selected one.
6256 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6257 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6258 value. */)
6259 (Lisp_Object window)
6261 struct window *w = decode_live_window (window);
6262 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6263 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6264 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6265 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6266 Fcons (w->vertical_scroll_bar_type,
6267 Fcons (Qnil, Qnil))));
6272 /***********************************************************************
6273 Smooth scrolling
6274 ***********************************************************************/
6276 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6277 doc: /* Return the amount by which WINDOW is scrolled vertically.
6278 If WINDOW is omitted or nil, it defaults to the selected window.
6279 Normally, value is a multiple of the canonical character height of WINDOW;
6280 optional second arg PIXELS-P means value is measured in pixels. */)
6281 (Lisp_Object window, Lisp_Object pixels_p)
6283 Lisp_Object result;
6284 struct frame *f;
6285 struct window *w;
6287 if (NILP (window))
6288 window = selected_window;
6289 else
6290 CHECK_WINDOW (window);
6291 w = XWINDOW (window);
6292 f = XFRAME (w->frame);
6294 if (FRAME_WINDOW_P (f))
6295 result = (NILP (pixels_p)
6296 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6297 : make_number (-w->vscroll));
6298 else
6299 result = make_number (0);
6300 return result;
6304 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6305 2, 3, 0,
6306 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6307 WINDOW nil means use the selected window. Normally, VSCROLL is a
6308 non-negative multiple of the canonical character height of WINDOW;
6309 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6310 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6311 corresponds to an integral number of pixels. The return value is the
6312 result of this rounding.
6313 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6314 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6316 struct window *w;
6317 struct frame *f;
6319 if (NILP (window))
6320 window = selected_window;
6321 else
6322 CHECK_WINDOW (window);
6323 CHECK_NUMBER_OR_FLOAT (vscroll);
6325 w = XWINDOW (window);
6326 f = XFRAME (w->frame);
6328 if (FRAME_WINDOW_P (f))
6330 int old_dy = w->vscroll;
6332 w->vscroll = - (NILP (pixels_p)
6333 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6334 : XFLOATINT (vscroll));
6335 w->vscroll = min (w->vscroll, 0);
6337 if (w->vscroll != old_dy)
6339 /* Adjust glyph matrix of the frame if the virtual display
6340 area becomes larger than before. */
6341 if (w->vscroll < 0 && w->vscroll < old_dy)
6342 adjust_glyphs (f);
6344 /* Prevent redisplay shortcuts. */
6345 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6349 return Fwindow_vscroll (window, pixels_p);
6353 /* Call FN for all leaf windows on frame F. FN is called with the
6354 first argument being a pointer to the leaf window, and with
6355 additional argument USER_DATA. Stops when FN returns 0. */
6357 static void
6358 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6359 void *user_data)
6361 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6362 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6363 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6367 /* Helper function for foreach_window. Call FN for all leaf windows
6368 reachable from W. FN is called with the first argument being a
6369 pointer to the leaf window, and with additional argument USER_DATA.
6370 Stop when FN returns 0. Value is 0 if stopped by FN. */
6372 static int
6373 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6375 int cont;
6377 for (cont = 1; w && cont;)
6379 if (!NILP (w->hchild))
6380 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6381 else if (!NILP (w->vchild))
6382 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6383 else
6384 cont = fn (w, user_data);
6386 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6389 return cont;
6393 /* Freeze or unfreeze the window start of W unless it is a
6394 mini-window or the selected window. FREEZE_P non-null means freeze
6395 the window start. */
6397 static int
6398 freeze_window_start (struct window *w, void *freeze_p)
6400 if (MINI_WINDOW_P (w)
6401 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6402 && (w == XWINDOW (selected_window)
6403 || (MINI_WINDOW_P (XWINDOW (selected_window))
6404 && ! NILP (Vminibuf_scroll_window)
6405 && w == XWINDOW (Vminibuf_scroll_window)))))
6406 freeze_p = NULL;
6408 w->frozen_window_start_p = freeze_p != NULL;
6409 return 1;
6413 /* Freeze or unfreeze the window starts of all leaf windows on frame
6414 F, except the selected window and a mini-window. FREEZE_P non-zero
6415 means freeze the window start. */
6417 void
6418 freeze_window_starts (struct frame *f, int freeze_p)
6420 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6424 /***********************************************************************
6425 Initialization
6426 ***********************************************************************/
6428 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6429 describe the same state of affairs. This is used by Fequal.
6431 ignore_positions non-zero means ignore non-matching scroll positions
6432 and the like.
6434 This ignores a couple of things like the dedication status of
6435 window, combination_limit and the like. This might have to be
6436 fixed. */
6439 compare_window_configurations (Lisp_Object configuration1, Lisp_Object configuration2, int ignore_positions)
6441 register struct save_window_data *d1, *d2;
6442 struct Lisp_Vector *sws1, *sws2;
6443 ptrdiff_t i;
6445 CHECK_WINDOW_CONFIGURATION (configuration1);
6446 CHECK_WINDOW_CONFIGURATION (configuration2);
6448 d1 = (struct save_window_data *) XVECTOR (configuration1);
6449 d2 = (struct save_window_data *) XVECTOR (configuration2);
6450 sws1 = XVECTOR (d1->saved_windows);
6451 sws2 = XVECTOR (d2->saved_windows);
6453 /* Frame settings must match. */
6454 if (d1->frame_cols != d2->frame_cols
6455 || d1->frame_lines != d2->frame_lines
6456 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6457 || !EQ (d1->selected_frame, d2->selected_frame)
6458 || !EQ (d1->current_buffer, d2->current_buffer)
6459 || (!ignore_positions
6460 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6461 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6462 || !EQ (d1->focus_frame, d2->focus_frame)
6463 /* Verify that the two configurations have the same number of windows. */
6464 || sws1->header.size != sws2->header.size)
6465 return 0;
6467 for (i = 0; i < sws1->header.size; i++)
6469 struct saved_window *sw1, *sw2;
6471 sw1 = SAVED_WINDOW_N (sws1, i);
6472 sw2 = SAVED_WINDOW_N (sws2, i);
6474 if (
6475 /* The "current" windows in the two configurations must
6476 correspond to each other. */
6477 EQ (d1->current_window, sw1->window)
6478 != EQ (d2->current_window, sw2->window)
6479 /* Windows' buffers must match. */
6480 || !EQ (sw1->buffer, sw2->buffer)
6481 || !EQ (sw1->left_col, sw2->left_col)
6482 || !EQ (sw1->top_line, sw2->top_line)
6483 || !EQ (sw1->total_cols, sw2->total_cols)
6484 || !EQ (sw1->total_lines, sw2->total_lines)
6485 || !EQ (sw1->display_table, sw2->display_table)
6486 /* The next two disjuncts check the window structure for
6487 equality. */
6488 || !EQ (sw1->parent, sw2->parent)
6489 || !EQ (sw1->prev, sw2->prev)
6490 || (!ignore_positions
6491 && (!EQ (sw1->hscroll, sw2->hscroll)
6492 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6493 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6494 || NILP (Fequal (sw1->start, sw2->start))
6495 || NILP (Fequal (sw1->pointm, sw2->pointm))
6496 || NILP (Fequal (sw1->mark, sw2->mark))))
6497 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6498 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6499 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6500 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6501 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6502 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6503 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6504 return 0;
6507 return 1;
6510 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6511 Scompare_window_configurations, 2, 2, 0,
6512 doc: /* Compare two window configurations as regards the structure of windows.
6513 This function ignores details such as the values of point and mark
6514 and scrolling positions. */)
6515 (Lisp_Object x, Lisp_Object y)
6517 if (compare_window_configurations (x, y, 1))
6518 return Qt;
6519 return Qnil;
6522 void
6523 init_window_once (void)
6525 struct frame *f = make_initial_frame ();
6526 XSETFRAME (selected_frame, f);
6527 Vterminal_frame = selected_frame;
6528 minibuf_window = f->minibuffer_window;
6529 selected_window = f->selected_window;
6530 last_nonminibuf_frame = f;
6532 window_initialized = 1;
6535 void
6536 init_window (void)
6538 Vwindow_list = Qnil;
6541 void
6542 syms_of_window (void)
6544 DEFSYM (Qscroll_up, "scroll-up");
6545 DEFSYM (Qscroll_down, "scroll-down");
6546 DEFSYM (Qscroll_command, "scroll-command");
6548 Fput (Qscroll_up, Qscroll_command, Qt);
6549 Fput (Qscroll_down, Qscroll_command, Qt);
6551 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6552 DEFSYM (Qwindowp, "windowp");
6553 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6554 DEFSYM (Qwindow_live_p, "window-live-p");
6555 DEFSYM (Qwindow_valid_p, "window-valid-p");
6556 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6557 DEFSYM (Qdelete_window, "delete-window");
6558 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6559 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6560 DEFSYM (Qsafe, "safe");
6561 DEFSYM (Qdisplay_buffer, "display-buffer");
6562 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6563 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6564 DEFSYM (Qget_mru_window, "get-mru-window");
6565 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6566 DEFSYM (Qabove, "above");
6567 DEFSYM (Qbelow, "below");
6568 DEFSYM (Qauto_buffer_name, "auto-buffer-name");
6569 DEFSYM (Qclone_of, "clone-of");
6571 staticpro (&Vwindow_list);
6573 minibuf_selected_window = Qnil;
6574 staticpro (&minibuf_selected_window);
6576 window_scroll_pixel_based_preserve_x = -1;
6577 window_scroll_pixel_based_preserve_y = -1;
6578 window_scroll_preserve_hpos = -1;
6579 window_scroll_preserve_vpos = -1;
6581 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6582 doc: /* Non-nil means call as function to display a help buffer.
6583 The function is called with one argument, the buffer to be displayed.
6584 Used by `with-output-to-temp-buffer'.
6585 If this function is used, then it must do the entire job of showing
6586 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6587 Vtemp_buffer_show_function = Qnil;
6589 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6590 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6591 Vminibuf_scroll_window = Qnil;
6593 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6594 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6595 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6596 is displayed in the `mode-line' face. */);
6597 mode_line_in_non_selected_windows = 1;
6599 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6600 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6601 Vother_window_scroll_buffer = Qnil;
6603 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6604 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6605 auto_window_vscroll_p = 1;
6607 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6608 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6609 next_screen_context_lines = 2;
6611 DEFVAR_LISP ("scroll-preserve-screen-position",
6612 Vscroll_preserve_screen_position,
6613 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6614 A value of nil means point does not keep its screen position except
6615 at the scroll margin or window boundary respectively.
6616 A value of t means point keeps its screen position if the scroll
6617 command moved it vertically out of the window, e.g. when scrolling
6618 by full screens.
6619 Any other value means point always keeps its screen position.
6620 Scroll commands should have the `scroll-command' property
6621 on their symbols to be controlled by this variable. */);
6622 Vscroll_preserve_screen_position = Qnil;
6624 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6625 doc: /* Type of marker to use for `window-point'. */);
6626 Vwindow_point_insertion_type = Qnil;
6628 DEFVAR_LISP ("window-configuration-change-hook",
6629 Vwindow_configuration_change_hook,
6630 doc: /* Functions to call when window configuration changes.
6631 The buffer-local part is run once per window, with the relevant window
6632 selected; while the global part is run only once for the modified frame,
6633 with the relevant frame selected. */);
6634 Vwindow_configuration_change_hook = Qnil;
6636 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6637 doc: /* Non-nil means `recenter' redraws entire frame.
6638 If this option is non-nil, then the `recenter' command with a nil
6639 argument will redraw the entire frame; the special value `tty' causes
6640 the frame to be redrawn only if it is a tty frame. */);
6641 Vrecenter_redisplay = Qtty;
6643 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6644 doc: /* If t, resize window combinations proportionally.
6645 If this variable is nil, splitting a window gets the entire screen space
6646 for displaying the new window from the window to split. Deleting and
6647 resizing a window preferably resizes one adjacent window only.
6649 If this variable is t, splitting a window tries to get the space
6650 proportionally from all windows in the same combination. This also
6651 allows to split a window that is otherwise too small or of fixed size.
6652 Resizing and deleting a window proportionally resize all windows in the
6653 same combination.
6655 Other values are reserved for future use.
6657 This variable takes no effect if `window-combination-limit' is non-nil. */);
6658 Vwindow_combination_resize = Qnil;
6660 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6661 doc: /* If t, splitting a window makes a new parent window.
6662 If this variable is nil, splitting a window will create a new parent
6663 window only if the window has no parent window or the window shall
6664 become a combination orthogonal to the one it is part of.
6666 If this variable is t, splitting a window always creates a new parent
6667 window. If all splits behave this way, each frame's window tree is a
6668 binary tree and every window but the frame's root window has exactly one
6669 sibling.
6671 Other values are reserved for future use.
6673 The value of this variable is also assigned to the combination limit of
6674 the new parent window. The combination limit of a window can be
6675 retrieved via the function `window-combination-limit' and altered by the
6676 function `set-window-combination-limit'. */);
6677 Vwindow_combination_limit = Qnil;
6679 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6680 doc: /* Alist of persistent window parameters.
6681 This alist specifies which window parameters shall get saved by
6682 `current-window-configuration' and `window-state-get' and subsequently
6683 restored to their previous values by `set-window-configuration' and
6684 `window-state-put'.
6686 The car of each entry of this alist is the symbol specifying the
6687 parameter. The cdr is one of the following:
6689 nil means the parameter is neither saved by `window-state-get' nor by
6690 `current-window-configuration'.
6692 t means the parameter is saved by `current-window-configuration' and,
6693 provided its WRITABLE argument is nil, by `window-state-get'.
6695 The symbol `writable' means the parameter is saved unconditionally by
6696 both `current-window-configuration' and `window-state-get'. Do not use
6697 this value for parameters without read syntax (like windows or frames).
6699 Parameters not saved by `current-window-configuration' or
6700 `window-state-get' are left alone by `set-window-configuration'
6701 respectively are not installed by `window-state-put'. */);
6702 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6704 defsubr (&Sselected_window);
6705 defsubr (&Sminibuffer_window);
6706 defsubr (&Swindow_minibuffer_p);
6707 defsubr (&Swindowp);
6708 defsubr (&Swindow_valid_p);
6709 defsubr (&Swindow_live_p);
6710 defsubr (&Swindow_frame);
6711 defsubr (&Sframe_root_window);
6712 defsubr (&Sframe_first_window);
6713 defsubr (&Sframe_selected_window);
6714 defsubr (&Sset_frame_selected_window);
6715 defsubr (&Spos_visible_in_window_p);
6716 defsubr (&Swindow_line_height);
6717 defsubr (&Swindow_buffer);
6718 defsubr (&Swindow_parent);
6719 defsubr (&Swindow_top_child);
6720 defsubr (&Swindow_left_child);
6721 defsubr (&Swindow_next_sibling);
6722 defsubr (&Swindow_prev_sibling);
6723 defsubr (&Swindow_combination_limit);
6724 defsubr (&Sset_window_combination_limit);
6725 defsubr (&Swindow_use_time);
6726 defsubr (&Swindow_top_line);
6727 defsubr (&Swindow_left_column);
6728 defsubr (&Swindow_total_height);
6729 defsubr (&Swindow_total_width);
6730 defsubr (&Swindow_normal_size);
6731 defsubr (&Swindow_new_total);
6732 defsubr (&Swindow_new_normal);
6733 defsubr (&Sset_window_new_total);
6734 defsubr (&Sset_window_new_normal);
6735 defsubr (&Swindow_resize_apply);
6736 defsubr (&Swindow_body_height);
6737 defsubr (&Swindow_body_width);
6738 defsubr (&Swindow_hscroll);
6739 defsubr (&Sset_window_hscroll);
6740 defsubr (&Swindow_redisplay_end_trigger);
6741 defsubr (&Sset_window_redisplay_end_trigger);
6742 defsubr (&Swindow_edges);
6743 defsubr (&Swindow_pixel_edges);
6744 defsubr (&Swindow_absolute_pixel_edges);
6745 defsubr (&Swindow_inside_edges);
6746 defsubr (&Swindow_inside_pixel_edges);
6747 defsubr (&Swindow_inside_absolute_pixel_edges);
6748 defsubr (&Scoordinates_in_window_p);
6749 defsubr (&Swindow_at);
6750 defsubr (&Swindow_point);
6751 defsubr (&Swindow_start);
6752 defsubr (&Swindow_end);
6753 defsubr (&Sset_window_point);
6754 defsubr (&Sset_window_start);
6755 defsubr (&Swindow_dedicated_p);
6756 defsubr (&Sset_window_dedicated_p);
6757 defsubr (&Swindow_display_table);
6758 defsubr (&Sset_window_display_table);
6759 defsubr (&Snext_window);
6760 defsubr (&Sprevious_window);
6761 defsubr (&Sget_buffer_window);
6762 defsubr (&Sdelete_other_windows_internal);
6763 defsubr (&Sdelete_window_internal);
6764 defsubr (&Sresize_mini_window_internal);
6765 defsubr (&Sset_window_buffer);
6766 defsubr (&Srun_window_configuration_change_hook);
6767 defsubr (&Sselect_window);
6768 defsubr (&Sforce_window_update);
6769 defsubr (&Stemp_output_buffer_show);
6770 defsubr (&Ssplit_window_internal);
6771 defsubr (&Sscroll_up);
6772 defsubr (&Sscroll_down);
6773 defsubr (&Sscroll_left);
6774 defsubr (&Sscroll_right);
6775 defsubr (&Sother_window_for_scrolling);
6776 defsubr (&Sscroll_other_window);
6777 defsubr (&Sminibuffer_selected_window);
6778 defsubr (&Srecenter);
6779 defsubr (&Swindow_text_height);
6780 defsubr (&Smove_to_window_line);
6781 defsubr (&Swindow_configuration_p);
6782 defsubr (&Swindow_configuration_frame);
6783 defsubr (&Sset_window_configuration);
6784 defsubr (&Scurrent_window_configuration);
6785 defsubr (&Sset_window_margins);
6786 defsubr (&Swindow_margins);
6787 defsubr (&Sset_window_fringes);
6788 defsubr (&Swindow_fringes);
6789 defsubr (&Sset_window_scroll_bars);
6790 defsubr (&Swindow_scroll_bars);
6791 defsubr (&Swindow_vscroll);
6792 defsubr (&Sset_window_vscroll);
6793 defsubr (&Scompare_window_configurations);
6794 defsubr (&Swindow_list);
6795 defsubr (&Swindow_list_1);
6796 defsubr (&Swindow_prev_buffers);
6797 defsubr (&Sset_window_prev_buffers);
6798 defsubr (&Swindow_next_buffers);
6799 defsubr (&Sset_window_next_buffers);
6800 defsubr (&Swindow_parameters);
6801 defsubr (&Swindow_parameter);
6802 defsubr (&Sset_window_parameter);
6805 void
6806 keys_of_window (void)
6808 initial_define_key (control_x_map, '<', "scroll-left");
6809 initial_define_key (control_x_map, '>', "scroll-right");
6811 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6812 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6813 initial_define_key (meta_map, 'v', "scroll-down-command");