Don't lose frame's background color when setting foreground (Bug#19802)
[emacs.git] / src / window.c
blobaafa4a16df1a9e7eca25160a7b8e86de530eb235
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2015 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "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;
58 static Lisp_Object Qwindow_point_insertion_type;
60 static int displayed_window_lines (struct window *);
61 static int count_windows (struct window *);
62 static int get_leaf_windows (struct window *, struct window **, int);
63 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
64 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
65 static void window_scroll_line_based (Lisp_Object, int, bool, int);
66 static int add_window_to_list (struct window *, void *);
67 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
68 Lisp_Object, int);
69 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
70 Lisp_Object *);
71 static void foreach_window (struct frame *,
72 int (* fn) (struct window *, void *),
73 void *);
74 static int foreach_window_1 (struct window *,
75 int (* fn) (struct window *, void *),
76 void *);
77 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
78 static int window_resize_check (struct window *, bool);
79 static void window_resize_apply (struct window *, bool);
80 static void window_resize_apply_total (struct window *, bool);
81 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
82 static void select_window_1 (Lisp_Object, bool);
84 static struct window *set_window_fringes (struct window *, Lisp_Object,
85 Lisp_Object, Lisp_Object);
86 static struct window *set_window_margins (struct window *, Lisp_Object,
87 Lisp_Object);
88 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
89 Lisp_Object, Lisp_Object);
90 static void apply_window_adjustment (struct window *);
92 /* This is the window in which the terminal's cursor should
93 be left when nothing is being done with it. This must
94 always be a leaf window, and its buffer is selected by
95 the top level editing loop at the end of each command.
97 This value is always the same as
98 FRAME_SELECTED_WINDOW (selected_frame). */
99 Lisp_Object selected_window;
101 /* A list of all windows for use by next_window and Fwindow_list.
102 Functions creating or deleting windows should invalidate this cache
103 by setting it to nil. */
104 Lisp_Object Vwindow_list;
106 /* The mini-buffer window of the selected frame.
107 Note that you cannot test for mini-bufferness of an arbitrary window
108 by comparing against this; but you can test for mini-bufferness of
109 the selected window. */
110 Lisp_Object minibuf_window;
112 /* Non-nil means it is the window whose mode line should be
113 shown as the selected window when the minibuffer is selected. */
114 Lisp_Object minibuf_selected_window;
116 /* Hook run at end of temp_output_buffer_show. */
117 static Lisp_Object Qtemp_buffer_show_hook;
119 /* Incremented for each window created. */
120 static int sequence_number;
122 /* Nonzero after init_window_once has finished. */
123 static int window_initialized;
125 /* Hook to run when window config changes. */
126 static Lisp_Object Qwindow_configuration_change_hook;
128 /* Used by the function window_scroll_pixel_based. */
129 static int window_scroll_pixel_based_preserve_x;
130 static int window_scroll_pixel_based_preserve_y;
132 /* Same for window_scroll_line_based. */
133 static EMACS_INT window_scroll_preserve_hpos;
134 static EMACS_INT window_scroll_preserve_vpos;
136 static void
137 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
139 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
142 /* These setters are used only in this file, so they can be private. */
143 static void
144 wset_combination_limit (struct window *w, Lisp_Object val)
146 w->combination_limit = val;
148 static void
149 wset_dedicated (struct window *w, Lisp_Object val)
151 w->dedicated = val;
153 static void
154 wset_display_table (struct window *w, Lisp_Object val)
156 w->display_table = val;
158 static void
159 wset_new_normal (struct window *w, Lisp_Object val)
161 w->new_normal = val;
163 static void
164 wset_new_total (struct window *w, Lisp_Object val)
166 w->new_total = val;
168 static void
169 wset_normal_cols (struct window *w, Lisp_Object val)
171 w->normal_cols = val;
173 static void
174 wset_normal_lines (struct window *w, Lisp_Object val)
176 w->normal_lines = val;
178 static void
179 wset_parent (struct window *w, Lisp_Object val)
181 w->parent = val;
183 static void
184 wset_pointm (struct window *w, Lisp_Object val)
186 w->pointm = val;
188 static void
189 wset_start (struct window *w, Lisp_Object val)
191 w->start = val;
193 static void
194 wset_temslot (struct window *w, Lisp_Object val)
196 w->temslot = val;
198 static void
199 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
201 w->vertical_scroll_bar_type = val;
203 static void
204 wset_window_parameters (struct window *w, Lisp_Object val)
206 w->window_parameters = val;
208 static void
209 wset_combination (struct window *w, bool horflag, Lisp_Object val)
211 /* Since leaf windows never becomes non-leaf, there should
212 be no buffer and markers in start and pointm fields of W. */
213 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
214 w->contents = val;
215 /* When an internal window is deleted and VAL is nil, HORFLAG
216 is meaningless. */
217 if (!NILP (val))
218 w->horizontal = horflag;
221 /* Nonzero if leaf window W doesn't reflect the actual state
222 of displayed buffer due to its text or overlays change. */
224 bool
225 window_outdated (struct window *w)
227 struct buffer *b = XBUFFER (w->contents);
228 return (w->last_modified < BUF_MODIFF (b)
229 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
232 struct window *
233 decode_live_window (register Lisp_Object window)
235 if (NILP (window))
236 return XWINDOW (selected_window);
238 CHECK_LIVE_WINDOW (window);
239 return XWINDOW (window);
242 struct window *
243 decode_any_window (register Lisp_Object window)
245 struct window *w;
247 if (NILP (window))
248 return XWINDOW (selected_window);
250 CHECK_WINDOW (window);
251 w = XWINDOW (window);
252 return w;
255 static struct window *
256 decode_valid_window (register Lisp_Object window)
258 struct window *w;
260 if (NILP (window))
261 return XWINDOW (selected_window);
263 CHECK_VALID_WINDOW (window);
264 w = XWINDOW (window);
265 return w;
268 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
269 cease its buffer, and 1 means that W is about to set up the new one. */
271 static void
272 adjust_window_count (struct window *w, int arg)
274 eassert (eabs (arg) == 1);
275 if (BUFFERP (w->contents))
277 struct buffer *b = XBUFFER (w->contents);
279 if (b->base_buffer)
280 b = b->base_buffer;
281 b->window_count += arg;
282 eassert (b->window_count >= 0);
283 /* These should be recalculated by redisplay code. */
284 w->window_end_valid = 0;
285 w->base_line_pos = 0;
289 /* Set W's buffer slot to VAL and recompute number
290 of windows showing VAL if it is a buffer. */
292 void
293 wset_buffer (struct window *w, Lisp_Object val)
295 adjust_window_count (w, -1);
296 if (BUFFERP (val))
297 /* Make sure that we do not assign the buffer
298 to an internal window. */
299 eassert (MARKERP (w->start) && MARKERP (w->pointm));
300 w->contents = val;
301 adjust_window_count (w, 1);
304 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
305 doc: /* Return t if OBJECT is a window and nil otherwise. */)
306 (Lisp_Object object)
308 return WINDOWP (object) ? Qt : Qnil;
311 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
312 doc: /* Return t if OBJECT is a valid window and nil otherwise.
313 A valid window is either a window that displays a buffer or an internal
314 window. Windows that have been deleted are not valid. */)
315 (Lisp_Object object)
317 return WINDOW_VALID_P (object) ? Qt : Qnil;
320 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
321 doc: /* Return t if OBJECT is a live window and nil otherwise.
322 A live window is a window that displays a buffer.
323 Internal windows and deleted windows are not live. */)
324 (Lisp_Object object)
326 return WINDOW_LIVE_P (object) ? Qt : Qnil;
329 /* Frames and windows. */
330 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
331 doc: /* Return the frame that window WINDOW is on.
332 WINDOW must be a valid window and defaults to the selected one. */)
333 (Lisp_Object window)
335 return decode_valid_window (window)->frame;
338 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
339 doc: /* Return the root window of FRAME-OR-WINDOW.
340 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
341 With a frame argument, return that frame's root window.
342 With a window argument, return the root window of that window's frame. */)
343 (Lisp_Object frame_or_window)
345 Lisp_Object window;
347 if (NILP (frame_or_window))
348 window = SELECTED_FRAME ()->root_window;
349 else if (WINDOW_VALID_P (frame_or_window))
350 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
351 else
353 CHECK_LIVE_FRAME (frame_or_window);
354 window = XFRAME (frame_or_window)->root_window;
357 return window;
360 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
361 doc: /* Return the minibuffer window for frame FRAME.
362 If FRAME is omitted or nil, it defaults to the selected frame. */)
363 (Lisp_Object frame)
365 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
368 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
369 Swindow_minibuffer_p, 0, 1, 0,
370 doc: /* Return non-nil if WINDOW is a minibuffer window.
371 WINDOW must be a valid window and defaults to the selected one. */)
372 (Lisp_Object window)
374 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
377 /* Don't move this to window.el - this must be a safe routine. */
378 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
379 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
380 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
381 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
382 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
383 the first window of that frame. */)
384 (Lisp_Object frame_or_window)
386 Lisp_Object window;
388 if (NILP (frame_or_window))
389 window = SELECTED_FRAME ()->root_window;
390 else if (WINDOW_VALID_P (frame_or_window))
391 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
392 else
394 CHECK_LIVE_FRAME (frame_or_window);
395 window = XFRAME (frame_or_window)->root_window;
398 while (WINDOWP (XWINDOW (window)->contents))
399 window = XWINDOW (window)->contents;
401 return window;
404 DEFUN ("frame-selected-window", Fframe_selected_window,
405 Sframe_selected_window, 0, 1, 0,
406 doc: /* Return the selected window of FRAME-OR-WINDOW.
407 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
408 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
409 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
410 return the selected window of that frame. */)
411 (Lisp_Object frame_or_window)
413 Lisp_Object window;
415 if (NILP (frame_or_window))
416 window = SELECTED_FRAME ()->selected_window;
417 else if (WINDOW_VALID_P (frame_or_window))
418 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
419 else
421 CHECK_LIVE_FRAME (frame_or_window);
422 window = XFRAME (frame_or_window)->selected_window;
425 return window;
428 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
429 Sset_frame_selected_window, 2, 3, 0,
430 doc: /* Set selected window of FRAME to WINDOW.
431 FRAME must be a live frame and defaults to the selected one. If FRAME
432 is the selected frame, this makes WINDOW the selected window. Optional
433 argument NORECORD non-nil means to neither change the order of recently
434 selected windows nor the buffer list. WINDOW must denote a live window.
435 Return WINDOW. */)
436 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
438 if (NILP (frame))
439 frame = selected_frame;
441 CHECK_LIVE_FRAME (frame);
442 CHECK_LIVE_WINDOW (window);
444 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
445 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
447 if (EQ (frame, selected_frame))
448 return Fselect_window (window, norecord);
449 else
451 fset_selected_window (XFRAME (frame), window);
452 return window;
456 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
457 doc: /* Return the selected window.
458 The selected window is the window in which the standard cursor for
459 selected windows appears and to which many commands apply. */)
460 (void)
462 return selected_window;
465 int window_select_count;
467 /* If select_window is called with inhibit_point_swap non-zero it will
468 not store point of the old selected window's buffer back into that
469 window's pointm slot. This is needed by Fset_window_configuration to
470 avoid that the display routine is called with selected_window set to
471 Qnil causing a subsequent crash. */
472 static Lisp_Object
473 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
475 register struct window *w;
476 struct frame *sf;
478 CHECK_LIVE_WINDOW (window);
480 w = XWINDOW (window);
482 /* Make the selected window's buffer current. */
483 Fset_buffer (w->contents);
485 if (EQ (window, selected_window) && !inhibit_point_swap)
486 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
487 way to call record_buffer from Elisp, so it's important that we call
488 record_buffer before returning here. */
489 goto record_and_return;
491 if (NILP (norecord))
492 { /* Mark the window for redisplay since the selected-window has
493 a different mode-line. */
494 wset_redisplay (XWINDOW (selected_window));
495 wset_redisplay (w);
497 else
498 redisplay_other_windows ();
500 sf = SELECTED_FRAME ();
501 if (XFRAME (WINDOW_FRAME (w)) != sf)
503 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
504 /* Use this rather than Fhandle_switch_frame
505 so that FRAME_FOCUS_FRAME is moved appropriately as we
506 move around in the state where a minibuffer in a separate
507 frame is active. */
508 Fselect_frame (WINDOW_FRAME (w), norecord);
509 /* Fselect_frame called us back so we've done all the work already. */
510 eassert (EQ (window, selected_window));
511 return window;
513 else
514 fset_selected_window (sf, window);
516 select_window_1 (window, inhibit_point_swap);
517 bset_last_selected_window (XBUFFER (w->contents), window);
519 record_and_return:
520 /* record_buffer can run QUIT, so make sure it is run only after we have
521 re-established the invariant between selected_window and selected_frame,
522 otherwise the temporary broken invariant might "escape" (bug#14161). */
523 if (NILP (norecord))
525 w->use_time = ++window_select_count;
526 record_buffer (w->contents);
529 return window;
532 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
533 (not even for redisplay's benefit), and assume that the window's frame is
534 already selected. */
535 static void
536 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
538 /* Store the old selected window's buffer's point in pointm of the old
539 selected window. It belongs to that window, and when the window is
540 not selected, must be in the window. */
541 if (!inhibit_point_swap)
543 struct window *ow = XWINDOW (selected_window);
544 if (BUFFERP (ow->contents))
545 set_marker_both (ow->pointm, ow->contents,
546 BUF_PT (XBUFFER (ow->contents)),
547 BUF_PT_BYTE (XBUFFER (ow->contents)));
550 selected_window = window;
552 /* Go to the point recorded in the window.
553 This is important when the buffer is in more
554 than one window. It also matters when
555 redisplay_window has altered point after scrolling,
556 because it makes the change only in the window. */
557 set_point_from_marker (XWINDOW (window)->pointm);
560 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
561 doc: /* Select WINDOW which must be a live window.
562 Also make WINDOW's frame the selected frame and WINDOW that frame's
563 selected window. In addition, make WINDOW's buffer current and set its
564 buffer's value of `point' to the value of WINDOW's `window-point'.
565 Return WINDOW.
567 Optional second arg NORECORD non-nil means do not put this buffer at the
568 front of the buffer list and do not make this window the most recently
569 selected one.
571 Run `buffer-list-update-hook' unless NORECORD is non-nil. Note that
572 applications and internal routines often select a window temporarily for
573 various purposes; mostly, to simplify coding. As a rule, such
574 selections should be not recorded and therefore will not pollute
575 `buffer-list-update-hook'. Selections that "really count" are those
576 causing a visible change in the next redisplay of WINDOW's frame and
577 should be always recorded. So if you think of running a function each
578 time a window gets selected put it on `buffer-list-update-hook'.
580 Also note that the main editor command loop sets the current buffer to
581 the buffer of the selected window before each command. */)
582 (register Lisp_Object window, Lisp_Object norecord)
584 return select_window (window, norecord, 0);
587 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
588 doc: /* Return the buffer displayed in window WINDOW.
589 If WINDOW is omitted or nil, it defaults to the selected window.
590 Return nil for an internal window or a deleted window. */)
591 (Lisp_Object window)
593 struct window *w = decode_any_window (window);
594 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
597 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
598 doc: /* Return the parent window of window WINDOW.
599 WINDOW must be a valid window and defaults to the selected one.
600 Return nil for a window with no parent (e.g. a root window). */)
601 (Lisp_Object window)
603 return decode_valid_window (window)->parent;
606 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
607 doc: /* Return the topmost child window of window WINDOW.
608 WINDOW must be a valid window and defaults to the selected one.
609 Return nil if WINDOW is a live window (live windows have no children).
610 Return nil if WINDOW is an internal window whose children form a
611 horizontal combination. */)
612 (Lisp_Object window)
614 struct window *w = decode_valid_window (window);
615 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
618 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
619 doc: /* Return the leftmost child window of window WINDOW.
620 WINDOW must be a valid window and defaults to the selected one.
621 Return nil if WINDOW is a live window (live windows have no children).
622 Return nil if WINDOW is an internal window whose children form a
623 vertical combination. */)
624 (Lisp_Object window)
626 struct window *w = decode_valid_window (window);
627 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
630 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
631 doc: /* Return the next sibling window of window WINDOW.
632 WINDOW must be a valid window and defaults to the selected one.
633 Return nil if WINDOW has no next sibling. */)
634 (Lisp_Object window)
636 return decode_valid_window (window)->next;
639 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
640 doc: /* Return the previous sibling window of window WINDOW.
641 WINDOW must be a valid window and defaults to the selected one.
642 Return nil if WINDOW has no previous sibling. */)
643 (Lisp_Object window)
645 return decode_valid_window (window)->prev;
648 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
649 doc: /* Return combination limit of window WINDOW.
650 WINDOW must be a valid window used in horizontal or vertical combination.
651 If the return value is nil, child windows of WINDOW can be recombined with
652 WINDOW's siblings. A return value of t means that child windows of
653 WINDOW are never \(re-)combined with WINDOW's siblings. */)
654 (Lisp_Object window)
656 struct window *w;
658 CHECK_VALID_WINDOW (window);
659 w = XWINDOW (window);
660 if (WINDOW_LEAF_P (w))
661 error ("Combination limit is meaningful for internal windows only");
662 return w->combination_limit;
665 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
666 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
667 WINDOW must be a valid window used in horizontal or vertical combination.
668 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
669 siblings. LIMIT t means that child windows of WINDOW are never
670 \(re-)combined with WINDOW's siblings. Other values are reserved for
671 future use. */)
672 (Lisp_Object window, Lisp_Object limit)
674 struct window *w;
676 CHECK_VALID_WINDOW (window);
677 w = XWINDOW (window);
678 if (WINDOW_LEAF_P (w))
679 error ("Combination limit is meaningful for internal windows only");
680 wset_combination_limit (w, limit);
681 return limit;
684 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
685 doc: /* Return the use time of window WINDOW.
686 WINDOW must be a live window and defaults to the selected one.
687 The window with the highest use time is the most recently selected
688 one. The window with the lowest use time is the least recently
689 selected one. */)
690 (Lisp_Object window)
692 return make_number (decode_live_window (window)->use_time);
695 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
696 doc: /* Return the width of window WINDOW in pixels.
697 WINDOW must be a valid window and defaults to the selected one.
699 The return value includes the fringes and margins of WINDOW as well as
700 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
701 an internal window, its pixel width is the width of the screen areas
702 spanned by its children. */)
703 (Lisp_Object window)
705 return make_number (decode_valid_window (window)->pixel_width);
708 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
709 doc: /* Return the height of window WINDOW in pixels.
710 WINDOW must be a valid window and defaults to the selected one.
712 The return value includes the mode line and header line and the bottom
713 divider, if any. If WINDOW is an internal window, its pixel height is
714 the height of the screen areas spanned by its children. */)
715 (Lisp_Object window)
717 return make_number (decode_valid_window (window)->pixel_height);
720 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
721 doc: /* Return the height of window WINDOW in lines.
722 WINDOW must be a valid window and defaults to the selected one.
724 The return value includes the heights of WINDOW's mode and header line
725 and its bottom divider, if any. If WINDOW is an internal window, the
726 total height is the height of the screen areas spanned by its children.
728 If WINDOW's pixel height is not an integral multiple of its frame's
729 character height, the number of lines occupied by WINDOW is rounded
730 internally. This is done in a way such that, if WINDOW is a parent
731 window, the sum of the total heights of all its children internally
732 equals the total height of WINDOW.
734 If the optional argument ROUND is `ceiling', return the smallest integer
735 larger than WINDOW's pixel height divided by the character height of
736 WINDOW's frame. ROUND `floor' means to return the largest integer
737 smaller than WINDOW's pixel height divided by the character height of
738 WINDOW's frame. Any other value of ROUND means to return the internal
739 total height of WINDOW. */)
740 (Lisp_Object window, Lisp_Object round)
742 struct window *w = decode_valid_window (window);
744 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
745 return make_number (w->total_lines);
746 else
748 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
750 return make_number (EQ (round, Qceiling)
751 ? ((w->pixel_height + unit - 1) /unit)
752 : (w->pixel_height / unit));
756 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
757 doc: /* Return the total width of window WINDOW in columns.
758 WINDOW must be a valid window and defaults to the selected one.
760 The return value includes the widths of WINDOW's fringes, margins,
761 scroll bars and its right divider, if any. If WINDOW is an internal
762 window, the total width is the width of the screen areas spanned by its
763 children.
765 If WINDOW's pixel width is not an integral multiple of its frame's
766 character width, the number of lines occupied by WINDOW is rounded
767 internally. This is done in a way such that, if WINDOW is a parent
768 window, the sum of the total widths of all its children internally
769 equals the total width of WINDOW.
771 If the optional argument ROUND is `ceiling', return the smallest integer
772 larger than WINDOW's pixel width divided by the character width of
773 WINDOW's frame. ROUND `floor' means to return the largest integer
774 smaller than WINDOW's pixel width divided by the character width of
775 WINDOW's frame. Any other value of ROUND means to return the internal
776 total width of WINDOW. */)
777 (Lisp_Object window, Lisp_Object round)
779 struct window *w = decode_valid_window (window);
781 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
782 return make_number (w->total_cols);
783 else
785 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
787 return make_number (EQ (round, Qceiling)
788 ? ((w->pixel_width + unit - 1) /unit)
789 : (w->pixel_width / unit));
793 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
794 doc: /* Return the new total size of window WINDOW.
795 WINDOW must be a valid window and defaults to the selected one.
797 The new total size of WINDOW is the value set by the last call of
798 `set-window-new-total' for WINDOW. If it is valid, it will be shortly
799 installed as WINDOW's total height (see `window-total-height') or total
800 width (see `window-total-width'). */)
801 (Lisp_Object window)
803 return decode_valid_window (window)->new_total;
806 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
807 doc: /* Return the normal height of window WINDOW.
808 WINDOW must be a valid window and defaults to the selected one.
809 If HORIZONTAL is non-nil, return the normal width of WINDOW.
811 The normal height of a frame's root window or a window that is
812 horizontally combined (a window that has a left or right sibling) is
813 1.0. The normal height of a window that is vertically combined (has a
814 sibling above or below) is the fraction of the window's height with
815 respect to its parent. The sum of the normal heights of all windows in a
816 vertical combination equals 1.0.
818 Similarly, the normal width of a frame's root window or a window that is
819 vertically combined equals 1.0. The normal width of a window that is
820 horizontally combined is the fraction of the window's width with respect
821 to its parent. The sum of the normal widths of all windows in a
822 horizontal combination equals 1.0.
824 The normal sizes of windows are used to restore the proportional sizes
825 of windows after they have been shrunk to their minimum sizes; for
826 example when a frame is temporarily made very small and afterwards gets
827 re-enlarged to its previous size. */)
828 (Lisp_Object window, Lisp_Object horizontal)
830 struct window *w = decode_valid_window (window);
832 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
835 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
836 doc: /* Return new normal size of window WINDOW.
837 WINDOW must be a valid window and defaults to the selected one.
839 The new normal size of WINDOW is the value set by the last call of
840 `set-window-new-normal' for WINDOW. If valid, it will be shortly
841 installed as WINDOW's normal size (see `window-normal-size'). */)
842 (Lisp_Object window)
844 return decode_valid_window (window)->new_normal;
847 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
848 doc: /* Return new pixel size of window WINDOW.
849 WINDOW must be a valid window and defaults to the selected one.
851 The new pixel size of WINDOW is the value set by the last call of
852 `set-window-new-pixel' for WINDOW. If it is valid, it will be shortly
853 installed as WINDOW's pixel height (see `window-pixel-height') or pixel
854 width (see `window-pixel-width'). */)
855 (Lisp_Object window)
857 return decode_valid_window (window)->new_pixel;
860 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
861 doc: /* Return left pixel edge of window WINDOW.
862 WINDOW must be a valid window and defaults to the selected one. */)
863 (Lisp_Object window)
865 return make_number (decode_valid_window (window)->pixel_left);
868 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
869 doc: /* Return top pixel edge of window WINDOW.
870 WINDOW must be a valid window and defaults to the selected one. */)
871 (Lisp_Object window)
873 return make_number (decode_valid_window (window)->pixel_top);
876 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
877 doc: /* Return left column of window WINDOW.
878 This is the distance, in columns, between the left edge of WINDOW and
879 the left edge of the frame's window area. For instance, the return
880 value is 0 if there is no window to the left of WINDOW.
882 WINDOW must be a valid window and defaults to the selected one. */)
883 (Lisp_Object window)
885 return make_number (decode_valid_window (window)->left_col);
888 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
889 doc: /* Return top line of window WINDOW.
890 This is the distance, in lines, between the top of WINDOW and the top
891 of the frame's window area. For instance, the return value is 0 if
892 there is no window above WINDOW.
894 WINDOW must be a valid window and defaults to the selected one. */)
895 (Lisp_Object window)
897 return make_number (decode_valid_window (window)->top_line);
900 /* Return the number of lines/pixels of W's body. Don't count any mode
901 or header line or horizontal divider of W. Rounds down to nearest
902 integer when not working pixelwise. */
903 static int
904 window_body_height (struct window *w, bool pixelwise)
906 int height = (w->pixel_height
907 - WINDOW_HEADER_LINE_HEIGHT (w)
908 - WINDOW_MODE_LINE_HEIGHT (w)
909 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
911 /* Don't return a negative value. */
912 return max (pixelwise
913 ? height
914 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
918 /* Return the number of columns/pixels of W's body. Don't count columns
919 occupied by the scroll bar or the divider/vertical bar separating W
920 from its right sibling or margins. On window-systems don't count
921 fringes either. Round down to nearest integer when not working
922 pixelwise. */
924 window_body_width (struct window *w, bool pixelwise)
926 struct frame *f = XFRAME (WINDOW_FRAME (w));
928 int width = (w->pixel_width
929 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
930 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
931 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
932 : ((!FRAME_WINDOW_P (f)
933 && !WINDOW_RIGHTMOST_P (w)
934 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
935 /* A vertical bar is either 1 or 0. */
936 ? 1 : 0))
937 - WINDOW_MARGINS_WIDTH (w)
938 - (FRAME_WINDOW_P (f)
939 ? WINDOW_FRINGES_WIDTH (w)
940 : 0));
942 /* Don't return a negative value. */
943 return max (pixelwise
944 ? width
945 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
949 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
950 doc: /* Return the height of WINDOW's text area.
951 WINDOW must be a live window and defaults to the selected one. Optional
952 argument PIXELWISE non-nil means return the height of WINDOW's text area
953 in pixels. The return value does not include the mode line or header
954 line or any horizontal divider.
956 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
957 pixel height divided by the character height of WINDOW's frame. This
958 means that if a line at the bottom of the text area is only partially
959 visible, that line is not counted. */)
960 (Lisp_Object window, Lisp_Object pixelwise)
962 return make_number (window_body_height (decode_live_window (window),
963 NILP (pixelwise) ? 0 : 1));
966 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
967 doc: /* Return the width of WINDOW's text area.
968 WINDOW must be a live window and defaults to the selected one. Optional
969 argument PIXELWISE non-nil means return the width in pixels. The return
970 value does not include any vertical dividers, fringes or marginal areas,
971 or scroll bars.
973 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
974 pixel width divided by the character width of WINDOW's frame. This
975 means that if a column at the right of the text area is only partially
976 visible, that column is not counted.
978 Note that the returned value includes the column reserved for the
979 continuation glyph. */)
980 (Lisp_Object window, Lisp_Object pixelwise)
982 return make_number (window_body_width (decode_live_window (window),
983 NILP (pixelwise) ? 0 : 1));
986 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
987 Swindow_mode_line_height, 0, 1, 0,
988 doc: /* Return the height in pixels of WINDOW's mode-line.
989 WINDOW must be a live window and defaults to the selected one. */)
990 (Lisp_Object window)
992 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
995 DEFUN ("window-header-line-height", Fwindow_header_line_height,
996 Swindow_header_line_height, 0, 1, 0,
997 doc: /* Return the height in pixels of WINDOW's header-line.
998 WINDOW must be a live window and defaults to the selected one. */)
999 (Lisp_Object window)
1001 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1004 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1005 Swindow_right_divider_width, 0, 1, 0,
1006 doc: /* Return the width in pixels of WINDOW's right divider.
1007 WINDOW must be a live window and defaults to the selected one. */)
1008 (Lisp_Object window)
1010 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1013 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1014 Swindow_bottom_divider_width, 0, 1, 0,
1015 doc: /* Return the width in pixels of WINDOW's bottom divider.
1016 WINDOW must be a live window and defaults to the selected one. */)
1017 (Lisp_Object window)
1019 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1022 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1023 Swindow_scroll_bar_width, 0, 1, 0,
1024 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1025 WINDOW must be a live window and defaults to the selected one. */)
1026 (Lisp_Object window)
1028 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1031 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1032 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1033 WINDOW must be a live window and defaults to the selected one. */)
1034 (Lisp_Object window)
1036 return make_number (decode_live_window (window)->hscroll);
1039 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1040 range, returning the new amount as a fixnum. */
1041 static Lisp_Object
1042 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1044 /* Horizontal scrolling has problems with large scroll amounts.
1045 It's too slow with long lines, and even with small lines the
1046 display can be messed up. For now, though, impose only the limits
1047 required by the internal representation: horizontal scrolling must
1048 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1049 (since it's stored in a ptrdiff_t). */
1050 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1051 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1053 /* Prevent redisplay shortcuts when changing the hscroll. */
1054 if (w->hscroll != new_hscroll)
1055 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1057 w->hscroll = new_hscroll;
1058 return make_number (new_hscroll);
1061 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1062 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1063 WINDOW must be a live window and defaults to the selected one.
1064 Clip the number to a reasonable value if out of range.
1065 Return the new number. NCOL should be zero or positive.
1067 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1068 window so that the location of point moves off-window. */)
1069 (Lisp_Object window, Lisp_Object ncol)
1071 CHECK_NUMBER (ncol);
1072 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1075 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1076 Swindow_redisplay_end_trigger, 0, 1, 0,
1077 doc: /* Return WINDOW's redisplay end trigger value.
1078 WINDOW must be a live window and defaults to the selected one.
1079 See `set-window-redisplay-end-trigger' for more information. */)
1080 (Lisp_Object window)
1082 return decode_live_window (window)->redisplay_end_trigger;
1085 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1086 Sset_window_redisplay_end_trigger, 2, 2, 0,
1087 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1088 WINDOW must be a live window and defaults to the selected one. VALUE
1089 should be a buffer position (typically a marker) or nil. If it is a
1090 buffer position, then if redisplay in WINDOW reaches a position beyond
1091 VALUE, the functions in `redisplay-end-trigger-functions' are called
1092 with two arguments: WINDOW, and the end trigger value. Afterwards the
1093 end-trigger value is reset to nil. */)
1094 (register Lisp_Object window, Lisp_Object value)
1096 wset_redisplay_end_trigger (decode_live_window (window), value);
1097 return value;
1100 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1101 doc: /* Return a list of the edge coordinates of WINDOW.
1102 WINDOW must be a valid window and defaults to the selected one.
1104 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1105 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1106 0 at top left corner of frame.
1108 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1109 is one more than the bottommost row occupied by WINDOW. The edges
1110 include the space used by WINDOW's scroll bar, display margins, fringes,
1111 header line, and/or mode line. For the edges of just the text area, use
1112 `window-inside-edges'. */)
1113 (Lisp_Object window)
1115 register struct window *w = decode_valid_window (window);
1117 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1118 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1121 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1122 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1123 WINDOW must be a valid window and defaults to the selected one.
1125 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1126 0, 0 at the top left corner of the frame.
1128 RIGHT is one more than the rightmost x position occupied by WINDOW.
1129 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1130 The pixel edges include the space used by WINDOW's scroll bar, display
1131 margins, fringes, header line, and/or mode line. For the pixel edges
1132 of just the text area, use `window-inside-pixel-edges'. */)
1133 (Lisp_Object window)
1135 register struct window *w = decode_valid_window (window);
1137 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1138 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1141 static void
1142 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1144 struct frame *f = XFRAME (w->frame);
1145 *add_y = f->top_pos;
1146 #ifdef FRAME_MENUBAR_HEIGHT
1147 *add_y += FRAME_MENUBAR_HEIGHT (f);
1148 #endif
1149 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1150 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1151 #elif defined (FRAME_TOOLBAR_HEIGHT)
1152 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1153 #endif
1154 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1155 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1156 #endif
1157 *add_x = f->left_pos;
1158 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1159 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1160 #endif
1163 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1164 Swindow_absolute_pixel_edges, 0, 1, 0,
1165 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1166 WINDOW must be a valid window and defaults to the selected one.
1168 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1169 0, 0 at the top left corner of the display.
1171 RIGHT is one more than the rightmost x position occupied by WINDOW.
1172 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1173 The pixel edges include the space used by WINDOW's scroll bar, display
1174 margins, fringes, header line, and/or mode line. For the pixel edges
1175 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1176 (Lisp_Object window)
1178 register struct window *w = decode_valid_window (window);
1179 int add_x, add_y;
1181 calc_absolute_offset (w, &add_x, &add_y);
1183 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1184 WINDOW_TOP_EDGE_Y (w) + add_y,
1185 WINDOW_RIGHT_EDGE_X (w) + add_x,
1186 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1189 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1190 doc: /* Return a list of the edge coordinates of WINDOW.
1191 WINDOW must be a live window and defaults to the selected one.
1193 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1194 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1195 0 at top left corner of frame.
1197 RIGHT is one more than the rightmost column of WINDOW's text area.
1198 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1199 inside edges do not include the space used by the WINDOW's scroll bar,
1200 display margins, fringes, header line, and/or mode line. */)
1201 (Lisp_Object window)
1203 register struct window *w = decode_live_window (window);
1205 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1206 + WINDOW_LEFT_MARGIN_COLS (w)
1207 + WINDOW_LEFT_FRINGE_COLS (w)),
1208 (WINDOW_TOP_EDGE_LINE (w)
1209 + WINDOW_HEADER_LINE_LINES (w)),
1210 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1211 - WINDOW_RIGHT_MARGIN_COLS (w)
1212 - WINDOW_RIGHT_FRINGE_COLS (w)),
1213 (WINDOW_BOTTOM_EDGE_LINE (w)
1214 - WINDOW_MODE_LINE_LINES (w)));
1217 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1218 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1219 WINDOW must be a live window and defaults to the selected one.
1221 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1222 (0,0) at the top left corner of the frame's window area.
1224 RIGHT is one more than the rightmost x position of WINDOW's text area.
1225 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1226 The inside edges do not include the space used by WINDOW's scroll bar,
1227 display margins, fringes, header line, and/or mode line. */)
1228 (Lisp_Object window)
1230 register struct window *w = decode_live_window (window);
1232 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1233 + WINDOW_LEFT_MARGIN_WIDTH (w)
1234 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1235 (WINDOW_TOP_EDGE_Y (w)
1236 + WINDOW_HEADER_LINE_HEIGHT (w)),
1237 (WINDOW_BOX_RIGHT_EDGE_X (w)
1238 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1239 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1240 (WINDOW_BOTTOM_EDGE_Y (w)
1241 - WINDOW_MODE_LINE_HEIGHT (w)));
1244 DEFUN ("window-inside-absolute-pixel-edges",
1245 Fwindow_inside_absolute_pixel_edges,
1246 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1247 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1248 WINDOW must be a live window and defaults to the selected one.
1250 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1251 (0,0) at the top left corner of the frame's window area.
1253 RIGHT is one more than the rightmost x position of WINDOW's text area.
1254 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1255 The inside edges do not include the space used by WINDOW's scroll bar,
1256 display margins, fringes, header line, and/or mode line. */)
1257 (Lisp_Object window)
1259 register struct window *w = decode_live_window (window);
1260 int add_x, add_y;
1262 calc_absolute_offset (w, &add_x, &add_y);
1264 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1265 + WINDOW_LEFT_MARGIN_WIDTH (w)
1266 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1267 (WINDOW_TOP_EDGE_Y (w)
1268 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1269 (WINDOW_BOX_RIGHT_EDGE_X (w)
1270 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1271 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1272 (WINDOW_BOTTOM_EDGE_Y (w)
1273 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1276 /* Test if the character at column X, row Y is within window W.
1277 If it is not, return ON_NOTHING;
1278 if it is on the window's vertical divider, return
1279 ON_RIGHT_DIVIDER;
1280 if it is on the window's horizontal divider, return
1281 ON_BOTTOM_DIVIDER;
1282 if it is in the window's text area, return ON_TEXT;
1283 if it is on the window's modeline, return ON_MODE_LINE;
1284 if it is on the border between the window and its right sibling,
1285 return ON_VERTICAL_BORDER;
1286 if it is on a scroll bar, return ON_SCROLL_BAR;
1287 if it is on the window's top line, return ON_HEADER_LINE;
1288 if it is in left or right fringe of the window,
1289 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1290 if it is in the marginal area to the left/right of the window,
1291 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1293 X and Y are frame relative pixel coordinates. */
1295 static enum window_part
1296 coordinates_in_window (register struct window *w, int x, int y)
1298 struct frame *f = XFRAME (WINDOW_FRAME (w));
1299 enum window_part part;
1300 int ux = FRAME_COLUMN_WIDTH (f);
1301 int left_x = WINDOW_LEFT_EDGE_X (w);
1302 int right_x = WINDOW_RIGHT_EDGE_X (w);
1303 int top_y = WINDOW_TOP_EDGE_Y (w);
1304 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1305 /* The width of the area where the vertical line can be dragged.
1306 (Between mode lines for instance. */
1307 int grabbable_width = ux;
1308 int lmargin_width, rmargin_width, text_left, text_right;
1310 /* Outside any interesting row or column? */
1311 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1312 return ON_NOTHING;
1314 /* On the horizontal window divider (which prevails the vertical
1315 divider)? */
1316 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1317 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1318 && y <= bottom_y)
1319 return ON_BOTTOM_DIVIDER;
1320 /* On vertical window divider? */
1321 else if (!WINDOW_RIGHTMOST_P (w)
1322 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1323 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1324 && x <= right_x)
1325 return ON_RIGHT_DIVIDER;
1326 /* On the mode or header line? */
1327 else if ((WINDOW_WANTS_MODELINE_P (w)
1328 && y >= (bottom_y
1329 - CURRENT_MODE_LINE_HEIGHT (w)
1330 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1331 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1332 && (part = ON_MODE_LINE))
1333 || (WINDOW_WANTS_HEADER_LINE_P (w)
1334 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1335 && (part = ON_HEADER_LINE)))
1337 /* If it's under/over the scroll bar portion of the mode/header
1338 line, say it's on the vertical line. That's to be able to
1339 resize windows horizontally in case we're using toolkit scroll
1340 bars. Note: If scrollbars are on the left, the window that
1341 must be eventually resized is that on the left of WINDOW. */
1342 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1343 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1344 && !WINDOW_LEFTMOST_P (w)
1345 && eabs (x - left_x) < grabbable_width)
1346 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1347 && !WINDOW_RIGHTMOST_P (w)
1348 && eabs (x - right_x) < grabbable_width)))
1349 return ON_VERTICAL_BORDER;
1350 else
1351 return part;
1354 /* In what's below, we subtract 1 when computing right_x because we
1355 want the rightmost pixel, which is given by left_pixel+width-1. */
1356 if (w->pseudo_window_p)
1358 left_x = 0;
1359 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1361 else
1363 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1364 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1367 /* Outside any interesting column? */
1368 if (x < left_x || x > right_x)
1369 return ON_SCROLL_BAR;
1371 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1372 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1374 text_left = window_box_left (w, TEXT_AREA);
1375 text_right = text_left + window_box_width (w, TEXT_AREA);
1377 if (FRAME_WINDOW_P (f))
1379 if (!w->pseudo_window_p
1380 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1381 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1382 && !WINDOW_RIGHTMOST_P (w)
1383 && (eabs (x - right_x) < grabbable_width))
1384 return ON_VERTICAL_BORDER;
1386 /* Need to say "x > right_x" rather than >=, since on character
1387 terminals, the vertical line's x coordinate is right_x. */
1388 else if (!w->pseudo_window_p
1389 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1390 && !WINDOW_RIGHTMOST_P (w)
1391 /* Why check ux if we are not the rightmost window? Also
1392 shouldn't a pseudo window always be rightmost? */
1393 && x > right_x - ux)
1394 return ON_VERTICAL_BORDER;
1396 if (x < text_left)
1398 if (lmargin_width > 0
1399 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1400 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1401 : (x < left_x + lmargin_width)))
1402 return ON_LEFT_MARGIN;
1403 else
1404 return ON_LEFT_FRINGE;
1407 if (x >= text_right)
1409 if (rmargin_width > 0
1410 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1411 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1412 : (x >= right_x - rmargin_width)))
1413 return ON_RIGHT_MARGIN;
1414 else
1415 return ON_RIGHT_FRINGE;
1418 /* Everything special ruled out - must be on text area */
1419 return ON_TEXT;
1422 /* Take X is the frame-relative pixel x-coordinate, and return the
1423 x-coordinate relative to part PART of window W. */
1425 window_relative_x_coord (struct window *w, enum window_part part, int x)
1427 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1429 switch (part)
1431 case ON_TEXT:
1432 return x - window_box_left (w, TEXT_AREA);
1434 case ON_HEADER_LINE:
1435 case ON_MODE_LINE:
1436 case ON_LEFT_FRINGE:
1437 return x - left_x;
1439 case ON_RIGHT_FRINGE:
1440 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1442 case ON_LEFT_MARGIN:
1443 return (x - left_x
1444 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1445 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1447 case ON_RIGHT_MARGIN:
1448 return (x + 1
1449 - ((w->pseudo_window_p)
1450 ? WINDOW_PIXEL_WIDTH (w)
1451 : WINDOW_BOX_RIGHT_EDGE_X (w))
1452 + window_box_width (w, RIGHT_MARGIN_AREA)
1453 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1454 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1457 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1458 return 0;
1462 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1463 Scoordinates_in_window_p, 2, 2, 0,
1464 doc: /* Return non-nil if COORDINATES are in WINDOW.
1465 WINDOW must be a live window and defaults to the selected one.
1466 COORDINATES is a cons of the form (X . Y), X and Y being distances
1467 measured in characters from the upper-left corner of the frame.
1468 \(0 . 0) denotes the character in the upper left corner of the
1469 frame.
1470 If COORDINATES are in the text portion of WINDOW,
1471 the coordinates relative to the window are returned.
1472 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1473 If they are in the right divider of WINDOW, `right-divider' is returned.
1474 If they are in the mode line of WINDOW, `mode-line' is returned.
1475 If they are in the header line of WINDOW, `header-line' is returned.
1476 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1477 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1478 If they are on the border between WINDOW and its right sibling,
1479 `vertical-line' is returned.
1480 If they are in the windows's left or right marginal areas, `left-margin'\n\
1481 or `right-margin' is returned. */)
1482 (register Lisp_Object coordinates, Lisp_Object window)
1484 struct window *w;
1485 struct frame *f;
1486 int x, y;
1487 Lisp_Object lx, ly;
1489 w = decode_live_window (window);
1490 f = XFRAME (w->frame);
1491 CHECK_CONS (coordinates);
1492 lx = Fcar (coordinates);
1493 ly = Fcdr (coordinates);
1494 CHECK_NUMBER_OR_FLOAT (lx);
1495 CHECK_NUMBER_OR_FLOAT (ly);
1496 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1497 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1499 switch (coordinates_in_window (w, x, y))
1501 case ON_NOTHING:
1502 return Qnil;
1504 case ON_TEXT:
1505 /* Convert X and Y to window relative pixel coordinates, and
1506 return the canonical char units. */
1507 x -= window_box_left (w, TEXT_AREA);
1508 y -= WINDOW_TOP_EDGE_Y (w);
1509 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1510 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1512 case ON_MODE_LINE:
1513 return Qmode_line;
1515 case ON_VERTICAL_BORDER:
1516 return Qvertical_line;
1518 case ON_HEADER_LINE:
1519 return Qheader_line;
1521 case ON_LEFT_FRINGE:
1522 return Qleft_fringe;
1524 case ON_RIGHT_FRINGE:
1525 return Qright_fringe;
1527 case ON_LEFT_MARGIN:
1528 return Qleft_margin;
1530 case ON_RIGHT_MARGIN:
1531 return Qright_margin;
1533 case ON_SCROLL_BAR:
1534 /* Historically we are supposed to return nil in this case. */
1535 return Qnil;
1537 case ON_RIGHT_DIVIDER:
1538 return Qright_divider;
1540 case ON_BOTTOM_DIVIDER:
1541 return Qbottom_divider;
1543 default:
1544 emacs_abort ();
1549 /* Callback for foreach_window, used in window_from_coordinates.
1550 Check if window W contains coordinates specified by USER_DATA which
1551 is actually a pointer to a struct check_window_data CW.
1553 Check if window W contains coordinates *CW->x and *CW->y. If it
1554 does, return W in *CW->window, as Lisp_Object, and return in
1555 *CW->part the part of the window under coordinates *X,*Y. Return
1556 zero from this function to stop iterating over windows. */
1558 struct check_window_data
1560 Lisp_Object *window;
1561 int x, y;
1562 enum window_part *part;
1565 static int
1566 check_window_containing (struct window *w, void *user_data)
1568 struct check_window_data *cw = user_data;
1569 enum window_part found;
1570 int continue_p = 1;
1572 found = coordinates_in_window (w, cw->x, cw->y);
1573 if (found != ON_NOTHING)
1575 *cw->part = found;
1576 XSETWINDOW (*cw->window, w);
1577 continue_p = 0;
1580 return continue_p;
1584 /* Find the window containing frame-relative pixel position X/Y and
1585 return it as a Lisp_Object.
1587 If X, Y is on one of the window's special `window_part' elements,
1588 set *PART to the id of that element.
1590 If there is no window under X, Y return nil and leave *PART
1591 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1593 This function was previously implemented with a loop cycling over
1594 windows with Fnext_window, and starting with the frame's selected
1595 window. It turned out that this doesn't work with an
1596 implementation of next_window using Vwindow_list, because
1597 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1598 tree of F when this function is called asynchronously from
1599 note_mouse_highlight. The original loop didn't terminate in this
1600 case. */
1602 Lisp_Object
1603 window_from_coordinates (struct frame *f, int x, int y,
1604 enum window_part *part, bool tool_bar_p)
1606 Lisp_Object window;
1607 struct check_window_data cw;
1608 enum window_part dummy;
1610 if (part == 0)
1611 part = &dummy;
1613 window = Qnil;
1614 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1615 foreach_window (f, check_window_containing, &cw);
1617 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1618 /* If not found above, see if it's in the tool bar window, if a tool
1619 bar exists. */
1620 if (NILP (window)
1621 && tool_bar_p
1622 && WINDOWP (f->tool_bar_window)
1623 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1624 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1625 != ON_NOTHING))
1627 *part = ON_TEXT;
1628 window = f->tool_bar_window;
1630 #endif
1632 return window;
1635 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1636 doc: /* Return window containing coordinates X and Y on FRAME.
1637 FRAME must be a live frame and defaults to the selected one.
1638 The top left corner of the frame is considered to be row 0,
1639 column 0. */)
1640 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1642 struct frame *f = decode_live_frame (frame);
1644 /* Check that arguments are integers or floats. */
1645 CHECK_NUMBER_OR_FLOAT (x);
1646 CHECK_NUMBER_OR_FLOAT (y);
1648 return window_from_coordinates (f,
1649 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1650 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1651 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1652 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1653 0, 0);
1656 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1657 doc: /* Return current value of point in WINDOW.
1658 WINDOW must be a live window and defaults to the selected one.
1660 For a nonselected window, this is the value point would have if that
1661 window were selected.
1663 Note that, when WINDOW is selected, the value returned is the same as
1664 that returned by `point' for WINDOW's buffer. It would be more strictly
1665 correct to return the top-level value of `point', outside of any
1666 `save-excursion' forms. But that is hard to define. */)
1667 (Lisp_Object window)
1669 register struct window *w = decode_live_window (window);
1671 if (w == XWINDOW (selected_window))
1672 return make_number (BUF_PT (XBUFFER (w->contents)));
1673 else
1674 return Fmarker_position (w->pointm);
1677 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1678 doc: /* Return position at which display currently starts in WINDOW.
1679 WINDOW must be a live window and defaults to the selected one.
1680 This is updated by redisplay or by calling `set-window-start'. */)
1681 (Lisp_Object window)
1683 return Fmarker_position (decode_live_window (window)->start);
1686 /* This is text temporarily removed from the doc string below.
1688 This function returns nil if the position is not currently known.
1689 That happens when redisplay is preempted and doesn't finish.
1690 If in that case you want to compute where the end of the window would
1691 have been if redisplay had finished, do this:
1692 (save-excursion
1693 (goto-char (window-start window))
1694 (vertical-motion (1- (window-height window)) window)
1695 (point))") */
1697 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1698 doc: /* Return position at which display currently ends in WINDOW.
1699 WINDOW must be a live window and defaults to the selected one.
1700 This is updated by redisplay, when it runs to completion.
1701 Simply changing the buffer text or setting `window-start'
1702 does not update this value.
1703 Return nil if there is no recorded value. (This can happen if the
1704 last redisplay of WINDOW was preempted, and did not finish.)
1705 If UPDATE is non-nil, compute the up-to-date position
1706 if it isn't already recorded. */)
1707 (Lisp_Object window, Lisp_Object update)
1709 Lisp_Object value;
1710 struct window *w = decode_live_window (window);
1711 Lisp_Object buf;
1712 struct buffer *b;
1714 buf = w->contents;
1715 CHECK_BUFFER (buf);
1716 b = XBUFFER (buf);
1718 if (! NILP (update)
1719 && (windows_or_buffers_changed
1720 || !w->window_end_valid
1721 || b->clip_changed
1722 || b->prevent_redisplay_optimizations_p
1723 || window_outdated (w))
1724 && !noninteractive)
1726 struct text_pos startp;
1727 struct it it;
1728 struct buffer *old_buffer = NULL;
1729 void *itdata = NULL;
1731 /* Cannot use Fvertical_motion because that function doesn't
1732 cope with variable-height lines. */
1733 if (b != current_buffer)
1735 old_buffer = current_buffer;
1736 set_buffer_internal (b);
1739 /* In case W->start is out of the range, use something
1740 reasonable. This situation occurred when loading a file with
1741 `-l' containing a call to `rmail' with subsequent other
1742 commands. At the end, W->start happened to be BEG, while
1743 rmail had already narrowed the buffer. */
1744 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1746 itdata = bidi_shelve_cache ();
1747 start_display (&it, w, startp);
1748 move_it_vertically (&it, window_box_height (w));
1749 if (it.current_y < it.last_visible_y)
1750 move_it_past_eol (&it);
1751 value = make_number (IT_CHARPOS (it));
1752 bidi_unshelve_cache (itdata, 0);
1754 if (old_buffer)
1755 set_buffer_internal (old_buffer);
1757 else
1758 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1760 return value;
1763 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1764 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1765 WINDOW must be a live window and defaults to the selected one.
1766 Return POS. */)
1767 (Lisp_Object window, Lisp_Object pos)
1769 register struct window *w = decode_live_window (window);
1771 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1773 if (w == XWINDOW (selected_window))
1775 if (XBUFFER (w->contents) == current_buffer)
1776 Fgoto_char (pos);
1777 else
1779 struct buffer *old_buffer = current_buffer;
1781 /* ... but here we want to catch type error before buffer change. */
1782 CHECK_NUMBER_COERCE_MARKER (pos);
1783 set_buffer_internal (XBUFFER (w->contents));
1784 Fgoto_char (pos);
1785 set_buffer_internal (old_buffer);
1788 else
1790 set_marker_restricted (w->pointm, pos, w->contents);
1791 /* We have to make sure that redisplay updates the window to show
1792 the new value of point. */
1793 wset_redisplay (w);
1796 return pos;
1799 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1800 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1801 WINDOW must be a live window and defaults to the selected one. Return
1802 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1803 overriding motion of point in order to display at this exact start. */)
1804 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1806 register struct window *w = decode_live_window (window);
1808 set_marker_restricted (w->start, pos, w->contents);
1809 /* This is not right, but much easier than doing what is right. */
1810 w->start_at_line_beg = 0;
1811 if (NILP (noforce))
1812 w->force_start = 1;
1813 w->update_mode_line = 1;
1814 /* Bug#15957. */
1815 w->window_end_valid = 0;
1816 wset_redisplay (w);
1818 return pos;
1821 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1822 Spos_visible_in_window_p, 0, 3, 0,
1823 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1824 WINDOW must be a live window and defaults to the selected one.
1826 Return nil if that position is scrolled vertically out of view. If a
1827 character is only partially visible, nil is returned, unless the
1828 optional argument PARTIALLY is non-nil. If POS is only out of view
1829 because of horizontal scrolling, return non-nil. If POS is t, it
1830 specifies the position of the last visible glyph in WINDOW. POS
1831 defaults to point in WINDOW; WINDOW defaults to the selected window.
1833 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1834 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1835 where X and Y are the pixel coordinates relative to the top left corner
1836 of the window. The remaining elements are omitted if the character after
1837 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1838 off-window at the top and bottom of the screen line ("row") containing
1839 POS, ROWH is the visible height of that row, and VPOS is the row number
1840 \(zero-based). */)
1841 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1843 register struct window *w;
1844 register EMACS_INT posint;
1845 register struct buffer *buf;
1846 struct text_pos top;
1847 Lisp_Object in_window = Qnil;
1848 int rtop, rbot, rowh, vpos, fully_p = 1;
1849 int x, y;
1851 w = decode_live_window (window);
1852 buf = XBUFFER (w->contents);
1853 SET_TEXT_POS_FROM_MARKER (top, w->start);
1855 if (EQ (pos, Qt))
1856 posint = -1;
1857 else if (!NILP (pos))
1859 CHECK_NUMBER_COERCE_MARKER (pos);
1860 posint = XINT (pos);
1862 else if (w == XWINDOW (selected_window))
1863 posint = PT;
1864 else
1865 posint = marker_position (w->pointm);
1867 /* If position is above window start or outside buffer boundaries,
1868 or if window start is out of range, position is not visible. */
1869 if ((EQ (pos, Qt)
1870 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1871 && CHARPOS (top) >= BUF_BEGV (buf)
1872 && CHARPOS (top) <= BUF_ZV (buf)
1873 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1874 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1875 in_window = Qt;
1877 if (!NILP (in_window) && !NILP (partially))
1879 Lisp_Object part = Qnil;
1880 if (!fully_p)
1881 part = list4i (rtop, rbot, rowh, vpos);
1882 in_window = Fcons (make_number (x),
1883 Fcons (make_number (y), part));
1886 return in_window;
1889 DEFUN ("window-line-height", Fwindow_line_height,
1890 Swindow_line_height, 0, 2, 0,
1891 doc: /* Return height in pixels of text line LINE in window WINDOW.
1892 WINDOW must be a live window and defaults to the selected one.
1894 Return height of current line if LINE is omitted or nil. Return height of
1895 header or mode line if LINE is `header-line' or `mode-line'.
1896 Otherwise, LINE is a text line number starting from 0. A negative number
1897 counts from the end of the window.
1899 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1900 in pixels of the visible part of the line, VPOS and YPOS are the
1901 vertical position in lines and pixels of the line, relative to the top
1902 of the first text line, and OFFBOT is the number of off-window pixels at
1903 the bottom of the text line. If there are off-window pixels at the top
1904 of the (first) text line, YPOS is negative.
1906 Return nil if window display is not up-to-date. In that case, use
1907 `pos-visible-in-window-p' to obtain the information. */)
1908 (Lisp_Object line, Lisp_Object window)
1910 register struct window *w;
1911 register struct buffer *b;
1912 struct glyph_row *row, *end_row;
1913 int max_y, crop, i;
1914 EMACS_INT n;
1916 w = decode_live_window (window);
1918 if (noninteractive || w->pseudo_window_p)
1919 return Qnil;
1921 CHECK_BUFFER (w->contents);
1922 b = XBUFFER (w->contents);
1924 /* Fail if current matrix is not up-to-date. */
1925 if (!w->window_end_valid
1926 || windows_or_buffers_changed
1927 || b->clip_changed
1928 || b->prevent_redisplay_optimizations_p
1929 || window_outdated (w))
1930 return Qnil;
1932 if (NILP (line))
1934 i = w->cursor.vpos;
1935 if (i < 0 || i >= w->current_matrix->nrows
1936 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1937 return Qnil;
1938 max_y = window_text_bottom_y (w);
1939 goto found_row;
1942 if (EQ (line, Qheader_line))
1944 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1945 return Qnil;
1946 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1947 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1950 if (EQ (line, Qmode_line))
1952 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1953 return (row->enabled_p ?
1954 list4i (row->height,
1955 0, /* not accurate */
1956 (WINDOW_HEADER_LINE_HEIGHT (w)
1957 + window_text_bottom_y (w)),
1959 : Qnil);
1962 CHECK_NUMBER (line);
1963 n = XINT (line);
1965 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1966 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1967 max_y = window_text_bottom_y (w);
1968 i = 0;
1970 while ((n < 0 || i < n)
1971 && row <= end_row && row->enabled_p
1972 && row->y + row->height < max_y)
1973 row++, i++;
1975 if (row > end_row || !row->enabled_p)
1976 return Qnil;
1978 if (++n < 0)
1980 if (-n > i)
1981 return Qnil;
1982 row += n;
1983 i += n;
1986 found_row:
1987 crop = max (0, (row->y + row->height) - max_y);
1988 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1991 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1992 0, 1, 0,
1993 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1994 More precisely, return the value assigned by the last call of
1995 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1996 never called with WINDOW as its argument, or the value set by that
1997 function was internally reset since its last call. WINDOW must be a
1998 live window and defaults to the selected one.
2000 When a window is dedicated to its buffer, `display-buffer' will refrain
2001 from displaying another buffer in it. `get-lru-window' and
2002 `get-largest-window' treat dedicated windows specially.
2003 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2004 `kill-buffer' can delete a dedicated window and the containing frame.
2006 Functions like `set-window-buffer' may change the buffer displayed by a
2007 window, unless that window is "strongly" dedicated to its buffer, that
2008 is the value returned by `window-dedicated-p' is t. */)
2009 (Lisp_Object window)
2011 return decode_live_window (window)->dedicated;
2014 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2015 Sset_window_dedicated_p, 2, 2, 0,
2016 doc: /* Mark WINDOW as dedicated according to FLAG.
2017 WINDOW must be a live window and defaults to the selected one. FLAG
2018 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2019 mark WINDOW as non-dedicated. Return FLAG.
2021 When a window is dedicated to its buffer, `display-buffer' will refrain
2022 from displaying another buffer in it. `get-lru-window' and
2023 `get-largest-window' treat dedicated windows specially.
2024 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2025 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2026 and the containing frame.
2028 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2029 its buffer. Functions like `set-window-buffer' may change the buffer
2030 displayed by a window, unless that window is strongly dedicated to its
2031 buffer. If and when `set-window-buffer' displays another buffer in a
2032 window, it also makes sure that the window is no more dedicated. */)
2033 (Lisp_Object window, Lisp_Object flag)
2035 wset_dedicated (decode_live_window (window), flag);
2036 return flag;
2039 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2040 0, 1, 0,
2041 doc: /* Return buffers previously shown in WINDOW.
2042 WINDOW must be a live window and defaults to the selected one.
2044 The return value is a list of elements (BUFFER WINDOW-START POS),
2045 where BUFFER is a buffer, WINDOW-START is the start position of the
2046 window for that buffer, and POS is a window-specific point value. */)
2047 (Lisp_Object window)
2049 return decode_live_window (window)->prev_buffers;
2052 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2053 Sset_window_prev_buffers, 2, 2, 0,
2054 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2055 WINDOW must be a live window and defaults to the selected one.
2057 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2058 where BUFFER is a buffer, WINDOW-START is the start position of the
2059 window for that buffer, and POS is a window-specific point value. */)
2060 (Lisp_Object window, Lisp_Object prev_buffers)
2062 wset_prev_buffers (decode_live_window (window), prev_buffers);
2063 return prev_buffers;
2066 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2067 0, 1, 0,
2068 doc: /* Return list of buffers recently re-shown in WINDOW.
2069 WINDOW must be a live window and defaults to the selected one. */)
2070 (Lisp_Object window)
2072 return decode_live_window (window)->next_buffers;
2075 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2076 Sset_window_next_buffers, 2, 2, 0,
2077 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2078 WINDOW must be a live window and defaults to the selected one.
2079 NEXT-BUFFERS should be a list of buffers. */)
2080 (Lisp_Object window, Lisp_Object next_buffers)
2082 wset_next_buffers (decode_live_window (window), next_buffers);
2083 return next_buffers;
2086 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2087 0, 1, 0,
2088 doc: /* Return the parameters of WINDOW and their values.
2089 WINDOW must be a valid window and defaults to the selected one. The
2090 return value is a list of elements of the form (PARAMETER . VALUE). */)
2091 (Lisp_Object window)
2093 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2096 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2097 2, 2, 0,
2098 doc: /* Return WINDOW's value for PARAMETER.
2099 WINDOW can be any window and defaults to the selected one. */)
2100 (Lisp_Object window, Lisp_Object parameter)
2102 Lisp_Object result;
2104 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2105 return CDR_SAFE (result);
2108 DEFUN ("set-window-parameter", Fset_window_parameter,
2109 Sset_window_parameter, 3, 3, 0,
2110 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2111 WINDOW can be any window and defaults to the selected one.
2112 Return VALUE. */)
2113 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2115 register struct window *w = decode_any_window (window);
2116 Lisp_Object old_alist_elt;
2118 old_alist_elt = Fassq (parameter, w->window_parameters);
2119 if (NILP (old_alist_elt))
2120 wset_window_parameters
2121 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2122 else
2123 Fsetcdr (old_alist_elt, value);
2124 return value;
2127 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2128 0, 1, 0,
2129 doc: /* Return the display-table that WINDOW is using.
2130 WINDOW must be a live window and defaults to the selected one. */)
2131 (Lisp_Object window)
2133 return decode_live_window (window)->display_table;
2136 /* Get the display table for use on window W. This is either W's
2137 display table or W's buffer's display table. Ignore the specified
2138 tables if they are not valid; if no valid table is specified,
2139 return 0. */
2141 struct Lisp_Char_Table *
2142 window_display_table (struct window *w)
2144 struct Lisp_Char_Table *dp = NULL;
2146 if (DISP_TABLE_P (w->display_table))
2147 dp = XCHAR_TABLE (w->display_table);
2148 else if (BUFFERP (w->contents))
2150 struct buffer *b = XBUFFER (w->contents);
2152 if (DISP_TABLE_P (BVAR (b, display_table)))
2153 dp = XCHAR_TABLE (BVAR (b, display_table));
2154 else if (DISP_TABLE_P (Vstandard_display_table))
2155 dp = XCHAR_TABLE (Vstandard_display_table);
2158 return dp;
2161 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2162 doc: /* Set WINDOW's display-table to TABLE.
2163 WINDOW must be a live window and defaults to the selected one. */)
2164 (register Lisp_Object window, Lisp_Object table)
2166 wset_display_table (decode_live_window (window), table);
2167 return table;
2170 /* Record info on buffer window W is displaying
2171 when it is about to cease to display that buffer. */
2172 static void
2173 unshow_buffer (register struct window *w)
2175 Lisp_Object buf = w->contents;
2176 struct buffer *b = XBUFFER (buf);
2178 eassert (b == XMARKER (w->pointm)->buffer);
2180 #if 0
2181 if (w == XWINDOW (selected_window)
2182 || ! EQ (buf, XWINDOW (selected_window)->contents))
2183 /* Do this except when the selected window's buffer
2184 is being removed from some other window. */
2185 #endif
2186 /* last_window_start records the start position that this buffer
2187 had in the last window to be disconnected from it.
2188 Now that this statement is unconditional,
2189 it is possible for the buffer to be displayed in the
2190 selected window, while last_window_start reflects another
2191 window which was recently showing the same buffer.
2192 Some people might say that might be a good thing. Let's see. */
2193 b->last_window_start = marker_position (w->start);
2195 /* Point in the selected window's buffer
2196 is actually stored in that buffer, and the window's pointm isn't used.
2197 So don't clobber point in that buffer. */
2198 if (! EQ (buf, XWINDOW (selected_window)->contents)
2199 /* Don't clobber point in current buffer either (this could be
2200 useful in connection with bug#12208).
2201 && XBUFFER (buf) != current_buffer */
2202 /* This line helps to fix Horsley's testbug.el bug. */
2203 && !(WINDOWP (BVAR (b, last_selected_window))
2204 && w != XWINDOW (BVAR (b, last_selected_window))
2205 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2206 temp_set_point_both (b,
2207 clip_to_bounds (BUF_BEGV (b),
2208 marker_position (w->pointm),
2209 BUF_ZV (b)),
2210 clip_to_bounds (BUF_BEGV_BYTE (b),
2211 marker_byte_position (w->pointm),
2212 BUF_ZV_BYTE (b)));
2214 if (WINDOWP (BVAR (b, last_selected_window))
2215 && w == XWINDOW (BVAR (b, last_selected_window)))
2216 bset_last_selected_window (b, Qnil);
2219 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2220 means change window structure only. Otherwise store geometry and
2221 other settings as well. */
2222 static void
2223 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2225 register Lisp_Object tem;
2226 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2228 /* If OLD is its frame's root window, then NEW is the new
2229 root window for that frame. */
2230 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2231 fset_root_window (XFRAME (o->frame), new);
2233 if (setflag)
2235 n->pixel_left = o->pixel_left;
2236 n->pixel_top = o->pixel_top;
2237 n->pixel_width = o->pixel_width;
2238 n->pixel_height = o->pixel_height;
2239 n->left_col = o->left_col;
2240 n->top_line = o->top_line;
2241 n->total_cols = o->total_cols;
2242 n->total_lines = o->total_lines;
2243 wset_normal_cols (n, o->normal_cols);
2244 wset_normal_cols (o, make_float (1.0));
2245 wset_normal_lines (n, o->normal_lines);
2246 wset_normal_lines (o, make_float (1.0));
2247 n->desired_matrix = n->current_matrix = 0;
2248 n->vscroll = 0;
2249 memset (&n->cursor, 0, sizeof (n->cursor));
2250 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2251 n->last_cursor_vpos = 0;
2252 #ifdef HAVE_WINDOW_SYSTEM
2253 n->phys_cursor_type = NO_CURSOR;
2254 n->phys_cursor_width = -1;
2255 #endif
2256 n->must_be_updated_p = 0;
2257 n->pseudo_window_p = 0;
2258 n->window_end_vpos = 0;
2259 n->window_end_pos = 0;
2260 n->window_end_valid = 0;
2263 tem = o->next;
2264 wset_next (n, tem);
2265 if (!NILP (tem))
2266 wset_prev (XWINDOW (tem), new);
2268 tem = o->prev;
2269 wset_prev (n, tem);
2270 if (!NILP (tem))
2271 wset_next (XWINDOW (tem), new);
2273 tem = o->parent;
2274 wset_parent (n, tem);
2275 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2276 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2279 /* If window WINDOW and its parent window are iso-combined, merge
2280 WINDOW's children into those of its parent window and mark WINDOW as
2281 deleted. */
2283 static void
2284 recombine_windows (Lisp_Object window)
2286 struct window *w, *p, *c;
2287 Lisp_Object parent, child;
2288 bool horflag;
2290 w = XWINDOW (window);
2291 parent = w->parent;
2292 if (!NILP (parent) && NILP (w->combination_limit))
2294 p = XWINDOW (parent);
2295 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2296 && p->horizontal == w->horizontal)
2297 /* WINDOW and PARENT are both either a vertical or a horizontal
2298 combination. */
2300 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2301 child = w->contents;
2302 c = XWINDOW (child);
2304 /* Splice WINDOW's children into its parent's children and
2305 assign new normal sizes. */
2306 if (NILP (w->prev))
2307 wset_combination (p, horflag, child);
2308 else
2310 wset_prev (c, w->prev);
2311 wset_next (XWINDOW (w->prev), child);
2314 while (c)
2316 wset_parent (c, parent);
2318 if (horflag)
2319 wset_normal_cols
2320 (c, make_float ((double) c->pixel_width
2321 / (double) p->pixel_width));
2322 else
2323 wset_normal_lines
2324 (c, make_float ((double) c->pixel_height
2325 / (double) p->pixel_height));
2327 if (NILP (c->next))
2329 if (!NILP (w->next))
2331 wset_next (c, w->next);
2332 wset_prev (XWINDOW (c->next), child);
2335 c = 0;
2337 else
2339 child = c->next;
2340 c = XWINDOW (child);
2344 /* WINDOW can be deleted now. */
2345 wset_combination (w, 0, Qnil);
2350 /* If WINDOW can be deleted, delete it. */
2351 static void
2352 delete_deletable_window (Lisp_Object window)
2354 if (!NILP (call1 (Qwindow_deletable_p, window)))
2355 call1 (Qdelete_window, window);
2358 /***********************************************************************
2359 Window List
2360 ***********************************************************************/
2362 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2363 pointer. This is a callback function for foreach_window, used in
2364 the window_list function. */
2366 static int
2367 add_window_to_list (struct window *w, void *user_data)
2369 Lisp_Object *list = user_data;
2370 Lisp_Object window;
2371 XSETWINDOW (window, w);
2372 *list = Fcons (window, *list);
2373 return 1;
2377 /* Return a list of all windows, for use by next_window. If
2378 Vwindow_list is a list, return that list. Otherwise, build a new
2379 list, cache it in Vwindow_list, and return that. */
2381 Lisp_Object
2382 window_list (void)
2384 if (!CONSP (Vwindow_list))
2386 Lisp_Object tail, frame;
2388 Vwindow_list = Qnil;
2389 FOR_EACH_FRAME (tail, frame)
2391 Lisp_Object args[2];
2393 /* We are visiting windows in canonical order, and add
2394 new windows at the front of args[1], which means we
2395 have to reverse this list at the end. */
2396 args[1] = Qnil;
2397 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2398 args[0] = Vwindow_list;
2399 args[1] = Fnreverse (args[1]);
2400 Vwindow_list = Fnconc (2, args);
2404 return Vwindow_list;
2408 /* Value is non-zero if WINDOW satisfies the constraints given by
2409 OWINDOW, MINIBUF and ALL_FRAMES.
2411 MINIBUF t means WINDOW may be minibuffer windows.
2412 `lambda' means WINDOW may not be a minibuffer window.
2413 a window means a specific minibuffer window
2415 ALL_FRAMES t means search all frames,
2416 nil means search just current frame,
2417 `visible' means search just visible frames on the
2418 current terminal,
2419 0 means search visible and iconified frames on the
2420 current terminal,
2421 a window means search the frame that window belongs to,
2422 a frame means consider windows on that frame, only. */
2424 static bool
2425 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2426 Lisp_Object minibuf, Lisp_Object all_frames)
2428 struct window *w = XWINDOW (window);
2429 struct frame *f = XFRAME (w->frame);
2430 bool candidate_p = 1;
2432 if (!BUFFERP (w->contents))
2433 candidate_p = 0;
2434 else if (MINI_WINDOW_P (w)
2435 && (EQ (minibuf, Qlambda)
2436 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2438 /* If MINIBUF is `lambda' don't consider any mini-windows.
2439 If it is a window, consider only that one. */
2440 candidate_p = 0;
2442 else if (EQ (all_frames, Qt))
2443 candidate_p = 1;
2444 else if (NILP (all_frames))
2446 eassert (WINDOWP (owindow));
2447 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2449 else if (EQ (all_frames, Qvisible))
2451 candidate_p = FRAME_VISIBLE_P (f)
2452 && (FRAME_TERMINAL (XFRAME (w->frame))
2453 == FRAME_TERMINAL (XFRAME (selected_frame)));
2456 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2458 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2459 #ifdef HAVE_X_WINDOWS
2460 /* Yuck!! If we've just created the frame and the
2461 window-manager requested the user to place it
2462 manually, the window may still not be considered
2463 `visible'. I'd argue it should be at least
2464 something like `iconified', but don't know how to do
2465 that yet. --Stef */
2466 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2467 && !f->output_data.x->has_been_visible)
2468 #endif
2470 && (FRAME_TERMINAL (XFRAME (w->frame))
2471 == FRAME_TERMINAL (XFRAME (selected_frame)));
2473 else if (WINDOWP (all_frames))
2474 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2475 || EQ (XWINDOW (all_frames)->frame, w->frame)
2476 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2477 else if (FRAMEP (all_frames))
2478 candidate_p = EQ (all_frames, w->frame);
2480 return candidate_p;
2484 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2485 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2486 MINIBUF, and ALL_FRAMES. */
2488 static void
2489 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2491 struct window *w = decode_live_window (*window);
2493 XSETWINDOW (*window, w);
2494 /* MINIBUF nil may or may not include minibuffers. Decide if it
2495 does. */
2496 if (NILP (*minibuf))
2497 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2498 else if (!EQ (*minibuf, Qt))
2499 *minibuf = Qlambda;
2501 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2502 => count none of them, or a specific minibuffer window (the
2503 active one) to count. */
2505 /* ALL_FRAMES nil doesn't specify which frames to include. */
2506 if (NILP (*all_frames))
2507 *all_frames
2508 = (!EQ (*minibuf, Qlambda)
2509 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2510 : Qnil);
2511 else if (EQ (*all_frames, Qvisible))
2513 else if (EQ (*all_frames, make_number (0)))
2515 else if (FRAMEP (*all_frames))
2517 else if (!EQ (*all_frames, Qt))
2518 *all_frames = Qnil;
2522 /* Return the next or previous window of WINDOW in cyclic ordering
2523 of windows. NEXT_P non-zero means return the next window. See the
2524 documentation string of next-window for the meaning of MINIBUF and
2525 ALL_FRAMES. */
2527 static Lisp_Object
2528 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2530 decode_next_window_args (&window, &minibuf, &all_frames);
2532 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2533 return the first window on the frame. */
2534 if (FRAMEP (all_frames)
2535 && !EQ (all_frames, XWINDOW (window)->frame))
2536 return Fframe_first_window (all_frames);
2538 if (next_p)
2540 Lisp_Object list;
2542 /* Find WINDOW in the list of all windows. */
2543 list = Fmemq (window, window_list ());
2545 /* Scan forward from WINDOW to the end of the window list. */
2546 if (CONSP (list))
2547 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2548 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2549 break;
2551 /* Scan from the start of the window list up to WINDOW. */
2552 if (!CONSP (list))
2553 for (list = Vwindow_list;
2554 CONSP (list) && !EQ (XCAR (list), window);
2555 list = XCDR (list))
2556 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2557 break;
2559 if (CONSP (list))
2560 window = XCAR (list);
2562 else
2564 Lisp_Object candidate, list;
2566 /* Scan through the list of windows for candidates. If there are
2567 candidate windows in front of WINDOW, the last one of these
2568 is the one we want. If there are candidates following WINDOW
2569 in the list, again the last one of these is the one we want. */
2570 candidate = Qnil;
2571 for (list = window_list (); CONSP (list); list = XCDR (list))
2573 if (EQ (XCAR (list), window))
2575 if (WINDOWP (candidate))
2576 break;
2578 else if (candidate_window_p (XCAR (list), window, minibuf,
2579 all_frames))
2580 candidate = XCAR (list);
2583 if (WINDOWP (candidate))
2584 window = candidate;
2587 return window;
2591 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2592 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2593 WINDOW must be a live window and defaults to the selected one. The
2594 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2595 consider.
2597 MINIBUF nil or omitted means consider the minibuffer window only if the
2598 minibuffer is active. MINIBUF t means consider the minibuffer window
2599 even if the minibuffer is not active. Any other value means do not
2600 consider the minibuffer window even if the minibuffer is active.
2602 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2603 plus the minibuffer window if specified by the MINIBUF argument. If the
2604 minibuffer counts, consider all windows on all frames that share that
2605 minibuffer too. The following non-nil values of ALL-FRAMES have special
2606 meanings:
2608 - t means consider all windows on all existing frames.
2610 - `visible' means consider all windows on all visible frames.
2612 - 0 (the number zero) means consider all windows on all visible and
2613 iconified frames.
2615 - A frame means consider all windows on that frame only.
2617 Anything else means consider all windows on WINDOW's frame and no
2618 others.
2620 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2621 `next-window' to iterate through the entire cycle of acceptable
2622 windows, eventually ending up back at the window you started with.
2623 `previous-window' traverses the same cycle, in the reverse order. */)
2624 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2626 return next_window (window, minibuf, all_frames, 1);
2630 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2631 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2632 WINDOW must be a live window and defaults to the selected one. The
2633 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2634 consider.
2636 MINIBUF nil or omitted means consider the minibuffer window only if the
2637 minibuffer is active. MINIBUF t means consider the minibuffer window
2638 even if the minibuffer is not active. Any other value means do not
2639 consider the minibuffer window even if the minibuffer is active.
2641 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2642 plus the minibuffer window if specified by the MINIBUF argument. If the
2643 minibuffer counts, consider all windows on all frames that share that
2644 minibuffer too. The following non-nil values of ALL-FRAMES have special
2645 meanings:
2647 - t means consider all windows on all existing frames.
2649 - `visible' means consider all windows on all visible frames.
2651 - 0 (the number zero) means consider all windows on all visible and
2652 iconified frames.
2654 - A frame means consider all windows on that frame only.
2656 Anything else means consider all windows on WINDOW's frame and no
2657 others.
2659 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2660 use `previous-window' to iterate through the entire cycle of
2661 acceptable windows, eventually ending up back at the window you
2662 started with. `next-window' traverses the same cycle, in the
2663 reverse order. */)
2664 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2666 return next_window (window, minibuf, all_frames, 0);
2670 /* Return a list of windows in cyclic ordering. Arguments are like
2671 for `next-window'. */
2673 static Lisp_Object
2674 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2676 Lisp_Object tail, list, rest;
2678 decode_next_window_args (&window, &minibuf, &all_frames);
2679 list = Qnil;
2681 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2682 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2683 list = Fcons (XCAR (tail), list);
2685 /* Rotate the list to start with WINDOW. */
2686 list = Fnreverse (list);
2687 rest = Fmemq (window, list);
2688 if (!NILP (rest) && !EQ (rest, list))
2690 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2692 XSETCDR (tail, Qnil);
2693 list = nconc2 (rest, list);
2695 return list;
2699 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2700 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2701 FRAME nil or omitted means use the selected frame.
2702 WINDOW nil or omitted means use the window selected within FRAME.
2703 MINIBUF t means include the minibuffer window, even if it isn't active.
2704 MINIBUF nil or omitted means include the minibuffer window only
2705 if it's active.
2706 MINIBUF neither nil nor t means never include the minibuffer window. */)
2707 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2709 if (NILP (window))
2710 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2711 CHECK_WINDOW (window);
2712 if (NILP (frame))
2713 frame = selected_frame;
2715 if (!EQ (frame, XWINDOW (window)->frame))
2716 error ("Window is on a different frame");
2718 return window_list_1 (window, minibuf, frame);
2722 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2723 doc: /* Return a list of all live windows.
2724 WINDOW specifies the first window to list and defaults to the selected
2725 window.
2727 Optional argument MINIBUF nil or omitted means consider the minibuffer
2728 window only if the minibuffer is active. MINIBUF t means consider the
2729 minibuffer window even if the minibuffer is not active. Any other value
2730 means do not consider the minibuffer window even if the minibuffer is
2731 active.
2733 Optional argument ALL-FRAMES nil or omitted means consider all windows
2734 on WINDOW's frame, plus the minibuffer window if specified by the
2735 MINIBUF argument. If the minibuffer counts, consider all windows on all
2736 frames that share that minibuffer too. The following non-nil values of
2737 ALL-FRAMES have special meanings:
2739 - t means consider all windows on all existing frames.
2741 - `visible' means consider all windows on all visible frames.
2743 - 0 (the number zero) means consider all windows on all visible and
2744 iconified frames.
2746 - A frame means consider all windows on that frame only.
2748 Anything else means consider all windows on WINDOW's frame and no
2749 others.
2751 If WINDOW is not on the list of windows returned, some other window will
2752 be listed first but no error is signaled. */)
2753 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2755 return window_list_1 (window, minibuf, all_frames);
2758 /* Look at all windows, performing an operation specified by TYPE
2759 with argument OBJ.
2760 If FRAMES is Qt, look at all frames;
2761 Qnil, look at just the selected frame;
2762 Qvisible, look at visible frames;
2763 a frame, just look at windows on that frame.
2764 If MINI is non-zero, perform the operation on minibuffer windows too. */
2766 enum window_loop
2768 WINDOW_LOOP_UNUSED,
2769 GET_BUFFER_WINDOW, /* Arg is buffer */
2770 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2771 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2772 CHECK_ALL_WINDOWS /* Arg is ignored */
2775 static Lisp_Object
2776 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2778 Lisp_Object window, windows, best_window, frame_arg;
2779 int frame_best_window_flag = 0;
2780 struct frame *f;
2781 struct gcpro gcpro1;
2783 /* If we're only looping through windows on a particular frame,
2784 frame points to that frame. If we're looping through windows
2785 on all frames, frame is 0. */
2786 if (FRAMEP (frames))
2787 f = XFRAME (frames);
2788 else if (NILP (frames))
2789 f = SELECTED_FRAME ();
2790 else
2791 f = NULL;
2793 if (f)
2794 frame_arg = Qlambda;
2795 else if (EQ (frames, make_number (0)))
2796 frame_arg = frames;
2797 else if (EQ (frames, Qvisible))
2798 frame_arg = frames;
2799 else
2800 frame_arg = Qt;
2802 /* frame_arg is Qlambda to stick to one frame,
2803 Qvisible to consider all visible frames,
2804 or Qt otherwise. */
2806 /* Pick a window to start with. */
2807 if (WINDOWP (obj))
2808 window = obj;
2809 else if (f)
2810 window = FRAME_SELECTED_WINDOW (f);
2811 else
2812 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2814 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2815 GCPRO1 (windows);
2816 best_window = Qnil;
2818 for (; CONSP (windows); windows = XCDR (windows))
2820 struct window *w;
2822 window = XCAR (windows);
2823 w = XWINDOW (window);
2825 /* Note that we do not pay attention here to whether the frame
2826 is visible, since Fwindow_list skips non-visible frames if
2827 that is desired, under the control of frame_arg. */
2828 if (!MINI_WINDOW_P (w)
2829 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2830 consider all windows. */
2831 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2832 || (mini && minibuf_level > 0))
2833 switch (type)
2835 case GET_BUFFER_WINDOW:
2836 if (EQ (w->contents, obj)
2837 /* Don't find any minibuffer window except the one that
2838 is currently in use. */
2839 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2841 if (EQ (window, selected_window))
2842 /* Preferably return the selected window. */
2843 RETURN_UNGCPRO (window);
2844 else if (EQ (XWINDOW (window)->frame, selected_frame)
2845 && !frame_best_window_flag)
2846 /* Prefer windows on the current frame (but don't
2847 choose another one if we have one already). */
2849 best_window = window;
2850 frame_best_window_flag = 1;
2852 else if (NILP (best_window))
2853 best_window = window;
2855 break;
2857 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2858 /* We could simply check whether the buffer shown by window
2859 is live, and show another buffer in case it isn't. */
2860 if (EQ (w->contents, obj))
2862 /* Undedicate WINDOW. */
2863 wset_dedicated (w, Qnil);
2864 /* Make WINDOW show the buffer returned by
2865 other_buffer_safely, don't run any hooks. */
2866 set_window_buffer
2867 (window, other_buffer_safely (w->contents), 0, 0);
2868 /* If WINDOW is the selected window, make its buffer
2869 current. But do so only if the window shows the
2870 current buffer (Bug#6454). */
2871 if (EQ (window, selected_window)
2872 && XBUFFER (w->contents) == current_buffer)
2873 Fset_buffer (w->contents);
2875 break;
2877 case REDISPLAY_BUFFER_WINDOWS:
2878 if (EQ (w->contents, obj))
2880 mark_window_display_accurate (window, 0);
2881 w->update_mode_line = 1;
2882 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2883 update_mode_lines = 27;
2884 best_window = window;
2886 break;
2888 /* Check for a leaf window that has a killed buffer
2889 or broken markers. */
2890 case CHECK_ALL_WINDOWS:
2891 if (BUFFERP (w->contents))
2893 struct buffer *b = XBUFFER (w->contents);
2895 if (!BUFFER_LIVE_P (b))
2896 emacs_abort ();
2897 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2898 emacs_abort ();
2899 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2900 emacs_abort ();
2902 break;
2904 case WINDOW_LOOP_UNUSED:
2905 break;
2909 UNGCPRO;
2910 return best_window;
2913 /* Used for debugging. Abort if any window has a dead buffer. */
2915 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2916 void
2917 check_all_windows (void)
2919 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2922 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2923 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2924 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2925 the current buffer.
2927 The optional argument ALL-FRAMES specifies the frames to consider:
2929 - t means consider all windows on all existing frames.
2931 - `visible' means consider all windows on all visible frames.
2933 - 0 (the number zero) means consider all windows on all visible
2934 and iconified frames.
2936 - A frame means consider all windows on that frame only.
2938 Any other value of ALL-FRAMES means consider all windows on the
2939 selected frame and no others. */)
2940 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2942 Lisp_Object buffer;
2944 if (NILP (buffer_or_name))
2945 buffer = Fcurrent_buffer ();
2946 else
2947 buffer = Fget_buffer (buffer_or_name);
2949 if (BUFFERP (buffer))
2950 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2951 else
2952 return Qnil;
2955 static Lisp_Object
2956 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2958 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2962 static Lisp_Object
2963 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2965 return call2(Qwindow_pixel_to_total, frame, horizontal);
2969 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2970 Sdelete_other_windows_internal, 0, 2, "",
2971 doc: /* Make WINDOW fill its frame.
2972 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2973 and defaults to the selected one.
2975 Optional argument ROOT, if non-nil, must specify an internal window such
2976 that WINDOW is in its window subtree. If this is the case, replace ROOT
2977 by WINDOW and leave alone any windows not part of ROOT's subtree.
2979 When WINDOW is live try to reduce display jumps by keeping the text
2980 previously visible in WINDOW in the same place on the frame. Doing this
2981 depends on the value of (window-start WINDOW), so if calling this
2982 function in a program gives strange scrolling, make sure the
2983 window-start value is reasonable when this function is called. */)
2984 (Lisp_Object window, Lisp_Object root)
2986 struct window *w, *r, *s;
2987 struct frame *f;
2988 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2989 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2990 int top IF_LINT (= 0), new_top, resize_failed;
2992 w = decode_valid_window (window);
2993 XSETWINDOW (window, w);
2994 f = XFRAME (w->frame);
2996 if (NILP (root))
2997 /* ROOT is the frame's root window. */
2999 root = FRAME_ROOT_WINDOW (f);
3000 r = XWINDOW (root);
3002 else
3003 /* ROOT must be an ancestor of WINDOW. */
3005 r = decode_valid_window (root);
3006 pwindow = XWINDOW (window)->parent;
3007 while (!NILP (pwindow))
3008 if (EQ (pwindow, root))
3009 break;
3010 else
3011 pwindow = XWINDOW (pwindow)->parent;
3012 if (!EQ (pwindow, root))
3013 error ("Specified root is not an ancestor of specified window");
3016 if (EQ (window, root))
3017 /* A noop. */
3018 return Qnil;
3019 /* I don't understand the "top > 0" part below. If we deal with a
3020 standalone minibuffer it would have been caught by the preceding
3021 test. */
3022 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3023 error ("Can't expand minibuffer to full frame");
3025 if (BUFFERP (w->contents))
3027 startpos = marker_position (w->start);
3028 startbyte = marker_byte_position (w->start);
3029 top = (WINDOW_TOP_EDGE_LINE (w)
3030 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3031 /* Make sure WINDOW is the frame's selected window. */
3032 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3034 if (EQ (selected_frame, w->frame))
3035 Fselect_window (window, Qnil);
3036 else
3037 fset_selected_window (f, window);
3040 else
3042 /* See if the frame's selected window is a part of the window
3043 subtree rooted at WINDOW, by finding all the selected window's
3044 parents and comparing each one with WINDOW. If it isn't we
3045 need a new selected window for this frame. */
3046 swindow = FRAME_SELECTED_WINDOW (f);
3047 while (1)
3049 pwindow = swindow;
3050 while (!NILP (pwindow) && !EQ (window, pwindow))
3051 pwindow = XWINDOW (pwindow)->parent;
3053 if (EQ (window, pwindow))
3054 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3055 as the new selected window. */
3056 break;
3057 else
3058 /* Else try the previous window of SWINDOW. */
3059 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3062 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3064 if (EQ (selected_frame, w->frame))
3065 Fselect_window (swindow, Qnil);
3066 else
3067 fset_selected_window (f, swindow);
3071 block_input ();
3072 if (!FRAME_INITIAL_P (f))
3074 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3076 /* We are going to free the glyph matrices of WINDOW, and with
3077 that we might lose any information about glyph rows that have
3078 some of their glyphs highlighted in mouse face. (These rows
3079 are marked with a non-zero mouse_face_p flag.) If WINDOW
3080 indeed has some glyphs highlighted in mouse face, signal to
3081 frame's up-to-date hook that mouse highlight was overwritten,
3082 so that it will arrange for redisplaying the highlight. */
3083 if (EQ (hlinfo->mouse_face_window, window))
3084 reset_mouse_highlight (hlinfo);
3086 free_window_matrices (r);
3088 fset_redisplay (f);
3089 Vwindow_list = Qnil;
3090 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3091 resize_failed = 0;
3093 if (!WINDOW_LEAF_P (w))
3095 /* Resize child windows vertically. */
3096 XSETINT (delta, r->pixel_height - w->pixel_height);
3097 w->pixel_top = r->pixel_top;
3098 w->top_line = r->top_line;
3099 resize_root_window (window, delta, Qnil, Qnil, Qt);
3100 if (window_resize_check (w, 0))
3102 window_resize_apply (w, 0);
3103 window_pixel_to_total (w->frame, Qnil);
3105 else
3107 resize_root_window (window, delta, Qnil, Qt, Qt);
3108 if (window_resize_check (w, 0))
3110 window_resize_apply (w, 0);
3111 window_pixel_to_total (w->frame, Qnil);
3113 else
3114 resize_failed = 1;
3117 /* Resize child windows horizontally. */
3118 if (!resize_failed)
3120 w->left_col = r->left_col;
3121 w->pixel_left = r->pixel_left;
3122 XSETINT (delta, r->pixel_width - w->pixel_width);
3123 resize_root_window (window, delta, Qt, Qnil, Qt);
3124 if (window_resize_check (w, 1))
3126 window_resize_apply (w, 1);
3127 window_pixel_to_total (w->frame, Qt);
3129 else
3131 resize_root_window (window, delta, Qt, Qt, Qt);
3132 if (window_resize_check (w, 1))
3134 window_resize_apply (w, 1);
3135 window_pixel_to_total (w->frame, Qt);
3137 else
3138 resize_failed = 1;
3142 if (resize_failed)
3143 /* Play safe, if we still can ... */
3145 window = swindow;
3146 w = XWINDOW (window);
3150 /* Cleanly unlink WINDOW from window-tree. */
3151 if (!NILP (w->prev))
3152 /* Get SIBLING above (on the left of) WINDOW. */
3154 sibling = w->prev;
3155 s = XWINDOW (sibling);
3156 wset_next (s, w->next);
3157 if (!NILP (s->next))
3158 wset_prev (XWINDOW (s->next), sibling);
3160 else
3161 /* Get SIBLING below (on the right of) WINDOW. */
3163 sibling = w->next;
3164 s = XWINDOW (sibling);
3165 wset_prev (s, Qnil);
3166 wset_combination (XWINDOW (w->parent),
3167 XWINDOW (w->parent)->horizontal, sibling);
3170 /* Delete ROOT and all child windows of ROOT. */
3171 if (WINDOWP (r->contents))
3173 delete_all_child_windows (r->contents);
3174 wset_combination (r, 0, Qnil);
3177 replace_window (root, window, 1);
3179 /* This must become SWINDOW anyway ....... */
3180 if (BUFFERP (w->contents) && !resize_failed)
3182 /* Try to minimize scrolling, by setting the window start to the
3183 point will cause the text at the old window start to be at the
3184 same place on the frame. But don't try to do this if the
3185 window start is outside the visible portion (as might happen
3186 when the display is not current, due to typeahead). */
3187 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3188 if (new_top != top
3189 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3190 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3192 struct position pos;
3193 struct buffer *obuf = current_buffer;
3195 Fset_buffer (w->contents);
3196 /* This computation used to temporarily move point, but that
3197 can have unwanted side effects due to text properties. */
3198 pos = *vmotion (startpos, startbyte, -top, w);
3200 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3201 w->window_end_valid = 0;
3202 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3203 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3204 /* We need to do this, so that the window-scroll-functions
3205 get called. */
3206 w->optional_new_start = 1;
3208 set_buffer_internal (obuf);
3212 adjust_frame_glyphs (f);
3213 unblock_input ();
3215 run_window_configuration_change_hook (f);
3217 return Qnil;
3221 void
3222 replace_buffer_in_windows (Lisp_Object buffer)
3224 call1 (Qreplace_buffer_in_windows, buffer);
3227 /* If BUFFER is shown in a window, safely replace it with some other
3228 buffer in all windows of all frames, even those on other keyboards. */
3230 void
3231 replace_buffer_in_windows_safely (Lisp_Object buffer)
3233 if (buffer_window_count (XBUFFER (buffer)))
3235 Lisp_Object tail, frame;
3237 /* A single call to window_loop won't do the job because it only
3238 considers frames on the current keyboard. So loop manually over
3239 frames, and handle each one. */
3240 FOR_EACH_FRAME (tail, frame)
3241 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3245 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3246 minimum allowable size. PIXELWISE means interpret these as pixel
3247 sizes. */
3249 void
3250 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3252 /* For height, we have to see:
3253 how many windows the frame has at minimum (one or two),
3254 and whether it has a menu bar or other special stuff at the top. */
3255 if (pixelwise)
3257 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3258 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3260 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3261 min_height = 2 * min_height;
3263 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3264 min_height += FRAME_INTERNAL_BORDER_WIDTH (frame);
3266 if (*height < min_height)
3267 *height = min_height;
3268 if (*width < min_width)
3269 *width = min_width;
3271 else
3273 int min_height
3274 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3275 ? MIN_SAFE_WINDOW_HEIGHT
3276 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3278 if (FRAME_TOP_MARGIN (frame) > 0)
3279 min_height += FRAME_TOP_MARGIN (frame);
3281 if (*height < min_height)
3282 *height = min_height;
3283 if (*width < MIN_SAFE_WINDOW_WIDTH)
3284 *width = MIN_SAFE_WINDOW_WIDTH;
3288 /* Adjust the margins of window W if text area is too small.
3289 Return 1 if window width is ok after adjustment; 0 if window
3290 is still too narrow. */
3292 static int
3293 adjust_window_margins (struct window *w)
3295 int box_width = (WINDOW_PIXEL_WIDTH (w)
3296 - WINDOW_FRINGES_WIDTH (w)
3297 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3298 int margin_width = WINDOW_MARGINS_WIDTH (w);
3300 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3301 return 1;
3303 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3304 return 0;
3305 else
3306 /* Window's text area is too narrow, but reducing the window
3307 margins will fix that. */
3309 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3311 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3313 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3315 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3316 w->left_margin_cols = w->right_margin_cols =
3317 margin_width / (2 * unit);
3318 else
3319 w->right_margin_cols = margin_width / unit;
3321 else
3322 w->left_margin_cols = margin_width / unit;
3324 return 1;
3328 /* The following three routines are needed for running a window's
3329 configuration change hook. */
3330 static void
3331 run_funs (Lisp_Object funs)
3333 for (; CONSP (funs); funs = XCDR (funs))
3334 if (!EQ (XCAR (funs), Qt))
3335 call0 (XCAR (funs));
3338 static void
3339 select_window_norecord (Lisp_Object window)
3341 if (WINDOW_LIVE_P (window))
3342 Fselect_window (window, Qt);
3345 static void
3346 select_frame_norecord (Lisp_Object frame)
3348 if (FRAME_LIVE_P (XFRAME (frame)))
3349 Fselect_frame (frame, Qt);
3352 void
3353 run_window_configuration_change_hook (struct frame *f)
3355 ptrdiff_t count = SPECPDL_INDEX ();
3356 Lisp_Object frame, global_wcch
3357 = Fdefault_value (Qwindow_configuration_change_hook);
3358 XSETFRAME (frame, f);
3360 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3361 return;
3363 /* Use the right buffer. Matters when running the local hooks. */
3364 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3366 record_unwind_current_buffer ();
3367 Fset_buffer (Fwindow_buffer (Qnil));
3370 if (SELECTED_FRAME () != f)
3372 record_unwind_protect (select_frame_norecord, selected_frame);
3373 select_frame_norecord (frame);
3376 /* Look for buffer-local values. */
3378 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3379 for (; CONSP (windows); windows = XCDR (windows))
3381 Lisp_Object window = XCAR (windows);
3382 Lisp_Object buffer = Fwindow_buffer (window);
3383 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3384 buffer)))
3386 ptrdiff_t inner_count = SPECPDL_INDEX ();
3387 record_unwind_protect (select_window_norecord, selected_window);
3388 select_window_norecord (window);
3389 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3390 buffer));
3391 unbind_to (inner_count, Qnil);
3396 run_funs (global_wcch);
3397 unbind_to (count, Qnil);
3400 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3401 Srun_window_configuration_change_hook, 0, 1, 0,
3402 doc: /* Run `window-configuration-change-hook' for FRAME.
3403 If FRAME is omitted or nil, it defaults to the selected frame. */)
3404 (Lisp_Object frame)
3406 run_window_configuration_change_hook (decode_live_frame (frame));
3407 return Qnil;
3410 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3411 Srun_window_scroll_functions, 0, 1, 0,
3412 doc: /* Run `window-scroll-functions' for WINDOW.
3413 If WINDOW is omitted or nil, it defaults to the selected window. */)
3414 (Lisp_Object window)
3416 if (! NILP (Vwindow_scroll_functions))
3417 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3418 Fmarker_position (decode_live_window (window)->start));
3419 return Qnil;
3422 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3423 to run hooks. See make_frame for a case where it's not allowed.
3424 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3425 scroll-bar settings of the window are not reset from the buffer's
3426 local settings. */
3428 void
3429 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3430 bool run_hooks_p, bool keep_margins_p)
3432 struct window *w = XWINDOW (window);
3433 struct buffer *b = XBUFFER (buffer);
3434 ptrdiff_t count = SPECPDL_INDEX ();
3435 bool samebuf = EQ (buffer, w->contents);
3437 wset_buffer (w, buffer);
3439 if (EQ (window, selected_window))
3440 bset_last_selected_window (b, window);
3442 /* Let redisplay errors through. */
3443 b->display_error_modiff = 0;
3445 /* Update time stamps of buffer display. */
3446 if (INTEGERP (BVAR (b, display_count)))
3447 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3448 bset_display_time (b, Fcurrent_time ());
3450 w->window_end_pos = 0;
3451 w->window_end_vpos = 0;
3452 w->last_cursor_vpos = 0;
3454 if (!(keep_margins_p && samebuf))
3455 { /* If we're not actually changing the buffer, don't reset hscroll and
3456 vscroll. This case happens for example when called from
3457 change_frame_size_1, where we use a dummy call to
3458 Fset_window_buffer on the frame's selected window (and no other)
3459 just in order to run window-configuration-change-hook.
3460 Resetting hscroll and vscroll here is problematic for things like
3461 image-mode and doc-view-mode since it resets the image's position
3462 whenever we resize the frame. */
3463 w->hscroll = w->min_hscroll = 0;
3464 w->vscroll = 0;
3465 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3466 set_marker_restricted (w->start,
3467 make_number (b->last_window_start),
3468 buffer);
3469 w->start_at_line_beg = 0;
3470 w->force_start = 0;
3472 /* Maybe we could move this into the `if' but it's not obviously safe and
3473 I doubt it's worth the trouble. */
3474 wset_redisplay (w);
3475 w->update_mode_line = true;
3477 /* We must select BUFFER for running the window-scroll-functions. */
3478 /* We can't check ! NILP (Vwindow_scroll_functions) here
3479 because that might itself be a local variable. */
3480 if (window_initialized)
3482 record_unwind_current_buffer ();
3483 Fset_buffer (buffer);
3486 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3488 if (!keep_margins_p)
3490 /* Set left and right marginal area width etc. from buffer. */
3491 set_window_fringes (w, BVAR (b, left_fringe_width),
3492 BVAR (b, right_fringe_width),
3493 BVAR (b, fringes_outside_margins));
3494 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3495 BVAR (b, vertical_scroll_bar_type), Qnil);
3496 set_window_margins (w, BVAR (b, left_margin_cols),
3497 BVAR (b, right_margin_cols));
3498 apply_window_adjustment (w);
3501 if (run_hooks_p)
3503 if (! NILP (Vwindow_scroll_functions))
3504 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3505 Fmarker_position (w->start));
3506 if (!samebuf)
3507 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3510 unbind_to (count, Qnil);
3513 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3514 doc: /* Make WINDOW display BUFFER-OR-NAME.
3515 WINDOW must be a live window and defaults to the selected one.
3516 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3518 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3519 display margins, fringe widths, and scroll bar settings are preserved;
3520 the default is to reset these from the local settings for BUFFER-OR-NAME
3521 or the frame defaults. Return nil.
3523 This function throws an error when WINDOW is strongly dedicated to its
3524 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3525 already display BUFFER-OR-NAME.
3527 This function runs `window-scroll-functions' before running
3528 `window-configuration-change-hook'. */)
3529 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3531 register Lisp_Object tem, buffer;
3532 register struct window *w = decode_live_window (window);
3534 XSETWINDOW (window, w);
3535 buffer = Fget_buffer (buffer_or_name);
3536 CHECK_BUFFER (buffer);
3537 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3538 error ("Attempt to display deleted buffer");
3540 tem = w->contents;
3541 if (NILP (tem))
3542 error ("Window is deleted");
3543 else
3545 if (!EQ (tem, buffer))
3547 if (EQ (w->dedicated, Qt))
3548 /* WINDOW is strongly dedicated to its buffer, signal an
3549 error. */
3550 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3551 else
3552 /* WINDOW is weakly dedicated to its buffer, reset
3553 dedication. */
3554 wset_dedicated (w, Qnil);
3556 call1 (Qrecord_window_buffer, window);
3559 unshow_buffer (w);
3562 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3564 return Qnil;
3567 static Lisp_Object
3568 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3570 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3573 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3574 0, 1, 0,
3575 doc: /* Force all windows to be updated on next redisplay.
3576 If optional arg OBJECT is a window, force redisplay of that window only.
3577 If OBJECT is a buffer or buffer name, force redisplay of all windows
3578 displaying that buffer. */)
3579 (Lisp_Object object)
3581 if (NILP (object))
3583 windows_or_buffers_changed = 29;
3584 update_mode_lines = 28;
3585 return Qt;
3588 if (WINDOWP (object))
3590 struct window *w = XWINDOW (object);
3591 mark_window_display_accurate (object, 0);
3592 w->update_mode_line = 1;
3593 if (BUFFERP (w->contents))
3594 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3595 update_mode_lines = 29;
3596 return Qt;
3599 if (STRINGP (object))
3600 object = Fget_buffer (object);
3601 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3602 && buffer_window_count (XBUFFER (object)))
3604 /* If buffer is live and shown in at least one window, find
3605 all windows showing this buffer and force update of them. */
3606 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3607 return NILP (object) ? Qnil : Qt;
3610 /* If nothing suitable was found, just return.
3611 We could signal an error, but this feature will typically be used
3612 asynchronously in timers or process sentinels, so we don't. */
3613 return Qnil;
3616 /* Obsolete since 24.3. */
3617 void
3618 temp_output_buffer_show (register Lisp_Object buf)
3620 register struct buffer *old = current_buffer;
3621 register Lisp_Object window;
3622 register struct window *w;
3624 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3626 Fset_buffer (buf);
3627 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3628 BEGV = BEG;
3629 ZV = Z;
3630 SET_PT (BEG);
3631 set_buffer_internal (old);
3633 if (!NILP (Vtemp_buffer_show_function))
3634 call1 (Vtemp_buffer_show_function, buf);
3635 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3637 if (!EQ (XWINDOW (window)->frame, selected_frame))
3638 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3639 Vminibuf_scroll_window = window;
3640 w = XWINDOW (window);
3641 w->hscroll = 0;
3642 w->min_hscroll = 0;
3643 set_marker_restricted_both (w->start, buf, BEG, BEG);
3644 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3646 /* Run temp-buffer-show-hook, with the chosen window selected
3647 and its buffer current. */
3649 ptrdiff_t count = SPECPDL_INDEX ();
3650 Lisp_Object prev_window, prev_buffer;
3651 prev_window = selected_window;
3652 XSETBUFFER (prev_buffer, old);
3654 /* Select the window that was chosen, for running the hook.
3655 Note: Both Fselect_window and select_window_norecord may
3656 set-buffer to the buffer displayed in the window,
3657 so we need to save the current buffer. --stef */
3658 record_unwind_protect (restore_buffer, prev_buffer);
3659 record_unwind_protect (select_window_norecord, prev_window);
3660 Fselect_window (window, Qt);
3661 Fset_buffer (w->contents);
3662 Frun_hooks (1, &Qtemp_buffer_show_hook);
3663 unbind_to (count, Qnil);
3668 /* Make new window, have it replace WINDOW in window-tree, and make
3669 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3670 horizontal child). */
3671 static void
3672 make_parent_window (Lisp_Object window, bool horflag)
3674 Lisp_Object parent;
3675 register struct window *o, *p;
3677 o = XWINDOW (window);
3678 p = allocate_window ();
3679 memcpy ((char *) p + sizeof (struct vectorlike_header),
3680 (char *) o + sizeof (struct vectorlike_header),
3681 word_size * VECSIZE (struct window));
3682 /* P's buffer slot may change from nil to a buffer... */
3683 adjust_window_count (p, 1);
3684 XSETWINDOW (parent, p);
3686 p->sequence_number = ++sequence_number;
3688 replace_window (window, parent, 1);
3690 wset_next (o, Qnil);
3691 wset_prev (o, Qnil);
3692 wset_parent (o, parent);
3693 /* ...but now P becomes an internal window. */
3694 wset_start (p, Qnil);
3695 wset_pointm (p, Qnil);
3696 wset_buffer (p, Qnil);
3697 wset_combination (p, horflag, window);
3698 wset_combination_limit (p, Qnil);
3699 wset_window_parameters (p, Qnil);
3702 /* Make new window from scratch. */
3703 Lisp_Object
3704 make_window (void)
3706 Lisp_Object window;
3707 register struct window *w;
3709 w = allocate_window ();
3710 /* Initialize Lisp data. Note that allocate_window initializes all
3711 Lisp data to nil, so do it only for slots which should not be nil. */
3712 wset_normal_lines (w, make_float (1.0));
3713 wset_normal_cols (w, make_float (1.0));
3714 wset_new_total (w, make_number (0));
3715 wset_new_normal (w, make_number (0));
3716 wset_new_pixel (w, make_number (0));
3717 wset_start (w, Fmake_marker ());
3718 wset_pointm (w, Fmake_marker ());
3719 wset_vertical_scroll_bar_type (w, Qt);
3720 /* These Lisp fields are marked specially so they're not set to nil by
3721 allocate_window. */
3722 wset_prev_buffers (w, Qnil);
3723 wset_next_buffers (w, Qnil);
3725 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3726 non-Lisp data, so do it only for slots which should not be zero. */
3727 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3728 w->left_fringe_width = w->right_fringe_width = -1;
3729 w->mode_line_height = w->header_line_height = -1;
3730 #ifdef HAVE_WINDOW_SYSTEM
3731 w->phys_cursor_type = NO_CURSOR;
3732 w->phys_cursor_width = -1;
3733 #endif
3734 w->sequence_number = ++sequence_number;
3735 w->scroll_bar_width = -1;
3736 w->column_number_displayed = -1;
3738 /* Reset window_list. */
3739 Vwindow_list = Qnil;
3740 /* Return window. */
3741 XSETWINDOW (window, w);
3742 return window;
3745 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3746 doc: /* Set new pixel size of WINDOW to SIZE.
3747 WINDOW must be a valid window and defaults to the selected one.
3748 Return SIZE.
3750 Optional argument ADD non-nil means add SIZE to the new pixel size of
3751 WINDOW and return the sum.
3753 The new pixel size of WINDOW, if valid, will be shortly installed as
3754 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3755 `window-pixel-width').
3757 Note: This function does not operate on any child windows of WINDOW. */)
3758 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3760 struct window *w = decode_valid_window (window);
3761 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3762 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3764 CHECK_RANGED_INTEGER (size, size_min, size_max);
3765 if (NILP (add))
3766 wset_new_pixel (w, size);
3767 else
3768 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3770 return w->new_pixel;
3773 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3774 doc: /* Set new total size of WINDOW to SIZE.
3775 WINDOW must be a valid window and defaults to the selected one.
3776 Return SIZE.
3778 Optional argument ADD non-nil means add SIZE to the new total size of
3779 WINDOW and return the sum.
3781 The new total size of WINDOW, if valid, will be shortly installed as
3782 WINDOW's total height (see `window-total-height') or total width (see
3783 `window-total-width').
3785 Note: This function does not operate on any child windows of WINDOW. */)
3786 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3788 struct window *w = decode_valid_window (window);
3790 CHECK_NUMBER (size);
3791 if (NILP (add))
3792 wset_new_total (w, size);
3793 else
3794 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3796 return w->new_total;
3799 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3800 doc: /* Set new normal size of WINDOW to SIZE.
3801 WINDOW must be a valid window and defaults to the selected one.
3802 Return SIZE.
3804 The new normal size of WINDOW, if valid, will be shortly installed as
3805 WINDOW's normal size (see `window-normal-size').
3807 Note: This function does not operate on any child windows of WINDOW. */)
3808 (Lisp_Object window, Lisp_Object size)
3810 wset_new_normal (decode_valid_window (window), size);
3811 return size;
3814 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3815 non-zero) to w->new_pixel would result in correct heights (widths)
3816 for window W and recursively all child windows of W.
3818 Note: This function does not check any of `window-fixed-size-p',
3819 `window-min-height' or `window-min-width'. It does check that window
3820 sizes do not drop below one line (two columns). */
3821 static int
3822 window_resize_check (struct window *w, bool horflag)
3824 struct frame *f = XFRAME (w->frame);
3825 struct window *c;
3827 if (WINDOW_VERTICAL_COMBINATION_P (w))
3828 /* W is a vertical combination. */
3830 c = XWINDOW (w->contents);
3831 if (horflag)
3832 /* All child windows of W must have the same width as W. */
3834 while (c)
3836 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3837 || !window_resize_check (c, horflag))
3838 return 0;
3840 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3843 return 1;
3845 else
3846 /* The sum of the heights of the child windows of W must equal
3847 W's height. */
3849 int remaining_pixels = XINT (w->new_pixel);
3851 while (c)
3853 if (!window_resize_check (c, horflag))
3854 return 0;
3856 remaining_pixels -= XINT (c->new_pixel);
3857 if (remaining_pixels < 0)
3858 return 0;
3859 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3862 return remaining_pixels == 0;
3865 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3866 /* W is a horizontal combination. */
3868 c = XWINDOW (w->contents);
3869 if (horflag)
3870 /* The sum of the widths of the child windows of W must equal W's
3871 width. */
3873 int remaining_pixels = XINT (w->new_pixel);
3875 while (c)
3877 if (!window_resize_check (c, horflag))
3878 return 0;
3880 remaining_pixels -= XINT (c->new_pixel);
3881 if (remaining_pixels < 0)
3882 return 0;
3883 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3886 return remaining_pixels == 0;
3888 else
3889 /* All child windows of W must have the same height as W. */
3891 while (c)
3893 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3894 || !window_resize_check (c, horflag))
3895 return 0;
3897 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3900 return 1;
3903 else
3904 /* A leaf window. Make sure it's not too small. The following
3905 hardcodes the values of `window-safe-min-width' (2) and
3906 `window-safe-min-height' (1) which are defined in window.el. */
3907 return (XINT (w->new_pixel) >= (horflag
3908 ? (2 * FRAME_COLUMN_WIDTH (f))
3909 : FRAME_LINE_HEIGHT (f)));
3913 /* Set w->pixel_height (w->pixel_width if HORFLAG is non-zero) to
3914 w->new_pixel for window W and recursively all child windows of W.
3915 Also calculate and assign the new vertical (horizontal) pixel start
3916 positions of each of these windows.
3918 This function does not perform any error checks. Make sure you have
3919 run window_resize_check on W before applying this function. */
3920 static void
3921 window_resize_apply (struct window *w, bool horflag)
3923 struct window *c;
3924 int edge;
3925 int unit = (horflag
3926 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3927 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3929 /* Note: Assigning new_normal requires that the new total size of the
3930 parent window has been set *before*. */
3931 if (horflag)
3933 w->pixel_width = XFASTINT (w->new_pixel);
3934 w->total_cols = w->pixel_width / unit;
3935 if (NUMBERP (w->new_normal))
3936 wset_normal_cols (w, w->new_normal);
3938 edge = w->pixel_left;
3940 else
3942 w->pixel_height = XFASTINT (w->new_pixel);
3943 w->total_lines = w->pixel_height / unit;
3944 if (NUMBERP (w->new_normal))
3945 wset_normal_lines (w, w->new_normal);
3947 edge = w->pixel_top;
3950 if (WINDOW_VERTICAL_COMBINATION_P (w))
3951 /* W is a vertical combination. */
3953 c = XWINDOW (w->contents);
3954 while (c)
3956 if (horflag)
3958 c->pixel_left = edge;
3959 c->left_col = edge / unit;
3961 else
3963 c->pixel_top = edge;
3964 c->top_line = edge / unit;
3966 window_resize_apply (c, horflag);
3967 if (!horflag)
3968 edge = edge + c->pixel_height;
3970 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3973 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3974 /* W is a horizontal combination. */
3976 c = XWINDOW (w->contents);
3977 while (c)
3979 if (horflag)
3981 c->pixel_left = edge;
3982 c->left_col = edge / unit;
3984 else
3986 c->pixel_top = edge;
3987 c->top_line = edge / unit;
3990 window_resize_apply (c, horflag);
3991 if (horflag)
3992 edge = edge + c->pixel_width;
3994 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3997 else
3999 adjust_window_margins (w);
4000 /* Bug#15957. */
4001 w->window_end_valid = 0;
4006 /* Set w->total_lines (w->total_cols if HORFLAG is non-zero) to
4007 w->new_total for window W and recursively all child windows of W.
4008 Also calculate and assign the new vertical (horizontal) start
4009 positions of each of these windows. */
4010 static void
4011 window_resize_apply_total (struct window *w, bool horflag)
4013 struct window *c;
4014 int edge;
4016 /* Note: Assigning new_normal requires that the new total size of the
4017 parent window has been set *before*. */
4018 if (horflag)
4020 w->total_cols = XFASTINT (w->new_total);
4021 edge = w->left_col;
4023 else
4025 w->total_lines = XFASTINT (w->new_total);
4026 edge = w->top_line;
4029 if (WINDOW_VERTICAL_COMBINATION_P (w))
4030 /* W is a vertical combination. */
4032 c = XWINDOW (w->contents);
4033 while (c)
4035 if (horflag)
4036 c->left_col = edge;
4037 else
4038 c->top_line = edge;
4040 window_resize_apply_total (c, horflag);
4041 if (!horflag)
4042 edge = edge + c->total_lines;
4044 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4047 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4048 /* W is a horizontal combination. */
4050 c = XWINDOW (w->contents);
4051 while (c)
4053 if (horflag)
4054 c->left_col = edge;
4055 else
4056 c->top_line = edge;
4058 window_resize_apply_total (c, horflag);
4059 if (horflag)
4060 edge = edge + c->total_cols;
4062 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4067 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4068 doc: /* Apply requested size values for window-tree of FRAME.
4069 If FRAME is omitted or nil, it defaults to the selected frame.
4071 Optional argument HORIZONTAL omitted or nil means apply requested
4072 height values. HORIZONTAL non-nil means apply requested width values.
4074 The requested size values are those set by `set-window-new-pixel' and
4075 `set-window-new-normal'. This function checks whether the requested
4076 values sum up to a valid window layout, recursively assigns the new
4077 sizes of all child windows and calculates and assigns the new start
4078 positions of these windows.
4080 Return t if the requested values have been applied correctly, nil
4081 otherwise.
4083 Note: This function does not check any of `window-fixed-size-p',
4084 `window-min-height' or `window-min-width'. All these checks have to
4085 be applied on the Elisp level. */)
4086 (Lisp_Object frame, Lisp_Object horizontal)
4088 struct frame *f = decode_live_frame (frame);
4089 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4090 bool horflag = !NILP (horizontal);
4092 if (!window_resize_check (r, horflag)
4093 || (XINT (r->new_pixel)
4094 != (horflag ? r->pixel_width : r->pixel_height)))
4095 return Qnil;
4097 block_input ();
4098 window_resize_apply (r, horflag);
4100 fset_redisplay (f);
4101 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4103 adjust_frame_glyphs (f);
4104 unblock_input ();
4106 return Qt;
4110 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4111 doc: /* Apply requested total size values for window-tree of FRAME.
4112 If FRAME is omitted or nil, it defaults to the selected frame.
4114 This function does not assign pixel or normal size values. You should
4115 have run `window-resize-apply' before running this.
4117 Optional argument HORIZONTAL omitted or nil means apply requested
4118 height values. HORIZONTAL non-nil means apply requested width
4119 values. */)
4120 (Lisp_Object frame, Lisp_Object horizontal)
4122 struct frame *f = decode_live_frame (frame);
4123 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4125 block_input ();
4126 /* Necessary when deleting the top-/or leftmost window. */
4127 r->left_col = 0;
4128 r->top_line = FRAME_TOP_MARGIN (f);
4129 window_resize_apply_total (r, !NILP (horizontal));
4130 /* Handle the mini window. */
4131 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4133 struct window *m = XWINDOW (f->minibuffer_window);
4135 if (NILP (horizontal))
4137 m->top_line = r->top_line + r->total_lines;
4138 m->total_lines = XFASTINT (m->new_total);
4140 else
4141 m->total_cols = XFASTINT (m->new_total);
4144 unblock_input ();
4146 return Qt;
4150 /* Resize frame F's windows when number of lines of F is set to SIZE.
4151 HORFLAG 1 means resize windows when number of columns of F is set to
4152 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4154 This function can delete all windows but the selected one in order to
4155 satisfy the request. The result will be meaningful if and only if
4156 F's windows have meaningful sizes when you call this. */
4157 void
4158 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4160 Lisp_Object root = f->root_window;
4161 struct window *r = XWINDOW (root);
4162 Lisp_Object mini = f->minibuffer_window;
4163 struct window *m;
4164 /* old_size is the old size of the frame's root window. */
4165 int old_size = horflag ? r->total_cols : r->total_lines;
4166 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4167 /* new_size is the new size of the frame's root window. */
4168 int new_size, new_pixel_size;
4169 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4171 /* Don't let the size drop below one unit. This is more comforting
4172 when we are called from x_set_tool_bar_lines since the latter may
4173 have implicitly given us a zero or negative height. */
4174 if (pixelwise)
4176 /* Note: This does not include the size for internal borders
4177 since these are not part of the frame's text area. */
4178 new_pixel_size = max (horflag
4179 ? size
4180 : (size
4181 - FRAME_TOP_MARGIN_HEIGHT (f)
4182 - ((FRAME_HAS_MINIBUF_P (f)
4183 && !FRAME_MINIBUF_ONLY_P (f))
4184 ? FRAME_LINE_HEIGHT (f) : 0)),
4185 unit);
4186 new_size = new_pixel_size / unit;
4188 else
4190 new_size = max (horflag
4191 ? size
4192 : (size
4193 - FRAME_TOP_MARGIN (f)
4194 - ((FRAME_HAS_MINIBUF_P (f)
4195 && !FRAME_MINIBUF_ONLY_P (f))
4196 ? 1 : 0)),
4198 new_pixel_size = new_size * unit;
4201 r->top_line = FRAME_TOP_MARGIN (f);
4202 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4204 if (new_pixel_size == old_pixel_size)
4206 else if (WINDOW_LEAF_P (r))
4207 /* For a leaf root window just set the size. */
4208 if (horflag)
4210 r->total_cols = new_size;
4211 r->pixel_width = new_pixel_size;
4213 else
4215 r->total_lines = new_size;
4216 r->pixel_height = new_pixel_size;
4218 else
4220 Lisp_Object delta;
4222 if (pixelwise)
4223 XSETINT (delta, new_pixel_size - old_pixel_size);
4224 else
4225 XSETINT (delta, new_size - old_size);
4227 /* Try a "normal" resize first. */
4228 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4229 pixelwise ? Qt : Qnil);
4230 if (window_resize_check (r, horflag)
4231 && new_pixel_size == XINT (r->new_pixel))
4233 window_resize_apply (r, horflag);
4234 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4236 else
4238 /* Try with "reasonable" minimum sizes next. */
4239 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4240 pixelwise ? Qt : Qnil);
4241 if (window_resize_check (r, horflag)
4242 && new_pixel_size == XINT (r->new_pixel))
4244 window_resize_apply (r, horflag);
4245 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4247 else
4249 /* Finally, try with "safe" minimum sizes. */
4250 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4251 pixelwise ? Qt : Qnil);
4252 if (window_resize_check (r, horflag)
4253 && new_pixel_size == XINT (r->new_pixel))
4255 window_resize_apply (r, horflag);
4256 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4258 #if 0 /* Let's try without killing other windows. */
4259 else
4261 /* We lost. Delete all windows but the frame's
4262 selected one. */
4263 root = f->selected_window;
4264 Fdelete_other_windows_internal (root, Qnil);
4265 if (horflag)
4267 XWINDOW (root)->total_cols = new_size;
4268 XWINDOW (root)->pixel_width = new_pixel_size;
4270 else
4272 XWINDOW (root)->total_lines = new_size;
4273 XWINDOW (root)->pixel_height = new_pixel_size;
4276 #endif /* 0 */
4281 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4283 m = XWINDOW (mini);
4284 if (horflag)
4286 m->total_cols = size;
4287 m->pixel_width = new_pixel_size;
4289 else
4291 /* Are we sure we always want 1 line here? */
4292 m->total_lines = 1;
4293 m->pixel_height = FRAME_LINE_HEIGHT (f);
4294 m->top_line = r->top_line + r->total_lines;
4295 m->pixel_top = r->pixel_top + r->pixel_height;
4299 fset_redisplay (f);
4303 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4304 doc: /* Split window OLD.
4305 Second argument PIXEL-SIZE specifies the number of pixels of the
4306 new window. In any case TOTAL-SIZE must be a positive integer.
4308 Third argument SIDE nil (or `below') specifies that the new window shall
4309 be located below WINDOW. SIDE `above' means the new window shall be
4310 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4311 height of the new window including space reserved for the mode and/or
4312 header line.
4314 SIDE t (or `right') specifies that the new window shall be located on
4315 the right side of WINDOW. SIDE `left' means the new window shall be
4316 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4317 width of the new window including space reserved for fringes and the
4318 scrollbar or a divider column.
4320 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4321 according to the SIDE argument.
4323 The new pixel and normal sizes of all involved windows must have been
4324 set correctly. See the code of `split-window' for how this is done. */)
4325 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4327 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4328 parent window or an internal window we have to install as OLD's new
4329 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4330 provided OLD is a leaf window, or to the frame's selected window.
4331 NEW (*n) is the new window created with some parameters taken from
4332 REFERENCE (*r). */
4333 register Lisp_Object new, frame, reference;
4334 register struct window *o, *p, *n, *r, *c;
4335 struct frame *f;
4336 bool horflag
4337 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4338 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4339 int combination_limit = 0, sum = 0;
4340 int total_size;
4342 CHECK_WINDOW (old);
4343 o = XWINDOW (old);
4344 frame = WINDOW_FRAME (o);
4345 f = XFRAME (frame);
4347 CHECK_NUMBER (pixel_size);
4348 total_size
4349 = XINT (pixel_size) / (horflag
4350 ? FRAME_COLUMN_WIDTH (f)
4351 : FRAME_LINE_HEIGHT (f));
4353 /* Set combination_limit to 1 if we have to make a new parent window.
4354 We do that if either `window-combination-limit' is t, or OLD has no
4355 parent, or OLD is ortho-combined. */
4356 combination_limit =
4357 EQ (Vwindow_combination_limit, Qt)
4358 || NILP (o->parent)
4359 || (horflag
4360 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4361 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4363 /* We need a live reference window to initialize some parameters. */
4364 if (WINDOW_LIVE_P (old))
4365 /* OLD is live, use it as reference window. */
4366 reference = old;
4367 else
4368 /* Use the frame's selected window as reference window. */
4369 reference = FRAME_SELECTED_WINDOW (f);
4370 r = XWINDOW (reference);
4372 /* The following bugs are caught by `split-window'. */
4373 if (MINI_WINDOW_P (o))
4374 error ("Attempt to split minibuffer window");
4375 else if (total_size < (horflag ? 2 : 1))
4376 error ("Size of new window too small (after split)");
4377 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4378 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4379 proportionally. */
4381 p = XWINDOW (o->parent);
4382 /* Temporarily pretend we split the parent window. */
4383 wset_new_pixel
4384 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4385 - XINT (pixel_size)));
4386 if (!window_resize_check (p, horflag))
4387 error ("Window sizes don't fit");
4388 else
4389 /* Undo the temporary pretension. */
4390 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4392 else
4394 if (!window_resize_check (o, horflag))
4395 error ("Resizing old window failed");
4396 else if (XINT (pixel_size) + XINT (o->new_pixel)
4397 != (horflag ? o->pixel_width : o->pixel_height))
4398 error ("Sum of sizes of old and new window don't fit");
4401 /* This is our point of no return. */
4402 if (combination_limit)
4404 /* Save the old value of o->normal_cols/lines. It gets corrupted
4405 by make_parent_window and we need it below for assigning it to
4406 p->new_normal. */
4407 Lisp_Object new_normal
4408 = horflag ? o->normal_cols : o->normal_lines;
4410 make_parent_window (old, horflag);
4411 p = XWINDOW (o->parent);
4412 if (EQ (Vwindow_combination_limit, Qt))
4413 /* Store t in the new parent's combination_limit slot to avoid
4414 that its children get merged into another window. */
4415 wset_combination_limit (p, Qt);
4416 /* These get applied below. */
4417 wset_new_pixel
4418 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4419 wset_new_total
4420 (p, make_number (horflag ? o->total_cols : o->total_lines));
4421 wset_new_normal (p, new_normal);
4423 else
4424 p = XWINDOW (o->parent);
4426 fset_redisplay (f);
4427 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4428 new = make_window ();
4429 n = XWINDOW (new);
4430 wset_frame (n, frame);
4431 wset_parent (n, o->parent);
4433 if (EQ (side, Qabove) || EQ (side, Qleft))
4435 wset_prev (n, o->prev);
4436 if (NILP (n->prev))
4437 wset_combination (p, horflag, new);
4438 else
4439 wset_next (XWINDOW (n->prev), new);
4440 wset_next (n, old);
4441 wset_prev (o, new);
4443 else
4445 wset_next (n, o->next);
4446 if (!NILP (n->next))
4447 wset_prev (XWINDOW (n->next), new);
4448 wset_prev (n, old);
4449 wset_next (o, new);
4452 n->window_end_valid = 0;
4453 n->last_cursor_vpos = 0;
4455 /* Get special geometry settings from reference window. */
4456 n->left_margin_cols = r->left_margin_cols;
4457 n->right_margin_cols = r->right_margin_cols;
4458 n->left_fringe_width = r->left_fringe_width;
4459 n->right_fringe_width = r->right_fringe_width;
4460 n->fringes_outside_margins = r->fringes_outside_margins;
4461 n->scroll_bar_width = r->scroll_bar_width;
4462 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4464 /* Directly assign orthogonal coordinates and sizes. */
4465 if (horflag)
4467 n->pixel_top = o->pixel_top;
4468 n->top_line = o->top_line;
4469 n->pixel_height = o->pixel_height;
4470 n->total_lines = o->total_lines;
4472 else
4474 n->pixel_left = o->pixel_left;
4475 n->left_col = o->left_col;
4476 n->pixel_width = o->pixel_width;
4477 n->total_cols = o->total_cols;
4480 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4481 get them ready here. */
4482 wset_new_pixel (n, pixel_size);
4483 c = XWINDOW (p->contents);
4484 while (c)
4486 if (c != n)
4487 sum = sum + XINT (c->new_total);
4488 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4490 wset_new_total (n, make_number ((horflag
4491 ? p->total_cols
4492 : p->total_lines)
4493 - sum));
4494 wset_new_normal (n, normal_size);
4496 block_input ();
4497 window_resize_apply (p, horflag);
4498 adjust_frame_glyphs (f);
4499 /* Set buffer of NEW to buffer of reference window. Don't run
4500 any hooks. */
4501 set_window_buffer (new, r->contents, 0, 1);
4502 unblock_input ();
4504 /* Maybe we should run the scroll functions in Elisp (which already
4505 runs the configuration change hook). */
4506 if (! NILP (Vwindow_scroll_functions))
4507 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4508 Fmarker_position (n->start));
4509 /* Return NEW. */
4510 return new;
4514 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4515 doc: /* Remove WINDOW from its frame.
4516 WINDOW defaults to the selected window. Return nil.
4517 Signal an error when WINDOW is the only window on its frame. */)
4518 (register Lisp_Object window)
4520 register Lisp_Object parent, sibling, frame, root;
4521 struct window *w, *p, *s, *r;
4522 struct frame *f;
4523 bool horflag, before_sibling = 0;
4525 w = decode_any_window (window);
4526 XSETWINDOW (window, w);
4527 if (NILP (w->contents))
4528 /* It's a no-op to delete an already deleted window. */
4529 return Qnil;
4531 parent = w->parent;
4532 if (NILP (parent))
4533 /* Never delete a minibuffer or frame root window. */
4534 error ("Attempt to delete minibuffer or sole ordinary window");
4535 else if (NILP (w->prev) && NILP (w->next))
4536 /* Rather bow out here, this case should be handled on the Elisp
4537 level. */
4538 error ("Attempt to delete sole window of parent");
4540 p = XWINDOW (parent);
4541 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4543 frame = WINDOW_FRAME (w);
4544 f = XFRAME (frame);
4546 root = FRAME_ROOT_WINDOW (f);
4547 r = XWINDOW (root);
4549 /* Unlink WINDOW from window tree. */
4550 if (NILP (w->prev))
4551 /* Get SIBLING below (on the right of) WINDOW. */
4553 /* before_sibling 1 means WINDOW is the first child of its
4554 parent and thus before the sibling. */
4555 before_sibling = 1;
4556 sibling = w->next;
4557 s = XWINDOW (sibling);
4558 wset_prev (s, Qnil);
4559 wset_combination (p, horflag, sibling);
4561 else
4562 /* Get SIBLING above (on the left of) WINDOW. */
4564 sibling = w->prev;
4565 s = XWINDOW (sibling);
4566 wset_next (s, w->next);
4567 if (!NILP (s->next))
4568 wset_prev (XWINDOW (s->next), sibling);
4571 if (window_resize_check (r, horflag)
4572 && (XINT (r->new_pixel)
4573 == (horflag ? r->pixel_width : r->pixel_height)))
4574 /* We can delete WINDOW now. */
4577 /* Block input. */
4578 block_input ();
4579 window_resize_apply (p, horflag);
4580 /* If this window is referred to by the dpyinfo's mouse
4581 highlight, invalidate that slot to be safe (Bug#9904). */
4582 if (!FRAME_INITIAL_P (f))
4584 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4586 if (EQ (hlinfo->mouse_face_window, window))
4587 hlinfo->mouse_face_window = Qnil;
4590 fset_redisplay (f);
4591 Vwindow_list = Qnil;
4592 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4594 wset_next (w, Qnil); /* Don't delete w->next too. */
4595 free_window_matrices (w);
4597 if (WINDOWP (w->contents))
4599 delete_all_child_windows (w->contents);
4600 wset_combination (w, 0, Qnil);
4602 else
4604 unshow_buffer (w);
4605 unchain_marker (XMARKER (w->pointm));
4606 unchain_marker (XMARKER (w->start));
4607 wset_buffer (w, Qnil);
4610 if (NILP (s->prev) && NILP (s->next))
4611 /* A matrjoshka where SIBLING has become the only child of
4612 PARENT. */
4614 /* Put SIBLING into PARENT's place. */
4615 replace_window (parent, sibling, 0);
4616 /* Have SIBLING inherit the following three slot values from
4617 PARENT (the combination_limit slot is not inherited). */
4618 wset_normal_cols (s, p->normal_cols);
4619 wset_normal_lines (s, p->normal_lines);
4620 /* Mark PARENT as deleted. */
4621 wset_combination (p, 0, Qnil);
4622 /* Try to merge SIBLING into its new parent. */
4623 recombine_windows (sibling);
4626 adjust_frame_glyphs (f);
4628 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4629 /* We deleted the frame's selected window. */
4631 /* Use the frame's first window as fallback ... */
4632 Lisp_Object new_selected_window = Fframe_first_window (frame);
4633 /* ... but preferably use its most recently used window. */
4634 Lisp_Object mru_window;
4636 /* `get-mru-window' might fail for some reason so play it safe
4637 - promote the first window _without recording it_ first. */
4638 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4639 Fselect_window (new_selected_window, Qt);
4640 else
4641 fset_selected_window (f, new_selected_window);
4643 unblock_input ();
4645 /* Now look whether `get-mru-window' gets us something. */
4646 mru_window = call1 (Qget_mru_window, frame);
4647 if (WINDOW_LIVE_P (mru_window)
4648 && EQ (XWINDOW (mru_window)->frame, frame))
4649 new_selected_window = mru_window;
4651 /* If all ended up well, we now promote the mru window. */
4652 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4653 Fselect_window (new_selected_window, Qnil);
4654 else
4655 fset_selected_window (f, new_selected_window);
4657 else
4658 unblock_input ();
4660 /* Must be run by the caller:
4661 run_window_configuration_change_hook (f); */
4663 else
4664 /* We failed: Relink WINDOW into window tree. */
4666 if (before_sibling)
4668 wset_prev (s, window);
4669 wset_combination (p, horflag, window);
4671 else
4673 wset_next (s, window);
4674 if (!NILP (w->next))
4675 wset_prev (XWINDOW (w->next), window);
4677 error ("Deletion failed");
4680 return Qnil;
4683 /***********************************************************************
4684 Resizing Mini-Windows
4685 ***********************************************************************/
4687 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4688 can. */
4689 void
4690 grow_mini_window (struct window *w, int delta, bool pixelwise)
4692 struct frame *f = XFRAME (w->frame);
4693 struct window *r;
4694 Lisp_Object root, height;
4695 int line_height, pixel_height;
4697 eassert (MINI_WINDOW_P (w));
4698 eassert (delta >= 0);
4700 if (delta > 0)
4702 root = FRAME_ROOT_WINDOW (f);
4703 r = XWINDOW (root);
4704 height = call3 (Qwindow_resize_root_window_vertically,
4705 root, make_number (- delta), pixelwise ? Qt : Qnil);
4706 if (INTEGERP (height) && window_resize_check (r, 0))
4708 block_input ();
4709 window_resize_apply (r, 0);
4711 if (pixelwise)
4713 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4714 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4716 else
4718 line_height = min (-XINT (height),
4719 ((INT_MAX - w->pixel_height)
4720 / FRAME_LINE_HEIGHT (f)));
4721 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4724 /* Grow the mini-window. */
4725 w->pixel_top = r->pixel_top + r->pixel_height;
4726 w->top_line = r->top_line + r->total_lines;
4727 /* Make sure the mini-window has always at least one line. */
4728 w->pixel_height = max (w->pixel_height + pixel_height,
4729 FRAME_LINE_HEIGHT (f));
4730 w->total_lines = max (w->total_lines + line_height, 1);
4732 /* Enforce full redisplay of the frame. */
4733 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4734 fset_redisplay (f);
4735 adjust_frame_glyphs (f);
4736 unblock_input ();
4741 /* Shrink mini-window W to one line. */
4742 void
4743 shrink_mini_window (struct window *w, bool pixelwise)
4745 struct frame *f = XFRAME (w->frame);
4746 struct window *r;
4747 Lisp_Object root, delta;
4748 EMACS_INT height, unit;
4750 eassert (MINI_WINDOW_P (w));
4752 height = pixelwise ? w->pixel_height : w->total_lines;
4753 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4754 if (height > unit)
4756 root = FRAME_ROOT_WINDOW (f);
4757 r = XWINDOW (root);
4758 delta = call3 (Qwindow_resize_root_window_vertically,
4759 root, make_number (height - unit),
4760 pixelwise ? Qt : Qnil);
4761 if (INTEGERP (delta) && window_resize_check (r, 0))
4763 block_input ();
4764 window_resize_apply (r, 0);
4766 /* Shrink the mini-window. */
4767 w->top_line = r->top_line + r->total_lines;
4768 w->total_lines = 1;
4769 w->pixel_top = r->pixel_top + r->pixel_height;
4770 w->pixel_height = FRAME_LINE_HEIGHT (f);
4771 /* Enforce full redisplay of the frame. */
4772 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4773 fset_redisplay (f);
4774 adjust_frame_glyphs (f);
4775 unblock_input ();
4777 /* If the above failed for whatever strange reason we must make a
4778 one window frame here. The same routine will be needed when
4779 shrinking the frame (and probably when making the initial
4780 *scratch* window). For the moment leave things as they are. */
4784 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4785 doc: /* Resize minibuffer window WINDOW. */)
4786 (Lisp_Object window)
4788 struct window *w = XWINDOW (window);
4789 struct window *r;
4790 struct frame *f;
4791 int height;
4793 CHECK_WINDOW (window);
4794 f = XFRAME (w->frame);
4796 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4797 error ("Not a valid minibuffer window");
4798 else if (FRAME_MINIBUF_ONLY_P (f))
4799 error ("Cannot resize a minibuffer-only frame");
4801 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4802 height = r->pixel_height + w->pixel_height;
4803 if (window_resize_check (r, 0)
4804 && XINT (w->new_pixel) > 0
4805 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4807 block_input ();
4808 window_resize_apply (r, 0);
4810 w->pixel_height = XFASTINT (w->new_pixel);
4811 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4812 w->pixel_top = r->pixel_top + r->pixel_height;
4813 w->top_line = r->top_line + r->total_lines;
4815 fset_redisplay (f);
4816 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4817 adjust_frame_glyphs (f);
4818 unblock_input ();
4819 return Qt;
4821 else
4822 error ("Failed to resize minibuffer window");
4825 /* Mark window cursors off for all windows in the window tree rooted
4826 at W by setting their phys_cursor_on_p flag to zero. Called from
4827 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4828 the frame are cleared. */
4830 void
4831 mark_window_cursors_off (struct window *w)
4833 while (w)
4835 if (WINDOWP (w->contents))
4836 mark_window_cursors_off (XWINDOW (w->contents));
4837 else
4838 w->phys_cursor_on_p = 0;
4840 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4845 /* Return number of lines of text (not counting mode lines) in W. */
4848 window_internal_height (struct window *w)
4850 int ht = w->total_lines;
4852 if (!MINI_WINDOW_P (w))
4854 if (!NILP (w->parent)
4855 || WINDOWP (w->contents)
4856 || !NILP (w->next)
4857 || !NILP (w->prev)
4858 || WINDOW_WANTS_MODELINE_P (w))
4859 --ht;
4861 if (WINDOW_WANTS_HEADER_LINE_P (w))
4862 --ht;
4865 return ht;
4868 /************************************************************************
4869 Window Scrolling
4870 ***********************************************************************/
4872 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4873 N screen-fulls, which is defined as the height of the window minus
4874 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4875 instead. Negative values of N mean scroll down. NOERROR non-zero
4876 means don't signal an error if we try to move over BEGV or ZV,
4877 respectively. */
4879 static void
4880 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4882 immediate_quit = 1;
4883 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4885 wset_redisplay (XWINDOW (window));
4887 /* If we must, use the pixel-based version which is much slower than
4888 the line-based one but can handle varying line heights. */
4889 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4890 window_scroll_pixel_based (window, n, whole, noerror);
4891 else
4892 window_scroll_line_based (window, n, whole, noerror);
4894 /* Bug#15957. */
4895 XWINDOW (window)->window_end_valid = 0;
4896 immediate_quit = 0;
4900 /* Implementation of window_scroll that works based on pixel line
4901 heights. See the comment of window_scroll for parameter
4902 descriptions. */
4904 static void
4905 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4907 struct it it;
4908 struct window *w = XWINDOW (window);
4909 struct text_pos start;
4910 int this_scroll_margin;
4911 /* True if we fiddled the window vscroll field without really scrolling. */
4912 int vscrolled = 0;
4913 int x, y, rtop, rbot, rowh, vpos;
4914 void *itdata = NULL;
4915 int window_total_lines;
4916 int frame_line_height = default_line_pixel_height (w);
4918 SET_TEXT_POS_FROM_MARKER (start, w->start);
4919 /* Scrolling a minibuffer window via scroll bar when the echo area
4920 shows long text sometimes resets the minibuffer contents behind
4921 our backs. */
4922 if (CHARPOS (start) > ZV)
4923 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4925 /* If PT is not visible in WINDOW, move back one half of
4926 the screen. Allow PT to be partially visible, otherwise
4927 something like (scroll-down 1) with PT in the line before
4928 the partially visible one would recenter. */
4930 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4932 itdata = bidi_shelve_cache ();
4933 /* Move backward half the height of the window. Performance note:
4934 vmotion used here is about 10% faster, but would give wrong
4935 results for variable height lines. */
4936 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4937 it.current_y = it.last_visible_y;
4938 move_it_vertically_backward (&it, window_box_height (w) / 2);
4940 /* The function move_iterator_vertically may move over more than
4941 the specified y-distance. If it->w is small, e.g. a
4942 mini-buffer window, we may end up in front of the window's
4943 display area. Start displaying at the start of the line
4944 containing PT in this case. */
4945 if (it.current_y <= 0)
4947 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4948 move_it_vertically_backward (&it, 0);
4949 it.current_y = 0;
4952 start = it.current.pos;
4953 bidi_unshelve_cache (itdata, 0);
4955 else if (auto_window_vscroll_p)
4957 if (rtop || rbot) /* partially visible */
4959 int px;
4960 int dy = frame_line_height;
4961 /* In the below we divide the window box height by the
4962 frame's line height to make the result predictable when
4963 the window box is not an integral multiple of the line
4964 height. This is important to ensure we get back to the
4965 same position when scrolling up, then down. */
4966 if (whole)
4967 dy = max ((window_box_height (w) / dy
4968 - next_screen_context_lines) * dy,
4969 dy);
4970 dy *= n;
4972 if (n < 0)
4974 /* Only vscroll backwards if already vscrolled forwards. */
4975 if (w->vscroll < 0 && rtop > 0)
4977 px = max (0, -w->vscroll - min (rtop, -dy));
4978 Fset_window_vscroll (window, make_number (px), Qt);
4979 return;
4982 if (n > 0)
4984 /* Do vscroll if already vscrolled or only display line. */
4985 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4987 px = max (0, -w->vscroll + min (rbot, dy));
4988 Fset_window_vscroll (window, make_number (px), Qt);
4989 return;
4992 /* Maybe modify window start instead of scrolling. */
4993 if (rbot > 0 || w->vscroll < 0)
4995 ptrdiff_t spos;
4997 Fset_window_vscroll (window, make_number (0), Qt);
4998 /* If there are other text lines above the current row,
4999 move window start to current row. Else to next row. */
5000 if (rbot > 0)
5001 spos = XINT (Fline_beginning_position (Qnil));
5002 else
5003 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5004 set_marker_restricted (w->start, make_number (spos),
5005 w->contents);
5006 w->start_at_line_beg = 1;
5007 w->update_mode_line = 1;
5008 /* Set force_start so that redisplay_window will run the
5009 window-scroll-functions. */
5010 w->force_start = 1;
5011 return;
5015 /* Cancel previous vscroll. */
5016 Fset_window_vscroll (window, make_number (0), Qt);
5019 itdata = bidi_shelve_cache ();
5020 /* If scroll_preserve_screen_position is non-nil, we try to set
5021 point in the same window line as it is now, so get that line. */
5022 if (!NILP (Vscroll_preserve_screen_position))
5024 /* We preserve the goal pixel coordinate across consecutive
5025 calls to scroll-up, scroll-down and other commands that
5026 have the `scroll-command' property. This avoids the
5027 possibility of point becoming "stuck" on a tall line when
5028 scrolling by one line. */
5029 if (window_scroll_pixel_based_preserve_y < 0
5030 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5031 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5033 start_display (&it, w, start);
5034 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5035 window_scroll_pixel_based_preserve_y = it.current_y;
5036 window_scroll_pixel_based_preserve_x = it.current_x;
5039 else
5040 window_scroll_pixel_based_preserve_y
5041 = window_scroll_pixel_based_preserve_x = -1;
5043 /* Move iterator it from start the specified distance forward or
5044 backward. The result is the new window start. */
5045 start_display (&it, w, start);
5046 if (whole)
5048 ptrdiff_t start_pos = IT_CHARPOS (it);
5049 int dy = frame_line_height;
5050 /* In the below we divide the window box height by the frame's
5051 line height to make the result predictable when the window
5052 box is not an integral multiple of the line height. This is
5053 important to ensure we get back to the same position when
5054 scrolling up, then down. */
5055 dy = max ((window_box_height (w) / dy - next_screen_context_lines) * dy,
5056 dy) * n;
5058 /* Note that move_it_vertically always moves the iterator to the
5059 start of a line. So, if the last line doesn't have a newline,
5060 we would end up at the start of the line ending at ZV. */
5061 if (dy <= 0)
5063 move_it_vertically_backward (&it, -dy);
5064 /* Ensure we actually do move, e.g. in case we are currently
5065 looking at an image that is taller that the window height. */
5066 while (start_pos == IT_CHARPOS (it)
5067 && start_pos > BEGV)
5068 move_it_by_lines (&it, -1);
5070 else if (dy > 0)
5072 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5073 MOVE_TO_POS | MOVE_TO_Y);
5074 /* Ensure we actually do move, e.g. in case we are currently
5075 looking at an image that is taller that the window height. */
5076 while (start_pos == IT_CHARPOS (it)
5077 && start_pos < ZV)
5078 move_it_by_lines (&it, 1);
5081 else
5082 move_it_by_lines (&it, n);
5084 /* We failed if we find ZV is already on the screen (scrolling up,
5085 means there's nothing past the end), or if we can't start any
5086 earlier (scrolling down, means there's nothing past the top). */
5087 if ((n > 0 && IT_CHARPOS (it) == ZV)
5088 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5090 if (IT_CHARPOS (it) == ZV)
5092 if (it.current_y < it.last_visible_y
5093 && (it.current_y + it.max_ascent + it.max_descent
5094 > it.last_visible_y))
5096 /* The last line was only partially visible, make it fully
5097 visible. */
5098 w->vscroll = (it.last_visible_y
5099 - it.current_y + it.max_ascent + it.max_descent);
5100 adjust_frame_glyphs (it.f);
5102 else
5104 bidi_unshelve_cache (itdata, 0);
5105 if (noerror)
5106 return;
5107 else if (n < 0) /* could happen with empty buffers */
5108 xsignal0 (Qbeginning_of_buffer);
5109 else
5110 xsignal0 (Qend_of_buffer);
5113 else
5115 if (w->vscroll != 0)
5116 /* The first line was only partially visible, make it fully
5117 visible. */
5118 w->vscroll = 0;
5119 else
5121 bidi_unshelve_cache (itdata, 0);
5122 if (noerror)
5123 return;
5124 else
5125 xsignal0 (Qbeginning_of_buffer);
5129 /* If control gets here, then we vscrolled. */
5131 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5133 /* Don't try to change the window start below. */
5134 vscrolled = 1;
5137 if (! vscrolled)
5139 ptrdiff_t pos = IT_CHARPOS (it);
5140 ptrdiff_t bytepos;
5142 /* If in the middle of a multi-glyph character move forward to
5143 the next character. */
5144 if (in_display_vector_p (&it))
5146 ++pos;
5147 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5150 /* Set the window start, and set up the window for redisplay. */
5151 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5152 IT_BYTEPOS (it));
5153 bytepos = marker_byte_position (w->start);
5154 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5155 w->update_mode_line = 1;
5156 /* Set force_start so that redisplay_window will run the
5157 window-scroll-functions. */
5158 w->force_start = 1;
5161 /* The rest of this function uses current_y in a nonstandard way,
5162 not including the height of the header line if any. */
5163 it.current_y = it.vpos = 0;
5165 /* Move PT out of scroll margins.
5166 This code wants current_y to be zero at the window start position
5167 even if there is a header line. */
5168 window_total_lines
5169 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5170 this_scroll_margin = max (0, scroll_margin);
5171 this_scroll_margin
5172 = min (this_scroll_margin, window_total_lines / 4);
5173 this_scroll_margin *= frame_line_height;
5175 if (n > 0)
5177 /* We moved the window start towards ZV, so PT may be now
5178 in the scroll margin at the top. */
5179 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5180 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5181 && (NILP (Vscroll_preserve_screen_position)
5182 || EQ (Vscroll_preserve_screen_position, Qt)))
5183 /* We found PT at a legitimate height. Leave it alone. */
5185 else if (window_scroll_pixel_based_preserve_y >= 0)
5187 /* If we have a header line, take account of it.
5188 This is necessary because we set it.current_y to 0, above. */
5189 move_it_to (&it, -1,
5190 window_scroll_pixel_based_preserve_x,
5191 window_scroll_pixel_based_preserve_y
5192 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5193 -1, MOVE_TO_Y | MOVE_TO_X);
5194 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5196 else
5198 while (it.current_y < this_scroll_margin)
5200 int prev = it.current_y;
5201 move_it_by_lines (&it, 1);
5202 if (prev == it.current_y)
5203 break;
5205 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5208 else if (n < 0)
5210 ptrdiff_t charpos, bytepos;
5211 int partial_p;
5213 /* Save our position, for the
5214 window_scroll_pixel_based_preserve_y case. */
5215 charpos = IT_CHARPOS (it);
5216 bytepos = IT_BYTEPOS (it);
5218 /* We moved the window start towards BEGV, so PT may be now
5219 in the scroll margin at the bottom. */
5220 move_it_to (&it, PT, -1,
5221 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5222 - this_scroll_margin - 1),
5224 MOVE_TO_POS | MOVE_TO_Y);
5226 /* Save our position, in case it's correct. */
5227 charpos = IT_CHARPOS (it);
5228 bytepos = IT_BYTEPOS (it);
5230 /* If PT is in the screen line at the last fully visible line,
5231 move_it_to will stop at X = 0 in that line, because the
5232 required Y coordinate is reached there. See if we can get to
5233 PT without descending lower in Y, and if we can, it means we
5234 reached PT before the scroll margin. */
5235 if (charpos != PT)
5237 struct it it2;
5238 void *it_data;
5240 it2 = it;
5241 it_data = bidi_shelve_cache ();
5242 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5243 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5245 charpos = IT_CHARPOS (it);
5246 bytepos = IT_BYTEPOS (it);
5247 bidi_unshelve_cache (it_data, 1);
5249 else
5251 it = it2;
5252 bidi_unshelve_cache (it_data, 0);
5256 /* See if point is on a partially visible line at the end. */
5257 if (it.what == IT_EOB)
5258 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5259 else
5261 move_it_by_lines (&it, 1);
5262 partial_p = it.current_y > it.last_visible_y;
5265 if (charpos == PT && !partial_p
5266 && (NILP (Vscroll_preserve_screen_position)
5267 || EQ (Vscroll_preserve_screen_position, Qt)))
5268 /* We found PT before we found the display margin, so PT is ok. */
5270 else if (window_scroll_pixel_based_preserve_y >= 0)
5272 SET_TEXT_POS_FROM_MARKER (start, w->start);
5273 start_display (&it, w, start);
5274 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5275 here because we called start_display again and did not
5276 alter it.current_y this time. */
5277 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5278 window_scroll_pixel_based_preserve_y, -1,
5279 MOVE_TO_Y | MOVE_TO_X);
5280 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5282 else
5284 if (partial_p)
5285 /* The last line was only partially visible, so back up two
5286 lines to make sure we're on a fully visible line. */
5288 move_it_by_lines (&it, -2);
5289 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5291 else
5292 /* No, the position we saved is OK, so use it. */
5293 SET_PT_BOTH (charpos, bytepos);
5296 bidi_unshelve_cache (itdata, 0);
5300 /* Implementation of window_scroll that works based on screen lines.
5301 See the comment of window_scroll for parameter descriptions. */
5303 static void
5304 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5306 register struct window *w = XWINDOW (window);
5307 /* Fvertical_motion enters redisplay, which can trigger
5308 fontification, which in turn can modify buffer text (e.g., if the
5309 fontification functions replace escape sequences with faces, as
5310 in `grep-mode-font-lock-keywords'). So we use a marker to record
5311 the old point position, to prevent crashes in SET_PT_BOTH. */
5312 Lisp_Object opoint_marker = Fpoint_marker ();
5313 register ptrdiff_t pos, pos_byte;
5314 register int ht = window_internal_height (w);
5315 register Lisp_Object tem;
5316 bool lose;
5317 Lisp_Object bolp;
5318 ptrdiff_t startpos = marker_position (w->start);
5319 ptrdiff_t startbyte = marker_byte_position (w->start);
5320 Lisp_Object original_pos = Qnil;
5322 /* If scrolling screen-fulls, compute the number of lines to
5323 scroll from the window's height. */
5324 if (whole)
5325 n *= max (1, ht - next_screen_context_lines);
5327 if (!NILP (Vscroll_preserve_screen_position))
5329 if (window_scroll_preserve_vpos <= 0
5330 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5331 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5333 struct position posit
5334 = *compute_motion (startpos, startbyte, 0, 0, 0,
5335 PT, ht, 0, -1, w->hscroll, 0, w);
5336 window_scroll_preserve_vpos = posit.vpos;
5337 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5340 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5341 make_number (window_scroll_preserve_vpos));
5344 XSETFASTINT (tem, PT);
5345 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5347 if (NILP (tem))
5349 Fvertical_motion (make_number (- (ht / 2)), window);
5350 startpos = PT;
5351 startbyte = PT_BYTE;
5354 SET_PT_BOTH (startpos, startbyte);
5355 lose = n < 0 && PT == BEGV;
5356 Fvertical_motion (make_number (n), window);
5357 pos = PT;
5358 pos_byte = PT_BYTE;
5359 bolp = Fbolp ();
5360 SET_PT_BOTH (marker_position (opoint_marker),
5361 marker_byte_position (opoint_marker));
5363 if (lose)
5365 if (noerror)
5366 return;
5367 else
5368 xsignal0 (Qbeginning_of_buffer);
5371 if (pos < ZV)
5373 /* Don't use a scroll margin that is negative or too large. */
5374 int this_scroll_margin =
5375 max (0, min (scroll_margin, w->total_lines / 4));
5377 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5378 w->start_at_line_beg = !NILP (bolp);
5379 w->update_mode_line = 1;
5380 /* Set force_start so that redisplay_window will run
5381 the window-scroll-functions. */
5382 w->force_start = 1;
5384 if (!NILP (Vscroll_preserve_screen_position)
5385 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5387 SET_PT_BOTH (pos, pos_byte);
5388 Fvertical_motion (original_pos, window);
5390 /* If we scrolled forward, put point enough lines down
5391 that it is outside the scroll margin. */
5392 else if (n > 0)
5394 int top_margin;
5396 if (this_scroll_margin > 0)
5398 SET_PT_BOTH (pos, pos_byte);
5399 Fvertical_motion (make_number (this_scroll_margin), window);
5400 top_margin = PT;
5402 else
5403 top_margin = pos;
5405 if (top_margin <= marker_position (opoint_marker))
5406 SET_PT_BOTH (marker_position (opoint_marker),
5407 marker_byte_position (opoint_marker));
5408 else if (!NILP (Vscroll_preserve_screen_position))
5410 SET_PT_BOTH (pos, pos_byte);
5411 Fvertical_motion (original_pos, window);
5413 else
5414 SET_PT (top_margin);
5416 else if (n < 0)
5418 int bottom_margin;
5420 /* If we scrolled backward, put point near the end of the window
5421 but not within the scroll margin. */
5422 SET_PT_BOTH (pos, pos_byte);
5423 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5424 if (XFASTINT (tem) == ht - this_scroll_margin)
5425 bottom_margin = PT;
5426 else
5427 bottom_margin = PT + 1;
5429 if (bottom_margin > marker_position (opoint_marker))
5430 SET_PT_BOTH (marker_position (opoint_marker),
5431 marker_byte_position (opoint_marker));
5432 else
5434 if (!NILP (Vscroll_preserve_screen_position))
5436 SET_PT_BOTH (pos, pos_byte);
5437 Fvertical_motion (original_pos, window);
5439 else
5440 Fvertical_motion (make_number (-1), window);
5444 else
5446 if (noerror)
5447 return;
5448 else
5449 xsignal0 (Qend_of_buffer);
5454 /* Scroll selected_window up or down. If N is nil, scroll a
5455 screen-full which is defined as the height of the window minus
5456 next_screen_context_lines. If N is the symbol `-', scroll.
5457 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5458 up. This is the guts of Fscroll_up and Fscroll_down. */
5460 static void
5461 scroll_command (Lisp_Object n, int direction)
5463 ptrdiff_t count = SPECPDL_INDEX ();
5465 eassert (eabs (direction) == 1);
5467 /* If selected window's buffer isn't current, make it current for
5468 the moment. But don't screw up if window_scroll gets an error. */
5469 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5471 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5472 Fset_buffer (XWINDOW (selected_window)->contents);
5475 if (NILP (n))
5476 window_scroll (selected_window, direction, 1, 0);
5477 else if (EQ (n, Qminus))
5478 window_scroll (selected_window, -direction, 1, 0);
5479 else
5481 n = Fprefix_numeric_value (n);
5482 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5485 unbind_to (count, Qnil);
5488 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5489 doc: /* Scroll text of selected window upward ARG lines.
5490 If ARG is omitted or nil, scroll upward by a near full screen.
5491 A near full screen is `next-screen-context-lines' less than a full screen.
5492 Negative ARG means scroll downward.
5493 If ARG is the atom `-', scroll downward by nearly full screen.
5494 When calling from a program, supply as argument a number, nil, or `-'. */)
5495 (Lisp_Object arg)
5497 scroll_command (arg, 1);
5498 return Qnil;
5501 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5502 doc: /* Scroll text of selected window down ARG lines.
5503 If ARG is omitted or nil, scroll down by a near full screen.
5504 A near full screen is `next-screen-context-lines' less than a full screen.
5505 Negative ARG means scroll upward.
5506 If ARG is the atom `-', scroll upward by nearly full screen.
5507 When calling from a program, supply as argument a number, nil, or `-'. */)
5508 (Lisp_Object arg)
5510 scroll_command (arg, -1);
5511 return Qnil;
5514 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5515 doc: /* Return the other window for \"other window scroll\" commands.
5516 If `other-window-scroll-buffer' is non-nil, a window
5517 showing that buffer is used.
5518 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5519 specifies the window. This takes precedence over
5520 `other-window-scroll-buffer'. */)
5521 (void)
5523 Lisp_Object window;
5525 if (MINI_WINDOW_P (XWINDOW (selected_window))
5526 && !NILP (Vminibuf_scroll_window))
5527 window = Vminibuf_scroll_window;
5528 /* If buffer is specified and live, scroll that buffer. */
5529 else if (!NILP (Vother_window_scroll_buffer)
5530 && BUFFERP (Vother_window_scroll_buffer)
5531 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5533 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5534 if (NILP (window))
5535 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5537 else
5539 /* Nothing specified; look for a neighboring window on the same
5540 frame. */
5541 window = Fnext_window (selected_window, Qnil, Qnil);
5543 if (EQ (window, selected_window))
5544 /* That didn't get us anywhere; look for a window on another
5545 visible frame. */
5547 window = Fnext_window (window, Qnil, Qt);
5548 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5549 && ! EQ (window, selected_window));
5552 CHECK_LIVE_WINDOW (window);
5554 if (EQ (window, selected_window))
5555 error ("There is no other window");
5557 return window;
5560 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5561 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5562 A near full screen is `next-screen-context-lines' less than a full screen.
5563 The next window is the one below the current one; or the one at the top
5564 if the current one is at the bottom. Negative ARG means scroll downward.
5565 If ARG is the atom `-', scroll downward by nearly full screen.
5566 When calling from a program, supply as argument a number, nil, or `-'.
5568 If `other-window-scroll-buffer' is non-nil, scroll the window
5569 showing that buffer, popping the buffer up if necessary.
5570 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5571 specifies the window to scroll. This takes precedence over
5572 `other-window-scroll-buffer'. */)
5573 (Lisp_Object arg)
5575 Lisp_Object window;
5576 struct window *w;
5577 ptrdiff_t count = SPECPDL_INDEX ();
5579 window = Fother_window_for_scrolling ();
5580 w = XWINDOW (window);
5582 /* Don't screw up if window_scroll gets an error. */
5583 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5585 Fset_buffer (w->contents);
5586 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5588 if (NILP (arg))
5589 window_scroll (window, 1, 1, 1);
5590 else if (EQ (arg, Qminus))
5591 window_scroll (window, -1, 1, 1);
5592 else
5594 if (CONSP (arg))
5595 arg = XCAR (arg);
5596 CHECK_NUMBER (arg);
5597 window_scroll (window, XINT (arg), 0, 1);
5600 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5601 unbind_to (count, Qnil);
5603 return Qnil;
5606 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5607 doc: /* Scroll selected window display ARG columns left.
5608 Default for ARG is window width minus 2.
5609 Value is the total amount of leftward horizontal scrolling in
5610 effect after the change.
5611 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5612 lower bound for automatic scrolling, i.e. automatic scrolling
5613 will not scroll a window to a column less than the value returned
5614 by this function. This happens in an interactive call. */)
5615 (register Lisp_Object arg, Lisp_Object set_minimum)
5617 struct window *w = XWINDOW (selected_window);
5618 EMACS_INT requested_arg = (NILP (arg)
5619 ? window_body_width (w, 0) - 2
5620 : XINT (Fprefix_numeric_value (arg)));
5621 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5623 if (!NILP (set_minimum))
5624 w->min_hscroll = w->hscroll;
5626 return result;
5629 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5630 doc: /* Scroll selected window display ARG columns right.
5631 Default for ARG is window width minus 2.
5632 Value is the total amount of leftward horizontal scrolling in
5633 effect after the change.
5634 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5635 lower bound for automatic scrolling, i.e. automatic scrolling
5636 will not scroll a window to a column less than the value returned
5637 by this function. This happens in an interactive call. */)
5638 (register Lisp_Object arg, Lisp_Object set_minimum)
5640 struct window *w = XWINDOW (selected_window);
5641 EMACS_INT requested_arg = (NILP (arg)
5642 ? window_body_width (w, 0) - 2
5643 : XINT (Fprefix_numeric_value (arg)));
5644 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5646 if (!NILP (set_minimum))
5647 w->min_hscroll = w->hscroll;
5649 return result;
5652 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5653 doc: /* Return the window which was selected when entering the minibuffer.
5654 Returns nil, if selected window is not a minibuffer window. */)
5655 (void)
5657 if (minibuf_level > 0
5658 && MINI_WINDOW_P (XWINDOW (selected_window))
5659 && WINDOW_LIVE_P (minibuf_selected_window))
5660 return minibuf_selected_window;
5662 return Qnil;
5665 /* Value is the number of lines actually displayed in window W,
5666 as opposed to its height. */
5668 static int
5669 displayed_window_lines (struct window *w)
5671 struct it it;
5672 struct text_pos start;
5673 int height = window_box_height (w);
5674 struct buffer *old_buffer;
5675 int bottom_y;
5676 void *itdata = NULL;
5678 if (XBUFFER (w->contents) != current_buffer)
5680 old_buffer = current_buffer;
5681 set_buffer_internal (XBUFFER (w->contents));
5683 else
5684 old_buffer = NULL;
5686 /* In case W->start is out of the accessible range, do something
5687 reasonable. This happens in Info mode when Info-scroll-down
5688 calls (recenter -1) while W->start is 1. */
5689 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5691 itdata = bidi_shelve_cache ();
5692 start_display (&it, w, start);
5693 move_it_vertically (&it, height);
5694 bottom_y = line_bottom_y (&it);
5695 bidi_unshelve_cache (itdata, 0);
5697 /* rms: On a non-window display,
5698 the value of it.vpos at the bottom of the screen
5699 seems to be 1 larger than window_box_height (w).
5700 This kludge fixes a bug whereby (move-to-window-line -1)
5701 when ZV is on the last screen line
5702 moves to the previous screen line instead of the last one. */
5703 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5704 height++;
5706 /* Add in empty lines at the bottom of the window. */
5707 if (bottom_y < height)
5709 int uy = FRAME_LINE_HEIGHT (it.f);
5710 it.vpos += (height - bottom_y + uy - 1) / uy;
5713 if (old_buffer)
5714 set_buffer_internal (old_buffer);
5716 return it.vpos;
5720 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5721 doc: /* Center point in selected window and maybe redisplay frame.
5722 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5723 relative to the selected window. If ARG is negative, it counts up from the
5724 bottom of the window. (ARG should be less than the height of the window.)
5726 If ARG is omitted or nil, then recenter with point on the middle line of
5727 the selected window; if the variable `recenter-redisplay' is non-nil,
5728 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5729 is set to `grow-only', this resets the tool-bar's height to the minimum
5730 height needed); if `recenter-redisplay' has the special value `tty',
5731 then only tty frames are redrawn.
5733 Just C-u as prefix means put point in the center of the window
5734 and redisplay normally--don't erase and redraw the frame. */)
5735 (register Lisp_Object arg)
5737 struct window *w = XWINDOW (selected_window);
5738 struct buffer *buf = XBUFFER (w->contents);
5739 struct buffer *obuf = current_buffer;
5740 bool center_p = 0;
5741 ptrdiff_t charpos, bytepos;
5742 EMACS_INT iarg IF_LINT (= 0);
5743 int this_scroll_margin;
5745 /* If redisplay is suppressed due to an error, try again. */
5746 obuf->display_error_modiff = 0;
5748 if (NILP (arg))
5750 if (!NILP (Vrecenter_redisplay)
5751 && (!EQ (Vrecenter_redisplay, Qtty)
5752 || !NILP (Ftty_type (selected_frame))))
5754 ptrdiff_t i;
5756 /* Invalidate pixel data calculated for all compositions. */
5757 for (i = 0; i < n_compositions; i++)
5758 composition_table[i]->font = NULL;
5759 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5760 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5761 #endif
5762 Fredraw_frame (WINDOW_FRAME (w));
5763 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5766 center_p = 1;
5768 else if (CONSP (arg)) /* Just C-u. */
5769 center_p = 1;
5770 else
5772 arg = Fprefix_numeric_value (arg);
5773 CHECK_NUMBER (arg);
5774 iarg = XINT (arg);
5777 set_buffer_internal (buf);
5779 /* Do this after making BUF current
5780 in case scroll_margin is buffer-local. */
5781 this_scroll_margin =
5782 max (0, min (scroll_margin, w->total_lines / 4));
5784 /* Handle centering on a graphical frame specially. Such frames can
5785 have variable-height lines and centering point on the basis of
5786 line counts would lead to strange effects. */
5787 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5789 if (center_p)
5791 struct it it;
5792 struct text_pos pt;
5793 void *itdata = bidi_shelve_cache ();
5795 SET_TEXT_POS (pt, PT, PT_BYTE);
5796 start_display (&it, w, pt);
5797 move_it_vertically_backward (&it, window_box_height (w) / 2);
5798 charpos = IT_CHARPOS (it);
5799 bytepos = IT_BYTEPOS (it);
5800 bidi_unshelve_cache (itdata, 0);
5802 else if (iarg < 0)
5804 struct it it;
5805 struct text_pos pt;
5806 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5807 int extra_line_spacing;
5808 int h = window_box_height (w);
5809 void *itdata = bidi_shelve_cache ();
5811 iarg = - max (-iarg, this_scroll_margin);
5813 SET_TEXT_POS (pt, PT, PT_BYTE);
5814 start_display (&it, w, pt);
5816 /* Be sure we have the exact height of the full line containing PT. */
5817 move_it_by_lines (&it, 0);
5819 /* The amount of pixels we have to move back is the window
5820 height minus what's displayed in the line containing PT,
5821 and the lines below. */
5822 it.current_y = 0;
5823 it.vpos = 0;
5824 move_it_by_lines (&it, nlines);
5826 if (it.vpos == nlines)
5827 h -= it.current_y;
5828 else
5830 /* Last line has no newline */
5831 h -= line_bottom_y (&it);
5832 it.vpos++;
5835 /* Don't reserve space for extra line spacing of last line. */
5836 extra_line_spacing = it.max_extra_line_spacing;
5838 /* If we can't move down NLINES lines because we hit
5839 the end of the buffer, count in some empty lines. */
5840 if (it.vpos < nlines)
5842 nlines -= it.vpos;
5843 extra_line_spacing = it.extra_line_spacing;
5844 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5846 if (h <= 0)
5848 bidi_unshelve_cache (itdata, 0);
5849 return Qnil;
5852 /* Now find the new top line (starting position) of the window. */
5853 start_display (&it, w, pt);
5854 it.current_y = 0;
5855 move_it_vertically_backward (&it, h);
5857 /* If extra line spacing is present, we may move too far
5858 back. This causes the last line to be only partially
5859 visible (which triggers redisplay to recenter that line
5860 in the middle), so move forward.
5861 But ignore extra line spacing on last line, as it is not
5862 considered to be part of the visible height of the line.
5864 h += extra_line_spacing;
5865 while (-it.current_y > h)
5866 move_it_by_lines (&it, 1);
5868 charpos = IT_CHARPOS (it);
5869 bytepos = IT_BYTEPOS (it);
5871 bidi_unshelve_cache (itdata, 0);
5873 else
5875 struct position pos;
5877 iarg = max (iarg, this_scroll_margin);
5879 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5880 charpos = pos.bufpos;
5881 bytepos = pos.bytepos;
5884 else
5886 struct position pos;
5887 int ht = window_internal_height (w);
5889 if (center_p)
5890 iarg = ht / 2;
5891 else if (iarg < 0)
5892 iarg += ht;
5894 /* Don't let it get into the margin at either top or bottom. */
5895 iarg = clip_to_bounds (this_scroll_margin, iarg,
5896 ht - this_scroll_margin - 1);
5898 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5899 charpos = pos.bufpos;
5900 bytepos = pos.bytepos;
5903 /* Set the new window start. */
5904 set_marker_both (w->start, w->contents, charpos, bytepos);
5905 w->window_end_valid = 0;
5907 w->optional_new_start = 1;
5909 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5910 FETCH_BYTE (bytepos - 1) == '\n');
5912 wset_redisplay (w);
5914 set_buffer_internal (obuf);
5915 return Qnil;
5918 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5919 0, 1, 0,
5920 doc: /* Return the width in columns of the text display area of WINDOW.
5921 WINDOW must be a live window and defaults to the selected one.
5923 The returned width does not include dividers, scrollbars, margins,
5924 fringes, nor any partial-width columns at the right of the text
5925 area. */)
5926 (Lisp_Object window)
5928 struct window *w = decode_live_window (window);
5930 return make_number (window_box_width (w, TEXT_AREA)
5931 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5934 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5935 0, 1, 0,
5936 doc: /* Return the height in lines of the text display area of WINDOW.
5937 WINDOW must be a live window and defaults to the selected one.
5939 The returned height does not include dividers, the mode line, any header
5940 line, nor any partial-height lines at the bottom of the text area. */)
5941 (Lisp_Object window)
5943 struct window *w = decode_live_window (window);
5945 return make_number (window_box_height (w)
5946 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5949 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5950 1, 1, "P",
5951 doc: /* Position point relative to window.
5952 ARG nil means position point at center of window.
5953 Else, ARG specifies vertical position within the window;
5954 zero means top of window, negative means relative to bottom of window. */)
5955 (Lisp_Object arg)
5957 struct window *w = XWINDOW (selected_window);
5958 int lines, start;
5959 Lisp_Object window;
5960 #if 0
5961 int this_scroll_margin;
5962 #endif
5964 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5965 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5966 when passed below to set_marker_both. */
5967 error ("move-to-window-line called from unrelated buffer");
5969 window = selected_window;
5970 start = marker_position (w->start);
5971 if (start < BEGV || start > ZV)
5973 int height = window_internal_height (w);
5974 Fvertical_motion (make_number (- (height / 2)), window);
5975 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5976 w->start_at_line_beg = !NILP (Fbolp ());
5977 w->force_start = 1;
5979 else
5980 Fgoto_char (w->start);
5982 lines = displayed_window_lines (w);
5984 #if 0
5985 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5986 #endif
5988 if (NILP (arg))
5989 XSETFASTINT (arg, lines / 2);
5990 else
5992 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5994 if (iarg < 0)
5995 iarg = iarg + lines;
5997 #if 0 /* This code would prevent move-to-window-line from moving point
5998 to a place inside the scroll margins (which would cause the
5999 next redisplay to scroll). I wrote this code, but then concluded
6000 it is probably better not to install it. However, it is here
6001 inside #if 0 so as not to lose it. -- rms. */
6003 /* Don't let it get into the margin at either top or bottom. */
6004 iarg = max (iarg, this_scroll_margin);
6005 iarg = min (iarg, lines - this_scroll_margin - 1);
6006 #endif
6008 arg = make_number (iarg);
6011 /* Skip past a partially visible first line. */
6012 if (w->vscroll)
6013 XSETINT (arg, XINT (arg) + 1);
6015 return Fvertical_motion (arg, window);
6020 /***********************************************************************
6021 Window Configuration
6022 ***********************************************************************/
6024 struct save_window_data
6026 struct vectorlike_header header;
6027 Lisp_Object selected_frame;
6028 Lisp_Object current_window;
6029 Lisp_Object current_buffer;
6030 Lisp_Object minibuf_scroll_window;
6031 Lisp_Object minibuf_selected_window;
6032 Lisp_Object root_window;
6033 Lisp_Object focus_frame;
6034 /* A vector, each of whose elements is a struct saved_window
6035 for one window. */
6036 Lisp_Object saved_windows;
6038 /* All fields above are traced by the GC.
6039 From `frame-cols' down, the fields are ignored by the GC. */
6040 /* We should be able to do without the following two. */
6041 int frame_cols, frame_lines;
6042 /* These two should get eventually replaced by their pixel
6043 counterparts. */
6044 int frame_menu_bar_lines, frame_tool_bar_lines;
6045 int frame_text_width, frame_text_height;
6046 /* These are currently unused. We need them as soon as we convert
6047 to pixels. */
6048 int frame_menu_bar_height, frame_tool_bar_height;
6051 /* This is saved as a Lisp_Vector */
6052 struct saved_window
6054 struct vectorlike_header header;
6056 Lisp_Object window, buffer, start, pointm, mark;
6057 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6058 Lisp_Object left_col, top_line, total_cols, total_lines;
6059 Lisp_Object normal_cols, normal_lines;
6060 Lisp_Object hscroll, min_hscroll;
6061 Lisp_Object parent, prev;
6062 Lisp_Object start_at_line_beg;
6063 Lisp_Object display_table;
6064 Lisp_Object left_margin_cols, right_margin_cols;
6065 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6066 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6067 Lisp_Object combination_limit, window_parameters;
6070 #define SAVED_WINDOW_N(swv,n) \
6071 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6073 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6074 doc: /* Return t if OBJECT is a window-configuration object. */)
6075 (Lisp_Object object)
6077 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6080 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6081 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6082 (Lisp_Object config)
6084 register struct save_window_data *data;
6085 struct Lisp_Vector *saved_windows;
6087 CHECK_WINDOW_CONFIGURATION (config);
6089 data = (struct save_window_data *) XVECTOR (config);
6090 saved_windows = XVECTOR (data->saved_windows);
6091 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6094 /* From Chong's unwind_create_frame_1. */
6095 static void
6096 unwind_change_frame (Lisp_Object val)
6098 inhibit_lisp_code = val;
6101 DEFUN ("set-window-configuration", Fset_window_configuration,
6102 Sset_window_configuration, 1, 1, 0,
6103 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6104 CONFIGURATION must be a value previously returned
6105 by `current-window-configuration' (which see).
6106 If CONFIGURATION was made from a frame that is now deleted,
6107 only frame-independent values can be restored. In this case,
6108 the return value is nil. Otherwise the value is t. */)
6109 (Lisp_Object configuration)
6111 register struct save_window_data *data;
6112 struct Lisp_Vector *saved_windows;
6113 Lisp_Object new_current_buffer;
6114 Lisp_Object frame;
6115 struct frame *f;
6116 ptrdiff_t old_point = -1;
6118 CHECK_WINDOW_CONFIGURATION (configuration);
6120 data = (struct save_window_data *) XVECTOR (configuration);
6121 saved_windows = XVECTOR (data->saved_windows);
6123 new_current_buffer = data->current_buffer;
6124 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6125 new_current_buffer = Qnil;
6126 else
6128 if (XBUFFER (new_current_buffer) == current_buffer)
6129 /* The code further down "preserves point" by saving here PT in
6130 old_point and then setting it later back into PT. When the
6131 current-selected-window and the final-selected-window both show
6132 the current buffer, this suffers from the problem that the
6133 current PT is the window-point of the current-selected-window,
6134 while the final PT is the point of the final-selected-window, so
6135 this copy from one PT to the other would end up moving the
6136 window-point of the final-selected-window to the window-point of
6137 the current-selected-window. So we have to be careful which
6138 point of the current-buffer we copy into old_point. */
6139 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6140 && WINDOWP (selected_window)
6141 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6142 && !EQ (selected_window, data->current_window))
6143 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6144 else
6145 old_point = PT;
6146 else
6147 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6148 point in new_current_buffer as of the last time this buffer was
6149 used. This can be non-deterministic since it can be changed by
6150 things like jit-lock by mere temporary selection of some random
6151 window that happens to show this buffer.
6152 So if possible we want this arbitrary choice of "which point" to
6153 be the one from the to-be-selected-window so as to prevent this
6154 window's cursor from being copied from another window. */
6155 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6156 /* If current_window = selected_window, its point is in BUF_PT. */
6157 && !EQ (selected_window, data->current_window))
6158 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6159 else
6160 old_point = BUF_PT (XBUFFER (new_current_buffer));
6163 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6164 f = XFRAME (frame);
6166 /* If f is a dead frame, don't bother rebuilding its window tree.
6167 However, there is other stuff we should still try to do below. */
6168 if (FRAME_LIVE_P (f))
6170 Lisp_Object window;
6171 Lisp_Object dead_windows = Qnil;
6172 register Lisp_Object tem, par, pers;
6173 register struct window *w;
6174 register struct saved_window *p;
6175 struct window *root_window;
6176 struct window **leaf_windows;
6177 int n_leaf_windows;
6178 ptrdiff_t k;
6179 int i, n;
6180 ptrdiff_t count = SPECPDL_INDEX ();
6181 /* If the frame has been resized since this window configuration was
6182 made, we change the frame to the size specified in the
6183 configuration, restore the configuration, and then resize it
6184 back. We keep track of the prevailing height in these variables. */
6185 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6186 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6187 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6188 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6189 /* int previous_frame_lines = FRAME_LINES (f); */
6190 /* int previous_frame_cols = FRAME_COLS (f); */
6191 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6192 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6194 /* Don't do this within the main loop below: This may call Lisp
6195 code and is thus potentially unsafe while input is blocked. */
6196 for (k = 0; k < saved_windows->header.size; k++)
6198 p = SAVED_WINDOW_N (saved_windows, k);
6199 window = p->window;
6200 w = XWINDOW (window);
6201 if (BUFFERP (w->contents)
6202 && !EQ (w->contents, p->buffer)
6203 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6204 /* If a window we restore gets another buffer, record the
6205 window's old buffer. */
6206 call1 (Qrecord_window_buffer, window);
6209 /* Don't run lisp in the following segment since the frame is in a
6210 completely inconsistent state. See Bug#16207. */
6211 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6212 inhibit_lisp_code = Qt;
6213 /* The mouse highlighting code could get screwed up
6214 if it runs during this. */
6215 block_input ();
6217 if (data->frame_text_width != previous_frame_text_width
6218 || data->frame_text_height != previous_frame_text_height)
6219 change_frame_size (f, data->frame_text_width,
6220 data->frame_text_height, 0, 0, 0, 1);
6222 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6224 #ifdef HAVE_WINDOW_SYSTEM
6225 if (FRAME_WINDOW_P (f))
6226 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6227 make_number (0));
6228 else /* TTY or MSDOS */
6229 #endif
6230 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6231 make_number (0));
6233 #ifdef HAVE_WINDOW_SYSTEM
6234 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6235 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6236 make_number (0));
6237 #endif
6239 /* "Swap out" point from the selected window's buffer
6240 into the window itself. (Normally the pointm of the selected
6241 window holds garbage.) We do this now, before
6242 restoring the window contents, and prevent it from
6243 being done later on when we select a new window. */
6244 if (! NILP (XWINDOW (selected_window)->contents))
6246 w = XWINDOW (selected_window);
6247 set_marker_both (w->pointm,
6248 w->contents,
6249 BUF_PT (XBUFFER (w->contents)),
6250 BUF_PT_BYTE (XBUFFER (w->contents)));
6253 fset_redisplay (f);
6254 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6256 /* Problem: Freeing all matrices and later allocating them again
6257 is a serious redisplay flickering problem. What we would
6258 really like to do is to free only those matrices not reused
6259 below. */
6260 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6261 leaf_windows = alloca (count_windows (root_window)
6262 * sizeof *leaf_windows);
6263 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6265 /* Kludge Alert!
6266 Mark all windows now on frame as "deleted".
6267 Restoring the new configuration "undeletes" any that are in it.
6269 Save their current buffers in their height fields, since we may
6270 need it later, if a buffer saved in the configuration is now
6271 dead. */
6272 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6274 for (k = 0; k < saved_windows->header.size; k++)
6276 p = SAVED_WINDOW_N (saved_windows, k);
6277 window = p->window;
6278 w = XWINDOW (window);
6279 wset_next (w, Qnil);
6281 if (!NILP (p->parent))
6282 wset_parent
6283 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6284 else
6285 wset_parent (w, Qnil);
6287 if (!NILP (p->prev))
6289 wset_prev
6290 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6291 wset_next (XWINDOW (w->prev), p->window);
6293 else
6295 wset_prev (w, Qnil);
6296 if (!NILP (w->parent))
6297 wset_combination (XWINDOW (w->parent),
6298 (XINT (p->total_cols)
6299 != XWINDOW (w->parent)->total_cols),
6300 p->window);
6303 /* If we squirreled away the buffer, restore it now. */
6304 if (BUFFERP (w->combination_limit))
6305 wset_buffer (w, w->combination_limit);
6306 w->pixel_left = XFASTINT (p->pixel_left);
6307 w->pixel_top = XFASTINT (p->pixel_top);
6308 w->pixel_width = XFASTINT (p->pixel_width);
6309 w->pixel_height = XFASTINT (p->pixel_height);
6310 w->left_col = XFASTINT (p->left_col);
6311 w->top_line = XFASTINT (p->top_line);
6312 w->total_cols = XFASTINT (p->total_cols);
6313 w->total_lines = XFASTINT (p->total_lines);
6314 wset_normal_cols (w, p->normal_cols);
6315 wset_normal_lines (w, p->normal_lines);
6316 w->hscroll = XFASTINT (p->hscroll);
6317 w->min_hscroll = XFASTINT (p->min_hscroll);
6318 wset_display_table (w, p->display_table);
6319 w->left_margin_cols = XINT (p->left_margin_cols);
6320 w->right_margin_cols = XINT (p->right_margin_cols);
6321 w->left_fringe_width = XINT (p->left_fringe_width);
6322 w->right_fringe_width = XINT (p->right_fringe_width);
6323 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6324 w->scroll_bar_width = XINT (p->scroll_bar_width);
6325 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6326 wset_dedicated (w, p->dedicated);
6327 wset_combination_limit (w, p->combination_limit);
6328 /* Restore any window parameters that have been saved.
6329 Parameters that have not been saved are left alone. */
6330 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6332 pers = XCAR (tem);
6333 if (CONSP (pers))
6335 if (NILP (XCDR (pers)))
6337 par = Fassq (XCAR (pers), w->window_parameters);
6338 if (CONSP (par) && !NILP (XCDR (par)))
6339 /* Reset a parameter to nil if and only if it
6340 has a non-nil association. Don't make new
6341 associations. */
6342 Fsetcdr (par, Qnil);
6344 else
6345 /* Always restore a non-nil value. */
6346 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6350 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6351 /* If saved buffer is alive, install it. */
6353 wset_buffer (w, p->buffer);
6354 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6355 set_marker_restricted (w->start, p->start, w->contents);
6356 set_marker_restricted (w->pointm, p->pointm,
6357 w->contents);
6358 Fset_marker (BVAR (XBUFFER (w->contents), mark),
6359 p->mark, w->contents);
6361 /* As documented in Fcurrent_window_configuration, don't
6362 restore the location of point in the buffer which was
6363 current when the window configuration was recorded. */
6364 if (!EQ (p->buffer, new_current_buffer)
6365 && XBUFFER (p->buffer) == current_buffer)
6366 Fgoto_char (w->pointm);
6368 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6369 /* Keep window's old buffer; make sure the markers are real. */
6371 /* Set window markers at start of visible range. */
6372 if (XMARKER (w->start)->buffer == 0)
6373 set_marker_restricted_both (w->start, w->contents, 0, 0);
6374 if (XMARKER (w->pointm)->buffer == 0)
6375 set_marker_restricted_both
6376 (w->pointm, w->contents,
6377 BUF_PT (XBUFFER (w->contents)),
6378 BUF_PT_BYTE (XBUFFER (w->contents)));
6379 w->start_at_line_beg = 1;
6381 else if (!NILP (w->start))
6382 /* Leaf window has no live buffer, get one. */
6384 /* Get the buffer via other_buffer_safely in order to
6385 avoid showing an unimportant buffer and, if necessary, to
6386 recreate *scratch* in the course (part of Juanma's bs-show
6387 scenario from March 2011). */
6388 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6389 /* This will set the markers to beginning of visible
6390 range. */
6391 set_marker_restricted_both (w->start, w->contents, 0, 0);
6392 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6393 w->start_at_line_beg = 1;
6394 if (!NILP (w->dedicated))
6395 /* Record this window as dead. */
6396 dead_windows = Fcons (window, dead_windows);
6397 /* Make sure window is no more dedicated. */
6398 wset_dedicated (w, Qnil);
6402 fset_root_window (f, data->root_window);
6403 /* Arrange *not* to restore point in the buffer that was
6404 current when the window configuration was saved. */
6405 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6406 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6407 make_number (old_point),
6408 XWINDOW (data->current_window)->contents);
6410 /* In the following call to `select-window', prevent "swapping out
6411 point" in the old selected window using the buffer that has
6412 been restored into it. We already swapped out that point from
6413 that window's old buffer.
6415 Do not record the buffer here. We do that in a separate call
6416 to select_window below. See also Bug#16207. */
6417 select_window (data->current_window, Qt, 1);
6418 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6419 last_selected_window)
6420 = selected_window;
6422 if (NILP (data->focus_frame)
6423 || (FRAMEP (data->focus_frame)
6424 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6425 Fredirect_frame_focus (frame, data->focus_frame);
6427 /* Set the frame size to the value it had before this function. */
6428 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6429 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6430 change_frame_size (f, previous_frame_text_width,
6431 previous_frame_text_height, 0, 0, 0, 1);
6433 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6435 #ifdef HAVE_WINDOW_SYSTEM
6436 if (FRAME_WINDOW_P (f))
6437 x_set_menu_bar_lines (f,
6438 make_number (previous_frame_menu_bar_lines),
6439 make_number (0));
6440 else /* TTY or MSDOS */
6441 #endif
6442 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6443 make_number (0));
6445 #ifdef HAVE_WINDOW_SYSTEM
6446 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6447 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6448 make_number (0));
6449 #endif
6451 /* Now, free glyph matrices in windows that were not reused. */
6452 for (i = n = 0; i < n_leaf_windows; ++i)
6454 if (NILP (leaf_windows[i]->contents))
6455 free_window_matrices (leaf_windows[i]);
6456 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6457 ++n;
6460 adjust_frame_glyphs (f);
6461 unblock_input ();
6462 unbind_to (count, Qnil);
6464 /* Scan dead buffer windows. */
6465 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6467 window = XCAR (dead_windows);
6468 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6469 delete_deletable_window (window);
6472 /* Record the selected window's buffer here. The window should
6473 already be the selected one from the call above. */
6474 select_window (data->current_window, Qnil, 0);
6476 /* Fselect_window will have made f the selected frame, so we
6477 reselect the proper frame here. Fhandle_switch_frame will change the
6478 selected window too, but that doesn't make the call to
6479 Fselect_window above totally superfluous; it still sets f's
6480 selected window. */
6481 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6482 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6484 run_window_configuration_change_hook (f);
6487 if (!NILP (new_current_buffer))
6489 Fset_buffer (new_current_buffer);
6490 /* If the new current buffer doesn't appear in the selected
6491 window, go to its old point (see bug#12208). */
6492 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6493 Fgoto_char (make_number (old_point));
6496 Vminibuf_scroll_window = data->minibuf_scroll_window;
6497 minibuf_selected_window = data->minibuf_selected_window;
6499 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6502 void
6503 restore_window_configuration (Lisp_Object configuration)
6505 Fset_window_configuration (configuration);
6509 /* If WINDOW is an internal window, recursively delete all child windows
6510 reachable via the next and contents slots of WINDOW. Otherwise setup
6511 WINDOW to not show any buffer. */
6513 void
6514 delete_all_child_windows (Lisp_Object window)
6516 register struct window *w;
6518 w = XWINDOW (window);
6520 if (!NILP (w->next))
6521 /* Delete WINDOW's siblings (we traverse postorderly). */
6522 delete_all_child_windows (w->next);
6524 if (WINDOWP (w->contents))
6526 delete_all_child_windows (w->contents);
6527 wset_combination (w, 0, Qnil);
6529 else if (BUFFERP (w->contents))
6531 unshow_buffer (w);
6532 unchain_marker (XMARKER (w->pointm));
6533 unchain_marker (XMARKER (w->start));
6534 /* Since combination limit makes sense for an internal windows
6535 only, we use this slot to save the buffer for the sake of
6536 possible resurrection in Fset_window_configuration. */
6537 wset_combination_limit (w, w->contents);
6538 wset_buffer (w, Qnil);
6541 Vwindow_list = Qnil;
6544 static int
6545 count_windows (register struct window *window)
6547 register int count = 1;
6548 if (!NILP (window->next))
6549 count += count_windows (XWINDOW (window->next));
6550 if (WINDOWP (window->contents))
6551 count += count_windows (XWINDOW (window->contents));
6552 return count;
6556 /* Fill vector FLAT with leaf windows under W, starting at index I.
6557 Value is last index + 1. */
6558 static int
6559 get_leaf_windows (struct window *w, struct window **flat, int i)
6561 while (w)
6563 if (WINDOWP (w->contents))
6564 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6565 else
6566 flat[i++] = w;
6568 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6571 return i;
6575 /* Return a pointer to the glyph W's physical cursor is on. Value is
6576 null if W's current matrix is invalid, so that no meaningful glyph
6577 can be returned. */
6578 struct glyph *
6579 get_phys_cursor_glyph (struct window *w)
6581 struct glyph_row *row;
6582 struct glyph *glyph;
6583 int hpos = w->phys_cursor.hpos;
6585 if (!(w->phys_cursor.vpos >= 0
6586 && w->phys_cursor.vpos < w->current_matrix->nrows))
6587 return NULL;
6589 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6590 if (!row->enabled_p)
6591 return NULL;
6593 if (w->hscroll)
6595 /* When the window is hscrolled, cursor hpos can legitimately be
6596 out of bounds, but we draw the cursor at the corresponding
6597 window margin in that case. */
6598 if (!row->reversed_p && hpos < 0)
6599 hpos = 0;
6600 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6601 hpos = row->used[TEXT_AREA] - 1;
6604 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6605 glyph = row->glyphs[TEXT_AREA] + hpos;
6606 else
6607 glyph = NULL;
6609 return glyph;
6613 static int
6614 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6616 register struct saved_window *p;
6617 register struct window *w;
6618 register Lisp_Object tem, pers, par;
6620 for (; !NILP (window); window = w->next)
6622 p = SAVED_WINDOW_N (vector, i);
6623 w = XWINDOW (window);
6625 wset_temslot (w, make_number (i)); i++;
6626 p->window = window;
6627 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6628 p->pixel_left = make_number (w->pixel_left);
6629 p->pixel_top = make_number (w->pixel_top);
6630 p->pixel_width = make_number (w->pixel_width);
6631 p->pixel_height = make_number (w->pixel_height);
6632 p->left_col = make_number (w->left_col);
6633 p->top_line = make_number (w->top_line);
6634 p->total_cols = make_number (w->total_cols);
6635 p->total_lines = make_number (w->total_lines);
6636 p->normal_cols = w->normal_cols;
6637 p->normal_lines = w->normal_lines;
6638 XSETFASTINT (p->hscroll, w->hscroll);
6639 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6640 p->display_table = w->display_table;
6641 p->left_margin_cols = make_number (w->left_margin_cols);
6642 p->right_margin_cols = make_number (w->right_margin_cols);
6643 p->left_fringe_width = make_number (w->left_fringe_width);
6644 p->right_fringe_width = make_number (w->right_fringe_width);
6645 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6646 p->scroll_bar_width = make_number (w->scroll_bar_width);
6647 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6648 p->dedicated = w->dedicated;
6649 p->combination_limit = w->combination_limit;
6650 p->window_parameters = Qnil;
6652 if (!NILP (Vwindow_persistent_parameters))
6654 /* Run cycle detection on Vwindow_persistent_parameters. */
6655 Lisp_Object tortoise, hare;
6657 hare = tortoise = Vwindow_persistent_parameters;
6658 while (CONSP (hare))
6660 hare = XCDR (hare);
6661 if (!CONSP (hare))
6662 break;
6664 hare = XCDR (hare);
6665 tortoise = XCDR (tortoise);
6667 if (EQ (hare, tortoise))
6668 /* Reset Vwindow_persistent_parameters to Qnil. */
6670 Vwindow_persistent_parameters = Qnil;
6671 break;
6675 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6676 tem = XCDR (tem))
6678 pers = XCAR (tem);
6679 /* Save values for persistent window parameters. */
6680 if (CONSP (pers) && !NILP (XCDR (pers)))
6682 par = Fassq (XCAR (pers), w->window_parameters);
6683 if (NILP (par))
6684 /* If the window has no value for the parameter,
6685 make one. */
6686 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6687 p->window_parameters);
6688 else
6689 /* If the window has a value for the parameter,
6690 save it. */
6691 p->window_parameters = Fcons (Fcons (XCAR (par),
6692 XCDR (par)),
6693 p->window_parameters);
6698 if (BUFFERP (w->contents))
6700 /* Save w's value of point in the window configuration. If w
6701 is the selected window, then get the value of point from
6702 the buffer; pointm is garbage in the selected window. */
6703 if (EQ (window, selected_window))
6704 p->pointm = build_marker (XBUFFER (w->contents),
6705 BUF_PT (XBUFFER (w->contents)),
6706 BUF_PT_BYTE (XBUFFER (w->contents)));
6707 else
6708 p->pointm = Fcopy_marker (w->pointm, Qnil);
6709 XMARKER (p->pointm)->insertion_type
6710 = !NILP (buffer_local_value_1 /* Don't signal error if void. */
6711 (Qwindow_point_insertion_type, w->contents));
6713 p->start = Fcopy_marker (w->start, Qnil);
6714 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6716 tem = BVAR (XBUFFER (w->contents), mark);
6717 p->mark = Fcopy_marker (tem, Qnil);
6719 else
6721 p->pointm = Qnil;
6722 p->start = Qnil;
6723 p->mark = Qnil;
6724 p->start_at_line_beg = Qnil;
6727 if (NILP (w->parent))
6728 p->parent = Qnil;
6729 else
6730 p->parent = XWINDOW (w->parent)->temslot;
6732 if (NILP (w->prev))
6733 p->prev = Qnil;
6734 else
6735 p->prev = XWINDOW (w->prev)->temslot;
6737 if (WINDOWP (w->contents))
6738 i = save_window_save (w->contents, vector, i);
6741 return i;
6744 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6745 Scurrent_window_configuration, 0, 1, 0,
6746 doc: /* Return an object representing the current window configuration of FRAME.
6747 If FRAME is nil or omitted, use the selected frame.
6748 This describes the number of windows, their sizes and current buffers,
6749 and for each displayed buffer, where display starts, and the positions of
6750 point and mark. An exception is made for point in the current buffer:
6751 its value is -not- saved.
6752 This also records the currently selected frame, and FRAME's focus
6753 redirection (see `redirect-frame-focus'). The variable
6754 `window-persistent-parameters' specifies which window parameters are
6755 saved by this function. */)
6756 (Lisp_Object frame)
6758 register Lisp_Object tem;
6759 register int n_windows;
6760 register struct save_window_data *data;
6761 register int i;
6762 struct frame *f = decode_live_frame (frame);
6764 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6765 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6766 PVEC_WINDOW_CONFIGURATION);
6768 data->frame_cols = FRAME_COLS (f);
6769 data->frame_lines = FRAME_LINES (f);
6770 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6771 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6772 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6773 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6774 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6775 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6776 data->selected_frame = selected_frame;
6777 data->current_window = FRAME_SELECTED_WINDOW (f);
6778 XSETBUFFER (data->current_buffer, current_buffer);
6779 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6780 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6781 data->root_window = FRAME_ROOT_WINDOW (f);
6782 data->focus_frame = FRAME_FOCUS_FRAME (f);
6783 tem = make_uninit_vector (n_windows);
6784 data->saved_windows = tem;
6785 for (i = 0; i < n_windows; i++)
6786 ASET (tem, i,
6787 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6788 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6789 XSETWINDOW_CONFIGURATION (tem, data);
6790 return (tem);
6793 /* Called after W's margins, fringes or scroll bars was adjusted. */
6795 static void
6796 apply_window_adjustment (struct window *w)
6798 eassert (w);
6799 adjust_window_margins (w);
6800 clear_glyph_matrix (w->current_matrix);
6801 w->window_end_valid = 0;
6802 windows_or_buffers_changed = 30;
6803 wset_redisplay (w);
6804 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6808 /***********************************************************************
6809 Marginal Areas
6810 ***********************************************************************/
6812 static struct window *
6813 set_window_margins (struct window *w, Lisp_Object left_width,
6814 Lisp_Object right_width)
6816 int left, right;
6818 /* FIXME: what about margins that are too wide? */
6819 left = (NILP (left_width) ? 0
6820 : (CHECK_NATNUM (left_width), XINT (left_width)));
6821 right = (NILP (right_width) ? 0
6822 : (CHECK_NATNUM (right_width), XINT (right_width)));
6824 if (w->left_margin_cols != left || w->right_margin_cols != right)
6826 w->left_margin_cols = left;
6827 w->right_margin_cols = right;
6828 return w;
6830 return NULL;
6833 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6834 2, 3, 0,
6835 doc: /* Set width of marginal areas of window WINDOW.
6836 WINDOW must be a live window and defaults to the selected one.
6838 Second arg LEFT-WIDTH specifies the number of character cells to
6839 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6840 does the same for the right marginal area. A nil width parameter
6841 means no margin.
6843 Return t if any margin was actually changed and nil otherwise. */)
6844 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6846 struct window *w = set_window_margins (decode_live_window (window),
6847 left_width, right_width);
6848 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6852 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6853 0, 1, 0,
6854 doc: /* Get width of marginal areas of window WINDOW.
6855 WINDOW must be a live window and defaults to the selected one.
6857 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6858 If a marginal area does not exist, its width will be returned
6859 as nil. */)
6860 (Lisp_Object window)
6862 struct window *w = decode_live_window (window);
6863 return Fcons (w->left_margin_cols
6864 ? make_number (w->left_margin_cols) : Qnil,
6865 w->right_margin_cols
6866 ? make_number (w->right_margin_cols) : Qnil);
6871 /***********************************************************************
6872 Fringes
6873 ***********************************************************************/
6875 static struct window *
6876 set_window_fringes (struct window *w, Lisp_Object left_width,
6877 Lisp_Object right_width, Lisp_Object outside_margins)
6879 int left, right, outside = !NILP (outside_margins);
6881 left = (NILP (left_width) ? -1
6882 : (CHECK_NATNUM (left_width), XINT (left_width)));
6883 right = (NILP (right_width) ? -1
6884 : (CHECK_NATNUM (right_width), XINT (right_width)));
6886 /* Do nothing on a tty or if nothing to actually change. */
6887 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6888 && (w->left_fringe_width != left
6889 || w->right_fringe_width != right
6890 || w->fringes_outside_margins != outside))
6892 w->left_fringe_width = left;
6893 w->right_fringe_width = right;
6894 w->fringes_outside_margins = outside;
6895 return w;
6897 return NULL;
6900 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6901 2, 4, 0,
6902 doc: /* Set the fringe widths of window WINDOW.
6903 WINDOW must be a live window and defaults to the selected one.
6905 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6906 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6907 fringe width. If a fringe width arg is nil, that means to use the
6908 frame's default fringe width. Default fringe widths can be set with
6909 the command `set-fringe-style'.
6910 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6911 outside of the display margins. By default, fringes are drawn between
6912 display marginal areas and the text area.
6914 Return t if any fringe was actually changed and nil otherwise. */)
6915 (Lisp_Object window, Lisp_Object left_width,
6916 Lisp_Object right_width, Lisp_Object outside_margins)
6918 struct window *w
6919 = set_window_fringes (decode_live_window (window),
6920 left_width, right_width, outside_margins);
6921 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6925 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6926 0, 1, 0,
6927 doc: /* Get width of fringes of window WINDOW.
6928 WINDOW must be a live window and defaults to the selected one.
6930 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6931 (Lisp_Object window)
6933 struct window *w = decode_live_window (window);
6935 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6936 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6937 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6942 /***********************************************************************
6943 Scroll bars
6944 ***********************************************************************/
6946 static struct window *
6947 set_window_scroll_bars (struct window *w, Lisp_Object width,
6948 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6950 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6952 if (iwidth == 0)
6953 vertical_type = Qnil;
6955 if (!(NILP (vertical_type)
6956 || EQ (vertical_type, Qleft)
6957 || EQ (vertical_type, Qright)
6958 || EQ (vertical_type, Qt)))
6959 error ("Invalid type of vertical scroll bar");
6961 if (w->scroll_bar_width != iwidth
6962 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6964 w->scroll_bar_width = iwidth;
6965 wset_vertical_scroll_bar_type (w, vertical_type);
6966 return w;
6968 return NULL;
6971 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6972 Sset_window_scroll_bars, 2, 4, 0,
6973 doc: /* Set width and type of scroll bars of window WINDOW.
6974 WINDOW must be a live window and defaults to the selected one.
6976 Second parameter WIDTH specifies the pixel width for the scroll bar.
6977 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6978 bar: left, right, or nil.
6979 If WIDTH is nil, use the frame's scroll-bar width.
6980 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6981 Fourth parameter HORIZONTAL-TYPE is currently unused.
6983 Return t if scroll bars were actually changed and nil otherwise. */)
6984 (Lisp_Object window, Lisp_Object width,
6985 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6987 struct window *w
6988 = set_window_scroll_bars (decode_live_window (window),
6989 width, vertical_type, horizontal_type);
6990 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6994 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6995 0, 1, 0,
6996 doc: /* Get width and type of scroll bars of window WINDOW.
6997 WINDOW must be a live window and defaults to the selected one.
6999 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
7000 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
7001 value. */)
7002 (Lisp_Object window)
7004 struct window *w = decode_live_window (window);
7006 return list4 (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (w)),
7007 make_number (WINDOW_SCROLL_BAR_COLS (w)),
7008 w->vertical_scroll_bar_type, Qnil);
7013 /***********************************************************************
7014 Smooth scrolling
7015 ***********************************************************************/
7017 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7018 doc: /* Return the amount by which WINDOW is scrolled vertically.
7019 If WINDOW is omitted or nil, it defaults to the selected window.
7020 Normally, value is a multiple of the canonical character height of WINDOW;
7021 optional second arg PIXELS-P means value is measured in pixels. */)
7022 (Lisp_Object window, Lisp_Object pixels_p)
7024 Lisp_Object result;
7025 struct window *w = decode_live_window (window);
7026 struct frame *f = XFRAME (w->frame);
7028 if (FRAME_WINDOW_P (f))
7029 result = (NILP (pixels_p)
7030 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7031 : make_number (-w->vscroll));
7032 else
7033 result = make_number (0);
7034 return result;
7038 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7039 2, 3, 0,
7040 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7041 WINDOW nil means use the selected window. Normally, VSCROLL is a
7042 non-negative multiple of the canonical character height of WINDOW;
7043 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7044 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7045 corresponds to an integral number of pixels. The return value is the
7046 result of this rounding.
7047 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7048 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7050 struct window *w = decode_live_window (window);
7051 struct frame *f = XFRAME (w->frame);
7053 CHECK_NUMBER_OR_FLOAT (vscroll);
7055 if (FRAME_WINDOW_P (f))
7057 int old_dy = w->vscroll;
7059 w->vscroll = - (NILP (pixels_p)
7060 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7061 : XFLOATINT (vscroll));
7062 w->vscroll = min (w->vscroll, 0);
7064 if (w->vscroll != old_dy)
7066 /* Adjust glyph matrix of the frame if the virtual display
7067 area becomes larger than before. */
7068 if (w->vscroll < 0 && w->vscroll < old_dy)
7069 adjust_frame_glyphs (f);
7071 /* Prevent redisplay shortcuts. */
7072 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
7076 return Fwindow_vscroll (window, pixels_p);
7080 /* Call FN for all leaf windows on frame F. FN is called with the
7081 first argument being a pointer to the leaf window, and with
7082 additional argument USER_DATA. Stops when FN returns 0. */
7084 static void
7085 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
7086 void *user_data)
7088 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7089 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7090 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7094 /* Helper function for foreach_window. Call FN for all leaf windows
7095 reachable from W. FN is called with the first argument being a
7096 pointer to the leaf window, and with additional argument USER_DATA.
7097 Stop when FN returns 0. Value is 0 if stopped by FN. */
7099 static int
7100 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7102 int cont;
7104 for (cont = 1; w && cont;)
7106 if (WINDOWP (w->contents))
7107 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7108 else
7109 cont = fn (w, user_data);
7111 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7114 return cont;
7117 /***********************************************************************
7118 Initialization
7119 ***********************************************************************/
7121 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7122 describe the same state of affairs. This is used by Fequal.
7124 IGNORE_POSITIONS means ignore non-matching scroll positions
7125 and the like.
7127 This ignores a couple of things like the dedication status of
7128 window, combination_limit and the like. This might have to be
7129 fixed. */
7131 bool
7132 compare_window_configurations (Lisp_Object configuration1,
7133 Lisp_Object configuration2,
7134 bool ignore_positions)
7136 register struct save_window_data *d1, *d2;
7137 struct Lisp_Vector *sws1, *sws2;
7138 ptrdiff_t i;
7140 CHECK_WINDOW_CONFIGURATION (configuration1);
7141 CHECK_WINDOW_CONFIGURATION (configuration2);
7143 d1 = (struct save_window_data *) XVECTOR (configuration1);
7144 d2 = (struct save_window_data *) XVECTOR (configuration2);
7145 sws1 = XVECTOR (d1->saved_windows);
7146 sws2 = XVECTOR (d2->saved_windows);
7148 /* Frame settings must match. */
7149 if (d1->frame_cols != d2->frame_cols
7150 || d1->frame_lines != d2->frame_lines
7151 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7152 || !EQ (d1->selected_frame, d2->selected_frame)
7153 || !EQ (d1->current_buffer, d2->current_buffer)
7154 || (!ignore_positions
7155 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7156 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7157 || !EQ (d1->focus_frame, d2->focus_frame)
7158 /* Verify that the two configurations have the same number of windows. */
7159 || sws1->header.size != sws2->header.size)
7160 return 0;
7162 for (i = 0; i < sws1->header.size; i++)
7164 struct saved_window *sw1, *sw2;
7166 sw1 = SAVED_WINDOW_N (sws1, i);
7167 sw2 = SAVED_WINDOW_N (sws2, i);
7169 if (
7170 /* The "current" windows in the two configurations must
7171 correspond to each other. */
7172 EQ (d1->current_window, sw1->window)
7173 != EQ (d2->current_window, sw2->window)
7174 /* Windows' buffers must match. */
7175 || !EQ (sw1->buffer, sw2->buffer)
7176 || !EQ (sw1->pixel_left, sw2->pixel_left)
7177 || !EQ (sw1->pixel_top, sw2->pixel_top)
7178 || !EQ (sw1->pixel_height, sw2->pixel_height)
7179 || !EQ (sw1->pixel_width, sw2->pixel_width)
7180 || !EQ (sw1->left_col, sw2->left_col)
7181 || !EQ (sw1->top_line, sw2->top_line)
7182 || !EQ (sw1->total_cols, sw2->total_cols)
7183 || !EQ (sw1->total_lines, sw2->total_lines)
7184 || !EQ (sw1->display_table, sw2->display_table)
7185 /* The next two disjuncts check the window structure for
7186 equality. */
7187 || !EQ (sw1->parent, sw2->parent)
7188 || !EQ (sw1->prev, sw2->prev)
7189 || (!ignore_positions
7190 && (!EQ (sw1->hscroll, sw2->hscroll)
7191 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7192 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7193 || NILP (Fequal (sw1->start, sw2->start))
7194 || NILP (Fequal (sw1->pointm, sw2->pointm))
7195 || NILP (Fequal (sw1->mark, sw2->mark))))
7196 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7197 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7198 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7199 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7200 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7201 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7202 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7203 return 0;
7206 return 1;
7209 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7210 Scompare_window_configurations, 2, 2, 0,
7211 doc: /* Compare two window configurations as regards the structure of windows.
7212 This function ignores details such as the values of point and mark
7213 and scrolling positions. */)
7214 (Lisp_Object x, Lisp_Object y)
7216 if (compare_window_configurations (x, y, 1))
7217 return Qt;
7218 return Qnil;
7221 void
7222 init_window_once (void)
7224 struct frame *f = make_initial_frame ();
7225 XSETFRAME (selected_frame, f);
7226 Vterminal_frame = selected_frame;
7227 minibuf_window = f->minibuffer_window;
7228 selected_window = f->selected_window;
7230 window_initialized = 1;
7233 void
7234 init_window (void)
7236 Vwindow_list = Qnil;
7239 void
7240 syms_of_window (void)
7242 DEFSYM (Qscroll_up, "scroll-up");
7243 DEFSYM (Qscroll_down, "scroll-down");
7244 DEFSYM (Qscroll_command, "scroll-command");
7246 Fput (Qscroll_up, Qscroll_command, Qt);
7247 Fput (Qscroll_down, Qscroll_command, Qt);
7249 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7250 DEFSYM (Qwindowp, "windowp");
7251 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7252 DEFSYM (Qwindow_live_p, "window-live-p");
7253 DEFSYM (Qwindow_valid_p, "window-valid-p");
7254 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7255 DEFSYM (Qdelete_window, "delete-window");
7256 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7257 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7258 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7259 DEFSYM (Qsafe, "safe");
7260 DEFSYM (Qdisplay_buffer, "display-buffer");
7261 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7262 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7263 DEFSYM (Qget_mru_window, "get-mru-window");
7264 DEFSYM (Qwindow_size, "window-size");
7265 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7266 DEFSYM (Qabove, "above");
7267 DEFSYM (Qbelow, "below");
7268 DEFSYM (Qclone_of, "clone-of");
7269 DEFSYM (Qfloor, "floor");
7270 DEFSYM (Qceiling, "ceiling");
7272 staticpro (&Vwindow_list);
7274 minibuf_selected_window = Qnil;
7275 staticpro (&minibuf_selected_window);
7277 window_scroll_pixel_based_preserve_x = -1;
7278 window_scroll_pixel_based_preserve_y = -1;
7279 window_scroll_preserve_hpos = -1;
7280 window_scroll_preserve_vpos = -1;
7282 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7283 doc: /* Non-nil means call as function to display a help buffer.
7284 The function is called with one argument, the buffer to be displayed.
7285 Used by `with-output-to-temp-buffer'.
7286 If this function is used, then it must do the entire job of showing
7287 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7288 Vtemp_buffer_show_function = Qnil;
7290 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7291 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7292 Vminibuf_scroll_window = Qnil;
7294 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7295 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7296 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7297 is displayed in the `mode-line' face. */);
7298 mode_line_in_non_selected_windows = 1;
7300 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7301 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7302 Vother_window_scroll_buffer = Qnil;
7304 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7305 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7306 auto_window_vscroll_p = 1;
7308 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7309 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7310 next_screen_context_lines = 2;
7312 DEFVAR_LISP ("scroll-preserve-screen-position",
7313 Vscroll_preserve_screen_position,
7314 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7315 A value of nil means point does not keep its screen position except
7316 at the scroll margin or window boundary respectively.
7317 A value of t means point keeps its screen position if the scroll
7318 command moved it vertically out of the window, e.g. when scrolling
7319 by full screens.
7320 Any other value means point always keeps its screen position.
7321 Scroll commands should have the `scroll-command' property
7322 on their symbols to be controlled by this variable. */);
7323 Vscroll_preserve_screen_position = Qnil;
7325 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7326 doc: /* Type of marker to use for `window-point'. */);
7327 Vwindow_point_insertion_type = Qnil;
7328 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7330 DEFVAR_LISP ("window-configuration-change-hook",
7331 Vwindow_configuration_change_hook,
7332 doc: /* Functions to call when window configuration changes.
7333 The buffer-local part is run once per window, with the relevant window
7334 selected; while the global part is run only once for the modified frame,
7335 with the relevant frame selected. */);
7336 Vwindow_configuration_change_hook = Qnil;
7338 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7339 doc: /* Non-nil means `recenter' redraws entire frame.
7340 If this option is non-nil, then the `recenter' command with a nil
7341 argument will redraw the entire frame; the special value `tty' causes
7342 the frame to be redrawn only if it is a tty frame. */);
7343 Vrecenter_redisplay = Qtty;
7345 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7346 doc: /* If t, resize window combinations proportionally.
7347 If this variable is nil, splitting a window gets the entire screen space
7348 for displaying the new window from the window to split. Deleting and
7349 resizing a window preferably resizes one adjacent window only.
7351 If this variable is t, splitting a window tries to get the space
7352 proportionally from all windows in the same combination. This also
7353 allows to split a window that is otherwise too small or of fixed size.
7354 Resizing and deleting a window proportionally resize all windows in the
7355 same combination.
7357 Other values are reserved for future use.
7359 This variable takes no effect if the variable `window-combination-limit' is
7360 non-nil. */);
7361 Vwindow_combination_resize = Qnil;
7363 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7364 doc: /* If non-nil, splitting a window makes a new parent window.
7365 The following values are recognized:
7367 nil means splitting a window will create a new parent window only if the
7368 window has no parent window or the window shall become part of a
7369 combination orthogonal to the one it is part of.
7371 `window-size' means that splitting a window for displaying a buffer
7372 makes a new parent window provided `display-buffer' is supposed to
7373 explicitly set the window's size due to the presence of a
7374 `window-height' or `window-width' entry in the alist used by
7375 `display-buffer'. Otherwise, this value is handled like nil.
7377 `temp-buffer' means that splitting a window for displaying a temporary
7378 buffer always makes a new parent window. Otherwise, this value is
7379 handled like nil.
7381 `display-buffer' means that splitting a window for displaying a buffer
7382 always makes a new parent window. Since temporary buffers are
7383 displayed by the function `display-buffer', this value is stronger
7384 than `temp-buffer'. Splitting a window for other purpose makes a
7385 new parent window only if needed.
7387 t means that splitting a window always creates a new parent window. If
7388 all splits behave this way, each frame's window tree is a binary
7389 tree and every window but the frame's root window has exactly one
7390 sibling.
7392 Other values are reserved for future use. */);
7393 Vwindow_combination_limit = Qwindow_size;
7395 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7396 doc: /* Alist of persistent window parameters.
7397 This alist specifies which window parameters shall get saved by
7398 `current-window-configuration' and `window-state-get' and subsequently
7399 restored to their previous values by `set-window-configuration' and
7400 `window-state-put'.
7402 The car of each entry of this alist is the symbol specifying the
7403 parameter. The cdr is one of the following:
7405 nil means the parameter is neither saved by `window-state-get' nor by
7406 `current-window-configuration'.
7408 t means the parameter is saved by `current-window-configuration' and,
7409 provided its WRITABLE argument is nil, by `window-state-get'.
7411 The symbol `writable' means the parameter is saved unconditionally by
7412 both `current-window-configuration' and `window-state-get'. Do not use
7413 this value for parameters without read syntax (like windows or frames).
7415 Parameters not saved by `current-window-configuration' or
7416 `window-state-get' are left alone by `set-window-configuration'
7417 respectively are not installed by `window-state-put'. */);
7418 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7420 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7421 doc: /* Non-nil means resize windows pixelwise.
7422 This currently affects the functions: `split-window', `maximize-window',
7423 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7424 all functions that symmetrically resize a parent window.
7426 Note that when a frame's pixel size is not a multiple of the
7427 frame's character size, at least one window may get resized
7428 pixelwise even if this option is nil. */);
7429 window_resize_pixelwise = 0;
7431 defsubr (&Sselected_window);
7432 defsubr (&Sminibuffer_window);
7433 defsubr (&Swindow_minibuffer_p);
7434 defsubr (&Swindowp);
7435 defsubr (&Swindow_valid_p);
7436 defsubr (&Swindow_live_p);
7437 defsubr (&Swindow_frame);
7438 defsubr (&Sframe_root_window);
7439 defsubr (&Sframe_first_window);
7440 defsubr (&Sframe_selected_window);
7441 defsubr (&Sset_frame_selected_window);
7442 defsubr (&Spos_visible_in_window_p);
7443 defsubr (&Swindow_line_height);
7444 defsubr (&Swindow_buffer);
7445 defsubr (&Swindow_parent);
7446 defsubr (&Swindow_top_child);
7447 defsubr (&Swindow_left_child);
7448 defsubr (&Swindow_next_sibling);
7449 defsubr (&Swindow_prev_sibling);
7450 defsubr (&Swindow_combination_limit);
7451 defsubr (&Sset_window_combination_limit);
7452 defsubr (&Swindow_use_time);
7453 defsubr (&Swindow_pixel_width);
7454 defsubr (&Swindow_pixel_height);
7455 defsubr (&Swindow_total_width);
7456 defsubr (&Swindow_total_height);
7457 defsubr (&Swindow_normal_size);
7458 defsubr (&Swindow_new_pixel);
7459 defsubr (&Swindow_new_total);
7460 defsubr (&Swindow_new_normal);
7461 defsubr (&Swindow_pixel_left);
7462 defsubr (&Swindow_pixel_top);
7463 defsubr (&Swindow_left_column);
7464 defsubr (&Swindow_top_line);
7465 defsubr (&Sset_window_new_pixel);
7466 defsubr (&Sset_window_new_total);
7467 defsubr (&Sset_window_new_normal);
7468 defsubr (&Swindow_resize_apply);
7469 defsubr (&Swindow_resize_apply_total);
7470 defsubr (&Swindow_body_height);
7471 defsubr (&Swindow_body_width);
7472 defsubr (&Swindow_hscroll);
7473 defsubr (&Sset_window_hscroll);
7474 defsubr (&Swindow_redisplay_end_trigger);
7475 defsubr (&Sset_window_redisplay_end_trigger);
7476 defsubr (&Swindow_edges);
7477 defsubr (&Swindow_pixel_edges);
7478 defsubr (&Swindow_absolute_pixel_edges);
7479 defsubr (&Swindow_mode_line_height);
7480 defsubr (&Swindow_header_line_height);
7481 defsubr (&Swindow_right_divider_width);
7482 defsubr (&Swindow_bottom_divider_width);
7483 defsubr (&Swindow_scroll_bar_width);
7484 defsubr (&Swindow_inside_edges);
7485 defsubr (&Swindow_inside_pixel_edges);
7486 defsubr (&Swindow_inside_absolute_pixel_edges);
7487 defsubr (&Scoordinates_in_window_p);
7488 defsubr (&Swindow_at);
7489 defsubr (&Swindow_point);
7490 defsubr (&Swindow_start);
7491 defsubr (&Swindow_end);
7492 defsubr (&Sset_window_point);
7493 defsubr (&Sset_window_start);
7494 defsubr (&Swindow_dedicated_p);
7495 defsubr (&Sset_window_dedicated_p);
7496 defsubr (&Swindow_display_table);
7497 defsubr (&Sset_window_display_table);
7498 defsubr (&Snext_window);
7499 defsubr (&Sprevious_window);
7500 defsubr (&Sget_buffer_window);
7501 defsubr (&Sdelete_other_windows_internal);
7502 defsubr (&Sdelete_window_internal);
7503 defsubr (&Sresize_mini_window_internal);
7504 defsubr (&Sset_window_buffer);
7505 defsubr (&Srun_window_configuration_change_hook);
7506 defsubr (&Srun_window_scroll_functions);
7507 defsubr (&Sselect_window);
7508 defsubr (&Sforce_window_update);
7509 defsubr (&Ssplit_window_internal);
7510 defsubr (&Sscroll_up);
7511 defsubr (&Sscroll_down);
7512 defsubr (&Sscroll_left);
7513 defsubr (&Sscroll_right);
7514 defsubr (&Sother_window_for_scrolling);
7515 defsubr (&Sscroll_other_window);
7516 defsubr (&Sminibuffer_selected_window);
7517 defsubr (&Srecenter);
7518 defsubr (&Swindow_text_width);
7519 defsubr (&Swindow_text_height);
7520 defsubr (&Smove_to_window_line);
7521 defsubr (&Swindow_configuration_p);
7522 defsubr (&Swindow_configuration_frame);
7523 defsubr (&Sset_window_configuration);
7524 defsubr (&Scurrent_window_configuration);
7525 defsubr (&Sset_window_margins);
7526 defsubr (&Swindow_margins);
7527 defsubr (&Sset_window_fringes);
7528 defsubr (&Swindow_fringes);
7529 defsubr (&Sset_window_scroll_bars);
7530 defsubr (&Swindow_scroll_bars);
7531 defsubr (&Swindow_vscroll);
7532 defsubr (&Sset_window_vscroll);
7533 defsubr (&Scompare_window_configurations);
7534 defsubr (&Swindow_list);
7535 defsubr (&Swindow_list_1);
7536 defsubr (&Swindow_prev_buffers);
7537 defsubr (&Sset_window_prev_buffers);
7538 defsubr (&Swindow_next_buffers);
7539 defsubr (&Sset_window_next_buffers);
7540 defsubr (&Swindow_parameters);
7541 defsubr (&Swindow_parameter);
7542 defsubr (&Sset_window_parameter);
7545 void
7546 keys_of_window (void)
7548 initial_define_key (control_x_map, '<', "scroll-left");
7549 initial_define_key (control_x_map, '>', "scroll-right");
7551 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7552 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7553 initial_define_key (meta_map, 'v', "scroll-down-command");