* lisp/progmodes/xref.el (xref-find-apropos): Use read-string.
[emacs.git] / src / window.c
blobb8281661b4d1277e97899abec972c1c473ff37a1
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 "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "menu.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "indent.h"
35 #include "termchar.h"
36 #include "disptab.h"
37 #include "dispextern.h"
38 #include "blockinput.h"
39 #include "intervals.h"
40 #include "termhooks.h" /* For FRAME_TERMINAL. */
41 #ifdef HAVE_WINDOW_SYSTEM
42 #include TERM_HEADER
43 #endif /* HAVE_WINDOW_SYSTEM */
44 #ifdef MSDOS
45 #include "msdos.h"
46 #endif
48 static ptrdiff_t count_windows (struct window *);
49 static ptrdiff_t get_leaf_windows (struct window *, struct window **,
50 ptrdiff_t);
51 static void window_scroll_pixel_based (Lisp_Object, int, bool, bool);
52 static void window_scroll_line_based (Lisp_Object, int, bool, bool);
53 static void foreach_window (struct frame *,
54 bool (* fn) (struct window *, void *),
55 void *);
56 static bool foreach_window_1 (struct window *,
57 bool (* fn) (struct window *, void *),
58 void *);
59 static bool window_resize_check (struct window *, bool);
60 static void window_resize_apply (struct window *, bool);
61 static void select_window_1 (Lisp_Object, bool);
63 static struct window *set_window_fringes (struct window *, Lisp_Object,
64 Lisp_Object, Lisp_Object);
65 static struct window *set_window_margins (struct window *, Lisp_Object,
66 Lisp_Object);
67 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
68 Lisp_Object, Lisp_Object,
69 Lisp_Object);
70 static void apply_window_adjustment (struct window *);
72 /* This is the window in which the terminal's cursor should
73 be left when nothing is being done with it. This must
74 always be a leaf window, and its buffer is selected by
75 the top level editing loop at the end of each command.
77 This value is always the same as
78 FRAME_SELECTED_WINDOW (selected_frame). */
79 Lisp_Object selected_window;
81 /* A list of all windows for use by next_window and Fwindow_list.
82 Functions creating or deleting windows should invalidate this cache
83 by setting it to nil. */
84 Lisp_Object Vwindow_list;
86 /* The mini-buffer window of the selected frame.
87 Note that you cannot test for mini-bufferness of an arbitrary window
88 by comparing against this; but you can test for mini-bufferness of
89 the selected window. */
90 Lisp_Object minibuf_window;
92 /* Non-nil means it is the window whose mode line should be
93 shown as the selected window when the minibuffer is selected. */
94 Lisp_Object minibuf_selected_window;
96 /* Incremented for each window created. */
97 static EMACS_INT sequence_number;
99 /* Used by the function window_scroll_pixel_based. */
100 static int window_scroll_pixel_based_preserve_x;
101 static int window_scroll_pixel_based_preserve_y;
103 /* Same for window_scroll_line_based. */
104 static EMACS_INT window_scroll_preserve_hpos;
105 static EMACS_INT window_scroll_preserve_vpos;
107 static void
108 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
110 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
113 /* These setters are used only in this file, so they can be private. */
114 static void
115 wset_combination_limit (struct window *w, Lisp_Object val)
117 w->combination_limit = val;
120 static void
121 wset_dedicated (struct window *w, Lisp_Object val)
123 w->dedicated = val;
126 static void
127 wset_display_table (struct window *w, Lisp_Object val)
129 w->display_table = val;
132 static void
133 wset_new_normal (struct window *w, Lisp_Object val)
135 w->new_normal = val;
138 static void
139 wset_new_total (struct window *w, Lisp_Object val)
141 w->new_total = val;
144 static void
145 wset_normal_cols (struct window *w, Lisp_Object val)
147 w->normal_cols = val;
150 static void
151 wset_normal_lines (struct window *w, Lisp_Object val)
153 w->normal_lines = val;
156 static void
157 wset_parent (struct window *w, Lisp_Object val)
159 w->parent = val;
162 static void
163 wset_pointm (struct window *w, Lisp_Object val)
165 w->pointm = val;
168 static void
169 wset_old_pointm (struct window *w, Lisp_Object val)
171 w->old_pointm = val;
174 static void
175 wset_start (struct window *w, Lisp_Object val)
177 w->start = val;
180 static void
181 wset_temslot (struct window *w, Lisp_Object val)
183 w->temslot = val;
186 static void
187 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
189 w->vertical_scroll_bar_type = val;
192 static void
193 wset_window_parameters (struct window *w, Lisp_Object val)
195 w->window_parameters = val;
198 static void
199 wset_combination (struct window *w, bool horflag, Lisp_Object val)
201 /* Since leaf windows never becomes non-leaf, there should
202 be no buffer and markers in start and pointm fields of W. */
203 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
204 w->contents = val;
205 /* When an internal window is deleted and VAL is nil, HORFLAG
206 is meaningless. */
207 if (!NILP (val))
208 w->horizontal = horflag;
211 /* True if leaf window W doesn't reflect the actual state
212 of displayed buffer due to its text or overlays change. */
214 bool
215 window_outdated (struct window *w)
217 struct buffer *b = XBUFFER (w->contents);
218 return (w->last_modified < BUF_MODIFF (b)
219 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
222 struct window *
223 decode_live_window (register Lisp_Object window)
225 if (NILP (window))
226 return XWINDOW (selected_window);
228 CHECK_LIVE_WINDOW (window);
229 return XWINDOW (window);
232 struct window *
233 decode_any_window (register Lisp_Object window)
235 struct window *w;
237 if (NILP (window))
238 return XWINDOW (selected_window);
240 CHECK_WINDOW (window);
241 w = XWINDOW (window);
242 return w;
245 static struct window *
246 decode_valid_window (register Lisp_Object window)
248 struct window *w;
250 if (NILP (window))
251 return XWINDOW (selected_window);
253 CHECK_VALID_WINDOW (window);
254 w = XWINDOW (window);
255 return w;
258 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
259 cease its buffer, and 1 means that W is about to set up the new one. */
261 static void
262 adjust_window_count (struct window *w, int arg)
264 eassert (eabs (arg) == 1);
265 if (BUFFERP (w->contents))
267 struct buffer *b = XBUFFER (w->contents);
269 if (b->base_buffer)
270 b = b->base_buffer;
271 b->window_count += arg;
272 eassert (b->window_count >= 0);
273 /* These should be recalculated by redisplay code. */
274 w->window_end_valid = false;
275 w->base_line_pos = 0;
279 /* Set W's buffer slot to VAL and recompute number
280 of windows showing VAL if it is a buffer. */
282 void
283 wset_buffer (struct window *w, Lisp_Object val)
285 adjust_window_count (w, -1);
286 if (BUFFERP (val))
287 /* Make sure that we do not assign the buffer
288 to an internal window. */
289 eassert (MARKERP (w->start) && MARKERP (w->pointm));
290 w->contents = val;
291 adjust_window_count (w, 1);
294 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
295 doc: /* Return t if OBJECT is a window and nil otherwise. */)
296 (Lisp_Object object)
298 return WINDOWP (object) ? Qt : Qnil;
301 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
302 doc: /* Return t if OBJECT is a valid window and nil otherwise.
303 A valid window is either a window that displays a buffer or an internal
304 window. Windows that have been deleted are not valid. */)
305 (Lisp_Object object)
307 return WINDOW_VALID_P (object) ? Qt : Qnil;
310 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
311 doc: /* Return t if OBJECT is a live window and nil otherwise.
312 A live window is a window that displays a buffer.
313 Internal windows and deleted windows are not live. */)
314 (Lisp_Object object)
316 return WINDOW_LIVE_P (object) ? Qt : Qnil;
319 /* Frames and windows. */
320 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
321 doc: /* Return the frame that window WINDOW is on.
322 WINDOW must be a valid window and defaults to the selected one. */)
323 (Lisp_Object window)
325 return decode_valid_window (window)->frame;
328 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
329 doc: /* Return the root window of FRAME-OR-WINDOW.
330 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
331 With a frame argument, return that frame's root window.
332 With a window argument, return the root window of that window's frame. */)
333 (Lisp_Object frame_or_window)
335 Lisp_Object window;
337 if (NILP (frame_or_window))
338 window = SELECTED_FRAME ()->root_window;
339 else if (WINDOW_VALID_P (frame_or_window))
340 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
341 else
343 CHECK_LIVE_FRAME (frame_or_window);
344 window = XFRAME (frame_or_window)->root_window;
347 return window;
350 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
351 doc: /* Return the minibuffer window for frame FRAME.
352 If FRAME is omitted or nil, it defaults to the selected frame. */)
353 (Lisp_Object frame)
355 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
358 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
359 Swindow_minibuffer_p, 0, 1, 0,
360 doc: /* Return non-nil if WINDOW is a minibuffer window.
361 WINDOW must be a valid window and defaults to the selected one. */)
362 (Lisp_Object window)
364 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
367 /* Don't move this to window.el - this must be a safe routine. */
368 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
369 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
370 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
371 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
372 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
373 the first window of that frame. */)
374 (Lisp_Object frame_or_window)
376 Lisp_Object window;
378 if (NILP (frame_or_window))
379 window = SELECTED_FRAME ()->root_window;
380 else if (WINDOW_VALID_P (frame_or_window))
381 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
382 else
384 CHECK_LIVE_FRAME (frame_or_window);
385 window = XFRAME (frame_or_window)->root_window;
388 while (WINDOWP (XWINDOW (window)->contents))
389 window = XWINDOW (window)->contents;
391 return window;
394 DEFUN ("frame-selected-window", Fframe_selected_window,
395 Sframe_selected_window, 0, 1, 0,
396 doc: /* Return the selected window of FRAME-OR-WINDOW.
397 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
398 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
399 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
400 return the selected window of that frame. */)
401 (Lisp_Object frame_or_window)
403 Lisp_Object window;
405 if (NILP (frame_or_window))
406 window = SELECTED_FRAME ()->selected_window;
407 else if (WINDOW_VALID_P (frame_or_window))
408 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
409 else
411 CHECK_LIVE_FRAME (frame_or_window);
412 window = XFRAME (frame_or_window)->selected_window;
415 return window;
418 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
419 Sset_frame_selected_window, 2, 3, 0,
420 doc: /* Set selected window of FRAME to WINDOW.
421 FRAME must be a live frame and defaults to the selected one. If FRAME
422 is the selected frame, this makes WINDOW the selected window. Optional
423 argument NORECORD non-nil means to neither change the order of recently
424 selected windows nor the buffer list. WINDOW must denote a live window.
425 Return WINDOW. */)
426 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
428 if (NILP (frame))
429 frame = selected_frame;
431 CHECK_LIVE_FRAME (frame);
432 CHECK_LIVE_WINDOW (window);
434 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
435 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
437 if (EQ (frame, selected_frame))
438 return Fselect_window (window, norecord);
439 else
441 fset_selected_window (XFRAME (frame), window);
442 return window;
446 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
447 doc: /* Return the selected window.
448 The selected window is the window in which the standard cursor for
449 selected windows appears and to which many commands apply. */)
450 (void)
452 return selected_window;
455 EMACS_INT window_select_count;
457 /* If select_window is called with inhibit_point_swap true it will
458 not store point of the old selected window's buffer back into that
459 window's pointm slot. This is needed by Fset_window_configuration to
460 avoid that the display routine is called with selected_window set to
461 Qnil causing a subsequent crash. */
462 static Lisp_Object
463 select_window (Lisp_Object window, Lisp_Object norecord,
464 bool inhibit_point_swap)
466 struct window *w;
467 struct frame *sf;
469 CHECK_LIVE_WINDOW (window);
471 w = XWINDOW (window);
473 /* Make the selected window's buffer current. */
474 Fset_buffer (w->contents);
476 if (EQ (window, selected_window) && !inhibit_point_swap)
477 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
478 way to call record_buffer from Elisp, so it's important that we call
479 record_buffer before returning here. */
480 goto record_and_return;
482 if (NILP (norecord))
483 { /* Mark the window for redisplay since the selected-window has
484 a different mode-line. */
485 wset_redisplay (XWINDOW (selected_window));
486 wset_redisplay (w);
488 else
489 redisplay_other_windows ();
491 sf = SELECTED_FRAME ();
492 if (XFRAME (WINDOW_FRAME (w)) != sf)
494 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
495 /* Use this rather than Fhandle_switch_frame
496 so that FRAME_FOCUS_FRAME is moved appropriately as we
497 move around in the state where a minibuffer in a separate
498 frame is active. */
499 Fselect_frame (WINDOW_FRAME (w), norecord);
500 /* Fselect_frame called us back so we've done all the work already. */
501 eassert (EQ (window, selected_window));
502 return window;
504 else
505 fset_selected_window (sf, window);
507 select_window_1 (window, inhibit_point_swap);
508 bset_last_selected_window (XBUFFER (w->contents), window);
510 record_and_return:
511 /* record_buffer can run QUIT, so make sure it is run only after we have
512 re-established the invariant between selected_window and selected_frame,
513 otherwise the temporary broken invariant might "escape" (bug#14161). */
514 if (NILP (norecord))
516 w->use_time = ++window_select_count;
517 record_buffer (w->contents);
520 return window;
523 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
524 (not even for redisplay's benefit), and assume that the window's frame is
525 already selected. */
526 static void
527 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
529 /* Store the old selected window's buffer's point in pointm of the old
530 selected window. It belongs to that window, and when the window is
531 not selected, must be in the window. */
532 if (!inhibit_point_swap)
534 struct window *ow = XWINDOW (selected_window);
535 if (BUFFERP (ow->contents))
536 set_marker_both (ow->pointm, ow->contents,
537 BUF_PT (XBUFFER (ow->contents)),
538 BUF_PT_BYTE (XBUFFER (ow->contents)));
541 selected_window = window;
543 /* Go to the point recorded in the window.
544 This is important when the buffer is in more
545 than one window. It also matters when
546 redisplay_window has altered point after scrolling,
547 because it makes the change only in the window. */
548 set_point_from_marker (XWINDOW (window)->pointm);
551 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
552 doc: /* Select WINDOW which must be a live window.
553 Also make WINDOW's frame the selected frame and WINDOW that frame's
554 selected window. In addition, make WINDOW's buffer current and set its
555 buffer's value of `point' to the value of WINDOW's `window-point'.
556 Return WINDOW.
558 Optional second arg NORECORD non-nil means do not put this buffer at the
559 front of the buffer list and do not make this window the most recently
560 selected one.
562 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
563 applications and internal routines often select a window temporarily for
564 various purposes; mostly, to simplify coding. As a rule, such
565 selections should be not recorded and therefore will not pollute
566 `buffer-list-update-hook'. Selections that "really count" are those
567 causing a visible change in the next redisplay of WINDOW's frame and
568 should be always recorded. So if you think of running a function each
569 time a window gets selected put it on `buffer-list-update-hook'.
571 Also note that the main editor command loop sets the current buffer to
572 the buffer of the selected window before each command. */)
573 (Lisp_Object window, Lisp_Object norecord)
575 return select_window (window, norecord, false);
578 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
579 doc: /* Return the buffer displayed in window WINDOW.
580 If WINDOW is omitted or nil, it defaults to the selected window.
581 Return nil for an internal window or a deleted window. */)
582 (Lisp_Object window)
584 struct window *w = decode_any_window (window);
585 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
588 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
589 doc: /* Return the parent window of window WINDOW.
590 WINDOW must be a valid window and defaults to the selected one.
591 Return nil for a window with no parent (e.g. a root window). */)
592 (Lisp_Object window)
594 return decode_valid_window (window)->parent;
597 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
598 doc: /* Return the topmost child window of window WINDOW.
599 WINDOW must be a valid window and defaults to the selected one.
600 Return nil if WINDOW is a live window (live windows have no children).
601 Return nil if WINDOW is an internal window whose children form a
602 horizontal combination. */)
603 (Lisp_Object window)
605 struct window *w = decode_valid_window (window);
606 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
609 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
610 doc: /* Return the leftmost child window of window WINDOW.
611 WINDOW must be a valid window and defaults to the selected one.
612 Return nil if WINDOW is a live window (live windows have no children).
613 Return nil if WINDOW is an internal window whose children form a
614 vertical combination. */)
615 (Lisp_Object window)
617 struct window *w = decode_valid_window (window);
618 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
621 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
622 doc: /* Return the next sibling window of window WINDOW.
623 WINDOW must be a valid window and defaults to the selected one.
624 Return nil if WINDOW has no next sibling. */)
625 (Lisp_Object window)
627 return decode_valid_window (window)->next;
630 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
631 doc: /* Return the previous sibling window of window WINDOW.
632 WINDOW must be a valid window and defaults to the selected one.
633 Return nil if WINDOW has no previous sibling. */)
634 (Lisp_Object window)
636 return decode_valid_window (window)->prev;
639 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
640 doc: /* Return combination limit of window WINDOW.
641 WINDOW must be a valid window used in horizontal or vertical combination.
642 If the return value is nil, child windows of WINDOW can be recombined with
643 WINDOW's siblings. A return value of t means that child windows of
644 WINDOW are never \(re-)combined with WINDOW's siblings. */)
645 (Lisp_Object window)
647 struct window *w;
649 CHECK_VALID_WINDOW (window);
650 w = XWINDOW (window);
651 if (WINDOW_LEAF_P (w))
652 error ("Combination limit is meaningful for internal windows only");
653 return w->combination_limit;
656 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
657 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
658 WINDOW must be a valid window used in horizontal or vertical combination.
659 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
660 siblings. LIMIT t means that child windows of WINDOW are never
661 \(re-)combined with WINDOW's siblings. Other values are reserved for
662 future use. */)
663 (Lisp_Object window, Lisp_Object limit)
665 struct window *w;
667 CHECK_VALID_WINDOW (window);
668 w = XWINDOW (window);
669 if (WINDOW_LEAF_P (w))
670 error ("Combination limit is meaningful for internal windows only");
671 wset_combination_limit (w, limit);
672 return limit;
675 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
676 doc: /* Return the use time of window WINDOW.
677 WINDOW must be a live window and defaults to the selected one.
678 The window with the highest use time is the most recently selected
679 one. The window with the lowest use time is the least recently
680 selected one. */)
681 (Lisp_Object window)
683 return make_number (decode_live_window (window)->use_time);
686 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
687 doc: /* Return the width of window WINDOW in pixels.
688 WINDOW must be a valid window and defaults to the selected one.
690 The return value includes the fringes and margins of WINDOW as well as
691 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
692 an internal window, its pixel width is the width of the screen areas
693 spanned by its children. */)
694 (Lisp_Object window)
696 return make_number (decode_valid_window (window)->pixel_width);
699 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
700 doc: /* Return the height of window WINDOW in pixels.
701 WINDOW must be a valid window and defaults to the selected one.
703 The return value includes the mode line and header line and the bottom
704 divider, if any. If WINDOW is an internal window, its pixel height is
705 the height of the screen areas spanned by its children. */)
706 (Lisp_Object window)
708 return make_number (decode_valid_window (window)->pixel_height);
711 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
712 doc: /* Return the height of window WINDOW in lines.
713 WINDOW must be a valid window and defaults to the selected one.
715 The return value includes the heights of WINDOW's mode and header line
716 and its bottom divider, if any. If WINDOW is an internal window, the
717 total height is the height of the screen areas spanned by its children.
719 If WINDOW's pixel height is not an integral multiple of its frame's
720 character height, the number of lines occupied by WINDOW is rounded
721 internally. This is done in a way such that, if WINDOW is a parent
722 window, the sum of the total heights of all its children internally
723 equals the total height of WINDOW.
725 If the optional argument ROUND is `ceiling', return the smallest integer
726 larger than WINDOW's pixel height divided by the character height of
727 WINDOW's frame. ROUND `floor' means to return the largest integer
728 smaller than WINDOW's pixel height divided by the character height of
729 WINDOW's frame. Any other value of ROUND means to return the internal
730 total height of WINDOW. */)
731 (Lisp_Object window, Lisp_Object round)
733 struct window *w = decode_valid_window (window);
735 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
736 return make_number (w->total_lines);
737 else
739 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
741 return make_number (EQ (round, Qceiling)
742 ? ((w->pixel_height + unit - 1) /unit)
743 : (w->pixel_height / unit));
747 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
748 doc: /* Return the total width of window WINDOW in columns.
749 WINDOW must be a valid window and defaults to the selected one.
751 The return value includes the widths of WINDOW's fringes, margins,
752 scroll bars and its right divider, if any. If WINDOW is an internal
753 window, the total width is the width of the screen areas spanned by its
754 children.
756 If WINDOW's pixel width is not an integral multiple of its frame's
757 character width, the number of lines occupied by WINDOW is rounded
758 internally. This is done in a way such that, if WINDOW is a parent
759 window, the sum of the total widths of all its children internally
760 equals the total width of WINDOW.
762 If the optional argument ROUND is `ceiling', return the smallest integer
763 larger than WINDOW's pixel width divided by the character width of
764 WINDOW's frame. ROUND `floor' means to return the largest integer
765 smaller than WINDOW's pixel width divided by the character width of
766 WINDOW's frame. Any other value of ROUND means to return the internal
767 total width of WINDOW. */)
768 (Lisp_Object window, Lisp_Object round)
770 struct window *w = decode_valid_window (window);
772 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
773 return make_number (w->total_cols);
774 else
776 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
778 return make_number (EQ (round, Qceiling)
779 ? ((w->pixel_width + unit - 1) /unit)
780 : (w->pixel_width / unit));
784 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
785 doc: /* Return the new total size of window WINDOW.
786 WINDOW must be a valid window and defaults to the selected one.
788 The new total size of WINDOW is the value set by the last call of
789 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
790 installed as WINDOW's total height (see `window-total-height') or total
791 width (see `window-total-width'). */)
792 (Lisp_Object window)
794 return decode_valid_window (window)->new_total;
797 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
798 doc: /* Return the normal height of window WINDOW.
799 WINDOW must be a valid window and defaults to the selected one.
800 If HORIZONTAL is non-nil, return the normal width of WINDOW.
802 The normal height of a frame's root window or a window that is
803 horizontally combined (a window that has a left or right sibling) is
804 1.0. The normal height of a window that is vertically combined (has a
805 sibling above or below) is the fraction of the window's height with
806 respect to its parent. The sum of the normal heights of all windows in a
807 vertical combination equals 1.0.
809 Similarly, the normal width of a frame's root window or a window that is
810 vertically combined equals 1.0. The normal width of a window that is
811 horizontally combined is the fraction of the window's width with respect
812 to its parent. The sum of the normal widths of all windows in a
813 horizontal combination equals 1.0.
815 The normal sizes of windows are used to restore the proportional sizes
816 of windows after they have been shrunk to their minimum sizes; for
817 example when a frame is temporarily made very small and afterwards gets
818 re-enlarged to its previous size. */)
819 (Lisp_Object window, Lisp_Object horizontal)
821 struct window *w = decode_valid_window (window);
823 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
826 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
827 doc: /* Return new normal size of window WINDOW.
828 WINDOW must be a valid window and defaults to the selected one.
830 The new normal size of WINDOW is the value set by the last call of
831 `set-window-new-normal' for WINDOW. If valid, it will be shortly
832 installed as WINDOW's normal size (see `window-normal-size'). */)
833 (Lisp_Object window)
835 return decode_valid_window (window)->new_normal;
838 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
839 doc: /* Return new pixel size of window WINDOW.
840 WINDOW must be a valid window and defaults to the selected one.
842 The new pixel size of WINDOW is the value set by the last call of
843 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
844 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
845 width (see `window-pixel-width'). */)
846 (Lisp_Object window)
848 return decode_valid_window (window)->new_pixel;
851 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
852 doc: /* Return left pixel edge of window WINDOW.
853 WINDOW must be a valid window and defaults to the selected one. */)
854 (Lisp_Object window)
856 return make_number (decode_valid_window (window)->pixel_left);
859 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
860 doc: /* Return top pixel edge of window WINDOW.
861 WINDOW must be a valid window and defaults to the selected one. */)
862 (Lisp_Object window)
864 return make_number (decode_valid_window (window)->pixel_top);
867 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
868 doc: /* Return left column of window WINDOW.
869 This is the distance, in columns, between the left edge of WINDOW and
870 the left edge of the frame's window area. For instance, the return
871 value is 0 if there is no window to the left of WINDOW.
873 WINDOW must be a valid window and defaults to the selected one. */)
874 (Lisp_Object window)
876 return make_number (decode_valid_window (window)->left_col);
879 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
880 doc: /* Return top line of window WINDOW.
881 This is the distance, in lines, between the top of WINDOW and the top
882 of the frame's window area. For instance, the return value is 0 if
883 there is no window above WINDOW.
885 WINDOW must be a valid window and defaults to the selected one. */)
886 (Lisp_Object window)
888 return make_number (decode_valid_window (window)->top_line);
891 /* Return the number of lines/pixels of W's body. Don't count any mode
892 or header line or horizontal divider of W. Rounds down to nearest
893 integer when not working pixelwise. */
894 static int
895 window_body_height (struct window *w, bool pixelwise)
897 int height = (w->pixel_height
898 - WINDOW_HEADER_LINE_HEIGHT (w)
899 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
900 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
901 : 0)
902 - WINDOW_MODE_LINE_HEIGHT (w)
903 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
905 /* Don't return a negative value. */
906 return max (pixelwise
907 ? height
908 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
912 /* Return the number of columns/pixels of W's body. Don't count columns
913 occupied by the scroll bar or the divider/vertical bar separating W
914 from its right sibling or margins. On window-systems don't count
915 fringes either. Round down to nearest integer when not working
916 pixelwise. */
918 window_body_width (struct window *w, bool pixelwise)
920 struct frame *f = XFRAME (WINDOW_FRAME (w));
922 int width = (w->pixel_width
923 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
924 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
925 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
926 : (/* A vertical bar is either 1 or 0. */
927 !FRAME_WINDOW_P (f)
928 && !WINDOW_RIGHTMOST_P (w)
929 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)))
930 - WINDOW_MARGINS_WIDTH (w)
931 - (FRAME_WINDOW_P (f)
932 ? WINDOW_FRINGES_WIDTH (w)
933 : 0));
935 /* Don't return a negative value. */
936 return max (pixelwise
937 ? width
938 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
942 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
943 doc: /* Return the height of WINDOW's text area.
944 WINDOW must be a live window and defaults to the selected one. Optional
945 argument PIXELWISE non-nil means return the height of WINDOW's text area
946 in pixels. The return value does not include the mode line or header
947 line or any horizontal divider.
949 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
950 pixel height divided by the character height of WINDOW's frame. This
951 means that if a line at the bottom of the text area is only partially
952 visible, that line is not counted. */)
953 (Lisp_Object window, Lisp_Object pixelwise)
955 return make_number (window_body_height (decode_live_window (window),
956 !NILP (pixelwise)));
959 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
960 doc: /* Return the width of WINDOW's text area.
961 WINDOW must be a live window and defaults to the selected one. Optional
962 argument PIXELWISE non-nil means return the width in pixels. The return
963 value does not include any vertical dividers, fringes or marginal areas,
964 or scroll bars.
966 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
967 pixel width divided by the character width of WINDOW's frame. This
968 means that if a column at the right of the text area is only partially
969 visible, that column is not counted.
971 Note that the returned value includes the column reserved for the
972 continuation glyph. */)
973 (Lisp_Object window, Lisp_Object pixelwise)
975 return make_number (window_body_width (decode_live_window (window),
976 !NILP (pixelwise)));
979 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
980 Swindow_mode_line_height, 0, 1, 0,
981 doc: /* Return the height in pixels of WINDOW's mode-line.
982 WINDOW must be a live window and defaults to the selected one. */)
983 (Lisp_Object window)
985 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
988 DEFUN ("window-header-line-height", Fwindow_header_line_height,
989 Swindow_header_line_height, 0, 1, 0,
990 doc: /* Return the height in pixels of WINDOW's header-line.
991 WINDOW must be a live window and defaults to the selected one. */)
992 (Lisp_Object window)
994 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
997 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
998 Swindow_right_divider_width, 0, 1, 0,
999 doc: /* Return the width in pixels of WINDOW's right divider.
1000 WINDOW must be a live window and defaults to the selected one. */)
1001 (Lisp_Object window)
1003 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1006 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1007 Swindow_bottom_divider_width, 0, 1, 0,
1008 doc: /* Return the width in pixels of WINDOW's bottom divider.
1009 WINDOW must be a live window and defaults to the selected one. */)
1010 (Lisp_Object window)
1012 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1015 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1016 Swindow_scroll_bar_width, 0, 1, 0,
1017 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1018 WINDOW must be a live window and defaults to the selected one. */)
1019 (Lisp_Object window)
1021 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1024 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1025 Swindow_scroll_bar_height, 0, 1, 0,
1026 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1027 WINDOW must be a live window and defaults to the selected one. */)
1028 (Lisp_Object window)
1030 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1033 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1034 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1035 WINDOW must be a live window and defaults to the selected one. */)
1036 (Lisp_Object window)
1038 return make_number (decode_live_window (window)->hscroll);
1041 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1042 range, returning the new amount as a fixnum. */
1043 static Lisp_Object
1044 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1046 /* Horizontal scrolling has problems with large scroll amounts.
1047 It's too slow with long lines, and even with small lines the
1048 display can be messed up. For now, though, impose only the limits
1049 required by the internal representation: horizontal scrolling must
1050 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1051 (since it's stored in a ptrdiff_t). */
1052 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1053 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1055 /* Prevent redisplay shortcuts when changing the hscroll. */
1056 if (w->hscroll != new_hscroll)
1057 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
1059 w->hscroll = new_hscroll;
1060 w->suspend_auto_hscroll = true;
1062 return make_number (new_hscroll);
1065 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1066 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1067 WINDOW must be a live window and defaults to the selected one.
1068 Clip the number to a reasonable value if out of range.
1069 Return the new number. NCOL should be zero or positive.
1071 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1072 window so that the location of point moves off-window. */)
1073 (Lisp_Object window, Lisp_Object ncol)
1075 CHECK_NUMBER (ncol);
1076 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1079 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1080 Swindow_redisplay_end_trigger, 0, 1, 0,
1081 doc: /* Return WINDOW's redisplay end trigger value.
1082 WINDOW must be a live window and defaults to the selected one.
1083 See `set-window-redisplay-end-trigger' for more information. */)
1084 (Lisp_Object window)
1086 return decode_live_window (window)->redisplay_end_trigger;
1089 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1090 Sset_window_redisplay_end_trigger, 2, 2, 0,
1091 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1092 WINDOW must be a live window and defaults to the selected one. VALUE
1093 should be a buffer position (typically a marker) or nil. If it is a
1094 buffer position, then if redisplay in WINDOW reaches a position beyond
1095 VALUE, the functions in `redisplay-end-trigger-functions' are called
1096 with two arguments: WINDOW, and the end trigger value. Afterwards the
1097 end-trigger value is reset to nil. */)
1098 (register Lisp_Object window, Lisp_Object value)
1100 wset_redisplay_end_trigger (decode_live_window (window), value);
1101 return value;
1104 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1105 doc: /* Return a list of the edge coordinates of WINDOW.
1106 WINDOW must be a valid window and defaults to the selected one.
1108 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1109 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1110 0 at top left corner of frame.
1112 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1113 is one more than the bottommost row occupied by WINDOW. The edges
1114 include the space used by WINDOW's scroll bar, display margins, fringes,
1115 header line, and/or mode line. For the edges of just the text area, use
1116 `window-inside-edges'. */)
1117 (Lisp_Object window)
1119 register struct window *w = decode_valid_window (window);
1121 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1122 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1125 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1126 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1127 WINDOW must be a valid window and defaults to the selected one.
1129 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1130 0, 0 at the top left corner of the frame.
1132 RIGHT is one more than the rightmost x position occupied by WINDOW.
1133 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1134 The pixel edges include the space used by WINDOW's scroll bar, display
1135 margins, fringes, header line, and/or mode line. For the pixel edges
1136 of just the text area, use `window-inside-pixel-edges'. */)
1137 (Lisp_Object window)
1139 register struct window *w = decode_valid_window (window);
1141 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1142 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1145 static void
1146 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1148 struct frame *f = XFRAME (w->frame);
1149 *add_y = f->top_pos;
1150 #ifdef FRAME_MENUBAR_HEIGHT
1151 *add_y += FRAME_MENUBAR_HEIGHT (f);
1152 #endif
1153 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1154 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1155 #elif defined (FRAME_TOOLBAR_HEIGHT)
1156 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1157 #endif
1158 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1159 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1160 #endif
1161 *add_x = f->left_pos;
1162 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1163 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1164 #endif
1167 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1168 Swindow_absolute_pixel_edges, 0, 1, 0,
1169 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1170 WINDOW must be a valid window and defaults to the selected one.
1172 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1173 0, 0 at the top left corner of the display.
1175 RIGHT is one more than the rightmost x position occupied by WINDOW.
1176 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1177 The pixel edges include the space used by WINDOW's scroll bar, display
1178 margins, fringes, header line, and/or mode line. For the pixel edges
1179 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1180 (Lisp_Object window)
1182 register struct window *w = decode_valid_window (window);
1183 int add_x, add_y;
1185 calc_absolute_offset (w, &add_x, &add_y);
1187 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1188 WINDOW_TOP_EDGE_Y (w) + add_y,
1189 WINDOW_RIGHT_EDGE_X (w) + add_x,
1190 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1193 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1194 doc: /* Return a list of the edge coordinates of WINDOW.
1195 WINDOW must be a live window and defaults to the selected one.
1197 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1198 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1199 0 at top left corner of frame.
1201 RIGHT is one more than the rightmost column of WINDOW's text area.
1202 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1203 inside edges do not include the space used by the WINDOW's scroll bar,
1204 display margins, fringes, header line, and/or mode line. */)
1205 (Lisp_Object window)
1207 register struct window *w = decode_live_window (window);
1209 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1210 + WINDOW_LEFT_MARGIN_COLS (w)
1211 + ((WINDOW_LEFT_FRINGE_WIDTH (w)
1212 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1213 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1214 (WINDOW_TOP_EDGE_LINE (w)
1215 + WINDOW_HEADER_LINE_LINES (w)),
1216 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1217 - WINDOW_RIGHT_MARGIN_COLS (w)
1218 - ((WINDOW_RIGHT_FRINGE_WIDTH (w)
1219 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1220 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1221 (WINDOW_BOTTOM_EDGE_LINE (w)
1222 - WINDOW_MODE_LINE_LINES (w)));
1225 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1226 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1227 WINDOW must be a live window and defaults to the selected one.
1229 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1230 (0,0) at the top left corner of the frame's window area.
1232 RIGHT is one more than the rightmost x position of WINDOW's text area.
1233 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1234 The inside edges do not include the space used by WINDOW's scroll bar,
1235 display margins, fringes, header line, and/or mode line. */)
1236 (Lisp_Object window)
1238 register struct window *w = decode_live_window (window);
1240 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1241 + WINDOW_LEFT_MARGIN_WIDTH (w)
1242 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1243 (WINDOW_TOP_EDGE_Y (w)
1244 + WINDOW_HEADER_LINE_HEIGHT (w)),
1245 (WINDOW_BOX_RIGHT_EDGE_X (w)
1246 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1247 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1248 (WINDOW_BOTTOM_EDGE_Y (w)
1249 - WINDOW_MODE_LINE_HEIGHT (w)));
1252 DEFUN ("window-inside-absolute-pixel-edges",
1253 Fwindow_inside_absolute_pixel_edges,
1254 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1255 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1256 WINDOW must be a live window and defaults to the selected one.
1258 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1259 (0,0) at the top left corner of the frame's window area.
1261 RIGHT is one more than the rightmost x position of WINDOW's text area.
1262 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1263 The inside edges do not include the space used by WINDOW's scroll bar,
1264 display margins, fringes, header line, and/or mode line. */)
1265 (Lisp_Object window)
1267 register struct window *w = decode_live_window (window);
1268 int add_x, add_y;
1270 calc_absolute_offset (w, &add_x, &add_y);
1272 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1273 + WINDOW_LEFT_MARGIN_WIDTH (w)
1274 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1275 (WINDOW_TOP_EDGE_Y (w)
1276 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1277 (WINDOW_BOX_RIGHT_EDGE_X (w)
1278 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1279 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1280 (WINDOW_BOTTOM_EDGE_Y (w)
1281 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1284 /* Test if the character at column X, row Y is within window W.
1285 If it is not, return ON_NOTHING;
1286 if it is on the window's vertical divider, return
1287 ON_RIGHT_DIVIDER;
1288 if it is on the window's horizontal divider, return
1289 ON_BOTTOM_DIVIDER;
1290 if it is in the window's text area, return ON_TEXT;
1291 if it is on the window's modeline, return ON_MODE_LINE;
1292 if it is on the border between the window and its right sibling,
1293 return ON_VERTICAL_BORDER;
1294 if it is on a scroll bar, return ON_SCROLL_BAR;
1295 if it is on the window's top line, return ON_HEADER_LINE;
1296 if it is in left or right fringe of the window,
1297 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1298 if it is in the marginal area to the left/right of the window,
1299 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1301 X and Y are frame relative pixel coordinates. */
1303 static enum window_part
1304 coordinates_in_window (register struct window *w, int x, int y)
1306 struct frame *f = XFRAME (WINDOW_FRAME (w));
1307 enum window_part part;
1308 int ux = FRAME_COLUMN_WIDTH (f);
1309 int left_x = WINDOW_LEFT_EDGE_X (w);
1310 int right_x = WINDOW_RIGHT_EDGE_X (w);
1311 int top_y = WINDOW_TOP_EDGE_Y (w);
1312 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1313 /* The width of the area where the vertical line can be dragged.
1314 (Between mode lines for instance. */
1315 int grabbable_width = ux;
1316 int lmargin_width, rmargin_width, text_left, text_right;
1318 /* Outside any interesting row or column? */
1319 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1320 return ON_NOTHING;
1322 /* On the horizontal window divider (which prevails the vertical
1323 divider)? */
1324 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1325 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1326 && y <= bottom_y)
1327 return ON_BOTTOM_DIVIDER;
1328 /* On vertical window divider? */
1329 else if (!WINDOW_RIGHTMOST_P (w)
1330 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1331 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1332 && x <= right_x)
1333 return ON_RIGHT_DIVIDER;
1334 /* On the horizontal scroll bar? (Including the empty space at its
1335 right!) */
1336 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1337 && y >= (bottom_y
1338 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1339 - CURRENT_MODE_LINE_HEIGHT (w)
1340 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1341 && y <= (bottom_y
1342 - CURRENT_MODE_LINE_HEIGHT (w)
1343 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1344 return ON_HORIZONTAL_SCROLL_BAR;
1345 /* On the mode or header line? */
1346 else if ((WINDOW_WANTS_MODELINE_P (w)
1347 && y >= (bottom_y
1348 - CURRENT_MODE_LINE_HEIGHT (w)
1349 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1350 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1351 && (part = ON_MODE_LINE))
1352 || (WINDOW_WANTS_HEADER_LINE_P (w)
1353 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1354 && (part = ON_HEADER_LINE)))
1356 /* If it's under/over the scroll bar portion of the mode/header
1357 line, say it's on the vertical line. That's to be able to
1358 resize windows horizontally in case we're using toolkit scroll
1359 bars. Note: If scrollbars are on the left, the window that
1360 must be eventually resized is that on the left of WINDOW. */
1361 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1362 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1363 && !WINDOW_LEFTMOST_P (w)
1364 && eabs (x - left_x) < grabbable_width)
1365 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1366 && !WINDOW_RIGHTMOST_P (w)
1367 && eabs (x - right_x) < grabbable_width)))
1368 return ON_VERTICAL_BORDER;
1369 else
1370 return part;
1373 /* In what's below, we subtract 1 when computing right_x because we
1374 want the rightmost pixel, which is given by left_pixel+width-1. */
1375 if (w->pseudo_window_p)
1377 left_x = 0;
1378 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1380 else
1382 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1383 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1386 /* Outside any interesting column? */
1387 if (x < left_x || x > right_x)
1388 return ON_VERTICAL_SCROLL_BAR;
1390 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1391 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1393 text_left = window_box_left (w, TEXT_AREA);
1394 text_right = text_left + window_box_width (w, TEXT_AREA);
1396 if (FRAME_WINDOW_P (f))
1398 if (!w->pseudo_window_p
1399 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1400 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1401 && !WINDOW_RIGHTMOST_P (w)
1402 && (eabs (x - right_x) < grabbable_width))
1403 return ON_VERTICAL_BORDER;
1405 /* Need to say "x > right_x" rather than >=, since on character
1406 terminals, the vertical line's x coordinate is right_x. */
1407 else if (!w->pseudo_window_p
1408 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1409 && !WINDOW_RIGHTMOST_P (w)
1410 /* Why check ux if we are not the rightmost window? Also
1411 shouldn't a pseudo window always be rightmost? */
1412 && x > right_x - ux)
1413 return ON_VERTICAL_BORDER;
1415 if (x < text_left)
1417 if (lmargin_width > 0
1418 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1419 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1420 : (x < left_x + lmargin_width)))
1421 return ON_LEFT_MARGIN;
1422 else
1423 return ON_LEFT_FRINGE;
1426 if (x >= text_right)
1428 if (rmargin_width > 0
1429 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1430 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1431 : (x >= right_x - rmargin_width)))
1432 return ON_RIGHT_MARGIN;
1433 else
1434 return ON_RIGHT_FRINGE;
1437 /* Everything special ruled out - must be on text area */
1438 return ON_TEXT;
1441 /* Take X is the frame-relative pixel x-coordinate, and return the
1442 x-coordinate relative to part PART of window W. */
1444 window_relative_x_coord (struct window *w, enum window_part part, int x)
1446 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1448 switch (part)
1450 case ON_TEXT:
1451 return x - window_box_left (w, TEXT_AREA);
1453 case ON_HEADER_LINE:
1454 case ON_MODE_LINE:
1455 case ON_LEFT_FRINGE:
1456 return x - left_x;
1458 case ON_RIGHT_FRINGE:
1459 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1461 case ON_LEFT_MARGIN:
1462 return (x - left_x
1463 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1464 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1466 case ON_RIGHT_MARGIN:
1467 return (x + 1
1468 - ((w->pseudo_window_p)
1469 ? WINDOW_PIXEL_WIDTH (w)
1470 : WINDOW_BOX_RIGHT_EDGE_X (w))
1471 + window_box_width (w, RIGHT_MARGIN_AREA)
1472 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1473 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1476 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1477 return 0;
1481 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1482 Scoordinates_in_window_p, 2, 2, 0,
1483 doc: /* Return non-nil if COORDINATES are in WINDOW.
1484 WINDOW must be a live window and defaults to the selected one.
1485 COORDINATES is a cons of the form (X . Y), X and Y being distances
1486 measured in characters from the upper-left corner of the frame.
1487 \(0 . 0) denotes the character in the upper left corner of the
1488 frame.
1489 If COORDINATES are in the text portion of WINDOW,
1490 the coordinates relative to the window are returned.
1491 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1492 If they are in the right divider of WINDOW, `right-divider' is returned.
1493 If they are in the mode line of WINDOW, `mode-line' is returned.
1494 If they are in the header line of WINDOW, `header-line' is returned.
1495 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1496 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1497 If they are on the border between WINDOW and its right sibling,
1498 `vertical-line' is returned.
1499 If they are in the windows's left or right marginal areas, `left-margin'\n\
1500 or `right-margin' is returned. */)
1501 (register Lisp_Object coordinates, Lisp_Object window)
1503 struct window *w;
1504 struct frame *f;
1505 int x, y;
1506 Lisp_Object lx, ly;
1508 w = decode_live_window (window);
1509 f = XFRAME (w->frame);
1510 CHECK_CONS (coordinates);
1511 lx = Fcar (coordinates);
1512 ly = Fcdr (coordinates);
1513 CHECK_NUMBER_OR_FLOAT (lx);
1514 CHECK_NUMBER_OR_FLOAT (ly);
1515 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1516 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1518 switch (coordinates_in_window (w, x, y))
1520 case ON_NOTHING:
1521 return Qnil;
1523 case ON_TEXT:
1524 /* Convert X and Y to window relative pixel coordinates, and
1525 return the canonical char units. */
1526 x -= window_box_left (w, TEXT_AREA);
1527 y -= WINDOW_TOP_EDGE_Y (w);
1528 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1529 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1531 case ON_MODE_LINE:
1532 return Qmode_line;
1534 case ON_VERTICAL_BORDER:
1535 return Qvertical_line;
1537 case ON_HEADER_LINE:
1538 return Qheader_line;
1540 case ON_LEFT_FRINGE:
1541 return Qleft_fringe;
1543 case ON_RIGHT_FRINGE:
1544 return Qright_fringe;
1546 case ON_LEFT_MARGIN:
1547 return Qleft_margin;
1549 case ON_RIGHT_MARGIN:
1550 return Qright_margin;
1552 case ON_VERTICAL_SCROLL_BAR:
1553 /* Historically we are supposed to return nil in this case. */
1554 return Qnil;
1556 case ON_HORIZONTAL_SCROLL_BAR:
1557 return Qnil;
1559 case ON_RIGHT_DIVIDER:
1560 return Qright_divider;
1562 case ON_BOTTOM_DIVIDER:
1563 return Qbottom_divider;
1565 default:
1566 emacs_abort ();
1571 /* Callback for foreach_window, used in window_from_coordinates.
1572 Check if window W contains coordinates specified by USER_DATA which
1573 is actually a pointer to a struct check_window_data CW.
1575 Check if window W contains coordinates *CW->x and *CW->y. If it
1576 does, return W in *CW->window, as Lisp_Object, and return in
1577 *CW->part the part of the window under coordinates *X,*Y. Return
1578 false from this function to stop iterating over windows. */
1580 struct check_window_data
1582 Lisp_Object *window;
1583 int x, y;
1584 enum window_part *part;
1587 static bool
1588 check_window_containing (struct window *w, void *user_data)
1590 struct check_window_data *cw = user_data;
1591 enum window_part found = coordinates_in_window (w, cw->x, cw->y);
1592 if (found == ON_NOTHING)
1593 return true;
1594 else
1596 *cw->part = found;
1597 XSETWINDOW (*cw->window, w);
1598 return false;
1603 /* Find the window containing frame-relative pixel position X/Y and
1604 return it as a Lisp_Object.
1606 If X, Y is on one of the window's special `window_part' elements,
1607 set *PART to the id of that element.
1609 If there is no window under X, Y return nil and leave *PART
1610 unmodified. TOOL_BAR_P means detect tool-bar windows.
1612 This function was previously implemented with a loop cycling over
1613 windows with Fnext_window, and starting with the frame's selected
1614 window. It turned out that this doesn't work with an
1615 implementation of next_window using Vwindow_list, because
1616 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1617 tree of F when this function is called asynchronously from
1618 note_mouse_highlight. The original loop didn't terminate in this
1619 case. */
1621 Lisp_Object
1622 window_from_coordinates (struct frame *f, int x, int y,
1623 enum window_part *part, bool tool_bar_p)
1625 Lisp_Object window;
1626 struct check_window_data cw;
1627 enum window_part dummy;
1629 if (part == 0)
1630 part = &dummy;
1632 window = Qnil;
1633 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1634 foreach_window (f, check_window_containing, &cw);
1636 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1637 /* If not found above, see if it's in the tool bar window, if a tool
1638 bar exists. */
1639 if (NILP (window)
1640 && tool_bar_p
1641 && WINDOWP (f->tool_bar_window)
1642 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1643 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1644 != ON_NOTHING))
1646 *part = ON_TEXT;
1647 window = f->tool_bar_window;
1649 #endif
1651 return window;
1654 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1655 doc: /* Return window containing coordinates X and Y on FRAME.
1656 FRAME must be a live frame and defaults to the selected one.
1657 The top left corner of the frame is considered to be row 0,
1658 column 0. */)
1659 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1661 struct frame *f = decode_live_frame (frame);
1663 /* Check that arguments are integers or floats. */
1664 CHECK_NUMBER_OR_FLOAT (x);
1665 CHECK_NUMBER_OR_FLOAT (y);
1667 return window_from_coordinates (f,
1668 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1669 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1670 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1671 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1672 0, false);
1675 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1676 doc: /* Return current value of point in WINDOW.
1677 WINDOW must be a live window and defaults to the selected one.
1679 For a nonselected window, this is the value point would have if that
1680 window were selected.
1682 Note that, when WINDOW is selected, the value returned is the same as
1683 that returned by `point' for WINDOW's buffer. It would be more strictly
1684 correct to return the top-level value of `point', outside of any
1685 `save-excursion' forms. But that is hard to define. */)
1686 (Lisp_Object window)
1688 register struct window *w = decode_live_window (window);
1690 if (w == XWINDOW (selected_window))
1691 return make_number (BUF_PT (XBUFFER (w->contents)));
1692 else
1693 return Fmarker_position (w->pointm);
1696 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1697 doc: /* Return old value of point in WINDOW.
1698 WINDOW must be a live window and defaults to the selected one. */)
1699 (Lisp_Object window)
1701 return Fmarker_position (decode_live_window (window)->old_pointm);
1704 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1705 doc: /* Return position at which display currently starts in WINDOW.
1706 WINDOW must be a live window and defaults to the selected one.
1707 This is updated by redisplay or by calling `set-window-start'. */)
1708 (Lisp_Object window)
1710 return Fmarker_position (decode_live_window (window)->start);
1713 /* This is text temporarily removed from the doc string below.
1715 This function returns nil if the position is not currently known.
1716 That happens when redisplay is preempted and doesn't finish.
1717 If in that case you want to compute where the end of the window would
1718 have been if redisplay had finished, do this:
1719 (save-excursion
1720 (goto-char (window-start window))
1721 (vertical-motion (1- (window-height window)) window)
1722 (point))") */
1724 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1725 doc: /* Return position at which display currently ends in WINDOW.
1726 WINDOW must be a live window and defaults to the selected one.
1727 This is updated by redisplay, when it runs to completion.
1728 Simply changing the buffer text or setting `window-start'
1729 does not update this value.
1730 Return nil if there is no recorded value. (This can happen if the
1731 last redisplay of WINDOW was preempted, and did not finish.)
1732 If UPDATE is non-nil, compute the up-to-date position
1733 if it isn't already recorded. */)
1734 (Lisp_Object window, Lisp_Object update)
1736 Lisp_Object value;
1737 struct window *w = decode_live_window (window);
1738 Lisp_Object buf;
1739 struct buffer *b;
1741 buf = w->contents;
1742 CHECK_BUFFER (buf);
1743 b = XBUFFER (buf);
1745 if (! NILP (update)
1746 && (windows_or_buffers_changed
1747 || !w->window_end_valid
1748 || b->clip_changed
1749 || b->prevent_redisplay_optimizations_p
1750 || window_outdated (w))
1751 /* Don't call display routines if we didn't yet create any real
1752 frames, because the glyph matrices are not yet allocated in
1753 that case. This could happen in some code that runs in the
1754 daemon during initialization (e.g., see bug#20565). */
1755 && !(noninteractive || FRAME_INITIAL_P (WINDOW_XFRAME (w))))
1757 struct text_pos startp;
1758 struct it it;
1759 struct buffer *old_buffer = NULL;
1760 void *itdata = NULL;
1762 /* Cannot use Fvertical_motion because that function doesn't
1763 cope with variable-height lines. */
1764 if (b != current_buffer)
1766 old_buffer = current_buffer;
1767 set_buffer_internal (b);
1770 /* In case W->start is out of the range, use something
1771 reasonable. This situation occurred when loading a file with
1772 `-l' containing a call to `rmail' with subsequent other
1773 commands. At the end, W->start happened to be BEG, while
1774 rmail had already narrowed the buffer. */
1775 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1777 itdata = bidi_shelve_cache ();
1778 start_display (&it, w, startp);
1779 move_it_vertically (&it, window_box_height (w));
1780 if (it.current_y < it.last_visible_y)
1781 move_it_past_eol (&it);
1782 value = make_number (IT_CHARPOS (it));
1783 bidi_unshelve_cache (itdata, false);
1785 if (old_buffer)
1786 set_buffer_internal (old_buffer);
1788 else
1789 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1791 return value;
1794 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1795 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1796 WINDOW must be a live window and defaults to the selected one.
1797 Return POS. */)
1798 (Lisp_Object window, Lisp_Object pos)
1800 register struct window *w = decode_live_window (window);
1802 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1804 if (w == XWINDOW (selected_window))
1806 if (XBUFFER (w->contents) == current_buffer)
1807 Fgoto_char (pos);
1808 else
1810 struct buffer *old_buffer = current_buffer;
1812 /* ... but here we want to catch type error before buffer change. */
1813 CHECK_NUMBER_COERCE_MARKER (pos);
1814 set_buffer_internal (XBUFFER (w->contents));
1815 Fgoto_char (pos);
1816 set_buffer_internal (old_buffer);
1819 else
1821 set_marker_restricted (w->pointm, pos, w->contents);
1822 /* We have to make sure that redisplay updates the window to show
1823 the new value of point. */
1824 wset_redisplay (w);
1827 return pos;
1830 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1831 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1832 WINDOW must be a live window and defaults to the selected one. Return
1833 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1834 overriding motion of point in order to display at this exact start. */)
1835 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1837 register struct window *w = decode_live_window (window);
1839 set_marker_restricted (w->start, pos, w->contents);
1840 /* This is not right, but much easier than doing what is right. */
1841 w->start_at_line_beg = false;
1842 if (NILP (noforce))
1843 w->force_start = true;
1844 w->update_mode_line = true;
1845 /* Bug#15957. */
1846 w->window_end_valid = false;
1847 wset_redisplay (w);
1849 return pos;
1852 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1853 Spos_visible_in_window_p, 0, 3, 0,
1854 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1855 WINDOW must be a live window and defaults to the selected one.
1857 Return nil if that position is scrolled vertically out of view. If a
1858 character is only partially visible, nil is returned, unless the
1859 optional argument PARTIALLY is non-nil. If POS is only out of view
1860 because of horizontal scrolling, return non-nil. If POS is t, it
1861 specifies the position of the last visible glyph in WINDOW. POS
1862 defaults to point in WINDOW; WINDOW defaults to the selected window.
1864 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1865 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1866 where X and Y are the pixel coordinates relative to the top left corner
1867 of the window. The remaining elements are omitted if the character after
1868 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1869 off-window at the top and bottom of the screen line ("row") containing
1870 POS, ROWH is the visible height of that row, and VPOS is the row number
1871 \(zero-based). */)
1872 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1874 struct window *w;
1875 EMACS_INT posint;
1876 struct buffer *buf;
1877 struct text_pos top;
1878 Lisp_Object in_window = Qnil;
1879 int rtop, rbot, rowh, vpos;
1880 bool fully_p = true;
1881 int x, y;
1883 w = decode_live_window (window);
1884 buf = XBUFFER (w->contents);
1885 SET_TEXT_POS_FROM_MARKER (top, w->start);
1887 if (EQ (pos, Qt))
1888 posint = -1;
1889 else if (!NILP (pos))
1891 CHECK_NUMBER_COERCE_MARKER (pos);
1892 posint = XINT (pos);
1894 else if (w == XWINDOW (selected_window))
1895 posint = PT;
1896 else
1897 posint = marker_position (w->pointm);
1899 /* If position is above window start or outside buffer boundaries,
1900 or if window start is out of range, position is not visible. */
1901 if ((EQ (pos, Qt)
1902 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1903 && CHARPOS (top) >= BUF_BEGV (buf)
1904 && CHARPOS (top) <= BUF_ZV (buf)
1905 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos))
1907 fully_p = !rtop && !rbot;
1908 if (!NILP (partially) || fully_p)
1909 in_window = Qt;
1912 if (!NILP (in_window) && !NILP (partially))
1914 Lisp_Object part = Qnil;
1915 if (!fully_p)
1916 part = list4i (rtop, rbot, rowh, vpos);
1917 in_window = Fcons (make_number (x),
1918 Fcons (make_number (y), part));
1921 return in_window;
1924 DEFUN ("window-line-height", Fwindow_line_height,
1925 Swindow_line_height, 0, 2, 0,
1926 doc: /* Return height in pixels of text line LINE in window WINDOW.
1927 WINDOW must be a live window and defaults to the selected one.
1929 Return height of current line if LINE is omitted or nil. Return height of
1930 header or mode line if LINE is `header-line' or `mode-line'.
1931 Otherwise, LINE is a text line number starting from 0. A negative number
1932 counts from the end of the window.
1934 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1935 in pixels of the visible part of the line, VPOS and YPOS are the
1936 vertical position in lines and pixels of the line, relative to the top
1937 of the first text line, and OFFBOT is the number of off-window pixels at
1938 the bottom of the text line. If there are off-window pixels at the top
1939 of the (first) text line, YPOS is negative.
1941 Return nil if window display is not up-to-date. In that case, use
1942 `pos-visible-in-window-p' to obtain the information. */)
1943 (Lisp_Object line, Lisp_Object window)
1945 register struct window *w;
1946 register struct buffer *b;
1947 struct glyph_row *row, *end_row;
1948 int max_y, crop, i;
1949 EMACS_INT n;
1951 w = decode_live_window (window);
1953 if (noninteractive || w->pseudo_window_p)
1954 return Qnil;
1956 CHECK_BUFFER (w->contents);
1957 b = XBUFFER (w->contents);
1959 /* Fail if current matrix is not up-to-date. */
1960 if (!w->window_end_valid
1961 || windows_or_buffers_changed
1962 || b->clip_changed
1963 || b->prevent_redisplay_optimizations_p
1964 || window_outdated (w))
1965 return Qnil;
1967 if (NILP (line))
1969 i = w->cursor.vpos;
1970 if (i < 0 || i >= w->current_matrix->nrows
1971 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1972 return Qnil;
1973 max_y = window_text_bottom_y (w);
1974 goto found_row;
1977 if (EQ (line, Qheader_line))
1979 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1980 return Qnil;
1981 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1982 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1985 if (EQ (line, Qmode_line))
1987 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1988 return (row->enabled_p ?
1989 list4i (row->height,
1990 0, /* not accurate */
1991 (WINDOW_HEADER_LINE_HEIGHT (w)
1992 + window_text_bottom_y (w)),
1994 : Qnil);
1997 CHECK_NUMBER (line);
1998 n = XINT (line);
2000 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2001 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2002 max_y = window_text_bottom_y (w);
2003 i = 0;
2005 while ((n < 0 || i < n)
2006 && row <= end_row && row->enabled_p
2007 && row->y + row->height < max_y)
2008 row++, i++;
2010 if (row > end_row || !row->enabled_p)
2011 return Qnil;
2013 if (++n < 0)
2015 if (-n > i)
2016 return Qnil;
2017 row += n;
2018 i += n;
2021 found_row:
2022 crop = max (0, (row->y + row->height) - max_y);
2023 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
2026 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
2027 0, 1, 0,
2028 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
2029 More precisely, return the value assigned by the last call of
2030 `set-window-dedicated-p' for WINDOW. Return nil if that function was
2031 never called with WINDOW as its argument, or the value set by that
2032 function was internally reset since its last call. WINDOW must be a
2033 live window and defaults to the selected one.
2035 When a window is dedicated to its buffer, `display-buffer' will refrain
2036 from displaying another buffer in it. `get-lru-window' and
2037 `get-largest-window' treat dedicated windows specially.
2038 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2039 `kill-buffer' can delete a dedicated window and the containing frame.
2041 Functions like `set-window-buffer' may change the buffer displayed by a
2042 window, unless that window is "strongly" dedicated to its buffer, that
2043 is the value returned by `window-dedicated-p' is t. */)
2044 (Lisp_Object window)
2046 return decode_live_window (window)->dedicated;
2049 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2050 Sset_window_dedicated_p, 2, 2, 0,
2051 doc: /* Mark WINDOW as dedicated according to FLAG.
2052 WINDOW must be a live window and defaults to the selected one. FLAG
2053 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2054 mark WINDOW as non-dedicated. Return FLAG.
2056 When a window is dedicated to its buffer, `display-buffer' will refrain
2057 from displaying another buffer in it. `get-lru-window' and
2058 `get-largest-window' treat dedicated windows specially.
2059 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2060 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2061 and the containing frame.
2063 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2064 its buffer. Functions like `set-window-buffer' may change the buffer
2065 displayed by a window, unless that window is strongly dedicated to its
2066 buffer. If and when `set-window-buffer' displays another buffer in a
2067 window, it also makes sure that the window is no more dedicated. */)
2068 (Lisp_Object window, Lisp_Object flag)
2070 wset_dedicated (decode_live_window (window), flag);
2071 return flag;
2074 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2075 0, 1, 0,
2076 doc: /* Return buffers previously shown in WINDOW.
2077 WINDOW must be a live window and defaults to the selected one.
2079 The return value is a list of elements (BUFFER WINDOW-START POS),
2080 where BUFFER is a buffer, WINDOW-START is the start position of the
2081 window for that buffer, and POS is a window-specific point value. */)
2082 (Lisp_Object window)
2084 return decode_live_window (window)->prev_buffers;
2087 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2088 Sset_window_prev_buffers, 2, 2, 0,
2089 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2090 WINDOW must be a live window and defaults to the selected one.
2092 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2093 where BUFFER is a buffer, WINDOW-START is the start position of the
2094 window for that buffer, and POS is a window-specific point value. */)
2095 (Lisp_Object window, Lisp_Object prev_buffers)
2097 wset_prev_buffers (decode_live_window (window), prev_buffers);
2098 return prev_buffers;
2101 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2102 0, 1, 0,
2103 doc: /* Return list of buffers recently re-shown in WINDOW.
2104 WINDOW must be a live window and defaults to the selected one. */)
2105 (Lisp_Object window)
2107 return decode_live_window (window)->next_buffers;
2110 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2111 Sset_window_next_buffers, 2, 2, 0,
2112 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2113 WINDOW must be a live window and defaults to the selected one.
2114 NEXT-BUFFERS should be a list of buffers. */)
2115 (Lisp_Object window, Lisp_Object next_buffers)
2117 wset_next_buffers (decode_live_window (window), next_buffers);
2118 return next_buffers;
2121 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2122 0, 1, 0,
2123 doc: /* Return the parameters of WINDOW and their values.
2124 WINDOW must be a valid window and defaults to the selected one. The
2125 return value is a list of elements of the form (PARAMETER . VALUE). */)
2126 (Lisp_Object window)
2128 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2131 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2132 2, 2, 0,
2133 doc: /* Return WINDOW's value for PARAMETER.
2134 WINDOW can be any window and defaults to the selected one. */)
2135 (Lisp_Object window, Lisp_Object parameter)
2137 Lisp_Object result;
2139 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2140 return CDR_SAFE (result);
2143 DEFUN ("set-window-parameter", Fset_window_parameter,
2144 Sset_window_parameter, 3, 3, 0,
2145 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2146 WINDOW can be any window and defaults to the selected one.
2147 Return VALUE. */)
2148 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2150 register struct window *w = decode_any_window (window);
2151 Lisp_Object old_alist_elt;
2153 old_alist_elt = Fassq (parameter, w->window_parameters);
2154 if (NILP (old_alist_elt))
2155 wset_window_parameters
2156 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2157 else
2158 Fsetcdr (old_alist_elt, value);
2159 return value;
2162 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2163 0, 1, 0,
2164 doc: /* Return the display-table that WINDOW is using.
2165 WINDOW must be a live window and defaults to the selected one. */)
2166 (Lisp_Object window)
2168 return decode_live_window (window)->display_table;
2171 /* Get the display table for use on window W. This is either W's
2172 display table or W's buffer's display table. Ignore the specified
2173 tables if they are not valid; if no valid table is specified,
2174 return 0. */
2176 struct Lisp_Char_Table *
2177 window_display_table (struct window *w)
2179 struct Lisp_Char_Table *dp = NULL;
2181 if (DISP_TABLE_P (w->display_table))
2182 dp = XCHAR_TABLE (w->display_table);
2183 else if (BUFFERP (w->contents))
2185 struct buffer *b = XBUFFER (w->contents);
2187 if (DISP_TABLE_P (BVAR (b, display_table)))
2188 dp = XCHAR_TABLE (BVAR (b, display_table));
2189 else if (DISP_TABLE_P (Vstandard_display_table))
2190 dp = XCHAR_TABLE (Vstandard_display_table);
2193 return dp;
2196 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2197 doc: /* Set WINDOW's display-table to TABLE.
2198 WINDOW must be a live window and defaults to the selected one. */)
2199 (register Lisp_Object window, Lisp_Object table)
2201 wset_display_table (decode_live_window (window), table);
2202 return table;
2205 /* Record info on buffer window W is displaying
2206 when it is about to cease to display that buffer. */
2207 static void
2208 unshow_buffer (register struct window *w)
2210 Lisp_Object buf = w->contents;
2211 struct buffer *b = XBUFFER (buf);
2213 eassert (b == XMARKER (w->pointm)->buffer);
2215 #if false
2216 if (w == XWINDOW (selected_window)
2217 || ! EQ (buf, XWINDOW (selected_window)->contents))
2218 /* Do this except when the selected window's buffer
2219 is being removed from some other window. */
2220 #endif
2221 /* last_window_start records the start position that this buffer
2222 had in the last window to be disconnected from it.
2223 Now that this statement is unconditional,
2224 it is possible for the buffer to be displayed in the
2225 selected window, while last_window_start reflects another
2226 window which was recently showing the same buffer.
2227 Some people might say that might be a good thing. Let's see. */
2228 b->last_window_start = marker_position (w->start);
2230 /* Point in the selected window's buffer
2231 is actually stored in that buffer, and the window's pointm isn't used.
2232 So don't clobber point in that buffer. */
2233 if (! EQ (buf, XWINDOW (selected_window)->contents)
2234 /* Don't clobber point in current buffer either (this could be
2235 useful in connection with bug#12208).
2236 && XBUFFER (buf) != current_buffer */
2237 /* This line helps to fix Horsley's testbug.el bug. */
2238 && !(WINDOWP (BVAR (b, last_selected_window))
2239 && w != XWINDOW (BVAR (b, last_selected_window))
2240 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2241 temp_set_point_both (b,
2242 clip_to_bounds (BUF_BEGV (b),
2243 marker_position (w->pointm),
2244 BUF_ZV (b)),
2245 clip_to_bounds (BUF_BEGV_BYTE (b),
2246 marker_byte_position (w->pointm),
2247 BUF_ZV_BYTE (b)));
2249 if (WINDOWP (BVAR (b, last_selected_window))
2250 && w == XWINDOW (BVAR (b, last_selected_window)))
2251 bset_last_selected_window (b, Qnil);
2254 /* Put NEW into the window structure in place of OLD. SETFLAG false
2255 means change window structure only. Otherwise store geometry and
2256 other settings as well. */
2257 static void
2258 replace_window (Lisp_Object old, Lisp_Object new, bool setflag)
2260 Lisp_Object tem;
2261 struct window *o = XWINDOW (old), *n = XWINDOW (new);
2263 /* If OLD is its frame's root window, then NEW is the new
2264 root window for that frame. */
2265 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2266 fset_root_window (XFRAME (o->frame), new);
2268 if (setflag)
2270 n->pixel_left = o->pixel_left;
2271 n->pixel_top = o->pixel_top;
2272 n->pixel_width = o->pixel_width;
2273 n->pixel_height = o->pixel_height;
2274 n->left_col = o->left_col;
2275 n->top_line = o->top_line;
2276 n->total_cols = o->total_cols;
2277 n->total_lines = o->total_lines;
2278 wset_normal_cols (n, o->normal_cols);
2279 wset_normal_cols (o, make_float (1.0));
2280 wset_normal_lines (n, o->normal_lines);
2281 wset_normal_lines (o, make_float (1.0));
2282 n->desired_matrix = n->current_matrix = 0;
2283 n->vscroll = 0;
2284 memset (&n->cursor, 0, sizeof (n->cursor));
2285 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2286 n->last_cursor_vpos = 0;
2287 #ifdef HAVE_WINDOW_SYSTEM
2288 n->phys_cursor_type = NO_CURSOR;
2289 n->phys_cursor_width = -1;
2290 #endif
2291 n->must_be_updated_p = false;
2292 n->pseudo_window_p = false;
2293 n->window_end_vpos = 0;
2294 n->window_end_pos = 0;
2295 n->window_end_valid = false;
2298 tem = o->next;
2299 wset_next (n, tem);
2300 if (!NILP (tem))
2301 wset_prev (XWINDOW (tem), new);
2303 tem = o->prev;
2304 wset_prev (n, tem);
2305 if (!NILP (tem))
2306 wset_next (XWINDOW (tem), new);
2308 tem = o->parent;
2309 wset_parent (n, tem);
2310 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2311 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2314 /* If window WINDOW and its parent window are iso-combined, merge
2315 WINDOW's children into those of its parent window and mark WINDOW as
2316 deleted. */
2318 static void
2319 recombine_windows (Lisp_Object window)
2321 struct window *w, *p, *c;
2322 Lisp_Object parent, child;
2323 bool horflag;
2325 w = XWINDOW (window);
2326 parent = w->parent;
2327 if (!NILP (parent) && NILP (w->combination_limit))
2329 p = XWINDOW (parent);
2330 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2331 && p->horizontal == w->horizontal)
2332 /* WINDOW and PARENT are both either a vertical or a horizontal
2333 combination. */
2335 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2336 child = w->contents;
2337 c = XWINDOW (child);
2339 /* Splice WINDOW's children into its parent's children and
2340 assign new normal sizes. */
2341 if (NILP (w->prev))
2342 wset_combination (p, horflag, child);
2343 else
2345 wset_prev (c, w->prev);
2346 wset_next (XWINDOW (w->prev), child);
2349 while (c)
2351 wset_parent (c, parent);
2353 if (horflag)
2354 wset_normal_cols
2355 (c, make_float ((double) c->pixel_width
2356 / (double) p->pixel_width));
2357 else
2358 wset_normal_lines
2359 (c, make_float ((double) c->pixel_height
2360 / (double) p->pixel_height));
2362 if (NILP (c->next))
2364 if (!NILP (w->next))
2366 wset_next (c, w->next);
2367 wset_prev (XWINDOW (c->next), child);
2370 c = 0;
2372 else
2374 child = c->next;
2375 c = XWINDOW (child);
2379 /* WINDOW can be deleted now. */
2380 wset_combination (w, false, Qnil);
2385 /* If WINDOW can be deleted, delete it. */
2386 static void
2387 delete_deletable_window (Lisp_Object window)
2389 if (!NILP (call1 (Qwindow_deletable_p, window)))
2390 call1 (Qdelete_window, window);
2393 /***********************************************************************
2394 Window List
2395 ***********************************************************************/
2397 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2398 pointer. This is a callback function for foreach_window, used in
2399 the window_list function. */
2401 static bool
2402 add_window_to_list (struct window *w, void *user_data)
2404 Lisp_Object *list = user_data;
2405 Lisp_Object window;
2406 XSETWINDOW (window, w);
2407 *list = Fcons (window, *list);
2408 return true;
2412 /* Return a list of all windows, for use by next_window. If
2413 Vwindow_list is a list, return that list. Otherwise, build a new
2414 list, cache it in Vwindow_list, and return that. */
2416 Lisp_Object
2417 window_list (void)
2419 if (!CONSP (Vwindow_list))
2421 Lisp_Object tail, frame;
2423 Vwindow_list = Qnil;
2424 FOR_EACH_FRAME (tail, frame)
2426 Lisp_Object arglist = Qnil;
2428 /* We are visiting windows in canonical order, and add
2429 new windows at the front of args[1], which means we
2430 have to reverse this list at the end. */
2431 foreach_window (XFRAME (frame), add_window_to_list, &arglist);
2432 arglist = Fnreverse (arglist);
2433 Vwindow_list = CALLN (Fnconc, Vwindow_list, arglist);
2437 return Vwindow_list;
2441 /* Value is true if WINDOW satisfies the constraints given by
2442 OWINDOW, MINIBUF and ALL_FRAMES.
2444 MINIBUF t means WINDOW may be minibuffer windows.
2445 `lambda' means WINDOW may not be a minibuffer window.
2446 a window means a specific minibuffer window
2448 ALL_FRAMES t means search all frames,
2449 nil means search just current frame,
2450 `visible' means search just visible frames on the
2451 current terminal,
2452 0 means search visible and iconified frames on the
2453 current terminal,
2454 a window means search the frame that window belongs to,
2455 a frame means consider windows on that frame, only. */
2457 static bool
2458 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2459 Lisp_Object minibuf, Lisp_Object all_frames)
2461 struct window *w = XWINDOW (window);
2462 struct frame *f = XFRAME (w->frame);
2463 bool candidate_p = true;
2465 if (!BUFFERP (w->contents))
2466 candidate_p = false;
2467 else if (MINI_WINDOW_P (w)
2468 && (EQ (minibuf, Qlambda)
2469 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2471 /* If MINIBUF is `lambda' don't consider any mini-windows.
2472 If it is a window, consider only that one. */
2473 candidate_p = false;
2475 else if (EQ (all_frames, Qt))
2476 candidate_p = true;
2477 else if (NILP (all_frames))
2479 eassert (WINDOWP (owindow));
2480 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2482 else if (EQ (all_frames, Qvisible))
2484 candidate_p = FRAME_VISIBLE_P (f)
2485 && (FRAME_TERMINAL (XFRAME (w->frame))
2486 == FRAME_TERMINAL (XFRAME (selected_frame)));
2489 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2491 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2492 #ifdef HAVE_X_WINDOWS
2493 /* Yuck!! If we've just created the frame and the
2494 window-manager requested the user to place it
2495 manually, the window may still not be considered
2496 `visible'. I'd argue it should be at least
2497 something like `iconified', but don't know how to do
2498 that yet. --Stef */
2499 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2500 && !f->output_data.x->has_been_visible)
2501 #endif
2503 && (FRAME_TERMINAL (XFRAME (w->frame))
2504 == FRAME_TERMINAL (XFRAME (selected_frame)));
2506 else if (WINDOWP (all_frames))
2507 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2508 || EQ (XWINDOW (all_frames)->frame, w->frame)
2509 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2510 else if (FRAMEP (all_frames))
2511 candidate_p = EQ (all_frames, w->frame);
2513 return candidate_p;
2517 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2518 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2519 MINIBUF, and ALL_FRAMES. */
2521 static void
2522 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2524 struct window *w = decode_live_window (*window);
2526 XSETWINDOW (*window, w);
2527 /* MINIBUF nil may or may not include minibuffers. Decide if it
2528 does. */
2529 if (NILP (*minibuf))
2530 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2531 else if (!EQ (*minibuf, Qt))
2532 *minibuf = Qlambda;
2534 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2535 => count none of them, or a specific minibuffer window (the
2536 active one) to count. */
2538 /* ALL_FRAMES nil doesn't specify which frames to include. */
2539 if (NILP (*all_frames))
2540 *all_frames
2541 = (!EQ (*minibuf, Qlambda)
2542 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2543 : Qnil);
2544 else if (EQ (*all_frames, Qvisible))
2546 else if (EQ (*all_frames, make_number (0)))
2548 else if (FRAMEP (*all_frames))
2550 else if (!EQ (*all_frames, Qt))
2551 *all_frames = Qnil;
2555 /* Return the next or previous window of WINDOW in cyclic ordering
2556 of windows. NEXT_P means return the next window. See the
2557 documentation string of next-window for the meaning of MINIBUF and
2558 ALL_FRAMES. */
2560 static Lisp_Object
2561 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames,
2562 bool next_p)
2564 decode_next_window_args (&window, &minibuf, &all_frames);
2566 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2567 return the first window on the frame. */
2568 if (FRAMEP (all_frames)
2569 && !EQ (all_frames, XWINDOW (window)->frame))
2570 return Fframe_first_window (all_frames);
2572 if (next_p)
2574 Lisp_Object list;
2576 /* Find WINDOW in the list of all windows. */
2577 list = Fmemq (window, window_list ());
2579 /* Scan forward from WINDOW to the end of the window list. */
2580 if (CONSP (list))
2581 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2582 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2583 break;
2585 /* Scan from the start of the window list up to WINDOW. */
2586 if (!CONSP (list))
2587 for (list = Vwindow_list;
2588 CONSP (list) && !EQ (XCAR (list), window);
2589 list = XCDR (list))
2590 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2591 break;
2593 if (CONSP (list))
2594 window = XCAR (list);
2596 else
2598 Lisp_Object candidate, list;
2600 /* Scan through the list of windows for candidates. If there are
2601 candidate windows in front of WINDOW, the last one of these
2602 is the one we want. If there are candidates following WINDOW
2603 in the list, again the last one of these is the one we want. */
2604 candidate = Qnil;
2605 for (list = window_list (); CONSP (list); list = XCDR (list))
2607 if (EQ (XCAR (list), window))
2609 if (WINDOWP (candidate))
2610 break;
2612 else if (candidate_window_p (XCAR (list), window, minibuf,
2613 all_frames))
2614 candidate = XCAR (list);
2617 if (WINDOWP (candidate))
2618 window = candidate;
2621 return window;
2625 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2626 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2627 WINDOW must be a live window and defaults to the selected one. The
2628 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2629 consider.
2631 MINIBUF nil or omitted means consider the minibuffer window only if the
2632 minibuffer is active. MINIBUF t means consider the minibuffer window
2633 even if the minibuffer is not active. Any other value means do not
2634 consider the minibuffer window even if the minibuffer is active.
2636 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2637 plus the minibuffer window if specified by the MINIBUF argument. If the
2638 minibuffer counts, consider all windows on all frames that share that
2639 minibuffer too. The following non-nil values of ALL-FRAMES have special
2640 meanings:
2642 - t means consider all windows on all existing frames.
2644 - `visible' means consider all windows on all visible frames.
2646 - 0 (the number zero) means consider all windows on all visible and
2647 iconified frames.
2649 - A frame means consider all windows on that frame only.
2651 Anything else means consider all windows on WINDOW's frame and no
2652 others.
2654 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2655 `next-window' to iterate through the entire cycle of acceptable
2656 windows, eventually ending up back at the window you started with.
2657 `previous-window' traverses the same cycle, in the reverse order. */)
2658 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2660 return next_window (window, minibuf, all_frames, true);
2664 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2665 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2666 WINDOW must be a live window and defaults to the selected one. The
2667 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2668 consider.
2670 MINIBUF nil or omitted means consider the minibuffer window only if the
2671 minibuffer is active. MINIBUF t means consider the minibuffer window
2672 even if the minibuffer is not active. Any other value means do not
2673 consider the minibuffer window even if the minibuffer is active.
2675 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2676 plus the minibuffer window if specified by the MINIBUF argument. If the
2677 minibuffer counts, consider all windows on all frames that share that
2678 minibuffer too. The following non-nil values of ALL-FRAMES have special
2679 meanings:
2681 - t means consider all windows on all existing frames.
2683 - `visible' means consider all windows on all visible frames.
2685 - 0 (the number zero) means consider all windows on all visible and
2686 iconified frames.
2688 - A frame means consider all windows on that frame only.
2690 Anything else means consider all windows on WINDOW's frame and no
2691 others.
2693 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2694 use `previous-window' to iterate through the entire cycle of
2695 acceptable windows, eventually ending up back at the window you
2696 started with. `next-window' traverses the same cycle, in the
2697 reverse order. */)
2698 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2700 return next_window (window, minibuf, all_frames, false);
2704 /* Return a list of windows in cyclic ordering. Arguments are like
2705 for `next-window'. */
2707 static Lisp_Object
2708 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2710 Lisp_Object tail, list, rest;
2712 decode_next_window_args (&window, &minibuf, &all_frames);
2713 list = Qnil;
2715 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2716 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2717 list = Fcons (XCAR (tail), list);
2719 /* Rotate the list to start with WINDOW. */
2720 list = Fnreverse (list);
2721 rest = Fmemq (window, list);
2722 if (!NILP (rest) && !EQ (rest, list))
2724 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2726 XSETCDR (tail, Qnil);
2727 list = nconc2 (rest, list);
2729 return list;
2733 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2734 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2735 FRAME nil or omitted means use the selected frame.
2736 WINDOW nil or omitted means use the window selected within FRAME.
2737 MINIBUF t means include the minibuffer window, even if it isn't active.
2738 MINIBUF nil or omitted means include the minibuffer window only
2739 if it's active.
2740 MINIBUF neither nil nor t means never include the minibuffer window. */)
2741 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2743 if (NILP (window))
2744 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2745 CHECK_WINDOW (window);
2746 if (NILP (frame))
2747 frame = selected_frame;
2749 if (!EQ (frame, XWINDOW (window)->frame))
2750 error ("Window is on a different frame");
2752 return window_list_1 (window, minibuf, frame);
2756 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2757 doc: /* Return a list of all live windows.
2758 WINDOW specifies the first window to list and defaults to the selected
2759 window.
2761 Optional argument MINIBUF nil or omitted means consider the minibuffer
2762 window only if the minibuffer is active. MINIBUF t means consider the
2763 minibuffer window even if the minibuffer is not active. Any other value
2764 means do not consider the minibuffer window even if the minibuffer is
2765 active.
2767 Optional argument ALL-FRAMES nil or omitted means consider all windows
2768 on WINDOW's frame, plus the minibuffer window if specified by the
2769 MINIBUF argument. If the minibuffer counts, consider all windows on all
2770 frames that share that minibuffer too. The following non-nil values of
2771 ALL-FRAMES have special meanings:
2773 - t means consider all windows on all existing frames.
2775 - `visible' means consider all windows on all visible frames.
2777 - 0 (the number zero) means consider all windows on all visible and
2778 iconified frames.
2780 - A frame means consider all windows on that frame only.
2782 Anything else means consider all windows on WINDOW's frame and no
2783 others.
2785 If WINDOW is not on the list of windows returned, some other window will
2786 be listed first but no error is signaled. */)
2787 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2789 return window_list_1 (window, minibuf, all_frames);
2792 /* Look at all windows, performing an operation specified by TYPE
2793 with argument OBJ.
2794 If FRAMES is Qt, look at all frames;
2795 Qnil, look at just the selected frame;
2796 Qvisible, look at visible frames;
2797 a frame, just look at windows on that frame.
2798 If MINI, perform the operation on minibuffer windows too. */
2800 enum window_loop
2802 WINDOW_LOOP_UNUSED,
2803 GET_BUFFER_WINDOW, /* Arg is buffer */
2804 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2805 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2806 CHECK_ALL_WINDOWS /* Arg is ignored */
2809 static Lisp_Object
2810 window_loop (enum window_loop type, Lisp_Object obj, bool mini,
2811 Lisp_Object frames)
2813 Lisp_Object window, windows, best_window, frame_arg;
2814 bool frame_best_window_flag = false;
2815 struct frame *f;
2816 struct gcpro gcpro1;
2818 /* If we're only looping through windows on a particular frame,
2819 frame points to that frame. If we're looping through windows
2820 on all frames, frame is 0. */
2821 if (FRAMEP (frames))
2822 f = XFRAME (frames);
2823 else if (NILP (frames))
2824 f = SELECTED_FRAME ();
2825 else
2826 f = NULL;
2828 if (f)
2829 frame_arg = Qlambda;
2830 else if (EQ (frames, make_number (0)))
2831 frame_arg = frames;
2832 else if (EQ (frames, Qvisible))
2833 frame_arg = frames;
2834 else
2835 frame_arg = Qt;
2837 /* frame_arg is Qlambda to stick to one frame,
2838 Qvisible to consider all visible frames,
2839 or Qt otherwise. */
2841 /* Pick a window to start with. */
2842 if (WINDOWP (obj))
2843 window = obj;
2844 else if (f)
2845 window = FRAME_SELECTED_WINDOW (f);
2846 else
2847 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2849 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2850 GCPRO1 (windows);
2851 best_window = Qnil;
2853 for (; CONSP (windows); windows = XCDR (windows))
2855 struct window *w;
2857 window = XCAR (windows);
2858 w = XWINDOW (window);
2860 /* Note that we do not pay attention here to whether the frame
2861 is visible, since Fwindow_list skips non-visible frames if
2862 that is desired, under the control of frame_arg. */
2863 if (!MINI_WINDOW_P (w)
2864 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2865 consider all windows. */
2866 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2867 || (mini && minibuf_level > 0))
2868 switch (type)
2870 case GET_BUFFER_WINDOW:
2871 if (EQ (w->contents, obj)
2872 /* Don't find any minibuffer window except the one that
2873 is currently in use. */
2874 && (!MINI_WINDOW_P (w) || EQ (window, minibuf_window)))
2876 if (EQ (window, selected_window))
2877 /* Preferably return the selected window. */
2878 RETURN_UNGCPRO (window);
2879 else if (EQ (XWINDOW (window)->frame, selected_frame)
2880 && !frame_best_window_flag)
2881 /* Prefer windows on the current frame (but don't
2882 choose another one if we have one already). */
2884 best_window = window;
2885 frame_best_window_flag = true;
2887 else if (NILP (best_window))
2888 best_window = window;
2890 break;
2892 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2893 /* We could simply check whether the buffer shown by window
2894 is live, and show another buffer in case it isn't. */
2895 if (EQ (w->contents, obj))
2897 /* Undedicate WINDOW. */
2898 wset_dedicated (w, Qnil);
2899 /* Make WINDOW show the buffer returned by
2900 other_buffer_safely, don't run any hooks. */
2901 set_window_buffer
2902 (window, other_buffer_safely (w->contents), false, false);
2903 /* If WINDOW is the selected window, make its buffer
2904 current. But do so only if the window shows the
2905 current buffer (Bug#6454). */
2906 if (EQ (window, selected_window)
2907 && XBUFFER (w->contents) == current_buffer)
2908 Fset_buffer (w->contents);
2910 break;
2912 case REDISPLAY_BUFFER_WINDOWS:
2913 if (EQ (w->contents, obj))
2915 mark_window_display_accurate (window, false);
2916 w->update_mode_line = true;
2917 XBUFFER (obj)->prevent_redisplay_optimizations_p = true;
2918 update_mode_lines = 27;
2919 best_window = window;
2921 break;
2923 /* Check for a leaf window that has a killed buffer
2924 or broken markers. */
2925 case CHECK_ALL_WINDOWS:
2926 if (BUFFERP (w->contents))
2928 struct buffer *b = XBUFFER (w->contents);
2930 if (!BUFFER_LIVE_P (b))
2931 emacs_abort ();
2932 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2933 emacs_abort ();
2934 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2935 emacs_abort ();
2937 break;
2939 case WINDOW_LOOP_UNUSED:
2940 break;
2944 UNGCPRO;
2945 return best_window;
2948 /* Used for debugging. Abort if any window has a dead buffer. */
2950 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2951 void
2952 check_all_windows (void)
2954 window_loop (CHECK_ALL_WINDOWS, Qnil, true, Qt);
2957 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2958 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2959 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2960 the current buffer.
2962 The optional argument ALL-FRAMES specifies the frames to consider:
2964 - t means consider all windows on all existing frames.
2966 - `visible' means consider all windows on all visible frames.
2968 - 0 (the number zero) means consider all windows on all visible
2969 and iconified frames.
2971 - A frame means consider all windows on that frame only.
2973 Any other value of ALL-FRAMES means consider all windows on the
2974 selected frame and no others. */)
2975 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2977 Lisp_Object buffer;
2979 if (NILP (buffer_or_name))
2980 buffer = Fcurrent_buffer ();
2981 else
2982 buffer = Fget_buffer (buffer_or_name);
2984 if (BUFFERP (buffer))
2985 return window_loop (GET_BUFFER_WINDOW, buffer, true, all_frames);
2986 else
2987 return Qnil;
2991 static Lisp_Object
2992 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2994 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2997 /* Placeholder used by temacs -nw before window.el is loaded. */
2998 DEFUN ("window--sanitize-window-sizes", Fwindow__sanitize_window_sizes,
2999 Swindow__sanitize_window_sizes, 2, 2, 0,
3000 doc: /* */
3001 attributes: const)
3002 (Lisp_Object frame, Lisp_Object horizontal)
3004 return Qnil;
3007 Lisp_Object
3008 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
3010 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
3014 static Lisp_Object
3015 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
3017 return call2 (Qwindow_pixel_to_total, frame, horizontal);
3021 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
3022 Sdelete_other_windows_internal, 0, 2, "",
3023 doc: /* Make WINDOW fill its frame.
3024 Only the frame WINDOW is on is affected. WINDOW must be a valid window
3025 and defaults to the selected one.
3027 Optional argument ROOT, if non-nil, must specify an internal window such
3028 that WINDOW is in its window subtree. If this is the case, replace ROOT
3029 by WINDOW and leave alone any windows not part of ROOT's subtree.
3031 When WINDOW is live try to reduce display jumps by keeping the text
3032 previously visible in WINDOW in the same place on the frame. Doing this
3033 depends on the value of (window-start WINDOW), so if calling this
3034 function in a program gives strange scrolling, make sure the
3035 window-start value is reasonable when this function is called. */)
3036 (Lisp_Object window, Lisp_Object root)
3038 struct window *w, *r, *s;
3039 struct frame *f;
3040 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
3041 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
3042 int top IF_LINT (= 0), new_top;
3044 w = decode_valid_window (window);
3045 XSETWINDOW (window, w);
3046 f = XFRAME (w->frame);
3048 if (NILP (root))
3049 /* ROOT is the frame's root window. */
3051 root = FRAME_ROOT_WINDOW (f);
3052 r = XWINDOW (root);
3054 else
3055 /* ROOT must be an ancestor of WINDOW. */
3057 r = decode_valid_window (root);
3058 pwindow = XWINDOW (window)->parent;
3059 while (!NILP (pwindow))
3060 if (EQ (pwindow, root))
3061 break;
3062 else
3063 pwindow = XWINDOW (pwindow)->parent;
3064 if (!EQ (pwindow, root))
3065 error ("Specified root is not an ancestor of specified window");
3068 if (EQ (window, root))
3069 /* A noop. */
3070 return Qnil;
3071 /* I don't understand the "top > 0" part below. If we deal with a
3072 standalone minibuffer it would have been caught by the preceding
3073 test. */
3074 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3075 error ("Can't expand minibuffer to full frame");
3077 if (BUFFERP (w->contents))
3079 startpos = marker_position (w->start);
3080 startbyte = marker_byte_position (w->start);
3081 top = (WINDOW_TOP_EDGE_LINE (w)
3082 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3083 /* Make sure WINDOW is the frame's selected window. */
3084 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3086 if (EQ (selected_frame, w->frame))
3087 Fselect_window (window, Qnil);
3088 else
3089 fset_selected_window (f, window);
3092 else
3094 /* See if the frame's selected window is a part of the window
3095 subtree rooted at WINDOW, by finding all the selected window's
3096 parents and comparing each one with WINDOW. If it isn't we
3097 need a new selected window for this frame. */
3098 swindow = FRAME_SELECTED_WINDOW (f);
3099 while (true)
3101 pwindow = swindow;
3102 while (!NILP (pwindow) && !EQ (window, pwindow))
3103 pwindow = XWINDOW (pwindow)->parent;
3105 if (EQ (window, pwindow))
3106 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3107 as the new selected window. */
3108 break;
3109 else
3110 /* Else try the previous window of SWINDOW. */
3111 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3114 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3116 if (EQ (selected_frame, w->frame))
3117 Fselect_window (swindow, Qnil);
3118 else
3119 fset_selected_window (f, swindow);
3123 block_input ();
3124 if (!FRAME_INITIAL_P (f))
3126 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3128 /* We are going to free the glyph matrices of WINDOW, and with
3129 that we might lose any information about glyph rows that have
3130 some of their glyphs highlighted in mouse face. (These rows
3131 are marked with a mouse_face_p flag.) If WINDOW
3132 indeed has some glyphs highlighted in mouse face, signal to
3133 frame's up-to-date hook that mouse highlight was overwritten,
3134 so that it will arrange for redisplaying the highlight. */
3135 if (EQ (hlinfo->mouse_face_window, window))
3136 reset_mouse_highlight (hlinfo);
3138 free_window_matrices (r);
3140 fset_redisplay (f);
3141 Vwindow_list = Qnil;
3142 FRAME_WINDOW_SIZES_CHANGED (f) = true;
3143 bool resize_failed = false;
3145 if (!WINDOW_LEAF_P (w))
3147 /* Resize child windows vertically. */
3148 XSETINT (delta, r->pixel_height - w->pixel_height);
3149 w->pixel_top = r->pixel_top;
3150 w->top_line = r->top_line;
3151 resize_root_window (window, delta, Qnil, Qnil, Qt);
3152 if (window_resize_check (w, false))
3154 window_resize_apply (w, false);
3155 window_pixel_to_total (w->frame, Qnil);
3157 else
3159 resize_root_window (window, delta, Qnil, Qt, Qt);
3160 if (window_resize_check (w, false))
3162 window_resize_apply (w, false);
3163 window_pixel_to_total (w->frame, Qnil);
3165 else
3166 resize_failed = true;
3169 /* Resize child windows horizontally. */
3170 if (!resize_failed)
3172 w->left_col = r->left_col;
3173 w->pixel_left = r->pixel_left;
3174 XSETINT (delta, r->pixel_width - w->pixel_width);
3175 resize_root_window (window, delta, Qt, Qnil, Qt);
3176 if (window_resize_check (w, true))
3178 window_resize_apply (w, true);
3179 window_pixel_to_total (w->frame, Qt);
3181 else
3183 resize_root_window (window, delta, Qt, Qt, Qt);
3184 if (window_resize_check (w, true))
3186 window_resize_apply (w, true);
3187 window_pixel_to_total (w->frame, Qt);
3189 else
3190 resize_failed = true;
3194 if (resize_failed)
3195 /* Play safe, if we still can ... */
3197 window = swindow;
3198 w = XWINDOW (window);
3202 /* Cleanly unlink WINDOW from window-tree. */
3203 if (!NILP (w->prev))
3204 /* Get SIBLING above (on the left of) WINDOW. */
3206 sibling = w->prev;
3207 s = XWINDOW (sibling);
3208 wset_next (s, w->next);
3209 if (!NILP (s->next))
3210 wset_prev (XWINDOW (s->next), sibling);
3212 else
3213 /* Get SIBLING below (on the right of) WINDOW. */
3215 sibling = w->next;
3216 s = XWINDOW (sibling);
3217 wset_prev (s, Qnil);
3218 wset_combination (XWINDOW (w->parent),
3219 XWINDOW (w->parent)->horizontal, sibling);
3222 /* Delete ROOT and all child windows of ROOT. */
3223 if (WINDOWP (r->contents))
3225 delete_all_child_windows (r->contents);
3226 wset_combination (r, false, Qnil);
3229 replace_window (root, window, true);
3231 /* This must become SWINDOW anyway ....... */
3232 if (BUFFERP (w->contents) && !resize_failed)
3234 /* Try to minimize scrolling, by setting the window start to the
3235 point will cause the text at the old window start to be at the
3236 same place on the frame. But don't try to do this if the
3237 window start is outside the visible portion (as might happen
3238 when the display is not current, due to typeahead). */
3239 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3240 if (new_top != top
3241 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3242 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3244 struct position pos;
3245 struct buffer *obuf = current_buffer;
3247 Fset_buffer (w->contents);
3248 /* This computation used to temporarily move point, but that
3249 can have unwanted side effects due to text properties. */
3250 pos = *vmotion (startpos, startbyte, -top, w);
3252 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3253 w->window_end_valid = false;
3254 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3255 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3256 /* We need to do this, so that the window-scroll-functions
3257 get called. */
3258 w->optional_new_start = true;
3260 set_buffer_internal (obuf);
3264 adjust_frame_glyphs (f);
3265 unblock_input ();
3267 run_window_configuration_change_hook (f);
3269 return Qnil;
3273 void
3274 replace_buffer_in_windows (Lisp_Object buffer)
3276 call1 (Qreplace_buffer_in_windows, buffer);
3279 /* If BUFFER is shown in a window, safely replace it with some other
3280 buffer in all windows of all frames, even those on other keyboards. */
3282 void
3283 replace_buffer_in_windows_safely (Lisp_Object buffer)
3285 if (buffer_window_count (XBUFFER (buffer)))
3287 Lisp_Object tail, frame;
3289 /* A single call to window_loop won't do the job because it only
3290 considers frames on the current keyboard. So loop manually over
3291 frames, and handle each one. */
3292 FOR_EACH_FRAME (tail, frame)
3293 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, true, frame);
3297 /* The following three routines are needed for running a window's
3298 configuration change hook. */
3299 static void
3300 run_funs (Lisp_Object funs)
3302 for (; CONSP (funs); funs = XCDR (funs))
3303 if (!EQ (XCAR (funs), Qt))
3304 call0 (XCAR (funs));
3307 static void
3308 select_window_norecord (Lisp_Object window)
3310 if (WINDOW_LIVE_P (window))
3311 Fselect_window (window, Qt);
3314 static void
3315 select_frame_norecord (Lisp_Object frame)
3317 if (FRAME_LIVE_P (XFRAME (frame)))
3318 Fselect_frame (frame, Qt);
3321 void
3322 run_window_configuration_change_hook (struct frame *f)
3324 ptrdiff_t count = SPECPDL_INDEX ();
3325 Lisp_Object frame, global_wcch
3326 = Fdefault_value (Qwindow_configuration_change_hook);
3327 XSETFRAME (frame, f);
3329 if (NILP (Vrun_hooks)
3330 || !(f->can_x_set_window_size)
3331 || !(f->after_make_frame))
3332 return;
3334 /* Use the right buffer. Matters when running the local hooks. */
3335 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3337 record_unwind_current_buffer ();
3338 Fset_buffer (Fwindow_buffer (Qnil));
3341 if (SELECTED_FRAME () != f)
3343 record_unwind_protect (select_frame_norecord, selected_frame);
3344 select_frame_norecord (frame);
3347 /* Look for buffer-local values. */
3349 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3350 for (; CONSP (windows); windows = XCDR (windows))
3352 Lisp_Object window = XCAR (windows);
3353 Lisp_Object buffer = Fwindow_buffer (window);
3354 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3355 buffer)))
3357 ptrdiff_t inner_count = SPECPDL_INDEX ();
3358 record_unwind_protect (select_window_norecord, selected_window);
3359 select_window_norecord (window);
3360 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3361 buffer));
3362 unbind_to (inner_count, Qnil);
3367 run_funs (global_wcch);
3368 unbind_to (count, Qnil);
3371 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3372 Srun_window_configuration_change_hook, 0, 1, 0,
3373 doc: /* Run `window-configuration-change-hook' for FRAME.
3374 If FRAME is omitted or nil, it defaults to the selected frame. */)
3375 (Lisp_Object frame)
3377 run_window_configuration_change_hook (decode_live_frame (frame));
3378 return Qnil;
3381 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3382 Srun_window_scroll_functions, 0, 1, 0,
3383 doc: /* Run `window-scroll-functions' for WINDOW.
3384 If WINDOW is omitted or nil, it defaults to the selected window. */)
3385 (Lisp_Object window)
3387 if (! NILP (Vwindow_scroll_functions))
3388 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3389 Fmarker_position (decode_live_window (window)->start));
3390 return Qnil;
3393 /* Make WINDOW display BUFFER. RUN_HOOKS_P means it's allowed
3394 to run hooks. See make_frame for a case where it's not allowed.
3395 KEEP_MARGINS_P means that the current margins, fringes, and
3396 scroll-bar settings of the window are not reset from the buffer's
3397 local settings. */
3399 void
3400 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3401 bool run_hooks_p, bool keep_margins_p)
3403 struct window *w = XWINDOW (window);
3404 struct buffer *b = XBUFFER (buffer);
3405 ptrdiff_t count = SPECPDL_INDEX ();
3406 bool samebuf = EQ (buffer, w->contents);
3408 wset_buffer (w, buffer);
3410 if (EQ (window, selected_window))
3411 bset_last_selected_window (b, window);
3413 /* Let redisplay errors through. */
3414 b->display_error_modiff = 0;
3416 /* Update time stamps of buffer display. */
3417 if (INTEGERP (BVAR (b, display_count)))
3418 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3419 bset_display_time (b, Fcurrent_time ());
3421 w->window_end_pos = 0;
3422 w->window_end_vpos = 0;
3423 w->last_cursor_vpos = 0;
3425 if (!(keep_margins_p && samebuf))
3426 { /* If we're not actually changing the buffer, don't reset hscroll
3427 and vscroll. This case happens for example when called from
3428 change_frame_size_1, where we use a dummy call to
3429 Fset_window_buffer on the frame's selected window (and no
3430 other) just in order to run window-configuration-change-hook
3431 (no longer true since change_frame_size_1 directly calls
3432 run_window_configuration_change_hook). Resetting hscroll and
3433 vscroll here is problematic for things like image-mode and
3434 doc-view-mode since it resets the image's position whenever we
3435 resize the frame. */
3436 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3437 w->suspend_auto_hscroll = false;
3438 w->vscroll = 0;
3439 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3440 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3441 set_marker_restricted (w->start,
3442 make_number (b->last_window_start),
3443 buffer);
3444 w->start_at_line_beg = false;
3445 w->force_start = false;
3447 /* Maybe we could move this into the `if' but it's not obviously safe and
3448 I doubt it's worth the trouble. */
3449 wset_redisplay (w);
3450 w->update_mode_line = true;
3452 /* We must select BUFFER to run the window-scroll-functions and to look up
3453 the buffer-local value of Vwindow_point_insertion_type. */
3454 record_unwind_current_buffer ();
3455 Fset_buffer (buffer);
3457 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3458 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3460 if (!keep_margins_p)
3462 /* Set left and right marginal area width etc. from buffer. */
3463 set_window_fringes (w, BVAR (b, left_fringe_width),
3464 BVAR (b, right_fringe_width),
3465 BVAR (b, fringes_outside_margins));
3466 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3467 BVAR (b, vertical_scroll_bar_type),
3468 BVAR (b, scroll_bar_height),
3469 BVAR (b, horizontal_scroll_bar_type));
3470 set_window_margins (w, BVAR (b, left_margin_cols),
3471 BVAR (b, right_margin_cols));
3472 apply_window_adjustment (w);
3475 if (run_hooks_p)
3477 if (! NILP (Vwindow_scroll_functions))
3478 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3479 Fmarker_position (w->start));
3480 if (!samebuf)
3481 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3484 unbind_to (count, Qnil);
3487 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3488 doc: /* Make WINDOW display BUFFER-OR-NAME.
3489 WINDOW must be a live window and defaults to the selected one.
3490 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3492 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3493 display margins, fringe widths, and scroll bar settings are preserved;
3494 the default is to reset these from the local settings for BUFFER-OR-NAME
3495 or the frame defaults. Return nil.
3497 This function throws an error when WINDOW is strongly dedicated to its
3498 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3499 already display BUFFER-OR-NAME.
3501 This function runs `window-scroll-functions' before running
3502 `window-configuration-change-hook'. */)
3503 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3505 register Lisp_Object tem, buffer;
3506 register struct window *w = decode_live_window (window);
3508 XSETWINDOW (window, w);
3509 buffer = Fget_buffer (buffer_or_name);
3510 CHECK_BUFFER (buffer);
3511 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3512 error ("Attempt to display deleted buffer");
3514 tem = w->contents;
3515 if (NILP (tem))
3516 error ("Window is deleted");
3517 else
3519 if (!EQ (tem, buffer))
3521 if (EQ (w->dedicated, Qt))
3522 /* WINDOW is strongly dedicated to its buffer, signal an
3523 error. */
3524 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3525 else
3526 /* WINDOW is weakly dedicated to its buffer, reset
3527 dedication. */
3528 wset_dedicated (w, Qnil);
3530 call1 (Qrecord_window_buffer, window);
3533 unshow_buffer (w);
3536 set_window_buffer (window, buffer, true, !NILP (keep_margins));
3538 return Qnil;
3541 static Lisp_Object
3542 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3544 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3547 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3548 0, 1, 0,
3549 doc: /* Force all windows to be updated on next redisplay.
3550 If optional arg OBJECT is a window, force redisplay of that window only.
3551 If OBJECT is a buffer or buffer name, force redisplay of all windows
3552 displaying that buffer. */)
3553 (Lisp_Object object)
3555 if (NILP (object))
3557 windows_or_buffers_changed = 29;
3558 update_mode_lines = 28;
3559 return Qt;
3562 if (WINDOWP (object))
3564 struct window *w = XWINDOW (object);
3565 mark_window_display_accurate (object, false);
3566 w->update_mode_line = true;
3567 if (BUFFERP (w->contents))
3568 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
3569 update_mode_lines = 29;
3570 return Qt;
3573 if (STRINGP (object))
3574 object = Fget_buffer (object);
3575 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3576 && buffer_window_count (XBUFFER (object)))
3578 /* If buffer is live and shown in at least one window, find
3579 all windows showing this buffer and force update of them. */
3580 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, false, Qvisible);
3581 return NILP (object) ? Qnil : Qt;
3584 /* If nothing suitable was found, just return.
3585 We could signal an error, but this feature will typically be used
3586 asynchronously in timers or process sentinels, so we don't. */
3587 return Qnil;
3590 /* Obsolete since 24.3. */
3591 void
3592 temp_output_buffer_show (register Lisp_Object buf)
3594 register struct buffer *old = current_buffer;
3595 register Lisp_Object window;
3596 register struct window *w;
3598 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3600 Fset_buffer (buf);
3601 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3602 BEGV = BEG;
3603 ZV = Z;
3604 SET_PT (BEG);
3605 set_buffer_internal (old);
3607 if (!NILP (Vtemp_buffer_show_function))
3608 call1 (Vtemp_buffer_show_function, buf);
3609 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3611 if (!EQ (XWINDOW (window)->frame, selected_frame))
3612 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3613 Vminibuf_scroll_window = window;
3614 w = XWINDOW (window);
3615 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3616 w->suspend_auto_hscroll = false;
3617 set_marker_restricted_both (w->start, buf, BEG, BEG);
3618 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3619 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3621 /* Run temp-buffer-show-hook, with the chosen window selected
3622 and its buffer current. */
3624 ptrdiff_t count = SPECPDL_INDEX ();
3625 Lisp_Object prev_window, prev_buffer;
3626 prev_window = selected_window;
3627 XSETBUFFER (prev_buffer, old);
3629 /* Select the window that was chosen, for running the hook.
3630 Note: Both Fselect_window and select_window_norecord may
3631 set-buffer to the buffer displayed in the window,
3632 so we need to save the current buffer. --stef */
3633 record_unwind_protect (restore_buffer, prev_buffer);
3634 record_unwind_protect (select_window_norecord, prev_window);
3635 Fselect_window (window, Qt);
3636 Fset_buffer (w->contents);
3637 run_hook (Qtemp_buffer_show_hook);
3638 unbind_to (count, Qnil);
3643 /* Allocate basically initialized window. */
3645 static struct window *
3646 allocate_window (void)
3648 return ALLOCATE_ZEROED_PSEUDOVECTOR
3649 (struct window, current_matrix, PVEC_WINDOW);
3652 /* Make new window, have it replace WINDOW in window-tree, and make
3653 WINDOW its only vertical child (HORFLAG means make WINDOW its only
3654 horizontal child). */
3655 static void
3656 make_parent_window (Lisp_Object window, bool horflag)
3658 Lisp_Object parent;
3659 register struct window *o, *p;
3661 o = XWINDOW (window);
3662 p = allocate_window ();
3663 memcpy ((char *) p + sizeof (struct vectorlike_header),
3664 (char *) o + sizeof (struct vectorlike_header),
3665 word_size * VECSIZE (struct window));
3666 /* P's buffer slot may change from nil to a buffer... */
3667 adjust_window_count (p, 1);
3668 XSETWINDOW (parent, p);
3670 p->sequence_number = ++sequence_number;
3672 replace_window (window, parent, true);
3674 wset_next (o, Qnil);
3675 wset_prev (o, Qnil);
3676 wset_parent (o, parent);
3677 /* ...but now P becomes an internal window. */
3678 wset_start (p, Qnil);
3679 wset_pointm (p, Qnil);
3680 wset_old_pointm (p, Qnil);
3681 wset_buffer (p, Qnil);
3682 wset_combination (p, horflag, window);
3683 wset_combination_limit (p, Qnil);
3684 wset_window_parameters (p, Qnil);
3687 /* Make new window from scratch. */
3688 Lisp_Object
3689 make_window (void)
3691 Lisp_Object window;
3692 register struct window *w;
3694 w = allocate_window ();
3695 /* Initialize Lisp data. Note that allocate_window initializes all
3696 Lisp data to nil, so do it only for slots which should not be nil. */
3697 wset_normal_lines (w, make_float (1.0));
3698 wset_normal_cols (w, make_float (1.0));
3699 wset_new_total (w, make_number (0));
3700 wset_new_normal (w, make_number (0));
3701 wset_new_pixel (w, make_number (0));
3702 wset_start (w, Fmake_marker ());
3703 wset_pointm (w, Fmake_marker ());
3704 wset_old_pointm (w, Fmake_marker ());
3705 wset_vertical_scroll_bar_type (w, Qt);
3706 wset_horizontal_scroll_bar_type (w, Qt);
3707 /* These Lisp fields are marked specially so they're not set to nil by
3708 allocate_window. */
3709 wset_prev_buffers (w, Qnil);
3710 wset_next_buffers (w, Qnil);
3712 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3713 non-Lisp data, so do it only for slots which should not be zero. */
3714 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3715 w->left_fringe_width = w->right_fringe_width = -1;
3716 w->mode_line_height = w->header_line_height = -1;
3717 #ifdef HAVE_WINDOW_SYSTEM
3718 w->phys_cursor_type = NO_CURSOR;
3719 w->phys_cursor_width = -1;
3720 #endif
3721 w->sequence_number = ++sequence_number;
3722 w->scroll_bar_width = -1;
3723 w->scroll_bar_height = -1;
3724 w->column_number_displayed = -1;
3725 /* Reset window_list. */
3726 Vwindow_list = Qnil;
3727 /* Return window. */
3728 XSETWINDOW (window, w);
3729 return window;
3732 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3733 doc: /* Set new pixel size of WINDOW to SIZE.
3734 WINDOW must be a valid window and defaults to the selected one.
3735 Return SIZE.
3737 Optional argument ADD non-nil means add SIZE to the new pixel size of
3738 WINDOW and return the sum.
3740 The new pixel size of WINDOW, if valid, will be shortly installed as
3741 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3742 `window-pixel-width').
3744 Note: This function does not operate on any child windows of WINDOW. */)
3745 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3747 struct window *w = decode_valid_window (window);
3748 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3749 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3751 CHECK_RANGED_INTEGER (size, size_min, size_max);
3752 if (NILP (add))
3753 wset_new_pixel (w, size);
3754 else
3755 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3757 return w->new_pixel;
3760 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3761 doc: /* Set new total size of WINDOW to SIZE.
3762 WINDOW must be a valid window and defaults to the selected one.
3763 Return SIZE.
3765 Optional argument ADD non-nil means add SIZE to the new total size of
3766 WINDOW and return the sum.
3768 The new total size of WINDOW, if valid, will be shortly installed as
3769 WINDOW's total height (see `window-total-height') or total width (see
3770 `window-total-width').
3772 Note: This function does not operate on any child windows of WINDOW. */)
3773 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3775 struct window *w = decode_valid_window (window);
3777 CHECK_NUMBER (size);
3778 if (NILP (add))
3779 wset_new_total (w, size);
3780 else
3781 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3783 return w->new_total;
3786 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3787 doc: /* Set new normal size of WINDOW to SIZE.
3788 WINDOW must be a valid window and defaults to the selected one.
3789 Return SIZE.
3791 The new normal size of WINDOW, if valid, will be shortly installed as
3792 WINDOW's normal size (see `window-normal-size').
3794 Note: This function does not operate on any child windows of WINDOW. */)
3795 (Lisp_Object window, Lisp_Object size)
3797 wset_new_normal (decode_valid_window (window), size);
3798 return size;
3801 /* Return true if setting w->pixel_height (w->pixel_width if HORFLAG)
3802 to w->new_pixel would result in correct heights (widths)
3803 for window W and recursively all child windows of W.
3805 Note: This function does not check any of `window-fixed-size-p',
3806 `window-min-height' or `window-min-width'. It does check that window
3807 sizes do not drop below one line (two columns). */
3808 static bool
3809 window_resize_check (struct window *w, bool horflag)
3811 struct frame *f = XFRAME (w->frame);
3812 struct window *c;
3814 if (WINDOW_VERTICAL_COMBINATION_P (w))
3815 /* W is a vertical combination. */
3817 c = XWINDOW (w->contents);
3818 if (horflag)
3819 /* All child windows of W must have the same width as W. */
3821 while (c)
3823 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3824 || !window_resize_check (c, horflag))
3825 return false;
3827 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3830 return true;
3832 else
3833 /* The sum of the heights of the child windows of W must equal
3834 W's height. */
3836 int remaining_pixels = XINT (w->new_pixel);
3838 while (c)
3840 if (!window_resize_check (c, horflag))
3841 return false;
3843 remaining_pixels -= XINT (c->new_pixel);
3844 if (remaining_pixels < 0)
3845 return false;
3846 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3849 return remaining_pixels == 0;
3852 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3853 /* W is a horizontal combination. */
3855 c = XWINDOW (w->contents);
3856 if (horflag)
3857 /* The sum of the widths of the child windows of W must equal W's
3858 width. */
3860 int remaining_pixels = XINT (w->new_pixel);
3862 while (c)
3864 if (!window_resize_check (c, horflag))
3865 return false;
3867 remaining_pixels -= XINT (c->new_pixel);
3868 if (remaining_pixels < 0)
3869 return false;
3870 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3873 return remaining_pixels == 0;
3875 else
3876 /* All child windows of W must have the same height as W. */
3878 while (c)
3880 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3881 || !window_resize_check (c, horflag))
3882 return false;
3884 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3887 return true;
3890 else
3891 /* A leaf window. Make sure it's not too small. The following
3892 hardcodes the values of `window-safe-min-width' (2) and
3893 `window-safe-min-height' (1) which are defined in window.el. */
3894 return (XINT (w->new_pixel) >= (horflag
3895 ? (2 * FRAME_COLUMN_WIDTH (f))
3896 : FRAME_LINE_HEIGHT (f)));
3900 /* Set w->pixel_height (w->pixel_width if HORFLAG) to
3901 w->new_pixel for window W and recursively all child windows of W.
3902 Also calculate and assign the new vertical (horizontal) pixel start
3903 positions of each of these windows.
3905 This function does not perform any error checks. Make sure you have
3906 run window_resize_check on W before applying this function. */
3907 static void
3908 window_resize_apply (struct window *w, bool horflag)
3910 struct window *c;
3911 int edge;
3912 int unit = (horflag
3913 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3914 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3916 /* Note: Assigning new_normal requires that the new total size of the
3917 parent window has been set *before*. */
3918 if (horflag)
3920 w->pixel_width = XFASTINT (w->new_pixel);
3921 w->total_cols = w->pixel_width / unit;
3922 if (NUMBERP (w->new_normal))
3923 wset_normal_cols (w, w->new_normal);
3925 edge = w->pixel_left;
3927 else
3929 w->pixel_height = XFASTINT (w->new_pixel);
3930 w->total_lines = w->pixel_height / unit;
3931 if (NUMBERP (w->new_normal))
3932 wset_normal_lines (w, w->new_normal);
3934 edge = w->pixel_top;
3937 if (WINDOW_VERTICAL_COMBINATION_P (w))
3938 /* W is a vertical combination. */
3940 c = XWINDOW (w->contents);
3941 while (c)
3943 if (horflag)
3945 c->pixel_left = edge;
3946 c->left_col = edge / unit;
3948 else
3950 c->pixel_top = edge;
3951 c->top_line = edge / unit;
3953 window_resize_apply (c, horflag);
3954 if (!horflag)
3955 edge = edge + c->pixel_height;
3957 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3960 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3961 /* W is a horizontal combination. */
3963 c = XWINDOW (w->contents);
3964 while (c)
3966 if (horflag)
3968 c->pixel_left = edge;
3969 c->left_col = edge / unit;
3971 else
3973 c->pixel_top = edge;
3974 c->top_line = edge / unit;
3977 window_resize_apply (c, horflag);
3978 if (horflag)
3979 edge = edge + c->pixel_width;
3981 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3984 else
3985 /* Bug#15957. */
3986 w->window_end_valid = false;
3990 /* Set w->total_lines (w->total_cols if HORFLAG) to
3991 w->new_total for window W and recursively all child windows of W.
3992 Also calculate and assign the new vertical (horizontal) start
3993 positions of each of these windows. */
3994 static void
3995 window_resize_apply_total (struct window *w, bool horflag)
3997 struct window *c;
3998 int edge;
4000 /* Note: Assigning new_normal requires that the new total size of the
4001 parent window has been set *before*. */
4002 if (horflag)
4004 w->total_cols = XFASTINT (w->new_total);
4005 edge = w->left_col;
4007 else
4009 w->total_lines = XFASTINT (w->new_total);
4010 edge = w->top_line;
4013 if (WINDOW_VERTICAL_COMBINATION_P (w))
4014 /* W is a vertical combination. */
4016 c = XWINDOW (w->contents);
4017 while (c)
4019 if (horflag)
4020 c->left_col = edge;
4021 else
4022 c->top_line = edge;
4024 window_resize_apply_total (c, horflag);
4025 if (!horflag)
4026 edge = edge + c->total_lines;
4028 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4031 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4032 /* W is a horizontal combination. */
4034 c = XWINDOW (w->contents);
4035 while (c)
4037 if (horflag)
4038 c->left_col = edge;
4039 else
4040 c->top_line = edge;
4042 window_resize_apply_total (c, horflag);
4043 if (horflag)
4044 edge = edge + c->total_cols;
4046 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4051 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4052 doc: /* Apply requested size values for window-tree of FRAME.
4053 If FRAME is omitted or nil, it defaults to the selected frame.
4055 Optional argument HORIZONTAL omitted or nil means apply requested
4056 height values. HORIZONTAL non-nil means apply requested width values.
4058 The requested size values are those set by `set-window-new-pixel' and
4059 `set-window-new-normal'. This function checks whether the requested
4060 values sum up to a valid window layout, recursively assigns the new
4061 sizes of all child windows and calculates and assigns the new start
4062 positions of these windows.
4064 Return t if the requested values have been applied correctly, nil
4065 otherwise.
4067 Note: This function does not check any of `window-fixed-size-p',
4068 `window-min-height' or `window-min-width'. All these checks have to
4069 be applied on the Elisp level. */)
4070 (Lisp_Object frame, Lisp_Object horizontal)
4072 struct frame *f = decode_live_frame (frame);
4073 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4074 bool horflag = !NILP (horizontal);
4076 if (!window_resize_check (r, horflag)
4077 || (XINT (r->new_pixel)
4078 != (horflag ? r->pixel_width : r->pixel_height)))
4079 return Qnil;
4081 block_input ();
4082 window_resize_apply (r, horflag);
4084 fset_redisplay (f);
4085 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4087 adjust_frame_glyphs (f);
4088 unblock_input ();
4090 return Qt;
4094 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4095 doc: /* Apply requested total size values for window-tree of FRAME.
4096 If FRAME is omitted or nil, it defaults to the selected frame.
4098 This function does not assign pixel or normal size values. You should
4099 have run `window-resize-apply' before running this.
4101 Optional argument HORIZONTAL omitted or nil means apply requested
4102 height values. HORIZONTAL non-nil means apply requested width
4103 values. */)
4104 (Lisp_Object frame, Lisp_Object horizontal)
4106 struct frame *f = decode_live_frame (frame);
4107 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4109 block_input ();
4110 /* Necessary when deleting the top-/or leftmost window. */
4111 r->left_col = 0;
4112 r->top_line = FRAME_TOP_MARGIN (f);
4113 window_resize_apply_total (r, !NILP (horizontal));
4114 /* Handle the mini window. */
4115 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4117 struct window *m = XWINDOW (f->minibuffer_window);
4119 if (NILP (horizontal))
4121 m->top_line = r->top_line + r->total_lines;
4122 m->total_lines = XFASTINT (m->new_total);
4124 else
4125 m->total_cols = XFASTINT (m->new_total);
4128 unblock_input ();
4130 return Qt;
4134 /* Resize frame F's windows when number of lines of F is set to SIZE.
4135 HORFLAG means resize windows when number of columns of F is set to
4136 SIZE. PIXELWISE means to interpret SIZE as pixels. */
4137 void
4138 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4140 Lisp_Object root = f->root_window;
4141 struct window *r = XWINDOW (root);
4142 Lisp_Object mini = f->minibuffer_window;
4143 struct window *m;
4144 /* old_size is the old size of the frame's root window. */
4145 int old_size = horflag ? r->total_cols : r->total_lines;
4146 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4147 int old_pixel_top = r->pixel_top;
4148 /* new_size is the new size of the frame's root window. */
4149 int new_size, new_pixel_size;
4150 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4152 /* Don't let the size drop below one unit. This is more comforting
4153 when we are called from x_set_tool_bar_lines since the latter may
4154 have implicitly given us a zero or negative height. */
4155 if (pixelwise)
4157 /* Note: This does not include the size for internal borders
4158 since these are not part of the frame's text area. */
4159 new_pixel_size = max (horflag
4160 ? size
4161 : (size
4162 - ((FRAME_HAS_MINIBUF_P (f)
4163 && !FRAME_MINIBUF_ONLY_P (f))
4164 ? FRAME_LINE_HEIGHT (f) : 0)),
4165 unit);
4166 new_size = new_pixel_size / unit;
4168 else
4170 new_size = max (size - (!horflag
4171 && FRAME_HAS_MINIBUF_P (f)
4172 && !FRAME_MINIBUF_ONLY_P (f)),
4174 new_pixel_size = new_size * unit;
4177 r->top_line = FRAME_TOP_MARGIN (f);
4178 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4180 if (new_pixel_size == old_pixel_size
4181 && r->pixel_top == old_pixel_top)
4183 else if (WINDOW_LEAF_P (r))
4184 /* For a leaf root window just set the size. */
4185 if (horflag)
4187 r->total_cols = new_size;
4188 r->pixel_width = new_pixel_size;
4190 else
4192 r->total_lines = new_size;
4193 r->pixel_height = new_pixel_size;
4195 else
4197 Lisp_Object delta;
4199 if (pixelwise)
4200 XSETINT (delta, new_pixel_size - old_pixel_size);
4201 else
4202 XSETINT (delta, new_size - old_size);
4204 /* Try a "normal" resize first. */
4205 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4206 pixelwise ? Qt : Qnil);
4207 if (window_resize_check (r, horflag)
4208 && new_pixel_size == XINT (r->new_pixel))
4210 window_resize_apply (r, horflag);
4211 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4213 else
4215 /* Try with "reasonable" minimum sizes next. */
4216 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4217 pixelwise ? Qt : Qnil);
4218 if (window_resize_check (r, horflag)
4219 && new_pixel_size == XINT (r->new_pixel))
4221 window_resize_apply (r, horflag);
4222 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4223 #if false /* Let's try without safe sizes and/or killing other windows. */
4225 else
4227 /* Finally, try with "safe" minimum sizes. */
4228 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4229 pixelwise ? Qt : Qnil);
4230 if (window_resize_check (r, horflag)
4231 && new_pixel_size == XINT (r->new_pixel))
4233 window_resize_apply (r, horflag);
4234 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4236 else
4238 /* We lost. Delete all windows but the frame's
4239 selected one. */
4240 root = f->selected_window;
4241 Fdelete_other_windows_internal (root, Qnil);
4242 if (horflag)
4244 XWINDOW (root)->total_cols = new_size;
4245 XWINDOW (root)->pixel_width = new_pixel_size;
4247 else
4249 XWINDOW (root)->total_lines = new_size;
4250 XWINDOW (root)->pixel_height = new_pixel_size;
4253 #endif /* false */
4258 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4260 m = XWINDOW (mini);
4261 if (horflag)
4263 m->total_cols = size;
4264 m->pixel_width = new_pixel_size;
4266 else
4268 /* Are we sure we always want 1 line here? */
4269 m->total_lines = 1;
4270 m->pixel_height = FRAME_LINE_HEIGHT (f);
4271 m->top_line = r->top_line + r->total_lines;
4272 m->pixel_top = r->pixel_top + r->pixel_height;
4276 fset_redisplay (f);
4280 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4281 doc: /* Split window OLD.
4282 Second argument PIXEL-SIZE specifies the number of pixels of the
4283 new window. It must be a positive integer.
4285 Third argument SIDE nil (or `below') specifies that the new window shall
4286 be located below WINDOW. SIDE `above' means the new window shall be
4287 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4288 height of the new window including space reserved for the mode and/or
4289 header line.
4291 SIDE t (or `right') specifies that the new window shall be located on
4292 the right side of WINDOW. SIDE `left' means the new window shall be
4293 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4294 width of the new window including space reserved for fringes and the
4295 scrollbar or a divider column.
4297 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4298 according to the SIDE argument.
4300 The new pixel and normal sizes of all involved windows must have been
4301 set correctly. See the code of `split-window' for how this is done. */)
4302 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4304 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4305 parent window or an internal window we have to install as OLD's new
4306 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4307 provided OLD is a leaf window, or to the frame's selected window.
4308 NEW (*n) is the new window created with some parameters taken from
4309 REFERENCE (*r). */
4310 Lisp_Object new, frame, reference;
4311 struct window *o, *p, *n, *r, *c;
4312 struct frame *f;
4313 bool horflag
4314 /* HORFLAG is true when we split side-by-side, false otherwise. */
4315 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4317 CHECK_WINDOW (old);
4318 o = XWINDOW (old);
4319 frame = WINDOW_FRAME (o);
4320 f = XFRAME (frame);
4322 CHECK_NUMBER (pixel_size);
4323 EMACS_INT total_size
4324 = XINT (pixel_size) / (horflag
4325 ? FRAME_COLUMN_WIDTH (f)
4326 : FRAME_LINE_HEIGHT (f));
4328 /* Set combination_limit if we have to make a new parent window.
4329 We do that if either `window-combination-limit' is t, or OLD has no
4330 parent, or OLD is ortho-combined. */
4331 bool combination_limit
4332 = (EQ (Vwindow_combination_limit, Qt)
4333 || NILP (o->parent)
4334 || (horflag
4335 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4336 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent))));
4338 /* We need a live reference window to initialize some parameters. */
4339 if (WINDOW_LIVE_P (old))
4340 /* OLD is live, use it as reference window. */
4341 reference = old;
4342 else
4343 /* Use the frame's selected window as reference window. */
4344 reference = FRAME_SELECTED_WINDOW (f);
4345 r = XWINDOW (reference);
4347 /* The following bugs are caught by `split-window'. */
4348 if (MINI_WINDOW_P (o))
4349 error ("Attempt to split minibuffer window");
4350 else if (total_size < (horflag ? 2 : 1))
4351 error ("Size of new window too small (after split)");
4352 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4353 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4354 proportionally. */
4356 p = XWINDOW (o->parent);
4357 /* Temporarily pretend we split the parent window. */
4358 wset_new_pixel
4359 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4360 - XINT (pixel_size)));
4361 if (!window_resize_check (p, horflag))
4362 error ("Window sizes don't fit");
4363 else
4364 /* Undo the temporary pretension. */
4365 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4367 else
4369 if (!window_resize_check (o, horflag))
4370 error ("Resizing old window failed");
4371 else if (XINT (pixel_size) + XINT (o->new_pixel)
4372 != (horflag ? o->pixel_width : o->pixel_height))
4373 error ("Sum of sizes of old and new window don't fit");
4376 /* This is our point of no return. */
4377 if (combination_limit)
4379 /* Save the old value of o->normal_cols/lines. It gets corrupted
4380 by make_parent_window and we need it below for assigning it to
4381 p->new_normal. */
4382 Lisp_Object new_normal
4383 = horflag ? o->normal_cols : o->normal_lines;
4385 make_parent_window (old, horflag);
4386 p = XWINDOW (o->parent);
4387 if (EQ (Vwindow_combination_limit, Qt))
4388 /* Store t in the new parent's combination_limit slot to avoid
4389 that its children get merged into another window. */
4390 wset_combination_limit (p, Qt);
4391 /* These get applied below. */
4392 wset_new_pixel
4393 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4394 wset_new_total
4395 (p, make_number (horflag ? o->total_cols : o->total_lines));
4396 wset_new_normal (p, new_normal);
4398 else
4399 p = XWINDOW (o->parent);
4401 fset_redisplay (f);
4402 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4403 new = make_window ();
4404 n = XWINDOW (new);
4405 wset_frame (n, frame);
4406 wset_parent (n, o->parent);
4408 if (EQ (side, Qabove) || EQ (side, Qleft))
4410 wset_prev (n, o->prev);
4411 if (NILP (n->prev))
4412 wset_combination (p, horflag, new);
4413 else
4414 wset_next (XWINDOW (n->prev), new);
4415 wset_next (n, old);
4416 wset_prev (o, new);
4418 else
4420 wset_next (n, o->next);
4421 if (!NILP (n->next))
4422 wset_prev (XWINDOW (n->next), new);
4423 wset_prev (n, old);
4424 wset_next (o, new);
4427 n->window_end_valid = false;
4428 n->last_cursor_vpos = 0;
4430 /* Get special geometry settings from reference window. */
4431 n->left_margin_cols = r->left_margin_cols;
4432 n->right_margin_cols = r->right_margin_cols;
4433 n->left_fringe_width = r->left_fringe_width;
4434 n->right_fringe_width = r->right_fringe_width;
4435 n->fringes_outside_margins = r->fringes_outside_margins;
4436 n->scroll_bar_width = r->scroll_bar_width;
4437 n->scroll_bar_height = r->scroll_bar_height;
4438 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4439 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4441 /* Directly assign orthogonal coordinates and sizes. */
4442 if (horflag)
4444 n->pixel_top = o->pixel_top;
4445 n->top_line = o->top_line;
4446 n->pixel_height = o->pixel_height;
4447 n->total_lines = o->total_lines;
4449 else
4451 n->pixel_left = o->pixel_left;
4452 n->left_col = o->left_col;
4453 n->pixel_width = o->pixel_width;
4454 n->total_cols = o->total_cols;
4457 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4458 get them ready here. */
4459 wset_new_pixel (n, pixel_size);
4460 EMACS_INT sum = 0;
4461 c = XWINDOW (p->contents);
4462 while (c)
4464 if (c != n)
4465 sum = sum + XINT (c->new_total);
4466 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4468 wset_new_total (n, make_number ((horflag
4469 ? p->total_cols
4470 : p->total_lines)
4471 - sum));
4472 wset_new_normal (n, normal_size);
4474 block_input ();
4475 window_resize_apply (p, horflag);
4476 adjust_frame_glyphs (f);
4477 /* Set buffer of NEW to buffer of reference window. Don't run
4478 any hooks. */
4479 set_window_buffer (new, r->contents, false, true);
4480 unblock_input ();
4482 /* Maybe we should run the scroll functions in Elisp (which already
4483 runs the configuration change hook). */
4484 if (! NILP (Vwindow_scroll_functions))
4485 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4486 Fmarker_position (n->start));
4487 /* Return NEW. */
4488 return new;
4492 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4493 doc: /* Remove WINDOW from its frame.
4494 WINDOW defaults to the selected window. Return nil.
4495 Signal an error when WINDOW is the only window on its frame. */)
4496 (Lisp_Object window)
4498 Lisp_Object parent, sibling, frame, root;
4499 struct window *w, *p, *s, *r;
4500 struct frame *f;
4501 bool horflag, before_sibling = false;
4503 w = decode_any_window (window);
4504 XSETWINDOW (window, w);
4505 if (NILP (w->contents))
4506 /* It's a no-op to delete an already deleted window. */
4507 return Qnil;
4509 parent = w->parent;
4510 if (NILP (parent))
4511 /* Never delete a minibuffer or frame root window. */
4512 error ("Attempt to delete minibuffer or sole ordinary window");
4513 else if (NILP (w->prev) && NILP (w->next))
4514 /* Rather bow out here, this case should be handled on the Elisp
4515 level. */
4516 error ("Attempt to delete sole window of parent");
4518 p = XWINDOW (parent);
4519 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4521 frame = WINDOW_FRAME (w);
4522 f = XFRAME (frame);
4524 root = FRAME_ROOT_WINDOW (f);
4525 r = XWINDOW (root);
4527 /* Unlink WINDOW from window tree. */
4528 if (NILP (w->prev))
4529 /* Get SIBLING below (on the right of) WINDOW. */
4531 /* before_sibling means WINDOW is the first child of its
4532 parent and thus before the sibling. */
4533 before_sibling = true;
4534 sibling = w->next;
4535 s = XWINDOW (sibling);
4536 wset_prev (s, Qnil);
4537 wset_combination (p, horflag, sibling);
4539 else
4540 /* Get SIBLING above (on the left of) WINDOW. */
4542 sibling = w->prev;
4543 s = XWINDOW (sibling);
4544 wset_next (s, w->next);
4545 if (!NILP (s->next))
4546 wset_prev (XWINDOW (s->next), sibling);
4549 if (window_resize_check (r, horflag)
4550 && (XINT (r->new_pixel)
4551 == (horflag ? r->pixel_width : r->pixel_height)))
4552 /* We can delete WINDOW now. */
4555 /* Block input. */
4556 block_input ();
4557 window_resize_apply (p, horflag);
4558 /* If this window is referred to by the dpyinfo's mouse
4559 highlight, invalidate that slot to be safe (Bug#9904). */
4560 if (!FRAME_INITIAL_P (f))
4562 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4564 if (EQ (hlinfo->mouse_face_window, window))
4565 hlinfo->mouse_face_window = Qnil;
4568 fset_redisplay (f);
4569 Vwindow_list = Qnil;
4570 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4572 wset_next (w, Qnil); /* Don't delete w->next too. */
4573 free_window_matrices (w);
4575 if (WINDOWP (w->contents))
4577 delete_all_child_windows (w->contents);
4578 wset_combination (w, false, Qnil);
4580 else
4582 unshow_buffer (w);
4583 unchain_marker (XMARKER (w->pointm));
4584 unchain_marker (XMARKER (w->old_pointm));
4585 unchain_marker (XMARKER (w->start));
4586 wset_buffer (w, Qnil);
4589 if (NILP (s->prev) && NILP (s->next))
4590 /* A matrjoshka where SIBLING has become the only child of
4591 PARENT. */
4593 /* Put SIBLING into PARENT's place. */
4594 replace_window (parent, sibling, false);
4595 /* Have SIBLING inherit the following three slot values from
4596 PARENT (the combination_limit slot is not inherited). */
4597 wset_normal_cols (s, p->normal_cols);
4598 wset_normal_lines (s, p->normal_lines);
4599 /* Mark PARENT as deleted. */
4600 wset_combination (p, false, Qnil);
4601 /* Try to merge SIBLING into its new parent. */
4602 recombine_windows (sibling);
4605 adjust_frame_glyphs (f);
4607 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4608 /* We deleted the frame's selected window. */
4610 /* Use the frame's first window as fallback ... */
4611 Lisp_Object new_selected_window = Fframe_first_window (frame);
4612 /* ... but preferably use its most recently used window. */
4613 Lisp_Object mru_window;
4615 /* `get-mru-window' might fail for some reason so play it safe
4616 - promote the first window _without recording it_ first. */
4617 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4618 Fselect_window (new_selected_window, Qt);
4619 else
4620 fset_selected_window (f, new_selected_window);
4622 unblock_input ();
4624 /* Now look whether `get-mru-window' gets us something. */
4625 mru_window = call1 (Qget_mru_window, frame);
4626 if (WINDOW_LIVE_P (mru_window)
4627 && EQ (XWINDOW (mru_window)->frame, frame))
4628 new_selected_window = mru_window;
4630 /* If all ended up well, we now promote the mru window. */
4631 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4632 Fselect_window (new_selected_window, Qnil);
4633 else
4634 fset_selected_window (f, new_selected_window);
4636 else
4637 unblock_input ();
4639 /* Must be run by the caller:
4640 run_window_configuration_change_hook (f); */
4642 else
4643 /* We failed: Relink WINDOW into window tree. */
4645 if (before_sibling)
4647 wset_prev (s, window);
4648 wset_combination (p, horflag, window);
4650 else
4652 wset_next (s, window);
4653 if (!NILP (w->next))
4654 wset_prev (XWINDOW (w->next), window);
4656 error ("Deletion failed");
4659 return Qnil;
4662 /***********************************************************************
4663 Resizing Mini-Windows
4664 ***********************************************************************/
4666 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4667 can. */
4668 void
4669 grow_mini_window (struct window *w, int delta, bool pixelwise)
4671 struct frame *f = XFRAME (w->frame);
4672 struct window *r;
4673 Lisp_Object root, height;
4674 int line_height, pixel_height;
4676 eassert (MINI_WINDOW_P (w));
4677 eassert (delta >= 0);
4679 if (delta > 0)
4681 root = FRAME_ROOT_WINDOW (f);
4682 r = XWINDOW (root);
4683 height = call3 (Qwindow_resize_root_window_vertically,
4684 root, make_number (- delta), pixelwise ? Qt : Qnil);
4685 if (INTEGERP (height) && window_resize_check (r, false))
4687 block_input ();
4688 window_resize_apply (r, false);
4690 if (pixelwise)
4692 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4693 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4695 else
4697 line_height = min (-XINT (height),
4698 ((INT_MAX - w->pixel_height)
4699 / FRAME_LINE_HEIGHT (f)));
4700 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4703 /* Grow the mini-window. */
4704 w->pixel_top = r->pixel_top + r->pixel_height;
4705 w->top_line = r->top_line + r->total_lines;
4706 /* Make sure the mini-window has always at least one line. */
4707 w->pixel_height = max (w->pixel_height + pixel_height,
4708 FRAME_LINE_HEIGHT (f));
4709 w->total_lines = max (w->total_lines + line_height, 1);
4711 /* Enforce full redisplay of the frame. */
4712 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4713 fset_redisplay (f);
4714 adjust_frame_glyphs (f);
4715 unblock_input ();
4720 /* Shrink mini-window W to one line. */
4721 void
4722 shrink_mini_window (struct window *w, bool pixelwise)
4724 struct frame *f = XFRAME (w->frame);
4725 struct window *r;
4726 Lisp_Object root, delta;
4727 EMACS_INT height, unit;
4729 eassert (MINI_WINDOW_P (w));
4731 height = pixelwise ? w->pixel_height : w->total_lines;
4732 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4733 if (height > unit)
4735 root = FRAME_ROOT_WINDOW (f);
4736 r = XWINDOW (root);
4737 delta = call3 (Qwindow_resize_root_window_vertically,
4738 root, make_number (height - unit),
4739 pixelwise ? Qt : Qnil);
4740 if (INTEGERP (delta) && window_resize_check (r, false))
4742 block_input ();
4743 window_resize_apply (r, false);
4745 /* Shrink the mini-window. */
4746 w->top_line = r->top_line + r->total_lines;
4747 w->total_lines = 1;
4748 w->pixel_top = r->pixel_top + r->pixel_height;
4749 w->pixel_height = FRAME_LINE_HEIGHT (f);
4750 /* Enforce full redisplay of the frame. */
4751 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4752 fset_redisplay (f);
4753 adjust_frame_glyphs (f);
4754 unblock_input ();
4756 /* If the above failed for whatever strange reason we must make a
4757 one window frame here. The same routine will be needed when
4758 shrinking the frame (and probably when making the initial
4759 *scratch* window). For the moment leave things as they are. */
4763 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4764 doc: /* Resize minibuffer window WINDOW. */)
4765 (Lisp_Object window)
4767 struct window *w = XWINDOW (window);
4768 struct window *r;
4769 struct frame *f;
4770 int height;
4772 CHECK_WINDOW (window);
4773 f = XFRAME (w->frame);
4775 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4776 error ("Not a valid minibuffer window");
4777 else if (FRAME_MINIBUF_ONLY_P (f))
4778 error ("Cannot resize a minibuffer-only frame");
4780 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4781 height = r->pixel_height + w->pixel_height;
4782 if (window_resize_check (r, false)
4783 && XINT (w->new_pixel) > 0
4784 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4786 block_input ();
4787 window_resize_apply (r, false);
4789 w->pixel_height = XFASTINT (w->new_pixel);
4790 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4791 w->pixel_top = r->pixel_top + r->pixel_height;
4792 w->top_line = r->top_line + r->total_lines;
4794 fset_redisplay (f);
4795 FRAME_WINDOW_SIZES_CHANGED (f) = true;
4796 adjust_frame_glyphs (f);
4797 unblock_input ();
4798 return Qt;
4800 else
4801 error ("Failed to resize minibuffer window");
4804 /* Mark window cursors off for all windows in the window tree rooted
4805 at W by setting their phys_cursor_on_p flag to zero. Called from
4806 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4807 the frame are cleared. */
4809 void
4810 mark_window_cursors_off (struct window *w)
4812 while (w)
4814 if (WINDOWP (w->contents))
4815 mark_window_cursors_off (XWINDOW (w->contents));
4816 else
4817 w->phys_cursor_on_p = false;
4819 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4824 /* Return number of lines of text (not counting mode lines) in W. */
4827 window_internal_height (struct window *w)
4829 int ht = w->total_lines;
4831 if (!MINI_WINDOW_P (w))
4833 if (!NILP (w->parent)
4834 || WINDOWP (w->contents)
4835 || !NILP (w->next)
4836 || !NILP (w->prev)
4837 || WINDOW_WANTS_MODELINE_P (w))
4838 --ht;
4840 if (WINDOW_WANTS_HEADER_LINE_P (w))
4841 --ht;
4844 return ht;
4848 /************************************************************************
4849 Window Scrolling
4850 ***********************************************************************/
4852 /* Scroll contents of window WINDOW up. If WHOLE, scroll
4853 N screen-fulls, which is defined as the height of the window minus
4854 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4855 instead. Negative values of N mean scroll down. NOERROR
4856 means don't signal an error if we try to move over BEGV or ZV,
4857 respectively. */
4859 static void
4860 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, bool noerror)
4862 ptrdiff_t count = SPECPDL_INDEX ();
4864 immediate_quit = true;
4865 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4867 wset_redisplay (XWINDOW (window));
4869 if (whole && Vfast_but_imprecise_scrolling)
4870 specbind (Qfontification_functions, Qnil);
4872 /* If we must, use the pixel-based version which is much slower than
4873 the line-based one but can handle varying line heights. */
4874 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4875 window_scroll_pixel_based (window, n, whole, noerror);
4876 else
4877 window_scroll_line_based (window, n, whole, noerror);
4879 unbind_to (count, Qnil);
4881 /* Bug#15957. */
4882 XWINDOW (window)->window_end_valid = false;
4883 immediate_quit = false;
4887 /* Implementation of window_scroll that works based on pixel line
4888 heights. See the comment of window_scroll for parameter
4889 descriptions. */
4891 static void
4892 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
4894 struct it it;
4895 struct window *w = XWINDOW (window);
4896 struct text_pos start;
4897 int this_scroll_margin;
4898 /* True if we fiddled the window vscroll field without really scrolling. */
4899 bool vscrolled = false;
4900 int x, y, rtop, rbot, rowh, vpos;
4901 void *itdata = NULL;
4902 int window_total_lines;
4903 int frame_line_height = default_line_pixel_height (w);
4904 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4905 Fwindow_old_point (window)));
4907 SET_TEXT_POS_FROM_MARKER (start, w->start);
4908 /* Scrolling a minibuffer window via scroll bar when the echo area
4909 shows long text sometimes resets the minibuffer contents behind
4910 our backs. */
4911 if (CHARPOS (start) > ZV)
4912 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4914 /* If PT is not visible in WINDOW, move back one half of
4915 the screen. Allow PT to be partially visible, otherwise
4916 something like (scroll-down 1) with PT in the line before
4917 the partially visible one would recenter. */
4919 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4921 itdata = bidi_shelve_cache ();
4922 /* Move backward half the height of the window. Performance note:
4923 vmotion used here is about 10% faster, but would give wrong
4924 results for variable height lines. */
4925 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4926 it.current_y = it.last_visible_y;
4927 move_it_vertically_backward (&it, window_box_height (w) / 2);
4929 /* The function move_iterator_vertically may move over more than
4930 the specified y-distance. If it->w is small, e.g. a
4931 mini-buffer window, we may end up in front of the window's
4932 display area. Start displaying at the start of the line
4933 containing PT in this case. */
4934 if (it.current_y <= 0)
4936 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4937 move_it_vertically_backward (&it, 0);
4938 it.current_y = 0;
4941 start = it.current.pos;
4942 bidi_unshelve_cache (itdata, false);
4944 else if (auto_window_vscroll_p)
4946 if (rtop || rbot) /* Partially visible. */
4948 int px;
4949 int dy = frame_line_height;
4950 /* In the below we divide the window box height by the
4951 frame's line height to make the result predictable when
4952 the window box is not an integral multiple of the line
4953 height. This is important to ensure we get back to the
4954 same position when scrolling up, then down. */
4955 if (whole)
4956 dy = max ((window_box_height (w) / dy
4957 - next_screen_context_lines) * dy,
4958 dy);
4959 dy *= n;
4961 if (n < 0)
4963 /* Only vscroll backwards if already vscrolled forwards. */
4964 if (w->vscroll < 0 && rtop > 0)
4966 px = max (0, -w->vscroll - min (rtop, -dy));
4967 Fset_window_vscroll (window, make_number (px), Qt);
4968 return;
4971 if (n > 0)
4973 /* Do vscroll if already vscrolled or only display line. */
4974 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4976 px = max (0, -w->vscroll + min (rbot, dy));
4977 Fset_window_vscroll (window, make_number (px), Qt);
4978 return;
4981 /* Maybe modify window start instead of scrolling. */
4982 if (rbot > 0 || w->vscroll < 0)
4984 ptrdiff_t spos;
4986 Fset_window_vscroll (window, make_number (0), Qt);
4987 /* If there are other text lines above the current row,
4988 move window start to current row. Else to next row. */
4989 if (rbot > 0)
4990 spos = XINT (Fline_beginning_position (Qnil));
4991 else
4992 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4993 set_marker_restricted (w->start, make_number (spos),
4994 w->contents);
4995 w->start_at_line_beg = true;
4996 w->update_mode_line = true;
4997 /* Set force_start so that redisplay_window will run the
4998 window-scroll-functions. */
4999 w->force_start = true;
5000 return;
5004 /* Cancel previous vscroll. */
5005 Fset_window_vscroll (window, make_number (0), Qt);
5008 itdata = bidi_shelve_cache ();
5009 /* If scroll_preserve_screen_position is non-nil, we try to set
5010 point in the same window line as it is now, so get that line. */
5011 if (!NILP (Vscroll_preserve_screen_position))
5013 /* We preserve the goal pixel coordinate across consecutive
5014 calls to scroll-up, scroll-down and other commands that
5015 have the `scroll-command' property. This avoids the
5016 possibility of point becoming "stuck" on a tall line when
5017 scrolling by one line. */
5018 if (window_scroll_pixel_based_preserve_y < 0
5019 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5020 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5022 start_display (&it, w, start);
5023 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5024 window_scroll_pixel_based_preserve_y = it.current_y;
5025 window_scroll_pixel_based_preserve_x = it.current_x;
5028 else
5029 window_scroll_pixel_based_preserve_y
5030 = window_scroll_pixel_based_preserve_x = -1;
5032 /* Move iterator it from start the specified distance forward or
5033 backward. The result is the new window start. */
5034 start_display (&it, w, start);
5035 if (whole)
5037 ptrdiff_t start_pos = IT_CHARPOS (it);
5038 int dy = frame_line_height;
5039 /* In the below we divide the window box height by the frame's
5040 line height to make the result predictable when the window
5041 box is not an integral multiple of the line height. This is
5042 important to ensure we get back to the same position when
5043 scrolling up, then down. */
5044 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
5045 dy) * n;
5047 /* Note that move_it_vertically always moves the iterator to the
5048 start of a line. So, if the last line doesn't have a newline,
5049 we would end up at the start of the line ending at ZV. */
5050 if (dy <= 0)
5052 move_it_vertically_backward (&it, -dy);
5053 /* Ensure we actually do move, e.g. in case we are currently
5054 looking at an image that is taller that the window height. */
5055 while (start_pos == IT_CHARPOS (it)
5056 && start_pos > BEGV)
5057 move_it_by_lines (&it, -1);
5059 else if (dy > 0)
5061 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5062 MOVE_TO_POS | MOVE_TO_Y);
5063 /* Ensure we actually do move, e.g. in case we are currently
5064 looking at an image that is taller that the window height. */
5065 while (start_pos == IT_CHARPOS (it)
5066 && start_pos < ZV)
5067 move_it_by_lines (&it, 1);
5070 else
5071 move_it_by_lines (&it, n);
5073 /* We failed if we find ZV is already on the screen (scrolling up,
5074 means there's nothing past the end), or if we can't start any
5075 earlier (scrolling down, means there's nothing past the top). */
5076 if ((n > 0 && IT_CHARPOS (it) == ZV)
5077 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5079 if (IT_CHARPOS (it) == ZV)
5081 if (it.current_y < it.last_visible_y
5082 && (it.current_y + it.max_ascent + it.max_descent
5083 > it.last_visible_y))
5085 /* The last line was only partially visible, make it fully
5086 visible. */
5087 w->vscroll = (it.last_visible_y
5088 - it.current_y + it.max_ascent + it.max_descent);
5089 adjust_frame_glyphs (it.f);
5091 else
5093 bidi_unshelve_cache (itdata, false);
5094 if (noerror)
5095 return;
5096 else if (n < 0) /* could happen with empty buffers */
5097 xsignal0 (Qbeginning_of_buffer);
5098 else
5099 xsignal0 (Qend_of_buffer);
5102 else
5104 if (w->vscroll != 0)
5105 /* The first line was only partially visible, make it fully
5106 visible. */
5107 w->vscroll = 0;
5108 else
5110 bidi_unshelve_cache (itdata, false);
5111 if (noerror)
5112 return;
5113 else
5114 xsignal0 (Qbeginning_of_buffer);
5118 /* If control gets here, then we vscrolled. */
5120 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
5122 /* Don't try to change the window start below. */
5123 vscrolled = true;
5126 if (! vscrolled)
5128 ptrdiff_t pos = IT_CHARPOS (it);
5129 ptrdiff_t bytepos;
5131 /* If in the middle of a multi-glyph character move forward to
5132 the next character. */
5133 if (in_display_vector_p (&it))
5135 ++pos;
5136 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5139 /* Set the window start, and set up the window for redisplay. */
5140 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5141 IT_BYTEPOS (it));
5142 bytepos = marker_byte_position (w->start);
5143 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5144 w->update_mode_line = true;
5145 /* Set force_start so that redisplay_window will run the
5146 window-scroll-functions. */
5147 w->force_start = true;
5150 /* The rest of this function uses current_y in a nonstandard way,
5151 not including the height of the header line if any. */
5152 it.current_y = it.vpos = 0;
5154 /* Move PT out of scroll margins.
5155 This code wants current_y to be zero at the window start position
5156 even if there is a header line. */
5157 window_total_lines
5158 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5159 this_scroll_margin = max (0, scroll_margin);
5160 this_scroll_margin
5161 = min (this_scroll_margin, window_total_lines / 4);
5162 this_scroll_margin *= frame_line_height;
5164 if (n > 0)
5166 /* We moved the window start towards ZV, so PT may be now
5167 in the scroll margin at the top. */
5168 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5169 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5170 && (NILP (Vscroll_preserve_screen_position)
5171 || EQ (Vscroll_preserve_screen_position, Qt)))
5172 /* We found PT at a legitimate height. Leave it alone. */
5174 else if (window_scroll_pixel_based_preserve_y >= 0)
5176 /* If we have a header line, take account of it.
5177 This is necessary because we set it.current_y to 0, above. */
5178 move_it_to (&it, -1,
5179 window_scroll_pixel_based_preserve_x,
5180 (window_scroll_pixel_based_preserve_y
5181 - WINDOW_WANTS_HEADER_LINE_P (w)),
5182 -1, MOVE_TO_Y | MOVE_TO_X);
5183 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5185 else
5187 while (it.current_y < this_scroll_margin)
5189 int prev = it.current_y;
5190 move_it_by_lines (&it, 1);
5191 if (prev == it.current_y)
5192 break;
5194 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5197 else if (n < 0)
5199 ptrdiff_t charpos, bytepos;
5200 bool partial_p;
5202 /* Save our position, for the
5203 window_scroll_pixel_based_preserve_y case. */
5204 charpos = IT_CHARPOS (it);
5205 bytepos = IT_BYTEPOS (it);
5207 /* We moved the window start towards BEGV, so PT may be now
5208 in the scroll margin at the bottom. */
5209 move_it_to (&it, PT, -1,
5210 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5211 - this_scroll_margin - 1),
5213 MOVE_TO_POS | MOVE_TO_Y);
5215 /* Save our position, in case it's correct. */
5216 charpos = IT_CHARPOS (it);
5217 bytepos = IT_BYTEPOS (it);
5219 /* If PT is in the screen line at the last fully visible line,
5220 move_it_to will stop at X = 0 in that line, because the
5221 required Y coordinate is reached there. See if we can get to
5222 PT without descending lower in Y, and if we can, it means we
5223 reached PT before the scroll margin. */
5224 if (charpos != PT)
5226 struct it it2;
5227 void *it_data;
5229 it2 = it;
5230 it_data = bidi_shelve_cache ();
5231 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5232 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5234 charpos = IT_CHARPOS (it);
5235 bytepos = IT_BYTEPOS (it);
5236 bidi_unshelve_cache (it_data, true);
5238 else
5240 it = it2;
5241 bidi_unshelve_cache (it_data, false);
5245 /* See if point is on a partially visible line at the end. */
5246 if (it.what == IT_EOB)
5247 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5248 else
5250 move_it_by_lines (&it, 1);
5251 partial_p = it.current_y > it.last_visible_y;
5254 if (charpos == PT && !partial_p
5255 && (NILP (Vscroll_preserve_screen_position)
5256 || EQ (Vscroll_preserve_screen_position, Qt)))
5257 /* We found PT before we found the display margin, so PT is ok. */
5259 else if (window_scroll_pixel_based_preserve_y >= 0)
5261 SET_TEXT_POS_FROM_MARKER (start, w->start);
5262 start_display (&it, w, start);
5263 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5264 here because we called start_display again and did not
5265 alter it.current_y this time. */
5266 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5267 window_scroll_pixel_based_preserve_y, -1,
5268 MOVE_TO_Y | MOVE_TO_X);
5269 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5271 else
5273 if (partial_p)
5274 /* The last line was only partially visible, so back up two
5275 lines to make sure we're on a fully visible line. */
5277 move_it_by_lines (&it, -2);
5278 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5280 else
5281 /* No, the position we saved is OK, so use it. */
5282 SET_PT_BOTH (charpos, bytepos);
5285 bidi_unshelve_cache (itdata, false);
5287 if (adjust_old_pointm)
5288 Fset_marker (w->old_pointm,
5289 ((w == XWINDOW (selected_window))
5290 ? make_number (BUF_PT (XBUFFER (w->contents)))
5291 : Fmarker_position (w->pointm)),
5292 w->contents);
5296 /* Implementation of window_scroll that works based on screen lines.
5297 See the comment of window_scroll for parameter descriptions. */
5299 static void
5300 window_scroll_line_based (Lisp_Object window, int n, bool whole, bool noerror)
5302 struct window *w = XWINDOW (window);
5303 /* Fvertical_motion enters redisplay, which can trigger
5304 fontification, which in turn can modify buffer text (e.g., if the
5305 fontification functions replace escape sequences with faces, as
5306 in `grep-mode-font-lock-keywords'). So we use a marker to record
5307 the old point position, to prevent crashes in SET_PT_BOTH. */
5308 Lisp_Object opoint_marker = Fpoint_marker ();
5309 register ptrdiff_t pos, pos_byte;
5310 register int ht = window_internal_height (w);
5311 register Lisp_Object tem;
5312 bool lose;
5313 Lisp_Object bolp;
5314 ptrdiff_t startpos = marker_position (w->start);
5315 ptrdiff_t startbyte = marker_byte_position (w->start);
5316 Lisp_Object original_pos = Qnil;
5317 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5318 Fwindow_old_point (window)));
5320 /* If scrolling screen-fulls, compute the number of lines to
5321 scroll from the window's height. */
5322 if (whole)
5323 n *= max (1, ht - next_screen_context_lines);
5325 if (!NILP (Vscroll_preserve_screen_position))
5327 if (window_scroll_preserve_vpos <= 0
5328 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5329 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5331 struct position posit
5332 = *compute_motion (startpos, startbyte, 0, 0, false,
5333 PT, ht, 0, -1, w->hscroll, 0, w);
5335 window_scroll_preserve_vpos = posit.vpos;
5336 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5339 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5340 make_number (window_scroll_preserve_vpos));
5343 XSETFASTINT (tem, PT);
5344 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5346 if (NILP (tem))
5348 Fvertical_motion (make_number (- (ht / 2)), window, Qnil);
5349 startpos = PT;
5350 startbyte = PT_BYTE;
5353 SET_PT_BOTH (startpos, startbyte);
5354 lose = n < 0 && PT == BEGV;
5355 Fvertical_motion (make_number (n), window, Qnil);
5356 pos = PT;
5357 pos_byte = PT_BYTE;
5358 bolp = Fbolp ();
5359 SET_PT_BOTH (marker_position (opoint_marker),
5360 marker_byte_position (opoint_marker));
5362 if (lose)
5364 if (noerror)
5365 return;
5366 else
5367 xsignal0 (Qbeginning_of_buffer);
5370 if (pos < ZV)
5372 /* Don't use a scroll margin that is negative or too large. */
5373 int this_scroll_margin =
5374 max (0, min (scroll_margin, w->total_lines / 4));
5376 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5377 w->start_at_line_beg = !NILP (bolp);
5378 w->update_mode_line = true;
5379 /* Set force_start so that redisplay_window will run
5380 the window-scroll-functions. */
5381 w->force_start = true;
5383 if (!NILP (Vscroll_preserve_screen_position)
5384 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5386 SET_PT_BOTH (pos, pos_byte);
5387 Fvertical_motion (original_pos, window, Qnil);
5389 /* If we scrolled forward, put point enough lines down
5390 that it is outside the scroll margin. */
5391 else if (n > 0)
5393 int top_margin;
5395 if (this_scroll_margin > 0)
5397 SET_PT_BOTH (pos, pos_byte);
5398 Fvertical_motion (make_number (this_scroll_margin), window, Qnil);
5399 top_margin = PT;
5401 else
5402 top_margin = pos;
5404 if (top_margin <= marker_position (opoint_marker))
5405 SET_PT_BOTH (marker_position (opoint_marker),
5406 marker_byte_position (opoint_marker));
5407 else if (!NILP (Vscroll_preserve_screen_position))
5409 SET_PT_BOTH (pos, pos_byte);
5410 Fvertical_motion (original_pos, window, Qnil);
5412 else
5413 SET_PT (top_margin);
5415 else if (n < 0)
5417 int bottom_margin;
5419 /* If we scrolled backward, put point near the end of the window
5420 but not within the scroll margin. */
5421 SET_PT_BOTH (pos, pos_byte);
5422 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window,
5423 Qnil);
5424 if (XFASTINT (tem) == ht - this_scroll_margin)
5425 bottom_margin = PT;
5426 else
5427 bottom_margin = PT + 1;
5429 if (bottom_margin > marker_position (opoint_marker))
5430 SET_PT_BOTH (marker_position (opoint_marker),
5431 marker_byte_position (opoint_marker));
5432 else
5434 if (!NILP (Vscroll_preserve_screen_position))
5436 SET_PT_BOTH (pos, pos_byte);
5437 Fvertical_motion (original_pos, window, Qnil);
5439 else
5440 Fvertical_motion (make_number (-1), window, Qnil);
5444 else
5446 if (noerror)
5447 return;
5448 else
5449 xsignal0 (Qend_of_buffer);
5452 if (adjust_old_pointm)
5453 Fset_marker (w->old_pointm,
5454 ((w == XWINDOW (selected_window))
5455 ? make_number (BUF_PT (XBUFFER (w->contents)))
5456 : Fmarker_position (w->pointm)),
5457 w->contents);
5461 /* Scroll selected_window up or down. If N is nil, scroll a
5462 screen-full which is defined as the height of the window minus
5463 next_screen_context_lines. If N is the symbol `-', scroll.
5464 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5465 up. This is the guts of Fscroll_up and Fscroll_down. */
5467 static void
5468 scroll_command (Lisp_Object n, int direction)
5470 ptrdiff_t count = SPECPDL_INDEX ();
5472 eassert (eabs (direction) == 1);
5474 /* If selected window's buffer isn't current, make it current for
5475 the moment. But don't screw up if window_scroll gets an error. */
5476 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5478 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5479 Fset_buffer (XWINDOW (selected_window)->contents);
5482 if (NILP (n))
5483 window_scroll (selected_window, direction, true, false);
5484 else if (EQ (n, Qminus))
5485 window_scroll (selected_window, -direction, true, false);
5486 else
5488 n = Fprefix_numeric_value (n);
5489 window_scroll (selected_window, XINT (n) * direction, false, false);
5492 unbind_to (count, Qnil);
5495 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5496 doc: /* Scroll text of selected window upward ARG lines.
5497 If ARG is omitted or nil, scroll upward by a near full screen.
5498 A near full screen is `next-screen-context-lines' less than a full screen.
5499 Negative ARG means scroll downward.
5500 If ARG is the atom `-', scroll downward by nearly full screen.
5501 When calling from a program, supply as argument a number, nil, or `-'. */)
5502 (Lisp_Object arg)
5504 scroll_command (arg, 1);
5505 return Qnil;
5508 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5509 doc: /* Scroll text of selected window down ARG lines.
5510 If ARG is omitted or nil, scroll down by a near full screen.
5511 A near full screen is `next-screen-context-lines' less than a full screen.
5512 Negative ARG means scroll upward.
5513 If ARG is the atom `-', scroll upward by nearly full screen.
5514 When calling from a program, supply as argument a number, nil, or `-'. */)
5515 (Lisp_Object arg)
5517 scroll_command (arg, -1);
5518 return Qnil;
5521 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5522 doc: /* Return the other window for \"other window scroll\" commands.
5523 If `other-window-scroll-buffer' is non-nil, a window
5524 showing that buffer is used.
5525 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5526 specifies the window. This takes precedence over
5527 `other-window-scroll-buffer'. */)
5528 (void)
5530 Lisp_Object window;
5532 if (MINI_WINDOW_P (XWINDOW (selected_window))
5533 && !NILP (Vminibuf_scroll_window))
5534 window = Vminibuf_scroll_window;
5535 /* If buffer is specified and live, scroll that buffer. */
5536 else if (!NILP (Vother_window_scroll_buffer)
5537 && BUFFERP (Vother_window_scroll_buffer)
5538 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5540 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5541 if (NILP (window))
5542 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5544 else
5546 /* Nothing specified; look for a neighboring window on the same
5547 frame. */
5548 window = Fnext_window (selected_window, Qnil, Qnil);
5550 if (EQ (window, selected_window))
5551 /* That didn't get us anywhere; look for a window on another
5552 visible frame. */
5554 window = Fnext_window (window, Qnil, Qt);
5555 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5556 && ! EQ (window, selected_window));
5559 CHECK_LIVE_WINDOW (window);
5561 if (EQ (window, selected_window))
5562 error ("There is no other window");
5564 return window;
5567 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5568 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5569 A near full screen is `next-screen-context-lines' less than a full screen.
5570 The next window is the one below the current one; or the one at the top
5571 if the current one is at the bottom. Negative ARG means scroll downward.
5572 If ARG is the atom `-', scroll downward by nearly full screen.
5573 When calling from a program, supply as argument a number, nil, or `-'.
5575 If `other-window-scroll-buffer' is non-nil, scroll the window
5576 showing that buffer, popping the buffer up if necessary.
5577 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5578 specifies the window to scroll. This takes precedence over
5579 `other-window-scroll-buffer'. */)
5580 (Lisp_Object arg)
5582 Lisp_Object window;
5583 struct window *w;
5584 ptrdiff_t count = SPECPDL_INDEX ();
5586 window = Fother_window_for_scrolling ();
5587 w = XWINDOW (window);
5589 /* Don't screw up if window_scroll gets an error. */
5590 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5592 Fset_buffer (w->contents);
5593 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5594 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5596 if (NILP (arg))
5597 window_scroll (window, 1, true, true);
5598 else if (EQ (arg, Qminus))
5599 window_scroll (window, -1, true, true);
5600 else
5602 if (CONSP (arg))
5603 arg = XCAR (arg);
5604 CHECK_NUMBER (arg);
5605 window_scroll (window, XINT (arg), false, true);
5608 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5609 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5610 unbind_to (count, Qnil);
5612 return Qnil;
5615 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5616 doc: /* Scroll selected window display ARG columns left.
5617 Default for ARG is window width minus 2.
5618 Value is the total amount of leftward horizontal scrolling in
5619 effect after the change.
5620 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5621 lower bound for automatic scrolling, i.e. automatic scrolling
5622 will not scroll a window to a column less than the value returned
5623 by this function. This happens in an interactive call. */)
5624 (register Lisp_Object arg, Lisp_Object set_minimum)
5626 struct window *w = XWINDOW (selected_window);
5627 EMACS_INT requested_arg = (NILP (arg)
5628 ? window_body_width (w, 0) - 2
5629 : XINT (Fprefix_numeric_value (arg)));
5630 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5632 if (!NILP (set_minimum))
5633 w->min_hscroll = w->hscroll;
5635 w->suspend_auto_hscroll = true;
5637 return result;
5640 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5641 doc: /* Scroll selected window display ARG columns right.
5642 Default for ARG is window width minus 2.
5643 Value is the total amount of leftward horizontal scrolling in
5644 effect after the change.
5645 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5646 lower bound for automatic scrolling, i.e. automatic scrolling
5647 will not scroll a window to a column less than the value returned
5648 by this function. This happens in an interactive call. */)
5649 (register Lisp_Object arg, Lisp_Object set_minimum)
5651 struct window *w = XWINDOW (selected_window);
5652 EMACS_INT requested_arg = (NILP (arg)
5653 ? window_body_width (w, 0) - 2
5654 : XINT (Fprefix_numeric_value (arg)));
5655 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5657 if (!NILP (set_minimum))
5658 w->min_hscroll = w->hscroll;
5660 w->suspend_auto_hscroll = true;
5662 return result;
5665 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5666 doc: /* Return the window which was selected when entering the minibuffer.
5667 Returns nil, if selected window is not a minibuffer window. */)
5668 (void)
5670 if (minibuf_level > 0
5671 && MINI_WINDOW_P (XWINDOW (selected_window))
5672 && WINDOW_LIVE_P (minibuf_selected_window))
5673 return minibuf_selected_window;
5675 return Qnil;
5678 /* Value is the number of lines actually displayed in window W,
5679 as opposed to its height. */
5681 static int
5682 displayed_window_lines (struct window *w)
5684 struct it it;
5685 struct text_pos start;
5686 int height = window_box_height (w);
5687 struct buffer *old_buffer;
5688 int bottom_y;
5689 void *itdata = NULL;
5691 if (XBUFFER (w->contents) != current_buffer)
5693 old_buffer = current_buffer;
5694 set_buffer_internal (XBUFFER (w->contents));
5696 else
5697 old_buffer = NULL;
5699 /* In case W->start is out of the accessible range, do something
5700 reasonable. This happens in Info mode when Info-scroll-down
5701 calls (recenter -1) while W->start is 1. */
5702 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5704 itdata = bidi_shelve_cache ();
5705 start_display (&it, w, start);
5706 move_it_vertically (&it, height);
5707 bottom_y = line_bottom_y (&it);
5708 bidi_unshelve_cache (itdata, false);
5710 /* rms: On a non-window display,
5711 the value of it.vpos at the bottom of the screen
5712 seems to be 1 larger than window_box_height (w).
5713 This kludge fixes a bug whereby (move-to-window-line -1)
5714 when ZV is on the last screen line
5715 moves to the previous screen line instead of the last one. */
5716 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5717 height++;
5719 /* Add in empty lines at the bottom of the window. */
5720 if (bottom_y < height)
5722 int uy = FRAME_LINE_HEIGHT (it.f);
5723 it.vpos += (height - bottom_y + uy - 1) / uy;
5726 if (old_buffer)
5727 set_buffer_internal (old_buffer);
5729 return it.vpos;
5733 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5734 doc: /* Center point in selected window and maybe redisplay frame.
5735 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5736 relative to the selected window. If ARG is negative, it counts up from the
5737 bottom of the window. (ARG should be less than the height of the window.)
5739 If ARG is omitted or nil, then recenter with point on the middle line of
5740 the selected window; if the variable `recenter-redisplay' is non-nil,
5741 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5742 is set to `grow-only', this resets the tool-bar's height to the minimum
5743 height needed); if `recenter-redisplay' has the special value `tty',
5744 then only tty frames are redrawn.
5746 Just C-u as prefix means put point in the center of the window
5747 and redisplay normally--don't erase and redraw the frame. */)
5748 (register Lisp_Object arg)
5750 struct window *w = XWINDOW (selected_window);
5751 struct buffer *buf = XBUFFER (w->contents);
5752 bool center_p = false;
5753 ptrdiff_t charpos, bytepos;
5754 EMACS_INT iarg IF_LINT (= 0);
5755 int this_scroll_margin;
5757 if (buf != current_buffer)
5758 error ("`recenter'ing a window that does not display current-buffer.");
5760 /* If redisplay is suppressed due to an error, try again. */
5761 buf->display_error_modiff = 0;
5763 if (NILP (arg))
5765 if (!NILP (Vrecenter_redisplay)
5766 && (!EQ (Vrecenter_redisplay, Qtty)
5767 || !NILP (Ftty_type (selected_frame))))
5769 ptrdiff_t i;
5771 /* Invalidate pixel data calculated for all compositions. */
5772 for (i = 0; i < n_compositions; i++)
5773 composition_table[i]->font = NULL;
5774 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5775 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5776 #endif
5777 Fredraw_frame (WINDOW_FRAME (w));
5778 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5781 center_p = true;
5783 else if (CONSP (arg)) /* Just C-u. */
5784 center_p = true;
5785 else
5787 arg = Fprefix_numeric_value (arg);
5788 CHECK_NUMBER (arg);
5789 iarg = XINT (arg);
5792 /* Do this after making BUF current
5793 in case scroll_margin is buffer-local. */
5794 this_scroll_margin
5795 = max (0, min (scroll_margin, w->total_lines / 4));
5797 /* Don't use redisplay code for initial frames, as the necessary
5798 data structures might not be set up yet then. */
5799 if (!FRAME_INITIAL_P (XFRAME (w->frame)))
5801 if (center_p)
5803 struct it it;
5804 struct text_pos pt;
5805 void *itdata = bidi_shelve_cache ();
5807 SET_TEXT_POS (pt, PT, PT_BYTE);
5808 start_display (&it, w, pt);
5809 move_it_vertically_backward (&it, window_box_height (w) / 2);
5810 charpos = IT_CHARPOS (it);
5811 bytepos = IT_BYTEPOS (it);
5812 bidi_unshelve_cache (itdata, false);
5814 else if (iarg < 0)
5816 struct it it;
5817 struct text_pos pt;
5818 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5819 int extra_line_spacing;
5820 int h = window_box_height (w);
5821 int ht = window_internal_height (w);
5822 void *itdata = bidi_shelve_cache ();
5824 nlines = clip_to_bounds (this_scroll_margin + 1, nlines,
5825 ht - this_scroll_margin);
5827 SET_TEXT_POS (pt, PT, PT_BYTE);
5828 start_display (&it, w, pt);
5830 /* Be sure we have the exact height of the full line containing PT. */
5831 move_it_by_lines (&it, 0);
5833 /* The amount of pixels we have to move back is the window
5834 height minus what's displayed in the line containing PT,
5835 and the lines below. */
5836 it.current_y = 0;
5837 it.vpos = 0;
5838 move_it_by_lines (&it, nlines);
5840 if (it.vpos == nlines)
5841 h -= it.current_y;
5842 else
5844 /* Last line has no newline. */
5845 h -= line_bottom_y (&it);
5846 it.vpos++;
5849 /* Don't reserve space for extra line spacing of last line. */
5850 extra_line_spacing = it.max_extra_line_spacing;
5852 /* If we can't move down NLINES lines because we hit
5853 the end of the buffer, count in some empty lines. */
5854 if (it.vpos < nlines)
5856 nlines -= it.vpos;
5857 extra_line_spacing = it.extra_line_spacing;
5858 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5860 if (h <= 0)
5862 bidi_unshelve_cache (itdata, false);
5863 return Qnil;
5866 /* Now find the new top line (starting position) of the window. */
5867 start_display (&it, w, pt);
5868 it.current_y = 0;
5869 move_it_vertically_backward (&it, h);
5871 /* If extra line spacing is present, we may move too far
5872 back. This causes the last line to be only partially
5873 visible (which triggers redisplay to recenter that line
5874 in the middle), so move forward.
5875 But ignore extra line spacing on last line, as it is not
5876 considered to be part of the visible height of the line.
5878 h += extra_line_spacing;
5879 while (-it.current_y > h)
5880 move_it_by_lines (&it, 1);
5882 charpos = IT_CHARPOS (it);
5883 bytepos = IT_BYTEPOS (it);
5885 bidi_unshelve_cache (itdata, false);
5887 else
5889 struct it it;
5890 struct text_pos pt;
5891 ptrdiff_t nlines = min (PTRDIFF_MAX, iarg);
5892 int ht = window_internal_height (w);
5893 void *itdata = bidi_shelve_cache ();
5895 nlines = clip_to_bounds (this_scroll_margin, nlines,
5896 ht - this_scroll_margin - 1);
5898 SET_TEXT_POS (pt, PT, PT_BYTE);
5899 start_display (&it, w, pt);
5901 /* Move to the beginning of screen line containing PT. */
5902 move_it_by_lines (&it, 0);
5904 /* Move back to find the point which is ARG screen lines above PT. */
5905 if (nlines > 0)
5907 it.current_y = 0;
5908 it.vpos = 0;
5909 move_it_by_lines (&it, -nlines);
5912 charpos = IT_CHARPOS (it);
5913 bytepos = IT_BYTEPOS (it);
5915 bidi_unshelve_cache (itdata, false);
5918 else
5920 struct position pos;
5921 int ht = window_internal_height (w);
5923 if (center_p)
5924 iarg = ht / 2;
5925 else if (iarg < 0)
5926 iarg += ht;
5928 /* Don't let it get into the margin at either top or bottom. */
5929 iarg = clip_to_bounds (this_scroll_margin, iarg,
5930 ht - this_scroll_margin - 1);
5932 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5933 charpos = pos.bufpos;
5934 bytepos = pos.bytepos;
5937 /* Set the new window start. */
5938 set_marker_both (w->start, w->contents, charpos, bytepos);
5939 w->window_end_valid = false;
5941 w->optional_new_start = true;
5943 w->start_at_line_beg = (bytepos == BEGV_BYTE
5944 || FETCH_BYTE (bytepos - 1) == '\n');
5946 wset_redisplay (w);
5948 return Qnil;
5951 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5952 0, 2, 0,
5953 doc: /* Return the width in columns of the text display area of WINDOW.
5954 WINDOW must be a live window and defaults to the selected one.
5956 The returned width does not include dividers, scrollbars, margins,
5957 fringes, nor any partial-width columns at the right of the text
5958 area.
5960 Optional argument PIXELWISE non-nil, means to return the width in
5961 pixels. */)
5962 (Lisp_Object window, Lisp_Object pixelwise)
5964 struct window *w = decode_live_window (window);
5966 if (NILP (pixelwise))
5967 return make_number (window_box_width (w, TEXT_AREA)
5968 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5969 else
5970 return make_number (window_box_width (w, TEXT_AREA));
5973 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5974 0, 2, 0,
5975 doc: /* Return the height in lines of the text display area of WINDOW.
5976 WINDOW must be a live window and defaults to the selected one.
5978 The returned height does not include dividers, the mode line, any header
5979 line, nor any partial-height lines at the bottom of the text area.
5981 Optional argument PIXELWISE non-nil, means to return the height in
5982 pixels. */)
5983 (Lisp_Object window, Lisp_Object pixelwise)
5985 struct window *w = decode_live_window (window);
5987 if (NILP (pixelwise))
5988 return make_number (window_box_height (w)
5989 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5990 else
5991 return make_number (window_box_height (w));
5994 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5995 1, 1, "P",
5996 doc: /* Position point relative to window.
5997 ARG nil means position point at center of window.
5998 Else, ARG specifies vertical position within the window;
5999 zero means top of window, negative means relative to bottom of window. */)
6000 (Lisp_Object arg)
6002 struct window *w = XWINDOW (selected_window);
6003 int lines, start;
6004 Lisp_Object window;
6005 #if false
6006 int this_scroll_margin;
6007 #endif
6009 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
6010 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
6011 when passed below to set_marker_both. */
6012 error ("move-to-window-line called from unrelated buffer");
6014 window = selected_window;
6015 start = marker_position (w->start);
6016 if (start < BEGV || start > ZV)
6018 int height = window_internal_height (w);
6019 Fvertical_motion (make_number (- (height / 2)), window, Qnil);
6020 set_marker_both (w->start, w->contents, PT, PT_BYTE);
6021 w->start_at_line_beg = !NILP (Fbolp ());
6022 w->force_start = true;
6024 else
6025 Fgoto_char (w->start);
6027 lines = displayed_window_lines (w);
6029 #if false
6030 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
6031 #endif
6033 if (NILP (arg))
6034 XSETFASTINT (arg, lines / 2);
6035 else
6037 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
6039 if (iarg < 0)
6040 iarg = iarg + lines;
6042 #if false /* This code would prevent move-to-window-line from moving point
6043 to a place inside the scroll margins (which would cause the
6044 next redisplay to scroll). I wrote this code, but then concluded
6045 it is probably better not to install it. However, it is here
6046 inside #if false so as not to lose it. -- rms. */
6048 /* Don't let it get into the margin at either top or bottom. */
6049 iarg = max (iarg, this_scroll_margin);
6050 iarg = min (iarg, lines - this_scroll_margin - 1);
6051 #endif
6053 arg = make_number (iarg);
6056 /* Skip past a partially visible first line. */
6057 if (w->vscroll)
6058 XSETINT (arg, XINT (arg) + 1);
6060 return Fvertical_motion (arg, window, Qnil);
6065 /***********************************************************************
6066 Window Configuration
6067 ***********************************************************************/
6069 struct save_window_data
6071 struct vectorlike_header header;
6072 Lisp_Object selected_frame;
6073 Lisp_Object current_window;
6074 Lisp_Object current_buffer;
6075 Lisp_Object minibuf_scroll_window;
6076 Lisp_Object minibuf_selected_window;
6077 Lisp_Object root_window;
6078 Lisp_Object focus_frame;
6079 /* A vector, each of whose elements is a struct saved_window
6080 for one window. */
6081 Lisp_Object saved_windows;
6083 /* All fields above are traced by the GC.
6084 From `frame-cols' down, the fields are ignored by the GC. */
6085 /* We should be able to do without the following two. */
6086 int frame_cols, frame_lines;
6087 /* These two should get eventually replaced by their pixel
6088 counterparts. */
6089 int frame_menu_bar_lines, frame_tool_bar_lines;
6090 int frame_text_width, frame_text_height;
6091 /* These are currently unused. We need them as soon as we convert
6092 to pixels. */
6093 int frame_menu_bar_height, frame_tool_bar_height;
6096 /* This is saved as a Lisp_Vector. */
6097 struct saved_window
6099 struct vectorlike_header header;
6101 Lisp_Object window, buffer, start, pointm, old_pointm;
6102 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6103 Lisp_Object left_col, top_line, total_cols, total_lines;
6104 Lisp_Object normal_cols, normal_lines;
6105 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6106 Lisp_Object parent, prev;
6107 Lisp_Object start_at_line_beg;
6108 Lisp_Object display_table;
6109 Lisp_Object left_margin_cols, right_margin_cols;
6110 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6111 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6112 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6113 Lisp_Object combination_limit, window_parameters;
6116 #define SAVED_WINDOW_N(swv,n) \
6117 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6119 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6120 doc: /* Return t if OBJECT is a window-configuration object. */)
6121 (Lisp_Object object)
6123 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6126 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6127 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6128 (Lisp_Object config)
6130 register struct save_window_data *data;
6131 struct Lisp_Vector *saved_windows;
6133 CHECK_WINDOW_CONFIGURATION (config);
6135 data = (struct save_window_data *) XVECTOR (config);
6136 saved_windows = XVECTOR (data->saved_windows);
6137 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6140 DEFUN ("set-window-configuration", Fset_window_configuration,
6141 Sset_window_configuration, 1, 1, 0,
6142 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6143 CONFIGURATION must be a value previously returned
6144 by `current-window-configuration' (which see).
6145 If CONFIGURATION was made from a frame that is now deleted,
6146 only frame-independent values can be restored. In this case,
6147 the return value is nil. Otherwise the value is t. */)
6148 (Lisp_Object configuration)
6150 register struct save_window_data *data;
6151 struct Lisp_Vector *saved_windows;
6152 Lisp_Object new_current_buffer;
6153 Lisp_Object frame;
6154 struct frame *f;
6155 ptrdiff_t old_point = -1;
6156 USE_SAFE_ALLOCA;
6158 CHECK_WINDOW_CONFIGURATION (configuration);
6160 data = (struct save_window_data *) XVECTOR (configuration);
6161 saved_windows = XVECTOR (data->saved_windows);
6163 new_current_buffer = data->current_buffer;
6164 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6165 new_current_buffer = Qnil;
6166 else
6168 if (XBUFFER (new_current_buffer) == current_buffer)
6169 /* The code further down "preserves point" by saving here PT in
6170 old_point and then setting it later back into PT. When the
6171 current-selected-window and the final-selected-window both show
6172 the current buffer, this suffers from the problem that the
6173 current PT is the window-point of the current-selected-window,
6174 while the final PT is the point of the final-selected-window, so
6175 this copy from one PT to the other would end up moving the
6176 window-point of the final-selected-window to the window-point of
6177 the current-selected-window. So we have to be careful which
6178 point of the current-buffer we copy into old_point. */
6179 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6180 && WINDOWP (selected_window)
6181 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6182 && !EQ (selected_window, data->current_window))
6183 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6184 else
6185 old_point = PT;
6186 else
6187 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6188 point in new_current_buffer as of the last time this buffer was
6189 used. This can be non-deterministic since it can be changed by
6190 things like jit-lock by mere temporary selection of some random
6191 window that happens to show this buffer.
6192 So if possible we want this arbitrary choice of "which point" to
6193 be the one from the to-be-selected-window so as to prevent this
6194 window's cursor from being copied from another window. */
6195 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6196 /* If current_window = selected_window, its point is in BUF_PT. */
6197 && !EQ (selected_window, data->current_window))
6198 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6199 else
6200 old_point = BUF_PT (XBUFFER (new_current_buffer));
6203 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6204 f = XFRAME (frame);
6206 /* If f is a dead frame, don't bother rebuilding its window tree.
6207 However, there is other stuff we should still try to do below. */
6208 if (FRAME_LIVE_P (f))
6210 Lisp_Object window;
6211 Lisp_Object dead_windows = Qnil;
6212 Lisp_Object tem, par, pers;
6213 struct window *w;
6214 struct saved_window *p;
6215 struct window *root_window;
6216 struct window **leaf_windows;
6217 ptrdiff_t i, k, n_leaf_windows;
6219 /* Don't do this within the main loop below: This may call Lisp
6220 code and is thus potentially unsafe while input is blocked. */
6221 for (k = 0; k < saved_windows->header.size; k++)
6223 p = SAVED_WINDOW_N (saved_windows, k);
6224 window = p->window;
6225 w = XWINDOW (window);
6226 if (BUFFERP (w->contents)
6227 && !EQ (w->contents, p->buffer)
6228 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6229 /* If a window we restore gets another buffer, record the
6230 window's old buffer. */
6231 call1 (Qrecord_window_buffer, window);
6234 /* Disallow x_set_window_size, temporarily. */
6235 f->can_x_set_window_size = false;
6236 /* The mouse highlighting code could get screwed up
6237 if it runs during this. */
6238 block_input ();
6240 /* "Swap out" point from the selected window's buffer
6241 into the window itself. (Normally the pointm of the selected
6242 window holds garbage.) We do this now, before
6243 restoring the window contents, and prevent it from
6244 being done later on when we select a new window. */
6245 if (! NILP (XWINDOW (selected_window)->contents))
6247 w = XWINDOW (selected_window);
6248 set_marker_both (w->pointm,
6249 w->contents,
6250 BUF_PT (XBUFFER (w->contents)),
6251 BUF_PT_BYTE (XBUFFER (w->contents)));
6254 fset_redisplay (f);
6255 FRAME_WINDOW_SIZES_CHANGED (f) = true;
6257 /* Problem: Freeing all matrices and later allocating them again
6258 is a serious redisplay flickering problem. What we would
6259 really like to do is to free only those matrices not reused
6260 below. */
6261 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6262 ptrdiff_t nwindows = count_windows (root_window);
6263 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6264 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6266 /* Kludge Alert!
6267 Mark all windows now on frame as "deleted".
6268 Restoring the new configuration "undeletes" any that are in it.
6270 Save their current buffers in their height fields, since we may
6271 need it later, if a buffer saved in the configuration is now
6272 dead. */
6273 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6275 for (k = 0; k < saved_windows->header.size; k++)
6277 p = SAVED_WINDOW_N (saved_windows, k);
6278 window = p->window;
6279 w = XWINDOW (window);
6280 wset_next (w, Qnil);
6282 if (!NILP (p->parent))
6283 wset_parent
6284 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6285 else
6286 wset_parent (w, Qnil);
6288 if (!NILP (p->prev))
6290 wset_prev
6291 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6292 wset_next (XWINDOW (w->prev), p->window);
6294 else
6296 wset_prev (w, Qnil);
6297 if (!NILP (w->parent))
6298 wset_combination (XWINDOW (w->parent),
6299 (XINT (p->total_cols)
6300 != XWINDOW (w->parent)->total_cols),
6301 p->window);
6304 /* If we squirreled away the buffer, restore it now. */
6305 if (BUFFERP (w->combination_limit))
6306 wset_buffer (w, w->combination_limit);
6307 w->pixel_left = XFASTINT (p->pixel_left);
6308 w->pixel_top = XFASTINT (p->pixel_top);
6309 w->pixel_width = XFASTINT (p->pixel_width);
6310 w->pixel_height = XFASTINT (p->pixel_height);
6311 w->left_col = XFASTINT (p->left_col);
6312 w->top_line = XFASTINT (p->top_line);
6313 w->total_cols = XFASTINT (p->total_cols);
6314 w->total_lines = XFASTINT (p->total_lines);
6315 wset_normal_cols (w, p->normal_cols);
6316 wset_normal_lines (w, p->normal_lines);
6317 w->hscroll = XFASTINT (p->hscroll);
6318 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6319 w->min_hscroll = XFASTINT (p->min_hscroll);
6320 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6321 wset_display_table (w, p->display_table);
6322 w->left_margin_cols = XINT (p->left_margin_cols);
6323 w->right_margin_cols = XINT (p->right_margin_cols);
6324 w->left_fringe_width = XINT (p->left_fringe_width);
6325 w->right_fringe_width = XINT (p->right_fringe_width);
6326 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6327 w->scroll_bar_width = XINT (p->scroll_bar_width);
6328 w->scroll_bar_height = XINT (p->scroll_bar_height);
6329 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6330 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6331 wset_dedicated (w, p->dedicated);
6332 wset_combination_limit (w, p->combination_limit);
6333 /* Restore any window parameters that have been saved.
6334 Parameters that have not been saved are left alone. */
6335 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6337 pers = XCAR (tem);
6338 if (CONSP (pers))
6340 if (NILP (XCDR (pers)))
6342 par = Fassq (XCAR (pers), w->window_parameters);
6343 if (CONSP (par) && !NILP (XCDR (par)))
6344 /* Reset a parameter to nil if and only if it
6345 has a non-nil association. Don't make new
6346 associations. */
6347 Fsetcdr (par, Qnil);
6349 else
6350 /* Always restore a non-nil value. */
6351 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6355 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6356 /* If saved buffer is alive, install it. */
6358 wset_buffer (w, p->buffer);
6359 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6360 set_marker_restricted (w->start, p->start, w->contents);
6361 set_marker_restricted (w->pointm, p->pointm, w->contents);
6362 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6363 /* As documented in Fcurrent_window_configuration, don't
6364 restore the location of point in the buffer which was
6365 current when the window configuration was recorded. */
6366 if (!EQ (p->buffer, new_current_buffer)
6367 && XBUFFER (p->buffer) == current_buffer)
6368 Fgoto_char (w->pointm);
6370 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6371 /* Keep window's old buffer; make sure the markers are real. */
6373 /* Set window markers at start of visible range. */
6374 if (XMARKER (w->start)->buffer == 0)
6375 set_marker_restricted_both (w->start, w->contents, 0, 0);
6376 if (XMARKER (w->pointm)->buffer == 0)
6377 set_marker_restricted_both
6378 (w->pointm, w->contents,
6379 BUF_PT (XBUFFER (w->contents)),
6380 BUF_PT_BYTE (XBUFFER (w->contents)));
6381 if (XMARKER (w->old_pointm)->buffer == 0)
6382 set_marker_restricted_both
6383 (w->old_pointm, w->contents,
6384 BUF_PT (XBUFFER (w->contents)),
6385 BUF_PT_BYTE (XBUFFER (w->contents)));
6386 w->start_at_line_beg = true;
6388 else if (!NILP (w->start))
6389 /* Leaf window has no live buffer, get one. */
6391 /* Get the buffer via other_buffer_safely in order to
6392 avoid showing an unimportant buffer and, if necessary, to
6393 recreate *scratch* in the course (part of Juanma's bs-show
6394 scenario from March 2011). */
6395 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6396 /* This will set the markers to beginning of visible
6397 range. */
6398 set_marker_restricted_both (w->start, w->contents, 0, 0);
6399 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6400 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6401 w->start_at_line_beg = true;
6402 if (!NILP (w->dedicated))
6403 /* Record this window as dead. */
6404 dead_windows = Fcons (window, dead_windows);
6405 /* Make sure window is no more dedicated. */
6406 wset_dedicated (w, Qnil);
6410 fset_root_window (f, data->root_window);
6411 /* Arrange *not* to restore point in the buffer that was
6412 current when the window configuration was saved. */
6413 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6414 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6415 make_number (old_point),
6416 XWINDOW (data->current_window)->contents);
6418 /* In the following call to `select-window', prevent "swapping out
6419 point" in the old selected window using the buffer that has
6420 been restored into it. We already swapped out that point from
6421 that window's old buffer.
6423 Do not record the buffer here. We do that in a separate call
6424 to select_window below. See also Bug#16207. */
6425 select_window (data->current_window, Qt, true);
6426 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6427 last_selected_window)
6428 = selected_window;
6430 if (NILP (data->focus_frame)
6431 || (FRAMEP (data->focus_frame)
6432 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6433 Fredirect_frame_focus (frame, data->focus_frame);
6435 /* Now, free glyph matrices in windows that were not reused. */
6436 for (i = 0; i < n_leaf_windows; i++)
6437 if (NILP (leaf_windows[i]->contents))
6438 free_window_matrices (leaf_windows[i]);
6440 /* Allow x_set_window_size again and apply frame size changes if
6441 needed. */
6442 f->can_x_set_window_size = true;
6443 adjust_frame_size (f, -1, -1, 1, false, Qset_window_configuration);
6445 adjust_frame_glyphs (f);
6446 unblock_input ();
6448 /* Scan dead buffer windows. */
6449 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6451 window = XCAR (dead_windows);
6452 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6453 delete_deletable_window (window);
6456 /* Record the selected window's buffer here. The window should
6457 already be the selected one from the call above. */
6458 select_window (data->current_window, Qnil, false);
6460 /* Fselect_window will have made f the selected frame, so we
6461 reselect the proper frame here. Fhandle_switch_frame will change the
6462 selected window too, but that doesn't make the call to
6463 Fselect_window above totally superfluous; it still sets f's
6464 selected window. */
6465 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6466 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6468 run_window_configuration_change_hook (f);
6471 if (!NILP (new_current_buffer))
6473 Fset_buffer (new_current_buffer);
6474 /* If the new current buffer doesn't appear in the selected
6475 window, go to its old point (see bug#12208). */
6476 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6477 Fgoto_char (make_number (old_point));
6480 Vminibuf_scroll_window = data->minibuf_scroll_window;
6481 minibuf_selected_window = data->minibuf_selected_window;
6483 SAFE_FREE ();
6484 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6488 void
6489 restore_window_configuration (Lisp_Object configuration)
6491 Fset_window_configuration (configuration);
6495 /* If WINDOW is an internal window, recursively delete all child windows
6496 reachable via the next and contents slots of WINDOW. Otherwise setup
6497 WINDOW to not show any buffer. */
6499 void
6500 delete_all_child_windows (Lisp_Object window)
6502 register struct window *w;
6504 w = XWINDOW (window);
6506 if (!NILP (w->next))
6507 /* Delete WINDOW's siblings (we traverse postorderly). */
6508 delete_all_child_windows (w->next);
6510 if (WINDOWP (w->contents))
6512 delete_all_child_windows (w->contents);
6513 wset_combination (w, false, Qnil);
6515 else if (BUFFERP (w->contents))
6517 unshow_buffer (w);
6518 unchain_marker (XMARKER (w->pointm));
6519 unchain_marker (XMARKER (w->old_pointm));
6520 unchain_marker (XMARKER (w->start));
6521 /* Since combination limit makes sense for an internal windows
6522 only, we use this slot to save the buffer for the sake of
6523 possible resurrection in Fset_window_configuration. */
6524 wset_combination_limit (w, w->contents);
6525 wset_buffer (w, Qnil);
6528 Vwindow_list = Qnil;
6531 static ptrdiff_t
6532 count_windows (struct window *window)
6534 ptrdiff_t count = 1;
6535 if (!NILP (window->next))
6536 count += count_windows (XWINDOW (window->next));
6537 if (WINDOWP (window->contents))
6538 count += count_windows (XWINDOW (window->contents));
6539 return count;
6543 /* Fill vector FLAT with leaf windows under W, starting at index I.
6544 Value is last index + 1. */
6545 static ptrdiff_t
6546 get_leaf_windows (struct window *w, struct window **flat, ptrdiff_t i)
6548 while (w)
6550 if (WINDOWP (w->contents))
6551 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6552 else
6553 flat[i++] = w;
6555 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6558 return i;
6562 /* Return a pointer to the glyph W's physical cursor is on. Value is
6563 null if W's current matrix is invalid, so that no meaningful glyph
6564 can be returned. */
6565 struct glyph *
6566 get_phys_cursor_glyph (struct window *w)
6568 struct glyph_row *row;
6569 struct glyph *glyph;
6570 int hpos = w->phys_cursor.hpos;
6572 if (!(w->phys_cursor.vpos >= 0
6573 && w->phys_cursor.vpos < w->current_matrix->nrows))
6574 return NULL;
6576 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6577 if (!row->enabled_p)
6578 return NULL;
6580 if (w->hscroll)
6582 /* When the window is hscrolled, cursor hpos can legitimately be
6583 out of bounds, but we draw the cursor at the corresponding
6584 window margin in that case. */
6585 if (!row->reversed_p && hpos < 0)
6586 hpos = 0;
6587 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6588 hpos = row->used[TEXT_AREA] - 1;
6591 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6592 glyph = row->glyphs[TEXT_AREA] + hpos;
6593 else
6594 glyph = NULL;
6596 return glyph;
6600 static ptrdiff_t
6601 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, ptrdiff_t i)
6603 struct saved_window *p;
6604 struct window *w;
6605 Lisp_Object tem, pers, par;
6607 for (; !NILP (window); window = w->next)
6609 p = SAVED_WINDOW_N (vector, i);
6610 w = XWINDOW (window);
6612 wset_temslot (w, make_number (i)); i++;
6613 p->window = window;
6614 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6615 p->pixel_left = make_number (w->pixel_left);
6616 p->pixel_top = make_number (w->pixel_top);
6617 p->pixel_width = make_number (w->pixel_width);
6618 p->pixel_height = make_number (w->pixel_height);
6619 p->left_col = make_number (w->left_col);
6620 p->top_line = make_number (w->top_line);
6621 p->total_cols = make_number (w->total_cols);
6622 p->total_lines = make_number (w->total_lines);
6623 p->normal_cols = w->normal_cols;
6624 p->normal_lines = w->normal_lines;
6625 XSETFASTINT (p->hscroll, w->hscroll);
6626 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6627 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6628 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6629 p->display_table = w->display_table;
6630 p->left_margin_cols = make_number (w->left_margin_cols);
6631 p->right_margin_cols = make_number (w->right_margin_cols);
6632 p->left_fringe_width = make_number (w->left_fringe_width);
6633 p->right_fringe_width = make_number (w->right_fringe_width);
6634 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6635 p->scroll_bar_width = make_number (w->scroll_bar_width);
6636 p->scroll_bar_height = make_number (w->scroll_bar_height);
6637 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6638 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6639 p->dedicated = w->dedicated;
6640 p->combination_limit = w->combination_limit;
6641 p->window_parameters = Qnil;
6643 if (!NILP (Vwindow_persistent_parameters))
6645 /* Run cycle detection on Vwindow_persistent_parameters. */
6646 Lisp_Object tortoise, hare;
6648 hare = tortoise = Vwindow_persistent_parameters;
6649 while (CONSP (hare))
6651 hare = XCDR (hare);
6652 if (!CONSP (hare))
6653 break;
6655 hare = XCDR (hare);
6656 tortoise = XCDR (tortoise);
6658 if (EQ (hare, tortoise))
6659 /* Reset Vwindow_persistent_parameters to Qnil. */
6661 Vwindow_persistent_parameters = Qnil;
6662 break;
6666 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6667 tem = XCDR (tem))
6669 pers = XCAR (tem);
6670 /* Save values for persistent window parameters. */
6671 if (CONSP (pers) && !NILP (XCDR (pers)))
6673 par = Fassq (XCAR (pers), w->window_parameters);
6674 if (NILP (par))
6675 /* If the window has no value for the parameter,
6676 make one. */
6677 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6678 p->window_parameters);
6679 else
6680 /* If the window has a value for the parameter,
6681 save it. */
6682 p->window_parameters = Fcons (Fcons (XCAR (par),
6683 XCDR (par)),
6684 p->window_parameters);
6689 if (BUFFERP (w->contents))
6691 /* Save w's value of point in the window configuration. If w
6692 is the selected window, then get the value of point from
6693 the buffer; pointm is garbage in the selected window. */
6694 if (EQ (window, selected_window))
6695 p->pointm = build_marker (XBUFFER (w->contents),
6696 BUF_PT (XBUFFER (w->contents)),
6697 BUF_PT_BYTE (XBUFFER (w->contents)));
6698 else
6699 p->pointm = Fcopy_marker (w->pointm, Qnil);
6700 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6701 XMARKER (p->pointm)->insertion_type
6702 = !NILP (buffer_local_value /* Don't signal error if void. */
6703 (Qwindow_point_insertion_type, w->contents));
6704 XMARKER (p->old_pointm)->insertion_type
6705 = !NILP (buffer_local_value /* Don't signal error if void. */
6706 (Qwindow_point_insertion_type, w->contents));
6708 p->start = Fcopy_marker (w->start, Qnil);
6709 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6711 else
6713 p->pointm = Qnil;
6714 p->old_pointm = Qnil;
6715 p->start = Qnil;
6716 p->start_at_line_beg = Qnil;
6719 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6720 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6722 if (WINDOWP (w->contents))
6723 i = save_window_save (w->contents, vector, i);
6726 return i;
6729 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6730 Scurrent_window_configuration, 0, 1, 0,
6731 doc: /* Return an object representing the current window configuration of FRAME.
6732 If FRAME is nil or omitted, use the selected frame.
6733 This describes the number of windows, their sizes and current buffers,
6734 and for each displayed buffer, where display starts, and the position of
6735 point. An exception is made for point in the current buffer:
6736 its value is -not- saved.
6737 This also records the currently selected frame, and FRAME's focus
6738 redirection (see `redirect-frame-focus'). The variable
6739 `window-persistent-parameters' specifies which window parameters are
6740 saved by this function. */)
6741 (Lisp_Object frame)
6743 Lisp_Object tem;
6744 ptrdiff_t i, n_windows;
6745 struct save_window_data *data;
6746 struct frame *f = decode_live_frame (frame);
6748 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6749 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6750 PVEC_WINDOW_CONFIGURATION);
6752 data->frame_cols = FRAME_COLS (f);
6753 data->frame_lines = FRAME_LINES (f);
6754 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6755 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6756 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6757 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6758 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6759 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6760 data->selected_frame = selected_frame;
6761 data->current_window = FRAME_SELECTED_WINDOW (f);
6762 XSETBUFFER (data->current_buffer, current_buffer);
6763 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6764 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6765 data->root_window = FRAME_ROOT_WINDOW (f);
6766 data->focus_frame = FRAME_FOCUS_FRAME (f);
6767 tem = make_uninit_vector (n_windows);
6768 data->saved_windows = tem;
6769 for (i = 0; i < n_windows; i++)
6770 ASET (tem, i,
6771 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6772 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6773 XSETWINDOW_CONFIGURATION (tem, data);
6774 return (tem);
6777 /* Called after W's margins, fringes or scroll bars was adjusted. */
6779 static void
6780 apply_window_adjustment (struct window *w)
6782 eassert (w);
6783 clear_glyph_matrix (w->current_matrix);
6784 w->window_end_valid = false;
6785 windows_or_buffers_changed = 30;
6786 wset_redisplay (w);
6787 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6791 /***********************************************************************
6792 Marginal Areas
6793 ***********************************************************************/
6795 static int
6796 extract_dimension (Lisp_Object dimension)
6798 if (NILP (dimension))
6799 return -1;
6800 CHECK_RANGED_INTEGER (dimension, 0, INT_MAX);
6801 return XINT (dimension);
6804 static struct window *
6805 set_window_margins (struct window *w, Lisp_Object left_width,
6806 Lisp_Object right_width)
6808 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6809 int left = NILP (left_width) ? 0 : extract_dimension (left_width);
6810 int right = NILP (right_width) ? 0 : extract_dimension (right_width);
6812 if (w->left_margin_cols != left || w->right_margin_cols != right)
6814 /* Don't change anything if new margins won't fit. */
6815 if ((WINDOW_PIXEL_WIDTH (w)
6816 - WINDOW_FRINGES_WIDTH (w)
6817 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6818 - (left + right) * unit)
6819 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6821 w->left_margin_cols = left;
6822 w->right_margin_cols = right;
6824 return w;
6826 else
6827 return NULL;
6829 else
6830 return NULL;
6833 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6834 2, 3, 0,
6835 doc: /* Set width of marginal areas of window WINDOW.
6836 WINDOW must be a live window and defaults to the selected one.
6838 Second arg LEFT-WIDTH specifies the number of character cells to
6839 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6840 does the same for the right marginal area. A nil width parameter
6841 means no margin.
6843 Return t if any margin was actually changed and nil otherwise. */)
6844 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6846 struct window *w = set_window_margins (decode_live_window (window),
6847 left_width, right_width);
6848 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6852 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6853 0, 1, 0,
6854 doc: /* Get width of marginal areas of window WINDOW.
6855 WINDOW must be a live window and defaults to the selected one.
6857 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6858 If a marginal area does not exist, its width will be returned
6859 as nil. */)
6860 (Lisp_Object window)
6862 struct window *w = decode_live_window (window);
6863 return Fcons (w->left_margin_cols
6864 ? make_number (w->left_margin_cols) : Qnil,
6865 w->right_margin_cols
6866 ? make_number (w->right_margin_cols) : Qnil);
6871 /***********************************************************************
6872 Fringes
6873 ***********************************************************************/
6875 static struct window *
6876 set_window_fringes (struct window *w, Lisp_Object left_width,
6877 Lisp_Object right_width, Lisp_Object outside_margins)
6879 bool outside = !NILP (outside_margins);
6880 int left = extract_dimension (left_width);
6881 int right = extract_dimension (right_width);
6883 /* Do nothing on a tty or if nothing to actually change. */
6884 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6885 && (w->left_fringe_width != left
6886 || w->right_fringe_width != right
6887 || w->fringes_outside_margins != outside))
6889 if (left > 0 || right > 0)
6891 /* Don't change anything if new fringes don't fit. */
6892 if ((WINDOW_PIXEL_WIDTH (w)
6893 - WINDOW_MARGINS_WIDTH (w)
6894 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6895 - max (left, 0) - max (right, 0))
6896 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6897 return NULL;
6900 w->left_fringe_width = left;
6901 w->right_fringe_width = right;
6902 w->fringes_outside_margins = outside;
6904 return w;
6906 else
6907 return NULL;
6910 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6911 2, 4, 0,
6912 doc: /* Set the fringe widths of window WINDOW.
6913 WINDOW must be a live window and defaults to the selected one.
6915 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6916 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6917 fringe width. If a fringe width arg is nil, that means to use the
6918 frame's default fringe width. Default fringe widths can be set with
6919 the command `set-fringe-style'.
6920 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6921 outside of the display margins. By default, fringes are drawn between
6922 display marginal areas and the text area.
6924 Return t if any fringe was actually changed and nil otherwise. */)
6925 (Lisp_Object window, Lisp_Object left_width,
6926 Lisp_Object right_width, Lisp_Object outside_margins)
6928 struct window *w
6929 = set_window_fringes (decode_live_window (window),
6930 left_width, right_width, outside_margins);
6931 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6935 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6936 0, 1, 0,
6937 doc: /* Get width of fringes of window WINDOW.
6938 WINDOW must be a live window and defaults to the selected one.
6940 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6941 (Lisp_Object window)
6943 struct window *w = decode_live_window (window);
6945 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6946 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6947 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6952 /***********************************************************************
6953 Scroll bars
6954 ***********************************************************************/
6956 static struct window *
6957 set_window_scroll_bars (struct window *w, Lisp_Object width,
6958 Lisp_Object vertical_type, Lisp_Object height,
6959 Lisp_Object horizontal_type)
6961 int iwidth = extract_dimension (width);
6962 bool changed = false;
6964 if (iwidth == 0)
6965 vertical_type = Qnil;
6967 if (!(NILP (vertical_type)
6968 || EQ (vertical_type, Qleft)
6969 || EQ (vertical_type, Qright)
6970 || EQ (vertical_type, Qt)))
6971 error ("Invalid type of vertical scroll bar");
6973 if (w->scroll_bar_width != iwidth
6974 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6976 /* Don't change anything if new scroll bar won't fit. */
6977 if ((WINDOW_PIXEL_WIDTH (w)
6978 - WINDOW_MARGINS_WIDTH (w)
6979 - WINDOW_FRINGES_WIDTH (w)
6980 - max (iwidth, 0))
6981 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6983 w->scroll_bar_width = iwidth;
6984 wset_vertical_scroll_bar_type (w, vertical_type);
6985 changed = true;
6989 #if USE_HORIZONTAL_SCROLL_BARS
6991 int iheight = extract_dimension (height);
6993 if (MINI_WINDOW_P (w) || iheight == 0)
6994 horizontal_type = Qnil;
6996 if (!(NILP (horizontal_type)
6997 || EQ (horizontal_type, Qbottom)
6998 || EQ (horizontal_type, Qt)))
6999 error ("Invalid type of horizontal scroll bar");
7001 if (w->scroll_bar_height != iheight
7002 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
7004 /* Don't change anything if new scroll bar won't fit. */
7005 if ((WINDOW_PIXEL_HEIGHT (w)
7006 - WINDOW_HEADER_LINE_HEIGHT (w)
7007 - WINDOW_MODE_LINE_HEIGHT (w)
7008 - max (iheight, 0))
7009 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7011 w->scroll_bar_height = iheight;
7012 wset_horizontal_scroll_bar_type (w, horizontal_type);
7013 changed = true;
7017 #else
7018 wset_horizontal_scroll_bar_type (w, Qnil);
7019 #endif
7021 return changed ? w : NULL;
7024 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7025 Sset_window_scroll_bars, 1, 5, 0,
7026 doc: /* Set width and type of scroll bars of window WINDOW.
7027 WINDOW must be a live window and defaults to the selected one.
7029 Second parameter WIDTH specifies the pixel width for the vertical scroll
7030 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
7031 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7032 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
7033 frame's scroll-bar type.
7035 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7036 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
7037 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7038 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
7039 means to use the frame's horizontal scroll-bar type.
7041 Return t if scroll bars were actually changed and nil otherwise. */)
7042 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7043 Lisp_Object height, Lisp_Object horizontal_type)
7045 struct window *w
7046 = set_window_scroll_bars (decode_live_window (window),
7047 width, vertical_type, height, horizontal_type);
7048 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7052 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7053 0, 1, 0,
7054 doc: /* Get width and type of scroll bars of window WINDOW.
7055 WINDOW must be a live window and defaults to the selected one.
7057 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7058 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7059 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7060 value. */)
7061 (Lisp_Object window)
7063 struct window *w = decode_live_window (window);
7065 return Fcons (((w->scroll_bar_width >= 0)
7066 ? make_number (w->scroll_bar_width)
7067 : Qnil),
7068 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7069 w->vertical_scroll_bar_type,
7070 ((w->scroll_bar_height >= 0)
7071 ? make_number (w->scroll_bar_height)
7072 : Qnil),
7073 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7074 w->horizontal_scroll_bar_type));
7077 /***********************************************************************
7078 Smooth scrolling
7079 ***********************************************************************/
7081 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7082 doc: /* Return the amount by which WINDOW is scrolled vertically.
7083 If WINDOW is omitted or nil, it defaults to the selected window.
7084 Normally, value is a multiple of the canonical character height of WINDOW;
7085 optional second arg PIXELS-P means value is measured in pixels. */)
7086 (Lisp_Object window, Lisp_Object pixels_p)
7088 Lisp_Object result;
7089 struct window *w = decode_live_window (window);
7090 struct frame *f = XFRAME (w->frame);
7092 if (FRAME_WINDOW_P (f))
7093 result = (NILP (pixels_p)
7094 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7095 : make_number (-w->vscroll));
7096 else
7097 result = make_number (0);
7098 return result;
7102 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7103 2, 3, 0,
7104 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7105 WINDOW nil means use the selected window. Normally, VSCROLL is a
7106 non-negative multiple of the canonical character height of WINDOW;
7107 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7108 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7109 corresponds to an integral number of pixels. The return value is the
7110 result of this rounding.
7111 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7112 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7114 struct window *w = decode_live_window (window);
7115 struct frame *f = XFRAME (w->frame);
7117 CHECK_NUMBER_OR_FLOAT (vscroll);
7119 if (FRAME_WINDOW_P (f))
7121 int old_dy = w->vscroll;
7123 w->vscroll = - (NILP (pixels_p)
7124 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7125 : XFLOATINT (vscroll));
7126 w->vscroll = min (w->vscroll, 0);
7128 if (w->vscroll != old_dy)
7130 /* Adjust glyph matrix of the frame if the virtual display
7131 area becomes larger than before. */
7132 if (w->vscroll < 0 && w->vscroll < old_dy)
7133 adjust_frame_glyphs (f);
7135 /* Prevent redisplay shortcuts. */
7136 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = true;
7140 return Fwindow_vscroll (window, pixels_p);
7144 /* Call FN for all leaf windows on frame F. FN is called with the
7145 first argument being a pointer to the leaf window, and with
7146 additional argument USER_DATA. Stops when FN returns 0. */
7148 static void
7149 foreach_window (struct frame *f, bool (*fn) (struct window *, void *),
7150 void *user_data)
7152 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7153 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7154 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7158 /* Helper function for foreach_window. Call FN for all leaf windows
7159 reachable from W. FN is called with the first argument being a
7160 pointer to the leaf window, and with additional argument USER_DATA.
7161 Stop when FN returns false. Value is false if stopped by FN. */
7163 static bool
7164 foreach_window_1 (struct window *w, bool (*fn) (struct window *, void *),
7165 void *user_data)
7167 bool cont;
7169 for (cont = true; w && cont;)
7171 if (WINDOWP (w->contents))
7172 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7173 else
7174 cont = fn (w, user_data);
7176 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7179 return cont;
7182 /***********************************************************************
7183 Initialization
7184 ***********************************************************************/
7186 /* Return true if window configurations CONFIGURATION1 and CONFIGURATION2
7187 describe the same state of affairs. This is used by Fequal.
7189 IGNORE_POSITIONS means ignore non-matching scroll positions
7190 and the like.
7192 This ignores a couple of things like the dedication status of
7193 window, combination_limit and the like. This might have to be
7194 fixed. */
7196 bool
7197 compare_window_configurations (Lisp_Object configuration1,
7198 Lisp_Object configuration2,
7199 bool ignore_positions)
7201 register struct save_window_data *d1, *d2;
7202 struct Lisp_Vector *sws1, *sws2;
7203 ptrdiff_t i;
7205 CHECK_WINDOW_CONFIGURATION (configuration1);
7206 CHECK_WINDOW_CONFIGURATION (configuration2);
7208 d1 = (struct save_window_data *) XVECTOR (configuration1);
7209 d2 = (struct save_window_data *) XVECTOR (configuration2);
7210 sws1 = XVECTOR (d1->saved_windows);
7211 sws2 = XVECTOR (d2->saved_windows);
7213 /* Frame settings must match. */
7214 if (d1->frame_cols != d2->frame_cols
7215 || d1->frame_lines != d2->frame_lines
7216 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7217 || !EQ (d1->selected_frame, d2->selected_frame)
7218 || !EQ (d1->current_buffer, d2->current_buffer)
7219 || (!ignore_positions
7220 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7221 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7222 || !EQ (d1->focus_frame, d2->focus_frame)
7223 /* Verify that the two configurations have the same number of windows. */
7224 || sws1->header.size != sws2->header.size)
7225 return false;
7227 for (i = 0; i < sws1->header.size; i++)
7229 struct saved_window *sw1, *sw2;
7231 sw1 = SAVED_WINDOW_N (sws1, i);
7232 sw2 = SAVED_WINDOW_N (sws2, i);
7234 if (
7235 /* The "current" windows in the two configurations must
7236 correspond to each other. */
7237 EQ (d1->current_window, sw1->window)
7238 != EQ (d2->current_window, sw2->window)
7239 /* Windows' buffers must match. */
7240 || !EQ (sw1->buffer, sw2->buffer)
7241 || !EQ (sw1->pixel_left, sw2->pixel_left)
7242 || !EQ (sw1->pixel_top, sw2->pixel_top)
7243 || !EQ (sw1->pixel_height, sw2->pixel_height)
7244 || !EQ (sw1->pixel_width, sw2->pixel_width)
7245 || !EQ (sw1->left_col, sw2->left_col)
7246 || !EQ (sw1->top_line, sw2->top_line)
7247 || !EQ (sw1->total_cols, sw2->total_cols)
7248 || !EQ (sw1->total_lines, sw2->total_lines)
7249 || !EQ (sw1->display_table, sw2->display_table)
7250 /* The next two disjuncts check the window structure for
7251 equality. */
7252 || !EQ (sw1->parent, sw2->parent)
7253 || !EQ (sw1->prev, sw2->prev)
7254 || (!ignore_positions
7255 && (!EQ (sw1->hscroll, sw2->hscroll)
7256 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7257 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7258 || NILP (Fequal (sw1->start, sw2->start))
7259 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7260 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7261 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7262 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7263 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7264 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7265 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7266 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7267 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7268 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7269 return false;
7272 return true;
7275 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7276 Scompare_window_configurations, 2, 2, 0,
7277 doc: /* Compare two window configurations as regards the structure of windows.
7278 This function ignores details such as the values of point
7279 and scrolling positions. */)
7280 (Lisp_Object x, Lisp_Object y)
7282 if (compare_window_configurations (x, y, true))
7283 return Qt;
7284 return Qnil;
7287 void
7288 init_window_once (void)
7290 struct frame *f = make_initial_frame ();
7291 XSETFRAME (selected_frame, f);
7292 Vterminal_frame = selected_frame;
7293 minibuf_window = f->minibuffer_window;
7294 selected_window = f->selected_window;
7297 void
7298 init_window (void)
7300 Vwindow_list = Qnil;
7303 void
7304 syms_of_window (void)
7306 DEFSYM (Qscroll_up, "scroll-up");
7307 DEFSYM (Qscroll_down, "scroll-down");
7308 DEFSYM (Qscroll_command, "scroll-command");
7310 Fput (Qscroll_up, Qscroll_command, Qt);
7311 Fput (Qscroll_down, Qscroll_command, Qt);
7313 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7314 DEFSYM (Qwindowp, "windowp");
7315 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7316 DEFSYM (Qwindow_live_p, "window-live-p");
7317 DEFSYM (Qwindow_valid_p, "window-valid-p");
7318 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7319 DEFSYM (Qdelete_window, "delete-window");
7320 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7321 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7322 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7323 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7324 DEFSYM (Qsafe, "safe");
7325 DEFSYM (Qdisplay_buffer, "display-buffer");
7326 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7327 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7328 DEFSYM (Qget_mru_window, "get-mru-window");
7329 DEFSYM (Qwindow_size, "window-size");
7330 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7331 DEFSYM (Qabove, "above");
7332 DEFSYM (Qbelow, "below");
7333 DEFSYM (Qclone_of, "clone-of");
7334 DEFSYM (Qfloor, "floor");
7335 DEFSYM (Qceiling, "ceiling");
7337 staticpro (&Vwindow_list);
7339 minibuf_selected_window = Qnil;
7340 staticpro (&minibuf_selected_window);
7342 window_scroll_pixel_based_preserve_x = -1;
7343 window_scroll_pixel_based_preserve_y = -1;
7344 window_scroll_preserve_hpos = -1;
7345 window_scroll_preserve_vpos = -1;
7347 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7348 doc: /* Non-nil means call as function to display a help buffer.
7349 The function is called with one argument, the buffer to be displayed.
7350 Used by `with-output-to-temp-buffer'.
7351 If this function is used, then it must do the entire job of showing
7352 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7353 Vtemp_buffer_show_function = Qnil;
7355 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7356 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7357 Vminibuf_scroll_window = Qnil;
7359 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7360 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7361 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7362 is displayed in the `mode-line' face. */);
7363 mode_line_in_non_selected_windows = true;
7365 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7366 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7367 Vother_window_scroll_buffer = Qnil;
7369 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7370 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7371 auto_window_vscroll_p = true;
7373 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7374 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7375 next_screen_context_lines = 2;
7377 DEFVAR_LISP ("scroll-preserve-screen-position",
7378 Vscroll_preserve_screen_position,
7379 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7380 A value of nil means point does not keep its screen position except
7381 at the scroll margin or window boundary respectively.
7382 A value of t means point keeps its screen position if the scroll
7383 command moved it vertically out of the window, e.g. when scrolling
7384 by full screens.
7385 Any other value means point always keeps its screen position.
7386 Scroll commands should have the `scroll-command' property
7387 on their symbols to be controlled by this variable. */);
7388 Vscroll_preserve_screen_position = Qnil;
7390 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7391 doc: /* Type of marker to use for `window-point'. */);
7392 Vwindow_point_insertion_type = Qnil;
7393 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7395 DEFVAR_LISP ("window-configuration-change-hook",
7396 Vwindow_configuration_change_hook,
7397 doc: /* Functions to call when window configuration changes.
7398 The buffer-local part is run once per window, with the relevant window
7399 selected; while the global part is run only once for the modified frame,
7400 with the relevant frame selected. */);
7401 Vwindow_configuration_change_hook = Qnil;
7403 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7404 doc: /* Non-nil means `recenter' redraws entire frame.
7405 If this option is non-nil, then the `recenter' command with a nil
7406 argument will redraw the entire frame; the special value `tty' causes
7407 the frame to be redrawn only if it is a tty frame. */);
7408 Vrecenter_redisplay = Qtty;
7410 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7411 doc: /* If t, resize window combinations proportionally.
7412 If this variable is nil, splitting a window gets the entire screen space
7413 for displaying the new window from the window to split. Deleting and
7414 resizing a window preferably resizes one adjacent window only.
7416 If this variable is t, splitting a window tries to get the space
7417 proportionally from all windows in the same combination. This also
7418 allows to split a window that is otherwise too small or of fixed size.
7419 Resizing and deleting a window proportionally resize all windows in the
7420 same combination.
7422 Other values are reserved for future use.
7424 This variable takes no effect if the variable `window-combination-limit' is
7425 non-nil. */);
7426 Vwindow_combination_resize = Qnil;
7428 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7429 doc: /* If non-nil, splitting a window makes a new parent window.
7430 The following values are recognized:
7432 nil means splitting a window will create a new parent window only if the
7433 window has no parent window or the window shall become part of a
7434 combination orthogonal to the one it is part of.
7436 `window-size' means that splitting a window for displaying a buffer
7437 makes a new parent window provided `display-buffer' is supposed to
7438 explicitly set the window's size due to the presence of a
7439 `window-height' or `window-width' entry in the alist used by
7440 `display-buffer'. Otherwise, this value is handled like nil.
7442 `temp-buffer' means that splitting a window for displaying a temporary
7443 buffer always makes a new parent window. Otherwise, this value is
7444 handled like nil.
7446 `display-buffer' means that splitting a window for displaying a buffer
7447 always makes a new parent window. Since temporary buffers are
7448 displayed by the function `display-buffer', this value is stronger
7449 than `temp-buffer'. Splitting a window for other purpose makes a
7450 new parent window only if needed.
7452 t means that splitting a window always creates a new parent window. If
7453 all splits behave this way, each frame's window tree is a binary
7454 tree and every window but the frame's root window has exactly one
7455 sibling.
7457 Other values are reserved for future use. */);
7458 Vwindow_combination_limit = Qwindow_size;
7460 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7461 doc: /* Alist of persistent window parameters.
7462 This alist specifies which window parameters shall get saved by
7463 `current-window-configuration' and `window-state-get' and subsequently
7464 restored to their previous values by `set-window-configuration' and
7465 `window-state-put'.
7467 The car of each entry of this alist is the symbol specifying the
7468 parameter. The cdr is one of the following:
7470 nil means the parameter is neither saved by `window-state-get' nor by
7471 `current-window-configuration'.
7473 t means the parameter is saved by `current-window-configuration' and,
7474 provided its WRITABLE argument is nil, by `window-state-get'.
7476 The symbol `writable' means the parameter is saved unconditionally by
7477 both `current-window-configuration' and `window-state-get'. Do not use
7478 this value for parameters without read syntax (like windows or frames).
7480 Parameters not saved by `current-window-configuration' or
7481 `window-state-get' are left alone by `set-window-configuration'
7482 respectively are not installed by `window-state-put'. */);
7483 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7485 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7486 doc: /* Non-nil means resize windows pixelwise.
7487 This currently affects the functions: `split-window', `maximize-window',
7488 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7489 all functions that symmetrically resize a parent window.
7491 Note that when a frame's pixel size is not a multiple of the
7492 frame's character size, at least one window may get resized
7493 pixelwise even if this option is nil. */);
7494 window_resize_pixelwise = false;
7496 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7497 Vfast_but_imprecise_scrolling,
7498 doc: /* When non-nil, accelerate scrolling operations.
7499 This comes into play when scrolling rapidly over previously
7500 unfontified buffer regions. Only those portions of the buffer which
7501 are actually going to be displayed get fontified.
7503 Note that this optimization can cause the portion of the buffer
7504 displayed after a scrolling operation to be somewhat inaccurate. */);
7505 Vfast_but_imprecise_scrolling = false;
7507 defsubr (&Sselected_window);
7508 defsubr (&Sminibuffer_window);
7509 defsubr (&Swindow_minibuffer_p);
7510 defsubr (&Swindowp);
7511 defsubr (&Swindow_valid_p);
7512 defsubr (&Swindow_live_p);
7513 defsubr (&Swindow_frame);
7514 defsubr (&Sframe_root_window);
7515 defsubr (&Sframe_first_window);
7516 defsubr (&Sframe_selected_window);
7517 defsubr (&Sset_frame_selected_window);
7518 defsubr (&Spos_visible_in_window_p);
7519 defsubr (&Swindow_line_height);
7520 defsubr (&Swindow_buffer);
7521 defsubr (&Swindow_parent);
7522 defsubr (&Swindow_top_child);
7523 defsubr (&Swindow_left_child);
7524 defsubr (&Swindow_next_sibling);
7525 defsubr (&Swindow_prev_sibling);
7526 defsubr (&Swindow_combination_limit);
7527 defsubr (&Sset_window_combination_limit);
7528 defsubr (&Swindow_use_time);
7529 defsubr (&Swindow_pixel_width);
7530 defsubr (&Swindow_pixel_height);
7531 defsubr (&Swindow_total_width);
7532 defsubr (&Swindow_total_height);
7533 defsubr (&Swindow_normal_size);
7534 defsubr (&Swindow_new_pixel);
7535 defsubr (&Swindow_new_total);
7536 defsubr (&Swindow_new_normal);
7537 defsubr (&Swindow_pixel_left);
7538 defsubr (&Swindow_pixel_top);
7539 defsubr (&Swindow_left_column);
7540 defsubr (&Swindow_top_line);
7541 defsubr (&Sset_window_new_pixel);
7542 defsubr (&Sset_window_new_total);
7543 defsubr (&Sset_window_new_normal);
7544 defsubr (&Swindow_resize_apply);
7545 defsubr (&Swindow_resize_apply_total);
7546 defsubr (&Swindow_body_height);
7547 defsubr (&Swindow_body_width);
7548 defsubr (&Swindow_hscroll);
7549 defsubr (&Sset_window_hscroll);
7550 defsubr (&Swindow_redisplay_end_trigger);
7551 defsubr (&Sset_window_redisplay_end_trigger);
7552 defsubr (&Swindow_edges);
7553 defsubr (&Swindow_pixel_edges);
7554 defsubr (&Swindow_absolute_pixel_edges);
7555 defsubr (&Swindow_mode_line_height);
7556 defsubr (&Swindow_header_line_height);
7557 defsubr (&Swindow_right_divider_width);
7558 defsubr (&Swindow_bottom_divider_width);
7559 defsubr (&Swindow_scroll_bar_width);
7560 defsubr (&Swindow_scroll_bar_height);
7561 defsubr (&Swindow_inside_edges);
7562 defsubr (&Swindow_inside_pixel_edges);
7563 defsubr (&Swindow_inside_absolute_pixel_edges);
7564 defsubr (&Scoordinates_in_window_p);
7565 defsubr (&Swindow_at);
7566 defsubr (&Swindow_point);
7567 defsubr (&Swindow_old_point);
7568 defsubr (&Swindow_start);
7569 defsubr (&Swindow_end);
7570 defsubr (&Sset_window_point);
7571 defsubr (&Sset_window_start);
7572 defsubr (&Swindow_dedicated_p);
7573 defsubr (&Sset_window_dedicated_p);
7574 defsubr (&Swindow_display_table);
7575 defsubr (&Sset_window_display_table);
7576 defsubr (&Snext_window);
7577 defsubr (&Sprevious_window);
7578 defsubr (&Swindow__sanitize_window_sizes);
7579 defsubr (&Sget_buffer_window);
7580 defsubr (&Sdelete_other_windows_internal);
7581 defsubr (&Sdelete_window_internal);
7582 defsubr (&Sresize_mini_window_internal);
7583 defsubr (&Sset_window_buffer);
7584 defsubr (&Srun_window_configuration_change_hook);
7585 defsubr (&Srun_window_scroll_functions);
7586 defsubr (&Sselect_window);
7587 defsubr (&Sforce_window_update);
7588 defsubr (&Ssplit_window_internal);
7589 defsubr (&Sscroll_up);
7590 defsubr (&Sscroll_down);
7591 defsubr (&Sscroll_left);
7592 defsubr (&Sscroll_right);
7593 defsubr (&Sother_window_for_scrolling);
7594 defsubr (&Sscroll_other_window);
7595 defsubr (&Sminibuffer_selected_window);
7596 defsubr (&Srecenter);
7597 defsubr (&Swindow_text_width);
7598 defsubr (&Swindow_text_height);
7599 defsubr (&Smove_to_window_line);
7600 defsubr (&Swindow_configuration_p);
7601 defsubr (&Swindow_configuration_frame);
7602 defsubr (&Sset_window_configuration);
7603 defsubr (&Scurrent_window_configuration);
7604 defsubr (&Sset_window_margins);
7605 defsubr (&Swindow_margins);
7606 defsubr (&Sset_window_fringes);
7607 defsubr (&Swindow_fringes);
7608 defsubr (&Sset_window_scroll_bars);
7609 defsubr (&Swindow_scroll_bars);
7610 defsubr (&Swindow_vscroll);
7611 defsubr (&Sset_window_vscroll);
7612 defsubr (&Scompare_window_configurations);
7613 defsubr (&Swindow_list);
7614 defsubr (&Swindow_list_1);
7615 defsubr (&Swindow_prev_buffers);
7616 defsubr (&Sset_window_prev_buffers);
7617 defsubr (&Swindow_next_buffers);
7618 defsubr (&Sset_window_next_buffers);
7619 defsubr (&Swindow_parameters);
7620 defsubr (&Swindow_parameter);
7621 defsubr (&Sset_window_parameter);
7624 void
7625 keys_of_window (void)
7627 initial_define_key (control_x_map, '<', "scroll-left");
7628 initial_define_key (control_x_map, '>', "scroll-right");
7630 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7631 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7632 initial_define_key (meta_map, 'v', "scroll-down-command");