Assume GC_MARK_STACK == GC_MAKE_GCPROS_NOOPS
[emacs.git] / src / window.c
blobf6fe0cdb05989406f9ffa0e5efff3e4e65c930fc
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));
1296 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1297 return 0;
1301 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1302 Scoordinates_in_window_p, 2, 2, 0,
1303 doc: /* Return non-nil if COORDINATES are in WINDOW.
1304 WINDOW must be a live window and defaults to the selected one.
1305 COORDINATES is a cons of the form (X . Y), X and Y being distances
1306 measured in characters from the upper-left corner of the frame.
1307 \(0 . 0) denotes the character in the upper left corner of the
1308 frame.
1309 If COORDINATES are in the text portion of WINDOW,
1310 the coordinates relative to the window are returned.
1311 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1312 If they are in the right divider of WINDOW, `right-divider' is returned.
1313 If they are in the mode line of WINDOW, `mode-line' is returned.
1314 If they are in the header line of WINDOW, `header-line' is returned.
1315 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1316 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1317 If they are on the border between WINDOW and its right sibling,
1318 `vertical-line' is returned.
1319 If they are in the windows's left or right marginal areas, `left-margin'\n\
1320 or `right-margin' is returned. */)
1321 (register Lisp_Object coordinates, Lisp_Object window)
1323 struct window *w;
1324 struct frame *f;
1325 int x, y;
1326 Lisp_Object lx, ly;
1328 w = decode_live_window (window);
1329 f = XFRAME (w->frame);
1330 CHECK_CONS (coordinates);
1331 lx = Fcar (coordinates);
1332 ly = Fcdr (coordinates);
1333 CHECK_NUMBER_OR_FLOAT (lx);
1334 CHECK_NUMBER_OR_FLOAT (ly);
1335 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1336 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1338 switch (coordinates_in_window (w, x, y))
1340 case ON_NOTHING:
1341 return Qnil;
1343 case ON_TEXT:
1344 /* Convert X and Y to window relative pixel coordinates, and
1345 return the canonical char units. */
1346 x -= window_box_left (w, TEXT_AREA);
1347 y -= WINDOW_TOP_EDGE_Y (w);
1348 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1349 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1351 case ON_MODE_LINE:
1352 return Qmode_line;
1354 case ON_VERTICAL_BORDER:
1355 return Qvertical_line;
1357 case ON_HEADER_LINE:
1358 return Qheader_line;
1360 case ON_LEFT_FRINGE:
1361 return Qleft_fringe;
1363 case ON_RIGHT_FRINGE:
1364 return Qright_fringe;
1366 case ON_LEFT_MARGIN:
1367 return Qleft_margin;
1369 case ON_RIGHT_MARGIN:
1370 return Qright_margin;
1372 case ON_VERTICAL_SCROLL_BAR:
1373 /* Historically we are supposed to return nil in this case. */
1374 return Qnil;
1376 case ON_HORIZONTAL_SCROLL_BAR:
1377 return Qnil;
1379 case ON_RIGHT_DIVIDER:
1380 return Qright_divider;
1382 case ON_BOTTOM_DIVIDER:
1383 return Qbottom_divider;
1385 default:
1386 emacs_abort ();
1391 /* Callback for foreach_window, used in window_from_coordinates.
1392 Check if window W contains coordinates specified by USER_DATA which
1393 is actually a pointer to a struct check_window_data CW.
1395 Check if window W contains coordinates *CW->x and *CW->y. If it
1396 does, return W in *CW->window, as Lisp_Object, and return in
1397 *CW->part the part of the window under coordinates *X,*Y. Return
1398 false from this function to stop iterating over windows. */
1400 struct check_window_data
1402 Lisp_Object *window;
1403 int x, y;
1404 enum window_part *part;
1407 static bool
1408 check_window_containing (struct window *w, void *user_data)
1410 struct check_window_data *cw = user_data;
1411 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1412 if (found == ON_NOTHING)
1413 return true;
1414 else
1416 *cw->part = found;
1417 XSETWINDOW (*cw->window, w);
1418 return false;
1423 /* Find the window containing frame-relative pixel position X/Y and
1424 return it as a Lisp_Object.
1426 If X, Y is on one of the window's special `window_part' elements,
1427 set *PART to the id of that element.
1429 If there is no window under X, Y return nil and leave *PART
1430 unmodified. TOOL_BAR_P means detect tool-bar windows.
1432 This function was previously implemented with a loop cycling over
1433 windows with Fnext_window, and starting with the frame's selected
1434 window. It turned out that this doesn't work with an
1435 implementation of next_window using Vwindow_list, because
1436 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1437 tree of F when this function is called asynchronously from
1438 note_mouse_highlight. The original loop didn't terminate in this
1439 case. */
1441 Lisp_Object
1442 window_from_coordinates (struct frame *f, int x, int y,
1443 enum window_part *part, bool tool_bar_p)
1445 Lisp_Object window;
1446 struct check_window_data cw;
1447 enum window_part dummy;
1449 if (part == 0)
1450 part = &dummy;
1452 window = Qnil;
1453 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1454 foreach_window (f, check_window_containing, &cw);
1456 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1457 /* If not found above, see if it's in the tool bar window, if a tool
1458 bar exists. */
1459 if (NILP (window)
1460 && tool_bar_p
1461 && WINDOWP (f->tool_bar_window)
1462 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1463 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1464 != ON_NOTHING))
1466 *part = ON_TEXT;
1467 window = f->tool_bar_window;
1469 #endif
1471 return window;
1474 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1475 doc: /* Return window containing coordinates X and Y on FRAME.
1476 FRAME must be a live frame and defaults to the selected one.
1477 The top left corner of the frame is considered to be row 0,
1478 column 0. */)
1479 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1481 struct frame *f = decode_live_frame (frame);
1483 /* Check that arguments are integers or floats. */
1484 CHECK_NUMBER_OR_FLOAT (x);
1485 CHECK_NUMBER_OR_FLOAT (y);
1487 return window_from_coordinates (f,
1488 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1489 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1490 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1491 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1492 0, false);
1495 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1496 doc: /* Return current value of point in WINDOW.
1497 WINDOW must be a live window and defaults to the selected one.
1499 For a nonselected window, this is the value point would have if that
1500 window were selected.
1502 Note that, when WINDOW is selected, the value returned is the same as
1503 that returned by `point' for WINDOW's buffer. It would be more strictly
1504 correct to return the top-level value of `point', outside of any
1505 `save-excursion' forms. But that is hard to define. */)
1506 (Lisp_Object window)
1508 register struct window *w = decode_live_window (window);
1510 if (w == XWINDOW (selected_window))
1511 return make_number (BUF_PT (XBUFFER (w->contents)));
1512 else
1513 return Fmarker_position (w->pointm);
1516 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1517 doc: /* Return old value of point in WINDOW.
1518 WINDOW must be a live window and defaults to the selected one. */)
1519 (Lisp_Object window)
1521 return Fmarker_position (decode_live_window (window)->old_pointm);
1524 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1525 doc: /* Return position at which display currently starts in WINDOW.
1526 WINDOW must be a live window and defaults to the selected one.
1527 This is updated by redisplay or by calling `set-window-start'. */)
1528 (Lisp_Object window)
1530 return Fmarker_position (decode_live_window (window)->start);
1533 /* This is text temporarily removed from the doc string below.
1535 This function returns nil if the position is not currently known.
1536 That happens when redisplay is preempted and doesn't finish.
1537 If in that case you want to compute where the end of the window would
1538 have been if redisplay had finished, do this:
1539 (save-excursion
1540 (goto-char (window-start window))
1541 (vertical-motion (1- (window-height window)) window)
1542 (point))") */
1544 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1545 doc: /* Return position at which display currently ends in WINDOW.
1546 WINDOW must be a live window and defaults to the selected one.
1547 This is updated by redisplay, when it runs to completion.
1548 Simply changing the buffer text or setting `window-start'
1549 does not update this value.
1550 Return nil if there is no recorded value. (This can happen if the
1551 last redisplay of WINDOW was preempted, and did not finish.)
1552 If UPDATE is non-nil, compute the up-to-date position
1553 if it isn't already recorded. */)
1554 (Lisp_Object window, Lisp_Object update)
1556 Lisp_Object value;
1557 struct window *w = decode_live_window (window);
1558 Lisp_Object buf;
1559 struct buffer *b;
1561 buf = w->contents;
1562 CHECK_BUFFER (buf);
1563 b = XBUFFER (buf);
1565 if (! NILP (update)
1566 && (windows_or_buffers_changed
1567 || !w->window_end_valid
1568 || b->clip_changed
1569 || b->prevent_redisplay_optimizations_p
1570 || window_outdated (w))
1571 /* Don't call display routines if we didn't yet create any real
1572 frames, because the glyph matrices are not yet allocated in
1573 that case. This could happen in some code that runs in the
1574 daemon during initialization (e.g., see bug#20565). */
1575 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1577 struct text_pos startp;
1578 struct it it;
1579 struct buffer *old_buffer = NULL;
1580 void *itdata = NULL;
1582 /* Cannot use Fvertical_motion because that function doesn't
1583 cope with variable-height lines. */
1584 if (b != current_buffer)
1586 old_buffer = current_buffer;
1587 set_buffer_internal (b);
1590 /* In case W->start is out of the range, use something
1591 reasonable. This situation occurred when loading a file with
1592 `-l' containing a call to `rmail' with subsequent other
1593 commands. At the end, W->start happened to be BEG, while
1594 rmail had already narrowed the buffer. */
1595 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1597 itdata = bidi_shelve_cache ();
1598 start_display (&it, w, startp);
1599 move_it_vertically (&it, window_box_height (w));
1600 if (it.current_y < it.last_visible_y)
1601 move_it_past_eol (&it);
1602 value = make_number (IT_CHARPOS (it));
1603 bidi_unshelve_cache (itdata, false);
1605 if (old_buffer)
1606 set_buffer_internal (old_buffer);
1608 else
1609 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1611 return value;
1614 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1615 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1616 WINDOW must be a live window and defaults to the selected one.
1617 Return POS. */)
1618 (Lisp_Object window, Lisp_Object pos)
1620 register struct window *w = decode_live_window (window);
1622 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1624 if (w == XWINDOW (selected_window))
1626 if (XBUFFER (w->contents) == current_buffer)
1627 Fgoto_char (pos);
1628 else
1630 struct buffer *old_buffer = current_buffer;
1632 /* ... but here we want to catch type error before buffer change. */
1633 CHECK_NUMBER_COERCE_MARKER (pos);
1634 set_buffer_internal (XBUFFER (w->contents));
1635 Fgoto_char (pos);
1636 set_buffer_internal (old_buffer);
1639 else
1641 set_marker_restricted (w->pointm, pos, w->contents);
1642 /* We have to make sure that redisplay updates the window to show
1643 the new value of point. */
1644 wset_redisplay (w);
1647 return pos;
1650 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1651 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1652 WINDOW must be a live window and defaults to the selected one. Return
1653 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1654 overriding motion of point in order to display at this exact start. */)
1655 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1657 register struct window *w = decode_live_window (window);
1659 set_marker_restricted (w->start, pos, w->contents);
1660 /* This is not right, but much easier than doing what is right. */
1661 w->start_at_line_beg = false;
1662 if (NILP (noforce))
1663 w->force_start = true;
1664 w->update_mode_line = true;
1665 /* Bug#15957. */
1666 w->window_end_valid = false;
1667 wset_redisplay (w);
1669 return pos;
1672 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1673 Spos_visible_in_window_p, 0, 3, 0,
1674 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1675 WINDOW must be a live window and defaults to the selected one.
1677 Return nil if that position is scrolled vertically out of view. If a
1678 character is only partially visible, nil is returned, unless the
1679 optional argument PARTIALLY is non-nil. If POS is only out of view
1680 because of horizontal scrolling, return non-nil. If POS is t, it
1681 specifies the position of the last visible glyph in WINDOW. POS
1682 defaults to point in WINDOW; WINDOW defaults to the selected window.
1684 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1685 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1686 where X and Y are the pixel coordinates relative to the top left corner
1687 of the window. The remaining elements are omitted if the character after
1688 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1689 off-window at the top and bottom of the screen line ("row") containing
1690 POS, ROWH is the visible height of that row, and VPOS is the row number
1691 \(zero-based). */)
1692 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1694 struct window *w;
1695 EMACS_INT posint;
1696 struct buffer *buf;
1697 struct text_pos top;
1698 Lisp_Object in_window = Qnil;
1699 int rtop, rbot, rowh, vpos;
1700 bool fully_p = true;
1701 int x, y;
1703 w = decode_live_window (window);
1704 buf = XBUFFER (w->contents);
1705 SET_TEXT_POS_FROM_MARKER (top, w->start);
1707 if (EQ (pos, Qt))
1708 posint = -1;
1709 else if (!NILP (pos))
1711 CHECK_NUMBER_COERCE_MARKER (pos);
1712 posint = XINT (pos);
1714 else if (w == XWINDOW (selected_window))
1715 posint = PT;
1716 else
1717 posint = marker_position (w->pointm);
1719 /* If position is above window start or outside buffer boundaries,
1720 or if window start is out of range, position is not visible. */
1721 if ((EQ (pos, Qt)
1722 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1723 && CHARPOS (top) >= BUF_BEGV (buf)
1724 && CHARPOS (top) <= BUF_ZV (buf)
1725 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1727 fully_p = !rtop && !rbot;
1728 if (!NILP (partially) || fully_p)
1729 in_window = Qt;
1732 if (!NILP (in_window) && !NILP (partially))
1734 Lisp_Object part = Qnil;
1735 if (!fully_p)
1736 part = list4i (rtop, rbot, rowh, vpos);
1737 in_window = Fcons (make_number (x),
1738 Fcons (make_number (y), part));
1741 return in_window;
1744 DEFUN ("window-line-height", Fwindow_line_height,
1745 Swindow_line_height, 0, 2, 0,
1746 doc: /* Return height in pixels of text line LINE in window WINDOW.
1747 WINDOW must be a live window and defaults to the selected one.
1749 Return height of current line if LINE is omitted or nil. Return height of
1750 header or mode line if LINE is `header-line' or `mode-line'.
1751 Otherwise, LINE is a text line number starting from 0. A negative number
1752 counts from the end of the window.
1754 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1755 in pixels of the visible part of the line, VPOS and YPOS are the
1756 vertical position in lines and pixels of the line, relative to the top
1757 of the first text line, and OFFBOT is the number of off-window pixels at
1758 the bottom of the text line. If there are off-window pixels at the top
1759 of the (first) text line, YPOS is negative.
1761 Return nil if window display is not up-to-date. In that case, use
1762 `pos-visible-in-window-p' to obtain the information. */)
1763 (Lisp_Object line, Lisp_Object window)
1765 register struct window *w;
1766 register struct buffer *b;
1767 struct glyph_row *row, *end_row;
1768 int max_y, crop, i;
1769 EMACS_INT n;
1771 w = decode_live_window (window);
1773 if (noninteractive || w->pseudo_window_p)
1774 return Qnil;
1776 CHECK_BUFFER (w->contents);
1777 b = XBUFFER (w->contents);
1779 /* Fail if current matrix is not up-to-date. */
1780 if (!w->window_end_valid
1781 || windows_or_buffers_changed
1782 || b->clip_changed
1783 || b->prevent_redisplay_optimizations_p
1784 || window_outdated (w))
1785 return Qnil;
1787 if (NILP (line))
1789 i = w->cursor.vpos;
1790 if (i < 0 || i >= w->current_matrix->nrows
1791 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1792 return Qnil;
1793 max_y = window_text_bottom_y (w);
1794 goto found_row;
1797 if (EQ (line, Qheader_line))
1799 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1800 return Qnil;
1801 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1802 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1805 if (EQ (line, Qmode_line))
1807 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1808 return (row->enabled_p ?
1809 list4i (row->height,
1810 0, /* not accurate */
1811 (WINDOW_HEADER_LINE_HEIGHT (w)
1812 + window_text_bottom_y (w)),
1814 : Qnil);
1817 CHECK_NUMBER (line);
1818 n = XINT (line);
1820 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1821 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1822 max_y = window_text_bottom_y (w);
1823 i = 0;
1825 while ((n < 0 || i < n)
1826 && row <= end_row && row->enabled_p
1827 && row->y + row->height < max_y)
1828 row++, i++;
1830 if (row > end_row || !row->enabled_p)
1831 return Qnil;
1833 if (++n < 0)
1835 if (-n > i)
1836 return Qnil;
1837 row += n;
1838 i += n;
1841 found_row:
1842 crop = max (0, (row->y + row->height) - max_y);
1843 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1846 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1847 0, 1, 0,
1848 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1849 More precisely, return the value assigned by the last call of
1850 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1851 never called with WINDOW as its argument, or the value set by that
1852 function was internally reset since its last call. WINDOW must be a
1853 live window and defaults to the selected one.
1855 When a window is dedicated to its buffer, `display-buffer' will refrain
1856 from displaying another buffer in it. `get-lru-window' and
1857 `get-largest-window' treat dedicated windows specially.
1858 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1859 `kill-buffer' can delete a dedicated window and the containing frame.
1861 Functions like `set-window-buffer' may change the buffer displayed by a
1862 window, unless that window is "strongly" dedicated to its buffer, that
1863 is the value returned by `window-dedicated-p' is t. */)
1864 (Lisp_Object window)
1866 return decode_live_window (window)->dedicated;
1869 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1870 Sset_window_dedicated_p, 2, 2, 0,
1871 doc: /* Mark WINDOW as dedicated according to FLAG.
1872 WINDOW must be a live window and defaults to the selected one. FLAG
1873 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1874 mark WINDOW as non-dedicated. Return FLAG.
1876 When a window is dedicated to its buffer, `display-buffer' will refrain
1877 from displaying another buffer in it. `get-lru-window' and
1878 `get-largest-window' treat dedicated windows specially.
1879 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1880 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1881 and the containing frame.
1883 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1884 its buffer. Functions like `set-window-buffer' may change the buffer
1885 displayed by a window, unless that window is strongly dedicated to its
1886 buffer. If and when `set-window-buffer' displays another buffer in a
1887 window, it also makes sure that the window is no more dedicated. */)
1888 (Lisp_Object window, Lisp_Object flag)
1890 wset_dedicated (decode_live_window (window), flag);
1891 return flag;
1894 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1895 0, 1, 0,
1896 doc: /* Return buffers previously shown in WINDOW.
1897 WINDOW must be a live window and defaults to the selected one.
1899 The return value is a list of elements (BUFFER WINDOW-START POS),
1900 where BUFFER is a buffer, WINDOW-START is the start position of the
1901 window for that buffer, and POS is a window-specific point value. */)
1902 (Lisp_Object window)
1904 return decode_live_window (window)->prev_buffers;
1907 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1908 Sset_window_prev_buffers, 2, 2, 0,
1909 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1910 WINDOW must be a live window and defaults to the selected one.
1912 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1913 where BUFFER is a buffer, WINDOW-START is the start position of the
1914 window for that buffer, and POS is a window-specific point value. */)
1915 (Lisp_Object window, Lisp_Object prev_buffers)
1917 wset_prev_buffers (decode_live_window (window), prev_buffers);
1918 return prev_buffers;
1921 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1922 0, 1, 0,
1923 doc: /* Return list of buffers recently re-shown in WINDOW.
1924 WINDOW must be a live window and defaults to the selected one. */)
1925 (Lisp_Object window)
1927 return decode_live_window (window)->next_buffers;
1930 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1931 Sset_window_next_buffers, 2, 2, 0,
1932 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1933 WINDOW must be a live window and defaults to the selected one.
1934 NEXT-BUFFERS should be a list of buffers. */)
1935 (Lisp_Object window, Lisp_Object next_buffers)
1937 wset_next_buffers (decode_live_window (window), next_buffers);
1938 return next_buffers;
1941 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1942 0, 1, 0,
1943 doc: /* Return the parameters of WINDOW and their values.
1944 WINDOW must be a valid window and defaults to the selected one. The
1945 return value is a list of elements of the form (PARAMETER . VALUE). */)
1946 (Lisp_Object window)
1948 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1951 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1952 2, 2, 0,
1953 doc: /* Return WINDOW's value for PARAMETER.
1954 WINDOW can be any window and defaults to the selected one. */)
1955 (Lisp_Object window, Lisp_Object parameter)
1957 Lisp_Object result;
1959 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1960 return CDR_SAFE (result);
1963 DEFUN ("set-window-parameter", Fset_window_parameter,
1964 Sset_window_parameter, 3, 3, 0,
1965 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1966 WINDOW can be any window and defaults to the selected one.
1967 Return VALUE. */)
1968 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1970 register struct window *w = decode_any_window (window);
1971 Lisp_Object old_alist_elt;
1973 old_alist_elt = Fassq (parameter, w->window_parameters);
1974 if (NILP (old_alist_elt))
1975 wset_window_parameters
1976 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1977 else
1978 Fsetcdr (old_alist_elt, value);
1979 return value;
1982 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1983 0, 1, 0,
1984 doc: /* Return the display-table that WINDOW is using.
1985 WINDOW must be a live window and defaults to the selected one. */)
1986 (Lisp_Object window)
1988 return decode_live_window (window)->display_table;
1991 /* Get the display table for use on window W. This is either W's
1992 display table or W's buffer's display table. Ignore the specified
1993 tables if they are not valid; if no valid table is specified,
1994 return 0. */
1996 struct Lisp_Char_Table *
1997 window_display_table (struct window *w)
1999 struct Lisp_Char_Table *dp = NULL;
2001 if (DISP_TABLE_P (w->display_table))
2002 dp = XCHAR_TABLE (w->display_table);
2003 else if (BUFFERP (w->contents))
2005 struct buffer *b = XBUFFER (w->contents);
2007 if (DISP_TABLE_P (BVAR (b, display_table)))
2008 dp = XCHAR_TABLE (BVAR (b, display_table));
2009 else if (DISP_TABLE_P (Vstandard_display_table))
2010 dp = XCHAR_TABLE (Vstandard_display_table);
2013 return dp;
2016 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2017 doc: /* Set WINDOW's display-table to TABLE.
2018 WINDOW must be a live window and defaults to the selected one. */)
2019 (register Lisp_Object window, Lisp_Object table)
2021 wset_display_table (decode_live_window (window), table);
2022 return table;
2025 /* Record info on buffer window W is displaying
2026 when it is about to cease to display that buffer. */
2027 static void
2028 unshow_buffer (register struct window *w)
2030 Lisp_Object buf = w->contents;
2031 struct buffer *b = XBUFFER (buf);
2033 eassert (b == XMARKER (w->pointm)->buffer);
2035 #if false
2036 if (w == XWINDOW (selected_window)
2037 || ! EQ (buf, XWINDOW (selected_window)->contents))
2038 /* Do this except when the selected window's buffer
2039 is being removed from some other window. */
2040 #endif
2041 /* last_window_start records the start position that this buffer
2042 had in the last window to be disconnected from it.
2043 Now that this statement is unconditional,
2044 it is possible for the buffer to be displayed in the
2045 selected window, while last_window_start reflects another
2046 window which was recently showing the same buffer.
2047 Some people might say that might be a good thing. Let's see. */
2048 b->last_window_start = marker_position (w->start);
2050 /* Point in the selected window's buffer
2051 is actually stored in that buffer, and the window's pointm isn't used.
2052 So don't clobber point in that buffer. */
2053 if (! EQ (buf, XWINDOW (selected_window)->contents)
2054 /* Don't clobber point in current buffer either (this could be
2055 useful in connection with bug#12208).
2056 && XBUFFER (buf) != current_buffer */
2057 /* This line helps to fix Horsley's testbug.el bug. */
2058 && !(WINDOWP (BVAR (b, last_selected_window))
2059 && w != XWINDOW (BVAR (b, last_selected_window))
2060 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2061 temp_set_point_both (b,
2062 clip_to_bounds (BUF_BEGV (b),
2063 marker_position (w->pointm),
2064 BUF_ZV (b)),
2065 clip_to_bounds (BUF_BEGV_BYTE (b),
2066 marker_byte_position (w->pointm),
2067 BUF_ZV_BYTE (b)));
2069 if (WINDOWP (BVAR (b, last_selected_window))
2070 && w == XWINDOW (BVAR (b, last_selected_window)))
2071 bset_last_selected_window (b, Qnil);
2074 /* Put NEW into the window structure in place of OLD. SETFLAG false
2075 means change window structure only. Otherwise store geometry and
2076 other settings as well. */
2077 static void
2078 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2080 Lisp_Object tem;
2081 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2083 /* If OLD is its frame's root window, then NEW is the new
2084 root window for that frame. */
2085 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2086 fset_root_window (XFRAME (o->frame), new);
2088 if (setflag)
2090 n->pixel_left = o->pixel_left;
2091 n->pixel_top = o->pixel_top;
2092 n->pixel_width = o->pixel_width;
2093 n->pixel_height = o->pixel_height;
2094 n->left_col = o->left_col;
2095 n->top_line = o->top_line;
2096 n->total_cols = o->total_cols;
2097 n->total_lines = o->total_lines;
2098 wset_normal_cols (n, o->normal_cols);
2099 wset_normal_cols (o, make_float (1.0));
2100 wset_normal_lines (n, o->normal_lines);
2101 wset_normal_lines (o, make_float (1.0));
2102 n->desired_matrix = n->current_matrix = 0;
2103 n->vscroll = 0;
2104 memset (&n->cursor, 0, sizeof (n->cursor));
2105 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2106 n->last_cursor_vpos = 0;
2107 #ifdef HAVE_WINDOW_SYSTEM
2108 n->phys_cursor_type = NO_CURSOR;
2109 n->phys_cursor_width = -1;
2110 #endif
2111 n->must_be_updated_p = false;
2112 n->pseudo_window_p = false;
2113 n->window_end_vpos = 0;
2114 n->window_end_pos = 0;
2115 n->window_end_valid = false;
2118 tem = o->next;
2119 wset_next (n, tem);
2120 if (!NILP (tem))
2121 wset_prev (XWINDOW (tem), new);
2123 tem = o->prev;
2124 wset_prev (n, tem);
2125 if (!NILP (tem))
2126 wset_next (XWINDOW (tem), new);
2128 tem = o->parent;
2129 wset_parent (n, tem);
2130 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2131 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2134 /* If window WINDOW and its parent window are iso-combined, merge
2135 WINDOW's children into those of its parent window and mark WINDOW as
2136 deleted. */
2138 static void
2139 recombine_windows (Lisp_Object window)
2141 struct window *w, *p, *c;
2142 Lisp_Object parent, child;
2143 bool horflag;
2145 w = XWINDOW (window);
2146 parent = w->parent;
2147 if (!NILP (parent) && NILP (w->combination_limit))
2149 p = XWINDOW (parent);
2150 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2151 && p->horizontal == w->horizontal)
2152 /* WINDOW and PARENT are both either a vertical or a horizontal
2153 combination. */
2155 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2156 child = w->contents;
2157 c = XWINDOW (child);
2159 /* Splice WINDOW's children into its parent's children and
2160 assign new normal sizes. */
2161 if (NILP (w->prev))
2162 wset_combination (p, horflag, child);
2163 else
2165 wset_prev (c, w->prev);
2166 wset_next (XWINDOW (w->prev), child);
2169 while (c)
2171 wset_parent (c, parent);
2173 if (horflag)
2174 wset_normal_cols
2175 (c, make_float ((double) c->pixel_width
2176 / (double) p->pixel_width));
2177 else
2178 wset_normal_lines
2179 (c, make_float ((double) c->pixel_height
2180 / (double) p->pixel_height));
2182 if (NILP (c->next))
2184 if (!NILP (w->next))
2186 wset_next (c, w->next);
2187 wset_prev (XWINDOW (c->next), child);
2190 c = 0;
2192 else
2194 child = c->next;
2195 c = XWINDOW (child);
2199 /* WINDOW can be deleted now. */
2200 wset_combination (w, false, Qnil);
2205 /* If WINDOW can be deleted, delete it. */
2206 static void
2207 delete_deletable_window (Lisp_Object window)
2209 if (!NILP (call1 (Qwindow_deletable_p, window)))
2210 call1 (Qdelete_window, window);
2213 /***********************************************************************
2214 Window List
2215 ***********************************************************************/
2217 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2218 pointer. This is a callback function for foreach_window, used in
2219 the window_list function. */
2221 static bool
2222 add_window_to_list (struct window *w, void *user_data)
2224 Lisp_Object *list = user_data;
2225 Lisp_Object window;
2226 XSETWINDOW (window, w);
2227 *list = Fcons (window, *list);
2228 return true;
2232 /* Return a list of all windows, for use by next_window. If
2233 Vwindow_list is a list, return that list. Otherwise, build a new
2234 list, cache it in Vwindow_list, and return that. */
2236 Lisp_Object
2237 window_list (void)
2239 if (!CONSP (Vwindow_list))
2241 Lisp_Object tail, frame;
2243 Vwindow_list = Qnil;
2244 FOR_EACH_FRAME (tail, frame)
2246 Lisp_Object arglist = Qnil;
2248 /* We are visiting windows in canonical order, and add
2249 new windows at the front of args[1], which means we
2250 have to reverse this list at the end. */
2251 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2252 arglist = Fnreverse (arglist);
2253 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2257 return Vwindow_list;
2261 /* Value is true if WINDOW satisfies the constraints given by
2262 OWINDOW, MINIBUF and ALL_FRAMES.
2264 MINIBUF t means WINDOW may be minibuffer windows.
2265 `lambda' means WINDOW may not be a minibuffer window.
2266 a window means a specific minibuffer window
2268 ALL_FRAMES t means search all frames,
2269 nil means search just current frame,
2270 `visible' means search just visible frames on the
2271 current terminal,
2272 0 means search visible and iconified frames on the
2273 current terminal,
2274 a window means search the frame that window belongs to,
2275 a frame means consider windows on that frame, only. */
2277 static bool
2278 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2279 Lisp_Object minibuf, Lisp_Object all_frames)
2281 struct window *w = XWINDOW (window);
2282 struct frame *f = XFRAME (w->frame);
2283 bool candidate_p = true;
2285 if (!BUFFERP (w->contents))
2286 candidate_p = false;
2287 else if (MINI_WINDOW_P (w)
2288 && (EQ (minibuf, Qlambda)
2289 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2291 /* If MINIBUF is `lambda' don't consider any mini-windows.
2292 If it is a window, consider only that one. */
2293 candidate_p = false;
2295 else if (EQ (all_frames, Qt))
2296 candidate_p = true;
2297 else if (NILP (all_frames))
2299 eassert (WINDOWP (owindow));
2300 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2302 else if (EQ (all_frames, Qvisible))
2304 candidate_p = FRAME_VISIBLE_P (f)
2305 && (FRAME_TERMINAL (XFRAME (w->frame))
2306 == FRAME_TERMINAL (XFRAME (selected_frame)));
2309 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2311 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2312 #ifdef HAVE_X_WINDOWS
2313 /* Yuck!! If we've just created the frame and the
2314 window-manager requested the user to place it
2315 manually, the window may still not be considered
2316 `visible'. I'd argue it should be at least
2317 something like `iconified', but don't know how to do
2318 that yet. --Stef */
2319 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2320 && !f->output_data.x->has_been_visible)
2321 #endif
2323 && (FRAME_TERMINAL (XFRAME (w->frame))
2324 == FRAME_TERMINAL (XFRAME (selected_frame)));
2326 else if (WINDOWP (all_frames))
2327 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2328 || EQ (XWINDOW (all_frames)->frame, w->frame)
2329 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2330 else if (FRAMEP (all_frames))
2331 candidate_p = EQ (all_frames, w->frame);
2333 return candidate_p;
2337 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2338 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2339 MINIBUF, and ALL_FRAMES. */
2341 static void
2342 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2344 struct window *w = decode_live_window (*window);
2346 XSETWINDOW (*window, w);
2347 /* MINIBUF nil may or may not include minibuffers. Decide if it
2348 does. */
2349 if (NILP (*minibuf))
2350 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2351 else if (!EQ (*minibuf, Qt))
2352 *minibuf = Qlambda;
2354 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2355 => count none of them, or a specific minibuffer window (the
2356 active one) to count. */
2358 /* ALL_FRAMES nil doesn't specify which frames to include. */
2359 if (NILP (*all_frames))
2360 *all_frames
2361 = (!EQ (*minibuf, Qlambda)
2362 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2363 : Qnil);
2364 else if (EQ (*all_frames, Qvisible))
2366 else if (EQ (*all_frames, make_number (0)))
2368 else if (FRAMEP (*all_frames))
2370 else if (!EQ (*all_frames, Qt))
2371 *all_frames = Qnil;
2375 /* Return the next or previous window of WINDOW in cyclic ordering
2376 of windows. NEXT_P means return the next window. See the
2377 documentation string of next-window for the meaning of MINIBUF and
2378 ALL_FRAMES. */
2380 static Lisp_Object
2381 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2382 bool next_p)
2384 decode_next_window_args (&window, &minibuf, &all_frames);
2386 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2387 return the first window on the frame. */
2388 if (FRAMEP (all_frames)
2389 && !EQ (all_frames, XWINDOW (window)->frame))
2390 return Fframe_first_window (all_frames);
2392 if (next_p)
2394 Lisp_Object list;
2396 /* Find WINDOW in the list of all windows. */
2397 list = Fmemq (window, window_list ());
2399 /* Scan forward from WINDOW to the end of the window list. */
2400 if (CONSP (list))
2401 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2402 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2403 break;
2405 /* Scan from the start of the window list up to WINDOW. */
2406 if (!CONSP (list))
2407 for (list = Vwindow_list;
2408 CONSP (list) && !EQ (XCAR (list), window);
2409 list = XCDR (list))
2410 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2411 break;
2413 if (CONSP (list))
2414 window = XCAR (list);
2416 else
2418 Lisp_Object candidate, list;
2420 /* Scan through the list of windows for candidates. If there are
2421 candidate windows in front of WINDOW, the last one of these
2422 is the one we want. If there are candidates following WINDOW
2423 in the list, again the last one of these is the one we want. */
2424 candidate = Qnil;
2425 for (list = window_list (); CONSP (list); list = XCDR (list))
2427 if (EQ (XCAR (list), window))
2429 if (WINDOWP (candidate))
2430 break;
2432 else if (candidate_window_p (XCAR (list), window, minibuf,
2433 all_frames))
2434 candidate = XCAR (list);
2437 if (WINDOWP (candidate))
2438 window = candidate;
2441 return window;
2445 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2446 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2447 WINDOW must be a live window and defaults to the selected one. The
2448 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2449 consider.
2451 MINIBUF nil or omitted means consider the minibuffer window only if the
2452 minibuffer is active. MINIBUF t means consider the minibuffer window
2453 even if the minibuffer is not active. Any other value means do not
2454 consider the minibuffer window even if the minibuffer is active.
2456 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2457 plus the minibuffer window if specified by the MINIBUF argument. If the
2458 minibuffer counts, consider all windows on all frames that share that
2459 minibuffer too. The following non-nil values of ALL-FRAMES have special
2460 meanings:
2462 - t means consider all windows on all existing frames.
2464 - `visible' means consider all windows on all visible frames.
2466 - 0 (the number zero) means consider all windows on all visible and
2467 iconified frames.
2469 - A frame means consider all windows on that frame only.
2471 Anything else means consider all windows on WINDOW's frame and no
2472 others.
2474 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2475 `next-window' to iterate through the entire cycle of acceptable
2476 windows, eventually ending up back at the window you started with.
2477 `previous-window' traverses the same cycle, in the reverse order. */)
2478 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2480 return next_window (window, minibuf, all_frames, true);
2484 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2485 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2486 WINDOW must be a live window and defaults to the selected one. The
2487 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2488 consider.
2490 MINIBUF nil or omitted means consider the minibuffer window only if the
2491 minibuffer is active. MINIBUF t means consider the minibuffer window
2492 even if the minibuffer is not active. Any other value means do not
2493 consider the minibuffer window even if the minibuffer is active.
2495 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2496 plus the minibuffer window if specified by the MINIBUF argument. If the
2497 minibuffer counts, consider all windows on all frames that share that
2498 minibuffer too. The following non-nil values of ALL-FRAMES have special
2499 meanings:
2501 - t means consider all windows on all existing frames.
2503 - `visible' means consider all windows on all visible frames.
2505 - 0 (the number zero) means consider all windows on all visible and
2506 iconified frames.
2508 - A frame means consider all windows on that frame only.
2510 Anything else means consider all windows on WINDOW's frame and no
2511 others.
2513 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2514 use `previous-window' to iterate through the entire cycle of
2515 acceptable windows, eventually ending up back at the window you
2516 started with. `next-window' traverses the same cycle, in the
2517 reverse order. */)
2518 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2520 return next_window (window, minibuf, all_frames, false);
2524 /* Return a list of windows in cyclic ordering. Arguments are like
2525 for `next-window'. */
2527 static Lisp_Object
2528 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2530 Lisp_Object tail, list, rest;
2532 decode_next_window_args (&window, &minibuf, &all_frames);
2533 list = Qnil;
2535 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2536 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2537 list = Fcons (XCAR (tail), list);
2539 /* Rotate the list to start with WINDOW. */
2540 list = Fnreverse (list);
2541 rest = Fmemq (window, list);
2542 if (!NILP (rest) && !EQ (rest, list))
2544 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2546 XSETCDR (tail, Qnil);
2547 list = nconc2 (rest, list);
2549 return list;
2553 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2554 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2555 FRAME nil or omitted means use the selected frame.
2556 WINDOW nil or omitted means use the window selected within FRAME.
2557 MINIBUF t means include the minibuffer window, even if it isn't active.
2558 MINIBUF nil or omitted means include the minibuffer window only
2559 if it's active.
2560 MINIBUF neither nil nor t means never include the minibuffer window. */)
2561 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2563 if (NILP (window))
2564 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2565 CHECK_WINDOW (window);
2566 if (NILP (frame))
2567 frame = selected_frame;
2569 if (!EQ (frame, XWINDOW (window)->frame))
2570 error ("Window is on a different frame");
2572 return window_list_1 (window, minibuf, frame);
2576 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2577 doc: /* Return a list of all live windows.
2578 WINDOW specifies the first window to list and defaults to the selected
2579 window.
2581 Optional argument MINIBUF nil or omitted means consider the minibuffer
2582 window only if the minibuffer is active. MINIBUF t means consider the
2583 minibuffer window even if the minibuffer is not active. Any other value
2584 means do not consider the minibuffer window even if the minibuffer is
2585 active.
2587 Optional argument ALL-FRAMES nil or omitted means consider all windows
2588 on WINDOW's frame, plus the minibuffer window if specified by the
2589 MINIBUF argument. If the minibuffer counts, consider all windows on all
2590 frames that share that minibuffer too. The following non-nil values of
2591 ALL-FRAMES have special meanings:
2593 - t means consider all windows on all existing frames.
2595 - `visible' means consider all windows on all visible frames.
2597 - 0 (the number zero) means consider all windows on all visible and
2598 iconified frames.
2600 - A frame means consider all windows on that frame only.
2602 Anything else means consider all windows on WINDOW's frame and no
2603 others.
2605 If WINDOW is not on the list of windows returned, some other window will
2606 be listed first but no error is signaled. */)
2607 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2609 return window_list_1 (window, minibuf, all_frames);
2612 /* Look at all windows, performing an operation specified by TYPE
2613 with argument OBJ.
2614 If FRAMES is Qt, look at all frames;
2615 Qnil, look at just the selected frame;
2616 Qvisible, look at visible frames;
2617 a frame, just look at windows on that frame.
2618 If MINI, perform the operation on minibuffer windows too. */
2620 enum window_loop
2622 WINDOW_LOOP_UNUSED,
2623 GET_BUFFER_WINDOW, /* Arg is buffer */
2624 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2625 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2626 CHECK_ALL_WINDOWS /* Arg is ignored */
2629 static Lisp_Object
2630 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2631 Lisp_Object frames)
2633 Lisp_Object window, windows, best_window, frame_arg;
2634 bool frame_best_window_flag = false;
2635 struct frame *f;
2637 /* If we're only looping through windows on a particular frame,
2638 frame points to that frame. If we're looping through windows
2639 on all frames, frame is 0. */
2640 if (FRAMEP (frames))
2641 f = XFRAME (frames);
2642 else if (NILP (frames))
2643 f = SELECTED_FRAME ();
2644 else
2645 f = NULL;
2647 if (f)
2648 frame_arg = Qlambda;
2649 else if (EQ (frames, make_number (0)))
2650 frame_arg = frames;
2651 else if (EQ (frames, Qvisible))
2652 frame_arg = frames;
2653 else
2654 frame_arg = Qt;
2656 /* frame_arg is Qlambda to stick to one frame,
2657 Qvisible to consider all visible frames,
2658 or Qt otherwise. */
2660 /* Pick a window to start with. */
2661 if (WINDOWP (obj))
2662 window = obj;
2663 else if (f)
2664 window = FRAME_SELECTED_WINDOW (f);
2665 else
2666 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2668 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2669 best_window = Qnil;
2671 for (; CONSP (windows); windows = XCDR (windows))
2673 struct window *w;
2675 window = XCAR (windows);
2676 w = XWINDOW (window);
2678 /* Note that we do not pay attention here to whether the frame
2679 is visible, since Fwindow_list skips non-visible frames if
2680 that is desired, under the control of frame_arg. */
2681 if (!MINI_WINDOW_P (w)
2682 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2683 consider all windows. */
2684 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2685 || (mini && minibuf_level > 0))
2686 switch (type)
2688 case GET_BUFFER_WINDOW:
2689 if (EQ (w->contents, obj)
2690 /* Don't find any minibuffer window except the one that
2691 is currently in use. */
2692 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2694 if (EQ (window, selected_window))
2695 /* Preferably return the selected window. */
2696 return window;
2697 else if (EQ (XWINDOW (window)->frame, selected_frame)
2698 && !frame_best_window_flag)
2699 /* Prefer windows on the current frame (but don't
2700 choose another one if we have one already). */
2702 best_window = window;
2703 frame_best_window_flag = true;
2705 else if (NILP (best_window))
2706 best_window = window;
2708 break;
2710 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2711 /* We could simply check whether the buffer shown by window
2712 is live, and show another buffer in case it isn't. */
2713 if (EQ (w->contents, obj))
2715 /* Undedicate WINDOW. */
2716 wset_dedicated (w, Qnil);
2717 /* Make WINDOW show the buffer returned by
2718 other_buffer_safely, don't run any hooks. */
2719 set_window_buffer
2720 (window, other_buffer_safely (w->contents), false, false);
2721 /* If WINDOW is the selected window, make its buffer
2722 current. But do so only if the window shows the
2723 current buffer (Bug#6454). */
2724 if (EQ (window, selected_window)
2725 && XBUFFER (w->contents) == current_buffer)
2726 Fset_buffer (w->contents);
2728 break;
2730 case REDISPLAY_BUFFER_WINDOWS:
2731 if (EQ (w->contents, obj))
2733 mark_window_display_accurate (window, false);
2734 w->update_mode_line = true;
2735 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2736 update_mode_lines = 27;
2737 best_window = window;
2739 break;
2741 /* Check for a leaf window that has a killed buffer
2742 or broken markers. */
2743 case CHECK_ALL_WINDOWS:
2744 if (BUFFERP (w->contents))
2746 struct buffer *b = XBUFFER (w->contents);
2748 if (!BUFFER_LIVE_P (b))
2749 emacs_abort ();
2750 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2751 emacs_abort ();
2752 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2753 emacs_abort ();
2755 break;
2757 case WINDOW_LOOP_UNUSED:
2758 break;
2762 return best_window;
2765 /* Used for debugging. Abort if any window has a dead buffer. */
2767 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2768 void
2769 check_all_windows (void)
2771 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2774 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2775 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2776 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2777 the current buffer.
2779 The optional argument ALL-FRAMES specifies the frames to consider:
2781 - t means consider all windows on all existing frames.
2783 - `visible' means consider all windows on all visible frames.
2785 - 0 (the number zero) means consider all windows on all visible
2786 and iconified frames.
2788 - A frame means consider all windows on that frame only.
2790 Any other value of ALL-FRAMES means consider all windows on the
2791 selected frame and no others. */)
2792 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2794 Lisp_Object buffer;
2796 if (NILP (buffer_or_name))
2797 buffer = Fcurrent_buffer ();
2798 else
2799 buffer = Fget_buffer (buffer_or_name);
2801 if (BUFFERP (buffer))
2802 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2803 else
2804 return Qnil;
2808 static Lisp_Object
2809 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2811 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2814 /* Placeholder used by temacs -nw before window.el is loaded. */
2815 DEFUN ("window--sanitize-window-sizes", Fwindow__sanitize_window_sizes,
2816 Swindow__sanitize_window_sizes, 2, 2, 0,
2817 doc: /* */
2818 attributes: const)
2819 (Lisp_Object frame, Lisp_Object horizontal)
2821 return Qnil;
2824 Lisp_Object
2825 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
2827 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
2831 static Lisp_Object
2832 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2834 return call2 (Qwindow_pixel_to_total, frame, horizontal);
2838 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2839 Sdelete_other_windows_internal, 0, 2, "",
2840 doc: /* Make WINDOW fill its frame.
2841 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2842 and defaults to the selected one.
2844 Optional argument ROOT, if non-nil, must specify an internal window such
2845 that WINDOW is in its window subtree. If this is the case, replace ROOT
2846 by WINDOW and leave alone any windows not part of ROOT's subtree.
2848 When WINDOW is live try to reduce display jumps by keeping the text
2849 previously visible in WINDOW in the same place on the frame. Doing this
2850 depends on the value of (window-start WINDOW), so if calling this
2851 function in a program gives strange scrolling, make sure the
2852 window-start value is reasonable when this function is called. */)
2853 (Lisp_Object window, Lisp_Object root)
2855 struct window *w, *r, *s;
2856 struct frame *f;
2857 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2858 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2859 int top IF_LINT (= 0), new_top;
2861 w = decode_valid_window (window);
2862 XSETWINDOW (window, w);
2863 f = XFRAME (w->frame);
2865 if (NILP (root))
2866 /* ROOT is the frame's root window. */
2868 root = FRAME_ROOT_WINDOW (f);
2869 r = XWINDOW (root);
2871 else
2872 /* ROOT must be an ancestor of WINDOW. */
2874 r = decode_valid_window (root);
2875 pwindow = XWINDOW (window)->parent;
2876 while (!NILP (pwindow))
2877 if (EQ (pwindow, root))
2878 break;
2879 else
2880 pwindow = XWINDOW (pwindow)->parent;
2881 if (!EQ (pwindow, root))
2882 error ("Specified root is not an ancestor of specified window");
2885 if (EQ (window, root))
2886 /* A noop. */
2887 return Qnil;
2888 /* I don't understand the "top > 0" part below. If we deal with a
2889 standalone minibuffer it would have been caught by the preceding
2890 test. */
2891 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2892 error ("Can't expand minibuffer to full frame");
2894 if (BUFFERP (w->contents))
2896 startpos = marker_position (w->start);
2897 startbyte = marker_byte_position (w->start);
2898 top = (WINDOW_TOP_EDGE_LINE (w)
2899 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2900 /* Make sure WINDOW is the frame's selected window. */
2901 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2903 if (EQ (selected_frame, w->frame))
2904 Fselect_window (window, Qnil);
2905 else
2906 fset_selected_window (f, window);
2909 else
2911 /* See if the frame's selected window is a part of the window
2912 subtree rooted at WINDOW, by finding all the selected window's
2913 parents and comparing each one with WINDOW. If it isn't we
2914 need a new selected window for this frame. */
2915 swindow = FRAME_SELECTED_WINDOW (f);
2916 while (true)
2918 pwindow = swindow;
2919 while (!NILP (pwindow) && !EQ (window, pwindow))
2920 pwindow = XWINDOW (pwindow)->parent;
2922 if (EQ (window, pwindow))
2923 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2924 as the new selected window. */
2925 break;
2926 else
2927 /* Else try the previous window of SWINDOW. */
2928 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2931 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2933 if (EQ (selected_frame, w->frame))
2934 Fselect_window (swindow, Qnil);
2935 else
2936 fset_selected_window (f, swindow);
2940 block_input ();
2941 if (!FRAME_INITIAL_P (f))
2943 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2945 /* We are going to free the glyph matrices of WINDOW, and with
2946 that we might lose any information about glyph rows that have
2947 some of their glyphs highlighted in mouse face. (These rows
2948 are marked with a mouse_face_p flag.) If WINDOW
2949 indeed has some glyphs highlighted in mouse face, signal to
2950 frame's up-to-date hook that mouse highlight was overwritten,
2951 so that it will arrange for redisplaying the highlight. */
2952 if (EQ (hlinfo->mouse_face_window, window))
2953 reset_mouse_highlight (hlinfo);
2955 free_window_matrices (r);
2957 fset_redisplay (f);
2958 Vwindow_list = Qnil;
2959 FRAME_WINDOW_SIZES_CHANGED (f) = true;
2960 bool resize_failed = false;
2962 if (!WINDOW_LEAF_P (w))
2964 /* Resize child windows vertically. */
2965 XSETINT (delta, r->pixel_height - w->pixel_height);
2966 w->pixel_top = r->pixel_top;
2967 w->top_line = r->top_line;
2968 resize_root_window (window, delta, Qnil, Qnil, Qt);
2969 if (window_resize_check (w, false))
2971 window_resize_apply (w, false);
2972 window_pixel_to_total (w->frame, Qnil);
2974 else
2976 resize_root_window (window, delta, Qnil, Qt, Qt);
2977 if (window_resize_check (w, false))
2979 window_resize_apply (w, false);
2980 window_pixel_to_total (w->frame, Qnil);
2982 else
2983 resize_failed = true;
2986 /* Resize child windows horizontally. */
2987 if (!resize_failed)
2989 w->left_col = r->left_col;
2990 w->pixel_left = r->pixel_left;
2991 XSETINT (delta, r->pixel_width - w->pixel_width);
2992 resize_root_window (window, delta, Qt, Qnil, Qt);
2993 if (window_resize_check (w, true))
2995 window_resize_apply (w, true);
2996 window_pixel_to_total (w->frame, Qt);
2998 else
3000 resize_root_window (window, delta, Qt, Qt, Qt);
3001 if (window_resize_check (w, true))
3003 window_resize_apply (w, true);
3004 window_pixel_to_total (w->frame, Qt);
3006 else
3007 resize_failed = true;
3011 if (resize_failed)
3012 /* Play safe, if we still can ... */
3014 window = swindow;
3015 w = XWINDOW (window);
3019 /* Cleanly unlink WINDOW from window-tree. */
3020 if (!NILP (w->prev))
3021 /* Get SIBLING above (on the left of) WINDOW. */
3023 sibling = w->prev;
3024 s = XWINDOW (sibling);
3025 wset_next (s, w->next);
3026 if (!NILP (s->next))
3027 wset_prev (XWINDOW (s->next), sibling);
3029 else
3030 /* Get SIBLING below (on the right of) WINDOW. */
3032 sibling = w->next;
3033 s = XWINDOW (sibling);
3034 wset_prev (s, Qnil);
3035 wset_combination (XWINDOW (w->parent),
3036 XWINDOW (w->parent)->horizontal, sibling);
3039 /* Delete ROOT and all child windows of ROOT. */
3040 if (WINDOWP (r->contents))
3042 delete_all_child_windows (r->contents);
3043 wset_combination (r, false, Qnil);
3046 replace_window (root, window, true);
3048 /* This must become SWINDOW anyway ....... */
3049 if (BUFFERP (w->contents) && !resize_failed)
3051 /* Try to minimize scrolling, by setting the window start to the
3052 point will cause the text at the old window start to be at the
3053 same place on the frame. But don't try to do this if the
3054 window start is outside the visible portion (as might happen
3055 when the display is not current, due to typeahead). */
3056 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3057 if (new_top != top
3058 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3059 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3061 struct position pos;
3062 struct buffer *obuf = current_buffer;
3064 Fset_buffer (w->contents);
3065 /* This computation used to temporarily move point, but that
3066 can have unwanted side effects due to text properties. */
3067 pos = *vmotion (startpos, startbyte, -top, w);
3069 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3070 w->window_end_valid = false;
3071 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3072 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3073 /* We need to do this, so that the window-scroll-functions
3074 get called. */
3075 w->optional_new_start = true;
3077 set_buffer_internal (obuf);
3081 adjust_frame_glyphs (f);
3082 unblock_input ();
3084 run_window_configuration_change_hook (f);
3086 return Qnil;
3090 void
3091 replace_buffer_in_windows (Lisp_Object buffer)
3093 call1 (Qreplace_buffer_in_windows, buffer);
3096 /* If BUFFER is shown in a window, safely replace it with some other
3097 buffer in all windows of all frames, even those on other keyboards. */
3099 void
3100 replace_buffer_in_windows_safely (Lisp_Object buffer)
3102 if (buffer_window_count (XBUFFER (buffer)))
3104 Lisp_Object tail, frame;
3106 /* A single call to window_loop won't do the job because it only
3107 considers frames on the current keyboard. So loop manually over
3108 frames, and handle each one. */
3109 FOR_EACH_FRAME (tail, frame)
3110 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3114 /* The following three routines are needed for running a window's
3115 configuration change hook. */
3116 static void
3117 run_funs (Lisp_Object funs)
3119 for (; CONSP (funs); funs = XCDR (funs))
3120 if (!EQ (XCAR (funs), Qt))
3121 call0 (XCAR (funs));
3124 static void
3125 select_window_norecord (Lisp_Object window)
3127 if (WINDOW_LIVE_P (window))
3128 Fselect_window (window, Qt);
3131 static void
3132 select_frame_norecord (Lisp_Object frame)
3134 if (FRAME_LIVE_P (XFRAME (frame)))
3135 Fselect_frame (frame, Qt);
3138 void
3139 run_window_configuration_change_hook (struct frame *f)
3141 ptrdiff_t count = SPECPDL_INDEX ();
3142 Lisp_Object frame, global_wcch
3143 = Fdefault_value (Qwindow_configuration_change_hook);
3144 XSETFRAME (frame, f);
3146 if (NILP (Vrun_hooks)
3147 || !(f->can_x_set_window_size)
3148 || !(f->after_make_frame))
3149 return;
3151 /* Use the right buffer. Matters when running the local hooks. */
3152 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3154 record_unwind_current_buffer ();
3155 Fset_buffer (Fwindow_buffer (Qnil));
3158 if (SELECTED_FRAME () != f)
3160 record_unwind_protect (select_frame_norecord, selected_frame);
3161 select_frame_norecord (frame);
3164 /* Look for buffer-local values. */
3166 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3167 for (; CONSP (windows); windows = XCDR (windows))
3169 Lisp_Object window = XCAR (windows);
3170 Lisp_Object buffer = Fwindow_buffer (window);
3171 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3172 buffer)))
3174 ptrdiff_t inner_count = SPECPDL_INDEX ();
3175 record_unwind_protect (select_window_norecord, selected_window);
3176 select_window_norecord (window);
3177 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3178 buffer));
3179 unbind_to (inner_count, Qnil);
3184 run_funs (global_wcch);
3185 unbind_to (count, Qnil);
3188 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3189 Srun_window_configuration_change_hook, 0, 1, 0,
3190 doc: /* Run `window-configuration-change-hook' for FRAME.
3191 If FRAME is omitted or nil, it defaults to the selected frame. */)
3192 (Lisp_Object frame)
3194 run_window_configuration_change_hook (decode_live_frame (frame));
3195 return Qnil;
3198 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3199 Srun_window_scroll_functions, 0, 1, 0,
3200 doc: /* Run `window-scroll-functions' for WINDOW.
3201 If WINDOW is omitted or nil, it defaults to the selected window. */)
3202 (Lisp_Object window)
3204 if (! NILP (Vwindow_scroll_functions))
3205 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3206 Fmarker_position (decode_live_window (window)->start));
3207 return Qnil;
3210 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3211 to run hooks. See make_frame for a case where it's not allowed.
3212 KEEP_MARGINS_P means that the current margins, fringes, and
3213 scroll-bar settings of the window are not reset from the buffer's
3214 local settings. */
3216 void
3217 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3218 bool run_hooks_p, bool keep_margins_p)
3220 struct window *w = XWINDOW (window);
3221 struct buffer *b = XBUFFER (buffer);
3222 ptrdiff_t count = SPECPDL_INDEX ();
3223 bool samebuf = EQ (buffer, w->contents);
3225 wset_buffer (w, buffer);
3227 if (EQ (window, selected_window))
3228 bset_last_selected_window (b, window);
3230 /* Let redisplay errors through. */
3231 b->display_error_modiff = 0;
3233 /* Update time stamps of buffer display. */
3234 if (INTEGERP (BVAR (b, display_count)))
3235 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3236 bset_display_time (b, Fcurrent_time ());
3238 w->window_end_pos = 0;
3239 w->window_end_vpos = 0;
3240 w->last_cursor_vpos = 0;
3242 if (!(keep_margins_p && samebuf))
3243 { /* If we're not actually changing the buffer, don't reset hscroll
3244 and vscroll. This case happens for example when called from
3245 change_frame_size_1, where we use a dummy call to
3246 Fset_window_buffer on the frame's selected window (and no
3247 other) just in order to run window-configuration-change-hook
3248 (no longer true since change_frame_size_1 directly calls
3249 run_window_configuration_change_hook). Resetting hscroll and
3250 vscroll here is problematic for things like image-mode and
3251 doc-view-mode since it resets the image's position whenever we
3252 resize the frame. */
3253 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3254 w->suspend_auto_hscroll = false;
3255 w->vscroll = 0;
3256 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3257 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3258 set_marker_restricted (w->start,
3259 make_number (b->last_window_start),
3260 buffer);
3261 w->start_at_line_beg = false;
3262 w->force_start = false;
3264 /* Maybe we could move this into the `if' but it's not obviously safe and
3265 I doubt it's worth the trouble. */
3266 wset_redisplay (w);
3267 w->update_mode_line = true;
3269 /* We must select BUFFER to run the window-scroll-functions and to look up
3270 the buffer-local value of Vwindow_point_insertion_type. */
3271 record_unwind_current_buffer ();
3272 Fset_buffer (buffer);
3274 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3275 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3277 if (!keep_margins_p)
3279 /* Set left and right marginal area width etc. from buffer. */
3280 set_window_fringes (w, BVAR (b, left_fringe_width),
3281 BVAR (b, right_fringe_width),
3282 BVAR (b, fringes_outside_margins));
3283 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3284 BVAR (b, vertical_scroll_bar_type),
3285 BVAR (b, scroll_bar_height),
3286 BVAR (b, horizontal_scroll_bar_type));
3287 set_window_margins (w, BVAR (b, left_margin_cols),
3288 BVAR (b, right_margin_cols));
3289 apply_window_adjustment (w);
3292 if (run_hooks_p)
3294 if (! NILP (Vwindow_scroll_functions))
3295 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3296 Fmarker_position (w->start));
3297 if (!samebuf)
3298 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3301 unbind_to (count, Qnil);
3304 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3305 doc: /* Make WINDOW display BUFFER-OR-NAME.
3306 WINDOW must be a live window and defaults to the selected one.
3307 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3309 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3310 display margins, fringe widths, and scroll bar settings are preserved;
3311 the default is to reset these from the local settings for BUFFER-OR-NAME
3312 or the frame defaults. Return nil.
3314 This function throws an error when WINDOW is strongly dedicated to its
3315 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3316 already display BUFFER-OR-NAME.
3318 This function runs `window-scroll-functions' before running
3319 `window-configuration-change-hook'. */)
3320 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3322 register Lisp_Object tem, buffer;
3323 register struct window *w = decode_live_window (window);
3325 XSETWINDOW (window, w);
3326 buffer = Fget_buffer (buffer_or_name);
3327 CHECK_BUFFER (buffer);
3328 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3329 error ("Attempt to display deleted buffer");
3331 tem = w->contents;
3332 if (NILP (tem))
3333 error ("Window is deleted");
3334 else
3336 if (!EQ (tem, buffer))
3338 if (EQ (w->dedicated, Qt))
3339 /* WINDOW is strongly dedicated to its buffer, signal an
3340 error. */
3341 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3342 else
3343 /* WINDOW is weakly dedicated to its buffer, reset
3344 dedication. */
3345 wset_dedicated (w, Qnil);
3347 call1 (Qrecord_window_buffer, window);
3350 unshow_buffer (w);
3353 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3355 return Qnil;
3358 static Lisp_Object
3359 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3361 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3364 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3365 0, 1, 0,
3366 doc: /* Force all windows to be updated on next redisplay.
3367 If optional arg OBJECT is a window, force redisplay of that window only.
3368 If OBJECT is a buffer or buffer name, force redisplay of all windows
3369 displaying that buffer. */)
3370 (Lisp_Object object)
3372 if (NILP (object))
3374 windows_or_buffers_changed = 29;
3375 update_mode_lines = 28;
3376 return Qt;
3379 if (WINDOWP (object))
3381 struct window *w = XWINDOW (object);
3382 mark_window_display_accurate (object, false);
3383 w->update_mode_line = true;
3384 if (BUFFERP (w->contents))
3385 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3386 update_mode_lines = 29;
3387 return Qt;
3390 if (STRINGP (object))
3391 object = Fget_buffer (object);
3392 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3393 && buffer_window_count (XBUFFER (object)))
3395 /* If buffer is live and shown in at least one window, find
3396 all windows showing this buffer and force update of them. */
3397 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3398 return NILP (object) ? Qnil : Qt;
3401 /* If nothing suitable was found, just return.
3402 We could signal an error, but this feature will typically be used
3403 asynchronously in timers or process sentinels, so we don't. */
3404 return Qnil;
3407 /* Obsolete since 24.3. */
3408 void
3409 temp_output_buffer_show (register Lisp_Object buf)
3411 register struct buffer *old = current_buffer;
3412 register Lisp_Object window;
3413 register struct window *w;
3415 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3417 Fset_buffer (buf);
3418 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3419 BEGV = BEG;
3420 ZV = Z;
3421 SET_PT (BEG);
3422 set_buffer_internal (old);
3424 if (!NILP (Vtemp_buffer_show_function))
3425 call1 (Vtemp_buffer_show_function, buf);
3426 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3428 if (!EQ (XWINDOW (window)->frame, selected_frame))
3429 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3430 Vminibuf_scroll_window = window;
3431 w = XWINDOW (window);
3432 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3433 w->suspend_auto_hscroll = false;
3434 set_marker_restricted_both (w->start, buf, BEG, BEG);
3435 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3436 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3438 /* Run temp-buffer-show-hook, with the chosen window selected
3439 and its buffer current. */
3441 ptrdiff_t count = SPECPDL_INDEX ();
3442 Lisp_Object prev_window, prev_buffer;
3443 prev_window = selected_window;
3444 XSETBUFFER (prev_buffer, old);
3446 /* Select the window that was chosen, for running the hook.
3447 Note: Both Fselect_window and select_window_norecord may
3448 set-buffer to the buffer displayed in the window,
3449 so we need to save the current buffer. --stef */
3450 record_unwind_protect (restore_buffer, prev_buffer);
3451 record_unwind_protect (select_window_norecord, prev_window);
3452 Fselect_window (window, Qt);
3453 Fset_buffer (w->contents);
3454 run_hook (Qtemp_buffer_show_hook);
3455 unbind_to (count, Qnil);
3460 /* Allocate basically initialized window. */
3462 static struct window *
3463 allocate_window (void)
3465 return ALLOCATE_ZEROED_PSEUDOVECTOR
3466 (struct window, current_matrix, PVEC_WINDOW);
3469 /* Make new window, have it replace WINDOW in window-tree, and make
3470 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3471 horizontal child). */
3472 static void
3473 make_parent_window (Lisp_Object window, bool horflag)
3475 Lisp_Object parent;
3476 register struct window *o, *p;
3478 o = XWINDOW (window);
3479 p = allocate_window ();
3480 memcpy ((char *) p + sizeof (struct vectorlike_header),
3481 (char *) o + sizeof (struct vectorlike_header),
3482 word_size * VECSIZE (struct window));
3483 /* P's buffer slot may change from nil to a buffer... */
3484 adjust_window_count (p, 1);
3485 XSETWINDOW (parent, p);
3487 p->sequence_number = ++sequence_number;
3489 replace_window (window, parent, true);
3491 wset_next (o, Qnil);
3492 wset_prev (o, Qnil);
3493 wset_parent (o, parent);
3494 /* ...but now P becomes an internal window. */
3495 wset_start (p, Qnil);
3496 wset_pointm (p, Qnil);
3497 wset_old_pointm (p, Qnil);
3498 wset_buffer (p, Qnil);
3499 wset_combination (p, horflag, window);
3500 wset_combination_limit (p, Qnil);
3501 wset_window_parameters (p, Qnil);
3504 /* Make new window from scratch. */
3505 Lisp_Object
3506 make_window (void)
3508 Lisp_Object window;
3509 register struct window *w;
3511 w = allocate_window ();
3512 /* Initialize Lisp data. Note that allocate_window initializes all
3513 Lisp data to nil, so do it only for slots which should not be nil. */
3514 wset_normal_lines (w, make_float (1.0));
3515 wset_normal_cols (w, make_float (1.0));
3516 wset_new_total (w, make_number (0));
3517 wset_new_normal (w, make_number (0));
3518 wset_new_pixel (w, make_number (0));
3519 wset_start (w, Fmake_marker ());
3520 wset_pointm (w, Fmake_marker ());
3521 wset_old_pointm (w, Fmake_marker ());
3522 wset_vertical_scroll_bar_type (w, Qt);
3523 wset_horizontal_scroll_bar_type (w, Qt);
3524 /* These Lisp fields are marked specially so they're not set to nil by
3525 allocate_window. */
3526 wset_prev_buffers (w, Qnil);
3527 wset_next_buffers (w, Qnil);
3529 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3530 non-Lisp data, so do it only for slots which should not be zero. */
3531 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3532 w->left_fringe_width = w->right_fringe_width = -1;
3533 w->mode_line_height = w->header_line_height = -1;
3534 #ifdef HAVE_WINDOW_SYSTEM
3535 w->phys_cursor_type = NO_CURSOR;
3536 w->phys_cursor_width = -1;
3537 #endif
3538 w->sequence_number = ++sequence_number;
3539 w->scroll_bar_width = -1;
3540 w->scroll_bar_height = -1;
3541 w->column_number_displayed = -1;
3542 /* Reset window_list. */
3543 Vwindow_list = Qnil;
3544 /* Return window. */
3545 XSETWINDOW (window, w);
3546 return window;
3549 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3550 doc: /* Set new pixel size of WINDOW to SIZE.
3551 WINDOW must be a valid window and defaults to the selected one.
3552 Return SIZE.
3554 Optional argument ADD non-nil means add SIZE to the new pixel size of
3555 WINDOW and return the sum.
3557 The new pixel size of WINDOW, if valid, will be shortly installed as
3558 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3559 `window-pixel-width').
3561 Note: This function does not operate on any child windows of WINDOW. */)
3562 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3564 struct window *w = decode_valid_window (window);
3565 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3566 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3568 CHECK_RANGED_INTEGER (size, size_min, size_max);
3569 if (NILP (add))
3570 wset_new_pixel (w, size);
3571 else
3572 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3574 return w->new_pixel;
3577 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3578 doc: /* Set new total size of WINDOW to SIZE.
3579 WINDOW must be a valid window and defaults to the selected one.
3580 Return SIZE.
3582 Optional argument ADD non-nil means add SIZE to the new total size of
3583 WINDOW and return the sum.
3585 The new total size of WINDOW, if valid, will be shortly installed as
3586 WINDOW's total height (see `window-total-height') or total width (see
3587 `window-total-width').
3589 Note: This function does not operate on any child windows of WINDOW. */)
3590 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3592 struct window *w = decode_valid_window (window);
3594 CHECK_NUMBER (size);
3595 if (NILP (add))
3596 wset_new_total (w, size);
3597 else
3598 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3600 return w->new_total;
3603 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3604 doc: /* Set new normal size of WINDOW to SIZE.
3605 WINDOW must be a valid window and defaults to the selected one.
3606 Return SIZE.
3608 The new normal size of WINDOW, if valid, will be shortly installed as
3609 WINDOW's normal size (see `window-normal-size').
3611 Note: This function does not operate on any child windows of WINDOW. */)
3612 (Lisp_Object window, Lisp_Object size)
3614 wset_new_normal (decode_valid_window (window), size);
3615 return size;
3618 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3619 to w->new_pixel would result in correct heights (widths)
3620 for window W and recursively all child windows of W.
3622 Note: This function does not check any of `window-fixed-size-p',
3623 `window-min-height' or `window-min-width'. It does check that window
3624 sizes do not drop below one line (two columns). */
3625 static bool
3626 window_resize_check (struct window *w, bool horflag)
3628 struct frame *f = XFRAME (w->frame);
3629 struct window *c;
3631 if (WINDOW_VERTICAL_COMBINATION_P (w))
3632 /* W is a vertical combination. */
3634 c = XWINDOW (w->contents);
3635 if (horflag)
3636 /* All child windows of W must have the same width as W. */
3638 while (c)
3640 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3641 || !window_resize_check (c, horflag))
3642 return false;
3644 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3647 return true;
3649 else
3650 /* The sum of the heights of the child windows of W must equal
3651 W's height. */
3653 int remaining_pixels = XINT (w->new_pixel);
3655 while (c)
3657 if (!window_resize_check (c, horflag))
3658 return false;
3660 remaining_pixels -= XINT (c->new_pixel);
3661 if (remaining_pixels < 0)
3662 return false;
3663 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3666 return remaining_pixels == 0;
3669 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3670 /* W is a horizontal combination. */
3672 c = XWINDOW (w->contents);
3673 if (horflag)
3674 /* The sum of the widths of the child windows of W must equal W's
3675 width. */
3677 int remaining_pixels = XINT (w->new_pixel);
3679 while (c)
3681 if (!window_resize_check (c, horflag))
3682 return false;
3684 remaining_pixels -= XINT (c->new_pixel);
3685 if (remaining_pixels < 0)
3686 return false;
3687 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3690 return remaining_pixels == 0;
3692 else
3693 /* All child windows of W must have the same height as W. */
3695 while (c)
3697 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3698 || !window_resize_check (c, horflag))
3699 return false;
3701 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3704 return true;
3707 else
3708 /* A leaf window. Make sure it's not too small. The following
3709 hardcodes the values of `window-safe-min-width' (2) and
3710 `window-safe-min-height' (1) which are defined in window.el. */
3711 return (XINT (w->new_pixel) >= (horflag
3712 ? (2 * FRAME_COLUMN_WIDTH (f))
3713 : FRAME_LINE_HEIGHT (f)));
3717 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3718 w->new_pixel for window W and recursively all child windows of W.
3719 Also calculate and assign the new vertical (horizontal) pixel start
3720 positions of each of these windows.
3722 This function does not perform any error checks. Make sure you have
3723 run window_resize_check on W before applying this function. */
3724 static void
3725 window_resize_apply (struct window *w, bool horflag)
3727 struct window *c;
3728 int edge;
3729 int unit = (horflag
3730 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3731 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3733 /* Note: Assigning new_normal requires that the new total size of the
3734 parent window has been set *before*. */
3735 if (horflag)
3737 w->pixel_width = XFASTINT (w->new_pixel);
3738 w->total_cols = w->pixel_width / unit;
3739 if (NUMBERP (w->new_normal))
3740 wset_normal_cols (w, w->new_normal);
3742 edge = w->pixel_left;
3744 else
3746 w->pixel_height = XFASTINT (w->new_pixel);
3747 w->total_lines = w->pixel_height / unit;
3748 if (NUMBERP (w->new_normal))
3749 wset_normal_lines (w, w->new_normal);
3751 edge = w->pixel_top;
3754 if (WINDOW_VERTICAL_COMBINATION_P (w))
3755 /* W is a vertical combination. */
3757 c = XWINDOW (w->contents);
3758 while (c)
3760 if (horflag)
3762 c->pixel_left = edge;
3763 c->left_col = edge / unit;
3765 else
3767 c->pixel_top = edge;
3768 c->top_line = edge / unit;
3770 window_resize_apply (c, horflag);
3771 if (!horflag)
3772 edge = edge + c->pixel_height;
3774 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3777 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3778 /* W is a horizontal combination. */
3780 c = XWINDOW (w->contents);
3781 while (c)
3783 if (horflag)
3785 c->pixel_left = edge;
3786 c->left_col = edge / unit;
3788 else
3790 c->pixel_top = edge;
3791 c->top_line = edge / unit;
3794 window_resize_apply (c, horflag);
3795 if (horflag)
3796 edge = edge + c->pixel_width;
3798 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3801 else
3802 /* Bug#15957. */
3803 w->window_end_valid = false;
3807 /* Set w->total_lines (w->total_cols if HORFLAG) to
3808 w->new_total for window W and recursively all child windows of W.
3809 Also calculate and assign the new vertical (horizontal) start
3810 positions of each of these windows. */
3811 static void
3812 window_resize_apply_total (struct window *w, bool horflag)
3814 struct window *c;
3815 int edge;
3817 /* Note: Assigning new_normal requires that the new total size of the
3818 parent window has been set *before*. */
3819 if (horflag)
3821 w->total_cols = XFASTINT (w->new_total);
3822 edge = w->left_col;
3824 else
3826 w->total_lines = XFASTINT (w->new_total);
3827 edge = w->top_line;
3830 if (WINDOW_VERTICAL_COMBINATION_P (w))
3831 /* W is a vertical combination. */
3833 c = XWINDOW (w->contents);
3834 while (c)
3836 if (horflag)
3837 c->left_col = edge;
3838 else
3839 c->top_line = edge;
3841 window_resize_apply_total (c, horflag);
3842 if (!horflag)
3843 edge = edge + c->total_lines;
3845 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3848 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3849 /* W is a horizontal combination. */
3851 c = XWINDOW (w->contents);
3852 while (c)
3854 if (horflag)
3855 c->left_col = edge;
3856 else
3857 c->top_line = edge;
3859 window_resize_apply_total (c, horflag);
3860 if (horflag)
3861 edge = edge + c->total_cols;
3863 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3868 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3869 doc: /* Apply requested size values for window-tree of FRAME.
3870 If FRAME is omitted or nil, it defaults to the selected frame.
3872 Optional argument HORIZONTAL omitted or nil means apply requested
3873 height values. HORIZONTAL non-nil means apply requested width values.
3875 The requested size values are those set by `set-window-new-pixel' and
3876 `set-window-new-normal'. This function checks whether the requested
3877 values sum up to a valid window layout, recursively assigns the new
3878 sizes of all child windows and calculates and assigns the new start
3879 positions of these windows.
3881 Return t if the requested values have been applied correctly, nil
3882 otherwise.
3884 Note: This function does not check any of `window-fixed-size-p',
3885 `window-min-height' or `window-min-width'. All these checks have to
3886 be applied on the Elisp level. */)
3887 (Lisp_Object frame, Lisp_Object horizontal)
3889 struct frame *f = decode_live_frame (frame);
3890 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3891 bool horflag = !NILP (horizontal);
3893 if (!window_resize_check (r, horflag)
3894 || (XINT (r->new_pixel)
3895 != (horflag ? r->pixel_width : r->pixel_height)))
3896 return Qnil;
3898 block_input ();
3899 window_resize_apply (r, horflag);
3901 fset_redisplay (f);
3902 FRAME_WINDOW_SIZES_CHANGED (f) = true;
3904 adjust_frame_glyphs (f);
3905 unblock_input ();
3907 return Qt;
3911 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
3912 doc: /* Apply requested total size values for window-tree of FRAME.
3913 If FRAME is omitted or nil, it defaults to the selected frame.
3915 This function does not assign pixel or normal size values. You should
3916 have run `window-resize-apply' before running this.
3918 Optional argument HORIZONTAL omitted or nil means apply requested
3919 height values. HORIZONTAL non-nil means apply requested width
3920 values. */)
3921 (Lisp_Object frame, Lisp_Object horizontal)
3923 struct frame *f = decode_live_frame (frame);
3924 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3926 block_input ();
3927 /* Necessary when deleting the top-/or leftmost window. */
3928 r->left_col = 0;
3929 r->top_line = FRAME_TOP_MARGIN (f);
3930 window_resize_apply_total (r, !NILP (horizontal));
3931 /* Handle the mini window. */
3932 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3934 struct window *m = XWINDOW (f->minibuffer_window);
3936 if (NILP (horizontal))
3938 m->top_line = r->top_line + r->total_lines;
3939 m->total_lines = XFASTINT (m->new_total);
3941 else
3942 m->total_cols = XFASTINT (m->new_total);
3945 unblock_input ();
3947 return Qt;
3951 /* Resize frame F's windows when number of lines of F is set to SIZE.
3952 HORFLAG means resize windows when number of columns of F is set to
3953 SIZE. PIXELWISE means to interpret SIZE as pixels. */
3954 void
3955 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
3957 Lisp_Object root = f->root_window;
3958 struct window *r = XWINDOW (root);
3959 Lisp_Object mini = f->minibuffer_window;
3960 struct window *m;
3961 /* old_size is the old size of the frame's root window. */
3962 int old_size = horflag ? r->total_cols : r->total_lines;
3963 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
3964 int old_pixel_top = r->pixel_top;
3965 /* new_size is the new size of the frame's root window. */
3966 int new_size, new_pixel_size;
3967 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
3969 /* Don't let the size drop below one unit. This is more comforting
3970 when we are called from x_set_tool_bar_lines since the latter may
3971 have implicitly given us a zero or negative height. */
3972 if (pixelwise)
3974 /* Note: This does not include the size for internal borders
3975 since these are not part of the frame's text area. */
3976 new_pixel_size = max (horflag
3977 ? size
3978 : (size
3979 - ((FRAME_HAS_MINIBUF_P (f)
3980 && !FRAME_MINIBUF_ONLY_P (f))
3981 ? FRAME_LINE_HEIGHT (f) : 0)),
3982 unit);
3983 new_size = new_pixel_size / unit;
3985 else
3987 new_size = max (size - (!horflag
3988 && FRAME_HAS_MINIBUF_P (f)
3989 && !FRAME_MINIBUF_ONLY_P (f)),
3991 new_pixel_size = new_size * unit;
3994 r->top_line = FRAME_TOP_MARGIN (f);
3995 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
3997 if (new_pixel_size == old_pixel_size
3998 && r->pixel_top == old_pixel_top)
4000 else if (WINDOW_LEAF_P (r))
4001 /* For a leaf root window just set the size. */
4002 if (horflag)
4004 r->total_cols = new_size;
4005 r->pixel_width = new_pixel_size;
4007 else
4009 r->total_lines = new_size;
4010 r->pixel_height = new_pixel_size;
4012 else
4014 Lisp_Object delta;
4016 if (pixelwise)
4017 XSETINT (delta, new_pixel_size - old_pixel_size);
4018 else
4019 XSETINT (delta, new_size - old_size);
4021 /* Try a "normal" resize first. */
4022 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4023 pixelwise ? Qt : Qnil);
4024 if (window_resize_check (r, horflag)
4025 && new_pixel_size == XINT (r->new_pixel))
4027 window_resize_apply (r, horflag);
4028 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4030 else
4032 /* Try with "reasonable" minimum sizes next. */
4033 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4034 pixelwise ? Qt : Qnil);
4035 if (window_resize_check (r, horflag)
4036 && new_pixel_size == XINT (r->new_pixel))
4038 window_resize_apply (r, horflag);
4039 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4040 #if false /* Let's try without safe sizes and/or killing other windows. */
4042 else
4044 /* Finally, try with "safe" minimum sizes. */
4045 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4046 pixelwise ? Qt : Qnil);
4047 if (window_resize_check (r, horflag)
4048 && new_pixel_size == XINT (r->new_pixel))
4050 window_resize_apply (r, horflag);
4051 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4053 else
4055 /* We lost. Delete all windows but the frame's
4056 selected one. */
4057 root = f->selected_window;
4058 Fdelete_other_windows_internal (root, Qnil);
4059 if (horflag)
4061 XWINDOW (root)->total_cols = new_size;
4062 XWINDOW (root)->pixel_width = new_pixel_size;
4064 else
4066 XWINDOW (root)->total_lines = new_size;
4067 XWINDOW (root)->pixel_height = new_pixel_size;
4070 #endif /* false */
4075 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4077 m = XWINDOW (mini);
4078 if (horflag)
4080 m->total_cols = size;
4081 m->pixel_width = new_pixel_size;
4083 else
4085 /* Are we sure we always want 1 line here? */
4086 m->total_lines = 1;
4087 m->pixel_height = FRAME_LINE_HEIGHT (f);
4088 m->top_line = r->top_line + r->total_lines;
4089 m->pixel_top = r->pixel_top + r->pixel_height;
4093 fset_redisplay (f);
4097 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4098 doc: /* Split window OLD.
4099 Second argument PIXEL-SIZE specifies the number of pixels of the
4100 new window. It must be a positive integer.
4102 Third argument SIDE nil (or `below') specifies that the new window shall
4103 be located below WINDOW. SIDE `above' means the new window shall be
4104 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4105 height of the new window including space reserved for the mode and/or
4106 header line.
4108 SIDE t (or `right') specifies that the new window shall be located on
4109 the right side of WINDOW. SIDE `left' means the new window shall be
4110 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4111 width of the new window including space reserved for fringes and the
4112 scrollbar or a divider column.
4114 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4115 according to the SIDE argument.
4117 The new pixel and normal sizes of all involved windows must have been
4118 set correctly. See the code of `split-window' for how this is done. */)
4119 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4121 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4122 parent window or an internal window we have to install as OLD's new
4123 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4124 provided OLD is a leaf window, or to the frame's selected window.
4125 NEW (*n) is the new window created with some parameters taken from
4126 REFERENCE (*r). */
4127 Lisp_Object new, frame, reference;
4128 struct window *o, *p, *n, *r, *c;
4129 struct frame *f;
4130 bool horflag
4131 /* HORFLAG is true when we split side-by-side, false otherwise. */
4132 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4134 CHECK_WINDOW (old);
4135 o = XWINDOW (old);
4136 frame = WINDOW_FRAME (o);
4137 f = XFRAME (frame);
4139 CHECK_NUMBER (pixel_size);
4140 EMACS_INT total_size
4141 = XINT (pixel_size) / (horflag
4142 ? FRAME_COLUMN_WIDTH (f)
4143 : FRAME_LINE_HEIGHT (f));
4145 /* Set combination_limit if we have to make a new parent window.
4146 We do that if either `window-combination-limit' is t, or OLD has no
4147 parent, or OLD is ortho-combined. */
4148 bool combination_limit
4149 = (EQ (Vwindow_combination_limit, Qt)
4150 || NILP (o->parent)
4151 || (horflag
4152 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4153 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4155 /* We need a live reference window to initialize some parameters. */
4156 if (WINDOW_LIVE_P (old))
4157 /* OLD is live, use it as reference window. */
4158 reference = old;
4159 else
4160 /* Use the frame's selected window as reference window. */
4161 reference = FRAME_SELECTED_WINDOW (f);
4162 r = XWINDOW (reference);
4164 /* The following bugs are caught by `split-window'. */
4165 if (MINI_WINDOW_P (o))
4166 error ("Attempt to split minibuffer window");
4167 else if (total_size < (horflag ? 2 : 1))
4168 error ("Size of new window too small (after split)");
4169 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4170 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4171 proportionally. */
4173 p = XWINDOW (o->parent);
4174 /* Temporarily pretend we split the parent window. */
4175 wset_new_pixel
4176 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4177 - XINT (pixel_size)));
4178 if (!window_resize_check (p, horflag))
4179 error ("Window sizes don't fit");
4180 else
4181 /* Undo the temporary pretension. */
4182 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4184 else
4186 if (!window_resize_check (o, horflag))
4187 error ("Resizing old window failed");
4188 else if (XINT (pixel_size) + XINT (o->new_pixel)
4189 != (horflag ? o->pixel_width : o->pixel_height))
4190 error ("Sum of sizes of old and new window don't fit");
4193 /* This is our point of no return. */
4194 if (combination_limit)
4196 /* Save the old value of o->normal_cols/lines. It gets corrupted
4197 by make_parent_window and we need it below for assigning it to
4198 p->new_normal. */
4199 Lisp_Object new_normal
4200 = horflag ? o->normal_cols : o->normal_lines;
4202 make_parent_window (old, horflag);
4203 p = XWINDOW (o->parent);
4204 if (EQ (Vwindow_combination_limit, Qt))
4205 /* Store t in the new parent's combination_limit slot to avoid
4206 that its children get merged into another window. */
4207 wset_combination_limit (p, Qt);
4208 /* These get applied below. */
4209 wset_new_pixel
4210 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4211 wset_new_total
4212 (p, make_number (horflag ? o->total_cols : o->total_lines));
4213 wset_new_normal (p, new_normal);
4215 else
4216 p = XWINDOW (o->parent);
4218 fset_redisplay (f);
4219 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4220 new = make_window ();
4221 n = XWINDOW (new);
4222 wset_frame (n, frame);
4223 wset_parent (n, o->parent);
4225 if (EQ (side, Qabove) || EQ (side, Qleft))
4227 wset_prev (n, o->prev);
4228 if (NILP (n->prev))
4229 wset_combination (p, horflag, new);
4230 else
4231 wset_next (XWINDOW (n->prev), new);
4232 wset_next (n, old);
4233 wset_prev (o, new);
4235 else
4237 wset_next (n, o->next);
4238 if (!NILP (n->next))
4239 wset_prev (XWINDOW (n->next), new);
4240 wset_prev (n, old);
4241 wset_next (o, new);
4244 n->window_end_valid = false;
4245 n->last_cursor_vpos = 0;
4247 /* Get special geometry settings from reference window. */
4248 n->left_margin_cols = r->left_margin_cols;
4249 n->right_margin_cols = r->right_margin_cols;
4250 n->left_fringe_width = r->left_fringe_width;
4251 n->right_fringe_width = r->right_fringe_width;
4252 n->fringes_outside_margins = r->fringes_outside_margins;
4253 n->scroll_bar_width = r->scroll_bar_width;
4254 n->scroll_bar_height = r->scroll_bar_height;
4255 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4256 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4258 /* Directly assign orthogonal coordinates and sizes. */
4259 if (horflag)
4261 n->pixel_top = o->pixel_top;
4262 n->top_line = o->top_line;
4263 n->pixel_height = o->pixel_height;
4264 n->total_lines = o->total_lines;
4266 else
4268 n->pixel_left = o->pixel_left;
4269 n->left_col = o->left_col;
4270 n->pixel_width = o->pixel_width;
4271 n->total_cols = o->total_cols;
4274 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4275 get them ready here. */
4276 wset_new_pixel (n, pixel_size);
4277 EMACS_INT sum = 0;
4278 c = XWINDOW (p->contents);
4279 while (c)
4281 if (c != n)
4282 sum = sum + XINT (c->new_total);
4283 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4285 wset_new_total (n, make_number ((horflag
4286 ? p->total_cols
4287 : p->total_lines)
4288 - sum));
4289 wset_new_normal (n, normal_size);
4291 block_input ();
4292 window_resize_apply (p, horflag);
4293 adjust_frame_glyphs (f);
4294 /* Set buffer of NEW to buffer of reference window. Don't run
4295 any hooks. */
4296 set_window_buffer (new, r->contents, false, true);
4297 unblock_input ();
4299 /* Maybe we should run the scroll functions in Elisp (which already
4300 runs the configuration change hook). */
4301 if (! NILP (Vwindow_scroll_functions))
4302 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4303 Fmarker_position (n->start));
4304 /* Return NEW. */
4305 return new;
4309 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4310 doc: /* Remove WINDOW from its frame.
4311 WINDOW defaults to the selected window. Return nil.
4312 Signal an error when WINDOW is the only window on its frame. */)
4313 (Lisp_Object window)
4315 Lisp_Object parent, sibling, frame, root;
4316 struct window *w, *p, *s, *r;
4317 struct frame *f;
4318 bool horflag, before_sibling = false;
4320 w = decode_any_window (window);
4321 XSETWINDOW (window, w);
4322 if (NILP (w->contents))
4323 /* It's a no-op to delete an already deleted window. */
4324 return Qnil;
4326 parent = w->parent;
4327 if (NILP (parent))
4328 /* Never delete a minibuffer or frame root window. */
4329 error ("Attempt to delete minibuffer or sole ordinary window");
4330 else if (NILP (w->prev) && NILP (w->next))
4331 /* Rather bow out here, this case should be handled on the Elisp
4332 level. */
4333 error ("Attempt to delete sole window of parent");
4335 p = XWINDOW (parent);
4336 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4338 frame = WINDOW_FRAME (w);
4339 f = XFRAME (frame);
4341 root = FRAME_ROOT_WINDOW (f);
4342 r = XWINDOW (root);
4344 /* Unlink WINDOW from window tree. */
4345 if (NILP (w->prev))
4346 /* Get SIBLING below (on the right of) WINDOW. */
4348 /* before_sibling means WINDOW is the first child of its
4349 parent and thus before the sibling. */
4350 before_sibling = true;
4351 sibling = w->next;
4352 s = XWINDOW (sibling);
4353 wset_prev (s, Qnil);
4354 wset_combination (p, horflag, sibling);
4356 else
4357 /* Get SIBLING above (on the left of) WINDOW. */
4359 sibling = w->prev;
4360 s = XWINDOW (sibling);
4361 wset_next (s, w->next);
4362 if (!NILP (s->next))
4363 wset_prev (XWINDOW (s->next), sibling);
4366 if (window_resize_check (r, horflag)
4367 && (XINT (r->new_pixel)
4368 == (horflag ? r->pixel_width : r->pixel_height)))
4369 /* We can delete WINDOW now. */
4372 /* Block input. */
4373 block_input ();
4374 window_resize_apply (p, horflag);
4375 /* If this window is referred to by the dpyinfo's mouse
4376 highlight, invalidate that slot to be safe (Bug#9904). */
4377 if (!FRAME_INITIAL_P (f))
4379 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4381 if (EQ (hlinfo->mouse_face_window, window))
4382 hlinfo->mouse_face_window = Qnil;
4385 fset_redisplay (f);
4386 Vwindow_list = Qnil;
4387 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4389 wset_next (w, Qnil); /* Don't delete w->next too. */
4390 free_window_matrices (w);
4392 if (WINDOWP (w->contents))
4394 delete_all_child_windows (w->contents);
4395 wset_combination (w, false, Qnil);
4397 else
4399 unshow_buffer (w);
4400 unchain_marker (XMARKER (w->pointm));
4401 unchain_marker (XMARKER (w->old_pointm));
4402 unchain_marker (XMARKER (w->start));
4403 wset_buffer (w, Qnil);
4406 if (NILP (s->prev) && NILP (s->next))
4407 /* A matrjoshka where SIBLING has become the only child of
4408 PARENT. */
4410 /* Put SIBLING into PARENT's place. */
4411 replace_window (parent, sibling, false);
4412 /* Have SIBLING inherit the following three slot values from
4413 PARENT (the combination_limit slot is not inherited). */
4414 wset_normal_cols (s, p->normal_cols);
4415 wset_normal_lines (s, p->normal_lines);
4416 /* Mark PARENT as deleted. */
4417 wset_combination (p, false, Qnil);
4418 /* Try to merge SIBLING into its new parent. */
4419 recombine_windows (sibling);
4422 adjust_frame_glyphs (f);
4424 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4425 /* We deleted the frame's selected window. */
4427 /* Use the frame's first window as fallback ... */
4428 Lisp_Object new_selected_window = Fframe_first_window (frame);
4429 /* ... but preferably use its most recently used window. */
4430 Lisp_Object mru_window;
4432 /* `get-mru-window' might fail for some reason so play it safe
4433 - promote the first window _without recording it_ first. */
4434 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4435 Fselect_window (new_selected_window, Qt);
4436 else
4437 fset_selected_window (f, new_selected_window);
4439 unblock_input ();
4441 /* Now look whether `get-mru-window' gets us something. */
4442 mru_window = call1 (Qget_mru_window, frame);
4443 if (WINDOW_LIVE_P (mru_window)
4444 && EQ (XWINDOW (mru_window)->frame, frame))
4445 new_selected_window = mru_window;
4447 /* If all ended up well, we now promote the mru window. */
4448 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4449 Fselect_window (new_selected_window, Qnil);
4450 else
4451 fset_selected_window (f, new_selected_window);
4453 else
4454 unblock_input ();
4456 /* Must be run by the caller:
4457 run_window_configuration_change_hook (f); */
4459 else
4460 /* We failed: Relink WINDOW into window tree. */
4462 if (before_sibling)
4464 wset_prev (s, window);
4465 wset_combination (p, horflag, window);
4467 else
4469 wset_next (s, window);
4470 if (!NILP (w->next))
4471 wset_prev (XWINDOW (w->next), window);
4473 error ("Deletion failed");
4476 return Qnil;
4479 /***********************************************************************
4480 Resizing Mini-Windows
4481 ***********************************************************************/
4483 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4484 can. */
4485 void
4486 grow_mini_window (struct window *w, int delta, bool pixelwise)
4488 struct frame *f = XFRAME (w->frame);
4489 struct window *r;
4490 Lisp_Object root, height;
4491 int line_height, pixel_height;
4493 eassert (MINI_WINDOW_P (w));
4494 eassert (delta >= 0);
4496 if (delta > 0)
4498 root = FRAME_ROOT_WINDOW (f);
4499 r = XWINDOW (root);
4500 height = call3 (Qwindow_resize_root_window_vertically,
4501 root, make_number (- delta), pixelwise ? Qt : Qnil);
4502 if (INTEGERP (height) && window_resize_check (r, false))
4504 block_input ();
4505 window_resize_apply (r, false);
4507 if (pixelwise)
4509 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4510 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4512 else
4514 line_height = min (-XINT (height),
4515 ((INT_MAX - w->pixel_height)
4516 / FRAME_LINE_HEIGHT (f)));
4517 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4520 /* Grow the mini-window. */
4521 w->pixel_top = r->pixel_top + r->pixel_height;
4522 w->top_line = r->top_line + r->total_lines;
4523 /* Make sure the mini-window has always at least one line. */
4524 w->pixel_height = max (w->pixel_height + pixel_height,
4525 FRAME_LINE_HEIGHT (f));
4526 w->total_lines = max (w->total_lines + line_height, 1);
4528 /* Enforce full redisplay of the frame. */
4529 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4530 fset_redisplay (f);
4531 adjust_frame_glyphs (f);
4532 unblock_input ();
4537 /* Shrink mini-window W to one line. */
4538 void
4539 shrink_mini_window (struct window *w, bool pixelwise)
4541 struct frame *f = XFRAME (w->frame);
4542 struct window *r;
4543 Lisp_Object root, delta;
4544 EMACS_INT height, unit;
4546 eassert (MINI_WINDOW_P (w));
4548 height = pixelwise ? w->pixel_height : w->total_lines;
4549 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4550 if (height > unit)
4552 root = FRAME_ROOT_WINDOW (f);
4553 r = XWINDOW (root);
4554 delta = call3 (Qwindow_resize_root_window_vertically,
4555 root, make_number (height - unit),
4556 pixelwise ? Qt : Qnil);
4557 if (INTEGERP (delta) && window_resize_check (r, false))
4559 block_input ();
4560 window_resize_apply (r, false);
4562 /* Shrink the mini-window. */
4563 w->top_line = r->top_line + r->total_lines;
4564 w->total_lines = 1;
4565 w->pixel_top = r->pixel_top + r->pixel_height;
4566 w->pixel_height = FRAME_LINE_HEIGHT (f);
4567 /* Enforce full redisplay of the frame. */
4568 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4569 fset_redisplay (f);
4570 adjust_frame_glyphs (f);
4571 unblock_input ();
4573 /* If the above failed for whatever strange reason we must make a
4574 one window frame here. The same routine will be needed when
4575 shrinking the frame (and probably when making the initial
4576 *scratch* window). For the moment leave things as they are. */
4580 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4581 doc: /* Resize minibuffer window WINDOW. */)
4582 (Lisp_Object window)
4584 struct window *w = XWINDOW (window);
4585 struct window *r;
4586 struct frame *f;
4587 int height;
4589 CHECK_WINDOW (window);
4590 f = XFRAME (w->frame);
4592 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4593 error ("Not a valid minibuffer window");
4594 else if (FRAME_MINIBUF_ONLY_P (f))
4595 error ("Cannot resize a minibuffer-only frame");
4597 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4598 height = r->pixel_height + w->pixel_height;
4599 if (window_resize_check (r, false)
4600 && XINT (w->new_pixel) > 0
4601 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4603 block_input ();
4604 window_resize_apply (r, false);
4606 w->pixel_height = XFASTINT (w->new_pixel);
4607 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4608 w->pixel_top = r->pixel_top + r->pixel_height;
4609 w->top_line = r->top_line + r->total_lines;
4611 fset_redisplay (f);
4612 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4613 adjust_frame_glyphs (f);
4614 unblock_input ();
4615 return Qt;
4617 else
4618 error ("Failed to resize minibuffer window");
4621 /* Mark window cursors off for all windows in the window tree rooted
4622 at W by setting their phys_cursor_on_p flag to zero. Called from
4623 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4624 the frame are cleared. */
4626 void
4627 mark_window_cursors_off (struct window *w)
4629 while (w)
4631 if (WINDOWP (w->contents))
4632 mark_window_cursors_off (XWINDOW (w->contents));
4633 else
4634 w->phys_cursor_on_p = false;
4636 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4641 /* Return number of lines of text (not counting mode lines) in W. */
4644 window_internal_height (struct window *w)
4646 int ht = w->total_lines;
4648 if (!MINI_WINDOW_P (w))
4650 if (!NILP (w->parent)
4651 || WINDOWP (w->contents)
4652 || !NILP (w->next)
4653 || !NILP (w->prev)
4654 || WINDOW_WANTS_MODELINE_P (w))
4655 --ht;
4657 if (WINDOW_WANTS_HEADER_LINE_P (w))
4658 --ht;
4661 return ht;
4665 /************************************************************************
4666 Window Scrolling
4667 ***********************************************************************/
4669 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4670 N screen-fulls, which is defined as the height of the window minus
4671 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4672 instead. Negative values of N mean scroll down. NOERROR
4673 means don't signal an error if we try to move over BEGV or ZV,
4674 respectively. */
4676 static void
4677 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4679 ptrdiff_t count = SPECPDL_INDEX ();
4681 immediate_quit = true;
4682 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4684 wset_redisplay (XWINDOW (window));
4686 if (whole && Vfast_but_imprecise_scrolling)
4687 specbind (Qfontification_functions, Qnil);
4689 /* If we must, use the pixel-based version which is much slower than
4690 the line-based one but can handle varying line heights. */
4691 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4692 window_scroll_pixel_based (window, n, whole, noerror);
4693 else
4694 window_scroll_line_based (window, n, whole, noerror);
4696 unbind_to (count, Qnil);
4698 /* Bug#15957. */
4699 XWINDOW (window)->window_end_valid = false;
4700 immediate_quit = false;
4704 /* Implementation of window_scroll that works based on pixel line
4705 heights. See the comment of window_scroll for parameter
4706 descriptions. */
4708 static void
4709 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4711 struct it it;
4712 struct window *w = XWINDOW (window);
4713 struct text_pos start;
4714 int this_scroll_margin;
4715 /* True if we fiddled the window vscroll field without really scrolling. */
4716 bool vscrolled = false;
4717 int x, y, rtop, rbot, rowh, vpos;
4718 void *itdata = NULL;
4719 int window_total_lines;
4720 int frame_line_height = default_line_pixel_height (w);
4721 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4722 Fwindow_old_point (window)));
4724 SET_TEXT_POS_FROM_MARKER (start, w->start);
4725 /* Scrolling a minibuffer window via scroll bar when the echo area
4726 shows long text sometimes resets the minibuffer contents behind
4727 our backs. */
4728 if (CHARPOS (start) > ZV)
4729 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4731 /* If PT is not visible in WINDOW, move back one half of
4732 the screen. Allow PT to be partially visible, otherwise
4733 something like (scroll-down 1) with PT in the line before
4734 the partially visible one would recenter. */
4736 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4738 itdata = bidi_shelve_cache ();
4739 /* Move backward half the height of the window. Performance note:
4740 vmotion used here is about 10% faster, but would give wrong
4741 results for variable height lines. */
4742 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4743 it.current_y = it.last_visible_y;
4744 move_it_vertically_backward (&it, window_box_height (w) / 2);
4746 /* The function move_iterator_vertically may move over more than
4747 the specified y-distance. If it->w is small, e.g. a
4748 mini-buffer window, we may end up in front of the window's
4749 display area. Start displaying at the start of the line
4750 containing PT in this case. */
4751 if (it.current_y <= 0)
4753 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4754 move_it_vertically_backward (&it, 0);
4755 it.current_y = 0;
4758 start = it.current.pos;
4759 bidi_unshelve_cache (itdata, false);
4761 else if (auto_window_vscroll_p)
4763 if (rtop || rbot) /* Partially visible. */
4765 int px;
4766 int dy = frame_line_height;
4767 /* In the below we divide the window box height by the
4768 frame's line height to make the result predictable when
4769 the window box is not an integral multiple of the line
4770 height. This is important to ensure we get back to the
4771 same position when scrolling up, then down. */
4772 if (whole)
4773 dy = max ((window_box_height (w) / dy
4774 - next_screen_context_lines) * dy,
4775 dy);
4776 dy *= n;
4778 if (n < 0)
4780 /* Only vscroll backwards if already vscrolled forwards. */
4781 if (w->vscroll < 0 && rtop > 0)
4783 px = max (0, -w->vscroll - min (rtop, -dy));
4784 Fset_window_vscroll (window, make_number (px), Qt);
4785 return;
4788 if (n > 0)
4790 /* Do vscroll if already vscrolled or only display line. */
4791 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4793 px = max (0, -w->vscroll + min (rbot, dy));
4794 Fset_window_vscroll (window, make_number (px), Qt);
4795 return;
4798 /* Maybe modify window start instead of scrolling. */
4799 if (rbot > 0 || w->vscroll < 0)
4801 ptrdiff_t spos;
4803 Fset_window_vscroll (window, make_number (0), Qt);
4804 /* If there are other text lines above the current row,
4805 move window start to current row. Else to next row. */
4806 if (rbot > 0)
4807 spos = XINT (Fline_beginning_position (Qnil));
4808 else
4809 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4810 set_marker_restricted (w->start, make_number (spos),
4811 w->contents);
4812 w->start_at_line_beg = true;
4813 w->update_mode_line = true;
4814 /* Set force_start so that redisplay_window will run the
4815 window-scroll-functions. */
4816 w->force_start = true;
4817 return;
4821 /* Cancel previous vscroll. */
4822 Fset_window_vscroll (window, make_number (0), Qt);
4825 itdata = bidi_shelve_cache ();
4826 /* If scroll_preserve_screen_position is non-nil, we try to set
4827 point in the same window line as it is now, so get that line. */
4828 if (!NILP (Vscroll_preserve_screen_position))
4830 /* We preserve the goal pixel coordinate across consecutive
4831 calls to scroll-up, scroll-down and other commands that
4832 have the `scroll-command' property. This avoids the
4833 possibility of point becoming "stuck" on a tall line when
4834 scrolling by one line. */
4835 if (window_scroll_pixel_based_preserve_y < 0
4836 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4837 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4839 start_display (&it, w, start);
4840 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4841 window_scroll_pixel_based_preserve_y = it.current_y;
4842 window_scroll_pixel_based_preserve_x = it.current_x;
4845 else
4846 window_scroll_pixel_based_preserve_y
4847 = window_scroll_pixel_based_preserve_x = -1;
4849 /* Move iterator it from start the specified distance forward or
4850 backward. The result is the new window start. */
4851 start_display (&it, w, start);
4852 if (whole)
4854 ptrdiff_t start_pos = IT_CHARPOS (it);
4855 int dy = frame_line_height;
4856 /* In the below we divide the window box height by the frame's
4857 line height to make the result predictable when the window
4858 box is not an integral multiple of the line height. This is
4859 important to ensure we get back to the same position when
4860 scrolling up, then down. */
4861 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4862 dy) * n;
4864 /* Note that move_it_vertically always moves the iterator to the
4865 start of a line. So, if the last line doesn't have a newline,
4866 we would end up at the start of the line ending at ZV. */
4867 if (dy <= 0)
4869 move_it_vertically_backward (&it, -dy);
4870 /* Ensure we actually do move, e.g. in case we are currently
4871 looking at an image that is taller that the window height. */
4872 while (start_pos == IT_CHARPOS (it)
4873 && start_pos > BEGV)
4874 move_it_by_lines (&it, -1);
4876 else if (dy > 0)
4878 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4879 MOVE_TO_POS | MOVE_TO_Y);
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 < ZV)
4884 move_it_by_lines (&it, 1);
4887 else
4888 move_it_by_lines (&it, n);
4890 /* We failed if we find ZV is already on the screen (scrolling up,
4891 means there's nothing past the end), or if we can't start any
4892 earlier (scrolling down, means there's nothing past the top). */
4893 if ((n > 0 && IT_CHARPOS (it) == ZV)
4894 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4896 if (IT_CHARPOS (it) == ZV)
4898 if (it.current_y < it.last_visible_y
4899 && (it.current_y + it.max_ascent + it.max_descent
4900 > it.last_visible_y))
4902 /* The last line was only partially visible, make it fully
4903 visible. */
4904 w->vscroll = (it.last_visible_y
4905 - it.current_y + it.max_ascent + it.max_descent);
4906 adjust_frame_glyphs (it.f);
4908 else
4910 bidi_unshelve_cache (itdata, false);
4911 if (noerror)
4912 return;
4913 else if (n < 0) /* could happen with empty buffers */
4914 xsignal0 (Qbeginning_of_buffer);
4915 else
4916 xsignal0 (Qend_of_buffer);
4919 else
4921 if (w->vscroll != 0)
4922 /* The first line was only partially visible, make it fully
4923 visible. */
4924 w->vscroll = 0;
4925 else
4927 bidi_unshelve_cache (itdata, false);
4928 if (noerror)
4929 return;
4930 else
4931 xsignal0 (Qbeginning_of_buffer);
4935 /* If control gets here, then we vscrolled. */
4937 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
4939 /* Don't try to change the window start below. */
4940 vscrolled = true;
4943 if (! vscrolled)
4945 ptrdiff_t pos = IT_CHARPOS (it);
4946 ptrdiff_t bytepos;
4948 /* If in the middle of a multi-glyph character move forward to
4949 the next character. */
4950 if (in_display_vector_p (&it))
4952 ++pos;
4953 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4956 /* Set the window start, and set up the window for redisplay. */
4957 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4958 IT_BYTEPOS (it));
4959 bytepos = marker_byte_position (w->start);
4960 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4961 w->update_mode_line = true;
4962 /* Set force_start so that redisplay_window will run the
4963 window-scroll-functions. */
4964 w->force_start = true;
4967 /* The rest of this function uses current_y in a nonstandard way,
4968 not including the height of the header line if any. */
4969 it.current_y = it.vpos = 0;
4971 /* Move PT out of scroll margins.
4972 This code wants current_y to be zero at the window start position
4973 even if there is a header line. */
4974 window_total_lines
4975 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4976 this_scroll_margin = max (0, scroll_margin);
4977 this_scroll_margin
4978 = min (this_scroll_margin, window_total_lines / 4);
4979 this_scroll_margin *= frame_line_height;
4981 if (n > 0)
4983 /* We moved the window start towards ZV, so PT may be now
4984 in the scroll margin at the top. */
4985 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4986 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4987 && (NILP (Vscroll_preserve_screen_position)
4988 || EQ (Vscroll_preserve_screen_position, Qt)))
4989 /* We found PT at a legitimate height. Leave it alone. */
4991 else if (window_scroll_pixel_based_preserve_y >= 0)
4993 /* If we have a header line, take account of it.
4994 This is necessary because we set it.current_y to 0, above. */
4995 move_it_to (&it, -1,
4996 window_scroll_pixel_based_preserve_x,
4997 (window_scroll_pixel_based_preserve_y
4998 - WINDOW_WANTS_HEADER_LINE_P (w)),
4999 -1, MOVE_TO_Y | MOVE_TO_X);
5000 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5002 else
5004 while (it.current_y < this_scroll_margin)
5006 int prev = it.current_y;
5007 move_it_by_lines (&it, 1);
5008 if (prev == it.current_y)
5009 break;
5011 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5014 else if (n < 0)
5016 ptrdiff_t charpos, bytepos;
5017 bool partial_p;
5019 /* Save our position, for the
5020 window_scroll_pixel_based_preserve_y case. */
5021 charpos = IT_CHARPOS (it);
5022 bytepos = IT_BYTEPOS (it);
5024 /* We moved the window start towards BEGV, so PT may be now
5025 in the scroll margin at the bottom. */
5026 move_it_to (&it, PT, -1,
5027 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5028 - this_scroll_margin - 1),
5030 MOVE_TO_POS | MOVE_TO_Y);
5032 /* Save our position, in case it's correct. */
5033 charpos = IT_CHARPOS (it);
5034 bytepos = IT_BYTEPOS (it);
5036 /* If PT is in the screen line at the last fully visible line,
5037 move_it_to will stop at X = 0 in that line, because the
5038 required Y coordinate is reached there. See if we can get to
5039 PT without descending lower in Y, and if we can, it means we
5040 reached PT before the scroll margin. */
5041 if (charpos != PT)
5043 struct it it2;
5044 void *it_data;
5046 it2 = it;
5047 it_data = bidi_shelve_cache ();
5048 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5049 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5051 charpos = IT_CHARPOS (it);
5052 bytepos = IT_BYTEPOS (it);
5053 bidi_unshelve_cache (it_data, true);
5055 else
5057 it = it2;
5058 bidi_unshelve_cache (it_data, false);
5062 /* See if point is on a partially visible line at the end. */
5063 if (it.what == IT_EOB)
5064 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5065 else
5067 move_it_by_lines (&it, 1);
5068 partial_p = it.current_y > it.last_visible_y;
5071 if (charpos == PT && !partial_p
5072 && (NILP (Vscroll_preserve_screen_position)
5073 || EQ (Vscroll_preserve_screen_position, Qt)))
5074 /* We found PT before we found the display margin, so PT is ok. */
5076 else if (window_scroll_pixel_based_preserve_y >= 0)
5078 SET_TEXT_POS_FROM_MARKER (start, w->start);
5079 start_display (&it, w, start);
5080 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5081 here because we called start_display again and did not
5082 alter it.current_y this time. */
5083 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5084 window_scroll_pixel_based_preserve_y, -1,
5085 MOVE_TO_Y | MOVE_TO_X);
5086 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5088 else
5090 if (partial_p)
5091 /* The last line was only partially visible, so back up two
5092 lines to make sure we're on a fully visible line. */
5094 move_it_by_lines (&it, -2);
5095 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5097 else
5098 /* No, the position we saved is OK, so use it. */
5099 SET_PT_BOTH (charpos, bytepos);
5102 bidi_unshelve_cache (itdata, false);
5104 if (adjust_old_pointm)
5105 Fset_marker (w->old_pointm,
5106 ((w == XWINDOW (selected_window))
5107 ? make_number (BUF_PT (XBUFFER (w->contents)))
5108 : Fmarker_position (w->pointm)),
5109 w->contents);
5113 /* Implementation of window_scroll that works based on screen lines.
5114 See the comment of window_scroll for parameter descriptions. */
5116 static void
5117 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5119 struct window *w = XWINDOW (window);
5120 /* Fvertical_motion enters redisplay, which can trigger
5121 fontification, which in turn can modify buffer text (e.g., if the
5122 fontification functions replace escape sequences with faces, as
5123 in `grep-mode-font-lock-keywords'). So we use a marker to record
5124 the old point position, to prevent crashes in SET_PT_BOTH. */
5125 Lisp_Object opoint_marker = Fpoint_marker ();
5126 register ptrdiff_t pos, pos_byte;
5127 register int ht = window_internal_height (w);
5128 register Lisp_Object tem;
5129 bool lose;
5130 Lisp_Object bolp;
5131 ptrdiff_t startpos = marker_position (w->start);
5132 ptrdiff_t startbyte = marker_byte_position (w->start);
5133 Lisp_Object original_pos = Qnil;
5134 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5135 Fwindow_old_point (window)));
5137 /* If scrolling screen-fulls, compute the number of lines to
5138 scroll from the window's height. */
5139 if (whole)
5140 n *= max (1, ht - next_screen_context_lines);
5142 if (!NILP (Vscroll_preserve_screen_position))
5144 if (window_scroll_preserve_vpos <= 0
5145 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5146 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5148 struct position posit
5149 = *compute_motion (startpos, startbyte, 0, 0, false,
5150 PT, ht, 0, -1, w->hscroll, 0, w);
5152 window_scroll_preserve_vpos = posit.vpos;
5153 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5156 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5157 make_number (window_scroll_preserve_vpos));
5160 XSETFASTINT (tem, PT);
5161 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5163 if (NILP (tem))
5165 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5166 startpos = PT;
5167 startbyte = PT_BYTE;
5170 SET_PT_BOTH (startpos, startbyte);
5171 lose = n < 0 && PT == BEGV;
5172 Fvertical_motion (make_number (n), window, Qnil);
5173 pos = PT;
5174 pos_byte = PT_BYTE;
5175 bolp = Fbolp ();
5176 SET_PT_BOTH (marker_position (opoint_marker),
5177 marker_byte_position (opoint_marker));
5179 if (lose)
5181 if (noerror)
5182 return;
5183 else
5184 xsignal0 (Qbeginning_of_buffer);
5187 if (pos < ZV)
5189 /* Don't use a scroll margin that is negative or too large. */
5190 int this_scroll_margin =
5191 max (0, min (scroll_margin, w->total_lines / 4));
5193 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5194 w->start_at_line_beg = !NILP (bolp);
5195 w->update_mode_line = true;
5196 /* Set force_start so that redisplay_window will run
5197 the window-scroll-functions. */
5198 w->force_start = true;
5200 if (!NILP (Vscroll_preserve_screen_position)
5201 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5203 SET_PT_BOTH (pos, pos_byte);
5204 Fvertical_motion (original_pos, window, Qnil);
5206 /* If we scrolled forward, put point enough lines down
5207 that it is outside the scroll margin. */
5208 else if (n > 0)
5210 int top_margin;
5212 if (this_scroll_margin > 0)
5214 SET_PT_BOTH (pos, pos_byte);
5215 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5216 top_margin = PT;
5218 else
5219 top_margin = pos;
5221 if (top_margin <= marker_position (opoint_marker))
5222 SET_PT_BOTH (marker_position (opoint_marker),
5223 marker_byte_position (opoint_marker));
5224 else if (!NILP (Vscroll_preserve_screen_position))
5226 SET_PT_BOTH (pos, pos_byte);
5227 Fvertical_motion (original_pos, window, Qnil);
5229 else
5230 SET_PT (top_margin);
5232 else if (n < 0)
5234 int bottom_margin;
5236 /* If we scrolled backward, put point near the end of the window
5237 but not within the scroll margin. */
5238 SET_PT_BOTH (pos, pos_byte);
5239 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5240 Qnil);
5241 if (XFASTINT (tem) == ht - this_scroll_margin)
5242 bottom_margin = PT;
5243 else
5244 bottom_margin = PT + 1;
5246 if (bottom_margin > marker_position (opoint_marker))
5247 SET_PT_BOTH (marker_position (opoint_marker),
5248 marker_byte_position (opoint_marker));
5249 else
5251 if (!NILP (Vscroll_preserve_screen_position))
5253 SET_PT_BOTH (pos, pos_byte);
5254 Fvertical_motion (original_pos, window, Qnil);
5256 else
5257 Fvertical_motion (make_number (-1), window, Qnil);
5261 else
5263 if (noerror)
5264 return;
5265 else
5266 xsignal0 (Qend_of_buffer);
5269 if (adjust_old_pointm)
5270 Fset_marker (w->old_pointm,
5271 ((w == XWINDOW (selected_window))
5272 ? make_number (BUF_PT (XBUFFER (w->contents)))
5273 : Fmarker_position (w->pointm)),
5274 w->contents);
5278 /* Scroll selected_window up or down. If N is nil, scroll a
5279 screen-full which is defined as the height of the window minus
5280 next_screen_context_lines. If N is the symbol `-', scroll.
5281 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5282 up. This is the guts of Fscroll_up and Fscroll_down. */
5284 static void
5285 scroll_command (Lisp_Object n, int direction)
5287 ptrdiff_t count = SPECPDL_INDEX ();
5289 eassert (eabs (direction) == 1);
5291 /* If selected window's buffer isn't current, make it current for
5292 the moment. But don't screw up if window_scroll gets an error. */
5293 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5295 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5296 Fset_buffer (XWINDOW (selected_window)->contents);
5299 if (NILP (n))
5300 window_scroll (selected_window, direction, true, false);
5301 else if (EQ (n, Qminus))
5302 window_scroll (selected_window, -direction, true, false);
5303 else
5305 n = Fprefix_numeric_value (n);
5306 window_scroll (selected_window, XINT (n) * direction, false, false);
5309 unbind_to (count, Qnil);
5312 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5313 doc: /* Scroll text of selected window upward ARG lines.
5314 If ARG is omitted or nil, scroll upward by a near full screen.
5315 A near full screen is `next-screen-context-lines' less than a full screen.
5316 Negative ARG means scroll downward.
5317 If ARG is the atom `-', scroll downward by nearly full screen.
5318 When calling from a program, supply as argument a number, nil, or `-'. */)
5319 (Lisp_Object arg)
5321 scroll_command (arg, 1);
5322 return Qnil;
5325 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5326 doc: /* Scroll text of selected window down ARG lines.
5327 If ARG is omitted or nil, scroll down by a near full screen.
5328 A near full screen is `next-screen-context-lines' less than a full screen.
5329 Negative ARG means scroll upward.
5330 If ARG is the atom `-', scroll upward by nearly full screen.
5331 When calling from a program, supply as argument a number, nil, or `-'. */)
5332 (Lisp_Object arg)
5334 scroll_command (arg, -1);
5335 return Qnil;
5338 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5339 doc: /* Return the other window for \"other window scroll\" commands.
5340 If `other-window-scroll-buffer' is non-nil, a window
5341 showing that buffer is used.
5342 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5343 specifies the window. This takes precedence over
5344 `other-window-scroll-buffer'. */)
5345 (void)
5347 Lisp_Object window;
5349 if (MINI_WINDOW_P (XWINDOW (selected_window))
5350 && !NILP (Vminibuf_scroll_window))
5351 window = Vminibuf_scroll_window;
5352 /* If buffer is specified and live, scroll that buffer. */
5353 else if (!NILP (Vother_window_scroll_buffer)
5354 && BUFFERP (Vother_window_scroll_buffer)
5355 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5357 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5358 if (NILP (window))
5359 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5361 else
5363 /* Nothing specified; look for a neighboring window on the same
5364 frame. */
5365 window = Fnext_window (selected_window, Qnil, Qnil);
5367 if (EQ (window, selected_window))
5368 /* That didn't get us anywhere; look for a window on another
5369 visible frame. */
5371 window = Fnext_window (window, Qnil, Qt);
5372 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5373 && ! EQ (window, selected_window));
5376 CHECK_LIVE_WINDOW (window);
5378 if (EQ (window, selected_window))
5379 error ("There is no other window");
5381 return window;
5384 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5385 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5386 A near full screen is `next-screen-context-lines' less than a full screen.
5387 The next window is the one below the current one; or the one at the top
5388 if the current one is at the bottom. Negative ARG means scroll downward.
5389 If ARG is the atom `-', scroll downward by nearly full screen.
5390 When calling from a program, supply as argument a number, nil, or `-'.
5392 If `other-window-scroll-buffer' is non-nil, scroll the window
5393 showing that buffer, popping the buffer up if necessary.
5394 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5395 specifies the window to scroll. This takes precedence over
5396 `other-window-scroll-buffer'. */)
5397 (Lisp_Object arg)
5399 Lisp_Object window;
5400 struct window *w;
5401 ptrdiff_t count = SPECPDL_INDEX ();
5403 window = Fother_window_for_scrolling ();
5404 w = XWINDOW (window);
5406 /* Don't screw up if window_scroll gets an error. */
5407 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5409 Fset_buffer (w->contents);
5410 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5411 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5413 if (NILP (arg))
5414 window_scroll (window, 1, true, true);
5415 else if (EQ (arg, Qminus))
5416 window_scroll (window, -1, true, true);
5417 else
5419 if (CONSP (arg))
5420 arg = XCAR (arg);
5421 CHECK_NUMBER (arg);
5422 window_scroll (window, XINT (arg), false, true);
5425 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5426 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5427 unbind_to (count, Qnil);
5429 return Qnil;
5432 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5433 doc: /* Scroll selected window display ARG columns left.
5434 Default for ARG is window width minus 2.
5435 Value is the total amount of leftward horizontal scrolling in
5436 effect after the change.
5437 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5438 lower bound for automatic scrolling, i.e. automatic scrolling
5439 will not scroll a window to a column less than the value returned
5440 by this function. This happens in an interactive call. */)
5441 (register Lisp_Object arg, Lisp_Object set_minimum)
5443 struct window *w = XWINDOW (selected_window);
5444 EMACS_INT requested_arg = (NILP (arg)
5445 ? window_body_width (w, 0) - 2
5446 : XINT (Fprefix_numeric_value (arg)));
5447 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5449 if (!NILP (set_minimum))
5450 w->min_hscroll = w->hscroll;
5452 w->suspend_auto_hscroll = true;
5454 return result;
5457 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5458 doc: /* Scroll selected window display ARG columns right.
5459 Default for ARG is window width minus 2.
5460 Value is the total amount of leftward horizontal scrolling in
5461 effect after the change.
5462 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5463 lower bound for automatic scrolling, i.e. automatic scrolling
5464 will not scroll a window to a column less than the value returned
5465 by this function. This happens in an interactive call. */)
5466 (register Lisp_Object arg, Lisp_Object set_minimum)
5468 struct window *w = XWINDOW (selected_window);
5469 EMACS_INT requested_arg = (NILP (arg)
5470 ? window_body_width (w, 0) - 2
5471 : XINT (Fprefix_numeric_value (arg)));
5472 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5474 if (!NILP (set_minimum))
5475 w->min_hscroll = w->hscroll;
5477 w->suspend_auto_hscroll = true;
5479 return result;
5482 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5483 doc: /* Return the window which was selected when entering the minibuffer.
5484 Returns nil, if selected window is not a minibuffer window. */)
5485 (void)
5487 if (minibuf_level > 0
5488 && MINI_WINDOW_P (XWINDOW (selected_window))
5489 && WINDOW_LIVE_P (minibuf_selected_window))
5490 return minibuf_selected_window;
5492 return Qnil;
5495 /* Value is the number of lines actually displayed in window W,
5496 as opposed to its height. */
5498 static int
5499 displayed_window_lines (struct window *w)
5501 struct it it;
5502 struct text_pos start;
5503 int height = window_box_height (w);
5504 struct buffer *old_buffer;
5505 int bottom_y;
5506 void *itdata = NULL;
5508 if (XBUFFER (w->contents) != current_buffer)
5510 old_buffer = current_buffer;
5511 set_buffer_internal (XBUFFER (w->contents));
5513 else
5514 old_buffer = NULL;
5516 /* In case W->start is out of the accessible range, do something
5517 reasonable. This happens in Info mode when Info-scroll-down
5518 calls (recenter -1) while W->start is 1. */
5519 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5521 itdata = bidi_shelve_cache ();
5522 start_display (&it, w, start);
5523 move_it_vertically (&it, height);
5524 bottom_y = line_bottom_y (&it);
5525 bidi_unshelve_cache (itdata, false);
5527 /* rms: On a non-window display,
5528 the value of it.vpos at the bottom of the screen
5529 seems to be 1 larger than window_box_height (w).
5530 This kludge fixes a bug whereby (move-to-window-line -1)
5531 when ZV is on the last screen line
5532 moves to the previous screen line instead of the last one. */
5533 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5534 height++;
5536 /* Add in empty lines at the bottom of the window. */
5537 if (bottom_y < height)
5539 int uy = FRAME_LINE_HEIGHT (it.f);
5540 it.vpos += (height - bottom_y + uy - 1) / uy;
5543 if (old_buffer)
5544 set_buffer_internal (old_buffer);
5546 return it.vpos;
5550 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5551 doc: /* Center point in selected window and maybe redisplay frame.
5552 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5553 relative to the selected window. If ARG is negative, it counts up from the
5554 bottom of the window. (ARG should be less than the height of the window.)
5556 If ARG is omitted or nil, then recenter with point on the middle line of
5557 the selected window; if the variable `recenter-redisplay' is non-nil,
5558 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5559 is set to `grow-only', this resets the tool-bar's height to the minimum
5560 height needed); if `recenter-redisplay' has the special value `tty',
5561 then only tty frames are redrawn.
5563 Just C-u as prefix means put point in the center of the window
5564 and redisplay normally--don't erase and redraw the frame. */)
5565 (register Lisp_Object arg)
5567 struct window *w = XWINDOW (selected_window);
5568 struct buffer *buf = XBUFFER (w->contents);
5569 bool center_p = false;
5570 ptrdiff_t charpos, bytepos;
5571 EMACS_INT iarg IF_LINT (= 0);
5572 int this_scroll_margin;
5574 if (buf != current_buffer)
5575 error ("`recenter'ing a window that does not display current-buffer.");
5577 /* If redisplay is suppressed due to an error, try again. */
5578 buf->display_error_modiff = 0;
5580 if (NILP (arg))
5582 if (!NILP (Vrecenter_redisplay)
5583 && (!EQ (Vrecenter_redisplay, Qtty)
5584 || !NILP (Ftty_type (selected_frame))))
5586 ptrdiff_t i;
5588 /* Invalidate pixel data calculated for all compositions. */
5589 for (i = 0; i < n_compositions; i++)
5590 composition_table[i]->font = NULL;
5591 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5592 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5593 #endif
5594 Fredraw_frame (WINDOW_FRAME (w));
5595 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5598 center_p = true;
5600 else if (CONSP (arg)) /* Just C-u. */
5601 center_p = true;
5602 else
5604 arg = Fprefix_numeric_value (arg);
5605 CHECK_NUMBER (arg);
5606 iarg = XINT (arg);
5609 /* Do this after making BUF current
5610 in case scroll_margin is buffer-local. */
5611 this_scroll_margin
5612 = max (0, min (scroll_margin, w->total_lines / 4));
5614 /* Don't use redisplay code for initial frames, as the necessary
5615 data structures might not be set up yet then. */
5616 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5618 if (center_p)
5620 struct it it;
5621 struct text_pos pt;
5622 void *itdata = bidi_shelve_cache ();
5624 SET_TEXT_POS (pt, PT, PT_BYTE);
5625 start_display (&it, w, pt);
5626 move_it_vertically_backward (&it, window_box_height (w) / 2);
5627 charpos = IT_CHARPOS (it);
5628 bytepos = IT_BYTEPOS (it);
5629 bidi_unshelve_cache (itdata, false);
5631 else if (iarg < 0)
5633 struct it it;
5634 struct text_pos pt;
5635 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5636 int extra_line_spacing;
5637 int h = window_box_height (w);
5638 int ht = window_internal_height (w);
5639 void *itdata = bidi_shelve_cache ();
5641 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5642 ht - this_scroll_margin);
5644 SET_TEXT_POS (pt, PT, PT_BYTE);
5645 start_display (&it, w, pt);
5647 /* Be sure we have the exact height of the full line containing PT. */
5648 move_it_by_lines (&it, 0);
5650 /* The amount of pixels we have to move back is the window
5651 height minus what's displayed in the line containing PT,
5652 and the lines below. */
5653 it.current_y = 0;
5654 it.vpos = 0;
5655 move_it_by_lines (&it, nlines);
5657 if (it.vpos == nlines)
5658 h -= it.current_y;
5659 else
5661 /* Last line has no newline. */
5662 h -= line_bottom_y (&it);
5663 it.vpos++;
5666 /* Don't reserve space for extra line spacing of last line. */
5667 extra_line_spacing = it.max_extra_line_spacing;
5669 /* If we can't move down NLINES lines because we hit
5670 the end of the buffer, count in some empty lines. */
5671 if (it.vpos < nlines)
5673 nlines -= it.vpos;
5674 extra_line_spacing = it.extra_line_spacing;
5675 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5677 if (h <= 0)
5679 bidi_unshelve_cache (itdata, false);
5680 return Qnil;
5683 /* Now find the new top line (starting position) of the window. */
5684 start_display (&it, w, pt);
5685 it.current_y = 0;
5686 move_it_vertically_backward (&it, h);
5688 /* If extra line spacing is present, we may move too far
5689 back. This causes the last line to be only partially
5690 visible (which triggers redisplay to recenter that line
5691 in the middle), so move forward.
5692 But ignore extra line spacing on last line, as it is not
5693 considered to be part of the visible height of the line.
5695 h += extra_line_spacing;
5696 while (-it.current_y > h)
5697 move_it_by_lines (&it, 1);
5699 charpos = IT_CHARPOS (it);
5700 bytepos = IT_BYTEPOS (it);
5702 bidi_unshelve_cache (itdata, false);
5704 else
5706 struct it it;
5707 struct text_pos pt;
5708 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5709 int ht = window_internal_height (w);
5710 void *itdata = bidi_shelve_cache ();
5712 nlines = clip_to_bounds (this_scroll_margin, nlines,
5713 ht - this_scroll_margin - 1);
5715 SET_TEXT_POS (pt, PT, PT_BYTE);
5716 start_display (&it, w, pt);
5718 /* Move to the beginning of screen line containing PT. */
5719 move_it_by_lines (&it, 0);
5721 /* Move back to find the point which is ARG screen lines above PT. */
5722 if (nlines > 0)
5724 it.current_y = 0;
5725 it.vpos = 0;
5726 move_it_by_lines (&it, -nlines);
5729 charpos = IT_CHARPOS (it);
5730 bytepos = IT_BYTEPOS (it);
5732 bidi_unshelve_cache (itdata, false);
5735 else
5737 struct position pos;
5738 int ht = window_internal_height (w);
5740 if (center_p)
5741 iarg = ht / 2;
5742 else if (iarg < 0)
5743 iarg += ht;
5745 /* Don't let it get into the margin at either top or bottom. */
5746 iarg = clip_to_bounds (this_scroll_margin, iarg,
5747 ht - this_scroll_margin - 1);
5749 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5750 charpos = pos.bufpos;
5751 bytepos = pos.bytepos;
5754 /* Set the new window start. */
5755 set_marker_both (w->start, w->contents, charpos, bytepos);
5756 w->window_end_valid = false;
5758 w->optional_new_start = true;
5760 w->start_at_line_beg = (bytepos == BEGV_BYTE
5761 || FETCH_BYTE (bytepos - 1) == '\n');
5763 wset_redisplay (w);
5765 return Qnil;
5768 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5769 0, 2, 0,
5770 doc: /* Return the width in columns of the text display area of WINDOW.
5771 WINDOW must be a live window and defaults to the selected one.
5773 The returned width does not include dividers, scrollbars, margins,
5774 fringes, nor any partial-width columns at the right of the text
5775 area.
5777 Optional argument PIXELWISE non-nil, means to return the width in
5778 pixels. */)
5779 (Lisp_Object window, Lisp_Object pixelwise)
5781 struct window *w = decode_live_window (window);
5783 if (NILP (pixelwise))
5784 return make_number (window_box_width (w, TEXT_AREA)
5785 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5786 else
5787 return make_number (window_box_width (w, TEXT_AREA));
5790 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5791 0, 2, 0,
5792 doc: /* Return the height in lines of the text display area of WINDOW.
5793 WINDOW must be a live window and defaults to the selected one.
5795 The returned height does not include dividers, the mode line, any header
5796 line, nor any partial-height lines at the bottom of the text area.
5798 Optional argument PIXELWISE non-nil, means to return the height in
5799 pixels. */)
5800 (Lisp_Object window, Lisp_Object pixelwise)
5802 struct window *w = decode_live_window (window);
5804 if (NILP (pixelwise))
5805 return make_number (window_box_height (w)
5806 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5807 else
5808 return make_number (window_box_height (w));
5811 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5812 1, 1, "P",
5813 doc: /* Position point relative to window.
5814 ARG nil means position point at center of window.
5815 Else, ARG specifies vertical position within the window;
5816 zero means top of window, negative means relative to bottom of window. */)
5817 (Lisp_Object arg)
5819 struct window *w = XWINDOW (selected_window);
5820 int lines, start;
5821 Lisp_Object window;
5822 #if false
5823 int this_scroll_margin;
5824 #endif
5826 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5827 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5828 when passed below to set_marker_both. */
5829 error ("move-to-window-line called from unrelated buffer");
5831 window = selected_window;
5832 start = marker_position (w->start);
5833 if (start < BEGV || start > ZV)
5835 int height = window_internal_height (w);
5836 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5837 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5838 w->start_at_line_beg = !NILP (Fbolp ());
5839 w->force_start = true;
5841 else
5842 Fgoto_char (w->start);
5844 lines = displayed_window_lines (w);
5846 #if false
5847 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5848 #endif
5850 if (NILP (arg))
5851 XSETFASTINT (arg, lines / 2);
5852 else
5854 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5856 if (iarg < 0)
5857 iarg = iarg + lines;
5859 #if false /* This code would prevent move-to-window-line from moving point
5860 to a place inside the scroll margins (which would cause the
5861 next redisplay to scroll). I wrote this code, but then concluded
5862 it is probably better not to install it. However, it is here
5863 inside #if false so as not to lose it. -- rms. */
5865 /* Don't let it get into the margin at either top or bottom. */
5866 iarg = max (iarg, this_scroll_margin);
5867 iarg = min (iarg, lines - this_scroll_margin - 1);
5868 #endif
5870 arg = make_number (iarg);
5873 /* Skip past a partially visible first line. */
5874 if (w->vscroll)
5875 XSETINT (arg, XINT (arg) + 1);
5877 return Fvertical_motion (arg, window, Qnil);
5882 /***********************************************************************
5883 Window Configuration
5884 ***********************************************************************/
5886 struct save_window_data
5888 struct vectorlike_header header;
5889 Lisp_Object selected_frame;
5890 Lisp_Object current_window;
5891 Lisp_Object current_buffer;
5892 Lisp_Object minibuf_scroll_window;
5893 Lisp_Object minibuf_selected_window;
5894 Lisp_Object root_window;
5895 Lisp_Object focus_frame;
5896 /* A vector, each of whose elements is a struct saved_window
5897 for one window. */
5898 Lisp_Object saved_windows;
5900 /* All fields above are traced by the GC.
5901 From `frame-cols' down, the fields are ignored by the GC. */
5902 /* We should be able to do without the following two. */
5903 int frame_cols, frame_lines;
5904 /* These two should get eventually replaced by their pixel
5905 counterparts. */
5906 int frame_menu_bar_lines, frame_tool_bar_lines;
5907 int frame_text_width, frame_text_height;
5908 /* These are currently unused. We need them as soon as we convert
5909 to pixels. */
5910 int frame_menu_bar_height, frame_tool_bar_height;
5913 /* This is saved as a Lisp_Vector. */
5914 struct saved_window
5916 struct vectorlike_header header;
5918 Lisp_Object window, buffer, start, pointm, old_pointm;
5919 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5920 Lisp_Object left_col, top_line, total_cols, total_lines;
5921 Lisp_Object normal_cols, normal_lines;
5922 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
5923 Lisp_Object parent, prev;
5924 Lisp_Object start_at_line_beg;
5925 Lisp_Object display_table;
5926 Lisp_Object left_margin_cols, right_margin_cols;
5927 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5928 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5929 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
5930 Lisp_Object combination_limit, window_parameters;
5933 #define SAVED_WINDOW_N(swv,n) \
5934 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5936 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5937 doc: /* Return t if OBJECT is a window-configuration object. */)
5938 (Lisp_Object object)
5940 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5943 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5944 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5945 (Lisp_Object config)
5947 register struct save_window_data *data;
5948 struct Lisp_Vector *saved_windows;
5950 CHECK_WINDOW_CONFIGURATION (config);
5952 data = (struct save_window_data *) XVECTOR (config);
5953 saved_windows = XVECTOR (data->saved_windows);
5954 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5957 DEFUN ("set-window-configuration", Fset_window_configuration,
5958 Sset_window_configuration, 1, 1, 0,
5959 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5960 CONFIGURATION must be a value previously returned
5961 by `current-window-configuration' (which see).
5962 If CONFIGURATION was made from a frame that is now deleted,
5963 only frame-independent values can be restored. In this case,
5964 the return value is nil. Otherwise the value is t. */)
5965 (Lisp_Object configuration)
5967 register struct save_window_data *data;
5968 struct Lisp_Vector *saved_windows;
5969 Lisp_Object new_current_buffer;
5970 Lisp_Object frame;
5971 struct frame *f;
5972 ptrdiff_t old_point = -1;
5973 USE_SAFE_ALLOCA;
5975 CHECK_WINDOW_CONFIGURATION (configuration);
5977 data = (struct save_window_data *) XVECTOR (configuration);
5978 saved_windows = XVECTOR (data->saved_windows);
5980 new_current_buffer = data->current_buffer;
5981 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5982 new_current_buffer = Qnil;
5983 else
5985 if (XBUFFER (new_current_buffer) == current_buffer)
5986 /* The code further down "preserves point" by saving here PT in
5987 old_point and then setting it later back into PT. When the
5988 current-selected-window and the final-selected-window both show
5989 the current buffer, this suffers from the problem that the
5990 current PT is the window-point of the current-selected-window,
5991 while the final PT is the point of the final-selected-window, so
5992 this copy from one PT to the other would end up moving the
5993 window-point of the final-selected-window to the window-point of
5994 the current-selected-window. So we have to be careful which
5995 point of the current-buffer we copy into old_point. */
5996 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5997 && WINDOWP (selected_window)
5998 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5999 && !EQ (selected_window, data->current_window))
6000 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6001 else
6002 old_point = PT;
6003 else
6004 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6005 point in new_current_buffer as of the last time this buffer was
6006 used. This can be non-deterministic since it can be changed by
6007 things like jit-lock by mere temporary selection of some random
6008 window that happens to show this buffer.
6009 So if possible we want this arbitrary choice of "which point" to
6010 be the one from the to-be-selected-window so as to prevent this
6011 window's cursor from being copied from another window. */
6012 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6013 /* If current_window = selected_window, its point is in BUF_PT. */
6014 && !EQ (selected_window, data->current_window))
6015 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6016 else
6017 old_point = BUF_PT (XBUFFER (new_current_buffer));
6020 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6021 f = XFRAME (frame);
6023 /* If f is a dead frame, don't bother rebuilding its window tree.
6024 However, there is other stuff we should still try to do below. */
6025 if (FRAME_LIVE_P (f))
6027 Lisp_Object window;
6028 Lisp_Object dead_windows = Qnil;
6029 Lisp_Object tem, par, pers;
6030 struct window *w;
6031 struct saved_window *p;
6032 struct window *root_window;
6033 struct window **leaf_windows;
6034 ptrdiff_t i, k, n_leaf_windows;
6036 /* Don't do this within the main loop below: This may call Lisp
6037 code and is thus potentially unsafe while input is blocked. */
6038 for (k = 0; k < saved_windows->header.size; k++)
6040 p = SAVED_WINDOW_N (saved_windows, k);
6041 window = p->window;
6042 w = XWINDOW (window);
6043 if (BUFFERP (w->contents)
6044 && !EQ (w->contents, p->buffer)
6045 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6046 /* If a window we restore gets another buffer, record the
6047 window's old buffer. */
6048 call1 (Qrecord_window_buffer, window);
6051 /* Disallow x_set_window_size, temporarily. */
6052 f->can_x_set_window_size = false;
6053 /* The mouse highlighting code could get screwed up
6054 if it runs during this. */
6055 block_input ();
6057 /* "Swap out" point from the selected window's buffer
6058 into the window itself. (Normally the pointm of the selected
6059 window holds garbage.) We do this now, before
6060 restoring the window contents, and prevent it from
6061 being done later on when we select a new window. */
6062 if (! NILP (XWINDOW (selected_window)->contents))
6064 w = XWINDOW (selected_window);
6065 set_marker_both (w->pointm,
6066 w->contents,
6067 BUF_PT (XBUFFER (w->contents)),
6068 BUF_PT_BYTE (XBUFFER (w->contents)));
6071 fset_redisplay (f);
6072 FRAME_WINDOW_SIZES_CHANGED (f) = true;
6074 /* Problem: Freeing all matrices and later allocating them again
6075 is a serious redisplay flickering problem. What we would
6076 really like to do is to free only those matrices not reused
6077 below. */
6078 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6079 ptrdiff_t nwindows = count_windows (root_window);
6080 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6081 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6083 /* Kludge Alert!
6084 Mark all windows now on frame as "deleted".
6085 Restoring the new configuration "undeletes" any that are in it.
6087 Save their current buffers in their height fields, since we may
6088 need it later, if a buffer saved in the configuration is now
6089 dead. */
6090 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6092 for (k = 0; k < saved_windows->header.size; k++)
6094 p = SAVED_WINDOW_N (saved_windows, k);
6095 window = p->window;
6096 w = XWINDOW (window);
6097 wset_next (w, Qnil);
6099 if (!NILP (p->parent))
6100 wset_parent
6101 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6102 else
6103 wset_parent (w, Qnil);
6105 if (!NILP (p->prev))
6107 wset_prev
6108 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6109 wset_next (XWINDOW (w->prev), p->window);
6111 else
6113 wset_prev (w, Qnil);
6114 if (!NILP (w->parent))
6115 wset_combination (XWINDOW (w->parent),
6116 (XINT (p->total_cols)
6117 != XWINDOW (w->parent)->total_cols),
6118 p->window);
6121 /* If we squirreled away the buffer, restore it now. */
6122 if (BUFFERP (w->combination_limit))
6123 wset_buffer (w, w->combination_limit);
6124 w->pixel_left = XFASTINT (p->pixel_left);
6125 w->pixel_top = XFASTINT (p->pixel_top);
6126 w->pixel_width = XFASTINT (p->pixel_width);
6127 w->pixel_height = XFASTINT (p->pixel_height);
6128 w->left_col = XFASTINT (p->left_col);
6129 w->top_line = XFASTINT (p->top_line);
6130 w->total_cols = XFASTINT (p->total_cols);
6131 w->total_lines = XFASTINT (p->total_lines);
6132 wset_normal_cols (w, p->normal_cols);
6133 wset_normal_lines (w, p->normal_lines);
6134 w->hscroll = XFASTINT (p->hscroll);
6135 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6136 w->min_hscroll = XFASTINT (p->min_hscroll);
6137 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6138 wset_display_table (w, p->display_table);
6139 w->left_margin_cols = XINT (p->left_margin_cols);
6140 w->right_margin_cols = XINT (p->right_margin_cols);
6141 w->left_fringe_width = XINT (p->left_fringe_width);
6142 w->right_fringe_width = XINT (p->right_fringe_width);
6143 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6144 w->scroll_bar_width = XINT (p->scroll_bar_width);
6145 w->scroll_bar_height = XINT (p->scroll_bar_height);
6146 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6147 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6148 wset_dedicated (w, p->dedicated);
6149 wset_combination_limit (w, p->combination_limit);
6150 /* Restore any window parameters that have been saved.
6151 Parameters that have not been saved are left alone. */
6152 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6154 pers = XCAR (tem);
6155 if (CONSP (pers))
6157 if (NILP (XCDR (pers)))
6159 par = Fassq (XCAR (pers), w->window_parameters);
6160 if (CONSP (par) && !NILP (XCDR (par)))
6161 /* Reset a parameter to nil if and only if it
6162 has a non-nil association. Don't make new
6163 associations. */
6164 Fsetcdr (par, Qnil);
6166 else
6167 /* Always restore a non-nil value. */
6168 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6172 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6173 /* If saved buffer is alive, install it. */
6175 wset_buffer (w, p->buffer);
6176 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6177 set_marker_restricted (w->start, p->start, w->contents);
6178 set_marker_restricted (w->pointm, p->pointm, w->contents);
6179 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6180 /* As documented in Fcurrent_window_configuration, don't
6181 restore the location of point in the buffer which was
6182 current when the window configuration was recorded. */
6183 if (!EQ (p->buffer, new_current_buffer)
6184 && XBUFFER (p->buffer) == current_buffer)
6185 Fgoto_char (w->pointm);
6187 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6188 /* Keep window's old buffer; make sure the markers are real. */
6190 /* Set window markers at start of visible range. */
6191 if (XMARKER (w->start)->buffer == 0)
6192 set_marker_restricted_both (w->start, w->contents, 0, 0);
6193 if (XMARKER (w->pointm)->buffer == 0)
6194 set_marker_restricted_both
6195 (w->pointm, w->contents,
6196 BUF_PT (XBUFFER (w->contents)),
6197 BUF_PT_BYTE (XBUFFER (w->contents)));
6198 if (XMARKER (w->old_pointm)->buffer == 0)
6199 set_marker_restricted_both
6200 (w->old_pointm, w->contents,
6201 BUF_PT (XBUFFER (w->contents)),
6202 BUF_PT_BYTE (XBUFFER (w->contents)));
6203 w->start_at_line_beg = true;
6205 else if (!NILP (w->start))
6206 /* Leaf window has no live buffer, get one. */
6208 /* Get the buffer via other_buffer_safely in order to
6209 avoid showing an unimportant buffer and, if necessary, to
6210 recreate *scratch* in the course (part of Juanma's bs-show
6211 scenario from March 2011). */
6212 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6213 /* This will set the markers to beginning of visible
6214 range. */
6215 set_marker_restricted_both (w->start, w->contents, 0, 0);
6216 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6217 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6218 w->start_at_line_beg = true;
6219 if (!NILP (w->dedicated))
6220 /* Record this window as dead. */
6221 dead_windows = Fcons (window, dead_windows);
6222 /* Make sure window is no more dedicated. */
6223 wset_dedicated (w, Qnil);
6227 fset_root_window (f, data->root_window);
6228 /* Arrange *not* to restore point in the buffer that was
6229 current when the window configuration was saved. */
6230 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6231 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6232 make_number (old_point),
6233 XWINDOW (data->current_window)->contents);
6235 /* In the following call to `select-window', prevent "swapping out
6236 point" in the old selected window using the buffer that has
6237 been restored into it. We already swapped out that point from
6238 that window's old buffer.
6240 Do not record the buffer here. We do that in a separate call
6241 to select_window below. See also Bug#16207. */
6242 select_window (data->current_window, Qt, true);
6243 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6244 last_selected_window)
6245 = selected_window;
6247 if (NILP (data->focus_frame)
6248 || (FRAMEP (data->focus_frame)
6249 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6250 Fredirect_frame_focus (frame, data->focus_frame);
6252 /* Now, free glyph matrices in windows that were not reused. */
6253 for (i = 0; i < n_leaf_windows; i++)
6254 if (NILP (leaf_windows[i]->contents))
6255 free_window_matrices (leaf_windows[i]);
6257 /* Allow x_set_window_size again and apply frame size changes if
6258 needed. */
6259 f->can_x_set_window_size = true;
6260 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6262 adjust_frame_glyphs (f);
6263 unblock_input ();
6265 /* Scan dead buffer windows. */
6266 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6268 window = XCAR (dead_windows);
6269 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6270 delete_deletable_window (window);
6273 /* Record the selected window's buffer here. The window should
6274 already be the selected one from the call above. */
6275 select_window (data->current_window, Qnil, false);
6277 /* Fselect_window will have made f the selected frame, so we
6278 reselect the proper frame here. Fhandle_switch_frame will change the
6279 selected window too, but that doesn't make the call to
6280 Fselect_window above totally superfluous; it still sets f's
6281 selected window. */
6282 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6283 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6285 run_window_configuration_change_hook (f);
6288 if (!NILP (new_current_buffer))
6290 Fset_buffer (new_current_buffer);
6291 /* If the new current buffer doesn't appear in the selected
6292 window, go to its old point (see bug#12208). */
6293 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6294 Fgoto_char (make_number (old_point));
6297 Vminibuf_scroll_window = data->minibuf_scroll_window;
6298 minibuf_selected_window = data->minibuf_selected_window;
6300 SAFE_FREE ();
6301 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6305 void
6306 restore_window_configuration (Lisp_Object configuration)
6308 Fset_window_configuration (configuration);
6312 /* If WINDOW is an internal window, recursively delete all child windows
6313 reachable via the next and contents slots of WINDOW. Otherwise setup
6314 WINDOW to not show any buffer. */
6316 void
6317 delete_all_child_windows (Lisp_Object window)
6319 register struct window *w;
6321 w = XWINDOW (window);
6323 if (!NILP (w->next))
6324 /* Delete WINDOW's siblings (we traverse postorderly). */
6325 delete_all_child_windows (w->next);
6327 if (WINDOWP (w->contents))
6329 delete_all_child_windows (w->contents);
6330 wset_combination (w, false, Qnil);
6332 else if (BUFFERP (w->contents))
6334 unshow_buffer (w);
6335 unchain_marker (XMARKER (w->pointm));
6336 unchain_marker (XMARKER (w->old_pointm));
6337 unchain_marker (XMARKER (w->start));
6338 /* Since combination limit makes sense for an internal windows
6339 only, we use this slot to save the buffer for the sake of
6340 possible resurrection in Fset_window_configuration. */
6341 wset_combination_limit (w, w->contents);
6342 wset_buffer (w, Qnil);
6345 Vwindow_list = Qnil;
6348 static ptrdiff_t
6349 count_windows (struct window *window)
6351 ptrdiff_t count = 1;
6352 if (!NILP (window->next))
6353 count += count_windows (XWINDOW (window->next));
6354 if (WINDOWP (window->contents))
6355 count += count_windows (XWINDOW (window->contents));
6356 return count;
6360 /* Fill vector FLAT with leaf windows under W, starting at index I.
6361 Value is last index + 1. */
6362 static ptrdiff_t
6363 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6365 while (w)
6367 if (WINDOWP (w->contents))
6368 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6369 else
6370 flat[i++] = w;
6372 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6375 return i;
6379 /* Return a pointer to the glyph W's physical cursor is on. Value is
6380 null if W's current matrix is invalid, so that no meaningful glyph
6381 can be returned. */
6382 struct glyph *
6383 get_phys_cursor_glyph (struct window *w)
6385 struct glyph_row *row;
6386 struct glyph *glyph;
6387 int hpos = w->phys_cursor.hpos;
6389 if (!(w->phys_cursor.vpos >= 0
6390 && w->phys_cursor.vpos < w->current_matrix->nrows))
6391 return NULL;
6393 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6394 if (!row->enabled_p)
6395 return NULL;
6397 if (w->hscroll)
6399 /* When the window is hscrolled, cursor hpos can legitimately be
6400 out of bounds, but we draw the cursor at the corresponding
6401 window margin in that case. */
6402 if (!row->reversed_p && hpos < 0)
6403 hpos = 0;
6404 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6405 hpos = row->used[TEXT_AREA] - 1;
6408 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6409 glyph = row->glyphs[TEXT_AREA] + hpos;
6410 else
6411 glyph = NULL;
6413 return glyph;
6417 static ptrdiff_t
6418 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6420 struct saved_window *p;
6421 struct window *w;
6422 Lisp_Object tem, pers, par;
6424 for (; !NILP (window); window = w->next)
6426 p = SAVED_WINDOW_N (vector, i);
6427 w = XWINDOW (window);
6429 wset_temslot (w, make_number (i)); i++;
6430 p->window = window;
6431 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6432 p->pixel_left = make_number (w->pixel_left);
6433 p->pixel_top = make_number (w->pixel_top);
6434 p->pixel_width = make_number (w->pixel_width);
6435 p->pixel_height = make_number (w->pixel_height);
6436 p->left_col = make_number (w->left_col);
6437 p->top_line = make_number (w->top_line);
6438 p->total_cols = make_number (w->total_cols);
6439 p->total_lines = make_number (w->total_lines);
6440 p->normal_cols = w->normal_cols;
6441 p->normal_lines = w->normal_lines;
6442 XSETFASTINT (p->hscroll, w->hscroll);
6443 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6444 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6445 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6446 p->display_table = w->display_table;
6447 p->left_margin_cols = make_number (w->left_margin_cols);
6448 p->right_margin_cols = make_number (w->right_margin_cols);
6449 p->left_fringe_width = make_number (w->left_fringe_width);
6450 p->right_fringe_width = make_number (w->right_fringe_width);
6451 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6452 p->scroll_bar_width = make_number (w->scroll_bar_width);
6453 p->scroll_bar_height = make_number (w->scroll_bar_height);
6454 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6455 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6456 p->dedicated = w->dedicated;
6457 p->combination_limit = w->combination_limit;
6458 p->window_parameters = Qnil;
6460 if (!NILP (Vwindow_persistent_parameters))
6462 /* Run cycle detection on Vwindow_persistent_parameters. */
6463 Lisp_Object tortoise, hare;
6465 hare = tortoise = Vwindow_persistent_parameters;
6466 while (CONSP (hare))
6468 hare = XCDR (hare);
6469 if (!CONSP (hare))
6470 break;
6472 hare = XCDR (hare);
6473 tortoise = XCDR (tortoise);
6475 if (EQ (hare, tortoise))
6476 /* Reset Vwindow_persistent_parameters to Qnil. */
6478 Vwindow_persistent_parameters = Qnil;
6479 break;
6483 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6484 tem = XCDR (tem))
6486 pers = XCAR (tem);
6487 /* Save values for persistent window parameters. */
6488 if (CONSP (pers) && !NILP (XCDR (pers)))
6490 par = Fassq (XCAR (pers), w->window_parameters);
6491 if (NILP (par))
6492 /* If the window has no value for the parameter,
6493 make one. */
6494 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6495 p->window_parameters);
6496 else
6497 /* If the window has a value for the parameter,
6498 save it. */
6499 p->window_parameters = Fcons (Fcons (XCAR (par),
6500 XCDR (par)),
6501 p->window_parameters);
6506 if (BUFFERP (w->contents))
6508 /* Save w's value of point in the window configuration. If w
6509 is the selected window, then get the value of point from
6510 the buffer; pointm is garbage in the selected window. */
6511 if (EQ (window, selected_window))
6512 p->pointm = build_marker (XBUFFER (w->contents),
6513 BUF_PT (XBUFFER (w->contents)),
6514 BUF_PT_BYTE (XBUFFER (w->contents)));
6515 else
6516 p->pointm = Fcopy_marker (w->pointm, Qnil);
6517 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6518 XMARKER (p->pointm)->insertion_type
6519 = !NILP (buffer_local_value /* Don't signal error if void. */
6520 (Qwindow_point_insertion_type, w->contents));
6521 XMARKER (p->old_pointm)->insertion_type
6522 = !NILP (buffer_local_value /* Don't signal error if void. */
6523 (Qwindow_point_insertion_type, w->contents));
6525 p->start = Fcopy_marker (w->start, Qnil);
6526 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6528 else
6530 p->pointm = Qnil;
6531 p->old_pointm = Qnil;
6532 p->start = Qnil;
6533 p->start_at_line_beg = Qnil;
6536 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6537 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6539 if (WINDOWP (w->contents))
6540 i = save_window_save (w->contents, vector, i);
6543 return i;
6546 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6547 Scurrent_window_configuration, 0, 1, 0,
6548 doc: /* Return an object representing the current window configuration of FRAME.
6549 If FRAME is nil or omitted, use the selected frame.
6550 This describes the number of windows, their sizes and current buffers,
6551 and for each displayed buffer, where display starts, and the position of
6552 point. An exception is made for point in the current buffer:
6553 its value is -not- saved.
6554 This also records the currently selected frame, and FRAME's focus
6555 redirection (see `redirect-frame-focus'). The variable
6556 `window-persistent-parameters' specifies which window parameters are
6557 saved by this function. */)
6558 (Lisp_Object frame)
6560 Lisp_Object tem;
6561 ptrdiff_t i, n_windows;
6562 struct save_window_data *data;
6563 struct frame *f = decode_live_frame (frame);
6565 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6566 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6567 PVEC_WINDOW_CONFIGURATION);
6569 data->frame_cols = FRAME_COLS (f);
6570 data->frame_lines = FRAME_LINES (f);
6571 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6572 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6573 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6574 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6575 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6576 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6577 data->selected_frame = selected_frame;
6578 data->current_window = FRAME_SELECTED_WINDOW (f);
6579 XSETBUFFER (data->current_buffer, current_buffer);
6580 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6581 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6582 data->root_window = FRAME_ROOT_WINDOW (f);
6583 data->focus_frame = FRAME_FOCUS_FRAME (f);
6584 tem = make_uninit_vector (n_windows);
6585 data->saved_windows = tem;
6586 for (i = 0; i < n_windows; i++)
6587 ASET (tem, i,
6588 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6589 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6590 XSETWINDOW_CONFIGURATION (tem, data);
6591 return (tem);
6594 /* Called after W's margins, fringes or scroll bars was adjusted. */
6596 static void
6597 apply_window_adjustment (struct window *w)
6599 eassert (w);
6600 clear_glyph_matrix (w->current_matrix);
6601 w->window_end_valid = false;
6602 windows_or_buffers_changed = 30;
6603 wset_redisplay (w);
6604 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6608 /***********************************************************************
6609 Marginal Areas
6610 ***********************************************************************/
6612 static int
6613 extract_dimension (Lisp_Object dimension)
6615 if (NILP (dimension))
6616 return -1;
6617 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6618 return XINT (dimension);
6621 static struct window *
6622 set_window_margins (struct window *w, Lisp_Object left_width,
6623 Lisp_Object right_width)
6625 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6626 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6627 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6629 if (w->left_margin_cols != left || w->right_margin_cols != right)
6631 /* Don't change anything if new margins won't fit. */
6632 if ((WINDOW_PIXEL_WIDTH (w)
6633 - WINDOW_FRINGES_WIDTH (w)
6634 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6635 - (left + right) * unit)
6636 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6638 w->left_margin_cols = left;
6639 w->right_margin_cols = right;
6641 return w;
6643 else
6644 return NULL;
6646 else
6647 return NULL;
6650 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6651 2, 3, 0,
6652 doc: /* Set width of marginal areas of window WINDOW.
6653 WINDOW must be a live window and defaults to the selected one.
6655 Second arg LEFT-WIDTH specifies the number of character cells to
6656 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6657 does the same for the right marginal area. A nil width parameter
6658 means no margin.
6660 Return t if any margin was actually changed and nil otherwise. */)
6661 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6663 struct window *w = set_window_margins (decode_live_window (window),
6664 left_width, right_width);
6665 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6669 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6670 0, 1, 0,
6671 doc: /* Get width of marginal areas of window WINDOW.
6672 WINDOW must be a live window and defaults to the selected one.
6674 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6675 If a marginal area does not exist, its width will be returned
6676 as nil. */)
6677 (Lisp_Object window)
6679 struct window *w = decode_live_window (window);
6680 return Fcons (w->left_margin_cols
6681 ? make_number (w->left_margin_cols) : Qnil,
6682 w->right_margin_cols
6683 ? make_number (w->right_margin_cols) : Qnil);
6688 /***********************************************************************
6689 Fringes
6690 ***********************************************************************/
6692 static struct window *
6693 set_window_fringes (struct window *w, Lisp_Object left_width,
6694 Lisp_Object right_width, Lisp_Object outside_margins)
6696 bool outside = !NILP (outside_margins);
6697 int left = extract_dimension (left_width);
6698 int right = extract_dimension (right_width);
6700 /* Do nothing on a tty or if nothing to actually change. */
6701 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6702 && (w->left_fringe_width != left
6703 || w->right_fringe_width != right
6704 || w->fringes_outside_margins != outside))
6706 if (left > 0 || right > 0)
6708 /* Don't change anything if new fringes don't fit. */
6709 if ((WINDOW_PIXEL_WIDTH (w)
6710 - WINDOW_MARGINS_WIDTH (w)
6711 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6712 - max (left, 0) - max (right, 0))
6713 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6714 return NULL;
6717 w->left_fringe_width = left;
6718 w->right_fringe_width = right;
6719 w->fringes_outside_margins = outside;
6721 return w;
6723 else
6724 return NULL;
6727 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6728 2, 4, 0,
6729 doc: /* Set the fringe widths of window WINDOW.
6730 WINDOW must be a live window and defaults to the selected one.
6732 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6733 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6734 fringe width. If a fringe width arg is nil, that means to use the
6735 frame's default fringe width. Default fringe widths can be set with
6736 the command `set-fringe-style'.
6737 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6738 outside of the display margins. By default, fringes are drawn between
6739 display marginal areas and the text area.
6741 Return t if any fringe was actually changed and nil otherwise. */)
6742 (Lisp_Object window, Lisp_Object left_width,
6743 Lisp_Object right_width, Lisp_Object outside_margins)
6745 struct window *w
6746 = set_window_fringes (decode_live_window (window),
6747 left_width, right_width, outside_margins);
6748 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6752 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6753 0, 1, 0,
6754 doc: /* Get width of fringes of window WINDOW.
6755 WINDOW must be a live window and defaults to the selected one.
6757 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6758 (Lisp_Object window)
6760 struct window *w = decode_live_window (window);
6762 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6763 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6764 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6769 /***********************************************************************
6770 Scroll bars
6771 ***********************************************************************/
6773 static struct window *
6774 set_window_scroll_bars (struct window *w, Lisp_Object width,
6775 Lisp_Object vertical_type, Lisp_Object height,
6776 Lisp_Object horizontal_type)
6778 int iwidth = extract_dimension (width);
6779 bool changed = false;
6781 if (iwidth == 0)
6782 vertical_type = Qnil;
6784 if (!(NILP (vertical_type)
6785 || EQ (vertical_type, Qleft)
6786 || EQ (vertical_type, Qright)
6787 || EQ (vertical_type, Qt)))
6788 error ("Invalid type of vertical scroll bar");
6790 if (w->scroll_bar_width != iwidth
6791 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6793 /* Don't change anything if new scroll bar won't fit. */
6794 if ((WINDOW_PIXEL_WIDTH (w)
6795 - WINDOW_MARGINS_WIDTH (w)
6796 - WINDOW_FRINGES_WIDTH (w)
6797 - max (iwidth, 0))
6798 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6800 w->scroll_bar_width = iwidth;
6801 wset_vertical_scroll_bar_type (w, vertical_type);
6802 changed = true;
6806 #if USE_HORIZONTAL_SCROLL_BARS
6808 int iheight = extract_dimension (height);
6810 if (MINI_WINDOW_P (w) || iheight == 0)
6811 horizontal_type = Qnil;
6813 if (!(NILP (horizontal_type)
6814 || EQ (horizontal_type, Qbottom)
6815 || EQ (horizontal_type, Qt)))
6816 error ("Invalid type of horizontal scroll bar");
6818 if (w->scroll_bar_height != iheight
6819 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6821 /* Don't change anything if new scroll bar won't fit. */
6822 if ((WINDOW_PIXEL_HEIGHT (w)
6823 - WINDOW_HEADER_LINE_HEIGHT (w)
6824 - WINDOW_MODE_LINE_HEIGHT (w)
6825 - max (iheight, 0))
6826 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
6828 w->scroll_bar_height = iheight;
6829 wset_horizontal_scroll_bar_type (w, horizontal_type);
6830 changed = true;
6834 #else
6835 wset_horizontal_scroll_bar_type (w, Qnil);
6836 #endif
6838 return changed ? w : NULL;
6841 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6842 Sset_window_scroll_bars, 1, 5, 0,
6843 doc: /* Set width and type of scroll bars of window WINDOW.
6844 WINDOW must be a live window and defaults to the selected one.
6846 Second parameter WIDTH specifies the pixel width for the vertical scroll
6847 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
6848 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6849 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
6850 frame's scroll-bar type.
6852 Fourth parameter HEIGHT specifies the pixel height for the horizontal
6853 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
6854 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
6855 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
6856 means to use the frame's horizontal scroll-bar type.
6858 Return t if scroll bars were actually changed and nil otherwise. */)
6859 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
6860 Lisp_Object height, Lisp_Object horizontal_type)
6862 struct window *w
6863 = set_window_scroll_bars (decode_live_window (window),
6864 width, vertical_type, height, horizontal_type);
6865 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6869 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6870 0, 1, 0,
6871 doc: /* Get width and type of scroll bars of window WINDOW.
6872 WINDOW must be a live window and defaults to the selected one.
6874 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
6875 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
6876 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
6877 value. */)
6878 (Lisp_Object window)
6880 struct window *w = decode_live_window (window);
6882 return Fcons (((w->scroll_bar_width >= 0)
6883 ? make_number (w->scroll_bar_width)
6884 : Qnil),
6885 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6886 w->vertical_scroll_bar_type,
6887 ((w->scroll_bar_height >= 0)
6888 ? make_number (w->scroll_bar_height)
6889 : Qnil),
6890 make_number (WINDOW_SCROLL_BAR_LINES (w)),
6891 w->horizontal_scroll_bar_type));
6894 /***********************************************************************
6895 Smooth scrolling
6896 ***********************************************************************/
6898 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6899 doc: /* Return the amount by which WINDOW is scrolled vertically.
6900 If WINDOW is omitted or nil, it defaults to the selected window.
6901 Normally, value is a multiple of the canonical character height of WINDOW;
6902 optional second arg PIXELS-P means value is measured in pixels. */)
6903 (Lisp_Object window, Lisp_Object pixels_p)
6905 Lisp_Object result;
6906 struct window *w = decode_live_window (window);
6907 struct frame *f = XFRAME (w->frame);
6909 if (FRAME_WINDOW_P (f))
6910 result = (NILP (pixels_p)
6911 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6912 : make_number (-w->vscroll));
6913 else
6914 result = make_number (0);
6915 return result;
6919 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6920 2, 3, 0,
6921 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6922 WINDOW nil means use the selected window. Normally, VSCROLL is a
6923 non-negative multiple of the canonical character height of WINDOW;
6924 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6925 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6926 corresponds to an integral number of pixels. The return value is the
6927 result of this rounding.
6928 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6929 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6931 struct window *w = decode_live_window (window);
6932 struct frame *f = XFRAME (w->frame);
6934 CHECK_NUMBER_OR_FLOAT (vscroll);
6936 if (FRAME_WINDOW_P (f))
6938 int old_dy = w->vscroll;
6940 w->vscroll = - (NILP (pixels_p)
6941 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6942 : XFLOATINT (vscroll));
6943 w->vscroll = min (w->vscroll, 0);
6945 if (w->vscroll != old_dy)
6947 /* Adjust glyph matrix of the frame if the virtual display
6948 area becomes larger than before. */
6949 if (w->vscroll < 0 && w->vscroll < old_dy)
6950 adjust_frame_glyphs (f);
6952 /* Prevent redisplay shortcuts. */
6953 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
6957 return Fwindow_vscroll (window, pixels_p);
6961 /* Call FN for all leaf windows on frame F. FN is called with the
6962 first argument being a pointer to the leaf window, and with
6963 additional argument USER_DATA. Stops when FN returns 0. */
6965 static void
6966 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
6967 void *user_data)
6969 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6970 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6971 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6975 /* Helper function for foreach_window. Call FN for all leaf windows
6976 reachable from W. FN is called with the first argument being a
6977 pointer to the leaf window, and with additional argument USER_DATA.
6978 Stop when FN returns false. Value is false if stopped by FN. */
6980 static bool
6981 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
6982 void *user_data)
6984 bool cont;
6986 for (cont = true; w && cont;)
6988 if (WINDOWP (w->contents))
6989 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6990 else
6991 cont = fn (w, user_data);
6993 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6996 return cont;
6999 /***********************************************************************
7000 Initialization
7001 ***********************************************************************/
7003 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7004 describe the same state of affairs. This is used by Fequal.
7006 IGNORE_POSITIONS means ignore non-matching scroll positions
7007 and the like.
7009 This ignores a couple of things like the dedication status of
7010 window, combination_limit and the like. This might have to be
7011 fixed. */
7013 bool
7014 compare_window_configurations (Lisp_Object configuration1,
7015 Lisp_Object configuration2,
7016 bool ignore_positions)
7018 register struct save_window_data *d1, *d2;
7019 struct Lisp_Vector *sws1, *sws2;
7020 ptrdiff_t i;
7022 CHECK_WINDOW_CONFIGURATION (configuration1);
7023 CHECK_WINDOW_CONFIGURATION (configuration2);
7025 d1 = (struct save_window_data *) XVECTOR (configuration1);
7026 d2 = (struct save_window_data *) XVECTOR (configuration2);
7027 sws1 = XVECTOR (d1->saved_windows);
7028 sws2 = XVECTOR (d2->saved_windows);
7030 /* Frame settings must match. */
7031 if (d1->frame_cols != d2->frame_cols
7032 || d1->frame_lines != d2->frame_lines
7033 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7034 || !EQ (d1->selected_frame, d2->selected_frame)
7035 || !EQ (d1->current_buffer, d2->current_buffer)
7036 || (!ignore_positions
7037 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7038 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7039 || !EQ (d1->focus_frame, d2->focus_frame)
7040 /* Verify that the two configurations have the same number of windows. */
7041 || sws1->header.size != sws2->header.size)
7042 return false;
7044 for (i = 0; i < sws1->header.size; i++)
7046 struct saved_window *sw1, *sw2;
7048 sw1 = SAVED_WINDOW_N (sws1, i);
7049 sw2 = SAVED_WINDOW_N (sws2, i);
7051 if (
7052 /* The "current" windows in the two configurations must
7053 correspond to each other. */
7054 EQ (d1->current_window, sw1->window)
7055 != EQ (d2->current_window, sw2->window)
7056 /* Windows' buffers must match. */
7057 || !EQ (sw1->buffer, sw2->buffer)
7058 || !EQ (sw1->pixel_left, sw2->pixel_left)
7059 || !EQ (sw1->pixel_top, sw2->pixel_top)
7060 || !EQ (sw1->pixel_height, sw2->pixel_height)
7061 || !EQ (sw1->pixel_width, sw2->pixel_width)
7062 || !EQ (sw1->left_col, sw2->left_col)
7063 || !EQ (sw1->top_line, sw2->top_line)
7064 || !EQ (sw1->total_cols, sw2->total_cols)
7065 || !EQ (sw1->total_lines, sw2->total_lines)
7066 || !EQ (sw1->display_table, sw2->display_table)
7067 /* The next two disjuncts check the window structure for
7068 equality. */
7069 || !EQ (sw1->parent, sw2->parent)
7070 || !EQ (sw1->prev, sw2->prev)
7071 || (!ignore_positions
7072 && (!EQ (sw1->hscroll, sw2->hscroll)
7073 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7074 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7075 || NILP (Fequal (sw1->start, sw2->start))
7076 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7077 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7078 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7079 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7080 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7081 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7082 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7083 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7084 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7085 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7086 return false;
7089 return true;
7092 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7093 Scompare_window_configurations, 2, 2, 0,
7094 doc: /* Compare two window configurations as regards the structure of windows.
7095 This function ignores details such as the values of point
7096 and scrolling positions. */)
7097 (Lisp_Object x, Lisp_Object y)
7099 if (compare_window_configurations (x, y, true))
7100 return Qt;
7101 return Qnil;
7104 void
7105 init_window_once (void)
7107 struct frame *f = make_initial_frame ();
7108 XSETFRAME (selected_frame, f);
7109 Vterminal_frame = selected_frame;
7110 minibuf_window = f->minibuffer_window;
7111 selected_window = f->selected_window;
7114 void
7115 init_window (void)
7117 Vwindow_list = Qnil;
7120 void
7121 syms_of_window (void)
7123 DEFSYM (Qscroll_up, "scroll-up");
7124 DEFSYM (Qscroll_down, "scroll-down");
7125 DEFSYM (Qscroll_command, "scroll-command");
7127 Fput (Qscroll_up, Qscroll_command, Qt);
7128 Fput (Qscroll_down, Qscroll_command, Qt);
7130 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7131 DEFSYM (Qwindowp, "windowp");
7132 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7133 DEFSYM (Qwindow_live_p, "window-live-p");
7134 DEFSYM (Qwindow_valid_p, "window-valid-p");
7135 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7136 DEFSYM (Qdelete_window, "delete-window");
7137 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7138 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7139 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7140 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7141 DEFSYM (Qsafe, "safe");
7142 DEFSYM (Qdisplay_buffer, "display-buffer");
7143 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7144 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7145 DEFSYM (Qget_mru_window, "get-mru-window");
7146 DEFSYM (Qwindow_size, "window-size");
7147 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7148 DEFSYM (Qabove, "above");
7149 DEFSYM (Qclone_of, "clone-of");
7150 DEFSYM (Qfloor, "floor");
7151 DEFSYM (Qceiling, "ceiling");
7153 staticpro (&Vwindow_list);
7155 minibuf_selected_window = Qnil;
7156 staticpro (&minibuf_selected_window);
7158 window_scroll_pixel_based_preserve_x = -1;
7159 window_scroll_pixel_based_preserve_y = -1;
7160 window_scroll_preserve_hpos = -1;
7161 window_scroll_preserve_vpos = -1;
7163 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7164 doc: /* Non-nil means call as function to display a help buffer.
7165 The function is called with one argument, the buffer to be displayed.
7166 Used by `with-output-to-temp-buffer'.
7167 If this function is used, then it must do the entire job of showing
7168 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7169 Vtemp_buffer_show_function = Qnil;
7171 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7172 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7173 Vminibuf_scroll_window = Qnil;
7175 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7176 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7177 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7178 is displayed in the `mode-line' face. */);
7179 mode_line_in_non_selected_windows = true;
7181 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7182 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7183 Vother_window_scroll_buffer = Qnil;
7185 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7186 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7187 auto_window_vscroll_p = true;
7189 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7190 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7191 next_screen_context_lines = 2;
7193 DEFVAR_LISP ("scroll-preserve-screen-position",
7194 Vscroll_preserve_screen_position,
7195 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7196 A value of nil means point does not keep its screen position except
7197 at the scroll margin or window boundary respectively.
7198 A value of t means point keeps its screen position if the scroll
7199 command moved it vertically out of the window, e.g. when scrolling
7200 by full screens.
7201 Any other value means point always keeps its screen position.
7202 Scroll commands should have the `scroll-command' property
7203 on their symbols to be controlled by this variable. */);
7204 Vscroll_preserve_screen_position = Qnil;
7206 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7207 doc: /* Type of marker to use for `window-point'. */);
7208 Vwindow_point_insertion_type = Qnil;
7209 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7211 DEFVAR_LISP ("window-configuration-change-hook",
7212 Vwindow_configuration_change_hook,
7213 doc: /* Functions to call when window configuration changes.
7214 The buffer-local part is run once per window, with the relevant window
7215 selected; while the global part is run only once for the modified frame,
7216 with the relevant frame selected. */);
7217 Vwindow_configuration_change_hook = Qnil;
7219 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7220 doc: /* Non-nil means `recenter' redraws entire frame.
7221 If this option is non-nil, then the `recenter' command with a nil
7222 argument will redraw the entire frame; the special value `tty' causes
7223 the frame to be redrawn only if it is a tty frame. */);
7224 Vrecenter_redisplay = Qtty;
7226 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7227 doc: /* If t, resize window combinations proportionally.
7228 If this variable is nil, splitting a window gets the entire screen space
7229 for displaying the new window from the window to split. Deleting and
7230 resizing a window preferably resizes one adjacent window only.
7232 If this variable is t, splitting a window tries to get the space
7233 proportionally from all windows in the same combination. This also
7234 allows to split a window that is otherwise too small or of fixed size.
7235 Resizing and deleting a window proportionally resize all windows in the
7236 same combination.
7238 Other values are reserved for future use.
7240 This variable takes no effect if the variable `window-combination-limit' is
7241 non-nil. */);
7242 Vwindow_combination_resize = Qnil;
7244 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7245 doc: /* If non-nil, splitting a window makes a new parent window.
7246 The following values are recognized:
7248 nil means splitting a window will create a new parent window only if the
7249 window has no parent window or the window shall become part of a
7250 combination orthogonal to the one it is part of.
7252 `window-size' means that splitting a window for displaying a buffer
7253 makes a new parent window provided `display-buffer' is supposed to
7254 explicitly set the window's size due to the presence of a
7255 `window-height' or `window-width' entry in the alist used by
7256 `display-buffer'. Otherwise, this value is handled like nil.
7258 `temp-buffer' means that splitting a window for displaying a temporary
7259 buffer always makes a new parent window. Otherwise, this value is
7260 handled like nil.
7262 `display-buffer' means that splitting a window for displaying a buffer
7263 always makes a new parent window. Since temporary buffers are
7264 displayed by the function `display-buffer', this value is stronger
7265 than `temp-buffer'. Splitting a window for other purpose makes a
7266 new parent window only if needed.
7268 t means that splitting a window always creates a new parent window. If
7269 all splits behave this way, each frame's window tree is a binary
7270 tree and every window but the frame's root window has exactly one
7271 sibling.
7273 Other values are reserved for future use. */);
7274 Vwindow_combination_limit = Qwindow_size;
7276 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7277 doc: /* Alist of persistent window parameters.
7278 This alist specifies which window parameters shall get saved by
7279 `current-window-configuration' and `window-state-get' and subsequently
7280 restored to their previous values by `set-window-configuration' and
7281 `window-state-put'.
7283 The car of each entry of this alist is the symbol specifying the
7284 parameter. The cdr is one of the following:
7286 nil means the parameter is neither saved by `window-state-get' nor by
7287 `current-window-configuration'.
7289 t means the parameter is saved by `current-window-configuration' and,
7290 provided its WRITABLE argument is nil, by `window-state-get'.
7292 The symbol `writable' means the parameter is saved unconditionally by
7293 both `current-window-configuration' and `window-state-get'. Do not use
7294 this value for parameters without read syntax (like windows or frames).
7296 Parameters not saved by `current-window-configuration' or
7297 `window-state-get' are left alone by `set-window-configuration'
7298 respectively are not installed by `window-state-put'. */);
7299 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7301 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7302 doc: /* Non-nil means resize windows pixelwise.
7303 This currently affects the functions: `split-window', `maximize-window',
7304 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7305 all functions that symmetrically resize a parent window.
7307 Note that when a frame's pixel size is not a multiple of the
7308 frame's character size, at least one window may get resized
7309 pixelwise even if this option is nil. */);
7310 window_resize_pixelwise = false;
7312 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7313 Vfast_but_imprecise_scrolling,
7314 doc: /* When non-nil, accelerate scrolling operations.
7315 This comes into play when scrolling rapidly over previously
7316 unfontified buffer regions. Only those portions of the buffer which
7317 are actually going to be displayed get fontified.
7319 Note that this optimization can cause the portion of the buffer
7320 displayed after a scrolling operation to be somewhat inaccurate. */);
7321 Vfast_but_imprecise_scrolling = false;
7323 defsubr (&Sselected_window);
7324 defsubr (&Sminibuffer_window);
7325 defsubr (&Swindow_minibuffer_p);
7326 defsubr (&Swindowp);
7327 defsubr (&Swindow_valid_p);
7328 defsubr (&Swindow_live_p);
7329 defsubr (&Swindow_frame);
7330 defsubr (&Sframe_root_window);
7331 defsubr (&Sframe_first_window);
7332 defsubr (&Sframe_selected_window);
7333 defsubr (&Sset_frame_selected_window);
7334 defsubr (&Spos_visible_in_window_p);
7335 defsubr (&Swindow_line_height);
7336 defsubr (&Swindow_buffer);
7337 defsubr (&Swindow_parent);
7338 defsubr (&Swindow_top_child);
7339 defsubr (&Swindow_left_child);
7340 defsubr (&Swindow_next_sibling);
7341 defsubr (&Swindow_prev_sibling);
7342 defsubr (&Swindow_combination_limit);
7343 defsubr (&Sset_window_combination_limit);
7344 defsubr (&Swindow_use_time);
7345 defsubr (&Swindow_pixel_width);
7346 defsubr (&Swindow_pixel_height);
7347 defsubr (&Swindow_total_width);
7348 defsubr (&Swindow_total_height);
7349 defsubr (&Swindow_normal_size);
7350 defsubr (&Swindow_new_pixel);
7351 defsubr (&Swindow_new_total);
7352 defsubr (&Swindow_new_normal);
7353 defsubr (&Swindow_pixel_left);
7354 defsubr (&Swindow_pixel_top);
7355 defsubr (&Swindow_left_column);
7356 defsubr (&Swindow_top_line);
7357 defsubr (&Sset_window_new_pixel);
7358 defsubr (&Sset_window_new_total);
7359 defsubr (&Sset_window_new_normal);
7360 defsubr (&Swindow_resize_apply);
7361 defsubr (&Swindow_resize_apply_total);
7362 defsubr (&Swindow_body_height);
7363 defsubr (&Swindow_body_width);
7364 defsubr (&Swindow_hscroll);
7365 defsubr (&Sset_window_hscroll);
7366 defsubr (&Swindow_redisplay_end_trigger);
7367 defsubr (&Sset_window_redisplay_end_trigger);
7368 defsubr (&Swindow_mode_line_height);
7369 defsubr (&Swindow_header_line_height);
7370 defsubr (&Swindow_right_divider_width);
7371 defsubr (&Swindow_bottom_divider_width);
7372 defsubr (&Swindow_scroll_bar_width);
7373 defsubr (&Swindow_scroll_bar_height);
7374 defsubr (&Scoordinates_in_window_p);
7375 defsubr (&Swindow_at);
7376 defsubr (&Swindow_point);
7377 defsubr (&Swindow_old_point);
7378 defsubr (&Swindow_start);
7379 defsubr (&Swindow_end);
7380 defsubr (&Sset_window_point);
7381 defsubr (&Sset_window_start);
7382 defsubr (&Swindow_dedicated_p);
7383 defsubr (&Sset_window_dedicated_p);
7384 defsubr (&Swindow_display_table);
7385 defsubr (&Sset_window_display_table);
7386 defsubr (&Snext_window);
7387 defsubr (&Sprevious_window);
7388 defsubr (&Swindow__sanitize_window_sizes);
7389 defsubr (&Sget_buffer_window);
7390 defsubr (&Sdelete_other_windows_internal);
7391 defsubr (&Sdelete_window_internal);
7392 defsubr (&Sresize_mini_window_internal);
7393 defsubr (&Sset_window_buffer);
7394 defsubr (&Srun_window_configuration_change_hook);
7395 defsubr (&Srun_window_scroll_functions);
7396 defsubr (&Sselect_window);
7397 defsubr (&Sforce_window_update);
7398 defsubr (&Ssplit_window_internal);
7399 defsubr (&Sscroll_up);
7400 defsubr (&Sscroll_down);
7401 defsubr (&Sscroll_left);
7402 defsubr (&Sscroll_right);
7403 defsubr (&Sother_window_for_scrolling);
7404 defsubr (&Sscroll_other_window);
7405 defsubr (&Sminibuffer_selected_window);
7406 defsubr (&Srecenter);
7407 defsubr (&Swindow_text_width);
7408 defsubr (&Swindow_text_height);
7409 defsubr (&Smove_to_window_line);
7410 defsubr (&Swindow_configuration_p);
7411 defsubr (&Swindow_configuration_frame);
7412 defsubr (&Sset_window_configuration);
7413 defsubr (&Scurrent_window_configuration);
7414 defsubr (&Sset_window_margins);
7415 defsubr (&Swindow_margins);
7416 defsubr (&Sset_window_fringes);
7417 defsubr (&Swindow_fringes);
7418 defsubr (&Sset_window_scroll_bars);
7419 defsubr (&Swindow_scroll_bars);
7420 defsubr (&Swindow_vscroll);
7421 defsubr (&Sset_window_vscroll);
7422 defsubr (&Scompare_window_configurations);
7423 defsubr (&Swindow_list);
7424 defsubr (&Swindow_list_1);
7425 defsubr (&Swindow_prev_buffers);
7426 defsubr (&Sset_window_prev_buffers);
7427 defsubr (&Swindow_next_buffers);
7428 defsubr (&Sset_window_next_buffers);
7429 defsubr (&Swindow_parameters);
7430 defsubr (&Swindow_parameter);
7431 defsubr (&Sset_window_parameter);
7434 void
7435 keys_of_window (void)
7437 initial_define_key (control_x_map, '<', "scroll-left");
7438 initial_define_key (control_x_map, '>', "scroll-right");
7440 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7441 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7442 initial_define_key (meta_map, 'v', "scroll-down-command");