cleanups
[emacs.git] / src / window.c
blob65a837dbbad41b259ab636c4139d75f9ed3b265e
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
47 #ifdef HAVE_XWIDGETS
48 #include "xwidget.h"
49 #endif
51 Lisp_Object Qwindowp, Qwindow_live_p;
52 static Lisp_Object Qwindow_valid_p;
53 static Lisp_Object Qwindow_configuration_p;
54 static Lisp_Object Qrecord_window_buffer;
55 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
56 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
57 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
58 static Lisp_Object Qwindow_sanitize_window_sizes;
59 static Lisp_Object Qwindow_pixel_to_total;
60 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
61 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
62 static Lisp_Object Qfloor, Qceiling;
63 static Lisp_Object Qwindow_point_insertion_type;
65 static int displayed_window_lines (struct window *);
66 static int count_windows (struct window *);
67 static int get_leaf_windows (struct window *, struct window **, int);
68 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
69 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
70 static void window_scroll_line_based (Lisp_Object, int, bool, int);
71 static int add_window_to_list (struct window *, void *);
72 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
73 Lisp_Object, int);
74 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
75 Lisp_Object *);
76 static void foreach_window (struct frame *,
77 int (* fn) (struct window *, void *),
78 void *);
79 static int foreach_window_1 (struct window *,
80 int (* fn) (struct window *, void *),
81 void *);
82 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
83 static int window_resize_check (struct window *, bool);
84 static void window_resize_apply (struct window *, bool);
85 static void window_resize_apply_total (struct window *, bool);
86 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
87 static void select_window_1 (Lisp_Object, bool);
89 static struct window *set_window_fringes (struct window *, Lisp_Object,
90 Lisp_Object, Lisp_Object);
91 static struct window *set_window_margins (struct window *, Lisp_Object,
92 Lisp_Object);
93 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
94 Lisp_Object, Lisp_Object, Lisp_Object);
95 static void apply_window_adjustment (struct window *);
97 /* This is the window in which the terminal's cursor should
98 be left when nothing is being done with it. This must
99 always be a leaf window, and its buffer is selected by
100 the top level editing loop at the end of each command.
102 This value is always the same as
103 FRAME_SELECTED_WINDOW (selected_frame). */
104 Lisp_Object selected_window;
106 /* A list of all windows for use by next_window and Fwindow_list.
107 Functions creating or deleting windows should invalidate this cache
108 by setting it to nil. */
109 Lisp_Object Vwindow_list;
111 /* The mini-buffer window of the selected frame.
112 Note that you cannot test for mini-bufferness of an arbitrary window
113 by comparing against this; but you can test for mini-bufferness of
114 the selected window. */
115 Lisp_Object minibuf_window;
117 /* Non-nil means it is the window whose mode line should be
118 shown as the selected window when the minibuffer is selected. */
119 Lisp_Object minibuf_selected_window;
121 /* Hook run at end of temp_output_buffer_show. */
122 static Lisp_Object Qtemp_buffer_show_hook;
124 /* Incremented for each window created. */
125 static int sequence_number;
127 /* Hook to run when window config changes. */
128 static Lisp_Object Qwindow_configuration_change_hook;
130 /* Used by the function window_scroll_pixel_based. */
131 static int window_scroll_pixel_based_preserve_x;
132 static int window_scroll_pixel_based_preserve_y;
134 /* Same for window_scroll_line_based. */
135 static EMACS_INT window_scroll_preserve_hpos;
136 static EMACS_INT window_scroll_preserve_vpos;
138 static void
139 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
141 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
144 /* These setters are used only in this file, so they can be private. */
145 static void
146 wset_combination_limit (struct window *w, Lisp_Object val)
148 w->combination_limit = val;
151 static void
152 wset_dedicated (struct window *w, Lisp_Object val)
154 w->dedicated = val;
157 static void
158 wset_display_table (struct window *w, Lisp_Object val)
160 w->display_table = val;
163 static void
164 wset_new_normal (struct window *w, Lisp_Object val)
166 w->new_normal = val;
169 static void
170 wset_new_total (struct window *w, Lisp_Object val)
172 w->new_total = val;
175 static void
176 wset_normal_cols (struct window *w, Lisp_Object val)
178 w->normal_cols = val;
181 static void
182 wset_normal_lines (struct window *w, Lisp_Object val)
184 w->normal_lines = val;
187 static void
188 wset_parent (struct window *w, Lisp_Object val)
190 w->parent = val;
193 static void
194 wset_pointm (struct window *w, Lisp_Object val)
196 w->pointm = val;
199 static void
200 wset_old_pointm (struct window *w, Lisp_Object val)
202 w->old_pointm = val;
205 static void
206 wset_start (struct window *w, Lisp_Object val)
208 w->start = val;
211 static void
212 wset_temslot (struct window *w, Lisp_Object val)
214 w->temslot = val;
217 static void
218 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
220 w->vertical_scroll_bar_type = val;
223 static void
224 wset_window_parameters (struct window *w, Lisp_Object val)
226 w->window_parameters = val;
229 static void
230 wset_combination (struct window *w, bool horflag, Lisp_Object val)
232 /* Since leaf windows never becomes non-leaf, there should
233 be no buffer and markers in start and pointm fields of W. */
234 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
235 w->contents = val;
236 /* When an internal window is deleted and VAL is nil, HORFLAG
237 is meaningless. */
238 if (!NILP (val))
239 w->horizontal = horflag;
242 /* Nonzero if leaf window W doesn't reflect the actual state
243 of displayed buffer due to its text or overlays change. */
245 bool
246 window_outdated (struct window *w)
248 struct buffer *b = XBUFFER (w->contents);
249 return (w->last_modified < BUF_MODIFF (b)
250 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
253 struct window *
254 decode_live_window (register Lisp_Object window)
256 if (NILP (window))
257 return XWINDOW (selected_window);
259 CHECK_LIVE_WINDOW (window);
260 return XWINDOW (window);
263 struct window *
264 decode_any_window (register Lisp_Object window)
266 struct window *w;
268 if (NILP (window))
269 return XWINDOW (selected_window);
271 CHECK_WINDOW (window);
272 w = XWINDOW (window);
273 return w;
276 static struct window *
277 decode_valid_window (register Lisp_Object window)
279 struct window *w;
281 if (NILP (window))
282 return XWINDOW (selected_window);
284 CHECK_VALID_WINDOW (window);
285 w = XWINDOW (window);
286 return w;
289 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
290 cease its buffer, and 1 means that W is about to set up the new one. */
292 static void
293 adjust_window_count (struct window *w, int arg)
295 eassert (eabs (arg) == 1);
296 if (BUFFERP (w->contents))
298 struct buffer *b = XBUFFER (w->contents);
300 if (b->base_buffer)
301 b = b->base_buffer;
302 b->window_count += arg;
303 eassert (b->window_count >= 0);
304 /* These should be recalculated by redisplay code. */
305 w->window_end_valid = 0;
306 w->base_line_pos = 0;
310 /* Set W's buffer slot to VAL and recompute number
311 of windows showing VAL if it is a buffer. */
313 void
314 wset_buffer (struct window *w, Lisp_Object val)
316 adjust_window_count (w, -1);
317 if (BUFFERP (val))
318 /* Make sure that we do not assign the buffer
319 to an internal window. */
320 eassert (MARKERP (w->start) && MARKERP (w->pointm));
321 w->contents = val;
322 adjust_window_count (w, 1);
325 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
326 doc: /* Return t if OBJECT is a window and nil otherwise. */)
327 (Lisp_Object object)
329 return WINDOWP (object) ? Qt : Qnil;
332 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
333 doc: /* Return t if OBJECT is a valid window and nil otherwise.
334 A valid window is either a window that displays a buffer or an internal
335 window. Windows that have been deleted are not valid. */)
336 (Lisp_Object object)
338 return WINDOW_VALID_P (object) ? Qt : Qnil;
341 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
342 doc: /* Return t if OBJECT is a live window and nil otherwise.
343 A live window is a window that displays a buffer.
344 Internal windows and deleted windows are not live. */)
345 (Lisp_Object object)
347 return WINDOW_LIVE_P (object) ? Qt : Qnil;
350 /* Frames and windows. */
351 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
352 doc: /* Return the frame that window WINDOW is on.
353 WINDOW must be a valid window and defaults to the selected one. */)
354 (Lisp_Object window)
356 return decode_valid_window (window)->frame;
359 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
360 doc: /* Return the root window of FRAME-OR-WINDOW.
361 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
362 With a frame argument, return that frame's root window.
363 With a window argument, return the root window of that window's frame. */)
364 (Lisp_Object frame_or_window)
366 Lisp_Object window;
368 if (NILP (frame_or_window))
369 window = SELECTED_FRAME ()->root_window;
370 else if (WINDOW_VALID_P (frame_or_window))
371 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
372 else
374 CHECK_LIVE_FRAME (frame_or_window);
375 window = XFRAME (frame_or_window)->root_window;
378 return window;
381 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
382 doc: /* Return the minibuffer window for frame FRAME.
383 If FRAME is omitted or nil, it defaults to the selected frame. */)
384 (Lisp_Object frame)
386 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
389 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
390 Swindow_minibuffer_p, 0, 1, 0,
391 doc: /* Return non-nil if WINDOW is a minibuffer window.
392 WINDOW must be a valid window and defaults to the selected one. */)
393 (Lisp_Object window)
395 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
398 /* Don't move this to window.el - this must be a safe routine. */
399 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
400 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
401 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
402 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
403 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
404 the first window of that frame. */)
405 (Lisp_Object frame_or_window)
407 Lisp_Object window;
409 if (NILP (frame_or_window))
410 window = SELECTED_FRAME ()->root_window;
411 else if (WINDOW_VALID_P (frame_or_window))
412 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
413 else
415 CHECK_LIVE_FRAME (frame_or_window);
416 window = XFRAME (frame_or_window)->root_window;
419 while (WINDOWP (XWINDOW (window)->contents))
420 window = XWINDOW (window)->contents;
422 return window;
425 DEFUN ("frame-selected-window", Fframe_selected_window,
426 Sframe_selected_window, 0, 1, 0,
427 doc: /* Return the selected window of FRAME-OR-WINDOW.
428 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
429 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
430 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
431 return the selected window of that frame. */)
432 (Lisp_Object frame_or_window)
434 Lisp_Object window;
436 if (NILP (frame_or_window))
437 window = SELECTED_FRAME ()->selected_window;
438 else if (WINDOW_VALID_P (frame_or_window))
439 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
440 else
442 CHECK_LIVE_FRAME (frame_or_window);
443 window = XFRAME (frame_or_window)->selected_window;
446 return window;
449 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
450 Sset_frame_selected_window, 2, 3, 0,
451 doc: /* Set selected window of FRAME to WINDOW.
452 FRAME must be a live frame and defaults to the selected one. If FRAME
453 is the selected frame, this makes WINDOW the selected window. Optional
454 argument NORECORD non-nil means to neither change the order of recently
455 selected windows nor the buffer list. WINDOW must denote a live window.
456 Return WINDOW. */)
457 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
459 if (NILP (frame))
460 frame = selected_frame;
462 CHECK_LIVE_FRAME (frame);
463 CHECK_LIVE_WINDOW (window);
465 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
466 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
468 if (EQ (frame, selected_frame))
469 return Fselect_window (window, norecord);
470 else
472 fset_selected_window (XFRAME (frame), window);
473 return window;
477 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
478 doc: /* Return the selected window.
479 The selected window is the window in which the standard cursor for
480 selected windows appears and to which many commands apply. */)
481 (void)
483 return selected_window;
486 int window_select_count;
488 /* If select_window is called with inhibit_point_swap non-zero it will
489 not store point of the old selected window's buffer back into that
490 window's pointm slot. This is needed by Fset_window_configuration to
491 avoid that the display routine is called with selected_window set to
492 Qnil causing a subsequent crash. */
493 static Lisp_Object
494 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
496 register struct window *w;
497 struct frame *sf;
499 CHECK_LIVE_WINDOW (window);
501 w = XWINDOW (window);
503 /* Make the selected window's buffer current. */
504 Fset_buffer (w->contents);
506 if (EQ (window, selected_window) && !inhibit_point_swap)
507 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
508 way to call record_buffer from Elisp, so it's important that we call
509 record_buffer before returning here. */
510 goto record_and_return;
512 if (NILP (norecord))
513 { /* Mark the window for redisplay since the selected-window has
514 a different mode-line. */
515 wset_redisplay (XWINDOW (selected_window));
516 wset_redisplay (w);
518 else
519 redisplay_other_windows ();
521 sf = SELECTED_FRAME ();
522 if (XFRAME (WINDOW_FRAME (w)) != sf)
524 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
525 /* Use this rather than Fhandle_switch_frame
526 so that FRAME_FOCUS_FRAME is moved appropriately as we
527 move around in the state where a minibuffer in a separate
528 frame is active. */
529 Fselect_frame (WINDOW_FRAME (w), norecord);
530 /* Fselect_frame called us back so we've done all the work already. */
531 eassert (EQ (window, selected_window));
532 return window;
534 else
535 fset_selected_window (sf, window);
537 select_window_1 (window, inhibit_point_swap);
538 bset_last_selected_window (XBUFFER (w->contents), window);
540 record_and_return:
541 /* record_buffer can run QUIT, so make sure it is run only after we have
542 re-established the invariant between selected_window and selected_frame,
543 otherwise the temporary broken invariant might "escape" (bug#14161). */
544 if (NILP (norecord))
546 w->use_time = ++window_select_count;
547 record_buffer (w->contents);
550 return window;
553 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
554 (not even for redisplay's benefit), and assume that the window's frame is
555 already selected. */
556 static void
557 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
559 /* Store the old selected window's buffer's point in pointm of the old
560 selected window. It belongs to that window, and when the window is
561 not selected, must be in the window. */
562 if (!inhibit_point_swap)
564 struct window *ow = XWINDOW (selected_window);
565 if (BUFFERP (ow->contents))
566 set_marker_both (ow->pointm, ow->contents,
567 BUF_PT (XBUFFER (ow->contents)),
568 BUF_PT_BYTE (XBUFFER (ow->contents)));
571 selected_window = window;
573 /* Go to the point recorded in the window.
574 This is important when the buffer is in more
575 than one window. It also matters when
576 redisplay_window has altered point after scrolling,
577 because it makes the change only in the window. */
578 set_point_from_marker (XWINDOW (window)->pointm);
581 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
582 doc: /* Select WINDOW which must be a live window.
583 Also make WINDOW's frame the selected frame and WINDOW that frame's
584 selected window. In addition, make WINDOW's buffer current and set its
585 buffer's value of `point' to the value of WINDOW's `window-point'.
586 Return WINDOW.
588 Optional second arg NORECORD non-nil means do not put this buffer at the
589 front of the buffer list and do not make this window the most recently
590 selected one.
592 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
593 applications and internal routines often select a window temporarily for
594 various purposes; mostly, to simplify coding. As a rule, such
595 selections should be not recorded and therefore will not pollute
596 `buffer-list-update-hook'. Selections that "really count" are those
597 causing a visible change in the next redisplay of WINDOW's frame and
598 should be always recorded. So if you think of running a function each
599 time a window gets selected put it on `buffer-list-update-hook'.
601 Also note that the main editor command loop sets the current buffer to
602 the buffer of the selected window before each command. */)
603 (register Lisp_Object window, Lisp_Object norecord)
605 return select_window (window, norecord, 0);
608 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
609 doc: /* Return the buffer displayed in window WINDOW.
610 If WINDOW is omitted or nil, it defaults to the selected window.
611 Return nil for an internal window or a deleted window. */)
612 (Lisp_Object window)
614 struct window *w = decode_any_window (window);
615 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
618 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
619 doc: /* Return the parent window of window WINDOW.
620 WINDOW must be a valid window and defaults to the selected one.
621 Return nil for a window with no parent (e.g. a root window). */)
622 (Lisp_Object window)
624 return decode_valid_window (window)->parent;
627 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
628 doc: /* Return the topmost child window of window WINDOW.
629 WINDOW must be a valid window and defaults to the selected one.
630 Return nil if WINDOW is a live window (live windows have no children).
631 Return nil if WINDOW is an internal window whose children form a
632 horizontal combination. */)
633 (Lisp_Object window)
635 struct window *w = decode_valid_window (window);
636 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
639 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
640 doc: /* Return the leftmost child window of window WINDOW.
641 WINDOW must be a valid window and defaults to the selected one.
642 Return nil if WINDOW is a live window (live windows have no children).
643 Return nil if WINDOW is an internal window whose children form a
644 vertical combination. */)
645 (Lisp_Object window)
647 struct window *w = decode_valid_window (window);
648 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
651 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
652 doc: /* Return the next sibling window of window WINDOW.
653 WINDOW must be a valid window and defaults to the selected one.
654 Return nil if WINDOW has no next sibling. */)
655 (Lisp_Object window)
657 return decode_valid_window (window)->next;
660 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
661 doc: /* Return the previous sibling window of window WINDOW.
662 WINDOW must be a valid window and defaults to the selected one.
663 Return nil if WINDOW has no previous sibling. */)
664 (Lisp_Object window)
666 return decode_valid_window (window)->prev;
669 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
670 doc: /* Return combination limit of window WINDOW.
671 WINDOW must be a valid window used in horizontal or vertical combination.
672 If the return value is nil, child windows of WINDOW can be recombined with
673 WINDOW's siblings. A return value of t means that child windows of
674 WINDOW are never \(re-)combined with WINDOW's siblings. */)
675 (Lisp_Object window)
677 struct window *w;
679 CHECK_VALID_WINDOW (window);
680 w = XWINDOW (window);
681 if (WINDOW_LEAF_P (w))
682 error ("Combination limit is meaningful for internal windows only");
683 return w->combination_limit;
686 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
687 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
688 WINDOW must be a valid window used in horizontal or vertical combination.
689 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
690 siblings. LIMIT t means that child windows of WINDOW are never
691 \(re-)combined with WINDOW's siblings. Other values are reserved for
692 future use. */)
693 (Lisp_Object window, Lisp_Object limit)
695 struct window *w;
697 CHECK_VALID_WINDOW (window);
698 w = XWINDOW (window);
699 if (WINDOW_LEAF_P (w))
700 error ("Combination limit is meaningful for internal windows only");
701 wset_combination_limit (w, limit);
702 return limit;
705 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
706 doc: /* Return the use time of window WINDOW.
707 WINDOW must be a live window and defaults to the selected one.
708 The window with the highest use time is the most recently selected
709 one. The window with the lowest use time is the least recently
710 selected one. */)
711 (Lisp_Object window)
713 return make_number (decode_live_window (window)->use_time);
716 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
717 doc: /* Return the width of window WINDOW in pixels.
718 WINDOW must be a valid window and defaults to the selected one.
720 The return value includes the fringes and margins of WINDOW as well as
721 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
722 an internal window, its pixel width is the width of the screen areas
723 spanned by its children. */)
724 (Lisp_Object window)
726 return make_number (decode_valid_window (window)->pixel_width);
729 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
730 doc: /* Return the height of window WINDOW in pixels.
731 WINDOW must be a valid window and defaults to the selected one.
733 The return value includes the mode line and header line and the bottom
734 divider, if any. If WINDOW is an internal window, its pixel height is
735 the height of the screen areas spanned by its children. */)
736 (Lisp_Object window)
738 return make_number (decode_valid_window (window)->pixel_height);
741 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
742 doc: /* Return the height of window WINDOW in lines.
743 WINDOW must be a valid window and defaults to the selected one.
745 The return value includes the heights of WINDOW's mode and header line
746 and its bottom divider, if any. If WINDOW is an internal window, the
747 total height is the height of the screen areas spanned by its children.
749 If WINDOW's pixel height is not an integral multiple of its frame's
750 character height, the number of lines occupied by WINDOW is rounded
751 internally. This is done in a way such that, if WINDOW is a parent
752 window, the sum of the total heights of all its children internally
753 equals the total height of WINDOW.
755 If the optional argument ROUND is `ceiling', return the smallest integer
756 larger than WINDOW's pixel height divided by the character height of
757 WINDOW's frame. ROUND `floor' means to return the largest integer
758 smaller than WINDOW's pixel height divided by the character height of
759 WINDOW's frame. Any other value of ROUND means to return the internal
760 total height of WINDOW. */)
761 (Lisp_Object window, Lisp_Object round)
763 struct window *w = decode_valid_window (window);
765 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
766 return make_number (w->total_lines);
767 else
769 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
771 return make_number (EQ (round, Qceiling)
772 ? ((w->pixel_height + unit - 1) /unit)
773 : (w->pixel_height / unit));
777 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
778 doc: /* Return the total width of window WINDOW in columns.
779 WINDOW must be a valid window and defaults to the selected one.
781 The return value includes the widths of WINDOW's fringes, margins,
782 scroll bars and its right divider, if any. If WINDOW is an internal
783 window, the total width is the width of the screen areas spanned by its
784 children.
786 If WINDOW's pixel width is not an integral multiple of its frame's
787 character width, the number of lines occupied by WINDOW is rounded
788 internally. This is done in a way such that, if WINDOW is a parent
789 window, the sum of the total widths of all its children internally
790 equals the total width of WINDOW.
792 If the optional argument ROUND is `ceiling', return the smallest integer
793 larger than WINDOW's pixel width divided by the character width of
794 WINDOW's frame. ROUND `floor' means to return the largest integer
795 smaller than WINDOW's pixel width divided by the character width of
796 WINDOW's frame. Any other value of ROUND means to return the internal
797 total width of WINDOW. */)
798 (Lisp_Object window, Lisp_Object round)
800 struct window *w = decode_valid_window (window);
802 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
803 return make_number (w->total_cols);
804 else
806 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
808 return make_number (EQ (round, Qceiling)
809 ? ((w->pixel_width + unit - 1) /unit)
810 : (w->pixel_width / unit));
814 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
815 doc: /* Return the new total size of window WINDOW.
816 WINDOW must be a valid window and defaults to the selected one.
818 The new total size of WINDOW is the value set by the last call of
819 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
820 installed as WINDOW's total height (see `window-total-height') or total
821 width (see `window-total-width'). */)
822 (Lisp_Object window)
824 return decode_valid_window (window)->new_total;
827 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
828 doc: /* Return the normal height of window WINDOW.
829 WINDOW must be a valid window and defaults to the selected one.
830 If HORIZONTAL is non-nil, return the normal width of WINDOW.
832 The normal height of a frame's root window or a window that is
833 horizontally combined (a window that has a left or right sibling) is
834 1.0. The normal height of a window that is vertically combined (has a
835 sibling above or below) is the fraction of the window's height with
836 respect to its parent. The sum of the normal heights of all windows in a
837 vertical combination equals 1.0.
839 Similarly, the normal width of a frame's root window or a window that is
840 vertically combined equals 1.0. The normal width of a window that is
841 horizontally combined is the fraction of the window's width with respect
842 to its parent. The sum of the normal widths of all windows in a
843 horizontal combination equals 1.0.
845 The normal sizes of windows are used to restore the proportional sizes
846 of windows after they have been shrunk to their minimum sizes; for
847 example when a frame is temporarily made very small and afterwards gets
848 re-enlarged to its previous size. */)
849 (Lisp_Object window, Lisp_Object horizontal)
851 struct window *w = decode_valid_window (window);
853 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
856 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
857 doc: /* Return new normal size of window WINDOW.
858 WINDOW must be a valid window and defaults to the selected one.
860 The new normal size of WINDOW is the value set by the last call of
861 `set-window-new-normal' for WINDOW. If valid, it will be shortly
862 installed as WINDOW's normal size (see `window-normal-size'). */)
863 (Lisp_Object window)
865 return decode_valid_window (window)->new_normal;
868 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
869 doc: /* Return new pixel size of window WINDOW.
870 WINDOW must be a valid window and defaults to the selected one.
872 The new pixel size of WINDOW is the value set by the last call of
873 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
874 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
875 width (see `window-pixel-width'). */)
876 (Lisp_Object window)
878 return decode_valid_window (window)->new_pixel;
881 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
882 doc: /* Return left pixel edge of window WINDOW.
883 WINDOW must be a valid window and defaults to the selected one. */)
884 (Lisp_Object window)
886 return make_number (decode_valid_window (window)->pixel_left);
889 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
890 doc: /* Return top pixel edge of window WINDOW.
891 WINDOW must be a valid window and defaults to the selected one. */)
892 (Lisp_Object window)
894 return make_number (decode_valid_window (window)->pixel_top);
897 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
898 doc: /* Return left column of window WINDOW.
899 This is the distance, in columns, between the left edge of WINDOW and
900 the left edge of the frame's window area. For instance, the return
901 value is 0 if there is no window to the left of WINDOW.
903 WINDOW must be a valid window and defaults to the selected one. */)
904 (Lisp_Object window)
906 return make_number (decode_valid_window (window)->left_col);
909 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
910 doc: /* Return top line of window WINDOW.
911 This is the distance, in lines, between the top of WINDOW and the top
912 of the frame's window area. For instance, the return value is 0 if
913 there is no window above WINDOW.
915 WINDOW must be a valid window and defaults to the selected one. */)
916 (Lisp_Object window)
918 return make_number (decode_valid_window (window)->top_line);
921 /* Return the number of lines/pixels of W's body. Don't count any mode
922 or header line or horizontal divider of W. Rounds down to nearest
923 integer when not working pixelwise. */
924 static int
925 window_body_height (struct window *w, bool pixelwise)
927 int height = (w->pixel_height
928 - WINDOW_HEADER_LINE_HEIGHT (w)
929 - (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
930 ? WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
931 : 0)
932 - WINDOW_MODE_LINE_HEIGHT (w)
933 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
935 /* Don't return a negative value. */
936 return max (pixelwise
937 ? height
938 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
942 /* Return the number of columns/pixels of W's body. Don't count columns
943 occupied by the scroll bar or the divider/vertical bar separating W
944 from its right sibling or margins. On window-systems don't count
945 fringes either. Round down to nearest integer when not working
946 pixelwise. */
948 window_body_width (struct window *w, bool pixelwise)
950 struct frame *f = XFRAME (WINDOW_FRAME (w));
952 int width = (w->pixel_width
953 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
954 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
955 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
956 : ((!FRAME_WINDOW_P (f)
957 && !WINDOW_RIGHTMOST_P (w)
958 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
959 /* A vertical bar is either 1 or 0. */
960 ? 1 : 0))
961 - WINDOW_MARGINS_WIDTH (w)
962 - (FRAME_WINDOW_P (f)
963 ? WINDOW_FRINGES_WIDTH (w)
964 : 0));
966 /* Don't return a negative value. */
967 return max (pixelwise
968 ? width
969 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
973 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
974 doc: /* Return the height of WINDOW's text area.
975 WINDOW must be a live window and defaults to the selected one. Optional
976 argument PIXELWISE non-nil means return the height of WINDOW's text area
977 in pixels. The return value does not include the mode line or header
978 line or any horizontal divider.
980 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
981 pixel height divided by the character height of WINDOW's frame. This
982 means that if a line at the bottom of the text area is only partially
983 visible, that line is not counted. */)
984 (Lisp_Object window, Lisp_Object pixelwise)
986 return make_number (window_body_height (decode_live_window (window),
987 NILP (pixelwise) ? 0 : 1));
990 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
991 doc: /* Return the width of WINDOW's text area.
992 WINDOW must be a live window and defaults to the selected one. Optional
993 argument PIXELWISE non-nil means return the width in pixels. The return
994 value does not include any vertical dividers, fringes or marginal areas,
995 or scroll bars.
997 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
998 pixel width divided by the character width of WINDOW's frame. This
999 means that if a column at the right of the text area is only partially
1000 visible, that column is not counted. */)
1001 (Lisp_Object window, Lisp_Object pixelwise)
1003 return make_number (window_body_width (decode_live_window (window),
1004 NILP (pixelwise) ? 0 : 1));
1007 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
1008 Swindow_mode_line_height, 0, 1, 0,
1009 doc: /* Return the height in pixels of WINDOW's mode-line.
1010 WINDOW must be a live window and defaults to the selected one. */)
1011 (Lisp_Object window)
1013 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
1016 DEFUN ("window-header-line-height", Fwindow_header_line_height,
1017 Swindow_header_line_height, 0, 1, 0,
1018 doc: /* Return the height in pixels of WINDOW's header-line.
1019 WINDOW must be a live window and defaults to the selected one. */)
1020 (Lisp_Object window)
1022 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1025 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1026 Swindow_right_divider_width, 0, 1, 0,
1027 doc: /* Return the width in pixels of WINDOW's right divider.
1028 WINDOW must be a live window and defaults to the selected one. */)
1029 (Lisp_Object window)
1031 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1034 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1035 Swindow_bottom_divider_width, 0, 1, 0,
1036 doc: /* Return the width in pixels of WINDOW's bottom divider.
1037 WINDOW must be a live window and defaults to the selected one. */)
1038 (Lisp_Object window)
1040 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1043 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1044 Swindow_scroll_bar_width, 0, 1, 0,
1045 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1046 WINDOW must be a live window and defaults to the selected one. */)
1047 (Lisp_Object window)
1049 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1052 DEFUN ("window-scroll-bar-height", Fwindow_scroll_bar_height,
1053 Swindow_scroll_bar_height, 0, 1, 0,
1054 doc: /* Return the height in pixels of WINDOW's horizontal scrollbar.
1055 WINDOW must be a live window and defaults to the selected one. */)
1056 (Lisp_Object window)
1058 return (make_number (WINDOW_SCROLL_BAR_AREA_HEIGHT (decode_live_window (window))));
1061 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1062 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1063 WINDOW must be a live window and defaults to the selected one. */)
1064 (Lisp_Object window)
1066 return make_number (decode_live_window (window)->hscroll);
1069 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1070 range, returning the new amount as a fixnum. */
1071 static Lisp_Object
1072 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1074 /* Horizontal scrolling has problems with large scroll amounts.
1075 It's too slow with long lines, and even with small lines the
1076 display can be messed up. For now, though, impose only the limits
1077 required by the internal representation: horizontal scrolling must
1078 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1079 (since it's stored in a ptrdiff_t). */
1080 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1081 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1083 /* Prevent redisplay shortcuts when changing the hscroll. */
1084 if (w->hscroll != new_hscroll)
1085 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1087 w->hscroll = new_hscroll;
1088 w->suspend_auto_hscroll = 1;
1090 return make_number (new_hscroll);
1093 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1094 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1095 WINDOW must be a live window and defaults to the selected one.
1096 Clip the number to a reasonable value if out of range.
1097 Return the new number. NCOL should be zero or positive.
1099 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1100 window so that the location of point moves off-window. */)
1101 (Lisp_Object window, Lisp_Object ncol)
1103 CHECK_NUMBER (ncol);
1104 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1107 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1108 Swindow_redisplay_end_trigger, 0, 1, 0,
1109 doc: /* Return WINDOW's redisplay end trigger value.
1110 WINDOW must be a live window and defaults to the selected one.
1111 See `set-window-redisplay-end-trigger' for more information. */)
1112 (Lisp_Object window)
1114 return decode_live_window (window)->redisplay_end_trigger;
1117 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1118 Sset_window_redisplay_end_trigger, 2, 2, 0,
1119 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1120 WINDOW must be a live window and defaults to the selected one. VALUE
1121 should be a buffer position (typically a marker) or nil. If it is a
1122 buffer position, then if redisplay in WINDOW reaches a position beyond
1123 VALUE, the functions in `redisplay-end-trigger-functions' are called
1124 with two arguments: WINDOW, and the end trigger value. Afterwards the
1125 end-trigger value is reset to nil. */)
1126 (register Lisp_Object window, Lisp_Object value)
1128 wset_redisplay_end_trigger (decode_live_window (window), value);
1129 return value;
1132 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1133 doc: /* Return a list of the edge coordinates of WINDOW.
1134 WINDOW must be a valid window and defaults to the selected one.
1136 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1137 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1138 0 at top left corner of frame.
1140 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1141 is one more than the bottommost row occupied by WINDOW. The edges
1142 include the space used by WINDOW's scroll bar, display margins, fringes,
1143 header line, and/or mode line. For the edges of just the text area, use
1144 `window-inside-edges'. */)
1145 (Lisp_Object window)
1147 register struct window *w = decode_valid_window (window);
1149 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1150 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1153 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1154 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1155 WINDOW must be a valid window and defaults to the selected one.
1157 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1158 0, 0 at the top left corner of the frame.
1160 RIGHT is one more than the rightmost x position occupied by WINDOW.
1161 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1162 The pixel edges include the space used by WINDOW's scroll bar, display
1163 margins, fringes, header line, and/or mode line. For the pixel edges
1164 of just the text area, use `window-inside-pixel-edges'. */)
1165 (Lisp_Object window)
1167 register struct window *w = decode_valid_window (window);
1169 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1170 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1173 static void
1174 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1176 struct frame *f = XFRAME (w->frame);
1177 *add_y = f->top_pos;
1178 #ifdef FRAME_MENUBAR_HEIGHT
1179 *add_y += FRAME_MENUBAR_HEIGHT (f);
1180 #endif
1181 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1182 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1183 #elif defined (FRAME_TOOLBAR_HEIGHT)
1184 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1185 #endif
1186 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1187 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1188 #endif
1189 *add_x = f->left_pos;
1190 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1191 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1192 #endif
1195 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1196 Swindow_absolute_pixel_edges, 0, 1, 0,
1197 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1198 WINDOW must be a valid window and defaults to the selected one.
1200 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1201 0, 0 at the top left corner of the display.
1203 RIGHT is one more than the rightmost x position occupied by WINDOW.
1204 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1205 The pixel edges include the space used by WINDOW's scroll bar, display
1206 margins, fringes, header line, and/or mode line. For the pixel edges
1207 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1208 (Lisp_Object window)
1210 register struct window *w = decode_valid_window (window);
1211 int add_x, add_y;
1213 calc_absolute_offset (w, &add_x, &add_y);
1215 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1216 WINDOW_TOP_EDGE_Y (w) + add_y,
1217 WINDOW_RIGHT_EDGE_X (w) + add_x,
1218 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1221 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1222 doc: /* Return a list of the edge coordinates of WINDOW.
1223 WINDOW must be a live window and defaults to the selected one.
1225 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1226 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1227 0 at top left corner of frame.
1229 RIGHT is one more than the rightmost column of WINDOW's text area.
1230 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1231 inside edges do not include the space used by the WINDOW's scroll bar,
1232 display margins, fringes, header line, and/or mode line. */)
1233 (Lisp_Object window)
1235 register struct window *w = decode_live_window (window);
1237 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1238 + WINDOW_LEFT_MARGIN_COLS (w)
1239 + ((WINDOW_LEFT_FRINGE_WIDTH (w)
1240 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1241 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1242 (WINDOW_TOP_EDGE_LINE (w)
1243 + WINDOW_HEADER_LINE_LINES (w)),
1244 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1245 - WINDOW_RIGHT_MARGIN_COLS (w)
1246 - ((WINDOW_RIGHT_FRINGE_WIDTH (w)
1247 + WINDOW_FRAME_COLUMN_WIDTH (w) - 1)
1248 / WINDOW_FRAME_COLUMN_WIDTH (w))),
1249 (WINDOW_BOTTOM_EDGE_LINE (w)
1250 - WINDOW_MODE_LINE_LINES (w)));
1253 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1254 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1255 WINDOW must be a live window and defaults to the selected one.
1257 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1258 (0,0) at the top left corner of the frame's window area.
1260 RIGHT is one more than the rightmost x position of WINDOW's text area.
1261 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1262 The inside edges do not include the space used by WINDOW's scroll bar,
1263 display margins, fringes, header line, and/or mode line. */)
1264 (Lisp_Object window)
1266 register struct window *w = decode_live_window (window);
1268 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1269 + WINDOW_LEFT_MARGIN_WIDTH (w)
1270 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1271 (WINDOW_TOP_EDGE_Y (w)
1272 + WINDOW_HEADER_LINE_HEIGHT (w)),
1273 (WINDOW_BOX_RIGHT_EDGE_X (w)
1274 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1275 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1276 (WINDOW_BOTTOM_EDGE_Y (w)
1277 - WINDOW_MODE_LINE_HEIGHT (w)));
1280 DEFUN ("window-inside-absolute-pixel-edges",
1281 Fwindow_inside_absolute_pixel_edges,
1282 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1283 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1284 WINDOW must be a live window and defaults to the selected one.
1286 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1287 (0,0) at the top left corner of the frame's window area.
1289 RIGHT is one more than the rightmost x position of WINDOW's text area.
1290 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1291 The inside edges do not include the space used by WINDOW's scroll bar,
1292 display margins, fringes, header line, and/or mode line. */)
1293 (Lisp_Object window)
1295 register struct window *w = decode_live_window (window);
1296 int add_x, add_y;
1298 calc_absolute_offset (w, &add_x, &add_y);
1300 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1301 + WINDOW_LEFT_MARGIN_WIDTH (w)
1302 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1303 (WINDOW_TOP_EDGE_Y (w)
1304 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1305 (WINDOW_BOX_RIGHT_EDGE_X (w)
1306 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1307 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1308 (WINDOW_BOTTOM_EDGE_Y (w)
1309 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1312 /* Test if the character at column X, row Y is within window W.
1313 If it is not, return ON_NOTHING;
1314 if it is on the window's vertical divider, return
1315 ON_RIGHT_DIVIDER;
1316 if it is on the window's horizontal divider, return
1317 ON_BOTTOM_DIVIDER;
1318 if it is in the window's text area, return ON_TEXT;
1319 if it is on the window's modeline, return ON_MODE_LINE;
1320 if it is on the border between the window and its right sibling,
1321 return ON_VERTICAL_BORDER;
1322 if it is on a scroll bar, return ON_SCROLL_BAR;
1323 if it is on the window's top line, return ON_HEADER_LINE;
1324 if it is in left or right fringe of the window,
1325 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1326 if it is in the marginal area to the left/right of the window,
1327 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1329 X and Y are frame relative pixel coordinates. */
1331 static enum window_part
1332 coordinates_in_window (register struct window *w, int x, int y)
1334 struct frame *f = XFRAME (WINDOW_FRAME (w));
1335 enum window_part part;
1336 int ux = FRAME_COLUMN_WIDTH (f);
1337 int left_x = WINDOW_LEFT_EDGE_X (w);
1338 int right_x = WINDOW_RIGHT_EDGE_X (w);
1339 int top_y = WINDOW_TOP_EDGE_Y (w);
1340 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1341 /* The width of the area where the vertical line can be dragged.
1342 (Between mode lines for instance. */
1343 int grabbable_width = ux;
1344 int lmargin_width, rmargin_width, text_left, text_right;
1346 /* Outside any interesting row or column? */
1347 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1348 return ON_NOTHING;
1350 /* On the horizontal window divider (which prevails the vertical
1351 divider)? */
1352 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1353 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1354 && y <= bottom_y)
1355 return ON_BOTTOM_DIVIDER;
1356 /* On vertical window divider? */
1357 else if (!WINDOW_RIGHTMOST_P (w)
1358 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1359 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1360 && x <= right_x)
1361 return ON_RIGHT_DIVIDER;
1362 /* On the horizontal scroll bar? (Including the empty space at its
1363 right!) */
1364 else if ((WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w)
1365 && y >= (bottom_y
1366 - WINDOW_SCROLL_BAR_AREA_HEIGHT (w)
1367 - CURRENT_MODE_LINE_HEIGHT (w)
1368 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1369 && y <= (bottom_y
1370 - CURRENT_MODE_LINE_HEIGHT (w)
1371 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))))
1372 return ON_HORIZONTAL_SCROLL_BAR;
1373 /* On the mode or header line? */
1374 else if ((WINDOW_WANTS_MODELINE_P (w)
1375 && y >= (bottom_y
1376 - CURRENT_MODE_LINE_HEIGHT (w)
1377 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1378 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1379 && (part = ON_MODE_LINE))
1380 || (WINDOW_WANTS_HEADER_LINE_P (w)
1381 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1382 && (part = ON_HEADER_LINE)))
1384 /* If it's under/over the scroll bar portion of the mode/header
1385 line, say it's on the vertical line. That's to be able to
1386 resize windows horizontally in case we're using toolkit scroll
1387 bars. Note: If scrollbars are on the left, the window that
1388 must be eventually resized is that on the left of WINDOW. */
1389 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1390 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1391 && !WINDOW_LEFTMOST_P (w)
1392 && eabs (x - left_x) < grabbable_width)
1393 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1394 && !WINDOW_RIGHTMOST_P (w)
1395 && eabs (x - right_x) < grabbable_width)))
1396 return ON_VERTICAL_BORDER;
1397 else
1398 return part;
1401 /* In what's below, we subtract 1 when computing right_x because we
1402 want the rightmost pixel, which is given by left_pixel+width-1. */
1403 if (w->pseudo_window_p)
1405 left_x = 0;
1406 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1408 else
1410 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1411 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1414 /* Outside any interesting column? */
1415 if (x < left_x || x > right_x)
1416 return ON_VERTICAL_SCROLL_BAR;
1418 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1419 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1421 text_left = window_box_left (w, TEXT_AREA);
1422 text_right = text_left + window_box_width (w, TEXT_AREA);
1424 if (FRAME_WINDOW_P (f))
1426 if (!w->pseudo_window_p
1427 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1428 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1429 && !WINDOW_RIGHTMOST_P (w)
1430 && (eabs (x - right_x) < grabbable_width))
1431 return ON_VERTICAL_BORDER;
1433 /* Need to say "x > right_x" rather than >=, since on character
1434 terminals, the vertical line's x coordinate is right_x. */
1435 else if (!w->pseudo_window_p
1436 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1437 && !WINDOW_RIGHTMOST_P (w)
1438 /* Why check ux if we are not the rightmost window? Also
1439 shouldn't a pseudo window always be rightmost? */
1440 && x > right_x - ux)
1441 return ON_VERTICAL_BORDER;
1443 if (x < text_left)
1445 if (lmargin_width > 0
1446 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1447 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1448 : (x < left_x + lmargin_width)))
1449 return ON_LEFT_MARGIN;
1450 else
1451 return ON_LEFT_FRINGE;
1454 if (x >= text_right)
1456 if (rmargin_width > 0
1457 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1458 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1459 : (x >= right_x - rmargin_width)))
1460 return ON_RIGHT_MARGIN;
1461 else
1462 return ON_RIGHT_FRINGE;
1465 /* Everything special ruled out - must be on text area */
1466 return ON_TEXT;
1469 /* Take X is the frame-relative pixel x-coordinate, and return the
1470 x-coordinate relative to part PART of window W. */
1472 window_relative_x_coord (struct window *w, enum window_part part, int x)
1474 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1476 switch (part)
1478 case ON_TEXT:
1479 return x - window_box_left (w, TEXT_AREA);
1481 case ON_HEADER_LINE:
1482 case ON_MODE_LINE:
1483 case ON_LEFT_FRINGE:
1484 return x - left_x;
1486 case ON_RIGHT_FRINGE:
1487 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1489 case ON_LEFT_MARGIN:
1490 return (x - left_x
1491 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1492 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1494 case ON_RIGHT_MARGIN:
1495 return (x + 1
1496 - ((w->pseudo_window_p)
1497 ? WINDOW_PIXEL_WIDTH (w)
1498 : WINDOW_BOX_RIGHT_EDGE_X (w))
1499 + window_box_width (w, RIGHT_MARGIN_AREA)
1500 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1501 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1504 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1505 return 0;
1509 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1510 Scoordinates_in_window_p, 2, 2, 0,
1511 doc: /* Return non-nil if COORDINATES are in WINDOW.
1512 WINDOW must be a live window and defaults to the selected one.
1513 COORDINATES is a cons of the form (X . Y), X and Y being distances
1514 measured in characters from the upper-left corner of the frame.
1515 \(0 . 0) denotes the character in the upper left corner of the
1516 frame.
1517 If COORDINATES are in the text portion of WINDOW,
1518 the coordinates relative to the window are returned.
1519 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1520 If they are in the right divider of WINDOW, `right-divider' is returned.
1521 If they are in the mode line of WINDOW, `mode-line' is returned.
1522 If they are in the header line of WINDOW, `header-line' is returned.
1523 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1524 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1525 If they are on the border between WINDOW and its right sibling,
1526 `vertical-line' is returned.
1527 If they are in the windows's left or right marginal areas, `left-margin'\n\
1528 or `right-margin' is returned. */)
1529 (register Lisp_Object coordinates, Lisp_Object window)
1531 struct window *w;
1532 struct frame *f;
1533 int x, y;
1534 Lisp_Object lx, ly;
1536 w = decode_live_window (window);
1537 f = XFRAME (w->frame);
1538 CHECK_CONS (coordinates);
1539 lx = Fcar (coordinates);
1540 ly = Fcdr (coordinates);
1541 CHECK_NUMBER_OR_FLOAT (lx);
1542 CHECK_NUMBER_OR_FLOAT (ly);
1543 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1544 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1546 switch (coordinates_in_window (w, x, y))
1548 case ON_NOTHING:
1549 return Qnil;
1551 case ON_TEXT:
1552 /* Convert X and Y to window relative pixel coordinates, and
1553 return the canonical char units. */
1554 x -= window_box_left (w, TEXT_AREA);
1555 y -= WINDOW_TOP_EDGE_Y (w);
1556 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1557 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1559 case ON_MODE_LINE:
1560 return Qmode_line;
1562 case ON_VERTICAL_BORDER:
1563 return Qvertical_line;
1565 case ON_HEADER_LINE:
1566 return Qheader_line;
1568 case ON_LEFT_FRINGE:
1569 return Qleft_fringe;
1571 case ON_RIGHT_FRINGE:
1572 return Qright_fringe;
1574 case ON_LEFT_MARGIN:
1575 return Qleft_margin;
1577 case ON_RIGHT_MARGIN:
1578 return Qright_margin;
1580 case ON_VERTICAL_SCROLL_BAR:
1581 /* Historically we are supposed to return nil in this case. */
1582 return Qnil;
1584 case ON_HORIZONTAL_SCROLL_BAR:
1585 return Qnil;
1587 case ON_RIGHT_DIVIDER:
1588 return Qright_divider;
1590 case ON_BOTTOM_DIVIDER:
1591 return Qbottom_divider;
1593 default:
1594 emacs_abort ();
1599 /* Callback for foreach_window, used in window_from_coordinates.
1600 Check if window W contains coordinates specified by USER_DATA which
1601 is actually a pointer to a struct check_window_data CW.
1603 Check if window W contains coordinates *CW->x and *CW->y. If it
1604 does, return W in *CW->window, as Lisp_Object, and return in
1605 *CW->part the part of the window under coordinates *X,*Y. Return
1606 zero from this function to stop iterating over windows. */
1608 struct check_window_data
1610 Lisp_Object *window;
1611 int x, y;
1612 enum window_part *part;
1615 static int
1616 check_window_containing (struct window *w, void *user_data)
1618 struct check_window_data *cw = user_data;
1619 enum window_part found;
1620 int continue_p = 1;
1622 found = coordinates_in_window (w, cw->x, cw->y);
1623 if (found != ON_NOTHING)
1625 *cw->part = found;
1626 XSETWINDOW (*cw->window, w);
1627 continue_p = 0;
1630 return continue_p;
1634 /* Find the window containing frame-relative pixel position X/Y and
1635 return it as a Lisp_Object.
1637 If X, Y is on one of the window's special `window_part' elements,
1638 set *PART to the id of that element.
1640 If there is no window under X, Y return nil and leave *PART
1641 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1643 This function was previously implemented with a loop cycling over
1644 windows with Fnext_window, and starting with the frame's selected
1645 window. It turned out that this doesn't work with an
1646 implementation of next_window using Vwindow_list, because
1647 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1648 tree of F when this function is called asynchronously from
1649 note_mouse_highlight. The original loop didn't terminate in this
1650 case. */
1652 Lisp_Object
1653 window_from_coordinates (struct frame *f, int x, int y,
1654 enum window_part *part, bool tool_bar_p)
1656 Lisp_Object window;
1657 struct check_window_data cw;
1658 enum window_part dummy;
1660 if (part == 0)
1661 part = &dummy;
1663 window = Qnil;
1664 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1665 foreach_window (f, check_window_containing, &cw);
1667 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1668 /* If not found above, see if it's in the tool bar window, if a tool
1669 bar exists. */
1670 if (NILP (window)
1671 && tool_bar_p
1672 && WINDOWP (f->tool_bar_window)
1673 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1674 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1675 != ON_NOTHING))
1677 *part = ON_TEXT;
1678 window = f->tool_bar_window;
1680 #endif
1682 return window;
1685 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1686 doc: /* Return window containing coordinates X and Y on FRAME.
1687 FRAME must be a live frame and defaults to the selected one.
1688 The top left corner of the frame is considered to be row 0,
1689 column 0. */)
1690 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1692 struct frame *f = decode_live_frame (frame);
1694 /* Check that arguments are integers or floats. */
1695 CHECK_NUMBER_OR_FLOAT (x);
1696 CHECK_NUMBER_OR_FLOAT (y);
1698 return window_from_coordinates (f,
1699 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1700 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1701 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1702 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1703 0, 0);
1706 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1707 doc: /* Return current value of point in WINDOW.
1708 WINDOW must be a live window and defaults to the selected one.
1710 For a nonselected window, this is the value point would have if that
1711 window were selected.
1713 Note that, when WINDOW is selected, the value returned is the same as
1714 that returned by `point' for WINDOW's buffer. It would be more strictly
1715 correct to return the top-level value of `point', outside of any
1716 `save-excursion' forms. But that is hard to define. */)
1717 (Lisp_Object window)
1719 register struct window *w = decode_live_window (window);
1721 if (w == XWINDOW (selected_window))
1722 return make_number (BUF_PT (XBUFFER (w->contents)));
1723 else
1724 return Fmarker_position (w->pointm);
1727 DEFUN ("window-old-point", Fwindow_old_point, Swindow_old_point, 0, 1, 0,
1728 doc: /* Return old value of point in WINDOW.
1729 WINDOW must be a live window and defaults to the selected one. */)
1730 (Lisp_Object window)
1732 return Fmarker_position (decode_live_window (window)->old_pointm);
1735 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1736 doc: /* Return position at which display currently starts in WINDOW.
1737 WINDOW must be a live window and defaults to the selected one.
1738 This is updated by redisplay or by calling `set-window-start'. */)
1739 (Lisp_Object window)
1741 return Fmarker_position (decode_live_window (window)->start);
1744 /* This is text temporarily removed from the doc string below.
1746 This function returns nil if the position is not currently known.
1747 That happens when redisplay is preempted and doesn't finish.
1748 If in that case you want to compute where the end of the window would
1749 have been if redisplay had finished, do this:
1750 (save-excursion
1751 (goto-char (window-start window))
1752 (vertical-motion (1- (window-height window)) window)
1753 (point))") */
1755 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1756 doc: /* Return position at which display currently ends in WINDOW.
1757 WINDOW must be a live window and defaults to the selected one.
1758 This is updated by redisplay, when it runs to completion.
1759 Simply changing the buffer text or setting `window-start'
1760 does not update this value.
1761 Return nil if there is no recorded value. (This can happen if the
1762 last redisplay of WINDOW was preempted, and did not finish.)
1763 If UPDATE is non-nil, compute the up-to-date position
1764 if it isn't already recorded. */)
1765 (Lisp_Object window, Lisp_Object update)
1767 Lisp_Object value;
1768 struct window *w = decode_live_window (window);
1769 Lisp_Object buf;
1770 struct buffer *b;
1772 buf = w->contents;
1773 CHECK_BUFFER (buf);
1774 b = XBUFFER (buf);
1776 if (! NILP (update)
1777 && (windows_or_buffers_changed
1778 || !w->window_end_valid
1779 || b->clip_changed
1780 || b->prevent_redisplay_optimizations_p
1781 || window_outdated (w))
1782 && !noninteractive)
1784 struct text_pos startp;
1785 struct it it;
1786 struct buffer *old_buffer = NULL;
1787 void *itdata = NULL;
1789 /* Cannot use Fvertical_motion because that function doesn't
1790 cope with variable-height lines. */
1791 if (b != current_buffer)
1793 old_buffer = current_buffer;
1794 set_buffer_internal (b);
1797 /* In case W->start is out of the range, use something
1798 reasonable. This situation occurred when loading a file with
1799 `-l' containing a call to `rmail' with subsequent other
1800 commands. At the end, W->start happened to be BEG, while
1801 rmail had already narrowed the buffer. */
1802 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1804 itdata = bidi_shelve_cache ();
1805 start_display (&it, w, startp);
1806 move_it_vertically (&it, window_box_height (w));
1807 if (it.current_y < it.last_visible_y)
1808 move_it_past_eol (&it);
1809 value = make_number (IT_CHARPOS (it));
1810 bidi_unshelve_cache (itdata, 0);
1812 if (old_buffer)
1813 set_buffer_internal (old_buffer);
1815 else
1816 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1818 return value;
1821 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1822 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1823 WINDOW must be a live window and defaults to the selected one.
1824 Return POS. */)
1825 (Lisp_Object window, Lisp_Object pos)
1827 register struct window *w = decode_live_window (window);
1829 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1831 if (w == XWINDOW (selected_window))
1833 if (XBUFFER (w->contents) == current_buffer)
1834 Fgoto_char (pos);
1835 else
1837 struct buffer *old_buffer = current_buffer;
1839 /* ... but here we want to catch type error before buffer change. */
1840 CHECK_NUMBER_COERCE_MARKER (pos);
1841 set_buffer_internal (XBUFFER (w->contents));
1842 Fgoto_char (pos);
1843 set_buffer_internal (old_buffer);
1846 else
1848 set_marker_restricted (w->pointm, pos, w->contents);
1849 /* We have to make sure that redisplay updates the window to show
1850 the new value of point. */
1851 wset_redisplay (w);
1854 return pos;
1857 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1858 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1859 WINDOW must be a live window and defaults to the selected one. Return
1860 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1861 overriding motion of point in order to display at this exact start. */)
1862 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1864 register struct window *w = decode_live_window (window);
1866 set_marker_restricted (w->start, pos, w->contents);
1867 /* This is not right, but much easier than doing what is right. */
1868 w->start_at_line_beg = 0;
1869 if (NILP (noforce))
1870 w->force_start = 1;
1871 w->update_mode_line = 1;
1872 /* Bug#15957. */
1873 w->window_end_valid = 0;
1874 wset_redisplay (w);
1876 return pos;
1879 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1880 Spos_visible_in_window_p, 0, 3, 0,
1881 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1882 WINDOW must be a live window and defaults to the selected one.
1884 Return nil if that position is scrolled vertically out of view. If a
1885 character is only partially visible, nil is returned, unless the
1886 optional argument PARTIALLY is non-nil. If POS is only out of view
1887 because of horizontal scrolling, return non-nil. If POS is t, it
1888 specifies the position of the last visible glyph in WINDOW. POS
1889 defaults to point in WINDOW; WINDOW defaults to the selected window.
1891 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1892 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1893 where X and Y are the pixel coordinates relative to the top left corner
1894 of the window. The remaining elements are omitted if the character after
1895 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1896 off-window at the top and bottom of the screen line ("row") containing
1897 POS, ROWH is the visible height of that row, and VPOS is the row number
1898 \(zero-based). */)
1899 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1901 register struct window *w;
1902 register EMACS_INT posint;
1903 register struct buffer *buf;
1904 struct text_pos top;
1905 Lisp_Object in_window = Qnil;
1906 int rtop, rbot, rowh, vpos, fully_p = 1;
1907 int x, y;
1909 w = decode_live_window (window);
1910 buf = XBUFFER (w->contents);
1911 SET_TEXT_POS_FROM_MARKER (top, w->start);
1913 if (EQ (pos, Qt))
1914 posint = -1;
1915 else if (!NILP (pos))
1917 CHECK_NUMBER_COERCE_MARKER (pos);
1918 posint = XINT (pos);
1920 else if (w == XWINDOW (selected_window))
1921 posint = PT;
1922 else
1923 posint = marker_position (w->pointm);
1925 /* If position is above window start or outside buffer boundaries,
1926 or if window start is out of range, position is not visible. */
1927 if ((EQ (pos, Qt)
1928 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1929 && CHARPOS (top) >= BUF_BEGV (buf)
1930 && CHARPOS (top) <= BUF_ZV (buf)
1931 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1932 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1933 in_window = Qt;
1935 if (!NILP (in_window) && !NILP (partially))
1937 Lisp_Object part = Qnil;
1938 if (!fully_p)
1939 part = list4i (rtop, rbot, rowh, vpos);
1940 in_window = Fcons (make_number (x),
1941 Fcons (make_number (y), part));
1944 return in_window;
1947 DEFUN ("window-line-height", Fwindow_line_height,
1948 Swindow_line_height, 0, 2, 0,
1949 doc: /* Return height in pixels of text line LINE in window WINDOW.
1950 WINDOW must be a live window and defaults to the selected one.
1952 Return height of current line if LINE is omitted or nil. Return height of
1953 header or mode line if LINE is `header-line' or `mode-line'.
1954 Otherwise, LINE is a text line number starting from 0. A negative number
1955 counts from the end of the window.
1957 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1958 in pixels of the visible part of the line, VPOS and YPOS are the
1959 vertical position in lines and pixels of the line, relative to the top
1960 of the first text line, and OFFBOT is the number of off-window pixels at
1961 the bottom of the text line. If there are off-window pixels at the top
1962 of the (first) text line, YPOS is negative.
1964 Return nil if window display is not up-to-date. In that case, use
1965 `pos-visible-in-window-p' to obtain the information. */)
1966 (Lisp_Object line, Lisp_Object window)
1968 register struct window *w;
1969 register struct buffer *b;
1970 struct glyph_row *row, *end_row;
1971 int max_y, crop, i;
1972 EMACS_INT n;
1974 w = decode_live_window (window);
1976 if (noninteractive || w->pseudo_window_p)
1977 return Qnil;
1979 CHECK_BUFFER (w->contents);
1980 b = XBUFFER (w->contents);
1982 /* Fail if current matrix is not up-to-date. */
1983 if (!w->window_end_valid
1984 || windows_or_buffers_changed
1985 || b->clip_changed
1986 || b->prevent_redisplay_optimizations_p
1987 || window_outdated (w))
1988 return Qnil;
1990 if (NILP (line))
1992 i = w->cursor.vpos;
1993 if (i < 0 || i >= w->current_matrix->nrows
1994 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1995 return Qnil;
1996 max_y = window_text_bottom_y (w);
1997 goto found_row;
2000 if (EQ (line, Qheader_line))
2002 if (!WINDOW_WANTS_HEADER_LINE_P (w))
2003 return Qnil;
2004 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
2005 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
2008 if (EQ (line, Qmode_line))
2010 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
2011 return (row->enabled_p ?
2012 list4i (row->height,
2013 0, /* not accurate */
2014 (WINDOW_HEADER_LINE_HEIGHT (w)
2015 + window_text_bottom_y (w)),
2017 : Qnil);
2020 CHECK_NUMBER (line);
2021 n = XINT (line);
2023 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2024 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2025 max_y = window_text_bottom_y (w);
2026 i = 0;
2028 while ((n < 0 || i < n)
2029 && row <= end_row && row->enabled_p
2030 && row->y + row->height < max_y)
2031 row++, i++;
2033 if (row > end_row || !row->enabled_p)
2034 return Qnil;
2036 if (++n < 0)
2038 if (-n > i)
2039 return Qnil;
2040 row += n;
2041 i += n;
2044 found_row:
2045 crop = max (0, (row->y + row->height) - max_y);
2046 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
2049 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
2050 0, 1, 0,
2051 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
2052 More precisely, return the value assigned by the last call of
2053 `set-window-dedicated-p' for WINDOW. Return nil if that function was
2054 never called with WINDOW as its argument, or the value set by that
2055 function was internally reset since its last call. WINDOW must be a
2056 live window and defaults to the selected one.
2058 When a window is dedicated to its buffer, `display-buffer' will refrain
2059 from displaying another buffer in it. `get-lru-window' and
2060 `get-largest-window' treat dedicated windows specially.
2061 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2062 `kill-buffer' can delete a dedicated window and the containing frame.
2064 Functions like `set-window-buffer' may change the buffer displayed by a
2065 window, unless that window is "strongly" dedicated to its buffer, that
2066 is the value returned by `window-dedicated-p' is t. */)
2067 (Lisp_Object window)
2069 return decode_live_window (window)->dedicated;
2072 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2073 Sset_window_dedicated_p, 2, 2, 0,
2074 doc: /* Mark WINDOW as dedicated according to FLAG.
2075 WINDOW must be a live window and defaults to the selected one. FLAG
2076 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2077 mark WINDOW as non-dedicated. Return FLAG.
2079 When a window is dedicated to its buffer, `display-buffer' will refrain
2080 from displaying another buffer in it. `get-lru-window' and
2081 `get-largest-window' treat dedicated windows specially.
2082 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2083 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2084 and the containing frame.
2086 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2087 its buffer. Functions like `set-window-buffer' may change the buffer
2088 displayed by a window, unless that window is strongly dedicated to its
2089 buffer. If and when `set-window-buffer' displays another buffer in a
2090 window, it also makes sure that the window is no more dedicated. */)
2091 (Lisp_Object window, Lisp_Object flag)
2093 wset_dedicated (decode_live_window (window), flag);
2094 return flag;
2097 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2098 0, 1, 0,
2099 doc: /* Return buffers previously shown in WINDOW.
2100 WINDOW must be a live window and defaults to the selected one.
2102 The return value is a list of elements (BUFFER WINDOW-START POS),
2103 where BUFFER is a buffer, WINDOW-START is the start position of the
2104 window for that buffer, and POS is a window-specific point value. */)
2105 (Lisp_Object window)
2107 return decode_live_window (window)->prev_buffers;
2110 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2111 Sset_window_prev_buffers, 2, 2, 0,
2112 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2113 WINDOW must be a live window and defaults to the selected one.
2115 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2116 where BUFFER is a buffer, WINDOW-START is the start position of the
2117 window for that buffer, and POS is a window-specific point value. */)
2118 (Lisp_Object window, Lisp_Object prev_buffers)
2120 wset_prev_buffers (decode_live_window (window), prev_buffers);
2121 return prev_buffers;
2124 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2125 0, 1, 0,
2126 doc: /* Return list of buffers recently re-shown in WINDOW.
2127 WINDOW must be a live window and defaults to the selected one. */)
2128 (Lisp_Object window)
2130 return decode_live_window (window)->next_buffers;
2133 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2134 Sset_window_next_buffers, 2, 2, 0,
2135 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2136 WINDOW must be a live window and defaults to the selected one.
2137 NEXT-BUFFERS should be a list of buffers. */)
2138 (Lisp_Object window, Lisp_Object next_buffers)
2140 wset_next_buffers (decode_live_window (window), next_buffers);
2141 return next_buffers;
2144 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2145 0, 1, 0,
2146 doc: /* Return the parameters of WINDOW and their values.
2147 WINDOW must be a valid window and defaults to the selected one. The
2148 return value is a list of elements of the form (PARAMETER . VALUE). */)
2149 (Lisp_Object window)
2151 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2154 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2155 2, 2, 0,
2156 doc: /* Return WINDOW's value for PARAMETER.
2157 WINDOW can be any window and defaults to the selected one. */)
2158 (Lisp_Object window, Lisp_Object parameter)
2160 Lisp_Object result;
2162 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2163 return CDR_SAFE (result);
2166 DEFUN ("set-window-parameter", Fset_window_parameter,
2167 Sset_window_parameter, 3, 3, 0,
2168 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2169 WINDOW can be any window and defaults to the selected one.
2170 Return VALUE. */)
2171 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2173 register struct window *w = decode_any_window (window);
2174 Lisp_Object old_alist_elt;
2176 old_alist_elt = Fassq (parameter, w->window_parameters);
2177 if (NILP (old_alist_elt))
2178 wset_window_parameters
2179 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2180 else
2181 Fsetcdr (old_alist_elt, value);
2182 return value;
2185 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2186 0, 1, 0,
2187 doc: /* Return the display-table that WINDOW is using.
2188 WINDOW must be a live window and defaults to the selected one. */)
2189 (Lisp_Object window)
2191 return decode_live_window (window)->display_table;
2194 /* Get the display table for use on window W. This is either W's
2195 display table or W's buffer's display table. Ignore the specified
2196 tables if they are not valid; if no valid table is specified,
2197 return 0. */
2199 struct Lisp_Char_Table *
2200 window_display_table (struct window *w)
2202 struct Lisp_Char_Table *dp = NULL;
2204 if (DISP_TABLE_P (w->display_table))
2205 dp = XCHAR_TABLE (w->display_table);
2206 else if (BUFFERP (w->contents))
2208 struct buffer *b = XBUFFER (w->contents);
2210 if (DISP_TABLE_P (BVAR (b, display_table)))
2211 dp = XCHAR_TABLE (BVAR (b, display_table));
2212 else if (DISP_TABLE_P (Vstandard_display_table))
2213 dp = XCHAR_TABLE (Vstandard_display_table);
2216 return dp;
2219 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2220 doc: /* Set WINDOW's display-table to TABLE.
2221 WINDOW must be a live window and defaults to the selected one. */)
2222 (register Lisp_Object window, Lisp_Object table)
2224 wset_display_table (decode_live_window (window), table);
2225 return table;
2228 /* Record info on buffer window W is displaying
2229 when it is about to cease to display that buffer. */
2230 static void
2231 unshow_buffer (register struct window *w)
2233 Lisp_Object buf = w->contents;
2234 struct buffer *b = XBUFFER (buf);
2236 eassert (b == XMARKER (w->pointm)->buffer);
2238 #if 0
2239 if (w == XWINDOW (selected_window)
2240 || ! EQ (buf, XWINDOW (selected_window)->contents))
2241 /* Do this except when the selected window's buffer
2242 is being removed from some other window. */
2243 #endif
2244 /* last_window_start records the start position that this buffer
2245 had in the last window to be disconnected from it.
2246 Now that this statement is unconditional,
2247 it is possible for the buffer to be displayed in the
2248 selected window, while last_window_start reflects another
2249 window which was recently showing the same buffer.
2250 Some people might say that might be a good thing. Let's see. */
2251 b->last_window_start = marker_position (w->start);
2253 /* Point in the selected window's buffer
2254 is actually stored in that buffer, and the window's pointm isn't used.
2255 So don't clobber point in that buffer. */
2256 if (! EQ (buf, XWINDOW (selected_window)->contents)
2257 /* Don't clobber point in current buffer either (this could be
2258 useful in connection with bug#12208).
2259 && XBUFFER (buf) != current_buffer */
2260 /* This line helps to fix Horsley's testbug.el bug. */
2261 && !(WINDOWP (BVAR (b, last_selected_window))
2262 && w != XWINDOW (BVAR (b, last_selected_window))
2263 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2264 temp_set_point_both (b,
2265 clip_to_bounds (BUF_BEGV (b),
2266 marker_position (w->pointm),
2267 BUF_ZV (b)),
2268 clip_to_bounds (BUF_BEGV_BYTE (b),
2269 marker_byte_position (w->pointm),
2270 BUF_ZV_BYTE (b)));
2272 if (WINDOWP (BVAR (b, last_selected_window))
2273 && w == XWINDOW (BVAR (b, last_selected_window)))
2274 bset_last_selected_window (b, Qnil);
2277 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2278 means change window structure only. Otherwise store geometry and
2279 other settings as well. */
2280 static void
2281 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2283 register Lisp_Object tem;
2284 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2286 /* If OLD is its frame's root window, then NEW is the new
2287 root window for that frame. */
2288 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2289 fset_root_window (XFRAME (o->frame), new);
2291 if (setflag)
2293 n->pixel_left = o->pixel_left;
2294 n->pixel_top = o->pixel_top;
2295 n->pixel_width = o->pixel_width;
2296 n->pixel_height = o->pixel_height;
2297 n->left_col = o->left_col;
2298 n->top_line = o->top_line;
2299 n->total_cols = o->total_cols;
2300 n->total_lines = o->total_lines;
2301 wset_normal_cols (n, o->normal_cols);
2302 wset_normal_cols (o, make_float (1.0));
2303 wset_normal_lines (n, o->normal_lines);
2304 wset_normal_lines (o, make_float (1.0));
2305 n->desired_matrix = n->current_matrix = 0;
2306 n->vscroll = 0;
2307 memset (&n->cursor, 0, sizeof (n->cursor));
2308 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2309 n->last_cursor_vpos = 0;
2310 #ifdef HAVE_WINDOW_SYSTEM
2311 n->phys_cursor_type = NO_CURSOR;
2312 n->phys_cursor_width = -1;
2313 #endif
2314 n->must_be_updated_p = 0;
2315 n->pseudo_window_p = 0;
2316 n->window_end_vpos = 0;
2317 n->window_end_pos = 0;
2318 n->window_end_valid = 0;
2321 tem = o->next;
2322 wset_next (n, tem);
2323 if (!NILP (tem))
2324 wset_prev (XWINDOW (tem), new);
2326 tem = o->prev;
2327 wset_prev (n, tem);
2328 if (!NILP (tem))
2329 wset_next (XWINDOW (tem), new);
2331 tem = o->parent;
2332 wset_parent (n, tem);
2333 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2334 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2337 /* If window WINDOW and its parent window are iso-combined, merge
2338 WINDOW's children into those of its parent window and mark WINDOW as
2339 deleted. */
2341 static void
2342 recombine_windows (Lisp_Object window)
2344 struct window *w, *p, *c;
2345 Lisp_Object parent, child;
2346 bool horflag;
2348 w = XWINDOW (window);
2349 parent = w->parent;
2350 if (!NILP (parent) && NILP (w->combination_limit))
2352 p = XWINDOW (parent);
2353 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2354 && p->horizontal == w->horizontal)
2355 /* WINDOW and PARENT are both either a vertical or a horizontal
2356 combination. */
2358 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2359 child = w->contents;
2360 c = XWINDOW (child);
2362 /* Splice WINDOW's children into its parent's children and
2363 assign new normal sizes. */
2364 if (NILP (w->prev))
2365 wset_combination (p, horflag, child);
2366 else
2368 wset_prev (c, w->prev);
2369 wset_next (XWINDOW (w->prev), child);
2372 while (c)
2374 wset_parent (c, parent);
2376 if (horflag)
2377 wset_normal_cols
2378 (c, make_float ((double) c->pixel_width
2379 / (double) p->pixel_width));
2380 else
2381 wset_normal_lines
2382 (c, make_float ((double) c->pixel_height
2383 / (double) p->pixel_height));
2385 if (NILP (c->next))
2387 if (!NILP (w->next))
2389 wset_next (c, w->next);
2390 wset_prev (XWINDOW (c->next), child);
2393 c = 0;
2395 else
2397 child = c->next;
2398 c = XWINDOW (child);
2402 /* WINDOW can be deleted now. */
2403 wset_combination (w, 0, Qnil);
2408 /* If WINDOW can be deleted, delete it. */
2409 static void
2410 delete_deletable_window (Lisp_Object window)
2412 if (!NILP (call1 (Qwindow_deletable_p, window)))
2413 call1 (Qdelete_window, window);
2416 /***********************************************************************
2417 Window List
2418 ***********************************************************************/
2420 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2421 pointer. This is a callback function for foreach_window, used in
2422 the window_list function. */
2424 static int
2425 add_window_to_list (struct window *w, void *user_data)
2427 Lisp_Object *list = user_data;
2428 Lisp_Object window;
2429 XSETWINDOW (window, w);
2430 *list = Fcons (window, *list);
2431 return 1;
2435 /* Return a list of all windows, for use by next_window. If
2436 Vwindow_list is a list, return that list. Otherwise, build a new
2437 list, cache it in Vwindow_list, and return that. */
2439 Lisp_Object
2440 window_list (void)
2442 if (!CONSP (Vwindow_list))
2444 Lisp_Object tail, frame;
2446 Vwindow_list = Qnil;
2447 FOR_EACH_FRAME (tail, frame)
2449 Lisp_Object args[2];
2451 /* We are visiting windows in canonical order, and add
2452 new windows at the front of args[1], which means we
2453 have to reverse this list at the end. */
2454 args[1] = Qnil;
2455 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2456 args[0] = Vwindow_list;
2457 args[1] = Fnreverse (args[1]);
2458 Vwindow_list = Fnconc (2, args);
2462 return Vwindow_list;
2466 /* Value is non-zero if WINDOW satisfies the constraints given by
2467 OWINDOW, MINIBUF and ALL_FRAMES.
2469 MINIBUF t means WINDOW may be minibuffer windows.
2470 `lambda' means WINDOW may not be a minibuffer window.
2471 a window means a specific minibuffer window
2473 ALL_FRAMES t means search all frames,
2474 nil means search just current frame,
2475 `visible' means search just visible frames on the
2476 current terminal,
2477 0 means search visible and iconified frames on the
2478 current terminal,
2479 a window means search the frame that window belongs to,
2480 a frame means consider windows on that frame, only. */
2482 static bool
2483 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2484 Lisp_Object minibuf, Lisp_Object all_frames)
2486 struct window *w = XWINDOW (window);
2487 struct frame *f = XFRAME (w->frame);
2488 bool candidate_p = 1;
2490 if (!BUFFERP (w->contents))
2491 candidate_p = 0;
2492 else if (MINI_WINDOW_P (w)
2493 && (EQ (minibuf, Qlambda)
2494 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2496 /* If MINIBUF is `lambda' don't consider any mini-windows.
2497 If it is a window, consider only that one. */
2498 candidate_p = 0;
2500 else if (EQ (all_frames, Qt))
2501 candidate_p = 1;
2502 else if (NILP (all_frames))
2504 eassert (WINDOWP (owindow));
2505 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2507 else if (EQ (all_frames, Qvisible))
2509 candidate_p = FRAME_VISIBLE_P (f)
2510 && (FRAME_TERMINAL (XFRAME (w->frame))
2511 == FRAME_TERMINAL (XFRAME (selected_frame)));
2514 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2516 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2517 #ifdef HAVE_X_WINDOWS
2518 /* Yuck!! If we've just created the frame and the
2519 window-manager requested the user to place it
2520 manually, the window may still not be considered
2521 `visible'. I'd argue it should be at least
2522 something like `iconified', but don't know how to do
2523 that yet. --Stef */
2524 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2525 && !f->output_data.x->has_been_visible)
2526 #endif
2528 && (FRAME_TERMINAL (XFRAME (w->frame))
2529 == FRAME_TERMINAL (XFRAME (selected_frame)));
2531 else if (WINDOWP (all_frames))
2532 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2533 || EQ (XWINDOW (all_frames)->frame, w->frame)
2534 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2535 else if (FRAMEP (all_frames))
2536 candidate_p = EQ (all_frames, w->frame);
2538 return candidate_p;
2542 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2543 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2544 MINIBUF, and ALL_FRAMES. */
2546 static void
2547 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2549 struct window *w = decode_live_window (*window);
2551 XSETWINDOW (*window, w);
2552 /* MINIBUF nil may or may not include minibuffers. Decide if it
2553 does. */
2554 if (NILP (*minibuf))
2555 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2556 else if (!EQ (*minibuf, Qt))
2557 *minibuf = Qlambda;
2559 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2560 => count none of them, or a specific minibuffer window (the
2561 active one) to count. */
2563 /* ALL_FRAMES nil doesn't specify which frames to include. */
2564 if (NILP (*all_frames))
2565 *all_frames
2566 = (!EQ (*minibuf, Qlambda)
2567 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2568 : Qnil);
2569 else if (EQ (*all_frames, Qvisible))
2571 else if (EQ (*all_frames, make_number (0)))
2573 else if (FRAMEP (*all_frames))
2575 else if (!EQ (*all_frames, Qt))
2576 *all_frames = Qnil;
2580 /* Return the next or previous window of WINDOW in cyclic ordering
2581 of windows. NEXT_P non-zero means return the next window. See the
2582 documentation string of next-window for the meaning of MINIBUF and
2583 ALL_FRAMES. */
2585 static Lisp_Object
2586 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2588 decode_next_window_args (&window, &minibuf, &all_frames);
2590 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2591 return the first window on the frame. */
2592 if (FRAMEP (all_frames)
2593 && !EQ (all_frames, XWINDOW (window)->frame))
2594 return Fframe_first_window (all_frames);
2596 if (next_p)
2598 Lisp_Object list;
2600 /* Find WINDOW in the list of all windows. */
2601 list = Fmemq (window, window_list ());
2603 /* Scan forward from WINDOW to the end of the window list. */
2604 if (CONSP (list))
2605 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2606 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2607 break;
2609 /* Scan from the start of the window list up to WINDOW. */
2610 if (!CONSP (list))
2611 for (list = Vwindow_list;
2612 CONSP (list) && !EQ (XCAR (list), window);
2613 list = XCDR (list))
2614 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2615 break;
2617 if (CONSP (list))
2618 window = XCAR (list);
2620 else
2622 Lisp_Object candidate, list;
2624 /* Scan through the list of windows for candidates. If there are
2625 candidate windows in front of WINDOW, the last one of these
2626 is the one we want. If there are candidates following WINDOW
2627 in the list, again the last one of these is the one we want. */
2628 candidate = Qnil;
2629 for (list = window_list (); CONSP (list); list = XCDR (list))
2631 if (EQ (XCAR (list), window))
2633 if (WINDOWP (candidate))
2634 break;
2636 else if (candidate_window_p (XCAR (list), window, minibuf,
2637 all_frames))
2638 candidate = XCAR (list);
2641 if (WINDOWP (candidate))
2642 window = candidate;
2645 return window;
2649 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2650 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2651 WINDOW must be a live window and defaults to the selected one. The
2652 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2653 consider.
2655 MINIBUF nil or omitted means consider the minibuffer window only if the
2656 minibuffer is active. MINIBUF t means consider the minibuffer window
2657 even if the minibuffer is not active. Any other value means do not
2658 consider the minibuffer window even if the minibuffer is active.
2660 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2661 plus the minibuffer window if specified by the MINIBUF argument. If the
2662 minibuffer counts, consider all windows on all frames that share that
2663 minibuffer too. The following non-nil values of ALL-FRAMES have special
2664 meanings:
2666 - t means consider all windows on all existing frames.
2668 - `visible' means consider all windows on all visible frames.
2670 - 0 (the number zero) means consider all windows on all visible and
2671 iconified frames.
2673 - A frame means consider all windows on that frame only.
2675 Anything else means consider all windows on WINDOW's frame and no
2676 others.
2678 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2679 `next-window' to iterate through the entire cycle of acceptable
2680 windows, eventually ending up back at the window you started with.
2681 `previous-window' traverses the same cycle, in the reverse order. */)
2682 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2684 return next_window (window, minibuf, all_frames, 1);
2688 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2689 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2690 WINDOW must be a live window and defaults to the selected one. The
2691 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2692 consider.
2694 MINIBUF nil or omitted means consider the minibuffer window only if the
2695 minibuffer is active. MINIBUF t means consider the minibuffer window
2696 even if the minibuffer is not active. Any other value means do not
2697 consider the minibuffer window even if the minibuffer is active.
2699 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2700 plus the minibuffer window if specified by the MINIBUF argument. If the
2701 minibuffer counts, consider all windows on all frames that share that
2702 minibuffer too. The following non-nil values of ALL-FRAMES have special
2703 meanings:
2705 - t means consider all windows on all existing frames.
2707 - `visible' means consider all windows on all visible frames.
2709 - 0 (the number zero) means consider all windows on all visible and
2710 iconified frames.
2712 - A frame means consider all windows on that frame only.
2714 Anything else means consider all windows on WINDOW's frame and no
2715 others.
2717 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2718 use `previous-window' to iterate through the entire cycle of
2719 acceptable windows, eventually ending up back at the window you
2720 started with. `next-window' traverses the same cycle, in the
2721 reverse order. */)
2722 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2724 return next_window (window, minibuf, all_frames, 0);
2728 /* Return a list of windows in cyclic ordering. Arguments are like
2729 for `next-window'. */
2731 static Lisp_Object
2732 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2734 Lisp_Object tail, list, rest;
2736 decode_next_window_args (&window, &minibuf, &all_frames);
2737 list = Qnil;
2739 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2740 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2741 list = Fcons (XCAR (tail), list);
2743 /* Rotate the list to start with WINDOW. */
2744 list = Fnreverse (list);
2745 rest = Fmemq (window, list);
2746 if (!NILP (rest) && !EQ (rest, list))
2748 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2750 XSETCDR (tail, Qnil);
2751 list = nconc2 (rest, list);
2753 return list;
2757 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2758 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2759 FRAME nil or omitted means use the selected frame.
2760 WINDOW nil or omitted means use the window selected within FRAME.
2761 MINIBUF t means include the minibuffer window, even if it isn't active.
2762 MINIBUF nil or omitted means include the minibuffer window only
2763 if it's active.
2764 MINIBUF neither nil nor t means never include the minibuffer window. */)
2765 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2767 if (NILP (window))
2768 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2769 CHECK_WINDOW (window);
2770 if (NILP (frame))
2771 frame = selected_frame;
2773 if (!EQ (frame, XWINDOW (window)->frame))
2774 error ("Window is on a different frame");
2776 return window_list_1 (window, minibuf, frame);
2780 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2781 doc: /* Return a list of all live windows.
2782 WINDOW specifies the first window to list and defaults to the selected
2783 window.
2785 Optional argument MINIBUF nil or omitted means consider the minibuffer
2786 window only if the minibuffer is active. MINIBUF t means consider the
2787 minibuffer window even if the minibuffer is not active. Any other value
2788 means do not consider the minibuffer window even if the minibuffer is
2789 active.
2791 Optional argument ALL-FRAMES nil or omitted means consider all windows
2792 on WINDOW's frame, plus the minibuffer window if specified by the
2793 MINIBUF argument. If the minibuffer counts, consider all windows on all
2794 frames that share that minibuffer too. The following non-nil values of
2795 ALL-FRAMES have special meanings:
2797 - t means consider all windows on all existing frames.
2799 - `visible' means consider all windows on all visible frames.
2801 - 0 (the number zero) means consider all windows on all visible and
2802 iconified frames.
2804 - A frame means consider all windows on that frame only.
2806 Anything else means consider all windows on WINDOW's frame and no
2807 others.
2809 If WINDOW is not on the list of windows returned, some other window will
2810 be listed first but no error is signaled. */)
2811 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2813 return window_list_1 (window, minibuf, all_frames);
2816 /* Look at all windows, performing an operation specified by TYPE
2817 with argument OBJ.
2818 If FRAMES is Qt, look at all frames;
2819 Qnil, look at just the selected frame;
2820 Qvisible, look at visible frames;
2821 a frame, just look at windows on that frame.
2822 If MINI is non-zero, perform the operation on minibuffer windows too. */
2824 enum window_loop
2826 WINDOW_LOOP_UNUSED,
2827 GET_BUFFER_WINDOW, /* Arg is buffer */
2828 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2829 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2830 CHECK_ALL_WINDOWS /* Arg is ignored */
2833 static Lisp_Object
2834 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2836 Lisp_Object window, windows, best_window, frame_arg;
2837 int frame_best_window_flag = 0;
2838 struct frame *f;
2839 struct gcpro gcpro1;
2841 /* If we're only looping through windows on a particular frame,
2842 frame points to that frame. If we're looping through windows
2843 on all frames, frame is 0. */
2844 if (FRAMEP (frames))
2845 f = XFRAME (frames);
2846 else if (NILP (frames))
2847 f = SELECTED_FRAME ();
2848 else
2849 f = NULL;
2851 if (f)
2852 frame_arg = Qlambda;
2853 else if (EQ (frames, make_number (0)))
2854 frame_arg = frames;
2855 else if (EQ (frames, Qvisible))
2856 frame_arg = frames;
2857 else
2858 frame_arg = Qt;
2860 /* frame_arg is Qlambda to stick to one frame,
2861 Qvisible to consider all visible frames,
2862 or Qt otherwise. */
2864 /* Pick a window to start with. */
2865 if (WINDOWP (obj))
2866 window = obj;
2867 else if (f)
2868 window = FRAME_SELECTED_WINDOW (f);
2869 else
2870 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2872 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2873 GCPRO1 (windows);
2874 best_window = Qnil;
2876 for (; CONSP (windows); windows = XCDR (windows))
2878 struct window *w;
2880 window = XCAR (windows);
2881 w = XWINDOW (window);
2883 /* Note that we do not pay attention here to whether the frame
2884 is visible, since Fwindow_list skips non-visible frames if
2885 that is desired, under the control of frame_arg. */
2886 if (!MINI_WINDOW_P (w)
2887 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2888 consider all windows. */
2889 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2890 || (mini && minibuf_level > 0))
2891 switch (type)
2893 case GET_BUFFER_WINDOW:
2894 if (EQ (w->contents, obj)
2895 /* Don't find any minibuffer window except the one that
2896 is currently in use. */
2897 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2899 if (EQ (window, selected_window))
2900 /* Preferably return the selected window. */
2901 RETURN_UNGCPRO (window);
2902 else if (EQ (XWINDOW (window)->frame, selected_frame)
2903 && !frame_best_window_flag)
2904 /* Prefer windows on the current frame (but don't
2905 choose another one if we have one already). */
2907 best_window = window;
2908 frame_best_window_flag = 1;
2910 else if (NILP (best_window))
2911 best_window = window;
2913 break;
2915 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2916 /* We could simply check whether the buffer shown by window
2917 is live, and show another buffer in case it isn't. */
2918 if (EQ (w->contents, obj))
2920 /* Undedicate WINDOW. */
2921 wset_dedicated (w, Qnil);
2922 /* Make WINDOW show the buffer returned by
2923 other_buffer_safely, don't run any hooks. */
2924 set_window_buffer
2925 (window, other_buffer_safely (w->contents), 0, 0);
2926 /* If WINDOW is the selected window, make its buffer
2927 current. But do so only if the window shows the
2928 current buffer (Bug#6454). */
2929 if (EQ (window, selected_window)
2930 && XBUFFER (w->contents) == current_buffer)
2931 Fset_buffer (w->contents);
2933 break;
2935 case REDISPLAY_BUFFER_WINDOWS:
2936 if (EQ (w->contents, obj))
2938 mark_window_display_accurate (window, 0);
2939 w->update_mode_line = 1;
2940 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2941 update_mode_lines = 27;
2942 best_window = window;
2944 break;
2946 /* Check for a leaf window that has a killed buffer
2947 or broken markers. */
2948 case CHECK_ALL_WINDOWS:
2949 if (BUFFERP (w->contents))
2951 struct buffer *b = XBUFFER (w->contents);
2953 if (!BUFFER_LIVE_P (b))
2954 emacs_abort ();
2955 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2956 emacs_abort ();
2957 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2958 emacs_abort ();
2960 break;
2962 case WINDOW_LOOP_UNUSED:
2963 break;
2967 UNGCPRO;
2968 return best_window;
2971 /* Used for debugging. Abort if any window has a dead buffer. */
2973 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2974 void
2975 check_all_windows (void)
2977 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2980 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2981 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2982 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2983 the current buffer.
2985 The optional argument ALL-FRAMES specifies the frames to consider:
2987 - t means consider all windows on all existing frames.
2989 - `visible' means consider all windows on all visible frames.
2991 - 0 (the number zero) means consider all windows on all visible
2992 and iconified frames.
2994 - A frame means consider all windows on that frame only.
2996 Any other value of ALL-FRAMES means consider all windows on the
2997 selected frame and no others. */)
2998 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
3000 Lisp_Object buffer;
3002 if (NILP (buffer_or_name))
3003 buffer = Fcurrent_buffer ();
3004 else
3005 buffer = Fget_buffer (buffer_or_name);
3007 if (BUFFERP (buffer))
3008 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
3009 else
3010 return Qnil;
3014 static Lisp_Object
3015 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
3017 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
3021 Lisp_Object
3022 sanitize_window_sizes (Lisp_Object frame, Lisp_Object horizontal)
3024 return call2 (Qwindow_sanitize_window_sizes, frame, horizontal);
3028 static Lisp_Object
3029 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
3031 return call2 (Qwindow_pixel_to_total, frame, horizontal);
3035 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
3036 Sdelete_other_windows_internal, 0, 2, "",
3037 doc: /* Make WINDOW fill its frame.
3038 Only the frame WINDOW is on is affected. WINDOW must be a valid window
3039 and defaults to the selected one.
3041 Optional argument ROOT, if non-nil, must specify an internal window such
3042 that WINDOW is in its window subtree. If this is the case, replace ROOT
3043 by WINDOW and leave alone any windows not part of ROOT's subtree.
3045 When WINDOW is live try to reduce display jumps by keeping the text
3046 previously visible in WINDOW in the same place on the frame. Doing this
3047 depends on the value of (window-start WINDOW), so if calling this
3048 function in a program gives strange scrolling, make sure the
3049 window-start value is reasonable when this function is called. */)
3050 (Lisp_Object window, Lisp_Object root)
3052 struct window *w, *r, *s;
3053 struct frame *f;
3054 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
3055 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
3056 int top IF_LINT (= 0), new_top, resize_failed;
3058 w = decode_valid_window (window);
3059 XSETWINDOW (window, w);
3060 f = XFRAME (w->frame);
3062 if (NILP (root))
3063 /* ROOT is the frame's root window. */
3065 root = FRAME_ROOT_WINDOW (f);
3066 r = XWINDOW (root);
3068 else
3069 /* ROOT must be an ancestor of WINDOW. */
3071 r = decode_valid_window (root);
3072 pwindow = XWINDOW (window)->parent;
3073 while (!NILP (pwindow))
3074 if (EQ (pwindow, root))
3075 break;
3076 else
3077 pwindow = XWINDOW (pwindow)->parent;
3078 if (!EQ (pwindow, root))
3079 error ("Specified root is not an ancestor of specified window");
3082 if (EQ (window, root))
3083 /* A noop. */
3084 return Qnil;
3085 /* I don't understand the "top > 0" part below. If we deal with a
3086 standalone minibuffer it would have been caught by the preceding
3087 test. */
3088 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3089 error ("Can't expand minibuffer to full frame");
3091 if (BUFFERP (w->contents))
3093 startpos = marker_position (w->start);
3094 startbyte = marker_byte_position (w->start);
3095 top = (WINDOW_TOP_EDGE_LINE (w)
3096 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3097 /* Make sure WINDOW is the frame's selected window. */
3098 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3100 if (EQ (selected_frame, w->frame))
3101 Fselect_window (window, Qnil);
3102 else
3103 fset_selected_window (f, window);
3106 else
3108 /* See if the frame's selected window is a part of the window
3109 subtree rooted at WINDOW, by finding all the selected window's
3110 parents and comparing each one with WINDOW. If it isn't we
3111 need a new selected window for this frame. */
3112 swindow = FRAME_SELECTED_WINDOW (f);
3113 while (1)
3115 pwindow = swindow;
3116 while (!NILP (pwindow) && !EQ (window, pwindow))
3117 pwindow = XWINDOW (pwindow)->parent;
3119 if (EQ (window, pwindow))
3120 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3121 as the new selected window. */
3122 break;
3123 else
3124 /* Else try the previous window of SWINDOW. */
3125 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3128 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3130 if (EQ (selected_frame, w->frame))
3131 Fselect_window (swindow, Qnil);
3132 else
3133 fset_selected_window (f, swindow);
3137 block_input ();
3138 if (!FRAME_INITIAL_P (f))
3140 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3142 /* We are going to free the glyph matrices of WINDOW, and with
3143 that we might lose any information about glyph rows that have
3144 some of their glyphs highlighted in mouse face. (These rows
3145 are marked with a non-zero mouse_face_p flag.) If WINDOW
3146 indeed has some glyphs highlighted in mouse face, signal to
3147 frame's up-to-date hook that mouse highlight was overwritten,
3148 so that it will arrange for redisplaying the highlight. */
3149 if (EQ (hlinfo->mouse_face_window, window))
3150 reset_mouse_highlight (hlinfo);
3152 free_window_matrices (r);
3154 fset_redisplay (f);
3155 Vwindow_list = Qnil;
3156 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3157 resize_failed = 0;
3159 if (!WINDOW_LEAF_P (w))
3161 /* Resize child windows vertically. */
3162 XSETINT (delta, r->pixel_height - w->pixel_height);
3163 w->pixel_top = r->pixel_top;
3164 w->top_line = r->top_line;
3165 resize_root_window (window, delta, Qnil, Qnil, Qt);
3166 if (window_resize_check (w, 0))
3168 window_resize_apply (w, 0);
3169 window_pixel_to_total (w->frame, Qnil);
3171 else
3173 resize_root_window (window, delta, Qnil, Qt, Qt);
3174 if (window_resize_check (w, 0))
3176 window_resize_apply (w, 0);
3177 window_pixel_to_total (w->frame, Qnil);
3179 else
3180 resize_failed = 1;
3183 /* Resize child windows horizontally. */
3184 if (!resize_failed)
3186 w->left_col = r->left_col;
3187 w->pixel_left = r->pixel_left;
3188 XSETINT (delta, r->pixel_width - w->pixel_width);
3189 resize_root_window (window, delta, Qt, Qnil, Qt);
3190 if (window_resize_check (w, 1))
3192 window_resize_apply (w, 1);
3193 window_pixel_to_total (w->frame, Qt);
3195 else
3197 resize_root_window (window, delta, Qt, Qt, Qt);
3198 if (window_resize_check (w, 1))
3200 window_resize_apply (w, 1);
3201 window_pixel_to_total (w->frame, Qt);
3203 else
3204 resize_failed = 1;
3208 if (resize_failed)
3209 /* Play safe, if we still can ... */
3211 window = swindow;
3212 w = XWINDOW (window);
3216 /* Cleanly unlink WINDOW from window-tree. */
3217 if (!NILP (w->prev))
3218 /* Get SIBLING above (on the left of) WINDOW. */
3220 sibling = w->prev;
3221 s = XWINDOW (sibling);
3222 wset_next (s, w->next);
3223 if (!NILP (s->next))
3224 wset_prev (XWINDOW (s->next), sibling);
3226 else
3227 /* Get SIBLING below (on the right of) WINDOW. */
3229 sibling = w->next;
3230 s = XWINDOW (sibling);
3231 wset_prev (s, Qnil);
3232 wset_combination (XWINDOW (w->parent),
3233 XWINDOW (w->parent)->horizontal, sibling);
3236 /* Delete ROOT and all child windows of ROOT. */
3237 if (WINDOWP (r->contents))
3239 delete_all_child_windows (r->contents);
3240 wset_combination (r, 0, Qnil);
3243 replace_window (root, window, 1);
3245 /* This must become SWINDOW anyway ....... */
3246 if (BUFFERP (w->contents) && !resize_failed)
3248 /* Try to minimize scrolling, by setting the window start to the
3249 point will cause the text at the old window start to be at the
3250 same place on the frame. But don't try to do this if the
3251 window start is outside the visible portion (as might happen
3252 when the display is not current, due to typeahead). */
3253 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3254 if (new_top != top
3255 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3256 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3258 struct position pos;
3259 struct buffer *obuf = current_buffer;
3261 Fset_buffer (w->contents);
3262 /* This computation used to temporarily move point, but that
3263 can have unwanted side effects due to text properties. */
3264 pos = *vmotion (startpos, startbyte, -top, w);
3266 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3267 w->window_end_valid = 0;
3268 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3269 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3270 /* We need to do this, so that the window-scroll-functions
3271 get called. */
3272 w->optional_new_start = 1;
3274 set_buffer_internal (obuf);
3278 adjust_frame_glyphs (f);
3279 unblock_input ();
3281 run_window_configuration_change_hook (f);
3283 return Qnil;
3287 void
3288 replace_buffer_in_windows (Lisp_Object buffer)
3290 call1 (Qreplace_buffer_in_windows, buffer);
3293 /* If BUFFER is shown in a window, safely replace it with some other
3294 buffer in all windows of all frames, even those on other keyboards. */
3296 void
3297 replace_buffer_in_windows_safely (Lisp_Object buffer)
3299 if (buffer_window_count (XBUFFER (buffer)))
3301 Lisp_Object tail, frame;
3303 /* A single call to window_loop won't do the job because it only
3304 considers frames on the current keyboard. So loop manually over
3305 frames, and handle each one. */
3306 FOR_EACH_FRAME (tail, frame)
3307 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3311 /* The following three routines are needed for running a window's
3312 configuration change hook. */
3313 static void
3314 run_funs (Lisp_Object funs)
3316 for (; CONSP (funs); funs = XCDR (funs))
3317 if (!EQ (XCAR (funs), Qt))
3318 call0 (XCAR (funs));
3321 static void
3322 select_window_norecord (Lisp_Object window)
3324 if (WINDOW_LIVE_P (window))
3325 Fselect_window (window, Qt);
3328 static void
3329 select_frame_norecord (Lisp_Object frame)
3331 if (FRAME_LIVE_P (XFRAME (frame)))
3332 Fselect_frame (frame, Qt);
3335 void
3336 run_window_configuration_change_hook (struct frame *f)
3338 ptrdiff_t count = SPECPDL_INDEX ();
3339 Lisp_Object frame, global_wcch
3340 = Fdefault_value (Qwindow_configuration_change_hook);
3341 XSETFRAME (frame, f);
3343 if (NILP (Vrun_hooks)
3344 || !(f->can_x_set_window_size)
3345 || !(f->can_run_window_configuration_change_hook))
3346 return;
3348 /* Use the right buffer. Matters when running the local hooks. */
3349 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3351 record_unwind_current_buffer ();
3352 Fset_buffer (Fwindow_buffer (Qnil));
3355 if (SELECTED_FRAME () != f)
3357 record_unwind_protect (select_frame_norecord, selected_frame);
3358 select_frame_norecord (frame);
3361 /* Look for buffer-local values. */
3363 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3364 for (; CONSP (windows); windows = XCDR (windows))
3366 Lisp_Object window = XCAR (windows);
3367 Lisp_Object buffer = Fwindow_buffer (window);
3368 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3369 buffer)))
3371 ptrdiff_t inner_count = SPECPDL_INDEX ();
3372 record_unwind_protect (select_window_norecord, selected_window);
3373 select_window_norecord (window);
3374 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3375 buffer));
3376 unbind_to (inner_count, Qnil);
3381 run_funs (global_wcch);
3382 unbind_to (count, Qnil);
3385 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3386 Srun_window_configuration_change_hook, 0, 1, 0,
3387 doc: /* Run `window-configuration-change-hook' for FRAME.
3388 If FRAME is omitted or nil, it defaults to the selected frame. */)
3389 (Lisp_Object frame)
3391 run_window_configuration_change_hook (decode_live_frame (frame));
3392 return Qnil;
3395 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3396 Srun_window_scroll_functions, 0, 1, 0,
3397 doc: /* Run `window-scroll-functions' for WINDOW.
3398 If WINDOW is omitted or nil, it defaults to the selected window. */)
3399 (Lisp_Object window)
3401 if (! NILP (Vwindow_scroll_functions))
3402 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3403 Fmarker_position (decode_live_window (window)->start));
3404 return Qnil;
3407 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3408 to run hooks. See make_frame for a case where it's not allowed.
3409 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3410 scroll-bar settings of the window are not reset from the buffer's
3411 local settings. */
3413 void
3414 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3415 bool run_hooks_p, bool keep_margins_p)
3417 struct window *w = XWINDOW (window);
3418 struct buffer *b = XBUFFER (buffer);
3419 ptrdiff_t count = SPECPDL_INDEX ();
3420 bool samebuf = EQ (buffer, w->contents);
3422 wset_buffer (w, buffer);
3424 if (EQ (window, selected_window))
3425 bset_last_selected_window (b, window);
3427 /* Let redisplay errors through. */
3428 b->display_error_modiff = 0;
3430 /* Update time stamps of buffer display. */
3431 if (INTEGERP (BVAR (b, display_count)))
3432 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3433 bset_display_time (b, Fcurrent_time ());
3435 w->window_end_pos = 0;
3436 w->window_end_vpos = 0;
3437 w->last_cursor_vpos = 0;
3439 if (!(keep_margins_p && samebuf))
3440 { /* If we're not actually changing the buffer, don't reset hscroll
3441 and vscroll. This case happens for example when called from
3442 change_frame_size_1, where we use a dummy call to
3443 Fset_window_buffer on the frame's selected window (and no
3444 other) just in order to run window-configuration-change-hook
3445 (no longer true since change_frame_size_1 directly calls
3446 run_window_configuration_change_hook). Resetting hscroll and
3447 vscroll here is problematic for things like image-mode and
3448 doc-view-mode since it resets the image's position whenever we
3449 resize the frame. */
3450 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3451 w->suspend_auto_hscroll = 0;
3452 w->vscroll = 0;
3453 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3454 set_marker_both (w->old_pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3455 set_marker_restricted (w->start,
3456 make_number (b->last_window_start),
3457 buffer);
3458 w->start_at_line_beg = 0;
3459 w->force_start = 0;
3461 /* Maybe we could move this into the `if' but it's not obviously safe and
3462 I doubt it's worth the trouble. */
3463 wset_redisplay (w);
3464 w->update_mode_line = true;
3466 /* We must select BUFFER to run the window-scroll-functions and to look up
3467 the buffer-local value of Vwindow_point_insertion_type. */
3468 record_unwind_current_buffer ();
3469 Fset_buffer (buffer);
3471 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3472 XMARKER (w->old_pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3474 if (!keep_margins_p)
3476 /* Set left and right marginal area width etc. from buffer. */
3477 set_window_fringes (w, BVAR (b, left_fringe_width),
3478 BVAR (b, right_fringe_width),
3479 BVAR (b, fringes_outside_margins));
3480 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3481 BVAR (b, vertical_scroll_bar_type),
3482 BVAR (b, scroll_bar_height),
3483 BVAR (b, horizontal_scroll_bar_type));
3484 set_window_margins (w, BVAR (b, left_margin_cols),
3485 BVAR (b, right_margin_cols));
3486 apply_window_adjustment (w);
3489 if (run_hooks_p)
3491 if (! NILP (Vwindow_scroll_functions))
3492 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3493 Fmarker_position (w->start));
3494 if (!samebuf)
3495 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3498 unbind_to (count, Qnil);
3501 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3502 doc: /* Make WINDOW display BUFFER-OR-NAME.
3503 WINDOW must be a live window and defaults to the selected one.
3504 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3506 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3507 display margins, fringe widths, and scroll bar settings are preserved;
3508 the default is to reset these from the local settings for BUFFER-OR-NAME
3509 or the frame defaults. Return nil.
3511 This function throws an error when WINDOW is strongly dedicated to its
3512 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3513 already display BUFFER-OR-NAME.
3515 This function runs `window-scroll-functions' before running
3516 `window-configuration-change-hook'. */)
3517 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3519 register Lisp_Object tem, buffer;
3520 register struct window *w = decode_live_window (window);
3522 XSETWINDOW (window, w);
3523 buffer = Fget_buffer (buffer_or_name);
3524 CHECK_BUFFER (buffer);
3525 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3526 error ("Attempt to display deleted buffer");
3528 tem = w->contents;
3529 if (NILP (tem))
3530 error ("Window is deleted");
3531 else
3533 if (!EQ (tem, buffer))
3535 if (EQ (w->dedicated, Qt))
3536 /* WINDOW is strongly dedicated to its buffer, signal an
3537 error. */
3538 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3539 else
3540 /* WINDOW is weakly dedicated to its buffer, reset
3541 dedication. */
3542 wset_dedicated (w, Qnil);
3544 call1 (Qrecord_window_buffer, window);
3547 unshow_buffer (w);
3550 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3552 return Qnil;
3555 static Lisp_Object
3556 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3558 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3561 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3562 0, 1, 0,
3563 doc: /* Force all windows to be updated on next redisplay.
3564 If optional arg OBJECT is a window, force redisplay of that window only.
3565 If OBJECT is a buffer or buffer name, force redisplay of all windows
3566 displaying that buffer. */)
3567 (Lisp_Object object)
3569 if (NILP (object))
3571 windows_or_buffers_changed = 29;
3572 update_mode_lines = 28;
3573 return Qt;
3576 if (WINDOWP (object))
3578 struct window *w = XWINDOW (object);
3579 mark_window_display_accurate (object, 0);
3580 w->update_mode_line = 1;
3581 if (BUFFERP (w->contents))
3582 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3583 update_mode_lines = 29;
3584 return Qt;
3587 if (STRINGP (object))
3588 object = Fget_buffer (object);
3589 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3590 && buffer_window_count (XBUFFER (object)))
3592 /* If buffer is live and shown in at least one window, find
3593 all windows showing this buffer and force update of them. */
3594 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3595 return NILP (object) ? Qnil : Qt;
3598 /* If nothing suitable was found, just return.
3599 We could signal an error, but this feature will typically be used
3600 asynchronously in timers or process sentinels, so we don't. */
3601 return Qnil;
3604 /* Obsolete since 24.3. */
3605 void
3606 temp_output_buffer_show (register Lisp_Object buf)
3608 register struct buffer *old = current_buffer;
3609 register Lisp_Object window;
3610 register struct window *w;
3612 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3614 Fset_buffer (buf);
3615 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3616 BEGV = BEG;
3617 ZV = Z;
3618 SET_PT (BEG);
3619 set_buffer_internal (old);
3621 if (!NILP (Vtemp_buffer_show_function))
3622 call1 (Vtemp_buffer_show_function, buf);
3623 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3625 if (!EQ (XWINDOW (window)->frame, selected_frame))
3626 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3627 Vminibuf_scroll_window = window;
3628 w = XWINDOW (window);
3629 w->hscroll = w->min_hscroll = w->hscroll_whole = 0;
3630 w->suspend_auto_hscroll = 0;
3631 set_marker_restricted_both (w->start, buf, BEG, BEG);
3632 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3633 set_marker_restricted_both (w->old_pointm, buf, BEG, BEG);
3635 /* Run temp-buffer-show-hook, with the chosen window selected
3636 and its buffer current. */
3638 ptrdiff_t count = SPECPDL_INDEX ();
3639 Lisp_Object prev_window, prev_buffer;
3640 prev_window = selected_window;
3641 XSETBUFFER (prev_buffer, old);
3643 /* Select the window that was chosen, for running the hook.
3644 Note: Both Fselect_window and select_window_norecord may
3645 set-buffer to the buffer displayed in the window,
3646 so we need to save the current buffer. --stef */
3647 record_unwind_protect (restore_buffer, prev_buffer);
3648 record_unwind_protect (select_window_norecord, prev_window);
3649 Fselect_window (window, Qt);
3650 Fset_buffer (w->contents);
3651 Frun_hooks (1, &Qtemp_buffer_show_hook);
3652 unbind_to (count, Qnil);
3657 /* Make new window, have it replace WINDOW in window-tree, and make
3658 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3659 horizontal child). */
3660 static void
3661 make_parent_window (Lisp_Object window, bool horflag)
3663 Lisp_Object parent;
3664 register struct window *o, *p;
3666 o = XWINDOW (window);
3667 p = allocate_window ();
3668 memcpy ((char *) p + sizeof (struct vectorlike_header),
3669 (char *) o + sizeof (struct vectorlike_header),
3670 word_size * VECSIZE (struct window));
3671 /* P's buffer slot may change from nil to a buffer... */
3672 adjust_window_count (p, 1);
3673 XSETWINDOW (parent, p);
3675 p->sequence_number = ++sequence_number;
3677 replace_window (window, parent, 1);
3679 wset_next (o, Qnil);
3680 wset_prev (o, Qnil);
3681 wset_parent (o, parent);
3682 /* ...but now P becomes an internal window. */
3683 wset_start (p, Qnil);
3684 wset_pointm (p, Qnil);
3685 wset_old_pointm (p, Qnil);
3686 wset_buffer (p, Qnil);
3687 wset_combination (p, horflag, window);
3688 wset_combination_limit (p, Qnil);
3689 wset_window_parameters (p, Qnil);
3692 /* Make new window from scratch. */
3693 Lisp_Object
3694 make_window (void)
3696 Lisp_Object window;
3697 register struct window *w;
3699 w = allocate_window ();
3700 /* Initialize Lisp data. Note that allocate_window initializes all
3701 Lisp data to nil, so do it only for slots which should not be nil. */
3702 wset_normal_lines (w, make_float (1.0));
3703 wset_normal_cols (w, make_float (1.0));
3704 wset_new_total (w, make_number (0));
3705 wset_new_normal (w, make_number (0));
3706 wset_new_pixel (w, make_number (0));
3707 wset_start (w, Fmake_marker ());
3708 wset_pointm (w, Fmake_marker ());
3709 wset_old_pointm (w, Fmake_marker ());
3710 wset_vertical_scroll_bar_type (w, Qt);
3711 wset_horizontal_scroll_bar_type (w, Qt);
3712 /* These Lisp fields are marked specially so they're not set to nil by
3713 allocate_window. */
3714 wset_prev_buffers (w, Qnil);
3715 wset_next_buffers (w, Qnil);
3717 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3718 non-Lisp data, so do it only for slots which should not be zero. */
3719 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3720 w->left_fringe_width = w->right_fringe_width = -1;
3721 w->mode_line_height = w->header_line_height = -1;
3722 #ifdef HAVE_WINDOW_SYSTEM
3723 w->phys_cursor_type = NO_CURSOR;
3724 w->phys_cursor_width = -1;
3725 #endif
3726 w->sequence_number = ++sequence_number;
3727 w->scroll_bar_width = -1;
3728 w->scroll_bar_height = -1;
3729 w->column_number_displayed = -1;
3730 /* Reset window_list. */
3731 Vwindow_list = Qnil;
3732 /* Return window. */
3733 XSETWINDOW (window, w);
3734 return window;
3737 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3738 doc: /* Set new pixel size of WINDOW to SIZE.
3739 WINDOW must be a valid window and defaults to the selected one.
3740 Return SIZE.
3742 Optional argument ADD non-nil means add SIZE to the new pixel size of
3743 WINDOW and return the sum.
3745 The new pixel size of WINDOW, if valid, will be shortly installed as
3746 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3747 `window-pixel-width').
3749 Note: This function does not operate on any child windows of WINDOW. */)
3750 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3752 struct window *w = decode_valid_window (window);
3753 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3754 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3756 CHECK_RANGED_INTEGER (size, size_min, size_max);
3757 if (NILP (add))
3758 wset_new_pixel (w, size);
3759 else
3760 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3762 return w->new_pixel;
3765 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3766 doc: /* Set new total size of WINDOW to SIZE.
3767 WINDOW must be a valid window and defaults to the selected one.
3768 Return SIZE.
3770 Optional argument ADD non-nil means add SIZE to the new total size of
3771 WINDOW and return the sum.
3773 The new total size of WINDOW, if valid, will be shortly installed as
3774 WINDOW's total height (see `window-total-height') or total width (see
3775 `window-total-width').
3777 Note: This function does not operate on any child windows of WINDOW. */)
3778 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3780 struct window *w = decode_valid_window (window);
3782 CHECK_NUMBER (size);
3783 if (NILP (add))
3784 wset_new_total (w, size);
3785 else
3786 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3788 return w->new_total;
3791 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3792 doc: /* Set new normal size of WINDOW to SIZE.
3793 WINDOW must be a valid window and defaults to the selected one.
3794 Return SIZE.
3796 The new normal size of WINDOW, if valid, will be shortly installed as
3797 WINDOW's normal size (see `window-normal-size').
3799 Note: This function does not operate on any child windows of WINDOW. */)
3800 (Lisp_Object window, Lisp_Object size)
3802 wset_new_normal (decode_valid_window (window), size);
3803 return size;
3806 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3807 non-zero) to w->new_pixel would result in correct heights (widths)
3808 for window W and recursively all child windows of W.
3810 Note: This function does not check any of `window-fixed-size-p',
3811 `window-min-height' or `window-min-width'. It does check that window
3812 sizes do not drop below one line (two columns). */
3813 static int
3814 window_resize_check (struct window *w, bool horflag)
3816 struct frame *f = XFRAME (w->frame);
3817 struct window *c;
3819 if (WINDOW_VERTICAL_COMBINATION_P (w))
3820 /* W is a vertical combination. */
3822 c = XWINDOW (w->contents);
3823 if (horflag)
3824 /* All child windows of W must have the same width as W. */
3826 while (c)
3828 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3829 || !window_resize_check (c, horflag))
3830 return 0;
3832 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3835 return 1;
3837 else
3838 /* The sum of the heights of the child windows of W must equal
3839 W's height. */
3841 int remaining_pixels = XINT (w->new_pixel);
3843 while (c)
3845 if (!window_resize_check (c, horflag))
3846 return 0;
3848 remaining_pixels -= XINT (c->new_pixel);
3849 if (remaining_pixels < 0)
3850 return 0;
3851 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3854 return remaining_pixels == 0;
3857 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3858 /* W is a horizontal combination. */
3860 c = XWINDOW (w->contents);
3861 if (horflag)
3862 /* The sum of the widths of the child windows of W must equal W's
3863 width. */
3865 int remaining_pixels = XINT (w->new_pixel);
3867 while (c)
3869 if (!window_resize_check (c, horflag))
3870 return 0;
3872 remaining_pixels -= XINT (c->new_pixel);
3873 if (remaining_pixels < 0)
3874 return 0;
3875 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3878 return remaining_pixels == 0;
3880 else
3881 /* All child windows of W must have the same height as W. */
3883 while (c)
3885 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3886 || !window_resize_check (c, horflag))
3887 return 0;
3889 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3892 return 1;
3895 else
3896 /* A leaf window. Make sure it's not too small. The following
3897 hardcodes the values of `window-safe-min-width' (2) and
3898 `window-safe-min-height' (1) which are defined in window.el. */
3899 return (XINT (w->new_pixel) >= (horflag
3900 ? (2 * FRAME_COLUMN_WIDTH (f))
3901 : FRAME_LINE_HEIGHT (f)));
3905 /* Set w->pixel_height (w->pixel_width if HORFLAG is non-zero) to
3906 w->new_pixel for window W and recursively all child windows of W.
3907 Also calculate and assign the new vertical (horizontal) pixel start
3908 positions of each of these windows.
3910 This function does not perform any error checks. Make sure you have
3911 run window_resize_check on W before applying this function. */
3912 static void
3913 window_resize_apply (struct window *w, bool horflag)
3915 struct window *c;
3916 int edge;
3917 int unit = (horflag
3918 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3919 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3921 /* Note: Assigning new_normal requires that the new total size of the
3922 parent window has been set *before*. */
3923 if (horflag)
3925 w->pixel_width = XFASTINT (w->new_pixel);
3926 w->total_cols = w->pixel_width / unit;
3927 if (NUMBERP (w->new_normal))
3928 wset_normal_cols (w, w->new_normal);
3930 edge = w->pixel_left;
3932 else
3934 w->pixel_height = XFASTINT (w->new_pixel);
3935 w->total_lines = w->pixel_height / unit;
3936 if (NUMBERP (w->new_normal))
3937 wset_normal_lines (w, w->new_normal);
3939 edge = w->pixel_top;
3942 if (WINDOW_VERTICAL_COMBINATION_P (w))
3943 /* W is a vertical combination. */
3945 c = XWINDOW (w->contents);
3946 while (c)
3948 if (horflag)
3950 c->pixel_left = edge;
3951 c->left_col = edge / unit;
3953 else
3955 c->pixel_top = edge;
3956 c->top_line = edge / unit;
3958 window_resize_apply (c, horflag);
3959 if (!horflag)
3960 edge = edge + c->pixel_height;
3962 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3965 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3966 /* W is a horizontal combination. */
3968 c = XWINDOW (w->contents);
3969 while (c)
3971 if (horflag)
3973 c->pixel_left = edge;
3974 c->left_col = edge / unit;
3976 else
3978 c->pixel_top = edge;
3979 c->top_line = edge / unit;
3982 window_resize_apply (c, horflag);
3983 if (horflag)
3984 edge = edge + c->pixel_width;
3986 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3989 else
3990 /* Bug#15957. */
3991 w->window_end_valid = 0;
3995 /* Set w->total_lines (w->total_cols if HORFLAG is non-zero) to
3996 w->new_total for window W and recursively all child windows of W.
3997 Also calculate and assign the new vertical (horizontal) start
3998 positions of each of these windows. */
3999 static void
4000 window_resize_apply_total (struct window *w, bool horflag)
4002 struct window *c;
4003 int edge;
4005 /* Note: Assigning new_normal requires that the new total size of the
4006 parent window has been set *before*. */
4007 if (horflag)
4009 w->total_cols = XFASTINT (w->new_total);
4010 edge = w->left_col;
4012 else
4014 w->total_lines = XFASTINT (w->new_total);
4015 edge = w->top_line;
4018 if (WINDOW_VERTICAL_COMBINATION_P (w))
4019 /* W is a vertical combination. */
4021 c = XWINDOW (w->contents);
4022 while (c)
4024 if (horflag)
4025 c->left_col = edge;
4026 else
4027 c->top_line = edge;
4029 window_resize_apply_total (c, horflag);
4030 if (!horflag)
4031 edge = edge + c->total_lines;
4033 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4036 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4037 /* W is a horizontal combination. */
4039 c = XWINDOW (w->contents);
4040 while (c)
4042 if (horflag)
4043 c->left_col = edge;
4044 else
4045 c->top_line = edge;
4047 window_resize_apply_total (c, horflag);
4048 if (horflag)
4049 edge = edge + c->total_cols;
4051 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4056 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4057 doc: /* Apply requested size values for window-tree of FRAME.
4058 If FRAME is omitted or nil, it defaults to the selected frame.
4060 Optional argument HORIZONTAL omitted or nil means apply requested
4061 height values. HORIZONTAL non-nil means apply requested width values.
4063 The requested size values are those set by `set-window-new-pixel' and
4064 `set-window-new-normal'. This function checks whether the requested
4065 values sum up to a valid window layout, recursively assigns the new
4066 sizes of all child windows and calculates and assigns the new start
4067 positions of these windows.
4069 Return t if the requested values have been applied correctly, nil
4070 otherwise.
4072 Note: This function does not check any of `window-fixed-size-p',
4073 `window-min-height' or `window-min-width'. All these checks have to
4074 be applied on the Elisp level. */)
4075 (Lisp_Object frame, Lisp_Object horizontal)
4077 struct frame *f = decode_live_frame (frame);
4078 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4079 bool horflag = !NILP (horizontal);
4081 if (!window_resize_check (r, horflag)
4082 || (XINT (r->new_pixel)
4083 != (horflag ? r->pixel_width : r->pixel_height)))
4084 return Qnil;
4086 block_input ();
4087 window_resize_apply (r, horflag);
4089 fset_redisplay (f);
4090 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4092 adjust_frame_glyphs (f);
4093 unblock_input ();
4095 return Qt;
4099 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4100 doc: /* Apply requested total size values for window-tree of FRAME.
4101 If FRAME is omitted or nil, it defaults to the selected frame.
4103 This function does not assign pixel or normal size values. You should
4104 have run `window-resize-apply' before running this.
4106 Optional argument HORIZONTAL omitted or nil means apply requested
4107 height values. HORIZONTAL non-nil means apply requested width
4108 values. */)
4109 (Lisp_Object frame, Lisp_Object horizontal)
4111 struct frame *f = decode_live_frame (frame);
4112 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4114 block_input ();
4115 /* Necessary when deleting the top-/or leftmost window. */
4116 r->left_col = 0;
4117 r->top_line = FRAME_TOP_MARGIN (f);
4118 window_resize_apply_total (r, !NILP (horizontal));
4119 /* Handle the mini window. */
4120 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4122 struct window *m = XWINDOW (f->minibuffer_window);
4124 if (NILP (horizontal))
4126 m->top_line = r->top_line + r->total_lines;
4127 m->total_lines = XFASTINT (m->new_total);
4129 else
4130 m->total_cols = XFASTINT (m->new_total);
4133 unblock_input ();
4135 return Qt;
4139 /* Resize frame F's windows when number of lines of F is set to SIZE.
4140 HORFLAG 1 means resize windows when number of columns of F is set to
4141 SIZE. PIXELWISE 1 means to interpret SIZE as pixels. */
4142 void
4143 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4145 Lisp_Object root = f->root_window;
4146 struct window *r = XWINDOW (root);
4147 Lisp_Object mini = f->minibuffer_window;
4148 struct window *m;
4149 /* old_size is the old size of the frame's root window. */
4150 int old_size = horflag ? r->total_cols : r->total_lines;
4151 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4152 int old_pixel_top = r->pixel_top;
4153 /* new_size is the new size of the frame's root window. */
4154 int new_size, new_pixel_size;
4155 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4157 /* Don't let the size drop below one unit. This is more comforting
4158 when we are called from x_set_tool_bar_lines since the latter may
4159 have implicitly given us a zero or negative height. */
4160 if (pixelwise)
4162 /* Note: This does not include the size for internal borders
4163 since these are not part of the frame's text area. */
4164 new_pixel_size = max (horflag
4165 ? size
4166 : (size
4167 - ((FRAME_HAS_MINIBUF_P (f)
4168 && !FRAME_MINIBUF_ONLY_P (f))
4169 ? FRAME_LINE_HEIGHT (f) : 0)),
4170 unit);
4171 new_size = new_pixel_size / unit;
4173 else
4175 new_size = max (horflag
4176 ? size
4177 : (size
4178 - ((FRAME_HAS_MINIBUF_P (f)
4179 && !FRAME_MINIBUF_ONLY_P (f))
4180 ? 1 : 0)),
4182 new_pixel_size = new_size * unit;
4185 r->top_line = FRAME_TOP_MARGIN (f);
4186 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4188 if (new_pixel_size == old_pixel_size
4189 && r->pixel_top == old_pixel_top)
4191 else if (WINDOW_LEAF_P (r))
4192 /* For a leaf root window just set the size. */
4193 if (horflag)
4195 r->total_cols = new_size;
4196 r->pixel_width = new_pixel_size;
4198 else
4200 r->total_lines = new_size;
4201 r->pixel_height = new_pixel_size;
4203 else
4205 Lisp_Object delta;
4207 if (pixelwise)
4208 XSETINT (delta, new_pixel_size - old_pixel_size);
4209 else
4210 XSETINT (delta, new_size - old_size);
4212 /* Try a "normal" resize first. */
4213 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4214 pixelwise ? Qt : Qnil);
4215 if (window_resize_check (r, horflag)
4216 && new_pixel_size == XINT (r->new_pixel))
4218 window_resize_apply (r, horflag);
4219 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4221 else
4223 /* Try with "reasonable" minimum sizes next. */
4224 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4225 pixelwise ? Qt : Qnil);
4226 if (window_resize_check (r, horflag)
4227 && new_pixel_size == XINT (r->new_pixel))
4229 window_resize_apply (r, horflag);
4230 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4231 #if 0 /* Let's try without safe sizes and/or killing other windows. */
4233 else
4235 /* Finally, try with "safe" minimum sizes. */
4236 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4237 pixelwise ? Qt : Qnil);
4238 if (window_resize_check (r, horflag)
4239 && new_pixel_size == XINT (r->new_pixel))
4241 window_resize_apply (r, horflag);
4242 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4244 else
4246 /* We lost. Delete all windows but the frame's
4247 selected one. */
4248 root = f->selected_window;
4249 Fdelete_other_windows_internal (root, Qnil);
4250 if (horflag)
4252 XWINDOW (root)->total_cols = new_size;
4253 XWINDOW (root)->pixel_width = new_pixel_size;
4255 else
4257 XWINDOW (root)->total_lines = new_size;
4258 XWINDOW (root)->pixel_height = new_pixel_size;
4261 #endif /* 0 */
4266 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4268 m = XWINDOW (mini);
4269 if (horflag)
4271 m->total_cols = size;
4272 m->pixel_width = new_pixel_size;
4274 else
4276 /* Are we sure we always want 1 line here? */
4277 m->total_lines = 1;
4278 m->pixel_height = FRAME_LINE_HEIGHT (f);
4279 m->top_line = r->top_line + r->total_lines;
4280 m->pixel_top = r->pixel_top + r->pixel_height;
4284 fset_redisplay (f);
4288 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4289 doc: /* Split window OLD.
4290 Second argument PIXEL-SIZE specifies the number of pixels of the
4291 new window. In any case TOTAL-SIZE must be a positive integer.
4293 Third argument SIDE nil (or `below') specifies that the new window shall
4294 be located below WINDOW. SIDE `above' means the new window shall be
4295 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4296 height of the new window including space reserved for the mode and/or
4297 header line.
4299 SIDE t (or `right') specifies that the new window shall be located on
4300 the right side of WINDOW. SIDE `left' means the new window shall be
4301 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4302 width of the new window including space reserved for fringes and the
4303 scrollbar or a divider column.
4305 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4306 according to the SIDE argument.
4308 The new pixel and normal sizes of all involved windows must have been
4309 set correctly. See the code of `split-window' for how this is done. */)
4310 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4312 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4313 parent window or an internal window we have to install as OLD's new
4314 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4315 provided OLD is a leaf window, or to the frame's selected window.
4316 NEW (*n) is the new window created with some parameters taken from
4317 REFERENCE (*r). */
4318 register Lisp_Object new, frame, reference;
4319 register struct window *o, *p, *n, *r, *c;
4320 struct frame *f;
4321 bool horflag
4322 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4323 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4324 int combination_limit = 0, sum = 0;
4325 int total_size;
4327 CHECK_WINDOW (old);
4328 o = XWINDOW (old);
4329 frame = WINDOW_FRAME (o);
4330 f = XFRAME (frame);
4332 CHECK_NUMBER (pixel_size);
4333 total_size
4334 = XINT (pixel_size) / (horflag
4335 ? FRAME_COLUMN_WIDTH (f)
4336 : FRAME_LINE_HEIGHT (f));
4338 /* Set combination_limit to 1 if we have to make a new parent window.
4339 We do that if either `window-combination-limit' is t, or OLD has no
4340 parent, or OLD is ortho-combined. */
4341 combination_limit =
4342 EQ (Vwindow_combination_limit, Qt)
4343 || NILP (o->parent)
4344 || (horflag
4345 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4346 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4348 /* We need a live reference window to initialize some parameters. */
4349 if (WINDOW_LIVE_P (old))
4350 /* OLD is live, use it as reference window. */
4351 reference = old;
4352 else
4353 /* Use the frame's selected window as reference window. */
4354 reference = FRAME_SELECTED_WINDOW (f);
4355 r = XWINDOW (reference);
4357 /* The following bugs are caught by `split-window'. */
4358 if (MINI_WINDOW_P (o))
4359 error ("Attempt to split minibuffer window");
4360 else if (total_size < (horflag ? 2 : 1))
4361 error ("Size of new window too small (after split)");
4362 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4363 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4364 proportionally. */
4366 p = XWINDOW (o->parent);
4367 /* Temporarily pretend we split the parent window. */
4368 wset_new_pixel
4369 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4370 - XINT (pixel_size)));
4371 if (!window_resize_check (p, horflag))
4372 error ("Window sizes don't fit");
4373 else
4374 /* Undo the temporary pretension. */
4375 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4377 else
4379 if (!window_resize_check (o, horflag))
4380 error ("Resizing old window failed");
4381 else if (XINT (pixel_size) + XINT (o->new_pixel)
4382 != (horflag ? o->pixel_width : o->pixel_height))
4383 error ("Sum of sizes of old and new window don't fit");
4386 /* This is our point of no return. */
4387 if (combination_limit)
4389 /* Save the old value of o->normal_cols/lines. It gets corrupted
4390 by make_parent_window and we need it below for assigning it to
4391 p->new_normal. */
4392 Lisp_Object new_normal
4393 = horflag ? o->normal_cols : o->normal_lines;
4395 make_parent_window (old, horflag);
4396 p = XWINDOW (o->parent);
4397 if (EQ (Vwindow_combination_limit, Qt))
4398 /* Store t in the new parent's combination_limit slot to avoid
4399 that its children get merged into another window. */
4400 wset_combination_limit (p, Qt);
4401 /* These get applied below. */
4402 wset_new_pixel
4403 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4404 wset_new_total
4405 (p, make_number (horflag ? o->total_cols : o->total_lines));
4406 wset_new_normal (p, new_normal);
4408 else
4409 p = XWINDOW (o->parent);
4411 fset_redisplay (f);
4412 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4413 new = make_window ();
4414 n = XWINDOW (new);
4415 wset_frame (n, frame);
4416 wset_parent (n, o->parent);
4418 if (EQ (side, Qabove) || EQ (side, Qleft))
4420 wset_prev (n, o->prev);
4421 if (NILP (n->prev))
4422 wset_combination (p, horflag, new);
4423 else
4424 wset_next (XWINDOW (n->prev), new);
4425 wset_next (n, old);
4426 wset_prev (o, new);
4428 else
4430 wset_next (n, o->next);
4431 if (!NILP (n->next))
4432 wset_prev (XWINDOW (n->next), new);
4433 wset_prev (n, old);
4434 wset_next (o, new);
4437 n->window_end_valid = 0;
4438 n->last_cursor_vpos = 0;
4440 /* Get special geometry settings from reference window. */
4441 n->left_margin_cols = r->left_margin_cols;
4442 n->right_margin_cols = r->right_margin_cols;
4443 n->left_fringe_width = r->left_fringe_width;
4444 n->right_fringe_width = r->right_fringe_width;
4445 n->fringes_outside_margins = r->fringes_outside_margins;
4446 n->scroll_bar_width = r->scroll_bar_width;
4447 n->scroll_bar_height = r->scroll_bar_height;
4448 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4449 wset_horizontal_scroll_bar_type (n, r->horizontal_scroll_bar_type);
4451 /* Directly assign orthogonal coordinates and sizes. */
4452 if (horflag)
4454 n->pixel_top = o->pixel_top;
4455 n->top_line = o->top_line;
4456 n->pixel_height = o->pixel_height;
4457 n->total_lines = o->total_lines;
4459 else
4461 n->pixel_left = o->pixel_left;
4462 n->left_col = o->left_col;
4463 n->pixel_width = o->pixel_width;
4464 n->total_cols = o->total_cols;
4467 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4468 get them ready here. */
4469 wset_new_pixel (n, pixel_size);
4470 c = XWINDOW (p->contents);
4471 while (c)
4473 if (c != n)
4474 sum = sum + XINT (c->new_total);
4475 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4477 wset_new_total (n, make_number ((horflag
4478 ? p->total_cols
4479 : p->total_lines)
4480 - sum));
4481 wset_new_normal (n, normal_size);
4483 block_input ();
4484 window_resize_apply (p, horflag);
4485 adjust_frame_glyphs (f);
4486 /* Set buffer of NEW to buffer of reference window. Don't run
4487 any hooks. */
4488 set_window_buffer (new, r->contents, 0, 1);
4489 unblock_input ();
4491 /* Maybe we should run the scroll functions in Elisp (which already
4492 runs the configuration change hook). */
4493 if (! NILP (Vwindow_scroll_functions))
4494 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4495 Fmarker_position (n->start));
4496 /* Return NEW. */
4497 return new;
4501 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4502 doc: /* Remove WINDOW from its frame.
4503 WINDOW defaults to the selected window. Return nil.
4504 Signal an error when WINDOW is the only window on its frame. */)
4505 (register Lisp_Object window)
4507 register Lisp_Object parent, sibling, frame, root;
4508 struct window *w, *p, *s, *r;
4509 struct frame *f;
4510 bool horflag, before_sibling = 0;
4512 w = decode_any_window (window);
4513 XSETWINDOW (window, w);
4514 if (NILP (w->contents))
4515 /* It's a no-op to delete an already deleted window. */
4516 return Qnil;
4518 parent = w->parent;
4519 if (NILP (parent))
4520 /* Never delete a minibuffer or frame root window. */
4521 error ("Attempt to delete minibuffer or sole ordinary window");
4522 else if (NILP (w->prev) && NILP (w->next))
4523 /* Rather bow out here, this case should be handled on the Elisp
4524 level. */
4525 error ("Attempt to delete sole window of parent");
4527 p = XWINDOW (parent);
4528 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4530 frame = WINDOW_FRAME (w);
4531 f = XFRAME (frame);
4533 root = FRAME_ROOT_WINDOW (f);
4534 r = XWINDOW (root);
4536 /* Unlink WINDOW from window tree. */
4537 if (NILP (w->prev))
4538 /* Get SIBLING below (on the right of) WINDOW. */
4540 /* before_sibling 1 means WINDOW is the first child of its
4541 parent and thus before the sibling. */
4542 before_sibling = 1;
4543 sibling = w->next;
4544 s = XWINDOW (sibling);
4545 wset_prev (s, Qnil);
4546 wset_combination (p, horflag, sibling);
4548 else
4549 /* Get SIBLING above (on the left of) WINDOW. */
4551 sibling = w->prev;
4552 s = XWINDOW (sibling);
4553 wset_next (s, w->next);
4554 if (!NILP (s->next))
4555 wset_prev (XWINDOW (s->next), sibling);
4558 if (window_resize_check (r, horflag)
4559 && (XINT (r->new_pixel)
4560 == (horflag ? r->pixel_width : r->pixel_height)))
4561 /* We can delete WINDOW now. */
4564 /* Block input. */
4565 block_input ();
4566 #ifdef HAVE_XWIDGETS
4567 xwidget_view_delete_all_in_window(w);
4568 #endif
4569 window_resize_apply (p, horflag);
4570 /* If this window is referred to by the dpyinfo's mouse
4571 highlight, invalidate that slot to be safe (Bug#9904). */
4572 if (!FRAME_INITIAL_P (f))
4574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4576 if (EQ (hlinfo->mouse_face_window, window))
4577 hlinfo->mouse_face_window = Qnil;
4580 fset_redisplay (f);
4581 Vwindow_list = Qnil;
4582 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4584 wset_next (w, Qnil); /* Don't delete w->next too. */
4585 free_window_matrices (w);
4587 if (WINDOWP (w->contents))
4589 delete_all_child_windows (w->contents);
4590 wset_combination (w, 0, Qnil);
4592 else
4594 unshow_buffer (w);
4595 unchain_marker (XMARKER (w->pointm));
4596 unchain_marker (XMARKER (w->old_pointm));
4597 unchain_marker (XMARKER (w->start));
4598 wset_buffer (w, Qnil);
4601 if (NILP (s->prev) && NILP (s->next))
4602 /* A matrjoshka where SIBLING has become the only child of
4603 PARENT. */
4605 /* Put SIBLING into PARENT's place. */
4606 replace_window (parent, sibling, 0);
4607 /* Have SIBLING inherit the following three slot values from
4608 PARENT (the combination_limit slot is not inherited). */
4609 wset_normal_cols (s, p->normal_cols);
4610 wset_normal_lines (s, p->normal_lines);
4611 /* Mark PARENT as deleted. */
4612 wset_combination (p, 0, Qnil);
4613 /* Try to merge SIBLING into its new parent. */
4614 recombine_windows (sibling);
4617 adjust_frame_glyphs (f);
4619 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4620 /* We deleted the frame's selected window. */
4622 /* Use the frame's first window as fallback ... */
4623 Lisp_Object new_selected_window = Fframe_first_window (frame);
4624 /* ... but preferably use its most recently used window. */
4625 Lisp_Object mru_window;
4627 /* `get-mru-window' might fail for some reason so play it safe
4628 - promote the first window _without recording it_ first. */
4629 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4630 Fselect_window (new_selected_window, Qt);
4631 else
4632 fset_selected_window (f, new_selected_window);
4634 unblock_input ();
4636 /* Now look whether `get-mru-window' gets us something. */
4637 mru_window = call1 (Qget_mru_window, frame);
4638 if (WINDOW_LIVE_P (mru_window)
4639 && EQ (XWINDOW (mru_window)->frame, frame))
4640 new_selected_window = mru_window;
4642 /* If all ended up well, we now promote the mru window. */
4643 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4644 Fselect_window (new_selected_window, Qnil);
4645 else
4646 fset_selected_window (f, new_selected_window);
4648 else
4649 unblock_input ();
4651 /* Must be run by the caller:
4652 run_window_configuration_change_hook (f); */
4654 else
4655 /* We failed: Relink WINDOW into window tree. */
4657 if (before_sibling)
4659 wset_prev (s, window);
4660 wset_combination (p, horflag, window);
4662 else
4664 wset_next (s, window);
4665 if (!NILP (w->next))
4666 wset_prev (XWINDOW (w->next), window);
4668 error ("Deletion failed");
4671 return Qnil;
4674 /***********************************************************************
4675 Resizing Mini-Windows
4676 ***********************************************************************/
4678 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4679 can. */
4680 void
4681 grow_mini_window (struct window *w, int delta, bool pixelwise)
4683 struct frame *f = XFRAME (w->frame);
4684 struct window *r;
4685 Lisp_Object root, height;
4686 int line_height, pixel_height;
4688 eassert (MINI_WINDOW_P (w));
4689 eassert (delta >= 0);
4691 if (delta > 0)
4693 root = FRAME_ROOT_WINDOW (f);
4694 r = XWINDOW (root);
4695 height = call3 (Qwindow_resize_root_window_vertically,
4696 root, make_number (- delta), pixelwise ? Qt : Qnil);
4697 if (INTEGERP (height) && window_resize_check (r, 0))
4699 block_input ();
4700 window_resize_apply (r, 0);
4702 if (pixelwise)
4704 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4705 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4707 else
4709 line_height = min (-XINT (height),
4710 ((INT_MAX - w->pixel_height)
4711 / FRAME_LINE_HEIGHT (f)));
4712 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4715 /* Grow the mini-window. */
4716 w->pixel_top = r->pixel_top + r->pixel_height;
4717 w->top_line = r->top_line + r->total_lines;
4718 /* Make sure the mini-window has always at least one line. */
4719 w->pixel_height = max (w->pixel_height + pixel_height,
4720 FRAME_LINE_HEIGHT (f));
4721 w->total_lines = max (w->total_lines + line_height, 1);
4723 /* Enforce full redisplay of the frame. */
4724 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4725 fset_redisplay (f);
4726 adjust_frame_glyphs (f);
4727 unblock_input ();
4732 /* Shrink mini-window W to one line. */
4733 void
4734 shrink_mini_window (struct window *w, bool pixelwise)
4736 struct frame *f = XFRAME (w->frame);
4737 struct window *r;
4738 Lisp_Object root, delta;
4739 EMACS_INT height, unit;
4741 eassert (MINI_WINDOW_P (w));
4743 height = pixelwise ? w->pixel_height : w->total_lines;
4744 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4745 if (height > unit)
4747 root = FRAME_ROOT_WINDOW (f);
4748 r = XWINDOW (root);
4749 delta = call3 (Qwindow_resize_root_window_vertically,
4750 root, make_number (height - unit),
4751 pixelwise ? Qt : Qnil);
4752 if (INTEGERP (delta) && window_resize_check (r, 0))
4754 block_input ();
4755 window_resize_apply (r, 0);
4757 /* Shrink the mini-window. */
4758 w->top_line = r->top_line + r->total_lines;
4759 w->total_lines = 1;
4760 w->pixel_top = r->pixel_top + r->pixel_height;
4761 w->pixel_height = FRAME_LINE_HEIGHT (f);
4762 /* Enforce full redisplay of the frame. */
4763 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4764 fset_redisplay (f);
4765 adjust_frame_glyphs (f);
4766 unblock_input ();
4768 /* If the above failed for whatever strange reason we must make a
4769 one window frame here. The same routine will be needed when
4770 shrinking the frame (and probably when making the initial
4771 *scratch* window). For the moment leave things as they are. */
4775 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4776 doc: /* Resize minibuffer window WINDOW. */)
4777 (Lisp_Object window)
4779 struct window *w = XWINDOW (window);
4780 struct window *r;
4781 struct frame *f;
4782 int height;
4784 CHECK_WINDOW (window);
4785 f = XFRAME (w->frame);
4787 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4788 error ("Not a valid minibuffer window");
4789 else if (FRAME_MINIBUF_ONLY_P (f))
4790 error ("Cannot resize a minibuffer-only frame");
4792 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4793 height = r->pixel_height + w->pixel_height;
4794 if (window_resize_check (r, 0)
4795 && XINT (w->new_pixel) > 0
4796 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4798 block_input ();
4799 window_resize_apply (r, 0);
4801 w->pixel_height = XFASTINT (w->new_pixel);
4802 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4803 w->pixel_top = r->pixel_top + r->pixel_height;
4804 w->top_line = r->top_line + r->total_lines;
4806 fset_redisplay (f);
4807 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4808 adjust_frame_glyphs (f);
4809 unblock_input ();
4810 return Qt;
4812 else
4813 error ("Failed to resize minibuffer window");
4816 /* Mark window cursors off for all windows in the window tree rooted
4817 at W by setting their phys_cursor_on_p flag to zero. Called from
4818 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4819 the frame are cleared. */
4821 void
4822 mark_window_cursors_off (struct window *w)
4824 while (w)
4826 if (WINDOWP (w->contents))
4827 mark_window_cursors_off (XWINDOW (w->contents));
4828 else
4829 w->phys_cursor_on_p = 0;
4831 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4836 /* Return number of lines of text (not counting mode lines) in W. */
4839 window_internal_height (struct window *w)
4841 int ht = w->total_lines;
4843 if (!MINI_WINDOW_P (w))
4845 if (!NILP (w->parent)
4846 || WINDOWP (w->contents)
4847 || !NILP (w->next)
4848 || !NILP (w->prev)
4849 || WINDOW_WANTS_MODELINE_P (w))
4850 --ht;
4852 if (WINDOW_WANTS_HEADER_LINE_P (w))
4853 --ht;
4856 return ht;
4860 /************************************************************************
4861 Window Scrolling
4862 ***********************************************************************/
4864 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4865 N screen-fulls, which is defined as the height of the window minus
4866 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4867 instead. Negative values of N mean scroll down. NOERROR non-zero
4868 means don't signal an error if we try to move over BEGV or ZV,
4869 respectively. */
4871 static void
4872 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4874 ptrdiff_t count = SPECPDL_INDEX ();
4876 immediate_quit = 1;
4877 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4879 wset_redisplay (XWINDOW (window));
4881 if (whole && Vfast_but_imprecise_scrolling)
4882 specbind (Qfontification_functions, Qnil);
4884 /* If we must, use the pixel-based version which is much slower than
4885 the line-based one but can handle varying line heights. */
4886 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4887 window_scroll_pixel_based (window, n, whole, noerror);
4888 else
4889 window_scroll_line_based (window, n, whole, noerror);
4891 unbind_to (count, Qnil);
4893 /* Bug#15957. */
4894 XWINDOW (window)->window_end_valid = 0;
4895 immediate_quit = 0;
4899 /* Implementation of window_scroll that works based on pixel line
4900 heights. See the comment of window_scroll for parameter
4901 descriptions. */
4903 static void
4904 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4906 struct it it;
4907 struct window *w = XWINDOW (window);
4908 struct text_pos start;
4909 int this_scroll_margin;
4910 /* True if we fiddled the window vscroll field without really scrolling. */
4911 int vscrolled = 0;
4912 int x, y, rtop, rbot, rowh, vpos;
4913 void *itdata = NULL;
4914 int window_total_lines;
4915 int frame_line_height = default_line_pixel_height (w);
4916 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
4917 Fwindow_old_point (window)));
4919 SET_TEXT_POS_FROM_MARKER (start, w->start);
4920 /* Scrolling a minibuffer window via scroll bar when the echo area
4921 shows long text sometimes resets the minibuffer contents behind
4922 our backs. */
4923 if (CHARPOS (start) > ZV)
4924 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4926 /* If PT is not visible in WINDOW, move back one half of
4927 the screen. Allow PT to be partially visible, otherwise
4928 something like (scroll-down 1) with PT in the line before
4929 the partially visible one would recenter. */
4931 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4933 itdata = bidi_shelve_cache ();
4934 /* Move backward half the height of the window. Performance note:
4935 vmotion used here is about 10% faster, but would give wrong
4936 results for variable height lines. */
4937 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4938 it.current_y = it.last_visible_y;
4939 move_it_vertically_backward (&it, window_box_height (w) / 2);
4941 /* The function move_iterator_vertically may move over more than
4942 the specified y-distance. If it->w is small, e.g. a
4943 mini-buffer window, we may end up in front of the window's
4944 display area. Start displaying at the start of the line
4945 containing PT in this case. */
4946 if (it.current_y <= 0)
4948 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4949 move_it_vertically_backward (&it, 0);
4950 it.current_y = 0;
4953 start = it.current.pos;
4954 bidi_unshelve_cache (itdata, 0);
4956 else if (auto_window_vscroll_p)
4958 if (rtop || rbot) /* Partially visible. */
4960 int px;
4961 int dy = frame_line_height;
4962 /* In the below we divide the window box height by the
4963 frame's line height to make the result predictable when
4964 the window box is not an integral multiple of the line
4965 height. This is important to ensure we get back to the
4966 same position when scrolling up, then down. */
4967 if (whole)
4968 dy = max ((window_box_height (w) / dy
4969 - next_screen_context_lines) * dy,
4970 dy);
4971 dy *= n;
4973 if (n < 0)
4975 /* Only vscroll backwards if already vscrolled forwards. */
4976 if (w->vscroll < 0 && rtop > 0)
4978 px = max (0, -w->vscroll - min (rtop, -dy));
4979 Fset_window_vscroll (window, make_number (px), Qt);
4980 return;
4983 if (n > 0)
4985 /* Do vscroll if already vscrolled or only display line. */
4986 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4988 px = max (0, -w->vscroll + min (rbot, dy));
4989 Fset_window_vscroll (window, make_number (px), Qt);
4990 return;
4993 /* Maybe modify window start instead of scrolling. */
4994 if (rbot > 0 || w->vscroll < 0)
4996 ptrdiff_t spos;
4998 Fset_window_vscroll (window, make_number (0), Qt);
4999 /* If there are other text lines above the current row,
5000 move window start to current row. Else to next row. */
5001 if (rbot > 0)
5002 spos = XINT (Fline_beginning_position (Qnil));
5003 else
5004 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5005 set_marker_restricted (w->start, make_number (spos),
5006 w->contents);
5007 w->start_at_line_beg = 1;
5008 w->update_mode_line = 1;
5009 /* Set force_start so that redisplay_window will run the
5010 window-scroll-functions. */
5011 w->force_start = 1;
5012 return;
5016 /* Cancel previous vscroll. */
5017 Fset_window_vscroll (window, make_number (0), Qt);
5020 itdata = bidi_shelve_cache ();
5021 /* If scroll_preserve_screen_position is non-nil, we try to set
5022 point in the same window line as it is now, so get that line. */
5023 if (!NILP (Vscroll_preserve_screen_position))
5025 /* We preserve the goal pixel coordinate across consecutive
5026 calls to scroll-up, scroll-down and other commands that
5027 have the `scroll-command' property. This avoids the
5028 possibility of point becoming "stuck" on a tall line when
5029 scrolling by one line. */
5030 if (window_scroll_pixel_based_preserve_y < 0
5031 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5032 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5034 start_display (&it, w, start);
5035 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5036 window_scroll_pixel_based_preserve_y = it.current_y;
5037 window_scroll_pixel_based_preserve_x = it.current_x;
5040 else
5041 window_scroll_pixel_based_preserve_y
5042 = window_scroll_pixel_based_preserve_x = -1;
5044 /* Move iterator it from start the specified distance forward or
5045 backward. The result is the new window start. */
5046 start_display (&it, w, start);
5047 if (whole)
5049 ptrdiff_t start_pos = IT_CHARPOS (it);
5050 int dy = frame_line_height;
5051 /* In the below we divide the window box height by the frame's
5052 line height to make the result predictable when the window
5053 box is not an integral multiple of the line height. This is
5054 important to ensure we get back to the same position when
5055 scrolling up, then down. */
5056 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
5057 dy) * n;
5059 /* Note that move_it_vertically always moves the iterator to the
5060 start of a line. So, if the last line doesn't have a newline,
5061 we would end up at the start of the line ending at ZV. */
5062 if (dy <= 0)
5064 move_it_vertically_backward (&it, -dy);
5065 /* Ensure we actually do move, e.g. in case we are currently
5066 looking at an image that is taller that the window height. */
5067 while (start_pos == IT_CHARPOS (it)
5068 && start_pos > BEGV)
5069 move_it_by_lines (&it, -1);
5071 else if (dy > 0)
5073 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5074 MOVE_TO_POS | MOVE_TO_Y);
5075 /* Ensure we actually do move, e.g. in case we are currently
5076 looking at an image that is taller that the window height. */
5077 while (start_pos == IT_CHARPOS (it)
5078 && start_pos < ZV)
5079 move_it_by_lines (&it, 1);
5082 else
5083 move_it_by_lines (&it, n);
5085 /* We failed if we find ZV is already on the screen (scrolling up,
5086 means there's nothing past the end), or if we can't start any
5087 earlier (scrolling down, means there's nothing past the top). */
5088 if ((n > 0 && IT_CHARPOS (it) == ZV)
5089 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5091 if (IT_CHARPOS (it) == ZV)
5093 if (it.current_y < it.last_visible_y
5094 && (it.current_y + it.max_ascent + it.max_descent
5095 > it.last_visible_y))
5097 /* The last line was only partially visible, make it fully
5098 visible. */
5099 w->vscroll = (it.last_visible_y
5100 - it.current_y + it.max_ascent + it.max_descent);
5101 adjust_frame_glyphs (it.f);
5103 else
5105 bidi_unshelve_cache (itdata, 0);
5106 if (noerror)
5107 return;
5108 else if (n < 0) /* could happen with empty buffers */
5109 xsignal0 (Qbeginning_of_buffer);
5110 else
5111 xsignal0 (Qend_of_buffer);
5114 else
5116 if (w->vscroll != 0)
5117 /* The first line was only partially visible, make it fully
5118 visible. */
5119 w->vscroll = 0;
5120 else
5122 bidi_unshelve_cache (itdata, 0);
5123 if (noerror)
5124 return;
5125 else
5126 xsignal0 (Qbeginning_of_buffer);
5130 /* If control gets here, then we vscrolled. */
5132 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5134 /* Don't try to change the window start below. */
5135 vscrolled = 1;
5138 if (! vscrolled)
5140 ptrdiff_t pos = IT_CHARPOS (it);
5141 ptrdiff_t bytepos;
5143 /* If in the middle of a multi-glyph character move forward to
5144 the next character. */
5145 if (in_display_vector_p (&it))
5147 ++pos;
5148 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5151 /* Set the window start, and set up the window for redisplay. */
5152 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5153 IT_BYTEPOS (it));
5154 bytepos = marker_byte_position (w->start);
5155 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5156 w->update_mode_line = 1;
5157 /* Set force_start so that redisplay_window will run the
5158 window-scroll-functions. */
5159 w->force_start = 1;
5162 /* The rest of this function uses current_y in a nonstandard way,
5163 not including the height of the header line if any. */
5164 it.current_y = it.vpos = 0;
5166 /* Move PT out of scroll margins.
5167 This code wants current_y to be zero at the window start position
5168 even if there is a header line. */
5169 window_total_lines
5170 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5171 this_scroll_margin = max (0, scroll_margin);
5172 this_scroll_margin
5173 = min (this_scroll_margin, window_total_lines / 4);
5174 this_scroll_margin *= frame_line_height;
5176 if (n > 0)
5178 /* We moved the window start towards ZV, so PT may be now
5179 in the scroll margin at the top. */
5180 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5181 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5182 && (NILP (Vscroll_preserve_screen_position)
5183 || EQ (Vscroll_preserve_screen_position, Qt)))
5184 /* We found PT at a legitimate height. Leave it alone. */
5186 else if (window_scroll_pixel_based_preserve_y >= 0)
5188 /* If we have a header line, take account of it.
5189 This is necessary because we set it.current_y to 0, above. */
5190 move_it_to (&it, -1,
5191 window_scroll_pixel_based_preserve_x,
5192 window_scroll_pixel_based_preserve_y
5193 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5194 -1, MOVE_TO_Y | MOVE_TO_X);
5195 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5197 else
5199 while (it.current_y < this_scroll_margin)
5201 int prev = it.current_y;
5202 move_it_by_lines (&it, 1);
5203 if (prev == it.current_y)
5204 break;
5206 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5209 else if (n < 0)
5211 ptrdiff_t charpos, bytepos;
5212 int partial_p;
5214 /* Save our position, for the
5215 window_scroll_pixel_based_preserve_y case. */
5216 charpos = IT_CHARPOS (it);
5217 bytepos = IT_BYTEPOS (it);
5219 /* We moved the window start towards BEGV, so PT may be now
5220 in the scroll margin at the bottom. */
5221 move_it_to (&it, PT, -1,
5222 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5223 - this_scroll_margin - 1),
5225 MOVE_TO_POS | MOVE_TO_Y);
5227 /* Save our position, in case it's correct. */
5228 charpos = IT_CHARPOS (it);
5229 bytepos = IT_BYTEPOS (it);
5231 /* If PT is in the screen line at the last fully visible line,
5232 move_it_to will stop at X = 0 in that line, because the
5233 required Y coordinate is reached there. See if we can get to
5234 PT without descending lower in Y, and if we can, it means we
5235 reached PT before the scroll margin. */
5236 if (charpos != PT)
5238 struct it it2;
5239 void *it_data;
5241 it2 = it;
5242 it_data = bidi_shelve_cache ();
5243 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5244 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5246 charpos = IT_CHARPOS (it);
5247 bytepos = IT_BYTEPOS (it);
5248 bidi_unshelve_cache (it_data, 1);
5250 else
5252 it = it2;
5253 bidi_unshelve_cache (it_data, 0);
5257 /* See if point is on a partially visible line at the end. */
5258 if (it.what == IT_EOB)
5259 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5260 else
5262 move_it_by_lines (&it, 1);
5263 partial_p = it.current_y > it.last_visible_y;
5266 if (charpos == PT && !partial_p
5267 && (NILP (Vscroll_preserve_screen_position)
5268 || EQ (Vscroll_preserve_screen_position, Qt)))
5269 /* We found PT before we found the display margin, so PT is ok. */
5271 else if (window_scroll_pixel_based_preserve_y >= 0)
5273 SET_TEXT_POS_FROM_MARKER (start, w->start);
5274 start_display (&it, w, start);
5275 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5276 here because we called start_display again and did not
5277 alter it.current_y this time. */
5278 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5279 window_scroll_pixel_based_preserve_y, -1,
5280 MOVE_TO_Y | MOVE_TO_X);
5281 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5283 else
5285 if (partial_p)
5286 /* The last line was only partially visible, so back up two
5287 lines to make sure we're on a fully visible line. */
5289 move_it_by_lines (&it, -2);
5290 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5292 else
5293 /* No, the position we saved is OK, so use it. */
5294 SET_PT_BOTH (charpos, bytepos);
5297 bidi_unshelve_cache (itdata, 0);
5299 if (adjust_old_pointm)
5300 Fset_marker (w->old_pointm,
5301 ((w == XWINDOW (selected_window))
5302 ? make_number (BUF_PT (XBUFFER (w->contents)))
5303 : Fmarker_position (w->pointm)),
5304 w->contents);
5308 /* Implementation of window_scroll that works based on screen lines.
5309 See the comment of window_scroll for parameter descriptions. */
5311 static void
5312 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5314 register struct window *w = XWINDOW (window);
5315 /* Fvertical_motion enters redisplay, which can trigger
5316 fontification, which in turn can modify buffer text (e.g., if the
5317 fontification functions replace escape sequences with faces, as
5318 in `grep-mode-font-lock-keywords'). So we use a marker to record
5319 the old point position, to prevent crashes in SET_PT_BOTH. */
5320 Lisp_Object opoint_marker = Fpoint_marker ();
5321 register ptrdiff_t pos, pos_byte;
5322 register int ht = window_internal_height (w);
5323 register Lisp_Object tem;
5324 bool lose;
5325 Lisp_Object bolp;
5326 ptrdiff_t startpos = marker_position (w->start);
5327 ptrdiff_t startbyte = marker_byte_position (w->start);
5328 Lisp_Object original_pos = Qnil;
5329 bool adjust_old_pointm = !NILP (Fequal (Fwindow_point (window),
5330 Fwindow_old_point (window)));
5332 /* If scrolling screen-fulls, compute the number of lines to
5333 scroll from the window's height. */
5334 if (whole)
5335 n *= max (1, ht - next_screen_context_lines);
5337 if (!NILP (Vscroll_preserve_screen_position))
5339 if (window_scroll_preserve_vpos <= 0
5340 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5341 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5343 struct position posit
5344 = *compute_motion (startpos, startbyte, 0, 0, 0,
5345 PT, ht, 0, -1, w->hscroll, 0, w);
5347 window_scroll_preserve_vpos = posit.vpos;
5348 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5351 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5352 make_number (window_scroll_preserve_vpos));
5355 XSETFASTINT (tem, PT);
5356 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5358 if (NILP (tem))
5360 Fvertical_motion (make_number (- (ht / 2)), window);
5361 startpos = PT;
5362 startbyte = PT_BYTE;
5365 SET_PT_BOTH (startpos, startbyte);
5366 lose = n < 0 && PT == BEGV;
5367 Fvertical_motion (make_number (n), window);
5368 pos = PT;
5369 pos_byte = PT_BYTE;
5370 bolp = Fbolp ();
5371 SET_PT_BOTH (marker_position (opoint_marker),
5372 marker_byte_position (opoint_marker));
5374 if (lose)
5376 if (noerror)
5377 return;
5378 else
5379 xsignal0 (Qbeginning_of_buffer);
5382 if (pos < ZV)
5384 /* Don't use a scroll margin that is negative or too large. */
5385 int this_scroll_margin =
5386 max (0, min (scroll_margin, w->total_lines / 4));
5388 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5389 w->start_at_line_beg = !NILP (bolp);
5390 w->update_mode_line = 1;
5391 /* Set force_start so that redisplay_window will run
5392 the window-scroll-functions. */
5393 w->force_start = 1;
5395 if (!NILP (Vscroll_preserve_screen_position)
5396 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5398 SET_PT_BOTH (pos, pos_byte);
5399 Fvertical_motion (original_pos, window);
5401 /* If we scrolled forward, put point enough lines down
5402 that it is outside the scroll margin. */
5403 else if (n > 0)
5405 int top_margin;
5407 if (this_scroll_margin > 0)
5409 SET_PT_BOTH (pos, pos_byte);
5410 Fvertical_motion (make_number (this_scroll_margin), window);
5411 top_margin = PT;
5413 else
5414 top_margin = pos;
5416 if (top_margin <= marker_position (opoint_marker))
5417 SET_PT_BOTH (marker_position (opoint_marker),
5418 marker_byte_position (opoint_marker));
5419 else if (!NILP (Vscroll_preserve_screen_position))
5421 SET_PT_BOTH (pos, pos_byte);
5422 Fvertical_motion (original_pos, window);
5424 else
5425 SET_PT (top_margin);
5427 else if (n < 0)
5429 int bottom_margin;
5431 /* If we scrolled backward, put point near the end of the window
5432 but not within the scroll margin. */
5433 SET_PT_BOTH (pos, pos_byte);
5434 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5435 if (XFASTINT (tem) == ht - this_scroll_margin)
5436 bottom_margin = PT;
5437 else
5438 bottom_margin = PT + 1;
5440 if (bottom_margin > marker_position (opoint_marker))
5441 SET_PT_BOTH (marker_position (opoint_marker),
5442 marker_byte_position (opoint_marker));
5443 else
5445 if (!NILP (Vscroll_preserve_screen_position))
5447 SET_PT_BOTH (pos, pos_byte);
5448 Fvertical_motion (original_pos, window);
5450 else
5451 Fvertical_motion (make_number (-1), window);
5455 else
5457 if (noerror)
5458 return;
5459 else
5460 xsignal0 (Qend_of_buffer);
5463 if (adjust_old_pointm)
5464 Fset_marker (w->old_pointm,
5465 ((w == XWINDOW (selected_window))
5466 ? make_number (BUF_PT (XBUFFER (w->contents)))
5467 : Fmarker_position (w->pointm)),
5468 w->contents);
5472 /* Scroll selected_window up or down. If N is nil, scroll a
5473 screen-full which is defined as the height of the window minus
5474 next_screen_context_lines. If N is the symbol `-', scroll.
5475 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5476 up. This is the guts of Fscroll_up and Fscroll_down. */
5478 static void
5479 scroll_command (Lisp_Object n, int direction)
5481 ptrdiff_t count = SPECPDL_INDEX ();
5483 eassert (eabs (direction) == 1);
5485 /* If selected window's buffer isn't current, make it current for
5486 the moment. But don't screw up if window_scroll gets an error. */
5487 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5489 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5490 Fset_buffer (XWINDOW (selected_window)->contents);
5493 if (NILP (n))
5494 window_scroll (selected_window, direction, 1, 0);
5495 else if (EQ (n, Qminus))
5496 window_scroll (selected_window, -direction, 1, 0);
5497 else
5499 n = Fprefix_numeric_value (n);
5500 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5503 unbind_to (count, Qnil);
5506 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5507 doc: /* Scroll text of selected window upward ARG lines.
5508 If ARG is omitted or nil, scroll upward by a near full screen.
5509 A near full screen is `next-screen-context-lines' less than a full screen.
5510 Negative ARG means scroll downward.
5511 If ARG is the atom `-', scroll downward by nearly full screen.
5512 When calling from a program, supply as argument a number, nil, or `-'. */)
5513 (Lisp_Object arg)
5515 scroll_command (arg, 1);
5516 return Qnil;
5519 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5520 doc: /* Scroll text of selected window down ARG lines.
5521 If ARG is omitted or nil, scroll down by a near full screen.
5522 A near full screen is `next-screen-context-lines' less than a full screen.
5523 Negative ARG means scroll upward.
5524 If ARG is the atom `-', scroll upward by nearly full screen.
5525 When calling from a program, supply as argument a number, nil, or `-'. */)
5526 (Lisp_Object arg)
5528 scroll_command (arg, -1);
5529 return Qnil;
5532 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5533 doc: /* Return the other window for \"other window scroll\" commands.
5534 If `other-window-scroll-buffer' is non-nil, a window
5535 showing that buffer is used.
5536 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5537 specifies the window. This takes precedence over
5538 `other-window-scroll-buffer'. */)
5539 (void)
5541 Lisp_Object window;
5543 if (MINI_WINDOW_P (XWINDOW (selected_window))
5544 && !NILP (Vminibuf_scroll_window))
5545 window = Vminibuf_scroll_window;
5546 /* If buffer is specified and live, scroll that buffer. */
5547 else if (!NILP (Vother_window_scroll_buffer)
5548 && BUFFERP (Vother_window_scroll_buffer)
5549 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5551 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5552 if (NILP (window))
5553 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5555 else
5557 /* Nothing specified; look for a neighboring window on the same
5558 frame. */
5559 window = Fnext_window (selected_window, Qnil, Qnil);
5561 if (EQ (window, selected_window))
5562 /* That didn't get us anywhere; look for a window on another
5563 visible frame. */
5565 window = Fnext_window (window, Qnil, Qt);
5566 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5567 && ! EQ (window, selected_window));
5570 CHECK_LIVE_WINDOW (window);
5572 if (EQ (window, selected_window))
5573 error ("There is no other window");
5575 return window;
5578 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5579 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5580 A near full screen is `next-screen-context-lines' less than a full screen.
5581 The next window is the one below the current one; or the one at the top
5582 if the current one is at the bottom. Negative ARG means scroll downward.
5583 If ARG is the atom `-', scroll downward by nearly full screen.
5584 When calling from a program, supply as argument a number, nil, or `-'.
5586 If `other-window-scroll-buffer' is non-nil, scroll the window
5587 showing that buffer, popping the buffer up if necessary.
5588 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5589 specifies the window to scroll. This takes precedence over
5590 `other-window-scroll-buffer'. */)
5591 (Lisp_Object arg)
5593 Lisp_Object window;
5594 struct window *w;
5595 ptrdiff_t count = SPECPDL_INDEX ();
5597 window = Fother_window_for_scrolling ();
5598 w = XWINDOW (window);
5600 /* Don't screw up if window_scroll gets an error. */
5601 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5603 Fset_buffer (w->contents);
5604 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5605 SET_PT_BOTH (marker_position (w->old_pointm), marker_byte_position (w->old_pointm));
5607 if (NILP (arg))
5608 window_scroll (window, 1, 1, 1);
5609 else if (EQ (arg, Qminus))
5610 window_scroll (window, -1, 1, 1);
5611 else
5613 if (CONSP (arg))
5614 arg = XCAR (arg);
5615 CHECK_NUMBER (arg);
5616 window_scroll (window, XINT (arg), 0, 1);
5619 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5620 set_marker_both (w->old_pointm, Qnil, PT, PT_BYTE);
5621 unbind_to (count, Qnil);
5623 return Qnil;
5626 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5627 doc: /* Scroll selected window display ARG columns left.
5628 Default for ARG is window width minus 2.
5629 Value is the total amount of leftward horizontal scrolling in
5630 effect after the change.
5631 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5632 lower bound for automatic scrolling, i.e. automatic scrolling
5633 will not scroll a window to a column less than the value returned
5634 by this function. This happens in an interactive call. */)
5635 (register Lisp_Object arg, Lisp_Object set_minimum)
5637 struct window *w = XWINDOW (selected_window);
5638 EMACS_INT requested_arg = (NILP (arg)
5639 ? window_body_width (w, 0) - 2
5640 : XINT (Fprefix_numeric_value (arg)));
5641 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5643 if (!NILP (set_minimum))
5644 w->min_hscroll = w->hscroll;
5646 w->suspend_auto_hscroll = 1;
5648 return result;
5651 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5652 doc: /* Scroll selected window display ARG columns right.
5653 Default for ARG is window width minus 2.
5654 Value is the total amount of leftward horizontal scrolling in
5655 effect after the change.
5656 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5657 lower bound for automatic scrolling, i.e. automatic scrolling
5658 will not scroll a window to a column less than the value returned
5659 by this function. This happens in an interactive call. */)
5660 (register Lisp_Object arg, Lisp_Object set_minimum)
5662 struct window *w = XWINDOW (selected_window);
5663 EMACS_INT requested_arg = (NILP (arg)
5664 ? window_body_width (w, 0) - 2
5665 : XINT (Fprefix_numeric_value (arg)));
5666 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5668 if (!NILP (set_minimum))
5669 w->min_hscroll = w->hscroll;
5671 w->suspend_auto_hscroll = 1;
5673 return result;
5676 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5677 doc: /* Return the window which was selected when entering the minibuffer.
5678 Returns nil, if selected window is not a minibuffer window. */)
5679 (void)
5681 if (minibuf_level > 0
5682 && MINI_WINDOW_P (XWINDOW (selected_window))
5683 && WINDOW_LIVE_P (minibuf_selected_window))
5684 return minibuf_selected_window;
5686 return Qnil;
5689 /* Value is the number of lines actually displayed in window W,
5690 as opposed to its height. */
5692 static int
5693 displayed_window_lines (struct window *w)
5695 struct it it;
5696 struct text_pos start;
5697 int height = window_box_height (w);
5698 struct buffer *old_buffer;
5699 int bottom_y;
5700 void *itdata = NULL;
5702 if (XBUFFER (w->contents) != current_buffer)
5704 old_buffer = current_buffer;
5705 set_buffer_internal (XBUFFER (w->contents));
5707 else
5708 old_buffer = NULL;
5710 /* In case W->start is out of the accessible range, do something
5711 reasonable. This happens in Info mode when Info-scroll-down
5712 calls (recenter -1) while W->start is 1. */
5713 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5715 itdata = bidi_shelve_cache ();
5716 start_display (&it, w, start);
5717 move_it_vertically (&it, height);
5718 bottom_y = line_bottom_y (&it);
5719 bidi_unshelve_cache (itdata, 0);
5721 /* rms: On a non-window display,
5722 the value of it.vpos at the bottom of the screen
5723 seems to be 1 larger than window_box_height (w).
5724 This kludge fixes a bug whereby (move-to-window-line -1)
5725 when ZV is on the last screen line
5726 moves to the previous screen line instead of the last one. */
5727 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5728 height++;
5730 /* Add in empty lines at the bottom of the window. */
5731 if (bottom_y < height)
5733 int uy = FRAME_LINE_HEIGHT (it.f);
5734 it.vpos += (height - bottom_y + uy - 1) / uy;
5737 if (old_buffer)
5738 set_buffer_internal (old_buffer);
5740 return it.vpos;
5744 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5745 doc: /* Center point in selected window and maybe redisplay frame.
5746 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5747 relative to the selected window. If ARG is negative, it counts up from the
5748 bottom of the window. (ARG should be less than the height of the window.)
5750 If ARG is omitted or nil, then recenter with point on the middle line of
5751 the selected window; if the variable `recenter-redisplay' is non-nil,
5752 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5753 is set to `grow-only', this resets the tool-bar's height to the minimum
5754 height needed); if `recenter-redisplay' has the special value `tty',
5755 then only tty frames are redrawn.
5757 Just C-u as prefix means put point in the center of the window
5758 and redisplay normally--don't erase and redraw the frame. */)
5759 (register Lisp_Object arg)
5761 struct window *w = XWINDOW (selected_window);
5762 struct buffer *buf = XBUFFER (w->contents);
5763 bool center_p = 0;
5764 ptrdiff_t charpos, bytepos;
5765 EMACS_INT iarg IF_LINT (= 0);
5766 int this_scroll_margin;
5768 if (buf != current_buffer)
5769 error ("`recenter'ing a window that does not display current-buffer.");
5771 /* If redisplay is suppressed due to an error, try again. */
5772 buf->display_error_modiff = 0;
5774 if (NILP (arg))
5776 if (!NILP (Vrecenter_redisplay)
5777 && (!EQ (Vrecenter_redisplay, Qtty)
5778 || !NILP (Ftty_type (selected_frame))))
5780 ptrdiff_t i;
5782 /* Invalidate pixel data calculated for all compositions. */
5783 for (i = 0; i < n_compositions; i++)
5784 composition_table[i]->font = NULL;
5785 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5786 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5787 #endif
5788 Fredraw_frame (WINDOW_FRAME (w));
5789 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5792 center_p = 1;
5794 else if (CONSP (arg)) /* Just C-u. */
5795 center_p = 1;
5796 else
5798 arg = Fprefix_numeric_value (arg);
5799 CHECK_NUMBER (arg);
5800 iarg = XINT (arg);
5803 /* Do this after making BUF current
5804 in case scroll_margin is buffer-local. */
5805 this_scroll_margin
5806 = max (0, min (scroll_margin, w->total_lines / 4));
5808 /* Handle centering on a graphical frame specially. Such frames can
5809 have variable-height lines and centering point on the basis of
5810 line counts would lead to strange effects. */
5811 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5813 if (center_p)
5815 struct it it;
5816 struct text_pos pt;
5817 void *itdata = bidi_shelve_cache ();
5819 SET_TEXT_POS (pt, PT, PT_BYTE);
5820 start_display (&it, w, pt);
5821 move_it_vertically_backward (&it, window_box_height (w) / 2);
5822 charpos = IT_CHARPOS (it);
5823 bytepos = IT_BYTEPOS (it);
5824 bidi_unshelve_cache (itdata, 0);
5826 else if (iarg < 0)
5828 struct it it;
5829 struct text_pos pt;
5830 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5831 int extra_line_spacing;
5832 int h = window_box_height (w);
5833 void *itdata = bidi_shelve_cache ();
5835 iarg = - max (-iarg, this_scroll_margin);
5837 SET_TEXT_POS (pt, PT, PT_BYTE);
5838 start_display (&it, w, pt);
5840 /* Be sure we have the exact height of the full line containing PT. */
5841 move_it_by_lines (&it, 0);
5843 /* The amount of pixels we have to move back is the window
5844 height minus what's displayed in the line containing PT,
5845 and the lines below. */
5846 it.current_y = 0;
5847 it.vpos = 0;
5848 move_it_by_lines (&it, nlines);
5850 if (it.vpos == nlines)
5851 h -= it.current_y;
5852 else
5854 /* Last line has no newline. */
5855 h -= line_bottom_y (&it);
5856 it.vpos++;
5859 /* Don't reserve space for extra line spacing of last line. */
5860 extra_line_spacing = it.max_extra_line_spacing;
5862 /* If we can't move down NLINES lines because we hit
5863 the end of the buffer, count in some empty lines. */
5864 if (it.vpos < nlines)
5866 nlines -= it.vpos;
5867 extra_line_spacing = it.extra_line_spacing;
5868 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5870 if (h <= 0)
5872 bidi_unshelve_cache (itdata, 0);
5873 return Qnil;
5876 /* Now find the new top line (starting position) of the window. */
5877 start_display (&it, w, pt);
5878 it.current_y = 0;
5879 move_it_vertically_backward (&it, h);
5881 /* If extra line spacing is present, we may move too far
5882 back. This causes the last line to be only partially
5883 visible (which triggers redisplay to recenter that line
5884 in the middle), so move forward.
5885 But ignore extra line spacing on last line, as it is not
5886 considered to be part of the visible height of the line.
5888 h += extra_line_spacing;
5889 while (-it.current_y > h)
5890 move_it_by_lines (&it, 1);
5892 charpos = IT_CHARPOS (it);
5893 bytepos = IT_BYTEPOS (it);
5895 bidi_unshelve_cache (itdata, 0);
5897 else
5899 struct position pos;
5901 iarg = max (iarg, this_scroll_margin);
5903 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5904 charpos = pos.bufpos;
5905 bytepos = pos.bytepos;
5908 else
5910 struct position pos;
5911 int ht = window_internal_height (w);
5913 if (center_p)
5914 iarg = ht / 2;
5915 else if (iarg < 0)
5916 iarg += ht;
5918 /* Don't let it get into the margin at either top or bottom. */
5919 iarg = clip_to_bounds (this_scroll_margin, iarg,
5920 ht - this_scroll_margin - 1);
5922 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5923 charpos = pos.bufpos;
5924 bytepos = pos.bytepos;
5927 /* Set the new window start. */
5928 set_marker_both (w->start, w->contents, charpos, bytepos);
5929 w->window_end_valid = 0;
5931 w->optional_new_start = 1;
5933 w->start_at_line_beg = (bytepos == BEGV_BYTE
5934 || FETCH_BYTE (bytepos - 1) == '\n');
5936 wset_redisplay (w);
5938 return Qnil;
5941 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5942 0, 2, 0,
5943 doc: /* Return the width in columns of the text display area of WINDOW.
5944 WINDOW must be a live window and defaults to the selected one.
5946 The returned width does not include dividers, scrollbars, margins,
5947 fringes, nor any partial-width columns at the right of the text
5948 area.
5950 Optional argument PIXELWISE non-nil, means to return the width in
5951 pixels. */)
5952 (Lisp_Object window, Lisp_Object pixelwise)
5954 struct window *w = decode_live_window (window);
5956 if (NILP (pixelwise))
5957 return make_number (window_box_width (w, TEXT_AREA)
5958 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5959 else
5960 return make_number (window_box_width (w, TEXT_AREA));
5963 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5964 0, 2, 0,
5965 doc: /* Return the height in lines of the text display area of WINDOW.
5966 WINDOW must be a live window and defaults to the selected one.
5968 The returned height does not include dividers, the mode line, any header
5969 line, nor any partial-height lines at the bottom of the text area.
5971 Optional argument PIXELWISE non-nil, means to return the height in
5972 pixels. */)
5973 (Lisp_Object window, Lisp_Object pixelwise)
5975 struct window *w = decode_live_window (window);
5977 if (NILP (pixelwise))
5978 return make_number (window_box_height (w)
5979 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5980 else
5981 return make_number (window_box_height (w));
5984 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5985 1, 1, "P",
5986 doc: /* Position point relative to window.
5987 ARG nil means position point at center of window.
5988 Else, ARG specifies vertical position within the window;
5989 zero means top of window, negative means relative to bottom of window. */)
5990 (Lisp_Object arg)
5992 struct window *w = XWINDOW (selected_window);
5993 int lines, start;
5994 Lisp_Object window;
5995 #if 0
5996 int this_scroll_margin;
5997 #endif
5999 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
6000 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
6001 when passed below to set_marker_both. */
6002 error ("move-to-window-line called from unrelated buffer");
6004 window = selected_window;
6005 start = marker_position (w->start);
6006 if (start < BEGV || start > ZV)
6008 int height = window_internal_height (w);
6009 Fvertical_motion (make_number (- (height / 2)), window);
6010 set_marker_both (w->start, w->contents, PT, PT_BYTE);
6011 w->start_at_line_beg = !NILP (Fbolp ());
6012 w->force_start = 1;
6014 else
6015 Fgoto_char (w->start);
6017 lines = displayed_window_lines (w);
6019 #if 0
6020 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
6021 #endif
6023 if (NILP (arg))
6024 XSETFASTINT (arg, lines / 2);
6025 else
6027 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
6029 if (iarg < 0)
6030 iarg = iarg + lines;
6032 #if 0 /* This code would prevent move-to-window-line from moving point
6033 to a place inside the scroll margins (which would cause the
6034 next redisplay to scroll). I wrote this code, but then concluded
6035 it is probably better not to install it. However, it is here
6036 inside #if 0 so as not to lose it. -- rms. */
6038 /* Don't let it get into the margin at either top or bottom. */
6039 iarg = max (iarg, this_scroll_margin);
6040 iarg = min (iarg, lines - this_scroll_margin - 1);
6041 #endif
6043 arg = make_number (iarg);
6046 /* Skip past a partially visible first line. */
6047 if (w->vscroll)
6048 XSETINT (arg, XINT (arg) + 1);
6050 return Fvertical_motion (arg, window);
6055 /***********************************************************************
6056 Window Configuration
6057 ***********************************************************************/
6059 struct save_window_data
6061 struct vectorlike_header header;
6062 Lisp_Object selected_frame;
6063 Lisp_Object current_window;
6064 Lisp_Object current_buffer;
6065 Lisp_Object minibuf_scroll_window;
6066 Lisp_Object minibuf_selected_window;
6067 Lisp_Object root_window;
6068 Lisp_Object focus_frame;
6069 /* A vector, each of whose elements is a struct saved_window
6070 for one window. */
6071 Lisp_Object saved_windows;
6073 /* All fields above are traced by the GC.
6074 From `frame-cols' down, the fields are ignored by the GC. */
6075 /* We should be able to do without the following two. */
6076 int frame_cols, frame_lines;
6077 /* These two should get eventually replaced by their pixel
6078 counterparts. */
6079 int frame_menu_bar_lines, frame_tool_bar_lines;
6080 int frame_text_width, frame_text_height;
6081 /* These are currently unused. We need them as soon as we convert
6082 to pixels. */
6083 int frame_menu_bar_height, frame_tool_bar_height;
6086 /* This is saved as a Lisp_Vector. */
6087 struct saved_window
6089 struct vectorlike_header header;
6091 Lisp_Object window, buffer, start, pointm, old_pointm;
6092 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6093 Lisp_Object left_col, top_line, total_cols, total_lines;
6094 Lisp_Object normal_cols, normal_lines;
6095 Lisp_Object hscroll, min_hscroll, hscroll_whole, suspend_auto_hscroll;
6096 Lisp_Object parent, prev;
6097 Lisp_Object start_at_line_beg;
6098 Lisp_Object display_table;
6099 Lisp_Object left_margin_cols, right_margin_cols;
6100 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6101 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6102 Lisp_Object scroll_bar_height, horizontal_scroll_bar_type;
6103 Lisp_Object combination_limit, window_parameters;
6106 #define SAVED_WINDOW_N(swv,n) \
6107 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6109 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6110 doc: /* Return t if OBJECT is a window-configuration object. */)
6111 (Lisp_Object object)
6113 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6116 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6117 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6118 (Lisp_Object config)
6120 register struct save_window_data *data;
6121 struct Lisp_Vector *saved_windows;
6123 CHECK_WINDOW_CONFIGURATION (config);
6125 data = (struct save_window_data *) XVECTOR (config);
6126 saved_windows = XVECTOR (data->saved_windows);
6127 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6130 DEFUN ("set-window-configuration", Fset_window_configuration,
6131 Sset_window_configuration, 1, 1, 0,
6132 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6133 CONFIGURATION must be a value previously returned
6134 by `current-window-configuration' (which see).
6135 If CONFIGURATION was made from a frame that is now deleted,
6136 only frame-independent values can be restored. In this case,
6137 the return value is nil. Otherwise the value is t. */)
6138 (Lisp_Object configuration)
6140 register struct save_window_data *data;
6141 struct Lisp_Vector *saved_windows;
6142 Lisp_Object new_current_buffer;
6143 Lisp_Object frame;
6144 struct frame *f;
6145 ptrdiff_t old_point = -1;
6146 USE_SAFE_ALLOCA;
6148 CHECK_WINDOW_CONFIGURATION (configuration);
6150 data = (struct save_window_data *) XVECTOR (configuration);
6151 saved_windows = XVECTOR (data->saved_windows);
6153 new_current_buffer = data->current_buffer;
6154 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6155 new_current_buffer = Qnil;
6156 else
6158 if (XBUFFER (new_current_buffer) == current_buffer)
6159 /* The code further down "preserves point" by saving here PT in
6160 old_point and then setting it later back into PT. When the
6161 current-selected-window and the final-selected-window both show
6162 the current buffer, this suffers from the problem that the
6163 current PT is the window-point of the current-selected-window,
6164 while the final PT is the point of the final-selected-window, so
6165 this copy from one PT to the other would end up moving the
6166 window-point of the final-selected-window to the window-point of
6167 the current-selected-window. So we have to be careful which
6168 point of the current-buffer we copy into old_point. */
6169 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6170 && WINDOWP (selected_window)
6171 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6172 && !EQ (selected_window, data->current_window))
6173 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6174 else
6175 old_point = PT;
6176 else
6177 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6178 point in new_current_buffer as of the last time this buffer was
6179 used. This can be non-deterministic since it can be changed by
6180 things like jit-lock by mere temporary selection of some random
6181 window that happens to show this buffer.
6182 So if possible we want this arbitrary choice of "which point" to
6183 be the one from the to-be-selected-window so as to prevent this
6184 window's cursor from being copied from another window. */
6185 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6186 /* If current_window = selected_window, its point is in BUF_PT. */
6187 && !EQ (selected_window, data->current_window))
6188 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6189 else
6190 old_point = BUF_PT (XBUFFER (new_current_buffer));
6193 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6194 f = XFRAME (frame);
6196 /* If f is a dead frame, don't bother rebuilding its window tree.
6197 However, there is other stuff we should still try to do below. */
6198 if (FRAME_LIVE_P (f))
6200 Lisp_Object window;
6201 Lisp_Object dead_windows = Qnil;
6202 register Lisp_Object tem, par, pers;
6203 register struct window *w;
6204 register struct saved_window *p;
6205 struct window *root_window;
6206 struct window **leaf_windows;
6207 int n_leaf_windows;
6208 ptrdiff_t k;
6209 int i, n;
6211 /* Don't do this within the main loop below: This may call Lisp
6212 code and is thus potentially unsafe while input is blocked. */
6213 for (k = 0; k < saved_windows->header.size; k++)
6215 p = SAVED_WINDOW_N (saved_windows, k);
6216 window = p->window;
6217 w = XWINDOW (window);
6218 if (BUFFERP (w->contents)
6219 && !EQ (w->contents, p->buffer)
6220 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6221 /* If a window we restore gets another buffer, record the
6222 window's old buffer. */
6223 call1 (Qrecord_window_buffer, window);
6226 /* Disallow x_set_window_size, temporarily. */
6227 f->can_x_set_window_size = false;
6228 /* The mouse highlighting code could get screwed up
6229 if it runs during this. */
6230 block_input ();
6232 /* "Swap out" point from the selected window's buffer
6233 into the window itself. (Normally the pointm of the selected
6234 window holds garbage.) We do this now, before
6235 restoring the window contents, and prevent it from
6236 being done later on when we select a new window. */
6237 if (! NILP (XWINDOW (selected_window)->contents))
6239 w = XWINDOW (selected_window);
6240 set_marker_both (w->pointm,
6241 w->contents,
6242 BUF_PT (XBUFFER (w->contents)),
6243 BUF_PT_BYTE (XBUFFER (w->contents)));
6246 fset_redisplay (f);
6247 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6249 /* Problem: Freeing all matrices and later allocating them again
6250 is a serious redisplay flickering problem. What we would
6251 really like to do is to free only those matrices not reused
6252 below. */
6253 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6254 int nwindows = count_windows (root_window);
6255 SAFE_NALLOCA (leaf_windows, 1, nwindows);
6256 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6258 /* Kludge Alert!
6259 Mark all windows now on frame as "deleted".
6260 Restoring the new configuration "undeletes" any that are in it.
6262 Save their current buffers in their height fields, since we may
6263 need it later, if a buffer saved in the configuration is now
6264 dead. */
6265 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6267 for (k = 0; k < saved_windows->header.size; k++)
6269 p = SAVED_WINDOW_N (saved_windows, k);
6270 window = p->window;
6271 w = XWINDOW (window);
6272 wset_next (w, Qnil);
6274 if (!NILP (p->parent))
6275 wset_parent
6276 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6277 else
6278 wset_parent (w, Qnil);
6280 if (!NILP (p->prev))
6282 wset_prev
6283 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6284 wset_next (XWINDOW (w->prev), p->window);
6286 else
6288 wset_prev (w, Qnil);
6289 if (!NILP (w->parent))
6290 wset_combination (XWINDOW (w->parent),
6291 (XINT (p->total_cols)
6292 != XWINDOW (w->parent)->total_cols),
6293 p->window);
6296 /* If we squirreled away the buffer, restore it now. */
6297 if (BUFFERP (w->combination_limit))
6298 wset_buffer (w, w->combination_limit);
6299 w->pixel_left = XFASTINT (p->pixel_left);
6300 w->pixel_top = XFASTINT (p->pixel_top);
6301 w->pixel_width = XFASTINT (p->pixel_width);
6302 w->pixel_height = XFASTINT (p->pixel_height);
6303 w->left_col = XFASTINT (p->left_col);
6304 w->top_line = XFASTINT (p->top_line);
6305 w->total_cols = XFASTINT (p->total_cols);
6306 w->total_lines = XFASTINT (p->total_lines);
6307 wset_normal_cols (w, p->normal_cols);
6308 wset_normal_lines (w, p->normal_lines);
6309 w->hscroll = XFASTINT (p->hscroll);
6310 w->suspend_auto_hscroll = !NILP (p->suspend_auto_hscroll);
6311 w->min_hscroll = XFASTINT (p->min_hscroll);
6312 w->hscroll_whole = XFASTINT (p->hscroll_whole);
6313 wset_display_table (w, p->display_table);
6314 w->left_margin_cols = XINT (p->left_margin_cols);
6315 w->right_margin_cols = XINT (p->right_margin_cols);
6316 w->left_fringe_width = XINT (p->left_fringe_width);
6317 w->right_fringe_width = XINT (p->right_fringe_width);
6318 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6319 w->scroll_bar_width = XINT (p->scroll_bar_width);
6320 w->scroll_bar_height = XINT (p->scroll_bar_height);
6321 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6322 wset_horizontal_scroll_bar_type (w, p->horizontal_scroll_bar_type);
6323 wset_dedicated (w, p->dedicated);
6324 wset_combination_limit (w, p->combination_limit);
6325 /* Restore any window parameters that have been saved.
6326 Parameters that have not been saved are left alone. */
6327 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6329 pers = XCAR (tem);
6330 if (CONSP (pers))
6332 if (NILP (XCDR (pers)))
6334 par = Fassq (XCAR (pers), w->window_parameters);
6335 if (CONSP (par) && !NILP (XCDR (par)))
6336 /* Reset a parameter to nil if and only if it
6337 has a non-nil association. Don't make new
6338 associations. */
6339 Fsetcdr (par, Qnil);
6341 else
6342 /* Always restore a non-nil value. */
6343 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6347 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6348 /* If saved buffer is alive, install it. */
6350 wset_buffer (w, p->buffer);
6351 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6352 set_marker_restricted (w->start, p->start, w->contents);
6353 set_marker_restricted (w->pointm, p->pointm, w->contents);
6354 set_marker_restricted (w->old_pointm, p->old_pointm, w->contents);
6355 /* As documented in Fcurrent_window_configuration, don't
6356 restore the location of point in the buffer which was
6357 current when the window configuration was recorded. */
6358 if (!EQ (p->buffer, new_current_buffer)
6359 && XBUFFER (p->buffer) == current_buffer)
6360 Fgoto_char (w->pointm);
6362 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6363 /* Keep window's old buffer; make sure the markers are real. */
6365 /* Set window markers at start of visible range. */
6366 if (XMARKER (w->start)->buffer == 0)
6367 set_marker_restricted_both (w->start, w->contents, 0, 0);
6368 if (XMARKER (w->pointm)->buffer == 0)
6369 set_marker_restricted_both
6370 (w->pointm, w->contents,
6371 BUF_PT (XBUFFER (w->contents)),
6372 BUF_PT_BYTE (XBUFFER (w->contents)));
6373 if (XMARKER (w->old_pointm)->buffer == 0)
6374 set_marker_restricted_both
6375 (w->old_pointm, w->contents,
6376 BUF_PT (XBUFFER (w->contents)),
6377 BUF_PT_BYTE (XBUFFER (w->contents)));
6378 w->start_at_line_beg = 1;
6380 else if (!NILP (w->start))
6381 /* Leaf window has no live buffer, get one. */
6383 /* Get the buffer via other_buffer_safely in order to
6384 avoid showing an unimportant buffer and, if necessary, to
6385 recreate *scratch* in the course (part of Juanma's bs-show
6386 scenario from March 2011). */
6387 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6388 /* This will set the markers to beginning of visible
6389 range. */
6390 set_marker_restricted_both (w->start, w->contents, 0, 0);
6391 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6392 set_marker_restricted_both (w->old_pointm, w->contents, 0, 0);
6393 w->start_at_line_beg = 1;
6394 if (!NILP (w->dedicated))
6395 /* Record this window as dead. */
6396 dead_windows = Fcons (window, dead_windows);
6397 /* Make sure window is no more dedicated. */
6398 wset_dedicated (w, Qnil);
6402 fset_root_window (f, data->root_window);
6403 /* Arrange *not* to restore point in the buffer that was
6404 current when the window configuration was saved. */
6405 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6406 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6407 make_number (old_point),
6408 XWINDOW (data->current_window)->contents);
6410 /* In the following call to `select-window', prevent "swapping out
6411 point" in the old selected window using the buffer that has
6412 been restored into it. We already swapped out that point from
6413 that window's old buffer.
6415 Do not record the buffer here. We do that in a separate call
6416 to select_window below. See also Bug#16207. */
6417 select_window (data->current_window, Qt, 1);
6418 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6419 last_selected_window)
6420 = selected_window;
6422 if (NILP (data->focus_frame)
6423 || (FRAMEP (data->focus_frame)
6424 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6425 Fredirect_frame_focus (frame, data->focus_frame);
6427 /* Now, free glyph matrices in windows that were not reused. */
6428 for (i = n = 0; i < n_leaf_windows; ++i)
6430 if (NILP (leaf_windows[i]->contents))
6431 free_window_matrices (leaf_windows[i]);
6432 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6433 ++n;
6436 /* Allow x_set_window_size again and apply frame size changes if
6437 needed. */
6438 f->can_x_set_window_size = true;
6439 adjust_frame_size (f, -1, -1, 1, 0, Qnil);
6441 adjust_frame_glyphs (f);
6442 unblock_input ();
6444 /* Scan dead buffer windows. */
6445 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6447 window = XCAR (dead_windows);
6448 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6449 delete_deletable_window (window);
6452 /* Record the selected window's buffer here. The window should
6453 already be the selected one from the call above. */
6454 select_window (data->current_window, Qnil, 0);
6456 /* Fselect_window will have made f the selected frame, so we
6457 reselect the proper frame here. Fhandle_switch_frame will change the
6458 selected window too, but that doesn't make the call to
6459 Fselect_window above totally superfluous; it still sets f's
6460 selected window. */
6461 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6462 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6464 run_window_configuration_change_hook (f);
6467 if (!NILP (new_current_buffer))
6469 Fset_buffer (new_current_buffer);
6470 /* If the new current buffer doesn't appear in the selected
6471 window, go to its old point (see bug#12208). */
6472 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6473 Fgoto_char (make_number (old_point));
6476 Vminibuf_scroll_window = data->minibuf_scroll_window;
6477 minibuf_selected_window = data->minibuf_selected_window;
6479 SAFE_FREE ();
6480 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6484 void
6485 restore_window_configuration (Lisp_Object configuration)
6487 Fset_window_configuration (configuration);
6491 /* If WINDOW is an internal window, recursively delete all child windows
6492 reachable via the next and contents slots of WINDOW. Otherwise setup
6493 WINDOW to not show any buffer. */
6495 void
6496 delete_all_child_windows (Lisp_Object window)
6498 register struct window *w;
6500 w = XWINDOW (window);
6502 if (!NILP (w->next))
6503 /* Delete WINDOW's siblings (we traverse postorderly). */
6504 delete_all_child_windows (w->next);
6506 if (WINDOWP (w->contents))
6508 delete_all_child_windows (w->contents);
6509 wset_combination (w, 0, Qnil);
6511 else if (BUFFERP (w->contents))
6513 unshow_buffer (w);
6514 unchain_marker (XMARKER (w->pointm));
6515 unchain_marker (XMARKER (w->old_pointm));
6516 unchain_marker (XMARKER (w->start));
6517 /* Since combination limit makes sense for an internal windows
6518 only, we use this slot to save the buffer for the sake of
6519 possible resurrection in Fset_window_configuration. */
6520 wset_combination_limit (w, w->contents);
6521 wset_buffer (w, Qnil);
6524 Vwindow_list = Qnil;
6527 static int
6528 count_windows (register struct window *window)
6530 register int count = 1;
6531 if (!NILP (window->next))
6532 count += count_windows (XWINDOW (window->next));
6533 if (WINDOWP (window->contents))
6534 count += count_windows (XWINDOW (window->contents));
6535 return count;
6539 /* Fill vector FLAT with leaf windows under W, starting at index I.
6540 Value is last index + 1. */
6541 static int
6542 get_leaf_windows (struct window *w, struct window **flat, int i)
6544 while (w)
6546 if (WINDOWP (w->contents))
6547 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6548 else
6549 flat[i++] = w;
6551 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6554 return i;
6558 /* Return a pointer to the glyph W's physical cursor is on. Value is
6559 null if W's current matrix is invalid, so that no meaningful glyph
6560 can be returned. */
6561 struct glyph *
6562 get_phys_cursor_glyph (struct window *w)
6564 struct glyph_row *row;
6565 struct glyph *glyph;
6566 int hpos = w->phys_cursor.hpos;
6568 if (!(w->phys_cursor.vpos >= 0
6569 && w->phys_cursor.vpos < w->current_matrix->nrows))
6570 return NULL;
6572 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6573 if (!row->enabled_p)
6574 return NULL;
6576 if (w->hscroll)
6578 /* When the window is hscrolled, cursor hpos can legitimately be
6579 out of bounds, but we draw the cursor at the corresponding
6580 window margin in that case. */
6581 if (!row->reversed_p && hpos < 0)
6582 hpos = 0;
6583 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6584 hpos = row->used[TEXT_AREA] - 1;
6587 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6588 glyph = row->glyphs[TEXT_AREA] + hpos;
6589 else
6590 glyph = NULL;
6592 return glyph;
6596 static int
6597 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6599 register struct saved_window *p;
6600 register struct window *w;
6601 register Lisp_Object tem, pers, par;
6603 for (; !NILP (window); window = w->next)
6605 p = SAVED_WINDOW_N (vector, i);
6606 w = XWINDOW (window);
6608 wset_temslot (w, make_number (i)); i++;
6609 p->window = window;
6610 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6611 p->pixel_left = make_number (w->pixel_left);
6612 p->pixel_top = make_number (w->pixel_top);
6613 p->pixel_width = make_number (w->pixel_width);
6614 p->pixel_height = make_number (w->pixel_height);
6615 p->left_col = make_number (w->left_col);
6616 p->top_line = make_number (w->top_line);
6617 p->total_cols = make_number (w->total_cols);
6618 p->total_lines = make_number (w->total_lines);
6619 p->normal_cols = w->normal_cols;
6620 p->normal_lines = w->normal_lines;
6621 XSETFASTINT (p->hscroll, w->hscroll);
6622 p->suspend_auto_hscroll = w->suspend_auto_hscroll ? Qt : Qnil;
6623 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6624 XSETFASTINT (p->hscroll_whole, w->hscroll_whole);
6625 p->display_table = w->display_table;
6626 p->left_margin_cols = make_number (w->left_margin_cols);
6627 p->right_margin_cols = make_number (w->right_margin_cols);
6628 p->left_fringe_width = make_number (w->left_fringe_width);
6629 p->right_fringe_width = make_number (w->right_fringe_width);
6630 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6631 p->scroll_bar_width = make_number (w->scroll_bar_width);
6632 p->scroll_bar_height = make_number (w->scroll_bar_height);
6633 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6634 p->horizontal_scroll_bar_type = w->horizontal_scroll_bar_type;
6635 p->dedicated = w->dedicated;
6636 p->combination_limit = w->combination_limit;
6637 p->window_parameters = Qnil;
6639 if (!NILP (Vwindow_persistent_parameters))
6641 /* Run cycle detection on Vwindow_persistent_parameters. */
6642 Lisp_Object tortoise, hare;
6644 hare = tortoise = Vwindow_persistent_parameters;
6645 while (CONSP (hare))
6647 hare = XCDR (hare);
6648 if (!CONSP (hare))
6649 break;
6651 hare = XCDR (hare);
6652 tortoise = XCDR (tortoise);
6654 if (EQ (hare, tortoise))
6655 /* Reset Vwindow_persistent_parameters to Qnil. */
6657 Vwindow_persistent_parameters = Qnil;
6658 break;
6662 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6663 tem = XCDR (tem))
6665 pers = XCAR (tem);
6666 /* Save values for persistent window parameters. */
6667 if (CONSP (pers) && !NILP (XCDR (pers)))
6669 par = Fassq (XCAR (pers), w->window_parameters);
6670 if (NILP (par))
6671 /* If the window has no value for the parameter,
6672 make one. */
6673 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6674 p->window_parameters);
6675 else
6676 /* If the window has a value for the parameter,
6677 save it. */
6678 p->window_parameters = Fcons (Fcons (XCAR (par),
6679 XCDR (par)),
6680 p->window_parameters);
6685 if (BUFFERP (w->contents))
6687 /* Save w's value of point in the window configuration. If w
6688 is the selected window, then get the value of point from
6689 the buffer; pointm is garbage in the selected window. */
6690 if (EQ (window, selected_window))
6691 p->pointm = build_marker (XBUFFER (w->contents),
6692 BUF_PT (XBUFFER (w->contents)),
6693 BUF_PT_BYTE (XBUFFER (w->contents)));
6694 else
6695 p->pointm = Fcopy_marker (w->pointm, Qnil);
6696 p->old_pointm = Fcopy_marker (w->old_pointm, Qnil);
6697 XMARKER (p->pointm)->insertion_type
6698 = !NILP (buffer_local_value /* Don't signal error if void. */
6699 (Qwindow_point_insertion_type, w->contents));
6700 XMARKER (p->old_pointm)->insertion_type
6701 = !NILP (buffer_local_value /* Don't signal error if void. */
6702 (Qwindow_point_insertion_type, w->contents));
6704 p->start = Fcopy_marker (w->start, Qnil);
6705 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6707 else
6709 p->pointm = Qnil;
6710 p->old_pointm = Qnil;
6711 p->start = Qnil;
6712 p->start_at_line_beg = Qnil;
6715 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6716 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6718 if (WINDOWP (w->contents))
6719 i = save_window_save (w->contents, vector, i);
6722 return i;
6725 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6726 Scurrent_window_configuration, 0, 1, 0,
6727 doc: /* Return an object representing the current window configuration of FRAME.
6728 If FRAME is nil or omitted, use the selected frame.
6729 This describes the number of windows, their sizes and current buffers,
6730 and for each displayed buffer, where display starts, and the position of
6731 point. An exception is made for point in the current buffer:
6732 its value is -not- saved.
6733 This also records the currently selected frame, and FRAME's focus
6734 redirection (see `redirect-frame-focus'). The variable
6735 `window-persistent-parameters' specifies which window parameters are
6736 saved by this function. */)
6737 (Lisp_Object frame)
6739 register Lisp_Object tem;
6740 register int n_windows;
6741 register struct save_window_data *data;
6742 register int i;
6743 struct frame *f = decode_live_frame (frame);
6745 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6746 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6747 PVEC_WINDOW_CONFIGURATION);
6749 data->frame_cols = FRAME_COLS (f);
6750 data->frame_lines = FRAME_LINES (f);
6751 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6752 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6753 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6754 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6755 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6756 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6757 data->selected_frame = selected_frame;
6758 data->current_window = FRAME_SELECTED_WINDOW (f);
6759 XSETBUFFER (data->current_buffer, current_buffer);
6760 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6761 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6762 data->root_window = FRAME_ROOT_WINDOW (f);
6763 data->focus_frame = FRAME_FOCUS_FRAME (f);
6764 tem = make_uninit_vector (n_windows);
6765 data->saved_windows = tem;
6766 for (i = 0; i < n_windows; i++)
6767 ASET (tem, i,
6768 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6769 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6770 XSETWINDOW_CONFIGURATION (tem, data);
6771 return (tem);
6774 /* Called after W's margins, fringes or scroll bars was adjusted. */
6776 static void
6777 apply_window_adjustment (struct window *w)
6779 eassert (w);
6780 clear_glyph_matrix (w->current_matrix);
6781 w->window_end_valid = 0;
6782 windows_or_buffers_changed = 30;
6783 wset_redisplay (w);
6784 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6788 /***********************************************************************
6789 Marginal Areas
6790 ***********************************************************************/
6792 static struct window *
6793 set_window_margins (struct window *w, Lisp_Object left_width,
6794 Lisp_Object right_width)
6796 int left, right;
6797 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
6799 left = (NILP (left_width) ? 0
6800 : (CHECK_NATNUM (left_width), XINT (left_width)));
6801 right = (NILP (right_width) ? 0
6802 : (CHECK_NATNUM (right_width), XINT (right_width)));
6804 if (w->left_margin_cols != left || w->right_margin_cols != right)
6806 /* Don't change anything if new margins won't fit. */
6807 if ((WINDOW_PIXEL_WIDTH (w)
6808 - WINDOW_FRINGES_WIDTH (w)
6809 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6810 - (left + right) * unit)
6811 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6813 w->left_margin_cols = left;
6814 w->right_margin_cols = right;
6816 return w;
6818 else
6819 return NULL;
6821 else
6822 return NULL;
6825 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6826 2, 3, 0,
6827 doc: /* Set width of marginal areas of window WINDOW.
6828 WINDOW must be a live window and defaults to the selected one.
6830 Second arg LEFT-WIDTH specifies the number of character cells to
6831 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6832 does the same for the right marginal area. A nil width parameter
6833 means no margin.
6835 Return t if any margin was actually changed and nil otherwise. */)
6836 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6838 struct window *w = set_window_margins (decode_live_window (window),
6839 left_width, right_width);
6840 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6844 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6845 0, 1, 0,
6846 doc: /* Get width of marginal areas of window WINDOW.
6847 WINDOW must be a live window and defaults to the selected one.
6849 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6850 If a marginal area does not exist, its width will be returned
6851 as nil. */)
6852 (Lisp_Object window)
6854 struct window *w = decode_live_window (window);
6855 return Fcons (w->left_margin_cols
6856 ? make_number (w->left_margin_cols) : Qnil,
6857 w->right_margin_cols
6858 ? make_number (w->right_margin_cols) : Qnil);
6863 /***********************************************************************
6864 Fringes
6865 ***********************************************************************/
6867 static struct window *
6868 set_window_fringes (struct window *w, Lisp_Object left_width,
6869 Lisp_Object right_width, Lisp_Object outside_margins)
6871 int left, right, outside = !NILP (outside_margins);
6873 left = (NILP (left_width) ? -1
6874 : (CHECK_NATNUM (left_width), XINT (left_width)));
6875 right = (NILP (right_width) ? -1
6876 : (CHECK_NATNUM (right_width), XINT (right_width)));
6878 /* Do nothing on a tty or if nothing to actually change. */
6879 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6880 && (w->left_fringe_width != left
6881 || w->right_fringe_width != right
6882 || w->fringes_outside_margins != outside))
6884 if (left > 0 || right > 0)
6886 /* Don't change anything if new fringes don't fit. */
6887 if ((WINDOW_PIXEL_WIDTH (w)
6888 - WINDOW_MARGINS_WIDTH (w)
6889 - WINDOW_SCROLL_BAR_AREA_WIDTH (w)
6890 - max (left, 0) - max (right, 0))
6891 < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6892 return NULL;
6895 w->left_fringe_width = left;
6896 w->right_fringe_width = right;
6897 w->fringes_outside_margins = outside;
6899 return w;
6901 else
6902 return NULL;
6905 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6906 2, 4, 0,
6907 doc: /* Set the fringe widths of window WINDOW.
6908 WINDOW must be a live window and defaults to the selected one.
6910 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6911 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6912 fringe width. If a fringe width arg is nil, that means to use the
6913 frame's default fringe width. Default fringe widths can be set with
6914 the command `set-fringe-style'.
6915 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6916 outside of the display margins. By default, fringes are drawn between
6917 display marginal areas and the text area.
6919 Return t if any fringe was actually changed and nil otherwise. */)
6920 (Lisp_Object window, Lisp_Object left_width,
6921 Lisp_Object right_width, Lisp_Object outside_margins)
6923 struct window *w
6924 = set_window_fringes (decode_live_window (window),
6925 left_width, right_width, outside_margins);
6926 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6930 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6931 0, 1, 0,
6932 doc: /* Get width of fringes of window WINDOW.
6933 WINDOW must be a live window and defaults to the selected one.
6935 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6936 (Lisp_Object window)
6938 struct window *w = decode_live_window (window);
6940 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6941 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6942 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6947 /***********************************************************************
6948 Scroll bars
6949 ***********************************************************************/
6951 static struct window *
6952 set_window_scroll_bars (struct window *w, Lisp_Object width,
6953 Lisp_Object vertical_type, Lisp_Object height,
6954 Lisp_Object horizontal_type)
6956 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6957 bool changed = 0;
6959 if (iwidth == 0)
6960 vertical_type = Qnil;
6962 if (!(NILP (vertical_type)
6963 || EQ (vertical_type, Qleft)
6964 || EQ (vertical_type, Qright)
6965 || EQ (vertical_type, Qt)))
6966 error ("Invalid type of vertical scroll bar");
6968 if (w->scroll_bar_width != iwidth
6969 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6971 /* Don't change anything if new scroll bar won't fit. */
6972 if ((WINDOW_PIXEL_WIDTH (w)
6973 - WINDOW_MARGINS_WIDTH (w)
6974 - WINDOW_FRINGES_WIDTH (w)
6975 - max (iwidth, 0))
6976 >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
6978 w->scroll_bar_width = iwidth;
6979 wset_vertical_scroll_bar_type (w, vertical_type);
6980 changed = 1;
6984 #if USE_HORIZONTAL_SCROLL_BARS
6986 int iheight = (NILP (height) ? -1 : (CHECK_NATNUM (height), XINT (height)));
6988 if (MINI_WINDOW_P (w) || iheight == 0)
6989 horizontal_type = Qnil;
6991 if (!(NILP (horizontal_type)
6992 || EQ (horizontal_type, Qbottom)
6993 || EQ (horizontal_type, Qt)))
6994 error ("Invalid type of horizontal scroll bar");
6996 if (w->scroll_bar_height != iheight
6997 || !EQ (w->horizontal_scroll_bar_type, horizontal_type))
6999 /* Don't change anything if new scroll bar won't fit. */
7000 if ((WINDOW_PIXEL_HEIGHT (w)
7001 - WINDOW_HEADER_LINE_HEIGHT (w)
7002 - WINDOW_MODE_LINE_HEIGHT (w)
7003 - max (iheight, 0))
7004 >= MIN_SAFE_WINDOW_PIXEL_HEIGHT (w))
7006 w->scroll_bar_height = iheight;
7007 wset_horizontal_scroll_bar_type (w, horizontal_type);
7008 changed = 1;
7012 #else
7013 wset_horizontal_scroll_bar_type (w, Qnil);
7014 #endif
7016 return changed ? w : NULL;
7019 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
7020 Sset_window_scroll_bars, 1, 5, 0,
7021 doc: /* Set width and type of scroll bars of window WINDOW.
7022 WINDOW must be a live window and defaults to the selected one.
7024 Second parameter WIDTH specifies the pixel width for the vertical scroll
7025 bar. If WIDTH is nil, use the scroll-bar width of WINDOW's frame.
7026 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
7027 bar: left, right, or nil. If VERTICAL-TYPE is t, this means use the
7028 frame's scroll-bar type.
7030 Fourth parameter HEIGHT specifies the pixel height for the horizontal
7031 scroll bar. If HEIGHT is nil, use the scroll-bar height of WINDOW's
7032 frame. Fifth parameter HORIZONTAL-TYPE specifies the type of the
7033 horizontal scroll bar: nil, bottom, or t. If HORIZONTAL-TYPE is t, this
7034 means to use the frame's horizontal scroll-bar type.
7036 Return t if scroll bars were actually changed and nil otherwise. */)
7037 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type,
7038 Lisp_Object height, Lisp_Object horizontal_type)
7040 struct window *w
7041 = set_window_scroll_bars (decode_live_window (window),
7042 width, vertical_type, height, horizontal_type);
7043 return w ? (apply_window_adjustment (w), Qt) : Qnil;
7047 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
7048 0, 1, 0,
7049 doc: /* Get width and type of scroll bars of window WINDOW.
7050 WINDOW must be a live window and defaults to the selected one.
7052 Value is a list of the form (WIDTH COLUMNS VERTICAL-TYPE HEIGHT LINES
7053 HORIZONTAL-TYPE). If WIDTH or HEIGHT is nil or VERTICAL-TYPE or
7054 HORIZONTAL-TYPE is t, the window is using the frame's corresponding
7055 value. */)
7056 (Lisp_Object window)
7058 struct window *w = decode_live_window (window);
7060 return Fcons (((w->scroll_bar_width >= 0)
7061 ? make_number (w->scroll_bar_width)
7062 : Qnil),
7063 list5 (make_number (WINDOW_SCROLL_BAR_COLS (w)),
7064 w->vertical_scroll_bar_type,
7065 ((w->scroll_bar_height >= 0)
7066 ? make_number (w->scroll_bar_height)
7067 : Qnil),
7068 make_number (WINDOW_SCROLL_BAR_LINES (w)),
7069 w->horizontal_scroll_bar_type));
7072 /***********************************************************************
7073 Smooth scrolling
7074 ***********************************************************************/
7076 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7077 doc: /* Return the amount by which WINDOW is scrolled vertically.
7078 If WINDOW is omitted or nil, it defaults to the selected window.
7079 Normally, value is a multiple of the canonical character height of WINDOW;
7080 optional second arg PIXELS-P means value is measured in pixels. */)
7081 (Lisp_Object window, Lisp_Object pixels_p)
7083 Lisp_Object result;
7084 struct window *w = decode_live_window (window);
7085 struct frame *f = XFRAME (w->frame);
7087 if (FRAME_WINDOW_P (f))
7088 result = (NILP (pixels_p)
7089 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7090 : make_number (-w->vscroll));
7091 else
7092 result = make_number (0);
7093 return result;
7097 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7098 2, 3, 0,
7099 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7100 WINDOW nil means use the selected window. Normally, VSCROLL is a
7101 non-negative multiple of the canonical character height of WINDOW;
7102 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7103 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7104 corresponds to an integral number of pixels. The return value is the
7105 result of this rounding.
7106 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7107 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7109 struct window *w = decode_live_window (window);
7110 struct frame *f = XFRAME (w->frame);
7112 CHECK_NUMBER_OR_FLOAT (vscroll);
7114 if (FRAME_WINDOW_P (f))
7116 int old_dy = w->vscroll;
7118 w->vscroll = - (NILP (pixels_p)
7119 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7120 : XFLOATINT (vscroll));
7121 w->vscroll = min (w->vscroll, 0);
7123 if (w->vscroll != old_dy)
7125 /* Adjust glyph matrix of the frame if the virtual display
7126 area becomes larger than before. */
7127 if (w->vscroll < 0 && w->vscroll < old_dy)
7128 adjust_frame_glyphs (f);
7130 /* Prevent redisplay shortcuts. */
7131 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
7135 return Fwindow_vscroll (window, pixels_p);
7139 /* Call FN for all leaf windows on frame F. FN is called with the
7140 first argument being a pointer to the leaf window, and with
7141 additional argument USER_DATA. Stops when FN returns 0. */
7143 static void
7144 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
7145 void *user_data)
7147 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7148 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7149 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7153 /* Helper function for foreach_window. Call FN for all leaf windows
7154 reachable from W. FN is called with the first argument being a
7155 pointer to the leaf window, and with additional argument USER_DATA.
7156 Stop when FN returns 0. Value is 0 if stopped by FN. */
7158 static int
7159 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7161 int cont;
7163 for (cont = 1; w && cont;)
7165 if (WINDOWP (w->contents))
7166 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7167 else
7168 cont = fn (w, user_data);
7170 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7173 return cont;
7176 /***********************************************************************
7177 Initialization
7178 ***********************************************************************/
7180 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7181 describe the same state of affairs. This is used by Fequal.
7183 IGNORE_POSITIONS means ignore non-matching scroll positions
7184 and the like.
7186 This ignores a couple of things like the dedication status of
7187 window, combination_limit and the like. This might have to be
7188 fixed. */
7190 bool
7191 compare_window_configurations (Lisp_Object configuration1,
7192 Lisp_Object configuration2,
7193 bool ignore_positions)
7195 register struct save_window_data *d1, *d2;
7196 struct Lisp_Vector *sws1, *sws2;
7197 ptrdiff_t i;
7199 CHECK_WINDOW_CONFIGURATION (configuration1);
7200 CHECK_WINDOW_CONFIGURATION (configuration2);
7202 d1 = (struct save_window_data *) XVECTOR (configuration1);
7203 d2 = (struct save_window_data *) XVECTOR (configuration2);
7204 sws1 = XVECTOR (d1->saved_windows);
7205 sws2 = XVECTOR (d2->saved_windows);
7207 /* Frame settings must match. */
7208 if (d1->frame_cols != d2->frame_cols
7209 || d1->frame_lines != d2->frame_lines
7210 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7211 || !EQ (d1->selected_frame, d2->selected_frame)
7212 || !EQ (d1->current_buffer, d2->current_buffer)
7213 || (!ignore_positions
7214 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7215 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7216 || !EQ (d1->focus_frame, d2->focus_frame)
7217 /* Verify that the two configurations have the same number of windows. */
7218 || sws1->header.size != sws2->header.size)
7219 return 0;
7221 for (i = 0; i < sws1->header.size; i++)
7223 struct saved_window *sw1, *sw2;
7225 sw1 = SAVED_WINDOW_N (sws1, i);
7226 sw2 = SAVED_WINDOW_N (sws2, i);
7228 if (
7229 /* The "current" windows in the two configurations must
7230 correspond to each other. */
7231 EQ (d1->current_window, sw1->window)
7232 != EQ (d2->current_window, sw2->window)
7233 /* Windows' buffers must match. */
7234 || !EQ (sw1->buffer, sw2->buffer)
7235 || !EQ (sw1->pixel_left, sw2->pixel_left)
7236 || !EQ (sw1->pixel_top, sw2->pixel_top)
7237 || !EQ (sw1->pixel_height, sw2->pixel_height)
7238 || !EQ (sw1->pixel_width, sw2->pixel_width)
7239 || !EQ (sw1->left_col, sw2->left_col)
7240 || !EQ (sw1->top_line, sw2->top_line)
7241 || !EQ (sw1->total_cols, sw2->total_cols)
7242 || !EQ (sw1->total_lines, sw2->total_lines)
7243 || !EQ (sw1->display_table, sw2->display_table)
7244 /* The next two disjuncts check the window structure for
7245 equality. */
7246 || !EQ (sw1->parent, sw2->parent)
7247 || !EQ (sw1->prev, sw2->prev)
7248 || (!ignore_positions
7249 && (!EQ (sw1->hscroll, sw2->hscroll)
7250 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7251 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7252 || NILP (Fequal (sw1->start, sw2->start))
7253 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7254 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7255 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7256 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7257 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7258 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7259 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7260 || !EQ (sw1->scroll_bar_height, sw2->scroll_bar_height)
7261 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type)
7262 || !EQ (sw1->horizontal_scroll_bar_type, sw2->horizontal_scroll_bar_type))
7263 return 0;
7266 return 1;
7269 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7270 Scompare_window_configurations, 2, 2, 0,
7271 doc: /* Compare two window configurations as regards the structure of windows.
7272 This function ignores details such as the values of point
7273 and scrolling positions. */)
7274 (Lisp_Object x, Lisp_Object y)
7276 if (compare_window_configurations (x, y, 1))
7277 return Qt;
7278 return Qnil;
7281 void
7282 init_window_once (void)
7284 struct frame *f = make_initial_frame ();
7285 XSETFRAME (selected_frame, f);
7286 Vterminal_frame = selected_frame;
7287 minibuf_window = f->minibuffer_window;
7288 selected_window = f->selected_window;
7291 void
7292 init_window (void)
7294 Vwindow_list = Qnil;
7297 void
7298 syms_of_window (void)
7300 DEFSYM (Qscroll_up, "scroll-up");
7301 DEFSYM (Qscroll_down, "scroll-down");
7302 DEFSYM (Qscroll_command, "scroll-command");
7304 Fput (Qscroll_up, Qscroll_command, Qt);
7305 Fput (Qscroll_down, Qscroll_command, Qt);
7307 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7308 DEFSYM (Qwindowp, "windowp");
7309 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7310 DEFSYM (Qwindow_live_p, "window-live-p");
7311 DEFSYM (Qwindow_valid_p, "window-valid-p");
7312 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7313 DEFSYM (Qdelete_window, "delete-window");
7314 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7315 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7316 DEFSYM (Qwindow_sanitize_window_sizes, "window--sanitize-window-sizes");
7317 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7318 DEFSYM (Qsafe, "safe");
7319 DEFSYM (Qdisplay_buffer, "display-buffer");
7320 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7321 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7322 DEFSYM (Qget_mru_window, "get-mru-window");
7323 DEFSYM (Qwindow_size, "window-size");
7324 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7325 DEFSYM (Qabove, "above");
7326 DEFSYM (Qbelow, "below");
7327 DEFSYM (Qclone_of, "clone-of");
7328 DEFSYM (Qfloor, "floor");
7329 DEFSYM (Qceiling, "ceiling");
7331 staticpro (&Vwindow_list);
7333 minibuf_selected_window = Qnil;
7334 staticpro (&minibuf_selected_window);
7336 window_scroll_pixel_based_preserve_x = -1;
7337 window_scroll_pixel_based_preserve_y = -1;
7338 window_scroll_preserve_hpos = -1;
7339 window_scroll_preserve_vpos = -1;
7341 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7342 doc: /* Non-nil means call as function to display a help buffer.
7343 The function is called with one argument, the buffer to be displayed.
7344 Used by `with-output-to-temp-buffer'.
7345 If this function is used, then it must do the entire job of showing
7346 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7347 Vtemp_buffer_show_function = Qnil;
7349 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7350 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7351 Vminibuf_scroll_window = Qnil;
7353 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7354 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7355 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7356 is displayed in the `mode-line' face. */);
7357 mode_line_in_non_selected_windows = true;
7359 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7360 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7361 Vother_window_scroll_buffer = Qnil;
7363 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7364 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7365 auto_window_vscroll_p = true;
7367 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7368 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7369 next_screen_context_lines = 2;
7371 DEFVAR_LISP ("scroll-preserve-screen-position",
7372 Vscroll_preserve_screen_position,
7373 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7374 A value of nil means point does not keep its screen position except
7375 at the scroll margin or window boundary respectively.
7376 A value of t means point keeps its screen position if the scroll
7377 command moved it vertically out of the window, e.g. when scrolling
7378 by full screens.
7379 Any other value means point always keeps its screen position.
7380 Scroll commands should have the `scroll-command' property
7381 on their symbols to be controlled by this variable. */);
7382 Vscroll_preserve_screen_position = Qnil;
7384 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7385 doc: /* Type of marker to use for `window-point'. */);
7386 Vwindow_point_insertion_type = Qnil;
7387 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7389 DEFVAR_LISP ("window-configuration-change-hook",
7390 Vwindow_configuration_change_hook,
7391 doc: /* Functions to call when window configuration changes.
7392 The buffer-local part is run once per window, with the relevant window
7393 selected; while the global part is run only once for the modified frame,
7394 with the relevant frame selected. */);
7395 Vwindow_configuration_change_hook = Qnil;
7397 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7398 doc: /* Non-nil means `recenter' redraws entire frame.
7399 If this option is non-nil, then the `recenter' command with a nil
7400 argument will redraw the entire frame; the special value `tty' causes
7401 the frame to be redrawn only if it is a tty frame. */);
7402 Vrecenter_redisplay = Qtty;
7404 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7405 doc: /* If t, resize window combinations proportionally.
7406 If this variable is nil, splitting a window gets the entire screen space
7407 for displaying the new window from the window to split. Deleting and
7408 resizing a window preferably resizes one adjacent window only.
7410 If this variable is t, splitting a window tries to get the space
7411 proportionally from all windows in the same combination. This also
7412 allows to split a window that is otherwise too small or of fixed size.
7413 Resizing and deleting a window proportionally resize all windows in the
7414 same combination.
7416 Other values are reserved for future use.
7418 This variable takes no effect if the variable `window-combination-limit' is
7419 non-nil. */);
7420 Vwindow_combination_resize = Qnil;
7422 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7423 doc: /* If non-nil, splitting a window makes a new parent window.
7424 The following values are recognized:
7426 nil means splitting a window will create a new parent window only if the
7427 window has no parent window or the window shall become part of a
7428 combination orthogonal to the one it is part of.
7430 `window-size' means that splitting a window for displaying a buffer
7431 makes a new parent window provided `display-buffer' is supposed to
7432 explicitly set the window's size due to the presence of a
7433 `window-height' or `window-width' entry in the alist used by
7434 `display-buffer'. Otherwise, this value is handled like nil.
7436 `temp-buffer' means that splitting a window for displaying a temporary
7437 buffer always makes a new parent window. Otherwise, this value is
7438 handled like nil.
7440 `display-buffer' means that splitting a window for displaying a buffer
7441 always makes a new parent window. Since temporary buffers are
7442 displayed by the function `display-buffer', this value is stronger
7443 than `temp-buffer'. Splitting a window for other purpose makes a
7444 new parent window only if needed.
7446 t means that splitting a window always creates a new parent window. If
7447 all splits behave this way, each frame's window tree is a binary
7448 tree and every window but the frame's root window has exactly one
7449 sibling.
7451 Other values are reserved for future use. */);
7452 Vwindow_combination_limit = Qwindow_size;
7454 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7455 doc: /* Alist of persistent window parameters.
7456 This alist specifies which window parameters shall get saved by
7457 `current-window-configuration' and `window-state-get' and subsequently
7458 restored to their previous values by `set-window-configuration' and
7459 `window-state-put'.
7461 The car of each entry of this alist is the symbol specifying the
7462 parameter. The cdr is one of the following:
7464 nil means the parameter is neither saved by `window-state-get' nor by
7465 `current-window-configuration'.
7467 t means the parameter is saved by `current-window-configuration' and,
7468 provided its WRITABLE argument is nil, by `window-state-get'.
7470 The symbol `writable' means the parameter is saved unconditionally by
7471 both `current-window-configuration' and `window-state-get'. Do not use
7472 this value for parameters without read syntax (like windows or frames).
7474 Parameters not saved by `current-window-configuration' or
7475 `window-state-get' are left alone by `set-window-configuration'
7476 respectively are not installed by `window-state-put'. */);
7477 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7479 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7480 doc: /* Non-nil means resize windows pixelwise.
7481 This currently affects the functions: `split-window', `maximize-window',
7482 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7483 all functions that symmetrically resize a parent window.
7485 Note that when a frame's pixel size is not a multiple of the
7486 frame's character size, at least one window may get resized
7487 pixelwise even if this option is nil. */);
7488 window_resize_pixelwise = false;
7490 DEFVAR_BOOL ("fast-but-imprecise-scrolling",
7491 Vfast_but_imprecise_scrolling,
7492 doc: /* When non-nil, accelerate scrolling operations.
7493 This comes into play when scrolling rapidly over previously
7494 unfontified buffer regions. Only those portions of the buffer which
7495 are actually going to be displayed get fontified.
7497 Note that this optimization can cause the portion of the buffer
7498 displayed after a scrolling operation to be somewhat inaccurate. */);
7499 Vfast_but_imprecise_scrolling = false;
7501 defsubr (&Sselected_window);
7502 defsubr (&Sminibuffer_window);
7503 defsubr (&Swindow_minibuffer_p);
7504 defsubr (&Swindowp);
7505 defsubr (&Swindow_valid_p);
7506 defsubr (&Swindow_live_p);
7507 defsubr (&Swindow_frame);
7508 defsubr (&Sframe_root_window);
7509 defsubr (&Sframe_first_window);
7510 defsubr (&Sframe_selected_window);
7511 defsubr (&Sset_frame_selected_window);
7512 defsubr (&Spos_visible_in_window_p);
7513 defsubr (&Swindow_line_height);
7514 defsubr (&Swindow_buffer);
7515 defsubr (&Swindow_parent);
7516 defsubr (&Swindow_top_child);
7517 defsubr (&Swindow_left_child);
7518 defsubr (&Swindow_next_sibling);
7519 defsubr (&Swindow_prev_sibling);
7520 defsubr (&Swindow_combination_limit);
7521 defsubr (&Sset_window_combination_limit);
7522 defsubr (&Swindow_use_time);
7523 defsubr (&Swindow_pixel_width);
7524 defsubr (&Swindow_pixel_height);
7525 defsubr (&Swindow_total_width);
7526 defsubr (&Swindow_total_height);
7527 defsubr (&Swindow_normal_size);
7528 defsubr (&Swindow_new_pixel);
7529 defsubr (&Swindow_new_total);
7530 defsubr (&Swindow_new_normal);
7531 defsubr (&Swindow_pixel_left);
7532 defsubr (&Swindow_pixel_top);
7533 defsubr (&Swindow_left_column);
7534 defsubr (&Swindow_top_line);
7535 defsubr (&Sset_window_new_pixel);
7536 defsubr (&Sset_window_new_total);
7537 defsubr (&Sset_window_new_normal);
7538 defsubr (&Swindow_resize_apply);
7539 defsubr (&Swindow_resize_apply_total);
7540 defsubr (&Swindow_body_height);
7541 defsubr (&Swindow_body_width);
7542 defsubr (&Swindow_hscroll);
7543 defsubr (&Sset_window_hscroll);
7544 defsubr (&Swindow_redisplay_end_trigger);
7545 defsubr (&Sset_window_redisplay_end_trigger);
7546 defsubr (&Swindow_edges);
7547 defsubr (&Swindow_pixel_edges);
7548 defsubr (&Swindow_absolute_pixel_edges);
7549 defsubr (&Swindow_mode_line_height);
7550 defsubr (&Swindow_header_line_height);
7551 defsubr (&Swindow_right_divider_width);
7552 defsubr (&Swindow_bottom_divider_width);
7553 defsubr (&Swindow_scroll_bar_width);
7554 defsubr (&Swindow_scroll_bar_height);
7555 defsubr (&Swindow_inside_edges);
7556 defsubr (&Swindow_inside_pixel_edges);
7557 defsubr (&Swindow_inside_absolute_pixel_edges);
7558 defsubr (&Scoordinates_in_window_p);
7559 defsubr (&Swindow_at);
7560 defsubr (&Swindow_point);
7561 defsubr (&Swindow_old_point);
7562 defsubr (&Swindow_start);
7563 defsubr (&Swindow_end);
7564 defsubr (&Sset_window_point);
7565 defsubr (&Sset_window_start);
7566 defsubr (&Swindow_dedicated_p);
7567 defsubr (&Sset_window_dedicated_p);
7568 defsubr (&Swindow_display_table);
7569 defsubr (&Sset_window_display_table);
7570 defsubr (&Snext_window);
7571 defsubr (&Sprevious_window);
7572 defsubr (&Sget_buffer_window);
7573 defsubr (&Sdelete_other_windows_internal);
7574 defsubr (&Sdelete_window_internal);
7575 defsubr (&Sresize_mini_window_internal);
7576 defsubr (&Sset_window_buffer);
7577 defsubr (&Srun_window_configuration_change_hook);
7578 defsubr (&Srun_window_scroll_functions);
7579 defsubr (&Sselect_window);
7580 defsubr (&Sforce_window_update);
7581 defsubr (&Ssplit_window_internal);
7582 defsubr (&Sscroll_up);
7583 defsubr (&Sscroll_down);
7584 defsubr (&Sscroll_left);
7585 defsubr (&Sscroll_right);
7586 defsubr (&Sother_window_for_scrolling);
7587 defsubr (&Sscroll_other_window);
7588 defsubr (&Sminibuffer_selected_window);
7589 defsubr (&Srecenter);
7590 defsubr (&Swindow_text_width);
7591 defsubr (&Swindow_text_height);
7592 defsubr (&Smove_to_window_line);
7593 defsubr (&Swindow_configuration_p);
7594 defsubr (&Swindow_configuration_frame);
7595 defsubr (&Sset_window_configuration);
7596 defsubr (&Scurrent_window_configuration);
7597 defsubr (&Sset_window_margins);
7598 defsubr (&Swindow_margins);
7599 defsubr (&Sset_window_fringes);
7600 defsubr (&Swindow_fringes);
7601 defsubr (&Sset_window_scroll_bars);
7602 defsubr (&Swindow_scroll_bars);
7603 defsubr (&Swindow_vscroll);
7604 defsubr (&Sset_window_vscroll);
7605 defsubr (&Scompare_window_configurations);
7606 defsubr (&Swindow_list);
7607 defsubr (&Swindow_list_1);
7608 defsubr (&Swindow_prev_buffers);
7609 defsubr (&Sset_window_prev_buffers);
7610 defsubr (&Swindow_next_buffers);
7611 defsubr (&Sset_window_next_buffers);
7612 defsubr (&Swindow_parameters);
7613 defsubr (&Swindow_parameter);
7614 defsubr (&Sset_window_parameter);
7617 void
7618 keys_of_window (void)
7620 initial_define_key (control_x_map, '<', "scroll-left");
7621 initial_define_key (control_x_map, '>', "scroll-right");
7623 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7624 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7625 initial_define_key (meta_map, 'v', "scroll-down-command");