Subject: Restore correct Gnus newsgroup name after sending message
[emacs.git] / src / window.c
blob71a82b522c4a9417428f8a276003925ca82f10cc
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 <http://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))
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.
576 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
577 applications and internal routines often select a window temporarily for
578 various purposes; mostly, to simplify coding. As a rule, such
579 selections should be not recorded and therefore will not pollute
580 `buffer-list-update-hook'. Selections that "really count" are those
581 causing a visible change in the next redisplay of WINDOW's frame and
582 should be always recorded. So if you think of running a function each
583 time a window gets selected put it on `buffer-list-update-hook'.
585 Also note that the main editor command loop sets the current buffer to
586 the buffer of the selected window before each command. */)
587 (Lisp_Object window, Lisp_Object norecord)
589 return select_window (window, norecord, false);
592 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
593 doc: /* Return the buffer displayed in window WINDOW.
594 If WINDOW is omitted or nil, it defaults to the selected window.
595 Return nil for an internal window or a deleted window. */)
596 (Lisp_Object window)
598 struct window *w = decode_any_window (window);
599 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
602 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
603 doc: /* Return the parent window of window WINDOW.
604 WINDOW must be a valid window and defaults to the selected one.
605 Return nil for a window with no parent (e.g. a root window). */)
606 (Lisp_Object window)
608 return decode_valid_window (window)->parent;
611 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
612 doc: /* Return the topmost child window of window WINDOW.
613 WINDOW must be a valid window and defaults to the selected one.
614 Return nil if WINDOW is a live window (live windows have no children).
615 Return nil if WINDOW is an internal window whose children form a
616 horizontal combination. */)
617 (Lisp_Object window)
619 struct window *w = decode_valid_window (window);
620 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
623 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
624 doc: /* Return the leftmost child window of window WINDOW.
625 WINDOW must be a valid window and defaults to the selected one.
626 Return nil if WINDOW is a live window (live windows have no children).
627 Return nil if WINDOW is an internal window whose children form a
628 vertical combination. */)
629 (Lisp_Object window)
631 struct window *w = decode_valid_window (window);
632 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
635 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
636 doc: /* Return the next sibling window of window WINDOW.
637 WINDOW must be a valid window and defaults to the selected one.
638 Return nil if WINDOW has no next sibling. */)
639 (Lisp_Object window)
641 return decode_valid_window (window)->next;
644 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
645 doc: /* Return the previous sibling window of window WINDOW.
646 WINDOW must be a valid window and defaults to the selected one.
647 Return nil if WINDOW has no previous sibling. */)
648 (Lisp_Object window)
650 return decode_valid_window (window)->prev;
653 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
654 doc: /* Return combination limit of window WINDOW.
655 WINDOW must be a valid window used in horizontal or vertical combination.
656 If the return value is nil, child windows of WINDOW can be recombined with
657 WINDOW's siblings. A return value of t means that child windows of
658 WINDOW are never (re-)combined with WINDOW's siblings. */)
659 (Lisp_Object window)
661 struct window *w;
663 CHECK_VALID_WINDOW (window);
664 w = XWINDOW (window);
665 if (WINDOW_LEAF_P (w))
666 error ("Combination limit is meaningful for internal windows only");
667 return w->combination_limit;
670 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
671 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
672 WINDOW must be a valid window used in horizontal or vertical combination.
673 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
674 siblings. LIMIT t means that child windows of WINDOW are never
675 \(re-)combined with WINDOW's siblings. Other values are reserved for
676 future use. */)
677 (Lisp_Object window, Lisp_Object limit)
679 struct window *w;
681 CHECK_VALID_WINDOW (window);
682 w = XWINDOW (window);
683 if (WINDOW_LEAF_P (w))
684 error ("Combination limit is meaningful for internal windows only");
685 wset_combination_limit (w, limit);
686 return limit;
689 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
690 doc: /* Return the use time of window WINDOW.
691 WINDOW must be a live window and defaults to the selected one.
692 The window with the highest use time is the most recently selected
693 one. The window with the lowest use time is the least recently
694 selected one. */)
695 (Lisp_Object window)
697 return make_number (decode_live_window (window)->use_time);
700 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
701 doc: /* Return the width of window WINDOW in pixels.
702 WINDOW must be a valid window and defaults to the selected one.
704 The return value includes the fringes and margins of WINDOW as well as
705 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
706 an internal window, its pixel width is the width of the screen areas
707 spanned by its children. */)
708 (Lisp_Object window)
710 return make_number (decode_valid_window (window)->pixel_width);
713 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
714 doc: /* Return the height of window WINDOW in pixels.
715 WINDOW must be a valid window and defaults to the selected one.
717 The return value includes the mode line and header line and the bottom
718 divider, if any. If WINDOW is an internal window, its pixel height is
719 the height of the screen areas spanned by its children. */)
720 (Lisp_Object window)
722 return make_number (decode_valid_window (window)->pixel_height);
725 DEFUN ("window-pixel-width-before-size-change",
726 Fwindow_pixel_width_before_size_change,
727 Swindow_pixel_width_before_size_change, 0, 1, 0,
728 doc: /* Return pixel width of window WINDOW before last size changes.
729 WINDOW must be a valid window and defaults to the selected one.
731 The return value is the pixel width of WINDOW at the last time
732 `window-size-change-functions' was run. It's zero if WINDOW was made
733 after that. */)
734 (Lisp_Object window)
736 return (make_number
737 (decode_valid_window (window)->pixel_width_before_size_change));
740 DEFUN ("window-pixel-height-before-size-change",
741 Fwindow_pixel_height_before_size_change,
742 Swindow_pixel_height_before_size_change, 0, 1, 0,
743 doc: /* Return pixel height of window WINDOW before last size changes.
744 WINDOW must be a valid window and defaults to the selected one.
746 The return value is the pixel height of WINDOW at the last time
747 `window-size-change-functions' was run. It's zero if WINDOW was made
748 after that. */)
749 (Lisp_Object window)
751 return (make_number
752 (decode_valid_window (window)->pixel_height_before_size_change));
755 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
756 doc: /* Return the height of window WINDOW in lines.
757 WINDOW must be a valid window and defaults to the selected one.
759 The return value includes the heights of WINDOW's mode and header line
760 and its bottom divider, if any. If WINDOW is an internal window, the
761 total height is the height of the screen areas spanned by its children.
763 If WINDOW's pixel height is not an integral multiple of its frame's
764 character height, the number of lines occupied by WINDOW is rounded
765 internally. This is done in a way such that, if WINDOW is a parent
766 window, the sum of the total heights of all its children internally
767 equals the total height of WINDOW.
769 If the optional argument ROUND is `ceiling', return the smallest integer
770 larger than WINDOW's pixel height divided by the character height of
771 WINDOW's frame. ROUND `floor' means to return the largest integer
772 smaller than WINDOW's pixel height divided by the character height of
773 WINDOW's frame. Any other value of ROUND means to return the internal
774 total height of WINDOW. */)
775 (Lisp_Object window, Lisp_Object round)
777 struct window *w = decode_valid_window (window);
779 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
780 return make_number (w->total_lines);
781 else
783 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
785 return make_number (EQ (round, Qceiling)
786 ? ((w->pixel_height + unit - 1) /unit)
787 : (w->pixel_height / unit));
791 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
792 doc: /* Return the total width of window WINDOW in columns.
793 WINDOW must be a valid window and defaults to the selected one.
795 The return value includes the widths of WINDOW's fringes, margins,
796 scroll bars and its right divider, if any. If WINDOW is an internal
797 window, the total width is the width of the screen areas spanned by its
798 children.
800 If WINDOW's pixel width is not an integral multiple of its frame's
801 character width, the number of lines occupied by WINDOW is rounded
802 internally. This is done in a way such that, if WINDOW is a parent
803 window, the sum of the total widths of all its children internally
804 equals the total width of WINDOW.
806 If the optional argument ROUND is `ceiling', return the smallest integer
807 larger than WINDOW's pixel width divided by the character width of
808 WINDOW's frame. ROUND `floor' means to return the largest integer
809 smaller than WINDOW's pixel width divided by the character width of
810 WINDOW's frame. Any other value of ROUND means to return the internal
811 total width of WINDOW. */)
812 (Lisp_Object window, Lisp_Object round)
814 struct window *w = decode_valid_window (window);
816 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
817 return make_number (w->total_cols);
818 else
820 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
822 return make_number (EQ (round, Qceiling)
823 ? ((w->pixel_width + unit - 1) /unit)
824 : (w->pixel_width / unit));
828 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
829 doc: /* Return the new total size of window WINDOW.
830 WINDOW must be a valid window and defaults to the selected one.
832 The new total size of WINDOW is the value set by the last call of
833 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
834 installed as WINDOW's total height (see `window-total-height') or total
835 width (see `window-total-width'). */)
836 (Lisp_Object window)
838 return decode_valid_window (window)->new_total;
841 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
842 doc: /* Return the normal height of window WINDOW.
843 WINDOW must be a valid window and defaults to the selected one.
844 If HORIZONTAL is non-nil, return the normal width of WINDOW.
846 The normal height of a frame's root window or a window that is
847 horizontally combined (a window that has a left or right sibling) is
848 1.0. The normal height of a window that is vertically combined (has a
849 sibling above or below) is the fraction of the window's height with
850 respect to its parent. The sum of the normal heights of all windows in a
851 vertical combination equals 1.0.
853 Similarly, the normal width of a frame's root window or a window that is
854 vertically combined equals 1.0. The normal width of a window that is
855 horizontally combined is the fraction of the window's width with respect
856 to its parent. The sum of the normal widths of all windows in a
857 horizontal combination equals 1.0.
859 The normal sizes of windows are used to restore the proportional sizes
860 of windows after they have been shrunk to their minimum sizes; for
861 example when a frame is temporarily made very small and afterwards gets
862 re-enlarged to its previous size. */)
863 (Lisp_Object window, Lisp_Object horizontal)
865 struct window *w = decode_valid_window (window);
867 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
870 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
871 doc: /* Return new normal size of window WINDOW.
872 WINDOW must be a valid window and defaults to the selected one.
874 The new normal size of WINDOW is the value set by the last call of
875 `set-window-new-normal' for WINDOW. If valid, it will be shortly
876 installed as WINDOW's normal size (see `window-normal-size'). */)
877 (Lisp_Object window)
879 return decode_valid_window (window)->new_normal;
882 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
883 doc: /* Return new pixel size of window WINDOW.
884 WINDOW must be a valid window and defaults to the selected one.
886 The new pixel size of WINDOW is the value set by the last call of
887 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
888 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
889 width (see `window-pixel-width'). */)
890 (Lisp_Object window)
892 return decode_valid_window (window)->new_pixel;
895 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
896 doc: /* Return left pixel edge of window WINDOW.
897 WINDOW must be a valid window and defaults to the selected one. */)
898 (Lisp_Object window)
900 return make_number (decode_valid_window (window)->pixel_left);
903 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
904 doc: /* Return top pixel edge of window WINDOW.
905 WINDOW must be a valid window and defaults to the selected one. */)
906 (Lisp_Object window)
908 return make_number (decode_valid_window (window)->pixel_top);
911 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
912 doc: /* Return left column of window WINDOW.
913 This is the distance, in columns, between the left edge of WINDOW and
914 the left edge of the frame's window area. For instance, the return
915 value is 0 if there is no window to the left of WINDOW.
917 WINDOW must be a valid window and defaults to the selected one. */)
918 (Lisp_Object window)
920 return make_number (decode_valid_window (window)->left_col);
923 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
924 doc: /* Return top line of window WINDOW.
925 This is the distance, in lines, between the top of WINDOW and the top
926 of the frame's window area. For instance, the return value is 0 if
927 there is no window above WINDOW.
929 WINDOW must be a valid window and defaults to the selected one. */)
930 (Lisp_Object window)
932 return make_number (decode_valid_window (window)->top_line);
935 /* Return the number of lines/pixels of W's body. Don't count any mode
936 or header line or horizontal divider of W. Rounds down to nearest
937 integer when not working pixelwise. */
938 static int
939 window_body_height (struct window *w, bool pixelwise)
941 int height = (w->pixel_height
942 - WINDOW_HEADER_LINE_HEIGHT (w)
943 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
944 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
945 : 0)
946 - WINDOW_MODE_LINE_HEIGHT (w)
947 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
949 /* Don't return a negative value. */
950 return max (pixelwise
951 ? height
952 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
956 /* Return the number of columns/pixels of W's body. Don't count columns
957 occupied by the scroll bar or the divider/vertical bar separating W
958 from its right sibling or margins. On window-systems don't count
959 fringes either. Round down to nearest integer when not working
960 pixelwise. */
962 window_body_width (struct window *w, bool pixelwise)
964 struct frame *f = XFRAME (WINDOW_FRAME (w));
966 int width = (w->pixel_width
967 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
968 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
969 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
970 : (/* A vertical bar is either 1 or 0. */
971 !FRAME_WINDOW_P (f)
972 && !WINDOW_RIGHTMOST_P (w)
973 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
974 - WINDOW_MARGINS_WIDTH (w)
975 - (FRAME_WINDOW_P (f)
976 ? WINDOW_FRINGES_WIDTH (w)
977 : 0));
979 /* Don't return a negative value. */
980 return max (pixelwise
981 ? width
982 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
986 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
987 doc: /* Return the height of WINDOW's text area.
988 WINDOW must be a live window and defaults to the selected one. Optional
989 argument PIXELWISE non-nil means return the height of WINDOW's text area
990 in pixels. The return value does not include the mode line or header
991 line or any horizontal divider.
993 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
994 pixel height divided by the character height of WINDOW's frame. This
995 means that if a line at the bottom of the text area is only partially
996 visible, that line is not counted. */)
997 (Lisp_Object window, Lisp_Object pixelwise)
999 return make_number (window_body_height (decode_live_window (window),
1000 !NILP (pixelwise)));
1003 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
1004 doc: /* Return the width of WINDOW's text area.
1005 WINDOW must be a live window and defaults to the selected one. Optional
1006 argument PIXELWISE non-nil means return the width in pixels. The return
1007 value does not include any vertical dividers, fringes or marginal areas,
1008 or scroll bars.
1010 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
1011 pixel width divided by the character width of WINDOW's frame. This
1012 means that if a column at the right of the text area is only partially
1013 visible, that column is not counted.
1015 Note that the returned value includes the column reserved for the
1016 continuation glyph. */)
1017 (Lisp_Object window, Lisp_Object pixelwise)
1019 return make_number (window_body_width (decode_live_window (window),
1020 !NILP (pixelwise)));
1023 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
1024 Swindow_mode_line_height, 0, 1, 0,
1025 doc: /* Return the height in pixels of WINDOW's mode-line.
1026 WINDOW must be a live window and defaults to the selected one. */)
1027 (Lisp_Object window)
1029 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
1032 DEFUN ("window-header-line-height", Fwindow_header_line_height,
1033 Swindow_header_line_height, 0, 1, 0,
1034 doc: /* Return the height in pixels of WINDOW's header-line.
1035 WINDOW must be a live window and defaults to the selected one. */)
1036 (Lisp_Object window)
1038 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1041 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1042 Swindow_right_divider_width, 0, 1, 0,
1043 doc: /* Return the width in pixels of WINDOW's right divider.
1044 WINDOW must be a live window and defaults to the selected one. */)
1045 (Lisp_Object window)
1047 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1050 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1051 Swindow_bottom_divider_width, 0, 1, 0,
1052 doc: /* Return the width in pixels of WINDOW's bottom divider.
1053 WINDOW must be a live window and defaults to the selected one. */)
1054 (Lisp_Object window)
1056 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1059 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1060 Swindow_scroll_bar_width, 0, 1, 0,
1061 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1062 WINDOW must be a live window and defaults to the selected one. */)
1063 (Lisp_Object window)
1065 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1068 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1069 Swindow_scroll_bar_height, 0, 1, 0,
1070 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1071 WINDOW must be a live window and defaults to the selected one. */)
1072 (Lisp_Object window)
1074 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1077 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1078 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1079 WINDOW must be a live window and defaults to the selected one. */)
1080 (Lisp_Object window)
1082 return make_number (decode_live_window (window)->hscroll);
1085 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1086 range, returning the new amount as a fixnum. */
1087 static Lisp_Object
1088 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1090 /* Horizontal scrolling has problems with large scroll amounts.
1091 It's too slow with long lines, and even with small lines the
1092 display can be messed up. For now, though, impose only the limits
1093 required by the internal representation: horizontal scrolling must
1094 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1095 (since it's stored in a ptrdiff_t). */
1096 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1097 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1099 /* Prevent redisplay shortcuts when changing the hscroll. */
1100 if (w->hscroll != new_hscroll)
1101 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1103 w->hscroll = new_hscroll;
1104 w->suspend_auto_hscroll = true;
1106 return make_number (new_hscroll);
1109 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1110 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1111 WINDOW must be a live window and defaults to the selected one.
1112 Clip the number to a reasonable value if out of range.
1113 Return the new number. NCOL should be zero or positive.
1115 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1116 window so that the location of point moves off-window. */)
1117 (Lisp_Object window, Lisp_Object ncol)
1119 CHECK_NUMBER (ncol);
1120 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1123 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1124 Swindow_redisplay_end_trigger, 0, 1, 0,
1125 doc: /* Return WINDOW's redisplay end trigger value.
1126 WINDOW must be a live window and defaults to the selected one.
1127 See `set-window-redisplay-end-trigger' for more information. */)
1128 (Lisp_Object window)
1130 return decode_live_window (window)->redisplay_end_trigger;
1133 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1134 Sset_window_redisplay_end_trigger, 2, 2, 0,
1135 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1136 WINDOW must be a live window and defaults to the selected one. VALUE
1137 should be a buffer position (typically a marker) or nil. If it is a
1138 buffer position, then if redisplay in WINDOW reaches a position beyond
1139 VALUE, the functions in `redisplay-end-trigger-functions' are called
1140 with two arguments: WINDOW, and the end trigger value. Afterwards the
1141 end-trigger value is reset to nil. */)
1142 (register Lisp_Object window, Lisp_Object value)
1144 wset_redisplay_end_trigger (decode_live_window (window), value);
1145 return value;
1148 /* Test if the character at column X, row Y is within window W.
1149 If it is not, return ON_NOTHING;
1150 if it is on the window's vertical divider, return
1151 ON_RIGHT_DIVIDER;
1152 if it is on the window's horizontal divider, return
1153 ON_BOTTOM_DIVIDER;
1154 if it is in the window's text area, return ON_TEXT;
1155 if it is on the window's modeline, return ON_MODE_LINE;
1156 if it is on the border between the window and its right sibling,
1157 return ON_VERTICAL_BORDER;
1158 if it is on a scroll bar, return ON_SCROLL_BAR;
1159 if it is on the window's top line, return ON_HEADER_LINE;
1160 if it is in left or right fringe of the window,
1161 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1162 if it is in the marginal area to the left/right of the window,
1163 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1165 X and Y are frame relative pixel coordinates. */
1167 static enum window_part
1168 coordinates_in_window (register struct window *w, int x, int y)
1170 struct frame *f = XFRAME (WINDOW_FRAME (w));
1171 enum window_part part;
1172 int ux = FRAME_COLUMN_WIDTH (f);
1173 int left_x = WINDOW_LEFT_EDGE_X (w);
1174 int right_x = WINDOW_RIGHT_EDGE_X (w);
1175 int top_y = WINDOW_TOP_EDGE_Y (w);
1176 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1177 /* The width of the area where the vertical line can be dragged.
1178 (Between mode lines for instance. */
1179 int grabbable_width = ux;
1180 int lmargin_width, rmargin_width, text_left, text_right;
1182 /* Outside any interesting row or column? */
1183 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1184 return ON_NOTHING;
1186 /* On the horizontal window divider (which prevails the vertical
1187 divider)? */
1188 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1189 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1190 && y <= bottom_y)
1191 return ON_BOTTOM_DIVIDER;
1192 /* On vertical window divider? */
1193 else if (!WINDOW_RIGHTMOST_P (w)
1194 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1195 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1196 && x <= right_x)
1197 return ON_RIGHT_DIVIDER;
1198 /* On the horizontal scroll bar? (Including the empty space at its
1199 right!) */
1200 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1201 && y >= (bottom_y
1202 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1203 - CURRENT_MODE_LINE_HEIGHT (w)
1204 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1205 && y <= (bottom_y
1206 - CURRENT_MODE_LINE_HEIGHT (w)
1207 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1208 return ON_HORIZONTAL_SCROLL_BAR;
1209 /* On the mode or header line? */
1210 else if ((WINDOW_WANTS_MODELINE_P (w)
1211 && y >= (bottom_y
1212 - CURRENT_MODE_LINE_HEIGHT (w)
1213 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1214 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1215 && (part = ON_MODE_LINE))
1216 || (WINDOW_WANTS_HEADER_LINE_P (w)
1217 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1218 && (part = ON_HEADER_LINE)))
1220 /* If it's under/over the scroll bar portion of the mode/header
1221 line, say it's on the vertical line. That's to be able to
1222 resize windows horizontally in case we're using toolkit scroll
1223 bars. Note: If scrollbars are on the left, the window that
1224 must be eventually resized is that on the left of WINDOW. */
1225 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1226 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1227 && !WINDOW_LEFTMOST_P (w)
1228 && eabs (x - left_x) < grabbable_width)
1229 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1230 && !WINDOW_RIGHTMOST_P (w)
1231 && eabs (x - right_x) < grabbable_width)))
1232 return ON_VERTICAL_BORDER;
1233 else
1234 return part;
1237 /* In what's below, we subtract 1 when computing right_x because we
1238 want the rightmost pixel, which is given by left_pixel+width-1. */
1239 if (w->pseudo_window_p)
1241 left_x = 0;
1242 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1244 else
1246 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1247 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1250 /* Outside any interesting column? */
1251 if (x < left_x || x > right_x)
1252 return ON_VERTICAL_SCROLL_BAR;
1254 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1255 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1257 text_left = window_box_left (w, TEXT_AREA);
1258 text_right = text_left + window_box_width (w, TEXT_AREA);
1260 if (FRAME_WINDOW_P (f))
1262 if (!w->pseudo_window_p
1263 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1264 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1265 && !WINDOW_RIGHTMOST_P (w)
1266 && (eabs (x - right_x) < grabbable_width))
1267 return ON_VERTICAL_BORDER;
1269 /* Need to say "x > right_x" rather than >=, since on character
1270 terminals, the vertical line's x coordinate is right_x. */
1271 else if (!w->pseudo_window_p
1272 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1273 && !WINDOW_RIGHTMOST_P (w)
1274 /* Why check ux if we are not the rightmost window? Also
1275 shouldn't a pseudo window always be rightmost? */
1276 && x > right_x - ux)
1277 return ON_VERTICAL_BORDER;
1279 if (x < text_left)
1281 if (lmargin_width > 0
1282 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1283 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1284 : (x < left_x + lmargin_width)))
1285 return ON_LEFT_MARGIN;
1286 else
1287 return ON_LEFT_FRINGE;
1290 if (x >= text_right)
1292 if (rmargin_width > 0
1293 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1294 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1295 : (x >= right_x - rmargin_width)))
1296 return ON_RIGHT_MARGIN;
1297 else
1298 return ON_RIGHT_FRINGE;
1301 /* Everything special ruled out - must be on text area */
1302 return ON_TEXT;
1305 /* Take X is the frame-relative pixel x-coordinate, and return the
1306 x-coordinate relative to part PART of window W. */
1308 window_relative_x_coord (struct window *w, enum window_part part, int x)
1310 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1312 switch (part)
1314 case ON_TEXT:
1315 return x - window_box_left (w, TEXT_AREA);
1317 case ON_HEADER_LINE:
1318 case ON_MODE_LINE:
1319 case ON_LEFT_FRINGE:
1320 return x - left_x;
1322 case ON_RIGHT_FRINGE:
1323 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1325 case ON_LEFT_MARGIN:
1326 return (x - left_x
1327 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1328 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1330 case ON_RIGHT_MARGIN:
1331 return (x + 1
1332 - ((w->pseudo_window_p)
1333 ? WINDOW_PIXEL_WIDTH (w)
1334 : WINDOW_BOX_RIGHT_EDGE_X (w))
1335 + window_box_width (w, RIGHT_MARGIN_AREA)
1336 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1337 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1339 case ON_NOTHING:
1340 case ON_VERTICAL_BORDER:
1341 case ON_VERTICAL_SCROLL_BAR:
1342 case ON_HORIZONTAL_SCROLL_BAR:
1343 case ON_RIGHT_DIVIDER:
1344 case ON_BOTTOM_DIVIDER:
1345 return 0;
1347 default:
1348 emacs_abort ();
1353 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1354 Scoordinates_in_window_p, 2, 2, 0,
1355 doc: /* Return non-nil if COORDINATES are in WINDOW.
1356 WINDOW must be a live window and defaults to the selected one.
1357 COORDINATES is a cons of the form (X . Y), X and Y being distances
1358 measured in characters from the upper-left corner of the frame.
1359 \(0 . 0) denotes the character in the upper left corner of the
1360 frame.
1361 If COORDINATES are in the text portion of WINDOW,
1362 the coordinates relative to the window are returned.
1363 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1364 If they are in the right divider of WINDOW, `right-divider' is returned.
1365 If they are in the mode line of WINDOW, `mode-line' is returned.
1366 If they are in the header line of WINDOW, `header-line' is returned.
1367 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1368 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1369 If they are on the border between WINDOW and its right sibling,
1370 `vertical-line' is returned.
1371 If they are in the windows's left or right marginal areas, `left-margin'\n\
1372 or `right-margin' is returned. */)
1373 (register Lisp_Object coordinates, Lisp_Object window)
1375 struct window *w;
1376 struct frame *f;
1377 int x, y;
1378 Lisp_Object lx, ly;
1380 w = decode_live_window (window);
1381 f = XFRAME (w->frame);
1382 CHECK_CONS (coordinates);
1383 lx = Fcar (coordinates);
1384 ly = Fcdr (coordinates);
1385 CHECK_NUMBER_OR_FLOAT (lx);
1386 CHECK_NUMBER_OR_FLOAT (ly);
1387 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1388 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1390 switch (coordinates_in_window (w, x, y))
1392 case ON_NOTHING:
1393 return Qnil;
1395 case ON_TEXT:
1396 /* Convert X and Y to window relative pixel coordinates, and
1397 return the canonical char units. */
1398 x -= window_box_left (w, TEXT_AREA);
1399 y -= WINDOW_TOP_EDGE_Y (w);
1400 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1401 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1403 case ON_MODE_LINE:
1404 return Qmode_line;
1406 case ON_VERTICAL_BORDER:
1407 return Qvertical_line;
1409 case ON_HEADER_LINE:
1410 return Qheader_line;
1412 case ON_LEFT_FRINGE:
1413 return Qleft_fringe;
1415 case ON_RIGHT_FRINGE:
1416 return Qright_fringe;
1418 case ON_LEFT_MARGIN:
1419 return Qleft_margin;
1421 case ON_RIGHT_MARGIN:
1422 return Qright_margin;
1424 case ON_VERTICAL_SCROLL_BAR:
1425 /* Historically we are supposed to return nil in this case. */
1426 return Qnil;
1428 case ON_HORIZONTAL_SCROLL_BAR:
1429 return Qnil;
1431 case ON_RIGHT_DIVIDER:
1432 return Qright_divider;
1434 case ON_BOTTOM_DIVIDER:
1435 return Qbottom_divider;
1437 default:
1438 emacs_abort ();
1443 /* Callback for foreach_window, used in window_from_coordinates.
1444 Check if window W contains coordinates specified by USER_DATA which
1445 is actually a pointer to a struct check_window_data CW.
1447 Check if window W contains coordinates *CW->x and *CW->y. If it
1448 does, return W in *CW->window, as Lisp_Object, and return in
1449 *CW->part the part of the window under coordinates *X,*Y. Return
1450 false from this function to stop iterating over windows. */
1452 struct check_window_data
1454 Lisp_Object *window;
1455 int x, y;
1456 enum window_part *part;
1459 static bool
1460 check_window_containing (struct window *w, void *user_data)
1462 struct check_window_data *cw = user_data;
1463 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1464 if (found == ON_NOTHING)
1465 return true;
1466 else
1468 *cw->part = found;
1469 XSETWINDOW (*cw->window, w);
1470 return false;
1475 /* Find the window containing frame-relative pixel position X/Y and
1476 return it as a Lisp_Object.
1478 If X, Y is on one of the window's special `window_part' elements,
1479 set *PART to the id of that element.
1481 If there is no window under X, Y return nil and leave *PART
1482 unmodified. TOOL_BAR_P means detect tool-bar windows.
1484 This function was previously implemented with a loop cycling over
1485 windows with Fnext_window, and starting with the frame's selected
1486 window. It turned out that this doesn't work with an
1487 implementation of next_window using Vwindow_list, because
1488 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1489 tree of F when this function is called asynchronously from
1490 note_mouse_highlight. The original loop didn't terminate in this
1491 case. */
1493 Lisp_Object
1494 window_from_coordinates (struct frame *f, int x, int y,
1495 enum window_part *part, bool tool_bar_p)
1497 Lisp_Object window;
1498 struct check_window_data cw;
1499 enum window_part dummy;
1501 if (part == 0)
1502 part = &dummy;
1504 window = Qnil;
1505 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1506 foreach_window (f, check_window_containing, &cw);
1508 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1509 /* If not found above, see if it's in the tool bar window, if a tool
1510 bar exists. */
1511 if (NILP (window)
1512 && tool_bar_p
1513 && WINDOWP (f->tool_bar_window)
1514 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1515 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1516 != ON_NOTHING))
1518 *part = ON_TEXT;
1519 window = f->tool_bar_window;
1521 #endif
1523 return window;
1526 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1527 doc: /* Return window containing coordinates X and Y on FRAME.
1528 FRAME must be a live frame and defaults to the selected one.
1529 The top left corner of the frame is considered to be row 0,
1530 column 0. */)
1531 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1533 struct frame *f = decode_live_frame (frame);
1535 /* Check that arguments are integers or floats. */
1536 CHECK_NUMBER_OR_FLOAT (x);
1537 CHECK_NUMBER_OR_FLOAT (y);
1539 return window_from_coordinates (f,
1540 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1541 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1542 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1543 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1544 0, false);
1547 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1548 doc: /* Return current value of point in WINDOW.
1549 WINDOW must be a live window and defaults to the selected one.
1551 For a nonselected window, this is the value point would have if that
1552 window were selected.
1554 Note that, when WINDOW is selected, the value returned is the same as
1555 that returned by `point' for WINDOW's buffer. It would be more strictly
1556 correct to return the top-level value of `point', outside of any
1557 `save-excursion' forms. But that is hard to define. */)
1558 (Lisp_Object window)
1560 register struct window *w = decode_live_window (window);
1562 if (w == XWINDOW (selected_window))
1563 return make_number (BUF_PT (XBUFFER (w->contents)));
1564 else
1565 return Fmarker_position (w->pointm);
1568 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1569 doc: /* Return old value of point in WINDOW.
1570 WINDOW must be a live window and defaults to the selected one. */)
1571 (Lisp_Object window)
1573 return Fmarker_position (decode_live_window (window)->old_pointm);
1576 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1577 doc: /* Return position at which display currently starts in WINDOW.
1578 WINDOW must be a live window and defaults to the selected one.
1579 This is updated by redisplay or by calling `set-window-start'. */)
1580 (Lisp_Object window)
1582 return Fmarker_position (decode_live_window (window)->start);
1585 /* This is text temporarily removed from the doc string below.
1587 This function returns nil if the position is not currently known.
1588 That happens when redisplay is preempted and doesn't finish.
1589 If in that case you want to compute where the end of the window would
1590 have been if redisplay had finished, do this:
1591 (save-excursion
1592 (goto-char (window-start window))
1593 (vertical-motion (1- (window-height window)) window)
1594 (point))") */
1596 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1597 doc: /* Return position at which display currently ends in WINDOW.
1598 WINDOW must be a live window and defaults to the selected one.
1599 This is updated by redisplay, when it runs to completion.
1600 Simply changing the buffer text or setting `window-start'
1601 does not update this value.
1602 Return nil if there is no recorded value. (This can happen if the
1603 last redisplay of WINDOW was preempted, and did not finish.)
1604 If UPDATE is non-nil, compute the up-to-date position
1605 if it isn't already recorded. */)
1606 (Lisp_Object window, Lisp_Object update)
1608 Lisp_Object value;
1609 struct window *w = decode_live_window (window);
1610 Lisp_Object buf;
1611 struct buffer *b;
1613 buf = w->contents;
1614 CHECK_BUFFER (buf);
1615 b = XBUFFER (buf);
1617 if (! NILP (update)
1618 && (windows_or_buffers_changed
1619 || !w->window_end_valid
1620 || b->clip_changed
1621 || b->prevent_redisplay_optimizations_p
1622 || window_outdated (w))
1623 /* Don't call display routines if we didn't yet create any real
1624 frames, because the glyph matrices are not yet allocated in
1625 that case. This could happen in some code that runs in the
1626 daemon during initialization (e.g., see bug#20565). */
1627 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1629 struct text_pos startp;
1630 struct it it;
1631 struct buffer *old_buffer = NULL;
1632 void *itdata = NULL;
1634 /* Cannot use Fvertical_motion because that function doesn't
1635 cope with variable-height lines. */
1636 if (b != current_buffer)
1638 old_buffer = current_buffer;
1639 set_buffer_internal (b);
1642 /* In case W->start is out of the range, use something
1643 reasonable. This situation occurred when loading a file with
1644 `-l' containing a call to `rmail' with subsequent other
1645 commands. At the end, W->start happened to be BEG, while
1646 rmail had already narrowed the buffer. */
1647 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1649 itdata = bidi_shelve_cache ();
1650 start_display (&it, w, startp);
1651 move_it_vertically (&it, window_box_height (w));
1652 if (it.current_y < it.last_visible_y)
1653 move_it_past_eol (&it);
1654 value = make_number (IT_CHARPOS (it));
1655 bidi_unshelve_cache (itdata, false);
1657 if (old_buffer)
1658 set_buffer_internal (old_buffer);
1660 else
1661 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1663 return value;
1666 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1667 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1668 WINDOW must be a live window and defaults to the selected one.
1669 Return POS. */)
1670 (Lisp_Object window, Lisp_Object pos)
1672 register struct window *w = decode_live_window (window);
1674 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1676 if (w == XWINDOW (selected_window))
1678 if (XBUFFER (w->contents) == current_buffer)
1679 Fgoto_char (pos);
1680 else
1682 struct buffer *old_buffer = current_buffer;
1684 /* ... but here we want to catch type error before buffer change. */
1685 CHECK_NUMBER_COERCE_MARKER (pos);
1686 set_buffer_internal (XBUFFER (w->contents));
1687 Fgoto_char (pos);
1688 set_buffer_internal (old_buffer);
1691 else
1693 set_marker_restricted (w->pointm, pos, w->contents);
1694 /* We have to make sure that redisplay updates the window to show
1695 the new value of point. */
1696 wset_redisplay (w);
1699 return pos;
1702 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1703 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1704 WINDOW must be a live window and defaults to the selected one. Return
1705 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1706 overriding motion of point in order to display at this exact start. */)
1707 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1709 register struct window *w = decode_live_window (window);
1711 set_marker_restricted (w->start, pos, w->contents);
1712 /* This is not right, but much easier than doing what is right. */
1713 w->start_at_line_beg = false;
1714 if (NILP (noforce))
1715 w->force_start = true;
1716 wset_update_mode_line (w);
1717 /* Bug#15957. */
1718 w->window_end_valid = false;
1719 wset_redisplay (w);
1721 return pos;
1724 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1725 Spos_visible_in_window_p, 0, 3, 0,
1726 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1727 WINDOW must be a live window and defaults to the selected one.
1729 Return nil if that position is scrolled vertically out of view. If a
1730 character is only partially visible, nil is returned, unless the
1731 optional argument PARTIALLY is non-nil. If POS is only out of view
1732 because of horizontal scrolling, return non-nil. If POS is t, it
1733 specifies either the first position displayed on the last visible
1734 screen line in WINDOW, or the end-of-buffer position, whichever comes
1735 first. POS defaults to point in WINDOW; WINDOW defaults to the
1736 selected window.
1738 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1739 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1740 where X and Y are the pixel coordinates relative to the top left corner
1741 of the window. The remaining elements are omitted if the character after
1742 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1743 off-window at the top and bottom of the screen line ("row") containing
1744 POS, ROWH is the visible height of that row, and VPOS is the row number
1745 \(zero-based). */)
1746 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1748 struct window *w;
1749 EMACS_INT posint;
1750 struct buffer *buf;
1751 struct text_pos top;
1752 Lisp_Object in_window = Qnil;
1753 int rtop, rbot, rowh, vpos;
1754 bool fully_p = true;
1755 int x, y;
1757 w = decode_live_window (window);
1758 buf = XBUFFER (w->contents);
1759 SET_TEXT_POS_FROM_MARKER (top, w->start);
1761 if (EQ (pos, Qt))
1762 posint = -1;
1763 else if (!NILP (pos))
1765 CHECK_NUMBER_COERCE_MARKER (pos);
1766 posint = XINT (pos);
1768 else if (w == XWINDOW (selected_window))
1769 posint = PT;
1770 else
1771 posint = marker_position (w->pointm);
1773 /* If position is above window start or outside buffer boundaries,
1774 or if window start is out of range, position is not visible. */
1775 if ((EQ (pos, Qt)
1776 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1777 && CHARPOS (top) >= BUF_BEGV (buf)
1778 && CHARPOS (top) <= BUF_ZV (buf)
1779 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1781 fully_p = !rtop && !rbot;
1782 if (!NILP (partially) || fully_p)
1783 in_window = Qt;
1786 if (!NILP (in_window) && !NILP (partially))
1788 Lisp_Object part = Qnil;
1789 if (!fully_p)
1790 part = list4i (rtop, rbot, rowh, vpos);
1791 in_window = Fcons (make_number (x),
1792 Fcons (make_number (y), part));
1795 return in_window;
1798 DEFUN ("window-line-height", Fwindow_line_height,
1799 Swindow_line_height, 0, 2, 0,
1800 doc: /* Return height in pixels of text line LINE in window WINDOW.
1801 WINDOW must be a live window and defaults to the selected one.
1803 Return height of current line if LINE is omitted or nil. Return height of
1804 header or mode line if LINE is `header-line' or `mode-line'.
1805 Otherwise, LINE is a text line number starting from 0. A negative number
1806 counts from the end of the window.
1808 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1809 in pixels of the visible part of the line, VPOS and YPOS are the
1810 vertical position in lines and pixels of the line, relative to the top
1811 of the first text line, and OFFBOT is the number of off-window pixels at
1812 the bottom of the text line. If there are off-window pixels at the top
1813 of the (first) text line, YPOS is negative.
1815 Return nil if window display is not up-to-date. In that case, use
1816 `pos-visible-in-window-p' to obtain the information. */)
1817 (Lisp_Object line, Lisp_Object window)
1819 register struct window *w;
1820 register struct buffer *b;
1821 struct glyph_row *row, *end_row;
1822 int max_y, crop, i;
1823 EMACS_INT n;
1825 w = decode_live_window (window);
1827 if (noninteractive || w->pseudo_window_p)
1828 return Qnil;
1830 CHECK_BUFFER (w->contents);
1831 b = XBUFFER (w->contents);
1833 /* Fail if current matrix is not up-to-date. */
1834 if (!w->window_end_valid
1835 || windows_or_buffers_changed
1836 || b->clip_changed
1837 || b->prevent_redisplay_optimizations_p
1838 || window_outdated (w))
1839 return Qnil;
1841 if (NILP (line))
1843 i = w->cursor.vpos;
1844 if (i < 0 || i >= w->current_matrix->nrows
1845 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1846 return Qnil;
1847 max_y = window_text_bottom_y (w);
1848 goto found_row;
1851 if (EQ (line, Qheader_line))
1853 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1854 return Qnil;
1855 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1856 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1859 if (EQ (line, Qmode_line))
1861 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1862 return (row->enabled_p ?
1863 list4i (row->height,
1864 0, /* not accurate */
1865 (WINDOW_HEADER_LINE_HEIGHT (w)
1866 + window_text_bottom_y (w)),
1868 : Qnil);
1871 CHECK_NUMBER (line);
1872 n = XINT (line);
1874 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1875 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1876 max_y = window_text_bottom_y (w);
1877 i = 0;
1879 while ((n < 0 || i < n)
1880 && row <= end_row && row->enabled_p
1881 && row->y + row->height < max_y)
1882 row++, i++;
1884 if (row > end_row || !row->enabled_p)
1885 return Qnil;
1887 if (++n < 0)
1889 if (-n > i)
1890 return Qnil;
1891 row += n;
1892 i += n;
1895 found_row:
1896 crop = max (0, (row->y + row->height) - max_y);
1897 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1900 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1901 0, 1, 0,
1902 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1903 More precisely, return the value assigned by the last call of
1904 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1905 never called with WINDOW as its argument, or the value set by that
1906 function was internally reset since its last call. WINDOW must be a
1907 live window and defaults to the selected one.
1909 When a window is dedicated to its buffer, `display-buffer' will refrain
1910 from displaying another buffer in it. `get-lru-window' and
1911 `get-largest-window' treat dedicated windows specially.
1912 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1913 `kill-buffer' can delete a dedicated window and the containing frame.
1915 Functions like `set-window-buffer' may change the buffer displayed by a
1916 window, unless that window is "strongly" dedicated to its buffer, that
1917 is the value returned by `window-dedicated-p' is t. */)
1918 (Lisp_Object window)
1920 return decode_live_window (window)->dedicated;
1923 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1924 Sset_window_dedicated_p, 2, 2, 0,
1925 doc: /* Mark WINDOW as dedicated according to FLAG.
1926 WINDOW must be a live window and defaults to the selected one. FLAG
1927 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1928 mark WINDOW as non-dedicated. Return FLAG.
1930 When a window is dedicated to its buffer, `display-buffer' will refrain
1931 from displaying another buffer in it. `get-lru-window' and
1932 `get-largest-window' treat dedicated windows specially.
1933 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1934 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1935 and the containing frame.
1937 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1938 its buffer. Functions like `set-window-buffer' may change the buffer
1939 displayed by a window, unless that window is strongly dedicated to its
1940 buffer. If and when `set-window-buffer' displays another buffer in a
1941 window, it also makes sure that the window is no more dedicated. */)
1942 (Lisp_Object window, Lisp_Object flag)
1944 wset_dedicated (decode_live_window (window), flag);
1945 return flag;
1948 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1949 0, 1, 0,
1950 doc: /* Return buffers previously shown in WINDOW.
1951 WINDOW must be a live window and defaults to the selected one.
1953 The return value is a list of elements (BUFFER WINDOW-START POS),
1954 where BUFFER is a buffer, WINDOW-START is the start position of the
1955 window for that buffer, and POS is a window-specific point value. */)
1956 (Lisp_Object window)
1958 return decode_live_window (window)->prev_buffers;
1961 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1962 Sset_window_prev_buffers, 2, 2, 0,
1963 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1964 WINDOW must be a live window and defaults to the selected one.
1966 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1967 where BUFFER is a buffer, WINDOW-START is the start position of the
1968 window for that buffer, and POS is a window-specific point value. */)
1969 (Lisp_Object window, Lisp_Object prev_buffers)
1971 wset_prev_buffers (decode_live_window (window), prev_buffers);
1972 return prev_buffers;
1975 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1976 0, 1, 0,
1977 doc: /* Return list of buffers recently re-shown in WINDOW.
1978 WINDOW must be a live window and defaults to the selected one. */)
1979 (Lisp_Object window)
1981 return decode_live_window (window)->next_buffers;
1984 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1985 Sset_window_next_buffers, 2, 2, 0,
1986 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1987 WINDOW must be a live window and defaults to the selected one.
1988 NEXT-BUFFERS should be a list of buffers. */)
1989 (Lisp_Object window, Lisp_Object next_buffers)
1991 wset_next_buffers (decode_live_window (window), next_buffers);
1992 return next_buffers;
1995 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1996 0, 1, 0,
1997 doc: /* Return the parameters of WINDOW and their values.
1998 WINDOW must be a valid window and defaults to the selected one. The
1999 return value is a list of elements of the form (PARAMETER . VALUE). */)
2000 (Lisp_Object window)
2002 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2005 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2006 2, 2, 0,
2007 doc: /* Return WINDOW's value for PARAMETER.
2008 WINDOW can be any window and defaults to the selected one. */)
2009 (Lisp_Object window, Lisp_Object parameter)
2011 Lisp_Object result;
2013 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2014 return CDR_SAFE (result);
2017 DEFUN ("set-window-parameter", Fset_window_parameter,
2018 Sset_window_parameter, 3, 3, 0,
2019 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2020 WINDOW can be any window and defaults to the selected one.
2021 Return VALUE. */)
2022 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2024 register struct window *w = decode_any_window (window);
2025 Lisp_Object old_alist_elt;
2027 old_alist_elt = Fassq (parameter, w->window_parameters);
2028 if (NILP (old_alist_elt))
2029 wset_window_parameters
2030 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2031 else
2032 Fsetcdr (old_alist_elt, value);
2033 return value;
2036 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2037 0, 1, 0,
2038 doc: /* Return the display-table that WINDOW is using.
2039 WINDOW must be a live window and defaults to the selected one. */)
2040 (Lisp_Object window)
2042 return decode_live_window (window)->display_table;
2045 /* Get the display table for use on window W. This is either W's
2046 display table or W's buffer's display table. Ignore the specified
2047 tables if they are not valid; if no valid table is specified,
2048 return 0. */
2050 struct Lisp_Char_Table *
2051 window_display_table (struct window *w)
2053 struct Lisp_Char_Table *dp = NULL;
2055 if (DISP_TABLE_P (w->display_table))
2056 dp = XCHAR_TABLE (w->display_table);
2057 else if (BUFFERP (w->contents))
2059 struct buffer *b = XBUFFER (w->contents);
2061 if (DISP_TABLE_P (BVAR (b, display_table)))
2062 dp = XCHAR_TABLE (BVAR (b, display_table));
2063 else if (DISP_TABLE_P (Vstandard_display_table))
2064 dp = XCHAR_TABLE (Vstandard_display_table);
2067 return dp;
2070 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2071 doc: /* Set WINDOW's display-table to TABLE.
2072 WINDOW must be a live window and defaults to the selected one. */)
2073 (register Lisp_Object window, Lisp_Object table)
2075 wset_display_table (decode_live_window (window), table);
2076 return table;
2079 /* Record info on buffer window W is displaying
2080 when it is about to cease to display that buffer. */
2081 static void
2082 unshow_buffer (register struct window *w)
2084 Lisp_Object buf = w->contents;
2085 struct buffer *b = XBUFFER (buf);
2087 eassert (b == XMARKER (w->pointm)->buffer);
2089 #if false
2090 if (w == XWINDOW (selected_window)
2091 || ! EQ (buf, XWINDOW (selected_window)->contents))
2092 /* Do this except when the selected window's buffer
2093 is being removed from some other window. */
2094 #endif
2095 /* last_window_start records the start position that this buffer
2096 had in the last window to be disconnected from it.
2097 Now that this statement is unconditional,
2098 it is possible for the buffer to be displayed in the
2099 selected window, while last_window_start reflects another
2100 window which was recently showing the same buffer.
2101 Some people might say that might be a good thing. Let's see. */
2102 b->last_window_start = marker_position (w->start);
2104 /* Point in the selected window's buffer
2105 is actually stored in that buffer, and the window's pointm isn't used.
2106 So don't clobber point in that buffer. */
2107 if (! EQ (buf, XWINDOW (selected_window)->contents)
2108 /* Don't clobber point in current buffer either (this could be
2109 useful in connection with bug#12208).
2110 && XBUFFER (buf) != current_buffer */
2111 /* This line helps to fix Horsley's testbug.el bug. */
2112 && !(WINDOWP (BVAR (b, last_selected_window))
2113 && w != XWINDOW (BVAR (b, last_selected_window))
2114 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2115 temp_set_point_both (b,
2116 clip_to_bounds (BUF_BEGV (b),
2117 marker_position (w->pointm),
2118 BUF_ZV (b)),
2119 clip_to_bounds (BUF_BEGV_BYTE (b),
2120 marker_byte_position (w->pointm),
2121 BUF_ZV_BYTE (b)));
2123 if (WINDOWP (BVAR (b, last_selected_window))
2124 && w == XWINDOW (BVAR (b, last_selected_window)))
2125 bset_last_selected_window (b, Qnil);
2128 /* Put NEW into the window structure in place of OLD. SETFLAG false
2129 means change window structure only. Otherwise store geometry and
2130 other settings as well. */
2131 static void
2132 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2134 Lisp_Object tem;
2135 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2137 /* If OLD is its frame's root window, then NEW is the new
2138 root window for that frame. */
2139 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2140 fset_root_window (XFRAME (o->frame), new);
2142 if (setflag)
2144 n->pixel_left = o->pixel_left;
2145 n->pixel_top = o->pixel_top;
2146 n->pixel_width = o->pixel_width;
2147 n->pixel_height = o->pixel_height;
2148 n->left_col = o->left_col;
2149 n->top_line = o->top_line;
2150 n->total_cols = o->total_cols;
2151 n->total_lines = o->total_lines;
2152 wset_normal_cols (n, o->normal_cols);
2153 wset_normal_cols (o, make_float (1.0));
2154 wset_normal_lines (n, o->normal_lines);
2155 wset_normal_lines (o, make_float (1.0));
2156 n->desired_matrix = n->current_matrix = 0;
2157 n->vscroll = 0;
2158 memset (&n->cursor, 0, sizeof (n->cursor));
2159 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2160 n->last_cursor_vpos = 0;
2161 #ifdef HAVE_WINDOW_SYSTEM
2162 n->phys_cursor_type = NO_CURSOR;
2163 n->phys_cursor_width = -1;
2164 #endif
2165 n->must_be_updated_p = false;
2166 n->pseudo_window_p = false;
2167 n->window_end_vpos = 0;
2168 n->window_end_pos = 0;
2169 n->window_end_valid = false;
2172 tem = o->next;
2173 wset_next (n, tem);
2174 if (!NILP (tem))
2175 wset_prev (XWINDOW (tem), new);
2177 tem = o->prev;
2178 wset_prev (n, tem);
2179 if (!NILP (tem))
2180 wset_next (XWINDOW (tem), new);
2182 tem = o->parent;
2183 wset_parent (n, tem);
2184 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2185 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2188 /* If window WINDOW and its parent window are iso-combined, merge
2189 WINDOW's children into those of its parent window and mark WINDOW as
2190 deleted. */
2192 static void
2193 recombine_windows (Lisp_Object window)
2195 struct window *w, *p, *c;
2196 Lisp_Object parent, child;
2197 bool horflag;
2199 w = XWINDOW (window);
2200 parent = w->parent;
2201 if (!NILP (parent) && NILP (w->combination_limit))
2203 p = XWINDOW (parent);
2204 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2205 && p->horizontal == w->horizontal)
2206 /* WINDOW and PARENT are both either a vertical or a horizontal
2207 combination. */
2209 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2210 child = w->contents;
2211 c = XWINDOW (child);
2213 /* Splice WINDOW's children into its parent's children and
2214 assign new normal sizes. */
2215 if (NILP (w->prev))
2216 wset_combination (p, horflag, child);
2217 else
2219 wset_prev (c, w->prev);
2220 wset_next (XWINDOW (w->prev), child);
2223 while (c)
2225 wset_parent (c, parent);
2227 if (horflag)
2228 wset_normal_cols
2229 (c, make_float ((double) c->pixel_width
2230 / (double) p->pixel_width));
2231 else
2232 wset_normal_lines
2233 (c, make_float ((double) c->pixel_height
2234 / (double) p->pixel_height));
2236 if (NILP (c->next))
2238 if (!NILP (w->next))
2240 wset_next (c, w->next);
2241 wset_prev (XWINDOW (c->next), child);
2244 c = 0;
2246 else
2248 child = c->next;
2249 c = XWINDOW (child);
2253 /* WINDOW can be deleted now. */
2254 wset_combination (w, false, Qnil);
2259 /* If WINDOW can be deleted, delete it. */
2260 static void
2261 delete_deletable_window (Lisp_Object window)
2263 if (!NILP (call1 (Qwindow_deletable_p, window)))
2264 call1 (Qdelete_window, window);
2267 /***********************************************************************
2268 Window List
2269 ***********************************************************************/
2271 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2272 pointer. This is a callback function for foreach_window, used in
2273 the window_list function. */
2275 static bool
2276 add_window_to_list (struct window *w, void *user_data)
2278 Lisp_Object *list = user_data;
2279 Lisp_Object window;
2280 XSETWINDOW (window, w);
2281 *list = Fcons (window, *list);
2282 return true;
2286 /* Return a list of all windows, for use by next_window. If
2287 Vwindow_list is a list, return that list. Otherwise, build a new
2288 list, cache it in Vwindow_list, and return that. */
2290 Lisp_Object
2291 window_list (void)
2293 if (!CONSP (Vwindow_list))
2295 Lisp_Object tail, frame;
2297 Vwindow_list = Qnil;
2298 FOR_EACH_FRAME (tail, frame)
2300 Lisp_Object arglist = Qnil;
2302 /* We are visiting windows in canonical order, and add
2303 new windows at the front of args[1], which means we
2304 have to reverse this list at the end. */
2305 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2306 arglist = Fnreverse (arglist);
2307 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2311 return Vwindow_list;
2315 /* Value is true if WINDOW satisfies the constraints given by
2316 OWINDOW, MINIBUF and ALL_FRAMES.
2318 MINIBUF t means WINDOW may be minibuffer windows.
2319 `lambda' means WINDOW may not be a minibuffer window.
2320 a window means a specific minibuffer window
2322 ALL_FRAMES t means search all frames,
2323 nil means search just current frame,
2324 `visible' means search just visible frames on the
2325 current terminal,
2326 0 means search visible and iconified frames on the
2327 current terminal,
2328 a window means search the frame that window belongs to,
2329 a frame means consider windows on that frame, only. */
2331 static bool
2332 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2333 Lisp_Object minibuf, Lisp_Object all_frames)
2335 struct window *w = XWINDOW (window);
2336 struct frame *f = XFRAME (w->frame);
2337 bool candidate_p = true;
2339 if (!BUFFERP (w->contents))
2340 candidate_p = false;
2341 else if (MINI_WINDOW_P (w)
2342 && (EQ (minibuf, Qlambda)
2343 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2345 /* If MINIBUF is `lambda' don't consider any mini-windows.
2346 If it is a window, consider only that one. */
2347 candidate_p = false;
2349 else if (EQ (all_frames, Qt))
2350 candidate_p = true;
2351 else if (NILP (all_frames))
2353 eassert (WINDOWP (owindow));
2354 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2356 else if (EQ (all_frames, Qvisible))
2358 candidate_p = FRAME_VISIBLE_P (f)
2359 && (FRAME_TERMINAL (XFRAME (w->frame))
2360 == FRAME_TERMINAL (XFRAME (selected_frame)));
2363 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2365 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2366 #ifdef HAVE_X_WINDOWS
2367 /* Yuck!! If we've just created the frame and the
2368 window-manager requested the user to place it
2369 manually, the window may still not be considered
2370 `visible'. I'd argue it should be at least
2371 something like `iconified', but don't know how to do
2372 that yet. --Stef */
2373 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2374 && !f->output_data.x->has_been_visible)
2375 #endif
2377 && (FRAME_TERMINAL (XFRAME (w->frame))
2378 == FRAME_TERMINAL (XFRAME (selected_frame)));
2380 else if (WINDOWP (all_frames))
2381 /* To qualify as candidate, it's not sufficient for WINDOW's frame
2382 to just share the minibuffer window - it must be active as well
2383 (see Bug#24500). */
2384 candidate_p = (EQ (XWINDOW (all_frames)->frame, w->frame)
2385 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2386 else if (FRAMEP (all_frames))
2387 candidate_p = EQ (all_frames, w->frame);
2389 return candidate_p;
2393 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2394 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2395 MINIBUF, and ALL_FRAMES. */
2397 static void
2398 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2400 struct window *w = decode_live_window (*window);
2402 XSETWINDOW (*window, w);
2403 /* MINIBUF nil may or may not include minibuffers. Decide if it
2404 does. */
2405 if (NILP (*minibuf))
2406 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2407 else if (!EQ (*minibuf, Qt))
2408 *minibuf = Qlambda;
2410 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2411 => count none of them, or a specific minibuffer window (the
2412 active one) to count. */
2414 /* ALL_FRAMES nil doesn't specify which frames to include. */
2415 if (NILP (*all_frames))
2416 *all_frames
2417 = (!EQ (*minibuf, Qlambda)
2418 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2419 : Qnil);
2420 else if (EQ (*all_frames, Qvisible))
2422 else if (EQ (*all_frames, make_number (0)))
2424 else if (FRAMEP (*all_frames))
2426 else if (!EQ (*all_frames, Qt))
2427 *all_frames = Qnil;
2431 /* Return the next or previous window of WINDOW in cyclic ordering
2432 of windows. NEXT_P means return the next window. See the
2433 documentation string of next-window for the meaning of MINIBUF and
2434 ALL_FRAMES. */
2436 static Lisp_Object
2437 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2438 bool next_p)
2440 decode_next_window_args (&window, &minibuf, &all_frames);
2442 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2443 return the first window on the frame. */
2444 if (FRAMEP (all_frames)
2445 && !EQ (all_frames, XWINDOW (window)->frame))
2446 return Fframe_first_window (all_frames);
2448 if (next_p)
2450 Lisp_Object list;
2452 /* Find WINDOW in the list of all windows. */
2453 list = Fmemq (window, window_list ());
2455 /* Scan forward from WINDOW to the end of the window list. */
2456 if (CONSP (list))
2457 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2458 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2459 break;
2461 /* Scan from the start of the window list up to WINDOW. */
2462 if (!CONSP (list))
2463 for (list = Vwindow_list;
2464 CONSP (list) && !EQ (XCAR (list), window);
2465 list = XCDR (list))
2466 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2467 break;
2469 if (CONSP (list))
2470 window = XCAR (list);
2472 else
2474 Lisp_Object candidate, list;
2476 /* Scan through the list of windows for candidates. If there are
2477 candidate windows in front of WINDOW, the last one of these
2478 is the one we want. If there are candidates following WINDOW
2479 in the list, again the last one of these is the one we want. */
2480 candidate = Qnil;
2481 for (list = window_list (); CONSP (list); list = XCDR (list))
2483 if (EQ (XCAR (list), window))
2485 if (WINDOWP (candidate))
2486 break;
2488 else if (candidate_window_p (XCAR (list), window, minibuf,
2489 all_frames))
2490 candidate = XCAR (list);
2493 if (WINDOWP (candidate))
2494 window = candidate;
2497 return window;
2501 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2502 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2503 WINDOW must be a live window and defaults to the selected one. The
2504 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2505 consider.
2507 MINIBUF nil or omitted means consider the minibuffer window only if the
2508 minibuffer is active. MINIBUF t means consider the minibuffer window
2509 even if the minibuffer is not active. Any other value means do not
2510 consider the minibuffer window even if the minibuffer is active.
2512 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2513 plus the minibuffer window if specified by the MINIBUF argument. If the
2514 minibuffer counts, consider all windows on all frames that share that
2515 minibuffer too. The following non-nil values of ALL-FRAMES have special
2516 meanings:
2518 - t means consider all windows on all existing frames.
2520 - `visible' means consider all windows on all visible frames.
2522 - 0 (the number zero) means consider all windows on all visible and
2523 iconified frames.
2525 - A frame means consider all windows on that frame only.
2527 Anything else means consider all windows on WINDOW's frame and no
2528 others.
2530 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2531 `next-window' to iterate through the entire cycle of acceptable
2532 windows, eventually ending up back at the window you started with.
2533 `previous-window' traverses the same cycle, in the reverse order. */)
2534 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2536 return next_window (window, minibuf, all_frames, true);
2540 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2541 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2542 WINDOW must be a live window and defaults to the selected one. The
2543 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2544 consider.
2546 MINIBUF nil or omitted means consider the minibuffer window only if the
2547 minibuffer is active. MINIBUF t means consider the minibuffer window
2548 even if the minibuffer is not active. Any other value means do not
2549 consider the minibuffer window even if the minibuffer is active.
2551 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2552 plus the minibuffer window if specified by the MINIBUF argument. If the
2553 minibuffer counts, consider all windows on all frames that share that
2554 minibuffer too. The following non-nil values of ALL-FRAMES have special
2555 meanings:
2557 - t means consider all windows on all existing frames.
2559 - `visible' means consider all windows on all visible frames.
2561 - 0 (the number zero) means consider all windows on all visible and
2562 iconified frames.
2564 - A frame means consider all windows on that frame only.
2566 Anything else means consider all windows on WINDOW's frame and no
2567 others.
2569 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2570 use `previous-window' to iterate through the entire cycle of
2571 acceptable windows, eventually ending up back at the window you
2572 started with. `next-window' traverses the same cycle, in the
2573 reverse order. */)
2574 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2576 return next_window (window, minibuf, all_frames, false);
2580 /* Return a list of windows in cyclic ordering. Arguments are like
2581 for `next-window'. */
2583 static Lisp_Object
2584 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2586 Lisp_Object tail, list, rest;
2588 decode_next_window_args (&window, &minibuf, &all_frames);
2589 list = Qnil;
2591 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2592 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2593 list = Fcons (XCAR (tail), list);
2595 /* Rotate the list to start with WINDOW. */
2596 list = Fnreverse (list);
2597 rest = Fmemq (window, list);
2598 if (!NILP (rest) && !EQ (rest, list))
2600 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2602 XSETCDR (tail, Qnil);
2603 list = nconc2 (rest, list);
2605 return list;
2609 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2610 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2611 FRAME nil or omitted means use the selected frame.
2612 WINDOW nil or omitted means use the window selected within FRAME.
2613 MINIBUF t means include the minibuffer window, even if it isn't active.
2614 MINIBUF nil or omitted means include the minibuffer window only
2615 if it's active.
2616 MINIBUF neither nil nor t means never include the minibuffer window. */)
2617 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2619 if (NILP (window))
2620 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2621 CHECK_WINDOW (window);
2622 if (NILP (frame))
2623 frame = selected_frame;
2625 if (!EQ (frame, XWINDOW (window)->frame))
2626 error ("Window is on a different frame");
2628 return window_list_1 (window, minibuf, frame);
2632 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2633 doc: /* Return a list of all live windows.
2634 WINDOW specifies the first window to list and defaults to the selected
2635 window.
2637 Optional argument MINIBUF nil or omitted means consider the minibuffer
2638 window only if the minibuffer is active. MINIBUF t means consider the
2639 minibuffer window even if the minibuffer is not active. Any other value
2640 means do not consider the minibuffer window even if the minibuffer is
2641 active.
2643 Optional argument ALL-FRAMES nil or omitted means consider all windows
2644 on WINDOW's frame, plus the minibuffer window if specified by the
2645 MINIBUF argument. If the minibuffer counts, consider all windows on all
2646 frames that share that minibuffer too. The following non-nil values of
2647 ALL-FRAMES have special meanings:
2649 - t means consider all windows on all existing frames.
2651 - `visible' means consider all windows on all visible frames.
2653 - 0 (the number zero) means consider all windows on all visible and
2654 iconified frames.
2656 - A frame means consider all windows on that frame only.
2658 Anything else means consider all windows on WINDOW's frame and no
2659 others.
2661 If WINDOW is not on the list of windows returned, some other window will
2662 be listed first but no error is signaled. */)
2663 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2665 return window_list_1 (window, minibuf, all_frames);
2668 /* Look at all windows, performing an operation specified by TYPE
2669 with argument OBJ.
2670 If FRAMES is Qt, look at all frames;
2671 Qnil, look at just the selected frame;
2672 Qvisible, look at visible frames;
2673 a frame, just look at windows on that frame.
2674 If MINI, perform the operation on minibuffer windows too. */
2676 enum window_loop
2678 WINDOW_LOOP_UNUSED,
2679 GET_BUFFER_WINDOW, /* Arg is buffer */
2680 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2681 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2682 CHECK_ALL_WINDOWS /* Arg is ignored */
2685 static Lisp_Object
2686 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2687 Lisp_Object frames)
2689 Lisp_Object window, windows, best_window, frame_arg;
2690 bool frame_best_window_flag = false;
2691 struct frame *f;
2693 /* If we're only looping through windows on a particular frame,
2694 frame points to that frame. If we're looping through windows
2695 on all frames, frame is 0. */
2696 if (FRAMEP (frames))
2697 f = XFRAME (frames);
2698 else if (NILP (frames))
2699 f = SELECTED_FRAME ();
2700 else
2701 f = NULL;
2703 if (f)
2704 frame_arg = Qlambda;
2705 else if (EQ (frames, make_number (0)))
2706 frame_arg = frames;
2707 else if (EQ (frames, Qvisible))
2708 frame_arg = frames;
2709 else
2710 frame_arg = Qt;
2712 /* frame_arg is Qlambda to stick to one frame,
2713 Qvisible to consider all visible frames,
2714 or Qt otherwise. */
2716 /* Pick a window to start with. */
2717 if (WINDOWP (obj))
2718 window = obj;
2719 else if (f)
2720 window = FRAME_SELECTED_WINDOW (f);
2721 else
2722 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2724 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2725 best_window = Qnil;
2727 for (; CONSP (windows); windows = XCDR (windows))
2729 struct window *w;
2731 window = XCAR (windows);
2732 w = XWINDOW (window);
2734 /* Note that we do not pay attention here to whether the frame
2735 is visible, since Fwindow_list skips non-visible frames if
2736 that is desired, under the control of frame_arg. */
2737 if (!MINI_WINDOW_P (w)
2738 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2739 consider all windows. */
2740 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2741 || (mini && minibuf_level > 0))
2742 switch (type)
2744 case GET_BUFFER_WINDOW:
2745 if (EQ (w->contents, obj)
2746 /* Don't find any minibuffer window except the one that
2747 is currently in use. */
2748 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2750 if (EQ (window, selected_window))
2751 /* Preferably return the selected window. */
2752 return window;
2753 else if (EQ (XWINDOW (window)->frame, selected_frame)
2754 && !frame_best_window_flag)
2755 /* Prefer windows on the current frame (but don't
2756 choose another one if we have one already). */
2758 best_window = window;
2759 frame_best_window_flag = true;
2761 else if (NILP (best_window))
2762 best_window = window;
2764 break;
2766 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2767 /* We could simply check whether the buffer shown by window
2768 is live, and show another buffer in case it isn't. */
2769 if (EQ (w->contents, obj))
2771 /* Undedicate WINDOW. */
2772 wset_dedicated (w, Qnil);
2773 /* Make WINDOW show the buffer returned by
2774 other_buffer_safely, don't run any hooks. */
2775 set_window_buffer
2776 (window, other_buffer_safely (w->contents), false, false);
2777 /* If WINDOW is the selected window, make its buffer
2778 current. But do so only if the window shows the
2779 current buffer (Bug#6454). */
2780 if (EQ (window, selected_window)
2781 && XBUFFER (w->contents) == current_buffer)
2782 Fset_buffer (w->contents);
2784 break;
2786 case REDISPLAY_BUFFER_WINDOWS:
2787 if (EQ (w->contents, obj))
2789 mark_window_display_accurate (window, false);
2790 w->update_mode_line = true;
2791 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2792 update_mode_lines = 27;
2793 best_window = window;
2795 break;
2797 /* Check for a leaf window that has a killed buffer
2798 or broken markers. */
2799 case CHECK_ALL_WINDOWS:
2800 if (BUFFERP (w->contents))
2802 struct buffer *b = XBUFFER (w->contents);
2804 if (!BUFFER_LIVE_P (b))
2805 emacs_abort ();
2806 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2807 emacs_abort ();
2808 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2809 emacs_abort ();
2811 break;
2813 case WINDOW_LOOP_UNUSED:
2814 break;
2818 return best_window;
2821 /* Used for debugging. Abort if any window has a dead buffer. */
2823 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2824 void
2825 check_all_windows (void)
2827 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2830 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2831 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2832 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2833 the current buffer.
2835 The optional argument ALL-FRAMES specifies the frames to consider:
2837 - t means consider all windows on all existing frames.
2839 - `visible' means consider all windows on all visible frames.
2841 - 0 (the number zero) means consider all windows on all visible
2842 and iconified frames.
2844 - A frame means consider all windows on that frame only.
2846 Any other value of ALL-FRAMES means consider all windows on the
2847 selected frame and no others. */)
2848 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2850 Lisp_Object buffer;
2852 if (NILP (buffer_or_name))
2853 buffer = Fcurrent_buffer ();
2854 else
2855 buffer = Fget_buffer (buffer_or_name);
2857 if (BUFFERP (buffer))
2858 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2859 else
2860 return Qnil;
2864 static Lisp_Object
2865 resize_root_window (Lisp_Object window, Lisp_Object delta,
2866 Lisp_Object horizontal, Lisp_Object ignore,
2867 Lisp_Object pixelwise)
2869 return call5 (Qwindow__resize_root_window, window, delta,
2870 horizontal, ignore, pixelwise);
2873 void
2874 sanitize_window_sizes (Lisp_Object horizontal)
2876 /* Don't burp in temacs -nw before window.el is loaded. */
2877 if (!NILP (Fsymbol_function (Qwindow__sanitize_window_sizes)))
2878 call1 (Qwindow__sanitize_window_sizes, horizontal);
2882 static Lisp_Object
2883 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2885 return call2 (Qwindow__pixel_to_total, frame, horizontal);
2889 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2890 Sdelete_other_windows_internal, 0, 2, "",
2891 doc: /* Make WINDOW fill its frame.
2892 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2893 and defaults to the selected one.
2895 Optional argument ROOT, if non-nil, must specify an internal window such
2896 that WINDOW is in its window subtree. If this is the case, replace ROOT
2897 by WINDOW and leave alone any windows not part of ROOT's subtree.
2899 When WINDOW is live try to reduce display jumps by keeping the text
2900 previously visible in WINDOW in the same place on the frame. Doing this
2901 depends on the value of (window-start WINDOW), so if calling this
2902 function in a program gives strange scrolling, make sure the
2903 window-start value is reasonable when this function is called. */)
2904 (Lisp_Object window, Lisp_Object root)
2906 struct window *w, *r, *s;
2907 struct frame *f;
2908 Lisp_Object sibling, pwindow, delta;
2909 Lisp_Object swindow UNINIT;
2910 ptrdiff_t startpos UNINIT, startbyte UNINIT;
2911 int top UNINIT;
2912 int new_top;
2913 bool resize_failed = false;
2915 w = decode_valid_window (window);
2916 XSETWINDOW (window, w);
2917 f = XFRAME (w->frame);
2919 if (NILP (root))
2920 /* ROOT is the frame's root window. */
2922 root = FRAME_ROOT_WINDOW (f);
2923 r = XWINDOW (root);
2925 else
2926 /* ROOT must be an ancestor of WINDOW. */
2928 r = decode_valid_window (root);
2929 pwindow = XWINDOW (window)->parent;
2930 while (!NILP (pwindow))
2931 if (EQ (pwindow, root))
2932 break;
2933 else
2934 pwindow = XWINDOW (pwindow)->parent;
2935 if (!EQ (pwindow, root))
2936 error ("Specified root is not an ancestor of specified window");
2939 if (EQ (window, root))
2940 /* A noop. */
2941 return Qnil;
2942 /* I don't understand the "top > 0" part below. If we deal with a
2943 standalone minibuffer it would have been caught by the preceding
2944 test. */
2945 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2946 error ("Can't expand minibuffer to full frame");
2948 if (BUFFERP (w->contents))
2950 startpos = marker_position (w->start);
2951 startbyte = marker_byte_position (w->start);
2952 top = (WINDOW_TOP_EDGE_LINE (w)
2953 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2954 /* Make sure WINDOW is the frame's selected window. */
2955 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2957 if (EQ (selected_frame, w->frame))
2958 Fselect_window (window, Qnil);
2959 else
2960 fset_selected_window (f, window);
2963 else
2965 /* See if the frame's selected window is a part of the window
2966 subtree rooted at WINDOW, by finding all the selected window's
2967 parents and comparing each one with WINDOW. If it isn't we
2968 need a new selected window for this frame. */
2969 swindow = FRAME_SELECTED_WINDOW (f);
2970 while (true)
2972 pwindow = swindow;
2973 while (!NILP (pwindow) && !EQ (window, pwindow))
2974 pwindow = XWINDOW (pwindow)->parent;
2976 if (EQ (window, pwindow))
2977 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2978 as the new selected window. */
2979 break;
2980 else
2981 /* Else try the previous window of SWINDOW. */
2982 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2985 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2987 if (EQ (selected_frame, w->frame))
2988 Fselect_window (swindow, Qnil);
2989 else
2990 fset_selected_window (f, swindow);
2994 block_input ();
2995 if (!FRAME_INITIAL_P (f))
2997 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2999 /* We are going to free the glyph matrices of WINDOW, and with
3000 that we might lose any information about glyph rows that have
3001 some of their glyphs highlighted in mouse face. (These rows
3002 are marked with a mouse_face_p flag.) If WINDOW
3003 indeed has some glyphs highlighted in mouse face, signal to
3004 frame's up-to-date hook that mouse highlight was overwritten,
3005 so that it will arrange for redisplaying the highlight. */
3006 if (EQ (hlinfo->mouse_face_window, window))
3007 reset_mouse_highlight (hlinfo);
3009 free_window_matrices (r);
3011 fset_redisplay (f);
3012 Vwindow_list = Qnil;
3014 if (!WINDOW_LEAF_P (w))
3016 /* Resize child windows vertically. */
3017 XSETINT (delta, r->pixel_height - w->pixel_height);
3018 w->pixel_top = r->pixel_top;
3019 w->top_line = r->top_line;
3020 resize_root_window (window, delta, Qnil, Qnil, Qt);
3021 if (window_resize_check (w, false))
3023 window_resize_apply (w, false);
3024 window_pixel_to_total (w->frame, Qnil);
3026 else
3028 resize_root_window (window, delta, Qnil, Qt, Qt);
3029 if (window_resize_check (w, false))
3031 window_resize_apply (w, false);
3032 window_pixel_to_total (w->frame, Qnil);
3034 else
3035 resize_failed = true;
3038 /* Resize child windows horizontally. */
3039 if (!resize_failed)
3041 w->left_col = r->left_col;
3042 w->pixel_left = r->pixel_left;
3043 XSETINT (delta, r->pixel_width - w->pixel_width);
3044 resize_root_window (window, delta, Qt, Qnil, Qt);
3045 if (window_resize_check (w, true))
3047 window_resize_apply (w, true);
3048 window_pixel_to_total (w->frame, Qt);
3050 else
3052 resize_root_window (window, delta, Qt, Qt, Qt);
3053 if (window_resize_check (w, true))
3055 window_resize_apply (w, true);
3056 window_pixel_to_total (w->frame, Qt);
3058 else
3059 resize_failed = true;
3063 if (resize_failed)
3064 /* Play safe, if we still can ... */
3066 window = swindow;
3067 w = XWINDOW (window);
3071 /* Cleanly unlink WINDOW from window-tree. */
3072 if (!NILP (w->prev))
3073 /* Get SIBLING above (on the left of) WINDOW. */
3075 sibling = w->prev;
3076 s = XWINDOW (sibling);
3077 wset_next (s, w->next);
3078 if (!NILP (s->next))
3079 wset_prev (XWINDOW (s->next), sibling);
3081 else
3082 /* Get SIBLING below (on the right of) WINDOW. */
3084 sibling = w->next;
3085 s = XWINDOW (sibling);
3086 wset_prev (s, Qnil);
3087 wset_combination (XWINDOW (w->parent),
3088 XWINDOW (w->parent)->horizontal, sibling);
3091 /* Delete ROOT and all child windows of ROOT. */
3092 if (WINDOWP (r->contents))
3094 delete_all_child_windows (r->contents);
3095 wset_combination (r, false, Qnil);
3098 replace_window (root, window, true);
3100 /* This must become SWINDOW anyway ....... */
3101 if (BUFFERP (w->contents) && !resize_failed)
3103 /* Try to minimize scrolling, by setting the window start to the
3104 point will cause the text at the old window start to be at the
3105 same place on the frame. But don't try to do this if the
3106 window start is outside the visible portion (as might happen
3107 when the display is not current, due to typeahead). */
3108 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3109 if (new_top != top
3110 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3111 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3113 struct position pos;
3114 struct buffer *obuf = current_buffer;
3116 Fset_buffer (w->contents);
3117 /* This computation used to temporarily move point, but that
3118 can have unwanted side effects due to text properties. */
3119 pos = *vmotion (startpos, startbyte, -top, w);
3121 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3122 w->window_end_valid = false;
3123 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3124 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3125 /* We need to do this, so that the window-scroll-functions
3126 get called. */
3127 w->optional_new_start = true;
3129 set_buffer_internal (obuf);
3133 adjust_frame_glyphs (f);
3134 unblock_input ();
3136 run_window_configuration_change_hook (f);
3138 return Qnil;
3142 void
3143 replace_buffer_in_windows (Lisp_Object buffer)
3145 call1 (Qreplace_buffer_in_windows, buffer);
3148 /* If BUFFER is shown in a window, safely replace it with some other
3149 buffer in all windows of all frames, even those on other keyboards. */
3151 void
3152 replace_buffer_in_windows_safely (Lisp_Object buffer)
3154 if (buffer_window_count (XBUFFER (buffer)))
3156 Lisp_Object tail, frame;
3158 /* A single call to window_loop won't do the job because it only
3159 considers frames on the current keyboard. So loop manually over
3160 frames, and handle each one. */
3161 FOR_EACH_FRAME (tail, frame)
3162 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3166 /* The following three routines are needed for running a window's
3167 configuration change hook. */
3168 static void
3169 run_funs (Lisp_Object funs)
3171 for (; CONSP (funs); funs = XCDR (funs))
3172 if (!EQ (XCAR (funs), Qt))
3173 call0 (XCAR (funs));
3176 static void
3177 select_window_norecord (Lisp_Object window)
3179 if (WINDOW_LIVE_P (window))
3180 Fselect_window (window, Qt);
3183 static void
3184 select_frame_norecord (Lisp_Object frame)
3186 if (FRAME_LIVE_P (XFRAME (frame)))
3187 Fselect_frame (frame, Qt);
3190 static void
3191 run_window_configuration_change_hook (struct frame *f)
3193 ptrdiff_t count = SPECPDL_INDEX ();
3194 Lisp_Object frame, global_wcch
3195 = Fdefault_value (Qwindow_configuration_change_hook);
3196 XSETFRAME (frame, f);
3198 if (NILP (Vrun_hooks)
3199 || !(f->can_x_set_window_size)
3200 || !(f->after_make_frame))
3201 return;
3203 /* Use the right buffer. Matters when running the local hooks. */
3204 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3206 record_unwind_current_buffer ();
3207 Fset_buffer (Fwindow_buffer (Qnil));
3210 if (SELECTED_FRAME () != f)
3212 record_unwind_protect (select_frame_norecord, selected_frame);
3213 select_frame_norecord (frame);
3216 /* Look for buffer-local values. */
3218 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3219 for (; CONSP (windows); windows = XCDR (windows))
3221 Lisp_Object window = XCAR (windows);
3222 Lisp_Object buffer = Fwindow_buffer (window);
3223 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3224 buffer)))
3226 ptrdiff_t inner_count = SPECPDL_INDEX ();
3227 record_unwind_protect (select_window_norecord, selected_window);
3228 select_window_norecord (window);
3229 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3230 buffer));
3231 unbind_to (inner_count, Qnil);
3236 run_funs (global_wcch);
3237 unbind_to (count, Qnil);
3240 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3241 Srun_window_configuration_change_hook, 0, 1, 0,
3242 doc: /* Run `window-configuration-change-hook' for FRAME.
3243 If FRAME is omitted or nil, it defaults to the selected frame. */)
3244 (Lisp_Object frame)
3246 run_window_configuration_change_hook (decode_live_frame (frame));
3247 return Qnil;
3250 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3251 Srun_window_scroll_functions, 0, 1, 0,
3252 doc: /* Run `window-scroll-functions' for WINDOW.
3253 If WINDOW is omitted or nil, it defaults to the selected window. */)
3254 (Lisp_Object window)
3256 if (! NILP (Vwindow_scroll_functions))
3257 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3258 Fmarker_position (decode_live_window (window)->start));
3259 return Qnil;
3263 /* Compare old and present pixel sizes of windows in tree rooted at W.
3264 Return true iff any of these windows differs in size. */
3266 static bool
3267 window_size_changed (struct window *w)
3269 if (w->pixel_width != w->pixel_width_before_size_change
3270 || w->pixel_height != w->pixel_height_before_size_change)
3271 return true;
3273 if (WINDOW_INTERNAL_P (w))
3275 w = XWINDOW (w->contents);
3276 while (w)
3278 if (window_size_changed (w))
3279 return true;
3281 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3285 return false;
3288 /* Set before size change pixel sizes of windows in tree rooted at W to
3289 their present pixel sizes. */
3291 static void
3292 window_set_before_size_change_sizes (struct window *w)
3294 w->pixel_width_before_size_change = w->pixel_width;
3295 w->pixel_height_before_size_change = w->pixel_height;
3297 if (WINDOW_INTERNAL_P (w))
3299 w = XWINDOW (w->contents);
3300 while (w)
3302 window_set_before_size_change_sizes (w);
3303 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3309 void
3310 run_window_size_change_functions (Lisp_Object frame)
3312 struct frame *f = XFRAME (frame);
3313 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3314 Lisp_Object functions = Vwindow_size_change_functions;
3316 if (FRAME_WINDOW_CONFIGURATION_CHANGED (f)
3317 || window_size_changed (r))
3319 while (CONSP (functions))
3321 if (!EQ (XCAR (functions), Qt))
3322 safe_call1 (XCAR (functions), frame);
3323 functions = XCDR (functions);
3326 window_set_before_size_change_sizes (r);
3327 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = false;
3332 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3333 to run hooks. See make_frame for a case where it's not allowed.
3334 KEEP_MARGINS_P means that the current margins, fringes, and
3335 scroll-bar settings of the window are not reset from the buffer's
3336 local settings. */
3338 void
3339 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3340 bool run_hooks_p, bool keep_margins_p)
3342 struct window *w = XWINDOW (window);
3343 struct buffer *b = XBUFFER (buffer);
3344 ptrdiff_t count = SPECPDL_INDEX ();
3345 bool samebuf = EQ (buffer, w->contents);
3347 wset_buffer (w, buffer);
3349 if (EQ (window, selected_window))
3350 bset_last_selected_window (b, window);
3352 /* Let redisplay errors through. */
3353 b->display_error_modiff = 0;
3355 /* Update time stamps of buffer display. */
3356 if (INTEGERP (BVAR (b, display_count)))
3357 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3358 bset_display_time (b, Fcurrent_time ());
3360 w->window_end_pos = 0;
3361 w->window_end_vpos = 0;
3362 w->last_cursor_vpos = 0;
3364 if (!(keep_margins_p && samebuf))
3365 { /* If we're not actually changing the buffer, don't reset hscroll
3366 and vscroll. Resetting hscroll and vscroll here is problematic
3367 for things like image-mode and doc-view-mode since it resets
3368 the image's position whenever we resize the frame. */
3369 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3370 w->suspend_auto_hscroll = false;
3371 w->vscroll = 0;
3372 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3373 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3374 set_marker_restricted (w->start,
3375 make_number (b->last_window_start),
3376 buffer);
3377 w->start_at_line_beg = false;
3378 w->force_start = false;
3381 wset_redisplay (w);
3382 wset_update_mode_line (w);
3384 /* We must select BUFFER to run the window-scroll-functions and to look up
3385 the buffer-local value of Vwindow_point_insertion_type. */
3386 record_unwind_current_buffer ();
3387 Fset_buffer (buffer);
3389 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3390 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3392 if (!keep_margins_p)
3394 /* Set left and right marginal area width etc. from buffer. */
3395 set_window_fringes (w, BVAR (b, left_fringe_width),
3396 BVAR (b, right_fringe_width),
3397 BVAR (b, fringes_outside_margins));
3398 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3399 BVAR (b, vertical_scroll_bar_type),
3400 BVAR (b, scroll_bar_height),
3401 BVAR (b, horizontal_scroll_bar_type));
3402 set_window_margins (w, BVAR (b, left_margin_cols),
3403 BVAR (b, right_margin_cols));
3404 apply_window_adjustment (w);
3407 if (run_hooks_p)
3409 if (!NILP (Vwindow_scroll_functions))
3410 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3411 Fmarker_position (w->start));
3412 if (!samebuf)
3413 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3416 unbind_to (count, Qnil);
3419 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3420 doc: /* Make WINDOW display BUFFER-OR-NAME.
3421 WINDOW must be a live window and defaults to the selected one.
3422 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3424 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3425 display margins, fringe widths, and scroll bar settings are preserved;
3426 the default is to reset these from the local settings for BUFFER-OR-NAME
3427 or the frame defaults. Return nil.
3429 This function throws an error when WINDOW is strongly dedicated to its
3430 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3431 already display BUFFER-OR-NAME.
3433 This function runs `window-scroll-functions' before running
3434 `window-configuration-change-hook'. */)
3435 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3437 register Lisp_Object tem, buffer;
3438 register struct window *w = decode_live_window (window);
3440 XSETWINDOW (window, w);
3441 buffer = Fget_buffer (buffer_or_name);
3442 CHECK_BUFFER (buffer);
3443 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3444 error ("Attempt to display deleted buffer");
3446 tem = w->contents;
3447 if (NILP (tem))
3448 error ("Window is deleted");
3449 else
3451 if (!EQ (tem, buffer))
3453 if (EQ (w->dedicated, Qt))
3454 /* WINDOW is strongly dedicated to its buffer, signal an
3455 error. */
3456 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3457 else
3458 /* WINDOW is weakly dedicated to its buffer, reset
3459 dedication. */
3460 wset_dedicated (w, Qnil);
3462 call1 (Qrecord_window_buffer, window);
3465 unshow_buffer (w);
3468 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3470 return Qnil;
3473 static Lisp_Object
3474 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3476 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3479 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3480 0, 1, 0,
3481 doc: /* Force all windows to be updated on next redisplay.
3482 If optional arg OBJECT is a window, force redisplay of that window only.
3483 If OBJECT is a buffer or buffer name, force redisplay of all windows
3484 displaying that buffer. */)
3485 (Lisp_Object object)
3487 if (NILP (object))
3489 windows_or_buffers_changed = 29;
3490 update_mode_lines = 28;
3491 return Qt;
3494 if (WINDOWP (object))
3496 struct window *w = XWINDOW (object);
3497 mark_window_display_accurate (object, false);
3498 w->update_mode_line = true;
3499 if (BUFFERP (w->contents))
3500 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3501 update_mode_lines = 29;
3502 return Qt;
3505 if (STRINGP (object))
3506 object = Fget_buffer (object);
3507 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3508 && buffer_window_count (XBUFFER (object)))
3510 /* If buffer is live and shown in at least one window, find
3511 all windows showing this buffer and force update of them. */
3512 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3513 return NILP (object) ? Qnil : Qt;
3516 /* If nothing suitable was found, just return.
3517 We could signal an error, but this feature will typically be used
3518 asynchronously in timers or process sentinels, so we don't. */
3519 return Qnil;
3522 /* Obsolete since 24.3. */
3523 void
3524 temp_output_buffer_show (register Lisp_Object buf)
3526 register struct buffer *old = current_buffer;
3527 register Lisp_Object window;
3528 register struct window *w;
3530 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3532 Fset_buffer (buf);
3533 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3534 BEGV = BEG;
3535 ZV = Z;
3536 SET_PT (BEG);
3537 set_buffer_internal (old);
3539 if (!NILP (Vtemp_buffer_show_function))
3540 call1 (Vtemp_buffer_show_function, buf);
3541 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3543 if (!EQ (XWINDOW (window)->frame, selected_frame))
3544 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3545 Vminibuf_scroll_window = window;
3546 w = XWINDOW (window);
3547 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3548 w->suspend_auto_hscroll = false;
3549 set_marker_restricted_both (w->start, buf, BEG, BEG);
3550 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3551 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3553 /* Run temp-buffer-show-hook, with the chosen window selected
3554 and its buffer current. */
3556 ptrdiff_t count = SPECPDL_INDEX ();
3557 Lisp_Object prev_window, prev_buffer;
3558 prev_window = selected_window;
3559 XSETBUFFER (prev_buffer, old);
3561 /* Select the window that was chosen, for running the hook.
3562 Note: Both Fselect_window and select_window_norecord may
3563 set-buffer to the buffer displayed in the window,
3564 so we need to save the current buffer. --stef */
3565 record_unwind_protect (restore_buffer, prev_buffer);
3566 record_unwind_protect (select_window_norecord, prev_window);
3567 Fselect_window (window, Qt);
3568 Fset_buffer (w->contents);
3569 run_hook (Qtemp_buffer_show_hook);
3570 unbind_to (count, Qnil);
3575 /* Allocate basically initialized window. */
3577 static struct window *
3578 allocate_window (void)
3580 return ALLOCATE_ZEROED_PSEUDOVECTOR
3581 (struct window, current_matrix, PVEC_WINDOW);
3584 /* Make new window, have it replace WINDOW in window-tree, and make
3585 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3586 horizontal child). */
3587 static void
3588 make_parent_window (Lisp_Object window, bool horflag)
3590 Lisp_Object parent;
3591 register struct window *o, *p;
3593 o = XWINDOW (window);
3594 p = allocate_window ();
3595 memcpy ((char *) p + sizeof (struct vectorlike_header),
3596 (char *) o + sizeof (struct vectorlike_header),
3597 word_size * VECSIZE (struct window));
3598 /* P's buffer slot may change from nil to a buffer... */
3599 adjust_window_count (p, 1);
3600 XSETWINDOW (parent, p);
3602 p->sequence_number = ++sequence_number;
3604 replace_window (window, parent, true);
3606 wset_next (o, Qnil);
3607 wset_prev (o, Qnil);
3608 wset_parent (o, parent);
3609 /* ...but now P becomes an internal window. */
3610 wset_start (p, Qnil);
3611 wset_pointm (p, Qnil);
3612 wset_old_pointm (p, Qnil);
3613 wset_buffer (p, Qnil);
3614 wset_combination (p, horflag, window);
3615 wset_combination_limit (p, Qnil);
3616 wset_window_parameters (p, Qnil);
3619 /* Make new window from scratch. */
3620 Lisp_Object
3621 make_window (void)
3623 Lisp_Object window;
3624 register struct window *w;
3626 w = allocate_window ();
3627 /* Initialize Lisp data. Note that allocate_window initializes all
3628 Lisp data to nil, so do it only for slots which should not be nil. */
3629 wset_normal_lines (w, make_float (1.0));
3630 wset_normal_cols (w, make_float (1.0));
3631 wset_new_total (w, make_number (0));
3632 wset_new_normal (w, make_number (0));
3633 wset_new_pixel (w, make_number (0));
3634 wset_start (w, Fmake_marker ());
3635 wset_pointm (w, Fmake_marker ());
3636 wset_old_pointm (w, Fmake_marker ());
3637 wset_vertical_scroll_bar_type (w, Qt);
3638 wset_horizontal_scroll_bar_type (w, Qt);
3639 /* These Lisp fields are marked specially so they're not set to nil by
3640 allocate_window. */
3641 wset_prev_buffers (w, Qnil);
3642 wset_next_buffers (w, Qnil);
3644 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3645 non-Lisp data, so do it only for slots which should not be zero. */
3646 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3647 w->left_fringe_width = w->right_fringe_width = -1;
3648 w->mode_line_height = w->header_line_height = -1;
3649 #ifdef HAVE_WINDOW_SYSTEM
3650 w->phys_cursor_type = NO_CURSOR;
3651 w->phys_cursor_width = -1;
3652 #endif
3653 w->sequence_number = ++sequence_number;
3654 w->pixel_width_before_size_change = 0;
3655 w->pixel_height_before_size_change = 0;
3656 w->scroll_bar_width = -1;
3657 w->scroll_bar_height = -1;
3658 w->column_number_displayed = -1;
3659 /* Reset window_list. */
3660 Vwindow_list = Qnil;
3661 /* Return window. */
3662 XSETWINDOW (window, w);
3663 return window;
3666 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3667 doc: /* Set new pixel size of WINDOW to SIZE.
3668 WINDOW must be a valid window and defaults to the selected one.
3669 Return SIZE.
3671 Optional argument ADD non-nil means add SIZE to the new pixel size of
3672 WINDOW and return the sum.
3674 The new pixel size of WINDOW, if valid, will be shortly installed as
3675 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3676 `window-pixel-width').
3678 Note: This function does not operate on any child windows of WINDOW. */)
3679 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3681 struct window *w = decode_valid_window (window);
3682 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3683 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3685 CHECK_RANGED_INTEGER (size, size_min, size_max);
3686 if (NILP (add))
3687 wset_new_pixel (w, size);
3688 else
3689 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3691 return w->new_pixel;
3694 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3695 doc: /* Set new total size of WINDOW to SIZE.
3696 WINDOW must be a valid window and defaults to the selected one.
3697 Return SIZE.
3699 Optional argument ADD non-nil means add SIZE to the new total size of
3700 WINDOW and return the sum.
3702 The new total size of WINDOW, if valid, will be shortly installed as
3703 WINDOW's total height (see `window-total-height') or total width (see
3704 `window-total-width').
3706 Note: This function does not operate on any child windows of WINDOW. */)
3707 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3709 struct window *w = decode_valid_window (window);
3711 CHECK_NUMBER (size);
3712 if (NILP (add))
3713 wset_new_total (w, size);
3714 else
3715 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3717 return w->new_total;
3720 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3721 doc: /* Set new normal size of WINDOW to SIZE.
3722 WINDOW must be a valid window and defaults to the selected one.
3723 Return SIZE.
3725 The new normal size of WINDOW, if valid, will be shortly installed as
3726 WINDOW's normal size (see `window-normal-size').
3728 Note: This function does not operate on any child windows of WINDOW. */)
3729 (Lisp_Object window, Lisp_Object size)
3731 wset_new_normal (decode_valid_window (window), size);
3732 return size;
3735 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3736 to w->new_pixel would result in correct heights (widths)
3737 for window W and recursively all child windows of W.
3739 Note: This function does not check any of `window-fixed-size-p',
3740 `window-min-height' or `window-min-width'. It does check that window
3741 sizes do not drop below one line (two columns). */
3742 static bool
3743 window_resize_check (struct window *w, bool horflag)
3745 struct frame *f = XFRAME (w->frame);
3746 struct window *c;
3748 if (WINDOW_VERTICAL_COMBINATION_P (w))
3749 /* W is a vertical combination. */
3751 c = XWINDOW (w->contents);
3752 if (horflag)
3753 /* All child windows of W must have the same width as W. */
3755 while (c)
3757 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3758 || !window_resize_check (c, horflag))
3759 return false;
3761 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3764 return true;
3766 else
3767 /* The sum of the heights of the child windows of W must equal
3768 W's height. */
3770 int remaining_pixels = XINT (w->new_pixel);
3772 while (c)
3774 if (!window_resize_check (c, horflag))
3775 return false;
3777 remaining_pixels -= XINT (c->new_pixel);
3778 if (remaining_pixels < 0)
3779 return false;
3780 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3783 return remaining_pixels == 0;
3786 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3787 /* W is a horizontal combination. */
3789 c = XWINDOW (w->contents);
3790 if (horflag)
3791 /* The sum of the widths of the child windows of W must equal W's
3792 width. */
3794 int remaining_pixels = XINT (w->new_pixel);
3796 while (c)
3798 if (!window_resize_check (c, horflag))
3799 return false;
3801 remaining_pixels -= XINT (c->new_pixel);
3802 if (remaining_pixels < 0)
3803 return false;
3804 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3807 return remaining_pixels == 0;
3809 else
3810 /* All child windows of W must have the same height as W. */
3812 while (c)
3814 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3815 || !window_resize_check (c, horflag))
3816 return false;
3818 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3821 return true;
3824 else
3825 /* A leaf window. Make sure it's not too small. The following
3826 hardcodes the values of `window-safe-min-width' (2) and
3827 `window-safe-min-height' (1) which are defined in window.el. */
3828 return (XINT (w->new_pixel) >= (horflag
3829 ? (2 * FRAME_COLUMN_WIDTH (f))
3830 : FRAME_LINE_HEIGHT (f)));
3834 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3835 w->new_pixel for window W and recursively all child windows of W.
3836 Also calculate and assign the new vertical (horizontal) pixel start
3837 positions of each of these windows.
3839 This function does not perform any error checks. Make sure you have
3840 run window_resize_check on W before applying this function. */
3841 static void
3842 window_resize_apply (struct window *w, bool horflag)
3844 struct window *c;
3845 int edge;
3846 int unit = (horflag
3847 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3848 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3850 /* Note: Assigning new_normal requires that the new total size of the
3851 parent window has been set *before*. */
3852 if (horflag)
3854 w->pixel_width = XFASTINT (w->new_pixel);
3855 w->total_cols = w->pixel_width / unit;
3856 if (NUMBERP (w->new_normal))
3857 wset_normal_cols (w, w->new_normal);
3859 edge = w->pixel_left;
3861 else
3863 w->pixel_height = XFASTINT (w->new_pixel);
3864 w->total_lines = w->pixel_height / unit;
3865 if (NUMBERP (w->new_normal))
3866 wset_normal_lines (w, w->new_normal);
3868 edge = w->pixel_top;
3871 if (WINDOW_VERTICAL_COMBINATION_P (w))
3872 /* W is a vertical combination. */
3874 c = XWINDOW (w->contents);
3875 while (c)
3877 if (horflag)
3879 c->pixel_left = edge;
3880 c->left_col = edge / unit;
3882 else
3884 c->pixel_top = edge;
3885 c->top_line = edge / unit;
3887 window_resize_apply (c, horflag);
3888 if (!horflag)
3889 edge = edge + c->pixel_height;
3891 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3894 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3895 /* W is a horizontal combination. */
3897 c = XWINDOW (w->contents);
3898 while (c)
3900 if (horflag)
3902 c->pixel_left = edge;
3903 c->left_col = edge / unit;
3905 else
3907 c->pixel_top = edge;
3908 c->top_line = edge / unit;
3911 window_resize_apply (c, horflag);
3912 if (horflag)
3913 edge = edge + c->pixel_width;
3915 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3918 else
3919 /* Bug#15957. */
3920 w->window_end_valid = false;
3924 /* Set w->total_lines (w->total_cols if HORFLAG) to
3925 w->new_total for window W and recursively all child windows of W.
3926 Also calculate and assign the new vertical (horizontal) start
3927 positions of each of these windows. */
3928 static void
3929 window_resize_apply_total (struct window *w, bool horflag)
3931 struct window *c;
3932 int edge;
3934 /* Note: Assigning new_normal requires that the new total size of the
3935 parent window has been set *before*. */
3936 if (horflag)
3938 w->total_cols = XFASTINT (w->new_total);
3939 edge = w->left_col;
3941 else
3943 w->total_lines = XFASTINT (w->new_total);
3944 edge = w->top_line;
3947 if (WINDOW_VERTICAL_COMBINATION_P (w))
3948 /* W is a vertical combination. */
3950 c = XWINDOW (w->contents);
3951 while (c)
3953 if (horflag)
3954 c->left_col = edge;
3955 else
3956 c->top_line = edge;
3958 window_resize_apply_total (c, horflag);
3959 if (!horflag)
3960 edge = edge + c->total_lines;
3962 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3965 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3966 /* W is a horizontal combination. */
3968 c = XWINDOW (w->contents);
3969 while (c)
3971 if (horflag)
3972 c->left_col = edge;
3973 else
3974 c->top_line = edge;
3976 window_resize_apply_total (c, horflag);
3977 if (horflag)
3978 edge = edge + c->total_cols;
3980 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3985 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3986 doc: /* Apply requested size values for window-tree of FRAME.
3987 If FRAME is omitted or nil, it defaults to the selected frame.
3989 Optional argument HORIZONTAL omitted or nil means apply requested
3990 height values. HORIZONTAL non-nil means apply requested width values.
3992 The requested size values are those set by `set-window-new-pixel' and
3993 `set-window-new-normal'. This function checks whether the requested
3994 values sum up to a valid window layout, recursively assigns the new
3995 sizes of all child windows and calculates and assigns the new start
3996 positions of these windows.
3998 Return t if the requested values have been applied correctly, nil
3999 otherwise.
4001 Note: This function does not check any of `window-fixed-size-p',
4002 `window-min-height' or `window-min-width'. All these checks have to
4003 be applied on the Elisp level. */)
4004 (Lisp_Object frame, Lisp_Object horizontal)
4006 struct frame *f = decode_live_frame (frame);
4007 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4008 bool horflag = !NILP (horizontal);
4010 if (!window_resize_check (r, horflag)
4011 || (XINT (r->new_pixel)
4012 != (horflag ? r->pixel_width : r->pixel_height)))
4013 return Qnil;
4015 block_input ();
4016 window_resize_apply (r, horflag);
4018 fset_redisplay (f);
4020 adjust_frame_glyphs (f);
4021 unblock_input ();
4023 return Qt;
4027 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4028 doc: /* Apply requested total size values for window-tree of FRAME.
4029 If FRAME is omitted or nil, it defaults to the selected frame.
4031 This function does not assign pixel or normal size values. You should
4032 have run `window-resize-apply' before running this.
4034 Optional argument HORIZONTAL omitted or nil means apply requested
4035 height values. HORIZONTAL non-nil means apply requested width
4036 values. */)
4037 (Lisp_Object frame, Lisp_Object horizontal)
4039 struct frame *f = decode_live_frame (frame);
4040 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4042 block_input ();
4043 /* Necessary when deleting the top-/or leftmost window. */
4044 r->left_col = 0;
4045 r->top_line = FRAME_TOP_MARGIN (f);
4046 window_resize_apply_total (r, !NILP (horizontal));
4047 /* Handle the mini window. */
4048 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4050 struct window *m = XWINDOW (f->minibuffer_window);
4052 if (NILP (horizontal))
4054 m->top_line = r->top_line + r->total_lines;
4055 m->total_lines = XFASTINT (m->new_total);
4057 else
4058 m->total_cols = XFASTINT (m->new_total);
4061 unblock_input ();
4063 return Qt;
4067 /* Resize frame F's windows when F's width or height is set to SIZE.
4068 If HORFLAG is zero, F's width was set to SIZE, otherwise its height
4069 was set. SIZE is interpreted in F's canonical character units
4070 (a.k.a. "columns" or "lines"), unless PIXELWISE is non-zero, which
4071 means to interpret SIZE in pixel units. */
4072 void
4073 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4075 Lisp_Object root = f->root_window;
4076 struct window *r = XWINDOW (root);
4077 Lisp_Object mini = f->minibuffer_window;
4078 struct window *m;
4079 /* old_size is the old size of the frame's root window. */
4080 int old_size = horflag ? r->total_cols : r->total_lines;
4081 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4082 /* new_size is the new size of the frame's root window. */
4083 int new_size, new_pixel_size;
4084 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4086 /* Don't let the size drop below one unit. This is more comforting
4087 when we are called from x_set_tool_bar_lines since the latter may
4088 have implicitly given us a zero or negative height. */
4089 if (pixelwise)
4091 /* Note: This does not include the size for internal borders
4092 since these are not part of the frame's text area. */
4093 new_pixel_size = max (horflag
4094 ? size
4095 : (size
4096 - ((FRAME_HAS_MINIBUF_P (f)
4097 && !FRAME_MINIBUF_ONLY_P (f))
4098 ? FRAME_LINE_HEIGHT (f) : 0)),
4099 unit);
4100 new_size = new_pixel_size / unit;
4102 else
4104 new_size = max (size - (!horflag
4105 && FRAME_HAS_MINIBUF_P (f)
4106 && !FRAME_MINIBUF_ONLY_P (f)),
4108 new_pixel_size = new_size * unit;
4111 if (new_pixel_size == old_pixel_size
4112 && (horflag || r->pixel_top == FRAME_TOP_MARGIN_HEIGHT (f)))
4114 else if (WINDOW_LEAF_P (r))
4115 /* For a leaf root window just set the size. */
4116 if (horflag)
4118 r->total_cols = new_size;
4119 r->pixel_width = new_pixel_size;
4121 else
4123 r->top_line = FRAME_TOP_MARGIN (f);
4124 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4126 r->total_lines = new_size;
4127 r->pixel_height = new_pixel_size;
4129 else
4131 Lisp_Object delta;
4133 if (!horflag)
4135 r->top_line = FRAME_TOP_MARGIN (f);
4136 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4139 if (pixelwise)
4140 XSETINT (delta, new_pixel_size - old_pixel_size);
4141 else
4142 XSETINT (delta, new_size - old_size);
4144 /* Try a "normal" resize first. */
4145 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4146 pixelwise ? Qt : Qnil);
4147 if (window_resize_check (r, horflag)
4148 && new_pixel_size == XINT (r->new_pixel))
4150 window_resize_apply (r, horflag);
4151 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4153 else
4155 /* Try with "reasonable" minimum sizes next. */
4156 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4157 pixelwise ? Qt : Qnil);
4158 if (window_resize_check (r, horflag)
4159 && new_pixel_size == XINT (r->new_pixel))
4161 window_resize_apply (r, horflag);
4162 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4167 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4169 m = XWINDOW (mini);
4170 if (horflag)
4172 m->total_cols = new_size;
4173 m->pixel_width = new_pixel_size;
4175 else
4177 /* Are we sure we always want 1 line here? */
4178 m->total_lines = 1;
4179 m->pixel_height = FRAME_LINE_HEIGHT (f);
4180 m->top_line = r->top_line + r->total_lines;
4181 m->pixel_top = r->pixel_top + r->pixel_height;
4185 fset_redisplay (f);
4189 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4190 doc: /* Split window OLD.
4191 Second argument PIXEL-SIZE specifies the number of pixels of the
4192 new window. It must be a positive integer.
4194 Third argument SIDE nil (or `below') specifies that the new window shall
4195 be located below WINDOW. SIDE `above' means the new window shall be
4196 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4197 height of the new window including space reserved for the mode and/or
4198 header line.
4200 SIDE t (or `right') specifies that the new window shall be located on
4201 the right side of WINDOW. SIDE `left' means the new window shall be
4202 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4203 width of the new window including space reserved for fringes and the
4204 scrollbar or a divider column.
4206 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4207 according to the SIDE argument.
4209 The new pixel and normal sizes of all involved windows must have been
4210 set correctly. See the code of `split-window' for how this is done. */)
4211 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4213 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4214 parent window or an internal window we have to install as OLD's new
4215 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4216 provided OLD is a leaf window, or to the frame's selected window.
4217 NEW (*n) is the new window created with some parameters taken from
4218 REFERENCE (*r). */
4219 Lisp_Object new, frame, reference;
4220 struct window *o, *p, *n, *r, *c;
4221 struct frame *f;
4222 bool horflag
4223 /* HORFLAG is true when we split side-by-side, false otherwise. */
4224 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4226 CHECK_WINDOW (old);
4227 o = XWINDOW (old);
4228 frame = WINDOW_FRAME (o);
4229 f = XFRAME (frame);
4231 CHECK_NUMBER (pixel_size);
4232 EMACS_INT total_size
4233 = XINT (pixel_size) / (horflag
4234 ? FRAME_COLUMN_WIDTH (f)
4235 : FRAME_LINE_HEIGHT (f));
4237 /* Set combination_limit if we have to make a new parent window.
4238 We do that if either `window-combination-limit' is t, or OLD has no
4239 parent, or OLD is ortho-combined. */
4240 bool combination_limit
4241 = (EQ (Vwindow_combination_limit, Qt)
4242 || NILP (o->parent)
4243 || (horflag
4244 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4245 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4247 /* We need a live reference window to initialize some parameters. */
4248 if (WINDOW_LIVE_P (old))
4249 /* OLD is live, use it as reference window. */
4250 reference = old;
4251 else
4252 /* Use the frame's selected window as reference window. */
4253 reference = FRAME_SELECTED_WINDOW (f);
4254 r = XWINDOW (reference);
4256 /* The following bugs are caught by `split-window'. */
4257 if (MINI_WINDOW_P (o))
4258 error ("Attempt to split minibuffer window");
4259 else if (total_size < (horflag ? 2 : 1))
4260 error ("Size of new window too small (after split)");
4261 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4262 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4263 proportionally. */
4265 p = XWINDOW (o->parent);
4266 /* Temporarily pretend we split the parent window. */
4267 wset_new_pixel
4268 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4269 - XINT (pixel_size)));
4270 if (!window_resize_check (p, horflag))
4271 error ("Window sizes don't fit");
4272 else
4273 /* Undo the temporary pretension. */
4274 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4276 else
4278 if (!window_resize_check (o, horflag))
4279 error ("Resizing old window failed");
4280 else if (XINT (pixel_size) + XINT (o->new_pixel)
4281 != (horflag ? o->pixel_width : o->pixel_height))
4282 error ("Sum of sizes of old and new window don't fit");
4285 /* This is our point of no return. */
4286 if (combination_limit)
4288 /* Save the old value of o->normal_cols/lines. It gets corrupted
4289 by make_parent_window and we need it below for assigning it to
4290 p->new_normal. */
4291 Lisp_Object new_normal
4292 = horflag ? o->normal_cols : o->normal_lines;
4294 make_parent_window (old, horflag);
4295 p = XWINDOW (o->parent);
4296 if (EQ (Vwindow_combination_limit, Qt))
4297 /* Store t in the new parent's combination_limit slot to avoid
4298 that its children get merged into another window. */
4299 wset_combination_limit (p, Qt);
4300 /* These get applied below. */
4301 wset_new_pixel
4302 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4303 wset_new_total
4304 (p, make_number (horflag ? o->total_cols : o->total_lines));
4305 wset_new_normal (p, new_normal);
4307 else
4308 p = XWINDOW (o->parent);
4310 fset_redisplay (f);
4311 new = make_window ();
4312 n = XWINDOW (new);
4313 wset_frame (n, frame);
4314 wset_parent (n, o->parent);
4316 if (EQ (side, Qabove) || EQ (side, Qleft))
4318 wset_prev (n, o->prev);
4319 if (NILP (n->prev))
4320 wset_combination (p, horflag, new);
4321 else
4322 wset_next (XWINDOW (n->prev), new);
4323 wset_next (n, old);
4324 wset_prev (o, new);
4326 else
4328 wset_next (n, o->next);
4329 if (!NILP (n->next))
4330 wset_prev (XWINDOW (n->next), new);
4331 wset_prev (n, old);
4332 wset_next (o, new);
4335 n->window_end_valid = false;
4336 n->last_cursor_vpos = 0;
4338 /* Get special geometry settings from reference window. */
4339 n->left_margin_cols = r->left_margin_cols;
4340 n->right_margin_cols = r->right_margin_cols;
4341 n->left_fringe_width = r->left_fringe_width;
4342 n->right_fringe_width = r->right_fringe_width;
4343 n->fringes_outside_margins = r->fringes_outside_margins;
4344 n->scroll_bar_width = r->scroll_bar_width;
4345 n->scroll_bar_height = r->scroll_bar_height;
4346 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4347 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4349 /* Directly assign orthogonal coordinates and sizes. */
4350 if (horflag)
4352 n->pixel_top = o->pixel_top;
4353 n->top_line = o->top_line;
4354 n->pixel_height = o->pixel_height;
4355 n->total_lines = o->total_lines;
4357 else
4359 n->pixel_left = o->pixel_left;
4360 n->left_col = o->left_col;
4361 n->pixel_width = o->pixel_width;
4362 n->total_cols = o->total_cols;
4365 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4366 get them ready here. */
4367 wset_new_pixel (n, pixel_size);
4368 EMACS_INT sum = 0;
4369 c = XWINDOW (p->contents);
4370 while (c)
4372 if (c != n)
4373 sum = sum + XINT (c->new_total);
4374 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4376 wset_new_total (n, make_number ((horflag
4377 ? p->total_cols
4378 : p->total_lines)
4379 - sum));
4380 wset_new_normal (n, normal_size);
4382 block_input ();
4383 window_resize_apply (p, horflag);
4384 adjust_frame_glyphs (f);
4385 /* Set buffer of NEW to buffer of reference window. Don't run
4386 any hooks. */
4387 set_window_buffer (new, r->contents, false, true);
4388 unblock_input ();
4390 /* Maybe we should run the scroll functions in Elisp (which already
4391 runs the configuration change hook). */
4392 if (! NILP (Vwindow_scroll_functions))
4393 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4394 Fmarker_position (n->start));
4395 /* Return NEW. */
4396 return new;
4400 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4401 doc: /* Remove WINDOW from its frame.
4402 WINDOW defaults to the selected window. Return nil.
4403 Signal an error when WINDOW is the only window on its frame. */)
4404 (Lisp_Object window)
4406 Lisp_Object parent, sibling, frame, root;
4407 struct window *w, *p, *s, *r;
4408 struct frame *f;
4409 bool horflag, before_sibling = false;
4411 w = decode_any_window (window);
4412 XSETWINDOW (window, w);
4413 if (NILP (w->contents))
4414 /* It's a no-op to delete an already deleted window. */
4415 return Qnil;
4417 parent = w->parent;
4418 if (NILP (parent))
4419 /* Never delete a minibuffer or frame root window. */
4420 error ("Attempt to delete minibuffer or sole ordinary window");
4421 else if (NILP (w->prev) && NILP (w->next))
4422 /* Rather bow out here, this case should be handled on the Elisp
4423 level. */
4424 error ("Attempt to delete sole window of parent");
4426 p = XWINDOW (parent);
4427 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4429 frame = WINDOW_FRAME (w);
4430 f = XFRAME (frame);
4432 root = FRAME_ROOT_WINDOW (f);
4433 r = XWINDOW (root);
4435 /* Unlink WINDOW from window tree. */
4436 if (NILP (w->prev))
4437 /* Get SIBLING below (on the right of) WINDOW. */
4439 /* before_sibling means WINDOW is the first child of its
4440 parent and thus before the sibling. */
4441 before_sibling = true;
4442 sibling = w->next;
4443 s = XWINDOW (sibling);
4444 wset_prev (s, Qnil);
4445 wset_combination (p, horflag, sibling);
4447 else
4448 /* Get SIBLING above (on the left of) WINDOW. */
4450 sibling = w->prev;
4451 s = XWINDOW (sibling);
4452 wset_next (s, w->next);
4453 if (!NILP (s->next))
4454 wset_prev (XWINDOW (s->next), sibling);
4457 if (window_resize_check (r, horflag)
4458 && (XINT (r->new_pixel)
4459 == (horflag ? r->pixel_width : r->pixel_height)))
4460 /* We can delete WINDOW now. */
4463 /* Block input. */
4464 block_input ();
4465 xwidget_view_delete_all_in_window (w);
4466 window_resize_apply (p, horflag);
4467 /* If this window is referred to by the dpyinfo's mouse
4468 highlight, invalidate that slot to be safe (Bug#9904). */
4469 if (!FRAME_INITIAL_P (f))
4471 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4473 if (EQ (hlinfo->mouse_face_window, window))
4474 hlinfo->mouse_face_window = Qnil;
4477 fset_redisplay (f);
4478 Vwindow_list = Qnil;
4480 wset_next (w, Qnil); /* Don't delete w->next too. */
4481 free_window_matrices (w);
4483 if (WINDOWP (w->contents))
4485 delete_all_child_windows (w->contents);
4486 wset_combination (w, false, Qnil);
4488 else
4490 unshow_buffer (w);
4491 unchain_marker (XMARKER (w->pointm));
4492 unchain_marker (XMARKER (w->old_pointm));
4493 unchain_marker (XMARKER (w->start));
4494 wset_buffer (w, Qnil);
4497 if (NILP (s->prev) && NILP (s->next))
4498 /* A matrjoshka where SIBLING has become the only child of
4499 PARENT. */
4501 /* Put SIBLING into PARENT's place. */
4502 replace_window (parent, sibling, false);
4503 /* Have SIBLING inherit the following three slot values from
4504 PARENT (the combination_limit slot is not inherited). */
4505 wset_normal_cols (s, p->normal_cols);
4506 wset_normal_lines (s, p->normal_lines);
4507 /* Mark PARENT as deleted. */
4508 wset_combination (p, false, Qnil);
4509 /* Try to merge SIBLING into its new parent. */
4510 recombine_windows (sibling);
4513 adjust_frame_glyphs (f);
4515 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4516 /* We deleted the frame's selected window. */
4518 /* Use the frame's first window as fallback ... */
4519 Lisp_Object new_selected_window = Fframe_first_window (frame);
4520 /* ... but preferably use its most recently used window. */
4521 Lisp_Object mru_window;
4523 /* `get-mru-window' might fail for some reason so play it safe
4524 - promote the first window _without recording it_ first. */
4525 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4526 Fselect_window (new_selected_window, Qt);
4527 else
4528 fset_selected_window (f, new_selected_window);
4530 unblock_input ();
4532 /* Now look whether `get-mru-window' gets us something. */
4533 mru_window = call1 (Qget_mru_window, frame);
4534 if (WINDOW_LIVE_P (mru_window)
4535 && EQ (XWINDOW (mru_window)->frame, frame))
4536 new_selected_window = mru_window;
4538 /* If all ended up well, we now promote the mru window. */
4539 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4540 Fselect_window (new_selected_window, Qnil);
4541 else
4542 fset_selected_window (f, new_selected_window);
4544 else
4545 unblock_input ();
4547 else
4548 /* We failed: Relink WINDOW into window tree. */
4550 if (before_sibling)
4552 wset_prev (s, window);
4553 wset_combination (p, horflag, window);
4555 else
4557 wset_next (s, window);
4558 if (!NILP (w->next))
4559 wset_prev (XWINDOW (w->next), window);
4561 error ("Deletion failed");
4564 return Qnil;
4567 /***********************************************************************
4568 Resizing Mini-Windows
4569 ***********************************************************************/
4571 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4572 can. */
4573 void
4574 grow_mini_window (struct window *w, int delta, bool pixelwise)
4576 struct frame *f = XFRAME (w->frame);
4577 struct window *r;
4578 Lisp_Object root, height;
4579 int line_height, pixel_height;
4581 eassert (MINI_WINDOW_P (w));
4582 eassert (delta >= 0);
4584 if (delta > 0)
4586 root = FRAME_ROOT_WINDOW (f);
4587 r = XWINDOW (root);
4588 height = call3 (Qwindow__resize_root_window_vertically,
4589 root, make_number (- delta), pixelwise ? Qt : Qnil);
4590 if (INTEGERP (height) && window_resize_check (r, false))
4592 block_input ();
4593 window_resize_apply (r, false);
4595 if (pixelwise)
4597 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4598 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4600 else
4602 line_height = min (-XINT (height),
4603 ((INT_MAX - w->pixel_height)
4604 / FRAME_LINE_HEIGHT (f)));
4605 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4608 /* Grow the mini-window. */
4609 w->pixel_top = r->pixel_top + r->pixel_height;
4610 w->top_line = r->top_line + r->total_lines;
4611 /* Make sure the mini-window has always at least one line. */
4612 w->pixel_height = max (w->pixel_height + pixel_height,
4613 FRAME_LINE_HEIGHT (f));
4614 w->total_lines = max (w->total_lines + line_height, 1);
4616 /* Enforce full redisplay of the frame. */
4617 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4618 fset_redisplay (f);
4619 adjust_frame_glyphs (f);
4620 unblock_input ();
4622 else
4623 error ("Failed to grow minibuffer window");
4628 /* Shrink mini-window W to one line. */
4629 void
4630 shrink_mini_window (struct window *w, bool pixelwise)
4632 struct frame *f = XFRAME (w->frame);
4633 struct window *r;
4634 Lisp_Object root, delta;
4635 EMACS_INT height, unit;
4637 eassert (MINI_WINDOW_P (w));
4639 height = pixelwise ? w->pixel_height : w->total_lines;
4640 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4641 if (height > unit)
4643 root = FRAME_ROOT_WINDOW (f);
4644 r = XWINDOW (root);
4645 delta = call3 (Qwindow__resize_root_window_vertically,
4646 root, make_number (height - unit),
4647 pixelwise ? Qt : Qnil);
4648 if (INTEGERP (delta) && window_resize_check (r, false))
4650 block_input ();
4651 window_resize_apply (r, false);
4653 /* Shrink the mini-window. */
4654 w->top_line = r->top_line + r->total_lines;
4655 w->total_lines = 1;
4656 w->pixel_top = r->pixel_top + r->pixel_height;
4657 w->pixel_height = FRAME_LINE_HEIGHT (f);
4658 /* Enforce full redisplay of the frame. */
4659 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4660 fset_redisplay (f);
4661 adjust_frame_glyphs (f);
4662 unblock_input ();
4664 /* If the above failed for whatever strange reason we must make a
4665 one window frame here. The same routine will be needed when
4666 shrinking the frame (and probably when making the initial
4667 *scratch* window). For the moment leave things as they are. */
4668 else
4669 error ("Failed to shrink minibuffer window");
4673 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4674 doc: /* Resize minibuffer window WINDOW. */)
4675 (Lisp_Object window)
4677 struct window *w = XWINDOW (window);
4678 struct window *r;
4679 struct frame *f;
4680 int height;
4682 CHECK_WINDOW (window);
4683 f = XFRAME (w->frame);
4685 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4686 error ("Not a valid minibuffer window");
4687 else if (FRAME_MINIBUF_ONLY_P (f))
4688 error ("Cannot resize a minibuffer-only frame");
4690 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4691 height = r->pixel_height + w->pixel_height;
4692 if (window_resize_check (r, false)
4693 && XINT (w->new_pixel) > 0
4694 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4696 block_input ();
4697 window_resize_apply (r, false);
4699 w->pixel_height = XFASTINT (w->new_pixel);
4700 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4701 w->pixel_top = r->pixel_top + r->pixel_height;
4702 w->top_line = r->top_line + r->total_lines;
4704 fset_redisplay (f);
4705 adjust_frame_glyphs (f);
4706 unblock_input ();
4707 return Qt;
4709 else
4710 error ("Failed to resize minibuffer window");
4713 /* Mark window cursors off for all windows in the window tree rooted
4714 at W by setting their phys_cursor_on_p flag to zero. Called from
4715 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4716 the frame are cleared. */
4718 void
4719 mark_window_cursors_off (struct window *w)
4721 while (w)
4723 if (WINDOWP (w->contents))
4724 mark_window_cursors_off (XWINDOW (w->contents));
4725 else
4726 w->phys_cursor_on_p = false;
4728 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4733 /* Return number of lines of text (not counting mode lines) in W. */
4736 window_internal_height (struct window *w)
4738 int ht = w->total_lines;
4740 if (!MINI_WINDOW_P (w))
4742 if (!NILP (w->parent)
4743 || WINDOWP (w->contents)
4744 || !NILP (w->next)
4745 || !NILP (w->prev)
4746 || WINDOW_WANTS_MODELINE_P (w))
4747 --ht;
4749 if (WINDOW_WANTS_HEADER_LINE_P (w))
4750 --ht;
4753 return ht;
4757 /************************************************************************
4758 Window Scrolling
4759 ***********************************************************************/
4761 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4762 N screen-fulls, which is defined as the height of the window minus
4763 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4764 instead. Negative values of N mean scroll down. NOERROR
4765 means don't signal an error if we try to move over BEGV or ZV,
4766 respectively. */
4768 static void
4769 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4771 ptrdiff_t count = SPECPDL_INDEX ();
4773 immediate_quit = true;
4774 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4776 wset_redisplay (XWINDOW (window));
4778 if (whole && Vfast_but_imprecise_scrolling)
4779 specbind (Qfontification_functions, Qnil);
4781 /* If we must, use the pixel-based version which is much slower than
4782 the line-based one but can handle varying line heights. */
4783 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4784 window_scroll_pixel_based (window, n, whole, noerror);
4785 else
4786 window_scroll_line_based (window, n, whole, noerror);
4788 unbind_to (count, Qnil);
4790 /* Bug#15957. */
4791 XWINDOW (window)->window_end_valid = false;
4792 immediate_quit = false;
4796 /* Implementation of window_scroll that works based on pixel line
4797 heights. See the comment of window_scroll for parameter
4798 descriptions. */
4800 static void
4801 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4803 struct it it;
4804 struct window *w = XWINDOW (window);
4805 struct text_pos start;
4806 int this_scroll_margin;
4807 /* True if we fiddled the window vscroll field without really scrolling. */
4808 bool vscrolled = false;
4809 int x, y, rtop, rbot, rowh, vpos;
4810 void *itdata = NULL;
4811 int window_total_lines;
4812 int frame_line_height = default_line_pixel_height (w);
4813 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4814 Fwindow_old_point (window)));
4816 SET_TEXT_POS_FROM_MARKER (start, w->start);
4817 /* Scrolling a minibuffer window via scroll bar when the echo area
4818 shows long text sometimes resets the minibuffer contents behind
4819 our backs. Also, someone might narrow-to-region and immediately
4820 call a scroll function. */
4821 if (CHARPOS (start) > ZV || CHARPOS (start) < BEGV)
4822 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4824 /* If PT is not visible in WINDOW, move back one half of
4825 the screen. Allow PT to be partially visible, otherwise
4826 something like (scroll-down 1) with PT in the line before
4827 the partially visible one would recenter. */
4829 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4831 itdata = bidi_shelve_cache ();
4832 /* Move backward half the height of the window. Performance note:
4833 vmotion used here is about 10% faster, but would give wrong
4834 results for variable height lines. */
4835 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4836 it.current_y = it.last_visible_y;
4837 move_it_vertically_backward (&it, window_box_height (w) / 2);
4839 /* The function move_iterator_vertically may move over more than
4840 the specified y-distance. If it->w is small, e.g. a
4841 mini-buffer window, we may end up in front of the window's
4842 display area. Start displaying at the start of the line
4843 containing PT in this case. */
4844 if (it.current_y <= 0)
4846 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4847 move_it_vertically_backward (&it, 0);
4848 it.current_y = 0;
4851 start = it.current.pos;
4852 bidi_unshelve_cache (itdata, false);
4854 else if (auto_window_vscroll_p)
4856 if (rtop || rbot) /* Partially visible. */
4858 int px;
4859 int dy = frame_line_height;
4860 /* In the below we divide the window box height by the
4861 frame's line height to make the result predictable when
4862 the window box is not an integral multiple of the line
4863 height. This is important to ensure we get back to the
4864 same position when scrolling up, then down. */
4865 if (whole)
4866 dy = max ((window_box_height (w) / dy
4867 - next_screen_context_lines) * dy,
4868 dy);
4869 dy *= n;
4871 if (n < 0)
4873 /* Only vscroll backwards if already vscrolled forwards. */
4874 if (w->vscroll < 0 && rtop > 0)
4876 px = max (0, -w->vscroll - min (rtop, -dy));
4877 Fset_window_vscroll (window, make_number (px), Qt);
4878 return;
4881 if (n > 0)
4883 /* Do vscroll if already vscrolled or only display line. */
4884 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4886 px = max (0, -w->vscroll + min (rbot, dy));
4887 Fset_window_vscroll (window, make_number (px), Qt);
4888 return;
4891 /* Maybe modify window start instead of scrolling. */
4892 if (rbot > 0 || w->vscroll < 0)
4894 ptrdiff_t spos;
4896 Fset_window_vscroll (window, make_number (0), Qt);
4897 /* If there are other text lines above the current row,
4898 move window start to current row. Else to next row. */
4899 if (rbot > 0)
4900 spos = XINT (Fline_beginning_position (Qnil));
4901 else
4902 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4903 set_marker_restricted (w->start, make_number (spos),
4904 w->contents);
4905 w->start_at_line_beg = true;
4906 wset_update_mode_line (w);
4907 /* Set force_start so that redisplay_window will run the
4908 window-scroll-functions. */
4909 w->force_start = true;
4910 return;
4914 /* Cancel previous vscroll. */
4915 Fset_window_vscroll (window, make_number (0), Qt);
4918 itdata = bidi_shelve_cache ();
4919 /* If scroll_preserve_screen_position is non-nil, we try to set
4920 point in the same window line as it is now, so get that line. */
4921 if (!NILP (Vscroll_preserve_screen_position))
4923 /* We preserve the goal pixel coordinate across consecutive
4924 calls to scroll-up, scroll-down and other commands that
4925 have the `scroll-command' property. This avoids the
4926 possibility of point becoming "stuck" on a tall line when
4927 scrolling by one line. */
4928 if (window_scroll_pixel_based_preserve_y < 0
4929 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4930 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4932 start_display (&it, w, start);
4933 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4934 window_scroll_pixel_based_preserve_y = it.current_y;
4935 window_scroll_pixel_based_preserve_x = it.current_x;
4938 else
4939 window_scroll_pixel_based_preserve_y
4940 = window_scroll_pixel_based_preserve_x = -1;
4942 /* Move iterator it from start the specified distance forward or
4943 backward. The result is the new window start. */
4944 start_display (&it, w, start);
4945 if (whole)
4947 ptrdiff_t start_pos = IT_CHARPOS (it);
4948 int dy = frame_line_height;
4949 /* In the below we divide the window box height by the frame's
4950 line height to make the result predictable when the window
4951 box is not an integral multiple of the line height. This is
4952 important to ensure we get back to the same position when
4953 scrolling up, then down. */
4954 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4955 dy) * n;
4957 /* Note that move_it_vertically always moves the iterator to the
4958 start of a line. So, if the last line doesn't have a newline,
4959 we would end up at the start of the line ending at ZV. */
4960 if (dy <= 0)
4962 move_it_vertically_backward (&it, -dy);
4963 /* Ensure we actually do move, e.g. in case we are currently
4964 looking at an image that is taller that the window height. */
4965 while (start_pos == IT_CHARPOS (it)
4966 && start_pos > BEGV)
4967 move_it_by_lines (&it, -1);
4969 else if (dy > 0)
4971 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4972 MOVE_TO_POS | MOVE_TO_Y);
4973 /* Ensure we actually do move, e.g. in case we are currently
4974 looking at an image that is taller that the window height. */
4975 while (start_pos == IT_CHARPOS (it)
4976 && start_pos < ZV)
4977 move_it_by_lines (&it, 1);
4980 else
4981 move_it_by_lines (&it, n);
4983 /* We failed if we find ZV is already on the screen (scrolling up,
4984 means there's nothing past the end), or if we can't start any
4985 earlier (scrolling down, means there's nothing past the top). */
4986 if ((n > 0 && IT_CHARPOS (it) == ZV)
4987 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4989 if (IT_CHARPOS (it) == ZV)
4991 if (it.current_y < it.last_visible_y
4992 && (it.current_y + it.max_ascent + it.max_descent
4993 > it.last_visible_y))
4995 /* The last line was only partially visible, make it fully
4996 visible. */
4997 w->vscroll = (it.last_visible_y
4998 - it.current_y + it.max_ascent + it.max_descent);
4999 adjust_frame_glyphs (it.f);
5001 else
5003 bidi_unshelve_cache (itdata, false);
5004 if (noerror)
5005 return;
5006 else if (n < 0) /* could happen with empty buffers */
5007 xsignal0 (Qbeginning_of_buffer);
5008 else
5009 xsignal0 (Qend_of_buffer);
5012 else
5014 if (w->vscroll != 0)
5015 /* The first line was only partially visible, make it fully
5016 visible. */
5017 w->vscroll = 0;
5018 else
5020 bidi_unshelve_cache (itdata, false);
5021 if (noerror)
5022 return;
5023 else
5024 xsignal0 (Qbeginning_of_buffer);
5028 /* If control gets here, then we vscrolled. */
5030 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5032 /* Don't try to change the window start below. */
5033 vscrolled = true;
5036 if (! vscrolled)
5038 ptrdiff_t pos = IT_CHARPOS (it);
5039 ptrdiff_t bytepos;
5041 /* If in the middle of a multi-glyph character move forward to
5042 the next character. */
5043 if (in_display_vector_p (&it))
5045 ++pos;
5046 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5049 /* Set the window start, and set up the window for redisplay. */
5050 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5051 IT_BYTEPOS (it));
5052 bytepos = marker_byte_position (w->start);
5053 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5054 wset_update_mode_line (w);
5055 /* Set force_start so that redisplay_window will run the
5056 window-scroll-functions. */
5057 w->force_start = true;
5060 /* The rest of this function uses current_y in a nonstandard way,
5061 not including the height of the header line if any. */
5062 it.current_y = it.vpos = 0;
5064 /* Move PT out of scroll margins.
5065 This code wants current_y to be zero at the window start position
5066 even if there is a header line. */
5067 window_total_lines
5068 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5069 this_scroll_margin = max (0, scroll_margin);
5070 this_scroll_margin
5071 = min (this_scroll_margin, window_total_lines / 4);
5072 this_scroll_margin *= frame_line_height;
5074 if (n > 0)
5076 int last_y = it.last_visible_y - this_scroll_margin - 1;
5078 /* We moved the window start towards ZV, so PT may be now
5079 in the scroll margin at the top. */
5080 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5081 if (IT_CHARPOS (it) == PT
5082 && it.current_y >= this_scroll_margin
5083 && it.current_y <= last_y - WINDOW_HEADER_LINE_HEIGHT (w)
5084 && (NILP (Vscroll_preserve_screen_position)
5085 || EQ (Vscroll_preserve_screen_position, Qt)))
5086 /* We found PT at a legitimate height. Leave it alone. */
5088 else
5090 if (window_scroll_pixel_based_preserve_y >= 0)
5092 /* Don't enter the scroll margin at the end of the window. */
5093 int goal_y = min (last_y, window_scroll_pixel_based_preserve_y);
5095 /* If we have a header line, take account of it. This
5096 is necessary because we set it.current_y to 0, above. */
5097 move_it_to (&it, -1,
5098 window_scroll_pixel_based_preserve_x,
5099 goal_y - WINDOW_HEADER_LINE_HEIGHT (w),
5100 -1, MOVE_TO_Y | MOVE_TO_X);
5103 /* Get out of the scroll margin at the top of the window. */
5104 while (it.current_y < this_scroll_margin)
5106 int prev = it.current_y;
5107 move_it_by_lines (&it, 1);
5108 if (prev == it.current_y)
5109 break;
5111 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5114 else if (n < 0)
5116 ptrdiff_t charpos, bytepos;
5117 bool partial_p;
5119 /* Save our position, for the
5120 window_scroll_pixel_based_preserve_y case. */
5121 charpos = IT_CHARPOS (it);
5122 bytepos = IT_BYTEPOS (it);
5124 /* We moved the window start towards BEGV, so PT may be now
5125 in the scroll margin at the bottom. */
5126 move_it_to (&it, PT, -1,
5127 (it.last_visible_y - WINDOW_HEADER_LINE_HEIGHT (w)
5128 - this_scroll_margin - 1),
5130 MOVE_TO_POS | MOVE_TO_Y);
5132 /* Save our position, in case it's correct. */
5133 charpos = IT_CHARPOS (it);
5134 bytepos = IT_BYTEPOS (it);
5136 /* If PT is in the screen line at the last fully visible line,
5137 move_it_to will stop at X = 0 in that line, because the
5138 required Y coordinate is reached there. See if we can get to
5139 PT without descending lower in Y, and if we can, it means we
5140 reached PT before the scroll margin. */
5141 if (charpos != PT)
5143 struct it it2;
5144 void *it_data;
5146 it2 = it;
5147 it_data = bidi_shelve_cache ();
5148 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5149 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5151 charpos = IT_CHARPOS (it);
5152 bytepos = IT_BYTEPOS (it);
5153 bidi_unshelve_cache (it_data, true);
5155 else
5157 it = it2;
5158 bidi_unshelve_cache (it_data, false);
5162 /* See if point is on a partially visible line at the end. */
5163 if (it.what == IT_EOB)
5164 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5165 else
5167 move_it_by_lines (&it, 1);
5168 partial_p = it.current_y > it.last_visible_y;
5171 if (charpos == PT && !partial_p
5172 && (NILP (Vscroll_preserve_screen_position)
5173 || EQ (Vscroll_preserve_screen_position, Qt)))
5174 /* We found PT before we found the display margin, so PT is ok. */
5176 else if (window_scroll_pixel_based_preserve_y >= 0)
5178 int goal_y = min (it.last_visible_y - this_scroll_margin - 1,
5179 window_scroll_pixel_based_preserve_y);
5181 /* Don't let the preserved screen Y coordinate put us inside
5182 any of the two margins. */
5183 if (goal_y < this_scroll_margin)
5184 goal_y = this_scroll_margin;
5185 SET_TEXT_POS_FROM_MARKER (start, w->start);
5186 start_display (&it, w, start);
5187 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5188 here because we called start_display again and did not
5189 alter it.current_y this time. */
5190 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5191 goal_y, -1, MOVE_TO_Y | MOVE_TO_X);
5192 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5194 else
5196 if (partial_p)
5197 /* The last line was only partially visible, so back up two
5198 lines to make sure we're on a fully visible line. */
5200 move_it_by_lines (&it, -2);
5201 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5203 else
5204 /* No, the position we saved is OK, so use it. */
5205 SET_PT_BOTH (charpos, bytepos);
5208 bidi_unshelve_cache (itdata, false);
5210 if (adjust_old_pointm)
5211 Fset_marker (w->old_pointm,
5212 ((w == XWINDOW (selected_window))
5213 ? make_number (BUF_PT (XBUFFER (w->contents)))
5214 : Fmarker_position (w->pointm)),
5215 w->contents);
5219 /* Implementation of window_scroll that works based on screen lines.
5220 See the comment of window_scroll for parameter descriptions. */
5222 static void
5223 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5225 struct window *w = XWINDOW (window);
5226 /* Fvertical_motion enters redisplay, which can trigger
5227 fontification, which in turn can modify buffer text (e.g., if the
5228 fontification functions replace escape sequences with faces, as
5229 in `grep-mode-font-lock-keywords'). So we use a marker to record
5230 the old point position, to prevent crashes in SET_PT_BOTH. */
5231 Lisp_Object opoint_marker = Fpoint_marker ();
5232 register ptrdiff_t pos, pos_byte;
5233 register int ht = window_internal_height (w);
5234 register Lisp_Object tem;
5235 bool lose;
5236 Lisp_Object bolp;
5237 ptrdiff_t startpos = marker_position (w->start);
5238 ptrdiff_t startbyte = marker_byte_position (w->start);
5239 Lisp_Object original_pos = Qnil;
5240 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5241 Fwindow_old_point (window)));
5243 /* If scrolling screen-fulls, compute the number of lines to
5244 scroll from the window's height. */
5245 if (whole)
5246 n *= max (1, ht - next_screen_context_lines);
5248 if (!NILP (Vscroll_preserve_screen_position))
5250 if (window_scroll_preserve_vpos <= 0
5251 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5252 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5254 struct position posit
5255 = *compute_motion (startpos, startbyte, 0, 0, false,
5256 PT, ht, 0, -1, w->hscroll, 0, w);
5258 window_scroll_preserve_vpos = posit.vpos;
5259 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5262 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5263 make_number (window_scroll_preserve_vpos));
5266 XSETFASTINT (tem, PT);
5267 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5269 if (NILP (tem))
5271 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5272 startpos = PT;
5273 startbyte = PT_BYTE;
5276 SET_PT_BOTH (startpos, startbyte);
5277 lose = n < 0 && PT == BEGV;
5278 Fvertical_motion (make_number (n), window, Qnil);
5279 pos = PT;
5280 pos_byte = PT_BYTE;
5281 bolp = Fbolp ();
5282 SET_PT_BOTH (marker_position (opoint_marker),
5283 marker_byte_position (opoint_marker));
5285 if (lose)
5287 if (noerror)
5288 return;
5289 else
5290 xsignal0 (Qbeginning_of_buffer);
5293 if (pos < ZV)
5295 /* Don't use a scroll margin that is negative or too large. */
5296 int this_scroll_margin =
5297 max (0, min (scroll_margin, w->total_lines / 4));
5299 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5300 w->start_at_line_beg = !NILP (bolp);
5301 wset_update_mode_line (w);
5302 /* Set force_start so that redisplay_window will run
5303 the window-scroll-functions. */
5304 w->force_start = true;
5306 if (!NILP (Vscroll_preserve_screen_position)
5307 && this_scroll_margin == 0
5308 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5310 SET_PT_BOTH (pos, pos_byte);
5311 Fvertical_motion (original_pos, window, Qnil);
5313 /* If we scrolled forward, put point enough lines down
5314 that it is outside the scroll margin. */
5315 else if (n > 0)
5317 int top_margin;
5319 if (this_scroll_margin > 0)
5321 SET_PT_BOTH (pos, pos_byte);
5322 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5323 top_margin = PT;
5325 else
5326 top_margin = pos;
5328 if (top_margin <= marker_position (opoint_marker))
5329 SET_PT_BOTH (marker_position (opoint_marker),
5330 marker_byte_position (opoint_marker));
5331 else if (!NILP (Vscroll_preserve_screen_position))
5333 int nlines = window_scroll_preserve_vpos;
5335 SET_PT_BOTH (pos, pos_byte);
5336 if (window_scroll_preserve_vpos < this_scroll_margin)
5337 nlines = this_scroll_margin;
5338 else if (window_scroll_preserve_vpos
5339 >= w->total_lines - this_scroll_margin)
5340 nlines = w->total_lines - this_scroll_margin - 1;
5341 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5342 make_number (nlines)), window, Qnil);
5344 else
5345 SET_PT (top_margin);
5347 else if (n < 0)
5349 int bottom_margin;
5351 /* If we scrolled backward, put point near the end of the window
5352 but not within the scroll margin. */
5353 SET_PT_BOTH (pos, pos_byte);
5354 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5355 Qnil);
5356 if (XFASTINT (tem) == ht - this_scroll_margin)
5357 bottom_margin = PT;
5358 else
5359 bottom_margin = PT + 1;
5361 if (bottom_margin > marker_position (opoint_marker))
5362 SET_PT_BOTH (marker_position (opoint_marker),
5363 marker_byte_position (opoint_marker));
5364 else
5366 if (!NILP (Vscroll_preserve_screen_position))
5368 int nlines = window_scroll_preserve_vpos;
5370 SET_PT_BOTH (pos, pos_byte);
5371 if (window_scroll_preserve_vpos < this_scroll_margin)
5372 nlines = this_scroll_margin;
5373 else if (window_scroll_preserve_vpos
5374 >= ht - this_scroll_margin)
5375 nlines = ht - this_scroll_margin - 1;
5376 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5377 make_number (nlines)), window, Qnil);
5379 else
5380 Fvertical_motion (make_number (-1), window, Qnil);
5384 else
5386 if (noerror)
5387 return;
5388 else
5389 xsignal0 (Qend_of_buffer);
5392 if (adjust_old_pointm)
5393 Fset_marker (w->old_pointm,
5394 ((w == XWINDOW (selected_window))
5395 ? make_number (BUF_PT (XBUFFER (w->contents)))
5396 : Fmarker_position (w->pointm)),
5397 w->contents);
5401 /* Scroll selected_window up or down. If N is nil, scroll a
5402 screen-full which is defined as the height of the window minus
5403 next_screen_context_lines. If N is the symbol `-', scroll.
5404 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5405 up. This is the guts of Fscroll_up and Fscroll_down. */
5407 static void
5408 scroll_command (Lisp_Object n, int direction)
5410 ptrdiff_t count = SPECPDL_INDEX ();
5412 eassert (eabs (direction) == 1);
5414 /* If selected window's buffer isn't current, make it current for
5415 the moment. But don't screw up if window_scroll gets an error. */
5416 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5418 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5419 Fset_buffer (XWINDOW (selected_window)->contents);
5422 if (NILP (n))
5423 window_scroll (selected_window, direction, true, false);
5424 else if (EQ (n, Qminus))
5425 window_scroll (selected_window, -direction, true, false);
5426 else
5428 n = Fprefix_numeric_value (n);
5429 window_scroll (selected_window, XINT (n) * direction, false, false);
5432 unbind_to (count, Qnil);
5435 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5436 doc: /* Scroll text of selected window upward ARG lines.
5437 If ARG is omitted or nil, scroll upward by a near full screen.
5438 A near full screen is `next-screen-context-lines' less than a full screen.
5439 Negative ARG means scroll downward.
5440 If ARG is the atom `-', scroll downward by nearly full screen.
5441 When calling from a program, supply as argument a number, nil, or `-'. */)
5442 (Lisp_Object arg)
5444 scroll_command (arg, 1);
5445 return Qnil;
5448 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5449 doc: /* Scroll text of selected window down ARG lines.
5450 If ARG is omitted or nil, scroll down by a near full screen.
5451 A near full screen is `next-screen-context-lines' less than a full screen.
5452 Negative ARG means scroll upward.
5453 If ARG is the atom `-', scroll upward by nearly full screen.
5454 When calling from a program, supply as argument a number, nil, or `-'. */)
5455 (Lisp_Object arg)
5457 scroll_command (arg, -1);
5458 return Qnil;
5461 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5462 doc: /* Return the other window for \"other window scroll\" commands.
5463 If `other-window-scroll-buffer' is non-nil, a window
5464 showing that buffer is used.
5465 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5466 specifies the window. This takes precedence over
5467 `other-window-scroll-buffer'. */)
5468 (void)
5470 Lisp_Object window;
5472 if (MINI_WINDOW_P (XWINDOW (selected_window))
5473 && !NILP (Vminibuf_scroll_window))
5474 window = Vminibuf_scroll_window;
5475 /* If buffer is specified and live, scroll that buffer. */
5476 else if (!NILP (Vother_window_scroll_buffer)
5477 && BUFFERP (Vother_window_scroll_buffer)
5478 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5480 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5481 if (NILP (window))
5482 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5484 else
5486 /* Nothing specified; look for a neighboring window on the same
5487 frame. */
5488 window = Fnext_window (selected_window, Qnil, Qnil);
5490 if (EQ (window, selected_window))
5491 /* That didn't get us anywhere; look for a window on another
5492 visible frame. */
5494 window = Fnext_window (window, Qnil, Qt);
5495 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5496 && ! EQ (window, selected_window));
5499 CHECK_LIVE_WINDOW (window);
5501 if (EQ (window, selected_window))
5502 error ("There is no other window");
5504 return window;
5507 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5508 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5509 A near full screen is `next-screen-context-lines' less than a full screen.
5510 The next window is the one below the current one; or the one at the top
5511 if the current one is at the bottom. Negative ARG means scroll downward.
5512 If ARG is the atom `-', scroll downward by nearly full screen.
5513 When calling from a program, supply as argument a number, nil, or `-'.
5515 If `other-window-scroll-buffer' is non-nil, scroll the window
5516 showing that buffer, popping the buffer up if necessary.
5517 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5518 specifies the window to scroll. This takes precedence over
5519 `other-window-scroll-buffer'. */)
5520 (Lisp_Object arg)
5522 Lisp_Object window;
5523 struct window *w;
5524 ptrdiff_t count = SPECPDL_INDEX ();
5526 window = Fother_window_for_scrolling ();
5527 w = XWINDOW (window);
5529 /* Don't screw up if window_scroll gets an error. */
5530 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5532 Fset_buffer (w->contents);
5533 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5534 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5536 if (NILP (arg))
5537 window_scroll (window, 1, true, true);
5538 else if (EQ (arg, Qminus))
5539 window_scroll (window, -1, true, true);
5540 else
5542 if (CONSP (arg))
5543 arg = XCAR (arg);
5544 CHECK_NUMBER (arg);
5545 window_scroll (window, XINT (arg), false, true);
5548 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5549 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5550 unbind_to (count, Qnil);
5552 return Qnil;
5555 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5556 doc: /* Scroll selected window display ARG columns left.
5557 Default for ARG is window width minus 2.
5558 Value is the total amount of leftward horizontal scrolling in
5559 effect after the change.
5560 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5561 lower bound for automatic scrolling, i.e. automatic scrolling
5562 will not scroll a window to a column less than the value returned
5563 by this function. This happens in an interactive call. */)
5564 (register Lisp_Object arg, Lisp_Object set_minimum)
5566 struct window *w = XWINDOW (selected_window);
5567 EMACS_INT requested_arg = (NILP (arg)
5568 ? window_body_width (w, 0) - 2
5569 : XINT (Fprefix_numeric_value (arg)));
5570 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5572 if (!NILP (set_minimum))
5573 w->min_hscroll = w->hscroll;
5575 w->suspend_auto_hscroll = true;
5577 return result;
5580 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5581 doc: /* Scroll selected window display ARG columns right.
5582 Default for ARG is window width minus 2.
5583 Value is the total amount of leftward horizontal scrolling in
5584 effect after the change.
5585 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5586 lower bound for automatic scrolling, i.e. automatic scrolling
5587 will not scroll a window to a column less than the value returned
5588 by this function. This happens in an interactive call. */)
5589 (register Lisp_Object arg, Lisp_Object set_minimum)
5591 struct window *w = XWINDOW (selected_window);
5592 EMACS_INT requested_arg = (NILP (arg)
5593 ? window_body_width (w, 0) - 2
5594 : XINT (Fprefix_numeric_value (arg)));
5595 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5597 if (!NILP (set_minimum))
5598 w->min_hscroll = w->hscroll;
5600 w->suspend_auto_hscroll = true;
5602 return result;
5605 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5606 doc: /* Return the window which was selected when entering the minibuffer.
5607 Returns nil, if selected window is not a minibuffer window. */)
5608 (void)
5610 if (minibuf_level > 0
5611 && MINI_WINDOW_P (XWINDOW (selected_window))
5612 && WINDOW_LIVE_P (minibuf_selected_window))
5613 return minibuf_selected_window;
5615 return Qnil;
5618 /* Value is the number of lines actually displayed in window W,
5619 as opposed to its height. */
5621 static int
5622 displayed_window_lines (struct window *w)
5624 struct it it;
5625 struct text_pos start;
5626 int height = window_box_height (w);
5627 struct buffer *old_buffer;
5628 int bottom_y;
5629 void *itdata = NULL;
5631 if (XBUFFER (w->contents) != current_buffer)
5633 old_buffer = current_buffer;
5634 set_buffer_internal (XBUFFER (w->contents));
5636 else
5637 old_buffer = NULL;
5639 /* In case W->start is out of the accessible range, do something
5640 reasonable. This happens in Info mode when Info-scroll-down
5641 calls (recenter -1) while W->start is 1. */
5642 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5644 itdata = bidi_shelve_cache ();
5645 start_display (&it, w, start);
5646 move_it_vertically (&it, height);
5647 bottom_y = line_bottom_y (&it);
5648 bidi_unshelve_cache (itdata, false);
5650 /* Add in empty lines at the bottom of the window. */
5651 if (bottom_y < height)
5653 int uy = FRAME_LINE_HEIGHT (it.f);
5654 it.vpos += (height - bottom_y + uy - 1) / uy;
5656 else if (bottom_y == height)
5657 it.vpos++;
5659 if (old_buffer)
5660 set_buffer_internal (old_buffer);
5662 return it.vpos;
5666 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5667 doc: /* Center point in selected window and maybe redisplay frame.
5668 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5669 relative to the selected window. If ARG is negative, it counts up from the
5670 bottom of the window. (ARG should be less than the height of the window.)
5672 If ARG is omitted or nil, then recenter with point on the middle line of
5673 the selected window; if the variable `recenter-redisplay' is non-nil,
5674 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5675 is set to `grow-only', this resets the tool-bar's height to the minimum
5676 height needed); if `recenter-redisplay' has the special value `tty',
5677 then only tty frames are redrawn.
5679 Just C-u as prefix means put point in the center of the window
5680 and redisplay normally--don't erase and redraw the frame. */)
5681 (register Lisp_Object arg)
5683 struct window *w = XWINDOW (selected_window);
5684 struct buffer *buf = XBUFFER (w->contents);
5685 bool center_p = false;
5686 ptrdiff_t charpos, bytepos;
5687 EMACS_INT iarg UNINIT;
5688 int this_scroll_margin;
5690 if (buf != current_buffer)
5691 error ("`recenter'ing a window that does not display current-buffer.");
5693 /* If redisplay is suppressed due to an error, try again. */
5694 buf->display_error_modiff = 0;
5696 if (NILP (arg))
5698 if (!NILP (Vrecenter_redisplay)
5699 && (!EQ (Vrecenter_redisplay, Qtty)
5700 || !NILP (Ftty_type (selected_frame))))
5702 ptrdiff_t i;
5704 /* Invalidate pixel data calculated for all compositions. */
5705 for (i = 0; i < n_compositions; i++)
5706 composition_table[i]->font = NULL;
5707 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5708 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5709 #endif
5710 Fredraw_frame (WINDOW_FRAME (w));
5711 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5714 center_p = true;
5716 else if (CONSP (arg)) /* Just C-u. */
5717 center_p = true;
5718 else
5720 arg = Fprefix_numeric_value (arg);
5721 CHECK_NUMBER (arg);
5722 iarg = XINT (arg);
5725 /* Do this after making BUF current
5726 in case scroll_margin is buffer-local. */
5727 this_scroll_margin
5728 = max (0, min (scroll_margin, w->total_lines / 4));
5730 /* Don't use redisplay code for initial frames, as the necessary
5731 data structures might not be set up yet then. */
5732 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5734 if (center_p)
5736 struct it it;
5737 struct text_pos pt;
5738 void *itdata = bidi_shelve_cache ();
5740 SET_TEXT_POS (pt, PT, PT_BYTE);
5741 start_display (&it, w, pt);
5742 move_it_vertically_backward (&it, window_box_height (w) / 2);
5743 charpos = IT_CHARPOS (it);
5744 bytepos = IT_BYTEPOS (it);
5745 bidi_unshelve_cache (itdata, false);
5747 else if (iarg < 0)
5749 struct it it;
5750 struct text_pos pt;
5751 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5752 int extra_line_spacing;
5753 int h = window_box_height (w);
5754 int ht = window_internal_height (w);
5755 void *itdata = bidi_shelve_cache ();
5757 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5758 ht - this_scroll_margin);
5760 SET_TEXT_POS (pt, PT, PT_BYTE);
5761 start_display (&it, w, pt);
5763 /* Be sure we have the exact height of the full line containing PT. */
5764 move_it_by_lines (&it, 0);
5766 /* The amount of pixels we have to move back is the window
5767 height minus what's displayed in the line containing PT,
5768 and the lines below. */
5769 it.current_y = 0;
5770 it.vpos = 0;
5771 move_it_by_lines (&it, nlines);
5773 if (it.vpos == nlines)
5774 h -= it.current_y;
5775 else
5777 /* Last line has no newline. */
5778 h -= line_bottom_y (&it);
5779 it.vpos++;
5782 /* Don't reserve space for extra line spacing of last line. */
5783 extra_line_spacing = it.max_extra_line_spacing;
5785 /* If we can't move down NLINES lines because we hit
5786 the end of the buffer, count in some empty lines. */
5787 if (it.vpos < nlines)
5789 nlines -= it.vpos;
5790 extra_line_spacing = it.extra_line_spacing;
5791 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5793 if (h <= 0)
5795 bidi_unshelve_cache (itdata, false);
5796 return Qnil;
5799 /* Now find the new top line (starting position) of the window. */
5800 start_display (&it, w, pt);
5801 it.current_y = 0;
5802 move_it_vertically_backward (&it, h);
5804 /* If extra line spacing is present, we may move too far
5805 back. This causes the last line to be only partially
5806 visible (which triggers redisplay to recenter that line
5807 in the middle), so move forward.
5808 But ignore extra line spacing on last line, as it is not
5809 considered to be part of the visible height of the line.
5811 h += extra_line_spacing;
5812 while (-it.current_y > h)
5813 move_it_by_lines (&it, 1);
5815 charpos = IT_CHARPOS (it);
5816 bytepos = IT_BYTEPOS (it);
5818 bidi_unshelve_cache (itdata, false);
5820 else
5822 struct it it;
5823 struct text_pos pt;
5824 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5825 int ht = window_internal_height (w);
5826 void *itdata = bidi_shelve_cache ();
5828 nlines = clip_to_bounds (this_scroll_margin, nlines,
5829 ht - this_scroll_margin - 1);
5831 SET_TEXT_POS (pt, PT, PT_BYTE);
5832 start_display (&it, w, pt);
5834 /* Move to the beginning of screen line containing PT. */
5835 move_it_by_lines (&it, 0);
5837 /* Move back to find the point which is ARG screen lines above PT. */
5838 if (nlines > 0)
5840 it.current_y = 0;
5841 it.vpos = 0;
5842 move_it_by_lines (&it, -nlines);
5845 charpos = IT_CHARPOS (it);
5846 bytepos = IT_BYTEPOS (it);
5848 bidi_unshelve_cache (itdata, false);
5851 else
5853 struct position pos;
5854 int ht = window_internal_height (w);
5856 if (center_p)
5857 iarg = ht / 2;
5858 else if (iarg < 0)
5859 iarg += ht;
5861 /* Don't let it get into the margin at either top or bottom. */
5862 iarg = clip_to_bounds (this_scroll_margin, iarg,
5863 ht - this_scroll_margin - 1);
5865 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5866 charpos = pos.bufpos;
5867 bytepos = pos.bytepos;
5870 /* Set the new window start. */
5871 set_marker_both (w->start, w->contents, charpos, bytepos);
5872 w->window_end_valid = false;
5874 w->optional_new_start = true;
5876 w->start_at_line_beg = (bytepos == BEGV_BYTE
5877 || FETCH_BYTE (bytepos - 1) == '\n');
5879 wset_redisplay (w);
5881 return Qnil;
5884 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5885 0, 2, 0,
5886 doc: /* Return the width in columns of the text display area of WINDOW.
5887 WINDOW must be a live window and defaults to the selected one.
5889 The returned width does not include dividers, scrollbars, margins,
5890 fringes, nor any partial-width columns at the right of the text
5891 area.
5893 Optional argument PIXELWISE non-nil, means to return the width in
5894 pixels. */)
5895 (Lisp_Object window, Lisp_Object pixelwise)
5897 struct window *w = decode_live_window (window);
5899 if (NILP (pixelwise))
5900 return make_number (window_box_width (w, TEXT_AREA)
5901 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5902 else
5903 return make_number (window_box_width (w, TEXT_AREA));
5906 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5907 0, 2, 0,
5908 doc: /* Return the height in lines of the text display area of WINDOW.
5909 WINDOW must be a live window and defaults to the selected one.
5911 The returned height does not include dividers, the mode line, any header
5912 line, nor any partial-height lines at the bottom of the text area.
5914 Optional argument PIXELWISE non-nil, means to return the height in
5915 pixels. */)
5916 (Lisp_Object window, Lisp_Object pixelwise)
5918 struct window *w = decode_live_window (window);
5920 if (NILP (pixelwise))
5921 return make_number (window_box_height (w)
5922 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5923 else
5924 return make_number (window_box_height (w));
5927 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5928 1, 1, "P",
5929 doc: /* Position point relative to window.
5930 ARG nil means position point at center of window.
5931 Else, ARG specifies vertical position within the window;
5932 zero means top of window, negative means relative to bottom
5933 of window, -1 meaning the last fully visible display line
5934 of the window.
5936 Value is the screen line of the window point moved to, counting
5937 from the top of the window. */)
5938 (Lisp_Object arg)
5940 struct window *w = XWINDOW (selected_window);
5941 int lines, start;
5942 Lisp_Object window;
5943 #if false
5944 int this_scroll_margin;
5945 #endif
5947 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5948 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5949 when passed below to set_marker_both. */
5950 error ("move-to-window-line called from unrelated buffer");
5952 window = selected_window;
5953 start = marker_position (w->start);
5954 if (start < BEGV || start > ZV)
5956 int height = window_internal_height (w);
5957 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5958 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5959 w->start_at_line_beg = !NILP (Fbolp ());
5960 w->force_start = true;
5962 else
5963 Fgoto_char (w->start);
5965 lines = displayed_window_lines (w);
5967 #if false
5968 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5969 #endif
5971 if (NILP (arg))
5972 XSETFASTINT (arg, lines / 2);
5973 else
5975 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5977 if (iarg < 0)
5978 iarg = iarg + lines;
5980 #if false /* This code would prevent move-to-window-line from moving point
5981 to a place inside the scroll margins (which would cause the
5982 next redisplay to scroll). I wrote this code, but then concluded
5983 it is probably better not to install it. However, it is here
5984 inside #if false so as not to lose it. -- rms. */
5986 /* Don't let it get into the margin at either top or bottom. */
5987 iarg = max (iarg, this_scroll_margin);
5988 iarg = min (iarg, lines - this_scroll_margin - 1);
5989 #endif
5991 arg = make_number (iarg);
5994 /* Skip past a partially visible first line. */
5995 if (w->vscroll)
5996 XSETINT (arg, XINT (arg) + 1);
5998 return Fvertical_motion (arg, window, Qnil);
6003 /***********************************************************************
6004 Window Configuration
6005 ***********************************************************************/
6007 struct save_window_data
6009 struct vectorlike_header header;
6010 Lisp_Object selected_frame;
6011 Lisp_Object current_window;
6012 Lisp_Object f_current_buffer;
6013 Lisp_Object minibuf_scroll_window;
6014 Lisp_Object minibuf_selected_window;
6015 Lisp_Object root_window;
6016 Lisp_Object focus_frame;
6017 /* A vector, each of whose elements is a struct saved_window
6018 for one window. */
6019 Lisp_Object saved_windows;
6021 /* All fields above are traced by the GC.
6022 From `frame-cols' down, the fields are ignored by the GC. */
6023 /* We should be able to do without the following two. */
6024 int frame_cols, frame_lines;
6025 /* These two should get eventually replaced by their pixel
6026 counterparts. */
6027 int frame_menu_bar_lines, frame_tool_bar_lines;
6028 int frame_text_width, frame_text_height;
6029 /* These are currently unused. We need them as soon as we convert
6030 to pixels. */
6031 int frame_menu_bar_height, frame_tool_bar_height;
6034 /* This is saved as a Lisp_Vector. */
6035 struct saved_window
6037 struct vectorlike_header header;
6039 Lisp_Object window, buffer, start, pointm, old_pointm;
6040 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6041 Lisp_Object pixel_height_before_size_change, pixel_width_before_size_change;
6042 Lisp_Object left_col, top_line, total_cols, total_lines;
6043 Lisp_Object normal_cols, normal_lines;
6044 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6045 Lisp_Object parent, prev;
6046 Lisp_Object start_at_line_beg;
6047 Lisp_Object display_table;
6048 Lisp_Object left_margin_cols, right_margin_cols;
6049 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6050 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6051 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6052 Lisp_Object combination_limit, window_parameters;
6055 #define SAVED_WINDOW_N(swv,n) \
6056 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6058 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6059 doc: /* Return t if OBJECT is a window-configuration object. */)
6060 (Lisp_Object object)
6062 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6065 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6066 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6067 (Lisp_Object config)
6069 register struct save_window_data *data;
6070 struct Lisp_Vector *saved_windows;
6072 CHECK_WINDOW_CONFIGURATION (config);
6074 data = (struct save_window_data *) XVECTOR (config);
6075 saved_windows = XVECTOR (data->saved_windows);
6076 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6079 DEFUN ("set-window-configuration", Fset_window_configuration,
6080 Sset_window_configuration, 1, 1, 0,
6081 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6082 CONFIGURATION must be a value previously returned
6083 by `current-window-configuration' (which see).
6084 If CONFIGURATION was made from a frame that is now deleted,
6085 only frame-independent values can be restored. In this case,
6086 the return value is nil. Otherwise the value is t. */)
6087 (Lisp_Object configuration)
6089 register struct save_window_data *data;
6090 struct Lisp_Vector *saved_windows;
6091 Lisp_Object new_current_buffer;
6092 Lisp_Object frame;
6093 struct frame *f;
6094 ptrdiff_t old_point = -1;
6095 USE_SAFE_ALLOCA;
6097 CHECK_WINDOW_CONFIGURATION (configuration);
6099 data = (struct save_window_data *) XVECTOR (configuration);
6100 saved_windows = XVECTOR (data->saved_windows);
6102 new_current_buffer = data->f_current_buffer;
6103 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6104 new_current_buffer = Qnil;
6105 else
6107 if (XBUFFER (new_current_buffer) == current_buffer)
6108 /* The code further down "preserves point" by saving here PT in
6109 old_point and then setting it later back into PT. When the
6110 current-selected-window and the final-selected-window both show
6111 the current buffer, this suffers from the problem that the
6112 current PT is the window-point of the current-selected-window,
6113 while the final PT is the point of the final-selected-window, so
6114 this copy from one PT to the other would end up moving the
6115 window-point of the final-selected-window to the window-point of
6116 the current-selected-window. So we have to be careful which
6117 point of the current-buffer we copy into old_point. */
6118 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6119 && WINDOWP (selected_window)
6120 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6121 && !EQ (selected_window, data->current_window))
6122 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6123 else
6124 old_point = PT;
6125 else
6126 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6127 point in new_current_buffer as of the last time this buffer was
6128 used. This can be non-deterministic since it can be changed by
6129 things like jit-lock by mere temporary selection of some random
6130 window that happens to show this buffer.
6131 So if possible we want this arbitrary choice of "which point" to
6132 be the one from the to-be-selected-window so as to prevent this
6133 window's cursor from being copied from another window. */
6134 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6135 /* If current_window = selected_window, its point is in BUF_PT. */
6136 && !EQ (selected_window, data->current_window))
6137 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6138 else
6139 old_point = BUF_PT (XBUFFER (new_current_buffer));
6142 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6143 f = XFRAME (frame);
6145 /* If f is a dead frame, don't bother rebuilding its window tree.
6146 However, there is other stuff we should still try to do below. */
6147 if (FRAME_LIVE_P (f))
6149 Lisp_Object window;
6150 Lisp_Object dead_windows = Qnil;
6151 Lisp_Object tem, par, pers;
6152 struct window *w;
6153 struct saved_window *p;
6154 struct window *root_window;
6155 struct window **leaf_windows;
6156 ptrdiff_t i, k, n_leaf_windows;
6157 /* Records whether a window has been added or removed wrt the
6158 original configuration. */
6159 bool window_changed = false;
6160 /* Records whether a window has changed its buffer wrt the
6161 original configuration. */
6162 bool buffer_changed = false;
6164 /* Don't do this within the main loop below: This may call Lisp
6165 code and is thus potentially unsafe while input is blocked. */
6166 for (k = 0; k < saved_windows->header.size; k++)
6168 p = SAVED_WINDOW_N (saved_windows, k);
6169 window = p->window;
6170 w = XWINDOW (window);
6172 if (NILP (w->contents))
6173 /* A dead window that will be resurrected, the window
6174 configuration will change. */
6175 window_changed = true;
6177 if (BUFFERP (w->contents)
6178 && !EQ (w->contents, p->buffer)
6179 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6180 /* If a window we restore gets another buffer, record the
6181 window's old buffer. */
6182 call1 (Qrecord_window_buffer, window);
6185 /* Disallow x_set_window_size, temporarily. */
6186 f->can_x_set_window_size = false;
6187 /* The mouse highlighting code could get screwed up
6188 if it runs during this. */
6189 block_input ();
6191 /* "Swap out" point from the selected window's buffer
6192 into the window itself. (Normally the pointm of the selected
6193 window holds garbage.) We do this now, before
6194 restoring the window contents, and prevent it from
6195 being done later on when we select a new window. */
6196 if (! NILP (XWINDOW (selected_window)->contents))
6198 w = XWINDOW (selected_window);
6199 set_marker_both (w->pointm,
6200 w->contents,
6201 BUF_PT (XBUFFER (w->contents)),
6202 BUF_PT_BYTE (XBUFFER (w->contents)));
6205 fset_redisplay (f);
6207 /* Problem: Freeing all matrices and later allocating them again
6208 is a serious redisplay flickering problem. What we would
6209 really like to do is to free only those matrices not reused
6210 below. */
6211 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6212 ptrdiff_t nwindows = count_windows (root_window);
6213 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6214 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6216 /* Kludge Alert!
6217 Mark all windows now on frame as "deleted".
6218 Restoring the new configuration "undeletes" any that are in it.
6220 Save their current buffers in their height fields, since we may
6221 need it later, if a buffer saved in the configuration is now
6222 dead. */
6223 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6225 for (k = 0; k < saved_windows->header.size; k++)
6227 p = SAVED_WINDOW_N (saved_windows, k);
6228 window = p->window;
6229 w = XWINDOW (window);
6230 wset_next (w, Qnil);
6232 if (!NILP (p->parent))
6233 wset_parent
6234 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6235 else
6236 wset_parent (w, Qnil);
6238 if (!NILP (p->prev))
6240 wset_prev
6241 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6242 wset_next (XWINDOW (w->prev), p->window);
6244 else
6246 wset_prev (w, Qnil);
6247 if (!NILP (w->parent))
6248 wset_combination (XWINDOW (w->parent),
6249 (XINT (p->total_cols)
6250 != XWINDOW (w->parent)->total_cols),
6251 p->window);
6254 /* If we squirreled away the buffer, restore it now. */
6255 if (BUFFERP (w->combination_limit))
6256 wset_buffer (w, w->combination_limit);
6257 w->pixel_left = XFASTINT (p->pixel_left);
6258 w->pixel_top = XFASTINT (p->pixel_top);
6259 w->pixel_width = XFASTINT (p->pixel_width);
6260 w->pixel_height = XFASTINT (p->pixel_height);
6261 w->pixel_width_before_size_change
6262 = XFASTINT (p->pixel_width_before_size_change);
6263 w->pixel_height_before_size_change
6264 = XFASTINT (p->pixel_height_before_size_change);
6265 w->left_col = XFASTINT (p->left_col);
6266 w->top_line = XFASTINT (p->top_line);
6267 w->total_cols = XFASTINT (p->total_cols);
6268 w->total_lines = XFASTINT (p->total_lines);
6269 wset_normal_cols (w, p->normal_cols);
6270 wset_normal_lines (w, p->normal_lines);
6271 w->hscroll = XFASTINT (p->hscroll);
6272 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6273 w->min_hscroll = XFASTINT (p->min_hscroll);
6274 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6275 wset_display_table (w, p->display_table);
6276 w->left_margin_cols = XINT (p->left_margin_cols);
6277 w->right_margin_cols = XINT (p->right_margin_cols);
6278 w->left_fringe_width = XINT (p->left_fringe_width);
6279 w->right_fringe_width = XINT (p->right_fringe_width);
6280 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6281 w->scroll_bar_width = XINT (p->scroll_bar_width);
6282 w->scroll_bar_height = XINT (p->scroll_bar_height);
6283 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6284 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6285 wset_dedicated (w, p->dedicated);
6286 wset_combination_limit (w, p->combination_limit);
6287 /* Restore any window parameters that have been saved.
6288 Parameters that have not been saved are left alone. */
6289 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6291 pers = XCAR (tem);
6292 if (CONSP (pers))
6294 if (NILP (XCDR (pers)))
6296 par = Fassq (XCAR (pers), w->window_parameters);
6297 if (CONSP (par) && !NILP (XCDR (par)))
6298 /* Reset a parameter to nil if and only if it
6299 has a non-nil association. Don't make new
6300 associations. */
6301 Fsetcdr (par, Qnil);
6303 else
6304 /* Always restore a non-nil value. */
6305 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6309 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6310 /* If saved buffer is alive, install it. */
6312 if (!EQ (w->contents, p->buffer))
6313 /* Record buffer configuration change. */
6314 buffer_changed = true;
6315 wset_buffer (w, p->buffer);
6316 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6317 set_marker_restricted (w->start, p->start, w->contents);
6318 set_marker_restricted (w->pointm, p->pointm, w->contents);
6319 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6320 /* As documented in Fcurrent_window_configuration, don't
6321 restore the location of point in the buffer which was
6322 current when the window configuration was recorded. */
6323 if (!EQ (p->buffer, new_current_buffer)
6324 && XBUFFER (p->buffer) == current_buffer)
6325 Fgoto_char (w->pointm);
6327 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6328 /* Keep window's old buffer; make sure the markers are real. */
6330 /* Set window markers at start of visible range. */
6331 if (XMARKER (w->start)->buffer == 0)
6332 set_marker_restricted_both (w->start, w->contents, 0, 0);
6333 if (XMARKER (w->pointm)->buffer == 0)
6334 set_marker_restricted_both
6335 (w->pointm, w->contents,
6336 BUF_PT (XBUFFER (w->contents)),
6337 BUF_PT_BYTE (XBUFFER (w->contents)));
6338 if (XMARKER (w->old_pointm)->buffer == 0)
6339 set_marker_restricted_both
6340 (w->old_pointm, w->contents,
6341 BUF_PT (XBUFFER (w->contents)),
6342 BUF_PT_BYTE (XBUFFER (w->contents)));
6343 w->start_at_line_beg = true;
6345 else if (!NILP (w->start))
6346 /* Leaf window has no live buffer, get one. */
6348 /* Record buffer configuration change. */
6349 buffer_changed = true;
6350 /* Get the buffer via other_buffer_safely in order to
6351 avoid showing an unimportant buffer and, if necessary, to
6352 recreate *scratch* in the course (part of Juanma's bs-show
6353 scenario from March 2011). */
6354 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6355 /* This will set the markers to beginning of visible
6356 range. */
6357 set_marker_restricted_both (w->start, w->contents, 0, 0);
6358 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6359 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6360 w->start_at_line_beg = true;
6361 if (!NILP (w->dedicated))
6362 /* Record this window as dead. */
6363 dead_windows = Fcons (window, dead_windows);
6364 /* Make sure window is no more dedicated. */
6365 wset_dedicated (w, Qnil);
6369 fset_root_window (f, data->root_window);
6370 /* Arrange *not* to restore point in the buffer that was
6371 current when the window configuration was saved. */
6372 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6373 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6374 make_number (old_point),
6375 XWINDOW (data->current_window)->contents);
6377 /* In the following call to `select-window', prevent "swapping out
6378 point" in the old selected window using the buffer that has
6379 been restored into it. We already swapped out that point from
6380 that window's old buffer.
6382 Do not record the buffer here. We do that in a separate call
6383 to select_window below. See also Bug#16207. */
6384 select_window (data->current_window, Qt, true);
6385 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6386 last_selected_window)
6387 = selected_window;
6389 if (NILP (data->focus_frame)
6390 || (FRAMEP (data->focus_frame)
6391 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6392 Fredirect_frame_focus (frame, data->focus_frame);
6394 /* Now, free glyph matrices in windows that were not reused. */
6395 for (i = 0; i < n_leaf_windows; i++)
6396 if (NILP (leaf_windows[i]->contents))
6398 free_window_matrices (leaf_windows[i]);
6399 window_changed = true;
6402 /* Allow x_set_window_size again and apply frame size changes if
6403 needed. */
6404 f->can_x_set_window_size = true;
6405 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6407 adjust_frame_glyphs (f);
6408 unblock_input ();
6410 /* Scan dead buffer windows. */
6411 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6413 window = XCAR (dead_windows);
6414 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6415 delete_deletable_window (window);
6418 /* Record the selected window's buffer here. The window should
6419 already be the selected one from the call above. */
6420 if (WINDOW_LIVE_P (data->current_window))
6421 select_window (data->current_window, Qnil, false);
6423 /* Fselect_window will have made f the selected frame, so we
6424 reselect the proper frame here. Fhandle_switch_frame will change the
6425 selected window too, but that doesn't make the call to
6426 Fselect_window above totally superfluous; it still sets f's
6427 selected window. */
6428 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6429 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6431 if (window_changed)
6432 /* At least one window has been added or removed. Run
6433 `window-configuration-change-hook' and make sure
6434 `window-size-change-functions' get run later.
6436 We have to do this in order to capture the following
6437 scenario: Suppose our frame contains two live windows W1 and
6438 W2 and ‘set-window-configuration’ replaces them by two
6439 windows W3 and W4 that were dead the last time
6440 run_window_size_change_functions was run. If W3 and W4 have
6441 the same values for their old and new pixel sizes but these
6442 values differ from those of W1 and W2, the sizes of our
6443 frame's two live windows changed but window_size_changed has
6444 no means to detect that fact.
6446 Obviously, this will get us false positives, for example,
6447 when we restore the original configuration with W1 and W2
6448 before run_window_size_change_functions gets called. */
6450 run_window_configuration_change_hook (f);
6451 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = true;
6453 else if (buffer_changed)
6454 /* At least one window has changed its buffer. Run
6455 `window-configuration-change-hook' only. */
6456 run_window_configuration_change_hook (f);
6459 if (!NILP (new_current_buffer))
6461 Fset_buffer (new_current_buffer);
6462 /* If the new current buffer doesn't appear in the selected
6463 window, go to its old point (see bug#12208). */
6464 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6465 Fgoto_char (make_number (old_point));
6468 Vminibuf_scroll_window = data->minibuf_scroll_window;
6469 minibuf_selected_window = data->minibuf_selected_window;
6471 SAFE_FREE ();
6472 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6476 void
6477 restore_window_configuration (Lisp_Object configuration)
6479 Fset_window_configuration (configuration);
6483 /* If WINDOW is an internal window, recursively delete all child windows
6484 reachable via the next and contents slots of WINDOW. Otherwise setup
6485 WINDOW to not show any buffer. */
6487 void
6488 delete_all_child_windows (Lisp_Object window)
6490 register struct window *w;
6492 w = XWINDOW (window);
6494 if (!NILP (w->next))
6495 /* Delete WINDOW's siblings (we traverse postorderly). */
6496 delete_all_child_windows (w->next);
6498 if (WINDOWP (w->contents))
6500 delete_all_child_windows (w->contents);
6501 wset_combination (w, false, Qnil);
6503 else if (BUFFERP (w->contents))
6505 unshow_buffer (w);
6506 unchain_marker (XMARKER (w->pointm));
6507 unchain_marker (XMARKER (w->old_pointm));
6508 unchain_marker (XMARKER (w->start));
6509 /* Since combination limit makes sense for an internal windows
6510 only, we use this slot to save the buffer for the sake of
6511 possible resurrection in Fset_window_configuration. */
6512 wset_combination_limit (w, w->contents);
6513 wset_buffer (w, Qnil);
6516 Vwindow_list = Qnil;
6519 static ptrdiff_t
6520 count_windows (struct window *window)
6522 ptrdiff_t count = 1;
6523 if (!NILP (window->next))
6524 count += count_windows (XWINDOW (window->next));
6525 if (WINDOWP (window->contents))
6526 count += count_windows (XWINDOW (window->contents));
6527 return count;
6531 /* Fill vector FLAT with leaf windows under W, starting at index I.
6532 Value is last index + 1. */
6533 static ptrdiff_t
6534 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6536 while (w)
6538 if (WINDOWP (w->contents))
6539 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6540 else
6541 flat[i++] = w;
6543 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6546 return i;
6550 /* Return a pointer to the glyph W's physical cursor is on. Value is
6551 null if W's current matrix is invalid, so that no meaningful glyph
6552 can be returned. */
6553 struct glyph *
6554 get_phys_cursor_glyph (struct window *w)
6556 struct glyph_row *row;
6557 struct glyph *glyph;
6558 int hpos = w->phys_cursor.hpos;
6560 if (!(w->phys_cursor.vpos >= 0
6561 && w->phys_cursor.vpos < w->current_matrix->nrows))
6562 return NULL;
6564 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6565 if (!row->enabled_p)
6566 return NULL;
6568 if (w->hscroll)
6570 /* When the window is hscrolled, cursor hpos can legitimately be
6571 out of bounds, but we draw the cursor at the corresponding
6572 window margin in that case. */
6573 if (!row->reversed_p && hpos < 0)
6574 hpos = 0;
6575 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6576 hpos = row->used[TEXT_AREA] - 1;
6579 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6580 glyph = row->glyphs[TEXT_AREA] + hpos;
6581 else
6582 glyph = NULL;
6584 return glyph;
6588 static ptrdiff_t
6589 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6591 struct saved_window *p;
6592 struct window *w;
6593 Lisp_Object tem, pers, par;
6595 for (; !NILP (window); window = w->next)
6597 p = SAVED_WINDOW_N (vector, i);
6598 w = XWINDOW (window);
6600 wset_temslot (w, make_number (i)); i++;
6601 p->window = window;
6602 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6603 p->pixel_left = make_number (w->pixel_left);
6604 p->pixel_top = make_number (w->pixel_top);
6605 p->pixel_width = make_number (w->pixel_width);
6606 p->pixel_height = make_number (w->pixel_height);
6607 p->pixel_width_before_size_change
6608 = make_number (w->pixel_width_before_size_change);
6609 p->pixel_height_before_size_change
6610 = make_number (w->pixel_height_before_size_change);
6611 p->left_col = make_number (w->left_col);
6612 p->top_line = make_number (w->top_line);
6613 p->total_cols = make_number (w->total_cols);
6614 p->total_lines = make_number (w->total_lines);
6615 p->normal_cols = w->normal_cols;
6616 p->normal_lines = w->normal_lines;
6617 XSETFASTINT (p->hscroll, w->hscroll);
6618 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6619 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6620 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6621 p->display_table = w->display_table;
6622 p->left_margin_cols = make_number (w->left_margin_cols);
6623 p->right_margin_cols = make_number (w->right_margin_cols);
6624 p->left_fringe_width = make_number (w->left_fringe_width);
6625 p->right_fringe_width = make_number (w->right_fringe_width);
6626 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6627 p->scroll_bar_width = make_number (w->scroll_bar_width);
6628 p->scroll_bar_height = make_number (w->scroll_bar_height);
6629 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6630 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6631 p->dedicated = w->dedicated;
6632 p->combination_limit = w->combination_limit;
6633 p->window_parameters = Qnil;
6635 if (!NILP (Vwindow_persistent_parameters))
6637 /* Run cycle detection on Vwindow_persistent_parameters. */
6638 Lisp_Object tortoise, hare;
6640 hare = tortoise = Vwindow_persistent_parameters;
6641 while (CONSP (hare))
6643 hare = XCDR (hare);
6644 if (!CONSP (hare))
6645 break;
6647 hare = XCDR (hare);
6648 tortoise = XCDR (tortoise);
6650 if (EQ (hare, tortoise))
6651 /* Reset Vwindow_persistent_parameters to Qnil. */
6653 Vwindow_persistent_parameters = Qnil;
6654 break;
6658 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6659 tem = XCDR (tem))
6661 pers = XCAR (tem);
6662 /* Save values for persistent window parameters. */
6663 if (CONSP (pers) && !NILP (XCDR (pers)))
6665 par = Fassq (XCAR (pers), w->window_parameters);
6666 if (NILP (par))
6667 /* If the window has no value for the parameter,
6668 make one. */
6669 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6670 p->window_parameters);
6671 else
6672 /* If the window has a value for the parameter,
6673 save it. */
6674 p->window_parameters = Fcons (Fcons (XCAR (par),
6675 XCDR (par)),
6676 p->window_parameters);
6681 if (BUFFERP (w->contents))
6683 /* Save w's value of point in the window configuration. If w
6684 is the selected window, then get the value of point from
6685 the buffer; pointm is garbage in the selected window. */
6686 if (EQ (window, selected_window))
6687 p->pointm = build_marker (XBUFFER (w->contents),
6688 BUF_PT (XBUFFER (w->contents)),
6689 BUF_PT_BYTE (XBUFFER (w->contents)));
6690 else
6691 p->pointm = Fcopy_marker (w->pointm, Qnil);
6692 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6693 XMARKER (p->pointm)->insertion_type
6694 = !NILP (buffer_local_value /* Don't signal error if void. */
6695 (Qwindow_point_insertion_type, w->contents));
6696 XMARKER (p->old_pointm)->insertion_type
6697 = !NILP (buffer_local_value /* Don't signal error if void. */
6698 (Qwindow_point_insertion_type, w->contents));
6700 p->start = Fcopy_marker (w->start, Qnil);
6701 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6703 else
6705 p->pointm = Qnil;
6706 p->old_pointm = Qnil;
6707 p->start = Qnil;
6708 p->start_at_line_beg = Qnil;
6711 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6712 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6714 if (WINDOWP (w->contents))
6715 i = save_window_save (w->contents, vector, i);
6718 return i;
6721 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6722 Scurrent_window_configuration, 0, 1, 0,
6723 doc: /* Return an object representing the current window configuration of FRAME.
6724 If FRAME is nil or omitted, use the selected frame.
6725 This describes the number of windows, their sizes and current buffers,
6726 and for each displayed buffer, where display starts, and the position of
6727 point. An exception is made for point in the current buffer:
6728 its value is -not- saved.
6729 This also records the currently selected frame, and FRAME's focus
6730 redirection (see `redirect-frame-focus'). The variable
6731 `window-persistent-parameters' specifies which window parameters are
6732 saved by this function. */)
6733 (Lisp_Object frame)
6735 Lisp_Object tem;
6736 ptrdiff_t i, n_windows;
6737 struct save_window_data *data;
6738 struct frame *f = decode_live_frame (frame);
6740 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6741 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6742 PVEC_WINDOW_CONFIGURATION);
6744 data->frame_cols = FRAME_COLS (f);
6745 data->frame_lines = FRAME_LINES (f);
6746 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6747 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6748 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6749 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6750 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6751 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6752 data->selected_frame = selected_frame;
6753 data->current_window = FRAME_SELECTED_WINDOW (f);
6754 XSETBUFFER (data->f_current_buffer, current_buffer);
6755 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6756 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6757 data->root_window = FRAME_ROOT_WINDOW (f);
6758 data->focus_frame = FRAME_FOCUS_FRAME (f);
6759 tem = make_uninit_vector (n_windows);
6760 data->saved_windows = tem;
6761 for (i = 0; i < n_windows; i++)
6762 ASET (tem, i,
6763 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6764 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6765 XSETWINDOW_CONFIGURATION (tem, data);
6766 return (tem);
6769 /* Called after W's margins, fringes or scroll bars was adjusted. */
6771 static void
6772 apply_window_adjustment (struct window *w)
6774 eassert (w);
6775 clear_glyph_matrix (w->current_matrix);
6776 w->window_end_valid = false;
6777 wset_redisplay (w);
6778 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6782 /***********************************************************************
6783 Marginal Areas
6784 ***********************************************************************/
6786 static int
6787 extract_dimension (Lisp_Object dimension)
6789 if (NILP (dimension))
6790 return -1;
6791 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6792 return XINT (dimension);
6795 static struct window *
6796 set_window_margins (struct window *w, Lisp_Object left_width,
6797 Lisp_Object right_width)
6799 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6800 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6801 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6803 if (w->left_margin_cols != left || w->right_margin_cols != right)
6805 /* Don't change anything if new margins won't fit. */
6806 if ((WINDOW_PIXEL_WIDTH (w)
6807 - WINDOW_FRINGES_WIDTH (w)
6808 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6809 - (left + right) * unit)
6810 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6812 w->left_margin_cols = left;
6813 w->right_margin_cols = right;
6815 return w;
6817 else
6818 return NULL;
6820 else
6821 return NULL;
6824 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6825 2, 3, 0,
6826 doc: /* Set width of marginal areas of window WINDOW.
6827 WINDOW must be a live window and defaults to the selected one.
6829 Second arg LEFT-WIDTH specifies the number of character cells to
6830 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6831 does the same for the right marginal area. A nil width parameter
6832 means no margin.
6834 Return t if any margin was actually changed and nil otherwise. */)
6835 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6837 struct window *w = set_window_margins (decode_live_window (window),
6838 left_width, right_width);
6839 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6843 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6844 0, 1, 0,
6845 doc: /* Get width of marginal areas of window WINDOW.
6846 WINDOW must be a live window and defaults to the selected one.
6848 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6849 If a marginal area does not exist, its width will be returned
6850 as nil. */)
6851 (Lisp_Object window)
6853 struct window *w = decode_live_window (window);
6854 return Fcons (w->left_margin_cols
6855 ? make_number (w->left_margin_cols) : Qnil,
6856 w->right_margin_cols
6857 ? make_number (w->right_margin_cols) : Qnil);
6862 /***********************************************************************
6863 Fringes
6864 ***********************************************************************/
6866 static struct window *
6867 set_window_fringes (struct window *w, Lisp_Object left_width,
6868 Lisp_Object right_width, Lisp_Object outside_margins)
6870 bool outside = !NILP (outside_margins);
6871 int left = extract_dimension (left_width);
6872 int right = extract_dimension (right_width);
6874 /* Do nothing on a tty or if nothing to actually change. */
6875 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6876 && (w->left_fringe_width != left
6877 || w->right_fringe_width != right
6878 || w->fringes_outside_margins != outside))
6880 if (left > 0 || right > 0)
6882 /* Don't change anything if new fringes don't fit. */
6883 if ((WINDOW_PIXEL_WIDTH (w)
6884 - WINDOW_MARGINS_WIDTH (w)
6885 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6886 - max (left, 0) - max (right, 0))
6887 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6888 return NULL;
6891 w->left_fringe_width = left;
6892 w->right_fringe_width = right;
6893 w->fringes_outside_margins = outside;
6895 return w;
6897 else
6898 return NULL;
6901 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6902 2, 4, 0,
6903 doc: /* Set the fringe widths of window WINDOW.
6904 WINDOW must be a live window and defaults to the selected one.
6906 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6907 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6908 fringe width. If a fringe width arg is nil, that means to use the
6909 frame's default fringe width. Default fringe widths can be set with
6910 the command `set-fringe-style'.
6911 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6912 outside of the display margins. By default, fringes are drawn between
6913 display marginal areas and the text area.
6915 Return t if any fringe was actually changed and nil otherwise. */)
6916 (Lisp_Object window, Lisp_Object left_width,
6917 Lisp_Object right_width, Lisp_Object outside_margins)
6919 struct window *w
6920 = set_window_fringes (decode_live_window (window),
6921 left_width, right_width, outside_margins);
6922 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6926 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6927 0, 1, 0,
6928 doc: /* Get width of fringes of window WINDOW.
6929 WINDOW must be a live window and defaults to the selected one.
6931 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6932 (Lisp_Object window)
6934 struct window *w = decode_live_window (window);
6936 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6937 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6938 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6943 /***********************************************************************
6944 Scroll bars
6945 ***********************************************************************/
6947 static struct window *
6948 set_window_scroll_bars (struct window *w, Lisp_Object width,
6949 Lisp_Object vertical_type, Lisp_Object height,
6950 Lisp_Object horizontal_type)
6952 int iwidth = extract_dimension (width);
6953 bool changed = false;
6955 if (iwidth == 0)
6956 vertical_type = Qnil;
6958 if (!(NILP (vertical_type)
6959 || EQ (vertical_type, Qleft)
6960 || EQ (vertical_type, Qright)
6961 || EQ (vertical_type, Qt)))
6962 error ("Invalid type of vertical scroll bar");
6964 if (w->scroll_bar_width != iwidth
6965 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6967 /* Don't change anything if new scroll bar won't fit. */
6968 if ((WINDOW_PIXEL_WIDTH (w)
6969 - WINDOW_MARGINS_WIDTH (w)
6970 - WINDOW_FRINGES_WIDTH (w)
6971 - max (iwidth, 0))
6972 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6974 w->scroll_bar_width = iwidth;
6975 wset_vertical_scroll_bar_type (w, vertical_type);
6976 changed = true;
6980 #if USE_HORIZONTAL_SCROLL_BARS
6982 int iheight = extract_dimension (height);
6984 if (MINI_WINDOW_P (w) || iheight == 0)
6985 horizontal_type = Qnil;
6987 if (!(NILP (horizontal_type)
6988 || EQ (horizontal_type, Qbottom)
6989 || EQ (horizontal_type, Qt)))
6990 error ("Invalid type of horizontal scroll bar");
6992 if (w->scroll_bar_height != iheight
6993 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6995 /* Don't change anything if new scroll bar won't fit. */
6996 if ((WINDOW_PIXEL_HEIGHT (w)
6997 - WINDOW_HEADER_LINE_HEIGHT (w)
6998 - WINDOW_MODE_LINE_HEIGHT (w)
6999 - max (iheight, 0))
7000 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7002 w->scroll_bar_height = iheight;
7003 wset_horizontal_scroll_bar_type (w, horizontal_type);
7004 changed = true;
7008 #else
7009 wset_horizontal_scroll_bar_type (w, Qnil);
7010 #endif
7012 return changed ? w : NULL;
7015 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7016 Sset_window_scroll_bars, 1, 5, 0,
7017 doc: /* Set width and type of scroll bars of window WINDOW.
7018 WINDOW must be a live window and defaults to the selected one.
7020 Second parameter WIDTH specifies the pixel width for the vertical scroll
7021 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
7022 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7023 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
7024 frame's scroll-bar type.
7026 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7027 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
7028 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7029 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
7030 means to use the frame's horizontal scroll-bar type.
7032 Return t if scroll bars were actually changed and nil otherwise. */)
7033 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7034 Lisp_Object height, Lisp_Object horizontal_type)
7036 struct window *w
7037 = set_window_scroll_bars (decode_live_window (window),
7038 width, vertical_type, height, horizontal_type);
7039 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7043 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7044 0, 1, 0,
7045 doc: /* Get width and type of scroll bars of window WINDOW.
7046 WINDOW must be a live window and defaults to the selected one.
7048 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7049 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7050 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7051 value. */)
7052 (Lisp_Object window)
7054 struct window *w = decode_live_window (window);
7056 return Fcons (((w->scroll_bar_width >= 0)
7057 ? make_number (w->scroll_bar_width)
7058 : Qnil),
7059 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7060 w->vertical_scroll_bar_type,
7061 ((w->scroll_bar_height >= 0)
7062 ? make_number (w->scroll_bar_height)
7063 : Qnil),
7064 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7065 w->horizontal_scroll_bar_type));
7068 /***********************************************************************
7069 Smooth scrolling
7070 ***********************************************************************/
7072 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7073 doc: /* Return the amount by which WINDOW is scrolled vertically.
7074 If WINDOW is omitted or nil, it defaults to the selected window.
7075 Normally, value is a multiple of the canonical character height of WINDOW;
7076 optional second arg PIXELS-P means value is measured in pixels. */)
7077 (Lisp_Object window, Lisp_Object pixels_p)
7079 Lisp_Object result;
7080 struct window *w = decode_live_window (window);
7081 struct frame *f = XFRAME (w->frame);
7083 if (FRAME_WINDOW_P (f))
7084 result = (NILP (pixels_p)
7085 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7086 : make_number (-w->vscroll));
7087 else
7088 result = make_number (0);
7089 return result;
7093 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7094 2, 3, 0,
7095 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7096 WINDOW nil means use the selected window. Normally, VSCROLL is a
7097 non-negative multiple of the canonical character height of WINDOW;
7098 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7099 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7100 corresponds to an integral number of pixels. The return value is the
7101 result of this rounding.
7102 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7103 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7105 struct window *w = decode_live_window (window);
7106 struct frame *f = XFRAME (w->frame);
7108 CHECK_NUMBER_OR_FLOAT (vscroll);
7110 if (FRAME_WINDOW_P (f))
7112 int old_dy = w->vscroll;
7114 w->vscroll = - (NILP (pixels_p)
7115 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7116 : XFLOATINT (vscroll));
7117 w->vscroll = min (w->vscroll, 0);
7119 if (w->vscroll != old_dy)
7121 /* Adjust glyph matrix of the frame if the virtual display
7122 area becomes larger than before. */
7123 if (w->vscroll < 0 && w->vscroll < old_dy)
7124 adjust_frame_glyphs (f);
7126 /* Prevent redisplay shortcuts. */
7127 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7131 return Fwindow_vscroll (window, pixels_p);
7135 /* Call FN for all leaf windows on frame F. FN is called with the
7136 first argument being a pointer to the leaf window, and with
7137 additional argument USER_DATA. Stops when FN returns 0. */
7139 static void
7140 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7141 void *user_data)
7143 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7144 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7145 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7149 /* Helper function for foreach_window. Call FN for all leaf windows
7150 reachable from W. FN is called with the first argument being a
7151 pointer to the leaf window, and with additional argument USER_DATA.
7152 Stop when FN returns false. Value is false if stopped by FN. */
7154 static bool
7155 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7156 void *user_data)
7158 bool cont;
7160 for (cont = true; w && cont;)
7162 if (WINDOWP (w->contents))
7163 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7164 else
7165 cont = fn (w, user_data);
7167 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7170 return cont;
7173 /***********************************************************************
7174 Initialization
7175 ***********************************************************************/
7177 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7178 describe the same state of affairs. This is used by Fequal.
7180 IGNORE_POSITIONS means ignore non-matching scroll positions
7181 and the like.
7183 This ignores a couple of things like the dedication status of
7184 window, combination_limit and the like. This might have to be
7185 fixed. */
7187 bool
7188 compare_window_configurations (Lisp_Object configuration1,
7189 Lisp_Object configuration2,
7190 bool ignore_positions)
7192 register struct save_window_data *d1, *d2;
7193 struct Lisp_Vector *sws1, *sws2;
7194 ptrdiff_t i;
7196 CHECK_WINDOW_CONFIGURATION (configuration1);
7197 CHECK_WINDOW_CONFIGURATION (configuration2);
7199 d1 = (struct save_window_data *) XVECTOR (configuration1);
7200 d2 = (struct save_window_data *) XVECTOR (configuration2);
7201 sws1 = XVECTOR (d1->saved_windows);
7202 sws2 = XVECTOR (d2->saved_windows);
7204 /* Frame settings must match. */
7205 if (d1->frame_cols != d2->frame_cols
7206 || d1->frame_lines != d2->frame_lines
7207 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7208 || !EQ (d1->selected_frame, d2->selected_frame)
7209 || !EQ (d1->f_current_buffer, d2->f_current_buffer)
7210 || (!ignore_positions
7211 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7212 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7213 || !EQ (d1->focus_frame, d2->focus_frame)
7214 /* Verify that the two configurations have the same number of windows. */
7215 || sws1->header.size != sws2->header.size)
7216 return false;
7218 for (i = 0; i < sws1->header.size; i++)
7220 struct saved_window *sw1, *sw2;
7222 sw1 = SAVED_WINDOW_N (sws1, i);
7223 sw2 = SAVED_WINDOW_N (sws2, i);
7225 if (
7226 /* The "current" windows in the two configurations must
7227 correspond to each other. */
7228 EQ (d1->current_window, sw1->window)
7229 != EQ (d2->current_window, sw2->window)
7230 /* Windows' buffers must match. */
7231 || !EQ (sw1->buffer, sw2->buffer)
7232 || !EQ (sw1->pixel_left, sw2->pixel_left)
7233 || !EQ (sw1->pixel_top, sw2->pixel_top)
7234 || !EQ (sw1->pixel_height, sw2->pixel_height)
7235 || !EQ (sw1->pixel_width, sw2->pixel_width)
7236 || !EQ (sw1->left_col, sw2->left_col)
7237 || !EQ (sw1->top_line, sw2->top_line)
7238 || !EQ (sw1->total_cols, sw2->total_cols)
7239 || !EQ (sw1->total_lines, sw2->total_lines)
7240 || !EQ (sw1->display_table, sw2->display_table)
7241 /* The next two disjuncts check the window structure for
7242 equality. */
7243 || !EQ (sw1->parent, sw2->parent)
7244 || !EQ (sw1->prev, sw2->prev)
7245 || (!ignore_positions
7246 && (!EQ (sw1->hscroll, sw2->hscroll)
7247 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7248 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7249 || NILP (Fequal (sw1->start, sw2->start))
7250 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7251 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7252 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7253 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7254 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7255 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7256 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7257 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7258 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7259 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7260 return false;
7263 return true;
7266 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7267 Scompare_window_configurations, 2, 2, 0,
7268 doc: /* Compare two window configurations as regards the structure of windows.
7269 This function ignores details such as the values of point
7270 and scrolling positions. */)
7271 (Lisp_Object x, Lisp_Object y)
7273 if (compare_window_configurations (x, y, true))
7274 return Qt;
7275 return Qnil;
7278 void
7279 init_window_once (void)
7281 struct frame *f = make_initial_frame ();
7282 XSETFRAME (selected_frame, f);
7283 Vterminal_frame = selected_frame;
7284 minibuf_window = f->minibuffer_window;
7285 selected_window = f->selected_window;
7288 void
7289 init_window (void)
7291 Vwindow_list = Qnil;
7294 void
7295 syms_of_window (void)
7297 DEFSYM (Qscroll_up, "scroll-up");
7298 DEFSYM (Qscroll_down, "scroll-down");
7299 DEFSYM (Qscroll_command, "scroll-command");
7301 Fput (Qscroll_up, Qscroll_command, Qt);
7302 Fput (Qscroll_down, Qscroll_command, Qt);
7304 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7305 DEFSYM (Qwindowp, "windowp");
7306 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7307 DEFSYM (Qwindow_live_p, "window-live-p");
7308 DEFSYM (Qwindow_valid_p, "window-valid-p");
7309 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7310 DEFSYM (Qdelete_window, "delete-window");
7311 DEFSYM (Qwindow__resize_root_window, "window--resize-root-window");
7312 DEFSYM (Qwindow__resize_root_window_vertically,
7313 "window--resize-root-window-vertically");
7314 DEFSYM (Qwindow__sanitize_window_sizes, "window--sanitize-window-sizes");
7315 DEFSYM (Qwindow__pixel_to_total, "window--pixel-to-total");
7316 DEFSYM (Qsafe, "safe");
7317 DEFSYM (Qdisplay_buffer, "display-buffer");
7318 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7319 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7320 DEFSYM (Qget_mru_window, "get-mru-window");
7321 DEFSYM (Qwindow_size, "window-size");
7322 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7323 DEFSYM (Qabove, "above");
7324 DEFSYM (Qclone_of, "clone-of");
7325 DEFSYM (Qfloor, "floor");
7326 DEFSYM (Qceiling, "ceiling");
7328 staticpro (&Vwindow_list);
7330 minibuf_selected_window = Qnil;
7331 staticpro (&minibuf_selected_window);
7333 window_scroll_pixel_based_preserve_x = -1;
7334 window_scroll_pixel_based_preserve_y = -1;
7335 window_scroll_preserve_hpos = -1;
7336 window_scroll_preserve_vpos = -1;
7338 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7339 doc: /* Non-nil means call as function to display a help buffer.
7340 The function is called with one argument, the buffer to be displayed.
7341 Used by `with-output-to-temp-buffer'.
7342 If this function is used, then it must do the entire job of showing
7343 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7344 Vtemp_buffer_show_function = Qnil;
7346 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7347 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7348 Vminibuf_scroll_window = Qnil;
7350 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7351 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7352 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7353 is displayed in the `mode-line' face. */);
7354 mode_line_in_non_selected_windows = true;
7356 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7357 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7358 Vother_window_scroll_buffer = Qnil;
7360 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7361 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7362 auto_window_vscroll_p = true;
7364 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7365 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7366 next_screen_context_lines = 2;
7368 DEFVAR_LISP ("scroll-preserve-screen-position",
7369 Vscroll_preserve_screen_position,
7370 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7371 A value of nil means point does not keep its screen position except
7372 at the scroll margin or window boundary respectively.
7373 A value of t means point keeps its screen position if the scroll
7374 command moved it vertically out of the window, e.g. when scrolling
7375 by full screens.
7376 Any other value means point always keeps its screen position.
7377 Scroll commands should have the `scroll-command' property
7378 on their symbols to be controlled by this variable. */);
7379 Vscroll_preserve_screen_position = Qnil;
7381 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7382 doc: /* Type of marker to use for `window-point'. */);
7383 Vwindow_point_insertion_type = Qnil;
7384 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7386 DEFVAR_LISP ("window-configuration-change-hook",
7387 Vwindow_configuration_change_hook,
7388 doc: /* Functions to call when window configuration changes.
7389 The buffer-local part is run once per window, with the relevant window
7390 selected; while the global part is run only once for the modified frame,
7391 with the relevant frame selected. */);
7392 Vwindow_configuration_change_hook = Qnil;
7394 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
7395 doc: /* Functions called during redisplay, if window sizes have changed.
7396 The value should be a list of functions that take one argument.
7397 During the first part of redisplay, for each frame, if any of its windows
7398 have changed size since the last redisplay, or have been split or deleted,
7399 all the functions in the list are called, with the frame as argument.
7400 If redisplay decides to resize the minibuffer window, it calls these
7401 functions on behalf of that as well. */);
7402 Vwindow_size_change_functions = Qnil;
7404 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7405 doc: /* Non-nil means `recenter' redraws entire frame.
7406 If this option is non-nil, then the `recenter' command with a nil
7407 argument will redraw the entire frame; the special value `tty' causes
7408 the frame to be redrawn only if it is a tty frame. */);
7409 Vrecenter_redisplay = Qtty;
7411 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7412 doc: /* If t, resize window combinations proportionally.
7413 If this variable is nil, splitting a window gets the entire screen space
7414 for displaying the new window from the window to split. Deleting and
7415 resizing a window preferably resizes one adjacent window only.
7417 If this variable is t, splitting a window tries to get the space
7418 proportionally from all windows in the same combination. This also
7419 allows splitting a window that is otherwise too small or of fixed size.
7420 Resizing and deleting a window proportionally resize all windows in the
7421 same combination.
7423 Other values are reserved for future use.
7425 A specific split operation may ignore the value of this variable if it
7426 is affected by a non-nil value of `window-combination-limit'. */);
7427 Vwindow_combination_resize = Qnil;
7429 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7430 doc: /* If non-nil, splitting a window makes a new parent window.
7431 The following values are recognized:
7433 nil means splitting a window will create a new parent window only if the
7434 window has no parent window or the window shall become part of a
7435 combination orthogonal to the one it is part of.
7437 `window-size' means that splitting a window for displaying a buffer
7438 makes a new parent window provided `display-buffer' is supposed to
7439 explicitly set the window's size due to the presence of a
7440 `window-height' or `window-width' entry in the alist used by
7441 `display-buffer'. Otherwise, this value is handled like nil.
7443 `temp-buffer' means that splitting a window for displaying a temporary
7444 buffer always makes a new parent window. Otherwise, this value is
7445 handled like nil.
7447 `display-buffer' means that splitting a window for displaying a buffer
7448 always makes a new parent window. Since temporary buffers are
7449 displayed by the function `display-buffer', this value is stronger
7450 than `temp-buffer'. Splitting a window for other purpose makes a
7451 new parent window only if needed.
7453 t means that splitting a window always creates a new parent window. If
7454 all splits behave this way, each frame's window tree is a binary
7455 tree and every window but the frame's root window has exactly one
7456 sibling.
7458 Other values are reserved for future use. */);
7459 Vwindow_combination_limit = Qwindow_size;
7461 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7462 doc: /* Alist of persistent window parameters.
7463 This alist specifies which window parameters shall get saved by
7464 `current-window-configuration' and `window-state-get' and subsequently
7465 restored to their previous values by `set-window-configuration' and
7466 `window-state-put'.
7468 The car of each entry of this alist is the symbol specifying the
7469 parameter. The cdr is one of the following:
7471 nil means the parameter is neither saved by `window-state-get' nor by
7472 `current-window-configuration'.
7474 t means the parameter is saved by `current-window-configuration' and,
7475 provided its WRITABLE argument is nil, by `window-state-get'.
7477 The symbol `writable' means the parameter is saved unconditionally by
7478 both `current-window-configuration' and `window-state-get'. Do not use
7479 this value for parameters without read syntax (like windows or frames).
7481 Parameters not saved by `current-window-configuration' or
7482 `window-state-get' are left alone by `set-window-configuration'
7483 respectively are not installed by `window-state-put'. */);
7484 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7486 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7487 doc: /* Non-nil means resize windows pixelwise.
7488 This currently affects the functions: `split-window', `maximize-window',
7489 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7490 all functions that symmetrically resize a parent window.
7492 Note that when a frame's pixel size is not a multiple of the
7493 frame's character size, at least one window may get resized
7494 pixelwise even if this option is nil. */);
7495 window_resize_pixelwise = false;
7497 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7498 Vfast_but_imprecise_scrolling,
7499 doc: /* When non-nil, accelerate scrolling operations.
7500 This comes into play when scrolling rapidly over previously
7501 unfontified buffer regions. Only those portions of the buffer which
7502 are actually going to be displayed get fontified.
7504 Note that this optimization can cause the portion of the buffer
7505 displayed after a scrolling operation to be somewhat inaccurate. */);
7506 Vfast_but_imprecise_scrolling = false;
7508 defsubr (&Sselected_window);
7509 defsubr (&Sminibuffer_window);
7510 defsubr (&Swindow_minibuffer_p);
7511 defsubr (&Swindowp);
7512 defsubr (&Swindow_valid_p);
7513 defsubr (&Swindow_live_p);
7514 defsubr (&Swindow_frame);
7515 defsubr (&Sframe_root_window);
7516 defsubr (&Sframe_first_window);
7517 defsubr (&Sframe_selected_window);
7518 defsubr (&Sset_frame_selected_window);
7519 defsubr (&Spos_visible_in_window_p);
7520 defsubr (&Swindow_line_height);
7521 defsubr (&Swindow_buffer);
7522 defsubr (&Swindow_parent);
7523 defsubr (&Swindow_top_child);
7524 defsubr (&Swindow_left_child);
7525 defsubr (&Swindow_next_sibling);
7526 defsubr (&Swindow_prev_sibling);
7527 defsubr (&Swindow_combination_limit);
7528 defsubr (&Sset_window_combination_limit);
7529 defsubr (&Swindow_use_time);
7530 defsubr (&Swindow_pixel_width);
7531 defsubr (&Swindow_pixel_height);
7532 defsubr (&Swindow_pixel_width_before_size_change);
7533 defsubr (&Swindow_pixel_height_before_size_change);
7534 defsubr (&Swindow_total_width);
7535 defsubr (&Swindow_total_height);
7536 defsubr (&Swindow_normal_size);
7537 defsubr (&Swindow_new_pixel);
7538 defsubr (&Swindow_new_total);
7539 defsubr (&Swindow_new_normal);
7540 defsubr (&Swindow_pixel_left);
7541 defsubr (&Swindow_pixel_top);
7542 defsubr (&Swindow_left_column);
7543 defsubr (&Swindow_top_line);
7544 defsubr (&Sset_window_new_pixel);
7545 defsubr (&Sset_window_new_total);
7546 defsubr (&Sset_window_new_normal);
7547 defsubr (&Swindow_resize_apply);
7548 defsubr (&Swindow_resize_apply_total);
7549 defsubr (&Swindow_body_height);
7550 defsubr (&Swindow_body_width);
7551 defsubr (&Swindow_hscroll);
7552 defsubr (&Sset_window_hscroll);
7553 defsubr (&Swindow_redisplay_end_trigger);
7554 defsubr (&Sset_window_redisplay_end_trigger);
7555 defsubr (&Swindow_mode_line_height);
7556 defsubr (&Swindow_header_line_height);
7557 defsubr (&Swindow_right_divider_width);
7558 defsubr (&Swindow_bottom_divider_width);
7559 defsubr (&Swindow_scroll_bar_width);
7560 defsubr (&Swindow_scroll_bar_height);
7561 defsubr (&Scoordinates_in_window_p);
7562 defsubr (&Swindow_at);
7563 defsubr (&Swindow_point);
7564 defsubr (&Swindow_old_point);
7565 defsubr (&Swindow_start);
7566 defsubr (&Swindow_end);
7567 defsubr (&Sset_window_point);
7568 defsubr (&Sset_window_start);
7569 defsubr (&Swindow_dedicated_p);
7570 defsubr (&Sset_window_dedicated_p);
7571 defsubr (&Swindow_display_table);
7572 defsubr (&Sset_window_display_table);
7573 defsubr (&Snext_window);
7574 defsubr (&Sprevious_window);
7575 defsubr (&Sget_buffer_window);
7576 defsubr (&Sdelete_other_windows_internal);
7577 defsubr (&Sdelete_window_internal);
7578 defsubr (&Sresize_mini_window_internal);
7579 defsubr (&Sset_window_buffer);
7580 defsubr (&Srun_window_configuration_change_hook);
7581 defsubr (&Srun_window_scroll_functions);
7582 defsubr (&Sselect_window);
7583 defsubr (&Sforce_window_update);
7584 defsubr (&Ssplit_window_internal);
7585 defsubr (&Sscroll_up);
7586 defsubr (&Sscroll_down);
7587 defsubr (&Sscroll_left);
7588 defsubr (&Sscroll_right);
7589 defsubr (&Sother_window_for_scrolling);
7590 defsubr (&Sscroll_other_window);
7591 defsubr (&Sminibuffer_selected_window);
7592 defsubr (&Srecenter);
7593 defsubr (&Swindow_text_width);
7594 defsubr (&Swindow_text_height);
7595 defsubr (&Smove_to_window_line);
7596 defsubr (&Swindow_configuration_p);
7597 defsubr (&Swindow_configuration_frame);
7598 defsubr (&Sset_window_configuration);
7599 defsubr (&Scurrent_window_configuration);
7600 defsubr (&Sset_window_margins);
7601 defsubr (&Swindow_margins);
7602 defsubr (&Sset_window_fringes);
7603 defsubr (&Swindow_fringes);
7604 defsubr (&Sset_window_scroll_bars);
7605 defsubr (&Swindow_scroll_bars);
7606 defsubr (&Swindow_vscroll);
7607 defsubr (&Sset_window_vscroll);
7608 defsubr (&Scompare_window_configurations);
7609 defsubr (&Swindow_list);
7610 defsubr (&Swindow_list_1);
7611 defsubr (&Swindow_prev_buffers);
7612 defsubr (&Sset_window_prev_buffers);
7613 defsubr (&Swindow_next_buffers);
7614 defsubr (&Sset_window_next_buffers);
7615 defsubr (&Swindow_parameters);
7616 defsubr (&Swindow_parameter);
7617 defsubr (&Sset_window_parameter);
7620 void
7621 keys_of_window (void)
7623 initial_define_key (control_x_map, '<', "scroll-left");
7624 initial_define_key (control_x_map, '>', "scroll-right");
7626 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7627 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7628 initial_define_key (meta_map, 'v', "scroll-down-command");