gnus-article-read-summary-keys: Don't move point for WDD and WDW commands
[emacs.git] / src / window.c
blobbf89f0e488bb8bed62703c47d4cd979243be372e
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) || EQ (norecord, Qmark_for_redisplay))
496 { /* Mark the window for redisplay since the selected-window has
497 a different mode-line. */
498 wset_redisplay (XWINDOW (selected_window));
499 wset_redisplay (w);
501 else
502 redisplay_other_windows ();
504 sf = SELECTED_FRAME ();
505 if (XFRAME (WINDOW_FRAME (w)) != sf)
507 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
508 /* Use this rather than Fhandle_switch_frame
509 so that FRAME_FOCUS_FRAME is moved appropriately as we
510 move around in the state where a minibuffer in a separate
511 frame is active. */
512 Fselect_frame (WINDOW_FRAME (w), norecord);
513 /* Fselect_frame called us back so we've done all the work already. */
514 eassert (EQ (window, selected_window));
515 return window;
517 else
518 fset_selected_window (sf, window);
520 select_window_1 (window, inhibit_point_swap);
521 bset_last_selected_window (XBUFFER (w->contents), window);
523 record_and_return:
524 /* record_buffer can call maybe_quit, so make sure it is run only
525 after we have re-established the invariant between
526 selected_window and selected_frame, otherwise the temporary
527 broken invariant might "escape" (Bug#14161). */
528 if (NILP (norecord))
530 w->use_time = ++window_select_count;
531 record_buffer (w->contents);
534 return window;
537 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
538 (not even for redisplay's benefit), and assume that the window's frame is
539 already selected. */
540 static void
541 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
543 /* Store the old selected window's buffer's point in pointm of the old
544 selected window. It belongs to that window, and when the window is
545 not selected, must be in the window. */
546 if (!inhibit_point_swap)
548 struct window *ow = XWINDOW (selected_window);
549 if (BUFFERP (ow->contents))
550 set_marker_both (ow->pointm, ow->contents,
551 BUF_PT (XBUFFER (ow->contents)),
552 BUF_PT_BYTE (XBUFFER (ow->contents)));
555 selected_window = window;
557 /* Go to the point recorded in the window.
558 This is important when the buffer is in more
559 than one window. It also matters when
560 redisplay_window has altered point after scrolling,
561 because it makes the change only in the window. */
562 set_point_from_marker (XWINDOW (window)->pointm);
565 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
566 doc: /* Select WINDOW which must be a live window.
567 Also make WINDOW's frame the selected frame and WINDOW that frame's
568 selected window. In addition, make WINDOW's buffer current and set its
569 buffer's value of `point' to the value of WINDOW's `window-point'.
570 Return WINDOW.
572 Optional second arg NORECORD non-nil means do not put this buffer at the
573 front of the buffer list and do not make this window the most recently
574 selected one. Also, do not mark WINDOW for redisplay unless NORECORD
575 equals the special symbol `mark-for-redisplay'.
577 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
578 applications and internal routines often select a window temporarily for
579 various purposes; mostly, to simplify coding. As a rule, such
580 selections should be not recorded and therefore will not pollute
581 `buffer-list-update-hook'. Selections that "really count" are those
582 causing a visible change in the next redisplay of WINDOW's frame and
583 should be always recorded. So if you think of running a function each
584 time a window gets selected put it on `buffer-list-update-hook'.
586 Also note that the main editor command loop sets the current buffer to
587 the buffer of the selected window before each command. */)
588 (Lisp_Object window, Lisp_Object norecord)
590 return select_window (window, norecord, false);
593 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
594 doc: /* Return the buffer displayed in window WINDOW.
595 If WINDOW is omitted or nil, it defaults to the selected window.
596 Return nil for an internal window or a deleted window. */)
597 (Lisp_Object window)
599 struct window *w = decode_any_window (window);
600 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
603 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
604 doc: /* Return the parent window of window WINDOW.
605 WINDOW must be a valid window and defaults to the selected one.
606 Return nil for a window with no parent (e.g. a root window). */)
607 (Lisp_Object window)
609 return decode_valid_window (window)->parent;
612 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
613 doc: /* Return the topmost child window of window WINDOW.
614 WINDOW must be a valid window and defaults to the selected one.
615 Return nil if WINDOW is a live window (live windows have no children).
616 Return nil if WINDOW is an internal window whose children form a
617 horizontal combination. */)
618 (Lisp_Object window)
620 struct window *w = decode_valid_window (window);
621 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
624 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
625 doc: /* Return the leftmost child window of window WINDOW.
626 WINDOW must be a valid window and defaults to the selected one.
627 Return nil if WINDOW is a live window (live windows have no children).
628 Return nil if WINDOW is an internal window whose children form a
629 vertical combination. */)
630 (Lisp_Object window)
632 struct window *w = decode_valid_window (window);
633 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
636 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
637 doc: /* Return the next sibling window of window WINDOW.
638 WINDOW must be a valid window and defaults to the selected one.
639 Return nil if WINDOW has no next sibling. */)
640 (Lisp_Object window)
642 return decode_valid_window (window)->next;
645 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
646 doc: /* Return the previous sibling window of window WINDOW.
647 WINDOW must be a valid window and defaults to the selected one.
648 Return nil if WINDOW has no previous sibling. */)
649 (Lisp_Object window)
651 return decode_valid_window (window)->prev;
654 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
655 doc: /* Return combination limit of window WINDOW.
656 WINDOW must be a valid window used in horizontal or vertical combination.
657 If the return value is nil, child windows of WINDOW can be recombined with
658 WINDOW's siblings. A return value of t means that child windows of
659 WINDOW are never (re-)combined with WINDOW's siblings. */)
660 (Lisp_Object window)
662 struct window *w;
664 CHECK_VALID_WINDOW (window);
665 w = XWINDOW (window);
666 if (WINDOW_LEAF_P (w))
667 error ("Combination limit is meaningful for internal windows only");
668 return w->combination_limit;
671 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
672 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
673 WINDOW must be a valid window used in horizontal or vertical combination.
674 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
675 siblings. LIMIT t means that child windows of WINDOW are never
676 \(re-)combined with WINDOW's siblings. Other values are reserved for
677 future use. */)
678 (Lisp_Object window, Lisp_Object limit)
680 struct window *w;
682 CHECK_VALID_WINDOW (window);
683 w = XWINDOW (window);
684 if (WINDOW_LEAF_P (w))
685 error ("Combination limit is meaningful for internal windows only");
686 wset_combination_limit (w, limit);
687 return limit;
690 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
691 doc: /* Return the use time of window WINDOW.
692 WINDOW must be a live window and defaults to the selected one.
693 The window with the highest use time is the most recently selected
694 one. The window with the lowest use time is the least recently
695 selected one. */)
696 (Lisp_Object window)
698 return make_number (decode_live_window (window)->use_time);
701 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
702 doc: /* Return the width of window WINDOW in pixels.
703 WINDOW must be a valid window and defaults to the selected one.
705 The return value includes the fringes and margins of WINDOW as well as
706 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
707 an internal window, its pixel width is the width of the screen areas
708 spanned by its children. */)
709 (Lisp_Object window)
711 return make_number (decode_valid_window (window)->pixel_width);
714 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
715 doc: /* Return the height of window WINDOW in pixels.
716 WINDOW must be a valid window and defaults to the selected one.
718 The return value includes the mode line and header line and the bottom
719 divider, if any. If WINDOW is an internal window, its pixel height is
720 the height of the screen areas spanned by its children. */)
721 (Lisp_Object window)
723 return make_number (decode_valid_window (window)->pixel_height);
726 DEFUN ("window-pixel-width-before-size-change",
727 Fwindow_pixel_width_before_size_change,
728 Swindow_pixel_width_before_size_change, 0, 1, 0,
729 doc: /* Return pixel width of window WINDOW before last size changes.
730 WINDOW must be a valid window and defaults to the selected one.
732 The return value is the pixel width of WINDOW at the last time
733 `window-size-change-functions' was run. It's zero if WINDOW was made
734 after that. */)
735 (Lisp_Object window)
737 return (make_number
738 (decode_valid_window (window)->pixel_width_before_size_change));
741 DEFUN ("window-pixel-height-before-size-change",
742 Fwindow_pixel_height_before_size_change,
743 Swindow_pixel_height_before_size_change, 0, 1, 0,
744 doc: /* Return pixel height of window WINDOW before last size changes.
745 WINDOW must be a valid window and defaults to the selected one.
747 The return value is the pixel height of WINDOW at the last time
748 `window-size-change-functions' was run. It's zero if WINDOW was made
749 after that. */)
750 (Lisp_Object window)
752 return (make_number
753 (decode_valid_window (window)->pixel_height_before_size_change));
756 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
757 doc: /* Return the height of window WINDOW in lines.
758 WINDOW must be a valid window and defaults to the selected one.
760 The return value includes the heights of WINDOW's mode and header line
761 and its bottom divider, if any. If WINDOW is an internal window, the
762 total height is the height of the screen areas spanned by its children.
764 If WINDOW's pixel height is not an integral multiple of its frame's
765 character height, the number of lines occupied by WINDOW is rounded
766 internally. This is done in a way such that, if WINDOW is a parent
767 window, the sum of the total heights of all its children internally
768 equals the total height of WINDOW.
770 If the optional argument ROUND is `ceiling', return the smallest integer
771 larger than WINDOW's pixel height divided by the character height of
772 WINDOW's frame. ROUND `floor' means to return the largest integer
773 smaller than WINDOW's pixel height divided by the character height of
774 WINDOW's frame. Any other value of ROUND means to return the internal
775 total height of WINDOW. */)
776 (Lisp_Object window, Lisp_Object round)
778 struct window *w = decode_valid_window (window);
780 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
781 return make_number (w->total_lines);
782 else
784 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
786 return make_number (EQ (round, Qceiling)
787 ? ((w->pixel_height + unit - 1) /unit)
788 : (w->pixel_height / unit));
792 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
793 doc: /* Return the total width of window WINDOW in columns.
794 WINDOW must be a valid window and defaults to the selected one.
796 The return value includes the widths of WINDOW's fringes, margins,
797 scroll bars and its right divider, if any. If WINDOW is an internal
798 window, the total width is the width of the screen areas spanned by its
799 children.
801 If WINDOW's pixel width is not an integral multiple of its frame's
802 character width, the number of lines occupied by WINDOW is rounded
803 internally. This is done in a way such that, if WINDOW is a parent
804 window, the sum of the total widths of all its children internally
805 equals the total width of WINDOW.
807 If the optional argument ROUND is `ceiling', return the smallest integer
808 larger than WINDOW's pixel width divided by the character width of
809 WINDOW's frame. ROUND `floor' means to return the largest integer
810 smaller than WINDOW's pixel width divided by the character width of
811 WINDOW's frame. Any other value of ROUND means to return the internal
812 total width of WINDOW. */)
813 (Lisp_Object window, Lisp_Object round)
815 struct window *w = decode_valid_window (window);
817 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
818 return make_number (w->total_cols);
819 else
821 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
823 return make_number (EQ (round, Qceiling)
824 ? ((w->pixel_width + unit - 1) /unit)
825 : (w->pixel_width / unit));
829 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
830 doc: /* Return the new total size of window WINDOW.
831 WINDOW must be a valid window and defaults to the selected one.
833 The new total size of WINDOW is the value set by the last call of
834 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
835 installed as WINDOW's total height (see `window-total-height') or total
836 width (see `window-total-width'). */)
837 (Lisp_Object window)
839 return decode_valid_window (window)->new_total;
842 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
843 doc: /* Return the normal height of window WINDOW.
844 WINDOW must be a valid window and defaults to the selected one.
845 If HORIZONTAL is non-nil, return the normal width of WINDOW.
847 The normal height of a frame's root window or a window that is
848 horizontally combined (a window that has a left or right sibling) is
849 1.0. The normal height of a window that is vertically combined (has a
850 sibling above or below) is the fraction of the window's height with
851 respect to its parent. The sum of the normal heights of all windows in a
852 vertical combination equals 1.0.
854 Similarly, the normal width of a frame's root window or a window that is
855 vertically combined equals 1.0. The normal width of a window that is
856 horizontally combined is the fraction of the window's width with respect
857 to its parent. The sum of the normal widths of all windows in a
858 horizontal combination equals 1.0.
860 The normal sizes of windows are used to restore the proportional sizes
861 of windows after they have been shrunk to their minimum sizes; for
862 example when a frame is temporarily made very small and afterwards gets
863 re-enlarged to its previous size. */)
864 (Lisp_Object window, Lisp_Object horizontal)
866 struct window *w = decode_valid_window (window);
868 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
871 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
872 doc: /* Return new normal size of window WINDOW.
873 WINDOW must be a valid window and defaults to the selected one.
875 The new normal size of WINDOW is the value set by the last call of
876 `set-window-new-normal' for WINDOW. If valid, it will be shortly
877 installed as WINDOW's normal size (see `window-normal-size'). */)
878 (Lisp_Object window)
880 return decode_valid_window (window)->new_normal;
883 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
884 doc: /* Return new pixel size of window WINDOW.
885 WINDOW must be a valid window and defaults to the selected one.
887 The new pixel size of WINDOW is the value set by the last call of
888 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
889 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
890 width (see `window-pixel-width'). */)
891 (Lisp_Object window)
893 return decode_valid_window (window)->new_pixel;
896 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
897 doc: /* Return left pixel edge of window WINDOW.
898 WINDOW must be a valid window and defaults to the selected one. */)
899 (Lisp_Object window)
901 return make_number (decode_valid_window (window)->pixel_left);
904 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
905 doc: /* Return top pixel edge of window WINDOW.
906 WINDOW must be a valid window and defaults to the selected one. */)
907 (Lisp_Object window)
909 return make_number (decode_valid_window (window)->pixel_top);
912 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
913 doc: /* Return left column of window WINDOW.
914 This is the distance, in columns, between the left edge of WINDOW and
915 the left edge of the frame's window area. For instance, the return
916 value is 0 if there is no window to the left of WINDOW.
918 WINDOW must be a valid window and defaults to the selected one. */)
919 (Lisp_Object window)
921 return make_number (decode_valid_window (window)->left_col);
924 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
925 doc: /* Return top line of window WINDOW.
926 This is the distance, in lines, between the top of WINDOW and the top
927 of the frame's window area. For instance, the return value is 0 if
928 there is no window above WINDOW.
930 WINDOW must be a valid window and defaults to the selected one. */)
931 (Lisp_Object window)
933 return make_number (decode_valid_window (window)->top_line);
936 /* Return the number of lines/pixels of W's body. Don't count any mode
937 or header line or horizontal divider of W. Rounds down to nearest
938 integer when not working pixelwise. */
939 static int
940 window_body_height (struct window *w, bool pixelwise)
942 int height = (w->pixel_height
943 - WINDOW_HEADER_LINE_HEIGHT (w)
944 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
945 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
946 : 0)
947 - WINDOW_MODE_LINE_HEIGHT (w)
948 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
950 /* Don't return a negative value. */
951 return max (pixelwise
952 ? height
953 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
957 /* Return the number of columns/pixels of W's body. Don't count columns
958 occupied by the scroll bar or the divider/vertical bar separating W
959 from its right sibling or margins. On window-systems don't count
960 fringes either. Round down to nearest integer when not working
961 pixelwise. */
963 window_body_width (struct window *w, bool pixelwise)
965 struct frame *f = XFRAME (WINDOW_FRAME (w));
967 int width = (w->pixel_width
968 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
969 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
970 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
971 : (/* A vertical bar is either 1 or 0. */
972 !FRAME_WINDOW_P (f)
973 && !WINDOW_RIGHTMOST_P (w)
974 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
975 - WINDOW_MARGINS_WIDTH (w)
976 - (FRAME_WINDOW_P (f)
977 ? WINDOW_FRINGES_WIDTH (w)
978 : 0));
980 /* Don't return a negative value. */
981 return max (pixelwise
982 ? width
983 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
987 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
988 doc: /* Return the height of WINDOW's text area.
989 WINDOW must be a live window and defaults to the selected one. Optional
990 argument PIXELWISE non-nil means return the height of WINDOW's text area
991 in pixels. The return value does not include the mode line or header
992 line or any horizontal divider.
994 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
995 pixel height divided by the character height of WINDOW's frame. This
996 means that if a line at the bottom of the text area is only partially
997 visible, that line is not counted. */)
998 (Lisp_Object window, Lisp_Object pixelwise)
1000 return make_number (window_body_height (decode_live_window (window),
1001 !NILP (pixelwise)));
1004 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
1005 doc: /* Return the width of WINDOW's text area.
1006 WINDOW must be a live window and defaults to the selected one. Optional
1007 argument PIXELWISE non-nil means return the width in pixels. The return
1008 value does not include any vertical dividers, fringes or marginal areas,
1009 or scroll bars.
1011 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
1012 pixel width divided by the character width of WINDOW's frame. This
1013 means that if a column at the right of the text area is only partially
1014 visible, that column is not counted.
1016 Note that the returned value includes the column reserved for the
1017 continuation glyph. */)
1018 (Lisp_Object window, Lisp_Object pixelwise)
1020 return make_number (window_body_width (decode_live_window (window),
1021 !NILP (pixelwise)));
1024 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
1025 Swindow_mode_line_height, 0, 1, 0,
1026 doc: /* Return the height in pixels of WINDOW's mode-line.
1027 WINDOW must be a live window and defaults to the selected one. */)
1028 (Lisp_Object window)
1030 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
1033 DEFUN ("window-header-line-height", Fwindow_header_line_height,
1034 Swindow_header_line_height, 0, 1, 0,
1035 doc: /* Return the height in pixels of WINDOW's header-line.
1036 WINDOW must be a live window and defaults to the selected one. */)
1037 (Lisp_Object window)
1039 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1042 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1043 Swindow_right_divider_width, 0, 1, 0,
1044 doc: /* Return the width in pixels of WINDOW's right divider.
1045 WINDOW must be a live window and defaults to the selected one. */)
1046 (Lisp_Object window)
1048 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1051 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1052 Swindow_bottom_divider_width, 0, 1, 0,
1053 doc: /* Return the width in pixels of WINDOW's bottom divider.
1054 WINDOW must be a live window and defaults to the selected one. */)
1055 (Lisp_Object window)
1057 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1060 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1061 Swindow_scroll_bar_width, 0, 1, 0,
1062 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1063 WINDOW must be a live window and defaults to the selected one. */)
1064 (Lisp_Object window)
1066 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1069 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1070 Swindow_scroll_bar_height, 0, 1, 0,
1071 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1072 WINDOW must be a live window and defaults to the selected one. */)
1073 (Lisp_Object window)
1075 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1078 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1079 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1080 WINDOW must be a live window and defaults to the selected one. */)
1081 (Lisp_Object window)
1083 return make_number (decode_live_window (window)->hscroll);
1086 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1087 range, returning the new amount as a fixnum. */
1088 static Lisp_Object
1089 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1091 /* Horizontal scrolling has problems with large scroll amounts.
1092 It's too slow with long lines, and even with small lines the
1093 display can be messed up. For now, though, impose only the limits
1094 required by the internal representation: horizontal scrolling must
1095 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1096 (since it's stored in a ptrdiff_t). */
1097 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1098 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1100 /* Prevent redisplay shortcuts when changing the hscroll. */
1101 if (w->hscroll != new_hscroll)
1102 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1104 w->hscroll = new_hscroll;
1105 w->suspend_auto_hscroll = true;
1107 return make_number (new_hscroll);
1110 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1111 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1112 WINDOW must be a live window and defaults to the selected one.
1113 Clip the number to a reasonable value if out of range.
1114 Return the new number. NCOL should be zero or positive.
1116 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1117 window so that the location of point moves off-window. */)
1118 (Lisp_Object window, Lisp_Object ncol)
1120 CHECK_NUMBER (ncol);
1121 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1124 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1125 Swindow_redisplay_end_trigger, 0, 1, 0,
1126 doc: /* Return WINDOW's redisplay end trigger value.
1127 WINDOW must be a live window and defaults to the selected one.
1128 See `set-window-redisplay-end-trigger' for more information. */)
1129 (Lisp_Object window)
1131 return decode_live_window (window)->redisplay_end_trigger;
1134 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1135 Sset_window_redisplay_end_trigger, 2, 2, 0,
1136 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1137 WINDOW must be a live window and defaults to the selected one. VALUE
1138 should be a buffer position (typically a marker) or nil. If it is a
1139 buffer position, then if redisplay in WINDOW reaches a position beyond
1140 VALUE, the functions in `redisplay-end-trigger-functions' are called
1141 with two arguments: WINDOW, and the end trigger value. Afterwards the
1142 end-trigger value is reset to nil. */)
1143 (register Lisp_Object window, Lisp_Object value)
1145 wset_redisplay_end_trigger (decode_live_window (window), value);
1146 return value;
1149 /* Test if the character at column X, row Y is within window W.
1150 If it is not, return ON_NOTHING;
1151 if it is on the window's vertical divider, return
1152 ON_RIGHT_DIVIDER;
1153 if it is on the window's horizontal divider, return
1154 ON_BOTTOM_DIVIDER;
1155 if it is in the window's text area, return ON_TEXT;
1156 if it is on the window's modeline, return ON_MODE_LINE;
1157 if it is on the border between the window and its right sibling,
1158 return ON_VERTICAL_BORDER;
1159 if it is on a scroll bar, return ON_SCROLL_BAR;
1160 if it is on the window's top line, return ON_HEADER_LINE;
1161 if it is in left or right fringe of the window,
1162 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1163 if it is in the marginal area to the left/right of the window,
1164 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1166 X and Y are frame relative pixel coordinates. */
1168 static enum window_part
1169 coordinates_in_window (register struct window *w, int x, int y)
1171 struct frame *f = XFRAME (WINDOW_FRAME (w));
1172 enum window_part part;
1173 int ux = FRAME_COLUMN_WIDTH (f);
1174 int left_x = WINDOW_LEFT_EDGE_X (w);
1175 int right_x = WINDOW_RIGHT_EDGE_X (w);
1176 int top_y = WINDOW_TOP_EDGE_Y (w);
1177 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1178 /* The width of the area where the vertical line can be dragged.
1179 (Between mode lines for instance. */
1180 int grabbable_width = ux;
1181 int lmargin_width, rmargin_width, text_left, text_right;
1183 /* Outside any interesting row or column? */
1184 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1185 return ON_NOTHING;
1187 /* On the horizontal window divider (which prevails the vertical
1188 divider)? */
1189 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1190 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1191 && y <= bottom_y)
1192 return ON_BOTTOM_DIVIDER;
1193 /* On vertical window divider? */
1194 else if (!WINDOW_RIGHTMOST_P (w)
1195 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1196 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1197 && x <= right_x)
1198 return ON_RIGHT_DIVIDER;
1199 /* On the horizontal scroll bar? (Including the empty space at its
1200 right!) */
1201 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1202 && y >= (bottom_y
1203 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1204 - CURRENT_MODE_LINE_HEIGHT (w)
1205 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1206 && y <= (bottom_y
1207 - CURRENT_MODE_LINE_HEIGHT (w)
1208 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1209 return ON_HORIZONTAL_SCROLL_BAR;
1210 /* On the mode or header line? */
1211 else if ((WINDOW_WANTS_MODELINE_P (w)
1212 && y >= (bottom_y
1213 - CURRENT_MODE_LINE_HEIGHT (w)
1214 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1215 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1216 && (part = ON_MODE_LINE))
1217 || (WINDOW_WANTS_HEADER_LINE_P (w)
1218 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1219 && (part = ON_HEADER_LINE)))
1221 /* If it's under/over the scroll bar portion of the mode/header
1222 line, say it's on the vertical line. That's to be able to
1223 resize windows horizontally in case we're using toolkit scroll
1224 bars. Note: If scrollbars are on the left, the window that
1225 must be eventually resized is that on the left of WINDOW. */
1226 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1227 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1228 && !WINDOW_LEFTMOST_P (w)
1229 && eabs (x - left_x) < grabbable_width)
1230 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1231 && !WINDOW_RIGHTMOST_P (w)
1232 && eabs (x - right_x) < grabbable_width)))
1233 return ON_VERTICAL_BORDER;
1234 else
1235 return part;
1238 /* In what's below, we subtract 1 when computing right_x because we
1239 want the rightmost pixel, which is given by left_pixel+width-1. */
1240 if (w->pseudo_window_p)
1242 left_x = 0;
1243 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1245 else
1247 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1248 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1251 /* Outside any interesting column? */
1252 if (x < left_x || x > right_x)
1253 return ON_VERTICAL_SCROLL_BAR;
1255 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1256 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1258 text_left = window_box_left (w, TEXT_AREA);
1259 text_right = text_left + window_box_width (w, TEXT_AREA);
1261 if (FRAME_WINDOW_P (f))
1263 if (!w->pseudo_window_p
1264 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1265 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1266 && !WINDOW_RIGHTMOST_P (w)
1267 && (eabs (x - right_x) < grabbable_width))
1268 return ON_VERTICAL_BORDER;
1270 /* Need to say "x > right_x" rather than >=, since on character
1271 terminals, the vertical line's x coordinate is right_x. */
1272 else if (!w->pseudo_window_p
1273 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1274 && !WINDOW_RIGHTMOST_P (w)
1275 /* Why check ux if we are not the rightmost window? Also
1276 shouldn't a pseudo window always be rightmost? */
1277 && x > right_x - ux)
1278 return ON_VERTICAL_BORDER;
1280 if (x < text_left)
1282 if (lmargin_width > 0
1283 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1284 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1285 : (x < left_x + lmargin_width)))
1286 return ON_LEFT_MARGIN;
1287 else
1288 return ON_LEFT_FRINGE;
1291 if (x >= text_right)
1293 if (rmargin_width > 0
1294 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1295 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1296 : (x >= right_x - rmargin_width)))
1297 return ON_RIGHT_MARGIN;
1298 else
1299 return ON_RIGHT_FRINGE;
1302 /* Everything special ruled out - must be on text area */
1303 return ON_TEXT;
1306 /* Take X is the frame-relative pixel x-coordinate, and return the
1307 x-coordinate relative to part PART of window W. */
1309 window_relative_x_coord (struct window *w, enum window_part part, int x)
1311 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1313 switch (part)
1315 case ON_TEXT:
1316 return x - window_box_left (w, TEXT_AREA);
1318 case ON_HEADER_LINE:
1319 case ON_MODE_LINE:
1320 case ON_LEFT_FRINGE:
1321 return x - left_x;
1323 case ON_RIGHT_FRINGE:
1324 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1326 case ON_LEFT_MARGIN:
1327 return (x - left_x
1328 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1329 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1331 case ON_RIGHT_MARGIN:
1332 return (x + 1
1333 - ((w->pseudo_window_p)
1334 ? WINDOW_PIXEL_WIDTH (w)
1335 : WINDOW_BOX_RIGHT_EDGE_X (w))
1336 + window_box_width (w, RIGHT_MARGIN_AREA)
1337 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1338 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1340 case ON_NOTHING:
1341 case ON_VERTICAL_BORDER:
1342 case ON_VERTICAL_SCROLL_BAR:
1343 case ON_HORIZONTAL_SCROLL_BAR:
1344 case ON_RIGHT_DIVIDER:
1345 case ON_BOTTOM_DIVIDER:
1346 return 0;
1348 default:
1349 emacs_abort ();
1354 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1355 Scoordinates_in_window_p, 2, 2, 0,
1356 doc: /* Return non-nil if COORDINATES are in WINDOW.
1357 WINDOW must be a live window and defaults to the selected one.
1358 COORDINATES is a cons of the form (X . Y), X and Y being distances
1359 measured in characters from the upper-left corner of the frame.
1360 \(0 . 0) denotes the character in the upper left corner of the
1361 frame.
1362 If COORDINATES are in the text portion of WINDOW,
1363 the coordinates relative to the window are returned.
1364 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1365 If they are in the right divider of WINDOW, `right-divider' is returned.
1366 If they are in the mode line of WINDOW, `mode-line' is returned.
1367 If they are in the header line of WINDOW, `header-line' is returned.
1368 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1369 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1370 If they are on the border between WINDOW and its right sibling,
1371 `vertical-line' is returned.
1372 If they are in the windows's left or right marginal areas, `left-margin'\n\
1373 or `right-margin' is returned. */)
1374 (register Lisp_Object coordinates, Lisp_Object window)
1376 struct window *w;
1377 struct frame *f;
1378 int x, y;
1379 Lisp_Object lx, ly;
1381 w = decode_live_window (window);
1382 f = XFRAME (w->frame);
1383 CHECK_CONS (coordinates);
1384 lx = Fcar (coordinates);
1385 ly = Fcdr (coordinates);
1386 CHECK_NUMBER_OR_FLOAT (lx);
1387 CHECK_NUMBER_OR_FLOAT (ly);
1388 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1389 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1391 switch (coordinates_in_window (w, x, y))
1393 case ON_NOTHING:
1394 return Qnil;
1396 case ON_TEXT:
1397 /* Convert X and Y to window relative pixel coordinates, and
1398 return the canonical char units. */
1399 x -= window_box_left (w, TEXT_AREA);
1400 y -= WINDOW_TOP_EDGE_Y (w);
1401 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1402 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1404 case ON_MODE_LINE:
1405 return Qmode_line;
1407 case ON_VERTICAL_BORDER:
1408 return Qvertical_line;
1410 case ON_HEADER_LINE:
1411 return Qheader_line;
1413 case ON_LEFT_FRINGE:
1414 return Qleft_fringe;
1416 case ON_RIGHT_FRINGE:
1417 return Qright_fringe;
1419 case ON_LEFT_MARGIN:
1420 return Qleft_margin;
1422 case ON_RIGHT_MARGIN:
1423 return Qright_margin;
1425 case ON_VERTICAL_SCROLL_BAR:
1426 /* Historically we are supposed to return nil in this case. */
1427 return Qnil;
1429 case ON_HORIZONTAL_SCROLL_BAR:
1430 return Qnil;
1432 case ON_RIGHT_DIVIDER:
1433 return Qright_divider;
1435 case ON_BOTTOM_DIVIDER:
1436 return Qbottom_divider;
1438 default:
1439 emacs_abort ();
1444 /* Callback for foreach_window, used in window_from_coordinates.
1445 Check if window W contains coordinates specified by USER_DATA which
1446 is actually a pointer to a struct check_window_data CW.
1448 Check if window W contains coordinates *CW->x and *CW->y. If it
1449 does, return W in *CW->window, as Lisp_Object, and return in
1450 *CW->part the part of the window under coordinates *X,*Y. Return
1451 false from this function to stop iterating over windows. */
1453 struct check_window_data
1455 Lisp_Object *window;
1456 int x, y;
1457 enum window_part *part;
1460 static bool
1461 check_window_containing (struct window *w, void *user_data)
1463 struct check_window_data *cw = user_data;
1464 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1465 if (found == ON_NOTHING)
1466 return true;
1467 else
1469 *cw->part = found;
1470 XSETWINDOW (*cw->window, w);
1471 return false;
1476 /* Find the window containing frame-relative pixel position X/Y and
1477 return it as a Lisp_Object.
1479 If X, Y is on one of the window's special `window_part' elements,
1480 set *PART to the id of that element.
1482 If there is no window under X, Y return nil and leave *PART
1483 unmodified. TOOL_BAR_P means detect tool-bar windows.
1485 This function was previously implemented with a loop cycling over
1486 windows with Fnext_window, and starting with the frame's selected
1487 window. It turned out that this doesn't work with an
1488 implementation of next_window using Vwindow_list, because
1489 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1490 tree of F when this function is called asynchronously from
1491 note_mouse_highlight. The original loop didn't terminate in this
1492 case. */
1494 Lisp_Object
1495 window_from_coordinates (struct frame *f, int x, int y,
1496 enum window_part *part, bool tool_bar_p)
1498 Lisp_Object window;
1499 struct check_window_data cw;
1500 enum window_part dummy;
1502 if (part == 0)
1503 part = &dummy;
1505 window = Qnil;
1506 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1507 foreach_window (f, check_window_containing, &cw);
1509 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1510 /* If not found above, see if it's in the tool bar window, if a tool
1511 bar exists. */
1512 if (NILP (window)
1513 && tool_bar_p
1514 && WINDOWP (f->tool_bar_window)
1515 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1516 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1517 != ON_NOTHING))
1519 *part = ON_TEXT;
1520 window = f->tool_bar_window;
1522 #endif
1524 return window;
1527 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1528 doc: /* Return window containing coordinates X and Y on FRAME.
1529 FRAME must be a live frame and defaults to the selected one.
1530 The top left corner of the frame is considered to be row 0,
1531 column 0. */)
1532 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1534 struct frame *f = decode_live_frame (frame);
1536 /* Check that arguments are integers or floats. */
1537 CHECK_NUMBER_OR_FLOAT (x);
1538 CHECK_NUMBER_OR_FLOAT (y);
1540 return window_from_coordinates (f,
1541 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1542 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1543 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1544 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1545 0, false);
1548 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1549 doc: /* Return current value of point in WINDOW.
1550 WINDOW must be a live window and defaults to the selected one.
1552 For a nonselected window, this is the value point would have if that
1553 window were selected.
1555 Note that, when WINDOW is selected, the value returned is the same as
1556 that returned by `point' for WINDOW's buffer. It would be more strictly
1557 correct to return the top-level value of `point', outside of any
1558 `save-excursion' forms. But that is hard to define. */)
1559 (Lisp_Object window)
1561 register struct window *w = decode_live_window (window);
1563 if (w == XWINDOW (selected_window))
1564 return make_number (BUF_PT (XBUFFER (w->contents)));
1565 else
1566 return Fmarker_position (w->pointm);
1569 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1570 doc: /* Return old value of point in WINDOW.
1571 WINDOW must be a live window and defaults to the selected one. */)
1572 (Lisp_Object window)
1574 return Fmarker_position (decode_live_window (window)->old_pointm);
1577 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1578 doc: /* Return position at which display currently starts in WINDOW.
1579 WINDOW must be a live window and defaults to the selected one.
1580 This is updated by redisplay or by calling `set-window-start'. */)
1581 (Lisp_Object window)
1583 return Fmarker_position (decode_live_window (window)->start);
1586 /* This is text temporarily removed from the doc string below.
1588 This function returns nil if the position is not currently known.
1589 That happens when redisplay is preempted and doesn't finish.
1590 If in that case you want to compute where the end of the window would
1591 have been if redisplay had finished, do this:
1592 (save-excursion
1593 (goto-char (window-start window))
1594 (vertical-motion (1- (window-height window)) window)
1595 (point))") */
1597 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1598 doc: /* Return position at which display currently ends in WINDOW.
1599 WINDOW must be a live window and defaults to the selected one.
1600 This is updated by redisplay, when it runs to completion.
1601 Simply changing the buffer text or setting `window-start'
1602 does not update this value.
1603 Return nil if there is no recorded value. (This can happen if the
1604 last redisplay of WINDOW was preempted, and did not finish.)
1605 If UPDATE is non-nil, compute the up-to-date position
1606 if it isn't already recorded. */)
1607 (Lisp_Object window, Lisp_Object update)
1609 Lisp_Object value;
1610 struct window *w = decode_live_window (window);
1611 Lisp_Object buf;
1612 struct buffer *b;
1614 buf = w->contents;
1615 CHECK_BUFFER (buf);
1616 b = XBUFFER (buf);
1618 if (! NILP (update)
1619 && (windows_or_buffers_changed
1620 || !w->window_end_valid
1621 || b->clip_changed
1622 || b->prevent_redisplay_optimizations_p
1623 || window_outdated (w))
1624 /* Don't call display routines if we didn't yet create any real
1625 frames, because the glyph matrices are not yet allocated in
1626 that case. This could happen in some code that runs in the
1627 daemon during initialization (e.g., see bug#20565). */
1628 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1630 struct text_pos startp;
1631 struct it it;
1632 struct buffer *old_buffer = NULL;
1633 void *itdata = NULL;
1635 /* Cannot use Fvertical_motion because that function doesn't
1636 cope with variable-height lines. */
1637 if (b != current_buffer)
1639 old_buffer = current_buffer;
1640 set_buffer_internal (b);
1643 /* In case W->start is out of the range, use something
1644 reasonable. This situation occurred when loading a file with
1645 `-l' containing a call to `rmail' with subsequent other
1646 commands. At the end, W->start happened to be BEG, while
1647 rmail had already narrowed the buffer. */
1648 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1650 itdata = bidi_shelve_cache ();
1651 start_display (&it, w, startp);
1652 move_it_vertically (&it, window_box_height (w));
1653 if (it.current_y < it.last_visible_y)
1654 move_it_past_eol (&it);
1655 value = make_number (IT_CHARPOS (it));
1656 bidi_unshelve_cache (itdata, false);
1658 if (old_buffer)
1659 set_buffer_internal (old_buffer);
1661 else
1662 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1664 return value;
1667 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1668 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1669 WINDOW must be a live window and defaults to the selected one.
1670 Return POS. */)
1671 (Lisp_Object window, Lisp_Object pos)
1673 register struct window *w = decode_live_window (window);
1675 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1677 if (w == XWINDOW (selected_window))
1679 if (XBUFFER (w->contents) == current_buffer)
1680 Fgoto_char (pos);
1681 else
1683 struct buffer *old_buffer = current_buffer;
1685 /* ... but here we want to catch type error before buffer change. */
1686 CHECK_NUMBER_COERCE_MARKER (pos);
1687 set_buffer_internal (XBUFFER (w->contents));
1688 Fgoto_char (pos);
1689 set_buffer_internal (old_buffer);
1692 else
1694 set_marker_restricted (w->pointm, pos, w->contents);
1695 /* We have to make sure that redisplay updates the window to show
1696 the new value of point. */
1697 wset_redisplay (w);
1700 return pos;
1703 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1704 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1705 WINDOW must be a live window and defaults to the selected one. Return
1706 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1707 overriding motion of point in order to display at this exact start. */)
1708 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1710 register struct window *w = decode_live_window (window);
1712 set_marker_restricted (w->start, pos, w->contents);
1713 /* This is not right, but much easier than doing what is right. */
1714 w->start_at_line_beg = false;
1715 if (NILP (noforce))
1716 w->force_start = true;
1717 wset_update_mode_line (w);
1718 /* Bug#15957. */
1719 w->window_end_valid = false;
1720 wset_redisplay (w);
1722 return pos;
1725 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1726 Spos_visible_in_window_p, 0, 3, 0,
1727 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1728 WINDOW must be a live window and defaults to the selected one.
1730 Return nil if that position is scrolled vertically out of view. If a
1731 character is only partially visible, nil is returned, unless the
1732 optional argument PARTIALLY is non-nil. If POS is only out of view
1733 because of horizontal scrolling, return non-nil. If POS is t, it
1734 specifies either the first position displayed on the last visible
1735 screen line in WINDOW, or the end-of-buffer position, whichever comes
1736 first. POS defaults to point in WINDOW; WINDOW defaults to the
1737 selected window.
1739 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1740 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1741 where X and Y are the pixel coordinates relative to the top left corner
1742 of the window. The remaining elements are omitted if the character after
1743 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1744 off-window at the top and bottom of the screen line ("row") containing
1745 POS, ROWH is the visible height of that row, and VPOS is the row number
1746 \(zero-based). */)
1747 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1749 struct window *w;
1750 EMACS_INT posint;
1751 struct buffer *buf;
1752 struct text_pos top;
1753 Lisp_Object in_window = Qnil;
1754 int rtop, rbot, rowh, vpos;
1755 bool fully_p = true;
1756 int x, y;
1758 w = decode_live_window (window);
1759 buf = XBUFFER (w->contents);
1760 SET_TEXT_POS_FROM_MARKER (top, w->start);
1762 if (EQ (pos, Qt))
1763 posint = -1;
1764 else if (!NILP (pos))
1766 CHECK_NUMBER_COERCE_MARKER (pos);
1767 posint = XINT (pos);
1769 else if (w == XWINDOW (selected_window))
1770 posint = PT;
1771 else
1772 posint = marker_position (w->pointm);
1774 /* If position is above window start or outside buffer boundaries,
1775 or if window start is out of range, position is not visible. */
1776 if ((EQ (pos, Qt)
1777 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1778 && CHARPOS (top) >= BUF_BEGV (buf)
1779 && CHARPOS (top) <= BUF_ZV (buf)
1780 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1782 fully_p = !rtop && !rbot;
1783 if (!NILP (partially) || fully_p)
1784 in_window = Qt;
1787 if (!NILP (in_window) && !NILP (partially))
1789 Lisp_Object part = Qnil;
1790 if (!fully_p)
1791 part = list4i (rtop, rbot, rowh, vpos);
1792 in_window = Fcons (make_number (x),
1793 Fcons (make_number (y), part));
1796 return in_window;
1799 DEFUN ("window-line-height", Fwindow_line_height,
1800 Swindow_line_height, 0, 2, 0,
1801 doc: /* Return height in pixels of text line LINE in window WINDOW.
1802 WINDOW must be a live window and defaults to the selected one.
1804 Return height of current line if LINE is omitted or nil. Return height of
1805 header or mode line if LINE is `header-line' or `mode-line'.
1806 Otherwise, LINE is a text line number starting from 0. A negative number
1807 counts from the end of the window.
1809 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1810 in pixels of the visible part of the line, VPOS and YPOS are the
1811 vertical position in lines and pixels of the line, relative to the top
1812 of the first text line, and OFFBOT is the number of off-window pixels at
1813 the bottom of the text line. If there are off-window pixels at the top
1814 of the (first) text line, YPOS is negative.
1816 Return nil if window display is not up-to-date. In that case, use
1817 `pos-visible-in-window-p' to obtain the information. */)
1818 (Lisp_Object line, Lisp_Object window)
1820 register struct window *w;
1821 register struct buffer *b;
1822 struct glyph_row *row, *end_row;
1823 int max_y, crop, i;
1824 EMACS_INT n;
1826 w = decode_live_window (window);
1828 if (noninteractive || w->pseudo_window_p)
1829 return Qnil;
1831 CHECK_BUFFER (w->contents);
1832 b = XBUFFER (w->contents);
1834 /* Fail if current matrix is not up-to-date. */
1835 if (!w->window_end_valid
1836 || windows_or_buffers_changed
1837 || b->clip_changed
1838 || b->prevent_redisplay_optimizations_p
1839 || window_outdated (w))
1840 return Qnil;
1842 if (NILP (line))
1844 i = w->cursor.vpos;
1845 if (i < 0 || i >= w->current_matrix->nrows
1846 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1847 return Qnil;
1848 max_y = window_text_bottom_y (w);
1849 goto found_row;
1852 if (EQ (line, Qheader_line))
1854 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1855 return Qnil;
1856 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1857 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1860 if (EQ (line, Qmode_line))
1862 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1863 return (row->enabled_p ?
1864 list4i (row->height,
1865 0, /* not accurate */
1866 (WINDOW_HEADER_LINE_HEIGHT (w)
1867 + window_text_bottom_y (w)),
1869 : Qnil);
1872 CHECK_NUMBER (line);
1873 n = XINT (line);
1875 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1876 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1877 max_y = window_text_bottom_y (w);
1878 i = 0;
1880 while ((n < 0 || i < n)
1881 && row <= end_row && row->enabled_p
1882 && row->y + row->height < max_y)
1883 row++, i++;
1885 if (row > end_row || !row->enabled_p)
1886 return Qnil;
1888 if (++n < 0)
1890 if (-n > i)
1891 return Qnil;
1892 row += n;
1893 i += n;
1896 found_row:
1897 crop = max (0, (row->y + row->height) - max_y);
1898 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1901 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1902 0, 1, 0,
1903 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1904 More precisely, return the value assigned by the last call of
1905 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1906 never called with WINDOW as its argument, or the value set by that
1907 function was internally reset since its last call. WINDOW must be a
1908 live window and defaults to the selected one.
1910 When a window is dedicated to its buffer, `display-buffer' will refrain
1911 from displaying another buffer in it. `get-lru-window' and
1912 `get-largest-window' treat dedicated windows specially.
1913 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1914 `kill-buffer' can delete a dedicated window and the containing frame.
1916 Functions like `set-window-buffer' may change the buffer displayed by a
1917 window, unless that window is "strongly" dedicated to its buffer, that
1918 is the value returned by `window-dedicated-p' is t. */)
1919 (Lisp_Object window)
1921 return decode_live_window (window)->dedicated;
1924 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1925 Sset_window_dedicated_p, 2, 2, 0,
1926 doc: /* Mark WINDOW as dedicated according to FLAG.
1927 WINDOW must be a live window and defaults to the selected one. FLAG
1928 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1929 mark WINDOW as non-dedicated. Return FLAG.
1931 When a window is dedicated to its buffer, `display-buffer' will refrain
1932 from displaying another buffer in it. `get-lru-window' and
1933 `get-largest-window' treat dedicated windows specially.
1934 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1935 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1936 and the containing frame.
1938 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1939 its buffer. Functions like `set-window-buffer' may change the buffer
1940 displayed by a window, unless that window is strongly dedicated to its
1941 buffer. If and when `set-window-buffer' displays another buffer in a
1942 window, it also makes sure that the window is no more dedicated. */)
1943 (Lisp_Object window, Lisp_Object flag)
1945 wset_dedicated (decode_live_window (window), flag);
1946 return flag;
1949 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1950 0, 1, 0,
1951 doc: /* Return buffers previously shown in WINDOW.
1952 WINDOW must be a live window and defaults to the selected one.
1954 The return value is a list of elements (BUFFER WINDOW-START POS),
1955 where BUFFER is a buffer, WINDOW-START is the start position of the
1956 window for that buffer, and POS is a window-specific point value. */)
1957 (Lisp_Object window)
1959 return decode_live_window (window)->prev_buffers;
1962 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1963 Sset_window_prev_buffers, 2, 2, 0,
1964 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1965 WINDOW must be a live window and defaults to the selected one.
1967 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1968 where BUFFER is a buffer, WINDOW-START is the start position of the
1969 window for that buffer, and POS is a window-specific point value. */)
1970 (Lisp_Object window, Lisp_Object prev_buffers)
1972 wset_prev_buffers (decode_live_window (window), prev_buffers);
1973 return prev_buffers;
1976 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1977 0, 1, 0,
1978 doc: /* Return list of buffers recently re-shown in WINDOW.
1979 WINDOW must be a live window and defaults to the selected one. */)
1980 (Lisp_Object window)
1982 return decode_live_window (window)->next_buffers;
1985 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1986 Sset_window_next_buffers, 2, 2, 0,
1987 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1988 WINDOW must be a live window and defaults to the selected one.
1989 NEXT-BUFFERS should be a list of buffers. */)
1990 (Lisp_Object window, Lisp_Object next_buffers)
1992 wset_next_buffers (decode_live_window (window), next_buffers);
1993 return next_buffers;
1996 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1997 0, 1, 0,
1998 doc: /* Return the parameters of WINDOW and their values.
1999 WINDOW must be a valid window and defaults to the selected one. The
2000 return value is a list of elements of the form (PARAMETER . VALUE). */)
2001 (Lisp_Object window)
2003 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2006 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2007 2, 2, 0,
2008 doc: /* Return WINDOW's value for PARAMETER.
2009 WINDOW can be any window and defaults to the selected one. */)
2010 (Lisp_Object window, Lisp_Object parameter)
2012 Lisp_Object result;
2014 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2015 return CDR_SAFE (result);
2018 DEFUN ("set-window-parameter", Fset_window_parameter,
2019 Sset_window_parameter, 3, 3, 0,
2020 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2021 WINDOW can be any window and defaults to the selected one.
2022 Return VALUE. */)
2023 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2025 register struct window *w = decode_any_window (window);
2026 Lisp_Object old_alist_elt;
2028 old_alist_elt = Fassq (parameter, w->window_parameters);
2029 if (NILP (old_alist_elt))
2030 wset_window_parameters
2031 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2032 else
2033 Fsetcdr (old_alist_elt, value);
2034 return value;
2037 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2038 0, 1, 0,
2039 doc: /* Return the display-table that WINDOW is using.
2040 WINDOW must be a live window and defaults to the selected one. */)
2041 (Lisp_Object window)
2043 return decode_live_window (window)->display_table;
2046 /* Get the display table for use on window W. This is either W's
2047 display table or W's buffer's display table. Ignore the specified
2048 tables if they are not valid; if no valid table is specified,
2049 return 0. */
2051 struct Lisp_Char_Table *
2052 window_display_table (struct window *w)
2054 struct Lisp_Char_Table *dp = NULL;
2056 if (DISP_TABLE_P (w->display_table))
2057 dp = XCHAR_TABLE (w->display_table);
2058 else if (BUFFERP (w->contents))
2060 struct buffer *b = XBUFFER (w->contents);
2062 if (DISP_TABLE_P (BVAR (b, display_table)))
2063 dp = XCHAR_TABLE (BVAR (b, display_table));
2064 else if (DISP_TABLE_P (Vstandard_display_table))
2065 dp = XCHAR_TABLE (Vstandard_display_table);
2068 return dp;
2071 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2072 doc: /* Set WINDOW's display-table to TABLE.
2073 WINDOW must be a live window and defaults to the selected one. */)
2074 (register Lisp_Object window, Lisp_Object table)
2076 wset_display_table (decode_live_window (window), table);
2077 return table;
2080 /* Record info on buffer window W is displaying
2081 when it is about to cease to display that buffer. */
2082 static void
2083 unshow_buffer (register struct window *w)
2085 Lisp_Object buf = w->contents;
2086 struct buffer *b = XBUFFER (buf);
2088 eassert (b == XMARKER (w->pointm)->buffer);
2090 #if false
2091 if (w == XWINDOW (selected_window)
2092 || ! EQ (buf, XWINDOW (selected_window)->contents))
2093 /* Do this except when the selected window's buffer
2094 is being removed from some other window. */
2095 #endif
2096 /* last_window_start records the start position that this buffer
2097 had in the last window to be disconnected from it.
2098 Now that this statement is unconditional,
2099 it is possible for the buffer to be displayed in the
2100 selected window, while last_window_start reflects another
2101 window which was recently showing the same buffer.
2102 Some people might say that might be a good thing. Let's see. */
2103 b->last_window_start = marker_position (w->start);
2105 /* Point in the selected window's buffer
2106 is actually stored in that buffer, and the window's pointm isn't used.
2107 So don't clobber point in that buffer. */
2108 if (! EQ (buf, XWINDOW (selected_window)->contents)
2109 /* Don't clobber point in current buffer either (this could be
2110 useful in connection with bug#12208).
2111 && XBUFFER (buf) != current_buffer */
2112 /* This line helps to fix Horsley's testbug.el bug. */
2113 && !(WINDOWP (BVAR (b, last_selected_window))
2114 && w != XWINDOW (BVAR (b, last_selected_window))
2115 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2116 temp_set_point_both (b,
2117 clip_to_bounds (BUF_BEGV (b),
2118 marker_position (w->pointm),
2119 BUF_ZV (b)),
2120 clip_to_bounds (BUF_BEGV_BYTE (b),
2121 marker_byte_position (w->pointm),
2122 BUF_ZV_BYTE (b)));
2124 if (WINDOWP (BVAR (b, last_selected_window))
2125 && w == XWINDOW (BVAR (b, last_selected_window)))
2126 bset_last_selected_window (b, Qnil);
2129 /* Put NEW into the window structure in place of OLD. SETFLAG false
2130 means change window structure only. Otherwise store geometry and
2131 other settings as well. */
2132 static void
2133 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2135 Lisp_Object tem;
2136 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2138 /* If OLD is its frame's root window, then NEW is the new
2139 root window for that frame. */
2140 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2141 fset_root_window (XFRAME (o->frame), new);
2143 if (setflag)
2145 n->pixel_left = o->pixel_left;
2146 n->pixel_top = o->pixel_top;
2147 n->pixel_width = o->pixel_width;
2148 n->pixel_height = o->pixel_height;
2149 n->left_col = o->left_col;
2150 n->top_line = o->top_line;
2151 n->total_cols = o->total_cols;
2152 n->total_lines = o->total_lines;
2153 wset_normal_cols (n, o->normal_cols);
2154 wset_normal_cols (o, make_float (1.0));
2155 wset_normal_lines (n, o->normal_lines);
2156 wset_normal_lines (o, make_float (1.0));
2157 n->desired_matrix = n->current_matrix = 0;
2158 n->vscroll = 0;
2159 memset (&n->cursor, 0, sizeof (n->cursor));
2160 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2161 n->last_cursor_vpos = 0;
2162 #ifdef HAVE_WINDOW_SYSTEM
2163 n->phys_cursor_type = NO_CURSOR;
2164 n->phys_cursor_width = -1;
2165 #endif
2166 n->must_be_updated_p = false;
2167 n->pseudo_window_p = false;
2168 n->window_end_vpos = 0;
2169 n->window_end_pos = 0;
2170 n->window_end_valid = false;
2173 tem = o->next;
2174 wset_next (n, tem);
2175 if (!NILP (tem))
2176 wset_prev (XWINDOW (tem), new);
2178 tem = o->prev;
2179 wset_prev (n, tem);
2180 if (!NILP (tem))
2181 wset_next (XWINDOW (tem), new);
2183 tem = o->parent;
2184 wset_parent (n, tem);
2185 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2186 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2189 /* If window WINDOW and its parent window are iso-combined, merge
2190 WINDOW's children into those of its parent window and mark WINDOW as
2191 deleted. */
2193 static void
2194 recombine_windows (Lisp_Object window)
2196 struct window *w, *p, *c;
2197 Lisp_Object parent, child;
2198 bool horflag;
2200 w = XWINDOW (window);
2201 parent = w->parent;
2202 if (!NILP (parent) && NILP (w->combination_limit))
2204 p = XWINDOW (parent);
2205 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2206 && p->horizontal == w->horizontal)
2207 /* WINDOW and PARENT are both either a vertical or a horizontal
2208 combination. */
2210 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2211 child = w->contents;
2212 c = XWINDOW (child);
2214 /* Splice WINDOW's children into its parent's children and
2215 assign new normal sizes. */
2216 if (NILP (w->prev))
2217 wset_combination (p, horflag, child);
2218 else
2220 wset_prev (c, w->prev);
2221 wset_next (XWINDOW (w->prev), child);
2224 while (c)
2226 wset_parent (c, parent);
2228 if (horflag)
2229 wset_normal_cols
2230 (c, make_float ((double) c->pixel_width
2231 / (double) p->pixel_width));
2232 else
2233 wset_normal_lines
2234 (c, make_float ((double) c->pixel_height
2235 / (double) p->pixel_height));
2237 if (NILP (c->next))
2239 if (!NILP (w->next))
2241 wset_next (c, w->next);
2242 wset_prev (XWINDOW (c->next), child);
2245 c = 0;
2247 else
2249 child = c->next;
2250 c = XWINDOW (child);
2254 /* WINDOW can be deleted now. */
2255 wset_combination (w, false, Qnil);
2260 /* If WINDOW can be deleted, delete it. */
2261 static void
2262 delete_deletable_window (Lisp_Object window)
2264 if (!NILP (call1 (Qwindow_deletable_p, window)))
2265 call1 (Qdelete_window, window);
2268 /***********************************************************************
2269 Window List
2270 ***********************************************************************/
2272 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2273 pointer. This is a callback function for foreach_window, used in
2274 the window_list function. */
2276 static bool
2277 add_window_to_list (struct window *w, void *user_data)
2279 Lisp_Object *list = user_data;
2280 Lisp_Object window;
2281 XSETWINDOW (window, w);
2282 *list = Fcons (window, *list);
2283 return true;
2287 /* Return a list of all windows, for use by next_window. If
2288 Vwindow_list is a list, return that list. Otherwise, build a new
2289 list, cache it in Vwindow_list, and return that. */
2291 Lisp_Object
2292 window_list (void)
2294 if (!CONSP (Vwindow_list))
2296 Lisp_Object tail, frame;
2298 Vwindow_list = Qnil;
2299 FOR_EACH_FRAME (tail, frame)
2301 Lisp_Object arglist = Qnil;
2303 /* We are visiting windows in canonical order, and add
2304 new windows at the front of args[1], which means we
2305 have to reverse this list at the end. */
2306 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2307 arglist = Fnreverse (arglist);
2308 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2312 return Vwindow_list;
2316 /* Value is true if WINDOW satisfies the constraints given by
2317 OWINDOW, MINIBUF and ALL_FRAMES.
2319 MINIBUF t means WINDOW may be minibuffer windows.
2320 `lambda' means WINDOW may not be a minibuffer window.
2321 a window means a specific minibuffer window
2323 ALL_FRAMES t means search all frames,
2324 nil means search just current frame,
2325 `visible' means search just visible frames on the
2326 current terminal,
2327 0 means search visible and iconified frames on the
2328 current terminal,
2329 a window means search the frame that window belongs to,
2330 a frame means consider windows on that frame, only. */
2332 static bool
2333 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2334 Lisp_Object minibuf, Lisp_Object all_frames)
2336 struct window *w = XWINDOW (window);
2337 struct frame *f = XFRAME (w->frame);
2338 bool candidate_p = true;
2340 if (!BUFFERP (w->contents))
2341 candidate_p = false;
2342 else if (MINI_WINDOW_P (w)
2343 && (EQ (minibuf, Qlambda)
2344 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2346 /* If MINIBUF is `lambda' don't consider any mini-windows.
2347 If it is a window, consider only that one. */
2348 candidate_p = false;
2350 else if (EQ (all_frames, Qt))
2351 candidate_p = true;
2352 else if (NILP (all_frames))
2354 eassert (WINDOWP (owindow));
2355 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2357 else if (EQ (all_frames, Qvisible))
2359 candidate_p = FRAME_VISIBLE_P (f)
2360 && (FRAME_TERMINAL (XFRAME (w->frame))
2361 == FRAME_TERMINAL (XFRAME (selected_frame)));
2364 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2366 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2367 #ifdef HAVE_X_WINDOWS
2368 /* Yuck!! If we've just created the frame and the
2369 window-manager requested the user to place it
2370 manually, the window may still not be considered
2371 `visible'. I'd argue it should be at least
2372 something like `iconified', but don't know how to do
2373 that yet. --Stef */
2374 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2375 && !f->output_data.x->has_been_visible)
2376 #endif
2378 && (FRAME_TERMINAL (XFRAME (w->frame))
2379 == FRAME_TERMINAL (XFRAME (selected_frame)));
2381 else if (WINDOWP (all_frames))
2382 /* To qualify as candidate, it's not sufficient for WINDOW's frame
2383 to just share the minibuffer window - it must be active as well
2384 (see Bug#24500). */
2385 candidate_p = (EQ (XWINDOW (all_frames)->frame, w->frame)
2386 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2387 else if (FRAMEP (all_frames))
2388 candidate_p = EQ (all_frames, w->frame);
2390 return candidate_p;
2394 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2395 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2396 MINIBUF, and ALL_FRAMES. */
2398 static void
2399 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2401 struct window *w = decode_live_window (*window);
2403 XSETWINDOW (*window, w);
2404 /* MINIBUF nil may or may not include minibuffers. Decide if it
2405 does. */
2406 if (NILP (*minibuf))
2407 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2408 else if (!EQ (*minibuf, Qt))
2409 *minibuf = Qlambda;
2411 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2412 => count none of them, or a specific minibuffer window (the
2413 active one) to count. */
2415 /* ALL_FRAMES nil doesn't specify which frames to include. */
2416 if (NILP (*all_frames))
2417 *all_frames
2418 = (!EQ (*minibuf, Qlambda)
2419 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2420 : Qnil);
2421 else if (EQ (*all_frames, Qvisible))
2423 else if (EQ (*all_frames, make_number (0)))
2425 else if (FRAMEP (*all_frames))
2427 else if (!EQ (*all_frames, Qt))
2428 *all_frames = Qnil;
2432 /* Return the next or previous window of WINDOW in cyclic ordering
2433 of windows. NEXT_P means return the next window. See the
2434 documentation string of next-window for the meaning of MINIBUF and
2435 ALL_FRAMES. */
2437 static Lisp_Object
2438 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2439 bool next_p)
2441 decode_next_window_args (&window, &minibuf, &all_frames);
2443 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2444 return the first window on the frame. */
2445 if (FRAMEP (all_frames)
2446 && !EQ (all_frames, XWINDOW (window)->frame))
2447 return Fframe_first_window (all_frames);
2449 if (next_p)
2451 Lisp_Object list;
2453 /* Find WINDOW in the list of all windows. */
2454 list = Fmemq (window, window_list ());
2456 /* Scan forward from WINDOW to the end of the window list. */
2457 if (CONSP (list))
2458 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2459 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2460 break;
2462 /* Scan from the start of the window list up to WINDOW. */
2463 if (!CONSP (list))
2464 for (list = Vwindow_list;
2465 CONSP (list) && !EQ (XCAR (list), window);
2466 list = XCDR (list))
2467 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2468 break;
2470 if (CONSP (list))
2471 window = XCAR (list);
2473 else
2475 Lisp_Object candidate, list;
2477 /* Scan through the list of windows for candidates. If there are
2478 candidate windows in front of WINDOW, the last one of these
2479 is the one we want. If there are candidates following WINDOW
2480 in the list, again the last one of these is the one we want. */
2481 candidate = Qnil;
2482 for (list = window_list (); CONSP (list); list = XCDR (list))
2484 if (EQ (XCAR (list), window))
2486 if (WINDOWP (candidate))
2487 break;
2489 else if (candidate_window_p (XCAR (list), window, minibuf,
2490 all_frames))
2491 candidate = XCAR (list);
2494 if (WINDOWP (candidate))
2495 window = candidate;
2498 return window;
2502 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2503 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2504 WINDOW must be a live window and defaults to the selected one. The
2505 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2506 consider.
2508 MINIBUF nil or omitted means consider the minibuffer window only if the
2509 minibuffer is active. MINIBUF t means consider the minibuffer window
2510 even if the minibuffer is not active. Any other value means do not
2511 consider the minibuffer window even if the minibuffer is active.
2513 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2514 plus the minibuffer window if specified by the MINIBUF argument. If the
2515 minibuffer counts, consider all windows on all frames that share that
2516 minibuffer too. The following non-nil values of ALL-FRAMES have special
2517 meanings:
2519 - t means consider all windows on all existing frames.
2521 - `visible' means consider all windows on all visible frames.
2523 - 0 (the number zero) means consider all windows on all visible and
2524 iconified frames.
2526 - A frame means consider all windows on that frame only.
2528 Anything else means consider all windows on WINDOW's frame and no
2529 others.
2531 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2532 `next-window' to iterate through the entire cycle of acceptable
2533 windows, eventually ending up back at the window you started with.
2534 `previous-window' traverses the same cycle, in the reverse order. */)
2535 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2537 return next_window (window, minibuf, all_frames, true);
2541 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2542 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2543 WINDOW must be a live window and defaults to the selected one. The
2544 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2545 consider.
2547 MINIBUF nil or omitted means consider the minibuffer window only if the
2548 minibuffer is active. MINIBUF t means consider the minibuffer window
2549 even if the minibuffer is not active. Any other value means do not
2550 consider the minibuffer window even if the minibuffer is active.
2552 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2553 plus the minibuffer window if specified by the MINIBUF argument. If the
2554 minibuffer counts, consider all windows on all frames that share that
2555 minibuffer too. The following non-nil values of ALL-FRAMES have special
2556 meanings:
2558 - t means consider all windows on all existing frames.
2560 - `visible' means consider all windows on all visible frames.
2562 - 0 (the number zero) means consider all windows on all visible and
2563 iconified frames.
2565 - A frame means consider all windows on that frame only.
2567 Anything else means consider all windows on WINDOW's frame and no
2568 others.
2570 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2571 use `previous-window' to iterate through the entire cycle of
2572 acceptable windows, eventually ending up back at the window you
2573 started with. `next-window' traverses the same cycle, in the
2574 reverse order. */)
2575 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2577 return next_window (window, minibuf, all_frames, false);
2581 /* Return a list of windows in cyclic ordering. Arguments are like
2582 for `next-window'. */
2584 static Lisp_Object
2585 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2587 Lisp_Object tail, list, rest;
2589 decode_next_window_args (&window, &minibuf, &all_frames);
2590 list = Qnil;
2592 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2593 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2594 list = Fcons (XCAR (tail), list);
2596 /* Rotate the list to start with WINDOW. */
2597 list = Fnreverse (list);
2598 rest = Fmemq (window, list);
2599 if (!NILP (rest) && !EQ (rest, list))
2601 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2603 XSETCDR (tail, Qnil);
2604 list = nconc2 (rest, list);
2606 return list;
2610 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2611 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2612 FRAME nil or omitted means use the selected frame.
2613 WINDOW nil or omitted means use the window selected within FRAME.
2614 MINIBUF t means include the minibuffer window, even if it isn't active.
2615 MINIBUF nil or omitted means include the minibuffer window only
2616 if it's active.
2617 MINIBUF neither nil nor t means never include the minibuffer window. */)
2618 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2620 if (NILP (window))
2621 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2622 CHECK_WINDOW (window);
2623 if (NILP (frame))
2624 frame = selected_frame;
2626 if (!EQ (frame, XWINDOW (window)->frame))
2627 error ("Window is on a different frame");
2629 return window_list_1 (window, minibuf, frame);
2633 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2634 doc: /* Return a list of all live windows.
2635 WINDOW specifies the first window to list and defaults to the selected
2636 window.
2638 Optional argument MINIBUF nil or omitted means consider the minibuffer
2639 window only if the minibuffer is active. MINIBUF t means consider the
2640 minibuffer window even if the minibuffer is not active. Any other value
2641 means do not consider the minibuffer window even if the minibuffer is
2642 active.
2644 Optional argument ALL-FRAMES nil or omitted means consider all windows
2645 on WINDOW's frame, plus the minibuffer window if specified by the
2646 MINIBUF argument. If the minibuffer counts, consider all windows on all
2647 frames that share that minibuffer too. The following non-nil values of
2648 ALL-FRAMES have special meanings:
2650 - t means consider all windows on all existing frames.
2652 - `visible' means consider all windows on all visible frames.
2654 - 0 (the number zero) means consider all windows on all visible and
2655 iconified frames.
2657 - A frame means consider all windows on that frame only.
2659 Anything else means consider all windows on WINDOW's frame and no
2660 others.
2662 If WINDOW is not on the list of windows returned, some other window will
2663 be listed first but no error is signaled. */)
2664 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2666 return window_list_1 (window, minibuf, all_frames);
2669 /* Look at all windows, performing an operation specified by TYPE
2670 with argument OBJ.
2671 If FRAMES is Qt, look at all frames;
2672 Qnil, look at just the selected frame;
2673 Qvisible, look at visible frames;
2674 a frame, just look at windows on that frame.
2675 If MINI, perform the operation on minibuffer windows too. */
2677 enum window_loop
2679 WINDOW_LOOP_UNUSED,
2680 GET_BUFFER_WINDOW, /* Arg is buffer */
2681 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2682 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2683 CHECK_ALL_WINDOWS /* Arg is ignored */
2686 static Lisp_Object
2687 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2688 Lisp_Object frames)
2690 Lisp_Object window, windows, best_window, frame_arg;
2691 bool frame_best_window_flag = false;
2692 struct frame *f;
2694 /* If we're only looping through windows on a particular frame,
2695 frame points to that frame. If we're looping through windows
2696 on all frames, frame is 0. */
2697 if (FRAMEP (frames))
2698 f = XFRAME (frames);
2699 else if (NILP (frames))
2700 f = SELECTED_FRAME ();
2701 else
2702 f = NULL;
2704 if (f)
2705 frame_arg = Qlambda;
2706 else if (EQ (frames, make_number (0)))
2707 frame_arg = frames;
2708 else if (EQ (frames, Qvisible))
2709 frame_arg = frames;
2710 else
2711 frame_arg = Qt;
2713 /* frame_arg is Qlambda to stick to one frame,
2714 Qvisible to consider all visible frames,
2715 or Qt otherwise. */
2717 /* Pick a window to start with. */
2718 if (WINDOWP (obj))
2719 window = obj;
2720 else if (f)
2721 window = FRAME_SELECTED_WINDOW (f);
2722 else
2723 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2725 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2726 best_window = Qnil;
2728 for (; CONSP (windows); windows = XCDR (windows))
2730 struct window *w;
2732 window = XCAR (windows);
2733 w = XWINDOW (window);
2735 /* Note that we do not pay attention here to whether the frame
2736 is visible, since Fwindow_list skips non-visible frames if
2737 that is desired, under the control of frame_arg. */
2738 if (!MINI_WINDOW_P (w)
2739 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2740 consider all windows. */
2741 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2742 || (mini && minibuf_level > 0))
2743 switch (type)
2745 case GET_BUFFER_WINDOW:
2746 if (EQ (w->contents, obj)
2747 /* Don't find any minibuffer window except the one that
2748 is currently in use. */
2749 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2751 if (EQ (window, selected_window))
2752 /* Preferably return the selected window. */
2753 return window;
2754 else if (EQ (XWINDOW (window)->frame, selected_frame)
2755 && !frame_best_window_flag)
2756 /* Prefer windows on the current frame (but don't
2757 choose another one if we have one already). */
2759 best_window = window;
2760 frame_best_window_flag = true;
2762 else if (NILP (best_window))
2763 best_window = window;
2765 break;
2767 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2768 /* We could simply check whether the buffer shown by window
2769 is live, and show another buffer in case it isn't. */
2770 if (EQ (w->contents, obj))
2772 /* Undedicate WINDOW. */
2773 wset_dedicated (w, Qnil);
2774 /* Make WINDOW show the buffer returned by
2775 other_buffer_safely, don't run any hooks. */
2776 set_window_buffer
2777 (window, other_buffer_safely (w->contents), false, false);
2778 /* If WINDOW is the selected window, make its buffer
2779 current. But do so only if the window shows the
2780 current buffer (Bug#6454). */
2781 if (EQ (window, selected_window)
2782 && XBUFFER (w->contents) == current_buffer)
2783 Fset_buffer (w->contents);
2785 break;
2787 case REDISPLAY_BUFFER_WINDOWS:
2788 if (EQ (w->contents, obj))
2790 mark_window_display_accurate (window, false);
2791 w->update_mode_line = true;
2792 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2793 update_mode_lines = 27;
2794 best_window = window;
2796 break;
2798 /* Check for a leaf window that has a killed buffer
2799 or broken markers. */
2800 case CHECK_ALL_WINDOWS:
2801 if (BUFFERP (w->contents))
2803 struct buffer *b = XBUFFER (w->contents);
2805 if (!BUFFER_LIVE_P (b))
2806 emacs_abort ();
2807 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2808 emacs_abort ();
2809 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2810 emacs_abort ();
2812 break;
2814 case WINDOW_LOOP_UNUSED:
2815 break;
2819 return best_window;
2822 /* Used for debugging. Abort if any window has a dead buffer. */
2824 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2825 void
2826 check_all_windows (void)
2828 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2831 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2832 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2833 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2834 the current buffer.
2836 The optional argument ALL-FRAMES specifies the frames to consider:
2838 - t means consider all windows on all existing frames.
2840 - `visible' means consider all windows on all visible frames.
2842 - 0 (the number zero) means consider all windows on all visible
2843 and iconified frames.
2845 - A frame means consider all windows on that frame only.
2847 Any other value of ALL-FRAMES means consider all windows on the
2848 selected frame and no others. */)
2849 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2851 Lisp_Object buffer;
2853 if (NILP (buffer_or_name))
2854 buffer = Fcurrent_buffer ();
2855 else
2856 buffer = Fget_buffer (buffer_or_name);
2858 if (BUFFERP (buffer))
2859 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2860 else
2861 return Qnil;
2865 static Lisp_Object
2866 resize_root_window (Lisp_Object window, Lisp_Object delta,
2867 Lisp_Object horizontal, Lisp_Object ignore,
2868 Lisp_Object pixelwise)
2870 return call5 (Qwindow__resize_root_window, window, delta,
2871 horizontal, ignore, pixelwise);
2874 void
2875 sanitize_window_sizes (Lisp_Object horizontal)
2877 /* Don't burp in temacs -nw before window.el is loaded. */
2878 if (!NILP (Fsymbol_function (Qwindow__sanitize_window_sizes)))
2879 call1 (Qwindow__sanitize_window_sizes, horizontal);
2883 static Lisp_Object
2884 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2886 return call2 (Qwindow__pixel_to_total, frame, horizontal);
2890 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2891 Sdelete_other_windows_internal, 0, 2, "",
2892 doc: /* Make WINDOW fill its frame.
2893 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2894 and defaults to the selected one.
2896 Optional argument ROOT, if non-nil, must specify an internal window such
2897 that WINDOW is in its window subtree. If this is the case, replace ROOT
2898 by WINDOW and leave alone any windows not part of ROOT's subtree.
2900 When WINDOW is live try to reduce display jumps by keeping the text
2901 previously visible in WINDOW in the same place on the frame. Doing this
2902 depends on the value of (window-start WINDOW), so if calling this
2903 function in a program gives strange scrolling, make sure the
2904 window-start value is reasonable when this function is called. */)
2905 (Lisp_Object window, Lisp_Object root)
2907 struct window *w, *r, *s;
2908 struct frame *f;
2909 Lisp_Object sibling, pwindow, delta;
2910 Lisp_Object swindow UNINIT;
2911 ptrdiff_t startpos UNINIT, startbyte UNINIT;
2912 int top UNINIT;
2913 int new_top;
2914 bool resize_failed = false;
2916 w = decode_valid_window (window);
2917 XSETWINDOW (window, w);
2918 f = XFRAME (w->frame);
2920 if (NILP (root))
2921 /* ROOT is the frame's root window. */
2923 root = FRAME_ROOT_WINDOW (f);
2924 r = XWINDOW (root);
2926 else
2927 /* ROOT must be an ancestor of WINDOW. */
2929 r = decode_valid_window (root);
2930 pwindow = XWINDOW (window)->parent;
2931 while (!NILP (pwindow))
2932 if (EQ (pwindow, root))
2933 break;
2934 else
2935 pwindow = XWINDOW (pwindow)->parent;
2936 if (!EQ (pwindow, root))
2937 error ("Specified root is not an ancestor of specified window");
2940 if (EQ (window, root))
2941 /* A noop. */
2942 return Qnil;
2943 /* I don't understand the "top > 0" part below. If we deal with a
2944 standalone minibuffer it would have been caught by the preceding
2945 test. */
2946 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2947 error ("Can't expand minibuffer to full frame");
2949 if (BUFFERP (w->contents))
2951 startpos = marker_position (w->start);
2952 startbyte = marker_byte_position (w->start);
2953 top = (WINDOW_TOP_EDGE_LINE (w)
2954 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2955 /* Make sure WINDOW is the frame's selected window. */
2956 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2958 if (EQ (selected_frame, w->frame))
2959 Fselect_window (window, Qnil);
2960 else
2961 fset_selected_window (f, window);
2964 else
2966 /* See if the frame's selected window is a part of the window
2967 subtree rooted at WINDOW, by finding all the selected window's
2968 parents and comparing each one with WINDOW. If it isn't we
2969 need a new selected window for this frame. */
2970 swindow = FRAME_SELECTED_WINDOW (f);
2971 while (true)
2973 pwindow = swindow;
2974 while (!NILP (pwindow) && !EQ (window, pwindow))
2975 pwindow = XWINDOW (pwindow)->parent;
2977 if (EQ (window, pwindow))
2978 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2979 as the new selected window. */
2980 break;
2981 else
2982 /* Else try the previous window of SWINDOW. */
2983 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2986 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2988 if (EQ (selected_frame, w->frame))
2989 Fselect_window (swindow, Qnil);
2990 else
2991 fset_selected_window (f, swindow);
2995 block_input ();
2996 if (!FRAME_INITIAL_P (f))
2998 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3000 /* We are going to free the glyph matrices of WINDOW, and with
3001 that we might lose any information about glyph rows that have
3002 some of their glyphs highlighted in mouse face. (These rows
3003 are marked with a mouse_face_p flag.) If WINDOW
3004 indeed has some glyphs highlighted in mouse face, signal to
3005 frame's up-to-date hook that mouse highlight was overwritten,
3006 so that it will arrange for redisplaying the highlight. */
3007 if (EQ (hlinfo->mouse_face_window, window))
3008 reset_mouse_highlight (hlinfo);
3010 free_window_matrices (r);
3012 fset_redisplay (f);
3013 Vwindow_list = Qnil;
3015 if (!WINDOW_LEAF_P (w))
3017 /* Resize child windows vertically. */
3018 XSETINT (delta, r->pixel_height - w->pixel_height);
3019 w->pixel_top = r->pixel_top;
3020 w->top_line = r->top_line;
3021 resize_root_window (window, delta, Qnil, Qnil, Qt);
3022 if (window_resize_check (w, false))
3024 window_resize_apply (w, false);
3025 window_pixel_to_total (w->frame, Qnil);
3027 else
3029 resize_root_window (window, delta, Qnil, Qt, Qt);
3030 if (window_resize_check (w, false))
3032 window_resize_apply (w, false);
3033 window_pixel_to_total (w->frame, Qnil);
3035 else
3036 resize_failed = true;
3039 /* Resize child windows horizontally. */
3040 if (!resize_failed)
3042 w->left_col = r->left_col;
3043 w->pixel_left = r->pixel_left;
3044 XSETINT (delta, r->pixel_width - w->pixel_width);
3045 resize_root_window (window, delta, Qt, Qnil, Qt);
3046 if (window_resize_check (w, true))
3048 window_resize_apply (w, true);
3049 window_pixel_to_total (w->frame, Qt);
3051 else
3053 resize_root_window (window, delta, Qt, Qt, Qt);
3054 if (window_resize_check (w, true))
3056 window_resize_apply (w, true);
3057 window_pixel_to_total (w->frame, Qt);
3059 else
3060 resize_failed = true;
3064 if (resize_failed)
3065 /* Play safe, if we still can ... */
3067 window = swindow;
3068 w = XWINDOW (window);
3072 /* Cleanly unlink WINDOW from window-tree. */
3073 if (!NILP (w->prev))
3074 /* Get SIBLING above (on the left of) WINDOW. */
3076 sibling = w->prev;
3077 s = XWINDOW (sibling);
3078 wset_next (s, w->next);
3079 if (!NILP (s->next))
3080 wset_prev (XWINDOW (s->next), sibling);
3082 else
3083 /* Get SIBLING below (on the right of) WINDOW. */
3085 sibling = w->next;
3086 s = XWINDOW (sibling);
3087 wset_prev (s, Qnil);
3088 wset_combination (XWINDOW (w->parent),
3089 XWINDOW (w->parent)->horizontal, sibling);
3092 /* Delete ROOT and all child windows of ROOT. */
3093 if (WINDOWP (r->contents))
3095 delete_all_child_windows (r->contents);
3096 wset_combination (r, false, Qnil);
3099 replace_window (root, window, true);
3101 /* This must become SWINDOW anyway ....... */
3102 if (BUFFERP (w->contents) && !resize_failed)
3104 /* Try to minimize scrolling, by setting the window start to the
3105 point will cause the text at the old window start to be at the
3106 same place on the frame. But don't try to do this if the
3107 window start is outside the visible portion (as might happen
3108 when the display is not current, due to typeahead). */
3109 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3110 if (new_top != top
3111 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3112 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3114 struct position pos;
3115 struct buffer *obuf = current_buffer;
3117 Fset_buffer (w->contents);
3118 /* This computation used to temporarily move point, but that
3119 can have unwanted side effects due to text properties. */
3120 pos = *vmotion (startpos, startbyte, -top, w);
3122 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3123 w->window_end_valid = false;
3124 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3125 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3126 /* We need to do this, so that the window-scroll-functions
3127 get called. */
3128 w->optional_new_start = true;
3130 set_buffer_internal (obuf);
3134 adjust_frame_glyphs (f);
3135 unblock_input ();
3137 run_window_configuration_change_hook (f);
3139 return Qnil;
3143 void
3144 replace_buffer_in_windows (Lisp_Object buffer)
3146 call1 (Qreplace_buffer_in_windows, buffer);
3149 /* If BUFFER is shown in a window, safely replace it with some other
3150 buffer in all windows of all frames, even those on other keyboards. */
3152 void
3153 replace_buffer_in_windows_safely (Lisp_Object buffer)
3155 if (buffer_window_count (XBUFFER (buffer)))
3157 Lisp_Object tail, frame;
3159 /* A single call to window_loop won't do the job because it only
3160 considers frames on the current keyboard. So loop manually over
3161 frames, and handle each one. */
3162 FOR_EACH_FRAME (tail, frame)
3163 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3167 /* The following three routines are needed for running a window's
3168 configuration change hook. */
3169 static void
3170 run_funs (Lisp_Object funs)
3172 for (; CONSP (funs); funs = XCDR (funs))
3173 if (!EQ (XCAR (funs), Qt))
3174 call0 (XCAR (funs));
3177 static void
3178 select_window_norecord (Lisp_Object window)
3180 if (WINDOW_LIVE_P (window))
3181 Fselect_window (window, Qt);
3184 static void
3185 select_frame_norecord (Lisp_Object frame)
3187 if (FRAME_LIVE_P (XFRAME (frame)))
3188 Fselect_frame (frame, Qt);
3191 static void
3192 run_window_configuration_change_hook (struct frame *f)
3194 ptrdiff_t count = SPECPDL_INDEX ();
3195 Lisp_Object frame, global_wcch
3196 = Fdefault_value (Qwindow_configuration_change_hook);
3197 XSETFRAME (frame, f);
3199 if (NILP (Vrun_hooks)
3200 || !(f->can_x_set_window_size)
3201 || !(f->after_make_frame))
3202 return;
3204 /* Use the right buffer. Matters when running the local hooks. */
3205 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3207 record_unwind_current_buffer ();
3208 Fset_buffer (Fwindow_buffer (Qnil));
3211 if (SELECTED_FRAME () != f)
3213 record_unwind_protect (select_frame_norecord, selected_frame);
3214 select_frame_norecord (frame);
3217 /* Look for buffer-local values. */
3219 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3220 for (; CONSP (windows); windows = XCDR (windows))
3222 Lisp_Object window = XCAR (windows);
3223 Lisp_Object buffer = Fwindow_buffer (window);
3224 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3225 buffer)))
3227 ptrdiff_t inner_count = SPECPDL_INDEX ();
3228 record_unwind_protect (select_window_norecord, selected_window);
3229 select_window_norecord (window);
3230 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3231 buffer));
3232 unbind_to (inner_count, Qnil);
3237 run_funs (global_wcch);
3238 unbind_to (count, Qnil);
3241 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3242 Srun_window_configuration_change_hook, 0, 1, 0,
3243 doc: /* Run `window-configuration-change-hook' for FRAME.
3244 If FRAME is omitted or nil, it defaults to the selected frame. */)
3245 (Lisp_Object frame)
3247 run_window_configuration_change_hook (decode_live_frame (frame));
3248 return Qnil;
3251 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3252 Srun_window_scroll_functions, 0, 1, 0,
3253 doc: /* Run `window-scroll-functions' for WINDOW.
3254 If WINDOW is omitted or nil, it defaults to the selected window. */)
3255 (Lisp_Object window)
3257 if (! NILP (Vwindow_scroll_functions))
3258 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3259 Fmarker_position (decode_live_window (window)->start));
3260 return Qnil;
3264 /* Compare old and present pixel sizes of windows in tree rooted at W.
3265 Return true iff any of these windows differs in size. */
3267 static bool
3268 window_size_changed (struct window *w)
3270 if (w->pixel_width != w->pixel_width_before_size_change
3271 || w->pixel_height != w->pixel_height_before_size_change)
3272 return true;
3274 if (WINDOW_INTERNAL_P (w))
3276 w = XWINDOW (w->contents);
3277 while (w)
3279 if (window_size_changed (w))
3280 return true;
3282 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3286 return false;
3289 /* Set before size change pixel sizes of windows in tree rooted at W to
3290 their present pixel sizes. */
3292 static void
3293 window_set_before_size_change_sizes (struct window *w)
3295 w->pixel_width_before_size_change = w->pixel_width;
3296 w->pixel_height_before_size_change = w->pixel_height;
3298 if (WINDOW_INTERNAL_P (w))
3300 w = XWINDOW (w->contents);
3301 while (w)
3303 window_set_before_size_change_sizes (w);
3304 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3310 void
3311 run_window_size_change_functions (Lisp_Object frame)
3313 struct frame *f = XFRAME (frame);
3314 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3315 Lisp_Object functions = Vwindow_size_change_functions;
3317 if (FRAME_WINDOW_CONFIGURATION_CHANGED (f)
3318 /* Here we implicitly exclude the possibility that the height of
3319 FRAME and its minibuffer window both change leaving the height
3320 of FRAME's root window alone. */
3321 || window_size_changed (r))
3323 while (CONSP (functions))
3325 if (!EQ (XCAR (functions), Qt))
3326 safe_call1 (XCAR (functions), frame);
3327 functions = XCDR (functions);
3330 window_set_before_size_change_sizes (r);
3332 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3333 /* Record size of FRAME's minibuffer window too. */
3334 window_set_before_size_change_sizes
3335 (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
3337 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = false;
3342 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3343 to run hooks. See make_frame for a case where it's not allowed.
3344 KEEP_MARGINS_P means that the current margins, fringes, and
3345 scroll bar settings of the window are not reset from the buffer's
3346 local settings. */
3348 void
3349 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3350 bool run_hooks_p, bool keep_margins_p)
3352 struct window *w = XWINDOW (window);
3353 struct buffer *b = XBUFFER (buffer);
3354 ptrdiff_t count = SPECPDL_INDEX ();
3355 bool samebuf = EQ (buffer, w->contents);
3357 wset_buffer (w, buffer);
3359 if (EQ (window, selected_window))
3360 bset_last_selected_window (b, window);
3362 /* Let redisplay errors through. */
3363 b->display_error_modiff = 0;
3365 /* Update time stamps of buffer display. */
3366 if (INTEGERP (BVAR (b, display_count)))
3367 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3368 bset_display_time (b, Fcurrent_time ());
3370 w->window_end_pos = 0;
3371 w->window_end_vpos = 0;
3372 w->last_cursor_vpos = 0;
3374 if (!(keep_margins_p && samebuf))
3375 { /* If we're not actually changing the buffer, don't reset hscroll
3376 and vscroll. Resetting hscroll and vscroll here is problematic
3377 for things like image-mode and doc-view-mode since it resets
3378 the image's position whenever we resize the frame. */
3379 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3380 w->suspend_auto_hscroll = false;
3381 w->vscroll = 0;
3382 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3383 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3384 set_marker_restricted (w->start,
3385 make_number (b->last_window_start),
3386 buffer);
3387 w->start_at_line_beg = false;
3388 w->force_start = false;
3391 wset_redisplay (w);
3392 wset_update_mode_line (w);
3394 /* We must select BUFFER to run the window-scroll-functions and to look up
3395 the buffer-local value of Vwindow_point_insertion_type. */
3396 record_unwind_current_buffer ();
3397 Fset_buffer (buffer);
3399 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3400 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3402 if (!keep_margins_p)
3404 /* Set left and right marginal area width etc. from buffer. */
3405 set_window_fringes (w, BVAR (b, left_fringe_width),
3406 BVAR (b, right_fringe_width),
3407 BVAR (b, fringes_outside_margins));
3408 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3409 BVAR (b, vertical_scroll_bar_type),
3410 BVAR (b, scroll_bar_height),
3411 BVAR (b, horizontal_scroll_bar_type));
3412 set_window_margins (w, BVAR (b, left_margin_cols),
3413 BVAR (b, right_margin_cols));
3414 apply_window_adjustment (w);
3417 if (run_hooks_p)
3419 if (!NILP (Vwindow_scroll_functions))
3420 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3421 Fmarker_position (w->start));
3422 if (!samebuf)
3423 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3426 unbind_to (count, Qnil);
3429 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3430 doc: /* Make WINDOW display BUFFER-OR-NAME.
3431 WINDOW must be a live window and defaults to the selected one.
3432 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3434 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3435 display margins, fringe widths, and scroll bar settings are preserved;
3436 the default is to reset these from the local settings for BUFFER-OR-NAME
3437 or the frame defaults. Return nil.
3439 This function throws an error when WINDOW is strongly dedicated to its
3440 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3441 already display BUFFER-OR-NAME.
3443 This function runs `window-scroll-functions' before running
3444 `window-configuration-change-hook'. */)
3445 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3447 register Lisp_Object tem, buffer;
3448 register struct window *w = decode_live_window (window);
3450 XSETWINDOW (window, w);
3451 buffer = Fget_buffer (buffer_or_name);
3452 CHECK_BUFFER (buffer);
3453 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3454 error ("Attempt to display deleted buffer");
3456 tem = w->contents;
3457 if (NILP (tem))
3458 error ("Window is deleted");
3459 else
3461 if (!EQ (tem, buffer))
3463 if (EQ (w->dedicated, Qt))
3464 /* WINDOW is strongly dedicated to its buffer, signal an
3465 error. */
3466 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3467 else
3468 /* WINDOW is weakly dedicated to its buffer, reset
3469 dedication. */
3470 wset_dedicated (w, Qnil);
3472 call1 (Qrecord_window_buffer, window);
3475 unshow_buffer (w);
3478 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3480 return Qnil;
3483 static Lisp_Object
3484 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3486 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3489 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3490 0, 1, 0,
3491 doc: /* Force all windows to be updated on next redisplay.
3492 If optional arg OBJECT is a window, force redisplay of that window only.
3493 If OBJECT is a buffer or buffer name, force redisplay of all windows
3494 displaying that buffer. */)
3495 (Lisp_Object object)
3497 if (NILP (object))
3499 windows_or_buffers_changed = 29;
3500 update_mode_lines = 28;
3501 return Qt;
3504 if (WINDOWP (object))
3506 struct window *w = XWINDOW (object);
3507 mark_window_display_accurate (object, false);
3508 w->update_mode_line = true;
3509 if (BUFFERP (w->contents))
3510 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3511 update_mode_lines = 29;
3512 return Qt;
3515 if (STRINGP (object))
3516 object = Fget_buffer (object);
3517 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3518 && buffer_window_count (XBUFFER (object)))
3520 /* If buffer is live and shown in at least one window, find
3521 all windows showing this buffer and force update of them. */
3522 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3523 return NILP (object) ? Qnil : Qt;
3526 /* If nothing suitable was found, just return.
3527 We could signal an error, but this feature will typically be used
3528 asynchronously in timers or process sentinels, so we don't. */
3529 return Qnil;
3532 /* Obsolete since 24.3. */
3533 void
3534 temp_output_buffer_show (register Lisp_Object buf)
3536 register struct buffer *old = current_buffer;
3537 register Lisp_Object window;
3538 register struct window *w;
3540 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3542 Fset_buffer (buf);
3543 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3544 BEGV = BEG;
3545 ZV = Z;
3546 SET_PT (BEG);
3547 set_buffer_internal (old);
3549 if (!NILP (Vtemp_buffer_show_function))
3550 call1 (Vtemp_buffer_show_function, buf);
3551 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3553 if (!EQ (XWINDOW (window)->frame, selected_frame))
3554 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3555 Vminibuf_scroll_window = window;
3556 w = XWINDOW (window);
3557 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3558 w->suspend_auto_hscroll = false;
3559 set_marker_restricted_both (w->start, buf, BEG, BEG);
3560 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3561 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3563 /* Run temp-buffer-show-hook, with the chosen window selected
3564 and its buffer current. */
3566 ptrdiff_t count = SPECPDL_INDEX ();
3567 Lisp_Object prev_window, prev_buffer;
3568 prev_window = selected_window;
3569 XSETBUFFER (prev_buffer, old);
3571 /* Select the window that was chosen, for running the hook.
3572 Note: Both Fselect_window and select_window_norecord may
3573 set-buffer to the buffer displayed in the window,
3574 so we need to save the current buffer. --stef */
3575 record_unwind_protect (restore_buffer, prev_buffer);
3576 record_unwind_protect (select_window_norecord, prev_window);
3577 Fselect_window (window, Qt);
3578 Fset_buffer (w->contents);
3579 run_hook (Qtemp_buffer_show_hook);
3580 unbind_to (count, Qnil);
3585 /* Allocate basically initialized window. */
3587 static struct window *
3588 allocate_window (void)
3590 return ALLOCATE_ZEROED_PSEUDOVECTOR
3591 (struct window, current_matrix, PVEC_WINDOW);
3594 /* Make new window, have it replace WINDOW in window-tree, and make
3595 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3596 horizontal child). */
3597 static void
3598 make_parent_window (Lisp_Object window, bool horflag)
3600 Lisp_Object parent;
3601 register struct window *o, *p;
3603 o = XWINDOW (window);
3604 p = allocate_window ();
3605 memcpy ((char *) p + sizeof (struct vectorlike_header),
3606 (char *) o + sizeof (struct vectorlike_header),
3607 word_size * VECSIZE (struct window));
3608 /* P's buffer slot may change from nil to a buffer... */
3609 adjust_window_count (p, 1);
3610 XSETWINDOW (parent, p);
3612 p->sequence_number = ++sequence_number;
3614 replace_window (window, parent, true);
3616 wset_next (o, Qnil);
3617 wset_prev (o, Qnil);
3618 wset_parent (o, parent);
3619 /* ...but now P becomes an internal window. */
3620 wset_start (p, Qnil);
3621 wset_pointm (p, Qnil);
3622 wset_old_pointm (p, Qnil);
3623 wset_buffer (p, Qnil);
3624 wset_combination (p, horflag, window);
3625 wset_combination_limit (p, Qnil);
3626 wset_window_parameters (p, Qnil);
3629 /* Make new window from scratch. */
3630 Lisp_Object
3631 make_window (void)
3633 Lisp_Object window;
3634 register struct window *w;
3636 w = allocate_window ();
3637 /* Initialize Lisp data. Note that allocate_window initializes all
3638 Lisp data to nil, so do it only for slots which should not be nil. */
3639 wset_normal_lines (w, make_float (1.0));
3640 wset_normal_cols (w, make_float (1.0));
3641 wset_new_total (w, make_number (0));
3642 wset_new_normal (w, make_number (0));
3643 wset_new_pixel (w, make_number (0));
3644 wset_start (w, Fmake_marker ());
3645 wset_pointm (w, Fmake_marker ());
3646 wset_old_pointm (w, Fmake_marker ());
3647 wset_vertical_scroll_bar_type (w, Qt);
3648 wset_horizontal_scroll_bar_type (w, Qt);
3649 /* These Lisp fields are marked specially so they're not set to nil by
3650 allocate_window. */
3651 wset_prev_buffers (w, Qnil);
3652 wset_next_buffers (w, Qnil);
3654 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3655 non-Lisp data, so do it only for slots which should not be zero. */
3656 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3657 w->left_fringe_width = w->right_fringe_width = -1;
3658 w->mode_line_height = w->header_line_height = -1;
3659 #ifdef HAVE_WINDOW_SYSTEM
3660 w->phys_cursor_type = NO_CURSOR;
3661 w->phys_cursor_width = -1;
3662 #endif
3663 w->sequence_number = ++sequence_number;
3664 w->pixel_width_before_size_change = 0;
3665 w->pixel_height_before_size_change = 0;
3666 w->scroll_bar_width = -1;
3667 w->scroll_bar_height = -1;
3668 w->column_number_displayed = -1;
3669 /* Reset window_list. */
3670 Vwindow_list = Qnil;
3671 /* Return window. */
3672 XSETWINDOW (window, w);
3673 return window;
3676 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3677 doc: /* Set new pixel size of WINDOW to SIZE.
3678 WINDOW must be a valid window and defaults to the selected one.
3679 Return SIZE.
3681 Optional argument ADD non-nil means add SIZE to the new pixel size of
3682 WINDOW and return the sum.
3684 The new pixel size of WINDOW, if valid, will be shortly installed as
3685 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3686 `window-pixel-width').
3688 Note: This function does not operate on any child windows of WINDOW. */)
3689 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3691 struct window *w = decode_valid_window (window);
3692 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3693 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3695 CHECK_RANGED_INTEGER (size, size_min, size_max);
3696 if (NILP (add))
3697 wset_new_pixel (w, size);
3698 else
3699 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3701 return w->new_pixel;
3704 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3705 doc: /* Set new total size of WINDOW to SIZE.
3706 WINDOW must be a valid window and defaults to the selected one.
3707 Return SIZE.
3709 Optional argument ADD non-nil means add SIZE to the new total size of
3710 WINDOW and return the sum.
3712 The new total size of WINDOW, if valid, will be shortly installed as
3713 WINDOW's total height (see `window-total-height') or total width (see
3714 `window-total-width').
3716 Note: This function does not operate on any child windows of WINDOW. */)
3717 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3719 struct window *w = decode_valid_window (window);
3721 CHECK_NUMBER (size);
3722 if (NILP (add))
3723 wset_new_total (w, size);
3724 else
3725 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3727 return w->new_total;
3730 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3731 doc: /* Set new normal size of WINDOW to SIZE.
3732 WINDOW must be a valid window and defaults to the selected one.
3733 Return SIZE.
3735 The new normal size of WINDOW, if valid, will be shortly installed as
3736 WINDOW's normal size (see `window-normal-size').
3738 Note: This function does not operate on any child windows of WINDOW. */)
3739 (Lisp_Object window, Lisp_Object size)
3741 wset_new_normal (decode_valid_window (window), size);
3742 return size;
3745 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3746 to w->new_pixel would result in correct heights (widths)
3747 for window W and recursively all child windows of W.
3749 Note: This function does not check any of `window-fixed-size-p',
3750 `window-min-height' or `window-min-width'. It does check that window
3751 sizes do not drop below one line (two columns). */
3752 static bool
3753 window_resize_check (struct window *w, bool horflag)
3755 struct frame *f = XFRAME (w->frame);
3756 struct window *c;
3758 if (WINDOW_VERTICAL_COMBINATION_P (w))
3759 /* W is a vertical combination. */
3761 c = XWINDOW (w->contents);
3762 if (horflag)
3763 /* All child windows of W must have the same width as W. */
3765 while (c)
3767 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3768 || !window_resize_check (c, horflag))
3769 return false;
3771 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3774 return true;
3776 else
3777 /* The sum of the heights of the child windows of W must equal
3778 W's height. */
3780 int remaining_pixels = XINT (w->new_pixel);
3782 while (c)
3784 if (!window_resize_check (c, horflag))
3785 return false;
3787 remaining_pixels -= XINT (c->new_pixel);
3788 if (remaining_pixels < 0)
3789 return false;
3790 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3793 return remaining_pixels == 0;
3796 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3797 /* W is a horizontal combination. */
3799 c = XWINDOW (w->contents);
3800 if (horflag)
3801 /* The sum of the widths of the child windows of W must equal W's
3802 width. */
3804 int remaining_pixels = XINT (w->new_pixel);
3806 while (c)
3808 if (!window_resize_check (c, horflag))
3809 return false;
3811 remaining_pixels -= XINT (c->new_pixel);
3812 if (remaining_pixels < 0)
3813 return false;
3814 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3817 return remaining_pixels == 0;
3819 else
3820 /* All child windows of W must have the same height as W. */
3822 while (c)
3824 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3825 || !window_resize_check (c, horflag))
3826 return false;
3828 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3831 return true;
3834 else
3835 /* A leaf window. Make sure it's not too small. The following
3836 hardcodes the values of `window-safe-min-width' (2) and
3837 `window-safe-min-height' (1) which are defined in window.el. */
3838 return (XINT (w->new_pixel) >= (horflag
3839 ? (2 * FRAME_COLUMN_WIDTH (f))
3840 : FRAME_LINE_HEIGHT (f)));
3844 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3845 w->new_pixel for window W and recursively all child windows of W.
3846 Also calculate and assign the new vertical (horizontal) pixel start
3847 positions of each of these windows.
3849 This function does not perform any error checks. Make sure you have
3850 run window_resize_check on W before applying this function. */
3851 static void
3852 window_resize_apply (struct window *w, bool horflag)
3854 struct window *c;
3855 int edge;
3856 int unit = (horflag
3857 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3858 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3860 /* Note: Assigning new_normal requires that the new total size of the
3861 parent window has been set *before*. */
3862 if (horflag)
3864 w->pixel_width = XFASTINT (w->new_pixel);
3865 w->total_cols = w->pixel_width / unit;
3866 if (NUMBERP (w->new_normal))
3867 wset_normal_cols (w, w->new_normal);
3869 edge = w->pixel_left;
3871 else
3873 w->pixel_height = XFASTINT (w->new_pixel);
3874 w->total_lines = w->pixel_height / unit;
3875 if (NUMBERP (w->new_normal))
3876 wset_normal_lines (w, w->new_normal);
3878 edge = w->pixel_top;
3881 if (WINDOW_VERTICAL_COMBINATION_P (w))
3882 /* W is a vertical combination. */
3884 c = XWINDOW (w->contents);
3885 while (c)
3887 if (horflag)
3889 c->pixel_left = edge;
3890 c->left_col = edge / unit;
3892 else
3894 c->pixel_top = edge;
3895 c->top_line = edge / unit;
3897 window_resize_apply (c, horflag);
3898 if (!horflag)
3899 edge = edge + c->pixel_height;
3901 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3904 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3905 /* W is a horizontal combination. */
3907 c = XWINDOW (w->contents);
3908 while (c)
3910 if (horflag)
3912 c->pixel_left = edge;
3913 c->left_col = edge / unit;
3915 else
3917 c->pixel_top = edge;
3918 c->top_line = edge / unit;
3921 window_resize_apply (c, horflag);
3922 if (horflag)
3923 edge = edge + c->pixel_width;
3925 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3928 else
3929 /* Bug#15957. */
3930 w->window_end_valid = false;
3934 /* Set w->total_lines (w->total_cols if HORFLAG) to
3935 w->new_total for window W and recursively all child windows of W.
3936 Also calculate and assign the new vertical (horizontal) start
3937 positions of each of these windows. */
3938 static void
3939 window_resize_apply_total (struct window *w, bool horflag)
3941 struct window *c;
3942 int edge;
3944 /* Note: Assigning new_normal requires that the new total size of the
3945 parent window has been set *before*. */
3946 if (horflag)
3948 w->total_cols = XFASTINT (w->new_total);
3949 edge = w->left_col;
3951 else
3953 w->total_lines = XFASTINT (w->new_total);
3954 edge = w->top_line;
3957 if (WINDOW_VERTICAL_COMBINATION_P (w))
3958 /* W is a vertical combination. */
3960 c = XWINDOW (w->contents);
3961 while (c)
3963 if (horflag)
3964 c->left_col = edge;
3965 else
3966 c->top_line = edge;
3968 window_resize_apply_total (c, horflag);
3969 if (!horflag)
3970 edge = edge + c->total_lines;
3972 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3975 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3976 /* W is a horizontal combination. */
3978 c = XWINDOW (w->contents);
3979 while (c)
3981 if (horflag)
3982 c->left_col = edge;
3983 else
3984 c->top_line = edge;
3986 window_resize_apply_total (c, horflag);
3987 if (horflag)
3988 edge = edge + c->total_cols;
3990 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3995 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3996 doc: /* Apply requested size values for window-tree of FRAME.
3997 If FRAME is omitted or nil, it defaults to the selected frame.
3999 Optional argument HORIZONTAL omitted or nil means apply requested
4000 height values. HORIZONTAL non-nil means apply requested width values.
4002 The requested size values are those set by `set-window-new-pixel' and
4003 `set-window-new-normal'. This function checks whether the requested
4004 values sum up to a valid window layout, recursively assigns the new
4005 sizes of all child windows and calculates and assigns the new start
4006 positions of these windows.
4008 Return t if the requested values have been applied correctly, nil
4009 otherwise.
4011 Note: This function does not check any of `window-fixed-size-p',
4012 `window-min-height' or `window-min-width'. All these checks have to
4013 be applied on the Elisp level. */)
4014 (Lisp_Object frame, Lisp_Object horizontal)
4016 struct frame *f = decode_live_frame (frame);
4017 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4018 bool horflag = !NILP (horizontal);
4020 if (!window_resize_check (r, horflag)
4021 || (XINT (r->new_pixel)
4022 != (horflag ? r->pixel_width : r->pixel_height)))
4023 return Qnil;
4025 block_input ();
4026 window_resize_apply (r, horflag);
4028 fset_redisplay (f);
4030 adjust_frame_glyphs (f);
4031 unblock_input ();
4033 return Qt;
4037 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4038 doc: /* Apply requested total size values for window-tree of FRAME.
4039 If FRAME is omitted or nil, it defaults to the selected frame.
4041 This function does not assign pixel or normal size values. You should
4042 have run `window-resize-apply' before running this.
4044 Optional argument HORIZONTAL omitted or nil means apply requested
4045 height values. HORIZONTAL non-nil means apply requested width
4046 values. */)
4047 (Lisp_Object frame, Lisp_Object horizontal)
4049 struct frame *f = decode_live_frame (frame);
4050 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4052 block_input ();
4053 /* Necessary when deleting the top-/or leftmost window. */
4054 r->left_col = 0;
4055 r->top_line = FRAME_TOP_MARGIN (f);
4056 window_resize_apply_total (r, !NILP (horizontal));
4057 /* Handle the mini window. */
4058 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4060 struct window *m = XWINDOW (f->minibuffer_window);
4062 if (NILP (horizontal))
4064 m->top_line = r->top_line + r->total_lines;
4065 m->total_lines = XFASTINT (m->new_total);
4067 else
4068 m->total_cols = XFASTINT (m->new_total);
4071 unblock_input ();
4073 return Qt;
4077 /* Resize frame F's windows when F's width or height is set to SIZE.
4078 If HORFLAG is zero, F's width was set to SIZE, otherwise its height
4079 was set. SIZE is interpreted in F's canonical character units
4080 (a.k.a. "columns" or "lines"), unless PIXELWISE is non-zero, which
4081 means to interpret SIZE in pixel units. */
4082 void
4083 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4085 Lisp_Object root = f->root_window;
4086 struct window *r = XWINDOW (root);
4087 Lisp_Object mini = f->minibuffer_window;
4088 struct window *m;
4089 /* old_size is the old size of the frame's root window. */
4090 int old_size = horflag ? r->total_cols : r->total_lines;
4091 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4092 /* new_size is the new size of the frame's root window. */
4093 int new_size, new_pixel_size;
4094 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4096 /* Don't let the size drop below one unit. This is more comforting
4097 when we are called from x_set_tool_bar_lines since the latter may
4098 have implicitly given us a zero or negative height. */
4099 if (pixelwise)
4101 /* Note: This does not include the size for internal borders
4102 since these are not part of the frame's text area. */
4103 new_pixel_size = max (horflag
4104 ? size
4105 : (size
4106 - ((FRAME_HAS_MINIBUF_P (f)
4107 && !FRAME_MINIBUF_ONLY_P (f))
4108 ? FRAME_LINE_HEIGHT (f) : 0)),
4109 unit);
4110 new_size = new_pixel_size / unit;
4112 else
4114 new_size = max (size - (!horflag
4115 && FRAME_HAS_MINIBUF_P (f)
4116 && !FRAME_MINIBUF_ONLY_P (f)),
4118 new_pixel_size = new_size * unit;
4121 if (new_pixel_size == old_pixel_size
4122 && (horflag || r->pixel_top == FRAME_TOP_MARGIN_HEIGHT (f)))
4124 else if (WINDOW_LEAF_P (r))
4125 /* For a leaf root window just set the size. */
4126 if (horflag)
4128 r->total_cols = new_size;
4129 r->pixel_width = new_pixel_size;
4131 else
4133 r->top_line = FRAME_TOP_MARGIN (f);
4134 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4136 r->total_lines = new_size;
4137 r->pixel_height = new_pixel_size;
4139 else
4141 Lisp_Object delta;
4143 if (!horflag)
4145 r->top_line = FRAME_TOP_MARGIN (f);
4146 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4149 if (pixelwise)
4150 XSETINT (delta, new_pixel_size - old_pixel_size);
4151 else
4152 XSETINT (delta, new_size - old_size);
4154 /* Try a "normal" resize first. */
4155 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4156 pixelwise ? Qt : Qnil);
4157 if (window_resize_check (r, horflag)
4158 && new_pixel_size == XINT (r->new_pixel))
4160 window_resize_apply (r, horflag);
4161 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4163 else
4165 /* Try with "reasonable" minimum sizes next. */
4166 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4167 pixelwise ? Qt : Qnil);
4168 if (window_resize_check (r, horflag)
4169 && new_pixel_size == XINT (r->new_pixel))
4171 window_resize_apply (r, horflag);
4172 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4177 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4179 m = XWINDOW (mini);
4180 if (horflag)
4182 m->total_cols = new_size;
4183 m->pixel_width = new_pixel_size;
4185 else
4187 /* Are we sure we always want 1 line here? */
4188 m->total_lines = 1;
4189 m->pixel_height = FRAME_LINE_HEIGHT (f);
4190 m->top_line = r->top_line + r->total_lines;
4191 m->pixel_top = r->pixel_top + r->pixel_height;
4195 fset_redisplay (f);
4199 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4200 doc: /* Split window OLD.
4201 Second argument PIXEL-SIZE specifies the number of pixels of the
4202 new window. It must be a positive integer.
4204 Third argument SIDE nil (or `below') specifies that the new window shall
4205 be located below WINDOW. SIDE `above' means the new window shall be
4206 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4207 height of the new window including space reserved for the mode and/or
4208 header line.
4210 SIDE t (or `right') specifies that the new window shall be located on
4211 the right side of WINDOW. SIDE `left' means the new window shall be
4212 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4213 width of the new window including space reserved for fringes and the
4214 scrollbar or a divider column.
4216 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4217 according to the SIDE argument.
4219 The new pixel and normal sizes of all involved windows must have been
4220 set correctly. See the code of `split-window' for how this is done. */)
4221 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4223 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4224 parent window or an internal window we have to install as OLD's new
4225 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4226 provided OLD is a leaf window, or to the frame's selected window.
4227 NEW (*n) is the new window created with some parameters taken from
4228 REFERENCE (*r). */
4229 Lisp_Object new, frame, reference;
4230 struct window *o, *p, *n, *r, *c;
4231 struct frame *f;
4232 bool horflag
4233 /* HORFLAG is true when we split side-by-side, false otherwise. */
4234 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4236 CHECK_WINDOW (old);
4237 o = XWINDOW (old);
4238 frame = WINDOW_FRAME (o);
4239 f = XFRAME (frame);
4241 CHECK_NUMBER (pixel_size);
4242 EMACS_INT total_size
4243 = XINT (pixel_size) / (horflag
4244 ? FRAME_COLUMN_WIDTH (f)
4245 : FRAME_LINE_HEIGHT (f));
4247 /* Set combination_limit if we have to make a new parent window.
4248 We do that if either `window-combination-limit' is t, or OLD has no
4249 parent, or OLD is ortho-combined. */
4250 bool combination_limit
4251 = (EQ (Vwindow_combination_limit, Qt)
4252 || NILP (o->parent)
4253 || (horflag
4254 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4255 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4257 /* We need a live reference window to initialize some parameters. */
4258 if (WINDOW_LIVE_P (old))
4259 /* OLD is live, use it as reference window. */
4260 reference = old;
4261 else
4262 /* Use the frame's selected window as reference window. */
4263 reference = FRAME_SELECTED_WINDOW (f);
4264 r = XWINDOW (reference);
4266 /* The following bugs are caught by `split-window'. */
4267 if (MINI_WINDOW_P (o))
4268 error ("Attempt to split minibuffer window");
4269 else if (total_size < (horflag ? 2 : 1))
4270 error ("Size of new window too small (after split)");
4271 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4272 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4273 proportionally. */
4275 p = XWINDOW (o->parent);
4276 /* Temporarily pretend we split the parent window. */
4277 wset_new_pixel
4278 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4279 - XINT (pixel_size)));
4280 if (!window_resize_check (p, horflag))
4281 error ("Window sizes don't fit");
4282 else
4283 /* Undo the temporary pretension. */
4284 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4286 else
4288 if (!window_resize_check (o, horflag))
4289 error ("Resizing old window failed");
4290 else if (XINT (pixel_size) + XINT (o->new_pixel)
4291 != (horflag ? o->pixel_width : o->pixel_height))
4292 error ("Sum of sizes of old and new window don't fit");
4295 /* This is our point of no return. */
4296 if (combination_limit)
4298 /* Save the old value of o->normal_cols/lines. It gets corrupted
4299 by make_parent_window and we need it below for assigning it to
4300 p->new_normal. */
4301 Lisp_Object new_normal
4302 = horflag ? o->normal_cols : o->normal_lines;
4304 make_parent_window (old, horflag);
4305 p = XWINDOW (o->parent);
4306 if (EQ (Vwindow_combination_limit, Qt))
4307 /* Store t in the new parent's combination_limit slot to avoid
4308 that its children get merged into another window. */
4309 wset_combination_limit (p, Qt);
4310 /* These get applied below. */
4311 wset_new_pixel
4312 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4313 wset_new_total
4314 (p, make_number (horflag ? o->total_cols : o->total_lines));
4315 wset_new_normal (p, new_normal);
4317 else
4318 p = XWINDOW (o->parent);
4320 fset_redisplay (f);
4321 new = make_window ();
4322 n = XWINDOW (new);
4323 wset_frame (n, frame);
4324 wset_parent (n, o->parent);
4326 if (EQ (side, Qabove) || EQ (side, Qleft))
4328 wset_prev (n, o->prev);
4329 if (NILP (n->prev))
4330 wset_combination (p, horflag, new);
4331 else
4332 wset_next (XWINDOW (n->prev), new);
4333 wset_next (n, old);
4334 wset_prev (o, new);
4336 else
4338 wset_next (n, o->next);
4339 if (!NILP (n->next))
4340 wset_prev (XWINDOW (n->next), new);
4341 wset_prev (n, old);
4342 wset_next (o, new);
4345 n->window_end_valid = false;
4346 n->last_cursor_vpos = 0;
4348 /* Get special geometry settings from reference window. */
4349 n->left_margin_cols = r->left_margin_cols;
4350 n->right_margin_cols = r->right_margin_cols;
4351 n->left_fringe_width = r->left_fringe_width;
4352 n->right_fringe_width = r->right_fringe_width;
4353 n->fringes_outside_margins = r->fringes_outside_margins;
4354 n->scroll_bar_width = r->scroll_bar_width;
4355 n->scroll_bar_height = r->scroll_bar_height;
4356 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4357 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4359 /* Directly assign orthogonal coordinates and sizes. */
4360 if (horflag)
4362 n->pixel_top = o->pixel_top;
4363 n->top_line = o->top_line;
4364 n->pixel_height = o->pixel_height;
4365 n->total_lines = o->total_lines;
4367 else
4369 n->pixel_left = o->pixel_left;
4370 n->left_col = o->left_col;
4371 n->pixel_width = o->pixel_width;
4372 n->total_cols = o->total_cols;
4375 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4376 get them ready here. */
4377 wset_new_pixel (n, pixel_size);
4378 EMACS_INT sum = 0;
4379 c = XWINDOW (p->contents);
4380 while (c)
4382 if (c != n)
4383 sum = sum + XINT (c->new_total);
4384 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4386 wset_new_total (n, make_number ((horflag
4387 ? p->total_cols
4388 : p->total_lines)
4389 - sum));
4390 wset_new_normal (n, normal_size);
4392 block_input ();
4393 window_resize_apply (p, horflag);
4394 adjust_frame_glyphs (f);
4395 /* Set buffer of NEW to buffer of reference window. Don't run
4396 any hooks. */
4397 set_window_buffer (new, r->contents, false, true);
4398 unblock_input ();
4400 /* Maybe we should run the scroll functions in Elisp (which already
4401 runs the configuration change hook). */
4402 if (! NILP (Vwindow_scroll_functions))
4403 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4404 Fmarker_position (n->start));
4405 /* Return NEW. */
4406 return new;
4410 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4411 doc: /* Remove WINDOW from its frame.
4412 WINDOW defaults to the selected window. Return nil.
4413 Signal an error when WINDOW is the only window on its frame. */)
4414 (Lisp_Object window)
4416 Lisp_Object parent, sibling, frame, root;
4417 struct window *w, *p, *s, *r;
4418 struct frame *f;
4419 bool horflag, before_sibling = false;
4421 w = decode_any_window (window);
4422 XSETWINDOW (window, w);
4423 if (NILP (w->contents))
4424 /* It's a no-op to delete an already deleted window. */
4425 return Qnil;
4427 parent = w->parent;
4428 if (NILP (parent))
4429 /* Never delete a minibuffer or frame root window. */
4430 error ("Attempt to delete minibuffer or sole ordinary window");
4431 else if (NILP (w->prev) && NILP (w->next))
4432 /* Rather bow out here, this case should be handled on the Elisp
4433 level. */
4434 error ("Attempt to delete sole window of parent");
4436 p = XWINDOW (parent);
4437 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4439 frame = WINDOW_FRAME (w);
4440 f = XFRAME (frame);
4442 root = FRAME_ROOT_WINDOW (f);
4443 r = XWINDOW (root);
4445 /* Unlink WINDOW from window tree. */
4446 if (NILP (w->prev))
4447 /* Get SIBLING below (on the right of) WINDOW. */
4449 /* before_sibling means WINDOW is the first child of its
4450 parent and thus before the sibling. */
4451 before_sibling = true;
4452 sibling = w->next;
4453 s = XWINDOW (sibling);
4454 wset_prev (s, Qnil);
4455 wset_combination (p, horflag, sibling);
4457 else
4458 /* Get SIBLING above (on the left of) WINDOW. */
4460 sibling = w->prev;
4461 s = XWINDOW (sibling);
4462 wset_next (s, w->next);
4463 if (!NILP (s->next))
4464 wset_prev (XWINDOW (s->next), sibling);
4467 if (window_resize_check (r, horflag)
4468 && (XINT (r->new_pixel)
4469 == (horflag ? r->pixel_width : r->pixel_height)))
4470 /* We can delete WINDOW now. */
4473 /* Block input. */
4474 block_input ();
4475 xwidget_view_delete_all_in_window (w);
4476 window_resize_apply (p, horflag);
4477 /* If this window is referred to by the dpyinfo's mouse
4478 highlight, invalidate that slot to be safe (Bug#9904). */
4479 if (!FRAME_INITIAL_P (f))
4481 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4483 if (EQ (hlinfo->mouse_face_window, window))
4484 hlinfo->mouse_face_window = Qnil;
4487 fset_redisplay (f);
4488 Vwindow_list = Qnil;
4490 wset_next (w, Qnil); /* Don't delete w->next too. */
4491 free_window_matrices (w);
4493 if (WINDOWP (w->contents))
4495 delete_all_child_windows (w->contents);
4496 wset_combination (w, false, Qnil);
4498 else
4500 unshow_buffer (w);
4501 unchain_marker (XMARKER (w->pointm));
4502 unchain_marker (XMARKER (w->old_pointm));
4503 unchain_marker (XMARKER (w->start));
4504 wset_buffer (w, Qnil);
4507 if (NILP (s->prev) && NILP (s->next))
4508 /* A matrjoshka where SIBLING has become the only child of
4509 PARENT. */
4511 /* Put SIBLING into PARENT's place. */
4512 replace_window (parent, sibling, false);
4513 /* Have SIBLING inherit the following three slot values from
4514 PARENT (the combination_limit slot is not inherited). */
4515 wset_normal_cols (s, p->normal_cols);
4516 wset_normal_lines (s, p->normal_lines);
4517 /* Mark PARENT as deleted. */
4518 wset_combination (p, false, Qnil);
4519 /* Try to merge SIBLING into its new parent. */
4520 recombine_windows (sibling);
4523 adjust_frame_glyphs (f);
4525 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4526 /* We deleted the frame's selected window. */
4528 /* Use the frame's first window as fallback ... */
4529 Lisp_Object new_selected_window = Fframe_first_window (frame);
4530 /* ... but preferably use its most recently used window. */
4531 Lisp_Object mru_window;
4533 /* `get-mru-window' might fail for some reason so play it safe
4534 - promote the first window _without recording it_ first. */
4535 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4536 Fselect_window (new_selected_window, Qt);
4537 else
4538 fset_selected_window (f, new_selected_window);
4540 unblock_input ();
4542 /* Now look whether `get-mru-window' gets us something. */
4543 mru_window = call1 (Qget_mru_window, frame);
4544 if (WINDOW_LIVE_P (mru_window)
4545 && EQ (XWINDOW (mru_window)->frame, frame))
4546 new_selected_window = mru_window;
4548 /* If all ended up well, we now promote the mru window. */
4549 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4550 Fselect_window (new_selected_window, Qnil);
4551 else
4552 fset_selected_window (f, new_selected_window);
4554 else
4555 unblock_input ();
4557 else
4558 /* We failed: Relink WINDOW into window tree. */
4560 if (before_sibling)
4562 wset_prev (s, window);
4563 wset_combination (p, horflag, window);
4565 else
4567 wset_next (s, window);
4568 if (!NILP (w->next))
4569 wset_prev (XWINDOW (w->next), window);
4571 error ("Deletion failed");
4574 return Qnil;
4577 /***********************************************************************
4578 Resizing Mini-Windows
4579 ***********************************************************************/
4581 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4582 can. */
4583 void
4584 grow_mini_window (struct window *w, int delta, bool pixelwise)
4586 struct frame *f = XFRAME (w->frame);
4587 struct window *r;
4588 Lisp_Object root, height;
4589 int line_height, pixel_height;
4591 eassert (MINI_WINDOW_P (w));
4592 eassert (delta >= 0);
4594 if (delta > 0)
4596 root = FRAME_ROOT_WINDOW (f);
4597 r = XWINDOW (root);
4598 height = call3 (Qwindow__resize_root_window_vertically,
4599 root, make_number (- delta), pixelwise ? Qt : Qnil);
4600 if (INTEGERP (height) && window_resize_check (r, false))
4602 block_input ();
4603 window_resize_apply (r, false);
4605 if (pixelwise)
4607 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4608 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4610 else
4612 line_height = min (-XINT (height),
4613 ((INT_MAX - w->pixel_height)
4614 / FRAME_LINE_HEIGHT (f)));
4615 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4618 /* Grow the mini-window. */
4619 w->pixel_top = r->pixel_top + r->pixel_height;
4620 w->top_line = r->top_line + r->total_lines;
4621 /* Make sure the mini-window has always at least one line. */
4622 w->pixel_height = max (w->pixel_height + pixel_height,
4623 FRAME_LINE_HEIGHT (f));
4624 w->total_lines = max (w->total_lines + line_height, 1);
4626 /* Enforce full redisplay of the frame. */
4627 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4628 fset_redisplay (f);
4629 adjust_frame_glyphs (f);
4630 unblock_input ();
4632 else
4633 error ("Failed to grow minibuffer window");
4638 /* Shrink mini-window W to one line. */
4639 void
4640 shrink_mini_window (struct window *w, bool pixelwise)
4642 struct frame *f = XFRAME (w->frame);
4643 struct window *r;
4644 Lisp_Object root, delta;
4645 EMACS_INT height, unit;
4647 eassert (MINI_WINDOW_P (w));
4649 height = pixelwise ? w->pixel_height : w->total_lines;
4650 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4651 if (height > unit)
4653 root = FRAME_ROOT_WINDOW (f);
4654 r = XWINDOW (root);
4655 delta = call3 (Qwindow__resize_root_window_vertically,
4656 root, make_number (height - unit),
4657 pixelwise ? Qt : Qnil);
4658 if (INTEGERP (delta) && window_resize_check (r, false))
4660 block_input ();
4661 window_resize_apply (r, false);
4663 /* Shrink the mini-window. */
4664 w->top_line = r->top_line + r->total_lines;
4665 w->total_lines = 1;
4666 w->pixel_top = r->pixel_top + r->pixel_height;
4667 w->pixel_height = FRAME_LINE_HEIGHT (f);
4668 /* Enforce full redisplay of the frame. */
4669 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4670 fset_redisplay (f);
4671 adjust_frame_glyphs (f);
4672 unblock_input ();
4674 /* If the above failed for whatever strange reason we must make a
4675 one window frame here. The same routine will be needed when
4676 shrinking the frame (and probably when making the initial
4677 *scratch* window). For the moment leave things as they are. */
4678 else
4679 error ("Failed to shrink minibuffer window");
4683 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4684 doc: /* Resize minibuffer window WINDOW. */)
4685 (Lisp_Object window)
4687 struct window *w = XWINDOW (window);
4688 struct window *r;
4689 struct frame *f;
4690 int height;
4692 CHECK_WINDOW (window);
4693 f = XFRAME (w->frame);
4695 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4696 error ("Not a valid minibuffer window");
4697 else if (FRAME_MINIBUF_ONLY_P (f))
4698 error ("Cannot resize a minibuffer-only frame");
4700 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4701 height = r->pixel_height + w->pixel_height;
4702 if (window_resize_check (r, false)
4703 && XINT (w->new_pixel) > 0
4704 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4706 block_input ();
4707 window_resize_apply (r, false);
4709 w->pixel_height = XFASTINT (w->new_pixel);
4710 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4711 w->pixel_top = r->pixel_top + r->pixel_height;
4712 w->top_line = r->top_line + r->total_lines;
4714 fset_redisplay (f);
4715 adjust_frame_glyphs (f);
4716 unblock_input ();
4717 return Qt;
4719 else
4720 error ("Failed to resize minibuffer window");
4723 /* Mark window cursors off for all windows in the window tree rooted
4724 at W by setting their phys_cursor_on_p flag to zero. Called from
4725 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4726 the frame are cleared. */
4728 void
4729 mark_window_cursors_off (struct window *w)
4731 while (w)
4733 if (WINDOWP (w->contents))
4734 mark_window_cursors_off (XWINDOW (w->contents));
4735 else
4736 w->phys_cursor_on_p = false;
4738 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4743 /* Return number of lines of text (not counting mode lines) in W. */
4746 window_internal_height (struct window *w)
4748 int ht = w->total_lines;
4750 if (!MINI_WINDOW_P (w))
4752 if (!NILP (w->parent)
4753 || WINDOWP (w->contents)
4754 || !NILP (w->next)
4755 || !NILP (w->prev)
4756 || WINDOW_WANTS_MODELINE_P (w))
4757 --ht;
4759 if (WINDOW_WANTS_HEADER_LINE_P (w))
4760 --ht;
4763 return ht;
4767 /************************************************************************
4768 Window Scrolling
4769 ***********************************************************************/
4771 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4772 N screen-fulls, which is defined as the height of the window minus
4773 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4774 instead. Negative values of N mean scroll down. NOERROR
4775 means don't signal an error if we try to move over BEGV or ZV,
4776 respectively. */
4778 static void
4779 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4781 ptrdiff_t count = SPECPDL_INDEX ();
4783 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4785 wset_redisplay (XWINDOW (window));
4787 if (whole && Vfast_but_imprecise_scrolling)
4788 specbind (Qfontification_functions, Qnil);
4790 /* If we must, use the pixel-based version which is much slower than
4791 the line-based one but can handle varying line heights. */
4792 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4793 window_scroll_pixel_based (window, n, whole, noerror);
4794 else
4795 window_scroll_line_based (window, n, whole, noerror);
4797 unbind_to (count, Qnil);
4799 /* Bug#15957. */
4800 XWINDOW (window)->window_end_valid = false;
4803 /* Compute scroll margin for WINDOW.
4804 We scroll when point is within this distance from the top or bottom
4805 of the window. The result is measured in lines or in pixels
4806 depending on the second parameter. */
4808 window_scroll_margin (struct window *window, enum margin_unit unit)
4810 if (scroll_margin > 0)
4812 int frame_line_height = default_line_pixel_height (window);
4813 int window_lines = window_box_height (window) / frame_line_height;
4815 double ratio = 0.25;
4816 if (FLOATP (Vmaximum_scroll_margin))
4818 ratio = XFLOAT_DATA (Vmaximum_scroll_margin);
4819 ratio = max (0.0, ratio);
4820 ratio = min (ratio, 0.5);
4822 int max_margin = min ((window_lines - 1)/2,
4823 (int) (window_lines * ratio));
4824 int margin = clip_to_bounds (0, scroll_margin, max_margin);
4825 return (unit == MARGIN_IN_PIXELS)
4826 ? margin * frame_line_height
4827 : margin;
4829 else
4830 return 0;
4834 /* Implementation of window_scroll that works based on pixel line
4835 heights. See the comment of window_scroll for parameter
4836 descriptions. */
4838 static void
4839 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4841 struct it it;
4842 struct window *w = XWINDOW (window);
4843 struct text_pos start;
4844 int this_scroll_margin;
4845 /* True if we fiddled the window vscroll field without really scrolling. */
4846 bool vscrolled = false;
4847 int x, y, rtop, rbot, rowh, vpos;
4848 void *itdata = NULL;
4849 int frame_line_height = default_line_pixel_height (w);
4850 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4851 Fwindow_old_point (window)));
4853 SET_TEXT_POS_FROM_MARKER (start, w->start);
4854 /* Scrolling a minibuffer window via scroll bar when the echo area
4855 shows long text sometimes resets the minibuffer contents behind
4856 our backs. Also, someone might narrow-to-region and immediately
4857 call a scroll function. */
4858 if (CHARPOS (start) > ZV || CHARPOS (start) < BEGV)
4859 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4861 /* If PT is not visible in WINDOW, move back one half of
4862 the screen. Allow PT to be partially visible, otherwise
4863 something like (scroll-down 1) with PT in the line before
4864 the partially visible one would recenter. */
4866 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4868 itdata = bidi_shelve_cache ();
4869 /* Move backward half the height of the window. Performance note:
4870 vmotion used here is about 10% faster, but would give wrong
4871 results for variable height lines. */
4872 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4873 it.current_y = it.last_visible_y;
4874 move_it_vertically_backward (&it, window_box_height (w) / 2);
4876 /* The function move_iterator_vertically may move over more than
4877 the specified y-distance. If it->w is small, e.g. a
4878 mini-buffer window, we may end up in front of the window's
4879 display area. Start displaying at the start of the line
4880 containing PT in this case. */
4881 if (it.current_y <= 0)
4883 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4884 move_it_vertically_backward (&it, 0);
4885 it.current_y = 0;
4888 start = it.current.pos;
4889 bidi_unshelve_cache (itdata, false);
4891 else if (auto_window_vscroll_p)
4893 if (rtop || rbot) /* Partially visible. */
4895 int px;
4896 int dy = frame_line_height;
4897 /* In the below we divide the window box height by the
4898 frame's line height to make the result predictable when
4899 the window box is not an integral multiple of the line
4900 height. This is important to ensure we get back to the
4901 same position when scrolling up, then down. */
4902 if (whole)
4903 dy = max ((window_box_height (w) / dy
4904 - next_screen_context_lines) * dy,
4905 dy);
4906 dy *= n;
4908 if (n < 0)
4910 /* Only vscroll backwards if already vscrolled forwards. */
4911 if (w->vscroll < 0 && rtop > 0)
4913 px = max (0, -w->vscroll - min (rtop, -dy));
4914 Fset_window_vscroll (window, make_number (px), Qt);
4915 return;
4918 if (n > 0)
4920 /* Do vscroll if already vscrolled or only display line. */
4921 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4923 px = max (0, -w->vscroll + min (rbot, dy));
4924 Fset_window_vscroll (window, make_number (px), Qt);
4925 return;
4928 /* Maybe modify window start instead of scrolling. */
4929 if (rbot > 0 || w->vscroll < 0)
4931 ptrdiff_t spos;
4933 Fset_window_vscroll (window, make_number (0), Qt);
4934 /* If there are other text lines above the current row,
4935 move window start to current row. Else to next row. */
4936 if (rbot > 0)
4937 spos = XINT (Fline_beginning_position (Qnil));
4938 else
4939 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4940 set_marker_restricted (w->start, make_number (spos),
4941 w->contents);
4942 w->start_at_line_beg = true;
4943 wset_update_mode_line (w);
4944 /* Set force_start so that redisplay_window will run the
4945 window-scroll-functions. */
4946 w->force_start = true;
4947 return;
4951 /* Cancel previous vscroll. */
4952 Fset_window_vscroll (window, make_number (0), Qt);
4955 itdata = bidi_shelve_cache ();
4956 /* If scroll_preserve_screen_position is non-nil, we try to set
4957 point in the same window line as it is now, so get that line. */
4958 if (!NILP (Vscroll_preserve_screen_position))
4960 /* We preserve the goal pixel coordinate across consecutive
4961 calls to scroll-up, scroll-down and other commands that
4962 have the `scroll-command' property. This avoids the
4963 possibility of point becoming "stuck" on a tall line when
4964 scrolling by one line. */
4965 if (window_scroll_pixel_based_preserve_y < 0
4966 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4967 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4969 start_display (&it, w, start);
4970 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4971 window_scroll_pixel_based_preserve_y = it.current_y;
4972 window_scroll_pixel_based_preserve_x = it.current_x;
4975 else
4976 window_scroll_pixel_based_preserve_y
4977 = window_scroll_pixel_based_preserve_x = -1;
4979 /* Move iterator it from start the specified distance forward or
4980 backward. The result is the new window start. */
4981 start_display (&it, w, start);
4982 if (whole)
4984 ptrdiff_t start_pos = IT_CHARPOS (it);
4985 int dy = frame_line_height;
4986 /* In the below we divide the window box height by the frame's
4987 line height to make the result predictable when the window
4988 box is not an integral multiple of the line height. This is
4989 important to ensure we get back to the same position when
4990 scrolling up, then down. */
4991 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4992 dy) * n;
4994 /* Note that move_it_vertically always moves the iterator to the
4995 start of a line. So, if the last line doesn't have a newline,
4996 we would end up at the start of the line ending at ZV. */
4997 if (dy <= 0)
4999 move_it_vertically_backward (&it, -dy);
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 > BEGV)
5004 move_it_by_lines (&it, -1);
5006 else if (dy > 0)
5008 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5009 MOVE_TO_POS | MOVE_TO_Y);
5010 /* Ensure we actually do move, e.g. in case we are currently
5011 looking at an image that is taller that the window height. */
5012 while (start_pos == IT_CHARPOS (it)
5013 && start_pos < ZV)
5014 move_it_by_lines (&it, 1);
5017 else
5018 move_it_by_lines (&it, n);
5020 /* We failed if we find ZV is already on the screen (scrolling up,
5021 means there's nothing past the end), or if we can't start any
5022 earlier (scrolling down, means there's nothing past the top). */
5023 if ((n > 0 && IT_CHARPOS (it) == ZV)
5024 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5026 if (IT_CHARPOS (it) == ZV)
5028 if (it.current_y < it.last_visible_y
5029 && (it.current_y + it.max_ascent + it.max_descent
5030 > it.last_visible_y))
5032 /* The last line was only partially visible, make it fully
5033 visible. */
5034 w->vscroll = (it.last_visible_y
5035 - it.current_y + it.max_ascent + it.max_descent);
5036 adjust_frame_glyphs (it.f);
5038 else
5040 bidi_unshelve_cache (itdata, false);
5041 if (noerror)
5042 return;
5043 else if (n < 0) /* could happen with empty buffers */
5044 xsignal0 (Qbeginning_of_buffer);
5045 else
5046 xsignal0 (Qend_of_buffer);
5049 else
5051 if (w->vscroll != 0)
5052 /* The first line was only partially visible, make it fully
5053 visible. */
5054 w->vscroll = 0;
5055 else
5057 bidi_unshelve_cache (itdata, false);
5058 if (noerror)
5059 return;
5060 else
5061 xsignal0 (Qbeginning_of_buffer);
5065 /* If control gets here, then we vscrolled. */
5067 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5069 /* Don't try to change the window start below. */
5070 vscrolled = true;
5073 if (! vscrolled)
5075 ptrdiff_t pos = IT_CHARPOS (it);
5076 ptrdiff_t bytepos;
5078 /* If in the middle of a multi-glyph character move forward to
5079 the next character. */
5080 if (in_display_vector_p (&it))
5082 ++pos;
5083 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5086 /* Set the window start, and set up the window for redisplay. */
5087 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5088 IT_BYTEPOS (it));
5089 bytepos = marker_byte_position (w->start);
5090 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5091 wset_update_mode_line (w);
5092 /* Set force_start so that redisplay_window will run the
5093 window-scroll-functions. */
5094 w->force_start = true;
5097 /* The rest of this function uses current_y in a nonstandard way,
5098 not including the height of the header line if any. */
5099 it.current_y = it.vpos = 0;
5101 /* Move PT out of scroll margins.
5102 This code wants current_y to be zero at the window start position
5103 even if there is a header line. */
5104 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
5106 if (n > 0)
5108 int last_y = it.last_visible_y - this_scroll_margin - 1;
5110 /* We moved the window start towards ZV, so PT may be now
5111 in the scroll margin at the top. */
5112 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5113 if (IT_CHARPOS (it) == PT
5114 && it.current_y >= this_scroll_margin
5115 && it.current_y <= last_y - WINDOW_HEADER_LINE_HEIGHT (w)
5116 && (NILP (Vscroll_preserve_screen_position)
5117 || EQ (Vscroll_preserve_screen_position, Qt)))
5118 /* We found PT at a legitimate height. Leave it alone. */
5120 else
5122 if (window_scroll_pixel_based_preserve_y >= 0)
5124 /* Don't enter the scroll margin at the end of the window. */
5125 int goal_y = min (last_y, window_scroll_pixel_based_preserve_y);
5127 /* If we have a header line, take account of it. This
5128 is necessary because we set it.current_y to 0, above. */
5129 move_it_to (&it, -1,
5130 window_scroll_pixel_based_preserve_x,
5131 goal_y - WINDOW_HEADER_LINE_HEIGHT (w),
5132 -1, MOVE_TO_Y | MOVE_TO_X);
5135 /* Get out of the scroll margin at the top of the window. */
5136 while (it.current_y < this_scroll_margin)
5138 int prev = it.current_y;
5139 move_it_by_lines (&it, 1);
5140 if (prev == it.current_y)
5141 break;
5143 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5146 else if (n < 0)
5148 ptrdiff_t charpos, bytepos;
5149 bool partial_p;
5151 /* Save our position, for the
5152 window_scroll_pixel_based_preserve_y case. */
5153 charpos = IT_CHARPOS (it);
5154 bytepos = IT_BYTEPOS (it);
5156 /* We moved the window start towards BEGV, so PT may be now
5157 in the scroll margin at the bottom. */
5158 move_it_to (&it, PT, -1,
5159 (it.last_visible_y - WINDOW_HEADER_LINE_HEIGHT (w)
5160 - partial_line_height (&it) - this_scroll_margin - 1),
5162 MOVE_TO_POS | MOVE_TO_Y);
5164 /* Save our position, in case it's correct. */
5165 charpos = IT_CHARPOS (it);
5166 bytepos = IT_BYTEPOS (it);
5168 /* If PT is in the screen line at the last fully visible line,
5169 move_it_to will stop at X = 0 in that line, because the
5170 required Y coordinate is reached there. See if we can get to
5171 PT without descending lower in Y, and if we can, it means we
5172 reached PT before the scroll margin. */
5173 if (charpos != PT)
5175 struct it it2;
5176 void *it_data;
5178 it2 = it;
5179 it_data = bidi_shelve_cache ();
5180 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5181 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5183 charpos = IT_CHARPOS (it);
5184 bytepos = IT_BYTEPOS (it);
5185 bidi_unshelve_cache (it_data, true);
5187 else
5189 it = it2;
5190 bidi_unshelve_cache (it_data, false);
5194 /* See if point is on a partially visible line at the end. */
5195 if (it.what == IT_EOB)
5196 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5197 else
5199 move_it_by_lines (&it, 1);
5200 partial_p = it.current_y > it.last_visible_y;
5203 if (charpos == PT && !partial_p
5204 && (NILP (Vscroll_preserve_screen_position)
5205 || EQ (Vscroll_preserve_screen_position, Qt)))
5206 /* We found PT before we found the display margin, so PT is ok. */
5208 else if (window_scroll_pixel_based_preserve_y >= 0)
5210 int goal_y = min (it.last_visible_y - this_scroll_margin - 1,
5211 window_scroll_pixel_based_preserve_y);
5213 /* Don't let the preserved screen Y coordinate put us inside
5214 any of the two margins. */
5215 if (goal_y < this_scroll_margin)
5216 goal_y = this_scroll_margin;
5217 SET_TEXT_POS_FROM_MARKER (start, w->start);
5218 start_display (&it, w, start);
5219 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5220 here because we called start_display again and did not
5221 alter it.current_y this time. */
5222 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5223 goal_y, -1, MOVE_TO_Y | MOVE_TO_X);
5224 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5226 else
5228 if (partial_p)
5229 /* The last line was only partially visible, so back up two
5230 lines to make sure we're on a fully visible line. */
5232 move_it_by_lines (&it, -2);
5233 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5235 else
5236 /* No, the position we saved is OK, so use it. */
5237 SET_PT_BOTH (charpos, bytepos);
5240 bidi_unshelve_cache (itdata, false);
5242 if (adjust_old_pointm)
5243 Fset_marker (w->old_pointm,
5244 ((w == XWINDOW (selected_window))
5245 ? make_number (BUF_PT (XBUFFER (w->contents)))
5246 : Fmarker_position (w->pointm)),
5247 w->contents);
5251 /* Implementation of window_scroll that works based on screen lines.
5252 See the comment of window_scroll for parameter descriptions. */
5254 static void
5255 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5257 struct window *w = XWINDOW (window);
5258 /* Fvertical_motion enters redisplay, which can trigger
5259 fontification, which in turn can modify buffer text (e.g., if the
5260 fontification functions replace escape sequences with faces, as
5261 in `grep-mode-font-lock-keywords'). So we use a marker to record
5262 the old point position, to prevent crashes in SET_PT_BOTH. */
5263 Lisp_Object opoint_marker = Fpoint_marker ();
5264 register ptrdiff_t pos, pos_byte;
5265 register int ht = window_internal_height (w);
5266 register Lisp_Object tem;
5267 bool lose;
5268 Lisp_Object bolp;
5269 ptrdiff_t startpos = marker_position (w->start);
5270 ptrdiff_t startbyte = marker_byte_position (w->start);
5271 Lisp_Object original_pos = Qnil;
5272 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5273 Fwindow_old_point (window)));
5275 /* If scrolling screen-fulls, compute the number of lines to
5276 scroll from the window's height. */
5277 if (whole)
5278 n *= max (1, ht - next_screen_context_lines);
5280 if (!NILP (Vscroll_preserve_screen_position))
5282 if (window_scroll_preserve_vpos <= 0
5283 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5284 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5286 struct position posit
5287 = *compute_motion (startpos, startbyte, 0, 0, false,
5288 PT, ht, 0, -1, w->hscroll, 0, w);
5290 window_scroll_preserve_vpos = posit.vpos;
5291 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5294 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5295 make_number (window_scroll_preserve_vpos));
5298 XSETFASTINT (tem, PT);
5299 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5301 if (NILP (tem))
5303 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5304 startpos = PT;
5305 startbyte = PT_BYTE;
5308 SET_PT_BOTH (startpos, startbyte);
5309 lose = n < 0 && PT == BEGV;
5310 Fvertical_motion (make_number (n), window, Qnil);
5311 pos = PT;
5312 pos_byte = PT_BYTE;
5313 bolp = Fbolp ();
5314 SET_PT_BOTH (marker_position (opoint_marker),
5315 marker_byte_position (opoint_marker));
5317 if (lose)
5319 if (noerror)
5320 return;
5321 else
5322 xsignal0 (Qbeginning_of_buffer);
5325 if (pos < ZV)
5327 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5329 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5330 w->start_at_line_beg = !NILP (bolp);
5331 wset_update_mode_line (w);
5332 /* Set force_start so that redisplay_window will run
5333 the window-scroll-functions. */
5334 w->force_start = true;
5336 if (!NILP (Vscroll_preserve_screen_position)
5337 && this_scroll_margin == 0
5338 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5340 SET_PT_BOTH (pos, pos_byte);
5341 Fvertical_motion (original_pos, window, Qnil);
5343 /* If we scrolled forward, put point enough lines down
5344 that it is outside the scroll margin. */
5345 else if (n > 0)
5347 int top_margin;
5349 if (this_scroll_margin > 0)
5351 SET_PT_BOTH (pos, pos_byte);
5352 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5353 top_margin = PT;
5355 else
5356 top_margin = pos;
5358 if (top_margin <= marker_position (opoint_marker))
5359 SET_PT_BOTH (marker_position (opoint_marker),
5360 marker_byte_position (opoint_marker));
5361 else if (!NILP (Vscroll_preserve_screen_position))
5363 int nlines = window_scroll_preserve_vpos;
5365 SET_PT_BOTH (pos, pos_byte);
5366 if (window_scroll_preserve_vpos < this_scroll_margin)
5367 nlines = this_scroll_margin;
5368 else if (window_scroll_preserve_vpos
5369 >= w->total_lines - this_scroll_margin)
5370 nlines = w->total_lines - this_scroll_margin - 1;
5371 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5372 make_number (nlines)), window, Qnil);
5374 else
5375 SET_PT (top_margin);
5377 else if (n < 0)
5379 int bottom_margin;
5381 /* If we scrolled backward, put point near the end of the window
5382 but not within the scroll margin. */
5383 SET_PT_BOTH (pos, pos_byte);
5384 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5385 Qnil);
5386 if (XFASTINT (tem) == ht - this_scroll_margin)
5387 bottom_margin = PT;
5388 else
5389 bottom_margin = PT + 1;
5391 if (bottom_margin > marker_position (opoint_marker))
5392 SET_PT_BOTH (marker_position (opoint_marker),
5393 marker_byte_position (opoint_marker));
5394 else
5396 if (!NILP (Vscroll_preserve_screen_position))
5398 int nlines = window_scroll_preserve_vpos;
5400 SET_PT_BOTH (pos, pos_byte);
5401 if (window_scroll_preserve_vpos < this_scroll_margin)
5402 nlines = this_scroll_margin;
5403 else if (window_scroll_preserve_vpos
5404 >= ht - this_scroll_margin)
5405 nlines = ht - this_scroll_margin - 1;
5406 Fvertical_motion (Fcons (make_number (window_scroll_preserve_hpos),
5407 make_number (nlines)), window, Qnil);
5409 else
5410 Fvertical_motion (make_number (-1), window, Qnil);
5414 else
5416 if (noerror)
5417 return;
5418 else
5419 xsignal0 (Qend_of_buffer);
5422 if (adjust_old_pointm)
5423 Fset_marker (w->old_pointm,
5424 ((w == XWINDOW (selected_window))
5425 ? make_number (BUF_PT (XBUFFER (w->contents)))
5426 : Fmarker_position (w->pointm)),
5427 w->contents);
5431 /* Scroll selected_window up or down. If N is nil, scroll a
5432 screen-full which is defined as the height of the window minus
5433 next_screen_context_lines. If N is the symbol `-', scroll.
5434 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5435 up. This is the guts of Fscroll_up and Fscroll_down. */
5437 static void
5438 scroll_command (Lisp_Object n, int direction)
5440 ptrdiff_t count = SPECPDL_INDEX ();
5442 eassert (eabs (direction) == 1);
5444 /* If selected window's buffer isn't current, make it current for
5445 the moment. But don't screw up if window_scroll gets an error. */
5446 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5448 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5449 Fset_buffer (XWINDOW (selected_window)->contents);
5452 if (NILP (n))
5453 window_scroll (selected_window, direction, true, false);
5454 else if (EQ (n, Qminus))
5455 window_scroll (selected_window, -direction, true, false);
5456 else
5458 n = Fprefix_numeric_value (n);
5459 window_scroll (selected_window, XINT (n) * direction, false, false);
5462 unbind_to (count, Qnil);
5465 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5466 doc: /* Scroll text of selected window upward ARG lines.
5467 If ARG is omitted or nil, scroll upward by a near full screen.
5468 A near full screen is `next-screen-context-lines' less than a full screen.
5469 Negative ARG means scroll downward.
5470 If ARG is the atom `-', scroll downward by nearly full screen.
5471 When calling from a program, supply as argument a number, nil, or `-'. */)
5472 (Lisp_Object arg)
5474 scroll_command (arg, 1);
5475 return Qnil;
5478 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5479 doc: /* Scroll text of selected window down ARG lines.
5480 If ARG is omitted or nil, scroll down by a near full screen.
5481 A near full screen is `next-screen-context-lines' less than a full screen.
5482 Negative ARG means scroll upward.
5483 If ARG is the atom `-', scroll upward by nearly full screen.
5484 When calling from a program, supply as argument a number, nil, or `-'. */)
5485 (Lisp_Object arg)
5487 scroll_command (arg, -1);
5488 return Qnil;
5491 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5492 doc: /* Return the other window for \"other window scroll\" commands.
5493 If `other-window-scroll-buffer' is non-nil, a window
5494 showing that buffer is used.
5495 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5496 specifies the window. This takes precedence over
5497 `other-window-scroll-buffer'. */)
5498 (void)
5500 Lisp_Object window;
5502 if (MINI_WINDOW_P (XWINDOW (selected_window))
5503 && !NILP (Vminibuf_scroll_window))
5504 window = Vminibuf_scroll_window;
5505 /* If buffer is specified and live, scroll that buffer. */
5506 else if (!NILP (Vother_window_scroll_buffer)
5507 && BUFFERP (Vother_window_scroll_buffer)
5508 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5510 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5511 if (NILP (window))
5512 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5514 else
5516 /* Nothing specified; look for a neighboring window on the same
5517 frame. */
5518 window = Fnext_window (selected_window, Qnil, Qnil);
5520 if (EQ (window, selected_window))
5521 /* That didn't get us anywhere; look for a window on another
5522 visible frame. */
5524 window = Fnext_window (window, Qnil, Qt);
5525 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5526 && ! EQ (window, selected_window));
5529 CHECK_LIVE_WINDOW (window);
5531 if (EQ (window, selected_window))
5532 error ("There is no other window");
5534 return window;
5537 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5538 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5539 A near full screen is `next-screen-context-lines' less than a full screen.
5540 The next window is the one below the current one; or the one at the top
5541 if the current one is at the bottom. Negative ARG means scroll downward.
5542 If ARG is the atom `-', scroll downward by nearly full screen.
5543 When calling from a program, supply as argument a number, nil, or `-'.
5545 If `other-window-scroll-buffer' is non-nil, scroll the window
5546 showing that buffer, popping the buffer up if necessary.
5547 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5548 specifies the window to scroll. This takes precedence over
5549 `other-window-scroll-buffer'. */)
5550 (Lisp_Object arg)
5552 Lisp_Object window;
5553 struct window *w;
5554 ptrdiff_t count = SPECPDL_INDEX ();
5556 window = Fother_window_for_scrolling ();
5557 w = XWINDOW (window);
5559 /* Don't screw up if window_scroll gets an error. */
5560 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5562 Fset_buffer (w->contents);
5563 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5564 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5566 if (NILP (arg))
5567 window_scroll (window, 1, true, true);
5568 else if (EQ (arg, Qminus))
5569 window_scroll (window, -1, true, true);
5570 else
5572 if (CONSP (arg))
5573 arg = XCAR (arg);
5574 CHECK_NUMBER (arg);
5575 window_scroll (window, XINT (arg), false, true);
5578 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5579 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5580 unbind_to (count, Qnil);
5582 return Qnil;
5585 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5586 doc: /* Scroll selected window display ARG columns left.
5587 Default for ARG is window width minus 2.
5588 Value is the total amount of leftward horizontal scrolling in
5589 effect after the change.
5590 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5591 lower bound for automatic scrolling, i.e. automatic scrolling
5592 will not scroll a window to a column less than the value returned
5593 by this function. This happens in an interactive call. */)
5594 (register Lisp_Object arg, Lisp_Object set_minimum)
5596 struct window *w = XWINDOW (selected_window);
5597 EMACS_INT requested_arg = (NILP (arg)
5598 ? window_body_width (w, 0) - 2
5599 : XINT (Fprefix_numeric_value (arg)));
5600 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5602 if (!NILP (set_minimum))
5603 w->min_hscroll = w->hscroll;
5605 w->suspend_auto_hscroll = true;
5607 return result;
5610 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5611 doc: /* Scroll selected window display ARG columns right.
5612 Default for ARG is window width minus 2.
5613 Value is the total amount of leftward horizontal scrolling in
5614 effect after the change.
5615 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5616 lower bound for automatic scrolling, i.e. automatic scrolling
5617 will not scroll a window to a column less than the value returned
5618 by this function. This happens in an interactive call. */)
5619 (register Lisp_Object arg, Lisp_Object set_minimum)
5621 struct window *w = XWINDOW (selected_window);
5622 EMACS_INT requested_arg = (NILP (arg)
5623 ? window_body_width (w, 0) - 2
5624 : XINT (Fprefix_numeric_value (arg)));
5625 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5627 if (!NILP (set_minimum))
5628 w->min_hscroll = w->hscroll;
5630 w->suspend_auto_hscroll = true;
5632 return result;
5635 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5636 doc: /* Return the window which was selected when entering the minibuffer.
5637 Returns nil, if selected window is not a minibuffer window. */)
5638 (void)
5640 if (minibuf_level > 0
5641 && MINI_WINDOW_P (XWINDOW (selected_window))
5642 && WINDOW_LIVE_P (minibuf_selected_window))
5643 return minibuf_selected_window;
5645 return Qnil;
5648 /* Value is the number of lines actually displayed in window W,
5649 as opposed to its height. */
5651 static int
5652 displayed_window_lines (struct window *w)
5654 struct it it;
5655 struct text_pos start;
5656 int height = window_box_height (w);
5657 struct buffer *old_buffer;
5658 int bottom_y;
5659 void *itdata = NULL;
5661 if (XBUFFER (w->contents) != current_buffer)
5663 old_buffer = current_buffer;
5664 set_buffer_internal (XBUFFER (w->contents));
5666 else
5667 old_buffer = NULL;
5669 /* In case W->start is out of the accessible range, do something
5670 reasonable. This happens in Info mode when Info-scroll-down
5671 calls (recenter -1) while W->start is 1. */
5672 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5674 itdata = bidi_shelve_cache ();
5675 start_display (&it, w, start);
5676 move_it_vertically (&it, height);
5677 bottom_y = line_bottom_y (&it);
5678 bidi_unshelve_cache (itdata, false);
5680 /* Add in empty lines at the bottom of the window. */
5681 if (bottom_y < height)
5683 int uy = FRAME_LINE_HEIGHT (it.f);
5684 it.vpos += (height - bottom_y + uy - 1) / uy;
5686 else if (bottom_y == height)
5687 it.vpos++;
5689 if (old_buffer)
5690 set_buffer_internal (old_buffer);
5692 return it.vpos;
5696 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5697 doc: /* Center point in selected window and maybe redisplay frame.
5698 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5699 relative to the selected window. If ARG is negative, it counts up from the
5700 bottom of the window. (ARG should be less than the height of the window.)
5702 If ARG is omitted or nil, then recenter with point on the middle line of
5703 the selected window; if the variable `recenter-redisplay' is non-nil,
5704 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5705 is set to `grow-only', this resets the tool-bar's height to the minimum
5706 height needed); if `recenter-redisplay' has the special value `tty',
5707 then only tty frames are redrawn.
5709 Just C-u as prefix means put point in the center of the window
5710 and redisplay normally--don't erase and redraw the frame. */)
5711 (register Lisp_Object arg)
5713 struct window *w = XWINDOW (selected_window);
5714 struct buffer *buf = XBUFFER (w->contents);
5715 bool center_p = false;
5716 ptrdiff_t charpos, bytepos;
5717 EMACS_INT iarg UNINIT;
5718 int this_scroll_margin;
5720 if (buf != current_buffer)
5721 error ("`recenter'ing a window that does not display current-buffer.");
5723 /* If redisplay is suppressed due to an error, try again. */
5724 buf->display_error_modiff = 0;
5726 if (NILP (arg))
5728 if (!NILP (Vrecenter_redisplay)
5729 && (!EQ (Vrecenter_redisplay, Qtty)
5730 || !NILP (Ftty_type (selected_frame))))
5732 ptrdiff_t i;
5734 /* Invalidate pixel data calculated for all compositions. */
5735 for (i = 0; i < n_compositions; i++)
5736 composition_table[i]->font = NULL;
5737 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5738 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5739 #endif
5740 Fredraw_frame (WINDOW_FRAME (w));
5741 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5744 center_p = true;
5746 else if (CONSP (arg)) /* Just C-u. */
5747 center_p = true;
5748 else
5750 arg = Fprefix_numeric_value (arg);
5751 CHECK_NUMBER (arg);
5752 iarg = XINT (arg);
5755 /* Do this after making BUF current
5756 in case scroll_margin is buffer-local. */
5757 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
5759 /* Don't use redisplay code for initial frames, as the necessary
5760 data structures might not be set up yet then. */
5761 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5763 if (center_p)
5765 struct it it;
5766 struct text_pos pt;
5767 void *itdata = bidi_shelve_cache ();
5769 SET_TEXT_POS (pt, PT, PT_BYTE);
5770 start_display (&it, w, pt);
5771 move_it_vertically_backward (&it, window_box_height (w) / 2);
5772 charpos = IT_CHARPOS (it);
5773 bytepos = IT_BYTEPOS (it);
5774 bidi_unshelve_cache (itdata, false);
5776 else if (iarg < 0)
5778 struct it it;
5779 struct text_pos pt;
5780 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5781 int extra_line_spacing;
5782 int h = window_box_height (w);
5783 int ht = window_internal_height (w);
5784 void *itdata = bidi_shelve_cache ();
5786 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5787 ht - this_scroll_margin);
5789 SET_TEXT_POS (pt, PT, PT_BYTE);
5790 start_display (&it, w, pt);
5792 /* Be sure we have the exact height of the full line containing PT. */
5793 move_it_by_lines (&it, 0);
5795 /* The amount of pixels we have to move back is the window
5796 height minus what's displayed in the line containing PT,
5797 and the lines below. */
5798 it.current_y = 0;
5799 it.vpos = 0;
5800 move_it_by_lines (&it, nlines);
5802 if (it.vpos == nlines)
5803 h -= it.current_y;
5804 else
5806 /* Last line has no newline. */
5807 h -= line_bottom_y (&it);
5808 it.vpos++;
5811 /* Don't reserve space for extra line spacing of last line. */
5812 extra_line_spacing = it.max_extra_line_spacing;
5814 /* If we can't move down NLINES lines because we hit
5815 the end of the buffer, count in some empty lines. */
5816 if (it.vpos < nlines)
5818 nlines -= it.vpos;
5819 extra_line_spacing = it.extra_line_spacing;
5820 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5822 if (h <= 0)
5824 bidi_unshelve_cache (itdata, false);
5825 return Qnil;
5828 /* Now find the new top line (starting position) of the window. */
5829 start_display (&it, w, pt);
5830 it.current_y = 0;
5831 move_it_vertically_backward (&it, h);
5833 /* If extra line spacing is present, we may move too far
5834 back. This causes the last line to be only partially
5835 visible (which triggers redisplay to recenter that line
5836 in the middle), so move forward.
5837 But ignore extra line spacing on last line, as it is not
5838 considered to be part of the visible height of the line.
5840 h += extra_line_spacing;
5841 while (-it.current_y > h)
5842 move_it_by_lines (&it, 1);
5844 charpos = IT_CHARPOS (it);
5845 bytepos = IT_BYTEPOS (it);
5847 bidi_unshelve_cache (itdata, false);
5849 else
5851 struct it it;
5852 struct text_pos pt;
5853 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5854 int ht = window_internal_height (w);
5855 void *itdata = bidi_shelve_cache ();
5857 nlines = clip_to_bounds (this_scroll_margin, nlines,
5858 ht - this_scroll_margin - 1);
5860 SET_TEXT_POS (pt, PT, PT_BYTE);
5861 start_display (&it, w, pt);
5863 /* Move to the beginning of screen line containing PT. */
5864 move_it_by_lines (&it, 0);
5866 /* Move back to find the point which is ARG screen lines above PT. */
5867 if (nlines > 0)
5869 it.current_y = 0;
5870 it.vpos = 0;
5871 move_it_by_lines (&it, -nlines);
5874 charpos = IT_CHARPOS (it);
5875 bytepos = IT_BYTEPOS (it);
5877 bidi_unshelve_cache (itdata, false);
5880 else
5882 struct position pos;
5883 int ht = window_internal_height (w);
5885 if (center_p)
5886 iarg = ht / 2;
5887 else if (iarg < 0)
5888 iarg += ht;
5890 /* Don't let it get into the margin at either top or bottom. */
5891 iarg = clip_to_bounds (this_scroll_margin, iarg,
5892 ht - this_scroll_margin - 1);
5894 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5895 charpos = pos.bufpos;
5896 bytepos = pos.bytepos;
5899 /* Set the new window start. */
5900 set_marker_both (w->start, w->contents, charpos, bytepos);
5901 w->window_end_valid = false;
5903 w->optional_new_start = true;
5905 w->start_at_line_beg = (bytepos == BEGV_BYTE
5906 || FETCH_BYTE (bytepos - 1) == '\n');
5908 wset_redisplay (w);
5910 return Qnil;
5913 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5914 0, 2, 0,
5915 doc: /* Return the width in columns of the text display area of WINDOW.
5916 WINDOW must be a live window and defaults to the selected one.
5918 The returned width does not include dividers, scrollbars, margins,
5919 fringes, nor any partial-width columns at the right of the text
5920 area.
5922 Optional argument PIXELWISE non-nil, means to return the width in
5923 pixels. */)
5924 (Lisp_Object window, Lisp_Object pixelwise)
5926 struct window *w = decode_live_window (window);
5928 if (NILP (pixelwise))
5929 return make_number (window_box_width (w, TEXT_AREA)
5930 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5931 else
5932 return make_number (window_box_width (w, TEXT_AREA));
5935 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5936 0, 2, 0,
5937 doc: /* Return the height in lines of the text display area of WINDOW.
5938 WINDOW must be a live window and defaults to the selected one.
5940 The returned height does not include dividers, the mode line, any header
5941 line, nor any partial-height lines at the bottom of the text area.
5943 Optional argument PIXELWISE non-nil, means to return the height in
5944 pixels. */)
5945 (Lisp_Object window, Lisp_Object pixelwise)
5947 struct window *w = decode_live_window (window);
5949 if (NILP (pixelwise))
5950 return make_number (window_box_height (w)
5951 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5952 else
5953 return make_number (window_box_height (w));
5956 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5957 1, 1, "P",
5958 doc: /* Position point relative to window.
5959 ARG nil means position point at center of window.
5960 Else, ARG specifies vertical position within the window;
5961 zero means top of window, negative means relative to bottom
5962 of window, -1 meaning the last fully visible display line
5963 of the window.
5965 Value is the screen line of the window point moved to, counting
5966 from the top of the window. */)
5967 (Lisp_Object arg)
5969 struct window *w = XWINDOW (selected_window);
5970 int lines, start;
5971 Lisp_Object window;
5972 #if false
5973 int this_scroll_margin;
5974 #endif
5976 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5977 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5978 when passed below to set_marker_both. */
5979 error ("move-to-window-line called from unrelated buffer");
5981 window = selected_window;
5982 start = marker_position (w->start);
5983 if (start < BEGV || start > ZV)
5985 int height = window_internal_height (w);
5986 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5987 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5988 w->start_at_line_beg = !NILP (Fbolp ());
5989 w->force_start = true;
5991 else
5992 Fgoto_char (w->start);
5994 lines = displayed_window_lines (w);
5996 if (NILP (arg))
5997 XSETFASTINT (arg, lines / 2);
5998 else
6000 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
6002 if (iarg < 0)
6003 iarg = iarg + lines;
6005 #if false /* This code would prevent move-to-window-line from moving point
6006 to a place inside the scroll margins (which would cause the
6007 next redisplay to scroll). I wrote this code, but then concluded
6008 it is probably better not to install it. However, it is here
6009 inside #if false so as not to lose it. -- rms. */
6011 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_LINES);
6013 /* Don't let it get into the margin at either top or bottom. */
6014 iarg = max (iarg, this_scroll_margin);
6015 iarg = min (iarg, lines - this_scroll_margin - 1);
6016 #endif
6018 arg = make_number (iarg);
6021 /* Skip past a partially visible first line. */
6022 if (w->vscroll)
6023 XSETINT (arg, XINT (arg) + 1);
6025 return Fvertical_motion (arg, window, Qnil);
6030 /***********************************************************************
6031 Window Configuration
6032 ***********************************************************************/
6034 struct save_window_data
6036 struct vectorlike_header header;
6037 Lisp_Object selected_frame;
6038 Lisp_Object current_window;
6039 Lisp_Object f_current_buffer;
6040 Lisp_Object minibuf_scroll_window;
6041 Lisp_Object minibuf_selected_window;
6042 Lisp_Object root_window;
6043 Lisp_Object focus_frame;
6044 /* A vector, each of whose elements is a struct saved_window
6045 for one window. */
6046 Lisp_Object saved_windows;
6048 /* All fields above are traced by the GC.
6049 From `frame-cols' down, the fields are ignored by the GC. */
6050 /* We should be able to do without the following two. */
6051 int frame_cols, frame_lines;
6052 /* These two should get eventually replaced by their pixel
6053 counterparts. */
6054 int frame_menu_bar_lines, frame_tool_bar_lines;
6055 int frame_text_width, frame_text_height;
6056 /* These are currently unused. We need them as soon as we convert
6057 to pixels. */
6058 int frame_menu_bar_height, frame_tool_bar_height;
6061 /* This is saved as a Lisp_Vector. */
6062 struct saved_window
6064 struct vectorlike_header header;
6066 Lisp_Object window, buffer, start, pointm, old_pointm;
6067 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6068 Lisp_Object pixel_height_before_size_change, pixel_width_before_size_change;
6069 Lisp_Object left_col, top_line, total_cols, total_lines;
6070 Lisp_Object normal_cols, normal_lines;
6071 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6072 Lisp_Object parent, prev;
6073 Lisp_Object start_at_line_beg;
6074 Lisp_Object display_table;
6075 Lisp_Object left_margin_cols, right_margin_cols;
6076 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6077 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6078 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6079 Lisp_Object combination_limit, window_parameters;
6082 #define SAVED_WINDOW_N(swv,n) \
6083 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6085 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6086 doc: /* Return t if OBJECT is a window-configuration object. */)
6087 (Lisp_Object object)
6089 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6092 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6093 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6094 (Lisp_Object config)
6096 register struct save_window_data *data;
6097 struct Lisp_Vector *saved_windows;
6099 CHECK_WINDOW_CONFIGURATION (config);
6101 data = (struct save_window_data *) XVECTOR (config);
6102 saved_windows = XVECTOR (data->saved_windows);
6103 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6106 DEFUN ("set-window-configuration", Fset_window_configuration,
6107 Sset_window_configuration, 1, 1, 0,
6108 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6109 CONFIGURATION must be a value previously returned
6110 by `current-window-configuration' (which see).
6111 If CONFIGURATION was made from a frame that is now deleted,
6112 only frame-independent values can be restored. In this case,
6113 the return value is nil. Otherwise the value is t. */)
6114 (Lisp_Object configuration)
6116 register struct save_window_data *data;
6117 struct Lisp_Vector *saved_windows;
6118 Lisp_Object new_current_buffer;
6119 Lisp_Object frame;
6120 struct frame *f;
6121 ptrdiff_t old_point = -1;
6122 USE_SAFE_ALLOCA;
6124 CHECK_WINDOW_CONFIGURATION (configuration);
6126 data = (struct save_window_data *) XVECTOR (configuration);
6127 saved_windows = XVECTOR (data->saved_windows);
6129 new_current_buffer = data->f_current_buffer;
6130 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6131 new_current_buffer = Qnil;
6132 else
6134 if (XBUFFER (new_current_buffer) == current_buffer)
6135 /* The code further down "preserves point" by saving here PT in
6136 old_point and then setting it later back into PT. When the
6137 current-selected-window and the final-selected-window both show
6138 the current buffer, this suffers from the problem that the
6139 current PT is the window-point of the current-selected-window,
6140 while the final PT is the point of the final-selected-window, so
6141 this copy from one PT to the other would end up moving the
6142 window-point of the final-selected-window to the window-point of
6143 the current-selected-window. So we have to be careful which
6144 point of the current-buffer we copy into old_point. */
6145 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6146 && WINDOWP (selected_window)
6147 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6148 && !EQ (selected_window, data->current_window))
6149 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6150 else
6151 old_point = PT;
6152 else
6153 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6154 point in new_current_buffer as of the last time this buffer was
6155 used. This can be non-deterministic since it can be changed by
6156 things like jit-lock by mere temporary selection of some random
6157 window that happens to show this buffer.
6158 So if possible we want this arbitrary choice of "which point" to
6159 be the one from the to-be-selected-window so as to prevent this
6160 window's cursor from being copied from another window. */
6161 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6162 /* If current_window = selected_window, its point is in BUF_PT. */
6163 && !EQ (selected_window, data->current_window))
6164 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6165 else
6166 old_point = BUF_PT (XBUFFER (new_current_buffer));
6169 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6170 f = XFRAME (frame);
6172 /* If f is a dead frame, don't bother rebuilding its window tree.
6173 However, there is other stuff we should still try to do below. */
6174 if (FRAME_LIVE_P (f))
6176 Lisp_Object window;
6177 Lisp_Object dead_windows = Qnil;
6178 Lisp_Object tem, par, pers;
6179 struct window *w;
6180 struct saved_window *p;
6181 struct window *root_window;
6182 struct window **leaf_windows;
6183 ptrdiff_t i, k, n_leaf_windows;
6184 /* Records whether a window has been added or removed wrt the
6185 original configuration. */
6186 bool window_changed = false;
6187 /* Records whether a window has changed its buffer wrt the
6188 original configuration. */
6189 bool buffer_changed = false;
6191 /* Don't do this within the main loop below: This may call Lisp
6192 code and is thus potentially unsafe while input is blocked. */
6193 for (k = 0; k < saved_windows->header.size; k++)
6195 p = SAVED_WINDOW_N (saved_windows, k);
6196 window = p->window;
6197 w = XWINDOW (window);
6199 if (NILP (w->contents))
6200 /* A dead window that will be resurrected, the window
6201 configuration will change. */
6202 window_changed = true;
6204 if (BUFFERP (w->contents)
6205 && !EQ (w->contents, p->buffer)
6206 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6207 /* If a window we restore gets another buffer, record the
6208 window's old buffer. */
6209 call1 (Qrecord_window_buffer, window);
6212 /* Disallow x_set_window_size, temporarily. */
6213 f->can_x_set_window_size = false;
6214 /* The mouse highlighting code could get screwed up
6215 if it runs during this. */
6216 block_input ();
6218 /* "Swap out" point from the selected window's buffer
6219 into the window itself. (Normally the pointm of the selected
6220 window holds garbage.) We do this now, before
6221 restoring the window contents, and prevent it from
6222 being done later on when we select a new window. */
6223 if (! NILP (XWINDOW (selected_window)->contents))
6225 w = XWINDOW (selected_window);
6226 set_marker_both (w->pointm,
6227 w->contents,
6228 BUF_PT (XBUFFER (w->contents)),
6229 BUF_PT_BYTE (XBUFFER (w->contents)));
6232 fset_redisplay (f);
6234 /* Problem: Freeing all matrices and later allocating them again
6235 is a serious redisplay flickering problem. What we would
6236 really like to do is to free only those matrices not reused
6237 below. */
6238 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6239 ptrdiff_t nwindows = count_windows (root_window);
6240 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6241 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6243 /* Kludge Alert!
6244 Mark all windows now on frame as "deleted".
6245 Restoring the new configuration "undeletes" any that are in it.
6247 Save their current buffers in their height fields, since we may
6248 need it later, if a buffer saved in the configuration is now
6249 dead. */
6250 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6252 for (k = 0; k < saved_windows->header.size; k++)
6254 p = SAVED_WINDOW_N (saved_windows, k);
6255 window = p->window;
6256 w = XWINDOW (window);
6257 wset_next (w, Qnil);
6259 if (!NILP (p->parent))
6260 wset_parent
6261 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6262 else
6263 wset_parent (w, Qnil);
6265 if (!NILP (p->prev))
6267 wset_prev
6268 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6269 wset_next (XWINDOW (w->prev), p->window);
6271 else
6273 wset_prev (w, Qnil);
6274 if (!NILP (w->parent))
6275 wset_combination (XWINDOW (w->parent),
6276 (XINT (p->total_cols)
6277 != XWINDOW (w->parent)->total_cols),
6278 p->window);
6281 /* If we squirreled away the buffer, restore it now. */
6282 if (BUFFERP (w->combination_limit))
6283 wset_buffer (w, w->combination_limit);
6284 w->pixel_left = XFASTINT (p->pixel_left);
6285 w->pixel_top = XFASTINT (p->pixel_top);
6286 w->pixel_width = XFASTINT (p->pixel_width);
6287 w->pixel_height = XFASTINT (p->pixel_height);
6288 w->pixel_width_before_size_change
6289 = XFASTINT (p->pixel_width_before_size_change);
6290 w->pixel_height_before_size_change
6291 = XFASTINT (p->pixel_height_before_size_change);
6292 w->left_col = XFASTINT (p->left_col);
6293 w->top_line = XFASTINT (p->top_line);
6294 w->total_cols = XFASTINT (p->total_cols);
6295 w->total_lines = XFASTINT (p->total_lines);
6296 wset_normal_cols (w, p->normal_cols);
6297 wset_normal_lines (w, p->normal_lines);
6298 w->hscroll = XFASTINT (p->hscroll);
6299 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6300 w->min_hscroll = XFASTINT (p->min_hscroll);
6301 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6302 wset_display_table (w, p->display_table);
6303 w->left_margin_cols = XINT (p->left_margin_cols);
6304 w->right_margin_cols = XINT (p->right_margin_cols);
6305 w->left_fringe_width = XINT (p->left_fringe_width);
6306 w->right_fringe_width = XINT (p->right_fringe_width);
6307 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6308 w->scroll_bar_width = XINT (p->scroll_bar_width);
6309 w->scroll_bar_height = XINT (p->scroll_bar_height);
6310 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6311 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6312 wset_dedicated (w, p->dedicated);
6313 wset_combination_limit (w, p->combination_limit);
6314 /* Restore any window parameters that have been saved.
6315 Parameters that have not been saved are left alone. */
6316 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6318 pers = XCAR (tem);
6319 if (CONSP (pers))
6321 if (NILP (XCDR (pers)))
6323 par = Fassq (XCAR (pers), w->window_parameters);
6324 if (CONSP (par) && !NILP (XCDR (par)))
6325 /* Reset a parameter to nil if and only if it
6326 has a non-nil association. Don't make new
6327 associations. */
6328 Fsetcdr (par, Qnil);
6330 else
6331 /* Always restore a non-nil value. */
6332 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6336 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6337 /* If saved buffer is alive, install it. */
6339 if (!EQ (w->contents, p->buffer))
6340 /* Record buffer configuration change. */
6341 buffer_changed = true;
6342 wset_buffer (w, p->buffer);
6343 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6344 set_marker_restricted (w->start, p->start, w->contents);
6345 set_marker_restricted (w->pointm, p->pointm, w->contents);
6346 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6347 /* As documented in Fcurrent_window_configuration, don't
6348 restore the location of point in the buffer which was
6349 current when the window configuration was recorded. */
6350 if (!EQ (p->buffer, new_current_buffer)
6351 && XBUFFER (p->buffer) == current_buffer)
6352 Fgoto_char (w->pointm);
6354 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6355 /* Keep window's old buffer; make sure the markers are real. */
6357 /* Set window markers at start of visible range. */
6358 if (XMARKER (w->start)->buffer == 0)
6359 set_marker_restricted_both (w->start, w->contents, 0, 0);
6360 if (XMARKER (w->pointm)->buffer == 0)
6361 set_marker_restricted_both
6362 (w->pointm, w->contents,
6363 BUF_PT (XBUFFER (w->contents)),
6364 BUF_PT_BYTE (XBUFFER (w->contents)));
6365 if (XMARKER (w->old_pointm)->buffer == 0)
6366 set_marker_restricted_both
6367 (w->old_pointm, w->contents,
6368 BUF_PT (XBUFFER (w->contents)),
6369 BUF_PT_BYTE (XBUFFER (w->contents)));
6370 w->start_at_line_beg = true;
6372 else if (!NILP (w->start))
6373 /* Leaf window has no live buffer, get one. */
6375 /* Record buffer configuration change. */
6376 buffer_changed = true;
6377 /* Get the buffer via other_buffer_safely in order to
6378 avoid showing an unimportant buffer and, if necessary, to
6379 recreate *scratch* in the course (part of Juanma's bs-show
6380 scenario from March 2011). */
6381 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6382 /* This will set the markers to beginning of visible
6383 range. */
6384 set_marker_restricted_both (w->start, w->contents, 0, 0);
6385 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6386 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6387 w->start_at_line_beg = true;
6388 if (!NILP (w->dedicated))
6389 /* Record this window as dead. */
6390 dead_windows = Fcons (window, dead_windows);
6391 /* Make sure window is no more dedicated. */
6392 wset_dedicated (w, Qnil);
6396 fset_root_window (f, data->root_window);
6397 /* Arrange *not* to restore point in the buffer that was
6398 current when the window configuration was saved. */
6399 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6400 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6401 make_number (old_point),
6402 XWINDOW (data->current_window)->contents);
6404 /* In the following call to `select-window', prevent "swapping out
6405 point" in the old selected window using the buffer that has
6406 been restored into it. We already swapped out that point from
6407 that window's old buffer.
6409 Do not record the buffer here. We do that in a separate call
6410 to select_window below. See also Bug#16207. */
6411 select_window (data->current_window, Qt, true);
6412 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6413 last_selected_window)
6414 = selected_window;
6416 if (NILP (data->focus_frame)
6417 || (FRAMEP (data->focus_frame)
6418 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6419 Fredirect_frame_focus (frame, data->focus_frame);
6421 /* Now, free glyph matrices in windows that were not reused. */
6422 for (i = 0; i < n_leaf_windows; i++)
6423 if (NILP (leaf_windows[i]->contents))
6425 free_window_matrices (leaf_windows[i]);
6426 window_changed = true;
6429 /* Allow x_set_window_size again and apply frame size changes if
6430 needed. */
6431 f->can_x_set_window_size = true;
6432 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6434 adjust_frame_glyphs (f);
6435 unblock_input ();
6437 /* Scan dead buffer windows. */
6438 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6440 window = XCAR (dead_windows);
6441 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6442 delete_deletable_window (window);
6445 /* Record the selected window's buffer here. The window should
6446 already be the selected one from the call above. */
6447 if (WINDOW_LIVE_P (data->current_window))
6448 select_window (data->current_window, Qnil, false);
6450 /* Fselect_window will have made f the selected frame, so we
6451 reselect the proper frame here. Fhandle_switch_frame will change the
6452 selected window too, but that doesn't make the call to
6453 Fselect_window above totally superfluous; it still sets f's
6454 selected window. */
6455 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6456 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6458 if (window_changed)
6459 /* At least one window has been added or removed. Run
6460 `window-configuration-change-hook' and make sure
6461 `window-size-change-functions' get run later.
6463 We have to do this in order to capture the following
6464 scenario: Suppose our frame contains two live windows W1 and
6465 W2 and ‘set-window-configuration’ replaces them by two
6466 windows W3 and W4 that were dead the last time
6467 run_window_size_change_functions was run. If W3 and W4 have
6468 the same values for their old and new pixel sizes but these
6469 values differ from those of W1 and W2, the sizes of our
6470 frame's two live windows changed but window_size_changed has
6471 no means to detect that fact.
6473 Obviously, this will get us false positives, for example,
6474 when we restore the original configuration with W1 and W2
6475 before run_window_size_change_functions gets called. */
6477 run_window_configuration_change_hook (f);
6478 FRAME_WINDOW_CONFIGURATION_CHANGED (f) = true;
6480 else if (buffer_changed)
6481 /* At least one window has changed its buffer. Run
6482 `window-configuration-change-hook' only. */
6483 run_window_configuration_change_hook (f);
6486 if (!NILP (new_current_buffer))
6488 Fset_buffer (new_current_buffer);
6489 /* If the new current buffer doesn't appear in the selected
6490 window, go to its old point (see bug#12208). */
6491 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6492 Fgoto_char (make_number (old_point));
6495 Vminibuf_scroll_window = data->minibuf_scroll_window;
6496 minibuf_selected_window = data->minibuf_selected_window;
6498 SAFE_FREE ();
6499 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6503 void
6504 restore_window_configuration (Lisp_Object configuration)
6506 Fset_window_configuration (configuration);
6510 /* If WINDOW is an internal window, recursively delete all child windows
6511 reachable via the next and contents slots of WINDOW. Otherwise setup
6512 WINDOW to not show any buffer. */
6514 void
6515 delete_all_child_windows (Lisp_Object window)
6517 register struct window *w;
6519 w = XWINDOW (window);
6521 if (!NILP (w->next))
6522 /* Delete WINDOW's siblings (we traverse postorderly). */
6523 delete_all_child_windows (w->next);
6525 if (WINDOWP (w->contents))
6527 delete_all_child_windows (w->contents);
6528 wset_combination (w, false, Qnil);
6530 else if (BUFFERP (w->contents))
6532 unshow_buffer (w);
6533 unchain_marker (XMARKER (w->pointm));
6534 unchain_marker (XMARKER (w->old_pointm));
6535 unchain_marker (XMARKER (w->start));
6536 /* Since combination limit makes sense for an internal windows
6537 only, we use this slot to save the buffer for the sake of
6538 possible resurrection in Fset_window_configuration. */
6539 wset_combination_limit (w, w->contents);
6540 wset_buffer (w, Qnil);
6543 Vwindow_list = Qnil;
6546 static ptrdiff_t
6547 count_windows (struct window *window)
6549 ptrdiff_t count = 1;
6550 if (!NILP (window->next))
6551 count += count_windows (XWINDOW (window->next));
6552 if (WINDOWP (window->contents))
6553 count += count_windows (XWINDOW (window->contents));
6554 return count;
6558 /* Fill vector FLAT with leaf windows under W, starting at index I.
6559 Value is last index + 1. */
6560 static ptrdiff_t
6561 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6563 while (w)
6565 if (WINDOWP (w->contents))
6566 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6567 else
6568 flat[i++] = w;
6570 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6573 return i;
6577 /* Return a pointer to the glyph W's physical cursor is on. Value is
6578 null if W's current matrix is invalid, so that no meaningful glyph
6579 can be returned. */
6580 struct glyph *
6581 get_phys_cursor_glyph (struct window *w)
6583 struct glyph_row *row;
6584 struct glyph *glyph;
6585 int hpos = w->phys_cursor.hpos;
6587 if (!(w->phys_cursor.vpos >= 0
6588 && w->phys_cursor.vpos < w->current_matrix->nrows))
6589 return NULL;
6591 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6592 if (!row->enabled_p)
6593 return NULL;
6595 if (w->hscroll)
6597 /* When the window is hscrolled, cursor hpos can legitimately be
6598 out of bounds, but we draw the cursor at the corresponding
6599 window margin in that case. */
6600 if (!row->reversed_p && hpos < 0)
6601 hpos = 0;
6602 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6603 hpos = row->used[TEXT_AREA] - 1;
6606 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6607 glyph = row->glyphs[TEXT_AREA] + hpos;
6608 else
6609 glyph = NULL;
6611 return glyph;
6615 static ptrdiff_t
6616 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6618 struct saved_window *p;
6619 struct window *w;
6620 Lisp_Object tem, pers, par;
6622 for (; !NILP (window); window = w->next)
6624 p = SAVED_WINDOW_N (vector, i);
6625 w = XWINDOW (window);
6627 wset_temslot (w, make_number (i)); i++;
6628 p->window = window;
6629 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6630 p->pixel_left = make_number (w->pixel_left);
6631 p->pixel_top = make_number (w->pixel_top);
6632 p->pixel_width = make_number (w->pixel_width);
6633 p->pixel_height = make_number (w->pixel_height);
6634 p->pixel_width_before_size_change
6635 = make_number (w->pixel_width_before_size_change);
6636 p->pixel_height_before_size_change
6637 = make_number (w->pixel_height_before_size_change);
6638 p->left_col = make_number (w->left_col);
6639 p->top_line = make_number (w->top_line);
6640 p->total_cols = make_number (w->total_cols);
6641 p->total_lines = make_number (w->total_lines);
6642 p->normal_cols = w->normal_cols;
6643 p->normal_lines = w->normal_lines;
6644 XSETFASTINT (p->hscroll, w->hscroll);
6645 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6646 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6647 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6648 p->display_table = w->display_table;
6649 p->left_margin_cols = make_number (w->left_margin_cols);
6650 p->right_margin_cols = make_number (w->right_margin_cols);
6651 p->left_fringe_width = make_number (w->left_fringe_width);
6652 p->right_fringe_width = make_number (w->right_fringe_width);
6653 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6654 p->scroll_bar_width = make_number (w->scroll_bar_width);
6655 p->scroll_bar_height = make_number (w->scroll_bar_height);
6656 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6657 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6658 p->dedicated = w->dedicated;
6659 p->combination_limit = w->combination_limit;
6660 p->window_parameters = Qnil;
6662 if (!NILP (Vwindow_persistent_parameters))
6664 /* Run cycle detection on Vwindow_persistent_parameters. */
6665 Lisp_Object tortoise, hare;
6667 hare = tortoise = Vwindow_persistent_parameters;
6668 while (CONSP (hare))
6670 hare = XCDR (hare);
6671 if (!CONSP (hare))
6672 break;
6674 hare = XCDR (hare);
6675 tortoise = XCDR (tortoise);
6677 if (EQ (hare, tortoise))
6678 /* Reset Vwindow_persistent_parameters to Qnil. */
6680 Vwindow_persistent_parameters = Qnil;
6681 break;
6685 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6686 tem = XCDR (tem))
6688 pers = XCAR (tem);
6689 /* Save values for persistent window parameters. */
6690 if (CONSP (pers) && !NILP (XCDR (pers)))
6692 par = Fassq (XCAR (pers), w->window_parameters);
6693 if (NILP (par))
6694 /* If the window has no value for the parameter,
6695 make one. */
6696 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6697 p->window_parameters);
6698 else
6699 /* If the window has a value for the parameter,
6700 save it. */
6701 p->window_parameters = Fcons (Fcons (XCAR (par),
6702 XCDR (par)),
6703 p->window_parameters);
6708 if (BUFFERP (w->contents))
6710 /* Save w's value of point in the window configuration. If w
6711 is the selected window, then get the value of point from
6712 the buffer; pointm is garbage in the selected window. */
6713 if (EQ (window, selected_window))
6714 p->pointm = build_marker (XBUFFER (w->contents),
6715 BUF_PT (XBUFFER (w->contents)),
6716 BUF_PT_BYTE (XBUFFER (w->contents)));
6717 else
6718 p->pointm = Fcopy_marker (w->pointm, Qnil);
6719 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6720 XMARKER (p->pointm)->insertion_type
6721 = !NILP (buffer_local_value /* Don't signal error if void. */
6722 (Qwindow_point_insertion_type, w->contents));
6723 XMARKER (p->old_pointm)->insertion_type
6724 = !NILP (buffer_local_value /* Don't signal error if void. */
6725 (Qwindow_point_insertion_type, w->contents));
6727 p->start = Fcopy_marker (w->start, Qnil);
6728 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6730 else
6732 p->pointm = Qnil;
6733 p->old_pointm = Qnil;
6734 p->start = Qnil;
6735 p->start_at_line_beg = Qnil;
6738 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6739 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6741 if (WINDOWP (w->contents))
6742 i = save_window_save (w->contents, vector, i);
6745 return i;
6748 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6749 Scurrent_window_configuration, 0, 1, 0,
6750 doc: /* Return an object representing the current window configuration of FRAME.
6751 If FRAME is nil or omitted, use the selected frame.
6752 This describes the number of windows, their sizes and current buffers,
6753 and for each displayed buffer, where display starts, and the position of
6754 point. An exception is made for point in the current buffer:
6755 its value is -not- saved.
6756 This also records the currently selected frame, and FRAME's focus
6757 redirection (see `redirect-frame-focus'). The variable
6758 `window-persistent-parameters' specifies which window parameters are
6759 saved by this function. */)
6760 (Lisp_Object frame)
6762 Lisp_Object tem;
6763 ptrdiff_t i, n_windows;
6764 struct save_window_data *data;
6765 struct frame *f = decode_live_frame (frame);
6767 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6768 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6769 PVEC_WINDOW_CONFIGURATION);
6771 data->frame_cols = FRAME_COLS (f);
6772 data->frame_lines = FRAME_LINES (f);
6773 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6774 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6775 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6776 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6777 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6778 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6779 data->selected_frame = selected_frame;
6780 data->current_window = FRAME_SELECTED_WINDOW (f);
6781 XSETBUFFER (data->f_current_buffer, current_buffer);
6782 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6783 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6784 data->root_window = FRAME_ROOT_WINDOW (f);
6785 data->focus_frame = FRAME_FOCUS_FRAME (f);
6786 tem = make_uninit_vector (n_windows);
6787 data->saved_windows = tem;
6788 for (i = 0; i < n_windows; i++)
6789 ASET (tem, i,
6790 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6791 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6792 XSETWINDOW_CONFIGURATION (tem, data);
6793 return (tem);
6796 /* Called after W's margins, fringes or scroll bars was adjusted. */
6798 static void
6799 apply_window_adjustment (struct window *w)
6801 eassert (w);
6802 clear_glyph_matrix (w->current_matrix);
6803 w->window_end_valid = false;
6804 wset_redisplay (w);
6805 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6809 /***********************************************************************
6810 Marginal Areas
6811 ***********************************************************************/
6813 static int
6814 extract_dimension (Lisp_Object dimension)
6816 if (NILP (dimension))
6817 return -1;
6818 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6819 return XINT (dimension);
6822 static struct window *
6823 set_window_margins (struct window *w, Lisp_Object left_width,
6824 Lisp_Object right_width)
6826 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6827 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6828 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6830 if (w->left_margin_cols != left || w->right_margin_cols != right)
6832 /* Don't change anything if new margins won't fit. */
6833 if ((WINDOW_PIXEL_WIDTH (w)
6834 - WINDOW_FRINGES_WIDTH (w)
6835 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6836 - (left + right) * unit)
6837 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6839 w->left_margin_cols = left;
6840 w->right_margin_cols = right;
6842 return w;
6844 else
6845 return NULL;
6847 else
6848 return NULL;
6851 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6852 2, 3, 0,
6853 doc: /* Set width of marginal areas of window WINDOW.
6854 WINDOW must be a live window and defaults to the selected one.
6856 Second arg LEFT-WIDTH specifies the number of character cells to
6857 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6858 does the same for the right marginal area. A nil width parameter
6859 means no margin.
6861 Return t if any margin was actually changed and nil otherwise. */)
6862 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6864 struct window *w = set_window_margins (decode_live_window (window),
6865 left_width, right_width);
6866 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6870 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6871 0, 1, 0,
6872 doc: /* Get width of marginal areas of window WINDOW.
6873 WINDOW must be a live window and defaults to the selected one.
6875 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6876 If a marginal area does not exist, its width will be returned
6877 as nil. */)
6878 (Lisp_Object window)
6880 struct window *w = decode_live_window (window);
6881 return Fcons (w->left_margin_cols
6882 ? make_number (w->left_margin_cols) : Qnil,
6883 w->right_margin_cols
6884 ? make_number (w->right_margin_cols) : Qnil);
6889 /***********************************************************************
6890 Fringes
6891 ***********************************************************************/
6893 static struct window *
6894 set_window_fringes (struct window *w, Lisp_Object left_width,
6895 Lisp_Object right_width, Lisp_Object outside_margins)
6897 bool outside = !NILP (outside_margins);
6898 int left = extract_dimension (left_width);
6899 int right = extract_dimension (right_width);
6901 /* Do nothing on a tty or if nothing to actually change. */
6902 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6903 && (w->left_fringe_width != left
6904 || w->right_fringe_width != right
6905 || w->fringes_outside_margins != outside))
6907 if (left > 0 || right > 0)
6909 /* Don't change anything if new fringes don't fit. */
6910 if ((WINDOW_PIXEL_WIDTH (w)
6911 - WINDOW_MARGINS_WIDTH (w)
6912 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6913 - max (left, 0) - max (right, 0))
6914 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6915 return NULL;
6918 w->left_fringe_width = left;
6919 w->right_fringe_width = right;
6920 w->fringes_outside_margins = outside;
6922 return w;
6924 else
6925 return NULL;
6928 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6929 2, 4, 0,
6930 doc: /* Set the fringe widths of window WINDOW.
6931 WINDOW must be a live window and defaults to the selected one.
6933 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6934 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6935 fringe width. If a fringe width arg is nil, that means to use the
6936 frame's default fringe width. Default fringe widths can be set with
6937 the command `set-fringe-style'.
6938 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6939 outside of the display margins. By default, fringes are drawn between
6940 display marginal areas and the text area.
6942 Return t if any fringe was actually changed and nil otherwise. */)
6943 (Lisp_Object window, Lisp_Object left_width,
6944 Lisp_Object right_width, Lisp_Object outside_margins)
6946 struct window *w
6947 = set_window_fringes (decode_live_window (window),
6948 left_width, right_width, outside_margins);
6949 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6953 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6954 0, 1, 0,
6955 doc: /* Get width of fringes of window WINDOW.
6956 WINDOW must be a live window and defaults to the selected one.
6958 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6959 (Lisp_Object window)
6961 struct window *w = decode_live_window (window);
6963 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6964 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6965 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6970 /***********************************************************************
6971 Scroll bars
6972 ***********************************************************************/
6974 static struct window *
6975 set_window_scroll_bars (struct window *w, Lisp_Object width,
6976 Lisp_Object vertical_type, Lisp_Object height,
6977 Lisp_Object horizontal_type)
6979 int iwidth = extract_dimension (width);
6980 bool changed = false;
6982 if (iwidth == 0)
6983 vertical_type = Qnil;
6985 if (!(NILP (vertical_type)
6986 || EQ (vertical_type, Qleft)
6987 || EQ (vertical_type, Qright)
6988 || EQ (vertical_type, Qt)))
6989 error ("Invalid type of vertical scroll bar");
6991 if (w->scroll_bar_width != iwidth
6992 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6994 /* Don't change anything if new scroll bar won't fit. */
6995 if ((WINDOW_PIXEL_WIDTH (w)
6996 - WINDOW_MARGINS_WIDTH (w)
6997 - WINDOW_FRINGES_WIDTH (w)
6998 - max (iwidth, 0))
6999 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
7001 w->scroll_bar_width = iwidth;
7002 wset_vertical_scroll_bar_type (w, vertical_type);
7003 changed = true;
7007 #if USE_HORIZONTAL_SCROLL_BARS
7009 int iheight = extract_dimension (height);
7011 if (MINI_WINDOW_P (w) || iheight == 0)
7012 horizontal_type = Qnil;
7014 if (!(NILP (horizontal_type)
7015 || EQ (horizontal_type, Qbottom)
7016 || EQ (horizontal_type, Qt)))
7017 error ("Invalid type of horizontal scroll bar");
7019 if (w->scroll_bar_height != iheight
7020 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
7022 /* Don't change anything if new scroll bar won't fit. */
7023 if ((WINDOW_PIXEL_HEIGHT (w)
7024 - WINDOW_HEADER_LINE_HEIGHT (w)
7025 - WINDOW_MODE_LINE_HEIGHT (w)
7026 - max (iheight, 0))
7027 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7029 w->scroll_bar_height = iheight;
7030 wset_horizontal_scroll_bar_type (w, horizontal_type);
7031 changed = true;
7035 #else
7036 wset_horizontal_scroll_bar_type (w, Qnil);
7037 #endif
7039 return changed ? w : NULL;
7042 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7043 Sset_window_scroll_bars, 1, 5, 0,
7044 doc: /* Set width and type of scroll bars of window WINDOW.
7045 WINDOW must be a live window and defaults to the selected one.
7047 Second parameter WIDTH specifies the pixel width for the vertical scroll
7048 bar. If WIDTH is nil, use the scroll bar width of WINDOW's frame.
7049 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7050 bar: left, right, nil or t where nil means to not display a vertical
7051 scroll bar on WINDOW and t means to use WINDOW frame's vertical scroll
7052 bar type.
7054 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7055 scroll bar. If HEIGHT is nil, use the scroll bar height of WINDOW's
7056 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7057 horizontal scroll bar: bottom, nil, or t where nil means to not display
7058 a horizontal scroll bar on WINDOW and t means to use WINDOW frame's
7059 horizontal scroll bar type.
7061 Return t if scroll bars were actually changed and nil otherwise. */)
7062 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7063 Lisp_Object height, Lisp_Object horizontal_type)
7065 struct window *w
7066 = set_window_scroll_bars (decode_live_window (window),
7067 width, vertical_type, height, horizontal_type);
7068 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7072 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7073 0, 1, 0,
7074 doc: /* Get width and type of scroll bars of window WINDOW.
7075 WINDOW must be a live window and defaults to the selected one.
7077 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7078 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7079 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7080 value. */)
7081 (Lisp_Object window)
7083 struct window *w = decode_live_window (window);
7085 return Fcons (((w->scroll_bar_width >= 0)
7086 ? make_number (w->scroll_bar_width)
7087 : Qnil),
7088 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7089 w->vertical_scroll_bar_type,
7090 ((w->scroll_bar_height >= 0)
7091 ? make_number (w->scroll_bar_height)
7092 : Qnil),
7093 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7094 w->horizontal_scroll_bar_type));
7097 /***********************************************************************
7098 Smooth scrolling
7099 ***********************************************************************/
7101 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7102 doc: /* Return the amount by which WINDOW is scrolled vertically.
7103 If WINDOW is omitted or nil, it defaults to the selected window.
7104 Normally, value is a multiple of the canonical character height of WINDOW;
7105 optional second arg PIXELS-P means value is measured in pixels. */)
7106 (Lisp_Object window, Lisp_Object pixels_p)
7108 Lisp_Object result;
7109 struct window *w = decode_live_window (window);
7110 struct frame *f = XFRAME (w->frame);
7112 if (FRAME_WINDOW_P (f))
7113 result = (NILP (pixels_p)
7114 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7115 : make_number (-w->vscroll));
7116 else
7117 result = make_number (0);
7118 return result;
7122 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7123 2, 3, 0,
7124 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7125 WINDOW nil means use the selected window. Normally, VSCROLL is a
7126 non-negative multiple of the canonical character height of WINDOW;
7127 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7128 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7129 corresponds to an integral number of pixels. The return value is the
7130 result of this rounding.
7131 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7132 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7134 struct window *w = decode_live_window (window);
7135 struct frame *f = XFRAME (w->frame);
7137 CHECK_NUMBER_OR_FLOAT (vscroll);
7139 if (FRAME_WINDOW_P (f))
7141 int old_dy = w->vscroll;
7143 w->vscroll = - (NILP (pixels_p)
7144 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7145 : XFLOATINT (vscroll));
7146 w->vscroll = min (w->vscroll, 0);
7148 if (w->vscroll != old_dy)
7150 /* Adjust glyph matrix of the frame if the virtual display
7151 area becomes larger than before. */
7152 if (w->vscroll < 0 && w->vscroll < old_dy)
7153 adjust_frame_glyphs (f);
7155 /* Prevent redisplay shortcuts. */
7156 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7160 return Fwindow_vscroll (window, pixels_p);
7164 /* Call FN for all leaf windows on frame F. FN is called with the
7165 first argument being a pointer to the leaf window, and with
7166 additional argument USER_DATA. Stops when FN returns 0. */
7168 static void
7169 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7170 void *user_data)
7172 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7173 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7174 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7178 /* Helper function for foreach_window. Call FN for all leaf windows
7179 reachable from W. FN is called with the first argument being a
7180 pointer to the leaf window, and with additional argument USER_DATA.
7181 Stop when FN returns false. Value is false if stopped by FN. */
7183 static bool
7184 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7185 void *user_data)
7187 bool cont;
7189 for (cont = true; w && cont;)
7191 if (WINDOWP (w->contents))
7192 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7193 else
7194 cont = fn (w, user_data);
7196 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7199 return cont;
7202 /***********************************************************************
7203 Initialization
7204 ***********************************************************************/
7206 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7207 describe the same state of affairs. This is used by Fequal.
7209 IGNORE_POSITIONS means ignore non-matching scroll positions
7210 and the like.
7212 This ignores a couple of things like the dedication status of
7213 window, combination_limit and the like. This might have to be
7214 fixed. */
7216 bool
7217 compare_window_configurations (Lisp_Object configuration1,
7218 Lisp_Object configuration2,
7219 bool ignore_positions)
7221 register struct save_window_data *d1, *d2;
7222 struct Lisp_Vector *sws1, *sws2;
7223 ptrdiff_t i;
7225 CHECK_WINDOW_CONFIGURATION (configuration1);
7226 CHECK_WINDOW_CONFIGURATION (configuration2);
7228 d1 = (struct save_window_data *) XVECTOR (configuration1);
7229 d2 = (struct save_window_data *) XVECTOR (configuration2);
7230 sws1 = XVECTOR (d1->saved_windows);
7231 sws2 = XVECTOR (d2->saved_windows);
7233 /* Frame settings must match. */
7234 if (d1->frame_cols != d2->frame_cols
7235 || d1->frame_lines != d2->frame_lines
7236 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7237 || !EQ (d1->selected_frame, d2->selected_frame)
7238 || !EQ (d1->f_current_buffer, d2->f_current_buffer)
7239 || (!ignore_positions
7240 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7241 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7242 || !EQ (d1->focus_frame, d2->focus_frame)
7243 /* Verify that the two configurations have the same number of windows. */
7244 || sws1->header.size != sws2->header.size)
7245 return false;
7247 for (i = 0; i < sws1->header.size; i++)
7249 struct saved_window *sw1, *sw2;
7251 sw1 = SAVED_WINDOW_N (sws1, i);
7252 sw2 = SAVED_WINDOW_N (sws2, i);
7254 if (
7255 /* The "current" windows in the two configurations must
7256 correspond to each other. */
7257 EQ (d1->current_window, sw1->window)
7258 != EQ (d2->current_window, sw2->window)
7259 /* Windows' buffers must match. */
7260 || !EQ (sw1->buffer, sw2->buffer)
7261 || !EQ (sw1->pixel_left, sw2->pixel_left)
7262 || !EQ (sw1->pixel_top, sw2->pixel_top)
7263 || !EQ (sw1->pixel_height, sw2->pixel_height)
7264 || !EQ (sw1->pixel_width, sw2->pixel_width)
7265 || !EQ (sw1->left_col, sw2->left_col)
7266 || !EQ (sw1->top_line, sw2->top_line)
7267 || !EQ (sw1->total_cols, sw2->total_cols)
7268 || !EQ (sw1->total_lines, sw2->total_lines)
7269 || !EQ (sw1->display_table, sw2->display_table)
7270 /* The next two disjuncts check the window structure for
7271 equality. */
7272 || !EQ (sw1->parent, sw2->parent)
7273 || !EQ (sw1->prev, sw2->prev)
7274 || (!ignore_positions
7275 && (!EQ (sw1->hscroll, sw2->hscroll)
7276 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7277 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7278 || NILP (Fequal (sw1->start, sw2->start))
7279 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7280 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7281 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7282 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7283 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7284 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7285 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7286 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7287 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7288 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7289 return false;
7292 return true;
7295 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7296 Scompare_window_configurations, 2, 2, 0,
7297 doc: /* Compare two window configurations as regards the structure of windows.
7298 This function ignores details such as the values of point
7299 and scrolling positions. */)
7300 (Lisp_Object x, Lisp_Object y)
7302 if (compare_window_configurations (x, y, true))
7303 return Qt;
7304 return Qnil;
7307 void
7308 init_window_once (void)
7310 struct frame *f = make_initial_frame ();
7311 XSETFRAME (selected_frame, f);
7312 Vterminal_frame = selected_frame;
7313 minibuf_window = f->minibuffer_window;
7314 selected_window = f->selected_window;
7317 void
7318 init_window (void)
7320 Vwindow_list = Qnil;
7323 void
7324 syms_of_window (void)
7326 DEFSYM (Qscroll_up, "scroll-up");
7327 DEFSYM (Qscroll_down, "scroll-down");
7328 DEFSYM (Qscroll_command, "scroll-command");
7330 Fput (Qscroll_up, Qscroll_command, Qt);
7331 Fput (Qscroll_down, Qscroll_command, Qt);
7333 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7334 DEFSYM (Qwindowp, "windowp");
7335 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7336 DEFSYM (Qwindow_live_p, "window-live-p");
7337 DEFSYM (Qwindow_valid_p, "window-valid-p");
7338 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7339 DEFSYM (Qdelete_window, "delete-window");
7340 DEFSYM (Qwindow__resize_root_window, "window--resize-root-window");
7341 DEFSYM (Qwindow__resize_root_window_vertically,
7342 "window--resize-root-window-vertically");
7343 DEFSYM (Qwindow__sanitize_window_sizes, "window--sanitize-window-sizes");
7344 DEFSYM (Qwindow__pixel_to_total, "window--pixel-to-total");
7345 DEFSYM (Qsafe, "safe");
7346 DEFSYM (Qdisplay_buffer, "display-buffer");
7347 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7348 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7349 DEFSYM (Qget_mru_window, "get-mru-window");
7350 DEFSYM (Qwindow_size, "window-size");
7351 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7352 DEFSYM (Qabove, "above");
7353 DEFSYM (Qclone_of, "clone-of");
7354 DEFSYM (Qfloor, "floor");
7355 DEFSYM (Qceiling, "ceiling");
7356 DEFSYM (Qmark_for_redisplay, "mark-for-redisplay");
7358 staticpro (&Vwindow_list);
7360 minibuf_selected_window = Qnil;
7361 staticpro (&minibuf_selected_window);
7363 window_scroll_pixel_based_preserve_x = -1;
7364 window_scroll_pixel_based_preserve_y = -1;
7365 window_scroll_preserve_hpos = -1;
7366 window_scroll_preserve_vpos = -1;
7368 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7369 doc: /* Non-nil means call as function to display a help buffer.
7370 The function is called with one argument, the buffer to be displayed.
7371 Used by `with-output-to-temp-buffer'.
7372 If this function is used, then it must do the entire job of showing
7373 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7374 Vtemp_buffer_show_function = Qnil;
7376 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7377 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7378 Vminibuf_scroll_window = Qnil;
7380 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7381 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7382 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7383 is displayed in the `mode-line' face. */);
7384 mode_line_in_non_selected_windows = true;
7386 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7387 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7388 Vother_window_scroll_buffer = Qnil;
7390 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7391 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7392 auto_window_vscroll_p = true;
7394 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7395 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7396 next_screen_context_lines = 2;
7398 DEFVAR_LISP ("scroll-preserve-screen-position",
7399 Vscroll_preserve_screen_position,
7400 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7401 A value of nil means point does not keep its screen position except
7402 at the scroll margin or window boundary respectively.
7403 A value of t means point keeps its screen position if the scroll
7404 command moved it vertically out of the window, e.g. when scrolling
7405 by full screens.
7406 Any other value means point always keeps its screen position.
7407 Scroll commands should have the `scroll-command' property
7408 on their symbols to be controlled by this variable. */);
7409 Vscroll_preserve_screen_position = Qnil;
7411 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7412 doc: /* Type of marker to use for `window-point'. */);
7413 Vwindow_point_insertion_type = Qnil;
7414 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7416 DEFVAR_LISP ("window-configuration-change-hook",
7417 Vwindow_configuration_change_hook,
7418 doc: /* Functions to call when window configuration changes.
7419 The buffer-local part is run once per window, with the relevant window
7420 selected; while the global part is run only once for the modified frame,
7421 with the relevant frame selected. */);
7422 Vwindow_configuration_change_hook = Qnil;
7424 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
7425 doc: /* Functions called during redisplay, if window sizes have changed.
7426 The value should be a list of functions that take one argument.
7427 During the first part of redisplay, for each frame, if any of its windows
7428 have changed size since the last redisplay, or have been split or deleted,
7429 all the functions in the list are called, with the frame as argument.
7430 If redisplay decides to resize the minibuffer window, it calls these
7431 functions on behalf of that as well. */);
7432 Vwindow_size_change_functions = Qnil;
7434 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7435 doc: /* Non-nil means `recenter' redraws entire frame.
7436 If this option is non-nil, then the `recenter' command with a nil
7437 argument will redraw the entire frame; the special value `tty' causes
7438 the frame to be redrawn only if it is a tty frame. */);
7439 Vrecenter_redisplay = Qtty;
7441 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7442 doc: /* If t, resize window combinations proportionally.
7443 If this variable is nil, splitting a window gets the entire screen space
7444 for displaying the new window from the window to split. Deleting and
7445 resizing a window preferably resizes one adjacent window only.
7447 If this variable is t, splitting a window tries to get the space
7448 proportionally from all windows in the same combination. This also
7449 allows splitting a window that is otherwise too small or of fixed size.
7450 Resizing and deleting a window proportionally resize all windows in the
7451 same combination.
7453 Other values are reserved for future use.
7455 A specific split operation may ignore the value of this variable if it
7456 is affected by a non-nil value of `window-combination-limit'. */);
7457 Vwindow_combination_resize = Qnil;
7459 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7460 doc: /* If non-nil, splitting a window makes a new parent window.
7461 The following values are recognized:
7463 nil means splitting a window will create a new parent window only if the
7464 window has no parent window or the window shall become part of a
7465 combination orthogonal to the one it is part of.
7467 `window-size' means that splitting a window for displaying a buffer
7468 makes a new parent window provided `display-buffer' is supposed to
7469 explicitly set the window's size due to the presence of a
7470 `window-height' or `window-width' entry in the alist used by
7471 `display-buffer'. Otherwise, this value is handled like nil.
7473 `temp-buffer-resize' means that splitting a window for displaying a
7474 temporary buffer via `with-temp-buffer-window' makes a new parent
7475 window only if `temp-buffer-resize-mode' is enabled. Otherwise,
7476 this value is handled like nil.
7478 `temp-buffer' means that splitting a window for displaying a temporary
7479 buffer via `with-temp-buffer-window' always makes a new parent
7480 window. Otherwise, this value is handled like nil.
7482 `display-buffer' means that splitting a window for displaying a buffer
7483 always makes a new parent window. Since temporary buffers are
7484 displayed by the function `display-buffer', this value is stronger
7485 than `temp-buffer'. Splitting a window for other purpose makes a
7486 new parent window only if needed.
7488 t means that splitting a window always creates a new parent window. If
7489 all splits behave this way, each frame's window tree is a binary
7490 tree and every window but the frame's root window has exactly one
7491 sibling.
7493 The default value is `window-size'. Other values are reserved for
7494 future use. */);
7495 Vwindow_combination_limit = Qwindow_size;
7497 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7498 doc: /* Alist of persistent window parameters.
7499 This alist specifies which window parameters shall get saved by
7500 `current-window-configuration' and `window-state-get' and subsequently
7501 restored to their previous values by `set-window-configuration' and
7502 `window-state-put'.
7504 The car of each entry of this alist is the symbol specifying the
7505 parameter. The cdr is one of the following:
7507 nil means the parameter is neither saved by `window-state-get' nor by
7508 `current-window-configuration'.
7510 t means the parameter is saved by `current-window-configuration' and,
7511 provided its WRITABLE argument is nil, by `window-state-get'.
7513 The symbol `writable' means the parameter is saved unconditionally by
7514 both `current-window-configuration' and `window-state-get'. Do not use
7515 this value for parameters without read syntax (like windows or frames).
7517 Parameters not saved by `current-window-configuration' or
7518 `window-state-get' are left alone by `set-window-configuration'
7519 respectively are not installed by `window-state-put'. */);
7520 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7522 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7523 doc: /* Non-nil means resize windows pixelwise.
7524 This currently affects the functions: `split-window', `maximize-window',
7525 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7526 all functions that symmetrically resize a parent window.
7528 Note that when a frame's pixel size is not a multiple of the
7529 frame's character size, at least one window may get resized
7530 pixelwise even if this option is nil. */);
7531 window_resize_pixelwise = false;
7533 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7534 Vfast_but_imprecise_scrolling,
7535 doc: /* When non-nil, accelerate scrolling operations.
7536 This comes into play when scrolling rapidly over previously
7537 unfontified buffer regions. Only those portions of the buffer which
7538 are actually going to be displayed get fontified.
7540 Note that this optimization can cause the portion of the buffer
7541 displayed after a scrolling operation to be somewhat inaccurate. */);
7542 Vfast_but_imprecise_scrolling = false;
7544 defsubr (&Sselected_window);
7545 defsubr (&Sminibuffer_window);
7546 defsubr (&Swindow_minibuffer_p);
7547 defsubr (&Swindowp);
7548 defsubr (&Swindow_valid_p);
7549 defsubr (&Swindow_live_p);
7550 defsubr (&Swindow_frame);
7551 defsubr (&Sframe_root_window);
7552 defsubr (&Sframe_first_window);
7553 defsubr (&Sframe_selected_window);
7554 defsubr (&Sset_frame_selected_window);
7555 defsubr (&Spos_visible_in_window_p);
7556 defsubr (&Swindow_line_height);
7557 defsubr (&Swindow_buffer);
7558 defsubr (&Swindow_parent);
7559 defsubr (&Swindow_top_child);
7560 defsubr (&Swindow_left_child);
7561 defsubr (&Swindow_next_sibling);
7562 defsubr (&Swindow_prev_sibling);
7563 defsubr (&Swindow_combination_limit);
7564 defsubr (&Sset_window_combination_limit);
7565 defsubr (&Swindow_use_time);
7566 defsubr (&Swindow_pixel_width);
7567 defsubr (&Swindow_pixel_height);
7568 defsubr (&Swindow_pixel_width_before_size_change);
7569 defsubr (&Swindow_pixel_height_before_size_change);
7570 defsubr (&Swindow_total_width);
7571 defsubr (&Swindow_total_height);
7572 defsubr (&Swindow_normal_size);
7573 defsubr (&Swindow_new_pixel);
7574 defsubr (&Swindow_new_total);
7575 defsubr (&Swindow_new_normal);
7576 defsubr (&Swindow_pixel_left);
7577 defsubr (&Swindow_pixel_top);
7578 defsubr (&Swindow_left_column);
7579 defsubr (&Swindow_top_line);
7580 defsubr (&Sset_window_new_pixel);
7581 defsubr (&Sset_window_new_total);
7582 defsubr (&Sset_window_new_normal);
7583 defsubr (&Swindow_resize_apply);
7584 defsubr (&Swindow_resize_apply_total);
7585 defsubr (&Swindow_body_height);
7586 defsubr (&Swindow_body_width);
7587 defsubr (&Swindow_hscroll);
7588 defsubr (&Sset_window_hscroll);
7589 defsubr (&Swindow_redisplay_end_trigger);
7590 defsubr (&Sset_window_redisplay_end_trigger);
7591 defsubr (&Swindow_mode_line_height);
7592 defsubr (&Swindow_header_line_height);
7593 defsubr (&Swindow_right_divider_width);
7594 defsubr (&Swindow_bottom_divider_width);
7595 defsubr (&Swindow_scroll_bar_width);
7596 defsubr (&Swindow_scroll_bar_height);
7597 defsubr (&Scoordinates_in_window_p);
7598 defsubr (&Swindow_at);
7599 defsubr (&Swindow_point);
7600 defsubr (&Swindow_old_point);
7601 defsubr (&Swindow_start);
7602 defsubr (&Swindow_end);
7603 defsubr (&Sset_window_point);
7604 defsubr (&Sset_window_start);
7605 defsubr (&Swindow_dedicated_p);
7606 defsubr (&Sset_window_dedicated_p);
7607 defsubr (&Swindow_display_table);
7608 defsubr (&Sset_window_display_table);
7609 defsubr (&Snext_window);
7610 defsubr (&Sprevious_window);
7611 defsubr (&Sget_buffer_window);
7612 defsubr (&Sdelete_other_windows_internal);
7613 defsubr (&Sdelete_window_internal);
7614 defsubr (&Sresize_mini_window_internal);
7615 defsubr (&Sset_window_buffer);
7616 defsubr (&Srun_window_configuration_change_hook);
7617 defsubr (&Srun_window_scroll_functions);
7618 defsubr (&Sselect_window);
7619 defsubr (&Sforce_window_update);
7620 defsubr (&Ssplit_window_internal);
7621 defsubr (&Sscroll_up);
7622 defsubr (&Sscroll_down);
7623 defsubr (&Sscroll_left);
7624 defsubr (&Sscroll_right);
7625 defsubr (&Sother_window_for_scrolling);
7626 defsubr (&Sscroll_other_window);
7627 defsubr (&Sminibuffer_selected_window);
7628 defsubr (&Srecenter);
7629 defsubr (&Swindow_text_width);
7630 defsubr (&Swindow_text_height);
7631 defsubr (&Smove_to_window_line);
7632 defsubr (&Swindow_configuration_p);
7633 defsubr (&Swindow_configuration_frame);
7634 defsubr (&Sset_window_configuration);
7635 defsubr (&Scurrent_window_configuration);
7636 defsubr (&Sset_window_margins);
7637 defsubr (&Swindow_margins);
7638 defsubr (&Sset_window_fringes);
7639 defsubr (&Swindow_fringes);
7640 defsubr (&Sset_window_scroll_bars);
7641 defsubr (&Swindow_scroll_bars);
7642 defsubr (&Swindow_vscroll);
7643 defsubr (&Sset_window_vscroll);
7644 defsubr (&Scompare_window_configurations);
7645 defsubr (&Swindow_list);
7646 defsubr (&Swindow_list_1);
7647 defsubr (&Swindow_prev_buffers);
7648 defsubr (&Sset_window_prev_buffers);
7649 defsubr (&Swindow_next_buffers);
7650 defsubr (&Sset_window_next_buffers);
7651 defsubr (&Swindow_parameters);
7652 defsubr (&Swindow_parameter);
7653 defsubr (&Sset_window_parameter);
7656 void
7657 keys_of_window (void)
7659 initial_define_key (control_x_map, '<', "scroll-left");
7660 initial_define_key (control_x_map, '>', "scroll-right");
7662 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7663 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7664 initial_define_key (meta_map, 'v', "scroll-down-command");