Replace some uses of obsolete argument of display-completion-list
[emacs.git] / src / window.c
blobb2a6ff4ff2d1ea941e91c0d7270d02b549ef2699
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 "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
43 #ifdef MSDOS
44 #include "msdos.h"
45 #endif
47 Lisp_Object Qwindowp, Qwindow_live_p;
48 static Lisp_Object Qwindow_valid_p;
49 static Lisp_Object Qwindow_configuration_p;
50 static Lisp_Object Qrecord_window_buffer;
51 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
52 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
53 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
54 static Lisp_Object Qwindow_pixel_to_total;
55 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
56 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
57 static Lisp_Object Qfloor, Qceiling;
59 static int displayed_window_lines (struct window *);
60 static int count_windows (struct window *);
61 static int get_leaf_windows (struct window *, struct window **, int);
62 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
63 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
64 static void window_scroll_line_based (Lisp_Object, int, bool, int);
65 static int add_window_to_list (struct window *, void *);
66 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
67 Lisp_Object, int);
68 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
69 Lisp_Object *);
70 static void foreach_window (struct frame *,
71 int (* fn) (struct window *, void *),
72 void *);
73 static int foreach_window_1 (struct window *,
74 int (* fn) (struct window *, void *),
75 void *);
76 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
77 static int window_resize_check (struct window *, bool);
78 static void window_resize_apply (struct window *, bool);
79 static void window_resize_apply_total (struct window *, bool);
80 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
81 static void select_window_1 (Lisp_Object, bool);
83 static struct window *set_window_fringes (struct window *, Lisp_Object,
84 Lisp_Object, Lisp_Object);
85 static struct window *set_window_margins (struct window *, Lisp_Object,
86 Lisp_Object);
87 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
88 Lisp_Object, Lisp_Object);
89 static void apply_window_adjustment (struct window *);
91 /* This is the window in which the terminal's cursor should
92 be left when nothing is being done with it. This must
93 always be a leaf window, and its buffer is selected by
94 the top level editing loop at the end of each command.
96 This value is always the same as
97 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109 Lisp_Object minibuf_window;
111 /* Non-nil means it is the window whose mode line should be
112 shown as the selected window when the minibuffer is selected. */
113 Lisp_Object minibuf_selected_window;
115 /* Hook run at end of temp_output_buffer_show. */
116 static Lisp_Object Qtemp_buffer_show_hook;
118 /* Incremented for each window created. */
119 static int sequence_number;
121 /* Nonzero after init_window_once has finished. */
122 static int window_initialized;
124 /* Hook to run when window config changes. */
125 static Lisp_Object Qwindow_configuration_change_hook;
127 /* Used by the function window_scroll_pixel_based */
128 static int window_scroll_pixel_based_preserve_x;
129 static int window_scroll_pixel_based_preserve_y;
131 /* Same for window_scroll_line_based. */
132 static EMACS_INT window_scroll_preserve_hpos;
133 static EMACS_INT window_scroll_preserve_vpos;
135 static void
136 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
138 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
141 /* These setters are used only in this file, so they can be private. */
142 static void
143 wset_combination_limit (struct window *w, Lisp_Object val)
145 w->combination_limit = val;
147 static void
148 wset_dedicated (struct window *w, Lisp_Object val)
150 w->dedicated = val;
152 static void
153 wset_display_table (struct window *w, Lisp_Object val)
155 w->display_table = val;
157 static void
158 wset_new_normal (struct window *w, Lisp_Object val)
160 w->new_normal = val;
162 static void
163 wset_new_total (struct window *w, Lisp_Object val)
165 w->new_total = val;
167 static void
168 wset_normal_cols (struct window *w, Lisp_Object val)
170 w->normal_cols = val;
172 static void
173 wset_normal_lines (struct window *w, Lisp_Object val)
175 w->normal_lines = val;
177 static void
178 wset_parent (struct window *w, Lisp_Object val)
180 w->parent = val;
182 static void
183 wset_pointm (struct window *w, Lisp_Object val)
185 w->pointm = val;
187 static void
188 wset_start (struct window *w, Lisp_Object val)
190 w->start = val;
192 static void
193 wset_temslot (struct window *w, Lisp_Object val)
195 w->temslot = val;
197 static void
198 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
200 w->vertical_scroll_bar_type = val;
202 static void
203 wset_window_parameters (struct window *w, Lisp_Object val)
205 w->window_parameters = val;
207 static void
208 wset_combination (struct window *w, bool horflag, Lisp_Object val)
210 /* Since leaf windows never becomes non-leaf, there should
211 be no buffer and markers in start and pointm fields of W. */
212 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
213 w->contents = val;
214 /* When an internal window is deleted and VAL is nil, HORFLAG
215 is meaningless. */
216 if (!NILP (val))
217 w->horizontal = horflag;
220 /* Nonzero if leaf window W doesn't reflect the actual state
221 of displayed buffer due to its text or overlays change. */
223 bool
224 window_outdated (struct window *w)
226 struct buffer *b = XBUFFER (w->contents);
227 return (w->last_modified < BUF_MODIFF (b)
228 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
231 struct window *
232 decode_live_window (register Lisp_Object window)
234 if (NILP (window))
235 return XWINDOW (selected_window);
237 CHECK_LIVE_WINDOW (window);
238 return XWINDOW (window);
241 struct window *
242 decode_any_window (register Lisp_Object window)
244 struct window *w;
246 if (NILP (window))
247 return XWINDOW (selected_window);
249 CHECK_WINDOW (window);
250 w = XWINDOW (window);
251 return w;
254 static struct window *
255 decode_valid_window (register Lisp_Object window)
257 struct window *w;
259 if (NILP (window))
260 return XWINDOW (selected_window);
262 CHECK_VALID_WINDOW (window);
263 w = XWINDOW (window);
264 return w;
267 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
268 cease its buffer, and 1 means that W is about to set up the new one. */
270 static void
271 adjust_window_count (struct window *w, int arg)
273 eassert (eabs (arg) == 1);
274 if (BUFFERP (w->contents))
276 struct buffer *b = XBUFFER (w->contents);
278 if (b->base_buffer)
279 b = b->base_buffer;
280 b->window_count += arg;
281 eassert (b->window_count >= 0);
282 /* These should be recalculated by redisplay code. */
283 w->window_end_valid = 0;
284 w->base_line_pos = 0;
288 /* Set W's buffer slot to VAL and recompute number
289 of windows showing VAL if it is a buffer. */
291 void
292 wset_buffer (struct window *w, Lisp_Object val)
294 adjust_window_count (w, -1);
295 if (BUFFERP (val))
296 /* Make sure that we do not assign the buffer
297 to an internal window. */
298 eassert (MARKERP (w->start) && MARKERP (w->pointm));
299 w->contents = val;
300 adjust_window_count (w, 1);
303 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
304 doc: /* Return t if OBJECT is a window and nil otherwise. */)
305 (Lisp_Object object)
307 return WINDOWP (object) ? Qt : Qnil;
310 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
311 doc: /* Return t if OBJECT is a valid window and nil otherwise.
312 A valid window is either a window that displays a buffer or an internal
313 window. Deleted windows are not live. */)
314 (Lisp_Object object)
316 return WINDOW_VALID_P (object) ? Qt : Qnil;
319 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
320 doc: /* Return t if OBJECT is a live window and nil otherwise.
321 A live window is a window that displays a buffer.
322 Internal windows and deleted windows are not live. */)
323 (Lisp_Object object)
325 return WINDOW_LIVE_P (object) ? Qt : Qnil;
328 /* Frames and windows. */
329 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
330 doc: /* Return the frame that window WINDOW is on.
331 WINDOW must be a valid window and defaults to the selected one. */)
332 (Lisp_Object window)
334 return decode_valid_window (window)->frame;
337 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
338 doc: /* Return the root window of FRAME-OR-WINDOW.
339 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
340 With a frame argument, return that frame's root window.
341 With a window argument, return the root window of that window's frame. */)
342 (Lisp_Object frame_or_window)
344 Lisp_Object window;
346 if (NILP (frame_or_window))
347 window = SELECTED_FRAME ()->root_window;
348 else if (WINDOW_VALID_P (frame_or_window))
349 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
350 else
352 CHECK_LIVE_FRAME (frame_or_window);
353 window = XFRAME (frame_or_window)->root_window;
356 return window;
359 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
360 doc: /* Return the minibuffer window for frame FRAME.
361 If FRAME is omitted or nil, it defaults to the selected frame. */)
362 (Lisp_Object frame)
364 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
367 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
368 Swindow_minibuffer_p, 0, 1, 0,
369 doc: /* Return non-nil if WINDOW is a minibuffer window.
370 WINDOW must be a valid window and defaults to the selected one. */)
371 (Lisp_Object window)
373 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
376 /* Don't move this to window.el - this must be a safe routine. */
377 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
378 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
379 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
380 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
381 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
382 the first window of that frame. */)
383 (Lisp_Object frame_or_window)
385 Lisp_Object window;
387 if (NILP (frame_or_window))
388 window = SELECTED_FRAME ()->root_window;
389 else if (WINDOW_VALID_P (frame_or_window))
390 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
391 else
393 CHECK_LIVE_FRAME (frame_or_window);
394 window = XFRAME (frame_or_window)->root_window;
397 while (WINDOWP (XWINDOW (window)->contents))
398 window = XWINDOW (window)->contents;
400 return window;
403 DEFUN ("frame-selected-window", Fframe_selected_window,
404 Sframe_selected_window, 0, 1, 0,
405 doc: /* Return the selected window of FRAME-OR-WINDOW.
406 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
407 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
408 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
409 return the selected window of that frame. */)
410 (Lisp_Object frame_or_window)
412 Lisp_Object window;
414 if (NILP (frame_or_window))
415 window = SELECTED_FRAME ()->selected_window;
416 else if (WINDOW_VALID_P (frame_or_window))
417 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
418 else
420 CHECK_LIVE_FRAME (frame_or_window);
421 window = XFRAME (frame_or_window)->selected_window;
424 return window;
427 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
428 Sset_frame_selected_window, 2, 3, 0,
429 doc: /* Set selected window of FRAME to WINDOW.
430 FRAME must be a live frame and defaults to the selected one. If FRAME
431 is the selected frame, this makes WINDOW the selected window. Optional
432 argument NORECORD non-nil means to neither change the order of recently
433 selected windows nor the buffer list. WINDOW must denote a live window.
434 Return WINDOW. */)
435 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
437 if (NILP (frame))
438 frame = selected_frame;
440 CHECK_LIVE_FRAME (frame);
441 CHECK_LIVE_WINDOW (window);
443 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
444 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
446 if (EQ (frame, selected_frame))
447 return Fselect_window (window, norecord);
448 else
450 fset_selected_window (XFRAME (frame), window);
451 return window;
455 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
456 doc: /* Return the selected window.
457 The selected window is the window in which the standard cursor for
458 selected windows appears and to which many commands apply. */)
459 (void)
461 return selected_window;
464 int window_select_count;
466 /* If select_window is called with inhibit_point_swap non-zero it will
467 not store point of the old selected window's buffer back into that
468 window's pointm slot. This is needed by Fset_window_configuration to
469 avoid that the display routine is called with selected_window set to
470 Qnil causing a subsequent crash. */
471 static Lisp_Object
472 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
474 register struct window *w;
475 struct frame *sf;
477 CHECK_LIVE_WINDOW (window);
479 w = XWINDOW (window);
481 /* Make the selected window's buffer current. */
482 Fset_buffer (w->contents);
484 if (EQ (window, selected_window) && !inhibit_point_swap)
485 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
486 way to call record_buffer from Elisp, so it's important that we call
487 record_buffer before returning here. */
488 goto record_and_return;
490 if (NILP (norecord))
491 { /* Mark the window for redisplay since the selected-window has
492 a different mode-line. */
493 wset_redisplay (XWINDOW (selected_window));
494 wset_redisplay (w);
496 else
497 redisplay_other_windows ();
499 sf = SELECTED_FRAME ();
500 if (XFRAME (WINDOW_FRAME (w)) != sf)
502 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
503 /* Use this rather than Fhandle_switch_frame
504 so that FRAME_FOCUS_FRAME is moved appropriately as we
505 move around in the state where a minibuffer in a separate
506 frame is active. */
507 Fselect_frame (WINDOW_FRAME (w), norecord);
508 /* Fselect_frame called us back so we've done all the work already. */
509 eassert (EQ (window, selected_window));
510 return window;
512 else
513 fset_selected_window (sf, window);
515 select_window_1 (window, inhibit_point_swap);
516 bset_last_selected_window (XBUFFER (w->contents), window);
518 record_and_return:
519 /* record_buffer can run QUIT, so make sure it is run only after we have
520 re-established the invariant between selected_window and selected_frame,
521 otherwise the temporary broken invariant might "escape" (bug#14161). */
522 if (NILP (norecord))
524 w->use_time = ++window_select_count;
525 record_buffer (w->contents);
528 return window;
531 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
532 (not even for redisplay's benefit), and assume that the window's frame is
533 already selected. */
534 static void
535 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
537 /* Store the old selected window's buffer's point in pointm of the old
538 selected window. It belongs to that window, and when the window is
539 not selected, must be in the window. */
540 if (!inhibit_point_swap)
542 struct window *ow = XWINDOW (selected_window);
543 if (BUFFERP (ow->contents))
544 set_marker_both (ow->pointm, ow->contents,
545 BUF_PT (XBUFFER (ow->contents)),
546 BUF_PT_BYTE (XBUFFER (ow->contents)));
549 selected_window = window;
551 /* Go to the point recorded in the window.
552 This is important when the buffer is in more
553 than one window. It also matters when
554 redisplay_window has altered point after scrolling,
555 because it makes the change only in the window. */
556 set_point_from_marker (XWINDOW (window)->pointm);
559 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
560 doc: /* Select WINDOW which must be a live window.
561 Also make WINDOW's frame the selected frame and WINDOW that frame's
562 selected window. In addition, make WINDOW's buffer current and set that
563 buffer's value of `point' to the value of WINDOW's `window-point'.
564 Return WINDOW.
566 Optional second arg NORECORD non-nil means do not put this buffer at the
567 front of the buffer list and do not make this window the most recently
568 selected one.
570 Note that the main editor command loop sets the current buffer to the
571 buffer of the selected window before each command. */)
572 (register Lisp_Object window, Lisp_Object norecord)
574 return select_window (window, norecord, 0);
577 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
578 doc: /* Return the buffer displayed in window WINDOW.
579 If WINDOW is omitted or nil, it defaults to the selected window.
580 Return nil for an internal window or a deleted window. */)
581 (Lisp_Object window)
583 struct window *w = decode_any_window (window);
584 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
587 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
588 doc: /* Return the parent window of window WINDOW.
589 WINDOW must be a valid window and defaults to the selected one.
590 Return nil for a window with no parent (e.g. a root window). */)
591 (Lisp_Object window)
593 return decode_valid_window (window)->parent;
596 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
597 doc: /* Return the topmost child window of window WINDOW.
598 WINDOW must be a valid window and defaults to the selected one.
599 Return nil if WINDOW is a live window (live windows have no children).
600 Return nil if WINDOW is an internal window whose children form a
601 horizontal combination. */)
602 (Lisp_Object window)
604 struct window *w = decode_valid_window (window);
605 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
608 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
609 doc: /* Return the leftmost child window of window WINDOW.
610 WINDOW must be a valid window and defaults to the selected one.
611 Return nil if WINDOW is a live window (live windows have no children).
612 Return nil if WINDOW is an internal window whose children form a
613 vertical combination. */)
614 (Lisp_Object window)
616 struct window *w = decode_valid_window (window);
617 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
620 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
621 doc: /* Return the next sibling window of window WINDOW.
622 WINDOW must be a valid window and defaults to the selected one.
623 Return nil if WINDOW has no next sibling. */)
624 (Lisp_Object window)
626 return decode_valid_window (window)->next;
629 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
630 doc: /* Return the previous sibling window of window WINDOW.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW has no previous sibling. */)
633 (Lisp_Object window)
635 return decode_valid_window (window)->prev;
638 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
639 doc: /* Return combination limit of window WINDOW.
640 WINDOW must be a valid window used in horizontal or vertical combination.
641 If the return value is nil, child windows of WINDOW can be recombined with
642 WINDOW's siblings. A return value of t means that child windows of
643 WINDOW are never \(re-)combined with WINDOW's siblings. */)
644 (Lisp_Object window)
646 struct window *w;
648 CHECK_VALID_WINDOW (window);
649 w = XWINDOW (window);
650 if (WINDOW_LEAF_P (w))
651 error ("Combination limit is meaningful for internal windows only");
652 return w->combination_limit;
655 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
656 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
657 WINDOW must be a valid window used in horizontal or vertical combination.
658 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
659 siblings. LIMIT t means that child windows of WINDOW are never
660 \(re-)combined with WINDOW's siblings. Other values are reserved for
661 future use. */)
662 (Lisp_Object window, Lisp_Object limit)
664 struct window *w;
666 CHECK_VALID_WINDOW (window);
667 w = XWINDOW (window);
668 if (WINDOW_LEAF_P (w))
669 error ("Combination limit is meaningful for internal windows only");
670 wset_combination_limit (w, limit);
671 return limit;
674 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
675 doc: /* Return the use time of window WINDOW.
676 WINDOW must be a live window and defaults to the selected one.
677 The window with the highest use time is the most recently selected
678 one. The window with the lowest use time is the least recently
679 selected one. */)
680 (Lisp_Object window)
682 return make_number (decode_live_window (window)->use_time);
685 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
686 doc: /* Return the width of window WINDOW in pixels.
687 WINDOW must be a valid window and defaults to the selected one.
689 The return value includes the fringes and margins of WINDOW as well as
690 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
691 an internal window, its pixel width is the width of the screen areas
692 spanned by its children. */)
693 (Lisp_Object window)
695 return make_number (decode_valid_window (window)->pixel_width);
698 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
699 doc: /* Return the height of window WINDOW in pixels.
700 WINDOW must be a valid window and defaults to the selected one.
702 The return value includes the mode line and header line, if any. If
703 WINDOW is an internal window, its pixel height is the height of the
704 screen areas spanned by its children. */)
705 (Lisp_Object window)
707 return make_number (decode_valid_window (window)->pixel_height);
710 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
711 doc: /* Return the height of window WINDOW in lines.
712 WINDOW must be a valid window and defaults to the selected one.
714 The return value includes the heights of WINDOW's mode and header line
715 and its bottom divider, if any. If WINDOW is an internal window, the
716 total height is the height of the screen areas spanned by its children.
718 If WINDOW's pixel height is not an integral multiple of its frame's
719 character height, the number of lines occupied by WINDOW is rounded
720 internally. This is done in a way such that, if WINDOW is a parent
721 window, the sum of the total heights of all its children internally
722 equals the total height of WINDOW.
724 If the optional argument ROUND is `ceiling', return the smallest integer
725 larger than WINDOW's pixel height divided by the character height of
726 WINDOW's frame. ROUND `floor' means to return the largest integer
727 smaller than WINDOW's pixel height divided by the character height of
728 WINDOW's frame. Any other value of ROUND means to return the internal
729 total height of WINDOW. */)
730 (Lisp_Object window, Lisp_Object round)
732 struct window *w = decode_valid_window (window);
734 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
735 return make_number (w->total_lines);
736 else
738 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
740 return make_number (EQ (round, Qceiling)
741 ? ((w->pixel_height + unit - 1) /unit)
742 : (w->pixel_height / unit));
746 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
747 doc: /* Return the total width of window WINDOW in columns.
748 WINDOW must be a valid window and defaults to the selected one.
750 The return value includes the widths of WINDOW's fringes, margins,
751 scroll bars and its right divider, if any. If WINDOW is an internal
752 window, the total width is the width of the screen areas spanned by its
753 children.
755 If WINDOW's pixel width is not an integral multiple of its frame's
756 character width, the number of lines occupied by WINDOW is rounded
757 internally. This is done in a way such that, if WINDOW is a parent
758 window, the sum of the total widths of all its children internally
759 equals the total width of WINDOW.
761 If the optional argument ROUND is `ceiling', return the smallest integer
762 larger than WINDOW's pixel width divided by the character width of
763 WINDOW's frame. ROUND `floor' means to return the largest integer
764 smaller than WINDOW's pixel width divided by the character width of
765 WINDOW's frame. Any other value of ROUND means to return the internal
766 total width of WINDOW. */)
767 (Lisp_Object window, Lisp_Object round)
769 struct window *w = decode_valid_window (window);
771 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
772 return make_number (w->total_cols);
773 else
775 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
777 return make_number (EQ (round, Qceiling)
778 ? ((w->pixel_width + unit - 1) /unit)
779 : (w->pixel_width / unit));
783 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
784 doc: /* Return the new total size of window WINDOW.
785 WINDOW must be a valid window and defaults to the selected one. */)
786 (Lisp_Object window)
788 return decode_valid_window (window)->new_total;
791 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
792 doc: /* Return the normal height of window WINDOW.
793 WINDOW must be a valid window and defaults to the selected one.
794 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
795 (Lisp_Object window, Lisp_Object horizontal)
797 struct window *w = decode_valid_window (window);
799 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
802 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
803 doc: /* Return new normal size of window WINDOW.
804 WINDOW must be a valid window and defaults to the selected one. */)
805 (Lisp_Object window)
807 return decode_valid_window (window)->new_normal;
810 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
811 doc: /* Return new pixel size of window WINDOW.
812 WINDOW must be a valid window and defaults to the selected one. */)
813 (Lisp_Object window)
815 return decode_valid_window (window)->new_pixel;
818 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
819 doc: /* Return left pixel edge of window WINDOW.
820 WINDOW must be a valid window and defaults to the selected one. */)
821 (Lisp_Object window)
823 return make_number (decode_valid_window (window)->pixel_left);
826 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
827 doc: /* Return top pixel edge of window WINDOW.
828 WINDOW must be a valid window and defaults to the selected one. */)
829 (Lisp_Object window)
831 return make_number (decode_valid_window (window)->pixel_top);
834 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
835 doc: /* Return left column of window WINDOW.
836 This is the distance, in columns, between the left edge of WINDOW and
837 the left edge of the frame's window area. For instance, the return
838 value is 0 if there is no window to the left of WINDOW.
840 WINDOW must be a valid window and defaults to the selected one. */)
841 (Lisp_Object window)
843 return make_number (decode_valid_window (window)->left_col);
846 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
847 doc: /* Return top line of window WINDOW.
848 This is the distance, in lines, between the top of WINDOW and the top
849 of the frame's window area. For instance, the return value is 0 if
850 there is no window above WINDOW.
852 WINDOW must be a valid window and defaults to the selected one. */)
853 (Lisp_Object window)
855 return make_number (decode_valid_window (window)->top_line);
858 /* Return the number of lines/pixels of W's body. Don't count any mode
859 or header line or horizontal divider of W. Rounds down to nearest
860 integer when not working pixelwise. */
861 static int
862 window_body_height (struct window *w, bool pixelwise)
864 int height = (w->pixel_height
865 - WINDOW_HEADER_LINE_HEIGHT (w)
866 - WINDOW_MODE_LINE_HEIGHT (w)
867 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
869 /* Don't return a negative value. */
870 return max (pixelwise
871 ? height
872 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
876 /* Return the number of columns/pixels of W's body. Don't count columns
877 occupied by the scroll bar or the divider/vertical bar separating W
878 from its right sibling or margins. On window-systems don't count
879 fringes either. Round down to nearest integer when not working
880 pixelwise. */
882 window_body_width (struct window *w, bool pixelwise)
884 struct frame *f = XFRAME (WINDOW_FRAME (w));
886 int width = (w->pixel_width
887 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
888 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
889 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
890 : ((!FRAME_WINDOW_P (f)
891 && !WINDOW_RIGHTMOST_P (w)
892 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
893 /* A vertical bar is either 1 or 0. */
894 ? 1 : 0))
895 - WINDOW_MARGINS_WIDTH (w)
896 - (FRAME_WINDOW_P (f)
897 ? WINDOW_FRINGES_WIDTH (w)
898 : 0));
900 /* Don't return a negative value. */
901 return max (pixelwise
902 ? width
903 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
907 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
908 doc: /* Return the height of WINDOW's text area.
909 WINDOW must be a live window and defaults to the selected one. Optional
910 argument PIXELWISE non-nil means return the height of WINDOW's text area
911 in pixels. The return value does not include the mode line or header
912 line or any horizontal divider.
914 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
915 pixel height divided by the character height of WINDOW's frame. This
916 means that if a line at the bottom of the text area is only partially
917 visible, that line is not counted. */)
918 (Lisp_Object window, Lisp_Object pixelwise)
920 return make_number (window_body_height (decode_live_window (window),
921 NILP (pixelwise) ? 0 : 1));
924 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
925 doc: /* Return the width of WINDOW's text area.
926 WINDOW must be a live window and defaults to the selected one. Optional
927 argument PIXELWISE non-nil means return the width in pixels. The return
928 value does not include any vertical dividers, fringes or marginal areas,
929 or scroll bars.
931 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
932 pixel width divided by the character width of WINDOW's frame. This
933 means that if a column at the right of the text area is only partially
934 visible, that column is not counted. */)
935 (Lisp_Object window, Lisp_Object pixelwise)
937 return make_number (window_body_width (decode_live_window (window),
938 NILP (pixelwise) ? 0 : 1));
941 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
942 Swindow_mode_line_height, 0, 1, 0,
943 doc: /* Return the height in pixels of WINDOW's mode-line.
944 WINDOW must be a live window and defaults to the selected one. */)
945 (Lisp_Object window)
947 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
950 DEFUN ("window-header-line-height", Fwindow_header_line_height,
951 Swindow_header_line_height, 0, 1, 0,
952 doc: /* Return the height in pixels of WINDOW's header-line.
953 WINDOW must be a live window and defaults to the selected one. */)
954 (Lisp_Object window)
956 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
959 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
960 Swindow_right_divider_width, 0, 1, 0,
961 doc: /* Return the width in pixels of WINDOW's right divider.
962 WINDOW must be a live window and defaults to the selected one. */)
963 (Lisp_Object window)
965 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
968 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
969 Swindow_bottom_divider_width, 0, 1, 0,
970 doc: /* Return the width in pixels of WINDOW's bottom divider.
971 WINDOW must be a live window and defaults to the selected one. */)
972 (Lisp_Object window)
974 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
977 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
978 Swindow_scroll_bar_width, 0, 1, 0,
979 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
980 WINDOW must be a live window and defaults to the selected one. */)
981 (Lisp_Object window)
983 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
986 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
987 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
988 WINDOW must be a live window and defaults to the selected one. */)
989 (Lisp_Object window)
991 return make_number (decode_live_window (window)->hscroll);
994 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
995 range, returning the new amount as a fixnum. */
996 static Lisp_Object
997 set_window_hscroll (struct window *w, EMACS_INT hscroll)
999 /* Horizontal scrolling has problems with large scroll amounts.
1000 It's too slow with long lines, and even with small lines the
1001 display can be messed up. For now, though, impose only the limits
1002 required by the internal representation: horizontal scrolling must
1003 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1004 (since it's stored in a ptrdiff_t). */
1005 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1006 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1008 /* Prevent redisplay shortcuts when changing the hscroll. */
1009 if (w->hscroll != new_hscroll)
1010 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1012 w->hscroll = new_hscroll;
1013 return make_number (new_hscroll);
1016 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1017 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1018 WINDOW must be a live window and defaults to the selected one.
1019 Clip the number to a reasonable value if out of range.
1020 Return the new number. NCOL should be zero or positive.
1022 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1023 window so that the location of point moves off-window. */)
1024 (Lisp_Object window, Lisp_Object ncol)
1026 CHECK_NUMBER (ncol);
1027 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1030 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1031 Swindow_redisplay_end_trigger, 0, 1, 0,
1032 doc: /* Return WINDOW's redisplay end trigger value.
1033 WINDOW must be a live window and defaults to the selected one.
1034 See `set-window-redisplay-end-trigger' for more information. */)
1035 (Lisp_Object window)
1037 return decode_live_window (window)->redisplay_end_trigger;
1040 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1041 Sset_window_redisplay_end_trigger, 2, 2, 0,
1042 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1043 WINDOW must be a live window and defaults to the selected one. VALUE
1044 should be a buffer position (typically a marker) or nil. If it is a
1045 buffer position, then if redisplay in WINDOW reaches a position beyond
1046 VALUE, the functions in `redisplay-end-trigger-functions' are called
1047 with two arguments: WINDOW, and the end trigger value. Afterwards the
1048 end-trigger value is reset to nil. */)
1049 (register Lisp_Object window, Lisp_Object value)
1051 wset_redisplay_end_trigger (decode_live_window (window), value);
1052 return value;
1055 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1056 doc: /* Return a list of the edge coordinates of WINDOW.
1057 WINDOW must be a valid window and defaults to the selected one.
1059 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1060 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1061 0 at top left corner of frame.
1063 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1064 is one more than the bottommost row occupied by WINDOW. The edges
1065 include the space used by WINDOW's scroll bar, display margins, fringes,
1066 header line, and/or mode line. For the edges of just the text area, use
1067 `window-inside-edges'. */)
1068 (Lisp_Object window)
1070 register struct window *w = decode_valid_window (window);
1072 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1073 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1076 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1077 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1078 WINDOW must be a valid window and defaults to the selected one.
1080 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1081 0, 0 at the top left corner of the frame.
1083 RIGHT is one more than the rightmost x position occupied by WINDOW.
1084 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1085 The pixel edges include the space used by WINDOW's scroll bar, display
1086 margins, fringes, header line, and/or mode line. For the pixel edges
1087 of just the text area, use `window-inside-pixel-edges'. */)
1088 (Lisp_Object window)
1090 register struct window *w = decode_valid_window (window);
1092 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1093 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1096 static void
1097 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1099 struct frame *f = XFRAME (w->frame);
1100 *add_y = f->top_pos;
1101 #ifdef FRAME_MENUBAR_HEIGHT
1102 *add_y += FRAME_MENUBAR_HEIGHT (f);
1103 #endif
1104 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1105 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1106 #elif defined (FRAME_TOOLBAR_HEIGHT)
1107 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1108 #endif
1109 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1110 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1111 #endif
1112 *add_x = f->left_pos;
1113 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1114 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1115 #endif
1118 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1119 Swindow_absolute_pixel_edges, 0, 1, 0,
1120 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1121 WINDOW must be a valid window and defaults to the selected one.
1123 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1124 0, 0 at the top left corner of the display.
1126 RIGHT is one more than the rightmost x position occupied by WINDOW.
1127 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1128 The pixel edges include the space used by WINDOW's scroll bar, display
1129 margins, fringes, header line, and/or mode line. For the pixel edges
1130 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1131 (Lisp_Object window)
1133 register struct window *w = decode_valid_window (window);
1134 int add_x, add_y;
1136 calc_absolute_offset (w, &add_x, &add_y);
1138 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1139 WINDOW_TOP_EDGE_Y (w) + add_y,
1140 WINDOW_RIGHT_EDGE_X (w) + add_x,
1141 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1144 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1145 doc: /* Return a list of the edge coordinates of WINDOW.
1146 WINDOW must be a live window and defaults to the selected one.
1148 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1149 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1150 0 at top left corner of frame.
1152 RIGHT is one more than the rightmost column of WINDOW's text area.
1153 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1154 inside edges do not include the space used by the WINDOW's scroll bar,
1155 display margins, fringes, header line, and/or mode line. */)
1156 (Lisp_Object window)
1158 register struct window *w = decode_live_window (window);
1160 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1161 + WINDOW_LEFT_MARGIN_COLS (w)
1162 + WINDOW_LEFT_FRINGE_COLS (w)),
1163 (WINDOW_TOP_EDGE_LINE (w)
1164 + WINDOW_HEADER_LINE_LINES (w)),
1165 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1166 - WINDOW_RIGHT_MARGIN_COLS (w)
1167 - WINDOW_RIGHT_FRINGE_COLS (w)),
1168 (WINDOW_BOTTOM_EDGE_LINE (w)
1169 - WINDOW_MODE_LINE_LINES (w)));
1172 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1173 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1174 WINDOW must be a live window and defaults to the selected one.
1176 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1177 (0,0) at the top left corner of the frame's window area.
1179 RIGHT is one more than the rightmost x position of WINDOW's text area.
1180 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1181 The inside edges do not include the space used by WINDOW's scroll bar,
1182 display margins, fringes, header line, and/or mode line. */)
1183 (Lisp_Object window)
1185 register struct window *w = decode_live_window (window);
1187 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1188 + WINDOW_LEFT_MARGIN_WIDTH (w)
1189 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1190 (WINDOW_TOP_EDGE_Y (w)
1191 + WINDOW_HEADER_LINE_HEIGHT (w)),
1192 (WINDOW_BOX_RIGHT_EDGE_X (w)
1193 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1194 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1195 (WINDOW_BOTTOM_EDGE_Y (w)
1196 - WINDOW_MODE_LINE_HEIGHT (w)));
1199 DEFUN ("window-inside-absolute-pixel-edges",
1200 Fwindow_inside_absolute_pixel_edges,
1201 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1202 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1203 WINDOW must be a live window and defaults to the selected one.
1205 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1206 (0,0) at the top left corner of the frame's window area.
1208 RIGHT is one more than the rightmost x position of WINDOW's text area.
1209 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1210 The inside edges do not include the space used by WINDOW's scroll bar,
1211 display margins, fringes, header line, and/or mode line. */)
1212 (Lisp_Object window)
1214 register struct window *w = decode_live_window (window);
1215 int add_x, add_y;
1217 calc_absolute_offset (w, &add_x, &add_y);
1219 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1220 + WINDOW_LEFT_MARGIN_WIDTH (w)
1221 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1222 (WINDOW_TOP_EDGE_Y (w)
1223 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1224 (WINDOW_BOX_RIGHT_EDGE_X (w)
1225 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1226 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1227 (WINDOW_BOTTOM_EDGE_Y (w)
1228 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1231 /* Test if the character at column X, row Y is within window W.
1232 If it is not, return ON_NOTHING;
1233 if it is on the window's vertical divider, return
1234 ON_RIGHT_DIVIDER;
1235 if it is on the window's horizontal divider, return
1236 ON_BOTTOM_DIVIDER;
1237 if it is in the window's text area, return ON_TEXT;
1238 if it is on the window's modeline, return ON_MODE_LINE;
1239 if it is on the border between the window and its right sibling,
1240 return ON_VERTICAL_BORDER;
1241 if it is on a scroll bar, return ON_SCROLL_BAR;
1242 if it is on the window's top line, return ON_HEADER_LINE;
1243 if it is in left or right fringe of the window,
1244 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1245 if it is in the marginal area to the left/right of the window,
1246 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1248 X and Y are frame relative pixel coordinates. */
1250 static enum window_part
1251 coordinates_in_window (register struct window *w, int x, int y)
1253 struct frame *f = XFRAME (WINDOW_FRAME (w));
1254 enum window_part part;
1255 int ux = FRAME_COLUMN_WIDTH (f);
1256 int left_x = WINDOW_LEFT_EDGE_X (w);
1257 int right_x = WINDOW_RIGHT_EDGE_X (w);
1258 int top_y = WINDOW_TOP_EDGE_Y (w);
1259 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1260 /* The width of the area where the vertical line can be dragged.
1261 (Between mode lines for instance. */
1262 int grabbable_width = ux;
1263 int lmargin_width, rmargin_width, text_left, text_right;
1265 /* Outside any interesting row or column? */
1266 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1267 return ON_NOTHING;
1269 /* On the horizontal window divider (which prevails the vertical
1270 divider)? */
1271 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1272 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1273 && y <= bottom_y)
1274 return ON_BOTTOM_DIVIDER;
1275 /* On vertical window divider? */
1276 else if (!WINDOW_RIGHTMOST_P (w)
1277 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1278 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1279 && x <= right_x)
1280 return ON_RIGHT_DIVIDER;
1281 /* On the mode or header line? */
1282 else if ((WINDOW_WANTS_MODELINE_P (w)
1283 && y >= (bottom_y
1284 - CURRENT_MODE_LINE_HEIGHT (w)
1285 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1286 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1287 && (part = ON_MODE_LINE))
1288 || (WINDOW_WANTS_HEADER_LINE_P (w)
1289 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1290 && (part = ON_HEADER_LINE)))
1292 /* If it's under/over the scroll bar portion of the mode/header
1293 line, say it's on the vertical line. That's to be able to
1294 resize windows horizontally in case we're using toolkit scroll
1295 bars. Note: If scrollbars are on the left, the window that
1296 must be eventually resized is that on the left of WINDOW. */
1297 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1298 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1299 && !WINDOW_LEFTMOST_P (w)
1300 && eabs (x - left_x) < grabbable_width)
1301 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1302 && !WINDOW_RIGHTMOST_P (w)
1303 && eabs (x - right_x) < grabbable_width)))
1304 return ON_VERTICAL_BORDER;
1305 else
1306 return part;
1309 /* In what's below, we subtract 1 when computing right_x because we
1310 want the rightmost pixel, which is given by left_pixel+width-1. */
1311 if (w->pseudo_window_p)
1313 left_x = 0;
1314 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1316 else
1318 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1319 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1322 /* Outside any interesting column? */
1323 if (x < left_x || x > right_x)
1324 return ON_SCROLL_BAR;
1326 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1327 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1329 text_left = window_box_left (w, TEXT_AREA);
1330 text_right = text_left + window_box_width (w, TEXT_AREA);
1332 if (FRAME_WINDOW_P (f))
1334 if (!w->pseudo_window_p
1335 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1336 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1337 && !WINDOW_RIGHTMOST_P (w)
1338 && (eabs (x - right_x) < grabbable_width))
1339 return ON_VERTICAL_BORDER;
1341 /* Need to say "x > right_x" rather than >=, since on character
1342 terminals, the vertical line's x coordinate is right_x. */
1343 else if (!w->pseudo_window_p
1344 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1345 && !WINDOW_RIGHTMOST_P (w)
1346 /* Why check ux if we are not the rightmost window? Also
1347 shouldn't a pseudo window always be rightmost? */
1348 && x > right_x - ux)
1349 return ON_VERTICAL_BORDER;
1351 if (x < text_left)
1353 if (lmargin_width > 0
1354 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1355 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1356 : (x < left_x + lmargin_width)))
1357 return ON_LEFT_MARGIN;
1358 else
1359 return ON_LEFT_FRINGE;
1362 if (x >= text_right)
1364 if (rmargin_width > 0
1365 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1366 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1367 : (x >= right_x - rmargin_width)))
1368 return ON_RIGHT_MARGIN;
1369 else
1370 return ON_RIGHT_FRINGE;
1373 /* Everything special ruled out - must be on text area */
1374 return ON_TEXT;
1377 /* Take X is the frame-relative pixel x-coordinate, and return the
1378 x-coordinate relative to part PART of window W. */
1380 window_relative_x_coord (struct window *w, enum window_part part, int x)
1382 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1384 switch (part)
1386 case ON_TEXT:
1387 return x - window_box_left (w, TEXT_AREA);
1389 case ON_HEADER_LINE:
1390 case ON_MODE_LINE:
1391 case ON_LEFT_FRINGE:
1392 return x - left_x;
1394 case ON_RIGHT_FRINGE:
1395 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1397 case ON_LEFT_MARGIN:
1398 return (x - left_x
1399 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1400 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1402 case ON_RIGHT_MARGIN:
1403 return (x + 1
1404 - ((w->pseudo_window_p)
1405 ? WINDOW_PIXEL_WIDTH (w)
1406 : WINDOW_BOX_RIGHT_EDGE_X (w))
1407 + window_box_width (w, RIGHT_MARGIN_AREA)
1408 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1409 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1412 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1413 return 0;
1417 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1418 Scoordinates_in_window_p, 2, 2, 0,
1419 doc: /* Return non-nil if COORDINATES are in WINDOW.
1420 WINDOW must be a live window and defaults to the selected one.
1421 COORDINATES is a cons of the form (X . Y), X and Y being distances
1422 measured in characters from the upper-left corner of the frame.
1423 \(0 . 0) denotes the character in the upper left corner of the
1424 frame.
1425 If COORDINATES are in the text portion of WINDOW,
1426 the coordinates relative to the window are returned.
1427 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1428 If they are in the right divider of WINDOW, `right-divider' is returned.
1429 If they are in the mode line of WINDOW, `mode-line' is returned.
1430 If they are in the header line of WINDOW, `header-line' is returned.
1431 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1432 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1433 If they are on the border between WINDOW and its right sibling,
1434 `vertical-line' is returned.
1435 If they are in the windows's left or right marginal areas, `left-margin'\n\
1436 or `right-margin' is returned. */)
1437 (register Lisp_Object coordinates, Lisp_Object window)
1439 struct window *w;
1440 struct frame *f;
1441 int x, y;
1442 Lisp_Object lx, ly;
1444 w = decode_live_window (window);
1445 f = XFRAME (w->frame);
1446 CHECK_CONS (coordinates);
1447 lx = Fcar (coordinates);
1448 ly = Fcdr (coordinates);
1449 CHECK_NUMBER_OR_FLOAT (lx);
1450 CHECK_NUMBER_OR_FLOAT (ly);
1451 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1452 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1454 switch (coordinates_in_window (w, x, y))
1456 case ON_NOTHING:
1457 return Qnil;
1459 case ON_TEXT:
1460 /* Convert X and Y to window relative pixel coordinates, and
1461 return the canonical char units. */
1462 x -= window_box_left (w, TEXT_AREA);
1463 y -= WINDOW_TOP_EDGE_Y (w);
1464 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1465 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1467 case ON_MODE_LINE:
1468 return Qmode_line;
1470 case ON_VERTICAL_BORDER:
1471 return Qvertical_line;
1473 case ON_HEADER_LINE:
1474 return Qheader_line;
1476 case ON_LEFT_FRINGE:
1477 return Qleft_fringe;
1479 case ON_RIGHT_FRINGE:
1480 return Qright_fringe;
1482 case ON_LEFT_MARGIN:
1483 return Qleft_margin;
1485 case ON_RIGHT_MARGIN:
1486 return Qright_margin;
1488 case ON_SCROLL_BAR:
1489 /* Historically we are supposed to return nil in this case. */
1490 return Qnil;
1492 case ON_RIGHT_DIVIDER:
1493 return Qright_divider;
1495 case ON_BOTTOM_DIVIDER:
1496 return Qbottom_divider;
1498 default:
1499 emacs_abort ();
1504 /* Callback for foreach_window, used in window_from_coordinates.
1505 Check if window W contains coordinates specified by USER_DATA which
1506 is actually a pointer to a struct check_window_data CW.
1508 Check if window W contains coordinates *CW->x and *CW->y. If it
1509 does, return W in *CW->window, as Lisp_Object, and return in
1510 *CW->part the part of the window under coordinates *X,*Y. Return
1511 zero from this function to stop iterating over windows. */
1513 struct check_window_data
1515 Lisp_Object *window;
1516 int x, y;
1517 enum window_part *part;
1520 static int
1521 check_window_containing (struct window *w, void *user_data)
1523 struct check_window_data *cw = user_data;
1524 enum window_part found;
1525 int continue_p = 1;
1527 found = coordinates_in_window (w, cw->x, cw->y);
1528 if (found != ON_NOTHING)
1530 *cw->part = found;
1531 XSETWINDOW (*cw->window, w);
1532 continue_p = 0;
1535 return continue_p;
1539 /* Find the window containing frame-relative pixel position X/Y and
1540 return it as a Lisp_Object.
1542 If X, Y is on one of the window's special `window_part' elements,
1543 set *PART to the id of that element.
1545 If there is no window under X, Y return nil and leave *PART
1546 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1548 This function was previously implemented with a loop cycling over
1549 windows with Fnext_window, and starting with the frame's selected
1550 window. It turned out that this doesn't work with an
1551 implementation of next_window using Vwindow_list, because
1552 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1553 tree of F when this function is called asynchronously from
1554 note_mouse_highlight. The original loop didn't terminate in this
1555 case. */
1557 Lisp_Object
1558 window_from_coordinates (struct frame *f, int x, int y,
1559 enum window_part *part, bool tool_bar_p)
1561 Lisp_Object window;
1562 struct check_window_data cw;
1563 enum window_part dummy;
1565 if (part == 0)
1566 part = &dummy;
1568 window = Qnil;
1569 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1570 foreach_window (f, check_window_containing, &cw);
1572 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1573 /* If not found above, see if it's in the tool bar window, if a tool
1574 bar exists. */
1575 if (NILP (window)
1576 && tool_bar_p
1577 && WINDOWP (f->tool_bar_window)
1578 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1579 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1580 != ON_NOTHING))
1582 *part = ON_TEXT;
1583 window = f->tool_bar_window;
1585 #endif
1587 return window;
1590 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1591 doc: /* Return window containing coordinates X and Y on FRAME.
1592 FRAME must be a live frame and defaults to the selected one.
1593 The top left corner of the frame is considered to be row 0,
1594 column 0. */)
1595 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1597 struct frame *f = decode_live_frame (frame);
1599 /* Check that arguments are integers or floats. */
1600 CHECK_NUMBER_OR_FLOAT (x);
1601 CHECK_NUMBER_OR_FLOAT (y);
1603 return window_from_coordinates (f,
1604 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1605 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1606 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1607 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1608 0, 0);
1611 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1612 doc: /* Return current value of point in WINDOW.
1613 WINDOW must be a live window and defaults to the selected one.
1615 For a nonselected window, this is the value point would have if that
1616 window were selected.
1618 Note that, when WINDOW is selected, the value returned is the same as
1619 that returned by `point' for WINDOW's buffer. It would be more strictly
1620 correct to return the top-level value of `point', outside of any
1621 `save-excursion' forms. But that is hard to define. */)
1622 (Lisp_Object window)
1624 register struct window *w = decode_live_window (window);
1626 if (w == XWINDOW (selected_window))
1627 return make_number (BUF_PT (XBUFFER (w->contents)));
1628 else
1629 return Fmarker_position (w->pointm);
1632 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1633 doc: /* Return position at which display currently starts in WINDOW.
1634 WINDOW must be a live window and defaults to the selected one.
1635 This is updated by redisplay or by calling `set-window-start'. */)
1636 (Lisp_Object window)
1638 return Fmarker_position (decode_live_window (window)->start);
1641 /* This is text temporarily removed from the doc string below.
1643 This function returns nil if the position is not currently known.
1644 That happens when redisplay is preempted and doesn't finish.
1645 If in that case you want to compute where the end of the window would
1646 have been if redisplay had finished, do this:
1647 (save-excursion
1648 (goto-char (window-start window))
1649 (vertical-motion (1- (window-height window)) window)
1650 (point))") */
1652 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1653 doc: /* Return position at which display currently ends in WINDOW.
1654 WINDOW must be a live window and defaults to the selected one.
1655 This is updated by redisplay, when it runs to completion.
1656 Simply changing the buffer text or setting `window-start'
1657 does not update this value.
1658 Return nil if there is no recorded value. (This can happen if the
1659 last redisplay of WINDOW was preempted, and did not finish.)
1660 If UPDATE is non-nil, compute the up-to-date position
1661 if it isn't already recorded. */)
1662 (Lisp_Object window, Lisp_Object update)
1664 Lisp_Object value;
1665 struct window *w = decode_live_window (window);
1666 Lisp_Object buf;
1667 struct buffer *b;
1669 buf = w->contents;
1670 CHECK_BUFFER (buf);
1671 b = XBUFFER (buf);
1673 if (! NILP (update)
1674 && (windows_or_buffers_changed
1675 || !w->window_end_valid
1676 || b->clip_changed
1677 || b->prevent_redisplay_optimizations_p
1678 || window_outdated (w))
1679 && !noninteractive)
1681 struct text_pos startp;
1682 struct it it;
1683 struct buffer *old_buffer = NULL;
1684 void *itdata = NULL;
1686 /* Cannot use Fvertical_motion because that function doesn't
1687 cope with variable-height lines. */
1688 if (b != current_buffer)
1690 old_buffer = current_buffer;
1691 set_buffer_internal (b);
1694 /* In case W->start is out of the range, use something
1695 reasonable. This situation occurred when loading a file with
1696 `-l' containing a call to `rmail' with subsequent other
1697 commands. At the end, W->start happened to be BEG, while
1698 rmail had already narrowed the buffer. */
1699 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1701 itdata = bidi_shelve_cache ();
1702 start_display (&it, w, startp);
1703 move_it_vertically (&it, window_box_height (w));
1704 if (it.current_y < it.last_visible_y)
1705 move_it_past_eol (&it);
1706 value = make_number (IT_CHARPOS (it));
1707 bidi_unshelve_cache (itdata, 0);
1709 if (old_buffer)
1710 set_buffer_internal (old_buffer);
1712 else
1713 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1715 return value;
1718 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1719 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1720 WINDOW must be a live window and defaults to the selected one.
1721 Return POS. */)
1722 (Lisp_Object window, Lisp_Object pos)
1724 register struct window *w = decode_live_window (window);
1726 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1728 if (w == XWINDOW (selected_window))
1730 if (XBUFFER (w->contents) == current_buffer)
1731 Fgoto_char (pos);
1732 else
1734 struct buffer *old_buffer = current_buffer;
1736 /* ... but here we want to catch type error before buffer change. */
1737 CHECK_NUMBER_COERCE_MARKER (pos);
1738 set_buffer_internal (XBUFFER (w->contents));
1739 Fgoto_char (pos);
1740 set_buffer_internal (old_buffer);
1743 else
1745 set_marker_restricted (w->pointm, pos, w->contents);
1746 /* We have to make sure that redisplay updates the window to show
1747 the new value of point. */
1748 wset_redisplay (w);
1751 return pos;
1754 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1755 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1756 WINDOW must be a live window and defaults to the selected one. Return
1757 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1758 overriding motion of point in order to display at this exact start. */)
1759 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1761 register struct window *w = decode_live_window (window);
1763 set_marker_restricted (w->start, pos, w->contents);
1764 /* This is not right, but much easier than doing what is right. */
1765 w->start_at_line_beg = 0;
1766 if (NILP (noforce))
1767 w->force_start = 1;
1768 w->update_mode_line = 1;
1769 /* Bug#15957. */
1770 w->window_end_valid = 0;
1771 wset_redisplay (w);
1773 return pos;
1776 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1777 Spos_visible_in_window_p, 0, 3, 0,
1778 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1779 WINDOW must be a live window and defaults to the selected one.
1781 Return nil if that position is scrolled vertically out of view. If a
1782 character is only partially visible, nil is returned, unless the
1783 optional argument PARTIALLY is non-nil. If POS is only out of view
1784 because of horizontal scrolling, return non-nil. If POS is t, it
1785 specifies the position of the last visible glyph in WINDOW. POS
1786 defaults to point in WINDOW; WINDOW defaults to the selected window.
1788 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1789 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1790 where X and Y are the pixel coordinates relative to the top left corner
1791 of the window. The remaining elements are omitted if the character after
1792 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1793 off-window at the top and bottom of the screen line ("row") containing
1794 POS, ROWH is the visible height of that row, and VPOS is the row number
1795 \(zero-based). */)
1796 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1798 register struct window *w;
1799 register EMACS_INT posint;
1800 register struct buffer *buf;
1801 struct text_pos top;
1802 Lisp_Object in_window = Qnil;
1803 int rtop, rbot, rowh, vpos, fully_p = 1;
1804 int x, y;
1806 w = decode_live_window (window);
1807 buf = XBUFFER (w->contents);
1808 SET_TEXT_POS_FROM_MARKER (top, w->start);
1810 if (EQ (pos, Qt))
1811 posint = -1;
1812 else if (!NILP (pos))
1814 CHECK_NUMBER_COERCE_MARKER (pos);
1815 posint = XINT (pos);
1817 else if (w == XWINDOW (selected_window))
1818 posint = PT;
1819 else
1820 posint = marker_position (w->pointm);
1822 /* If position is above window start or outside buffer boundaries,
1823 or if window start is out of range, position is not visible. */
1824 if ((EQ (pos, Qt)
1825 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1826 && CHARPOS (top) >= BUF_BEGV (buf)
1827 && CHARPOS (top) <= BUF_ZV (buf)
1828 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1829 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1830 in_window = Qt;
1832 if (!NILP (in_window) && !NILP (partially))
1834 Lisp_Object part = Qnil;
1835 if (!fully_p)
1836 part = list4i (rtop, rbot, rowh, vpos);
1837 in_window = Fcons (make_number (x),
1838 Fcons (make_number (y), part));
1841 return in_window;
1844 DEFUN ("window-line-height", Fwindow_line_height,
1845 Swindow_line_height, 0, 2, 0,
1846 doc: /* Return height in pixels of text line LINE in window WINDOW.
1847 WINDOW must be a live window and defaults to the selected one.
1849 Return height of current line if LINE is omitted or nil. Return height of
1850 header or mode line if LINE is `header-line' or `mode-line'.
1851 Otherwise, LINE is a text line number starting from 0. A negative number
1852 counts from the end of the window.
1854 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1855 in pixels of the visible part of the line, VPOS and YPOS are the
1856 vertical position in lines and pixels of the line, relative to the top
1857 of the first text line, and OFFBOT is the number of off-window pixels at
1858 the bottom of the text line. If there are off-window pixels at the top
1859 of the (first) text line, YPOS is negative.
1861 Return nil if window display is not up-to-date. In that case, use
1862 `pos-visible-in-window-p' to obtain the information. */)
1863 (Lisp_Object line, Lisp_Object window)
1865 register struct window *w;
1866 register struct buffer *b;
1867 struct glyph_row *row, *end_row;
1868 int max_y, crop, i;
1869 EMACS_INT n;
1871 w = decode_live_window (window);
1873 if (noninteractive || w->pseudo_window_p)
1874 return Qnil;
1876 CHECK_BUFFER (w->contents);
1877 b = XBUFFER (w->contents);
1879 /* Fail if current matrix is not up-to-date. */
1880 if (!w->window_end_valid
1881 || windows_or_buffers_changed
1882 || b->clip_changed
1883 || b->prevent_redisplay_optimizations_p
1884 || window_outdated (w))
1885 return Qnil;
1887 if (NILP (line))
1889 i = w->cursor.vpos;
1890 if (i < 0 || i >= w->current_matrix->nrows
1891 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1892 return Qnil;
1893 max_y = window_text_bottom_y (w);
1894 goto found_row;
1897 if (EQ (line, Qheader_line))
1899 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1900 return Qnil;
1901 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1902 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1905 if (EQ (line, Qmode_line))
1907 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1908 return (row->enabled_p ?
1909 list4i (row->height,
1910 0, /* not accurate */
1911 (WINDOW_HEADER_LINE_HEIGHT (w)
1912 + window_text_bottom_y (w)),
1914 : Qnil);
1917 CHECK_NUMBER (line);
1918 n = XINT (line);
1920 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1921 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1922 max_y = window_text_bottom_y (w);
1923 i = 0;
1925 while ((n < 0 || i < n)
1926 && row <= end_row && row->enabled_p
1927 && row->y + row->height < max_y)
1928 row++, i++;
1930 if (row > end_row || !row->enabled_p)
1931 return Qnil;
1933 if (++n < 0)
1935 if (-n > i)
1936 return Qnil;
1937 row += n;
1938 i += n;
1941 found_row:
1942 crop = max (0, (row->y + row->height) - max_y);
1943 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1946 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1947 0, 1, 0,
1948 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1949 More precisely, return the value assigned by the last call of
1950 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1951 never called with WINDOW as its argument, or the value set by that
1952 function was internally reset since its last call. WINDOW must be a
1953 live window and defaults to the selected one.
1955 When a window is dedicated to its buffer, `display-buffer' will refrain
1956 from displaying another buffer in it. `get-lru-window' and
1957 `get-largest-window' treat dedicated windows specially.
1958 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1959 `kill-buffer' can delete a dedicated window and the containing frame.
1961 Functions like `set-window-buffer' may change the buffer displayed by a
1962 window, unless that window is "strongly" dedicated to its buffer, that
1963 is the value returned by `window-dedicated-p' is t. */)
1964 (Lisp_Object window)
1966 return decode_live_window (window)->dedicated;
1969 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1970 Sset_window_dedicated_p, 2, 2, 0,
1971 doc: /* Mark WINDOW as dedicated according to FLAG.
1972 WINDOW must be a live window and defaults to the selected one. FLAG
1973 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1974 mark WINDOW as non-dedicated. Return FLAG.
1976 When a window is dedicated to its buffer, `display-buffer' will refrain
1977 from displaying another buffer in it. `get-lru-window' and
1978 `get-largest-window' treat dedicated windows specially.
1979 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1980 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1981 and the containing frame.
1983 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1984 its buffer. Functions like `set-window-buffer' may change the buffer
1985 displayed by a window, unless that window is strongly dedicated to its
1986 buffer. If and when `set-window-buffer' displays another buffer in a
1987 window, it also makes sure that the window is no more dedicated. */)
1988 (Lisp_Object window, Lisp_Object flag)
1990 wset_dedicated (decode_live_window (window), flag);
1991 return flag;
1994 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1995 0, 1, 0,
1996 doc: /* Return buffers previously shown in WINDOW.
1997 WINDOW must be a live window and defaults to the selected one.
1999 The return value is a list of elements (BUFFER WINDOW-START POS),
2000 where BUFFER is a buffer, WINDOW-START is the start position of the
2001 window for that buffer, and POS is a window-specific point value. */)
2002 (Lisp_Object window)
2004 return decode_live_window (window)->prev_buffers;
2007 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2008 Sset_window_prev_buffers, 2, 2, 0,
2009 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2010 WINDOW must be a live window and defaults to the selected one.
2012 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2013 where BUFFER is a buffer, WINDOW-START is the start position of the
2014 window for that buffer, and POS is a window-specific point value. */)
2015 (Lisp_Object window, Lisp_Object prev_buffers)
2017 wset_prev_buffers (decode_live_window (window), prev_buffers);
2018 return prev_buffers;
2021 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2022 0, 1, 0,
2023 doc: /* Return list of buffers recently re-shown in WINDOW.
2024 WINDOW must be a live window and defaults to the selected one. */)
2025 (Lisp_Object window)
2027 return decode_live_window (window)->next_buffers;
2030 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2031 Sset_window_next_buffers, 2, 2, 0,
2032 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2033 WINDOW must be a live window and defaults to the selected one.
2034 NEXT-BUFFERS should be a list of buffers. */)
2035 (Lisp_Object window, Lisp_Object next_buffers)
2037 wset_next_buffers (decode_live_window (window), next_buffers);
2038 return next_buffers;
2041 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2042 0, 1, 0,
2043 doc: /* Return the parameters of WINDOW and their values.
2044 WINDOW must be a valid window and defaults to the selected one. The
2045 return value is a list of elements of the form (PARAMETER . VALUE). */)
2046 (Lisp_Object window)
2048 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2051 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2052 2, 2, 0,
2053 doc: /* Return WINDOW's value for PARAMETER.
2054 WINDOW can be any window and defaults to the selected one. */)
2055 (Lisp_Object window, Lisp_Object parameter)
2057 Lisp_Object result;
2059 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2060 return CDR_SAFE (result);
2063 DEFUN ("set-window-parameter", Fset_window_parameter,
2064 Sset_window_parameter, 3, 3, 0,
2065 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2066 WINDOW can be any window and defaults to the selected one.
2067 Return VALUE. */)
2068 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2070 register struct window *w = decode_any_window (window);
2071 Lisp_Object old_alist_elt;
2073 old_alist_elt = Fassq (parameter, w->window_parameters);
2074 if (NILP (old_alist_elt))
2075 wset_window_parameters
2076 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2077 else
2078 Fsetcdr (old_alist_elt, value);
2079 return value;
2082 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2083 0, 1, 0,
2084 doc: /* Return the display-table that WINDOW is using.
2085 WINDOW must be a live window and defaults to the selected one. */)
2086 (Lisp_Object window)
2088 return decode_live_window (window)->display_table;
2091 /* Get the display table for use on window W. This is either W's
2092 display table or W's buffer's display table. Ignore the specified
2093 tables if they are not valid; if no valid table is specified,
2094 return 0. */
2096 struct Lisp_Char_Table *
2097 window_display_table (struct window *w)
2099 struct Lisp_Char_Table *dp = NULL;
2101 if (DISP_TABLE_P (w->display_table))
2102 dp = XCHAR_TABLE (w->display_table);
2103 else if (BUFFERP (w->contents))
2105 struct buffer *b = XBUFFER (w->contents);
2107 if (DISP_TABLE_P (BVAR (b, display_table)))
2108 dp = XCHAR_TABLE (BVAR (b, display_table));
2109 else if (DISP_TABLE_P (Vstandard_display_table))
2110 dp = XCHAR_TABLE (Vstandard_display_table);
2113 return dp;
2116 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2117 doc: /* Set WINDOW's display-table to TABLE.
2118 WINDOW must be a live window and defaults to the selected one. */)
2119 (register Lisp_Object window, Lisp_Object table)
2121 wset_display_table (decode_live_window (window), table);
2122 return table;
2125 /* Record info on buffer window W is displaying
2126 when it is about to cease to display that buffer. */
2127 static void
2128 unshow_buffer (register struct window *w)
2130 Lisp_Object buf = w->contents;
2131 struct buffer *b = XBUFFER (buf);
2133 eassert (b == XMARKER (w->pointm)->buffer);
2135 #if 0
2136 if (w == XWINDOW (selected_window)
2137 || ! EQ (buf, XWINDOW (selected_window)->contents))
2138 /* Do this except when the selected window's buffer
2139 is being removed from some other window. */
2140 #endif
2141 /* last_window_start records the start position that this buffer
2142 had in the last window to be disconnected from it.
2143 Now that this statement is unconditional,
2144 it is possible for the buffer to be displayed in the
2145 selected window, while last_window_start reflects another
2146 window which was recently showing the same buffer.
2147 Some people might say that might be a good thing. Let's see. */
2148 b->last_window_start = marker_position (w->start);
2150 /* Point in the selected window's buffer
2151 is actually stored in that buffer, and the window's pointm isn't used.
2152 So don't clobber point in that buffer. */
2153 if (! EQ (buf, XWINDOW (selected_window)->contents)
2154 /* Don't clobber point in current buffer either (this could be
2155 useful in connection with bug#12208).
2156 && XBUFFER (buf) != current_buffer */
2157 /* This line helps to fix Horsley's testbug.el bug. */
2158 && !(WINDOWP (BVAR (b, last_selected_window))
2159 && w != XWINDOW (BVAR (b, last_selected_window))
2160 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2161 temp_set_point_both (b,
2162 clip_to_bounds (BUF_BEGV (b),
2163 marker_position (w->pointm),
2164 BUF_ZV (b)),
2165 clip_to_bounds (BUF_BEGV_BYTE (b),
2166 marker_byte_position (w->pointm),
2167 BUF_ZV_BYTE (b)));
2169 if (WINDOWP (BVAR (b, last_selected_window))
2170 && w == XWINDOW (BVAR (b, last_selected_window)))
2171 bset_last_selected_window (b, Qnil);
2174 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2175 means change window structure only. Otherwise store geometry and
2176 other settings as well. */
2177 static void
2178 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2180 register Lisp_Object tem;
2181 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2183 /* If OLD is its frame's root window, then NEW is the new
2184 root window for that frame. */
2185 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2186 fset_root_window (XFRAME (o->frame), new);
2188 if (setflag)
2190 n->pixel_left = o->pixel_left;
2191 n->pixel_top = o->pixel_top;
2192 n->pixel_width = o->pixel_width;
2193 n->pixel_height = o->pixel_height;
2194 n->left_col = o->left_col;
2195 n->top_line = o->top_line;
2196 n->total_cols = o->total_cols;
2197 n->total_lines = o->total_lines;
2198 wset_normal_cols (n, o->normal_cols);
2199 wset_normal_cols (o, make_float (1.0));
2200 wset_normal_lines (n, o->normal_lines);
2201 wset_normal_lines (o, make_float (1.0));
2202 n->desired_matrix = n->current_matrix = 0;
2203 n->vscroll = 0;
2204 memset (&n->cursor, 0, sizeof (n->cursor));
2205 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2206 n->last_cursor_vpos = 0;
2207 #ifdef HAVE_WINDOW_SYSTEM
2208 n->phys_cursor_type = NO_CURSOR;
2209 n->phys_cursor_width = -1;
2210 #endif
2211 n->must_be_updated_p = 0;
2212 n->pseudo_window_p = 0;
2213 n->window_end_vpos = 0;
2214 n->window_end_pos = 0;
2215 n->window_end_valid = 0;
2218 tem = o->next;
2219 wset_next (n, tem);
2220 if (!NILP (tem))
2221 wset_prev (XWINDOW (tem), new);
2223 tem = o->prev;
2224 wset_prev (n, tem);
2225 if (!NILP (tem))
2226 wset_next (XWINDOW (tem), new);
2228 tem = o->parent;
2229 wset_parent (n, tem);
2230 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2231 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2234 /* If window WINDOW and its parent window are iso-combined, merge
2235 WINDOW's children into those of its parent window and mark WINDOW as
2236 deleted. */
2238 static void
2239 recombine_windows (Lisp_Object window)
2241 struct window *w, *p, *c;
2242 Lisp_Object parent, child;
2243 bool horflag;
2245 w = XWINDOW (window);
2246 parent = w->parent;
2247 if (!NILP (parent) && NILP (w->combination_limit))
2249 p = XWINDOW (parent);
2250 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2251 && p->horizontal == w->horizontal)
2252 /* WINDOW and PARENT are both either a vertical or a horizontal
2253 combination. */
2255 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2256 child = w->contents;
2257 c = XWINDOW (child);
2259 /* Splice WINDOW's children into its parent's children and
2260 assign new normal sizes. */
2261 if (NILP (w->prev))
2262 wset_combination (p, horflag, child);
2263 else
2265 wset_prev (c, w->prev);
2266 wset_next (XWINDOW (w->prev), child);
2269 while (c)
2271 wset_parent (c, parent);
2273 if (horflag)
2274 wset_normal_cols
2275 (c, make_float ((double) c->pixel_width
2276 / (double) p->pixel_width));
2277 else
2278 wset_normal_lines
2279 (c, make_float ((double) c->pixel_height
2280 / (double) p->pixel_height));
2282 if (NILP (c->next))
2284 if (!NILP (w->next))
2286 wset_next (c, w->next);
2287 wset_prev (XWINDOW (c->next), child);
2290 c = 0;
2292 else
2294 child = c->next;
2295 c = XWINDOW (child);
2299 /* WINDOW can be deleted now. */
2300 wset_combination (w, 0, Qnil);
2305 /* If WINDOW can be deleted, delete it. */
2306 static void
2307 delete_deletable_window (Lisp_Object window)
2309 if (!NILP (call1 (Qwindow_deletable_p, window)))
2310 call1 (Qdelete_window, window);
2313 /***********************************************************************
2314 Window List
2315 ***********************************************************************/
2317 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2318 pointer. This is a callback function for foreach_window, used in
2319 the window_list function. */
2321 static int
2322 add_window_to_list (struct window *w, void *user_data)
2324 Lisp_Object *list = user_data;
2325 Lisp_Object window;
2326 XSETWINDOW (window, w);
2327 *list = Fcons (window, *list);
2328 return 1;
2332 /* Return a list of all windows, for use by next_window. If
2333 Vwindow_list is a list, return that list. Otherwise, build a new
2334 list, cache it in Vwindow_list, and return that. */
2336 Lisp_Object
2337 window_list (void)
2339 if (!CONSP (Vwindow_list))
2341 Lisp_Object tail, frame;
2343 Vwindow_list = Qnil;
2344 FOR_EACH_FRAME (tail, frame)
2346 Lisp_Object args[2];
2348 /* We are visiting windows in canonical order, and add
2349 new windows at the front of args[1], which means we
2350 have to reverse this list at the end. */
2351 args[1] = Qnil;
2352 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2353 args[0] = Vwindow_list;
2354 args[1] = Fnreverse (args[1]);
2355 Vwindow_list = Fnconc (2, args);
2359 return Vwindow_list;
2363 /* Value is non-zero if WINDOW satisfies the constraints given by
2364 OWINDOW, MINIBUF and ALL_FRAMES.
2366 MINIBUF t means WINDOW may be minibuffer windows.
2367 `lambda' means WINDOW may not be a minibuffer window.
2368 a window means a specific minibuffer window
2370 ALL_FRAMES t means search all frames,
2371 nil means search just current frame,
2372 `visible' means search just visible frames on the
2373 current terminal,
2374 0 means search visible and iconified frames on the
2375 current terminal,
2376 a window means search the frame that window belongs to,
2377 a frame means consider windows on that frame, only. */
2379 static bool
2380 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2381 Lisp_Object minibuf, Lisp_Object all_frames)
2383 struct window *w = XWINDOW (window);
2384 struct frame *f = XFRAME (w->frame);
2385 bool candidate_p = 1;
2387 if (!BUFFERP (w->contents))
2388 candidate_p = 0;
2389 else if (MINI_WINDOW_P (w)
2390 && (EQ (minibuf, Qlambda)
2391 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2393 /* If MINIBUF is `lambda' don't consider any mini-windows.
2394 If it is a window, consider only that one. */
2395 candidate_p = 0;
2397 else if (EQ (all_frames, Qt))
2398 candidate_p = 1;
2399 else if (NILP (all_frames))
2401 eassert (WINDOWP (owindow));
2402 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2404 else if (EQ (all_frames, Qvisible))
2406 candidate_p = FRAME_VISIBLE_P (f)
2407 && (FRAME_TERMINAL (XFRAME (w->frame))
2408 == FRAME_TERMINAL (XFRAME (selected_frame)));
2411 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2413 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2414 #ifdef HAVE_X_WINDOWS
2415 /* Yuck!! If we've just created the frame and the
2416 window-manager requested the user to place it
2417 manually, the window may still not be considered
2418 `visible'. I'd argue it should be at least
2419 something like `iconified', but don't know how to do
2420 that yet. --Stef */
2421 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2422 && !f->output_data.x->has_been_visible)
2423 #endif
2425 && (FRAME_TERMINAL (XFRAME (w->frame))
2426 == FRAME_TERMINAL (XFRAME (selected_frame)));
2428 else if (WINDOWP (all_frames))
2429 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2430 || EQ (XWINDOW (all_frames)->frame, w->frame)
2431 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2432 else if (FRAMEP (all_frames))
2433 candidate_p = EQ (all_frames, w->frame);
2435 return candidate_p;
2439 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2440 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2441 MINIBUF, and ALL_FRAMES. */
2443 static void
2444 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2446 struct window *w = decode_live_window (*window);
2448 XSETWINDOW (*window, w);
2449 /* MINIBUF nil may or may not include minibuffers. Decide if it
2450 does. */
2451 if (NILP (*minibuf))
2452 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2453 else if (!EQ (*minibuf, Qt))
2454 *minibuf = Qlambda;
2456 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2457 => count none of them, or a specific minibuffer window (the
2458 active one) to count. */
2460 /* ALL_FRAMES nil doesn't specify which frames to include. */
2461 if (NILP (*all_frames))
2462 *all_frames
2463 = (!EQ (*minibuf, Qlambda)
2464 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2465 : Qnil);
2466 else if (EQ (*all_frames, Qvisible))
2468 else if (EQ (*all_frames, make_number (0)))
2470 else if (FRAMEP (*all_frames))
2472 else if (!EQ (*all_frames, Qt))
2473 *all_frames = Qnil;
2477 /* Return the next or previous window of WINDOW in cyclic ordering
2478 of windows. NEXT_P non-zero means return the next window. See the
2479 documentation string of next-window for the meaning of MINIBUF and
2480 ALL_FRAMES. */
2482 static Lisp_Object
2483 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2485 decode_next_window_args (&window, &minibuf, &all_frames);
2487 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2488 return the first window on the frame. */
2489 if (FRAMEP (all_frames)
2490 && !EQ (all_frames, XWINDOW (window)->frame))
2491 return Fframe_first_window (all_frames);
2493 if (next_p)
2495 Lisp_Object list;
2497 /* Find WINDOW in the list of all windows. */
2498 list = Fmemq (window, window_list ());
2500 /* Scan forward from WINDOW to the end of the window list. */
2501 if (CONSP (list))
2502 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2503 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2504 break;
2506 /* Scan from the start of the window list up to WINDOW. */
2507 if (!CONSP (list))
2508 for (list = Vwindow_list;
2509 CONSP (list) && !EQ (XCAR (list), window);
2510 list = XCDR (list))
2511 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2512 break;
2514 if (CONSP (list))
2515 window = XCAR (list);
2517 else
2519 Lisp_Object candidate, list;
2521 /* Scan through the list of windows for candidates. If there are
2522 candidate windows in front of WINDOW, the last one of these
2523 is the one we want. If there are candidates following WINDOW
2524 in the list, again the last one of these is the one we want. */
2525 candidate = Qnil;
2526 for (list = window_list (); CONSP (list); list = XCDR (list))
2528 if (EQ (XCAR (list), window))
2530 if (WINDOWP (candidate))
2531 break;
2533 else if (candidate_window_p (XCAR (list), window, minibuf,
2534 all_frames))
2535 candidate = XCAR (list);
2538 if (WINDOWP (candidate))
2539 window = candidate;
2542 return window;
2546 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2547 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2548 WINDOW must be a live window and defaults to the selected one. The
2549 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2550 consider.
2552 MINIBUF nil or omitted means consider the minibuffer window only if the
2553 minibuffer is active. MINIBUF t means consider the minibuffer window
2554 even if the minibuffer is not active. Any other value means do not
2555 consider the minibuffer window even if the minibuffer is active.
2557 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2558 plus the minibuffer window if specified by the MINIBUF argument. If the
2559 minibuffer counts, consider all windows on all frames that share that
2560 minibuffer too. The following non-nil values of ALL-FRAMES have special
2561 meanings:
2563 - t means consider all windows on all existing frames.
2565 - `visible' means consider all windows on all visible frames.
2567 - 0 (the number zero) means consider all windows on all visible and
2568 iconified frames.
2570 - A frame means consider all windows on that frame only.
2572 Anything else means consider all windows on WINDOW's frame and no
2573 others.
2575 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2576 `next-window' to iterate through the entire cycle of acceptable
2577 windows, eventually ending up back at the window you started with.
2578 `previous-window' traverses the same cycle, in the reverse order. */)
2579 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2581 return next_window (window, minibuf, all_frames, 1);
2585 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2586 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2587 WINDOW must be a live window and defaults to the selected one. The
2588 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2589 consider.
2591 MINIBUF nil or omitted means consider the minibuffer window only if the
2592 minibuffer is active. MINIBUF t means consider the minibuffer window
2593 even if the minibuffer is not active. Any other value means do not
2594 consider the minibuffer window even if the minibuffer is active.
2596 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2597 plus the minibuffer window if specified by the MINIBUF argument. If the
2598 minibuffer counts, consider all windows on all frames that share that
2599 minibuffer too. The following non-nil values of ALL-FRAMES have special
2600 meanings:
2602 - t means consider all windows on all existing frames.
2604 - `visible' means consider all windows on all visible frames.
2606 - 0 (the number zero) means consider all windows on all visible and
2607 iconified frames.
2609 - A frame means consider all windows on that frame only.
2611 Anything else means consider all windows on WINDOW's frame and no
2612 others.
2614 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2615 use `previous-window' to iterate through the entire cycle of
2616 acceptable windows, eventually ending up back at the window you
2617 started with. `next-window' traverses the same cycle, in the
2618 reverse order. */)
2619 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2621 return next_window (window, minibuf, all_frames, 0);
2625 /* Return a list of windows in cyclic ordering. Arguments are like
2626 for `next-window'. */
2628 static Lisp_Object
2629 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2631 Lisp_Object tail, list, rest;
2633 decode_next_window_args (&window, &minibuf, &all_frames);
2634 list = Qnil;
2636 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2637 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2638 list = Fcons (XCAR (tail), list);
2640 /* Rotate the list to start with WINDOW. */
2641 list = Fnreverse (list);
2642 rest = Fmemq (window, list);
2643 if (!NILP (rest) && !EQ (rest, list))
2645 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2647 XSETCDR (tail, Qnil);
2648 list = nconc2 (rest, list);
2650 return list;
2654 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2655 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2656 FRAME nil or omitted means use the selected frame.
2657 WINDOW nil or omitted means use the window selected within FRAME.
2658 MINIBUF t means include the minibuffer window, even if it isn't active.
2659 MINIBUF nil or omitted means include the minibuffer window only
2660 if it's active.
2661 MINIBUF neither nil nor t means never include the minibuffer window. */)
2662 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2664 if (NILP (window))
2665 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2666 CHECK_WINDOW (window);
2667 if (NILP (frame))
2668 frame = selected_frame;
2670 if (!EQ (frame, XWINDOW (window)->frame))
2671 error ("Window is on a different frame");
2673 return window_list_1 (window, minibuf, frame);
2677 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2678 doc: /* Return a list of all live windows.
2679 WINDOW specifies the first window to list and defaults to the selected
2680 window.
2682 Optional argument MINIBUF nil or omitted means consider the minibuffer
2683 window only if the minibuffer is active. MINIBUF t means consider the
2684 minibuffer window even if the minibuffer is not active. Any other value
2685 means do not consider the minibuffer window even if the minibuffer is
2686 active.
2688 Optional argument ALL-FRAMES nil or omitted means consider all windows
2689 on WINDOW's frame, plus the minibuffer window if specified by the
2690 MINIBUF argument. If the minibuffer counts, consider all windows on all
2691 frames that share that minibuffer too. The following non-nil values of
2692 ALL-FRAMES have special meanings:
2694 - t means consider all windows on all existing frames.
2696 - `visible' means consider all windows on all visible frames.
2698 - 0 (the number zero) means consider all windows on all visible and
2699 iconified frames.
2701 - A frame means consider all windows on that frame only.
2703 Anything else means consider all windows on WINDOW's frame and no
2704 others.
2706 If WINDOW is not on the list of windows returned, some other window will
2707 be listed first but no error is signaled. */)
2708 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2710 return window_list_1 (window, minibuf, all_frames);
2713 /* Look at all windows, performing an operation specified by TYPE
2714 with argument OBJ.
2715 If FRAMES is Qt, look at all frames;
2716 Qnil, look at just the selected frame;
2717 Qvisible, look at visible frames;
2718 a frame, just look at windows on that frame.
2719 If MINI is non-zero, perform the operation on minibuffer windows too. */
2721 enum window_loop
2723 WINDOW_LOOP_UNUSED,
2724 GET_BUFFER_WINDOW, /* Arg is buffer */
2725 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2726 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2727 CHECK_ALL_WINDOWS /* Arg is ignored */
2730 static Lisp_Object
2731 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2733 Lisp_Object window, windows, best_window, frame_arg;
2734 int frame_best_window_flag = 0;
2735 struct frame *f;
2736 struct gcpro gcpro1;
2738 /* If we're only looping through windows on a particular frame,
2739 frame points to that frame. If we're looping through windows
2740 on all frames, frame is 0. */
2741 if (FRAMEP (frames))
2742 f = XFRAME (frames);
2743 else if (NILP (frames))
2744 f = SELECTED_FRAME ();
2745 else
2746 f = NULL;
2748 if (f)
2749 frame_arg = Qlambda;
2750 else if (EQ (frames, make_number (0)))
2751 frame_arg = frames;
2752 else if (EQ (frames, Qvisible))
2753 frame_arg = frames;
2754 else
2755 frame_arg = Qt;
2757 /* frame_arg is Qlambda to stick to one frame,
2758 Qvisible to consider all visible frames,
2759 or Qt otherwise. */
2761 /* Pick a window to start with. */
2762 if (WINDOWP (obj))
2763 window = obj;
2764 else if (f)
2765 window = FRAME_SELECTED_WINDOW (f);
2766 else
2767 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2769 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2770 GCPRO1 (windows);
2771 best_window = Qnil;
2773 for (; CONSP (windows); windows = XCDR (windows))
2775 struct window *w;
2777 window = XCAR (windows);
2778 w = XWINDOW (window);
2780 /* Note that we do not pay attention here to whether the frame
2781 is visible, since Fwindow_list skips non-visible frames if
2782 that is desired, under the control of frame_arg. */
2783 if (!MINI_WINDOW_P (w)
2784 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2785 consider all windows. */
2786 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2787 || (mini && minibuf_level > 0))
2788 switch (type)
2790 case GET_BUFFER_WINDOW:
2791 if (EQ (w->contents, obj)
2792 /* Don't find any minibuffer window except the one that
2793 is currently in use. */
2794 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2796 if (EQ (window, selected_window))
2797 /* Preferably return the selected window. */
2798 RETURN_UNGCPRO (window);
2799 else if (EQ (XWINDOW (window)->frame, selected_frame)
2800 && !frame_best_window_flag)
2801 /* Prefer windows on the current frame (but don't
2802 choose another one if we have one already). */
2804 best_window = window;
2805 frame_best_window_flag = 1;
2807 else if (NILP (best_window))
2808 best_window = window;
2810 break;
2812 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2813 /* We could simply check whether the buffer shown by window
2814 is live, and show another buffer in case it isn't. */
2815 if (EQ (w->contents, obj))
2817 /* Undedicate WINDOW. */
2818 wset_dedicated (w, Qnil);
2819 /* Make WINDOW show the buffer returned by
2820 other_buffer_safely, don't run any hooks. */
2821 set_window_buffer
2822 (window, other_buffer_safely (w->contents), 0, 0);
2823 /* If WINDOW is the selected window, make its buffer
2824 current. But do so only if the window shows the
2825 current buffer (Bug#6454). */
2826 if (EQ (window, selected_window)
2827 && XBUFFER (w->contents) == current_buffer)
2828 Fset_buffer (w->contents);
2830 break;
2832 case REDISPLAY_BUFFER_WINDOWS:
2833 if (EQ (w->contents, obj))
2835 mark_window_display_accurate (window, 0);
2836 w->update_mode_line = 1;
2837 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2838 update_mode_lines = 27;
2839 best_window = window;
2841 break;
2843 /* Check for a leaf window that has a killed buffer
2844 or broken markers. */
2845 case CHECK_ALL_WINDOWS:
2846 if (BUFFERP (w->contents))
2848 struct buffer *b = XBUFFER (w->contents);
2850 if (!BUFFER_LIVE_P (b))
2851 emacs_abort ();
2852 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2853 emacs_abort ();
2854 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2855 emacs_abort ();
2857 break;
2859 case WINDOW_LOOP_UNUSED:
2860 break;
2864 UNGCPRO;
2865 return best_window;
2868 /* Used for debugging. Abort if any window has a dead buffer. */
2870 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2871 void
2872 check_all_windows (void)
2874 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2877 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2878 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2879 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2880 the current buffer.
2882 The optional argument ALL-FRAMES specifies the frames to consider:
2884 - t means consider all windows on all existing frames.
2886 - `visible' means consider all windows on all visible frames.
2888 - 0 (the number zero) means consider all windows on all visible
2889 and iconified frames.
2891 - A frame means consider all windows on that frame only.
2893 Any other value of ALL-FRAMES means consider all windows on the
2894 selected frame and no others. */)
2895 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2897 Lisp_Object buffer;
2899 if (NILP (buffer_or_name))
2900 buffer = Fcurrent_buffer ();
2901 else
2902 buffer = Fget_buffer (buffer_or_name);
2904 if (BUFFERP (buffer))
2905 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2906 else
2907 return Qnil;
2910 static Lisp_Object
2911 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2913 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2917 static Lisp_Object
2918 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2920 return call2(Qwindow_pixel_to_total, frame, horizontal);
2924 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2925 Sdelete_other_windows_internal, 0, 2, "",
2926 doc: /* Make WINDOW fill its frame.
2927 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2928 and defaults to the selected one.
2930 Optional argument ROOT, if non-nil, must specify an internal window such
2931 that WINDOW is in its window subtree. If this is the case, replace ROOT
2932 by WINDOW and leave alone any windows not part of ROOT's subtree.
2934 When WINDOW is live try to reduce display jumps by keeping the text
2935 previously visible in WINDOW in the same place on the frame. Doing this
2936 depends on the value of (window-start WINDOW), so if calling this
2937 function in a program gives strange scrolling, make sure the
2938 window-start value is reasonable when this function is called. */)
2939 (Lisp_Object window, Lisp_Object root)
2941 struct window *w, *r, *s;
2942 struct frame *f;
2943 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2944 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2945 int top IF_LINT (= 0), new_top, resize_failed;
2947 w = decode_valid_window (window);
2948 XSETWINDOW (window, w);
2949 f = XFRAME (w->frame);
2951 if (NILP (root))
2952 /* ROOT is the frame's root window. */
2954 root = FRAME_ROOT_WINDOW (f);
2955 r = XWINDOW (root);
2957 else
2958 /* ROOT must be an ancestor of WINDOW. */
2960 r = decode_valid_window (root);
2961 pwindow = XWINDOW (window)->parent;
2962 while (!NILP (pwindow))
2963 if (EQ (pwindow, root))
2964 break;
2965 else
2966 pwindow = XWINDOW (pwindow)->parent;
2967 if (!EQ (pwindow, root))
2968 error ("Specified root is not an ancestor of specified window");
2971 if (EQ (window, root))
2972 /* A noop. */
2973 return Qnil;
2974 /* I don't understand the "top > 0" part below. If we deal with a
2975 standalone minibuffer it would have been caught by the preceding
2976 test. */
2977 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2978 error ("Can't expand minibuffer to full frame");
2980 if (BUFFERP (w->contents))
2982 startpos = marker_position (w->start);
2983 startbyte = marker_byte_position (w->start);
2984 top = (WINDOW_TOP_EDGE_LINE (w)
2985 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2986 /* Make sure WINDOW is the frame's selected window. */
2987 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2989 if (EQ (selected_frame, w->frame))
2990 Fselect_window (window, Qnil);
2991 else
2992 fset_selected_window (f, window);
2995 else
2997 /* See if the frame's selected window is a part of the window
2998 subtree rooted at WINDOW, by finding all the selected window's
2999 parents and comparing each one with WINDOW. If it isn't we
3000 need a new selected window for this frame. */
3001 swindow = FRAME_SELECTED_WINDOW (f);
3002 while (1)
3004 pwindow = swindow;
3005 while (!NILP (pwindow) && !EQ (window, pwindow))
3006 pwindow = XWINDOW (pwindow)->parent;
3008 if (EQ (window, pwindow))
3009 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3010 as the new selected window. */
3011 break;
3012 else
3013 /* Else try the previous window of SWINDOW. */
3014 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3017 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3019 if (EQ (selected_frame, w->frame))
3020 Fselect_window (swindow, Qnil);
3021 else
3022 fset_selected_window (f, swindow);
3026 block_input ();
3027 if (!FRAME_INITIAL_P (f))
3029 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3031 /* We are going to free the glyph matrices of WINDOW, and with
3032 that we might lose any information about glyph rows that have
3033 some of their glyphs highlighted in mouse face. (These rows
3034 are marked with a non-zero mouse_face_p flag.) If WINDOW
3035 indeed has some glyphs highlighted in mouse face, signal to
3036 frame's up-to-date hook that mouse highlight was overwritten,
3037 so that it will arrange for redisplaying the highlight. */
3038 if (EQ (hlinfo->mouse_face_window, window))
3039 reset_mouse_highlight (hlinfo);
3041 free_window_matrices (r);
3043 fset_redisplay (f);
3044 Vwindow_list = Qnil;
3045 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3046 resize_failed = 0;
3048 if (!WINDOW_LEAF_P (w))
3050 /* Resize child windows vertically. */
3051 XSETINT (delta, r->pixel_height - w->pixel_height);
3052 w->pixel_top = r->pixel_top;
3053 w->top_line = r->top_line;
3054 resize_root_window (window, delta, Qnil, Qnil, Qt);
3055 if (window_resize_check (w, 0))
3057 window_resize_apply (w, 0);
3058 window_pixel_to_total (w->frame, Qnil);
3060 else
3062 resize_root_window (window, delta, Qnil, Qt, Qt);
3063 if (window_resize_check (w, 0))
3065 window_resize_apply (w, 0);
3066 window_pixel_to_total (w->frame, Qnil);
3068 else
3069 resize_failed = 1;
3072 /* Resize child windows horizontally. */
3073 if (!resize_failed)
3075 w->left_col = r->left_col;
3076 w->pixel_left = r->pixel_left;
3077 XSETINT (delta, r->pixel_width - w->pixel_width);
3078 resize_root_window (window, delta, Qt, Qnil, Qt);
3079 if (window_resize_check (w, 1))
3081 window_resize_apply (w, 1);
3082 window_pixel_to_total (w->frame, Qt);
3084 else
3086 resize_root_window (window, delta, Qt, Qt, Qt);
3087 if (window_resize_check (w, 1))
3089 window_resize_apply (w, 1);
3090 window_pixel_to_total (w->frame, Qt);
3092 else
3093 resize_failed = 1;
3097 if (resize_failed)
3098 /* Play safe, if we still can ... */
3100 window = swindow;
3101 w = XWINDOW (window);
3105 /* Cleanly unlink WINDOW from window-tree. */
3106 if (!NILP (w->prev))
3107 /* Get SIBLING above (on the left of) WINDOW. */
3109 sibling = w->prev;
3110 s = XWINDOW (sibling);
3111 wset_next (s, w->next);
3112 if (!NILP (s->next))
3113 wset_prev (XWINDOW (s->next), sibling);
3115 else
3116 /* Get SIBLING below (on the right of) WINDOW. */
3118 sibling = w->next;
3119 s = XWINDOW (sibling);
3120 wset_prev (s, Qnil);
3121 wset_combination (XWINDOW (w->parent),
3122 XWINDOW (w->parent)->horizontal, sibling);
3125 /* Delete ROOT and all child windows of ROOT. */
3126 if (WINDOWP (r->contents))
3128 delete_all_child_windows (r->contents);
3129 wset_combination (r, 0, Qnil);
3132 replace_window (root, window, 1);
3134 /* This must become SWINDOW anyway ....... */
3135 if (BUFFERP (w->contents) && !resize_failed)
3137 /* Try to minimize scrolling, by setting the window start to the
3138 point will cause the text at the old window start to be at the
3139 same place on the frame. But don't try to do this if the
3140 window start is outside the visible portion (as might happen
3141 when the display is not current, due to typeahead). */
3142 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3143 if (new_top != top
3144 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3145 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3147 struct position pos;
3148 struct buffer *obuf = current_buffer;
3150 Fset_buffer (w->contents);
3151 /* This computation used to temporarily move point, but that
3152 can have unwanted side effects due to text properties. */
3153 pos = *vmotion (startpos, startbyte, -top, w);
3155 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3156 w->window_end_valid = 0;
3157 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3158 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3159 /* We need to do this, so that the window-scroll-functions
3160 get called. */
3161 w->optional_new_start = 1;
3163 set_buffer_internal (obuf);
3167 adjust_frame_glyphs (f);
3168 unblock_input ();
3170 run_window_configuration_change_hook (f);
3172 return Qnil;
3176 void
3177 replace_buffer_in_windows (Lisp_Object buffer)
3179 call1 (Qreplace_buffer_in_windows, buffer);
3182 /* If BUFFER is shown in a window, safely replace it with some other
3183 buffer in all windows of all frames, even those on other keyboards. */
3185 void
3186 replace_buffer_in_windows_safely (Lisp_Object buffer)
3188 if (buffer_window_count (XBUFFER (buffer)))
3190 Lisp_Object tail, frame;
3192 /* A single call to window_loop won't do the job because it only
3193 considers frames on the current keyboard. So loop manually over
3194 frames, and handle each one. */
3195 FOR_EACH_FRAME (tail, frame)
3196 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3200 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3201 minimum allowable size. PIXELWISE means interpret these as pixel
3202 sizes. */
3204 void
3205 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3207 /* For height, we have to see:
3208 how many windows the frame has at minimum (one or two),
3209 and whether it has a menu bar or other special stuff at the top. */
3210 if (pixelwise)
3212 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3213 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3215 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3216 min_height = 2 * min_height;
3218 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3219 min_height += FRAME_INTERNAL_BORDER_WIDTH (frame);
3221 if (*height < min_height)
3222 *height = min_height;
3223 if (*width < min_width)
3224 *width = min_width;
3226 else
3228 int min_height
3229 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3230 ? MIN_SAFE_WINDOW_HEIGHT
3231 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3233 if (FRAME_TOP_MARGIN (frame) > 0)
3234 min_height += FRAME_TOP_MARGIN (frame);
3236 if (*height < min_height)
3237 *height = min_height;
3238 if (*width < MIN_SAFE_WINDOW_WIDTH)
3239 *width = MIN_SAFE_WINDOW_WIDTH;
3243 /* Adjust the margins of window W if text area is too small.
3244 Return 1 if window width is ok after adjustment; 0 if window
3245 is still too narrow. */
3247 static int
3248 adjust_window_margins (struct window *w)
3250 int box_width = (WINDOW_PIXEL_WIDTH (w)
3251 - WINDOW_FRINGES_WIDTH (w)
3252 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3253 int margin_width = WINDOW_MARGINS_WIDTH (w);
3255 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3256 return 1;
3258 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3259 return 0;
3260 else
3261 /* Window's text area is too narrow, but reducing the window
3262 margins will fix that. */
3264 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3266 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3268 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3270 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3271 w->left_margin_cols = w->right_margin_cols =
3272 margin_width / (2 * unit);
3273 else
3274 w->right_margin_cols = margin_width / unit;
3276 else
3277 w->left_margin_cols = margin_width / unit;
3279 return 1;
3283 /* The following three routines are needed for running a window's
3284 configuration change hook. */
3285 static void
3286 run_funs (Lisp_Object funs)
3288 for (; CONSP (funs); funs = XCDR (funs))
3289 if (!EQ (XCAR (funs), Qt))
3290 call0 (XCAR (funs));
3293 static void
3294 select_window_norecord (Lisp_Object window)
3296 if (WINDOW_LIVE_P (window))
3297 Fselect_window (window, Qt);
3300 static void
3301 select_frame_norecord (Lisp_Object frame)
3303 if (FRAME_LIVE_P (XFRAME (frame)))
3304 Fselect_frame (frame, Qt);
3307 void
3308 run_window_configuration_change_hook (struct frame *f)
3310 ptrdiff_t count = SPECPDL_INDEX ();
3311 Lisp_Object frame, global_wcch
3312 = Fdefault_value (Qwindow_configuration_change_hook);
3313 XSETFRAME (frame, f);
3315 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3316 return;
3318 /* Use the right buffer. Matters when running the local hooks. */
3319 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3321 record_unwind_current_buffer ();
3322 Fset_buffer (Fwindow_buffer (Qnil));
3325 if (SELECTED_FRAME () != f)
3327 record_unwind_protect (select_frame_norecord, selected_frame);
3328 select_frame_norecord (frame);
3331 /* Look for buffer-local values. */
3333 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3334 for (; CONSP (windows); windows = XCDR (windows))
3336 Lisp_Object window = XCAR (windows);
3337 Lisp_Object buffer = Fwindow_buffer (window);
3338 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3339 buffer)))
3341 ptrdiff_t inner_count = SPECPDL_INDEX ();
3342 record_unwind_protect (select_window_norecord, selected_window);
3343 select_window_norecord (window);
3344 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3345 buffer));
3346 unbind_to (inner_count, Qnil);
3351 run_funs (global_wcch);
3352 unbind_to (count, Qnil);
3355 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3356 Srun_window_configuration_change_hook, 0, 1, 0,
3357 doc: /* Run `window-configuration-change-hook' for FRAME.
3358 If FRAME is omitted or nil, it defaults to the selected frame. */)
3359 (Lisp_Object frame)
3361 run_window_configuration_change_hook (decode_live_frame (frame));
3362 return Qnil;
3365 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3366 Srun_window_scroll_functions, 0, 1, 0,
3367 doc: /* Run `window-scroll-functions' for WINDOW.
3368 If WINDOW is omitted or nil, it defaults to the selected window. */)
3369 (Lisp_Object window)
3371 if (! NILP (Vwindow_scroll_functions))
3372 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3373 Fmarker_position (decode_live_window (window)->start));
3374 return Qnil;
3377 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3378 to run hooks. See make_frame for a case where it's not allowed.
3379 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3380 scroll-bar settings of the window are not reset from the buffer's
3381 local settings. */
3383 void
3384 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3385 bool run_hooks_p, bool keep_margins_p)
3387 struct window *w = XWINDOW (window);
3388 struct buffer *b = XBUFFER (buffer);
3389 ptrdiff_t count = SPECPDL_INDEX ();
3390 bool samebuf = EQ (buffer, w->contents);
3392 wset_buffer (w, buffer);
3394 if (EQ (window, selected_window))
3395 bset_last_selected_window (b, window);
3397 /* Let redisplay errors through. */
3398 b->display_error_modiff = 0;
3400 /* Update time stamps of buffer display. */
3401 if (INTEGERP (BVAR (b, display_count)))
3402 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3403 bset_display_time (b, Fcurrent_time ());
3405 w->window_end_pos = 0;
3406 w->window_end_vpos = 0;
3407 w->last_cursor_vpos = 0;
3409 if (!(keep_margins_p && samebuf))
3410 { /* If we're not actually changing the buffer, don't reset hscroll and
3411 vscroll. This case happens for example when called from
3412 change_frame_size_1, where we use a dummy call to
3413 Fset_window_buffer on the frame's selected window (and no other)
3414 just in order to run window-configuration-change-hook.
3415 Resetting hscroll and vscroll here is problematic for things like
3416 image-mode and doc-view-mode since it resets the image's position
3417 whenever we resize the frame. */
3418 w->hscroll = w->min_hscroll = 0;
3419 w->vscroll = 0;
3420 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3421 set_marker_restricted (w->start,
3422 make_number (b->last_window_start),
3423 buffer);
3424 w->start_at_line_beg = 0;
3425 w->force_start = 0;
3427 /* Maybe we could move this into the `if' but it's not obviously safe and
3428 I doubt it's worth the trouble. */
3429 wset_redisplay (w);
3430 w->update_mode_line = true;
3432 /* We must select BUFFER for running the window-scroll-functions. */
3433 /* We can't check ! NILP (Vwindow_scroll_functions) here
3434 because that might itself be a local variable. */
3435 if (window_initialized)
3437 record_unwind_current_buffer ();
3438 Fset_buffer (buffer);
3441 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3443 if (!keep_margins_p)
3445 /* Set left and right marginal area width etc. from buffer. */
3446 set_window_fringes (w, BVAR (b, left_fringe_width),
3447 BVAR (b, right_fringe_width),
3448 BVAR (b, fringes_outside_margins));
3449 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3450 BVAR (b, vertical_scroll_bar_type), Qnil);
3451 set_window_margins (w, BVAR (b, left_margin_cols),
3452 BVAR (b, right_margin_cols));
3453 apply_window_adjustment (w);
3456 if (run_hooks_p)
3458 if (! NILP (Vwindow_scroll_functions))
3459 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3460 Fmarker_position (w->start));
3461 if (!samebuf)
3462 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3465 unbind_to (count, Qnil);
3468 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3469 doc: /* Make WINDOW display BUFFER-OR-NAME.
3470 WINDOW must be a live window and defaults to the selected one.
3471 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3473 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3474 display margins, fringe widths, and scroll bar settings are preserved;
3475 the default is to reset these from the local settings for BUFFER-OR-NAME
3476 or the frame defaults. Return nil.
3478 This function throws an error when WINDOW is strongly dedicated to its
3479 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3480 already display BUFFER-OR-NAME.
3482 This function runs `window-scroll-functions' before running
3483 `window-configuration-change-hook'. */)
3484 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3486 register Lisp_Object tem, buffer;
3487 register struct window *w = decode_live_window (window);
3489 XSETWINDOW (window, w);
3490 buffer = Fget_buffer (buffer_or_name);
3491 CHECK_BUFFER (buffer);
3492 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3493 error ("Attempt to display deleted buffer");
3495 tem = w->contents;
3496 if (NILP (tem))
3497 error ("Window is deleted");
3498 else
3500 if (!EQ (tem, buffer))
3502 if (EQ (w->dedicated, Qt))
3503 /* WINDOW is strongly dedicated to its buffer, signal an
3504 error. */
3505 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3506 else
3507 /* WINDOW is weakly dedicated to its buffer, reset
3508 dedication. */
3509 wset_dedicated (w, Qnil);
3511 call1 (Qrecord_window_buffer, window);
3514 unshow_buffer (w);
3517 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3519 return Qnil;
3522 static Lisp_Object
3523 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3525 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3528 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3529 0, 1, 0,
3530 doc: /* Force all windows to be updated on next redisplay.
3531 If optional arg OBJECT is a window, force redisplay of that window only.
3532 If OBJECT is a buffer or buffer name, force redisplay of all windows
3533 displaying that buffer. */)
3534 (Lisp_Object object)
3536 if (NILP (object))
3538 windows_or_buffers_changed = 29;
3539 update_mode_lines = 28;
3540 return Qt;
3543 if (WINDOWP (object))
3545 struct window *w = XWINDOW (object);
3546 mark_window_display_accurate (object, 0);
3547 w->update_mode_line = 1;
3548 if (BUFFERP (w->contents))
3549 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3550 update_mode_lines = 29;
3551 return Qt;
3554 if (STRINGP (object))
3555 object = Fget_buffer (object);
3556 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3557 && buffer_window_count (XBUFFER (object)))
3559 /* If buffer is live and shown in at least one window, find
3560 all windows showing this buffer and force update of them. */
3561 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3562 return NILP (object) ? Qnil : Qt;
3565 /* If nothing suitable was found, just return.
3566 We could signal an error, but this feature will typically be used
3567 asynchronously in timers or process sentinels, so we don't. */
3568 return Qnil;
3571 /* Obsolete since 24.3. */
3572 void
3573 temp_output_buffer_show (register Lisp_Object buf)
3575 register struct buffer *old = current_buffer;
3576 register Lisp_Object window;
3577 register struct window *w;
3579 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3581 Fset_buffer (buf);
3582 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3583 BEGV = BEG;
3584 ZV = Z;
3585 SET_PT (BEG);
3586 set_buffer_internal (old);
3588 if (!NILP (Vtemp_buffer_show_function))
3589 call1 (Vtemp_buffer_show_function, buf);
3590 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3592 if (!EQ (XWINDOW (window)->frame, selected_frame))
3593 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3594 Vminibuf_scroll_window = window;
3595 w = XWINDOW (window);
3596 w->hscroll = 0;
3597 w->min_hscroll = 0;
3598 set_marker_restricted_both (w->start, buf, BEG, BEG);
3599 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3601 /* Run temp-buffer-show-hook, with the chosen window selected
3602 and its buffer current. */
3604 ptrdiff_t count = SPECPDL_INDEX ();
3605 Lisp_Object prev_window, prev_buffer;
3606 prev_window = selected_window;
3607 XSETBUFFER (prev_buffer, old);
3609 /* Select the window that was chosen, for running the hook.
3610 Note: Both Fselect_window and select_window_norecord may
3611 set-buffer to the buffer displayed in the window,
3612 so we need to save the current buffer. --stef */
3613 record_unwind_protect (restore_buffer, prev_buffer);
3614 record_unwind_protect (select_window_norecord, prev_window);
3615 Fselect_window (window, Qt);
3616 Fset_buffer (w->contents);
3617 Frun_hooks (1, &Qtemp_buffer_show_hook);
3618 unbind_to (count, Qnil);
3623 /* Make new window, have it replace WINDOW in window-tree, and make
3624 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3625 horizontal child). */
3626 static void
3627 make_parent_window (Lisp_Object window, bool horflag)
3629 Lisp_Object parent;
3630 register struct window *o, *p;
3632 o = XWINDOW (window);
3633 p = allocate_window ();
3634 memcpy ((char *) p + sizeof (struct vectorlike_header),
3635 (char *) o + sizeof (struct vectorlike_header),
3636 word_size * VECSIZE (struct window));
3637 /* P's buffer slot may change from nil to a buffer... */
3638 adjust_window_count (p, 1);
3639 XSETWINDOW (parent, p);
3641 p->sequence_number = ++sequence_number;
3643 replace_window (window, parent, 1);
3645 wset_next (o, Qnil);
3646 wset_prev (o, Qnil);
3647 wset_parent (o, parent);
3648 /* ...but now P becomes an internal window. */
3649 wset_start (p, Qnil);
3650 wset_pointm (p, Qnil);
3651 wset_buffer (p, Qnil);
3652 wset_combination (p, horflag, window);
3653 wset_combination_limit (p, Qnil);
3654 wset_window_parameters (p, Qnil);
3657 /* Make new window from scratch. */
3658 Lisp_Object
3659 make_window (void)
3661 Lisp_Object window;
3662 register struct window *w;
3664 w = allocate_window ();
3665 /* Initialize Lisp data. Note that allocate_window initializes all
3666 Lisp data to nil, so do it only for slots which should not be nil. */
3667 wset_normal_lines (w, make_float (1.0));
3668 wset_normal_cols (w, make_float (1.0));
3669 wset_new_total (w, make_number (0));
3670 wset_new_normal (w, make_number (0));
3671 wset_new_pixel (w, make_number (0));
3672 wset_start (w, Fmake_marker ());
3673 wset_pointm (w, Fmake_marker ());
3674 wset_vertical_scroll_bar_type (w, Qt);
3675 /* These Lisp fields are marked specially so they're not set to nil by
3676 allocate_window. */
3677 wset_prev_buffers (w, Qnil);
3678 wset_next_buffers (w, Qnil);
3680 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3681 non-Lisp data, so do it only for slots which should not be zero. */
3682 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3683 w->left_fringe_width = w->right_fringe_width = -1;
3684 w->mode_line_height = w->header_line_height = -1;
3685 #ifdef HAVE_WINDOW_SYSTEM
3686 w->phys_cursor_type = NO_CURSOR;
3687 w->phys_cursor_width = -1;
3688 #endif
3689 w->sequence_number = ++sequence_number;
3690 w->scroll_bar_width = -1;
3691 w->column_number_displayed = -1;
3693 /* Reset window_list. */
3694 Vwindow_list = Qnil;
3695 /* Return window. */
3696 XSETWINDOW (window, w);
3697 return window;
3700 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3701 doc: /* Set new pixel size of WINDOW to SIZE.
3702 WINDOW must be a valid window and defaults to the selected one.
3703 Return SIZE.
3705 Optional argument ADD non-nil means add SIZE to the new pixel size of
3706 WINDOW and return the sum.
3708 Note: This function does not operate on any child windows of WINDOW. */)
3709 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3711 struct window *w = decode_valid_window (window);
3712 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3713 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3715 CHECK_RANGED_INTEGER (size, size_min, size_max);
3716 if (NILP (add))
3717 wset_new_pixel (w, size);
3718 else
3719 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3721 return w->new_pixel;
3724 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3725 doc: /* Set new total size of WINDOW to SIZE.
3726 WINDOW must be a valid window and defaults to the selected one.
3727 Return SIZE.
3729 Optional argument ADD non-nil means add SIZE to the new total size of
3730 WINDOW and return the sum.
3732 Note: This function does not operate on any child windows of WINDOW. */)
3733 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3735 struct window *w = decode_valid_window (window);
3737 CHECK_NUMBER (size);
3738 if (NILP (add))
3739 wset_new_total (w, size);
3740 else
3741 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3743 return w->new_total;
3746 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3747 doc: /* Set new normal size of WINDOW to SIZE.
3748 WINDOW must be a valid window and defaults to the selected one.
3749 Return SIZE.
3751 Note: This function does not operate on any child windows of WINDOW. */)
3752 (Lisp_Object window, Lisp_Object size)
3754 wset_new_normal (decode_valid_window (window), size);
3755 return size;
3758 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3759 non-zero) to w->new_pixel would result in correct heights (widths)
3760 for window W and recursively all child windows of W.
3762 Note: This function does not check any of `window-fixed-size-p',
3763 `window-min-height' or `window-min-width'. It does check that window
3764 sizes do not drop below one line (two columns). */
3765 static int
3766 window_resize_check (struct window *w, bool horflag)
3768 struct frame *f = XFRAME (w->frame);
3769 struct window *c;
3771 if (WINDOW_VERTICAL_COMBINATION_P (w))
3772 /* W is a vertical combination. */
3774 c = XWINDOW (w->contents);
3775 if (horflag)
3776 /* All child windows of W must have the same width as W. */
3778 while (c)
3780 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3781 || !window_resize_check (c, horflag))
3782 return 0;
3784 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3787 return 1;
3789 else
3790 /* The sum of the heights of the child windows of W must equal
3791 W's height. */
3793 int remaining_pixels = XINT (w->new_pixel);
3795 while (c)
3797 if (!window_resize_check (c, horflag))
3798 return 0;
3800 remaining_pixels -= XINT (c->new_pixel);
3801 if (remaining_pixels < 0)
3802 return 0;
3803 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3806 return remaining_pixels == 0;
3809 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3810 /* W is a horizontal combination. */
3812 c = XWINDOW (w->contents);
3813 if (horflag)
3814 /* The sum of the widths of the child windows of W must equal W's
3815 width. */
3817 int remaining_pixels = XINT (w->new_pixel);
3819 while (c)
3821 if (!window_resize_check (c, horflag))
3822 return 0;
3824 remaining_pixels -= XINT (c->new_pixel);
3825 if (remaining_pixels < 0)
3826 return 0;
3827 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3830 return remaining_pixels == 0;
3832 else
3833 /* All child windows of W must have the same height as W. */
3835 while (c)
3837 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3838 || !window_resize_check (c, horflag))
3839 return 0;
3841 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3844 return 1;
3847 else
3848 /* A leaf window. Make sure it's not too small. The following
3849 hardcodes the values of `window-safe-min-width' (2) and
3850 `window-safe-min-height' (1) which are defined in window.el. */
3851 return (XINT (w->new_pixel) >= (horflag
3852 ? (2 * FRAME_COLUMN_WIDTH (f))
3853 : FRAME_LINE_HEIGHT (f)));
3857 /* Set w->pixel_height (w->pixel_height if HORIZONTAL is non-zero) to
3858 w->new_pixel for window W and recursively all child windows of W.
3859 Also calculate and assign the new vertical (horizontal) pixel start
3860 positions of each of these windows.
3862 This function does not perform any error checks. Make sure you have
3863 run window_resize_check on W before applying this function. */
3864 static void
3865 window_resize_apply (struct window *w, bool horflag)
3867 struct window *c;
3868 int edge;
3869 int unit = (horflag
3870 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3871 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3873 /* Note: Assigning new_normal requires that the new total size of the
3874 parent window has been set *before*. */
3875 if (horflag)
3877 w->pixel_width = XFASTINT (w->new_pixel);
3878 w->total_cols = w->pixel_width / unit;
3879 if (NUMBERP (w->new_normal))
3880 wset_normal_cols (w, w->new_normal);
3882 edge = w->pixel_left;
3884 else
3886 w->pixel_height = XFASTINT (w->new_pixel);
3887 w->total_lines = w->pixel_height / unit;
3888 if (NUMBERP (w->new_normal))
3889 wset_normal_lines (w, w->new_normal);
3891 edge = w->pixel_top;
3894 if (WINDOW_VERTICAL_COMBINATION_P (w))
3895 /* W is a vertical combination. */
3897 c = XWINDOW (w->contents);
3898 while (c)
3900 if (horflag)
3902 c->pixel_left = edge;
3903 c->left_col = edge / unit;
3905 else
3907 c->pixel_top = edge;
3908 c->top_line = edge / unit;
3910 window_resize_apply (c, horflag);
3911 if (!horflag)
3912 edge = edge + c->pixel_height;
3914 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3917 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3918 /* W is a horizontal combination. */
3920 c = XWINDOW (w->contents);
3921 while (c)
3923 if (horflag)
3925 c->pixel_left = edge;
3926 c->left_col = edge / unit;
3928 else
3930 c->pixel_top = edge;
3931 c->top_line = edge / unit;
3934 window_resize_apply (c, horflag);
3935 if (horflag)
3936 edge = edge + c->pixel_width;
3938 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3941 else
3943 adjust_window_margins (w);
3944 /* Bug#15957. */
3945 w->window_end_valid = 0;
3950 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3951 w->new_total for window W and recursively all child windows of W.
3952 Also calculate and assign the new vertical (horizontal) start
3953 positions of each of these windows. */
3954 static void
3955 window_resize_apply_total (struct window *w, bool horflag)
3957 struct window *c;
3958 int edge;
3960 /* Note: Assigning new_normal requires that the new total size of the
3961 parent window has been set *before*. */
3962 if (horflag)
3964 w->total_cols = XFASTINT (w->new_total);
3965 edge = w->left_col;
3967 else
3969 w->total_lines = XFASTINT (w->new_total);
3970 edge = w->top_line;
3973 if (WINDOW_VERTICAL_COMBINATION_P (w))
3974 /* W is a vertical combination. */
3976 c = XWINDOW (w->contents);
3977 while (c)
3979 if (horflag)
3980 c->left_col = edge;
3981 else
3982 c->top_line = edge;
3984 window_resize_apply_total (c, horflag);
3985 if (!horflag)
3986 edge = edge + c->total_lines;
3988 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3991 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3992 /* W is a horizontal combination. */
3994 c = XWINDOW (w->contents);
3995 while (c)
3997 if (horflag)
3998 c->left_col = edge;
3999 else
4000 c->top_line = edge;
4002 window_resize_apply_total (c, horflag);
4003 if (horflag)
4004 edge = edge + c->total_cols;
4006 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4011 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4012 doc: /* Apply requested size values for window-tree of FRAME.
4013 If FRAME is omitted or nil, it defaults to the selected frame.
4015 Optional argument HORIZONTAL omitted or nil means apply requested
4016 height values. HORIZONTAL non-nil means apply requested width values.
4018 This function checks whether the requested values sum up to a valid
4019 window layout, recursively assigns the new sizes of all child windows
4020 and calculates and assigns the new start positions of these windows.
4022 Note: This function does not check any of `window-fixed-size-p',
4023 `window-min-height' or `window-min-width'. All these checks have to
4024 be applied on the Elisp level. */)
4025 (Lisp_Object frame, Lisp_Object horizontal)
4027 struct frame *f = decode_live_frame (frame);
4028 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4029 bool horflag = !NILP (horizontal);
4031 if (!window_resize_check (r, horflag)
4032 || (XINT (r->new_pixel)
4033 != (horflag ? r->pixel_width : r->pixel_height)))
4034 return Qnil;
4036 block_input ();
4037 window_resize_apply (r, horflag);
4039 fset_redisplay (f);
4040 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4042 adjust_frame_glyphs (f);
4043 unblock_input ();
4045 return Qt;
4049 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4050 doc: /* Apply requested total size values for window-tree of FRAME.
4051 If FRAME is omitted or nil, it defaults to the selected frame.
4053 This function does not assign pixel or normal size values. You should
4054 have run `window-resize-apply' before running this.
4056 Optional argument HORIZONTAL omitted or nil means apply requested
4057 height values. HORIZONTAL non-nil means apply requested width
4058 values. */)
4059 (Lisp_Object frame, Lisp_Object horizontal)
4061 struct frame *f = decode_live_frame (frame);
4062 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4064 block_input ();
4065 /* Necessary when deleting the top-/or leftmost window. */
4066 r->left_col = 0;
4067 r->top_line = FRAME_TOP_MARGIN (f);
4068 window_resize_apply_total (r, !NILP (horizontal));
4069 /* Handle the mini window. */
4070 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4072 struct window *m = XWINDOW (f->minibuffer_window);
4074 if (NILP (horizontal))
4076 m->top_line = r->top_line + r->total_lines;
4077 m->total_lines = XFASTINT (m->new_total);
4079 else
4080 m->total_cols = XFASTINT (m->new_total);
4083 unblock_input ();
4085 return Qt;
4089 /* Resize frame F's windows when number of lines of F is set to SIZE.
4090 HORFLAG 1 means resize windows when number of columns of F is set to
4091 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4093 This function can delete all windows but the selected one in order to
4094 satisfy the request. The result will be meaningful if and only if
4095 F's windows have meaningful sizes when you call this. */
4096 void
4097 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4099 Lisp_Object root = f->root_window;
4100 struct window *r = XWINDOW (root);
4101 Lisp_Object mini = f->minibuffer_window;
4102 struct window *m;
4103 /* old_size is the old size of the frame's root window. */
4104 int old_size = horflag ? r->total_cols : r->total_lines;
4105 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4106 /* new_size is the new size of the frame's root window. */
4107 int new_size, new_pixel_size;
4108 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4110 /* Don't let the size drop below one unit. This is more comforting
4111 when we are called from x_set_tool_bar_lines since the latter may
4112 have implicitly given us a zero or negative height. */
4113 if (pixelwise)
4115 /* Note: This does not include the size for internal borders
4116 since these are not part of the frame's text area. */
4117 new_pixel_size = max (horflag
4118 ? size
4119 : (size
4120 - FRAME_TOP_MARGIN_HEIGHT (f)
4121 - ((FRAME_HAS_MINIBUF_P (f)
4122 && !FRAME_MINIBUF_ONLY_P (f))
4123 ? FRAME_LINE_HEIGHT (f) : 0)),
4124 unit);
4125 new_size = new_pixel_size / unit;
4127 else
4129 new_size = max (horflag
4130 ? size
4131 : (size
4132 - FRAME_TOP_MARGIN (f)
4133 - ((FRAME_HAS_MINIBUF_P (f)
4134 && !FRAME_MINIBUF_ONLY_P (f))
4135 ? 1 : 0)),
4137 new_pixel_size = new_size * unit;
4140 r->top_line = FRAME_TOP_MARGIN (f);
4141 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4143 if (new_pixel_size == old_pixel_size)
4145 else if (WINDOW_LEAF_P (r))
4146 /* For a leaf root window just set the size. */
4147 if (horflag)
4149 r->total_cols = new_size;
4150 r->pixel_width = new_pixel_size;
4152 else
4154 r->total_lines = new_size;
4155 r->pixel_height = new_pixel_size;
4157 else
4159 Lisp_Object delta;
4161 if (pixelwise)
4162 XSETINT (delta, new_pixel_size - old_pixel_size);
4163 else
4164 XSETINT (delta, new_size - old_size);
4166 /* Try a "normal" resize first. */
4167 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4168 pixelwise ? Qt : Qnil);
4169 if (window_resize_check (r, horflag)
4170 && new_pixel_size == XINT (r->new_pixel))
4172 window_resize_apply (r, horflag);
4173 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4175 else
4177 /* Try with "reasonable" minimum sizes next. */
4178 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4179 pixelwise ? Qt : Qnil);
4180 if (window_resize_check (r, horflag)
4181 && new_pixel_size == XINT (r->new_pixel))
4183 window_resize_apply (r, horflag);
4184 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4186 else
4188 /* Finally, try with "safe" minimum sizes. */
4189 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4190 pixelwise ? Qt : Qnil);
4191 if (window_resize_check (r, horflag)
4192 && new_pixel_size == XINT (r->new_pixel))
4194 window_resize_apply (r, horflag);
4195 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4197 #if 0 /* Let's try without killing other windows. */
4198 else
4200 /* We lost. Delete all windows but the frame's
4201 selected one. */
4202 root = f->selected_window;
4203 Fdelete_other_windows_internal (root, Qnil);
4204 if (horflag)
4206 XWINDOW (root)->total_cols = new_size;
4207 XWINDOW (root)->pixel_width = new_pixel_size;
4209 else
4211 XWINDOW (root)->total_lines = new_size;
4212 XWINDOW (root)->pixel_height = new_pixel_size;
4215 #endif /* 0 */
4220 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4222 m = XWINDOW (mini);
4223 if (horflag)
4225 m->total_cols = size;
4226 m->pixel_width = new_pixel_size;
4228 else
4230 /* Are we sure we always want 1 line here? */
4231 m->total_lines = 1;
4232 m->pixel_height = FRAME_LINE_HEIGHT (f);
4233 m->top_line = r->top_line + r->total_lines;
4234 m->pixel_top = r->pixel_top + r->pixel_height;
4238 fset_redisplay (f);
4242 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4243 doc: /* Split window OLD.
4244 Second argument PIXEL-SIZE specifies the number of pixels of the
4245 new window. In any case TOTAL-SIZE must be a positive integer.
4247 Third argument SIDE nil (or `below') specifies that the new window shall
4248 be located below WINDOW. SIDE `above' means the new window shall be
4249 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4250 height of the new window including space reserved for the mode and/or
4251 header line.
4253 SIDE t (or `right') specifies that the new window shall be located on
4254 the right side of WINDOW. SIDE `left' means the new window shall be
4255 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4256 width of the new window including space reserved for fringes and the
4257 scrollbar or a divider column.
4259 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4260 according to the SIDE argument.
4262 The new pixel and normal sizes of all involved windows must have been
4263 set correctly. See the code of `split-window' for how this is done. */)
4264 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4266 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4267 parent window or an internal window we have to install as OLD's new
4268 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4269 provided OLD is a leaf window, or to the frame's selected window.
4270 NEW (*n) is the new window created with some parameters taken from
4271 REFERENCE (*r). */
4272 register Lisp_Object new, frame, reference;
4273 register struct window *o, *p, *n, *r, *c;
4274 struct frame *f;
4275 bool horflag
4276 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4277 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4278 int combination_limit = 0, sum = 0;
4279 int total_size;
4281 CHECK_WINDOW (old);
4282 o = XWINDOW (old);
4283 frame = WINDOW_FRAME (o);
4284 f = XFRAME (frame);
4286 CHECK_NUMBER (pixel_size);
4287 total_size
4288 = XINT (pixel_size) / (horflag
4289 ? FRAME_COLUMN_WIDTH (f)
4290 : FRAME_LINE_HEIGHT (f));
4292 /* Set combination_limit to 1 if we have to make a new parent window.
4293 We do that if either `window-combination-limit' is t, or OLD has no
4294 parent, or OLD is ortho-combined. */
4295 combination_limit =
4296 EQ (Vwindow_combination_limit, Qt)
4297 || NILP (o->parent)
4298 || (horflag
4299 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4300 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4302 /* We need a live reference window to initialize some parameters. */
4303 if (WINDOW_LIVE_P (old))
4304 /* OLD is live, use it as reference window. */
4305 reference = old;
4306 else
4307 /* Use the frame's selected window as reference window. */
4308 reference = FRAME_SELECTED_WINDOW (f);
4309 r = XWINDOW (reference);
4311 /* The following bugs are caught by `split-window'. */
4312 if (MINI_WINDOW_P (o))
4313 error ("Attempt to split minibuffer window");
4314 else if (total_size < (horflag ? 2 : 1))
4315 error ("Size of new window too small (after split)");
4316 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4317 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4318 proportionally. */
4320 p = XWINDOW (o->parent);
4321 /* Temporarily pretend we split the parent window. */
4322 wset_new_pixel
4323 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4324 - XINT (pixel_size)));
4325 if (!window_resize_check (p, horflag))
4326 error ("Window sizes don't fit");
4327 else
4328 /* Undo the temporary pretension. */
4329 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4331 else
4333 if (!window_resize_check (o, horflag))
4334 error ("Resizing old window failed");
4335 else if (XINT (pixel_size) + XINT (o->new_pixel)
4336 != (horflag ? o->pixel_width : o->pixel_height))
4337 error ("Sum of sizes of old and new window don't fit");
4340 /* This is our point of no return. */
4341 if (combination_limit)
4343 /* Save the old value of o->normal_cols/lines. It gets corrupted
4344 by make_parent_window and we need it below for assigning it to
4345 p->new_normal. */
4346 Lisp_Object new_normal
4347 = horflag ? o->normal_cols : o->normal_lines;
4349 make_parent_window (old, horflag);
4350 p = XWINDOW (o->parent);
4351 if (EQ (Vwindow_combination_limit, Qt))
4352 /* Store t in the new parent's combination_limit slot to avoid
4353 that its children get merged into another window. */
4354 wset_combination_limit (p, Qt);
4355 /* These get applied below. */
4356 wset_new_pixel
4357 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4358 wset_new_total
4359 (p, make_number (horflag ? o->total_cols : o->total_lines));
4360 wset_new_normal (p, new_normal);
4362 else
4363 p = XWINDOW (o->parent);
4365 fset_redisplay (f);
4366 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4367 new = make_window ();
4368 n = XWINDOW (new);
4369 wset_frame (n, frame);
4370 wset_parent (n, o->parent);
4372 if (EQ (side, Qabove) || EQ (side, Qleft))
4374 wset_prev (n, o->prev);
4375 if (NILP (n->prev))
4376 wset_combination (p, horflag, new);
4377 else
4378 wset_next (XWINDOW (n->prev), new);
4379 wset_next (n, old);
4380 wset_prev (o, new);
4382 else
4384 wset_next (n, o->next);
4385 if (!NILP (n->next))
4386 wset_prev (XWINDOW (n->next), new);
4387 wset_prev (n, old);
4388 wset_next (o, new);
4391 n->window_end_valid = 0;
4392 n->last_cursor_vpos = 0;
4394 /* Get special geometry settings from reference window. */
4395 n->left_margin_cols = r->left_margin_cols;
4396 n->right_margin_cols = r->right_margin_cols;
4397 n->left_fringe_width = r->left_fringe_width;
4398 n->right_fringe_width = r->right_fringe_width;
4399 n->fringes_outside_margins = r->fringes_outside_margins;
4400 n->scroll_bar_width = r->scroll_bar_width;
4401 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4403 /* Directly assign orthogonal coordinates and sizes. */
4404 if (horflag)
4406 n->pixel_top = o->pixel_top;
4407 n->top_line = o->top_line;
4408 n->pixel_height = o->pixel_height;
4409 n->total_lines = o->total_lines;
4411 else
4413 n->pixel_left = o->pixel_left;
4414 n->left_col = o->left_col;
4415 n->pixel_width = o->pixel_width;
4416 n->total_cols = o->total_cols;
4419 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4420 get them ready here. */
4421 wset_new_pixel (n, pixel_size);
4422 c = XWINDOW (p->contents);
4423 while (c)
4425 if (c != n)
4426 sum = sum + XINT (c->new_total);
4427 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4429 wset_new_total (n, make_number ((horflag
4430 ? p->total_cols
4431 : p->total_lines)
4432 - sum));
4433 wset_new_normal (n, normal_size);
4435 block_input ();
4436 window_resize_apply (p, horflag);
4437 adjust_frame_glyphs (f);
4438 /* Set buffer of NEW to buffer of reference window. Don't run
4439 any hooks. */
4440 set_window_buffer (new, r->contents, 0, 1);
4441 unblock_input ();
4443 /* Maybe we should run the scroll functions in Elisp (which already
4444 runs the configuration change hook). */
4445 if (! NILP (Vwindow_scroll_functions))
4446 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4447 Fmarker_position (n->start));
4448 /* Return NEW. */
4449 return new;
4453 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4454 doc: /* Remove WINDOW from its frame.
4455 WINDOW defaults to the selected window. Return nil.
4456 Signal an error when WINDOW is the only window on its frame. */)
4457 (register Lisp_Object window)
4459 register Lisp_Object parent, sibling, frame, root;
4460 struct window *w, *p, *s, *r;
4461 struct frame *f;
4462 bool horflag, before_sibling = 0;
4464 w = decode_any_window (window);
4465 XSETWINDOW (window, w);
4466 if (NILP (w->contents))
4467 /* It's a no-op to delete an already deleted window. */
4468 return Qnil;
4470 parent = w->parent;
4471 if (NILP (parent))
4472 /* Never delete a minibuffer or frame root window. */
4473 error ("Attempt to delete minibuffer or sole ordinary window");
4474 else if (NILP (w->prev) && NILP (w->next))
4475 /* Rather bow out here, this case should be handled on the Elisp
4476 level. */
4477 error ("Attempt to delete sole window of parent");
4479 p = XWINDOW (parent);
4480 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4482 frame = WINDOW_FRAME (w);
4483 f = XFRAME (frame);
4485 root = FRAME_ROOT_WINDOW (f);
4486 r = XWINDOW (root);
4488 /* Unlink WINDOW from window tree. */
4489 if (NILP (w->prev))
4490 /* Get SIBLING below (on the right of) WINDOW. */
4492 /* before_sibling 1 means WINDOW is the first child of its
4493 parent and thus before the sibling. */
4494 before_sibling = 1;
4495 sibling = w->next;
4496 s = XWINDOW (sibling);
4497 wset_prev (s, Qnil);
4498 wset_combination (p, horflag, sibling);
4500 else
4501 /* Get SIBLING above (on the left of) WINDOW. */
4503 sibling = w->prev;
4504 s = XWINDOW (sibling);
4505 wset_next (s, w->next);
4506 if (!NILP (s->next))
4507 wset_prev (XWINDOW (s->next), sibling);
4510 if (window_resize_check (r, horflag)
4511 && (XINT (r->new_pixel)
4512 == (horflag ? r->pixel_width : r->pixel_height)))
4513 /* We can delete WINDOW now. */
4516 /* Block input. */
4517 block_input ();
4518 window_resize_apply (p, horflag);
4519 /* If this window is referred to by the dpyinfo's mouse
4520 highlight, invalidate that slot to be safe (Bug#9904). */
4521 if (!FRAME_INITIAL_P (f))
4523 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4525 if (EQ (hlinfo->mouse_face_window, window))
4526 hlinfo->mouse_face_window = Qnil;
4529 fset_redisplay (f);
4530 Vwindow_list = Qnil;
4531 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4533 wset_next (w, Qnil); /* Don't delete w->next too. */
4534 free_window_matrices (w);
4536 if (WINDOWP (w->contents))
4538 delete_all_child_windows (w->contents);
4539 wset_combination (w, 0, Qnil);
4541 else
4543 unshow_buffer (w);
4544 unchain_marker (XMARKER (w->pointm));
4545 unchain_marker (XMARKER (w->start));
4546 wset_buffer (w, Qnil);
4549 if (NILP (s->prev) && NILP (s->next))
4550 /* A matrjoshka where SIBLING has become the only child of
4551 PARENT. */
4553 /* Put SIBLING into PARENT's place. */
4554 replace_window (parent, sibling, 0);
4555 /* Have SIBLING inherit the following three slot values from
4556 PARENT (the combination_limit slot is not inherited). */
4557 wset_normal_cols (s, p->normal_cols);
4558 wset_normal_lines (s, p->normal_lines);
4559 /* Mark PARENT as deleted. */
4560 wset_combination (p, 0, Qnil);
4561 /* Try to merge SIBLING into its new parent. */
4562 recombine_windows (sibling);
4565 adjust_frame_glyphs (f);
4567 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4568 /* We deleted the frame's selected window. */
4570 /* Use the frame's first window as fallback ... */
4571 Lisp_Object new_selected_window = Fframe_first_window (frame);
4572 /* ... but preferably use its most recently used window. */
4573 Lisp_Object mru_window;
4575 /* `get-mru-window' might fail for some reason so play it safe
4576 - promote the first window _without recording it_ first. */
4577 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4578 Fselect_window (new_selected_window, Qt);
4579 else
4580 fset_selected_window (f, new_selected_window);
4582 unblock_input ();
4584 /* Now look whether `get-mru-window' gets us something. */
4585 mru_window = call1 (Qget_mru_window, frame);
4586 if (WINDOW_LIVE_P (mru_window)
4587 && EQ (XWINDOW (mru_window)->frame, frame))
4588 new_selected_window = mru_window;
4590 /* If all ended up well, we now promote the mru window. */
4591 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4592 Fselect_window (new_selected_window, Qnil);
4593 else
4594 fset_selected_window (f, new_selected_window);
4596 else
4597 unblock_input ();
4599 /* Must be run by the caller:
4600 run_window_configuration_change_hook (f); */
4602 else
4603 /* We failed: Relink WINDOW into window tree. */
4605 if (before_sibling)
4607 wset_prev (s, window);
4608 wset_combination (p, horflag, window);
4610 else
4612 wset_next (s, window);
4613 if (!NILP (w->next))
4614 wset_prev (XWINDOW (w->next), window);
4616 error ("Deletion failed");
4619 return Qnil;
4622 /***********************************************************************
4623 Resizing Mini-Windows
4624 ***********************************************************************/
4626 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4627 can. */
4628 void
4629 grow_mini_window (struct window *w, int delta, bool pixelwise)
4631 struct frame *f = XFRAME (w->frame);
4632 struct window *r;
4633 Lisp_Object root, height;
4634 int line_height, pixel_height;
4636 eassert (MINI_WINDOW_P (w));
4637 eassert (delta >= 0);
4639 if (delta > 0)
4641 root = FRAME_ROOT_WINDOW (f);
4642 r = XWINDOW (root);
4643 height = call3 (Qwindow_resize_root_window_vertically,
4644 root, make_number (- delta), pixelwise ? Qt : Qnil);
4645 if (INTEGERP (height) && window_resize_check (r, 0))
4647 block_input ();
4648 window_resize_apply (r, 0);
4650 if (pixelwise)
4652 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4653 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4655 else
4657 line_height = min (-XINT (height),
4658 ((INT_MAX - w->pixel_height)
4659 / FRAME_LINE_HEIGHT (f)));
4660 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4663 /* Grow the mini-window. */
4664 w->pixel_top = r->pixel_top + r->pixel_height;
4665 w->top_line = r->top_line + r->total_lines;
4666 /* Make sure the mini-window has always at least one line. */
4667 w->pixel_height = max (w->pixel_height + pixel_height,
4668 FRAME_LINE_HEIGHT (f));
4669 w->total_lines = max (w->total_lines + line_height, 1);
4671 /* Enforce full redisplay of the frame. */
4672 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4673 fset_redisplay (f);
4674 adjust_frame_glyphs (f);
4675 unblock_input ();
4680 /* Shrink mini-window W to one line. */
4681 void
4682 shrink_mini_window (struct window *w, bool pixelwise)
4684 struct frame *f = XFRAME (w->frame);
4685 struct window *r;
4686 Lisp_Object root, delta;
4687 EMACS_INT height, unit;
4689 eassert (MINI_WINDOW_P (w));
4691 height = pixelwise ? w->pixel_height : w->total_lines;
4692 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4693 if (height > unit)
4695 root = FRAME_ROOT_WINDOW (f);
4696 r = XWINDOW (root);
4697 delta = call3 (Qwindow_resize_root_window_vertically,
4698 root, make_number (height - unit),
4699 pixelwise ? Qt : Qnil);
4700 if (INTEGERP (delta) && window_resize_check (r, 0))
4702 block_input ();
4703 window_resize_apply (r, 0);
4705 /* Shrink the mini-window. */
4706 w->top_line = r->top_line + r->total_lines;
4707 w->total_lines = 1;
4708 w->pixel_top = r->pixel_top + r->pixel_height;
4709 w->pixel_height = FRAME_LINE_HEIGHT (f);
4710 /* Enforce full redisplay of the frame. */
4711 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4712 fset_redisplay (f);
4713 adjust_frame_glyphs (f);
4714 unblock_input ();
4716 /* If the above failed for whatever strange reason we must make a
4717 one window frame here. The same routine will be needed when
4718 shrinking the frame (and probably when making the initial
4719 *scratch* window). For the moment leave things as they are. */
4723 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4724 doc: /* Resize minibuffer window WINDOW. */)
4725 (Lisp_Object window)
4727 struct window *w = XWINDOW (window);
4728 struct window *r;
4729 struct frame *f;
4730 int height;
4732 CHECK_WINDOW (window);
4733 f = XFRAME (w->frame);
4735 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4736 error ("Not a valid minibuffer window");
4737 else if (FRAME_MINIBUF_ONLY_P (f))
4738 error ("Cannot resize a minibuffer-only frame");
4740 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4741 height = r->pixel_height + w->pixel_height;
4742 if (window_resize_check (r, 0)
4743 && XINT (w->new_pixel) > 0
4744 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4746 block_input ();
4747 window_resize_apply (r, 0);
4749 w->total_lines = XFASTINT (w->new_total);
4750 w->top_line = r->top_line + r->total_lines;
4751 w->pixel_height = XFASTINT (w->new_pixel);
4752 w->pixel_top = r->pixel_top + r->pixel_height;
4754 fset_redisplay (f);
4755 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4756 adjust_frame_glyphs (f);
4757 unblock_input ();
4758 return Qt;
4760 else
4761 error ("Failed to resize minibuffer window");
4764 /* Mark window cursors off for all windows in the window tree rooted
4765 at W by setting their phys_cursor_on_p flag to zero. Called from
4766 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4767 the frame are cleared. */
4769 void
4770 mark_window_cursors_off (struct window *w)
4772 while (w)
4774 if (WINDOWP (w->contents))
4775 mark_window_cursors_off (XWINDOW (w->contents));
4776 else
4777 w->phys_cursor_on_p = 0;
4779 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4784 /* Return number of lines of text (not counting mode lines) in W. */
4787 window_internal_height (struct window *w)
4789 int ht = w->total_lines;
4791 if (!MINI_WINDOW_P (w))
4793 if (!NILP (w->parent)
4794 || WINDOWP (w->contents)
4795 || !NILP (w->next)
4796 || !NILP (w->prev)
4797 || WINDOW_WANTS_MODELINE_P (w))
4798 --ht;
4800 if (WINDOW_WANTS_HEADER_LINE_P (w))
4801 --ht;
4804 return ht;
4807 /************************************************************************
4808 Window Scrolling
4809 ***********************************************************************/
4811 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4812 N screen-fulls, which is defined as the height of the window minus
4813 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4814 instead. Negative values of N mean scroll down. NOERROR non-zero
4815 means don't signal an error if we try to move over BEGV or ZV,
4816 respectively. */
4818 static void
4819 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4821 immediate_quit = 1;
4822 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4824 wset_redisplay (XWINDOW (window));
4826 /* If we must, use the pixel-based version which is much slower than
4827 the line-based one but can handle varying line heights. */
4828 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4829 window_scroll_pixel_based (window, n, whole, noerror);
4830 else
4831 window_scroll_line_based (window, n, whole, noerror);
4833 /* Bug#15957. */
4834 XWINDOW (window)->window_end_valid = 0;
4835 immediate_quit = 0;
4839 /* Implementation of window_scroll that works based on pixel line
4840 heights. See the comment of window_scroll for parameter
4841 descriptions. */
4843 static void
4844 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4846 struct it it;
4847 struct window *w = XWINDOW (window);
4848 struct text_pos start;
4849 int this_scroll_margin;
4850 /* True if we fiddled the window vscroll field without really scrolling. */
4851 int vscrolled = 0;
4852 int x, y, rtop, rbot, rowh, vpos;
4853 void *itdata = NULL;
4854 int window_total_lines;
4855 int frame_line_height = default_line_pixel_height (w);
4857 SET_TEXT_POS_FROM_MARKER (start, w->start);
4858 /* Scrolling a minibuffer window via scroll bar when the echo area
4859 shows long text sometimes resets the minibuffer contents behind
4860 our backs. */
4861 if (CHARPOS (start) > ZV)
4862 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4864 /* If PT is not visible in WINDOW, move back one half of
4865 the screen. Allow PT to be partially visible, otherwise
4866 something like (scroll-down 1) with PT in the line before
4867 the partially visible one would recenter. */
4869 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4871 itdata = bidi_shelve_cache ();
4872 /* Move backward half the height of the window. Performance note:
4873 vmotion used here is about 10% faster, but would give wrong
4874 results for variable height lines. */
4875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4876 it.current_y = it.last_visible_y;
4877 move_it_vertically_backward (&it, window_box_height (w) / 2);
4879 /* The function move_iterator_vertically may move over more than
4880 the specified y-distance. If it->w is small, e.g. a
4881 mini-buffer window, we may end up in front of the window's
4882 display area. This is the case when Start displaying at the
4883 start of the line containing PT in this case. */
4884 if (it.current_y <= 0)
4886 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4887 move_it_vertically_backward (&it, 0);
4888 it.current_y = 0;
4891 start = it.current.pos;
4892 bidi_unshelve_cache (itdata, 0);
4894 else if (auto_window_vscroll_p)
4896 if (rtop || rbot) /* partially visible */
4898 int px;
4899 int dy = frame_line_height;
4900 if (whole)
4901 dy = max ((window_box_height (w)
4902 - next_screen_context_lines * dy),
4903 dy);
4904 dy *= n;
4906 if (n < 0)
4908 /* Only vscroll backwards if already vscrolled forwards. */
4909 if (w->vscroll < 0 && rtop > 0)
4911 px = max (0, -w->vscroll - min (rtop, -dy));
4912 Fset_window_vscroll (window, make_number (px), Qt);
4913 return;
4916 if (n > 0)
4918 /* Do vscroll if already vscrolled or only display line. */
4919 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4921 px = max (0, -w->vscroll + min (rbot, dy));
4922 Fset_window_vscroll (window, make_number (px), Qt);
4923 return;
4926 /* Maybe modify window start instead of scrolling. */
4927 if (rbot > 0 || w->vscroll < 0)
4929 ptrdiff_t spos;
4931 Fset_window_vscroll (window, make_number (0), Qt);
4932 /* If there are other text lines above the current row,
4933 move window start to current row. Else to next row. */
4934 if (rbot > 0)
4935 spos = XINT (Fline_beginning_position (Qnil));
4936 else
4937 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4938 set_marker_restricted (w->start, make_number (spos),
4939 w->contents);
4940 w->start_at_line_beg = 1;
4941 w->update_mode_line = 1;
4942 /* Set force_start so that redisplay_window will run the
4943 window-scroll-functions. */
4944 w->force_start = 1;
4945 return;
4949 /* Cancel previous vscroll. */
4950 Fset_window_vscroll (window, make_number (0), Qt);
4953 itdata = bidi_shelve_cache ();
4954 /* If scroll_preserve_screen_position is non-nil, we try to set
4955 point in the same window line as it is now, so get that line. */
4956 if (!NILP (Vscroll_preserve_screen_position))
4958 /* We preserve the goal pixel coordinate across consecutive
4959 calls to scroll-up, scroll-down and other commands that
4960 have the `scroll-command' property. This avoids the
4961 possibility of point becoming "stuck" on a tall line when
4962 scrolling by one line. */
4963 if (window_scroll_pixel_based_preserve_y < 0
4964 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4965 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4967 start_display (&it, w, start);
4968 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4969 window_scroll_pixel_based_preserve_y = it.current_y;
4970 window_scroll_pixel_based_preserve_x = it.current_x;
4973 else
4974 window_scroll_pixel_based_preserve_y
4975 = window_scroll_pixel_based_preserve_x = -1;
4977 /* Move iterator it from start the specified distance forward or
4978 backward. The result is the new window start. */
4979 start_display (&it, w, start);
4980 if (whole)
4982 ptrdiff_t start_pos = IT_CHARPOS (it);
4983 int dy = frame_line_height;
4984 dy = max ((window_box_height (w)
4985 - next_screen_context_lines * dy),
4986 dy) * n;
4988 /* Note that move_it_vertically always moves the iterator to the
4989 start of a line. So, if the last line doesn't have a newline,
4990 we would end up at the start of the line ending at ZV. */
4991 if (dy <= 0)
4993 move_it_vertically_backward (&it, -dy);
4994 /* Ensure we actually do move, e.g. in case we are currently
4995 looking at an image that is taller that the window height. */
4996 while (start_pos == IT_CHARPOS (it)
4997 && start_pos > BEGV)
4998 move_it_by_lines (&it, -1);
5000 else if (dy > 0)
5002 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5003 MOVE_TO_POS | MOVE_TO_Y);
5004 /* Ensure we actually do move, e.g. in case we are currently
5005 looking at an image that is taller that the window height. */
5006 while (start_pos == IT_CHARPOS (it)
5007 && start_pos < ZV)
5008 move_it_by_lines (&it, 1);
5011 else
5012 move_it_by_lines (&it, n);
5014 /* We failed if we find ZV is already on the screen (scrolling up,
5015 means there's nothing past the end), or if we can't start any
5016 earlier (scrolling down, means there's nothing past the top). */
5017 if ((n > 0 && IT_CHARPOS (it) == ZV)
5018 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5020 if (IT_CHARPOS (it) == ZV)
5022 if (it.current_y < it.last_visible_y
5023 && (it.current_y + it.max_ascent + it.max_descent
5024 > it.last_visible_y))
5026 /* The last line was only partially visible, make it fully
5027 visible. */
5028 w->vscroll = (it.last_visible_y
5029 - it.current_y + it.max_ascent + it.max_descent);
5030 adjust_frame_glyphs (it.f);
5032 else
5034 bidi_unshelve_cache (itdata, 0);
5035 if (noerror)
5036 return;
5037 else if (n < 0) /* could happen with empty buffers */
5038 xsignal0 (Qbeginning_of_buffer);
5039 else
5040 xsignal0 (Qend_of_buffer);
5043 else
5045 if (w->vscroll != 0)
5046 /* The first line was only partially visible, make it fully
5047 visible. */
5048 w->vscroll = 0;
5049 else
5051 bidi_unshelve_cache (itdata, 0);
5052 if (noerror)
5053 return;
5054 else
5055 xsignal0 (Qbeginning_of_buffer);
5059 /* If control gets here, then we vscrolled. */
5061 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5063 /* Don't try to change the window start below. */
5064 vscrolled = 1;
5067 if (! vscrolled)
5069 ptrdiff_t pos = IT_CHARPOS (it);
5070 ptrdiff_t bytepos;
5072 /* If in the middle of a multi-glyph character move forward to
5073 the next character. */
5074 if (in_display_vector_p (&it))
5076 ++pos;
5077 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5080 /* Set the window start, and set up the window for redisplay. */
5081 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5082 IT_BYTEPOS (it));
5083 bytepos = marker_byte_position (w->start);
5084 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5085 w->update_mode_line = 1;
5086 /* Set force_start so that redisplay_window will run the
5087 window-scroll-functions. */
5088 w->force_start = 1;
5091 /* The rest of this function uses current_y in a nonstandard way,
5092 not including the height of the header line if any. */
5093 it.current_y = it.vpos = 0;
5095 /* Move PT out of scroll margins.
5096 This code wants current_y to be zero at the window start position
5097 even if there is a header line. */
5098 window_total_lines
5099 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5100 this_scroll_margin = max (0, scroll_margin);
5101 this_scroll_margin
5102 = min (this_scroll_margin, window_total_lines / 4);
5103 this_scroll_margin *= frame_line_height;
5105 if (n > 0)
5107 /* We moved the window start towards ZV, so PT may be now
5108 in the scroll margin at the top. */
5109 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5110 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5111 && (NILP (Vscroll_preserve_screen_position)
5112 || EQ (Vscroll_preserve_screen_position, Qt)))
5113 /* We found PT at a legitimate height. Leave it alone. */
5115 else if (window_scroll_pixel_based_preserve_y >= 0)
5117 /* If we have a header line, take account of it.
5118 This is necessary because we set it.current_y to 0, above. */
5119 move_it_to (&it, -1,
5120 window_scroll_pixel_based_preserve_x,
5121 window_scroll_pixel_based_preserve_y
5122 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5123 -1, MOVE_TO_Y | MOVE_TO_X);
5124 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5126 else
5128 while (it.current_y < this_scroll_margin)
5130 int prev = it.current_y;
5131 move_it_by_lines (&it, 1);
5132 if (prev == it.current_y)
5133 break;
5135 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5138 else if (n < 0)
5140 ptrdiff_t charpos, bytepos;
5141 int partial_p;
5143 /* Save our position, for the
5144 window_scroll_pixel_based_preserve_y case. */
5145 charpos = IT_CHARPOS (it);
5146 bytepos = IT_BYTEPOS (it);
5148 /* We moved the window start towards BEGV, so PT may be now
5149 in the scroll margin at the bottom. */
5150 move_it_to (&it, PT, -1,
5151 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5152 - this_scroll_margin - 1),
5154 MOVE_TO_POS | MOVE_TO_Y);
5156 /* Save our position, in case it's correct. */
5157 charpos = IT_CHARPOS (it);
5158 bytepos = IT_BYTEPOS (it);
5160 /* See if point is on a partially visible line at the end. */
5161 if (it.what == IT_EOB)
5162 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5163 else
5165 move_it_by_lines (&it, 1);
5166 partial_p = it.current_y > it.last_visible_y;
5169 if (charpos == PT && !partial_p
5170 && (NILP (Vscroll_preserve_screen_position)
5171 || EQ (Vscroll_preserve_screen_position, Qt)))
5172 /* We found PT before we found the display margin, so PT is ok. */
5174 else if (window_scroll_pixel_based_preserve_y >= 0)
5176 SET_TEXT_POS_FROM_MARKER (start, w->start);
5177 start_display (&it, w, start);
5178 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5179 here because we called start_display again and did not
5180 alter it.current_y this time. */
5181 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5182 window_scroll_pixel_based_preserve_y, -1,
5183 MOVE_TO_Y | MOVE_TO_X);
5184 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5186 else
5188 if (partial_p)
5189 /* The last line was only partially visible, so back up two
5190 lines to make sure we're on a fully visible line. */
5192 move_it_by_lines (&it, -2);
5193 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5195 else
5196 /* No, the position we saved is OK, so use it. */
5197 SET_PT_BOTH (charpos, bytepos);
5200 bidi_unshelve_cache (itdata, 0);
5204 /* Implementation of window_scroll that works based on screen lines.
5205 See the comment of window_scroll for parameter descriptions. */
5207 static void
5208 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5210 register struct window *w = XWINDOW (window);
5211 /* Fvertical_motion enters redisplay, which can trigger
5212 fontification, which in turn can modify buffer text (e.g., if the
5213 fontification functions replace escape sequences with faces, as
5214 in `grep-mode-font-lock-keywords'). So we use a marker to record
5215 the old point position, to prevent crashes in SET_PT_BOTH. */
5216 Lisp_Object opoint_marker = Fpoint_marker ();
5217 register ptrdiff_t pos, pos_byte;
5218 register int ht = window_internal_height (w);
5219 register Lisp_Object tem;
5220 bool lose;
5221 Lisp_Object bolp;
5222 ptrdiff_t startpos = marker_position (w->start);
5223 ptrdiff_t startbyte = marker_byte_position (w->start);
5224 Lisp_Object original_pos = Qnil;
5226 /* If scrolling screen-fulls, compute the number of lines to
5227 scroll from the window's height. */
5228 if (whole)
5229 n *= max (1, ht - next_screen_context_lines);
5231 if (!NILP (Vscroll_preserve_screen_position))
5233 if (window_scroll_preserve_vpos <= 0
5234 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5235 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5237 struct position posit
5238 = *compute_motion (startpos, startbyte, 0, 0, 0,
5239 PT, ht, 0, -1, w->hscroll, 0, w);
5240 window_scroll_preserve_vpos = posit.vpos;
5241 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5244 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5245 make_number (window_scroll_preserve_vpos));
5248 XSETFASTINT (tem, PT);
5249 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5251 if (NILP (tem))
5253 Fvertical_motion (make_number (- (ht / 2)), window);
5254 startpos = PT;
5255 startbyte = PT_BYTE;
5258 SET_PT_BOTH (startpos, startbyte);
5259 lose = n < 0 && PT == BEGV;
5260 Fvertical_motion (make_number (n), window);
5261 pos = PT;
5262 pos_byte = PT_BYTE;
5263 bolp = Fbolp ();
5264 SET_PT_BOTH (marker_position (opoint_marker),
5265 marker_byte_position (opoint_marker));
5267 if (lose)
5269 if (noerror)
5270 return;
5271 else
5272 xsignal0 (Qbeginning_of_buffer);
5275 if (pos < ZV)
5277 /* Don't use a scroll margin that is negative or too large. */
5278 int this_scroll_margin =
5279 max (0, min (scroll_margin, w->total_lines / 4));
5281 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5282 w->start_at_line_beg = !NILP (bolp);
5283 w->update_mode_line = 1;
5284 /* Set force_start so that redisplay_window will run
5285 the window-scroll-functions. */
5286 w->force_start = 1;
5288 if (!NILP (Vscroll_preserve_screen_position)
5289 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5291 SET_PT_BOTH (pos, pos_byte);
5292 Fvertical_motion (original_pos, window);
5294 /* If we scrolled forward, put point enough lines down
5295 that it is outside the scroll margin. */
5296 else if (n > 0)
5298 int top_margin;
5300 if (this_scroll_margin > 0)
5302 SET_PT_BOTH (pos, pos_byte);
5303 Fvertical_motion (make_number (this_scroll_margin), window);
5304 top_margin = PT;
5306 else
5307 top_margin = pos;
5309 if (top_margin <= marker_position (opoint_marker))
5310 SET_PT_BOTH (marker_position (opoint_marker),
5311 marker_byte_position (opoint_marker));
5312 else if (!NILP (Vscroll_preserve_screen_position))
5314 SET_PT_BOTH (pos, pos_byte);
5315 Fvertical_motion (original_pos, window);
5317 else
5318 SET_PT (top_margin);
5320 else if (n < 0)
5322 int bottom_margin;
5324 /* If we scrolled backward, put point near the end of the window
5325 but not within the scroll margin. */
5326 SET_PT_BOTH (pos, pos_byte);
5327 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5328 if (XFASTINT (tem) == ht - this_scroll_margin)
5329 bottom_margin = PT;
5330 else
5331 bottom_margin = PT + 1;
5333 if (bottom_margin > marker_position (opoint_marker))
5334 SET_PT_BOTH (marker_position (opoint_marker),
5335 marker_byte_position (opoint_marker));
5336 else
5338 if (!NILP (Vscroll_preserve_screen_position))
5340 SET_PT_BOTH (pos, pos_byte);
5341 Fvertical_motion (original_pos, window);
5343 else
5344 Fvertical_motion (make_number (-1), window);
5348 else
5350 if (noerror)
5351 return;
5352 else
5353 xsignal0 (Qend_of_buffer);
5358 /* Scroll selected_window up or down. If N is nil, scroll a
5359 screen-full which is defined as the height of the window minus
5360 next_screen_context_lines. If N is the symbol `-', scroll.
5361 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5362 up. This is the guts of Fscroll_up and Fscroll_down. */
5364 static void
5365 scroll_command (Lisp_Object n, int direction)
5367 ptrdiff_t count = SPECPDL_INDEX ();
5369 eassert (eabs (direction) == 1);
5371 /* If selected window's buffer isn't current, make it current for
5372 the moment. But don't screw up if window_scroll gets an error. */
5373 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5375 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5376 Fset_buffer (XWINDOW (selected_window)->contents);
5379 if (NILP (n))
5380 window_scroll (selected_window, direction, 1, 0);
5381 else if (EQ (n, Qminus))
5382 window_scroll (selected_window, -direction, 1, 0);
5383 else
5385 n = Fprefix_numeric_value (n);
5386 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5389 unbind_to (count, Qnil);
5392 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5393 doc: /* Scroll text of selected window upward ARG lines.
5394 If ARG is omitted or nil, scroll upward by a near full screen.
5395 A near full screen is `next-screen-context-lines' less than a full screen.
5396 Negative ARG means scroll downward.
5397 If ARG is the atom `-', scroll downward by nearly full screen.
5398 When calling from a program, supply as argument a number, nil, or `-'. */)
5399 (Lisp_Object arg)
5401 scroll_command (arg, 1);
5402 return Qnil;
5405 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5406 doc: /* Scroll text of selected window down ARG lines.
5407 If ARG is omitted or nil, scroll down 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 upward.
5410 If ARG is the atom `-', scroll upward 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 ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5419 doc: /* Return the other window for \"other window scroll\" commands.
5420 If `other-window-scroll-buffer' is non-nil, a window
5421 showing that buffer is used.
5422 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5423 specifies the window. This takes precedence over
5424 `other-window-scroll-buffer'. */)
5425 (void)
5427 Lisp_Object window;
5429 if (MINI_WINDOW_P (XWINDOW (selected_window))
5430 && !NILP (Vminibuf_scroll_window))
5431 window = Vminibuf_scroll_window;
5432 /* If buffer is specified, scroll that buffer. */
5433 else if (!NILP (Vother_window_scroll_buffer))
5435 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5436 if (NILP (window))
5437 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5439 else
5441 /* Nothing specified; look for a neighboring window on the same
5442 frame. */
5443 window = Fnext_window (selected_window, Qnil, Qnil);
5445 if (EQ (window, selected_window))
5446 /* That didn't get us anywhere; look for a window on another
5447 visible frame. */
5449 window = Fnext_window (window, Qnil, Qt);
5450 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5451 && ! EQ (window, selected_window));
5454 CHECK_LIVE_WINDOW (window);
5456 if (EQ (window, selected_window))
5457 error ("There is no other window");
5459 return window;
5462 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5463 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5464 A near full screen is `next-screen-context-lines' less than a full screen.
5465 The next window is the one below the current one; or the one at the top
5466 if the current one is at the bottom. Negative ARG means scroll downward.
5467 If ARG is the atom `-', scroll downward by nearly full screen.
5468 When calling from a program, supply as argument a number, nil, or `-'.
5470 If `other-window-scroll-buffer' is non-nil, scroll the window
5471 showing that buffer, popping the buffer up if necessary.
5472 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5473 specifies the window to scroll. This takes precedence over
5474 `other-window-scroll-buffer'. */)
5475 (Lisp_Object arg)
5477 Lisp_Object window;
5478 struct window *w;
5479 ptrdiff_t count = SPECPDL_INDEX ();
5481 window = Fother_window_for_scrolling ();
5482 w = XWINDOW (window);
5484 /* Don't screw up if window_scroll gets an error. */
5485 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5487 Fset_buffer (w->contents);
5488 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5490 if (NILP (arg))
5491 window_scroll (window, 1, 1, 1);
5492 else if (EQ (arg, Qminus))
5493 window_scroll (window, -1, 1, 1);
5494 else
5496 if (CONSP (arg))
5497 arg = XCAR (arg);
5498 CHECK_NUMBER (arg);
5499 window_scroll (window, XINT (arg), 0, 1);
5502 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5503 unbind_to (count, Qnil);
5505 return Qnil;
5508 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5509 doc: /* Scroll selected window display ARG columns left.
5510 Default for ARG is window width minus 2.
5511 Value is the total amount of leftward horizontal scrolling in
5512 effect after the change.
5513 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5514 lower bound for automatic scrolling, i.e. automatic scrolling
5515 will not scroll a window to a column less than the value returned
5516 by this function. This happens in an interactive call. */)
5517 (register Lisp_Object arg, Lisp_Object set_minimum)
5519 struct window *w = XWINDOW (selected_window);
5520 EMACS_INT requested_arg = (NILP (arg)
5521 ? window_body_width (w, 0) - 2
5522 : XINT (Fprefix_numeric_value (arg)));
5523 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5525 if (!NILP (set_minimum))
5526 w->min_hscroll = w->hscroll;
5528 return result;
5531 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5532 doc: /* Scroll selected window display ARG columns right.
5533 Default for ARG is window width minus 2.
5534 Value is the total amount of leftward horizontal scrolling in
5535 effect after the change.
5536 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5537 lower bound for automatic scrolling, i.e. automatic scrolling
5538 will not scroll a window to a column less than the value returned
5539 by this function. This happens in an interactive call. */)
5540 (register Lisp_Object arg, Lisp_Object set_minimum)
5542 struct window *w = XWINDOW (selected_window);
5543 EMACS_INT requested_arg = (NILP (arg)
5544 ? window_body_width (w, 0) - 2
5545 : XINT (Fprefix_numeric_value (arg)));
5546 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5548 if (!NILP (set_minimum))
5549 w->min_hscroll = w->hscroll;
5551 return result;
5554 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5555 doc: /* Return the window which was selected when entering the minibuffer.
5556 Returns nil, if selected window is not a minibuffer window. */)
5557 (void)
5559 if (minibuf_level > 0
5560 && MINI_WINDOW_P (XWINDOW (selected_window))
5561 && WINDOW_LIVE_P (minibuf_selected_window))
5562 return minibuf_selected_window;
5564 return Qnil;
5567 /* Value is the number of lines actually displayed in window W,
5568 as opposed to its height. */
5570 static int
5571 displayed_window_lines (struct window *w)
5573 struct it it;
5574 struct text_pos start;
5575 int height = window_box_height (w);
5576 struct buffer *old_buffer;
5577 int bottom_y;
5578 void *itdata = NULL;
5580 if (XBUFFER (w->contents) != current_buffer)
5582 old_buffer = current_buffer;
5583 set_buffer_internal (XBUFFER (w->contents));
5585 else
5586 old_buffer = NULL;
5588 /* In case W->start is out of the accessible range, do something
5589 reasonable. This happens in Info mode when Info-scroll-down
5590 calls (recenter -1) while W->start is 1. */
5591 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5593 itdata = bidi_shelve_cache ();
5594 start_display (&it, w, start);
5595 move_it_vertically (&it, height);
5596 bottom_y = line_bottom_y (&it);
5597 bidi_unshelve_cache (itdata, 0);
5599 /* rms: On a non-window display,
5600 the value of it.vpos at the bottom of the screen
5601 seems to be 1 larger than window_box_height (w).
5602 This kludge fixes a bug whereby (move-to-window-line -1)
5603 when ZV is on the last screen line
5604 moves to the previous screen line instead of the last one. */
5605 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5606 height++;
5608 /* Add in empty lines at the bottom of the window. */
5609 if (bottom_y < height)
5611 int uy = FRAME_LINE_HEIGHT (it.f);
5612 it.vpos += (height - bottom_y + uy - 1) / uy;
5615 if (old_buffer)
5616 set_buffer_internal (old_buffer);
5618 return it.vpos;
5622 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5623 doc: /* Center point in selected window and maybe redisplay frame.
5624 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5625 relative to the selected window. If ARG is negative, it counts up from the
5626 bottom of the window. (ARG should be less than the height of the window.)
5628 If ARG is omitted or nil, then recenter with point on the middle line of
5629 the selected window; if the variable `recenter-redisplay' is non-nil,
5630 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5631 is set to `grow-only', this resets the tool-bar's height to the minimum
5632 height needed); if `recenter-redisplay' has the special value `tty',
5633 then only tty frames are redrawn.
5635 Just C-u as prefix means put point in the center of the window
5636 and redisplay normally--don't erase and redraw the frame. */)
5637 (register Lisp_Object arg)
5639 struct window *w = XWINDOW (selected_window);
5640 struct buffer *buf = XBUFFER (w->contents);
5641 struct buffer *obuf = current_buffer;
5642 bool center_p = 0;
5643 ptrdiff_t charpos, bytepos;
5644 EMACS_INT iarg IF_LINT (= 0);
5645 int this_scroll_margin;
5647 /* If redisplay is suppressed due to an error, try again. */
5648 obuf->display_error_modiff = 0;
5650 if (NILP (arg))
5652 if (!NILP (Vrecenter_redisplay)
5653 && (!EQ (Vrecenter_redisplay, Qtty)
5654 || !NILP (Ftty_type (selected_frame))))
5656 ptrdiff_t i;
5658 /* Invalidate pixel data calculated for all compositions. */
5659 for (i = 0; i < n_compositions; i++)
5660 composition_table[i]->font = NULL;
5661 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5662 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5663 #endif
5664 Fredraw_frame (WINDOW_FRAME (w));
5665 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5668 center_p = 1;
5670 else if (CONSP (arg)) /* Just C-u. */
5671 center_p = 1;
5672 else
5674 arg = Fprefix_numeric_value (arg);
5675 CHECK_NUMBER (arg);
5676 iarg = XINT (arg);
5679 set_buffer_internal (buf);
5681 /* Do this after making BUF current
5682 in case scroll_margin is buffer-local. */
5683 this_scroll_margin =
5684 max (0, min (scroll_margin, w->total_lines / 4));
5686 /* Handle centering on a graphical frame specially. Such frames can
5687 have variable-height lines and centering point on the basis of
5688 line counts would lead to strange effects. */
5689 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5691 if (center_p)
5693 struct it it;
5694 struct text_pos pt;
5695 void *itdata = bidi_shelve_cache ();
5697 SET_TEXT_POS (pt, PT, PT_BYTE);
5698 start_display (&it, w, pt);
5699 move_it_vertically_backward (&it, window_box_height (w) / 2);
5700 charpos = IT_CHARPOS (it);
5701 bytepos = IT_BYTEPOS (it);
5702 bidi_unshelve_cache (itdata, 0);
5704 else if (iarg < 0)
5706 struct it it;
5707 struct text_pos pt;
5708 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5709 int extra_line_spacing;
5710 int h = window_box_height (w);
5711 void *itdata = bidi_shelve_cache ();
5713 iarg = - max (-iarg, this_scroll_margin);
5715 SET_TEXT_POS (pt, PT, PT_BYTE);
5716 start_display (&it, w, pt);
5718 /* Be sure we have the exact height of the full line containing PT. */
5719 move_it_by_lines (&it, 0);
5721 /* The amount of pixels we have to move back is the window
5722 height minus what's displayed in the line containing PT,
5723 and the lines below. */
5724 it.current_y = 0;
5725 it.vpos = 0;
5726 move_it_by_lines (&it, nlines);
5728 if (it.vpos == nlines)
5729 h -= it.current_y;
5730 else
5732 /* Last line has no newline */
5733 h -= line_bottom_y (&it);
5734 it.vpos++;
5737 /* Don't reserve space for extra line spacing of last line. */
5738 extra_line_spacing = it.max_extra_line_spacing;
5740 /* If we can't move down NLINES lines because we hit
5741 the end of the buffer, count in some empty lines. */
5742 if (it.vpos < nlines)
5744 nlines -= it.vpos;
5745 extra_line_spacing = it.extra_line_spacing;
5746 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5748 if (h <= 0)
5750 bidi_unshelve_cache (itdata, 0);
5751 return Qnil;
5754 /* Now find the new top line (starting position) of the window. */
5755 start_display (&it, w, pt);
5756 it.current_y = 0;
5757 move_it_vertically_backward (&it, h);
5759 /* If extra line spacing is present, we may move too far
5760 back. This causes the last line to be only partially
5761 visible (which triggers redisplay to recenter that line
5762 in the middle), so move forward.
5763 But ignore extra line spacing on last line, as it is not
5764 considered to be part of the visible height of the line.
5766 h += extra_line_spacing;
5767 while (-it.current_y > h)
5768 move_it_by_lines (&it, 1);
5770 charpos = IT_CHARPOS (it);
5771 bytepos = IT_BYTEPOS (it);
5773 bidi_unshelve_cache (itdata, 0);
5775 else
5777 struct position pos;
5779 iarg = max (iarg, this_scroll_margin);
5781 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5782 charpos = pos.bufpos;
5783 bytepos = pos.bytepos;
5786 else
5788 struct position pos;
5789 int ht = window_internal_height (w);
5791 if (center_p)
5792 iarg = ht / 2;
5793 else if (iarg < 0)
5794 iarg += ht;
5796 /* Don't let it get into the margin at either top or bottom. */
5797 iarg = clip_to_bounds (this_scroll_margin, iarg,
5798 ht - this_scroll_margin - 1);
5800 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5801 charpos = pos.bufpos;
5802 bytepos = pos.bytepos;
5805 /* Set the new window start. */
5806 set_marker_both (w->start, w->contents, charpos, bytepos);
5807 w->window_end_valid = 0;
5809 w->optional_new_start = 1;
5811 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5812 FETCH_BYTE (bytepos - 1) == '\n');
5814 set_buffer_internal (obuf);
5815 return Qnil;
5818 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5819 0, 1, 0,
5820 doc: /* Return the width in columns of the text display area of WINDOW.
5821 WINDOW must be a live window and defaults to the selected one.
5823 The returned width does not include dividers, scrollbars, margins,
5824 fringes, nor any partial-width columns at the right of the text
5825 area. */)
5826 (Lisp_Object window)
5828 struct window *w = decode_live_window (window);
5830 return make_number (window_box_width (w, TEXT_AREA)
5831 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5834 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5835 0, 1, 0,
5836 doc: /* Return the height in lines of the text display area of WINDOW.
5837 WINDOW must be a live window and defaults to the selected one.
5839 The returned height does not include dividers, the mode line, any header
5840 line, nor any partial-height lines at the bottom of the text area. */)
5841 (Lisp_Object window)
5843 struct window *w = decode_live_window (window);
5845 return make_number (window_box_height (w)
5846 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5849 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5850 1, 1, "P",
5851 doc: /* Position point relative to window.
5852 ARG nil means position point at center of window.
5853 Else, ARG specifies vertical position within the window;
5854 zero means top of window, negative means relative to bottom of window. */)
5855 (Lisp_Object arg)
5857 struct window *w = XWINDOW (selected_window);
5858 int lines, start;
5859 Lisp_Object window;
5860 #if 0
5861 int this_scroll_margin;
5862 #endif
5864 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5865 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5866 when passed below to set_marker_both. */
5867 error ("move-to-window-line called from unrelated buffer");
5869 window = selected_window;
5870 start = marker_position (w->start);
5871 if (start < BEGV || start > ZV)
5873 int height = window_internal_height (w);
5874 Fvertical_motion (make_number (- (height / 2)), window);
5875 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5876 w->start_at_line_beg = !NILP (Fbolp ());
5877 w->force_start = 1;
5879 else
5880 Fgoto_char (w->start);
5882 lines = displayed_window_lines (w);
5884 #if 0
5885 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5886 #endif
5888 if (NILP (arg))
5889 XSETFASTINT (arg, lines / 2);
5890 else
5892 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5894 if (iarg < 0)
5895 iarg = iarg + lines;
5897 #if 0 /* This code would prevent move-to-window-line from moving point
5898 to a place inside the scroll margins (which would cause the
5899 next redisplay to scroll). I wrote this code, but then concluded
5900 it is probably better not to install it. However, it is here
5901 inside #if 0 so as not to lose it. -- rms. */
5903 /* Don't let it get into the margin at either top or bottom. */
5904 iarg = max (iarg, this_scroll_margin);
5905 iarg = min (iarg, lines - this_scroll_margin - 1);
5906 #endif
5908 arg = make_number (iarg);
5911 /* Skip past a partially visible first line. */
5912 if (w->vscroll)
5913 XSETINT (arg, XINT (arg) + 1);
5915 return Fvertical_motion (arg, window);
5920 /***********************************************************************
5921 Window Configuration
5922 ***********************************************************************/
5924 struct save_window_data
5926 struct vectorlike_header header;
5927 Lisp_Object selected_frame;
5928 Lisp_Object current_window;
5929 Lisp_Object current_buffer;
5930 Lisp_Object minibuf_scroll_window;
5931 Lisp_Object minibuf_selected_window;
5932 Lisp_Object root_window;
5933 Lisp_Object focus_frame;
5934 /* A vector, each of whose elements is a struct saved_window
5935 for one window. */
5936 Lisp_Object saved_windows;
5938 /* All fields above are traced by the GC.
5939 From `frame-cols' down, the fields are ignored by the GC. */
5940 /* We should be able to do without the following two. */
5941 int frame_cols, frame_lines;
5942 /* These two should get eventually replaced by their pixel
5943 counterparts. */
5944 int frame_menu_bar_lines, frame_tool_bar_lines;
5945 int frame_text_width, frame_text_height;
5946 /* These are currently unused. We need them as soon as we convert
5947 to pixels. */
5948 int frame_menu_bar_height, frame_tool_bar_height;
5951 /* This is saved as a Lisp_Vector */
5952 struct saved_window
5954 struct vectorlike_header header;
5956 Lisp_Object window, buffer, start, pointm, mark;
5957 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5958 Lisp_Object left_col, top_line, total_cols, total_lines;
5959 Lisp_Object normal_cols, normal_lines;
5960 Lisp_Object hscroll, min_hscroll;
5961 Lisp_Object parent, prev;
5962 Lisp_Object start_at_line_beg;
5963 Lisp_Object display_table;
5964 Lisp_Object left_margin_cols, right_margin_cols;
5965 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5966 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5967 Lisp_Object combination_limit, window_parameters;
5970 #define SAVED_WINDOW_N(swv,n) \
5971 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5973 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5974 doc: /* Return t if OBJECT is a window-configuration object. */)
5975 (Lisp_Object object)
5977 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5980 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5981 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5982 (Lisp_Object config)
5984 register struct save_window_data *data;
5985 struct Lisp_Vector *saved_windows;
5987 CHECK_WINDOW_CONFIGURATION (config);
5989 data = (struct save_window_data *) XVECTOR (config);
5990 saved_windows = XVECTOR (data->saved_windows);
5991 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5994 /* From Chong's unwind_create_frame_1. */
5995 static void
5996 unwind_change_frame (Lisp_Object val)
5998 inhibit_lisp_code = val;
6001 DEFUN ("set-window-configuration", Fset_window_configuration,
6002 Sset_window_configuration, 1, 1, 0,
6003 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6004 CONFIGURATION must be a value previously returned
6005 by `current-window-configuration' (which see).
6006 If CONFIGURATION was made from a frame that is now deleted,
6007 only frame-independent values can be restored. In this case,
6008 the return value is nil. Otherwise the value is t. */)
6009 (Lisp_Object configuration)
6011 register struct save_window_data *data;
6012 struct Lisp_Vector *saved_windows;
6013 Lisp_Object new_current_buffer;
6014 Lisp_Object frame;
6015 struct frame *f;
6016 ptrdiff_t old_point = -1;
6018 CHECK_WINDOW_CONFIGURATION (configuration);
6020 data = (struct save_window_data *) XVECTOR (configuration);
6021 saved_windows = XVECTOR (data->saved_windows);
6023 new_current_buffer = data->current_buffer;
6024 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6025 new_current_buffer = Qnil;
6026 else
6028 if (XBUFFER (new_current_buffer) == current_buffer)
6029 /* The code further down "preserves point" by saving here PT in
6030 old_point and then setting it later back into PT. When the
6031 current-selected-window and the final-selected-window both show
6032 the current buffer, this suffers from the problem that the
6033 current PT is the window-point of the current-selected-window,
6034 while the final PT is the point of the final-selected-window, so
6035 this copy from one PT to the other would end up moving the
6036 window-point of the final-selected-window to the window-point of
6037 the current-selected-window. So we have to be careful which
6038 point of the current-buffer we copy into old_point. */
6039 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6040 && WINDOWP (selected_window)
6041 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6042 && !EQ (selected_window, data->current_window))
6043 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6044 else
6045 old_point = PT;
6046 else
6047 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6048 point in new_current_buffer as of the last time this buffer was
6049 used. This can be non-deterministic since it can be changed by
6050 things like jit-lock by mere temporary selection of some random
6051 window that happens to show this buffer.
6052 So if possible we want this arbitrary choice of "which point" to
6053 be the one from the to-be-selected-window so as to prevent this
6054 window's cursor from being copied from another window. */
6055 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6056 /* If current_window = selected_window, its point is in BUF_PT. */
6057 && !EQ (selected_window, data->current_window))
6058 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6059 else
6060 old_point = BUF_PT (XBUFFER (new_current_buffer));
6063 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6064 f = XFRAME (frame);
6066 /* If f is a dead frame, don't bother rebuilding its window tree.
6067 However, there is other stuff we should still try to do below. */
6068 if (FRAME_LIVE_P (f))
6070 Lisp_Object window;
6071 Lisp_Object dead_windows = Qnil;
6072 register Lisp_Object tem, par, pers;
6073 register struct window *w;
6074 register struct saved_window *p;
6075 struct window *root_window;
6076 struct window **leaf_windows;
6077 int n_leaf_windows;
6078 ptrdiff_t k;
6079 int i, n;
6080 ptrdiff_t count = SPECPDL_INDEX ();
6081 /* If the frame has been resized since this window configuration was
6082 made, we change the frame to the size specified in the
6083 configuration, restore the configuration, and then resize it
6084 back. We keep track of the prevailing height in these variables. */
6085 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6086 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6087 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6088 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6089 /* int previous_frame_lines = FRAME_LINES (f); */
6090 /* int previous_frame_cols = FRAME_COLS (f); */
6091 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6092 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6094 /* Don't do this within the main loop below: This may call Lisp
6095 code and is thus potentially unsafe while input is blocked. */
6096 for (k = 0; k < saved_windows->header.size; k++)
6098 p = SAVED_WINDOW_N (saved_windows, k);
6099 window = p->window;
6100 w = XWINDOW (window);
6101 if (BUFFERP (w->contents)
6102 && !EQ (w->contents, p->buffer)
6103 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6104 /* If a window we restore gets another buffer, record the
6105 window's old buffer. */
6106 call1 (Qrecord_window_buffer, window);
6109 /* Don't run lisp in the following segment since the frame is in a
6110 completely inconsistent state. See Bug#16207. */
6111 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6112 inhibit_lisp_code = Qt;
6113 /* The mouse highlighting code could get screwed up
6114 if it runs during this. */
6115 block_input ();
6117 if (data->frame_text_width != previous_frame_text_width
6118 || data->frame_text_height != previous_frame_text_height)
6119 change_frame_size (f, data->frame_text_width,
6120 data->frame_text_height, 0, 0, 0, 1);
6122 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6124 #ifdef HAVE_WINDOW_SYSTEM
6125 if (FRAME_WINDOW_P (f))
6126 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6127 make_number (0));
6128 else /* TTY or MSDOS */
6129 #endif
6130 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6131 make_number (0));
6133 #ifdef HAVE_WINDOW_SYSTEM
6134 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6135 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6136 make_number (0));
6137 #endif
6139 /* "Swap out" point from the selected window's buffer
6140 into the window itself. (Normally the pointm of the selected
6141 window holds garbage.) We do this now, before
6142 restoring the window contents, and prevent it from
6143 being done later on when we select a new window. */
6144 if (! NILP (XWINDOW (selected_window)->contents))
6146 w = XWINDOW (selected_window);
6147 set_marker_both (w->pointm,
6148 w->contents,
6149 BUF_PT (XBUFFER (w->contents)),
6150 BUF_PT_BYTE (XBUFFER (w->contents)));
6153 fset_redisplay (f);
6154 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6156 /* Problem: Freeing all matrices and later allocating them again
6157 is a serious redisplay flickering problem. What we would
6158 really like to do is to free only those matrices not reused
6159 below. */
6160 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6161 leaf_windows = alloca (count_windows (root_window)
6162 * sizeof *leaf_windows);
6163 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6165 /* Kludge Alert!
6166 Mark all windows now on frame as "deleted".
6167 Restoring the new configuration "undeletes" any that are in it.
6169 Save their current buffers in their height fields, since we may
6170 need it later, if a buffer saved in the configuration is now
6171 dead. */
6172 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6174 for (k = 0; k < saved_windows->header.size; k++)
6176 p = SAVED_WINDOW_N (saved_windows, k);
6177 window = p->window;
6178 w = XWINDOW (window);
6179 wset_next (w, Qnil);
6181 if (!NILP (p->parent))
6182 wset_parent
6183 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6184 else
6185 wset_parent (w, Qnil);
6187 if (!NILP (p->prev))
6189 wset_prev
6190 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6191 wset_next (XWINDOW (w->prev), p->window);
6193 else
6195 wset_prev (w, Qnil);
6196 if (!NILP (w->parent))
6197 wset_combination (XWINDOW (w->parent),
6198 (XINT (p->total_cols)
6199 != XWINDOW (w->parent)->total_cols),
6200 p->window);
6203 /* If we squirreled away the buffer, restore it now. */
6204 if (BUFFERP (w->combination_limit))
6205 wset_buffer (w, w->combination_limit);
6206 w->pixel_left = XFASTINT (p->pixel_left);
6207 w->pixel_top = XFASTINT (p->pixel_top);
6208 w->pixel_width = XFASTINT (p->pixel_width);
6209 w->pixel_height = XFASTINT (p->pixel_height);
6210 w->left_col = XFASTINT (p->left_col);
6211 w->top_line = XFASTINT (p->top_line);
6212 w->total_cols = XFASTINT (p->total_cols);
6213 w->total_lines = XFASTINT (p->total_lines);
6214 wset_normal_cols (w, p->normal_cols);
6215 wset_normal_lines (w, p->normal_lines);
6216 w->hscroll = XFASTINT (p->hscroll);
6217 w->min_hscroll = XFASTINT (p->min_hscroll);
6218 wset_display_table (w, p->display_table);
6219 w->left_margin_cols = XINT (p->left_margin_cols);
6220 w->right_margin_cols = XINT (p->right_margin_cols);
6221 w->left_fringe_width = XINT (p->left_fringe_width);
6222 w->right_fringe_width = XINT (p->right_fringe_width);
6223 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6224 w->scroll_bar_width = XINT (p->scroll_bar_width);
6225 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6226 wset_dedicated (w, p->dedicated);
6227 wset_combination_limit (w, p->combination_limit);
6228 /* Restore any window parameters that have been saved.
6229 Parameters that have not been saved are left alone. */
6230 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6232 pers = XCAR (tem);
6233 if (CONSP (pers))
6235 if (NILP (XCDR (pers)))
6237 par = Fassq (XCAR (pers), w->window_parameters);
6238 if (CONSP (par) && !NILP (XCDR (par)))
6239 /* Reset a parameter to nil if and only if it
6240 has a non-nil association. Don't make new
6241 associations. */
6242 Fsetcdr (par, Qnil);
6244 else
6245 /* Always restore a non-nil value. */
6246 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6250 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6251 /* If saved buffer is alive, install it. */
6253 wset_buffer (w, p->buffer);
6254 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6255 set_marker_restricted (w->start, p->start, w->contents);
6256 set_marker_restricted (w->pointm, p->pointm,
6257 w->contents);
6258 Fset_marker (BVAR (XBUFFER (w->contents), mark),
6259 p->mark, w->contents);
6261 /* As documented in Fcurrent_window_configuration, don't
6262 restore the location of point in the buffer which was
6263 current when the window configuration was recorded. */
6264 if (!EQ (p->buffer, new_current_buffer)
6265 && XBUFFER (p->buffer) == current_buffer)
6266 Fgoto_char (w->pointm);
6268 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6269 /* Keep window's old buffer; make sure the markers are real. */
6271 /* Set window markers at start of visible range. */
6272 if (XMARKER (w->start)->buffer == 0)
6273 set_marker_restricted_both (w->start, w->contents, 0, 0);
6274 if (XMARKER (w->pointm)->buffer == 0)
6275 set_marker_restricted_both
6276 (w->pointm, w->contents,
6277 BUF_PT (XBUFFER (w->contents)),
6278 BUF_PT_BYTE (XBUFFER (w->contents)));
6279 w->start_at_line_beg = 1;
6281 else if (!NILP (w->start))
6282 /* Leaf window has no live buffer, get one. */
6284 /* Get the buffer via other_buffer_safely in order to
6285 avoid showing an unimportant buffer and, if necessary, to
6286 recreate *scratch* in the course (part of Juanma's bs-show
6287 scenario from March 2011). */
6288 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6289 /* This will set the markers to beginning of visible
6290 range. */
6291 set_marker_restricted_both (w->start, w->contents, 0, 0);
6292 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6293 w->start_at_line_beg = 1;
6294 if (!NILP (w->dedicated))
6295 /* Record this window as dead. */
6296 dead_windows = Fcons (window, dead_windows);
6297 /* Make sure window is no more dedicated. */
6298 wset_dedicated (w, Qnil);
6302 fset_root_window (f, data->root_window);
6303 /* Arrange *not* to restore point in the buffer that was
6304 current when the window configuration was saved. */
6305 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6306 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6307 make_number (old_point),
6308 XWINDOW (data->current_window)->contents);
6310 /* In the following call to `select-window', prevent "swapping out
6311 point" in the old selected window using the buffer that has
6312 been restored into it. We already swapped out that point from
6313 that window's old buffer.
6315 Do not record the buffer here. We do that in a separate call
6316 to select_window below. See also Bug#16207. */
6317 select_window (data->current_window, Qt, 1);
6318 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6319 last_selected_window)
6320 = selected_window;
6322 if (NILP (data->focus_frame)
6323 || (FRAMEP (data->focus_frame)
6324 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6325 Fredirect_frame_focus (frame, data->focus_frame);
6327 /* Set the frame size to the value it had before this function. */
6328 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6329 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6330 change_frame_size (f, previous_frame_text_width,
6331 previous_frame_text_height, 0, 0, 0, 1);
6333 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6335 #ifdef HAVE_WINDOW_SYSTEM
6336 if (FRAME_WINDOW_P (f))
6337 x_set_menu_bar_lines (f,
6338 make_number (previous_frame_menu_bar_lines),
6339 make_number (0));
6340 else /* TTY or MSDOS */
6341 #endif
6342 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6343 make_number (0));
6345 #ifdef HAVE_WINDOW_SYSTEM
6346 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6347 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6348 make_number (0));
6349 #endif
6351 /* Now, free glyph matrices in windows that were not reused. */
6352 for (i = n = 0; i < n_leaf_windows; ++i)
6354 if (NILP (leaf_windows[i]->contents))
6355 free_window_matrices (leaf_windows[i]);
6356 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6357 ++n;
6360 adjust_frame_glyphs (f);
6361 unblock_input ();
6362 unbind_to (count, Qnil);
6364 /* Scan dead buffer windows. */
6365 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6367 window = XCAR (dead_windows);
6368 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6369 delete_deletable_window (window);
6372 /* Record the selected window's buffer here. The window should
6373 already be the selected one from the call above. */
6374 select_window (data->current_window, Qnil, 0);
6376 /* Fselect_window will have made f the selected frame, so we
6377 reselect the proper frame here. Fhandle_switch_frame will change the
6378 selected window too, but that doesn't make the call to
6379 Fselect_window above totally superfluous; it still sets f's
6380 selected window. */
6381 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6382 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6384 run_window_configuration_change_hook (f);
6387 if (!NILP (new_current_buffer))
6389 Fset_buffer (new_current_buffer);
6390 /* If the new current buffer doesn't appear in the selected
6391 window, go to its old point (see bug#12208). */
6392 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6393 Fgoto_char (make_number (old_point));
6396 Vminibuf_scroll_window = data->minibuf_scroll_window;
6397 minibuf_selected_window = data->minibuf_selected_window;
6399 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6402 void
6403 restore_window_configuration (Lisp_Object configuration)
6405 Fset_window_configuration (configuration);
6409 /* If WINDOW is an internal window, recursively delete all child windows
6410 reachable via the next and contents slots of WINDOW. Otherwise setup
6411 WINDOW to not show any buffer. */
6413 void
6414 delete_all_child_windows (Lisp_Object window)
6416 register struct window *w;
6418 w = XWINDOW (window);
6420 if (!NILP (w->next))
6421 /* Delete WINDOW's siblings (we traverse postorderly). */
6422 delete_all_child_windows (w->next);
6424 if (WINDOWP (w->contents))
6426 delete_all_child_windows (w->contents);
6427 wset_combination (w, 0, Qnil);
6429 else if (BUFFERP (w->contents))
6431 unshow_buffer (w);
6432 unchain_marker (XMARKER (w->pointm));
6433 unchain_marker (XMARKER (w->start));
6434 /* Since combination limit makes sense for an internal windows
6435 only, we use this slot to save the buffer for the sake of
6436 possible resurrection in Fset_window_configuration. */
6437 wset_combination_limit (w, w->contents);
6438 wset_buffer (w, Qnil);
6441 Vwindow_list = Qnil;
6444 static int
6445 count_windows (register struct window *window)
6447 register int count = 1;
6448 if (!NILP (window->next))
6449 count += count_windows (XWINDOW (window->next));
6450 if (WINDOWP (window->contents))
6451 count += count_windows (XWINDOW (window->contents));
6452 return count;
6456 /* Fill vector FLAT with leaf windows under W, starting at index I.
6457 Value is last index + 1. */
6458 static int
6459 get_leaf_windows (struct window *w, struct window **flat, int i)
6461 while (w)
6463 if (WINDOWP (w->contents))
6464 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6465 else
6466 flat[i++] = w;
6468 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6471 return i;
6475 /* Return a pointer to the glyph W's physical cursor is on. Value is
6476 null if W's current matrix is invalid, so that no meaningful glyph
6477 can be returned. */
6478 struct glyph *
6479 get_phys_cursor_glyph (struct window *w)
6481 struct glyph_row *row;
6482 struct glyph *glyph;
6483 int hpos = w->phys_cursor.hpos;
6485 if (!(w->phys_cursor.vpos >= 0
6486 && w->phys_cursor.vpos < w->current_matrix->nrows))
6487 return NULL;
6489 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6490 if (!row->enabled_p)
6491 return NULL;
6493 if (w->hscroll)
6495 /* When the window is hscrolled, cursor hpos can legitimately be
6496 out of bounds, but we draw the cursor at the corresponding
6497 window margin in that case. */
6498 if (!row->reversed_p && hpos < 0)
6499 hpos = 0;
6500 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6501 hpos = row->used[TEXT_AREA] - 1;
6504 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6505 glyph = row->glyphs[TEXT_AREA] + hpos;
6506 else
6507 glyph = NULL;
6509 return glyph;
6513 static int
6514 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6516 register struct saved_window *p;
6517 register struct window *w;
6518 register Lisp_Object tem, pers, par;
6520 for (; !NILP (window); window = w->next)
6522 p = SAVED_WINDOW_N (vector, i);
6523 w = XWINDOW (window);
6525 wset_temslot (w, make_number (i)); i++;
6526 p->window = window;
6527 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6528 p->pixel_left = make_number (w->pixel_left);
6529 p->pixel_top = make_number (w->pixel_top);
6530 p->pixel_width = make_number (w->pixel_width);
6531 p->pixel_height = make_number (w->pixel_height);
6532 p->left_col = make_number (w->left_col);
6533 p->top_line = make_number (w->top_line);
6534 p->total_cols = make_number (w->total_cols);
6535 p->total_lines = make_number (w->total_lines);
6536 p->normal_cols = w->normal_cols;
6537 p->normal_lines = w->normal_lines;
6538 XSETFASTINT (p->hscroll, w->hscroll);
6539 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6540 p->display_table = w->display_table;
6541 p->left_margin_cols = make_number (w->left_margin_cols);
6542 p->right_margin_cols = make_number (w->right_margin_cols);
6543 p->left_fringe_width = make_number (w->left_fringe_width);
6544 p->right_fringe_width = make_number (w->right_fringe_width);
6545 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6546 p->scroll_bar_width = make_number (w->scroll_bar_width);
6547 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6548 p->dedicated = w->dedicated;
6549 p->combination_limit = w->combination_limit;
6550 p->window_parameters = Qnil;
6552 if (!NILP (Vwindow_persistent_parameters))
6554 /* Run cycle detection on Vwindow_persistent_parameters. */
6555 Lisp_Object tortoise, hare;
6557 hare = tortoise = Vwindow_persistent_parameters;
6558 while (CONSP (hare))
6560 hare = XCDR (hare);
6561 if (!CONSP (hare))
6562 break;
6564 hare = XCDR (hare);
6565 tortoise = XCDR (tortoise);
6567 if (EQ (hare, tortoise))
6568 /* Reset Vwindow_persistent_parameters to Qnil. */
6570 Vwindow_persistent_parameters = Qnil;
6571 break;
6575 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6576 tem = XCDR (tem))
6578 pers = XCAR (tem);
6579 /* Save values for persistent window parameters. */
6580 if (CONSP (pers) && !NILP (XCDR (pers)))
6582 par = Fassq (XCAR (pers), w->window_parameters);
6583 if (NILP (par))
6584 /* If the window has no value for the parameter,
6585 make one. */
6586 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6587 p->window_parameters);
6588 else
6589 /* If the window has a value for the parameter,
6590 save it. */
6591 p->window_parameters = Fcons (Fcons (XCAR (par),
6592 XCDR (par)),
6593 p->window_parameters);
6598 if (BUFFERP (w->contents))
6600 /* Save w's value of point in the window configuration. If w
6601 is the selected window, then get the value of point from
6602 the buffer; pointm is garbage in the selected window. */
6603 if (EQ (window, selected_window))
6604 p->pointm = build_marker (XBUFFER (w->contents),
6605 BUF_PT (XBUFFER (w->contents)),
6606 BUF_PT_BYTE (XBUFFER (w->contents)));
6607 else
6608 p->pointm = Fcopy_marker (w->pointm, Qnil);
6609 XMARKER (p->pointm)->insertion_type
6610 = !NILP (Vwindow_point_insertion_type);
6612 p->start = Fcopy_marker (w->start, Qnil);
6613 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6615 tem = BVAR (XBUFFER (w->contents), mark);
6616 p->mark = Fcopy_marker (tem, Qnil);
6618 else
6620 p->pointm = Qnil;
6621 p->start = Qnil;
6622 p->mark = Qnil;
6623 p->start_at_line_beg = Qnil;
6626 if (NILP (w->parent))
6627 p->parent = Qnil;
6628 else
6629 p->parent = XWINDOW (w->parent)->temslot;
6631 if (NILP (w->prev))
6632 p->prev = Qnil;
6633 else
6634 p->prev = XWINDOW (w->prev)->temslot;
6636 if (WINDOWP (w->contents))
6637 i = save_window_save (w->contents, vector, i);
6640 return i;
6643 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6644 Scurrent_window_configuration, 0, 1, 0,
6645 doc: /* Return an object representing the current window configuration of FRAME.
6646 If FRAME is nil or omitted, use the selected frame.
6647 This describes the number of windows, their sizes and current buffers,
6648 and for each displayed buffer, where display starts, and the positions of
6649 point and mark. An exception is made for point in the current buffer:
6650 its value is -not- saved.
6651 This also records the currently selected frame, and FRAME's focus
6652 redirection (see `redirect-frame-focus'). The variable
6653 `window-persistent-parameters' specifies which window parameters are
6654 saved by this function. */)
6655 (Lisp_Object frame)
6657 register Lisp_Object tem;
6658 register int n_windows;
6659 register struct save_window_data *data;
6660 register int i;
6661 struct frame *f = decode_live_frame (frame);
6663 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6664 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6665 PVEC_WINDOW_CONFIGURATION);
6667 data->frame_cols = FRAME_COLS (f);
6668 data->frame_lines = FRAME_LINES (f);
6669 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6670 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6671 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6672 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6673 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6674 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6675 data->selected_frame = selected_frame;
6676 data->current_window = FRAME_SELECTED_WINDOW (f);
6677 XSETBUFFER (data->current_buffer, current_buffer);
6678 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6679 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6680 data->root_window = FRAME_ROOT_WINDOW (f);
6681 data->focus_frame = FRAME_FOCUS_FRAME (f);
6682 tem = make_uninit_vector (n_windows);
6683 data->saved_windows = tem;
6684 for (i = 0; i < n_windows; i++)
6685 ASET (tem, i,
6686 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6687 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6688 XSETWINDOW_CONFIGURATION (tem, data);
6689 return (tem);
6692 /* Called after W's margins, fringes or scroll bars was adjusted. */
6694 static void
6695 apply_window_adjustment (struct window *w)
6697 eassert (w);
6698 adjust_window_margins (w);
6699 clear_glyph_matrix (w->current_matrix);
6700 w->window_end_valid = 0;
6701 windows_or_buffers_changed = 30;
6702 wset_redisplay (w);
6703 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6707 /***********************************************************************
6708 Marginal Areas
6709 ***********************************************************************/
6711 static struct window *
6712 set_window_margins (struct window *w, Lisp_Object left_width,
6713 Lisp_Object right_width)
6715 int left, right;
6717 /* FIXME: what about margins that are too wide? */
6718 left = (NILP (left_width) ? 0
6719 : (CHECK_NATNUM (left_width), XINT (left_width)));
6720 right = (NILP (right_width) ? 0
6721 : (CHECK_NATNUM (right_width), XINT (right_width)));
6723 if (w->left_margin_cols != left || w->right_margin_cols != right)
6725 w->left_margin_cols = left;
6726 w->right_margin_cols = right;
6727 return w;
6729 return NULL;
6732 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6733 2, 3, 0,
6734 doc: /* Set width of marginal areas of window WINDOW.
6735 WINDOW must be a live window and defaults to the selected one.
6737 Second arg LEFT-WIDTH specifies the number of character cells to
6738 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6739 does the same for the right marginal area. A nil width parameter
6740 means no margin.
6742 Return t if any margin was actually changed and nil otherwise. */)
6743 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6745 struct window *w = set_window_margins (decode_live_window (window),
6746 left_width, right_width);
6747 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6751 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6752 0, 1, 0,
6753 doc: /* Get width of marginal areas of window WINDOW.
6754 WINDOW must be a live window and defaults to the selected one.
6756 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6757 If a marginal area does not exist, its width will be returned
6758 as nil. */)
6759 (Lisp_Object window)
6761 struct window *w = decode_live_window (window);
6762 return Fcons (w->left_margin_cols
6763 ? make_number (w->left_margin_cols) : Qnil,
6764 w->right_margin_cols
6765 ? make_number (w->right_margin_cols) : Qnil);
6770 /***********************************************************************
6771 Fringes
6772 ***********************************************************************/
6774 static struct window *
6775 set_window_fringes (struct window *w, Lisp_Object left_width,
6776 Lisp_Object right_width, Lisp_Object outside_margins)
6778 int left, right, outside = !NILP (outside_margins);
6780 left = (NILP (left_width) ? -1
6781 : (CHECK_NATNUM (left_width), XINT (left_width)));
6782 right = (NILP (right_width) ? -1
6783 : (CHECK_NATNUM (right_width), XINT (right_width)));
6785 /* Do nothing on a tty or if nothing to actually change. */
6786 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6787 && (w->left_fringe_width != left
6788 || w->right_fringe_width != right
6789 || w->fringes_outside_margins != outside))
6791 w->left_fringe_width = left;
6792 w->right_fringe_width = right;
6793 w->fringes_outside_margins = outside;
6794 return w;
6796 return NULL;
6799 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6800 2, 4, 0,
6801 doc: /* Set the fringe widths of window WINDOW.
6802 WINDOW must be a live window and defaults to the selected one.
6804 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6805 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6806 fringe width. If a fringe width arg is nil, that means to use the
6807 frame's default fringe width. Default fringe widths can be set with
6808 the command `set-fringe-style'.
6809 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6810 outside of the display margins. By default, fringes are drawn between
6811 display marginal areas and the text area.
6813 Return t if any fringe was actually changed and nil otherwise. */)
6814 (Lisp_Object window, Lisp_Object left_width,
6815 Lisp_Object right_width, Lisp_Object outside_margins)
6817 struct window *w
6818 = set_window_fringes (decode_live_window (window),
6819 left_width, right_width, outside_margins);
6820 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6824 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6825 0, 1, 0,
6826 doc: /* Get width of fringes of window WINDOW.
6827 WINDOW must be a live window and defaults to the selected one.
6829 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6830 (Lisp_Object window)
6832 struct window *w = decode_live_window (window);
6834 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6835 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6836 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6841 /***********************************************************************
6842 Scroll bars
6843 ***********************************************************************/
6845 static struct window *
6846 set_window_scroll_bars (struct window *w, Lisp_Object width,
6847 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6849 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6851 if (iwidth == 0)
6852 vertical_type = Qnil;
6854 if (!(NILP (vertical_type)
6855 || EQ (vertical_type, Qleft)
6856 || EQ (vertical_type, Qright)
6857 || EQ (vertical_type, Qt)))
6858 error ("Invalid type of vertical scroll bar");
6860 if (w->scroll_bar_width != iwidth
6861 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6863 w->scroll_bar_width = iwidth;
6864 wset_vertical_scroll_bar_type (w, vertical_type);
6865 return w;
6867 return NULL;
6870 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6871 Sset_window_scroll_bars, 2, 4, 0,
6872 doc: /* Set width and type of scroll bars of window WINDOW.
6873 WINDOW must be a live window and defaults to the selected one.
6875 Second parameter WIDTH specifies the pixel width for the scroll bar.
6876 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6877 bar: left, right, or nil.
6878 If WIDTH is nil, use the frame's scroll-bar width.
6879 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6880 Fourth parameter HORIZONTAL-TYPE is currently unused.
6882 Return t if scroll bars were actually changed and nil otherwise. */)
6883 (Lisp_Object window, Lisp_Object width,
6884 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6886 struct window *w
6887 = set_window_scroll_bars (decode_live_window (window),
6888 width, vertical_type, horizontal_type);
6889 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6893 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6894 0, 1, 0,
6895 doc: /* Get width and type of scroll bars of window WINDOW.
6896 WINDOW must be a live window and defaults to the selected one.
6898 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6899 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6900 value. */)
6901 (Lisp_Object window)
6903 struct window *w = decode_live_window (window);
6905 return list4 (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (w)),
6906 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6907 w->vertical_scroll_bar_type, Qnil);
6912 /***********************************************************************
6913 Smooth scrolling
6914 ***********************************************************************/
6916 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6917 doc: /* Return the amount by which WINDOW is scrolled vertically.
6918 If WINDOW is omitted or nil, it defaults to the selected window.
6919 Normally, value is a multiple of the canonical character height of WINDOW;
6920 optional second arg PIXELS-P means value is measured in pixels. */)
6921 (Lisp_Object window, Lisp_Object pixels_p)
6923 Lisp_Object result;
6924 struct window *w = decode_live_window (window);
6925 struct frame *f = XFRAME (w->frame);
6927 if (FRAME_WINDOW_P (f))
6928 result = (NILP (pixels_p)
6929 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6930 : make_number (-w->vscroll));
6931 else
6932 result = make_number (0);
6933 return result;
6937 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6938 2, 3, 0,
6939 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6940 WINDOW nil means use the selected window. Normally, VSCROLL is a
6941 non-negative multiple of the canonical character height of WINDOW;
6942 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6943 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6944 corresponds to an integral number of pixels. The return value is the
6945 result of this rounding.
6946 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6947 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6949 struct window *w = decode_live_window (window);
6950 struct frame *f = XFRAME (w->frame);
6952 CHECK_NUMBER_OR_FLOAT (vscroll);
6954 if (FRAME_WINDOW_P (f))
6956 int old_dy = w->vscroll;
6958 w->vscroll = - (NILP (pixels_p)
6959 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6960 : XFLOATINT (vscroll));
6961 w->vscroll = min (w->vscroll, 0);
6963 if (w->vscroll != old_dy)
6965 /* Adjust glyph matrix of the frame if the virtual display
6966 area becomes larger than before. */
6967 if (w->vscroll < 0 && w->vscroll < old_dy)
6968 adjust_frame_glyphs (f);
6970 /* Prevent redisplay shortcuts. */
6971 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6975 return Fwindow_vscroll (window, pixels_p);
6979 /* Call FN for all leaf windows on frame F. FN is called with the
6980 first argument being a pointer to the leaf window, and with
6981 additional argument USER_DATA. Stops when FN returns 0. */
6983 static void
6984 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6985 void *user_data)
6987 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6988 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6989 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6993 /* Helper function for foreach_window. Call FN for all leaf windows
6994 reachable from W. FN is called with the first argument being a
6995 pointer to the leaf window, and with additional argument USER_DATA.
6996 Stop when FN returns 0. Value is 0 if stopped by FN. */
6998 static int
6999 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7001 int cont;
7003 for (cont = 1; w && cont;)
7005 if (WINDOWP (w->contents))
7006 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7007 else
7008 cont = fn (w, user_data);
7010 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7013 return cont;
7016 /***********************************************************************
7017 Initialization
7018 ***********************************************************************/
7020 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7021 describe the same state of affairs. This is used by Fequal.
7023 IGNORE_POSITIONS means ignore non-matching scroll positions
7024 and the like.
7026 This ignores a couple of things like the dedication status of
7027 window, combination_limit and the like. This might have to be
7028 fixed. */
7030 bool
7031 compare_window_configurations (Lisp_Object configuration1,
7032 Lisp_Object configuration2,
7033 bool ignore_positions)
7035 register struct save_window_data *d1, *d2;
7036 struct Lisp_Vector *sws1, *sws2;
7037 ptrdiff_t i;
7039 CHECK_WINDOW_CONFIGURATION (configuration1);
7040 CHECK_WINDOW_CONFIGURATION (configuration2);
7042 d1 = (struct save_window_data *) XVECTOR (configuration1);
7043 d2 = (struct save_window_data *) XVECTOR (configuration2);
7044 sws1 = XVECTOR (d1->saved_windows);
7045 sws2 = XVECTOR (d2->saved_windows);
7047 /* Frame settings must match. */
7048 if (d1->frame_cols != d2->frame_cols
7049 || d1->frame_lines != d2->frame_lines
7050 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7051 || !EQ (d1->selected_frame, d2->selected_frame)
7052 || !EQ (d1->current_buffer, d2->current_buffer)
7053 || (!ignore_positions
7054 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7055 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7056 || !EQ (d1->focus_frame, d2->focus_frame)
7057 /* Verify that the two configurations have the same number of windows. */
7058 || sws1->header.size != sws2->header.size)
7059 return 0;
7061 for (i = 0; i < sws1->header.size; i++)
7063 struct saved_window *sw1, *sw2;
7065 sw1 = SAVED_WINDOW_N (sws1, i);
7066 sw2 = SAVED_WINDOW_N (sws2, i);
7068 if (
7069 /* The "current" windows in the two configurations must
7070 correspond to each other. */
7071 EQ (d1->current_window, sw1->window)
7072 != EQ (d2->current_window, sw2->window)
7073 /* Windows' buffers must match. */
7074 || !EQ (sw1->buffer, sw2->buffer)
7075 || !EQ (sw1->pixel_left, sw2->pixel_left)
7076 || !EQ (sw1->pixel_top, sw2->pixel_top)
7077 || !EQ (sw1->pixel_height, sw2->pixel_height)
7078 || !EQ (sw1->pixel_width, sw2->pixel_width)
7079 || !EQ (sw1->left_col, sw2->left_col)
7080 || !EQ (sw1->top_line, sw2->top_line)
7081 || !EQ (sw1->total_cols, sw2->total_cols)
7082 || !EQ (sw1->total_lines, sw2->total_lines)
7083 || !EQ (sw1->display_table, sw2->display_table)
7084 /* The next two disjuncts check the window structure for
7085 equality. */
7086 || !EQ (sw1->parent, sw2->parent)
7087 || !EQ (sw1->prev, sw2->prev)
7088 || (!ignore_positions
7089 && (!EQ (sw1->hscroll, sw2->hscroll)
7090 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7091 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7092 || NILP (Fequal (sw1->start, sw2->start))
7093 || NILP (Fequal (sw1->pointm, sw2->pointm))
7094 || NILP (Fequal (sw1->mark, sw2->mark))))
7095 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7096 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7097 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7098 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7099 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7100 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7101 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7102 return 0;
7105 return 1;
7108 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7109 Scompare_window_configurations, 2, 2, 0,
7110 doc: /* Compare two window configurations as regards the structure of windows.
7111 This function ignores details such as the values of point and mark
7112 and scrolling positions. */)
7113 (Lisp_Object x, Lisp_Object y)
7115 if (compare_window_configurations (x, y, 1))
7116 return Qt;
7117 return Qnil;
7120 void
7121 init_window_once (void)
7123 struct frame *f = make_initial_frame ();
7124 XSETFRAME (selected_frame, f);
7125 Vterminal_frame = selected_frame;
7126 minibuf_window = f->minibuffer_window;
7127 selected_window = f->selected_window;
7129 window_initialized = 1;
7132 void
7133 init_window (void)
7135 Vwindow_list = Qnil;
7138 void
7139 syms_of_window (void)
7141 DEFSYM (Qscroll_up, "scroll-up");
7142 DEFSYM (Qscroll_down, "scroll-down");
7143 DEFSYM (Qscroll_command, "scroll-command");
7145 Fput (Qscroll_up, Qscroll_command, Qt);
7146 Fput (Qscroll_down, Qscroll_command, Qt);
7148 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7149 DEFSYM (Qwindowp, "windowp");
7150 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7151 DEFSYM (Qwindow_live_p, "window-live-p");
7152 DEFSYM (Qwindow_valid_p, "window-valid-p");
7153 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7154 DEFSYM (Qdelete_window, "delete-window");
7155 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7156 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7157 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7158 DEFSYM (Qsafe, "safe");
7159 DEFSYM (Qdisplay_buffer, "display-buffer");
7160 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7161 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7162 DEFSYM (Qget_mru_window, "get-mru-window");
7163 DEFSYM (Qwindow_size, "window-size");
7164 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7165 DEFSYM (Qabove, "above");
7166 DEFSYM (Qbelow, "below");
7167 DEFSYM (Qclone_of, "clone-of");
7168 DEFSYM (Qfloor, "floor");
7169 DEFSYM (Qceiling, "ceiling");
7171 staticpro (&Vwindow_list);
7173 minibuf_selected_window = Qnil;
7174 staticpro (&minibuf_selected_window);
7176 window_scroll_pixel_based_preserve_x = -1;
7177 window_scroll_pixel_based_preserve_y = -1;
7178 window_scroll_preserve_hpos = -1;
7179 window_scroll_preserve_vpos = -1;
7181 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7182 doc: /* Non-nil means call as function to display a help buffer.
7183 The function is called with one argument, the buffer to be displayed.
7184 Used by `with-output-to-temp-buffer'.
7185 If this function is used, then it must do the entire job of showing
7186 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7187 Vtemp_buffer_show_function = Qnil;
7189 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7190 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7191 Vminibuf_scroll_window = Qnil;
7193 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7194 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7195 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7196 is displayed in the `mode-line' face. */);
7197 mode_line_in_non_selected_windows = 1;
7199 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7200 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7201 Vother_window_scroll_buffer = Qnil;
7203 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7204 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7205 auto_window_vscroll_p = 1;
7207 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7208 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7209 next_screen_context_lines = 2;
7211 DEFVAR_LISP ("scroll-preserve-screen-position",
7212 Vscroll_preserve_screen_position,
7213 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7214 A value of nil means point does not keep its screen position except
7215 at the scroll margin or window boundary respectively.
7216 A value of t means point keeps its screen position if the scroll
7217 command moved it vertically out of the window, e.g. when scrolling
7218 by full screens.
7219 Any other value means point always keeps its screen position.
7220 Scroll commands should have the `scroll-command' property
7221 on their symbols to be controlled by this variable. */);
7222 Vscroll_preserve_screen_position = Qnil;
7224 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7225 doc: /* Type of marker to use for `window-point'. */);
7226 Vwindow_point_insertion_type = Qnil;
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");