Remove old cl-assert calls in 'newline'
[emacs.git] / src / window.c
blobba86d73911f76b69cb6ce7f25ad3e3afdcc594a5
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2017 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 (at
11 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 <https://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28 #include "keymap.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "commands.h"
32 #include "indent.h"
33 #include "termchar.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "blockinput.h"
37 #include "termhooks.h" /* For FRAME_TERMINAL. */
38 #include "xwidget.h"
39 #ifdef HAVE_WINDOW_SYSTEM
40 #include TERM_HEADER
41 #endif /* HAVE_WINDOW_SYSTEM */
42 #ifdef MSDOS
43 #include "msdos.h"
44 #endif
46 static ptrdiff_t count_windows (struct window *);
47 static ptrdiff_t get_leaf_windows (struct window *, struct window **,
48 ptrdiff_t);
49 static void window_scroll_pixel_based (Lisp_Object, int, bool, bool);
50 static void window_scroll_line_based (Lisp_Object, int, bool, bool);
51 static void foreach_window (struct frame *,
52 bool (* fn) (struct window *, void *),
53 void *);
54 static bool foreach_window_1 (struct window *,
55 bool (* fn) (struct window *, void *),
56 void *);
57 static bool window_resize_check (struct window *, bool);
58 static void window_resize_apply (struct window *, bool);
59 static void select_window_1 (Lisp_Object, bool);
60 static void run_window_configuration_change_hook (struct frame *);
62 static struct window *set_window_fringes (struct window *, Lisp_Object,
63 Lisp_Object, Lisp_Object);
64 static struct window *set_window_margins (struct window *, Lisp_Object,
65 Lisp_Object);
66 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
67 Lisp_Object, Lisp_Object,
68 Lisp_Object);
69 static void apply_window_adjustment (struct window *);
71 /* This is the window in which the terminal's cursor should
72 be left when nothing is being done with it. This must
73 always be a leaf window, and its buffer is selected by
74 the top level editing loop at the end of each command.
76 This value is always the same as
77 FRAME_SELECTED_WINDOW (selected_frame). */
78 Lisp_Object selected_window;
80 /* A list of all windows for use by next_window and Fwindow_list.
81 Functions creating or deleting windows should invalidate this cache
82 by setting it to nil. */
83 Lisp_Object Vwindow_list;
85 /* The mini-buffer window of the selected frame.
86 Note that you cannot test for mini-bufferness of an arbitrary window
87 by comparing against this; but you can test for mini-bufferness of
88 the selected window. */
89 Lisp_Object minibuf_window;
91 /* Non-nil means it is the window whose mode line should be
92 shown as the selected window when the minibuffer is selected. */
93 Lisp_Object minibuf_selected_window;
95 /* Incremented for each window created. */
96 static EMACS_INT sequence_number;
98 /* Used by the function window_scroll_pixel_based. */
99 static int window_scroll_pixel_based_preserve_x;
100 static int window_scroll_pixel_based_preserve_y;
102 /* Same for window_scroll_line_based. */
103 static EMACS_INT window_scroll_preserve_hpos;
104 static EMACS_INT window_scroll_preserve_vpos;
106 static void
107 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
109 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
112 /* These setters are used only in this file, so they can be private. */
113 static void
114 wset_combination_limit (struct window *w, Lisp_Object val)
116 w->combination_limit = val;
119 static void
120 wset_dedicated (struct window *w, Lisp_Object val)
122 w->dedicated = val;
125 static void
126 wset_display_table (struct window *w, Lisp_Object val)
128 w->display_table = val;
131 static void
132 wset_new_normal (struct window *w, Lisp_Object val)
134 w->new_normal = val;
137 static void
138 wset_new_total (struct window *w, Lisp_Object val)
140 w->new_total = val;
143 static void
144 wset_normal_cols (struct window *w, Lisp_Object val)
146 w->normal_cols = val;
149 static void
150 wset_normal_lines (struct window *w, Lisp_Object val)
152 w->normal_lines = val;
155 static void
156 wset_parent (struct window *w, Lisp_Object val)
158 w->parent = val;
161 static void
162 wset_pointm (struct window *w, Lisp_Object val)
164 w->pointm = val;
167 static void
168 wset_old_pointm (struct window *w, Lisp_Object val)
170 w->old_pointm = val;
173 static void
174 wset_start (struct window *w, Lisp_Object val)
176 w->start = val;
179 static void
180 wset_temslot (struct window *w, Lisp_Object val)
182 w->temslot = val;
185 static void
186 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
188 w->vertical_scroll_bar_type = val;
191 static void
192 wset_window_parameters (struct window *w, Lisp_Object val)
194 w->window_parameters = val;
197 static void
198 wset_combination (struct window *w, bool horflag, Lisp_Object val)
200 /* Since leaf windows never becomes non-leaf, there should
201 be no buffer and markers in start and pointm fields of W. */
202 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
203 w->contents = val;
204 /* When an internal window is deleted and VAL is nil, HORFLAG
205 is meaningless. */
206 if (!NILP (val))
207 w->horizontal = horflag;
210 static void
211 wset_update_mode_line (struct window *w)
213 /* If this window is the selected window on its frame, set the
214 global variable update_mode_lines, so that x_consider_frame_title
215 will consider this frame's title for redisplay. */
216 Lisp_Object fselected_window = XFRAME (WINDOW_FRAME (w))->selected_window;
218 if (WINDOWP (fselected_window) && XWINDOW (fselected_window) == w)
219 update_mode_lines = 42;
220 else
221 w->update_mode_line = true;
224 /* True if leaf window W doesn't reflect the actual state
225 of displayed buffer due to its text or overlays change. */
227 bool
228 window_outdated (struct window *w)
230 struct buffer *b = XBUFFER (w->contents);
231 return (w->last_modified < BUF_MODIFF (b)
232 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
235 struct window *
236 decode_live_window (register Lisp_Object window)
238 if (NILP (window))
239 return XWINDOW (selected_window);
241 CHECK_LIVE_WINDOW (window);
242 return XWINDOW (window);
245 struct window *
246 decode_any_window (register Lisp_Object window)
248 struct window *w;
250 if (NILP (window))
251 return XWINDOW (selected_window);
253 CHECK_WINDOW (window);
254 w = XWINDOW (window);
255 return w;
258 static struct window *
259 decode_valid_window (register Lisp_Object window)
261 struct window *w;
263 if (NILP (window))
264 return XWINDOW (selected_window);
266 CHECK_VALID_WINDOW (window);
267 w = XWINDOW (window);
268 return w;
271 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
272 cease its buffer, and 1 means that W is about to set up the new one. */
274 static void
275 adjust_window_count (struct window *w, int arg)
277 eassert (eabs (arg) == 1);
278 if (BUFFERP (w->contents))
280 struct buffer *b = XBUFFER (w->contents);
282 if (b->base_buffer)
283 b = b->base_buffer;
284 b->window_count += arg;
285 eassert (b->window_count >= 0);
286 /* These should be recalculated by redisplay code. */
287 w->window_end_valid = false;
288 w->base_line_pos = 0;
292 /* Set W's buffer slot to VAL and recompute number
293 of windows showing VAL if it is a buffer. */
295 void
296 wset_buffer (struct window *w, Lisp_Object val)
298 adjust_window_count (w, -1);
299 if (BUFFERP (val))
300 /* Make sure that we do not assign the buffer
301 to an internal window. */
302 eassert (MARKERP (w->start) && MARKERP (w->pointm));
303 w->contents = val;
304 adjust_window_count (w, 1);
307 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
308 doc: /* Return t if OBJECT is a window and nil otherwise. */)
309 (Lisp_Object object)
311 return WINDOWP (object) ? Qt : Qnil;
314 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
315 doc: /* Return t if OBJECT is a valid window and nil otherwise.
316 A valid window is either a window that displays a buffer or an internal
317 window. Windows that have been deleted are not valid. */)
318 (Lisp_Object object)
320 return WINDOW_VALID_P (object) ? Qt : Qnil;
323 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
324 doc: /* Return t if OBJECT is a live window and nil otherwise.
325 A live window is a window that displays a buffer.
326 Internal windows and deleted windows are not live. */)
327 (Lisp_Object object)
329 return WINDOW_LIVE_P (object) ? Qt : Qnil;
332 /* Frames and windows. */
333 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
334 doc: /* Return the frame that window WINDOW is on.
335 WINDOW must be a valid window and defaults to the selected one. */)
336 (Lisp_Object window)
338 return decode_valid_window (window)->frame;
341 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
342 doc: /* Return the root window of FRAME-OR-WINDOW.
343 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
344 With a frame argument, return that frame's root window.
345 With a window argument, return the root window of that window's frame. */)
346 (Lisp_Object frame_or_window)
348 Lisp_Object window;
350 if (NILP (frame_or_window))
351 window = SELECTED_FRAME ()->root_window;
352 else if (WINDOW_VALID_P (frame_or_window))
353 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
354 else
356 CHECK_LIVE_FRAME (frame_or_window);
357 window = XFRAME (frame_or_window)->root_window;
360 return window;
363 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
364 doc: /* Return the minibuffer window for frame FRAME.
365 If FRAME is omitted or nil, it defaults to the selected frame. */)
366 (Lisp_Object frame)
368 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
371 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
372 Swindow_minibuffer_p, 0, 1, 0,
373 doc: /* Return non-nil if WINDOW is a minibuffer window.
374 WINDOW must be a valid window and defaults to the selected one. */)
375 (Lisp_Object window)
377 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
380 /* Don't move this to window.el - this must be a safe routine. */
381 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
382 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
383 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
384 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
385 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
386 the first window of that frame. */)
387 (Lisp_Object frame_or_window)
389 Lisp_Object window;
391 if (NILP (frame_or_window))
392 window = SELECTED_FRAME ()->root_window;
393 else if (WINDOW_VALID_P (frame_or_window))
394 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
395 else
397 CHECK_LIVE_FRAME (frame_or_window);
398 window = XFRAME (frame_or_window)->root_window;
401 while (WINDOWP (XWINDOW (window)->contents))
402 window = XWINDOW (window)->contents;
404 return window;
407 DEFUN ("frame-selected-window", Fframe_selected_window,
408 Sframe_selected_window, 0, 1, 0,
409 doc: /* Return the selected window of FRAME-OR-WINDOW.
410 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
411 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
412 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
413 return the selected window of that frame. */)
414 (Lisp_Object frame_or_window)
416 Lisp_Object window;
418 if (NILP (frame_or_window))
419 window = SELECTED_FRAME ()->selected_window;
420 else if (WINDOW_VALID_P (frame_or_window))
421 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
422 else
424 CHECK_LIVE_FRAME (frame_or_window);
425 window = XFRAME (frame_or_window)->selected_window;
428 return window;
431 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
432 Sset_frame_selected_window, 2, 3, 0,
433 doc: /* Set selected window of FRAME to WINDOW.
434 FRAME must be a live frame and defaults to the selected one. If FRAME
435 is the selected frame, this makes WINDOW the selected window. Optional
436 argument NORECORD non-nil means to neither change the order of recently
437 selected windows nor the buffer list. WINDOW must denote a live window.
438 Return WINDOW. */)
439 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
441 if (NILP (frame))
442 frame = selected_frame;
444 CHECK_LIVE_FRAME (frame);
445 CHECK_LIVE_WINDOW (window);
447 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
448 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
450 if (EQ (frame, selected_frame))
451 return Fselect_window (window, norecord);
452 else
454 fset_selected_window (XFRAME (frame), window);
455 return window;
459 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
460 doc: /* Return the selected window.
461 The selected window is the window in which the standard cursor for
462 selected windows appears and to which many commands apply. */)
463 (void)
465 return selected_window;
468 EMACS_INT window_select_count;
470 /* If select_window is called with inhibit_point_swap true it will
471 not store point of the old selected window's buffer back into that
472 window's pointm slot. This is needed by Fset_window_configuration to
473 avoid that the display routine is called with selected_window set to
474 Qnil causing a subsequent crash. */
475 static Lisp_Object
476 select_window (Lisp_Object window, Lisp_Object norecord,
477 bool inhibit_point_swap)
479 struct window *w;
480 struct frame *sf;
482 CHECK_LIVE_WINDOW (window);
484 w = XWINDOW (window);
486 /* Make the selected window's buffer current. */
487 Fset_buffer (w->contents);
489 if (EQ (window, selected_window) && !inhibit_point_swap)
490 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
491 way to call record_buffer from Elisp, so it's important that we call
492 record_buffer before returning here. */
493 goto record_and_return;
495 if (NILP (norecord) || EQ (norecord, Qmark_for_redisplay))
496 { /* Mark the window for redisplay since the selected-window has
497 a different mode-line. */
498 wset_redisplay (XWINDOW (selected_window));
499 wset_redisplay (w);
501 else
502 redisplay_other_windows ();
504 sf = SELECTED_FRAME ();
505 if (XFRAME (WINDOW_FRAME (w)) != sf)
507 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
508 /* Use this rather than Fhandle_switch_frame
509 so that FRAME_FOCUS_FRAME is moved appropriately as we
510 move around in the state where a minibuffer in a separate
511 frame is active. */
512 Fselect_frame (WINDOW_FRAME (w), norecord);
513 /* Fselect_frame called us back so we've done all the work already. */
514 eassert (EQ (window, selected_window));
515 return window;
517 else
518 fset_selected_window (sf, window);
520 select_window_1 (window, inhibit_point_swap);
521 bset_last_selected_window (XBUFFER (w->contents), window);
523 record_and_return:
524 /* record_buffer can call maybe_quit, so make sure it is run only
525 after we have re-established the invariant between
526 selected_window and selected_frame, otherwise the temporary
527 broken invariant might "escape" (Bug#14161). */
528 if (NILP (norecord))
530 w->use_time = ++window_select_count;
531 record_buffer (w->contents);
534 return window;
537 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
538 (not even for redisplay's benefit), and assume that the window's frame is
539 already selected. */
540 static void
541 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
543 /* Store the old selected window's buffer's point in pointm of the old
544 selected window. It belongs to that window, and when the window is
545 not selected, must be in the window. */
546 if (!inhibit_point_swap)
548 struct window *ow = XWINDOW (selected_window);
549 if (BUFFERP (ow->contents))
550 set_marker_both (ow->pointm, ow->contents,
551 BUF_PT (XBUFFER (ow->contents)),
552 BUF_PT_BYTE (XBUFFER (ow->contents)));
555 selected_window = window;
557 /* Go to the point recorded in the window.
558 This is important when the buffer is in more
559 than one window. It also matters when
560 redisplay_window has altered point after scrolling,
561 because it makes the change only in the window. */
562 set_point_from_marker (XWINDOW (window)->pointm);
565 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
566 doc: /* Select WINDOW which must be a live window.
567 Also make WINDOW's frame the selected frame and WINDOW that frame's
568 selected window. In addition, make WINDOW's buffer current and set its
569 buffer's value of `point' to the value of WINDOW's `window-point'.
570 Return WINDOW.
572 Optional second arg NORECORD non-nil means do not put this buffer at the
573 front of the buffer list and do not make this window the most recently
574 selected one. Also, do not mark WINDOW for redisplay unless NORECORD
575 equals the special symbol `mark-for-redisplay'.
577 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
578 applications and internal routines often select a window temporarily for
579 various purposes; mostly, to simplify coding. As a rule, such
580 selections should be not recorded and therefore will not pollute
581 `buffer-list-update-hook'. Selections that "really count" are those
582 causing a visible change in the next redisplay of WINDOW's frame and
583 should be always recorded. So if you think of running a function each
584 time a window gets selected put it on `buffer-list-update-hook'.
586 Also note that the main editor command loop sets the current buffer to
587 the buffer of the selected window before each command. */)
588 (Lisp_Object window, Lisp_Object norecord)
590 return select_window (window, norecord, false);
593 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
594 doc: /* Return the buffer displayed in window WINDOW.
595 If WINDOW is omitted or nil, it defaults to the selected window.
596 Return nil for an internal window or a deleted window. */)
597 (Lisp_Object window)
599 struct window *w = decode_any_window (window);
600 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
603 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
604 doc: /* Return the parent window of window WINDOW.
605 WINDOW must be a valid window and defaults to the selected one.
606 Return nil for a window with no parent (e.g. a root window). */)
607 (Lisp_Object window)
609 return decode_valid_window (window)->parent;
612 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
613 doc: /* Return the topmost child window of window WINDOW.
614 WINDOW must be a valid window and defaults to the selected one.
615 Return nil if WINDOW is a live window (live windows have no children).
616 Return nil if WINDOW is an internal window whose children form a
617 horizontal combination. */)
618 (Lisp_Object window)
620 struct window *w = decode_valid_window (window);
621 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
624 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
625 doc: /* Return the leftmost child window of window WINDOW.
626 WINDOW must be a valid window and defaults to the selected one.
627 Return nil if WINDOW is a live window (live windows have no children).
628 Return nil if WINDOW is an internal window whose children form a
629 vertical combination. */)
630 (Lisp_Object window)
632 struct window *w = decode_valid_window (window);
633 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
636 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
637 doc: /* Return the next sibling window of window WINDOW.
638 WINDOW must be a valid window and defaults to the selected one.
639 Return nil if WINDOW has no next sibling. */)
640 (Lisp_Object window)
642 return decode_valid_window (window)->next;
645 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
646 doc: /* Return the previous sibling window of window WINDOW.
647 WINDOW must be a valid window and defaults to the selected one.
648 Return nil if WINDOW has no previous sibling. */)
649 (Lisp_Object window)
651 return decode_valid_window (window)->prev;
654 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
655 doc: /* Return combination limit of window WINDOW.
656 WINDOW must be a valid window used in horizontal or vertical combination.
657 If the return value is nil, child windows of WINDOW can be recombined with
658 WINDOW's siblings. A return value of t means that child windows of
659 WINDOW are never (re-)combined with WINDOW's siblings. */)
660 (Lisp_Object window)
662 struct window *w;
664 CHECK_VALID_WINDOW (window);
665 w = XWINDOW (window);
666 if (WINDOW_LEAF_P (w))
667 error ("Combination limit is meaningful for internal windows only");
668 return w->combination_limit;
671 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
672 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
673 WINDOW must be a valid window used in horizontal or vertical combination.
674 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
675 siblings. LIMIT t means that child windows of WINDOW are never
676 \(re-)combined with WINDOW's siblings. Other values are reserved for
677 future use. */)
678 (Lisp_Object window, Lisp_Object limit)
680 struct window *w;
682 CHECK_VALID_WINDOW (window);
683 w = XWINDOW (window);
684 if (WINDOW_LEAF_P (w))
685 error ("Combination limit is meaningful for internal windows only");
686 wset_combination_limit (w, limit);
687 return limit;
690 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
691 doc: /* Return the use time of window WINDOW.
692 WINDOW must be a live window and defaults to the selected one.
693 The window with the highest use time is the most recently selected
694 one. The window with the lowest use time is the least recently
695 selected one. */)
696 (Lisp_Object window)
698 return make_number (decode_live_window (window)->use_time);
701 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
702 doc: /* Return the width of window WINDOW in pixels.
703 WINDOW must be a valid window and defaults to the selected one.
705 The return value includes the fringes and margins of WINDOW as well as
706 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
707 an internal window, its pixel width is the width of the screen areas
708 spanned by its children. */)
709 (Lisp_Object window)
711 return make_number (decode_valid_window (window)->pixel_width);
714 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
715 doc: /* Return the height of window WINDOW in pixels.
716 WINDOW must be a valid window and defaults to the selected one.
718 The return value includes the mode line and header line and the bottom
719 divider, if any. If WINDOW is an internal window, its pixel height is
720 the height of the screen areas spanned by its children. */)
721 (Lisp_Object window)
723 return make_number (decode_valid_window (window)->pixel_height);
726 DEFUN ("window-pixel-width-before-size-change",
727 Fwindow_pixel_width_before_size_change,
728 Swindow_pixel_width_before_size_change, 0, 1, 0,
729 doc: /* Return pixel width of window WINDOW before last size changes.
730 WINDOW must be a valid window and defaults to the selected one.
732 The return value is the pixel width of WINDOW at the last time
733 `window-size-change-functions' was run. It's zero if WINDOW was made
734 after that. */)
735 (Lisp_Object window)
737 return (make_number
738 (decode_valid_window (window)->pixel_width_before_size_change));
741 DEFUN ("window-pixel-height-before-size-change",
742 Fwindow_pixel_height_before_size_change,
743 Swindow_pixel_height_before_size_change, 0, 1, 0,
744 doc: /* Return pixel height of window WINDOW before last size changes.
745 WINDOW must be a valid window and defaults to the selected one.
747 The return value is the pixel height of WINDOW at the last time
748 `window-size-change-functions' was run. It's zero if WINDOW was made
749 after that. */)
750 (Lisp_Object window)
752 return (make_number
753 (decode_valid_window (window)->pixel_height_before_size_change));
756 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
757 doc: /* Return the height of window WINDOW in lines.
758 WINDOW must be a valid window and defaults to the selected one.
760 The return value includes the heights of WINDOW's mode and header line
761 and its bottom divider, if any. If WINDOW is an internal window, the
762 total height is the height of the screen areas spanned by its children.
764 If WINDOW's pixel height is not an integral multiple of its frame's
765 character height, the number of lines occupied by WINDOW is rounded
766 internally. This is done in a way such that, if WINDOW is a parent
767 window, the sum of the total heights of all its children internally
768 equals the total height of WINDOW.
770 If the optional argument ROUND is `ceiling', return the smallest integer
771 larger than WINDOW's pixel height divided by the character height of
772 WINDOW's frame. ROUND `floor' means to return the largest integer
773 smaller than WINDOW's pixel height divided by the character height of
774 WINDOW's frame. Any other value of ROUND means to return the internal
775 total height of WINDOW. */)
776 (Lisp_Object window, Lisp_Object round)
778 struct window *w = decode_valid_window (window);
780 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
781 return make_number (w->total_lines);
782 else
784 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
786 return make_number (EQ (round, Qceiling)
787 ? ((w->pixel_height + unit - 1) /unit)
788 : (w->pixel_height / unit));
792 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
793 doc: /* Return the total width of window WINDOW in columns.
794 WINDOW must be a valid window and defaults to the selected one.
796 The return value includes the widths of WINDOW's fringes, margins,
797 scroll bars and its right divider, if any. If WINDOW is an internal
798 window, the total width is the width of the screen areas spanned by its
799 children.
801 If WINDOW's pixel width is not an integral multiple of its frame's
802 character width, the number of lines occupied by WINDOW is rounded
803 internally. This is done in a way such that, if WINDOW is a parent
804 window, the sum of the total widths of all its children internally
805 equals the total width of WINDOW.
807 If the optional argument ROUND is `ceiling', return the smallest integer
808 larger than WINDOW's pixel width divided by the character width of
809 WINDOW's frame. ROUND `floor' means to return the largest integer
810 smaller than WINDOW's pixel width divided by the character width of
811 WINDOW's frame. Any other value of ROUND means to return the internal
812 total width of WINDOW. */)
813 (Lisp_Object window, Lisp_Object round)
815 struct window *w = decode_valid_window (window);
817 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
818 return make_number (w->total_cols);
819 else
821 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
823 return make_number (EQ (round, Qceiling)
824 ? ((w->pixel_width + unit - 1) /unit)
825 : (w->pixel_width / unit));
829 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
830 doc: /* Return the new total size of window WINDOW.
831 WINDOW must be a valid window and defaults to the selected one.
833 The new total size of WINDOW is the value set by the last call of
834 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
835 installed as WINDOW's total height (see `window-total-height') or total
836 width (see `window-total-width'). */)
837 (Lisp_Object window)
839 return decode_valid_window (window)->new_total;
842 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
843 doc: /* Return the normal height of window WINDOW.
844 WINDOW must be a valid window and defaults to the selected one.
845 If HORIZONTAL is non-nil, return the normal width of WINDOW.
847 The normal height of a frame's root window or a window that is
848 horizontally combined (a window that has a left or right sibling) is
849 1.0. The normal height of a window that is vertically combined (has a
850 sibling above or below) is the fraction of the window's height with
851 respect to its parent. The sum of the normal heights of all windows in a
852 vertical combination equals 1.0.
854 Similarly, the normal width of a frame's root window or a window that is
855 vertically combined equals 1.0. The normal width of a window that is
856 horizontally combined is the fraction of the window's width with respect
857 to its parent. The sum of the normal widths of all windows in a
858 horizontal combination equals 1.0.
860 The normal sizes of windows are used to restore the proportional sizes
861 of windows after they have been shrunk to their minimum sizes; for
862 example when a frame is temporarily made very small and afterwards gets
863 re-enlarged to its previous size. */)
864 (Lisp_Object window, Lisp_Object horizontal)
866 struct window *w = decode_valid_window (window);
868 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
871 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
872 doc: /* Return new normal size of window WINDOW.
873 WINDOW must be a valid window and defaults to the selected one.
875 The new normal size of WINDOW is the value set by the last call of
876 `set-window-new-normal' for WINDOW. If valid, it will be shortly
877 installed as WINDOW's normal size (see `window-normal-size'). */)
878 (Lisp_Object window)
880 return decode_valid_window (window)->new_normal;
883 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
884 doc: /* Return new pixel size of window WINDOW.
885 WINDOW must be a valid window and defaults to the selected one.
887 The new pixel size of WINDOW is the value set by the last call of
888 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
889 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
890 width (see `window-pixel-width'). */)
891 (Lisp_Object window)
893 return decode_valid_window (window)->new_pixel;
896 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
897 doc: /* Return left pixel edge of window WINDOW.
898 WINDOW must be a valid window and defaults to the selected one. */)
899 (Lisp_Object window)
901 return make_number (decode_valid_window (window)->pixel_left);
904 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
905 doc: /* Return top pixel edge of window WINDOW.
906 WINDOW must be a valid window and defaults to the selected one. */)
907 (Lisp_Object window)
909 return make_number (decode_valid_window (window)->pixel_top);
912 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
913 doc: /* Return left column of window WINDOW.
914 This is the distance, in columns, between the left edge of WINDOW and
915 the left edge of the frame's window area. For instance, the return
916 value is 0 if there is no window to the left of WINDOW.
918 WINDOW must be a valid window and defaults to the selected one. */)
919 (Lisp_Object window)
921 return make_number (decode_valid_window (window)->left_col);
924 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
925 doc: /* Return top line of window WINDOW.
926 This is the distance, in lines, between the top of WINDOW and the top
927 of the frame's window area. For instance, the return value is 0 if
928 there is no window above WINDOW.
930 WINDOW must be a valid window and defaults to the selected one. */)
931 (Lisp_Object window)
933 return make_number (decode_valid_window (window)->top_line);
936 /* Return the number of lines/pixels of W's body. Don't count any mode
937 or header line or horizontal divider of W. Rounds down to nearest
938 integer when not working pixelwise. */
939 static int
940 window_body_height (struct window *w, bool pixelwise)
942 int height = (w->pixel_height
943 - WINDOW_HEADER_LINE_HEIGHT (w)
944 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
945 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
946 : 0)
947 - WINDOW_MODE_LINE_HEIGHT (w)
948 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
950 /* Don't return a negative value. */
951 return max (pixelwise
952 ? height
953 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
957 /* Return the number of columns/pixels of W's body. Don't count columns
958 occupied by the scroll bar or the divider/vertical bar separating W
959 from its right sibling or margins. On window-systems don't count
960 fringes either. Round down to nearest integer when not working
961 pixelwise. */
963 window_body_width (struct window *w, bool pixelwise)
965 struct frame *f = XFRAME (WINDOW_FRAME (w));
967 int width = (w->pixel_width
968 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
969 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
970 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
971 : (/* A vertical bar is either 1 or 0. */
972 !FRAME_WINDOW_P (f)
973 && !WINDOW_RIGHTMOST_P (w)
974 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
975 - WINDOW_MARGINS_WIDTH (w)
976 - (FRAME_WINDOW_P (f)
977 ? WINDOW_FRINGES_WIDTH (w)
978 : 0));
980 /* Don't return a negative value. */
981 return max (pixelwise
982 ? width
983 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
987 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
988 doc: /* Return the height of WINDOW's text area.
989 WINDOW must be a live window and defaults to the selected one. Optional
990 argument PIXELWISE non-nil means return the height of WINDOW's text area
991 in pixels. The return value does not include the mode line or header
992 line or any horizontal divider.
994 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
995 pixel height divided by the character height of WINDOW's frame. This
996 means that if a line at the bottom of the text area is only partially
997 visible, that line is not counted. */)
998 (Lisp_Object window, Lisp_Object pixelwise)
1000 return make_number (window_body_height (decode_live_window (window),
1001 !NILP (pixelwise)));
1004 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
1005 doc: /* Return the width of WINDOW's text area.
1006 WINDOW must be a live window and defaults to the selected one. Optional
1007 argument PIXELWISE non-nil means return the width in pixels. The return
1008 value does not include any vertical dividers, fringes or marginal areas,
1009 or scroll bars.
1011 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
1012 pixel width divided by the character width of WINDOW's frame. This
1013 means that if a column at the right of the text area is only partially
1014 visible, that column is not counted.
1016 Note that the returned value includes the column reserved for the
1017 continuation glyph. */)
1018 (Lisp_Object window, Lisp_Object pixelwise)
1020 return make_number (window_body_width (decode_live_window (window),
1021 !NILP (pixelwise)));
1024 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
1025 Swindow_mode_line_height, 0, 1, 0,
1026 doc: /* Return the height in pixels of WINDOW's mode-line.
1027 WINDOW must be a live window and defaults to the selected one. */)
1028 (Lisp_Object window)
1030 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
1033 DEFUN ("window-header-line-height", Fwindow_header_line_height,
1034 Swindow_header_line_height, 0, 1, 0,
1035 doc: /* Return the height in pixels of WINDOW's header-line.
1036 WINDOW must be a live window and defaults to the selected one. */)
1037 (Lisp_Object window)
1039 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1042 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1043 Swindow_right_divider_width, 0, 1, 0,
1044 doc: /* Return the width in pixels of WINDOW's right divider.
1045 WINDOW must be a live window and defaults to the selected one. */)
1046 (Lisp_Object window)
1048 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1051 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1052 Swindow_bottom_divider_width, 0, 1, 0,
1053 doc: /* Return the width in pixels of WINDOW's bottom divider.
1054 WINDOW must be a live window and defaults to the selected one. */)
1055 (Lisp_Object window)
1057 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1060 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1061 Swindow_scroll_bar_width, 0, 1, 0,
1062 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1063 WINDOW must be a live window and defaults to the selected one. */)
1064 (Lisp_Object window)
1066 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1069 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1070 Swindow_scroll_bar_height, 0, 1, 0,
1071 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1072 WINDOW must be a live window and defaults to the selected one. */)
1073 (Lisp_Object window)
1075 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1078 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1079 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1080 WINDOW must be a live window and defaults to the selected one. */)
1081 (Lisp_Object window)
1083 return make_number (decode_live_window (window)->hscroll);
1086 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1087 range, returning the new amount as a fixnum. */
1088 static Lisp_Object
1089 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1091 /* Horizontal scrolling has problems with large scroll amounts.
1092 It's too slow with long lines, and even with small lines the
1093 display can be messed up. For now, though, impose only the limits
1094 required by the internal representation: horizontal scrolling must
1095 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1096 (since it's stored in a ptrdiff_t). */
1097 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1098 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1100 /* Prevent redisplay shortcuts when changing the hscroll. */
1101 if (w->hscroll != new_hscroll)
1102 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1104 w->hscroll = new_hscroll;
1105 w->suspend_auto_hscroll = true;
1107 return make_number (new_hscroll);
1110 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1111 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1112 WINDOW must be a live window and defaults to the selected one.
1113 Clip the number to a reasonable value if out of range.
1114 Return the new number. NCOL should be zero or positive.
1116 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1117 window so that the location of point moves off-window. */)
1118 (Lisp_Object window, Lisp_Object ncol)
1120 CHECK_NUMBER (ncol);
1121 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1124 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1125 Swindow_redisplay_end_trigger, 0, 1, 0,
1126 doc: /* Return WINDOW's redisplay end trigger value.
1127 WINDOW must be a live window and defaults to the selected one.
1128 See `set-window-redisplay-end-trigger' for more information. */)
1129 (Lisp_Object window)
1131 return decode_live_window (window)->redisplay_end_trigger;
1134 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1135 Sset_window_redisplay_end_trigger, 2, 2, 0,
1136 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1137 WINDOW must be a live window and defaults to the selected one. VALUE
1138 should be a buffer position (typically a marker) or nil. If it is a
1139 buffer position, then if redisplay in WINDOW reaches a position beyond
1140 VALUE, the functions in `redisplay-end-trigger-functions' are called
1141 with two arguments: WINDOW, and the end trigger value. Afterwards the
1142 end-trigger value is reset to nil. */)
1143 (register Lisp_Object window, Lisp_Object value)
1145 wset_redisplay_end_trigger (decode_live_window (window), value);
1146 return value;
1149 /* Test if the character at column X, row Y is within window W.
1150 If it is not, return ON_NOTHING;
1151 if it is on the window's vertical divider, return
1152 ON_RIGHT_DIVIDER;
1153 if it is on the window's horizontal divider, return
1154 ON_BOTTOM_DIVIDER;
1155 if it is in the window's text area, return ON_TEXT;
1156 if it is on the window's modeline, return ON_MODE_LINE;
1157 if it is on the border between the window and its right sibling,
1158 return ON_VERTICAL_BORDER;
1159 if it is on a scroll bar, return ON_SCROLL_BAR;
1160 if it is on the window's top line, return ON_HEADER_LINE;
1161 if it is in left or right fringe of the window,
1162 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1163 if it is in the marginal area to the left/right of the window,
1164 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1166 X and Y are frame relative pixel coordinates. */
1168 static enum window_part
1169 coordinates_in_window (register struct window *w, int x, int y)
1171 struct frame *f = XFRAME (WINDOW_FRAME (w));
1172 enum window_part part;
1173 int ux = FRAME_COLUMN_WIDTH (f);
1174 int left_x = WINDOW_LEFT_EDGE_X (w);
1175 int right_x = WINDOW_RIGHT_EDGE_X (w);
1176 int top_y = WINDOW_TOP_EDGE_Y (w);
1177 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1178 /* The width of the area where the vertical line can be dragged.
1179 (Between mode lines for instance. */
1180 int grabbable_width = ux;
1181 int lmargin_width, rmargin_width, text_left, text_right;
1183 /* Outside any interesting row or column? */
1184 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1185 return ON_NOTHING;
1187 /* On the horizontal window divider (which prevails the vertical
1188 divider)? */
1189 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1190 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1191 && y <= bottom_y)
1192 return ON_BOTTOM_DIVIDER;
1193 /* On vertical window divider? */
1194 else if (!WINDOW_RIGHTMOST_P (w)
1195 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1196 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1197 && x <= right_x)
1198 return ON_RIGHT_DIVIDER;
1199 /* On the horizontal scroll bar? (Including the empty space at its
1200 right!) */
1201 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1202 && y >= (bottom_y
1203 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1204 - CURRENT_MODE_LINE_HEIGHT (w)
1205 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1206 && y <= (bottom_y
1207 - CURRENT_MODE_LINE_HEIGHT (w)
1208 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1209 return ON_HORIZONTAL_SCROLL_BAR;
1210 /* On the mode or header line? */
1211 else if ((window_wants_mode_line (w)
1212 && y >= (bottom_y
1213 - CURRENT_MODE_LINE_HEIGHT (w)
1214 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1215 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1216 && (part = ON_MODE_LINE))
1217 || (window_wants_header_line (w)
1218 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1219 && (part = ON_HEADER_LINE)))
1221 /* If it's under/over the scroll bar portion of the mode/header
1222 line, say it's on the vertical line. That's to be able to
1223 resize windows horizontally in case we're using toolkit scroll
1224 bars. Note: If scrollbars are on the left, the window that
1225 must be eventually resized is that on the left of WINDOW. */
1226 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1227 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1228 && !WINDOW_LEFTMOST_P (w)
1229 && eabs (x - left_x) < grabbable_width)
1230 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1231 && !WINDOW_RIGHTMOST_P (w)
1232 && eabs (x - right_x) < grabbable_width)))
1233 return ON_VERTICAL_BORDER;
1234 else
1235 return part;
1238 /* In what's below, we subtract 1 when computing right_x because we
1239 want the rightmost pixel, which is given by left_pixel+width-1. */
1240 if (w->pseudo_window_p)
1242 left_x = 0;
1243 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1245 else
1247 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1248 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1251 /* Outside any interesting column? */
1252 if (x < left_x || x > right_x)
1253 return ON_VERTICAL_SCROLL_BAR;
1255 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1256 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1258 text_left = window_box_left (w, TEXT_AREA);
1259 text_right = text_left + window_box_width (w, TEXT_AREA);
1261 if (FRAME_WINDOW_P (f))
1263 if (!w->pseudo_window_p
1264 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1265 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1266 && !WINDOW_RIGHTMOST_P (w)
1267 && (eabs (x - right_x) < grabbable_width))
1268 return ON_VERTICAL_BORDER;
1270 /* Need to say "x > right_x" rather than >=, since on character
1271 terminals, the vertical line's x coordinate is right_x. */
1272 else if (!w->pseudo_window_p
1273 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1274 && !WINDOW_RIGHTMOST_P (w)
1275 /* Why check ux if we are not the rightmost window? Also
1276 shouldn't a pseudo window always be rightmost? */
1277 && x > right_x - ux)
1278 return ON_VERTICAL_BORDER;
1280 if (x < text_left)
1282 if (lmargin_width > 0
1283 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1284 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1285 : (x < left_x + lmargin_width)))
1286 return ON_LEFT_MARGIN;
1287 else
1288 return ON_LEFT_FRINGE;
1291 if (x >= text_right)
1293 if (rmargin_width > 0
1294 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1295 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1296 : (x >= right_x - rmargin_width)))
1297 return ON_RIGHT_MARGIN;
1298 else
1299 return ON_RIGHT_FRINGE;
1302 /* Everything special ruled out - must be on text area */
1303 return ON_TEXT;
1306 /* Take X is the frame-relative pixel x-coordinate, and return the
1307 x-coordinate relative to part PART of window W. */
1309 window_relative_x_coord (struct window *w, enum window_part part, int x)
1311 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1313 switch (part)
1315 case ON_TEXT:
1316 return x - window_box_left (w, TEXT_AREA);
1318 case ON_HEADER_LINE:
1319 case ON_MODE_LINE:
1320 case ON_LEFT_FRINGE:
1321 return x - left_x;
1323 case ON_RIGHT_FRINGE:
1324 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1326 case ON_LEFT_MARGIN:
1327 return (x - left_x
1328 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1329 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1331 case ON_RIGHT_MARGIN:
1332 return (x + 1
1333 - ((w->pseudo_window_p)
1334 ? WINDOW_PIXEL_WIDTH (w)
1335 : WINDOW_BOX_RIGHT_EDGE_X (w))
1336 + window_box_width (w, RIGHT_MARGIN_AREA)
1337 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1338 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1340 case ON_NOTHING:
1341 case ON_VERTICAL_BORDER:
1342 case ON_VERTICAL_SCROLL_BAR:
1343 case ON_HORIZONTAL_SCROLL_BAR:
1344 case ON_RIGHT_DIVIDER:
1345 case ON_BOTTOM_DIVIDER:
1346 return 0;
1348 default:
1349 emacs_abort ();
1354 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1355 Scoordinates_in_window_p, 2, 2, 0,
1356 doc: /* Return non-nil if COORDINATES are in WINDOW.
1357 WINDOW must be a live window and defaults to the selected one.
1358 COORDINATES is a cons of the form (X . Y), X and Y being distances
1359 measured in characters from the upper-left corner of the frame.
1360 \(0 . 0) denotes the character in the upper left corner of the
1361 frame.
1362 If COORDINATES are in the text portion of WINDOW,
1363 the coordinates relative to the window are returned.
1364 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1365 If they are in the right divider of WINDOW, `right-divider' is returned.
1366 If they are in the mode line of WINDOW, `mode-line' is returned.
1367 If they are in the header line of WINDOW, `header-line' is returned.
1368 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1369 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1370 If they are on the border between WINDOW and its right sibling,
1371 `vertical-line' is returned.
1372 If they are in the windows's left or right marginal areas, `left-margin'\n\
1373 or `right-margin' is returned. */)
1374 (register Lisp_Object coordinates, Lisp_Object window)
1376 struct window *w;
1377 struct frame *f;
1378 int x, y;
1379 Lisp_Object lx, ly;
1381 w = decode_live_window (window);
1382 f = XFRAME (w->frame);
1383 CHECK_CONS (coordinates);
1384 lx = Fcar (coordinates);
1385 ly = Fcdr (coordinates);
1386 CHECK_NUMBER_OR_FLOAT (lx);
1387 CHECK_NUMBER_OR_FLOAT (ly);
1388 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1389 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1391 switch (coordinates_in_window (w, x, y))
1393 case ON_NOTHING:
1394 return Qnil;
1396 case ON_TEXT:
1397 /* Convert X and Y to window relative pixel coordinates, and
1398 return the canonical char units. */
1399 x -= window_box_left (w, TEXT_AREA);
1400 y -= WINDOW_TOP_EDGE_Y (w);
1401 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1402 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1404 case ON_MODE_LINE:
1405 return Qmode_line;
1407 case ON_VERTICAL_BORDER:
1408 return Qvertical_line;
1410 case ON_HEADER_LINE:
1411 return Qheader_line;
1413 case ON_LEFT_FRINGE:
1414 return Qleft_fringe;
1416 case ON_RIGHT_FRINGE:
1417 return Qright_fringe;
1419 case ON_LEFT_MARGIN:
1420 return Qleft_margin;
1422 case ON_RIGHT_MARGIN:
1423 return Qright_margin;
1425 case ON_VERTICAL_SCROLL_BAR:
1426 /* Historically we are supposed to return nil in this case. */
1427 return Qnil;
1429 case ON_HORIZONTAL_SCROLL_BAR:
1430 return Qnil;
1432 case ON_RIGHT_DIVIDER:
1433 return Qright_divider;
1435 case ON_BOTTOM_DIVIDER:
1436 return Qbottom_divider;
1438 default:
1439 emacs_abort ();
1444 /* Callback for foreach_window, used in window_from_coordinates.
1445 Check if window W contains coordinates specified by USER_DATA which
1446 is actually a pointer to a struct check_window_data CW.
1448 Check if window W contains coordinates *CW->x and *CW->y. If it
1449 does, return W in *CW->window, as Lisp_Object, and return in
1450 *CW->part the part of the window under coordinates *X,*Y. Return
1451 false from this function to stop iterating over windows. */
1453 struct check_window_data
1455 Lisp_Object *window;
1456 int x, y;
1457 enum window_part *part;
1460 static bool
1461 check_window_containing (struct window *w, void *user_data)
1463 struct check_window_data *cw = user_data;
1464 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1465 if (found == ON_NOTHING)
1466 return true;
1467 else
1469 *cw->part = found;
1470 XSETWINDOW (*cw->window, w);
1471 return false;
1476 /* Find the window containing frame-relative pixel position X/Y and
1477 return it as a Lisp_Object.
1479 If X, Y is on one of the window's special `window_part' elements,
1480 set *PART to the id of that element.
1482 If there is no window under X, Y return nil and leave *PART
1483 unmodified. TOOL_BAR_P means detect tool-bar windows.
1485 This function was previously implemented with a loop cycling over
1486 windows with Fnext_window, and starting with the frame's selected
1487 window. It turned out that this doesn't work with an
1488 implementation of next_window using Vwindow_list, because
1489 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1490 tree of F when this function is called asynchronously from
1491 note_mouse_highlight. The original loop didn't terminate in this
1492 case. */
1494 Lisp_Object
1495 window_from_coordinates (struct frame *f, int x, int y,
1496 enum window_part *part, bool tool_bar_p)
1498 Lisp_Object window;
1499 struct check_window_data cw;
1500 enum window_part dummy;
1502 if (part == 0)
1503 part = &dummy;
1505 window = Qnil;
1506 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1507 foreach_window (f, check_window_containing, &cw);
1509 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1510 /* If not found above, see if it's in the tool bar window, if a tool
1511 bar exists. */
1512 if (NILP (window)
1513 && tool_bar_p
1514 && WINDOWP (f->tool_bar_window)
1515 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1516 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1517 != ON_NOTHING))
1519 *part = ON_TEXT;
1520 window = f->tool_bar_window;
1522 #endif
1524 return window;
1527 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1528 doc: /* Return window containing coordinates X and Y on FRAME.
1529 FRAME must be a live frame and defaults to the selected one.
1530 The top left corner of the frame is considered to be row 0,
1531 column 0. */)
1532 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1534 struct frame *f = decode_live_frame (frame);
1536 /* Check that arguments are integers or floats. */
1537 CHECK_NUMBER_OR_FLOAT (x);
1538 CHECK_NUMBER_OR_FLOAT (y);
1540 return window_from_coordinates (f,
1541 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1542 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1543 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1544 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1545 0, false);
1548 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1549 doc: /* Return current value of point in WINDOW.
1550 WINDOW must be a live window and defaults to the selected one.
1552 For a nonselected window, this is the value point would have if that
1553 window were selected.
1555 Note that, when WINDOW is selected, the value returned is the same as
1556 that returned by `point' for WINDOW's buffer. It would be more strictly
1557 correct to return the top-level value of `point', outside of any
1558 `save-excursion' forms. But that is hard to define. */)
1559 (Lisp_Object window)
1561 register struct window *w = decode_live_window (window);
1563 if (w == XWINDOW (selected_window))
1564 return make_number (BUF_PT (XBUFFER (w->contents)));
1565 else
1566 return Fmarker_position (w->pointm);
1569 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1570 doc: /* Return old value of point in WINDOW.
1571 WINDOW must be a live window and defaults to the selected one. */)
1572 (Lisp_Object window)
1574 return Fmarker_position (decode_live_window (window)->old_pointm);
1577 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1578 doc: /* Return position at which display currently starts in WINDOW.
1579 WINDOW must be a live window and defaults to the selected one.
1580 This is updated by redisplay or by calling `set-window-start'. */)
1581 (Lisp_Object window)
1583 return Fmarker_position (decode_live_window (window)->start);
1586 /* This is text temporarily removed from the doc string below.
1588 This function returns nil if the position is not currently known.
1589 That happens when redisplay is preempted and doesn't finish.
1590 If in that case you want to compute where the end of the window would
1591 have been if redisplay had finished, do this:
1592 (save-excursion
1593 (goto-char (window-start window))
1594 (vertical-motion (1- (window-height window)) window)
1595 (point))") */
1597 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1598 doc: /* Return position at which display currently ends in WINDOW.
1599 WINDOW must be a live window and defaults to the selected one.
1600 This is updated by redisplay, when it runs to completion.
1601 Simply changing the buffer text or setting `window-start'
1602 does not update this value.
1603 Return nil if there is no recorded value. (This can happen if the
1604 last redisplay of WINDOW was preempted, and did not finish.)
1605 If UPDATE is non-nil, compute the up-to-date position
1606 if it isn't already recorded. */)
1607 (Lisp_Object window, Lisp_Object update)
1609 Lisp_Object value;
1610 struct window *w = decode_live_window (window);
1611 Lisp_Object buf;
1612 struct buffer *b;
1614 buf = w->contents;
1615 CHECK_BUFFER (buf);
1616 b = XBUFFER (buf);
1618 if (! NILP (update)
1619 && (windows_or_buffers_changed
1620 || !w->window_end_valid
1621 || b->clip_changed
1622 || b->prevent_redisplay_optimizations_p
1623 || window_outdated (w))
1624 /* Don't call display routines if we didn't yet create any real
1625 frames, because the glyph matrices are not yet allocated in
1626 that case. This could happen in some code that runs in the
1627 daemon during initialization (e.g., see bug#20565). */
1628 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1630 struct text_pos startp;
1631 struct it it;
1632 struct buffer *old_buffer = NULL;
1633 void *itdata = NULL;
1635 /* Cannot use Fvertical_motion because that function doesn't
1636 cope with variable-height lines. */
1637 if (b != current_buffer)
1639 old_buffer = current_buffer;
1640 set_buffer_internal (b);
1643 /* In case W->start is out of the range, use something
1644 reasonable. This situation occurred when loading a file with
1645 `-l' containing a call to `rmail' with subsequent other
1646 commands. At the end, W->start happened to be BEG, while
1647 rmail had already narrowed the buffer. */
1648 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1650 itdata = bidi_shelve_cache ();
1651 start_display (&it, w, startp);
1652 move_it_vertically (&it, window_box_height (w));
1653 if (it.current_y < it.last_visible_y)
1654 move_it_past_eol (&it);
1655 value = make_number (IT_CHARPOS (it));
1656 bidi_unshelve_cache (itdata, false);
1658 if (old_buffer)
1659 set_buffer_internal (old_buffer);
1661 else
1662 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1664 return value;
1667 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1668 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1669 WINDOW must be a live window and defaults to the selected one.
1670 Return POS. */)
1671 (Lisp_Object window, Lisp_Object pos)
1673 register struct window *w = decode_live_window (window);
1675 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1677 if (w == XWINDOW (selected_window))
1679 if (XBUFFER (w->contents) == current_buffer)
1680 Fgoto_char (pos);
1681 else
1683 struct buffer *old_buffer = current_buffer;
1685 /* ... but here we want to catch type error before buffer change. */
1686 CHECK_NUMBER_COERCE_MARKER (pos);
1687 set_buffer_internal (XBUFFER (w->contents));
1688 Fgoto_char (pos);
1689 set_buffer_internal (old_buffer);
1692 else
1694 set_marker_restricted (w->pointm, pos, w->contents);
1695 /* We have to make sure that redisplay updates the window to show
1696 the new value of point. */
1697 wset_redisplay (w);
1700 return pos;
1703 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1704 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1705 WINDOW must be a live window and defaults to the selected one. Return
1706 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1707 overriding motion of point in order to display at this exact start. */)
1708 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1710 register struct window *w = decode_live_window (window);
1712 set_marker_restricted (w->start, pos, w->contents);
1713 /* This is not right, but much easier than doing what is right. */
1714 w->start_at_line_beg = false;
1715 if (NILP (noforce))
1716 w->force_start = true;
1717 wset_update_mode_line (w);
1718 /* Bug#15957. */
1719 w->window_end_valid = false;
1720 wset_redisplay (w);
1722 return pos;
1725 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1726 Spos_visible_in_window_p, 0, 3, 0,
1727 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1728 WINDOW must be a live window and defaults to the selected one.
1730 Return nil if that position is scrolled vertically out of view. If a
1731 character is only partially visible, nil is returned, unless the
1732 optional argument PARTIALLY is non-nil. If POS is only out of view
1733 because of horizontal scrolling, return non-nil. If POS is t, it
1734 specifies either the first position displayed on the last visible
1735 screen line in WINDOW, or the end-of-buffer position, whichever comes
1736 first. POS defaults to point in WINDOW; WINDOW defaults to the
1737 selected window.
1739 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1740 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1741 where X and Y are the pixel coordinates relative to the top left corner
1742 of the window. The remaining elements are omitted if the character after
1743 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1744 off-window at the top and bottom of the screen line ("row") containing
1745 POS, ROWH is the visible height of that row, and VPOS is the row number
1746 \(zero-based). */)
1747 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1749 struct window *w;
1750 EMACS_INT posint;
1751 struct buffer *buf;
1752 struct text_pos top;
1753 Lisp_Object in_window = Qnil;
1754 int rtop, rbot, rowh, vpos;
1755 bool fully_p = true;
1756 int x, y;
1758 w = decode_live_window (window);
1759 buf = XBUFFER (w->contents);
1760 SET_TEXT_POS_FROM_MARKER (top, w->start);
1762 if (EQ (pos, Qt))
1763 posint = -1;
1764 else if (!NILP (pos))
1766 CHECK_NUMBER_COERCE_MARKER (pos);
1767 posint = XINT (pos);
1769 else if (w == XWINDOW (selected_window))
1770 posint = PT;
1771 else
1772 posint = marker_position (w->pointm);
1774 /* If position is above window start or outside buffer boundaries,
1775 or if window start is out of range, position is not visible. */
1776 if ((EQ (pos, Qt)
1777 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1778 && CHARPOS (top) >= BUF_BEGV (buf)
1779 && CHARPOS (top) <= BUF_ZV (buf)
1780 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1782 fully_p = !rtop && !rbot;
1783 if (!NILP (partially) || fully_p)
1784 in_window = Qt;
1787 if (!NILP (in_window) && !NILP (partially))
1789 Lisp_Object part = Qnil;
1790 if (!fully_p)
1791 part = list4i (rtop, rbot, rowh, vpos);
1792 in_window = Fcons (make_number (x),
1793 Fcons (make_number (y), part));
1796 return in_window;
1799 DEFUN ("window-line-height", Fwindow_line_height,
1800 Swindow_line_height, 0, 2, 0,
1801 doc: /* Return height in pixels of text line LINE in window WINDOW.
1802 WINDOW must be a live window and defaults to the selected one.
1804 Return height of current line if LINE is omitted or nil. Return height of
1805 header or mode line if LINE is `header-line' or `mode-line'.
1806 Otherwise, LINE is a text line number starting from 0. A negative number
1807 counts from the end of the window.
1809 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1810 in pixels of the visible part of the line, VPOS and YPOS are the
1811 vertical position in lines and pixels of the line, relative to the top
1812 of the first text line, and OFFBOT is the number of off-window pixels at
1813 the bottom of the text line. If there are off-window pixels at the top
1814 of the (first) text line, YPOS is negative.
1816 Return nil if window display is not up-to-date. In that case, use
1817 `pos-visible-in-window-p' to obtain the information. */)
1818 (Lisp_Object line, Lisp_Object window)
1820 register struct window *w;
1821 register struct buffer *b;
1822 struct glyph_row *row, *end_row;
1823 int max_y, crop, i;
1824 EMACS_INT n;
1826 w = decode_live_window (window);
1828 if (noninteractive || w->pseudo_window_p)
1829 return Qnil;
1831 CHECK_BUFFER (w->contents);
1832 b = XBUFFER (w->contents);
1834 /* Fail if current matrix is not up-to-date. */
1835 if (!w->window_end_valid
1836 || windows_or_buffers_changed
1837 || b->clip_changed
1838 || b->prevent_redisplay_optimizations_p
1839 || window_outdated (w))
1840 return Qnil;
1842 if (NILP (line))
1844 i = w->cursor.vpos;
1845 if (i < 0 || i >= w->current_matrix->nrows
1846 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1847 return Qnil;
1848 max_y = window_text_bottom_y (w);
1849 goto found_row;
1852 if (EQ (line, Qheader_line))
1854 if (!window_wants_header_line (w))
1855 return Qnil;
1856 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1857 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1860 if (EQ (line, Qmode_line))
1862 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1863 return (row->enabled_p ?
1864 list4i (row->height,
1865 0, /* not accurate */
1866 (WINDOW_HEADER_LINE_HEIGHT (w)
1867 + window_text_bottom_y (w)),
1869 : Qnil);
1872 CHECK_NUMBER (line);
1873 n = XINT (line);
1875 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1876 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1877 max_y = window_text_bottom_y (w);
1878 i = 0;
1880 while ((n < 0 || i < n)
1881 && row <= end_row && row->enabled_p
1882 && row->y + row->height < max_y)
1883 row++, i++;
1885 if (row > end_row || !row->enabled_p)
1886 return Qnil;
1888 if (++n < 0)
1890 if (-n > i)
1891 return Qnil;
1892 row += n;
1893 i += n;
1896 found_row:
1897 crop = max (0, (row->y + row->height) - max_y);
1898 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1901 DEFUN ("window-lines-pixel-dimensions", Fwindow_lines_pixel_dimensions, Swindow_lines_pixel_dimensions, 0, 6, 0,
1902 doc: /* Return pixel dimensions of WINDOW's lines.
1903 The return value is a list of the x- and y-coordinates of the lower
1904 right corner of the last character of each line. Return nil if the
1905 current glyph matrix of WINDOW is not up-to-date.
1907 Optional argument WINDOW specifies the window whose lines' dimensions
1908 shall be returned. Nil or omitted means to return the dimensions for
1909 the selected window.
1911 FIRST, if non-nil, specifies the index of the first line whose
1912 dimensions shall be returned. If FIRST is nil and BODY is non-nil,
1913 start with the first text line of WINDOW. Otherwise, start with the
1914 first line of WINDOW.
1916 LAST, if non-nil, specifies the last line whose dimensions shall be
1917 returned. If LAST is nil and BODY is non-nil, the last line is the last
1918 line of the body (text area) of WINDOW. Otherwise, last is the last
1919 line of WINDOW.
1921 INVERSE, if nil, means that the y-pixel value returned for a specific
1922 line specifies the distance in pixels from the left edge (body edge if
1923 BODY is non-nil) of WINDOW to the right edge of the last glyph of that
1924 line. INVERSE non-nil means that the y-pixel value returned for a
1925 specific line specifies the distance in pixels from the right edge of
1926 the last glyph of that line to the right edge (body edge if BODY is
1927 non-nil) of WINDOW.
1929 LEFT non-nil means to return the x- and y-coordinates of the lower left
1930 corner of the leftmost character on each line. This is the value that
1931 should be used for buffers that mostly display text from right to left.
1933 If LEFT is non-nil and INVERSE is nil, this means that the y-pixel value
1934 returned for a specific line specifies the distance in pixels from the
1935 left edge of the last (leftmost) glyph of that line to the right edge
1936 (body edge if BODY is non-nil) of WINDOW. If LEFT and INVERSE are both
1937 non-nil, the y-pixel value returned for a specific line specifies the
1938 distance in pixels from the left edge (body edge if BODY is non-nil) of
1939 WINDOW to the left edge of the last (leftmost) glyph of that line.
1941 Normally, the value of this function is not available while Emacs is
1942 busy, for example, when processing a command. It should be retrievable
1943 though when run from an idle timer with a delay of zero seconds. */)
1944 (Lisp_Object window, Lisp_Object first, Lisp_Object last, Lisp_Object body, Lisp_Object inverse, Lisp_Object left)
1946 struct window *w = decode_live_window (window);
1947 struct buffer *b;
1948 struct glyph_row *row, *end_row;
1949 int max_y = NILP (body) ? WINDOW_PIXEL_HEIGHT (w) : window_text_bottom_y (w);
1950 Lisp_Object rows = Qnil;
1951 int window_width = NILP (body) ? w->pixel_width : window_body_width (w, true);
1952 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
1953 int subtract = NILP (body) ? 0 : header_line_height;
1954 bool invert = !NILP (inverse);
1955 bool left_flag = !NILP (left);
1957 if (noninteractive || w->pseudo_window_p)
1958 return Qnil;
1960 CHECK_BUFFER (w->contents);
1961 b = XBUFFER (w->contents);
1963 /* Fail if current matrix is not up-to-date. */
1964 if (!w->window_end_valid
1965 || windows_or_buffers_changed
1966 || b->clip_changed
1967 || b->prevent_redisplay_optimizations_p
1968 || window_outdated (w))
1969 return Qnil;
1971 if (NILP (first))
1972 row = (NILP (body)
1973 ? MATRIX_ROW (w->current_matrix, 0)
1974 : MATRIX_FIRST_TEXT_ROW (w->current_matrix));
1975 else if (NUMBERP (first))
1977 CHECK_RANGED_INTEGER (first, 0, w->current_matrix->nrows);
1978 row = MATRIX_ROW (w->current_matrix, XINT (first));
1980 else
1981 error ("Invalid specification of first line");
1983 if (NILP (last))
1985 end_row = (NILP (body)
1986 ? MATRIX_ROW (w->current_matrix, w->current_matrix->nrows)
1987 : MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w));
1988 else if (NUMBERP (last))
1990 CHECK_RANGED_INTEGER (last, 0, w->current_matrix->nrows);
1991 end_row = MATRIX_ROW (w->current_matrix, XINT (last));
1993 else
1994 error ("Invalid specification of last line");
1996 while (row <= end_row && row->enabled_p
1997 && row->y + row->height < max_y)
2000 if (left_flag)
2002 struct glyph *glyph = row->glyphs[TEXT_AREA];
2004 rows = Fcons (Fcons (make_number
2005 (invert
2006 ? glyph->pixel_width
2007 : window_width - glyph->pixel_width),
2008 make_number (row->y + row->height - subtract)),
2009 rows);
2011 else
2012 rows = Fcons (Fcons (make_number
2013 (invert
2014 ? window_width - row->pixel_width
2015 : row->pixel_width),
2016 make_number (row->y + row->height - subtract)),
2017 rows);
2018 row++;
2021 return Fnreverse (rows);
2024 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
2025 0, 1, 0,
2026 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
2027 More precisely, return the value assigned by the last call of
2028 `set-window-dedicated-p' for WINDOW. Return nil if that function was
2029 never called with WINDOW as its argument, or the value set by that
2030 function was internally reset since its last call. WINDOW must be a
2031 live window and defaults to the selected one.
2033 When a window is dedicated to its buffer, `display-buffer' will refrain
2034 from displaying another buffer in it. `get-lru-window' and
2035 `get-largest-window' treat dedicated windows specially.
2036 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2037 `kill-buffer' can delete a dedicated window and the containing frame.
2039 Functions like `set-window-buffer' may change the buffer displayed by a
2040 window, unless that window is "strongly" dedicated to its buffer, that
2041 is the value returned by `window-dedicated-p' is t. */)
2042 (Lisp_Object window)
2044 return decode_live_window (window)->dedicated;
2047 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2048 Sset_window_dedicated_p, 2, 2, 0,
2049 doc: /* Mark WINDOW as dedicated according to FLAG.
2050 WINDOW must be a live window and defaults to the selected one. FLAG
2051 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2052 mark WINDOW as non-dedicated. Return FLAG.
2054 When a window is dedicated to its buffer, `display-buffer' will refrain
2055 from displaying another buffer in it. `get-lru-window' and
2056 `get-largest-window' treat dedicated windows specially.
2057 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2058 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2059 and the containing frame.
2061 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2062 its buffer. Functions like `set-window-buffer' may change the buffer
2063 displayed by a window, unless that window is strongly dedicated to its
2064 buffer. If and when `set-window-buffer' displays another buffer in a
2065 window, it also makes sure that the window is no more dedicated. */)
2066 (Lisp_Object window, Lisp_Object flag)
2068 wset_dedicated (decode_live_window (window), flag);
2069 return flag;
2072 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2073 0, 1, 0,
2074 doc: /* Return buffers previously shown in WINDOW.
2075 WINDOW must be a live window and defaults to the selected one.
2077 The return value is a list of elements (BUFFER WINDOW-START POS),
2078 where BUFFER is a buffer, WINDOW-START is the start position of the
2079 window for that buffer, and POS is a window-specific point value. */)
2080 (Lisp_Object window)
2082 return decode_live_window (window)->prev_buffers;
2085 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2086 Sset_window_prev_buffers, 2, 2, 0,
2087 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2088 WINDOW must be a live window and defaults to the selected one.
2090 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2091 where BUFFER is a buffer, WINDOW-START is the start position of the
2092 window for that buffer, and POS is a window-specific point value. */)
2093 (Lisp_Object window, Lisp_Object prev_buffers)
2095 wset_prev_buffers (decode_live_window (window), prev_buffers);
2096 return prev_buffers;
2099 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2100 0, 1, 0,
2101 doc: /* Return list of buffers recently re-shown in WINDOW.
2102 WINDOW must be a live window and defaults to the selected one. */)
2103 (Lisp_Object window)
2105 return decode_live_window (window)->next_buffers;
2108 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2109 Sset_window_next_buffers, 2, 2, 0,
2110 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2111 WINDOW must be a live window and defaults to the selected one.
2112 NEXT-BUFFERS should be a list of buffers. */)
2113 (Lisp_Object window, Lisp_Object next_buffers)
2115 wset_next_buffers (decode_live_window (window), next_buffers);
2116 return next_buffers;
2119 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2120 0, 1, 0,
2121 doc: /* Return the parameters of WINDOW and their values.
2122 WINDOW must be a valid window and defaults to the selected one. The
2123 return value is a list of elements of the form (PARAMETER . VALUE). */)
2124 (Lisp_Object window)
2126 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2129 Lisp_Object
2130 window_parameter (struct window *w, Lisp_Object parameter)
2132 Lisp_Object result = Fassq (parameter, w->window_parameters);
2134 return CDR_SAFE (result);
2138 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2139 2, 2, 0,
2140 doc: /* Return WINDOW's value for PARAMETER.
2141 WINDOW can be any window and defaults to the selected one. */)
2142 (Lisp_Object window, Lisp_Object parameter)
2144 struct window *w = decode_any_window (window);
2146 return window_parameter (w, parameter);
2149 DEFUN ("set-window-parameter", Fset_window_parameter,
2150 Sset_window_parameter, 3, 3, 0,
2151 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2152 WINDOW can be any window and defaults to the selected one.
2153 Return VALUE. */)
2154 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2156 register struct window *w = decode_any_window (window);
2157 Lisp_Object old_alist_elt;
2159 old_alist_elt = Fassq (parameter, w->window_parameters);
2160 if (NILP (old_alist_elt))
2161 wset_window_parameters
2162 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2163 else
2164 Fsetcdr (old_alist_elt, value);
2165 return value;
2168 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2169 0, 1, 0,
2170 doc: /* Return the display-table that WINDOW is using.
2171 WINDOW must be a live window and defaults to the selected one. */)
2172 (Lisp_Object window)
2174 return decode_live_window (window)->display_table;
2177 /* Get the display table for use on window W. This is either W's
2178 display table or W's buffer's display table. Ignore the specified
2179 tables if they are not valid; if no valid table is specified,
2180 return 0. */
2182 struct Lisp_Char_Table *
2183 window_display_table (struct window *w)
2185 struct Lisp_Char_Table *dp = NULL;
2187 if (DISP_TABLE_P (w->display_table))
2188 dp = XCHAR_TABLE (w->display_table);
2189 else if (BUFFERP (w->contents))
2191 struct buffer *b = XBUFFER (w->contents);
2193 if (DISP_TABLE_P (BVAR (b, display_table)))
2194 dp = XCHAR_TABLE (BVAR (b, display_table));
2195 else if (DISP_TABLE_P (Vstandard_display_table))
2196 dp = XCHAR_TABLE (Vstandard_display_table);
2199 return dp;
2202 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2203 doc: /* Set WINDOW's display-table to TABLE.
2204 WINDOW must be a live window and defaults to the selected one. */)
2205 (register Lisp_Object window, Lisp_Object table)
2207 wset_display_table (decode_live_window (window), table);
2208 return table;
2211 /* Record info on buffer window W is displaying
2212 when it is about to cease to display that buffer. */
2213 static void
2214 unshow_buffer (register struct window *w)
2216 Lisp_Object buf = w->contents;
2217 struct buffer *b = XBUFFER (buf);
2219 eassert (b == XMARKER (w->pointm)->buffer);
2221 #if false
2222 if (w == XWINDOW (selected_window)
2223 || ! EQ (buf, XWINDOW (selected_window)->contents))
2224 /* Do this except when the selected window's buffer
2225 is being removed from some other window. */
2226 #endif
2227 /* last_window_start records the start position that this buffer
2228 had in the last window to be disconnected from it.
2229 Now that this statement is unconditional,
2230 it is possible for the buffer to be displayed in the
2231 selected window, while last_window_start reflects another
2232 window which was recently showing the same buffer.
2233 Some people might say that might be a good thing. Let's see. */
2234 b->last_window_start = marker_position (w->start);
2236 /* Point in the selected window's buffer
2237 is actually stored in that buffer, and the window's pointm isn't used.
2238 So don't clobber point in that buffer. */
2239 if (! EQ (buf, XWINDOW (selected_window)->contents)
2240 /* Don't clobber point in current buffer either (this could be
2241 useful in connection with bug#12208).
2242 && XBUFFER (buf) != current_buffer */
2243 /* This line helps to fix Horsley's testbug.el bug. */
2244 && !(WINDOWP (BVAR (b, last_selected_window))
2245 && w != XWINDOW (BVAR (b, last_selected_window))
2246 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2247 temp_set_point_both (b,
2248 clip_to_bounds (BUF_BEGV (b),
2249 marker_position (w->pointm),
2250 BUF_ZV (b)),
2251 clip_to_bounds (BUF_BEGV_BYTE (b),
2252 marker_byte_position (w->pointm),
2253 BUF_ZV_BYTE (b)));
2255 if (WINDOWP (BVAR (b, last_selected_window))
2256 && w == XWINDOW (BVAR (b, last_selected_window)))
2257 bset_last_selected_window (b, Qnil);
2260 /* Put NEW into the window structure in place of OLD. SETFLAG false
2261 means change window structure only. Otherwise store geometry and
2262 other settings as well. */
2263 static void
2264 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2266 Lisp_Object tem;
2267 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2269 /* If OLD is its frame's root window, then NEW is the new
2270 root window for that frame. */
2271 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2272 fset_root_window (XFRAME (o->frame), new);
2274 if (setflag)
2276 n->pixel_left = o->pixel_left;
2277 n->pixel_top = o->pixel_top;
2278 n->pixel_width = o->pixel_width;
2279 n->pixel_height = o->pixel_height;
2280 n->left_col = o->left_col;
2281 n->top_line = o->top_line;
2282 n->total_cols = o->total_cols;
2283 n->total_lines = o->total_lines;
2284 wset_normal_cols (n, o->normal_cols);
2285 wset_normal_cols (o, make_float (1.0));
2286 wset_normal_lines (n, o->normal_lines);
2287 wset_normal_lines (o, make_float (1.0));
2288 n->desired_matrix = n->current_matrix = 0;
2289 n->vscroll = 0;
2290 memset (&n->cursor, 0, sizeof (n->cursor));
2291 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2292 n->last_cursor_vpos = 0;
2293 #ifdef HAVE_WINDOW_SYSTEM
2294 n->phys_cursor_type = NO_CURSOR;
2295 n->phys_cursor_width = -1;
2296 #endif
2297 n->must_be_updated_p = false;
2298 n->pseudo_window_p = false;
2299 n->window_end_vpos = 0;
2300 n->window_end_pos = 0;
2301 n->window_end_valid = false;
2304 tem = o->next;
2305 wset_next (n, tem);
2306 if (!NILP (tem))
2307 wset_prev (XWINDOW (tem), new);
2309 tem = o->prev;
2310 wset_prev (n, tem);
2311 if (!NILP (tem))
2312 wset_next (XWINDOW (tem), new);
2314 tem = o->parent;
2315 wset_parent (n, tem);
2316 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2317 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2320 /* If window WINDOW and its parent window are iso-combined, merge
2321 WINDOW's children into those of its parent window and mark WINDOW as
2322 deleted. */
2324 static void
2325 recombine_windows (Lisp_Object window)
2327 struct window *w, *p, *c;
2328 Lisp_Object parent, child;
2329 bool horflag;
2331 w = XWINDOW (window);
2332 parent = w->parent;
2333 if (!NILP (parent) && NILP (w->combination_limit))
2335 p = XWINDOW (parent);
2336 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2337 && p->horizontal == w->horizontal)
2338 /* WINDOW and PARENT are both either a vertical or a horizontal
2339 combination. */
2341 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2342 child = w->contents;
2343 c = XWINDOW (child);
2345 /* Splice WINDOW's children into its parent's children and
2346 assign new normal sizes. */
2347 if (NILP (w->prev))
2348 wset_combination (p, horflag, child);
2349 else
2351 wset_prev (c, w->prev);
2352 wset_next (XWINDOW (w->prev), child);
2355 while (c)
2357 wset_parent (c, parent);
2359 if (horflag)
2360 wset_normal_cols
2361 (c, make_float ((double) c->pixel_width
2362 / (double) p->pixel_width));
2363 else
2364 wset_normal_lines
2365 (c, make_float ((double) c->pixel_height
2366 / (double) p->pixel_height));
2368 if (NILP (c->next))
2370 if (!NILP (w->next))
2372 wset_next (c, w->next);
2373 wset_prev (XWINDOW (c->next), child);
2376 c = 0;
2378 else
2380 child = c->next;
2381 c = XWINDOW (child);
2385 /* WINDOW can be deleted now. */
2386 wset_combination (w, false, Qnil);
2391 /* If WINDOW can be deleted, delete it. */
2392 static void
2393 delete_deletable_window (Lisp_Object window)
2395 if (!NILP (call1 (Qwindow_deletable_p, window)))
2396 call1 (Qdelete_window, window);
2399 /***********************************************************************
2400 Window List
2401 ***********************************************************************/
2403 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2404 pointer. This is a callback function for foreach_window, used in
2405 the window_list function. */
2407 static bool
2408 add_window_to_list (struct window *w, void *user_data)
2410 Lisp_Object *list = user_data;
2411 Lisp_Object window;
2412 XSETWINDOW (window, w);
2413 *list = Fcons (window, *list);
2414 return true;
2418 /* Return a list of all windows, for use by next_window. If
2419 Vwindow_list is a list, return that list. Otherwise, build a new
2420 list, cache it in Vwindow_list, and return that. */
2422 Lisp_Object
2423 window_list (void)
2425 if (!CONSP (Vwindow_list))
2427 Lisp_Object tail, frame;
2429 Vwindow_list = Qnil;
2430 FOR_EACH_FRAME (tail, frame)
2432 Lisp_Object arglist = Qnil;
2434 /* We are visiting windows in canonical order, and add
2435 new windows at the front of args[1], which means we
2436 have to reverse this list at the end. */
2437 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2438 arglist = Fnreverse (arglist);
2439 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2443 return Vwindow_list;
2447 /* Value is true if WINDOW satisfies the constraints given by
2448 OWINDOW, MINIBUF and ALL_FRAMES.
2450 MINIBUF t means WINDOW may be minibuffer windows.
2451 `lambda' means WINDOW may not be a minibuffer window.
2452 a window means a specific minibuffer window
2454 ALL_FRAMES t means search all frames,
2455 nil means search just current frame,
2456 `visible' means search just visible frames on the
2457 current terminal,
2458 0 means search visible and iconified frames on the
2459 current terminal,
2460 a window means search the frame that window belongs to,
2461 a frame means consider windows on that frame, only. */
2463 static bool
2464 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2465 Lisp_Object minibuf, Lisp_Object all_frames)
2467 struct window *w = XWINDOW (window);
2468 struct frame *f = XFRAME (w->frame);
2469 bool candidate_p = true;
2471 if (!BUFFERP (w->contents))
2472 candidate_p = false;
2473 else if (MINI_WINDOW_P (w)
2474 && (EQ (minibuf, Qlambda)
2475 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2477 /* If MINIBUF is `lambda' don't consider any mini-windows.
2478 If it is a window, consider only that one. */
2479 candidate_p = false;
2481 else if (EQ (all_frames, Qt))
2482 candidate_p = true;
2483 else if (NILP (all_frames))
2485 eassert (WINDOWP (owindow));
2486 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2488 else if (EQ (all_frames, Qvisible))
2490 candidate_p = FRAME_VISIBLE_P (f)
2491 && (FRAME_TERMINAL (XFRAME (w->frame))
2492 == FRAME_TERMINAL (XFRAME (selected_frame)));
2495 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2497 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2498 #ifdef HAVE_X_WINDOWS
2499 /* Yuck!! If we've just created the frame and the
2500 window-manager requested the user to place it
2501 manually, the window may still not be considered
2502 `visible'. I'd argue it should be at least
2503 something like `iconified', but don't know how to do
2504 that yet. --Stef */
2505 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2506 && !f->output_data.x->has_been_visible)
2507 #endif
2509 && (FRAME_TERMINAL (XFRAME (w->frame))
2510 == FRAME_TERMINAL (XFRAME (selected_frame)));
2512 else if (WINDOWP (all_frames))
2513 /* To qualify as candidate, it's not sufficient for WINDOW's frame
2514 to just share the minibuffer window - it must be active as well
2515 (see Bug#24500). */
2516 candidate_p = (EQ (XWINDOW (all_frames)->frame, w->frame)
2517 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2518 else if (FRAMEP (all_frames))
2519 candidate_p = EQ (all_frames, w->frame);
2521 return candidate_p;
2525 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2526 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2527 MINIBUF, and ALL_FRAMES. */
2529 static void
2530 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2532 struct window *w = decode_live_window (*window);
2534 XSETWINDOW (*window, w);
2535 /* MINIBUF nil may or may not include minibuffers. Decide if it
2536 does. */
2537 if (NILP (*minibuf))
2538 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2539 else if (!EQ (*minibuf, Qt))
2540 *minibuf = Qlambda;
2542 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2543 => count none of them, or a specific minibuffer window (the
2544 active one) to count. */
2546 /* ALL_FRAMES nil doesn't specify which frames to include. */
2547 if (NILP (*all_frames))
2548 *all_frames
2549 = (!EQ (*minibuf, Qlambda)
2550 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2551 : Qnil);
2552 else if (EQ (*all_frames, Qvisible))
2554 else if (EQ (*all_frames, make_number (0)))
2556 else if (FRAMEP (*all_frames))
2558 else if (!EQ (*all_frames, Qt))
2559 *all_frames = Qnil;
2563 /* Return the next or previous window of WINDOW in cyclic ordering
2564 of windows. NEXT_P means return the next window. See the
2565 documentation string of next-window for the meaning of MINIBUF and
2566 ALL_FRAMES. */
2568 static Lisp_Object
2569 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2570 bool next_p)
2572 decode_next_window_args (&window, &minibuf, &all_frames);
2574 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2575 return the first window on the frame. */
2576 if (FRAMEP (all_frames)
2577 && !EQ (all_frames, XWINDOW (window)->frame))
2578 return Fframe_first_window (all_frames);
2580 if (next_p)
2582 Lisp_Object list;
2584 /* Find WINDOW in the list of all windows. */
2585 list = Fmemq (window, window_list ());
2587 /* Scan forward from WINDOW to the end of the window list. */
2588 if (CONSP (list))
2589 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2590 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2591 break;
2593 /* Scan from the start of the window list up to WINDOW. */
2594 if (!CONSP (list))
2595 for (list = Vwindow_list;
2596 CONSP (list) && !EQ (XCAR (list), window);
2597 list = XCDR (list))
2598 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2599 break;
2601 if (CONSP (list))
2602 window = XCAR (list);
2604 else
2606 Lisp_Object candidate, list;
2608 /* Scan through the list of windows for candidates. If there are
2609 candidate windows in front of WINDOW, the last one of these
2610 is the one we want. If there are candidates following WINDOW
2611 in the list, again the last one of these is the one we want. */
2612 candidate = Qnil;
2613 for (list = window_list (); CONSP (list); list = XCDR (list))
2615 if (EQ (XCAR (list), window))
2617 if (WINDOWP (candidate))
2618 break;
2620 else if (candidate_window_p (XCAR (list), window, minibuf,
2621 all_frames))
2622 candidate = XCAR (list);
2625 if (WINDOWP (candidate))
2626 window = candidate;
2629 return window;
2633 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2634 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2635 WINDOW must be a live window and defaults to the selected one. The
2636 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2637 consider.
2639 MINIBUF nil or omitted means consider the minibuffer window only if the
2640 minibuffer is active. MINIBUF t means consider the minibuffer window
2641 even if the minibuffer is not active. Any other value means do not
2642 consider the minibuffer window even if the minibuffer is active.
2644 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2645 plus the minibuffer window if specified by the MINIBUF argument. If the
2646 minibuffer counts, consider all windows on all frames that share that
2647 minibuffer too. The following non-nil values of ALL-FRAMES have special
2648 meanings:
2650 - t means consider all windows on all existing frames.
2652 - `visible' means consider all windows on all visible frames.
2654 - 0 (the number zero) means consider all windows on all visible and
2655 iconified frames.
2657 - A frame means consider all windows on that frame only.
2659 Anything else means consider all windows on WINDOW's frame and no
2660 others.
2662 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2663 `next-window' to iterate through the entire cycle of acceptable
2664 windows, eventually ending up back at the window you started with.
2665 `previous-window' traverses the same cycle, in the reverse order. */)
2666 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2668 return next_window (window, minibuf, all_frames, true);
2672 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2673 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2674 WINDOW must be a live window and defaults to the selected one. The
2675 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2676 consider.
2678 MINIBUF nil or omitted means consider the minibuffer window only if the
2679 minibuffer is active. MINIBUF t means consider the minibuffer window
2680 even if the minibuffer is not active. Any other value means do not
2681 consider the minibuffer window even if the minibuffer is active.
2683 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2684 plus the minibuffer window if specified by the MINIBUF argument. If the
2685 minibuffer counts, consider all windows on all frames that share that
2686 minibuffer too. The following non-nil values of ALL-FRAMES have special
2687 meanings:
2689 - t means consider all windows on all existing frames.
2691 - `visible' means consider all windows on all visible frames.
2693 - 0 (the number zero) means consider all windows on all visible and
2694 iconified frames.
2696 - A frame means consider all windows on that frame only.
2698 Anything else means consider all windows on WINDOW's frame and no
2699 others.
2701 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2702 use `previous-window' to iterate through the entire cycle of
2703 acceptable windows, eventually ending up back at the window you
2704 started with. `next-window' traverses the same cycle, in the
2705 reverse order. */)
2706 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2708 return next_window (window, minibuf, all_frames, false);
2712 /* Return a list of windows in cyclic ordering. Arguments are like
2713 for `next-window'. */
2715 static Lisp_Object
2716 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2718 Lisp_Object tail, list, rest;
2720 decode_next_window_args (&window, &minibuf, &all_frames);
2721 list = Qnil;
2723 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2724 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2725 list = Fcons (XCAR (tail), list);
2727 /* Rotate the list to start with WINDOW. */
2728 list = Fnreverse (list);
2729 rest = Fmemq (window, list);
2730 if (!NILP (rest) && !EQ (rest, list))
2732 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2734 XSETCDR (tail, Qnil);
2735 list = nconc2 (rest, list);
2737 return list;
2741 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2742 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2743 FRAME nil or omitted means use the selected frame.
2744 WINDOW nil or omitted means use the window selected within FRAME.
2745 MINIBUF t means include the minibuffer window, even if it isn't active.
2746 MINIBUF nil or omitted means include the minibuffer window only
2747 if it's active.
2748 MINIBUF neither nil nor t means never include the minibuffer window. */)
2749 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2751 if (NILP (window))
2752 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2753 CHECK_WINDOW (window);
2754 if (NILP (frame))
2755 frame = selected_frame;
2757 if (!EQ (frame, XWINDOW (window)->frame))
2758 error ("Window is on a different frame");
2760 return window_list_1 (window, minibuf, frame);
2764 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2765 doc: /* Return a list of all live windows.
2766 WINDOW specifies the first window to list and defaults to the selected
2767 window.
2769 Optional argument MINIBUF nil or omitted means consider the minibuffer
2770 window only if the minibuffer is active. MINIBUF t means consider the
2771 minibuffer window even if the minibuffer is not active. Any other value
2772 means do not consider the minibuffer window even if the minibuffer is
2773 active.
2775 Optional argument ALL-FRAMES nil or omitted means consider all windows
2776 on WINDOW's frame, plus the minibuffer window if specified by the
2777 MINIBUF argument. If the minibuffer counts, consider all windows on all
2778 frames that share that minibuffer too. The following non-nil values of
2779 ALL-FRAMES have special meanings:
2781 - t means consider all windows on all existing frames.
2783 - `visible' means consider all windows on all visible frames.
2785 - 0 (the number zero) means consider all windows on all visible and
2786 iconified frames.
2788 - A frame means consider all windows on that frame only.
2790 Anything else means consider all windows on WINDOW's frame and no
2791 others.
2793 If WINDOW is not on the list of windows returned, some other window will
2794 be listed first but no error is signaled. */)
2795 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2797 return window_list_1 (window, minibuf, all_frames);
2800 /* Look at all windows, performing an operation specified by TYPE
2801 with argument OBJ.
2802 If FRAMES is Qt, look at all frames;
2803 Qnil, look at just the selected frame;
2804 Qvisible, look at visible frames;
2805 a frame, just look at windows on that frame.
2806 If MINI, perform the operation on minibuffer windows too. */
2808 enum window_loop
2810 WINDOW_LOOP_UNUSED,
2811 GET_BUFFER_WINDOW, /* Arg is buffer */
2812 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2813 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2814 CHECK_ALL_WINDOWS /* Arg is ignored */
2817 static Lisp_Object
2818 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2819 Lisp_Object frames)
2821 Lisp_Object window, windows, best_window, frame_arg;
2822 bool frame_best_window_flag = false;
2823 struct frame *f;
2825 /* If we're only looping through windows on a particular frame,
2826 frame points to that frame. If we're looping through windows
2827 on all frames, frame is 0. */
2828 if (FRAMEP (frames))
2829 f = XFRAME (frames);
2830 else if (NILP (frames))
2831 f = SELECTED_FRAME ();
2832 else
2833 f = NULL;
2835 if (f)
2836 frame_arg = Qlambda;
2837 else if (EQ (frames, make_number (0)))
2838 frame_arg = frames;
2839 else if (EQ (frames, Qvisible))
2840 frame_arg = frames;
2841 else
2842 frame_arg = Qt;
2844 /* frame_arg is Qlambda to stick to one frame,
2845 Qvisible to consider all visible frames,
2846 or Qt otherwise. */
2848 /* Pick a window to start with. */
2849 if (WINDOWP (obj))
2850 window = obj;
2851 else if (f)
2852 window = FRAME_SELECTED_WINDOW (f);
2853 else
2854 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2856 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2857 best_window = Qnil;
2859 for (; CONSP (windows); windows = XCDR (windows))
2861 struct window *w;
2863 window = XCAR (windows);
2864 w = XWINDOW (window);
2866 /* Note that we do not pay attention here to whether the frame
2867 is visible, since Fwindow_list skips non-visible frames if
2868 that is desired, under the control of frame_arg. */
2869 if (!MINI_WINDOW_P (w)
2870 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2871 consider all windows. */
2872 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2873 || (mini && minibuf_level > 0))
2874 switch (type)
2876 case GET_BUFFER_WINDOW:
2877 if (EQ (w->contents, obj)
2878 /* Don't find any minibuffer window except the one that
2879 is currently in use. */
2880 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2882 if (EQ (window, selected_window))
2883 /* Preferably return the selected window. */
2884 return window;
2885 else if (EQ (XWINDOW (window)->frame, selected_frame)
2886 && !frame_best_window_flag)
2887 /* Prefer windows on the current frame (but don't
2888 choose another one if we have one already). */
2890 best_window = window;
2891 frame_best_window_flag = true;
2893 else if (NILP (best_window))
2894 best_window = window;
2896 break;
2898 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2899 /* We could simply check whether the buffer shown by window
2900 is live, and show another buffer in case it isn't. */
2901 if (EQ (w->contents, obj))
2903 /* Undedicate WINDOW. */
2904 wset_dedicated (w, Qnil);
2905 /* Make WINDOW show the buffer returned by
2906 other_buffer_safely, don't run any hooks. */
2907 set_window_buffer
2908 (window, other_buffer_safely (w->contents), false, false);
2909 /* If WINDOW is the selected window, make its buffer
2910 current. But do so only if the window shows the
2911 current buffer (Bug#6454). */
2912 if (EQ (window, selected_window)
2913 && XBUFFER (w->contents) == current_buffer)
2914 Fset_buffer (w->contents);
2916 break;
2918 case REDISPLAY_BUFFER_WINDOWS:
2919 if (EQ (w->contents, obj))
2921 mark_window_display_accurate (window, false);
2922 w->update_mode_line = true;
2923 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2924 update_mode_lines = 27;
2925 best_window = window;
2927 break;
2929 /* Check for a leaf window that has a killed buffer
2930 or broken markers. */
2931 case CHECK_ALL_WINDOWS:
2932 if (BUFFERP (w->contents))
2934 struct buffer *b = XBUFFER (w->contents);
2936 if (!BUFFER_LIVE_P (b))
2937 emacs_abort ();
2938 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2939 emacs_abort ();
2940 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2941 emacs_abort ();
2943 break;
2945 case WINDOW_LOOP_UNUSED:
2946 break;
2950 return best_window;
2953 /* Used for debugging. Abort if any window has a dead buffer. */
2955 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2956 void
2957 check_all_windows (void)
2959 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2962 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2963 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2964 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2965 the current buffer.
2967 The optional argument ALL-FRAMES specifies the frames to consider:
2969 - t means consider all windows on all existing frames.
2971 - `visible' means consider all windows on all visible frames.
2973 - 0 (the number zero) means consider all windows on all visible
2974 and iconified frames.
2976 - A frame means consider all windows on that frame only.
2978 Any other value of ALL-FRAMES means consider all windows on the
2979 selected frame and no others. */)
2980 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2982 Lisp_Object buffer;
2984 if (NILP (buffer_or_name))
2985 buffer = Fcurrent_buffer ();
2986 else
2987 buffer = Fget_buffer (buffer_or_name);
2989 if (BUFFERP (buffer))
2990 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2991 else
2992 return Qnil;
2996 static Lisp_Object
2997 resize_root_window (Lisp_Object window, Lisp_Object delta,
2998 Lisp_Object horizontal, Lisp_Object ignore,
2999 Lisp_Object pixelwise)
3001 return call5 (Qwindow__resize_root_window, window, delta,
3002 horizontal, ignore, pixelwise);
3005 void
3006 sanitize_window_sizes (Lisp_Object horizontal)
3008 /* Don't burp in temacs -nw before window.el is loaded. */
3009 if (!NILP (Fsymbol_function (Qwindow__sanitize_window_sizes)))
3010 call1 (Qwindow__sanitize_window_sizes, horizontal);
3014 static Lisp_Object
3015 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
3017 return call2 (Qwindow__pixel_to_total, frame, horizontal);
3021 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
3022 Sdelete_other_windows_internal, 0, 2, "",
3023 doc: /* Make WINDOW fill its frame.
3024 Only the frame WINDOW is on is affected. WINDOW must be a valid window
3025 and defaults to the selected one.
3027 Optional argument ROOT, if non-nil, must specify an internal window such
3028 that WINDOW is in its window subtree. If this is the case, replace ROOT
3029 by WINDOW and leave alone any windows not part of ROOT's subtree.
3031 When WINDOW is live try to reduce display jumps by keeping the text
3032 previously visible in WINDOW in the same place on the frame. Doing this
3033 depends on the value of (window-start WINDOW), so if calling this
3034 function in a program gives strange scrolling, make sure the
3035 window-start value is reasonable when this function is called. */)
3036 (Lisp_Object window, Lisp_Object root)
3038 struct window *w, *r, *s;
3039 struct frame *f;
3040 Lisp_Object sibling, pwindow, delta;
3041 Lisp_Object swindow UNINIT;
3042 ptrdiff_t startpos UNINIT, startbyte UNINIT;
3043 int top UNINIT;
3044 int new_top;
3045 bool resize_failed = false;
3047 w = decode_valid_window (window);
3048 XSETWINDOW (window, w);
3049 f = XFRAME (w->frame);
3051 if (NILP (root))
3052 /* ROOT is the frame's root window. */
3054 root = FRAME_ROOT_WINDOW (f);
3055 r = XWINDOW (root);
3057 else
3058 /* ROOT must be an ancestor of WINDOW. */
3060 r = decode_valid_window (root);
3061 pwindow = XWINDOW (window)->parent;
3062 while (!NILP (pwindow))
3063 if (EQ (pwindow, root))
3064 break;
3065 else
3066 pwindow = XWINDOW (pwindow)->parent;
3067 if (!EQ (pwindow, root))
3068 error ("Specified root is not an ancestor of specified window");
3071 if (EQ (window, root))
3072 /* A noop. */
3073 return Qnil;
3074 /* I don't understand the "top > 0" part below. If we deal with a
3075 standalone minibuffer it would have been caught by the preceding
3076 test. */
3077 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3078 error ("Can't expand minibuffer to full frame");
3080 if (BUFFERP (w->contents))
3082 startpos = marker_position (w->start);
3083 startbyte = marker_byte_position (w->start);
3084 top = (WINDOW_TOP_EDGE_LINE (w)
3085 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3086 /* Make sure WINDOW is the frame's selected window. */
3087 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3089 if (EQ (selected_frame, w->frame))
3090 Fselect_window (window, Qnil);
3091 else
3092 fset_selected_window (f, window);
3095 else
3097 /* See if the frame's selected window is a part of the window
3098 subtree rooted at WINDOW, by finding all the selected window's
3099 parents and comparing each one with WINDOW. If it isn't we
3100 need a new selected window for this frame. */
3101 swindow = FRAME_SELECTED_WINDOW (f);
3102 while (true)
3104 pwindow = swindow;
3105 while (!NILP (pwindow) && !EQ (window, pwindow))
3106 pwindow = XWINDOW (pwindow)->parent;
3108 if (EQ (window, pwindow))
3109 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3110 as the new selected window. */
3111 break;
3112 else
3113 /* Else try the previous window of SWINDOW. */
3114 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3117 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3119 if (EQ (selected_frame, w->frame))
3120 Fselect_window (swindow, Qnil);
3121 else
3122 fset_selected_window (f, swindow);
3126 block_input ();
3127 if (!FRAME_INITIAL_P (f))
3129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3131 /* We are going to free the glyph matrices of WINDOW, and with
3132 that we might lose any information about glyph rows that have
3133 some of their glyphs highlighted in mouse face. (These rows
3134 are marked with a mouse_face_p flag.) If WINDOW
3135 indeed has some glyphs highlighted in mouse face, signal to
3136 frame's up-to-date hook that mouse highlight was overwritten,
3137 so that it will arrange for redisplaying the highlight. */
3138 if (EQ (hlinfo->mouse_face_window, window))
3139 reset_mouse_highlight (hlinfo);
3141 free_window_matrices (r);
3143 fset_redisplay (f);
3144 Vwindow_list = Qnil;
3146 if (!WINDOW_LEAF_P (w))
3148 /* Resize child windows vertically. */
3149 XSETINT (delta, r->pixel_height - w->pixel_height);
3150 w->pixel_top = r->pixel_top;
3151 w->top_line = r->top_line;
3152 resize_root_window (window, delta, Qnil, Qnil, Qt);
3153 if (window_resize_check (w, false))
3155 window_resize_apply (w, false);
3156 window_pixel_to_total (w->frame, Qnil);
3158 else
3160 resize_root_window (window, delta, Qnil, Qt, Qt);
3161 if (window_resize_check (w, false))
3163 window_resize_apply (w, false);
3164 window_pixel_to_total (w->frame, Qnil);
3166 else
3167 resize_failed = true;
3170 /* Resize child windows horizontally. */
3171 if (!resize_failed)
3173 w->left_col = r->left_col;
3174 w->pixel_left = r->pixel_left;
3175 XSETINT (delta, r->pixel_width - w->pixel_width);
3176 resize_root_window (window, delta, Qt, Qnil, Qt);
3177 if (window_resize_check (w, true))
3179 window_resize_apply (w, true);
3180 window_pixel_to_total (w->frame, Qt);
3182 else
3184 resize_root_window (window, delta, Qt, Qt, Qt);
3185 if (window_resize_check (w, true))
3187 window_resize_apply (w, true);
3188 window_pixel_to_total (w->frame, Qt);
3190 else
3191 resize_failed = true;
3195 if (resize_failed)
3196 /* Play safe, if we still can ... */
3198 window = swindow;
3199 w = XWINDOW (window);
3203 /* Cleanly unlink WINDOW from window-tree. */
3204 if (!NILP (w->prev))
3205 /* Get SIBLING above (on the left of) WINDOW. */
3207 sibling = w->prev;
3208 s = XWINDOW (sibling);
3209 wset_next (s, w->next);
3210 if (!NILP (s->next))
3211 wset_prev (XWINDOW (s->next), sibling);
3213 else
3214 /* Get SIBLING below (on the right of) WINDOW. */
3216 sibling = w->next;
3217 s = XWINDOW (sibling);
3218 wset_prev (s, Qnil);
3219 wset_combination (XWINDOW (w->parent),
3220 XWINDOW (w->parent)->horizontal, sibling);
3223 /* Delete ROOT and all child windows of ROOT. */
3224 if (WINDOWP (r->contents))
3226 delete_all_child_windows (r->contents);
3227 wset_combination (r, false, Qnil);
3230 replace_window (root, window, true);
3232 /* This must become SWINDOW anyway ....... */
3233 if (BUFFERP (w->contents) && !resize_failed)
3235 /* Try to minimize scrolling, by setting the window start to the
3236 point will cause the text at the old window start to be at the
3237 same place on the frame. But don't try to do this if the
3238 window start is outside the visible portion (as might happen
3239 when the display is not current, due to typeahead). */
3240 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3241 if (new_top != top
3242 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3243 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3245 struct position pos;
3246 struct buffer *obuf = current_buffer;
3248 Fset_buffer (w->contents);
3249 /* This computation used to temporarily move point, but that
3250 can have unwanted side effects due to text properties. */
3251 pos = *vmotion (startpos, startbyte, -top, w);
3253 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3254 w->window_end_valid = false;
3255 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3256 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3257 /* We need to do this, so that the window-scroll-functions
3258 get called. */
3259 w->optional_new_start = true;
3261 set_buffer_internal (obuf);
3265 adjust_frame_glyphs (f);
3266 unblock_input ();
3268 run_window_configuration_change_hook (f);
3270 return Qnil;
3274 void
3275 replace_buffer_in_windows (Lisp_Object buffer)
3277 call1 (Qreplace_buffer_in_windows, buffer);
3280 /* If BUFFER is shown in a window, safely replace it with some other
3281 buffer in all windows of all frames, even those on other keyboards. */
3283 void
3284 replace_buffer_in_windows_safely (Lisp_Object buffer)
3286 if (buffer_window_count (XBUFFER (buffer)))
3288 Lisp_Object tail, frame;
3290 /* A single call to window_loop won't do the job because it only
3291 considers frames on the current keyboard. So loop manually over
3292 frames, and handle each one. */
3293 FOR_EACH_FRAME (tail, frame)
3294 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3298 /* The following three routines are needed for running a window's
3299 configuration change hook. */
3300 static void
3301 run_funs (Lisp_Object funs)
3303 for (; CONSP (funs); funs = XCDR (funs))
3304 if (!EQ (XCAR (funs), Qt))
3305 call0 (XCAR (funs));
3308 static void
3309 select_window_norecord (Lisp_Object window)
3311 if (WINDOW_LIVE_P (window))
3312 Fselect_window (window, Qt);
3315 static void
3316 select_frame_norecord (Lisp_Object frame)
3318 if (FRAME_LIVE_P (XFRAME (frame)))
3319 Fselect_frame (frame, Qt);
3322 static void
3323 run_window_configuration_change_hook (struct frame *f)
3325 ptrdiff_t count = SPECPDL_INDEX ();
3326 Lisp_Object frame, global_wcch
3327 = Fdefault_value (Qwindow_configuration_change_hook);
3328 XSETFRAME (frame, f);
3330 if (NILP (Vrun_hooks)
3331 || !(f->can_x_set_window_size)
3332 || !(f->after_make_frame))
3333 return;
3335 /* Use the right buffer. Matters when running the local hooks. */
3336 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3338 record_unwind_current_buffer ();
3339 Fset_buffer (Fwindow_buffer (Qnil));
3342 if (SELECTED_FRAME () != f)
3344 record_unwind_protect (select_frame_norecord, selected_frame);
3345 select_frame_norecord (frame);
3348 /* Look for buffer-local values. */
3350 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3351 for (; CONSP (windows); windows = XCDR (windows))
3353 Lisp_Object window = XCAR (windows);
3354 Lisp_Object buffer = Fwindow_buffer (window);
3355 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3356 buffer)))
3358 ptrdiff_t inner_count = SPECPDL_INDEX ();
3359 record_unwind_protect (select_window_norecord, selected_window);
3360 select_window_norecord (window);
3361 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3362 buffer));
3363 unbind_to (inner_count, Qnil);
3368 run_funs (global_wcch);
3369 unbind_to (count, Qnil);
3372 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3373 Srun_window_configuration_change_hook, 0, 1, 0,
3374 doc: /* Run `window-configuration-change-hook' for FRAME.
3375 If FRAME is omitted or nil, it defaults to the selected frame. */)
3376 (Lisp_Object frame)
3378 run_window_configuration_change_hook (decode_live_frame (frame));
3379 return Qnil;
3382 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3383 Srun_window_scroll_functions, 0, 1, 0,
3384 doc: /* Run `window-scroll-functions' for WINDOW.
3385 If WINDOW is omitted or nil, it defaults to the selected window. */)
3386 (Lisp_Object window)
3388 if (! NILP (Vwindow_scroll_functions))
3389 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3390 Fmarker_position (decode_live_window (window)->start));
3391 return Qnil;
3395 /* Compare old and present pixel sizes of windows in tree rooted at W.
3396 Return true iff any of these windows differs in size. */
3398 static bool
3399 window_size_changed (struct window *w)
3401 if (w->pixel_width != w->pixel_width_before_size_change
3402 || w->pixel_height != w->pixel_height_before_size_change)
3403 return true;
3405 if (WINDOW_INTERNAL_P (w))
3407 w = XWINDOW (w->contents);
3408 while (w)
3410 if (window_size_changed (w))
3411 return true;
3413 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3417 return false;
3420 /* Set before size change pixel sizes of windows in tree rooted at W to
3421 their present pixel sizes. */
3423 static void
3424 window_set_before_size_change_sizes (struct window *w)
3426 w->pixel_width_before_size_change = w->pixel_width;
3427 w->pixel_height_before_size_change = w->pixel_height;
3429 if (WINDOW_INTERNAL_P (w))
3431 w = XWINDOW (w->contents);
3432 while (w)
3434 window_set_before_size_change_sizes (w);
3435 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3441 void
3442 run_window_size_change_functions (Lisp_Object frame)
3444 struct frame *f = XFRAME (frame);
3445 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3446 Lisp_Object functions = Vwindow_size_change_functions;
3448 if (FRAME_WINDOW_CONFIGURATION_CHANGED (f)
3449 /* Here we implicitly exclude the possibility that the height of
3450 FRAME and its minibuffer window both change leaving the height
3451 of FRAME's root window alone. */
3452 || window_size_changed (r))
3454 while (CONSP (functions))
3456 if (!EQ (XCAR (functions), Qt))
3457 safe_call1 (XCAR (functions), frame);
3458 functions = XCDR (functions);
3461 window_set_before_size_change_sizes (r);
3463 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3464 /* Record size of FRAME's minibuffer window too. */
3465 window_set_before_size_change_sizes
3466 (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
3468 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = false;
3473 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3474 to run hooks. See make_frame for a case where it's not allowed.
3475 KEEP_MARGINS_P means that the current margins, fringes, and
3476 scroll bar settings of the window are not reset from the buffer's
3477 local settings. */
3479 void
3480 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3481 bool run_hooks_p, bool keep_margins_p)
3483 struct window *w = XWINDOW (window);
3484 struct buffer *b = XBUFFER (buffer);
3485 ptrdiff_t count = SPECPDL_INDEX ();
3486 bool samebuf = EQ (buffer, w->contents);
3488 wset_buffer (w, buffer);
3490 if (EQ (window, selected_window))
3491 bset_last_selected_window (b, window);
3493 /* Let redisplay errors through. */
3494 b->display_error_modiff = 0;
3496 /* Update time stamps of buffer display. */
3497 if (INTEGERP (BVAR (b, display_count)))
3498 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3499 bset_display_time (b, Fcurrent_time ());
3501 w->window_end_pos = 0;
3502 w->window_end_vpos = 0;
3503 w->last_cursor_vpos = 0;
3505 if (!(keep_margins_p && samebuf))
3506 { /* If we're not actually changing the buffer, don't reset hscroll
3507 and vscroll. Resetting hscroll and vscroll here is problematic
3508 for things like image-mode and doc-view-mode since it resets
3509 the image's position whenever we resize the frame. */
3510 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3511 w->suspend_auto_hscroll = false;
3512 w->vscroll = 0;
3513 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3514 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3515 set_marker_restricted (w->start,
3516 make_number (b->last_window_start),
3517 buffer);
3518 w->start_at_line_beg = false;
3519 w->force_start = false;
3522 wset_redisplay (w);
3523 wset_update_mode_line (w);
3525 /* We must select BUFFER to run the window-scroll-functions and to look up
3526 the buffer-local value of Vwindow_point_insertion_type. */
3527 record_unwind_current_buffer ();
3528 Fset_buffer (buffer);
3530 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3531 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3533 if (!keep_margins_p)
3535 /* Set left and right marginal area width etc. from buffer. */
3536 set_window_fringes (w, BVAR (b, left_fringe_width),
3537 BVAR (b, right_fringe_width),
3538 BVAR (b, fringes_outside_margins));
3539 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3540 BVAR (b, vertical_scroll_bar_type),
3541 BVAR (b, scroll_bar_height),
3542 BVAR (b, horizontal_scroll_bar_type));
3543 set_window_margins (w, BVAR (b, left_margin_cols),
3544 BVAR (b, right_margin_cols));
3545 apply_window_adjustment (w);
3548 if (run_hooks_p)
3550 if (!NILP (Vwindow_scroll_functions))
3551 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3552 Fmarker_position (w->start));
3553 if (!samebuf)
3554 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3557 unbind_to (count, Qnil);
3560 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3561 doc: /* Make WINDOW display BUFFER-OR-NAME.
3562 WINDOW must be a live window and defaults to the selected one.
3563 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3565 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3566 display margins, fringe widths, and scroll bar settings are preserved;
3567 the default is to reset these from the local settings for BUFFER-OR-NAME
3568 or the frame defaults. Return nil.
3570 This function throws an error when WINDOW is strongly dedicated to its
3571 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3572 already display BUFFER-OR-NAME.
3574 This function runs `window-scroll-functions' before running
3575 `window-configuration-change-hook'. */)
3576 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3578 register Lisp_Object tem, buffer;
3579 register struct window *w = decode_live_window (window);
3581 XSETWINDOW (window, w);
3582 buffer = Fget_buffer (buffer_or_name);
3583 CHECK_BUFFER (buffer);
3584 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3585 error ("Attempt to display deleted buffer");
3587 tem = w->contents;
3588 if (NILP (tem))
3589 error ("Window is deleted");
3590 else
3592 if (!EQ (tem, buffer))
3594 if (EQ (w->dedicated, Qt))
3595 /* WINDOW is strongly dedicated to its buffer, signal an
3596 error. */
3597 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3598 else
3599 /* WINDOW is weakly dedicated to its buffer, reset
3600 dedication. */
3601 wset_dedicated (w, Qnil);
3603 call1 (Qrecord_window_buffer, window);
3606 unshow_buffer (w);
3609 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3611 return Qnil;
3614 static Lisp_Object
3615 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3617 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3620 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3621 0, 1, 0,
3622 doc: /* Force all windows to be updated on next redisplay.
3623 If optional arg OBJECT is a window, force redisplay of that window only.
3624 If OBJECT is a buffer or buffer name, force redisplay of all windows
3625 displaying that buffer. */)
3626 (Lisp_Object object)
3628 if (NILP (object))
3630 windows_or_buffers_changed = 29;
3631 update_mode_lines = 28;
3632 return Qt;
3635 if (WINDOWP (object))
3637 struct window *w = XWINDOW (object);
3638 mark_window_display_accurate (object, false);
3639 w->update_mode_line = true;
3640 if (BUFFERP (w->contents))
3641 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3642 update_mode_lines = 29;
3643 return Qt;
3646 if (STRINGP (object))
3647 object = Fget_buffer (object);
3648 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3649 && buffer_window_count (XBUFFER (object)))
3651 /* If buffer is live and shown in at least one window, find
3652 all windows showing this buffer and force update of them. */
3653 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3654 return NILP (object) ? Qnil : Qt;
3657 /* If nothing suitable was found, just return.
3658 We could signal an error, but this feature will typically be used
3659 asynchronously in timers or process sentinels, so we don't. */
3660 return Qnil;
3663 /* Obsolete since 24.3. */
3664 void
3665 temp_output_buffer_show (register Lisp_Object buf)
3667 register struct buffer *old = current_buffer;
3668 register Lisp_Object window;
3669 register struct window *w;
3671 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3673 Fset_buffer (buf);
3674 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3675 BEGV = BEG;
3676 ZV = Z;
3677 SET_PT (BEG);
3678 set_buffer_internal (old);
3680 if (!NILP (Vtemp_buffer_show_function))
3681 call1 (Vtemp_buffer_show_function, buf);
3682 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3684 if (!EQ (XWINDOW (window)->frame, selected_frame))
3685 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3686 Vminibuf_scroll_window = window;
3687 w = XWINDOW (window);
3688 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3689 w->suspend_auto_hscroll = false;
3690 set_marker_restricted_both (w->start, buf, BEG, BEG);
3691 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3692 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3694 /* Run temp-buffer-show-hook, with the chosen window selected
3695 and its buffer current. */
3697 ptrdiff_t count = SPECPDL_INDEX ();
3698 Lisp_Object prev_window, prev_buffer;
3699 prev_window = selected_window;
3700 XSETBUFFER (prev_buffer, old);
3702 /* Select the window that was chosen, for running the hook.
3703 Note: Both Fselect_window and select_window_norecord may
3704 set-buffer to the buffer displayed in the window,
3705 so we need to save the current buffer. --stef */
3706 record_unwind_protect (restore_buffer, prev_buffer);
3707 record_unwind_protect (select_window_norecord, prev_window);
3708 Fselect_window (window, Qt);
3709 Fset_buffer (w->contents);
3710 run_hook (Qtemp_buffer_show_hook);
3711 unbind_to (count, Qnil);
3716 /* Allocate basically initialized window. */
3718 static struct window *
3719 allocate_window (void)
3721 return ALLOCATE_ZEROED_PSEUDOVECTOR
3722 (struct window, current_matrix, PVEC_WINDOW);
3725 /* Make new window, have it replace WINDOW in window-tree, and make
3726 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3727 horizontal child). */
3728 static void
3729 make_parent_window (Lisp_Object window, bool horflag)
3731 Lisp_Object parent;
3732 register struct window *o, *p;
3734 o = XWINDOW (window);
3735 p = allocate_window ();
3736 memcpy ((char *) p + sizeof (struct vectorlike_header),
3737 (char *) o + sizeof (struct vectorlike_header),
3738 word_size * VECSIZE (struct window));
3739 /* P's buffer slot may change from nil to a buffer... */
3740 adjust_window_count (p, 1);
3741 XSETWINDOW (parent, p);
3743 p->sequence_number = ++sequence_number;
3745 replace_window (window, parent, true);
3747 wset_next (o, Qnil);
3748 wset_prev (o, Qnil);
3749 wset_parent (o, parent);
3750 /* ...but now P becomes an internal window. */
3751 wset_start (p, Qnil);
3752 wset_pointm (p, Qnil);
3753 wset_old_pointm (p, Qnil);
3754 wset_buffer (p, Qnil);
3755 wset_combination (p, horflag, window);
3756 wset_combination_limit (p, Qnil);
3757 wset_window_parameters (p, Qnil);
3760 /* Make new window from scratch. */
3761 Lisp_Object
3762 make_window (void)
3764 Lisp_Object window;
3765 register struct window *w;
3767 w = allocate_window ();
3768 /* Initialize Lisp data. Note that allocate_window initializes all
3769 Lisp data to nil, so do it only for slots which should not be nil. */
3770 wset_normal_lines (w, make_float (1.0));
3771 wset_normal_cols (w, make_float (1.0));
3772 wset_new_total (w, make_number (0));
3773 wset_new_normal (w, make_number (0));
3774 wset_new_pixel (w, make_number (0));
3775 wset_start (w, Fmake_marker ());
3776 wset_pointm (w, Fmake_marker ());
3777 wset_old_pointm (w, Fmake_marker ());
3778 wset_vertical_scroll_bar_type (w, Qt);
3779 wset_horizontal_scroll_bar_type (w, Qt);
3780 /* These Lisp fields are marked specially so they're not set to nil by
3781 allocate_window. */
3782 wset_prev_buffers (w, Qnil);
3783 wset_next_buffers (w, Qnil);
3785 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3786 non-Lisp data, so do it only for slots which should not be zero. */
3787 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3788 w->left_fringe_width = w->right_fringe_width = -1;
3789 w->mode_line_height = w->header_line_height = -1;
3790 #ifdef HAVE_WINDOW_SYSTEM
3791 w->phys_cursor_type = NO_CURSOR;
3792 w->phys_cursor_width = -1;
3793 #endif
3794 w->sequence_number = ++sequence_number;
3795 w->pixel_width_before_size_change = 0;
3796 w->pixel_height_before_size_change = 0;
3797 w->scroll_bar_width = -1;
3798 w->scroll_bar_height = -1;
3799 w->column_number_displayed = -1;
3800 /* Reset window_list. */
3801 Vwindow_list = Qnil;
3802 /* Return window. */
3803 XSETWINDOW (window, w);
3804 return window;
3807 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3808 doc: /* Set new pixel size of WINDOW to SIZE.
3809 WINDOW must be a valid window and defaults to the selected one.
3810 Return SIZE.
3812 Optional argument ADD non-nil means add SIZE to the new pixel size of
3813 WINDOW and return the sum.
3815 The new pixel size of WINDOW, if valid, will be shortly installed as
3816 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3817 `window-pixel-width').
3819 Note: This function does not operate on any child windows of WINDOW. */)
3820 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3822 struct window *w = decode_valid_window (window);
3823 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3824 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3826 CHECK_RANGED_INTEGER (size, size_min, size_max);
3827 if (NILP (add))
3828 wset_new_pixel (w, size);
3829 else
3830 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3832 return w->new_pixel;
3835 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3836 doc: /* Set new total size of WINDOW to SIZE.
3837 WINDOW must be a valid window and defaults to the selected one.
3838 Return SIZE.
3840 Optional argument ADD non-nil means add SIZE to the new total size of
3841 WINDOW and return the sum.
3843 The new total size of WINDOW, if valid, will be shortly installed as
3844 WINDOW's total height (see `window-total-height') or total width (see
3845 `window-total-width').
3847 Note: This function does not operate on any child windows of WINDOW. */)
3848 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3850 struct window *w = decode_valid_window (window);
3852 CHECK_NUMBER (size);
3853 if (NILP (add))
3854 wset_new_total (w, size);
3855 else
3856 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3858 return w->new_total;
3861 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3862 doc: /* Set new normal size of WINDOW to SIZE.
3863 WINDOW must be a valid window and defaults to the selected one.
3864 Return SIZE.
3866 The new normal size of WINDOW, if valid, will be shortly installed as
3867 WINDOW's normal size (see `window-normal-size').
3869 Note: This function does not operate on any child windows of WINDOW. */)
3870 (Lisp_Object window, Lisp_Object size)
3872 wset_new_normal (decode_valid_window (window), size);
3873 return size;
3876 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3877 to w->new_pixel would result in correct heights (widths)
3878 for window W and recursively all child windows of W.
3880 Note: This function does not check any of `window-fixed-size-p',
3881 `window-min-height' or `window-min-width'. It does check that window
3882 sizes do not drop below one line (two columns). */
3883 static bool
3884 window_resize_check (struct window *w, bool horflag)
3886 struct frame *f = XFRAME (w->frame);
3887 struct window *c;
3889 if (WINDOW_VERTICAL_COMBINATION_P (w))
3890 /* W is a vertical combination. */
3892 c = XWINDOW (w->contents);
3893 if (horflag)
3894 /* All child windows of W must have the same width as W. */
3896 while (c)
3898 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3899 || !window_resize_check (c, horflag))
3900 return false;
3902 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3905 return true;
3907 else
3908 /* The sum of the heights of the child windows of W must equal
3909 W's height. */
3911 int remaining_pixels = XINT (w->new_pixel);
3913 while (c)
3915 if (!window_resize_check (c, horflag))
3916 return false;
3918 remaining_pixels -= XINT (c->new_pixel);
3919 if (remaining_pixels < 0)
3920 return false;
3921 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3924 return remaining_pixels == 0;
3927 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3928 /* W is a horizontal combination. */
3930 c = XWINDOW (w->contents);
3931 if (horflag)
3932 /* The sum of the widths of the child windows of W must equal W's
3933 width. */
3935 int remaining_pixels = XINT (w->new_pixel);
3937 while (c)
3939 if (!window_resize_check (c, horflag))
3940 return false;
3942 remaining_pixels -= XINT (c->new_pixel);
3943 if (remaining_pixels < 0)
3944 return false;
3945 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3948 return remaining_pixels == 0;
3950 else
3951 /* All child windows of W must have the same height as W. */
3953 while (c)
3955 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3956 || !window_resize_check (c, horflag))
3957 return false;
3959 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3962 return true;
3965 else
3966 /* A leaf window. Make sure it's not too small. The following
3967 hardcodes the values of `window-safe-min-width' (2) and
3968 `window-safe-min-height' (1) which are defined in window.el. */
3969 return (XINT (w->new_pixel) >= (horflag
3970 ? (2 * FRAME_COLUMN_WIDTH (f))
3971 : FRAME_LINE_HEIGHT (f)));
3975 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3976 w->new_pixel for window W and recursively all child windows of W.
3977 Also calculate and assign the new vertical (horizontal) pixel start
3978 positions of each of these windows.
3980 This function does not perform any error checks. Make sure you have
3981 run window_resize_check on W before applying this function. */
3982 static void
3983 window_resize_apply (struct window *w, bool horflag)
3985 struct window *c;
3986 int edge;
3987 int unit = (horflag
3988 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3989 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3991 /* Note: Assigning new_normal requires that the new total size of the
3992 parent window has been set *before*. */
3993 if (horflag)
3995 w->pixel_width = XFASTINT (w->new_pixel);
3996 w->total_cols = w->pixel_width / unit;
3997 if (NUMBERP (w->new_normal))
3998 wset_normal_cols (w, w->new_normal);
4000 edge = w->pixel_left;
4002 else
4004 w->pixel_height = XFASTINT (w->new_pixel);
4005 w->total_lines = w->pixel_height / unit;
4006 if (NUMBERP (w->new_normal))
4007 wset_normal_lines (w, w->new_normal);
4009 edge = w->pixel_top;
4012 if (WINDOW_VERTICAL_COMBINATION_P (w))
4013 /* W is a vertical combination. */
4015 c = XWINDOW (w->contents);
4016 while (c)
4018 if (horflag)
4020 c->pixel_left = edge;
4021 c->left_col = edge / unit;
4023 else
4025 c->pixel_top = edge;
4026 c->top_line = edge / unit;
4028 window_resize_apply (c, horflag);
4029 if (!horflag)
4030 edge = edge + c->pixel_height;
4032 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4035 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4036 /* W is a horizontal combination. */
4038 c = XWINDOW (w->contents);
4039 while (c)
4041 if (horflag)
4043 c->pixel_left = edge;
4044 c->left_col = edge / unit;
4046 else
4048 c->pixel_top = edge;
4049 c->top_line = edge / unit;
4052 window_resize_apply (c, horflag);
4053 if (horflag)
4054 edge = edge + c->pixel_width;
4056 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4059 else
4060 /* Bug#15957. */
4061 w->window_end_valid = false;
4065 /* Set w->total_lines (w->total_cols if HORFLAG) to
4066 w->new_total for window W and recursively all child windows of W.
4067 Also calculate and assign the new vertical (horizontal) start
4068 positions of each of these windows. */
4069 static void
4070 window_resize_apply_total (struct window *w, bool horflag)
4072 struct window *c;
4073 int edge;
4075 /* Note: Assigning new_normal requires that the new total size of the
4076 parent window has been set *before*. */
4077 if (horflag)
4079 w->total_cols = XFASTINT (w->new_total);
4080 edge = w->left_col;
4082 else
4084 w->total_lines = XFASTINT (w->new_total);
4085 edge = w->top_line;
4088 if (WINDOW_VERTICAL_COMBINATION_P (w))
4089 /* W is a vertical combination. */
4091 c = XWINDOW (w->contents);
4092 while (c)
4094 if (horflag)
4095 c->left_col = edge;
4096 else
4097 c->top_line = edge;
4099 window_resize_apply_total (c, horflag);
4100 if (!horflag)
4101 edge = edge + c->total_lines;
4103 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4106 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4107 /* W is a horizontal combination. */
4109 c = XWINDOW (w->contents);
4110 while (c)
4112 if (horflag)
4113 c->left_col = edge;
4114 else
4115 c->top_line = edge;
4117 window_resize_apply_total (c, horflag);
4118 if (horflag)
4119 edge = edge + c->total_cols;
4121 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4126 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4127 doc: /* Apply requested size values for window-tree of FRAME.
4128 If FRAME is omitted or nil, it defaults to the selected frame.
4130 Optional argument HORIZONTAL omitted or nil means apply requested
4131 height values. HORIZONTAL non-nil means apply requested width values.
4133 The requested size values are those set by `set-window-new-pixel' and
4134 `set-window-new-normal'. This function checks whether the requested
4135 values sum up to a valid window layout, recursively assigns the new
4136 sizes of all child windows and calculates and assigns the new start
4137 positions of these windows.
4139 Return t if the requested values have been applied correctly, nil
4140 otherwise.
4142 Note: This function does not check any of `window-fixed-size-p',
4143 `window-min-height' or `window-min-width'. All these checks have to
4144 be applied on the Elisp level. */)
4145 (Lisp_Object frame, Lisp_Object horizontal)
4147 struct frame *f = decode_live_frame (frame);
4148 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4149 bool horflag = !NILP (horizontal);
4151 if (!window_resize_check (r, horflag)
4152 || (XINT (r->new_pixel)
4153 != (horflag ? r->pixel_width : r->pixel_height)))
4154 return Qnil;
4156 block_input ();
4157 window_resize_apply (r, horflag);
4159 fset_redisplay (f);
4161 adjust_frame_glyphs (f);
4162 unblock_input ();
4164 return Qt;
4168 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4169 doc: /* Apply requested total size values for window-tree of FRAME.
4170 If FRAME is omitted or nil, it defaults to the selected frame.
4172 This function does not assign pixel or normal size values. You should
4173 have run `window-resize-apply' before running this.
4175 Optional argument HORIZONTAL omitted or nil means apply requested
4176 height values. HORIZONTAL non-nil means apply requested width
4177 values. */)
4178 (Lisp_Object frame, Lisp_Object horizontal)
4180 struct frame *f = decode_live_frame (frame);
4181 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4183 block_input ();
4184 /* Necessary when deleting the top-/or leftmost window. */
4185 r->left_col = 0;
4186 r->top_line = FRAME_TOP_MARGIN (f);
4187 window_resize_apply_total (r, !NILP (horizontal));
4188 /* Handle the mini window. */
4189 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4191 struct window *m = XWINDOW (f->minibuffer_window);
4193 if (NILP (horizontal))
4195 m->top_line = r->top_line + r->total_lines;
4196 m->total_lines = XFASTINT (m->new_total);
4198 else
4199 m->total_cols = XFASTINT (m->new_total);
4202 unblock_input ();
4204 return Qt;
4208 /* Resize frame F's windows when F's width or height is set to SIZE.
4209 If HORFLAG is zero, F's width was set to SIZE, otherwise its height
4210 was set. SIZE is interpreted in F's canonical character units
4211 (a.k.a. "columns" or "lines"), unless PIXELWISE is non-zero, which
4212 means to interpret SIZE in pixel units. */
4213 void
4214 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4216 Lisp_Object root = f->root_window;
4217 struct window *r = XWINDOW (root);
4218 Lisp_Object mini = f->minibuffer_window;
4219 struct window *m;
4220 /* old_size is the old size of the frame's root window. */
4221 int old_size = horflag ? r->total_cols : r->total_lines;
4222 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4223 /* new_size is the new size of the frame's root window. */
4224 int new_size, new_pixel_size;
4225 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4227 /* Don't let the size drop below one unit. This is more comforting
4228 when we are called from x_set_tool_bar_lines since the latter may
4229 have implicitly given us a zero or negative height. */
4230 if (pixelwise)
4232 /* Note: This does not include the size for internal borders
4233 since these are not part of the frame's text area. */
4234 new_pixel_size = max (horflag
4235 ? size
4236 : (size
4237 - ((FRAME_HAS_MINIBUF_P (f)
4238 && !FRAME_MINIBUF_ONLY_P (f))
4239 ? FRAME_LINE_HEIGHT (f) : 0)),
4240 unit);
4241 new_size = new_pixel_size / unit;
4243 else
4245 new_size = max (size - (!horflag
4246 && FRAME_HAS_MINIBUF_P (f)
4247 && !FRAME_MINIBUF_ONLY_P (f)),
4249 new_pixel_size = new_size * unit;
4252 if (new_pixel_size == old_pixel_size
4253 && (horflag || r->pixel_top == FRAME_TOP_MARGIN_HEIGHT (f)))
4255 else if (WINDOW_LEAF_P (r))
4256 /* For a leaf root window just set the size. */
4257 if (horflag)
4259 r->total_cols = new_size;
4260 r->pixel_width = new_pixel_size;
4262 else
4264 r->top_line = FRAME_TOP_MARGIN (f);
4265 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4267 r->total_lines = new_size;
4268 r->pixel_height = new_pixel_size;
4270 else
4272 Lisp_Object delta;
4274 if (!horflag)
4276 r->top_line = FRAME_TOP_MARGIN (f);
4277 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4280 if (pixelwise)
4281 XSETINT (delta, new_pixel_size - old_pixel_size);
4282 else
4283 XSETINT (delta, new_size - old_size);
4285 /* Try a "normal" resize first. */
4286 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4287 pixelwise ? Qt : Qnil);
4288 if (window_resize_check (r, horflag)
4289 && new_pixel_size == XINT (r->new_pixel))
4291 window_resize_apply (r, horflag);
4292 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4294 else
4296 /* Try with "reasonable" minimum sizes next. */
4297 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4298 pixelwise ? Qt : Qnil);
4299 if (window_resize_check (r, horflag)
4300 && new_pixel_size == XINT (r->new_pixel))
4302 window_resize_apply (r, horflag);
4303 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4308 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4310 m = XWINDOW (mini);
4311 if (horflag)
4313 m->total_cols = new_size;
4314 m->pixel_width = new_pixel_size;
4316 else
4318 /* Are we sure we always want 1 line here? */
4319 m->total_lines = 1;
4320 m->pixel_height = FRAME_LINE_HEIGHT (f);
4321 m->top_line = r->top_line + r->total_lines;
4322 m->pixel_top = r->pixel_top + r->pixel_height;
4326 fset_redisplay (f);
4330 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4331 doc: /* Split window OLD.
4332 Second argument PIXEL-SIZE specifies the number of pixels of the
4333 new window. It must be a positive integer.
4335 Third argument SIDE nil (or `below') specifies that the new window shall
4336 be located below WINDOW. SIDE `above' means the new window shall be
4337 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4338 height of the new window including space reserved for the mode and/or
4339 header line.
4341 SIDE t (or `right') specifies that the new window shall be located on
4342 the right side of WINDOW. SIDE `left' means the new window shall be
4343 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4344 width of the new window including space reserved for fringes and the
4345 scrollbar or a divider column.
4347 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4348 according to the SIDE argument.
4350 The new pixel and normal sizes of all involved windows must have been
4351 set correctly. See the code of `split-window' for how this is done. */)
4352 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4354 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4355 parent window or an internal window we have to install as OLD's new
4356 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4357 provided OLD is a leaf window, or to the frame's selected window.
4358 NEW (*n) is the new window created with some parameters taken from
4359 REFERENCE (*r). */
4360 Lisp_Object new, frame, reference;
4361 struct window *o, *p, *n, *r, *c;
4362 struct frame *f;
4363 bool horflag
4364 /* HORFLAG is true when we split side-by-side, false otherwise. */
4365 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4367 CHECK_WINDOW (old);
4368 o = XWINDOW (old);
4369 frame = WINDOW_FRAME (o);
4370 f = XFRAME (frame);
4372 CHECK_NUMBER (pixel_size);
4373 EMACS_INT total_size
4374 = XINT (pixel_size) / (horflag
4375 ? FRAME_COLUMN_WIDTH (f)
4376 : FRAME_LINE_HEIGHT (f));
4378 /* Set combination_limit if we have to make a new parent window.
4379 We do that if either `window-combination-limit' is t, or OLD has no
4380 parent, or OLD is ortho-combined. */
4381 bool combination_limit
4382 = (EQ (Vwindow_combination_limit, Qt)
4383 || NILP (o->parent)
4384 || (horflag
4385 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4386 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4388 /* We need a live reference window to initialize some parameters. */
4389 if (WINDOW_LIVE_P (old))
4390 /* OLD is live, use it as reference window. */
4391 reference = old;
4392 else
4393 /* Use the frame's selected window as reference window. */
4394 reference = FRAME_SELECTED_WINDOW (f);
4395 r = XWINDOW (reference);
4397 /* The following bugs are caught by `split-window'. */
4398 if (MINI_WINDOW_P (o))
4399 error ("Attempt to split minibuffer window");
4400 else if (total_size < (horflag ? 2 : 1))
4401 error ("Size of new window too small (after split)");
4402 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4403 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4404 proportionally. */
4406 p = XWINDOW (o->parent);
4407 /* Temporarily pretend we split the parent window. */
4408 wset_new_pixel
4409 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4410 - XINT (pixel_size)));
4411 if (!window_resize_check (p, horflag))
4412 error ("Window sizes don't fit");
4413 else
4414 /* Undo the temporary pretension. */
4415 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4417 else
4419 if (!window_resize_check (o, horflag))
4420 error ("Resizing old window failed");
4421 else if (XINT (pixel_size) + XINT (o->new_pixel)
4422 != (horflag ? o->pixel_width : o->pixel_height))
4423 error ("Sum of sizes of old and new window don't fit");
4426 /* This is our point of no return. */
4427 if (combination_limit)
4429 /* Save the old value of o->normal_cols/lines. It gets corrupted
4430 by make_parent_window and we need it below for assigning it to
4431 p->new_normal. */
4432 Lisp_Object new_normal
4433 = horflag ? o->normal_cols : o->normal_lines;
4435 make_parent_window (old, horflag);
4436 p = XWINDOW (o->parent);
4437 if (EQ (Vwindow_combination_limit, Qt))
4438 /* Store t in the new parent's combination_limit slot to avoid
4439 that its children get merged into another window. */
4440 wset_combination_limit (p, Qt);
4441 /* These get applied below. */
4442 wset_new_pixel
4443 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4444 wset_new_total
4445 (p, make_number (horflag ? o->total_cols : o->total_lines));
4446 wset_new_normal (p, new_normal);
4448 else
4449 p = XWINDOW (o->parent);
4451 fset_redisplay (f);
4452 new = make_window ();
4453 n = XWINDOW (new);
4454 wset_frame (n, frame);
4455 wset_parent (n, o->parent);
4457 if (EQ (side, Qabove) || EQ (side, Qleft))
4459 wset_prev (n, o->prev);
4460 if (NILP (n->prev))
4461 wset_combination (p, horflag, new);
4462 else
4463 wset_next (XWINDOW (n->prev), new);
4464 wset_next (n, old);
4465 wset_prev (o, new);
4467 else
4469 wset_next (n, o->next);
4470 if (!NILP (n->next))
4471 wset_prev (XWINDOW (n->next), new);
4472 wset_prev (n, old);
4473 wset_next (o, new);
4476 n->window_end_valid = false;
4477 n->last_cursor_vpos = 0;
4479 /* Get special geometry settings from reference window. */
4480 n->left_margin_cols = r->left_margin_cols;
4481 n->right_margin_cols = r->right_margin_cols;
4482 n->left_fringe_width = r->left_fringe_width;
4483 n->right_fringe_width = r->right_fringe_width;
4484 n->fringes_outside_margins = r->fringes_outside_margins;
4485 n->scroll_bar_width = r->scroll_bar_width;
4486 n->scroll_bar_height = r->scroll_bar_height;
4487 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4488 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4490 /* Directly assign orthogonal coordinates and sizes. */
4491 if (horflag)
4493 n->pixel_top = o->pixel_top;
4494 n->top_line = o->top_line;
4495 n->pixel_height = o->pixel_height;
4496 n->total_lines = o->total_lines;
4498 else
4500 n->pixel_left = o->pixel_left;
4501 n->left_col = o->left_col;
4502 n->pixel_width = o->pixel_width;
4503 n->total_cols = o->total_cols;
4506 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4507 get them ready here. */
4508 wset_new_pixel (n, pixel_size);
4509 EMACS_INT sum = 0;
4510 c = XWINDOW (p->contents);
4511 while (c)
4513 if (c != n)
4514 sum = sum + XINT (c->new_total);
4515 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4517 wset_new_total (n, make_number ((horflag
4518 ? p->total_cols
4519 : p->total_lines)
4520 - sum));
4521 wset_new_normal (n, normal_size);
4523 block_input ();
4524 window_resize_apply (p, horflag);
4525 adjust_frame_glyphs (f);
4526 /* Set buffer of NEW to buffer of reference window. Don't run
4527 any hooks. */
4528 set_window_buffer (new, r->contents, false, true);
4529 unblock_input ();
4531 /* Maybe we should run the scroll functions in Elisp (which already
4532 runs the configuration change hook). */
4533 if (! NILP (Vwindow_scroll_functions))
4534 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4535 Fmarker_position (n->start));
4536 /* Return NEW. */
4537 return new;
4541 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4542 doc: /* Remove WINDOW from its frame.
4543 WINDOW defaults to the selected window. Return nil.
4544 Signal an error when WINDOW is the only window on its frame. */)
4545 (Lisp_Object window)
4547 Lisp_Object parent, sibling, frame, root;
4548 struct window *w, *p, *s, *r;
4549 struct frame *f;
4550 bool horflag, before_sibling = false;
4552 w = decode_any_window (window);
4553 XSETWINDOW (window, w);
4554 if (NILP (w->contents))
4555 /* It's a no-op to delete an already deleted window. */
4556 return Qnil;
4558 parent = w->parent;
4559 if (NILP (parent))
4560 /* Never delete a minibuffer or frame root window. */
4561 error ("Attempt to delete minibuffer or sole ordinary window");
4562 else if (NILP (w->prev) && NILP (w->next))
4563 /* Rather bow out here, this case should be handled on the Elisp
4564 level. */
4565 error ("Attempt to delete sole window of parent");
4567 p = XWINDOW (parent);
4568 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4570 frame = WINDOW_FRAME (w);
4571 f = XFRAME (frame);
4573 root = FRAME_ROOT_WINDOW (f);
4574 r = XWINDOW (root);
4576 /* Unlink WINDOW from window tree. */
4577 if (NILP (w->prev))
4578 /* Get SIBLING below (on the right of) WINDOW. */
4580 /* before_sibling means WINDOW is the first child of its
4581 parent and thus before the sibling. */
4582 before_sibling = true;
4583 sibling = w->next;
4584 s = XWINDOW (sibling);
4585 wset_prev (s, Qnil);
4586 wset_combination (p, horflag, sibling);
4588 else
4589 /* Get SIBLING above (on the left of) WINDOW. */
4591 sibling = w->prev;
4592 s = XWINDOW (sibling);
4593 wset_next (s, w->next);
4594 if (!NILP (s->next))
4595 wset_prev (XWINDOW (s->next), sibling);
4598 if (window_resize_check (r, horflag)
4599 && (XINT (r->new_pixel)
4600 == (horflag ? r->pixel_width : r->pixel_height)))
4601 /* We can delete WINDOW now. */
4604 /* Block input. */
4605 block_input ();
4606 xwidget_view_delete_all_in_window (w);
4607 window_resize_apply (p, horflag);
4608 /* If this window is referred to by the dpyinfo's mouse
4609 highlight, invalidate that slot to be safe (Bug#9904). */
4610 if (!FRAME_INITIAL_P (f))
4612 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4614 if (EQ (hlinfo->mouse_face_window, window))
4615 hlinfo->mouse_face_window = Qnil;
4618 fset_redisplay (f);
4619 Vwindow_list = Qnil;
4621 wset_next (w, Qnil); /* Don't delete w->next too. */
4622 free_window_matrices (w);
4624 if (WINDOWP (w->contents))
4626 delete_all_child_windows (w->contents);
4627 wset_combination (w, false, Qnil);
4629 else
4631 unshow_buffer (w);
4632 unchain_marker (XMARKER (w->pointm));
4633 unchain_marker (XMARKER (w->old_pointm));
4634 unchain_marker (XMARKER (w->start));
4635 wset_buffer (w, Qnil);
4638 if (NILP (s->prev) && NILP (s->next))
4639 /* A matrjoshka where SIBLING has become the only child of
4640 PARENT. */
4642 /* Put SIBLING into PARENT's place. */
4643 replace_window (parent, sibling, false);
4644 /* Have SIBLING inherit the following three slot values from
4645 PARENT (the combination_limit slot is not inherited). */
4646 wset_normal_cols (s, p->normal_cols);
4647 wset_normal_lines (s, p->normal_lines);
4648 /* Mark PARENT as deleted. */
4649 wset_combination (p, false, Qnil);
4650 /* Try to merge SIBLING into its new parent. */
4651 recombine_windows (sibling);
4654 adjust_frame_glyphs (f);
4656 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4657 /* We deleted the frame's selected window. */
4659 /* Use the frame's first window as fallback ... */
4660 Lisp_Object new_selected_window = Fframe_first_window (frame);
4661 /* ... but preferably use its most recently used window. */
4662 Lisp_Object mru_window;
4664 /* `get-mru-window' might fail for some reason so play it safe
4665 - promote the first window _without recording it_ first. */
4666 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4667 Fselect_window (new_selected_window, Qt);
4668 else
4669 fset_selected_window (f, new_selected_window);
4671 unblock_input ();
4673 /* Now look whether `get-mru-window' gets us something. */
4674 mru_window = call1 (Qget_mru_window, frame);
4675 if (WINDOW_LIVE_P (mru_window)
4676 && EQ (XWINDOW (mru_window)->frame, frame))
4677 new_selected_window = mru_window;
4679 /* If all ended up well, we now promote the mru window. */
4680 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4681 Fselect_window (new_selected_window, Qnil);
4682 else
4683 fset_selected_window (f, new_selected_window);
4685 else
4686 unblock_input ();
4688 else
4689 /* We failed: Relink WINDOW into window tree. */
4691 if (before_sibling)
4693 wset_prev (s, window);
4694 wset_combination (p, horflag, window);
4696 else
4698 wset_next (s, window);
4699 if (!NILP (w->next))
4700 wset_prev (XWINDOW (w->next), window);
4702 error ("Deletion failed");
4705 return Qnil;
4708 /***********************************************************************
4709 Resizing Mini-Windows
4710 ***********************************************************************/
4712 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4713 can. */
4714 void
4715 grow_mini_window (struct window *w, int delta, bool pixelwise)
4717 struct frame *f = XFRAME (w->frame);
4718 struct window *r;
4719 Lisp_Object root, height;
4720 int line_height, pixel_height;
4722 eassert (MINI_WINDOW_P (w));
4723 eassert (delta >= 0);
4725 if (delta > 0)
4727 root = FRAME_ROOT_WINDOW (f);
4728 r = XWINDOW (root);
4729 height = call3 (Qwindow__resize_root_window_vertically,
4730 root, make_number (- delta), pixelwise ? Qt : Qnil);
4731 if (INTEGERP (height) && window_resize_check (r, false))
4733 block_input ();
4734 window_resize_apply (r, false);
4736 if (pixelwise)
4738 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4739 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4741 else
4743 line_height = min (-XINT (height),
4744 ((INT_MAX - w->pixel_height)
4745 / FRAME_LINE_HEIGHT (f)));
4746 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4749 /* Grow the mini-window. */
4750 w->pixel_top = r->pixel_top + r->pixel_height;
4751 w->top_line = r->top_line + r->total_lines;
4752 /* Make sure the mini-window has always at least one line. */
4753 w->pixel_height = max (w->pixel_height + pixel_height,
4754 FRAME_LINE_HEIGHT (f));
4755 w->total_lines = max (w->total_lines + line_height, 1);
4757 /* Enforce full redisplay of the frame. */
4758 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4759 fset_redisplay (f);
4760 adjust_frame_glyphs (f);
4761 unblock_input ();
4763 else
4764 error ("Failed to grow minibuffer window");
4769 /* Shrink mini-window W to one line. */
4770 void
4771 shrink_mini_window (struct window *w, bool pixelwise)
4773 struct frame *f = XFRAME (w->frame);
4774 struct window *r;
4775 Lisp_Object root, delta;
4776 EMACS_INT height, unit;
4778 eassert (MINI_WINDOW_P (w));
4780 height = pixelwise ? w->pixel_height : w->total_lines;
4781 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4782 if (height > unit)
4784 root = FRAME_ROOT_WINDOW (f);
4785 r = XWINDOW (root);
4786 delta = call3 (Qwindow__resize_root_window_vertically,
4787 root, make_number (height - unit),
4788 pixelwise ? Qt : Qnil);
4789 if (INTEGERP (delta) && window_resize_check (r, false))
4791 block_input ();
4792 window_resize_apply (r, false);
4794 /* Shrink the mini-window. */
4795 w->top_line = r->top_line + r->total_lines;
4796 w->total_lines = 1;
4797 w->pixel_top = r->pixel_top + r->pixel_height;
4798 w->pixel_height = FRAME_LINE_HEIGHT (f);
4799 /* Enforce full redisplay of the frame. */
4800 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4801 fset_redisplay (f);
4802 adjust_frame_glyphs (f);
4803 unblock_input ();
4805 /* If the above failed for whatever strange reason we must make a
4806 one window frame here. The same routine will be needed when
4807 shrinking the frame (and probably when making the initial
4808 *scratch* window). For the moment leave things as they are. */
4809 else
4810 error ("Failed to shrink minibuffer window");
4814 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4815 doc: /* Resize minibuffer window WINDOW. */)
4816 (Lisp_Object window)
4818 struct window *w = XWINDOW (window);
4819 struct window *r;
4820 struct frame *f;
4821 int height;
4823 CHECK_WINDOW (window);
4824 f = XFRAME (w->frame);
4826 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4827 error ("Not a valid minibuffer window");
4828 else if (FRAME_MINIBUF_ONLY_P (f))
4829 error ("Cannot resize a minibuffer-only frame");
4831 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4832 height = r->pixel_height + w->pixel_height;
4833 if (window_resize_check (r, false)
4834 && XINT (w->new_pixel) > 0
4835 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4837 block_input ();
4838 window_resize_apply (r, false);
4840 w->pixel_height = XFASTINT (w->new_pixel);
4841 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4842 w->pixel_top = r->pixel_top + r->pixel_height;
4843 w->top_line = r->top_line + r->total_lines;
4845 fset_redisplay (f);
4846 adjust_frame_glyphs (f);
4847 unblock_input ();
4848 return Qt;
4850 else
4851 error ("Failed to resize minibuffer window");
4854 /* Mark window cursors off for all windows in the window tree rooted
4855 at W by setting their phys_cursor_on_p flag to zero. Called from
4856 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4857 the frame are cleared. */
4859 void
4860 mark_window_cursors_off (struct window *w)
4862 while (w)
4864 if (WINDOWP (w->contents))
4865 mark_window_cursors_off (XWINDOW (w->contents));
4866 else
4867 w->phys_cursor_on_p = false;
4869 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4875 * window_wants_mode_line:
4877 * Return 1 if window W wants a mode line and is high enough to
4878 * accommodate it, 0 otherwise.
4880 * W wants a mode line if it's a leaf window and neither a minibuffer
4881 * nor a pseudo window. Moreover, its 'window-mode-line-format'
4882 * parameter must not be 'none' and either that parameter or W's
4883 * buffer's 'mode-line-format' value must be non-nil. Finally, W must
4884 * be higher than its frame's canonical character height.
4886 bool
4887 window_wants_mode_line (struct window *w)
4889 Lisp_Object window_mode_line_format =
4890 window_parameter (w, Qmode_line_format);
4892 return ((WINDOW_LEAF_P (w)
4893 && !MINI_WINDOW_P (w)
4894 && !WINDOW_PSEUDO_P (w)
4895 && !EQ (window_mode_line_format, Qnone)
4896 && (!NILP (window_mode_line_format)
4897 || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), mode_line_format)))
4898 && WINDOW_PIXEL_HEIGHT (w) > WINDOW_FRAME_LINE_HEIGHT (w))
4900 : 0);
4905 * window_wants_header_line:
4907 * Return 1 if window W wants a header line and is high enough to
4908 * accommodate it, 0 otherwise.
4910 * W wants a header line if it's a leaf window and neither a minibuffer
4911 * nor a pseudo window. Moreover, its 'window-mode-line-format'
4912 * parameter must not be 'none' and either that parameter or W's
4913 * buffer's 'mode-line-format' value must be non-nil. Finally, W must
4914 * be higher than its frame's canonical character height and be able to
4915 * accommodate a mode line too if necessary (the mode line prevails).
4917 bool
4918 window_wants_header_line (struct window *w)
4920 Lisp_Object window_header_line_format =
4921 window_parameter (w, Qheader_line_format);
4923 return ((WINDOW_LEAF_P (w)
4924 && !MINI_WINDOW_P (w)
4925 && !WINDOW_PSEUDO_P (w)
4926 && !EQ (window_header_line_format, Qnone)
4927 && (!NILP (window_header_line_format)
4928 || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), header_line_format)))
4929 && (WINDOW_PIXEL_HEIGHT (w)
4930 > (window_wants_mode_line (w)
4931 ? 2 * WINDOW_FRAME_LINE_HEIGHT (w)
4932 : WINDOW_FRAME_LINE_HEIGHT (w))))
4934 : 0);
4937 /* Return number of lines of text (not counting mode lines) in W. */
4940 window_internal_height (struct window *w)
4942 int ht = w->total_lines;
4944 if (!MINI_WINDOW_P (w))
4946 if (!NILP (w->parent)
4947 || WINDOWP (w->contents)
4948 || !NILP (w->next)
4949 || !NILP (w->prev)
4950 || window_wants_mode_line (w))
4951 --ht;
4953 if (window_wants_header_line (w))
4954 --ht;
4957 return ht;
4961 /************************************************************************
4962 Window Scrolling
4963 ***********************************************************************/
4965 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4966 N screen-fulls, which is defined as the height of the window minus
4967 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4968 instead. Negative values of N mean scroll down. NOERROR
4969 means don't signal an error if we try to move over BEGV or ZV,
4970 respectively. */
4972 static void
4973 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4975 ptrdiff_t count = SPECPDL_INDEX ();
4977 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4979 wset_redisplay (XWINDOW (window));
4981 if (whole && Vfast_but_imprecise_scrolling)
4982 specbind (Qfontification_functions, Qnil);
4984 /* If we must, use the pixel-based version which is much slower than
4985 the line-based one but can handle varying line heights. */
4986 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4987 window_scroll_pixel_based (window, n, whole, noerror);
4988 else
4989 window_scroll_line_based (window, n, whole, noerror);
4991 unbind_to (count, Qnil);
4993 /* Bug#15957. */
4994 XWINDOW (window)->window_end_valid = false;
4997 /* Compute scroll margin for WINDOW.
4998 We scroll when point is within this distance from the top or bottom
4999 of the window. The result is measured in lines or in pixels
5000 depending on the second parameter. */
5002 window_scroll_margin (struct window *window, enum margin_unit unit)
5004 if (scroll_margin > 0)
5006 int frame_line_height = default_line_pixel_height (window);
5007 int window_lines = window_box_height (window) / frame_line_height;
5009 double ratio = 0.25;
5010 if (FLOATP (Vmaximum_scroll_margin))
5012 ratio = XFLOAT_DATA (Vmaximum_scroll_margin);
5013 ratio = max (0.0, ratio);
5014 ratio = min (ratio, 0.5);
5016 int max_margin = min ((window_lines - 1)/2,
5017 (int) (window_lines * ratio));
5018 int margin = clip_to_bounds (0, scroll_margin, max_margin);
5019 return (unit == MARGIN_IN_PIXELS)
5020 ? margin * frame_line_height
5021 : margin;
5023 else
5024 return 0;
5028 /* Implementation of window_scroll that works based on pixel line
5029 heights. See the comment of window_scroll for parameter
5030 descriptions. */
5032 static void
5033 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
5035 struct it it;
5036 struct window *w = XWINDOW (window);
5037 struct text_pos start;
5038 int this_scroll_margin;
5039 /* True if we fiddled the window vscroll field without really scrolling. */
5040 bool vscrolled = false;
5041 int x, y, rtop, rbot, rowh, vpos;
5042 void *itdata = NULL;
5043 int frame_line_height = default_line_pixel_height (w);
5044 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5045 Fwindow_old_point (window)));
5047 SET_TEXT_POS_FROM_MARKER (start, w->start);
5048 /* Scrolling a minibuffer window via scroll bar when the echo area
5049 shows long text sometimes resets the minibuffer contents behind
5050 our backs. Also, someone might narrow-to-region and immediately
5051 call a scroll function. */
5052 if (CHARPOS (start) > ZV || CHARPOS (start) < BEGV)
5053 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5055 /* If PT is not visible in WINDOW, move back one half of
5056 the screen. Allow PT to be partially visible, otherwise
5057 something like (scroll-down 1) with PT in the line before
5058 the partially visible one would recenter. */
5060 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
5062 itdata = bidi_shelve_cache ();
5063 /* Move backward half the height of the window. Performance note:
5064 vmotion used here is about 10% faster, but would give wrong
5065 results for variable height lines. */
5066 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5067 it.current_y = it.last_visible_y;
5068 move_it_vertically_backward (&it, window_box_height (w) / 2);
5070 /* The function move_iterator_vertically may move over more than
5071 the specified y-distance. If it->w is small, e.g. a
5072 mini-buffer window, we may end up in front of the window's
5073 display area. Start displaying at the start of the line
5074 containing PT in this case. */
5075 if (it.current_y <= 0)
5077 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5078 move_it_vertically_backward (&it, 0);
5079 it.current_y = 0;
5082 start = it.current.pos;
5083 bidi_unshelve_cache (itdata, false);
5085 else if (auto_window_vscroll_p)
5087 if (rtop || rbot) /* Partially visible. */
5089 int px;
5090 int dy = frame_line_height;
5091 /* In the below we divide the window box height by the
5092 frame's line height to make the result predictable when
5093 the window box is not an integral multiple of the line
5094 height. This is important to ensure we get back to the
5095 same position when scrolling up, then down. */
5096 if (whole)
5097 dy = max ((window_box_height (w) / dy
5098 - next_screen_context_lines) * dy,
5099 dy);
5100 dy *= n;
5102 if (n < 0)
5104 /* Only vscroll backwards if already vscrolled forwards. */
5105 if (w->vscroll < 0 && rtop > 0)
5107 px = max (0, -w->vscroll - min (rtop, -dy));
5108 Fset_window_vscroll (window, make_number (px), Qt);
5109 return;
5112 if (n > 0)
5114 /* Do vscroll if already vscrolled or only display line. */
5115 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
5117 px = max (0, -w->vscroll + min (rbot, dy));
5118 Fset_window_vscroll (window, make_number (px), Qt);
5119 return;
5122 /* Maybe modify window start instead of scrolling. */
5123 if (rbot > 0 || w->vscroll < 0)
5125 ptrdiff_t spos;
5127 Fset_window_vscroll (window, make_number (0), Qt);
5128 /* If there are other text lines above the current row,
5129 move window start to current row. Else to next row. */
5130 if (rbot > 0)
5131 spos = XINT (Fline_beginning_position (Qnil));
5132 else
5133 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5134 set_marker_restricted (w->start, make_number (spos),
5135 w->contents);
5136 w->start_at_line_beg = true;
5137 wset_update_mode_line (w);
5138 /* Set force_start so that redisplay_window will run the
5139 window-scroll-functions. */
5140 w->force_start = true;
5141 return;
5145 /* Cancel previous vscroll. */
5146 Fset_window_vscroll (window, make_number (0), Qt);
5149 itdata = bidi_shelve_cache ();
5150 /* If scroll_preserve_screen_position is non-nil, we try to set
5151 point in the same window line as it is now, so get that line. */
5152 if (!NILP (Vscroll_preserve_screen_position))
5154 /* We preserve the goal pixel coordinate across consecutive
5155 calls to scroll-up, scroll-down and other commands that
5156 have the `scroll-command' property. This avoids the
5157 possibility of point becoming "stuck" on a tall line when
5158 scrolling by one line. */
5159 if (window_scroll_pixel_based_preserve_y < 0
5160 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5161 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5163 start_display (&it, w, start);
5164 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5165 window_scroll_pixel_based_preserve_y = it.current_y;
5166 window_scroll_pixel_based_preserve_x = it.current_x;
5169 else
5170 window_scroll_pixel_based_preserve_y
5171 = window_scroll_pixel_based_preserve_x = -1;
5173 /* Move iterator it from start the specified distance forward or
5174 backward. The result is the new window start. */
5175 start_display (&it, w, start);
5176 if (whole)
5178 ptrdiff_t start_pos = IT_CHARPOS (it);
5179 int dy = frame_line_height;
5180 /* In the below we divide the window box height by the frame's
5181 line height to make the result predictable when the window
5182 box is not an integral multiple of the line height. This is
5183 important to ensure we get back to the same position when
5184 scrolling up, then down. */
5185 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
5186 dy) * n;
5188 /* Note that move_it_vertically always moves the iterator to the
5189 start of a line. So, if the last line doesn't have a newline,
5190 we would end up at the start of the line ending at ZV. */
5191 if (dy <= 0)
5193 move_it_vertically_backward (&it, -dy);
5194 /* Ensure we actually do move, e.g. in case we are currently
5195 looking at an image that is taller that the window height. */
5196 while (start_pos == IT_CHARPOS (it)
5197 && start_pos > BEGV)
5198 move_it_by_lines (&it, -1);
5200 else if (dy > 0)
5202 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5203 MOVE_TO_POS | MOVE_TO_Y);
5204 /* Ensure we actually do move, e.g. in case we are currently
5205 looking at an image that is taller that the window height. */
5206 while (start_pos == IT_CHARPOS (it)
5207 && start_pos < ZV)
5208 move_it_by_lines (&it, 1);
5211 else
5212 move_it_by_lines (&it, n);
5214 /* We failed if we find ZV is already on the screen (scrolling up,
5215 means there's nothing past the end), or if we can't start any
5216 earlier (scrolling down, means there's nothing past the top). */
5217 if ((n > 0 && IT_CHARPOS (it) == ZV)
5218 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5220 if (IT_CHARPOS (it) == ZV)
5222 if (it.current_y < it.last_visible_y
5223 && (it.current_y + it.max_ascent + it.max_descent
5224 > it.last_visible_y))
5226 /* The last line was only partially visible, make it fully
5227 visible. */
5228 w->vscroll = (it.last_visible_y
5229 - it.current_y + it.max_ascent + it.max_descent);
5230 adjust_frame_glyphs (it.f);
5232 else
5234 bidi_unshelve_cache (itdata, false);
5235 if (noerror)
5236 return;
5237 else if (n < 0) /* could happen with empty buffers */
5238 xsignal0 (Qbeginning_of_buffer);
5239 else
5240 xsignal0 (Qend_of_buffer);
5243 else
5245 if (w->vscroll != 0)
5246 /* The first line was only partially visible, make it fully
5247 visible. */
5248 w->vscroll = 0;
5249 else
5251 bidi_unshelve_cache (itdata, false);
5252 if (noerror)
5253 return;
5254 else
5255 xsignal0 (Qbeginning_of_buffer);
5259 /* If control gets here, then we vscrolled. */
5261 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5263 /* Don't try to change the window start below. */
5264 vscrolled = true;
5267 if (! vscrolled)
5269 ptrdiff_t pos = IT_CHARPOS (it);
5270 ptrdiff_t bytepos;
5272 /* If in the middle of a multi-glyph character move forward to
5273 the next character. */
5274 if (in_display_vector_p (&it))
5276 ++pos;
5277 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5280 /* Set the window start, and set up the window for redisplay. */
5281 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5282 IT_BYTEPOS (it));
5283 bytepos = marker_byte_position (w->start);
5284 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5285 wset_update_mode_line (w);
5286 /* Set force_start so that redisplay_window will run the
5287 window-scroll-functions. */
5288 w->force_start = true;
5291 /* The rest of this function uses current_y in a nonstandard way,
5292 not including the height of the header line if any. */
5293 it.current_y = it.vpos = 0;
5295 /* Move PT out of scroll margins.
5296 This code wants current_y to be zero at the window start position
5297 even if there is a header line. */
5298 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
5300 if (n > 0)
5302 int last_y = it.last_visible_y - this_scroll_margin - 1;
5304 /* We moved the window start towards ZV, so PT may be now
5305 in the scroll margin at the top. */
5306 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5307 if (IT_CHARPOS (it) == PT
5308 && it.current_y >= this_scroll_margin
5309 && it.current_y <= last_y - WINDOW_HEADER_LINE_HEIGHT (w)
5310 && (NILP (Vscroll_preserve_screen_position)
5311 || EQ (Vscroll_preserve_screen_position, Qt)))
5312 /* We found PT at a legitimate height. Leave it alone. */
5314 else
5316 if (window_scroll_pixel_based_preserve_y >= 0)
5318 /* Don't enter the scroll margin at the end of the window. */
5319 int goal_y = min (last_y, window_scroll_pixel_based_preserve_y);
5321 /* If we have a header line, take account of it. This
5322 is necessary because we set it.current_y to 0, above. */
5323 move_it_to (&it, -1,
5324 window_scroll_pixel_based_preserve_x,
5325 goal_y - WINDOW_HEADER_LINE_HEIGHT (w),
5326 -1, MOVE_TO_Y | MOVE_TO_X);
5329 /* Get out of the scroll margin at the top of the window. */
5330 while (it.current_y < this_scroll_margin)
5332 int prev = it.current_y;
5333 move_it_by_lines (&it, 1);
5334 if (prev == it.current_y)
5335 break;
5337 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5338 /* Fix up the Y position to preserve, if it is inside the
5339 scroll margin at the window top. */
5340 if (window_scroll_pixel_based_preserve_y >= 0
5341 && window_scroll_pixel_based_preserve_y < this_scroll_margin)
5342 window_scroll_pixel_based_preserve_y = this_scroll_margin;
5345 else if (n < 0)
5347 ptrdiff_t charpos, bytepos;
5348 bool partial_p;
5350 /* Save our position, for the
5351 window_scroll_pixel_based_preserve_y case. */
5352 charpos = IT_CHARPOS (it);
5353 bytepos = IT_BYTEPOS (it);
5355 /* We moved the window start towards BEGV, so PT may be now
5356 in the scroll margin at the bottom. */
5357 move_it_to (&it, PT, -1,
5358 (it.last_visible_y - WINDOW_HEADER_LINE_HEIGHT (w)
5359 - partial_line_height (&it) - this_scroll_margin - 1),
5361 MOVE_TO_POS | MOVE_TO_Y);
5363 /* Save our position, in case it's correct. */
5364 charpos = IT_CHARPOS (it);
5365 bytepos = IT_BYTEPOS (it);
5367 /* If PT is in the screen line at the last fully visible line,
5368 move_it_to will stop at X = 0 in that line, because the
5369 required Y coordinate is reached there. See if we can get to
5370 PT without descending lower in Y, and if we can, it means we
5371 reached PT before the scroll margin. */
5372 if (charpos != PT)
5374 struct it it2;
5375 void *it_data;
5377 it2 = it;
5378 it_data = bidi_shelve_cache ();
5379 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5380 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5382 charpos = IT_CHARPOS (it);
5383 bytepos = IT_BYTEPOS (it);
5384 bidi_unshelve_cache (it_data, true);
5386 else
5388 it = it2;
5389 bidi_unshelve_cache (it_data, false);
5393 /* See if point is on a partially visible line at the end. */
5394 if (it.what == IT_EOB)
5395 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5396 else
5398 move_it_by_lines (&it, 1);
5399 partial_p = it.current_y > it.last_visible_y;
5402 if (charpos == PT && !partial_p
5403 && (NILP (Vscroll_preserve_screen_position)
5404 || EQ (Vscroll_preserve_screen_position, Qt)))
5405 /* We found PT before we found the display margin, so PT is ok. */
5407 else if (window_scroll_pixel_based_preserve_y >= 0)
5409 int goal_y = min (it.last_visible_y - this_scroll_margin - 1,
5410 window_scroll_pixel_based_preserve_y);
5412 /* Don't let the preserved screen Y coordinate put us inside
5413 any of the two margins. */
5414 if (goal_y < this_scroll_margin)
5415 goal_y = this_scroll_margin;
5416 SET_TEXT_POS_FROM_MARKER (start, w->start);
5417 start_display (&it, w, start);
5418 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5419 here because we called start_display again and did not
5420 alter it.current_y this time. */
5421 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5422 goal_y, -1, MOVE_TO_Y | MOVE_TO_X);
5423 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5425 else
5427 if (partial_p)
5428 /* The last line was only partially visible, so back up two
5429 lines to make sure we're on a fully visible line. */
5431 move_it_by_lines (&it, -2);
5432 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5434 else
5435 /* No, the position we saved is OK, so use it. */
5436 SET_PT_BOTH (charpos, bytepos);
5439 bidi_unshelve_cache (itdata, false);
5441 if (adjust_old_pointm)
5442 Fset_marker (w->old_pointm,
5443 ((w == XWINDOW (selected_window))
5444 ? make_number (BUF_PT (XBUFFER (w->contents)))
5445 : Fmarker_position (w->pointm)),
5446 w->contents);
5450 /* Implementation of window_scroll that works based on screen lines.
5451 See the comment of window_scroll for parameter descriptions. */
5453 static void
5454 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5456 struct window *w = XWINDOW (window);
5457 /* Fvertical_motion enters redisplay, which can trigger
5458 fontification, which in turn can modify buffer text (e.g., if the
5459 fontification functions replace escape sequences with faces, as
5460 in `grep-mode-font-lock-keywords'). So we use a marker to record
5461 the old point position, to prevent crashes in SET_PT_BOTH. */
5462 Lisp_Object opoint_marker = Fpoint_marker ();
5463 register ptrdiff_t pos, pos_byte;
5464 register int ht = window_internal_height (w);
5465 register Lisp_Object tem;
5466 bool lose;
5467 Lisp_Object bolp;
5468 ptrdiff_t startpos = marker_position (w->start);
5469 ptrdiff_t startbyte = marker_byte_position (w->start);
5470 Lisp_Object original_pos = Qnil;
5471 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5472 Fwindow_old_point (window)));
5474 /* If scrolling screen-fulls, compute the number of lines to
5475 scroll from the window's height. */
5476 if (whole)
5477 n *= max (1, ht - next_screen_context_lines);
5479 if (!NILP (Vscroll_preserve_screen_position))
5481 if (window_scroll_preserve_vpos <= 0
5482 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5483 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5485 struct position posit
5486 = *compute_motion (startpos, startbyte, 0, 0, false,
5487 PT, ht, 0, -1, w->hscroll, 0, w);
5489 window_scroll_preserve_vpos = posit.vpos;
5490 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5493 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5494 make_number (window_scroll_preserve_vpos));
5497 XSETFASTINT (tem, PT);
5498 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5500 if (NILP (tem))
5502 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5503 startpos = PT;
5504 startbyte = PT_BYTE;
5507 SET_PT_BOTH (startpos, startbyte);
5508 lose = n < 0 && PT == BEGV;
5509 Fvertical_motion (make_number (n), window, Qnil);
5510 pos = PT;
5511 pos_byte = PT_BYTE;
5512 bolp = Fbolp ();
5513 SET_PT_BOTH (marker_position (opoint_marker),
5514 marker_byte_position (opoint_marker));
5516 if (lose)
5518 if (noerror)
5519 return;
5520 else
5521 xsignal0 (Qbeginning_of_buffer);
5524 if (pos < ZV)
5526 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5528 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5529 w->start_at_line_beg = !NILP (bolp);
5530 wset_update_mode_line (w);
5531 /* Set force_start so that redisplay_window will run
5532 the window-scroll-functions. */
5533 w->force_start = true;
5535 if (!NILP (Vscroll_preserve_screen_position)
5536 && this_scroll_margin == 0
5537 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5539 SET_PT_BOTH (pos, pos_byte);
5540 Fvertical_motion (original_pos, window, Qnil);
5542 /* If we scrolled forward, put point enough lines down
5543 that it is outside the scroll margin. */
5544 else if (n > 0)
5546 int top_margin;
5548 if (this_scroll_margin > 0)
5550 SET_PT_BOTH (pos, pos_byte);
5551 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5552 top_margin = PT;
5554 else
5555 top_margin = pos;
5557 if (top_margin <= marker_position (opoint_marker))
5558 SET_PT_BOTH (marker_position (opoint_marker),
5559 marker_byte_position (opoint_marker));
5560 else if (!NILP (Vscroll_preserve_screen_position))
5562 int nlines = window_scroll_preserve_vpos;
5564 SET_PT_BOTH (pos, pos_byte);
5565 if (window_scroll_preserve_vpos < this_scroll_margin)
5566 nlines = this_scroll_margin;
5567 else if (window_scroll_preserve_vpos
5568 >= w->total_lines - this_scroll_margin)
5569 nlines = w->total_lines - this_scroll_margin - 1;
5570 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5571 make_number (nlines)), window, Qnil);
5573 else
5574 SET_PT (top_margin);
5576 else if (n < 0)
5578 int bottom_margin;
5580 /* If we scrolled backward, put point near the end of the window
5581 but not within the scroll margin. */
5582 SET_PT_BOTH (pos, pos_byte);
5583 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5584 Qnil);
5585 if (XFASTINT (tem) == ht - this_scroll_margin)
5586 bottom_margin = PT;
5587 else
5588 bottom_margin = PT + 1;
5590 if (bottom_margin > marker_position (opoint_marker))
5591 SET_PT_BOTH (marker_position (opoint_marker),
5592 marker_byte_position (opoint_marker));
5593 else
5595 if (!NILP (Vscroll_preserve_screen_position))
5597 int nlines = window_scroll_preserve_vpos;
5599 SET_PT_BOTH (pos, pos_byte);
5600 if (window_scroll_preserve_vpos < this_scroll_margin)
5601 nlines = this_scroll_margin;
5602 else if (window_scroll_preserve_vpos
5603 >= ht - this_scroll_margin)
5604 nlines = ht - this_scroll_margin - 1;
5605 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5606 make_number (nlines)), window, Qnil);
5608 else
5609 Fvertical_motion (make_number (-1), window, Qnil);
5613 else
5615 if (noerror)
5616 return;
5617 else
5618 xsignal0 (Qend_of_buffer);
5621 if (adjust_old_pointm)
5622 Fset_marker (w->old_pointm,
5623 ((w == XWINDOW (selected_window))
5624 ? make_number (BUF_PT (XBUFFER (w->contents)))
5625 : Fmarker_position (w->pointm)),
5626 w->contents);
5630 /* Scroll selected_window up or down. If N is nil, scroll a
5631 screen-full which is defined as the height of the window minus
5632 next_screen_context_lines. If N is the symbol `-', scroll.
5633 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5634 up. This is the guts of Fscroll_up and Fscroll_down. */
5636 static void
5637 scroll_command (Lisp_Object n, int direction)
5639 ptrdiff_t count = SPECPDL_INDEX ();
5641 eassert (eabs (direction) == 1);
5643 /* If selected window's buffer isn't current, make it current for
5644 the moment. But don't screw up if window_scroll gets an error. */
5645 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5647 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5648 Fset_buffer (XWINDOW (selected_window)->contents);
5651 if (NILP (n))
5652 window_scroll (selected_window, direction, true, false);
5653 else if (EQ (n, Qminus))
5654 window_scroll (selected_window, -direction, true, false);
5655 else
5657 n = Fprefix_numeric_value (n);
5658 window_scroll (selected_window, XINT (n) * direction, false, false);
5661 unbind_to (count, Qnil);
5664 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5665 doc: /* Scroll text of selected window upward ARG lines.
5666 If ARG is omitted or nil, scroll upward by a near full screen.
5667 A near full screen is `next-screen-context-lines' less than a full screen.
5668 Negative ARG means scroll downward.
5669 If ARG is the atom `-', scroll downward by nearly full screen.
5670 When calling from a program, supply as argument a number, nil, or `-'. */)
5671 (Lisp_Object arg)
5673 scroll_command (arg, 1);
5674 return Qnil;
5677 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5678 doc: /* Scroll text of selected window down ARG lines.
5679 If ARG is omitted or nil, scroll down by a near full screen.
5680 A near full screen is `next-screen-context-lines' less than a full screen.
5681 Negative ARG means scroll upward.
5682 If ARG is the atom `-', scroll upward by nearly full screen.
5683 When calling from a program, supply as argument a number, nil, or `-'. */)
5684 (Lisp_Object arg)
5686 scroll_command (arg, -1);
5687 return Qnil;
5690 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5691 doc: /* Return the other window for \"other window scroll\" commands.
5692 If `other-window-scroll-buffer' is non-nil, a window
5693 showing that buffer is used.
5694 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5695 specifies the window. This takes precedence over
5696 `other-window-scroll-buffer'. */)
5697 (void)
5699 Lisp_Object window;
5701 if (MINI_WINDOW_P (XWINDOW (selected_window))
5702 && !NILP (Vminibuf_scroll_window))
5703 window = Vminibuf_scroll_window;
5704 /* If buffer is specified and live, scroll that buffer. */
5705 else if (!NILP (Vother_window_scroll_buffer)
5706 && BUFFERP (Vother_window_scroll_buffer)
5707 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5709 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5710 if (NILP (window))
5711 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5713 else
5715 /* Nothing specified; look for a neighboring window on the same
5716 frame. */
5717 window = Fnext_window (selected_window, Qnil, Qnil);
5719 if (EQ (window, selected_window))
5720 /* That didn't get us anywhere; look for a window on another
5721 visible frame. */
5723 window = Fnext_window (window, Qnil, Qt);
5724 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5725 && ! EQ (window, selected_window));
5728 CHECK_LIVE_WINDOW (window);
5730 if (EQ (window, selected_window))
5731 error ("There is no other window");
5733 return window;
5736 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5737 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5738 A near full screen is `next-screen-context-lines' less than a full screen.
5739 The next window is the one below the current one; or the one at the top
5740 if the current one is at the bottom. Negative ARG means scroll downward.
5741 If ARG is the atom `-', scroll downward by nearly full screen.
5742 When calling from a program, supply as argument a number, nil, or `-'.
5744 If `other-window-scroll-buffer' is non-nil, scroll the window
5745 showing that buffer, popping the buffer up if necessary.
5746 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5747 specifies the window to scroll. This takes precedence over
5748 `other-window-scroll-buffer'. */)
5749 (Lisp_Object arg)
5751 Lisp_Object window;
5752 struct window *w;
5753 ptrdiff_t count = SPECPDL_INDEX ();
5755 window = Fother_window_for_scrolling ();
5756 w = XWINDOW (window);
5758 /* Don't screw up if window_scroll gets an error. */
5759 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5761 Fset_buffer (w->contents);
5762 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5763 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5765 if (NILP (arg))
5766 window_scroll (window, 1, true, true);
5767 else if (EQ (arg, Qminus))
5768 window_scroll (window, -1, true, true);
5769 else
5771 if (CONSP (arg))
5772 arg = XCAR (arg);
5773 CHECK_NUMBER (arg);
5774 window_scroll (window, XINT (arg), false, true);
5777 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5778 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5779 unbind_to (count, Qnil);
5781 return Qnil;
5784 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5785 doc: /* Scroll selected window display ARG columns left.
5786 Default for ARG is window width minus 2.
5787 Value is the total amount of leftward horizontal scrolling in
5788 effect after the change.
5789 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5790 lower bound for automatic scrolling, i.e. automatic scrolling
5791 will not scroll a window to a column less than the value returned
5792 by this function. This happens in an interactive call. */)
5793 (register Lisp_Object arg, Lisp_Object set_minimum)
5795 struct window *w = XWINDOW (selected_window);
5796 EMACS_INT requested_arg = (NILP (arg)
5797 ? window_body_width (w, 0) - 2
5798 : XINT (Fprefix_numeric_value (arg)));
5799 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5801 if (!NILP (set_minimum))
5802 w->min_hscroll = w->hscroll;
5804 w->suspend_auto_hscroll = true;
5806 return result;
5809 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5810 doc: /* Scroll selected window display ARG columns right.
5811 Default for ARG is window width minus 2.
5812 Value is the total amount of leftward horizontal scrolling in
5813 effect after the change.
5814 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5815 lower bound for automatic scrolling, i.e. automatic scrolling
5816 will not scroll a window to a column less than the value returned
5817 by this function. This happens in an interactive call. */)
5818 (register Lisp_Object arg, Lisp_Object set_minimum)
5820 struct window *w = XWINDOW (selected_window);
5821 EMACS_INT requested_arg = (NILP (arg)
5822 ? window_body_width (w, 0) - 2
5823 : XINT (Fprefix_numeric_value (arg)));
5824 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5826 if (!NILP (set_minimum))
5827 w->min_hscroll = w->hscroll;
5829 w->suspend_auto_hscroll = true;
5831 return result;
5834 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5835 doc: /* Return the window which was selected when entering the minibuffer.
5836 Returns nil, if selected window is not a minibuffer window. */)
5837 (void)
5839 if (minibuf_level > 0
5840 && MINI_WINDOW_P (XWINDOW (selected_window))
5841 && WINDOW_LIVE_P (minibuf_selected_window))
5842 return minibuf_selected_window;
5844 return Qnil;
5847 /* Value is the number of lines actually displayed in window W,
5848 as opposed to its height. */
5850 static int
5851 displayed_window_lines (struct window *w)
5853 struct it it;
5854 struct text_pos start;
5855 int height = window_box_height (w);
5856 struct buffer *old_buffer;
5857 int bottom_y;
5858 void *itdata = NULL;
5860 if (XBUFFER (w->contents) != current_buffer)
5862 old_buffer = current_buffer;
5863 set_buffer_internal (XBUFFER (w->contents));
5865 else
5866 old_buffer = NULL;
5868 /* In case W->start is out of the accessible range, do something
5869 reasonable. This happens in Info mode when Info-scroll-down
5870 calls (recenter -1) while W->start is 1. */
5871 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5873 itdata = bidi_shelve_cache ();
5874 start_display (&it, w, start);
5875 move_it_vertically (&it, height);
5876 bottom_y = line_bottom_y (&it);
5877 bidi_unshelve_cache (itdata, false);
5879 /* Add in empty lines at the bottom of the window. */
5880 if (bottom_y < height)
5882 int uy = FRAME_LINE_HEIGHT (it.f);
5883 it.vpos += (height - bottom_y + uy - 1) / uy;
5885 else if (bottom_y == height)
5886 it.vpos++;
5888 if (old_buffer)
5889 set_buffer_internal (old_buffer);
5891 return it.vpos;
5895 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5896 doc: /* Center point in selected window and maybe redisplay frame.
5897 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5898 relative to the selected window. If ARG is negative, it counts up from the
5899 bottom of the window. (ARG should be less than the height of the window.)
5901 If ARG is omitted or nil, then recenter with point on the middle line of
5902 the selected window; if the variable `recenter-redisplay' is non-nil,
5903 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5904 is set to `grow-only', this resets the tool-bar's height to the minimum
5905 height needed); if `recenter-redisplay' has the special value `tty',
5906 then only tty frames are redrawn.
5908 Just C-u as prefix means put point in the center of the window
5909 and redisplay normally--don't erase and redraw the frame. */)
5910 (register Lisp_Object arg)
5912 struct window *w = XWINDOW (selected_window);
5913 struct buffer *buf = XBUFFER (w->contents);
5914 bool center_p = false;
5915 ptrdiff_t charpos, bytepos;
5916 EMACS_INT iarg UNINIT;
5917 int this_scroll_margin;
5919 if (buf != current_buffer)
5920 error ("`recenter'ing a window that does not display current-buffer.");
5922 /* If redisplay is suppressed due to an error, try again. */
5923 buf->display_error_modiff = 0;
5925 if (NILP (arg))
5927 if (!NILP (Vrecenter_redisplay)
5928 && (!EQ (Vrecenter_redisplay, Qtty)
5929 || !NILP (Ftty_type (selected_frame))))
5931 ptrdiff_t i;
5933 /* Invalidate pixel data calculated for all compositions. */
5934 for (i = 0; i < n_compositions; i++)
5935 composition_table[i]->font = NULL;
5936 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5937 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5938 #endif
5939 Fredraw_frame (WINDOW_FRAME (w));
5940 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5943 center_p = true;
5945 else if (CONSP (arg)) /* Just C-u. */
5946 center_p = true;
5947 else
5949 arg = Fprefix_numeric_value (arg);
5950 CHECK_NUMBER (arg);
5951 iarg = XINT (arg);
5954 /* Do this after making BUF current
5955 in case scroll_margin is buffer-local. */
5956 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5958 /* Don't use redisplay code for initial frames, as the necessary
5959 data structures might not be set up yet then. */
5960 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5962 if (center_p)
5964 struct it it;
5965 struct text_pos pt;
5966 void *itdata = bidi_shelve_cache ();
5968 SET_TEXT_POS (pt, PT, PT_BYTE);
5969 start_display (&it, w, pt);
5970 move_it_vertically_backward (&it, window_box_height (w) / 2);
5971 charpos = IT_CHARPOS (it);
5972 bytepos = IT_BYTEPOS (it);
5973 bidi_unshelve_cache (itdata, false);
5975 else if (iarg < 0)
5977 struct it it;
5978 struct text_pos pt;
5979 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5980 int extra_line_spacing;
5981 int h = window_box_height (w);
5982 int ht = window_internal_height (w);
5983 void *itdata = bidi_shelve_cache ();
5985 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5986 ht - this_scroll_margin);
5988 SET_TEXT_POS (pt, PT, PT_BYTE);
5989 start_display (&it, w, pt);
5991 /* Be sure we have the exact height of the full line containing PT. */
5992 move_it_by_lines (&it, 0);
5994 /* The amount of pixels we have to move back is the window
5995 height minus what's displayed in the line containing PT,
5996 and the lines below. */
5997 it.current_y = 0;
5998 it.vpos = 0;
5999 move_it_by_lines (&it, nlines);
6001 if (it.vpos == nlines)
6002 h -= it.current_y;
6003 else
6005 /* Last line has no newline. */
6006 h -= line_bottom_y (&it);
6007 it.vpos++;
6010 /* Don't reserve space for extra line spacing of last line. */
6011 extra_line_spacing = it.max_extra_line_spacing;
6013 /* If we can't move down NLINES lines because we hit
6014 the end of the buffer, count in some empty lines. */
6015 if (it.vpos < nlines)
6017 nlines -= it.vpos;
6018 extra_line_spacing = it.extra_line_spacing;
6019 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
6021 if (h <= 0)
6023 bidi_unshelve_cache (itdata, false);
6024 return Qnil;
6027 /* Now find the new top line (starting position) of the window. */
6028 start_display (&it, w, pt);
6029 it.current_y = 0;
6030 move_it_vertically_backward (&it, h);
6032 /* If extra line spacing is present, we may move too far
6033 back. This causes the last line to be only partially
6034 visible (which triggers redisplay to recenter that line
6035 in the middle), so move forward.
6036 But ignore extra line spacing on last line, as it is not
6037 considered to be part of the visible height of the line.
6039 h += extra_line_spacing;
6040 while (-it.current_y > h)
6041 move_it_by_lines (&it, 1);
6043 charpos = IT_CHARPOS (it);
6044 bytepos = IT_BYTEPOS (it);
6046 bidi_unshelve_cache (itdata, false);
6048 else
6050 struct it it;
6051 struct text_pos pt;
6052 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
6053 int ht = window_internal_height (w);
6054 void *itdata = bidi_shelve_cache ();
6056 nlines = clip_to_bounds (this_scroll_margin, nlines,
6057 ht - this_scroll_margin - 1);
6059 SET_TEXT_POS (pt, PT, PT_BYTE);
6060 start_display (&it, w, pt);
6062 /* Move to the beginning of screen line containing PT. */
6063 move_it_by_lines (&it, 0);
6065 /* Move back to find the point which is ARG screen lines above PT. */
6066 if (nlines > 0)
6068 it.current_y = 0;
6069 it.vpos = 0;
6070 move_it_by_lines (&it, -nlines);
6073 charpos = IT_CHARPOS (it);
6074 bytepos = IT_BYTEPOS (it);
6076 bidi_unshelve_cache (itdata, false);
6079 else
6081 struct position pos;
6082 int ht = window_internal_height (w);
6084 if (center_p)
6085 iarg = ht / 2;
6086 else if (iarg < 0)
6087 iarg += ht;
6089 /* Don't let it get into the margin at either top or bottom. */
6090 iarg = clip_to_bounds (this_scroll_margin, iarg,
6091 ht - this_scroll_margin - 1);
6093 pos = *vmotion (PT, PT_BYTE, - iarg, w);
6094 charpos = pos.bufpos;
6095 bytepos = pos.bytepos;
6098 /* Set the new window start. */
6099 set_marker_both (w->start, w->contents, charpos, bytepos);
6100 w->window_end_valid = false;
6102 w->optional_new_start = true;
6104 w->start_at_line_beg = (bytepos == BEGV_BYTE
6105 || FETCH_BYTE (bytepos - 1) == '\n');
6107 wset_redisplay (w);
6109 return Qnil;
6112 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
6113 0, 2, 0,
6114 doc: /* Return the width in columns of the text display area of WINDOW.
6115 WINDOW must be a live window and defaults to the selected one.
6117 The returned width does not include dividers, scrollbars, margins,
6118 fringes, nor any partial-width columns at the right of the text
6119 area.
6121 Optional argument PIXELWISE non-nil, means to return the width in
6122 pixels. */)
6123 (Lisp_Object window, Lisp_Object pixelwise)
6125 struct window *w = decode_live_window (window);
6127 if (NILP (pixelwise))
6128 return make_number (window_box_width (w, TEXT_AREA)
6129 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
6130 else
6131 return make_number (window_box_width (w, TEXT_AREA));
6134 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
6135 0, 2, 0,
6136 doc: /* Return the height in lines of the text display area of WINDOW.
6137 WINDOW must be a live window and defaults to the selected one.
6139 The returned height does not include dividers, the mode line, any header
6140 line, nor any partial-height lines at the bottom of the text area.
6142 Optional argument PIXELWISE non-nil, means to return the height in
6143 pixels. */)
6144 (Lisp_Object window, Lisp_Object pixelwise)
6146 struct window *w = decode_live_window (window);
6148 if (NILP (pixelwise))
6149 return make_number (window_box_height (w)
6150 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
6151 else
6152 return make_number (window_box_height (w));
6155 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
6156 1, 1, "P",
6157 doc: /* Position point relative to window.
6158 ARG nil means position point at center of window.
6159 Else, ARG specifies vertical position within the window;
6160 zero means top of window, negative means relative to bottom
6161 of window, -1 meaning the last fully visible display line
6162 of the window.
6164 Value is the screen line of the window point moved to, counting
6165 from the top of the window. */)
6166 (Lisp_Object arg)
6168 struct window *w = XWINDOW (selected_window);
6169 int lines, start;
6170 Lisp_Object window;
6171 #if false
6172 int this_scroll_margin;
6173 #endif
6175 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
6176 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
6177 when passed below to set_marker_both. */
6178 error ("move-to-window-line called from unrelated buffer");
6180 window = selected_window;
6181 start = marker_position (w->start);
6182 if (start < BEGV || start > ZV)
6184 int height = window_internal_height (w);
6185 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
6186 set_marker_both (w->start, w->contents, PT, PT_BYTE);
6187 w->start_at_line_beg = !NILP (Fbolp ());
6188 w->force_start = true;
6190 else
6191 Fgoto_char (w->start);
6193 lines = displayed_window_lines (w);
6195 if (NILP (arg))
6196 XSETFASTINT (arg, lines / 2);
6197 else
6199 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
6201 if (iarg < 0)
6202 iarg = iarg + lines;
6204 #if false /* This code would prevent move-to-window-line from moving point
6205 to a place inside the scroll margins (which would cause the
6206 next redisplay to scroll). I wrote this code, but then concluded
6207 it is probably better not to install it. However, it is here
6208 inside #if false so as not to lose it. -- rms. */
6210 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
6212 /* Don't let it get into the margin at either top or bottom. */
6213 iarg = max (iarg, this_scroll_margin);
6214 iarg = min (iarg, lines - this_scroll_margin - 1);
6215 #endif
6217 arg = make_number (iarg);
6220 /* Skip past a partially visible first line. */
6221 if (w->vscroll)
6222 XSETINT (arg, XINT (arg) + 1);
6224 return Fvertical_motion (arg, window, Qnil);
6229 /***********************************************************************
6230 Window Configuration
6231 ***********************************************************************/
6233 struct save_window_data
6235 struct vectorlike_header header;
6236 Lisp_Object selected_frame;
6237 Lisp_Object current_window;
6238 Lisp_Object f_current_buffer;
6239 Lisp_Object minibuf_scroll_window;
6240 Lisp_Object minibuf_selected_window;
6241 Lisp_Object root_window;
6242 Lisp_Object focus_frame;
6243 /* A vector, each of whose elements is a struct saved_window
6244 for one window. */
6245 Lisp_Object saved_windows;
6247 /* All fields above are traced by the GC.
6248 From `frame-cols' down, the fields are ignored by the GC. */
6249 /* We should be able to do without the following two. */
6250 int frame_cols, frame_lines;
6251 /* These two should get eventually replaced by their pixel
6252 counterparts. */
6253 int frame_menu_bar_lines, frame_tool_bar_lines;
6254 int frame_text_width, frame_text_height;
6255 /* These are currently unused. We need them as soon as we convert
6256 to pixels. */
6257 int frame_menu_bar_height, frame_tool_bar_height;
6260 /* This is saved as a Lisp_Vector. */
6261 struct saved_window
6263 struct vectorlike_header header;
6265 Lisp_Object window, buffer, start, pointm, old_pointm;
6266 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6267 Lisp_Object pixel_height_before_size_change, pixel_width_before_size_change;
6268 Lisp_Object left_col, top_line, total_cols, total_lines;
6269 Lisp_Object normal_cols, normal_lines;
6270 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6271 Lisp_Object parent, prev;
6272 Lisp_Object start_at_line_beg;
6273 Lisp_Object display_table;
6274 Lisp_Object left_margin_cols, right_margin_cols;
6275 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6276 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6277 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6278 Lisp_Object combination_limit, window_parameters;
6281 #define SAVED_WINDOW_N(swv,n) \
6282 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6284 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6285 doc: /* Return t if OBJECT is a window-configuration object. */)
6286 (Lisp_Object object)
6288 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6291 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6292 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6293 (Lisp_Object config)
6295 register struct save_window_data *data;
6296 struct Lisp_Vector *saved_windows;
6298 CHECK_WINDOW_CONFIGURATION (config);
6300 data = (struct save_window_data *) XVECTOR (config);
6301 saved_windows = XVECTOR (data->saved_windows);
6302 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6305 DEFUN ("set-window-configuration", Fset_window_configuration,
6306 Sset_window_configuration, 1, 1, 0,
6307 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6308 CONFIGURATION must be a value previously returned
6309 by `current-window-configuration' (which see).
6310 If CONFIGURATION was made from a frame that is now deleted,
6311 only frame-independent values can be restored. In this case,
6312 the return value is nil. Otherwise the value is t. */)
6313 (Lisp_Object configuration)
6315 register struct save_window_data *data;
6316 struct Lisp_Vector *saved_windows;
6317 Lisp_Object new_current_buffer;
6318 Lisp_Object frame;
6319 struct frame *f;
6320 ptrdiff_t old_point = -1;
6321 USE_SAFE_ALLOCA;
6323 CHECK_WINDOW_CONFIGURATION (configuration);
6325 data = (struct save_window_data *) XVECTOR (configuration);
6326 saved_windows = XVECTOR (data->saved_windows);
6328 new_current_buffer = data->f_current_buffer;
6329 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6330 new_current_buffer = Qnil;
6331 else
6333 if (XBUFFER (new_current_buffer) == current_buffer)
6334 /* The code further down "preserves point" by saving here PT in
6335 old_point and then setting it later back into PT. When the
6336 current-selected-window and the final-selected-window both show
6337 the current buffer, this suffers from the problem that the
6338 current PT is the window-point of the current-selected-window,
6339 while the final PT is the point of the final-selected-window, so
6340 this copy from one PT to the other would end up moving the
6341 window-point of the final-selected-window to the window-point of
6342 the current-selected-window. So we have to be careful which
6343 point of the current-buffer we copy into old_point. */
6344 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6345 && WINDOWP (selected_window)
6346 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6347 && !EQ (selected_window, data->current_window))
6348 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6349 else
6350 old_point = PT;
6351 else
6352 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6353 point in new_current_buffer as of the last time this buffer was
6354 used. This can be non-deterministic since it can be changed by
6355 things like jit-lock by mere temporary selection of some random
6356 window that happens to show this buffer.
6357 So if possible we want this arbitrary choice of "which point" to
6358 be the one from the to-be-selected-window so as to prevent this
6359 window's cursor from being copied from another window. */
6360 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6361 /* If current_window = selected_window, its point is in BUF_PT. */
6362 && !EQ (selected_window, data->current_window))
6363 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6364 else
6365 old_point = BUF_PT (XBUFFER (new_current_buffer));
6368 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6369 f = XFRAME (frame);
6371 /* If f is a dead frame, don't bother rebuilding its window tree.
6372 However, there is other stuff we should still try to do below. */
6373 if (FRAME_LIVE_P (f))
6375 Lisp_Object window;
6376 Lisp_Object dead_windows = Qnil;
6377 Lisp_Object tem, par, pers;
6378 struct window *w;
6379 struct saved_window *p;
6380 struct window *root_window;
6381 struct window **leaf_windows;
6382 ptrdiff_t i, k, n_leaf_windows;
6383 /* Records whether a window has been added or removed wrt the
6384 original configuration. */
6385 bool window_changed = false;
6386 /* Records whether a window has changed its buffer wrt the
6387 original configuration. */
6388 bool buffer_changed = false;
6390 /* Don't do this within the main loop below: This may call Lisp
6391 code and is thus potentially unsafe while input is blocked. */
6392 for (k = 0; k < saved_windows->header.size; k++)
6394 p = SAVED_WINDOW_N (saved_windows, k);
6395 window = p->window;
6396 w = XWINDOW (window);
6398 if (NILP (w->contents))
6399 /* A dead window that will be resurrected, the window
6400 configuration will change. */
6401 window_changed = true;
6403 if (BUFFERP (w->contents)
6404 && !EQ (w->contents, p->buffer)
6405 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6406 /* If a window we restore gets another buffer, record the
6407 window's old buffer. */
6408 call1 (Qrecord_window_buffer, window);
6411 /* Disallow x_set_window_size, temporarily. */
6412 f->can_x_set_window_size = false;
6413 /* The mouse highlighting code could get screwed up
6414 if it runs during this. */
6415 block_input ();
6417 /* "Swap out" point from the selected window's buffer
6418 into the window itself. (Normally the pointm of the selected
6419 window holds garbage.) We do this now, before
6420 restoring the window contents, and prevent it from
6421 being done later on when we select a new window. */
6422 if (! NILP (XWINDOW (selected_window)->contents))
6424 w = XWINDOW (selected_window);
6425 set_marker_both (w->pointm,
6426 w->contents,
6427 BUF_PT (XBUFFER (w->contents)),
6428 BUF_PT_BYTE (XBUFFER (w->contents)));
6431 fset_redisplay (f);
6433 /* Problem: Freeing all matrices and later allocating them again
6434 is a serious redisplay flickering problem. What we would
6435 really like to do is to free only those matrices not reused
6436 below. */
6437 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6438 ptrdiff_t nwindows = count_windows (root_window);
6439 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6440 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6442 /* Kludge Alert!
6443 Mark all windows now on frame as "deleted".
6444 Restoring the new configuration "undeletes" any that are in it.
6446 Save their current buffers in their height fields, since we may
6447 need it later, if a buffer saved in the configuration is now
6448 dead. */
6449 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6451 for (k = 0; k < saved_windows->header.size; k++)
6453 p = SAVED_WINDOW_N (saved_windows, k);
6454 window = p->window;
6455 w = XWINDOW (window);
6456 wset_next (w, Qnil);
6458 if (!NILP (p->parent))
6459 wset_parent
6460 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6461 else
6462 wset_parent (w, Qnil);
6464 if (!NILP (p->prev))
6466 wset_prev
6467 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6468 wset_next (XWINDOW (w->prev), p->window);
6470 else
6472 wset_prev (w, Qnil);
6473 if (!NILP (w->parent))
6474 wset_combination (XWINDOW (w->parent),
6475 (XINT (p->total_cols)
6476 != XWINDOW (w->parent)->total_cols),
6477 p->window);
6480 /* If we squirreled away the buffer, restore it now. */
6481 if (BUFFERP (w->combination_limit))
6482 wset_buffer (w, w->combination_limit);
6483 w->pixel_left = XFASTINT (p->pixel_left);
6484 w->pixel_top = XFASTINT (p->pixel_top);
6485 w->pixel_width = XFASTINT (p->pixel_width);
6486 w->pixel_height = XFASTINT (p->pixel_height);
6487 w->pixel_width_before_size_change
6488 = XFASTINT (p->pixel_width_before_size_change);
6489 w->pixel_height_before_size_change
6490 = XFASTINT (p->pixel_height_before_size_change);
6491 w->left_col = XFASTINT (p->left_col);
6492 w->top_line = XFASTINT (p->top_line);
6493 w->total_cols = XFASTINT (p->total_cols);
6494 w->total_lines = XFASTINT (p->total_lines);
6495 wset_normal_cols (w, p->normal_cols);
6496 wset_normal_lines (w, p->normal_lines);
6497 w->hscroll = XFASTINT (p->hscroll);
6498 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6499 w->min_hscroll = XFASTINT (p->min_hscroll);
6500 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6501 wset_display_table (w, p->display_table);
6502 w->left_margin_cols = XINT (p->left_margin_cols);
6503 w->right_margin_cols = XINT (p->right_margin_cols);
6504 w->left_fringe_width = XINT (p->left_fringe_width);
6505 w->right_fringe_width = XINT (p->right_fringe_width);
6506 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6507 w->scroll_bar_width = XINT (p->scroll_bar_width);
6508 w->scroll_bar_height = XINT (p->scroll_bar_height);
6509 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6510 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6511 wset_dedicated (w, p->dedicated);
6512 wset_combination_limit (w, p->combination_limit);
6513 /* Restore any window parameters that have been saved.
6514 Parameters that have not been saved are left alone. */
6515 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6517 pers = XCAR (tem);
6518 if (CONSP (pers))
6520 if (NILP (XCDR (pers)))
6522 par = Fassq (XCAR (pers), w->window_parameters);
6523 if (CONSP (par) && !NILP (XCDR (par)))
6524 /* Reset a parameter to nil if and only if it
6525 has a non-nil association. Don't make new
6526 associations. */
6527 Fsetcdr (par, Qnil);
6529 else
6530 /* Always restore a non-nil value. */
6531 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6535 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6536 /* If saved buffer is alive, install it. */
6538 if (!EQ (w->contents, p->buffer))
6539 /* Record buffer configuration change. */
6540 buffer_changed = true;
6541 wset_buffer (w, p->buffer);
6542 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6543 set_marker_restricted (w->start, p->start, w->contents);
6544 set_marker_restricted (w->pointm, p->pointm, w->contents);
6545 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6546 /* As documented in Fcurrent_window_configuration, don't
6547 restore the location of point in the buffer which was
6548 current when the window configuration was recorded. */
6549 if (!EQ (p->buffer, new_current_buffer)
6550 && XBUFFER (p->buffer) == current_buffer)
6551 Fgoto_char (w->pointm);
6553 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6554 /* Keep window's old buffer; make sure the markers are real. */
6556 /* Set window markers at start of visible range. */
6557 if (XMARKER (w->start)->buffer == 0)
6558 set_marker_restricted_both (w->start, w->contents, 0, 0);
6559 if (XMARKER (w->pointm)->buffer == 0)
6560 set_marker_restricted_both
6561 (w->pointm, w->contents,
6562 BUF_PT (XBUFFER (w->contents)),
6563 BUF_PT_BYTE (XBUFFER (w->contents)));
6564 if (XMARKER (w->old_pointm)->buffer == 0)
6565 set_marker_restricted_both
6566 (w->old_pointm, w->contents,
6567 BUF_PT (XBUFFER (w->contents)),
6568 BUF_PT_BYTE (XBUFFER (w->contents)));
6569 w->start_at_line_beg = true;
6571 else if (!NILP (w->start))
6572 /* Leaf window has no live buffer, get one. */
6574 /* Record buffer configuration change. */
6575 buffer_changed = true;
6576 /* Get the buffer via other_buffer_safely in order to
6577 avoid showing an unimportant buffer and, if necessary, to
6578 recreate *scratch* in the course (part of Juanma's bs-show
6579 scenario from March 2011). */
6580 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6581 /* This will set the markers to beginning of visible
6582 range. */
6583 set_marker_restricted_both (w->start, w->contents, 0, 0);
6584 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6585 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6586 w->start_at_line_beg = true;
6587 if (!NILP (w->dedicated))
6588 /* Record this window as dead. */
6589 dead_windows = Fcons (window, dead_windows);
6590 /* Make sure window is no more dedicated. */
6591 wset_dedicated (w, Qnil);
6595 fset_root_window (f, data->root_window);
6596 /* Arrange *not* to restore point in the buffer that was
6597 current when the window configuration was saved. */
6598 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6599 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6600 make_number (old_point),
6601 XWINDOW (data->current_window)->contents);
6603 /* In the following call to `select-window', prevent "swapping out
6604 point" in the old selected window using the buffer that has
6605 been restored into it. We already swapped out that point from
6606 that window's old buffer.
6608 Do not record the buffer here. We do that in a separate call
6609 to select_window below. See also Bug#16207. */
6610 select_window (data->current_window, Qt, true);
6611 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6612 last_selected_window)
6613 = selected_window;
6615 if (NILP (data->focus_frame)
6616 || (FRAMEP (data->focus_frame)
6617 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6618 Fredirect_frame_focus (frame, data->focus_frame);
6620 /* Now, free glyph matrices in windows that were not reused. */
6621 for (i = 0; i < n_leaf_windows; i++)
6622 if (NILP (leaf_windows[i]->contents))
6624 free_window_matrices (leaf_windows[i]);
6625 window_changed = true;
6628 /* Allow x_set_window_size again and apply frame size changes if
6629 needed. */
6630 f->can_x_set_window_size = true;
6631 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6633 adjust_frame_glyphs (f);
6634 unblock_input ();
6636 /* Scan dead buffer windows. */
6637 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6639 window = XCAR (dead_windows);
6640 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6641 delete_deletable_window (window);
6644 /* Record the selected window's buffer here. The window should
6645 already be the selected one from the call above. */
6646 if (WINDOW_LIVE_P (data->current_window))
6647 select_window (data->current_window, Qnil, false);
6649 /* Fselect_window will have made f the selected frame, so we
6650 reselect the proper frame here. Fhandle_switch_frame will change the
6651 selected window too, but that doesn't make the call to
6652 Fselect_window above totally superfluous; it still sets f's
6653 selected window. */
6654 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6655 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6657 if (window_changed)
6658 /* At least one window has been added or removed. Run
6659 `window-configuration-change-hook' and make sure
6660 `window-size-change-functions' get run later.
6662 We have to do this in order to capture the following
6663 scenario: Suppose our frame contains two live windows W1 and
6664 W2 and ‘set-window-configuration’ replaces them by two
6665 windows W3 and W4 that were dead the last time
6666 run_window_size_change_functions was run. If W3 and W4 have
6667 the same values for their old and new pixel sizes but these
6668 values differ from those of W1 and W2, the sizes of our
6669 frame's two live windows changed but window_size_changed has
6670 no means to detect that fact.
6672 Obviously, this will get us false positives, for example,
6673 when we restore the original configuration with W1 and W2
6674 before run_window_size_change_functions gets called. */
6676 run_window_configuration_change_hook (f);
6677 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = true;
6679 else if (buffer_changed)
6680 /* At least one window has changed its buffer. Run
6681 `window-configuration-change-hook' only. */
6682 run_window_configuration_change_hook (f);
6685 if (!NILP (new_current_buffer))
6687 Fset_buffer (new_current_buffer);
6688 /* If the new current buffer doesn't appear in the selected
6689 window, go to its old point (see bug#12208). */
6690 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6691 Fgoto_char (make_number (old_point));
6694 Vminibuf_scroll_window = data->minibuf_scroll_window;
6695 minibuf_selected_window = data->minibuf_selected_window;
6697 SAFE_FREE ();
6698 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6702 void
6703 restore_window_configuration (Lisp_Object configuration)
6705 Fset_window_configuration (configuration);
6709 /* If WINDOW is an internal window, recursively delete all child windows
6710 reachable via the next and contents slots of WINDOW. Otherwise setup
6711 WINDOW to not show any buffer. */
6713 void
6714 delete_all_child_windows (Lisp_Object window)
6716 register struct window *w;
6718 w = XWINDOW (window);
6720 if (!NILP (w->next))
6721 /* Delete WINDOW's siblings (we traverse postorderly). */
6722 delete_all_child_windows (w->next);
6724 if (WINDOWP (w->contents))
6726 delete_all_child_windows (w->contents);
6727 wset_combination (w, false, Qnil);
6729 else if (BUFFERP (w->contents))
6731 unshow_buffer (w);
6732 unchain_marker (XMARKER (w->pointm));
6733 unchain_marker (XMARKER (w->old_pointm));
6734 unchain_marker (XMARKER (w->start));
6735 /* Since combination limit makes sense for an internal windows
6736 only, we use this slot to save the buffer for the sake of
6737 possible resurrection in Fset_window_configuration. */
6738 wset_combination_limit (w, w->contents);
6739 wset_buffer (w, Qnil);
6742 Vwindow_list = Qnil;
6745 static ptrdiff_t
6746 count_windows (struct window *window)
6748 ptrdiff_t count = 1;
6749 if (!NILP (window->next))
6750 count += count_windows (XWINDOW (window->next));
6751 if (WINDOWP (window->contents))
6752 count += count_windows (XWINDOW (window->contents));
6753 return count;
6757 /* Fill vector FLAT with leaf windows under W, starting at index I.
6758 Value is last index + 1. */
6759 static ptrdiff_t
6760 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6762 while (w)
6764 if (WINDOWP (w->contents))
6765 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6766 else
6767 flat[i++] = w;
6769 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6772 return i;
6776 /* Return a pointer to the glyph W's physical cursor is on. Value is
6777 null if W's current matrix is invalid, so that no meaningful glyph
6778 can be returned. */
6779 struct glyph *
6780 get_phys_cursor_glyph (struct window *w)
6782 struct glyph_row *row;
6783 struct glyph *glyph;
6784 int hpos = w->phys_cursor.hpos;
6786 if (!(w->phys_cursor.vpos >= 0
6787 && w->phys_cursor.vpos < w->current_matrix->nrows))
6788 return NULL;
6790 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6791 if (!row->enabled_p)
6792 return NULL;
6794 if (w->hscroll)
6796 /* When the window is hscrolled, cursor hpos can legitimately be
6797 out of bounds, but we draw the cursor at the corresponding
6798 window margin in that case. */
6799 if (!row->reversed_p && hpos < 0)
6800 hpos = 0;
6801 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6802 hpos = row->used[TEXT_AREA] - 1;
6805 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6806 glyph = row->glyphs[TEXT_AREA] + hpos;
6807 else
6808 glyph = NULL;
6810 return glyph;
6814 static ptrdiff_t
6815 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6817 struct saved_window *p;
6818 struct window *w;
6819 Lisp_Object tem, pers, par;
6821 for (; !NILP (window); window = w->next)
6823 p = SAVED_WINDOW_N (vector, i);
6824 w = XWINDOW (window);
6826 wset_temslot (w, make_number (i)); i++;
6827 p->window = window;
6828 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6829 p->pixel_left = make_number (w->pixel_left);
6830 p->pixel_top = make_number (w->pixel_top);
6831 p->pixel_width = make_number (w->pixel_width);
6832 p->pixel_height = make_number (w->pixel_height);
6833 p->pixel_width_before_size_change
6834 = make_number (w->pixel_width_before_size_change);
6835 p->pixel_height_before_size_change
6836 = make_number (w->pixel_height_before_size_change);
6837 p->left_col = make_number (w->left_col);
6838 p->top_line = make_number (w->top_line);
6839 p->total_cols = make_number (w->total_cols);
6840 p->total_lines = make_number (w->total_lines);
6841 p->normal_cols = w->normal_cols;
6842 p->normal_lines = w->normal_lines;
6843 XSETFASTINT (p->hscroll, w->hscroll);
6844 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6845 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6846 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6847 p->display_table = w->display_table;
6848 p->left_margin_cols = make_number (w->left_margin_cols);
6849 p->right_margin_cols = make_number (w->right_margin_cols);
6850 p->left_fringe_width = make_number (w->left_fringe_width);
6851 p->right_fringe_width = make_number (w->right_fringe_width);
6852 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6853 p->scroll_bar_width = make_number (w->scroll_bar_width);
6854 p->scroll_bar_height = make_number (w->scroll_bar_height);
6855 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6856 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6857 p->dedicated = w->dedicated;
6858 p->combination_limit = w->combination_limit;
6859 p->window_parameters = Qnil;
6861 if (!NILP (Vwindow_persistent_parameters))
6863 /* Run cycle detection on Vwindow_persistent_parameters. */
6864 Lisp_Object tortoise, hare;
6866 hare = tortoise = Vwindow_persistent_parameters;
6867 while (CONSP (hare))
6869 hare = XCDR (hare);
6870 if (!CONSP (hare))
6871 break;
6873 hare = XCDR (hare);
6874 tortoise = XCDR (tortoise);
6876 if (EQ (hare, tortoise))
6877 /* Reset Vwindow_persistent_parameters to Qnil. */
6879 Vwindow_persistent_parameters = Qnil;
6880 break;
6884 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6885 tem = XCDR (tem))
6887 pers = XCAR (tem);
6888 /* Save values for persistent window parameters. */
6889 if (CONSP (pers) && !NILP (XCDR (pers)))
6891 par = Fassq (XCAR (pers), w->window_parameters);
6892 if (NILP (par))
6893 /* If the window has no value for the parameter,
6894 make one. */
6895 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6896 p->window_parameters);
6897 else
6898 /* If the window has a value for the parameter,
6899 save it. */
6900 p->window_parameters = Fcons (Fcons (XCAR (par),
6901 XCDR (par)),
6902 p->window_parameters);
6907 if (BUFFERP (w->contents))
6909 /* Save w's value of point in the window configuration. If w
6910 is the selected window, then get the value of point from
6911 the buffer; pointm is garbage in the selected window. */
6912 if (EQ (window, selected_window))
6913 p->pointm = build_marker (XBUFFER (w->contents),
6914 BUF_PT (XBUFFER (w->contents)),
6915 BUF_PT_BYTE (XBUFFER (w->contents)));
6916 else
6917 p->pointm = Fcopy_marker (w->pointm, Qnil);
6918 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6919 XMARKER (p->pointm)->insertion_type
6920 = !NILP (buffer_local_value /* Don't signal error if void. */
6921 (Qwindow_point_insertion_type, w->contents));
6922 XMARKER (p->old_pointm)->insertion_type
6923 = !NILP (buffer_local_value /* Don't signal error if void. */
6924 (Qwindow_point_insertion_type, w->contents));
6926 p->start = Fcopy_marker (w->start, Qnil);
6927 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6929 else
6931 p->pointm = Qnil;
6932 p->old_pointm = Qnil;
6933 p->start = Qnil;
6934 p->start_at_line_beg = Qnil;
6937 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6938 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6940 if (WINDOWP (w->contents))
6941 i = save_window_save (w->contents, vector, i);
6944 return i;
6947 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6948 Scurrent_window_configuration, 0, 1, 0,
6949 doc: /* Return an object representing the current window configuration of FRAME.
6950 If FRAME is nil or omitted, use the selected frame.
6951 This describes the number of windows, their sizes and current buffers,
6952 and for each displayed buffer, where display starts, and the position of
6953 point. An exception is made for point in the current buffer:
6954 its value is -not- saved.
6955 This also records the currently selected frame, and FRAME's focus
6956 redirection (see `redirect-frame-focus'). The variable
6957 `window-persistent-parameters' specifies which window parameters are
6958 saved by this function. */)
6959 (Lisp_Object frame)
6961 Lisp_Object tem;
6962 ptrdiff_t i, n_windows;
6963 struct save_window_data *data;
6964 struct frame *f = decode_live_frame (frame);
6966 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6967 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6968 PVEC_WINDOW_CONFIGURATION);
6970 data->frame_cols = FRAME_COLS (f);
6971 data->frame_lines = FRAME_LINES (f);
6972 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6973 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6974 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6975 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6976 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6977 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6978 data->selected_frame = selected_frame;
6979 data->current_window = FRAME_SELECTED_WINDOW (f);
6980 XSETBUFFER (data->f_current_buffer, current_buffer);
6981 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6982 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6983 data->root_window = FRAME_ROOT_WINDOW (f);
6984 data->focus_frame = FRAME_FOCUS_FRAME (f);
6985 tem = make_uninit_vector (n_windows);
6986 data->saved_windows = tem;
6987 for (i = 0; i < n_windows; i++)
6988 ASET (tem, i,
6989 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6990 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6991 XSETWINDOW_CONFIGURATION (tem, data);
6992 return (tem);
6995 /* Called after W's margins, fringes or scroll bars was adjusted. */
6997 static void
6998 apply_window_adjustment (struct window *w)
7000 eassert (w);
7001 clear_glyph_matrix (w->current_matrix);
7002 w->window_end_valid = false;
7003 wset_redisplay (w);
7004 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
7008 /***********************************************************************
7009 Marginal Areas
7010 ***********************************************************************/
7012 static int
7013 extract_dimension (Lisp_Object dimension)
7015 if (NILP (dimension))
7016 return -1;
7017 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
7018 return XINT (dimension);
7021 static struct window *
7022 set_window_margins (struct window *w, Lisp_Object left_width,
7023 Lisp_Object right_width)
7025 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
7026 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
7027 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
7029 if (w->left_margin_cols != left || w->right_margin_cols != right)
7031 /* Don't change anything if new margins won't fit. */
7032 if ((WINDOW_PIXEL_WIDTH (w)
7033 - WINDOW_FRINGES_WIDTH (w)
7034 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
7035 - (left + right) * unit)
7036 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
7038 w->left_margin_cols = left;
7039 w->right_margin_cols = right;
7041 return w;
7043 else
7044 return NULL;
7046 else
7047 return NULL;
7050 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
7051 2, 3, 0,
7052 doc: /* Set width of marginal areas of window WINDOW.
7053 WINDOW must be a live window and defaults to the selected one.
7055 Second arg LEFT-WIDTH specifies the number of character cells to
7056 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
7057 does the same for the right marginal area. A nil width parameter
7058 means no margin.
7060 Return t if any margin was actually changed and nil otherwise. */)
7061 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
7063 struct window *w = set_window_margins (decode_live_window (window),
7064 left_width, right_width);
7065 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7069 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
7070 0, 1, 0,
7071 doc: /* Get width of marginal areas of window WINDOW.
7072 WINDOW must be a live window and defaults to the selected one.
7074 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
7075 If a marginal area does not exist, its width will be returned
7076 as nil. */)
7077 (Lisp_Object window)
7079 struct window *w = decode_live_window (window);
7080 return Fcons (w->left_margin_cols
7081 ? make_number (w->left_margin_cols) : Qnil,
7082 w->right_margin_cols
7083 ? make_number (w->right_margin_cols) : Qnil);
7088 /***********************************************************************
7089 Fringes
7090 ***********************************************************************/
7092 static struct window *
7093 set_window_fringes (struct window *w, Lisp_Object left_width,
7094 Lisp_Object right_width, Lisp_Object outside_margins)
7096 bool outside = !NILP (outside_margins);
7097 int left = extract_dimension (left_width);
7098 int right = extract_dimension (right_width);
7100 /* Do nothing on a tty or if nothing to actually change. */
7101 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
7102 && (w->left_fringe_width != left
7103 || w->right_fringe_width != right
7104 || w->fringes_outside_margins != outside))
7106 if (left > 0 || right > 0)
7108 /* Don't change anything if new fringes don't fit. */
7109 if ((WINDOW_PIXEL_WIDTH (w)
7110 - WINDOW_MARGINS_WIDTH (w)
7111 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
7112 - max (left, 0) - max (right, 0))
7113 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
7114 return NULL;
7117 w->left_fringe_width = left;
7118 w->right_fringe_width = right;
7119 w->fringes_outside_margins = outside;
7121 return w;
7123 else
7124 return NULL;
7127 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
7128 2, 4, 0,
7129 doc: /* Set the fringe widths of window WINDOW.
7130 WINDOW must be a live window and defaults to the selected one.
7132 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
7133 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
7134 fringe width. If a fringe width arg is nil, that means to use the
7135 frame's default fringe width. Default fringe widths can be set with
7136 the command `set-fringe-style'.
7137 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
7138 outside of the display margins. By default, fringes are drawn between
7139 display marginal areas and the text area.
7141 Return t if any fringe was actually changed and nil otherwise. */)
7142 (Lisp_Object window, Lisp_Object left_width,
7143 Lisp_Object right_width, Lisp_Object outside_margins)
7145 struct window *w
7146 = set_window_fringes (decode_live_window (window),
7147 left_width, right_width, outside_margins);
7148 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7152 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
7153 0, 1, 0,
7154 doc: /* Get width of fringes of window WINDOW.
7155 WINDOW must be a live window and defaults to the selected one.
7157 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
7158 (Lisp_Object window)
7160 struct window *w = decode_live_window (window);
7162 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
7163 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
7164 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
7169 /***********************************************************************
7170 Scroll bars
7171 ***********************************************************************/
7173 static struct window *
7174 set_window_scroll_bars (struct window *w, Lisp_Object width,
7175 Lisp_Object vertical_type, Lisp_Object height,
7176 Lisp_Object horizontal_type)
7178 int iwidth = extract_dimension (width);
7179 bool changed = false;
7181 if (iwidth == 0)
7182 vertical_type = Qnil;
7184 if (!(NILP (vertical_type)
7185 || EQ (vertical_type, Qleft)
7186 || EQ (vertical_type, Qright)
7187 || EQ (vertical_type, Qt)))
7188 error ("Invalid type of vertical scroll bar");
7190 if (w->scroll_bar_width != iwidth
7191 || !EQ (w->vertical_scroll_bar_type, vertical_type))
7193 /* Don't change anything if new scroll bar won't fit. */
7194 if ((WINDOW_PIXEL_WIDTH (w)
7195 - WINDOW_MARGINS_WIDTH (w)
7196 - WINDOW_FRINGES_WIDTH (w)
7197 - max (iwidth, 0))
7198 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
7200 w->scroll_bar_width = iwidth;
7201 wset_vertical_scroll_bar_type (w, vertical_type);
7202 changed = true;
7206 #if USE_HORIZONTAL_SCROLL_BARS
7208 int iheight = extract_dimension (height);
7210 if (MINI_WINDOW_P (w) || iheight == 0)
7211 horizontal_type = Qnil;
7213 if (!(NILP (horizontal_type)
7214 || EQ (horizontal_type, Qbottom)
7215 || EQ (horizontal_type, Qt)))
7216 error ("Invalid type of horizontal scroll bar");
7218 if (w->scroll_bar_height != iheight
7219 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
7221 /* Don't change anything if new scroll bar won't fit. */
7222 if ((WINDOW_PIXEL_HEIGHT (w)
7223 - WINDOW_HEADER_LINE_HEIGHT (w)
7224 - WINDOW_MODE_LINE_HEIGHT (w)
7225 - max (iheight, 0))
7226 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7228 w->scroll_bar_height = iheight;
7229 wset_horizontal_scroll_bar_type (w, horizontal_type);
7230 changed = true;
7234 #else
7235 wset_horizontal_scroll_bar_type (w, Qnil);
7236 #endif
7238 return changed ? w : NULL;
7241 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7242 Sset_window_scroll_bars, 1, 5, 0,
7243 doc: /* Set width and type of scroll bars of window WINDOW.
7244 WINDOW must be a live window and defaults to the selected one.
7246 Second parameter WIDTH specifies the pixel width for the vertical scroll
7247 bar. If WIDTH is nil, use the scroll bar width of WINDOW's frame.
7248 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7249 bar: left, right, nil or t where nil means to not display a vertical
7250 scroll bar on WINDOW and t means to use WINDOW frame's vertical scroll
7251 bar type.
7253 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7254 scroll bar. If HEIGHT is nil, use the scroll bar height of WINDOW's
7255 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7256 horizontal scroll bar: bottom, nil, or t where nil means to not display
7257 a horizontal scroll bar on WINDOW and t means to use WINDOW frame's
7258 horizontal scroll bar type.
7260 Return t if scroll bars were actually changed and nil otherwise. */)
7261 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7262 Lisp_Object height, Lisp_Object horizontal_type)
7264 struct window *w
7265 = set_window_scroll_bars (decode_live_window (window),
7266 width, vertical_type, height, horizontal_type);
7267 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7271 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7272 0, 1, 0,
7273 doc: /* Get width and type of scroll bars of window WINDOW.
7274 WINDOW must be a live window and defaults to the selected one.
7276 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7277 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7278 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7279 value. */)
7280 (Lisp_Object window)
7282 struct window *w = decode_live_window (window);
7284 return Fcons (((w->scroll_bar_width >= 0)
7285 ? make_number (w->scroll_bar_width)
7286 : Qnil),
7287 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7288 w->vertical_scroll_bar_type,
7289 ((w->scroll_bar_height >= 0)
7290 ? make_number (w->scroll_bar_height)
7291 : Qnil),
7292 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7293 w->horizontal_scroll_bar_type));
7296 /***********************************************************************
7297 Smooth scrolling
7298 ***********************************************************************/
7300 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7301 doc: /* Return the amount by which WINDOW is scrolled vertically.
7302 If WINDOW is omitted or nil, it defaults to the selected window.
7303 Normally, value is a multiple of the canonical character height of WINDOW;
7304 optional second arg PIXELS-P means value is measured in pixels. */)
7305 (Lisp_Object window, Lisp_Object pixels_p)
7307 Lisp_Object result;
7308 struct window *w = decode_live_window (window);
7309 struct frame *f = XFRAME (w->frame);
7311 if (FRAME_WINDOW_P (f))
7312 result = (NILP (pixels_p)
7313 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7314 : make_number (-w->vscroll));
7315 else
7316 result = make_number (0);
7317 return result;
7321 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7322 2, 3, 0,
7323 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7324 WINDOW nil means use the selected window. Normally, VSCROLL is a
7325 non-negative multiple of the canonical character height of WINDOW;
7326 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7327 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7328 corresponds to an integral number of pixels. The return value is the
7329 result of this rounding.
7330 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7331 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7333 struct window *w = decode_live_window (window);
7334 struct frame *f = XFRAME (w->frame);
7336 CHECK_NUMBER_OR_FLOAT (vscroll);
7338 if (FRAME_WINDOW_P (f))
7340 int old_dy = w->vscroll;
7342 w->vscroll = - (NILP (pixels_p)
7343 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7344 : XFLOATINT (vscroll));
7345 w->vscroll = min (w->vscroll, 0);
7347 if (w->vscroll != old_dy)
7349 /* Adjust glyph matrix of the frame if the virtual display
7350 area becomes larger than before. */
7351 if (w->vscroll < 0 && w->vscroll < old_dy)
7352 adjust_frame_glyphs (f);
7354 /* Prevent redisplay shortcuts. */
7355 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7359 return Fwindow_vscroll (window, pixels_p);
7363 /* Call FN for all leaf windows on frame F. FN is called with the
7364 first argument being a pointer to the leaf window, and with
7365 additional argument USER_DATA. Stops when FN returns 0. */
7367 static void
7368 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7369 void *user_data)
7371 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7372 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7373 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7377 /* Helper function for foreach_window. Call FN for all leaf windows
7378 reachable from W. FN is called with the first argument being a
7379 pointer to the leaf window, and with additional argument USER_DATA.
7380 Stop when FN returns false. Value is false if stopped by FN. */
7382 static bool
7383 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7384 void *user_data)
7386 bool cont;
7388 for (cont = true; w && cont;)
7390 if (WINDOWP (w->contents))
7391 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7392 else
7393 cont = fn (w, user_data);
7395 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7398 return cont;
7401 /***********************************************************************
7402 Initialization
7403 ***********************************************************************/
7405 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7406 describe the same state of affairs. This is used by Fequal.
7408 IGNORE_POSITIONS means ignore non-matching scroll positions
7409 and the like.
7411 This ignores a couple of things like the dedication status of
7412 window, combination_limit and the like. This might have to be
7413 fixed. */
7415 bool
7416 compare_window_configurations (Lisp_Object configuration1,
7417 Lisp_Object configuration2,
7418 bool ignore_positions)
7420 register struct save_window_data *d1, *d2;
7421 struct Lisp_Vector *sws1, *sws2;
7422 ptrdiff_t i;
7424 CHECK_WINDOW_CONFIGURATION (configuration1);
7425 CHECK_WINDOW_CONFIGURATION (configuration2);
7427 d1 = (struct save_window_data *) XVECTOR (configuration1);
7428 d2 = (struct save_window_data *) XVECTOR (configuration2);
7429 sws1 = XVECTOR (d1->saved_windows);
7430 sws2 = XVECTOR (d2->saved_windows);
7432 /* Frame settings must match. */
7433 if (d1->frame_cols != d2->frame_cols
7434 || d1->frame_lines != d2->frame_lines
7435 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7436 || !EQ (d1->selected_frame, d2->selected_frame)
7437 || !EQ (d1->f_current_buffer, d2->f_current_buffer)
7438 || (!ignore_positions
7439 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7440 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7441 || !EQ (d1->focus_frame, d2->focus_frame)
7442 /* Verify that the two configurations have the same number of windows. */
7443 || sws1->header.size != sws2->header.size)
7444 return false;
7446 for (i = 0; i < sws1->header.size; i++)
7448 struct saved_window *sw1, *sw2;
7450 sw1 = SAVED_WINDOW_N (sws1, i);
7451 sw2 = SAVED_WINDOW_N (sws2, i);
7453 if (
7454 /* The "current" windows in the two configurations must
7455 correspond to each other. */
7456 EQ (d1->current_window, sw1->window)
7457 != EQ (d2->current_window, sw2->window)
7458 /* Windows' buffers must match. */
7459 || !EQ (sw1->buffer, sw2->buffer)
7460 || !EQ (sw1->pixel_left, sw2->pixel_left)
7461 || !EQ (sw1->pixel_top, sw2->pixel_top)
7462 || !EQ (sw1->pixel_height, sw2->pixel_height)
7463 || !EQ (sw1->pixel_width, sw2->pixel_width)
7464 || !EQ (sw1->left_col, sw2->left_col)
7465 || !EQ (sw1->top_line, sw2->top_line)
7466 || !EQ (sw1->total_cols, sw2->total_cols)
7467 || !EQ (sw1->total_lines, sw2->total_lines)
7468 || !EQ (sw1->display_table, sw2->display_table)
7469 /* The next two disjuncts check the window structure for
7470 equality. */
7471 || !EQ (sw1->parent, sw2->parent)
7472 || !EQ (sw1->prev, sw2->prev)
7473 || (!ignore_positions
7474 && (!EQ (sw1->hscroll, sw2->hscroll)
7475 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7476 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7477 || NILP (Fequal (sw1->start, sw2->start))
7478 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7479 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7480 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7481 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7482 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7483 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7484 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7485 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7486 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7487 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7488 return false;
7491 return true;
7494 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7495 Scompare_window_configurations, 2, 2, 0,
7496 doc: /* Compare two window configurations as regards the structure of windows.
7497 This function ignores details such as the values of point
7498 and scrolling positions. */)
7499 (Lisp_Object x, Lisp_Object y)
7501 if (compare_window_configurations (x, y, true))
7502 return Qt;
7503 return Qnil;
7506 void
7507 init_window_once (void)
7509 struct frame *f = make_initial_frame ();
7510 XSETFRAME (selected_frame, f);
7511 Vterminal_frame = selected_frame;
7512 minibuf_window = f->minibuffer_window;
7513 selected_window = f->selected_window;
7516 void
7517 init_window (void)
7519 Vwindow_list = Qnil;
7522 void
7523 syms_of_window (void)
7525 DEFSYM (Qscroll_up, "scroll-up");
7526 DEFSYM (Qscroll_down, "scroll-down");
7527 DEFSYM (Qscroll_command, "scroll-command");
7529 Fput (Qscroll_up, Qscroll_command, Qt);
7530 Fput (Qscroll_down, Qscroll_command, Qt);
7532 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7533 DEFSYM (Qwindowp, "windowp");
7534 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7535 DEFSYM (Qwindow_live_p, "window-live-p");
7536 DEFSYM (Qwindow_valid_p, "window-valid-p");
7537 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7538 DEFSYM (Qdelete_window, "delete-window");
7539 DEFSYM (Qwindow__resize_root_window, "window--resize-root-window");
7540 DEFSYM (Qwindow__resize_root_window_vertically,
7541 "window--resize-root-window-vertically");
7542 DEFSYM (Qwindow__sanitize_window_sizes, "window--sanitize-window-sizes");
7543 DEFSYM (Qwindow__pixel_to_total, "window--pixel-to-total");
7544 DEFSYM (Qsafe, "safe");
7545 DEFSYM (Qdisplay_buffer, "display-buffer");
7546 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7547 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7548 DEFSYM (Qget_mru_window, "get-mru-window");
7549 DEFSYM (Qwindow_size, "window-size");
7550 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7551 DEFSYM (Qabove, "above");
7552 DEFSYM (Qclone_of, "clone-of");
7553 DEFSYM (Qfloor, "floor");
7554 DEFSYM (Qceiling, "ceiling");
7555 DEFSYM (Qmark_for_redisplay, "mark-for-redisplay");
7556 DEFSYM (Qmode_line_format, "mode-line-format");
7557 DEFSYM (Qheader_line_format, "header-line-format");
7559 staticpro (&Vwindow_list);
7561 minibuf_selected_window = Qnil;
7562 staticpro (&minibuf_selected_window);
7564 window_scroll_pixel_based_preserve_x = -1;
7565 window_scroll_pixel_based_preserve_y = -1;
7566 window_scroll_preserve_hpos = -1;
7567 window_scroll_preserve_vpos = -1;
7569 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7570 doc: /* Non-nil means call as function to display a help buffer.
7571 The function is called with one argument, the buffer to be displayed.
7572 Used by `with-output-to-temp-buffer'.
7573 If this function is used, then it must do the entire job of showing
7574 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7575 Vtemp_buffer_show_function = Qnil;
7577 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7578 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7579 Vminibuf_scroll_window = Qnil;
7581 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7582 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7583 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7584 is displayed in the `mode-line' face. */);
7585 mode_line_in_non_selected_windows = true;
7587 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7588 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7589 Vother_window_scroll_buffer = Qnil;
7591 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7592 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7593 auto_window_vscroll_p = true;
7595 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7596 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7597 next_screen_context_lines = 2;
7599 DEFVAR_LISP ("scroll-preserve-screen-position",
7600 Vscroll_preserve_screen_position,
7601 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7602 A value of nil means point does not keep its screen position except
7603 at the scroll margin or window boundary respectively.
7604 A value of t means point keeps its screen position if the scroll
7605 command moved it vertically out of the window, e.g. when scrolling
7606 by full screens.
7607 Any other value means point always keeps its screen position.
7608 Scroll commands should have the `scroll-command' property
7609 on their symbols to be controlled by this variable. */);
7610 Vscroll_preserve_screen_position = Qnil;
7612 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7613 doc: /* Type of marker to use for `window-point'. */);
7614 Vwindow_point_insertion_type = Qnil;
7615 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7617 DEFVAR_LISP ("window-configuration-change-hook",
7618 Vwindow_configuration_change_hook,
7619 doc: /* Functions to call when window configuration changes.
7620 The buffer-local part is run once per window, with the relevant window
7621 selected; while the global part is run only once for the modified frame,
7622 with the relevant frame selected. */);
7623 Vwindow_configuration_change_hook = Qnil;
7625 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
7626 doc: /* Functions called during redisplay, if window sizes have changed.
7627 The value should be a list of functions that take one argument.
7628 During the first part of redisplay, for each frame, if any of its windows
7629 have changed size since the last redisplay, or have been split or deleted,
7630 all the functions in the list are called, with the frame as argument.
7631 If redisplay decides to resize the minibuffer window, it calls these
7632 functions on behalf of that as well. */);
7633 Vwindow_size_change_functions = Qnil;
7635 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7636 doc: /* Non-nil means `recenter' redraws entire frame.
7637 If this option is non-nil, then the `recenter' command with a nil
7638 argument will redraw the entire frame; the special value `tty' causes
7639 the frame to be redrawn only if it is a tty frame. */);
7640 Vrecenter_redisplay = Qtty;
7642 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7643 doc: /* If t, resize window combinations proportionally.
7644 If this variable is nil, splitting a window gets the entire screen space
7645 for displaying the new window from the window to split. Deleting and
7646 resizing a window preferably resizes one adjacent window only.
7648 If this variable is t, splitting a window tries to get the space
7649 proportionally from all windows in the same combination. This also
7650 allows splitting a window that is otherwise too small or of fixed size.
7651 Resizing and deleting a window proportionally resize all windows in the
7652 same combination.
7654 Other values are reserved for future use.
7656 A specific split operation may ignore the value of this variable if it
7657 is affected by a non-nil value of `window-combination-limit'. */);
7658 Vwindow_combination_resize = Qnil;
7660 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7661 doc: /* If non-nil, splitting a window makes a new parent window.
7662 The following values are recognized:
7664 nil means splitting a window will create a new parent window only if the
7665 window has no parent window or the window shall become part of a
7666 combination orthogonal to the one it is part of.
7668 `window-size' means that splitting a window for displaying a buffer
7669 makes a new parent window provided `display-buffer' is supposed to
7670 explicitly set the window's size due to the presence of a
7671 `window-height' or `window-width' entry in the alist used by
7672 `display-buffer'. Otherwise, this value is handled like nil.
7674 `temp-buffer-resize' means that splitting a window for displaying a
7675 temporary buffer via `with-temp-buffer-window' makes a new parent
7676 window only if `temp-buffer-resize-mode' is enabled. Otherwise,
7677 this value is handled like nil.
7679 `temp-buffer' means that splitting a window for displaying a temporary
7680 buffer via `with-temp-buffer-window' always makes a new parent
7681 window. Otherwise, this value is handled like nil.
7683 `display-buffer' means that splitting a window for displaying a buffer
7684 always makes a new parent window. Since temporary buffers are
7685 displayed by the function `display-buffer', this value is stronger
7686 than `temp-buffer'. Splitting a window for other purpose makes a
7687 new parent window only if needed.
7689 t means that splitting a window always creates a new parent window. If
7690 all splits behave this way, each frame's window tree is a binary
7691 tree and every window but the frame's root window has exactly one
7692 sibling.
7694 The default value is `window-size'. Other values are reserved for
7695 future use. */);
7696 Vwindow_combination_limit = Qwindow_size;
7698 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7699 doc: /* Alist of persistent window parameters.
7700 This alist specifies which window parameters shall get saved by
7701 `current-window-configuration' and `window-state-get' and subsequently
7702 restored to their previous values by `set-window-configuration' and
7703 `window-state-put'.
7705 The car of each entry of this alist is the symbol specifying the
7706 parameter. The cdr is one of the following:
7708 nil means the parameter is neither saved by `window-state-get' nor by
7709 `current-window-configuration'.
7711 t means the parameter is saved by `current-window-configuration' and,
7712 provided its WRITABLE argument is nil, by `window-state-get'.
7714 The symbol `writable' means the parameter is saved unconditionally by
7715 both `current-window-configuration' and `window-state-get'. Do not use
7716 this value for parameters without read syntax (like windows or frames).
7718 Parameters not saved by `current-window-configuration' or
7719 `window-state-get' are left alone by `set-window-configuration'
7720 respectively are not installed by `window-state-put'. */);
7721 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7723 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7724 doc: /* Non-nil means resize windows pixelwise.
7725 This currently affects the functions: `split-window', `maximize-window',
7726 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7727 all functions that symmetrically resize a parent window.
7729 Note that when a frame's pixel size is not a multiple of the
7730 frame's character size, at least one window may get resized
7731 pixelwise even if this option is nil. */);
7732 window_resize_pixelwise = false;
7734 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7735 Vfast_but_imprecise_scrolling,
7736 doc: /* When non-nil, accelerate scrolling operations.
7737 This comes into play when scrolling rapidly over previously
7738 unfontified buffer regions. Only those portions of the buffer which
7739 are actually going to be displayed get fontified.
7741 Note that this optimization can cause the portion of the buffer
7742 displayed after a scrolling operation to be somewhat inaccurate. */);
7743 Vfast_but_imprecise_scrolling = false;
7745 defsubr (&Sselected_window);
7746 defsubr (&Sminibuffer_window);
7747 defsubr (&Swindow_minibuffer_p);
7748 defsubr (&Swindowp);
7749 defsubr (&Swindow_valid_p);
7750 defsubr (&Swindow_live_p);
7751 defsubr (&Swindow_frame);
7752 defsubr (&Sframe_root_window);
7753 defsubr (&Sframe_first_window);
7754 defsubr (&Sframe_selected_window);
7755 defsubr (&Sset_frame_selected_window);
7756 defsubr (&Spos_visible_in_window_p);
7757 defsubr (&Swindow_line_height);
7758 defsubr (&Swindow_buffer);
7759 defsubr (&Swindow_parent);
7760 defsubr (&Swindow_top_child);
7761 defsubr (&Swindow_left_child);
7762 defsubr (&Swindow_next_sibling);
7763 defsubr (&Swindow_prev_sibling);
7764 defsubr (&Swindow_combination_limit);
7765 defsubr (&Sset_window_combination_limit);
7766 defsubr (&Swindow_use_time);
7767 defsubr (&Swindow_pixel_width);
7768 defsubr (&Swindow_pixel_height);
7769 defsubr (&Swindow_pixel_width_before_size_change);
7770 defsubr (&Swindow_pixel_height_before_size_change);
7771 defsubr (&Swindow_total_width);
7772 defsubr (&Swindow_total_height);
7773 defsubr (&Swindow_normal_size);
7774 defsubr (&Swindow_new_pixel);
7775 defsubr (&Swindow_new_total);
7776 defsubr (&Swindow_new_normal);
7777 defsubr (&Swindow_pixel_left);
7778 defsubr (&Swindow_pixel_top);
7779 defsubr (&Swindow_left_column);
7780 defsubr (&Swindow_top_line);
7781 defsubr (&Sset_window_new_pixel);
7782 defsubr (&Sset_window_new_total);
7783 defsubr (&Sset_window_new_normal);
7784 defsubr (&Swindow_resize_apply);
7785 defsubr (&Swindow_resize_apply_total);
7786 defsubr (&Swindow_body_height);
7787 defsubr (&Swindow_body_width);
7788 defsubr (&Swindow_hscroll);
7789 defsubr (&Sset_window_hscroll);
7790 defsubr (&Swindow_redisplay_end_trigger);
7791 defsubr (&Sset_window_redisplay_end_trigger);
7792 defsubr (&Swindow_mode_line_height);
7793 defsubr (&Swindow_header_line_height);
7794 defsubr (&Swindow_right_divider_width);
7795 defsubr (&Swindow_bottom_divider_width);
7796 defsubr (&Swindow_scroll_bar_width);
7797 defsubr (&Swindow_scroll_bar_height);
7798 defsubr (&Scoordinates_in_window_p);
7799 defsubr (&Swindow_at);
7800 defsubr (&Swindow_point);
7801 defsubr (&Swindow_old_point);
7802 defsubr (&Swindow_start);
7803 defsubr (&Swindow_end);
7804 defsubr (&Sset_window_point);
7805 defsubr (&Sset_window_start);
7806 defsubr (&Swindow_dedicated_p);
7807 defsubr (&Swindow_lines_pixel_dimensions);
7808 defsubr (&Sset_window_dedicated_p);
7809 defsubr (&Swindow_display_table);
7810 defsubr (&Sset_window_display_table);
7811 defsubr (&Snext_window);
7812 defsubr (&Sprevious_window);
7813 defsubr (&Sget_buffer_window);
7814 defsubr (&Sdelete_other_windows_internal);
7815 defsubr (&Sdelete_window_internal);
7816 defsubr (&Sresize_mini_window_internal);
7817 defsubr (&Sset_window_buffer);
7818 defsubr (&Srun_window_configuration_change_hook);
7819 defsubr (&Srun_window_scroll_functions);
7820 defsubr (&Sselect_window);
7821 defsubr (&Sforce_window_update);
7822 defsubr (&Ssplit_window_internal);
7823 defsubr (&Sscroll_up);
7824 defsubr (&Sscroll_down);
7825 defsubr (&Sscroll_left);
7826 defsubr (&Sscroll_right);
7827 defsubr (&Sother_window_for_scrolling);
7828 defsubr (&Sscroll_other_window);
7829 defsubr (&Sminibuffer_selected_window);
7830 defsubr (&Srecenter);
7831 defsubr (&Swindow_text_width);
7832 defsubr (&Swindow_text_height);
7833 defsubr (&Smove_to_window_line);
7834 defsubr (&Swindow_configuration_p);
7835 defsubr (&Swindow_configuration_frame);
7836 defsubr (&Sset_window_configuration);
7837 defsubr (&Scurrent_window_configuration);
7838 defsubr (&Sset_window_margins);
7839 defsubr (&Swindow_margins);
7840 defsubr (&Sset_window_fringes);
7841 defsubr (&Swindow_fringes);
7842 defsubr (&Sset_window_scroll_bars);
7843 defsubr (&Swindow_scroll_bars);
7844 defsubr (&Swindow_vscroll);
7845 defsubr (&Sset_window_vscroll);
7846 defsubr (&Scompare_window_configurations);
7847 defsubr (&Swindow_list);
7848 defsubr (&Swindow_list_1);
7849 defsubr (&Swindow_prev_buffers);
7850 defsubr (&Sset_window_prev_buffers);
7851 defsubr (&Swindow_next_buffers);
7852 defsubr (&Sset_window_next_buffers);
7853 defsubr (&Swindow_parameters);
7854 defsubr (&Swindow_parameter);
7855 defsubr (&Sset_window_parameter);
7858 void
7859 keys_of_window (void)
7861 initial_define_key (control_x_map, '<', "scroll-left");
7862 initial_define_key (control_x_map, '>', "scroll-right");
7864 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7865 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7866 initial_define_key (meta_map, 'v', "scroll-down-command");