Fix autorevert-tests on MS-Windows
[emacs.git] / src / window.c
blob95690443f8e1a84e79ebe2d818df54c098880328
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 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4775 wset_redisplay (XWINDOW (window));
4777 if (whole && Vfast_but_imprecise_scrolling)
4778 specbind (Qfontification_functions, Qnil);
4780 /* If we must, use the pixel-based version which is much slower than
4781 the line-based one but can handle varying line heights. */
4782 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4783 window_scroll_pixel_based (window, n, whole, noerror);
4784 else
4785 window_scroll_line_based (window, n, whole, noerror);
4787 unbind_to (count, Qnil);
4789 /* Bug#15957. */
4790 XWINDOW (window)->window_end_valid = false;
4793 /* Compute scroll margin for WINDOW.
4794 We scroll when point is within this distance from the top or bottom
4795 of the window. The result is measured in lines or in pixels
4796 depending on the second parameter. */
4798 window_scroll_margin (struct window *window, enum margin_unit unit)
4800 if (scroll_margin > 0)
4802 int frame_line_height = default_line_pixel_height (window);
4803 int window_lines = window_box_height (window) / frame_line_height;
4805 double ratio = 0.25;
4806 if (FLOATP (Vmaximum_scroll_margin))
4808 ratio = XFLOAT_DATA (Vmaximum_scroll_margin);
4809 ratio = max (0.0, ratio);
4810 ratio = min (ratio, 0.5);
4812 int max_margin = min ((window_lines - 1)/2,
4813 (int) (window_lines * ratio));
4814 int margin = clip_to_bounds (0, scroll_margin, max_margin);
4815 return (unit == MARGIN_IN_PIXELS)
4816 ? margin * frame_line_height
4817 : margin;
4819 else
4820 return 0;
4824 /* Implementation of window_scroll that works based on pixel line
4825 heights. See the comment of window_scroll for parameter
4826 descriptions. */
4828 static void
4829 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4831 struct it it;
4832 struct window *w = XWINDOW (window);
4833 struct text_pos start;
4834 int this_scroll_margin;
4835 /* True if we fiddled the window vscroll field without really scrolling. */
4836 bool vscrolled = false;
4837 int x, y, rtop, rbot, rowh, vpos;
4838 void *itdata = NULL;
4839 int frame_line_height = default_line_pixel_height (w);
4840 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4841 Fwindow_old_point (window)));
4843 SET_TEXT_POS_FROM_MARKER (start, w->start);
4844 /* Scrolling a minibuffer window via scroll bar when the echo area
4845 shows long text sometimes resets the minibuffer contents behind
4846 our backs. Also, someone might narrow-to-region and immediately
4847 call a scroll function. */
4848 if (CHARPOS (start) > ZV || CHARPOS (start) < BEGV)
4849 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4851 /* If PT is not visible in WINDOW, move back one half of
4852 the screen. Allow PT to be partially visible, otherwise
4853 something like (scroll-down 1) with PT in the line before
4854 the partially visible one would recenter. */
4856 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4858 itdata = bidi_shelve_cache ();
4859 /* Move backward half the height of the window. Performance note:
4860 vmotion used here is about 10% faster, but would give wrong
4861 results for variable height lines. */
4862 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4863 it.current_y = it.last_visible_y;
4864 move_it_vertically_backward (&it, window_box_height (w) / 2);
4866 /* The function move_iterator_vertically may move over more than
4867 the specified y-distance. If it->w is small, e.g. a
4868 mini-buffer window, we may end up in front of the window's
4869 display area. Start displaying at the start of the line
4870 containing PT in this case. */
4871 if (it.current_y <= 0)
4873 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4874 move_it_vertically_backward (&it, 0);
4875 it.current_y = 0;
4878 start = it.current.pos;
4879 bidi_unshelve_cache (itdata, false);
4881 else if (auto_window_vscroll_p)
4883 if (rtop || rbot) /* Partially visible. */
4885 int px;
4886 int dy = frame_line_height;
4887 /* In the below we divide the window box height by the
4888 frame's line height to make the result predictable when
4889 the window box is not an integral multiple of the line
4890 height. This is important to ensure we get back to the
4891 same position when scrolling up, then down. */
4892 if (whole)
4893 dy = max ((window_box_height (w) / dy
4894 - next_screen_context_lines) * dy,
4895 dy);
4896 dy *= n;
4898 if (n < 0)
4900 /* Only vscroll backwards if already vscrolled forwards. */
4901 if (w->vscroll < 0 && rtop > 0)
4903 px = max (0, -w->vscroll - min (rtop, -dy));
4904 Fset_window_vscroll (window, make_number (px), Qt);
4905 return;
4908 if (n > 0)
4910 /* Do vscroll if already vscrolled or only display line. */
4911 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4913 px = max (0, -w->vscroll + min (rbot, dy));
4914 Fset_window_vscroll (window, make_number (px), Qt);
4915 return;
4918 /* Maybe modify window start instead of scrolling. */
4919 if (rbot > 0 || w->vscroll < 0)
4921 ptrdiff_t spos;
4923 Fset_window_vscroll (window, make_number (0), Qt);
4924 /* If there are other text lines above the current row,
4925 move window start to current row. Else to next row. */
4926 if (rbot > 0)
4927 spos = XINT (Fline_beginning_position (Qnil));
4928 else
4929 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4930 set_marker_restricted (w->start, make_number (spos),
4931 w->contents);
4932 w->start_at_line_beg = true;
4933 wset_update_mode_line (w);
4934 /* Set force_start so that redisplay_window will run the
4935 window-scroll-functions. */
4936 w->force_start = true;
4937 return;
4941 /* Cancel previous vscroll. */
4942 Fset_window_vscroll (window, make_number (0), Qt);
4945 itdata = bidi_shelve_cache ();
4946 /* If scroll_preserve_screen_position is non-nil, we try to set
4947 point in the same window line as it is now, so get that line. */
4948 if (!NILP (Vscroll_preserve_screen_position))
4950 /* We preserve the goal pixel coordinate across consecutive
4951 calls to scroll-up, scroll-down and other commands that
4952 have the `scroll-command' property. This avoids the
4953 possibility of point becoming "stuck" on a tall line when
4954 scrolling by one line. */
4955 if (window_scroll_pixel_based_preserve_y < 0
4956 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4957 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4959 start_display (&it, w, start);
4960 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4961 window_scroll_pixel_based_preserve_y = it.current_y;
4962 window_scroll_pixel_based_preserve_x = it.current_x;
4965 else
4966 window_scroll_pixel_based_preserve_y
4967 = window_scroll_pixel_based_preserve_x = -1;
4969 /* Move iterator it from start the specified distance forward or
4970 backward. The result is the new window start. */
4971 start_display (&it, w, start);
4972 if (whole)
4974 ptrdiff_t start_pos = IT_CHARPOS (it);
4975 int dy = frame_line_height;
4976 /* In the below we divide the window box height by the frame's
4977 line height to make the result predictable when the window
4978 box is not an integral multiple of the line height. This is
4979 important to ensure we get back to the same position when
4980 scrolling up, then down. */
4981 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4982 dy) * n;
4984 /* Note that move_it_vertically always moves the iterator to the
4985 start of a line. So, if the last line doesn't have a newline,
4986 we would end up at the start of the line ending at ZV. */
4987 if (dy <= 0)
4989 move_it_vertically_backward (&it, -dy);
4990 /* Ensure we actually do move, e.g. in case we are currently
4991 looking at an image that is taller that the window height. */
4992 while (start_pos == IT_CHARPOS (it)
4993 && start_pos > BEGV)
4994 move_it_by_lines (&it, -1);
4996 else if (dy > 0)
4998 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4999 MOVE_TO_POS | MOVE_TO_Y);
5000 /* Ensure we actually do move, e.g. in case we are currently
5001 looking at an image that is taller that the window height. */
5002 while (start_pos == IT_CHARPOS (it)
5003 && start_pos < ZV)
5004 move_it_by_lines (&it, 1);
5007 else
5008 move_it_by_lines (&it, n);
5010 /* We failed if we find ZV is already on the screen (scrolling up,
5011 means there's nothing past the end), or if we can't start any
5012 earlier (scrolling down, means there's nothing past the top). */
5013 if ((n > 0 && IT_CHARPOS (it) == ZV)
5014 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5016 if (IT_CHARPOS (it) == ZV)
5018 if (it.current_y < it.last_visible_y
5019 && (it.current_y + it.max_ascent + it.max_descent
5020 > it.last_visible_y))
5022 /* The last line was only partially visible, make it fully
5023 visible. */
5024 w->vscroll = (it.last_visible_y
5025 - it.current_y + it.max_ascent + it.max_descent);
5026 adjust_frame_glyphs (it.f);
5028 else
5030 bidi_unshelve_cache (itdata, false);
5031 if (noerror)
5032 return;
5033 else if (n < 0) /* could happen with empty buffers */
5034 xsignal0 (Qbeginning_of_buffer);
5035 else
5036 xsignal0 (Qend_of_buffer);
5039 else
5041 if (w->vscroll != 0)
5042 /* The first line was only partially visible, make it fully
5043 visible. */
5044 w->vscroll = 0;
5045 else
5047 bidi_unshelve_cache (itdata, false);
5048 if (noerror)
5049 return;
5050 else
5051 xsignal0 (Qbeginning_of_buffer);
5055 /* If control gets here, then we vscrolled. */
5057 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5059 /* Don't try to change the window start below. */
5060 vscrolled = true;
5063 if (! vscrolled)
5065 ptrdiff_t pos = IT_CHARPOS (it);
5066 ptrdiff_t bytepos;
5068 /* If in the middle of a multi-glyph character move forward to
5069 the next character. */
5070 if (in_display_vector_p (&it))
5072 ++pos;
5073 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5076 /* Set the window start, and set up the window for redisplay. */
5077 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5078 IT_BYTEPOS (it));
5079 bytepos = marker_byte_position (w->start);
5080 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5081 wset_update_mode_line (w);
5082 /* Set force_start so that redisplay_window will run the
5083 window-scroll-functions. */
5084 w->force_start = true;
5087 /* The rest of this function uses current_y in a nonstandard way,
5088 not including the height of the header line if any. */
5089 it.current_y = it.vpos = 0;
5091 /* Move PT out of scroll margins.
5092 This code wants current_y to be zero at the window start position
5093 even if there is a header line. */
5094 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
5096 if (n > 0)
5098 int last_y = it.last_visible_y - this_scroll_margin - 1;
5100 /* We moved the window start towards ZV, so PT may be now
5101 in the scroll margin at the top. */
5102 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5103 if (IT_CHARPOS (it) == PT
5104 && it.current_y >= this_scroll_margin
5105 && it.current_y <= last_y - WINDOW_HEADER_LINE_HEIGHT (w)
5106 && (NILP (Vscroll_preserve_screen_position)
5107 || EQ (Vscroll_preserve_screen_position, Qt)))
5108 /* We found PT at a legitimate height. Leave it alone. */
5110 else
5112 if (window_scroll_pixel_based_preserve_y >= 0)
5114 /* Don't enter the scroll margin at the end of the window. */
5115 int goal_y = min (last_y, window_scroll_pixel_based_preserve_y);
5117 /* If we have a header line, take account of it. This
5118 is necessary because we set it.current_y to 0, above. */
5119 move_it_to (&it, -1,
5120 window_scroll_pixel_based_preserve_x,
5121 goal_y - WINDOW_HEADER_LINE_HEIGHT (w),
5122 -1, MOVE_TO_Y | MOVE_TO_X);
5125 /* Get out of the scroll margin at the top of the window. */
5126 while (it.current_y < this_scroll_margin)
5128 int prev = it.current_y;
5129 move_it_by_lines (&it, 1);
5130 if (prev == it.current_y)
5131 break;
5133 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5136 else if (n < 0)
5138 ptrdiff_t charpos, bytepos;
5139 bool partial_p;
5141 /* Save our position, for the
5142 window_scroll_pixel_based_preserve_y case. */
5143 charpos = IT_CHARPOS (it);
5144 bytepos = IT_BYTEPOS (it);
5146 /* We moved the window start towards BEGV, so PT may be now
5147 in the scroll margin at the bottom. */
5148 move_it_to (&it, PT, -1,
5149 (it.last_visible_y - WINDOW_HEADER_LINE_HEIGHT (w)
5150 - partial_line_height (&it) - this_scroll_margin - 1),
5152 MOVE_TO_POS | MOVE_TO_Y);
5154 /* Save our position, in case it's correct. */
5155 charpos = IT_CHARPOS (it);
5156 bytepos = IT_BYTEPOS (it);
5158 /* If PT is in the screen line at the last fully visible line,
5159 move_it_to will stop at X = 0 in that line, because the
5160 required Y coordinate is reached there. See if we can get to
5161 PT without descending lower in Y, and if we can, it means we
5162 reached PT before the scroll margin. */
5163 if (charpos != PT)
5165 struct it it2;
5166 void *it_data;
5168 it2 = it;
5169 it_data = bidi_shelve_cache ();
5170 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5171 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5173 charpos = IT_CHARPOS (it);
5174 bytepos = IT_BYTEPOS (it);
5175 bidi_unshelve_cache (it_data, true);
5177 else
5179 it = it2;
5180 bidi_unshelve_cache (it_data, false);
5184 /* See if point is on a partially visible line at the end. */
5185 if (it.what == IT_EOB)
5186 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5187 else
5189 move_it_by_lines (&it, 1);
5190 partial_p = it.current_y > it.last_visible_y;
5193 if (charpos == PT && !partial_p
5194 && (NILP (Vscroll_preserve_screen_position)
5195 || EQ (Vscroll_preserve_screen_position, Qt)))
5196 /* We found PT before we found the display margin, so PT is ok. */
5198 else if (window_scroll_pixel_based_preserve_y >= 0)
5200 int goal_y = min (it.last_visible_y - this_scroll_margin - 1,
5201 window_scroll_pixel_based_preserve_y);
5203 /* Don't let the preserved screen Y coordinate put us inside
5204 any of the two margins. */
5205 if (goal_y < this_scroll_margin)
5206 goal_y = this_scroll_margin;
5207 SET_TEXT_POS_FROM_MARKER (start, w->start);
5208 start_display (&it, w, start);
5209 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5210 here because we called start_display again and did not
5211 alter it.current_y this time. */
5212 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5213 goal_y, -1, MOVE_TO_Y | MOVE_TO_X);
5214 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5216 else
5218 if (partial_p)
5219 /* The last line was only partially visible, so back up two
5220 lines to make sure we're on a fully visible line. */
5222 move_it_by_lines (&it, -2);
5223 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5225 else
5226 /* No, the position we saved is OK, so use it. */
5227 SET_PT_BOTH (charpos, bytepos);
5230 bidi_unshelve_cache (itdata, false);
5232 if (adjust_old_pointm)
5233 Fset_marker (w->old_pointm,
5234 ((w == XWINDOW (selected_window))
5235 ? make_number (BUF_PT (XBUFFER (w->contents)))
5236 : Fmarker_position (w->pointm)),
5237 w->contents);
5241 /* Implementation of window_scroll that works based on screen lines.
5242 See the comment of window_scroll for parameter descriptions. */
5244 static void
5245 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5247 struct window *w = XWINDOW (window);
5248 /* Fvertical_motion enters redisplay, which can trigger
5249 fontification, which in turn can modify buffer text (e.g., if the
5250 fontification functions replace escape sequences with faces, as
5251 in `grep-mode-font-lock-keywords'). So we use a marker to record
5252 the old point position, to prevent crashes in SET_PT_BOTH. */
5253 Lisp_Object opoint_marker = Fpoint_marker ();
5254 register ptrdiff_t pos, pos_byte;
5255 register int ht = window_internal_height (w);
5256 register Lisp_Object tem;
5257 bool lose;
5258 Lisp_Object bolp;
5259 ptrdiff_t startpos = marker_position (w->start);
5260 ptrdiff_t startbyte = marker_byte_position (w->start);
5261 Lisp_Object original_pos = Qnil;
5262 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5263 Fwindow_old_point (window)));
5265 /* If scrolling screen-fulls, compute the number of lines to
5266 scroll from the window's height. */
5267 if (whole)
5268 n *= max (1, ht - next_screen_context_lines);
5270 if (!NILP (Vscroll_preserve_screen_position))
5272 if (window_scroll_preserve_vpos <= 0
5273 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5274 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5276 struct position posit
5277 = *compute_motion (startpos, startbyte, 0, 0, false,
5278 PT, ht, 0, -1, w->hscroll, 0, w);
5280 window_scroll_preserve_vpos = posit.vpos;
5281 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5284 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5285 make_number (window_scroll_preserve_vpos));
5288 XSETFASTINT (tem, PT);
5289 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5291 if (NILP (tem))
5293 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5294 startpos = PT;
5295 startbyte = PT_BYTE;
5298 SET_PT_BOTH (startpos, startbyte);
5299 lose = n < 0 && PT == BEGV;
5300 Fvertical_motion (make_number (n), window, Qnil);
5301 pos = PT;
5302 pos_byte = PT_BYTE;
5303 bolp = Fbolp ();
5304 SET_PT_BOTH (marker_position (opoint_marker),
5305 marker_byte_position (opoint_marker));
5307 if (lose)
5309 if (noerror)
5310 return;
5311 else
5312 xsignal0 (Qbeginning_of_buffer);
5315 if (pos < ZV)
5317 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5319 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5320 w->start_at_line_beg = !NILP (bolp);
5321 wset_update_mode_line (w);
5322 /* Set force_start so that redisplay_window will run
5323 the window-scroll-functions. */
5324 w->force_start = true;
5326 if (!NILP (Vscroll_preserve_screen_position)
5327 && this_scroll_margin == 0
5328 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5330 SET_PT_BOTH (pos, pos_byte);
5331 Fvertical_motion (original_pos, window, Qnil);
5333 /* If we scrolled forward, put point enough lines down
5334 that it is outside the scroll margin. */
5335 else if (n > 0)
5337 int top_margin;
5339 if (this_scroll_margin > 0)
5341 SET_PT_BOTH (pos, pos_byte);
5342 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5343 top_margin = PT;
5345 else
5346 top_margin = pos;
5348 if (top_margin <= marker_position (opoint_marker))
5349 SET_PT_BOTH (marker_position (opoint_marker),
5350 marker_byte_position (opoint_marker));
5351 else if (!NILP (Vscroll_preserve_screen_position))
5353 int nlines = window_scroll_preserve_vpos;
5355 SET_PT_BOTH (pos, pos_byte);
5356 if (window_scroll_preserve_vpos < this_scroll_margin)
5357 nlines = this_scroll_margin;
5358 else if (window_scroll_preserve_vpos
5359 >= w->total_lines - this_scroll_margin)
5360 nlines = w->total_lines - this_scroll_margin - 1;
5361 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5362 make_number (nlines)), window, Qnil);
5364 else
5365 SET_PT (top_margin);
5367 else if (n < 0)
5369 int bottom_margin;
5371 /* If we scrolled backward, put point near the end of the window
5372 but not within the scroll margin. */
5373 SET_PT_BOTH (pos, pos_byte);
5374 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5375 Qnil);
5376 if (XFASTINT (tem) == ht - this_scroll_margin)
5377 bottom_margin = PT;
5378 else
5379 bottom_margin = PT + 1;
5381 if (bottom_margin > marker_position (opoint_marker))
5382 SET_PT_BOTH (marker_position (opoint_marker),
5383 marker_byte_position (opoint_marker));
5384 else
5386 if (!NILP (Vscroll_preserve_screen_position))
5388 int nlines = window_scroll_preserve_vpos;
5390 SET_PT_BOTH (pos, pos_byte);
5391 if (window_scroll_preserve_vpos < this_scroll_margin)
5392 nlines = this_scroll_margin;
5393 else if (window_scroll_preserve_vpos
5394 >= ht - this_scroll_margin)
5395 nlines = ht - this_scroll_margin - 1;
5396 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5397 make_number (nlines)), window, Qnil);
5399 else
5400 Fvertical_motion (make_number (-1), window, Qnil);
5404 else
5406 if (noerror)
5407 return;
5408 else
5409 xsignal0 (Qend_of_buffer);
5412 if (adjust_old_pointm)
5413 Fset_marker (w->old_pointm,
5414 ((w == XWINDOW (selected_window))
5415 ? make_number (BUF_PT (XBUFFER (w->contents)))
5416 : Fmarker_position (w->pointm)),
5417 w->contents);
5421 /* Scroll selected_window up or down. If N is nil, scroll a
5422 screen-full which is defined as the height of the window minus
5423 next_screen_context_lines. If N is the symbol `-', scroll.
5424 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5425 up. This is the guts of Fscroll_up and Fscroll_down. */
5427 static void
5428 scroll_command (Lisp_Object n, int direction)
5430 ptrdiff_t count = SPECPDL_INDEX ();
5432 eassert (eabs (direction) == 1);
5434 /* If selected window's buffer isn't current, make it current for
5435 the moment. But don't screw up if window_scroll gets an error. */
5436 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5438 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5439 Fset_buffer (XWINDOW (selected_window)->contents);
5442 if (NILP (n))
5443 window_scroll (selected_window, direction, true, false);
5444 else if (EQ (n, Qminus))
5445 window_scroll (selected_window, -direction, true, false);
5446 else
5448 n = Fprefix_numeric_value (n);
5449 window_scroll (selected_window, XINT (n) * direction, false, false);
5452 unbind_to (count, Qnil);
5455 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5456 doc: /* Scroll text of selected window upward ARG lines.
5457 If ARG is omitted or nil, scroll upward by a near full screen.
5458 A near full screen is `next-screen-context-lines' less than a full screen.
5459 Negative ARG means scroll downward.
5460 If ARG is the atom `-', scroll downward by nearly full screen.
5461 When calling from a program, supply as argument a number, nil, or `-'. */)
5462 (Lisp_Object arg)
5464 scroll_command (arg, 1);
5465 return Qnil;
5468 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5469 doc: /* Scroll text of selected window down ARG lines.
5470 If ARG is omitted or nil, scroll down by a near full screen.
5471 A near full screen is `next-screen-context-lines' less than a full screen.
5472 Negative ARG means scroll upward.
5473 If ARG is the atom `-', scroll upward by nearly full screen.
5474 When calling from a program, supply as argument a number, nil, or `-'. */)
5475 (Lisp_Object arg)
5477 scroll_command (arg, -1);
5478 return Qnil;
5481 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5482 doc: /* Return the other window for \"other window scroll\" commands.
5483 If `other-window-scroll-buffer' is non-nil, a window
5484 showing that buffer is used.
5485 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5486 specifies the window. This takes precedence over
5487 `other-window-scroll-buffer'. */)
5488 (void)
5490 Lisp_Object window;
5492 if (MINI_WINDOW_P (XWINDOW (selected_window))
5493 && !NILP (Vminibuf_scroll_window))
5494 window = Vminibuf_scroll_window;
5495 /* If buffer is specified and live, scroll that buffer. */
5496 else if (!NILP (Vother_window_scroll_buffer)
5497 && BUFFERP (Vother_window_scroll_buffer)
5498 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5500 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5501 if (NILP (window))
5502 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5504 else
5506 /* Nothing specified; look for a neighboring window on the same
5507 frame. */
5508 window = Fnext_window (selected_window, Qnil, Qnil);
5510 if (EQ (window, selected_window))
5511 /* That didn't get us anywhere; look for a window on another
5512 visible frame. */
5514 window = Fnext_window (window, Qnil, Qt);
5515 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5516 && ! EQ (window, selected_window));
5519 CHECK_LIVE_WINDOW (window);
5521 if (EQ (window, selected_window))
5522 error ("There is no other window");
5524 return window;
5527 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5528 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5529 A near full screen is `next-screen-context-lines' less than a full screen.
5530 The next window is the one below the current one; or the one at the top
5531 if the current one is at the bottom. Negative ARG means scroll downward.
5532 If ARG is the atom `-', scroll downward by nearly full screen.
5533 When calling from a program, supply as argument a number, nil, or `-'.
5535 If `other-window-scroll-buffer' is non-nil, scroll the window
5536 showing that buffer, popping the buffer up if necessary.
5537 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5538 specifies the window to scroll. This takes precedence over
5539 `other-window-scroll-buffer'. */)
5540 (Lisp_Object arg)
5542 Lisp_Object window;
5543 struct window *w;
5544 ptrdiff_t count = SPECPDL_INDEX ();
5546 window = Fother_window_for_scrolling ();
5547 w = XWINDOW (window);
5549 /* Don't screw up if window_scroll gets an error. */
5550 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5552 Fset_buffer (w->contents);
5553 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5554 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5556 if (NILP (arg))
5557 window_scroll (window, 1, true, true);
5558 else if (EQ (arg, Qminus))
5559 window_scroll (window, -1, true, true);
5560 else
5562 if (CONSP (arg))
5563 arg = XCAR (arg);
5564 CHECK_NUMBER (arg);
5565 window_scroll (window, XINT (arg), false, true);
5568 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5569 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5570 unbind_to (count, Qnil);
5572 return Qnil;
5575 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5576 doc: /* Scroll selected window display ARG columns left.
5577 Default for ARG is window width minus 2.
5578 Value is the total amount of leftward horizontal scrolling in
5579 effect after the change.
5580 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5581 lower bound for automatic scrolling, i.e. automatic scrolling
5582 will not scroll a window to a column less than the value returned
5583 by this function. This happens in an interactive call. */)
5584 (register Lisp_Object arg, Lisp_Object set_minimum)
5586 struct window *w = XWINDOW (selected_window);
5587 EMACS_INT requested_arg = (NILP (arg)
5588 ? window_body_width (w, 0) - 2
5589 : XINT (Fprefix_numeric_value (arg)));
5590 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5592 if (!NILP (set_minimum))
5593 w->min_hscroll = w->hscroll;
5595 w->suspend_auto_hscroll = true;
5597 return result;
5600 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5601 doc: /* Scroll selected window display ARG columns right.
5602 Default for ARG is window width minus 2.
5603 Value is the total amount of leftward horizontal scrolling in
5604 effect after the change.
5605 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5606 lower bound for automatic scrolling, i.e. automatic scrolling
5607 will not scroll a window to a column less than the value returned
5608 by this function. This happens in an interactive call. */)
5609 (register Lisp_Object arg, Lisp_Object set_minimum)
5611 struct window *w = XWINDOW (selected_window);
5612 EMACS_INT requested_arg = (NILP (arg)
5613 ? window_body_width (w, 0) - 2
5614 : XINT (Fprefix_numeric_value (arg)));
5615 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5617 if (!NILP (set_minimum))
5618 w->min_hscroll = w->hscroll;
5620 w->suspend_auto_hscroll = true;
5622 return result;
5625 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5626 doc: /* Return the window which was selected when entering the minibuffer.
5627 Returns nil, if selected window is not a minibuffer window. */)
5628 (void)
5630 if (minibuf_level > 0
5631 && MINI_WINDOW_P (XWINDOW (selected_window))
5632 && WINDOW_LIVE_P (minibuf_selected_window))
5633 return minibuf_selected_window;
5635 return Qnil;
5638 /* Value is the number of lines actually displayed in window W,
5639 as opposed to its height. */
5641 static int
5642 displayed_window_lines (struct window *w)
5644 struct it it;
5645 struct text_pos start;
5646 int height = window_box_height (w);
5647 struct buffer *old_buffer;
5648 int bottom_y;
5649 void *itdata = NULL;
5651 if (XBUFFER (w->contents) != current_buffer)
5653 old_buffer = current_buffer;
5654 set_buffer_internal (XBUFFER (w->contents));
5656 else
5657 old_buffer = NULL;
5659 /* In case W->start is out of the accessible range, do something
5660 reasonable. This happens in Info mode when Info-scroll-down
5661 calls (recenter -1) while W->start is 1. */
5662 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5664 itdata = bidi_shelve_cache ();
5665 start_display (&it, w, start);
5666 move_it_vertically (&it, height);
5667 bottom_y = line_bottom_y (&it);
5668 bidi_unshelve_cache (itdata, false);
5670 /* Add in empty lines at the bottom of the window. */
5671 if (bottom_y < height)
5673 int uy = FRAME_LINE_HEIGHT (it.f);
5674 it.vpos += (height - bottom_y + uy - 1) / uy;
5676 else if (bottom_y == height)
5677 it.vpos++;
5679 if (old_buffer)
5680 set_buffer_internal (old_buffer);
5682 return it.vpos;
5686 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5687 doc: /* Center point in selected window and maybe redisplay frame.
5688 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5689 relative to the selected window. If ARG is negative, it counts up from the
5690 bottom of the window. (ARG should be less than the height of the window.)
5692 If ARG is omitted or nil, then recenter with point on the middle line of
5693 the selected window; if the variable `recenter-redisplay' is non-nil,
5694 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5695 is set to `grow-only', this resets the tool-bar's height to the minimum
5696 height needed); if `recenter-redisplay' has the special value `tty',
5697 then only tty frames are redrawn.
5699 Just C-u as prefix means put point in the center of the window
5700 and redisplay normally--don't erase and redraw the frame. */)
5701 (register Lisp_Object arg)
5703 struct window *w = XWINDOW (selected_window);
5704 struct buffer *buf = XBUFFER (w->contents);
5705 bool center_p = false;
5706 ptrdiff_t charpos, bytepos;
5707 EMACS_INT iarg UNINIT;
5708 int this_scroll_margin;
5710 if (buf != current_buffer)
5711 error ("`recenter'ing a window that does not display current-buffer.");
5713 /* If redisplay is suppressed due to an error, try again. */
5714 buf->display_error_modiff = 0;
5716 if (NILP (arg))
5718 if (!NILP (Vrecenter_redisplay)
5719 && (!EQ (Vrecenter_redisplay, Qtty)
5720 || !NILP (Ftty_type (selected_frame))))
5722 ptrdiff_t i;
5724 /* Invalidate pixel data calculated for all compositions. */
5725 for (i = 0; i < n_compositions; i++)
5726 composition_table[i]->font = NULL;
5727 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5728 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5729 #endif
5730 Fredraw_frame (WINDOW_FRAME (w));
5731 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5734 center_p = true;
5736 else if (CONSP (arg)) /* Just C-u. */
5737 center_p = true;
5738 else
5740 arg = Fprefix_numeric_value (arg);
5741 CHECK_NUMBER (arg);
5742 iarg = XINT (arg);
5745 /* Do this after making BUF current
5746 in case scroll_margin is buffer-local. */
5747 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5749 /* Don't use redisplay code for initial frames, as the necessary
5750 data structures might not be set up yet then. */
5751 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5753 if (center_p)
5755 struct it it;
5756 struct text_pos pt;
5757 void *itdata = bidi_shelve_cache ();
5759 SET_TEXT_POS (pt, PT, PT_BYTE);
5760 start_display (&it, w, pt);
5761 move_it_vertically_backward (&it, window_box_height (w) / 2);
5762 charpos = IT_CHARPOS (it);
5763 bytepos = IT_BYTEPOS (it);
5764 bidi_unshelve_cache (itdata, false);
5766 else if (iarg < 0)
5768 struct it it;
5769 struct text_pos pt;
5770 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5771 int extra_line_spacing;
5772 int h = window_box_height (w);
5773 int ht = window_internal_height (w);
5774 void *itdata = bidi_shelve_cache ();
5776 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5777 ht - this_scroll_margin);
5779 SET_TEXT_POS (pt, PT, PT_BYTE);
5780 start_display (&it, w, pt);
5782 /* Be sure we have the exact height of the full line containing PT. */
5783 move_it_by_lines (&it, 0);
5785 /* The amount of pixels we have to move back is the window
5786 height minus what's displayed in the line containing PT,
5787 and the lines below. */
5788 it.current_y = 0;
5789 it.vpos = 0;
5790 move_it_by_lines (&it, nlines);
5792 if (it.vpos == nlines)
5793 h -= it.current_y;
5794 else
5796 /* Last line has no newline. */
5797 h -= line_bottom_y (&it);
5798 it.vpos++;
5801 /* Don't reserve space for extra line spacing of last line. */
5802 extra_line_spacing = it.max_extra_line_spacing;
5804 /* If we can't move down NLINES lines because we hit
5805 the end of the buffer, count in some empty lines. */
5806 if (it.vpos < nlines)
5808 nlines -= it.vpos;
5809 extra_line_spacing = it.extra_line_spacing;
5810 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5812 if (h <= 0)
5814 bidi_unshelve_cache (itdata, false);
5815 return Qnil;
5818 /* Now find the new top line (starting position) of the window. */
5819 start_display (&it, w, pt);
5820 it.current_y = 0;
5821 move_it_vertically_backward (&it, h);
5823 /* If extra line spacing is present, we may move too far
5824 back. This causes the last line to be only partially
5825 visible (which triggers redisplay to recenter that line
5826 in the middle), so move forward.
5827 But ignore extra line spacing on last line, as it is not
5828 considered to be part of the visible height of the line.
5830 h += extra_line_spacing;
5831 while (-it.current_y > h)
5832 move_it_by_lines (&it, 1);
5834 charpos = IT_CHARPOS (it);
5835 bytepos = IT_BYTEPOS (it);
5837 bidi_unshelve_cache (itdata, false);
5839 else
5841 struct it it;
5842 struct text_pos pt;
5843 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5844 int ht = window_internal_height (w);
5845 void *itdata = bidi_shelve_cache ();
5847 nlines = clip_to_bounds (this_scroll_margin, nlines,
5848 ht - this_scroll_margin - 1);
5850 SET_TEXT_POS (pt, PT, PT_BYTE);
5851 start_display (&it, w, pt);
5853 /* Move to the beginning of screen line containing PT. */
5854 move_it_by_lines (&it, 0);
5856 /* Move back to find the point which is ARG screen lines above PT. */
5857 if (nlines > 0)
5859 it.current_y = 0;
5860 it.vpos = 0;
5861 move_it_by_lines (&it, -nlines);
5864 charpos = IT_CHARPOS (it);
5865 bytepos = IT_BYTEPOS (it);
5867 bidi_unshelve_cache (itdata, false);
5870 else
5872 struct position pos;
5873 int ht = window_internal_height (w);
5875 if (center_p)
5876 iarg = ht / 2;
5877 else if (iarg < 0)
5878 iarg += ht;
5880 /* Don't let it get into the margin at either top or bottom. */
5881 iarg = clip_to_bounds (this_scroll_margin, iarg,
5882 ht - this_scroll_margin - 1);
5884 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5885 charpos = pos.bufpos;
5886 bytepos = pos.bytepos;
5889 /* Set the new window start. */
5890 set_marker_both (w->start, w->contents, charpos, bytepos);
5891 w->window_end_valid = false;
5893 w->optional_new_start = true;
5895 w->start_at_line_beg = (bytepos == BEGV_BYTE
5896 || FETCH_BYTE (bytepos - 1) == '\n');
5898 wset_redisplay (w);
5900 return Qnil;
5903 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5904 0, 2, 0,
5905 doc: /* Return the width in columns of the text display area of WINDOW.
5906 WINDOW must be a live window and defaults to the selected one.
5908 The returned width does not include dividers, scrollbars, margins,
5909 fringes, nor any partial-width columns at the right of the text
5910 area.
5912 Optional argument PIXELWISE non-nil, means to return the width in
5913 pixels. */)
5914 (Lisp_Object window, Lisp_Object pixelwise)
5916 struct window *w = decode_live_window (window);
5918 if (NILP (pixelwise))
5919 return make_number (window_box_width (w, TEXT_AREA)
5920 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5921 else
5922 return make_number (window_box_width (w, TEXT_AREA));
5925 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5926 0, 2, 0,
5927 doc: /* Return the height in lines of the text display area of WINDOW.
5928 WINDOW must be a live window and defaults to the selected one.
5930 The returned height does not include dividers, the mode line, any header
5931 line, nor any partial-height lines at the bottom of the text area.
5933 Optional argument PIXELWISE non-nil, means to return the height in
5934 pixels. */)
5935 (Lisp_Object window, Lisp_Object pixelwise)
5937 struct window *w = decode_live_window (window);
5939 if (NILP (pixelwise))
5940 return make_number (window_box_height (w)
5941 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5942 else
5943 return make_number (window_box_height (w));
5946 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5947 1, 1, "P",
5948 doc: /* Position point relative to window.
5949 ARG nil means position point at center of window.
5950 Else, ARG specifies vertical position within the window;
5951 zero means top of window, negative means relative to bottom
5952 of window, -1 meaning the last fully visible display line
5953 of the window.
5955 Value is the screen line of the window point moved to, counting
5956 from the top of the window. */)
5957 (Lisp_Object arg)
5959 struct window *w = XWINDOW (selected_window);
5960 int lines, start;
5961 Lisp_Object window;
5962 #if false
5963 int this_scroll_margin;
5964 #endif
5966 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5967 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5968 when passed below to set_marker_both. */
5969 error ("move-to-window-line called from unrelated buffer");
5971 window = selected_window;
5972 start = marker_position (w->start);
5973 if (start < BEGV || start > ZV)
5975 int height = window_internal_height (w);
5976 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5977 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5978 w->start_at_line_beg = !NILP (Fbolp ());
5979 w->force_start = true;
5981 else
5982 Fgoto_char (w->start);
5984 lines = displayed_window_lines (w);
5986 if (NILP (arg))
5987 XSETFASTINT (arg, lines / 2);
5988 else
5990 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5992 if (iarg < 0)
5993 iarg = iarg + lines;
5995 #if false /* This code would prevent move-to-window-line from moving point
5996 to a place inside the scroll margins (which would cause the
5997 next redisplay to scroll). I wrote this code, but then concluded
5998 it is probably better not to install it. However, it is here
5999 inside #if false so as not to lose it. -- rms. */
6001 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
6003 /* Don't let it get into the margin at either top or bottom. */
6004 iarg = max (iarg, this_scroll_margin);
6005 iarg = min (iarg, lines - this_scroll_margin - 1);
6006 #endif
6008 arg = make_number (iarg);
6011 /* Skip past a partially visible first line. */
6012 if (w->vscroll)
6013 XSETINT (arg, XINT (arg) + 1);
6015 return Fvertical_motion (arg, window, Qnil);
6020 /***********************************************************************
6021 Window Configuration
6022 ***********************************************************************/
6024 struct save_window_data
6026 struct vectorlike_header header;
6027 Lisp_Object selected_frame;
6028 Lisp_Object current_window;
6029 Lisp_Object f_current_buffer;
6030 Lisp_Object minibuf_scroll_window;
6031 Lisp_Object minibuf_selected_window;
6032 Lisp_Object root_window;
6033 Lisp_Object focus_frame;
6034 /* A vector, each of whose elements is a struct saved_window
6035 for one window. */
6036 Lisp_Object saved_windows;
6038 /* All fields above are traced by the GC.
6039 From `frame-cols' down, the fields are ignored by the GC. */
6040 /* We should be able to do without the following two. */
6041 int frame_cols, frame_lines;
6042 /* These two should get eventually replaced by their pixel
6043 counterparts. */
6044 int frame_menu_bar_lines, frame_tool_bar_lines;
6045 int frame_text_width, frame_text_height;
6046 /* These are currently unused. We need them as soon as we convert
6047 to pixels. */
6048 int frame_menu_bar_height, frame_tool_bar_height;
6051 /* This is saved as a Lisp_Vector. */
6052 struct saved_window
6054 struct vectorlike_header header;
6056 Lisp_Object window, buffer, start, pointm, old_pointm;
6057 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6058 Lisp_Object pixel_height_before_size_change, pixel_width_before_size_change;
6059 Lisp_Object left_col, top_line, total_cols, total_lines;
6060 Lisp_Object normal_cols, normal_lines;
6061 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6062 Lisp_Object parent, prev;
6063 Lisp_Object start_at_line_beg;
6064 Lisp_Object display_table;
6065 Lisp_Object left_margin_cols, right_margin_cols;
6066 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6067 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6068 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6069 Lisp_Object combination_limit, window_parameters;
6072 #define SAVED_WINDOW_N(swv,n) \
6073 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6075 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6076 doc: /* Return t if OBJECT is a window-configuration object. */)
6077 (Lisp_Object object)
6079 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6082 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6083 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6084 (Lisp_Object config)
6086 register struct save_window_data *data;
6087 struct Lisp_Vector *saved_windows;
6089 CHECK_WINDOW_CONFIGURATION (config);
6091 data = (struct save_window_data *) XVECTOR (config);
6092 saved_windows = XVECTOR (data->saved_windows);
6093 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6096 DEFUN ("set-window-configuration", Fset_window_configuration,
6097 Sset_window_configuration, 1, 1, 0,
6098 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6099 CONFIGURATION must be a value previously returned
6100 by `current-window-configuration' (which see).
6101 If CONFIGURATION was made from a frame that is now deleted,
6102 only frame-independent values can be restored. In this case,
6103 the return value is nil. Otherwise the value is t. */)
6104 (Lisp_Object configuration)
6106 register struct save_window_data *data;
6107 struct Lisp_Vector *saved_windows;
6108 Lisp_Object new_current_buffer;
6109 Lisp_Object frame;
6110 struct frame *f;
6111 ptrdiff_t old_point = -1;
6112 USE_SAFE_ALLOCA;
6114 CHECK_WINDOW_CONFIGURATION (configuration);
6116 data = (struct save_window_data *) XVECTOR (configuration);
6117 saved_windows = XVECTOR (data->saved_windows);
6119 new_current_buffer = data->f_current_buffer;
6120 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6121 new_current_buffer = Qnil;
6122 else
6124 if (XBUFFER (new_current_buffer) == current_buffer)
6125 /* The code further down "preserves point" by saving here PT in
6126 old_point and then setting it later back into PT. When the
6127 current-selected-window and the final-selected-window both show
6128 the current buffer, this suffers from the problem that the
6129 current PT is the window-point of the current-selected-window,
6130 while the final PT is the point of the final-selected-window, so
6131 this copy from one PT to the other would end up moving the
6132 window-point of the final-selected-window to the window-point of
6133 the current-selected-window. So we have to be careful which
6134 point of the current-buffer we copy into old_point. */
6135 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6136 && WINDOWP (selected_window)
6137 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6138 && !EQ (selected_window, data->current_window))
6139 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6140 else
6141 old_point = PT;
6142 else
6143 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6144 point in new_current_buffer as of the last time this buffer was
6145 used. This can be non-deterministic since it can be changed by
6146 things like jit-lock by mere temporary selection of some random
6147 window that happens to show this buffer.
6148 So if possible we want this arbitrary choice of "which point" to
6149 be the one from the to-be-selected-window so as to prevent this
6150 window's cursor from being copied from another window. */
6151 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6152 /* If current_window = selected_window, its point is in BUF_PT. */
6153 && !EQ (selected_window, data->current_window))
6154 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6155 else
6156 old_point = BUF_PT (XBUFFER (new_current_buffer));
6159 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6160 f = XFRAME (frame);
6162 /* If f is a dead frame, don't bother rebuilding its window tree.
6163 However, there is other stuff we should still try to do below. */
6164 if (FRAME_LIVE_P (f))
6166 Lisp_Object window;
6167 Lisp_Object dead_windows = Qnil;
6168 Lisp_Object tem, par, pers;
6169 struct window *w;
6170 struct saved_window *p;
6171 struct window *root_window;
6172 struct window **leaf_windows;
6173 ptrdiff_t i, k, n_leaf_windows;
6174 /* Records whether a window has been added or removed wrt the
6175 original configuration. */
6176 bool window_changed = false;
6177 /* Records whether a window has changed its buffer wrt the
6178 original configuration. */
6179 bool buffer_changed = false;
6181 /* Don't do this within the main loop below: This may call Lisp
6182 code and is thus potentially unsafe while input is blocked. */
6183 for (k = 0; k < saved_windows->header.size; k++)
6185 p = SAVED_WINDOW_N (saved_windows, k);
6186 window = p->window;
6187 w = XWINDOW (window);
6189 if (NILP (w->contents))
6190 /* A dead window that will be resurrected, the window
6191 configuration will change. */
6192 window_changed = true;
6194 if (BUFFERP (w->contents)
6195 && !EQ (w->contents, p->buffer)
6196 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6197 /* If a window we restore gets another buffer, record the
6198 window's old buffer. */
6199 call1 (Qrecord_window_buffer, window);
6202 /* Disallow x_set_window_size, temporarily. */
6203 f->can_x_set_window_size = false;
6204 /* The mouse highlighting code could get screwed up
6205 if it runs during this. */
6206 block_input ();
6208 /* "Swap out" point from the selected window's buffer
6209 into the window itself. (Normally the pointm of the selected
6210 window holds garbage.) We do this now, before
6211 restoring the window contents, and prevent it from
6212 being done later on when we select a new window. */
6213 if (! NILP (XWINDOW (selected_window)->contents))
6215 w = XWINDOW (selected_window);
6216 set_marker_both (w->pointm,
6217 w->contents,
6218 BUF_PT (XBUFFER (w->contents)),
6219 BUF_PT_BYTE (XBUFFER (w->contents)));
6222 fset_redisplay (f);
6224 /* Problem: Freeing all matrices and later allocating them again
6225 is a serious redisplay flickering problem. What we would
6226 really like to do is to free only those matrices not reused
6227 below. */
6228 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6229 ptrdiff_t nwindows = count_windows (root_window);
6230 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6231 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6233 /* Kludge Alert!
6234 Mark all windows now on frame as "deleted".
6235 Restoring the new configuration "undeletes" any that are in it.
6237 Save their current buffers in their height fields, since we may
6238 need it later, if a buffer saved in the configuration is now
6239 dead. */
6240 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6242 for (k = 0; k < saved_windows->header.size; k++)
6244 p = SAVED_WINDOW_N (saved_windows, k);
6245 window = p->window;
6246 w = XWINDOW (window);
6247 wset_next (w, Qnil);
6249 if (!NILP (p->parent))
6250 wset_parent
6251 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6252 else
6253 wset_parent (w, Qnil);
6255 if (!NILP (p->prev))
6257 wset_prev
6258 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6259 wset_next (XWINDOW (w->prev), p->window);
6261 else
6263 wset_prev (w, Qnil);
6264 if (!NILP (w->parent))
6265 wset_combination (XWINDOW (w->parent),
6266 (XINT (p->total_cols)
6267 != XWINDOW (w->parent)->total_cols),
6268 p->window);
6271 /* If we squirreled away the buffer, restore it now. */
6272 if (BUFFERP (w->combination_limit))
6273 wset_buffer (w, w->combination_limit);
6274 w->pixel_left = XFASTINT (p->pixel_left);
6275 w->pixel_top = XFASTINT (p->pixel_top);
6276 w->pixel_width = XFASTINT (p->pixel_width);
6277 w->pixel_height = XFASTINT (p->pixel_height);
6278 w->pixel_width_before_size_change
6279 = XFASTINT (p->pixel_width_before_size_change);
6280 w->pixel_height_before_size_change
6281 = XFASTINT (p->pixel_height_before_size_change);
6282 w->left_col = XFASTINT (p->left_col);
6283 w->top_line = XFASTINT (p->top_line);
6284 w->total_cols = XFASTINT (p->total_cols);
6285 w->total_lines = XFASTINT (p->total_lines);
6286 wset_normal_cols (w, p->normal_cols);
6287 wset_normal_lines (w, p->normal_lines);
6288 w->hscroll = XFASTINT (p->hscroll);
6289 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6290 w->min_hscroll = XFASTINT (p->min_hscroll);
6291 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6292 wset_display_table (w, p->display_table);
6293 w->left_margin_cols = XINT (p->left_margin_cols);
6294 w->right_margin_cols = XINT (p->right_margin_cols);
6295 w->left_fringe_width = XINT (p->left_fringe_width);
6296 w->right_fringe_width = XINT (p->right_fringe_width);
6297 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6298 w->scroll_bar_width = XINT (p->scroll_bar_width);
6299 w->scroll_bar_height = XINT (p->scroll_bar_height);
6300 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6301 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6302 wset_dedicated (w, p->dedicated);
6303 wset_combination_limit (w, p->combination_limit);
6304 /* Restore any window parameters that have been saved.
6305 Parameters that have not been saved are left alone. */
6306 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6308 pers = XCAR (tem);
6309 if (CONSP (pers))
6311 if (NILP (XCDR (pers)))
6313 par = Fassq (XCAR (pers), w->window_parameters);
6314 if (CONSP (par) && !NILP (XCDR (par)))
6315 /* Reset a parameter to nil if and only if it
6316 has a non-nil association. Don't make new
6317 associations. */
6318 Fsetcdr (par, Qnil);
6320 else
6321 /* Always restore a non-nil value. */
6322 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6326 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6327 /* If saved buffer is alive, install it. */
6329 if (!EQ (w->contents, p->buffer))
6330 /* Record buffer configuration change. */
6331 buffer_changed = true;
6332 wset_buffer (w, p->buffer);
6333 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6334 set_marker_restricted (w->start, p->start, w->contents);
6335 set_marker_restricted (w->pointm, p->pointm, w->contents);
6336 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6337 /* As documented in Fcurrent_window_configuration, don't
6338 restore the location of point in the buffer which was
6339 current when the window configuration was recorded. */
6340 if (!EQ (p->buffer, new_current_buffer)
6341 && XBUFFER (p->buffer) == current_buffer)
6342 Fgoto_char (w->pointm);
6344 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6345 /* Keep window's old buffer; make sure the markers are real. */
6347 /* Set window markers at start of visible range. */
6348 if (XMARKER (w->start)->buffer == 0)
6349 set_marker_restricted_both (w->start, w->contents, 0, 0);
6350 if (XMARKER (w->pointm)->buffer == 0)
6351 set_marker_restricted_both
6352 (w->pointm, w->contents,
6353 BUF_PT (XBUFFER (w->contents)),
6354 BUF_PT_BYTE (XBUFFER (w->contents)));
6355 if (XMARKER (w->old_pointm)->buffer == 0)
6356 set_marker_restricted_both
6357 (w->old_pointm, w->contents,
6358 BUF_PT (XBUFFER (w->contents)),
6359 BUF_PT_BYTE (XBUFFER (w->contents)));
6360 w->start_at_line_beg = true;
6362 else if (!NILP (w->start))
6363 /* Leaf window has no live buffer, get one. */
6365 /* Record buffer configuration change. */
6366 buffer_changed = true;
6367 /* Get the buffer via other_buffer_safely in order to
6368 avoid showing an unimportant buffer and, if necessary, to
6369 recreate *scratch* in the course (part of Juanma's bs-show
6370 scenario from March 2011). */
6371 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6372 /* This will set the markers to beginning of visible
6373 range. */
6374 set_marker_restricted_both (w->start, w->contents, 0, 0);
6375 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6376 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6377 w->start_at_line_beg = true;
6378 if (!NILP (w->dedicated))
6379 /* Record this window as dead. */
6380 dead_windows = Fcons (window, dead_windows);
6381 /* Make sure window is no more dedicated. */
6382 wset_dedicated (w, Qnil);
6386 fset_root_window (f, data->root_window);
6387 /* Arrange *not* to restore point in the buffer that was
6388 current when the window configuration was saved. */
6389 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6390 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6391 make_number (old_point),
6392 XWINDOW (data->current_window)->contents);
6394 /* In the following call to `select-window', prevent "swapping out
6395 point" in the old selected window using the buffer that has
6396 been restored into it. We already swapped out that point from
6397 that window's old buffer.
6399 Do not record the buffer here. We do that in a separate call
6400 to select_window below. See also Bug#16207. */
6401 select_window (data->current_window, Qt, true);
6402 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6403 last_selected_window)
6404 = selected_window;
6406 if (NILP (data->focus_frame)
6407 || (FRAMEP (data->focus_frame)
6408 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6409 Fredirect_frame_focus (frame, data->focus_frame);
6411 /* Now, free glyph matrices in windows that were not reused. */
6412 for (i = 0; i < n_leaf_windows; i++)
6413 if (NILP (leaf_windows[i]->contents))
6415 free_window_matrices (leaf_windows[i]);
6416 window_changed = true;
6419 /* Allow x_set_window_size again and apply frame size changes if
6420 needed. */
6421 f->can_x_set_window_size = true;
6422 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6424 adjust_frame_glyphs (f);
6425 unblock_input ();
6427 /* Scan dead buffer windows. */
6428 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6430 window = XCAR (dead_windows);
6431 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6432 delete_deletable_window (window);
6435 /* Record the selected window's buffer here. The window should
6436 already be the selected one from the call above. */
6437 if (WINDOW_LIVE_P (data->current_window))
6438 select_window (data->current_window, Qnil, false);
6440 /* Fselect_window will have made f the selected frame, so we
6441 reselect the proper frame here. Fhandle_switch_frame will change the
6442 selected window too, but that doesn't make the call to
6443 Fselect_window above totally superfluous; it still sets f's
6444 selected window. */
6445 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6446 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6448 if (window_changed)
6449 /* At least one window has been added or removed. Run
6450 `window-configuration-change-hook' and make sure
6451 `window-size-change-functions' get run later.
6453 We have to do this in order to capture the following
6454 scenario: Suppose our frame contains two live windows W1 and
6455 W2 and ‘set-window-configuration’ replaces them by two
6456 windows W3 and W4 that were dead the last time
6457 run_window_size_change_functions was run. If W3 and W4 have
6458 the same values for their old and new pixel sizes but these
6459 values differ from those of W1 and W2, the sizes of our
6460 frame's two live windows changed but window_size_changed has
6461 no means to detect that fact.
6463 Obviously, this will get us false positives, for example,
6464 when we restore the original configuration with W1 and W2
6465 before run_window_size_change_functions gets called. */
6467 run_window_configuration_change_hook (f);
6468 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = true;
6470 else if (buffer_changed)
6471 /* At least one window has changed its buffer. Run
6472 `window-configuration-change-hook' only. */
6473 run_window_configuration_change_hook (f);
6476 if (!NILP (new_current_buffer))
6478 Fset_buffer (new_current_buffer);
6479 /* If the new current buffer doesn't appear in the selected
6480 window, go to its old point (see bug#12208). */
6481 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6482 Fgoto_char (make_number (old_point));
6485 Vminibuf_scroll_window = data->minibuf_scroll_window;
6486 minibuf_selected_window = data->minibuf_selected_window;
6488 SAFE_FREE ();
6489 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6493 void
6494 restore_window_configuration (Lisp_Object configuration)
6496 Fset_window_configuration (configuration);
6500 /* If WINDOW is an internal window, recursively delete all child windows
6501 reachable via the next and contents slots of WINDOW. Otherwise setup
6502 WINDOW to not show any buffer. */
6504 void
6505 delete_all_child_windows (Lisp_Object window)
6507 register struct window *w;
6509 w = XWINDOW (window);
6511 if (!NILP (w->next))
6512 /* Delete WINDOW's siblings (we traverse postorderly). */
6513 delete_all_child_windows (w->next);
6515 if (WINDOWP (w->contents))
6517 delete_all_child_windows (w->contents);
6518 wset_combination (w, false, Qnil);
6520 else if (BUFFERP (w->contents))
6522 unshow_buffer (w);
6523 unchain_marker (XMARKER (w->pointm));
6524 unchain_marker (XMARKER (w->old_pointm));
6525 unchain_marker (XMARKER (w->start));
6526 /* Since combination limit makes sense for an internal windows
6527 only, we use this slot to save the buffer for the sake of
6528 possible resurrection in Fset_window_configuration. */
6529 wset_combination_limit (w, w->contents);
6530 wset_buffer (w, Qnil);
6533 Vwindow_list = Qnil;
6536 static ptrdiff_t
6537 count_windows (struct window *window)
6539 ptrdiff_t count = 1;
6540 if (!NILP (window->next))
6541 count += count_windows (XWINDOW (window->next));
6542 if (WINDOWP (window->contents))
6543 count += count_windows (XWINDOW (window->contents));
6544 return count;
6548 /* Fill vector FLAT with leaf windows under W, starting at index I.
6549 Value is last index + 1. */
6550 static ptrdiff_t
6551 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6553 while (w)
6555 if (WINDOWP (w->contents))
6556 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6557 else
6558 flat[i++] = w;
6560 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6563 return i;
6567 /* Return a pointer to the glyph W's physical cursor is on. Value is
6568 null if W's current matrix is invalid, so that no meaningful glyph
6569 can be returned. */
6570 struct glyph *
6571 get_phys_cursor_glyph (struct window *w)
6573 struct glyph_row *row;
6574 struct glyph *glyph;
6575 int hpos = w->phys_cursor.hpos;
6577 if (!(w->phys_cursor.vpos >= 0
6578 && w->phys_cursor.vpos < w->current_matrix->nrows))
6579 return NULL;
6581 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6582 if (!row->enabled_p)
6583 return NULL;
6585 if (w->hscroll)
6587 /* When the window is hscrolled, cursor hpos can legitimately be
6588 out of bounds, but we draw the cursor at the corresponding
6589 window margin in that case. */
6590 if (!row->reversed_p && hpos < 0)
6591 hpos = 0;
6592 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6593 hpos = row->used[TEXT_AREA] - 1;
6596 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6597 glyph = row->glyphs[TEXT_AREA] + hpos;
6598 else
6599 glyph = NULL;
6601 return glyph;
6605 static ptrdiff_t
6606 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6608 struct saved_window *p;
6609 struct window *w;
6610 Lisp_Object tem, pers, par;
6612 for (; !NILP (window); window = w->next)
6614 p = SAVED_WINDOW_N (vector, i);
6615 w = XWINDOW (window);
6617 wset_temslot (w, make_number (i)); i++;
6618 p->window = window;
6619 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6620 p->pixel_left = make_number (w->pixel_left);
6621 p->pixel_top = make_number (w->pixel_top);
6622 p->pixel_width = make_number (w->pixel_width);
6623 p->pixel_height = make_number (w->pixel_height);
6624 p->pixel_width_before_size_change
6625 = make_number (w->pixel_width_before_size_change);
6626 p->pixel_height_before_size_change
6627 = make_number (w->pixel_height_before_size_change);
6628 p->left_col = make_number (w->left_col);
6629 p->top_line = make_number (w->top_line);
6630 p->total_cols = make_number (w->total_cols);
6631 p->total_lines = make_number (w->total_lines);
6632 p->normal_cols = w->normal_cols;
6633 p->normal_lines = w->normal_lines;
6634 XSETFASTINT (p->hscroll, w->hscroll);
6635 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6636 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6637 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6638 p->display_table = w->display_table;
6639 p->left_margin_cols = make_number (w->left_margin_cols);
6640 p->right_margin_cols = make_number (w->right_margin_cols);
6641 p->left_fringe_width = make_number (w->left_fringe_width);
6642 p->right_fringe_width = make_number (w->right_fringe_width);
6643 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6644 p->scroll_bar_width = make_number (w->scroll_bar_width);
6645 p->scroll_bar_height = make_number (w->scroll_bar_height);
6646 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6647 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6648 p->dedicated = w->dedicated;
6649 p->combination_limit = w->combination_limit;
6650 p->window_parameters = Qnil;
6652 if (!NILP (Vwindow_persistent_parameters))
6654 /* Run cycle detection on Vwindow_persistent_parameters. */
6655 Lisp_Object tortoise, hare;
6657 hare = tortoise = Vwindow_persistent_parameters;
6658 while (CONSP (hare))
6660 hare = XCDR (hare);
6661 if (!CONSP (hare))
6662 break;
6664 hare = XCDR (hare);
6665 tortoise = XCDR (tortoise);
6667 if (EQ (hare, tortoise))
6668 /* Reset Vwindow_persistent_parameters to Qnil. */
6670 Vwindow_persistent_parameters = Qnil;
6671 break;
6675 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6676 tem = XCDR (tem))
6678 pers = XCAR (tem);
6679 /* Save values for persistent window parameters. */
6680 if (CONSP (pers) && !NILP (XCDR (pers)))
6682 par = Fassq (XCAR (pers), w->window_parameters);
6683 if (NILP (par))
6684 /* If the window has no value for the parameter,
6685 make one. */
6686 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6687 p->window_parameters);
6688 else
6689 /* If the window has a value for the parameter,
6690 save it. */
6691 p->window_parameters = Fcons (Fcons (XCAR (par),
6692 XCDR (par)),
6693 p->window_parameters);
6698 if (BUFFERP (w->contents))
6700 /* Save w's value of point in the window configuration. If w
6701 is the selected window, then get the value of point from
6702 the buffer; pointm is garbage in the selected window. */
6703 if (EQ (window, selected_window))
6704 p->pointm = build_marker (XBUFFER (w->contents),
6705 BUF_PT (XBUFFER (w->contents)),
6706 BUF_PT_BYTE (XBUFFER (w->contents)));
6707 else
6708 p->pointm = Fcopy_marker (w->pointm, Qnil);
6709 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6710 XMARKER (p->pointm)->insertion_type
6711 = !NILP (buffer_local_value /* Don't signal error if void. */
6712 (Qwindow_point_insertion_type, w->contents));
6713 XMARKER (p->old_pointm)->insertion_type
6714 = !NILP (buffer_local_value /* Don't signal error if void. */
6715 (Qwindow_point_insertion_type, w->contents));
6717 p->start = Fcopy_marker (w->start, Qnil);
6718 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6720 else
6722 p->pointm = Qnil;
6723 p->old_pointm = Qnil;
6724 p->start = Qnil;
6725 p->start_at_line_beg = Qnil;
6728 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6729 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6731 if (WINDOWP (w->contents))
6732 i = save_window_save (w->contents, vector, i);
6735 return i;
6738 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6739 Scurrent_window_configuration, 0, 1, 0,
6740 doc: /* Return an object representing the current window configuration of FRAME.
6741 If FRAME is nil or omitted, use the selected frame.
6742 This describes the number of windows, their sizes and current buffers,
6743 and for each displayed buffer, where display starts, and the position of
6744 point. An exception is made for point in the current buffer:
6745 its value is -not- saved.
6746 This also records the currently selected frame, and FRAME's focus
6747 redirection (see `redirect-frame-focus'). The variable
6748 `window-persistent-parameters' specifies which window parameters are
6749 saved by this function. */)
6750 (Lisp_Object frame)
6752 Lisp_Object tem;
6753 ptrdiff_t i, n_windows;
6754 struct save_window_data *data;
6755 struct frame *f = decode_live_frame (frame);
6757 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6758 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6759 PVEC_WINDOW_CONFIGURATION);
6761 data->frame_cols = FRAME_COLS (f);
6762 data->frame_lines = FRAME_LINES (f);
6763 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6764 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6765 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6766 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6767 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6768 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6769 data->selected_frame = selected_frame;
6770 data->current_window = FRAME_SELECTED_WINDOW (f);
6771 XSETBUFFER (data->f_current_buffer, current_buffer);
6772 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6773 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6774 data->root_window = FRAME_ROOT_WINDOW (f);
6775 data->focus_frame = FRAME_FOCUS_FRAME (f);
6776 tem = make_uninit_vector (n_windows);
6777 data->saved_windows = tem;
6778 for (i = 0; i < n_windows; i++)
6779 ASET (tem, i,
6780 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6781 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6782 XSETWINDOW_CONFIGURATION (tem, data);
6783 return (tem);
6786 /* Called after W's margins, fringes or scroll bars was adjusted. */
6788 static void
6789 apply_window_adjustment (struct window *w)
6791 eassert (w);
6792 clear_glyph_matrix (w->current_matrix);
6793 w->window_end_valid = false;
6794 wset_redisplay (w);
6795 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6799 /***********************************************************************
6800 Marginal Areas
6801 ***********************************************************************/
6803 static int
6804 extract_dimension (Lisp_Object dimension)
6806 if (NILP (dimension))
6807 return -1;
6808 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6809 return XINT (dimension);
6812 static struct window *
6813 set_window_margins (struct window *w, Lisp_Object left_width,
6814 Lisp_Object right_width)
6816 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6817 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6818 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6820 if (w->left_margin_cols != left || w->right_margin_cols != right)
6822 /* Don't change anything if new margins won't fit. */
6823 if ((WINDOW_PIXEL_WIDTH (w)
6824 - WINDOW_FRINGES_WIDTH (w)
6825 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6826 - (left + right) * unit)
6827 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6829 w->left_margin_cols = left;
6830 w->right_margin_cols = right;
6832 return w;
6834 else
6835 return NULL;
6837 else
6838 return NULL;
6841 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6842 2, 3, 0,
6843 doc: /* Set width of marginal areas of window WINDOW.
6844 WINDOW must be a live window and defaults to the selected one.
6846 Second arg LEFT-WIDTH specifies the number of character cells to
6847 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6848 does the same for the right marginal area. A nil width parameter
6849 means no margin.
6851 Return t if any margin was actually changed and nil otherwise. */)
6852 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6854 struct window *w = set_window_margins (decode_live_window (window),
6855 left_width, right_width);
6856 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6860 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6861 0, 1, 0,
6862 doc: /* Get width of marginal areas of window WINDOW.
6863 WINDOW must be a live window and defaults to the selected one.
6865 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6866 If a marginal area does not exist, its width will be returned
6867 as nil. */)
6868 (Lisp_Object window)
6870 struct window *w = decode_live_window (window);
6871 return Fcons (w->left_margin_cols
6872 ? make_number (w->left_margin_cols) : Qnil,
6873 w->right_margin_cols
6874 ? make_number (w->right_margin_cols) : Qnil);
6879 /***********************************************************************
6880 Fringes
6881 ***********************************************************************/
6883 static struct window *
6884 set_window_fringes (struct window *w, Lisp_Object left_width,
6885 Lisp_Object right_width, Lisp_Object outside_margins)
6887 bool outside = !NILP (outside_margins);
6888 int left = extract_dimension (left_width);
6889 int right = extract_dimension (right_width);
6891 /* Do nothing on a tty or if nothing to actually change. */
6892 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6893 && (w->left_fringe_width != left
6894 || w->right_fringe_width != right
6895 || w->fringes_outside_margins != outside))
6897 if (left > 0 || right > 0)
6899 /* Don't change anything if new fringes don't fit. */
6900 if ((WINDOW_PIXEL_WIDTH (w)
6901 - WINDOW_MARGINS_WIDTH (w)
6902 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6903 - max (left, 0) - max (right, 0))
6904 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6905 return NULL;
6908 w->left_fringe_width = left;
6909 w->right_fringe_width = right;
6910 w->fringes_outside_margins = outside;
6912 return w;
6914 else
6915 return NULL;
6918 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6919 2, 4, 0,
6920 doc: /* Set the fringe widths of window WINDOW.
6921 WINDOW must be a live window and defaults to the selected one.
6923 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6924 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6925 fringe width. If a fringe width arg is nil, that means to use the
6926 frame's default fringe width. Default fringe widths can be set with
6927 the command `set-fringe-style'.
6928 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6929 outside of the display margins. By default, fringes are drawn between
6930 display marginal areas and the text area.
6932 Return t if any fringe was actually changed and nil otherwise. */)
6933 (Lisp_Object window, Lisp_Object left_width,
6934 Lisp_Object right_width, Lisp_Object outside_margins)
6936 struct window *w
6937 = set_window_fringes (decode_live_window (window),
6938 left_width, right_width, outside_margins);
6939 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6943 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6944 0, 1, 0,
6945 doc: /* Get width of fringes of window WINDOW.
6946 WINDOW must be a live window and defaults to the selected one.
6948 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6949 (Lisp_Object window)
6951 struct window *w = decode_live_window (window);
6953 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6954 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6955 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6960 /***********************************************************************
6961 Scroll bars
6962 ***********************************************************************/
6964 static struct window *
6965 set_window_scroll_bars (struct window *w, Lisp_Object width,
6966 Lisp_Object vertical_type, Lisp_Object height,
6967 Lisp_Object horizontal_type)
6969 int iwidth = extract_dimension (width);
6970 bool changed = false;
6972 if (iwidth == 0)
6973 vertical_type = Qnil;
6975 if (!(NILP (vertical_type)
6976 || EQ (vertical_type, Qleft)
6977 || EQ (vertical_type, Qright)
6978 || EQ (vertical_type, Qt)))
6979 error ("Invalid type of vertical scroll bar");
6981 if (w->scroll_bar_width != iwidth
6982 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6984 /* Don't change anything if new scroll bar won't fit. */
6985 if ((WINDOW_PIXEL_WIDTH (w)
6986 - WINDOW_MARGINS_WIDTH (w)
6987 - WINDOW_FRINGES_WIDTH (w)
6988 - max (iwidth, 0))
6989 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6991 w->scroll_bar_width = iwidth;
6992 wset_vertical_scroll_bar_type (w, vertical_type);
6993 changed = true;
6997 #if USE_HORIZONTAL_SCROLL_BARS
6999 int iheight = extract_dimension (height);
7001 if (MINI_WINDOW_P (w) || iheight == 0)
7002 horizontal_type = Qnil;
7004 if (!(NILP (horizontal_type)
7005 || EQ (horizontal_type, Qbottom)
7006 || EQ (horizontal_type, Qt)))
7007 error ("Invalid type of horizontal scroll bar");
7009 if (w->scroll_bar_height != iheight
7010 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
7012 /* Don't change anything if new scroll bar won't fit. */
7013 if ((WINDOW_PIXEL_HEIGHT (w)
7014 - WINDOW_HEADER_LINE_HEIGHT (w)
7015 - WINDOW_MODE_LINE_HEIGHT (w)
7016 - max (iheight, 0))
7017 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7019 w->scroll_bar_height = iheight;
7020 wset_horizontal_scroll_bar_type (w, horizontal_type);
7021 changed = true;
7025 #else
7026 wset_horizontal_scroll_bar_type (w, Qnil);
7027 #endif
7029 return changed ? w : NULL;
7032 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7033 Sset_window_scroll_bars, 1, 5, 0,
7034 doc: /* Set width and type of scroll bars of window WINDOW.
7035 WINDOW must be a live window and defaults to the selected one.
7037 Second parameter WIDTH specifies the pixel width for the vertical scroll
7038 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
7039 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7040 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
7041 frame's scroll-bar type.
7043 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7044 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
7045 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7046 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
7047 means to use the frame's horizontal scroll-bar type.
7049 Return t if scroll bars were actually changed and nil otherwise. */)
7050 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7051 Lisp_Object height, Lisp_Object horizontal_type)
7053 struct window *w
7054 = set_window_scroll_bars (decode_live_window (window),
7055 width, vertical_type, height, horizontal_type);
7056 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7060 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7061 0, 1, 0,
7062 doc: /* Get width and type of scroll bars of window WINDOW.
7063 WINDOW must be a live window and defaults to the selected one.
7065 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7066 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7067 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7068 value. */)
7069 (Lisp_Object window)
7071 struct window *w = decode_live_window (window);
7073 return Fcons (((w->scroll_bar_width >= 0)
7074 ? make_number (w->scroll_bar_width)
7075 : Qnil),
7076 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7077 w->vertical_scroll_bar_type,
7078 ((w->scroll_bar_height >= 0)
7079 ? make_number (w->scroll_bar_height)
7080 : Qnil),
7081 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7082 w->horizontal_scroll_bar_type));
7085 /***********************************************************************
7086 Smooth scrolling
7087 ***********************************************************************/
7089 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7090 doc: /* Return the amount by which WINDOW is scrolled vertically.
7091 If WINDOW is omitted or nil, it defaults to the selected window.
7092 Normally, value is a multiple of the canonical character height of WINDOW;
7093 optional second arg PIXELS-P means value is measured in pixels. */)
7094 (Lisp_Object window, Lisp_Object pixels_p)
7096 Lisp_Object result;
7097 struct window *w = decode_live_window (window);
7098 struct frame *f = XFRAME (w->frame);
7100 if (FRAME_WINDOW_P (f))
7101 result = (NILP (pixels_p)
7102 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7103 : make_number (-w->vscroll));
7104 else
7105 result = make_number (0);
7106 return result;
7110 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7111 2, 3, 0,
7112 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7113 WINDOW nil means use the selected window. Normally, VSCROLL is a
7114 non-negative multiple of the canonical character height of WINDOW;
7115 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7116 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7117 corresponds to an integral number of pixels. The return value is the
7118 result of this rounding.
7119 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7120 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7122 struct window *w = decode_live_window (window);
7123 struct frame *f = XFRAME (w->frame);
7125 CHECK_NUMBER_OR_FLOAT (vscroll);
7127 if (FRAME_WINDOW_P (f))
7129 int old_dy = w->vscroll;
7131 w->vscroll = - (NILP (pixels_p)
7132 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7133 : XFLOATINT (vscroll));
7134 w->vscroll = min (w->vscroll, 0);
7136 if (w->vscroll != old_dy)
7138 /* Adjust glyph matrix of the frame if the virtual display
7139 area becomes larger than before. */
7140 if (w->vscroll < 0 && w->vscroll < old_dy)
7141 adjust_frame_glyphs (f);
7143 /* Prevent redisplay shortcuts. */
7144 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7148 return Fwindow_vscroll (window, pixels_p);
7152 /* Call FN for all leaf windows on frame F. FN is called with the
7153 first argument being a pointer to the leaf window, and with
7154 additional argument USER_DATA. Stops when FN returns 0. */
7156 static void
7157 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7158 void *user_data)
7160 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7161 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7162 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7166 /* Helper function for foreach_window. Call FN for all leaf windows
7167 reachable from W. FN is called with the first argument being a
7168 pointer to the leaf window, and with additional argument USER_DATA.
7169 Stop when FN returns false. Value is false if stopped by FN. */
7171 static bool
7172 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7173 void *user_data)
7175 bool cont;
7177 for (cont = true; w && cont;)
7179 if (WINDOWP (w->contents))
7180 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7181 else
7182 cont = fn (w, user_data);
7184 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7187 return cont;
7190 /***********************************************************************
7191 Initialization
7192 ***********************************************************************/
7194 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7195 describe the same state of affairs. This is used by Fequal.
7197 IGNORE_POSITIONS means ignore non-matching scroll positions
7198 and the like.
7200 This ignores a couple of things like the dedication status of
7201 window, combination_limit and the like. This might have to be
7202 fixed. */
7204 bool
7205 compare_window_configurations (Lisp_Object configuration1,
7206 Lisp_Object configuration2,
7207 bool ignore_positions)
7209 register struct save_window_data *d1, *d2;
7210 struct Lisp_Vector *sws1, *sws2;
7211 ptrdiff_t i;
7213 CHECK_WINDOW_CONFIGURATION (configuration1);
7214 CHECK_WINDOW_CONFIGURATION (configuration2);
7216 d1 = (struct save_window_data *) XVECTOR (configuration1);
7217 d2 = (struct save_window_data *) XVECTOR (configuration2);
7218 sws1 = XVECTOR (d1->saved_windows);
7219 sws2 = XVECTOR (d2->saved_windows);
7221 /* Frame settings must match. */
7222 if (d1->frame_cols != d2->frame_cols
7223 || d1->frame_lines != d2->frame_lines
7224 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7225 || !EQ (d1->selected_frame, d2->selected_frame)
7226 || !EQ (d1->f_current_buffer, d2->f_current_buffer)
7227 || (!ignore_positions
7228 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7229 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7230 || !EQ (d1->focus_frame, d2->focus_frame)
7231 /* Verify that the two configurations have the same number of windows. */
7232 || sws1->header.size != sws2->header.size)
7233 return false;
7235 for (i = 0; i < sws1->header.size; i++)
7237 struct saved_window *sw1, *sw2;
7239 sw1 = SAVED_WINDOW_N (sws1, i);
7240 sw2 = SAVED_WINDOW_N (sws2, i);
7242 if (
7243 /* The "current" windows in the two configurations must
7244 correspond to each other. */
7245 EQ (d1->current_window, sw1->window)
7246 != EQ (d2->current_window, sw2->window)
7247 /* Windows' buffers must match. */
7248 || !EQ (sw1->buffer, sw2->buffer)
7249 || !EQ (sw1->pixel_left, sw2->pixel_left)
7250 || !EQ (sw1->pixel_top, sw2->pixel_top)
7251 || !EQ (sw1->pixel_height, sw2->pixel_height)
7252 || !EQ (sw1->pixel_width, sw2->pixel_width)
7253 || !EQ (sw1->left_col, sw2->left_col)
7254 || !EQ (sw1->top_line, sw2->top_line)
7255 || !EQ (sw1->total_cols, sw2->total_cols)
7256 || !EQ (sw1->total_lines, sw2->total_lines)
7257 || !EQ (sw1->display_table, sw2->display_table)
7258 /* The next two disjuncts check the window structure for
7259 equality. */
7260 || !EQ (sw1->parent, sw2->parent)
7261 || !EQ (sw1->prev, sw2->prev)
7262 || (!ignore_positions
7263 && (!EQ (sw1->hscroll, sw2->hscroll)
7264 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7265 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7266 || NILP (Fequal (sw1->start, sw2->start))
7267 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7268 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7269 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7270 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7271 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7272 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7273 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7274 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7275 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7276 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7277 return false;
7280 return true;
7283 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7284 Scompare_window_configurations, 2, 2, 0,
7285 doc: /* Compare two window configurations as regards the structure of windows.
7286 This function ignores details such as the values of point
7287 and scrolling positions. */)
7288 (Lisp_Object x, Lisp_Object y)
7290 if (compare_window_configurations (x, y, true))
7291 return Qt;
7292 return Qnil;
7295 void
7296 init_window_once (void)
7298 struct frame *f = make_initial_frame ();
7299 XSETFRAME (selected_frame, f);
7300 Vterminal_frame = selected_frame;
7301 minibuf_window = f->minibuffer_window;
7302 selected_window = f->selected_window;
7305 void
7306 init_window (void)
7308 Vwindow_list = Qnil;
7311 void
7312 syms_of_window (void)
7314 DEFSYM (Qscroll_up, "scroll-up");
7315 DEFSYM (Qscroll_down, "scroll-down");
7316 DEFSYM (Qscroll_command, "scroll-command");
7318 Fput (Qscroll_up, Qscroll_command, Qt);
7319 Fput (Qscroll_down, Qscroll_command, Qt);
7321 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7322 DEFSYM (Qwindowp, "windowp");
7323 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7324 DEFSYM (Qwindow_live_p, "window-live-p");
7325 DEFSYM (Qwindow_valid_p, "window-valid-p");
7326 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7327 DEFSYM (Qdelete_window, "delete-window");
7328 DEFSYM (Qwindow__resize_root_window, "window--resize-root-window");
7329 DEFSYM (Qwindow__resize_root_window_vertically,
7330 "window--resize-root-window-vertically");
7331 DEFSYM (Qwindow__sanitize_window_sizes, "window--sanitize-window-sizes");
7332 DEFSYM (Qwindow__pixel_to_total, "window--pixel-to-total");
7333 DEFSYM (Qsafe, "safe");
7334 DEFSYM (Qdisplay_buffer, "display-buffer");
7335 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7336 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7337 DEFSYM (Qget_mru_window, "get-mru-window");
7338 DEFSYM (Qwindow_size, "window-size");
7339 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7340 DEFSYM (Qabove, "above");
7341 DEFSYM (Qclone_of, "clone-of");
7342 DEFSYM (Qfloor, "floor");
7343 DEFSYM (Qceiling, "ceiling");
7345 staticpro (&Vwindow_list);
7347 minibuf_selected_window = Qnil;
7348 staticpro (&minibuf_selected_window);
7350 window_scroll_pixel_based_preserve_x = -1;
7351 window_scroll_pixel_based_preserve_y = -1;
7352 window_scroll_preserve_hpos = -1;
7353 window_scroll_preserve_vpos = -1;
7355 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7356 doc: /* Non-nil means call as function to display a help buffer.
7357 The function is called with one argument, the buffer to be displayed.
7358 Used by `with-output-to-temp-buffer'.
7359 If this function is used, then it must do the entire job of showing
7360 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7361 Vtemp_buffer_show_function = Qnil;
7363 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7364 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7365 Vminibuf_scroll_window = Qnil;
7367 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7368 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7369 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7370 is displayed in the `mode-line' face. */);
7371 mode_line_in_non_selected_windows = true;
7373 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7374 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7375 Vother_window_scroll_buffer = Qnil;
7377 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7378 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7379 auto_window_vscroll_p = true;
7381 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7382 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7383 next_screen_context_lines = 2;
7385 DEFVAR_LISP ("scroll-preserve-screen-position",
7386 Vscroll_preserve_screen_position,
7387 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7388 A value of nil means point does not keep its screen position except
7389 at the scroll margin or window boundary respectively.
7390 A value of t means point keeps its screen position if the scroll
7391 command moved it vertically out of the window, e.g. when scrolling
7392 by full screens.
7393 Any other value means point always keeps its screen position.
7394 Scroll commands should have the `scroll-command' property
7395 on their symbols to be controlled by this variable. */);
7396 Vscroll_preserve_screen_position = Qnil;
7398 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7399 doc: /* Type of marker to use for `window-point'. */);
7400 Vwindow_point_insertion_type = Qnil;
7401 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7403 DEFVAR_LISP ("window-configuration-change-hook",
7404 Vwindow_configuration_change_hook,
7405 doc: /* Functions to call when window configuration changes.
7406 The buffer-local part is run once per window, with the relevant window
7407 selected; while the global part is run only once for the modified frame,
7408 with the relevant frame selected. */);
7409 Vwindow_configuration_change_hook = Qnil;
7411 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
7412 doc: /* Functions called during redisplay, if window sizes have changed.
7413 The value should be a list of functions that take one argument.
7414 During the first part of redisplay, for each frame, if any of its windows
7415 have changed size since the last redisplay, or have been split or deleted,
7416 all the functions in the list are called, with the frame as argument.
7417 If redisplay decides to resize the minibuffer window, it calls these
7418 functions on behalf of that as well. */);
7419 Vwindow_size_change_functions = Qnil;
7421 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7422 doc: /* Non-nil means `recenter' redraws entire frame.
7423 If this option is non-nil, then the `recenter' command with a nil
7424 argument will redraw the entire frame; the special value `tty' causes
7425 the frame to be redrawn only if it is a tty frame. */);
7426 Vrecenter_redisplay = Qtty;
7428 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7429 doc: /* If t, resize window combinations proportionally.
7430 If this variable is nil, splitting a window gets the entire screen space
7431 for displaying the new window from the window to split. Deleting and
7432 resizing a window preferably resizes one adjacent window only.
7434 If this variable is t, splitting a window tries to get the space
7435 proportionally from all windows in the same combination. This also
7436 allows splitting a window that is otherwise too small or of fixed size.
7437 Resizing and deleting a window proportionally resize all windows in the
7438 same combination.
7440 Other values are reserved for future use.
7442 A specific split operation may ignore the value of this variable if it
7443 is affected by a non-nil value of `window-combination-limit'. */);
7444 Vwindow_combination_resize = Qnil;
7446 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7447 doc: /* If non-nil, splitting a window makes a new parent window.
7448 The following values are recognized:
7450 nil means splitting a window will create a new parent window only if the
7451 window has no parent window or the window shall become part of a
7452 combination orthogonal to the one it is part of.
7454 `window-size' means that splitting a window for displaying a buffer
7455 makes a new parent window provided `display-buffer' is supposed to
7456 explicitly set the window's size due to the presence of a
7457 `window-height' or `window-width' entry in the alist used by
7458 `display-buffer'. Otherwise, this value is handled like nil.
7460 `temp-buffer' means that splitting a window for displaying a temporary
7461 buffer always makes a new parent window. Otherwise, this value is
7462 handled like nil.
7464 `display-buffer' means that splitting a window for displaying a buffer
7465 always makes a new parent window. Since temporary buffers are
7466 displayed by the function `display-buffer', this value is stronger
7467 than `temp-buffer'. Splitting a window for other purpose makes a
7468 new parent window only if needed.
7470 t means that splitting a window always creates a new parent window. If
7471 all splits behave this way, each frame's window tree is a binary
7472 tree and every window but the frame's root window has exactly one
7473 sibling.
7475 Other values are reserved for future use. */);
7476 Vwindow_combination_limit = Qwindow_size;
7478 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7479 doc: /* Alist of persistent window parameters.
7480 This alist specifies which window parameters shall get saved by
7481 `current-window-configuration' and `window-state-get' and subsequently
7482 restored to their previous values by `set-window-configuration' and
7483 `window-state-put'.
7485 The car of each entry of this alist is the symbol specifying the
7486 parameter. The cdr is one of the following:
7488 nil means the parameter is neither saved by `window-state-get' nor by
7489 `current-window-configuration'.
7491 t means the parameter is saved by `current-window-configuration' and,
7492 provided its WRITABLE argument is nil, by `window-state-get'.
7494 The symbol `writable' means the parameter is saved unconditionally by
7495 both `current-window-configuration' and `window-state-get'. Do not use
7496 this value for parameters without read syntax (like windows or frames).
7498 Parameters not saved by `current-window-configuration' or
7499 `window-state-get' are left alone by `set-window-configuration'
7500 respectively are not installed by `window-state-put'. */);
7501 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7503 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7504 doc: /* Non-nil means resize windows pixelwise.
7505 This currently affects the functions: `split-window', `maximize-window',
7506 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7507 all functions that symmetrically resize a parent window.
7509 Note that when a frame's pixel size is not a multiple of the
7510 frame's character size, at least one window may get resized
7511 pixelwise even if this option is nil. */);
7512 window_resize_pixelwise = false;
7514 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7515 Vfast_but_imprecise_scrolling,
7516 doc: /* When non-nil, accelerate scrolling operations.
7517 This comes into play when scrolling rapidly over previously
7518 unfontified buffer regions. Only those portions of the buffer which
7519 are actually going to be displayed get fontified.
7521 Note that this optimization can cause the portion of the buffer
7522 displayed after a scrolling operation to be somewhat inaccurate. */);
7523 Vfast_but_imprecise_scrolling = false;
7525 defsubr (&Sselected_window);
7526 defsubr (&Sminibuffer_window);
7527 defsubr (&Swindow_minibuffer_p);
7528 defsubr (&Swindowp);
7529 defsubr (&Swindow_valid_p);
7530 defsubr (&Swindow_live_p);
7531 defsubr (&Swindow_frame);
7532 defsubr (&Sframe_root_window);
7533 defsubr (&Sframe_first_window);
7534 defsubr (&Sframe_selected_window);
7535 defsubr (&Sset_frame_selected_window);
7536 defsubr (&Spos_visible_in_window_p);
7537 defsubr (&Swindow_line_height);
7538 defsubr (&Swindow_buffer);
7539 defsubr (&Swindow_parent);
7540 defsubr (&Swindow_top_child);
7541 defsubr (&Swindow_left_child);
7542 defsubr (&Swindow_next_sibling);
7543 defsubr (&Swindow_prev_sibling);
7544 defsubr (&Swindow_combination_limit);
7545 defsubr (&Sset_window_combination_limit);
7546 defsubr (&Swindow_use_time);
7547 defsubr (&Swindow_pixel_width);
7548 defsubr (&Swindow_pixel_height);
7549 defsubr (&Swindow_pixel_width_before_size_change);
7550 defsubr (&Swindow_pixel_height_before_size_change);
7551 defsubr (&Swindow_total_width);
7552 defsubr (&Swindow_total_height);
7553 defsubr (&Swindow_normal_size);
7554 defsubr (&Swindow_new_pixel);
7555 defsubr (&Swindow_new_total);
7556 defsubr (&Swindow_new_normal);
7557 defsubr (&Swindow_pixel_left);
7558 defsubr (&Swindow_pixel_top);
7559 defsubr (&Swindow_left_column);
7560 defsubr (&Swindow_top_line);
7561 defsubr (&Sset_window_new_pixel);
7562 defsubr (&Sset_window_new_total);
7563 defsubr (&Sset_window_new_normal);
7564 defsubr (&Swindow_resize_apply);
7565 defsubr (&Swindow_resize_apply_total);
7566 defsubr (&Swindow_body_height);
7567 defsubr (&Swindow_body_width);
7568 defsubr (&Swindow_hscroll);
7569 defsubr (&Sset_window_hscroll);
7570 defsubr (&Swindow_redisplay_end_trigger);
7571 defsubr (&Sset_window_redisplay_end_trigger);
7572 defsubr (&Swindow_mode_line_height);
7573 defsubr (&Swindow_header_line_height);
7574 defsubr (&Swindow_right_divider_width);
7575 defsubr (&Swindow_bottom_divider_width);
7576 defsubr (&Swindow_scroll_bar_width);
7577 defsubr (&Swindow_scroll_bar_height);
7578 defsubr (&Scoordinates_in_window_p);
7579 defsubr (&Swindow_at);
7580 defsubr (&Swindow_point);
7581 defsubr (&Swindow_old_point);
7582 defsubr (&Swindow_start);
7583 defsubr (&Swindow_end);
7584 defsubr (&Sset_window_point);
7585 defsubr (&Sset_window_start);
7586 defsubr (&Swindow_dedicated_p);
7587 defsubr (&Sset_window_dedicated_p);
7588 defsubr (&Swindow_display_table);
7589 defsubr (&Sset_window_display_table);
7590 defsubr (&Snext_window);
7591 defsubr (&Sprevious_window);
7592 defsubr (&Sget_buffer_window);
7593 defsubr (&Sdelete_other_windows_internal);
7594 defsubr (&Sdelete_window_internal);
7595 defsubr (&Sresize_mini_window_internal);
7596 defsubr (&Sset_window_buffer);
7597 defsubr (&Srun_window_configuration_change_hook);
7598 defsubr (&Srun_window_scroll_functions);
7599 defsubr (&Sselect_window);
7600 defsubr (&Sforce_window_update);
7601 defsubr (&Ssplit_window_internal);
7602 defsubr (&Sscroll_up);
7603 defsubr (&Sscroll_down);
7604 defsubr (&Sscroll_left);
7605 defsubr (&Sscroll_right);
7606 defsubr (&Sother_window_for_scrolling);
7607 defsubr (&Sscroll_other_window);
7608 defsubr (&Sminibuffer_selected_window);
7609 defsubr (&Srecenter);
7610 defsubr (&Swindow_text_width);
7611 defsubr (&Swindow_text_height);
7612 defsubr (&Smove_to_window_line);
7613 defsubr (&Swindow_configuration_p);
7614 defsubr (&Swindow_configuration_frame);
7615 defsubr (&Sset_window_configuration);
7616 defsubr (&Scurrent_window_configuration);
7617 defsubr (&Sset_window_margins);
7618 defsubr (&Swindow_margins);
7619 defsubr (&Sset_window_fringes);
7620 defsubr (&Swindow_fringes);
7621 defsubr (&Sset_window_scroll_bars);
7622 defsubr (&Swindow_scroll_bars);
7623 defsubr (&Swindow_vscroll);
7624 defsubr (&Sset_window_vscroll);
7625 defsubr (&Scompare_window_configurations);
7626 defsubr (&Swindow_list);
7627 defsubr (&Swindow_list_1);
7628 defsubr (&Swindow_prev_buffers);
7629 defsubr (&Sset_window_prev_buffers);
7630 defsubr (&Swindow_next_buffers);
7631 defsubr (&Sset_window_next_buffers);
7632 defsubr (&Swindow_parameters);
7633 defsubr (&Swindow_parameter);
7634 defsubr (&Sset_window_parameter);
7637 void
7638 keys_of_window (void)
7640 initial_define_key (control_x_map, '<', "scroll-left");
7641 initial_define_key (control_x_map, '>', "scroll-right");
7643 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7644 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7645 initial_define_key (meta_map, 'v', "scroll-down-command");