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