manual upstream merge
[emacs.git] / src / window.c
blob5f29791284a9aecdb3f4f6508a21eef332c1c2a1
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2014 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_NS
48 #include "nsterm.h"
49 #endif
50 #ifdef HAVE_XWIDGETS
51 #include "xwidget.h"
52 #endif
54 Lisp_Object Qwindowp, Qwindow_live_p;
55 static Lisp_Object Qwindow_valid_p;
56 static Lisp_Object Qwindow_configuration_p;
57 static Lisp_Object Qrecord_window_buffer;
58 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
59 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
60 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
61 static Lisp_Object Qwindow_pixel_to_total;
62 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
63 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
64 static Lisp_Object Qfloor, Qceiling;
65 static Lisp_Object Qwindow_point_insertion_type;
67 static int displayed_window_lines (struct window *);
68 static int count_windows (struct window *);
69 static int get_leaf_windows (struct window *, struct window **, int);
70 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
71 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
72 static void window_scroll_line_based (Lisp_Object, int, bool, int);
73 static int add_window_to_list (struct window *, void *);
74 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
75 Lisp_Object, int);
76 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
77 Lisp_Object *);
78 static void foreach_window (struct frame *,
79 int (* fn) (struct window *, void *),
80 void *);
81 static int foreach_window_1 (struct window *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
85 static int window_resize_check (struct window *, bool);
86 static void window_resize_apply (struct window *, bool);
87 static void window_resize_apply_total (struct window *, bool);
88 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
89 static void select_window_1 (Lisp_Object, bool);
91 static struct window *set_window_fringes (struct window *, Lisp_Object,
92 Lisp_Object, Lisp_Object);
93 static struct window *set_window_margins (struct window *, Lisp_Object,
94 Lisp_Object);
95 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
96 Lisp_Object, Lisp_Object);
97 static void apply_window_adjustment (struct window *);
99 /* This is the window in which the terminal's cursor should
100 be left when nothing is being done with it. This must
101 always be a leaf window, and its buffer is selected by
102 the top level editing loop at the end of each command.
104 This value is always the same as
105 FRAME_SELECTED_WINDOW (selected_frame). */
106 Lisp_Object selected_window;
108 /* A list of all windows for use by next_window and Fwindow_list.
109 Functions creating or deleting windows should invalidate this cache
110 by setting it to nil. */
111 Lisp_Object Vwindow_list;
113 /* The mini-buffer window of the selected frame.
114 Note that you cannot test for mini-bufferness of an arbitrary window
115 by comparing against this; but you can test for mini-bufferness of
116 the selected window. */
117 Lisp_Object minibuf_window;
119 /* Non-nil means it is the window whose mode line should be
120 shown as the selected window when the minibuffer is selected. */
121 Lisp_Object minibuf_selected_window;
123 /* Hook run at end of temp_output_buffer_show. */
124 static Lisp_Object Qtemp_buffer_show_hook;
126 /* Incremented for each window created. */
127 static int sequence_number;
129 /* Hook to run when window config changes. */
130 static Lisp_Object Qwindow_configuration_change_hook;
132 /* Used by the function window_scroll_pixel_based. */
133 static int window_scroll_pixel_based_preserve_x;
134 static int window_scroll_pixel_based_preserve_y;
136 /* Same for window_scroll_line_based. */
137 static EMACS_INT window_scroll_preserve_hpos;
138 static EMACS_INT window_scroll_preserve_vpos;
140 static void
141 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
143 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
146 /* These setters are used only in this file, so they can be private. */
147 static void
148 wset_combination_limit (struct window *w, Lisp_Object val)
150 w->combination_limit = val;
152 static void
153 wset_dedicated (struct window *w, Lisp_Object val)
155 w->dedicated = val;
157 static void
158 wset_display_table (struct window *w, Lisp_Object val)
160 w->display_table = val;
162 static void
163 wset_new_normal (struct window *w, Lisp_Object val)
165 w->new_normal = val;
167 static void
168 wset_new_total (struct window *w, Lisp_Object val)
170 w->new_total = val;
172 static void
173 wset_normal_cols (struct window *w, Lisp_Object val)
175 w->normal_cols = val;
177 static void
178 wset_normal_lines (struct window *w, Lisp_Object val)
180 w->normal_lines = val;
182 static void
183 wset_parent (struct window *w, Lisp_Object val)
185 w->parent = val;
187 static void
188 wset_pointm (struct window *w, Lisp_Object val)
190 w->pointm = val;
192 static void
193 wset_start (struct window *w, Lisp_Object val)
195 w->start = val;
197 static void
198 wset_temslot (struct window *w, Lisp_Object val)
200 w->temslot = val;
202 static void
203 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
205 w->vertical_scroll_bar_type = val;
207 static void
208 wset_window_parameters (struct window *w, Lisp_Object val)
210 w->window_parameters = val;
212 static void
213 wset_combination (struct window *w, bool horflag, Lisp_Object val)
215 /* Since leaf windows never becomes non-leaf, there should
216 be no buffer and markers in start and pointm fields of W. */
217 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
218 w->contents = val;
219 /* When an internal window is deleted and VAL is nil, HORFLAG
220 is meaningless. */
221 if (!NILP (val))
222 w->horizontal = horflag;
225 /* Nonzero if leaf window W doesn't reflect the actual state
226 of displayed buffer due to its text or overlays change. */
228 bool
229 window_outdated (struct window *w)
231 struct buffer *b = XBUFFER (w->contents);
232 return (w->last_modified < BUF_MODIFF (b)
233 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
236 struct window *
237 decode_live_window (register Lisp_Object window)
239 if (NILP (window))
240 return XWINDOW (selected_window);
242 CHECK_LIVE_WINDOW (window);
243 return XWINDOW (window);
246 struct window *
247 decode_any_window (register Lisp_Object window)
249 struct window *w;
251 if (NILP (window))
252 return XWINDOW (selected_window);
254 CHECK_WINDOW (window);
255 w = XWINDOW (window);
256 return w;
259 static struct window *
260 decode_valid_window (register Lisp_Object window)
262 struct window *w;
264 if (NILP (window))
265 return XWINDOW (selected_window);
267 CHECK_VALID_WINDOW (window);
268 w = XWINDOW (window);
269 return w;
272 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
273 cease its buffer, and 1 means that W is about to set up the new one. */
275 static void
276 adjust_window_count (struct window *w, int arg)
278 eassert (eabs (arg) == 1);
279 if (BUFFERP (w->contents))
281 struct buffer *b = XBUFFER (w->contents);
283 if (b->base_buffer)
284 b = b->base_buffer;
285 b->window_count += arg;
286 eassert (b->window_count >= 0);
287 /* These should be recalculated by redisplay code. */
288 w->window_end_valid = 0;
289 w->base_line_pos = 0;
293 /* Set W's buffer slot to VAL and recompute number
294 of windows showing VAL if it is a buffer. */
296 void
297 wset_buffer (struct window *w, Lisp_Object val)
299 adjust_window_count (w, -1);
300 if (BUFFERP (val))
301 /* Make sure that we do not assign the buffer
302 to an internal window. */
303 eassert (MARKERP (w->start) && MARKERP (w->pointm));
304 w->contents = val;
305 adjust_window_count (w, 1);
308 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
309 doc: /* Return t if OBJECT is a window and nil otherwise. */)
310 (Lisp_Object object)
312 return WINDOWP (object) ? Qt : Qnil;
315 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
316 doc: /* Return t if OBJECT is a valid window and nil otherwise.
317 A valid window is either a window that displays a buffer or an internal
318 window. Deleted windows are not live. */)
319 (Lisp_Object object)
321 return WINDOW_VALID_P (object) ? Qt : Qnil;
324 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
325 doc: /* Return t if OBJECT is a live window and nil otherwise.
326 A live window is a window that displays a buffer.
327 Internal windows and deleted windows are not live. */)
328 (Lisp_Object object)
330 return WINDOW_LIVE_P (object) ? Qt : Qnil;
333 /* Frames and windows. */
334 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
335 doc: /* Return the frame that window WINDOW is on.
336 WINDOW must be a valid window and defaults to the selected one. */)
337 (Lisp_Object window)
339 return decode_valid_window (window)->frame;
342 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
343 doc: /* Return the root window of FRAME-OR-WINDOW.
344 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
345 With a frame argument, return that frame's root window.
346 With a window argument, return the root window of that window's frame. */)
347 (Lisp_Object frame_or_window)
349 Lisp_Object window;
351 if (NILP (frame_or_window))
352 window = SELECTED_FRAME ()->root_window;
353 else if (WINDOW_VALID_P (frame_or_window))
354 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
355 else
357 CHECK_LIVE_FRAME (frame_or_window);
358 window = XFRAME (frame_or_window)->root_window;
361 return window;
364 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
365 doc: /* Return the minibuffer window for frame FRAME.
366 If FRAME is omitted or nil, it defaults to the selected frame. */)
367 (Lisp_Object frame)
369 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
372 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
373 Swindow_minibuffer_p, 0, 1, 0,
374 doc: /* Return non-nil if WINDOW is a minibuffer window.
375 WINDOW must be a valid window and defaults to the selected one. */)
376 (Lisp_Object window)
378 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
381 /* Don't move this to window.el - this must be a safe routine. */
382 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
383 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
384 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
385 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
386 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
387 the first window of that frame. */)
388 (Lisp_Object frame_or_window)
390 Lisp_Object window;
392 if (NILP (frame_or_window))
393 window = SELECTED_FRAME ()->root_window;
394 else if (WINDOW_VALID_P (frame_or_window))
395 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
396 else
398 CHECK_LIVE_FRAME (frame_or_window);
399 window = XFRAME (frame_or_window)->root_window;
402 while (WINDOWP (XWINDOW (window)->contents))
403 window = XWINDOW (window)->contents;
405 return window;
408 DEFUN ("frame-selected-window", Fframe_selected_window,
409 Sframe_selected_window, 0, 1, 0,
410 doc: /* Return the selected window of FRAME-OR-WINDOW.
411 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
412 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
413 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
414 return the selected window of that frame. */)
415 (Lisp_Object frame_or_window)
417 Lisp_Object window;
419 if (NILP (frame_or_window))
420 window = SELECTED_FRAME ()->selected_window;
421 else if (WINDOW_VALID_P (frame_or_window))
422 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
423 else
425 CHECK_LIVE_FRAME (frame_or_window);
426 window = XFRAME (frame_or_window)->selected_window;
429 return window;
432 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
433 Sset_frame_selected_window, 2, 3, 0,
434 doc: /* Set selected window of FRAME to WINDOW.
435 FRAME must be a live frame and defaults to the selected one. If FRAME
436 is the selected frame, this makes WINDOW the selected window. Optional
437 argument NORECORD non-nil means to neither change the order of recently
438 selected windows nor the buffer list. WINDOW must denote a live window.
439 Return WINDOW. */)
440 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
442 if (NILP (frame))
443 frame = selected_frame;
445 CHECK_LIVE_FRAME (frame);
446 CHECK_LIVE_WINDOW (window);
448 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
449 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
451 if (EQ (frame, selected_frame))
452 return Fselect_window (window, norecord);
453 else
455 fset_selected_window (XFRAME (frame), window);
456 return window;
460 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
461 doc: /* Return the selected window.
462 The selected window is the window in which the standard cursor for
463 selected windows appears and to which many commands apply. */)
464 (void)
466 return selected_window;
469 int window_select_count;
471 /* If select_window is called with inhibit_point_swap non-zero it will
472 not store point of the old selected window's buffer back into that
473 window's pointm slot. This is needed by Fset_window_configuration to
474 avoid that the display routine is called with selected_window set to
475 Qnil causing a subsequent crash. */
476 static Lisp_Object
477 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
479 register struct window *w;
480 struct frame *sf;
482 CHECK_LIVE_WINDOW (window);
484 w = XWINDOW (window);
486 /* Make the selected window's buffer current. */
487 Fset_buffer (w->contents);
489 if (EQ (window, selected_window) && !inhibit_point_swap)
490 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
491 way to call record_buffer from Elisp, so it's important that we call
492 record_buffer before returning here. */
493 goto record_and_return;
495 if (NILP (norecord))
496 { /* Mark the window for redisplay since the selected-window has
497 a different mode-line. */
498 wset_redisplay (XWINDOW (selected_window));
499 wset_redisplay (w);
501 else
502 redisplay_other_windows ();
504 sf = SELECTED_FRAME ();
505 if (XFRAME (WINDOW_FRAME (w)) != sf)
507 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
508 /* Use this rather than Fhandle_switch_frame
509 so that FRAME_FOCUS_FRAME is moved appropriately as we
510 move around in the state where a minibuffer in a separate
511 frame is active. */
512 Fselect_frame (WINDOW_FRAME (w), norecord);
513 /* Fselect_frame called us back so we've done all the work already. */
514 eassert (EQ (window, selected_window));
515 return window;
517 else
518 fset_selected_window (sf, window);
520 select_window_1 (window, inhibit_point_swap);
521 bset_last_selected_window (XBUFFER (w->contents), window);
523 record_and_return:
524 /* record_buffer can run QUIT, so make sure it is run only after we have
525 re-established the invariant between selected_window and selected_frame,
526 otherwise the temporary broken invariant might "escape" (bug#14161). */
527 if (NILP (norecord))
529 w->use_time = ++window_select_count;
530 record_buffer (w->contents);
533 return window;
536 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
537 (not even for redisplay's benefit), and assume that the window's frame is
538 already selected. */
539 static void
540 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
542 /* Store the old selected window's buffer's point in pointm of the old
543 selected window. It belongs to that window, and when the window is
544 not selected, must be in the window. */
545 if (!inhibit_point_swap)
547 struct window *ow = XWINDOW (selected_window);
548 if (BUFFERP (ow->contents))
549 set_marker_both (ow->pointm, ow->contents,
550 BUF_PT (XBUFFER (ow->contents)),
551 BUF_PT_BYTE (XBUFFER (ow->contents)));
554 selected_window = window;
556 /* Go to the point recorded in the window.
557 This is important when the buffer is in more
558 than one window. It also matters when
559 redisplay_window has altered point after scrolling,
560 because it makes the change only in the window. */
561 set_point_from_marker (XWINDOW (window)->pointm);
564 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
565 doc: /* Select WINDOW which must be a live window.
566 Also make WINDOW's frame the selected frame and WINDOW that frame's
567 selected window. In addition, make WINDOW's buffer current and set its
568 buffer's value of `point' to the value of WINDOW's `window-point'.
569 Return WINDOW.
571 Optional second arg NORECORD non-nil means do not put this buffer at the
572 front of the buffer list and do not make this window the most recently
573 selected one.
575 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
576 applications and internal routines often select a window temporarily for
577 various purposes; mostly, to simplify coding. As a rule, such
578 selections should be not recorded and therefore will not pollute
579 `buffer-list-update-hook'. Selections that "really count" are those
580 causing a visible change in the next redisplay of WINDOW's frame and
581 should be always recorded. So if you think of running a function each
582 time a window gets selected put it on `buffer-list-update-hook'.
584 Also note that the main editor command loop sets the current buffer to
585 the buffer of the selected window before each command. */)
586 (register Lisp_Object window, Lisp_Object norecord)
588 return select_window (window, norecord, 0);
591 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
592 doc: /* Return the buffer displayed in window WINDOW.
593 If WINDOW is omitted or nil, it defaults to the selected window.
594 Return nil for an internal window or a deleted window. */)
595 (Lisp_Object window)
597 struct window *w = decode_any_window (window);
598 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
601 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
602 doc: /* Return the parent window of window WINDOW.
603 WINDOW must be a valid window and defaults to the selected one.
604 Return nil for a window with no parent (e.g. a root window). */)
605 (Lisp_Object window)
607 return decode_valid_window (window)->parent;
610 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
611 doc: /* Return the topmost child window of window WINDOW.
612 WINDOW must be a valid window and defaults to the selected one.
613 Return nil if WINDOW is a live window (live windows have no children).
614 Return nil if WINDOW is an internal window whose children form a
615 horizontal combination. */)
616 (Lisp_Object window)
618 struct window *w = decode_valid_window (window);
619 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
622 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
623 doc: /* Return the leftmost child window of window WINDOW.
624 WINDOW must be a valid window and defaults to the selected one.
625 Return nil if WINDOW is a live window (live windows have no children).
626 Return nil if WINDOW is an internal window whose children form a
627 vertical combination. */)
628 (Lisp_Object window)
630 struct window *w = decode_valid_window (window);
631 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
634 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
635 doc: /* Return the next sibling window of window WINDOW.
636 WINDOW must be a valid window and defaults to the selected one.
637 Return nil if WINDOW has no next sibling. */)
638 (Lisp_Object window)
640 return decode_valid_window (window)->next;
643 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
644 doc: /* Return the previous sibling window of window WINDOW.
645 WINDOW must be a valid window and defaults to the selected one.
646 Return nil if WINDOW has no previous sibling. */)
647 (Lisp_Object window)
649 return decode_valid_window (window)->prev;
652 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
653 doc: /* Return combination limit of window WINDOW.
654 WINDOW must be a valid window used in horizontal or vertical combination.
655 If the return value is nil, child windows of WINDOW can be recombined with
656 WINDOW's siblings. A return value of t means that child windows of
657 WINDOW are never \(re-)combined with WINDOW's siblings. */)
658 (Lisp_Object window)
660 struct window *w;
662 CHECK_VALID_WINDOW (window);
663 w = XWINDOW (window);
664 if (WINDOW_LEAF_P (w))
665 error ("Combination limit is meaningful for internal windows only");
666 return w->combination_limit;
669 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
670 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
671 WINDOW must be a valid window used in horizontal or vertical combination.
672 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
673 siblings. LIMIT t means that child windows of WINDOW are never
674 \(re-)combined with WINDOW's siblings. Other values are reserved for
675 future use. */)
676 (Lisp_Object window, Lisp_Object limit)
678 struct window *w;
680 CHECK_VALID_WINDOW (window);
681 w = XWINDOW (window);
682 if (WINDOW_LEAF_P (w))
683 error ("Combination limit is meaningful for internal windows only");
684 wset_combination_limit (w, limit);
685 return limit;
688 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
689 doc: /* Return the use time of window WINDOW.
690 WINDOW must be a live window and defaults to the selected one.
691 The window with the highest use time is the most recently selected
692 one. The window with the lowest use time is the least recently
693 selected one. */)
694 (Lisp_Object window)
696 return make_number (decode_live_window (window)->use_time);
699 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
700 doc: /* Return the width of window WINDOW in pixels.
701 WINDOW must be a valid window and defaults to the selected one.
703 The return value includes the fringes and margins of WINDOW as well as
704 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
705 an internal window, its pixel width is the width of the screen areas
706 spanned by its children. */)
707 (Lisp_Object window)
709 return make_number (decode_valid_window (window)->pixel_width);
712 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
713 doc: /* Return the height of window WINDOW in pixels.
714 WINDOW must be a valid window and defaults to the selected one.
716 The return value includes the mode line and header line and the bottom
717 divider, if any. If WINDOW is an internal window, its pixel height is
718 the height of the screen areas spanned by its children. */)
719 (Lisp_Object window)
721 return make_number (decode_valid_window (window)->pixel_height);
724 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
725 doc: /* Return the height of window WINDOW in lines.
726 WINDOW must be a valid window and defaults to the selected one.
728 The return value includes the heights of WINDOW's mode and header line
729 and its bottom divider, if any. If WINDOW is an internal window, the
730 total height is the height of the screen areas spanned by its children.
732 If WINDOW's pixel height is not an integral multiple of its frame's
733 character height, the number of lines occupied by WINDOW is rounded
734 internally. This is done in a way such that, if WINDOW is a parent
735 window, the sum of the total heights of all its children internally
736 equals the total height of WINDOW.
738 If the optional argument ROUND is `ceiling', return the smallest integer
739 larger than WINDOW's pixel height divided by the character height of
740 WINDOW's frame. ROUND `floor' means to return the largest integer
741 smaller than WINDOW's pixel height divided by the character height of
742 WINDOW's frame. Any other value of ROUND means to return the internal
743 total height of WINDOW. */)
744 (Lisp_Object window, Lisp_Object round)
746 struct window *w = decode_valid_window (window);
748 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
749 return make_number (w->total_lines);
750 else
752 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
754 return make_number (EQ (round, Qceiling)
755 ? ((w->pixel_height + unit - 1) /unit)
756 : (w->pixel_height / unit));
760 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
761 doc: /* Return the total width of window WINDOW in columns.
762 WINDOW must be a valid window and defaults to the selected one.
764 The return value includes the widths of WINDOW's fringes, margins,
765 scroll bars and its right divider, if any. If WINDOW is an internal
766 window, the total width is the width of the screen areas spanned by its
767 children.
769 If WINDOW's pixel width is not an integral multiple of its frame's
770 character width, the number of lines occupied by WINDOW is rounded
771 internally. This is done in a way such that, if WINDOW is a parent
772 window, the sum of the total widths of all its children internally
773 equals the total width of WINDOW.
775 If the optional argument ROUND is `ceiling', return the smallest integer
776 larger than WINDOW's pixel width divided by the character width of
777 WINDOW's frame. ROUND `floor' means to return the largest integer
778 smaller than WINDOW's pixel width divided by the character width of
779 WINDOW's frame. Any other value of ROUND means to return the internal
780 total width of WINDOW. */)
781 (Lisp_Object window, Lisp_Object round)
783 struct window *w = decode_valid_window (window);
785 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
786 return make_number (w->total_cols);
787 else
789 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
791 return make_number (EQ (round, Qceiling)
792 ? ((w->pixel_width + unit - 1) /unit)
793 : (w->pixel_width / unit));
797 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
798 doc: /* Return the new total size of window WINDOW.
799 WINDOW must be a valid window and defaults to the selected one. */)
800 (Lisp_Object window)
802 return decode_valid_window (window)->new_total;
805 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
806 doc: /* Return the normal height of window WINDOW.
807 WINDOW must be a valid window and defaults to the selected one.
808 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
809 (Lisp_Object window, Lisp_Object horizontal)
811 struct window *w = decode_valid_window (window);
813 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
816 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
817 doc: /* Return new normal size of window WINDOW.
818 WINDOW must be a valid window and defaults to the selected one. */)
819 (Lisp_Object window)
821 return decode_valid_window (window)->new_normal;
824 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
825 doc: /* Return new pixel size of window WINDOW.
826 WINDOW must be a valid window and defaults to the selected one. */)
827 (Lisp_Object window)
829 return decode_valid_window (window)->new_pixel;
832 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
833 doc: /* Return left pixel edge of window WINDOW.
834 WINDOW must be a valid window and defaults to the selected one. */)
835 (Lisp_Object window)
837 return make_number (decode_valid_window (window)->pixel_left);
840 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
841 doc: /* Return top pixel edge of window WINDOW.
842 WINDOW must be a valid window and defaults to the selected one. */)
843 (Lisp_Object window)
845 return make_number (decode_valid_window (window)->pixel_top);
848 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
849 doc: /* Return left column of window WINDOW.
850 This is the distance, in columns, between the left edge of WINDOW and
851 the left edge of the frame's window area. For instance, the return
852 value is 0 if there is no window to the left of WINDOW.
854 WINDOW must be a valid window and defaults to the selected one. */)
855 (Lisp_Object window)
857 return make_number (decode_valid_window (window)->left_col);
860 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
861 doc: /* Return top line of window WINDOW.
862 This is the distance, in lines, between the top of WINDOW and the top
863 of the frame's window area. For instance, the return value is 0 if
864 there is no window above WINDOW.
866 WINDOW must be a valid window and defaults to the selected one. */)
867 (Lisp_Object window)
869 return make_number (decode_valid_window (window)->top_line);
872 /* Return the number of lines/pixels of W's body. Don't count any mode
873 or header line or horizontal divider of W. Rounds down to nearest
874 integer when not working pixelwise. */
875 static int
876 window_body_height (struct window *w, bool pixelwise)
878 int height = (w->pixel_height
879 - WINDOW_HEADER_LINE_HEIGHT (w)
880 - WINDOW_MODE_LINE_HEIGHT (w)
881 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
883 /* Don't return a negative value. */
884 return max (pixelwise
885 ? height
886 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
890 /* Return the number of columns/pixels of W's body. Don't count columns
891 occupied by the scroll bar or the divider/vertical bar separating W
892 from its right sibling or margins. On window-systems don't count
893 fringes either. Round down to nearest integer when not working
894 pixelwise. */
896 window_body_width (struct window *w, bool pixelwise)
898 struct frame *f = XFRAME (WINDOW_FRAME (w));
900 int width = (w->pixel_width
901 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
902 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
903 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
904 : ((!FRAME_WINDOW_P (f)
905 && !WINDOW_RIGHTMOST_P (w)
906 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
907 /* A vertical bar is either 1 or 0. */
908 ? 1 : 0))
909 - WINDOW_MARGINS_WIDTH (w)
910 - (FRAME_WINDOW_P (f)
911 ? WINDOW_FRINGES_WIDTH (w)
912 : 0));
914 /* Don't return a negative value. */
915 return max (pixelwise
916 ? width
917 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
921 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
922 doc: /* Return the height of WINDOW's text area.
923 WINDOW must be a live window and defaults to the selected one. Optional
924 argument PIXELWISE non-nil means return the height of WINDOW's text area
925 in pixels. The return value does not include the mode line or header
926 line or any horizontal divider.
928 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
929 pixel height divided by the character height of WINDOW's frame. This
930 means that if a line at the bottom of the text area is only partially
931 visible, that line is not counted. */)
932 (Lisp_Object window, Lisp_Object pixelwise)
934 return make_number (window_body_height (decode_live_window (window),
935 NILP (pixelwise) ? 0 : 1));
938 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
939 doc: /* Return the width of WINDOW's text area.
940 WINDOW must be a live window and defaults to the selected one. Optional
941 argument PIXELWISE non-nil means return the width in pixels. The return
942 value does not include any vertical dividers, fringes or marginal areas,
943 or scroll bars.
945 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
946 pixel width divided by the character width of WINDOW's frame. This
947 means that if a column at the right of the text area is only partially
948 visible, that column is not counted. */)
949 (Lisp_Object window, Lisp_Object pixelwise)
951 return make_number (window_body_width (decode_live_window (window),
952 NILP (pixelwise) ? 0 : 1));
955 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
956 Swindow_mode_line_height, 0, 1, 0,
957 doc: /* Return the height in pixels of WINDOW's mode-line.
958 WINDOW must be a live window and defaults to the selected one. */)
959 (Lisp_Object window)
961 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
964 DEFUN ("window-header-line-height", Fwindow_header_line_height,
965 Swindow_header_line_height, 0, 1, 0,
966 doc: /* Return the height in pixels of WINDOW's header-line.
967 WINDOW must be a live window and defaults to the selected one. */)
968 (Lisp_Object window)
970 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
973 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
974 Swindow_right_divider_width, 0, 1, 0,
975 doc: /* Return the width in pixels of WINDOW's right divider.
976 WINDOW must be a live window and defaults to the selected one. */)
977 (Lisp_Object window)
979 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
982 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
983 Swindow_bottom_divider_width, 0, 1, 0,
984 doc: /* Return the width in pixels of WINDOW's bottom divider.
985 WINDOW must be a live window and defaults to the selected one. */)
986 (Lisp_Object window)
988 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
991 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
992 Swindow_scroll_bar_width, 0, 1, 0,
993 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
994 WINDOW must be a live window and defaults to the selected one. */)
995 (Lisp_Object window)
997 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1000 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1001 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1002 WINDOW must be a live window and defaults to the selected one. */)
1003 (Lisp_Object window)
1005 return make_number (decode_live_window (window)->hscroll);
1008 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1009 range, returning the new amount as a fixnum. */
1010 static Lisp_Object
1011 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1013 /* Horizontal scrolling has problems with large scroll amounts.
1014 It's too slow with long lines, and even with small lines the
1015 display can be messed up. For now, though, impose only the limits
1016 required by the internal representation: horizontal scrolling must
1017 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1018 (since it's stored in a ptrdiff_t). */
1019 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1020 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1022 /* Prevent redisplay shortcuts when changing the hscroll. */
1023 if (w->hscroll != new_hscroll)
1024 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1026 w->hscroll = new_hscroll;
1027 return make_number (new_hscroll);
1030 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1031 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1032 WINDOW must be a live window and defaults to the selected one.
1033 Clip the number to a reasonable value if out of range.
1034 Return the new number. NCOL should be zero or positive.
1036 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1037 window so that the location of point moves off-window. */)
1038 (Lisp_Object window, Lisp_Object ncol)
1040 CHECK_NUMBER (ncol);
1041 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1044 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1045 Swindow_redisplay_end_trigger, 0, 1, 0,
1046 doc: /* Return WINDOW's redisplay end trigger value.
1047 WINDOW must be a live window and defaults to the selected one.
1048 See `set-window-redisplay-end-trigger' for more information. */)
1049 (Lisp_Object window)
1051 return decode_live_window (window)->redisplay_end_trigger;
1054 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1055 Sset_window_redisplay_end_trigger, 2, 2, 0,
1056 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1057 WINDOW must be a live window and defaults to the selected one. VALUE
1058 should be a buffer position (typically a marker) or nil. If it is a
1059 buffer position, then if redisplay in WINDOW reaches a position beyond
1060 VALUE, the functions in `redisplay-end-trigger-functions' are called
1061 with two arguments: WINDOW, and the end trigger value. Afterwards the
1062 end-trigger value is reset to nil. */)
1063 (register Lisp_Object window, Lisp_Object value)
1065 wset_redisplay_end_trigger (decode_live_window (window), value);
1066 return value;
1069 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1070 doc: /* Return a list of the edge coordinates of WINDOW.
1071 WINDOW must be a valid window and defaults to the selected one.
1073 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1074 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1075 0 at top left corner of frame.
1077 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1078 is one more than the bottommost row occupied by WINDOW. The edges
1079 include the space used by WINDOW's scroll bar, display margins, fringes,
1080 header line, and/or mode line. For the edges of just the text area, use
1081 `window-inside-edges'. */)
1082 (Lisp_Object window)
1084 register struct window *w = decode_valid_window (window);
1086 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1087 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1090 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1091 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1092 WINDOW must be a valid window and defaults to the selected one.
1094 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1095 0, 0 at the top left corner of the frame.
1097 RIGHT is one more than the rightmost x position occupied by WINDOW.
1098 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1099 The pixel edges include the space used by WINDOW's scroll bar, display
1100 margins, fringes, header line, and/or mode line. For the pixel edges
1101 of just the text area, use `window-inside-pixel-edges'. */)
1102 (Lisp_Object window)
1104 register struct window *w = decode_valid_window (window);
1106 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1107 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1110 static void
1111 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1113 struct frame *f = XFRAME (w->frame);
1114 *add_y = f->top_pos;
1115 #ifdef FRAME_MENUBAR_HEIGHT
1116 *add_y += FRAME_MENUBAR_HEIGHT (f);
1117 #endif
1118 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1119 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1120 #elif defined (FRAME_TOOLBAR_HEIGHT)
1121 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1122 #endif
1123 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1124 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1125 #endif
1126 *add_x = f->left_pos;
1127 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1128 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1129 #endif
1132 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1133 Swindow_absolute_pixel_edges, 0, 1, 0,
1134 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1135 WINDOW must be a valid window and defaults to the selected one.
1137 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1138 0, 0 at the top left corner of the display.
1140 RIGHT is one more than the rightmost x position occupied by WINDOW.
1141 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1142 The pixel edges include the space used by WINDOW's scroll bar, display
1143 margins, fringes, header line, and/or mode line. For the pixel edges
1144 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1145 (Lisp_Object window)
1147 register struct window *w = decode_valid_window (window);
1148 int add_x, add_y;
1150 calc_absolute_offset (w, &add_x, &add_y);
1152 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1153 WINDOW_TOP_EDGE_Y (w) + add_y,
1154 WINDOW_RIGHT_EDGE_X (w) + add_x,
1155 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1158 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1159 doc: /* Return a list of the edge coordinates of WINDOW.
1160 WINDOW must be a live window and defaults to the selected one.
1162 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1163 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1164 0 at top left corner of frame.
1166 RIGHT is one more than the rightmost column of WINDOW's text area.
1167 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1168 inside edges do not include the space used by the WINDOW's scroll bar,
1169 display margins, fringes, header line, and/or mode line. */)
1170 (Lisp_Object window)
1172 register struct window *w = decode_live_window (window);
1174 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1175 + WINDOW_LEFT_MARGIN_COLS (w)
1176 + WINDOW_LEFT_FRINGE_COLS (w)),
1177 (WINDOW_TOP_EDGE_LINE (w)
1178 + WINDOW_HEADER_LINE_LINES (w)),
1179 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1180 - WINDOW_RIGHT_MARGIN_COLS (w)
1181 - WINDOW_RIGHT_FRINGE_COLS (w)),
1182 (WINDOW_BOTTOM_EDGE_LINE (w)
1183 - WINDOW_MODE_LINE_LINES (w)));
1186 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1187 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1188 WINDOW must be a live window and defaults to the selected one.
1190 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1191 (0,0) at the top left corner of the frame's window area.
1193 RIGHT is one more than the rightmost x position of WINDOW's text area.
1194 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1195 The inside edges do not include the space used by WINDOW's scroll bar,
1196 display margins, fringes, header line, and/or mode line. */)
1197 (Lisp_Object window)
1199 register struct window *w = decode_live_window (window);
1201 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1202 + WINDOW_LEFT_MARGIN_WIDTH (w)
1203 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1204 (WINDOW_TOP_EDGE_Y (w)
1205 + WINDOW_HEADER_LINE_HEIGHT (w)),
1206 (WINDOW_BOX_RIGHT_EDGE_X (w)
1207 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1208 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1209 (WINDOW_BOTTOM_EDGE_Y (w)
1210 - WINDOW_MODE_LINE_HEIGHT (w)));
1213 DEFUN ("window-inside-absolute-pixel-edges",
1214 Fwindow_inside_absolute_pixel_edges,
1215 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1216 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1217 WINDOW must be a live window and defaults to the selected one.
1219 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1220 (0,0) at the top left corner of the frame's window area.
1222 RIGHT is one more than the rightmost x position of WINDOW's text area.
1223 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1224 The inside edges do not include the space used by WINDOW's scroll bar,
1225 display margins, fringes, header line, and/or mode line. */)
1226 (Lisp_Object window)
1228 register struct window *w = decode_live_window (window);
1229 int add_x, add_y;
1231 calc_absolute_offset (w, &add_x, &add_y);
1233 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1234 + WINDOW_LEFT_MARGIN_WIDTH (w)
1235 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1236 (WINDOW_TOP_EDGE_Y (w)
1237 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1238 (WINDOW_BOX_RIGHT_EDGE_X (w)
1239 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1240 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1241 (WINDOW_BOTTOM_EDGE_Y (w)
1242 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1245 /* Test if the character at column X, row Y is within window W.
1246 If it is not, return ON_NOTHING;
1247 if it is on the window's vertical divider, return
1248 ON_RIGHT_DIVIDER;
1249 if it is on the window's horizontal divider, return
1250 ON_BOTTOM_DIVIDER;
1251 if it is in the window's text area, return ON_TEXT;
1252 if it is on the window's modeline, return ON_MODE_LINE;
1253 if it is on the border between the window and its right sibling,
1254 return ON_VERTICAL_BORDER;
1255 if it is on a scroll bar, return ON_SCROLL_BAR;
1256 if it is on the window's top line, return ON_HEADER_LINE;
1257 if it is in left or right fringe of the window,
1258 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1259 if it is in the marginal area to the left/right of the window,
1260 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1262 X and Y are frame relative pixel coordinates. */
1264 static enum window_part
1265 coordinates_in_window (register struct window *w, int x, int y)
1267 struct frame *f = XFRAME (WINDOW_FRAME (w));
1268 enum window_part part;
1269 int ux = FRAME_COLUMN_WIDTH (f);
1270 int left_x = WINDOW_LEFT_EDGE_X (w);
1271 int right_x = WINDOW_RIGHT_EDGE_X (w);
1272 int top_y = WINDOW_TOP_EDGE_Y (w);
1273 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1274 /* The width of the area where the vertical line can be dragged.
1275 (Between mode lines for instance. */
1276 int grabbable_width = ux;
1277 int lmargin_width, rmargin_width, text_left, text_right;
1279 /* Outside any interesting row or column? */
1280 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1281 return ON_NOTHING;
1283 /* On the horizontal window divider (which prevails the vertical
1284 divider)? */
1285 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1286 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1287 && y <= bottom_y)
1288 return ON_BOTTOM_DIVIDER;
1289 /* On vertical window divider? */
1290 else if (!WINDOW_RIGHTMOST_P (w)
1291 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1292 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1293 && x <= right_x)
1294 return ON_RIGHT_DIVIDER;
1295 /* On the mode or header line? */
1296 else if ((WINDOW_WANTS_MODELINE_P (w)
1297 && y >= (bottom_y
1298 - CURRENT_MODE_LINE_HEIGHT (w)
1299 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1300 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1301 && (part = ON_MODE_LINE))
1302 || (WINDOW_WANTS_HEADER_LINE_P (w)
1303 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1304 && (part = ON_HEADER_LINE)))
1306 /* If it's under/over the scroll bar portion of the mode/header
1307 line, say it's on the vertical line. That's to be able to
1308 resize windows horizontally in case we're using toolkit scroll
1309 bars. Note: If scrollbars are on the left, the window that
1310 must be eventually resized is that on the left of WINDOW. */
1311 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1312 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1313 && !WINDOW_LEFTMOST_P (w)
1314 && eabs (x - left_x) < grabbable_width)
1315 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1316 && !WINDOW_RIGHTMOST_P (w)
1317 && eabs (x - right_x) < grabbable_width)))
1318 return ON_VERTICAL_BORDER;
1319 else
1320 return part;
1323 /* In what's below, we subtract 1 when computing right_x because we
1324 want the rightmost pixel, which is given by left_pixel+width-1. */
1325 if (w->pseudo_window_p)
1327 left_x = 0;
1328 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1330 else
1332 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1333 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1336 /* Outside any interesting column? */
1337 if (x < left_x || x > right_x)
1338 return ON_SCROLL_BAR;
1340 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1341 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1343 text_left = window_box_left (w, TEXT_AREA);
1344 text_right = text_left + window_box_width (w, TEXT_AREA);
1346 if (FRAME_WINDOW_P (f))
1348 if (!w->pseudo_window_p
1349 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1350 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1351 && !WINDOW_RIGHTMOST_P (w)
1352 && (eabs (x - right_x) < grabbable_width))
1353 return ON_VERTICAL_BORDER;
1355 /* Need to say "x > right_x" rather than >=, since on character
1356 terminals, the vertical line's x coordinate is right_x. */
1357 else if (!w->pseudo_window_p
1358 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1359 && !WINDOW_RIGHTMOST_P (w)
1360 /* Why check ux if we are not the rightmost window? Also
1361 shouldn't a pseudo window always be rightmost? */
1362 && x > right_x - ux)
1363 return ON_VERTICAL_BORDER;
1365 if (x < text_left)
1367 if (lmargin_width > 0
1368 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1369 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1370 : (x < left_x + lmargin_width)))
1371 return ON_LEFT_MARGIN;
1372 else
1373 return ON_LEFT_FRINGE;
1376 if (x >= text_right)
1378 if (rmargin_width > 0
1379 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1380 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1381 : (x >= right_x - rmargin_width)))
1382 return ON_RIGHT_MARGIN;
1383 else
1384 return ON_RIGHT_FRINGE;
1387 /* Everything special ruled out - must be on text area */
1388 return ON_TEXT;
1391 /* Take X is the frame-relative pixel x-coordinate, and return the
1392 x-coordinate relative to part PART of window W. */
1394 window_relative_x_coord (struct window *w, enum window_part part, int x)
1396 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1398 switch (part)
1400 case ON_TEXT:
1401 return x - window_box_left (w, TEXT_AREA);
1403 case ON_HEADER_LINE:
1404 case ON_MODE_LINE:
1405 case ON_LEFT_FRINGE:
1406 return x - left_x;
1408 case ON_RIGHT_FRINGE:
1409 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1411 case ON_LEFT_MARGIN:
1412 return (x - left_x
1413 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1414 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1416 case ON_RIGHT_MARGIN:
1417 return (x + 1
1418 - ((w->pseudo_window_p)
1419 ? WINDOW_PIXEL_WIDTH (w)
1420 : WINDOW_BOX_RIGHT_EDGE_X (w))
1421 + window_box_width (w, RIGHT_MARGIN_AREA)
1422 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1423 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1426 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1427 return 0;
1431 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1432 Scoordinates_in_window_p, 2, 2, 0,
1433 doc: /* Return non-nil if COORDINATES are in WINDOW.
1434 WINDOW must be a live window and defaults to the selected one.
1435 COORDINATES is a cons of the form (X . Y), X and Y being distances
1436 measured in characters from the upper-left corner of the frame.
1437 \(0 . 0) denotes the character in the upper left corner of the
1438 frame.
1439 If COORDINATES are in the text portion of WINDOW,
1440 the coordinates relative to the window are returned.
1441 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1442 If they are in the right divider of WINDOW, `right-divider' is returned.
1443 If they are in the mode line of WINDOW, `mode-line' is returned.
1444 If they are in the header line of WINDOW, `header-line' is returned.
1445 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1446 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1447 If they are on the border between WINDOW and its right sibling,
1448 `vertical-line' is returned.
1449 If they are in the windows's left or right marginal areas, `left-margin'\n\
1450 or `right-margin' is returned. */)
1451 (register Lisp_Object coordinates, Lisp_Object window)
1453 struct window *w;
1454 struct frame *f;
1455 int x, y;
1456 Lisp_Object lx, ly;
1458 w = decode_live_window (window);
1459 f = XFRAME (w->frame);
1460 CHECK_CONS (coordinates);
1461 lx = Fcar (coordinates);
1462 ly = Fcdr (coordinates);
1463 CHECK_NUMBER_OR_FLOAT (lx);
1464 CHECK_NUMBER_OR_FLOAT (ly);
1465 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1466 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1468 switch (coordinates_in_window (w, x, y))
1470 case ON_NOTHING:
1471 return Qnil;
1473 case ON_TEXT:
1474 /* Convert X and Y to window relative pixel coordinates, and
1475 return the canonical char units. */
1476 x -= window_box_left (w, TEXT_AREA);
1477 y -= WINDOW_TOP_EDGE_Y (w);
1478 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1479 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1481 case ON_MODE_LINE:
1482 return Qmode_line;
1484 case ON_VERTICAL_BORDER:
1485 return Qvertical_line;
1487 case ON_HEADER_LINE:
1488 return Qheader_line;
1490 case ON_LEFT_FRINGE:
1491 return Qleft_fringe;
1493 case ON_RIGHT_FRINGE:
1494 return Qright_fringe;
1496 case ON_LEFT_MARGIN:
1497 return Qleft_margin;
1499 case ON_RIGHT_MARGIN:
1500 return Qright_margin;
1502 case ON_SCROLL_BAR:
1503 /* Historically we are supposed to return nil in this case. */
1504 return Qnil;
1506 case ON_RIGHT_DIVIDER:
1507 return Qright_divider;
1509 case ON_BOTTOM_DIVIDER:
1510 return Qbottom_divider;
1512 default:
1513 emacs_abort ();
1518 /* Callback for foreach_window, used in window_from_coordinates.
1519 Check if window W contains coordinates specified by USER_DATA which
1520 is actually a pointer to a struct check_window_data CW.
1522 Check if window W contains coordinates *CW->x and *CW->y. If it
1523 does, return W in *CW->window, as Lisp_Object, and return in
1524 *CW->part the part of the window under coordinates *X,*Y. Return
1525 zero from this function to stop iterating over windows. */
1527 struct check_window_data
1529 Lisp_Object *window;
1530 int x, y;
1531 enum window_part *part;
1534 static int
1535 check_window_containing (struct window *w, void *user_data)
1537 struct check_window_data *cw = user_data;
1538 enum window_part found;
1539 int continue_p = 1;
1541 found = coordinates_in_window (w, cw->x, cw->y);
1542 if (found != ON_NOTHING)
1544 *cw->part = found;
1545 XSETWINDOW (*cw->window, w);
1546 continue_p = 0;
1549 return continue_p;
1553 /* Find the window containing frame-relative pixel position X/Y and
1554 return it as a Lisp_Object.
1556 If X, Y is on one of the window's special `window_part' elements,
1557 set *PART to the id of that element.
1559 If there is no window under X, Y return nil and leave *PART
1560 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1562 This function was previously implemented with a loop cycling over
1563 windows with Fnext_window, and starting with the frame's selected
1564 window. It turned out that this doesn't work with an
1565 implementation of next_window using Vwindow_list, because
1566 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1567 tree of F when this function is called asynchronously from
1568 note_mouse_highlight. The original loop didn't terminate in this
1569 case. */
1571 Lisp_Object
1572 window_from_coordinates (struct frame *f, int x, int y,
1573 enum window_part *part, bool tool_bar_p)
1575 Lisp_Object window;
1576 struct check_window_data cw;
1577 enum window_part dummy;
1579 if (part == 0)
1580 part = &dummy;
1582 window = Qnil;
1583 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1584 foreach_window (f, check_window_containing, &cw);
1586 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1587 /* If not found above, see if it's in the tool bar window, if a tool
1588 bar exists. */
1589 if (NILP (window)
1590 && tool_bar_p
1591 && WINDOWP (f->tool_bar_window)
1592 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1593 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1594 != ON_NOTHING))
1596 *part = ON_TEXT;
1597 window = f->tool_bar_window;
1599 #endif
1601 return window;
1604 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1605 doc: /* Return window containing coordinates X and Y on FRAME.
1606 FRAME must be a live frame and defaults to the selected one.
1607 The top left corner of the frame is considered to be row 0,
1608 column 0. */)
1609 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1611 struct frame *f = decode_live_frame (frame);
1613 /* Check that arguments are integers or floats. */
1614 CHECK_NUMBER_OR_FLOAT (x);
1615 CHECK_NUMBER_OR_FLOAT (y);
1617 return window_from_coordinates (f,
1618 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1619 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1620 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1621 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1622 0, 0);
1625 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1626 doc: /* Return current value of point in WINDOW.
1627 WINDOW must be a live window and defaults to the selected one.
1629 For a nonselected window, this is the value point would have if that
1630 window were selected.
1632 Note that, when WINDOW is selected, the value returned is the same as
1633 that returned by `point' for WINDOW's buffer. It would be more strictly
1634 correct to return the top-level value of `point', outside of any
1635 `save-excursion' forms. But that is hard to define. */)
1636 (Lisp_Object window)
1638 register struct window *w = decode_live_window (window);
1640 if (w == XWINDOW (selected_window))
1641 return make_number (BUF_PT (XBUFFER (w->contents)));
1642 else
1643 return Fmarker_position (w->pointm);
1646 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1647 doc: /* Return position at which display currently starts in WINDOW.
1648 WINDOW must be a live window and defaults to the selected one.
1649 This is updated by redisplay or by calling `set-window-start'. */)
1650 (Lisp_Object window)
1652 return Fmarker_position (decode_live_window (window)->start);
1655 /* This is text temporarily removed from the doc string below.
1657 This function returns nil if the position is not currently known.
1658 That happens when redisplay is preempted and doesn't finish.
1659 If in that case you want to compute where the end of the window would
1660 have been if redisplay had finished, do this:
1661 (save-excursion
1662 (goto-char (window-start window))
1663 (vertical-motion (1- (window-height window)) window)
1664 (point))") */
1666 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1667 doc: /* Return position at which display currently ends in WINDOW.
1668 WINDOW must be a live window and defaults to the selected one.
1669 This is updated by redisplay, when it runs to completion.
1670 Simply changing the buffer text or setting `window-start'
1671 does not update this value.
1672 Return nil if there is no recorded value. (This can happen if the
1673 last redisplay of WINDOW was preempted, and did not finish.)
1674 If UPDATE is non-nil, compute the up-to-date position
1675 if it isn't already recorded. */)
1676 (Lisp_Object window, Lisp_Object update)
1678 Lisp_Object value;
1679 struct window *w = decode_live_window (window);
1680 Lisp_Object buf;
1681 struct buffer *b;
1683 buf = w->contents;
1684 CHECK_BUFFER (buf);
1685 b = XBUFFER (buf);
1687 if (! NILP (update)
1688 && (windows_or_buffers_changed
1689 || !w->window_end_valid
1690 || b->clip_changed
1691 || b->prevent_redisplay_optimizations_p
1692 || window_outdated (w))
1693 && !noninteractive)
1695 struct text_pos startp;
1696 struct it it;
1697 struct buffer *old_buffer = NULL;
1698 void *itdata = NULL;
1700 /* Cannot use Fvertical_motion because that function doesn't
1701 cope with variable-height lines. */
1702 if (b != current_buffer)
1704 old_buffer = current_buffer;
1705 set_buffer_internal (b);
1708 /* In case W->start is out of the range, use something
1709 reasonable. This situation occurred when loading a file with
1710 `-l' containing a call to `rmail' with subsequent other
1711 commands. At the end, W->start happened to be BEG, while
1712 rmail had already narrowed the buffer. */
1713 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1715 itdata = bidi_shelve_cache ();
1716 start_display (&it, w, startp);
1717 move_it_vertically (&it, window_box_height (w));
1718 if (it.current_y < it.last_visible_y)
1719 move_it_past_eol (&it);
1720 value = make_number (IT_CHARPOS (it));
1721 bidi_unshelve_cache (itdata, 0);
1723 if (old_buffer)
1724 set_buffer_internal (old_buffer);
1726 else
1727 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1729 return value;
1732 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1733 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1734 WINDOW must be a live window and defaults to the selected one.
1735 Return POS. */)
1736 (Lisp_Object window, Lisp_Object pos)
1738 register struct window *w = decode_live_window (window);
1740 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1742 if (w == XWINDOW (selected_window))
1744 if (XBUFFER (w->contents) == current_buffer)
1745 Fgoto_char (pos);
1746 else
1748 struct buffer *old_buffer = current_buffer;
1750 /* ... but here we want to catch type error before buffer change. */
1751 CHECK_NUMBER_COERCE_MARKER (pos);
1752 set_buffer_internal (XBUFFER (w->contents));
1753 Fgoto_char (pos);
1754 set_buffer_internal (old_buffer);
1757 else
1759 set_marker_restricted (w->pointm, pos, w->contents);
1760 /* We have to make sure that redisplay updates the window to show
1761 the new value of point. */
1762 wset_redisplay (w);
1765 return pos;
1768 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1769 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1770 WINDOW must be a live window and defaults to the selected one. Return
1771 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1772 overriding motion of point in order to display at this exact start. */)
1773 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1775 register struct window *w = decode_live_window (window);
1777 set_marker_restricted (w->start, pos, w->contents);
1778 /* This is not right, but much easier than doing what is right. */
1779 w->start_at_line_beg = 0;
1780 if (NILP (noforce))
1781 w->force_start = 1;
1782 w->update_mode_line = 1;
1783 /* Bug#15957. */
1784 w->window_end_valid = 0;
1785 wset_redisplay (w);
1787 return pos;
1790 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1791 Spos_visible_in_window_p, 0, 3, 0,
1792 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1793 WINDOW must be a live window and defaults to the selected one.
1795 Return nil if that position is scrolled vertically out of view. If a
1796 character is only partially visible, nil is returned, unless the
1797 optional argument PARTIALLY is non-nil. If POS is only out of view
1798 because of horizontal scrolling, return non-nil. If POS is t, it
1799 specifies the position of the last visible glyph in WINDOW. POS
1800 defaults to point in WINDOW; WINDOW defaults to the selected window.
1802 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1803 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1804 where X and Y are the pixel coordinates relative to the top left corner
1805 of the window. The remaining elements are omitted if the character after
1806 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1807 off-window at the top and bottom of the screen line ("row") containing
1808 POS, ROWH is the visible height of that row, and VPOS is the row number
1809 \(zero-based). */)
1810 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1812 register struct window *w;
1813 register EMACS_INT posint;
1814 register struct buffer *buf;
1815 struct text_pos top;
1816 Lisp_Object in_window = Qnil;
1817 int rtop, rbot, rowh, vpos, fully_p = 1;
1818 int x, y;
1820 w = decode_live_window (window);
1821 buf = XBUFFER (w->contents);
1822 SET_TEXT_POS_FROM_MARKER (top, w->start);
1824 if (EQ (pos, Qt))
1825 posint = -1;
1826 else if (!NILP (pos))
1828 CHECK_NUMBER_COERCE_MARKER (pos);
1829 posint = XINT (pos);
1831 else if (w == XWINDOW (selected_window))
1832 posint = PT;
1833 else
1834 posint = marker_position (w->pointm);
1836 /* If position is above window start or outside buffer boundaries,
1837 or if window start is out of range, position is not visible. */
1838 if ((EQ (pos, Qt)
1839 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1840 && CHARPOS (top) >= BUF_BEGV (buf)
1841 && CHARPOS (top) <= BUF_ZV (buf)
1842 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1843 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1844 in_window = Qt;
1846 if (!NILP (in_window) && !NILP (partially))
1848 Lisp_Object part = Qnil;
1849 if (!fully_p)
1850 part = list4i (rtop, rbot, rowh, vpos);
1851 in_window = Fcons (make_number (x),
1852 Fcons (make_number (y), part));
1855 return in_window;
1858 DEFUN ("window-line-height", Fwindow_line_height,
1859 Swindow_line_height, 0, 2, 0,
1860 doc: /* Return height in pixels of text line LINE in window WINDOW.
1861 WINDOW must be a live window and defaults to the selected one.
1863 Return height of current line if LINE is omitted or nil. Return height of
1864 header or mode line if LINE is `header-line' or `mode-line'.
1865 Otherwise, LINE is a text line number starting from 0. A negative number
1866 counts from the end of the window.
1868 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1869 in pixels of the visible part of the line, VPOS and YPOS are the
1870 vertical position in lines and pixels of the line, relative to the top
1871 of the first text line, and OFFBOT is the number of off-window pixels at
1872 the bottom of the text line. If there are off-window pixels at the top
1873 of the (first) text line, YPOS is negative.
1875 Return nil if window display is not up-to-date. In that case, use
1876 `pos-visible-in-window-p' to obtain the information. */)
1877 (Lisp_Object line, Lisp_Object window)
1879 register struct window *w;
1880 register struct buffer *b;
1881 struct glyph_row *row, *end_row;
1882 int max_y, crop, i;
1883 EMACS_INT n;
1885 w = decode_live_window (window);
1887 if (noninteractive || w->pseudo_window_p)
1888 return Qnil;
1890 CHECK_BUFFER (w->contents);
1891 b = XBUFFER (w->contents);
1893 /* Fail if current matrix is not up-to-date. */
1894 if (!w->window_end_valid
1895 || windows_or_buffers_changed
1896 || b->clip_changed
1897 || b->prevent_redisplay_optimizations_p
1898 || window_outdated (w))
1899 return Qnil;
1901 if (NILP (line))
1903 i = w->cursor.vpos;
1904 if (i < 0 || i >= w->current_matrix->nrows
1905 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1906 return Qnil;
1907 max_y = window_text_bottom_y (w);
1908 goto found_row;
1911 if (EQ (line, Qheader_line))
1913 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1914 return Qnil;
1915 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1916 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1919 if (EQ (line, Qmode_line))
1921 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1922 return (row->enabled_p ?
1923 list4i (row->height,
1924 0, /* not accurate */
1925 (WINDOW_HEADER_LINE_HEIGHT (w)
1926 + window_text_bottom_y (w)),
1928 : Qnil);
1931 CHECK_NUMBER (line);
1932 n = XINT (line);
1934 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1935 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1936 max_y = window_text_bottom_y (w);
1937 i = 0;
1939 while ((n < 0 || i < n)
1940 && row <= end_row && row->enabled_p
1941 && row->y + row->height < max_y)
1942 row++, i++;
1944 if (row > end_row || !row->enabled_p)
1945 return Qnil;
1947 if (++n < 0)
1949 if (-n > i)
1950 return Qnil;
1951 row += n;
1952 i += n;
1955 found_row:
1956 crop = max (0, (row->y + row->height) - max_y);
1957 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1960 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1961 0, 1, 0,
1962 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1963 More precisely, return the value assigned by the last call of
1964 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1965 never called with WINDOW as its argument, or the value set by that
1966 function was internally reset since its last call. WINDOW must be a
1967 live window and defaults to the selected one.
1969 When a window is dedicated to its buffer, `display-buffer' will refrain
1970 from displaying another buffer in it. `get-lru-window' and
1971 `get-largest-window' treat dedicated windows specially.
1972 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1973 `kill-buffer' can delete a dedicated window and the containing frame.
1975 Functions like `set-window-buffer' may change the buffer displayed by a
1976 window, unless that window is "strongly" dedicated to its buffer, that
1977 is the value returned by `window-dedicated-p' is t. */)
1978 (Lisp_Object window)
1980 return decode_live_window (window)->dedicated;
1983 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1984 Sset_window_dedicated_p, 2, 2, 0,
1985 doc: /* Mark WINDOW as dedicated according to FLAG.
1986 WINDOW must be a live window and defaults to the selected one. FLAG
1987 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1988 mark WINDOW as non-dedicated. Return FLAG.
1990 When a window is dedicated to its buffer, `display-buffer' will refrain
1991 from displaying another buffer in it. `get-lru-window' and
1992 `get-largest-window' treat dedicated windows specially.
1993 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1994 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1995 and the containing frame.
1997 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1998 its buffer. Functions like `set-window-buffer' may change the buffer
1999 displayed by a window, unless that window is strongly dedicated to its
2000 buffer. If and when `set-window-buffer' displays another buffer in a
2001 window, it also makes sure that the window is no more dedicated. */)
2002 (Lisp_Object window, Lisp_Object flag)
2004 wset_dedicated (decode_live_window (window), flag);
2005 return flag;
2008 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2009 0, 1, 0,
2010 doc: /* Return buffers previously shown in WINDOW.
2011 WINDOW must be a live window and defaults to the selected one.
2013 The return value is a list of elements (BUFFER WINDOW-START POS),
2014 where BUFFER is a buffer, WINDOW-START is the start position of the
2015 window for that buffer, and POS is a window-specific point value. */)
2016 (Lisp_Object window)
2018 return decode_live_window (window)->prev_buffers;
2021 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2022 Sset_window_prev_buffers, 2, 2, 0,
2023 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2024 WINDOW must be a live window and defaults to the selected one.
2026 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2027 where BUFFER is a buffer, WINDOW-START is the start position of the
2028 window for that buffer, and POS is a window-specific point value. */)
2029 (Lisp_Object window, Lisp_Object prev_buffers)
2031 wset_prev_buffers (decode_live_window (window), prev_buffers);
2032 return prev_buffers;
2035 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2036 0, 1, 0,
2037 doc: /* Return list of buffers recently re-shown in WINDOW.
2038 WINDOW must be a live window and defaults to the selected one. */)
2039 (Lisp_Object window)
2041 return decode_live_window (window)->next_buffers;
2044 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2045 Sset_window_next_buffers, 2, 2, 0,
2046 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2047 WINDOW must be a live window and defaults to the selected one.
2048 NEXT-BUFFERS should be a list of buffers. */)
2049 (Lisp_Object window, Lisp_Object next_buffers)
2051 wset_next_buffers (decode_live_window (window), next_buffers);
2052 return next_buffers;
2055 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2056 0, 1, 0,
2057 doc: /* Return the parameters of WINDOW and their values.
2058 WINDOW must be a valid window and defaults to the selected one. The
2059 return value is a list of elements of the form (PARAMETER . VALUE). */)
2060 (Lisp_Object window)
2062 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2065 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2066 2, 2, 0,
2067 doc: /* Return WINDOW's value for PARAMETER.
2068 WINDOW can be any window and defaults to the selected one. */)
2069 (Lisp_Object window, Lisp_Object parameter)
2071 Lisp_Object result;
2073 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2074 return CDR_SAFE (result);
2077 DEFUN ("set-window-parameter", Fset_window_parameter,
2078 Sset_window_parameter, 3, 3, 0,
2079 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2080 WINDOW can be any window and defaults to the selected one.
2081 Return VALUE. */)
2082 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2084 register struct window *w = decode_any_window (window);
2085 Lisp_Object old_alist_elt;
2087 old_alist_elt = Fassq (parameter, w->window_parameters);
2088 if (NILP (old_alist_elt))
2089 wset_window_parameters
2090 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2091 else
2092 Fsetcdr (old_alist_elt, value);
2093 return value;
2096 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2097 0, 1, 0,
2098 doc: /* Return the display-table that WINDOW is using.
2099 WINDOW must be a live window and defaults to the selected one. */)
2100 (Lisp_Object window)
2102 return decode_live_window (window)->display_table;
2105 /* Get the display table for use on window W. This is either W's
2106 display table or W's buffer's display table. Ignore the specified
2107 tables if they are not valid; if no valid table is specified,
2108 return 0. */
2110 struct Lisp_Char_Table *
2111 window_display_table (struct window *w)
2113 struct Lisp_Char_Table *dp = NULL;
2115 if (DISP_TABLE_P (w->display_table))
2116 dp = XCHAR_TABLE (w->display_table);
2117 else if (BUFFERP (w->contents))
2119 struct buffer *b = XBUFFER (w->contents);
2121 if (DISP_TABLE_P (BVAR (b, display_table)))
2122 dp = XCHAR_TABLE (BVAR (b, display_table));
2123 else if (DISP_TABLE_P (Vstandard_display_table))
2124 dp = XCHAR_TABLE (Vstandard_display_table);
2127 return dp;
2130 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2131 doc: /* Set WINDOW's display-table to TABLE.
2132 WINDOW must be a live window and defaults to the selected one. */)
2133 (register Lisp_Object window, Lisp_Object table)
2135 wset_display_table (decode_live_window (window), table);
2136 return table;
2139 /* Record info on buffer window W is displaying
2140 when it is about to cease to display that buffer. */
2141 static void
2142 unshow_buffer (register struct window *w)
2144 Lisp_Object buf = w->contents;
2145 struct buffer *b = XBUFFER (buf);
2147 eassert (b == XMARKER (w->pointm)->buffer);
2149 #if 0
2150 if (w == XWINDOW (selected_window)
2151 || ! EQ (buf, XWINDOW (selected_window)->contents))
2152 /* Do this except when the selected window's buffer
2153 is being removed from some other window. */
2154 #endif
2155 /* last_window_start records the start position that this buffer
2156 had in the last window to be disconnected from it.
2157 Now that this statement is unconditional,
2158 it is possible for the buffer to be displayed in the
2159 selected window, while last_window_start reflects another
2160 window which was recently showing the same buffer.
2161 Some people might say that might be a good thing. Let's see. */
2162 b->last_window_start = marker_position (w->start);
2164 /* Point in the selected window's buffer
2165 is actually stored in that buffer, and the window's pointm isn't used.
2166 So don't clobber point in that buffer. */
2167 if (! EQ (buf, XWINDOW (selected_window)->contents)
2168 /* Don't clobber point in current buffer either (this could be
2169 useful in connection with bug#12208).
2170 && XBUFFER (buf) != current_buffer */
2171 /* This line helps to fix Horsley's testbug.el bug. */
2172 && !(WINDOWP (BVAR (b, last_selected_window))
2173 && w != XWINDOW (BVAR (b, last_selected_window))
2174 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2175 temp_set_point_both (b,
2176 clip_to_bounds (BUF_BEGV (b),
2177 marker_position (w->pointm),
2178 BUF_ZV (b)),
2179 clip_to_bounds (BUF_BEGV_BYTE (b),
2180 marker_byte_position (w->pointm),
2181 BUF_ZV_BYTE (b)));
2183 if (WINDOWP (BVAR (b, last_selected_window))
2184 && w == XWINDOW (BVAR (b, last_selected_window)))
2185 bset_last_selected_window (b, Qnil);
2188 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2189 means change window structure only. Otherwise store geometry and
2190 other settings as well. */
2191 static void
2192 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2194 register Lisp_Object tem;
2195 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2197 /* If OLD is its frame's root window, then NEW is the new
2198 root window for that frame. */
2199 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2200 fset_root_window (XFRAME (o->frame), new);
2202 if (setflag)
2204 n->pixel_left = o->pixel_left;
2205 n->pixel_top = o->pixel_top;
2206 n->pixel_width = o->pixel_width;
2207 n->pixel_height = o->pixel_height;
2208 n->left_col = o->left_col;
2209 n->top_line = o->top_line;
2210 n->total_cols = o->total_cols;
2211 n->total_lines = o->total_lines;
2212 wset_normal_cols (n, o->normal_cols);
2213 wset_normal_cols (o, make_float (1.0));
2214 wset_normal_lines (n, o->normal_lines);
2215 wset_normal_lines (o, make_float (1.0));
2216 n->desired_matrix = n->current_matrix = 0;
2217 n->vscroll = 0;
2218 memset (&n->cursor, 0, sizeof (n->cursor));
2219 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2220 n->last_cursor_vpos = 0;
2221 #ifdef HAVE_WINDOW_SYSTEM
2222 n->phys_cursor_type = NO_CURSOR;
2223 n->phys_cursor_width = -1;
2224 #endif
2225 n->must_be_updated_p = 0;
2226 n->pseudo_window_p = 0;
2227 n->window_end_vpos = 0;
2228 n->window_end_pos = 0;
2229 n->window_end_valid = 0;
2232 tem = o->next;
2233 wset_next (n, tem);
2234 if (!NILP (tem))
2235 wset_prev (XWINDOW (tem), new);
2237 tem = o->prev;
2238 wset_prev (n, tem);
2239 if (!NILP (tem))
2240 wset_next (XWINDOW (tem), new);
2242 tem = o->parent;
2243 wset_parent (n, tem);
2244 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2245 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2248 /* If window WINDOW and its parent window are iso-combined, merge
2249 WINDOW's children into those of its parent window and mark WINDOW as
2250 deleted. */
2252 static void
2253 recombine_windows (Lisp_Object window)
2255 struct window *w, *p, *c;
2256 Lisp_Object parent, child;
2257 bool horflag;
2259 w = XWINDOW (window);
2260 parent = w->parent;
2261 if (!NILP (parent) && NILP (w->combination_limit))
2263 p = XWINDOW (parent);
2264 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2265 && p->horizontal == w->horizontal)
2266 /* WINDOW and PARENT are both either a vertical or a horizontal
2267 combination. */
2269 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2270 child = w->contents;
2271 c = XWINDOW (child);
2273 /* Splice WINDOW's children into its parent's children and
2274 assign new normal sizes. */
2275 if (NILP (w->prev))
2276 wset_combination (p, horflag, child);
2277 else
2279 wset_prev (c, w->prev);
2280 wset_next (XWINDOW (w->prev), child);
2283 while (c)
2285 wset_parent (c, parent);
2287 if (horflag)
2288 wset_normal_cols
2289 (c, make_float ((double) c->pixel_width
2290 / (double) p->pixel_width));
2291 else
2292 wset_normal_lines
2293 (c, make_float ((double) c->pixel_height
2294 / (double) p->pixel_height));
2296 if (NILP (c->next))
2298 if (!NILP (w->next))
2300 wset_next (c, w->next);
2301 wset_prev (XWINDOW (c->next), child);
2304 c = 0;
2306 else
2308 child = c->next;
2309 c = XWINDOW (child);
2313 /* WINDOW can be deleted now. */
2314 wset_combination (w, 0, Qnil);
2319 /* If WINDOW can be deleted, delete it. */
2320 static void
2321 delete_deletable_window (Lisp_Object window)
2323 if (!NILP (call1 (Qwindow_deletable_p, window)))
2324 call1 (Qdelete_window, window);
2327 /***********************************************************************
2328 Window List
2329 ***********************************************************************/
2331 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2332 pointer. This is a callback function for foreach_window, used in
2333 the window_list function. */
2335 static int
2336 add_window_to_list (struct window *w, void *user_data)
2338 Lisp_Object *list = user_data;
2339 Lisp_Object window;
2340 XSETWINDOW (window, w);
2341 *list = Fcons (window, *list);
2342 return 1;
2346 /* Return a list of all windows, for use by next_window. If
2347 Vwindow_list is a list, return that list. Otherwise, build a new
2348 list, cache it in Vwindow_list, and return that. */
2350 Lisp_Object
2351 window_list (void)
2353 if (!CONSP (Vwindow_list))
2355 Lisp_Object tail, frame;
2357 Vwindow_list = Qnil;
2358 FOR_EACH_FRAME (tail, frame)
2360 Lisp_Object args[2];
2362 /* We are visiting windows in canonical order, and add
2363 new windows at the front of args[1], which means we
2364 have to reverse this list at the end. */
2365 args[1] = Qnil;
2366 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2367 args[0] = Vwindow_list;
2368 args[1] = Fnreverse (args[1]);
2369 Vwindow_list = Fnconc (2, args);
2373 return Vwindow_list;
2377 /* Value is non-zero if WINDOW satisfies the constraints given by
2378 OWINDOW, MINIBUF and ALL_FRAMES.
2380 MINIBUF t means WINDOW may be minibuffer windows.
2381 `lambda' means WINDOW may not be a minibuffer window.
2382 a window means a specific minibuffer window
2384 ALL_FRAMES t means search all frames,
2385 nil means search just current frame,
2386 `visible' means search just visible frames on the
2387 current terminal,
2388 0 means search visible and iconified frames on the
2389 current terminal,
2390 a window means search the frame that window belongs to,
2391 a frame means consider windows on that frame, only. */
2393 static bool
2394 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2395 Lisp_Object minibuf, Lisp_Object all_frames)
2397 struct window *w = XWINDOW (window);
2398 struct frame *f = XFRAME (w->frame);
2399 bool candidate_p = 1;
2401 if (!BUFFERP (w->contents))
2402 candidate_p = 0;
2403 else if (MINI_WINDOW_P (w)
2404 && (EQ (minibuf, Qlambda)
2405 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2407 /* If MINIBUF is `lambda' don't consider any mini-windows.
2408 If it is a window, consider only that one. */
2409 candidate_p = 0;
2411 else if (EQ (all_frames, Qt))
2412 candidate_p = 1;
2413 else if (NILP (all_frames))
2415 eassert (WINDOWP (owindow));
2416 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2418 else if (EQ (all_frames, Qvisible))
2420 candidate_p = FRAME_VISIBLE_P (f)
2421 && (FRAME_TERMINAL (XFRAME (w->frame))
2422 == FRAME_TERMINAL (XFRAME (selected_frame)));
2425 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2427 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2428 #ifdef HAVE_X_WINDOWS
2429 /* Yuck!! If we've just created the frame and the
2430 window-manager requested the user to place it
2431 manually, the window may still not be considered
2432 `visible'. I'd argue it should be at least
2433 something like `iconified', but don't know how to do
2434 that yet. --Stef */
2435 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2436 && !f->output_data.x->has_been_visible)
2437 #endif
2439 && (FRAME_TERMINAL (XFRAME (w->frame))
2440 == FRAME_TERMINAL (XFRAME (selected_frame)));
2442 else if (WINDOWP (all_frames))
2443 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2444 || EQ (XWINDOW (all_frames)->frame, w->frame)
2445 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2446 else if (FRAMEP (all_frames))
2447 candidate_p = EQ (all_frames, w->frame);
2449 return candidate_p;
2453 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2454 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2455 MINIBUF, and ALL_FRAMES. */
2457 static void
2458 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2460 struct window *w = decode_live_window (*window);
2462 XSETWINDOW (*window, w);
2463 /* MINIBUF nil may or may not include minibuffers. Decide if it
2464 does. */
2465 if (NILP (*minibuf))
2466 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2467 else if (!EQ (*minibuf, Qt))
2468 *minibuf = Qlambda;
2470 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2471 => count none of them, or a specific minibuffer window (the
2472 active one) to count. */
2474 /* ALL_FRAMES nil doesn't specify which frames to include. */
2475 if (NILP (*all_frames))
2476 *all_frames
2477 = (!EQ (*minibuf, Qlambda)
2478 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2479 : Qnil);
2480 else if (EQ (*all_frames, Qvisible))
2482 else if (EQ (*all_frames, make_number (0)))
2484 else if (FRAMEP (*all_frames))
2486 else if (!EQ (*all_frames, Qt))
2487 *all_frames = Qnil;
2491 /* Return the next or previous window of WINDOW in cyclic ordering
2492 of windows. NEXT_P non-zero means return the next window. See the
2493 documentation string of next-window for the meaning of MINIBUF and
2494 ALL_FRAMES. */
2496 static Lisp_Object
2497 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2499 decode_next_window_args (&window, &minibuf, &all_frames);
2501 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2502 return the first window on the frame. */
2503 if (FRAMEP (all_frames)
2504 && !EQ (all_frames, XWINDOW (window)->frame))
2505 return Fframe_first_window (all_frames);
2507 if (next_p)
2509 Lisp_Object list;
2511 /* Find WINDOW in the list of all windows. */
2512 list = Fmemq (window, window_list ());
2514 /* Scan forward from WINDOW to the end of the window list. */
2515 if (CONSP (list))
2516 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2517 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2518 break;
2520 /* Scan from the start of the window list up to WINDOW. */
2521 if (!CONSP (list))
2522 for (list = Vwindow_list;
2523 CONSP (list) && !EQ (XCAR (list), window);
2524 list = XCDR (list))
2525 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2526 break;
2528 if (CONSP (list))
2529 window = XCAR (list);
2531 else
2533 Lisp_Object candidate, list;
2535 /* Scan through the list of windows for candidates. If there are
2536 candidate windows in front of WINDOW, the last one of these
2537 is the one we want. If there are candidates following WINDOW
2538 in the list, again the last one of these is the one we want. */
2539 candidate = Qnil;
2540 for (list = window_list (); CONSP (list); list = XCDR (list))
2542 if (EQ (XCAR (list), window))
2544 if (WINDOWP (candidate))
2545 break;
2547 else if (candidate_window_p (XCAR (list), window, minibuf,
2548 all_frames))
2549 candidate = XCAR (list);
2552 if (WINDOWP (candidate))
2553 window = candidate;
2556 return window;
2560 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2561 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2562 WINDOW must be a live window and defaults to the selected one. The
2563 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2564 consider.
2566 MINIBUF nil or omitted means consider the minibuffer window only if the
2567 minibuffer is active. MINIBUF t means consider the minibuffer window
2568 even if the minibuffer is not active. Any other value means do not
2569 consider the minibuffer window even if the minibuffer is active.
2571 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2572 plus the minibuffer window if specified by the MINIBUF argument. If the
2573 minibuffer counts, consider all windows on all frames that share that
2574 minibuffer too. The following non-nil values of ALL-FRAMES have special
2575 meanings:
2577 - t means consider all windows on all existing frames.
2579 - `visible' means consider all windows on all visible frames.
2581 - 0 (the number zero) means consider all windows on all visible and
2582 iconified frames.
2584 - A frame means consider all windows on that frame only.
2586 Anything else means consider all windows on WINDOW's frame and no
2587 others.
2589 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2590 `next-window' to iterate through the entire cycle of acceptable
2591 windows, eventually ending up back at the window you started with.
2592 `previous-window' traverses the same cycle, in the reverse order. */)
2593 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2595 return next_window (window, minibuf, all_frames, 1);
2599 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2600 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2601 WINDOW must be a live window and defaults to the selected one. The
2602 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2603 consider.
2605 MINIBUF nil or omitted means consider the minibuffer window only if the
2606 minibuffer is active. MINIBUF t means consider the minibuffer window
2607 even if the minibuffer is not active. Any other value means do not
2608 consider the minibuffer window even if the minibuffer is active.
2610 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2611 plus the minibuffer window if specified by the MINIBUF argument. If the
2612 minibuffer counts, consider all windows on all frames that share that
2613 minibuffer too. The following non-nil values of ALL-FRAMES have special
2614 meanings:
2616 - t means consider all windows on all existing frames.
2618 - `visible' means consider all windows on all visible frames.
2620 - 0 (the number zero) means consider all windows on all visible and
2621 iconified frames.
2623 - A frame means consider all windows on that frame only.
2625 Anything else means consider all windows on WINDOW's frame and no
2626 others.
2628 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2629 use `previous-window' to iterate through the entire cycle of
2630 acceptable windows, eventually ending up back at the window you
2631 started with. `next-window' traverses the same cycle, in the
2632 reverse order. */)
2633 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2635 return next_window (window, minibuf, all_frames, 0);
2639 /* Return a list of windows in cyclic ordering. Arguments are like
2640 for `next-window'. */
2642 static Lisp_Object
2643 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2645 Lisp_Object tail, list, rest;
2647 decode_next_window_args (&window, &minibuf, &all_frames);
2648 list = Qnil;
2650 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2651 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2652 list = Fcons (XCAR (tail), list);
2654 /* Rotate the list to start with WINDOW. */
2655 list = Fnreverse (list);
2656 rest = Fmemq (window, list);
2657 if (!NILP (rest) && !EQ (rest, list))
2659 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2661 XSETCDR (tail, Qnil);
2662 list = nconc2 (rest, list);
2664 return list;
2668 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2669 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2670 FRAME nil or omitted means use the selected frame.
2671 WINDOW nil or omitted means use the window selected within FRAME.
2672 MINIBUF t means include the minibuffer window, even if it isn't active.
2673 MINIBUF nil or omitted means include the minibuffer window only
2674 if it's active.
2675 MINIBUF neither nil nor t means never include the minibuffer window. */)
2676 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2678 if (NILP (window))
2679 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2680 CHECK_WINDOW (window);
2681 if (NILP (frame))
2682 frame = selected_frame;
2684 if (!EQ (frame, XWINDOW (window)->frame))
2685 error ("Window is on a different frame");
2687 return window_list_1 (window, minibuf, frame);
2691 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2692 doc: /* Return a list of all live windows.
2693 WINDOW specifies the first window to list and defaults to the selected
2694 window.
2696 Optional argument MINIBUF nil or omitted means consider the minibuffer
2697 window only if the minibuffer is active. MINIBUF t means consider the
2698 minibuffer window even if the minibuffer is not active. Any other value
2699 means do not consider the minibuffer window even if the minibuffer is
2700 active.
2702 Optional argument ALL-FRAMES nil or omitted means consider all windows
2703 on WINDOW's frame, plus the minibuffer window if specified by the
2704 MINIBUF argument. If the minibuffer counts, consider all windows on all
2705 frames that share that minibuffer too. The following non-nil values of
2706 ALL-FRAMES have special meanings:
2708 - t means consider all windows on all existing frames.
2710 - `visible' means consider all windows on all visible frames.
2712 - 0 (the number zero) means consider all windows on all visible and
2713 iconified frames.
2715 - A frame means consider all windows on that frame only.
2717 Anything else means consider all windows on WINDOW's frame and no
2718 others.
2720 If WINDOW is not on the list of windows returned, some other window will
2721 be listed first but no error is signaled. */)
2722 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2724 return window_list_1 (window, minibuf, all_frames);
2727 /* Look at all windows, performing an operation specified by TYPE
2728 with argument OBJ.
2729 If FRAMES is Qt, look at all frames;
2730 Qnil, look at just the selected frame;
2731 Qvisible, look at visible frames;
2732 a frame, just look at windows on that frame.
2733 If MINI is non-zero, perform the operation on minibuffer windows too. */
2735 enum window_loop
2737 WINDOW_LOOP_UNUSED,
2738 GET_BUFFER_WINDOW, /* Arg is buffer */
2739 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2740 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2741 CHECK_ALL_WINDOWS /* Arg is ignored */
2744 static Lisp_Object
2745 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2747 Lisp_Object window, windows, best_window, frame_arg;
2748 int frame_best_window_flag = 0;
2749 struct frame *f;
2750 struct gcpro gcpro1;
2752 /* If we're only looping through windows on a particular frame,
2753 frame points to that frame. If we're looping through windows
2754 on all frames, frame is 0. */
2755 if (FRAMEP (frames))
2756 f = XFRAME (frames);
2757 else if (NILP (frames))
2758 f = SELECTED_FRAME ();
2759 else
2760 f = NULL;
2762 if (f)
2763 frame_arg = Qlambda;
2764 else if (EQ (frames, make_number (0)))
2765 frame_arg = frames;
2766 else if (EQ (frames, Qvisible))
2767 frame_arg = frames;
2768 else
2769 frame_arg = Qt;
2771 /* frame_arg is Qlambda to stick to one frame,
2772 Qvisible to consider all visible frames,
2773 or Qt otherwise. */
2775 /* Pick a window to start with. */
2776 if (WINDOWP (obj))
2777 window = obj;
2778 else if (f)
2779 window = FRAME_SELECTED_WINDOW (f);
2780 else
2781 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2783 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2784 GCPRO1 (windows);
2785 best_window = Qnil;
2787 for (; CONSP (windows); windows = XCDR (windows))
2789 struct window *w;
2791 window = XCAR (windows);
2792 w = XWINDOW (window);
2794 /* Note that we do not pay attention here to whether the frame
2795 is visible, since Fwindow_list skips non-visible frames if
2796 that is desired, under the control of frame_arg. */
2797 if (!MINI_WINDOW_P (w)
2798 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2799 consider all windows. */
2800 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2801 || (mini && minibuf_level > 0))
2802 switch (type)
2804 case GET_BUFFER_WINDOW:
2805 if (EQ (w->contents, obj)
2806 /* Don't find any minibuffer window except the one that
2807 is currently in use. */
2808 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2810 if (EQ (window, selected_window))
2811 /* Preferably return the selected window. */
2812 RETURN_UNGCPRO (window);
2813 else if (EQ (XWINDOW (window)->frame, selected_frame)
2814 && !frame_best_window_flag)
2815 /* Prefer windows on the current frame (but don't
2816 choose another one if we have one already). */
2818 best_window = window;
2819 frame_best_window_flag = 1;
2821 else if (NILP (best_window))
2822 best_window = window;
2824 break;
2826 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2827 /* We could simply check whether the buffer shown by window
2828 is live, and show another buffer in case it isn't. */
2829 if (EQ (w->contents, obj))
2831 /* Undedicate WINDOW. */
2832 wset_dedicated (w, Qnil);
2833 /* Make WINDOW show the buffer returned by
2834 other_buffer_safely, don't run any hooks. */
2835 set_window_buffer
2836 (window, other_buffer_safely (w->contents), 0, 0);
2837 /* If WINDOW is the selected window, make its buffer
2838 current. But do so only if the window shows the
2839 current buffer (Bug#6454). */
2840 if (EQ (window, selected_window)
2841 && XBUFFER (w->contents) == current_buffer)
2842 Fset_buffer (w->contents);
2844 break;
2846 case REDISPLAY_BUFFER_WINDOWS:
2847 if (EQ (w->contents, obj))
2849 mark_window_display_accurate (window, 0);
2850 w->update_mode_line = 1;
2851 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2852 update_mode_lines = 27;
2853 best_window = window;
2855 break;
2857 /* Check for a leaf window that has a killed buffer
2858 or broken markers. */
2859 case CHECK_ALL_WINDOWS:
2860 if (BUFFERP (w->contents))
2862 struct buffer *b = XBUFFER (w->contents);
2864 if (!BUFFER_LIVE_P (b))
2865 emacs_abort ();
2866 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2867 emacs_abort ();
2868 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2869 emacs_abort ();
2871 break;
2873 case WINDOW_LOOP_UNUSED:
2874 break;
2878 UNGCPRO;
2879 return best_window;
2882 /* Used for debugging. Abort if any window has a dead buffer. */
2884 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2885 void
2886 check_all_windows (void)
2888 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2891 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2892 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2893 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2894 the current buffer.
2896 The optional argument ALL-FRAMES specifies the frames to consider:
2898 - t means consider all windows on all existing frames.
2900 - `visible' means consider all windows on all visible frames.
2902 - 0 (the number zero) means consider all windows on all visible
2903 and iconified frames.
2905 - A frame means consider all windows on that frame only.
2907 Any other value of ALL-FRAMES means consider all windows on the
2908 selected frame and no others. */)
2909 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2911 Lisp_Object buffer;
2913 if (NILP (buffer_or_name))
2914 buffer = Fcurrent_buffer ();
2915 else
2916 buffer = Fget_buffer (buffer_or_name);
2918 if (BUFFERP (buffer))
2919 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2920 else
2921 return Qnil;
2924 static Lisp_Object
2925 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2927 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2931 static Lisp_Object
2932 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2934 return call2(Qwindow_pixel_to_total, frame, horizontal);
2938 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2939 Sdelete_other_windows_internal, 0, 2, "",
2940 doc: /* Make WINDOW fill its frame.
2941 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2942 and defaults to the selected one.
2944 Optional argument ROOT, if non-nil, must specify an internal window such
2945 that WINDOW is in its window subtree. If this is the case, replace ROOT
2946 by WINDOW and leave alone any windows not part of ROOT's subtree.
2948 When WINDOW is live try to reduce display jumps by keeping the text
2949 previously visible in WINDOW in the same place on the frame. Doing this
2950 depends on the value of (window-start WINDOW), so if calling this
2951 function in a program gives strange scrolling, make sure the
2952 window-start value is reasonable when this function is called. */)
2953 (Lisp_Object window, Lisp_Object root)
2955 struct window *w, *r, *s;
2956 struct frame *f;
2957 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2958 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2959 int top IF_LINT (= 0), new_top, resize_failed;
2961 w = decode_valid_window (window);
2962 XSETWINDOW (window, w);
2963 f = XFRAME (w->frame);
2965 if (NILP (root))
2966 /* ROOT is the frame's root window. */
2968 root = FRAME_ROOT_WINDOW (f);
2969 r = XWINDOW (root);
2971 else
2972 /* ROOT must be an ancestor of WINDOW. */
2974 r = decode_valid_window (root);
2975 pwindow = XWINDOW (window)->parent;
2976 while (!NILP (pwindow))
2977 if (EQ (pwindow, root))
2978 break;
2979 else
2980 pwindow = XWINDOW (pwindow)->parent;
2981 if (!EQ (pwindow, root))
2982 error ("Specified root is not an ancestor of specified window");
2985 if (EQ (window, root))
2986 /* A noop. */
2987 return Qnil;
2988 /* I don't understand the "top > 0" part below. If we deal with a
2989 standalone minibuffer it would have been caught by the preceding
2990 test. */
2991 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2992 error ("Can't expand minibuffer to full frame");
2994 if (BUFFERP (w->contents))
2996 startpos = marker_position (w->start);
2997 startbyte = marker_byte_position (w->start);
2998 top = (WINDOW_TOP_EDGE_LINE (w)
2999 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3000 /* Make sure WINDOW is the frame's selected window. */
3001 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3003 if (EQ (selected_frame, w->frame))
3004 Fselect_window (window, Qnil);
3005 else
3006 fset_selected_window (f, window);
3009 else
3011 /* See if the frame's selected window is a part of the window
3012 subtree rooted at WINDOW, by finding all the selected window's
3013 parents and comparing each one with WINDOW. If it isn't we
3014 need a new selected window for this frame. */
3015 swindow = FRAME_SELECTED_WINDOW (f);
3016 while (1)
3018 pwindow = swindow;
3019 while (!NILP (pwindow) && !EQ (window, pwindow))
3020 pwindow = XWINDOW (pwindow)->parent;
3022 if (EQ (window, pwindow))
3023 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3024 as the new selected window. */
3025 break;
3026 else
3027 /* Else try the previous window of SWINDOW. */
3028 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3031 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3033 if (EQ (selected_frame, w->frame))
3034 Fselect_window (swindow, Qnil);
3035 else
3036 fset_selected_window (f, swindow);
3040 block_input ();
3041 if (!FRAME_INITIAL_P (f))
3043 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3045 /* We are going to free the glyph matrices of WINDOW, and with
3046 that we might lose any information about glyph rows that have
3047 some of their glyphs highlighted in mouse face. (These rows
3048 are marked with a non-zero mouse_face_p flag.) If WINDOW
3049 indeed has some glyphs highlighted in mouse face, signal to
3050 frame's up-to-date hook that mouse highlight was overwritten,
3051 so that it will arrange for redisplaying the highlight. */
3052 if (EQ (hlinfo->mouse_face_window, window))
3053 reset_mouse_highlight (hlinfo);
3055 free_window_matrices (r);
3057 fset_redisplay (f);
3058 Vwindow_list = Qnil;
3059 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3060 resize_failed = 0;
3062 if (!WINDOW_LEAF_P (w))
3064 /* Resize child windows vertically. */
3065 XSETINT (delta, r->pixel_height - w->pixel_height);
3066 w->pixel_top = r->pixel_top;
3067 w->top_line = r->top_line;
3068 resize_root_window (window, delta, Qnil, Qnil, Qt);
3069 if (window_resize_check (w, 0))
3071 window_resize_apply (w, 0);
3072 window_pixel_to_total (w->frame, Qnil);
3074 else
3076 resize_root_window (window, delta, Qnil, Qt, Qt);
3077 if (window_resize_check (w, 0))
3079 window_resize_apply (w, 0);
3080 window_pixel_to_total (w->frame, Qnil);
3082 else
3083 resize_failed = 1;
3086 /* Resize child windows horizontally. */
3087 if (!resize_failed)
3089 w->left_col = r->left_col;
3090 w->pixel_left = r->pixel_left;
3091 XSETINT (delta, r->pixel_width - w->pixel_width);
3092 resize_root_window (window, delta, Qt, Qnil, Qt);
3093 if (window_resize_check (w, 1))
3095 window_resize_apply (w, 1);
3096 window_pixel_to_total (w->frame, Qt);
3098 else
3100 resize_root_window (window, delta, Qt, Qt, Qt);
3101 if (window_resize_check (w, 1))
3103 window_resize_apply (w, 1);
3104 window_pixel_to_total (w->frame, Qt);
3106 else
3107 resize_failed = 1;
3111 if (resize_failed)
3112 /* Play safe, if we still can ... */
3114 window = swindow;
3115 w = XWINDOW (window);
3119 /* Cleanly unlink WINDOW from window-tree. */
3120 if (!NILP (w->prev))
3121 /* Get SIBLING above (on the left of) WINDOW. */
3123 sibling = w->prev;
3124 s = XWINDOW (sibling);
3125 wset_next (s, w->next);
3126 if (!NILP (s->next))
3127 wset_prev (XWINDOW (s->next), sibling);
3129 else
3130 /* Get SIBLING below (on the right of) WINDOW. */
3132 sibling = w->next;
3133 s = XWINDOW (sibling);
3134 wset_prev (s, Qnil);
3135 wset_combination (XWINDOW (w->parent),
3136 XWINDOW (w->parent)->horizontal, sibling);
3139 /* Delete ROOT and all child windows of ROOT. */
3140 if (WINDOWP (r->contents))
3142 delete_all_child_windows (r->contents);
3143 wset_combination (r, 0, Qnil);
3146 replace_window (root, window, 1);
3148 /* This must become SWINDOW anyway ....... */
3149 if (BUFFERP (w->contents) && !resize_failed)
3151 /* Try to minimize scrolling, by setting the window start to the
3152 point will cause the text at the old window start to be at the
3153 same place on the frame. But don't try to do this if the
3154 window start is outside the visible portion (as might happen
3155 when the display is not current, due to typeahead). */
3156 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3157 if (new_top != top
3158 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3159 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3161 struct position pos;
3162 struct buffer *obuf = current_buffer;
3164 Fset_buffer (w->contents);
3165 /* This computation used to temporarily move point, but that
3166 can have unwanted side effects due to text properties. */
3167 pos = *vmotion (startpos, startbyte, -top, w);
3169 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3170 w->window_end_valid = 0;
3171 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3172 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3173 /* We need to do this, so that the window-scroll-functions
3174 get called. */
3175 w->optional_new_start = 1;
3177 set_buffer_internal (obuf);
3181 adjust_frame_glyphs (f);
3182 unblock_input ();
3184 run_window_configuration_change_hook (f);
3186 return Qnil;
3190 void
3191 replace_buffer_in_windows (Lisp_Object buffer)
3193 call1 (Qreplace_buffer_in_windows, buffer);
3196 /* If BUFFER is shown in a window, safely replace it with some other
3197 buffer in all windows of all frames, even those on other keyboards. */
3199 void
3200 replace_buffer_in_windows_safely (Lisp_Object buffer)
3202 if (buffer_window_count (XBUFFER (buffer)))
3204 Lisp_Object tail, frame;
3206 /* A single call to window_loop won't do the job because it only
3207 considers frames on the current keyboard. So loop manually over
3208 frames, and handle each one. */
3209 FOR_EACH_FRAME (tail, frame)
3210 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3214 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3215 minimum allowable size. PIXELWISE means interpret these as pixel
3216 sizes. */
3218 void
3219 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3221 /* For height, we have to see:
3222 how many windows the frame has at minimum (one or two),
3223 and whether it has a menu bar or other special stuff at the top. */
3224 if (pixelwise)
3226 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3227 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3229 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3230 min_height = 2 * min_height;
3232 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3233 min_height += FRAME_INTERNAL_BORDER_WIDTH (frame);
3235 if (*height < min_height)
3236 *height = min_height;
3237 if (*width < min_width)
3238 *width = min_width;
3240 else
3242 int min_height
3243 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3244 ? MIN_SAFE_WINDOW_HEIGHT
3245 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3247 if (FRAME_TOP_MARGIN (frame) > 0)
3248 min_height += FRAME_TOP_MARGIN (frame);
3250 if (*height < min_height)
3251 *height = min_height;
3252 if (*width < MIN_SAFE_WINDOW_WIDTH)
3253 *width = MIN_SAFE_WINDOW_WIDTH;
3257 /* Adjust the margins of window W if text area is too small.
3258 Return 1 if window width is ok after adjustment; 0 if window
3259 is still too narrow. */
3261 static int
3262 adjust_window_margins (struct window *w)
3264 int box_width = (WINDOW_PIXEL_WIDTH (w)
3265 - WINDOW_FRINGES_WIDTH (w)
3266 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3267 int margin_width = WINDOW_MARGINS_WIDTH (w);
3269 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3270 return 1;
3272 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3273 return 0;
3274 else
3275 /* Window's text area is too narrow, but reducing the window
3276 margins will fix that. */
3278 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3280 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3282 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3284 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3285 w->left_margin_cols = w->right_margin_cols =
3286 margin_width / (2 * unit);
3287 else
3288 w->right_margin_cols = margin_width / unit;
3290 else
3291 w->left_margin_cols = margin_width / unit;
3293 return 1;
3297 /* The following three routines are needed for running a window's
3298 configuration change hook. */
3299 static void
3300 run_funs (Lisp_Object funs)
3302 for (; CONSP (funs); funs = XCDR (funs))
3303 if (!EQ (XCAR (funs), Qt))
3304 call0 (XCAR (funs));
3307 static void
3308 select_window_norecord (Lisp_Object window)
3310 if (WINDOW_LIVE_P (window))
3311 Fselect_window (window, Qt);
3314 static void
3315 select_frame_norecord (Lisp_Object frame)
3317 if (FRAME_LIVE_P (XFRAME (frame)))
3318 Fselect_frame (frame, Qt);
3321 void
3322 run_window_configuration_change_hook (struct frame *f)
3324 ptrdiff_t count = SPECPDL_INDEX ();
3325 Lisp_Object frame, global_wcch
3326 = Fdefault_value (Qwindow_configuration_change_hook);
3327 XSETFRAME (frame, f);
3329 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3330 return;
3332 /* Use the right buffer. Matters when running the local hooks. */
3333 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3335 record_unwind_current_buffer ();
3336 Fset_buffer (Fwindow_buffer (Qnil));
3339 if (SELECTED_FRAME () != f)
3341 record_unwind_protect (select_frame_norecord, selected_frame);
3342 select_frame_norecord (frame);
3345 /* Look for buffer-local values. */
3347 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3348 for (; CONSP (windows); windows = XCDR (windows))
3350 Lisp_Object window = XCAR (windows);
3351 Lisp_Object buffer = Fwindow_buffer (window);
3352 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3353 buffer)))
3355 ptrdiff_t inner_count = SPECPDL_INDEX ();
3356 record_unwind_protect (select_window_norecord, selected_window);
3357 select_window_norecord (window);
3358 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3359 buffer));
3360 unbind_to (inner_count, Qnil);
3365 run_funs (global_wcch);
3366 unbind_to (count, Qnil);
3369 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3370 Srun_window_configuration_change_hook, 0, 1, 0,
3371 doc: /* Run `window-configuration-change-hook' for FRAME.
3372 If FRAME is omitted or nil, it defaults to the selected frame. */)
3373 (Lisp_Object frame)
3375 run_window_configuration_change_hook (decode_live_frame (frame));
3376 return Qnil;
3379 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3380 Srun_window_scroll_functions, 0, 1, 0,
3381 doc: /* Run `window-scroll-functions' for WINDOW.
3382 If WINDOW is omitted or nil, it defaults to the selected window. */)
3383 (Lisp_Object window)
3385 if (! NILP (Vwindow_scroll_functions))
3386 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3387 Fmarker_position (decode_live_window (window)->start));
3388 return Qnil;
3391 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3392 to run hooks. See make_frame for a case where it's not allowed.
3393 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3394 scroll-bar settings of the window are not reset from the buffer's
3395 local settings. */
3397 void
3398 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3399 bool run_hooks_p, bool keep_margins_p)
3401 struct window *w = XWINDOW (window);
3402 struct buffer *b = XBUFFER (buffer);
3403 ptrdiff_t count = SPECPDL_INDEX ();
3404 bool samebuf = EQ (buffer, w->contents);
3406 wset_buffer (w, buffer);
3408 if (EQ (window, selected_window))
3409 bset_last_selected_window (b, window);
3411 /* Let redisplay errors through. */
3412 b->display_error_modiff = 0;
3414 /* Update time stamps of buffer display. */
3415 if (INTEGERP (BVAR (b, display_count)))
3416 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3417 bset_display_time (b, Fcurrent_time ());
3419 w->window_end_pos = 0;
3420 w->window_end_vpos = 0;
3421 w->last_cursor_vpos = 0;
3423 if (!(keep_margins_p && samebuf))
3424 { /* If we're not actually changing the buffer, don't reset hscroll and
3425 vscroll. This case happens for example when called from
3426 change_frame_size_1, where we use a dummy call to
3427 Fset_window_buffer on the frame's selected window (and no other)
3428 just in order to run window-configuration-change-hook.
3429 Resetting hscroll and vscroll here is problematic for things like
3430 image-mode and doc-view-mode since it resets the image's position
3431 whenever we resize the frame. */
3432 w->hscroll = w->min_hscroll = 0;
3433 w->vscroll = 0;
3434 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3435 set_marker_restricted (w->start,
3436 make_number (b->last_window_start),
3437 buffer);
3438 w->start_at_line_beg = 0;
3439 w->force_start = 0;
3441 /* Maybe we could move this into the `if' but it's not obviously safe and
3442 I doubt it's worth the trouble. */
3443 wset_redisplay (w);
3444 w->update_mode_line = true;
3446 /* We must select BUFFER to run the window-scroll-functions and to look up
3447 the buffer-local value of Vwindow_point_insertion_type. */
3448 record_unwind_current_buffer ();
3449 Fset_buffer (buffer);
3451 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3453 if (!keep_margins_p)
3455 /* Set left and right marginal area width etc. from buffer. */
3456 set_window_fringes (w, BVAR (b, left_fringe_width),
3457 BVAR (b, right_fringe_width),
3458 BVAR (b, fringes_outside_margins));
3459 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3460 BVAR (b, vertical_scroll_bar_type), Qnil);
3461 set_window_margins (w, BVAR (b, left_margin_cols),
3462 BVAR (b, right_margin_cols));
3463 apply_window_adjustment (w);
3466 if (run_hooks_p)
3468 if (! NILP (Vwindow_scroll_functions))
3469 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3470 Fmarker_position (w->start));
3471 if (!samebuf)
3472 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3475 unbind_to (count, Qnil);
3478 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3479 doc: /* Make WINDOW display BUFFER-OR-NAME.
3480 WINDOW must be a live window and defaults to the selected one.
3481 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3483 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3484 display margins, fringe widths, and scroll bar settings are preserved;
3485 the default is to reset these from the local settings for BUFFER-OR-NAME
3486 or the frame defaults. Return nil.
3488 This function throws an error when WINDOW is strongly dedicated to its
3489 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3490 already display BUFFER-OR-NAME.
3492 This function runs `window-scroll-functions' before running
3493 `window-configuration-change-hook'. */)
3494 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3496 register Lisp_Object tem, buffer;
3497 register struct window *w = decode_live_window (window);
3499 XSETWINDOW (window, w);
3500 buffer = Fget_buffer (buffer_or_name);
3501 CHECK_BUFFER (buffer);
3502 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3503 error ("Attempt to display deleted buffer");
3505 tem = w->contents;
3506 if (NILP (tem))
3507 error ("Window is deleted");
3508 else
3510 if (!EQ (tem, buffer))
3512 if (EQ (w->dedicated, Qt))
3513 /* WINDOW is strongly dedicated to its buffer, signal an
3514 error. */
3515 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3516 else
3517 /* WINDOW is weakly dedicated to its buffer, reset
3518 dedication. */
3519 wset_dedicated (w, Qnil);
3521 call1 (Qrecord_window_buffer, window);
3524 unshow_buffer (w);
3527 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3529 return Qnil;
3532 static Lisp_Object
3533 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3535 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3538 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3539 0, 1, 0,
3540 doc: /* Force all windows to be updated on next redisplay.
3541 If optional arg OBJECT is a window, force redisplay of that window only.
3542 If OBJECT is a buffer or buffer name, force redisplay of all windows
3543 displaying that buffer. */)
3544 (Lisp_Object object)
3546 if (NILP (object))
3548 windows_or_buffers_changed = 29;
3549 update_mode_lines = 28;
3550 return Qt;
3553 if (WINDOWP (object))
3555 struct window *w = XWINDOW (object);
3556 mark_window_display_accurate (object, 0);
3557 w->update_mode_line = 1;
3558 if (BUFFERP (w->contents))
3559 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3560 update_mode_lines = 29;
3561 return Qt;
3564 if (STRINGP (object))
3565 object = Fget_buffer (object);
3566 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3567 && buffer_window_count (XBUFFER (object)))
3569 /* If buffer is live and shown in at least one window, find
3570 all windows showing this buffer and force update of them. */
3571 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3572 return NILP (object) ? Qnil : Qt;
3575 /* If nothing suitable was found, just return.
3576 We could signal an error, but this feature will typically be used
3577 asynchronously in timers or process sentinels, so we don't. */
3578 return Qnil;
3581 /* Obsolete since 24.3. */
3582 void
3583 temp_output_buffer_show (register Lisp_Object buf)
3585 register struct buffer *old = current_buffer;
3586 register Lisp_Object window;
3587 register struct window *w;
3589 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3591 Fset_buffer (buf);
3592 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3593 BEGV = BEG;
3594 ZV = Z;
3595 SET_PT (BEG);
3596 set_buffer_internal (old);
3598 if (!NILP (Vtemp_buffer_show_function))
3599 call1 (Vtemp_buffer_show_function, buf);
3600 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3602 if (!EQ (XWINDOW (window)->frame, selected_frame))
3603 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3604 Vminibuf_scroll_window = window;
3605 w = XWINDOW (window);
3606 w->hscroll = 0;
3607 w->min_hscroll = 0;
3608 set_marker_restricted_both (w->start, buf, BEG, BEG);
3609 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3611 /* Run temp-buffer-show-hook, with the chosen window selected
3612 and its buffer current. */
3614 ptrdiff_t count = SPECPDL_INDEX ();
3615 Lisp_Object prev_window, prev_buffer;
3616 prev_window = selected_window;
3617 XSETBUFFER (prev_buffer, old);
3619 /* Select the window that was chosen, for running the hook.
3620 Note: Both Fselect_window and select_window_norecord may
3621 set-buffer to the buffer displayed in the window,
3622 so we need to save the current buffer. --stef */
3623 record_unwind_protect (restore_buffer, prev_buffer);
3624 record_unwind_protect (select_window_norecord, prev_window);
3625 Fselect_window (window, Qt);
3626 Fset_buffer (w->contents);
3627 Frun_hooks (1, &Qtemp_buffer_show_hook);
3628 unbind_to (count, Qnil);
3633 /* Make new window, have it replace WINDOW in window-tree, and make
3634 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3635 horizontal child). */
3636 static void
3637 make_parent_window (Lisp_Object window, bool horflag)
3639 Lisp_Object parent;
3640 register struct window *o, *p;
3642 o = XWINDOW (window);
3643 p = allocate_window ();
3644 memcpy ((char *) p + sizeof (struct vectorlike_header),
3645 (char *) o + sizeof (struct vectorlike_header),
3646 word_size * VECSIZE (struct window));
3647 /* P's buffer slot may change from nil to a buffer... */
3648 adjust_window_count (p, 1);
3649 XSETWINDOW (parent, p);
3651 p->sequence_number = ++sequence_number;
3653 replace_window (window, parent, 1);
3655 wset_next (o, Qnil);
3656 wset_prev (o, Qnil);
3657 wset_parent (o, parent);
3658 /* ...but now P becomes an internal window. */
3659 wset_start (p, Qnil);
3660 wset_pointm (p, Qnil);
3661 wset_buffer (p, Qnil);
3662 wset_combination (p, horflag, window);
3663 wset_combination_limit (p, Qnil);
3664 wset_window_parameters (p, Qnil);
3667 /* Make new window from scratch. */
3668 Lisp_Object
3669 make_window (void)
3671 Lisp_Object window;
3672 register struct window *w;
3674 w = allocate_window ();
3675 /* Initialize Lisp data. Note that allocate_window initializes all
3676 Lisp data to nil, so do it only for slots which should not be nil. */
3677 wset_normal_lines (w, make_float (1.0));
3678 wset_normal_cols (w, make_float (1.0));
3679 wset_new_total (w, make_number (0));
3680 wset_new_normal (w, make_number (0));
3681 wset_new_pixel (w, make_number (0));
3682 wset_start (w, Fmake_marker ());
3683 wset_pointm (w, Fmake_marker ());
3684 wset_vertical_scroll_bar_type (w, Qt);
3685 /* These Lisp fields are marked specially so they're not set to nil by
3686 allocate_window. */
3687 wset_prev_buffers (w, Qnil);
3688 wset_next_buffers (w, Qnil);
3690 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3691 non-Lisp data, so do it only for slots which should not be zero. */
3692 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3693 w->left_fringe_width = w->right_fringe_width = -1;
3694 w->mode_line_height = w->header_line_height = -1;
3695 #ifdef HAVE_WINDOW_SYSTEM
3696 w->phys_cursor_type = NO_CURSOR;
3697 w->phys_cursor_width = -1;
3698 #endif
3699 w->sequence_number = ++sequence_number;
3700 w->scroll_bar_width = -1;
3701 w->column_number_displayed = -1;
3703 /* Reset window_list. */
3704 Vwindow_list = Qnil;
3705 /* Return window. */
3706 XSETWINDOW (window, w);
3707 return window;
3710 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3711 doc: /* Set new pixel size of WINDOW to SIZE.
3712 WINDOW must be a valid window and defaults to the selected one.
3713 Return SIZE.
3715 Optional argument ADD non-nil means add SIZE to the new pixel size of
3716 WINDOW and return the sum.
3718 Note: This function does not operate on any child windows of WINDOW. */)
3719 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3721 struct window *w = decode_valid_window (window);
3722 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3723 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3725 CHECK_RANGED_INTEGER (size, size_min, size_max);
3726 if (NILP (add))
3727 wset_new_pixel (w, size);
3728 else
3729 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3731 return w->new_pixel;
3734 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3735 doc: /* Set new total size of WINDOW to SIZE.
3736 WINDOW must be a valid window and defaults to the selected one.
3737 Return SIZE.
3739 Optional argument ADD non-nil means add SIZE to the new total size of
3740 WINDOW and return the sum.
3742 Note: This function does not operate on any child windows of WINDOW. */)
3743 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3745 struct window *w = decode_valid_window (window);
3747 CHECK_NUMBER (size);
3748 if (NILP (add))
3749 wset_new_total (w, size);
3750 else
3751 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3753 return w->new_total;
3756 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3757 doc: /* Set new normal size of WINDOW to SIZE.
3758 WINDOW must be a valid window and defaults to the selected one.
3759 Return SIZE.
3761 Note: This function does not operate on any child windows of WINDOW. */)
3762 (Lisp_Object window, Lisp_Object size)
3764 wset_new_normal (decode_valid_window (window), size);
3765 return size;
3768 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3769 non-zero) to w->new_pixel would result in correct heights (widths)
3770 for window W and recursively all child windows of W.
3772 Note: This function does not check any of `window-fixed-size-p',
3773 `window-min-height' or `window-min-width'. It does check that window
3774 sizes do not drop below one line (two columns). */
3775 static int
3776 window_resize_check (struct window *w, bool horflag)
3778 struct frame *f = XFRAME (w->frame);
3779 struct window *c;
3781 if (WINDOW_VERTICAL_COMBINATION_P (w))
3782 /* W is a vertical combination. */
3784 c = XWINDOW (w->contents);
3785 if (horflag)
3786 /* All child windows of W must have the same width as W. */
3788 while (c)
3790 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3791 || !window_resize_check (c, horflag))
3792 return 0;
3794 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3797 return 1;
3799 else
3800 /* The sum of the heights of the child windows of W must equal
3801 W's height. */
3803 int remaining_pixels = XINT (w->new_pixel);
3805 while (c)
3807 if (!window_resize_check (c, horflag))
3808 return 0;
3810 remaining_pixels -= XINT (c->new_pixel);
3811 if (remaining_pixels < 0)
3812 return 0;
3813 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3816 return remaining_pixels == 0;
3819 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3820 /* W is a horizontal combination. */
3822 c = XWINDOW (w->contents);
3823 if (horflag)
3824 /* The sum of the widths of the child windows of W must equal W's
3825 width. */
3827 int remaining_pixels = XINT (w->new_pixel);
3829 while (c)
3831 if (!window_resize_check (c, horflag))
3832 return 0;
3834 remaining_pixels -= XINT (c->new_pixel);
3835 if (remaining_pixels < 0)
3836 return 0;
3837 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3840 return remaining_pixels == 0;
3842 else
3843 /* All child windows of W must have the same height as W. */
3845 while (c)
3847 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3848 || !window_resize_check (c, horflag))
3849 return 0;
3851 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3854 return 1;
3857 else
3858 /* A leaf window. Make sure it's not too small. The following
3859 hardcodes the values of `window-safe-min-width' (2) and
3860 `window-safe-min-height' (1) which are defined in window.el. */
3861 return (XINT (w->new_pixel) >= (horflag
3862 ? (2 * FRAME_COLUMN_WIDTH (f))
3863 : FRAME_LINE_HEIGHT (f)));
3867 /* Set w->pixel_height (w->pixel_height if HORIZONTAL is non-zero) to
3868 w->new_pixel for window W and recursively all child windows of W.
3869 Also calculate and assign the new vertical (horizontal) pixel start
3870 positions of each of these windows.
3872 This function does not perform any error checks. Make sure you have
3873 run window_resize_check on W before applying this function. */
3874 static void
3875 window_resize_apply (struct window *w, bool horflag)
3877 struct window *c;
3878 int edge;
3879 int unit = (horflag
3880 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3881 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3883 /* Note: Assigning new_normal requires that the new total size of the
3884 parent window has been set *before*. */
3885 if (horflag)
3887 w->pixel_width = XFASTINT (w->new_pixel);
3888 w->total_cols = w->pixel_width / unit;
3889 if (NUMBERP (w->new_normal))
3890 wset_normal_cols (w, w->new_normal);
3892 edge = w->pixel_left;
3894 else
3896 w->pixel_height = XFASTINT (w->new_pixel);
3897 w->total_lines = w->pixel_height / unit;
3898 if (NUMBERP (w->new_normal))
3899 wset_normal_lines (w, w->new_normal);
3901 edge = w->pixel_top;
3904 if (WINDOW_VERTICAL_COMBINATION_P (w))
3905 /* W is a vertical combination. */
3907 c = XWINDOW (w->contents);
3908 while (c)
3910 if (horflag)
3912 c->pixel_left = edge;
3913 c->left_col = edge / unit;
3915 else
3917 c->pixel_top = edge;
3918 c->top_line = edge / unit;
3920 window_resize_apply (c, horflag);
3921 if (!horflag)
3922 edge = edge + c->pixel_height;
3924 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3927 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3928 /* W is a horizontal combination. */
3930 c = XWINDOW (w->contents);
3931 while (c)
3933 if (horflag)
3935 c->pixel_left = edge;
3936 c->left_col = edge / unit;
3938 else
3940 c->pixel_top = edge;
3941 c->top_line = edge / unit;
3944 window_resize_apply (c, horflag);
3945 if (horflag)
3946 edge = edge + c->pixel_width;
3948 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3951 else
3953 adjust_window_margins (w);
3954 /* Bug#15957. */
3955 w->window_end_valid = 0;
3960 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3961 w->new_total for window W and recursively all child windows of W.
3962 Also calculate and assign the new vertical (horizontal) start
3963 positions of each of these windows. */
3964 static void
3965 window_resize_apply_total (struct window *w, bool horflag)
3967 struct window *c;
3968 int edge;
3970 /* Note: Assigning new_normal requires that the new total size of the
3971 parent window has been set *before*. */
3972 if (horflag)
3974 w->total_cols = XFASTINT (w->new_total);
3975 edge = w->left_col;
3977 else
3979 w->total_lines = XFASTINT (w->new_total);
3980 edge = w->top_line;
3983 if (WINDOW_VERTICAL_COMBINATION_P (w))
3984 /* W is a vertical combination. */
3986 c = XWINDOW (w->contents);
3987 while (c)
3989 if (horflag)
3990 c->left_col = edge;
3991 else
3992 c->top_line = edge;
3994 window_resize_apply_total (c, horflag);
3995 if (!horflag)
3996 edge = edge + c->total_lines;
3998 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4001 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4002 /* W is a horizontal combination. */
4004 c = XWINDOW (w->contents);
4005 while (c)
4007 if (horflag)
4008 c->left_col = edge;
4009 else
4010 c->top_line = edge;
4012 window_resize_apply_total (c, horflag);
4013 if (horflag)
4014 edge = edge + c->total_cols;
4016 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4021 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4022 doc: /* Apply requested size values for window-tree of FRAME.
4023 If FRAME is omitted or nil, it defaults to the selected frame.
4025 Optional argument HORIZONTAL omitted or nil means apply requested
4026 height values. HORIZONTAL non-nil means apply requested width values.
4028 This function checks whether the requested values sum up to a valid
4029 window layout, recursively assigns the new sizes of all child windows
4030 and calculates and assigns the new start positions of these windows.
4032 Note: This function does not check any of `window-fixed-size-p',
4033 `window-min-height' or `window-min-width'. All these checks have to
4034 be applied on the Elisp level. */)
4035 (Lisp_Object frame, Lisp_Object horizontal)
4037 struct frame *f = decode_live_frame (frame);
4038 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4039 bool horflag = !NILP (horizontal);
4041 if (!window_resize_check (r, horflag)
4042 || (XINT (r->new_pixel)
4043 != (horflag ? r->pixel_width : r->pixel_height)))
4044 return Qnil;
4046 block_input ();
4047 window_resize_apply (r, horflag);
4049 fset_redisplay (f);
4050 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4052 adjust_frame_glyphs (f);
4053 unblock_input ();
4055 return Qt;
4059 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4060 doc: /* Apply requested total size values for window-tree of FRAME.
4061 If FRAME is omitted or nil, it defaults to the selected frame.
4063 This function does not assign pixel or normal size values. You should
4064 have run `window-resize-apply' before running this.
4066 Optional argument HORIZONTAL omitted or nil means apply requested
4067 height values. HORIZONTAL non-nil means apply requested width
4068 values. */)
4069 (Lisp_Object frame, Lisp_Object horizontal)
4071 struct frame *f = decode_live_frame (frame);
4072 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4074 block_input ();
4075 /* Necessary when deleting the top-/or leftmost window. */
4076 r->left_col = 0;
4077 r->top_line = FRAME_TOP_MARGIN (f);
4078 window_resize_apply_total (r, !NILP (horizontal));
4079 /* Handle the mini window. */
4080 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4082 struct window *m = XWINDOW (f->minibuffer_window);
4084 if (NILP (horizontal))
4086 m->top_line = r->top_line + r->total_lines;
4087 m->total_lines = XFASTINT (m->new_total);
4089 else
4090 m->total_cols = XFASTINT (m->new_total);
4093 unblock_input ();
4095 return Qt;
4099 /* Resize frame F's windows when number of lines of F is set to SIZE.
4100 HORFLAG 1 means resize windows when number of columns of F is set to
4101 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4103 This function can delete all windows but the selected one in order to
4104 satisfy the request. The result will be meaningful if and only if
4105 F's windows have meaningful sizes when you call this. */
4106 void
4107 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4109 Lisp_Object root = f->root_window;
4110 struct window *r = XWINDOW (root);
4111 Lisp_Object mini = f->minibuffer_window;
4112 struct window *m;
4113 /* old_size is the old size of the frame's root window. */
4114 int old_size = horflag ? r->total_cols : r->total_lines;
4115 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4116 /* new_size is the new size of the frame's root window. */
4117 int new_size, new_pixel_size;
4118 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4120 /* Don't let the size drop below one unit. This is more comforting
4121 when we are called from x_set_tool_bar_lines since the latter may
4122 have implicitly given us a zero or negative height. */
4123 if (pixelwise)
4125 /* Note: This does not include the size for internal borders
4126 since these are not part of the frame's text area. */
4127 new_pixel_size = max (horflag
4128 ? size
4129 : (size
4130 - FRAME_TOP_MARGIN_HEIGHT (f)
4131 - ((FRAME_HAS_MINIBUF_P (f)
4132 && !FRAME_MINIBUF_ONLY_P (f))
4133 ? FRAME_LINE_HEIGHT (f) : 0)),
4134 unit);
4135 new_size = new_pixel_size / unit;
4137 else
4139 new_size = max (horflag
4140 ? size
4141 : (size
4142 - FRAME_TOP_MARGIN (f)
4143 - ((FRAME_HAS_MINIBUF_P (f)
4144 && !FRAME_MINIBUF_ONLY_P (f))
4145 ? 1 : 0)),
4147 new_pixel_size = new_size * unit;
4150 r->top_line = FRAME_TOP_MARGIN (f);
4151 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4153 if (new_pixel_size == old_pixel_size)
4155 else if (WINDOW_LEAF_P (r))
4156 /* For a leaf root window just set the size. */
4157 if (horflag)
4159 r->total_cols = new_size;
4160 r->pixel_width = new_pixel_size;
4162 else
4164 r->total_lines = new_size;
4165 r->pixel_height = new_pixel_size;
4167 else
4169 Lisp_Object delta;
4171 if (pixelwise)
4172 XSETINT (delta, new_pixel_size - old_pixel_size);
4173 else
4174 XSETINT (delta, new_size - old_size);
4176 /* Try a "normal" resize first. */
4177 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4178 pixelwise ? Qt : Qnil);
4179 if (window_resize_check (r, horflag)
4180 && new_pixel_size == XINT (r->new_pixel))
4182 window_resize_apply (r, horflag);
4183 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4185 else
4187 /* Try with "reasonable" minimum sizes next. */
4188 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4189 pixelwise ? Qt : Qnil);
4190 if (window_resize_check (r, horflag)
4191 && new_pixel_size == XINT (r->new_pixel))
4193 window_resize_apply (r, horflag);
4194 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4196 else
4198 /* Finally, try with "safe" minimum sizes. */
4199 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4200 pixelwise ? Qt : Qnil);
4201 if (window_resize_check (r, horflag)
4202 && new_pixel_size == XINT (r->new_pixel))
4204 window_resize_apply (r, horflag);
4205 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4207 #if 0 /* Let's try without killing other windows. */
4208 else
4210 /* We lost. Delete all windows but the frame's
4211 selected one. */
4212 root = f->selected_window;
4213 Fdelete_other_windows_internal (root, Qnil);
4214 if (horflag)
4216 XWINDOW (root)->total_cols = new_size;
4217 XWINDOW (root)->pixel_width = new_pixel_size;
4219 else
4221 XWINDOW (root)->total_lines = new_size;
4222 XWINDOW (root)->pixel_height = new_pixel_size;
4225 #endif /* 0 */
4230 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4232 m = XWINDOW (mini);
4233 if (horflag)
4235 m->total_cols = size;
4236 m->pixel_width = new_pixel_size;
4238 else
4240 /* Are we sure we always want 1 line here? */
4241 m->total_lines = 1;
4242 m->pixel_height = FRAME_LINE_HEIGHT (f);
4243 m->top_line = r->top_line + r->total_lines;
4244 m->pixel_top = r->pixel_top + r->pixel_height;
4248 fset_redisplay (f);
4252 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4253 doc: /* Split window OLD.
4254 Second argument PIXEL-SIZE specifies the number of pixels of the
4255 new window. In any case TOTAL-SIZE must be a positive integer.
4257 Third argument SIDE nil (or `below') specifies that the new window shall
4258 be located below WINDOW. SIDE `above' means the new window shall be
4259 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4260 height of the new window including space reserved for the mode and/or
4261 header line.
4263 SIDE t (or `right') specifies that the new window shall be located on
4264 the right side of WINDOW. SIDE `left' means the new window shall be
4265 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4266 width of the new window including space reserved for fringes and the
4267 scrollbar or a divider column.
4269 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4270 according to the SIDE argument.
4272 The new pixel and normal sizes of all involved windows must have been
4273 set correctly. See the code of `split-window' for how this is done. */)
4274 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4276 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4277 parent window or an internal window we have to install as OLD's new
4278 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4279 provided OLD is a leaf window, or to the frame's selected window.
4280 NEW (*n) is the new window created with some parameters taken from
4281 REFERENCE (*r). */
4282 register Lisp_Object new, frame, reference;
4283 register struct window *o, *p, *n, *r, *c;
4284 struct frame *f;
4285 bool horflag
4286 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4287 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4288 int combination_limit = 0, sum = 0;
4289 int total_size;
4291 CHECK_WINDOW (old);
4292 o = XWINDOW (old);
4293 frame = WINDOW_FRAME (o);
4294 f = XFRAME (frame);
4296 CHECK_NUMBER (pixel_size);
4297 total_size
4298 = XINT (pixel_size) / (horflag
4299 ? FRAME_COLUMN_WIDTH (f)
4300 : FRAME_LINE_HEIGHT (f));
4302 /* Set combination_limit to 1 if we have to make a new parent window.
4303 We do that if either `window-combination-limit' is t, or OLD has no
4304 parent, or OLD is ortho-combined. */
4305 combination_limit =
4306 EQ (Vwindow_combination_limit, Qt)
4307 || NILP (o->parent)
4308 || (horflag
4309 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4310 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4312 /* We need a live reference window to initialize some parameters. */
4313 if (WINDOW_LIVE_P (old))
4314 /* OLD is live, use it as reference window. */
4315 reference = old;
4316 else
4317 /* Use the frame's selected window as reference window. */
4318 reference = FRAME_SELECTED_WINDOW (f);
4319 r = XWINDOW (reference);
4321 /* The following bugs are caught by `split-window'. */
4322 if (MINI_WINDOW_P (o))
4323 error ("Attempt to split minibuffer window");
4324 else if (total_size < (horflag ? 2 : 1))
4325 error ("Size of new window too small (after split)");
4326 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4327 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4328 proportionally. */
4330 p = XWINDOW (o->parent);
4331 /* Temporarily pretend we split the parent window. */
4332 wset_new_pixel
4333 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4334 - XINT (pixel_size)));
4335 if (!window_resize_check (p, horflag))
4336 error ("Window sizes don't fit");
4337 else
4338 /* Undo the temporary pretension. */
4339 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4341 else
4343 if (!window_resize_check (o, horflag))
4344 error ("Resizing old window failed");
4345 else if (XINT (pixel_size) + XINT (o->new_pixel)
4346 != (horflag ? o->pixel_width : o->pixel_height))
4347 error ("Sum of sizes of old and new window don't fit");
4350 /* This is our point of no return. */
4351 if (combination_limit)
4353 /* Save the old value of o->normal_cols/lines. It gets corrupted
4354 by make_parent_window and we need it below for assigning it to
4355 p->new_normal. */
4356 Lisp_Object new_normal
4357 = horflag ? o->normal_cols : o->normal_lines;
4359 make_parent_window (old, horflag);
4360 p = XWINDOW (o->parent);
4361 if (EQ (Vwindow_combination_limit, Qt))
4362 /* Store t in the new parent's combination_limit slot to avoid
4363 that its children get merged into another window. */
4364 wset_combination_limit (p, Qt);
4365 /* These get applied below. */
4366 wset_new_pixel
4367 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4368 wset_new_total
4369 (p, make_number (horflag ? o->total_cols : o->total_lines));
4370 wset_new_normal (p, new_normal);
4372 else
4373 p = XWINDOW (o->parent);
4375 fset_redisplay (f);
4376 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4377 new = make_window ();
4378 n = XWINDOW (new);
4379 wset_frame (n, frame);
4380 wset_parent (n, o->parent);
4382 if (EQ (side, Qabove) || EQ (side, Qleft))
4384 wset_prev (n, o->prev);
4385 if (NILP (n->prev))
4386 wset_combination (p, horflag, new);
4387 else
4388 wset_next (XWINDOW (n->prev), new);
4389 wset_next (n, old);
4390 wset_prev (o, new);
4392 else
4394 wset_next (n, o->next);
4395 if (!NILP (n->next))
4396 wset_prev (XWINDOW (n->next), new);
4397 wset_prev (n, old);
4398 wset_next (o, new);
4401 n->window_end_valid = 0;
4402 n->last_cursor_vpos = 0;
4404 /* Get special geometry settings from reference window. */
4405 n->left_margin_cols = r->left_margin_cols;
4406 n->right_margin_cols = r->right_margin_cols;
4407 n->left_fringe_width = r->left_fringe_width;
4408 n->right_fringe_width = r->right_fringe_width;
4409 n->fringes_outside_margins = r->fringes_outside_margins;
4410 n->scroll_bar_width = r->scroll_bar_width;
4411 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4413 /* Directly assign orthogonal coordinates and sizes. */
4414 if (horflag)
4416 n->pixel_top = o->pixel_top;
4417 n->top_line = o->top_line;
4418 n->pixel_height = o->pixel_height;
4419 n->total_lines = o->total_lines;
4421 else
4423 n->pixel_left = o->pixel_left;
4424 n->left_col = o->left_col;
4425 n->pixel_width = o->pixel_width;
4426 n->total_cols = o->total_cols;
4429 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4430 get them ready here. */
4431 wset_new_pixel (n, pixel_size);
4432 c = XWINDOW (p->contents);
4433 while (c)
4435 if (c != n)
4436 sum = sum + XINT (c->new_total);
4437 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4439 wset_new_total (n, make_number ((horflag
4440 ? p->total_cols
4441 : p->total_lines)
4442 - sum));
4443 wset_new_normal (n, normal_size);
4445 block_input ();
4446 window_resize_apply (p, horflag);
4447 adjust_frame_glyphs (f);
4448 /* Set buffer of NEW to buffer of reference window. Don't run
4449 any hooks. */
4450 set_window_buffer (new, r->contents, 0, 1);
4451 unblock_input ();
4453 /* Maybe we should run the scroll functions in Elisp (which already
4454 runs the configuration change hook). */
4455 if (! NILP (Vwindow_scroll_functions))
4456 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4457 Fmarker_position (n->start));
4458 /* Return NEW. */
4459 return new;
4463 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4464 doc: /* Remove WINDOW from its frame.
4465 WINDOW defaults to the selected window. Return nil.
4466 Signal an error when WINDOW is the only window on its frame. */)
4467 (register Lisp_Object window)
4469 register Lisp_Object parent, sibling, frame, root;
4470 struct window *w, *p, *s, *r;
4471 struct frame *f;
4472 bool horflag, before_sibling = 0;
4474 w = decode_any_window (window);
4475 XSETWINDOW (window, w);
4476 if (NILP (w->contents))
4477 /* It's a no-op to delete an already deleted window. */
4478 return Qnil;
4480 parent = w->parent;
4481 if (NILP (parent))
4482 /* Never delete a minibuffer or frame root window. */
4483 error ("Attempt to delete minibuffer or sole ordinary window");
4484 else if (NILP (w->prev) && NILP (w->next))
4485 /* Rather bow out here, this case should be handled on the Elisp
4486 level. */
4487 error ("Attempt to delete sole window of parent");
4489 p = XWINDOW (parent);
4490 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4492 frame = WINDOW_FRAME (w);
4493 f = XFRAME (frame);
4495 root = FRAME_ROOT_WINDOW (f);
4496 r = XWINDOW (root);
4498 /* Unlink WINDOW from window tree. */
4499 if (NILP (w->prev))
4500 /* Get SIBLING below (on the right of) WINDOW. */
4502 /* before_sibling 1 means WINDOW is the first child of its
4503 parent and thus before the sibling. */
4504 before_sibling = 1;
4505 sibling = w->next;
4506 s = XWINDOW (sibling);
4507 wset_prev (s, Qnil);
4508 wset_combination (p, horflag, sibling);
4510 else
4511 /* Get SIBLING above (on the left of) WINDOW. */
4513 sibling = w->prev;
4514 s = XWINDOW (sibling);
4515 wset_next (s, w->next);
4516 if (!NILP (s->next))
4517 wset_prev (XWINDOW (s->next), sibling);
4520 if (window_resize_check (r, horflag)
4521 && (XINT (r->new_pixel)
4522 == (horflag ? r->pixel_width : r->pixel_height)))
4523 /* We can delete WINDOW now. */
4526 /* Block input. */
4527 block_input ();
4528 #ifdef HAVE_XWIDGETS
4529 xwidget_view_delete_all_in_window(w);
4530 #endif
4531 window_resize_apply (p, horflag);
4532 /* If this window is referred to by the dpyinfo's mouse
4533 highlight, invalidate that slot to be safe (Bug#9904). */
4534 if (!FRAME_INITIAL_P (f))
4536 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4538 if (EQ (hlinfo->mouse_face_window, window))
4539 hlinfo->mouse_face_window = Qnil;
4542 fset_redisplay (f);
4543 Vwindow_list = Qnil;
4544 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4546 wset_next (w, Qnil); /* Don't delete w->next too. */
4547 free_window_matrices (w);
4549 if (WINDOWP (w->contents))
4551 delete_all_child_windows (w->contents);
4552 wset_combination (w, 0, Qnil);
4554 else
4556 unshow_buffer (w);
4557 unchain_marker (XMARKER (w->pointm));
4558 unchain_marker (XMARKER (w->start));
4559 wset_buffer (w, Qnil);
4562 if (NILP (s->prev) && NILP (s->next))
4563 /* A matrjoshka where SIBLING has become the only child of
4564 PARENT. */
4566 /* Put SIBLING into PARENT's place. */
4567 replace_window (parent, sibling, 0);
4568 /* Have SIBLING inherit the following three slot values from
4569 PARENT (the combination_limit slot is not inherited). */
4570 wset_normal_cols (s, p->normal_cols);
4571 wset_normal_lines (s, p->normal_lines);
4572 /* Mark PARENT as deleted. */
4573 wset_combination (p, 0, Qnil);
4574 /* Try to merge SIBLING into its new parent. */
4575 recombine_windows (sibling);
4578 adjust_frame_glyphs (f);
4580 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4581 /* We deleted the frame's selected window. */
4583 /* Use the frame's first window as fallback ... */
4584 Lisp_Object new_selected_window = Fframe_first_window (frame);
4585 /* ... but preferably use its most recently used window. */
4586 Lisp_Object mru_window;
4588 /* `get-mru-window' might fail for some reason so play it safe
4589 - promote the first window _without recording it_ first. */
4590 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4591 Fselect_window (new_selected_window, Qt);
4592 else
4593 fset_selected_window (f, new_selected_window);
4595 unblock_input ();
4597 /* Now look whether `get-mru-window' gets us something. */
4598 mru_window = call1 (Qget_mru_window, frame);
4599 if (WINDOW_LIVE_P (mru_window)
4600 && EQ (XWINDOW (mru_window)->frame, frame))
4601 new_selected_window = mru_window;
4603 /* If all ended up well, we now promote the mru window. */
4604 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4605 Fselect_window (new_selected_window, Qnil);
4606 else
4607 fset_selected_window (f, new_selected_window);
4609 else
4610 unblock_input ();
4612 /* Must be run by the caller:
4613 run_window_configuration_change_hook (f); */
4615 else
4616 /* We failed: Relink WINDOW into window tree. */
4618 if (before_sibling)
4620 wset_prev (s, window);
4621 wset_combination (p, horflag, window);
4623 else
4625 wset_next (s, window);
4626 if (!NILP (w->next))
4627 wset_prev (XWINDOW (w->next), window);
4629 error ("Deletion failed");
4632 return Qnil;
4635 /***********************************************************************
4636 Resizing Mini-Windows
4637 ***********************************************************************/
4639 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4640 can. */
4641 void
4642 grow_mini_window (struct window *w, int delta, bool pixelwise)
4644 struct frame *f = XFRAME (w->frame);
4645 struct window *r;
4646 Lisp_Object root, height;
4647 int line_height, pixel_height;
4649 eassert (MINI_WINDOW_P (w));
4650 eassert (delta >= 0);
4652 if (delta > 0)
4654 root = FRAME_ROOT_WINDOW (f);
4655 r = XWINDOW (root);
4656 height = call3 (Qwindow_resize_root_window_vertically,
4657 root, make_number (- delta), pixelwise ? Qt : Qnil);
4658 if (INTEGERP (height) && window_resize_check (r, 0))
4660 block_input ();
4661 window_resize_apply (r, 0);
4663 if (pixelwise)
4665 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4666 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4668 else
4670 line_height = min (-XINT (height),
4671 ((INT_MAX - w->pixel_height)
4672 / FRAME_LINE_HEIGHT (f)));
4673 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4676 /* Grow the mini-window. */
4677 w->pixel_top = r->pixel_top + r->pixel_height;
4678 w->top_line = r->top_line + r->total_lines;
4679 /* Make sure the mini-window has always at least one line. */
4680 w->pixel_height = max (w->pixel_height + pixel_height,
4681 FRAME_LINE_HEIGHT (f));
4682 w->total_lines = max (w->total_lines + line_height, 1);
4684 /* Enforce full redisplay of the frame. */
4685 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4686 fset_redisplay (f);
4687 adjust_frame_glyphs (f);
4688 unblock_input ();
4693 /* Shrink mini-window W to one line. */
4694 void
4695 shrink_mini_window (struct window *w, bool pixelwise)
4697 struct frame *f = XFRAME (w->frame);
4698 struct window *r;
4699 Lisp_Object root, delta;
4700 EMACS_INT height, unit;
4702 eassert (MINI_WINDOW_P (w));
4704 height = pixelwise ? w->pixel_height : w->total_lines;
4705 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4706 if (height > unit)
4708 root = FRAME_ROOT_WINDOW (f);
4709 r = XWINDOW (root);
4710 delta = call3 (Qwindow_resize_root_window_vertically,
4711 root, make_number (height - unit),
4712 pixelwise ? Qt : Qnil);
4713 if (INTEGERP (delta) && window_resize_check (r, 0))
4715 block_input ();
4716 window_resize_apply (r, 0);
4718 /* Shrink the mini-window. */
4719 w->top_line = r->top_line + r->total_lines;
4720 w->total_lines = 1;
4721 w->pixel_top = r->pixel_top + r->pixel_height;
4722 w->pixel_height = FRAME_LINE_HEIGHT (f);
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 ();
4729 /* If the above failed for whatever strange reason we must make a
4730 one window frame here. The same routine will be needed when
4731 shrinking the frame (and probably when making the initial
4732 *scratch* window). For the moment leave things as they are. */
4736 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4737 doc: /* Resize minibuffer window WINDOW. */)
4738 (Lisp_Object window)
4740 struct window *w = XWINDOW (window);
4741 struct window *r;
4742 struct frame *f;
4743 int height;
4745 CHECK_WINDOW (window);
4746 f = XFRAME (w->frame);
4748 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4749 error ("Not a valid minibuffer window");
4750 else if (FRAME_MINIBUF_ONLY_P (f))
4751 error ("Cannot resize a minibuffer-only frame");
4753 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4754 height = r->pixel_height + w->pixel_height;
4755 if (window_resize_check (r, 0)
4756 && XINT (w->new_pixel) > 0
4757 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4759 block_input ();
4760 window_resize_apply (r, 0);
4762 w->total_lines = XFASTINT (w->new_total);
4763 w->top_line = r->top_line + r->total_lines;
4764 w->pixel_height = XFASTINT (w->new_pixel);
4765 w->pixel_top = r->pixel_top + r->pixel_height;
4767 fset_redisplay (f);
4768 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4769 adjust_frame_glyphs (f);
4770 unblock_input ();
4771 return Qt;
4773 else
4774 error ("Failed to resize minibuffer window");
4777 /* Mark window cursors off for all windows in the window tree rooted
4778 at W by setting their phys_cursor_on_p flag to zero. Called from
4779 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4780 the frame are cleared. */
4782 void
4783 mark_window_cursors_off (struct window *w)
4785 while (w)
4787 if (WINDOWP (w->contents))
4788 mark_window_cursors_off (XWINDOW (w->contents));
4789 else
4790 w->phys_cursor_on_p = 0;
4792 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4797 /* Return number of lines of text (not counting mode lines) in W. */
4800 window_internal_height (struct window *w)
4802 int ht = w->total_lines;
4804 if (!MINI_WINDOW_P (w))
4806 if (!NILP (w->parent)
4807 || WINDOWP (w->contents)
4808 || !NILP (w->next)
4809 || !NILP (w->prev)
4810 || WINDOW_WANTS_MODELINE_P (w))
4811 --ht;
4813 if (WINDOW_WANTS_HEADER_LINE_P (w))
4814 --ht;
4817 return ht;
4820 /************************************************************************
4821 Window Scrolling
4822 ***********************************************************************/
4824 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4825 N screen-fulls, which is defined as the height of the window minus
4826 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4827 instead. Negative values of N mean scroll down. NOERROR non-zero
4828 means don't signal an error if we try to move over BEGV or ZV,
4829 respectively. */
4831 static void
4832 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4834 immediate_quit = 1;
4835 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4837 wset_redisplay (XWINDOW (window));
4839 /* If we must, use the pixel-based version which is much slower than
4840 the line-based one but can handle varying line heights. */
4841 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4842 window_scroll_pixel_based (window, n, whole, noerror);
4843 else
4844 window_scroll_line_based (window, n, whole, noerror);
4846 /* Bug#15957. */
4847 XWINDOW (window)->window_end_valid = 0;
4848 immediate_quit = 0;
4852 /* Implementation of window_scroll that works based on pixel line
4853 heights. See the comment of window_scroll for parameter
4854 descriptions. */
4856 static void
4857 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4859 struct it it;
4860 struct window *w = XWINDOW (window);
4861 struct text_pos start;
4862 int this_scroll_margin;
4863 /* True if we fiddled the window vscroll field without really scrolling. */
4864 int vscrolled = 0;
4865 int x, y, rtop, rbot, rowh, vpos;
4866 void *itdata = NULL;
4867 int window_total_lines;
4868 int frame_line_height = default_line_pixel_height (w);
4870 SET_TEXT_POS_FROM_MARKER (start, w->start);
4871 /* Scrolling a minibuffer window via scroll bar when the echo area
4872 shows long text sometimes resets the minibuffer contents behind
4873 our backs. */
4874 if (CHARPOS (start) > ZV)
4875 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4877 /* If PT is not visible in WINDOW, move back one half of
4878 the screen. Allow PT to be partially visible, otherwise
4879 something like (scroll-down 1) with PT in the line before
4880 the partially visible one would recenter. */
4882 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4884 itdata = bidi_shelve_cache ();
4885 /* Move backward half the height of the window. Performance note:
4886 vmotion used here is about 10% faster, but would give wrong
4887 results for variable height lines. */
4888 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4889 it.current_y = it.last_visible_y;
4890 move_it_vertically_backward (&it, window_box_height (w) / 2);
4892 /* The function move_iterator_vertically may move over more than
4893 the specified y-distance. If it->w is small, e.g. a
4894 mini-buffer window, we may end up in front of the window's
4895 display area. This is the case when Start displaying at the
4896 start of the line containing PT in this case. */
4897 if (it.current_y <= 0)
4899 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4900 move_it_vertically_backward (&it, 0);
4901 it.current_y = 0;
4904 start = it.current.pos;
4905 bidi_unshelve_cache (itdata, 0);
4907 else if (auto_window_vscroll_p)
4909 if (rtop || rbot) /* Partially visible. */
4911 int px;
4912 int dy = frame_line_height;
4913 if (whole)
4914 dy = max ((window_box_height (w)
4915 - next_screen_context_lines * dy),
4916 dy);
4917 dy *= n;
4919 if (n < 0)
4921 /* Only vscroll backwards if already vscrolled forwards. */
4922 if (w->vscroll < 0 && rtop > 0)
4924 px = max (0, -w->vscroll - min (rtop, -dy));
4925 Fset_window_vscroll (window, make_number (px), Qt);
4926 return;
4929 if (n > 0)
4931 /* Do vscroll if already vscrolled or only display line. */
4932 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4934 px = max (0, -w->vscroll + min (rbot, dy));
4935 Fset_window_vscroll (window, make_number (px), Qt);
4936 return;
4939 /* Maybe modify window start instead of scrolling. */
4940 if (rbot > 0 || w->vscroll < 0)
4942 ptrdiff_t spos;
4944 Fset_window_vscroll (window, make_number (0), Qt);
4945 /* If there are other text lines above the current row,
4946 move window start to current row. Else to next row. */
4947 if (rbot > 0)
4948 spos = XINT (Fline_beginning_position (Qnil));
4949 else
4950 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4951 set_marker_restricted (w->start, make_number (spos),
4952 w->contents);
4953 w->start_at_line_beg = 1;
4954 w->update_mode_line = 1;
4955 /* Set force_start so that redisplay_window will run the
4956 window-scroll-functions. */
4957 w->force_start = 1;
4958 return;
4962 /* Cancel previous vscroll. */
4963 Fset_window_vscroll (window, make_number (0), Qt);
4966 itdata = bidi_shelve_cache ();
4967 /* If scroll_preserve_screen_position is non-nil, we try to set
4968 point in the same window line as it is now, so get that line. */
4969 if (!NILP (Vscroll_preserve_screen_position))
4971 /* We preserve the goal pixel coordinate across consecutive
4972 calls to scroll-up, scroll-down and other commands that
4973 have the `scroll-command' property. This avoids the
4974 possibility of point becoming "stuck" on a tall line when
4975 scrolling by one line. */
4976 if (window_scroll_pixel_based_preserve_y < 0
4977 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4978 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4980 start_display (&it, w, start);
4981 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4982 window_scroll_pixel_based_preserve_y = it.current_y;
4983 window_scroll_pixel_based_preserve_x = it.current_x;
4986 else
4987 window_scroll_pixel_based_preserve_y
4988 = window_scroll_pixel_based_preserve_x = -1;
4990 /* Move iterator it from start the specified distance forward or
4991 backward. The result is the new window start. */
4992 start_display (&it, w, start);
4993 if (whole)
4995 ptrdiff_t start_pos = IT_CHARPOS (it);
4996 int dy = frame_line_height;
4997 dy = max ((window_box_height (w)
4998 - next_screen_context_lines * dy),
4999 dy) * n;
5001 /* Note that move_it_vertically always moves the iterator to the
5002 start of a line. So, if the last line doesn't have a newline,
5003 we would end up at the start of the line ending at ZV. */
5004 if (dy <= 0)
5006 move_it_vertically_backward (&it, -dy);
5007 /* Ensure we actually do move, e.g. in case we are currently
5008 looking at an image that is taller that the window height. */
5009 while (start_pos == IT_CHARPOS (it)
5010 && start_pos > BEGV)
5011 move_it_by_lines (&it, -1);
5013 else if (dy > 0)
5015 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5016 MOVE_TO_POS | MOVE_TO_Y);
5017 /* Ensure we actually do move, e.g. in case we are currently
5018 looking at an image that is taller that the window height. */
5019 while (start_pos == IT_CHARPOS (it)
5020 && start_pos < ZV)
5021 move_it_by_lines (&it, 1);
5024 else
5025 move_it_by_lines (&it, n);
5027 /* We failed if we find ZV is already on the screen (scrolling up,
5028 means there's nothing past the end), or if we can't start any
5029 earlier (scrolling down, means there's nothing past the top). */
5030 if ((n > 0 && IT_CHARPOS (it) == ZV)
5031 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5033 if (IT_CHARPOS (it) == ZV)
5035 if (it.current_y < it.last_visible_y
5036 && (it.current_y + it.max_ascent + it.max_descent
5037 > it.last_visible_y))
5039 /* The last line was only partially visible, make it fully
5040 visible. */
5041 w->vscroll = (it.last_visible_y
5042 - it.current_y + it.max_ascent + it.max_descent);
5043 adjust_frame_glyphs (it.f);
5045 else
5047 bidi_unshelve_cache (itdata, 0);
5048 if (noerror)
5049 return;
5050 else if (n < 0) /* could happen with empty buffers */
5051 xsignal0 (Qbeginning_of_buffer);
5052 else
5053 xsignal0 (Qend_of_buffer);
5056 else
5058 if (w->vscroll != 0)
5059 /* The first line was only partially visible, make it fully
5060 visible. */
5061 w->vscroll = 0;
5062 else
5064 bidi_unshelve_cache (itdata, 0);
5065 if (noerror)
5066 return;
5067 else
5068 xsignal0 (Qbeginning_of_buffer);
5072 /* If control gets here, then we vscrolled. */
5074 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5076 /* Don't try to change the window start below. */
5077 vscrolled = 1;
5080 if (! vscrolled)
5082 ptrdiff_t pos = IT_CHARPOS (it);
5083 ptrdiff_t bytepos;
5085 /* If in the middle of a multi-glyph character move forward to
5086 the next character. */
5087 if (in_display_vector_p (&it))
5089 ++pos;
5090 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5093 /* Set the window start, and set up the window for redisplay. */
5094 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5095 IT_BYTEPOS (it));
5096 bytepos = marker_byte_position (w->start);
5097 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5098 w->update_mode_line = 1;
5099 /* Set force_start so that redisplay_window will run the
5100 window-scroll-functions. */
5101 w->force_start = 1;
5104 /* The rest of this function uses current_y in a nonstandard way,
5105 not including the height of the header line if any. */
5106 it.current_y = it.vpos = 0;
5108 /* Move PT out of scroll margins.
5109 This code wants current_y to be zero at the window start position
5110 even if there is a header line. */
5111 window_total_lines
5112 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5113 this_scroll_margin = max (0, scroll_margin);
5114 this_scroll_margin
5115 = min (this_scroll_margin, window_total_lines / 4);
5116 this_scroll_margin *= frame_line_height;
5118 if (n > 0)
5120 /* We moved the window start towards ZV, so PT may be now
5121 in the scroll margin at the top. */
5122 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5123 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5124 && (NILP (Vscroll_preserve_screen_position)
5125 || EQ (Vscroll_preserve_screen_position, Qt)))
5126 /* We found PT at a legitimate height. Leave it alone. */
5128 else if (window_scroll_pixel_based_preserve_y >= 0)
5130 /* If we have a header line, take account of it.
5131 This is necessary because we set it.current_y to 0, above. */
5132 move_it_to (&it, -1,
5133 window_scroll_pixel_based_preserve_x,
5134 window_scroll_pixel_based_preserve_y
5135 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5136 -1, MOVE_TO_Y | MOVE_TO_X);
5137 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5139 else
5141 while (it.current_y < this_scroll_margin)
5143 int prev = it.current_y;
5144 move_it_by_lines (&it, 1);
5145 if (prev == it.current_y)
5146 break;
5148 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5151 else if (n < 0)
5153 ptrdiff_t charpos, bytepos;
5154 int partial_p;
5156 /* Save our position, for the
5157 window_scroll_pixel_based_preserve_y case. */
5158 charpos = IT_CHARPOS (it);
5159 bytepos = IT_BYTEPOS (it);
5161 /* We moved the window start towards BEGV, so PT may be now
5162 in the scroll margin at the bottom. */
5163 move_it_to (&it, PT, -1,
5164 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5165 - this_scroll_margin - 1),
5167 MOVE_TO_POS | MOVE_TO_Y);
5169 /* Save our position, in case it's correct. */
5170 charpos = IT_CHARPOS (it);
5171 bytepos = IT_BYTEPOS (it);
5173 /* See if point is on a partially visible line at the end. */
5174 if (it.what == IT_EOB)
5175 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5176 else
5178 move_it_by_lines (&it, 1);
5179 partial_p = it.current_y > it.last_visible_y;
5182 if (charpos == PT && !partial_p
5183 && (NILP (Vscroll_preserve_screen_position)
5184 || EQ (Vscroll_preserve_screen_position, Qt)))
5185 /* We found PT before we found the display margin, so PT is ok. */
5187 else if (window_scroll_pixel_based_preserve_y >= 0)
5189 SET_TEXT_POS_FROM_MARKER (start, w->start);
5190 start_display (&it, w, start);
5191 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5192 here because we called start_display again and did not
5193 alter it.current_y this time. */
5194 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5195 window_scroll_pixel_based_preserve_y, -1,
5196 MOVE_TO_Y | MOVE_TO_X);
5197 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5199 else
5201 if (partial_p)
5202 /* The last line was only partially visible, so back up two
5203 lines to make sure we're on a fully visible line. */
5205 move_it_by_lines (&it, -2);
5206 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5208 else
5209 /* No, the position we saved is OK, so use it. */
5210 SET_PT_BOTH (charpos, bytepos);
5213 bidi_unshelve_cache (itdata, 0);
5217 /* Implementation of window_scroll that works based on screen lines.
5218 See the comment of window_scroll for parameter descriptions. */
5220 static void
5221 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5223 register struct window *w = XWINDOW (window);
5224 /* Fvertical_motion enters redisplay, which can trigger
5225 fontification, which in turn can modify buffer text (e.g., if the
5226 fontification functions replace escape sequences with faces, as
5227 in `grep-mode-font-lock-keywords'). So we use a marker to record
5228 the old point position, to prevent crashes in SET_PT_BOTH. */
5229 Lisp_Object opoint_marker = Fpoint_marker ();
5230 register ptrdiff_t pos, pos_byte;
5231 register int ht = window_internal_height (w);
5232 register Lisp_Object tem;
5233 bool lose;
5234 Lisp_Object bolp;
5235 ptrdiff_t startpos = marker_position (w->start);
5236 ptrdiff_t startbyte = marker_byte_position (w->start);
5237 Lisp_Object original_pos = Qnil;
5239 /* If scrolling screen-fulls, compute the number of lines to
5240 scroll from the window's height. */
5241 if (whole)
5242 n *= max (1, ht - next_screen_context_lines);
5244 if (!NILP (Vscroll_preserve_screen_position))
5246 if (window_scroll_preserve_vpos <= 0
5247 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5248 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5250 struct position posit
5251 = *compute_motion (startpos, startbyte, 0, 0, 0,
5252 PT, ht, 0, -1, w->hscroll, 0, w);
5253 window_scroll_preserve_vpos = posit.vpos;
5254 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5257 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5258 make_number (window_scroll_preserve_vpos));
5261 XSETFASTINT (tem, PT);
5262 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5264 if (NILP (tem))
5266 Fvertical_motion (make_number (- (ht / 2)), window);
5267 startpos = PT;
5268 startbyte = PT_BYTE;
5271 SET_PT_BOTH (startpos, startbyte);
5272 lose = n < 0 && PT == BEGV;
5273 Fvertical_motion (make_number (n), window);
5274 pos = PT;
5275 pos_byte = PT_BYTE;
5276 bolp = Fbolp ();
5277 SET_PT_BOTH (marker_position (opoint_marker),
5278 marker_byte_position (opoint_marker));
5280 if (lose)
5282 if (noerror)
5283 return;
5284 else
5285 xsignal0 (Qbeginning_of_buffer);
5288 if (pos < ZV)
5290 /* Don't use a scroll margin that is negative or too large. */
5291 int this_scroll_margin =
5292 max (0, min (scroll_margin, w->total_lines / 4));
5294 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5295 w->start_at_line_beg = !NILP (bolp);
5296 w->update_mode_line = 1;
5297 /* Set force_start so that redisplay_window will run
5298 the window-scroll-functions. */
5299 w->force_start = 1;
5301 if (!NILP (Vscroll_preserve_screen_position)
5302 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5304 SET_PT_BOTH (pos, pos_byte);
5305 Fvertical_motion (original_pos, window);
5307 /* If we scrolled forward, put point enough lines down
5308 that it is outside the scroll margin. */
5309 else if (n > 0)
5311 int top_margin;
5313 if (this_scroll_margin > 0)
5315 SET_PT_BOTH (pos, pos_byte);
5316 Fvertical_motion (make_number (this_scroll_margin), window);
5317 top_margin = PT;
5319 else
5320 top_margin = pos;
5322 if (top_margin <= marker_position (opoint_marker))
5323 SET_PT_BOTH (marker_position (opoint_marker),
5324 marker_byte_position (opoint_marker));
5325 else if (!NILP (Vscroll_preserve_screen_position))
5327 SET_PT_BOTH (pos, pos_byte);
5328 Fvertical_motion (original_pos, window);
5330 else
5331 SET_PT (top_margin);
5333 else if (n < 0)
5335 int bottom_margin;
5337 /* If we scrolled backward, put point near the end of the window
5338 but not within the scroll margin. */
5339 SET_PT_BOTH (pos, pos_byte);
5340 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5341 if (XFASTINT (tem) == ht - this_scroll_margin)
5342 bottom_margin = PT;
5343 else
5344 bottom_margin = PT + 1;
5346 if (bottom_margin > marker_position (opoint_marker))
5347 SET_PT_BOTH (marker_position (opoint_marker),
5348 marker_byte_position (opoint_marker));
5349 else
5351 if (!NILP (Vscroll_preserve_screen_position))
5353 SET_PT_BOTH (pos, pos_byte);
5354 Fvertical_motion (original_pos, window);
5356 else
5357 Fvertical_motion (make_number (-1), window);
5361 else
5363 if (noerror)
5364 return;
5365 else
5366 xsignal0 (Qend_of_buffer);
5371 /* Scroll selected_window up or down. If N is nil, scroll a
5372 screen-full which is defined as the height of the window minus
5373 next_screen_context_lines. If N is the symbol `-', scroll.
5374 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5375 up. This is the guts of Fscroll_up and Fscroll_down. */
5377 static void
5378 scroll_command (Lisp_Object n, int direction)
5380 ptrdiff_t count = SPECPDL_INDEX ();
5382 eassert (eabs (direction) == 1);
5384 /* If selected window's buffer isn't current, make it current for
5385 the moment. But don't screw up if window_scroll gets an error. */
5386 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5388 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5389 Fset_buffer (XWINDOW (selected_window)->contents);
5392 if (NILP (n))
5393 window_scroll (selected_window, direction, 1, 0);
5394 else if (EQ (n, Qminus))
5395 window_scroll (selected_window, -direction, 1, 0);
5396 else
5398 n = Fprefix_numeric_value (n);
5399 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5402 unbind_to (count, Qnil);
5405 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5406 doc: /* Scroll text of selected window upward ARG lines.
5407 If ARG is omitted or nil, scroll upward by a near full screen.
5408 A near full screen is `next-screen-context-lines' less than a full screen.
5409 Negative ARG means scroll downward.
5410 If ARG is the atom `-', scroll downward by nearly full screen.
5411 When calling from a program, supply as argument a number, nil, or `-'. */)
5412 (Lisp_Object arg)
5414 scroll_command (arg, 1);
5415 return Qnil;
5418 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5419 doc: /* Scroll text of selected window down ARG lines.
5420 If ARG is omitted or nil, scroll down by a near full screen.
5421 A near full screen is `next-screen-context-lines' less than a full screen.
5422 Negative ARG means scroll upward.
5423 If ARG is the atom `-', scroll upward by nearly full screen.
5424 When calling from a program, supply as argument a number, nil, or `-'. */)
5425 (Lisp_Object arg)
5427 scroll_command (arg, -1);
5428 return Qnil;
5431 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5432 doc: /* Return the other window for \"other window scroll\" commands.
5433 If `other-window-scroll-buffer' is non-nil, a window
5434 showing that buffer is used.
5435 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5436 specifies the window. This takes precedence over
5437 `other-window-scroll-buffer'. */)
5438 (void)
5440 Lisp_Object window;
5442 if (MINI_WINDOW_P (XWINDOW (selected_window))
5443 && !NILP (Vminibuf_scroll_window))
5444 window = Vminibuf_scroll_window;
5445 /* If buffer is specified and live, scroll that buffer. */
5446 else if (!NILP (Vother_window_scroll_buffer)
5447 && BUFFERP (Vother_window_scroll_buffer)
5448 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5450 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5451 if (NILP (window))
5452 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5454 else
5456 /* Nothing specified; look for a neighboring window on the same
5457 frame. */
5458 window = Fnext_window (selected_window, Qnil, Qnil);
5460 if (EQ (window, selected_window))
5461 /* That didn't get us anywhere; look for a window on another
5462 visible frame. */
5464 window = Fnext_window (window, Qnil, Qt);
5465 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5466 && ! EQ (window, selected_window));
5469 CHECK_LIVE_WINDOW (window);
5471 if (EQ (window, selected_window))
5472 error ("There is no other window");
5474 return window;
5477 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5478 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5479 A near full screen is `next-screen-context-lines' less than a full screen.
5480 The next window is the one below the current one; or the one at the top
5481 if the current one is at the bottom. Negative ARG means scroll downward.
5482 If ARG is the atom `-', scroll downward by nearly full screen.
5483 When calling from a program, supply as argument a number, nil, or `-'.
5485 If `other-window-scroll-buffer' is non-nil, scroll the window
5486 showing that buffer, popping the buffer up if necessary.
5487 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5488 specifies the window to scroll. This takes precedence over
5489 `other-window-scroll-buffer'. */)
5490 (Lisp_Object arg)
5492 Lisp_Object window;
5493 struct window *w;
5494 ptrdiff_t count = SPECPDL_INDEX ();
5496 window = Fother_window_for_scrolling ();
5497 w = XWINDOW (window);
5499 /* Don't screw up if window_scroll gets an error. */
5500 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5502 Fset_buffer (w->contents);
5503 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5505 if (NILP (arg))
5506 window_scroll (window, 1, 1, 1);
5507 else if (EQ (arg, Qminus))
5508 window_scroll (window, -1, 1, 1);
5509 else
5511 if (CONSP (arg))
5512 arg = XCAR (arg);
5513 CHECK_NUMBER (arg);
5514 window_scroll (window, XINT (arg), 0, 1);
5517 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5518 unbind_to (count, Qnil);
5520 return Qnil;
5523 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5524 doc: /* Scroll selected window display ARG columns left.
5525 Default for ARG is window width minus 2.
5526 Value is the total amount of leftward horizontal scrolling in
5527 effect after the change.
5528 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5529 lower bound for automatic scrolling, i.e. automatic scrolling
5530 will not scroll a window to a column less than the value returned
5531 by this function. This happens in an interactive call. */)
5532 (register Lisp_Object arg, Lisp_Object set_minimum)
5534 struct window *w = XWINDOW (selected_window);
5535 EMACS_INT requested_arg = (NILP (arg)
5536 ? window_body_width (w, 0) - 2
5537 : XINT (Fprefix_numeric_value (arg)));
5538 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5540 if (!NILP (set_minimum))
5541 w->min_hscroll = w->hscroll;
5543 return result;
5546 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5547 doc: /* Scroll selected window display ARG columns right.
5548 Default for ARG is window width minus 2.
5549 Value is the total amount of leftward horizontal scrolling in
5550 effect after the change.
5551 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5552 lower bound for automatic scrolling, i.e. automatic scrolling
5553 will not scroll a window to a column less than the value returned
5554 by this function. This happens in an interactive call. */)
5555 (register Lisp_Object arg, Lisp_Object set_minimum)
5557 struct window *w = XWINDOW (selected_window);
5558 EMACS_INT requested_arg = (NILP (arg)
5559 ? window_body_width (w, 0) - 2
5560 : XINT (Fprefix_numeric_value (arg)));
5561 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5563 if (!NILP (set_minimum))
5564 w->min_hscroll = w->hscroll;
5566 return result;
5569 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5570 doc: /* Return the window which was selected when entering the minibuffer.
5571 Returns nil, if selected window is not a minibuffer window. */)
5572 (void)
5574 if (minibuf_level > 0
5575 && MINI_WINDOW_P (XWINDOW (selected_window))
5576 && WINDOW_LIVE_P (minibuf_selected_window))
5577 return minibuf_selected_window;
5579 return Qnil;
5582 /* Value is the number of lines actually displayed in window W,
5583 as opposed to its height. */
5585 static int
5586 displayed_window_lines (struct window *w)
5588 struct it it;
5589 struct text_pos start;
5590 int height = window_box_height (w);
5591 struct buffer *old_buffer;
5592 int bottom_y;
5593 void *itdata = NULL;
5595 if (XBUFFER (w->contents) != current_buffer)
5597 old_buffer = current_buffer;
5598 set_buffer_internal (XBUFFER (w->contents));
5600 else
5601 old_buffer = NULL;
5603 /* In case W->start is out of the accessible range, do something
5604 reasonable. This happens in Info mode when Info-scroll-down
5605 calls (recenter -1) while W->start is 1. */
5606 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5608 itdata = bidi_shelve_cache ();
5609 start_display (&it, w, start);
5610 move_it_vertically (&it, height);
5611 bottom_y = line_bottom_y (&it);
5612 bidi_unshelve_cache (itdata, 0);
5614 /* rms: On a non-window display,
5615 the value of it.vpos at the bottom of the screen
5616 seems to be 1 larger than window_box_height (w).
5617 This kludge fixes a bug whereby (move-to-window-line -1)
5618 when ZV is on the last screen line
5619 moves to the previous screen line instead of the last one. */
5620 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5621 height++;
5623 /* Add in empty lines at the bottom of the window. */
5624 if (bottom_y < height)
5626 int uy = FRAME_LINE_HEIGHT (it.f);
5627 it.vpos += (height - bottom_y + uy - 1) / uy;
5630 if (old_buffer)
5631 set_buffer_internal (old_buffer);
5633 return it.vpos;
5637 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5638 doc: /* Center point in selected window and maybe redisplay frame.
5639 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5640 relative to the selected window. If ARG is negative, it counts up from the
5641 bottom of the window. (ARG should be less than the height of the window.)
5643 If ARG is omitted or nil, then recenter with point on the middle line of
5644 the selected window; if the variable `recenter-redisplay' is non-nil,
5645 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5646 is set to `grow-only', this resets the tool-bar's height to the minimum
5647 height needed); if `recenter-redisplay' has the special value `tty',
5648 then only tty frames are redrawn.
5650 Just C-u as prefix means put point in the center of the window
5651 and redisplay normally--don't erase and redraw the frame. */)
5652 (register Lisp_Object arg)
5654 struct window *w = XWINDOW (selected_window);
5655 struct buffer *buf = XBUFFER (w->contents);
5656 bool center_p = 0;
5657 ptrdiff_t charpos, bytepos;
5658 EMACS_INT iarg IF_LINT (= 0);
5659 int this_scroll_margin;
5661 if (buf != current_buffer)
5662 error ("`recenter'ing a window that does not display current-buffer.");
5664 /* If redisplay is suppressed due to an error, try again. */
5665 buf->display_error_modiff = 0;
5667 if (NILP (arg))
5669 if (!NILP (Vrecenter_redisplay)
5670 && (!EQ (Vrecenter_redisplay, Qtty)
5671 || !NILP (Ftty_type (selected_frame))))
5673 ptrdiff_t i;
5675 /* Invalidate pixel data calculated for all compositions. */
5676 for (i = 0; i < n_compositions; i++)
5677 composition_table[i]->font = NULL;
5678 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5679 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5680 #endif
5681 Fredraw_frame (WINDOW_FRAME (w));
5682 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5685 center_p = 1;
5687 else if (CONSP (arg)) /* Just C-u. */
5688 center_p = 1;
5689 else
5691 arg = Fprefix_numeric_value (arg);
5692 CHECK_NUMBER (arg);
5693 iarg = XINT (arg);
5696 /* Do this after making BUF current
5697 in case scroll_margin is buffer-local. */
5698 this_scroll_margin
5699 = max (0, min (scroll_margin, w->total_lines / 4));
5701 /* Handle centering on a graphical frame specially. Such frames can
5702 have variable-height lines and centering point on the basis of
5703 line counts would lead to strange effects. */
5704 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5706 if (center_p)
5708 struct it it;
5709 struct text_pos pt;
5710 void *itdata = bidi_shelve_cache ();
5712 SET_TEXT_POS (pt, PT, PT_BYTE);
5713 start_display (&it, w, pt);
5714 move_it_vertically_backward (&it, window_box_height (w) / 2);
5715 charpos = IT_CHARPOS (it);
5716 bytepos = IT_BYTEPOS (it);
5717 bidi_unshelve_cache (itdata, 0);
5719 else if (iarg < 0)
5721 struct it it;
5722 struct text_pos pt;
5723 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5724 int extra_line_spacing;
5725 int h = window_box_height (w);
5726 void *itdata = bidi_shelve_cache ();
5728 iarg = - max (-iarg, this_scroll_margin);
5730 SET_TEXT_POS (pt, PT, PT_BYTE);
5731 start_display (&it, w, pt);
5733 /* Be sure we have the exact height of the full line containing PT. */
5734 move_it_by_lines (&it, 0);
5736 /* The amount of pixels we have to move back is the window
5737 height minus what's displayed in the line containing PT,
5738 and the lines below. */
5739 it.current_y = 0;
5740 it.vpos = 0;
5741 move_it_by_lines (&it, nlines);
5743 if (it.vpos == nlines)
5744 h -= it.current_y;
5745 else
5747 /* Last line has no newline. */
5748 h -= line_bottom_y (&it);
5749 it.vpos++;
5752 /* Don't reserve space for extra line spacing of last line. */
5753 extra_line_spacing = it.max_extra_line_spacing;
5755 /* If we can't move down NLINES lines because we hit
5756 the end of the buffer, count in some empty lines. */
5757 if (it.vpos < nlines)
5759 nlines -= it.vpos;
5760 extra_line_spacing = it.extra_line_spacing;
5761 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5763 if (h <= 0)
5765 bidi_unshelve_cache (itdata, 0);
5766 return Qnil;
5769 /* Now find the new top line (starting position) of the window. */
5770 start_display (&it, w, pt);
5771 it.current_y = 0;
5772 move_it_vertically_backward (&it, h);
5774 /* If extra line spacing is present, we may move too far
5775 back. This causes the last line to be only partially
5776 visible (which triggers redisplay to recenter that line
5777 in the middle), so move forward.
5778 But ignore extra line spacing on last line, as it is not
5779 considered to be part of the visible height of the line.
5781 h += extra_line_spacing;
5782 while (-it.current_y > h)
5783 move_it_by_lines (&it, 1);
5785 charpos = IT_CHARPOS (it);
5786 bytepos = IT_BYTEPOS (it);
5788 bidi_unshelve_cache (itdata, 0);
5790 else
5792 struct position pos;
5794 iarg = max (iarg, this_scroll_margin);
5796 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5797 charpos = pos.bufpos;
5798 bytepos = pos.bytepos;
5801 else
5803 struct position pos;
5804 int ht = window_internal_height (w);
5806 if (center_p)
5807 iarg = ht / 2;
5808 else if (iarg < 0)
5809 iarg += ht;
5811 /* Don't let it get into the margin at either top or bottom. */
5812 iarg = clip_to_bounds (this_scroll_margin, iarg,
5813 ht - this_scroll_margin - 1);
5815 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5816 charpos = pos.bufpos;
5817 bytepos = pos.bytepos;
5820 /* Set the new window start. */
5821 set_marker_both (w->start, w->contents, charpos, bytepos);
5822 w->window_end_valid = 0;
5824 w->optional_new_start = 1;
5826 w->start_at_line_beg = (bytepos == BEGV_BYTE
5827 || FETCH_BYTE (bytepos - 1) == '\n');
5829 return Qnil;
5832 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5833 0, 1, 0,
5834 doc: /* Return the width in columns of the text display area of WINDOW.
5835 WINDOW must be a live window and defaults to the selected one.
5837 The returned width does not include dividers, scrollbars, margins,
5838 fringes, nor any partial-width columns at the right of the text
5839 area. */)
5840 (Lisp_Object window)
5842 struct window *w = decode_live_window (window);
5844 return make_number (window_box_width (w, TEXT_AREA)
5845 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5848 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5849 0, 1, 0,
5850 doc: /* Return the height in lines of the text display area of WINDOW.
5851 WINDOW must be a live window and defaults to the selected one.
5853 The returned height does not include dividers, the mode line, any header
5854 line, nor any partial-height lines at the bottom of the text area. */)
5855 (Lisp_Object window)
5857 struct window *w = decode_live_window (window);
5859 return make_number (window_box_height (w)
5860 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5863 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5864 1, 1, "P",
5865 doc: /* Position point relative to window.
5866 ARG nil means position point at center of window.
5867 Else, ARG specifies vertical position within the window;
5868 zero means top of window, negative means relative to bottom of window. */)
5869 (Lisp_Object arg)
5871 struct window *w = XWINDOW (selected_window);
5872 int lines, start;
5873 Lisp_Object window;
5874 #if 0
5875 int this_scroll_margin;
5876 #endif
5878 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5879 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5880 when passed below to set_marker_both. */
5881 error ("move-to-window-line called from unrelated buffer");
5883 window = selected_window;
5884 start = marker_position (w->start);
5885 if (start < BEGV || start > ZV)
5887 int height = window_internal_height (w);
5888 Fvertical_motion (make_number (- (height / 2)), window);
5889 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5890 w->start_at_line_beg = !NILP (Fbolp ());
5891 w->force_start = 1;
5893 else
5894 Fgoto_char (w->start);
5896 lines = displayed_window_lines (w);
5898 #if 0
5899 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5900 #endif
5902 if (NILP (arg))
5903 XSETFASTINT (arg, lines / 2);
5904 else
5906 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5908 if (iarg < 0)
5909 iarg = iarg + lines;
5911 #if 0 /* This code would prevent move-to-window-line from moving point
5912 to a place inside the scroll margins (which would cause the
5913 next redisplay to scroll). I wrote this code, but then concluded
5914 it is probably better not to install it. However, it is here
5915 inside #if 0 so as not to lose it. -- rms. */
5917 /* Don't let it get into the margin at either top or bottom. */
5918 iarg = max (iarg, this_scroll_margin);
5919 iarg = min (iarg, lines - this_scroll_margin - 1);
5920 #endif
5922 arg = make_number (iarg);
5925 /* Skip past a partially visible first line. */
5926 if (w->vscroll)
5927 XSETINT (arg, XINT (arg) + 1);
5929 return Fvertical_motion (arg, window);
5934 /***********************************************************************
5935 Window Configuration
5936 ***********************************************************************/
5938 struct save_window_data
5940 struct vectorlike_header header;
5941 Lisp_Object selected_frame;
5942 Lisp_Object current_window;
5943 Lisp_Object current_buffer;
5944 Lisp_Object minibuf_scroll_window;
5945 Lisp_Object minibuf_selected_window;
5946 Lisp_Object root_window;
5947 Lisp_Object focus_frame;
5948 /* A vector, each of whose elements is a struct saved_window
5949 for one window. */
5950 Lisp_Object saved_windows;
5952 /* All fields above are traced by the GC.
5953 From `frame-cols' down, the fields are ignored by the GC. */
5954 /* We should be able to do without the following two. */
5955 int frame_cols, frame_lines;
5956 /* These two should get eventually replaced by their pixel
5957 counterparts. */
5958 int frame_menu_bar_lines, frame_tool_bar_lines;
5959 int frame_text_width, frame_text_height;
5960 /* These are currently unused. We need them as soon as we convert
5961 to pixels. */
5962 int frame_menu_bar_height, frame_tool_bar_height;
5965 /* This is saved as a Lisp_Vector. */
5966 struct saved_window
5968 struct vectorlike_header header;
5970 Lisp_Object window, buffer, start, pointm;
5971 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5972 Lisp_Object left_col, top_line, total_cols, total_lines;
5973 Lisp_Object normal_cols, normal_lines;
5974 Lisp_Object hscroll, min_hscroll;
5975 Lisp_Object parent, prev;
5976 Lisp_Object start_at_line_beg;
5977 Lisp_Object display_table;
5978 Lisp_Object left_margin_cols, right_margin_cols;
5979 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5980 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5981 Lisp_Object combination_limit, window_parameters;
5984 #define SAVED_WINDOW_N(swv,n) \
5985 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5987 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5988 doc: /* Return t if OBJECT is a window-configuration object. */)
5989 (Lisp_Object object)
5991 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5994 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5995 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5996 (Lisp_Object config)
5998 register struct save_window_data *data;
5999 struct Lisp_Vector *saved_windows;
6001 CHECK_WINDOW_CONFIGURATION (config);
6003 data = (struct save_window_data *) XVECTOR (config);
6004 saved_windows = XVECTOR (data->saved_windows);
6005 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6008 /* From Chong's unwind_create_frame_1. */
6009 static void
6010 unwind_change_frame (Lisp_Object val)
6012 inhibit_lisp_code = val;
6015 DEFUN ("set-window-configuration", Fset_window_configuration,
6016 Sset_window_configuration, 1, 1, 0,
6017 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6018 CONFIGURATION must be a value previously returned
6019 by `current-window-configuration' (which see).
6020 If CONFIGURATION was made from a frame that is now deleted,
6021 only frame-independent values can be restored. In this case,
6022 the return value is nil. Otherwise the value is t. */)
6023 (Lisp_Object configuration)
6025 register struct save_window_data *data;
6026 struct Lisp_Vector *saved_windows;
6027 Lisp_Object new_current_buffer;
6028 Lisp_Object frame;
6029 struct frame *f;
6030 ptrdiff_t old_point = -1;
6032 CHECK_WINDOW_CONFIGURATION (configuration);
6034 data = (struct save_window_data *) XVECTOR (configuration);
6035 saved_windows = XVECTOR (data->saved_windows);
6037 new_current_buffer = data->current_buffer;
6038 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6039 new_current_buffer = Qnil;
6040 else
6042 if (XBUFFER (new_current_buffer) == current_buffer)
6043 /* The code further down "preserves point" by saving here PT in
6044 old_point and then setting it later back into PT. When the
6045 current-selected-window and the final-selected-window both show
6046 the current buffer, this suffers from the problem that the
6047 current PT is the window-point of the current-selected-window,
6048 while the final PT is the point of the final-selected-window, so
6049 this copy from one PT to the other would end up moving the
6050 window-point of the final-selected-window to the window-point of
6051 the current-selected-window. So we have to be careful which
6052 point of the current-buffer we copy into old_point. */
6053 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6054 && WINDOWP (selected_window)
6055 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6056 && !EQ (selected_window, data->current_window))
6057 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6058 else
6059 old_point = PT;
6060 else
6061 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6062 point in new_current_buffer as of the last time this buffer was
6063 used. This can be non-deterministic since it can be changed by
6064 things like jit-lock by mere temporary selection of some random
6065 window that happens to show this buffer.
6066 So if possible we want this arbitrary choice of "which point" to
6067 be the one from the to-be-selected-window so as to prevent this
6068 window's cursor from being copied from another window. */
6069 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6070 /* If current_window = selected_window, its point is in BUF_PT. */
6071 && !EQ (selected_window, data->current_window))
6072 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6073 else
6074 old_point = BUF_PT (XBUFFER (new_current_buffer));
6077 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6078 f = XFRAME (frame);
6080 /* If f is a dead frame, don't bother rebuilding its window tree.
6081 However, there is other stuff we should still try to do below. */
6082 if (FRAME_LIVE_P (f))
6084 Lisp_Object window;
6085 Lisp_Object dead_windows = Qnil;
6086 register Lisp_Object tem, par, pers;
6087 register struct window *w;
6088 register struct saved_window *p;
6089 struct window *root_window;
6090 struct window **leaf_windows;
6091 int n_leaf_windows;
6092 ptrdiff_t k;
6093 int i, n;
6094 ptrdiff_t count = SPECPDL_INDEX ();
6095 /* If the frame has been resized since this window configuration was
6096 made, we change the frame to the size specified in the
6097 configuration, restore the configuration, and then resize it
6098 back. We keep track of the prevailing height in these variables. */
6099 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6100 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6101 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6102 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6103 /* int previous_frame_lines = FRAME_LINES (f); */
6104 /* int previous_frame_cols = FRAME_COLS (f); */
6105 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6106 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6108 /* Don't do this within the main loop below: This may call Lisp
6109 code and is thus potentially unsafe while input is blocked. */
6110 for (k = 0; k < saved_windows->header.size; k++)
6112 p = SAVED_WINDOW_N (saved_windows, k);
6113 window = p->window;
6114 w = XWINDOW (window);
6115 if (BUFFERP (w->contents)
6116 && !EQ (w->contents, p->buffer)
6117 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6118 /* If a window we restore gets another buffer, record the
6119 window's old buffer. */
6120 call1 (Qrecord_window_buffer, window);
6123 /* Don't run lisp in the following segment since the frame is in a
6124 completely inconsistent state. See Bug#16207. */
6125 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6126 inhibit_lisp_code = Qt;
6127 /* The mouse highlighting code could get screwed up
6128 if it runs during this. */
6129 block_input ();
6131 if (data->frame_text_width != previous_frame_text_width
6132 || data->frame_text_height != previous_frame_text_height)
6133 change_frame_size (f, data->frame_text_width,
6134 data->frame_text_height, 0, 0, 0, 1);
6136 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6138 #ifdef HAVE_WINDOW_SYSTEM
6139 if (FRAME_WINDOW_P (f))
6140 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6141 make_number (0));
6142 else /* TTY or MSDOS */
6143 #endif
6144 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6145 make_number (0));
6147 #ifdef HAVE_WINDOW_SYSTEM
6148 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6149 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6150 make_number (0));
6151 #endif
6153 /* "Swap out" point from the selected window's buffer
6154 into the window itself. (Normally the pointm of the selected
6155 window holds garbage.) We do this now, before
6156 restoring the window contents, and prevent it from
6157 being done later on when we select a new window. */
6158 if (! NILP (XWINDOW (selected_window)->contents))
6160 w = XWINDOW (selected_window);
6161 set_marker_both (w->pointm,
6162 w->contents,
6163 BUF_PT (XBUFFER (w->contents)),
6164 BUF_PT_BYTE (XBUFFER (w->contents)));
6167 fset_redisplay (f);
6168 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6170 /* Problem: Freeing all matrices and later allocating them again
6171 is a serious redisplay flickering problem. What we would
6172 really like to do is to free only those matrices not reused
6173 below. */
6174 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6175 leaf_windows = alloca (count_windows (root_window)
6176 * sizeof *leaf_windows);
6177 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6179 /* Kludge Alert!
6180 Mark all windows now on frame as "deleted".
6181 Restoring the new configuration "undeletes" any that are in it.
6183 Save their current buffers in their height fields, since we may
6184 need it later, if a buffer saved in the configuration is now
6185 dead. */
6186 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6188 for (k = 0; k < saved_windows->header.size; k++)
6190 p = SAVED_WINDOW_N (saved_windows, k);
6191 window = p->window;
6192 w = XWINDOW (window);
6193 wset_next (w, Qnil);
6195 if (!NILP (p->parent))
6196 wset_parent
6197 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6198 else
6199 wset_parent (w, Qnil);
6201 if (!NILP (p->prev))
6203 wset_prev
6204 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6205 wset_next (XWINDOW (w->prev), p->window);
6207 else
6209 wset_prev (w, Qnil);
6210 if (!NILP (w->parent))
6211 wset_combination (XWINDOW (w->parent),
6212 (XINT (p->total_cols)
6213 != XWINDOW (w->parent)->total_cols),
6214 p->window);
6217 /* If we squirreled away the buffer, restore it now. */
6218 if (BUFFERP (w->combination_limit))
6219 wset_buffer (w, w->combination_limit);
6220 w->pixel_left = XFASTINT (p->pixel_left);
6221 w->pixel_top = XFASTINT (p->pixel_top);
6222 w->pixel_width = XFASTINT (p->pixel_width);
6223 w->pixel_height = XFASTINT (p->pixel_height);
6224 w->left_col = XFASTINT (p->left_col);
6225 w->top_line = XFASTINT (p->top_line);
6226 w->total_cols = XFASTINT (p->total_cols);
6227 w->total_lines = XFASTINT (p->total_lines);
6228 wset_normal_cols (w, p->normal_cols);
6229 wset_normal_lines (w, p->normal_lines);
6230 w->hscroll = XFASTINT (p->hscroll);
6231 w->min_hscroll = XFASTINT (p->min_hscroll);
6232 wset_display_table (w, p->display_table);
6233 w->left_margin_cols = XINT (p->left_margin_cols);
6234 w->right_margin_cols = XINT (p->right_margin_cols);
6235 w->left_fringe_width = XINT (p->left_fringe_width);
6236 w->right_fringe_width = XINT (p->right_fringe_width);
6237 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6238 w->scroll_bar_width = XINT (p->scroll_bar_width);
6239 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6240 wset_dedicated (w, p->dedicated);
6241 wset_combination_limit (w, p->combination_limit);
6242 /* Restore any window parameters that have been saved.
6243 Parameters that have not been saved are left alone. */
6244 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6246 pers = XCAR (tem);
6247 if (CONSP (pers))
6249 if (NILP (XCDR (pers)))
6251 par = Fassq (XCAR (pers), w->window_parameters);
6252 if (CONSP (par) && !NILP (XCDR (par)))
6253 /* Reset a parameter to nil if and only if it
6254 has a non-nil association. Don't make new
6255 associations. */
6256 Fsetcdr (par, Qnil);
6258 else
6259 /* Always restore a non-nil value. */
6260 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6264 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6265 /* If saved buffer is alive, install it. */
6267 wset_buffer (w, p->buffer);
6268 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6269 set_marker_restricted (w->start, p->start, w->contents);
6270 set_marker_restricted (w->pointm, p->pointm,
6271 w->contents);
6273 /* As documented in Fcurrent_window_configuration, don't
6274 restore the location of point in the buffer which was
6275 current when the window configuration was recorded. */
6276 if (!EQ (p->buffer, new_current_buffer)
6277 && XBUFFER (p->buffer) == current_buffer)
6278 Fgoto_char (w->pointm);
6280 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6281 /* Keep window's old buffer; make sure the markers are real. */
6283 /* Set window markers at start of visible range. */
6284 if (XMARKER (w->start)->buffer == 0)
6285 set_marker_restricted_both (w->start, w->contents, 0, 0);
6286 if (XMARKER (w->pointm)->buffer == 0)
6287 set_marker_restricted_both
6288 (w->pointm, w->contents,
6289 BUF_PT (XBUFFER (w->contents)),
6290 BUF_PT_BYTE (XBUFFER (w->contents)));
6291 w->start_at_line_beg = 1;
6293 else if (!NILP (w->start))
6294 /* Leaf window has no live buffer, get one. */
6296 /* Get the buffer via other_buffer_safely in order to
6297 avoid showing an unimportant buffer and, if necessary, to
6298 recreate *scratch* in the course (part of Juanma's bs-show
6299 scenario from March 2011). */
6300 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6301 /* This will set the markers to beginning of visible
6302 range. */
6303 set_marker_restricted_both (w->start, w->contents, 0, 0);
6304 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6305 w->start_at_line_beg = 1;
6306 if (!NILP (w->dedicated))
6307 /* Record this window as dead. */
6308 dead_windows = Fcons (window, dead_windows);
6309 /* Make sure window is no more dedicated. */
6310 wset_dedicated (w, Qnil);
6314 fset_root_window (f, data->root_window);
6315 /* Arrange *not* to restore point in the buffer that was
6316 current when the window configuration was saved. */
6317 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6318 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6319 make_number (old_point),
6320 XWINDOW (data->current_window)->contents);
6322 /* In the following call to `select-window', prevent "swapping out
6323 point" in the old selected window using the buffer that has
6324 been restored into it. We already swapped out that point from
6325 that window's old buffer.
6327 Do not record the buffer here. We do that in a separate call
6328 to select_window below. See also Bug#16207. */
6329 select_window (data->current_window, Qt, 1);
6330 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6331 last_selected_window)
6332 = selected_window;
6334 if (NILP (data->focus_frame)
6335 || (FRAMEP (data->focus_frame)
6336 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6337 Fredirect_frame_focus (frame, data->focus_frame);
6339 /* Set the frame size to the value it had before this function. */
6340 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6341 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6342 change_frame_size (f, previous_frame_text_width,
6343 previous_frame_text_height, 0, 0, 0, 1);
6345 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6347 #ifdef HAVE_WINDOW_SYSTEM
6348 if (FRAME_WINDOW_P (f))
6349 x_set_menu_bar_lines (f,
6350 make_number (previous_frame_menu_bar_lines),
6351 make_number (0));
6352 else /* TTY or MSDOS */
6353 #endif
6354 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6355 make_number (0));
6357 #ifdef HAVE_WINDOW_SYSTEM
6358 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6359 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6360 make_number (0));
6361 #endif
6363 /* Now, free glyph matrices in windows that were not reused. */
6364 for (i = n = 0; i < n_leaf_windows; ++i)
6366 if (NILP (leaf_windows[i]->contents))
6367 free_window_matrices (leaf_windows[i]);
6368 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6369 ++n;
6372 adjust_frame_glyphs (f);
6373 unblock_input ();
6374 unbind_to (count, Qnil);
6376 /* Scan dead buffer windows. */
6377 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6379 window = XCAR (dead_windows);
6380 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6381 delete_deletable_window (window);
6384 /* Record the selected window's buffer here. The window should
6385 already be the selected one from the call above. */
6386 select_window (data->current_window, Qnil, 0);
6388 /* Fselect_window will have made f the selected frame, so we
6389 reselect the proper frame here. Fhandle_switch_frame will change the
6390 selected window too, but that doesn't make the call to
6391 Fselect_window above totally superfluous; it still sets f's
6392 selected window. */
6393 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6394 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6396 run_window_configuration_change_hook (f);
6399 if (!NILP (new_current_buffer))
6401 Fset_buffer (new_current_buffer);
6402 /* If the new current buffer doesn't appear in the selected
6403 window, go to its old point (see bug#12208). */
6404 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6405 Fgoto_char (make_number (old_point));
6408 Vminibuf_scroll_window = data->minibuf_scroll_window;
6409 minibuf_selected_window = data->minibuf_selected_window;
6411 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6414 void
6415 restore_window_configuration (Lisp_Object configuration)
6417 Fset_window_configuration (configuration);
6421 /* If WINDOW is an internal window, recursively delete all child windows
6422 reachable via the next and contents slots of WINDOW. Otherwise setup
6423 WINDOW to not show any buffer. */
6425 void
6426 delete_all_child_windows (Lisp_Object window)
6428 register struct window *w;
6430 w = XWINDOW (window);
6432 if (!NILP (w->next))
6433 /* Delete WINDOW's siblings (we traverse postorderly). */
6434 delete_all_child_windows (w->next);
6436 if (WINDOWP (w->contents))
6438 delete_all_child_windows (w->contents);
6439 wset_combination (w, 0, Qnil);
6441 else if (BUFFERP (w->contents))
6443 unshow_buffer (w);
6444 unchain_marker (XMARKER (w->pointm));
6445 unchain_marker (XMARKER (w->start));
6446 /* Since combination limit makes sense for an internal windows
6447 only, we use this slot to save the buffer for the sake of
6448 possible resurrection in Fset_window_configuration. */
6449 wset_combination_limit (w, w->contents);
6450 wset_buffer (w, Qnil);
6453 Vwindow_list = Qnil;
6456 static int
6457 count_windows (register struct window *window)
6459 register int count = 1;
6460 if (!NILP (window->next))
6461 count += count_windows (XWINDOW (window->next));
6462 if (WINDOWP (window->contents))
6463 count += count_windows (XWINDOW (window->contents));
6464 return count;
6468 /* Fill vector FLAT with leaf windows under W, starting at index I.
6469 Value is last index + 1. */
6470 static int
6471 get_leaf_windows (struct window *w, struct window **flat, int i)
6473 while (w)
6475 if (WINDOWP (w->contents))
6476 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6477 else
6478 flat[i++] = w;
6480 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6483 return i;
6487 /* Return a pointer to the glyph W's physical cursor is on. Value is
6488 null if W's current matrix is invalid, so that no meaningful glyph
6489 can be returned. */
6490 struct glyph *
6491 get_phys_cursor_glyph (struct window *w)
6493 struct glyph_row *row;
6494 struct glyph *glyph;
6495 int hpos = w->phys_cursor.hpos;
6497 if (!(w->phys_cursor.vpos >= 0
6498 && w->phys_cursor.vpos < w->current_matrix->nrows))
6499 return NULL;
6501 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6502 if (!row->enabled_p)
6503 return NULL;
6505 if (w->hscroll)
6507 /* When the window is hscrolled, cursor hpos can legitimately be
6508 out of bounds, but we draw the cursor at the corresponding
6509 window margin in that case. */
6510 if (!row->reversed_p && hpos < 0)
6511 hpos = 0;
6512 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6513 hpos = row->used[TEXT_AREA] - 1;
6516 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6517 glyph = row->glyphs[TEXT_AREA] + hpos;
6518 else
6519 glyph = NULL;
6521 return glyph;
6525 static int
6526 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6528 register struct saved_window *p;
6529 register struct window *w;
6530 register Lisp_Object tem, pers, par;
6532 for (; !NILP (window); window = w->next)
6534 p = SAVED_WINDOW_N (vector, i);
6535 w = XWINDOW (window);
6537 wset_temslot (w, make_number (i)); i++;
6538 p->window = window;
6539 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6540 p->pixel_left = make_number (w->pixel_left);
6541 p->pixel_top = make_number (w->pixel_top);
6542 p->pixel_width = make_number (w->pixel_width);
6543 p->pixel_height = make_number (w->pixel_height);
6544 p->left_col = make_number (w->left_col);
6545 p->top_line = make_number (w->top_line);
6546 p->total_cols = make_number (w->total_cols);
6547 p->total_lines = make_number (w->total_lines);
6548 p->normal_cols = w->normal_cols;
6549 p->normal_lines = w->normal_lines;
6550 XSETFASTINT (p->hscroll, w->hscroll);
6551 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6552 p->display_table = w->display_table;
6553 p->left_margin_cols = make_number (w->left_margin_cols);
6554 p->right_margin_cols = make_number (w->right_margin_cols);
6555 p->left_fringe_width = make_number (w->left_fringe_width);
6556 p->right_fringe_width = make_number (w->right_fringe_width);
6557 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6558 p->scroll_bar_width = make_number (w->scroll_bar_width);
6559 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6560 p->dedicated = w->dedicated;
6561 p->combination_limit = w->combination_limit;
6562 p->window_parameters = Qnil;
6564 if (!NILP (Vwindow_persistent_parameters))
6566 /* Run cycle detection on Vwindow_persistent_parameters. */
6567 Lisp_Object tortoise, hare;
6569 hare = tortoise = Vwindow_persistent_parameters;
6570 while (CONSP (hare))
6572 hare = XCDR (hare);
6573 if (!CONSP (hare))
6574 break;
6576 hare = XCDR (hare);
6577 tortoise = XCDR (tortoise);
6579 if (EQ (hare, tortoise))
6580 /* Reset Vwindow_persistent_parameters to Qnil. */
6582 Vwindow_persistent_parameters = Qnil;
6583 break;
6587 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6588 tem = XCDR (tem))
6590 pers = XCAR (tem);
6591 /* Save values for persistent window parameters. */
6592 if (CONSP (pers) && !NILP (XCDR (pers)))
6594 par = Fassq (XCAR (pers), w->window_parameters);
6595 if (NILP (par))
6596 /* If the window has no value for the parameter,
6597 make one. */
6598 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6599 p->window_parameters);
6600 else
6601 /* If the window has a value for the parameter,
6602 save it. */
6603 p->window_parameters = Fcons (Fcons (XCAR (par),
6604 XCDR (par)),
6605 p->window_parameters);
6610 if (BUFFERP (w->contents))
6612 /* Save w's value of point in the window configuration. If w
6613 is the selected window, then get the value of point from
6614 the buffer; pointm is garbage in the selected window. */
6615 if (EQ (window, selected_window))
6616 p->pointm = build_marker (XBUFFER (w->contents),
6617 BUF_PT (XBUFFER (w->contents)),
6618 BUF_PT_BYTE (XBUFFER (w->contents)));
6619 else
6620 p->pointm = Fcopy_marker (w->pointm, Qnil);
6621 XMARKER (p->pointm)->insertion_type
6622 = !NILP (buffer_local_value /* Don't signal error if void. */
6623 (Qwindow_point_insertion_type, w->contents));
6625 p->start = Fcopy_marker (w->start, Qnil);
6626 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6628 else
6630 p->pointm = Qnil;
6631 p->start = Qnil;
6632 p->start_at_line_beg = Qnil;
6635 p->parent = NILP (w->parent) ? Qnil : XWINDOW (w->parent)->temslot;
6636 p->prev = NILP (w->prev) ? Qnil : XWINDOW (w->prev)->temslot;
6638 if (WINDOWP (w->contents))
6639 i = save_window_save (w->contents, vector, i);
6642 return i;
6645 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6646 Scurrent_window_configuration, 0, 1, 0,
6647 doc: /* Return an object representing the current window configuration of FRAME.
6648 If FRAME is nil or omitted, use the selected frame.
6649 This describes the number of windows, their sizes and current buffers,
6650 and for each displayed buffer, where display starts, and the position of
6651 point. An exception is made for point in the current buffer:
6652 its value is -not- saved.
6653 This also records the currently selected frame, and FRAME's focus
6654 redirection (see `redirect-frame-focus'). The variable
6655 `window-persistent-parameters' specifies which window parameters are
6656 saved by this function. */)
6657 (Lisp_Object frame)
6659 register Lisp_Object tem;
6660 register int n_windows;
6661 register struct save_window_data *data;
6662 register int i;
6663 struct frame *f = decode_live_frame (frame);
6665 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6666 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6667 PVEC_WINDOW_CONFIGURATION);
6669 data->frame_cols = FRAME_COLS (f);
6670 data->frame_lines = FRAME_LINES (f);
6671 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6672 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6673 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6674 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6675 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6676 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6677 data->selected_frame = selected_frame;
6678 data->current_window = FRAME_SELECTED_WINDOW (f);
6679 XSETBUFFER (data->current_buffer, current_buffer);
6680 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6681 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6682 data->root_window = FRAME_ROOT_WINDOW (f);
6683 data->focus_frame = FRAME_FOCUS_FRAME (f);
6684 tem = make_uninit_vector (n_windows);
6685 data->saved_windows = tem;
6686 for (i = 0; i < n_windows; i++)
6687 ASET (tem, i,
6688 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6689 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6690 XSETWINDOW_CONFIGURATION (tem, data);
6691 return (tem);
6694 /* Called after W's margins, fringes or scroll bars was adjusted. */
6696 static void
6697 apply_window_adjustment (struct window *w)
6699 eassert (w);
6700 adjust_window_margins (w);
6701 clear_glyph_matrix (w->current_matrix);
6702 w->window_end_valid = 0;
6703 windows_or_buffers_changed = 30;
6704 wset_redisplay (w);
6705 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6709 /***********************************************************************
6710 Marginal Areas
6711 ***********************************************************************/
6713 static struct window *
6714 set_window_margins (struct window *w, Lisp_Object left_width,
6715 Lisp_Object right_width)
6717 int left, right;
6719 /* FIXME: what about margins that are too wide? */
6720 left = (NILP (left_width) ? 0
6721 : (CHECK_NATNUM (left_width), XINT (left_width)));
6722 right = (NILP (right_width) ? 0
6723 : (CHECK_NATNUM (right_width), XINT (right_width)));
6725 if (w->left_margin_cols != left || w->right_margin_cols != right)
6727 w->left_margin_cols = left;
6728 w->right_margin_cols = right;
6729 return w;
6731 return NULL;
6734 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6735 2, 3, 0,
6736 doc: /* Set width of marginal areas of window WINDOW.
6737 WINDOW must be a live window and defaults to the selected one.
6739 Second arg LEFT-WIDTH specifies the number of character cells to
6740 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6741 does the same for the right marginal area. A nil width parameter
6742 means no margin.
6744 Return t if any margin was actually changed and nil otherwise. */)
6745 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6747 struct window *w = set_window_margins (decode_live_window (window),
6748 left_width, right_width);
6749 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6753 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6754 0, 1, 0,
6755 doc: /* Get width of marginal areas of window WINDOW.
6756 WINDOW must be a live window and defaults to the selected one.
6758 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6759 If a marginal area does not exist, its width will be returned
6760 as nil. */)
6761 (Lisp_Object window)
6763 struct window *w = decode_live_window (window);
6764 return Fcons (w->left_margin_cols
6765 ? make_number (w->left_margin_cols) : Qnil,
6766 w->right_margin_cols
6767 ? make_number (w->right_margin_cols) : Qnil);
6772 /***********************************************************************
6773 Fringes
6774 ***********************************************************************/
6776 static struct window *
6777 set_window_fringes (struct window *w, Lisp_Object left_width,
6778 Lisp_Object right_width, Lisp_Object outside_margins)
6780 int left, right, outside = !NILP (outside_margins);
6782 left = (NILP (left_width) ? -1
6783 : (CHECK_NATNUM (left_width), XINT (left_width)));
6784 right = (NILP (right_width) ? -1
6785 : (CHECK_NATNUM (right_width), XINT (right_width)));
6787 /* Do nothing on a tty or if nothing to actually change. */
6788 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6789 && (w->left_fringe_width != left
6790 || w->right_fringe_width != right
6791 || w->fringes_outside_margins != outside))
6793 w->left_fringe_width = left;
6794 w->right_fringe_width = right;
6795 w->fringes_outside_margins = outside;
6796 return w;
6798 return NULL;
6801 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6802 2, 4, 0,
6803 doc: /* Set the fringe widths of window WINDOW.
6804 WINDOW must be a live window and defaults to the selected one.
6806 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6807 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6808 fringe width. If a fringe width arg is nil, that means to use the
6809 frame's default fringe width. Default fringe widths can be set with
6810 the command `set-fringe-style'.
6811 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6812 outside of the display margins. By default, fringes are drawn between
6813 display marginal areas and the text area.
6815 Return t if any fringe was actually changed and nil otherwise. */)
6816 (Lisp_Object window, Lisp_Object left_width,
6817 Lisp_Object right_width, Lisp_Object outside_margins)
6819 struct window *w
6820 = set_window_fringes (decode_live_window (window),
6821 left_width, right_width, outside_margins);
6822 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6826 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6827 0, 1, 0,
6828 doc: /* Get width of fringes of window WINDOW.
6829 WINDOW must be a live window and defaults to the selected one.
6831 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6832 (Lisp_Object window)
6834 struct window *w = decode_live_window (window);
6836 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6837 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6838 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6843 /***********************************************************************
6844 Scroll bars
6845 ***********************************************************************/
6847 static struct window *
6848 set_window_scroll_bars (struct window *w, Lisp_Object width,
6849 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6851 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6853 if (iwidth == 0)
6854 vertical_type = Qnil;
6856 if (!(NILP (vertical_type)
6857 || EQ (vertical_type, Qleft)
6858 || EQ (vertical_type, Qright)
6859 || EQ (vertical_type, Qt)))
6860 error ("Invalid type of vertical scroll bar");
6862 if (w->scroll_bar_width != iwidth
6863 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6865 w->scroll_bar_width = iwidth;
6866 wset_vertical_scroll_bar_type (w, vertical_type);
6867 return w;
6869 return NULL;
6872 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6873 Sset_window_scroll_bars, 2, 4, 0,
6874 doc: /* Set width and type of scroll bars of window WINDOW.
6875 WINDOW must be a live window and defaults to the selected one.
6877 Second parameter WIDTH specifies the pixel width for the scroll bar.
6878 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6879 bar: left, right, or nil.
6880 If WIDTH is nil, use the frame's scroll-bar width.
6881 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6882 Fourth parameter HORIZONTAL-TYPE is currently unused.
6884 Return t if scroll bars were actually changed and nil otherwise. */)
6885 (Lisp_Object window, Lisp_Object width,
6886 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6888 struct window *w
6889 = set_window_scroll_bars (decode_live_window (window),
6890 width, vertical_type, horizontal_type);
6891 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6895 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6896 0, 1, 0,
6897 doc: /* Get width and type of scroll bars of window WINDOW.
6898 WINDOW must be a live window and defaults to the selected one.
6900 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6901 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6902 value. */)
6903 (Lisp_Object window)
6905 struct window *w = decode_live_window (window);
6907 return list4 (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (w)),
6908 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6909 w->vertical_scroll_bar_type, Qnil);
6914 /***********************************************************************
6915 Smooth scrolling
6916 ***********************************************************************/
6918 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6919 doc: /* Return the amount by which WINDOW is scrolled vertically.
6920 If WINDOW is omitted or nil, it defaults to the selected window.
6921 Normally, value is a multiple of the canonical character height of WINDOW;
6922 optional second arg PIXELS-P means value is measured in pixels. */)
6923 (Lisp_Object window, Lisp_Object pixels_p)
6925 Lisp_Object result;
6926 struct window *w = decode_live_window (window);
6927 struct frame *f = XFRAME (w->frame);
6929 if (FRAME_WINDOW_P (f))
6930 result = (NILP (pixels_p)
6931 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6932 : make_number (-w->vscroll));
6933 else
6934 result = make_number (0);
6935 return result;
6939 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6940 2, 3, 0,
6941 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6942 WINDOW nil means use the selected window. Normally, VSCROLL is a
6943 non-negative multiple of the canonical character height of WINDOW;
6944 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6945 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6946 corresponds to an integral number of pixels. The return value is the
6947 result of this rounding.
6948 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6949 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6951 struct window *w = decode_live_window (window);
6952 struct frame *f = XFRAME (w->frame);
6954 CHECK_NUMBER_OR_FLOAT (vscroll);
6956 if (FRAME_WINDOW_P (f))
6958 int old_dy = w->vscroll;
6960 w->vscroll = - (NILP (pixels_p)
6961 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6962 : XFLOATINT (vscroll));
6963 w->vscroll = min (w->vscroll, 0);
6965 if (w->vscroll != old_dy)
6967 /* Adjust glyph matrix of the frame if the virtual display
6968 area becomes larger than before. */
6969 if (w->vscroll < 0 && w->vscroll < old_dy)
6970 adjust_frame_glyphs (f);
6972 /* Prevent redisplay shortcuts. */
6973 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6977 return Fwindow_vscroll (window, pixels_p);
6981 /* Call FN for all leaf windows on frame F. FN is called with the
6982 first argument being a pointer to the leaf window, and with
6983 additional argument USER_DATA. Stops when FN returns 0. */
6985 static void
6986 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6987 void *user_data)
6989 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6990 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6991 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6995 /* Helper function for foreach_window. Call FN for all leaf windows
6996 reachable from W. FN is called with the first argument being a
6997 pointer to the leaf window, and with additional argument USER_DATA.
6998 Stop when FN returns 0. Value is 0 if stopped by FN. */
7000 static int
7001 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7003 int cont;
7005 for (cont = 1; w && cont;)
7007 if (WINDOWP (w->contents))
7008 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7009 else
7010 cont = fn (w, user_data);
7012 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7015 return cont;
7018 /***********************************************************************
7019 Initialization
7020 ***********************************************************************/
7022 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7023 describe the same state of affairs. This is used by Fequal.
7025 IGNORE_POSITIONS means ignore non-matching scroll positions
7026 and the like.
7028 This ignores a couple of things like the dedication status of
7029 window, combination_limit and the like. This might have to be
7030 fixed. */
7032 bool
7033 compare_window_configurations (Lisp_Object configuration1,
7034 Lisp_Object configuration2,
7035 bool ignore_positions)
7037 register struct save_window_data *d1, *d2;
7038 struct Lisp_Vector *sws1, *sws2;
7039 ptrdiff_t i;
7041 CHECK_WINDOW_CONFIGURATION (configuration1);
7042 CHECK_WINDOW_CONFIGURATION (configuration2);
7044 d1 = (struct save_window_data *) XVECTOR (configuration1);
7045 d2 = (struct save_window_data *) XVECTOR (configuration2);
7046 sws1 = XVECTOR (d1->saved_windows);
7047 sws2 = XVECTOR (d2->saved_windows);
7049 /* Frame settings must match. */
7050 if (d1->frame_cols != d2->frame_cols
7051 || d1->frame_lines != d2->frame_lines
7052 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7053 || !EQ (d1->selected_frame, d2->selected_frame)
7054 || !EQ (d1->current_buffer, d2->current_buffer)
7055 || (!ignore_positions
7056 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7057 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7058 || !EQ (d1->focus_frame, d2->focus_frame)
7059 /* Verify that the two configurations have the same number of windows. */
7060 || sws1->header.size != sws2->header.size)
7061 return 0;
7063 for (i = 0; i < sws1->header.size; i++)
7065 struct saved_window *sw1, *sw2;
7067 sw1 = SAVED_WINDOW_N (sws1, i);
7068 sw2 = SAVED_WINDOW_N (sws2, i);
7070 if (
7071 /* The "current" windows in the two configurations must
7072 correspond to each other. */
7073 EQ (d1->current_window, sw1->window)
7074 != EQ (d2->current_window, sw2->window)
7075 /* Windows' buffers must match. */
7076 || !EQ (sw1->buffer, sw2->buffer)
7077 || !EQ (sw1->pixel_left, sw2->pixel_left)
7078 || !EQ (sw1->pixel_top, sw2->pixel_top)
7079 || !EQ (sw1->pixel_height, sw2->pixel_height)
7080 || !EQ (sw1->pixel_width, sw2->pixel_width)
7081 || !EQ (sw1->left_col, sw2->left_col)
7082 || !EQ (sw1->top_line, sw2->top_line)
7083 || !EQ (sw1->total_cols, sw2->total_cols)
7084 || !EQ (sw1->total_lines, sw2->total_lines)
7085 || !EQ (sw1->display_table, sw2->display_table)
7086 /* The next two disjuncts check the window structure for
7087 equality. */
7088 || !EQ (sw1->parent, sw2->parent)
7089 || !EQ (sw1->prev, sw2->prev)
7090 || (!ignore_positions
7091 && (!EQ (sw1->hscroll, sw2->hscroll)
7092 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7093 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7094 || NILP (Fequal (sw1->start, sw2->start))
7095 || NILP (Fequal (sw1->pointm, sw2->pointm))))
7096 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7097 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7098 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7099 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7100 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7101 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7102 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7103 return 0;
7106 return 1;
7109 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7110 Scompare_window_configurations, 2, 2, 0,
7111 doc: /* Compare two window configurations as regards the structure of windows.
7112 This function ignores details such as the values of point
7113 and scrolling positions. */)
7114 (Lisp_Object x, Lisp_Object y)
7116 if (compare_window_configurations (x, y, 1))
7117 return Qt;
7118 return Qnil;
7121 void
7122 init_window_once (void)
7124 struct frame *f = make_initial_frame ();
7125 XSETFRAME (selected_frame, f);
7126 Vterminal_frame = selected_frame;
7127 minibuf_window = f->minibuffer_window;
7128 selected_window = f->selected_window;
7131 void
7132 init_window (void)
7134 Vwindow_list = Qnil;
7137 void
7138 syms_of_window (void)
7140 DEFSYM (Qscroll_up, "scroll-up");
7141 DEFSYM (Qscroll_down, "scroll-down");
7142 DEFSYM (Qscroll_command, "scroll-command");
7144 Fput (Qscroll_up, Qscroll_command, Qt);
7145 Fput (Qscroll_down, Qscroll_command, Qt);
7147 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7148 DEFSYM (Qwindowp, "windowp");
7149 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7150 DEFSYM (Qwindow_live_p, "window-live-p");
7151 DEFSYM (Qwindow_valid_p, "window-valid-p");
7152 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7153 DEFSYM (Qdelete_window, "delete-window");
7154 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7155 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7156 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7157 DEFSYM (Qsafe, "safe");
7158 DEFSYM (Qdisplay_buffer, "display-buffer");
7159 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7160 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7161 DEFSYM (Qget_mru_window, "get-mru-window");
7162 DEFSYM (Qwindow_size, "window-size");
7163 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7164 DEFSYM (Qabove, "above");
7165 DEFSYM (Qbelow, "below");
7166 DEFSYM (Qclone_of, "clone-of");
7167 DEFSYM (Qfloor, "floor");
7168 DEFSYM (Qceiling, "ceiling");
7170 staticpro (&Vwindow_list);
7172 minibuf_selected_window = Qnil;
7173 staticpro (&minibuf_selected_window);
7175 window_scroll_pixel_based_preserve_x = -1;
7176 window_scroll_pixel_based_preserve_y = -1;
7177 window_scroll_preserve_hpos = -1;
7178 window_scroll_preserve_vpos = -1;
7180 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7181 doc: /* Non-nil means call as function to display a help buffer.
7182 The function is called with one argument, the buffer to be displayed.
7183 Used by `with-output-to-temp-buffer'.
7184 If this function is used, then it must do the entire job of showing
7185 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7186 Vtemp_buffer_show_function = Qnil;
7188 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7189 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7190 Vminibuf_scroll_window = Qnil;
7192 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7193 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7194 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7195 is displayed in the `mode-line' face. */);
7196 mode_line_in_non_selected_windows = 1;
7198 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7199 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7200 Vother_window_scroll_buffer = Qnil;
7202 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7203 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7204 auto_window_vscroll_p = 1;
7206 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7207 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7208 next_screen_context_lines = 2;
7210 DEFVAR_LISP ("scroll-preserve-screen-position",
7211 Vscroll_preserve_screen_position,
7212 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7213 A value of nil means point does not keep its screen position except
7214 at the scroll margin or window boundary respectively.
7215 A value of t means point keeps its screen position if the scroll
7216 command moved it vertically out of the window, e.g. when scrolling
7217 by full screens.
7218 Any other value means point always keeps its screen position.
7219 Scroll commands should have the `scroll-command' property
7220 on their symbols to be controlled by this variable. */);
7221 Vscroll_preserve_screen_position = Qnil;
7223 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7224 doc: /* Type of marker to use for `window-point'. */);
7225 Vwindow_point_insertion_type = Qnil;
7226 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7228 DEFVAR_LISP ("window-configuration-change-hook",
7229 Vwindow_configuration_change_hook,
7230 doc: /* Functions to call when window configuration changes.
7231 The buffer-local part is run once per window, with the relevant window
7232 selected; while the global part is run only once for the modified frame,
7233 with the relevant frame selected. */);
7234 Vwindow_configuration_change_hook = Qnil;
7236 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7237 doc: /* Non-nil means `recenter' redraws entire frame.
7238 If this option is non-nil, then the `recenter' command with a nil
7239 argument will redraw the entire frame; the special value `tty' causes
7240 the frame to be redrawn only if it is a tty frame. */);
7241 Vrecenter_redisplay = Qtty;
7243 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7244 doc: /* If t, resize window combinations proportionally.
7245 If this variable is nil, splitting a window gets the entire screen space
7246 for displaying the new window from the window to split. Deleting and
7247 resizing a window preferably resizes one adjacent window only.
7249 If this variable is t, splitting a window tries to get the space
7250 proportionally from all windows in the same combination. This also
7251 allows to split a window that is otherwise too small or of fixed size.
7252 Resizing and deleting a window proportionally resize all windows in the
7253 same combination.
7255 Other values are reserved for future use.
7257 This variable takes no effect if the variable `window-combination-limit' is
7258 non-nil. */);
7259 Vwindow_combination_resize = Qnil;
7261 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7262 doc: /* If non-nil, splitting a window makes a new parent window.
7263 The following values are recognized:
7265 nil means splitting a window will create a new parent window only if the
7266 window has no parent window or the window shall become part of a
7267 combination orthogonal to the one it is part of.
7269 `window-size' means that splitting a window for displaying a buffer
7270 makes a new parent window provided `display-buffer' is supposed to
7271 explicitly set the window's size due to the presence of a
7272 `window-height' or `window-width' entry in the alist used by
7273 `display-buffer'. Otherwise, this value is handled like nil.
7275 `temp-buffer' means that splitting a window for displaying a temporary
7276 buffer always makes a new parent window. Otherwise, this value is
7277 handled like nil.
7279 `display-buffer' means that splitting a window for displaying a buffer
7280 always makes a new parent window. Since temporary buffers are
7281 displayed by the function `display-buffer', this value is stronger
7282 than `temp-buffer'. Splitting a window for other purpose makes a
7283 new parent window only if needed.
7285 t means that splitting a window always creates a new parent window. If
7286 all splits behave this way, each frame's window tree is a binary
7287 tree and every window but the frame's root window has exactly one
7288 sibling.
7290 Other values are reserved for future use. */);
7291 Vwindow_combination_limit = Qwindow_size;
7293 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7294 doc: /* Alist of persistent window parameters.
7295 This alist specifies which window parameters shall get saved by
7296 `current-window-configuration' and `window-state-get' and subsequently
7297 restored to their previous values by `set-window-configuration' and
7298 `window-state-put'.
7300 The car of each entry of this alist is the symbol specifying the
7301 parameter. The cdr is one of the following:
7303 nil means the parameter is neither saved by `window-state-get' nor by
7304 `current-window-configuration'.
7306 t means the parameter is saved by `current-window-configuration' and,
7307 provided its WRITABLE argument is nil, by `window-state-get'.
7309 The symbol `writable' means the parameter is saved unconditionally by
7310 both `current-window-configuration' and `window-state-get'. Do not use
7311 this value for parameters without read syntax (like windows or frames).
7313 Parameters not saved by `current-window-configuration' or
7314 `window-state-get' are left alone by `set-window-configuration'
7315 respectively are not installed by `window-state-put'. */);
7316 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7318 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7319 doc: /* Non-nil means resize windows pixelwise.
7320 This currently affects the functions: `split-window', `maximize-window',
7321 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7322 all functions that symmetrically resize a parent window.
7324 Note that when a frame's pixel size is not a multiple of the
7325 frame's character size, at least one window may get resized
7326 pixelwise even if this option is nil. */);
7327 window_resize_pixelwise = 0;
7329 defsubr (&Sselected_window);
7330 defsubr (&Sminibuffer_window);
7331 defsubr (&Swindow_minibuffer_p);
7332 defsubr (&Swindowp);
7333 defsubr (&Swindow_valid_p);
7334 defsubr (&Swindow_live_p);
7335 defsubr (&Swindow_frame);
7336 defsubr (&Sframe_root_window);
7337 defsubr (&Sframe_first_window);
7338 defsubr (&Sframe_selected_window);
7339 defsubr (&Sset_frame_selected_window);
7340 defsubr (&Spos_visible_in_window_p);
7341 defsubr (&Swindow_line_height);
7342 defsubr (&Swindow_buffer);
7343 defsubr (&Swindow_parent);
7344 defsubr (&Swindow_top_child);
7345 defsubr (&Swindow_left_child);
7346 defsubr (&Swindow_next_sibling);
7347 defsubr (&Swindow_prev_sibling);
7348 defsubr (&Swindow_combination_limit);
7349 defsubr (&Sset_window_combination_limit);
7350 defsubr (&Swindow_use_time);
7351 defsubr (&Swindow_pixel_width);
7352 defsubr (&Swindow_pixel_height);
7353 defsubr (&Swindow_total_width);
7354 defsubr (&Swindow_total_height);
7355 defsubr (&Swindow_normal_size);
7356 defsubr (&Swindow_new_pixel);
7357 defsubr (&Swindow_new_total);
7358 defsubr (&Swindow_new_normal);
7359 defsubr (&Swindow_pixel_left);
7360 defsubr (&Swindow_pixel_top);
7361 defsubr (&Swindow_left_column);
7362 defsubr (&Swindow_top_line);
7363 defsubr (&Sset_window_new_pixel);
7364 defsubr (&Sset_window_new_total);
7365 defsubr (&Sset_window_new_normal);
7366 defsubr (&Swindow_resize_apply);
7367 defsubr (&Swindow_resize_apply_total);
7368 defsubr (&Swindow_body_height);
7369 defsubr (&Swindow_body_width);
7370 defsubr (&Swindow_hscroll);
7371 defsubr (&Sset_window_hscroll);
7372 defsubr (&Swindow_redisplay_end_trigger);
7373 defsubr (&Sset_window_redisplay_end_trigger);
7374 defsubr (&Swindow_edges);
7375 defsubr (&Swindow_pixel_edges);
7376 defsubr (&Swindow_absolute_pixel_edges);
7377 defsubr (&Swindow_mode_line_height);
7378 defsubr (&Swindow_header_line_height);
7379 defsubr (&Swindow_right_divider_width);
7380 defsubr (&Swindow_bottom_divider_width);
7381 defsubr (&Swindow_scroll_bar_width);
7382 defsubr (&Swindow_inside_edges);
7383 defsubr (&Swindow_inside_pixel_edges);
7384 defsubr (&Swindow_inside_absolute_pixel_edges);
7385 defsubr (&Scoordinates_in_window_p);
7386 defsubr (&Swindow_at);
7387 defsubr (&Swindow_point);
7388 defsubr (&Swindow_start);
7389 defsubr (&Swindow_end);
7390 defsubr (&Sset_window_point);
7391 defsubr (&Sset_window_start);
7392 defsubr (&Swindow_dedicated_p);
7393 defsubr (&Sset_window_dedicated_p);
7394 defsubr (&Swindow_display_table);
7395 defsubr (&Sset_window_display_table);
7396 defsubr (&Snext_window);
7397 defsubr (&Sprevious_window);
7398 defsubr (&Sget_buffer_window);
7399 defsubr (&Sdelete_other_windows_internal);
7400 defsubr (&Sdelete_window_internal);
7401 defsubr (&Sresize_mini_window_internal);
7402 defsubr (&Sset_window_buffer);
7403 defsubr (&Srun_window_configuration_change_hook);
7404 defsubr (&Srun_window_scroll_functions);
7405 defsubr (&Sselect_window);
7406 defsubr (&Sforce_window_update);
7407 defsubr (&Ssplit_window_internal);
7408 defsubr (&Sscroll_up);
7409 defsubr (&Sscroll_down);
7410 defsubr (&Sscroll_left);
7411 defsubr (&Sscroll_right);
7412 defsubr (&Sother_window_for_scrolling);
7413 defsubr (&Sscroll_other_window);
7414 defsubr (&Sminibuffer_selected_window);
7415 defsubr (&Srecenter);
7416 defsubr (&Swindow_text_width);
7417 defsubr (&Swindow_text_height);
7418 defsubr (&Smove_to_window_line);
7419 defsubr (&Swindow_configuration_p);
7420 defsubr (&Swindow_configuration_frame);
7421 defsubr (&Sset_window_configuration);
7422 defsubr (&Scurrent_window_configuration);
7423 defsubr (&Sset_window_margins);
7424 defsubr (&Swindow_margins);
7425 defsubr (&Sset_window_fringes);
7426 defsubr (&Swindow_fringes);
7427 defsubr (&Sset_window_scroll_bars);
7428 defsubr (&Swindow_scroll_bars);
7429 defsubr (&Swindow_vscroll);
7430 defsubr (&Sset_window_vscroll);
7431 defsubr (&Scompare_window_configurations);
7432 defsubr (&Swindow_list);
7433 defsubr (&Swindow_list_1);
7434 defsubr (&Swindow_prev_buffers);
7435 defsubr (&Sset_window_prev_buffers);
7436 defsubr (&Swindow_next_buffers);
7437 defsubr (&Sset_window_next_buffers);
7438 defsubr (&Swindow_parameters);
7439 defsubr (&Swindow_parameter);
7440 defsubr (&Sset_window_parameter);
7443 void
7444 keys_of_window (void)
7446 initial_define_key (control_x_map, '<', "scroll-left");
7447 initial_define_key (control_x_map, '>', "scroll-right");
7449 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7450 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7451 initial_define_key (meta_map, 'v', "scroll-down-command");