First commit to scratch/follow. Make Isearch work with Follow Mode, etc.
[emacs.git] / src / window.c
blobefb4c9b4f8cd24ba3e39e6a61e147e476e4b8fe9
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2015 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "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 #ifdef HAVE_WINDOW_SYSTEM
39 #include TERM_HEADER
40 #endif /* HAVE_WINDOW_SYSTEM */
41 #ifdef MSDOS
42 #include "msdos.h"
43 #endif
45 static ptrdiff_t count_windows (struct window *);
46 static ptrdiff_t get_leaf_windows (struct window *, struct window **,
47 ptrdiff_t);
48 static void window_scroll_pixel_based (Lisp_Object, int, bool, bool);
49 static void window_scroll_line_based (Lisp_Object, int, bool, bool);
50 static void foreach_window (struct frame *,
51 bool (* fn) (struct window *, void *),
52 void *);
53 static bool foreach_window_1 (struct window *,
54 bool (* fn) (struct window *, void *),
55 void *);
56 static bool window_resize_check (struct window *, bool);
57 static void window_resize_apply (struct window *, bool);
58 static void select_window_1 (Lisp_Object, bool);
60 static struct window *set_window_fringes (struct window *, Lisp_Object,
61 Lisp_Object, Lisp_Object);
62 static struct window *set_window_margins (struct window *, Lisp_Object,
63 Lisp_Object);
64 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
65 Lisp_Object, Lisp_Object,
66 Lisp_Object);
67 static void apply_window_adjustment (struct window *);
69 /* This is the window in which the terminal's cursor should
70 be left when nothing is being done with it. This must
71 always be a leaf window, and its buffer is selected by
72 the top level editing loop at the end of each command.
74 This value is always the same as
75 FRAME_SELECTED_WINDOW (selected_frame). */
76 Lisp_Object selected_window;
78 /* A list of all windows for use by next_window and Fwindow_list.
79 Functions creating or deleting windows should invalidate this cache
80 by setting it to nil. */
81 Lisp_Object Vwindow_list;
83 /* The mini-buffer window of the selected frame.
84 Note that you cannot test for mini-bufferness of an arbitrary window
85 by comparing against this; but you can test for mini-bufferness of
86 the selected window. */
87 Lisp_Object minibuf_window;
89 /* Non-nil means it is the window whose mode line should be
90 shown as the selected window when the minibuffer is selected. */
91 Lisp_Object minibuf_selected_window;
93 /* Incremented for each window created. */
94 static EMACS_INT sequence_number;
96 /* Used by the function window_scroll_pixel_based. */
97 static int window_scroll_pixel_based_preserve_x;
98 static int window_scroll_pixel_based_preserve_y;
100 /* Same for window_scroll_line_based. */
101 static EMACS_INT window_scroll_preserve_hpos;
102 static EMACS_INT window_scroll_preserve_vpos;
104 static void
105 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
107 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
110 /* These setters are used only in this file, so they can be private. */
111 static void
112 wset_combination_limit (struct window *w, Lisp_Object val)
114 w->combination_limit = val;
117 static void
118 wset_dedicated (struct window *w, Lisp_Object val)
120 w->dedicated = val;
123 static void
124 wset_display_table (struct window *w, Lisp_Object val)
126 w->display_table = val;
129 static void
130 wset_new_normal (struct window *w, Lisp_Object val)
132 w->new_normal = val;
135 static void
136 wset_new_total (struct window *w, Lisp_Object val)
138 w->new_total = val;
141 static void
142 wset_normal_cols (struct window *w, Lisp_Object val)
144 w->normal_cols = val;
147 static void
148 wset_normal_lines (struct window *w, Lisp_Object val)
150 w->normal_lines = val;
153 static void
154 wset_parent (struct window *w, Lisp_Object val)
156 w->parent = val;
159 static void
160 wset_pointm (struct window *w, Lisp_Object val)
162 w->pointm = val;
165 static void
166 wset_old_pointm (struct window *w, Lisp_Object val)
168 w->old_pointm = val;
171 static void
172 wset_start (struct window *w, Lisp_Object val)
174 w->start = val;
177 static void
178 wset_temslot (struct window *w, Lisp_Object val)
180 w->temslot = val;
183 static void
184 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
186 w->vertical_scroll_bar_type = val;
189 static void
190 wset_window_parameters (struct window *w, Lisp_Object val)
192 w->window_parameters = val;
195 static void
196 wset_combination (struct window *w, bool horflag, Lisp_Object val)
198 /* Since leaf windows never becomes non-leaf, there should
199 be no buffer and markers in start and pointm fields of W. */
200 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
201 w->contents = val;
202 /* When an internal window is deleted and VAL is nil, HORFLAG
203 is meaningless. */
204 if (!NILP (val))
205 w->horizontal = horflag;
208 static void
209 wset_update_mode_line (struct window *w)
211 /* If this window is the selected window on its frame, set the
212 global variable update_mode_lines, so that x_consider_frame_title
213 will consider this frame's title for redisplay. */
214 Lisp_Object fselected_window = XFRAME (WINDOW_FRAME (w))->selected_window;
216 if (WINDOWP (fselected_window) && XWINDOW (fselected_window) == w)
217 update_mode_lines = 42;
218 else
219 w->update_mode_line = true;
222 /* True if leaf window W doesn't reflect the actual state
223 of displayed buffer due to its text or overlays change. */
225 bool
226 window_outdated (struct window *w)
228 struct buffer *b = XBUFFER (w->contents);
229 return (w->last_modified < BUF_MODIFF (b)
230 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
233 struct window *
234 decode_live_window (register Lisp_Object window)
236 if (NILP (window))
237 return XWINDOW (selected_window);
239 CHECK_LIVE_WINDOW (window);
240 return XWINDOW (window);
243 struct window *
244 decode_any_window (register Lisp_Object window)
246 struct window *w;
248 if (NILP (window))
249 return XWINDOW (selected_window);
251 CHECK_WINDOW (window);
252 w = XWINDOW (window);
253 return w;
256 static struct window *
257 decode_valid_window (register Lisp_Object window)
259 struct window *w;
261 if (NILP (window))
262 return XWINDOW (selected_window);
264 CHECK_VALID_WINDOW (window);
265 w = XWINDOW (window);
266 return w;
269 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
270 cease its buffer, and 1 means that W is about to set up the new one. */
272 static void
273 adjust_window_count (struct window *w, int arg)
275 eassert (eabs (arg) == 1);
276 if (BUFFERP (w->contents))
278 struct buffer *b = XBUFFER (w->contents);
280 if (b->base_buffer)
281 b = b->base_buffer;
282 b->window_count += arg;
283 eassert (b->window_count >= 0);
284 /* These should be recalculated by redisplay code. */
285 w->window_end_valid = false;
286 w->base_line_pos = 0;
290 /* Set W's buffer slot to VAL and recompute number
291 of windows showing VAL if it is a buffer. */
293 void
294 wset_buffer (struct window *w, Lisp_Object val)
296 adjust_window_count (w, -1);
297 if (BUFFERP (val))
298 /* Make sure that we do not assign the buffer
299 to an internal window. */
300 eassert (MARKERP (w->start) && MARKERP (w->pointm));
301 w->contents = val;
302 adjust_window_count (w, 1);
305 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
306 doc: /* Return t if OBJECT is a window and nil otherwise. */)
307 (Lisp_Object object)
309 return WINDOWP (object) ? Qt : Qnil;
312 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
313 doc: /* Return t if OBJECT is a valid window and nil otherwise.
314 A valid window is either a window that displays a buffer or an internal
315 window. Windows that have been deleted are not valid. */)
316 (Lisp_Object object)
318 return WINDOW_VALID_P (object) ? Qt : Qnil;
321 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
322 doc: /* Return t if OBJECT is a live window and nil otherwise.
323 A live window is a window that displays a buffer.
324 Internal windows and deleted windows are not live. */)
325 (Lisp_Object object)
327 return WINDOW_LIVE_P (object) ? Qt : Qnil;
330 /* Frames and windows. */
331 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
332 doc: /* Return the frame that window WINDOW is on.
333 WINDOW must be a valid window and defaults to the selected one. */)
334 (Lisp_Object window)
336 return decode_valid_window (window)->frame;
339 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
340 doc: /* Return the root window of FRAME-OR-WINDOW.
341 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
342 With a frame argument, return that frame's root window.
343 With a window argument, return the root window of that window's frame. */)
344 (Lisp_Object frame_or_window)
346 Lisp_Object window;
348 if (NILP (frame_or_window))
349 window = SELECTED_FRAME ()->root_window;
350 else if (WINDOW_VALID_P (frame_or_window))
351 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
352 else
354 CHECK_LIVE_FRAME (frame_or_window);
355 window = XFRAME (frame_or_window)->root_window;
358 return window;
361 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
362 doc: /* Return the minibuffer window for frame FRAME.
363 If FRAME is omitted or nil, it defaults to the selected frame. */)
364 (Lisp_Object frame)
366 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
369 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
370 Swindow_minibuffer_p, 0, 1, 0,
371 doc: /* Return non-nil if WINDOW is a minibuffer window.
372 WINDOW must be a valid window and defaults to the selected one. */)
373 (Lisp_Object window)
375 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
378 /* Don't move this to window.el - this must be a safe routine. */
379 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
380 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
381 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
382 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
383 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
384 the first window of that frame. */)
385 (Lisp_Object frame_or_window)
387 Lisp_Object window;
389 if (NILP (frame_or_window))
390 window = SELECTED_FRAME ()->root_window;
391 else if (WINDOW_VALID_P (frame_or_window))
392 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
393 else
395 CHECK_LIVE_FRAME (frame_or_window);
396 window = XFRAME (frame_or_window)->root_window;
399 while (WINDOWP (XWINDOW (window)->contents))
400 window = XWINDOW (window)->contents;
402 return window;
405 DEFUN ("frame-selected-window", Fframe_selected_window,
406 Sframe_selected_window, 0, 1, 0,
407 doc: /* Return the selected window of FRAME-OR-WINDOW.
408 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
409 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
410 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
411 return the selected window of that frame. */)
412 (Lisp_Object frame_or_window)
414 Lisp_Object window;
416 if (NILP (frame_or_window))
417 window = SELECTED_FRAME ()->selected_window;
418 else if (WINDOW_VALID_P (frame_or_window))
419 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
420 else
422 CHECK_LIVE_FRAME (frame_or_window);
423 window = XFRAME (frame_or_window)->selected_window;
426 return window;
429 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
430 Sset_frame_selected_window, 2, 3, 0,
431 doc: /* Set selected window of FRAME to WINDOW.
432 FRAME must be a live frame and defaults to the selected one. If FRAME
433 is the selected frame, this makes WINDOW the selected window. Optional
434 argument NORECORD non-nil means to neither change the order of recently
435 selected windows nor the buffer list. WINDOW must denote a live window.
436 Return WINDOW. */)
437 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
439 if (NILP (frame))
440 frame = selected_frame;
442 CHECK_LIVE_FRAME (frame);
443 CHECK_LIVE_WINDOW (window);
445 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
446 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
448 if (EQ (frame, selected_frame))
449 return Fselect_window (window, norecord);
450 else
452 fset_selected_window (XFRAME (frame), window);
453 return window;
457 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
458 doc: /* Return the selected window.
459 The selected window is the window in which the standard cursor for
460 selected windows appears and to which many commands apply. */)
461 (void)
463 return selected_window;
466 EMACS_INT window_select_count;
468 /* If select_window is called with inhibit_point_swap true it will
469 not store point of the old selected window's buffer back into that
470 window's pointm slot. This is needed by Fset_window_configuration to
471 avoid that the display routine is called with selected_window set to
472 Qnil causing a subsequent crash. */
473 static Lisp_Object
474 select_window (Lisp_Object window, Lisp_Object norecord,
475 bool inhibit_point_swap)
477 struct window *w;
478 struct frame *sf;
480 CHECK_LIVE_WINDOW (window);
482 w = XWINDOW (window);
484 /* Make the selected window's buffer current. */
485 Fset_buffer (w->contents);
487 if (EQ (window, selected_window) && !inhibit_point_swap)
488 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
489 way to call record_buffer from Elisp, so it's important that we call
490 record_buffer before returning here. */
491 goto record_and_return;
493 if (NILP (norecord))
494 { /* Mark the window for redisplay since the selected-window has
495 a different mode-line. */
496 wset_redisplay (XWINDOW (selected_window));
497 wset_redisplay (w);
499 else
500 redisplay_other_windows ();
502 sf = SELECTED_FRAME ();
503 if (XFRAME (WINDOW_FRAME (w)) != sf)
505 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
506 /* Use this rather than Fhandle_switch_frame
507 so that FRAME_FOCUS_FRAME is moved appropriately as we
508 move around in the state where a minibuffer in a separate
509 frame is active. */
510 Fselect_frame (WINDOW_FRAME (w), norecord);
511 /* Fselect_frame called us back so we've done all the work already. */
512 eassert (EQ (window, selected_window));
513 return window;
515 else
516 fset_selected_window (sf, window);
518 select_window_1 (window, inhibit_point_swap);
519 bset_last_selected_window (XBUFFER (w->contents), window);
521 record_and_return:
522 /* record_buffer can run QUIT, so make sure it is run only after we have
523 re-established the invariant between selected_window and selected_frame,
524 otherwise the temporary broken invariant might "escape" (bug#14161). */
525 if (NILP (norecord))
527 w->use_time = ++window_select_count;
528 record_buffer (w->contents);
531 return window;
534 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
535 (not even for redisplay's benefit), and assume that the window's frame is
536 already selected. */
537 static void
538 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
540 /* Store the old selected window's buffer's point in pointm of the old
541 selected window. It belongs to that window, and when the window is
542 not selected, must be in the window. */
543 if (!inhibit_point_swap)
545 struct window *ow = XWINDOW (selected_window);
546 if (BUFFERP (ow->contents))
547 set_marker_both (ow->pointm, ow->contents,
548 BUF_PT (XBUFFER (ow->contents)),
549 BUF_PT_BYTE (XBUFFER (ow->contents)));
552 selected_window = window;
554 /* Go to the point recorded in the window.
555 This is important when the buffer is in more
556 than one window. It also matters when
557 redisplay_window has altered point after scrolling,
558 because it makes the change only in the window. */
559 set_point_from_marker (XWINDOW (window)->pointm);
562 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
563 doc: /* Select WINDOW which must be a live window.
564 Also make WINDOW's frame the selected frame and WINDOW that frame's
565 selected window. In addition, make WINDOW's buffer current and set its
566 buffer's value of `point' to the value of WINDOW's `window-point'.
567 Return WINDOW.
569 Optional second arg NORECORD non-nil means do not put this buffer at the
570 front of the buffer list and do not make this window the most recently
571 selected one.
573 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
574 applications and internal routines often select a window temporarily for
575 various purposes; mostly, to simplify coding. As a rule, such
576 selections should be not recorded and therefore will not pollute
577 `buffer-list-update-hook'. Selections that "really count" are those
578 causing a visible change in the next redisplay of WINDOW's frame and
579 should be always recorded. So if you think of running a function each
580 time a window gets selected put it on `buffer-list-update-hook'.
582 Also note that the main editor command loop sets the current buffer to
583 the buffer of the selected window before each command. */)
584 (Lisp_Object window, Lisp_Object norecord)
586 return select_window (window, norecord, false);
589 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
590 doc: /* Return the buffer displayed in window WINDOW.
591 If WINDOW is omitted or nil, it defaults to the selected window.
592 Return nil for an internal window or a deleted window. */)
593 (Lisp_Object window)
595 struct window *w = decode_any_window (window);
596 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
599 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
600 doc: /* Return the parent window of window WINDOW.
601 WINDOW must be a valid window and defaults to the selected one.
602 Return nil for a window with no parent (e.g. a root window). */)
603 (Lisp_Object window)
605 return decode_valid_window (window)->parent;
608 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
609 doc: /* Return the topmost child window of window WINDOW.
610 WINDOW must be a valid window and defaults to the selected one.
611 Return nil if WINDOW is a live window (live windows have no children).
612 Return nil if WINDOW is an internal window whose children form a
613 horizontal combination. */)
614 (Lisp_Object window)
616 struct window *w = decode_valid_window (window);
617 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
620 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
621 doc: /* Return the leftmost child window of window WINDOW.
622 WINDOW must be a valid window and defaults to the selected one.
623 Return nil if WINDOW is a live window (live windows have no children).
624 Return nil if WINDOW is an internal window whose children form a
625 vertical combination. */)
626 (Lisp_Object window)
628 struct window *w = decode_valid_window (window);
629 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
632 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
633 doc: /* Return the next sibling window of window WINDOW.
634 WINDOW must be a valid window and defaults to the selected one.
635 Return nil if WINDOW has no next sibling. */)
636 (Lisp_Object window)
638 return decode_valid_window (window)->next;
641 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
642 doc: /* Return the previous sibling window of window WINDOW.
643 WINDOW must be a valid window and defaults to the selected one.
644 Return nil if WINDOW has no previous sibling. */)
645 (Lisp_Object window)
647 return decode_valid_window (window)->prev;
650 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
651 doc: /* Return combination limit of window WINDOW.
652 WINDOW must be a valid window used in horizontal or vertical combination.
653 If the return value is nil, child windows of WINDOW can be recombined with
654 WINDOW's siblings. A return value of t means that child windows of
655 WINDOW are never (re-)combined with WINDOW's siblings. */)
656 (Lisp_Object window)
658 struct window *w;
660 CHECK_VALID_WINDOW (window);
661 w = XWINDOW (window);
662 if (WINDOW_LEAF_P (w))
663 error ("Combination limit is meaningful for internal windows only");
664 return w->combination_limit;
667 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
668 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
669 WINDOW must be a valid window used in horizontal or vertical combination.
670 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
671 siblings. LIMIT t means that child windows of WINDOW are never
672 (re-)combined with WINDOW's siblings. Other values are reserved for
673 future use. */)
674 (Lisp_Object window, Lisp_Object limit)
676 struct window *w;
678 CHECK_VALID_WINDOW (window);
679 w = XWINDOW (window);
680 if (WINDOW_LEAF_P (w))
681 error ("Combination limit is meaningful for internal windows only");
682 wset_combination_limit (w, limit);
683 return limit;
686 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
687 doc: /* Return the use time of window WINDOW.
688 WINDOW must be a live window and defaults to the selected one.
689 The window with the highest use time is the most recently selected
690 one. The window with the lowest use time is the least recently
691 selected one. */)
692 (Lisp_Object window)
694 return make_number (decode_live_window (window)->use_time);
697 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
698 doc: /* Return the width of window WINDOW in pixels.
699 WINDOW must be a valid window and defaults to the selected one.
701 The return value includes the fringes and margins of WINDOW as well as
702 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
703 an internal window, its pixel width is the width of the screen areas
704 spanned by its children. */)
705 (Lisp_Object window)
707 return make_number (decode_valid_window (window)->pixel_width);
710 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
711 doc: /* Return the height of window WINDOW in pixels.
712 WINDOW must be a valid window and defaults to the selected one.
714 The return value includes the mode line and header line and the bottom
715 divider, if any. If WINDOW is an internal window, its pixel height is
716 the height of the screen areas spanned by its children. */)
717 (Lisp_Object window)
719 return make_number (decode_valid_window (window)->pixel_height);
722 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
723 doc: /* Return the height of window WINDOW in lines.
724 WINDOW must be a valid window and defaults to the selected one.
726 The return value includes the heights of WINDOW's mode and header line
727 and its bottom divider, if any. If WINDOW is an internal window, the
728 total height is the height of the screen areas spanned by its children.
730 If WINDOW's pixel height is not an integral multiple of its frame's
731 character height, the number of lines occupied by WINDOW is rounded
732 internally. This is done in a way such that, if WINDOW is a parent
733 window, the sum of the total heights of all its children internally
734 equals the total height of WINDOW.
736 If the optional argument ROUND is `ceiling', return the smallest integer
737 larger than WINDOW's pixel height divided by the character height of
738 WINDOW's frame. ROUND `floor' means to return the largest integer
739 smaller than WINDOW's pixel height divided by the character height of
740 WINDOW's frame. Any other value of ROUND means to return the internal
741 total height of WINDOW. */)
742 (Lisp_Object window, Lisp_Object round)
744 struct window *w = decode_valid_window (window);
746 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
747 return make_number (w->total_lines);
748 else
750 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
752 return make_number (EQ (round, Qceiling)
753 ? ((w->pixel_height + unit - 1) /unit)
754 : (w->pixel_height / unit));
758 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
759 doc: /* Return the total width of window WINDOW in columns.
760 WINDOW must be a valid window and defaults to the selected one.
762 The return value includes the widths of WINDOW's fringes, margins,
763 scroll bars and its right divider, if any. If WINDOW is an internal
764 window, the total width is the width of the screen areas spanned by its
765 children.
767 If WINDOW's pixel width is not an integral multiple of its frame's
768 character width, the number of lines occupied by WINDOW is rounded
769 internally. This is done in a way such that, if WINDOW is a parent
770 window, the sum of the total widths of all its children internally
771 equals the total width of WINDOW.
773 If the optional argument ROUND is `ceiling', return the smallest integer
774 larger than WINDOW's pixel width divided by the character width of
775 WINDOW's frame. ROUND `floor' means to return the largest integer
776 smaller than WINDOW's pixel width divided by the character width of
777 WINDOW's frame. Any other value of ROUND means to return the internal
778 total width of WINDOW. */)
779 (Lisp_Object window, Lisp_Object round)
781 struct window *w = decode_valid_window (window);
783 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
784 return make_number (w->total_cols);
785 else
787 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
789 return make_number (EQ (round, Qceiling)
790 ? ((w->pixel_width + unit - 1) /unit)
791 : (w->pixel_width / unit));
795 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
796 doc: /* Return the new total size of window WINDOW.
797 WINDOW must be a valid window and defaults to the selected one.
799 The new total size of WINDOW is the value set by the last call of
800 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
801 installed as WINDOW's total height (see `window-total-height') or total
802 width (see `window-total-width'). */)
803 (Lisp_Object window)
805 return decode_valid_window (window)->new_total;
808 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
809 doc: /* Return the normal height of window WINDOW.
810 WINDOW must be a valid window and defaults to the selected one.
811 If HORIZONTAL is non-nil, return the normal width of WINDOW.
813 The normal height of a frame's root window or a window that is
814 horizontally combined (a window that has a left or right sibling) is
815 1.0. The normal height of a window that is vertically combined (has a
816 sibling above or below) is the fraction of the window's height with
817 respect to its parent. The sum of the normal heights of all windows in a
818 vertical combination equals 1.0.
820 Similarly, the normal width of a frame's root window or a window that is
821 vertically combined equals 1.0. The normal width of a window that is
822 horizontally combined is the fraction of the window's width with respect
823 to its parent. The sum of the normal widths of all windows in a
824 horizontal combination equals 1.0.
826 The normal sizes of windows are used to restore the proportional sizes
827 of windows after they have been shrunk to their minimum sizes; for
828 example when a frame is temporarily made very small and afterwards gets
829 re-enlarged to its previous size. */)
830 (Lisp_Object window, Lisp_Object horizontal)
832 struct window *w = decode_valid_window (window);
834 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
837 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
838 doc: /* Return new normal size of window WINDOW.
839 WINDOW must be a valid window and defaults to the selected one.
841 The new normal size of WINDOW is the value set by the last call of
842 `set-window-new-normal' for WINDOW. If valid, it will be shortly
843 installed as WINDOW's normal size (see `window-normal-size'). */)
844 (Lisp_Object window)
846 return decode_valid_window (window)->new_normal;
849 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
850 doc: /* Return new pixel size of window WINDOW.
851 WINDOW must be a valid window and defaults to the selected one.
853 The new pixel size of WINDOW is the value set by the last call of
854 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
855 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
856 width (see `window-pixel-width'). */)
857 (Lisp_Object window)
859 return decode_valid_window (window)->new_pixel;
862 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
863 doc: /* Return left pixel edge of window WINDOW.
864 WINDOW must be a valid window and defaults to the selected one. */)
865 (Lisp_Object window)
867 return make_number (decode_valid_window (window)->pixel_left);
870 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
871 doc: /* Return top pixel edge of window WINDOW.
872 WINDOW must be a valid window and defaults to the selected one. */)
873 (Lisp_Object window)
875 return make_number (decode_valid_window (window)->pixel_top);
878 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
879 doc: /* Return left column of window WINDOW.
880 This is the distance, in columns, between the left edge of WINDOW and
881 the left edge of the frame's window area. For instance, the return
882 value is 0 if there is no window to the left of WINDOW.
884 WINDOW must be a valid window and defaults to the selected one. */)
885 (Lisp_Object window)
887 return make_number (decode_valid_window (window)->left_col);
890 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
891 doc: /* Return top line of window WINDOW.
892 This is the distance, in lines, between the top of WINDOW and the top
893 of the frame's window area. For instance, the return value is 0 if
894 there is no window above WINDOW.
896 WINDOW must be a valid window and defaults to the selected one. */)
897 (Lisp_Object window)
899 return make_number (decode_valid_window (window)->top_line);
902 /* Return the number of lines/pixels of W's body. Don't count any mode
903 or header line or horizontal divider of W. Rounds down to nearest
904 integer when not working pixelwise. */
905 static int
906 window_body_height (struct window *w, bool pixelwise)
908 int height = (w->pixel_height
909 - WINDOW_HEADER_LINE_HEIGHT (w)
910 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
911 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
912 : 0)
913 - WINDOW_MODE_LINE_HEIGHT (w)
914 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
916 /* Don't return a negative value. */
917 return max (pixelwise
918 ? height
919 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
923 /* Return the number of columns/pixels of W's body. Don't count columns
924 occupied by the scroll bar or the divider/vertical bar separating W
925 from its right sibling or margins. On window-systems don't count
926 fringes either. Round down to nearest integer when not working
927 pixelwise. */
929 window_body_width (struct window *w, bool pixelwise)
931 struct frame *f = XFRAME (WINDOW_FRAME (w));
933 int width = (w->pixel_width
934 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
935 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
936 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
937 : (/* A vertical bar is either 1 or 0. */
938 !FRAME_WINDOW_P (f)
939 && !WINDOW_RIGHTMOST_P (w)
940 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
941 - WINDOW_MARGINS_WIDTH (w)
942 - (FRAME_WINDOW_P (f)
943 ? WINDOW_FRINGES_WIDTH (w)
944 : 0));
946 /* Don't return a negative value. */
947 return max (pixelwise
948 ? width
949 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
953 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
954 doc: /* Return the height of WINDOW's text area.
955 WINDOW must be a live window and defaults to the selected one. Optional
956 argument PIXELWISE non-nil means return the height of WINDOW's text area
957 in pixels. The return value does not include the mode line or header
958 line or any horizontal divider.
960 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
961 pixel height divided by the character height of WINDOW's frame. This
962 means that if a line at the bottom of the text area is only partially
963 visible, that line is not counted. */)
964 (Lisp_Object window, Lisp_Object pixelwise)
966 return make_number (window_body_height (decode_live_window (window),
967 !NILP (pixelwise)));
970 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
971 doc: /* Return the width of WINDOW's text area.
972 WINDOW must be a live window and defaults to the selected one. Optional
973 argument PIXELWISE non-nil means return the width in pixels. The return
974 value does not include any vertical dividers, fringes or marginal areas,
975 or scroll bars.
977 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
978 pixel width divided by the character width of WINDOW's frame. This
979 means that if a column at the right of the text area is only partially
980 visible, that column is not counted.
982 Note that the returned value includes the column reserved for the
983 continuation glyph. */)
984 (Lisp_Object window, Lisp_Object pixelwise)
986 return make_number (window_body_width (decode_live_window (window),
987 !NILP (pixelwise)));
990 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
991 Swindow_mode_line_height, 0, 1, 0,
992 doc: /* Return the height in pixels of WINDOW's mode-line.
993 WINDOW must be a live window and defaults to the selected one. */)
994 (Lisp_Object window)
996 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
999 DEFUN ("window-header-line-height", Fwindow_header_line_height,
1000 Swindow_header_line_height, 0, 1, 0,
1001 doc: /* Return the height in pixels of WINDOW's header-line.
1002 WINDOW must be a live window and defaults to the selected one. */)
1003 (Lisp_Object window)
1005 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1008 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1009 Swindow_right_divider_width, 0, 1, 0,
1010 doc: /* Return the width in pixels of WINDOW's right divider.
1011 WINDOW must be a live window and defaults to the selected one. */)
1012 (Lisp_Object window)
1014 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1017 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1018 Swindow_bottom_divider_width, 0, 1, 0,
1019 doc: /* Return the width in pixels of WINDOW's bottom divider.
1020 WINDOW must be a live window and defaults to the selected one. */)
1021 (Lisp_Object window)
1023 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1026 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1027 Swindow_scroll_bar_width, 0, 1, 0,
1028 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1029 WINDOW must be a live window and defaults to the selected one. */)
1030 (Lisp_Object window)
1032 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1035 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1036 Swindow_scroll_bar_height, 0, 1, 0,
1037 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1038 WINDOW must be a live window and defaults to the selected one. */)
1039 (Lisp_Object window)
1041 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1044 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1045 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1046 WINDOW must be a live window and defaults to the selected one. */)
1047 (Lisp_Object window)
1049 return make_number (decode_live_window (window)->hscroll);
1052 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1053 range, returning the new amount as a fixnum. */
1054 static Lisp_Object
1055 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1057 /* Horizontal scrolling has problems with large scroll amounts.
1058 It's too slow with long lines, and even with small lines the
1059 display can be messed up. For now, though, impose only the limits
1060 required by the internal representation: horizontal scrolling must
1061 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1062 (since it's stored in a ptrdiff_t). */
1063 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1064 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1066 /* Prevent redisplay shortcuts when changing the hscroll. */
1067 if (w->hscroll != new_hscroll)
1068 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1070 w->hscroll = new_hscroll;
1071 w->suspend_auto_hscroll = true;
1073 return make_number (new_hscroll);
1076 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1077 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1078 WINDOW must be a live window and defaults to the selected one.
1079 Clip the number to a reasonable value if out of range.
1080 Return the new number. NCOL should be zero or positive.
1082 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1083 window so that the location of point moves off-window. */)
1084 (Lisp_Object window, Lisp_Object ncol)
1086 CHECK_NUMBER (ncol);
1087 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1090 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1091 Swindow_redisplay_end_trigger, 0, 1, 0,
1092 doc: /* Return WINDOW's redisplay end trigger value.
1093 WINDOW must be a live window and defaults to the selected one.
1094 See `set-window-redisplay-end-trigger' for more information. */)
1095 (Lisp_Object window)
1097 return decode_live_window (window)->redisplay_end_trigger;
1100 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1101 Sset_window_redisplay_end_trigger, 2, 2, 0,
1102 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1103 WINDOW must be a live window and defaults to the selected one. VALUE
1104 should be a buffer position (typically a marker) or nil. If it is a
1105 buffer position, then if redisplay in WINDOW reaches a position beyond
1106 VALUE, the functions in `redisplay-end-trigger-functions' are called
1107 with two arguments: WINDOW, and the end trigger value. Afterwards the
1108 end-trigger value is reset to nil. */)
1109 (register Lisp_Object window, Lisp_Object value)
1111 wset_redisplay_end_trigger (decode_live_window (window), value);
1112 return value;
1115 /* Test if the character at column X, row Y is within window W.
1116 If it is not, return ON_NOTHING;
1117 if it is on the window's vertical divider, return
1118 ON_RIGHT_DIVIDER;
1119 if it is on the window's horizontal divider, return
1120 ON_BOTTOM_DIVIDER;
1121 if it is in the window's text area, return ON_TEXT;
1122 if it is on the window's modeline, return ON_MODE_LINE;
1123 if it is on the border between the window and its right sibling,
1124 return ON_VERTICAL_BORDER;
1125 if it is on a scroll bar, return ON_SCROLL_BAR;
1126 if it is on the window's top line, return ON_HEADER_LINE;
1127 if it is in left or right fringe of the window,
1128 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1129 if it is in the marginal area to the left/right of the window,
1130 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1132 X and Y are frame relative pixel coordinates. */
1134 static enum window_part
1135 coordinates_in_window (register struct window *w, int x, int y)
1137 struct frame *f = XFRAME (WINDOW_FRAME (w));
1138 enum window_part part;
1139 int ux = FRAME_COLUMN_WIDTH (f);
1140 int left_x = WINDOW_LEFT_EDGE_X (w);
1141 int right_x = WINDOW_RIGHT_EDGE_X (w);
1142 int top_y = WINDOW_TOP_EDGE_Y (w);
1143 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1144 /* The width of the area where the vertical line can be dragged.
1145 (Between mode lines for instance. */
1146 int grabbable_width = ux;
1147 int lmargin_width, rmargin_width, text_left, text_right;
1149 /* Outside any interesting row or column? */
1150 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1151 return ON_NOTHING;
1153 /* On the horizontal window divider (which prevails the vertical
1154 divider)? */
1155 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1156 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1157 && y <= bottom_y)
1158 return ON_BOTTOM_DIVIDER;
1159 /* On vertical window divider? */
1160 else if (!WINDOW_RIGHTMOST_P (w)
1161 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1162 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1163 && x <= right_x)
1164 return ON_RIGHT_DIVIDER;
1165 /* On the horizontal scroll bar? (Including the empty space at its
1166 right!) */
1167 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1168 && y >= (bottom_y
1169 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1170 - CURRENT_MODE_LINE_HEIGHT (w)
1171 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1172 && y <= (bottom_y
1173 - CURRENT_MODE_LINE_HEIGHT (w)
1174 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1175 return ON_HORIZONTAL_SCROLL_BAR;
1176 /* On the mode or header line? */
1177 else if ((WINDOW_WANTS_MODELINE_P (w)
1178 && y >= (bottom_y
1179 - CURRENT_MODE_LINE_HEIGHT (w)
1180 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1181 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1182 && (part = ON_MODE_LINE))
1183 || (WINDOW_WANTS_HEADER_LINE_P (w)
1184 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1185 && (part = ON_HEADER_LINE)))
1187 /* If it's under/over the scroll bar portion of the mode/header
1188 line, say it's on the vertical line. That's to be able to
1189 resize windows horizontally in case we're using toolkit scroll
1190 bars. Note: If scrollbars are on the left, the window that
1191 must be eventually resized is that on the left of WINDOW. */
1192 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1193 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1194 && !WINDOW_LEFTMOST_P (w)
1195 && eabs (x - left_x) < grabbable_width)
1196 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1197 && !WINDOW_RIGHTMOST_P (w)
1198 && eabs (x - right_x) < grabbable_width)))
1199 return ON_VERTICAL_BORDER;
1200 else
1201 return part;
1204 /* In what's below, we subtract 1 when computing right_x because we
1205 want the rightmost pixel, which is given by left_pixel+width-1. */
1206 if (w->pseudo_window_p)
1208 left_x = 0;
1209 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1211 else
1213 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1214 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1217 /* Outside any interesting column? */
1218 if (x < left_x || x > right_x)
1219 return ON_VERTICAL_SCROLL_BAR;
1221 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1222 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1224 text_left = window_box_left (w, TEXT_AREA);
1225 text_right = text_left + window_box_width (w, TEXT_AREA);
1227 if (FRAME_WINDOW_P (f))
1229 if (!w->pseudo_window_p
1230 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1231 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1232 && !WINDOW_RIGHTMOST_P (w)
1233 && (eabs (x - right_x) < grabbable_width))
1234 return ON_VERTICAL_BORDER;
1236 /* Need to say "x > right_x" rather than >=, since on character
1237 terminals, the vertical line's x coordinate is right_x. */
1238 else if (!w->pseudo_window_p
1239 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1240 && !WINDOW_RIGHTMOST_P (w)
1241 /* Why check ux if we are not the rightmost window? Also
1242 shouldn't a pseudo window always be rightmost? */
1243 && x > right_x - ux)
1244 return ON_VERTICAL_BORDER;
1246 if (x < text_left)
1248 if (lmargin_width > 0
1249 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1250 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1251 : (x < left_x + lmargin_width)))
1252 return ON_LEFT_MARGIN;
1253 else
1254 return ON_LEFT_FRINGE;
1257 if (x >= text_right)
1259 if (rmargin_width > 0
1260 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1261 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1262 : (x >= right_x - rmargin_width)))
1263 return ON_RIGHT_MARGIN;
1264 else
1265 return ON_RIGHT_FRINGE;
1268 /* Everything special ruled out - must be on text area */
1269 return ON_TEXT;
1272 /* Take X is the frame-relative pixel x-coordinate, and return the
1273 x-coordinate relative to part PART of window W. */
1275 window_relative_x_coord (struct window *w, enum window_part part, int x)
1277 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1279 switch (part)
1281 case ON_TEXT:
1282 return x - window_box_left (w, TEXT_AREA);
1284 case ON_HEADER_LINE:
1285 case ON_MODE_LINE:
1286 case ON_LEFT_FRINGE:
1287 return x - left_x;
1289 case ON_RIGHT_FRINGE:
1290 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1292 case ON_LEFT_MARGIN:
1293 return (x - left_x
1294 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1295 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1297 case ON_RIGHT_MARGIN:
1298 return (x + 1
1299 - ((w->pseudo_window_p)
1300 ? WINDOW_PIXEL_WIDTH (w)
1301 : WINDOW_BOX_RIGHT_EDGE_X (w))
1302 + window_box_width (w, RIGHT_MARGIN_AREA)
1303 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1304 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1306 case ON_NOTHING:
1307 case ON_VERTICAL_BORDER:
1308 case ON_VERTICAL_SCROLL_BAR:
1309 case ON_HORIZONTAL_SCROLL_BAR:
1310 case ON_RIGHT_DIVIDER:
1311 case ON_BOTTOM_DIVIDER:
1312 return 0;
1314 default:
1315 emacs_abort ();
1320 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1321 Scoordinates_in_window_p, 2, 2, 0,
1322 doc: /* Return non-nil if COORDINATES are in WINDOW.
1323 WINDOW must be a live window and defaults to the selected one.
1324 COORDINATES is a cons of the form (X . Y), X and Y being distances
1325 measured in characters from the upper-left corner of the frame.
1326 (0 . 0) denotes the character in the upper left corner of the
1327 frame.
1328 If COORDINATES are in the text portion of WINDOW,
1329 the coordinates relative to the window are returned.
1330 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1331 If they are in the right divider of WINDOW, `right-divider' is returned.
1332 If they are in the mode line of WINDOW, `mode-line' is returned.
1333 If they are in the header line of WINDOW, `header-line' is returned.
1334 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1335 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1336 If they are on the border between WINDOW and its right sibling,
1337 `vertical-line' is returned.
1338 If they are in the windows's left or right marginal areas, `left-margin'\n\
1339 or `right-margin' is returned. */)
1340 (register Lisp_Object coordinates, Lisp_Object window)
1342 struct window *w;
1343 struct frame *f;
1344 int x, y;
1345 Lisp_Object lx, ly;
1347 w = decode_live_window (window);
1348 f = XFRAME (w->frame);
1349 CHECK_CONS (coordinates);
1350 lx = Fcar (coordinates);
1351 ly = Fcdr (coordinates);
1352 CHECK_NUMBER_OR_FLOAT (lx);
1353 CHECK_NUMBER_OR_FLOAT (ly);
1354 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1355 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1357 switch (coordinates_in_window (w, x, y))
1359 case ON_NOTHING:
1360 return Qnil;
1362 case ON_TEXT:
1363 /* Convert X and Y to window relative pixel coordinates, and
1364 return the canonical char units. */
1365 x -= window_box_left (w, TEXT_AREA);
1366 y -= WINDOW_TOP_EDGE_Y (w);
1367 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1368 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1370 case ON_MODE_LINE:
1371 return Qmode_line;
1373 case ON_VERTICAL_BORDER:
1374 return Qvertical_line;
1376 case ON_HEADER_LINE:
1377 return Qheader_line;
1379 case ON_LEFT_FRINGE:
1380 return Qleft_fringe;
1382 case ON_RIGHT_FRINGE:
1383 return Qright_fringe;
1385 case ON_LEFT_MARGIN:
1386 return Qleft_margin;
1388 case ON_RIGHT_MARGIN:
1389 return Qright_margin;
1391 case ON_VERTICAL_SCROLL_BAR:
1392 /* Historically we are supposed to return nil in this case. */
1393 return Qnil;
1395 case ON_HORIZONTAL_SCROLL_BAR:
1396 return Qnil;
1398 case ON_RIGHT_DIVIDER:
1399 return Qright_divider;
1401 case ON_BOTTOM_DIVIDER:
1402 return Qbottom_divider;
1404 default:
1405 emacs_abort ();
1410 /* Callback for foreach_window, used in window_from_coordinates.
1411 Check if window W contains coordinates specified by USER_DATA which
1412 is actually a pointer to a struct check_window_data CW.
1414 Check if window W contains coordinates *CW->x and *CW->y. If it
1415 does, return W in *CW->window, as Lisp_Object, and return in
1416 *CW->part the part of the window under coordinates *X,*Y. Return
1417 false from this function to stop iterating over windows. */
1419 struct check_window_data
1421 Lisp_Object *window;
1422 int x, y;
1423 enum window_part *part;
1426 static bool
1427 check_window_containing (struct window *w, void *user_data)
1429 struct check_window_data *cw = user_data;
1430 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1431 if (found == ON_NOTHING)
1432 return true;
1433 else
1435 *cw->part = found;
1436 XSETWINDOW (*cw->window, w);
1437 return false;
1442 /* Find the window containing frame-relative pixel position X/Y and
1443 return it as a Lisp_Object.
1445 If X, Y is on one of the window's special `window_part' elements,
1446 set *PART to the id of that element.
1448 If there is no window under X, Y return nil and leave *PART
1449 unmodified. TOOL_BAR_P means detect tool-bar windows.
1451 This function was previously implemented with a loop cycling over
1452 windows with Fnext_window, and starting with the frame's selected
1453 window. It turned out that this doesn't work with an
1454 implementation of next_window using Vwindow_list, because
1455 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1456 tree of F when this function is called asynchronously from
1457 note_mouse_highlight. The original loop didn't terminate in this
1458 case. */
1460 Lisp_Object
1461 window_from_coordinates (struct frame *f, int x, int y,
1462 enum window_part *part, bool tool_bar_p)
1464 Lisp_Object window;
1465 struct check_window_data cw;
1466 enum window_part dummy;
1468 if (part == 0)
1469 part = &dummy;
1471 window = Qnil;
1472 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1473 foreach_window (f, check_window_containing, &cw);
1475 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1476 /* If not found above, see if it's in the tool bar window, if a tool
1477 bar exists. */
1478 if (NILP (window)
1479 && tool_bar_p
1480 && WINDOWP (f->tool_bar_window)
1481 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1482 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1483 != ON_NOTHING))
1485 *part = ON_TEXT;
1486 window = f->tool_bar_window;
1488 #endif
1490 return window;
1493 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1494 doc: /* Return window containing coordinates X and Y on FRAME.
1495 FRAME must be a live frame and defaults to the selected one.
1496 The top left corner of the frame is considered to be row 0,
1497 column 0. */)
1498 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1500 struct frame *f = decode_live_frame (frame);
1502 /* Check that arguments are integers or floats. */
1503 CHECK_NUMBER_OR_FLOAT (x);
1504 CHECK_NUMBER_OR_FLOAT (y);
1506 return window_from_coordinates (f,
1507 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1508 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1509 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1510 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1511 0, false);
1514 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1515 doc: /* Return current value of point in WINDOW.
1516 WINDOW must be a live window and defaults to the selected one.
1518 For a nonselected window, this is the value point would have if that
1519 window were selected.
1521 Note that, when WINDOW is selected, the value returned is the same as
1522 that returned by `point' for WINDOW's buffer. It would be more strictly
1523 correct to return the top-level value of `point', outside of any
1524 `save-excursion' forms. But that is hard to define. */)
1525 (Lisp_Object window)
1527 register struct window *w = decode_live_window (window);
1529 if (w == XWINDOW (selected_window))
1530 return make_number (BUF_PT (XBUFFER (w->contents)));
1531 else
1532 return Fmarker_position (w->pointm);
1535 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1536 doc: /* Return old value of point in WINDOW.
1537 WINDOW must be a live window and defaults to the selected one. */)
1538 (Lisp_Object window)
1540 return Fmarker_position (decode_live_window (window)->old_pointm);
1543 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 2, 0,
1544 doc: /* Return position at which display currently starts in WINDOW.
1545 WINDOW must be a live window and defaults to the selected one.
1546 This is updated by redisplay or by calling `set-window-start'.
1548 If GROUP is non-nil, and WINDOW is part of a group of windows collectively
1549 displaying a buffer (such as with Follow Mode), return the start position of
1550 the group rather than of the individual WINDOW. This condition holds when
1551 `window-start-group-function' is set to a function, in which case
1552 `window-start' calls the function with the argument WINDOW, then returns its
1553 result, instead of doing its normal processing. */)
1554 (Lisp_Object window, Lisp_Object group)
1556 return (!NILP (group)
1557 && FUNCTIONP (Vwindow_start_group_function))
1558 ? call1 (Vwindow_start_group_function, window)
1559 : Fmarker_position (decode_live_window (window)->start);
1562 /* This is text temporarily removed from the doc string below.
1564 This function returns nil if the position is not currently known.
1565 That happens when redisplay is preempted and doesn't finish.
1566 If in that case you want to compute where the end of the window would
1567 have been if redisplay had finished, do this:
1568 (save-excursion
1569 (goto-char (window-start window))
1570 (vertical-motion (1- (window-height window)) window)
1571 (point))") */
1573 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 3, 0,
1574 doc: /* Return position at which display currently ends in WINDOW.
1575 WINDOW must be a live window and defaults to the selected one.
1576 This is updated by redisplay, when it runs to completion.
1577 Simply changing the buffer text or setting `window-start'
1578 does not update this value.
1579 Return nil if there is no recorded value. (This can happen if the
1580 last redisplay of WINDOW was preempted, and did not finish.)
1581 If UPDATE is non-nil, compute the up-to-date position
1582 if it isn't already recorded.
1584 If GROUP is non-nil, and WINDOW is part of a group of windows collectively
1585 displaying a buffer (such as with Follow Mode), return the end position of
1586 the group rather than of the individual WINDOW. This condition holds when
1587 `window-end-group-function' is set to a function, in which case `window-end'
1588 calls the function with the two arguments WINDOW and UPDATE, then returns its
1589 result, instead of doing its normal processing. */)
1590 (Lisp_Object window, Lisp_Object update, Lisp_Object group)
1592 if (!NILP (group)
1593 && FUNCTIONP (Vwindow_end_group_function))
1594 return call2 (Vwindow_end_group_function, window, update);
1596 Lisp_Object value;
1597 struct window *w = decode_live_window (window);
1598 Lisp_Object buf;
1599 struct buffer *b;
1601 buf = w->contents;
1602 CHECK_BUFFER (buf);
1603 b = XBUFFER (buf);
1605 if (! NILP (update)
1606 && (windows_or_buffers_changed
1607 || !w->window_end_valid
1608 || b->clip_changed
1609 || b->prevent_redisplay_optimizations_p
1610 || window_outdated (w))
1611 /* Don't call display routines if we didn't yet create any real
1612 frames, because the glyph matrices are not yet allocated in
1613 that case. This could happen in some code that runs in the
1614 daemon during initialization (e.g., see bug#20565). */
1615 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1617 struct text_pos startp;
1618 struct it it;
1619 struct buffer *old_buffer = NULL;
1620 void *itdata = NULL;
1622 /* Cannot use Fvertical_motion because that function doesn't
1623 cope with variable-height lines. */
1624 if (b != current_buffer)
1626 old_buffer = current_buffer;
1627 set_buffer_internal (b);
1630 /* In case W->start is out of the range, use something
1631 reasonable. This situation occurred when loading a file with
1632 `-l' containing a call to `rmail' with subsequent other
1633 commands. At the end, W->start happened to be BEG, while
1634 rmail had already narrowed the buffer. */
1635 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1637 itdata = bidi_shelve_cache ();
1638 start_display (&it, w, startp);
1639 move_it_vertically (&it, window_box_height (w));
1640 if (it.current_y < it.last_visible_y)
1641 move_it_past_eol (&it);
1642 value = make_number (IT_CHARPOS (it));
1643 bidi_unshelve_cache (itdata, false);
1645 if (old_buffer)
1646 set_buffer_internal (old_buffer);
1648 else
1649 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1651 return value;
1655 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1656 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1657 WINDOW must be a live window and defaults to the selected one.
1658 Return POS. */)
1659 (Lisp_Object window, Lisp_Object pos)
1661 register struct window *w = decode_live_window (window);
1663 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1665 if (w == XWINDOW (selected_window))
1667 if (XBUFFER (w->contents) == current_buffer)
1668 Fgoto_char (pos);
1669 else
1671 struct buffer *old_buffer = current_buffer;
1673 /* ... but here we want to catch type error before buffer change. */
1674 CHECK_NUMBER_COERCE_MARKER (pos);
1675 set_buffer_internal (XBUFFER (w->contents));
1676 Fgoto_char (pos);
1677 set_buffer_internal (old_buffer);
1680 else
1682 set_marker_restricted (w->pointm, pos, w->contents);
1683 /* We have to make sure that redisplay updates the window to show
1684 the new value of point. */
1685 wset_redisplay (w);
1688 return pos;
1691 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 4, 0,
1692 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1693 WINDOW must be a live window and defaults to the selected one. Return
1694 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1695 overriding motion of point in order to display at this exact start.
1697 If GROUP is non-nil, and WINDOW is part of a group of windows collectively
1698 displaying a buffer (such as with Follow Mode), set the start position of
1699 the group rather than of the individual WINDOW. This condition holds when
1700 `set-window-start-group-function' is set to a function, in which case
1701 `set-window-start' calls the function with the three arguments WINDOW, POS,
1702 and NOFORCE, then returns its result, instead of doing its normal
1703 processing. */)
1704 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce, Lisp_Object group)
1706 if (!NILP (group)
1707 && FUNCTIONP (Vset_window_start_group_function))
1708 return call3 (Vset_window_start_group_function, window, pos, 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;
1726 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1727 Spos_visible_in_window_p, 0, 4, 0,
1728 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1729 WINDOW must be a live window and defaults to the selected one.
1731 Return nil if that position is scrolled vertically out of view. If a
1732 character is only partially visible, nil is returned, unless the
1733 optional argument PARTIALLY is non-nil. If POS is only out of view
1734 because of horizontal scrolling, return non-nil. If POS is t, it
1735 specifies either the first position displayed on the last visible
1736 screen line in WINDOW, or the end-of-buffer position, whichever comes
1737 first. POS defaults to point in WINDOW; WINDOW defaults to the
1738 selected window.
1740 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1741 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1742 where X and Y are the pixel coordinates relative to the top left corner
1743 of the window. The remaining elements are omitted if the character after
1744 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1745 off-window at the top and bottom of the screen line ("row") containing
1746 POS, ROWH is the visible height of that row, and VPOS is the row number
1747 (zero-based).
1749 If GROUP is non-nil, and WINDOW is part of a group of windows collectively
1750 displaying a buffer (such as with Follow Mode), test whether POS is visible
1751 in the group of windows rather than in the individual WINDOW. This
1752 condition holds when `pos-visible-in-window-p-function' is set to a
1753 function, in which case `pos-visible-in-window-p' calls the function with
1754 the three arguments POS, WINDOW, and PARTIALLY, then returns its result,
1755 instead of doing its normal processing. */)
1756 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially, Lisp_Object group)
1758 if (!NILP (group)
1759 && FUNCTIONP (Vpos_visible_in_window_p_group_function))
1760 return call3 (Vpos_visible_in_window_p_group_function, pos, window, partially);
1762 struct window *w;
1763 EMACS_INT posint;
1764 struct buffer *buf;
1765 struct text_pos top;
1766 Lisp_Object in_window = Qnil;
1767 int rtop, rbot, rowh, vpos;
1768 bool fully_p = true;
1769 int x, y;
1771 w = decode_live_window (window);
1772 buf = XBUFFER (w->contents);
1773 SET_TEXT_POS_FROM_MARKER (top, w->start);
1775 if (EQ (pos, Qt))
1776 posint = -1;
1777 else if (!NILP (pos))
1779 CHECK_NUMBER_COERCE_MARKER (pos);
1780 posint = XINT (pos);
1782 else if (w == XWINDOW (selected_window))
1783 posint = PT;
1784 else
1785 posint = marker_position (w->pointm);
1787 /* If position is above window start or outside buffer boundaries,
1788 or if window start is out of range, position is not visible. */
1789 if ((EQ (pos, Qt)
1790 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1791 && CHARPOS (top) >= BUF_BEGV (buf)
1792 && CHARPOS (top) <= BUF_ZV (buf)
1793 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1795 fully_p = !rtop && !rbot;
1796 if (!NILP (partially) || fully_p)
1797 in_window = Qt;
1800 if (!NILP (in_window) && !NILP (partially))
1802 Lisp_Object part = Qnil;
1803 if (!fully_p)
1804 part = list4i (rtop, rbot, rowh, vpos);
1805 in_window = Fcons (make_number (x),
1806 Fcons (make_number (y), part));
1809 return in_window;
1813 DEFUN ("window-line-height", Fwindow_line_height,
1814 Swindow_line_height, 0, 2, 0,
1815 doc: /* Return height in pixels of text line LINE in window WINDOW.
1816 WINDOW must be a live window and defaults to the selected one.
1818 Return height of current line if LINE is omitted or nil. Return height of
1819 header or mode line if LINE is `header-line' or `mode-line'.
1820 Otherwise, LINE is a text line number starting from 0. A negative number
1821 counts from the end of the window.
1823 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1824 in pixels of the visible part of the line, VPOS and YPOS are the
1825 vertical position in lines and pixels of the line, relative to the top
1826 of the first text line, and OFFBOT is the number of off-window pixels at
1827 the bottom of the text line. If there are off-window pixels at the top
1828 of the (first) text line, YPOS is negative.
1830 Return nil if window display is not up-to-date. In that case, use
1831 `pos-visible-in-window-p' to obtain the information. */)
1832 (Lisp_Object line, Lisp_Object window)
1834 register struct window *w;
1835 register struct buffer *b;
1836 struct glyph_row *row, *end_row;
1837 int max_y, crop, i;
1838 EMACS_INT n;
1840 w = decode_live_window (window);
1842 if (noninteractive || w->pseudo_window_p)
1843 return Qnil;
1845 CHECK_BUFFER (w->contents);
1846 b = XBUFFER (w->contents);
1848 /* Fail if current matrix is not up-to-date. */
1849 if (!w->window_end_valid
1850 || windows_or_buffers_changed
1851 || b->clip_changed
1852 || b->prevent_redisplay_optimizations_p
1853 || window_outdated (w))
1854 return Qnil;
1856 if (NILP (line))
1858 i = w->cursor.vpos;
1859 if (i < 0 || i >= w->current_matrix->nrows
1860 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1861 return Qnil;
1862 max_y = window_text_bottom_y (w);
1863 goto found_row;
1866 if (EQ (line, Qheader_line))
1868 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1869 return Qnil;
1870 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1871 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1874 if (EQ (line, Qmode_line))
1876 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1877 return (row->enabled_p ?
1878 list4i (row->height,
1879 0, /* not accurate */
1880 (WINDOW_HEADER_LINE_HEIGHT (w)
1881 + window_text_bottom_y (w)),
1883 : Qnil);
1886 CHECK_NUMBER (line);
1887 n = XINT (line);
1889 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1890 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1891 max_y = window_text_bottom_y (w);
1892 i = 0;
1894 while ((n < 0 || i < n)
1895 && row <= end_row && row->enabled_p
1896 && row->y + row->height < max_y)
1897 row++, i++;
1899 if (row > end_row || !row->enabled_p)
1900 return Qnil;
1902 if (++n < 0)
1904 if (-n > i)
1905 return Qnil;
1906 row += n;
1907 i += n;
1910 found_row:
1911 crop = max (0, (row->y + row->height) - max_y);
1912 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1915 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1916 0, 1, 0,
1917 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1918 More precisely, return the value assigned by the last call of
1919 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1920 never called with WINDOW as its argument, or the value set by that
1921 function was internally reset since its last call. WINDOW must be a
1922 live window and defaults to the selected one.
1924 When a window is dedicated to its buffer, `display-buffer' will refrain
1925 from displaying another buffer in it. `get-lru-window' and
1926 `get-largest-window' treat dedicated windows specially.
1927 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1928 `kill-buffer' can delete a dedicated window and the containing frame.
1930 Functions like `set-window-buffer' may change the buffer displayed by a
1931 window, unless that window is "strongly" dedicated to its buffer, that
1932 is the value returned by `window-dedicated-p' is t. */)
1933 (Lisp_Object window)
1935 return decode_live_window (window)->dedicated;
1938 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1939 Sset_window_dedicated_p, 2, 2, 0,
1940 doc: /* Mark WINDOW as dedicated according to FLAG.
1941 WINDOW must be a live window and defaults to the selected one. FLAG
1942 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1943 mark WINDOW as non-dedicated. Return FLAG.
1945 When a window is dedicated to its buffer, `display-buffer' will refrain
1946 from displaying another buffer in it. `get-lru-window' and
1947 `get-largest-window' treat dedicated windows specially.
1948 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1949 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1950 and the containing frame.
1952 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1953 its buffer. Functions like `set-window-buffer' may change the buffer
1954 displayed by a window, unless that window is strongly dedicated to its
1955 buffer. If and when `set-window-buffer' displays another buffer in a
1956 window, it also makes sure that the window is no more dedicated. */)
1957 (Lisp_Object window, Lisp_Object flag)
1959 wset_dedicated (decode_live_window (window), flag);
1960 return flag;
1963 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1964 0, 1, 0,
1965 doc: /* Return buffers previously shown in WINDOW.
1966 WINDOW must be a live window and defaults to the selected one.
1968 The return value is a list of elements (BUFFER WINDOW-START POS),
1969 where BUFFER is a buffer, WINDOW-START is the start position of the
1970 window for that buffer, and POS is a window-specific point value. */)
1971 (Lisp_Object window)
1973 return decode_live_window (window)->prev_buffers;
1976 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1977 Sset_window_prev_buffers, 2, 2, 0,
1978 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1979 WINDOW must be a live window and defaults to the selected one.
1981 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1982 where BUFFER is a buffer, WINDOW-START is the start position of the
1983 window for that buffer, and POS is a window-specific point value. */)
1984 (Lisp_Object window, Lisp_Object prev_buffers)
1986 wset_prev_buffers (decode_live_window (window), prev_buffers);
1987 return prev_buffers;
1990 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1991 0, 1, 0,
1992 doc: /* Return list of buffers recently re-shown in WINDOW.
1993 WINDOW must be a live window and defaults to the selected one. */)
1994 (Lisp_Object window)
1996 return decode_live_window (window)->next_buffers;
1999 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2000 Sset_window_next_buffers, 2, 2, 0,
2001 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2002 WINDOW must be a live window and defaults to the selected one.
2003 NEXT-BUFFERS should be a list of buffers. */)
2004 (Lisp_Object window, Lisp_Object next_buffers)
2006 wset_next_buffers (decode_live_window (window), next_buffers);
2007 return next_buffers;
2010 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2011 0, 1, 0,
2012 doc: /* Return the parameters of WINDOW and their values.
2013 WINDOW must be a valid window and defaults to the selected one. The
2014 return value is a list of elements of the form (PARAMETER . VALUE). */)
2015 (Lisp_Object window)
2017 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2020 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2021 2, 2, 0,
2022 doc: /* Return WINDOW's value for PARAMETER.
2023 WINDOW can be any window and defaults to the selected one. */)
2024 (Lisp_Object window, Lisp_Object parameter)
2026 Lisp_Object result;
2028 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2029 return CDR_SAFE (result);
2032 DEFUN ("set-window-parameter", Fset_window_parameter,
2033 Sset_window_parameter, 3, 3, 0,
2034 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2035 WINDOW can be any window and defaults to the selected one.
2036 Return VALUE. */)
2037 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2039 register struct window *w = decode_any_window (window);
2040 Lisp_Object old_alist_elt;
2042 old_alist_elt = Fassq (parameter, w->window_parameters);
2043 if (NILP (old_alist_elt))
2044 wset_window_parameters
2045 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2046 else
2047 Fsetcdr (old_alist_elt, value);
2048 return value;
2051 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2052 0, 1, 0,
2053 doc: /* Return the display-table that WINDOW is using.
2054 WINDOW must be a live window and defaults to the selected one. */)
2055 (Lisp_Object window)
2057 return decode_live_window (window)->display_table;
2060 /* Get the display table for use on window W. This is either W's
2061 display table or W's buffer's display table. Ignore the specified
2062 tables if they are not valid; if no valid table is specified,
2063 return 0. */
2065 struct Lisp_Char_Table *
2066 window_display_table (struct window *w)
2068 struct Lisp_Char_Table *dp = NULL;
2070 if (DISP_TABLE_P (w->display_table))
2071 dp = XCHAR_TABLE (w->display_table);
2072 else if (BUFFERP (w->contents))
2074 struct buffer *b = XBUFFER (w->contents);
2076 if (DISP_TABLE_P (BVAR (b, display_table)))
2077 dp = XCHAR_TABLE (BVAR (b, display_table));
2078 else if (DISP_TABLE_P (Vstandard_display_table))
2079 dp = XCHAR_TABLE (Vstandard_display_table);
2082 return dp;
2085 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2086 doc: /* Set WINDOW's display-table to TABLE.
2087 WINDOW must be a live window and defaults to the selected one. */)
2088 (register Lisp_Object window, Lisp_Object table)
2090 wset_display_table (decode_live_window (window), table);
2091 return table;
2094 /* Record info on buffer window W is displaying
2095 when it is about to cease to display that buffer. */
2096 static void
2097 unshow_buffer (register struct window *w)
2099 Lisp_Object buf = w->contents;
2100 struct buffer *b = XBUFFER (buf);
2102 eassert (b == XMARKER (w->pointm)->buffer);
2104 #if false
2105 if (w == XWINDOW (selected_window)
2106 || ! EQ (buf, XWINDOW (selected_window)->contents))
2107 /* Do this except when the selected window's buffer
2108 is being removed from some other window. */
2109 #endif
2110 /* last_window_start records the start position that this buffer
2111 had in the last window to be disconnected from it.
2112 Now that this statement is unconditional,
2113 it is possible for the buffer to be displayed in the
2114 selected window, while last_window_start reflects another
2115 window which was recently showing the same buffer.
2116 Some people might say that might be a good thing. Let's see. */
2117 b->last_window_start = marker_position (w->start);
2119 /* Point in the selected window's buffer
2120 is actually stored in that buffer, and the window's pointm isn't used.
2121 So don't clobber point in that buffer. */
2122 if (! EQ (buf, XWINDOW (selected_window)->contents)
2123 /* Don't clobber point in current buffer either (this could be
2124 useful in connection with bug#12208).
2125 && XBUFFER (buf) != current_buffer */
2126 /* This line helps to fix Horsley's testbug.el bug. */
2127 && !(WINDOWP (BVAR (b, last_selected_window))
2128 && w != XWINDOW (BVAR (b, last_selected_window))
2129 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2130 temp_set_point_both (b,
2131 clip_to_bounds (BUF_BEGV (b),
2132 marker_position (w->pointm),
2133 BUF_ZV (b)),
2134 clip_to_bounds (BUF_BEGV_BYTE (b),
2135 marker_byte_position (w->pointm),
2136 BUF_ZV_BYTE (b)));
2138 if (WINDOWP (BVAR (b, last_selected_window))
2139 && w == XWINDOW (BVAR (b, last_selected_window)))
2140 bset_last_selected_window (b, Qnil);
2143 /* Put NEW into the window structure in place of OLD. SETFLAG false
2144 means change window structure only. Otherwise store geometry and
2145 other settings as well. */
2146 static void
2147 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2149 Lisp_Object tem;
2150 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2152 /* If OLD is its frame's root window, then NEW is the new
2153 root window for that frame. */
2154 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2155 fset_root_window (XFRAME (o->frame), new);
2157 if (setflag)
2159 n->pixel_left = o->pixel_left;
2160 n->pixel_top = o->pixel_top;
2161 n->pixel_width = o->pixel_width;
2162 n->pixel_height = o->pixel_height;
2163 n->left_col = o->left_col;
2164 n->top_line = o->top_line;
2165 n->total_cols = o->total_cols;
2166 n->total_lines = o->total_lines;
2167 wset_normal_cols (n, o->normal_cols);
2168 wset_normal_cols (o, make_float (1.0));
2169 wset_normal_lines (n, o->normal_lines);
2170 wset_normal_lines (o, make_float (1.0));
2171 n->desired_matrix = n->current_matrix = 0;
2172 n->vscroll = 0;
2173 memset (&n->cursor, 0, sizeof (n->cursor));
2174 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2175 n->last_cursor_vpos = 0;
2176 #ifdef HAVE_WINDOW_SYSTEM
2177 n->phys_cursor_type = NO_CURSOR;
2178 n->phys_cursor_width = -1;
2179 #endif
2180 n->must_be_updated_p = false;
2181 n->pseudo_window_p = false;
2182 n->window_end_vpos = 0;
2183 n->window_end_pos = 0;
2184 n->window_end_valid = false;
2187 tem = o->next;
2188 wset_next (n, tem);
2189 if (!NILP (tem))
2190 wset_prev (XWINDOW (tem), new);
2192 tem = o->prev;
2193 wset_prev (n, tem);
2194 if (!NILP (tem))
2195 wset_next (XWINDOW (tem), new);
2197 tem = o->parent;
2198 wset_parent (n, tem);
2199 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2200 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2203 /* If window WINDOW and its parent window are iso-combined, merge
2204 WINDOW's children into those of its parent window and mark WINDOW as
2205 deleted. */
2207 static void
2208 recombine_windows (Lisp_Object window)
2210 struct window *w, *p, *c;
2211 Lisp_Object parent, child;
2212 bool horflag;
2214 w = XWINDOW (window);
2215 parent = w->parent;
2216 if (!NILP (parent) && NILP (w->combination_limit))
2218 p = XWINDOW (parent);
2219 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2220 && p->horizontal == w->horizontal)
2221 /* WINDOW and PARENT are both either a vertical or a horizontal
2222 combination. */
2224 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2225 child = w->contents;
2226 c = XWINDOW (child);
2228 /* Splice WINDOW's children into its parent's children and
2229 assign new normal sizes. */
2230 if (NILP (w->prev))
2231 wset_combination (p, horflag, child);
2232 else
2234 wset_prev (c, w->prev);
2235 wset_next (XWINDOW (w->prev), child);
2238 while (c)
2240 wset_parent (c, parent);
2242 if (horflag)
2243 wset_normal_cols
2244 (c, make_float ((double) c->pixel_width
2245 / (double) p->pixel_width));
2246 else
2247 wset_normal_lines
2248 (c, make_float ((double) c->pixel_height
2249 / (double) p->pixel_height));
2251 if (NILP (c->next))
2253 if (!NILP (w->next))
2255 wset_next (c, w->next);
2256 wset_prev (XWINDOW (c->next), child);
2259 c = 0;
2261 else
2263 child = c->next;
2264 c = XWINDOW (child);
2268 /* WINDOW can be deleted now. */
2269 wset_combination (w, false, Qnil);
2274 /* If WINDOW can be deleted, delete it. */
2275 static void
2276 delete_deletable_window (Lisp_Object window)
2278 if (!NILP (call1 (Qwindow_deletable_p, window)))
2279 call1 (Qdelete_window, window);
2282 /***********************************************************************
2283 Window List
2284 ***********************************************************************/
2286 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2287 pointer. This is a callback function for foreach_window, used in
2288 the window_list function. */
2290 static bool
2291 add_window_to_list (struct window *w, void *user_data)
2293 Lisp_Object *list = user_data;
2294 Lisp_Object window;
2295 XSETWINDOW (window, w);
2296 *list = Fcons (window, *list);
2297 return true;
2301 /* Return a list of all windows, for use by next_window. If
2302 Vwindow_list is a list, return that list. Otherwise, build a new
2303 list, cache it in Vwindow_list, and return that. */
2305 Lisp_Object
2306 window_list (void)
2308 if (!CONSP (Vwindow_list))
2310 Lisp_Object tail, frame;
2312 Vwindow_list = Qnil;
2313 FOR_EACH_FRAME (tail, frame)
2315 Lisp_Object arglist = Qnil;
2317 /* We are visiting windows in canonical order, and add
2318 new windows at the front of args[1], which means we
2319 have to reverse this list at the end. */
2320 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2321 arglist = Fnreverse (arglist);
2322 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2326 return Vwindow_list;
2330 /* Value is true if WINDOW satisfies the constraints given by
2331 OWINDOW, MINIBUF and ALL_FRAMES.
2333 MINIBUF t means WINDOW may be minibuffer windows.
2334 `lambda' means WINDOW may not be a minibuffer window.
2335 a window means a specific minibuffer window
2337 ALL_FRAMES t means search all frames,
2338 nil means search just current frame,
2339 `visible' means search just visible frames on the
2340 current terminal,
2341 0 means search visible and iconified frames on the
2342 current terminal,
2343 a window means search the frame that window belongs to,
2344 a frame means consider windows on that frame, only. */
2346 static bool
2347 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2348 Lisp_Object minibuf, Lisp_Object all_frames)
2350 struct window *w = XWINDOW (window);
2351 struct frame *f = XFRAME (w->frame);
2352 bool candidate_p = true;
2354 if (!BUFFERP (w->contents))
2355 candidate_p = false;
2356 else if (MINI_WINDOW_P (w)
2357 && (EQ (minibuf, Qlambda)
2358 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2360 /* If MINIBUF is `lambda' don't consider any mini-windows.
2361 If it is a window, consider only that one. */
2362 candidate_p = false;
2364 else if (EQ (all_frames, Qt))
2365 candidate_p = true;
2366 else if (NILP (all_frames))
2368 eassert (WINDOWP (owindow));
2369 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2371 else if (EQ (all_frames, Qvisible))
2373 candidate_p = FRAME_VISIBLE_P (f)
2374 && (FRAME_TERMINAL (XFRAME (w->frame))
2375 == FRAME_TERMINAL (XFRAME (selected_frame)));
2378 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2380 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2381 #ifdef HAVE_X_WINDOWS
2382 /* Yuck!! If we've just created the frame and the
2383 window-manager requested the user to place it
2384 manually, the window may still not be considered
2385 `visible'. I'd argue it should be at least
2386 something like `iconified', but don't know how to do
2387 that yet. --Stef */
2388 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2389 && !f->output_data.x->has_been_visible)
2390 #endif
2392 && (FRAME_TERMINAL (XFRAME (w->frame))
2393 == FRAME_TERMINAL (XFRAME (selected_frame)));
2395 else if (WINDOWP (all_frames))
2396 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2397 || EQ (XWINDOW (all_frames)->frame, w->frame)
2398 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2399 else if (FRAMEP (all_frames))
2400 candidate_p = EQ (all_frames, w->frame);
2402 return candidate_p;
2406 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2407 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2408 MINIBUF, and ALL_FRAMES. */
2410 static void
2411 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2413 struct window *w = decode_live_window (*window);
2415 XSETWINDOW (*window, w);
2416 /* MINIBUF nil may or may not include minibuffers. Decide if it
2417 does. */
2418 if (NILP (*minibuf))
2419 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2420 else if (!EQ (*minibuf, Qt))
2421 *minibuf = Qlambda;
2423 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2424 => count none of them, or a specific minibuffer window (the
2425 active one) to count. */
2427 /* ALL_FRAMES nil doesn't specify which frames to include. */
2428 if (NILP (*all_frames))
2429 *all_frames
2430 = (!EQ (*minibuf, Qlambda)
2431 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2432 : Qnil);
2433 else if (EQ (*all_frames, Qvisible))
2435 else if (EQ (*all_frames, make_number (0)))
2437 else if (FRAMEP (*all_frames))
2439 else if (!EQ (*all_frames, Qt))
2440 *all_frames = Qnil;
2444 /* Return the next or previous window of WINDOW in cyclic ordering
2445 of windows. NEXT_P means return the next window. See the
2446 documentation string of next-window for the meaning of MINIBUF and
2447 ALL_FRAMES. */
2449 static Lisp_Object
2450 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2451 bool next_p)
2453 decode_next_window_args (&window, &minibuf, &all_frames);
2455 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2456 return the first window on the frame. */
2457 if (FRAMEP (all_frames)
2458 && !EQ (all_frames, XWINDOW (window)->frame))
2459 return Fframe_first_window (all_frames);
2461 if (next_p)
2463 Lisp_Object list;
2465 /* Find WINDOW in the list of all windows. */
2466 list = Fmemq (window, window_list ());
2468 /* Scan forward from WINDOW to the end of the window list. */
2469 if (CONSP (list))
2470 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2471 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2472 break;
2474 /* Scan from the start of the window list up to WINDOW. */
2475 if (!CONSP (list))
2476 for (list = Vwindow_list;
2477 CONSP (list) && !EQ (XCAR (list), window);
2478 list = XCDR (list))
2479 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2480 break;
2482 if (CONSP (list))
2483 window = XCAR (list);
2485 else
2487 Lisp_Object candidate, list;
2489 /* Scan through the list of windows for candidates. If there are
2490 candidate windows in front of WINDOW, the last one of these
2491 is the one we want. If there are candidates following WINDOW
2492 in the list, again the last one of these is the one we want. */
2493 candidate = Qnil;
2494 for (list = window_list (); CONSP (list); list = XCDR (list))
2496 if (EQ (XCAR (list), window))
2498 if (WINDOWP (candidate))
2499 break;
2501 else if (candidate_window_p (XCAR (list), window, minibuf,
2502 all_frames))
2503 candidate = XCAR (list);
2506 if (WINDOWP (candidate))
2507 window = candidate;
2510 return window;
2514 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2515 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2516 WINDOW must be a live window and defaults to the selected one. The
2517 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2518 consider.
2520 MINIBUF nil or omitted means consider the minibuffer window only if the
2521 minibuffer is active. MINIBUF t means consider the minibuffer window
2522 even if the minibuffer is not active. Any other value means do not
2523 consider the minibuffer window even if the minibuffer is active.
2525 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2526 plus the minibuffer window if specified by the MINIBUF argument. If the
2527 minibuffer counts, consider all windows on all frames that share that
2528 minibuffer too. The following non-nil values of ALL-FRAMES have special
2529 meanings:
2531 - t means consider all windows on all existing frames.
2533 - `visible' means consider all windows on all visible frames.
2535 - 0 (the number zero) means consider all windows on all visible and
2536 iconified frames.
2538 - A frame means consider all windows on that frame only.
2540 Anything else means consider all windows on WINDOW's frame and no
2541 others.
2543 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2544 `next-window' to iterate through the entire cycle of acceptable
2545 windows, eventually ending up back at the window you started with.
2546 `previous-window' traverses the same cycle, in the reverse order. */)
2547 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2549 return next_window (window, minibuf, all_frames, true);
2553 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2554 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2555 WINDOW must be a live window and defaults to the selected one. The
2556 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2557 consider.
2559 MINIBUF nil or omitted means consider the minibuffer window only if the
2560 minibuffer is active. MINIBUF t means consider the minibuffer window
2561 even if the minibuffer is not active. Any other value means do not
2562 consider the minibuffer window even if the minibuffer is active.
2564 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2565 plus the minibuffer window if specified by the MINIBUF argument. If the
2566 minibuffer counts, consider all windows on all frames that share that
2567 minibuffer too. The following non-nil values of ALL-FRAMES have special
2568 meanings:
2570 - t means consider all windows on all existing frames.
2572 - `visible' means consider all windows on all visible frames.
2574 - 0 (the number zero) means consider all windows on all visible and
2575 iconified frames.
2577 - A frame means consider all windows on that frame only.
2579 Anything else means consider all windows on WINDOW's frame and no
2580 others.
2582 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2583 use `previous-window' to iterate through the entire cycle of
2584 acceptable windows, eventually ending up back at the window you
2585 started with. `next-window' traverses the same cycle, in the
2586 reverse order. */)
2587 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2589 return next_window (window, minibuf, all_frames, false);
2593 /* Return a list of windows in cyclic ordering. Arguments are like
2594 for `next-window'. */
2596 static Lisp_Object
2597 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2599 Lisp_Object tail, list, rest;
2601 decode_next_window_args (&window, &minibuf, &all_frames);
2602 list = Qnil;
2604 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2605 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2606 list = Fcons (XCAR (tail), list);
2608 /* Rotate the list to start with WINDOW. */
2609 list = Fnreverse (list);
2610 rest = Fmemq (window, list);
2611 if (!NILP (rest) && !EQ (rest, list))
2613 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2615 XSETCDR (tail, Qnil);
2616 list = nconc2 (rest, list);
2618 return list;
2622 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2623 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2624 FRAME nil or omitted means use the selected frame.
2625 WINDOW nil or omitted means use the window selected within FRAME.
2626 MINIBUF t means include the minibuffer window, even if it isn't active.
2627 MINIBUF nil or omitted means include the minibuffer window only
2628 if it's active.
2629 MINIBUF neither nil nor t means never include the minibuffer window. */)
2630 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2632 if (NILP (window))
2633 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2634 CHECK_WINDOW (window);
2635 if (NILP (frame))
2636 frame = selected_frame;
2638 if (!EQ (frame, XWINDOW (window)->frame))
2639 error ("Window is on a different frame");
2641 return window_list_1 (window, minibuf, frame);
2645 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2646 doc: /* Return a list of all live windows.
2647 WINDOW specifies the first window to list and defaults to the selected
2648 window.
2650 Optional argument MINIBUF nil or omitted means consider the minibuffer
2651 window only if the minibuffer is active. MINIBUF t means consider the
2652 minibuffer window even if the minibuffer is not active. Any other value
2653 means do not consider the minibuffer window even if the minibuffer is
2654 active.
2656 Optional argument ALL-FRAMES nil or omitted means consider all windows
2657 on WINDOW's frame, plus the minibuffer window if specified by the
2658 MINIBUF argument. If the minibuffer counts, consider all windows on all
2659 frames that share that minibuffer too. The following non-nil values of
2660 ALL-FRAMES have special meanings:
2662 - t means consider all windows on all existing frames.
2664 - `visible' means consider all windows on all visible frames.
2666 - 0 (the number zero) means consider all windows on all visible and
2667 iconified frames.
2669 - A frame means consider all windows on that frame only.
2671 Anything else means consider all windows on WINDOW's frame and no
2672 others.
2674 If WINDOW is not on the list of windows returned, some other window will
2675 be listed first but no error is signaled. */)
2676 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2678 return window_list_1 (window, minibuf, all_frames);
2681 /* Look at all windows, performing an operation specified by TYPE
2682 with argument OBJ.
2683 If FRAMES is Qt, look at all frames;
2684 Qnil, look at just the selected frame;
2685 Qvisible, look at visible frames;
2686 a frame, just look at windows on that frame.
2687 If MINI, perform the operation on minibuffer windows too. */
2689 enum window_loop
2691 WINDOW_LOOP_UNUSED,
2692 GET_BUFFER_WINDOW, /* Arg is buffer */
2693 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2694 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2695 CHECK_ALL_WINDOWS /* Arg is ignored */
2698 static Lisp_Object
2699 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2700 Lisp_Object frames)
2702 Lisp_Object window, windows, best_window, frame_arg;
2703 bool frame_best_window_flag = false;
2704 struct frame *f;
2706 /* If we're only looping through windows on a particular frame,
2707 frame points to that frame. If we're looping through windows
2708 on all frames, frame is 0. */
2709 if (FRAMEP (frames))
2710 f = XFRAME (frames);
2711 else if (NILP (frames))
2712 f = SELECTED_FRAME ();
2713 else
2714 f = NULL;
2716 if (f)
2717 frame_arg = Qlambda;
2718 else if (EQ (frames, make_number (0)))
2719 frame_arg = frames;
2720 else if (EQ (frames, Qvisible))
2721 frame_arg = frames;
2722 else
2723 frame_arg = Qt;
2725 /* frame_arg is Qlambda to stick to one frame,
2726 Qvisible to consider all visible frames,
2727 or Qt otherwise. */
2729 /* Pick a window to start with. */
2730 if (WINDOWP (obj))
2731 window = obj;
2732 else if (f)
2733 window = FRAME_SELECTED_WINDOW (f);
2734 else
2735 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2737 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2738 best_window = Qnil;
2740 for (; CONSP (windows); windows = XCDR (windows))
2742 struct window *w;
2744 window = XCAR (windows);
2745 w = XWINDOW (window);
2747 /* Note that we do not pay attention here to whether the frame
2748 is visible, since Fwindow_list skips non-visible frames if
2749 that is desired, under the control of frame_arg. */
2750 if (!MINI_WINDOW_P (w)
2751 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2752 consider all windows. */
2753 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2754 || (mini && minibuf_level > 0))
2755 switch (type)
2757 case GET_BUFFER_WINDOW:
2758 if (EQ (w->contents, obj)
2759 /* Don't find any minibuffer window except the one that
2760 is currently in use. */
2761 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2763 if (EQ (window, selected_window))
2764 /* Preferably return the selected window. */
2765 return window;
2766 else if (EQ (XWINDOW (window)->frame, selected_frame)
2767 && !frame_best_window_flag)
2768 /* Prefer windows on the current frame (but don't
2769 choose another one if we have one already). */
2771 best_window = window;
2772 frame_best_window_flag = true;
2774 else if (NILP (best_window))
2775 best_window = window;
2777 break;
2779 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2780 /* We could simply check whether the buffer shown by window
2781 is live, and show another buffer in case it isn't. */
2782 if (EQ (w->contents, obj))
2784 /* Undedicate WINDOW. */
2785 wset_dedicated (w, Qnil);
2786 /* Make WINDOW show the buffer returned by
2787 other_buffer_safely, don't run any hooks. */
2788 set_window_buffer
2789 (window, other_buffer_safely (w->contents), false, false);
2790 /* If WINDOW is the selected window, make its buffer
2791 current. But do so only if the window shows the
2792 current buffer (Bug#6454). */
2793 if (EQ (window, selected_window)
2794 && XBUFFER (w->contents) == current_buffer)
2795 Fset_buffer (w->contents);
2797 break;
2799 case REDISPLAY_BUFFER_WINDOWS:
2800 if (EQ (w->contents, obj))
2802 mark_window_display_accurate (window, false);
2803 w->update_mode_line = true;
2804 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2805 update_mode_lines = 27;
2806 best_window = window;
2808 break;
2810 /* Check for a leaf window that has a killed buffer
2811 or broken markers. */
2812 case CHECK_ALL_WINDOWS:
2813 if (BUFFERP (w->contents))
2815 struct buffer *b = XBUFFER (w->contents);
2817 if (!BUFFER_LIVE_P (b))
2818 emacs_abort ();
2819 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2820 emacs_abort ();
2821 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2822 emacs_abort ();
2824 break;
2826 case WINDOW_LOOP_UNUSED:
2827 break;
2831 return best_window;
2834 /* Used for debugging. Abort if any window has a dead buffer. */
2836 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2837 void
2838 check_all_windows (void)
2840 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2843 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2844 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2845 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2846 the current buffer.
2848 The optional argument ALL-FRAMES specifies the frames to consider:
2850 - t means consider all windows on all existing frames.
2852 - `visible' means consider all windows on all visible frames.
2854 - 0 (the number zero) means consider all windows on all visible
2855 and iconified frames.
2857 - A frame means consider all windows on that frame only.
2859 Any other value of ALL-FRAMES means consider all windows on the
2860 selected frame and no others. */)
2861 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2863 Lisp_Object buffer;
2865 if (NILP (buffer_or_name))
2866 buffer = Fcurrent_buffer ();
2867 else
2868 buffer = Fget_buffer (buffer_or_name);
2870 if (BUFFERP (buffer))
2871 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2872 else
2873 return Qnil;
2877 static Lisp_Object
2878 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2880 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2883 /* Placeholder used by temacs -nw before window.el is loaded. */
2884 DEFUN ("window--sanitize-window-sizes", Fwindow__sanitize_window_sizes,
2885 Swindow__sanitize_window_sizes, 2, 2, 0,
2886 doc: /* */
2887 attributes: const)
2888 (Lisp_Object frame, Lisp_Object horizontal)
2890 return Qnil;
2893 Lisp_Object
2894 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
2896 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
2900 static Lisp_Object
2901 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2903 return call2 (Qwindow_pixel_to_total, frame, horizontal);
2907 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2908 Sdelete_other_windows_internal, 0, 2, "",
2909 doc: /* Make WINDOW fill its frame.
2910 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2911 and defaults to the selected one.
2913 Optional argument ROOT, if non-nil, must specify an internal window such
2914 that WINDOW is in its window subtree. If this is the case, replace ROOT
2915 by WINDOW and leave alone any windows not part of ROOT's subtree.
2917 When WINDOW is live try to reduce display jumps by keeping the text
2918 previously visible in WINDOW in the same place on the frame. Doing this
2919 depends on the value of (window-start WINDOW), so if calling this
2920 function in a program gives strange scrolling, make sure the
2921 window-start value is reasonable when this function is called. */)
2922 (Lisp_Object window, Lisp_Object root)
2924 struct window *w, *r, *s;
2925 struct frame *f;
2926 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2927 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2928 int top IF_LINT (= 0), new_top;
2930 w = decode_valid_window (window);
2931 XSETWINDOW (window, w);
2932 f = XFRAME (w->frame);
2934 if (NILP (root))
2935 /* ROOT is the frame's root window. */
2937 root = FRAME_ROOT_WINDOW (f);
2938 r = XWINDOW (root);
2940 else
2941 /* ROOT must be an ancestor of WINDOW. */
2943 r = decode_valid_window (root);
2944 pwindow = XWINDOW (window)->parent;
2945 while (!NILP (pwindow))
2946 if (EQ (pwindow, root))
2947 break;
2948 else
2949 pwindow = XWINDOW (pwindow)->parent;
2950 if (!EQ (pwindow, root))
2951 error ("Specified root is not an ancestor of specified window");
2954 if (EQ (window, root))
2955 /* A noop. */
2956 return Qnil;
2957 /* I don't understand the "top > 0" part below. If we deal with a
2958 standalone minibuffer it would have been caught by the preceding
2959 test. */
2960 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2961 error ("Can't expand minibuffer to full frame");
2963 if (BUFFERP (w->contents))
2965 startpos = marker_position (w->start);
2966 startbyte = marker_byte_position (w->start);
2967 top = (WINDOW_TOP_EDGE_LINE (w)
2968 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2969 /* Make sure WINDOW is the frame's selected window. */
2970 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2972 if (EQ (selected_frame, w->frame))
2973 Fselect_window (window, Qnil);
2974 else
2975 fset_selected_window (f, window);
2978 else
2980 /* See if the frame's selected window is a part of the window
2981 subtree rooted at WINDOW, by finding all the selected window's
2982 parents and comparing each one with WINDOW. If it isn't we
2983 need a new selected window for this frame. */
2984 swindow = FRAME_SELECTED_WINDOW (f);
2985 while (true)
2987 pwindow = swindow;
2988 while (!NILP (pwindow) && !EQ (window, pwindow))
2989 pwindow = XWINDOW (pwindow)->parent;
2991 if (EQ (window, pwindow))
2992 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2993 as the new selected window. */
2994 break;
2995 else
2996 /* Else try the previous window of SWINDOW. */
2997 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3000 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3002 if (EQ (selected_frame, w->frame))
3003 Fselect_window (swindow, Qnil);
3004 else
3005 fset_selected_window (f, swindow);
3009 block_input ();
3010 if (!FRAME_INITIAL_P (f))
3012 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3014 /* We are going to free the glyph matrices of WINDOW, and with
3015 that we might lose any information about glyph rows that have
3016 some of their glyphs highlighted in mouse face. (These rows
3017 are marked with a mouse_face_p flag.) If WINDOW
3018 indeed has some glyphs highlighted in mouse face, signal to
3019 frame's up-to-date hook that mouse highlight was overwritten,
3020 so that it will arrange for redisplaying the highlight. */
3021 if (EQ (hlinfo->mouse_face_window, window))
3022 reset_mouse_highlight (hlinfo);
3024 free_window_matrices (r);
3026 fset_redisplay (f);
3027 Vwindow_list = Qnil;
3028 FRAME_WINDOW_SIZES_CHANGED (f) = true;
3029 bool resize_failed = false;
3031 if (!WINDOW_LEAF_P (w))
3033 /* Resize child windows vertically. */
3034 XSETINT (delta, r->pixel_height - w->pixel_height);
3035 w->pixel_top = r->pixel_top;
3036 w->top_line = r->top_line;
3037 resize_root_window (window, delta, Qnil, Qnil, Qt);
3038 if (window_resize_check (w, false))
3040 window_resize_apply (w, false);
3041 window_pixel_to_total (w->frame, Qnil);
3043 else
3045 resize_root_window (window, delta, Qnil, Qt, Qt);
3046 if (window_resize_check (w, false))
3048 window_resize_apply (w, false);
3049 window_pixel_to_total (w->frame, Qnil);
3051 else
3052 resize_failed = true;
3055 /* Resize child windows horizontally. */
3056 if (!resize_failed)
3058 w->left_col = r->left_col;
3059 w->pixel_left = r->pixel_left;
3060 XSETINT (delta, r->pixel_width - w->pixel_width);
3061 resize_root_window (window, delta, Qt, Qnil, Qt);
3062 if (window_resize_check (w, true))
3064 window_resize_apply (w, true);
3065 window_pixel_to_total (w->frame, Qt);
3067 else
3069 resize_root_window (window, delta, Qt, Qt, Qt);
3070 if (window_resize_check (w, true))
3072 window_resize_apply (w, true);
3073 window_pixel_to_total (w->frame, Qt);
3075 else
3076 resize_failed = true;
3080 if (resize_failed)
3081 /* Play safe, if we still can ... */
3083 window = swindow;
3084 w = XWINDOW (window);
3088 /* Cleanly unlink WINDOW from window-tree. */
3089 if (!NILP (w->prev))
3090 /* Get SIBLING above (on the left of) WINDOW. */
3092 sibling = w->prev;
3093 s = XWINDOW (sibling);
3094 wset_next (s, w->next);
3095 if (!NILP (s->next))
3096 wset_prev (XWINDOW (s->next), sibling);
3098 else
3099 /* Get SIBLING below (on the right of) WINDOW. */
3101 sibling = w->next;
3102 s = XWINDOW (sibling);
3103 wset_prev (s, Qnil);
3104 wset_combination (XWINDOW (w->parent),
3105 XWINDOW (w->parent)->horizontal, sibling);
3108 /* Delete ROOT and all child windows of ROOT. */
3109 if (WINDOWP (r->contents))
3111 delete_all_child_windows (r->contents);
3112 wset_combination (r, false, Qnil);
3115 replace_window (root, window, true);
3117 /* This must become SWINDOW anyway ....... */
3118 if (BUFFERP (w->contents) && !resize_failed)
3120 /* Try to minimize scrolling, by setting the window start to the
3121 point will cause the text at the old window start to be at the
3122 same place on the frame. But don't try to do this if the
3123 window start is outside the visible portion (as might happen
3124 when the display is not current, due to typeahead). */
3125 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3126 if (new_top != top
3127 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3128 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3130 struct position pos;
3131 struct buffer *obuf = current_buffer;
3133 Fset_buffer (w->contents);
3134 /* This computation used to temporarily move point, but that
3135 can have unwanted side effects due to text properties. */
3136 pos = *vmotion (startpos, startbyte, -top, w);
3138 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3139 w->window_end_valid = false;
3140 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3141 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3142 /* We need to do this, so that the window-scroll-functions
3143 get called. */
3144 w->optional_new_start = true;
3146 set_buffer_internal (obuf);
3150 adjust_frame_glyphs (f);
3151 unblock_input ();
3153 run_window_configuration_change_hook (f);
3155 return Qnil;
3159 void
3160 replace_buffer_in_windows (Lisp_Object buffer)
3162 call1 (Qreplace_buffer_in_windows, buffer);
3165 /* If BUFFER is shown in a window, safely replace it with some other
3166 buffer in all windows of all frames, even those on other keyboards. */
3168 void
3169 replace_buffer_in_windows_safely (Lisp_Object buffer)
3171 if (buffer_window_count (XBUFFER (buffer)))
3173 Lisp_Object tail, frame;
3175 /* A single call to window_loop won't do the job because it only
3176 considers frames on the current keyboard. So loop manually over
3177 frames, and handle each one. */
3178 FOR_EACH_FRAME (tail, frame)
3179 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3183 /* The following three routines are needed for running a window's
3184 configuration change hook. */
3185 static void
3186 run_funs (Lisp_Object funs)
3188 for (; CONSP (funs); funs = XCDR (funs))
3189 if (!EQ (XCAR (funs), Qt))
3190 call0 (XCAR (funs));
3193 static void
3194 select_window_norecord (Lisp_Object window)
3196 if (WINDOW_LIVE_P (window))
3197 Fselect_window (window, Qt);
3200 static void
3201 select_frame_norecord (Lisp_Object frame)
3203 if (FRAME_LIVE_P (XFRAME (frame)))
3204 Fselect_frame (frame, Qt);
3207 void
3208 run_window_configuration_change_hook (struct frame *f)
3210 ptrdiff_t count = SPECPDL_INDEX ();
3211 Lisp_Object frame, global_wcch
3212 = Fdefault_value (Qwindow_configuration_change_hook);
3213 XSETFRAME (frame, f);
3215 if (NILP (Vrun_hooks)
3216 || !(f->can_x_set_window_size)
3217 || !(f->after_make_frame))
3218 return;
3220 /* Use the right buffer. Matters when running the local hooks. */
3221 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3223 record_unwind_current_buffer ();
3224 Fset_buffer (Fwindow_buffer (Qnil));
3227 if (SELECTED_FRAME () != f)
3229 record_unwind_protect (select_frame_norecord, selected_frame);
3230 select_frame_norecord (frame);
3233 /* Look for buffer-local values. */
3235 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3236 for (; CONSP (windows); windows = XCDR (windows))
3238 Lisp_Object window = XCAR (windows);
3239 Lisp_Object buffer = Fwindow_buffer (window);
3240 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3241 buffer)))
3243 ptrdiff_t inner_count = SPECPDL_INDEX ();
3244 record_unwind_protect (select_window_norecord, selected_window);
3245 select_window_norecord (window);
3246 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3247 buffer));
3248 unbind_to (inner_count, Qnil);
3253 run_funs (global_wcch);
3254 unbind_to (count, Qnil);
3257 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3258 Srun_window_configuration_change_hook, 0, 1, 0,
3259 doc: /* Run `window-configuration-change-hook' for FRAME.
3260 If FRAME is omitted or nil, it defaults to the selected frame. */)
3261 (Lisp_Object frame)
3263 run_window_configuration_change_hook (decode_live_frame (frame));
3264 return Qnil;
3267 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3268 Srun_window_scroll_functions, 0, 1, 0,
3269 doc: /* Run `window-scroll-functions' for WINDOW.
3270 If WINDOW is omitted or nil, it defaults to the selected window. */)
3271 (Lisp_Object window)
3273 if (! NILP (Vwindow_scroll_functions))
3274 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3275 Fmarker_position (decode_live_window (window)->start));
3276 return Qnil;
3279 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3280 to run hooks. See make_frame for a case where it's not allowed.
3281 KEEP_MARGINS_P means that the current margins, fringes, and
3282 scroll-bar settings of the window are not reset from the buffer's
3283 local settings. */
3285 void
3286 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3287 bool run_hooks_p, bool keep_margins_p)
3289 struct window *w = XWINDOW (window);
3290 struct buffer *b = XBUFFER (buffer);
3291 ptrdiff_t count = SPECPDL_INDEX ();
3292 bool samebuf = EQ (buffer, w->contents);
3294 wset_buffer (w, buffer);
3296 if (EQ (window, selected_window))
3297 bset_last_selected_window (b, window);
3299 /* Let redisplay errors through. */
3300 b->display_error_modiff = 0;
3302 /* Update time stamps of buffer display. */
3303 if (INTEGERP (BVAR (b, display_count)))
3304 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3305 bset_display_time (b, Fcurrent_time ());
3307 w->window_end_pos = 0;
3308 w->window_end_vpos = 0;
3309 w->last_cursor_vpos = 0;
3311 if (!(keep_margins_p && samebuf))
3312 { /* If we're not actually changing the buffer, don't reset hscroll
3313 and vscroll. This case happens for example when called from
3314 change_frame_size_1, where we use a dummy call to
3315 Fset_window_buffer on the frame's selected window (and no
3316 other) just in order to run window-configuration-change-hook
3317 (no longer true since change_frame_size_1 directly calls
3318 run_window_configuration_change_hook). Resetting hscroll and
3319 vscroll here is problematic for things like image-mode and
3320 doc-view-mode since it resets the image's position whenever we
3321 resize the frame. */
3322 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3323 w->suspend_auto_hscroll = false;
3324 w->vscroll = 0;
3325 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3326 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3327 set_marker_restricted (w->start,
3328 make_number (b->last_window_start),
3329 buffer);
3330 w->start_at_line_beg = false;
3331 w->force_start = false;
3333 /* Maybe we could move this into the `if' but it's not obviously safe and
3334 I doubt it's worth the trouble. */
3335 wset_redisplay (w);
3337 wset_update_mode_line (w);
3339 /* We must select BUFFER to run the window-scroll-functions and to look up
3340 the buffer-local value of Vwindow_point_insertion_type. */
3341 record_unwind_current_buffer ();
3342 Fset_buffer (buffer);
3344 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3345 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3347 if (!keep_margins_p)
3349 /* Set left and right marginal area width etc. from buffer. */
3350 set_window_fringes (w, BVAR (b, left_fringe_width),
3351 BVAR (b, right_fringe_width),
3352 BVAR (b, fringes_outside_margins));
3353 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3354 BVAR (b, vertical_scroll_bar_type),
3355 BVAR (b, scroll_bar_height),
3356 BVAR (b, horizontal_scroll_bar_type));
3357 set_window_margins (w, BVAR (b, left_margin_cols),
3358 BVAR (b, right_margin_cols));
3359 apply_window_adjustment (w);
3362 if (run_hooks_p)
3364 if (! NILP (Vwindow_scroll_functions))
3365 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3366 Fmarker_position (w->start));
3367 if (!samebuf)
3368 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3371 unbind_to (count, Qnil);
3374 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3375 doc: /* Make WINDOW display BUFFER-OR-NAME.
3376 WINDOW must be a live window and defaults to the selected one.
3377 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3379 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3380 display margins, fringe widths, and scroll bar settings are preserved;
3381 the default is to reset these from the local settings for BUFFER-OR-NAME
3382 or the frame defaults. Return nil.
3384 This function throws an error when WINDOW is strongly dedicated to its
3385 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3386 already display BUFFER-OR-NAME.
3388 This function runs `window-scroll-functions' before running
3389 `window-configuration-change-hook'. */)
3390 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3392 register Lisp_Object tem, buffer;
3393 register struct window *w = decode_live_window (window);
3395 XSETWINDOW (window, w);
3396 buffer = Fget_buffer (buffer_or_name);
3397 CHECK_BUFFER (buffer);
3398 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3399 error ("Attempt to display deleted buffer");
3401 tem = w->contents;
3402 if (NILP (tem))
3403 error ("Window is deleted");
3404 else
3406 if (!EQ (tem, buffer))
3408 if (EQ (w->dedicated, Qt))
3409 /* WINDOW is strongly dedicated to its buffer, signal an
3410 error. */
3411 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3412 else
3413 /* WINDOW is weakly dedicated to its buffer, reset
3414 dedication. */
3415 wset_dedicated (w, Qnil);
3417 call1 (Qrecord_window_buffer, window);
3420 unshow_buffer (w);
3423 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3425 return Qnil;
3428 static Lisp_Object
3429 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3431 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3434 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3435 0, 1, 0,
3436 doc: /* Force all windows to be updated on next redisplay.
3437 If optional arg OBJECT is a window, force redisplay of that window only.
3438 If OBJECT is a buffer or buffer name, force redisplay of all windows
3439 displaying that buffer. */)
3440 (Lisp_Object object)
3442 if (NILP (object))
3444 windows_or_buffers_changed = 29;
3445 update_mode_lines = 28;
3446 return Qt;
3449 if (WINDOWP (object))
3451 struct window *w = XWINDOW (object);
3452 mark_window_display_accurate (object, false);
3453 w->update_mode_line = true;
3454 if (BUFFERP (w->contents))
3455 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3456 update_mode_lines = 29;
3457 return Qt;
3460 if (STRINGP (object))
3461 object = Fget_buffer (object);
3462 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3463 && buffer_window_count (XBUFFER (object)))
3465 /* If buffer is live and shown in at least one window, find
3466 all windows showing this buffer and force update of them. */
3467 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3468 return NILP (object) ? Qnil : Qt;
3471 /* If nothing suitable was found, just return.
3472 We could signal an error, but this feature will typically be used
3473 asynchronously in timers or process sentinels, so we don't. */
3474 return Qnil;
3477 /* Obsolete since 24.3. */
3478 void
3479 temp_output_buffer_show (register Lisp_Object buf)
3481 register struct buffer *old = current_buffer;
3482 register Lisp_Object window;
3483 register struct window *w;
3485 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3487 Fset_buffer (buf);
3488 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3489 BEGV = BEG;
3490 ZV = Z;
3491 SET_PT (BEG);
3492 set_buffer_internal (old);
3494 if (!NILP (Vtemp_buffer_show_function))
3495 call1 (Vtemp_buffer_show_function, buf);
3496 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3498 if (!EQ (XWINDOW (window)->frame, selected_frame))
3499 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3500 Vminibuf_scroll_window = window;
3501 w = XWINDOW (window);
3502 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3503 w->suspend_auto_hscroll = false;
3504 set_marker_restricted_both (w->start, buf, BEG, BEG);
3505 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3506 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3508 /* Run temp-buffer-show-hook, with the chosen window selected
3509 and its buffer current. */
3511 ptrdiff_t count = SPECPDL_INDEX ();
3512 Lisp_Object prev_window, prev_buffer;
3513 prev_window = selected_window;
3514 XSETBUFFER (prev_buffer, old);
3516 /* Select the window that was chosen, for running the hook.
3517 Note: Both Fselect_window and select_window_norecord may
3518 set-buffer to the buffer displayed in the window,
3519 so we need to save the current buffer. --stef */
3520 record_unwind_protect (restore_buffer, prev_buffer);
3521 record_unwind_protect (select_window_norecord, prev_window);
3522 Fselect_window (window, Qt);
3523 Fset_buffer (w->contents);
3524 run_hook (Qtemp_buffer_show_hook);
3525 unbind_to (count, Qnil);
3530 /* Allocate basically initialized window. */
3532 static struct window *
3533 allocate_window (void)
3535 return ALLOCATE_ZEROED_PSEUDOVECTOR
3536 (struct window, current_matrix, PVEC_WINDOW);
3539 /* Make new window, have it replace WINDOW in window-tree, and make
3540 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3541 horizontal child). */
3542 static void
3543 make_parent_window (Lisp_Object window, bool horflag)
3545 Lisp_Object parent;
3546 register struct window *o, *p;
3548 o = XWINDOW (window);
3549 p = allocate_window ();
3550 memcpy ((char *) p + sizeof (struct vectorlike_header),
3551 (char *) o + sizeof (struct vectorlike_header),
3552 word_size * VECSIZE (struct window));
3553 /* P's buffer slot may change from nil to a buffer... */
3554 adjust_window_count (p, 1);
3555 XSETWINDOW (parent, p);
3557 p->sequence_number = ++sequence_number;
3559 replace_window (window, parent, true);
3561 wset_next (o, Qnil);
3562 wset_prev (o, Qnil);
3563 wset_parent (o, parent);
3564 /* ...but now P becomes an internal window. */
3565 wset_start (p, Qnil);
3566 wset_pointm (p, Qnil);
3567 wset_old_pointm (p, Qnil);
3568 wset_buffer (p, Qnil);
3569 wset_combination (p, horflag, window);
3570 wset_combination_limit (p, Qnil);
3571 wset_window_parameters (p, Qnil);
3574 /* Make new window from scratch. */
3575 Lisp_Object
3576 make_window (void)
3578 Lisp_Object window;
3579 register struct window *w;
3581 w = allocate_window ();
3582 /* Initialize Lisp data. Note that allocate_window initializes all
3583 Lisp data to nil, so do it only for slots which should not be nil. */
3584 wset_normal_lines (w, make_float (1.0));
3585 wset_normal_cols (w, make_float (1.0));
3586 wset_new_total (w, make_number (0));
3587 wset_new_normal (w, make_number (0));
3588 wset_new_pixel (w, make_number (0));
3589 wset_start (w, Fmake_marker ());
3590 wset_pointm (w, Fmake_marker ());
3591 wset_old_pointm (w, Fmake_marker ());
3592 wset_vertical_scroll_bar_type (w, Qt);
3593 wset_horizontal_scroll_bar_type (w, Qt);
3594 /* These Lisp fields are marked specially so they're not set to nil by
3595 allocate_window. */
3596 wset_prev_buffers (w, Qnil);
3597 wset_next_buffers (w, Qnil);
3599 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3600 non-Lisp data, so do it only for slots which should not be zero. */
3601 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3602 w->left_fringe_width = w->right_fringe_width = -1;
3603 w->mode_line_height = w->header_line_height = -1;
3604 #ifdef HAVE_WINDOW_SYSTEM
3605 w->phys_cursor_type = NO_CURSOR;
3606 w->phys_cursor_width = -1;
3607 #endif
3608 w->sequence_number = ++sequence_number;
3609 w->scroll_bar_width = -1;
3610 w->scroll_bar_height = -1;
3611 w->column_number_displayed = -1;
3612 /* Reset window_list. */
3613 Vwindow_list = Qnil;
3614 /* Return window. */
3615 XSETWINDOW (window, w);
3616 return window;
3619 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3620 doc: /* Set new pixel size of WINDOW to SIZE.
3621 WINDOW must be a valid window and defaults to the selected one.
3622 Return SIZE.
3624 Optional argument ADD non-nil means add SIZE to the new pixel size of
3625 WINDOW and return the sum.
3627 The new pixel size of WINDOW, if valid, will be shortly installed as
3628 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3629 `window-pixel-width').
3631 Note: This function does not operate on any child windows of WINDOW. */)
3632 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3634 struct window *w = decode_valid_window (window);
3635 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3636 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3638 CHECK_RANGED_INTEGER (size, size_min, size_max);
3639 if (NILP (add))
3640 wset_new_pixel (w, size);
3641 else
3642 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3644 return w->new_pixel;
3647 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3648 doc: /* Set new total size of WINDOW to SIZE.
3649 WINDOW must be a valid window and defaults to the selected one.
3650 Return SIZE.
3652 Optional argument ADD non-nil means add SIZE to the new total size of
3653 WINDOW and return the sum.
3655 The new total size of WINDOW, if valid, will be shortly installed as
3656 WINDOW's total height (see `window-total-height') or total width (see
3657 `window-total-width').
3659 Note: This function does not operate on any child windows of WINDOW. */)
3660 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3662 struct window *w = decode_valid_window (window);
3664 CHECK_NUMBER (size);
3665 if (NILP (add))
3666 wset_new_total (w, size);
3667 else
3668 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3670 return w->new_total;
3673 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3674 doc: /* Set new normal size of WINDOW to SIZE.
3675 WINDOW must be a valid window and defaults to the selected one.
3676 Return SIZE.
3678 The new normal size of WINDOW, if valid, will be shortly installed as
3679 WINDOW's normal size (see `window-normal-size').
3681 Note: This function does not operate on any child windows of WINDOW. */)
3682 (Lisp_Object window, Lisp_Object size)
3684 wset_new_normal (decode_valid_window (window), size);
3685 return size;
3688 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3689 to w->new_pixel would result in correct heights (widths)
3690 for window W and recursively all child windows of W.
3692 Note: This function does not check any of `window-fixed-size-p',
3693 `window-min-height' or `window-min-width'. It does check that window
3694 sizes do not drop below one line (two columns). */
3695 static bool
3696 window_resize_check (struct window *w, bool horflag)
3698 struct frame *f = XFRAME (w->frame);
3699 struct window *c;
3701 if (WINDOW_VERTICAL_COMBINATION_P (w))
3702 /* W is a vertical combination. */
3704 c = XWINDOW (w->contents);
3705 if (horflag)
3706 /* All child windows of W must have the same width as W. */
3708 while (c)
3710 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3711 || !window_resize_check (c, horflag))
3712 return false;
3714 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3717 return true;
3719 else
3720 /* The sum of the heights of the child windows of W must equal
3721 W's height. */
3723 int remaining_pixels = XINT (w->new_pixel);
3725 while (c)
3727 if (!window_resize_check (c, horflag))
3728 return false;
3730 remaining_pixels -= XINT (c->new_pixel);
3731 if (remaining_pixels < 0)
3732 return false;
3733 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3736 return remaining_pixels == 0;
3739 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3740 /* W is a horizontal combination. */
3742 c = XWINDOW (w->contents);
3743 if (horflag)
3744 /* The sum of the widths of the child windows of W must equal W's
3745 width. */
3747 int remaining_pixels = XINT (w->new_pixel);
3749 while (c)
3751 if (!window_resize_check (c, horflag))
3752 return false;
3754 remaining_pixels -= XINT (c->new_pixel);
3755 if (remaining_pixels < 0)
3756 return false;
3757 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3760 return remaining_pixels == 0;
3762 else
3763 /* All child windows of W must have the same height 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;
3777 else
3778 /* A leaf window. Make sure it's not too small. The following
3779 hardcodes the values of `window-safe-min-width' (2) and
3780 `window-safe-min-height' (1) which are defined in window.el. */
3781 return (XINT (w->new_pixel) >= (horflag
3782 ? (2 * FRAME_COLUMN_WIDTH (f))
3783 : FRAME_LINE_HEIGHT (f)));
3787 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3788 w->new_pixel for window W and recursively all child windows of W.
3789 Also calculate and assign the new vertical (horizontal) pixel start
3790 positions of each of these windows.
3792 This function does not perform any error checks. Make sure you have
3793 run window_resize_check on W before applying this function. */
3794 static void
3795 window_resize_apply (struct window *w, bool horflag)
3797 struct window *c;
3798 int edge;
3799 int unit = (horflag
3800 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3801 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3803 /* Note: Assigning new_normal requires that the new total size of the
3804 parent window has been set *before*. */
3805 if (horflag)
3807 w->pixel_width = XFASTINT (w->new_pixel);
3808 w->total_cols = w->pixel_width / unit;
3809 if (NUMBERP (w->new_normal))
3810 wset_normal_cols (w, w->new_normal);
3812 edge = w->pixel_left;
3814 else
3816 w->pixel_height = XFASTINT (w->new_pixel);
3817 w->total_lines = w->pixel_height / unit;
3818 if (NUMBERP (w->new_normal))
3819 wset_normal_lines (w, w->new_normal);
3821 edge = w->pixel_top;
3824 if (WINDOW_VERTICAL_COMBINATION_P (w))
3825 /* W is a vertical combination. */
3827 c = XWINDOW (w->contents);
3828 while (c)
3830 if (horflag)
3832 c->pixel_left = edge;
3833 c->left_col = edge / unit;
3835 else
3837 c->pixel_top = edge;
3838 c->top_line = edge / unit;
3840 window_resize_apply (c, horflag);
3841 if (!horflag)
3842 edge = edge + c->pixel_height;
3844 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3847 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3848 /* W is a horizontal combination. */
3850 c = XWINDOW (w->contents);
3851 while (c)
3853 if (horflag)
3855 c->pixel_left = edge;
3856 c->left_col = edge / unit;
3858 else
3860 c->pixel_top = edge;
3861 c->top_line = edge / unit;
3864 window_resize_apply (c, horflag);
3865 if (horflag)
3866 edge = edge + c->pixel_width;
3868 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3871 else
3872 /* Bug#15957. */
3873 w->window_end_valid = false;
3877 /* Set w->total_lines (w->total_cols if HORFLAG) to
3878 w->new_total for window W and recursively all child windows of W.
3879 Also calculate and assign the new vertical (horizontal) start
3880 positions of each of these windows. */
3881 static void
3882 window_resize_apply_total (struct window *w, bool horflag)
3884 struct window *c;
3885 int edge;
3887 /* Note: Assigning new_normal requires that the new total size of the
3888 parent window has been set *before*. */
3889 if (horflag)
3891 w->total_cols = XFASTINT (w->new_total);
3892 edge = w->left_col;
3894 else
3896 w->total_lines = XFASTINT (w->new_total);
3897 edge = w->top_line;
3900 if (WINDOW_VERTICAL_COMBINATION_P (w))
3901 /* W is a vertical combination. */
3903 c = XWINDOW (w->contents);
3904 while (c)
3906 if (horflag)
3907 c->left_col = edge;
3908 else
3909 c->top_line = edge;
3911 window_resize_apply_total (c, horflag);
3912 if (!horflag)
3913 edge = edge + c->total_lines;
3915 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3918 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3919 /* W is a horizontal combination. */
3921 c = XWINDOW (w->contents);
3922 while (c)
3924 if (horflag)
3925 c->left_col = edge;
3926 else
3927 c->top_line = edge;
3929 window_resize_apply_total (c, horflag);
3930 if (horflag)
3931 edge = edge + c->total_cols;
3933 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3938 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3939 doc: /* Apply requested size values for window-tree of FRAME.
3940 If FRAME is omitted or nil, it defaults to the selected frame.
3942 Optional argument HORIZONTAL omitted or nil means apply requested
3943 height values. HORIZONTAL non-nil means apply requested width values.
3945 The requested size values are those set by `set-window-new-pixel' and
3946 `set-window-new-normal'. This function checks whether the requested
3947 values sum up to a valid window layout, recursively assigns the new
3948 sizes of all child windows and calculates and assigns the new start
3949 positions of these windows.
3951 Return t if the requested values have been applied correctly, nil
3952 otherwise.
3954 Note: This function does not check any of `window-fixed-size-p',
3955 `window-min-height' or `window-min-width'. All these checks have to
3956 be applied on the Elisp level. */)
3957 (Lisp_Object frame, Lisp_Object horizontal)
3959 struct frame *f = decode_live_frame (frame);
3960 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3961 bool horflag = !NILP (horizontal);
3963 if (!window_resize_check (r, horflag)
3964 || (XINT (r->new_pixel)
3965 != (horflag ? r->pixel_width : r->pixel_height)))
3966 return Qnil;
3968 block_input ();
3969 window_resize_apply (r, horflag);
3971 fset_redisplay (f);
3972 FRAME_WINDOW_SIZES_CHANGED (f) = true;
3974 adjust_frame_glyphs (f);
3975 unblock_input ();
3977 return Qt;
3981 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
3982 doc: /* Apply requested total size values for window-tree of FRAME.
3983 If FRAME is omitted or nil, it defaults to the selected frame.
3985 This function does not assign pixel or normal size values. You should
3986 have run `window-resize-apply' before running this.
3988 Optional argument HORIZONTAL omitted or nil means apply requested
3989 height values. HORIZONTAL non-nil means apply requested width
3990 values. */)
3991 (Lisp_Object frame, Lisp_Object horizontal)
3993 struct frame *f = decode_live_frame (frame);
3994 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3996 block_input ();
3997 /* Necessary when deleting the top-/or leftmost window. */
3998 r->left_col = 0;
3999 r->top_line = FRAME_TOP_MARGIN (f);
4000 window_resize_apply_total (r, !NILP (horizontal));
4001 /* Handle the mini window. */
4002 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4004 struct window *m = XWINDOW (f->minibuffer_window);
4006 if (NILP (horizontal))
4008 m->top_line = r->top_line + r->total_lines;
4009 m->total_lines = XFASTINT (m->new_total);
4011 else
4012 m->total_cols = XFASTINT (m->new_total);
4015 unblock_input ();
4017 return Qt;
4021 /* Resize frame F's windows when number of lines of F is set to SIZE.
4022 HORFLAG means resize windows when number of columns of F is set to
4023 SIZE. PIXELWISE means to interpret SIZE as pixels. */
4024 void
4025 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4027 Lisp_Object root = f->root_window;
4028 struct window *r = XWINDOW (root);
4029 Lisp_Object mini = f->minibuffer_window;
4030 struct window *m;
4031 /* old_size is the old size of the frame's root window. */
4032 int old_size = horflag ? r->total_cols : r->total_lines;
4033 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4034 /* new_size is the new size of the frame's root window. */
4035 int new_size, new_pixel_size;
4036 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4038 /* Don't let the size drop below one unit. This is more comforting
4039 when we are called from x_set_tool_bar_lines since the latter may
4040 have implicitly given us a zero or negative height. */
4041 if (pixelwise)
4043 /* Note: This does not include the size for internal borders
4044 since these are not part of the frame's text area. */
4045 new_pixel_size = max (horflag
4046 ? size
4047 : (size
4048 - ((FRAME_HAS_MINIBUF_P (f)
4049 && !FRAME_MINIBUF_ONLY_P (f))
4050 ? FRAME_LINE_HEIGHT (f) : 0)),
4051 unit);
4052 new_size = new_pixel_size / unit;
4054 else
4056 new_size = max (size - (!horflag
4057 && FRAME_HAS_MINIBUF_P (f)
4058 && !FRAME_MINIBUF_ONLY_P (f)),
4060 new_pixel_size = new_size * unit;
4063 if (new_pixel_size == old_pixel_size
4064 && (horflag || r->pixel_top == FRAME_TOP_MARGIN_HEIGHT (f)))
4066 else if (WINDOW_LEAF_P (r))
4067 /* For a leaf root window just set the size. */
4068 if (horflag)
4070 r->total_cols = new_size;
4071 r->pixel_width = new_pixel_size;
4073 else
4075 r->top_line = FRAME_TOP_MARGIN (f);
4076 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4078 r->total_lines = new_size;
4079 r->pixel_height = new_pixel_size;
4081 else
4083 Lisp_Object delta;
4085 if (!horflag)
4087 r->top_line = FRAME_TOP_MARGIN (f);
4088 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4091 if (pixelwise)
4092 XSETINT (delta, new_pixel_size - old_pixel_size);
4093 else
4094 XSETINT (delta, new_size - old_size);
4096 /* Try a "normal" resize first. */
4097 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4098 pixelwise ? Qt : Qnil);
4099 if (window_resize_check (r, horflag)
4100 && new_pixel_size == XINT (r->new_pixel))
4102 window_resize_apply (r, horflag);
4103 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4105 else
4107 /* Try with "reasonable" minimum sizes next. */
4108 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4109 pixelwise ? Qt : Qnil);
4110 if (window_resize_check (r, horflag)
4111 && new_pixel_size == XINT (r->new_pixel))
4113 window_resize_apply (r, horflag);
4114 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4115 #if false /* Let's try without safe sizes and/or killing other windows. */
4117 else
4119 /* Finally, try with "safe" minimum sizes. */
4120 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4121 pixelwise ? Qt : Qnil);
4122 if (window_resize_check (r, horflag)
4123 && new_pixel_size == XINT (r->new_pixel))
4125 window_resize_apply (r, horflag);
4126 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4128 else
4130 /* We lost. Delete all windows but the frame's
4131 selected one. */
4132 root = f->selected_window;
4133 Fdelete_other_windows_internal (root, Qnil);
4134 if (horflag)
4136 XWINDOW (root)->total_cols = new_size;
4137 XWINDOW (root)->pixel_width = new_pixel_size;
4139 else
4141 XWINDOW (root)->total_lines = new_size;
4142 XWINDOW (root)->pixel_height = new_pixel_size;
4145 #endif /* false */
4150 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4152 m = XWINDOW (mini);
4153 if (horflag)
4155 m->total_cols = size;
4156 m->pixel_width = new_pixel_size;
4158 else
4160 /* Are we sure we always want 1 line here? */
4161 m->total_lines = 1;
4162 m->pixel_height = FRAME_LINE_HEIGHT (f);
4163 m->top_line = r->top_line + r->total_lines;
4164 m->pixel_top = r->pixel_top + r->pixel_height;
4168 fset_redisplay (f);
4172 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4173 doc: /* Split window OLD.
4174 Second argument PIXEL-SIZE specifies the number of pixels of the
4175 new window. It must be a positive integer.
4177 Third argument SIDE nil (or `below') specifies that the new window shall
4178 be located below WINDOW. SIDE `above' means the new window shall be
4179 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4180 height of the new window including space reserved for the mode and/or
4181 header line.
4183 SIDE t (or `right') specifies that the new window shall be located on
4184 the right side of WINDOW. SIDE `left' means the new window shall be
4185 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4186 width of the new window including space reserved for fringes and the
4187 scrollbar or a divider column.
4189 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4190 according to the SIDE argument.
4192 The new pixel and normal sizes of all involved windows must have been
4193 set correctly. See the code of `split-window' for how this is done. */)
4194 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4196 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4197 parent window or an internal window we have to install as OLD's new
4198 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4199 provided OLD is a leaf window, or to the frame's selected window.
4200 NEW (*n) is the new window created with some parameters taken from
4201 REFERENCE (*r). */
4202 Lisp_Object new, frame, reference;
4203 struct window *o, *p, *n, *r, *c;
4204 struct frame *f;
4205 bool horflag
4206 /* HORFLAG is true when we split side-by-side, false otherwise. */
4207 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4209 CHECK_WINDOW (old);
4210 o = XWINDOW (old);
4211 frame = WINDOW_FRAME (o);
4212 f = XFRAME (frame);
4214 CHECK_NUMBER (pixel_size);
4215 EMACS_INT total_size
4216 = XINT (pixel_size) / (horflag
4217 ? FRAME_COLUMN_WIDTH (f)
4218 : FRAME_LINE_HEIGHT (f));
4220 /* Set combination_limit if we have to make a new parent window.
4221 We do that if either `window-combination-limit' is t, or OLD has no
4222 parent, or OLD is ortho-combined. */
4223 bool combination_limit
4224 = (EQ (Vwindow_combination_limit, Qt)
4225 || NILP (o->parent)
4226 || (horflag
4227 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4228 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4230 /* We need a live reference window to initialize some parameters. */
4231 if (WINDOW_LIVE_P (old))
4232 /* OLD is live, use it as reference window. */
4233 reference = old;
4234 else
4235 /* Use the frame's selected window as reference window. */
4236 reference = FRAME_SELECTED_WINDOW (f);
4237 r = XWINDOW (reference);
4239 /* The following bugs are caught by `split-window'. */
4240 if (MINI_WINDOW_P (o))
4241 error ("Attempt to split minibuffer window");
4242 else if (total_size < (horflag ? 2 : 1))
4243 error ("Size of new window too small (after split)");
4244 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4245 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4246 proportionally. */
4248 p = XWINDOW (o->parent);
4249 /* Temporarily pretend we split the parent window. */
4250 wset_new_pixel
4251 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4252 - XINT (pixel_size)));
4253 if (!window_resize_check (p, horflag))
4254 error ("Window sizes don't fit");
4255 else
4256 /* Undo the temporary pretension. */
4257 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4259 else
4261 if (!window_resize_check (o, horflag))
4262 error ("Resizing old window failed");
4263 else if (XINT (pixel_size) + XINT (o->new_pixel)
4264 != (horflag ? o->pixel_width : o->pixel_height))
4265 error ("Sum of sizes of old and new window don't fit");
4268 /* This is our point of no return. */
4269 if (combination_limit)
4271 /* Save the old value of o->normal_cols/lines. It gets corrupted
4272 by make_parent_window and we need it below for assigning it to
4273 p->new_normal. */
4274 Lisp_Object new_normal
4275 = horflag ? o->normal_cols : o->normal_lines;
4277 make_parent_window (old, horflag);
4278 p = XWINDOW (o->parent);
4279 if (EQ (Vwindow_combination_limit, Qt))
4280 /* Store t in the new parent's combination_limit slot to avoid
4281 that its children get merged into another window. */
4282 wset_combination_limit (p, Qt);
4283 /* These get applied below. */
4284 wset_new_pixel
4285 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4286 wset_new_total
4287 (p, make_number (horflag ? o->total_cols : o->total_lines));
4288 wset_new_normal (p, new_normal);
4290 else
4291 p = XWINDOW (o->parent);
4293 fset_redisplay (f);
4294 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4295 new = make_window ();
4296 n = XWINDOW (new);
4297 wset_frame (n, frame);
4298 wset_parent (n, o->parent);
4300 if (EQ (side, Qabove) || EQ (side, Qleft))
4302 wset_prev (n, o->prev);
4303 if (NILP (n->prev))
4304 wset_combination (p, horflag, new);
4305 else
4306 wset_next (XWINDOW (n->prev), new);
4307 wset_next (n, old);
4308 wset_prev (o, new);
4310 else
4312 wset_next (n, o->next);
4313 if (!NILP (n->next))
4314 wset_prev (XWINDOW (n->next), new);
4315 wset_prev (n, old);
4316 wset_next (o, new);
4319 n->window_end_valid = false;
4320 n->last_cursor_vpos = 0;
4322 /* Get special geometry settings from reference window. */
4323 n->left_margin_cols = r->left_margin_cols;
4324 n->right_margin_cols = r->right_margin_cols;
4325 n->left_fringe_width = r->left_fringe_width;
4326 n->right_fringe_width = r->right_fringe_width;
4327 n->fringes_outside_margins = r->fringes_outside_margins;
4328 n->scroll_bar_width = r->scroll_bar_width;
4329 n->scroll_bar_height = r->scroll_bar_height;
4330 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4331 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4333 /* Directly assign orthogonal coordinates and sizes. */
4334 if (horflag)
4336 n->pixel_top = o->pixel_top;
4337 n->top_line = o->top_line;
4338 n->pixel_height = o->pixel_height;
4339 n->total_lines = o->total_lines;
4341 else
4343 n->pixel_left = o->pixel_left;
4344 n->left_col = o->left_col;
4345 n->pixel_width = o->pixel_width;
4346 n->total_cols = o->total_cols;
4349 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4350 get them ready here. */
4351 wset_new_pixel (n, pixel_size);
4352 EMACS_INT sum = 0;
4353 c = XWINDOW (p->contents);
4354 while (c)
4356 if (c != n)
4357 sum = sum + XINT (c->new_total);
4358 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4360 wset_new_total (n, make_number ((horflag
4361 ? p->total_cols
4362 : p->total_lines)
4363 - sum));
4364 wset_new_normal (n, normal_size);
4366 block_input ();
4367 window_resize_apply (p, horflag);
4368 adjust_frame_glyphs (f);
4369 /* Set buffer of NEW to buffer of reference window. Don't run
4370 any hooks. */
4371 set_window_buffer (new, r->contents, false, true);
4372 unblock_input ();
4374 /* Maybe we should run the scroll functions in Elisp (which already
4375 runs the configuration change hook). */
4376 if (! NILP (Vwindow_scroll_functions))
4377 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4378 Fmarker_position (n->start));
4379 /* Return NEW. */
4380 return new;
4384 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4385 doc: /* Remove WINDOW from its frame.
4386 WINDOW defaults to the selected window. Return nil.
4387 Signal an error when WINDOW is the only window on its frame. */)
4388 (Lisp_Object window)
4390 Lisp_Object parent, sibling, frame, root;
4391 struct window *w, *p, *s, *r;
4392 struct frame *f;
4393 bool horflag, before_sibling = false;
4395 w = decode_any_window (window);
4396 XSETWINDOW (window, w);
4397 if (NILP (w->contents))
4398 /* It's a no-op to delete an already deleted window. */
4399 return Qnil;
4401 parent = w->parent;
4402 if (NILP (parent))
4403 /* Never delete a minibuffer or frame root window. */
4404 error ("Attempt to delete minibuffer or sole ordinary window");
4405 else if (NILP (w->prev) && NILP (w->next))
4406 /* Rather bow out here, this case should be handled on the Elisp
4407 level. */
4408 error ("Attempt to delete sole window of parent");
4410 p = XWINDOW (parent);
4411 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4413 frame = WINDOW_FRAME (w);
4414 f = XFRAME (frame);
4416 root = FRAME_ROOT_WINDOW (f);
4417 r = XWINDOW (root);
4419 /* Unlink WINDOW from window tree. */
4420 if (NILP (w->prev))
4421 /* Get SIBLING below (on the right of) WINDOW. */
4423 /* before_sibling means WINDOW is the first child of its
4424 parent and thus before the sibling. */
4425 before_sibling = true;
4426 sibling = w->next;
4427 s = XWINDOW (sibling);
4428 wset_prev (s, Qnil);
4429 wset_combination (p, horflag, sibling);
4431 else
4432 /* Get SIBLING above (on the left of) WINDOW. */
4434 sibling = w->prev;
4435 s = XWINDOW (sibling);
4436 wset_next (s, w->next);
4437 if (!NILP (s->next))
4438 wset_prev (XWINDOW (s->next), sibling);
4441 if (window_resize_check (r, horflag)
4442 && (XINT (r->new_pixel)
4443 == (horflag ? r->pixel_width : r->pixel_height)))
4444 /* We can delete WINDOW now. */
4447 /* Block input. */
4448 block_input ();
4449 window_resize_apply (p, horflag);
4450 /* If this window is referred to by the dpyinfo's mouse
4451 highlight, invalidate that slot to be safe (Bug#9904). */
4452 if (!FRAME_INITIAL_P (f))
4454 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4456 if (EQ (hlinfo->mouse_face_window, window))
4457 hlinfo->mouse_face_window = Qnil;
4460 fset_redisplay (f);
4461 Vwindow_list = Qnil;
4462 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4464 wset_next (w, Qnil); /* Don't delete w->next too. */
4465 free_window_matrices (w);
4467 if (WINDOWP (w->contents))
4469 delete_all_child_windows (w->contents);
4470 wset_combination (w, false, Qnil);
4472 else
4474 unshow_buffer (w);
4475 unchain_marker (XMARKER (w->pointm));
4476 unchain_marker (XMARKER (w->old_pointm));
4477 unchain_marker (XMARKER (w->start));
4478 wset_buffer (w, Qnil);
4481 if (NILP (s->prev) && NILP (s->next))
4482 /* A matrjoshka where SIBLING has become the only child of
4483 PARENT. */
4485 /* Put SIBLING into PARENT's place. */
4486 replace_window (parent, sibling, false);
4487 /* Have SIBLING inherit the following three slot values from
4488 PARENT (the combination_limit slot is not inherited). */
4489 wset_normal_cols (s, p->normal_cols);
4490 wset_normal_lines (s, p->normal_lines);
4491 /* Mark PARENT as deleted. */
4492 wset_combination (p, false, Qnil);
4493 /* Try to merge SIBLING into its new parent. */
4494 recombine_windows (sibling);
4497 adjust_frame_glyphs (f);
4499 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4500 /* We deleted the frame's selected window. */
4502 /* Use the frame's first window as fallback ... */
4503 Lisp_Object new_selected_window = Fframe_first_window (frame);
4504 /* ... but preferably use its most recently used window. */
4505 Lisp_Object mru_window;
4507 /* `get-mru-window' might fail for some reason so play it safe
4508 - promote the first window _without recording it_ first. */
4509 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4510 Fselect_window (new_selected_window, Qt);
4511 else
4512 fset_selected_window (f, new_selected_window);
4514 unblock_input ();
4516 /* Now look whether `get-mru-window' gets us something. */
4517 mru_window = call1 (Qget_mru_window, frame);
4518 if (WINDOW_LIVE_P (mru_window)
4519 && EQ (XWINDOW (mru_window)->frame, frame))
4520 new_selected_window = mru_window;
4522 /* If all ended up well, we now promote the mru window. */
4523 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4524 Fselect_window (new_selected_window, Qnil);
4525 else
4526 fset_selected_window (f, new_selected_window);
4528 else
4529 unblock_input ();
4531 /* Must be run by the caller:
4532 run_window_configuration_change_hook (f); */
4534 else
4535 /* We failed: Relink WINDOW into window tree. */
4537 if (before_sibling)
4539 wset_prev (s, window);
4540 wset_combination (p, horflag, window);
4542 else
4544 wset_next (s, window);
4545 if (!NILP (w->next))
4546 wset_prev (XWINDOW (w->next), window);
4548 error ("Deletion failed");
4551 return Qnil;
4554 /***********************************************************************
4555 Resizing Mini-Windows
4556 ***********************************************************************/
4558 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4559 can. */
4560 void
4561 grow_mini_window (struct window *w, int delta, bool pixelwise)
4563 struct frame *f = XFRAME (w->frame);
4564 struct window *r;
4565 Lisp_Object root, height;
4566 int line_height, pixel_height;
4568 eassert (MINI_WINDOW_P (w));
4569 eassert (delta >= 0);
4571 if (delta > 0)
4573 root = FRAME_ROOT_WINDOW (f);
4574 r = XWINDOW (root);
4575 height = call3 (Qwindow_resize_root_window_vertically,
4576 root, make_number (- delta), pixelwise ? Qt : Qnil);
4577 if (INTEGERP (height) && window_resize_check (r, false))
4579 block_input ();
4580 window_resize_apply (r, false);
4582 if (pixelwise)
4584 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4585 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4587 else
4589 line_height = min (-XINT (height),
4590 ((INT_MAX - w->pixel_height)
4591 / FRAME_LINE_HEIGHT (f)));
4592 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4595 /* Grow the mini-window. */
4596 w->pixel_top = r->pixel_top + r->pixel_height;
4597 w->top_line = r->top_line + r->total_lines;
4598 /* Make sure the mini-window has always at least one line. */
4599 w->pixel_height = max (w->pixel_height + pixel_height,
4600 FRAME_LINE_HEIGHT (f));
4601 w->total_lines = max (w->total_lines + line_height, 1);
4603 /* Enforce full redisplay of the frame. */
4604 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4605 fset_redisplay (f);
4606 adjust_frame_glyphs (f);
4607 unblock_input ();
4612 /* Shrink mini-window W to one line. */
4613 void
4614 shrink_mini_window (struct window *w, bool pixelwise)
4616 struct frame *f = XFRAME (w->frame);
4617 struct window *r;
4618 Lisp_Object root, delta;
4619 EMACS_INT height, unit;
4621 eassert (MINI_WINDOW_P (w));
4623 height = pixelwise ? w->pixel_height : w->total_lines;
4624 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4625 if (height > unit)
4627 root = FRAME_ROOT_WINDOW (f);
4628 r = XWINDOW (root);
4629 delta = call3 (Qwindow_resize_root_window_vertically,
4630 root, make_number (height - unit),
4631 pixelwise ? Qt : Qnil);
4632 if (INTEGERP (delta) && window_resize_check (r, false))
4634 block_input ();
4635 window_resize_apply (r, false);
4637 /* Shrink the mini-window. */
4638 w->top_line = r->top_line + r->total_lines;
4639 w->total_lines = 1;
4640 w->pixel_top = r->pixel_top + r->pixel_height;
4641 w->pixel_height = FRAME_LINE_HEIGHT (f);
4642 /* Enforce full redisplay of the frame. */
4643 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4644 fset_redisplay (f);
4645 adjust_frame_glyphs (f);
4646 unblock_input ();
4648 /* If the above failed for whatever strange reason we must make a
4649 one window frame here. The same routine will be needed when
4650 shrinking the frame (and probably when making the initial
4651 *scratch* window). For the moment leave things as they are. */
4655 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4656 doc: /* Resize minibuffer window WINDOW. */)
4657 (Lisp_Object window)
4659 struct window *w = XWINDOW (window);
4660 struct window *r;
4661 struct frame *f;
4662 int height;
4664 CHECK_WINDOW (window);
4665 f = XFRAME (w->frame);
4667 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4668 error ("Not a valid minibuffer window");
4669 else if (FRAME_MINIBUF_ONLY_P (f))
4670 error ("Cannot resize a minibuffer-only frame");
4672 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4673 height = r->pixel_height + w->pixel_height;
4674 if (window_resize_check (r, false)
4675 && XINT (w->new_pixel) > 0
4676 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4678 block_input ();
4679 window_resize_apply (r, false);
4681 w->pixel_height = XFASTINT (w->new_pixel);
4682 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4683 w->pixel_top = r->pixel_top + r->pixel_height;
4684 w->top_line = r->top_line + r->total_lines;
4686 fset_redisplay (f);
4687 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4688 adjust_frame_glyphs (f);
4689 unblock_input ();
4690 return Qt;
4692 else
4693 error ("Failed to resize minibuffer window");
4696 /* Mark window cursors off for all windows in the window tree rooted
4697 at W by setting their phys_cursor_on_p flag to zero. Called from
4698 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4699 the frame are cleared. */
4701 void
4702 mark_window_cursors_off (struct window *w)
4704 while (w)
4706 if (WINDOWP (w->contents))
4707 mark_window_cursors_off (XWINDOW (w->contents));
4708 else
4709 w->phys_cursor_on_p = false;
4711 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4716 /* Return number of lines of text (not counting mode lines) in W. */
4719 window_internal_height (struct window *w)
4721 int ht = w->total_lines;
4723 if (!MINI_WINDOW_P (w))
4725 if (!NILP (w->parent)
4726 || WINDOWP (w->contents)
4727 || !NILP (w->next)
4728 || !NILP (w->prev)
4729 || WINDOW_WANTS_MODELINE_P (w))
4730 --ht;
4732 if (WINDOW_WANTS_HEADER_LINE_P (w))
4733 --ht;
4736 return ht;
4740 /************************************************************************
4741 Window Scrolling
4742 ***********************************************************************/
4744 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4745 N screen-fulls, which is defined as the height of the window minus
4746 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4747 instead. Negative values of N mean scroll down. NOERROR
4748 means don't signal an error if we try to move over BEGV or ZV,
4749 respectively. */
4751 static void
4752 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4754 ptrdiff_t count = SPECPDL_INDEX ();
4756 immediate_quit = true;
4757 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4759 wset_redisplay (XWINDOW (window));
4761 if (whole && Vfast_but_imprecise_scrolling)
4762 specbind (Qfontification_functions, Qnil);
4764 /* If we must, use the pixel-based version which is much slower than
4765 the line-based one but can handle varying line heights. */
4766 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4767 window_scroll_pixel_based (window, n, whole, noerror);
4768 else
4769 window_scroll_line_based (window, n, whole, noerror);
4771 unbind_to (count, Qnil);
4773 /* Bug#15957. */
4774 XWINDOW (window)->window_end_valid = false;
4775 immediate_quit = false;
4779 /* Implementation of window_scroll that works based on pixel line
4780 heights. See the comment of window_scroll for parameter
4781 descriptions. */
4783 static void
4784 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4786 struct it it;
4787 struct window *w = XWINDOW (window);
4788 struct text_pos start;
4789 int this_scroll_margin;
4790 /* True if we fiddled the window vscroll field without really scrolling. */
4791 bool vscrolled = false;
4792 int x, y, rtop, rbot, rowh, vpos;
4793 void *itdata = NULL;
4794 int window_total_lines;
4795 int frame_line_height = default_line_pixel_height (w);
4796 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4797 Fwindow_old_point (window)));
4799 SET_TEXT_POS_FROM_MARKER (start, w->start);
4800 /* Scrolling a minibuffer window via scroll bar when the echo area
4801 shows long text sometimes resets the minibuffer contents behind
4802 our backs. */
4803 if (CHARPOS (start) > ZV)
4804 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4806 /* If PT is not visible in WINDOW, move back one half of
4807 the screen. Allow PT to be partially visible, otherwise
4808 something like (scroll-down 1) with PT in the line before
4809 the partially visible one would recenter. */
4811 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4813 itdata = bidi_shelve_cache ();
4814 /* Move backward half the height of the window. Performance note:
4815 vmotion used here is about 10% faster, but would give wrong
4816 results for variable height lines. */
4817 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4818 it.current_y = it.last_visible_y;
4819 move_it_vertically_backward (&it, window_box_height (w) / 2);
4821 /* The function move_iterator_vertically may move over more than
4822 the specified y-distance. If it->w is small, e.g. a
4823 mini-buffer window, we may end up in front of the window's
4824 display area. Start displaying at the start of the line
4825 containing PT in this case. */
4826 if (it.current_y <= 0)
4828 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4829 move_it_vertically_backward (&it, 0);
4830 it.current_y = 0;
4833 start = it.current.pos;
4834 bidi_unshelve_cache (itdata, false);
4836 else if (auto_window_vscroll_p)
4838 if (rtop || rbot) /* Partially visible. */
4840 int px;
4841 int dy = frame_line_height;
4842 /* In the below we divide the window box height by the
4843 frame's line height to make the result predictable when
4844 the window box is not an integral multiple of the line
4845 height. This is important to ensure we get back to the
4846 same position when scrolling up, then down. */
4847 if (whole)
4848 dy = max ((window_box_height (w) / dy
4849 - next_screen_context_lines) * dy,
4850 dy);
4851 dy *= n;
4853 if (n < 0)
4855 /* Only vscroll backwards if already vscrolled forwards. */
4856 if (w->vscroll < 0 && rtop > 0)
4858 px = max (0, -w->vscroll - min (rtop, -dy));
4859 Fset_window_vscroll (window, make_number (px), Qt);
4860 return;
4863 if (n > 0)
4865 /* Do vscroll if already vscrolled or only display line. */
4866 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4868 px = max (0, -w->vscroll + min (rbot, dy));
4869 Fset_window_vscroll (window, make_number (px), Qt);
4870 return;
4873 /* Maybe modify window start instead of scrolling. */
4874 if (rbot > 0 || w->vscroll < 0)
4876 ptrdiff_t spos;
4878 Fset_window_vscroll (window, make_number (0), Qt);
4879 /* If there are other text lines above the current row,
4880 move window start to current row. Else to next row. */
4881 if (rbot > 0)
4882 spos = XINT (Fline_beginning_position (Qnil));
4883 else
4884 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4885 set_marker_restricted (w->start, make_number (spos),
4886 w->contents);
4887 w->start_at_line_beg = true;
4888 wset_update_mode_line (w);
4889 /* Set force_start so that redisplay_window will run the
4890 window-scroll-functions. */
4891 w->force_start = true;
4892 return;
4896 /* Cancel previous vscroll. */
4897 Fset_window_vscroll (window, make_number (0), Qt);
4900 itdata = bidi_shelve_cache ();
4901 /* If scroll_preserve_screen_position is non-nil, we try to set
4902 point in the same window line as it is now, so get that line. */
4903 if (!NILP (Vscroll_preserve_screen_position))
4905 /* We preserve the goal pixel coordinate across consecutive
4906 calls to scroll-up, scroll-down and other commands that
4907 have the `scroll-command' property. This avoids the
4908 possibility of point becoming "stuck" on a tall line when
4909 scrolling by one line. */
4910 if (window_scroll_pixel_based_preserve_y < 0
4911 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4912 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4914 start_display (&it, w, start);
4915 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4916 window_scroll_pixel_based_preserve_y = it.current_y;
4917 window_scroll_pixel_based_preserve_x = it.current_x;
4920 else
4921 window_scroll_pixel_based_preserve_y
4922 = window_scroll_pixel_based_preserve_x = -1;
4924 /* Move iterator it from start the specified distance forward or
4925 backward. The result is the new window start. */
4926 start_display (&it, w, start);
4927 if (whole)
4929 ptrdiff_t start_pos = IT_CHARPOS (it);
4930 int dy = frame_line_height;
4931 /* In the below we divide the window box height by the frame's
4932 line height to make the result predictable when the window
4933 box is not an integral multiple of the line height. This is
4934 important to ensure we get back to the same position when
4935 scrolling up, then down. */
4936 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
4937 dy) * n;
4939 /* Note that move_it_vertically always moves the iterator to the
4940 start of a line. So, if the last line doesn't have a newline,
4941 we would end up at the start of the line ending at ZV. */
4942 if (dy <= 0)
4944 move_it_vertically_backward (&it, -dy);
4945 /* Ensure we actually do move, e.g. in case we are currently
4946 looking at an image that is taller that the window height. */
4947 while (start_pos == IT_CHARPOS (it)
4948 && start_pos > BEGV)
4949 move_it_by_lines (&it, -1);
4951 else if (dy > 0)
4953 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4954 MOVE_TO_POS | MOVE_TO_Y);
4955 /* Ensure we actually do move, e.g. in case we are currently
4956 looking at an image that is taller that the window height. */
4957 while (start_pos == IT_CHARPOS (it)
4958 && start_pos < ZV)
4959 move_it_by_lines (&it, 1);
4962 else
4963 move_it_by_lines (&it, n);
4965 /* We failed if we find ZV is already on the screen (scrolling up,
4966 means there's nothing past the end), or if we can't start any
4967 earlier (scrolling down, means there's nothing past the top). */
4968 if ((n > 0 && IT_CHARPOS (it) == ZV)
4969 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4971 if (IT_CHARPOS (it) == ZV)
4973 if (it.current_y < it.last_visible_y
4974 && (it.current_y + it.max_ascent + it.max_descent
4975 > it.last_visible_y))
4977 /* The last line was only partially visible, make it fully
4978 visible. */
4979 w->vscroll = (it.last_visible_y
4980 - it.current_y + it.max_ascent + it.max_descent);
4981 adjust_frame_glyphs (it.f);
4983 else
4985 bidi_unshelve_cache (itdata, false);
4986 if (noerror)
4987 return;
4988 else if (n < 0) /* could happen with empty buffers */
4989 xsignal0 (Qbeginning_of_buffer);
4990 else
4991 xsignal0 (Qend_of_buffer);
4994 else
4996 if (w->vscroll != 0)
4997 /* The first line was only partially visible, make it fully
4998 visible. */
4999 w->vscroll = 0;
5000 else
5002 bidi_unshelve_cache (itdata, false);
5003 if (noerror)
5004 return;
5005 else
5006 xsignal0 (Qbeginning_of_buffer);
5010 /* If control gets here, then we vscrolled. */
5012 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5014 /* Don't try to change the window start below. */
5015 vscrolled = true;
5018 if (! vscrolled)
5020 ptrdiff_t pos = IT_CHARPOS (it);
5021 ptrdiff_t bytepos;
5023 /* If in the middle of a multi-glyph character move forward to
5024 the next character. */
5025 if (in_display_vector_p (&it))
5027 ++pos;
5028 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5031 /* Set the window start, and set up the window for redisplay. */
5032 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5033 IT_BYTEPOS (it));
5034 bytepos = marker_byte_position (w->start);
5035 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5036 wset_update_mode_line (w);
5037 /* Set force_start so that redisplay_window will run the
5038 window-scroll-functions. */
5039 w->force_start = true;
5042 /* The rest of this function uses current_y in a nonstandard way,
5043 not including the height of the header line if any. */
5044 it.current_y = it.vpos = 0;
5046 /* Move PT out of scroll margins.
5047 This code wants current_y to be zero at the window start position
5048 even if there is a header line. */
5049 window_total_lines
5050 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5051 this_scroll_margin = max (0, scroll_margin);
5052 this_scroll_margin
5053 = min (this_scroll_margin, window_total_lines / 4);
5054 this_scroll_margin *= frame_line_height;
5056 if (n > 0)
5058 /* We moved the window start towards ZV, so PT may be now
5059 in the scroll margin at the top. */
5060 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5061 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5062 && (NILP (Vscroll_preserve_screen_position)
5063 || EQ (Vscroll_preserve_screen_position, Qt)))
5064 /* We found PT at a legitimate height. Leave it alone. */
5066 else if (window_scroll_pixel_based_preserve_y >= 0)
5068 /* If we have a header line, take account of it.
5069 This is necessary because we set it.current_y to 0, above. */
5070 move_it_to (&it, -1,
5071 window_scroll_pixel_based_preserve_x,
5072 (window_scroll_pixel_based_preserve_y
5073 - WINDOW_WANTS_HEADER_LINE_P (w)),
5074 -1, MOVE_TO_Y | MOVE_TO_X);
5075 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5077 else
5079 while (it.current_y < this_scroll_margin)
5081 int prev = it.current_y;
5082 move_it_by_lines (&it, 1);
5083 if (prev == it.current_y)
5084 break;
5086 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5089 else if (n < 0)
5091 ptrdiff_t charpos, bytepos;
5092 bool partial_p;
5094 /* Save our position, for the
5095 window_scroll_pixel_based_preserve_y case. */
5096 charpos = IT_CHARPOS (it);
5097 bytepos = IT_BYTEPOS (it);
5099 /* We moved the window start towards BEGV, so PT may be now
5100 in the scroll margin at the bottom. */
5101 move_it_to (&it, PT, -1,
5102 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5103 - this_scroll_margin - 1),
5105 MOVE_TO_POS | MOVE_TO_Y);
5107 /* Save our position, in case it's correct. */
5108 charpos = IT_CHARPOS (it);
5109 bytepos = IT_BYTEPOS (it);
5111 /* If PT is in the screen line at the last fully visible line,
5112 move_it_to will stop at X = 0 in that line, because the
5113 required Y coordinate is reached there. See if we can get to
5114 PT without descending lower in Y, and if we can, it means we
5115 reached PT before the scroll margin. */
5116 if (charpos != PT)
5118 struct it it2;
5119 void *it_data;
5121 it2 = it;
5122 it_data = bidi_shelve_cache ();
5123 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5124 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5126 charpos = IT_CHARPOS (it);
5127 bytepos = IT_BYTEPOS (it);
5128 bidi_unshelve_cache (it_data, true);
5130 else
5132 it = it2;
5133 bidi_unshelve_cache (it_data, false);
5137 /* See if point is on a partially visible line at the end. */
5138 if (it.what == IT_EOB)
5139 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5140 else
5142 move_it_by_lines (&it, 1);
5143 partial_p = it.current_y > it.last_visible_y;
5146 if (charpos == PT && !partial_p
5147 && (NILP (Vscroll_preserve_screen_position)
5148 || EQ (Vscroll_preserve_screen_position, Qt)))
5149 /* We found PT before we found the display margin, so PT is ok. */
5151 else if (window_scroll_pixel_based_preserve_y >= 0)
5153 SET_TEXT_POS_FROM_MARKER (start, w->start);
5154 start_display (&it, w, start);
5155 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5156 here because we called start_display again and did not
5157 alter it.current_y this time. */
5158 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5159 window_scroll_pixel_based_preserve_y, -1,
5160 MOVE_TO_Y | MOVE_TO_X);
5161 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5163 else
5165 if (partial_p)
5166 /* The last line was only partially visible, so back up two
5167 lines to make sure we're on a fully visible line. */
5169 move_it_by_lines (&it, -2);
5170 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5172 else
5173 /* No, the position we saved is OK, so use it. */
5174 SET_PT_BOTH (charpos, bytepos);
5177 bidi_unshelve_cache (itdata, false);
5179 if (adjust_old_pointm)
5180 Fset_marker (w->old_pointm,
5181 ((w == XWINDOW (selected_window))
5182 ? make_number (BUF_PT (XBUFFER (w->contents)))
5183 : Fmarker_position (w->pointm)),
5184 w->contents);
5188 /* Implementation of window_scroll that works based on screen lines.
5189 See the comment of window_scroll for parameter descriptions. */
5191 static void
5192 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5194 struct window *w = XWINDOW (window);
5195 /* Fvertical_motion enters redisplay, which can trigger
5196 fontification, which in turn can modify buffer text (e.g., if the
5197 fontification functions replace escape sequences with faces, as
5198 in `grep-mode-font-lock-keywords'). So we use a marker to record
5199 the old point position, to prevent crashes in SET_PT_BOTH. */
5200 Lisp_Object opoint_marker = Fpoint_marker ();
5201 register ptrdiff_t pos, pos_byte;
5202 register int ht = window_internal_height (w);
5203 register Lisp_Object tem;
5204 bool lose;
5205 Lisp_Object bolp;
5206 ptrdiff_t startpos = marker_position (w->start);
5207 ptrdiff_t startbyte = marker_byte_position (w->start);
5208 Lisp_Object original_pos = Qnil;
5209 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5210 Fwindow_old_point (window)));
5212 /* If scrolling screen-fulls, compute the number of lines to
5213 scroll from the window's height. */
5214 if (whole)
5215 n *= max (1, ht - next_screen_context_lines);
5217 if (!NILP (Vscroll_preserve_screen_position))
5219 if (window_scroll_preserve_vpos <= 0
5220 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5221 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5223 struct position posit
5224 = *compute_motion (startpos, startbyte, 0, 0, false,
5225 PT, ht, 0, -1, w->hscroll, 0, w);
5227 window_scroll_preserve_vpos = posit.vpos;
5228 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5231 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5232 make_number (window_scroll_preserve_vpos));
5235 XSETFASTINT (tem, PT);
5236 tem = Fpos_visible_in_window_p (tem, window, Qnil, Qnil);
5238 if (NILP (tem))
5240 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5241 startpos = PT;
5242 startbyte = PT_BYTE;
5245 SET_PT_BOTH (startpos, startbyte);
5246 lose = n < 0 && PT == BEGV;
5247 Fvertical_motion (make_number (n), window, Qnil);
5248 pos = PT;
5249 pos_byte = PT_BYTE;
5250 bolp = Fbolp ();
5251 SET_PT_BOTH (marker_position (opoint_marker),
5252 marker_byte_position (opoint_marker));
5254 if (lose)
5256 if (noerror)
5257 return;
5258 else
5259 xsignal0 (Qbeginning_of_buffer);
5262 if (pos < ZV)
5264 /* Don't use a scroll margin that is negative or too large. */
5265 int this_scroll_margin =
5266 max (0, min (scroll_margin, w->total_lines / 4));
5268 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5269 w->start_at_line_beg = !NILP (bolp);
5270 wset_update_mode_line (w);
5271 /* Set force_start so that redisplay_window will run
5272 the window-scroll-functions. */
5273 w->force_start = true;
5275 if (!NILP (Vscroll_preserve_screen_position)
5276 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5278 SET_PT_BOTH (pos, pos_byte);
5279 Fvertical_motion (original_pos, window, Qnil);
5281 /* If we scrolled forward, put point enough lines down
5282 that it is outside the scroll margin. */
5283 else if (n > 0)
5285 int top_margin;
5287 if (this_scroll_margin > 0)
5289 SET_PT_BOTH (pos, pos_byte);
5290 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5291 top_margin = PT;
5293 else
5294 top_margin = pos;
5296 if (top_margin <= marker_position (opoint_marker))
5297 SET_PT_BOTH (marker_position (opoint_marker),
5298 marker_byte_position (opoint_marker));
5299 else if (!NILP (Vscroll_preserve_screen_position))
5301 SET_PT_BOTH (pos, pos_byte);
5302 Fvertical_motion (original_pos, window, Qnil);
5304 else
5305 SET_PT (top_margin);
5307 else if (n < 0)
5309 int bottom_margin;
5311 /* If we scrolled backward, put point near the end of the window
5312 but not within the scroll margin. */
5313 SET_PT_BOTH (pos, pos_byte);
5314 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5315 Qnil);
5316 if (XFASTINT (tem) == ht - this_scroll_margin)
5317 bottom_margin = PT;
5318 else
5319 bottom_margin = PT + 1;
5321 if (bottom_margin > marker_position (opoint_marker))
5322 SET_PT_BOTH (marker_position (opoint_marker),
5323 marker_byte_position (opoint_marker));
5324 else
5326 if (!NILP (Vscroll_preserve_screen_position))
5328 SET_PT_BOTH (pos, pos_byte);
5329 Fvertical_motion (original_pos, window, Qnil);
5331 else
5332 Fvertical_motion (make_number (-1), window, Qnil);
5336 else
5338 if (noerror)
5339 return;
5340 else
5341 xsignal0 (Qend_of_buffer);
5344 if (adjust_old_pointm)
5345 Fset_marker (w->old_pointm,
5346 ((w == XWINDOW (selected_window))
5347 ? make_number (BUF_PT (XBUFFER (w->contents)))
5348 : Fmarker_position (w->pointm)),
5349 w->contents);
5353 /* Scroll selected_window up or down. If N is nil, scroll a
5354 screen-full which is defined as the height of the window minus
5355 next_screen_context_lines. If N is the symbol `-', scroll.
5356 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5357 up. This is the guts of Fscroll_up and Fscroll_down. */
5359 static void
5360 scroll_command (Lisp_Object n, int direction)
5362 ptrdiff_t count = SPECPDL_INDEX ();
5364 eassert (eabs (direction) == 1);
5366 /* If selected window's buffer isn't current, make it current for
5367 the moment. But don't screw up if window_scroll gets an error. */
5368 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5370 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5371 Fset_buffer (XWINDOW (selected_window)->contents);
5374 if (NILP (n))
5375 window_scroll (selected_window, direction, true, false);
5376 else if (EQ (n, Qminus))
5377 window_scroll (selected_window, -direction, true, false);
5378 else
5380 n = Fprefix_numeric_value (n);
5381 window_scroll (selected_window, XINT (n) * direction, false, false);
5384 unbind_to (count, Qnil);
5387 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5388 doc: /* Scroll text of selected window upward ARG lines.
5389 If ARG is omitted or nil, scroll upward by a near full screen.
5390 A near full screen is `next-screen-context-lines' less than a full screen.
5391 Negative ARG means scroll downward.
5392 If ARG is the atom `-', scroll downward by nearly full screen.
5393 When calling from a program, supply as argument a number, nil, or `-'. */)
5394 (Lisp_Object arg)
5396 scroll_command (arg, 1);
5397 return Qnil;
5400 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5401 doc: /* Scroll text of selected window down ARG lines.
5402 If ARG is omitted or nil, scroll down by a near full screen.
5403 A near full screen is `next-screen-context-lines' less than a full screen.
5404 Negative ARG means scroll upward.
5405 If ARG is the atom `-', scroll upward by nearly full screen.
5406 When calling from a program, supply as argument a number, nil, or `-'. */)
5407 (Lisp_Object arg)
5409 scroll_command (arg, -1);
5410 return Qnil;
5413 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5414 doc: /* Return the other window for \"other window scroll\" commands.
5415 If `other-window-scroll-buffer' is non-nil, a window
5416 showing that buffer is used.
5417 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5418 specifies the window. This takes precedence over
5419 `other-window-scroll-buffer'. */)
5420 (void)
5422 Lisp_Object window;
5424 if (MINI_WINDOW_P (XWINDOW (selected_window))
5425 && !NILP (Vminibuf_scroll_window))
5426 window = Vminibuf_scroll_window;
5427 /* If buffer is specified and live, scroll that buffer. */
5428 else if (!NILP (Vother_window_scroll_buffer)
5429 && BUFFERP (Vother_window_scroll_buffer)
5430 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5432 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5433 if (NILP (window))
5434 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5436 else
5438 /* Nothing specified; look for a neighboring window on the same
5439 frame. */
5440 window = Fnext_window (selected_window, Qnil, Qnil);
5442 if (EQ (window, selected_window))
5443 /* That didn't get us anywhere; look for a window on another
5444 visible frame. */
5446 window = Fnext_window (window, Qnil, Qt);
5447 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5448 && ! EQ (window, selected_window));
5451 CHECK_LIVE_WINDOW (window);
5453 if (EQ (window, selected_window))
5454 error ("There is no other window");
5456 return window;
5459 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5460 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5461 A near full screen is `next-screen-context-lines' less than a full screen.
5462 The next window is the one below the current one; or the one at the top
5463 if the current one is at the bottom. Negative ARG means scroll downward.
5464 If ARG is the atom `-', scroll downward by nearly full screen.
5465 When calling from a program, supply as argument a number, nil, or `-'.
5467 If `other-window-scroll-buffer' is non-nil, scroll the window
5468 showing that buffer, popping the buffer up if necessary.
5469 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5470 specifies the window to scroll. This takes precedence over
5471 `other-window-scroll-buffer'. */)
5472 (Lisp_Object arg)
5474 Lisp_Object window;
5475 struct window *w;
5476 ptrdiff_t count = SPECPDL_INDEX ();
5478 window = Fother_window_for_scrolling ();
5479 w = XWINDOW (window);
5481 /* Don't screw up if window_scroll gets an error. */
5482 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5484 Fset_buffer (w->contents);
5485 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5486 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5488 if (NILP (arg))
5489 window_scroll (window, 1, true, true);
5490 else if (EQ (arg, Qminus))
5491 window_scroll (window, -1, true, true);
5492 else
5494 if (CONSP (arg))
5495 arg = XCAR (arg);
5496 CHECK_NUMBER (arg);
5497 window_scroll (window, XINT (arg), false, true);
5500 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5501 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5502 unbind_to (count, Qnil);
5504 return Qnil;
5507 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5508 doc: /* Scroll selected window display ARG columns left.
5509 Default for ARG is window width minus 2.
5510 Value is the total amount of leftward horizontal scrolling in
5511 effect after the change.
5512 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5513 lower bound for automatic scrolling, i.e. automatic scrolling
5514 will not scroll a window to a column less than the value returned
5515 by this function. This happens in an interactive call. */)
5516 (register Lisp_Object arg, Lisp_Object set_minimum)
5518 struct window *w = XWINDOW (selected_window);
5519 EMACS_INT requested_arg = (NILP (arg)
5520 ? window_body_width (w, 0) - 2
5521 : XINT (Fprefix_numeric_value (arg)));
5522 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5524 if (!NILP (set_minimum))
5525 w->min_hscroll = w->hscroll;
5527 w->suspend_auto_hscroll = true;
5529 return result;
5532 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5533 doc: /* Scroll selected window display ARG columns right.
5534 Default for ARG is window width minus 2.
5535 Value is the total amount of leftward horizontal scrolling in
5536 effect after the change.
5537 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5538 lower bound for automatic scrolling, i.e. automatic scrolling
5539 will not scroll a window to a column less than the value returned
5540 by this function. This happens in an interactive call. */)
5541 (register Lisp_Object arg, Lisp_Object set_minimum)
5543 struct window *w = XWINDOW (selected_window);
5544 EMACS_INT requested_arg = (NILP (arg)
5545 ? window_body_width (w, 0) - 2
5546 : XINT (Fprefix_numeric_value (arg)));
5547 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5549 if (!NILP (set_minimum))
5550 w->min_hscroll = w->hscroll;
5552 w->suspend_auto_hscroll = true;
5554 return result;
5557 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5558 doc: /* Return the window which was selected when entering the minibuffer.
5559 Returns nil, if selected window is not a minibuffer window. */)
5560 (void)
5562 if (minibuf_level > 0
5563 && MINI_WINDOW_P (XWINDOW (selected_window))
5564 && WINDOW_LIVE_P (minibuf_selected_window))
5565 return minibuf_selected_window;
5567 return Qnil;
5570 /* Value is the number of lines actually displayed in window W,
5571 as opposed to its height. */
5573 static int
5574 displayed_window_lines (struct window *w)
5576 struct it it;
5577 struct text_pos start;
5578 int height = window_box_height (w);
5579 struct buffer *old_buffer;
5580 int bottom_y;
5581 void *itdata = NULL;
5583 if (XBUFFER (w->contents) != current_buffer)
5585 old_buffer = current_buffer;
5586 set_buffer_internal (XBUFFER (w->contents));
5588 else
5589 old_buffer = NULL;
5591 /* In case W->start is out of the accessible range, do something
5592 reasonable. This happens in Info mode when Info-scroll-down
5593 calls (recenter -1) while W->start is 1. */
5594 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5596 itdata = bidi_shelve_cache ();
5597 start_display (&it, w, start);
5598 move_it_vertically (&it, height);
5599 bottom_y = line_bottom_y (&it);
5600 bidi_unshelve_cache (itdata, false);
5602 /* rms: On a non-window display,
5603 the value of it.vpos at the bottom of the screen
5604 seems to be 1 larger than window_box_height (w).
5605 This kludge fixes a bug whereby (move-to-window-line -1)
5606 when ZV is on the last screen line
5607 moves to the previous screen line instead of the last one. */
5608 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5609 height++;
5611 /* Add in empty lines at the bottom of the window. */
5612 if (bottom_y < height)
5614 int uy = FRAME_LINE_HEIGHT (it.f);
5615 it.vpos += (height - bottom_y + uy - 1) / uy;
5618 if (old_buffer)
5619 set_buffer_internal (old_buffer);
5621 return it.vpos;
5625 DEFUN ("recenter", Frecenter, Srecenter, 0, 2, "P\ni",
5626 doc: /* Center point in selected window and maybe redisplay frame.
5627 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5628 relative to the selected window. If ARG is negative, it counts up from the
5629 bottom of the window. (ARG should be less than the height of the window.)
5631 If ARG is omitted or nil, then recenter with point on the middle line of
5632 the selected window; if the variable `recenter-redisplay' is non-nil,
5633 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5634 is set to `grow-only', this resets the tool-bar's height to the minimum
5635 height needed); if `recenter-redisplay' has the special value `tty',
5636 then only tty frames are redrawn.
5638 Just C-u as prefix means put point in the center of the window
5639 and redisplay normally--don't erase and redraw the frame.
5641 When `recenter' is called from a program, GROUP is non-nil, and WINDOW is
5642 part of a group of windows collectively displaying a buffer (such as with
5643 Follow Mode), perform `recenter''s actions on the group rather than on the
5644 individual WINDOW. This condition holds when `recenter-group-function' is
5645 set to a function, in which case `recenter' calls the function with the
5646 argument ARG, then returns its value, instead of doing its normal
5647 processing. */)
5648 (register Lisp_Object arg, Lisp_Object group)
5650 if (!NILP (group)
5651 && FUNCTIONP (Vrecenter_group_function))
5652 return call1 (Vrecenter_group_function, arg);
5654 struct window *w = XWINDOW (selected_window);
5655 struct buffer *buf = XBUFFER (w->contents);
5656 bool center_p = false;
5657 ptrdiff_t charpos, bytepos;
5658 EMACS_INT iarg IF_LINT (= 0);
5659 int this_scroll_margin;
5661 if (buf != current_buffer)
5662 error ("`recenter'ing a window that does not display current-buffer.");
5664 /* If redisplay is suppressed due to an error, try again. */
5665 buf->display_error_modiff = 0;
5667 if (NILP (arg))
5669 if (!NILP (Vrecenter_redisplay)
5670 && (!EQ (Vrecenter_redisplay, Qtty)
5671 || !NILP (Ftty_type (selected_frame))))
5673 ptrdiff_t i;
5675 /* Invalidate pixel data calculated for all compositions. */
5676 for (i = 0; i < n_compositions; i++)
5677 composition_table[i]->font = NULL;
5678 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5679 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5680 #endif
5681 Fredraw_frame (WINDOW_FRAME (w));
5682 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5685 center_p = true;
5687 else if (CONSP (arg)) /* Just C-u. */
5688 center_p = true;
5689 else
5691 arg = Fprefix_numeric_value (arg);
5692 CHECK_NUMBER (arg);
5693 iarg = XINT (arg);
5696 /* Do this after making BUF current
5697 in case scroll_margin is buffer-local. */
5698 this_scroll_margin
5699 = max (0, min (scroll_margin, w->total_lines / 4));
5701 /* Don't use redisplay code for initial frames, as the necessary
5702 data structures might not be set up yet then. */
5703 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5705 if (center_p)
5707 struct it it;
5708 struct text_pos pt;
5709 void *itdata = bidi_shelve_cache ();
5711 SET_TEXT_POS (pt, PT, PT_BYTE);
5712 start_display (&it, w, pt);
5713 move_it_vertically_backward (&it, window_box_height (w) / 2);
5714 charpos = IT_CHARPOS (it);
5715 bytepos = IT_BYTEPOS (it);
5716 bidi_unshelve_cache (itdata, false);
5718 else if (iarg < 0)
5720 struct it it;
5721 struct text_pos pt;
5722 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5723 int extra_line_spacing;
5724 int h = window_box_height (w);
5725 int ht = window_internal_height (w);
5726 void *itdata = bidi_shelve_cache ();
5728 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5729 ht - this_scroll_margin);
5731 SET_TEXT_POS (pt, PT, PT_BYTE);
5732 start_display (&it, w, pt);
5734 /* Be sure we have the exact height of the full line containing PT. */
5735 move_it_by_lines (&it, 0);
5737 /* The amount of pixels we have to move back is the window
5738 height minus what's displayed in the line containing PT,
5739 and the lines below. */
5740 it.current_y = 0;
5741 it.vpos = 0;
5742 move_it_by_lines (&it, nlines);
5744 if (it.vpos == nlines)
5745 h -= it.current_y;
5746 else
5748 /* Last line has no newline. */
5749 h -= line_bottom_y (&it);
5750 it.vpos++;
5753 /* Don't reserve space for extra line spacing of last line. */
5754 extra_line_spacing = it.max_extra_line_spacing;
5756 /* If we can't move down NLINES lines because we hit
5757 the end of the buffer, count in some empty lines. */
5758 if (it.vpos < nlines)
5760 nlines -= it.vpos;
5761 extra_line_spacing = it.extra_line_spacing;
5762 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5764 if (h <= 0)
5766 bidi_unshelve_cache (itdata, false);
5767 return Qnil;
5770 /* Now find the new top line (starting position) of the window. */
5771 start_display (&it, w, pt);
5772 it.current_y = 0;
5773 move_it_vertically_backward (&it, h);
5775 /* If extra line spacing is present, we may move too far
5776 back. This causes the last line to be only partially
5777 visible (which triggers redisplay to recenter that line
5778 in the middle), so move forward.
5779 But ignore extra line spacing on last line, as it is not
5780 considered to be part of the visible height of the line.
5782 h += extra_line_spacing;
5783 while (-it.current_y > h)
5784 move_it_by_lines (&it, 1);
5786 charpos = IT_CHARPOS (it);
5787 bytepos = IT_BYTEPOS (it);
5789 bidi_unshelve_cache (itdata, false);
5791 else
5793 struct it it;
5794 struct text_pos pt;
5795 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5796 int ht = window_internal_height (w);
5797 void *itdata = bidi_shelve_cache ();
5799 nlines = clip_to_bounds (this_scroll_margin, nlines,
5800 ht - this_scroll_margin - 1);
5802 SET_TEXT_POS (pt, PT, PT_BYTE);
5803 start_display (&it, w, pt);
5805 /* Move to the beginning of screen line containing PT. */
5806 move_it_by_lines (&it, 0);
5808 /* Move back to find the point which is ARG screen lines above PT. */
5809 if (nlines > 0)
5811 it.current_y = 0;
5812 it.vpos = 0;
5813 move_it_by_lines (&it, -nlines);
5816 charpos = IT_CHARPOS (it);
5817 bytepos = IT_BYTEPOS (it);
5819 bidi_unshelve_cache (itdata, false);
5822 else
5824 struct position pos;
5825 int ht = window_internal_height (w);
5827 if (center_p)
5828 iarg = ht / 2;
5829 else if (iarg < 0)
5830 iarg += ht;
5832 /* Don't let it get into the margin at either top or bottom. */
5833 iarg = clip_to_bounds (this_scroll_margin, iarg,
5834 ht - this_scroll_margin - 1);
5836 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5837 charpos = pos.bufpos;
5838 bytepos = pos.bytepos;
5841 /* Set the new window start. */
5842 set_marker_both (w->start, w->contents, charpos, bytepos);
5843 w->window_end_valid = false;
5845 w->optional_new_start = true;
5847 w->start_at_line_beg = (bytepos == BEGV_BYTE
5848 || FETCH_BYTE (bytepos - 1) == '\n');
5850 wset_redisplay (w);
5852 return Qnil;
5856 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5857 0, 2, 0,
5858 doc: /* Return the width in columns of the text display area of WINDOW.
5859 WINDOW must be a live window and defaults to the selected one.
5861 The returned width does not include dividers, scrollbars, margins,
5862 fringes, nor any partial-width columns at the right of the text
5863 area.
5865 Optional argument PIXELWISE non-nil, means to return the width in
5866 pixels. */)
5867 (Lisp_Object window, Lisp_Object pixelwise)
5869 struct window *w = decode_live_window (window);
5871 if (NILP (pixelwise))
5872 return make_number (window_box_width (w, TEXT_AREA)
5873 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5874 else
5875 return make_number (window_box_width (w, TEXT_AREA));
5878 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5879 0, 2, 0,
5880 doc: /* Return the height in lines of the text display area of WINDOW.
5881 WINDOW must be a live window and defaults to the selected one.
5883 The returned height does not include dividers, the mode line, any header
5884 line, nor any partial-height lines at the bottom of the text area.
5886 Optional argument PIXELWISE non-nil, means to return the height in
5887 pixels. */)
5888 (Lisp_Object window, Lisp_Object pixelwise)
5890 struct window *w = decode_live_window (window);
5892 if (NILP (pixelwise))
5893 return make_number (window_box_height (w)
5894 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5895 else
5896 return make_number (window_box_height (w));
5899 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5900 1, 2, "P\ni",
5901 doc: /* Position point relative to window.
5902 ARG nil means position point at center of window.
5903 Else, ARG specifies vertical position within the window;
5904 zero means top of window, negative means relative to bottom of window.
5906 When GROUP is non-nil, and `move-to-window-line-group-function' is set to a
5907 function, then instead of the above, that function is called with the
5908 single argument ARG, and its result returned.
5910 If GROUP is non-nil, and WINDOW is part of a group of windows collectively
5911 displaying a buffer (such as with Follow Mode), position point relative to
5912 the group of windows as a whole rather than the individual WINDOW. This
5913 condition holds when `move-to-window-line-group-function' is set to a
5914 function, in which case `move-to-window-line' calls the function with the
5915 argument ARG, then returns its result, instead of doing its normal
5916 processing. */)
5917 (Lisp_Object arg, Lisp_Object group)
5919 if (!NILP (group)
5920 && FUNCTIONP (Vmove_to_window_line_group_function))
5921 return call1 (Vmove_to_window_line_group_function, arg);
5923 struct window *w = XWINDOW (selected_window);
5924 int lines, start;
5925 Lisp_Object window;
5926 #if false
5927 int this_scroll_margin;
5928 #endif
5930 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5931 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5932 when passed below to set_marker_both. */
5933 error ("move-to-window-line called from unrelated buffer");
5935 window = selected_window;
5936 start = marker_position (w->start);
5937 if (start < BEGV || start > ZV)
5939 int height = window_internal_height (w);
5940 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
5941 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5942 w->start_at_line_beg = !NILP (Fbolp ());
5943 w->force_start = true;
5945 else
5946 Fgoto_char (w->start);
5948 lines = displayed_window_lines (w);
5950 #if false
5951 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5952 #endif
5954 if (NILP (arg))
5955 XSETFASTINT (arg, lines / 2);
5956 else
5958 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5960 if (iarg < 0)
5961 iarg = iarg + lines;
5963 #if false /* This code would prevent move-to-window-line from moving point
5964 to a place inside the scroll margins (which would cause the
5965 next redisplay to scroll). I wrote this code, but then concluded
5966 it is probably better not to install it. However, it is here
5967 inside #if false so as not to lose it. -- rms. */
5969 /* Don't let it get into the margin at either top or bottom. */
5970 iarg = max (iarg, this_scroll_margin);
5971 iarg = min (iarg, lines - this_scroll_margin - 1);
5972 #endif
5974 arg = make_number (iarg);
5977 /* Skip past a partially visible first line. */
5978 if (w->vscroll)
5979 XSETINT (arg, XINT (arg) + 1);
5981 return Fvertical_motion (arg, window, Qnil);
5987 /***********************************************************************
5988 Window Configuration
5989 ***********************************************************************/
5991 struct save_window_data
5993 struct vectorlike_header header;
5994 Lisp_Object selected_frame;
5995 Lisp_Object current_window;
5996 Lisp_Object current_buffer;
5997 Lisp_Object minibuf_scroll_window;
5998 Lisp_Object minibuf_selected_window;
5999 Lisp_Object root_window;
6000 Lisp_Object focus_frame;
6001 /* A vector, each of whose elements is a struct saved_window
6002 for one window. */
6003 Lisp_Object saved_windows;
6005 /* All fields above are traced by the GC.
6006 From `frame-cols' down, the fields are ignored by the GC. */
6007 /* We should be able to do without the following two. */
6008 int frame_cols, frame_lines;
6009 /* These two should get eventually replaced by their pixel
6010 counterparts. */
6011 int frame_menu_bar_lines, frame_tool_bar_lines;
6012 int frame_text_width, frame_text_height;
6013 /* These are currently unused. We need them as soon as we convert
6014 to pixels. */
6015 int frame_menu_bar_height, frame_tool_bar_height;
6018 /* This is saved as a Lisp_Vector. */
6019 struct saved_window
6021 struct vectorlike_header header;
6023 Lisp_Object window, buffer, start, pointm, old_pointm;
6024 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6025 Lisp_Object left_col, top_line, total_cols, total_lines;
6026 Lisp_Object normal_cols, normal_lines;
6027 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6028 Lisp_Object parent, prev;
6029 Lisp_Object start_at_line_beg;
6030 Lisp_Object display_table;
6031 Lisp_Object left_margin_cols, right_margin_cols;
6032 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6033 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6034 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6035 Lisp_Object combination_limit, window_parameters;
6038 #define SAVED_WINDOW_N(swv,n) \
6039 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6041 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6042 doc: /* Return t if OBJECT is a window-configuration object. */)
6043 (Lisp_Object object)
6045 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6048 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6049 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6050 (Lisp_Object config)
6052 register struct save_window_data *data;
6053 struct Lisp_Vector *saved_windows;
6055 CHECK_WINDOW_CONFIGURATION (config);
6057 data = (struct save_window_data *) XVECTOR (config);
6058 saved_windows = XVECTOR (data->saved_windows);
6059 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6062 DEFUN ("set-window-configuration", Fset_window_configuration,
6063 Sset_window_configuration, 1, 1, 0,
6064 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6065 CONFIGURATION must be a value previously returned
6066 by `current-window-configuration' (which see).
6067 If CONFIGURATION was made from a frame that is now deleted,
6068 only frame-independent values can be restored. In this case,
6069 the return value is nil. Otherwise the value is t. */)
6070 (Lisp_Object configuration)
6072 register struct save_window_data *data;
6073 struct Lisp_Vector *saved_windows;
6074 Lisp_Object new_current_buffer;
6075 Lisp_Object frame;
6076 struct frame *f;
6077 ptrdiff_t old_point = -1;
6078 USE_SAFE_ALLOCA;
6080 CHECK_WINDOW_CONFIGURATION (configuration);
6082 data = (struct save_window_data *) XVECTOR (configuration);
6083 saved_windows = XVECTOR (data->saved_windows);
6085 new_current_buffer = data->current_buffer;
6086 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6087 new_current_buffer = Qnil;
6088 else
6090 if (XBUFFER (new_current_buffer) == current_buffer)
6091 /* The code further down "preserves point" by saving here PT in
6092 old_point and then setting it later back into PT. When the
6093 current-selected-window and the final-selected-window both show
6094 the current buffer, this suffers from the problem that the
6095 current PT is the window-point of the current-selected-window,
6096 while the final PT is the point of the final-selected-window, so
6097 this copy from one PT to the other would end up moving the
6098 window-point of the final-selected-window to the window-point of
6099 the current-selected-window. So we have to be careful which
6100 point of the current-buffer we copy into old_point. */
6101 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6102 && WINDOWP (selected_window)
6103 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6104 && !EQ (selected_window, data->current_window))
6105 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6106 else
6107 old_point = PT;
6108 else
6109 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6110 point in new_current_buffer as of the last time this buffer was
6111 used. This can be non-deterministic since it can be changed by
6112 things like jit-lock by mere temporary selection of some random
6113 window that happens to show this buffer.
6114 So if possible we want this arbitrary choice of "which point" to
6115 be the one from the to-be-selected-window so as to prevent this
6116 window's cursor from being copied from another window. */
6117 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6118 /* If current_window = selected_window, its point is in BUF_PT. */
6119 && !EQ (selected_window, data->current_window))
6120 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6121 else
6122 old_point = BUF_PT (XBUFFER (new_current_buffer));
6125 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6126 f = XFRAME (frame);
6128 /* If f is a dead frame, don't bother rebuilding its window tree.
6129 However, there is other stuff we should still try to do below. */
6130 if (FRAME_LIVE_P (f))
6132 Lisp_Object window;
6133 Lisp_Object dead_windows = Qnil;
6134 Lisp_Object tem, par, pers;
6135 struct window *w;
6136 struct saved_window *p;
6137 struct window *root_window;
6138 struct window **leaf_windows;
6139 ptrdiff_t i, k, n_leaf_windows;
6141 /* Don't do this within the main loop below: This may call Lisp
6142 code and is thus potentially unsafe while input is blocked. */
6143 for (k = 0; k < saved_windows->header.size; k++)
6145 p = SAVED_WINDOW_N (saved_windows, k);
6146 window = p->window;
6147 w = XWINDOW (window);
6148 if (BUFFERP (w->contents)
6149 && !EQ (w->contents, p->buffer)
6150 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6151 /* If a window we restore gets another buffer, record the
6152 window's old buffer. */
6153 call1 (Qrecord_window_buffer, window);
6156 /* Disallow x_set_window_size, temporarily. */
6157 f->can_x_set_window_size = false;
6158 /* The mouse highlighting code could get screwed up
6159 if it runs during this. */
6160 block_input ();
6162 /* "Swap out" point from the selected window's buffer
6163 into the window itself. (Normally the pointm of the selected
6164 window holds garbage.) We do this now, before
6165 restoring the window contents, and prevent it from
6166 being done later on when we select a new window. */
6167 if (! NILP (XWINDOW (selected_window)->contents))
6169 w = XWINDOW (selected_window);
6170 set_marker_both (w->pointm,
6171 w->contents,
6172 BUF_PT (XBUFFER (w->contents)),
6173 BUF_PT_BYTE (XBUFFER (w->contents)));
6176 fset_redisplay (f);
6177 FRAME_WINDOW_SIZES_CHANGED (f) = true;
6179 /* Problem: Freeing all matrices and later allocating them again
6180 is a serious redisplay flickering problem. What we would
6181 really like to do is to free only those matrices not reused
6182 below. */
6183 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6184 ptrdiff_t nwindows = count_windows (root_window);
6185 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6186 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6188 /* Kludge Alert!
6189 Mark all windows now on frame as "deleted".
6190 Restoring the new configuration "undeletes" any that are in it.
6192 Save their current buffers in their height fields, since we may
6193 need it later, if a buffer saved in the configuration is now
6194 dead. */
6195 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6197 for (k = 0; k < saved_windows->header.size; k++)
6199 p = SAVED_WINDOW_N (saved_windows, k);
6200 window = p->window;
6201 w = XWINDOW (window);
6202 wset_next (w, Qnil);
6204 if (!NILP (p->parent))
6205 wset_parent
6206 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6207 else
6208 wset_parent (w, Qnil);
6210 if (!NILP (p->prev))
6212 wset_prev
6213 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6214 wset_next (XWINDOW (w->prev), p->window);
6216 else
6218 wset_prev (w, Qnil);
6219 if (!NILP (w->parent))
6220 wset_combination (XWINDOW (w->parent),
6221 (XINT (p->total_cols)
6222 != XWINDOW (w->parent)->total_cols),
6223 p->window);
6226 /* If we squirreled away the buffer, restore it now. */
6227 if (BUFFERP (w->combination_limit))
6228 wset_buffer (w, w->combination_limit);
6229 w->pixel_left = XFASTINT (p->pixel_left);
6230 w->pixel_top = XFASTINT (p->pixel_top);
6231 w->pixel_width = XFASTINT (p->pixel_width);
6232 w->pixel_height = XFASTINT (p->pixel_height);
6233 w->left_col = XFASTINT (p->left_col);
6234 w->top_line = XFASTINT (p->top_line);
6235 w->total_cols = XFASTINT (p->total_cols);
6236 w->total_lines = XFASTINT (p->total_lines);
6237 wset_normal_cols (w, p->normal_cols);
6238 wset_normal_lines (w, p->normal_lines);
6239 w->hscroll = XFASTINT (p->hscroll);
6240 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6241 w->min_hscroll = XFASTINT (p->min_hscroll);
6242 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6243 wset_display_table (w, p->display_table);
6244 w->left_margin_cols = XINT (p->left_margin_cols);
6245 w->right_margin_cols = XINT (p->right_margin_cols);
6246 w->left_fringe_width = XINT (p->left_fringe_width);
6247 w->right_fringe_width = XINT (p->right_fringe_width);
6248 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6249 w->scroll_bar_width = XINT (p->scroll_bar_width);
6250 w->scroll_bar_height = XINT (p->scroll_bar_height);
6251 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6252 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6253 wset_dedicated (w, p->dedicated);
6254 wset_combination_limit (w, p->combination_limit);
6255 /* Restore any window parameters that have been saved.
6256 Parameters that have not been saved are left alone. */
6257 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6259 pers = XCAR (tem);
6260 if (CONSP (pers))
6262 if (NILP (XCDR (pers)))
6264 par = Fassq (XCAR (pers), w->window_parameters);
6265 if (CONSP (par) && !NILP (XCDR (par)))
6266 /* Reset a parameter to nil if and only if it
6267 has a non-nil association. Don't make new
6268 associations. */
6269 Fsetcdr (par, Qnil);
6271 else
6272 /* Always restore a non-nil value. */
6273 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6277 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6278 /* If saved buffer is alive, install it. */
6280 wset_buffer (w, p->buffer);
6281 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6282 set_marker_restricted (w->start, p->start, w->contents);
6283 set_marker_restricted (w->pointm, p->pointm, w->contents);
6284 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6285 /* As documented in Fcurrent_window_configuration, don't
6286 restore the location of point in the buffer which was
6287 current when the window configuration was recorded. */
6288 if (!EQ (p->buffer, new_current_buffer)
6289 && XBUFFER (p->buffer) == current_buffer)
6290 Fgoto_char (w->pointm);
6292 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6293 /* Keep window's old buffer; make sure the markers are real. */
6295 /* Set window markers at start of visible range. */
6296 if (XMARKER (w->start)->buffer == 0)
6297 set_marker_restricted_both (w->start, w->contents, 0, 0);
6298 if (XMARKER (w->pointm)->buffer == 0)
6299 set_marker_restricted_both
6300 (w->pointm, w->contents,
6301 BUF_PT (XBUFFER (w->contents)),
6302 BUF_PT_BYTE (XBUFFER (w->contents)));
6303 if (XMARKER (w->old_pointm)->buffer == 0)
6304 set_marker_restricted_both
6305 (w->old_pointm, w->contents,
6306 BUF_PT (XBUFFER (w->contents)),
6307 BUF_PT_BYTE (XBUFFER (w->contents)));
6308 w->start_at_line_beg = true;
6310 else if (!NILP (w->start))
6311 /* Leaf window has no live buffer, get one. */
6313 /* Get the buffer via other_buffer_safely in order to
6314 avoid showing an unimportant buffer and, if necessary, to
6315 recreate *scratch* in the course (part of Juanma's bs-show
6316 scenario from March 2011). */
6317 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6318 /* This will set the markers to beginning of visible
6319 range. */
6320 set_marker_restricted_both (w->start, w->contents, 0, 0);
6321 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6322 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6323 w->start_at_line_beg = true;
6324 if (!NILP (w->dedicated))
6325 /* Record this window as dead. */
6326 dead_windows = Fcons (window, dead_windows);
6327 /* Make sure window is no more dedicated. */
6328 wset_dedicated (w, Qnil);
6332 fset_root_window (f, data->root_window);
6333 /* Arrange *not* to restore point in the buffer that was
6334 current when the window configuration was saved. */
6335 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6336 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6337 make_number (old_point),
6338 XWINDOW (data->current_window)->contents);
6340 /* In the following call to `select-window', prevent "swapping out
6341 point" in the old selected window using the buffer that has
6342 been restored into it. We already swapped out that point from
6343 that window's old buffer.
6345 Do not record the buffer here. We do that in a separate call
6346 to select_window below. See also Bug#16207. */
6347 select_window (data->current_window, Qt, true);
6348 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6349 last_selected_window)
6350 = selected_window;
6352 if (NILP (data->focus_frame)
6353 || (FRAMEP (data->focus_frame)
6354 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6355 Fredirect_frame_focus (frame, data->focus_frame);
6357 /* Now, free glyph matrices in windows that were not reused. */
6358 for (i = 0; i < n_leaf_windows; i++)
6359 if (NILP (leaf_windows[i]->contents))
6360 free_window_matrices (leaf_windows[i]);
6362 /* Allow x_set_window_size again and apply frame size changes if
6363 needed. */
6364 f->can_x_set_window_size = true;
6365 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6367 adjust_frame_glyphs (f);
6368 unblock_input ();
6370 /* Scan dead buffer windows. */
6371 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6373 window = XCAR (dead_windows);
6374 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6375 delete_deletable_window (window);
6378 /* Record the selected window's buffer here. The window should
6379 already be the selected one from the call above. */
6380 select_window (data->current_window, Qnil, false);
6382 /* Fselect_window will have made f the selected frame, so we
6383 reselect the proper frame here. Fhandle_switch_frame will change the
6384 selected window too, but that doesn't make the call to
6385 Fselect_window above totally superfluous; it still sets f's
6386 selected window. */
6387 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6388 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6390 run_window_configuration_change_hook (f);
6393 if (!NILP (new_current_buffer))
6395 Fset_buffer (new_current_buffer);
6396 /* If the new current buffer doesn't appear in the selected
6397 window, go to its old point (see bug#12208). */
6398 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6399 Fgoto_char (make_number (old_point));
6402 Vminibuf_scroll_window = data->minibuf_scroll_window;
6403 minibuf_selected_window = data->minibuf_selected_window;
6405 SAFE_FREE ();
6406 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6410 void
6411 restore_window_configuration (Lisp_Object configuration)
6413 Fset_window_configuration (configuration);
6417 /* If WINDOW is an internal window, recursively delete all child windows
6418 reachable via the next and contents slots of WINDOW. Otherwise setup
6419 WINDOW to not show any buffer. */
6421 void
6422 delete_all_child_windows (Lisp_Object window)
6424 register struct window *w;
6426 w = XWINDOW (window);
6428 if (!NILP (w->next))
6429 /* Delete WINDOW's siblings (we traverse postorderly). */
6430 delete_all_child_windows (w->next);
6432 if (WINDOWP (w->contents))
6434 delete_all_child_windows (w->contents);
6435 wset_combination (w, false, Qnil);
6437 else if (BUFFERP (w->contents))
6439 unshow_buffer (w);
6440 unchain_marker (XMARKER (w->pointm));
6441 unchain_marker (XMARKER (w->old_pointm));
6442 unchain_marker (XMARKER (w->start));
6443 /* Since combination limit makes sense for an internal windows
6444 only, we use this slot to save the buffer for the sake of
6445 possible resurrection in Fset_window_configuration. */
6446 wset_combination_limit (w, w->contents);
6447 wset_buffer (w, Qnil);
6450 Vwindow_list = Qnil;
6453 static ptrdiff_t
6454 count_windows (struct window *window)
6456 ptrdiff_t count = 1;
6457 if (!NILP (window->next))
6458 count += count_windows (XWINDOW (window->next));
6459 if (WINDOWP (window->contents))
6460 count += count_windows (XWINDOW (window->contents));
6461 return count;
6465 /* Fill vector FLAT with leaf windows under W, starting at index I.
6466 Value is last index + 1. */
6467 static ptrdiff_t
6468 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6470 while (w)
6472 if (WINDOWP (w->contents))
6473 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6474 else
6475 flat[i++] = w;
6477 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6480 return i;
6484 /* Return a pointer to the glyph W's physical cursor is on. Value is
6485 null if W's current matrix is invalid, so that no meaningful glyph
6486 can be returned. */
6487 struct glyph *
6488 get_phys_cursor_glyph (struct window *w)
6490 struct glyph_row *row;
6491 struct glyph *glyph;
6492 int hpos = w->phys_cursor.hpos;
6494 if (!(w->phys_cursor.vpos >= 0
6495 && w->phys_cursor.vpos < w->current_matrix->nrows))
6496 return NULL;
6498 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6499 if (!row->enabled_p)
6500 return NULL;
6502 if (w->hscroll)
6504 /* When the window is hscrolled, cursor hpos can legitimately be
6505 out of bounds, but we draw the cursor at the corresponding
6506 window margin in that case. */
6507 if (!row->reversed_p && hpos < 0)
6508 hpos = 0;
6509 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6510 hpos = row->used[TEXT_AREA] - 1;
6513 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6514 glyph = row->glyphs[TEXT_AREA] + hpos;
6515 else
6516 glyph = NULL;
6518 return glyph;
6522 static ptrdiff_t
6523 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6525 struct saved_window *p;
6526 struct window *w;
6527 Lisp_Object tem, pers, par;
6529 for (; !NILP (window); window = w->next)
6531 p = SAVED_WINDOW_N (vector, i);
6532 w = XWINDOW (window);
6534 wset_temslot (w, make_number (i)); i++;
6535 p->window = window;
6536 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6537 p->pixel_left = make_number (w->pixel_left);
6538 p->pixel_top = make_number (w->pixel_top);
6539 p->pixel_width = make_number (w->pixel_width);
6540 p->pixel_height = make_number (w->pixel_height);
6541 p->left_col = make_number (w->left_col);
6542 p->top_line = make_number (w->top_line);
6543 p->total_cols = make_number (w->total_cols);
6544 p->total_lines = make_number (w->total_lines);
6545 p->normal_cols = w->normal_cols;
6546 p->normal_lines = w->normal_lines;
6547 XSETFASTINT (p->hscroll, w->hscroll);
6548 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6549 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6550 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6551 p->display_table = w->display_table;
6552 p->left_margin_cols = make_number (w->left_margin_cols);
6553 p->right_margin_cols = make_number (w->right_margin_cols);
6554 p->left_fringe_width = make_number (w->left_fringe_width);
6555 p->right_fringe_width = make_number (w->right_fringe_width);
6556 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6557 p->scroll_bar_width = make_number (w->scroll_bar_width);
6558 p->scroll_bar_height = make_number (w->scroll_bar_height);
6559 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6560 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6561 p->dedicated = w->dedicated;
6562 p->combination_limit = w->combination_limit;
6563 p->window_parameters = Qnil;
6565 if (!NILP (Vwindow_persistent_parameters))
6567 /* Run cycle detection on Vwindow_persistent_parameters. */
6568 Lisp_Object tortoise, hare;
6570 hare = tortoise = Vwindow_persistent_parameters;
6571 while (CONSP (hare))
6573 hare = XCDR (hare);
6574 if (!CONSP (hare))
6575 break;
6577 hare = XCDR (hare);
6578 tortoise = XCDR (tortoise);
6580 if (EQ (hare, tortoise))
6581 /* Reset Vwindow_persistent_parameters to Qnil. */
6583 Vwindow_persistent_parameters = Qnil;
6584 break;
6588 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6589 tem = XCDR (tem))
6591 pers = XCAR (tem);
6592 /* Save values for persistent window parameters. */
6593 if (CONSP (pers) && !NILP (XCDR (pers)))
6595 par = Fassq (XCAR (pers), w->window_parameters);
6596 if (NILP (par))
6597 /* If the window has no value for the parameter,
6598 make one. */
6599 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6600 p->window_parameters);
6601 else
6602 /* If the window has a value for the parameter,
6603 save it. */
6604 p->window_parameters = Fcons (Fcons (XCAR (par),
6605 XCDR (par)),
6606 p->window_parameters);
6611 if (BUFFERP (w->contents))
6613 /* Save w's value of point in the window configuration. If w
6614 is the selected window, then get the value of point from
6615 the buffer; pointm is garbage in the selected window. */
6616 if (EQ (window, selected_window))
6617 p->pointm = build_marker (XBUFFER (w->contents),
6618 BUF_PT (XBUFFER (w->contents)),
6619 BUF_PT_BYTE (XBUFFER (w->contents)));
6620 else
6621 p->pointm = Fcopy_marker (w->pointm, Qnil);
6622 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6623 XMARKER (p->pointm)->insertion_type
6624 = !NILP (buffer_local_value /* Don't signal error if void. */
6625 (Qwindow_point_insertion_type, w->contents));
6626 XMARKER (p->old_pointm)->insertion_type
6627 = !NILP (buffer_local_value /* Don't signal error if void. */
6628 (Qwindow_point_insertion_type, w->contents));
6630 p->start = Fcopy_marker (w->start, Qnil);
6631 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6633 else
6635 p->pointm = Qnil;
6636 p->old_pointm = Qnil;
6637 p->start = Qnil;
6638 p->start_at_line_beg = Qnil;
6641 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6642 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6644 if (WINDOWP (w->contents))
6645 i = save_window_save (w->contents, vector, i);
6648 return i;
6651 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6652 Scurrent_window_configuration, 0, 1, 0,
6653 doc: /* Return an object representing the current window configuration of FRAME.
6654 If FRAME is nil or omitted, use the selected frame.
6655 This describes the number of windows, their sizes and current buffers,
6656 and for each displayed buffer, where display starts, and the position of
6657 point. An exception is made for point in the current buffer:
6658 its value is -not- saved.
6659 This also records the currently selected frame, and FRAME's focus
6660 redirection (see `redirect-frame-focus'). The variable
6661 `window-persistent-parameters' specifies which window parameters are
6662 saved by this function. */)
6663 (Lisp_Object frame)
6665 Lisp_Object tem;
6666 ptrdiff_t i, n_windows;
6667 struct save_window_data *data;
6668 struct frame *f = decode_live_frame (frame);
6670 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6671 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6672 PVEC_WINDOW_CONFIGURATION);
6674 data->frame_cols = FRAME_COLS (f);
6675 data->frame_lines = FRAME_LINES (f);
6676 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6677 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6678 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6679 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6680 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6681 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6682 data->selected_frame = selected_frame;
6683 data->current_window = FRAME_SELECTED_WINDOW (f);
6684 XSETBUFFER (data->current_buffer, current_buffer);
6685 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6686 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6687 data->root_window = FRAME_ROOT_WINDOW (f);
6688 data->focus_frame = FRAME_FOCUS_FRAME (f);
6689 tem = make_uninit_vector (n_windows);
6690 data->saved_windows = tem;
6691 for (i = 0; i < n_windows; i++)
6692 ASET (tem, i,
6693 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6694 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6695 XSETWINDOW_CONFIGURATION (tem, data);
6696 return (tem);
6699 /* Called after W's margins, fringes or scroll bars was adjusted. */
6701 static void
6702 apply_window_adjustment (struct window *w)
6704 eassert (w);
6705 clear_glyph_matrix (w->current_matrix);
6706 w->window_end_valid = false;
6707 wset_redisplay (w);
6708 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6712 /***********************************************************************
6713 Marginal Areas
6714 ***********************************************************************/
6716 static int
6717 extract_dimension (Lisp_Object dimension)
6719 if (NILP (dimension))
6720 return -1;
6721 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6722 return XINT (dimension);
6725 static struct window *
6726 set_window_margins (struct window *w, Lisp_Object left_width,
6727 Lisp_Object right_width)
6729 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6730 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6731 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6733 if (w->left_margin_cols != left || w->right_margin_cols != right)
6735 /* Don't change anything if new margins won't fit. */
6736 if ((WINDOW_PIXEL_WIDTH (w)
6737 - WINDOW_FRINGES_WIDTH (w)
6738 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6739 - (left + right) * unit)
6740 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6742 w->left_margin_cols = left;
6743 w->right_margin_cols = right;
6745 return w;
6747 else
6748 return NULL;
6750 else
6751 return NULL;
6754 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6755 2, 3, 0,
6756 doc: /* Set width of marginal areas of window WINDOW.
6757 WINDOW must be a live window and defaults to the selected one.
6759 Second arg LEFT-WIDTH specifies the number of character cells to
6760 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6761 does the same for the right marginal area. A nil width parameter
6762 means no margin.
6764 Return t if any margin was actually changed and nil otherwise. */)
6765 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6767 struct window *w = set_window_margins (decode_live_window (window),
6768 left_width, right_width);
6769 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6773 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6774 0, 1, 0,
6775 doc: /* Get width of marginal areas of window WINDOW.
6776 WINDOW must be a live window and defaults to the selected one.
6778 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6779 If a marginal area does not exist, its width will be returned
6780 as nil. */)
6781 (Lisp_Object window)
6783 struct window *w = decode_live_window (window);
6784 return Fcons (w->left_margin_cols
6785 ? make_number (w->left_margin_cols) : Qnil,
6786 w->right_margin_cols
6787 ? make_number (w->right_margin_cols) : Qnil);
6792 /***********************************************************************
6793 Fringes
6794 ***********************************************************************/
6796 static struct window *
6797 set_window_fringes (struct window *w, Lisp_Object left_width,
6798 Lisp_Object right_width, Lisp_Object outside_margins)
6800 bool outside = !NILP (outside_margins);
6801 int left = extract_dimension (left_width);
6802 int right = extract_dimension (right_width);
6804 /* Do nothing on a tty or if nothing to actually change. */
6805 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6806 && (w->left_fringe_width != left
6807 || w->right_fringe_width != right
6808 || w->fringes_outside_margins != outside))
6810 if (left > 0 || right > 0)
6812 /* Don't change anything if new fringes don't fit. */
6813 if ((WINDOW_PIXEL_WIDTH (w)
6814 - WINDOW_MARGINS_WIDTH (w)
6815 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6816 - max (left, 0) - max (right, 0))
6817 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6818 return NULL;
6821 w->left_fringe_width = left;
6822 w->right_fringe_width = right;
6823 w->fringes_outside_margins = outside;
6825 return w;
6827 else
6828 return NULL;
6831 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6832 2, 4, 0,
6833 doc: /* Set the fringe widths of window WINDOW.
6834 WINDOW must be a live window and defaults to the selected one.
6836 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6837 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6838 fringe width. If a fringe width arg is nil, that means to use the
6839 frame's default fringe width. Default fringe widths can be set with
6840 the command `set-fringe-style'.
6841 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6842 outside of the display margins. By default, fringes are drawn between
6843 display marginal areas and the text area.
6845 Return t if any fringe was actually changed and nil otherwise. */)
6846 (Lisp_Object window, Lisp_Object left_width,
6847 Lisp_Object right_width, Lisp_Object outside_margins)
6849 struct window *w
6850 = set_window_fringes (decode_live_window (window),
6851 left_width, right_width, outside_margins);
6852 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6856 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6857 0, 1, 0,
6858 doc: /* Get width of fringes of window WINDOW.
6859 WINDOW must be a live window and defaults to the selected one.
6861 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6862 (Lisp_Object window)
6864 struct window *w = decode_live_window (window);
6866 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6867 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6868 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6873 /***********************************************************************
6874 Scroll bars
6875 ***********************************************************************/
6877 static struct window *
6878 set_window_scroll_bars (struct window *w, Lisp_Object width,
6879 Lisp_Object vertical_type, Lisp_Object height,
6880 Lisp_Object horizontal_type)
6882 int iwidth = extract_dimension (width);
6883 bool changed = false;
6885 if (iwidth == 0)
6886 vertical_type = Qnil;
6888 if (!(NILP (vertical_type)
6889 || EQ (vertical_type, Qleft)
6890 || EQ (vertical_type, Qright)
6891 || EQ (vertical_type, Qt)))
6892 error ("Invalid type of vertical scroll bar");
6894 if (w->scroll_bar_width != iwidth
6895 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6897 /* Don't change anything if new scroll bar won't fit. */
6898 if ((WINDOW_PIXEL_WIDTH (w)
6899 - WINDOW_MARGINS_WIDTH (w)
6900 - WINDOW_FRINGES_WIDTH (w)
6901 - max (iwidth, 0))
6902 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6904 w->scroll_bar_width = iwidth;
6905 wset_vertical_scroll_bar_type (w, vertical_type);
6906 changed = true;
6910 #if USE_HORIZONTAL_SCROLL_BARS
6912 int iheight = extract_dimension (height);
6914 if (MINI_WINDOW_P (w) || iheight == 0)
6915 horizontal_type = Qnil;
6917 if (!(NILP (horizontal_type)
6918 || EQ (horizontal_type, Qbottom)
6919 || EQ (horizontal_type, Qt)))
6920 error ("Invalid type of horizontal scroll bar");
6922 if (w->scroll_bar_height != iheight
6923 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6925 /* Don't change anything if new scroll bar won't fit. */
6926 if ((WINDOW_PIXEL_HEIGHT (w)
6927 - WINDOW_HEADER_LINE_HEIGHT (w)
6928 - WINDOW_MODE_LINE_HEIGHT (w)
6929 - max (iheight, 0))
6930 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
6932 w->scroll_bar_height = iheight;
6933 wset_horizontal_scroll_bar_type (w, horizontal_type);
6934 changed = true;
6938 #else
6939 wset_horizontal_scroll_bar_type (w, Qnil);
6940 #endif
6942 return changed ? w : NULL;
6945 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6946 Sset_window_scroll_bars, 1, 5, 0,
6947 doc: /* Set width and type of scroll bars of window WINDOW.
6948 WINDOW must be a live window and defaults to the selected one.
6950 Second parameter WIDTH specifies the pixel width for the vertical scroll
6951 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
6952 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6953 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
6954 frame's scroll-bar type.
6956 Fourth parameter HEIGHT specifies the pixel height for the horizontal
6957 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
6958 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
6959 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
6960 means to use the frame's horizontal scroll-bar type.
6962 Return t if scroll bars were actually changed and nil otherwise. */)
6963 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
6964 Lisp_Object height, Lisp_Object horizontal_type)
6966 struct window *w
6967 = set_window_scroll_bars (decode_live_window (window),
6968 width, vertical_type, height, horizontal_type);
6969 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6973 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6974 0, 1, 0,
6975 doc: /* Get width and type of scroll bars of window WINDOW.
6976 WINDOW must be a live window and defaults to the selected one.
6978 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
6979 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
6980 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
6981 value. */)
6982 (Lisp_Object window)
6984 struct window *w = decode_live_window (window);
6986 return Fcons (((w->scroll_bar_width >= 0)
6987 ? make_number (w->scroll_bar_width)
6988 : Qnil),
6989 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6990 w->vertical_scroll_bar_type,
6991 ((w->scroll_bar_height >= 0)
6992 ? make_number (w->scroll_bar_height)
6993 : Qnil),
6994 make_number (WINDOW_SCROLL_BAR_LINES (w)),
6995 w->horizontal_scroll_bar_type));
6998 /***********************************************************************
6999 Smooth scrolling
7000 ***********************************************************************/
7002 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7003 doc: /* Return the amount by which WINDOW is scrolled vertically.
7004 If WINDOW is omitted or nil, it defaults to the selected window.
7005 Normally, value is a multiple of the canonical character height of WINDOW;
7006 optional second arg PIXELS-P means value is measured in pixels. */)
7007 (Lisp_Object window, Lisp_Object pixels_p)
7009 Lisp_Object result;
7010 struct window *w = decode_live_window (window);
7011 struct frame *f = XFRAME (w->frame);
7013 if (FRAME_WINDOW_P (f))
7014 result = (NILP (pixels_p)
7015 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7016 : make_number (-w->vscroll));
7017 else
7018 result = make_number (0);
7019 return result;
7023 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7024 2, 3, 0,
7025 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7026 WINDOW nil means use the selected window. Normally, VSCROLL is a
7027 non-negative multiple of the canonical character height of WINDOW;
7028 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7029 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7030 corresponds to an integral number of pixels. The return value is the
7031 result of this rounding.
7032 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7033 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7035 struct window *w = decode_live_window (window);
7036 struct frame *f = XFRAME (w->frame);
7038 CHECK_NUMBER_OR_FLOAT (vscroll);
7040 if (FRAME_WINDOW_P (f))
7042 int old_dy = w->vscroll;
7044 w->vscroll = - (NILP (pixels_p)
7045 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7046 : XFLOATINT (vscroll));
7047 w->vscroll = min (w->vscroll, 0);
7049 if (w->vscroll != old_dy)
7051 /* Adjust glyph matrix of the frame if the virtual display
7052 area becomes larger than before. */
7053 if (w->vscroll < 0 && w->vscroll < old_dy)
7054 adjust_frame_glyphs (f);
7056 /* Prevent redisplay shortcuts. */
7057 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7061 return Fwindow_vscroll (window, pixels_p);
7065 /* Call FN for all leaf windows on frame F. FN is called with the
7066 first argument being a pointer to the leaf window, and with
7067 additional argument USER_DATA. Stops when FN returns 0. */
7069 static void
7070 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7071 void *user_data)
7073 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7074 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7075 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7079 /* Helper function for foreach_window. Call FN for all leaf windows
7080 reachable from W. FN is called with the first argument being a
7081 pointer to the leaf window, and with additional argument USER_DATA.
7082 Stop when FN returns false. Value is false if stopped by FN. */
7084 static bool
7085 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7086 void *user_data)
7088 bool cont;
7090 for (cont = true; w && cont;)
7092 if (WINDOWP (w->contents))
7093 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7094 else
7095 cont = fn (w, user_data);
7097 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7100 return cont;
7103 /***********************************************************************
7104 Initialization
7105 ***********************************************************************/
7107 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7108 describe the same state of affairs. This is used by Fequal.
7110 IGNORE_POSITIONS means ignore non-matching scroll positions
7111 and the like.
7113 This ignores a couple of things like the dedication status of
7114 window, combination_limit and the like. This might have to be
7115 fixed. */
7117 bool
7118 compare_window_configurations (Lisp_Object configuration1,
7119 Lisp_Object configuration2,
7120 bool ignore_positions)
7122 register struct save_window_data *d1, *d2;
7123 struct Lisp_Vector *sws1, *sws2;
7124 ptrdiff_t i;
7126 CHECK_WINDOW_CONFIGURATION (configuration1);
7127 CHECK_WINDOW_CONFIGURATION (configuration2);
7129 d1 = (struct save_window_data *) XVECTOR (configuration1);
7130 d2 = (struct save_window_data *) XVECTOR (configuration2);
7131 sws1 = XVECTOR (d1->saved_windows);
7132 sws2 = XVECTOR (d2->saved_windows);
7134 /* Frame settings must match. */
7135 if (d1->frame_cols != d2->frame_cols
7136 || d1->frame_lines != d2->frame_lines
7137 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7138 || !EQ (d1->selected_frame, d2->selected_frame)
7139 || !EQ (d1->current_buffer, d2->current_buffer)
7140 || (!ignore_positions
7141 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7142 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7143 || !EQ (d1->focus_frame, d2->focus_frame)
7144 /* Verify that the two configurations have the same number of windows. */
7145 || sws1->header.size != sws2->header.size)
7146 return false;
7148 for (i = 0; i < sws1->header.size; i++)
7150 struct saved_window *sw1, *sw2;
7152 sw1 = SAVED_WINDOW_N (sws1, i);
7153 sw2 = SAVED_WINDOW_N (sws2, i);
7155 if (
7156 /* The "current" windows in the two configurations must
7157 correspond to each other. */
7158 EQ (d1->current_window, sw1->window)
7159 != EQ (d2->current_window, sw2->window)
7160 /* Windows' buffers must match. */
7161 || !EQ (sw1->buffer, sw2->buffer)
7162 || !EQ (sw1->pixel_left, sw2->pixel_left)
7163 || !EQ (sw1->pixel_top, sw2->pixel_top)
7164 || !EQ (sw1->pixel_height, sw2->pixel_height)
7165 || !EQ (sw1->pixel_width, sw2->pixel_width)
7166 || !EQ (sw1->left_col, sw2->left_col)
7167 || !EQ (sw1->top_line, sw2->top_line)
7168 || !EQ (sw1->total_cols, sw2->total_cols)
7169 || !EQ (sw1->total_lines, sw2->total_lines)
7170 || !EQ (sw1->display_table, sw2->display_table)
7171 /* The next two disjuncts check the window structure for
7172 equality. */
7173 || !EQ (sw1->parent, sw2->parent)
7174 || !EQ (sw1->prev, sw2->prev)
7175 || (!ignore_positions
7176 && (!EQ (sw1->hscroll, sw2->hscroll)
7177 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7178 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7179 || NILP (Fequal (sw1->start, sw2->start))
7180 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7181 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7182 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7183 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7184 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7185 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7186 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7187 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7188 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7189 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7190 return false;
7193 return true;
7196 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7197 Scompare_window_configurations, 2, 2, 0,
7198 doc: /* Compare two window configurations as regards the structure of windows.
7199 This function ignores details such as the values of point
7200 and scrolling positions. */)
7201 (Lisp_Object x, Lisp_Object y)
7203 if (compare_window_configurations (x, y, true))
7204 return Qt;
7205 return Qnil;
7208 void
7209 init_window_once (void)
7211 struct frame *f = make_initial_frame ();
7212 XSETFRAME (selected_frame, f);
7213 Vterminal_frame = selected_frame;
7214 minibuf_window = f->minibuffer_window;
7215 selected_window = f->selected_window;
7218 void
7219 init_window (void)
7221 Vwindow_list = Qnil;
7224 void
7225 syms_of_window (void)
7227 DEFSYM (Qscroll_up, "scroll-up");
7228 DEFSYM (Qscroll_down, "scroll-down");
7229 DEFSYM (Qscroll_command, "scroll-command");
7231 Fput (Qscroll_up, Qscroll_command, Qt);
7232 Fput (Qscroll_down, Qscroll_command, Qt);
7234 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7235 DEFSYM (Qwindowp, "windowp");
7236 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7237 DEFSYM (Qwindow_live_p, "window-live-p");
7238 DEFSYM (Qwindow_valid_p, "window-valid-p");
7239 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7240 DEFSYM (Qdelete_window, "delete-window");
7241 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7242 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7243 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7244 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7245 DEFSYM (Qsafe, "safe");
7246 DEFSYM (Qdisplay_buffer, "display-buffer");
7247 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7248 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7249 DEFSYM (Qget_mru_window, "get-mru-window");
7250 DEFSYM (Qwindow_size, "window-size");
7251 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7252 DEFSYM (Qabove, "above");
7253 DEFSYM (Qclone_of, "clone-of");
7254 DEFSYM (Qfloor, "floor");
7255 DEFSYM (Qceiling, "ceiling");
7256 DEFSYM (Qwindow_start_group_function, "window-start-group-function");
7257 DEFSYM (Qwindow_end_group_function, "window-end-group-function");
7258 DEFSYM (Qset_window_start_group_function, "set-window-start-group-function");
7259 DEFSYM (Qrecenter_group_function, "recenter-group-function");
7260 DEFSYM (Qpos_visible_in_window_p_group_function, "pos-visible-in-window-p-group-function");
7261 DEFSYM (Qmove_to_window_line_group_function, "move-to-window-line-group-function");
7263 staticpro (&Vwindow_list);
7265 minibuf_selected_window = Qnil;
7266 staticpro (&minibuf_selected_window);
7268 window_scroll_pixel_based_preserve_x = -1;
7269 window_scroll_pixel_based_preserve_y = -1;
7270 window_scroll_preserve_hpos = -1;
7271 window_scroll_preserve_vpos = -1;
7273 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7274 doc: /* Non-nil means call as function to display a help buffer.
7275 The function is called with one argument, the buffer to be displayed.
7276 Used by `with-output-to-temp-buffer'.
7277 If this function is used, then it must do the entire job of showing
7278 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7279 Vtemp_buffer_show_function = Qnil;
7281 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7282 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7283 Vminibuf_scroll_window = Qnil;
7285 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7286 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7287 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7288 is displayed in the `mode-line' face. */);
7289 mode_line_in_non_selected_windows = true;
7291 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7292 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7293 Vother_window_scroll_buffer = Qnil;
7295 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7296 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7297 auto_window_vscroll_p = true;
7299 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7300 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7301 next_screen_context_lines = 2;
7303 DEFVAR_LISP ("scroll-preserve-screen-position",
7304 Vscroll_preserve_screen_position,
7305 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7306 A value of nil means point does not keep its screen position except
7307 at the scroll margin or window boundary respectively.
7308 A value of t means point keeps its screen position if the scroll
7309 command moved it vertically out of the window, e.g. when scrolling
7310 by full screens.
7311 Any other value means point always keeps its screen position.
7312 Scroll commands should have the `scroll-command' property
7313 on their symbols to be controlled by this variable. */);
7314 Vscroll_preserve_screen_position = Qnil;
7316 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7317 doc: /* Type of marker to use for `window-point'. */);
7318 Vwindow_point_insertion_type = Qnil;
7319 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7321 DEFVAR_LISP ("window-configuration-change-hook",
7322 Vwindow_configuration_change_hook,
7323 doc: /* Functions to call when window configuration changes.
7324 The buffer-local part is run once per window, with the relevant window
7325 selected; while the global part is run only once for the modified frame,
7326 with the relevant frame selected. */);
7327 Vwindow_configuration_change_hook = Qnil;
7329 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7330 doc: /* Non-nil means `recenter' redraws entire frame.
7331 If this option is non-nil, then the `recenter' command with a nil
7332 argument will redraw the entire frame; the special value `tty' causes
7333 the frame to be redrawn only if it is a tty frame. */);
7334 Vrecenter_redisplay = Qtty;
7336 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7337 doc: /* If t, resize window combinations proportionally.
7338 If this variable is nil, splitting a window gets the entire screen space
7339 for displaying the new window from the window to split. Deleting and
7340 resizing a window preferably resizes one adjacent window only.
7342 If this variable is t, splitting a window tries to get the space
7343 proportionally from all windows in the same combination. This also
7344 allows to split a window that is otherwise too small or of fixed size.
7345 Resizing and deleting a window proportionally resize all windows in the
7346 same combination.
7348 Other values are reserved for future use.
7350 This variable takes no effect if the variable `window-combination-limit' is
7351 non-nil. */);
7352 Vwindow_combination_resize = Qnil;
7354 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7355 doc: /* If non-nil, splitting a window makes a new parent window.
7356 The following values are recognized:
7358 nil means splitting a window will create a new parent window only if the
7359 window has no parent window or the window shall become part of a
7360 combination orthogonal to the one it is part of.
7362 `window-size' means that splitting a window for displaying a buffer
7363 makes a new parent window provided `display-buffer' is supposed to
7364 explicitly set the window's size due to the presence of a
7365 `window-height' or `window-width' entry in the alist used by
7366 `display-buffer'. Otherwise, this value is handled like nil.
7368 `temp-buffer' means that splitting a window for displaying a temporary
7369 buffer always makes a new parent window. Otherwise, this value is
7370 handled like nil.
7372 `display-buffer' means that splitting a window for displaying a buffer
7373 always makes a new parent window. Since temporary buffers are
7374 displayed by the function `display-buffer', this value is stronger
7375 than `temp-buffer'. Splitting a window for other purpose makes a
7376 new parent window only if needed.
7378 t means that splitting a window always creates a new parent window. If
7379 all splits behave this way, each frame's window tree is a binary
7380 tree and every window but the frame's root window has exactly one
7381 sibling.
7383 Other values are reserved for future use. */);
7384 Vwindow_combination_limit = Qwindow_size;
7386 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7387 doc: /* Alist of persistent window parameters.
7388 This alist specifies which window parameters shall get saved by
7389 `current-window-configuration' and `window-state-get' and subsequently
7390 restored to their previous values by `set-window-configuration' and
7391 `window-state-put'.
7393 The car of each entry of this alist is the symbol specifying the
7394 parameter. The cdr is one of the following:
7396 nil means the parameter is neither saved by `window-state-get' nor by
7397 `current-window-configuration'.
7399 t means the parameter is saved by `current-window-configuration' and,
7400 provided its WRITABLE argument is nil, by `window-state-get'.
7402 The symbol `writable' means the parameter is saved unconditionally by
7403 both `current-window-configuration' and `window-state-get'. Do not use
7404 this value for parameters without read syntax (like windows or frames).
7406 Parameters not saved by `current-window-configuration' or
7407 `window-state-get' are left alone by `set-window-configuration'
7408 respectively are not installed by `window-state-put'. */);
7409 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7411 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7412 doc: /* Non-nil means resize windows pixelwise.
7413 This currently affects the functions: `split-window', `maximize-window',
7414 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7415 all functions that symmetrically resize a parent window.
7417 Note that when a frame's pixel size is not a multiple of the
7418 frame's character size, at least one window may get resized
7419 pixelwise even if this option is nil. */);
7420 window_resize_pixelwise = false;
7422 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7423 Vfast_but_imprecise_scrolling,
7424 doc: /* When non-nil, accelerate scrolling operations.
7425 This comes into play when scrolling rapidly over previously
7426 unfontified buffer regions. Only those portions of the buffer which
7427 are actually going to be displayed get fontified.
7429 Note that this optimization can cause the portion of the buffer
7430 displayed after a scrolling operation to be somewhat inaccurate. */);
7431 Vfast_but_imprecise_scrolling = false;
7433 DEFVAR_LISP ("window-start-group-function", Vwindow_start_group_function,
7434 doc: /* Function to call for `window-start' when its GROUP parameter is non-nil.
7435 When this variable contains a function, and `window-start' is called with a
7436 non-nil GROUP parameter, the function is called instead of `window-start''s
7437 normal action. `window-start' passes the function its argument WINDOW, which
7438 might be nil. The function may recursively call `window-start'. */);
7439 Vwindow_start_group_function = Qnil;
7440 Fmake_variable_buffer_local (Qwindow_start_group_function);
7441 Fput (Qwindow_start_group_function, Qpermanent_local, Qt);
7443 DEFVAR_LISP ("window-end-group-function", Vwindow_end_group_function,
7444 doc: /* Function to call for `window-end' if its GROUP parameter is non-nil.
7445 When this variable contains a function, and `window-end' is called with a
7446 non-nil GROUP parameter, the function is called instead of `window-end''s
7447 normal action. `window-end' passes the function its two parameters WINDOW,
7448 and UPDATE. The function may call `window-end' recursively. */);
7449 Vwindow_end_group_function = Qnil;
7450 Fmake_variable_buffer_local (Qwindow_end_group_function);
7451 Fput (Qwindow_end_group_function, Qpermanent_local, Qt);
7453 DEFVAR_LISP ("set-window-start-group-function",
7454 Vset_window_start_group_function,
7455 doc: /* The function to call for `set-window-start' when its GROUP parameter is non-nil.
7456 When this variable contains a function, and `set-window-start' is called
7457 with a non-nil GROUP parameter, the function is called instead of
7458 `set-window-start''s normal action. `set-window-start' passes the function
7459 its three parameters WINDOW, POS, and NOFORCE. The function may call
7460 `set-window-start' recursively. */);
7461 Vset_window_start_group_function = Qnil;
7462 Fmake_variable_buffer_local (Qset_window_start_group_function);
7463 Fput (Qset_window_start_group_function, Qpermanent_local, Qt);
7465 DEFVAR_LISP ("recenter-group-function", Vrecenter_group_function,
7466 doc: /* Function to call for `recenter' when its GROUP parameter is non-nil.
7467 When this variable contains a function, and `recenter' is called with a
7468 non-nil GROUP parameter, the function is called instead of `recenter''s
7469 normal action. `recenter' passes the function its parameter ARG. The
7470 function may call `recenter' recursively. */);
7471 Vrecenter_group_function = Qnil;
7472 Fmake_variable_buffer_local (Qrecenter_group_function);
7473 Fput (Qrecenter_group_function, Qpermanent_local, Qt);
7475 DEFVAR_LISP ("pos-visible-in-window-p-group-function",
7476 Vpos_visible_in_window_p_group_function,
7477 doc: /* Function for `pos-visible-in-window-p' when its GROUP parameter is non-nil.
7478 When this variable contains a function, and `pos-visible-in-window-p' is
7479 called with a non-nil GROUP parameter, the function is called instead of
7480 `pos-visible-in-window-p''s normal action. `pos-visible-in-window-p'
7481 passes the function its three parameters POS, WINDOW, and PARTIALLY. The
7482 function may call `pos-visible-in-window-p' recursively. */);
7483 Vpos_visible_in_window_p_group_function = Qnil;
7484 Fmake_variable_buffer_local (Qpos_visible_in_window_p_group_function);
7485 Fput (Qpos_visible_in_window_p_group_function, Qpermanent_local, Qt);
7487 DEFVAR_LISP ("move-to-window-line-group-function",
7488 Vmove_to_window_line_group_function,
7489 doc: /* Function for `move-to-window-line' when its GROUP parameter is non-nil.
7490 When this variable contains a function, and `move-to-window-line' is
7491 called with a non-nil GROUP parameter, the function is called instead of
7492 `move-to-window-line''s normal action. `move-to-window-line' passes the
7493 function its parameter ARG. The function may call `move-to-window-line'
7494 recursively. */);
7495 Vmove_to_window_line_group_function = Qnil;
7496 Fmake_variable_buffer_local (Qmove_to_window_line_group_function);
7497 Fput (Qmove_to_window_line_group_function, Qpermanent_local, Qt);
7499 defsubr (&Sselected_window);
7500 defsubr (&Sminibuffer_window);
7501 defsubr (&Swindow_minibuffer_p);
7502 defsubr (&Swindowp);
7503 defsubr (&Swindow_valid_p);
7504 defsubr (&Swindow_live_p);
7505 defsubr (&Swindow_frame);
7506 defsubr (&Sframe_root_window);
7507 defsubr (&Sframe_first_window);
7508 defsubr (&Sframe_selected_window);
7509 defsubr (&Sset_frame_selected_window);
7510 defsubr (&Spos_visible_in_window_p);
7511 defsubr (&Swindow_line_height);
7512 defsubr (&Swindow_buffer);
7513 defsubr (&Swindow_parent);
7514 defsubr (&Swindow_top_child);
7515 defsubr (&Swindow_left_child);
7516 defsubr (&Swindow_next_sibling);
7517 defsubr (&Swindow_prev_sibling);
7518 defsubr (&Swindow_combination_limit);
7519 defsubr (&Sset_window_combination_limit);
7520 defsubr (&Swindow_use_time);
7521 defsubr (&Swindow_pixel_width);
7522 defsubr (&Swindow_pixel_height);
7523 defsubr (&Swindow_total_width);
7524 defsubr (&Swindow_total_height);
7525 defsubr (&Swindow_normal_size);
7526 defsubr (&Swindow_new_pixel);
7527 defsubr (&Swindow_new_total);
7528 defsubr (&Swindow_new_normal);
7529 defsubr (&Swindow_pixel_left);
7530 defsubr (&Swindow_pixel_top);
7531 defsubr (&Swindow_left_column);
7532 defsubr (&Swindow_top_line);
7533 defsubr (&Sset_window_new_pixel);
7534 defsubr (&Sset_window_new_total);
7535 defsubr (&Sset_window_new_normal);
7536 defsubr (&Swindow_resize_apply);
7537 defsubr (&Swindow_resize_apply_total);
7538 defsubr (&Swindow_body_height);
7539 defsubr (&Swindow_body_width);
7540 defsubr (&Swindow_hscroll);
7541 defsubr (&Sset_window_hscroll);
7542 defsubr (&Swindow_redisplay_end_trigger);
7543 defsubr (&Sset_window_redisplay_end_trigger);
7544 defsubr (&Swindow_mode_line_height);
7545 defsubr (&Swindow_header_line_height);
7546 defsubr (&Swindow_right_divider_width);
7547 defsubr (&Swindow_bottom_divider_width);
7548 defsubr (&Swindow_scroll_bar_width);
7549 defsubr (&Swindow_scroll_bar_height);
7550 defsubr (&Scoordinates_in_window_p);
7551 defsubr (&Swindow_at);
7552 defsubr (&Swindow_point);
7553 defsubr (&Swindow_old_point);
7554 defsubr (&Swindow_start);
7555 defsubr (&Swindow_end);
7556 defsubr (&Sset_window_point);
7557 defsubr (&Sset_window_start);
7558 defsubr (&Swindow_dedicated_p);
7559 defsubr (&Sset_window_dedicated_p);
7560 defsubr (&Swindow_display_table);
7561 defsubr (&Sset_window_display_table);
7562 defsubr (&Snext_window);
7563 defsubr (&Sprevious_window);
7564 defsubr (&Swindow__sanitize_window_sizes);
7565 defsubr (&Sget_buffer_window);
7566 defsubr (&Sdelete_other_windows_internal);
7567 defsubr (&Sdelete_window_internal);
7568 defsubr (&Sresize_mini_window_internal);
7569 defsubr (&Sset_window_buffer);
7570 defsubr (&Srun_window_configuration_change_hook);
7571 defsubr (&Srun_window_scroll_functions);
7572 defsubr (&Sselect_window);
7573 defsubr (&Sforce_window_update);
7574 defsubr (&Ssplit_window_internal);
7575 defsubr (&Sscroll_up);
7576 defsubr (&Sscroll_down);
7577 defsubr (&Sscroll_left);
7578 defsubr (&Sscroll_right);
7579 defsubr (&Sother_window_for_scrolling);
7580 defsubr (&Sscroll_other_window);
7581 defsubr (&Sminibuffer_selected_window);
7582 defsubr (&Srecenter);
7583 defsubr (&Swindow_text_width);
7584 defsubr (&Swindow_text_height);
7585 defsubr (&Smove_to_window_line);
7586 defsubr (&Swindow_configuration_p);
7587 defsubr (&Swindow_configuration_frame);
7588 defsubr (&Sset_window_configuration);
7589 defsubr (&Scurrent_window_configuration);
7590 defsubr (&Sset_window_margins);
7591 defsubr (&Swindow_margins);
7592 defsubr (&Sset_window_fringes);
7593 defsubr (&Swindow_fringes);
7594 defsubr (&Sset_window_scroll_bars);
7595 defsubr (&Swindow_scroll_bars);
7596 defsubr (&Swindow_vscroll);
7597 defsubr (&Sset_window_vscroll);
7598 defsubr (&Scompare_window_configurations);
7599 defsubr (&Swindow_list);
7600 defsubr (&Swindow_list_1);
7601 defsubr (&Swindow_prev_buffers);
7602 defsubr (&Sset_window_prev_buffers);
7603 defsubr (&Swindow_next_buffers);
7604 defsubr (&Sset_window_next_buffers);
7605 defsubr (&Swindow_parameters);
7606 defsubr (&Swindow_parameter);
7607 defsubr (&Sset_window_parameter);
7610 void
7611 keys_of_window (void)
7613 initial_define_key (control_x_map, '<', "scroll-left");
7614 initial_define_key (control_x_map, '>', "scroll-right");
7616 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7617 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7618 initial_define_key (meta_map, 'v', "scroll-down-command");