* configure.ac: Detect xcrun on OSX and use it for make, gcc and libxml.
[emacs.git] / src / window.c
blob9bc95224f7ccbbb4c1cba6c5cf4606df751ca405
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2013 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;
58 static int displayed_window_lines (struct window *);
59 static int count_windows (struct window *);
60 static int get_leaf_windows (struct window *, struct window **, int);
61 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
62 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
63 static void window_scroll_line_based (Lisp_Object, int, bool, int);
64 static int add_window_to_list (struct window *, void *);
65 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
66 Lisp_Object, int);
67 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
68 Lisp_Object *);
69 static void foreach_window (struct frame *,
70 int (* fn) (struct window *, void *),
71 void *);
72 static int foreach_window_1 (struct window *,
73 int (* fn) (struct window *, void *),
74 void *);
75 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
76 static int window_resize_check (struct window *, bool);
77 static void window_resize_apply (struct window *, bool);
78 static void window_resize_apply_total (struct window *, bool);
79 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
80 static void select_window_1 (Lisp_Object, bool);
82 static struct window *set_window_fringes (struct window *, Lisp_Object,
83 Lisp_Object, Lisp_Object);
84 static struct window *set_window_margins (struct window *, Lisp_Object,
85 Lisp_Object);
86 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
87 Lisp_Object, Lisp_Object);
88 static void apply_window_adjustment (struct window *);
90 /* This is the window in which the terminal's cursor should
91 be left when nothing is being done with it. This must
92 always be a leaf window, and its buffer is selected by
93 the top level editing loop at the end of each command.
95 This value is always the same as
96 FRAME_SELECTED_WINDOW (selected_frame). */
97 Lisp_Object selected_window;
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
102 Lisp_Object Vwindow_list;
104 /* The mini-buffer window of the selected frame.
105 Note that you cannot test for mini-bufferness of an arbitrary window
106 by comparing against this; but you can test for mini-bufferness of
107 the selected window. */
108 Lisp_Object minibuf_window;
110 /* Non-nil means it is the window whose mode line should be
111 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
114 /* Hook run at end of temp_output_buffer_show. */
115 static Lisp_Object Qtemp_buffer_show_hook;
117 /* Incremented for each window created. */
118 static int sequence_number;
120 /* Nonzero after init_window_once has finished. */
121 static int window_initialized;
123 /* Hook to run when window config changes. */
124 static Lisp_Object Qwindow_configuration_change_hook;
126 /* Used by the function window_scroll_pixel_based */
127 static int window_scroll_pixel_based_preserve_x;
128 static int window_scroll_pixel_based_preserve_y;
130 /* Same for window_scroll_line_based. */
131 static EMACS_INT window_scroll_preserve_hpos;
132 static EMACS_INT window_scroll_preserve_vpos;
134 static void
135 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
137 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
140 /* These setters are used only in this file, so they can be private. */
141 static void
142 wset_combination_limit (struct window *w, Lisp_Object val)
144 w->combination_limit = val;
146 static void
147 wset_dedicated (struct window *w, Lisp_Object val)
149 w->dedicated = val;
151 static void
152 wset_display_table (struct window *w, Lisp_Object val)
154 w->display_table = val;
156 static void
157 wset_new_normal (struct window *w, Lisp_Object val)
159 w->new_normal = val;
161 static void
162 wset_new_total (struct window *w, Lisp_Object val)
164 w->new_total = val;
166 static void
167 wset_normal_cols (struct window *w, Lisp_Object val)
169 w->normal_cols = val;
171 static void
172 wset_normal_lines (struct window *w, Lisp_Object val)
174 w->normal_lines = val;
176 static void
177 wset_parent (struct window *w, Lisp_Object val)
179 w->parent = val;
181 static void
182 wset_pointm (struct window *w, Lisp_Object val)
184 w->pointm = val;
186 static void
187 wset_start (struct window *w, Lisp_Object val)
189 w->start = val;
191 static void
192 wset_temslot (struct window *w, Lisp_Object val)
194 w->temslot = val;
196 static void
197 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
199 w->vertical_scroll_bar_type = val;
201 static void
202 wset_window_parameters (struct window *w, Lisp_Object val)
204 w->window_parameters = val;
206 static void
207 wset_combination (struct window *w, bool horflag, Lisp_Object val)
209 /* Since leaf windows never becomes non-leaf, there should
210 be no buffer and markers in start and pointm fields of W. */
211 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
212 w->contents = val;
213 /* When an internal window is deleted and VAL is nil, HORFLAG
214 is meaningless. */
215 if (!NILP (val))
216 w->horizontal = horflag;
219 /* Nonzero if leaf window W doesn't reflect the actual state
220 of displayed buffer due to its text or overlays change. */
222 bool
223 window_outdated (struct window *w)
225 struct buffer *b = XBUFFER (w->contents);
226 return (w->last_modified < BUF_MODIFF (b)
227 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
230 struct window *
231 decode_live_window (register Lisp_Object window)
233 if (NILP (window))
234 return XWINDOW (selected_window);
236 CHECK_LIVE_WINDOW (window);
237 return XWINDOW (window);
240 struct window *
241 decode_any_window (register Lisp_Object window)
243 struct window *w;
245 if (NILP (window))
246 return XWINDOW (selected_window);
248 CHECK_WINDOW (window);
249 w = XWINDOW (window);
250 return w;
253 static struct window *
254 decode_valid_window (register Lisp_Object window)
256 struct window *w;
258 if (NILP (window))
259 return XWINDOW (selected_window);
261 CHECK_VALID_WINDOW (window);
262 w = XWINDOW (window);
263 return w;
266 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
267 cease its buffer, and 1 means that W is about to set up the new one. */
269 static void
270 adjust_window_count (struct window *w, int arg)
272 eassert (eabs (arg) == 1);
273 if (BUFFERP (w->contents))
275 struct buffer *b = XBUFFER (w->contents);
277 if (b->base_buffer)
278 b = b->base_buffer;
279 b->window_count += arg;
280 eassert (b->window_count >= 0);
281 /* These should be recalculated by redisplay code. */
282 w->window_end_valid = 0;
283 w->base_line_pos = 0;
287 /* Set W's buffer slot to VAL and recompute number
288 of windows showing VAL if it is a buffer. */
290 void
291 wset_buffer (struct window *w, Lisp_Object val)
293 adjust_window_count (w, -1);
294 if (BUFFERP (val))
295 /* Make sure that we do not assign the buffer
296 to an internal window. */
297 eassert (MARKERP (w->start) && MARKERP (w->pointm));
298 w->contents = val;
299 adjust_window_count (w, 1);
302 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
303 doc: /* Return t if OBJECT is a window and nil otherwise. */)
304 (Lisp_Object object)
306 return WINDOWP (object) ? Qt : Qnil;
309 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
310 doc: /* Return t if OBJECT is a valid window and nil otherwise.
311 A valid window is either a window that displays a buffer or an internal
312 window. Deleted windows are not live. */)
313 (Lisp_Object object)
315 return WINDOW_VALID_P (object) ? Qt : Qnil;
318 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
319 doc: /* Return t if OBJECT is a live window and nil otherwise.
320 A live window is a window that displays a buffer.
321 Internal windows and deleted windows are not live. */)
322 (Lisp_Object object)
324 return WINDOW_LIVE_P (object) ? Qt : Qnil;
327 /* Frames and windows. */
328 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
329 doc: /* Return the frame that window WINDOW is on.
330 WINDOW must be a valid window and defaults to the selected one. */)
331 (Lisp_Object window)
333 return decode_valid_window (window)->frame;
336 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
337 doc: /* Return the root window of FRAME-OR-WINDOW.
338 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
339 With a frame argument, return that frame's root window.
340 With a window argument, return the root window of that window's frame. */)
341 (Lisp_Object frame_or_window)
343 Lisp_Object window;
345 if (NILP (frame_or_window))
346 window = SELECTED_FRAME ()->root_window;
347 else if (WINDOW_VALID_P (frame_or_window))
348 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
349 else
351 CHECK_LIVE_FRAME (frame_or_window);
352 window = XFRAME (frame_or_window)->root_window;
355 return window;
358 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
359 doc: /* Return the minibuffer window for frame FRAME.
360 If FRAME is omitted or nil, it defaults to the selected frame. */)
361 (Lisp_Object frame)
363 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
366 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
367 Swindow_minibuffer_p, 0, 1, 0,
368 doc: /* Return non-nil if WINDOW is a minibuffer window.
369 WINDOW must be a valid window and defaults to the selected one. */)
370 (Lisp_Object window)
372 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
375 /* Don't move this to window.el - this must be a safe routine. */
376 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
377 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
378 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
379 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
380 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
381 the first window of that frame. */)
382 (Lisp_Object frame_or_window)
384 Lisp_Object window;
386 if (NILP (frame_or_window))
387 window = SELECTED_FRAME ()->root_window;
388 else if (WINDOW_VALID_P (frame_or_window))
389 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
390 else
392 CHECK_LIVE_FRAME (frame_or_window);
393 window = XFRAME (frame_or_window)->root_window;
396 while (WINDOWP (XWINDOW (window)->contents))
397 window = XWINDOW (window)->contents;
399 return window;
402 DEFUN ("frame-selected-window", Fframe_selected_window,
403 Sframe_selected_window, 0, 1, 0,
404 doc: /* Return the selected window of FRAME-OR-WINDOW.
405 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
406 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
407 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
408 return the selected window of that frame. */)
409 (Lisp_Object frame_or_window)
411 Lisp_Object window;
413 if (NILP (frame_or_window))
414 window = SELECTED_FRAME ()->selected_window;
415 else if (WINDOW_VALID_P (frame_or_window))
416 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
417 else
419 CHECK_LIVE_FRAME (frame_or_window);
420 window = XFRAME (frame_or_window)->selected_window;
423 return window;
426 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
427 Sset_frame_selected_window, 2, 3, 0,
428 doc: /* Set selected window of FRAME to WINDOW.
429 FRAME must be a live frame and defaults to the selected one. If FRAME
430 is the selected frame, this makes WINDOW the selected window. Optional
431 argument NORECORD non-nil means to neither change the order of recently
432 selected windows nor the buffer list. WINDOW must denote a live window.
433 Return WINDOW. */)
434 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
436 if (NILP (frame))
437 frame = selected_frame;
439 CHECK_LIVE_FRAME (frame);
440 CHECK_LIVE_WINDOW (window);
442 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
443 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
445 if (EQ (frame, selected_frame))
446 return Fselect_window (window, norecord);
447 else
449 fset_selected_window (XFRAME (frame), window);
450 return window;
454 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
455 doc: /* Return the selected window.
456 The selected window is the window in which the standard cursor for
457 selected windows appears and to which many commands apply. */)
458 (void)
460 return selected_window;
463 int window_select_count;
465 /* If select_window is called with inhibit_point_swap non-zero it will
466 not store point of the old selected window's buffer back into that
467 window's pointm slot. This is needed by Fset_window_configuration to
468 avoid that the display routine is called with selected_window set to
469 Qnil causing a subsequent crash. */
470 static Lisp_Object
471 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
473 register struct window *w;
474 struct frame *sf;
476 CHECK_LIVE_WINDOW (window);
478 w = XWINDOW (window);
480 /* Make the selected window's buffer current. */
481 Fset_buffer (w->contents);
483 if (EQ (window, selected_window) && !inhibit_point_swap)
484 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
485 way to call record_buffer from Elisp, so it's important that we call
486 record_buffer before returning here. */
487 goto record_and_return;
489 if (NILP (norecord))
490 { /* Mark the window for redisplay since the selected-window has
491 a different mode-line. */
492 wset_redisplay (XWINDOW (selected_window));
493 wset_redisplay (w);
495 else
496 redisplay_other_windows ();
498 sf = SELECTED_FRAME ();
499 if (XFRAME (WINDOW_FRAME (w)) != sf)
501 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
502 /* Use this rather than Fhandle_switch_frame
503 so that FRAME_FOCUS_FRAME is moved appropriately as we
504 move around in the state where a minibuffer in a separate
505 frame is active. */
506 Fselect_frame (WINDOW_FRAME (w), norecord);
507 /* Fselect_frame called us back so we've done all the work already. */
508 eassert (EQ (window, selected_window));
509 return window;
511 else
512 fset_selected_window (sf, window);
514 select_window_1 (window, inhibit_point_swap);
515 bset_last_selected_window (XBUFFER (w->contents), window);
517 record_and_return:
518 /* record_buffer can run QUIT, so make sure it is run only after we have
519 re-established the invariant between selected_window and selected_frame,
520 otherwise the temporary broken invariant might "escape" (bug#14161). */
521 if (NILP (norecord))
523 w->use_time = ++window_select_count;
524 record_buffer (w->contents);
527 return window;
530 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
531 (not even for redisplay's benefit), and assume that the window's frame is
532 already selected. */
533 static void
534 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
536 /* Store the old selected window's buffer's point in pointm of the old
537 selected window. It belongs to that window, and when the window is
538 not selected, must be in the window. */
539 if (!inhibit_point_swap)
541 struct window *ow = XWINDOW (selected_window);
542 if (BUFFERP (ow->contents))
543 set_marker_both (ow->pointm, ow->contents,
544 BUF_PT (XBUFFER (ow->contents)),
545 BUF_PT_BYTE (XBUFFER (ow->contents)));
548 selected_window = window;
550 /* Go to the point recorded in the window.
551 This is important when the buffer is in more
552 than one window. It also matters when
553 redisplay_window has altered point after scrolling,
554 because it makes the change only in the window. */
555 set_point_from_marker (XWINDOW (window)->pointm);
558 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
559 doc: /* Select WINDOW which must be a live window.
560 Also make WINDOW's frame the selected frame and WINDOW that frame's
561 selected window. In addition, make WINDOW's buffer current and set that
562 buffer's value of `point' to the value of WINDOW's `window-point'.
563 Return WINDOW.
565 Optional second arg NORECORD non-nil means do not put this buffer at the
566 front of the buffer list and do not make this window the most recently
567 selected one.
569 Note that the main editor command loop sets the current buffer to the
570 buffer of the selected window before each command. */)
571 (register Lisp_Object window, Lisp_Object norecord)
573 return select_window (window, norecord, 0);
576 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
577 doc: /* Return the buffer displayed in window WINDOW.
578 If WINDOW is omitted or nil, it defaults to the selected window.
579 Return nil for an internal window or a deleted window. */)
580 (Lisp_Object window)
582 struct window *w = decode_any_window (window);
583 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
586 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
587 doc: /* Return the parent window of window WINDOW.
588 WINDOW must be a valid window and defaults to the selected one.
589 Return nil for a window with no parent (e.g. a root window). */)
590 (Lisp_Object window)
592 return decode_valid_window (window)->parent;
595 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
596 doc: /* Return the topmost child window of window WINDOW.
597 WINDOW must be a valid window and defaults to the selected one.
598 Return nil if WINDOW is a live window (live windows have no children).
599 Return nil if WINDOW is an internal window whose children form a
600 horizontal combination. */)
601 (Lisp_Object window)
603 struct window *w = decode_valid_window (window);
604 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
607 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
608 doc: /* Return the leftmost child window of window WINDOW.
609 WINDOW must be a valid window and defaults to the selected one.
610 Return nil if WINDOW is a live window (live windows have no children).
611 Return nil if WINDOW is an internal window whose children form a
612 vertical combination. */)
613 (Lisp_Object window)
615 struct window *w = decode_valid_window (window);
616 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
619 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
620 doc: /* Return the next sibling window of window WINDOW.
621 WINDOW must be a valid window and defaults to the selected one.
622 Return nil if WINDOW has no next sibling. */)
623 (Lisp_Object window)
625 return decode_valid_window (window)->next;
628 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
629 doc: /* Return the previous sibling window of window WINDOW.
630 WINDOW must be a valid window and defaults to the selected one.
631 Return nil if WINDOW has no previous sibling. */)
632 (Lisp_Object window)
634 return decode_valid_window (window)->prev;
637 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
638 doc: /* Return combination limit of window WINDOW.
639 WINDOW must be a valid window used in horizontal or vertical combination.
640 If the return value is nil, child windows of WINDOW can be recombined with
641 WINDOW's siblings. A return value of t means that child windows of
642 WINDOW are never \(re-)combined with WINDOW's siblings. */)
643 (Lisp_Object window)
645 struct window *w;
647 CHECK_VALID_WINDOW (window);
648 w = XWINDOW (window);
649 if (WINDOW_LEAF_P (w))
650 error ("Combination limit is meaningful for internal windows only");
651 return w->combination_limit;
654 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
655 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
656 WINDOW must be a valid window used in horizontal or vertical combination.
657 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
658 siblings. LIMIT t means that child windows of WINDOW are never
659 \(re-)combined with WINDOW's siblings. Other values are reserved for
660 future use. */)
661 (Lisp_Object window, Lisp_Object limit)
663 struct window *w;
665 CHECK_VALID_WINDOW (window);
666 w = XWINDOW (window);
667 if (WINDOW_LEAF_P (w))
668 error ("Combination limit is meaningful for internal windows only");
669 wset_combination_limit (w, limit);
670 return limit;
673 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
674 doc: /* Return the use time of window WINDOW.
675 WINDOW must be a live window and defaults to the selected one.
676 The window with the highest use time is the most recently selected
677 one. The window with the lowest use time is the least recently
678 selected one. */)
679 (Lisp_Object window)
681 return make_number (decode_live_window (window)->use_time);
684 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
685 doc: /* Return the pixel width of window WINDOW.
686 WINDOW must be a valid window and defaults to the selected one.
688 The return value includes the fringes and margins of WINDOW as well as
689 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
690 an internal window, its pixel width is the width of the screen areas
691 spanned by its children. */)
692 (Lisp_Object window)
694 return make_number (decode_valid_window (window)->pixel_width);
697 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
698 doc: /* Return the pixel height of window WINDOW.
699 WINDOW must be a valid window and defaults to the selected one.
701 The return value includes the mode line and header line, if any. If
702 WINDOW is an internal window, its pixel height is the height of the
703 screen areas spanned by its children. */)
704 (Lisp_Object window)
706 return make_number (decode_valid_window (window)->pixel_height);
709 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
710 doc: /* Return the total height, in lines, of window WINDOW.
711 WINDOW must be a valid window and defaults to the selected one.
713 The return value includes the mode line and header line, if any.
714 If WINDOW is an internal window, the total height is the height
715 of the screen areas spanned by its children.
717 On a graphical display, this total height is reported as an
718 integer multiple of the default character height. */)
719 (Lisp_Object window)
721 return make_number (decode_valid_window (window)->total_lines);
724 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
725 doc: /* Return the total width, in columns, of window WINDOW.
726 WINDOW must be a valid window and defaults to the selected one.
728 The return value includes any vertical dividers or scroll bars
729 belonging to WINDOW. If WINDOW is an internal window, the total width
730 is the width of the screen areas spanned by its children.
732 On a graphical display, this total width is reported as an
733 integer multiple of the default character width. */)
734 (Lisp_Object window)
736 return make_number (decode_valid_window (window)->total_cols);
739 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
740 doc: /* Return the new total size of window WINDOW.
741 WINDOW must be a valid window and defaults to the selected one. */)
742 (Lisp_Object window)
744 return decode_valid_window (window)->new_total;
747 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
748 doc: /* Return the normal height of window WINDOW.
749 WINDOW must be a valid window and defaults to the selected one.
750 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
751 (Lisp_Object window, Lisp_Object horizontal)
753 struct window *w = decode_valid_window (window);
755 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
758 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
759 doc: /* Return new normal size of window WINDOW.
760 WINDOW must be a valid window and defaults to the selected one. */)
761 (Lisp_Object window)
763 return decode_valid_window (window)->new_normal;
766 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
767 doc: /* Return new pixel size of window WINDOW.
768 WINDOW must be a valid window and defaults to the selected one. */)
769 (Lisp_Object window)
771 return decode_valid_window (window)->new_pixel;
774 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
775 doc: /* Return left pixel edge of window WINDOW.
776 WINDOW must be a valid window and defaults to the selected one. */)
777 (Lisp_Object window)
779 return make_number (decode_valid_window (window)->pixel_left);
782 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
783 doc: /* Return top pixel edge of window WINDOW.
784 WINDOW must be a valid window and defaults to the selected one. */)
785 (Lisp_Object window)
787 return make_number (decode_valid_window (window)->pixel_top);
790 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
791 doc: /* Return left column of window WINDOW.
792 This is the distance, in columns, between the left edge of WINDOW and
793 the left edge of the frame's window area. For instance, the return
794 value is 0 if there is no window to the left of WINDOW.
796 WINDOW must be a valid window and defaults to the selected one. */)
797 (Lisp_Object window)
799 return make_number (decode_valid_window (window)->left_col);
802 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
803 doc: /* Return top line of window WINDOW.
804 This is the distance, in lines, between the top of WINDOW and the top
805 of the frame's window area. For instance, the return value is 0 if
806 there is no window above WINDOW.
808 WINDOW must be a valid window and defaults to the selected one. */)
809 (Lisp_Object window)
811 return make_number (decode_valid_window (window)->top_line);
814 /* Return the number of lines of W's body. Don't count any mode or
815 header line of W. */
817 static int
818 window_body_height (struct window *w, bool pixelwise)
820 int pixels = (w->pixel_height
821 - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
822 - WINDOW_HEADER_LINE_HEIGHT (w)
823 - WINDOW_MODE_LINE_HEIGHT (w));
824 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
826 return pixelwise ? pixels : ((pixels + unit - 1) / unit);
829 /* Return the number of columns of W's body. Don't count columns
830 occupied by the scroll bar or the vertical bar separating W from its
831 right sibling. On window-systems don't count fringes or display
832 margins either. */
834 window_body_width (struct window *w, bool pixelwise)
836 struct frame *f = XFRAME (WINDOW_FRAME (w));
838 int pixels = (w->pixel_width
839 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
840 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
841 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
842 : ((!FRAME_WINDOW_P (f)
843 && !WINDOW_RIGHTMOST_P (w)
844 && !WINDOW_RIGHT_DIVIDER_WIDTH (w)
845 && !WINDOW_FULL_WIDTH_P (w))
846 /* According to Eli this is either 1 or 0. */
847 ? 1 : 0))
848 - WINDOW_MARGINS_WIDTH (w)
849 - (FRAME_WINDOW_P (f)
850 ? WINDOW_FRINGES_WIDTH (w)
851 : 0));
852 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
854 return pixelwise ? pixels : ((pixels + unit - 1) / unit);
857 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
858 doc: /* Return the height, in lines, of WINDOW's text area.
859 WINDOW must be a live window and defaults to the selected one.
861 Optional argument PIXELWISE non-nil means return the height in pixels.
863 The returned height does not include the mode line or header line. On a
864 graphical display, the height is expressed as an integer multiple of the
865 default character height if PIXELWISE is nil.
867 If PIXELWISE is nil and a line at the bottom of the text area is only
868 partially visible, that counts as a whole line; to exclude
869 partially-visible lines, use `window-text-height'. */)
870 (Lisp_Object window, Lisp_Object pixelwise)
872 return make_number (window_body_height (decode_live_window (window),
873 NILP (pixelwise) ? 0 : 1));
876 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
877 doc: /* Return the width, in columns, of WINDOW's text area.
878 WINDOW must be a live window and defaults to the selected one.
880 Optional argument PIXELWISE non-nil means return the width in pixels.
882 The return value does not include any vertical dividers, fringe or
883 marginal areas, or scroll bars. On a graphical display, the width is
884 expressed as an integer multiple of the default character width if
885 PIXELWISE is nil.
887 If PIXELWISE is nil and a column at the right of the text area is only
888 partially visible, that counts as a whole column; to exclude
889 partially-visible columns, use `window-text-width'. */)
890 (Lisp_Object window, Lisp_Object pixelwise)
892 return make_number (window_body_width (decode_live_window (window),
893 NILP (pixelwise) ? 0 : 1));
896 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
897 Swindow_mode_line_height, 0, 1, 0,
898 doc: /* Return the height in pixel of WINDOW's mode-line.
899 WINDOW must be a live window and defaults to the selected one. */)
900 (Lisp_Object window)
902 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
905 DEFUN ("window-header-line-height", Fwindow_header_line_height,
906 Swindow_header_line_height, 0, 1, 0,
907 doc: /* Return the height in pixel of WINDOW's header-line.
908 WINDOW must be a live window and defaults to the selected one. */)
909 (Lisp_Object window)
911 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
914 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
915 Swindow_right_divider_width, 0, 1, 0,
916 doc: /* Return the width of WINDOW's right divider.
917 WINDOW must be a live window and defaults to the selected one. */)
918 (Lisp_Object window)
920 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
923 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
924 Swindow_bottom_divider_width, 0, 1, 0,
925 doc: /* Return the width of WINDOW's bottom divider.
926 WINDOW must be a live window and defaults to the selected one. */)
927 (Lisp_Object window)
929 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
932 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
933 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
934 WINDOW must be a live window and defaults to the selected one. */)
935 (Lisp_Object window)
937 return make_number (decode_live_window (window)->hscroll);
940 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
941 range, returning the new amount as a fixnum. */
942 static Lisp_Object
943 set_window_hscroll (struct window *w, EMACS_INT hscroll)
945 /* Horizontal scrolling has problems with large scroll amounts.
946 It's too slow with long lines, and even with small lines the
947 display can be messed up. For now, though, impose only the limits
948 required by the internal representation: horizontal scrolling must
949 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
950 (since it's stored in a ptrdiff_t). */
951 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
952 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
954 /* Prevent redisplay shortcuts when changing the hscroll. */
955 if (w->hscroll != new_hscroll)
956 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
958 w->hscroll = new_hscroll;
959 return make_number (new_hscroll);
962 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
963 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
964 WINDOW must be a live window and defaults to the selected one.
965 Clip the number to a reasonable value if out of range.
966 Return the new number. NCOL should be zero or positive.
968 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
969 window so that the location of point moves off-window. */)
970 (Lisp_Object window, Lisp_Object ncol)
972 CHECK_NUMBER (ncol);
973 return set_window_hscroll (decode_live_window (window), XINT (ncol));
976 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
977 Swindow_redisplay_end_trigger, 0, 1, 0,
978 doc: /* Return WINDOW's redisplay end trigger value.
979 WINDOW must be a live window and defaults to the selected one.
980 See `set-window-redisplay-end-trigger' for more information. */)
981 (Lisp_Object window)
983 return decode_live_window (window)->redisplay_end_trigger;
986 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
987 Sset_window_redisplay_end_trigger, 2, 2, 0,
988 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
989 WINDOW must be a live window and defaults to the selected one. VALUE
990 should be a buffer position (typically a marker) or nil. If it is a
991 buffer position, then if redisplay in WINDOW reaches a position beyond
992 VALUE, the functions in `redisplay-end-trigger-functions' are called
993 with two arguments: WINDOW, and the end trigger value. Afterwards the
994 end-trigger value is reset to nil. */)
995 (register Lisp_Object window, Lisp_Object value)
997 wset_redisplay_end_trigger (decode_live_window (window), value);
998 return value;
1001 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1002 doc: /* Return a list of the edge coordinates of WINDOW.
1003 WINDOW must be a valid window and defaults to the selected one.
1005 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1006 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1007 0 at top left corner of frame.
1009 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1010 is one more than the bottommost row occupied by WINDOW. The edges
1011 include the space used by WINDOW's scroll bar, display margins, fringes,
1012 header line, and/or mode line. For the edges of just the text area, use
1013 `window-inside-edges'. */)
1014 (Lisp_Object window)
1016 register struct window *w = decode_valid_window (window);
1018 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1019 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1022 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1023 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1024 WINDOW must be a valid window and defaults to the selected one.
1026 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1027 0, 0 at the top left corner of the frame.
1029 RIGHT is one more than the rightmost x position occupied by WINDOW.
1030 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1031 The pixel edges include the space used by WINDOW's scroll bar, display
1032 margins, fringes, header line, and/or mode line. For the pixel edges
1033 of just the text area, use `window-inside-pixel-edges'. */)
1034 (Lisp_Object window)
1036 register struct window *w = decode_valid_window (window);
1038 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1039 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1042 static void
1043 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1045 struct frame *f = XFRAME (w->frame);
1046 *add_y = f->top_pos;
1047 #ifdef FRAME_MENUBAR_HEIGHT
1048 *add_y += FRAME_MENUBAR_HEIGHT (f);
1049 #endif
1050 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1051 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1052 #elif defined (FRAME_TOOLBAR_HEIGHT)
1053 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1054 #endif
1055 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1056 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1057 #endif
1058 *add_x = f->left_pos;
1059 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1060 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1061 #endif
1064 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1065 Swindow_absolute_pixel_edges, 0, 1, 0,
1066 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1067 WINDOW must be a valid window and defaults to the selected one.
1069 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1070 0, 0 at the top left corner of the display.
1072 RIGHT is one more than the rightmost x position occupied by WINDOW.
1073 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1074 The pixel edges include the space used by WINDOW's scroll bar, display
1075 margins, fringes, header line, and/or mode line. For the pixel edges
1076 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1077 (Lisp_Object window)
1079 register struct window *w = decode_valid_window (window);
1080 int add_x, add_y;
1082 calc_absolute_offset (w, &add_x, &add_y);
1084 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1085 WINDOW_TOP_EDGE_Y (w) + add_y,
1086 WINDOW_RIGHT_EDGE_X (w) + add_x,
1087 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1090 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1091 doc: /* Return a list of the edge coordinates of WINDOW.
1092 WINDOW must be a live window and defaults to the selected one.
1094 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1095 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1096 0 at top left corner of frame.
1098 RIGHT is one more than the rightmost column of WINDOW's text area.
1099 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1100 inside edges do not include the space used by the WINDOW's scroll bar,
1101 display margins, fringes, header line, and/or mode line. */)
1102 (Lisp_Object window)
1104 register struct window *w = decode_live_window (window);
1106 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1107 + WINDOW_LEFT_MARGIN_COLS (w)
1108 + WINDOW_LEFT_FRINGE_COLS (w)),
1109 (WINDOW_TOP_EDGE_LINE (w)
1110 + WINDOW_HEADER_LINE_LINES (w)),
1111 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1112 - WINDOW_RIGHT_MARGIN_COLS (w)
1113 - WINDOW_RIGHT_FRINGE_COLS (w)),
1114 (WINDOW_BOTTOM_EDGE_LINE (w)
1115 - WINDOW_MODE_LINE_LINES (w)));
1118 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1119 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1120 WINDOW must be a live 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's window area.
1125 RIGHT is one more than the rightmost x position of WINDOW's text area.
1126 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1127 The inside edges do not include the space used by WINDOW's scroll bar,
1128 display margins, fringes, header line, and/or mode line. */)
1129 (Lisp_Object window)
1131 register struct window *w = decode_live_window (window);
1133 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1134 + WINDOW_LEFT_MARGIN_WIDTH (w)
1135 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1136 (WINDOW_TOP_EDGE_Y (w)
1137 + WINDOW_HEADER_LINE_HEIGHT (w)),
1138 (WINDOW_BOX_RIGHT_EDGE_X (w)
1139 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1140 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1141 (WINDOW_BOTTOM_EDGE_Y (w)
1142 - WINDOW_MODE_LINE_HEIGHT (w)));
1145 DEFUN ("window-inside-absolute-pixel-edges",
1146 Fwindow_inside_absolute_pixel_edges,
1147 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1148 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1149 WINDOW must be a live window and defaults to the selected one.
1151 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1152 (0,0) at the top left corner of the frame's window area.
1154 RIGHT is one more than the rightmost x position of WINDOW's text area.
1155 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1156 The inside edges do not include the space used by WINDOW's scroll bar,
1157 display margins, fringes, header line, and/or mode line. */)
1158 (Lisp_Object window)
1160 register struct window *w = decode_live_window (window);
1161 int add_x, add_y;
1163 calc_absolute_offset (w, &add_x, &add_y);
1165 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1166 + WINDOW_LEFT_MARGIN_WIDTH (w)
1167 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1168 (WINDOW_TOP_EDGE_Y (w)
1169 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1170 (WINDOW_BOX_RIGHT_EDGE_X (w)
1171 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1172 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1173 (WINDOW_BOTTOM_EDGE_Y (w)
1174 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1177 /* Test if the character at column X, row Y is within window W.
1178 If it is not, return ON_NOTHING;
1179 if it is on the window's vertical divider, return
1180 ON_RIGHT_DIVIDER;
1181 if it is on the window's horizontal divider, return
1182 ON_BOTTOM_DIVIDER;
1183 if it is in the window's text area, return ON_TEXT;
1184 if it is on the window's modeline, return ON_MODE_LINE;
1185 if it is on the border between the window and its right sibling,
1186 return ON_VERTICAL_BORDER;
1187 if it is on a scroll bar, return ON_SCROLL_BAR;
1188 if it is on the window's top line, return ON_HEADER_LINE;
1189 if it is in left or right fringe of the window,
1190 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1191 if it is in the marginal area to the left/right of the window,
1192 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1194 X and Y are frame relative pixel coordinates. */
1196 static enum window_part
1197 coordinates_in_window (register struct window *w, int x, int y)
1199 struct frame *f = XFRAME (WINDOW_FRAME (w));
1200 enum window_part part;
1201 int ux = FRAME_COLUMN_WIDTH (f);
1202 int left_x = WINDOW_LEFT_EDGE_X (w);
1203 int right_x = WINDOW_RIGHT_EDGE_X (w);
1204 int top_y = WINDOW_TOP_EDGE_Y (w);
1205 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1206 /* The width of the area where the vertical line can be dragged.
1207 (Between mode lines for instance. */
1208 int grabbable_width = ux;
1209 int lmargin_width, rmargin_width, text_left, text_right;
1211 /* Outside any interesting row or column? */
1212 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1213 return ON_NOTHING;
1215 /* On vertical window divider (which prevails horizontal
1216 dividers)? */
1217 if (!WINDOW_RIGHTMOST_P (w)
1218 && WINDOW_RIGHT_DIVIDER_WIDTH (w)
1219 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1220 && x <= right_x)
1221 return ON_RIGHT_DIVIDER;
1222 /* On the horizontal window divider? */
1223 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1224 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1225 && y <= bottom_y)
1226 return ON_BOTTOM_DIVIDER;
1227 /* On the mode or header line? */
1228 else if ((WINDOW_WANTS_MODELINE_P (w)
1229 && y >= (bottom_y
1230 - CURRENT_MODE_LINE_HEIGHT (w)
1231 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1232 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1233 && (part = ON_MODE_LINE))
1234 || (WINDOW_WANTS_HEADER_LINE_P (w)
1235 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1236 && (part = ON_HEADER_LINE)))
1238 /* If it's under/over the scroll bar portion of the mode/header
1239 line, say it's on the vertical line. That's to be able to
1240 resize windows horizontally in case we're using toolkit scroll
1241 bars. Note: If scrollbars are on the left, the window that
1242 must be eventually resized is that on the left of WINDOW. */
1243 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1244 && !WINDOW_LEFTMOST_P (w)
1245 && eabs (x - left_x) < grabbable_width)
1246 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1247 && !WINDOW_RIGHTMOST_P (w)
1248 && eabs (x - right_x) < grabbable_width))
1249 return ON_VERTICAL_BORDER;
1250 else
1251 return part;
1254 /* In what's below, we subtract 1 when computing right_x because we
1255 want the rightmost pixel, which is given by left_pixel+width-1. */
1256 if (w->pseudo_window_p)
1258 left_x = 0;
1259 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1261 else
1263 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1264 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1267 /* Outside any interesting column? */
1268 if (x < left_x || x > right_x)
1269 return ON_SCROLL_BAR;
1271 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1272 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1274 text_left = window_box_left (w, TEXT_AREA);
1275 text_right = text_left + window_box_width (w, TEXT_AREA);
1277 if (FRAME_WINDOW_P (f))
1279 if (!w->pseudo_window_p
1280 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1281 && !WINDOW_RIGHTMOST_P (w)
1282 && (eabs (x - right_x) < grabbable_width))
1283 return ON_VERTICAL_BORDER;
1285 /* Need to say "x > right_x" rather than >=, since on character
1286 terminals, the vertical line's x coordinate is right_x. */
1287 else if (!w->pseudo_window_p
1288 && !WINDOW_RIGHTMOST_P (w)
1289 /* Why check ux if we are not the rightmost window? Also
1290 shouldn't a pseudo window always be rightmost? */
1291 && x > right_x - ux)
1292 return ON_VERTICAL_BORDER;
1294 if (x < text_left)
1296 if (lmargin_width > 0
1297 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1298 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1299 : (x < left_x + lmargin_width)))
1300 return ON_LEFT_MARGIN;
1302 return ON_LEFT_FRINGE;
1305 if (x >= text_right)
1307 if (rmargin_width > 0
1308 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1309 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1310 : (x >= right_x - rmargin_width)))
1311 return ON_RIGHT_MARGIN;
1313 return ON_RIGHT_FRINGE;
1316 /* Everything special ruled out - must be on text area */
1317 return ON_TEXT;
1320 /* Take X is the frame-relative pixel x-coordinate, and return the
1321 x-coordinate relative to part PART of window W. */
1323 window_relative_x_coord (struct window *w, enum window_part part, int x)
1325 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1327 switch (part)
1329 case ON_TEXT:
1330 return x - window_box_left (w, TEXT_AREA);
1332 case ON_LEFT_FRINGE:
1333 return x - left_x;
1335 case ON_RIGHT_FRINGE:
1336 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1338 case ON_LEFT_MARGIN:
1339 return (x - left_x
1340 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1341 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1343 case ON_RIGHT_MARGIN:
1344 return (x + 1
1345 - ((w->pseudo_window_p)
1346 ? WINDOW_PIXEL_WIDTH (w)
1347 : WINDOW_BOX_RIGHT_EDGE_X (w))
1348 + window_box_width (w, RIGHT_MARGIN_AREA)
1349 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1350 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1353 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1354 return 0;
1358 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1359 Scoordinates_in_window_p, 2, 2, 0,
1360 doc: /* Return non-nil if COORDINATES are in WINDOW.
1361 WINDOW must be a live window and defaults to the selected one.
1362 COORDINATES is a cons of the form (X . Y), X and Y being distances
1363 measured in characters from the upper-left corner of the frame.
1364 \(0 . 0) denotes the character in the upper left corner of the
1365 frame.
1366 If COORDINATES are in the text portion of WINDOW,
1367 the coordinates relative to the window are returned.
1368 If they are in the mode line of WINDOW, `mode-line' is returned.
1369 If they are in the top mode line of WINDOW, `header-line' is returned.
1370 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1371 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1372 If they are on the border between WINDOW and its right sibling,
1373 `vertical-line' is returned.
1374 If they are in the windows's left or right marginal areas, `left-margin'\n\
1375 or `right-margin' is returned. */)
1376 (register Lisp_Object coordinates, Lisp_Object window)
1378 struct window *w;
1379 struct frame *f;
1380 int x, y;
1381 Lisp_Object lx, ly;
1383 w = decode_live_window (window);
1384 f = XFRAME (w->frame);
1385 CHECK_CONS (coordinates);
1386 lx = Fcar (coordinates);
1387 ly = Fcdr (coordinates);
1388 CHECK_NUMBER_OR_FLOAT (lx);
1389 CHECK_NUMBER_OR_FLOAT (ly);
1390 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1391 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1393 switch (coordinates_in_window (w, x, y))
1395 case ON_NOTHING:
1396 return Qnil;
1398 case ON_TEXT:
1399 /* Convert X and Y to window relative pixel coordinates, and
1400 return the canonical char units. */
1401 x -= window_box_left (w, TEXT_AREA);
1402 y -= WINDOW_TOP_EDGE_Y (w);
1403 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1404 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1406 case ON_MODE_LINE:
1407 return Qmode_line;
1409 case ON_VERTICAL_BORDER:
1410 return Qvertical_line;
1412 case ON_HEADER_LINE:
1413 return Qheader_line;
1415 case ON_LEFT_FRINGE:
1416 return Qleft_fringe;
1418 case ON_RIGHT_FRINGE:
1419 return Qright_fringe;
1421 case ON_LEFT_MARGIN:
1422 return Qleft_margin;
1424 case ON_RIGHT_MARGIN:
1425 return Qright_margin;
1427 case ON_SCROLL_BAR:
1428 /* Historically we are supposed to return nil in this case. */
1429 return Qnil;
1431 case ON_RIGHT_DIVIDER:
1432 return Qright_divider;
1434 case ON_BOTTOM_DIVIDER:
1435 return Qbottom_divider;
1437 default:
1438 emacs_abort ();
1443 /* Callback for foreach_window, used in window_from_coordinates.
1444 Check if window W contains coordinates specified by USER_DATA which
1445 is actually a pointer to a struct check_window_data CW.
1447 Check if window W contains coordinates *CW->x and *CW->y. If it
1448 does, return W in *CW->window, as Lisp_Object, and return in
1449 *CW->part the part of the window under coordinates *X,*Y. Return
1450 zero from this function to stop iterating over windows. */
1452 struct check_window_data
1454 Lisp_Object *window;
1455 int x, y;
1456 enum window_part *part;
1459 static int
1460 check_window_containing (struct window *w, void *user_data)
1462 struct check_window_data *cw = user_data;
1463 enum window_part found;
1464 int continue_p = 1;
1466 found = coordinates_in_window (w, cw->x, cw->y);
1467 if (found != ON_NOTHING)
1469 *cw->part = found;
1470 XSETWINDOW (*cw->window, w);
1471 continue_p = 0;
1474 return continue_p;
1478 /* Find the window containing frame-relative pixel position X/Y and
1479 return it as a Lisp_Object.
1481 If X, Y is on one of the window's special `window_part' elements,
1482 set *PART to the id of that element.
1484 If there is no window under X, Y return nil and leave *PART
1485 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1487 This function was previously implemented with a loop cycling over
1488 windows with Fnext_window, and starting with the frame's selected
1489 window. It turned out that this doesn't work with an
1490 implementation of next_window using Vwindow_list, because
1491 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1492 tree of F when this function is called asynchronously from
1493 note_mouse_highlight. The original loop didn't terminate in this
1494 case. */
1496 Lisp_Object
1497 window_from_coordinates (struct frame *f, int x, int y,
1498 enum window_part *part, bool tool_bar_p)
1500 Lisp_Object window;
1501 struct check_window_data cw;
1502 enum window_part dummy;
1504 if (part == 0)
1505 part = &dummy;
1507 window = Qnil;
1508 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1509 foreach_window (f, check_window_containing, &cw);
1511 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1512 /* If not found above, see if it's in the tool bar window, if a tool
1513 bar exists. */
1514 if (NILP (window)
1515 && tool_bar_p
1516 && WINDOWP (f->tool_bar_window)
1517 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1518 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1519 != ON_NOTHING))
1521 *part = ON_TEXT;
1522 window = f->tool_bar_window;
1524 #endif
1526 return window;
1529 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1530 doc: /* Return window containing coordinates X and Y on FRAME.
1531 FRAME must be a live frame and defaults to the selected one.
1532 The top left corner of the frame is considered to be row 0,
1533 column 0. */)
1534 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1536 struct frame *f = decode_live_frame (frame);
1538 /* Check that arguments are integers or floats. */
1539 CHECK_NUMBER_OR_FLOAT (x);
1540 CHECK_NUMBER_OR_FLOAT (y);
1542 return window_from_coordinates (f,
1543 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1544 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1545 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1546 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1547 0, 0);
1550 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1551 doc: /* Return current value of point in WINDOW.
1552 WINDOW must be a live window and defaults to the selected one.
1554 For a nonselected window, this is the value point would have if that
1555 window were selected.
1557 Note that, when WINDOW is selected, the value returned is the same as
1558 that returned by `point' for WINDOW's buffer. It would be more strictly
1559 correct to return the top-level value of `point', outside of any
1560 `save-excursion' forms. But that is hard to define. */)
1561 (Lisp_Object window)
1563 register struct window *w = decode_live_window (window);
1565 if (w == XWINDOW (selected_window))
1566 return make_number (BUF_PT (XBUFFER (w->contents)));
1567 else
1568 return Fmarker_position (w->pointm);
1571 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1572 doc: /* Return position at which display currently starts in WINDOW.
1573 WINDOW must be a live window and defaults to the selected one.
1574 This is updated by redisplay or by calling `set-window-start'. */)
1575 (Lisp_Object window)
1577 return Fmarker_position (decode_live_window (window)->start);
1580 /* This is text temporarily removed from the doc string below.
1582 This function returns nil if the position is not currently known.
1583 That happens when redisplay is preempted and doesn't finish.
1584 If in that case you want to compute where the end of the window would
1585 have been if redisplay had finished, do this:
1586 (save-excursion
1587 (goto-char (window-start window))
1588 (vertical-motion (1- (window-height window)) window)
1589 (point))") */
1591 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1592 doc: /* Return position at which display currently ends in WINDOW.
1593 WINDOW must be a live window and defaults to the selected one.
1594 This is updated by redisplay, when it runs to completion.
1595 Simply changing the buffer text or setting `window-start'
1596 does not update this value.
1597 Return nil if there is no recorded value. (This can happen if the
1598 last redisplay of WINDOW was preempted, and did not finish.)
1599 If UPDATE is non-nil, compute the up-to-date position
1600 if it isn't already recorded. */)
1601 (Lisp_Object window, Lisp_Object update)
1603 Lisp_Object value;
1604 struct window *w = decode_live_window (window);
1605 Lisp_Object buf;
1606 struct buffer *b;
1608 buf = w->contents;
1609 CHECK_BUFFER (buf);
1610 b = XBUFFER (buf);
1612 if (! NILP (update)
1613 && (windows_or_buffers_changed
1614 || !w->window_end_valid
1615 || b->clip_changed
1616 || b->prevent_redisplay_optimizations_p
1617 || window_outdated (w))
1618 && !noninteractive)
1620 struct text_pos startp;
1621 struct it it;
1622 struct buffer *old_buffer = NULL;
1623 void *itdata = NULL;
1625 /* Cannot use Fvertical_motion because that function doesn't
1626 cope with variable-height lines. */
1627 if (b != current_buffer)
1629 old_buffer = current_buffer;
1630 set_buffer_internal (b);
1633 /* In case W->start is out of the range, use something
1634 reasonable. This situation occurred when loading a file with
1635 `-l' containing a call to `rmail' with subsequent other
1636 commands. At the end, W->start happened to be BEG, while
1637 rmail had already narrowed the buffer. */
1638 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1640 itdata = bidi_shelve_cache ();
1641 start_display (&it, w, startp);
1642 move_it_vertically (&it, window_box_height (w));
1643 if (it.current_y < it.last_visible_y)
1644 move_it_past_eol (&it);
1645 value = make_number (IT_CHARPOS (it));
1646 bidi_unshelve_cache (itdata, 0);
1648 if (old_buffer)
1649 set_buffer_internal (old_buffer);
1651 else
1652 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1654 return value;
1657 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1658 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1659 WINDOW must be a live window and defaults to the selected one.
1660 Return POS. */)
1661 (Lisp_Object window, Lisp_Object pos)
1663 register struct window *w = decode_live_window (window);
1665 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1667 if (w == XWINDOW (selected_window))
1669 if (XBUFFER (w->contents) == current_buffer)
1670 Fgoto_char (pos);
1671 else
1673 struct buffer *old_buffer = current_buffer;
1675 /* ... but here we want to catch type error before buffer change. */
1676 CHECK_NUMBER_COERCE_MARKER (pos);
1677 set_buffer_internal (XBUFFER (w->contents));
1678 Fgoto_char (pos);
1679 set_buffer_internal (old_buffer);
1682 else
1684 set_marker_restricted (w->pointm, pos, w->contents);
1685 /* We have to make sure that redisplay updates the window to show
1686 the new value of point. */
1687 wset_redisplay (w);
1690 return pos;
1693 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1694 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1695 WINDOW must be a live window and defaults to the selected one. Return
1696 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1697 overriding motion of point in order to display at this exact start. */)
1698 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1700 register struct window *w = decode_live_window (window);
1702 set_marker_restricted (w->start, pos, w->contents);
1703 /* This is not right, but much easier than doing what is right. */
1704 w->start_at_line_beg = 0;
1705 if (NILP (noforce))
1706 w->force_start = 1;
1707 w->update_mode_line = 1;
1708 /* Bug#15957. */
1709 w->window_end_valid = 0;
1710 wset_redisplay (w);
1712 return pos;
1715 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1716 Spos_visible_in_window_p, 0, 3, 0,
1717 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1718 WINDOW must be a live window and defaults to the selected one.
1720 Return nil if that position is scrolled vertically out of view. If a
1721 character is only partially visible, nil is returned, unless the
1722 optional argument PARTIALLY is non-nil. If POS is only out of view
1723 because of horizontal scrolling, return non-nil. If POS is t, it
1724 specifies the position of the last visible glyph in WINDOW. POS
1725 defaults to point in WINDOW; WINDOW defaults to the selected window.
1727 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1728 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1729 where X and Y are the pixel coordinates relative to the top left corner
1730 of the window. The remaining elements are omitted if the character after
1731 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1732 off-window at the top and bottom of the screen line ("row") containing
1733 POS, ROWH is the visible height of that row, and VPOS is the row number
1734 \(zero-based). */)
1735 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1737 register struct window *w;
1738 register EMACS_INT posint;
1739 register struct buffer *buf;
1740 struct text_pos top;
1741 Lisp_Object in_window = Qnil;
1742 int rtop, rbot, rowh, vpos, fully_p = 1;
1743 int x, y;
1745 w = decode_live_window (window);
1746 buf = XBUFFER (w->contents);
1747 SET_TEXT_POS_FROM_MARKER (top, w->start);
1749 if (EQ (pos, Qt))
1750 posint = -1;
1751 else if (!NILP (pos))
1753 CHECK_NUMBER_COERCE_MARKER (pos);
1754 posint = XINT (pos);
1756 else if (w == XWINDOW (selected_window))
1757 posint = PT;
1758 else
1759 posint = marker_position (w->pointm);
1761 /* If position is above window start or outside buffer boundaries,
1762 or if window start is out of range, position is not visible. */
1763 if ((EQ (pos, Qt)
1764 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1765 && CHARPOS (top) >= BUF_BEGV (buf)
1766 && CHARPOS (top) <= BUF_ZV (buf)
1767 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1768 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1769 in_window = Qt;
1771 if (!NILP (in_window) && !NILP (partially))
1773 Lisp_Object part = Qnil;
1774 if (!fully_p)
1775 part = list4i (rtop, rbot, rowh, vpos);
1776 in_window = Fcons (make_number (x),
1777 Fcons (make_number (y), part));
1780 return in_window;
1783 DEFUN ("window-line-height", Fwindow_line_height,
1784 Swindow_line_height, 0, 2, 0,
1785 doc: /* Return height in pixels of text line LINE in window WINDOW.
1786 WINDOW must be a live window and defaults to the selected one.
1788 Return height of current line if LINE is omitted or nil. Return height of
1789 header or mode line if LINE is `header-line' or `mode-line'.
1790 Otherwise, LINE is a text line number starting from 0. A negative number
1791 counts from the end of the window.
1793 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1794 in pixels of the visible part of the line, VPOS and YPOS are the
1795 vertical position in lines and pixels of the line, relative to the top
1796 of the first text line, and OFFBOT is the number of off-window pixels at
1797 the bottom of the text line. If there are off-window pixels at the top
1798 of the (first) text line, YPOS is negative.
1800 Return nil if window display is not up-to-date. In that case, use
1801 `pos-visible-in-window-p' to obtain the information. */)
1802 (Lisp_Object line, Lisp_Object window)
1804 register struct window *w;
1805 register struct buffer *b;
1806 struct glyph_row *row, *end_row;
1807 int max_y, crop, i;
1808 EMACS_INT n;
1810 w = decode_live_window (window);
1812 if (noninteractive || w->pseudo_window_p)
1813 return Qnil;
1815 CHECK_BUFFER (w->contents);
1816 b = XBUFFER (w->contents);
1818 /* Fail if current matrix is not up-to-date. */
1819 if (!w->window_end_valid
1820 || windows_or_buffers_changed
1821 || b->clip_changed
1822 || b->prevent_redisplay_optimizations_p
1823 || window_outdated (w))
1824 return Qnil;
1826 if (NILP (line))
1828 i = w->cursor.vpos;
1829 if (i < 0 || i >= w->current_matrix->nrows
1830 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1831 return Qnil;
1832 max_y = window_text_bottom_y (w);
1833 goto found_row;
1836 if (EQ (line, Qheader_line))
1838 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1839 return Qnil;
1840 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1841 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1844 if (EQ (line, Qmode_line))
1846 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1847 return (row->enabled_p ?
1848 list4i (row->height,
1849 0, /* not accurate */
1850 (WINDOW_HEADER_LINE_HEIGHT (w)
1851 + window_text_bottom_y (w)),
1853 : Qnil);
1856 CHECK_NUMBER (line);
1857 n = XINT (line);
1859 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1860 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1861 max_y = window_text_bottom_y (w);
1862 i = 0;
1864 while ((n < 0 || i < n)
1865 && row <= end_row && row->enabled_p
1866 && row->y + row->height < max_y)
1867 row++, i++;
1869 if (row > end_row || !row->enabled_p)
1870 return Qnil;
1872 if (++n < 0)
1874 if (-n > i)
1875 return Qnil;
1876 row += n;
1877 i += n;
1880 found_row:
1881 crop = max (0, (row->y + row->height) - max_y);
1882 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1885 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1886 0, 1, 0,
1887 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1888 More precisely, return the value assigned by the last call of
1889 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1890 never called with WINDOW as its argument, or the value set by that
1891 function was internally reset since its last call. WINDOW must be a
1892 live window and defaults to the selected one.
1894 When a window is dedicated to its buffer, `display-buffer' will refrain
1895 from displaying another buffer in it. `get-lru-window' and
1896 `get-largest-window' treat dedicated windows specially.
1897 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1898 `kill-buffer' can delete a dedicated window and the containing frame.
1900 Functions like `set-window-buffer' may change the buffer displayed by a
1901 window, unless that window is "strongly" dedicated to its buffer, that
1902 is the value returned by `window-dedicated-p' is t. */)
1903 (Lisp_Object window)
1905 return decode_live_window (window)->dedicated;
1908 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1909 Sset_window_dedicated_p, 2, 2, 0,
1910 doc: /* Mark WINDOW as dedicated according to FLAG.
1911 WINDOW must be a live window and defaults to the selected one. FLAG
1912 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1913 mark WINDOW as non-dedicated. Return FLAG.
1915 When a window is dedicated to its buffer, `display-buffer' will refrain
1916 from displaying another buffer in it. `get-lru-window' and
1917 `get-largest-window' treat dedicated windows specially.
1918 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1919 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1920 and the containing frame.
1922 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1923 its buffer. Functions like `set-window-buffer' may change the buffer
1924 displayed by a window, unless that window is strongly dedicated to its
1925 buffer. If and when `set-window-buffer' displays another buffer in a
1926 window, it also makes sure that the window is no more dedicated. */)
1927 (Lisp_Object window, Lisp_Object flag)
1929 wset_dedicated (decode_live_window (window), flag);
1930 return flag;
1933 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1934 0, 1, 0,
1935 doc: /* Return buffers previously shown in WINDOW.
1936 WINDOW must be a live window and defaults to the selected one.
1938 The return value is a list of elements (BUFFER WINDOW-START POS),
1939 where BUFFER is a buffer, WINDOW-START is the start position of the
1940 window for that buffer, and POS is a window-specific point value. */)
1941 (Lisp_Object window)
1943 return decode_live_window (window)->prev_buffers;
1946 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1947 Sset_window_prev_buffers, 2, 2, 0,
1948 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1949 WINDOW must be a live window and defaults to the selected one.
1951 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1952 where BUFFER is a buffer, WINDOW-START is the start position of the
1953 window for that buffer, and POS is a window-specific point value. */)
1954 (Lisp_Object window, Lisp_Object prev_buffers)
1956 wset_prev_buffers (decode_live_window (window), prev_buffers);
1957 return prev_buffers;
1960 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1961 0, 1, 0,
1962 doc: /* Return list of buffers recently re-shown in WINDOW.
1963 WINDOW must be a live window and defaults to the selected one. */)
1964 (Lisp_Object window)
1966 return decode_live_window (window)->next_buffers;
1969 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1970 Sset_window_next_buffers, 2, 2, 0,
1971 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1972 WINDOW must be a live window and defaults to the selected one.
1973 NEXT-BUFFERS should be a list of buffers. */)
1974 (Lisp_Object window, Lisp_Object next_buffers)
1976 wset_next_buffers (decode_live_window (window), next_buffers);
1977 return next_buffers;
1980 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1981 0, 1, 0,
1982 doc: /* Return the parameters of WINDOW and their values.
1983 WINDOW must be a valid window and defaults to the selected one. The
1984 return value is a list of elements of the form (PARAMETER . VALUE). */)
1985 (Lisp_Object window)
1987 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1990 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1991 2, 2, 0,
1992 doc: /* Return WINDOW's value for PARAMETER.
1993 WINDOW can be any window and defaults to the selected one. */)
1994 (Lisp_Object window, Lisp_Object parameter)
1996 Lisp_Object result;
1998 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1999 return CDR_SAFE (result);
2002 DEFUN ("set-window-parameter", Fset_window_parameter,
2003 Sset_window_parameter, 3, 3, 0,
2004 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2005 WINDOW can be any window and defaults to the selected one.
2006 Return VALUE. */)
2007 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2009 register struct window *w = decode_any_window (window);
2010 Lisp_Object old_alist_elt;
2012 old_alist_elt = Fassq (parameter, w->window_parameters);
2013 if (NILP (old_alist_elt))
2014 wset_window_parameters
2015 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2016 else
2017 Fsetcdr (old_alist_elt, value);
2018 return value;
2021 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2022 0, 1, 0,
2023 doc: /* Return the display-table that WINDOW is using.
2024 WINDOW must be a live window and defaults to the selected one. */)
2025 (Lisp_Object window)
2027 return decode_live_window (window)->display_table;
2030 /* Get the display table for use on window W. This is either W's
2031 display table or W's buffer's display table. Ignore the specified
2032 tables if they are not valid; if no valid table is specified,
2033 return 0. */
2035 struct Lisp_Char_Table *
2036 window_display_table (struct window *w)
2038 struct Lisp_Char_Table *dp = NULL;
2040 if (DISP_TABLE_P (w->display_table))
2041 dp = XCHAR_TABLE (w->display_table);
2042 else if (BUFFERP (w->contents))
2044 struct buffer *b = XBUFFER (w->contents);
2046 if (DISP_TABLE_P (BVAR (b, display_table)))
2047 dp = XCHAR_TABLE (BVAR (b, display_table));
2048 else if (DISP_TABLE_P (Vstandard_display_table))
2049 dp = XCHAR_TABLE (Vstandard_display_table);
2052 return dp;
2055 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2056 doc: /* Set WINDOW's display-table to TABLE.
2057 WINDOW must be a live window and defaults to the selected one. */)
2058 (register Lisp_Object window, Lisp_Object table)
2060 wset_display_table (decode_live_window (window), table);
2061 return table;
2064 /* Record info on buffer window W is displaying
2065 when it is about to cease to display that buffer. */
2066 static void
2067 unshow_buffer (register struct window *w)
2069 Lisp_Object buf = w->contents;
2070 struct buffer *b = XBUFFER (buf);
2072 eassert (b == XMARKER (w->pointm)->buffer);
2074 #if 0
2075 if (w == XWINDOW (selected_window)
2076 || ! EQ (buf, XWINDOW (selected_window)->contents))
2077 /* Do this except when the selected window's buffer
2078 is being removed from some other window. */
2079 #endif
2080 /* last_window_start records the start position that this buffer
2081 had in the last window to be disconnected from it.
2082 Now that this statement is unconditional,
2083 it is possible for the buffer to be displayed in the
2084 selected window, while last_window_start reflects another
2085 window which was recently showing the same buffer.
2086 Some people might say that might be a good thing. Let's see. */
2087 b->last_window_start = marker_position (w->start);
2089 /* Point in the selected window's buffer
2090 is actually stored in that buffer, and the window's pointm isn't used.
2091 So don't clobber point in that buffer. */
2092 if (! EQ (buf, XWINDOW (selected_window)->contents)
2093 /* Don't clobber point in current buffer either (this could be
2094 useful in connection with bug#12208).
2095 && XBUFFER (buf) != current_buffer */
2096 /* This line helps to fix Horsley's testbug.el bug. */
2097 && !(WINDOWP (BVAR (b, last_selected_window))
2098 && w != XWINDOW (BVAR (b, last_selected_window))
2099 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2100 temp_set_point_both (b,
2101 clip_to_bounds (BUF_BEGV (b),
2102 marker_position (w->pointm),
2103 BUF_ZV (b)),
2104 clip_to_bounds (BUF_BEGV_BYTE (b),
2105 marker_byte_position (w->pointm),
2106 BUF_ZV_BYTE (b)));
2108 if (WINDOWP (BVAR (b, last_selected_window))
2109 && w == XWINDOW (BVAR (b, last_selected_window)))
2110 bset_last_selected_window (b, Qnil);
2113 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2114 means change window structure only. Otherwise store geometry and
2115 other settings as well. */
2116 static void
2117 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2119 register Lisp_Object tem;
2120 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2122 /* If OLD is its frame's root window, then NEW is the new
2123 root window for that frame. */
2124 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2125 fset_root_window (XFRAME (o->frame), new);
2127 if (setflag)
2129 n->pixel_left = o->pixel_left;
2130 n->pixel_top = o->pixel_top;
2131 n->pixel_width = o->pixel_width;
2132 n->pixel_height = o->pixel_height;
2133 n->left_col = o->left_col;
2134 n->top_line = o->top_line;
2135 n->total_cols = o->total_cols;
2136 n->total_lines = o->total_lines;
2137 wset_normal_cols (n, o->normal_cols);
2138 wset_normal_cols (o, make_float (1.0));
2139 wset_normal_lines (n, o->normal_lines);
2140 wset_normal_lines (o, make_float (1.0));
2141 n->desired_matrix = n->current_matrix = 0;
2142 n->vscroll = 0;
2143 memset (&n->cursor, 0, sizeof (n->cursor));
2144 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2145 n->last_cursor_vpos = 0;
2146 #ifdef HAVE_WINDOW_SYSTEM
2147 n->phys_cursor_type = NO_CURSOR;
2148 n->phys_cursor_width = -1;
2149 #endif
2150 n->must_be_updated_p = 0;
2151 n->pseudo_window_p = 0;
2152 n->window_end_vpos = 0;
2153 n->window_end_pos = 0;
2154 n->window_end_valid = 0;
2157 tem = o->next;
2158 wset_next (n, tem);
2159 if (!NILP (tem))
2160 wset_prev (XWINDOW (tem), new);
2162 tem = o->prev;
2163 wset_prev (n, tem);
2164 if (!NILP (tem))
2165 wset_next (XWINDOW (tem), new);
2167 tem = o->parent;
2168 wset_parent (n, tem);
2169 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2170 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2173 /* If window WINDOW and its parent window are iso-combined, merge
2174 WINDOW's children into those of its parent window and mark WINDOW as
2175 deleted. */
2177 static void
2178 recombine_windows (Lisp_Object window)
2180 struct window *w, *p, *c;
2181 Lisp_Object parent, child;
2182 bool horflag;
2184 w = XWINDOW (window);
2185 parent = w->parent;
2186 if (!NILP (parent) && NILP (w->combination_limit))
2188 p = XWINDOW (parent);
2189 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2190 && p->horizontal == w->horizontal)
2191 /* WINDOW and PARENT are both either a vertical or a horizontal
2192 combination. */
2194 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2195 child = w->contents;
2196 c = XWINDOW (child);
2198 /* Splice WINDOW's children into its parent's children and
2199 assign new normal sizes. */
2200 if (NILP (w->prev))
2201 wset_combination (p, horflag, child);
2202 else
2204 wset_prev (c, w->prev);
2205 wset_next (XWINDOW (w->prev), child);
2208 while (c)
2210 wset_parent (c, parent);
2212 if (horflag)
2213 wset_normal_cols
2214 (c, make_float ((double) c->pixel_width
2215 / (double) p->pixel_width));
2216 else
2217 wset_normal_lines
2218 (c, make_float ((double) c->pixel_height
2219 / (double) p->pixel_height));
2221 if (NILP (c->next))
2223 if (!NILP (w->next))
2225 wset_next (c, w->next);
2226 wset_prev (XWINDOW (c->next), child);
2229 c = 0;
2231 else
2233 child = c->next;
2234 c = XWINDOW (child);
2238 /* WINDOW can be deleted now. */
2239 wset_combination (w, 0, Qnil);
2244 /* If WINDOW can be deleted, delete it. */
2245 static void
2246 delete_deletable_window (Lisp_Object window)
2248 if (!NILP (call1 (Qwindow_deletable_p, window)))
2249 call1 (Qdelete_window, window);
2252 /***********************************************************************
2253 Window List
2254 ***********************************************************************/
2256 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2257 pointer. This is a callback function for foreach_window, used in
2258 the window_list function. */
2260 static int
2261 add_window_to_list (struct window *w, void *user_data)
2263 Lisp_Object *list = user_data;
2264 Lisp_Object window;
2265 XSETWINDOW (window, w);
2266 *list = Fcons (window, *list);
2267 return 1;
2271 /* Return a list of all windows, for use by next_window. If
2272 Vwindow_list is a list, return that list. Otherwise, build a new
2273 list, cache it in Vwindow_list, and return that. */
2275 Lisp_Object
2276 window_list (void)
2278 if (!CONSP (Vwindow_list))
2280 Lisp_Object tail, frame;
2282 Vwindow_list = Qnil;
2283 FOR_EACH_FRAME (tail, frame)
2285 Lisp_Object args[2];
2287 /* We are visiting windows in canonical order, and add
2288 new windows at the front of args[1], which means we
2289 have to reverse this list at the end. */
2290 args[1] = Qnil;
2291 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2292 args[0] = Vwindow_list;
2293 args[1] = Fnreverse (args[1]);
2294 Vwindow_list = Fnconc (2, args);
2298 return Vwindow_list;
2302 /* Value is non-zero if WINDOW satisfies the constraints given by
2303 OWINDOW, MINIBUF and ALL_FRAMES.
2305 MINIBUF t means WINDOW may be minibuffer windows.
2306 `lambda' means WINDOW may not be a minibuffer window.
2307 a window means a specific minibuffer window
2309 ALL_FRAMES t means search all frames,
2310 nil means search just current frame,
2311 `visible' means search just visible frames on the
2312 current terminal,
2313 0 means search visible and iconified frames on the
2314 current terminal,
2315 a window means search the frame that window belongs to,
2316 a frame means consider windows on that frame, only. */
2318 static bool
2319 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2320 Lisp_Object minibuf, Lisp_Object all_frames)
2322 struct window *w = XWINDOW (window);
2323 struct frame *f = XFRAME (w->frame);
2324 bool candidate_p = 1;
2326 if (!BUFFERP (w->contents))
2327 candidate_p = 0;
2328 else if (MINI_WINDOW_P (w)
2329 && (EQ (minibuf, Qlambda)
2330 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2332 /* If MINIBUF is `lambda' don't consider any mini-windows.
2333 If it is a window, consider only that one. */
2334 candidate_p = 0;
2336 else if (EQ (all_frames, Qt))
2337 candidate_p = 1;
2338 else if (NILP (all_frames))
2340 eassert (WINDOWP (owindow));
2341 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2343 else if (EQ (all_frames, Qvisible))
2345 candidate_p = FRAME_VISIBLE_P (f)
2346 && (FRAME_TERMINAL (XFRAME (w->frame))
2347 == FRAME_TERMINAL (XFRAME (selected_frame)));
2350 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2352 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2353 #ifdef HAVE_X_WINDOWS
2354 /* Yuck!! If we've just created the frame and the
2355 window-manager requested the user to place it
2356 manually, the window may still not be considered
2357 `visible'. I'd argue it should be at least
2358 something like `iconified', but don't know how to do
2359 that yet. --Stef */
2360 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2361 && !f->output_data.x->has_been_visible)
2362 #endif
2364 && (FRAME_TERMINAL (XFRAME (w->frame))
2365 == FRAME_TERMINAL (XFRAME (selected_frame)));
2367 else if (WINDOWP (all_frames))
2368 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2369 || EQ (XWINDOW (all_frames)->frame, w->frame)
2370 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2371 else if (FRAMEP (all_frames))
2372 candidate_p = EQ (all_frames, w->frame);
2374 return candidate_p;
2378 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2379 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2380 MINIBUF, and ALL_FRAMES. */
2382 static void
2383 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2385 struct window *w = decode_live_window (*window);
2387 XSETWINDOW (*window, w);
2388 /* MINIBUF nil may or may not include minibuffers. Decide if it
2389 does. */
2390 if (NILP (*minibuf))
2391 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2392 else if (!EQ (*minibuf, Qt))
2393 *minibuf = Qlambda;
2395 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2396 => count none of them, or a specific minibuffer window (the
2397 active one) to count. */
2399 /* ALL_FRAMES nil doesn't specify which frames to include. */
2400 if (NILP (*all_frames))
2401 *all_frames
2402 = (!EQ (*minibuf, Qlambda)
2403 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2404 : Qnil);
2405 else if (EQ (*all_frames, Qvisible))
2407 else if (EQ (*all_frames, make_number (0)))
2409 else if (FRAMEP (*all_frames))
2411 else if (!EQ (*all_frames, Qt))
2412 *all_frames = Qnil;
2416 /* Return the next or previous window of WINDOW in cyclic ordering
2417 of windows. NEXT_P non-zero means return the next window. See the
2418 documentation string of next-window for the meaning of MINIBUF and
2419 ALL_FRAMES. */
2421 static Lisp_Object
2422 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2424 decode_next_window_args (&window, &minibuf, &all_frames);
2426 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2427 return the first window on the frame. */
2428 if (FRAMEP (all_frames)
2429 && !EQ (all_frames, XWINDOW (window)->frame))
2430 return Fframe_first_window (all_frames);
2432 if (next_p)
2434 Lisp_Object list;
2436 /* Find WINDOW in the list of all windows. */
2437 list = Fmemq (window, window_list ());
2439 /* Scan forward from WINDOW to the end of the window list. */
2440 if (CONSP (list))
2441 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2442 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2443 break;
2445 /* Scan from the start of the window list up to WINDOW. */
2446 if (!CONSP (list))
2447 for (list = Vwindow_list;
2448 CONSP (list) && !EQ (XCAR (list), window);
2449 list = XCDR (list))
2450 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2451 break;
2453 if (CONSP (list))
2454 window = XCAR (list);
2456 else
2458 Lisp_Object candidate, list;
2460 /* Scan through the list of windows for candidates. If there are
2461 candidate windows in front of WINDOW, the last one of these
2462 is the one we want. If there are candidates following WINDOW
2463 in the list, again the last one of these is the one we want. */
2464 candidate = Qnil;
2465 for (list = window_list (); CONSP (list); list = XCDR (list))
2467 if (EQ (XCAR (list), window))
2469 if (WINDOWP (candidate))
2470 break;
2472 else if (candidate_window_p (XCAR (list), window, minibuf,
2473 all_frames))
2474 candidate = XCAR (list);
2477 if (WINDOWP (candidate))
2478 window = candidate;
2481 return window;
2485 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2486 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2487 WINDOW must be a live window and defaults to the selected one. The
2488 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2489 consider.
2491 MINIBUF nil or omitted means consider the minibuffer window only if the
2492 minibuffer is active. MINIBUF t means consider the minibuffer window
2493 even if the minibuffer is not active. Any other value means do not
2494 consider the minibuffer window even if the minibuffer is active.
2496 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2497 plus the minibuffer window if specified by the MINIBUF argument. If the
2498 minibuffer counts, consider all windows on all frames that share that
2499 minibuffer too. The following non-nil values of ALL-FRAMES have special
2500 meanings:
2502 - t means consider all windows on all existing frames.
2504 - `visible' means consider all windows on all visible frames.
2506 - 0 (the number zero) means consider all windows on all visible and
2507 iconified frames.
2509 - A frame means consider all windows on that frame only.
2511 Anything else means consider all windows on WINDOW's frame and no
2512 others.
2514 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2515 `next-window' to iterate through the entire cycle of acceptable
2516 windows, eventually ending up back at the window you started with.
2517 `previous-window' traverses the same cycle, in the reverse order. */)
2518 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2520 return next_window (window, minibuf, all_frames, 1);
2524 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2525 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2526 WINDOW must be a live window and defaults to the selected one. The
2527 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2528 consider.
2530 MINIBUF nil or omitted means consider the minibuffer window only if the
2531 minibuffer is active. MINIBUF t means consider the minibuffer window
2532 even if the minibuffer is not active. Any other value means do not
2533 consider the minibuffer window even if the minibuffer is active.
2535 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2536 plus the minibuffer window if specified by the MINIBUF argument. If the
2537 minibuffer counts, consider all windows on all frames that share that
2538 minibuffer too. The following non-nil values of ALL-FRAMES have special
2539 meanings:
2541 - t means consider all windows on all existing frames.
2543 - `visible' means consider all windows on all visible frames.
2545 - 0 (the number zero) means consider all windows on all visible and
2546 iconified frames.
2548 - A frame means consider all windows on that frame only.
2550 Anything else means consider all windows on WINDOW's frame and no
2551 others.
2553 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2554 use `previous-window' to iterate through the entire cycle of
2555 acceptable windows, eventually ending up back at the window you
2556 started with. `next-window' traverses the same cycle, in the
2557 reverse order. */)
2558 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2560 return next_window (window, minibuf, all_frames, 0);
2564 /* Return a list of windows in cyclic ordering. Arguments are like
2565 for `next-window'. */
2567 static Lisp_Object
2568 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2570 Lisp_Object tail, list, rest;
2572 decode_next_window_args (&window, &minibuf, &all_frames);
2573 list = Qnil;
2575 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2576 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2577 list = Fcons (XCAR (tail), list);
2579 /* Rotate the list to start with WINDOW. */
2580 list = Fnreverse (list);
2581 rest = Fmemq (window, list);
2582 if (!NILP (rest) && !EQ (rest, list))
2584 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2586 XSETCDR (tail, Qnil);
2587 list = nconc2 (rest, list);
2589 return list;
2593 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2594 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2595 FRAME nil or omitted means use the selected frame.
2596 WINDOW nil or omitted means use the window selected within FRAME.
2597 MINIBUF t means include the minibuffer window, even if it isn't active.
2598 MINIBUF nil or omitted means include the minibuffer window only
2599 if it's active.
2600 MINIBUF neither nil nor t means never include the minibuffer window. */)
2601 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2603 if (NILP (window))
2604 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2605 CHECK_WINDOW (window);
2606 if (NILP (frame))
2607 frame = selected_frame;
2609 if (!EQ (frame, XWINDOW (window)->frame))
2610 error ("Window is on a different frame");
2612 return window_list_1 (window, minibuf, frame);
2616 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2617 doc: /* Return a list of all live windows.
2618 WINDOW specifies the first window to list and defaults to the selected
2619 window.
2621 Optional argument MINIBUF nil or omitted means consider the minibuffer
2622 window only if the minibuffer is active. MINIBUF t means consider the
2623 minibuffer window even if the minibuffer is not active. Any other value
2624 means do not consider the minibuffer window even if the minibuffer is
2625 active.
2627 Optional argument ALL-FRAMES nil or omitted means consider all windows
2628 on WINDOW's frame, plus the minibuffer window if specified by the
2629 MINIBUF argument. If the minibuffer counts, consider all windows on all
2630 frames that share that minibuffer too. The following non-nil values of
2631 ALL-FRAMES have special meanings:
2633 - t means consider all windows on all existing frames.
2635 - `visible' means consider all windows on all visible frames.
2637 - 0 (the number zero) means consider all windows on all visible and
2638 iconified frames.
2640 - A frame means consider all windows on that frame only.
2642 Anything else means consider all windows on WINDOW's frame and no
2643 others.
2645 If WINDOW is not on the list of windows returned, some other window will
2646 be listed first but no error is signaled. */)
2647 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2649 return window_list_1 (window, minibuf, all_frames);
2652 /* Look at all windows, performing an operation specified by TYPE
2653 with argument OBJ.
2654 If FRAMES is Qt, look at all frames;
2655 Qnil, look at just the selected frame;
2656 Qvisible, look at visible frames;
2657 a frame, just look at windows on that frame.
2658 If MINI is non-zero, perform the operation on minibuffer windows too. */
2660 enum window_loop
2662 WINDOW_LOOP_UNUSED,
2663 GET_BUFFER_WINDOW, /* Arg is buffer */
2664 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2665 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2666 CHECK_ALL_WINDOWS /* Arg is ignored */
2669 static Lisp_Object
2670 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2672 Lisp_Object window, windows, best_window, frame_arg;
2673 int frame_best_window_flag = 0;
2674 struct frame *f;
2675 struct gcpro gcpro1;
2677 /* If we're only looping through windows on a particular frame,
2678 frame points to that frame. If we're looping through windows
2679 on all frames, frame is 0. */
2680 if (FRAMEP (frames))
2681 f = XFRAME (frames);
2682 else if (NILP (frames))
2683 f = SELECTED_FRAME ();
2684 else
2685 f = NULL;
2687 if (f)
2688 frame_arg = Qlambda;
2689 else if (EQ (frames, make_number (0)))
2690 frame_arg = frames;
2691 else if (EQ (frames, Qvisible))
2692 frame_arg = frames;
2693 else
2694 frame_arg = Qt;
2696 /* frame_arg is Qlambda to stick to one frame,
2697 Qvisible to consider all visible frames,
2698 or Qt otherwise. */
2700 /* Pick a window to start with. */
2701 if (WINDOWP (obj))
2702 window = obj;
2703 else if (f)
2704 window = FRAME_SELECTED_WINDOW (f);
2705 else
2706 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2708 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2709 GCPRO1 (windows);
2710 best_window = Qnil;
2712 for (; CONSP (windows); windows = XCDR (windows))
2714 struct window *w;
2716 window = XCAR (windows);
2717 w = XWINDOW (window);
2719 /* Note that we do not pay attention here to whether the frame
2720 is visible, since Fwindow_list skips non-visible frames if
2721 that is desired, under the control of frame_arg. */
2722 if (!MINI_WINDOW_P (w)
2723 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2724 consider all windows. */
2725 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2726 || (mini && minibuf_level > 0))
2727 switch (type)
2729 case GET_BUFFER_WINDOW:
2730 if (EQ (w->contents, obj)
2731 /* Don't find any minibuffer window except the one that
2732 is currently in use. */
2733 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2735 if (EQ (window, selected_window))
2736 /* Preferably return the selected window. */
2737 RETURN_UNGCPRO (window);
2738 else if (EQ (XWINDOW (window)->frame, selected_frame)
2739 && !frame_best_window_flag)
2740 /* Prefer windows on the current frame (but don't
2741 choose another one if we have one already). */
2743 best_window = window;
2744 frame_best_window_flag = 1;
2746 else if (NILP (best_window))
2747 best_window = window;
2749 break;
2751 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2752 /* We could simply check whether the buffer shown by window
2753 is live, and show another buffer in case it isn't. */
2754 if (EQ (w->contents, obj))
2756 /* Undedicate WINDOW. */
2757 wset_dedicated (w, Qnil);
2758 /* Make WINDOW show the buffer returned by
2759 other_buffer_safely, don't run any hooks. */
2760 set_window_buffer
2761 (window, other_buffer_safely (w->contents), 0, 0);
2762 /* If WINDOW is the selected window, make its buffer
2763 current. But do so only if the window shows the
2764 current buffer (Bug#6454). */
2765 if (EQ (window, selected_window)
2766 && XBUFFER (w->contents) == current_buffer)
2767 Fset_buffer (w->contents);
2769 break;
2771 case REDISPLAY_BUFFER_WINDOWS:
2772 if (EQ (w->contents, obj))
2774 mark_window_display_accurate (window, 0);
2775 w->update_mode_line = 1;
2776 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2777 update_mode_lines = 27;
2778 best_window = window;
2780 break;
2782 /* Check for a leaf window that has a killed buffer
2783 or broken markers. */
2784 case CHECK_ALL_WINDOWS:
2785 if (BUFFERP (w->contents))
2787 struct buffer *b = XBUFFER (w->contents);
2789 if (!BUFFER_LIVE_P (b))
2790 emacs_abort ();
2791 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2792 emacs_abort ();
2793 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2794 emacs_abort ();
2796 break;
2798 case WINDOW_LOOP_UNUSED:
2799 break;
2803 UNGCPRO;
2804 return best_window;
2807 /* Used for debugging. Abort if any window has a dead buffer. */
2809 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2810 void
2811 check_all_windows (void)
2813 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2816 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2817 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2818 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2819 the current buffer.
2821 The optional argument ALL-FRAMES specifies the frames to consider:
2823 - t means consider all windows on all existing frames.
2825 - `visible' means consider all windows on all visible frames.
2827 - 0 (the number zero) means consider all windows on all visible
2828 and iconified frames.
2830 - A frame means consider all windows on that frame only.
2832 Any other value of ALL-FRAMES means consider all windows on the
2833 selected frame and no others. */)
2834 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2836 Lisp_Object buffer;
2838 if (NILP (buffer_or_name))
2839 buffer = Fcurrent_buffer ();
2840 else
2841 buffer = Fget_buffer (buffer_or_name);
2843 if (BUFFERP (buffer))
2844 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2845 else
2846 return Qnil;
2849 static Lisp_Object
2850 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2852 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2856 static Lisp_Object
2857 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2859 return call2(Qwindow_pixel_to_total, frame, horizontal);
2863 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2864 Sdelete_other_windows_internal, 0, 2, "",
2865 doc: /* Make WINDOW fill its frame.
2866 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2867 and defaults to the selected one.
2869 Optional argument ROOT, if non-nil, must specify an internal window such
2870 that WINDOW is in its window subtree. If this is the case, replace ROOT
2871 by WINDOW and leave alone any windows not part of ROOT's subtree.
2873 When WINDOW is live try to reduce display jumps by keeping the text
2874 previously visible in WINDOW in the same place on the frame. Doing this
2875 depends on the value of (window-start WINDOW), so if calling this
2876 function in a program gives strange scrolling, make sure the
2877 window-start value is reasonable when this function is called. */)
2878 (Lisp_Object window, Lisp_Object root)
2880 struct window *w, *r, *s;
2881 struct frame *f;
2882 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2883 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2884 int top IF_LINT (= 0), new_top, resize_failed;
2886 w = decode_valid_window (window);
2887 XSETWINDOW (window, w);
2888 f = XFRAME (w->frame);
2890 if (NILP (root))
2891 /* ROOT is the frame's root window. */
2893 root = FRAME_ROOT_WINDOW (f);
2894 r = XWINDOW (root);
2896 else
2897 /* ROOT must be an ancestor of WINDOW. */
2899 r = decode_valid_window (root);
2900 pwindow = XWINDOW (window)->parent;
2901 while (!NILP (pwindow))
2902 if (EQ (pwindow, root))
2903 break;
2904 else
2905 pwindow = XWINDOW (pwindow)->parent;
2906 if (!EQ (pwindow, root))
2907 error ("Specified root is not an ancestor of specified window");
2910 if (EQ (window, root))
2911 /* A noop. */
2912 return Qnil;
2913 /* I don't understand the "top > 0" part below. If we deal with a
2914 standalone minibuffer it would have been caught by the preceding
2915 test. */
2916 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2917 error ("Can't expand minibuffer to full frame");
2919 if (BUFFERP (w->contents))
2921 startpos = marker_position (w->start);
2922 startbyte = marker_byte_position (w->start);
2923 top = (WINDOW_TOP_EDGE_LINE (w)
2924 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2925 /* Make sure WINDOW is the frame's selected window. */
2926 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2928 if (EQ (selected_frame, w->frame))
2929 Fselect_window (window, Qnil);
2930 else
2931 fset_selected_window (f, window);
2934 else
2936 /* See if the frame's selected window is a part of the window
2937 subtree rooted at WINDOW, by finding all the selected window's
2938 parents and comparing each one with WINDOW. If it isn't we
2939 need a new selected window for this frame. */
2940 swindow = FRAME_SELECTED_WINDOW (f);
2941 while (1)
2943 pwindow = swindow;
2944 while (!NILP (pwindow) && !EQ (window, pwindow))
2945 pwindow = XWINDOW (pwindow)->parent;
2947 if (EQ (window, pwindow))
2948 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2949 as the new selected window. */
2950 break;
2951 else
2952 /* Else try the previous window of SWINDOW. */
2953 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2956 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2958 if (EQ (selected_frame, w->frame))
2959 Fselect_window (swindow, Qnil);
2960 else
2961 fset_selected_window (f, swindow);
2965 block_input ();
2966 if (!FRAME_INITIAL_P (f))
2968 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2970 /* We are going to free the glyph matrices of WINDOW, and with
2971 that we might lose any information about glyph rows that have
2972 some of their glyphs highlighted in mouse face. (These rows
2973 are marked with a non-zero mouse_face_p flag.) If WINDOW
2974 indeed has some glyphs highlighted in mouse face, signal to
2975 frame's up-to-date hook that mouse highlight was overwritten,
2976 so that it will arrange for redisplaying the highlight. */
2977 if (EQ (hlinfo->mouse_face_window, window))
2978 reset_mouse_highlight (hlinfo);
2980 free_window_matrices (r);
2982 fset_redisplay (f);
2983 Vwindow_list = Qnil;
2984 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2985 resize_failed = 0;
2987 if (!WINDOW_LEAF_P (w))
2989 /* Resize child windows vertically. */
2990 XSETINT (delta, r->pixel_height - w->pixel_height);
2991 w->pixel_top = r->pixel_top;
2992 w->top_line = r->top_line;
2993 resize_root_window (window, delta, Qnil, Qnil, Qt);
2994 if (window_resize_check (w, 0))
2996 window_resize_apply (w, 0);
2997 window_pixel_to_total (w->frame, Qnil);
2999 else
3001 resize_root_window (window, delta, Qnil, Qt, Qt);
3002 if (window_resize_check (w, 0))
3004 window_resize_apply (w, 0);
3005 window_pixel_to_total (w->frame, Qnil);
3007 else
3008 resize_failed = 1;
3011 /* Resize child windows horizontally. */
3012 if (!resize_failed)
3014 w->left_col = r->left_col;
3015 w->pixel_left = r->pixel_left;
3016 XSETINT (delta, r->pixel_width - w->pixel_width);
3017 resize_root_window (window, delta, Qt, Qnil, Qt);
3018 if (window_resize_check (w, 1))
3020 window_resize_apply (w, 1);
3021 window_pixel_to_total (w->frame, Qt);
3023 else
3025 resize_root_window (window, delta, Qt, Qt, Qt);
3026 if (window_resize_check (w, 1))
3028 window_resize_apply (w, 1);
3029 window_pixel_to_total (w->frame, Qt);
3031 else
3032 resize_failed = 1;
3036 if (resize_failed)
3037 /* Play safe, if we still can ... */
3039 window = swindow;
3040 w = XWINDOW (window);
3044 /* Cleanly unlink WINDOW from window-tree. */
3045 if (!NILP (w->prev))
3046 /* Get SIBLING above (on the left of) WINDOW. */
3048 sibling = w->prev;
3049 s = XWINDOW (sibling);
3050 wset_next (s, w->next);
3051 if (!NILP (s->next))
3052 wset_prev (XWINDOW (s->next), sibling);
3054 else
3055 /* Get SIBLING below (on the right of) WINDOW. */
3057 sibling = w->next;
3058 s = XWINDOW (sibling);
3059 wset_prev (s, Qnil);
3060 wset_combination (XWINDOW (w->parent),
3061 XWINDOW (w->parent)->horizontal, sibling);
3064 /* Delete ROOT and all child windows of ROOT. */
3065 if (WINDOWP (r->contents))
3067 delete_all_child_windows (r->contents);
3068 wset_combination (r, 0, Qnil);
3071 replace_window (root, window, 1);
3073 /* This must become SWINDOW anyway ....... */
3074 if (BUFFERP (w->contents) && !resize_failed)
3076 /* Try to minimize scrolling, by setting the window start to the
3077 point will cause the text at the old window start to be at the
3078 same place on the frame. But don't try to do this if the
3079 window start is outside the visible portion (as might happen
3080 when the display is not current, due to typeahead). */
3081 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3082 if (new_top != top
3083 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3084 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3086 struct position pos;
3087 struct buffer *obuf = current_buffer;
3089 Fset_buffer (w->contents);
3090 /* This computation used to temporarily move point, but that
3091 can have unwanted side effects due to text properties. */
3092 pos = *vmotion (startpos, startbyte, -top, w);
3094 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3095 w->window_end_valid = 0;
3096 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3097 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3098 /* We need to do this, so that the window-scroll-functions
3099 get called. */
3100 w->optional_new_start = 1;
3102 set_buffer_internal (obuf);
3106 adjust_frame_glyphs (f);
3107 unblock_input ();
3109 run_window_configuration_change_hook (f);
3111 return Qnil;
3115 void
3116 replace_buffer_in_windows (Lisp_Object buffer)
3118 call1 (Qreplace_buffer_in_windows, buffer);
3121 /* If BUFFER is shown in a window, safely replace it with some other
3122 buffer in all windows of all frames, even those on other keyboards. */
3124 void
3125 replace_buffer_in_windows_safely (Lisp_Object buffer)
3127 if (buffer_window_count (XBUFFER (buffer)))
3129 Lisp_Object tail, frame;
3131 /* A single call to window_loop won't do the job because it only
3132 considers frames on the current keyboard. So loop manually over
3133 frames, and handle each one. */
3134 FOR_EACH_FRAME (tail, frame)
3135 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3139 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3140 minimum allowable size. PIXELWISE means interpret these as pixel
3141 sizes. */
3143 void
3144 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3146 /* For height, we have to see:
3147 how many windows the frame has at minimum (one or two),
3148 and whether it has a menu bar or other special stuff at the top. */
3149 if (pixelwise)
3151 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3152 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3154 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3155 min_height = 2 * min_height;
3157 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3159 if (*height < min_height)
3160 *height = min_height;
3161 if (*width < min_width)
3162 *width = min_width;
3164 else
3166 int min_height
3167 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3168 ? MIN_SAFE_WINDOW_HEIGHT
3169 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3171 if (FRAME_TOP_MARGIN (frame) > 0)
3172 min_height += FRAME_TOP_MARGIN (frame);
3174 if (*height < min_height)
3175 *height = min_height;
3176 if (*width < MIN_SAFE_WINDOW_WIDTH)
3177 *width = MIN_SAFE_WINDOW_WIDTH;
3181 /* Adjust the margins of window W if text area is too small.
3182 Return 1 if window width is ok after adjustment; 0 if window
3183 is still too narrow. */
3185 static int
3186 adjust_window_margins (struct window *w)
3188 int box_width = (WINDOW_PIXEL_WIDTH (w)
3189 - WINDOW_FRINGES_WIDTH (w)
3190 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3191 int margin_width = WINDOW_MARGINS_WIDTH (w);
3193 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3194 return 1;
3196 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3197 return 0;
3198 else
3199 /* Window's text area is too narrow, but reducing the window
3200 margins will fix that. */
3202 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3204 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3206 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3208 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3209 w->left_margin_cols = w->right_margin_cols =
3210 margin_width / (2 * unit);
3211 else
3212 w->right_margin_cols = margin_width / unit;
3214 else
3215 w->left_margin_cols = margin_width / unit;
3217 return 1;
3221 /* The following three routines are needed for running a window's
3222 configuration change hook. */
3223 static void
3224 run_funs (Lisp_Object funs)
3226 for (; CONSP (funs); funs = XCDR (funs))
3227 if (!EQ (XCAR (funs), Qt))
3228 call0 (XCAR (funs));
3231 static void
3232 select_window_norecord (Lisp_Object window)
3234 if (WINDOW_LIVE_P (window))
3235 Fselect_window (window, Qt);
3238 static void
3239 select_frame_norecord (Lisp_Object frame)
3241 if (FRAME_LIVE_P (XFRAME (frame)))
3242 Fselect_frame (frame, Qt);
3245 void
3246 run_window_configuration_change_hook (struct frame *f)
3248 ptrdiff_t count = SPECPDL_INDEX ();
3249 Lisp_Object frame, global_wcch
3250 = Fdefault_value (Qwindow_configuration_change_hook);
3251 XSETFRAME (frame, f);
3253 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3254 return;
3256 /* Use the right buffer. Matters when running the local hooks. */
3257 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3259 record_unwind_current_buffer ();
3260 Fset_buffer (Fwindow_buffer (Qnil));
3263 if (SELECTED_FRAME () != f)
3265 record_unwind_protect (select_frame_norecord, selected_frame);
3266 select_frame_norecord (frame);
3269 /* Look for buffer-local values. */
3271 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3272 for (; CONSP (windows); windows = XCDR (windows))
3274 Lisp_Object window = XCAR (windows);
3275 Lisp_Object buffer = Fwindow_buffer (window);
3276 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3277 buffer)))
3279 ptrdiff_t inner_count = SPECPDL_INDEX ();
3280 record_unwind_protect (select_window_norecord, selected_window);
3281 select_window_norecord (window);
3282 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3283 buffer));
3284 unbind_to (inner_count, Qnil);
3289 run_funs (global_wcch);
3290 unbind_to (count, Qnil);
3293 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3294 Srun_window_configuration_change_hook, 0, 1, 0,
3295 doc: /* Run `window-configuration-change-hook' for FRAME.
3296 If FRAME is omitted or nil, it defaults to the selected frame. */)
3297 (Lisp_Object frame)
3299 run_window_configuration_change_hook (decode_live_frame (frame));
3300 return Qnil;
3303 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3304 Srun_window_scroll_functions, 0, 1, 0,
3305 doc: /* Run `window-scroll-functions' for WINDOW.
3306 If WINDOW is omitted or nil, it defaults to the selected window. */)
3307 (Lisp_Object window)
3309 if (! NILP (Vwindow_scroll_functions))
3310 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3311 Fmarker_position (decode_live_window (window)->start));
3312 return Qnil;
3315 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3316 to run hooks. See make_frame for a case where it's not allowed.
3317 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3318 scroll-bar settings of the window are not reset from the buffer's
3319 local settings. */
3321 void
3322 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3323 bool run_hooks_p, bool keep_margins_p)
3325 struct window *w = XWINDOW (window);
3326 struct buffer *b = XBUFFER (buffer);
3327 ptrdiff_t count = SPECPDL_INDEX ();
3328 bool samebuf = EQ (buffer, w->contents);
3330 wset_buffer (w, buffer);
3332 if (EQ (window, selected_window))
3333 bset_last_selected_window (b, window);
3335 /* Let redisplay errors through. */
3336 b->display_error_modiff = 0;
3338 /* Update time stamps of buffer display. */
3339 if (INTEGERP (BVAR (b, display_count)))
3340 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3341 bset_display_time (b, Fcurrent_time ());
3343 w->window_end_pos = 0;
3344 w->window_end_vpos = 0;
3345 w->last_cursor_vpos = 0;
3347 if (!(keep_margins_p && samebuf))
3348 { /* If we're not actually changing the buffer, don't reset hscroll and
3349 vscroll. This case happens for example when called from
3350 change_frame_size_1, where we use a dummy call to
3351 Fset_window_buffer on the frame's selected window (and no other)
3352 just in order to run window-configuration-change-hook.
3353 Resetting hscroll and vscroll here is problematic for things like
3354 image-mode and doc-view-mode since it resets the image's position
3355 whenever we resize the frame. */
3356 w->hscroll = w->min_hscroll = 0;
3357 w->vscroll = 0;
3358 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3359 set_marker_restricted (w->start,
3360 make_number (b->last_window_start),
3361 buffer);
3362 w->start_at_line_beg = 0;
3363 w->force_start = 0;
3365 /* Maybe we could move this into the `if' but it's not obviously safe and
3366 I doubt it's worth the trouble. */
3367 wset_redisplay (w);
3368 w->update_mode_line = true;
3370 /* We must select BUFFER for running the window-scroll-functions. */
3371 /* We can't check ! NILP (Vwindow_scroll_functions) here
3372 because that might itself be a local variable. */
3373 if (window_initialized)
3375 record_unwind_current_buffer ();
3376 Fset_buffer (buffer);
3379 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3381 if (!keep_margins_p)
3383 /* Set left and right marginal area width etc. from buffer. */
3384 set_window_fringes (w, BVAR (b, left_fringe_width),
3385 BVAR (b, right_fringe_width),
3386 BVAR (b, fringes_outside_margins));
3387 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3388 BVAR (b, vertical_scroll_bar_type), Qnil);
3389 set_window_margins (w, BVAR (b, left_margin_cols),
3390 BVAR (b, right_margin_cols));
3391 apply_window_adjustment (w);
3394 if (run_hooks_p)
3396 if (! NILP (Vwindow_scroll_functions))
3397 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3398 Fmarker_position (w->start));
3399 if (!samebuf)
3400 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3403 unbind_to (count, Qnil);
3406 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3407 doc: /* Make WINDOW display BUFFER-OR-NAME.
3408 WINDOW must be a live window and defaults to the selected one.
3409 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3411 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3412 display margins, fringe widths, and scroll bar settings are preserved;
3413 the default is to reset these from the local settings for BUFFER-OR-NAME
3414 or the frame defaults. Return nil.
3416 This function throws an error when WINDOW is strongly dedicated to its
3417 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3418 already display BUFFER-OR-NAME.
3420 This function runs `window-scroll-functions' before running
3421 `window-configuration-change-hook'. */)
3422 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3424 register Lisp_Object tem, buffer;
3425 register struct window *w = decode_live_window (window);
3427 XSETWINDOW (window, w);
3428 buffer = Fget_buffer (buffer_or_name);
3429 CHECK_BUFFER (buffer);
3430 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3431 error ("Attempt to display deleted buffer");
3433 tem = w->contents;
3434 if (NILP (tem))
3435 error ("Window is deleted");
3436 else
3438 if (!EQ (tem, buffer))
3440 if (EQ (w->dedicated, Qt))
3441 /* WINDOW is strongly dedicated to its buffer, signal an
3442 error. */
3443 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3444 else
3445 /* WINDOW is weakly dedicated to its buffer, reset
3446 dedication. */
3447 wset_dedicated (w, Qnil);
3449 call1 (Qrecord_window_buffer, window);
3452 unshow_buffer (w);
3455 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3457 return Qnil;
3460 static Lisp_Object
3461 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3463 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3466 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3467 0, 1, 0,
3468 doc: /* Force all windows to be updated on next redisplay.
3469 If optional arg OBJECT is a window, force redisplay of that window only.
3470 If OBJECT is a buffer or buffer name, force redisplay of all windows
3471 displaying that buffer. */)
3472 (Lisp_Object object)
3474 if (NILP (object))
3476 windows_or_buffers_changed = 29;
3477 update_mode_lines = 28;
3478 return Qt;
3481 if (WINDOWP (object))
3483 struct window *w = XWINDOW (object);
3484 mark_window_display_accurate (object, 0);
3485 w->update_mode_line = 1;
3486 if (BUFFERP (w->contents))
3487 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3488 update_mode_lines = 29;
3489 return Qt;
3492 if (STRINGP (object))
3493 object = Fget_buffer (object);
3494 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3495 && buffer_window_count (XBUFFER (object)))
3497 /* If buffer is live and shown in at least one window, find
3498 all windows showing this buffer and force update of them. */
3499 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3500 return NILP (object) ? Qnil : Qt;
3503 /* If nothing suitable was found, just return.
3504 We could signal an error, but this feature will typically be used
3505 asynchronously in timers or process sentinels, so we don't. */
3506 return Qnil;
3509 /* Obsolete since 24.3. */
3510 void
3511 temp_output_buffer_show (register Lisp_Object buf)
3513 register struct buffer *old = current_buffer;
3514 register Lisp_Object window;
3515 register struct window *w;
3517 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3519 Fset_buffer (buf);
3520 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3521 BEGV = BEG;
3522 ZV = Z;
3523 SET_PT (BEG);
3524 set_buffer_internal (old);
3526 if (!NILP (Vtemp_buffer_show_function))
3527 call1 (Vtemp_buffer_show_function, buf);
3528 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3530 if (!EQ (XWINDOW (window)->frame, selected_frame))
3531 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3532 Vminibuf_scroll_window = window;
3533 w = XWINDOW (window);
3534 w->hscroll = 0;
3535 w->min_hscroll = 0;
3536 set_marker_restricted_both (w->start, buf, BEG, BEG);
3537 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3539 /* Run temp-buffer-show-hook, with the chosen window selected
3540 and its buffer current. */
3542 ptrdiff_t count = SPECPDL_INDEX ();
3543 Lisp_Object prev_window, prev_buffer;
3544 prev_window = selected_window;
3545 XSETBUFFER (prev_buffer, old);
3547 /* Select the window that was chosen, for running the hook.
3548 Note: Both Fselect_window and select_window_norecord may
3549 set-buffer to the buffer displayed in the window,
3550 so we need to save the current buffer. --stef */
3551 record_unwind_protect (restore_buffer, prev_buffer);
3552 record_unwind_protect (select_window_norecord, prev_window);
3553 Fselect_window (window, Qt);
3554 Fset_buffer (w->contents);
3555 Frun_hooks (1, &Qtemp_buffer_show_hook);
3556 unbind_to (count, Qnil);
3561 /* Make new window, have it replace WINDOW in window-tree, and make
3562 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3563 horizontal child). */
3564 static void
3565 make_parent_window (Lisp_Object window, bool horflag)
3567 Lisp_Object parent;
3568 register struct window *o, *p;
3570 o = XWINDOW (window);
3571 p = allocate_window ();
3572 memcpy ((char *) p + sizeof (struct vectorlike_header),
3573 (char *) o + sizeof (struct vectorlike_header),
3574 word_size * VECSIZE (struct window));
3575 /* P's buffer slot may change from nil to a buffer... */
3576 adjust_window_count (p, 1);
3577 XSETWINDOW (parent, p);
3579 p->sequence_number = ++sequence_number;
3581 replace_window (window, parent, 1);
3583 wset_next (o, Qnil);
3584 wset_prev (o, Qnil);
3585 wset_parent (o, parent);
3586 /* ...but now P becomes an internal window. */
3587 wset_start (p, Qnil);
3588 wset_pointm (p, Qnil);
3589 wset_buffer (p, Qnil);
3590 wset_combination (p, horflag, window);
3591 wset_combination_limit (p, Qnil);
3592 wset_window_parameters (p, Qnil);
3595 /* Make new window from scratch. */
3596 Lisp_Object
3597 make_window (void)
3599 Lisp_Object window;
3600 register struct window *w;
3602 w = allocate_window ();
3603 /* Initialize Lisp data. Note that allocate_window initializes all
3604 Lisp data to nil, so do it only for slots which should not be nil. */
3605 wset_normal_lines (w, make_float (1.0));
3606 wset_normal_cols (w, make_float (1.0));
3607 wset_new_total (w, make_number (0));
3608 wset_new_normal (w, make_number (0));
3609 wset_new_pixel (w, make_number (0));
3610 wset_start (w, Fmake_marker ());
3611 wset_pointm (w, Fmake_marker ());
3612 wset_vertical_scroll_bar_type (w, Qt);
3613 /* These Lisp fields are marked specially so they're not set to nil by
3614 allocate_window. */
3615 wset_prev_buffers (w, Qnil);
3616 wset_next_buffers (w, Qnil);
3618 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3619 non-Lisp data, so do it only for slots which should not be zero. */
3620 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3621 w->left_fringe_width = w->right_fringe_width = -1;
3622 w->mode_line_height = w->header_line_height = -1;
3623 #ifdef HAVE_WINDOW_SYSTEM
3624 w->phys_cursor_type = NO_CURSOR;
3625 w->phys_cursor_width = -1;
3626 #endif
3627 w->sequence_number = ++sequence_number;
3628 w->scroll_bar_width = -1;
3629 w->column_number_displayed = -1;
3631 /* Reset window_list. */
3632 Vwindow_list = Qnil;
3633 /* Return window. */
3634 XSETWINDOW (window, w);
3635 return window;
3638 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3639 doc: /* Set new pixel size of WINDOW to SIZE.
3640 WINDOW must be a valid window and defaults to the selected one.
3641 Return SIZE.
3643 Optional argument ADD non-nil means add SIZE to the new pixel size of
3644 WINDOW and return the sum.
3646 Note: This function does not operate on any child windows of WINDOW. */)
3647 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3649 struct window *w = decode_valid_window (window);
3650 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3651 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3653 CHECK_RANGED_INTEGER (size, size_min, size_max);
3654 if (NILP (add))
3655 wset_new_pixel (w, size);
3656 else
3657 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3659 return w->new_pixel;
3662 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3663 doc: /* Set new total size of WINDOW to SIZE.
3664 WINDOW must be a valid window and defaults to the selected one.
3665 Return SIZE.
3667 Optional argument ADD non-nil means add SIZE to the new total size of
3668 WINDOW and return the sum.
3670 Note: This function does not operate on any child windows of WINDOW. */)
3671 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3673 struct window *w = decode_valid_window (window);
3675 CHECK_NUMBER (size);
3676 if (NILP (add))
3677 wset_new_total (w, size);
3678 else
3679 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3681 return w->new_total;
3684 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3685 doc: /* Set new normal size of WINDOW to SIZE.
3686 WINDOW must be a valid window and defaults to the selected one.
3687 Return SIZE.
3689 Note: This function does not operate on any child windows of WINDOW. */)
3690 (Lisp_Object window, Lisp_Object size)
3692 wset_new_normal (decode_valid_window (window), size);
3693 return size;
3696 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3697 non-zero) to w->new_pixel would result in correct heights (widths)
3698 for window W and recursively all child windows of W.
3700 Note: This function does not check any of `window-fixed-size-p',
3701 `window-min-height' or `window-min-width'. It does check that window
3702 sizes do not drop below one line (two columns). */
3703 static int
3704 window_resize_check (struct window *w, bool horflag)
3706 struct frame *f = XFRAME (w->frame);
3707 struct window *c;
3709 if (WINDOW_VERTICAL_COMBINATION_P (w))
3710 /* W is a vertical combination. */
3712 c = XWINDOW (w->contents);
3713 if (horflag)
3714 /* All child windows of W must have the same width as W. */
3716 while (c)
3718 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3719 || !window_resize_check (c, horflag))
3720 return 0;
3722 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3725 return 1;
3727 else
3728 /* The sum of the heights of the child windows of W must equal
3729 W's height. */
3731 int remaining_pixels = XINT (w->new_pixel);
3733 while (c)
3735 if (!window_resize_check (c, horflag))
3736 return 0;
3738 remaining_pixels -= XINT (c->new_pixel);
3739 if (remaining_pixels < 0)
3740 return 0;
3741 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3744 return remaining_pixels == 0;
3747 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3748 /* W is a horizontal combination. */
3750 c = XWINDOW (w->contents);
3751 if (horflag)
3752 /* The sum of the widths of the child windows of W must equal W's
3753 width. */
3755 int remaining_pixels = XINT (w->new_pixel);
3757 while (c)
3759 if (!window_resize_check (c, horflag))
3760 return 0;
3762 remaining_pixels -= XINT (c->new_pixel);
3763 if (remaining_pixels < 0)
3764 return 0;
3765 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3768 return remaining_pixels == 0;
3770 else
3771 /* All child windows of W must have the same height as W. */
3773 while (c)
3775 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3776 || !window_resize_check (c, horflag))
3777 return 0;
3779 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3782 return 1;
3785 else
3786 /* A leaf window. Make sure it's not too small. The following
3787 hardcodes the values of `window-safe-min-width' (2) and
3788 `window-safe-min-height' (1) which are defined in window.el. */
3789 return (XINT (w->new_pixel) >= (horflag
3790 ? (2 * FRAME_COLUMN_WIDTH (f))
3791 : FRAME_LINE_HEIGHT (f)));
3795 /* Set w->pixel_height (w->pixel_height if HORIZONTAL is non-zero) to
3796 w->new_pixel for window W and recursively all child windows of W.
3797 Also calculate and assign the new vertical (horizontal) pixel start
3798 positions of each of these windows.
3800 This function does not perform any error checks. Make sure you have
3801 run window_resize_check on W before applying this function. */
3802 static void
3803 window_resize_apply (struct window *w, bool horflag)
3805 struct window *c;
3806 int edge;
3807 int unit = (horflag
3808 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3809 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3811 /* Note: Assigning new_normal requires that the new total size of the
3812 parent window has been set *before*. */
3813 if (horflag)
3815 w->pixel_width = XFASTINT (w->new_pixel);
3816 w->total_cols = w->pixel_width / unit;
3817 if (NUMBERP (w->new_normal))
3818 wset_normal_cols (w, w->new_normal);
3820 edge = w->pixel_left;
3822 else
3824 w->pixel_height = XFASTINT (w->new_pixel);
3825 w->total_lines = w->pixel_height / unit;
3826 if (NUMBERP (w->new_normal))
3827 wset_normal_lines (w, w->new_normal);
3829 edge = w->pixel_top;
3832 if (WINDOW_VERTICAL_COMBINATION_P (w))
3833 /* W is a vertical combination. */
3835 c = XWINDOW (w->contents);
3836 while (c)
3838 if (horflag)
3840 c->pixel_left = edge;
3841 c->left_col = edge / unit;
3843 else
3845 c->pixel_top = edge;
3846 c->top_line = edge / unit;
3848 window_resize_apply (c, horflag);
3849 if (!horflag)
3850 edge = edge + c->pixel_height;
3852 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3855 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3856 /* W is a horizontal combination. */
3858 c = XWINDOW (w->contents);
3859 while (c)
3861 if (horflag)
3863 c->pixel_left = edge;
3864 c->left_col = edge / unit;
3866 else
3868 c->pixel_top = edge;
3869 c->top_line = edge / unit;
3872 window_resize_apply (c, horflag);
3873 if (horflag)
3874 edge = edge + c->pixel_width;
3876 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3879 else
3880 /* Bug#15957. */
3881 w->window_end_valid = 0;
3885 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3886 w->new_total for window W and recursively all child windows of W.
3887 Also calculate and assign the new vertical (horizontal) start
3888 positions of each of these windows. */
3889 static void
3890 window_resize_apply_total (struct window *w, bool horflag)
3892 struct window *c;
3893 int edge;
3895 /* Note: Assigning new_normal requires that the new total size of the
3896 parent window has been set *before*. */
3897 if (horflag)
3899 w->total_cols = XFASTINT (w->new_total);
3900 edge = w->left_col;
3902 else
3904 w->total_lines = XFASTINT (w->new_total);
3905 edge = w->top_line;
3908 if (WINDOW_VERTICAL_COMBINATION_P (w))
3909 /* W is a vertical combination. */
3911 c = XWINDOW (w->contents);
3912 while (c)
3914 if (horflag)
3915 c->left_col = edge;
3916 else
3917 c->top_line = edge;
3919 window_resize_apply_total (c, horflag);
3920 if (!horflag)
3921 edge = edge + c->total_lines;
3923 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3926 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3927 /* W is a horizontal combination. */
3929 c = XWINDOW (w->contents);
3930 while (c)
3932 if (horflag)
3933 c->left_col = edge;
3934 else
3935 c->top_line = edge;
3937 window_resize_apply_total (c, horflag);
3938 if (horflag)
3939 edge = edge + c->total_cols;
3941 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3946 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3947 doc: /* Apply requested size values for window-tree of FRAME.
3948 If FRAME is omitted or nil, it defaults to the selected frame.
3950 Optional argument HORIZONTAL omitted or nil means apply requested
3951 height values. HORIZONTAL non-nil means apply requested width values.
3953 This function checks whether the requested values sum up to a valid
3954 window layout, recursively assigns the new sizes of all child windows
3955 and calculates and assigns the new start positions of these windows.
3957 Note: This function does not check any of `window-fixed-size-p',
3958 `window-min-height' or `window-min-width'. All these checks have to
3959 be applied on the Elisp level. */)
3960 (Lisp_Object frame, Lisp_Object horizontal)
3962 struct frame *f = decode_live_frame (frame);
3963 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3964 bool horflag = !NILP (horizontal);
3966 if (!window_resize_check (r, horflag)
3967 || (XINT (r->new_pixel)
3968 != (horflag ? r->pixel_width : r->pixel_height)))
3969 return Qnil;
3971 block_input ();
3972 window_resize_apply (r, horflag);
3974 fset_redisplay (f);
3975 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3977 adjust_frame_glyphs (f);
3978 unblock_input ();
3980 return Qt;
3984 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
3985 doc: /* Apply requested total size values for window-tree of FRAME.
3986 If FRAME is omitted or nil, it defaults to the selected frame.
3988 This function does not assign pixel or normal size values. You should
3989 have run `window-resize-apply' before running this.
3991 Optional argument HORIZONTAL omitted or nil means apply requested
3992 height values. HORIZONTAL non-nil means apply requested width
3993 values. */)
3994 (Lisp_Object frame, Lisp_Object horizontal)
3996 struct frame *f = decode_live_frame (frame);
3997 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3999 block_input ();
4000 /* Necessary when deleting the top-/or leftmost window. */
4001 r->left_col = 0;
4002 r->top_line = FRAME_TOP_MARGIN (f);
4003 window_resize_apply_total (r, !NILP (horizontal));
4004 /* Handle the mini window. */
4005 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4007 struct window *m = XWINDOW (f->minibuffer_window);
4009 if (NILP (horizontal))
4011 m->top_line = r->top_line + r->total_lines;
4012 m->total_lines = XFASTINT (m->new_total);
4014 else
4015 m->total_cols = XFASTINT (m->new_total);
4018 unblock_input ();
4020 return Qt;
4024 /* Resize frame F's windows when number of lines of F is set to SIZE.
4025 HORFLAG 1 means resize windows when number of columns of F is set to
4026 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4028 This function can delete all windows but the selected one in order to
4029 satisfy the request. The result will be meaningful if and only if
4030 F's windows have meaningful sizes when you call this. */
4031 void
4032 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4034 Lisp_Object root = f->root_window;
4035 struct window *r = XWINDOW (root);
4036 Lisp_Object mini = f->minibuffer_window;
4037 struct window *m;
4038 /* old_size is the old size of the frame's root window. */
4039 int old_size = horflag ? r->total_cols : r->total_lines;
4040 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4041 /* new_size is the new size of the frame's root window. */
4042 int new_size, new_pixel_size;
4043 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4045 /* Don't let the size drop below one unit. This is more comforting
4046 when we are called from x_set_tool_bar_lines since the latter may
4047 have implicitly given us a zero or negative height. */
4048 if (pixelwise)
4050 new_pixel_size = max (horflag
4051 ? size
4052 : (size
4053 - FRAME_TOP_MARGIN_HEIGHT (f)
4054 - ((FRAME_HAS_MINIBUF_P (f)
4055 && !FRAME_MINIBUF_ONLY_P (f))
4056 ? FRAME_LINE_HEIGHT (f) : 0)),
4057 unit);
4058 new_size = new_pixel_size / unit;
4060 else
4062 new_size = max (horflag
4063 ? size
4064 : (size
4065 - FRAME_TOP_MARGIN (f)
4066 - ((FRAME_HAS_MINIBUF_P (f)
4067 && !FRAME_MINIBUF_ONLY_P (f))
4068 ? 1 : 0)),
4070 new_pixel_size = new_size * unit;
4073 r->top_line = FRAME_TOP_MARGIN (f);
4074 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4076 if (new_pixel_size == old_pixel_size)
4078 else if (WINDOW_LEAF_P (r))
4079 /* For a leaf root window just set the size. */
4080 if (horflag)
4082 r->total_cols = new_size;
4083 r->pixel_width = new_pixel_size;
4085 else
4087 r->total_lines = new_size;
4088 r->pixel_height = new_pixel_size;
4090 else
4092 Lisp_Object delta;
4094 if (pixelwise)
4095 XSETINT (delta, new_pixel_size - old_pixel_size);
4096 else
4097 XSETINT (delta, new_size - old_size);
4099 /* Try a "normal" resize first. */
4100 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4101 pixelwise ? Qt : Qnil);
4102 if (window_resize_check (r, horflag)
4103 && new_pixel_size == XINT (r->new_pixel))
4105 window_resize_apply (r, horflag);
4106 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4108 else
4110 /* Try with "reasonable" minimum sizes next. */
4111 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4112 pixelwise ? Qt : Qnil);
4113 if (window_resize_check (r, horflag)
4114 && new_pixel_size == XINT (r->new_pixel))
4116 window_resize_apply (r, horflag);
4117 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4119 else
4121 /* Finally, try with "safe" minimum sizes. */
4122 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4123 pixelwise ? Qt : Qnil);
4124 if (window_resize_check (r, horflag)
4125 && new_pixel_size == XINT (r->new_pixel))
4127 window_resize_apply (r, horflag);
4128 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4130 #if 0 /* Let's try without killing other windows. */
4131 else
4133 /* We lost. Delete all windows but the frame's
4134 selected one. */
4135 root = f->selected_window;
4136 Fdelete_other_windows_internal (root, Qnil);
4137 if (horflag)
4139 XWINDOW (root)->total_cols = new_size;
4140 XWINDOW (root)->pixel_width = new_pixel_size;
4142 else
4144 XWINDOW (root)->total_lines = new_size;
4145 XWINDOW (root)->pixel_height = new_pixel_size;
4148 #endif /* 0 */
4153 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4155 m = XWINDOW (mini);
4156 if (horflag)
4158 m->total_cols = size;
4159 m->pixel_width = new_pixel_size;
4161 else
4163 /* Are we sure we always want 1 line here? */
4164 m->total_lines = 1;
4165 m->pixel_height = FRAME_LINE_HEIGHT (f);
4166 m->top_line = r->top_line + r->total_lines;
4167 m->pixel_top = r->pixel_top + r->pixel_height;
4171 fset_redisplay (f);
4175 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4176 doc: /* Split window OLD.
4177 Second argument PIXEL-SIZE specifies the number of pixels of the
4178 new window. In any case TOTAL-SIZE must be a positive integer.
4180 Third argument SIDE nil (or `below') specifies that the new window shall
4181 be located below WINDOW. SIDE `above' means the new window shall be
4182 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4183 height of the new window including space reserved for the mode and/or
4184 header line.
4186 SIDE t (or `right') specifies that the new window shall be located on
4187 the right side of WINDOW. SIDE `left' means the new window shall be
4188 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4189 width of the new window including space reserved for fringes and the
4190 scrollbar or a divider column.
4192 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4193 according to the SIDE argument.
4195 The new pixel and normal sizes of all involved windows must have been
4196 set correctly. See the code of `split-window' for how this is done. */)
4197 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4199 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4200 parent window or an internal window we have to install as OLD's new
4201 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4202 provided OLD is a leaf window, or to the frame's selected window.
4203 NEW (*n) is the new window created with some parameters taken from
4204 REFERENCE (*r). */
4205 register Lisp_Object new, frame, reference;
4206 register struct window *o, *p, *n, *r, *c;
4207 struct frame *f;
4208 bool horflag
4209 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4210 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4211 int combination_limit = 0, sum = 0;
4212 int total_size;
4214 CHECK_WINDOW (old);
4215 o = XWINDOW (old);
4216 frame = WINDOW_FRAME (o);
4217 f = XFRAME (frame);
4219 CHECK_NUMBER (pixel_size);
4220 total_size
4221 = XINT (pixel_size) / (horflag
4222 ? FRAME_COLUMN_WIDTH (f)
4223 : FRAME_LINE_HEIGHT (f));
4225 /* Set combination_limit to 1 if we have to make a new parent window.
4226 We do that if either `window-combination-limit' is t, or OLD has no
4227 parent, or OLD is ortho-combined. */
4228 combination_limit =
4229 EQ (Vwindow_combination_limit, Qt)
4230 || NILP (o->parent)
4231 || (horflag
4232 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4233 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4235 /* We need a live reference window to initialize some parameters. */
4236 if (WINDOW_LIVE_P (old))
4237 /* OLD is live, use it as reference window. */
4238 reference = old;
4239 else
4240 /* Use the frame's selected window as reference window. */
4241 reference = FRAME_SELECTED_WINDOW (f);
4242 r = XWINDOW (reference);
4244 /* The following bugs are caught by `split-window'. */
4245 if (MINI_WINDOW_P (o))
4246 error ("Attempt to split minibuffer window");
4247 else if (total_size < (horflag ? 2 : 1))
4248 error ("Size of new window too small (after split)");
4249 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4250 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4251 proportionally. */
4253 p = XWINDOW (o->parent);
4254 /* Temporarily pretend we split the parent window. */
4255 wset_new_pixel
4256 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4257 - XINT (pixel_size)));
4258 if (!window_resize_check (p, horflag))
4259 error ("Window sizes don't fit");
4260 else
4261 /* Undo the temporary pretension. */
4262 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4264 else
4266 if (!window_resize_check (o, horflag))
4267 error ("Resizing old window failed");
4268 else if (XINT (pixel_size) + XINT (o->new_pixel)
4269 != (horflag ? o->pixel_width : o->pixel_height))
4270 error ("Sum of sizes of old and new window don't fit");
4273 /* This is our point of no return. */
4274 if (combination_limit)
4276 /* Save the old value of o->normal_cols/lines. It gets corrupted
4277 by make_parent_window and we need it below for assigning it to
4278 p->new_normal. */
4279 Lisp_Object new_normal
4280 = horflag ? o->normal_cols : o->normal_lines;
4282 make_parent_window (old, horflag);
4283 p = XWINDOW (o->parent);
4284 if (EQ (Vwindow_combination_limit, Qt))
4285 /* Store t in the new parent's combination_limit slot to avoid
4286 that its children get merged into another window. */
4287 wset_combination_limit (p, Qt);
4288 /* These get applied below. */
4289 wset_new_pixel
4290 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4291 wset_new_total
4292 (p, make_number (horflag ? o->total_cols : o->total_lines));
4293 wset_new_normal (p, new_normal);
4295 else
4296 p = XWINDOW (o->parent);
4298 fset_redisplay (f);
4299 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4300 new = make_window ();
4301 n = XWINDOW (new);
4302 wset_frame (n, frame);
4303 wset_parent (n, o->parent);
4305 if (EQ (side, Qabove) || EQ (side, Qleft))
4307 wset_prev (n, o->prev);
4308 if (NILP (n->prev))
4309 wset_combination (p, horflag, new);
4310 else
4311 wset_next (XWINDOW (n->prev), new);
4312 wset_next (n, old);
4313 wset_prev (o, new);
4315 else
4317 wset_next (n, o->next);
4318 if (!NILP (n->next))
4319 wset_prev (XWINDOW (n->next), new);
4320 wset_prev (n, old);
4321 wset_next (o, new);
4324 n->window_end_valid = 0;
4325 n->last_cursor_vpos = 0;
4327 /* Get special geometry settings from reference window. */
4328 n->left_margin_cols = r->left_margin_cols;
4329 n->right_margin_cols = r->right_margin_cols;
4330 n->left_fringe_width = r->left_fringe_width;
4331 n->right_fringe_width = r->right_fringe_width;
4332 n->fringes_outside_margins = r->fringes_outside_margins;
4333 n->scroll_bar_width = r->scroll_bar_width;
4334 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4336 /* Directly assign orthogonal coordinates and sizes. */
4337 if (horflag)
4339 n->pixel_top = o->pixel_top;
4340 n->top_line = o->top_line;
4341 n->pixel_height = o->pixel_height;
4342 n->total_lines = o->total_lines;
4344 else
4346 n->pixel_left = o->pixel_left;
4347 n->left_col = o->left_col;
4348 n->pixel_width = o->pixel_width;
4349 n->total_cols = o->total_cols;
4352 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4353 get them ready here. */
4354 wset_new_pixel (n, pixel_size);
4355 c = XWINDOW (p->contents);
4356 while (c)
4358 if (c != n)
4359 sum = sum + XINT (c->new_total);
4360 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4362 wset_new_total (n, make_number ((horflag
4363 ? p->total_cols
4364 : p->total_lines)
4365 - sum));
4366 wset_new_normal (n, normal_size);
4368 block_input ();
4369 window_resize_apply (p, horflag);
4370 adjust_frame_glyphs (f);
4371 /* Set buffer of NEW to buffer of reference window. Don't run
4372 any hooks. */
4373 set_window_buffer (new, r->contents, 0, 1);
4374 unblock_input ();
4376 /* Maybe we should run the scroll functions in Elisp (which already
4377 runs the configuration change hook). */
4378 if (! NILP (Vwindow_scroll_functions))
4379 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4380 Fmarker_position (n->start));
4381 /* Return NEW. */
4382 return new;
4386 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4387 doc: /* Remove WINDOW from its frame.
4388 WINDOW defaults to the selected window. Return nil.
4389 Signal an error when WINDOW is the only window on its frame. */)
4390 (register Lisp_Object window)
4392 register Lisp_Object parent, sibling, frame, root;
4393 struct window *w, *p, *s, *r;
4394 struct frame *f;
4395 bool horflag, before_sibling = 0;
4397 w = decode_any_window (window);
4398 XSETWINDOW (window, w);
4399 if (NILP (w->contents))
4400 /* It's a no-op to delete an already deleted window. */
4401 return Qnil;
4403 parent = w->parent;
4404 if (NILP (parent))
4405 /* Never delete a minibuffer or frame root window. */
4406 error ("Attempt to delete minibuffer or sole ordinary window");
4407 else if (NILP (w->prev) && NILP (w->next))
4408 /* Rather bow out here, this case should be handled on the Elisp
4409 level. */
4410 error ("Attempt to delete sole window of parent");
4412 p = XWINDOW (parent);
4413 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4415 frame = WINDOW_FRAME (w);
4416 f = XFRAME (frame);
4418 root = FRAME_ROOT_WINDOW (f);
4419 r = XWINDOW (root);
4421 /* Unlink WINDOW from window tree. */
4422 if (NILP (w->prev))
4423 /* Get SIBLING below (on the right of) WINDOW. */
4425 /* before_sibling 1 means WINDOW is the first child of its
4426 parent and thus before the sibling. */
4427 before_sibling = 1;
4428 sibling = w->next;
4429 s = XWINDOW (sibling);
4430 wset_prev (s, Qnil);
4431 wset_combination (p, horflag, sibling);
4433 else
4434 /* Get SIBLING above (on the left of) WINDOW. */
4436 sibling = w->prev;
4437 s = XWINDOW (sibling);
4438 wset_next (s, w->next);
4439 if (!NILP (s->next))
4440 wset_prev (XWINDOW (s->next), sibling);
4443 if (window_resize_check (r, horflag)
4444 && (XINT (r->new_pixel)
4445 == (horflag ? r->pixel_width : r->pixel_height)))
4446 /* We can delete WINDOW now. */
4449 /* Block input. */
4450 block_input ();
4451 window_resize_apply (p, horflag);
4452 /* If this window is referred to by the dpyinfo's mouse
4453 highlight, invalidate that slot to be safe (Bug#9904). */
4454 if (!FRAME_INITIAL_P (f))
4456 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4458 if (EQ (hlinfo->mouse_face_window, window))
4459 hlinfo->mouse_face_window = Qnil;
4462 fset_redisplay (f);
4463 Vwindow_list = Qnil;
4464 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4466 wset_next (w, Qnil); /* Don't delete w->next too. */
4467 free_window_matrices (w);
4469 if (WINDOWP (w->contents))
4471 delete_all_child_windows (w->contents);
4472 wset_combination (w, 0, Qnil);
4474 else
4476 unshow_buffer (w);
4477 unchain_marker (XMARKER (w->pointm));
4478 unchain_marker (XMARKER (w->start));
4479 wset_buffer (w, Qnil);
4482 if (NILP (s->prev) && NILP (s->next))
4483 /* A matrjoshka where SIBLING has become the only child of
4484 PARENT. */
4486 /* Put SIBLING into PARENT's place. */
4487 replace_window (parent, sibling, 0);
4488 /* Have SIBLING inherit the following three slot values from
4489 PARENT (the combination_limit slot is not inherited). */
4490 wset_normal_cols (s, p->normal_cols);
4491 wset_normal_lines (s, p->normal_lines);
4492 /* Mark PARENT as deleted. */
4493 wset_combination (p, 0, Qnil);
4494 /* Try to merge SIBLING into its new parent. */
4495 recombine_windows (sibling);
4498 adjust_frame_glyphs (f);
4500 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4501 /* We deleted the frame's selected window. */
4503 /* Use the frame's first window as fallback ... */
4504 Lisp_Object new_selected_window = Fframe_first_window (frame);
4505 /* ... but preferably use its most recently used window. */
4506 Lisp_Object mru_window;
4508 /* `get-mru-window' might fail for some reason so play it safe
4509 - promote the first window _without recording it_ first. */
4510 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4511 Fselect_window (new_selected_window, Qt);
4512 else
4513 fset_selected_window (f, new_selected_window);
4515 unblock_input ();
4517 /* Now look whether `get-mru-window' gets us something. */
4518 mru_window = call1 (Qget_mru_window, frame);
4519 if (WINDOW_LIVE_P (mru_window)
4520 && EQ (XWINDOW (mru_window)->frame, frame))
4521 new_selected_window = mru_window;
4523 /* If all ended up well, we now promote the mru window. */
4524 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4525 Fselect_window (new_selected_window, Qnil);
4526 else
4527 fset_selected_window (f, new_selected_window);
4529 else
4530 unblock_input ();
4532 /* Must be run by the caller:
4533 run_window_configuration_change_hook (f); */
4535 else
4536 /* We failed: Relink WINDOW into window tree. */
4538 if (before_sibling)
4540 wset_prev (s, window);
4541 wset_combination (p, horflag, window);
4543 else
4545 wset_next (s, window);
4546 if (!NILP (w->next))
4547 wset_prev (XWINDOW (w->next), window);
4549 error ("Deletion failed");
4552 return Qnil;
4555 /***********************************************************************
4556 Resizing Mini-Windows
4557 ***********************************************************************/
4559 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4560 can. */
4561 void
4562 grow_mini_window (struct window *w, int delta, bool pixelwise)
4564 struct frame *f = XFRAME (w->frame);
4565 struct window *r;
4566 Lisp_Object root, height;
4567 int line_height, pixel_height;
4569 eassert (MINI_WINDOW_P (w));
4570 eassert (delta >= 0);
4572 if (delta > 0)
4574 root = FRAME_ROOT_WINDOW (f);
4575 r = XWINDOW (root);
4576 height = call3 (Qwindow_resize_root_window_vertically,
4577 root, make_number (- delta), pixelwise ? Qt : Qnil);
4578 if (INTEGERP (height) && window_resize_check (r, 0))
4580 block_input ();
4581 window_resize_apply (r, 0);
4583 if (pixelwise)
4585 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4586 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4588 else
4590 line_height = min (-XINT (height),
4591 ((INT_MAX - w->pixel_height)
4592 / FRAME_LINE_HEIGHT (f)));
4593 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4596 /* Grow the mini-window. */
4597 w->pixel_top = r->pixel_top + r->pixel_height;
4598 w->top_line = r->top_line + r->total_lines;
4599 w->pixel_height += pixel_height;
4600 w->total_lines += line_height;
4602 /* Enforce full redisplay of the frame. */
4603 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4604 fset_redisplay (f);
4605 adjust_frame_glyphs (f);
4606 unblock_input ();
4611 /* Shrink mini-window W to one line. */
4612 void
4613 shrink_mini_window (struct window *w, bool pixelwise)
4615 struct frame *f = XFRAME (w->frame);
4616 struct window *r;
4617 Lisp_Object root, delta;
4618 EMACS_INT height, unit;
4620 eassert (MINI_WINDOW_P (w));
4622 height = pixelwise ? w->pixel_height : w->total_lines;
4623 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4624 if (height > unit)
4626 root = FRAME_ROOT_WINDOW (f);
4627 r = XWINDOW (root);
4628 delta = call3 (Qwindow_resize_root_window_vertically,
4629 root, make_number (height - unit),
4630 pixelwise ? Qt : Qnil);
4631 if (INTEGERP (delta) && window_resize_check (r, 0))
4633 block_input ();
4634 window_resize_apply (r, 0);
4636 /* Shrink the mini-window. */
4637 w->top_line = r->top_line + r->total_lines;
4638 w->total_lines = 1;
4639 w->pixel_top = r->pixel_top + r->pixel_height;
4640 w->pixel_height = FRAME_LINE_HEIGHT (f);
4641 /* Enforce full redisplay of the frame. */
4642 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4643 fset_redisplay (f);
4644 adjust_frame_glyphs (f);
4645 unblock_input ();
4647 /* If the above failed for whatever strange reason we must make a
4648 one window frame here. The same routine will be needed when
4649 shrinking the frame (and probably when making the initial
4650 *scratch* window). For the moment leave things as they are. */
4654 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4655 doc: /* Resize minibuffer window WINDOW. */)
4656 (Lisp_Object window)
4658 struct window *w = XWINDOW (window);
4659 struct window *r;
4660 struct frame *f;
4661 int height;
4663 CHECK_WINDOW (window);
4664 f = XFRAME (w->frame);
4666 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4667 error ("Not a valid minibuffer window");
4668 else if (FRAME_MINIBUF_ONLY_P (f))
4669 error ("Cannot resize a minibuffer-only frame");
4671 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4672 height = r->pixel_height + w->pixel_height;
4673 if (window_resize_check (r, 0)
4674 && XINT (w->new_pixel) > 0
4675 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4677 block_input ();
4678 window_resize_apply (r, 0);
4680 w->total_lines = XFASTINT (w->new_total);
4681 w->top_line = r->top_line + r->total_lines;
4682 w->pixel_height = XFASTINT (w->new_pixel);
4683 w->pixel_top = r->pixel_top + r->pixel_height;
4685 fset_redisplay (f);
4686 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4687 adjust_frame_glyphs (f);
4688 unblock_input ();
4689 return Qt;
4691 else
4692 error ("Failed to resize minibuffer window");
4695 /* Mark window cursors off for all windows in the window tree rooted
4696 at W by setting their phys_cursor_on_p flag to zero. Called from
4697 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4698 the frame are cleared. */
4700 void
4701 mark_window_cursors_off (struct window *w)
4703 while (w)
4705 if (WINDOWP (w->contents))
4706 mark_window_cursors_off (XWINDOW (w->contents));
4707 else
4708 w->phys_cursor_on_p = 0;
4710 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4715 /* Return number of lines of text (not counting mode lines) in W. */
4718 window_internal_height (struct window *w)
4720 int ht = w->total_lines;
4722 if (!MINI_WINDOW_P (w))
4724 if (!NILP (w->parent)
4725 || WINDOWP (w->contents)
4726 || !NILP (w->next)
4727 || !NILP (w->prev)
4728 || WINDOW_WANTS_MODELINE_P (w))
4729 --ht;
4731 if (WINDOW_WANTS_HEADER_LINE_P (w))
4732 --ht;
4735 return ht;
4738 /************************************************************************
4739 Window Scrolling
4740 ***********************************************************************/
4742 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4743 N screen-fulls, which is defined as the height of the window minus
4744 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4745 instead. Negative values of N mean scroll down. NOERROR non-zero
4746 means don't signal an error if we try to move over BEGV or ZV,
4747 respectively. */
4749 static void
4750 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4752 immediate_quit = 1;
4753 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4755 wset_redisplay (XWINDOW (window));
4757 /* If we must, use the pixel-based version which is much slower than
4758 the line-based one but can handle varying line heights. */
4759 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4760 window_scroll_pixel_based (window, n, whole, noerror);
4761 else
4762 window_scroll_line_based (window, n, whole, noerror);
4764 /* Bug#15957. */
4765 XWINDOW (window)->window_end_valid = 0;
4766 immediate_quit = 0;
4770 /* Implementation of window_scroll that works based on pixel line
4771 heights. See the comment of window_scroll for parameter
4772 descriptions. */
4774 static void
4775 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4777 struct it it;
4778 struct window *w = XWINDOW (window);
4779 struct text_pos start;
4780 int this_scroll_margin;
4781 /* True if we fiddled the window vscroll field without really scrolling. */
4782 int vscrolled = 0;
4783 int x, y, rtop, rbot, rowh, vpos;
4784 void *itdata = NULL;
4785 int window_total_lines;
4786 int frame_line_height = default_line_pixel_height (w);
4788 SET_TEXT_POS_FROM_MARKER (start, w->start);
4789 /* Scrolling a minibuffer window via scroll bar when the echo area
4790 shows long text sometimes resets the minibuffer contents behind
4791 our backs. */
4792 if (CHARPOS (start) > ZV)
4793 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4795 /* If PT is not visible in WINDOW, move back one half of
4796 the screen. Allow PT to be partially visible, otherwise
4797 something like (scroll-down 1) with PT in the line before
4798 the partially visible one would recenter. */
4800 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4802 itdata = bidi_shelve_cache ();
4803 /* Move backward half the height of the window. Performance note:
4804 vmotion used here is about 10% faster, but would give wrong
4805 results for variable height lines. */
4806 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4807 it.current_y = it.last_visible_y;
4808 move_it_vertically_backward (&it, window_box_height (w) / 2);
4810 /* The function move_iterator_vertically may move over more than
4811 the specified y-distance. If it->w is small, e.g. a
4812 mini-buffer window, we may end up in front of the window's
4813 display area. This is the case when Start displaying at the
4814 start of the line containing PT in this case. */
4815 if (it.current_y <= 0)
4817 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4818 move_it_vertically_backward (&it, 0);
4819 it.current_y = 0;
4822 start = it.current.pos;
4823 bidi_unshelve_cache (itdata, 0);
4825 else if (auto_window_vscroll_p)
4827 if (rtop || rbot) /* partially visible */
4829 int px;
4830 int dy = frame_line_height;
4831 if (whole)
4832 dy = max ((window_box_height (w)
4833 - next_screen_context_lines * dy),
4834 dy);
4835 dy *= n;
4837 if (n < 0)
4839 /* Only vscroll backwards if already vscrolled forwards. */
4840 if (w->vscroll < 0 && rtop > 0)
4842 px = max (0, -w->vscroll - min (rtop, -dy));
4843 Fset_window_vscroll (window, make_number (px), Qt);
4844 return;
4847 if (n > 0)
4849 /* Do vscroll if already vscrolled or only display line. */
4850 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4852 px = max (0, -w->vscroll + min (rbot, dy));
4853 Fset_window_vscroll (window, make_number (px), Qt);
4854 return;
4857 /* Maybe modify window start instead of scrolling. */
4858 if (rbot > 0 || w->vscroll < 0)
4860 ptrdiff_t spos;
4862 Fset_window_vscroll (window, make_number (0), Qt);
4863 /* If there are other text lines above the current row,
4864 move window start to current row. Else to next row. */
4865 if (rbot > 0)
4866 spos = XINT (Fline_beginning_position (Qnil));
4867 else
4868 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4869 set_marker_restricted (w->start, make_number (spos),
4870 w->contents);
4871 w->start_at_line_beg = 1;
4872 w->update_mode_line = 1;
4873 /* Set force_start so that redisplay_window will run the
4874 window-scroll-functions. */
4875 w->force_start = 1;
4876 return;
4880 /* Cancel previous vscroll. */
4881 Fset_window_vscroll (window, make_number (0), Qt);
4884 itdata = bidi_shelve_cache ();
4885 /* If scroll_preserve_screen_position is non-nil, we try to set
4886 point in the same window line as it is now, so get that line. */
4887 if (!NILP (Vscroll_preserve_screen_position))
4889 /* We preserve the goal pixel coordinate across consecutive
4890 calls to scroll-up, scroll-down and other commands that
4891 have the `scroll-command' property. This avoids the
4892 possibility of point becoming "stuck" on a tall line when
4893 scrolling by one line. */
4894 if (window_scroll_pixel_based_preserve_y < 0
4895 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4896 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4898 start_display (&it, w, start);
4899 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4900 window_scroll_pixel_based_preserve_y = it.current_y;
4901 window_scroll_pixel_based_preserve_x = it.current_x;
4904 else
4905 window_scroll_pixel_based_preserve_y
4906 = window_scroll_pixel_based_preserve_x = -1;
4908 /* Move iterator it from start the specified distance forward or
4909 backward. The result is the new window start. */
4910 start_display (&it, w, start);
4911 if (whole)
4913 ptrdiff_t start_pos = IT_CHARPOS (it);
4914 int dy = frame_line_height;
4915 dy = max ((window_box_height (w)
4916 - next_screen_context_lines * dy),
4917 dy) * n;
4919 /* Note that move_it_vertically always moves the iterator to the
4920 start of a line. So, if the last line doesn't have a newline,
4921 we would end up at the start of the line ending at ZV. */
4922 if (dy <= 0)
4924 move_it_vertically_backward (&it, -dy);
4925 /* Ensure we actually do move, e.g. in case we are currently
4926 looking at an image that is taller that the window height. */
4927 while (start_pos == IT_CHARPOS (it)
4928 && start_pos > BEGV)
4929 move_it_by_lines (&it, -1);
4931 else if (dy > 0)
4933 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4934 MOVE_TO_POS | MOVE_TO_Y);
4935 /* Ensure we actually do move, e.g. in case we are currently
4936 looking at an image that is taller that the window height. */
4937 while (start_pos == IT_CHARPOS (it)
4938 && start_pos < ZV)
4939 move_it_by_lines (&it, 1);
4942 else
4943 move_it_by_lines (&it, n);
4945 /* We failed if we find ZV is already on the screen (scrolling up,
4946 means there's nothing past the end), or if we can't start any
4947 earlier (scrolling down, means there's nothing past the top). */
4948 if ((n > 0 && IT_CHARPOS (it) == ZV)
4949 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4951 if (IT_CHARPOS (it) == ZV)
4953 if (it.current_y < it.last_visible_y
4954 && (it.current_y + it.max_ascent + it.max_descent
4955 > it.last_visible_y))
4957 /* The last line was only partially visible, make it fully
4958 visible. */
4959 w->vscroll = (it.last_visible_y
4960 - it.current_y + it.max_ascent + it.max_descent);
4961 adjust_frame_glyphs (it.f);
4963 else
4965 bidi_unshelve_cache (itdata, 0);
4966 if (noerror)
4967 return;
4968 else if (n < 0) /* could happen with empty buffers */
4969 xsignal0 (Qbeginning_of_buffer);
4970 else
4971 xsignal0 (Qend_of_buffer);
4974 else
4976 if (w->vscroll != 0)
4977 /* The first line was only partially visible, make it fully
4978 visible. */
4979 w->vscroll = 0;
4980 else
4982 bidi_unshelve_cache (itdata, 0);
4983 if (noerror)
4984 return;
4985 else
4986 xsignal0 (Qbeginning_of_buffer);
4990 /* If control gets here, then we vscrolled. */
4992 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
4994 /* Don't try to change the window start below. */
4995 vscrolled = 1;
4998 if (! vscrolled)
5000 ptrdiff_t pos = IT_CHARPOS (it);
5001 ptrdiff_t bytepos;
5003 /* If in the middle of a multi-glyph character move forward to
5004 the next character. */
5005 if (in_display_vector_p (&it))
5007 ++pos;
5008 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5011 /* Set the window start, and set up the window for redisplay. */
5012 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5013 IT_BYTEPOS (it));
5014 bytepos = marker_byte_position (w->start);
5015 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5016 w->update_mode_line = 1;
5017 /* Set force_start so that redisplay_window will run the
5018 window-scroll-functions. */
5019 w->force_start = 1;
5022 /* The rest of this function uses current_y in a nonstandard way,
5023 not including the height of the header line if any. */
5024 it.current_y = it.vpos = 0;
5026 /* Move PT out of scroll margins.
5027 This code wants current_y to be zero at the window start position
5028 even if there is a header line. */
5029 window_total_lines
5030 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5031 this_scroll_margin = max (0, scroll_margin);
5032 this_scroll_margin
5033 = min (this_scroll_margin, window_total_lines / 4);
5034 this_scroll_margin *= frame_line_height;
5036 if (n > 0)
5038 /* We moved the window start towards ZV, so PT may be now
5039 in the scroll margin at the top. */
5040 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5041 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5042 && (NILP (Vscroll_preserve_screen_position)
5043 || EQ (Vscroll_preserve_screen_position, Qt)))
5044 /* We found PT at a legitimate height. Leave it alone. */
5046 else if (window_scroll_pixel_based_preserve_y >= 0)
5048 /* If we have a header line, take account of it.
5049 This is necessary because we set it.current_y to 0, above. */
5050 move_it_to (&it, -1,
5051 window_scroll_pixel_based_preserve_x,
5052 window_scroll_pixel_based_preserve_y
5053 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5054 -1, MOVE_TO_Y | MOVE_TO_X);
5055 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5057 else
5059 while (it.current_y < this_scroll_margin)
5061 int prev = it.current_y;
5062 move_it_by_lines (&it, 1);
5063 if (prev == it.current_y)
5064 break;
5066 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5069 else if (n < 0)
5071 ptrdiff_t charpos, bytepos;
5072 int partial_p;
5074 /* Save our position, for the
5075 window_scroll_pixel_based_preserve_y case. */
5076 charpos = IT_CHARPOS (it);
5077 bytepos = IT_BYTEPOS (it);
5079 /* We moved the window start towards BEGV, so PT may be now
5080 in the scroll margin at the bottom. */
5081 move_it_to (&it, PT, -1,
5082 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5083 - this_scroll_margin - 1),
5085 MOVE_TO_POS | MOVE_TO_Y);
5087 /* Save our position, in case it's correct. */
5088 charpos = IT_CHARPOS (it);
5089 bytepos = IT_BYTEPOS (it);
5091 /* See if point is on a partially visible line at the end. */
5092 if (it.what == IT_EOB)
5093 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5094 else
5096 move_it_by_lines (&it, 1);
5097 partial_p = it.current_y > it.last_visible_y;
5100 if (charpos == PT && !partial_p
5101 && (NILP (Vscroll_preserve_screen_position)
5102 || EQ (Vscroll_preserve_screen_position, Qt)))
5103 /* We found PT before we found the display margin, so PT is ok. */
5105 else if (window_scroll_pixel_based_preserve_y >= 0)
5107 SET_TEXT_POS_FROM_MARKER (start, w->start);
5108 start_display (&it, w, start);
5109 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5110 here because we called start_display again and did not
5111 alter it.current_y this time. */
5112 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5113 window_scroll_pixel_based_preserve_y, -1,
5114 MOVE_TO_Y | MOVE_TO_X);
5115 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5117 else
5119 if (partial_p)
5120 /* The last line was only partially visible, so back up two
5121 lines to make sure we're on a fully visible line. */
5123 move_it_by_lines (&it, -2);
5124 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5126 else
5127 /* No, the position we saved is OK, so use it. */
5128 SET_PT_BOTH (charpos, bytepos);
5131 bidi_unshelve_cache (itdata, 0);
5135 /* Implementation of window_scroll that works based on screen lines.
5136 See the comment of window_scroll for parameter descriptions. */
5138 static void
5139 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5141 register struct window *w = XWINDOW (window);
5142 /* Fvertical_motion enters redisplay, which can trigger
5143 fontification, which in turn can modify buffer text (e.g., if the
5144 fontification functions replace escape sequences with faces, as
5145 in `grep-mode-font-lock-keywords'). So we use a marker to record
5146 the old point position, to prevent crashes in SET_PT_BOTH. */
5147 Lisp_Object opoint_marker = Fpoint_marker ();
5148 register ptrdiff_t pos, pos_byte;
5149 register int ht = window_internal_height (w);
5150 register Lisp_Object tem;
5151 bool lose;
5152 Lisp_Object bolp;
5153 ptrdiff_t startpos = marker_position (w->start);
5154 ptrdiff_t startbyte = marker_byte_position (w->start);
5155 Lisp_Object original_pos = Qnil;
5157 /* If scrolling screen-fulls, compute the number of lines to
5158 scroll from the window's height. */
5159 if (whole)
5160 n *= max (1, ht - next_screen_context_lines);
5162 if (!NILP (Vscroll_preserve_screen_position))
5164 if (window_scroll_preserve_vpos <= 0
5165 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5166 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5168 struct position posit
5169 = *compute_motion (startpos, startbyte, 0, 0, 0,
5170 PT, ht, 0, -1, w->hscroll, 0, w);
5171 window_scroll_preserve_vpos = posit.vpos;
5172 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5175 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5176 make_number (window_scroll_preserve_vpos));
5179 XSETFASTINT (tem, PT);
5180 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5182 if (NILP (tem))
5184 Fvertical_motion (make_number (- (ht / 2)), window);
5185 startpos = PT;
5186 startbyte = PT_BYTE;
5189 SET_PT_BOTH (startpos, startbyte);
5190 lose = n < 0 && PT == BEGV;
5191 Fvertical_motion (make_number (n), window);
5192 pos = PT;
5193 pos_byte = PT_BYTE;
5194 bolp = Fbolp ();
5195 SET_PT_BOTH (marker_position (opoint_marker),
5196 marker_byte_position (opoint_marker));
5198 if (lose)
5200 if (noerror)
5201 return;
5202 else
5203 xsignal0 (Qbeginning_of_buffer);
5206 if (pos < ZV)
5208 /* Don't use a scroll margin that is negative or too large. */
5209 int this_scroll_margin =
5210 max (0, min (scroll_margin, w->total_lines / 4));
5212 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5213 w->start_at_line_beg = !NILP (bolp);
5214 w->update_mode_line = 1;
5215 /* Set force_start so that redisplay_window will run
5216 the window-scroll-functions. */
5217 w->force_start = 1;
5219 if (!NILP (Vscroll_preserve_screen_position)
5220 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5222 SET_PT_BOTH (pos, pos_byte);
5223 Fvertical_motion (original_pos, window);
5225 /* If we scrolled forward, put point enough lines down
5226 that it is outside the scroll margin. */
5227 else if (n > 0)
5229 int top_margin;
5231 if (this_scroll_margin > 0)
5233 SET_PT_BOTH (pos, pos_byte);
5234 Fvertical_motion (make_number (this_scroll_margin), window);
5235 top_margin = PT;
5237 else
5238 top_margin = pos;
5240 if (top_margin <= marker_position (opoint_marker))
5241 SET_PT_BOTH (marker_position (opoint_marker),
5242 marker_byte_position (opoint_marker));
5243 else if (!NILP (Vscroll_preserve_screen_position))
5245 SET_PT_BOTH (pos, pos_byte);
5246 Fvertical_motion (original_pos, window);
5248 else
5249 SET_PT (top_margin);
5251 else if (n < 0)
5253 int bottom_margin;
5255 /* If we scrolled backward, put point near the end of the window
5256 but not within the scroll margin. */
5257 SET_PT_BOTH (pos, pos_byte);
5258 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5259 if (XFASTINT (tem) == ht - this_scroll_margin)
5260 bottom_margin = PT;
5261 else
5262 bottom_margin = PT + 1;
5264 if (bottom_margin > marker_position (opoint_marker))
5265 SET_PT_BOTH (marker_position (opoint_marker),
5266 marker_byte_position (opoint_marker));
5267 else
5269 if (!NILP (Vscroll_preserve_screen_position))
5271 SET_PT_BOTH (pos, pos_byte);
5272 Fvertical_motion (original_pos, window);
5274 else
5275 Fvertical_motion (make_number (-1), window);
5279 else
5281 if (noerror)
5282 return;
5283 else
5284 xsignal0 (Qend_of_buffer);
5289 /* Scroll selected_window up or down. If N is nil, scroll a
5290 screen-full which is defined as the height of the window minus
5291 next_screen_context_lines. If N is the symbol `-', scroll.
5292 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5293 up. This is the guts of Fscroll_up and Fscroll_down. */
5295 static void
5296 scroll_command (Lisp_Object n, int direction)
5298 ptrdiff_t count = SPECPDL_INDEX ();
5300 eassert (eabs (direction) == 1);
5302 /* If selected window's buffer isn't current, make it current for
5303 the moment. But don't screw up if window_scroll gets an error. */
5304 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5306 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5307 Fset_buffer (XWINDOW (selected_window)->contents);
5310 if (NILP (n))
5311 window_scroll (selected_window, direction, 1, 0);
5312 else if (EQ (n, Qminus))
5313 window_scroll (selected_window, -direction, 1, 0);
5314 else
5316 n = Fprefix_numeric_value (n);
5317 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5320 unbind_to (count, Qnil);
5323 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5324 doc: /* Scroll text of selected window upward ARG lines.
5325 If ARG is omitted or nil, scroll upward by a near full screen.
5326 A near full screen is `next-screen-context-lines' less than a full screen.
5327 Negative ARG means scroll downward.
5328 If ARG is the atom `-', scroll downward by nearly full screen.
5329 When calling from a program, supply as argument a number, nil, or `-'. */)
5330 (Lisp_Object arg)
5332 scroll_command (arg, 1);
5333 return Qnil;
5336 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5337 doc: /* Scroll text of selected window down ARG lines.
5338 If ARG is omitted or nil, scroll down by a near full screen.
5339 A near full screen is `next-screen-context-lines' less than a full screen.
5340 Negative ARG means scroll upward.
5341 If ARG is the atom `-', scroll upward by nearly full screen.
5342 When calling from a program, supply as argument a number, nil, or `-'. */)
5343 (Lisp_Object arg)
5345 scroll_command (arg, -1);
5346 return Qnil;
5349 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5350 doc: /* Return the other window for \"other window scroll\" commands.
5351 If `other-window-scroll-buffer' is non-nil, a window
5352 showing that buffer is used.
5353 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5354 specifies the window. This takes precedence over
5355 `other-window-scroll-buffer'. */)
5356 (void)
5358 Lisp_Object window;
5360 if (MINI_WINDOW_P (XWINDOW (selected_window))
5361 && !NILP (Vminibuf_scroll_window))
5362 window = Vminibuf_scroll_window;
5363 /* If buffer is specified, scroll that buffer. */
5364 else if (!NILP (Vother_window_scroll_buffer))
5366 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5367 if (NILP (window))
5368 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5370 else
5372 /* Nothing specified; look for a neighboring window on the same
5373 frame. */
5374 window = Fnext_window (selected_window, Qnil, Qnil);
5376 if (EQ (window, selected_window))
5377 /* That didn't get us anywhere; look for a window on another
5378 visible frame. */
5380 window = Fnext_window (window, Qnil, Qt);
5381 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5382 && ! EQ (window, selected_window));
5385 CHECK_LIVE_WINDOW (window);
5387 if (EQ (window, selected_window))
5388 error ("There is no other window");
5390 return window;
5393 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5394 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5395 A near full screen is `next-screen-context-lines' less than a full screen.
5396 The next window is the one below the current one; or the one at the top
5397 if the current one is at the bottom. Negative ARG means scroll downward.
5398 If ARG is the atom `-', scroll downward by nearly full screen.
5399 When calling from a program, supply as argument a number, nil, or `-'.
5401 If `other-window-scroll-buffer' is non-nil, scroll the window
5402 showing that buffer, popping the buffer up if necessary.
5403 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5404 specifies the window to scroll. This takes precedence over
5405 `other-window-scroll-buffer'. */)
5406 (Lisp_Object arg)
5408 Lisp_Object window;
5409 struct window *w;
5410 ptrdiff_t count = SPECPDL_INDEX ();
5412 window = Fother_window_for_scrolling ();
5413 w = XWINDOW (window);
5415 /* Don't screw up if window_scroll gets an error. */
5416 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5418 Fset_buffer (w->contents);
5419 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5421 if (NILP (arg))
5422 window_scroll (window, 1, 1, 1);
5423 else if (EQ (arg, Qminus))
5424 window_scroll (window, -1, 1, 1);
5425 else
5427 if (CONSP (arg))
5428 arg = XCAR (arg);
5429 CHECK_NUMBER (arg);
5430 window_scroll (window, XINT (arg), 0, 1);
5433 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5434 unbind_to (count, Qnil);
5436 return Qnil;
5439 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5440 doc: /* Scroll selected window display ARG columns left.
5441 Default for ARG is window width minus 2.
5442 Value is the total amount of leftward horizontal scrolling in
5443 effect after the change.
5444 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5445 lower bound for automatic scrolling, i.e. automatic scrolling
5446 will not scroll a window to a column less than the value returned
5447 by this function. This happens in an interactive call. */)
5448 (register Lisp_Object arg, Lisp_Object set_minimum)
5450 struct window *w = XWINDOW (selected_window);
5451 EMACS_INT requested_arg = (NILP (arg)
5452 ? window_body_width (w, 0) - 2
5453 : XINT (Fprefix_numeric_value (arg)));
5454 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5456 if (!NILP (set_minimum))
5457 w->min_hscroll = w->hscroll;
5459 return result;
5462 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5463 doc: /* Scroll selected window display ARG columns right.
5464 Default for ARG is window width minus 2.
5465 Value is the total amount of leftward horizontal scrolling in
5466 effect after the change.
5467 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5468 lower bound for automatic scrolling, i.e. automatic scrolling
5469 will not scroll a window to a column less than the value returned
5470 by this function. This happens in an interactive call. */)
5471 (register Lisp_Object arg, Lisp_Object set_minimum)
5473 struct window *w = XWINDOW (selected_window);
5474 EMACS_INT requested_arg = (NILP (arg)
5475 ? window_body_width (w, 0) - 2
5476 : XINT (Fprefix_numeric_value (arg)));
5477 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5479 if (!NILP (set_minimum))
5480 w->min_hscroll = w->hscroll;
5482 return result;
5485 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5486 doc: /* Return the window which was selected when entering the minibuffer.
5487 Returns nil, if selected window is not a minibuffer window. */)
5488 (void)
5490 if (minibuf_level > 0
5491 && MINI_WINDOW_P (XWINDOW (selected_window))
5492 && WINDOW_LIVE_P (minibuf_selected_window))
5493 return minibuf_selected_window;
5495 return Qnil;
5498 /* Value is the number of lines actually displayed in window W,
5499 as opposed to its height. */
5501 static int
5502 displayed_window_lines (struct window *w)
5504 struct it it;
5505 struct text_pos start;
5506 int height = window_box_height (w);
5507 struct buffer *old_buffer;
5508 int bottom_y;
5509 void *itdata = NULL;
5511 if (XBUFFER (w->contents) != current_buffer)
5513 old_buffer = current_buffer;
5514 set_buffer_internal (XBUFFER (w->contents));
5516 else
5517 old_buffer = NULL;
5519 /* In case W->start is out of the accessible range, do something
5520 reasonable. This happens in Info mode when Info-scroll-down
5521 calls (recenter -1) while W->start is 1. */
5522 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5524 itdata = bidi_shelve_cache ();
5525 start_display (&it, w, start);
5526 move_it_vertically (&it, height);
5527 bottom_y = line_bottom_y (&it);
5528 bidi_unshelve_cache (itdata, 0);
5530 /* rms: On a non-window display,
5531 the value of it.vpos at the bottom of the screen
5532 seems to be 1 larger than window_box_height (w).
5533 This kludge fixes a bug whereby (move-to-window-line -1)
5534 when ZV is on the last screen line
5535 moves to the previous screen line instead of the last one. */
5536 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5537 height++;
5539 /* Add in empty lines at the bottom of the window. */
5540 if (bottom_y < height)
5542 int uy = FRAME_LINE_HEIGHT (it.f);
5543 it.vpos += (height - bottom_y + uy - 1) / uy;
5546 if (old_buffer)
5547 set_buffer_internal (old_buffer);
5549 return it.vpos;
5553 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5554 doc: /* Center point in selected window and maybe redisplay frame.
5555 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5556 relative to the selected window. If ARG is negative, it counts up from the
5557 bottom of the window. (ARG should be less than the height of the window.)
5559 If ARG is omitted or nil, then recenter with point on the middle line of
5560 the selected window; if the variable `recenter-redisplay' is non-nil,
5561 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5562 is set to `grow-only', this resets the tool-bar's height to the minimum
5563 height needed); if `recenter-redisplay' has the special value `tty',
5564 then only tty frames are redrawn.
5566 Just C-u as prefix means put point in the center of the window
5567 and redisplay normally--don't erase and redraw the frame. */)
5568 (register Lisp_Object arg)
5570 struct window *w = XWINDOW (selected_window);
5571 struct buffer *buf = XBUFFER (w->contents);
5572 struct buffer *obuf = current_buffer;
5573 bool center_p = 0;
5574 ptrdiff_t charpos, bytepos;
5575 EMACS_INT iarg IF_LINT (= 0);
5576 int this_scroll_margin;
5578 /* If redisplay is suppressed due to an error, try again. */
5579 obuf->display_error_modiff = 0;
5581 if (NILP (arg))
5583 if (!NILP (Vrecenter_redisplay)
5584 && (!EQ (Vrecenter_redisplay, Qtty)
5585 || !NILP (Ftty_type (selected_frame))))
5587 ptrdiff_t i;
5589 /* Invalidate pixel data calculated for all compositions. */
5590 for (i = 0; i < n_compositions; i++)
5591 composition_table[i]->font = NULL;
5592 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5593 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5594 #endif
5595 Fredraw_frame (WINDOW_FRAME (w));
5596 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5599 center_p = 1;
5601 else if (CONSP (arg)) /* Just C-u. */
5602 center_p = 1;
5603 else
5605 arg = Fprefix_numeric_value (arg);
5606 CHECK_NUMBER (arg);
5607 iarg = XINT (arg);
5610 set_buffer_internal (buf);
5612 /* Do this after making BUF current
5613 in case scroll_margin is buffer-local. */
5614 this_scroll_margin =
5615 max (0, min (scroll_margin, w->total_lines / 4));
5617 /* Handle centering on a graphical frame specially. Such frames can
5618 have variable-height lines and centering point on the basis of
5619 line counts would lead to strange effects. */
5620 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5622 if (center_p)
5624 struct it it;
5625 struct text_pos pt;
5626 void *itdata = bidi_shelve_cache ();
5628 SET_TEXT_POS (pt, PT, PT_BYTE);
5629 start_display (&it, w, pt);
5630 move_it_vertically_backward (&it, window_box_height (w) / 2);
5631 charpos = IT_CHARPOS (it);
5632 bytepos = IT_BYTEPOS (it);
5633 bidi_unshelve_cache (itdata, 0);
5635 else if (iarg < 0)
5637 struct it it;
5638 struct text_pos pt;
5639 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5640 int extra_line_spacing;
5641 int h = window_box_height (w);
5642 void *itdata = bidi_shelve_cache ();
5644 iarg = - max (-iarg, this_scroll_margin);
5646 SET_TEXT_POS (pt, PT, PT_BYTE);
5647 start_display (&it, w, pt);
5649 /* Be sure we have the exact height of the full line containing PT. */
5650 move_it_by_lines (&it, 0);
5652 /* The amount of pixels we have to move back is the window
5653 height minus what's displayed in the line containing PT,
5654 and the lines below. */
5655 it.current_y = 0;
5656 it.vpos = 0;
5657 move_it_by_lines (&it, nlines);
5659 if (it.vpos == nlines)
5660 h -= it.current_y;
5661 else
5663 /* Last line has no newline */
5664 h -= line_bottom_y (&it);
5665 it.vpos++;
5668 /* Don't reserve space for extra line spacing of last line. */
5669 extra_line_spacing = it.max_extra_line_spacing;
5671 /* If we can't move down NLINES lines because we hit
5672 the end of the buffer, count in some empty lines. */
5673 if (it.vpos < nlines)
5675 nlines -= it.vpos;
5676 extra_line_spacing = it.extra_line_spacing;
5677 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5679 if (h <= 0)
5681 bidi_unshelve_cache (itdata, 0);
5682 return Qnil;
5685 /* Now find the new top line (starting position) of the window. */
5686 start_display (&it, w, pt);
5687 it.current_y = 0;
5688 move_it_vertically_backward (&it, h);
5690 /* If extra line spacing is present, we may move too far
5691 back. This causes the last line to be only partially
5692 visible (which triggers redisplay to recenter that line
5693 in the middle), so move forward.
5694 But ignore extra line spacing on last line, as it is not
5695 considered to be part of the visible height of the line.
5697 h += extra_line_spacing;
5698 while (-it.current_y > h)
5699 move_it_by_lines (&it, 1);
5701 charpos = IT_CHARPOS (it);
5702 bytepos = IT_BYTEPOS (it);
5704 bidi_unshelve_cache (itdata, 0);
5706 else
5708 struct position pos;
5710 iarg = max (iarg, this_scroll_margin);
5712 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5713 charpos = pos.bufpos;
5714 bytepos = pos.bytepos;
5717 else
5719 struct position pos;
5720 int ht = window_internal_height (w);
5722 if (center_p)
5723 iarg = ht / 2;
5724 else if (iarg < 0)
5725 iarg += ht;
5727 /* Don't let it get into the margin at either top or bottom. */
5728 iarg = clip_to_bounds (this_scroll_margin, iarg,
5729 ht - this_scroll_margin - 1);
5731 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5732 charpos = pos.bufpos;
5733 bytepos = pos.bytepos;
5736 /* Set the new window start. */
5737 set_marker_both (w->start, w->contents, charpos, bytepos);
5738 w->window_end_valid = 0;
5740 w->optional_new_start = 1;
5742 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5743 FETCH_BYTE (bytepos - 1) == '\n');
5745 set_buffer_internal (obuf);
5746 return Qnil;
5749 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5750 0, 1, 0,
5751 doc: /* Return the width in columns of the text display area of WINDOW.
5752 WINDOW must be a live window and defaults to the selected one.
5754 The returned width does not include dividers, scrollbars, margins,
5755 fringes, nor any partial-width columns at the right of the text
5756 area. */)
5757 (Lisp_Object window)
5759 struct window *w = decode_live_window (window);
5761 return make_number (window_box_width (w, TEXT_AREA)
5762 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5765 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5766 0, 1, 0,
5767 doc: /* Return the height in lines of the text display area of WINDOW.
5768 WINDOW must be a live window and defaults to the selected one.
5770 The returned height does not include dividers, the mode line, any header
5771 line, nor any partial-height lines at the bottom of the text area. */)
5772 (Lisp_Object window)
5774 struct window *w = decode_live_window (window);
5776 return make_number (window_box_height (w)
5777 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5780 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5781 1, 1, "P",
5782 doc: /* Position point relative to window.
5783 ARG nil means position point at center of window.
5784 Else, ARG specifies vertical position within the window;
5785 zero means top of window, negative means relative to bottom of window. */)
5786 (Lisp_Object arg)
5788 struct window *w = XWINDOW (selected_window);
5789 int lines, start;
5790 Lisp_Object window;
5791 #if 0
5792 int this_scroll_margin;
5793 #endif
5795 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5796 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5797 when passed below to set_marker_both. */
5798 error ("move-to-window-line called from unrelated buffer");
5800 window = selected_window;
5801 start = marker_position (w->start);
5802 if (start < BEGV || start > ZV)
5804 int height = window_internal_height (w);
5805 Fvertical_motion (make_number (- (height / 2)), window);
5806 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5807 w->start_at_line_beg = !NILP (Fbolp ());
5808 w->force_start = 1;
5810 else
5811 Fgoto_char (w->start);
5813 lines = displayed_window_lines (w);
5815 #if 0
5816 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5817 #endif
5819 if (NILP (arg))
5820 XSETFASTINT (arg, lines / 2);
5821 else
5823 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5825 if (iarg < 0)
5826 iarg = iarg + lines;
5828 #if 0 /* This code would prevent move-to-window-line from moving point
5829 to a place inside the scroll margins (which would cause the
5830 next redisplay to scroll). I wrote this code, but then concluded
5831 it is probably better not to install it. However, it is here
5832 inside #if 0 so as not to lose it. -- rms. */
5834 /* Don't let it get into the margin at either top or bottom. */
5835 iarg = max (iarg, this_scroll_margin);
5836 iarg = min (iarg, lines - this_scroll_margin - 1);
5837 #endif
5839 arg = make_number (iarg);
5842 /* Skip past a partially visible first line. */
5843 if (w->vscroll)
5844 XSETINT (arg, XINT (arg) + 1);
5846 return Fvertical_motion (arg, window);
5851 /***********************************************************************
5852 Window Configuration
5853 ***********************************************************************/
5855 struct save_window_data
5857 struct vectorlike_header header;
5858 Lisp_Object selected_frame;
5859 Lisp_Object current_window;
5860 Lisp_Object current_buffer;
5861 Lisp_Object minibuf_scroll_window;
5862 Lisp_Object minibuf_selected_window;
5863 Lisp_Object root_window;
5864 Lisp_Object focus_frame;
5865 /* A vector, each of whose elements is a struct saved_window
5866 for one window. */
5867 Lisp_Object saved_windows;
5869 /* All fields above are traced by the GC.
5870 From `frame-cols' down, the fields are ignored by the GC. */
5871 /* We should be able to do without the following two. */
5872 int frame_cols, frame_lines;
5873 /* These two should get eventually replaced by their pixel
5874 counterparts. */
5875 int frame_menu_bar_lines, frame_tool_bar_lines;
5876 int frame_text_width, frame_text_height;
5877 /* These are currently unused. We need them as soon as we convert
5878 to pixels. */
5879 int frame_menu_bar_height, frame_tool_bar_height;
5882 /* This is saved as a Lisp_Vector */
5883 struct saved_window
5885 struct vectorlike_header header;
5887 Lisp_Object window, buffer, start, pointm, mark;
5888 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5889 Lisp_Object left_col, top_line, total_cols, total_lines;
5890 Lisp_Object normal_cols, normal_lines;
5891 Lisp_Object hscroll, min_hscroll;
5892 Lisp_Object parent, prev;
5893 Lisp_Object start_at_line_beg;
5894 Lisp_Object display_table;
5895 Lisp_Object left_margin_cols, right_margin_cols;
5896 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5897 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5898 Lisp_Object combination_limit, window_parameters;
5901 #define SAVED_WINDOW_N(swv,n) \
5902 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5904 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5905 doc: /* Return t if OBJECT is a window-configuration object. */)
5906 (Lisp_Object object)
5908 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5911 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5912 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5913 (Lisp_Object config)
5915 register struct save_window_data *data;
5916 struct Lisp_Vector *saved_windows;
5918 CHECK_WINDOW_CONFIGURATION (config);
5920 data = (struct save_window_data *) XVECTOR (config);
5921 saved_windows = XVECTOR (data->saved_windows);
5922 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5925 /* From Chong's unwind_create_frame_1. */
5926 static void
5927 unwind_change_frame (Lisp_Object val)
5929 inhibit_lisp_code = val;
5932 DEFUN ("set-window-configuration", Fset_window_configuration,
5933 Sset_window_configuration, 1, 1, 0,
5934 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5935 CONFIGURATION must be a value previously returned
5936 by `current-window-configuration' (which see).
5937 If CONFIGURATION was made from a frame that is now deleted,
5938 only frame-independent values can be restored. In this case,
5939 the return value is nil. Otherwise the value is t. */)
5940 (Lisp_Object configuration)
5942 register struct save_window_data *data;
5943 struct Lisp_Vector *saved_windows;
5944 Lisp_Object new_current_buffer;
5945 Lisp_Object frame;
5946 struct frame *f;
5947 ptrdiff_t old_point = -1;
5949 CHECK_WINDOW_CONFIGURATION (configuration);
5951 data = (struct save_window_data *) XVECTOR (configuration);
5952 saved_windows = XVECTOR (data->saved_windows);
5954 new_current_buffer = data->current_buffer;
5955 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5956 new_current_buffer = Qnil;
5957 else
5959 if (XBUFFER (new_current_buffer) == current_buffer)
5960 /* The code further down "preserves point" by saving here PT in
5961 old_point and then setting it later back into PT. When the
5962 current-selected-window and the final-selected-window both show
5963 the current buffer, this suffers from the problem that the
5964 current PT is the window-point of the current-selected-window,
5965 while the final PT is the point of the final-selected-window, so
5966 this copy from one PT to the other would end up moving the
5967 window-point of the final-selected-window to the window-point of
5968 the current-selected-window. So we have to be careful which
5969 point of the current-buffer we copy into old_point. */
5970 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5971 && WINDOWP (selected_window)
5972 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5973 && !EQ (selected_window, data->current_window))
5974 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5975 else
5976 old_point = PT;
5977 else
5978 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5979 point in new_current_buffer as of the last time this buffer was
5980 used. This can be non-deterministic since it can be changed by
5981 things like jit-lock by mere temporary selection of some random
5982 window that happens to show this buffer.
5983 So if possible we want this arbitrary choice of "which point" to
5984 be the one from the to-be-selected-window so as to prevent this
5985 window's cursor from being copied from another window. */
5986 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5987 /* If current_window = selected_window, its point is in BUF_PT. */
5988 && !EQ (selected_window, data->current_window))
5989 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5990 else
5991 old_point = BUF_PT (XBUFFER (new_current_buffer));
5994 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5995 f = XFRAME (frame);
5997 /* If f is a dead frame, don't bother rebuilding its window tree.
5998 However, there is other stuff we should still try to do below. */
5999 if (FRAME_LIVE_P (f))
6001 Lisp_Object window;
6002 Lisp_Object dead_windows = Qnil;
6003 register Lisp_Object tem, par, pers;
6004 register struct window *w;
6005 register struct saved_window *p;
6006 struct window *root_window;
6007 struct window **leaf_windows;
6008 int n_leaf_windows;
6009 ptrdiff_t k;
6010 int i, n;
6011 ptrdiff_t count = SPECPDL_INDEX ();
6012 /* If the frame has been resized since this window configuration was
6013 made, we change the frame to the size specified in the
6014 configuration, restore the configuration, and then resize it
6015 back. We keep track of the prevailing height in these variables. */
6016 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6017 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6018 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6019 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6020 /* int previous_frame_lines = FRAME_LINES (f); */
6021 /* int previous_frame_cols = FRAME_COLS (f); */
6022 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6023 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6025 /* Don't do this within the main loop below: This may call Lisp
6026 code and is thus potentially unsafe while input is blocked. */
6027 for (k = 0; k < saved_windows->header.size; k++)
6029 p = SAVED_WINDOW_N (saved_windows, k);
6030 window = p->window;
6031 w = XWINDOW (window);
6032 if (BUFFERP (w->contents)
6033 && !EQ (w->contents, p->buffer)
6034 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6035 /* If a window we restore gets another buffer, record the
6036 window's old buffer. */
6037 call1 (Qrecord_window_buffer, window);
6040 /* Don't run lisp in the following segment since the frame is in a
6041 completely inconsistent state. See Bug#16207. */
6042 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6043 inhibit_lisp_code = Qt;
6044 /* The mouse highlighting code could get screwed up
6045 if it runs during this. */
6046 block_input ();
6048 if (data->frame_text_width != previous_frame_text_width
6049 || data->frame_text_height != previous_frame_text_height)
6050 change_frame_size (f, data->frame_text_width,
6051 data->frame_text_height, 0, 0, 0, 1);
6053 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6055 #ifdef HAVE_WINDOW_SYSTEM
6056 if (FRAME_WINDOW_P (f))
6057 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6058 make_number (0));
6059 else /* TTY or MSDOS */
6060 #endif
6061 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6062 make_number (0));
6064 #ifdef HAVE_WINDOW_SYSTEM
6065 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6066 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6067 make_number (0));
6068 #endif
6070 /* "Swap out" point from the selected window's buffer
6071 into the window itself. (Normally the pointm of the selected
6072 window holds garbage.) We do this now, before
6073 restoring the window contents, and prevent it from
6074 being done later on when we select a new window. */
6075 if (! NILP (XWINDOW (selected_window)->contents))
6077 w = XWINDOW (selected_window);
6078 set_marker_both (w->pointm,
6079 w->contents,
6080 BUF_PT (XBUFFER (w->contents)),
6081 BUF_PT_BYTE (XBUFFER (w->contents)));
6084 fset_redisplay (f);
6085 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6087 /* Problem: Freeing all matrices and later allocating them again
6088 is a serious redisplay flickering problem. What we would
6089 really like to do is to free only those matrices not reused
6090 below. */
6091 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6092 leaf_windows = alloca (count_windows (root_window)
6093 * sizeof *leaf_windows);
6094 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6096 /* Kludge Alert!
6097 Mark all windows now on frame as "deleted".
6098 Restoring the new configuration "undeletes" any that are in it.
6100 Save their current buffers in their height fields, since we may
6101 need it later, if a buffer saved in the configuration is now
6102 dead. */
6103 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6105 for (k = 0; k < saved_windows->header.size; k++)
6107 p = SAVED_WINDOW_N (saved_windows, k);
6108 window = p->window;
6109 w = XWINDOW (window);
6110 wset_next (w, Qnil);
6112 if (!NILP (p->parent))
6113 wset_parent
6114 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6115 else
6116 wset_parent (w, Qnil);
6118 if (!NILP (p->prev))
6120 wset_prev
6121 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6122 wset_next (XWINDOW (w->prev), p->window);
6124 else
6126 wset_prev (w, Qnil);
6127 if (!NILP (w->parent))
6128 wset_combination (XWINDOW (w->parent),
6129 (XINT (p->total_cols)
6130 != XWINDOW (w->parent)->total_cols),
6131 p->window);
6134 /* If we squirreled away the buffer, restore it now. */
6135 if (BUFFERP (w->combination_limit))
6136 wset_buffer (w, w->combination_limit);
6137 w->pixel_left = XFASTINT (p->pixel_left);
6138 w->pixel_top = XFASTINT (p->pixel_top);
6139 w->pixel_width = XFASTINT (p->pixel_width);
6140 w->pixel_height = XFASTINT (p->pixel_height);
6141 w->left_col = XFASTINT (p->left_col);
6142 w->top_line = XFASTINT (p->top_line);
6143 w->total_cols = XFASTINT (p->total_cols);
6144 w->total_lines = XFASTINT (p->total_lines);
6145 wset_normal_cols (w, p->normal_cols);
6146 wset_normal_lines (w, p->normal_lines);
6147 w->hscroll = XFASTINT (p->hscroll);
6148 w->min_hscroll = XFASTINT (p->min_hscroll);
6149 wset_display_table (w, p->display_table);
6150 w->left_margin_cols = XINT (p->left_margin_cols);
6151 w->right_margin_cols = XINT (p->right_margin_cols);
6152 w->left_fringe_width = XINT (p->left_fringe_width);
6153 w->right_fringe_width = XINT (p->right_fringe_width);
6154 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6155 w->scroll_bar_width = XINT (p->scroll_bar_width);
6156 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6157 wset_dedicated (w, p->dedicated);
6158 wset_combination_limit (w, p->combination_limit);
6159 /* Restore any window parameters that have been saved.
6160 Parameters that have not been saved are left alone. */
6161 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6163 pers = XCAR (tem);
6164 if (CONSP (pers))
6166 if (NILP (XCDR (pers)))
6168 par = Fassq (XCAR (pers), w->window_parameters);
6169 if (CONSP (par) && !NILP (XCDR (par)))
6170 /* Reset a parameter to nil if and only if it
6171 has a non-nil association. Don't make new
6172 associations. */
6173 Fsetcdr (par, Qnil);
6175 else
6176 /* Always restore a non-nil value. */
6177 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6181 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6182 /* If saved buffer is alive, install it. */
6184 wset_buffer (w, p->buffer);
6185 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6186 set_marker_restricted (w->start, p->start, w->contents);
6187 set_marker_restricted (w->pointm, p->pointm,
6188 w->contents);
6189 Fset_marker (BVAR (XBUFFER (w->contents), mark),
6190 p->mark, w->contents);
6192 /* As documented in Fcurrent_window_configuration, don't
6193 restore the location of point in the buffer which was
6194 current when the window configuration was recorded. */
6195 if (!EQ (p->buffer, new_current_buffer)
6196 && XBUFFER (p->buffer) == current_buffer)
6197 Fgoto_char (w->pointm);
6199 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6200 /* Keep window's old buffer; make sure the markers are real. */
6202 /* Set window markers at start of visible range. */
6203 if (XMARKER (w->start)->buffer == 0)
6204 set_marker_restricted_both (w->start, w->contents, 0, 0);
6205 if (XMARKER (w->pointm)->buffer == 0)
6206 set_marker_restricted_both
6207 (w->pointm, w->contents,
6208 BUF_PT (XBUFFER (w->contents)),
6209 BUF_PT_BYTE (XBUFFER (w->contents)));
6210 w->start_at_line_beg = 1;
6212 else if (!NILP (w->start))
6213 /* Leaf window has no live buffer, get one. */
6215 /* Get the buffer via other_buffer_safely in order to
6216 avoid showing an unimportant buffer and, if necessary, to
6217 recreate *scratch* in the course (part of Juanma's bs-show
6218 scenario from March 2011). */
6219 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6220 /* This will set the markers to beginning of visible
6221 range. */
6222 set_marker_restricted_both (w->start, w->contents, 0, 0);
6223 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6224 w->start_at_line_beg = 1;
6225 if (!NILP (w->dedicated))
6226 /* Record this window as dead. */
6227 dead_windows = Fcons (window, dead_windows);
6228 /* Make sure window is no more dedicated. */
6229 wset_dedicated (w, Qnil);
6233 fset_root_window (f, data->root_window);
6234 /* Arrange *not* to restore point in the buffer that was
6235 current when the window configuration was saved. */
6236 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6237 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6238 make_number (old_point),
6239 XWINDOW (data->current_window)->contents);
6241 /* In the following call to `select-window', prevent "swapping out
6242 point" in the old selected window using the buffer that has
6243 been restored into it. We already swapped out that point from
6244 that window's old buffer.
6246 Do not record the buffer here. We do that in a separate call
6247 to select_window below. See also Bug#16207. */
6248 select_window (data->current_window, Qt, 1);
6249 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6250 last_selected_window)
6251 = selected_window;
6253 if (NILP (data->focus_frame)
6254 || (FRAMEP (data->focus_frame)
6255 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6256 Fredirect_frame_focus (frame, data->focus_frame);
6258 /* Set the frame size to the value it had before this function. */
6259 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6260 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6261 change_frame_size (f, previous_frame_text_width,
6262 previous_frame_text_height, 0, 0, 0, 1);
6264 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6266 #ifdef HAVE_WINDOW_SYSTEM
6267 if (FRAME_WINDOW_P (f))
6268 x_set_menu_bar_lines (f,
6269 make_number (previous_frame_menu_bar_lines),
6270 make_number (0));
6271 else /* TTY or MSDOS */
6272 #endif
6273 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6274 make_number (0));
6276 #ifdef HAVE_WINDOW_SYSTEM
6277 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6278 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6279 make_number (0));
6280 #endif
6282 /* Now, free glyph matrices in windows that were not reused. */
6283 for (i = n = 0; i < n_leaf_windows; ++i)
6285 if (NILP (leaf_windows[i]->contents))
6286 free_window_matrices (leaf_windows[i]);
6287 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6288 ++n;
6291 adjust_frame_glyphs (f);
6292 unblock_input ();
6293 unbind_to (count, Qnil);
6295 /* Scan dead buffer windows. */
6296 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6298 window = XCAR (dead_windows);
6299 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6300 delete_deletable_window (window);
6303 /* Record the selected window's buffer here. The window should
6304 already be the selected one from the call above. */
6305 select_window (data->current_window, Qnil, 0);
6307 /* Fselect_window will have made f the selected frame, so we
6308 reselect the proper frame here. Fhandle_switch_frame will change the
6309 selected window too, but that doesn't make the call to
6310 Fselect_window above totally superfluous; it still sets f's
6311 selected window. */
6312 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6313 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6315 run_window_configuration_change_hook (f);
6318 if (!NILP (new_current_buffer))
6320 Fset_buffer (new_current_buffer);
6321 /* If the new current buffer doesn't appear in the selected
6322 window, go to its old point (see bug#12208). */
6323 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6324 Fgoto_char (make_number (old_point));
6327 Vminibuf_scroll_window = data->minibuf_scroll_window;
6328 minibuf_selected_window = data->minibuf_selected_window;
6330 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6333 void
6334 restore_window_configuration (Lisp_Object configuration)
6336 Fset_window_configuration (configuration);
6340 /* If WINDOW is an internal window, recursively delete all child windows
6341 reachable via the next and contents slots of WINDOW. Otherwise setup
6342 WINDOW to not show any buffer. */
6344 void
6345 delete_all_child_windows (Lisp_Object window)
6347 register struct window *w;
6349 w = XWINDOW (window);
6351 if (!NILP (w->next))
6352 /* Delete WINDOW's siblings (we traverse postorderly). */
6353 delete_all_child_windows (w->next);
6355 if (WINDOWP (w->contents))
6357 delete_all_child_windows (w->contents);
6358 wset_combination (w, 0, Qnil);
6360 else if (BUFFERP (w->contents))
6362 unshow_buffer (w);
6363 unchain_marker (XMARKER (w->pointm));
6364 unchain_marker (XMARKER (w->start));
6365 /* Since combination limit makes sense for an internal windows
6366 only, we use this slot to save the buffer for the sake of
6367 possible resurrection in Fset_window_configuration. */
6368 wset_combination_limit (w, w->contents);
6369 wset_buffer (w, Qnil);
6372 Vwindow_list = Qnil;
6375 static int
6376 count_windows (register struct window *window)
6378 register int count = 1;
6379 if (!NILP (window->next))
6380 count += count_windows (XWINDOW (window->next));
6381 if (WINDOWP (window->contents))
6382 count += count_windows (XWINDOW (window->contents));
6383 return count;
6387 /* Fill vector FLAT with leaf windows under W, starting at index I.
6388 Value is last index + 1. */
6389 static int
6390 get_leaf_windows (struct window *w, struct window **flat, int i)
6392 while (w)
6394 if (WINDOWP (w->contents))
6395 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6396 else
6397 flat[i++] = w;
6399 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6402 return i;
6406 /* Return a pointer to the glyph W's physical cursor is on. Value is
6407 null if W's current matrix is invalid, so that no meaningful glyph
6408 can be returned. */
6409 struct glyph *
6410 get_phys_cursor_glyph (struct window *w)
6412 struct glyph_row *row;
6413 struct glyph *glyph;
6414 int hpos = w->phys_cursor.hpos;
6416 if (!(w->phys_cursor.vpos >= 0
6417 && w->phys_cursor.vpos < w->current_matrix->nrows))
6418 return NULL;
6420 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6421 if (!row->enabled_p)
6422 return NULL;
6424 if (w->hscroll)
6426 /* When the window is hscrolled, cursor hpos can legitimately be
6427 out of bounds, but we draw the cursor at the corresponding
6428 window margin in that case. */
6429 if (!row->reversed_p && hpos < 0)
6430 hpos = 0;
6431 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6432 hpos = row->used[TEXT_AREA] - 1;
6435 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6436 glyph = row->glyphs[TEXT_AREA] + hpos;
6437 else
6438 glyph = NULL;
6440 return glyph;
6444 static int
6445 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6447 register struct saved_window *p;
6448 register struct window *w;
6449 register Lisp_Object tem, pers, par;
6451 for (; !NILP (window); window = w->next)
6453 p = SAVED_WINDOW_N (vector, i);
6454 w = XWINDOW (window);
6456 wset_temslot (w, make_number (i)); i++;
6457 p->window = window;
6458 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6459 p->pixel_left = make_number (w->pixel_left);
6460 p->pixel_top = make_number (w->pixel_top);
6461 p->pixel_width = make_number (w->pixel_width);
6462 p->pixel_height = make_number (w->pixel_height);
6463 p->left_col = make_number (w->left_col);
6464 p->top_line = make_number (w->top_line);
6465 p->total_cols = make_number (w->total_cols);
6466 p->total_lines = make_number (w->total_lines);
6467 p->normal_cols = w->normal_cols;
6468 p->normal_lines = w->normal_lines;
6469 XSETFASTINT (p->hscroll, w->hscroll);
6470 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6471 p->display_table = w->display_table;
6472 p->left_margin_cols = make_number (w->left_margin_cols);
6473 p->right_margin_cols = make_number (w->right_margin_cols);
6474 p->left_fringe_width = make_number (w->left_fringe_width);
6475 p->right_fringe_width = make_number (w->right_fringe_width);
6476 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6477 p->scroll_bar_width = make_number (w->scroll_bar_width);
6478 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6479 p->dedicated = w->dedicated;
6480 p->combination_limit = w->combination_limit;
6481 p->window_parameters = Qnil;
6483 if (!NILP (Vwindow_persistent_parameters))
6485 /* Run cycle detection on Vwindow_persistent_parameters. */
6486 Lisp_Object tortoise, hare;
6488 hare = tortoise = Vwindow_persistent_parameters;
6489 while (CONSP (hare))
6491 hare = XCDR (hare);
6492 if (!CONSP (hare))
6493 break;
6495 hare = XCDR (hare);
6496 tortoise = XCDR (tortoise);
6498 if (EQ (hare, tortoise))
6499 /* Reset Vwindow_persistent_parameters to Qnil. */
6501 Vwindow_persistent_parameters = Qnil;
6502 break;
6506 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6507 tem = XCDR (tem))
6509 pers = XCAR (tem);
6510 /* Save values for persistent window parameters. */
6511 if (CONSP (pers) && !NILP (XCDR (pers)))
6513 par = Fassq (XCAR (pers), w->window_parameters);
6514 if (NILP (par))
6515 /* If the window has no value for the parameter,
6516 make one. */
6517 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6518 p->window_parameters);
6519 else
6520 /* If the window has a value for the parameter,
6521 save it. */
6522 p->window_parameters = Fcons (Fcons (XCAR (par),
6523 XCDR (par)),
6524 p->window_parameters);
6529 if (BUFFERP (w->contents))
6531 /* Save w's value of point in the window configuration. If w
6532 is the selected window, then get the value of point from
6533 the buffer; pointm is garbage in the selected window. */
6534 if (EQ (window, selected_window))
6535 p->pointm = build_marker (XBUFFER (w->contents),
6536 BUF_PT (XBUFFER (w->contents)),
6537 BUF_PT_BYTE (XBUFFER (w->contents)));
6538 else
6539 p->pointm = Fcopy_marker (w->pointm, Qnil);
6540 XMARKER (p->pointm)->insertion_type
6541 = !NILP (Vwindow_point_insertion_type);
6543 p->start = Fcopy_marker (w->start, Qnil);
6544 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6546 tem = BVAR (XBUFFER (w->contents), mark);
6547 p->mark = Fcopy_marker (tem, Qnil);
6549 else
6551 p->pointm = Qnil;
6552 p->start = Qnil;
6553 p->mark = Qnil;
6554 p->start_at_line_beg = Qnil;
6557 if (NILP (w->parent))
6558 p->parent = Qnil;
6559 else
6560 p->parent = XWINDOW (w->parent)->temslot;
6562 if (NILP (w->prev))
6563 p->prev = Qnil;
6564 else
6565 p->prev = XWINDOW (w->prev)->temslot;
6567 if (WINDOWP (w->contents))
6568 i = save_window_save (w->contents, vector, i);
6571 return i;
6574 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6575 Scurrent_window_configuration, 0, 1, 0,
6576 doc: /* Return an object representing the current window configuration of FRAME.
6577 If FRAME is nil or omitted, use the selected frame.
6578 This describes the number of windows, their sizes and current buffers,
6579 and for each displayed buffer, where display starts, and the positions of
6580 point and mark. An exception is made for point in the current buffer:
6581 its value is -not- saved.
6582 This also records the currently selected frame, and FRAME's focus
6583 redirection (see `redirect-frame-focus'). The variable
6584 `window-persistent-parameters' specifies which window parameters are
6585 saved by this function. */)
6586 (Lisp_Object frame)
6588 register Lisp_Object tem;
6589 register int n_windows;
6590 register struct save_window_data *data;
6591 register int i;
6592 struct frame *f = decode_live_frame (frame);
6594 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6595 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6596 PVEC_WINDOW_CONFIGURATION);
6598 data->frame_cols = FRAME_COLS (f);
6599 data->frame_lines = FRAME_LINES (f);
6600 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6601 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6602 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6603 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6604 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6605 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6606 data->selected_frame = selected_frame;
6607 data->current_window = FRAME_SELECTED_WINDOW (f);
6608 XSETBUFFER (data->current_buffer, current_buffer);
6609 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6610 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6611 data->root_window = FRAME_ROOT_WINDOW (f);
6612 data->focus_frame = FRAME_FOCUS_FRAME (f);
6613 tem = make_uninit_vector (n_windows);
6614 data->saved_windows = tem;
6615 for (i = 0; i < n_windows; i++)
6616 ASET (tem, i,
6617 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6618 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6619 XSETWINDOW_CONFIGURATION (tem, data);
6620 return (tem);
6623 /* Called after W's margins, fringes or scroll bars was adjusted. */
6625 static void
6626 apply_window_adjustment (struct window *w)
6628 eassert (w);
6629 adjust_window_margins (w);
6630 clear_glyph_matrix (w->current_matrix);
6631 w->window_end_valid = 0;
6632 wset_redisplay (w);
6633 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6637 /***********************************************************************
6638 Marginal Areas
6639 ***********************************************************************/
6641 static struct window *
6642 set_window_margins (struct window *w, Lisp_Object left_width,
6643 Lisp_Object right_width)
6645 int left, right;
6647 /* FIXME: what about margins that are too wide? */
6648 left = (NILP (left_width) ? 0
6649 : (CHECK_NATNUM (left_width), XINT (left_width)));
6650 right = (NILP (right_width) ? 0
6651 : (CHECK_NATNUM (right_width), XINT (right_width)));
6653 if (w->left_margin_cols != left || w->right_margin_cols != right)
6655 w->left_margin_cols = left;
6656 w->right_margin_cols = right;
6657 return w;
6659 return NULL;
6662 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6663 2, 3, 0,
6664 doc: /* Set width of marginal areas of window WINDOW.
6665 WINDOW must be a live window and defaults to the selected one.
6667 Second arg LEFT-WIDTH specifies the number of character cells to
6668 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6669 does the same for the right marginal area. A nil width parameter
6670 means no margin.
6672 Return t if any margin was actually changed and nil otherwise. */)
6673 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6675 struct window *w = set_window_margins (decode_live_window (window),
6676 left_width, right_width);
6677 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6681 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6682 0, 1, 0,
6683 doc: /* Get width of marginal areas of window WINDOW.
6684 WINDOW must be a live window and defaults to the selected one.
6686 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6687 If a marginal area does not exist, its width will be returned
6688 as nil. */)
6689 (Lisp_Object window)
6691 struct window *w = decode_live_window (window);
6692 return Fcons (w->left_margin_cols
6693 ? make_number (w->left_margin_cols) : Qnil,
6694 w->right_margin_cols
6695 ? make_number (w->right_margin_cols) : Qnil);
6700 /***********************************************************************
6701 Fringes
6702 ***********************************************************************/
6704 static struct window *
6705 set_window_fringes (struct window *w, Lisp_Object left_width,
6706 Lisp_Object right_width, Lisp_Object outside_margins)
6708 int left, right, outside = !NILP (outside_margins);
6710 left = (NILP (left_width) ? -1
6711 : (CHECK_NATNUM (left_width), XINT (left_width)));
6712 right = (NILP (right_width) ? -1
6713 : (CHECK_NATNUM (right_width), XINT (right_width)));
6715 /* Do nothing on a tty or if nothing to actually change. */
6716 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6717 && (w->left_fringe_width != left
6718 || w->right_fringe_width != right
6719 || w->fringes_outside_margins != outside))
6721 w->left_fringe_width = left;
6722 w->right_fringe_width = right;
6723 w->fringes_outside_margins = outside;
6724 return w;
6726 return NULL;
6729 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6730 2, 4, 0,
6731 doc: /* Set the fringe widths of window WINDOW.
6732 WINDOW must be a live window and defaults to the selected one.
6734 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6735 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6736 fringe width. If a fringe width arg is nil, that means to use the
6737 frame's default fringe width. Default fringe widths can be set with
6738 the command `set-fringe-style'.
6739 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6740 outside of the display margins. By default, fringes are drawn between
6741 display marginal areas and the text area.
6743 Return t if any fringe was actually changed and nil otherwise. */)
6744 (Lisp_Object window, Lisp_Object left_width,
6745 Lisp_Object right_width, Lisp_Object outside_margins)
6747 struct window *w
6748 = set_window_fringes (decode_live_window (window),
6749 left_width, right_width, outside_margins);
6750 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6754 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6755 0, 1, 0,
6756 doc: /* Get width of fringes of window WINDOW.
6757 WINDOW must be a live window and defaults to the selected one.
6759 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6760 (Lisp_Object window)
6762 struct window *w = decode_live_window (window);
6764 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6765 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6766 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6771 /***********************************************************************
6772 Scroll bars
6773 ***********************************************************************/
6775 static struct window *
6776 set_window_scroll_bars (struct window *w, Lisp_Object width,
6777 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6779 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6781 if (iwidth == 0)
6782 vertical_type = Qnil;
6784 if (!(NILP (vertical_type)
6785 || EQ (vertical_type, Qleft)
6786 || EQ (vertical_type, Qright)
6787 || EQ (vertical_type, Qt)))
6788 error ("Invalid type of vertical scroll bar");
6790 if (w->scroll_bar_width != iwidth
6791 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6793 w->scroll_bar_width = iwidth;
6794 wset_vertical_scroll_bar_type (w, vertical_type);
6795 return w;
6797 return NULL;
6800 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6801 Sset_window_scroll_bars, 2, 4, 0,
6802 doc: /* Set width and type of scroll bars of window WINDOW.
6803 WINDOW must be a live window and defaults to the selected one.
6805 Second parameter WIDTH specifies the pixel width for the scroll bar.
6806 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6807 bar: left, right, or nil.
6808 If WIDTH is nil, use the frame's scroll-bar width.
6809 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6810 Fourth parameter HORIZONTAL-TYPE is currently unused.
6812 Return t if scroll bars were actually changed and nil otherwise. */)
6813 (Lisp_Object window, Lisp_Object width,
6814 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6816 struct window *w
6817 = set_window_scroll_bars (decode_live_window (window),
6818 width, vertical_type, horizontal_type);
6819 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6823 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6824 0, 1, 0,
6825 doc: /* Get width and type of scroll bars of window WINDOW.
6826 WINDOW must be a live window and defaults to the selected one.
6828 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6829 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6830 value. */)
6831 (Lisp_Object window)
6833 struct window *w = decode_live_window (window);
6835 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6836 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6837 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6838 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6839 w->vertical_scroll_bar_type, Qnil);
6844 /***********************************************************************
6845 Smooth scrolling
6846 ***********************************************************************/
6848 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6849 doc: /* Return the amount by which WINDOW is scrolled vertically.
6850 If WINDOW is omitted or nil, it defaults to the selected window.
6851 Normally, value is a multiple of the canonical character height of WINDOW;
6852 optional second arg PIXELS-P means value is measured in pixels. */)
6853 (Lisp_Object window, Lisp_Object pixels_p)
6855 Lisp_Object result;
6856 struct window *w = decode_live_window (window);
6857 struct frame *f = XFRAME (w->frame);
6859 if (FRAME_WINDOW_P (f))
6860 result = (NILP (pixels_p)
6861 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6862 : make_number (-w->vscroll));
6863 else
6864 result = make_number (0);
6865 return result;
6869 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6870 2, 3, 0,
6871 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6872 WINDOW nil means use the selected window. Normally, VSCROLL is a
6873 non-negative multiple of the canonical character height of WINDOW;
6874 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6875 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6876 corresponds to an integral number of pixels. The return value is the
6877 result of this rounding.
6878 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6879 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6881 struct window *w = decode_live_window (window);
6882 struct frame *f = XFRAME (w->frame);
6884 CHECK_NUMBER_OR_FLOAT (vscroll);
6886 if (FRAME_WINDOW_P (f))
6888 int old_dy = w->vscroll;
6890 w->vscroll = - (NILP (pixels_p)
6891 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6892 : XFLOATINT (vscroll));
6893 w->vscroll = min (w->vscroll, 0);
6895 if (w->vscroll != old_dy)
6897 /* Adjust glyph matrix of the frame if the virtual display
6898 area becomes larger than before. */
6899 if (w->vscroll < 0 && w->vscroll < old_dy)
6900 adjust_frame_glyphs (f);
6902 /* Prevent redisplay shortcuts. */
6903 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6907 return Fwindow_vscroll (window, pixels_p);
6911 /* Call FN for all leaf windows on frame F. FN is called with the
6912 first argument being a pointer to the leaf window, and with
6913 additional argument USER_DATA. Stops when FN returns 0. */
6915 static void
6916 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6917 void *user_data)
6919 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6920 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6921 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6925 /* Helper function for foreach_window. Call FN for all leaf windows
6926 reachable from W. FN is called with the first argument being a
6927 pointer to the leaf window, and with additional argument USER_DATA.
6928 Stop when FN returns 0. Value is 0 if stopped by FN. */
6930 static int
6931 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6933 int cont;
6935 for (cont = 1; w && cont;)
6937 if (WINDOWP (w->contents))
6938 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6939 else
6940 cont = fn (w, user_data);
6942 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6945 return cont;
6948 /***********************************************************************
6949 Initialization
6950 ***********************************************************************/
6952 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6953 describe the same state of affairs. This is used by Fequal.
6955 IGNORE_POSITIONS means ignore non-matching scroll positions
6956 and the like.
6958 This ignores a couple of things like the dedication status of
6959 window, combination_limit and the like. This might have to be
6960 fixed. */
6962 bool
6963 compare_window_configurations (Lisp_Object configuration1,
6964 Lisp_Object configuration2,
6965 bool ignore_positions)
6967 register struct save_window_data *d1, *d2;
6968 struct Lisp_Vector *sws1, *sws2;
6969 ptrdiff_t i;
6971 CHECK_WINDOW_CONFIGURATION (configuration1);
6972 CHECK_WINDOW_CONFIGURATION (configuration2);
6974 d1 = (struct save_window_data *) XVECTOR (configuration1);
6975 d2 = (struct save_window_data *) XVECTOR (configuration2);
6976 sws1 = XVECTOR (d1->saved_windows);
6977 sws2 = XVECTOR (d2->saved_windows);
6979 /* Frame settings must match. */
6980 if (d1->frame_cols != d2->frame_cols
6981 || d1->frame_lines != d2->frame_lines
6982 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6983 || !EQ (d1->selected_frame, d2->selected_frame)
6984 || !EQ (d1->current_buffer, d2->current_buffer)
6985 || (!ignore_positions
6986 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6987 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6988 || !EQ (d1->focus_frame, d2->focus_frame)
6989 /* Verify that the two configurations have the same number of windows. */
6990 || sws1->header.size != sws2->header.size)
6991 return 0;
6993 for (i = 0; i < sws1->header.size; i++)
6995 struct saved_window *sw1, *sw2;
6997 sw1 = SAVED_WINDOW_N (sws1, i);
6998 sw2 = SAVED_WINDOW_N (sws2, i);
7000 if (
7001 /* The "current" windows in the two configurations must
7002 correspond to each other. */
7003 EQ (d1->current_window, sw1->window)
7004 != EQ (d2->current_window, sw2->window)
7005 /* Windows' buffers must match. */
7006 || !EQ (sw1->buffer, sw2->buffer)
7007 || !EQ (sw1->pixel_left, sw2->pixel_left)
7008 || !EQ (sw1->pixel_top, sw2->pixel_top)
7009 || !EQ (sw1->pixel_height, sw2->pixel_height)
7010 || !EQ (sw1->pixel_width, sw2->pixel_width)
7011 || !EQ (sw1->left_col, sw2->left_col)
7012 || !EQ (sw1->top_line, sw2->top_line)
7013 || !EQ (sw1->total_cols, sw2->total_cols)
7014 || !EQ (sw1->total_lines, sw2->total_lines)
7015 || !EQ (sw1->display_table, sw2->display_table)
7016 /* The next two disjuncts check the window structure for
7017 equality. */
7018 || !EQ (sw1->parent, sw2->parent)
7019 || !EQ (sw1->prev, sw2->prev)
7020 || (!ignore_positions
7021 && (!EQ (sw1->hscroll, sw2->hscroll)
7022 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7023 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7024 || NILP (Fequal (sw1->start, sw2->start))
7025 || NILP (Fequal (sw1->pointm, sw2->pointm))
7026 || NILP (Fequal (sw1->mark, sw2->mark))))
7027 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7028 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7029 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7030 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7031 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7032 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7033 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7034 return 0;
7037 return 1;
7040 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7041 Scompare_window_configurations, 2, 2, 0,
7042 doc: /* Compare two window configurations as regards the structure of windows.
7043 This function ignores details such as the values of point and mark
7044 and scrolling positions. */)
7045 (Lisp_Object x, Lisp_Object y)
7047 if (compare_window_configurations (x, y, 1))
7048 return Qt;
7049 return Qnil;
7052 void
7053 init_window_once (void)
7055 struct frame *f = make_initial_frame ();
7056 XSETFRAME (selected_frame, f);
7057 Vterminal_frame = selected_frame;
7058 minibuf_window = f->minibuffer_window;
7059 selected_window = f->selected_window;
7061 window_initialized = 1;
7064 void
7065 init_window (void)
7067 Vwindow_list = Qnil;
7070 void
7071 syms_of_window (void)
7073 DEFSYM (Qscroll_up, "scroll-up");
7074 DEFSYM (Qscroll_down, "scroll-down");
7075 DEFSYM (Qscroll_command, "scroll-command");
7077 Fput (Qscroll_up, Qscroll_command, Qt);
7078 Fput (Qscroll_down, Qscroll_command, Qt);
7080 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7081 DEFSYM (Qwindowp, "windowp");
7082 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7083 DEFSYM (Qwindow_live_p, "window-live-p");
7084 DEFSYM (Qwindow_valid_p, "window-valid-p");
7085 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7086 DEFSYM (Qdelete_window, "delete-window");
7087 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7088 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7089 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7090 DEFSYM (Qsafe, "safe");
7091 DEFSYM (Qdisplay_buffer, "display-buffer");
7092 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7093 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7094 DEFSYM (Qget_mru_window, "get-mru-window");
7095 DEFSYM (Qwindow_size, "window-size");
7096 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7097 DEFSYM (Qabove, "above");
7098 DEFSYM (Qbelow, "below");
7099 DEFSYM (Qclone_of, "clone-of");
7101 staticpro (&Vwindow_list);
7103 minibuf_selected_window = Qnil;
7104 staticpro (&minibuf_selected_window);
7106 window_scroll_pixel_based_preserve_x = -1;
7107 window_scroll_pixel_based_preserve_y = -1;
7108 window_scroll_preserve_hpos = -1;
7109 window_scroll_preserve_vpos = -1;
7111 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7112 doc: /* Non-nil means call as function to display a help buffer.
7113 The function is called with one argument, the buffer to be displayed.
7114 Used by `with-output-to-temp-buffer'.
7115 If this function is used, then it must do the entire job of showing
7116 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7117 Vtemp_buffer_show_function = Qnil;
7119 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7120 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7121 Vminibuf_scroll_window = Qnil;
7123 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7124 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7125 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7126 is displayed in the `mode-line' face. */);
7127 mode_line_in_non_selected_windows = 1;
7129 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7130 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7131 Vother_window_scroll_buffer = Qnil;
7133 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7134 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7135 auto_window_vscroll_p = 1;
7137 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7138 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7139 next_screen_context_lines = 2;
7141 DEFVAR_LISP ("scroll-preserve-screen-position",
7142 Vscroll_preserve_screen_position,
7143 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7144 A value of nil means point does not keep its screen position except
7145 at the scroll margin or window boundary respectively.
7146 A value of t means point keeps its screen position if the scroll
7147 command moved it vertically out of the window, e.g. when scrolling
7148 by full screens.
7149 Any other value means point always keeps its screen position.
7150 Scroll commands should have the `scroll-command' property
7151 on their symbols to be controlled by this variable. */);
7152 Vscroll_preserve_screen_position = Qnil;
7154 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7155 doc: /* Type of marker to use for `window-point'. */);
7156 Vwindow_point_insertion_type = Qnil;
7158 DEFVAR_LISP ("window-configuration-change-hook",
7159 Vwindow_configuration_change_hook,
7160 doc: /* Functions to call when window configuration changes.
7161 The buffer-local part is run once per window, with the relevant window
7162 selected; while the global part is run only once for the modified frame,
7163 with the relevant frame selected. */);
7164 Vwindow_configuration_change_hook = Qnil;
7166 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7167 doc: /* Non-nil means `recenter' redraws entire frame.
7168 If this option is non-nil, then the `recenter' command with a nil
7169 argument will redraw the entire frame; the special value `tty' causes
7170 the frame to be redrawn only if it is a tty frame. */);
7171 Vrecenter_redisplay = Qtty;
7173 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7174 doc: /* If t, resize window combinations proportionally.
7175 If this variable is nil, splitting a window gets the entire screen space
7176 for displaying the new window from the window to split. Deleting and
7177 resizing a window preferably resizes one adjacent window only.
7179 If this variable is t, splitting a window tries to get the space
7180 proportionally from all windows in the same combination. This also
7181 allows to split a window that is otherwise too small or of fixed size.
7182 Resizing and deleting a window proportionally resize all windows in the
7183 same combination.
7185 Other values are reserved for future use.
7187 This variable takes no effect if the variable `window-combination-limit' is
7188 non-nil. */);
7189 Vwindow_combination_resize = Qnil;
7191 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7192 doc: /* If non-nil, splitting a window makes a new parent window.
7193 The following values are recognized:
7195 nil means splitting a window will create a new parent window only if the
7196 window has no parent window or the window shall become part of a
7197 combination orthogonal to the one it is part of.
7199 `window-size' means that splitting a window for displaying a buffer
7200 makes a new parent window provided `display-buffer' is supposed to
7201 explicitly set the window's size due to the presence of a
7202 `window-height' or `window-width' entry in the alist used by
7203 `display-buffer'. Otherwise, this value is handled like nil.
7205 `temp-buffer' means that splitting a window for displaying a temporary
7206 buffer always makes a new parent window. Otherwise, this value is
7207 handled like nil.
7209 `display-buffer' means that splitting a window for displaying a buffer
7210 always makes a new parent window. Since temporary buffers are
7211 displayed by the function `display-buffer', this value is stronger
7212 than `temp-buffer'. Splitting a window for other purpose makes a
7213 new parent window only if needed.
7215 t means that splitting a window always creates a new parent window. If
7216 all splits behave this way, each frame's window tree is a binary
7217 tree and every window but the frame's root window has exactly one
7218 sibling.
7220 Other values are reserved for future use. */);
7221 Vwindow_combination_limit = Qwindow_size;
7223 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7224 doc: /* Alist of persistent window parameters.
7225 This alist specifies which window parameters shall get saved by
7226 `current-window-configuration' and `window-state-get' and subsequently
7227 restored to their previous values by `set-window-configuration' and
7228 `window-state-put'.
7230 The car of each entry of this alist is the symbol specifying the
7231 parameter. The cdr is one of the following:
7233 nil means the parameter is neither saved by `window-state-get' nor by
7234 `current-window-configuration'.
7236 t means the parameter is saved by `current-window-configuration' and,
7237 provided its WRITABLE argument is nil, by `window-state-get'.
7239 The symbol `writable' means the parameter is saved unconditionally by
7240 both `current-window-configuration' and `window-state-get'. Do not use
7241 this value for parameters without read syntax (like windows or frames).
7243 Parameters not saved by `current-window-configuration' or
7244 `window-state-get' are left alone by `set-window-configuration'
7245 respectively are not installed by `window-state-put'. */);
7246 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7248 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7249 doc: /* Non-nil means resizing windows works pixelwise.
7250 Functions currently affected by this option are `split-window',
7251 `maximize-window', `minimize-window', `fit-window-to-buffer' and
7252 `fit-frame-to-buffer' and all functions symmetrically resizing a
7253 parent window.
7255 Note that when a frame's pixel size is not a multiple of the
7256 frame's character size, at least one window may get resized
7257 pixelwise even if this option is nil. */);
7258 window_resize_pixelwise = 0;
7260 defsubr (&Sselected_window);
7261 defsubr (&Sminibuffer_window);
7262 defsubr (&Swindow_minibuffer_p);
7263 defsubr (&Swindowp);
7264 defsubr (&Swindow_valid_p);
7265 defsubr (&Swindow_live_p);
7266 defsubr (&Swindow_frame);
7267 defsubr (&Sframe_root_window);
7268 defsubr (&Sframe_first_window);
7269 defsubr (&Sframe_selected_window);
7270 defsubr (&Sset_frame_selected_window);
7271 defsubr (&Spos_visible_in_window_p);
7272 defsubr (&Swindow_line_height);
7273 defsubr (&Swindow_buffer);
7274 defsubr (&Swindow_parent);
7275 defsubr (&Swindow_top_child);
7276 defsubr (&Swindow_left_child);
7277 defsubr (&Swindow_next_sibling);
7278 defsubr (&Swindow_prev_sibling);
7279 defsubr (&Swindow_combination_limit);
7280 defsubr (&Sset_window_combination_limit);
7281 defsubr (&Swindow_use_time);
7282 defsubr (&Swindow_pixel_width);
7283 defsubr (&Swindow_pixel_height);
7284 defsubr (&Swindow_total_width);
7285 defsubr (&Swindow_total_height);
7286 defsubr (&Swindow_normal_size);
7287 defsubr (&Swindow_new_pixel);
7288 defsubr (&Swindow_new_total);
7289 defsubr (&Swindow_new_normal);
7290 defsubr (&Swindow_pixel_left);
7291 defsubr (&Swindow_pixel_top);
7292 defsubr (&Swindow_left_column);
7293 defsubr (&Swindow_top_line);
7294 defsubr (&Sset_window_new_pixel);
7295 defsubr (&Sset_window_new_total);
7296 defsubr (&Sset_window_new_normal);
7297 defsubr (&Swindow_resize_apply);
7298 defsubr (&Swindow_resize_apply_total);
7299 defsubr (&Swindow_body_height);
7300 defsubr (&Swindow_body_width);
7301 defsubr (&Swindow_hscroll);
7302 defsubr (&Sset_window_hscroll);
7303 defsubr (&Swindow_redisplay_end_trigger);
7304 defsubr (&Sset_window_redisplay_end_trigger);
7305 defsubr (&Swindow_edges);
7306 defsubr (&Swindow_pixel_edges);
7307 defsubr (&Swindow_absolute_pixel_edges);
7308 defsubr (&Swindow_mode_line_height);
7309 defsubr (&Swindow_header_line_height);
7310 defsubr (&Swindow_right_divider_width);
7311 defsubr (&Swindow_bottom_divider_width);
7312 defsubr (&Swindow_inside_edges);
7313 defsubr (&Swindow_inside_pixel_edges);
7314 defsubr (&Swindow_inside_absolute_pixel_edges);
7315 defsubr (&Scoordinates_in_window_p);
7316 defsubr (&Swindow_at);
7317 defsubr (&Swindow_point);
7318 defsubr (&Swindow_start);
7319 defsubr (&Swindow_end);
7320 defsubr (&Sset_window_point);
7321 defsubr (&Sset_window_start);
7322 defsubr (&Swindow_dedicated_p);
7323 defsubr (&Sset_window_dedicated_p);
7324 defsubr (&Swindow_display_table);
7325 defsubr (&Sset_window_display_table);
7326 defsubr (&Snext_window);
7327 defsubr (&Sprevious_window);
7328 defsubr (&Sget_buffer_window);
7329 defsubr (&Sdelete_other_windows_internal);
7330 defsubr (&Sdelete_window_internal);
7331 defsubr (&Sresize_mini_window_internal);
7332 defsubr (&Sset_window_buffer);
7333 defsubr (&Srun_window_configuration_change_hook);
7334 defsubr (&Srun_window_scroll_functions);
7335 defsubr (&Sselect_window);
7336 defsubr (&Sforce_window_update);
7337 defsubr (&Ssplit_window_internal);
7338 defsubr (&Sscroll_up);
7339 defsubr (&Sscroll_down);
7340 defsubr (&Sscroll_left);
7341 defsubr (&Sscroll_right);
7342 defsubr (&Sother_window_for_scrolling);
7343 defsubr (&Sscroll_other_window);
7344 defsubr (&Sminibuffer_selected_window);
7345 defsubr (&Srecenter);
7346 defsubr (&Swindow_text_width);
7347 defsubr (&Swindow_text_height);
7348 defsubr (&Smove_to_window_line);
7349 defsubr (&Swindow_configuration_p);
7350 defsubr (&Swindow_configuration_frame);
7351 defsubr (&Sset_window_configuration);
7352 defsubr (&Scurrent_window_configuration);
7353 defsubr (&Sset_window_margins);
7354 defsubr (&Swindow_margins);
7355 defsubr (&Sset_window_fringes);
7356 defsubr (&Swindow_fringes);
7357 defsubr (&Sset_window_scroll_bars);
7358 defsubr (&Swindow_scroll_bars);
7359 defsubr (&Swindow_vscroll);
7360 defsubr (&Sset_window_vscroll);
7361 defsubr (&Scompare_window_configurations);
7362 defsubr (&Swindow_list);
7363 defsubr (&Swindow_list_1);
7364 defsubr (&Swindow_prev_buffers);
7365 defsubr (&Sset_window_prev_buffers);
7366 defsubr (&Swindow_next_buffers);
7367 defsubr (&Sset_window_next_buffers);
7368 defsubr (&Swindow_parameters);
7369 defsubr (&Swindow_parameter);
7370 defsubr (&Sset_window_parameter);
7373 void
7374 keys_of_window (void)
7376 initial_define_key (control_x_map, '<', "scroll-left");
7377 initial_define_key (control_x_map, '>', "scroll-right");
7379 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7380 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7381 initial_define_key (meta_map, 'v', "scroll-down-command");