(package--append-to-alist): Updated docstring due to new name.
[emacs.git] / src / window.c
blob53a235f84463c2774f1a9316698c8d2fdf9cf0a2
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 int displayed_window_lines (struct window *);
49 static int count_windows (struct window *);
50 static int get_leaf_windows (struct window *, struct window **, int);
51 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
52 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
53 static void window_scroll_line_based (Lisp_Object, int, bool, int);
54 static int add_window_to_list (struct window *, void *);
55 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
56 Lisp_Object, int);
57 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
58 Lisp_Object *);
59 static void foreach_window (struct frame *,
60 int (* fn) (struct window *, void *),
61 void *);
62 static int foreach_window_1 (struct window *,
63 int (* fn) (struct window *, void *),
64 void *);
65 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
66 static int window_resize_check (struct window *, bool);
67 static void window_resize_apply (struct window *, bool);
68 static void window_resize_apply_total (struct window *, bool);
69 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
70 static void select_window_1 (Lisp_Object, bool);
72 static struct window *set_window_fringes (struct window *, Lisp_Object,
73 Lisp_Object, Lisp_Object);
74 static struct window *set_window_margins (struct window *, Lisp_Object,
75 Lisp_Object);
76 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
77 Lisp_Object, Lisp_Object, Lisp_Object);
78 static void apply_window_adjustment (struct window *);
80 /* This is the window in which the terminal's cursor should
81 be left when nothing is being done with it. This must
82 always be a leaf window, and its buffer is selected by
83 the top level editing loop at the end of each command.
85 This value is always the same as
86 FRAME_SELECTED_WINDOW (selected_frame). */
87 Lisp_Object selected_window;
89 /* A list of all windows for use by next_window and Fwindow_list.
90 Functions creating or deleting windows should invalidate this cache
91 by setting it to nil. */
92 Lisp_Object Vwindow_list;
94 /* The mini-buffer window of the selected frame.
95 Note that you cannot test for mini-bufferness of an arbitrary window
96 by comparing against this; but you can test for mini-bufferness of
97 the selected window. */
98 Lisp_Object minibuf_window;
100 /* Non-nil means it is the window whose mode line should be
101 shown as the selected window when the minibuffer is selected. */
102 Lisp_Object minibuf_selected_window;
104 /* Incremented for each window created. */
105 static int sequence_number;
107 /* Used by the function window_scroll_pixel_based. */
108 static int window_scroll_pixel_based_preserve_x;
109 static int window_scroll_pixel_based_preserve_y;
111 /* Same for window_scroll_line_based. */
112 static EMACS_INT window_scroll_preserve_hpos;
113 static EMACS_INT window_scroll_preserve_vpos;
115 static void
116 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
118 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
121 /* These setters are used only in this file, so they can be private. */
122 static void
123 wset_combination_limit (struct window *w, Lisp_Object val)
125 w->combination_limit = val;
128 static void
129 wset_dedicated (struct window *w, Lisp_Object val)
131 w->dedicated = val;
134 static void
135 wset_display_table (struct window *w, Lisp_Object val)
137 w->display_table = val;
140 static void
141 wset_new_normal (struct window *w, Lisp_Object val)
143 w->new_normal = val;
146 static void
147 wset_new_total (struct window *w, Lisp_Object val)
149 w->new_total = val;
152 static void
153 wset_normal_cols (struct window *w, Lisp_Object val)
155 w->normal_cols = val;
158 static void
159 wset_normal_lines (struct window *w, Lisp_Object val)
161 w->normal_lines = val;
164 static void
165 wset_parent (struct window *w, Lisp_Object val)
167 w->parent = val;
170 static void
171 wset_pointm (struct window *w, Lisp_Object val)
173 w->pointm = val;
176 static void
177 wset_old_pointm (struct window *w, Lisp_Object val)
179 w->old_pointm = val;
182 static void
183 wset_start (struct window *w, Lisp_Object val)
185 w->start = val;
188 static void
189 wset_temslot (struct window *w, Lisp_Object val)
191 w->temslot = val;
194 static void
195 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
197 w->vertical_scroll_bar_type = val;
200 static void
201 wset_window_parameters (struct window *w, Lisp_Object val)
203 w->window_parameters = val;
206 static void
207 wset_combination (struct window *w, bool horflag, Lisp_Object val)
209 /* Since leaf windows never becomes non-leaf, there should
210 be no buffer and markers in start and pointm fields of W. */
211 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
212 w->contents = val;
213 /* When an internal window is deleted and VAL is nil, HORFLAG
214 is meaningless. */
215 if (!NILP (val))
216 w->horizontal = horflag;
219 /* Nonzero if leaf window W doesn't reflect the actual state
220 of displayed buffer due to its text or overlays change. */
222 bool
223 window_outdated (struct window *w)
225 struct buffer *b = XBUFFER (w->contents);
226 return (w->last_modified < BUF_MODIFF (b)
227 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
230 struct window *
231 decode_live_window (register Lisp_Object window)
233 if (NILP (window))
234 return XWINDOW (selected_window);
236 CHECK_LIVE_WINDOW (window);
237 return XWINDOW (window);
240 struct window *
241 decode_any_window (register Lisp_Object window)
243 struct window *w;
245 if (NILP (window))
246 return XWINDOW (selected_window);
248 CHECK_WINDOW (window);
249 w = XWINDOW (window);
250 return w;
253 static struct window *
254 decode_valid_window (register Lisp_Object window)
256 struct window *w;
258 if (NILP (window))
259 return XWINDOW (selected_window);
261 CHECK_VALID_WINDOW (window);
262 w = XWINDOW (window);
263 return w;
266 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
267 cease its buffer, and 1 means that W is about to set up the new one. */
269 static void
270 adjust_window_count (struct window *w, int arg)
272 eassert (eabs (arg) == 1);
273 if (BUFFERP (w->contents))
275 struct buffer *b = XBUFFER (w->contents);
277 if (b->base_buffer)
278 b = b->base_buffer;
279 b->window_count += arg;
280 eassert (b->window_count >= 0);
281 /* These should be recalculated by redisplay code. */
282 w->window_end_valid = 0;
283 w->base_line_pos = 0;
287 /* Set W's buffer slot to VAL and recompute number
288 of windows showing VAL if it is a buffer. */
290 void
291 wset_buffer (struct window *w, Lisp_Object val)
293 adjust_window_count (w, -1);
294 if (BUFFERP (val))
295 /* Make sure that we do not assign the buffer
296 to an internal window. */
297 eassert (MARKERP (w->start) && MARKERP (w->pointm));
298 w->contents = val;
299 adjust_window_count (w, 1);
302 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
303 doc: /* Return t if OBJECT is a window and nil otherwise. */)
304 (Lisp_Object object)
306 return WINDOWP (object) ? Qt : Qnil;
309 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
310 doc: /* Return t if OBJECT is a valid window and nil otherwise.
311 A valid window is either a window that displays a buffer or an internal
312 window. Windows that have been deleted are not valid. */)
313 (Lisp_Object object)
315 return WINDOW_VALID_P (object) ? Qt : Qnil;
318 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
319 doc: /* Return t if OBJECT is a live window and nil otherwise.
320 A live window is a window that displays a buffer.
321 Internal windows and deleted windows are not live. */)
322 (Lisp_Object object)
324 return WINDOW_LIVE_P (object) ? Qt : Qnil;
327 /* Frames and windows. */
328 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
329 doc: /* Return the frame that window WINDOW is on.
330 WINDOW must be a valid window and defaults to the selected one. */)
331 (Lisp_Object window)
333 return decode_valid_window (window)->frame;
336 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
337 doc: /* Return the root window of FRAME-OR-WINDOW.
338 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
339 With a frame argument, return that frame's root window.
340 With a window argument, return the root window of that window's frame. */)
341 (Lisp_Object frame_or_window)
343 Lisp_Object window;
345 if (NILP (frame_or_window))
346 window = SELECTED_FRAME ()->root_window;
347 else if (WINDOW_VALID_P (frame_or_window))
348 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
349 else
351 CHECK_LIVE_FRAME (frame_or_window);
352 window = XFRAME (frame_or_window)->root_window;
355 return window;
358 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
359 doc: /* Return the minibuffer window for frame FRAME.
360 If FRAME is omitted or nil, it defaults to the selected frame. */)
361 (Lisp_Object frame)
363 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
366 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
367 Swindow_minibuffer_p, 0, 1, 0,
368 doc: /* Return non-nil if WINDOW is a minibuffer window.
369 WINDOW must be a valid window and defaults to the selected one. */)
370 (Lisp_Object window)
372 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
375 /* Don't move this to window.el - this must be a safe routine. */
376 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
377 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
378 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
379 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
380 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
381 the first window of that frame. */)
382 (Lisp_Object frame_or_window)
384 Lisp_Object window;
386 if (NILP (frame_or_window))
387 window = SELECTED_FRAME ()->root_window;
388 else if (WINDOW_VALID_P (frame_or_window))
389 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
390 else
392 CHECK_LIVE_FRAME (frame_or_window);
393 window = XFRAME (frame_or_window)->root_window;
396 while (WINDOWP (XWINDOW (window)->contents))
397 window = XWINDOW (window)->contents;
399 return window;
402 DEFUN ("frame-selected-window", Fframe_selected_window,
403 Sframe_selected_window, 0, 1, 0,
404 doc: /* Return the selected window of FRAME-OR-WINDOW.
405 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
406 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
407 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
408 return the selected window of that frame. */)
409 (Lisp_Object frame_or_window)
411 Lisp_Object window;
413 if (NILP (frame_or_window))
414 window = SELECTED_FRAME ()->selected_window;
415 else if (WINDOW_VALID_P (frame_or_window))
416 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
417 else
419 CHECK_LIVE_FRAME (frame_or_window);
420 window = XFRAME (frame_or_window)->selected_window;
423 return window;
426 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
427 Sset_frame_selected_window, 2, 3, 0,
428 doc: /* Set selected window of FRAME to WINDOW.
429 FRAME must be a live frame and defaults to the selected one. If FRAME
430 is the selected frame, this makes WINDOW the selected window. Optional
431 argument NORECORD non-nil means to neither change the order of recently
432 selected windows nor the buffer list. WINDOW must denote a live window.
433 Return WINDOW. */)
434 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
436 if (NILP (frame))
437 frame = selected_frame;
439 CHECK_LIVE_FRAME (frame);
440 CHECK_LIVE_WINDOW (window);
442 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
443 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
445 if (EQ (frame, selected_frame))
446 return Fselect_window (window, norecord);
447 else
449 fset_selected_window (XFRAME (frame), window);
450 return window;
454 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
455 doc: /* Return the selected window.
456 The selected window is the window in which the standard cursor for
457 selected windows appears and to which many commands apply. */)
458 (void)
460 return selected_window;
463 int window_select_count;
465 /* If select_window is called with inhibit_point_swap non-zero it will
466 not store point of the old selected window's buffer back into that
467 window's pointm slot. This is needed by Fset_window_configuration to
468 avoid that the display routine is called with selected_window set to
469 Qnil causing a subsequent crash. */
470 static Lisp_Object
471 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
473 register struct window *w;
474 struct frame *sf;
476 CHECK_LIVE_WINDOW (window);
478 w = XWINDOW (window);
480 /* Make the selected window's buffer current. */
481 Fset_buffer (w->contents);
483 if (EQ (window, selected_window) && !inhibit_point_swap)
484 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
485 way to call record_buffer from Elisp, so it's important that we call
486 record_buffer before returning here. */
487 goto record_and_return;
489 if (NILP (norecord))
490 { /* Mark the window for redisplay since the selected-window has
491 a different mode-line. */
492 wset_redisplay (XWINDOW (selected_window));
493 wset_redisplay (w);
495 else
496 redisplay_other_windows ();
498 sf = SELECTED_FRAME ();
499 if (XFRAME (WINDOW_FRAME (w)) != sf)
501 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
502 /* Use this rather than Fhandle_switch_frame
503 so that FRAME_FOCUS_FRAME is moved appropriately as we
504 move around in the state where a minibuffer in a separate
505 frame is active. */
506 Fselect_frame (WINDOW_FRAME (w), norecord);
507 /* Fselect_frame called us back so we've done all the work already. */
508 eassert (EQ (window, selected_window));
509 return window;
511 else
512 fset_selected_window (sf, window);
514 select_window_1 (window, inhibit_point_swap);
515 bset_last_selected_window (XBUFFER (w->contents), window);
517 record_and_return:
518 /* record_buffer can run QUIT, so make sure it is run only after we have
519 re-established the invariant between selected_window and selected_frame,
520 otherwise the temporary broken invariant might "escape" (bug#14161). */
521 if (NILP (norecord))
523 w->use_time = ++window_select_count;
524 record_buffer (w->contents);
527 return window;
530 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
531 (not even for redisplay's benefit), and assume that the window's frame is
532 already selected. */
533 static void
534 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
536 /* Store the old selected window's buffer's point in pointm of the old
537 selected window. It belongs to that window, and when the window is
538 not selected, must be in the window. */
539 if (!inhibit_point_swap)
541 struct window *ow = XWINDOW (selected_window);
542 if (BUFFERP (ow->contents))
543 set_marker_both (ow->pointm, ow->contents,
544 BUF_PT (XBUFFER (ow->contents)),
545 BUF_PT_BYTE (XBUFFER (ow->contents)));
548 selected_window = window;
550 /* Go to the point recorded in the window.
551 This is important when the buffer is in more
552 than one window. It also matters when
553 redisplay_window has altered point after scrolling,
554 because it makes the change only in the window. */
555 set_point_from_marker (XWINDOW (window)->pointm);
558 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
559 doc: /* Select WINDOW which must be a live window.
560 Also make WINDOW's frame the selected frame and WINDOW that frame's
561 selected window. In addition, make WINDOW's buffer current and set its
562 buffer's value of `point' to the value of WINDOW's `window-point'.
563 Return WINDOW.
565 Optional second arg NORECORD non-nil means do not put this buffer at the
566 front of the buffer list and do not make this window the most recently
567 selected one.
569 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
570 applications and internal routines often select a window temporarily for
571 various purposes; mostly, to simplify coding. As a rule, such
572 selections should be not recorded and therefore will not pollute
573 `buffer-list-update-hook'. Selections that "really count" are those
574 causing a visible change in the next redisplay of WINDOW's frame and
575 should be always recorded. So if you think of running a function each
576 time a window gets selected put it on `buffer-list-update-hook'.
578 Also note that the main editor command loop sets the current buffer to
579 the buffer of the selected window before each command. */)
580 (register Lisp_Object window, Lisp_Object norecord)
582 return select_window (window, norecord, 0);
585 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
586 doc: /* Return the buffer displayed in window WINDOW.
587 If WINDOW is omitted or nil, it defaults to the selected window.
588 Return nil for an internal window or a deleted window. */)
589 (Lisp_Object window)
591 struct window *w = decode_any_window (window);
592 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
595 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
596 doc: /* Return the parent window of window WINDOW.
597 WINDOW must be a valid window and defaults to the selected one.
598 Return nil for a window with no parent (e.g. a root window). */)
599 (Lisp_Object window)
601 return decode_valid_window (window)->parent;
604 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
605 doc: /* Return the topmost child window of window WINDOW.
606 WINDOW must be a valid window and defaults to the selected one.
607 Return nil if WINDOW is a live window (live windows have no children).
608 Return nil if WINDOW is an internal window whose children form a
609 horizontal combination. */)
610 (Lisp_Object window)
612 struct window *w = decode_valid_window (window);
613 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
616 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
617 doc: /* Return the leftmost child window of window WINDOW.
618 WINDOW must be a valid window and defaults to the selected one.
619 Return nil if WINDOW is a live window (live windows have no children).
620 Return nil if WINDOW is an internal window whose children form a
621 vertical combination. */)
622 (Lisp_Object window)
624 struct window *w = decode_valid_window (window);
625 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
628 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
629 doc: /* Return the next sibling window of window WINDOW.
630 WINDOW must be a valid window and defaults to the selected one.
631 Return nil if WINDOW has no next sibling. */)
632 (Lisp_Object window)
634 return decode_valid_window (window)->next;
637 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
638 doc: /* Return the previous sibling window of window WINDOW.
639 WINDOW must be a valid window and defaults to the selected one.
640 Return nil if WINDOW has no previous sibling. */)
641 (Lisp_Object window)
643 return decode_valid_window (window)->prev;
646 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
647 doc: /* Return combination limit of window WINDOW.
648 WINDOW must be a valid window used in horizontal or vertical combination.
649 If the return value is nil, child windows of WINDOW can be recombined with
650 WINDOW's siblings. A return value of t means that child windows of
651 WINDOW are never \(re-)combined with WINDOW's siblings. */)
652 (Lisp_Object window)
654 struct window *w;
656 CHECK_VALID_WINDOW (window);
657 w = XWINDOW (window);
658 if (WINDOW_LEAF_P (w))
659 error ("Combination limit is meaningful for internal windows only");
660 return w->combination_limit;
663 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
664 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
665 WINDOW must be a valid window used in horizontal or vertical combination.
666 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
667 siblings. LIMIT t means that child windows of WINDOW are never
668 \(re-)combined with WINDOW's siblings. Other values are reserved for
669 future use. */)
670 (Lisp_Object window, Lisp_Object limit)
672 struct window *w;
674 CHECK_VALID_WINDOW (window);
675 w = XWINDOW (window);
676 if (WINDOW_LEAF_P (w))
677 error ("Combination limit is meaningful for internal windows only");
678 wset_combination_limit (w, limit);
679 return limit;
682 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
683 doc: /* Return the use time of window WINDOW.
684 WINDOW must be a live window and defaults to the selected one.
685 The window with the highest use time is the most recently selected
686 one. The window with the lowest use time is the least recently
687 selected one. */)
688 (Lisp_Object window)
690 return make_number (decode_live_window (window)->use_time);
693 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
694 doc: /* Return the width of window WINDOW in pixels.
695 WINDOW must be a valid window and defaults to the selected one.
697 The return value includes the fringes and margins of WINDOW as well as
698 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
699 an internal window, its pixel width is the width of the screen areas
700 spanned by its children. */)
701 (Lisp_Object window)
703 return make_number (decode_valid_window (window)->pixel_width);
706 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
707 doc: /* Return the height of window WINDOW in pixels.
708 WINDOW must be a valid window and defaults to the selected one.
710 The return value includes the mode line and header line and the bottom
711 divider, if any. If WINDOW is an internal window, its pixel height is
712 the height of the screen areas spanned by its children. */)
713 (Lisp_Object window)
715 return make_number (decode_valid_window (window)->pixel_height);
718 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
719 doc: /* Return the height of window WINDOW in lines.
720 WINDOW must be a valid window and defaults to the selected one.
722 The return value includes the heights of WINDOW's mode and header line
723 and its bottom divider, if any. If WINDOW is an internal window, the
724 total height is the height of the screen areas spanned by its children.
726 If WINDOW's pixel height is not an integral multiple of its frame's
727 character height, the number of lines occupied by WINDOW is rounded
728 internally. This is done in a way such that, if WINDOW is a parent
729 window, the sum of the total heights of all its children internally
730 equals the total height of WINDOW.
732 If the optional argument ROUND is `ceiling', return the smallest integer
733 larger than WINDOW's pixel height divided by the character height of
734 WINDOW's frame. ROUND `floor' means to return the largest integer
735 smaller than WINDOW's pixel height divided by the character height of
736 WINDOW's frame. Any other value of ROUND means to return the internal
737 total height of WINDOW. */)
738 (Lisp_Object window, Lisp_Object round)
740 struct window *w = decode_valid_window (window);
742 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
743 return make_number (w->total_lines);
744 else
746 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
748 return make_number (EQ (round, Qceiling)
749 ? ((w->pixel_height + unit - 1) /unit)
750 : (w->pixel_height / unit));
754 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
755 doc: /* Return the total width of window WINDOW in columns.
756 WINDOW must be a valid window and defaults to the selected one.
758 The return value includes the widths of WINDOW's fringes, margins,
759 scroll bars and its right divider, if any. If WINDOW is an internal
760 window, the total width is the width of the screen areas spanned by its
761 children.
763 If WINDOW's pixel width is not an integral multiple of its frame's
764 character width, the number of lines occupied by WINDOW is rounded
765 internally. This is done in a way such that, if WINDOW is a parent
766 window, the sum of the total widths of all its children internally
767 equals the total width of WINDOW.
769 If the optional argument ROUND is `ceiling', return the smallest integer
770 larger than WINDOW's pixel width divided by the character width of
771 WINDOW's frame. ROUND `floor' means to return the largest integer
772 smaller than WINDOW's pixel width divided by the character width of
773 WINDOW's frame. Any other value of ROUND means to return the internal
774 total width of WINDOW. */)
775 (Lisp_Object window, Lisp_Object round)
777 struct window *w = decode_valid_window (window);
779 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
780 return make_number (w->total_cols);
781 else
783 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
785 return make_number (EQ (round, Qceiling)
786 ? ((w->pixel_width + unit - 1) /unit)
787 : (w->pixel_width / unit));
791 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
792 doc: /* Return the new total size of window WINDOW.
793 WINDOW must be a valid window and defaults to the selected one.
795 The new total size of WINDOW is the value set by the last call of
796 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
797 installed as WINDOW's total height (see `window-total-height') or total
798 width (see `window-total-width'). */)
799 (Lisp_Object window)
801 return decode_valid_window (window)->new_total;
804 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
805 doc: /* Return the normal height of window WINDOW.
806 WINDOW must be a valid window and defaults to the selected one.
807 If HORIZONTAL is non-nil, return the normal width of WINDOW.
809 The normal height of a frame's root window or a window that is
810 horizontally combined (a window that has a left or right sibling) is
811 1.0. The normal height of a window that is vertically combined (has a
812 sibling above or below) is the fraction of the window's height with
813 respect to its parent. The sum of the normal heights of all windows in a
814 vertical combination equals 1.0.
816 Similarly, the normal width of a frame's root window or a window that is
817 vertically combined equals 1.0. The normal width of a window that is
818 horizontally combined is the fraction of the window's width with respect
819 to its parent. The sum of the normal widths of all windows in a
820 horizontal combination equals 1.0.
822 The normal sizes of windows are used to restore the proportional sizes
823 of windows after they have been shrunk to their minimum sizes; for
824 example when a frame is temporarily made very small and afterwards gets
825 re-enlarged to its previous size. */)
826 (Lisp_Object window, Lisp_Object horizontal)
828 struct window *w = decode_valid_window (window);
830 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
833 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
834 doc: /* Return new normal size of window WINDOW.
835 WINDOW must be a valid window and defaults to the selected one.
837 The new normal size of WINDOW is the value set by the last call of
838 `set-window-new-normal' for WINDOW. If valid, it will be shortly
839 installed as WINDOW's normal size (see `window-normal-size'). */)
840 (Lisp_Object window)
842 return decode_valid_window (window)->new_normal;
845 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
846 doc: /* Return new pixel size of window WINDOW.
847 WINDOW must be a valid window and defaults to the selected one.
849 The new pixel size of WINDOW is the value set by the last call of
850 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
851 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
852 width (see `window-pixel-width'). */)
853 (Lisp_Object window)
855 return decode_valid_window (window)->new_pixel;
858 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
859 doc: /* Return left pixel edge of window WINDOW.
860 WINDOW must be a valid window and defaults to the selected one. */)
861 (Lisp_Object window)
863 return make_number (decode_valid_window (window)->pixel_left);
866 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
867 doc: /* Return top pixel edge of window WINDOW.
868 WINDOW must be a valid window and defaults to the selected one. */)
869 (Lisp_Object window)
871 return make_number (decode_valid_window (window)->pixel_top);
874 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
875 doc: /* Return left column of window WINDOW.
876 This is the distance, in columns, between the left edge of WINDOW and
877 the left edge of the frame's window area. For instance, the return
878 value is 0 if there is no window to the left of WINDOW.
880 WINDOW must be a valid window and defaults to the selected one. */)
881 (Lisp_Object window)
883 return make_number (decode_valid_window (window)->left_col);
886 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
887 doc: /* Return top line of window WINDOW.
888 This is the distance, in lines, between the top of WINDOW and the top
889 of the frame's window area. For instance, the return value is 0 if
890 there is no window above WINDOW.
892 WINDOW must be a valid window and defaults to the selected one. */)
893 (Lisp_Object window)
895 return make_number (decode_valid_window (window)->top_line);
898 /* Return the number of lines/pixels of W's body. Don't count any mode
899 or header line or horizontal divider of W. Rounds down to nearest
900 integer when not working pixelwise. */
901 static int
902 window_body_height (struct window *w, bool pixelwise)
904 int height = (w->pixel_height
905 - WINDOW_HEADER_LINE_HEIGHT (w)
906 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
907 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
908 : 0)
909 - WINDOW_MODE_LINE_HEIGHT (w)
910 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
912 /* Don't return a negative value. */
913 return max (pixelwise
914 ? height
915 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
919 /* Return the number of columns/pixels of W's body. Don't count columns
920 occupied by the scroll bar or the divider/vertical bar separating W
921 from its right sibling or margins. On window-systems don't count
922 fringes either. Round down to nearest integer when not working
923 pixelwise. */
925 window_body_width (struct window *w, bool pixelwise)
927 struct frame *f = XFRAME (WINDOW_FRAME (w));
929 int width = (w->pixel_width
930 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
931 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
932 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
933 : ((!FRAME_WINDOW_P (f)
934 && !WINDOW_RIGHTMOST_P (w)
935 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
936 /* A vertical bar is either 1 or 0. */
937 ? 1 : 0))
938 - WINDOW_MARGINS_WIDTH (w)
939 - (FRAME_WINDOW_P (f)
940 ? WINDOW_FRINGES_WIDTH (w)
941 : 0));
943 /* Don't return a negative value. */
944 return max (pixelwise
945 ? width
946 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
950 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
951 doc: /* Return the height of WINDOW's text area.
952 WINDOW must be a live window and defaults to the selected one. Optional
953 argument PIXELWISE non-nil means return the height of WINDOW's text area
954 in pixels. The return value does not include the mode line or header
955 line or any horizontal divider.
957 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
958 pixel height divided by the character height of WINDOW's frame. This
959 means that if a line at the bottom of the text area is only partially
960 visible, that line is not counted. */)
961 (Lisp_Object window, Lisp_Object pixelwise)
963 return make_number (window_body_height (decode_live_window (window),
964 NILP (pixelwise) ? 0 : 1));
967 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
968 doc: /* Return the width of WINDOW's text area.
969 WINDOW must be a live window and defaults to the selected one. Optional
970 argument PIXELWISE non-nil means return the width in pixels. The return
971 value does not include any vertical dividers, fringes or marginal areas,
972 or scroll bars.
974 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
975 pixel width divided by the character width of WINDOW's frame. This
976 means that if a column at the right of the text area is only partially
977 visible, that column is not counted.
979 Note that the returned value includes the column reserved for the
980 continuation glyph. */)
981 (Lisp_Object window, Lisp_Object pixelwise)
983 return make_number (window_body_width (decode_live_window (window),
984 NILP (pixelwise) ? 0 : 1));
987 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
988 Swindow_mode_line_height, 0, 1, 0,
989 doc: /* Return the height in pixels of WINDOW's mode-line.
990 WINDOW must be a live window and defaults to the selected one. */)
991 (Lisp_Object window)
993 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
996 DEFUN ("window-header-line-height", Fwindow_header_line_height,
997 Swindow_header_line_height, 0, 1, 0,
998 doc: /* Return the height in pixels of WINDOW's header-line.
999 WINDOW must be a live window and defaults to the selected one. */)
1000 (Lisp_Object window)
1002 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1005 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1006 Swindow_right_divider_width, 0, 1, 0,
1007 doc: /* Return the width in pixels of WINDOW's right divider.
1008 WINDOW must be a live window and defaults to the selected one. */)
1009 (Lisp_Object window)
1011 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1014 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1015 Swindow_bottom_divider_width, 0, 1, 0,
1016 doc: /* Return the width in pixels of WINDOW's bottom divider.
1017 WINDOW must be a live window and defaults to the selected one. */)
1018 (Lisp_Object window)
1020 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1023 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1024 Swindow_scroll_bar_width, 0, 1, 0,
1025 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1026 WINDOW must be a live window and defaults to the selected one. */)
1027 (Lisp_Object window)
1029 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1032 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1033 Swindow_scroll_bar_height, 0, 1, 0,
1034 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1035 WINDOW must be a live window and defaults to the selected one. */)
1036 (Lisp_Object window)
1038 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1041 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1042 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1043 WINDOW must be a live window and defaults to the selected one. */)
1044 (Lisp_Object window)
1046 return make_number (decode_live_window (window)->hscroll);
1049 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1050 range, returning the new amount as a fixnum. */
1051 static Lisp_Object
1052 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1054 /* Horizontal scrolling has problems with large scroll amounts.
1055 It's too slow with long lines, and even with small lines the
1056 display can be messed up. For now, though, impose only the limits
1057 required by the internal representation: horizontal scrolling must
1058 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1059 (since it's stored in a ptrdiff_t). */
1060 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1061 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1063 /* Prevent redisplay shortcuts when changing the hscroll. */
1064 if (w->hscroll != new_hscroll)
1065 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1067 w->hscroll = new_hscroll;
1068 w->suspend_auto_hscroll = 1;
1070 return make_number (new_hscroll);
1073 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1074 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1075 WINDOW must be a live window and defaults to the selected one.
1076 Clip the number to a reasonable value if out of range.
1077 Return the new number. NCOL should be zero or positive.
1079 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1080 window so that the location of point moves off-window. */)
1081 (Lisp_Object window, Lisp_Object ncol)
1083 CHECK_NUMBER (ncol);
1084 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1087 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1088 Swindow_redisplay_end_trigger, 0, 1, 0,
1089 doc: /* Return WINDOW's redisplay end trigger value.
1090 WINDOW must be a live window and defaults to the selected one.
1091 See `set-window-redisplay-end-trigger' for more information. */)
1092 (Lisp_Object window)
1094 return decode_live_window (window)->redisplay_end_trigger;
1097 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1098 Sset_window_redisplay_end_trigger, 2, 2, 0,
1099 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1100 WINDOW must be a live window and defaults to the selected one. VALUE
1101 should be a buffer position (typically a marker) or nil. If it is a
1102 buffer position, then if redisplay in WINDOW reaches a position beyond
1103 VALUE, the functions in `redisplay-end-trigger-functions' are called
1104 with two arguments: WINDOW, and the end trigger value. Afterwards the
1105 end-trigger value is reset to nil. */)
1106 (register Lisp_Object window, Lisp_Object value)
1108 wset_redisplay_end_trigger (decode_live_window (window), value);
1109 return value;
1112 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1113 doc: /* Return a list of the edge coordinates of WINDOW.
1114 WINDOW must be a valid window and defaults to the selected one.
1116 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1117 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1118 0 at top left corner of frame.
1120 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1121 is one more than the bottommost row occupied by WINDOW. The edges
1122 include the space used by WINDOW's scroll bar, display margins, fringes,
1123 header line, and/or mode line. For the edges of just the text area, use
1124 `window-inside-edges'. */)
1125 (Lisp_Object window)
1127 register struct window *w = decode_valid_window (window);
1129 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1130 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1133 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1134 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1135 WINDOW must be a valid window and defaults to the selected one.
1137 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1138 0, 0 at the top left corner of the frame.
1140 RIGHT is one more than the rightmost x position occupied by WINDOW.
1141 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1142 The pixel edges include the space used by WINDOW's scroll bar, display
1143 margins, fringes, header line, and/or mode line. For the pixel edges
1144 of just the text area, use `window-inside-pixel-edges'. */)
1145 (Lisp_Object window)
1147 register struct window *w = decode_valid_window (window);
1149 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1150 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1153 static void
1154 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1156 struct frame *f = XFRAME (w->frame);
1157 *add_y = f->top_pos;
1158 #ifdef FRAME_MENUBAR_HEIGHT
1159 *add_y += FRAME_MENUBAR_HEIGHT (f);
1160 #endif
1161 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1162 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1163 #elif defined (FRAME_TOOLBAR_HEIGHT)
1164 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1165 #endif
1166 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1167 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1168 #endif
1169 *add_x = f->left_pos;
1170 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1171 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1172 #endif
1175 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1176 Swindow_absolute_pixel_edges, 0, 1, 0,
1177 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1178 WINDOW must be a valid window and defaults to the selected one.
1180 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1181 0, 0 at the top left corner of the display.
1183 RIGHT is one more than the rightmost x position occupied by WINDOW.
1184 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1185 The pixel edges include the space used by WINDOW's scroll bar, display
1186 margins, fringes, header line, and/or mode line. For the pixel edges
1187 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1188 (Lisp_Object window)
1190 register struct window *w = decode_valid_window (window);
1191 int add_x, add_y;
1193 calc_absolute_offset (w, &add_x, &add_y);
1195 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1196 WINDOW_TOP_EDGE_Y (w) + add_y,
1197 WINDOW_RIGHT_EDGE_X (w) + add_x,
1198 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1201 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1202 doc: /* Return a list of the edge coordinates of WINDOW.
1203 WINDOW must be a live window and defaults to the selected one.
1205 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1206 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1207 0 at top left corner of frame.
1209 RIGHT is one more than the rightmost column of WINDOW's text area.
1210 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1211 inside edges do not include the space used by the WINDOW's scroll bar,
1212 display margins, fringes, header line, and/or mode line. */)
1213 (Lisp_Object window)
1215 register struct window *w = decode_live_window (window);
1217 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1218 + WINDOW_LEFT_MARGIN_COLS (w)
1219 + ((WINDOW_LEFT_FRINGE_WIDTH (w)
1220 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1221 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1222 (WINDOW_TOP_EDGE_LINE (w)
1223 + WINDOW_HEADER_LINE_LINES (w)),
1224 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1225 - WINDOW_RIGHT_MARGIN_COLS (w)
1226 - ((WINDOW_RIGHT_FRINGE_WIDTH (w)
1227 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1228 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1229 (WINDOW_BOTTOM_EDGE_LINE (w)
1230 - WINDOW_MODE_LINE_LINES (w)));
1233 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1234 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1235 WINDOW must be a live window and defaults to the selected one.
1237 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1238 (0,0) at the top left corner of the frame's window area.
1240 RIGHT is one more than the rightmost x position of WINDOW's text area.
1241 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1242 The inside edges do not include the space used by WINDOW's scroll bar,
1243 display margins, fringes, header line, and/or mode line. */)
1244 (Lisp_Object window)
1246 register struct window *w = decode_live_window (window);
1248 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1249 + WINDOW_LEFT_MARGIN_WIDTH (w)
1250 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1251 (WINDOW_TOP_EDGE_Y (w)
1252 + WINDOW_HEADER_LINE_HEIGHT (w)),
1253 (WINDOW_BOX_RIGHT_EDGE_X (w)
1254 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1255 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1256 (WINDOW_BOTTOM_EDGE_Y (w)
1257 - WINDOW_MODE_LINE_HEIGHT (w)));
1260 DEFUN ("window-inside-absolute-pixel-edges",
1261 Fwindow_inside_absolute_pixel_edges,
1262 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1263 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1264 WINDOW must be a live window and defaults to the selected one.
1266 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1267 (0,0) at the top left corner of the frame's window area.
1269 RIGHT is one more than the rightmost x position of WINDOW's text area.
1270 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1271 The inside edges do not include the space used by WINDOW's scroll bar,
1272 display margins, fringes, header line, and/or mode line. */)
1273 (Lisp_Object window)
1275 register struct window *w = decode_live_window (window);
1276 int add_x, add_y;
1278 calc_absolute_offset (w, &add_x, &add_y);
1280 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1281 + WINDOW_LEFT_MARGIN_WIDTH (w)
1282 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1283 (WINDOW_TOP_EDGE_Y (w)
1284 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1285 (WINDOW_BOX_RIGHT_EDGE_X (w)
1286 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1287 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1288 (WINDOW_BOTTOM_EDGE_Y (w)
1289 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1292 /* Test if the character at column X, row Y is within window W.
1293 If it is not, return ON_NOTHING;
1294 if it is on the window's vertical divider, return
1295 ON_RIGHT_DIVIDER;
1296 if it is on the window's horizontal divider, return
1297 ON_BOTTOM_DIVIDER;
1298 if it is in the window's text area, return ON_TEXT;
1299 if it is on the window's modeline, return ON_MODE_LINE;
1300 if it is on the border between the window and its right sibling,
1301 return ON_VERTICAL_BORDER;
1302 if it is on a scroll bar, return ON_SCROLL_BAR;
1303 if it is on the window's top line, return ON_HEADER_LINE;
1304 if it is in left or right fringe of the window,
1305 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1306 if it is in the marginal area to the left/right of the window,
1307 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1309 X and Y are frame relative pixel coordinates. */
1311 static enum window_part
1312 coordinates_in_window (register struct window *w, int x, int y)
1314 struct frame *f = XFRAME (WINDOW_FRAME (w));
1315 enum window_part part;
1316 int ux = FRAME_COLUMN_WIDTH (f);
1317 int left_x = WINDOW_LEFT_EDGE_X (w);
1318 int right_x = WINDOW_RIGHT_EDGE_X (w);
1319 int top_y = WINDOW_TOP_EDGE_Y (w);
1320 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1321 /* The width of the area where the vertical line can be dragged.
1322 (Between mode lines for instance. */
1323 int grabbable_width = ux;
1324 int lmargin_width, rmargin_width, text_left, text_right;
1326 /* Outside any interesting row or column? */
1327 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1328 return ON_NOTHING;
1330 /* On the horizontal window divider (which prevails the vertical
1331 divider)? */
1332 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1333 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1334 && y <= bottom_y)
1335 return ON_BOTTOM_DIVIDER;
1336 /* On vertical window divider? */
1337 else if (!WINDOW_RIGHTMOST_P (w)
1338 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1339 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1340 && x <= right_x)
1341 return ON_RIGHT_DIVIDER;
1342 /* On the horizontal scroll bar? (Including the empty space at its
1343 right!) */
1344 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1345 && y >= (bottom_y
1346 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1347 - CURRENT_MODE_LINE_HEIGHT (w)
1348 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1349 && y <= (bottom_y
1350 - CURRENT_MODE_LINE_HEIGHT (w)
1351 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1352 return ON_HORIZONTAL_SCROLL_BAR;
1353 /* On the mode or header line? */
1354 else if ((WINDOW_WANTS_MODELINE_P (w)
1355 && y >= (bottom_y
1356 - CURRENT_MODE_LINE_HEIGHT (w)
1357 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1358 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1359 && (part = ON_MODE_LINE))
1360 || (WINDOW_WANTS_HEADER_LINE_P (w)
1361 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1362 && (part = ON_HEADER_LINE)))
1364 /* If it's under/over the scroll bar portion of the mode/header
1365 line, say it's on the vertical line. That's to be able to
1366 resize windows horizontally in case we're using toolkit scroll
1367 bars. Note: If scrollbars are on the left, the window that
1368 must be eventually resized is that on the left of WINDOW. */
1369 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1370 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1371 && !WINDOW_LEFTMOST_P (w)
1372 && eabs (x - left_x) < grabbable_width)
1373 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1374 && !WINDOW_RIGHTMOST_P (w)
1375 && eabs (x - right_x) < grabbable_width)))
1376 return ON_VERTICAL_BORDER;
1377 else
1378 return part;
1381 /* In what's below, we subtract 1 when computing right_x because we
1382 want the rightmost pixel, which is given by left_pixel+width-1. */
1383 if (w->pseudo_window_p)
1385 left_x = 0;
1386 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1388 else
1390 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1391 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1394 /* Outside any interesting column? */
1395 if (x < left_x || x > right_x)
1396 return ON_VERTICAL_SCROLL_BAR;
1398 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1399 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1401 text_left = window_box_left (w, TEXT_AREA);
1402 text_right = text_left + window_box_width (w, TEXT_AREA);
1404 if (FRAME_WINDOW_P (f))
1406 if (!w->pseudo_window_p
1407 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1408 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1409 && !WINDOW_RIGHTMOST_P (w)
1410 && (eabs (x - right_x) < grabbable_width))
1411 return ON_VERTICAL_BORDER;
1413 /* Need to say "x > right_x" rather than >=, since on character
1414 terminals, the vertical line's x coordinate is right_x. */
1415 else if (!w->pseudo_window_p
1416 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1417 && !WINDOW_RIGHTMOST_P (w)
1418 /* Why check ux if we are not the rightmost window? Also
1419 shouldn't a pseudo window always be rightmost? */
1420 && x > right_x - ux)
1421 return ON_VERTICAL_BORDER;
1423 if (x < text_left)
1425 if (lmargin_width > 0
1426 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1427 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1428 : (x < left_x + lmargin_width)))
1429 return ON_LEFT_MARGIN;
1430 else
1431 return ON_LEFT_FRINGE;
1434 if (x >= text_right)
1436 if (rmargin_width > 0
1437 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1438 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1439 : (x >= right_x - rmargin_width)))
1440 return ON_RIGHT_MARGIN;
1441 else
1442 return ON_RIGHT_FRINGE;
1445 /* Everything special ruled out - must be on text area */
1446 return ON_TEXT;
1449 /* Take X is the frame-relative pixel x-coordinate, and return the
1450 x-coordinate relative to part PART of window W. */
1452 window_relative_x_coord (struct window *w, enum window_part part, int x)
1454 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1456 switch (part)
1458 case ON_TEXT:
1459 return x - window_box_left (w, TEXT_AREA);
1461 case ON_HEADER_LINE:
1462 case ON_MODE_LINE:
1463 case ON_LEFT_FRINGE:
1464 return x - left_x;
1466 case ON_RIGHT_FRINGE:
1467 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1469 case ON_LEFT_MARGIN:
1470 return (x - left_x
1471 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1472 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1474 case ON_RIGHT_MARGIN:
1475 return (x + 1
1476 - ((w->pseudo_window_p)
1477 ? WINDOW_PIXEL_WIDTH (w)
1478 : WINDOW_BOX_RIGHT_EDGE_X (w))
1479 + window_box_width (w, RIGHT_MARGIN_AREA)
1480 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1481 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1484 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1485 return 0;
1489 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1490 Scoordinates_in_window_p, 2, 2, 0,
1491 doc: /* Return non-nil if COORDINATES are in WINDOW.
1492 WINDOW must be a live window and defaults to the selected one.
1493 COORDINATES is a cons of the form (X . Y), X and Y being distances
1494 measured in characters from the upper-left corner of the frame.
1495 \(0 . 0) denotes the character in the upper left corner of the
1496 frame.
1497 If COORDINATES are in the text portion of WINDOW,
1498 the coordinates relative to the window are returned.
1499 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1500 If they are in the right divider of WINDOW, `right-divider' is returned.
1501 If they are in the mode line of WINDOW, `mode-line' is returned.
1502 If they are in the header line of WINDOW, `header-line' is returned.
1503 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1504 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1505 If they are on the border between WINDOW and its right sibling,
1506 `vertical-line' is returned.
1507 If they are in the windows's left or right marginal areas, `left-margin'\n\
1508 or `right-margin' is returned. */)
1509 (register Lisp_Object coordinates, Lisp_Object window)
1511 struct window *w;
1512 struct frame *f;
1513 int x, y;
1514 Lisp_Object lx, ly;
1516 w = decode_live_window (window);
1517 f = XFRAME (w->frame);
1518 CHECK_CONS (coordinates);
1519 lx = Fcar (coordinates);
1520 ly = Fcdr (coordinates);
1521 CHECK_NUMBER_OR_FLOAT (lx);
1522 CHECK_NUMBER_OR_FLOAT (ly);
1523 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1524 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1526 switch (coordinates_in_window (w, x, y))
1528 case ON_NOTHING:
1529 return Qnil;
1531 case ON_TEXT:
1532 /* Convert X and Y to window relative pixel coordinates, and
1533 return the canonical char units. */
1534 x -= window_box_left (w, TEXT_AREA);
1535 y -= WINDOW_TOP_EDGE_Y (w);
1536 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1537 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1539 case ON_MODE_LINE:
1540 return Qmode_line;
1542 case ON_VERTICAL_BORDER:
1543 return Qvertical_line;
1545 case ON_HEADER_LINE:
1546 return Qheader_line;
1548 case ON_LEFT_FRINGE:
1549 return Qleft_fringe;
1551 case ON_RIGHT_FRINGE:
1552 return Qright_fringe;
1554 case ON_LEFT_MARGIN:
1555 return Qleft_margin;
1557 case ON_RIGHT_MARGIN:
1558 return Qright_margin;
1560 case ON_VERTICAL_SCROLL_BAR:
1561 /* Historically we are supposed to return nil in this case. */
1562 return Qnil;
1564 case ON_HORIZONTAL_SCROLL_BAR:
1565 return Qnil;
1567 case ON_RIGHT_DIVIDER:
1568 return Qright_divider;
1570 case ON_BOTTOM_DIVIDER:
1571 return Qbottom_divider;
1573 default:
1574 emacs_abort ();
1579 /* Callback for foreach_window, used in window_from_coordinates.
1580 Check if window W contains coordinates specified by USER_DATA which
1581 is actually a pointer to a struct check_window_data CW.
1583 Check if window W contains coordinates *CW->x and *CW->y. If it
1584 does, return W in *CW->window, as Lisp_Object, and return in
1585 *CW->part the part of the window under coordinates *X,*Y. Return
1586 zero from this function to stop iterating over windows. */
1588 struct check_window_data
1590 Lisp_Object *window;
1591 int x, y;
1592 enum window_part *part;
1595 static int
1596 check_window_containing (struct window *w, void *user_data)
1598 struct check_window_data *cw = user_data;
1599 enum window_part found;
1600 int continue_p = 1;
1602 found = coordinates_in_window (w, cw->x, cw->y);
1603 if (found != ON_NOTHING)
1605 *cw->part = found;
1606 XSETWINDOW (*cw->window, w);
1607 continue_p = 0;
1610 return continue_p;
1614 /* Find the window containing frame-relative pixel position X/Y and
1615 return it as a Lisp_Object.
1617 If X, Y is on one of the window's special `window_part' elements,
1618 set *PART to the id of that element.
1620 If there is no window under X, Y return nil and leave *PART
1621 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1623 This function was previously implemented with a loop cycling over
1624 windows with Fnext_window, and starting with the frame's selected
1625 window. It turned out that this doesn't work with an
1626 implementation of next_window using Vwindow_list, because
1627 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1628 tree of F when this function is called asynchronously from
1629 note_mouse_highlight. The original loop didn't terminate in this
1630 case. */
1632 Lisp_Object
1633 window_from_coordinates (struct frame *f, int x, int y,
1634 enum window_part *part, bool tool_bar_p)
1636 Lisp_Object window;
1637 struct check_window_data cw;
1638 enum window_part dummy;
1640 if (part == 0)
1641 part = &dummy;
1643 window = Qnil;
1644 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1645 foreach_window (f, check_window_containing, &cw);
1647 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1648 /* If not found above, see if it's in the tool bar window, if a tool
1649 bar exists. */
1650 if (NILP (window)
1651 && tool_bar_p
1652 && WINDOWP (f->tool_bar_window)
1653 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1654 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1655 != ON_NOTHING))
1657 *part = ON_TEXT;
1658 window = f->tool_bar_window;
1660 #endif
1662 return window;
1665 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1666 doc: /* Return window containing coordinates X and Y on FRAME.
1667 FRAME must be a live frame and defaults to the selected one.
1668 The top left corner of the frame is considered to be row 0,
1669 column 0. */)
1670 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1672 struct frame *f = decode_live_frame (frame);
1674 /* Check that arguments are integers or floats. */
1675 CHECK_NUMBER_OR_FLOAT (x);
1676 CHECK_NUMBER_OR_FLOAT (y);
1678 return window_from_coordinates (f,
1679 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1680 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1681 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1682 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1683 0, 0);
1686 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1687 doc: /* Return current value of point in WINDOW.
1688 WINDOW must be a live window and defaults to the selected one.
1690 For a nonselected window, this is the value point would have if that
1691 window were selected.
1693 Note that, when WINDOW is selected, the value returned is the same as
1694 that returned by `point' for WINDOW's buffer. It would be more strictly
1695 correct to return the top-level value of `point', outside of any
1696 `save-excursion' forms. But that is hard to define. */)
1697 (Lisp_Object window)
1699 register struct window *w = decode_live_window (window);
1701 if (w == XWINDOW (selected_window))
1702 return make_number (BUF_PT (XBUFFER (w->contents)));
1703 else
1704 return Fmarker_position (w->pointm);
1707 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1708 doc: /* Return old value of point in WINDOW.
1709 WINDOW must be a live window and defaults to the selected one. */)
1710 (Lisp_Object window)
1712 return Fmarker_position (decode_live_window (window)->old_pointm);
1715 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1716 doc: /* Return position at which display currently starts in WINDOW.
1717 WINDOW must be a live window and defaults to the selected one.
1718 This is updated by redisplay or by calling `set-window-start'. */)
1719 (Lisp_Object window)
1721 return Fmarker_position (decode_live_window (window)->start);
1724 /* This is text temporarily removed from the doc string below.
1726 This function returns nil if the position is not currently known.
1727 That happens when redisplay is preempted and doesn't finish.
1728 If in that case you want to compute where the end of the window would
1729 have been if redisplay had finished, do this:
1730 (save-excursion
1731 (goto-char (window-start window))
1732 (vertical-motion (1- (window-height window)) window)
1733 (point))") */
1735 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1736 doc: /* Return position at which display currently ends in WINDOW.
1737 WINDOW must be a live window and defaults to the selected one.
1738 This is updated by redisplay, when it runs to completion.
1739 Simply changing the buffer text or setting `window-start'
1740 does not update this value.
1741 Return nil if there is no recorded value. (This can happen if the
1742 last redisplay of WINDOW was preempted, and did not finish.)
1743 If UPDATE is non-nil, compute the up-to-date position
1744 if it isn't already recorded. */)
1745 (Lisp_Object window, Lisp_Object update)
1747 Lisp_Object value;
1748 struct window *w = decode_live_window (window);
1749 Lisp_Object buf;
1750 struct buffer *b;
1752 buf = w->contents;
1753 CHECK_BUFFER (buf);
1754 b = XBUFFER (buf);
1756 if (! NILP (update)
1757 && (windows_or_buffers_changed
1758 || !w->window_end_valid
1759 || b->clip_changed
1760 || b->prevent_redisplay_optimizations_p
1761 || window_outdated (w))
1762 && !noninteractive)
1764 struct text_pos startp;
1765 struct it it;
1766 struct buffer *old_buffer = NULL;
1767 void *itdata = NULL;
1769 /* Cannot use Fvertical_motion because that function doesn't
1770 cope with variable-height lines. */
1771 if (b != current_buffer)
1773 old_buffer = current_buffer;
1774 set_buffer_internal (b);
1777 /* In case W->start is out of the range, use something
1778 reasonable. This situation occurred when loading a file with
1779 `-l' containing a call to `rmail' with subsequent other
1780 commands. At the end, W->start happened to be BEG, while
1781 rmail had already narrowed the buffer. */
1782 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1784 itdata = bidi_shelve_cache ();
1785 start_display (&it, w, startp);
1786 move_it_vertically (&it, window_box_height (w));
1787 if (it.current_y < it.last_visible_y)
1788 move_it_past_eol (&it);
1789 value = make_number (IT_CHARPOS (it));
1790 bidi_unshelve_cache (itdata, 0);
1792 if (old_buffer)
1793 set_buffer_internal (old_buffer);
1795 else
1796 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1798 return value;
1801 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1802 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1803 WINDOW must be a live window and defaults to the selected one.
1804 Return POS. */)
1805 (Lisp_Object window, Lisp_Object pos)
1807 register struct window *w = decode_live_window (window);
1809 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1811 if (w == XWINDOW (selected_window))
1813 if (XBUFFER (w->contents) == current_buffer)
1814 Fgoto_char (pos);
1815 else
1817 struct buffer *old_buffer = current_buffer;
1819 /* ... but here we want to catch type error before buffer change. */
1820 CHECK_NUMBER_COERCE_MARKER (pos);
1821 set_buffer_internal (XBUFFER (w->contents));
1822 Fgoto_char (pos);
1823 set_buffer_internal (old_buffer);
1826 else
1828 set_marker_restricted (w->pointm, pos, w->contents);
1829 /* We have to make sure that redisplay updates the window to show
1830 the new value of point. */
1831 wset_redisplay (w);
1834 return pos;
1837 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1838 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1839 WINDOW must be a live window and defaults to the selected one. Return
1840 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1841 overriding motion of point in order to display at this exact start. */)
1842 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1844 register struct window *w = decode_live_window (window);
1846 set_marker_restricted (w->start, pos, w->contents);
1847 /* This is not right, but much easier than doing what is right. */
1848 w->start_at_line_beg = 0;
1849 if (NILP (noforce))
1850 w->force_start = 1;
1851 w->update_mode_line = 1;
1852 /* Bug#15957. */
1853 w->window_end_valid = 0;
1854 wset_redisplay (w);
1856 return pos;
1859 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1860 Spos_visible_in_window_p, 0, 3, 0,
1861 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1862 WINDOW must be a live window and defaults to the selected one.
1864 Return nil if that position is scrolled vertically out of view. If a
1865 character is only partially visible, nil is returned, unless the
1866 optional argument PARTIALLY is non-nil. If POS is only out of view
1867 because of horizontal scrolling, return non-nil. If POS is t, it
1868 specifies the position of the last visible glyph in WINDOW. POS
1869 defaults to point in WINDOW; WINDOW defaults to the selected window.
1871 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1872 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1873 where X and Y are the pixel coordinates relative to the top left corner
1874 of the window. The remaining elements are omitted if the character after
1875 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1876 off-window at the top and bottom of the screen line ("row") containing
1877 POS, ROWH is the visible height of that row, and VPOS is the row number
1878 \(zero-based). */)
1879 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1881 register struct window *w;
1882 register EMACS_INT posint;
1883 register struct buffer *buf;
1884 struct text_pos top;
1885 Lisp_Object in_window = Qnil;
1886 int rtop, rbot, rowh, vpos, fully_p = 1;
1887 int x, y;
1889 w = decode_live_window (window);
1890 buf = XBUFFER (w->contents);
1891 SET_TEXT_POS_FROM_MARKER (top, w->start);
1893 if (EQ (pos, Qt))
1894 posint = -1;
1895 else if (!NILP (pos))
1897 CHECK_NUMBER_COERCE_MARKER (pos);
1898 posint = XINT (pos);
1900 else if (w == XWINDOW (selected_window))
1901 posint = PT;
1902 else
1903 posint = marker_position (w->pointm);
1905 /* If position is above window start or outside buffer boundaries,
1906 or if window start is out of range, position is not visible. */
1907 if ((EQ (pos, Qt)
1908 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1909 && CHARPOS (top) >= BUF_BEGV (buf)
1910 && CHARPOS (top) <= BUF_ZV (buf)
1911 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1912 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1913 in_window = Qt;
1915 if (!NILP (in_window) && !NILP (partially))
1917 Lisp_Object part = Qnil;
1918 if (!fully_p)
1919 part = list4i (rtop, rbot, rowh, vpos);
1920 in_window = Fcons (make_number (x),
1921 Fcons (make_number (y), part));
1924 return in_window;
1927 DEFUN ("window-line-height", Fwindow_line_height,
1928 Swindow_line_height, 0, 2, 0,
1929 doc: /* Return height in pixels of text line LINE in window WINDOW.
1930 WINDOW must be a live window and defaults to the selected one.
1932 Return height of current line if LINE is omitted or nil. Return height of
1933 header or mode line if LINE is `header-line' or `mode-line'.
1934 Otherwise, LINE is a text line number starting from 0. A negative number
1935 counts from the end of the window.
1937 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1938 in pixels of the visible part of the line, VPOS and YPOS are the
1939 vertical position in lines and pixels of the line, relative to the top
1940 of the first text line, and OFFBOT is the number of off-window pixels at
1941 the bottom of the text line. If there are off-window pixels at the top
1942 of the (first) text line, YPOS is negative.
1944 Return nil if window display is not up-to-date. In that case, use
1945 `pos-visible-in-window-p' to obtain the information. */)
1946 (Lisp_Object line, Lisp_Object window)
1948 register struct window *w;
1949 register struct buffer *b;
1950 struct glyph_row *row, *end_row;
1951 int max_y, crop, i;
1952 EMACS_INT n;
1954 w = decode_live_window (window);
1956 if (noninteractive || w->pseudo_window_p)
1957 return Qnil;
1959 CHECK_BUFFER (w->contents);
1960 b = XBUFFER (w->contents);
1962 /* Fail if current matrix is not up-to-date. */
1963 if (!w->window_end_valid
1964 || windows_or_buffers_changed
1965 || b->clip_changed
1966 || b->prevent_redisplay_optimizations_p
1967 || window_outdated (w))
1968 return Qnil;
1970 if (NILP (line))
1972 i = w->cursor.vpos;
1973 if (i < 0 || i >= w->current_matrix->nrows
1974 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1975 return Qnil;
1976 max_y = window_text_bottom_y (w);
1977 goto found_row;
1980 if (EQ (line, Qheader_line))
1982 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1983 return Qnil;
1984 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1985 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1988 if (EQ (line, Qmode_line))
1990 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1991 return (row->enabled_p ?
1992 list4i (row->height,
1993 0, /* not accurate */
1994 (WINDOW_HEADER_LINE_HEIGHT (w)
1995 + window_text_bottom_y (w)),
1997 : Qnil);
2000 CHECK_NUMBER (line);
2001 n = XINT (line);
2003 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2004 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2005 max_y = window_text_bottom_y (w);
2006 i = 0;
2008 while ((n < 0 || i < n)
2009 && row <= end_row && row->enabled_p
2010 && row->y + row->height < max_y)
2011 row++, i++;
2013 if (row > end_row || !row->enabled_p)
2014 return Qnil;
2016 if (++n < 0)
2018 if (-n > i)
2019 return Qnil;
2020 row += n;
2021 i += n;
2024 found_row:
2025 crop = max (0, (row->y + row->height) - max_y);
2026 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
2029 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
2030 0, 1, 0,
2031 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
2032 More precisely, return the value assigned by the last call of
2033 `set-window-dedicated-p' for WINDOW. Return nil if that function was
2034 never called with WINDOW as its argument, or the value set by that
2035 function was internally reset since its last call. WINDOW must be a
2036 live window and defaults to the selected one.
2038 When a window is dedicated to its buffer, `display-buffer' will refrain
2039 from displaying another buffer in it. `get-lru-window' and
2040 `get-largest-window' treat dedicated windows specially.
2041 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2042 `kill-buffer' can delete a dedicated window and the containing frame.
2044 Functions like `set-window-buffer' may change the buffer displayed by a
2045 window, unless that window is "strongly" dedicated to its buffer, that
2046 is the value returned by `window-dedicated-p' is t. */)
2047 (Lisp_Object window)
2049 return decode_live_window (window)->dedicated;
2052 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2053 Sset_window_dedicated_p, 2, 2, 0,
2054 doc: /* Mark WINDOW as dedicated according to FLAG.
2055 WINDOW must be a live window and defaults to the selected one. FLAG
2056 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2057 mark WINDOW as non-dedicated. Return FLAG.
2059 When a window is dedicated to its buffer, `display-buffer' will refrain
2060 from displaying another buffer in it. `get-lru-window' and
2061 `get-largest-window' treat dedicated windows specially.
2062 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2063 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2064 and the containing frame.
2066 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2067 its buffer. Functions like `set-window-buffer' may change the buffer
2068 displayed by a window, unless that window is strongly dedicated to its
2069 buffer. If and when `set-window-buffer' displays another buffer in a
2070 window, it also makes sure that the window is no more dedicated. */)
2071 (Lisp_Object window, Lisp_Object flag)
2073 wset_dedicated (decode_live_window (window), flag);
2074 return flag;
2077 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2078 0, 1, 0,
2079 doc: /* Return buffers previously shown in WINDOW.
2080 WINDOW must be a live window and defaults to the selected one.
2082 The return value is a list of elements (BUFFER WINDOW-START POS),
2083 where BUFFER is a buffer, WINDOW-START is the start position of the
2084 window for that buffer, and POS is a window-specific point value. */)
2085 (Lisp_Object window)
2087 return decode_live_window (window)->prev_buffers;
2090 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2091 Sset_window_prev_buffers, 2, 2, 0,
2092 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2093 WINDOW must be a live window and defaults to the selected one.
2095 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2096 where BUFFER is a buffer, WINDOW-START is the start position of the
2097 window for that buffer, and POS is a window-specific point value. */)
2098 (Lisp_Object window, Lisp_Object prev_buffers)
2100 wset_prev_buffers (decode_live_window (window), prev_buffers);
2101 return prev_buffers;
2104 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2105 0, 1, 0,
2106 doc: /* Return list of buffers recently re-shown in WINDOW.
2107 WINDOW must be a live window and defaults to the selected one. */)
2108 (Lisp_Object window)
2110 return decode_live_window (window)->next_buffers;
2113 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2114 Sset_window_next_buffers, 2, 2, 0,
2115 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2116 WINDOW must be a live window and defaults to the selected one.
2117 NEXT-BUFFERS should be a list of buffers. */)
2118 (Lisp_Object window, Lisp_Object next_buffers)
2120 wset_next_buffers (decode_live_window (window), next_buffers);
2121 return next_buffers;
2124 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2125 0, 1, 0,
2126 doc: /* Return the parameters of WINDOW and their values.
2127 WINDOW must be a valid window and defaults to the selected one. The
2128 return value is a list of elements of the form (PARAMETER . VALUE). */)
2129 (Lisp_Object window)
2131 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2134 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2135 2, 2, 0,
2136 doc: /* Return WINDOW's value for PARAMETER.
2137 WINDOW can be any window and defaults to the selected one. */)
2138 (Lisp_Object window, Lisp_Object parameter)
2140 Lisp_Object result;
2142 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2143 return CDR_SAFE (result);
2146 DEFUN ("set-window-parameter", Fset_window_parameter,
2147 Sset_window_parameter, 3, 3, 0,
2148 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2149 WINDOW can be any window and defaults to the selected one.
2150 Return VALUE. */)
2151 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2153 register struct window *w = decode_any_window (window);
2154 Lisp_Object old_alist_elt;
2156 old_alist_elt = Fassq (parameter, w->window_parameters);
2157 if (NILP (old_alist_elt))
2158 wset_window_parameters
2159 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2160 else
2161 Fsetcdr (old_alist_elt, value);
2162 return value;
2165 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2166 0, 1, 0,
2167 doc: /* Return the display-table that WINDOW is using.
2168 WINDOW must be a live window and defaults to the selected one. */)
2169 (Lisp_Object window)
2171 return decode_live_window (window)->display_table;
2174 /* Get the display table for use on window W. This is either W's
2175 display table or W's buffer's display table. Ignore the specified
2176 tables if they are not valid; if no valid table is specified,
2177 return 0. */
2179 struct Lisp_Char_Table *
2180 window_display_table (struct window *w)
2182 struct Lisp_Char_Table *dp = NULL;
2184 if (DISP_TABLE_P (w->display_table))
2185 dp = XCHAR_TABLE (w->display_table);
2186 else if (BUFFERP (w->contents))
2188 struct buffer *b = XBUFFER (w->contents);
2190 if (DISP_TABLE_P (BVAR (b, display_table)))
2191 dp = XCHAR_TABLE (BVAR (b, display_table));
2192 else if (DISP_TABLE_P (Vstandard_display_table))
2193 dp = XCHAR_TABLE (Vstandard_display_table);
2196 return dp;
2199 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2200 doc: /* Set WINDOW's display-table to TABLE.
2201 WINDOW must be a live window and defaults to the selected one. */)
2202 (register Lisp_Object window, Lisp_Object table)
2204 wset_display_table (decode_live_window (window), table);
2205 return table;
2208 /* Record info on buffer window W is displaying
2209 when it is about to cease to display that buffer. */
2210 static void
2211 unshow_buffer (register struct window *w)
2213 Lisp_Object buf = w->contents;
2214 struct buffer *b = XBUFFER (buf);
2216 eassert (b == XMARKER (w->pointm)->buffer);
2218 #if 0
2219 if (w == XWINDOW (selected_window)
2220 || ! EQ (buf, XWINDOW (selected_window)->contents))
2221 /* Do this except when the selected window's buffer
2222 is being removed from some other window. */
2223 #endif
2224 /* last_window_start records the start position that this buffer
2225 had in the last window to be disconnected from it.
2226 Now that this statement is unconditional,
2227 it is possible for the buffer to be displayed in the
2228 selected window, while last_window_start reflects another
2229 window which was recently showing the same buffer.
2230 Some people might say that might be a good thing. Let's see. */
2231 b->last_window_start = marker_position (w->start);
2233 /* Point in the selected window's buffer
2234 is actually stored in that buffer, and the window's pointm isn't used.
2235 So don't clobber point in that buffer. */
2236 if (! EQ (buf, XWINDOW (selected_window)->contents)
2237 /* Don't clobber point in current buffer either (this could be
2238 useful in connection with bug#12208).
2239 && XBUFFER (buf) != current_buffer */
2240 /* This line helps to fix Horsley's testbug.el bug. */
2241 && !(WINDOWP (BVAR (b, last_selected_window))
2242 && w != XWINDOW (BVAR (b, last_selected_window))
2243 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2244 temp_set_point_both (b,
2245 clip_to_bounds (BUF_BEGV (b),
2246 marker_position (w->pointm),
2247 BUF_ZV (b)),
2248 clip_to_bounds (BUF_BEGV_BYTE (b),
2249 marker_byte_position (w->pointm),
2250 BUF_ZV_BYTE (b)));
2252 if (WINDOWP (BVAR (b, last_selected_window))
2253 && w == XWINDOW (BVAR (b, last_selected_window)))
2254 bset_last_selected_window (b, Qnil);
2257 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2258 means change window structure only. Otherwise store geometry and
2259 other settings as well. */
2260 static void
2261 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2263 register Lisp_Object tem;
2264 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2266 /* If OLD is its frame's root window, then NEW is the new
2267 root window for that frame. */
2268 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2269 fset_root_window (XFRAME (o->frame), new);
2271 if (setflag)
2273 n->pixel_left = o->pixel_left;
2274 n->pixel_top = o->pixel_top;
2275 n->pixel_width = o->pixel_width;
2276 n->pixel_height = o->pixel_height;
2277 n->left_col = o->left_col;
2278 n->top_line = o->top_line;
2279 n->total_cols = o->total_cols;
2280 n->total_lines = o->total_lines;
2281 wset_normal_cols (n, o->normal_cols);
2282 wset_normal_cols (o, make_float (1.0));
2283 wset_normal_lines (n, o->normal_lines);
2284 wset_normal_lines (o, make_float (1.0));
2285 n->desired_matrix = n->current_matrix = 0;
2286 n->vscroll = 0;
2287 memset (&n->cursor, 0, sizeof (n->cursor));
2288 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2289 n->last_cursor_vpos = 0;
2290 #ifdef HAVE_WINDOW_SYSTEM
2291 n->phys_cursor_type = NO_CURSOR;
2292 n->phys_cursor_width = -1;
2293 #endif
2294 n->must_be_updated_p = 0;
2295 n->pseudo_window_p = 0;
2296 n->window_end_vpos = 0;
2297 n->window_end_pos = 0;
2298 n->window_end_valid = 0;
2301 tem = o->next;
2302 wset_next (n, tem);
2303 if (!NILP (tem))
2304 wset_prev (XWINDOW (tem), new);
2306 tem = o->prev;
2307 wset_prev (n, tem);
2308 if (!NILP (tem))
2309 wset_next (XWINDOW (tem), new);
2311 tem = o->parent;
2312 wset_parent (n, tem);
2313 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2314 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2317 /* If window WINDOW and its parent window are iso-combined, merge
2318 WINDOW's children into those of its parent window and mark WINDOW as
2319 deleted. */
2321 static void
2322 recombine_windows (Lisp_Object window)
2324 struct window *w, *p, *c;
2325 Lisp_Object parent, child;
2326 bool horflag;
2328 w = XWINDOW (window);
2329 parent = w->parent;
2330 if (!NILP (parent) && NILP (w->combination_limit))
2332 p = XWINDOW (parent);
2333 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2334 && p->horizontal == w->horizontal)
2335 /* WINDOW and PARENT are both either a vertical or a horizontal
2336 combination. */
2338 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2339 child = w->contents;
2340 c = XWINDOW (child);
2342 /* Splice WINDOW's children into its parent's children and
2343 assign new normal sizes. */
2344 if (NILP (w->prev))
2345 wset_combination (p, horflag, child);
2346 else
2348 wset_prev (c, w->prev);
2349 wset_next (XWINDOW (w->prev), child);
2352 while (c)
2354 wset_parent (c, parent);
2356 if (horflag)
2357 wset_normal_cols
2358 (c, make_float ((double) c->pixel_width
2359 / (double) p->pixel_width));
2360 else
2361 wset_normal_lines
2362 (c, make_float ((double) c->pixel_height
2363 / (double) p->pixel_height));
2365 if (NILP (c->next))
2367 if (!NILP (w->next))
2369 wset_next (c, w->next);
2370 wset_prev (XWINDOW (c->next), child);
2373 c = 0;
2375 else
2377 child = c->next;
2378 c = XWINDOW (child);
2382 /* WINDOW can be deleted now. */
2383 wset_combination (w, 0, Qnil);
2388 /* If WINDOW can be deleted, delete it. */
2389 static void
2390 delete_deletable_window (Lisp_Object window)
2392 if (!NILP (call1 (Qwindow_deletable_p, window)))
2393 call1 (Qdelete_window, window);
2396 /***********************************************************************
2397 Window List
2398 ***********************************************************************/
2400 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2401 pointer. This is a callback function for foreach_window, used in
2402 the window_list function. */
2404 static int
2405 add_window_to_list (struct window *w, void *user_data)
2407 Lisp_Object *list = user_data;
2408 Lisp_Object window;
2409 XSETWINDOW (window, w);
2410 *list = Fcons (window, *list);
2411 return 1;
2415 /* Return a list of all windows, for use by next_window. If
2416 Vwindow_list is a list, return that list. Otherwise, build a new
2417 list, cache it in Vwindow_list, and return that. */
2419 Lisp_Object
2420 window_list (void)
2422 if (!CONSP (Vwindow_list))
2424 Lisp_Object tail, frame;
2426 Vwindow_list = Qnil;
2427 FOR_EACH_FRAME (tail, frame)
2429 Lisp_Object args[2];
2431 /* We are visiting windows in canonical order, and add
2432 new windows at the front of args[1], which means we
2433 have to reverse this list at the end. */
2434 args[1] = Qnil;
2435 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2436 args[0] = Vwindow_list;
2437 args[1] = Fnreverse (args[1]);
2438 Vwindow_list = Fnconc (2, args);
2442 return Vwindow_list;
2446 /* Value is non-zero if WINDOW satisfies the constraints given by
2447 OWINDOW, MINIBUF and ALL_FRAMES.
2449 MINIBUF t means WINDOW may be minibuffer windows.
2450 `lambda' means WINDOW may not be a minibuffer window.
2451 a window means a specific minibuffer window
2453 ALL_FRAMES t means search all frames,
2454 nil means search just current frame,
2455 `visible' means search just visible frames on the
2456 current terminal,
2457 0 means search visible and iconified frames on the
2458 current terminal,
2459 a window means search the frame that window belongs to,
2460 a frame means consider windows on that frame, only. */
2462 static bool
2463 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2464 Lisp_Object minibuf, Lisp_Object all_frames)
2466 struct window *w = XWINDOW (window);
2467 struct frame *f = XFRAME (w->frame);
2468 bool candidate_p = 1;
2470 if (!BUFFERP (w->contents))
2471 candidate_p = 0;
2472 else if (MINI_WINDOW_P (w)
2473 && (EQ (minibuf, Qlambda)
2474 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2476 /* If MINIBUF is `lambda' don't consider any mini-windows.
2477 If it is a window, consider only that one. */
2478 candidate_p = 0;
2480 else if (EQ (all_frames, Qt))
2481 candidate_p = 1;
2482 else if (NILP (all_frames))
2484 eassert (WINDOWP (owindow));
2485 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2487 else if (EQ (all_frames, Qvisible))
2489 candidate_p = FRAME_VISIBLE_P (f)
2490 && (FRAME_TERMINAL (XFRAME (w->frame))
2491 == FRAME_TERMINAL (XFRAME (selected_frame)));
2494 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2496 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2497 #ifdef HAVE_X_WINDOWS
2498 /* Yuck!! If we've just created the frame and the
2499 window-manager requested the user to place it
2500 manually, the window may still not be considered
2501 `visible'. I'd argue it should be at least
2502 something like `iconified', but don't know how to do
2503 that yet. --Stef */
2504 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2505 && !f->output_data.x->has_been_visible)
2506 #endif
2508 && (FRAME_TERMINAL (XFRAME (w->frame))
2509 == FRAME_TERMINAL (XFRAME (selected_frame)));
2511 else if (WINDOWP (all_frames))
2512 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2513 || EQ (XWINDOW (all_frames)->frame, w->frame)
2514 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2515 else if (FRAMEP (all_frames))
2516 candidate_p = EQ (all_frames, w->frame);
2518 return candidate_p;
2522 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2523 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2524 MINIBUF, and ALL_FRAMES. */
2526 static void
2527 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2529 struct window *w = decode_live_window (*window);
2531 XSETWINDOW (*window, w);
2532 /* MINIBUF nil may or may not include minibuffers. Decide if it
2533 does. */
2534 if (NILP (*minibuf))
2535 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2536 else if (!EQ (*minibuf, Qt))
2537 *minibuf = Qlambda;
2539 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2540 => count none of them, or a specific minibuffer window (the
2541 active one) to count. */
2543 /* ALL_FRAMES nil doesn't specify which frames to include. */
2544 if (NILP (*all_frames))
2545 *all_frames
2546 = (!EQ (*minibuf, Qlambda)
2547 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2548 : Qnil);
2549 else if (EQ (*all_frames, Qvisible))
2551 else if (EQ (*all_frames, make_number (0)))
2553 else if (FRAMEP (*all_frames))
2555 else if (!EQ (*all_frames, Qt))
2556 *all_frames = Qnil;
2560 /* Return the next or previous window of WINDOW in cyclic ordering
2561 of windows. NEXT_P non-zero means return the next window. See the
2562 documentation string of next-window for the meaning of MINIBUF and
2563 ALL_FRAMES. */
2565 static Lisp_Object
2566 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2568 decode_next_window_args (&window, &minibuf, &all_frames);
2570 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2571 return the first window on the frame. */
2572 if (FRAMEP (all_frames)
2573 && !EQ (all_frames, XWINDOW (window)->frame))
2574 return Fframe_first_window (all_frames);
2576 if (next_p)
2578 Lisp_Object list;
2580 /* Find WINDOW in the list of all windows. */
2581 list = Fmemq (window, window_list ());
2583 /* Scan forward from WINDOW to the end of the window list. */
2584 if (CONSP (list))
2585 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2586 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2587 break;
2589 /* Scan from the start of the window list up to WINDOW. */
2590 if (!CONSP (list))
2591 for (list = Vwindow_list;
2592 CONSP (list) && !EQ (XCAR (list), window);
2593 list = XCDR (list))
2594 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2595 break;
2597 if (CONSP (list))
2598 window = XCAR (list);
2600 else
2602 Lisp_Object candidate, list;
2604 /* Scan through the list of windows for candidates. If there are
2605 candidate windows in front of WINDOW, the last one of these
2606 is the one we want. If there are candidates following WINDOW
2607 in the list, again the last one of these is the one we want. */
2608 candidate = Qnil;
2609 for (list = window_list (); CONSP (list); list = XCDR (list))
2611 if (EQ (XCAR (list), window))
2613 if (WINDOWP (candidate))
2614 break;
2616 else if (candidate_window_p (XCAR (list), window, minibuf,
2617 all_frames))
2618 candidate = XCAR (list);
2621 if (WINDOWP (candidate))
2622 window = candidate;
2625 return window;
2629 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2630 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2631 WINDOW must be a live window and defaults to the selected one. The
2632 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2633 consider.
2635 MINIBUF nil or omitted means consider the minibuffer window only if the
2636 minibuffer is active. MINIBUF t means consider the minibuffer window
2637 even if the minibuffer is not active. Any other value means do not
2638 consider the minibuffer window even if the minibuffer is active.
2640 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2641 plus the minibuffer window if specified by the MINIBUF argument. If the
2642 minibuffer counts, consider all windows on all frames that share that
2643 minibuffer too. The following non-nil values of ALL-FRAMES have special
2644 meanings:
2646 - t means consider all windows on all existing frames.
2648 - `visible' means consider all windows on all visible frames.
2650 - 0 (the number zero) means consider all windows on all visible and
2651 iconified frames.
2653 - A frame means consider all windows on that frame only.
2655 Anything else means consider all windows on WINDOW's frame and no
2656 others.
2658 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2659 `next-window' to iterate through the entire cycle of acceptable
2660 windows, eventually ending up back at the window you started with.
2661 `previous-window' traverses the same cycle, in the reverse order. */)
2662 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2664 return next_window (window, minibuf, all_frames, 1);
2668 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2669 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2670 WINDOW must be a live window and defaults to the selected one. The
2671 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2672 consider.
2674 MINIBUF nil or omitted means consider the minibuffer window only if the
2675 minibuffer is active. MINIBUF t means consider the minibuffer window
2676 even if the minibuffer is not active. Any other value means do not
2677 consider the minibuffer window even if the minibuffer is active.
2679 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2680 plus the minibuffer window if specified by the MINIBUF argument. If the
2681 minibuffer counts, consider all windows on all frames that share that
2682 minibuffer too. The following non-nil values of ALL-FRAMES have special
2683 meanings:
2685 - t means consider all windows on all existing frames.
2687 - `visible' means consider all windows on all visible frames.
2689 - 0 (the number zero) means consider all windows on all visible and
2690 iconified frames.
2692 - A frame means consider all windows on that frame only.
2694 Anything else means consider all windows on WINDOW's frame and no
2695 others.
2697 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2698 use `previous-window' to iterate through the entire cycle of
2699 acceptable windows, eventually ending up back at the window you
2700 started with. `next-window' traverses the same cycle, in the
2701 reverse order. */)
2702 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2704 return next_window (window, minibuf, all_frames, 0);
2708 /* Return a list of windows in cyclic ordering. Arguments are like
2709 for `next-window'. */
2711 static Lisp_Object
2712 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2714 Lisp_Object tail, list, rest;
2716 decode_next_window_args (&window, &minibuf, &all_frames);
2717 list = Qnil;
2719 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2720 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2721 list = Fcons (XCAR (tail), list);
2723 /* Rotate the list to start with WINDOW. */
2724 list = Fnreverse (list);
2725 rest = Fmemq (window, list);
2726 if (!NILP (rest) && !EQ (rest, list))
2728 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2730 XSETCDR (tail, Qnil);
2731 list = nconc2 (rest, list);
2733 return list;
2737 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2738 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2739 FRAME nil or omitted means use the selected frame.
2740 WINDOW nil or omitted means use the window selected within FRAME.
2741 MINIBUF t means include the minibuffer window, even if it isn't active.
2742 MINIBUF nil or omitted means include the minibuffer window only
2743 if it's active.
2744 MINIBUF neither nil nor t means never include the minibuffer window. */)
2745 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2747 if (NILP (window))
2748 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2749 CHECK_WINDOW (window);
2750 if (NILP (frame))
2751 frame = selected_frame;
2753 if (!EQ (frame, XWINDOW (window)->frame))
2754 error ("Window is on a different frame");
2756 return window_list_1 (window, minibuf, frame);
2760 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2761 doc: /* Return a list of all live windows.
2762 WINDOW specifies the first window to list and defaults to the selected
2763 window.
2765 Optional argument MINIBUF nil or omitted means consider the minibuffer
2766 window only if the minibuffer is active. MINIBUF t means consider the
2767 minibuffer window even if the minibuffer is not active. Any other value
2768 means do not consider the minibuffer window even if the minibuffer is
2769 active.
2771 Optional argument ALL-FRAMES nil or omitted means consider all windows
2772 on WINDOW's frame, plus the minibuffer window if specified by the
2773 MINIBUF argument. If the minibuffer counts, consider all windows on all
2774 frames that share that minibuffer too. The following non-nil values of
2775 ALL-FRAMES have special meanings:
2777 - t means consider all windows on all existing frames.
2779 - `visible' means consider all windows on all visible frames.
2781 - 0 (the number zero) means consider all windows on all visible and
2782 iconified frames.
2784 - A frame means consider all windows on that frame only.
2786 Anything else means consider all windows on WINDOW's frame and no
2787 others.
2789 If WINDOW is not on the list of windows returned, some other window will
2790 be listed first but no error is signaled. */)
2791 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2793 return window_list_1 (window, minibuf, all_frames);
2796 /* Look at all windows, performing an operation specified by TYPE
2797 with argument OBJ.
2798 If FRAMES is Qt, look at all frames;
2799 Qnil, look at just the selected frame;
2800 Qvisible, look at visible frames;
2801 a frame, just look at windows on that frame.
2802 If MINI is non-zero, perform the operation on minibuffer windows too. */
2804 enum window_loop
2806 WINDOW_LOOP_UNUSED,
2807 GET_BUFFER_WINDOW, /* Arg is buffer */
2808 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2809 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2810 CHECK_ALL_WINDOWS /* Arg is ignored */
2813 static Lisp_Object
2814 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2816 Lisp_Object window, windows, best_window, frame_arg;
2817 int frame_best_window_flag = 0;
2818 struct frame *f;
2819 struct gcpro gcpro1;
2821 /* If we're only looping through windows on a particular frame,
2822 frame points to that frame. If we're looping through windows
2823 on all frames, frame is 0. */
2824 if (FRAMEP (frames))
2825 f = XFRAME (frames);
2826 else if (NILP (frames))
2827 f = SELECTED_FRAME ();
2828 else
2829 f = NULL;
2831 if (f)
2832 frame_arg = Qlambda;
2833 else if (EQ (frames, make_number (0)))
2834 frame_arg = frames;
2835 else if (EQ (frames, Qvisible))
2836 frame_arg = frames;
2837 else
2838 frame_arg = Qt;
2840 /* frame_arg is Qlambda to stick to one frame,
2841 Qvisible to consider all visible frames,
2842 or Qt otherwise. */
2844 /* Pick a window to start with. */
2845 if (WINDOWP (obj))
2846 window = obj;
2847 else if (f)
2848 window = FRAME_SELECTED_WINDOW (f);
2849 else
2850 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2852 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2853 GCPRO1 (windows);
2854 best_window = Qnil;
2856 for (; CONSP (windows); windows = XCDR (windows))
2858 struct window *w;
2860 window = XCAR (windows);
2861 w = XWINDOW (window);
2863 /* Note that we do not pay attention here to whether the frame
2864 is visible, since Fwindow_list skips non-visible frames if
2865 that is desired, under the control of frame_arg. */
2866 if (!MINI_WINDOW_P (w)
2867 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2868 consider all windows. */
2869 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2870 || (mini && minibuf_level > 0))
2871 switch (type)
2873 case GET_BUFFER_WINDOW:
2874 if (EQ (w->contents, obj)
2875 /* Don't find any minibuffer window except the one that
2876 is currently in use. */
2877 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2879 if (EQ (window, selected_window))
2880 /* Preferably return the selected window. */
2881 RETURN_UNGCPRO (window);
2882 else if (EQ (XWINDOW (window)->frame, selected_frame)
2883 && !frame_best_window_flag)
2884 /* Prefer windows on the current frame (but don't
2885 choose another one if we have one already). */
2887 best_window = window;
2888 frame_best_window_flag = 1;
2890 else if (NILP (best_window))
2891 best_window = window;
2893 break;
2895 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2896 /* We could simply check whether the buffer shown by window
2897 is live, and show another buffer in case it isn't. */
2898 if (EQ (w->contents, obj))
2900 /* Undedicate WINDOW. */
2901 wset_dedicated (w, Qnil);
2902 /* Make WINDOW show the buffer returned by
2903 other_buffer_safely, don't run any hooks. */
2904 set_window_buffer
2905 (window, other_buffer_safely (w->contents), 0, 0);
2906 /* If WINDOW is the selected window, make its buffer
2907 current. But do so only if the window shows the
2908 current buffer (Bug#6454). */
2909 if (EQ (window, selected_window)
2910 && XBUFFER (w->contents) == current_buffer)
2911 Fset_buffer (w->contents);
2913 break;
2915 case REDISPLAY_BUFFER_WINDOWS:
2916 if (EQ (w->contents, obj))
2918 mark_window_display_accurate (window, 0);
2919 w->update_mode_line = 1;
2920 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2921 update_mode_lines = 27;
2922 best_window = window;
2924 break;
2926 /* Check for a leaf window that has a killed buffer
2927 or broken markers. */
2928 case CHECK_ALL_WINDOWS:
2929 if (BUFFERP (w->contents))
2931 struct buffer *b = XBUFFER (w->contents);
2933 if (!BUFFER_LIVE_P (b))
2934 emacs_abort ();
2935 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2936 emacs_abort ();
2937 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2938 emacs_abort ();
2940 break;
2942 case WINDOW_LOOP_UNUSED:
2943 break;
2947 UNGCPRO;
2948 return best_window;
2951 /* Used for debugging. Abort if any window has a dead buffer. */
2953 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2954 void
2955 check_all_windows (void)
2957 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2960 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2961 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2962 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2963 the current buffer.
2965 The optional argument ALL-FRAMES specifies the frames to consider:
2967 - t means consider all windows on all existing frames.
2969 - `visible' means consider all windows on all visible frames.
2971 - 0 (the number zero) means consider all windows on all visible
2972 and iconified frames.
2974 - A frame means consider all windows on that frame only.
2976 Any other value of ALL-FRAMES means consider all windows on the
2977 selected frame and no others. */)
2978 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2980 Lisp_Object buffer;
2982 if (NILP (buffer_or_name))
2983 buffer = Fcurrent_buffer ();
2984 else
2985 buffer = Fget_buffer (buffer_or_name);
2987 if (BUFFERP (buffer))
2988 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2989 else
2990 return Qnil;
2994 static Lisp_Object
2995 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2997 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
3000 /* Placeholder used by temacs -nw before window.el is loaded. */
3001 DEFUN ("window--sanitize-window-sizes", Fwindow__sanitize_window_sizes,
3002 Swindow__sanitize_window_sizes, 2, 2, 0,
3003 doc: /* */
3004 attributes: const)
3005 (Lisp_Object frame, Lisp_Object horizontal)
3007 return Qnil;
3010 Lisp_Object
3011 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
3013 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
3017 static Lisp_Object
3018 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
3020 return call2 (Qwindow_pixel_to_total, frame, horizontal);
3024 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
3025 Sdelete_other_windows_internal, 0, 2, "",
3026 doc: /* Make WINDOW fill its frame.
3027 Only the frame WINDOW is on is affected. WINDOW must be a valid window
3028 and defaults to the selected one.
3030 Optional argument ROOT, if non-nil, must specify an internal window such
3031 that WINDOW is in its window subtree. If this is the case, replace ROOT
3032 by WINDOW and leave alone any windows not part of ROOT's subtree.
3034 When WINDOW is live try to reduce display jumps by keeping the text
3035 previously visible in WINDOW in the same place on the frame. Doing this
3036 depends on the value of (window-start WINDOW), so if calling this
3037 function in a program gives strange scrolling, make sure the
3038 window-start value is reasonable when this function is called. */)
3039 (Lisp_Object window, Lisp_Object root)
3041 struct window *w, *r, *s;
3042 struct frame *f;
3043 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
3044 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
3045 int top IF_LINT (= 0), new_top, resize_failed;
3047 w = decode_valid_window (window);
3048 XSETWINDOW (window, w);
3049 f = XFRAME (w->frame);
3051 if (NILP (root))
3052 /* ROOT is the frame's root window. */
3054 root = FRAME_ROOT_WINDOW (f);
3055 r = XWINDOW (root);
3057 else
3058 /* ROOT must be an ancestor of WINDOW. */
3060 r = decode_valid_window (root);
3061 pwindow = XWINDOW (window)->parent;
3062 while (!NILP (pwindow))
3063 if (EQ (pwindow, root))
3064 break;
3065 else
3066 pwindow = XWINDOW (pwindow)->parent;
3067 if (!EQ (pwindow, root))
3068 error ("Specified root is not an ancestor of specified window");
3071 if (EQ (window, root))
3072 /* A noop. */
3073 return Qnil;
3074 /* I don't understand the "top > 0" part below. If we deal with a
3075 standalone minibuffer it would have been caught by the preceding
3076 test. */
3077 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3078 error ("Can't expand minibuffer to full frame");
3080 if (BUFFERP (w->contents))
3082 startpos = marker_position (w->start);
3083 startbyte = marker_byte_position (w->start);
3084 top = (WINDOW_TOP_EDGE_LINE (w)
3085 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3086 /* Make sure WINDOW is the frame's selected window. */
3087 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3089 if (EQ (selected_frame, w->frame))
3090 Fselect_window (window, Qnil);
3091 else
3092 fset_selected_window (f, window);
3095 else
3097 /* See if the frame's selected window is a part of the window
3098 subtree rooted at WINDOW, by finding all the selected window's
3099 parents and comparing each one with WINDOW. If it isn't we
3100 need a new selected window for this frame. */
3101 swindow = FRAME_SELECTED_WINDOW (f);
3102 while (1)
3104 pwindow = swindow;
3105 while (!NILP (pwindow) && !EQ (window, pwindow))
3106 pwindow = XWINDOW (pwindow)->parent;
3108 if (EQ (window, pwindow))
3109 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3110 as the new selected window. */
3111 break;
3112 else
3113 /* Else try the previous window of SWINDOW. */
3114 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3117 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3119 if (EQ (selected_frame, w->frame))
3120 Fselect_window (swindow, Qnil);
3121 else
3122 fset_selected_window (f, swindow);
3126 block_input ();
3127 if (!FRAME_INITIAL_P (f))
3129 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3131 /* We are going to free the glyph matrices of WINDOW, and with
3132 that we might lose any information about glyph rows that have
3133 some of their glyphs highlighted in mouse face. (These rows
3134 are marked with a non-zero mouse_face_p flag.) If WINDOW
3135 indeed has some glyphs highlighted in mouse face, signal to
3136 frame's up-to-date hook that mouse highlight was overwritten,
3137 so that it will arrange for redisplaying the highlight. */
3138 if (EQ (hlinfo->mouse_face_window, window))
3139 reset_mouse_highlight (hlinfo);
3141 free_window_matrices (r);
3143 fset_redisplay (f);
3144 Vwindow_list = Qnil;
3145 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3146 resize_failed = 0;
3148 if (!WINDOW_LEAF_P (w))
3150 /* Resize child windows vertically. */
3151 XSETINT (delta, r->pixel_height - w->pixel_height);
3152 w->pixel_top = r->pixel_top;
3153 w->top_line = r->top_line;
3154 resize_root_window (window, delta, Qnil, Qnil, Qt);
3155 if (window_resize_check (w, 0))
3157 window_resize_apply (w, 0);
3158 window_pixel_to_total (w->frame, Qnil);
3160 else
3162 resize_root_window (window, delta, Qnil, Qt, Qt);
3163 if (window_resize_check (w, 0))
3165 window_resize_apply (w, 0);
3166 window_pixel_to_total (w->frame, Qnil);
3168 else
3169 resize_failed = 1;
3172 /* Resize child windows horizontally. */
3173 if (!resize_failed)
3175 w->left_col = r->left_col;
3176 w->pixel_left = r->pixel_left;
3177 XSETINT (delta, r->pixel_width - w->pixel_width);
3178 resize_root_window (window, delta, Qt, Qnil, Qt);
3179 if (window_resize_check (w, 1))
3181 window_resize_apply (w, 1);
3182 window_pixel_to_total (w->frame, Qt);
3184 else
3186 resize_root_window (window, delta, Qt, Qt, Qt);
3187 if (window_resize_check (w, 1))
3189 window_resize_apply (w, 1);
3190 window_pixel_to_total (w->frame, Qt);
3192 else
3193 resize_failed = 1;
3197 if (resize_failed)
3198 /* Play safe, if we still can ... */
3200 window = swindow;
3201 w = XWINDOW (window);
3205 /* Cleanly unlink WINDOW from window-tree. */
3206 if (!NILP (w->prev))
3207 /* Get SIBLING above (on the left of) WINDOW. */
3209 sibling = w->prev;
3210 s = XWINDOW (sibling);
3211 wset_next (s, w->next);
3212 if (!NILP (s->next))
3213 wset_prev (XWINDOW (s->next), sibling);
3215 else
3216 /* Get SIBLING below (on the right of) WINDOW. */
3218 sibling = w->next;
3219 s = XWINDOW (sibling);
3220 wset_prev (s, Qnil);
3221 wset_combination (XWINDOW (w->parent),
3222 XWINDOW (w->parent)->horizontal, sibling);
3225 /* Delete ROOT and all child windows of ROOT. */
3226 if (WINDOWP (r->contents))
3228 delete_all_child_windows (r->contents);
3229 wset_combination (r, 0, Qnil);
3232 replace_window (root, window, 1);
3234 /* This must become SWINDOW anyway ....... */
3235 if (BUFFERP (w->contents) && !resize_failed)
3237 /* Try to minimize scrolling, by setting the window start to the
3238 point will cause the text at the old window start to be at the
3239 same place on the frame. But don't try to do this if the
3240 window start is outside the visible portion (as might happen
3241 when the display is not current, due to typeahead). */
3242 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3243 if (new_top != top
3244 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3245 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3247 struct position pos;
3248 struct buffer *obuf = current_buffer;
3250 Fset_buffer (w->contents);
3251 /* This computation used to temporarily move point, but that
3252 can have unwanted side effects due to text properties. */
3253 pos = *vmotion (startpos, startbyte, -top, w);
3255 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3256 w->window_end_valid = 0;
3257 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3258 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3259 /* We need to do this, so that the window-scroll-functions
3260 get called. */
3261 w->optional_new_start = 1;
3263 set_buffer_internal (obuf);
3267 adjust_frame_glyphs (f);
3268 unblock_input ();
3270 run_window_configuration_change_hook (f);
3272 return Qnil;
3276 void
3277 replace_buffer_in_windows (Lisp_Object buffer)
3279 call1 (Qreplace_buffer_in_windows, buffer);
3282 /* If BUFFER is shown in a window, safely replace it with some other
3283 buffer in all windows of all frames, even those on other keyboards. */
3285 void
3286 replace_buffer_in_windows_safely (Lisp_Object buffer)
3288 if (buffer_window_count (XBUFFER (buffer)))
3290 Lisp_Object tail, frame;
3292 /* A single call to window_loop won't do the job because it only
3293 considers frames on the current keyboard. So loop manually over
3294 frames, and handle each one. */
3295 FOR_EACH_FRAME (tail, frame)
3296 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3300 /* The following three routines are needed for running a window's
3301 configuration change hook. */
3302 static void
3303 run_funs (Lisp_Object funs)
3305 for (; CONSP (funs); funs = XCDR (funs))
3306 if (!EQ (XCAR (funs), Qt))
3307 call0 (XCAR (funs));
3310 static void
3311 select_window_norecord (Lisp_Object window)
3313 if (WINDOW_LIVE_P (window))
3314 Fselect_window (window, Qt);
3317 static void
3318 select_frame_norecord (Lisp_Object frame)
3320 if (FRAME_LIVE_P (XFRAME (frame)))
3321 Fselect_frame (frame, Qt);
3324 void
3325 run_window_configuration_change_hook (struct frame *f)
3327 ptrdiff_t count = SPECPDL_INDEX ();
3328 Lisp_Object frame, global_wcch
3329 = Fdefault_value (Qwindow_configuration_change_hook);
3330 XSETFRAME (frame, f);
3332 if (NILP (Vrun_hooks)
3333 || !(f->can_x_set_window_size)
3334 || !(f->can_run_window_configuration_change_hook))
3335 return;
3337 /* Use the right buffer. Matters when running the local hooks. */
3338 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3340 record_unwind_current_buffer ();
3341 Fset_buffer (Fwindow_buffer (Qnil));
3344 if (SELECTED_FRAME () != f)
3346 record_unwind_protect (select_frame_norecord, selected_frame);
3347 select_frame_norecord (frame);
3350 /* Look for buffer-local values. */
3352 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3353 for (; CONSP (windows); windows = XCDR (windows))
3355 Lisp_Object window = XCAR (windows);
3356 Lisp_Object buffer = Fwindow_buffer (window);
3357 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3358 buffer)))
3360 ptrdiff_t inner_count = SPECPDL_INDEX ();
3361 record_unwind_protect (select_window_norecord, selected_window);
3362 select_window_norecord (window);
3363 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3364 buffer));
3365 unbind_to (inner_count, Qnil);
3370 run_funs (global_wcch);
3371 unbind_to (count, Qnil);
3374 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3375 Srun_window_configuration_change_hook, 0, 1, 0,
3376 doc: /* Run `window-configuration-change-hook' for FRAME.
3377 If FRAME is omitted or nil, it defaults to the selected frame. */)
3378 (Lisp_Object frame)
3380 run_window_configuration_change_hook (decode_live_frame (frame));
3381 return Qnil;
3384 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3385 Srun_window_scroll_functions, 0, 1, 0,
3386 doc: /* Run `window-scroll-functions' for WINDOW.
3387 If WINDOW is omitted or nil, it defaults to the selected window. */)
3388 (Lisp_Object window)
3390 if (! NILP (Vwindow_scroll_functions))
3391 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3392 Fmarker_position (decode_live_window (window)->start));
3393 return Qnil;
3396 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3397 to run hooks. See make_frame for a case where it's not allowed.
3398 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3399 scroll-bar settings of the window are not reset from the buffer's
3400 local settings. */
3402 void
3403 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3404 bool run_hooks_p, bool keep_margins_p)
3406 struct window *w = XWINDOW (window);
3407 struct buffer *b = XBUFFER (buffer);
3408 ptrdiff_t count = SPECPDL_INDEX ();
3409 bool samebuf = EQ (buffer, w->contents);
3411 wset_buffer (w, buffer);
3413 if (EQ (window, selected_window))
3414 bset_last_selected_window (b, window);
3416 /* Let redisplay errors through. */
3417 b->display_error_modiff = 0;
3419 /* Update time stamps of buffer display. */
3420 if (INTEGERP (BVAR (b, display_count)))
3421 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3422 bset_display_time (b, Fcurrent_time ());
3424 w->window_end_pos = 0;
3425 w->window_end_vpos = 0;
3426 w->last_cursor_vpos = 0;
3428 if (!(keep_margins_p && samebuf))
3429 { /* If we're not actually changing the buffer, don't reset hscroll
3430 and vscroll. This case happens for example when called from
3431 change_frame_size_1, where we use a dummy call to
3432 Fset_window_buffer on the frame's selected window (and no
3433 other) just in order to run window-configuration-change-hook
3434 (no longer true since change_frame_size_1 directly calls
3435 run_window_configuration_change_hook). Resetting hscroll and
3436 vscroll here is problematic for things like image-mode and
3437 doc-view-mode since it resets the image's position whenever we
3438 resize the frame. */
3439 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3440 w->suspend_auto_hscroll = 0;
3441 w->vscroll = 0;
3442 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3443 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3444 set_marker_restricted (w->start,
3445 make_number (b->last_window_start),
3446 buffer);
3447 w->start_at_line_beg = 0;
3448 w->force_start = 0;
3450 /* Maybe we could move this into the `if' but it's not obviously safe and
3451 I doubt it's worth the trouble. */
3452 wset_redisplay (w);
3453 w->update_mode_line = true;
3455 /* We must select BUFFER to run the window-scroll-functions and to look up
3456 the buffer-local value of Vwindow_point_insertion_type. */
3457 record_unwind_current_buffer ();
3458 Fset_buffer (buffer);
3460 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3461 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3463 if (!keep_margins_p)
3465 /* Set left and right marginal area width etc. from buffer. */
3466 set_window_fringes (w, BVAR (b, left_fringe_width),
3467 BVAR (b, right_fringe_width),
3468 BVAR (b, fringes_outside_margins));
3469 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3470 BVAR (b, vertical_scroll_bar_type),
3471 BVAR (b, scroll_bar_height),
3472 BVAR (b, horizontal_scroll_bar_type));
3473 set_window_margins (w, BVAR (b, left_margin_cols),
3474 BVAR (b, right_margin_cols));
3475 apply_window_adjustment (w);
3478 if (run_hooks_p)
3480 if (! NILP (Vwindow_scroll_functions))
3481 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3482 Fmarker_position (w->start));
3483 if (!samebuf)
3484 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3487 unbind_to (count, Qnil);
3490 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3491 doc: /* Make WINDOW display BUFFER-OR-NAME.
3492 WINDOW must be a live window and defaults to the selected one.
3493 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3495 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3496 display margins, fringe widths, and scroll bar settings are preserved;
3497 the default is to reset these from the local settings for BUFFER-OR-NAME
3498 or the frame defaults. Return nil.
3500 This function throws an error when WINDOW is strongly dedicated to its
3501 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3502 already display BUFFER-OR-NAME.
3504 This function runs `window-scroll-functions' before running
3505 `window-configuration-change-hook'. */)
3506 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3508 register Lisp_Object tem, buffer;
3509 register struct window *w = decode_live_window (window);
3511 XSETWINDOW (window, w);
3512 buffer = Fget_buffer (buffer_or_name);
3513 CHECK_BUFFER (buffer);
3514 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3515 error ("Attempt to display deleted buffer");
3517 tem = w->contents;
3518 if (NILP (tem))
3519 error ("Window is deleted");
3520 else
3522 if (!EQ (tem, buffer))
3524 if (EQ (w->dedicated, Qt))
3525 /* WINDOW is strongly dedicated to its buffer, signal an
3526 error. */
3527 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3528 else
3529 /* WINDOW is weakly dedicated to its buffer, reset
3530 dedication. */
3531 wset_dedicated (w, Qnil);
3533 call1 (Qrecord_window_buffer, window);
3536 unshow_buffer (w);
3539 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3541 return Qnil;
3544 static Lisp_Object
3545 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3547 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3550 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3551 0, 1, 0,
3552 doc: /* Force all windows to be updated on next redisplay.
3553 If optional arg OBJECT is a window, force redisplay of that window only.
3554 If OBJECT is a buffer or buffer name, force redisplay of all windows
3555 displaying that buffer. */)
3556 (Lisp_Object object)
3558 if (NILP (object))
3560 windows_or_buffers_changed = 29;
3561 update_mode_lines = 28;
3562 return Qt;
3565 if (WINDOWP (object))
3567 struct window *w = XWINDOW (object);
3568 mark_window_display_accurate (object, 0);
3569 w->update_mode_line = 1;
3570 if (BUFFERP (w->contents))
3571 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3572 update_mode_lines = 29;
3573 return Qt;
3576 if (STRINGP (object))
3577 object = Fget_buffer (object);
3578 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3579 && buffer_window_count (XBUFFER (object)))
3581 /* If buffer is live and shown in at least one window, find
3582 all windows showing this buffer and force update of them. */
3583 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3584 return NILP (object) ? Qnil : Qt;
3587 /* If nothing suitable was found, just return.
3588 We could signal an error, but this feature will typically be used
3589 asynchronously in timers or process sentinels, so we don't. */
3590 return Qnil;
3593 /* Obsolete since 24.3. */
3594 void
3595 temp_output_buffer_show (register Lisp_Object buf)
3597 register struct buffer *old = current_buffer;
3598 register Lisp_Object window;
3599 register struct window *w;
3601 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3603 Fset_buffer (buf);
3604 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3605 BEGV = BEG;
3606 ZV = Z;
3607 SET_PT (BEG);
3608 set_buffer_internal (old);
3610 if (!NILP (Vtemp_buffer_show_function))
3611 call1 (Vtemp_buffer_show_function, buf);
3612 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3614 if (!EQ (XWINDOW (window)->frame, selected_frame))
3615 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3616 Vminibuf_scroll_window = window;
3617 w = XWINDOW (window);
3618 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3619 w->suspend_auto_hscroll = 0;
3620 set_marker_restricted_both (w->start, buf, BEG, BEG);
3621 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3622 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3624 /* Run temp-buffer-show-hook, with the chosen window selected
3625 and its buffer current. */
3627 ptrdiff_t count = SPECPDL_INDEX ();
3628 Lisp_Object prev_window, prev_buffer;
3629 prev_window = selected_window;
3630 XSETBUFFER (prev_buffer, old);
3632 /* Select the window that was chosen, for running the hook.
3633 Note: Both Fselect_window and select_window_norecord may
3634 set-buffer to the buffer displayed in the window,
3635 so we need to save the current buffer. --stef */
3636 record_unwind_protect (restore_buffer, prev_buffer);
3637 record_unwind_protect (select_window_norecord, prev_window);
3638 Fselect_window (window, Qt);
3639 Fset_buffer (w->contents);
3640 run_hook (Qtemp_buffer_show_hook);
3641 unbind_to (count, Qnil);
3646 /* Allocate basically initialized window. */
3648 static struct window *
3649 allocate_window (void)
3651 return ALLOCATE_ZEROED_PSEUDOVECTOR
3652 (struct window, current_matrix, PVEC_WINDOW);
3655 /* Make new window, have it replace WINDOW in window-tree, and make
3656 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3657 horizontal child). */
3658 static void
3659 make_parent_window (Lisp_Object window, bool horflag)
3661 Lisp_Object parent;
3662 register struct window *o, *p;
3664 o = XWINDOW (window);
3665 p = allocate_window ();
3666 memcpy ((char *) p + sizeof (struct vectorlike_header),
3667 (char *) o + sizeof (struct vectorlike_header),
3668 word_size * VECSIZE (struct window));
3669 /* P's buffer slot may change from nil to a buffer... */
3670 adjust_window_count (p, 1);
3671 XSETWINDOW (parent, p);
3673 p->sequence_number = ++sequence_number;
3675 replace_window (window, parent, 1);
3677 wset_next (o, Qnil);
3678 wset_prev (o, Qnil);
3679 wset_parent (o, parent);
3680 /* ...but now P becomes an internal window. */
3681 wset_start (p, Qnil);
3682 wset_pointm (p, Qnil);
3683 wset_old_pointm (p, Qnil);
3684 wset_buffer (p, Qnil);
3685 wset_combination (p, horflag, window);
3686 wset_combination_limit (p, Qnil);
3687 wset_window_parameters (p, Qnil);
3690 /* Make new window from scratch. */
3691 Lisp_Object
3692 make_window (void)
3694 Lisp_Object window;
3695 register struct window *w;
3697 w = allocate_window ();
3698 /* Initialize Lisp data. Note that allocate_window initializes all
3699 Lisp data to nil, so do it only for slots which should not be nil. */
3700 wset_normal_lines (w, make_float (1.0));
3701 wset_normal_cols (w, make_float (1.0));
3702 wset_new_total (w, make_number (0));
3703 wset_new_normal (w, make_number (0));
3704 wset_new_pixel (w, make_number (0));
3705 wset_start (w, Fmake_marker ());
3706 wset_pointm (w, Fmake_marker ());
3707 wset_old_pointm (w, Fmake_marker ());
3708 wset_vertical_scroll_bar_type (w, Qt);
3709 wset_horizontal_scroll_bar_type (w, Qt);
3710 /* These Lisp fields are marked specially so they're not set to nil by
3711 allocate_window. */
3712 wset_prev_buffers (w, Qnil);
3713 wset_next_buffers (w, Qnil);
3715 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3716 non-Lisp data, so do it only for slots which should not be zero. */
3717 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3718 w->left_fringe_width = w->right_fringe_width = -1;
3719 w->mode_line_height = w->header_line_height = -1;
3720 #ifdef HAVE_WINDOW_SYSTEM
3721 w->phys_cursor_type = NO_CURSOR;
3722 w->phys_cursor_width = -1;
3723 #endif
3724 w->sequence_number = ++sequence_number;
3725 w->scroll_bar_width = -1;
3726 w->scroll_bar_height = -1;
3727 w->column_number_displayed = -1;
3728 /* Reset window_list. */
3729 Vwindow_list = Qnil;
3730 /* Return window. */
3731 XSETWINDOW (window, w);
3732 return window;
3735 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3736 doc: /* Set new pixel size of WINDOW to SIZE.
3737 WINDOW must be a valid window and defaults to the selected one.
3738 Return SIZE.
3740 Optional argument ADD non-nil means add SIZE to the new pixel size of
3741 WINDOW and return the sum.
3743 The new pixel size of WINDOW, if valid, will be shortly installed as
3744 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3745 `window-pixel-width').
3747 Note: This function does not operate on any child windows of WINDOW. */)
3748 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3750 struct window *w = decode_valid_window (window);
3751 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3752 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3754 CHECK_RANGED_INTEGER (size, size_min, size_max);
3755 if (NILP (add))
3756 wset_new_pixel (w, size);
3757 else
3758 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3760 return w->new_pixel;
3763 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3764 doc: /* Set new total size of WINDOW to SIZE.
3765 WINDOW must be a valid window and defaults to the selected one.
3766 Return SIZE.
3768 Optional argument ADD non-nil means add SIZE to the new total size of
3769 WINDOW and return the sum.
3771 The new total size of WINDOW, if valid, will be shortly installed as
3772 WINDOW's total height (see `window-total-height') or total width (see
3773 `window-total-width').
3775 Note: This function does not operate on any child windows of WINDOW. */)
3776 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3778 struct window *w = decode_valid_window (window);
3780 CHECK_NUMBER (size);
3781 if (NILP (add))
3782 wset_new_total (w, size);
3783 else
3784 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3786 return w->new_total;
3789 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3790 doc: /* Set new normal size of WINDOW to SIZE.
3791 WINDOW must be a valid window and defaults to the selected one.
3792 Return SIZE.
3794 The new normal size of WINDOW, if valid, will be shortly installed as
3795 WINDOW's normal size (see `window-normal-size').
3797 Note: This function does not operate on any child windows of WINDOW. */)
3798 (Lisp_Object window, Lisp_Object size)
3800 wset_new_normal (decode_valid_window (window), size);
3801 return size;
3804 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3805 non-zero) to w->new_pixel would result in correct heights (widths)
3806 for window W and recursively all child windows of W.
3808 Note: This function does not check any of `window-fixed-size-p',
3809 `window-min-height' or `window-min-width'. It does check that window
3810 sizes do not drop below one line (two columns). */
3811 static int
3812 window_resize_check (struct window *w, bool horflag)
3814 struct frame *f = XFRAME (w->frame);
3815 struct window *c;
3817 if (WINDOW_VERTICAL_COMBINATION_P (w))
3818 /* W is a vertical combination. */
3820 c = XWINDOW (w->contents);
3821 if (horflag)
3822 /* All child windows of W must have the same width as W. */
3824 while (c)
3826 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3827 || !window_resize_check (c, horflag))
3828 return 0;
3830 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3833 return 1;
3835 else
3836 /* The sum of the heights of the child windows of W must equal
3837 W's height. */
3839 int remaining_pixels = XINT (w->new_pixel);
3841 while (c)
3843 if (!window_resize_check (c, horflag))
3844 return 0;
3846 remaining_pixels -= XINT (c->new_pixel);
3847 if (remaining_pixels < 0)
3848 return 0;
3849 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3852 return remaining_pixels == 0;
3855 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3856 /* W is a horizontal combination. */
3858 c = XWINDOW (w->contents);
3859 if (horflag)
3860 /* The sum of the widths of the child windows of W must equal W's
3861 width. */
3863 int remaining_pixels = XINT (w->new_pixel);
3865 while (c)
3867 if (!window_resize_check (c, horflag))
3868 return 0;
3870 remaining_pixels -= XINT (c->new_pixel);
3871 if (remaining_pixels < 0)
3872 return 0;
3873 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3876 return remaining_pixels == 0;
3878 else
3879 /* All child windows of W must have the same height as W. */
3881 while (c)
3883 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3884 || !window_resize_check (c, horflag))
3885 return 0;
3887 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3890 return 1;
3893 else
3894 /* A leaf window. Make sure it's not too small. The following
3895 hardcodes the values of `window-safe-min-width' (2) and
3896 `window-safe-min-height' (1) which are defined in window.el. */
3897 return (XINT (w->new_pixel) >= (horflag
3898 ? (2 * FRAME_COLUMN_WIDTH (f))
3899 : FRAME_LINE_HEIGHT (f)));
3903 /* Set w->pixel_height (w->pixel_width if HORFLAG is non-zero) to
3904 w->new_pixel for window W and recursively all child windows of W.
3905 Also calculate and assign the new vertical (horizontal) pixel start
3906 positions of each of these windows.
3908 This function does not perform any error checks. Make sure you have
3909 run window_resize_check on W before applying this function. */
3910 static void
3911 window_resize_apply (struct window *w, bool horflag)
3913 struct window *c;
3914 int edge;
3915 int unit = (horflag
3916 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3917 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3919 /* Note: Assigning new_normal requires that the new total size of the
3920 parent window has been set *before*. */
3921 if (horflag)
3923 w->pixel_width = XFASTINT (w->new_pixel);
3924 w->total_cols = w->pixel_width / unit;
3925 if (NUMBERP (w->new_normal))
3926 wset_normal_cols (w, w->new_normal);
3928 edge = w->pixel_left;
3930 else
3932 w->pixel_height = XFASTINT (w->new_pixel);
3933 w->total_lines = w->pixel_height / unit;
3934 if (NUMBERP (w->new_normal))
3935 wset_normal_lines (w, w->new_normal);
3937 edge = w->pixel_top;
3940 if (WINDOW_VERTICAL_COMBINATION_P (w))
3941 /* W is a vertical combination. */
3943 c = XWINDOW (w->contents);
3944 while (c)
3946 if (horflag)
3948 c->pixel_left = edge;
3949 c->left_col = edge / unit;
3951 else
3953 c->pixel_top = edge;
3954 c->top_line = edge / unit;
3956 window_resize_apply (c, horflag);
3957 if (!horflag)
3958 edge = edge + c->pixel_height;
3960 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3963 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3964 /* W is a horizontal combination. */
3966 c = XWINDOW (w->contents);
3967 while (c)
3969 if (horflag)
3971 c->pixel_left = edge;
3972 c->left_col = edge / unit;
3974 else
3976 c->pixel_top = edge;
3977 c->top_line = edge / unit;
3980 window_resize_apply (c, horflag);
3981 if (horflag)
3982 edge = edge + c->pixel_width;
3984 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3987 else
3988 /* Bug#15957. */
3989 w->window_end_valid = 0;
3993 /* Set w->total_lines (w->total_cols if HORFLAG is non-zero) to
3994 w->new_total for window W and recursively all child windows of W.
3995 Also calculate and assign the new vertical (horizontal) start
3996 positions of each of these windows. */
3997 static void
3998 window_resize_apply_total (struct window *w, bool horflag)
4000 struct window *c;
4001 int edge;
4003 /* Note: Assigning new_normal requires that the new total size of the
4004 parent window has been set *before*. */
4005 if (horflag)
4007 w->total_cols = XFASTINT (w->new_total);
4008 edge = w->left_col;
4010 else
4012 w->total_lines = XFASTINT (w->new_total);
4013 edge = w->top_line;
4016 if (WINDOW_VERTICAL_COMBINATION_P (w))
4017 /* W is a vertical combination. */
4019 c = XWINDOW (w->contents);
4020 while (c)
4022 if (horflag)
4023 c->left_col = edge;
4024 else
4025 c->top_line = edge;
4027 window_resize_apply_total (c, horflag);
4028 if (!horflag)
4029 edge = edge + c->total_lines;
4031 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4034 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4035 /* W is a horizontal combination. */
4037 c = XWINDOW (w->contents);
4038 while (c)
4040 if (horflag)
4041 c->left_col = edge;
4042 else
4043 c->top_line = edge;
4045 window_resize_apply_total (c, horflag);
4046 if (horflag)
4047 edge = edge + c->total_cols;
4049 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4054 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4055 doc: /* Apply requested size values for window-tree of FRAME.
4056 If FRAME is omitted or nil, it defaults to the selected frame.
4058 Optional argument HORIZONTAL omitted or nil means apply requested
4059 height values. HORIZONTAL non-nil means apply requested width values.
4061 The requested size values are those set by `set-window-new-pixel' and
4062 `set-window-new-normal'. This function checks whether the requested
4063 values sum up to a valid window layout, recursively assigns the new
4064 sizes of all child windows and calculates and assigns the new start
4065 positions of these windows.
4067 Return t if the requested values have been applied correctly, nil
4068 otherwise.
4070 Note: This function does not check any of `window-fixed-size-p',
4071 `window-min-height' or `window-min-width'. All these checks have to
4072 be applied on the Elisp level. */)
4073 (Lisp_Object frame, Lisp_Object horizontal)
4075 struct frame *f = decode_live_frame (frame);
4076 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4077 bool horflag = !NILP (horizontal);
4079 if (!window_resize_check (r, horflag)
4080 || (XINT (r->new_pixel)
4081 != (horflag ? r->pixel_width : r->pixel_height)))
4082 return Qnil;
4084 block_input ();
4085 window_resize_apply (r, horflag);
4087 fset_redisplay (f);
4088 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4090 adjust_frame_glyphs (f);
4091 unblock_input ();
4093 return Qt;
4097 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4098 doc: /* Apply requested total size values for window-tree of FRAME.
4099 If FRAME is omitted or nil, it defaults to the selected frame.
4101 This function does not assign pixel or normal size values. You should
4102 have run `window-resize-apply' before running this.
4104 Optional argument HORIZONTAL omitted or nil means apply requested
4105 height values. HORIZONTAL non-nil means apply requested width
4106 values. */)
4107 (Lisp_Object frame, Lisp_Object horizontal)
4109 struct frame *f = decode_live_frame (frame);
4110 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4112 block_input ();
4113 /* Necessary when deleting the top-/or leftmost window. */
4114 r->left_col = 0;
4115 r->top_line = FRAME_TOP_MARGIN (f);
4116 window_resize_apply_total (r, !NILP (horizontal));
4117 /* Handle the mini window. */
4118 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4120 struct window *m = XWINDOW (f->minibuffer_window);
4122 if (NILP (horizontal))
4124 m->top_line = r->top_line + r->total_lines;
4125 m->total_lines = XFASTINT (m->new_total);
4127 else
4128 m->total_cols = XFASTINT (m->new_total);
4131 unblock_input ();
4133 return Qt;
4137 /* Resize frame F's windows when number of lines of F is set to SIZE.
4138 HORFLAG 1 means resize windows when number of columns of F is set to
4139 SIZE. PIXELWISE 1 means to interpret SIZE as pixels. */
4140 void
4141 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4143 Lisp_Object root = f->root_window;
4144 struct window *r = XWINDOW (root);
4145 Lisp_Object mini = f->minibuffer_window;
4146 struct window *m;
4147 /* old_size is the old size of the frame's root window. */
4148 int old_size = horflag ? r->total_cols : r->total_lines;
4149 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4150 int old_pixel_top = r->pixel_top;
4151 /* new_size is the new size of the frame's root window. */
4152 int new_size, new_pixel_size;
4153 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4155 /* Don't let the size drop below one unit. This is more comforting
4156 when we are called from x_set_tool_bar_lines since the latter may
4157 have implicitly given us a zero or negative height. */
4158 if (pixelwise)
4160 /* Note: This does not include the size for internal borders
4161 since these are not part of the frame's text area. */
4162 new_pixel_size = max (horflag
4163 ? size
4164 : (size
4165 - ((FRAME_HAS_MINIBUF_P (f)
4166 && !FRAME_MINIBUF_ONLY_P (f))
4167 ? FRAME_LINE_HEIGHT (f) : 0)),
4168 unit);
4169 new_size = new_pixel_size / unit;
4171 else
4173 new_size = max (horflag
4174 ? size
4175 : (size
4176 - ((FRAME_HAS_MINIBUF_P (f)
4177 && !FRAME_MINIBUF_ONLY_P (f))
4178 ? 1 : 0)),
4180 new_pixel_size = new_size * unit;
4183 r->top_line = FRAME_TOP_MARGIN (f);
4184 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4186 if (new_pixel_size == old_pixel_size
4187 && r->pixel_top == old_pixel_top)
4189 else if (WINDOW_LEAF_P (r))
4190 /* For a leaf root window just set the size. */
4191 if (horflag)
4193 r->total_cols = new_size;
4194 r->pixel_width = new_pixel_size;
4196 else
4198 r->total_lines = new_size;
4199 r->pixel_height = new_pixel_size;
4201 else
4203 Lisp_Object delta;
4205 if (pixelwise)
4206 XSETINT (delta, new_pixel_size - old_pixel_size);
4207 else
4208 XSETINT (delta, new_size - old_size);
4210 /* Try a "normal" resize first. */
4211 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4212 pixelwise ? Qt : Qnil);
4213 if (window_resize_check (r, horflag)
4214 && new_pixel_size == XINT (r->new_pixel))
4216 window_resize_apply (r, horflag);
4217 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4219 else
4221 /* Try with "reasonable" minimum sizes next. */
4222 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4223 pixelwise ? Qt : Qnil);
4224 if (window_resize_check (r, horflag)
4225 && new_pixel_size == XINT (r->new_pixel))
4227 window_resize_apply (r, horflag);
4228 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4229 #if 0 /* Let's try without safe sizes and/or killing other windows. */
4231 else
4233 /* Finally, try with "safe" minimum sizes. */
4234 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4235 pixelwise ? Qt : Qnil);
4236 if (window_resize_check (r, horflag)
4237 && new_pixel_size == XINT (r->new_pixel))
4239 window_resize_apply (r, horflag);
4240 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4242 else
4244 /* We lost. Delete all windows but the frame's
4245 selected one. */
4246 root = f->selected_window;
4247 Fdelete_other_windows_internal (root, Qnil);
4248 if (horflag)
4250 XWINDOW (root)->total_cols = new_size;
4251 XWINDOW (root)->pixel_width = new_pixel_size;
4253 else
4255 XWINDOW (root)->total_lines = new_size;
4256 XWINDOW (root)->pixel_height = new_pixel_size;
4259 #endif /* 0 */
4264 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4266 m = XWINDOW (mini);
4267 if (horflag)
4269 m->total_cols = size;
4270 m->pixel_width = new_pixel_size;
4272 else
4274 /* Are we sure we always want 1 line here? */
4275 m->total_lines = 1;
4276 m->pixel_height = FRAME_LINE_HEIGHT (f);
4277 m->top_line = r->top_line + r->total_lines;
4278 m->pixel_top = r->pixel_top + r->pixel_height;
4282 fset_redisplay (f);
4286 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4287 doc: /* Split window OLD.
4288 Second argument PIXEL-SIZE specifies the number of pixels of the
4289 new window. In any case TOTAL-SIZE must be a positive integer.
4291 Third argument SIDE nil (or `below') specifies that the new window shall
4292 be located below WINDOW. SIDE `above' means the new window shall be
4293 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4294 height of the new window including space reserved for the mode and/or
4295 header line.
4297 SIDE t (or `right') specifies that the new window shall be located on
4298 the right side of WINDOW. SIDE `left' means the new window shall be
4299 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4300 width of the new window including space reserved for fringes and the
4301 scrollbar or a divider column.
4303 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4304 according to the SIDE argument.
4306 The new pixel and normal sizes of all involved windows must have been
4307 set correctly. See the code of `split-window' for how this is done. */)
4308 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4310 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4311 parent window or an internal window we have to install as OLD's new
4312 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4313 provided OLD is a leaf window, or to the frame's selected window.
4314 NEW (*n) is the new window created with some parameters taken from
4315 REFERENCE (*r). */
4316 register Lisp_Object new, frame, reference;
4317 register struct window *o, *p, *n, *r, *c;
4318 struct frame *f;
4319 bool horflag
4320 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4321 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4322 int combination_limit = 0, sum = 0;
4323 int total_size;
4325 CHECK_WINDOW (old);
4326 o = XWINDOW (old);
4327 frame = WINDOW_FRAME (o);
4328 f = XFRAME (frame);
4330 CHECK_NUMBER (pixel_size);
4331 total_size
4332 = XINT (pixel_size) / (horflag
4333 ? FRAME_COLUMN_WIDTH (f)
4334 : FRAME_LINE_HEIGHT (f));
4336 /* Set combination_limit to 1 if we have to make a new parent window.
4337 We do that if either `window-combination-limit' is t, or OLD has no
4338 parent, or OLD is ortho-combined. */
4339 combination_limit =
4340 EQ (Vwindow_combination_limit, Qt)
4341 || NILP (o->parent)
4342 || (horflag
4343 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4344 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4346 /* We need a live reference window to initialize some parameters. */
4347 if (WINDOW_LIVE_P (old))
4348 /* OLD is live, use it as reference window. */
4349 reference = old;
4350 else
4351 /* Use the frame's selected window as reference window. */
4352 reference = FRAME_SELECTED_WINDOW (f);
4353 r = XWINDOW (reference);
4355 /* The following bugs are caught by `split-window'. */
4356 if (MINI_WINDOW_P (o))
4357 error ("Attempt to split minibuffer window");
4358 else if (total_size < (horflag ? 2 : 1))
4359 error ("Size of new window too small (after split)");
4360 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4361 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4362 proportionally. */
4364 p = XWINDOW (o->parent);
4365 /* Temporarily pretend we split the parent window. */
4366 wset_new_pixel
4367 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4368 - XINT (pixel_size)));
4369 if (!window_resize_check (p, horflag))
4370 error ("Window sizes don't fit");
4371 else
4372 /* Undo the temporary pretension. */
4373 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4375 else
4377 if (!window_resize_check (o, horflag))
4378 error ("Resizing old window failed");
4379 else if (XINT (pixel_size) + XINT (o->new_pixel)
4380 != (horflag ? o->pixel_width : o->pixel_height))
4381 error ("Sum of sizes of old and new window don't fit");
4384 /* This is our point of no return. */
4385 if (combination_limit)
4387 /* Save the old value of o->normal_cols/lines. It gets corrupted
4388 by make_parent_window and we need it below for assigning it to
4389 p->new_normal. */
4390 Lisp_Object new_normal
4391 = horflag ? o->normal_cols : o->normal_lines;
4393 make_parent_window (old, horflag);
4394 p = XWINDOW (o->parent);
4395 if (EQ (Vwindow_combination_limit, Qt))
4396 /* Store t in the new parent's combination_limit slot to avoid
4397 that its children get merged into another window. */
4398 wset_combination_limit (p, Qt);
4399 /* These get applied below. */
4400 wset_new_pixel
4401 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4402 wset_new_total
4403 (p, make_number (horflag ? o->total_cols : o->total_lines));
4404 wset_new_normal (p, new_normal);
4406 else
4407 p = XWINDOW (o->parent);
4409 fset_redisplay (f);
4410 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4411 new = make_window ();
4412 n = XWINDOW (new);
4413 wset_frame (n, frame);
4414 wset_parent (n, o->parent);
4416 if (EQ (side, Qabove) || EQ (side, Qleft))
4418 wset_prev (n, o->prev);
4419 if (NILP (n->prev))
4420 wset_combination (p, horflag, new);
4421 else
4422 wset_next (XWINDOW (n->prev), new);
4423 wset_next (n, old);
4424 wset_prev (o, new);
4426 else
4428 wset_next (n, o->next);
4429 if (!NILP (n->next))
4430 wset_prev (XWINDOW (n->next), new);
4431 wset_prev (n, old);
4432 wset_next (o, new);
4435 n->window_end_valid = 0;
4436 n->last_cursor_vpos = 0;
4438 /* Get special geometry settings from reference window. */
4439 n->left_margin_cols = r->left_margin_cols;
4440 n->right_margin_cols = r->right_margin_cols;
4441 n->left_fringe_width = r->left_fringe_width;
4442 n->right_fringe_width = r->right_fringe_width;
4443 n->fringes_outside_margins = r->fringes_outside_margins;
4444 n->scroll_bar_width = r->scroll_bar_width;
4445 n->scroll_bar_height = r->scroll_bar_height;
4446 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4447 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4449 /* Directly assign orthogonal coordinates and sizes. */
4450 if (horflag)
4452 n->pixel_top = o->pixel_top;
4453 n->top_line = o->top_line;
4454 n->pixel_height = o->pixel_height;
4455 n->total_lines = o->total_lines;
4457 else
4459 n->pixel_left = o->pixel_left;
4460 n->left_col = o->left_col;
4461 n->pixel_width = o->pixel_width;
4462 n->total_cols = o->total_cols;
4465 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4466 get them ready here. */
4467 wset_new_pixel (n, pixel_size);
4468 c = XWINDOW (p->contents);
4469 while (c)
4471 if (c != n)
4472 sum = sum + XINT (c->new_total);
4473 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4475 wset_new_total (n, make_number ((horflag
4476 ? p->total_cols
4477 : p->total_lines)
4478 - sum));
4479 wset_new_normal (n, normal_size);
4481 block_input ();
4482 window_resize_apply (p, horflag);
4483 adjust_frame_glyphs (f);
4484 /* Set buffer of NEW to buffer of reference window. Don't run
4485 any hooks. */
4486 set_window_buffer (new, r->contents, 0, 1);
4487 unblock_input ();
4489 /* Maybe we should run the scroll functions in Elisp (which already
4490 runs the configuration change hook). */
4491 if (! NILP (Vwindow_scroll_functions))
4492 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4493 Fmarker_position (n->start));
4494 /* Return NEW. */
4495 return new;
4499 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4500 doc: /* Remove WINDOW from its frame.
4501 WINDOW defaults to the selected window. Return nil.
4502 Signal an error when WINDOW is the only window on its frame. */)
4503 (register Lisp_Object window)
4505 register Lisp_Object parent, sibling, frame, root;
4506 struct window *w, *p, *s, *r;
4507 struct frame *f;
4508 bool horflag, before_sibling = 0;
4510 w = decode_any_window (window);
4511 XSETWINDOW (window, w);
4512 if (NILP (w->contents))
4513 /* It's a no-op to delete an already deleted window. */
4514 return Qnil;
4516 parent = w->parent;
4517 if (NILP (parent))
4518 /* Never delete a minibuffer or frame root window. */
4519 error ("Attempt to delete minibuffer or sole ordinary window");
4520 else if (NILP (w->prev) && NILP (w->next))
4521 /* Rather bow out here, this case should be handled on the Elisp
4522 level. */
4523 error ("Attempt to delete sole window of parent");
4525 p = XWINDOW (parent);
4526 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4528 frame = WINDOW_FRAME (w);
4529 f = XFRAME (frame);
4531 root = FRAME_ROOT_WINDOW (f);
4532 r = XWINDOW (root);
4534 /* Unlink WINDOW from window tree. */
4535 if (NILP (w->prev))
4536 /* Get SIBLING below (on the right of) WINDOW. */
4538 /* before_sibling 1 means WINDOW is the first child of its
4539 parent and thus before the sibling. */
4540 before_sibling = 1;
4541 sibling = w->next;
4542 s = XWINDOW (sibling);
4543 wset_prev (s, Qnil);
4544 wset_combination (p, horflag, sibling);
4546 else
4547 /* Get SIBLING above (on the left of) WINDOW. */
4549 sibling = w->prev;
4550 s = XWINDOW (sibling);
4551 wset_next (s, w->next);
4552 if (!NILP (s->next))
4553 wset_prev (XWINDOW (s->next), sibling);
4556 if (window_resize_check (r, horflag)
4557 && (XINT (r->new_pixel)
4558 == (horflag ? r->pixel_width : r->pixel_height)))
4559 /* We can delete WINDOW now. */
4562 /* Block input. */
4563 block_input ();
4564 window_resize_apply (p, horflag);
4565 /* If this window is referred to by the dpyinfo's mouse
4566 highlight, invalidate that slot to be safe (Bug#9904). */
4567 if (!FRAME_INITIAL_P (f))
4569 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4571 if (EQ (hlinfo->mouse_face_window, window))
4572 hlinfo->mouse_face_window = Qnil;
4575 fset_redisplay (f);
4576 Vwindow_list = Qnil;
4577 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4579 wset_next (w, Qnil); /* Don't delete w->next too. */
4580 free_window_matrices (w);
4582 if (WINDOWP (w->contents))
4584 delete_all_child_windows (w->contents);
4585 wset_combination (w, 0, Qnil);
4587 else
4589 unshow_buffer (w);
4590 unchain_marker (XMARKER (w->pointm));
4591 unchain_marker (XMARKER (w->old_pointm));
4592 unchain_marker (XMARKER (w->start));
4593 wset_buffer (w, Qnil);
4596 if (NILP (s->prev) && NILP (s->next))
4597 /* A matrjoshka where SIBLING has become the only child of
4598 PARENT. */
4600 /* Put SIBLING into PARENT's place. */
4601 replace_window (parent, sibling, 0);
4602 /* Have SIBLING inherit the following three slot values from
4603 PARENT (the combination_limit slot is not inherited). */
4604 wset_normal_cols (s, p->normal_cols);
4605 wset_normal_lines (s, p->normal_lines);
4606 /* Mark PARENT as deleted. */
4607 wset_combination (p, 0, Qnil);
4608 /* Try to merge SIBLING into its new parent. */
4609 recombine_windows (sibling);
4612 adjust_frame_glyphs (f);
4614 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4615 /* We deleted the frame's selected window. */
4617 /* Use the frame's first window as fallback ... */
4618 Lisp_Object new_selected_window = Fframe_first_window (frame);
4619 /* ... but preferably use its most recently used window. */
4620 Lisp_Object mru_window;
4622 /* `get-mru-window' might fail for some reason so play it safe
4623 - promote the first window _without recording it_ first. */
4624 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4625 Fselect_window (new_selected_window, Qt);
4626 else
4627 fset_selected_window (f, new_selected_window);
4629 unblock_input ();
4631 /* Now look whether `get-mru-window' gets us something. */
4632 mru_window = call1 (Qget_mru_window, frame);
4633 if (WINDOW_LIVE_P (mru_window)
4634 && EQ (XWINDOW (mru_window)->frame, frame))
4635 new_selected_window = mru_window;
4637 /* If all ended up well, we now promote the mru window. */
4638 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4639 Fselect_window (new_selected_window, Qnil);
4640 else
4641 fset_selected_window (f, new_selected_window);
4643 else
4644 unblock_input ();
4646 /* Must be run by the caller:
4647 run_window_configuration_change_hook (f); */
4649 else
4650 /* We failed: Relink WINDOW into window tree. */
4652 if (before_sibling)
4654 wset_prev (s, window);
4655 wset_combination (p, horflag, window);
4657 else
4659 wset_next (s, window);
4660 if (!NILP (w->next))
4661 wset_prev (XWINDOW (w->next), window);
4663 error ("Deletion failed");
4666 return Qnil;
4669 /***********************************************************************
4670 Resizing Mini-Windows
4671 ***********************************************************************/
4673 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4674 can. */
4675 void
4676 grow_mini_window (struct window *w, int delta, bool pixelwise)
4678 struct frame *f = XFRAME (w->frame);
4679 struct window *r;
4680 Lisp_Object root, height;
4681 int line_height, pixel_height;
4683 eassert (MINI_WINDOW_P (w));
4684 eassert (delta >= 0);
4686 if (delta > 0)
4688 root = FRAME_ROOT_WINDOW (f);
4689 r = XWINDOW (root);
4690 height = call3 (Qwindow_resize_root_window_vertically,
4691 root, make_number (- delta), pixelwise ? Qt : Qnil);
4692 if (INTEGERP (height) && window_resize_check (r, 0))
4694 block_input ();
4695 window_resize_apply (r, 0);
4697 if (pixelwise)
4699 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4700 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4702 else
4704 line_height = min (-XINT (height),
4705 ((INT_MAX - w->pixel_height)
4706 / FRAME_LINE_HEIGHT (f)));
4707 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4710 /* Grow the mini-window. */
4711 w->pixel_top = r->pixel_top + r->pixel_height;
4712 w->top_line = r->top_line + r->total_lines;
4713 /* Make sure the mini-window has always at least one line. */
4714 w->pixel_height = max (w->pixel_height + pixel_height,
4715 FRAME_LINE_HEIGHT (f));
4716 w->total_lines = max (w->total_lines + line_height, 1);
4718 /* Enforce full redisplay of the frame. */
4719 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4720 fset_redisplay (f);
4721 adjust_frame_glyphs (f);
4722 unblock_input ();
4727 /* Shrink mini-window W to one line. */
4728 void
4729 shrink_mini_window (struct window *w, bool pixelwise)
4731 struct frame *f = XFRAME (w->frame);
4732 struct window *r;
4733 Lisp_Object root, delta;
4734 EMACS_INT height, unit;
4736 eassert (MINI_WINDOW_P (w));
4738 height = pixelwise ? w->pixel_height : w->total_lines;
4739 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4740 if (height > unit)
4742 root = FRAME_ROOT_WINDOW (f);
4743 r = XWINDOW (root);
4744 delta = call3 (Qwindow_resize_root_window_vertically,
4745 root, make_number (height - unit),
4746 pixelwise ? Qt : Qnil);
4747 if (INTEGERP (delta) && window_resize_check (r, 0))
4749 block_input ();
4750 window_resize_apply (r, 0);
4752 /* Shrink the mini-window. */
4753 w->top_line = r->top_line + r->total_lines;
4754 w->total_lines = 1;
4755 w->pixel_top = r->pixel_top + r->pixel_height;
4756 w->pixel_height = FRAME_LINE_HEIGHT (f);
4757 /* Enforce full redisplay of the frame. */
4758 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4759 fset_redisplay (f);
4760 adjust_frame_glyphs (f);
4761 unblock_input ();
4763 /* If the above failed for whatever strange reason we must make a
4764 one window frame here. The same routine will be needed when
4765 shrinking the frame (and probably when making the initial
4766 *scratch* window). For the moment leave things as they are. */
4770 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4771 doc: /* Resize minibuffer window WINDOW. */)
4772 (Lisp_Object window)
4774 struct window *w = XWINDOW (window);
4775 struct window *r;
4776 struct frame *f;
4777 int height;
4779 CHECK_WINDOW (window);
4780 f = XFRAME (w->frame);
4782 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4783 error ("Not a valid minibuffer window");
4784 else if (FRAME_MINIBUF_ONLY_P (f))
4785 error ("Cannot resize a minibuffer-only frame");
4787 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4788 height = r->pixel_height + w->pixel_height;
4789 if (window_resize_check (r, 0)
4790 && XINT (w->new_pixel) > 0
4791 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4793 block_input ();
4794 window_resize_apply (r, 0);
4796 w->pixel_height = XFASTINT (w->new_pixel);
4797 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4798 w->pixel_top = r->pixel_top + r->pixel_height;
4799 w->top_line = r->top_line + r->total_lines;
4801 fset_redisplay (f);
4802 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4803 adjust_frame_glyphs (f);
4804 unblock_input ();
4805 return Qt;
4807 else
4808 error ("Failed to resize minibuffer window");
4811 /* Mark window cursors off for all windows in the window tree rooted
4812 at W by setting their phys_cursor_on_p flag to zero. Called from
4813 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4814 the frame are cleared. */
4816 void
4817 mark_window_cursors_off (struct window *w)
4819 while (w)
4821 if (WINDOWP (w->contents))
4822 mark_window_cursors_off (XWINDOW (w->contents));
4823 else
4824 w->phys_cursor_on_p = 0;
4826 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4831 /* Return number of lines of text (not counting mode lines) in W. */
4834 window_internal_height (struct window *w)
4836 int ht = w->total_lines;
4838 if (!MINI_WINDOW_P (w))
4840 if (!NILP (w->parent)
4841 || WINDOWP (w->contents)
4842 || !NILP (w->next)
4843 || !NILP (w->prev)
4844 || WINDOW_WANTS_MODELINE_P (w))
4845 --ht;
4847 if (WINDOW_WANTS_HEADER_LINE_P (w))
4848 --ht;
4851 return ht;
4855 /************************************************************************
4856 Window Scrolling
4857 ***********************************************************************/
4859 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4860 N screen-fulls, which is defined as the height of the window minus
4861 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4862 instead. Negative values of N mean scroll down. NOERROR non-zero
4863 means don't signal an error if we try to move over BEGV or ZV,
4864 respectively. */
4866 static void
4867 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4869 ptrdiff_t count = SPECPDL_INDEX ();
4871 immediate_quit = 1;
4872 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4874 wset_redisplay (XWINDOW (window));
4876 if (whole && Vfast_but_imprecise_scrolling)
4877 specbind (Qfontification_functions, Qnil);
4879 /* If we must, use the pixel-based version which is much slower than
4880 the line-based one but can handle varying line heights. */
4881 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4882 window_scroll_pixel_based (window, n, whole, noerror);
4883 else
4884 window_scroll_line_based (window, n, whole, noerror);
4886 unbind_to (count, Qnil);
4888 /* Bug#15957. */
4889 XWINDOW (window)->window_end_valid = 0;
4890 immediate_quit = 0;
4894 /* Implementation of window_scroll that works based on pixel line
4895 heights. See the comment of window_scroll for parameter
4896 descriptions. */
4898 static void
4899 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4901 struct it it;
4902 struct window *w = XWINDOW (window);
4903 struct text_pos start;
4904 int this_scroll_margin;
4905 /* True if we fiddled the window vscroll field without really scrolling. */
4906 int vscrolled = 0;
4907 int x, y, rtop, rbot, rowh, vpos;
4908 void *itdata = NULL;
4909 int window_total_lines;
4910 int frame_line_height = default_line_pixel_height (w);
4911 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4912 Fwindow_old_point (window)));
4914 SET_TEXT_POS_FROM_MARKER (start, w->start);
4915 /* Scrolling a minibuffer window via scroll bar when the echo area
4916 shows long text sometimes resets the minibuffer contents behind
4917 our backs. */
4918 if (CHARPOS (start) > ZV)
4919 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4921 /* If PT is not visible in WINDOW, move back one half of
4922 the screen. Allow PT to be partially visible, otherwise
4923 something like (scroll-down 1) with PT in the line before
4924 the partially visible one would recenter. */
4926 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4928 itdata = bidi_shelve_cache ();
4929 /* Move backward half the height of the window. Performance note:
4930 vmotion used here is about 10% faster, but would give wrong
4931 results for variable height lines. */
4932 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4933 it.current_y = it.last_visible_y;
4934 move_it_vertically_backward (&it, window_box_height (w) / 2);
4936 /* The function move_iterator_vertically may move over more than
4937 the specified y-distance. If it->w is small, e.g. a
4938 mini-buffer window, we may end up in front of the window's
4939 display area. Start displaying at the start of the line
4940 containing PT in this case. */
4941 if (it.current_y <= 0)
4943 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4944 move_it_vertically_backward (&it, 0);
4945 it.current_y = 0;
4948 start = it.current.pos;
4949 bidi_unshelve_cache (itdata, 0);
4951 else if (auto_window_vscroll_p)
4953 if (rtop || rbot) /* Partially visible. */
4955 int px;
4956 int dy = frame_line_height;
4957 /* In the below we divide the window box height by the
4958 frame's line height to make the result predictable when
4959 the window box is not an integral multiple of the line
4960 height. This is important to ensure we get back to the
4961 same position when scrolling up, then down. */
4962 if (whole)
4963 dy = max ((window_box_height (w) / dy
4964 - next_screen_context_lines) * dy,
4965 dy);
4966 dy *= n;
4968 if (n < 0)
4970 /* Only vscroll backwards if already vscrolled forwards. */
4971 if (w->vscroll < 0 && rtop > 0)
4973 px = max (0, -w->vscroll - min (rtop, -dy));
4974 Fset_window_vscroll (window, make_number (px), Qt);
4975 return;
4978 if (n > 0)
4980 /* Do vscroll if already vscrolled or only display line. */
4981 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4983 px = max (0, -w->vscroll + min (rbot, dy));
4984 Fset_window_vscroll (window, make_number (px), Qt);
4985 return;
4988 /* Maybe modify window start instead of scrolling. */
4989 if (rbot > 0 || w->vscroll < 0)
4991 ptrdiff_t spos;
4993 Fset_window_vscroll (window, make_number (0), Qt);
4994 /* If there are other text lines above the current row,
4995 move window start to current row. Else to next row. */
4996 if (rbot > 0)
4997 spos = XINT (Fline_beginning_position (Qnil));
4998 else
4999 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5000 set_marker_restricted (w->start, make_number (spos),
5001 w->contents);
5002 w->start_at_line_beg = 1;
5003 w->update_mode_line = 1;
5004 /* Set force_start so that redisplay_window will run the
5005 window-scroll-functions. */
5006 w->force_start = 1;
5007 return;
5011 /* Cancel previous vscroll. */
5012 Fset_window_vscroll (window, make_number (0), Qt);
5015 itdata = bidi_shelve_cache ();
5016 /* If scroll_preserve_screen_position is non-nil, we try to set
5017 point in the same window line as it is now, so get that line. */
5018 if (!NILP (Vscroll_preserve_screen_position))
5020 /* We preserve the goal pixel coordinate across consecutive
5021 calls to scroll-up, scroll-down and other commands that
5022 have the `scroll-command' property. This avoids the
5023 possibility of point becoming "stuck" on a tall line when
5024 scrolling by one line. */
5025 if (window_scroll_pixel_based_preserve_y < 0
5026 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5027 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5029 start_display (&it, w, start);
5030 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5031 window_scroll_pixel_based_preserve_y = it.current_y;
5032 window_scroll_pixel_based_preserve_x = it.current_x;
5035 else
5036 window_scroll_pixel_based_preserve_y
5037 = window_scroll_pixel_based_preserve_x = -1;
5039 /* Move iterator it from start the specified distance forward or
5040 backward. The result is the new window start. */
5041 start_display (&it, w, start);
5042 if (whole)
5044 ptrdiff_t start_pos = IT_CHARPOS (it);
5045 int dy = frame_line_height;
5046 /* In the below we divide the window box height by the frame's
5047 line height to make the result predictable when the window
5048 box is not an integral multiple of the line height. This is
5049 important to ensure we get back to the same position when
5050 scrolling up, then down. */
5051 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
5052 dy) * n;
5054 /* Note that move_it_vertically always moves the iterator to the
5055 start of a line. So, if the last line doesn't have a newline,
5056 we would end up at the start of the line ending at ZV. */
5057 if (dy <= 0)
5059 move_it_vertically_backward (&it, -dy);
5060 /* Ensure we actually do move, e.g. in case we are currently
5061 looking at an image that is taller that the window height. */
5062 while (start_pos == IT_CHARPOS (it)
5063 && start_pos > BEGV)
5064 move_it_by_lines (&it, -1);
5066 else if (dy > 0)
5068 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5069 MOVE_TO_POS | MOVE_TO_Y);
5070 /* Ensure we actually do move, e.g. in case we are currently
5071 looking at an image that is taller that the window height. */
5072 while (start_pos == IT_CHARPOS (it)
5073 && start_pos < ZV)
5074 move_it_by_lines (&it, 1);
5077 else
5078 move_it_by_lines (&it, n);
5080 /* We failed if we find ZV is already on the screen (scrolling up,
5081 means there's nothing past the end), or if we can't start any
5082 earlier (scrolling down, means there's nothing past the top). */
5083 if ((n > 0 && IT_CHARPOS (it) == ZV)
5084 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5086 if (IT_CHARPOS (it) == ZV)
5088 if (it.current_y < it.last_visible_y
5089 && (it.current_y + it.max_ascent + it.max_descent
5090 > it.last_visible_y))
5092 /* The last line was only partially visible, make it fully
5093 visible. */
5094 w->vscroll = (it.last_visible_y
5095 - it.current_y + it.max_ascent + it.max_descent);
5096 adjust_frame_glyphs (it.f);
5098 else
5100 bidi_unshelve_cache (itdata, 0);
5101 if (noerror)
5102 return;
5103 else if (n < 0) /* could happen with empty buffers */
5104 xsignal0 (Qbeginning_of_buffer);
5105 else
5106 xsignal0 (Qend_of_buffer);
5109 else
5111 if (w->vscroll != 0)
5112 /* The first line was only partially visible, make it fully
5113 visible. */
5114 w->vscroll = 0;
5115 else
5117 bidi_unshelve_cache (itdata, 0);
5118 if (noerror)
5119 return;
5120 else
5121 xsignal0 (Qbeginning_of_buffer);
5125 /* If control gets here, then we vscrolled. */
5127 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5129 /* Don't try to change the window start below. */
5130 vscrolled = 1;
5133 if (! vscrolled)
5135 ptrdiff_t pos = IT_CHARPOS (it);
5136 ptrdiff_t bytepos;
5138 /* If in the middle of a multi-glyph character move forward to
5139 the next character. */
5140 if (in_display_vector_p (&it))
5142 ++pos;
5143 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5146 /* Set the window start, and set up the window for redisplay. */
5147 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5148 IT_BYTEPOS (it));
5149 bytepos = marker_byte_position (w->start);
5150 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5151 w->update_mode_line = 1;
5152 /* Set force_start so that redisplay_window will run the
5153 window-scroll-functions. */
5154 w->force_start = 1;
5157 /* The rest of this function uses current_y in a nonstandard way,
5158 not including the height of the header line if any. */
5159 it.current_y = it.vpos = 0;
5161 /* Move PT out of scroll margins.
5162 This code wants current_y to be zero at the window start position
5163 even if there is a header line. */
5164 window_total_lines
5165 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5166 this_scroll_margin = max (0, scroll_margin);
5167 this_scroll_margin
5168 = min (this_scroll_margin, window_total_lines / 4);
5169 this_scroll_margin *= frame_line_height;
5171 if (n > 0)
5173 /* We moved the window start towards ZV, so PT may be now
5174 in the scroll margin at the top. */
5175 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5176 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5177 && (NILP (Vscroll_preserve_screen_position)
5178 || EQ (Vscroll_preserve_screen_position, Qt)))
5179 /* We found PT at a legitimate height. Leave it alone. */
5181 else if (window_scroll_pixel_based_preserve_y >= 0)
5183 /* If we have a header line, take account of it.
5184 This is necessary because we set it.current_y to 0, above. */
5185 move_it_to (&it, -1,
5186 window_scroll_pixel_based_preserve_x,
5187 window_scroll_pixel_based_preserve_y
5188 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5189 -1, MOVE_TO_Y | MOVE_TO_X);
5190 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5192 else
5194 while (it.current_y < this_scroll_margin)
5196 int prev = it.current_y;
5197 move_it_by_lines (&it, 1);
5198 if (prev == it.current_y)
5199 break;
5201 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5204 else if (n < 0)
5206 ptrdiff_t charpos, bytepos;
5207 int partial_p;
5209 /* Save our position, for the
5210 window_scroll_pixel_based_preserve_y case. */
5211 charpos = IT_CHARPOS (it);
5212 bytepos = IT_BYTEPOS (it);
5214 /* We moved the window start towards BEGV, so PT may be now
5215 in the scroll margin at the bottom. */
5216 move_it_to (&it, PT, -1,
5217 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5218 - this_scroll_margin - 1),
5220 MOVE_TO_POS | MOVE_TO_Y);
5222 /* Save our position, in case it's correct. */
5223 charpos = IT_CHARPOS (it);
5224 bytepos = IT_BYTEPOS (it);
5226 /* If PT is in the screen line at the last fully visible line,
5227 move_it_to will stop at X = 0 in that line, because the
5228 required Y coordinate is reached there. See if we can get to
5229 PT without descending lower in Y, and if we can, it means we
5230 reached PT before the scroll margin. */
5231 if (charpos != PT)
5233 struct it it2;
5234 void *it_data;
5236 it2 = it;
5237 it_data = bidi_shelve_cache ();
5238 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5239 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5241 charpos = IT_CHARPOS (it);
5242 bytepos = IT_BYTEPOS (it);
5243 bidi_unshelve_cache (it_data, 1);
5245 else
5247 it = it2;
5248 bidi_unshelve_cache (it_data, 0);
5252 /* See if point is on a partially visible line at the end. */
5253 if (it.what == IT_EOB)
5254 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5255 else
5257 move_it_by_lines (&it, 1);
5258 partial_p = it.current_y > it.last_visible_y;
5261 if (charpos == PT && !partial_p
5262 && (NILP (Vscroll_preserve_screen_position)
5263 || EQ (Vscroll_preserve_screen_position, Qt)))
5264 /* We found PT before we found the display margin, so PT is ok. */
5266 else if (window_scroll_pixel_based_preserve_y >= 0)
5268 SET_TEXT_POS_FROM_MARKER (start, w->start);
5269 start_display (&it, w, start);
5270 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5271 here because we called start_display again and did not
5272 alter it.current_y this time. */
5273 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5274 window_scroll_pixel_based_preserve_y, -1,
5275 MOVE_TO_Y | MOVE_TO_X);
5276 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5278 else
5280 if (partial_p)
5281 /* The last line was only partially visible, so back up two
5282 lines to make sure we're on a fully visible line. */
5284 move_it_by_lines (&it, -2);
5285 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5287 else
5288 /* No, the position we saved is OK, so use it. */
5289 SET_PT_BOTH (charpos, bytepos);
5292 bidi_unshelve_cache (itdata, 0);
5294 if (adjust_old_pointm)
5295 Fset_marker (w->old_pointm,
5296 ((w == XWINDOW (selected_window))
5297 ? make_number (BUF_PT (XBUFFER (w->contents)))
5298 : Fmarker_position (w->pointm)),
5299 w->contents);
5303 /* Implementation of window_scroll that works based on screen lines.
5304 See the comment of window_scroll for parameter descriptions. */
5306 static void
5307 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5309 register struct window *w = XWINDOW (window);
5310 /* Fvertical_motion enters redisplay, which can trigger
5311 fontification, which in turn can modify buffer text (e.g., if the
5312 fontification functions replace escape sequences with faces, as
5313 in `grep-mode-font-lock-keywords'). So we use a marker to record
5314 the old point position, to prevent crashes in SET_PT_BOTH. */
5315 Lisp_Object opoint_marker = Fpoint_marker ();
5316 register ptrdiff_t pos, pos_byte;
5317 register int ht = window_internal_height (w);
5318 register Lisp_Object tem;
5319 bool lose;
5320 Lisp_Object bolp;
5321 ptrdiff_t startpos = marker_position (w->start);
5322 ptrdiff_t startbyte = marker_byte_position (w->start);
5323 Lisp_Object original_pos = Qnil;
5324 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5325 Fwindow_old_point (window)));
5327 /* If scrolling screen-fulls, compute the number of lines to
5328 scroll from the window's height. */
5329 if (whole)
5330 n *= max (1, ht - next_screen_context_lines);
5332 if (!NILP (Vscroll_preserve_screen_position))
5334 if (window_scroll_preserve_vpos <= 0
5335 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5336 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5338 struct position posit
5339 = *compute_motion (startpos, startbyte, 0, 0, 0,
5340 PT, ht, 0, -1, w->hscroll, 0, w);
5342 window_scroll_preserve_vpos = posit.vpos;
5343 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5346 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5347 make_number (window_scroll_preserve_vpos));
5350 XSETFASTINT (tem, PT);
5351 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5353 if (NILP (tem))
5355 Fvertical_motion (make_number (- (ht / 2)), window);
5356 startpos = PT;
5357 startbyte = PT_BYTE;
5360 SET_PT_BOTH (startpos, startbyte);
5361 lose = n < 0 && PT == BEGV;
5362 Fvertical_motion (make_number (n), window);
5363 pos = PT;
5364 pos_byte = PT_BYTE;
5365 bolp = Fbolp ();
5366 SET_PT_BOTH (marker_position (opoint_marker),
5367 marker_byte_position (opoint_marker));
5369 if (lose)
5371 if (noerror)
5372 return;
5373 else
5374 xsignal0 (Qbeginning_of_buffer);
5377 if (pos < ZV)
5379 /* Don't use a scroll margin that is negative or too large. */
5380 int this_scroll_margin =
5381 max (0, min (scroll_margin, w->total_lines / 4));
5383 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5384 w->start_at_line_beg = !NILP (bolp);
5385 w->update_mode_line = 1;
5386 /* Set force_start so that redisplay_window will run
5387 the window-scroll-functions. */
5388 w->force_start = 1;
5390 if (!NILP (Vscroll_preserve_screen_position)
5391 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5393 SET_PT_BOTH (pos, pos_byte);
5394 Fvertical_motion (original_pos, window);
5396 /* If we scrolled forward, put point enough lines down
5397 that it is outside the scroll margin. */
5398 else if (n > 0)
5400 int top_margin;
5402 if (this_scroll_margin > 0)
5404 SET_PT_BOTH (pos, pos_byte);
5405 Fvertical_motion (make_number (this_scroll_margin), window);
5406 top_margin = PT;
5408 else
5409 top_margin = pos;
5411 if (top_margin <= marker_position (opoint_marker))
5412 SET_PT_BOTH (marker_position (opoint_marker),
5413 marker_byte_position (opoint_marker));
5414 else if (!NILP (Vscroll_preserve_screen_position))
5416 SET_PT_BOTH (pos, pos_byte);
5417 Fvertical_motion (original_pos, window);
5419 else
5420 SET_PT (top_margin);
5422 else if (n < 0)
5424 int bottom_margin;
5426 /* If we scrolled backward, put point near the end of the window
5427 but not within the scroll margin. */
5428 SET_PT_BOTH (pos, pos_byte);
5429 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5430 if (XFASTINT (tem) == ht - this_scroll_margin)
5431 bottom_margin = PT;
5432 else
5433 bottom_margin = PT + 1;
5435 if (bottom_margin > marker_position (opoint_marker))
5436 SET_PT_BOTH (marker_position (opoint_marker),
5437 marker_byte_position (opoint_marker));
5438 else
5440 if (!NILP (Vscroll_preserve_screen_position))
5442 SET_PT_BOTH (pos, pos_byte);
5443 Fvertical_motion (original_pos, window);
5445 else
5446 Fvertical_motion (make_number (-1), window);
5450 else
5452 if (noerror)
5453 return;
5454 else
5455 xsignal0 (Qend_of_buffer);
5458 if (adjust_old_pointm)
5459 Fset_marker (w->old_pointm,
5460 ((w == XWINDOW (selected_window))
5461 ? make_number (BUF_PT (XBUFFER (w->contents)))
5462 : Fmarker_position (w->pointm)),
5463 w->contents);
5467 /* Scroll selected_window up or down. If N is nil, scroll a
5468 screen-full which is defined as the height of the window minus
5469 next_screen_context_lines. If N is the symbol `-', scroll.
5470 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5471 up. This is the guts of Fscroll_up and Fscroll_down. */
5473 static void
5474 scroll_command (Lisp_Object n, int direction)
5476 ptrdiff_t count = SPECPDL_INDEX ();
5478 eassert (eabs (direction) == 1);
5480 /* If selected window's buffer isn't current, make it current for
5481 the moment. But don't screw up if window_scroll gets an error. */
5482 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5484 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5485 Fset_buffer (XWINDOW (selected_window)->contents);
5488 if (NILP (n))
5489 window_scroll (selected_window, direction, 1, 0);
5490 else if (EQ (n, Qminus))
5491 window_scroll (selected_window, -direction, 1, 0);
5492 else
5494 n = Fprefix_numeric_value (n);
5495 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5498 unbind_to (count, Qnil);
5501 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5502 doc: /* Scroll text of selected window upward ARG lines.
5503 If ARG is omitted or nil, scroll upward by a near full screen.
5504 A near full screen is `next-screen-context-lines' less than a full screen.
5505 Negative ARG means scroll downward.
5506 If ARG is the atom `-', scroll downward by nearly full screen.
5507 When calling from a program, supply as argument a number, nil, or `-'. */)
5508 (Lisp_Object arg)
5510 scroll_command (arg, 1);
5511 return Qnil;
5514 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5515 doc: /* Scroll text of selected window down ARG lines.
5516 If ARG is omitted or nil, scroll down by a near full screen.
5517 A near full screen is `next-screen-context-lines' less than a full screen.
5518 Negative ARG means scroll upward.
5519 If ARG is the atom `-', scroll upward by nearly full screen.
5520 When calling from a program, supply as argument a number, nil, or `-'. */)
5521 (Lisp_Object arg)
5523 scroll_command (arg, -1);
5524 return Qnil;
5527 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5528 doc: /* Return the other window for \"other window scroll\" commands.
5529 If `other-window-scroll-buffer' is non-nil, a window
5530 showing that buffer is used.
5531 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5532 specifies the window. This takes precedence over
5533 `other-window-scroll-buffer'. */)
5534 (void)
5536 Lisp_Object window;
5538 if (MINI_WINDOW_P (XWINDOW (selected_window))
5539 && !NILP (Vminibuf_scroll_window))
5540 window = Vminibuf_scroll_window;
5541 /* If buffer is specified and live, scroll that buffer. */
5542 else if (!NILP (Vother_window_scroll_buffer)
5543 && BUFFERP (Vother_window_scroll_buffer)
5544 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5546 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5547 if (NILP (window))
5548 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5550 else
5552 /* Nothing specified; look for a neighboring window on the same
5553 frame. */
5554 window = Fnext_window (selected_window, Qnil, Qnil);
5556 if (EQ (window, selected_window))
5557 /* That didn't get us anywhere; look for a window on another
5558 visible frame. */
5560 window = Fnext_window (window, Qnil, Qt);
5561 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5562 && ! EQ (window, selected_window));
5565 CHECK_LIVE_WINDOW (window);
5567 if (EQ (window, selected_window))
5568 error ("There is no other window");
5570 return window;
5573 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5574 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5575 A near full screen is `next-screen-context-lines' less than a full screen.
5576 The next window is the one below the current one; or the one at the top
5577 if the current one is at the bottom. Negative ARG means scroll downward.
5578 If ARG is the atom `-', scroll downward by nearly full screen.
5579 When calling from a program, supply as argument a number, nil, or `-'.
5581 If `other-window-scroll-buffer' is non-nil, scroll the window
5582 showing that buffer, popping the buffer up if necessary.
5583 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5584 specifies the window to scroll. This takes precedence over
5585 `other-window-scroll-buffer'. */)
5586 (Lisp_Object arg)
5588 Lisp_Object window;
5589 struct window *w;
5590 ptrdiff_t count = SPECPDL_INDEX ();
5592 window = Fother_window_for_scrolling ();
5593 w = XWINDOW (window);
5595 /* Don't screw up if window_scroll gets an error. */
5596 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5598 Fset_buffer (w->contents);
5599 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5600 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5602 if (NILP (arg))
5603 window_scroll (window, 1, 1, 1);
5604 else if (EQ (arg, Qminus))
5605 window_scroll (window, -1, 1, 1);
5606 else
5608 if (CONSP (arg))
5609 arg = XCAR (arg);
5610 CHECK_NUMBER (arg);
5611 window_scroll (window, XINT (arg), 0, 1);
5614 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5615 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5616 unbind_to (count, Qnil);
5618 return Qnil;
5621 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5622 doc: /* Scroll selected window display ARG columns left.
5623 Default for ARG is window width minus 2.
5624 Value is the total amount of leftward horizontal scrolling in
5625 effect after the change.
5626 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5627 lower bound for automatic scrolling, i.e. automatic scrolling
5628 will not scroll a window to a column less than the value returned
5629 by this function. This happens in an interactive call. */)
5630 (register Lisp_Object arg, Lisp_Object set_minimum)
5632 struct window *w = XWINDOW (selected_window);
5633 EMACS_INT requested_arg = (NILP (arg)
5634 ? window_body_width (w, 0) - 2
5635 : XINT (Fprefix_numeric_value (arg)));
5636 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5638 if (!NILP (set_minimum))
5639 w->min_hscroll = w->hscroll;
5641 w->suspend_auto_hscroll = 1;
5643 return result;
5646 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5647 doc: /* Scroll selected window display ARG columns right.
5648 Default for ARG is window width minus 2.
5649 Value is the total amount of leftward horizontal scrolling in
5650 effect after the change.
5651 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5652 lower bound for automatic scrolling, i.e. automatic scrolling
5653 will not scroll a window to a column less than the value returned
5654 by this function. This happens in an interactive call. */)
5655 (register Lisp_Object arg, Lisp_Object set_minimum)
5657 struct window *w = XWINDOW (selected_window);
5658 EMACS_INT requested_arg = (NILP (arg)
5659 ? window_body_width (w, 0) - 2
5660 : XINT (Fprefix_numeric_value (arg)));
5661 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5663 if (!NILP (set_minimum))
5664 w->min_hscroll = w->hscroll;
5666 w->suspend_auto_hscroll = 1;
5668 return result;
5671 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5672 doc: /* Return the window which was selected when entering the minibuffer.
5673 Returns nil, if selected window is not a minibuffer window. */)
5674 (void)
5676 if (minibuf_level > 0
5677 && MINI_WINDOW_P (XWINDOW (selected_window))
5678 && WINDOW_LIVE_P (minibuf_selected_window))
5679 return minibuf_selected_window;
5681 return Qnil;
5684 /* Value is the number of lines actually displayed in window W,
5685 as opposed to its height. */
5687 static int
5688 displayed_window_lines (struct window *w)
5690 struct it it;
5691 struct text_pos start;
5692 int height = window_box_height (w);
5693 struct buffer *old_buffer;
5694 int bottom_y;
5695 void *itdata = NULL;
5697 if (XBUFFER (w->contents) != current_buffer)
5699 old_buffer = current_buffer;
5700 set_buffer_internal (XBUFFER (w->contents));
5702 else
5703 old_buffer = NULL;
5705 /* In case W->start is out of the accessible range, do something
5706 reasonable. This happens in Info mode when Info-scroll-down
5707 calls (recenter -1) while W->start is 1. */
5708 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5710 itdata = bidi_shelve_cache ();
5711 start_display (&it, w, start);
5712 move_it_vertically (&it, height);
5713 bottom_y = line_bottom_y (&it);
5714 bidi_unshelve_cache (itdata, 0);
5716 /* rms: On a non-window display,
5717 the value of it.vpos at the bottom of the screen
5718 seems to be 1 larger than window_box_height (w).
5719 This kludge fixes a bug whereby (move-to-window-line -1)
5720 when ZV is on the last screen line
5721 moves to the previous screen line instead of the last one. */
5722 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5723 height++;
5725 /* Add in empty lines at the bottom of the window. */
5726 if (bottom_y < height)
5728 int uy = FRAME_LINE_HEIGHT (it.f);
5729 it.vpos += (height - bottom_y + uy - 1) / uy;
5732 if (old_buffer)
5733 set_buffer_internal (old_buffer);
5735 return it.vpos;
5739 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5740 doc: /* Center point in selected window and maybe redisplay frame.
5741 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5742 relative to the selected window. If ARG is negative, it counts up from the
5743 bottom of the window. (ARG should be less than the height of the window.)
5745 If ARG is omitted or nil, then recenter with point on the middle line of
5746 the selected window; if the variable `recenter-redisplay' is non-nil,
5747 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5748 is set to `grow-only', this resets the tool-bar's height to the minimum
5749 height needed); if `recenter-redisplay' has the special value `tty',
5750 then only tty frames are redrawn.
5752 Just C-u as prefix means put point in the center of the window
5753 and redisplay normally--don't erase and redraw the frame. */)
5754 (register Lisp_Object arg)
5756 struct window *w = XWINDOW (selected_window);
5757 struct buffer *buf = XBUFFER (w->contents);
5758 bool center_p = 0;
5759 ptrdiff_t charpos, bytepos;
5760 EMACS_INT iarg IF_LINT (= 0);
5761 int this_scroll_margin;
5763 if (buf != current_buffer)
5764 error ("`recenter'ing a window that does not display current-buffer.");
5766 /* If redisplay is suppressed due to an error, try again. */
5767 buf->display_error_modiff = 0;
5769 if (NILP (arg))
5771 if (!NILP (Vrecenter_redisplay)
5772 && (!EQ (Vrecenter_redisplay, Qtty)
5773 || !NILP (Ftty_type (selected_frame))))
5775 ptrdiff_t i;
5777 /* Invalidate pixel data calculated for all compositions. */
5778 for (i = 0; i < n_compositions; i++)
5779 composition_table[i]->font = NULL;
5780 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5781 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5782 #endif
5783 Fredraw_frame (WINDOW_FRAME (w));
5784 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5787 center_p = 1;
5789 else if (CONSP (arg)) /* Just C-u. */
5790 center_p = 1;
5791 else
5793 arg = Fprefix_numeric_value (arg);
5794 CHECK_NUMBER (arg);
5795 iarg = XINT (arg);
5798 /* Do this after making BUF current
5799 in case scroll_margin is buffer-local. */
5800 this_scroll_margin
5801 = max (0, min (scroll_margin, w->total_lines / 4));
5803 /* Handle centering on a graphical frame specially. Such frames can
5804 have variable-height lines and centering point on the basis of
5805 line counts would lead to strange effects. */
5806 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5808 if (center_p)
5810 struct it it;
5811 struct text_pos pt;
5812 void *itdata = bidi_shelve_cache ();
5814 SET_TEXT_POS (pt, PT, PT_BYTE);
5815 start_display (&it, w, pt);
5816 move_it_vertically_backward (&it, window_box_height (w) / 2);
5817 charpos = IT_CHARPOS (it);
5818 bytepos = IT_BYTEPOS (it);
5819 bidi_unshelve_cache (itdata, 0);
5821 else if (iarg < 0)
5823 struct it it;
5824 struct text_pos pt;
5825 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5826 int extra_line_spacing;
5827 int h = window_box_height (w);
5828 void *itdata = bidi_shelve_cache ();
5830 iarg = - max (-iarg, this_scroll_margin);
5832 SET_TEXT_POS (pt, PT, PT_BYTE);
5833 start_display (&it, w, pt);
5835 /* Be sure we have the exact height of the full line containing PT. */
5836 move_it_by_lines (&it, 0);
5838 /* The amount of pixels we have to move back is the window
5839 height minus what's displayed in the line containing PT,
5840 and the lines below. */
5841 it.current_y = 0;
5842 it.vpos = 0;
5843 move_it_by_lines (&it, nlines);
5845 if (it.vpos == nlines)
5846 h -= it.current_y;
5847 else
5849 /* Last line has no newline. */
5850 h -= line_bottom_y (&it);
5851 it.vpos++;
5854 /* Don't reserve space for extra line spacing of last line. */
5855 extra_line_spacing = it.max_extra_line_spacing;
5857 /* If we can't move down NLINES lines because we hit
5858 the end of the buffer, count in some empty lines. */
5859 if (it.vpos < nlines)
5861 nlines -= it.vpos;
5862 extra_line_spacing = it.extra_line_spacing;
5863 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5865 if (h <= 0)
5867 bidi_unshelve_cache (itdata, 0);
5868 return Qnil;
5871 /* Now find the new top line (starting position) of the window. */
5872 start_display (&it, w, pt);
5873 it.current_y = 0;
5874 move_it_vertically_backward (&it, h);
5876 /* If extra line spacing is present, we may move too far
5877 back. This causes the last line to be only partially
5878 visible (which triggers redisplay to recenter that line
5879 in the middle), so move forward.
5880 But ignore extra line spacing on last line, as it is not
5881 considered to be part of the visible height of the line.
5883 h += extra_line_spacing;
5884 while (-it.current_y > h)
5885 move_it_by_lines (&it, 1);
5887 charpos = IT_CHARPOS (it);
5888 bytepos = IT_BYTEPOS (it);
5890 bidi_unshelve_cache (itdata, 0);
5892 else
5894 struct position pos;
5896 iarg = max (iarg, this_scroll_margin);
5898 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5899 charpos = pos.bufpos;
5900 bytepos = pos.bytepos;
5903 else
5905 struct position pos;
5906 int ht = window_internal_height (w);
5908 if (center_p)
5909 iarg = ht / 2;
5910 else if (iarg < 0)
5911 iarg += ht;
5913 /* Don't let it get into the margin at either top or bottom. */
5914 iarg = clip_to_bounds (this_scroll_margin, iarg,
5915 ht - this_scroll_margin - 1);
5917 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5918 charpos = pos.bufpos;
5919 bytepos = pos.bytepos;
5922 /* Set the new window start. */
5923 set_marker_both (w->start, w->contents, charpos, bytepos);
5924 w->window_end_valid = 0;
5926 w->optional_new_start = 1;
5928 w->start_at_line_beg = (bytepos == BEGV_BYTE
5929 || FETCH_BYTE (bytepos - 1) == '\n');
5931 wset_redisplay (w);
5933 return Qnil;
5936 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5937 0, 2, 0,
5938 doc: /* Return the width in columns of the text display area of WINDOW.
5939 WINDOW must be a live window and defaults to the selected one.
5941 The returned width does not include dividers, scrollbars, margins,
5942 fringes, nor any partial-width columns at the right of the text
5943 area.
5945 Optional argument PIXELWISE non-nil, means to return the width in
5946 pixels. */)
5947 (Lisp_Object window, Lisp_Object pixelwise)
5949 struct window *w = decode_live_window (window);
5951 if (NILP (pixelwise))
5952 return make_number (window_box_width (w, TEXT_AREA)
5953 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5954 else
5955 return make_number (window_box_width (w, TEXT_AREA));
5958 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5959 0, 2, 0,
5960 doc: /* Return the height in lines of the text display area of WINDOW.
5961 WINDOW must be a live window and defaults to the selected one.
5963 The returned height does not include dividers, the mode line, any header
5964 line, nor any partial-height lines at the bottom of the text area.
5966 Optional argument PIXELWISE non-nil, means to return the height in
5967 pixels. */)
5968 (Lisp_Object window, Lisp_Object pixelwise)
5970 struct window *w = decode_live_window (window);
5972 if (NILP (pixelwise))
5973 return make_number (window_box_height (w)
5974 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5975 else
5976 return make_number (window_box_height (w));
5979 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5980 1, 1, "P",
5981 doc: /* Position point relative to window.
5982 ARG nil means position point at center of window.
5983 Else, ARG specifies vertical position within the window;
5984 zero means top of window, negative means relative to bottom of window. */)
5985 (Lisp_Object arg)
5987 struct window *w = XWINDOW (selected_window);
5988 int lines, start;
5989 Lisp_Object window;
5990 #if 0
5991 int this_scroll_margin;
5992 #endif
5994 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5995 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5996 when passed below to set_marker_both. */
5997 error ("move-to-window-line called from unrelated buffer");
5999 window = selected_window;
6000 start = marker_position (w->start);
6001 if (start < BEGV || start > ZV)
6003 int height = window_internal_height (w);
6004 Fvertical_motion (make_number (- (height / 2)), window);
6005 set_marker_both (w->start, w->contents, PT, PT_BYTE);
6006 w->start_at_line_beg = !NILP (Fbolp ());
6007 w->force_start = 1;
6009 else
6010 Fgoto_char (w->start);
6012 lines = displayed_window_lines (w);
6014 #if 0
6015 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
6016 #endif
6018 if (NILP (arg))
6019 XSETFASTINT (arg, lines / 2);
6020 else
6022 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
6024 if (iarg < 0)
6025 iarg = iarg + lines;
6027 #if 0 /* This code would prevent move-to-window-line from moving point
6028 to a place inside the scroll margins (which would cause the
6029 next redisplay to scroll). I wrote this code, but then concluded
6030 it is probably better not to install it. However, it is here
6031 inside #if 0 so as not to lose it. -- rms. */
6033 /* Don't let it get into the margin at either top or bottom. */
6034 iarg = max (iarg, this_scroll_margin);
6035 iarg = min (iarg, lines - this_scroll_margin - 1);
6036 #endif
6038 arg = make_number (iarg);
6041 /* Skip past a partially visible first line. */
6042 if (w->vscroll)
6043 XSETINT (arg, XINT (arg) + 1);
6045 return Fvertical_motion (arg, window);
6050 /***********************************************************************
6051 Window Configuration
6052 ***********************************************************************/
6054 struct save_window_data
6056 struct vectorlike_header header;
6057 Lisp_Object selected_frame;
6058 Lisp_Object current_window;
6059 Lisp_Object current_buffer;
6060 Lisp_Object minibuf_scroll_window;
6061 Lisp_Object minibuf_selected_window;
6062 Lisp_Object root_window;
6063 Lisp_Object focus_frame;
6064 /* A vector, each of whose elements is a struct saved_window
6065 for one window. */
6066 Lisp_Object saved_windows;
6068 /* All fields above are traced by the GC.
6069 From `frame-cols' down, the fields are ignored by the GC. */
6070 /* We should be able to do without the following two. */
6071 int frame_cols, frame_lines;
6072 /* These two should get eventually replaced by their pixel
6073 counterparts. */
6074 int frame_menu_bar_lines, frame_tool_bar_lines;
6075 int frame_text_width, frame_text_height;
6076 /* These are currently unused. We need them as soon as we convert
6077 to pixels. */
6078 int frame_menu_bar_height, frame_tool_bar_height;
6081 /* This is saved as a Lisp_Vector. */
6082 struct saved_window
6084 struct vectorlike_header header;
6086 Lisp_Object window, buffer, start, pointm, old_pointm;
6087 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6088 Lisp_Object left_col, top_line, total_cols, total_lines;
6089 Lisp_Object normal_cols, normal_lines;
6090 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6091 Lisp_Object parent, prev;
6092 Lisp_Object start_at_line_beg;
6093 Lisp_Object display_table;
6094 Lisp_Object left_margin_cols, right_margin_cols;
6095 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6096 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6097 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6098 Lisp_Object combination_limit, window_parameters;
6101 #define SAVED_WINDOW_N(swv,n) \
6102 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6104 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6105 doc: /* Return t if OBJECT is a window-configuration object. */)
6106 (Lisp_Object object)
6108 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6111 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6112 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6113 (Lisp_Object config)
6115 register struct save_window_data *data;
6116 struct Lisp_Vector *saved_windows;
6118 CHECK_WINDOW_CONFIGURATION (config);
6120 data = (struct save_window_data *) XVECTOR (config);
6121 saved_windows = XVECTOR (data->saved_windows);
6122 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6125 DEFUN ("set-window-configuration", Fset_window_configuration,
6126 Sset_window_configuration, 1, 1, 0,
6127 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6128 CONFIGURATION must be a value previously returned
6129 by `current-window-configuration' (which see).
6130 If CONFIGURATION was made from a frame that is now deleted,
6131 only frame-independent values can be restored. In this case,
6132 the return value is nil. Otherwise the value is t. */)
6133 (Lisp_Object configuration)
6135 register struct save_window_data *data;
6136 struct Lisp_Vector *saved_windows;
6137 Lisp_Object new_current_buffer;
6138 Lisp_Object frame;
6139 struct frame *f;
6140 ptrdiff_t old_point = -1;
6141 USE_SAFE_ALLOCA;
6143 CHECK_WINDOW_CONFIGURATION (configuration);
6145 data = (struct save_window_data *) XVECTOR (configuration);
6146 saved_windows = XVECTOR (data->saved_windows);
6148 new_current_buffer = data->current_buffer;
6149 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6150 new_current_buffer = Qnil;
6151 else
6153 if (XBUFFER (new_current_buffer) == current_buffer)
6154 /* The code further down "preserves point" by saving here PT in
6155 old_point and then setting it later back into PT. When the
6156 current-selected-window and the final-selected-window both show
6157 the current buffer, this suffers from the problem that the
6158 current PT is the window-point of the current-selected-window,
6159 while the final PT is the point of the final-selected-window, so
6160 this copy from one PT to the other would end up moving the
6161 window-point of the final-selected-window to the window-point of
6162 the current-selected-window. So we have to be careful which
6163 point of the current-buffer we copy into old_point. */
6164 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6165 && WINDOWP (selected_window)
6166 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6167 && !EQ (selected_window, data->current_window))
6168 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6169 else
6170 old_point = PT;
6171 else
6172 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6173 point in new_current_buffer as of the last time this buffer was
6174 used. This can be non-deterministic since it can be changed by
6175 things like jit-lock by mere temporary selection of some random
6176 window that happens to show this buffer.
6177 So if possible we want this arbitrary choice of "which point" to
6178 be the one from the to-be-selected-window so as to prevent this
6179 window's cursor from being copied from another window. */
6180 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6181 /* If current_window = selected_window, its point is in BUF_PT. */
6182 && !EQ (selected_window, data->current_window))
6183 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6184 else
6185 old_point = BUF_PT (XBUFFER (new_current_buffer));
6188 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6189 f = XFRAME (frame);
6191 /* If f is a dead frame, don't bother rebuilding its window tree.
6192 However, there is other stuff we should still try to do below. */
6193 if (FRAME_LIVE_P (f))
6195 Lisp_Object window;
6196 Lisp_Object dead_windows = Qnil;
6197 register Lisp_Object tem, par, pers;
6198 register struct window *w;
6199 register struct saved_window *p;
6200 struct window *root_window;
6201 struct window **leaf_windows;
6202 int n_leaf_windows;
6203 ptrdiff_t k;
6204 int i, n;
6206 /* Don't do this within the main loop below: This may call Lisp
6207 code and is thus potentially unsafe while input is blocked. */
6208 for (k = 0; k < saved_windows->header.size; k++)
6210 p = SAVED_WINDOW_N (saved_windows, k);
6211 window = p->window;
6212 w = XWINDOW (window);
6213 if (BUFFERP (w->contents)
6214 && !EQ (w->contents, p->buffer)
6215 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6216 /* If a window we restore gets another buffer, record the
6217 window's old buffer. */
6218 call1 (Qrecord_window_buffer, window);
6221 /* Disallow x_set_window_size, temporarily. */
6222 f->can_x_set_window_size = false;
6223 /* The mouse highlighting code could get screwed up
6224 if it runs during this. */
6225 block_input ();
6227 /* "Swap out" point from the selected window's buffer
6228 into the window itself. (Normally the pointm of the selected
6229 window holds garbage.) We do this now, before
6230 restoring the window contents, and prevent it from
6231 being done later on when we select a new window. */
6232 if (! NILP (XWINDOW (selected_window)->contents))
6234 w = XWINDOW (selected_window);
6235 set_marker_both (w->pointm,
6236 w->contents,
6237 BUF_PT (XBUFFER (w->contents)),
6238 BUF_PT_BYTE (XBUFFER (w->contents)));
6241 fset_redisplay (f);
6242 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6244 /* Problem: Freeing all matrices and later allocating them again
6245 is a serious redisplay flickering problem. What we would
6246 really like to do is to free only those matrices not reused
6247 below. */
6248 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6249 int nwindows = count_windows (root_window);
6250 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6251 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6253 /* Kludge Alert!
6254 Mark all windows now on frame as "deleted".
6255 Restoring the new configuration "undeletes" any that are in it.
6257 Save their current buffers in their height fields, since we may
6258 need it later, if a buffer saved in the configuration is now
6259 dead. */
6260 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6262 for (k = 0; k < saved_windows->header.size; k++)
6264 p = SAVED_WINDOW_N (saved_windows, k);
6265 window = p->window;
6266 w = XWINDOW (window);
6267 wset_next (w, Qnil);
6269 if (!NILP (p->parent))
6270 wset_parent
6271 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6272 else
6273 wset_parent (w, Qnil);
6275 if (!NILP (p->prev))
6277 wset_prev
6278 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6279 wset_next (XWINDOW (w->prev), p->window);
6281 else
6283 wset_prev (w, Qnil);
6284 if (!NILP (w->parent))
6285 wset_combination (XWINDOW (w->parent),
6286 (XINT (p->total_cols)
6287 != XWINDOW (w->parent)->total_cols),
6288 p->window);
6291 /* If we squirreled away the buffer, restore it now. */
6292 if (BUFFERP (w->combination_limit))
6293 wset_buffer (w, w->combination_limit);
6294 w->pixel_left = XFASTINT (p->pixel_left);
6295 w->pixel_top = XFASTINT (p->pixel_top);
6296 w->pixel_width = XFASTINT (p->pixel_width);
6297 w->pixel_height = XFASTINT (p->pixel_height);
6298 w->left_col = XFASTINT (p->left_col);
6299 w->top_line = XFASTINT (p->top_line);
6300 w->total_cols = XFASTINT (p->total_cols);
6301 w->total_lines = XFASTINT (p->total_lines);
6302 wset_normal_cols (w, p->normal_cols);
6303 wset_normal_lines (w, p->normal_lines);
6304 w->hscroll = XFASTINT (p->hscroll);
6305 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6306 w->min_hscroll = XFASTINT (p->min_hscroll);
6307 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6308 wset_display_table (w, p->display_table);
6309 w->left_margin_cols = XINT (p->left_margin_cols);
6310 w->right_margin_cols = XINT (p->right_margin_cols);
6311 w->left_fringe_width = XINT (p->left_fringe_width);
6312 w->right_fringe_width = XINT (p->right_fringe_width);
6313 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6314 w->scroll_bar_width = XINT (p->scroll_bar_width);
6315 w->scroll_bar_height = XINT (p->scroll_bar_height);
6316 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6317 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6318 wset_dedicated (w, p->dedicated);
6319 wset_combination_limit (w, p->combination_limit);
6320 /* Restore any window parameters that have been saved.
6321 Parameters that have not been saved are left alone. */
6322 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6324 pers = XCAR (tem);
6325 if (CONSP (pers))
6327 if (NILP (XCDR (pers)))
6329 par = Fassq (XCAR (pers), w->window_parameters);
6330 if (CONSP (par) && !NILP (XCDR (par)))
6331 /* Reset a parameter to nil if and only if it
6332 has a non-nil association. Don't make new
6333 associations. */
6334 Fsetcdr (par, Qnil);
6336 else
6337 /* Always restore a non-nil value. */
6338 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6342 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6343 /* If saved buffer is alive, install it. */
6345 wset_buffer (w, p->buffer);
6346 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6347 set_marker_restricted (w->start, p->start, w->contents);
6348 set_marker_restricted (w->pointm, p->pointm, w->contents);
6349 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6350 /* As documented in Fcurrent_window_configuration, don't
6351 restore the location of point in the buffer which was
6352 current when the window configuration was recorded. */
6353 if (!EQ (p->buffer, new_current_buffer)
6354 && XBUFFER (p->buffer) == current_buffer)
6355 Fgoto_char (w->pointm);
6357 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6358 /* Keep window's old buffer; make sure the markers are real. */
6360 /* Set window markers at start of visible range. */
6361 if (XMARKER (w->start)->buffer == 0)
6362 set_marker_restricted_both (w->start, w->contents, 0, 0);
6363 if (XMARKER (w->pointm)->buffer == 0)
6364 set_marker_restricted_both
6365 (w->pointm, w->contents,
6366 BUF_PT (XBUFFER (w->contents)),
6367 BUF_PT_BYTE (XBUFFER (w->contents)));
6368 if (XMARKER (w->old_pointm)->buffer == 0)
6369 set_marker_restricted_both
6370 (w->old_pointm, w->contents,
6371 BUF_PT (XBUFFER (w->contents)),
6372 BUF_PT_BYTE (XBUFFER (w->contents)));
6373 w->start_at_line_beg = 1;
6375 else if (!NILP (w->start))
6376 /* Leaf window has no live buffer, get one. */
6378 /* Get the buffer via other_buffer_safely in order to
6379 avoid showing an unimportant buffer and, if necessary, to
6380 recreate *scratch* in the course (part of Juanma's bs-show
6381 scenario from March 2011). */
6382 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6383 /* This will set the markers to beginning of visible
6384 range. */
6385 set_marker_restricted_both (w->start, w->contents, 0, 0);
6386 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6387 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6388 w->start_at_line_beg = 1;
6389 if (!NILP (w->dedicated))
6390 /* Record this window as dead. */
6391 dead_windows = Fcons (window, dead_windows);
6392 /* Make sure window is no more dedicated. */
6393 wset_dedicated (w, Qnil);
6397 fset_root_window (f, data->root_window);
6398 /* Arrange *not* to restore point in the buffer that was
6399 current when the window configuration was saved. */
6400 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6401 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6402 make_number (old_point),
6403 XWINDOW (data->current_window)->contents);
6405 /* In the following call to `select-window', prevent "swapping out
6406 point" in the old selected window using the buffer that has
6407 been restored into it. We already swapped out that point from
6408 that window's old buffer.
6410 Do not record the buffer here. We do that in a separate call
6411 to select_window below. See also Bug#16207. */
6412 select_window (data->current_window, Qt, 1);
6413 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6414 last_selected_window)
6415 = selected_window;
6417 if (NILP (data->focus_frame)
6418 || (FRAMEP (data->focus_frame)
6419 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6420 Fredirect_frame_focus (frame, data->focus_frame);
6422 /* Now, free glyph matrices in windows that were not reused. */
6423 for (i = n = 0; i < n_leaf_windows; ++i)
6425 if (NILP (leaf_windows[i]->contents))
6426 free_window_matrices (leaf_windows[i]);
6427 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6428 ++n;
6431 /* Allow x_set_window_size again and apply frame size changes if
6432 needed. */
6433 f->can_x_set_window_size = true;
6434 adjust_frame_size (f, -1, -1, 1, 0, Qset_window_configuration);
6436 adjust_frame_glyphs (f);
6437 unblock_input ();
6439 /* Scan dead buffer windows. */
6440 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6442 window = XCAR (dead_windows);
6443 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6444 delete_deletable_window (window);
6447 /* Record the selected window's buffer here. The window should
6448 already be the selected one from the call above. */
6449 select_window (data->current_window, Qnil, 0);
6451 /* Fselect_window will have made f the selected frame, so we
6452 reselect the proper frame here. Fhandle_switch_frame will change the
6453 selected window too, but that doesn't make the call to
6454 Fselect_window above totally superfluous; it still sets f's
6455 selected window. */
6456 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6457 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6459 run_window_configuration_change_hook (f);
6462 if (!NILP (new_current_buffer))
6464 Fset_buffer (new_current_buffer);
6465 /* If the new current buffer doesn't appear in the selected
6466 window, go to its old point (see bug#12208). */
6467 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6468 Fgoto_char (make_number (old_point));
6471 Vminibuf_scroll_window = data->minibuf_scroll_window;
6472 minibuf_selected_window = data->minibuf_selected_window;
6474 SAFE_FREE ();
6475 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6479 void
6480 restore_window_configuration (Lisp_Object configuration)
6482 Fset_window_configuration (configuration);
6486 /* If WINDOW is an internal window, recursively delete all child windows
6487 reachable via the next and contents slots of WINDOW. Otherwise setup
6488 WINDOW to not show any buffer. */
6490 void
6491 delete_all_child_windows (Lisp_Object window)
6493 register struct window *w;
6495 w = XWINDOW (window);
6497 if (!NILP (w->next))
6498 /* Delete WINDOW's siblings (we traverse postorderly). */
6499 delete_all_child_windows (w->next);
6501 if (WINDOWP (w->contents))
6503 delete_all_child_windows (w->contents);
6504 wset_combination (w, 0, Qnil);
6506 else if (BUFFERP (w->contents))
6508 unshow_buffer (w);
6509 unchain_marker (XMARKER (w->pointm));
6510 unchain_marker (XMARKER (w->old_pointm));
6511 unchain_marker (XMARKER (w->start));
6512 /* Since combination limit makes sense for an internal windows
6513 only, we use this slot to save the buffer for the sake of
6514 possible resurrection in Fset_window_configuration. */
6515 wset_combination_limit (w, w->contents);
6516 wset_buffer (w, Qnil);
6519 Vwindow_list = Qnil;
6522 static int
6523 count_windows (register struct window *window)
6525 register int count = 1;
6526 if (!NILP (window->next))
6527 count += count_windows (XWINDOW (window->next));
6528 if (WINDOWP (window->contents))
6529 count += count_windows (XWINDOW (window->contents));
6530 return count;
6534 /* Fill vector FLAT with leaf windows under W, starting at index I.
6535 Value is last index + 1. */
6536 static int
6537 get_leaf_windows (struct window *w, struct window **flat, int i)
6539 while (w)
6541 if (WINDOWP (w->contents))
6542 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6543 else
6544 flat[i++] = w;
6546 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6549 return i;
6553 /* Return a pointer to the glyph W's physical cursor is on. Value is
6554 null if W's current matrix is invalid, so that no meaningful glyph
6555 can be returned. */
6556 struct glyph *
6557 get_phys_cursor_glyph (struct window *w)
6559 struct glyph_row *row;
6560 struct glyph *glyph;
6561 int hpos = w->phys_cursor.hpos;
6563 if (!(w->phys_cursor.vpos >= 0
6564 && w->phys_cursor.vpos < w->current_matrix->nrows))
6565 return NULL;
6567 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6568 if (!row->enabled_p)
6569 return NULL;
6571 if (w->hscroll)
6573 /* When the window is hscrolled, cursor hpos can legitimately be
6574 out of bounds, but we draw the cursor at the corresponding
6575 window margin in that case. */
6576 if (!row->reversed_p && hpos < 0)
6577 hpos = 0;
6578 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6579 hpos = row->used[TEXT_AREA] - 1;
6582 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6583 glyph = row->glyphs[TEXT_AREA] + hpos;
6584 else
6585 glyph = NULL;
6587 return glyph;
6591 static int
6592 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6594 register struct saved_window *p;
6595 register struct window *w;
6596 register Lisp_Object tem, pers, par;
6598 for (; !NILP (window); window = w->next)
6600 p = SAVED_WINDOW_N (vector, i);
6601 w = XWINDOW (window);
6603 wset_temslot (w, make_number (i)); i++;
6604 p->window = window;
6605 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6606 p->pixel_left = make_number (w->pixel_left);
6607 p->pixel_top = make_number (w->pixel_top);
6608 p->pixel_width = make_number (w->pixel_width);
6609 p->pixel_height = make_number (w->pixel_height);
6610 p->left_col = make_number (w->left_col);
6611 p->top_line = make_number (w->top_line);
6612 p->total_cols = make_number (w->total_cols);
6613 p->total_lines = make_number (w->total_lines);
6614 p->normal_cols = w->normal_cols;
6615 p->normal_lines = w->normal_lines;
6616 XSETFASTINT (p->hscroll, w->hscroll);
6617 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6618 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6619 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6620 p->display_table = w->display_table;
6621 p->left_margin_cols = make_number (w->left_margin_cols);
6622 p->right_margin_cols = make_number (w->right_margin_cols);
6623 p->left_fringe_width = make_number (w->left_fringe_width);
6624 p->right_fringe_width = make_number (w->right_fringe_width);
6625 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6626 p->scroll_bar_width = make_number (w->scroll_bar_width);
6627 p->scroll_bar_height = make_number (w->scroll_bar_height);
6628 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6629 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6630 p->dedicated = w->dedicated;
6631 p->combination_limit = w->combination_limit;
6632 p->window_parameters = Qnil;
6634 if (!NILP (Vwindow_persistent_parameters))
6636 /* Run cycle detection on Vwindow_persistent_parameters. */
6637 Lisp_Object tortoise, hare;
6639 hare = tortoise = Vwindow_persistent_parameters;
6640 while (CONSP (hare))
6642 hare = XCDR (hare);
6643 if (!CONSP (hare))
6644 break;
6646 hare = XCDR (hare);
6647 tortoise = XCDR (tortoise);
6649 if (EQ (hare, tortoise))
6650 /* Reset Vwindow_persistent_parameters to Qnil. */
6652 Vwindow_persistent_parameters = Qnil;
6653 break;
6657 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6658 tem = XCDR (tem))
6660 pers = XCAR (tem);
6661 /* Save values for persistent window parameters. */
6662 if (CONSP (pers) && !NILP (XCDR (pers)))
6664 par = Fassq (XCAR (pers), w->window_parameters);
6665 if (NILP (par))
6666 /* If the window has no value for the parameter,
6667 make one. */
6668 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6669 p->window_parameters);
6670 else
6671 /* If the window has a value for the parameter,
6672 save it. */
6673 p->window_parameters = Fcons (Fcons (XCAR (par),
6674 XCDR (par)),
6675 p->window_parameters);
6680 if (BUFFERP (w->contents))
6682 /* Save w's value of point in the window configuration. If w
6683 is the selected window, then get the value of point from
6684 the buffer; pointm is garbage in the selected window. */
6685 if (EQ (window, selected_window))
6686 p->pointm = build_marker (XBUFFER (w->contents),
6687 BUF_PT (XBUFFER (w->contents)),
6688 BUF_PT_BYTE (XBUFFER (w->contents)));
6689 else
6690 p->pointm = Fcopy_marker (w->pointm, Qnil);
6691 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6692 XMARKER (p->pointm)->insertion_type
6693 = !NILP (buffer_local_value /* Don't signal error if void. */
6694 (Qwindow_point_insertion_type, w->contents));
6695 XMARKER (p->old_pointm)->insertion_type
6696 = !NILP (buffer_local_value /* Don't signal error if void. */
6697 (Qwindow_point_insertion_type, w->contents));
6699 p->start = Fcopy_marker (w->start, Qnil);
6700 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6702 else
6704 p->pointm = Qnil;
6705 p->old_pointm = Qnil;
6706 p->start = Qnil;
6707 p->start_at_line_beg = Qnil;
6710 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6711 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6713 if (WINDOWP (w->contents))
6714 i = save_window_save (w->contents, vector, i);
6717 return i;
6720 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6721 Scurrent_window_configuration, 0, 1, 0,
6722 doc: /* Return an object representing the current window configuration of FRAME.
6723 If FRAME is nil or omitted, use the selected frame.
6724 This describes the number of windows, their sizes and current buffers,
6725 and for each displayed buffer, where display starts, and the position of
6726 point. An exception is made for point in the current buffer:
6727 its value is -not- saved.
6728 This also records the currently selected frame, and FRAME's focus
6729 redirection (see `redirect-frame-focus'). The variable
6730 `window-persistent-parameters' specifies which window parameters are
6731 saved by this function. */)
6732 (Lisp_Object frame)
6734 register Lisp_Object tem;
6735 register int n_windows;
6736 register struct save_window_data *data;
6737 register int i;
6738 struct frame *f = decode_live_frame (frame);
6740 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6741 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6742 PVEC_WINDOW_CONFIGURATION);
6744 data->frame_cols = FRAME_COLS (f);
6745 data->frame_lines = FRAME_LINES (f);
6746 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6747 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6748 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6749 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6750 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6751 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6752 data->selected_frame = selected_frame;
6753 data->current_window = FRAME_SELECTED_WINDOW (f);
6754 XSETBUFFER (data->current_buffer, current_buffer);
6755 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6756 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6757 data->root_window = FRAME_ROOT_WINDOW (f);
6758 data->focus_frame = FRAME_FOCUS_FRAME (f);
6759 tem = make_uninit_vector (n_windows);
6760 data->saved_windows = tem;
6761 for (i = 0; i < n_windows; i++)
6762 ASET (tem, i,
6763 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6764 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6765 XSETWINDOW_CONFIGURATION (tem, data);
6766 return (tem);
6769 /* Called after W's margins, fringes or scroll bars was adjusted. */
6771 static void
6772 apply_window_adjustment (struct window *w)
6774 eassert (w);
6775 clear_glyph_matrix (w->current_matrix);
6776 w->window_end_valid = 0;
6777 windows_or_buffers_changed = 30;
6778 wset_redisplay (w);
6779 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6783 /***********************************************************************
6784 Marginal Areas
6785 ***********************************************************************/
6787 static struct window *
6788 set_window_margins (struct window *w, Lisp_Object left_width,
6789 Lisp_Object right_width)
6791 int left, right;
6792 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6794 left = (NILP (left_width) ? 0
6795 : (CHECK_NATNUM (left_width), XINT (left_width)));
6796 right = (NILP (right_width) ? 0
6797 : (CHECK_NATNUM (right_width), XINT (right_width)));
6799 if (w->left_margin_cols != left || w->right_margin_cols != right)
6801 /* Don't change anything if new margins won't fit. */
6802 if ((WINDOW_PIXEL_WIDTH (w)
6803 - WINDOW_FRINGES_WIDTH (w)
6804 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6805 - (left + right) * unit)
6806 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6808 w->left_margin_cols = left;
6809 w->right_margin_cols = right;
6811 return w;
6813 else
6814 return NULL;
6816 else
6817 return NULL;
6820 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6821 2, 3, 0,
6822 doc: /* Set width of marginal areas of window WINDOW.
6823 WINDOW must be a live window and defaults to the selected one.
6825 Second arg LEFT-WIDTH specifies the number of character cells to
6826 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6827 does the same for the right marginal area. A nil width parameter
6828 means no margin.
6830 Return t if any margin was actually changed and nil otherwise. */)
6831 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6833 struct window *w = set_window_margins (decode_live_window (window),
6834 left_width, right_width);
6835 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6839 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6840 0, 1, 0,
6841 doc: /* Get width of marginal areas of window WINDOW.
6842 WINDOW must be a live window and defaults to the selected one.
6844 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6845 If a marginal area does not exist, its width will be returned
6846 as nil. */)
6847 (Lisp_Object window)
6849 struct window *w = decode_live_window (window);
6850 return Fcons (w->left_margin_cols
6851 ? make_number (w->left_margin_cols) : Qnil,
6852 w->right_margin_cols
6853 ? make_number (w->right_margin_cols) : Qnil);
6858 /***********************************************************************
6859 Fringes
6860 ***********************************************************************/
6862 static struct window *
6863 set_window_fringes (struct window *w, Lisp_Object left_width,
6864 Lisp_Object right_width, Lisp_Object outside_margins)
6866 int left, right, outside = !NILP (outside_margins);
6868 left = (NILP (left_width) ? -1
6869 : (CHECK_NATNUM (left_width), XINT (left_width)));
6870 right = (NILP (right_width) ? -1
6871 : (CHECK_NATNUM (right_width), XINT (right_width)));
6873 /* Do nothing on a tty or if nothing to actually change. */
6874 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6875 && (w->left_fringe_width != left
6876 || w->right_fringe_width != right
6877 || w->fringes_outside_margins != outside))
6879 if (left > 0 || right > 0)
6881 /* Don't change anything if new fringes don't fit. */
6882 if ((WINDOW_PIXEL_WIDTH (w)
6883 - WINDOW_MARGINS_WIDTH (w)
6884 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6885 - max (left, 0) - max (right, 0))
6886 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6887 return NULL;
6890 w->left_fringe_width = left;
6891 w->right_fringe_width = right;
6892 w->fringes_outside_margins = outside;
6894 return w;
6896 else
6897 return NULL;
6900 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6901 2, 4, 0,
6902 doc: /* Set the fringe widths of window WINDOW.
6903 WINDOW must be a live window and defaults to the selected one.
6905 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6906 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6907 fringe width. If a fringe width arg is nil, that means to use the
6908 frame's default fringe width. Default fringe widths can be set with
6909 the command `set-fringe-style'.
6910 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6911 outside of the display margins. By default, fringes are drawn between
6912 display marginal areas and the text area.
6914 Return t if any fringe was actually changed and nil otherwise. */)
6915 (Lisp_Object window, Lisp_Object left_width,
6916 Lisp_Object right_width, Lisp_Object outside_margins)
6918 struct window *w
6919 = set_window_fringes (decode_live_window (window),
6920 left_width, right_width, outside_margins);
6921 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6925 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6926 0, 1, 0,
6927 doc: /* Get width of fringes of window WINDOW.
6928 WINDOW must be a live window and defaults to the selected one.
6930 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6931 (Lisp_Object window)
6933 struct window *w = decode_live_window (window);
6935 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6936 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6937 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6942 /***********************************************************************
6943 Scroll bars
6944 ***********************************************************************/
6946 static struct window *
6947 set_window_scroll_bars (struct window *w, Lisp_Object width,
6948 Lisp_Object vertical_type, Lisp_Object height,
6949 Lisp_Object horizontal_type)
6951 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6952 bool changed = 0;
6954 if (iwidth == 0)
6955 vertical_type = Qnil;
6957 if (!(NILP (vertical_type)
6958 || EQ (vertical_type, Qleft)
6959 || EQ (vertical_type, Qright)
6960 || EQ (vertical_type, Qt)))
6961 error ("Invalid type of vertical scroll bar");
6963 if (w->scroll_bar_width != iwidth
6964 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6966 /* Don't change anything if new scroll bar won't fit. */
6967 if ((WINDOW_PIXEL_WIDTH (w)
6968 - WINDOW_MARGINS_WIDTH (w)
6969 - WINDOW_FRINGES_WIDTH (w)
6970 - max (iwidth, 0))
6971 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6973 w->scroll_bar_width = iwidth;
6974 wset_vertical_scroll_bar_type (w, vertical_type);
6975 changed = 1;
6979 #if USE_HORIZONTAL_SCROLL_BARS
6981 int iheight = (NILP (height) ? -1 : (CHECK_NATNUM (height), XINT (height)));
6983 if (MINI_WINDOW_P (w) || iheight == 0)
6984 horizontal_type = Qnil;
6986 if (!(NILP (horizontal_type)
6987 || EQ (horizontal_type, Qbottom)
6988 || EQ (horizontal_type, Qt)))
6989 error ("Invalid type of horizontal scroll bar");
6991 if (w->scroll_bar_height != iheight
6992 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6994 /* Don't change anything if new scroll bar won't fit. */
6995 if ((WINDOW_PIXEL_HEIGHT (w)
6996 - WINDOW_HEADER_LINE_HEIGHT (w)
6997 - WINDOW_MODE_LINE_HEIGHT (w)
6998 - max (iheight, 0))
6999 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7001 w->scroll_bar_height = iheight;
7002 wset_horizontal_scroll_bar_type (w, horizontal_type);
7003 changed = 1;
7007 #else
7008 wset_horizontal_scroll_bar_type (w, Qnil);
7009 #endif
7011 return changed ? w : NULL;
7014 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7015 Sset_window_scroll_bars, 1, 5, 0,
7016 doc: /* Set width and type of scroll bars of window WINDOW.
7017 WINDOW must be a live window and defaults to the selected one.
7019 Second parameter WIDTH specifies the pixel width for the vertical scroll
7020 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
7021 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7022 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
7023 frame's scroll-bar type.
7025 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7026 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
7027 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7028 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
7029 means to use the frame's horizontal scroll-bar type.
7031 Return t if scroll bars were actually changed and nil otherwise. */)
7032 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7033 Lisp_Object height, Lisp_Object horizontal_type)
7035 struct window *w
7036 = set_window_scroll_bars (decode_live_window (window),
7037 width, vertical_type, height, horizontal_type);
7038 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7042 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7043 0, 1, 0,
7044 doc: /* Get width and type of scroll bars of window WINDOW.
7045 WINDOW must be a live window and defaults to the selected one.
7047 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7048 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7049 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7050 value. */)
7051 (Lisp_Object window)
7053 struct window *w = decode_live_window (window);
7055 return Fcons (((w->scroll_bar_width >= 0)
7056 ? make_number (w->scroll_bar_width)
7057 : Qnil),
7058 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7059 w->vertical_scroll_bar_type,
7060 ((w->scroll_bar_height >= 0)
7061 ? make_number (w->scroll_bar_height)
7062 : Qnil),
7063 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7064 w->horizontal_scroll_bar_type));
7067 /***********************************************************************
7068 Smooth scrolling
7069 ***********************************************************************/
7071 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7072 doc: /* Return the amount by which WINDOW is scrolled vertically.
7073 If WINDOW is omitted or nil, it defaults to the selected window.
7074 Normally, value is a multiple of the canonical character height of WINDOW;
7075 optional second arg PIXELS-P means value is measured in pixels. */)
7076 (Lisp_Object window, Lisp_Object pixels_p)
7078 Lisp_Object result;
7079 struct window *w = decode_live_window (window);
7080 struct frame *f = XFRAME (w->frame);
7082 if (FRAME_WINDOW_P (f))
7083 result = (NILP (pixels_p)
7084 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7085 : make_number (-w->vscroll));
7086 else
7087 result = make_number (0);
7088 return result;
7092 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7093 2, 3, 0,
7094 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7095 WINDOW nil means use the selected window. Normally, VSCROLL is a
7096 non-negative multiple of the canonical character height of WINDOW;
7097 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7098 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7099 corresponds to an integral number of pixels. The return value is the
7100 result of this rounding.
7101 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7102 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7104 struct window *w = decode_live_window (window);
7105 struct frame *f = XFRAME (w->frame);
7107 CHECK_NUMBER_OR_FLOAT (vscroll);
7109 if (FRAME_WINDOW_P (f))
7111 int old_dy = w->vscroll;
7113 w->vscroll = - (NILP (pixels_p)
7114 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7115 : XFLOATINT (vscroll));
7116 w->vscroll = min (w->vscroll, 0);
7118 if (w->vscroll != old_dy)
7120 /* Adjust glyph matrix of the frame if the virtual display
7121 area becomes larger than before. */
7122 if (w->vscroll < 0 && w->vscroll < old_dy)
7123 adjust_frame_glyphs (f);
7125 /* Prevent redisplay shortcuts. */
7126 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
7130 return Fwindow_vscroll (window, pixels_p);
7134 /* Call FN for all leaf windows on frame F. FN is called with the
7135 first argument being a pointer to the leaf window, and with
7136 additional argument USER_DATA. Stops when FN returns 0. */
7138 static void
7139 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
7140 void *user_data)
7142 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7143 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7144 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7148 /* Helper function for foreach_window. Call FN for all leaf windows
7149 reachable from W. FN is called with the first argument being a
7150 pointer to the leaf window, and with additional argument USER_DATA.
7151 Stop when FN returns 0. Value is 0 if stopped by FN. */
7153 static int
7154 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7156 int cont;
7158 for (cont = 1; w && cont;)
7160 if (WINDOWP (w->contents))
7161 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7162 else
7163 cont = fn (w, user_data);
7165 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7168 return cont;
7171 /***********************************************************************
7172 Initialization
7173 ***********************************************************************/
7175 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7176 describe the same state of affairs. This is used by Fequal.
7178 IGNORE_POSITIONS means ignore non-matching scroll positions
7179 and the like.
7181 This ignores a couple of things like the dedication status of
7182 window, combination_limit and the like. This might have to be
7183 fixed. */
7185 bool
7186 compare_window_configurations (Lisp_Object configuration1,
7187 Lisp_Object configuration2,
7188 bool ignore_positions)
7190 register struct save_window_data *d1, *d2;
7191 struct Lisp_Vector *sws1, *sws2;
7192 ptrdiff_t i;
7194 CHECK_WINDOW_CONFIGURATION (configuration1);
7195 CHECK_WINDOW_CONFIGURATION (configuration2);
7197 d1 = (struct save_window_data *) XVECTOR (configuration1);
7198 d2 = (struct save_window_data *) XVECTOR (configuration2);
7199 sws1 = XVECTOR (d1->saved_windows);
7200 sws2 = XVECTOR (d2->saved_windows);
7202 /* Frame settings must match. */
7203 if (d1->frame_cols != d2->frame_cols
7204 || d1->frame_lines != d2->frame_lines
7205 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7206 || !EQ (d1->selected_frame, d2->selected_frame)
7207 || !EQ (d1->current_buffer, d2->current_buffer)
7208 || (!ignore_positions
7209 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7210 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7211 || !EQ (d1->focus_frame, d2->focus_frame)
7212 /* Verify that the two configurations have the same number of windows. */
7213 || sws1->header.size != sws2->header.size)
7214 return 0;
7216 for (i = 0; i < sws1->header.size; i++)
7218 struct saved_window *sw1, *sw2;
7220 sw1 = SAVED_WINDOW_N (sws1, i);
7221 sw2 = SAVED_WINDOW_N (sws2, i);
7223 if (
7224 /* The "current" windows in the two configurations must
7225 correspond to each other. */
7226 EQ (d1->current_window, sw1->window)
7227 != EQ (d2->current_window, sw2->window)
7228 /* Windows' buffers must match. */
7229 || !EQ (sw1->buffer, sw2->buffer)
7230 || !EQ (sw1->pixel_left, sw2->pixel_left)
7231 || !EQ (sw1->pixel_top, sw2->pixel_top)
7232 || !EQ (sw1->pixel_height, sw2->pixel_height)
7233 || !EQ (sw1->pixel_width, sw2->pixel_width)
7234 || !EQ (sw1->left_col, sw2->left_col)
7235 || !EQ (sw1->top_line, sw2->top_line)
7236 || !EQ (sw1->total_cols, sw2->total_cols)
7237 || !EQ (sw1->total_lines, sw2->total_lines)
7238 || !EQ (sw1->display_table, sw2->display_table)
7239 /* The next two disjuncts check the window structure for
7240 equality. */
7241 || !EQ (sw1->parent, sw2->parent)
7242 || !EQ (sw1->prev, sw2->prev)
7243 || (!ignore_positions
7244 && (!EQ (sw1->hscroll, sw2->hscroll)
7245 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7246 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7247 || NILP (Fequal (sw1->start, sw2->start))
7248 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7249 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7250 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7251 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7252 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7253 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7254 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7255 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7256 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7257 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7258 return 0;
7261 return 1;
7264 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7265 Scompare_window_configurations, 2, 2, 0,
7266 doc: /* Compare two window configurations as regards the structure of windows.
7267 This function ignores details such as the values of point
7268 and scrolling positions. */)
7269 (Lisp_Object x, Lisp_Object y)
7271 if (compare_window_configurations (x, y, 1))
7272 return Qt;
7273 return Qnil;
7276 void
7277 init_window_once (void)
7279 struct frame *f = make_initial_frame ();
7280 XSETFRAME (selected_frame, f);
7281 Vterminal_frame = selected_frame;
7282 minibuf_window = f->minibuffer_window;
7283 selected_window = f->selected_window;
7286 void
7287 init_window (void)
7289 Vwindow_list = Qnil;
7292 void
7293 syms_of_window (void)
7295 DEFSYM (Qscroll_up, "scroll-up");
7296 DEFSYM (Qscroll_down, "scroll-down");
7297 DEFSYM (Qscroll_command, "scroll-command");
7299 Fput (Qscroll_up, Qscroll_command, Qt);
7300 Fput (Qscroll_down, Qscroll_command, Qt);
7302 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7303 DEFSYM (Qwindowp, "windowp");
7304 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7305 DEFSYM (Qwindow_live_p, "window-live-p");
7306 DEFSYM (Qwindow_valid_p, "window-valid-p");
7307 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7308 DEFSYM (Qdelete_window, "delete-window");
7309 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7310 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7311 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7312 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7313 DEFSYM (Qsafe, "safe");
7314 DEFSYM (Qdisplay_buffer, "display-buffer");
7315 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7316 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7317 DEFSYM (Qget_mru_window, "get-mru-window");
7318 DEFSYM (Qwindow_size, "window-size");
7319 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7320 DEFSYM (Qabove, "above");
7321 DEFSYM (Qbelow, "below");
7322 DEFSYM (Qclone_of, "clone-of");
7323 DEFSYM (Qfloor, "floor");
7324 DEFSYM (Qceiling, "ceiling");
7326 staticpro (&Vwindow_list);
7328 minibuf_selected_window = Qnil;
7329 staticpro (&minibuf_selected_window);
7331 window_scroll_pixel_based_preserve_x = -1;
7332 window_scroll_pixel_based_preserve_y = -1;
7333 window_scroll_preserve_hpos = -1;
7334 window_scroll_preserve_vpos = -1;
7336 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7337 doc: /* Non-nil means call as function to display a help buffer.
7338 The function is called with one argument, the buffer to be displayed.
7339 Used by `with-output-to-temp-buffer'.
7340 If this function is used, then it must do the entire job of showing
7341 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7342 Vtemp_buffer_show_function = Qnil;
7344 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7345 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7346 Vminibuf_scroll_window = Qnil;
7348 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7349 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7350 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7351 is displayed in the `mode-line' face. */);
7352 mode_line_in_non_selected_windows = true;
7354 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7355 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7356 Vother_window_scroll_buffer = Qnil;
7358 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7359 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7360 auto_window_vscroll_p = true;
7362 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7363 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7364 next_screen_context_lines = 2;
7366 DEFVAR_LISP ("scroll-preserve-screen-position",
7367 Vscroll_preserve_screen_position,
7368 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7369 A value of nil means point does not keep its screen position except
7370 at the scroll margin or window boundary respectively.
7371 A value of t means point keeps its screen position if the scroll
7372 command moved it vertically out of the window, e.g. when scrolling
7373 by full screens.
7374 Any other value means point always keeps its screen position.
7375 Scroll commands should have the `scroll-command' property
7376 on their symbols to be controlled by this variable. */);
7377 Vscroll_preserve_screen_position = Qnil;
7379 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7380 doc: /* Type of marker to use for `window-point'. */);
7381 Vwindow_point_insertion_type = Qnil;
7382 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7384 DEFVAR_LISP ("window-configuration-change-hook",
7385 Vwindow_configuration_change_hook,
7386 doc: /* Functions to call when window configuration changes.
7387 The buffer-local part is run once per window, with the relevant window
7388 selected; while the global part is run only once for the modified frame,
7389 with the relevant frame selected. */);
7390 Vwindow_configuration_change_hook = Qnil;
7392 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7393 doc: /* Non-nil means `recenter' redraws entire frame.
7394 If this option is non-nil, then the `recenter' command with a nil
7395 argument will redraw the entire frame; the special value `tty' causes
7396 the frame to be redrawn only if it is a tty frame. */);
7397 Vrecenter_redisplay = Qtty;
7399 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7400 doc: /* If t, resize window combinations proportionally.
7401 If this variable is nil, splitting a window gets the entire screen space
7402 for displaying the new window from the window to split. Deleting and
7403 resizing a window preferably resizes one adjacent window only.
7405 If this variable is t, splitting a window tries to get the space
7406 proportionally from all windows in the same combination. This also
7407 allows to split a window that is otherwise too small or of fixed size.
7408 Resizing and deleting a window proportionally resize all windows in the
7409 same combination.
7411 Other values are reserved for future use.
7413 This variable takes no effect if the variable `window-combination-limit' is
7414 non-nil. */);
7415 Vwindow_combination_resize = Qnil;
7417 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7418 doc: /* If non-nil, splitting a window makes a new parent window.
7419 The following values are recognized:
7421 nil means splitting a window will create a new parent window only if the
7422 window has no parent window or the window shall become part of a
7423 combination orthogonal to the one it is part of.
7425 `window-size' means that splitting a window for displaying a buffer
7426 makes a new parent window provided `display-buffer' is supposed to
7427 explicitly set the window's size due to the presence of a
7428 `window-height' or `window-width' entry in the alist used by
7429 `display-buffer'. Otherwise, this value is handled like nil.
7431 `temp-buffer' means that splitting a window for displaying a temporary
7432 buffer always makes a new parent window. Otherwise, this value is
7433 handled like nil.
7435 `display-buffer' means that splitting a window for displaying a buffer
7436 always makes a new parent window. Since temporary buffers are
7437 displayed by the function `display-buffer', this value is stronger
7438 than `temp-buffer'. Splitting a window for other purpose makes a
7439 new parent window only if needed.
7441 t means that splitting a window always creates a new parent window. If
7442 all splits behave this way, each frame's window tree is a binary
7443 tree and every window but the frame's root window has exactly one
7444 sibling.
7446 Other values are reserved for future use. */);
7447 Vwindow_combination_limit = Qwindow_size;
7449 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7450 doc: /* Alist of persistent window parameters.
7451 This alist specifies which window parameters shall get saved by
7452 `current-window-configuration' and `window-state-get' and subsequently
7453 restored to their previous values by `set-window-configuration' and
7454 `window-state-put'.
7456 The car of each entry of this alist is the symbol specifying the
7457 parameter. The cdr is one of the following:
7459 nil means the parameter is neither saved by `window-state-get' nor by
7460 `current-window-configuration'.
7462 t means the parameter is saved by `current-window-configuration' and,
7463 provided its WRITABLE argument is nil, by `window-state-get'.
7465 The symbol `writable' means the parameter is saved unconditionally by
7466 both `current-window-configuration' and `window-state-get'. Do not use
7467 this value for parameters without read syntax (like windows or frames).
7469 Parameters not saved by `current-window-configuration' or
7470 `window-state-get' are left alone by `set-window-configuration'
7471 respectively are not installed by `window-state-put'. */);
7472 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7474 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7475 doc: /* Non-nil means resize windows pixelwise.
7476 This currently affects the functions: `split-window', `maximize-window',
7477 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7478 all functions that symmetrically resize a parent window.
7480 Note that when a frame's pixel size is not a multiple of the
7481 frame's character size, at least one window may get resized
7482 pixelwise even if this option is nil. */);
7483 window_resize_pixelwise = false;
7485 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7486 Vfast_but_imprecise_scrolling,
7487 doc: /* When non-nil, accelerate scrolling operations.
7488 This comes into play when scrolling rapidly over previously
7489 unfontified buffer regions. Only those portions of the buffer which
7490 are actually going to be displayed get fontified.
7492 Note that this optimization can cause the portion of the buffer
7493 displayed after a scrolling operation to be somewhat inaccurate. */);
7494 Vfast_but_imprecise_scrolling = false;
7496 defsubr (&Sselected_window);
7497 defsubr (&Sminibuffer_window);
7498 defsubr (&Swindow_minibuffer_p);
7499 defsubr (&Swindowp);
7500 defsubr (&Swindow_valid_p);
7501 defsubr (&Swindow_live_p);
7502 defsubr (&Swindow_frame);
7503 defsubr (&Sframe_root_window);
7504 defsubr (&Sframe_first_window);
7505 defsubr (&Sframe_selected_window);
7506 defsubr (&Sset_frame_selected_window);
7507 defsubr (&Spos_visible_in_window_p);
7508 defsubr (&Swindow_line_height);
7509 defsubr (&Swindow_buffer);
7510 defsubr (&Swindow_parent);
7511 defsubr (&Swindow_top_child);
7512 defsubr (&Swindow_left_child);
7513 defsubr (&Swindow_next_sibling);
7514 defsubr (&Swindow_prev_sibling);
7515 defsubr (&Swindow_combination_limit);
7516 defsubr (&Sset_window_combination_limit);
7517 defsubr (&Swindow_use_time);
7518 defsubr (&Swindow_pixel_width);
7519 defsubr (&Swindow_pixel_height);
7520 defsubr (&Swindow_total_width);
7521 defsubr (&Swindow_total_height);
7522 defsubr (&Swindow_normal_size);
7523 defsubr (&Swindow_new_pixel);
7524 defsubr (&Swindow_new_total);
7525 defsubr (&Swindow_new_normal);
7526 defsubr (&Swindow_pixel_left);
7527 defsubr (&Swindow_pixel_top);
7528 defsubr (&Swindow_left_column);
7529 defsubr (&Swindow_top_line);
7530 defsubr (&Sset_window_new_pixel);
7531 defsubr (&Sset_window_new_total);
7532 defsubr (&Sset_window_new_normal);
7533 defsubr (&Swindow_resize_apply);
7534 defsubr (&Swindow_resize_apply_total);
7535 defsubr (&Swindow_body_height);
7536 defsubr (&Swindow_body_width);
7537 defsubr (&Swindow_hscroll);
7538 defsubr (&Sset_window_hscroll);
7539 defsubr (&Swindow_redisplay_end_trigger);
7540 defsubr (&Sset_window_redisplay_end_trigger);
7541 defsubr (&Swindow_edges);
7542 defsubr (&Swindow_pixel_edges);
7543 defsubr (&Swindow_absolute_pixel_edges);
7544 defsubr (&Swindow_mode_line_height);
7545 defsubr (&Swindow_header_line_height);
7546 defsubr (&Swindow_right_divider_width);
7547 defsubr (&Swindow_bottom_divider_width);
7548 defsubr (&Swindow_scroll_bar_width);
7549 defsubr (&Swindow_scroll_bar_height);
7550 defsubr (&Swindow_inside_edges);
7551 defsubr (&Swindow_inside_pixel_edges);
7552 defsubr (&Swindow_inside_absolute_pixel_edges);
7553 defsubr (&Scoordinates_in_window_p);
7554 defsubr (&Swindow_at);
7555 defsubr (&Swindow_point);
7556 defsubr (&Swindow_old_point);
7557 defsubr (&Swindow_start);
7558 defsubr (&Swindow_end);
7559 defsubr (&Sset_window_point);
7560 defsubr (&Sset_window_start);
7561 defsubr (&Swindow_dedicated_p);
7562 defsubr (&Sset_window_dedicated_p);
7563 defsubr (&Swindow_display_table);
7564 defsubr (&Sset_window_display_table);
7565 defsubr (&Snext_window);
7566 defsubr (&Sprevious_window);
7567 defsubr (&Swindow__sanitize_window_sizes);
7568 defsubr (&Sget_buffer_window);
7569 defsubr (&Sdelete_other_windows_internal);
7570 defsubr (&Sdelete_window_internal);
7571 defsubr (&Sresize_mini_window_internal);
7572 defsubr (&Sset_window_buffer);
7573 defsubr (&Srun_window_configuration_change_hook);
7574 defsubr (&Srun_window_scroll_functions);
7575 defsubr (&Sselect_window);
7576 defsubr (&Sforce_window_update);
7577 defsubr (&Ssplit_window_internal);
7578 defsubr (&Sscroll_up);
7579 defsubr (&Sscroll_down);
7580 defsubr (&Sscroll_left);
7581 defsubr (&Sscroll_right);
7582 defsubr (&Sother_window_for_scrolling);
7583 defsubr (&Sscroll_other_window);
7584 defsubr (&Sminibuffer_selected_window);
7585 defsubr (&Srecenter);
7586 defsubr (&Swindow_text_width);
7587 defsubr (&Swindow_text_height);
7588 defsubr (&Smove_to_window_line);
7589 defsubr (&Swindow_configuration_p);
7590 defsubr (&Swindow_configuration_frame);
7591 defsubr (&Sset_window_configuration);
7592 defsubr (&Scurrent_window_configuration);
7593 defsubr (&Sset_window_margins);
7594 defsubr (&Swindow_margins);
7595 defsubr (&Sset_window_fringes);
7596 defsubr (&Swindow_fringes);
7597 defsubr (&Sset_window_scroll_bars);
7598 defsubr (&Swindow_scroll_bars);
7599 defsubr (&Swindow_vscroll);
7600 defsubr (&Sset_window_vscroll);
7601 defsubr (&Scompare_window_configurations);
7602 defsubr (&Swindow_list);
7603 defsubr (&Swindow_list_1);
7604 defsubr (&Swindow_prev_buffers);
7605 defsubr (&Sset_window_prev_buffers);
7606 defsubr (&Swindow_next_buffers);
7607 defsubr (&Sset_window_next_buffers);
7608 defsubr (&Swindow_parameters);
7609 defsubr (&Swindow_parameter);
7610 defsubr (&Sset_window_parameter);
7613 void
7614 keys_of_window (void)
7616 initial_define_key (control_x_map, '<', "scroll-left");
7617 initial_define_key (control_x_map, '>', "scroll-right");
7619 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7620 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7621 initial_define_key (meta_map, 'v', "scroll-down-command");