Work around crash when displaying etc/HELLO on OS X 10.11
[emacs.git] / src / window.c
blob8d899d6d350daa71d4c6b4cab312658407c4c5fd
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2015 Free Software
4 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>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "menu.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "indent.h"
35 #include "termchar.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "blockinput.h"
39 #include "intervals.h"
40 #include "termhooks.h" /* For FRAME_TERMINAL. */
41 #ifdef HAVE_WINDOW_SYSTEM
42 #include TERM_HEADER
43 #endif /* HAVE_WINDOW_SYSTEM */
44 #ifdef MSDOS
45 #include "msdos.h"
46 #endif
48 static ptrdiff_t count_windows (struct window *);
49 static ptrdiff_t get_leaf_windows (struct window *, struct window **,
50 ptrdiff_t);
51 static void window_scroll_pixel_based (Lisp_Object, int, bool, bool);
52 static void window_scroll_line_based (Lisp_Object, int, bool, bool);
53 static void foreach_window (struct frame *,
54 bool (* fn) (struct window *, void *),
55 void *);
56 static bool foreach_window_1 (struct window *,
57 bool (* fn) (struct window *, void *),
58 void *);
59 static bool window_resize_check (struct window *, bool);
60 static void window_resize_apply (struct window *, bool);
61 static void select_window_1 (Lisp_Object, bool);
63 static struct window *set_window_fringes (struct window *, Lisp_Object,
64 Lisp_Object, Lisp_Object);
65 static struct window *set_window_margins (struct window *, Lisp_Object,
66 Lisp_Object);
67 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
68 Lisp_Object, Lisp_Object,
69 Lisp_Object);
70 static void apply_window_adjustment (struct window *);
72 /* This is the window in which the terminal's cursor should
73 be left when nothing is being done with it. This must
74 always be a leaf window, and its buffer is selected by
75 the top level editing loop at the end of each command.
77 This value is always the same as
78 FRAME_SELECTED_WINDOW (selected_frame). */
79 Lisp_Object selected_window;
81 /* A list of all windows for use by next_window and Fwindow_list.
82 Functions creating or deleting windows should invalidate this cache
83 by setting it to nil. */
84 Lisp_Object Vwindow_list;
86 /* The mini-buffer window of the selected frame.
87 Note that you cannot test for mini-bufferness of an arbitrary window
88 by comparing against this; but you can test for mini-bufferness of
89 the selected window. */
90 Lisp_Object minibuf_window;
92 /* Non-nil means it is the window whose mode line should be
93 shown as the selected window when the minibuffer is selected. */
94 Lisp_Object minibuf_selected_window;
96 /* Incremented for each window created. */
97 static EMACS_INT sequence_number;
99 /* Used by the function window_scroll_pixel_based. */
100 static int window_scroll_pixel_based_preserve_x;
101 static int window_scroll_pixel_based_preserve_y;
103 /* Same for window_scroll_line_based. */
104 static EMACS_INT window_scroll_preserve_hpos;
105 static EMACS_INT window_scroll_preserve_vpos;
107 static void
108 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
110 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
113 /* These setters are used only in this file, so they can be private. */
114 static void
115 wset_combination_limit (struct window *w, Lisp_Object val)
117 w->combination_limit = val;
120 static void
121 wset_dedicated (struct window *w, Lisp_Object val)
123 w->dedicated = val;
126 static void
127 wset_display_table (struct window *w, Lisp_Object val)
129 w->display_table = val;
132 static void
133 wset_new_normal (struct window *w, Lisp_Object val)
135 w->new_normal = val;
138 static void
139 wset_new_total (struct window *w, Lisp_Object val)
141 w->new_total = val;
144 static void
145 wset_normal_cols (struct window *w, Lisp_Object val)
147 w->normal_cols = val;
150 static void
151 wset_normal_lines (struct window *w, Lisp_Object val)
153 w->normal_lines = val;
156 static void
157 wset_parent (struct window *w, Lisp_Object val)
159 w->parent = val;
162 static void
163 wset_pointm (struct window *w, Lisp_Object val)
165 w->pointm = val;
168 static void
169 wset_old_pointm (struct window *w, Lisp_Object val)
171 w->old_pointm = val;
174 static void
175 wset_start (struct window *w, Lisp_Object val)
177 w->start = val;
180 static void
181 wset_temslot (struct window *w, Lisp_Object val)
183 w->temslot = val;
186 static void
187 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
189 w->vertical_scroll_bar_type = val;
192 static void
193 wset_window_parameters (struct window *w, Lisp_Object val)
195 w->window_parameters = val;
198 static void
199 wset_combination (struct window *w, bool horflag, Lisp_Object val)
201 /* Since leaf windows never becomes non-leaf, there should
202 be no buffer and markers in start and pointm fields of W. */
203 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
204 w->contents = val;
205 /* When an internal window is deleted and VAL is nil, HORFLAG
206 is meaningless. */
207 if (!NILP (val))
208 w->horizontal = horflag;
211 /* True if leaf window W doesn't reflect the actual state
212 of displayed buffer due to its text or overlays change. */
214 bool
215 window_outdated (struct window *w)
217 struct buffer *b = XBUFFER (w->contents);
218 return (w->last_modified < BUF_MODIFF (b)
219 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
222 struct window *
223 decode_live_window (register Lisp_Object window)
225 if (NILP (window))
226 return XWINDOW (selected_window);
228 CHECK_LIVE_WINDOW (window);
229 return XWINDOW (window);
232 struct window *
233 decode_any_window (register Lisp_Object window)
235 struct window *w;
237 if (NILP (window))
238 return XWINDOW (selected_window);
240 CHECK_WINDOW (window);
241 w = XWINDOW (window);
242 return w;
245 static struct window *
246 decode_valid_window (register Lisp_Object window)
248 struct window *w;
250 if (NILP (window))
251 return XWINDOW (selected_window);
253 CHECK_VALID_WINDOW (window);
254 w = XWINDOW (window);
255 return w;
258 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
259 cease its buffer, and 1 means that W is about to set up the new one. */
261 static void
262 adjust_window_count (struct window *w, int arg)
264 eassert (eabs (arg) == 1);
265 if (BUFFERP (w->contents))
267 struct buffer *b = XBUFFER (w->contents);
269 if (b->base_buffer)
270 b = b->base_buffer;
271 b->window_count += arg;
272 eassert (b->window_count >= 0);
273 /* These should be recalculated by redisplay code. */
274 w->window_end_valid = false;
275 w->base_line_pos = 0;
279 /* Set W's buffer slot to VAL and recompute number
280 of windows showing VAL if it is a buffer. */
282 void
283 wset_buffer (struct window *w, Lisp_Object val)
285 adjust_window_count (w, -1);
286 if (BUFFERP (val))
287 /* Make sure that we do not assign the buffer
288 to an internal window. */
289 eassert (MARKERP (w->start) && MARKERP (w->pointm));
290 w->contents = val;
291 adjust_window_count (w, 1);
294 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
295 doc: /* Return t if OBJECT is a window and nil otherwise. */)
296 (Lisp_Object object)
298 return WINDOWP (object) ? Qt : Qnil;
301 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
302 doc: /* Return t if OBJECT is a valid window and nil otherwise.
303 A valid window is either a window that displays a buffer or an internal
304 window. Windows that have been deleted are not valid. */)
305 (Lisp_Object object)
307 return WINDOW_VALID_P (object) ? Qt : Qnil;
310 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
311 doc: /* Return t if OBJECT is a live window and nil otherwise.
312 A live window is a window that displays a buffer.
313 Internal windows and deleted windows are not live. */)
314 (Lisp_Object object)
316 return WINDOW_LIVE_P (object) ? Qt : Qnil;
319 /* Frames and windows. */
320 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
321 doc: /* Return the frame that window WINDOW is on.
322 WINDOW must be a valid window and defaults to the selected one. */)
323 (Lisp_Object window)
325 return decode_valid_window (window)->frame;
328 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
329 doc: /* Return the root window of FRAME-OR-WINDOW.
330 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
331 With a frame argument, return that frame's root window.
332 With a window argument, return the root window of that window's frame. */)
333 (Lisp_Object frame_or_window)
335 Lisp_Object window;
337 if (NILP (frame_or_window))
338 window = SELECTED_FRAME ()->root_window;
339 else if (WINDOW_VALID_P (frame_or_window))
340 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
341 else
343 CHECK_LIVE_FRAME (frame_or_window);
344 window = XFRAME (frame_or_window)->root_window;
347 return window;
350 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
351 doc: /* Return the minibuffer window for frame FRAME.
352 If FRAME is omitted or nil, it defaults to the selected frame. */)
353 (Lisp_Object frame)
355 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
358 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
359 Swindow_minibuffer_p, 0, 1, 0,
360 doc: /* Return non-nil if WINDOW is a minibuffer window.
361 WINDOW must be a valid window and defaults to the selected one. */)
362 (Lisp_Object window)
364 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
367 /* Don't move this to window.el - this must be a safe routine. */
368 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
369 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
370 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
371 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
372 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
373 the first window of that frame. */)
374 (Lisp_Object frame_or_window)
376 Lisp_Object window;
378 if (NILP (frame_or_window))
379 window = SELECTED_FRAME ()->root_window;
380 else if (WINDOW_VALID_P (frame_or_window))
381 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
382 else
384 CHECK_LIVE_FRAME (frame_or_window);
385 window = XFRAME (frame_or_window)->root_window;
388 while (WINDOWP (XWINDOW (window)->contents))
389 window = XWINDOW (window)->contents;
391 return window;
394 DEFUN ("frame-selected-window", Fframe_selected_window,
395 Sframe_selected_window, 0, 1, 0,
396 doc: /* Return the selected window of FRAME-OR-WINDOW.
397 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
398 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
399 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
400 return the selected window of that frame. */)
401 (Lisp_Object frame_or_window)
403 Lisp_Object window;
405 if (NILP (frame_or_window))
406 window = SELECTED_FRAME ()->selected_window;
407 else if (WINDOW_VALID_P (frame_or_window))
408 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
409 else
411 CHECK_LIVE_FRAME (frame_or_window);
412 window = XFRAME (frame_or_window)->selected_window;
415 return window;
418 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
419 Sset_frame_selected_window, 2, 3, 0,
420 doc: /* Set selected window of FRAME to WINDOW.
421 FRAME must be a live frame and defaults to the selected one. If FRAME
422 is the selected frame, this makes WINDOW the selected window. Optional
423 argument NORECORD non-nil means to neither change the order of recently
424 selected windows nor the buffer list. WINDOW must denote a live window.
425 Return WINDOW. */)
426 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
428 if (NILP (frame))
429 frame = selected_frame;
431 CHECK_LIVE_FRAME (frame);
432 CHECK_LIVE_WINDOW (window);
434 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
435 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
437 if (EQ (frame, selected_frame))
438 return Fselect_window (window, norecord);
439 else
441 fset_selected_window (XFRAME (frame), window);
442 return window;
446 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
447 doc: /* Return the selected window.
448 The selected window is the window in which the standard cursor for
449 selected windows appears and to which many commands apply. */)
450 (void)
452 return selected_window;
455 EMACS_INT window_select_count;
457 /* If select_window is called with inhibit_point_swap true it will
458 not store point of the old selected window's buffer back into that
459 window's pointm slot. This is needed by Fset_window_configuration to
460 avoid that the display routine is called with selected_window set to
461 Qnil causing a subsequent crash. */
462 static Lisp_Object
463 select_window (Lisp_Object window, Lisp_Object norecord,
464 bool inhibit_point_swap)
466 struct window *w;
467 struct frame *sf;
469 CHECK_LIVE_WINDOW (window);
471 w = XWINDOW (window);
473 /* Make the selected window's buffer current. */
474 Fset_buffer (w->contents);
476 if (EQ (window, selected_window) && !inhibit_point_swap)
477 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
478 way to call record_buffer from Elisp, so it's important that we call
479 record_buffer before returning here. */
480 goto record_and_return;
482 if (NILP (norecord))
483 { /* Mark the window for redisplay since the selected-window has
484 a different mode-line. */
485 wset_redisplay (XWINDOW (selected_window));
486 wset_redisplay (w);
488 else
489 redisplay_other_windows ();
491 sf = SELECTED_FRAME ();
492 if (XFRAME (WINDOW_FRAME (w)) != sf)
494 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
495 /* Use this rather than Fhandle_switch_frame
496 so that FRAME_FOCUS_FRAME is moved appropriately as we
497 move around in the state where a minibuffer in a separate
498 frame is active. */
499 Fselect_frame (WINDOW_FRAME (w), norecord);
500 /* Fselect_frame called us back so we've done all the work already. */
501 eassert (EQ (window, selected_window));
502 return window;
504 else
505 fset_selected_window (sf, window);
507 select_window_1 (window, inhibit_point_swap);
508 bset_last_selected_window (XBUFFER (w->contents), window);
510 record_and_return:
511 /* record_buffer can run QUIT, so make sure it is run only after we have
512 re-established the invariant between selected_window and selected_frame,
513 otherwise the temporary broken invariant might "escape" (bug#14161). */
514 if (NILP (norecord))
516 w->use_time = ++window_select_count;
517 record_buffer (w->contents);
520 return window;
523 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
524 (not even for redisplay's benefit), and assume that the window's frame is
525 already selected. */
526 static void
527 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
529 /* Store the old selected window's buffer's point in pointm of the old
530 selected window. It belongs to that window, and when the window is
531 not selected, must be in the window. */
532 if (!inhibit_point_swap)
534 struct window *ow = XWINDOW (selected_window);
535 if (BUFFERP (ow->contents))
536 set_marker_both (ow->pointm, ow->contents,
537 BUF_PT (XBUFFER (ow->contents)),
538 BUF_PT_BYTE (XBUFFER (ow->contents)));
541 selected_window = window;
543 /* Go to the point recorded in the window.
544 This is important when the buffer is in more
545 than one window. It also matters when
546 redisplay_window has altered point after scrolling,
547 because it makes the change only in the window. */
548 set_point_from_marker (XWINDOW (window)->pointm);
551 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
552 doc: /* Select WINDOW which must be a live window.
553 Also make WINDOW's frame the selected frame and WINDOW that frame's
554 selected window. In addition, make WINDOW's buffer current and set its
555 buffer's value of `point' to the value of WINDOW's `window-point'.
556 Return WINDOW.
558 Optional second arg NORECORD non-nil means do not put this buffer at the
559 front of the buffer list and do not make this window the most recently
560 selected one.
562 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
563 applications and internal routines often select a window temporarily for
564 various purposes; mostly, to simplify coding. As a rule, such
565 selections should be not recorded and therefore will not pollute
566 `buffer-list-update-hook'. Selections that "really count" are those
567 causing a visible change in the next redisplay of WINDOW's frame and
568 should be always recorded. So if you think of running a function each
569 time a window gets selected put it on `buffer-list-update-hook'.
571 Also note that the main editor command loop sets the current buffer to
572 the buffer of the selected window before each command. */)
573 (Lisp_Object window, Lisp_Object norecord)
575 return select_window (window, norecord, false);
578 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
579 doc: /* Return the buffer displayed in window WINDOW.
580 If WINDOW is omitted or nil, it defaults to the selected window.
581 Return nil for an internal window or a deleted window. */)
582 (Lisp_Object window)
584 struct window *w = decode_any_window (window);
585 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
588 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
589 doc: /* Return the parent window of window WINDOW.
590 WINDOW must be a valid window and defaults to the selected one.
591 Return nil for a window with no parent (e.g. a root window). */)
592 (Lisp_Object window)
594 return decode_valid_window (window)->parent;
597 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
598 doc: /* Return the topmost child window of window WINDOW.
599 WINDOW must be a valid window and defaults to the selected one.
600 Return nil if WINDOW is a live window (live windows have no children).
601 Return nil if WINDOW is an internal window whose children form a
602 horizontal combination. */)
603 (Lisp_Object window)
605 struct window *w = decode_valid_window (window);
606 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
609 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
610 doc: /* Return the leftmost child window of window WINDOW.
611 WINDOW must be a valid window and defaults to the selected one.
612 Return nil if WINDOW is a live window (live windows have no children).
613 Return nil if WINDOW is an internal window whose children form a
614 vertical combination. */)
615 (Lisp_Object window)
617 struct window *w = decode_valid_window (window);
618 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
621 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
622 doc: /* Return the next sibling window of window WINDOW.
623 WINDOW must be a valid window and defaults to the selected one.
624 Return nil if WINDOW has no next sibling. */)
625 (Lisp_Object window)
627 return decode_valid_window (window)->next;
630 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
631 doc: /* Return the previous sibling window of window WINDOW.
632 WINDOW must be a valid window and defaults to the selected one.
633 Return nil if WINDOW has no previous sibling. */)
634 (Lisp_Object window)
636 return decode_valid_window (window)->prev;
639 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
640 doc: /* Return combination limit of window WINDOW.
641 WINDOW must be a valid window used in horizontal or vertical combination.
642 If the return value is nil, child windows of WINDOW can be recombined with
643 WINDOW's siblings. A return value of t means that child windows of
644 WINDOW are never (re-)combined with WINDOW's siblings. */)
645 (Lisp_Object window)
647 struct window *w;
649 CHECK_VALID_WINDOW (window);
650 w = XWINDOW (window);
651 if (WINDOW_LEAF_P (w))
652 error ("Combination limit is meaningful for internal windows only");
653 return w->combination_limit;
656 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
657 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
658 WINDOW must be a valid window used in horizontal or vertical combination.
659 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
660 siblings. LIMIT t means that child windows of WINDOW are never
661 (re-)combined with WINDOW's siblings. Other values are reserved for
662 future use. */)
663 (Lisp_Object window, Lisp_Object limit)
665 struct window *w;
667 CHECK_VALID_WINDOW (window);
668 w = XWINDOW (window);
669 if (WINDOW_LEAF_P (w))
670 error ("Combination limit is meaningful for internal windows only");
671 wset_combination_limit (w, limit);
672 return limit;
675 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
676 doc: /* Return the use time of window WINDOW.
677 WINDOW must be a live window and defaults to the selected one.
678 The window with the highest use time is the most recently selected
679 one. The window with the lowest use time is the least recently
680 selected one. */)
681 (Lisp_Object window)
683 return make_number (decode_live_window (window)->use_time);
686 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
687 doc: /* Return the width of window WINDOW in pixels.
688 WINDOW must be a valid window and defaults to the selected one.
690 The return value includes the fringes and margins of WINDOW as well as
691 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
692 an internal window, its pixel width is the width of the screen areas
693 spanned by its children. */)
694 (Lisp_Object window)
696 return make_number (decode_valid_window (window)->pixel_width);
699 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
700 doc: /* Return the height of window WINDOW in pixels.
701 WINDOW must be a valid window and defaults to the selected one.
703 The return value includes the mode line and header line and the bottom
704 divider, if any. If WINDOW is an internal window, its pixel height is
705 the height of the screen areas spanned by its children. */)
706 (Lisp_Object window)
708 return make_number (decode_valid_window (window)->pixel_height);
711 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
712 doc: /* Return the height of window WINDOW in lines.
713 WINDOW must be a valid window and defaults to the selected one.
715 The return value includes the heights of WINDOW's mode and header line
716 and its bottom divider, if any. If WINDOW is an internal window, the
717 total height is the height of the screen areas spanned by its children.
719 If WINDOW's pixel height is not an integral multiple of its frame's
720 character height, the number of lines occupied by WINDOW is rounded
721 internally. This is done in a way such that, if WINDOW is a parent
722 window, the sum of the total heights of all its children internally
723 equals the total height of WINDOW.
725 If the optional argument ROUND is `ceiling', return the smallest integer
726 larger than WINDOW's pixel height divided by the character height of
727 WINDOW's frame. ROUND `floor' means to return the largest integer
728 smaller than WINDOW's pixel height divided by the character height of
729 WINDOW's frame. Any other value of ROUND means to return the internal
730 total height of WINDOW. */)
731 (Lisp_Object window, Lisp_Object round)
733 struct window *w = decode_valid_window (window);
735 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
736 return make_number (w->total_lines);
737 else
739 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
741 return make_number (EQ (round, Qceiling)
742 ? ((w->pixel_height + unit - 1) /unit)
743 : (w->pixel_height / unit));
747 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
748 doc: /* Return the total width of window WINDOW in columns.
749 WINDOW must be a valid window and defaults to the selected one.
751 The return value includes the widths of WINDOW's fringes, margins,
752 scroll bars and its right divider, if any. If WINDOW is an internal
753 window, the total width is the width of the screen areas spanned by its
754 children.
756 If WINDOW's pixel width is not an integral multiple of its frame's
757 character width, the number of lines occupied by WINDOW is rounded
758 internally. This is done in a way such that, if WINDOW is a parent
759 window, the sum of the total widths of all its children internally
760 equals the total width of WINDOW.
762 If the optional argument ROUND is `ceiling', return the smallest integer
763 larger than WINDOW's pixel width divided by the character width of
764 WINDOW's frame. ROUND `floor' means to return the largest integer
765 smaller than WINDOW's pixel width divided by the character width of
766 WINDOW's frame. Any other value of ROUND means to return the internal
767 total width of WINDOW. */)
768 (Lisp_Object window, Lisp_Object round)
770 struct window *w = decode_valid_window (window);
772 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
773 return make_number (w->total_cols);
774 else
776 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
778 return make_number (EQ (round, Qceiling)
779 ? ((w->pixel_width + unit - 1) /unit)
780 : (w->pixel_width / unit));
784 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
785 doc: /* Return the new total size of window WINDOW.
786 WINDOW must be a valid window and defaults to the selected one.
788 The new total size of WINDOW is the value set by the last call of
789 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
790 installed as WINDOW's total height (see `window-total-height') or total
791 width (see `window-total-width'). */)
792 (Lisp_Object window)
794 return decode_valid_window (window)->new_total;
797 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
798 doc: /* Return the normal height of window WINDOW.
799 WINDOW must be a valid window and defaults to the selected one.
800 If HORIZONTAL is non-nil, return the normal width of WINDOW.
802 The normal height of a frame's root window or a window that is
803 horizontally combined (a window that has a left or right sibling) is
804 1.0. The normal height of a window that is vertically combined (has a
805 sibling above or below) is the fraction of the window's height with
806 respect to its parent. The sum of the normal heights of all windows in a
807 vertical combination equals 1.0.
809 Similarly, the normal width of a frame's root window or a window that is
810 vertically combined equals 1.0. The normal width of a window that is
811 horizontally combined is the fraction of the window's width with respect
812 to its parent. The sum of the normal widths of all windows in a
813 horizontal combination equals 1.0.
815 The normal sizes of windows are used to restore the proportional sizes
816 of windows after they have been shrunk to their minimum sizes; for
817 example when a frame is temporarily made very small and afterwards gets
818 re-enlarged to its previous size. */)
819 (Lisp_Object window, Lisp_Object horizontal)
821 struct window *w = decode_valid_window (window);
823 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
826 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
827 doc: /* Return new normal size of window WINDOW.
828 WINDOW must be a valid window and defaults to the selected one.
830 The new normal size of WINDOW is the value set by the last call of
831 `set-window-new-normal' for WINDOW. If valid, it will be shortly
832 installed as WINDOW's normal size (see `window-normal-size'). */)
833 (Lisp_Object window)
835 return decode_valid_window (window)->new_normal;
838 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
839 doc: /* Return new pixel size of window WINDOW.
840 WINDOW must be a valid window and defaults to the selected one.
842 The new pixel size of WINDOW is the value set by the last call of
843 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
844 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
845 width (see `window-pixel-width'). */)
846 (Lisp_Object window)
848 return decode_valid_window (window)->new_pixel;
851 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
852 doc: /* Return left pixel edge of window WINDOW.
853 WINDOW must be a valid window and defaults to the selected one. */)
854 (Lisp_Object window)
856 return make_number (decode_valid_window (window)->pixel_left);
859 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
860 doc: /* Return top pixel edge of window WINDOW.
861 WINDOW must be a valid window and defaults to the selected one. */)
862 (Lisp_Object window)
864 return make_number (decode_valid_window (window)->pixel_top);
867 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
868 doc: /* Return left column of window WINDOW.
869 This is the distance, in columns, between the left edge of WINDOW and
870 the left edge of the frame's window area. For instance, the return
871 value is 0 if there is no window to the left of WINDOW.
873 WINDOW must be a valid window and defaults to the selected one. */)
874 (Lisp_Object window)
876 return make_number (decode_valid_window (window)->left_col);
879 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
880 doc: /* Return top line of window WINDOW.
881 This is the distance, in lines, between the top of WINDOW and the top
882 of the frame's window area. For instance, the return value is 0 if
883 there is no window above WINDOW.
885 WINDOW must be a valid window and defaults to the selected one. */)
886 (Lisp_Object window)
888 return make_number (decode_valid_window (window)->top_line);
891 /* Return the number of lines/pixels of W's body. Don't count any mode
892 or header line or horizontal divider of W. Rounds down to nearest
893 integer when not working pixelwise. */
894 static int
895 window_body_height (struct window *w, bool pixelwise)
897 int height = (w->pixel_height
898 - WINDOW_HEADER_LINE_HEIGHT (w)
899 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
900 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
901 : 0)
902 - WINDOW_MODE_LINE_HEIGHT (w)
903 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
905 /* Don't return a negative value. */
906 return max (pixelwise
907 ? height
908 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
912 /* Return the number of columns/pixels of W's body. Don't count columns
913 occupied by the scroll bar or the divider/vertical bar separating W
914 from its right sibling or margins. On window-systems don't count
915 fringes either. Round down to nearest integer when not working
916 pixelwise. */
918 window_body_width (struct window *w, bool pixelwise)
920 struct frame *f = XFRAME (WINDOW_FRAME (w));
922 int width = (w->pixel_width
923 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
924 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
925 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
926 : (/* A vertical bar is either 1 or 0. */
927 !FRAME_WINDOW_P (f)
928 && !WINDOW_RIGHTMOST_P (w)
929 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
930 - WINDOW_MARGINS_WIDTH (w)
931 - (FRAME_WINDOW_P (f)
932 ? WINDOW_FRINGES_WIDTH (w)
933 : 0));
935 /* Don't return a negative value. */
936 return max (pixelwise
937 ? width
938 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
942 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
943 doc: /* Return the height of WINDOW's text area.
944 WINDOW must be a live window and defaults to the selected one. Optional
945 argument PIXELWISE non-nil means return the height of WINDOW's text area
946 in pixels. The return value does not include the mode line or header
947 line or any horizontal divider.
949 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
950 pixel height divided by the character height of WINDOW's frame. This
951 means that if a line at the bottom of the text area is only partially
952 visible, that line is not counted. */)
953 (Lisp_Object window, Lisp_Object pixelwise)
955 return make_number (window_body_height (decode_live_window (window),
956 !NILP (pixelwise)));
959 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
960 doc: /* Return the width of WINDOW's text area.
961 WINDOW must be a live window and defaults to the selected one. Optional
962 argument PIXELWISE non-nil means return the width in pixels. The return
963 value does not include any vertical dividers, fringes or marginal areas,
964 or scroll bars.
966 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
967 pixel width divided by the character width of WINDOW's frame. This
968 means that if a column at the right of the text area is only partially
969 visible, that column is not counted.
971 Note that the returned value includes the column reserved for the
972 continuation glyph. */)
973 (Lisp_Object window, Lisp_Object pixelwise)
975 return make_number (window_body_width (decode_live_window (window),
976 !NILP (pixelwise)));
979 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
980 Swindow_mode_line_height, 0, 1, 0,
981 doc: /* Return the height in pixels of WINDOW's mode-line.
982 WINDOW must be a live window and defaults to the selected one. */)
983 (Lisp_Object window)
985 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
988 DEFUN ("window-header-line-height", Fwindow_header_line_height,
989 Swindow_header_line_height, 0, 1, 0,
990 doc: /* Return the height in pixels of WINDOW's header-line.
991 WINDOW must be a live window and defaults to the selected one. */)
992 (Lisp_Object window)
994 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
997 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
998 Swindow_right_divider_width, 0, 1, 0,
999 doc: /* Return the width in pixels of WINDOW's right divider.
1000 WINDOW must be a live window and defaults to the selected one. */)
1001 (Lisp_Object window)
1003 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1006 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1007 Swindow_bottom_divider_width, 0, 1, 0,
1008 doc: /* Return the width in pixels of WINDOW's bottom divider.
1009 WINDOW must be a live window and defaults to the selected one. */)
1010 (Lisp_Object window)
1012 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1015 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1016 Swindow_scroll_bar_width, 0, 1, 0,
1017 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1018 WINDOW must be a live window and defaults to the selected one. */)
1019 (Lisp_Object window)
1021 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1024 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1025 Swindow_scroll_bar_height, 0, 1, 0,
1026 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1027 WINDOW must be a live window and defaults to the selected one. */)
1028 (Lisp_Object window)
1030 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1033 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1034 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1035 WINDOW must be a live window and defaults to the selected one. */)
1036 (Lisp_Object window)
1038 return make_number (decode_live_window (window)->hscroll);
1041 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1042 range, returning the new amount as a fixnum. */
1043 static Lisp_Object
1044 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1046 /* Horizontal scrolling has problems with large scroll amounts.
1047 It's too slow with long lines, and even with small lines the
1048 display can be messed up. For now, though, impose only the limits
1049 required by the internal representation: horizontal scrolling must
1050 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1051 (since it's stored in a ptrdiff_t). */
1052 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1053 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1055 /* Prevent redisplay shortcuts when changing the hscroll. */
1056 if (w->hscroll != new_hscroll)
1057 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1059 w->hscroll = new_hscroll;
1060 w->suspend_auto_hscroll = true;
1062 return make_number (new_hscroll);
1065 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1066 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1067 WINDOW must be a live window and defaults to the selected one.
1068 Clip the number to a reasonable value if out of range.
1069 Return the new number. NCOL should be zero or positive.
1071 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1072 window so that the location of point moves off-window. */)
1073 (Lisp_Object window, Lisp_Object ncol)
1075 CHECK_NUMBER (ncol);
1076 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1079 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1080 Swindow_redisplay_end_trigger, 0, 1, 0,
1081 doc: /* Return WINDOW's redisplay end trigger value.
1082 WINDOW must be a live window and defaults to the selected one.
1083 See `set-window-redisplay-end-trigger' for more information. */)
1084 (Lisp_Object window)
1086 return decode_live_window (window)->redisplay_end_trigger;
1089 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1090 Sset_window_redisplay_end_trigger, 2, 2, 0,
1091 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1092 WINDOW must be a live window and defaults to the selected one. VALUE
1093 should be a buffer position (typically a marker) or nil. If it is a
1094 buffer position, then if redisplay in WINDOW reaches a position beyond
1095 VALUE, the functions in `redisplay-end-trigger-functions' are called
1096 with two arguments: WINDOW, and the end trigger value. Afterwards the
1097 end-trigger value is reset to nil. */)
1098 (register Lisp_Object window, Lisp_Object value)
1100 wset_redisplay_end_trigger (decode_live_window (window), value);
1101 return value;
1104 /* Test if the character at column X, row Y is within window W.
1105 If it is not, return ON_NOTHING;
1106 if it is on the window's vertical divider, return
1107 ON_RIGHT_DIVIDER;
1108 if it is on the window's horizontal divider, return
1109 ON_BOTTOM_DIVIDER;
1110 if it is in the window's text area, return ON_TEXT;
1111 if it is on the window's modeline, return ON_MODE_LINE;
1112 if it is on the border between the window and its right sibling,
1113 return ON_VERTICAL_BORDER;
1114 if it is on a scroll bar, return ON_SCROLL_BAR;
1115 if it is on the window's top line, return ON_HEADER_LINE;
1116 if it is in left or right fringe of the window,
1117 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1118 if it is in the marginal area to the left/right of the window,
1119 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1121 X and Y are frame relative pixel coordinates. */
1123 static enum window_part
1124 coordinates_in_window (register struct window *w, int x, int y)
1126 struct frame *f = XFRAME (WINDOW_FRAME (w));
1127 enum window_part part;
1128 int ux = FRAME_COLUMN_WIDTH (f);
1129 int left_x = WINDOW_LEFT_EDGE_X (w);
1130 int right_x = WINDOW_RIGHT_EDGE_X (w);
1131 int top_y = WINDOW_TOP_EDGE_Y (w);
1132 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1133 /* The width of the area where the vertical line can be dragged.
1134 (Between mode lines for instance. */
1135 int grabbable_width = ux;
1136 int lmargin_width, rmargin_width, text_left, text_right;
1138 /* Outside any interesting row or column? */
1139 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1140 return ON_NOTHING;
1142 /* On the horizontal window divider (which prevails the vertical
1143 divider)? */
1144 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1145 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1146 && y <= bottom_y)
1147 return ON_BOTTOM_DIVIDER;
1148 /* On vertical window divider? */
1149 else if (!WINDOW_RIGHTMOST_P (w)
1150 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1151 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1152 && x <= right_x)
1153 return ON_RIGHT_DIVIDER;
1154 /* On the horizontal scroll bar? (Including the empty space at its
1155 right!) */
1156 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1157 && y >= (bottom_y
1158 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1159 - CURRENT_MODE_LINE_HEIGHT (w)
1160 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1161 && y <= (bottom_y
1162 - CURRENT_MODE_LINE_HEIGHT (w)
1163 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1164 return ON_HORIZONTAL_SCROLL_BAR;
1165 /* On the mode or header line? */
1166 else if ((WINDOW_WANTS_MODELINE_P (w)
1167 && y >= (bottom_y
1168 - CURRENT_MODE_LINE_HEIGHT (w)
1169 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1170 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1171 && (part = ON_MODE_LINE))
1172 || (WINDOW_WANTS_HEADER_LINE_P (w)
1173 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1174 && (part = ON_HEADER_LINE)))
1176 /* If it's under/over the scroll bar portion of the mode/header
1177 line, say it's on the vertical line. That's to be able to
1178 resize windows horizontally in case we're using toolkit scroll
1179 bars. Note: If scrollbars are on the left, the window that
1180 must be eventually resized is that on the left of WINDOW. */
1181 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1182 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1183 && !WINDOW_LEFTMOST_P (w)
1184 && eabs (x - left_x) < grabbable_width)
1185 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1186 && !WINDOW_RIGHTMOST_P (w)
1187 && eabs (x - right_x) < grabbable_width)))
1188 return ON_VERTICAL_BORDER;
1189 else
1190 return part;
1193 /* In what's below, we subtract 1 when computing right_x because we
1194 want the rightmost pixel, which is given by left_pixel+width-1. */
1195 if (w->pseudo_window_p)
1197 left_x = 0;
1198 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1200 else
1202 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1203 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1206 /* Outside any interesting column? */
1207 if (x < left_x || x > right_x)
1208 return ON_VERTICAL_SCROLL_BAR;
1210 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1211 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1213 text_left = window_box_left (w, TEXT_AREA);
1214 text_right = text_left + window_box_width (w, TEXT_AREA);
1216 if (FRAME_WINDOW_P (f))
1218 if (!w->pseudo_window_p
1219 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1220 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1221 && !WINDOW_RIGHTMOST_P (w)
1222 && (eabs (x - right_x) < grabbable_width))
1223 return ON_VERTICAL_BORDER;
1225 /* Need to say "x > right_x" rather than >=, since on character
1226 terminals, the vertical line's x coordinate is right_x. */
1227 else if (!w->pseudo_window_p
1228 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1229 && !WINDOW_RIGHTMOST_P (w)
1230 /* Why check ux if we are not the rightmost window? Also
1231 shouldn't a pseudo window always be rightmost? */
1232 && x > right_x - ux)
1233 return ON_VERTICAL_BORDER;
1235 if (x < text_left)
1237 if (lmargin_width > 0
1238 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1239 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1240 : (x < left_x + lmargin_width)))
1241 return ON_LEFT_MARGIN;
1242 else
1243 return ON_LEFT_FRINGE;
1246 if (x >= text_right)
1248 if (rmargin_width > 0
1249 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1250 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1251 : (x >= right_x - rmargin_width)))
1252 return ON_RIGHT_MARGIN;
1253 else
1254 return ON_RIGHT_FRINGE;
1257 /* Everything special ruled out - must be on text area */
1258 return ON_TEXT;
1261 /* Take X is the frame-relative pixel x-coordinate, and return the
1262 x-coordinate relative to part PART of window W. */
1264 window_relative_x_coord (struct window *w, enum window_part part, int x)
1266 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1268 switch (part)
1270 case ON_TEXT:
1271 return x - window_box_left (w, TEXT_AREA);
1273 case ON_HEADER_LINE:
1274 case ON_MODE_LINE:
1275 case ON_LEFT_FRINGE:
1276 return x - left_x;
1278 case ON_RIGHT_FRINGE:
1279 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1281 case ON_LEFT_MARGIN:
1282 return (x - left_x
1283 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1284 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1286 case ON_RIGHT_MARGIN:
1287 return (x + 1
1288 - ((w->pseudo_window_p)
1289 ? WINDOW_PIXEL_WIDTH (w)
1290 : WINDOW_BOX_RIGHT_EDGE_X (w))
1291 + window_box_width (w, RIGHT_MARGIN_AREA)
1292 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1293 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1295 case ON_NOTHING:
1296 case ON_VERTICAL_BORDER:
1297 case ON_VERTICAL_SCROLL_BAR:
1298 case ON_HORIZONTAL_SCROLL_BAR:
1299 case ON_RIGHT_DIVIDER:
1300 case ON_BOTTOM_DIVIDER:
1301 return 0;
1303 default:
1304 emacs_abort ();
1309 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1310 Scoordinates_in_window_p, 2, 2, 0,
1311 doc: /* Return non-nil if COORDINATES are in WINDOW.
1312 WINDOW must be a live window and defaults to the selected one.
1313 COORDINATES is a cons of the form (X . Y), X and Y being distances
1314 measured in characters from the upper-left corner of the frame.
1315 (0 . 0) denotes the character in the upper left corner of the
1316 frame.
1317 If COORDINATES are in the text portion of WINDOW,
1318 the coordinates relative to the window are returned.
1319 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1320 If they are in the right divider of WINDOW, `right-divider' is returned.
1321 If they are in the mode line of WINDOW, `mode-line' is returned.
1322 If they are in the header line of WINDOW, `header-line' is returned.
1323 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1324 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1325 If they are on the border between WINDOW and its right sibling,
1326 `vertical-line' is returned.
1327 If they are in the windows's left or right marginal areas, `left-margin'\n\
1328 or `right-margin' is returned. */)
1329 (register Lisp_Object coordinates, Lisp_Object window)
1331 struct window *w;
1332 struct frame *f;
1333 int x, y;
1334 Lisp_Object lx, ly;
1336 w = decode_live_window (window);
1337 f = XFRAME (w->frame);
1338 CHECK_CONS (coordinates);
1339 lx = Fcar (coordinates);
1340 ly = Fcdr (coordinates);
1341 CHECK_NUMBER_OR_FLOAT (lx);
1342 CHECK_NUMBER_OR_FLOAT (ly);
1343 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1344 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1346 switch (coordinates_in_window (w, x, y))
1348 case ON_NOTHING:
1349 return Qnil;
1351 case ON_TEXT:
1352 /* Convert X and Y to window relative pixel coordinates, and
1353 return the canonical char units. */
1354 x -= window_box_left (w, TEXT_AREA);
1355 y -= WINDOW_TOP_EDGE_Y (w);
1356 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1357 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1359 case ON_MODE_LINE:
1360 return Qmode_line;
1362 case ON_VERTICAL_BORDER:
1363 return Qvertical_line;
1365 case ON_HEADER_LINE:
1366 return Qheader_line;
1368 case ON_LEFT_FRINGE:
1369 return Qleft_fringe;
1371 case ON_RIGHT_FRINGE:
1372 return Qright_fringe;
1374 case ON_LEFT_MARGIN:
1375 return Qleft_margin;
1377 case ON_RIGHT_MARGIN:
1378 return Qright_margin;
1380 case ON_VERTICAL_SCROLL_BAR:
1381 /* Historically we are supposed to return nil in this case. */
1382 return Qnil;
1384 case ON_HORIZONTAL_SCROLL_BAR:
1385 return Qnil;
1387 case ON_RIGHT_DIVIDER:
1388 return Qright_divider;
1390 case ON_BOTTOM_DIVIDER:
1391 return Qbottom_divider;
1393 default:
1394 emacs_abort ();
1399 /* Callback for foreach_window, used in window_from_coordinates.
1400 Check if window W contains coordinates specified by USER_DATA which
1401 is actually a pointer to a struct check_window_data CW.
1403 Check if window W contains coordinates *CW->x and *CW->y. If it
1404 does, return W in *CW->window, as Lisp_Object, and return in
1405 *CW->part the part of the window under coordinates *X,*Y. Return
1406 false from this function to stop iterating over windows. */
1408 struct check_window_data
1410 Lisp_Object *window;
1411 int x, y;
1412 enum window_part *part;
1415 static bool
1416 check_window_containing (struct window *w, void *user_data)
1418 struct check_window_data *cw = user_data;
1419 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1420 if (found == ON_NOTHING)
1421 return true;
1422 else
1424 *cw->part = found;
1425 XSETWINDOW (*cw->window, w);
1426 return false;
1431 /* Find the window containing frame-relative pixel position X/Y and
1432 return it as a Lisp_Object.
1434 If X, Y is on one of the window's special `window_part' elements,
1435 set *PART to the id of that element.
1437 If there is no window under X, Y return nil and leave *PART
1438 unmodified. TOOL_BAR_P means detect tool-bar windows.
1440 This function was previously implemented with a loop cycling over
1441 windows with Fnext_window, and starting with the frame's selected
1442 window. It turned out that this doesn't work with an
1443 implementation of next_window using Vwindow_list, because
1444 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1445 tree of F when this function is called asynchronously from
1446 note_mouse_highlight. The original loop didn't terminate in this
1447 case. */
1449 Lisp_Object
1450 window_from_coordinates (struct frame *f, int x, int y,
1451 enum window_part *part, bool tool_bar_p)
1453 Lisp_Object window;
1454 struct check_window_data cw;
1455 enum window_part dummy;
1457 if (part == 0)
1458 part = &dummy;
1460 window = Qnil;
1461 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1462 foreach_window (f, check_window_containing, &cw);
1464 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1465 /* If not found above, see if it's in the tool bar window, if a tool
1466 bar exists. */
1467 if (NILP (window)
1468 && tool_bar_p
1469 && WINDOWP (f->tool_bar_window)
1470 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1471 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1472 != ON_NOTHING))
1474 *part = ON_TEXT;
1475 window = f->tool_bar_window;
1477 #endif
1479 return window;
1482 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1483 doc: /* Return window containing coordinates X and Y on FRAME.
1484 FRAME must be a live frame and defaults to the selected one.
1485 The top left corner of the frame is considered to be row 0,
1486 column 0. */)
1487 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1489 struct frame *f = decode_live_frame (frame);
1491 /* Check that arguments are integers or floats. */
1492 CHECK_NUMBER_OR_FLOAT (x);
1493 CHECK_NUMBER_OR_FLOAT (y);
1495 return window_from_coordinates (f,
1496 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1497 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1498 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1499 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1500 0, false);
1503 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1504 doc: /* Return current value of point in WINDOW.
1505 WINDOW must be a live window and defaults to the selected one.
1507 For a nonselected window, this is the value point would have if that
1508 window were selected.
1510 Note that, when WINDOW is selected, the value returned is the same as
1511 that returned by `point' for WINDOW's buffer. It would be more strictly
1512 correct to return the top-level value of `point', outside of any
1513 `save-excursion' forms. But that is hard to define. */)
1514 (Lisp_Object window)
1516 register struct window *w = decode_live_window (window);
1518 if (w == XWINDOW (selected_window))
1519 return make_number (BUF_PT (XBUFFER (w->contents)));
1520 else
1521 return Fmarker_position (w->pointm);
1524 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1525 doc: /* Return old value of point in WINDOW.
1526 WINDOW must be a live window and defaults to the selected one. */)
1527 (Lisp_Object window)
1529 return Fmarker_position (decode_live_window (window)->old_pointm);
1532 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1533 doc: /* Return position at which display currently starts in WINDOW.
1534 WINDOW must be a live window and defaults to the selected one.
1535 This is updated by redisplay or by calling `set-window-start'. */)
1536 (Lisp_Object window)
1538 return Fmarker_position (decode_live_window (window)->start);
1541 /* This is text temporarily removed from the doc string below.
1543 This function returns nil if the position is not currently known.
1544 That happens when redisplay is preempted and doesn't finish.
1545 If in that case you want to compute where the end of the window would
1546 have been if redisplay had finished, do this:
1547 (save-excursion
1548 (goto-char (window-start window))
1549 (vertical-motion (1- (window-height window)) window)
1550 (point))") */
1552 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1553 doc: /* Return position at which display currently ends in WINDOW.
1554 WINDOW must be a live window and defaults to the selected one.
1555 This is updated by redisplay, when it runs to completion.
1556 Simply changing the buffer text or setting `window-start'
1557 does not update this value.
1558 Return nil if there is no recorded value. (This can happen if the
1559 last redisplay of WINDOW was preempted, and did not finish.)
1560 If UPDATE is non-nil, compute the up-to-date position
1561 if it isn't already recorded. */)
1562 (Lisp_Object window, Lisp_Object update)
1564 Lisp_Object value;
1565 struct window *w = decode_live_window (window);
1566 Lisp_Object buf;
1567 struct buffer *b;
1569 buf = w->contents;
1570 CHECK_BUFFER (buf);
1571 b = XBUFFER (buf);
1573 if (! NILP (update)
1574 && (windows_or_buffers_changed
1575 || !w->window_end_valid
1576 || b->clip_changed
1577 || b->prevent_redisplay_optimizations_p
1578 || window_outdated (w))
1579 /* Don't call display routines if we didn't yet create any real
1580 frames, because the glyph matrices are not yet allocated in
1581 that case. This could happen in some code that runs in the
1582 daemon during initialization (e.g., see bug#20565). */
1583 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1585 struct text_pos startp;
1586 struct it it;
1587 struct buffer *old_buffer = NULL;
1588 void *itdata = NULL;
1590 /* Cannot use Fvertical_motion because that function doesn't
1591 cope with variable-height lines. */
1592 if (b != current_buffer)
1594 old_buffer = current_buffer;
1595 set_buffer_internal (b);
1598 /* In case W->start is out of the range, use something
1599 reasonable. This situation occurred when loading a file with
1600 `-l' containing a call to `rmail' with subsequent other
1601 commands. At the end, W->start happened to be BEG, while
1602 rmail had already narrowed the buffer. */
1603 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1605 itdata = bidi_shelve_cache ();
1606 start_display (&it, w, startp);
1607 move_it_vertically (&it, window_box_height (w));
1608 if (it.current_y < it.last_visible_y)
1609 move_it_past_eol (&it);
1610 value = make_number (IT_CHARPOS (it));
1611 bidi_unshelve_cache (itdata, false);
1613 if (old_buffer)
1614 set_buffer_internal (old_buffer);
1616 else
1617 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1619 return value;
1622 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1623 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1624 WINDOW must be a live window and defaults to the selected one.
1625 Return POS. */)
1626 (Lisp_Object window, Lisp_Object pos)
1628 register struct window *w = decode_live_window (window);
1630 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1632 if (w == XWINDOW (selected_window))
1634 if (XBUFFER (w->contents) == current_buffer)
1635 Fgoto_char (pos);
1636 else
1638 struct buffer *old_buffer = current_buffer;
1640 /* ... but here we want to catch type error before buffer change. */
1641 CHECK_NUMBER_COERCE_MARKER (pos);
1642 set_buffer_internal (XBUFFER (w->contents));
1643 Fgoto_char (pos);
1644 set_buffer_internal (old_buffer);
1647 else
1649 set_marker_restricted (w->pointm, pos, w->contents);
1650 /* We have to make sure that redisplay updates the window to show
1651 the new value of point. */
1652 wset_redisplay (w);
1655 return pos;
1658 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1659 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1660 WINDOW must be a live window and defaults to the selected one. Return
1661 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1662 overriding motion of point in order to display at this exact start. */)
1663 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1665 register struct window *w = decode_live_window (window);
1667 set_marker_restricted (w->start, pos, w->contents);
1668 /* This is not right, but much easier than doing what is right. */
1669 w->start_at_line_beg = false;
1670 if (NILP (noforce))
1671 w->force_start = true;
1672 w->update_mode_line = true;
1673 /* Bug#15957. */
1674 w->window_end_valid = false;
1675 wset_redisplay (w);
1677 return pos;
1680 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1681 Spos_visible_in_window_p, 0, 3, 0,
1682 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1683 WINDOW must be a live window and defaults to the selected one.
1685 Return nil if that position is scrolled vertically out of view. If a
1686 character is only partially visible, nil is returned, unless the
1687 optional argument PARTIALLY is non-nil. If POS is only out of view
1688 because of horizontal scrolling, return non-nil. If POS is t, it
1689 specifies the position of either the first position displayed on the
1690 last visible screen line in WINDOW, or the end-of-buffer position,
1691 whichever comes first. POS defaults to point in WINDOW; WINDOW defaults
1692 to the selected window.
1694 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1695 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1696 where X and Y are the pixel coordinates relative to the top left corner
1697 of the window. The remaining elements are omitted if the character after
1698 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1699 off-window at the top and bottom of the screen line ("row") containing
1700 POS, ROWH is the visible height of that row, and VPOS is the row number
1701 (zero-based). */)
1702 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1704 struct window *w;
1705 EMACS_INT posint;
1706 struct buffer *buf;
1707 struct text_pos top;
1708 Lisp_Object in_window = Qnil;
1709 int rtop, rbot, rowh, vpos;
1710 bool fully_p = true;
1711 int x, y;
1713 w = decode_live_window (window);
1714 buf = XBUFFER (w->contents);
1715 SET_TEXT_POS_FROM_MARKER (top, w->start);
1717 if (EQ (pos, Qt))
1718 posint = -1;
1719 else if (!NILP (pos))
1721 CHECK_NUMBER_COERCE_MARKER (pos);
1722 posint = XINT (pos);
1724 else if (w == XWINDOW (selected_window))
1725 posint = PT;
1726 else
1727 posint = marker_position (w->pointm);
1729 /* If position is above window start or outside buffer boundaries,
1730 or if window start is out of range, position is not visible. */
1731 if ((EQ (pos, Qt)
1732 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1733 && CHARPOS (top) >= BUF_BEGV (buf)
1734 && CHARPOS (top) <= BUF_ZV (buf)
1735 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1737 fully_p = !rtop && !rbot;
1738 if (!NILP (partially) || fully_p)
1739 in_window = Qt;
1742 if (!NILP (in_window) && !NILP (partially))
1744 Lisp_Object part = Qnil;
1745 if (!fully_p)
1746 part = list4i (rtop, rbot, rowh, vpos);
1747 in_window = Fcons (make_number (x),
1748 Fcons (make_number (y), part));
1751 return in_window;
1754 DEFUN ("window-line-height", Fwindow_line_height,
1755 Swindow_line_height, 0, 2, 0,
1756 doc: /* Return height in pixels of text line LINE in window WINDOW.
1757 WINDOW must be a live window and defaults to the selected one.
1759 Return height of current line if LINE is omitted or nil. Return height of
1760 header or mode line if LINE is `header-line' or `mode-line'.
1761 Otherwise, LINE is a text line number starting from 0. A negative number
1762 counts from the end of the window.
1764 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1765 in pixels of the visible part of the line, VPOS and YPOS are the
1766 vertical position in lines and pixels of the line, relative to the top
1767 of the first text line, and OFFBOT is the number of off-window pixels at
1768 the bottom of the text line. If there are off-window pixels at the top
1769 of the (first) text line, YPOS is negative.
1771 Return nil if window display is not up-to-date. In that case, use
1772 `pos-visible-in-window-p' to obtain the information. */)
1773 (Lisp_Object line, Lisp_Object window)
1775 register struct window *w;
1776 register struct buffer *b;
1777 struct glyph_row *row, *end_row;
1778 int max_y, crop, i;
1779 EMACS_INT n;
1781 w = decode_live_window (window);
1783 if (noninteractive || w->pseudo_window_p)
1784 return Qnil;
1786 CHECK_BUFFER (w->contents);
1787 b = XBUFFER (w->contents);
1789 /* Fail if current matrix is not up-to-date. */
1790 if (!w->window_end_valid
1791 || windows_or_buffers_changed
1792 || b->clip_changed
1793 || b->prevent_redisplay_optimizations_p
1794 || window_outdated (w))
1795 return Qnil;
1797 if (NILP (line))
1799 i = w->cursor.vpos;
1800 if (i < 0 || i >= w->current_matrix->nrows
1801 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1802 return Qnil;
1803 max_y = window_text_bottom_y (w);
1804 goto found_row;
1807 if (EQ (line, Qheader_line))
1809 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1810 return Qnil;
1811 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1812 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1815 if (EQ (line, Qmode_line))
1817 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1818 return (row->enabled_p ?
1819 list4i (row->height,
1820 0, /* not accurate */
1821 (WINDOW_HEADER_LINE_HEIGHT (w)
1822 + window_text_bottom_y (w)),
1824 : Qnil);
1827 CHECK_NUMBER (line);
1828 n = XINT (line);
1830 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1831 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1832 max_y = window_text_bottom_y (w);
1833 i = 0;
1835 while ((n < 0 || i < n)
1836 && row <= end_row && row->enabled_p
1837 && row->y + row->height < max_y)
1838 row++, i++;
1840 if (row > end_row || !row->enabled_p)
1841 return Qnil;
1843 if (++n < 0)
1845 if (-n > i)
1846 return Qnil;
1847 row += n;
1848 i += n;
1851 found_row:
1852 crop = max (0, (row->y + row->height) - max_y);
1853 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1856 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1857 0, 1, 0,
1858 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1859 More precisely, return the value assigned by the last call of
1860 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1861 never called with WINDOW as its argument, or the value set by that
1862 function was internally reset since its last call. WINDOW must be a
1863 live window and defaults to the selected one.
1865 When a window is dedicated to its buffer, `display-buffer' will refrain
1866 from displaying another buffer in it. `get-lru-window' and
1867 `get-largest-window' treat dedicated windows specially.
1868 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1869 `kill-buffer' can delete a dedicated window and the containing frame.
1871 Functions like `set-window-buffer' may change the buffer displayed by a
1872 window, unless that window is "strongly" dedicated to its buffer, that
1873 is the value returned by `window-dedicated-p' is t. */)
1874 (Lisp_Object window)
1876 return decode_live_window (window)->dedicated;
1879 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1880 Sset_window_dedicated_p, 2, 2, 0,
1881 doc: /* Mark WINDOW as dedicated according to FLAG.
1882 WINDOW must be a live window and defaults to the selected one. FLAG
1883 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1884 mark WINDOW as non-dedicated. Return FLAG.
1886 When a window is dedicated to its buffer, `display-buffer' will refrain
1887 from displaying another buffer in it. `get-lru-window' and
1888 `get-largest-window' treat dedicated windows specially.
1889 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1890 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1891 and the containing frame.
1893 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1894 its buffer. Functions like `set-window-buffer' may change the buffer
1895 displayed by a window, unless that window is strongly dedicated to its
1896 buffer. If and when `set-window-buffer' displays another buffer in a
1897 window, it also makes sure that the window is no more dedicated. */)
1898 (Lisp_Object window, Lisp_Object flag)
1900 wset_dedicated (decode_live_window (window), flag);
1901 return flag;
1904 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1905 0, 1, 0,
1906 doc: /* Return buffers previously shown in WINDOW.
1907 WINDOW must be a live window and defaults to the selected one.
1909 The return value is a list of elements (BUFFER WINDOW-START POS),
1910 where BUFFER is a buffer, WINDOW-START is the start position of the
1911 window for that buffer, and POS is a window-specific point value. */)
1912 (Lisp_Object window)
1914 return decode_live_window (window)->prev_buffers;
1917 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1918 Sset_window_prev_buffers, 2, 2, 0,
1919 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1920 WINDOW must be a live window and defaults to the selected one.
1922 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1923 where BUFFER is a buffer, WINDOW-START is the start position of the
1924 window for that buffer, and POS is a window-specific point value. */)
1925 (Lisp_Object window, Lisp_Object prev_buffers)
1927 wset_prev_buffers (decode_live_window (window), prev_buffers);
1928 return prev_buffers;
1931 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1932 0, 1, 0,
1933 doc: /* Return list of buffers recently re-shown in WINDOW.
1934 WINDOW must be a live window and defaults to the selected one. */)
1935 (Lisp_Object window)
1937 return decode_live_window (window)->next_buffers;
1940 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1941 Sset_window_next_buffers, 2, 2, 0,
1942 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1943 WINDOW must be a live window and defaults to the selected one.
1944 NEXT-BUFFERS should be a list of buffers. */)
1945 (Lisp_Object window, Lisp_Object next_buffers)
1947 wset_next_buffers (decode_live_window (window), next_buffers);
1948 return next_buffers;
1951 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1952 0, 1, 0,
1953 doc: /* Return the parameters of WINDOW and their values.
1954 WINDOW must be a valid window and defaults to the selected one. The
1955 return value is a list of elements of the form (PARAMETER . VALUE). */)
1956 (Lisp_Object window)
1958 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1961 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1962 2, 2, 0,
1963 doc: /* Return WINDOW's value for PARAMETER.
1964 WINDOW can be any window and defaults to the selected one. */)
1965 (Lisp_Object window, Lisp_Object parameter)
1967 Lisp_Object result;
1969 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1970 return CDR_SAFE (result);
1973 DEFUN ("set-window-parameter", Fset_window_parameter,
1974 Sset_window_parameter, 3, 3, 0,
1975 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1976 WINDOW can be any window and defaults to the selected one.
1977 Return VALUE. */)
1978 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1980 register struct window *w = decode_any_window (window);
1981 Lisp_Object old_alist_elt;
1983 old_alist_elt = Fassq (parameter, w->window_parameters);
1984 if (NILP (old_alist_elt))
1985 wset_window_parameters
1986 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1987 else
1988 Fsetcdr (old_alist_elt, value);
1989 return value;
1992 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1993 0, 1, 0,
1994 doc: /* Return the display-table that WINDOW is using.
1995 WINDOW must be a live window and defaults to the selected one. */)
1996 (Lisp_Object window)
1998 return decode_live_window (window)->display_table;
2001 /* Get the display table for use on window W. This is either W's
2002 display table or W's buffer's display table. Ignore the specified
2003 tables if they are not valid; if no valid table is specified,
2004 return 0. */
2006 struct Lisp_Char_Table *
2007 window_display_table (struct window *w)
2009 struct Lisp_Char_Table *dp = NULL;
2011 if (DISP_TABLE_P (w->display_table))
2012 dp = XCHAR_TABLE (w->display_table);
2013 else if (BUFFERP (w->contents))
2015 struct buffer *b = XBUFFER (w->contents);
2017 if (DISP_TABLE_P (BVAR (b, display_table)))
2018 dp = XCHAR_TABLE (BVAR (b, display_table));
2019 else if (DISP_TABLE_P (Vstandard_display_table))
2020 dp = XCHAR_TABLE (Vstandard_display_table);
2023 return dp;
2026 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2027 doc: /* Set WINDOW's display-table to TABLE.
2028 WINDOW must be a live window and defaults to the selected one. */)
2029 (register Lisp_Object window, Lisp_Object table)
2031 wset_display_table (decode_live_window (window), table);
2032 return table;
2035 /* Record info on buffer window W is displaying
2036 when it is about to cease to display that buffer. */
2037 static void
2038 unshow_buffer (register struct window *w)
2040 Lisp_Object buf = w->contents;
2041 struct buffer *b = XBUFFER (buf);
2043 eassert (b == XMARKER (w->pointm)->buffer);
2045 #if false
2046 if (w == XWINDOW (selected_window)
2047 || ! EQ (buf, XWINDOW (selected_window)->contents))
2048 /* Do this except when the selected window's buffer
2049 is being removed from some other window. */
2050 #endif
2051 /* last_window_start records the start position that this buffer
2052 had in the last window to be disconnected from it.
2053 Now that this statement is unconditional,
2054 it is possible for the buffer to be displayed in the
2055 selected window, while last_window_start reflects another
2056 window which was recently showing the same buffer.
2057 Some people might say that might be a good thing. Let's see. */
2058 b->last_window_start = marker_position (w->start);
2060 /* Point in the selected window's buffer
2061 is actually stored in that buffer, and the window's pointm isn't used.
2062 So don't clobber point in that buffer. */
2063 if (! EQ (buf, XWINDOW (selected_window)->contents)
2064 /* Don't clobber point in current buffer either (this could be
2065 useful in connection with bug#12208).
2066 && XBUFFER (buf) != current_buffer */
2067 /* This line helps to fix Horsley's testbug.el bug. */
2068 && !(WINDOWP (BVAR (b, last_selected_window))
2069 && w != XWINDOW (BVAR (b, last_selected_window))
2070 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2071 temp_set_point_both (b,
2072 clip_to_bounds (BUF_BEGV (b),
2073 marker_position (w->pointm),
2074 BUF_ZV (b)),
2075 clip_to_bounds (BUF_BEGV_BYTE (b),
2076 marker_byte_position (w->pointm),
2077 BUF_ZV_BYTE (b)));
2079 if (WINDOWP (BVAR (b, last_selected_window))
2080 && w == XWINDOW (BVAR (b, last_selected_window)))
2081 bset_last_selected_window (b, Qnil);
2084 /* Put NEW into the window structure in place of OLD. SETFLAG false
2085 means change window structure only. Otherwise store geometry and
2086 other settings as well. */
2087 static void
2088 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2090 Lisp_Object tem;
2091 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2093 /* If OLD is its frame's root window, then NEW is the new
2094 root window for that frame. */
2095 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2096 fset_root_window (XFRAME (o->frame), new);
2098 if (setflag)
2100 n->pixel_left = o->pixel_left;
2101 n->pixel_top = o->pixel_top;
2102 n->pixel_width = o->pixel_width;
2103 n->pixel_height = o->pixel_height;
2104 n->left_col = o->left_col;
2105 n->top_line = o->top_line;
2106 n->total_cols = o->total_cols;
2107 n->total_lines = o->total_lines;
2108 wset_normal_cols (n, o->normal_cols);
2109 wset_normal_cols (o, make_float (1.0));
2110 wset_normal_lines (n, o->normal_lines);
2111 wset_normal_lines (o, make_float (1.0));
2112 n->desired_matrix = n->current_matrix = 0;
2113 n->vscroll = 0;
2114 memset (&n->cursor, 0, sizeof (n->cursor));
2115 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2116 n->last_cursor_vpos = 0;
2117 #ifdef HAVE_WINDOW_SYSTEM
2118 n->phys_cursor_type = NO_CURSOR;
2119 n->phys_cursor_width = -1;
2120 #endif
2121 n->must_be_updated_p = false;
2122 n->pseudo_window_p = false;
2123 n->window_end_vpos = 0;
2124 n->window_end_pos = 0;
2125 n->window_end_valid = false;
2128 tem = o->next;
2129 wset_next (n, tem);
2130 if (!NILP (tem))
2131 wset_prev (XWINDOW (tem), new);
2133 tem = o->prev;
2134 wset_prev (n, tem);
2135 if (!NILP (tem))
2136 wset_next (XWINDOW (tem), new);
2138 tem = o->parent;
2139 wset_parent (n, tem);
2140 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2141 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2144 /* If window WINDOW and its parent window are iso-combined, merge
2145 WINDOW's children into those of its parent window and mark WINDOW as
2146 deleted. */
2148 static void
2149 recombine_windows (Lisp_Object window)
2151 struct window *w, *p, *c;
2152 Lisp_Object parent, child;
2153 bool horflag;
2155 w = XWINDOW (window);
2156 parent = w->parent;
2157 if (!NILP (parent) && NILP (w->combination_limit))
2159 p = XWINDOW (parent);
2160 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2161 && p->horizontal == w->horizontal)
2162 /* WINDOW and PARENT are both either a vertical or a horizontal
2163 combination. */
2165 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2166 child = w->contents;
2167 c = XWINDOW (child);
2169 /* Splice WINDOW's children into its parent's children and
2170 assign new normal sizes. */
2171 if (NILP (w->prev))
2172 wset_combination (p, horflag, child);
2173 else
2175 wset_prev (c, w->prev);
2176 wset_next (XWINDOW (w->prev), child);
2179 while (c)
2181 wset_parent (c, parent);
2183 if (horflag)
2184 wset_normal_cols
2185 (c, make_float ((double) c->pixel_width
2186 / (double) p->pixel_width));
2187 else
2188 wset_normal_lines
2189 (c, make_float ((double) c->pixel_height
2190 / (double) p->pixel_height));
2192 if (NILP (c->next))
2194 if (!NILP (w->next))
2196 wset_next (c, w->next);
2197 wset_prev (XWINDOW (c->next), child);
2200 c = 0;
2202 else
2204 child = c->next;
2205 c = XWINDOW (child);
2209 /* WINDOW can be deleted now. */
2210 wset_combination (w, false, Qnil);
2215 /* If WINDOW can be deleted, delete it. */
2216 static void
2217 delete_deletable_window (Lisp_Object window)
2219 if (!NILP (call1 (Qwindow_deletable_p, window)))
2220 call1 (Qdelete_window, window);
2223 /***********************************************************************
2224 Window List
2225 ***********************************************************************/
2227 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2228 pointer. This is a callback function for foreach_window, used in
2229 the window_list function. */
2231 static bool
2232 add_window_to_list (struct window *w, void *user_data)
2234 Lisp_Object *list = user_data;
2235 Lisp_Object window;
2236 XSETWINDOW (window, w);
2237 *list = Fcons (window, *list);
2238 return true;
2242 /* Return a list of all windows, for use by next_window. If
2243 Vwindow_list is a list, return that list. Otherwise, build a new
2244 list, cache it in Vwindow_list, and return that. */
2246 Lisp_Object
2247 window_list (void)
2249 if (!CONSP (Vwindow_list))
2251 Lisp_Object tail, frame;
2253 Vwindow_list = Qnil;
2254 FOR_EACH_FRAME (tail, frame)
2256 Lisp_Object arglist = Qnil;
2258 /* We are visiting windows in canonical order, and add
2259 new windows at the front of args[1], which means we
2260 have to reverse this list at the end. */
2261 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2262 arglist = Fnreverse (arglist);
2263 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2267 return Vwindow_list;
2271 /* Value is true if WINDOW satisfies the constraints given by
2272 OWINDOW, MINIBUF and ALL_FRAMES.
2274 MINIBUF t means WINDOW may be minibuffer windows.
2275 `lambda' means WINDOW may not be a minibuffer window.
2276 a window means a specific minibuffer window
2278 ALL_FRAMES t means search all frames,
2279 nil means search just current frame,
2280 `visible' means search just visible frames on the
2281 current terminal,
2282 0 means search visible and iconified frames on the
2283 current terminal,
2284 a window means search the frame that window belongs to,
2285 a frame means consider windows on that frame, only. */
2287 static bool
2288 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2289 Lisp_Object minibuf, Lisp_Object all_frames)
2291 struct window *w = XWINDOW (window);
2292 struct frame *f = XFRAME (w->frame);
2293 bool candidate_p = true;
2295 if (!BUFFERP (w->contents))
2296 candidate_p = false;
2297 else if (MINI_WINDOW_P (w)
2298 && (EQ (minibuf, Qlambda)
2299 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2301 /* If MINIBUF is `lambda' don't consider any mini-windows.
2302 If it is a window, consider only that one. */
2303 candidate_p = false;
2305 else if (EQ (all_frames, Qt))
2306 candidate_p = true;
2307 else if (NILP (all_frames))
2309 eassert (WINDOWP (owindow));
2310 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2312 else if (EQ (all_frames, Qvisible))
2314 candidate_p = FRAME_VISIBLE_P (f)
2315 && (FRAME_TERMINAL (XFRAME (w->frame))
2316 == FRAME_TERMINAL (XFRAME (selected_frame)));
2319 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2321 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2322 #ifdef HAVE_X_WINDOWS
2323 /* Yuck!! If we've just created the frame and the
2324 window-manager requested the user to place it
2325 manually, the window may still not be considered
2326 `visible'. I'd argue it should be at least
2327 something like `iconified', but don't know how to do
2328 that yet. --Stef */
2329 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2330 && !f->output_data.x->has_been_visible)
2331 #endif
2333 && (FRAME_TERMINAL (XFRAME (w->frame))
2334 == FRAME_TERMINAL (XFRAME (selected_frame)));
2336 else if (WINDOWP (all_frames))
2337 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2338 || EQ (XWINDOW (all_frames)->frame, w->frame)
2339 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2340 else if (FRAMEP (all_frames))
2341 candidate_p = EQ (all_frames, w->frame);
2343 return candidate_p;
2347 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2348 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2349 MINIBUF, and ALL_FRAMES. */
2351 static void
2352 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2354 struct window *w = decode_live_window (*window);
2356 XSETWINDOW (*window, w);
2357 /* MINIBUF nil may or may not include minibuffers. Decide if it
2358 does. */
2359 if (NILP (*minibuf))
2360 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2361 else if (!EQ (*minibuf, Qt))
2362 *minibuf = Qlambda;
2364 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2365 => count none of them, or a specific minibuffer window (the
2366 active one) to count. */
2368 /* ALL_FRAMES nil doesn't specify which frames to include. */
2369 if (NILP (*all_frames))
2370 *all_frames
2371 = (!EQ (*minibuf, Qlambda)
2372 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2373 : Qnil);
2374 else if (EQ (*all_frames, Qvisible))
2376 else if (EQ (*all_frames, make_number (0)))
2378 else if (FRAMEP (*all_frames))
2380 else if (!EQ (*all_frames, Qt))
2381 *all_frames = Qnil;
2385 /* Return the next or previous window of WINDOW in cyclic ordering
2386 of windows. NEXT_P means return the next window. See the
2387 documentation string of next-window for the meaning of MINIBUF and
2388 ALL_FRAMES. */
2390 static Lisp_Object
2391 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2392 bool next_p)
2394 decode_next_window_args (&window, &minibuf, &all_frames);
2396 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2397 return the first window on the frame. */
2398 if (FRAMEP (all_frames)
2399 && !EQ (all_frames, XWINDOW (window)->frame))
2400 return Fframe_first_window (all_frames);
2402 if (next_p)
2404 Lisp_Object list;
2406 /* Find WINDOW in the list of all windows. */
2407 list = Fmemq (window, window_list ());
2409 /* Scan forward from WINDOW to the end of the window list. */
2410 if (CONSP (list))
2411 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2412 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2413 break;
2415 /* Scan from the start of the window list up to WINDOW. */
2416 if (!CONSP (list))
2417 for (list = Vwindow_list;
2418 CONSP (list) && !EQ (XCAR (list), window);
2419 list = XCDR (list))
2420 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2421 break;
2423 if (CONSP (list))
2424 window = XCAR (list);
2426 else
2428 Lisp_Object candidate, list;
2430 /* Scan through the list of windows for candidates. If there are
2431 candidate windows in front of WINDOW, the last one of these
2432 is the one we want. If there are candidates following WINDOW
2433 in the list, again the last one of these is the one we want. */
2434 candidate = Qnil;
2435 for (list = window_list (); CONSP (list); list = XCDR (list))
2437 if (EQ (XCAR (list), window))
2439 if (WINDOWP (candidate))
2440 break;
2442 else if (candidate_window_p (XCAR (list), window, minibuf,
2443 all_frames))
2444 candidate = XCAR (list);
2447 if (WINDOWP (candidate))
2448 window = candidate;
2451 return window;
2455 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2456 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2457 WINDOW must be a live window and defaults to the selected one. The
2458 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2459 consider.
2461 MINIBUF nil or omitted means consider the minibuffer window only if the
2462 minibuffer is active. MINIBUF t means consider the minibuffer window
2463 even if the minibuffer is not active. Any other value means do not
2464 consider the minibuffer window even if the minibuffer is active.
2466 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2467 plus the minibuffer window if specified by the MINIBUF argument. If the
2468 minibuffer counts, consider all windows on all frames that share that
2469 minibuffer too. The following non-nil values of ALL-FRAMES have special
2470 meanings:
2472 - t means consider all windows on all existing frames.
2474 - `visible' means consider all windows on all visible frames.
2476 - 0 (the number zero) means consider all windows on all visible and
2477 iconified frames.
2479 - A frame means consider all windows on that frame only.
2481 Anything else means consider all windows on WINDOW's frame and no
2482 others.
2484 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2485 `next-window' to iterate through the entire cycle of acceptable
2486 windows, eventually ending up back at the window you started with.
2487 `previous-window' traverses the same cycle, in the reverse order. */)
2488 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2490 return next_window (window, minibuf, all_frames, true);
2494 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2495 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2496 WINDOW must be a live window and defaults to the selected one. The
2497 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2498 consider.
2500 MINIBUF nil or omitted means consider the minibuffer window only if the
2501 minibuffer is active. MINIBUF t means consider the minibuffer window
2502 even if the minibuffer is not active. Any other value means do not
2503 consider the minibuffer window even if the minibuffer is active.
2505 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2506 plus the minibuffer window if specified by the MINIBUF argument. If the
2507 minibuffer counts, consider all windows on all frames that share that
2508 minibuffer too. The following non-nil values of ALL-FRAMES have special
2509 meanings:
2511 - t means consider all windows on all existing frames.
2513 - `visible' means consider all windows on all visible frames.
2515 - 0 (the number zero) means consider all windows on all visible and
2516 iconified frames.
2518 - A frame means consider all windows on that frame only.
2520 Anything else means consider all windows on WINDOW's frame and no
2521 others.
2523 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2524 use `previous-window' to iterate through the entire cycle of
2525 acceptable windows, eventually ending up back at the window you
2526 started with. `next-window' traverses the same cycle, in the
2527 reverse order. */)
2528 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2530 return next_window (window, minibuf, all_frames, false);
2534 /* Return a list of windows in cyclic ordering. Arguments are like
2535 for `next-window'. */
2537 static Lisp_Object
2538 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2540 Lisp_Object tail, list, rest;
2542 decode_next_window_args (&window, &minibuf, &all_frames);
2543 list = Qnil;
2545 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2546 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2547 list = Fcons (XCAR (tail), list);
2549 /* Rotate the list to start with WINDOW. */
2550 list = Fnreverse (list);
2551 rest = Fmemq (window, list);
2552 if (!NILP (rest) && !EQ (rest, list))
2554 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2556 XSETCDR (tail, Qnil);
2557 list = nconc2 (rest, list);
2559 return list;
2563 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2564 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2565 FRAME nil or omitted means use the selected frame.
2566 WINDOW nil or omitted means use the window selected within FRAME.
2567 MINIBUF t means include the minibuffer window, even if it isn't active.
2568 MINIBUF nil or omitted means include the minibuffer window only
2569 if it's active.
2570 MINIBUF neither nil nor t means never include the minibuffer window. */)
2571 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2573 if (NILP (window))
2574 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2575 CHECK_WINDOW (window);
2576 if (NILP (frame))
2577 frame = selected_frame;
2579 if (!EQ (frame, XWINDOW (window)->frame))
2580 error ("Window is on a different frame");
2582 return window_list_1 (window, minibuf, frame);
2586 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2587 doc: /* Return a list of all live windows.
2588 WINDOW specifies the first window to list and defaults to the selected
2589 window.
2591 Optional argument MINIBUF nil or omitted means consider the minibuffer
2592 window only if the minibuffer is active. MINIBUF t means consider the
2593 minibuffer window even if the minibuffer is not active. Any other value
2594 means do not consider the minibuffer window even if the minibuffer is
2595 active.
2597 Optional argument ALL-FRAMES nil or omitted means consider all windows
2598 on WINDOW's frame, plus the minibuffer window if specified by the
2599 MINIBUF argument. If the minibuffer counts, consider all windows on all
2600 frames that share that minibuffer too. The following non-nil values of
2601 ALL-FRAMES have special meanings:
2603 - t means consider all windows on all existing frames.
2605 - `visible' means consider all windows on all visible frames.
2607 - 0 (the number zero) means consider all windows on all visible and
2608 iconified frames.
2610 - A frame means consider all windows on that frame only.
2612 Anything else means consider all windows on WINDOW's frame and no
2613 others.
2615 If WINDOW is not on the list of windows returned, some other window will
2616 be listed first but no error is signaled. */)
2617 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2619 return window_list_1 (window, minibuf, all_frames);
2622 /* Look at all windows, performing an operation specified by TYPE
2623 with argument OBJ.
2624 If FRAMES is Qt, look at all frames;
2625 Qnil, look at just the selected frame;
2626 Qvisible, look at visible frames;
2627 a frame, just look at windows on that frame.
2628 If MINI, perform the operation on minibuffer windows too. */
2630 enum window_loop
2632 WINDOW_LOOP_UNUSED,
2633 GET_BUFFER_WINDOW, /* Arg is buffer */
2634 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2635 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2636 CHECK_ALL_WINDOWS /* Arg is ignored */
2639 static Lisp_Object
2640 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2641 Lisp_Object frames)
2643 Lisp_Object window, windows, best_window, frame_arg;
2644 bool frame_best_window_flag = false;
2645 struct frame *f;
2647 /* If we're only looping through windows on a particular frame,
2648 frame points to that frame. If we're looping through windows
2649 on all frames, frame is 0. */
2650 if (FRAMEP (frames))
2651 f = XFRAME (frames);
2652 else if (NILP (frames))
2653 f = SELECTED_FRAME ();
2654 else
2655 f = NULL;
2657 if (f)
2658 frame_arg = Qlambda;
2659 else if (EQ (frames, make_number (0)))
2660 frame_arg = frames;
2661 else if (EQ (frames, Qvisible))
2662 frame_arg = frames;
2663 else
2664 frame_arg = Qt;
2666 /* frame_arg is Qlambda to stick to one frame,
2667 Qvisible to consider all visible frames,
2668 or Qt otherwise. */
2670 /* Pick a window to start with. */
2671 if (WINDOWP (obj))
2672 window = obj;
2673 else if (f)
2674 window = FRAME_SELECTED_WINDOW (f);
2675 else
2676 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2678 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2679 best_window = Qnil;
2681 for (; CONSP (windows); windows = XCDR (windows))
2683 struct window *w;
2685 window = XCAR (windows);
2686 w = XWINDOW (window);
2688 /* Note that we do not pay attention here to whether the frame
2689 is visible, since Fwindow_list skips non-visible frames if
2690 that is desired, under the control of frame_arg. */
2691 if (!MINI_WINDOW_P (w)
2692 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2693 consider all windows. */
2694 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2695 || (mini && minibuf_level > 0))
2696 switch (type)
2698 case GET_BUFFER_WINDOW:
2699 if (EQ (w->contents, obj)
2700 /* Don't find any minibuffer window except the one that
2701 is currently in use. */
2702 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2704 if (EQ (window, selected_window))
2705 /* Preferably return the selected window. */
2706 return window;
2707 else if (EQ (XWINDOW (window)->frame, selected_frame)
2708 && !frame_best_window_flag)
2709 /* Prefer windows on the current frame (but don't
2710 choose another one if we have one already). */
2712 best_window = window;
2713 frame_best_window_flag = true;
2715 else if (NILP (best_window))
2716 best_window = window;
2718 break;
2720 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2721 /* We could simply check whether the buffer shown by window
2722 is live, and show another buffer in case it isn't. */
2723 if (EQ (w->contents, obj))
2725 /* Undedicate WINDOW. */
2726 wset_dedicated (w, Qnil);
2727 /* Make WINDOW show the buffer returned by
2728 other_buffer_safely, don't run any hooks. */
2729 set_window_buffer
2730 (window, other_buffer_safely (w->contents), false, false);
2731 /* If WINDOW is the selected window, make its buffer
2732 current. But do so only if the window shows the
2733 current buffer (Bug#6454). */
2734 if (EQ (window, selected_window)
2735 && XBUFFER (w->contents) == current_buffer)
2736 Fset_buffer (w->contents);
2738 break;
2740 case REDISPLAY_BUFFER_WINDOWS:
2741 if (EQ (w->contents, obj))
2743 mark_window_display_accurate (window, false);
2744 w->update_mode_line = true;
2745 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2746 update_mode_lines = 27;
2747 best_window = window;
2749 break;
2751 /* Check for a leaf window that has a killed buffer
2752 or broken markers. */
2753 case CHECK_ALL_WINDOWS:
2754 if (BUFFERP (w->contents))
2756 struct buffer *b = XBUFFER (w->contents);
2758 if (!BUFFER_LIVE_P (b))
2759 emacs_abort ();
2760 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2761 emacs_abort ();
2762 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2763 emacs_abort ();
2765 break;
2767 case WINDOW_LOOP_UNUSED:
2768 break;
2772 return best_window;
2775 /* Used for debugging. Abort if any window has a dead buffer. */
2777 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2778 void
2779 check_all_windows (void)
2781 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2784 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2785 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2786 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2787 the current buffer.
2789 The optional argument ALL-FRAMES specifies the frames to consider:
2791 - t means consider all windows on all existing frames.
2793 - `visible' means consider all windows on all visible frames.
2795 - 0 (the number zero) means consider all windows on all visible
2796 and iconified frames.
2798 - A frame means consider all windows on that frame only.
2800 Any other value of ALL-FRAMES means consider all windows on the
2801 selected frame and no others. */)
2802 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2804 Lisp_Object buffer;
2806 if (NILP (buffer_or_name))
2807 buffer = Fcurrent_buffer ();
2808 else
2809 buffer = Fget_buffer (buffer_or_name);
2811 if (BUFFERP (buffer))
2812 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2813 else
2814 return Qnil;
2818 static Lisp_Object
2819 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2821 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2824 /* Placeholder used by temacs -nw before window.el is loaded. */
2825 DEFUN ("window--sanitize-window-sizes", Fwindow__sanitize_window_sizes,
2826 Swindow__sanitize_window_sizes, 2, 2, 0,
2827 doc: /* */
2828 attributes: const)
2829 (Lisp_Object frame, Lisp_Object horizontal)
2831 return Qnil;
2834 Lisp_Object
2835 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
2837 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
2841 static Lisp_Object
2842 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2844 return call2 (Qwindow_pixel_to_total, frame, horizontal);
2848 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2849 Sdelete_other_windows_internal, 0, 2, "",
2850 doc: /* Make WINDOW fill its frame.
2851 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2852 and defaults to the selected one.
2854 Optional argument ROOT, if non-nil, must specify an internal window such
2855 that WINDOW is in its window subtree. If this is the case, replace ROOT
2856 by WINDOW and leave alone any windows not part of ROOT's subtree.
2858 When WINDOW is live try to reduce display jumps by keeping the text
2859 previously visible in WINDOW in the same place on the frame. Doing this
2860 depends on the value of (window-start WINDOW), so if calling this
2861 function in a program gives strange scrolling, make sure the
2862 window-start value is reasonable when this function is called. */)
2863 (Lisp_Object window, Lisp_Object root)
2865 struct window *w, *r, *s;
2866 struct frame *f;
2867 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2868 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2869 int top IF_LINT (= 0), new_top;
2871 w = decode_valid_window (window);
2872 XSETWINDOW (window, w);
2873 f = XFRAME (w->frame);
2875 if (NILP (root))
2876 /* ROOT is the frame's root window. */
2878 root = FRAME_ROOT_WINDOW (f);
2879 r = XWINDOW (root);
2881 else
2882 /* ROOT must be an ancestor of WINDOW. */
2884 r = decode_valid_window (root);
2885 pwindow = XWINDOW (window)->parent;
2886 while (!NILP (pwindow))
2887 if (EQ (pwindow, root))
2888 break;
2889 else
2890 pwindow = XWINDOW (pwindow)->parent;
2891 if (!EQ (pwindow, root))
2892 error ("Specified root is not an ancestor of specified window");
2895 if (EQ (window, root))
2896 /* A noop. */
2897 return Qnil;
2898 /* I don't understand the "top > 0" part below. If we deal with a
2899 standalone minibuffer it would have been caught by the preceding
2900 test. */
2901 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2902 error ("Can't expand minibuffer to full frame");
2904 if (BUFFERP (w->contents))
2906 startpos = marker_position (w->start);
2907 startbyte = marker_byte_position (w->start);
2908 top = (WINDOW_TOP_EDGE_LINE (w)
2909 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2910 /* Make sure WINDOW is the frame's selected window. */
2911 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2913 if (EQ (selected_frame, w->frame))
2914 Fselect_window (window, Qnil);
2915 else
2916 fset_selected_window (f, window);
2919 else
2921 /* See if the frame's selected window is a part of the window
2922 subtree rooted at WINDOW, by finding all the selected window's
2923 parents and comparing each one with WINDOW. If it isn't we
2924 need a new selected window for this frame. */
2925 swindow = FRAME_SELECTED_WINDOW (f);
2926 while (true)
2928 pwindow = swindow;
2929 while (!NILP (pwindow) && !EQ (window, pwindow))
2930 pwindow = XWINDOW (pwindow)->parent;
2932 if (EQ (window, pwindow))
2933 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2934 as the new selected window. */
2935 break;
2936 else
2937 /* Else try the previous window of SWINDOW. */
2938 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2941 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2943 if (EQ (selected_frame, w->frame))
2944 Fselect_window (swindow, Qnil);
2945 else
2946 fset_selected_window (f, swindow);
2950 block_input ();
2951 if (!FRAME_INITIAL_P (f))
2953 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2955 /* We are going to free the glyph matrices of WINDOW, and with
2956 that we might lose any information about glyph rows that have
2957 some of their glyphs highlighted in mouse face. (These rows
2958 are marked with a mouse_face_p flag.) If WINDOW
2959 indeed has some glyphs highlighted in mouse face, signal to
2960 frame's up-to-date hook that mouse highlight was overwritten,
2961 so that it will arrange for redisplaying the highlight. */
2962 if (EQ (hlinfo->mouse_face_window, window))
2963 reset_mouse_highlight (hlinfo);
2965 free_window_matrices (r);
2967 fset_redisplay (f);
2968 Vwindow_list = Qnil;
2969 FRAME_WINDOW_SIZES_CHANGED (f) = true;
2970 bool resize_failed = false;
2972 if (!WINDOW_LEAF_P (w))
2974 /* Resize child windows vertically. */
2975 XSETINT (delta, r->pixel_height - w->pixel_height);
2976 w->pixel_top = r->pixel_top;
2977 w->top_line = r->top_line;
2978 resize_root_window (window, delta, Qnil, Qnil, Qt);
2979 if (window_resize_check (w, false))
2981 window_resize_apply (w, false);
2982 window_pixel_to_total (w->frame, Qnil);
2984 else
2986 resize_root_window (window, delta, Qnil, Qt, Qt);
2987 if (window_resize_check (w, false))
2989 window_resize_apply (w, false);
2990 window_pixel_to_total (w->frame, Qnil);
2992 else
2993 resize_failed = true;
2996 /* Resize child windows horizontally. */
2997 if (!resize_failed)
2999 w->left_col = r->left_col;
3000 w->pixel_left = r->pixel_left;
3001 XSETINT (delta, r->pixel_width - w->pixel_width);
3002 resize_root_window (window, delta, Qt, Qnil, Qt);
3003 if (window_resize_check (w, true))
3005 window_resize_apply (w, true);
3006 window_pixel_to_total (w->frame, Qt);
3008 else
3010 resize_root_window (window, delta, Qt, Qt, Qt);
3011 if (window_resize_check (w, true))
3013 window_resize_apply (w, true);
3014 window_pixel_to_total (w->frame, Qt);
3016 else
3017 resize_failed = true;
3021 if (resize_failed)
3022 /* Play safe, if we still can ... */
3024 window = swindow;
3025 w = XWINDOW (window);
3029 /* Cleanly unlink WINDOW from window-tree. */
3030 if (!NILP (w->prev))
3031 /* Get SIBLING above (on the left of) WINDOW. */
3033 sibling = w->prev;
3034 s = XWINDOW (sibling);
3035 wset_next (s, w->next);
3036 if (!NILP (s->next))
3037 wset_prev (XWINDOW (s->next), sibling);
3039 else
3040 /* Get SIBLING below (on the right of) WINDOW. */
3042 sibling = w->next;
3043 s = XWINDOW (sibling);
3044 wset_prev (s, Qnil);
3045 wset_combination (XWINDOW (w->parent),
3046 XWINDOW (w->parent)->horizontal, sibling);
3049 /* Delete ROOT and all child windows of ROOT. */
3050 if (WINDOWP (r->contents))
3052 delete_all_child_windows (r->contents);
3053 wset_combination (r, false, Qnil);
3056 replace_window (root, window, true);
3058 /* This must become SWINDOW anyway ....... */
3059 if (BUFFERP (w->contents) && !resize_failed)
3061 /* Try to minimize scrolling, by setting the window start to the
3062 point will cause the text at the old window start to be at the
3063 same place on the frame. But don't try to do this if the
3064 window start is outside the visible portion (as might happen
3065 when the display is not current, due to typeahead). */
3066 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3067 if (new_top != top
3068 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3069 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3071 struct position pos;
3072 struct buffer *obuf = current_buffer;
3074 Fset_buffer (w->contents);
3075 /* This computation used to temporarily move point, but that
3076 can have unwanted side effects due to text properties. */
3077 pos = *vmotion (startpos, startbyte, -top, w);
3079 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3080 w->window_end_valid = false;
3081 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3082 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3083 /* We need to do this, so that the window-scroll-functions
3084 get called. */
3085 w->optional_new_start = true;
3087 set_buffer_internal (obuf);
3091 adjust_frame_glyphs (f);
3092 unblock_input ();
3094 run_window_configuration_change_hook (f);
3096 return Qnil;
3100 void
3101 replace_buffer_in_windows (Lisp_Object buffer)
3103 call1 (Qreplace_buffer_in_windows, buffer);
3106 /* If BUFFER is shown in a window, safely replace it with some other
3107 buffer in all windows of all frames, even those on other keyboards. */
3109 void
3110 replace_buffer_in_windows_safely (Lisp_Object buffer)
3112 if (buffer_window_count (XBUFFER (buffer)))
3114 Lisp_Object tail, frame;
3116 /* A single call to window_loop won't do the job because it only
3117 considers frames on the current keyboard. So loop manually over
3118 frames, and handle each one. */
3119 FOR_EACH_FRAME (tail, frame)
3120 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3124 /* The following three routines are needed for running a window's
3125 configuration change hook. */
3126 static void
3127 run_funs (Lisp_Object funs)
3129 for (; CONSP (funs); funs = XCDR (funs))
3130 if (!EQ (XCAR (funs), Qt))
3131 call0 (XCAR (funs));
3134 static void
3135 select_window_norecord (Lisp_Object window)
3137 if (WINDOW_LIVE_P (window))
3138 Fselect_window (window, Qt);
3141 static void
3142 select_frame_norecord (Lisp_Object frame)
3144 if (FRAME_LIVE_P (XFRAME (frame)))
3145 Fselect_frame (frame, Qt);
3148 void
3149 run_window_configuration_change_hook (struct frame *f)
3151 ptrdiff_t count = SPECPDL_INDEX ();
3152 Lisp_Object frame, global_wcch
3153 = Fdefault_value (Qwindow_configuration_change_hook);
3154 XSETFRAME (frame, f);
3156 if (NILP (Vrun_hooks)
3157 || !(f->can_x_set_window_size)
3158 || !(f->after_make_frame))
3159 return;
3161 /* Use the right buffer. Matters when running the local hooks. */
3162 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3164 record_unwind_current_buffer ();
3165 Fset_buffer (Fwindow_buffer (Qnil));
3168 if (SELECTED_FRAME () != f)
3170 record_unwind_protect (select_frame_norecord, selected_frame);
3171 select_frame_norecord (frame);
3174 /* Look for buffer-local values. */
3176 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3177 for (; CONSP (windows); windows = XCDR (windows))
3179 Lisp_Object window = XCAR (windows);
3180 Lisp_Object buffer = Fwindow_buffer (window);
3181 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3182 buffer)))
3184 ptrdiff_t inner_count = SPECPDL_INDEX ();
3185 record_unwind_protect (select_window_norecord, selected_window);
3186 select_window_norecord (window);
3187 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3188 buffer));
3189 unbind_to (inner_count, Qnil);
3194 run_funs (global_wcch);
3195 unbind_to (count, Qnil);
3198 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3199 Srun_window_configuration_change_hook, 0, 1, 0,
3200 doc: /* Run `window-configuration-change-hook' for FRAME.
3201 If FRAME is omitted or nil, it defaults to the selected frame. */)
3202 (Lisp_Object frame)
3204 run_window_configuration_change_hook (decode_live_frame (frame));
3205 return Qnil;
3208 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3209 Srun_window_scroll_functions, 0, 1, 0,
3210 doc: /* Run `window-scroll-functions' for WINDOW.
3211 If WINDOW is omitted or nil, it defaults to the selected window. */)
3212 (Lisp_Object window)
3214 if (! NILP (Vwindow_scroll_functions))
3215 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3216 Fmarker_position (decode_live_window (window)->start));
3217 return Qnil;
3220 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3221 to run hooks. See make_frame for a case where it's not allowed.
3222 KEEP_MARGINS_P means that the current margins, fringes, and
3223 scroll-bar settings of the window are not reset from the buffer's
3224 local settings. */
3226 void
3227 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3228 bool run_hooks_p, bool keep_margins_p)
3230 struct window *w = XWINDOW (window);
3231 struct buffer *b = XBUFFER (buffer);
3232 ptrdiff_t count = SPECPDL_INDEX ();
3233 bool samebuf = EQ (buffer, w->contents);
3235 wset_buffer (w, buffer);
3237 if (EQ (window, selected_window))
3238 bset_last_selected_window (b, window);
3240 /* Let redisplay errors through. */
3241 b->display_error_modiff = 0;
3243 /* Update time stamps of buffer display. */
3244 if (INTEGERP (BVAR (b, display_count)))
3245 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3246 bset_display_time (b, Fcurrent_time ());
3248 w->window_end_pos = 0;
3249 w->window_end_vpos = 0;
3250 w->last_cursor_vpos = 0;
3252 if (!(keep_margins_p && samebuf))
3253 { /* If we're not actually changing the buffer, don't reset hscroll
3254 and vscroll. This case happens for example when called from
3255 change_frame_size_1, where we use a dummy call to
3256 Fset_window_buffer on the frame's selected window (and no
3257 other) just in order to run window-configuration-change-hook
3258 (no longer true since change_frame_size_1 directly calls
3259 run_window_configuration_change_hook). Resetting hscroll and
3260 vscroll here is problematic for things like image-mode and
3261 doc-view-mode since it resets the image's position whenever we
3262 resize the frame. */
3263 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3264 w->suspend_auto_hscroll = false;
3265 w->vscroll = 0;
3266 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3267 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3268 set_marker_restricted (w->start,
3269 make_number (b->last_window_start),
3270 buffer);
3271 w->start_at_line_beg = false;
3272 w->force_start = false;
3274 /* Maybe we could move this into the `if' but it's not obviously safe and
3275 I doubt it's worth the trouble. */
3276 wset_redisplay (w);
3277 w->update_mode_line = true;
3279 /* We must select BUFFER to run the window-scroll-functions and to look up
3280 the buffer-local value of Vwindow_point_insertion_type. */
3281 record_unwind_current_buffer ();
3282 Fset_buffer (buffer);
3284 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3285 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3287 if (!keep_margins_p)
3289 /* Set left and right marginal area width etc. from buffer. */
3290 set_window_fringes (w, BVAR (b, left_fringe_width),
3291 BVAR (b, right_fringe_width),
3292 BVAR (b, fringes_outside_margins));
3293 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3294 BVAR (b, vertical_scroll_bar_type),
3295 BVAR (b, scroll_bar_height),
3296 BVAR (b, horizontal_scroll_bar_type));
3297 set_window_margins (w, BVAR (b, left_margin_cols),
3298 BVAR (b, right_margin_cols));
3299 apply_window_adjustment (w);
3302 if (run_hooks_p)
3304 if (! NILP (Vwindow_scroll_functions))
3305 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3306 Fmarker_position (w->start));
3307 if (!samebuf)
3308 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3311 unbind_to (count, Qnil);
3314 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3315 doc: /* Make WINDOW display BUFFER-OR-NAME.
3316 WINDOW must be a live window and defaults to the selected one.
3317 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3319 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3320 display margins, fringe widths, and scroll bar settings are preserved;
3321 the default is to reset these from the local settings for BUFFER-OR-NAME
3322 or the frame defaults. Return nil.
3324 This function throws an error when WINDOW is strongly dedicated to its
3325 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3326 already display BUFFER-OR-NAME.
3328 This function runs `window-scroll-functions' before running
3329 `window-configuration-change-hook'. */)
3330 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3332 register Lisp_Object tem, buffer;
3333 register struct window *w = decode_live_window (window);
3335 XSETWINDOW (window, w);
3336 buffer = Fget_buffer (buffer_or_name);
3337 CHECK_BUFFER (buffer);
3338 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3339 error ("Attempt to display deleted buffer");
3341 tem = w->contents;
3342 if (NILP (tem))
3343 error ("Window is deleted");
3344 else
3346 if (!EQ (tem, buffer))
3348 if (EQ (w->dedicated, Qt))
3349 /* WINDOW is strongly dedicated to its buffer, signal an
3350 error. */
3351 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3352 else
3353 /* WINDOW is weakly dedicated to its buffer, reset
3354 dedication. */
3355 wset_dedicated (w, Qnil);
3357 call1 (Qrecord_window_buffer, window);
3360 unshow_buffer (w);
3363 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3365 return Qnil;
3368 static Lisp_Object
3369 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3371 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3374 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3375 0, 1, 0,
3376 doc: /* Force all windows to be updated on next redisplay.
3377 If optional arg OBJECT is a window, force redisplay of that window only.
3378 If OBJECT is a buffer or buffer name, force redisplay of all windows
3379 displaying that buffer. */)
3380 (Lisp_Object object)
3382 if (NILP (object))
3384 windows_or_buffers_changed = 29;
3385 update_mode_lines = 28;
3386 return Qt;
3389 if (WINDOWP (object))
3391 struct window *w = XWINDOW (object);
3392 mark_window_display_accurate (object, false);
3393 w->update_mode_line = true;
3394 if (BUFFERP (w->contents))
3395 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3396 update_mode_lines = 29;
3397 return Qt;
3400 if (STRINGP (object))
3401 object = Fget_buffer (object);
3402 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3403 && buffer_window_count (XBUFFER (object)))
3405 /* If buffer is live and shown in at least one window, find
3406 all windows showing this buffer and force update of them. */
3407 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3408 return NILP (object) ? Qnil : Qt;
3411 /* If nothing suitable was found, just return.
3412 We could signal an error, but this feature will typically be used
3413 asynchronously in timers or process sentinels, so we don't. */
3414 return Qnil;
3417 /* Obsolete since 24.3. */
3418 void
3419 temp_output_buffer_show (register Lisp_Object buf)
3421 register struct buffer *old = current_buffer;
3422 register Lisp_Object window;
3423 register struct window *w;
3425 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3427 Fset_buffer (buf);
3428 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3429 BEGV = BEG;
3430 ZV = Z;
3431 SET_PT (BEG);
3432 set_buffer_internal (old);
3434 if (!NILP (Vtemp_buffer_show_function))
3435 call1 (Vtemp_buffer_show_function, buf);
3436 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3438 if (!EQ (XWINDOW (window)->frame, selected_frame))
3439 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3440 Vminibuf_scroll_window = window;
3441 w = XWINDOW (window);
3442 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3443 w->suspend_auto_hscroll = false;
3444 set_marker_restricted_both (w->start, buf, BEG, BEG);
3445 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3446 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3448 /* Run temp-buffer-show-hook, with the chosen window selected
3449 and its buffer current. */
3451 ptrdiff_t count = SPECPDL_INDEX ();
3452 Lisp_Object prev_window, prev_buffer;
3453 prev_window = selected_window;
3454 XSETBUFFER (prev_buffer, old);
3456 /* Select the window that was chosen, for running the hook.
3457 Note: Both Fselect_window and select_window_norecord may
3458 set-buffer to the buffer displayed in the window,
3459 so we need to save the current buffer. --stef */
3460 record_unwind_protect (restore_buffer, prev_buffer);
3461 record_unwind_protect (select_window_norecord, prev_window);
3462 Fselect_window (window, Qt);
3463 Fset_buffer (w->contents);
3464 run_hook (Qtemp_buffer_show_hook);
3465 unbind_to (count, Qnil);
3470 /* Allocate basically initialized window. */
3472 static struct window *
3473 allocate_window (void)
3475 return ALLOCATE_ZEROED_PSEUDOVECTOR
3476 (struct window, current_matrix, PVEC_WINDOW);
3479 /* Make new window, have it replace WINDOW in window-tree, and make
3480 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3481 horizontal child). */
3482 static void
3483 make_parent_window (Lisp_Object window, bool horflag)
3485 Lisp_Object parent;
3486 register struct window *o, *p;
3488 o = XWINDOW (window);
3489 p = allocate_window ();
3490 memcpy ((char *) p + sizeof (struct vectorlike_header),
3491 (char *) o + sizeof (struct vectorlike_header),
3492 word_size * VECSIZE (struct window));
3493 /* P's buffer slot may change from nil to a buffer... */
3494 adjust_window_count (p, 1);
3495 XSETWINDOW (parent, p);
3497 p->sequence_number = ++sequence_number;
3499 replace_window (window, parent, true);
3501 wset_next (o, Qnil);
3502 wset_prev (o, Qnil);
3503 wset_parent (o, parent);
3504 /* ...but now P becomes an internal window. */
3505 wset_start (p, Qnil);
3506 wset_pointm (p, Qnil);
3507 wset_old_pointm (p, Qnil);
3508 wset_buffer (p, Qnil);
3509 wset_combination (p, horflag, window);
3510 wset_combination_limit (p, Qnil);
3511 wset_window_parameters (p, Qnil);
3514 /* Make new window from scratch. */
3515 Lisp_Object
3516 make_window (void)
3518 Lisp_Object window;
3519 register struct window *w;
3521 w = allocate_window ();
3522 /* Initialize Lisp data. Note that allocate_window initializes all
3523 Lisp data to nil, so do it only for slots which should not be nil. */
3524 wset_normal_lines (w, make_float (1.0));
3525 wset_normal_cols (w, make_float (1.0));
3526 wset_new_total (w, make_number (0));
3527 wset_new_normal (w, make_number (0));
3528 wset_new_pixel (w, make_number (0));
3529 wset_start (w, Fmake_marker ());
3530 wset_pointm (w, Fmake_marker ());
3531 wset_old_pointm (w, Fmake_marker ());
3532 wset_vertical_scroll_bar_type (w, Qt);
3533 wset_horizontal_scroll_bar_type (w, Qt);
3534 /* These Lisp fields are marked specially so they're not set to nil by
3535 allocate_window. */
3536 wset_prev_buffers (w, Qnil);
3537 wset_next_buffers (w, Qnil);
3539 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3540 non-Lisp data, so do it only for slots which should not be zero. */
3541 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3542 w->left_fringe_width = w->right_fringe_width = -1;
3543 w->mode_line_height = w->header_line_height = -1;
3544 #ifdef HAVE_WINDOW_SYSTEM
3545 w->phys_cursor_type = NO_CURSOR;
3546 w->phys_cursor_width = -1;
3547 #endif
3548 w->sequence_number = ++sequence_number;
3549 w->scroll_bar_width = -1;
3550 w->scroll_bar_height = -1;
3551 w->column_number_displayed = -1;
3552 /* Reset window_list. */
3553 Vwindow_list = Qnil;
3554 /* Return window. */
3555 XSETWINDOW (window, w);
3556 return window;
3559 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3560 doc: /* Set new pixel size of WINDOW to SIZE.
3561 WINDOW must be a valid window and defaults to the selected one.
3562 Return SIZE.
3564 Optional argument ADD non-nil means add SIZE to the new pixel size of
3565 WINDOW and return the sum.
3567 The new pixel size of WINDOW, if valid, will be shortly installed as
3568 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3569 `window-pixel-width').
3571 Note: This function does not operate on any child windows of WINDOW. */)
3572 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3574 struct window *w = decode_valid_window (window);
3575 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3576 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3578 CHECK_RANGED_INTEGER (size, size_min, size_max);
3579 if (NILP (add))
3580 wset_new_pixel (w, size);
3581 else
3582 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3584 return w->new_pixel;
3587 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3588 doc: /* Set new total size of WINDOW to SIZE.
3589 WINDOW must be a valid window and defaults to the selected one.
3590 Return SIZE.
3592 Optional argument ADD non-nil means add SIZE to the new total size of
3593 WINDOW and return the sum.
3595 The new total size of WINDOW, if valid, will be shortly installed as
3596 WINDOW's total height (see `window-total-height') or total width (see
3597 `window-total-width').
3599 Note: This function does not operate on any child windows of WINDOW. */)
3600 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3602 struct window *w = decode_valid_window (window);
3604 CHECK_NUMBER (size);
3605 if (NILP (add))
3606 wset_new_total (w, size);
3607 else
3608 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3610 return w->new_total;
3613 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3614 doc: /* Set new normal size of WINDOW to SIZE.
3615 WINDOW must be a valid window and defaults to the selected one.
3616 Return SIZE.
3618 The new normal size of WINDOW, if valid, will be shortly installed as
3619 WINDOW's normal size (see `window-normal-size').
3621 Note: This function does not operate on any child windows of WINDOW. */)
3622 (Lisp_Object window, Lisp_Object size)
3624 wset_new_normal (decode_valid_window (window), size);
3625 return size;
3628 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3629 to w->new_pixel would result in correct heights (widths)
3630 for window W and recursively all child windows of W.
3632 Note: This function does not check any of `window-fixed-size-p',
3633 `window-min-height' or `window-min-width'. It does check that window
3634 sizes do not drop below one line (two columns). */
3635 static bool
3636 window_resize_check (struct window *w, bool horflag)
3638 struct frame *f = XFRAME (w->frame);
3639 struct window *c;
3641 if (WINDOW_VERTICAL_COMBINATION_P (w))
3642 /* W is a vertical combination. */
3644 c = XWINDOW (w->contents);
3645 if (horflag)
3646 /* All child windows of W must have the same width as W. */
3648 while (c)
3650 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3651 || !window_resize_check (c, horflag))
3652 return false;
3654 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3657 return true;
3659 else
3660 /* The sum of the heights of the child windows of W must equal
3661 W's height. */
3663 int remaining_pixels = XINT (w->new_pixel);
3665 while (c)
3667 if (!window_resize_check (c, horflag))
3668 return false;
3670 remaining_pixels -= XINT (c->new_pixel);
3671 if (remaining_pixels < 0)
3672 return false;
3673 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3676 return remaining_pixels == 0;
3679 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3680 /* W is a horizontal combination. */
3682 c = XWINDOW (w->contents);
3683 if (horflag)
3684 /* The sum of the widths of the child windows of W must equal W's
3685 width. */
3687 int remaining_pixels = XINT (w->new_pixel);
3689 while (c)
3691 if (!window_resize_check (c, horflag))
3692 return false;
3694 remaining_pixels -= XINT (c->new_pixel);
3695 if (remaining_pixels < 0)
3696 return false;
3697 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3700 return remaining_pixels == 0;
3702 else
3703 /* All child windows of W must have the same height as W. */
3705 while (c)
3707 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3708 || !window_resize_check (c, horflag))
3709 return false;
3711 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3714 return true;
3717 else
3718 /* A leaf window. Make sure it's not too small. The following
3719 hardcodes the values of `window-safe-min-width' (2) and
3720 `window-safe-min-height' (1) which are defined in window.el. */
3721 return (XINT (w->new_pixel) >= (horflag
3722 ? (2 * FRAME_COLUMN_WIDTH (f))
3723 : FRAME_LINE_HEIGHT (f)));
3727 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3728 w->new_pixel for window W and recursively all child windows of W.
3729 Also calculate and assign the new vertical (horizontal) pixel start
3730 positions of each of these windows.
3732 This function does not perform any error checks. Make sure you have
3733 run window_resize_check on W before applying this function. */
3734 static void
3735 window_resize_apply (struct window *w, bool horflag)
3737 struct window *c;
3738 int edge;
3739 int unit = (horflag
3740 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3741 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3743 /* Note: Assigning new_normal requires that the new total size of the
3744 parent window has been set *before*. */
3745 if (horflag)
3747 w->pixel_width = XFASTINT (w->new_pixel);
3748 w->total_cols = w->pixel_width / unit;
3749 if (NUMBERP (w->new_normal))
3750 wset_normal_cols (w, w->new_normal);
3752 edge = w->pixel_left;
3754 else
3756 w->pixel_height = XFASTINT (w->new_pixel);
3757 w->total_lines = w->pixel_height / unit;
3758 if (NUMBERP (w->new_normal))
3759 wset_normal_lines (w, w->new_normal);
3761 edge = w->pixel_top;
3764 if (WINDOW_VERTICAL_COMBINATION_P (w))
3765 /* W is a vertical combination. */
3767 c = XWINDOW (w->contents);
3768 while (c)
3770 if (horflag)
3772 c->pixel_left = edge;
3773 c->left_col = edge / unit;
3775 else
3777 c->pixel_top = edge;
3778 c->top_line = edge / unit;
3780 window_resize_apply (c, horflag);
3781 if (!horflag)
3782 edge = edge + c->pixel_height;
3784 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3787 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3788 /* W is a horizontal combination. */
3790 c = XWINDOW (w->contents);
3791 while (c)
3793 if (horflag)
3795 c->pixel_left = edge;
3796 c->left_col = edge / unit;
3798 else
3800 c->pixel_top = edge;
3801 c->top_line = edge / unit;
3804 window_resize_apply (c, horflag);
3805 if (horflag)
3806 edge = edge + c->pixel_width;
3808 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3811 else
3812 /* Bug#15957. */
3813 w->window_end_valid = false;
3817 /* Set w->total_lines (w->total_cols if HORFLAG) to
3818 w->new_total for window W and recursively all child windows of W.
3819 Also calculate and assign the new vertical (horizontal) start
3820 positions of each of these windows. */
3821 static void
3822 window_resize_apply_total (struct window *w, bool horflag)
3824 struct window *c;
3825 int edge;
3827 /* Note: Assigning new_normal requires that the new total size of the
3828 parent window has been set *before*. */
3829 if (horflag)
3831 w->total_cols = XFASTINT (w->new_total);
3832 edge = w->left_col;
3834 else
3836 w->total_lines = XFASTINT (w->new_total);
3837 edge = w->top_line;
3840 if (WINDOW_VERTICAL_COMBINATION_P (w))
3841 /* W is a vertical combination. */
3843 c = XWINDOW (w->contents);
3844 while (c)
3846 if (horflag)
3847 c->left_col = edge;
3848 else
3849 c->top_line = edge;
3851 window_resize_apply_total (c, horflag);
3852 if (!horflag)
3853 edge = edge + c->total_lines;
3855 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3858 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3859 /* W is a horizontal combination. */
3861 c = XWINDOW (w->contents);
3862 while (c)
3864 if (horflag)
3865 c->left_col = edge;
3866 else
3867 c->top_line = edge;
3869 window_resize_apply_total (c, horflag);
3870 if (horflag)
3871 edge = edge + c->total_cols;
3873 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3878 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3879 doc: /* Apply requested size values for window-tree of FRAME.
3880 If FRAME is omitted or nil, it defaults to the selected frame.
3882 Optional argument HORIZONTAL omitted or nil means apply requested
3883 height values. HORIZONTAL non-nil means apply requested width values.
3885 The requested size values are those set by `set-window-new-pixel' and
3886 `set-window-new-normal'. This function checks whether the requested
3887 values sum up to a valid window layout, recursively assigns the new
3888 sizes of all child windows and calculates and assigns the new start
3889 positions of these windows.
3891 Return t if the requested values have been applied correctly, nil
3892 otherwise.
3894 Note: This function does not check any of `window-fixed-size-p',
3895 `window-min-height' or `window-min-width'. All these checks have to
3896 be applied on the Elisp level. */)
3897 (Lisp_Object frame, Lisp_Object horizontal)
3899 struct frame *f = decode_live_frame (frame);
3900 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3901 bool horflag = !NILP (horizontal);
3903 if (!window_resize_check (r, horflag)
3904 || (XINT (r->new_pixel)
3905 != (horflag ? r->pixel_width : r->pixel_height)))
3906 return Qnil;
3908 block_input ();
3909 window_resize_apply (r, horflag);
3911 fset_redisplay (f);
3912 FRAME_WINDOW_SIZES_CHANGED (f) = true;
3914 adjust_frame_glyphs (f);
3915 unblock_input ();
3917 return Qt;
3921 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
3922 doc: /* Apply requested total size values for window-tree of FRAME.
3923 If FRAME is omitted or nil, it defaults to the selected frame.
3925 This function does not assign pixel or normal size values. You should
3926 have run `window-resize-apply' before running this.
3928 Optional argument HORIZONTAL omitted or nil means apply requested
3929 height values. HORIZONTAL non-nil means apply requested width
3930 values. */)
3931 (Lisp_Object frame, Lisp_Object horizontal)
3933 struct frame *f = decode_live_frame (frame);
3934 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3936 block_input ();
3937 /* Necessary when deleting the top-/or leftmost window. */
3938 r->left_col = 0;
3939 r->top_line = FRAME_TOP_MARGIN (f);
3940 window_resize_apply_total (r, !NILP (horizontal));
3941 /* Handle the mini window. */
3942 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3944 struct window *m = XWINDOW (f->minibuffer_window);
3946 if (NILP (horizontal))
3948 m->top_line = r->top_line + r->total_lines;
3949 m->total_lines = XFASTINT (m->new_total);
3951 else
3952 m->total_cols = XFASTINT (m->new_total);
3955 unblock_input ();
3957 return Qt;
3961 /* Resize frame F's windows when number of lines of F is set to SIZE.
3962 HORFLAG means resize windows when number of columns of F is set to
3963 SIZE. PIXELWISE means to interpret SIZE as pixels. */
3964 void
3965 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
3967 Lisp_Object root = f->root_window;
3968 struct window *r = XWINDOW (root);
3969 Lisp_Object mini = f->minibuffer_window;
3970 struct window *m;
3971 /* old_size is the old size of the frame's root window. */
3972 int old_size = horflag ? r->total_cols : r->total_lines;
3973 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
3974 int old_pixel_top = r->pixel_top;
3975 /* new_size is the new size of the frame's root window. */
3976 int new_size, new_pixel_size;
3977 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
3979 /* Don't let the size drop below one unit. This is more comforting
3980 when we are called from x_set_tool_bar_lines since the latter may
3981 have implicitly given us a zero or negative height. */
3982 if (pixelwise)
3984 /* Note: This does not include the size for internal borders
3985 since these are not part of the frame's text area. */
3986 new_pixel_size = max (horflag
3987 ? size
3988 : (size
3989 - ((FRAME_HAS_MINIBUF_P (f)
3990 && !FRAME_MINIBUF_ONLY_P (f))
3991 ? FRAME_LINE_HEIGHT (f) : 0)),
3992 unit);
3993 new_size = new_pixel_size / unit;
3995 else
3997 new_size = max (size - (!horflag
3998 && FRAME_HAS_MINIBUF_P (f)
3999 && !FRAME_MINIBUF_ONLY_P (f)),
4001 new_pixel_size = new_size * unit;
4004 r->top_line = FRAME_TOP_MARGIN (f);
4005 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4007 if (new_pixel_size == old_pixel_size
4008 && r->pixel_top == old_pixel_top)
4010 else if (WINDOW_LEAF_P (r))
4011 /* For a leaf root window just set the size. */
4012 if (horflag)
4014 r->total_cols = new_size;
4015 r->pixel_width = new_pixel_size;
4017 else
4019 r->total_lines = new_size;
4020 r->pixel_height = new_pixel_size;
4022 else
4024 Lisp_Object delta;
4026 if (pixelwise)
4027 XSETINT (delta, new_pixel_size - old_pixel_size);
4028 else
4029 XSETINT (delta, new_size - old_size);
4031 /* Try a "normal" resize first. */
4032 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4033 pixelwise ? Qt : Qnil);
4034 if (window_resize_check (r, horflag)
4035 && new_pixel_size == XINT (r->new_pixel))
4037 window_resize_apply (r, horflag);
4038 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4040 else
4042 /* Try with "reasonable" minimum sizes next. */
4043 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4044 pixelwise ? Qt : Qnil);
4045 if (window_resize_check (r, horflag)
4046 && new_pixel_size == XINT (r->new_pixel))
4048 window_resize_apply (r, horflag);
4049 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4050 #if false /* Let's try without safe sizes and/or killing other windows. */
4052 else
4054 /* Finally, try with "safe" minimum sizes. */
4055 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4056 pixelwise ? Qt : Qnil);
4057 if (window_resize_check (r, horflag)
4058 && new_pixel_size == XINT (r->new_pixel))
4060 window_resize_apply (r, horflag);
4061 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4063 else
4065 /* We lost. Delete all windows but the frame's
4066 selected one. */
4067 root = f->selected_window;
4068 Fdelete_other_windows_internal (root, Qnil);
4069 if (horflag)
4071 XWINDOW (root)->total_cols = new_size;
4072 XWINDOW (root)->pixel_width = new_pixel_size;
4074 else
4076 XWINDOW (root)->total_lines = new_size;
4077 XWINDOW (root)->pixel_height = new_pixel_size;
4080 #endif /* false */
4085 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4087 m = XWINDOW (mini);
4088 if (horflag)
4090 m->total_cols = size;
4091 m->pixel_width = new_pixel_size;
4093 else
4095 /* Are we sure we always want 1 line here? */
4096 m->total_lines = 1;
4097 m->pixel_height = FRAME_LINE_HEIGHT (f);
4098 m->top_line = r->top_line + r->total_lines;
4099 m->pixel_top = r->pixel_top + r->pixel_height;
4103 fset_redisplay (f);
4107 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4108 doc: /* Split window OLD.
4109 Second argument PIXEL-SIZE specifies the number of pixels of the
4110 new window. It must be a positive integer.
4112 Third argument SIDE nil (or `below') specifies that the new window shall
4113 be located below WINDOW. SIDE `above' means the new window shall be
4114 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4115 height of the new window including space reserved for the mode and/or
4116 header line.
4118 SIDE t (or `right') specifies that the new window shall be located on
4119 the right side of WINDOW. SIDE `left' means the new window shall be
4120 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4121 width of the new window including space reserved for fringes and the
4122 scrollbar or a divider column.
4124 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4125 according to the SIDE argument.
4127 The new pixel and normal sizes of all involved windows must have been
4128 set correctly. See the code of `split-window' for how this is done. */)
4129 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4131 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4132 parent window or an internal window we have to install as OLD's new
4133 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4134 provided OLD is a leaf window, or to the frame's selected window.
4135 NEW (*n) is the new window created with some parameters taken from
4136 REFERENCE (*r). */
4137 Lisp_Object new, frame, reference;
4138 struct window *o, *p, *n, *r, *c;
4139 struct frame *f;
4140 bool horflag
4141 /* HORFLAG is true when we split side-by-side, false otherwise. */
4142 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4144 CHECK_WINDOW (old);
4145 o = XWINDOW (old);
4146 frame = WINDOW_FRAME (o);
4147 f = XFRAME (frame);
4149 CHECK_NUMBER (pixel_size);
4150 EMACS_INT total_size
4151 = XINT (pixel_size) / (horflag
4152 ? FRAME_COLUMN_WIDTH (f)
4153 : FRAME_LINE_HEIGHT (f));
4155 /* Set combination_limit if we have to make a new parent window.
4156 We do that if either `window-combination-limit' is t, or OLD has no
4157 parent, or OLD is ortho-combined. */
4158 bool combination_limit
4159 = (EQ (Vwindow_combination_limit, Qt)
4160 || NILP (o->parent)
4161 || (horflag
4162 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4163 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4165 /* We need a live reference window to initialize some parameters. */
4166 if (WINDOW_LIVE_P (old))
4167 /* OLD is live, use it as reference window. */
4168 reference = old;
4169 else
4170 /* Use the frame's selected window as reference window. */
4171 reference = FRAME_SELECTED_WINDOW (f);
4172 r = XWINDOW (reference);
4174 /* The following bugs are caught by `split-window'. */
4175 if (MINI_WINDOW_P (o))
4176 error ("Attempt to split minibuffer window");
4177 else if (total_size < (horflag ? 2 : 1))
4178 error ("Size of new window too small (after split)");
4179 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4180 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4181 proportionally. */
4183 p = XWINDOW (o->parent);
4184 /* Temporarily pretend we split the parent window. */
4185 wset_new_pixel
4186 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4187 - XINT (pixel_size)));
4188 if (!window_resize_check (p, horflag))
4189 error ("Window sizes don't fit");
4190 else
4191 /* Undo the temporary pretension. */
4192 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4194 else
4196 if (!window_resize_check (o, horflag))
4197 error ("Resizing old window failed");
4198 else if (XINT (pixel_size) + XINT (o->new_pixel)
4199 != (horflag ? o->pixel_width : o->pixel_height))
4200 error ("Sum of sizes of old and new window don't fit");
4203 /* This is our point of no return. */
4204 if (combination_limit)
4206 /* Save the old value of o->normal_cols/lines. It gets corrupted
4207 by make_parent_window and we need it below for assigning it to
4208 p->new_normal. */
4209 Lisp_Object new_normal
4210 = horflag ? o->normal_cols : o->normal_lines;
4212 make_parent_window (old, horflag);
4213 p = XWINDOW (o->parent);
4214 if (EQ (Vwindow_combination_limit, Qt))
4215 /* Store t in the new parent's combination_limit slot to avoid
4216 that its children get merged into another window. */
4217 wset_combination_limit (p, Qt);
4218 /* These get applied below. */
4219 wset_new_pixel
4220 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4221 wset_new_total
4222 (p, make_number (horflag ? o->total_cols : o->total_lines));
4223 wset_new_normal (p, new_normal);
4225 else
4226 p = XWINDOW (o->parent);
4228 fset_redisplay (f);
4229 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4230 new = make_window ();
4231 n = XWINDOW (new);
4232 wset_frame (n, frame);
4233 wset_parent (n, o->parent);
4235 if (EQ (side, Qabove) || EQ (side, Qleft))
4237 wset_prev (n, o->prev);
4238 if (NILP (n->prev))
4239 wset_combination (p, horflag, new);
4240 else
4241 wset_next (XWINDOW (n->prev), new);
4242 wset_next (n, old);
4243 wset_prev (o, new);
4245 else
4247 wset_next (n, o->next);
4248 if (!NILP (n->next))
4249 wset_prev (XWINDOW (n->next), new);
4250 wset_prev (n, old);
4251 wset_next (o, new);
4254 n->window_end_valid = false;
4255 n->last_cursor_vpos = 0;
4257 /* Get special geometry settings from reference window. */
4258 n->left_margin_cols = r->left_margin_cols;
4259 n->right_margin_cols = r->right_margin_cols;
4260 n->left_fringe_width = r->left_fringe_width;
4261 n->right_fringe_width = r->right_fringe_width;
4262 n->fringes_outside_margins = r->fringes_outside_margins;
4263 n->scroll_bar_width = r->scroll_bar_width;
4264 n->scroll_bar_height = r->scroll_bar_height;
4265 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4266 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4268 /* Directly assign orthogonal coordinates and sizes. */
4269 if (horflag)
4271 n->pixel_top = o->pixel_top;
4272 n->top_line = o->top_line;
4273 n->pixel_height = o->pixel_height;
4274 n->total_lines = o->total_lines;
4276 else
4278 n->pixel_left = o->pixel_left;
4279 n->left_col = o->left_col;
4280 n->pixel_width = o->pixel_width;
4281 n->total_cols = o->total_cols;
4284 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4285 get them ready here. */
4286 wset_new_pixel (n, pixel_size);
4287 EMACS_INT sum = 0;
4288 c = XWINDOW (p->contents);
4289 while (c)
4291 if (c != n)
4292 sum = sum + XINT (c->new_total);
4293 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4295 wset_new_total (n, make_number ((horflag
4296 ? p->total_cols
4297 : p->total_lines)
4298 - sum));
4299 wset_new_normal (n, normal_size);
4301 block_input ();
4302 window_resize_apply (p, horflag);
4303 adjust_frame_glyphs (f);
4304 /* Set buffer of NEW to buffer of reference window. Don't run
4305 any hooks. */
4306 set_window_buffer (new, r->contents, false, true);
4307 unblock_input ();
4309 /* Maybe we should run the scroll functions in Elisp (which already
4310 runs the configuration change hook). */
4311 if (! NILP (Vwindow_scroll_functions))
4312 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4313 Fmarker_position (n->start));
4314 /* Return NEW. */
4315 return new;
4319 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4320 doc: /* Remove WINDOW from its frame.
4321 WINDOW defaults to the selected window. Return nil.
4322 Signal an error when WINDOW is the only window on its frame. */)
4323 (Lisp_Object window)
4325 Lisp_Object parent, sibling, frame, root;
4326 struct window *w, *p, *s, *r;
4327 struct frame *f;
4328 bool horflag, before_sibling = false;
4330 w = decode_any_window (window);
4331 XSETWINDOW (window, w);
4332 if (NILP (w->contents))
4333 /* It's a no-op to delete an already deleted window. */
4334 return Qnil;
4336 parent = w->parent;
4337 if (NILP (parent))
4338 /* Never delete a minibuffer or frame root window. */
4339 error ("Attempt to delete minibuffer or sole ordinary window");
4340 else if (NILP (w->prev) && NILP (w->next))
4341 /* Rather bow out here, this case should be handled on the Elisp
4342 level. */
4343 error ("Attempt to delete sole window of parent");
4345 p = XWINDOW (parent);
4346 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4348 frame = WINDOW_FRAME (w);
4349 f = XFRAME (frame);
4351 root = FRAME_ROOT_WINDOW (f);
4352 r = XWINDOW (root);
4354 /* Unlink WINDOW from window tree. */
4355 if (NILP (w->prev))
4356 /* Get SIBLING below (on the right of) WINDOW. */
4358 /* before_sibling means WINDOW is the first child of its
4359 parent and thus before the sibling. */
4360 before_sibling = true;
4361 sibling = w->next;
4362 s = XWINDOW (sibling);
4363 wset_prev (s, Qnil);
4364 wset_combination (p, horflag, sibling);
4366 else
4367 /* Get SIBLING above (on the left of) WINDOW. */
4369 sibling = w->prev;
4370 s = XWINDOW (sibling);
4371 wset_next (s, w->next);
4372 if (!NILP (s->next))
4373 wset_prev (XWINDOW (s->next), sibling);
4376 if (window_resize_check (r, horflag)
4377 && (XINT (r->new_pixel)
4378 == (horflag ? r->pixel_width : r->pixel_height)))
4379 /* We can delete WINDOW now. */
4382 /* Block input. */
4383 block_input ();
4384 window_resize_apply (p, horflag);
4385 /* If this window is referred to by the dpyinfo's mouse
4386 highlight, invalidate that slot to be safe (Bug#9904). */
4387 if (!FRAME_INITIAL_P (f))
4389 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4391 if (EQ (hlinfo->mouse_face_window, window))
4392 hlinfo->mouse_face_window = Qnil;
4395 fset_redisplay (f);
4396 Vwindow_list = Qnil;
4397 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4399 wset_next (w, Qnil); /* Don't delete w->next too. */
4400 free_window_matrices (w);
4402 if (WINDOWP (w->contents))
4404 delete_all_child_windows (w->contents);
4405 wset_combination (w, false, Qnil);
4407 else
4409 unshow_buffer (w);
4410 unchain_marker (XMARKER (w->pointm));
4411 unchain_marker (XMARKER (w->old_pointm));
4412 unchain_marker (XMARKER (w->start));
4413 wset_buffer (w, Qnil);
4416 if (NILP (s->prev) && NILP (s->next))
4417 /* A matrjoshka where SIBLING has become the only child of
4418 PARENT. */
4420 /* Put SIBLING into PARENT's place. */
4421 replace_window (parent, sibling, false);
4422 /* Have SIBLING inherit the following three slot values from
4423 PARENT (the combination_limit slot is not inherited). */
4424 wset_normal_cols (s, p->normal_cols);
4425 wset_normal_lines (s, p->normal_lines);
4426 /* Mark PARENT as deleted. */
4427 wset_combination (p, false, Qnil);
4428 /* Try to merge SIBLING into its new parent. */
4429 recombine_windows (sibling);
4432 adjust_frame_glyphs (f);
4434 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4435 /* We deleted the frame's selected window. */
4437 /* Use the frame's first window as fallback ... */
4438 Lisp_Object new_selected_window = Fframe_first_window (frame);
4439 /* ... but preferably use its most recently used window. */
4440 Lisp_Object mru_window;
4442 /* `get-mru-window' might fail for some reason so play it safe
4443 - promote the first window _without recording it_ first. */
4444 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4445 Fselect_window (new_selected_window, Qt);
4446 else
4447 fset_selected_window (f, new_selected_window);
4449 unblock_input ();
4451 /* Now look whether `get-mru-window' gets us something. */
4452 mru_window = call1 (Qget_mru_window, frame);
4453 if (WINDOW_LIVE_P (mru_window)
4454 && EQ (XWINDOW (mru_window)->frame, frame))
4455 new_selected_window = mru_window;
4457 /* If all ended up well, we now promote the mru window. */
4458 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4459 Fselect_window (new_selected_window, Qnil);
4460 else
4461 fset_selected_window (f, new_selected_window);
4463 else
4464 unblock_input ();
4466 /* Must be run by the caller:
4467 run_window_configuration_change_hook (f); */
4469 else
4470 /* We failed: Relink WINDOW into window tree. */
4472 if (before_sibling)
4474 wset_prev (s, window);
4475 wset_combination (p, horflag, window);
4477 else
4479 wset_next (s, window);
4480 if (!NILP (w->next))
4481 wset_prev (XWINDOW (w->next), window);
4483 error ("Deletion failed");
4486 return Qnil;
4489 /***********************************************************************
4490 Resizing Mini-Windows
4491 ***********************************************************************/
4493 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4494 can. */
4495 void
4496 grow_mini_window (struct window *w, int delta, bool pixelwise)
4498 struct frame *f = XFRAME (w->frame);
4499 struct window *r;
4500 Lisp_Object root, height;
4501 int line_height, pixel_height;
4503 eassert (MINI_WINDOW_P (w));
4504 eassert (delta >= 0);
4506 if (delta > 0)
4508 root = FRAME_ROOT_WINDOW (f);
4509 r = XWINDOW (root);
4510 height = call3 (Qwindow_resize_root_window_vertically,
4511 root, make_number (- delta), pixelwise ? Qt : Qnil);
4512 if (INTEGERP (height) && window_resize_check (r, false))
4514 block_input ();
4515 window_resize_apply (r, false);
4517 if (pixelwise)
4519 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4520 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4522 else
4524 line_height = min (-XINT (height),
4525 ((INT_MAX - w->pixel_height)
4526 / FRAME_LINE_HEIGHT (f)));
4527 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4530 /* Grow the mini-window. */
4531 w->pixel_top = r->pixel_top + r->pixel_height;
4532 w->top_line = r->top_line + r->total_lines;
4533 /* Make sure the mini-window has always at least one line. */
4534 w->pixel_height = max (w->pixel_height + pixel_height,
4535 FRAME_LINE_HEIGHT (f));
4536 w->total_lines = max (w->total_lines + line_height, 1);
4538 /* Enforce full redisplay of the frame. */
4539 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4540 fset_redisplay (f);
4541 adjust_frame_glyphs (f);
4542 unblock_input ();
4547 /* Shrink mini-window W to one line. */
4548 void
4549 shrink_mini_window (struct window *w, bool pixelwise)
4551 struct frame *f = XFRAME (w->frame);
4552 struct window *r;
4553 Lisp_Object root, delta;
4554 EMACS_INT height, unit;
4556 eassert (MINI_WINDOW_P (w));
4558 height = pixelwise ? w->pixel_height : w->total_lines;
4559 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4560 if (height > unit)
4562 root = FRAME_ROOT_WINDOW (f);
4563 r = XWINDOW (root);
4564 delta = call3 (Qwindow_resize_root_window_vertically,
4565 root, make_number (height - unit),
4566 pixelwise ? Qt : Qnil);
4567 if (INTEGERP (delta) && window_resize_check (r, false))
4569 block_input ();
4570 window_resize_apply (r, false);
4572 /* Shrink the mini-window. */
4573 w->top_line = r->top_line + r->total_lines;
4574 w->total_lines = 1;
4575 w->pixel_top = r->pixel_top + r->pixel_height;
4576 w->pixel_height = FRAME_LINE_HEIGHT (f);
4577 /* Enforce full redisplay of the frame. */
4578 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4579 fset_redisplay (f);
4580 adjust_frame_glyphs (f);
4581 unblock_input ();
4583 /* If the above failed for whatever strange reason we must make a
4584 one window frame here. The same routine will be needed when
4585 shrinking the frame (and probably when making the initial
4586 *scratch* window). For the moment leave things as they are. */
4590 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4591 doc: /* Resize minibuffer window WINDOW. */)
4592 (Lisp_Object window)
4594 struct window *w = XWINDOW (window);
4595 struct window *r;
4596 struct frame *f;
4597 int height;
4599 CHECK_WINDOW (window);
4600 f = XFRAME (w->frame);
4602 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4603 error ("Not a valid minibuffer window");
4604 else if (FRAME_MINIBUF_ONLY_P (f))
4605 error ("Cannot resize a minibuffer-only frame");
4607 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4608 height = r->pixel_height + w->pixel_height;
4609 if (window_resize_check (r, false)
4610 && XINT (w->new_pixel) > 0
4611 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4613 block_input ();
4614 window_resize_apply (r, false);
4616 w->pixel_height = XFASTINT (w->new_pixel);
4617 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4618 w->pixel_top = r->pixel_top + r->pixel_height;
4619 w->top_line = r->top_line + r->total_lines;
4621 fset_redisplay (f);
4622 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4623 adjust_frame_glyphs (f);
4624 unblock_input ();
4625 return Qt;
4627 else
4628 error ("Failed to resize minibuffer window");
4631 /* Mark window cursors off for all windows in the window tree rooted
4632 at W by setting their phys_cursor_on_p flag to zero. Called from
4633 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4634 the frame are cleared. */
4636 void
4637 mark_window_cursors_off (struct window *w)
4639 while (w)
4641 if (WINDOWP (w->contents))
4642 mark_window_cursors_off (XWINDOW (w->contents));
4643 else
4644 w->phys_cursor_on_p = false;
4646 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4651 /* Return number of lines of text (not counting mode lines) in W. */
4654 window_internal_height (struct window *w)
4656 int ht = w->total_lines;
4658 if (!MINI_WINDOW_P (w))
4660 if (!NILP (w->parent)
4661 || WINDOWP (w->contents)
4662 || !NILP (w->next)
4663 || !NILP (w->prev)
4664 || WINDOW_WANTS_MODELINE_P (w))
4665 --ht;
4667 if (WINDOW_WANTS_HEADER_LINE_P (w))
4668 --ht;
4671 return ht;
4675 /************************************************************************
4676 Window Scrolling
4677 ***********************************************************************/
4679 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4680 N screen-fulls, which is defined as the height of the window minus
4681 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4682 instead. Negative values of N mean scroll down. NOERROR
4683 means don't signal an error if we try to move over BEGV or ZV,
4684 respectively. */
4686 static void
4687 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4689 ptrdiff_t count = SPECPDL_INDEX ();
4691 immediate_quit = true;
4692 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4694 wset_redisplay (XWINDOW (window));
4696 if (whole && Vfast_but_imprecise_scrolling)
4697 specbind (Qfontification_functions, Qnil);
4699 /* If we must, use the pixel-based version which is much slower than
4700 the line-based one but can handle varying line heights. */
4701 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4702 window_scroll_pixel_based (window, n, whole, noerror);
4703 else
4704 window_scroll_line_based (window, n, whole, noerror);
4706 unbind_to (count, Qnil);
4708 /* Bug#15957. */
4709 XWINDOW (window)->window_end_valid = false;
4710 immediate_quit = false;
4714 /* Implementation of window_scroll that works based on pixel line
4715 heights. See the comment of window_scroll for parameter
4716 descriptions. */
4718 static void
4719 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4721 struct it it;
4722 struct window *w = XWINDOW (window);
4723 struct text_pos start;
4724 int this_scroll_margin;
4725 /* True if we fiddled the window vscroll field without really scrolling. */
4726 bool vscrolled = false;
4727 int x, y, rtop, rbot, rowh, vpos;
4728 void *itdata = NULL;
4729 int window_total_lines;
4730 int frame_line_height = default_line_pixel_height (w);
4731 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4732 Fwindow_old_point (window)));
4734 SET_TEXT_POS_FROM_MARKER (start, w->start);
4735 /* Scrolling a minibuffer window via scroll bar when the echo area
4736 shows long text sometimes resets the minibuffer contents behind
4737 our backs. */
4738 if (CHARPOS (start) > ZV)
4739 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4741 /* If PT is not visible in WINDOW, move back one half of
4742 the screen. Allow PT to be partially visible, otherwise
4743 something like (scroll-down 1) with PT in the line before
4744 the partially visible one would recenter. */
4746 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4748 itdata = bidi_shelve_cache ();
4749 /* Move backward half the height of the window. Performance note:
4750 vmotion used here is about 10% faster, but would give wrong
4751 results for variable height lines. */
4752 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4753 it.current_y = it.last_visible_y;
4754 move_it_vertically_backward (&it, window_box_height (w) / 2);
4756 /* The function move_iterator_vertically may move over more than
4757 the specified y-distance. If it->w is small, e.g. a
4758 mini-buffer window, we may end up in front of the window's
4759 display area. Start displaying at the start of the line
4760 containing PT in this case. */
4761 if (it.current_y <= 0)
4763 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4764 move_it_vertically_backward (&it, 0);
4765 it.current_y = 0;
4768 start = it.current.pos;
4769 bidi_unshelve_cache (itdata, false);
4771 else if (auto_window_vscroll_p)
4773 if (rtop || rbot) /* Partially visible. */
4775 int px;
4776 int dy = frame_line_height;
4777 /* In the below we divide the window box height by the
4778 frame's line height to make the result predictable when
4779 the window box is not an integral multiple of the line
4780 height. This is important to ensure we get back to the
4781 same position when scrolling up, then down. */
4782 if (whole)
4783 dy = max ((window_box_height (w) / dy
4784 - next_screen_context_lines) * dy,
4785 dy);
4786 dy *= n;
4788 if (n < 0)
4790 /* Only vscroll backwards if already vscrolled forwards. */
4791 if (w->vscroll < 0 && rtop > 0)
4793 px = max (0, -w->vscroll - min (rtop, -dy));
4794 Fset_window_vscroll (window, make_number (px), Qt);
4795 return;
4798 if (n > 0)
4800 /* Do vscroll if already vscrolled or only display line. */
4801 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4803 px = max (0, -w->vscroll + min (rbot, dy));
4804 Fset_window_vscroll (window, make_number (px), Qt);
4805 return;
4808 /* Maybe modify window start instead of scrolling. */
4809 if (rbot > 0 || w->vscroll < 0)
4811 ptrdiff_t spos;
4813 Fset_window_vscroll (window, make_number (0), Qt);
4814 /* If there are other text lines above the current row,
4815 move window start to current row. Else to next row. */
4816 if (rbot > 0)
4817 spos = XINT (Fline_beginning_position (Qnil));
4818 else
4819 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4820 set_marker_restricted (w->start, make_number (spos),
4821 w->contents);
4822 w->start_at_line_beg = true;
4823 w->update_mode_line = true;
4824 /* Set force_start so that redisplay_window will run the
4825 window-scroll-functions. */
4826 w->force_start = true;
4827 return;
4831 /* Cancel previous vscroll. */
4832 Fset_window_vscroll (window, make_number (0), Qt);
4835 itdata = bidi_shelve_cache ();
4836 /* If scroll_preserve_screen_position is non-nil, we try to set
4837 point in the same window line as it is now, so get that line. */
4838 if (!NILP (Vscroll_preserve_screen_position))
4840 /* We preserve the goal pixel coordinate across consecutive
4841 calls to scroll-up, scroll-down and other commands that
4842 have the `scroll-command' property. This avoids the
4843 possibility of point becoming "stuck" on a tall line when
4844 scrolling by one line. */
4845 if (window_scroll_pixel_based_preserve_y < 0
4846 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4847 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4849 start_display (&it, w, start);
4850 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4851 window_scroll_pixel_based_preserve_y = it.current_y;
4852 window_scroll_pixel_based_preserve_x = it.current_x;
4855 else
4856 window_scroll_pixel_based_preserve_y
4857 = window_scroll_pixel_based_preserve_x = -1;
4859 /* Move iterator it from start the specified distance forward or
4860 backward. The result is the new window start. */
4861 start_display (&it, w, start);
4862 if (whole)
4864 ptrdiff_t start_pos = IT_CHARPOS (it);
4865 int dy = frame_line_height;
4866 /* In the below we divide the window box height by the frame's
4867 line height to make the result predictable when the window
4868 box is not an integral multiple of the line height. This is
4869 important to ensure we get back to the same position when
4870 scrolling up, then down. */
4871 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4872 dy) * n;
4874 /* Note that move_it_vertically always moves the iterator to the
4875 start of a line. So, if the last line doesn't have a newline,
4876 we would end up at the start of the line ending at ZV. */
4877 if (dy <= 0)
4879 move_it_vertically_backward (&it, -dy);
4880 /* Ensure we actually do move, e.g. in case we are currently
4881 looking at an image that is taller that the window height. */
4882 while (start_pos == IT_CHARPOS (it)
4883 && start_pos > BEGV)
4884 move_it_by_lines (&it, -1);
4886 else if (dy > 0)
4888 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4889 MOVE_TO_POS | MOVE_TO_Y);
4890 /* Ensure we actually do move, e.g. in case we are currently
4891 looking at an image that is taller that the window height. */
4892 while (start_pos == IT_CHARPOS (it)
4893 && start_pos < ZV)
4894 move_it_by_lines (&it, 1);
4897 else
4898 move_it_by_lines (&it, n);
4900 /* We failed if we find ZV is already on the screen (scrolling up,
4901 means there's nothing past the end), or if we can't start any
4902 earlier (scrolling down, means there's nothing past the top). */
4903 if ((n > 0 && IT_CHARPOS (it) == ZV)
4904 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4906 if (IT_CHARPOS (it) == ZV)
4908 if (it.current_y < it.last_visible_y
4909 && (it.current_y + it.max_ascent + it.max_descent
4910 > it.last_visible_y))
4912 /* The last line was only partially visible, make it fully
4913 visible. */
4914 w->vscroll = (it.last_visible_y
4915 - it.current_y + it.max_ascent + it.max_descent);
4916 adjust_frame_glyphs (it.f);
4918 else
4920 bidi_unshelve_cache (itdata, false);
4921 if (noerror)
4922 return;
4923 else if (n < 0) /* could happen with empty buffers */
4924 xsignal0 (Qbeginning_of_buffer);
4925 else
4926 xsignal0 (Qend_of_buffer);
4929 else
4931 if (w->vscroll != 0)
4932 /* The first line was only partially visible, make it fully
4933 visible. */
4934 w->vscroll = 0;
4935 else
4937 bidi_unshelve_cache (itdata, false);
4938 if (noerror)
4939 return;
4940 else
4941 xsignal0 (Qbeginning_of_buffer);
4945 /* If control gets here, then we vscrolled. */
4947 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
4949 /* Don't try to change the window start below. */
4950 vscrolled = true;
4953 if (! vscrolled)
4955 ptrdiff_t pos = IT_CHARPOS (it);
4956 ptrdiff_t bytepos;
4958 /* If in the middle of a multi-glyph character move forward to
4959 the next character. */
4960 if (in_display_vector_p (&it))
4962 ++pos;
4963 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4966 /* Set the window start, and set up the window for redisplay. */
4967 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4968 IT_BYTEPOS (it));
4969 bytepos = marker_byte_position (w->start);
4970 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4971 w->update_mode_line = true;
4972 /* Set force_start so that redisplay_window will run the
4973 window-scroll-functions. */
4974 w->force_start = true;
4977 /* The rest of this function uses current_y in a nonstandard way,
4978 not including the height of the header line if any. */
4979 it.current_y = it.vpos = 0;
4981 /* Move PT out of scroll margins.
4982 This code wants current_y to be zero at the window start position
4983 even if there is a header line. */
4984 window_total_lines
4985 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4986 this_scroll_margin = max (0, scroll_margin);
4987 this_scroll_margin
4988 = min (this_scroll_margin, window_total_lines / 4);
4989 this_scroll_margin *= frame_line_height;
4991 if (n > 0)
4993 /* We moved the window start towards ZV, so PT may be now
4994 in the scroll margin at the top. */
4995 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4996 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4997 && (NILP (Vscroll_preserve_screen_position)
4998 || EQ (Vscroll_preserve_screen_position, Qt)))
4999 /* We found PT at a legitimate height. Leave it alone. */
5001 else if (window_scroll_pixel_based_preserve_y >= 0)
5003 /* If we have a header line, take account of it.
5004 This is necessary because we set it.current_y to 0, above. */
5005 move_it_to (&it, -1,
5006 window_scroll_pixel_based_preserve_x,
5007 (window_scroll_pixel_based_preserve_y
5008 - WINDOW_WANTS_HEADER_LINE_P (w)),
5009 -1, MOVE_TO_Y | MOVE_TO_X);
5010 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5012 else
5014 while (it.current_y < this_scroll_margin)
5016 int prev = it.current_y;
5017 move_it_by_lines (&it, 1);
5018 if (prev == it.current_y)
5019 break;
5021 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5024 else if (n < 0)
5026 ptrdiff_t charpos, bytepos;
5027 bool partial_p;
5029 /* Save our position, for the
5030 window_scroll_pixel_based_preserve_y case. */
5031 charpos = IT_CHARPOS (it);
5032 bytepos = IT_BYTEPOS (it);
5034 /* We moved the window start towards BEGV, so PT may be now
5035 in the scroll margin at the bottom. */
5036 move_it_to (&it, PT, -1,
5037 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5038 - this_scroll_margin - 1),
5040 MOVE_TO_POS | MOVE_TO_Y);
5042 /* Save our position, in case it's correct. */
5043 charpos = IT_CHARPOS (it);
5044 bytepos = IT_BYTEPOS (it);
5046 /* If PT is in the screen line at the last fully visible line,
5047 move_it_to will stop at X = 0 in that line, because the
5048 required Y coordinate is reached there. See if we can get to
5049 PT without descending lower in Y, and if we can, it means we
5050 reached PT before the scroll margin. */
5051 if (charpos != PT)
5053 struct it it2;
5054 void *it_data;
5056 it2 = it;
5057 it_data = bidi_shelve_cache ();
5058 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5059 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5061 charpos = IT_CHARPOS (it);
5062 bytepos = IT_BYTEPOS (it);
5063 bidi_unshelve_cache (it_data, true);
5065 else
5067 it = it2;
5068 bidi_unshelve_cache (it_data, false);
5072 /* See if point is on a partially visible line at the end. */
5073 if (it.what == IT_EOB)
5074 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5075 else
5077 move_it_by_lines (&it, 1);
5078 partial_p = it.current_y > it.last_visible_y;
5081 if (charpos == PT && !partial_p
5082 && (NILP (Vscroll_preserve_screen_position)
5083 || EQ (Vscroll_preserve_screen_position, Qt)))
5084 /* We found PT before we found the display margin, so PT is ok. */
5086 else if (window_scroll_pixel_based_preserve_y >= 0)
5088 SET_TEXT_POS_FROM_MARKER (start, w->start);
5089 start_display (&it, w, start);
5090 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5091 here because we called start_display again and did not
5092 alter it.current_y this time. */
5093 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5094 window_scroll_pixel_based_preserve_y, -1,
5095 MOVE_TO_Y | MOVE_TO_X);
5096 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5098 else
5100 if (partial_p)
5101 /* The last line was only partially visible, so back up two
5102 lines to make sure we're on a fully visible line. */
5104 move_it_by_lines (&it, -2);
5105 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5107 else
5108 /* No, the position we saved is OK, so use it. */
5109 SET_PT_BOTH (charpos, bytepos);
5112 bidi_unshelve_cache (itdata, false);
5114 if (adjust_old_pointm)
5115 Fset_marker (w->old_pointm,
5116 ((w == XWINDOW (selected_window))
5117 ? make_number (BUF_PT (XBUFFER (w->contents)))
5118 : Fmarker_position (w->pointm)),
5119 w->contents);
5123 /* Implementation of window_scroll that works based on screen lines.
5124 See the comment of window_scroll for parameter descriptions. */
5126 static void
5127 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5129 struct window *w = XWINDOW (window);
5130 /* Fvertical_motion enters redisplay, which can trigger
5131 fontification, which in turn can modify buffer text (e.g., if the
5132 fontification functions replace escape sequences with faces, as
5133 in `grep-mode-font-lock-keywords'). So we use a marker to record
5134 the old point position, to prevent crashes in SET_PT_BOTH. */
5135 Lisp_Object opoint_marker = Fpoint_marker ();
5136 register ptrdiff_t pos, pos_byte;
5137 register int ht = window_internal_height (w);
5138 register Lisp_Object tem;
5139 bool lose;
5140 Lisp_Object bolp;
5141 ptrdiff_t startpos = marker_position (w->start);
5142 ptrdiff_t startbyte = marker_byte_position (w->start);
5143 Lisp_Object original_pos = Qnil;
5144 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5145 Fwindow_old_point (window)));
5147 /* If scrolling screen-fulls, compute the number of lines to
5148 scroll from the window's height. */
5149 if (whole)
5150 n *= max (1, ht - next_screen_context_lines);
5152 if (!NILP (Vscroll_preserve_screen_position))
5154 if (window_scroll_preserve_vpos <= 0
5155 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5156 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5158 struct position posit
5159 = *compute_motion (startpos, startbyte, 0, 0, false,
5160 PT, ht, 0, -1, w->hscroll, 0, w);
5162 window_scroll_preserve_vpos = posit.vpos;
5163 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5166 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5167 make_number (window_scroll_preserve_vpos));
5170 XSETFASTINT (tem, PT);
5171 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5173 if (NILP (tem))
5175 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5176 startpos = PT;
5177 startbyte = PT_BYTE;
5180 SET_PT_BOTH (startpos, startbyte);
5181 lose = n < 0 && PT == BEGV;
5182 Fvertical_motion (make_number (n), window, Qnil);
5183 pos = PT;
5184 pos_byte = PT_BYTE;
5185 bolp = Fbolp ();
5186 SET_PT_BOTH (marker_position (opoint_marker),
5187 marker_byte_position (opoint_marker));
5189 if (lose)
5191 if (noerror)
5192 return;
5193 else
5194 xsignal0 (Qbeginning_of_buffer);
5197 if (pos < ZV)
5199 /* Don't use a scroll margin that is negative or too large. */
5200 int this_scroll_margin =
5201 max (0, min (scroll_margin, w->total_lines / 4));
5203 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5204 w->start_at_line_beg = !NILP (bolp);
5205 w->update_mode_line = true;
5206 /* Set force_start so that redisplay_window will run
5207 the window-scroll-functions. */
5208 w->force_start = true;
5210 if (!NILP (Vscroll_preserve_screen_position)
5211 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5213 SET_PT_BOTH (pos, pos_byte);
5214 Fvertical_motion (original_pos, window, Qnil);
5216 /* If we scrolled forward, put point enough lines down
5217 that it is outside the scroll margin. */
5218 else if (n > 0)
5220 int top_margin;
5222 if (this_scroll_margin > 0)
5224 SET_PT_BOTH (pos, pos_byte);
5225 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5226 top_margin = PT;
5228 else
5229 top_margin = pos;
5231 if (top_margin <= marker_position (opoint_marker))
5232 SET_PT_BOTH (marker_position (opoint_marker),
5233 marker_byte_position (opoint_marker));
5234 else if (!NILP (Vscroll_preserve_screen_position))
5236 SET_PT_BOTH (pos, pos_byte);
5237 Fvertical_motion (original_pos, window, Qnil);
5239 else
5240 SET_PT (top_margin);
5242 else if (n < 0)
5244 int bottom_margin;
5246 /* If we scrolled backward, put point near the end of the window
5247 but not within the scroll margin. */
5248 SET_PT_BOTH (pos, pos_byte);
5249 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5250 Qnil);
5251 if (XFASTINT (tem) == ht - this_scroll_margin)
5252 bottom_margin = PT;
5253 else
5254 bottom_margin = PT + 1;
5256 if (bottom_margin > marker_position (opoint_marker))
5257 SET_PT_BOTH (marker_position (opoint_marker),
5258 marker_byte_position (opoint_marker));
5259 else
5261 if (!NILP (Vscroll_preserve_screen_position))
5263 SET_PT_BOTH (pos, pos_byte);
5264 Fvertical_motion (original_pos, window, Qnil);
5266 else
5267 Fvertical_motion (make_number (-1), window, Qnil);
5271 else
5273 if (noerror)
5274 return;
5275 else
5276 xsignal0 (Qend_of_buffer);
5279 if (adjust_old_pointm)
5280 Fset_marker (w->old_pointm,
5281 ((w == XWINDOW (selected_window))
5282 ? make_number (BUF_PT (XBUFFER (w->contents)))
5283 : Fmarker_position (w->pointm)),
5284 w->contents);
5288 /* Scroll selected_window up or down. If N is nil, scroll a
5289 screen-full which is defined as the height of the window minus
5290 next_screen_context_lines. If N is the symbol `-', scroll.
5291 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5292 up. This is the guts of Fscroll_up and Fscroll_down. */
5294 static void
5295 scroll_command (Lisp_Object n, int direction)
5297 ptrdiff_t count = SPECPDL_INDEX ();
5299 eassert (eabs (direction) == 1);
5301 /* If selected window's buffer isn't current, make it current for
5302 the moment. But don't screw up if window_scroll gets an error. */
5303 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5305 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5306 Fset_buffer (XWINDOW (selected_window)->contents);
5309 if (NILP (n))
5310 window_scroll (selected_window, direction, true, false);
5311 else if (EQ (n, Qminus))
5312 window_scroll (selected_window, -direction, true, false);
5313 else
5315 n = Fprefix_numeric_value (n);
5316 window_scroll (selected_window, XINT (n) * direction, false, false);
5319 unbind_to (count, Qnil);
5322 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5323 doc: /* Scroll text of selected window upward ARG lines.
5324 If ARG is omitted or nil, scroll upward by a near full screen.
5325 A near full screen is `next-screen-context-lines' less than a full screen.
5326 Negative ARG means scroll downward.
5327 If ARG is the atom `-', scroll downward by nearly full screen.
5328 When calling from a program, supply as argument a number, nil, or `-'. */)
5329 (Lisp_Object arg)
5331 scroll_command (arg, 1);
5332 return Qnil;
5335 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5336 doc: /* Scroll text of selected window down ARG lines.
5337 If ARG is omitted or nil, scroll down by a near full screen.
5338 A near full screen is `next-screen-context-lines' less than a full screen.
5339 Negative ARG means scroll upward.
5340 If ARG is the atom `-', scroll upward by nearly full screen.
5341 When calling from a program, supply as argument a number, nil, or `-'. */)
5342 (Lisp_Object arg)
5344 scroll_command (arg, -1);
5345 return Qnil;
5348 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5349 doc: /* Return the other window for \"other window scroll\" commands.
5350 If `other-window-scroll-buffer' is non-nil, a window
5351 showing that buffer is used.
5352 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5353 specifies the window. This takes precedence over
5354 `other-window-scroll-buffer'. */)
5355 (void)
5357 Lisp_Object window;
5359 if (MINI_WINDOW_P (XWINDOW (selected_window))
5360 && !NILP (Vminibuf_scroll_window))
5361 window = Vminibuf_scroll_window;
5362 /* If buffer is specified and live, scroll that buffer. */
5363 else if (!NILP (Vother_window_scroll_buffer)
5364 && BUFFERP (Vother_window_scroll_buffer)
5365 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5367 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5368 if (NILP (window))
5369 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5371 else
5373 /* Nothing specified; look for a neighboring window on the same
5374 frame. */
5375 window = Fnext_window (selected_window, Qnil, Qnil);
5377 if (EQ (window, selected_window))
5378 /* That didn't get us anywhere; look for a window on another
5379 visible frame. */
5381 window = Fnext_window (window, Qnil, Qt);
5382 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5383 && ! EQ (window, selected_window));
5386 CHECK_LIVE_WINDOW (window);
5388 if (EQ (window, selected_window))
5389 error ("There is no other window");
5391 return window;
5394 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5395 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5396 A near full screen is `next-screen-context-lines' less than a full screen.
5397 The next window is the one below the current one; or the one at the top
5398 if the current one is at the bottom. Negative ARG means scroll downward.
5399 If ARG is the atom `-', scroll downward by nearly full screen.
5400 When calling from a program, supply as argument a number, nil, or `-'.
5402 If `other-window-scroll-buffer' is non-nil, scroll the window
5403 showing that buffer, popping the buffer up if necessary.
5404 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5405 specifies the window to scroll. This takes precedence over
5406 `other-window-scroll-buffer'. */)
5407 (Lisp_Object arg)
5409 Lisp_Object window;
5410 struct window *w;
5411 ptrdiff_t count = SPECPDL_INDEX ();
5413 window = Fother_window_for_scrolling ();
5414 w = XWINDOW (window);
5416 /* Don't screw up if window_scroll gets an error. */
5417 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5419 Fset_buffer (w->contents);
5420 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5421 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5423 if (NILP (arg))
5424 window_scroll (window, 1, true, true);
5425 else if (EQ (arg, Qminus))
5426 window_scroll (window, -1, true, true);
5427 else
5429 if (CONSP (arg))
5430 arg = XCAR (arg);
5431 CHECK_NUMBER (arg);
5432 window_scroll (window, XINT (arg), false, true);
5435 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5436 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5437 unbind_to (count, Qnil);
5439 return Qnil;
5442 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5443 doc: /* Scroll selected window display ARG columns left.
5444 Default for ARG is window width minus 2.
5445 Value is the total amount of leftward horizontal scrolling in
5446 effect after the change.
5447 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5448 lower bound for automatic scrolling, i.e. automatic scrolling
5449 will not scroll a window to a column less than the value returned
5450 by this function. This happens in an interactive call. */)
5451 (register Lisp_Object arg, Lisp_Object set_minimum)
5453 struct window *w = XWINDOW (selected_window);
5454 EMACS_INT requested_arg = (NILP (arg)
5455 ? window_body_width (w, 0) - 2
5456 : XINT (Fprefix_numeric_value (arg)));
5457 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5459 if (!NILP (set_minimum))
5460 w->min_hscroll = w->hscroll;
5462 w->suspend_auto_hscroll = true;
5464 return result;
5467 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5468 doc: /* Scroll selected window display ARG columns right.
5469 Default for ARG is window width minus 2.
5470 Value is the total amount of leftward horizontal scrolling in
5471 effect after the change.
5472 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5473 lower bound for automatic scrolling, i.e. automatic scrolling
5474 will not scroll a window to a column less than the value returned
5475 by this function. This happens in an interactive call. */)
5476 (register Lisp_Object arg, Lisp_Object set_minimum)
5478 struct window *w = XWINDOW (selected_window);
5479 EMACS_INT requested_arg = (NILP (arg)
5480 ? window_body_width (w, 0) - 2
5481 : XINT (Fprefix_numeric_value (arg)));
5482 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5484 if (!NILP (set_minimum))
5485 w->min_hscroll = w->hscroll;
5487 w->suspend_auto_hscroll = true;
5489 return result;
5492 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5493 doc: /* Return the window which was selected when entering the minibuffer.
5494 Returns nil, if selected window is not a minibuffer window. */)
5495 (void)
5497 if (minibuf_level > 0
5498 && MINI_WINDOW_P (XWINDOW (selected_window))
5499 && WINDOW_LIVE_P (minibuf_selected_window))
5500 return minibuf_selected_window;
5502 return Qnil;
5505 /* Value is the number of lines actually displayed in window W,
5506 as opposed to its height. */
5508 static int
5509 displayed_window_lines (struct window *w)
5511 struct it it;
5512 struct text_pos start;
5513 int height = window_box_height (w);
5514 struct buffer *old_buffer;
5515 int bottom_y;
5516 void *itdata = NULL;
5518 if (XBUFFER (w->contents) != current_buffer)
5520 old_buffer = current_buffer;
5521 set_buffer_internal (XBUFFER (w->contents));
5523 else
5524 old_buffer = NULL;
5526 /* In case W->start is out of the accessible range, do something
5527 reasonable. This happens in Info mode when Info-scroll-down
5528 calls (recenter -1) while W->start is 1. */
5529 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5531 itdata = bidi_shelve_cache ();
5532 start_display (&it, w, start);
5533 move_it_vertically (&it, height);
5534 bottom_y = line_bottom_y (&it);
5535 bidi_unshelve_cache (itdata, false);
5537 /* rms: On a non-window display,
5538 the value of it.vpos at the bottom of the screen
5539 seems to be 1 larger than window_box_height (w).
5540 This kludge fixes a bug whereby (move-to-window-line -1)
5541 when ZV is on the last screen line
5542 moves to the previous screen line instead of the last one. */
5543 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5544 height++;
5546 /* Add in empty lines at the bottom of the window. */
5547 if (bottom_y < height)
5549 int uy = FRAME_LINE_HEIGHT (it.f);
5550 it.vpos += (height - bottom_y + uy - 1) / uy;
5553 if (old_buffer)
5554 set_buffer_internal (old_buffer);
5556 return it.vpos;
5560 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5561 doc: /* Center point in selected window and maybe redisplay frame.
5562 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5563 relative to the selected window. If ARG is negative, it counts up from the
5564 bottom of the window. (ARG should be less than the height of the window.)
5566 If ARG is omitted or nil, then recenter with point on the middle line of
5567 the selected window; if the variable `recenter-redisplay' is non-nil,
5568 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5569 is set to `grow-only', this resets the tool-bar's height to the minimum
5570 height needed); if `recenter-redisplay' has the special value `tty',
5571 then only tty frames are redrawn.
5573 Just C-u as prefix means put point in the center of the window
5574 and redisplay normally--don't erase and redraw the frame. */)
5575 (register Lisp_Object arg)
5577 struct window *w = XWINDOW (selected_window);
5578 struct buffer *buf = XBUFFER (w->contents);
5579 bool center_p = false;
5580 ptrdiff_t charpos, bytepos;
5581 EMACS_INT iarg IF_LINT (= 0);
5582 int this_scroll_margin;
5584 if (buf != current_buffer)
5585 error ("`recenter'ing a window that does not display current-buffer.");
5587 /* If redisplay is suppressed due to an error, try again. */
5588 buf->display_error_modiff = 0;
5590 if (NILP (arg))
5592 if (!NILP (Vrecenter_redisplay)
5593 && (!EQ (Vrecenter_redisplay, Qtty)
5594 || !NILP (Ftty_type (selected_frame))))
5596 ptrdiff_t i;
5598 /* Invalidate pixel data calculated for all compositions. */
5599 for (i = 0; i < n_compositions; i++)
5600 composition_table[i]->font = NULL;
5601 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5602 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5603 #endif
5604 Fredraw_frame (WINDOW_FRAME (w));
5605 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5608 center_p = true;
5610 else if (CONSP (arg)) /* Just C-u. */
5611 center_p = true;
5612 else
5614 arg = Fprefix_numeric_value (arg);
5615 CHECK_NUMBER (arg);
5616 iarg = XINT (arg);
5619 /* Do this after making BUF current
5620 in case scroll_margin is buffer-local. */
5621 this_scroll_margin
5622 = max (0, min (scroll_margin, w->total_lines / 4));
5624 /* Don't use redisplay code for initial frames, as the necessary
5625 data structures might not be set up yet then. */
5626 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5628 if (center_p)
5630 struct it it;
5631 struct text_pos pt;
5632 void *itdata = bidi_shelve_cache ();
5634 SET_TEXT_POS (pt, PT, PT_BYTE);
5635 start_display (&it, w, pt);
5636 move_it_vertically_backward (&it, window_box_height (w) / 2);
5637 charpos = IT_CHARPOS (it);
5638 bytepos = IT_BYTEPOS (it);
5639 bidi_unshelve_cache (itdata, false);
5641 else if (iarg < 0)
5643 struct it it;
5644 struct text_pos pt;
5645 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5646 int extra_line_spacing;
5647 int h = window_box_height (w);
5648 int ht = window_internal_height (w);
5649 void *itdata = bidi_shelve_cache ();
5651 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5652 ht - this_scroll_margin);
5654 SET_TEXT_POS (pt, PT, PT_BYTE);
5655 start_display (&it, w, pt);
5657 /* Be sure we have the exact height of the full line containing PT. */
5658 move_it_by_lines (&it, 0);
5660 /* The amount of pixels we have to move back is the window
5661 height minus what's displayed in the line containing PT,
5662 and the lines below. */
5663 it.current_y = 0;
5664 it.vpos = 0;
5665 move_it_by_lines (&it, nlines);
5667 if (it.vpos == nlines)
5668 h -= it.current_y;
5669 else
5671 /* Last line has no newline. */
5672 h -= line_bottom_y (&it);
5673 it.vpos++;
5676 /* Don't reserve space for extra line spacing of last line. */
5677 extra_line_spacing = it.max_extra_line_spacing;
5679 /* If we can't move down NLINES lines because we hit
5680 the end of the buffer, count in some empty lines. */
5681 if (it.vpos < nlines)
5683 nlines -= it.vpos;
5684 extra_line_spacing = it.extra_line_spacing;
5685 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5687 if (h <= 0)
5689 bidi_unshelve_cache (itdata, false);
5690 return Qnil;
5693 /* Now find the new top line (starting position) of the window. */
5694 start_display (&it, w, pt);
5695 it.current_y = 0;
5696 move_it_vertically_backward (&it, h);
5698 /* If extra line spacing is present, we may move too far
5699 back. This causes the last line to be only partially
5700 visible (which triggers redisplay to recenter that line
5701 in the middle), so move forward.
5702 But ignore extra line spacing on last line, as it is not
5703 considered to be part of the visible height of the line.
5705 h += extra_line_spacing;
5706 while (-it.current_y > h)
5707 move_it_by_lines (&it, 1);
5709 charpos = IT_CHARPOS (it);
5710 bytepos = IT_BYTEPOS (it);
5712 bidi_unshelve_cache (itdata, false);
5714 else
5716 struct it it;
5717 struct text_pos pt;
5718 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5719 int ht = window_internal_height (w);
5720 void *itdata = bidi_shelve_cache ();
5722 nlines = clip_to_bounds (this_scroll_margin, nlines,
5723 ht - this_scroll_margin - 1);
5725 SET_TEXT_POS (pt, PT, PT_BYTE);
5726 start_display (&it, w, pt);
5728 /* Move to the beginning of screen line containing PT. */
5729 move_it_by_lines (&it, 0);
5731 /* Move back to find the point which is ARG screen lines above PT. */
5732 if (nlines > 0)
5734 it.current_y = 0;
5735 it.vpos = 0;
5736 move_it_by_lines (&it, -nlines);
5739 charpos = IT_CHARPOS (it);
5740 bytepos = IT_BYTEPOS (it);
5742 bidi_unshelve_cache (itdata, false);
5745 else
5747 struct position pos;
5748 int ht = window_internal_height (w);
5750 if (center_p)
5751 iarg = ht / 2;
5752 else if (iarg < 0)
5753 iarg += ht;
5755 /* Don't let it get into the margin at either top or bottom. */
5756 iarg = clip_to_bounds (this_scroll_margin, iarg,
5757 ht - this_scroll_margin - 1);
5759 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5760 charpos = pos.bufpos;
5761 bytepos = pos.bytepos;
5764 /* Set the new window start. */
5765 set_marker_both (w->start, w->contents, charpos, bytepos);
5766 w->window_end_valid = false;
5768 w->optional_new_start = true;
5770 w->start_at_line_beg = (bytepos == BEGV_BYTE
5771 || FETCH_BYTE (bytepos - 1) == '\n');
5773 wset_redisplay (w);
5775 return Qnil;
5778 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5779 0, 2, 0,
5780 doc: /* Return the width in columns of the text display area of WINDOW.
5781 WINDOW must be a live window and defaults to the selected one.
5783 The returned width does not include dividers, scrollbars, margins,
5784 fringes, nor any partial-width columns at the right of the text
5785 area.
5787 Optional argument PIXELWISE non-nil, means to return the width in
5788 pixels. */)
5789 (Lisp_Object window, Lisp_Object pixelwise)
5791 struct window *w = decode_live_window (window);
5793 if (NILP (pixelwise))
5794 return make_number (window_box_width (w, TEXT_AREA)
5795 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5796 else
5797 return make_number (window_box_width (w, TEXT_AREA));
5800 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5801 0, 2, 0,
5802 doc: /* Return the height in lines of the text display area of WINDOW.
5803 WINDOW must be a live window and defaults to the selected one.
5805 The returned height does not include dividers, the mode line, any header
5806 line, nor any partial-height lines at the bottom of the text area.
5808 Optional argument PIXELWISE non-nil, means to return the height in
5809 pixels. */)
5810 (Lisp_Object window, Lisp_Object pixelwise)
5812 struct window *w = decode_live_window (window);
5814 if (NILP (pixelwise))
5815 return make_number (window_box_height (w)
5816 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5817 else
5818 return make_number (window_box_height (w));
5821 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5822 1, 1, "P",
5823 doc: /* Position point relative to window.
5824 ARG nil means position point at center of window.
5825 Else, ARG specifies vertical position within the window;
5826 zero means top of window, negative means relative to bottom of window. */)
5827 (Lisp_Object arg)
5829 struct window *w = XWINDOW (selected_window);
5830 int lines, start;
5831 Lisp_Object window;
5832 #if false
5833 int this_scroll_margin;
5834 #endif
5836 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5837 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5838 when passed below to set_marker_both. */
5839 error ("move-to-window-line called from unrelated buffer");
5841 window = selected_window;
5842 start = marker_position (w->start);
5843 if (start < BEGV || start > ZV)
5845 int height = window_internal_height (w);
5846 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5847 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5848 w->start_at_line_beg = !NILP (Fbolp ());
5849 w->force_start = true;
5851 else
5852 Fgoto_char (w->start);
5854 lines = displayed_window_lines (w);
5856 #if false
5857 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5858 #endif
5860 if (NILP (arg))
5861 XSETFASTINT (arg, lines / 2);
5862 else
5864 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5866 if (iarg < 0)
5867 iarg = iarg + lines;
5869 #if false /* This code would prevent move-to-window-line from moving point
5870 to a place inside the scroll margins (which would cause the
5871 next redisplay to scroll). I wrote this code, but then concluded
5872 it is probably better not to install it. However, it is here
5873 inside #if false so as not to lose it. -- rms. */
5875 /* Don't let it get into the margin at either top or bottom. */
5876 iarg = max (iarg, this_scroll_margin);
5877 iarg = min (iarg, lines - this_scroll_margin - 1);
5878 #endif
5880 arg = make_number (iarg);
5883 /* Skip past a partially visible first line. */
5884 if (w->vscroll)
5885 XSETINT (arg, XINT (arg) + 1);
5887 return Fvertical_motion (arg, window, Qnil);
5892 /***********************************************************************
5893 Window Configuration
5894 ***********************************************************************/
5896 struct save_window_data
5898 struct vectorlike_header header;
5899 Lisp_Object selected_frame;
5900 Lisp_Object current_window;
5901 Lisp_Object current_buffer;
5902 Lisp_Object minibuf_scroll_window;
5903 Lisp_Object minibuf_selected_window;
5904 Lisp_Object root_window;
5905 Lisp_Object focus_frame;
5906 /* A vector, each of whose elements is a struct saved_window
5907 for one window. */
5908 Lisp_Object saved_windows;
5910 /* All fields above are traced by the GC.
5911 From `frame-cols' down, the fields are ignored by the GC. */
5912 /* We should be able to do without the following two. */
5913 int frame_cols, frame_lines;
5914 /* These two should get eventually replaced by their pixel
5915 counterparts. */
5916 int frame_menu_bar_lines, frame_tool_bar_lines;
5917 int frame_text_width, frame_text_height;
5918 /* These are currently unused. We need them as soon as we convert
5919 to pixels. */
5920 int frame_menu_bar_height, frame_tool_bar_height;
5923 /* This is saved as a Lisp_Vector. */
5924 struct saved_window
5926 struct vectorlike_header header;
5928 Lisp_Object window, buffer, start, pointm, old_pointm;
5929 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5930 Lisp_Object left_col, top_line, total_cols, total_lines;
5931 Lisp_Object normal_cols, normal_lines;
5932 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
5933 Lisp_Object parent, prev;
5934 Lisp_Object start_at_line_beg;
5935 Lisp_Object display_table;
5936 Lisp_Object left_margin_cols, right_margin_cols;
5937 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5938 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5939 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
5940 Lisp_Object combination_limit, window_parameters;
5943 #define SAVED_WINDOW_N(swv,n) \
5944 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5946 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5947 doc: /* Return t if OBJECT is a window-configuration object. */)
5948 (Lisp_Object object)
5950 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5953 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5954 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5955 (Lisp_Object config)
5957 register struct save_window_data *data;
5958 struct Lisp_Vector *saved_windows;
5960 CHECK_WINDOW_CONFIGURATION (config);
5962 data = (struct save_window_data *) XVECTOR (config);
5963 saved_windows = XVECTOR (data->saved_windows);
5964 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5967 DEFUN ("set-window-configuration", Fset_window_configuration,
5968 Sset_window_configuration, 1, 1, 0,
5969 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5970 CONFIGURATION must be a value previously returned
5971 by `current-window-configuration' (which see).
5972 If CONFIGURATION was made from a frame that is now deleted,
5973 only frame-independent values can be restored. In this case,
5974 the return value is nil. Otherwise the value is t. */)
5975 (Lisp_Object configuration)
5977 register struct save_window_data *data;
5978 struct Lisp_Vector *saved_windows;
5979 Lisp_Object new_current_buffer;
5980 Lisp_Object frame;
5981 struct frame *f;
5982 ptrdiff_t old_point = -1;
5983 USE_SAFE_ALLOCA;
5985 CHECK_WINDOW_CONFIGURATION (configuration);
5987 data = (struct save_window_data *) XVECTOR (configuration);
5988 saved_windows = XVECTOR (data->saved_windows);
5990 new_current_buffer = data->current_buffer;
5991 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5992 new_current_buffer = Qnil;
5993 else
5995 if (XBUFFER (new_current_buffer) == current_buffer)
5996 /* The code further down "preserves point" by saving here PT in
5997 old_point and then setting it later back into PT. When the
5998 current-selected-window and the final-selected-window both show
5999 the current buffer, this suffers from the problem that the
6000 current PT is the window-point of the current-selected-window,
6001 while the final PT is the point of the final-selected-window, so
6002 this copy from one PT to the other would end up moving the
6003 window-point of the final-selected-window to the window-point of
6004 the current-selected-window. So we have to be careful which
6005 point of the current-buffer we copy into old_point. */
6006 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6007 && WINDOWP (selected_window)
6008 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6009 && !EQ (selected_window, data->current_window))
6010 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6011 else
6012 old_point = PT;
6013 else
6014 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6015 point in new_current_buffer as of the last time this buffer was
6016 used. This can be non-deterministic since it can be changed by
6017 things like jit-lock by mere temporary selection of some random
6018 window that happens to show this buffer.
6019 So if possible we want this arbitrary choice of "which point" to
6020 be the one from the to-be-selected-window so as to prevent this
6021 window's cursor from being copied from another window. */
6022 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6023 /* If current_window = selected_window, its point is in BUF_PT. */
6024 && !EQ (selected_window, data->current_window))
6025 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6026 else
6027 old_point = BUF_PT (XBUFFER (new_current_buffer));
6030 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6031 f = XFRAME (frame);
6033 /* If f is a dead frame, don't bother rebuilding its window tree.
6034 However, there is other stuff we should still try to do below. */
6035 if (FRAME_LIVE_P (f))
6037 Lisp_Object window;
6038 Lisp_Object dead_windows = Qnil;
6039 Lisp_Object tem, par, pers;
6040 struct window *w;
6041 struct saved_window *p;
6042 struct window *root_window;
6043 struct window **leaf_windows;
6044 ptrdiff_t i, k, n_leaf_windows;
6046 /* Don't do this within the main loop below: This may call Lisp
6047 code and is thus potentially unsafe while input is blocked. */
6048 for (k = 0; k < saved_windows->header.size; k++)
6050 p = SAVED_WINDOW_N (saved_windows, k);
6051 window = p->window;
6052 w = XWINDOW (window);
6053 if (BUFFERP (w->contents)
6054 && !EQ (w->contents, p->buffer)
6055 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6056 /* If a window we restore gets another buffer, record the
6057 window's old buffer. */
6058 call1 (Qrecord_window_buffer, window);
6061 /* Disallow x_set_window_size, temporarily. */
6062 f->can_x_set_window_size = false;
6063 /* The mouse highlighting code could get screwed up
6064 if it runs during this. */
6065 block_input ();
6067 /* "Swap out" point from the selected window's buffer
6068 into the window itself. (Normally the pointm of the selected
6069 window holds garbage.) We do this now, before
6070 restoring the window contents, and prevent it from
6071 being done later on when we select a new window. */
6072 if (! NILP (XWINDOW (selected_window)->contents))
6074 w = XWINDOW (selected_window);
6075 set_marker_both (w->pointm,
6076 w->contents,
6077 BUF_PT (XBUFFER (w->contents)),
6078 BUF_PT_BYTE (XBUFFER (w->contents)));
6081 fset_redisplay (f);
6082 FRAME_WINDOW_SIZES_CHANGED (f) = true;
6084 /* Problem: Freeing all matrices and later allocating them again
6085 is a serious redisplay flickering problem. What we would
6086 really like to do is to free only those matrices not reused
6087 below. */
6088 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6089 ptrdiff_t nwindows = count_windows (root_window);
6090 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6091 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6093 /* Kludge Alert!
6094 Mark all windows now on frame as "deleted".
6095 Restoring the new configuration "undeletes" any that are in it.
6097 Save their current buffers in their height fields, since we may
6098 need it later, if a buffer saved in the configuration is now
6099 dead. */
6100 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6102 for (k = 0; k < saved_windows->header.size; k++)
6104 p = SAVED_WINDOW_N (saved_windows, k);
6105 window = p->window;
6106 w = XWINDOW (window);
6107 wset_next (w, Qnil);
6109 if (!NILP (p->parent))
6110 wset_parent
6111 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6112 else
6113 wset_parent (w, Qnil);
6115 if (!NILP (p->prev))
6117 wset_prev
6118 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6119 wset_next (XWINDOW (w->prev), p->window);
6121 else
6123 wset_prev (w, Qnil);
6124 if (!NILP (w->parent))
6125 wset_combination (XWINDOW (w->parent),
6126 (XINT (p->total_cols)
6127 != XWINDOW (w->parent)->total_cols),
6128 p->window);
6131 /* If we squirreled away the buffer, restore it now. */
6132 if (BUFFERP (w->combination_limit))
6133 wset_buffer (w, w->combination_limit);
6134 w->pixel_left = XFASTINT (p->pixel_left);
6135 w->pixel_top = XFASTINT (p->pixel_top);
6136 w->pixel_width = XFASTINT (p->pixel_width);
6137 w->pixel_height = XFASTINT (p->pixel_height);
6138 w->left_col = XFASTINT (p->left_col);
6139 w->top_line = XFASTINT (p->top_line);
6140 w->total_cols = XFASTINT (p->total_cols);
6141 w->total_lines = XFASTINT (p->total_lines);
6142 wset_normal_cols (w, p->normal_cols);
6143 wset_normal_lines (w, p->normal_lines);
6144 w->hscroll = XFASTINT (p->hscroll);
6145 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6146 w->min_hscroll = XFASTINT (p->min_hscroll);
6147 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6148 wset_display_table (w, p->display_table);
6149 w->left_margin_cols = XINT (p->left_margin_cols);
6150 w->right_margin_cols = XINT (p->right_margin_cols);
6151 w->left_fringe_width = XINT (p->left_fringe_width);
6152 w->right_fringe_width = XINT (p->right_fringe_width);
6153 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6154 w->scroll_bar_width = XINT (p->scroll_bar_width);
6155 w->scroll_bar_height = XINT (p->scroll_bar_height);
6156 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6157 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6158 wset_dedicated (w, p->dedicated);
6159 wset_combination_limit (w, p->combination_limit);
6160 /* Restore any window parameters that have been saved.
6161 Parameters that have not been saved are left alone. */
6162 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6164 pers = XCAR (tem);
6165 if (CONSP (pers))
6167 if (NILP (XCDR (pers)))
6169 par = Fassq (XCAR (pers), w->window_parameters);
6170 if (CONSP (par) && !NILP (XCDR (par)))
6171 /* Reset a parameter to nil if and only if it
6172 has a non-nil association. Don't make new
6173 associations. */
6174 Fsetcdr (par, Qnil);
6176 else
6177 /* Always restore a non-nil value. */
6178 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6182 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6183 /* If saved buffer is alive, install it. */
6185 wset_buffer (w, p->buffer);
6186 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6187 set_marker_restricted (w->start, p->start, w->contents);
6188 set_marker_restricted (w->pointm, p->pointm, w->contents);
6189 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6190 /* As documented in Fcurrent_window_configuration, don't
6191 restore the location of point in the buffer which was
6192 current when the window configuration was recorded. */
6193 if (!EQ (p->buffer, new_current_buffer)
6194 && XBUFFER (p->buffer) == current_buffer)
6195 Fgoto_char (w->pointm);
6197 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6198 /* Keep window's old buffer; make sure the markers are real. */
6200 /* Set window markers at start of visible range. */
6201 if (XMARKER (w->start)->buffer == 0)
6202 set_marker_restricted_both (w->start, w->contents, 0, 0);
6203 if (XMARKER (w->pointm)->buffer == 0)
6204 set_marker_restricted_both
6205 (w->pointm, w->contents,
6206 BUF_PT (XBUFFER (w->contents)),
6207 BUF_PT_BYTE (XBUFFER (w->contents)));
6208 if (XMARKER (w->old_pointm)->buffer == 0)
6209 set_marker_restricted_both
6210 (w->old_pointm, w->contents,
6211 BUF_PT (XBUFFER (w->contents)),
6212 BUF_PT_BYTE (XBUFFER (w->contents)));
6213 w->start_at_line_beg = true;
6215 else if (!NILP (w->start))
6216 /* Leaf window has no live buffer, get one. */
6218 /* Get the buffer via other_buffer_safely in order to
6219 avoid showing an unimportant buffer and, if necessary, to
6220 recreate *scratch* in the course (part of Juanma's bs-show
6221 scenario from March 2011). */
6222 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6223 /* This will set the markers to beginning of visible
6224 range. */
6225 set_marker_restricted_both (w->start, w->contents, 0, 0);
6226 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6227 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6228 w->start_at_line_beg = true;
6229 if (!NILP (w->dedicated))
6230 /* Record this window as dead. */
6231 dead_windows = Fcons (window, dead_windows);
6232 /* Make sure window is no more dedicated. */
6233 wset_dedicated (w, Qnil);
6237 fset_root_window (f, data->root_window);
6238 /* Arrange *not* to restore point in the buffer that was
6239 current when the window configuration was saved. */
6240 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6241 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6242 make_number (old_point),
6243 XWINDOW (data->current_window)->contents);
6245 /* In the following call to `select-window', prevent "swapping out
6246 point" in the old selected window using the buffer that has
6247 been restored into it. We already swapped out that point from
6248 that window's old buffer.
6250 Do not record the buffer here. We do that in a separate call
6251 to select_window below. See also Bug#16207. */
6252 select_window (data->current_window, Qt, true);
6253 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6254 last_selected_window)
6255 = selected_window;
6257 if (NILP (data->focus_frame)
6258 || (FRAMEP (data->focus_frame)
6259 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6260 Fredirect_frame_focus (frame, data->focus_frame);
6262 /* Now, free glyph matrices in windows that were not reused. */
6263 for (i = 0; i < n_leaf_windows; i++)
6264 if (NILP (leaf_windows[i]->contents))
6265 free_window_matrices (leaf_windows[i]);
6267 /* Allow x_set_window_size again and apply frame size changes if
6268 needed. */
6269 f->can_x_set_window_size = true;
6270 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6272 adjust_frame_glyphs (f);
6273 unblock_input ();
6275 /* Scan dead buffer windows. */
6276 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6278 window = XCAR (dead_windows);
6279 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6280 delete_deletable_window (window);
6283 /* Record the selected window's buffer here. The window should
6284 already be the selected one from the call above. */
6285 select_window (data->current_window, Qnil, false);
6287 /* Fselect_window will have made f the selected frame, so we
6288 reselect the proper frame here. Fhandle_switch_frame will change the
6289 selected window too, but that doesn't make the call to
6290 Fselect_window above totally superfluous; it still sets f's
6291 selected window. */
6292 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6293 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6295 run_window_configuration_change_hook (f);
6298 if (!NILP (new_current_buffer))
6300 Fset_buffer (new_current_buffer);
6301 /* If the new current buffer doesn't appear in the selected
6302 window, go to its old point (see bug#12208). */
6303 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6304 Fgoto_char (make_number (old_point));
6307 Vminibuf_scroll_window = data->minibuf_scroll_window;
6308 minibuf_selected_window = data->minibuf_selected_window;
6310 SAFE_FREE ();
6311 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6315 void
6316 restore_window_configuration (Lisp_Object configuration)
6318 Fset_window_configuration (configuration);
6322 /* If WINDOW is an internal window, recursively delete all child windows
6323 reachable via the next and contents slots of WINDOW. Otherwise setup
6324 WINDOW to not show any buffer. */
6326 void
6327 delete_all_child_windows (Lisp_Object window)
6329 register struct window *w;
6331 w = XWINDOW (window);
6333 if (!NILP (w->next))
6334 /* Delete WINDOW's siblings (we traverse postorderly). */
6335 delete_all_child_windows (w->next);
6337 if (WINDOWP (w->contents))
6339 delete_all_child_windows (w->contents);
6340 wset_combination (w, false, Qnil);
6342 else if (BUFFERP (w->contents))
6344 unshow_buffer (w);
6345 unchain_marker (XMARKER (w->pointm));
6346 unchain_marker (XMARKER (w->old_pointm));
6347 unchain_marker (XMARKER (w->start));
6348 /* Since combination limit makes sense for an internal windows
6349 only, we use this slot to save the buffer for the sake of
6350 possible resurrection in Fset_window_configuration. */
6351 wset_combination_limit (w, w->contents);
6352 wset_buffer (w, Qnil);
6355 Vwindow_list = Qnil;
6358 static ptrdiff_t
6359 count_windows (struct window *window)
6361 ptrdiff_t count = 1;
6362 if (!NILP (window->next))
6363 count += count_windows (XWINDOW (window->next));
6364 if (WINDOWP (window->contents))
6365 count += count_windows (XWINDOW (window->contents));
6366 return count;
6370 /* Fill vector FLAT with leaf windows under W, starting at index I.
6371 Value is last index + 1. */
6372 static ptrdiff_t
6373 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6375 while (w)
6377 if (WINDOWP (w->contents))
6378 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6379 else
6380 flat[i++] = w;
6382 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6385 return i;
6389 /* Return a pointer to the glyph W's physical cursor is on. Value is
6390 null if W's current matrix is invalid, so that no meaningful glyph
6391 can be returned. */
6392 struct glyph *
6393 get_phys_cursor_glyph (struct window *w)
6395 struct glyph_row *row;
6396 struct glyph *glyph;
6397 int hpos = w->phys_cursor.hpos;
6399 if (!(w->phys_cursor.vpos >= 0
6400 && w->phys_cursor.vpos < w->current_matrix->nrows))
6401 return NULL;
6403 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6404 if (!row->enabled_p)
6405 return NULL;
6407 if (w->hscroll)
6409 /* When the window is hscrolled, cursor hpos can legitimately be
6410 out of bounds, but we draw the cursor at the corresponding
6411 window margin in that case. */
6412 if (!row->reversed_p && hpos < 0)
6413 hpos = 0;
6414 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6415 hpos = row->used[TEXT_AREA] - 1;
6418 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6419 glyph = row->glyphs[TEXT_AREA] + hpos;
6420 else
6421 glyph = NULL;
6423 return glyph;
6427 static ptrdiff_t
6428 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6430 struct saved_window *p;
6431 struct window *w;
6432 Lisp_Object tem, pers, par;
6434 for (; !NILP (window); window = w->next)
6436 p = SAVED_WINDOW_N (vector, i);
6437 w = XWINDOW (window);
6439 wset_temslot (w, make_number (i)); i++;
6440 p->window = window;
6441 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6442 p->pixel_left = make_number (w->pixel_left);
6443 p->pixel_top = make_number (w->pixel_top);
6444 p->pixel_width = make_number (w->pixel_width);
6445 p->pixel_height = make_number (w->pixel_height);
6446 p->left_col = make_number (w->left_col);
6447 p->top_line = make_number (w->top_line);
6448 p->total_cols = make_number (w->total_cols);
6449 p->total_lines = make_number (w->total_lines);
6450 p->normal_cols = w->normal_cols;
6451 p->normal_lines = w->normal_lines;
6452 XSETFASTINT (p->hscroll, w->hscroll);
6453 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6454 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6455 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6456 p->display_table = w->display_table;
6457 p->left_margin_cols = make_number (w->left_margin_cols);
6458 p->right_margin_cols = make_number (w->right_margin_cols);
6459 p->left_fringe_width = make_number (w->left_fringe_width);
6460 p->right_fringe_width = make_number (w->right_fringe_width);
6461 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6462 p->scroll_bar_width = make_number (w->scroll_bar_width);
6463 p->scroll_bar_height = make_number (w->scroll_bar_height);
6464 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6465 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6466 p->dedicated = w->dedicated;
6467 p->combination_limit = w->combination_limit;
6468 p->window_parameters = Qnil;
6470 if (!NILP (Vwindow_persistent_parameters))
6472 /* Run cycle detection on Vwindow_persistent_parameters. */
6473 Lisp_Object tortoise, hare;
6475 hare = tortoise = Vwindow_persistent_parameters;
6476 while (CONSP (hare))
6478 hare = XCDR (hare);
6479 if (!CONSP (hare))
6480 break;
6482 hare = XCDR (hare);
6483 tortoise = XCDR (tortoise);
6485 if (EQ (hare, tortoise))
6486 /* Reset Vwindow_persistent_parameters to Qnil. */
6488 Vwindow_persistent_parameters = Qnil;
6489 break;
6493 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6494 tem = XCDR (tem))
6496 pers = XCAR (tem);
6497 /* Save values for persistent window parameters. */
6498 if (CONSP (pers) && !NILP (XCDR (pers)))
6500 par = Fassq (XCAR (pers), w->window_parameters);
6501 if (NILP (par))
6502 /* If the window has no value for the parameter,
6503 make one. */
6504 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6505 p->window_parameters);
6506 else
6507 /* If the window has a value for the parameter,
6508 save it. */
6509 p->window_parameters = Fcons (Fcons (XCAR (par),
6510 XCDR (par)),
6511 p->window_parameters);
6516 if (BUFFERP (w->contents))
6518 /* Save w's value of point in the window configuration. If w
6519 is the selected window, then get the value of point from
6520 the buffer; pointm is garbage in the selected window. */
6521 if (EQ (window, selected_window))
6522 p->pointm = build_marker (XBUFFER (w->contents),
6523 BUF_PT (XBUFFER (w->contents)),
6524 BUF_PT_BYTE (XBUFFER (w->contents)));
6525 else
6526 p->pointm = Fcopy_marker (w->pointm, Qnil);
6527 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6528 XMARKER (p->pointm)->insertion_type
6529 = !NILP (buffer_local_value /* Don't signal error if void. */
6530 (Qwindow_point_insertion_type, w->contents));
6531 XMARKER (p->old_pointm)->insertion_type
6532 = !NILP (buffer_local_value /* Don't signal error if void. */
6533 (Qwindow_point_insertion_type, w->contents));
6535 p->start = Fcopy_marker (w->start, Qnil);
6536 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6538 else
6540 p->pointm = Qnil;
6541 p->old_pointm = Qnil;
6542 p->start = Qnil;
6543 p->start_at_line_beg = Qnil;
6546 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6547 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6549 if (WINDOWP (w->contents))
6550 i = save_window_save (w->contents, vector, i);
6553 return i;
6556 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6557 Scurrent_window_configuration, 0, 1, 0,
6558 doc: /* Return an object representing the current window configuration of FRAME.
6559 If FRAME is nil or omitted, use the selected frame.
6560 This describes the number of windows, their sizes and current buffers,
6561 and for each displayed buffer, where display starts, and the position of
6562 point. An exception is made for point in the current buffer:
6563 its value is -not- saved.
6564 This also records the currently selected frame, and FRAME's focus
6565 redirection (see `redirect-frame-focus'). The variable
6566 `window-persistent-parameters' specifies which window parameters are
6567 saved by this function. */)
6568 (Lisp_Object frame)
6570 Lisp_Object tem;
6571 ptrdiff_t i, n_windows;
6572 struct save_window_data *data;
6573 struct frame *f = decode_live_frame (frame);
6575 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6576 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6577 PVEC_WINDOW_CONFIGURATION);
6579 data->frame_cols = FRAME_COLS (f);
6580 data->frame_lines = FRAME_LINES (f);
6581 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6582 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6583 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6584 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6585 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6586 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6587 data->selected_frame = selected_frame;
6588 data->current_window = FRAME_SELECTED_WINDOW (f);
6589 XSETBUFFER (data->current_buffer, current_buffer);
6590 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6591 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6592 data->root_window = FRAME_ROOT_WINDOW (f);
6593 data->focus_frame = FRAME_FOCUS_FRAME (f);
6594 tem = make_uninit_vector (n_windows);
6595 data->saved_windows = tem;
6596 for (i = 0; i < n_windows; i++)
6597 ASET (tem, i,
6598 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6599 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6600 XSETWINDOW_CONFIGURATION (tem, data);
6601 return (tem);
6604 /* Called after W's margins, fringes or scroll bars was adjusted. */
6606 static void
6607 apply_window_adjustment (struct window *w)
6609 eassert (w);
6610 clear_glyph_matrix (w->current_matrix);
6611 w->window_end_valid = false;
6612 windows_or_buffers_changed = 30;
6613 wset_redisplay (w);
6614 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6618 /***********************************************************************
6619 Marginal Areas
6620 ***********************************************************************/
6622 static int
6623 extract_dimension (Lisp_Object dimension)
6625 if (NILP (dimension))
6626 return -1;
6627 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6628 return XINT (dimension);
6631 static struct window *
6632 set_window_margins (struct window *w, Lisp_Object left_width,
6633 Lisp_Object right_width)
6635 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6636 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6637 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6639 if (w->left_margin_cols != left || w->right_margin_cols != right)
6641 /* Don't change anything if new margins won't fit. */
6642 if ((WINDOW_PIXEL_WIDTH (w)
6643 - WINDOW_FRINGES_WIDTH (w)
6644 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6645 - (left + right) * unit)
6646 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6648 w->left_margin_cols = left;
6649 w->right_margin_cols = right;
6651 return w;
6653 else
6654 return NULL;
6656 else
6657 return NULL;
6660 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6661 2, 3, 0,
6662 doc: /* Set width of marginal areas of window WINDOW.
6663 WINDOW must be a live window and defaults to the selected one.
6665 Second arg LEFT-WIDTH specifies the number of character cells to
6666 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6667 does the same for the right marginal area. A nil width parameter
6668 means no margin.
6670 Return t if any margin was actually changed and nil otherwise. */)
6671 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6673 struct window *w = set_window_margins (decode_live_window (window),
6674 left_width, right_width);
6675 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6679 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6680 0, 1, 0,
6681 doc: /* Get width of marginal areas of window WINDOW.
6682 WINDOW must be a live window and defaults to the selected one.
6684 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6685 If a marginal area does not exist, its width will be returned
6686 as nil. */)
6687 (Lisp_Object window)
6689 struct window *w = decode_live_window (window);
6690 return Fcons (w->left_margin_cols
6691 ? make_number (w->left_margin_cols) : Qnil,
6692 w->right_margin_cols
6693 ? make_number (w->right_margin_cols) : Qnil);
6698 /***********************************************************************
6699 Fringes
6700 ***********************************************************************/
6702 static struct window *
6703 set_window_fringes (struct window *w, Lisp_Object left_width,
6704 Lisp_Object right_width, Lisp_Object outside_margins)
6706 bool outside = !NILP (outside_margins);
6707 int left = extract_dimension (left_width);
6708 int right = extract_dimension (right_width);
6710 /* Do nothing on a tty or if nothing to actually change. */
6711 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6712 && (w->left_fringe_width != left
6713 || w->right_fringe_width != right
6714 || w->fringes_outside_margins != outside))
6716 if (left > 0 || right > 0)
6718 /* Don't change anything if new fringes don't fit. */
6719 if ((WINDOW_PIXEL_WIDTH (w)
6720 - WINDOW_MARGINS_WIDTH (w)
6721 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6722 - max (left, 0) - max (right, 0))
6723 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6724 return NULL;
6727 w->left_fringe_width = left;
6728 w->right_fringe_width = right;
6729 w->fringes_outside_margins = outside;
6731 return w;
6733 else
6734 return NULL;
6737 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6738 2, 4, 0,
6739 doc: /* Set the fringe widths of window WINDOW.
6740 WINDOW must be a live window and defaults to the selected one.
6742 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6743 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6744 fringe width. If a fringe width arg is nil, that means to use the
6745 frame's default fringe width. Default fringe widths can be set with
6746 the command `set-fringe-style'.
6747 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6748 outside of the display margins. By default, fringes are drawn between
6749 display marginal areas and the text area.
6751 Return t if any fringe was actually changed and nil otherwise. */)
6752 (Lisp_Object window, Lisp_Object left_width,
6753 Lisp_Object right_width, Lisp_Object outside_margins)
6755 struct window *w
6756 = set_window_fringes (decode_live_window (window),
6757 left_width, right_width, outside_margins);
6758 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6762 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6763 0, 1, 0,
6764 doc: /* Get width of fringes of window WINDOW.
6765 WINDOW must be a live window and defaults to the selected one.
6767 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6768 (Lisp_Object window)
6770 struct window *w = decode_live_window (window);
6772 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6773 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6774 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6779 /***********************************************************************
6780 Scroll bars
6781 ***********************************************************************/
6783 static struct window *
6784 set_window_scroll_bars (struct window *w, Lisp_Object width,
6785 Lisp_Object vertical_type, Lisp_Object height,
6786 Lisp_Object horizontal_type)
6788 int iwidth = extract_dimension (width);
6789 bool changed = false;
6791 if (iwidth == 0)
6792 vertical_type = Qnil;
6794 if (!(NILP (vertical_type)
6795 || EQ (vertical_type, Qleft)
6796 || EQ (vertical_type, Qright)
6797 || EQ (vertical_type, Qt)))
6798 error ("Invalid type of vertical scroll bar");
6800 if (w->scroll_bar_width != iwidth
6801 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6803 /* Don't change anything if new scroll bar won't fit. */
6804 if ((WINDOW_PIXEL_WIDTH (w)
6805 - WINDOW_MARGINS_WIDTH (w)
6806 - WINDOW_FRINGES_WIDTH (w)
6807 - max (iwidth, 0))
6808 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6810 w->scroll_bar_width = iwidth;
6811 wset_vertical_scroll_bar_type (w, vertical_type);
6812 changed = true;
6816 #if USE_HORIZONTAL_SCROLL_BARS
6818 int iheight = extract_dimension (height);
6820 if (MINI_WINDOW_P (w) || iheight == 0)
6821 horizontal_type = Qnil;
6823 if (!(NILP (horizontal_type)
6824 || EQ (horizontal_type, Qbottom)
6825 || EQ (horizontal_type, Qt)))
6826 error ("Invalid type of horizontal scroll bar");
6828 if (w->scroll_bar_height != iheight
6829 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6831 /* Don't change anything if new scroll bar won't fit. */
6832 if ((WINDOW_PIXEL_HEIGHT (w)
6833 - WINDOW_HEADER_LINE_HEIGHT (w)
6834 - WINDOW_MODE_LINE_HEIGHT (w)
6835 - max (iheight, 0))
6836 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
6838 w->scroll_bar_height = iheight;
6839 wset_horizontal_scroll_bar_type (w, horizontal_type);
6840 changed = true;
6844 #else
6845 wset_horizontal_scroll_bar_type (w, Qnil);
6846 #endif
6848 return changed ? w : NULL;
6851 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6852 Sset_window_scroll_bars, 1, 5, 0,
6853 doc: /* Set width and type of scroll bars of window WINDOW.
6854 WINDOW must be a live window and defaults to the selected one.
6856 Second parameter WIDTH specifies the pixel width for the vertical scroll
6857 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
6858 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6859 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
6860 frame's scroll-bar type.
6862 Fourth parameter HEIGHT specifies the pixel height for the horizontal
6863 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
6864 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
6865 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
6866 means to use the frame's horizontal scroll-bar type.
6868 Return t if scroll bars were actually changed and nil otherwise. */)
6869 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
6870 Lisp_Object height, Lisp_Object horizontal_type)
6872 struct window *w
6873 = set_window_scroll_bars (decode_live_window (window),
6874 width, vertical_type, height, horizontal_type);
6875 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6879 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6880 0, 1, 0,
6881 doc: /* Get width and type of scroll bars of window WINDOW.
6882 WINDOW must be a live window and defaults to the selected one.
6884 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
6885 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
6886 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
6887 value. */)
6888 (Lisp_Object window)
6890 struct window *w = decode_live_window (window);
6892 return Fcons (((w->scroll_bar_width >= 0)
6893 ? make_number (w->scroll_bar_width)
6894 : Qnil),
6895 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6896 w->vertical_scroll_bar_type,
6897 ((w->scroll_bar_height >= 0)
6898 ? make_number (w->scroll_bar_height)
6899 : Qnil),
6900 make_number (WINDOW_SCROLL_BAR_LINES (w)),
6901 w->horizontal_scroll_bar_type));
6904 /***********************************************************************
6905 Smooth scrolling
6906 ***********************************************************************/
6908 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6909 doc: /* Return the amount by which WINDOW is scrolled vertically.
6910 If WINDOW is omitted or nil, it defaults to the selected window.
6911 Normally, value is a multiple of the canonical character height of WINDOW;
6912 optional second arg PIXELS-P means value is measured in pixels. */)
6913 (Lisp_Object window, Lisp_Object pixels_p)
6915 Lisp_Object result;
6916 struct window *w = decode_live_window (window);
6917 struct frame *f = XFRAME (w->frame);
6919 if (FRAME_WINDOW_P (f))
6920 result = (NILP (pixels_p)
6921 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6922 : make_number (-w->vscroll));
6923 else
6924 result = make_number (0);
6925 return result;
6929 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6930 2, 3, 0,
6931 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6932 WINDOW nil means use the selected window. Normally, VSCROLL is a
6933 non-negative multiple of the canonical character height of WINDOW;
6934 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6935 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6936 corresponds to an integral number of pixels. The return value is the
6937 result of this rounding.
6938 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6939 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6941 struct window *w = decode_live_window (window);
6942 struct frame *f = XFRAME (w->frame);
6944 CHECK_NUMBER_OR_FLOAT (vscroll);
6946 if (FRAME_WINDOW_P (f))
6948 int old_dy = w->vscroll;
6950 w->vscroll = - (NILP (pixels_p)
6951 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6952 : XFLOATINT (vscroll));
6953 w->vscroll = min (w->vscroll, 0);
6955 if (w->vscroll != old_dy)
6957 /* Adjust glyph matrix of the frame if the virtual display
6958 area becomes larger than before. */
6959 if (w->vscroll < 0 && w->vscroll < old_dy)
6960 adjust_frame_glyphs (f);
6962 /* Prevent redisplay shortcuts. */
6963 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
6967 return Fwindow_vscroll (window, pixels_p);
6971 /* Call FN for all leaf windows on frame F. FN is called with the
6972 first argument being a pointer to the leaf window, and with
6973 additional argument USER_DATA. Stops when FN returns 0. */
6975 static void
6976 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
6977 void *user_data)
6979 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6980 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6981 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6985 /* Helper function for foreach_window. Call FN for all leaf windows
6986 reachable from W. FN is called with the first argument being a
6987 pointer to the leaf window, and with additional argument USER_DATA.
6988 Stop when FN returns false. Value is false if stopped by FN. */
6990 static bool
6991 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
6992 void *user_data)
6994 bool cont;
6996 for (cont = true; w && cont;)
6998 if (WINDOWP (w->contents))
6999 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7000 else
7001 cont = fn (w, user_data);
7003 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7006 return cont;
7009 /***********************************************************************
7010 Initialization
7011 ***********************************************************************/
7013 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7014 describe the same state of affairs. This is used by Fequal.
7016 IGNORE_POSITIONS means ignore non-matching scroll positions
7017 and the like.
7019 This ignores a couple of things like the dedication status of
7020 window, combination_limit and the like. This might have to be
7021 fixed. */
7023 bool
7024 compare_window_configurations (Lisp_Object configuration1,
7025 Lisp_Object configuration2,
7026 bool ignore_positions)
7028 register struct save_window_data *d1, *d2;
7029 struct Lisp_Vector *sws1, *sws2;
7030 ptrdiff_t i;
7032 CHECK_WINDOW_CONFIGURATION (configuration1);
7033 CHECK_WINDOW_CONFIGURATION (configuration2);
7035 d1 = (struct save_window_data *) XVECTOR (configuration1);
7036 d2 = (struct save_window_data *) XVECTOR (configuration2);
7037 sws1 = XVECTOR (d1->saved_windows);
7038 sws2 = XVECTOR (d2->saved_windows);
7040 /* Frame settings must match. */
7041 if (d1->frame_cols != d2->frame_cols
7042 || d1->frame_lines != d2->frame_lines
7043 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7044 || !EQ (d1->selected_frame, d2->selected_frame)
7045 || !EQ (d1->current_buffer, d2->current_buffer)
7046 || (!ignore_positions
7047 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7048 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7049 || !EQ (d1->focus_frame, d2->focus_frame)
7050 /* Verify that the two configurations have the same number of windows. */
7051 || sws1->header.size != sws2->header.size)
7052 return false;
7054 for (i = 0; i < sws1->header.size; i++)
7056 struct saved_window *sw1, *sw2;
7058 sw1 = SAVED_WINDOW_N (sws1, i);
7059 sw2 = SAVED_WINDOW_N (sws2, i);
7061 if (
7062 /* The "current" windows in the two configurations must
7063 correspond to each other. */
7064 EQ (d1->current_window, sw1->window)
7065 != EQ (d2->current_window, sw2->window)
7066 /* Windows' buffers must match. */
7067 || !EQ (sw1->buffer, sw2->buffer)
7068 || !EQ (sw1->pixel_left, sw2->pixel_left)
7069 || !EQ (sw1->pixel_top, sw2->pixel_top)
7070 || !EQ (sw1->pixel_height, sw2->pixel_height)
7071 || !EQ (sw1->pixel_width, sw2->pixel_width)
7072 || !EQ (sw1->left_col, sw2->left_col)
7073 || !EQ (sw1->top_line, sw2->top_line)
7074 || !EQ (sw1->total_cols, sw2->total_cols)
7075 || !EQ (sw1->total_lines, sw2->total_lines)
7076 || !EQ (sw1->display_table, sw2->display_table)
7077 /* The next two disjuncts check the window structure for
7078 equality. */
7079 || !EQ (sw1->parent, sw2->parent)
7080 || !EQ (sw1->prev, sw2->prev)
7081 || (!ignore_positions
7082 && (!EQ (sw1->hscroll, sw2->hscroll)
7083 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7084 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7085 || NILP (Fequal (sw1->start, sw2->start))
7086 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7087 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7088 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7089 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7090 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7091 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7092 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7093 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7094 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7095 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7096 return false;
7099 return true;
7102 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7103 Scompare_window_configurations, 2, 2, 0,
7104 doc: /* Compare two window configurations as regards the structure of windows.
7105 This function ignores details such as the values of point
7106 and scrolling positions. */)
7107 (Lisp_Object x, Lisp_Object y)
7109 if (compare_window_configurations (x, y, true))
7110 return Qt;
7111 return Qnil;
7114 void
7115 init_window_once (void)
7117 struct frame *f = make_initial_frame ();
7118 XSETFRAME (selected_frame, f);
7119 Vterminal_frame = selected_frame;
7120 minibuf_window = f->minibuffer_window;
7121 selected_window = f->selected_window;
7124 void
7125 init_window (void)
7127 Vwindow_list = Qnil;
7130 void
7131 syms_of_window (void)
7133 DEFSYM (Qscroll_up, "scroll-up");
7134 DEFSYM (Qscroll_down, "scroll-down");
7135 DEFSYM (Qscroll_command, "scroll-command");
7137 Fput (Qscroll_up, Qscroll_command, Qt);
7138 Fput (Qscroll_down, Qscroll_command, Qt);
7140 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7141 DEFSYM (Qwindowp, "windowp");
7142 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7143 DEFSYM (Qwindow_live_p, "window-live-p");
7144 DEFSYM (Qwindow_valid_p, "window-valid-p");
7145 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7146 DEFSYM (Qdelete_window, "delete-window");
7147 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7148 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7149 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7150 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7151 DEFSYM (Qsafe, "safe");
7152 DEFSYM (Qdisplay_buffer, "display-buffer");
7153 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7154 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7155 DEFSYM (Qget_mru_window, "get-mru-window");
7156 DEFSYM (Qwindow_size, "window-size");
7157 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7158 DEFSYM (Qabove, "above");
7159 DEFSYM (Qclone_of, "clone-of");
7160 DEFSYM (Qfloor, "floor");
7161 DEFSYM (Qceiling, "ceiling");
7163 staticpro (&Vwindow_list);
7165 minibuf_selected_window = Qnil;
7166 staticpro (&minibuf_selected_window);
7168 window_scroll_pixel_based_preserve_x = -1;
7169 window_scroll_pixel_based_preserve_y = -1;
7170 window_scroll_preserve_hpos = -1;
7171 window_scroll_preserve_vpos = -1;
7173 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7174 doc: /* Non-nil means call as function to display a help buffer.
7175 The function is called with one argument, the buffer to be displayed.
7176 Used by `with-output-to-temp-buffer'.
7177 If this function is used, then it must do the entire job of showing
7178 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7179 Vtemp_buffer_show_function = Qnil;
7181 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7182 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7183 Vminibuf_scroll_window = Qnil;
7185 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7186 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7187 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7188 is displayed in the `mode-line' face. */);
7189 mode_line_in_non_selected_windows = true;
7191 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7192 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7193 Vother_window_scroll_buffer = Qnil;
7195 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7196 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7197 auto_window_vscroll_p = true;
7199 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7200 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7201 next_screen_context_lines = 2;
7203 DEFVAR_LISP ("scroll-preserve-screen-position",
7204 Vscroll_preserve_screen_position,
7205 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7206 A value of nil means point does not keep its screen position except
7207 at the scroll margin or window boundary respectively.
7208 A value of t means point keeps its screen position if the scroll
7209 command moved it vertically out of the window, e.g. when scrolling
7210 by full screens.
7211 Any other value means point always keeps its screen position.
7212 Scroll commands should have the `scroll-command' property
7213 on their symbols to be controlled by this variable. */);
7214 Vscroll_preserve_screen_position = Qnil;
7216 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7217 doc: /* Type of marker to use for `window-point'. */);
7218 Vwindow_point_insertion_type = Qnil;
7219 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7221 DEFVAR_LISP ("window-configuration-change-hook",
7222 Vwindow_configuration_change_hook,
7223 doc: /* Functions to call when window configuration changes.
7224 The buffer-local part is run once per window, with the relevant window
7225 selected; while the global part is run only once for the modified frame,
7226 with the relevant frame selected. */);
7227 Vwindow_configuration_change_hook = Qnil;
7229 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7230 doc: /* Non-nil means `recenter' redraws entire frame.
7231 If this option is non-nil, then the `recenter' command with a nil
7232 argument will redraw the entire frame; the special value `tty' causes
7233 the frame to be redrawn only if it is a tty frame. */);
7234 Vrecenter_redisplay = Qtty;
7236 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7237 doc: /* If t, resize window combinations proportionally.
7238 If this variable is nil, splitting a window gets the entire screen space
7239 for displaying the new window from the window to split. Deleting and
7240 resizing a window preferably resizes one adjacent window only.
7242 If this variable is t, splitting a window tries to get the space
7243 proportionally from all windows in the same combination. This also
7244 allows to split a window that is otherwise too small or of fixed size.
7245 Resizing and deleting a window proportionally resize all windows in the
7246 same combination.
7248 Other values are reserved for future use.
7250 This variable takes no effect if the variable `window-combination-limit' is
7251 non-nil. */);
7252 Vwindow_combination_resize = Qnil;
7254 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7255 doc: /* If non-nil, splitting a window makes a new parent window.
7256 The following values are recognized:
7258 nil means splitting a window will create a new parent window only if the
7259 window has no parent window or the window shall become part of a
7260 combination orthogonal to the one it is part of.
7262 `window-size' means that splitting a window for displaying a buffer
7263 makes a new parent window provided `display-buffer' is supposed to
7264 explicitly set the window's size due to the presence of a
7265 `window-height' or `window-width' entry in the alist used by
7266 `display-buffer'. Otherwise, this value is handled like nil.
7268 `temp-buffer' means that splitting a window for displaying a temporary
7269 buffer always makes a new parent window. Otherwise, this value is
7270 handled like nil.
7272 `display-buffer' means that splitting a window for displaying a buffer
7273 always makes a new parent window. Since temporary buffers are
7274 displayed by the function `display-buffer', this value is stronger
7275 than `temp-buffer'. Splitting a window for other purpose makes a
7276 new parent window only if needed.
7278 t means that splitting a window always creates a new parent window. If
7279 all splits behave this way, each frame's window tree is a binary
7280 tree and every window but the frame's root window has exactly one
7281 sibling.
7283 Other values are reserved for future use. */);
7284 Vwindow_combination_limit = Qwindow_size;
7286 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7287 doc: /* Alist of persistent window parameters.
7288 This alist specifies which window parameters shall get saved by
7289 `current-window-configuration' and `window-state-get' and subsequently
7290 restored to their previous values by `set-window-configuration' and
7291 `window-state-put'.
7293 The car of each entry of this alist is the symbol specifying the
7294 parameter. The cdr is one of the following:
7296 nil means the parameter is neither saved by `window-state-get' nor by
7297 `current-window-configuration'.
7299 t means the parameter is saved by `current-window-configuration' and,
7300 provided its WRITABLE argument is nil, by `window-state-get'.
7302 The symbol `writable' means the parameter is saved unconditionally by
7303 both `current-window-configuration' and `window-state-get'. Do not use
7304 this value for parameters without read syntax (like windows or frames).
7306 Parameters not saved by `current-window-configuration' or
7307 `window-state-get' are left alone by `set-window-configuration'
7308 respectively are not installed by `window-state-put'. */);
7309 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7311 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7312 doc: /* Non-nil means resize windows pixelwise.
7313 This currently affects the functions: `split-window', `maximize-window',
7314 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7315 all functions that symmetrically resize a parent window.
7317 Note that when a frame's pixel size is not a multiple of the
7318 frame's character size, at least one window may get resized
7319 pixelwise even if this option is nil. */);
7320 window_resize_pixelwise = false;
7322 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7323 Vfast_but_imprecise_scrolling,
7324 doc: /* When non-nil, accelerate scrolling operations.
7325 This comes into play when scrolling rapidly over previously
7326 unfontified buffer regions. Only those portions of the buffer which
7327 are actually going to be displayed get fontified.
7329 Note that this optimization can cause the portion of the buffer
7330 displayed after a scrolling operation to be somewhat inaccurate. */);
7331 Vfast_but_imprecise_scrolling = false;
7333 defsubr (&Sselected_window);
7334 defsubr (&Sminibuffer_window);
7335 defsubr (&Swindow_minibuffer_p);
7336 defsubr (&Swindowp);
7337 defsubr (&Swindow_valid_p);
7338 defsubr (&Swindow_live_p);
7339 defsubr (&Swindow_frame);
7340 defsubr (&Sframe_root_window);
7341 defsubr (&Sframe_first_window);
7342 defsubr (&Sframe_selected_window);
7343 defsubr (&Sset_frame_selected_window);
7344 defsubr (&Spos_visible_in_window_p);
7345 defsubr (&Swindow_line_height);
7346 defsubr (&Swindow_buffer);
7347 defsubr (&Swindow_parent);
7348 defsubr (&Swindow_top_child);
7349 defsubr (&Swindow_left_child);
7350 defsubr (&Swindow_next_sibling);
7351 defsubr (&Swindow_prev_sibling);
7352 defsubr (&Swindow_combination_limit);
7353 defsubr (&Sset_window_combination_limit);
7354 defsubr (&Swindow_use_time);
7355 defsubr (&Swindow_pixel_width);
7356 defsubr (&Swindow_pixel_height);
7357 defsubr (&Swindow_total_width);
7358 defsubr (&Swindow_total_height);
7359 defsubr (&Swindow_normal_size);
7360 defsubr (&Swindow_new_pixel);
7361 defsubr (&Swindow_new_total);
7362 defsubr (&Swindow_new_normal);
7363 defsubr (&Swindow_pixel_left);
7364 defsubr (&Swindow_pixel_top);
7365 defsubr (&Swindow_left_column);
7366 defsubr (&Swindow_top_line);
7367 defsubr (&Sset_window_new_pixel);
7368 defsubr (&Sset_window_new_total);
7369 defsubr (&Sset_window_new_normal);
7370 defsubr (&Swindow_resize_apply);
7371 defsubr (&Swindow_resize_apply_total);
7372 defsubr (&Swindow_body_height);
7373 defsubr (&Swindow_body_width);
7374 defsubr (&Swindow_hscroll);
7375 defsubr (&Sset_window_hscroll);
7376 defsubr (&Swindow_redisplay_end_trigger);
7377 defsubr (&Sset_window_redisplay_end_trigger);
7378 defsubr (&Swindow_mode_line_height);
7379 defsubr (&Swindow_header_line_height);
7380 defsubr (&Swindow_right_divider_width);
7381 defsubr (&Swindow_bottom_divider_width);
7382 defsubr (&Swindow_scroll_bar_width);
7383 defsubr (&Swindow_scroll_bar_height);
7384 defsubr (&Scoordinates_in_window_p);
7385 defsubr (&Swindow_at);
7386 defsubr (&Swindow_point);
7387 defsubr (&Swindow_old_point);
7388 defsubr (&Swindow_start);
7389 defsubr (&Swindow_end);
7390 defsubr (&Sset_window_point);
7391 defsubr (&Sset_window_start);
7392 defsubr (&Swindow_dedicated_p);
7393 defsubr (&Sset_window_dedicated_p);
7394 defsubr (&Swindow_display_table);
7395 defsubr (&Sset_window_display_table);
7396 defsubr (&Snext_window);
7397 defsubr (&Sprevious_window);
7398 defsubr (&Swindow__sanitize_window_sizes);
7399 defsubr (&Sget_buffer_window);
7400 defsubr (&Sdelete_other_windows_internal);
7401 defsubr (&Sdelete_window_internal);
7402 defsubr (&Sresize_mini_window_internal);
7403 defsubr (&Sset_window_buffer);
7404 defsubr (&Srun_window_configuration_change_hook);
7405 defsubr (&Srun_window_scroll_functions);
7406 defsubr (&Sselect_window);
7407 defsubr (&Sforce_window_update);
7408 defsubr (&Ssplit_window_internal);
7409 defsubr (&Sscroll_up);
7410 defsubr (&Sscroll_down);
7411 defsubr (&Sscroll_left);
7412 defsubr (&Sscroll_right);
7413 defsubr (&Sother_window_for_scrolling);
7414 defsubr (&Sscroll_other_window);
7415 defsubr (&Sminibuffer_selected_window);
7416 defsubr (&Srecenter);
7417 defsubr (&Swindow_text_width);
7418 defsubr (&Swindow_text_height);
7419 defsubr (&Smove_to_window_line);
7420 defsubr (&Swindow_configuration_p);
7421 defsubr (&Swindow_configuration_frame);
7422 defsubr (&Sset_window_configuration);
7423 defsubr (&Scurrent_window_configuration);
7424 defsubr (&Sset_window_margins);
7425 defsubr (&Swindow_margins);
7426 defsubr (&Sset_window_fringes);
7427 defsubr (&Swindow_fringes);
7428 defsubr (&Sset_window_scroll_bars);
7429 defsubr (&Swindow_scroll_bars);
7430 defsubr (&Swindow_vscroll);
7431 defsubr (&Sset_window_vscroll);
7432 defsubr (&Scompare_window_configurations);
7433 defsubr (&Swindow_list);
7434 defsubr (&Swindow_list_1);
7435 defsubr (&Swindow_prev_buffers);
7436 defsubr (&Sset_window_prev_buffers);
7437 defsubr (&Swindow_next_buffers);
7438 defsubr (&Sset_window_next_buffers);
7439 defsubr (&Swindow_parameters);
7440 defsubr (&Swindow_parameter);
7441 defsubr (&Sset_window_parameter);
7444 void
7445 keys_of_window (void)
7447 initial_define_key (control_x_map, '<', "scroll-left");
7448 initial_define_key (control_x_map, '>', "scroll-right");
7450 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7451 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7452 initial_define_key (meta_map, 'v', "scroll-down-command");