Doc tweaks re multi-monitor
[emacs.git] / src / window.c
blob2210d5bc86892b6de1d8de05f68d52c6d326ef3c
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2014 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
43 #ifdef MSDOS
44 #include "msdos.h"
45 #endif
47 Lisp_Object Qwindowp, Qwindow_live_p;
48 static Lisp_Object Qwindow_valid_p;
49 static Lisp_Object Qwindow_configuration_p;
50 static Lisp_Object Qrecord_window_buffer;
51 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
52 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
53 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
54 static Lisp_Object Qwindow_pixel_to_total;
55 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
56 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
57 static Lisp_Object Qfloor, Qceiling;
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. */)
977 (Lisp_Object window, Lisp_Object pixelwise)
979 return make_number (window_body_width (decode_live_window (window),
980 NILP (pixelwise) ? 0 : 1));
983 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
984 Swindow_mode_line_height, 0, 1, 0,
985 doc: /* Return the height in pixels of WINDOW's mode-line.
986 WINDOW must be a live window and defaults to the selected one. */)
987 (Lisp_Object window)
989 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
992 DEFUN ("window-header-line-height", Fwindow_header_line_height,
993 Swindow_header_line_height, 0, 1, 0,
994 doc: /* Return the height in pixels of WINDOW's header-line.
995 WINDOW must be a live window and defaults to the selected one. */)
996 (Lisp_Object window)
998 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
1001 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
1002 Swindow_right_divider_width, 0, 1, 0,
1003 doc: /* Return the width in pixels of WINDOW's right divider.
1004 WINDOW must be a live window and defaults to the selected one. */)
1005 (Lisp_Object window)
1007 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
1010 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
1011 Swindow_bottom_divider_width, 0, 1, 0,
1012 doc: /* Return the width in pixels of WINDOW's bottom divider.
1013 WINDOW must be a live window and defaults to the selected one. */)
1014 (Lisp_Object window)
1016 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
1019 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
1020 Swindow_scroll_bar_width, 0, 1, 0,
1021 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
1022 WINDOW must be a live window and defaults to the selected one. */)
1023 (Lisp_Object window)
1025 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
1028 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
1029 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
1030 WINDOW must be a live window and defaults to the selected one. */)
1031 (Lisp_Object window)
1033 return make_number (decode_live_window (window)->hscroll);
1036 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
1037 range, returning the new amount as a fixnum. */
1038 static Lisp_Object
1039 set_window_hscroll (struct window *w, EMACS_INT hscroll)
1041 /* Horizontal scrolling has problems with large scroll amounts.
1042 It's too slow with long lines, and even with small lines the
1043 display can be messed up. For now, though, impose only the limits
1044 required by the internal representation: horizontal scrolling must
1045 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1046 (since it's stored in a ptrdiff_t). */
1047 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1048 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1050 /* Prevent redisplay shortcuts when changing the hscroll. */
1051 if (w->hscroll != new_hscroll)
1052 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1054 w->hscroll = new_hscroll;
1055 return make_number (new_hscroll);
1058 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1059 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1060 WINDOW must be a live window and defaults to the selected one.
1061 Clip the number to a reasonable value if out of range.
1062 Return the new number. NCOL should be zero or positive.
1064 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1065 window so that the location of point moves off-window. */)
1066 (Lisp_Object window, Lisp_Object ncol)
1068 CHECK_NUMBER (ncol);
1069 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1072 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1073 Swindow_redisplay_end_trigger, 0, 1, 0,
1074 doc: /* Return WINDOW's redisplay end trigger value.
1075 WINDOW must be a live window and defaults to the selected one.
1076 See `set-window-redisplay-end-trigger' for more information. */)
1077 (Lisp_Object window)
1079 return decode_live_window (window)->redisplay_end_trigger;
1082 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1083 Sset_window_redisplay_end_trigger, 2, 2, 0,
1084 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1085 WINDOW must be a live window and defaults to the selected one. VALUE
1086 should be a buffer position (typically a marker) or nil. If it is a
1087 buffer position, then if redisplay in WINDOW reaches a position beyond
1088 VALUE, the functions in `redisplay-end-trigger-functions' are called
1089 with two arguments: WINDOW, and the end trigger value. Afterwards the
1090 end-trigger value is reset to nil. */)
1091 (register Lisp_Object window, Lisp_Object value)
1093 wset_redisplay_end_trigger (decode_live_window (window), value);
1094 return value;
1097 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1098 doc: /* Return a list of the edge coordinates of WINDOW.
1099 WINDOW must be a valid window and defaults to the selected one.
1101 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1102 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1103 0 at top left corner of frame.
1105 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1106 is one more than the bottommost row occupied by WINDOW. The edges
1107 include the space used by WINDOW's scroll bar, display margins, fringes,
1108 header line, and/or mode line. For the edges of just the text area, use
1109 `window-inside-edges'. */)
1110 (Lisp_Object window)
1112 register struct window *w = decode_valid_window (window);
1114 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1115 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1118 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1119 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1120 WINDOW must be a valid window and defaults to the selected one.
1122 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1123 0, 0 at the top left corner of the frame.
1125 RIGHT is one more than the rightmost x position occupied by WINDOW.
1126 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1127 The pixel edges include the space used by WINDOW's scroll bar, display
1128 margins, fringes, header line, and/or mode line. For the pixel edges
1129 of just the text area, use `window-inside-pixel-edges'. */)
1130 (Lisp_Object window)
1132 register struct window *w = decode_valid_window (window);
1134 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1135 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1138 static void
1139 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1141 struct frame *f = XFRAME (w->frame);
1142 *add_y = f->top_pos;
1143 #ifdef FRAME_MENUBAR_HEIGHT
1144 *add_y += FRAME_MENUBAR_HEIGHT (f);
1145 #endif
1146 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1147 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1148 #elif defined (FRAME_TOOLBAR_HEIGHT)
1149 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1150 #endif
1151 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1152 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1153 #endif
1154 *add_x = f->left_pos;
1155 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1156 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1157 #endif
1160 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1161 Swindow_absolute_pixel_edges, 0, 1, 0,
1162 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1163 WINDOW must be a valid window and defaults to the selected one.
1165 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1166 0, 0 at the top left corner of the display.
1168 RIGHT is one more than the rightmost x position occupied by WINDOW.
1169 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1170 The pixel edges include the space used by WINDOW's scroll bar, display
1171 margins, fringes, header line, and/or mode line. For the pixel edges
1172 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1173 (Lisp_Object window)
1175 register struct window *w = decode_valid_window (window);
1176 int add_x, add_y;
1178 calc_absolute_offset (w, &add_x, &add_y);
1180 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1181 WINDOW_TOP_EDGE_Y (w) + add_y,
1182 WINDOW_RIGHT_EDGE_X (w) + add_x,
1183 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1186 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1187 doc: /* Return a list of the edge coordinates of WINDOW.
1188 WINDOW must be a live window and defaults to the selected one.
1190 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1191 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1192 0 at top left corner of frame.
1194 RIGHT is one more than the rightmost column of WINDOW's text area.
1195 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1196 inside edges do not include the space used by the WINDOW's scroll bar,
1197 display margins, fringes, header line, and/or mode line. */)
1198 (Lisp_Object window)
1200 register struct window *w = decode_live_window (window);
1202 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1203 + WINDOW_LEFT_MARGIN_COLS (w)
1204 + WINDOW_LEFT_FRINGE_COLS (w)),
1205 (WINDOW_TOP_EDGE_LINE (w)
1206 + WINDOW_HEADER_LINE_LINES (w)),
1207 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1208 - WINDOW_RIGHT_MARGIN_COLS (w)
1209 - WINDOW_RIGHT_FRINGE_COLS (w)),
1210 (WINDOW_BOTTOM_EDGE_LINE (w)
1211 - WINDOW_MODE_LINE_LINES (w)));
1214 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1215 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1216 WINDOW must be a live window and defaults to the selected one.
1218 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1219 (0,0) at the top left corner of the frame's window area.
1221 RIGHT is one more than the rightmost x position of WINDOW's text area.
1222 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1223 The inside edges do not include the space used by WINDOW's scroll bar,
1224 display margins, fringes, header line, and/or mode line. */)
1225 (Lisp_Object window)
1227 register struct window *w = decode_live_window (window);
1229 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1230 + WINDOW_LEFT_MARGIN_WIDTH (w)
1231 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1232 (WINDOW_TOP_EDGE_Y (w)
1233 + WINDOW_HEADER_LINE_HEIGHT (w)),
1234 (WINDOW_BOX_RIGHT_EDGE_X (w)
1235 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1236 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1237 (WINDOW_BOTTOM_EDGE_Y (w)
1238 - WINDOW_MODE_LINE_HEIGHT (w)));
1241 DEFUN ("window-inside-absolute-pixel-edges",
1242 Fwindow_inside_absolute_pixel_edges,
1243 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1244 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1245 WINDOW must be a live window and defaults to the selected one.
1247 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1248 (0,0) at the top left corner of the frame's window area.
1250 RIGHT is one more than the rightmost x position of WINDOW's text area.
1251 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1252 The inside edges do not include the space used by WINDOW's scroll bar,
1253 display margins, fringes, header line, and/or mode line. */)
1254 (Lisp_Object window)
1256 register struct window *w = decode_live_window (window);
1257 int add_x, add_y;
1259 calc_absolute_offset (w, &add_x, &add_y);
1261 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1262 + WINDOW_LEFT_MARGIN_WIDTH (w)
1263 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1264 (WINDOW_TOP_EDGE_Y (w)
1265 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1266 (WINDOW_BOX_RIGHT_EDGE_X (w)
1267 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1268 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1269 (WINDOW_BOTTOM_EDGE_Y (w)
1270 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1273 /* Test if the character at column X, row Y is within window W.
1274 If it is not, return ON_NOTHING;
1275 if it is on the window's vertical divider, return
1276 ON_RIGHT_DIVIDER;
1277 if it is on the window's horizontal divider, return
1278 ON_BOTTOM_DIVIDER;
1279 if it is in the window's text area, return ON_TEXT;
1280 if it is on the window's modeline, return ON_MODE_LINE;
1281 if it is on the border between the window and its right sibling,
1282 return ON_VERTICAL_BORDER;
1283 if it is on a scroll bar, return ON_SCROLL_BAR;
1284 if it is on the window's top line, return ON_HEADER_LINE;
1285 if it is in left or right fringe of the window,
1286 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1287 if it is in the marginal area to the left/right of the window,
1288 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1290 X and Y are frame relative pixel coordinates. */
1292 static enum window_part
1293 coordinates_in_window (register struct window *w, int x, int y)
1295 struct frame *f = XFRAME (WINDOW_FRAME (w));
1296 enum window_part part;
1297 int ux = FRAME_COLUMN_WIDTH (f);
1298 int left_x = WINDOW_LEFT_EDGE_X (w);
1299 int right_x = WINDOW_RIGHT_EDGE_X (w);
1300 int top_y = WINDOW_TOP_EDGE_Y (w);
1301 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1302 /* The width of the area where the vertical line can be dragged.
1303 (Between mode lines for instance. */
1304 int grabbable_width = ux;
1305 int lmargin_width, rmargin_width, text_left, text_right;
1307 /* Outside any interesting row or column? */
1308 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1309 return ON_NOTHING;
1311 /* On the horizontal window divider (which prevails the vertical
1312 divider)? */
1313 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w) > 0
1314 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1315 && y <= bottom_y)
1316 return ON_BOTTOM_DIVIDER;
1317 /* On vertical window divider? */
1318 else if (!WINDOW_RIGHTMOST_P (w)
1319 && WINDOW_RIGHT_DIVIDER_WIDTH (w) > 0
1320 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1321 && x <= right_x)
1322 return ON_RIGHT_DIVIDER;
1323 /* On the mode or header line? */
1324 else if ((WINDOW_WANTS_MODELINE_P (w)
1325 && y >= (bottom_y
1326 - CURRENT_MODE_LINE_HEIGHT (w)
1327 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1328 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1329 && (part = ON_MODE_LINE))
1330 || (WINDOW_WANTS_HEADER_LINE_P (w)
1331 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1332 && (part = ON_HEADER_LINE)))
1334 /* If it's under/over the scroll bar portion of the mode/header
1335 line, say it's on the vertical line. That's to be able to
1336 resize windows horizontally in case we're using toolkit scroll
1337 bars. Note: If scrollbars are on the left, the window that
1338 must be eventually resized is that on the left of WINDOW. */
1339 if ((WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0)
1340 && ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1341 && !WINDOW_LEFTMOST_P (w)
1342 && eabs (x - left_x) < grabbable_width)
1343 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1344 && !WINDOW_RIGHTMOST_P (w)
1345 && eabs (x - right_x) < grabbable_width)))
1346 return ON_VERTICAL_BORDER;
1347 else
1348 return part;
1351 /* In what's below, we subtract 1 when computing right_x because we
1352 want the rightmost pixel, which is given by left_pixel+width-1. */
1353 if (w->pseudo_window_p)
1355 left_x = 0;
1356 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1358 else
1360 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1361 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1364 /* Outside any interesting column? */
1365 if (x < left_x || x > right_x)
1366 return ON_SCROLL_BAR;
1368 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1369 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1371 text_left = window_box_left (w, TEXT_AREA);
1372 text_right = text_left + window_box_width (w, TEXT_AREA);
1374 if (FRAME_WINDOW_P (f))
1376 if (!w->pseudo_window_p
1377 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1378 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1379 && !WINDOW_RIGHTMOST_P (w)
1380 && (eabs (x - right_x) < grabbable_width))
1381 return ON_VERTICAL_BORDER;
1383 /* Need to say "x > right_x" rather than >=, since on character
1384 terminals, the vertical line's x coordinate is right_x. */
1385 else if (!w->pseudo_window_p
1386 && WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
1387 && !WINDOW_RIGHTMOST_P (w)
1388 /* Why check ux if we are not the rightmost window? Also
1389 shouldn't a pseudo window always be rightmost? */
1390 && x > right_x - ux)
1391 return ON_VERTICAL_BORDER;
1393 if (x < text_left)
1395 if (lmargin_width > 0
1396 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1397 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1398 : (x < left_x + lmargin_width)))
1399 return ON_LEFT_MARGIN;
1400 else
1401 return ON_LEFT_FRINGE;
1404 if (x >= text_right)
1406 if (rmargin_width > 0
1407 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1408 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1409 : (x >= right_x - rmargin_width)))
1410 return ON_RIGHT_MARGIN;
1411 else
1412 return ON_RIGHT_FRINGE;
1415 /* Everything special ruled out - must be on text area */
1416 return ON_TEXT;
1419 /* Take X is the frame-relative pixel x-coordinate, and return the
1420 x-coordinate relative to part PART of window W. */
1422 window_relative_x_coord (struct window *w, enum window_part part, int x)
1424 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1426 switch (part)
1428 case ON_TEXT:
1429 return x - window_box_left (w, TEXT_AREA);
1431 case ON_HEADER_LINE:
1432 case ON_MODE_LINE:
1433 case ON_LEFT_FRINGE:
1434 return x - left_x;
1436 case ON_RIGHT_FRINGE:
1437 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1439 case ON_LEFT_MARGIN:
1440 return (x - left_x
1441 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1442 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1444 case ON_RIGHT_MARGIN:
1445 return (x + 1
1446 - ((w->pseudo_window_p)
1447 ? WINDOW_PIXEL_WIDTH (w)
1448 : WINDOW_BOX_RIGHT_EDGE_X (w))
1449 + window_box_width (w, RIGHT_MARGIN_AREA)
1450 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1451 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1454 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1455 return 0;
1459 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1460 Scoordinates_in_window_p, 2, 2, 0,
1461 doc: /* Return non-nil if COORDINATES are in WINDOW.
1462 WINDOW must be a live window and defaults to the selected one.
1463 COORDINATES is a cons of the form (X . Y), X and Y being distances
1464 measured in characters from the upper-left corner of the frame.
1465 \(0 . 0) denotes the character in the upper left corner of the
1466 frame.
1467 If COORDINATES are in the text portion of WINDOW,
1468 the coordinates relative to the window are returned.
1469 If they are in the bottom divider of WINDOW, `bottom-divider' is returned.
1470 If they are in the right divider of WINDOW, `right-divider' is returned.
1471 If they are in the mode line of WINDOW, `mode-line' is returned.
1472 If they are in the header line of WINDOW, `header-line' is returned.
1473 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1474 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1475 If they are on the border between WINDOW and its right sibling,
1476 `vertical-line' is returned.
1477 If they are in the windows's left or right marginal areas, `left-margin'\n\
1478 or `right-margin' is returned. */)
1479 (register Lisp_Object coordinates, Lisp_Object window)
1481 struct window *w;
1482 struct frame *f;
1483 int x, y;
1484 Lisp_Object lx, ly;
1486 w = decode_live_window (window);
1487 f = XFRAME (w->frame);
1488 CHECK_CONS (coordinates);
1489 lx = Fcar (coordinates);
1490 ly = Fcdr (coordinates);
1491 CHECK_NUMBER_OR_FLOAT (lx);
1492 CHECK_NUMBER_OR_FLOAT (ly);
1493 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1494 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1496 switch (coordinates_in_window (w, x, y))
1498 case ON_NOTHING:
1499 return Qnil;
1501 case ON_TEXT:
1502 /* Convert X and Y to window relative pixel coordinates, and
1503 return the canonical char units. */
1504 x -= window_box_left (w, TEXT_AREA);
1505 y -= WINDOW_TOP_EDGE_Y (w);
1506 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1507 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1509 case ON_MODE_LINE:
1510 return Qmode_line;
1512 case ON_VERTICAL_BORDER:
1513 return Qvertical_line;
1515 case ON_HEADER_LINE:
1516 return Qheader_line;
1518 case ON_LEFT_FRINGE:
1519 return Qleft_fringe;
1521 case ON_RIGHT_FRINGE:
1522 return Qright_fringe;
1524 case ON_LEFT_MARGIN:
1525 return Qleft_margin;
1527 case ON_RIGHT_MARGIN:
1528 return Qright_margin;
1530 case ON_SCROLL_BAR:
1531 /* Historically we are supposed to return nil in this case. */
1532 return Qnil;
1534 case ON_RIGHT_DIVIDER:
1535 return Qright_divider;
1537 case ON_BOTTOM_DIVIDER:
1538 return Qbottom_divider;
1540 default:
1541 emacs_abort ();
1546 /* Callback for foreach_window, used in window_from_coordinates.
1547 Check if window W contains coordinates specified by USER_DATA which
1548 is actually a pointer to a struct check_window_data CW.
1550 Check if window W contains coordinates *CW->x and *CW->y. If it
1551 does, return W in *CW->window, as Lisp_Object, and return in
1552 *CW->part the part of the window under coordinates *X,*Y. Return
1553 zero from this function to stop iterating over windows. */
1555 struct check_window_data
1557 Lisp_Object *window;
1558 int x, y;
1559 enum window_part *part;
1562 static int
1563 check_window_containing (struct window *w, void *user_data)
1565 struct check_window_data *cw = user_data;
1566 enum window_part found;
1567 int continue_p = 1;
1569 found = coordinates_in_window (w, cw->x, cw->y);
1570 if (found != ON_NOTHING)
1572 *cw->part = found;
1573 XSETWINDOW (*cw->window, w);
1574 continue_p = 0;
1577 return continue_p;
1581 /* Find the window containing frame-relative pixel position X/Y and
1582 return it as a Lisp_Object.
1584 If X, Y is on one of the window's special `window_part' elements,
1585 set *PART to the id of that element.
1587 If there is no window under X, Y return nil and leave *PART
1588 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1590 This function was previously implemented with a loop cycling over
1591 windows with Fnext_window, and starting with the frame's selected
1592 window. It turned out that this doesn't work with an
1593 implementation of next_window using Vwindow_list, because
1594 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1595 tree of F when this function is called asynchronously from
1596 note_mouse_highlight. The original loop didn't terminate in this
1597 case. */
1599 Lisp_Object
1600 window_from_coordinates (struct frame *f, int x, int y,
1601 enum window_part *part, bool tool_bar_p)
1603 Lisp_Object window;
1604 struct check_window_data cw;
1605 enum window_part dummy;
1607 if (part == 0)
1608 part = &dummy;
1610 window = Qnil;
1611 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1612 foreach_window (f, check_window_containing, &cw);
1614 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1615 /* If not found above, see if it's in the tool bar window, if a tool
1616 bar exists. */
1617 if (NILP (window)
1618 && tool_bar_p
1619 && WINDOWP (f->tool_bar_window)
1620 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1621 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1622 != ON_NOTHING))
1624 *part = ON_TEXT;
1625 window = f->tool_bar_window;
1627 #endif
1629 return window;
1632 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1633 doc: /* Return window containing coordinates X and Y on FRAME.
1634 FRAME must be a live frame and defaults to the selected one.
1635 The top left corner of the frame is considered to be row 0,
1636 column 0. */)
1637 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1639 struct frame *f = decode_live_frame (frame);
1641 /* Check that arguments are integers or floats. */
1642 CHECK_NUMBER_OR_FLOAT (x);
1643 CHECK_NUMBER_OR_FLOAT (y);
1645 return window_from_coordinates (f,
1646 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1647 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1648 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1649 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1650 0, 0);
1653 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1654 doc: /* Return current value of point in WINDOW.
1655 WINDOW must be a live window and defaults to the selected one.
1657 For a nonselected window, this is the value point would have if that
1658 window were selected.
1660 Note that, when WINDOW is selected, the value returned is the same as
1661 that returned by `point' for WINDOW's buffer. It would be more strictly
1662 correct to return the top-level value of `point', outside of any
1663 `save-excursion' forms. But that is hard to define. */)
1664 (Lisp_Object window)
1666 register struct window *w = decode_live_window (window);
1668 if (w == XWINDOW (selected_window))
1669 return make_number (BUF_PT (XBUFFER (w->contents)));
1670 else
1671 return Fmarker_position (w->pointm);
1674 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1675 doc: /* Return position at which display currently starts in WINDOW.
1676 WINDOW must be a live window and defaults to the selected one.
1677 This is updated by redisplay or by calling `set-window-start'. */)
1678 (Lisp_Object window)
1680 return Fmarker_position (decode_live_window (window)->start);
1683 /* This is text temporarily removed from the doc string below.
1685 This function returns nil if the position is not currently known.
1686 That happens when redisplay is preempted and doesn't finish.
1687 If in that case you want to compute where the end of the window would
1688 have been if redisplay had finished, do this:
1689 (save-excursion
1690 (goto-char (window-start window))
1691 (vertical-motion (1- (window-height window)) window)
1692 (point))") */
1694 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1695 doc: /* Return position at which display currently ends in WINDOW.
1696 WINDOW must be a live window and defaults to the selected one.
1697 This is updated by redisplay, when it runs to completion.
1698 Simply changing the buffer text or setting `window-start'
1699 does not update this value.
1700 Return nil if there is no recorded value. (This can happen if the
1701 last redisplay of WINDOW was preempted, and did not finish.)
1702 If UPDATE is non-nil, compute the up-to-date position
1703 if it isn't already recorded. */)
1704 (Lisp_Object window, Lisp_Object update)
1706 Lisp_Object value;
1707 struct window *w = decode_live_window (window);
1708 Lisp_Object buf;
1709 struct buffer *b;
1711 buf = w->contents;
1712 CHECK_BUFFER (buf);
1713 b = XBUFFER (buf);
1715 if (! NILP (update)
1716 && (windows_or_buffers_changed
1717 || !w->window_end_valid
1718 || b->clip_changed
1719 || b->prevent_redisplay_optimizations_p
1720 || window_outdated (w))
1721 && !noninteractive)
1723 struct text_pos startp;
1724 struct it it;
1725 struct buffer *old_buffer = NULL;
1726 void *itdata = NULL;
1728 /* Cannot use Fvertical_motion because that function doesn't
1729 cope with variable-height lines. */
1730 if (b != current_buffer)
1732 old_buffer = current_buffer;
1733 set_buffer_internal (b);
1736 /* In case W->start is out of the range, use something
1737 reasonable. This situation occurred when loading a file with
1738 `-l' containing a call to `rmail' with subsequent other
1739 commands. At the end, W->start happened to be BEG, while
1740 rmail had already narrowed the buffer. */
1741 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1743 itdata = bidi_shelve_cache ();
1744 start_display (&it, w, startp);
1745 move_it_vertically (&it, window_box_height (w));
1746 if (it.current_y < it.last_visible_y)
1747 move_it_past_eol (&it);
1748 value = make_number (IT_CHARPOS (it));
1749 bidi_unshelve_cache (itdata, 0);
1751 if (old_buffer)
1752 set_buffer_internal (old_buffer);
1754 else
1755 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1757 return value;
1760 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1761 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1762 WINDOW must be a live window and defaults to the selected one.
1763 Return POS. */)
1764 (Lisp_Object window, Lisp_Object pos)
1766 register struct window *w = decode_live_window (window);
1768 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1770 if (w == XWINDOW (selected_window))
1772 if (XBUFFER (w->contents) == current_buffer)
1773 Fgoto_char (pos);
1774 else
1776 struct buffer *old_buffer = current_buffer;
1778 /* ... but here we want to catch type error before buffer change. */
1779 CHECK_NUMBER_COERCE_MARKER (pos);
1780 set_buffer_internal (XBUFFER (w->contents));
1781 Fgoto_char (pos);
1782 set_buffer_internal (old_buffer);
1785 else
1787 set_marker_restricted (w->pointm, pos, w->contents);
1788 /* We have to make sure that redisplay updates the window to show
1789 the new value of point. */
1790 wset_redisplay (w);
1793 return pos;
1796 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1797 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1798 WINDOW must be a live window and defaults to the selected one. Return
1799 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1800 overriding motion of point in order to display at this exact start. */)
1801 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1803 register struct window *w = decode_live_window (window);
1805 set_marker_restricted (w->start, pos, w->contents);
1806 /* This is not right, but much easier than doing what is right. */
1807 w->start_at_line_beg = 0;
1808 if (NILP (noforce))
1809 w->force_start = 1;
1810 w->update_mode_line = 1;
1811 /* Bug#15957. */
1812 w->window_end_valid = 0;
1813 wset_redisplay (w);
1815 return pos;
1818 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1819 Spos_visible_in_window_p, 0, 3, 0,
1820 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1821 WINDOW must be a live window and defaults to the selected one.
1823 Return nil if that position is scrolled vertically out of view. If a
1824 character is only partially visible, nil is returned, unless the
1825 optional argument PARTIALLY is non-nil. If POS is only out of view
1826 because of horizontal scrolling, return non-nil. If POS is t, it
1827 specifies the position of the last visible glyph in WINDOW. POS
1828 defaults to point in WINDOW; WINDOW defaults to the selected window.
1830 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1831 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1832 where X and Y are the pixel coordinates relative to the top left corner
1833 of the window. The remaining elements are omitted if the character after
1834 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1835 off-window at the top and bottom of the screen line ("row") containing
1836 POS, ROWH is the visible height of that row, and VPOS is the row number
1837 \(zero-based). */)
1838 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1840 register struct window *w;
1841 register EMACS_INT posint;
1842 register struct buffer *buf;
1843 struct text_pos top;
1844 Lisp_Object in_window = Qnil;
1845 int rtop, rbot, rowh, vpos, fully_p = 1;
1846 int x, y;
1848 w = decode_live_window (window);
1849 buf = XBUFFER (w->contents);
1850 SET_TEXT_POS_FROM_MARKER (top, w->start);
1852 if (EQ (pos, Qt))
1853 posint = -1;
1854 else if (!NILP (pos))
1856 CHECK_NUMBER_COERCE_MARKER (pos);
1857 posint = XINT (pos);
1859 else if (w == XWINDOW (selected_window))
1860 posint = PT;
1861 else
1862 posint = marker_position (w->pointm);
1864 /* If position is above window start or outside buffer boundaries,
1865 or if window start is out of range, position is not visible. */
1866 if ((EQ (pos, Qt)
1867 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1868 && CHARPOS (top) >= BUF_BEGV (buf)
1869 && CHARPOS (top) <= BUF_ZV (buf)
1870 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1871 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1872 in_window = Qt;
1874 if (!NILP (in_window) && !NILP (partially))
1876 Lisp_Object part = Qnil;
1877 if (!fully_p)
1878 part = list4i (rtop, rbot, rowh, vpos);
1879 in_window = Fcons (make_number (x),
1880 Fcons (make_number (y), part));
1883 return in_window;
1886 DEFUN ("window-line-height", Fwindow_line_height,
1887 Swindow_line_height, 0, 2, 0,
1888 doc: /* Return height in pixels of text line LINE in window WINDOW.
1889 WINDOW must be a live window and defaults to the selected one.
1891 Return height of current line if LINE is omitted or nil. Return height of
1892 header or mode line if LINE is `header-line' or `mode-line'.
1893 Otherwise, LINE is a text line number starting from 0. A negative number
1894 counts from the end of the window.
1896 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1897 in pixels of the visible part of the line, VPOS and YPOS are the
1898 vertical position in lines and pixels of the line, relative to the top
1899 of the first text line, and OFFBOT is the number of off-window pixels at
1900 the bottom of the text line. If there are off-window pixels at the top
1901 of the (first) text line, YPOS is negative.
1903 Return nil if window display is not up-to-date. In that case, use
1904 `pos-visible-in-window-p' to obtain the information. */)
1905 (Lisp_Object line, Lisp_Object window)
1907 register struct window *w;
1908 register struct buffer *b;
1909 struct glyph_row *row, *end_row;
1910 int max_y, crop, i;
1911 EMACS_INT n;
1913 w = decode_live_window (window);
1915 if (noninteractive || w->pseudo_window_p)
1916 return Qnil;
1918 CHECK_BUFFER (w->contents);
1919 b = XBUFFER (w->contents);
1921 /* Fail if current matrix is not up-to-date. */
1922 if (!w->window_end_valid
1923 || windows_or_buffers_changed
1924 || b->clip_changed
1925 || b->prevent_redisplay_optimizations_p
1926 || window_outdated (w))
1927 return Qnil;
1929 if (NILP (line))
1931 i = w->cursor.vpos;
1932 if (i < 0 || i >= w->current_matrix->nrows
1933 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1934 return Qnil;
1935 max_y = window_text_bottom_y (w);
1936 goto found_row;
1939 if (EQ (line, Qheader_line))
1941 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1942 return Qnil;
1943 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1944 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1947 if (EQ (line, Qmode_line))
1949 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1950 return (row->enabled_p ?
1951 list4i (row->height,
1952 0, /* not accurate */
1953 (WINDOW_HEADER_LINE_HEIGHT (w)
1954 + window_text_bottom_y (w)),
1956 : Qnil);
1959 CHECK_NUMBER (line);
1960 n = XINT (line);
1962 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1963 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1964 max_y = window_text_bottom_y (w);
1965 i = 0;
1967 while ((n < 0 || i < n)
1968 && row <= end_row && row->enabled_p
1969 && row->y + row->height < max_y)
1970 row++, i++;
1972 if (row > end_row || !row->enabled_p)
1973 return Qnil;
1975 if (++n < 0)
1977 if (-n > i)
1978 return Qnil;
1979 row += n;
1980 i += n;
1983 found_row:
1984 crop = max (0, (row->y + row->height) - max_y);
1985 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1988 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1989 0, 1, 0,
1990 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1991 More precisely, return the value assigned by the last call of
1992 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1993 never called with WINDOW as its argument, or the value set by that
1994 function was internally reset since its last call. WINDOW must be a
1995 live window and defaults to the selected one.
1997 When a window is dedicated to its buffer, `display-buffer' will refrain
1998 from displaying another buffer in it. `get-lru-window' and
1999 `get-largest-window' treat dedicated windows specially.
2000 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
2001 `kill-buffer' can delete a dedicated window and the containing frame.
2003 Functions like `set-window-buffer' may change the buffer displayed by a
2004 window, unless that window is "strongly" dedicated to its buffer, that
2005 is the value returned by `window-dedicated-p' is t. */)
2006 (Lisp_Object window)
2008 return decode_live_window (window)->dedicated;
2011 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
2012 Sset_window_dedicated_p, 2, 2, 0,
2013 doc: /* Mark WINDOW as dedicated according to FLAG.
2014 WINDOW must be a live window and defaults to the selected one. FLAG
2015 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
2016 mark WINDOW as non-dedicated. Return FLAG.
2018 When a window is dedicated to its buffer, `display-buffer' will refrain
2019 from displaying another buffer in it. `get-lru-window' and
2020 `get-largest-window' treat dedicated windows specially.
2021 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
2022 `quit-restore-window' and `kill-buffer' can delete a dedicated window
2023 and the containing frame.
2025 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
2026 its buffer. Functions like `set-window-buffer' may change the buffer
2027 displayed by a window, unless that window is strongly dedicated to its
2028 buffer. If and when `set-window-buffer' displays another buffer in a
2029 window, it also makes sure that the window is no more dedicated. */)
2030 (Lisp_Object window, Lisp_Object flag)
2032 wset_dedicated (decode_live_window (window), flag);
2033 return flag;
2036 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
2037 0, 1, 0,
2038 doc: /* Return buffers previously shown in WINDOW.
2039 WINDOW must be a live window and defaults to the selected one.
2041 The return value is a list of elements (BUFFER WINDOW-START POS),
2042 where BUFFER is a buffer, WINDOW-START is the start position of the
2043 window for that buffer, and POS is a window-specific point value. */)
2044 (Lisp_Object window)
2046 return decode_live_window (window)->prev_buffers;
2049 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2050 Sset_window_prev_buffers, 2, 2, 0,
2051 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2052 WINDOW must be a live window and defaults to the selected one.
2054 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2055 where BUFFER is a buffer, WINDOW-START is the start position of the
2056 window for that buffer, and POS is a window-specific point value. */)
2057 (Lisp_Object window, Lisp_Object prev_buffers)
2059 wset_prev_buffers (decode_live_window (window), prev_buffers);
2060 return prev_buffers;
2063 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2064 0, 1, 0,
2065 doc: /* Return list of buffers recently re-shown in WINDOW.
2066 WINDOW must be a live window and defaults to the selected one. */)
2067 (Lisp_Object window)
2069 return decode_live_window (window)->next_buffers;
2072 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2073 Sset_window_next_buffers, 2, 2, 0,
2074 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2075 WINDOW must be a live window and defaults to the selected one.
2076 NEXT-BUFFERS should be a list of buffers. */)
2077 (Lisp_Object window, Lisp_Object next_buffers)
2079 wset_next_buffers (decode_live_window (window), next_buffers);
2080 return next_buffers;
2083 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2084 0, 1, 0,
2085 doc: /* Return the parameters of WINDOW and their values.
2086 WINDOW must be a valid window and defaults to the selected one. The
2087 return value is a list of elements of the form (PARAMETER . VALUE). */)
2088 (Lisp_Object window)
2090 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2093 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2094 2, 2, 0,
2095 doc: /* Return WINDOW's value for PARAMETER.
2096 WINDOW can be any window and defaults to the selected one. */)
2097 (Lisp_Object window, Lisp_Object parameter)
2099 Lisp_Object result;
2101 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2102 return CDR_SAFE (result);
2105 DEFUN ("set-window-parameter", Fset_window_parameter,
2106 Sset_window_parameter, 3, 3, 0,
2107 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2108 WINDOW can be any window and defaults to the selected one.
2109 Return VALUE. */)
2110 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2112 register struct window *w = decode_any_window (window);
2113 Lisp_Object old_alist_elt;
2115 old_alist_elt = Fassq (parameter, w->window_parameters);
2116 if (NILP (old_alist_elt))
2117 wset_window_parameters
2118 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2119 else
2120 Fsetcdr (old_alist_elt, value);
2121 return value;
2124 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2125 0, 1, 0,
2126 doc: /* Return the display-table that WINDOW is using.
2127 WINDOW must be a live window and defaults to the selected one. */)
2128 (Lisp_Object window)
2130 return decode_live_window (window)->display_table;
2133 /* Get the display table for use on window W. This is either W's
2134 display table or W's buffer's display table. Ignore the specified
2135 tables if they are not valid; if no valid table is specified,
2136 return 0. */
2138 struct Lisp_Char_Table *
2139 window_display_table (struct window *w)
2141 struct Lisp_Char_Table *dp = NULL;
2143 if (DISP_TABLE_P (w->display_table))
2144 dp = XCHAR_TABLE (w->display_table);
2145 else if (BUFFERP (w->contents))
2147 struct buffer *b = XBUFFER (w->contents);
2149 if (DISP_TABLE_P (BVAR (b, display_table)))
2150 dp = XCHAR_TABLE (BVAR (b, display_table));
2151 else if (DISP_TABLE_P (Vstandard_display_table))
2152 dp = XCHAR_TABLE (Vstandard_display_table);
2155 return dp;
2158 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2159 doc: /* Set WINDOW's display-table to TABLE.
2160 WINDOW must be a live window and defaults to the selected one. */)
2161 (register Lisp_Object window, Lisp_Object table)
2163 wset_display_table (decode_live_window (window), table);
2164 return table;
2167 /* Record info on buffer window W is displaying
2168 when it is about to cease to display that buffer. */
2169 static void
2170 unshow_buffer (register struct window *w)
2172 Lisp_Object buf = w->contents;
2173 struct buffer *b = XBUFFER (buf);
2175 eassert (b == XMARKER (w->pointm)->buffer);
2177 #if 0
2178 if (w == XWINDOW (selected_window)
2179 || ! EQ (buf, XWINDOW (selected_window)->contents))
2180 /* Do this except when the selected window's buffer
2181 is being removed from some other window. */
2182 #endif
2183 /* last_window_start records the start position that this buffer
2184 had in the last window to be disconnected from it.
2185 Now that this statement is unconditional,
2186 it is possible for the buffer to be displayed in the
2187 selected window, while last_window_start reflects another
2188 window which was recently showing the same buffer.
2189 Some people might say that might be a good thing. Let's see. */
2190 b->last_window_start = marker_position (w->start);
2192 /* Point in the selected window's buffer
2193 is actually stored in that buffer, and the window's pointm isn't used.
2194 So don't clobber point in that buffer. */
2195 if (! EQ (buf, XWINDOW (selected_window)->contents)
2196 /* Don't clobber point in current buffer either (this could be
2197 useful in connection with bug#12208).
2198 && XBUFFER (buf) != current_buffer */
2199 /* This line helps to fix Horsley's testbug.el bug. */
2200 && !(WINDOWP (BVAR (b, last_selected_window))
2201 && w != XWINDOW (BVAR (b, last_selected_window))
2202 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2203 temp_set_point_both (b,
2204 clip_to_bounds (BUF_BEGV (b),
2205 marker_position (w->pointm),
2206 BUF_ZV (b)),
2207 clip_to_bounds (BUF_BEGV_BYTE (b),
2208 marker_byte_position (w->pointm),
2209 BUF_ZV_BYTE (b)));
2211 if (WINDOWP (BVAR (b, last_selected_window))
2212 && w == XWINDOW (BVAR (b, last_selected_window)))
2213 bset_last_selected_window (b, Qnil);
2216 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2217 means change window structure only. Otherwise store geometry and
2218 other settings as well. */
2219 static void
2220 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2222 register Lisp_Object tem;
2223 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2225 /* If OLD is its frame's root window, then NEW is the new
2226 root window for that frame. */
2227 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2228 fset_root_window (XFRAME (o->frame), new);
2230 if (setflag)
2232 n->pixel_left = o->pixel_left;
2233 n->pixel_top = o->pixel_top;
2234 n->pixel_width = o->pixel_width;
2235 n->pixel_height = o->pixel_height;
2236 n->left_col = o->left_col;
2237 n->top_line = o->top_line;
2238 n->total_cols = o->total_cols;
2239 n->total_lines = o->total_lines;
2240 wset_normal_cols (n, o->normal_cols);
2241 wset_normal_cols (o, make_float (1.0));
2242 wset_normal_lines (n, o->normal_lines);
2243 wset_normal_lines (o, make_float (1.0));
2244 n->desired_matrix = n->current_matrix = 0;
2245 n->vscroll = 0;
2246 memset (&n->cursor, 0, sizeof (n->cursor));
2247 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2248 n->last_cursor_vpos = 0;
2249 #ifdef HAVE_WINDOW_SYSTEM
2250 n->phys_cursor_type = NO_CURSOR;
2251 n->phys_cursor_width = -1;
2252 #endif
2253 n->must_be_updated_p = 0;
2254 n->pseudo_window_p = 0;
2255 n->window_end_vpos = 0;
2256 n->window_end_pos = 0;
2257 n->window_end_valid = 0;
2260 tem = o->next;
2261 wset_next (n, tem);
2262 if (!NILP (tem))
2263 wset_prev (XWINDOW (tem), new);
2265 tem = o->prev;
2266 wset_prev (n, tem);
2267 if (!NILP (tem))
2268 wset_next (XWINDOW (tem), new);
2270 tem = o->parent;
2271 wset_parent (n, tem);
2272 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2273 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2276 /* If window WINDOW and its parent window are iso-combined, merge
2277 WINDOW's children into those of its parent window and mark WINDOW as
2278 deleted. */
2280 static void
2281 recombine_windows (Lisp_Object window)
2283 struct window *w, *p, *c;
2284 Lisp_Object parent, child;
2285 bool horflag;
2287 w = XWINDOW (window);
2288 parent = w->parent;
2289 if (!NILP (parent) && NILP (w->combination_limit))
2291 p = XWINDOW (parent);
2292 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2293 && p->horizontal == w->horizontal)
2294 /* WINDOW and PARENT are both either a vertical or a horizontal
2295 combination. */
2297 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2298 child = w->contents;
2299 c = XWINDOW (child);
2301 /* Splice WINDOW's children into its parent's children and
2302 assign new normal sizes. */
2303 if (NILP (w->prev))
2304 wset_combination (p, horflag, child);
2305 else
2307 wset_prev (c, w->prev);
2308 wset_next (XWINDOW (w->prev), child);
2311 while (c)
2313 wset_parent (c, parent);
2315 if (horflag)
2316 wset_normal_cols
2317 (c, make_float ((double) c->pixel_width
2318 / (double) p->pixel_width));
2319 else
2320 wset_normal_lines
2321 (c, make_float ((double) c->pixel_height
2322 / (double) p->pixel_height));
2324 if (NILP (c->next))
2326 if (!NILP (w->next))
2328 wset_next (c, w->next);
2329 wset_prev (XWINDOW (c->next), child);
2332 c = 0;
2334 else
2336 child = c->next;
2337 c = XWINDOW (child);
2341 /* WINDOW can be deleted now. */
2342 wset_combination (w, 0, Qnil);
2347 /* If WINDOW can be deleted, delete it. */
2348 static void
2349 delete_deletable_window (Lisp_Object window)
2351 if (!NILP (call1 (Qwindow_deletable_p, window)))
2352 call1 (Qdelete_window, window);
2355 /***********************************************************************
2356 Window List
2357 ***********************************************************************/
2359 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2360 pointer. This is a callback function for foreach_window, used in
2361 the window_list function. */
2363 static int
2364 add_window_to_list (struct window *w, void *user_data)
2366 Lisp_Object *list = user_data;
2367 Lisp_Object window;
2368 XSETWINDOW (window, w);
2369 *list = Fcons (window, *list);
2370 return 1;
2374 /* Return a list of all windows, for use by next_window. If
2375 Vwindow_list is a list, return that list. Otherwise, build a new
2376 list, cache it in Vwindow_list, and return that. */
2378 Lisp_Object
2379 window_list (void)
2381 if (!CONSP (Vwindow_list))
2383 Lisp_Object tail, frame;
2385 Vwindow_list = Qnil;
2386 FOR_EACH_FRAME (tail, frame)
2388 Lisp_Object args[2];
2390 /* We are visiting windows in canonical order, and add
2391 new windows at the front of args[1], which means we
2392 have to reverse this list at the end. */
2393 args[1] = Qnil;
2394 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2395 args[0] = Vwindow_list;
2396 args[1] = Fnreverse (args[1]);
2397 Vwindow_list = Fnconc (2, args);
2401 return Vwindow_list;
2405 /* Value is non-zero if WINDOW satisfies the constraints given by
2406 OWINDOW, MINIBUF and ALL_FRAMES.
2408 MINIBUF t means WINDOW may be minibuffer windows.
2409 `lambda' means WINDOW may not be a minibuffer window.
2410 a window means a specific minibuffer window
2412 ALL_FRAMES t means search all frames,
2413 nil means search just current frame,
2414 `visible' means search just visible frames on the
2415 current terminal,
2416 0 means search visible and iconified frames on the
2417 current terminal,
2418 a window means search the frame that window belongs to,
2419 a frame means consider windows on that frame, only. */
2421 static bool
2422 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2423 Lisp_Object minibuf, Lisp_Object all_frames)
2425 struct window *w = XWINDOW (window);
2426 struct frame *f = XFRAME (w->frame);
2427 bool candidate_p = 1;
2429 if (!BUFFERP (w->contents))
2430 candidate_p = 0;
2431 else if (MINI_WINDOW_P (w)
2432 && (EQ (minibuf, Qlambda)
2433 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2435 /* If MINIBUF is `lambda' don't consider any mini-windows.
2436 If it is a window, consider only that one. */
2437 candidate_p = 0;
2439 else if (EQ (all_frames, Qt))
2440 candidate_p = 1;
2441 else if (NILP (all_frames))
2443 eassert (WINDOWP (owindow));
2444 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2446 else if (EQ (all_frames, Qvisible))
2448 candidate_p = FRAME_VISIBLE_P (f)
2449 && (FRAME_TERMINAL (XFRAME (w->frame))
2450 == FRAME_TERMINAL (XFRAME (selected_frame)));
2453 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2455 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2456 #ifdef HAVE_X_WINDOWS
2457 /* Yuck!! If we've just created the frame and the
2458 window-manager requested the user to place it
2459 manually, the window may still not be considered
2460 `visible'. I'd argue it should be at least
2461 something like `iconified', but don't know how to do
2462 that yet. --Stef */
2463 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2464 && !f->output_data.x->has_been_visible)
2465 #endif
2467 && (FRAME_TERMINAL (XFRAME (w->frame))
2468 == FRAME_TERMINAL (XFRAME (selected_frame)));
2470 else if (WINDOWP (all_frames))
2471 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2472 || EQ (XWINDOW (all_frames)->frame, w->frame)
2473 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2474 else if (FRAMEP (all_frames))
2475 candidate_p = EQ (all_frames, w->frame);
2477 return candidate_p;
2481 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2482 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2483 MINIBUF, and ALL_FRAMES. */
2485 static void
2486 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2488 struct window *w = decode_live_window (*window);
2490 XSETWINDOW (*window, w);
2491 /* MINIBUF nil may or may not include minibuffers. Decide if it
2492 does. */
2493 if (NILP (*minibuf))
2494 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2495 else if (!EQ (*minibuf, Qt))
2496 *minibuf = Qlambda;
2498 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2499 => count none of them, or a specific minibuffer window (the
2500 active one) to count. */
2502 /* ALL_FRAMES nil doesn't specify which frames to include. */
2503 if (NILP (*all_frames))
2504 *all_frames
2505 = (!EQ (*minibuf, Qlambda)
2506 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2507 : Qnil);
2508 else if (EQ (*all_frames, Qvisible))
2510 else if (EQ (*all_frames, make_number (0)))
2512 else if (FRAMEP (*all_frames))
2514 else if (!EQ (*all_frames, Qt))
2515 *all_frames = Qnil;
2519 /* Return the next or previous window of WINDOW in cyclic ordering
2520 of windows. NEXT_P non-zero means return the next window. See the
2521 documentation string of next-window for the meaning of MINIBUF and
2522 ALL_FRAMES. */
2524 static Lisp_Object
2525 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2527 decode_next_window_args (&window, &minibuf, &all_frames);
2529 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2530 return the first window on the frame. */
2531 if (FRAMEP (all_frames)
2532 && !EQ (all_frames, XWINDOW (window)->frame))
2533 return Fframe_first_window (all_frames);
2535 if (next_p)
2537 Lisp_Object list;
2539 /* Find WINDOW in the list of all windows. */
2540 list = Fmemq (window, window_list ());
2542 /* Scan forward from WINDOW to the end of the window list. */
2543 if (CONSP (list))
2544 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2545 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2546 break;
2548 /* Scan from the start of the window list up to WINDOW. */
2549 if (!CONSP (list))
2550 for (list = Vwindow_list;
2551 CONSP (list) && !EQ (XCAR (list), window);
2552 list = XCDR (list))
2553 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2554 break;
2556 if (CONSP (list))
2557 window = XCAR (list);
2559 else
2561 Lisp_Object candidate, list;
2563 /* Scan through the list of windows for candidates. If there are
2564 candidate windows in front of WINDOW, the last one of these
2565 is the one we want. If there are candidates following WINDOW
2566 in the list, again the last one of these is the one we want. */
2567 candidate = Qnil;
2568 for (list = window_list (); CONSP (list); list = XCDR (list))
2570 if (EQ (XCAR (list), window))
2572 if (WINDOWP (candidate))
2573 break;
2575 else if (candidate_window_p (XCAR (list), window, minibuf,
2576 all_frames))
2577 candidate = XCAR (list);
2580 if (WINDOWP (candidate))
2581 window = candidate;
2584 return window;
2588 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2589 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2590 WINDOW must be a live window and defaults to the selected one. The
2591 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2592 consider.
2594 MINIBUF nil or omitted means consider the minibuffer window only if the
2595 minibuffer is active. MINIBUF t means consider the minibuffer window
2596 even if the minibuffer is not active. Any other value means do not
2597 consider the minibuffer window even if the minibuffer is active.
2599 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2600 plus the minibuffer window if specified by the MINIBUF argument. If the
2601 minibuffer counts, consider all windows on all frames that share that
2602 minibuffer too. The following non-nil values of ALL-FRAMES have special
2603 meanings:
2605 - t means consider all windows on all existing frames.
2607 - `visible' means consider all windows on all visible frames.
2609 - 0 (the number zero) means consider all windows on all visible and
2610 iconified frames.
2612 - A frame means consider all windows on that frame only.
2614 Anything else means consider all windows on WINDOW's frame and no
2615 others.
2617 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2618 `next-window' to iterate through the entire cycle of acceptable
2619 windows, eventually ending up back at the window you started with.
2620 `previous-window' traverses the same cycle, in the reverse order. */)
2621 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2623 return next_window (window, minibuf, all_frames, 1);
2627 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2628 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2629 WINDOW must be a live window and defaults to the selected one. The
2630 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2631 consider.
2633 MINIBUF nil or omitted means consider the minibuffer window only if the
2634 minibuffer is active. MINIBUF t means consider the minibuffer window
2635 even if the minibuffer is not active. Any other value means do not
2636 consider the minibuffer window even if the minibuffer is active.
2638 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2639 plus the minibuffer window if specified by the MINIBUF argument. If the
2640 minibuffer counts, consider all windows on all frames that share that
2641 minibuffer too. The following non-nil values of ALL-FRAMES have special
2642 meanings:
2644 - t means consider all windows on all existing frames.
2646 - `visible' means consider all windows on all visible frames.
2648 - 0 (the number zero) means consider all windows on all visible and
2649 iconified frames.
2651 - A frame means consider all windows on that frame only.
2653 Anything else means consider all windows on WINDOW's frame and no
2654 others.
2656 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2657 use `previous-window' to iterate through the entire cycle of
2658 acceptable windows, eventually ending up back at the window you
2659 started with. `next-window' traverses the same cycle, in the
2660 reverse order. */)
2661 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2663 return next_window (window, minibuf, all_frames, 0);
2667 /* Return a list of windows in cyclic ordering. Arguments are like
2668 for `next-window'. */
2670 static Lisp_Object
2671 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2673 Lisp_Object tail, list, rest;
2675 decode_next_window_args (&window, &minibuf, &all_frames);
2676 list = Qnil;
2678 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2679 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2680 list = Fcons (XCAR (tail), list);
2682 /* Rotate the list to start with WINDOW. */
2683 list = Fnreverse (list);
2684 rest = Fmemq (window, list);
2685 if (!NILP (rest) && !EQ (rest, list))
2687 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2689 XSETCDR (tail, Qnil);
2690 list = nconc2 (rest, list);
2692 return list;
2696 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2697 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2698 FRAME nil or omitted means use the selected frame.
2699 WINDOW nil or omitted means use the window selected within FRAME.
2700 MINIBUF t means include the minibuffer window, even if it isn't active.
2701 MINIBUF nil or omitted means include the minibuffer window only
2702 if it's active.
2703 MINIBUF neither nil nor t means never include the minibuffer window. */)
2704 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2706 if (NILP (window))
2707 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2708 CHECK_WINDOW (window);
2709 if (NILP (frame))
2710 frame = selected_frame;
2712 if (!EQ (frame, XWINDOW (window)->frame))
2713 error ("Window is on a different frame");
2715 return window_list_1 (window, minibuf, frame);
2719 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2720 doc: /* Return a list of all live windows.
2721 WINDOW specifies the first window to list and defaults to the selected
2722 window.
2724 Optional argument MINIBUF nil or omitted means consider the minibuffer
2725 window only if the minibuffer is active. MINIBUF t means consider the
2726 minibuffer window even if the minibuffer is not active. Any other value
2727 means do not consider the minibuffer window even if the minibuffer is
2728 active.
2730 Optional argument ALL-FRAMES nil or omitted means consider all windows
2731 on WINDOW's frame, plus the minibuffer window if specified by the
2732 MINIBUF argument. If the minibuffer counts, consider all windows on all
2733 frames that share that minibuffer too. The following non-nil values of
2734 ALL-FRAMES have special meanings:
2736 - t means consider all windows on all existing frames.
2738 - `visible' means consider all windows on all visible frames.
2740 - 0 (the number zero) means consider all windows on all visible and
2741 iconified frames.
2743 - A frame means consider all windows on that frame only.
2745 Anything else means consider all windows on WINDOW's frame and no
2746 others.
2748 If WINDOW is not on the list of windows returned, some other window will
2749 be listed first but no error is signaled. */)
2750 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2752 return window_list_1 (window, minibuf, all_frames);
2755 /* Look at all windows, performing an operation specified by TYPE
2756 with argument OBJ.
2757 If FRAMES is Qt, look at all frames;
2758 Qnil, look at just the selected frame;
2759 Qvisible, look at visible frames;
2760 a frame, just look at windows on that frame.
2761 If MINI is non-zero, perform the operation on minibuffer windows too. */
2763 enum window_loop
2765 WINDOW_LOOP_UNUSED,
2766 GET_BUFFER_WINDOW, /* Arg is buffer */
2767 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2768 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2769 CHECK_ALL_WINDOWS /* Arg is ignored */
2772 static Lisp_Object
2773 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2775 Lisp_Object window, windows, best_window, frame_arg;
2776 int frame_best_window_flag = 0;
2777 struct frame *f;
2778 struct gcpro gcpro1;
2780 /* If we're only looping through windows on a particular frame,
2781 frame points to that frame. If we're looping through windows
2782 on all frames, frame is 0. */
2783 if (FRAMEP (frames))
2784 f = XFRAME (frames);
2785 else if (NILP (frames))
2786 f = SELECTED_FRAME ();
2787 else
2788 f = NULL;
2790 if (f)
2791 frame_arg = Qlambda;
2792 else if (EQ (frames, make_number (0)))
2793 frame_arg = frames;
2794 else if (EQ (frames, Qvisible))
2795 frame_arg = frames;
2796 else
2797 frame_arg = Qt;
2799 /* frame_arg is Qlambda to stick to one frame,
2800 Qvisible to consider all visible frames,
2801 or Qt otherwise. */
2803 /* Pick a window to start with. */
2804 if (WINDOWP (obj))
2805 window = obj;
2806 else if (f)
2807 window = FRAME_SELECTED_WINDOW (f);
2808 else
2809 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2811 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2812 GCPRO1 (windows);
2813 best_window = Qnil;
2815 for (; CONSP (windows); windows = XCDR (windows))
2817 struct window *w;
2819 window = XCAR (windows);
2820 w = XWINDOW (window);
2822 /* Note that we do not pay attention here to whether the frame
2823 is visible, since Fwindow_list skips non-visible frames if
2824 that is desired, under the control of frame_arg. */
2825 if (!MINI_WINDOW_P (w)
2826 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2827 consider all windows. */
2828 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2829 || (mini && minibuf_level > 0))
2830 switch (type)
2832 case GET_BUFFER_WINDOW:
2833 if (EQ (w->contents, obj)
2834 /* Don't find any minibuffer window except the one that
2835 is currently in use. */
2836 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2838 if (EQ (window, selected_window))
2839 /* Preferably return the selected window. */
2840 RETURN_UNGCPRO (window);
2841 else if (EQ (XWINDOW (window)->frame, selected_frame)
2842 && !frame_best_window_flag)
2843 /* Prefer windows on the current frame (but don't
2844 choose another one if we have one already). */
2846 best_window = window;
2847 frame_best_window_flag = 1;
2849 else if (NILP (best_window))
2850 best_window = window;
2852 break;
2854 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2855 /* We could simply check whether the buffer shown by window
2856 is live, and show another buffer in case it isn't. */
2857 if (EQ (w->contents, obj))
2859 /* Undedicate WINDOW. */
2860 wset_dedicated (w, Qnil);
2861 /* Make WINDOW show the buffer returned by
2862 other_buffer_safely, don't run any hooks. */
2863 set_window_buffer
2864 (window, other_buffer_safely (w->contents), 0, 0);
2865 /* If WINDOW is the selected window, make its buffer
2866 current. But do so only if the window shows the
2867 current buffer (Bug#6454). */
2868 if (EQ (window, selected_window)
2869 && XBUFFER (w->contents) == current_buffer)
2870 Fset_buffer (w->contents);
2872 break;
2874 case REDISPLAY_BUFFER_WINDOWS:
2875 if (EQ (w->contents, obj))
2877 mark_window_display_accurate (window, 0);
2878 w->update_mode_line = 1;
2879 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2880 update_mode_lines = 27;
2881 best_window = window;
2883 break;
2885 /* Check for a leaf window that has a killed buffer
2886 or broken markers. */
2887 case CHECK_ALL_WINDOWS:
2888 if (BUFFERP (w->contents))
2890 struct buffer *b = XBUFFER (w->contents);
2892 if (!BUFFER_LIVE_P (b))
2893 emacs_abort ();
2894 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2895 emacs_abort ();
2896 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2897 emacs_abort ();
2899 break;
2901 case WINDOW_LOOP_UNUSED:
2902 break;
2906 UNGCPRO;
2907 return best_window;
2910 /* Used for debugging. Abort if any window has a dead buffer. */
2912 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2913 void
2914 check_all_windows (void)
2916 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2919 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2920 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2921 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2922 the current buffer.
2924 The optional argument ALL-FRAMES specifies the frames to consider:
2926 - t means consider all windows on all existing frames.
2928 - `visible' means consider all windows on all visible frames.
2930 - 0 (the number zero) means consider all windows on all visible
2931 and iconified frames.
2933 - A frame means consider all windows on that frame only.
2935 Any other value of ALL-FRAMES means consider all windows on the
2936 selected frame and no others. */)
2937 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2939 Lisp_Object buffer;
2941 if (NILP (buffer_or_name))
2942 buffer = Fcurrent_buffer ();
2943 else
2944 buffer = Fget_buffer (buffer_or_name);
2946 if (BUFFERP (buffer))
2947 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2948 else
2949 return Qnil;
2952 static Lisp_Object
2953 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2955 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2959 static Lisp_Object
2960 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2962 return call2(Qwindow_pixel_to_total, frame, horizontal);
2966 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2967 Sdelete_other_windows_internal, 0, 2, "",
2968 doc: /* Make WINDOW fill its frame.
2969 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2970 and defaults to the selected one.
2972 Optional argument ROOT, if non-nil, must specify an internal window such
2973 that WINDOW is in its window subtree. If this is the case, replace ROOT
2974 by WINDOW and leave alone any windows not part of ROOT's subtree.
2976 When WINDOW is live try to reduce display jumps by keeping the text
2977 previously visible in WINDOW in the same place on the frame. Doing this
2978 depends on the value of (window-start WINDOW), so if calling this
2979 function in a program gives strange scrolling, make sure the
2980 window-start value is reasonable when this function is called. */)
2981 (Lisp_Object window, Lisp_Object root)
2983 struct window *w, *r, *s;
2984 struct frame *f;
2985 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2986 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2987 int top IF_LINT (= 0), new_top, resize_failed;
2989 w = decode_valid_window (window);
2990 XSETWINDOW (window, w);
2991 f = XFRAME (w->frame);
2993 if (NILP (root))
2994 /* ROOT is the frame's root window. */
2996 root = FRAME_ROOT_WINDOW (f);
2997 r = XWINDOW (root);
2999 else
3000 /* ROOT must be an ancestor of WINDOW. */
3002 r = decode_valid_window (root);
3003 pwindow = XWINDOW (window)->parent;
3004 while (!NILP (pwindow))
3005 if (EQ (pwindow, root))
3006 break;
3007 else
3008 pwindow = XWINDOW (pwindow)->parent;
3009 if (!EQ (pwindow, root))
3010 error ("Specified root is not an ancestor of specified window");
3013 if (EQ (window, root))
3014 /* A noop. */
3015 return Qnil;
3016 /* I don't understand the "top > 0" part below. If we deal with a
3017 standalone minibuffer it would have been caught by the preceding
3018 test. */
3019 else if (MINI_WINDOW_P (w)) /* && top > 0) */
3020 error ("Can't expand minibuffer to full frame");
3022 if (BUFFERP (w->contents))
3024 startpos = marker_position (w->start);
3025 startbyte = marker_byte_position (w->start);
3026 top = (WINDOW_TOP_EDGE_LINE (w)
3027 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
3028 /* Make sure WINDOW is the frame's selected window. */
3029 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
3031 if (EQ (selected_frame, w->frame))
3032 Fselect_window (window, Qnil);
3033 else
3034 fset_selected_window (f, window);
3037 else
3039 /* See if the frame's selected window is a part of the window
3040 subtree rooted at WINDOW, by finding all the selected window's
3041 parents and comparing each one with WINDOW. If it isn't we
3042 need a new selected window for this frame. */
3043 swindow = FRAME_SELECTED_WINDOW (f);
3044 while (1)
3046 pwindow = swindow;
3047 while (!NILP (pwindow) && !EQ (window, pwindow))
3048 pwindow = XWINDOW (pwindow)->parent;
3050 if (EQ (window, pwindow))
3051 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3052 as the new selected window. */
3053 break;
3054 else
3055 /* Else try the previous window of SWINDOW. */
3056 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3059 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3061 if (EQ (selected_frame, w->frame))
3062 Fselect_window (swindow, Qnil);
3063 else
3064 fset_selected_window (f, swindow);
3068 block_input ();
3069 if (!FRAME_INITIAL_P (f))
3071 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3073 /* We are going to free the glyph matrices of WINDOW, and with
3074 that we might lose any information about glyph rows that have
3075 some of their glyphs highlighted in mouse face. (These rows
3076 are marked with a non-zero mouse_face_p flag.) If WINDOW
3077 indeed has some glyphs highlighted in mouse face, signal to
3078 frame's up-to-date hook that mouse highlight was overwritten,
3079 so that it will arrange for redisplaying the highlight. */
3080 if (EQ (hlinfo->mouse_face_window, window))
3081 reset_mouse_highlight (hlinfo);
3083 free_window_matrices (r);
3085 fset_redisplay (f);
3086 Vwindow_list = Qnil;
3087 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3088 resize_failed = 0;
3090 if (!WINDOW_LEAF_P (w))
3092 /* Resize child windows vertically. */
3093 XSETINT (delta, r->pixel_height - w->pixel_height);
3094 w->pixel_top = r->pixel_top;
3095 w->top_line = r->top_line;
3096 resize_root_window (window, delta, Qnil, Qnil, Qt);
3097 if (window_resize_check (w, 0))
3099 window_resize_apply (w, 0);
3100 window_pixel_to_total (w->frame, Qnil);
3102 else
3104 resize_root_window (window, delta, Qnil, Qt, Qt);
3105 if (window_resize_check (w, 0))
3107 window_resize_apply (w, 0);
3108 window_pixel_to_total (w->frame, Qnil);
3110 else
3111 resize_failed = 1;
3114 /* Resize child windows horizontally. */
3115 if (!resize_failed)
3117 w->left_col = r->left_col;
3118 w->pixel_left = r->pixel_left;
3119 XSETINT (delta, r->pixel_width - w->pixel_width);
3120 resize_root_window (window, delta, Qt, Qnil, Qt);
3121 if (window_resize_check (w, 1))
3123 window_resize_apply (w, 1);
3124 window_pixel_to_total (w->frame, Qt);
3126 else
3128 resize_root_window (window, delta, Qt, Qt, Qt);
3129 if (window_resize_check (w, 1))
3131 window_resize_apply (w, 1);
3132 window_pixel_to_total (w->frame, Qt);
3134 else
3135 resize_failed = 1;
3139 if (resize_failed)
3140 /* Play safe, if we still can ... */
3142 window = swindow;
3143 w = XWINDOW (window);
3147 /* Cleanly unlink WINDOW from window-tree. */
3148 if (!NILP (w->prev))
3149 /* Get SIBLING above (on the left of) WINDOW. */
3151 sibling = w->prev;
3152 s = XWINDOW (sibling);
3153 wset_next (s, w->next);
3154 if (!NILP (s->next))
3155 wset_prev (XWINDOW (s->next), sibling);
3157 else
3158 /* Get SIBLING below (on the right of) WINDOW. */
3160 sibling = w->next;
3161 s = XWINDOW (sibling);
3162 wset_prev (s, Qnil);
3163 wset_combination (XWINDOW (w->parent),
3164 XWINDOW (w->parent)->horizontal, sibling);
3167 /* Delete ROOT and all child windows of ROOT. */
3168 if (WINDOWP (r->contents))
3170 delete_all_child_windows (r->contents);
3171 wset_combination (r, 0, Qnil);
3174 replace_window (root, window, 1);
3176 /* This must become SWINDOW anyway ....... */
3177 if (BUFFERP (w->contents) && !resize_failed)
3179 /* Try to minimize scrolling, by setting the window start to the
3180 point will cause the text at the old window start to be at the
3181 same place on the frame. But don't try to do this if the
3182 window start is outside the visible portion (as might happen
3183 when the display is not current, due to typeahead). */
3184 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3185 if (new_top != top
3186 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3187 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3189 struct position pos;
3190 struct buffer *obuf = current_buffer;
3192 Fset_buffer (w->contents);
3193 /* This computation used to temporarily move point, but that
3194 can have unwanted side effects due to text properties. */
3195 pos = *vmotion (startpos, startbyte, -top, w);
3197 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3198 w->window_end_valid = 0;
3199 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3200 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3201 /* We need to do this, so that the window-scroll-functions
3202 get called. */
3203 w->optional_new_start = 1;
3205 set_buffer_internal (obuf);
3209 adjust_frame_glyphs (f);
3210 unblock_input ();
3212 run_window_configuration_change_hook (f);
3214 return Qnil;
3218 void
3219 replace_buffer_in_windows (Lisp_Object buffer)
3221 call1 (Qreplace_buffer_in_windows, buffer);
3224 /* If BUFFER is shown in a window, safely replace it with some other
3225 buffer in all windows of all frames, even those on other keyboards. */
3227 void
3228 replace_buffer_in_windows_safely (Lisp_Object buffer)
3230 if (buffer_window_count (XBUFFER (buffer)))
3232 Lisp_Object tail, frame;
3234 /* A single call to window_loop won't do the job because it only
3235 considers frames on the current keyboard. So loop manually over
3236 frames, and handle each one. */
3237 FOR_EACH_FRAME (tail, frame)
3238 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3242 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3243 minimum allowable size. PIXELWISE means interpret these as pixel
3244 sizes. */
3246 void
3247 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3249 /* For height, we have to see:
3250 how many windows the frame has at minimum (one or two),
3251 and whether it has a menu bar or other special stuff at the top. */
3252 if (pixelwise)
3254 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3255 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3257 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3258 min_height = 2 * min_height;
3260 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3261 min_height += FRAME_INTERNAL_BORDER_WIDTH (frame);
3263 if (*height < min_height)
3264 *height = min_height;
3265 if (*width < min_width)
3266 *width = min_width;
3268 else
3270 int min_height
3271 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3272 ? MIN_SAFE_WINDOW_HEIGHT
3273 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3275 if (FRAME_TOP_MARGIN (frame) > 0)
3276 min_height += FRAME_TOP_MARGIN (frame);
3278 if (*height < min_height)
3279 *height = min_height;
3280 if (*width < MIN_SAFE_WINDOW_WIDTH)
3281 *width = MIN_SAFE_WINDOW_WIDTH;
3285 /* Adjust the margins of window W if text area is too small.
3286 Return 1 if window width is ok after adjustment; 0 if window
3287 is still too narrow. */
3289 static int
3290 adjust_window_margins (struct window *w)
3292 int box_width = (WINDOW_PIXEL_WIDTH (w)
3293 - WINDOW_FRINGES_WIDTH (w)
3294 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3295 int margin_width = WINDOW_MARGINS_WIDTH (w);
3297 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3298 return 1;
3300 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3301 return 0;
3302 else
3303 /* Window's text area is too narrow, but reducing the window
3304 margins will fix that. */
3306 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3308 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3310 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3312 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3313 w->left_margin_cols = w->right_margin_cols =
3314 margin_width / (2 * unit);
3315 else
3316 w->right_margin_cols = margin_width / unit;
3318 else
3319 w->left_margin_cols = margin_width / unit;
3321 return 1;
3325 /* The following three routines are needed for running a window's
3326 configuration change hook. */
3327 static void
3328 run_funs (Lisp_Object funs)
3330 for (; CONSP (funs); funs = XCDR (funs))
3331 if (!EQ (XCAR (funs), Qt))
3332 call0 (XCAR (funs));
3335 static void
3336 select_window_norecord (Lisp_Object window)
3338 if (WINDOW_LIVE_P (window))
3339 Fselect_window (window, Qt);
3342 static void
3343 select_frame_norecord (Lisp_Object frame)
3345 if (FRAME_LIVE_P (XFRAME (frame)))
3346 Fselect_frame (frame, Qt);
3349 void
3350 run_window_configuration_change_hook (struct frame *f)
3352 ptrdiff_t count = SPECPDL_INDEX ();
3353 Lisp_Object frame, global_wcch
3354 = Fdefault_value (Qwindow_configuration_change_hook);
3355 XSETFRAME (frame, f);
3357 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3358 return;
3360 /* Use the right buffer. Matters when running the local hooks. */
3361 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3363 record_unwind_current_buffer ();
3364 Fset_buffer (Fwindow_buffer (Qnil));
3367 if (SELECTED_FRAME () != f)
3369 record_unwind_protect (select_frame_norecord, selected_frame);
3370 select_frame_norecord (frame);
3373 /* Look for buffer-local values. */
3375 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3376 for (; CONSP (windows); windows = XCDR (windows))
3378 Lisp_Object window = XCAR (windows);
3379 Lisp_Object buffer = Fwindow_buffer (window);
3380 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3381 buffer)))
3383 ptrdiff_t inner_count = SPECPDL_INDEX ();
3384 record_unwind_protect (select_window_norecord, selected_window);
3385 select_window_norecord (window);
3386 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3387 buffer));
3388 unbind_to (inner_count, Qnil);
3393 run_funs (global_wcch);
3394 unbind_to (count, Qnil);
3397 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3398 Srun_window_configuration_change_hook, 0, 1, 0,
3399 doc: /* Run `window-configuration-change-hook' for FRAME.
3400 If FRAME is omitted or nil, it defaults to the selected frame. */)
3401 (Lisp_Object frame)
3403 run_window_configuration_change_hook (decode_live_frame (frame));
3404 return Qnil;
3407 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3408 Srun_window_scroll_functions, 0, 1, 0,
3409 doc: /* Run `window-scroll-functions' for WINDOW.
3410 If WINDOW is omitted or nil, it defaults to the selected window. */)
3411 (Lisp_Object window)
3413 if (! NILP (Vwindow_scroll_functions))
3414 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3415 Fmarker_position (decode_live_window (window)->start));
3416 return Qnil;
3419 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3420 to run hooks. See make_frame for a case where it's not allowed.
3421 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3422 scroll-bar settings of the window are not reset from the buffer's
3423 local settings. */
3425 void
3426 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3427 bool run_hooks_p, bool keep_margins_p)
3429 struct window *w = XWINDOW (window);
3430 struct buffer *b = XBUFFER (buffer);
3431 ptrdiff_t count = SPECPDL_INDEX ();
3432 bool samebuf = EQ (buffer, w->contents);
3434 wset_buffer (w, buffer);
3436 if (EQ (window, selected_window))
3437 bset_last_selected_window (b, window);
3439 /* Let redisplay errors through. */
3440 b->display_error_modiff = 0;
3442 /* Update time stamps of buffer display. */
3443 if (INTEGERP (BVAR (b, display_count)))
3444 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3445 bset_display_time (b, Fcurrent_time ());
3447 w->window_end_pos = 0;
3448 w->window_end_vpos = 0;
3449 w->last_cursor_vpos = 0;
3451 if (!(keep_margins_p && samebuf))
3452 { /* If we're not actually changing the buffer, don't reset hscroll and
3453 vscroll. This case happens for example when called from
3454 change_frame_size_1, where we use a dummy call to
3455 Fset_window_buffer on the frame's selected window (and no other)
3456 just in order to run window-configuration-change-hook.
3457 Resetting hscroll and vscroll here is problematic for things like
3458 image-mode and doc-view-mode since it resets the image's position
3459 whenever we resize the frame. */
3460 w->hscroll = w->min_hscroll = 0;
3461 w->vscroll = 0;
3462 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3463 set_marker_restricted (w->start,
3464 make_number (b->last_window_start),
3465 buffer);
3466 w->start_at_line_beg = 0;
3467 w->force_start = 0;
3469 /* Maybe we could move this into the `if' but it's not obviously safe and
3470 I doubt it's worth the trouble. */
3471 wset_redisplay (w);
3472 w->update_mode_line = true;
3474 /* We must select BUFFER for running the window-scroll-functions. */
3475 /* We can't check ! NILP (Vwindow_scroll_functions) here
3476 because that might itself be a local variable. */
3477 if (window_initialized)
3479 record_unwind_current_buffer ();
3480 Fset_buffer (buffer);
3483 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3485 if (!keep_margins_p)
3487 /* Set left and right marginal area width etc. from buffer. */
3488 set_window_fringes (w, BVAR (b, left_fringe_width),
3489 BVAR (b, right_fringe_width),
3490 BVAR (b, fringes_outside_margins));
3491 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3492 BVAR (b, vertical_scroll_bar_type), Qnil);
3493 set_window_margins (w, BVAR (b, left_margin_cols),
3494 BVAR (b, right_margin_cols));
3495 apply_window_adjustment (w);
3498 if (run_hooks_p)
3500 if (! NILP (Vwindow_scroll_functions))
3501 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3502 Fmarker_position (w->start));
3503 if (!samebuf)
3504 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3507 unbind_to (count, Qnil);
3510 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3511 doc: /* Make WINDOW display BUFFER-OR-NAME.
3512 WINDOW must be a live window and defaults to the selected one.
3513 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3515 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3516 display margins, fringe widths, and scroll bar settings are preserved;
3517 the default is to reset these from the local settings for BUFFER-OR-NAME
3518 or the frame defaults. Return nil.
3520 This function throws an error when WINDOW is strongly dedicated to its
3521 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3522 already display BUFFER-OR-NAME.
3524 This function runs `window-scroll-functions' before running
3525 `window-configuration-change-hook'. */)
3526 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3528 register Lisp_Object tem, buffer;
3529 register struct window *w = decode_live_window (window);
3531 XSETWINDOW (window, w);
3532 buffer = Fget_buffer (buffer_or_name);
3533 CHECK_BUFFER (buffer);
3534 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3535 error ("Attempt to display deleted buffer");
3537 tem = w->contents;
3538 if (NILP (tem))
3539 error ("Window is deleted");
3540 else
3542 if (!EQ (tem, buffer))
3544 if (EQ (w->dedicated, Qt))
3545 /* WINDOW is strongly dedicated to its buffer, signal an
3546 error. */
3547 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3548 else
3549 /* WINDOW is weakly dedicated to its buffer, reset
3550 dedication. */
3551 wset_dedicated (w, Qnil);
3553 call1 (Qrecord_window_buffer, window);
3556 unshow_buffer (w);
3559 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3561 return Qnil;
3564 static Lisp_Object
3565 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3567 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3570 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3571 0, 1, 0,
3572 doc: /* Force all windows to be updated on next redisplay.
3573 If optional arg OBJECT is a window, force redisplay of that window only.
3574 If OBJECT is a buffer or buffer name, force redisplay of all windows
3575 displaying that buffer. */)
3576 (Lisp_Object object)
3578 if (NILP (object))
3580 windows_or_buffers_changed = 29;
3581 update_mode_lines = 28;
3582 return Qt;
3585 if (WINDOWP (object))
3587 struct window *w = XWINDOW (object);
3588 mark_window_display_accurate (object, 0);
3589 w->update_mode_line = 1;
3590 if (BUFFERP (w->contents))
3591 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3592 update_mode_lines = 29;
3593 return Qt;
3596 if (STRINGP (object))
3597 object = Fget_buffer (object);
3598 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3599 && buffer_window_count (XBUFFER (object)))
3601 /* If buffer is live and shown in at least one window, find
3602 all windows showing this buffer and force update of them. */
3603 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3604 return NILP (object) ? Qnil : Qt;
3607 /* If nothing suitable was found, just return.
3608 We could signal an error, but this feature will typically be used
3609 asynchronously in timers or process sentinels, so we don't. */
3610 return Qnil;
3613 /* Obsolete since 24.3. */
3614 void
3615 temp_output_buffer_show (register Lisp_Object buf)
3617 register struct buffer *old = current_buffer;
3618 register Lisp_Object window;
3619 register struct window *w;
3621 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3623 Fset_buffer (buf);
3624 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3625 BEGV = BEG;
3626 ZV = Z;
3627 SET_PT (BEG);
3628 set_buffer_internal (old);
3630 if (!NILP (Vtemp_buffer_show_function))
3631 call1 (Vtemp_buffer_show_function, buf);
3632 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3634 if (!EQ (XWINDOW (window)->frame, selected_frame))
3635 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3636 Vminibuf_scroll_window = window;
3637 w = XWINDOW (window);
3638 w->hscroll = 0;
3639 w->min_hscroll = 0;
3640 set_marker_restricted_both (w->start, buf, BEG, BEG);
3641 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3643 /* Run temp-buffer-show-hook, with the chosen window selected
3644 and its buffer current. */
3646 ptrdiff_t count = SPECPDL_INDEX ();
3647 Lisp_Object prev_window, prev_buffer;
3648 prev_window = selected_window;
3649 XSETBUFFER (prev_buffer, old);
3651 /* Select the window that was chosen, for running the hook.
3652 Note: Both Fselect_window and select_window_norecord may
3653 set-buffer to the buffer displayed in the window,
3654 so we need to save the current buffer. --stef */
3655 record_unwind_protect (restore_buffer, prev_buffer);
3656 record_unwind_protect (select_window_norecord, prev_window);
3657 Fselect_window (window, Qt);
3658 Fset_buffer (w->contents);
3659 Frun_hooks (1, &Qtemp_buffer_show_hook);
3660 unbind_to (count, Qnil);
3665 /* Make new window, have it replace WINDOW in window-tree, and make
3666 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3667 horizontal child). */
3668 static void
3669 make_parent_window (Lisp_Object window, bool horflag)
3671 Lisp_Object parent;
3672 register struct window *o, *p;
3674 o = XWINDOW (window);
3675 p = allocate_window ();
3676 memcpy ((char *) p + sizeof (struct vectorlike_header),
3677 (char *) o + sizeof (struct vectorlike_header),
3678 word_size * VECSIZE (struct window));
3679 /* P's buffer slot may change from nil to a buffer... */
3680 adjust_window_count (p, 1);
3681 XSETWINDOW (parent, p);
3683 p->sequence_number = ++sequence_number;
3685 replace_window (window, parent, 1);
3687 wset_next (o, Qnil);
3688 wset_prev (o, Qnil);
3689 wset_parent (o, parent);
3690 /* ...but now P becomes an internal window. */
3691 wset_start (p, Qnil);
3692 wset_pointm (p, Qnil);
3693 wset_buffer (p, Qnil);
3694 wset_combination (p, horflag, window);
3695 wset_combination_limit (p, Qnil);
3696 wset_window_parameters (p, Qnil);
3699 /* Make new window from scratch. */
3700 Lisp_Object
3701 make_window (void)
3703 Lisp_Object window;
3704 register struct window *w;
3706 w = allocate_window ();
3707 /* Initialize Lisp data. Note that allocate_window initializes all
3708 Lisp data to nil, so do it only for slots which should not be nil. */
3709 wset_normal_lines (w, make_float (1.0));
3710 wset_normal_cols (w, make_float (1.0));
3711 wset_new_total (w, make_number (0));
3712 wset_new_normal (w, make_number (0));
3713 wset_new_pixel (w, make_number (0));
3714 wset_start (w, Fmake_marker ());
3715 wset_pointm (w, Fmake_marker ());
3716 wset_vertical_scroll_bar_type (w, Qt);
3717 /* These Lisp fields are marked specially so they're not set to nil by
3718 allocate_window. */
3719 wset_prev_buffers (w, Qnil);
3720 wset_next_buffers (w, Qnil);
3722 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3723 non-Lisp data, so do it only for slots which should not be zero. */
3724 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3725 w->left_fringe_width = w->right_fringe_width = -1;
3726 w->mode_line_height = w->header_line_height = -1;
3727 #ifdef HAVE_WINDOW_SYSTEM
3728 w->phys_cursor_type = NO_CURSOR;
3729 w->phys_cursor_width = -1;
3730 #endif
3731 w->sequence_number = ++sequence_number;
3732 w->scroll_bar_width = -1;
3733 w->column_number_displayed = -1;
3735 /* Reset window_list. */
3736 Vwindow_list = Qnil;
3737 /* Return window. */
3738 XSETWINDOW (window, w);
3739 return window;
3742 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3743 doc: /* Set new pixel size of WINDOW to SIZE.
3744 WINDOW must be a valid window and defaults to the selected one.
3745 Return SIZE.
3747 Optional argument ADD non-nil means add SIZE to the new pixel size of
3748 WINDOW and return the sum.
3750 The new pixel size of WINDOW, if valid, will be shortly installed as
3751 WINDOW's pixel height (see `window-pixel-height') or pixel width (see
3752 `window-pixel-width').
3754 Note: This function does not operate on any child windows of WINDOW. */)
3755 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3757 struct window *w = decode_valid_window (window);
3758 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3759 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3761 CHECK_RANGED_INTEGER (size, size_min, size_max);
3762 if (NILP (add))
3763 wset_new_pixel (w, size);
3764 else
3765 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3767 return w->new_pixel;
3770 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3771 doc: /* Set new total size of WINDOW to SIZE.
3772 WINDOW must be a valid window and defaults to the selected one.
3773 Return SIZE.
3775 Optional argument ADD non-nil means add SIZE to the new total size of
3776 WINDOW and return the sum.
3778 The new total size of WINDOW, if valid, will be shortly installed as
3779 WINDOW's total height (see `window-total-height') or total width (see
3780 `window-total-width').
3782 Note: This function does not operate on any child windows of WINDOW. */)
3783 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3785 struct window *w = decode_valid_window (window);
3787 CHECK_NUMBER (size);
3788 if (NILP (add))
3789 wset_new_total (w, size);
3790 else
3791 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3793 return w->new_total;
3796 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3797 doc: /* Set new normal size of WINDOW to SIZE.
3798 WINDOW must be a valid window and defaults to the selected one.
3799 Return SIZE.
3801 The new normal size of WINDOW, if valid, will be shortly installed as
3802 WINDOW's normal size (see `window-normal-size').
3804 Note: This function does not operate on any child windows of WINDOW. */)
3805 (Lisp_Object window, Lisp_Object size)
3807 wset_new_normal (decode_valid_window (window), size);
3808 return size;
3811 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3812 non-zero) to w->new_pixel would result in correct heights (widths)
3813 for window W and recursively all child windows of W.
3815 Note: This function does not check any of `window-fixed-size-p',
3816 `window-min-height' or `window-min-width'. It does check that window
3817 sizes do not drop below one line (two columns). */
3818 static int
3819 window_resize_check (struct window *w, bool horflag)
3821 struct frame *f = XFRAME (w->frame);
3822 struct window *c;
3824 if (WINDOW_VERTICAL_COMBINATION_P (w))
3825 /* W is a vertical combination. */
3827 c = XWINDOW (w->contents);
3828 if (horflag)
3829 /* All child windows of W must have the same width as W. */
3831 while (c)
3833 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3834 || !window_resize_check (c, horflag))
3835 return 0;
3837 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3840 return 1;
3842 else
3843 /* The sum of the heights of the child windows of W must equal
3844 W's height. */
3846 int remaining_pixels = XINT (w->new_pixel);
3848 while (c)
3850 if (!window_resize_check (c, horflag))
3851 return 0;
3853 remaining_pixels -= XINT (c->new_pixel);
3854 if (remaining_pixels < 0)
3855 return 0;
3856 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3859 return remaining_pixels == 0;
3862 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3863 /* W is a horizontal combination. */
3865 c = XWINDOW (w->contents);
3866 if (horflag)
3867 /* The sum of the widths of the child windows of W must equal W's
3868 width. */
3870 int remaining_pixels = XINT (w->new_pixel);
3872 while (c)
3874 if (!window_resize_check (c, horflag))
3875 return 0;
3877 remaining_pixels -= XINT (c->new_pixel);
3878 if (remaining_pixels < 0)
3879 return 0;
3880 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3883 return remaining_pixels == 0;
3885 else
3886 /* All child windows of W must have the same height as W. */
3888 while (c)
3890 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3891 || !window_resize_check (c, horflag))
3892 return 0;
3894 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3897 return 1;
3900 else
3901 /* A leaf window. Make sure it's not too small. The following
3902 hardcodes the values of `window-safe-min-width' (2) and
3903 `window-safe-min-height' (1) which are defined in window.el. */
3904 return (XINT (w->new_pixel) >= (horflag
3905 ? (2 * FRAME_COLUMN_WIDTH (f))
3906 : FRAME_LINE_HEIGHT (f)));
3910 /* Set w->pixel_height (w->pixel_width if HORFLAG is non-zero) to
3911 w->new_pixel for window W and recursively all child windows of W.
3912 Also calculate and assign the new vertical (horizontal) pixel start
3913 positions of each of these windows.
3915 This function does not perform any error checks. Make sure you have
3916 run window_resize_check on W before applying this function. */
3917 static void
3918 window_resize_apply (struct window *w, bool horflag)
3920 struct window *c;
3921 int edge;
3922 int unit = (horflag
3923 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3924 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3926 /* Note: Assigning new_normal requires that the new total size of the
3927 parent window has been set *before*. */
3928 if (horflag)
3930 w->pixel_width = XFASTINT (w->new_pixel);
3931 w->total_cols = w->pixel_width / unit;
3932 if (NUMBERP (w->new_normal))
3933 wset_normal_cols (w, w->new_normal);
3935 edge = w->pixel_left;
3937 else
3939 w->pixel_height = XFASTINT (w->new_pixel);
3940 w->total_lines = w->pixel_height / unit;
3941 if (NUMBERP (w->new_normal))
3942 wset_normal_lines (w, w->new_normal);
3944 edge = w->pixel_top;
3947 if (WINDOW_VERTICAL_COMBINATION_P (w))
3948 /* W is a vertical combination. */
3950 c = XWINDOW (w->contents);
3951 while (c)
3953 if (horflag)
3955 c->pixel_left = edge;
3956 c->left_col = edge / unit;
3958 else
3960 c->pixel_top = edge;
3961 c->top_line = edge / unit;
3963 window_resize_apply (c, horflag);
3964 if (!horflag)
3965 edge = edge + c->pixel_height;
3967 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3970 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3971 /* W is a horizontal combination. */
3973 c = XWINDOW (w->contents);
3974 while (c)
3976 if (horflag)
3978 c->pixel_left = edge;
3979 c->left_col = edge / unit;
3981 else
3983 c->pixel_top = edge;
3984 c->top_line = edge / unit;
3987 window_resize_apply (c, horflag);
3988 if (horflag)
3989 edge = edge + c->pixel_width;
3991 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3994 else
3996 adjust_window_margins (w);
3997 /* Bug#15957. */
3998 w->window_end_valid = 0;
4003 /* Set w->total_lines (w->total_cols if HORFLAG is non-zero) to
4004 w->new_total for window W and recursively all child windows of W.
4005 Also calculate and assign the new vertical (horizontal) start
4006 positions of each of these windows. */
4007 static void
4008 window_resize_apply_total (struct window *w, bool horflag)
4010 struct window *c;
4011 int edge;
4013 /* Note: Assigning new_normal requires that the new total size of the
4014 parent window has been set *before*. */
4015 if (horflag)
4017 w->total_cols = XFASTINT (w->new_total);
4018 edge = w->left_col;
4020 else
4022 w->total_lines = XFASTINT (w->new_total);
4023 edge = w->top_line;
4026 if (WINDOW_VERTICAL_COMBINATION_P (w))
4027 /* W is a vertical combination. */
4029 c = XWINDOW (w->contents);
4030 while (c)
4032 if (horflag)
4033 c->left_col = edge;
4034 else
4035 c->top_line = edge;
4037 window_resize_apply_total (c, horflag);
4038 if (!horflag)
4039 edge = edge + c->total_lines;
4041 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4044 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
4045 /* W is a horizontal combination. */
4047 c = XWINDOW (w->contents);
4048 while (c)
4050 if (horflag)
4051 c->left_col = edge;
4052 else
4053 c->top_line = edge;
4055 window_resize_apply_total (c, horflag);
4056 if (horflag)
4057 edge = edge + c->total_cols;
4059 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4064 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4065 doc: /* Apply requested size values for window-tree of FRAME.
4066 If FRAME is omitted or nil, it defaults to the selected frame.
4068 Optional argument HORIZONTAL omitted or nil means apply requested
4069 height values. HORIZONTAL non-nil means apply requested width values.
4071 The requested size values are those set by `set-window-new-pixel' and
4072 `set-window-new-normal'. This function checks whether the requested
4073 values sum up to a valid window layout, recursively assigns the new
4074 sizes of all child windows and calculates and assigns the new start
4075 positions of these windows.
4077 Return t if the requested values have been applied correctly, nil
4078 otherwise.
4080 Note: This function does not check any of `window-fixed-size-p',
4081 `window-min-height' or `window-min-width'. All these checks have to
4082 be applied on the Elisp level. */)
4083 (Lisp_Object frame, Lisp_Object horizontal)
4085 struct frame *f = decode_live_frame (frame);
4086 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4087 bool horflag = !NILP (horizontal);
4089 if (!window_resize_check (r, horflag)
4090 || (XINT (r->new_pixel)
4091 != (horflag ? r->pixel_width : r->pixel_height)))
4092 return Qnil;
4094 block_input ();
4095 window_resize_apply (r, horflag);
4097 fset_redisplay (f);
4098 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4100 adjust_frame_glyphs (f);
4101 unblock_input ();
4103 return Qt;
4107 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4108 doc: /* Apply requested total size values for window-tree of FRAME.
4109 If FRAME is omitted or nil, it defaults to the selected frame.
4111 This function does not assign pixel or normal size values. You should
4112 have run `window-resize-apply' before running this.
4114 Optional argument HORIZONTAL omitted or nil means apply requested
4115 height values. HORIZONTAL non-nil means apply requested width
4116 values. */)
4117 (Lisp_Object frame, Lisp_Object horizontal)
4119 struct frame *f = decode_live_frame (frame);
4120 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4122 block_input ();
4123 /* Necessary when deleting the top-/or leftmost window. */
4124 r->left_col = 0;
4125 r->top_line = FRAME_TOP_MARGIN (f);
4126 window_resize_apply_total (r, !NILP (horizontal));
4127 /* Handle the mini window. */
4128 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4130 struct window *m = XWINDOW (f->minibuffer_window);
4132 if (NILP (horizontal))
4134 m->top_line = r->top_line + r->total_lines;
4135 m->total_lines = XFASTINT (m->new_total);
4137 else
4138 m->total_cols = XFASTINT (m->new_total);
4141 unblock_input ();
4143 return Qt;
4147 /* Resize frame F's windows when number of lines of F is set to SIZE.
4148 HORFLAG 1 means resize windows when number of columns of F is set to
4149 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4151 This function can delete all windows but the selected one in order to
4152 satisfy the request. The result will be meaningful if and only if
4153 F's windows have meaningful sizes when you call this. */
4154 void
4155 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4157 Lisp_Object root = f->root_window;
4158 struct window *r = XWINDOW (root);
4159 Lisp_Object mini = f->minibuffer_window;
4160 struct window *m;
4161 /* old_size is the old size of the frame's root window. */
4162 int old_size = horflag ? r->total_cols : r->total_lines;
4163 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4164 /* new_size is the new size of the frame's root window. */
4165 int new_size, new_pixel_size;
4166 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4168 /* Don't let the size drop below one unit. This is more comforting
4169 when we are called from x_set_tool_bar_lines since the latter may
4170 have implicitly given us a zero or negative height. */
4171 if (pixelwise)
4173 /* Note: This does not include the size for internal borders
4174 since these are not part of the frame's text area. */
4175 new_pixel_size = max (horflag
4176 ? size
4177 : (size
4178 - FRAME_TOP_MARGIN_HEIGHT (f)
4179 - ((FRAME_HAS_MINIBUF_P (f)
4180 && !FRAME_MINIBUF_ONLY_P (f))
4181 ? FRAME_LINE_HEIGHT (f) : 0)),
4182 unit);
4183 new_size = new_pixel_size / unit;
4185 else
4187 new_size = max (horflag
4188 ? size
4189 : (size
4190 - FRAME_TOP_MARGIN (f)
4191 - ((FRAME_HAS_MINIBUF_P (f)
4192 && !FRAME_MINIBUF_ONLY_P (f))
4193 ? 1 : 0)),
4195 new_pixel_size = new_size * unit;
4198 r->top_line = FRAME_TOP_MARGIN (f);
4199 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4201 if (new_pixel_size == old_pixel_size)
4203 else if (WINDOW_LEAF_P (r))
4204 /* For a leaf root window just set the size. */
4205 if (horflag)
4207 r->total_cols = new_size;
4208 r->pixel_width = new_pixel_size;
4210 else
4212 r->total_lines = new_size;
4213 r->pixel_height = new_pixel_size;
4215 else
4217 Lisp_Object delta;
4219 if (pixelwise)
4220 XSETINT (delta, new_pixel_size - old_pixel_size);
4221 else
4222 XSETINT (delta, new_size - old_size);
4224 /* Try a "normal" resize first. */
4225 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4226 pixelwise ? Qt : Qnil);
4227 if (window_resize_check (r, horflag)
4228 && new_pixel_size == XINT (r->new_pixel))
4230 window_resize_apply (r, horflag);
4231 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4233 else
4235 /* Try with "reasonable" minimum sizes next. */
4236 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4237 pixelwise ? Qt : Qnil);
4238 if (window_resize_check (r, horflag)
4239 && new_pixel_size == XINT (r->new_pixel))
4241 window_resize_apply (r, horflag);
4242 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4244 else
4246 /* Finally, try with "safe" minimum sizes. */
4247 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4248 pixelwise ? Qt : Qnil);
4249 if (window_resize_check (r, horflag)
4250 && new_pixel_size == XINT (r->new_pixel))
4252 window_resize_apply (r, horflag);
4253 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4255 #if 0 /* Let's try without killing other windows. */
4256 else
4258 /* We lost. Delete all windows but the frame's
4259 selected one. */
4260 root = f->selected_window;
4261 Fdelete_other_windows_internal (root, Qnil);
4262 if (horflag)
4264 XWINDOW (root)->total_cols = new_size;
4265 XWINDOW (root)->pixel_width = new_pixel_size;
4267 else
4269 XWINDOW (root)->total_lines = new_size;
4270 XWINDOW (root)->pixel_height = new_pixel_size;
4273 #endif /* 0 */
4278 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4280 m = XWINDOW (mini);
4281 if (horflag)
4283 m->total_cols = size;
4284 m->pixel_width = new_pixel_size;
4286 else
4288 /* Are we sure we always want 1 line here? */
4289 m->total_lines = 1;
4290 m->pixel_height = FRAME_LINE_HEIGHT (f);
4291 m->top_line = r->top_line + r->total_lines;
4292 m->pixel_top = r->pixel_top + r->pixel_height;
4296 fset_redisplay (f);
4300 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4301 doc: /* Split window OLD.
4302 Second argument PIXEL-SIZE specifies the number of pixels of the
4303 new window. In any case TOTAL-SIZE must be a positive integer.
4305 Third argument SIDE nil (or `below') specifies that the new window shall
4306 be located below WINDOW. SIDE `above' means the new window shall be
4307 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4308 height of the new window including space reserved for the mode and/or
4309 header line.
4311 SIDE t (or `right') specifies that the new window shall be located on
4312 the right side of WINDOW. SIDE `left' means the new window shall be
4313 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4314 width of the new window including space reserved for fringes and the
4315 scrollbar or a divider column.
4317 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4318 according to the SIDE argument.
4320 The new pixel and normal sizes of all involved windows must have been
4321 set correctly. See the code of `split-window' for how this is done. */)
4322 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4324 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4325 parent window or an internal window we have to install as OLD's new
4326 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4327 provided OLD is a leaf window, or to the frame's selected window.
4328 NEW (*n) is the new window created with some parameters taken from
4329 REFERENCE (*r). */
4330 register Lisp_Object new, frame, reference;
4331 register struct window *o, *p, *n, *r, *c;
4332 struct frame *f;
4333 bool horflag
4334 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4335 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4336 int combination_limit = 0, sum = 0;
4337 int total_size;
4339 CHECK_WINDOW (old);
4340 o = XWINDOW (old);
4341 frame = WINDOW_FRAME (o);
4342 f = XFRAME (frame);
4344 CHECK_NUMBER (pixel_size);
4345 total_size
4346 = XINT (pixel_size) / (horflag
4347 ? FRAME_COLUMN_WIDTH (f)
4348 : FRAME_LINE_HEIGHT (f));
4350 /* Set combination_limit to 1 if we have to make a new parent window.
4351 We do that if either `window-combination-limit' is t, or OLD has no
4352 parent, or OLD is ortho-combined. */
4353 combination_limit =
4354 EQ (Vwindow_combination_limit, Qt)
4355 || NILP (o->parent)
4356 || (horflag
4357 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4358 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4360 /* We need a live reference window to initialize some parameters. */
4361 if (WINDOW_LIVE_P (old))
4362 /* OLD is live, use it as reference window. */
4363 reference = old;
4364 else
4365 /* Use the frame's selected window as reference window. */
4366 reference = FRAME_SELECTED_WINDOW (f);
4367 r = XWINDOW (reference);
4369 /* The following bugs are caught by `split-window'. */
4370 if (MINI_WINDOW_P (o))
4371 error ("Attempt to split minibuffer window");
4372 else if (total_size < (horflag ? 2 : 1))
4373 error ("Size of new window too small (after split)");
4374 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4375 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4376 proportionally. */
4378 p = XWINDOW (o->parent);
4379 /* Temporarily pretend we split the parent window. */
4380 wset_new_pixel
4381 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4382 - XINT (pixel_size)));
4383 if (!window_resize_check (p, horflag))
4384 error ("Window sizes don't fit");
4385 else
4386 /* Undo the temporary pretension. */
4387 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4389 else
4391 if (!window_resize_check (o, horflag))
4392 error ("Resizing old window failed");
4393 else if (XINT (pixel_size) + XINT (o->new_pixel)
4394 != (horflag ? o->pixel_width : o->pixel_height))
4395 error ("Sum of sizes of old and new window don't fit");
4398 /* This is our point of no return. */
4399 if (combination_limit)
4401 /* Save the old value of o->normal_cols/lines. It gets corrupted
4402 by make_parent_window and we need it below for assigning it to
4403 p->new_normal. */
4404 Lisp_Object new_normal
4405 = horflag ? o->normal_cols : o->normal_lines;
4407 make_parent_window (old, horflag);
4408 p = XWINDOW (o->parent);
4409 if (EQ (Vwindow_combination_limit, Qt))
4410 /* Store t in the new parent's combination_limit slot to avoid
4411 that its children get merged into another window. */
4412 wset_combination_limit (p, Qt);
4413 /* These get applied below. */
4414 wset_new_pixel
4415 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4416 wset_new_total
4417 (p, make_number (horflag ? o->total_cols : o->total_lines));
4418 wset_new_normal (p, new_normal);
4420 else
4421 p = XWINDOW (o->parent);
4423 fset_redisplay (f);
4424 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4425 new = make_window ();
4426 n = XWINDOW (new);
4427 wset_frame (n, frame);
4428 wset_parent (n, o->parent);
4430 if (EQ (side, Qabove) || EQ (side, Qleft))
4432 wset_prev (n, o->prev);
4433 if (NILP (n->prev))
4434 wset_combination (p, horflag, new);
4435 else
4436 wset_next (XWINDOW (n->prev), new);
4437 wset_next (n, old);
4438 wset_prev (o, new);
4440 else
4442 wset_next (n, o->next);
4443 if (!NILP (n->next))
4444 wset_prev (XWINDOW (n->next), new);
4445 wset_prev (n, old);
4446 wset_next (o, new);
4449 n->window_end_valid = 0;
4450 n->last_cursor_vpos = 0;
4452 /* Get special geometry settings from reference window. */
4453 n->left_margin_cols = r->left_margin_cols;
4454 n->right_margin_cols = r->right_margin_cols;
4455 n->left_fringe_width = r->left_fringe_width;
4456 n->right_fringe_width = r->right_fringe_width;
4457 n->fringes_outside_margins = r->fringes_outside_margins;
4458 n->scroll_bar_width = r->scroll_bar_width;
4459 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4461 /* Directly assign orthogonal coordinates and sizes. */
4462 if (horflag)
4464 n->pixel_top = o->pixel_top;
4465 n->top_line = o->top_line;
4466 n->pixel_height = o->pixel_height;
4467 n->total_lines = o->total_lines;
4469 else
4471 n->pixel_left = o->pixel_left;
4472 n->left_col = o->left_col;
4473 n->pixel_width = o->pixel_width;
4474 n->total_cols = o->total_cols;
4477 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4478 get them ready here. */
4479 wset_new_pixel (n, pixel_size);
4480 c = XWINDOW (p->contents);
4481 while (c)
4483 if (c != n)
4484 sum = sum + XINT (c->new_total);
4485 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4487 wset_new_total (n, make_number ((horflag
4488 ? p->total_cols
4489 : p->total_lines)
4490 - sum));
4491 wset_new_normal (n, normal_size);
4493 block_input ();
4494 window_resize_apply (p, horflag);
4495 adjust_frame_glyphs (f);
4496 /* Set buffer of NEW to buffer of reference window. Don't run
4497 any hooks. */
4498 set_window_buffer (new, r->contents, 0, 1);
4499 unblock_input ();
4501 /* Maybe we should run the scroll functions in Elisp (which already
4502 runs the configuration change hook). */
4503 if (! NILP (Vwindow_scroll_functions))
4504 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4505 Fmarker_position (n->start));
4506 /* Return NEW. */
4507 return new;
4511 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4512 doc: /* Remove WINDOW from its frame.
4513 WINDOW defaults to the selected window. Return nil.
4514 Signal an error when WINDOW is the only window on its frame. */)
4515 (register Lisp_Object window)
4517 register Lisp_Object parent, sibling, frame, root;
4518 struct window *w, *p, *s, *r;
4519 struct frame *f;
4520 bool horflag, before_sibling = 0;
4522 w = decode_any_window (window);
4523 XSETWINDOW (window, w);
4524 if (NILP (w->contents))
4525 /* It's a no-op to delete an already deleted window. */
4526 return Qnil;
4528 parent = w->parent;
4529 if (NILP (parent))
4530 /* Never delete a minibuffer or frame root window. */
4531 error ("Attempt to delete minibuffer or sole ordinary window");
4532 else if (NILP (w->prev) && NILP (w->next))
4533 /* Rather bow out here, this case should be handled on the Elisp
4534 level. */
4535 error ("Attempt to delete sole window of parent");
4537 p = XWINDOW (parent);
4538 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4540 frame = WINDOW_FRAME (w);
4541 f = XFRAME (frame);
4543 root = FRAME_ROOT_WINDOW (f);
4544 r = XWINDOW (root);
4546 /* Unlink WINDOW from window tree. */
4547 if (NILP (w->prev))
4548 /* Get SIBLING below (on the right of) WINDOW. */
4550 /* before_sibling 1 means WINDOW is the first child of its
4551 parent and thus before the sibling. */
4552 before_sibling = 1;
4553 sibling = w->next;
4554 s = XWINDOW (sibling);
4555 wset_prev (s, Qnil);
4556 wset_combination (p, horflag, sibling);
4558 else
4559 /* Get SIBLING above (on the left of) WINDOW. */
4561 sibling = w->prev;
4562 s = XWINDOW (sibling);
4563 wset_next (s, w->next);
4564 if (!NILP (s->next))
4565 wset_prev (XWINDOW (s->next), sibling);
4568 if (window_resize_check (r, horflag)
4569 && (XINT (r->new_pixel)
4570 == (horflag ? r->pixel_width : r->pixel_height)))
4571 /* We can delete WINDOW now. */
4574 /* Block input. */
4575 block_input ();
4576 window_resize_apply (p, horflag);
4577 /* If this window is referred to by the dpyinfo's mouse
4578 highlight, invalidate that slot to be safe (Bug#9904). */
4579 if (!FRAME_INITIAL_P (f))
4581 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4583 if (EQ (hlinfo->mouse_face_window, window))
4584 hlinfo->mouse_face_window = Qnil;
4587 fset_redisplay (f);
4588 Vwindow_list = Qnil;
4589 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4591 wset_next (w, Qnil); /* Don't delete w->next too. */
4592 free_window_matrices (w);
4594 if (WINDOWP (w->contents))
4596 delete_all_child_windows (w->contents);
4597 wset_combination (w, 0, Qnil);
4599 else
4601 unshow_buffer (w);
4602 unchain_marker (XMARKER (w->pointm));
4603 unchain_marker (XMARKER (w->start));
4604 wset_buffer (w, Qnil);
4607 if (NILP (s->prev) && NILP (s->next))
4608 /* A matrjoshka where SIBLING has become the only child of
4609 PARENT. */
4611 /* Put SIBLING into PARENT's place. */
4612 replace_window (parent, sibling, 0);
4613 /* Have SIBLING inherit the following three slot values from
4614 PARENT (the combination_limit slot is not inherited). */
4615 wset_normal_cols (s, p->normal_cols);
4616 wset_normal_lines (s, p->normal_lines);
4617 /* Mark PARENT as deleted. */
4618 wset_combination (p, 0, Qnil);
4619 /* Try to merge SIBLING into its new parent. */
4620 recombine_windows (sibling);
4623 adjust_frame_glyphs (f);
4625 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4626 /* We deleted the frame's selected window. */
4628 /* Use the frame's first window as fallback ... */
4629 Lisp_Object new_selected_window = Fframe_first_window (frame);
4630 /* ... but preferably use its most recently used window. */
4631 Lisp_Object mru_window;
4633 /* `get-mru-window' might fail for some reason so play it safe
4634 - promote the first window _without recording it_ first. */
4635 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4636 Fselect_window (new_selected_window, Qt);
4637 else
4638 fset_selected_window (f, new_selected_window);
4640 unblock_input ();
4642 /* Now look whether `get-mru-window' gets us something. */
4643 mru_window = call1 (Qget_mru_window, frame);
4644 if (WINDOW_LIVE_P (mru_window)
4645 && EQ (XWINDOW (mru_window)->frame, frame))
4646 new_selected_window = mru_window;
4648 /* If all ended up well, we now promote the mru window. */
4649 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4650 Fselect_window (new_selected_window, Qnil);
4651 else
4652 fset_selected_window (f, new_selected_window);
4654 else
4655 unblock_input ();
4657 /* Must be run by the caller:
4658 run_window_configuration_change_hook (f); */
4660 else
4661 /* We failed: Relink WINDOW into window tree. */
4663 if (before_sibling)
4665 wset_prev (s, window);
4666 wset_combination (p, horflag, window);
4668 else
4670 wset_next (s, window);
4671 if (!NILP (w->next))
4672 wset_prev (XWINDOW (w->next), window);
4674 error ("Deletion failed");
4677 return Qnil;
4680 /***********************************************************************
4681 Resizing Mini-Windows
4682 ***********************************************************************/
4684 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4685 can. */
4686 void
4687 grow_mini_window (struct window *w, int delta, bool pixelwise)
4689 struct frame *f = XFRAME (w->frame);
4690 struct window *r;
4691 Lisp_Object root, height;
4692 int line_height, pixel_height;
4694 eassert (MINI_WINDOW_P (w));
4695 eassert (delta >= 0);
4697 if (delta > 0)
4699 root = FRAME_ROOT_WINDOW (f);
4700 r = XWINDOW (root);
4701 height = call3 (Qwindow_resize_root_window_vertically,
4702 root, make_number (- delta), pixelwise ? Qt : Qnil);
4703 if (INTEGERP (height) && window_resize_check (r, 0))
4705 block_input ();
4706 window_resize_apply (r, 0);
4708 if (pixelwise)
4710 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4711 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4713 else
4715 line_height = min (-XINT (height),
4716 ((INT_MAX - w->pixel_height)
4717 / FRAME_LINE_HEIGHT (f)));
4718 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4721 /* Grow the mini-window. */
4722 w->pixel_top = r->pixel_top + r->pixel_height;
4723 w->top_line = r->top_line + r->total_lines;
4724 /* Make sure the mini-window has always at least one line. */
4725 w->pixel_height = max (w->pixel_height + pixel_height,
4726 FRAME_LINE_HEIGHT (f));
4727 w->total_lines = max (w->total_lines + line_height, 1);
4729 /* Enforce full redisplay of the frame. */
4730 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4731 fset_redisplay (f);
4732 adjust_frame_glyphs (f);
4733 unblock_input ();
4738 /* Shrink mini-window W to one line. */
4739 void
4740 shrink_mini_window (struct window *w, bool pixelwise)
4742 struct frame *f = XFRAME (w->frame);
4743 struct window *r;
4744 Lisp_Object root, delta;
4745 EMACS_INT height, unit;
4747 eassert (MINI_WINDOW_P (w));
4749 height = pixelwise ? w->pixel_height : w->total_lines;
4750 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4751 if (height > unit)
4753 root = FRAME_ROOT_WINDOW (f);
4754 r = XWINDOW (root);
4755 delta = call3 (Qwindow_resize_root_window_vertically,
4756 root, make_number (height - unit),
4757 pixelwise ? Qt : Qnil);
4758 if (INTEGERP (delta) && window_resize_check (r, 0))
4760 block_input ();
4761 window_resize_apply (r, 0);
4763 /* Shrink the mini-window. */
4764 w->top_line = r->top_line + r->total_lines;
4765 w->total_lines = 1;
4766 w->pixel_top = r->pixel_top + r->pixel_height;
4767 w->pixel_height = FRAME_LINE_HEIGHT (f);
4768 /* Enforce full redisplay of the frame. */
4769 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4770 fset_redisplay (f);
4771 adjust_frame_glyphs (f);
4772 unblock_input ();
4774 /* If the above failed for whatever strange reason we must make a
4775 one window frame here. The same routine will be needed when
4776 shrinking the frame (and probably when making the initial
4777 *scratch* window). For the moment leave things as they are. */
4781 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4782 doc: /* Resize minibuffer window WINDOW. */)
4783 (Lisp_Object window)
4785 struct window *w = XWINDOW (window);
4786 struct window *r;
4787 struct frame *f;
4788 int height;
4790 CHECK_WINDOW (window);
4791 f = XFRAME (w->frame);
4793 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4794 error ("Not a valid minibuffer window");
4795 else if (FRAME_MINIBUF_ONLY_P (f))
4796 error ("Cannot resize a minibuffer-only frame");
4798 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4799 height = r->pixel_height + w->pixel_height;
4800 if (window_resize_check (r, 0)
4801 && XINT (w->new_pixel) > 0
4802 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4804 block_input ();
4805 window_resize_apply (r, 0);
4807 w->pixel_height = XFASTINT (w->new_pixel);
4808 w->total_lines = w->pixel_height / FRAME_LINE_HEIGHT (f);
4809 w->pixel_top = r->pixel_top + r->pixel_height;
4810 w->top_line = r->top_line + r->total_lines;
4812 fset_redisplay (f);
4813 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4814 adjust_frame_glyphs (f);
4815 unblock_input ();
4816 return Qt;
4818 else
4819 error ("Failed to resize minibuffer window");
4822 /* Mark window cursors off for all windows in the window tree rooted
4823 at W by setting their phys_cursor_on_p flag to zero. Called from
4824 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4825 the frame are cleared. */
4827 void
4828 mark_window_cursors_off (struct window *w)
4830 while (w)
4832 if (WINDOWP (w->contents))
4833 mark_window_cursors_off (XWINDOW (w->contents));
4834 else
4835 w->phys_cursor_on_p = 0;
4837 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4842 /* Return number of lines of text (not counting mode lines) in W. */
4845 window_internal_height (struct window *w)
4847 int ht = w->total_lines;
4849 if (!MINI_WINDOW_P (w))
4851 if (!NILP (w->parent)
4852 || WINDOWP (w->contents)
4853 || !NILP (w->next)
4854 || !NILP (w->prev)
4855 || WINDOW_WANTS_MODELINE_P (w))
4856 --ht;
4858 if (WINDOW_WANTS_HEADER_LINE_P (w))
4859 --ht;
4862 return ht;
4865 /************************************************************************
4866 Window Scrolling
4867 ***********************************************************************/
4869 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4870 N screen-fulls, which is defined as the height of the window minus
4871 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4872 instead. Negative values of N mean scroll down. NOERROR non-zero
4873 means don't signal an error if we try to move over BEGV or ZV,
4874 respectively. */
4876 static void
4877 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4879 immediate_quit = 1;
4880 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4882 wset_redisplay (XWINDOW (window));
4884 /* If we must, use the pixel-based version which is much slower than
4885 the line-based one but can handle varying line heights. */
4886 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4887 window_scroll_pixel_based (window, n, whole, noerror);
4888 else
4889 window_scroll_line_based (window, n, whole, noerror);
4891 /* Bug#15957. */
4892 XWINDOW (window)->window_end_valid = 0;
4893 immediate_quit = 0;
4897 /* Implementation of window_scroll that works based on pixel line
4898 heights. See the comment of window_scroll for parameter
4899 descriptions. */
4901 static void
4902 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4904 struct it it;
4905 struct window *w = XWINDOW (window);
4906 struct text_pos start;
4907 int this_scroll_margin;
4908 /* True if we fiddled the window vscroll field without really scrolling. */
4909 int vscrolled = 0;
4910 int x, y, rtop, rbot, rowh, vpos;
4911 void *itdata = NULL;
4912 int window_total_lines;
4913 int frame_line_height = default_line_pixel_height (w);
4915 SET_TEXT_POS_FROM_MARKER (start, w->start);
4916 /* Scrolling a minibuffer window via scroll bar when the echo area
4917 shows long text sometimes resets the minibuffer contents behind
4918 our backs. */
4919 if (CHARPOS (start) > ZV)
4920 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4922 /* If PT is not visible in WINDOW, move back one half of
4923 the screen. Allow PT to be partially visible, otherwise
4924 something like (scroll-down 1) with PT in the line before
4925 the partially visible one would recenter. */
4927 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4929 itdata = bidi_shelve_cache ();
4930 /* Move backward half the height of the window. Performance note:
4931 vmotion used here is about 10% faster, but would give wrong
4932 results for variable height lines. */
4933 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4934 it.current_y = it.last_visible_y;
4935 move_it_vertically_backward (&it, window_box_height (w) / 2);
4937 /* The function move_iterator_vertically may move over more than
4938 the specified y-distance. If it->w is small, e.g. a
4939 mini-buffer window, we may end up in front of the window's
4940 display area. This is the case when Start displaying at the
4941 start of the line containing PT in this case. */
4942 if (it.current_y <= 0)
4944 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4945 move_it_vertically_backward (&it, 0);
4946 it.current_y = 0;
4949 start = it.current.pos;
4950 bidi_unshelve_cache (itdata, 0);
4952 else if (auto_window_vscroll_p)
4954 if (rtop || rbot) /* partially visible */
4956 int px;
4957 int dy = frame_line_height;
4958 if (whole)
4959 dy = max ((window_box_height (w)
4960 - next_screen_context_lines * dy),
4961 dy);
4962 dy *= n;
4964 if (n < 0)
4966 /* Only vscroll backwards if already vscrolled forwards. */
4967 if (w->vscroll < 0 && rtop > 0)
4969 px = max (0, -w->vscroll - min (rtop, -dy));
4970 Fset_window_vscroll (window, make_number (px), Qt);
4971 return;
4974 if (n > 0)
4976 /* Do vscroll if already vscrolled or only display line. */
4977 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4979 px = max (0, -w->vscroll + min (rbot, dy));
4980 Fset_window_vscroll (window, make_number (px), Qt);
4981 return;
4984 /* Maybe modify window start instead of scrolling. */
4985 if (rbot > 0 || w->vscroll < 0)
4987 ptrdiff_t spos;
4989 Fset_window_vscroll (window, make_number (0), Qt);
4990 /* If there are other text lines above the current row,
4991 move window start to current row. Else to next row. */
4992 if (rbot > 0)
4993 spos = XINT (Fline_beginning_position (Qnil));
4994 else
4995 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4996 set_marker_restricted (w->start, make_number (spos),
4997 w->contents);
4998 w->start_at_line_beg = 1;
4999 w->update_mode_line = 1;
5000 /* Set force_start so that redisplay_window will run the
5001 window-scroll-functions. */
5002 w->force_start = 1;
5003 return;
5007 /* Cancel previous vscroll. */
5008 Fset_window_vscroll (window, make_number (0), Qt);
5011 itdata = bidi_shelve_cache ();
5012 /* If scroll_preserve_screen_position is non-nil, we try to set
5013 point in the same window line as it is now, so get that line. */
5014 if (!NILP (Vscroll_preserve_screen_position))
5016 /* We preserve the goal pixel coordinate across consecutive
5017 calls to scroll-up, scroll-down and other commands that
5018 have the `scroll-command' property. This avoids the
5019 possibility of point becoming "stuck" on a tall line when
5020 scrolling by one line. */
5021 if (window_scroll_pixel_based_preserve_y < 0
5022 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5023 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5025 start_display (&it, w, start);
5026 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5027 window_scroll_pixel_based_preserve_y = it.current_y;
5028 window_scroll_pixel_based_preserve_x = it.current_x;
5031 else
5032 window_scroll_pixel_based_preserve_y
5033 = window_scroll_pixel_based_preserve_x = -1;
5035 /* Move iterator it from start the specified distance forward or
5036 backward. The result is the new window start. */
5037 start_display (&it, w, start);
5038 if (whole)
5040 ptrdiff_t start_pos = IT_CHARPOS (it);
5041 int dy = frame_line_height;
5042 dy = max ((window_box_height (w)
5043 - next_screen_context_lines * dy),
5044 dy) * n;
5046 /* Note that move_it_vertically always moves the iterator to the
5047 start of a line. So, if the last line doesn't have a newline,
5048 we would end up at the start of the line ending at ZV. */
5049 if (dy <= 0)
5051 move_it_vertically_backward (&it, -dy);
5052 /* Ensure we actually do move, e.g. in case we are currently
5053 looking at an image that is taller that the window height. */
5054 while (start_pos == IT_CHARPOS (it)
5055 && start_pos > BEGV)
5056 move_it_by_lines (&it, -1);
5058 else if (dy > 0)
5060 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5061 MOVE_TO_POS | MOVE_TO_Y);
5062 /* Ensure we actually do move, e.g. in case we are currently
5063 looking at an image that is taller that the window height. */
5064 while (start_pos == IT_CHARPOS (it)
5065 && start_pos < ZV)
5066 move_it_by_lines (&it, 1);
5069 else
5070 move_it_by_lines (&it, n);
5072 /* We failed if we find ZV is already on the screen (scrolling up,
5073 means there's nothing past the end), or if we can't start any
5074 earlier (scrolling down, means there's nothing past the top). */
5075 if ((n > 0 && IT_CHARPOS (it) == ZV)
5076 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5078 if (IT_CHARPOS (it) == ZV)
5080 if (it.current_y < it.last_visible_y
5081 && (it.current_y + it.max_ascent + it.max_descent
5082 > it.last_visible_y))
5084 /* The last line was only partially visible, make it fully
5085 visible. */
5086 w->vscroll = (it.last_visible_y
5087 - it.current_y + it.max_ascent + it.max_descent);
5088 adjust_frame_glyphs (it.f);
5090 else
5092 bidi_unshelve_cache (itdata, 0);
5093 if (noerror)
5094 return;
5095 else if (n < 0) /* could happen with empty buffers */
5096 xsignal0 (Qbeginning_of_buffer);
5097 else
5098 xsignal0 (Qend_of_buffer);
5101 else
5103 if (w->vscroll != 0)
5104 /* The first line was only partially visible, make it fully
5105 visible. */
5106 w->vscroll = 0;
5107 else
5109 bidi_unshelve_cache (itdata, 0);
5110 if (noerror)
5111 return;
5112 else
5113 xsignal0 (Qbeginning_of_buffer);
5117 /* If control gets here, then we vscrolled. */
5119 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5121 /* Don't try to change the window start below. */
5122 vscrolled = 1;
5125 if (! vscrolled)
5127 ptrdiff_t pos = IT_CHARPOS (it);
5128 ptrdiff_t bytepos;
5130 /* If in the middle of a multi-glyph character move forward to
5131 the next character. */
5132 if (in_display_vector_p (&it))
5134 ++pos;
5135 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5138 /* Set the window start, and set up the window for redisplay. */
5139 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5140 IT_BYTEPOS (it));
5141 bytepos = marker_byte_position (w->start);
5142 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5143 w->update_mode_line = 1;
5144 /* Set force_start so that redisplay_window will run the
5145 window-scroll-functions. */
5146 w->force_start = 1;
5149 /* The rest of this function uses current_y in a nonstandard way,
5150 not including the height of the header line if any. */
5151 it.current_y = it.vpos = 0;
5153 /* Move PT out of scroll margins.
5154 This code wants current_y to be zero at the window start position
5155 even if there is a header line. */
5156 window_total_lines
5157 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5158 this_scroll_margin = max (0, scroll_margin);
5159 this_scroll_margin
5160 = min (this_scroll_margin, window_total_lines / 4);
5161 this_scroll_margin *= frame_line_height;
5163 if (n > 0)
5165 /* We moved the window start towards ZV, so PT may be now
5166 in the scroll margin at the top. */
5167 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5168 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5169 && (NILP (Vscroll_preserve_screen_position)
5170 || EQ (Vscroll_preserve_screen_position, Qt)))
5171 /* We found PT at a legitimate height. Leave it alone. */
5173 else if (window_scroll_pixel_based_preserve_y >= 0)
5175 /* If we have a header line, take account of it.
5176 This is necessary because we set it.current_y to 0, above. */
5177 move_it_to (&it, -1,
5178 window_scroll_pixel_based_preserve_x,
5179 window_scroll_pixel_based_preserve_y
5180 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5181 -1, MOVE_TO_Y | MOVE_TO_X);
5182 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5184 else
5186 while (it.current_y < this_scroll_margin)
5188 int prev = it.current_y;
5189 move_it_by_lines (&it, 1);
5190 if (prev == it.current_y)
5191 break;
5193 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5196 else if (n < 0)
5198 ptrdiff_t charpos, bytepos;
5199 int partial_p;
5201 /* Save our position, for the
5202 window_scroll_pixel_based_preserve_y case. */
5203 charpos = IT_CHARPOS (it);
5204 bytepos = IT_BYTEPOS (it);
5206 /* We moved the window start towards BEGV, so PT may be now
5207 in the scroll margin at the bottom. */
5208 move_it_to (&it, PT, -1,
5209 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5210 - this_scroll_margin - 1),
5212 MOVE_TO_POS | MOVE_TO_Y);
5214 /* Save our position, in case it's correct. */
5215 charpos = IT_CHARPOS (it);
5216 bytepos = IT_BYTEPOS (it);
5218 /* If PT is in the screen line at the last fully visible line,
5219 move_it_to will stop at X = 0 in that line, because the
5220 required Y coordinate is reached there. See if we can get to
5221 PT without descending lower in Y, and if we can, it means we
5222 reached PT before the scroll margin. */
5223 if (charpos != PT)
5225 struct it it2;
5226 void *it_data;
5228 it2 = it;
5229 it_data = bidi_shelve_cache ();
5230 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5231 if (IT_CHARPOS (it) == PT && it.current_y == it2.current_y)
5233 charpos = IT_CHARPOS (it);
5234 bytepos = IT_BYTEPOS (it);
5235 bidi_unshelve_cache (it_data, 1);
5237 else
5239 it = it2;
5240 bidi_unshelve_cache (it_data, 0);
5244 /* See if point is on a partially visible line at the end. */
5245 if (it.what == IT_EOB)
5246 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5247 else
5249 move_it_by_lines (&it, 1);
5250 partial_p = it.current_y > it.last_visible_y;
5253 if (charpos == PT && !partial_p
5254 && (NILP (Vscroll_preserve_screen_position)
5255 || EQ (Vscroll_preserve_screen_position, Qt)))
5256 /* We found PT before we found the display margin, so PT is ok. */
5258 else if (window_scroll_pixel_based_preserve_y >= 0)
5260 SET_TEXT_POS_FROM_MARKER (start, w->start);
5261 start_display (&it, w, start);
5262 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5263 here because we called start_display again and did not
5264 alter it.current_y this time. */
5265 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5266 window_scroll_pixel_based_preserve_y, -1,
5267 MOVE_TO_Y | MOVE_TO_X);
5268 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5270 else
5272 if (partial_p)
5273 /* The last line was only partially visible, so back up two
5274 lines to make sure we're on a fully visible line. */
5276 move_it_by_lines (&it, -2);
5277 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5279 else
5280 /* No, the position we saved is OK, so use it. */
5281 SET_PT_BOTH (charpos, bytepos);
5284 bidi_unshelve_cache (itdata, 0);
5288 /* Implementation of window_scroll that works based on screen lines.
5289 See the comment of window_scroll for parameter descriptions. */
5291 static void
5292 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5294 register struct window *w = XWINDOW (window);
5295 /* Fvertical_motion enters redisplay, which can trigger
5296 fontification, which in turn can modify buffer text (e.g., if the
5297 fontification functions replace escape sequences with faces, as
5298 in `grep-mode-font-lock-keywords'). So we use a marker to record
5299 the old point position, to prevent crashes in SET_PT_BOTH. */
5300 Lisp_Object opoint_marker = Fpoint_marker ();
5301 register ptrdiff_t pos, pos_byte;
5302 register int ht = window_internal_height (w);
5303 register Lisp_Object tem;
5304 bool lose;
5305 Lisp_Object bolp;
5306 ptrdiff_t startpos = marker_position (w->start);
5307 ptrdiff_t startbyte = marker_byte_position (w->start);
5308 Lisp_Object original_pos = Qnil;
5310 /* If scrolling screen-fulls, compute the number of lines to
5311 scroll from the window's height. */
5312 if (whole)
5313 n *= max (1, ht - next_screen_context_lines);
5315 if (!NILP (Vscroll_preserve_screen_position))
5317 if (window_scroll_preserve_vpos <= 0
5318 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5319 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5321 struct position posit
5322 = *compute_motion (startpos, startbyte, 0, 0, 0,
5323 PT, ht, 0, -1, w->hscroll, 0, w);
5324 window_scroll_preserve_vpos = posit.vpos;
5325 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5328 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5329 make_number (window_scroll_preserve_vpos));
5332 XSETFASTINT (tem, PT);
5333 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5335 if (NILP (tem))
5337 Fvertical_motion (make_number (- (ht / 2)), window);
5338 startpos = PT;
5339 startbyte = PT_BYTE;
5342 SET_PT_BOTH (startpos, startbyte);
5343 lose = n < 0 && PT == BEGV;
5344 Fvertical_motion (make_number (n), window);
5345 pos = PT;
5346 pos_byte = PT_BYTE;
5347 bolp = Fbolp ();
5348 SET_PT_BOTH (marker_position (opoint_marker),
5349 marker_byte_position (opoint_marker));
5351 if (lose)
5353 if (noerror)
5354 return;
5355 else
5356 xsignal0 (Qbeginning_of_buffer);
5359 if (pos < ZV)
5361 /* Don't use a scroll margin that is negative or too large. */
5362 int this_scroll_margin =
5363 max (0, min (scroll_margin, w->total_lines / 4));
5365 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5366 w->start_at_line_beg = !NILP (bolp);
5367 w->update_mode_line = 1;
5368 /* Set force_start so that redisplay_window will run
5369 the window-scroll-functions. */
5370 w->force_start = 1;
5372 if (!NILP (Vscroll_preserve_screen_position)
5373 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5375 SET_PT_BOTH (pos, pos_byte);
5376 Fvertical_motion (original_pos, window);
5378 /* If we scrolled forward, put point enough lines down
5379 that it is outside the scroll margin. */
5380 else if (n > 0)
5382 int top_margin;
5384 if (this_scroll_margin > 0)
5386 SET_PT_BOTH (pos, pos_byte);
5387 Fvertical_motion (make_number (this_scroll_margin), window);
5388 top_margin = PT;
5390 else
5391 top_margin = pos;
5393 if (top_margin <= marker_position (opoint_marker))
5394 SET_PT_BOTH (marker_position (opoint_marker),
5395 marker_byte_position (opoint_marker));
5396 else if (!NILP (Vscroll_preserve_screen_position))
5398 SET_PT_BOTH (pos, pos_byte);
5399 Fvertical_motion (original_pos, window);
5401 else
5402 SET_PT (top_margin);
5404 else if (n < 0)
5406 int bottom_margin;
5408 /* If we scrolled backward, put point near the end of the window
5409 but not within the scroll margin. */
5410 SET_PT_BOTH (pos, pos_byte);
5411 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5412 if (XFASTINT (tem) == ht - this_scroll_margin)
5413 bottom_margin = PT;
5414 else
5415 bottom_margin = PT + 1;
5417 if (bottom_margin > marker_position (opoint_marker))
5418 SET_PT_BOTH (marker_position (opoint_marker),
5419 marker_byte_position (opoint_marker));
5420 else
5422 if (!NILP (Vscroll_preserve_screen_position))
5424 SET_PT_BOTH (pos, pos_byte);
5425 Fvertical_motion (original_pos, window);
5427 else
5428 Fvertical_motion (make_number (-1), window);
5432 else
5434 if (noerror)
5435 return;
5436 else
5437 xsignal0 (Qend_of_buffer);
5442 /* Scroll selected_window up or down. If N is nil, scroll a
5443 screen-full which is defined as the height of the window minus
5444 next_screen_context_lines. If N is the symbol `-', scroll.
5445 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5446 up. This is the guts of Fscroll_up and Fscroll_down. */
5448 static void
5449 scroll_command (Lisp_Object n, int direction)
5451 ptrdiff_t count = SPECPDL_INDEX ();
5453 eassert (eabs (direction) == 1);
5455 /* If selected window's buffer isn't current, make it current for
5456 the moment. But don't screw up if window_scroll gets an error. */
5457 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5459 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5460 Fset_buffer (XWINDOW (selected_window)->contents);
5463 if (NILP (n))
5464 window_scroll (selected_window, direction, 1, 0);
5465 else if (EQ (n, Qminus))
5466 window_scroll (selected_window, -direction, 1, 0);
5467 else
5469 n = Fprefix_numeric_value (n);
5470 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5473 unbind_to (count, Qnil);
5476 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5477 doc: /* Scroll text of selected window upward ARG lines.
5478 If ARG is omitted or nil, scroll upward by a near full screen.
5479 A near full screen is `next-screen-context-lines' less than a full screen.
5480 Negative ARG means scroll downward.
5481 If ARG is the atom `-', scroll downward by nearly full screen.
5482 When calling from a program, supply as argument a number, nil, or `-'. */)
5483 (Lisp_Object arg)
5485 scroll_command (arg, 1);
5486 return Qnil;
5489 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5490 doc: /* Scroll text of selected window down ARG lines.
5491 If ARG is omitted or nil, scroll down by a near full screen.
5492 A near full screen is `next-screen-context-lines' less than a full screen.
5493 Negative ARG means scroll upward.
5494 If ARG is the atom `-', scroll upward by nearly full screen.
5495 When calling from a program, supply as argument a number, nil, or `-'. */)
5496 (Lisp_Object arg)
5498 scroll_command (arg, -1);
5499 return Qnil;
5502 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5503 doc: /* Return the other window for \"other window scroll\" commands.
5504 If `other-window-scroll-buffer' is non-nil, a window
5505 showing that buffer is used.
5506 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5507 specifies the window. This takes precedence over
5508 `other-window-scroll-buffer'. */)
5509 (void)
5511 Lisp_Object window;
5513 if (MINI_WINDOW_P (XWINDOW (selected_window))
5514 && !NILP (Vminibuf_scroll_window))
5515 window = Vminibuf_scroll_window;
5516 /* If buffer is specified and live, scroll that buffer. */
5517 else if (!NILP (Vother_window_scroll_buffer)
5518 && BUFFERP (Vother_window_scroll_buffer)
5519 && BUFFER_LIVE_P (XBUFFER (Vother_window_scroll_buffer)))
5521 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5522 if (NILP (window))
5523 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5525 else
5527 /* Nothing specified; look for a neighboring window on the same
5528 frame. */
5529 window = Fnext_window (selected_window, Qnil, Qnil);
5531 if (EQ (window, selected_window))
5532 /* That didn't get us anywhere; look for a window on another
5533 visible frame. */
5535 window = Fnext_window (window, Qnil, Qt);
5536 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5537 && ! EQ (window, selected_window));
5540 CHECK_LIVE_WINDOW (window);
5542 if (EQ (window, selected_window))
5543 error ("There is no other window");
5545 return window;
5548 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5549 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5550 A near full screen is `next-screen-context-lines' less than a full screen.
5551 The next window is the one below the current one; or the one at the top
5552 if the current one is at the bottom. Negative ARG means scroll downward.
5553 If ARG is the atom `-', scroll downward by nearly full screen.
5554 When calling from a program, supply as argument a number, nil, or `-'.
5556 If `other-window-scroll-buffer' is non-nil, scroll the window
5557 showing that buffer, popping the buffer up if necessary.
5558 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5559 specifies the window to scroll. This takes precedence over
5560 `other-window-scroll-buffer'. */)
5561 (Lisp_Object arg)
5563 Lisp_Object window;
5564 struct window *w;
5565 ptrdiff_t count = SPECPDL_INDEX ();
5567 window = Fother_window_for_scrolling ();
5568 w = XWINDOW (window);
5570 /* Don't screw up if window_scroll gets an error. */
5571 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5573 Fset_buffer (w->contents);
5574 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5576 if (NILP (arg))
5577 window_scroll (window, 1, 1, 1);
5578 else if (EQ (arg, Qminus))
5579 window_scroll (window, -1, 1, 1);
5580 else
5582 if (CONSP (arg))
5583 arg = XCAR (arg);
5584 CHECK_NUMBER (arg);
5585 window_scroll (window, XINT (arg), 0, 1);
5588 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5589 unbind_to (count, Qnil);
5591 return Qnil;
5594 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5595 doc: /* Scroll selected window display ARG columns left.
5596 Default for ARG is window width minus 2.
5597 Value is the total amount of leftward horizontal scrolling in
5598 effect after the change.
5599 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5600 lower bound for automatic scrolling, i.e. automatic scrolling
5601 will not scroll a window to a column less than the value returned
5602 by this function. This happens in an interactive call. */)
5603 (register Lisp_Object arg, Lisp_Object set_minimum)
5605 struct window *w = XWINDOW (selected_window);
5606 EMACS_INT requested_arg = (NILP (arg)
5607 ? window_body_width (w, 0) - 2
5608 : XINT (Fprefix_numeric_value (arg)));
5609 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5611 if (!NILP (set_minimum))
5612 w->min_hscroll = w->hscroll;
5614 return result;
5617 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5618 doc: /* Scroll selected window display ARG columns right.
5619 Default for ARG is window width minus 2.
5620 Value is the total amount of leftward horizontal scrolling in
5621 effect after the change.
5622 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5623 lower bound for automatic scrolling, i.e. automatic scrolling
5624 will not scroll a window to a column less than the value returned
5625 by this function. This happens in an interactive call. */)
5626 (register Lisp_Object arg, Lisp_Object set_minimum)
5628 struct window *w = XWINDOW (selected_window);
5629 EMACS_INT requested_arg = (NILP (arg)
5630 ? window_body_width (w, 0) - 2
5631 : XINT (Fprefix_numeric_value (arg)));
5632 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5634 if (!NILP (set_minimum))
5635 w->min_hscroll = w->hscroll;
5637 return result;
5640 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5641 doc: /* Return the window which was selected when entering the minibuffer.
5642 Returns nil, if selected window is not a minibuffer window. */)
5643 (void)
5645 if (minibuf_level > 0
5646 && MINI_WINDOW_P (XWINDOW (selected_window))
5647 && WINDOW_LIVE_P (minibuf_selected_window))
5648 return minibuf_selected_window;
5650 return Qnil;
5653 /* Value is the number of lines actually displayed in window W,
5654 as opposed to its height. */
5656 static int
5657 displayed_window_lines (struct window *w)
5659 struct it it;
5660 struct text_pos start;
5661 int height = window_box_height (w);
5662 struct buffer *old_buffer;
5663 int bottom_y;
5664 void *itdata = NULL;
5666 if (XBUFFER (w->contents) != current_buffer)
5668 old_buffer = current_buffer;
5669 set_buffer_internal (XBUFFER (w->contents));
5671 else
5672 old_buffer = NULL;
5674 /* In case W->start is out of the accessible range, do something
5675 reasonable. This happens in Info mode when Info-scroll-down
5676 calls (recenter -1) while W->start is 1. */
5677 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5679 itdata = bidi_shelve_cache ();
5680 start_display (&it, w, start);
5681 move_it_vertically (&it, height);
5682 bottom_y = line_bottom_y (&it);
5683 bidi_unshelve_cache (itdata, 0);
5685 /* rms: On a non-window display,
5686 the value of it.vpos at the bottom of the screen
5687 seems to be 1 larger than window_box_height (w).
5688 This kludge fixes a bug whereby (move-to-window-line -1)
5689 when ZV is on the last screen line
5690 moves to the previous screen line instead of the last one. */
5691 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5692 height++;
5694 /* Add in empty lines at the bottom of the window. */
5695 if (bottom_y < height)
5697 int uy = FRAME_LINE_HEIGHT (it.f);
5698 it.vpos += (height - bottom_y + uy - 1) / uy;
5701 if (old_buffer)
5702 set_buffer_internal (old_buffer);
5704 return it.vpos;
5708 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5709 doc: /* Center point in selected window and maybe redisplay frame.
5710 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5711 relative to the selected window. If ARG is negative, it counts up from the
5712 bottom of the window. (ARG should be less than the height of the window.)
5714 If ARG is omitted or nil, then recenter with point on the middle line of
5715 the selected window; if the variable `recenter-redisplay' is non-nil,
5716 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5717 is set to `grow-only', this resets the tool-bar's height to the minimum
5718 height needed); if `recenter-redisplay' has the special value `tty',
5719 then only tty frames are redrawn.
5721 Just C-u as prefix means put point in the center of the window
5722 and redisplay normally--don't erase and redraw the frame. */)
5723 (register Lisp_Object arg)
5725 struct window *w = XWINDOW (selected_window);
5726 struct buffer *buf = XBUFFER (w->contents);
5727 struct buffer *obuf = current_buffer;
5728 bool center_p = 0;
5729 ptrdiff_t charpos, bytepos;
5730 EMACS_INT iarg IF_LINT (= 0);
5731 int this_scroll_margin;
5733 /* If redisplay is suppressed due to an error, try again. */
5734 obuf->display_error_modiff = 0;
5736 if (NILP (arg))
5738 if (!NILP (Vrecenter_redisplay)
5739 && (!EQ (Vrecenter_redisplay, Qtty)
5740 || !NILP (Ftty_type (selected_frame))))
5742 ptrdiff_t i;
5744 /* Invalidate pixel data calculated for all compositions. */
5745 for (i = 0; i < n_compositions; i++)
5746 composition_table[i]->font = NULL;
5747 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5748 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5749 #endif
5750 Fredraw_frame (WINDOW_FRAME (w));
5751 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5754 center_p = 1;
5756 else if (CONSP (arg)) /* Just C-u. */
5757 center_p = 1;
5758 else
5760 arg = Fprefix_numeric_value (arg);
5761 CHECK_NUMBER (arg);
5762 iarg = XINT (arg);
5765 set_buffer_internal (buf);
5767 /* Do this after making BUF current
5768 in case scroll_margin is buffer-local. */
5769 this_scroll_margin =
5770 max (0, min (scroll_margin, w->total_lines / 4));
5772 /* Handle centering on a graphical frame specially. Such frames can
5773 have variable-height lines and centering point on the basis of
5774 line counts would lead to strange effects. */
5775 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5777 if (center_p)
5779 struct it it;
5780 struct text_pos pt;
5781 void *itdata = bidi_shelve_cache ();
5783 SET_TEXT_POS (pt, PT, PT_BYTE);
5784 start_display (&it, w, pt);
5785 move_it_vertically_backward (&it, window_box_height (w) / 2);
5786 charpos = IT_CHARPOS (it);
5787 bytepos = IT_BYTEPOS (it);
5788 bidi_unshelve_cache (itdata, 0);
5790 else if (iarg < 0)
5792 struct it it;
5793 struct text_pos pt;
5794 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5795 int extra_line_spacing;
5796 int h = window_box_height (w);
5797 void *itdata = bidi_shelve_cache ();
5799 iarg = - max (-iarg, this_scroll_margin);
5801 SET_TEXT_POS (pt, PT, PT_BYTE);
5802 start_display (&it, w, pt);
5804 /* Be sure we have the exact height of the full line containing PT. */
5805 move_it_by_lines (&it, 0);
5807 /* The amount of pixels we have to move back is the window
5808 height minus what's displayed in the line containing PT,
5809 and the lines below. */
5810 it.current_y = 0;
5811 it.vpos = 0;
5812 move_it_by_lines (&it, nlines);
5814 if (it.vpos == nlines)
5815 h -= it.current_y;
5816 else
5818 /* Last line has no newline */
5819 h -= line_bottom_y (&it);
5820 it.vpos++;
5823 /* Don't reserve space for extra line spacing of last line. */
5824 extra_line_spacing = it.max_extra_line_spacing;
5826 /* If we can't move down NLINES lines because we hit
5827 the end of the buffer, count in some empty lines. */
5828 if (it.vpos < nlines)
5830 nlines -= it.vpos;
5831 extra_line_spacing = it.extra_line_spacing;
5832 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5834 if (h <= 0)
5836 bidi_unshelve_cache (itdata, 0);
5837 return Qnil;
5840 /* Now find the new top line (starting position) of the window. */
5841 start_display (&it, w, pt);
5842 it.current_y = 0;
5843 move_it_vertically_backward (&it, h);
5845 /* If extra line spacing is present, we may move too far
5846 back. This causes the last line to be only partially
5847 visible (which triggers redisplay to recenter that line
5848 in the middle), so move forward.
5849 But ignore extra line spacing on last line, as it is not
5850 considered to be part of the visible height of the line.
5852 h += extra_line_spacing;
5853 while (-it.current_y > h)
5854 move_it_by_lines (&it, 1);
5856 charpos = IT_CHARPOS (it);
5857 bytepos = IT_BYTEPOS (it);
5859 bidi_unshelve_cache (itdata, 0);
5861 else
5863 struct position pos;
5865 iarg = max (iarg, this_scroll_margin);
5867 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5868 charpos = pos.bufpos;
5869 bytepos = pos.bytepos;
5872 else
5874 struct position pos;
5875 int ht = window_internal_height (w);
5877 if (center_p)
5878 iarg = ht / 2;
5879 else if (iarg < 0)
5880 iarg += ht;
5882 /* Don't let it get into the margin at either top or bottom. */
5883 iarg = clip_to_bounds (this_scroll_margin, iarg,
5884 ht - this_scroll_margin - 1);
5886 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5887 charpos = pos.bufpos;
5888 bytepos = pos.bytepos;
5891 /* Set the new window start. */
5892 set_marker_both (w->start, w->contents, charpos, bytepos);
5893 w->window_end_valid = 0;
5895 w->optional_new_start = 1;
5897 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5898 FETCH_BYTE (bytepos - 1) == '\n');
5900 wset_redisplay (w);
5902 set_buffer_internal (obuf);
5903 return Qnil;
5906 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5907 0, 1, 0,
5908 doc: /* Return the width in columns of the text display area of WINDOW.
5909 WINDOW must be a live window and defaults to the selected one.
5911 The returned width does not include dividers, scrollbars, margins,
5912 fringes, nor any partial-width columns at the right of the text
5913 area. */)
5914 (Lisp_Object window)
5916 struct window *w = decode_live_window (window);
5918 return make_number (window_box_width (w, TEXT_AREA)
5919 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5922 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5923 0, 1, 0,
5924 doc: /* Return the height in lines of the text display area of WINDOW.
5925 WINDOW must be a live window and defaults to the selected one.
5927 The returned height does not include dividers, the mode line, any header
5928 line, nor any partial-height lines at the bottom of the text area. */)
5929 (Lisp_Object window)
5931 struct window *w = decode_live_window (window);
5933 return make_number (window_box_height (w)
5934 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5937 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5938 1, 1, "P",
5939 doc: /* Position point relative to window.
5940 ARG nil means position point at center of window.
5941 Else, ARG specifies vertical position within the window;
5942 zero means top of window, negative means relative to bottom of window. */)
5943 (Lisp_Object arg)
5945 struct window *w = XWINDOW (selected_window);
5946 int lines, start;
5947 Lisp_Object window;
5948 #if 0
5949 int this_scroll_margin;
5950 #endif
5952 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5953 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5954 when passed below to set_marker_both. */
5955 error ("move-to-window-line called from unrelated buffer");
5957 window = selected_window;
5958 start = marker_position (w->start);
5959 if (start < BEGV || start > ZV)
5961 int height = window_internal_height (w);
5962 Fvertical_motion (make_number (- (height / 2)), window);
5963 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5964 w->start_at_line_beg = !NILP (Fbolp ());
5965 w->force_start = 1;
5967 else
5968 Fgoto_char (w->start);
5970 lines = displayed_window_lines (w);
5972 #if 0
5973 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5974 #endif
5976 if (NILP (arg))
5977 XSETFASTINT (arg, lines / 2);
5978 else
5980 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5982 if (iarg < 0)
5983 iarg = iarg + lines;
5985 #if 0 /* This code would prevent move-to-window-line from moving point
5986 to a place inside the scroll margins (which would cause the
5987 next redisplay to scroll). I wrote this code, but then concluded
5988 it is probably better not to install it. However, it is here
5989 inside #if 0 so as not to lose it. -- rms. */
5991 /* Don't let it get into the margin at either top or bottom. */
5992 iarg = max (iarg, this_scroll_margin);
5993 iarg = min (iarg, lines - this_scroll_margin - 1);
5994 #endif
5996 arg = make_number (iarg);
5999 /* Skip past a partially visible first line. */
6000 if (w->vscroll)
6001 XSETINT (arg, XINT (arg) + 1);
6003 return Fvertical_motion (arg, window);
6008 /***********************************************************************
6009 Window Configuration
6010 ***********************************************************************/
6012 struct save_window_data
6014 struct vectorlike_header header;
6015 Lisp_Object selected_frame;
6016 Lisp_Object current_window;
6017 Lisp_Object current_buffer;
6018 Lisp_Object minibuf_scroll_window;
6019 Lisp_Object minibuf_selected_window;
6020 Lisp_Object root_window;
6021 Lisp_Object focus_frame;
6022 /* A vector, each of whose elements is a struct saved_window
6023 for one window. */
6024 Lisp_Object saved_windows;
6026 /* All fields above are traced by the GC.
6027 From `frame-cols' down, the fields are ignored by the GC. */
6028 /* We should be able to do without the following two. */
6029 int frame_cols, frame_lines;
6030 /* These two should get eventually replaced by their pixel
6031 counterparts. */
6032 int frame_menu_bar_lines, frame_tool_bar_lines;
6033 int frame_text_width, frame_text_height;
6034 /* These are currently unused. We need them as soon as we convert
6035 to pixels. */
6036 int frame_menu_bar_height, frame_tool_bar_height;
6039 /* This is saved as a Lisp_Vector */
6040 struct saved_window
6042 struct vectorlike_header header;
6044 Lisp_Object window, buffer, start, pointm, mark;
6045 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
6046 Lisp_Object left_col, top_line, total_cols, total_lines;
6047 Lisp_Object normal_cols, normal_lines;
6048 Lisp_Object hscroll, min_hscroll;
6049 Lisp_Object parent, prev;
6050 Lisp_Object start_at_line_beg;
6051 Lisp_Object display_table;
6052 Lisp_Object left_margin_cols, right_margin_cols;
6053 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6054 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
6055 Lisp_Object combination_limit, window_parameters;
6058 #define SAVED_WINDOW_N(swv,n) \
6059 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6061 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6062 doc: /* Return t if OBJECT is a window-configuration object. */)
6063 (Lisp_Object object)
6065 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6068 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6069 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6070 (Lisp_Object config)
6072 register struct save_window_data *data;
6073 struct Lisp_Vector *saved_windows;
6075 CHECK_WINDOW_CONFIGURATION (config);
6077 data = (struct save_window_data *) XVECTOR (config);
6078 saved_windows = XVECTOR (data->saved_windows);
6079 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6082 /* From Chong's unwind_create_frame_1. */
6083 static void
6084 unwind_change_frame (Lisp_Object val)
6086 inhibit_lisp_code = val;
6089 DEFUN ("set-window-configuration", Fset_window_configuration,
6090 Sset_window_configuration, 1, 1, 0,
6091 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6092 CONFIGURATION must be a value previously returned
6093 by `current-window-configuration' (which see).
6094 If CONFIGURATION was made from a frame that is now deleted,
6095 only frame-independent values can be restored. In this case,
6096 the return value is nil. Otherwise the value is t. */)
6097 (Lisp_Object configuration)
6099 register struct save_window_data *data;
6100 struct Lisp_Vector *saved_windows;
6101 Lisp_Object new_current_buffer;
6102 Lisp_Object frame;
6103 struct frame *f;
6104 ptrdiff_t old_point = -1;
6106 CHECK_WINDOW_CONFIGURATION (configuration);
6108 data = (struct save_window_data *) XVECTOR (configuration);
6109 saved_windows = XVECTOR (data->saved_windows);
6111 new_current_buffer = data->current_buffer;
6112 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6113 new_current_buffer = Qnil;
6114 else
6116 if (XBUFFER (new_current_buffer) == current_buffer)
6117 /* The code further down "preserves point" by saving here PT in
6118 old_point and then setting it later back into PT. When the
6119 current-selected-window and the final-selected-window both show
6120 the current buffer, this suffers from the problem that the
6121 current PT is the window-point of the current-selected-window,
6122 while the final PT is the point of the final-selected-window, so
6123 this copy from one PT to the other would end up moving the
6124 window-point of the final-selected-window to the window-point of
6125 the current-selected-window. So we have to be careful which
6126 point of the current-buffer we copy into old_point. */
6127 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6128 && WINDOWP (selected_window)
6129 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6130 && !EQ (selected_window, data->current_window))
6131 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6132 else
6133 old_point = PT;
6134 else
6135 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6136 point in new_current_buffer as of the last time this buffer was
6137 used. This can be non-deterministic since it can be changed by
6138 things like jit-lock by mere temporary selection of some random
6139 window that happens to show this buffer.
6140 So if possible we want this arbitrary choice of "which point" to
6141 be the one from the to-be-selected-window so as to prevent this
6142 window's cursor from being copied from another window. */
6143 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6144 /* If current_window = selected_window, its point is in BUF_PT. */
6145 && !EQ (selected_window, data->current_window))
6146 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6147 else
6148 old_point = BUF_PT (XBUFFER (new_current_buffer));
6151 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6152 f = XFRAME (frame);
6154 /* If f is a dead frame, don't bother rebuilding its window tree.
6155 However, there is other stuff we should still try to do below. */
6156 if (FRAME_LIVE_P (f))
6158 Lisp_Object window;
6159 Lisp_Object dead_windows = Qnil;
6160 register Lisp_Object tem, par, pers;
6161 register struct window *w;
6162 register struct saved_window *p;
6163 struct window *root_window;
6164 struct window **leaf_windows;
6165 int n_leaf_windows;
6166 ptrdiff_t k;
6167 int i, n;
6168 ptrdiff_t count = SPECPDL_INDEX ();
6169 /* If the frame has been resized since this window configuration was
6170 made, we change the frame to the size specified in the
6171 configuration, restore the configuration, and then resize it
6172 back. We keep track of the prevailing height in these variables. */
6173 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6174 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6175 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6176 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6177 /* int previous_frame_lines = FRAME_LINES (f); */
6178 /* int previous_frame_cols = FRAME_COLS (f); */
6179 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6180 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6182 /* Don't do this within the main loop below: This may call Lisp
6183 code and is thus potentially unsafe while input is blocked. */
6184 for (k = 0; k < saved_windows->header.size; k++)
6186 p = SAVED_WINDOW_N (saved_windows, k);
6187 window = p->window;
6188 w = XWINDOW (window);
6189 if (BUFFERP (w->contents)
6190 && !EQ (w->contents, p->buffer)
6191 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6192 /* If a window we restore gets another buffer, record the
6193 window's old buffer. */
6194 call1 (Qrecord_window_buffer, window);
6197 /* Don't run lisp in the following segment since the frame is in a
6198 completely inconsistent state. See Bug#16207. */
6199 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6200 inhibit_lisp_code = Qt;
6201 /* The mouse highlighting code could get screwed up
6202 if it runs during this. */
6203 block_input ();
6205 if (data->frame_text_width != previous_frame_text_width
6206 || data->frame_text_height != previous_frame_text_height)
6207 change_frame_size (f, data->frame_text_width,
6208 data->frame_text_height, 0, 0, 0, 1);
6210 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6212 #ifdef HAVE_WINDOW_SYSTEM
6213 if (FRAME_WINDOW_P (f))
6214 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6215 make_number (0));
6216 else /* TTY or MSDOS */
6217 #endif
6218 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6219 make_number (0));
6221 #ifdef HAVE_WINDOW_SYSTEM
6222 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6223 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6224 make_number (0));
6225 #endif
6227 /* "Swap out" point from the selected window's buffer
6228 into the window itself. (Normally the pointm of the selected
6229 window holds garbage.) We do this now, before
6230 restoring the window contents, and prevent it from
6231 being done later on when we select a new window. */
6232 if (! NILP (XWINDOW (selected_window)->contents))
6234 w = XWINDOW (selected_window);
6235 set_marker_both (w->pointm,
6236 w->contents,
6237 BUF_PT (XBUFFER (w->contents)),
6238 BUF_PT_BYTE (XBUFFER (w->contents)));
6241 fset_redisplay (f);
6242 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6244 /* Problem: Freeing all matrices and later allocating them again
6245 is a serious redisplay flickering problem. What we would
6246 really like to do is to free only those matrices not reused
6247 below. */
6248 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6249 leaf_windows = alloca (count_windows (root_window)
6250 * sizeof *leaf_windows);
6251 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6253 /* Kludge Alert!
6254 Mark all windows now on frame as "deleted".
6255 Restoring the new configuration "undeletes" any that are in it.
6257 Save their current buffers in their height fields, since we may
6258 need it later, if a buffer saved in the configuration is now
6259 dead. */
6260 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6262 for (k = 0; k < saved_windows->header.size; k++)
6264 p = SAVED_WINDOW_N (saved_windows, k);
6265 window = p->window;
6266 w = XWINDOW (window);
6267 wset_next (w, Qnil);
6269 if (!NILP (p->parent))
6270 wset_parent
6271 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6272 else
6273 wset_parent (w, Qnil);
6275 if (!NILP (p->prev))
6277 wset_prev
6278 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6279 wset_next (XWINDOW (w->prev), p->window);
6281 else
6283 wset_prev (w, Qnil);
6284 if (!NILP (w->parent))
6285 wset_combination (XWINDOW (w->parent),
6286 (XINT (p->total_cols)
6287 != XWINDOW (w->parent)->total_cols),
6288 p->window);
6291 /* If we squirreled away the buffer, restore it now. */
6292 if (BUFFERP (w->combination_limit))
6293 wset_buffer (w, w->combination_limit);
6294 w->pixel_left = XFASTINT (p->pixel_left);
6295 w->pixel_top = XFASTINT (p->pixel_top);
6296 w->pixel_width = XFASTINT (p->pixel_width);
6297 w->pixel_height = XFASTINT (p->pixel_height);
6298 w->left_col = XFASTINT (p->left_col);
6299 w->top_line = XFASTINT (p->top_line);
6300 w->total_cols = XFASTINT (p->total_cols);
6301 w->total_lines = XFASTINT (p->total_lines);
6302 wset_normal_cols (w, p->normal_cols);
6303 wset_normal_lines (w, p->normal_lines);
6304 w->hscroll = XFASTINT (p->hscroll);
6305 w->min_hscroll = XFASTINT (p->min_hscroll);
6306 wset_display_table (w, p->display_table);
6307 w->left_margin_cols = XINT (p->left_margin_cols);
6308 w->right_margin_cols = XINT (p->right_margin_cols);
6309 w->left_fringe_width = XINT (p->left_fringe_width);
6310 w->right_fringe_width = XINT (p->right_fringe_width);
6311 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6312 w->scroll_bar_width = XINT (p->scroll_bar_width);
6313 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6314 wset_dedicated (w, p->dedicated);
6315 wset_combination_limit (w, p->combination_limit);
6316 /* Restore any window parameters that have been saved.
6317 Parameters that have not been saved are left alone. */
6318 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6320 pers = XCAR (tem);
6321 if (CONSP (pers))
6323 if (NILP (XCDR (pers)))
6325 par = Fassq (XCAR (pers), w->window_parameters);
6326 if (CONSP (par) && !NILP (XCDR (par)))
6327 /* Reset a parameter to nil if and only if it
6328 has a non-nil association. Don't make new
6329 associations. */
6330 Fsetcdr (par, Qnil);
6332 else
6333 /* Always restore a non-nil value. */
6334 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6338 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6339 /* If saved buffer is alive, install it. */
6341 wset_buffer (w, p->buffer);
6342 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6343 set_marker_restricted (w->start, p->start, w->contents);
6344 set_marker_restricted (w->pointm, p->pointm,
6345 w->contents);
6346 Fset_marker (BVAR (XBUFFER (w->contents), mark),
6347 p->mark, w->contents);
6349 /* As documented in Fcurrent_window_configuration, don't
6350 restore the location of point in the buffer which was
6351 current when the window configuration was recorded. */
6352 if (!EQ (p->buffer, new_current_buffer)
6353 && XBUFFER (p->buffer) == current_buffer)
6354 Fgoto_char (w->pointm);
6356 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6357 /* Keep window's old buffer; make sure the markers are real. */
6359 /* Set window markers at start of visible range. */
6360 if (XMARKER (w->start)->buffer == 0)
6361 set_marker_restricted_both (w->start, w->contents, 0, 0);
6362 if (XMARKER (w->pointm)->buffer == 0)
6363 set_marker_restricted_both
6364 (w->pointm, w->contents,
6365 BUF_PT (XBUFFER (w->contents)),
6366 BUF_PT_BYTE (XBUFFER (w->contents)));
6367 w->start_at_line_beg = 1;
6369 else if (!NILP (w->start))
6370 /* Leaf window has no live buffer, get one. */
6372 /* Get the buffer via other_buffer_safely in order to
6373 avoid showing an unimportant buffer and, if necessary, to
6374 recreate *scratch* in the course (part of Juanma's bs-show
6375 scenario from March 2011). */
6376 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6377 /* This will set the markers to beginning of visible
6378 range. */
6379 set_marker_restricted_both (w->start, w->contents, 0, 0);
6380 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6381 w->start_at_line_beg = 1;
6382 if (!NILP (w->dedicated))
6383 /* Record this window as dead. */
6384 dead_windows = Fcons (window, dead_windows);
6385 /* Make sure window is no more dedicated. */
6386 wset_dedicated (w, Qnil);
6390 fset_root_window (f, data->root_window);
6391 /* Arrange *not* to restore point in the buffer that was
6392 current when the window configuration was saved. */
6393 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6394 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6395 make_number (old_point),
6396 XWINDOW (data->current_window)->contents);
6398 /* In the following call to `select-window', prevent "swapping out
6399 point" in the old selected window using the buffer that has
6400 been restored into it. We already swapped out that point from
6401 that window's old buffer.
6403 Do not record the buffer here. We do that in a separate call
6404 to select_window below. See also Bug#16207. */
6405 select_window (data->current_window, Qt, 1);
6406 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6407 last_selected_window)
6408 = selected_window;
6410 if (NILP (data->focus_frame)
6411 || (FRAMEP (data->focus_frame)
6412 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6413 Fredirect_frame_focus (frame, data->focus_frame);
6415 /* Set the frame size to the value it had before this function. */
6416 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6417 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6418 change_frame_size (f, previous_frame_text_width,
6419 previous_frame_text_height, 0, 0, 0, 1);
6421 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6423 #ifdef HAVE_WINDOW_SYSTEM
6424 if (FRAME_WINDOW_P (f))
6425 x_set_menu_bar_lines (f,
6426 make_number (previous_frame_menu_bar_lines),
6427 make_number (0));
6428 else /* TTY or MSDOS */
6429 #endif
6430 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6431 make_number (0));
6433 #ifdef HAVE_WINDOW_SYSTEM
6434 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6435 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6436 make_number (0));
6437 #endif
6439 /* Now, free glyph matrices in windows that were not reused. */
6440 for (i = n = 0; i < n_leaf_windows; ++i)
6442 if (NILP (leaf_windows[i]->contents))
6443 free_window_matrices (leaf_windows[i]);
6444 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6445 ++n;
6448 adjust_frame_glyphs (f);
6449 unblock_input ();
6450 unbind_to (count, Qnil);
6452 /* Scan dead buffer windows. */
6453 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6455 window = XCAR (dead_windows);
6456 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6457 delete_deletable_window (window);
6460 /* Record the selected window's buffer here. The window should
6461 already be the selected one from the call above. */
6462 select_window (data->current_window, Qnil, 0);
6464 /* Fselect_window will have made f the selected frame, so we
6465 reselect the proper frame here. Fhandle_switch_frame will change the
6466 selected window too, but that doesn't make the call to
6467 Fselect_window above totally superfluous; it still sets f's
6468 selected window. */
6469 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6470 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6472 run_window_configuration_change_hook (f);
6475 if (!NILP (new_current_buffer))
6477 Fset_buffer (new_current_buffer);
6478 /* If the new current buffer doesn't appear in the selected
6479 window, go to its old point (see bug#12208). */
6480 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6481 Fgoto_char (make_number (old_point));
6484 Vminibuf_scroll_window = data->minibuf_scroll_window;
6485 minibuf_selected_window = data->minibuf_selected_window;
6487 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6490 void
6491 restore_window_configuration (Lisp_Object configuration)
6493 Fset_window_configuration (configuration);
6497 /* If WINDOW is an internal window, recursively delete all child windows
6498 reachable via the next and contents slots of WINDOW. Otherwise setup
6499 WINDOW to not show any buffer. */
6501 void
6502 delete_all_child_windows (Lisp_Object window)
6504 register struct window *w;
6506 w = XWINDOW (window);
6508 if (!NILP (w->next))
6509 /* Delete WINDOW's siblings (we traverse postorderly). */
6510 delete_all_child_windows (w->next);
6512 if (WINDOWP (w->contents))
6514 delete_all_child_windows (w->contents);
6515 wset_combination (w, 0, Qnil);
6517 else if (BUFFERP (w->contents))
6519 unshow_buffer (w);
6520 unchain_marker (XMARKER (w->pointm));
6521 unchain_marker (XMARKER (w->start));
6522 /* Since combination limit makes sense for an internal windows
6523 only, we use this slot to save the buffer for the sake of
6524 possible resurrection in Fset_window_configuration. */
6525 wset_combination_limit (w, w->contents);
6526 wset_buffer (w, Qnil);
6529 Vwindow_list = Qnil;
6532 static int
6533 count_windows (register struct window *window)
6535 register int count = 1;
6536 if (!NILP (window->next))
6537 count += count_windows (XWINDOW (window->next));
6538 if (WINDOWP (window->contents))
6539 count += count_windows (XWINDOW (window->contents));
6540 return count;
6544 /* Fill vector FLAT with leaf windows under W, starting at index I.
6545 Value is last index + 1. */
6546 static int
6547 get_leaf_windows (struct window *w, struct window **flat, int i)
6549 while (w)
6551 if (WINDOWP (w->contents))
6552 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6553 else
6554 flat[i++] = w;
6556 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6559 return i;
6563 /* Return a pointer to the glyph W's physical cursor is on. Value is
6564 null if W's current matrix is invalid, so that no meaningful glyph
6565 can be returned. */
6566 struct glyph *
6567 get_phys_cursor_glyph (struct window *w)
6569 struct glyph_row *row;
6570 struct glyph *glyph;
6571 int hpos = w->phys_cursor.hpos;
6573 if (!(w->phys_cursor.vpos >= 0
6574 && w->phys_cursor.vpos < w->current_matrix->nrows))
6575 return NULL;
6577 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6578 if (!row->enabled_p)
6579 return NULL;
6581 if (w->hscroll)
6583 /* When the window is hscrolled, cursor hpos can legitimately be
6584 out of bounds, but we draw the cursor at the corresponding
6585 window margin in that case. */
6586 if (!row->reversed_p && hpos < 0)
6587 hpos = 0;
6588 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6589 hpos = row->used[TEXT_AREA] - 1;
6592 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6593 glyph = row->glyphs[TEXT_AREA] + hpos;
6594 else
6595 glyph = NULL;
6597 return glyph;
6601 static int
6602 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6604 register struct saved_window *p;
6605 register struct window *w;
6606 register Lisp_Object tem, pers, par;
6608 for (; !NILP (window); window = w->next)
6610 p = SAVED_WINDOW_N (vector, i);
6611 w = XWINDOW (window);
6613 wset_temslot (w, make_number (i)); i++;
6614 p->window = window;
6615 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6616 p->pixel_left = make_number (w->pixel_left);
6617 p->pixel_top = make_number (w->pixel_top);
6618 p->pixel_width = make_number (w->pixel_width);
6619 p->pixel_height = make_number (w->pixel_height);
6620 p->left_col = make_number (w->left_col);
6621 p->top_line = make_number (w->top_line);
6622 p->total_cols = make_number (w->total_cols);
6623 p->total_lines = make_number (w->total_lines);
6624 p->normal_cols = w->normal_cols;
6625 p->normal_lines = w->normal_lines;
6626 XSETFASTINT (p->hscroll, w->hscroll);
6627 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6628 p->display_table = w->display_table;
6629 p->left_margin_cols = make_number (w->left_margin_cols);
6630 p->right_margin_cols = make_number (w->right_margin_cols);
6631 p->left_fringe_width = make_number (w->left_fringe_width);
6632 p->right_fringe_width = make_number (w->right_fringe_width);
6633 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6634 p->scroll_bar_width = make_number (w->scroll_bar_width);
6635 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6636 p->dedicated = w->dedicated;
6637 p->combination_limit = w->combination_limit;
6638 p->window_parameters = Qnil;
6640 if (!NILP (Vwindow_persistent_parameters))
6642 /* Run cycle detection on Vwindow_persistent_parameters. */
6643 Lisp_Object tortoise, hare;
6645 hare = tortoise = Vwindow_persistent_parameters;
6646 while (CONSP (hare))
6648 hare = XCDR (hare);
6649 if (!CONSP (hare))
6650 break;
6652 hare = XCDR (hare);
6653 tortoise = XCDR (tortoise);
6655 if (EQ (hare, tortoise))
6656 /* Reset Vwindow_persistent_parameters to Qnil. */
6658 Vwindow_persistent_parameters = Qnil;
6659 break;
6663 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6664 tem = XCDR (tem))
6666 pers = XCAR (tem);
6667 /* Save values for persistent window parameters. */
6668 if (CONSP (pers) && !NILP (XCDR (pers)))
6670 par = Fassq (XCAR (pers), w->window_parameters);
6671 if (NILP (par))
6672 /* If the window has no value for the parameter,
6673 make one. */
6674 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6675 p->window_parameters);
6676 else
6677 /* If the window has a value for the parameter,
6678 save it. */
6679 p->window_parameters = Fcons (Fcons (XCAR (par),
6680 XCDR (par)),
6681 p->window_parameters);
6686 if (BUFFERP (w->contents))
6688 /* Save w's value of point in the window configuration. If w
6689 is the selected window, then get the value of point from
6690 the buffer; pointm is garbage in the selected window. */
6691 if (EQ (window, selected_window))
6692 p->pointm = build_marker (XBUFFER (w->contents),
6693 BUF_PT (XBUFFER (w->contents)),
6694 BUF_PT_BYTE (XBUFFER (w->contents)));
6695 else
6696 p->pointm = Fcopy_marker (w->pointm, Qnil);
6697 XMARKER (p->pointm)->insertion_type
6698 = !NILP (buffer_local_value_1 /* Don't signal error if void. */
6699 (Qwindow_point_insertion_type, w->contents));
6701 p->start = Fcopy_marker (w->start, Qnil);
6702 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6704 tem = BVAR (XBUFFER (w->contents), mark);
6705 p->mark = Fcopy_marker (tem, Qnil);
6707 else
6709 p->pointm = Qnil;
6710 p->start = Qnil;
6711 p->mark = Qnil;
6712 p->start_at_line_beg = Qnil;
6715 if (NILP (w->parent))
6716 p->parent = Qnil;
6717 else
6718 p->parent = XWINDOW (w->parent)->temslot;
6720 if (NILP (w->prev))
6721 p->prev = Qnil;
6722 else
6723 p->prev = XWINDOW (w->prev)->temslot;
6725 if (WINDOWP (w->contents))
6726 i = save_window_save (w->contents, vector, i);
6729 return i;
6732 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6733 Scurrent_window_configuration, 0, 1, 0,
6734 doc: /* Return an object representing the current window configuration of FRAME.
6735 If FRAME is nil or omitted, use the selected frame.
6736 This describes the number of windows, their sizes and current buffers,
6737 and for each displayed buffer, where display starts, and the positions of
6738 point and mark. An exception is made for point in the current buffer:
6739 its value is -not- saved.
6740 This also records the currently selected frame, and FRAME's focus
6741 redirection (see `redirect-frame-focus'). The variable
6742 `window-persistent-parameters' specifies which window parameters are
6743 saved by this function. */)
6744 (Lisp_Object frame)
6746 register Lisp_Object tem;
6747 register int n_windows;
6748 register struct save_window_data *data;
6749 register int i;
6750 struct frame *f = decode_live_frame (frame);
6752 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6753 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6754 PVEC_WINDOW_CONFIGURATION);
6756 data->frame_cols = FRAME_COLS (f);
6757 data->frame_lines = FRAME_LINES (f);
6758 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6759 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6760 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6761 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6762 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6763 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6764 data->selected_frame = selected_frame;
6765 data->current_window = FRAME_SELECTED_WINDOW (f);
6766 XSETBUFFER (data->current_buffer, current_buffer);
6767 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6768 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6769 data->root_window = FRAME_ROOT_WINDOW (f);
6770 data->focus_frame = FRAME_FOCUS_FRAME (f);
6771 tem = make_uninit_vector (n_windows);
6772 data->saved_windows = tem;
6773 for (i = 0; i < n_windows; i++)
6774 ASET (tem, i,
6775 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6776 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6777 XSETWINDOW_CONFIGURATION (tem, data);
6778 return (tem);
6781 /* Called after W's margins, fringes or scroll bars was adjusted. */
6783 static void
6784 apply_window_adjustment (struct window *w)
6786 eassert (w);
6787 adjust_window_margins (w);
6788 clear_glyph_matrix (w->current_matrix);
6789 w->window_end_valid = 0;
6790 windows_or_buffers_changed = 30;
6791 wset_redisplay (w);
6792 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6796 /***********************************************************************
6797 Marginal Areas
6798 ***********************************************************************/
6800 static struct window *
6801 set_window_margins (struct window *w, Lisp_Object left_width,
6802 Lisp_Object right_width)
6804 int left, right;
6806 /* FIXME: what about margins that are too wide? */
6807 left = (NILP (left_width) ? 0
6808 : (CHECK_NATNUM (left_width), XINT (left_width)));
6809 right = (NILP (right_width) ? 0
6810 : (CHECK_NATNUM (right_width), XINT (right_width)));
6812 if (w->left_margin_cols != left || w->right_margin_cols != right)
6814 w->left_margin_cols = left;
6815 w->right_margin_cols = right;
6816 return w;
6818 return NULL;
6821 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6822 2, 3, 0,
6823 doc: /* Set width of marginal areas of window WINDOW.
6824 WINDOW must be a live window and defaults to the selected one.
6826 Second arg LEFT-WIDTH specifies the number of character cells to
6827 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6828 does the same for the right marginal area. A nil width parameter
6829 means no margin.
6831 Return t if any margin was actually changed and nil otherwise. */)
6832 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6834 struct window *w = set_window_margins (decode_live_window (window),
6835 left_width, right_width);
6836 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6840 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6841 0, 1, 0,
6842 doc: /* Get width of marginal areas of window WINDOW.
6843 WINDOW must be a live window and defaults to the selected one.
6845 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6846 If a marginal area does not exist, its width will be returned
6847 as nil. */)
6848 (Lisp_Object window)
6850 struct window *w = decode_live_window (window);
6851 return Fcons (w->left_margin_cols
6852 ? make_number (w->left_margin_cols) : Qnil,
6853 w->right_margin_cols
6854 ? make_number (w->right_margin_cols) : Qnil);
6859 /***********************************************************************
6860 Fringes
6861 ***********************************************************************/
6863 static struct window *
6864 set_window_fringes (struct window *w, Lisp_Object left_width,
6865 Lisp_Object right_width, Lisp_Object outside_margins)
6867 int left, right, outside = !NILP (outside_margins);
6869 left = (NILP (left_width) ? -1
6870 : (CHECK_NATNUM (left_width), XINT (left_width)));
6871 right = (NILP (right_width) ? -1
6872 : (CHECK_NATNUM (right_width), XINT (right_width)));
6874 /* Do nothing on a tty or if nothing to actually change. */
6875 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6876 && (w->left_fringe_width != left
6877 || w->right_fringe_width != right
6878 || w->fringes_outside_margins != outside))
6880 w->left_fringe_width = left;
6881 w->right_fringe_width = right;
6882 w->fringes_outside_margins = outside;
6883 return w;
6885 return NULL;
6888 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6889 2, 4, 0,
6890 doc: /* Set the fringe widths of window WINDOW.
6891 WINDOW must be a live window and defaults to the selected one.
6893 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6894 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6895 fringe width. If a fringe width arg is nil, that means to use the
6896 frame's default fringe width. Default fringe widths can be set with
6897 the command `set-fringe-style'.
6898 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6899 outside of the display margins. By default, fringes are drawn between
6900 display marginal areas and the text area.
6902 Return t if any fringe was actually changed and nil otherwise. */)
6903 (Lisp_Object window, Lisp_Object left_width,
6904 Lisp_Object right_width, Lisp_Object outside_margins)
6906 struct window *w
6907 = set_window_fringes (decode_live_window (window),
6908 left_width, right_width, outside_margins);
6909 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6913 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6914 0, 1, 0,
6915 doc: /* Get width of fringes of window WINDOW.
6916 WINDOW must be a live window and defaults to the selected one.
6918 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6919 (Lisp_Object window)
6921 struct window *w = decode_live_window (window);
6923 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6924 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6925 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6930 /***********************************************************************
6931 Scroll bars
6932 ***********************************************************************/
6934 static struct window *
6935 set_window_scroll_bars (struct window *w, Lisp_Object width,
6936 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6938 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6940 if (iwidth == 0)
6941 vertical_type = Qnil;
6943 if (!(NILP (vertical_type)
6944 || EQ (vertical_type, Qleft)
6945 || EQ (vertical_type, Qright)
6946 || EQ (vertical_type, Qt)))
6947 error ("Invalid type of vertical scroll bar");
6949 if (w->scroll_bar_width != iwidth
6950 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6952 w->scroll_bar_width = iwidth;
6953 wset_vertical_scroll_bar_type (w, vertical_type);
6954 return w;
6956 return NULL;
6959 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6960 Sset_window_scroll_bars, 2, 4, 0,
6961 doc: /* Set width and type of scroll bars of window WINDOW.
6962 WINDOW must be a live window and defaults to the selected one.
6964 Second parameter WIDTH specifies the pixel width for the scroll bar.
6965 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6966 bar: left, right, or nil.
6967 If WIDTH is nil, use the frame's scroll-bar width.
6968 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6969 Fourth parameter HORIZONTAL-TYPE is currently unused.
6971 Return t if scroll bars were actually changed and nil otherwise. */)
6972 (Lisp_Object window, Lisp_Object width,
6973 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6975 struct window *w
6976 = set_window_scroll_bars (decode_live_window (window),
6977 width, vertical_type, horizontal_type);
6978 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6982 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6983 0, 1, 0,
6984 doc: /* Get width and type of scroll bars of window WINDOW.
6985 WINDOW must be a live window and defaults to the selected one.
6987 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6988 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6989 value. */)
6990 (Lisp_Object window)
6992 struct window *w = decode_live_window (window);
6994 return list4 (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (w)),
6995 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6996 w->vertical_scroll_bar_type, Qnil);
7001 /***********************************************************************
7002 Smooth scrolling
7003 ***********************************************************************/
7005 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
7006 doc: /* Return the amount by which WINDOW is scrolled vertically.
7007 If WINDOW is omitted or nil, it defaults to the selected window.
7008 Normally, value is a multiple of the canonical character height of WINDOW;
7009 optional second arg PIXELS-P means value is measured in pixels. */)
7010 (Lisp_Object window, Lisp_Object pixels_p)
7012 Lisp_Object result;
7013 struct window *w = decode_live_window (window);
7014 struct frame *f = XFRAME (w->frame);
7016 if (FRAME_WINDOW_P (f))
7017 result = (NILP (pixels_p)
7018 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
7019 : make_number (-w->vscroll));
7020 else
7021 result = make_number (0);
7022 return result;
7026 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
7027 2, 3, 0,
7028 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
7029 WINDOW nil means use the selected window. Normally, VSCROLL is a
7030 non-negative multiple of the canonical character height of WINDOW;
7031 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
7032 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
7033 corresponds to an integral number of pixels. The return value is the
7034 result of this rounding.
7035 If PIXELS-P is non-nil, the return value is VSCROLL. */)
7036 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
7038 struct window *w = decode_live_window (window);
7039 struct frame *f = XFRAME (w->frame);
7041 CHECK_NUMBER_OR_FLOAT (vscroll);
7043 if (FRAME_WINDOW_P (f))
7045 int old_dy = w->vscroll;
7047 w->vscroll = - (NILP (pixels_p)
7048 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7049 : XFLOATINT (vscroll));
7050 w->vscroll = min (w->vscroll, 0);
7052 if (w->vscroll != old_dy)
7054 /* Adjust glyph matrix of the frame if the virtual display
7055 area becomes larger than before. */
7056 if (w->vscroll < 0 && w->vscroll < old_dy)
7057 adjust_frame_glyphs (f);
7059 /* Prevent redisplay shortcuts. */
7060 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
7064 return Fwindow_vscroll (window, pixels_p);
7068 /* Call FN for all leaf windows on frame F. FN is called with the
7069 first argument being a pointer to the leaf window, and with
7070 additional argument USER_DATA. Stops when FN returns 0. */
7072 static void
7073 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
7074 void *user_data)
7076 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7077 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7078 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7082 /* Helper function for foreach_window. Call FN for all leaf windows
7083 reachable from W. FN is called with the first argument being a
7084 pointer to the leaf window, and with additional argument USER_DATA.
7085 Stop when FN returns 0. Value is 0 if stopped by FN. */
7087 static int
7088 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7090 int cont;
7092 for (cont = 1; w && cont;)
7094 if (WINDOWP (w->contents))
7095 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7096 else
7097 cont = fn (w, user_data);
7099 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7102 return cont;
7105 /***********************************************************************
7106 Initialization
7107 ***********************************************************************/
7109 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7110 describe the same state of affairs. This is used by Fequal.
7112 IGNORE_POSITIONS means ignore non-matching scroll positions
7113 and the like.
7115 This ignores a couple of things like the dedication status of
7116 window, combination_limit and the like. This might have to be
7117 fixed. */
7119 bool
7120 compare_window_configurations (Lisp_Object configuration1,
7121 Lisp_Object configuration2,
7122 bool ignore_positions)
7124 register struct save_window_data *d1, *d2;
7125 struct Lisp_Vector *sws1, *sws2;
7126 ptrdiff_t i;
7128 CHECK_WINDOW_CONFIGURATION (configuration1);
7129 CHECK_WINDOW_CONFIGURATION (configuration2);
7131 d1 = (struct save_window_data *) XVECTOR (configuration1);
7132 d2 = (struct save_window_data *) XVECTOR (configuration2);
7133 sws1 = XVECTOR (d1->saved_windows);
7134 sws2 = XVECTOR (d2->saved_windows);
7136 /* Frame settings must match. */
7137 if (d1->frame_cols != d2->frame_cols
7138 || d1->frame_lines != d2->frame_lines
7139 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7140 || !EQ (d1->selected_frame, d2->selected_frame)
7141 || !EQ (d1->current_buffer, d2->current_buffer)
7142 || (!ignore_positions
7143 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7144 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7145 || !EQ (d1->focus_frame, d2->focus_frame)
7146 /* Verify that the two configurations have the same number of windows. */
7147 || sws1->header.size != sws2->header.size)
7148 return 0;
7150 for (i = 0; i < sws1->header.size; i++)
7152 struct saved_window *sw1, *sw2;
7154 sw1 = SAVED_WINDOW_N (sws1, i);
7155 sw2 = SAVED_WINDOW_N (sws2, i);
7157 if (
7158 /* The "current" windows in the two configurations must
7159 correspond to each other. */
7160 EQ (d1->current_window, sw1->window)
7161 != EQ (d2->current_window, sw2->window)
7162 /* Windows' buffers must match. */
7163 || !EQ (sw1->buffer, sw2->buffer)
7164 || !EQ (sw1->pixel_left, sw2->pixel_left)
7165 || !EQ (sw1->pixel_top, sw2->pixel_top)
7166 || !EQ (sw1->pixel_height, sw2->pixel_height)
7167 || !EQ (sw1->pixel_width, sw2->pixel_width)
7168 || !EQ (sw1->left_col, sw2->left_col)
7169 || !EQ (sw1->top_line, sw2->top_line)
7170 || !EQ (sw1->total_cols, sw2->total_cols)
7171 || !EQ (sw1->total_lines, sw2->total_lines)
7172 || !EQ (sw1->display_table, sw2->display_table)
7173 /* The next two disjuncts check the window structure for
7174 equality. */
7175 || !EQ (sw1->parent, sw2->parent)
7176 || !EQ (sw1->prev, sw2->prev)
7177 || (!ignore_positions
7178 && (!EQ (sw1->hscroll, sw2->hscroll)
7179 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7180 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7181 || NILP (Fequal (sw1->start, sw2->start))
7182 || NILP (Fequal (sw1->pointm, sw2->pointm))
7183 || NILP (Fequal (sw1->mark, sw2->mark))))
7184 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7185 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7186 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7187 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7188 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7189 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7190 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7191 return 0;
7194 return 1;
7197 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7198 Scompare_window_configurations, 2, 2, 0,
7199 doc: /* Compare two window configurations as regards the structure of windows.
7200 This function ignores details such as the values of point and mark
7201 and scrolling positions. */)
7202 (Lisp_Object x, Lisp_Object y)
7204 if (compare_window_configurations (x, y, 1))
7205 return Qt;
7206 return Qnil;
7209 void
7210 init_window_once (void)
7212 struct frame *f = make_initial_frame ();
7213 XSETFRAME (selected_frame, f);
7214 Vterminal_frame = selected_frame;
7215 minibuf_window = f->minibuffer_window;
7216 selected_window = f->selected_window;
7218 window_initialized = 1;
7221 void
7222 init_window (void)
7224 Vwindow_list = Qnil;
7227 void
7228 syms_of_window (void)
7230 DEFSYM (Qscroll_up, "scroll-up");
7231 DEFSYM (Qscroll_down, "scroll-down");
7232 DEFSYM (Qscroll_command, "scroll-command");
7234 Fput (Qscroll_up, Qscroll_command, Qt);
7235 Fput (Qscroll_down, Qscroll_command, Qt);
7237 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7238 DEFSYM (Qwindowp, "windowp");
7239 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7240 DEFSYM (Qwindow_live_p, "window-live-p");
7241 DEFSYM (Qwindow_valid_p, "window-valid-p");
7242 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7243 DEFSYM (Qdelete_window, "delete-window");
7244 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7245 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7246 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7247 DEFSYM (Qsafe, "safe");
7248 DEFSYM (Qdisplay_buffer, "display-buffer");
7249 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7250 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7251 DEFSYM (Qget_mru_window, "get-mru-window");
7252 DEFSYM (Qwindow_size, "window-size");
7253 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7254 DEFSYM (Qabove, "above");
7255 DEFSYM (Qbelow, "below");
7256 DEFSYM (Qclone_of, "clone-of");
7257 DEFSYM (Qfloor, "floor");
7258 DEFSYM (Qceiling, "ceiling");
7260 staticpro (&Vwindow_list);
7262 minibuf_selected_window = Qnil;
7263 staticpro (&minibuf_selected_window);
7265 window_scroll_pixel_based_preserve_x = -1;
7266 window_scroll_pixel_based_preserve_y = -1;
7267 window_scroll_preserve_hpos = -1;
7268 window_scroll_preserve_vpos = -1;
7270 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7271 doc: /* Non-nil means call as function to display a help buffer.
7272 The function is called with one argument, the buffer to be displayed.
7273 Used by `with-output-to-temp-buffer'.
7274 If this function is used, then it must do the entire job of showing
7275 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7276 Vtemp_buffer_show_function = Qnil;
7278 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7279 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7280 Vminibuf_scroll_window = Qnil;
7282 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7283 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7284 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7285 is displayed in the `mode-line' face. */);
7286 mode_line_in_non_selected_windows = 1;
7288 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7289 doc: /* If this is a live buffer, \\[scroll-other-window] should scroll its window. */);
7290 Vother_window_scroll_buffer = Qnil;
7292 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7293 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7294 auto_window_vscroll_p = 1;
7296 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7297 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7298 next_screen_context_lines = 2;
7300 DEFVAR_LISP ("scroll-preserve-screen-position",
7301 Vscroll_preserve_screen_position,
7302 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7303 A value of nil means point does not keep its screen position except
7304 at the scroll margin or window boundary respectively.
7305 A value of t means point keeps its screen position if the scroll
7306 command moved it vertically out of the window, e.g. when scrolling
7307 by full screens.
7308 Any other value means point always keeps its screen position.
7309 Scroll commands should have the `scroll-command' property
7310 on their symbols to be controlled by this variable. */);
7311 Vscroll_preserve_screen_position = Qnil;
7313 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7314 doc: /* Type of marker to use for `window-point'. */);
7315 Vwindow_point_insertion_type = Qnil;
7316 DEFSYM (Qwindow_point_insertion_type, "window_point_insertion_type");
7318 DEFVAR_LISP ("window-configuration-change-hook",
7319 Vwindow_configuration_change_hook,
7320 doc: /* Functions to call when window configuration changes.
7321 The buffer-local part is run once per window, with the relevant window
7322 selected; while the global part is run only once for the modified frame,
7323 with the relevant frame selected. */);
7324 Vwindow_configuration_change_hook = Qnil;
7326 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7327 doc: /* Non-nil means `recenter' redraws entire frame.
7328 If this option is non-nil, then the `recenter' command with a nil
7329 argument will redraw the entire frame; the special value `tty' causes
7330 the frame to be redrawn only if it is a tty frame. */);
7331 Vrecenter_redisplay = Qtty;
7333 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7334 doc: /* If t, resize window combinations proportionally.
7335 If this variable is nil, splitting a window gets the entire screen space
7336 for displaying the new window from the window to split. Deleting and
7337 resizing a window preferably resizes one adjacent window only.
7339 If this variable is t, splitting a window tries to get the space
7340 proportionally from all windows in the same combination. This also
7341 allows to split a window that is otherwise too small or of fixed size.
7342 Resizing and deleting a window proportionally resize all windows in the
7343 same combination.
7345 Other values are reserved for future use.
7347 This variable takes no effect if the variable `window-combination-limit' is
7348 non-nil. */);
7349 Vwindow_combination_resize = Qnil;
7351 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7352 doc: /* If non-nil, splitting a window makes a new parent window.
7353 The following values are recognized:
7355 nil means splitting a window will create a new parent window only if the
7356 window has no parent window or the window shall become part of a
7357 combination orthogonal to the one it is part of.
7359 `window-size' means that splitting a window for displaying a buffer
7360 makes a new parent window provided `display-buffer' is supposed to
7361 explicitly set the window's size due to the presence of a
7362 `window-height' or `window-width' entry in the alist used by
7363 `display-buffer'. Otherwise, this value is handled like nil.
7365 `temp-buffer' means that splitting a window for displaying a temporary
7366 buffer always makes a new parent window. Otherwise, this value is
7367 handled like nil.
7369 `display-buffer' means that splitting a window for displaying a buffer
7370 always makes a new parent window. Since temporary buffers are
7371 displayed by the function `display-buffer', this value is stronger
7372 than `temp-buffer'. Splitting a window for other purpose makes a
7373 new parent window only if needed.
7375 t means that splitting a window always creates a new parent window. If
7376 all splits behave this way, each frame's window tree is a binary
7377 tree and every window but the frame's root window has exactly one
7378 sibling.
7380 Other values are reserved for future use. */);
7381 Vwindow_combination_limit = Qwindow_size;
7383 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7384 doc: /* Alist of persistent window parameters.
7385 This alist specifies which window parameters shall get saved by
7386 `current-window-configuration' and `window-state-get' and subsequently
7387 restored to their previous values by `set-window-configuration' and
7388 `window-state-put'.
7390 The car of each entry of this alist is the symbol specifying the
7391 parameter. The cdr is one of the following:
7393 nil means the parameter is neither saved by `window-state-get' nor by
7394 `current-window-configuration'.
7396 t means the parameter is saved by `current-window-configuration' and,
7397 provided its WRITABLE argument is nil, by `window-state-get'.
7399 The symbol `writable' means the parameter is saved unconditionally by
7400 both `current-window-configuration' and `window-state-get'. Do not use
7401 this value for parameters without read syntax (like windows or frames).
7403 Parameters not saved by `current-window-configuration' or
7404 `window-state-get' are left alone by `set-window-configuration'
7405 respectively are not installed by `window-state-put'. */);
7406 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7408 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7409 doc: /* Non-nil means resize windows pixelwise.
7410 This currently affects the functions: `split-window', `maximize-window',
7411 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7412 all functions that symmetrically resize a parent window.
7414 Note that when a frame's pixel size is not a multiple of the
7415 frame's character size, at least one window may get resized
7416 pixelwise even if this option is nil. */);
7417 window_resize_pixelwise = 0;
7419 defsubr (&Sselected_window);
7420 defsubr (&Sminibuffer_window);
7421 defsubr (&Swindow_minibuffer_p);
7422 defsubr (&Swindowp);
7423 defsubr (&Swindow_valid_p);
7424 defsubr (&Swindow_live_p);
7425 defsubr (&Swindow_frame);
7426 defsubr (&Sframe_root_window);
7427 defsubr (&Sframe_first_window);
7428 defsubr (&Sframe_selected_window);
7429 defsubr (&Sset_frame_selected_window);
7430 defsubr (&Spos_visible_in_window_p);
7431 defsubr (&Swindow_line_height);
7432 defsubr (&Swindow_buffer);
7433 defsubr (&Swindow_parent);
7434 defsubr (&Swindow_top_child);
7435 defsubr (&Swindow_left_child);
7436 defsubr (&Swindow_next_sibling);
7437 defsubr (&Swindow_prev_sibling);
7438 defsubr (&Swindow_combination_limit);
7439 defsubr (&Sset_window_combination_limit);
7440 defsubr (&Swindow_use_time);
7441 defsubr (&Swindow_pixel_width);
7442 defsubr (&Swindow_pixel_height);
7443 defsubr (&Swindow_total_width);
7444 defsubr (&Swindow_total_height);
7445 defsubr (&Swindow_normal_size);
7446 defsubr (&Swindow_new_pixel);
7447 defsubr (&Swindow_new_total);
7448 defsubr (&Swindow_new_normal);
7449 defsubr (&Swindow_pixel_left);
7450 defsubr (&Swindow_pixel_top);
7451 defsubr (&Swindow_left_column);
7452 defsubr (&Swindow_top_line);
7453 defsubr (&Sset_window_new_pixel);
7454 defsubr (&Sset_window_new_total);
7455 defsubr (&Sset_window_new_normal);
7456 defsubr (&Swindow_resize_apply);
7457 defsubr (&Swindow_resize_apply_total);
7458 defsubr (&Swindow_body_height);
7459 defsubr (&Swindow_body_width);
7460 defsubr (&Swindow_hscroll);
7461 defsubr (&Sset_window_hscroll);
7462 defsubr (&Swindow_redisplay_end_trigger);
7463 defsubr (&Sset_window_redisplay_end_trigger);
7464 defsubr (&Swindow_edges);
7465 defsubr (&Swindow_pixel_edges);
7466 defsubr (&Swindow_absolute_pixel_edges);
7467 defsubr (&Swindow_mode_line_height);
7468 defsubr (&Swindow_header_line_height);
7469 defsubr (&Swindow_right_divider_width);
7470 defsubr (&Swindow_bottom_divider_width);
7471 defsubr (&Swindow_scroll_bar_width);
7472 defsubr (&Swindow_inside_edges);
7473 defsubr (&Swindow_inside_pixel_edges);
7474 defsubr (&Swindow_inside_absolute_pixel_edges);
7475 defsubr (&Scoordinates_in_window_p);
7476 defsubr (&Swindow_at);
7477 defsubr (&Swindow_point);
7478 defsubr (&Swindow_start);
7479 defsubr (&Swindow_end);
7480 defsubr (&Sset_window_point);
7481 defsubr (&Sset_window_start);
7482 defsubr (&Swindow_dedicated_p);
7483 defsubr (&Sset_window_dedicated_p);
7484 defsubr (&Swindow_display_table);
7485 defsubr (&Sset_window_display_table);
7486 defsubr (&Snext_window);
7487 defsubr (&Sprevious_window);
7488 defsubr (&Sget_buffer_window);
7489 defsubr (&Sdelete_other_windows_internal);
7490 defsubr (&Sdelete_window_internal);
7491 defsubr (&Sresize_mini_window_internal);
7492 defsubr (&Sset_window_buffer);
7493 defsubr (&Srun_window_configuration_change_hook);
7494 defsubr (&Srun_window_scroll_functions);
7495 defsubr (&Sselect_window);
7496 defsubr (&Sforce_window_update);
7497 defsubr (&Ssplit_window_internal);
7498 defsubr (&Sscroll_up);
7499 defsubr (&Sscroll_down);
7500 defsubr (&Sscroll_left);
7501 defsubr (&Sscroll_right);
7502 defsubr (&Sother_window_for_scrolling);
7503 defsubr (&Sscroll_other_window);
7504 defsubr (&Sminibuffer_selected_window);
7505 defsubr (&Srecenter);
7506 defsubr (&Swindow_text_width);
7507 defsubr (&Swindow_text_height);
7508 defsubr (&Smove_to_window_line);
7509 defsubr (&Swindow_configuration_p);
7510 defsubr (&Swindow_configuration_frame);
7511 defsubr (&Sset_window_configuration);
7512 defsubr (&Scurrent_window_configuration);
7513 defsubr (&Sset_window_margins);
7514 defsubr (&Swindow_margins);
7515 defsubr (&Sset_window_fringes);
7516 defsubr (&Swindow_fringes);
7517 defsubr (&Sset_window_scroll_bars);
7518 defsubr (&Swindow_scroll_bars);
7519 defsubr (&Swindow_vscroll);
7520 defsubr (&Sset_window_vscroll);
7521 defsubr (&Scompare_window_configurations);
7522 defsubr (&Swindow_list);
7523 defsubr (&Swindow_list_1);
7524 defsubr (&Swindow_prev_buffers);
7525 defsubr (&Sset_window_prev_buffers);
7526 defsubr (&Swindow_next_buffers);
7527 defsubr (&Sset_window_next_buffers);
7528 defsubr (&Swindow_parameters);
7529 defsubr (&Swindow_parameter);
7530 defsubr (&Sset_window_parameter);
7533 void
7534 keys_of_window (void)
7536 initial_define_key (control_x_map, '<', "scroll-left");
7537 initial_define_key (control_x_map, '>', "scroll-right");
7539 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7540 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7541 initial_define_key (meta_map, 'v', "scroll-down-command");