Some remember doc
[emacs.git] / src / window.c
blob0ec1aa963240e0b70bafc45fd9ccdda4b62a329f
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2014 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <stdio.h>
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
43 #ifdef MSDOS
44 #include "msdos.h"
45 #endif
47 Lisp_Object Qwindowp, Qwindow_live_p;
48 static Lisp_Object Qwindow_valid_p;
49 static Lisp_Object Qwindow_configuration_p;
50 static Lisp_Object Qrecord_window_buffer;
51 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
52 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
53 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
54 static Lisp_Object Qwindow_pixel_to_total;
55 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
56 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
57 static Lisp_Object Qfloor, Qceiling;
59 static int displayed_window_lines (struct window *);
60 static int count_windows (struct window *);
61 static int get_leaf_windows (struct window *, struct window **, int);
62 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
63 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
64 static void window_scroll_line_based (Lisp_Object, int, bool, int);
65 static int add_window_to_list (struct window *, void *);
66 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
67 Lisp_Object, int);
68 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
69 Lisp_Object *);
70 static void foreach_window (struct frame *,
71 int (* fn) (struct window *, void *),
72 void *);
73 static int foreach_window_1 (struct window *,
74 int (* fn) (struct window *, void *),
75 void *);
76 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
77 static int window_resize_check (struct window *, bool);
78 static void window_resize_apply (struct window *, bool);
79 static void window_resize_apply_total (struct window *, bool);
80 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
81 static void select_window_1 (Lisp_Object, bool);
83 static struct window *set_window_fringes (struct window *, Lisp_Object,
84 Lisp_Object, Lisp_Object);
85 static struct window *set_window_margins (struct window *, Lisp_Object,
86 Lisp_Object);
87 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
88 Lisp_Object, Lisp_Object);
89 static void apply_window_adjustment (struct window *);
91 /* This is the window in which the terminal's cursor should
92 be left when nothing is being done with it. This must
93 always be a leaf window, and its buffer is selected by
94 the top level editing loop at the end of each command.
96 This value is always the same as
97 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109 Lisp_Object minibuf_window;
111 /* Non-nil means it is the window whose mode line should be
112 shown as the selected window when the minibuffer is selected. */
113 Lisp_Object minibuf_selected_window;
115 /* Hook run at end of temp_output_buffer_show. */
116 static Lisp_Object Qtemp_buffer_show_hook;
118 /* Incremented for each window created. */
119 static int sequence_number;
121 /* Nonzero after init_window_once has finished. */
122 static int window_initialized;
124 /* Hook to run when window config changes. */
125 static Lisp_Object Qwindow_configuration_change_hook;
127 /* Used by the function window_scroll_pixel_based */
128 static int window_scroll_pixel_based_preserve_x;
129 static int window_scroll_pixel_based_preserve_y;
131 /* Same for window_scroll_line_based. */
132 static EMACS_INT window_scroll_preserve_hpos;
133 static EMACS_INT window_scroll_preserve_vpos;
135 static void
136 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
138 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
141 /* These setters are used only in this file, so they can be private. */
142 static void
143 wset_combination_limit (struct window *w, Lisp_Object val)
145 w->combination_limit = val;
147 static void
148 wset_dedicated (struct window *w, Lisp_Object val)
150 w->dedicated = val;
152 static void
153 wset_display_table (struct window *w, Lisp_Object val)
155 w->display_table = val;
157 static void
158 wset_new_normal (struct window *w, Lisp_Object val)
160 w->new_normal = val;
162 static void
163 wset_new_total (struct window *w, Lisp_Object val)
165 w->new_total = val;
167 static void
168 wset_normal_cols (struct window *w, Lisp_Object val)
170 w->normal_cols = val;
172 static void
173 wset_normal_lines (struct window *w, Lisp_Object val)
175 w->normal_lines = val;
177 static void
178 wset_parent (struct window *w, Lisp_Object val)
180 w->parent = val;
182 static void
183 wset_pointm (struct window *w, Lisp_Object val)
185 w->pointm = val;
187 static void
188 wset_start (struct window *w, Lisp_Object val)
190 w->start = val;
192 static void
193 wset_temslot (struct window *w, Lisp_Object val)
195 w->temslot = val;
197 static void
198 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
200 w->vertical_scroll_bar_type = val;
202 static void
203 wset_window_parameters (struct window *w, Lisp_Object val)
205 w->window_parameters = val;
207 static void
208 wset_combination (struct window *w, bool horflag, Lisp_Object val)
210 /* Since leaf windows never becomes non-leaf, there should
211 be no buffer and markers in start and pointm fields of W. */
212 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
213 w->contents = val;
214 /* When an internal window is deleted and VAL is nil, HORFLAG
215 is meaningless. */
216 if (!NILP (val))
217 w->horizontal = horflag;
220 /* Nonzero if leaf window W doesn't reflect the actual state
221 of displayed buffer due to its text or overlays change. */
223 bool
224 window_outdated (struct window *w)
226 struct buffer *b = XBUFFER (w->contents);
227 return (w->last_modified < BUF_MODIFF (b)
228 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
231 struct window *
232 decode_live_window (register Lisp_Object window)
234 if (NILP (window))
235 return XWINDOW (selected_window);
237 CHECK_LIVE_WINDOW (window);
238 return XWINDOW (window);
241 struct window *
242 decode_any_window (register Lisp_Object window)
244 struct window *w;
246 if (NILP (window))
247 return XWINDOW (selected_window);
249 CHECK_WINDOW (window);
250 w = XWINDOW (window);
251 return w;
254 static struct window *
255 decode_valid_window (register Lisp_Object window)
257 struct window *w;
259 if (NILP (window))
260 return XWINDOW (selected_window);
262 CHECK_VALID_WINDOW (window);
263 w = XWINDOW (window);
264 return w;
267 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
268 cease its buffer, and 1 means that W is about to set up the new one. */
270 static void
271 adjust_window_count (struct window *w, int arg)
273 eassert (eabs (arg) == 1);
274 if (BUFFERP (w->contents))
276 struct buffer *b = XBUFFER (w->contents);
278 if (b->base_buffer)
279 b = b->base_buffer;
280 b->window_count += arg;
281 eassert (b->window_count >= 0);
282 /* These should be recalculated by redisplay code. */
283 w->window_end_valid = 0;
284 w->base_line_pos = 0;
288 /* Set W's buffer slot to VAL and recompute number
289 of windows showing VAL if it is a buffer. */
291 void
292 wset_buffer (struct window *w, Lisp_Object val)
294 adjust_window_count (w, -1);
295 if (BUFFERP (val))
296 /* Make sure that we do not assign the buffer
297 to an internal window. */
298 eassert (MARKERP (w->start) && MARKERP (w->pointm));
299 w->contents = val;
300 adjust_window_count (w, 1);
303 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
304 doc: /* Return t if OBJECT is a window and nil otherwise. */)
305 (Lisp_Object object)
307 return WINDOWP (object) ? Qt : Qnil;
310 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
311 doc: /* Return t if OBJECT is a valid window and nil otherwise.
312 A valid window is either a window that displays a buffer or an internal
313 window. Deleted windows are not live. */)
314 (Lisp_Object object)
316 return WINDOW_VALID_P (object) ? Qt : Qnil;
319 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
320 doc: /* Return t if OBJECT is a live window and nil otherwise.
321 A live window is a window that displays a buffer.
322 Internal windows and deleted windows are not live. */)
323 (Lisp_Object object)
325 return WINDOW_LIVE_P (object) ? Qt : Qnil;
328 /* Frames and windows. */
329 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
330 doc: /* Return the frame that window WINDOW is on.
331 WINDOW must be a valid window and defaults to the selected one. */)
332 (Lisp_Object window)
334 return decode_valid_window (window)->frame;
337 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
338 doc: /* Return the root window of FRAME-OR-WINDOW.
339 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
340 With a frame argument, return that frame's root window.
341 With a window argument, return the root window of that window's frame. */)
342 (Lisp_Object frame_or_window)
344 Lisp_Object window;
346 if (NILP (frame_or_window))
347 window = SELECTED_FRAME ()->root_window;
348 else if (WINDOW_VALID_P (frame_or_window))
349 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
350 else
352 CHECK_LIVE_FRAME (frame_or_window);
353 window = XFRAME (frame_or_window)->root_window;
356 return window;
359 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
360 doc: /* Return the minibuffer window for frame FRAME.
361 If FRAME is omitted or nil, it defaults to the selected frame. */)
362 (Lisp_Object frame)
364 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
367 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
368 Swindow_minibuffer_p, 0, 1, 0,
369 doc: /* Return non-nil if WINDOW is a minibuffer window.
370 WINDOW must be a valid window and defaults to the selected one. */)
371 (Lisp_Object window)
373 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
376 /* Don't move this to window.el - this must be a safe routine. */
377 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
378 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
379 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
380 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
381 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
382 the first window of that frame. */)
383 (Lisp_Object frame_or_window)
385 Lisp_Object window;
387 if (NILP (frame_or_window))
388 window = SELECTED_FRAME ()->root_window;
389 else if (WINDOW_VALID_P (frame_or_window))
390 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
391 else
393 CHECK_LIVE_FRAME (frame_or_window);
394 window = XFRAME (frame_or_window)->root_window;
397 while (WINDOWP (XWINDOW (window)->contents))
398 window = XWINDOW (window)->contents;
400 return window;
403 DEFUN ("frame-selected-window", Fframe_selected_window,
404 Sframe_selected_window, 0, 1, 0,
405 doc: /* Return the selected window of FRAME-OR-WINDOW.
406 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
407 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
408 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
409 return the selected window of that frame. */)
410 (Lisp_Object frame_or_window)
412 Lisp_Object window;
414 if (NILP (frame_or_window))
415 window = SELECTED_FRAME ()->selected_window;
416 else if (WINDOW_VALID_P (frame_or_window))
417 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
418 else
420 CHECK_LIVE_FRAME (frame_or_window);
421 window = XFRAME (frame_or_window)->selected_window;
424 return window;
427 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
428 Sset_frame_selected_window, 2, 3, 0,
429 doc: /* Set selected window of FRAME to WINDOW.
430 FRAME must be a live frame and defaults to the selected one. If FRAME
431 is the selected frame, this makes WINDOW the selected window. Optional
432 argument NORECORD non-nil means to neither change the order of recently
433 selected windows nor the buffer list. WINDOW must denote a live window.
434 Return WINDOW. */)
435 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
437 if (NILP (frame))
438 frame = selected_frame;
440 CHECK_LIVE_FRAME (frame);
441 CHECK_LIVE_WINDOW (window);
443 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
444 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
446 if (EQ (frame, selected_frame))
447 return Fselect_window (window, norecord);
448 else
450 fset_selected_window (XFRAME (frame), window);
451 return window;
455 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
456 doc: /* Return the selected window.
457 The selected window is the window in which the standard cursor for
458 selected windows appears and to which many commands apply. */)
459 (void)
461 return selected_window;
464 int window_select_count;
466 /* If select_window is called with inhibit_point_swap non-zero it will
467 not store point of the old selected window's buffer back into that
468 window's pointm slot. This is needed by Fset_window_configuration to
469 avoid that the display routine is called with selected_window set to
470 Qnil causing a subsequent crash. */
471 static Lisp_Object
472 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
474 register struct window *w;
475 struct frame *sf;
477 CHECK_LIVE_WINDOW (window);
479 w = XWINDOW (window);
481 /* Make the selected window's buffer current. */
482 Fset_buffer (w->contents);
484 if (EQ (window, selected_window) && !inhibit_point_swap)
485 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
486 way to call record_buffer from Elisp, so it's important that we call
487 record_buffer before returning here. */
488 goto record_and_return;
490 if (NILP (norecord))
491 { /* Mark the window for redisplay since the selected-window has
492 a different mode-line. */
493 wset_redisplay (XWINDOW (selected_window));
494 wset_redisplay (w);
496 else
497 redisplay_other_windows ();
499 sf = SELECTED_FRAME ();
500 if (XFRAME (WINDOW_FRAME (w)) != sf)
502 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
503 /* Use this rather than Fhandle_switch_frame
504 so that FRAME_FOCUS_FRAME is moved appropriately as we
505 move around in the state where a minibuffer in a separate
506 frame is active. */
507 Fselect_frame (WINDOW_FRAME (w), norecord);
508 /* Fselect_frame called us back so we've done all the work already. */
509 eassert (EQ (window, selected_window));
510 return window;
512 else
513 fset_selected_window (sf, window);
515 select_window_1 (window, inhibit_point_swap);
516 bset_last_selected_window (XBUFFER (w->contents), window);
518 record_and_return:
519 /* record_buffer can run QUIT, so make sure it is run only after we have
520 re-established the invariant between selected_window and selected_frame,
521 otherwise the temporary broken invariant might "escape" (bug#14161). */
522 if (NILP (norecord))
524 w->use_time = ++window_select_count;
525 record_buffer (w->contents);
528 return window;
531 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
532 (not even for redisplay's benefit), and assume that the window's frame is
533 already selected. */
534 static void
535 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
537 /* Store the old selected window's buffer's point in pointm of the old
538 selected window. It belongs to that window, and when the window is
539 not selected, must be in the window. */
540 if (!inhibit_point_swap)
542 struct window *ow = XWINDOW (selected_window);
543 if (BUFFERP (ow->contents))
544 set_marker_both (ow->pointm, ow->contents,
545 BUF_PT (XBUFFER (ow->contents)),
546 BUF_PT_BYTE (XBUFFER (ow->contents)));
549 selected_window = window;
551 /* Go to the point recorded in the window.
552 This is important when the buffer is in more
553 than one window. It also matters when
554 redisplay_window has altered point after scrolling,
555 because it makes the change only in the window. */
556 set_point_from_marker (XWINDOW (window)->pointm);
559 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
560 doc: /* Select WINDOW which must be a live window.
561 Also make WINDOW's frame the selected frame and WINDOW that frame's
562 selected window. In addition, make WINDOW's buffer current and set that
563 buffer's value of `point' to the value of WINDOW's `window-point'.
564 Return WINDOW.
566 Optional second arg NORECORD non-nil means do not put this buffer at the
567 front of the buffer list and do not make this window the most recently
568 selected one.
570 Note that the main editor command loop sets the current buffer to the
571 buffer of the selected window before each command. */)
572 (register Lisp_Object window, Lisp_Object norecord)
574 return select_window (window, norecord, 0);
577 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
578 doc: /* Return the buffer displayed in window WINDOW.
579 If WINDOW is omitted or nil, it defaults to the selected window.
580 Return nil for an internal window or a deleted window. */)
581 (Lisp_Object window)
583 struct window *w = decode_any_window (window);
584 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
587 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
588 doc: /* Return the parent window of window WINDOW.
589 WINDOW must be a valid window and defaults to the selected one.
590 Return nil for a window with no parent (e.g. a root window). */)
591 (Lisp_Object window)
593 return decode_valid_window (window)->parent;
596 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
597 doc: /* Return the topmost child window of window WINDOW.
598 WINDOW must be a valid window and defaults to the selected one.
599 Return nil if WINDOW is a live window (live windows have no children).
600 Return nil if WINDOW is an internal window whose children form a
601 horizontal combination. */)
602 (Lisp_Object window)
604 struct window *w = decode_valid_window (window);
605 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
608 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
609 doc: /* Return the leftmost child window of window WINDOW.
610 WINDOW must be a valid window and defaults to the selected one.
611 Return nil if WINDOW is a live window (live windows have no children).
612 Return nil if WINDOW is an internal window whose children form a
613 vertical combination. */)
614 (Lisp_Object window)
616 struct window *w = decode_valid_window (window);
617 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
620 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
621 doc: /* Return the next sibling window of window WINDOW.
622 WINDOW must be a valid window and defaults to the selected one.
623 Return nil if WINDOW has no next sibling. */)
624 (Lisp_Object window)
626 return decode_valid_window (window)->next;
629 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
630 doc: /* Return the previous sibling window of window WINDOW.
631 WINDOW must be a valid window and defaults to the selected one.
632 Return nil if WINDOW has no previous sibling. */)
633 (Lisp_Object window)
635 return decode_valid_window (window)->prev;
638 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
639 doc: /* Return combination limit of window WINDOW.
640 WINDOW must be a valid window used in horizontal or vertical combination.
641 If the return value is nil, child windows of WINDOW can be recombined with
642 WINDOW's siblings. A return value of t means that child windows of
643 WINDOW are never \(re-)combined with WINDOW's siblings. */)
644 (Lisp_Object window)
646 struct window *w;
648 CHECK_VALID_WINDOW (window);
649 w = XWINDOW (window);
650 if (WINDOW_LEAF_P (w))
651 error ("Combination limit is meaningful for internal windows only");
652 return w->combination_limit;
655 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
656 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
657 WINDOW must be a valid window used in horizontal or vertical combination.
658 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
659 siblings. LIMIT t means that child windows of WINDOW are never
660 \(re-)combined with WINDOW's siblings. Other values are reserved for
661 future use. */)
662 (Lisp_Object window, Lisp_Object limit)
664 struct window *w;
666 CHECK_VALID_WINDOW (window);
667 w = XWINDOW (window);
668 if (WINDOW_LEAF_P (w))
669 error ("Combination limit is meaningful for internal windows only");
670 wset_combination_limit (w, limit);
671 return limit;
674 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
675 doc: /* Return the use time of window WINDOW.
676 WINDOW must be a live window and defaults to the selected one.
677 The window with the highest use time is the most recently selected
678 one. The window with the lowest use time is the least recently
679 selected one. */)
680 (Lisp_Object window)
682 return make_number (decode_live_window (window)->use_time);
685 DEFUN ("window-pixel-width", Fwindow_pixel_width, Swindow_pixel_width, 0, 1, 0,
686 doc: /* Return the width of window WINDOW in pixels.
687 WINDOW must be a valid window and defaults to the selected one.
689 The return value includes the fringes and margins of WINDOW as well as
690 any vertical dividers or scroll bars belonging to WINDOW. If WINDOW is
691 an internal window, its pixel width is the width of the screen areas
692 spanned by its children. */)
693 (Lisp_Object window)
695 return make_number (decode_valid_window (window)->pixel_width);
698 DEFUN ("window-pixel-height", Fwindow_pixel_height, Swindow_pixel_height, 0, 1, 0,
699 doc: /* Return the height of window WINDOW in pixels.
700 WINDOW must be a valid window and defaults to the selected one.
702 The return value includes the mode line and header line, if any. If
703 WINDOW is an internal window, its pixel height is the height of the
704 screen areas spanned by its children. */)
705 (Lisp_Object window)
707 return make_number (decode_valid_window (window)->pixel_height);
710 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 2, 0,
711 doc: /* Return the height of window WINDOW in lines.
712 WINDOW must be a valid window and defaults to the selected one.
714 The return value includes the heights of WINDOW's mode and header line
715 and its bottom divider, if any. If WINDOW is an internal window, the
716 total height is the height of the screen areas spanned by its children.
718 If WINDOW's pixel height is not an integral multiple of its frame's
719 character height, the number of lines occupied by WINDOW is rounded
720 internally. This is done in a way such that, if WINDOW is a parent
721 window, the sum of the total heights of all its children internally
722 equals the total height of WINDOW.
724 If the optional argument ROUND is `ceiling', return the smallest integer
725 larger than WINDOW's pixel height divided by the character height of
726 WINDOW's frame. ROUND `floor' means to return the largest integer
727 smaller than WINDOW's pixel height divided by the character height of
728 WINDOW's frame. Any other value of ROUND means to return the internal
729 total height of WINDOW. */)
730 (Lisp_Object window, Lisp_Object round)
732 struct window *w = decode_valid_window (window);
734 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
735 return make_number (w->total_lines);
736 else
738 int unit = FRAME_LINE_HEIGHT (WINDOW_XFRAME (w));
740 return make_number (EQ (round, Qceiling)
741 ? ((w->pixel_height + unit - 1) /unit)
742 : (w->pixel_height / unit));
746 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 2, 0,
747 doc: /* Return the total width of window WINDOW in columns.
748 WINDOW must be a valid window and defaults to the selected one.
750 The return value includes the widths of WINDOW's fringes, margins,
751 scroll bars and its right divider, if any. If WINDOW is an internal
752 window, the total width is the width of the screen areas spanned by its
753 children.
755 If WINDOW's pixel width is not an integral multiple of its frame's
756 character width, the number of lines occupied by WINDOW is rounded
757 internally. This is done in a way such that, if WINDOW is a parent
758 window, the sum of the total widths of all its children internally
759 equals the total width of WINDOW.
761 If the optional argument ROUND is `ceiling', return the smallest integer
762 larger than WINDOW's pixel width divided by the character width of
763 WINDOW's frame. ROUND `floor' means to return the largest integer
764 smaller than WINDOW's pixel width divided by the character width of
765 WINDOW's frame. Any other value of ROUND means to return the internal
766 total width of WINDOW. */)
767 (Lisp_Object window, Lisp_Object round)
769 struct window *w = decode_valid_window (window);
771 if (! EQ (round, Qfloor) && ! EQ (round, Qceiling))
772 return make_number (w->total_cols);
773 else
775 int unit = FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
777 return make_number (EQ (round, Qceiling)
778 ? ((w->pixel_width + unit - 1) /unit)
779 : (w->pixel_width / unit));
783 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
784 doc: /* Return the new total size of window WINDOW.
785 WINDOW must be a valid window and defaults to the selected one. */)
786 (Lisp_Object window)
788 return decode_valid_window (window)->new_total;
791 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
792 doc: /* Return the normal height of window WINDOW.
793 WINDOW must be a valid window and defaults to the selected one.
794 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
795 (Lisp_Object window, Lisp_Object horizontal)
797 struct window *w = decode_valid_window (window);
799 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
802 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
803 doc: /* Return new normal size of window WINDOW.
804 WINDOW must be a valid window and defaults to the selected one. */)
805 (Lisp_Object window)
807 return decode_valid_window (window)->new_normal;
810 DEFUN ("window-new-pixel", Fwindow_new_pixel, Swindow_new_pixel, 0, 1, 0,
811 doc: /* Return new pixel size of window WINDOW.
812 WINDOW must be a valid window and defaults to the selected one. */)
813 (Lisp_Object window)
815 return decode_valid_window (window)->new_pixel;
818 DEFUN ("window-pixel-left", Fwindow_pixel_left, Swindow_pixel_left, 0, 1, 0,
819 doc: /* Return left pixel edge of window WINDOW.
820 WINDOW must be a valid window and defaults to the selected one. */)
821 (Lisp_Object window)
823 return make_number (decode_valid_window (window)->pixel_left);
826 DEFUN ("window-pixel-top", Fwindow_pixel_top, Swindow_pixel_top, 0, 1, 0,
827 doc: /* Return top pixel edge of window WINDOW.
828 WINDOW must be a valid window and defaults to the selected one. */)
829 (Lisp_Object window)
831 return make_number (decode_valid_window (window)->pixel_top);
834 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
835 doc: /* Return left column of window WINDOW.
836 This is the distance, in columns, between the left edge of WINDOW and
837 the left edge of the frame's window area. For instance, the return
838 value is 0 if there is no window to the left of WINDOW.
840 WINDOW must be a valid window and defaults to the selected one. */)
841 (Lisp_Object window)
843 return make_number (decode_valid_window (window)->left_col);
846 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
847 doc: /* Return top line of window WINDOW.
848 This is the distance, in lines, between the top of WINDOW and the top
849 of the frame's window area. For instance, the return value is 0 if
850 there is no window above WINDOW.
852 WINDOW must be a valid window and defaults to the selected one. */)
853 (Lisp_Object window)
855 return make_number (decode_valid_window (window)->top_line);
858 /* Return the number of lines/pixels of W's body. Don't count any mode
859 or header line or horizontal divider of W. Rounds down to nearest
860 integer when not working pixelwise. */
861 static int
862 window_body_height (struct window *w, bool pixelwise)
864 int height = (w->pixel_height
865 - WINDOW_HEADER_LINE_HEIGHT (w)
866 - WINDOW_MODE_LINE_HEIGHT (w)
867 - WINDOW_BOTTOM_DIVIDER_WIDTH (w));
869 /* Don't return a negative value. */
870 return max (pixelwise
871 ? height
872 : height / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)),
876 /* Return the number of columns/pixels of W's body. Don't count columns
877 occupied by the scroll bar or the divider/vertical bar separating W
878 from its right sibling or margins. On window-systems don't count
879 fringes either. Round down to nearest integer when not working
880 pixelwise. */
882 window_body_width (struct window *w, bool pixelwise)
884 struct frame *f = XFRAME (WINDOW_FRAME (w));
886 int width = (w->pixel_width
887 - WINDOW_RIGHT_DIVIDER_WIDTH (w)
888 - (WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
889 ? WINDOW_SCROLL_BAR_AREA_WIDTH (w)
890 : ((!FRAME_WINDOW_P (f)
891 && !WINDOW_RIGHTMOST_P (w)
892 && !WINDOW_RIGHT_DIVIDER_WIDTH (w))
893 /* A vertical bar is either 1 or 0. */
894 ? 1 : 0))
895 - WINDOW_MARGINS_WIDTH (w)
896 - (FRAME_WINDOW_P (f)
897 ? WINDOW_FRINGES_WIDTH (w)
898 : 0));
900 /* Don't return a negative value. */
901 return max (pixelwise
902 ? width
903 : width / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)),
907 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 2, 0,
908 doc: /* Return the height of WINDOW's text area.
909 WINDOW must be a live window and defaults to the selected one. Optional
910 argument PIXELWISE non-nil means return the height of WINDOW's text area
911 in pixels. The return value does not include the mode line or header
912 line or any horizontal divider.
914 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
915 pixel height divided by the character height of WINDOW's frame. This
916 means that if a line at the bottom of the text area is only partially
917 visible, that line is not counted. */)
918 (Lisp_Object window, Lisp_Object pixelwise)
920 return make_number (window_body_height (decode_live_window (window),
921 NILP (pixelwise) ? 0 : 1));
924 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 2, 0,
925 doc: /* Return the width of WINDOW's text area.
926 WINDOW must be a live window and defaults to the selected one. Optional
927 argument PIXELWISE non-nil means return the width in pixels. The return
928 value does not include any vertical dividers, fringes or marginal areas,
929 or scroll bars.
931 If PIXELWISE is nil, return the largest integer smaller than WINDOW's
932 pixel width divided by the character width of WINDOW's frame. This
933 means that if a column at the right of the text area is only partially
934 visible, that column is not counted. */)
935 (Lisp_Object window, Lisp_Object pixelwise)
937 return make_number (window_body_width (decode_live_window (window),
938 NILP (pixelwise) ? 0 : 1));
941 DEFUN ("window-mode-line-height", Fwindow_mode_line_height,
942 Swindow_mode_line_height, 0, 1, 0,
943 doc: /* Return the height in pixels of WINDOW's mode-line.
944 WINDOW must be a live window and defaults to the selected one. */)
945 (Lisp_Object window)
947 return (make_number (WINDOW_MODE_LINE_HEIGHT (decode_live_window (window))));
950 DEFUN ("window-header-line-height", Fwindow_header_line_height,
951 Swindow_header_line_height, 0, 1, 0,
952 doc: /* Return the height in pixels of WINDOW's header-line.
953 WINDOW must be a live window and defaults to the selected one. */)
954 (Lisp_Object window)
956 return (make_number (WINDOW_HEADER_LINE_HEIGHT (decode_live_window (window))));
959 DEFUN ("window-right-divider-width", Fwindow_right_divider_width,
960 Swindow_right_divider_width, 0, 1, 0,
961 doc: /* Return the width in pixels of WINDOW's right divider.
962 WINDOW must be a live window and defaults to the selected one. */)
963 (Lisp_Object window)
965 return (make_number (WINDOW_RIGHT_DIVIDER_WIDTH (decode_live_window (window))));
968 DEFUN ("window-bottom-divider-width", Fwindow_bottom_divider_width,
969 Swindow_bottom_divider_width, 0, 1, 0,
970 doc: /* Return the width in pixels of WINDOW's bottom divider.
971 WINDOW must be a live window and defaults to the selected one. */)
972 (Lisp_Object window)
974 return (make_number (WINDOW_BOTTOM_DIVIDER_WIDTH (decode_live_window (window))));
977 DEFUN ("window-scroll-bar-width", Fwindow_scroll_bar_width,
978 Swindow_scroll_bar_width, 0, 1, 0,
979 doc: /* Return the width in pixels of WINDOW's vertical scrollbar.
980 WINDOW must be a live window and defaults to the selected one. */)
981 (Lisp_Object window)
983 return (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (decode_live_window (window))));
986 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
987 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
988 WINDOW must be a live window and defaults to the selected one. */)
989 (Lisp_Object window)
991 return make_number (decode_live_window (window)->hscroll);
994 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
995 range, returning the new amount as a fixnum. */
996 static Lisp_Object
997 set_window_hscroll (struct window *w, EMACS_INT hscroll)
999 /* Horizontal scrolling has problems with large scroll amounts.
1000 It's too slow with long lines, and even with small lines the
1001 display can be messed up. For now, though, impose only the limits
1002 required by the internal representation: horizontal scrolling must
1003 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
1004 (since it's stored in a ptrdiff_t). */
1005 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
1006 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
1008 /* Prevent redisplay shortcuts when changing the hscroll. */
1009 if (w->hscroll != new_hscroll)
1010 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
1012 w->hscroll = new_hscroll;
1013 return make_number (new_hscroll);
1016 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
1017 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
1018 WINDOW must be a live window and defaults to the selected one.
1019 Clip the number to a reasonable value if out of range.
1020 Return the new number. NCOL should be zero or positive.
1022 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
1023 window so that the location of point moves off-window. */)
1024 (Lisp_Object window, Lisp_Object ncol)
1026 CHECK_NUMBER (ncol);
1027 return set_window_hscroll (decode_live_window (window), XINT (ncol));
1030 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
1031 Swindow_redisplay_end_trigger, 0, 1, 0,
1032 doc: /* Return WINDOW's redisplay end trigger value.
1033 WINDOW must be a live window and defaults to the selected one.
1034 See `set-window-redisplay-end-trigger' for more information. */)
1035 (Lisp_Object window)
1037 return decode_live_window (window)->redisplay_end_trigger;
1040 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
1041 Sset_window_redisplay_end_trigger, 2, 2, 0,
1042 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
1043 WINDOW must be a live window and defaults to the selected one. VALUE
1044 should be a buffer position (typically a marker) or nil. If it is a
1045 buffer position, then if redisplay in WINDOW reaches a position beyond
1046 VALUE, the functions in `redisplay-end-trigger-functions' are called
1047 with two arguments: WINDOW, and the end trigger value. Afterwards the
1048 end-trigger value is reset to nil. */)
1049 (register Lisp_Object window, Lisp_Object value)
1051 wset_redisplay_end_trigger (decode_live_window (window), value);
1052 return value;
1055 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
1056 doc: /* Return a list of the edge coordinates of WINDOW.
1057 WINDOW must be a valid window and defaults to the selected one.
1059 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1060 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1061 0 at top left corner of frame.
1063 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
1064 is one more than the bottommost row occupied by WINDOW. The edges
1065 include the space used by WINDOW's scroll bar, display margins, fringes,
1066 header line, and/or mode line. For the edges of just the text area, use
1067 `window-inside-edges'. */)
1068 (Lisp_Object window)
1070 register struct window *w = decode_valid_window (window);
1072 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
1073 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
1076 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
1077 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1078 WINDOW must be a valid window and defaults to the selected one.
1080 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1081 0, 0 at the top left corner of the frame.
1083 RIGHT is one more than the rightmost x position occupied by WINDOW.
1084 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1085 The pixel edges include the space used by WINDOW's scroll bar, display
1086 margins, fringes, header line, and/or mode line. For the pixel edges
1087 of just the text area, use `window-inside-pixel-edges'. */)
1088 (Lisp_Object window)
1090 register struct window *w = decode_valid_window (window);
1092 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
1093 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
1096 static void
1097 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
1099 struct frame *f = XFRAME (w->frame);
1100 *add_y = f->top_pos;
1101 #ifdef FRAME_MENUBAR_HEIGHT
1102 *add_y += FRAME_MENUBAR_HEIGHT (f);
1103 #endif
1104 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
1105 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
1106 #elif defined (FRAME_TOOLBAR_HEIGHT)
1107 *add_y += FRAME_TOOLBAR_HEIGHT (f);
1108 #endif
1109 #ifdef FRAME_NS_TITLEBAR_HEIGHT
1110 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
1111 #endif
1112 *add_x = f->left_pos;
1113 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
1114 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
1115 #endif
1118 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
1119 Swindow_absolute_pixel_edges, 0, 1, 0,
1120 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1121 WINDOW must be a valid window and defaults to the selected one.
1123 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1124 0, 0 at the top left corner of the display.
1126 RIGHT is one more than the rightmost x position occupied by WINDOW.
1127 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1128 The pixel edges include the space used by WINDOW's scroll bar, display
1129 margins, fringes, header line, and/or mode line. For the pixel edges
1130 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1131 (Lisp_Object window)
1133 register struct window *w = decode_valid_window (window);
1134 int add_x, add_y;
1136 calc_absolute_offset (w, &add_x, &add_y);
1138 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1139 WINDOW_TOP_EDGE_Y (w) + add_y,
1140 WINDOW_RIGHT_EDGE_X (w) + add_x,
1141 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1144 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1145 doc: /* Return a list of the edge coordinates of WINDOW.
1146 WINDOW must be a live window and defaults to the selected one.
1148 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1149 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1150 0 at top left corner of frame.
1152 RIGHT is one more than the rightmost column of WINDOW's text area.
1153 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1154 inside edges do not include the space used by the WINDOW's scroll bar,
1155 display margins, fringes, header line, and/or mode line. */)
1156 (Lisp_Object window)
1158 register struct window *w = decode_live_window (window);
1160 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1161 + WINDOW_LEFT_MARGIN_COLS (w)
1162 + WINDOW_LEFT_FRINGE_COLS (w)),
1163 (WINDOW_TOP_EDGE_LINE (w)
1164 + WINDOW_HEADER_LINE_LINES (w)),
1165 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1166 - WINDOW_RIGHT_MARGIN_COLS (w)
1167 - WINDOW_RIGHT_FRINGE_COLS (w)),
1168 (WINDOW_BOTTOM_EDGE_LINE (w)
1169 - WINDOW_MODE_LINE_LINES (w)));
1172 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1173 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1174 WINDOW must be a live window and defaults to the selected one.
1176 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1177 (0,0) at the top left corner of the frame's window area.
1179 RIGHT is one more than the rightmost x position of WINDOW's text area.
1180 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1181 The inside edges do not include the space used by WINDOW's scroll bar,
1182 display margins, fringes, header line, and/or mode line. */)
1183 (Lisp_Object window)
1185 register struct window *w = decode_live_window (window);
1187 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1188 + WINDOW_LEFT_MARGIN_WIDTH (w)
1189 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1190 (WINDOW_TOP_EDGE_Y (w)
1191 + WINDOW_HEADER_LINE_HEIGHT (w)),
1192 (WINDOW_BOX_RIGHT_EDGE_X (w)
1193 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1194 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1195 (WINDOW_BOTTOM_EDGE_Y (w)
1196 - WINDOW_MODE_LINE_HEIGHT (w)));
1199 DEFUN ("window-inside-absolute-pixel-edges",
1200 Fwindow_inside_absolute_pixel_edges,
1201 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1202 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1203 WINDOW must be a live window and defaults to the selected one.
1205 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1206 (0,0) at the top left corner of the frame's window area.
1208 RIGHT is one more than the rightmost x position of WINDOW's text area.
1209 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1210 The inside edges do not include the space used by WINDOW's scroll bar,
1211 display margins, fringes, header line, and/or mode line. */)
1212 (Lisp_Object window)
1214 register struct window *w = decode_live_window (window);
1215 int add_x, add_y;
1217 calc_absolute_offset (w, &add_x, &add_y);
1219 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1220 + WINDOW_LEFT_MARGIN_WIDTH (w)
1221 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1222 (WINDOW_TOP_EDGE_Y (w)
1223 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1224 (WINDOW_BOX_RIGHT_EDGE_X (w)
1225 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1226 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1227 (WINDOW_BOTTOM_EDGE_Y (w)
1228 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1231 /* Test if the character at column X, row Y is within window W.
1232 If it is not, return ON_NOTHING;
1233 if it is on the window's vertical divider, return
1234 ON_RIGHT_DIVIDER;
1235 if it is on the window's horizontal divider, return
1236 ON_BOTTOM_DIVIDER;
1237 if it is in the window's text area, return ON_TEXT;
1238 if it is on the window's modeline, return ON_MODE_LINE;
1239 if it is on the border between the window and its right sibling,
1240 return ON_VERTICAL_BORDER;
1241 if it is on a scroll bar, return ON_SCROLL_BAR;
1242 if it is on the window's top line, return ON_HEADER_LINE;
1243 if it is in left or right fringe of the window,
1244 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1245 if it is in the marginal area to the left/right of the window,
1246 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1248 X and Y are frame relative pixel coordinates. */
1250 static enum window_part
1251 coordinates_in_window (register struct window *w, int x, int y)
1253 struct frame *f = XFRAME (WINDOW_FRAME (w));
1254 enum window_part part;
1255 int ux = FRAME_COLUMN_WIDTH (f);
1256 int left_x = WINDOW_LEFT_EDGE_X (w);
1257 int right_x = WINDOW_RIGHT_EDGE_X (w);
1258 int top_y = WINDOW_TOP_EDGE_Y (w);
1259 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1260 /* The width of the area where the vertical line can be dragged.
1261 (Between mode lines for instance. */
1262 int grabbable_width = ux;
1263 int lmargin_width, rmargin_width, text_left, text_right;
1265 /* Outside any interesting row or column? */
1266 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1267 return ON_NOTHING;
1269 /* On vertical window divider (which prevails horizontal
1270 dividers)? */
1271 if (!WINDOW_RIGHTMOST_P (w)
1272 && WINDOW_RIGHT_DIVIDER_WIDTH (w)
1273 && x >= right_x - WINDOW_RIGHT_DIVIDER_WIDTH (w)
1274 && x <= right_x)
1275 return ON_RIGHT_DIVIDER;
1276 /* On the horizontal window divider? */
1277 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1278 && y >= (bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1279 && y <= bottom_y)
1280 return ON_BOTTOM_DIVIDER;
1281 /* On the mode or header line? */
1282 else if ((WINDOW_WANTS_MODELINE_P (w)
1283 && y >= (bottom_y
1284 - CURRENT_MODE_LINE_HEIGHT (w)
1285 - WINDOW_BOTTOM_DIVIDER_WIDTH (w))
1286 && y <= bottom_y - WINDOW_BOTTOM_DIVIDER_WIDTH (w)
1287 && (part = ON_MODE_LINE))
1288 || (WINDOW_WANTS_HEADER_LINE_P (w)
1289 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1290 && (part = ON_HEADER_LINE)))
1292 /* If it's under/over the scroll bar portion of the mode/header
1293 line, say it's on the vertical line. That's to be able to
1294 resize windows horizontally in case we're using toolkit scroll
1295 bars. Note: If scrollbars are on the left, the window that
1296 must be eventually resized is that on the left of WINDOW. */
1297 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1298 && !WINDOW_LEFTMOST_P (w)
1299 && eabs (x - left_x) < grabbable_width)
1300 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1301 && !WINDOW_RIGHTMOST_P (w)
1302 && eabs (x - right_x) < grabbable_width))
1303 return ON_VERTICAL_BORDER;
1304 else
1305 return part;
1308 /* In what's below, we subtract 1 when computing right_x because we
1309 want the rightmost pixel, which is given by left_pixel+width-1. */
1310 if (w->pseudo_window_p)
1312 left_x = 0;
1313 right_x = WINDOW_PIXEL_WIDTH (w) - 1;
1315 else
1317 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1318 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1321 /* Outside any interesting column? */
1322 if (x < left_x || x > right_x)
1323 return ON_SCROLL_BAR;
1325 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1326 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1328 text_left = window_box_left (w, TEXT_AREA);
1329 text_right = text_left + window_box_width (w, TEXT_AREA);
1331 if (FRAME_WINDOW_P (f))
1333 if (!w->pseudo_window_p
1334 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1335 && !WINDOW_RIGHTMOST_P (w)
1336 && (eabs (x - right_x) < grabbable_width))
1337 return ON_VERTICAL_BORDER;
1339 /* Need to say "x > right_x" rather than >=, since on character
1340 terminals, the vertical line's x coordinate is right_x. */
1341 else if (!w->pseudo_window_p
1342 && !WINDOW_RIGHTMOST_P (w)
1343 /* Why check ux if we are not the rightmost window? Also
1344 shouldn't a pseudo window always be rightmost? */
1345 && x > right_x - ux)
1346 return ON_VERTICAL_BORDER;
1348 if (x < text_left)
1350 if (lmargin_width > 0
1351 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1352 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1353 : (x < left_x + lmargin_width)))
1354 return ON_LEFT_MARGIN;
1356 return ON_LEFT_FRINGE;
1359 if (x >= text_right)
1361 if (rmargin_width > 0
1362 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1363 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1364 : (x >= right_x - rmargin_width)))
1365 return ON_RIGHT_MARGIN;
1367 return ON_RIGHT_FRINGE;
1370 /* Everything special ruled out - must be on text area */
1371 return ON_TEXT;
1374 /* Take X is the frame-relative pixel x-coordinate, and return the
1375 x-coordinate relative to part PART of window W. */
1377 window_relative_x_coord (struct window *w, enum window_part part, int x)
1379 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1381 switch (part)
1383 case ON_TEXT:
1384 return x - window_box_left (w, TEXT_AREA);
1386 case ON_LEFT_FRINGE:
1387 return x - left_x;
1389 case ON_RIGHT_FRINGE:
1390 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1392 case ON_LEFT_MARGIN:
1393 return (x - left_x
1394 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1395 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1397 case ON_RIGHT_MARGIN:
1398 return (x + 1
1399 - ((w->pseudo_window_p)
1400 ? WINDOW_PIXEL_WIDTH (w)
1401 : WINDOW_BOX_RIGHT_EDGE_X (w))
1402 + window_box_width (w, RIGHT_MARGIN_AREA)
1403 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1404 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1407 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1408 return 0;
1412 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1413 Scoordinates_in_window_p, 2, 2, 0,
1414 doc: /* Return non-nil if COORDINATES are in WINDOW.
1415 WINDOW must be a live window and defaults to the selected one.
1416 COORDINATES is a cons of the form (X . Y), X and Y being distances
1417 measured in characters from the upper-left corner of the frame.
1418 \(0 . 0) denotes the character in the upper left corner of the
1419 frame.
1420 If COORDINATES are in the text portion of WINDOW,
1421 the coordinates relative to the window are returned.
1422 If they are in the mode line of WINDOW, `mode-line' is returned.
1423 If they are in the top mode line of WINDOW, `header-line' is returned.
1424 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1425 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1426 If they are on the border between WINDOW and its right sibling,
1427 `vertical-line' is returned.
1428 If they are in the windows's left or right marginal areas, `left-margin'\n\
1429 or `right-margin' is returned. */)
1430 (register Lisp_Object coordinates, Lisp_Object window)
1432 struct window *w;
1433 struct frame *f;
1434 int x, y;
1435 Lisp_Object lx, ly;
1437 w = decode_live_window (window);
1438 f = XFRAME (w->frame);
1439 CHECK_CONS (coordinates);
1440 lx = Fcar (coordinates);
1441 ly = Fcdr (coordinates);
1442 CHECK_NUMBER_OR_FLOAT (lx);
1443 CHECK_NUMBER_OR_FLOAT (ly);
1444 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1445 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1447 switch (coordinates_in_window (w, x, y))
1449 case ON_NOTHING:
1450 return Qnil;
1452 case ON_TEXT:
1453 /* Convert X and Y to window relative pixel coordinates, and
1454 return the canonical char units. */
1455 x -= window_box_left (w, TEXT_AREA);
1456 y -= WINDOW_TOP_EDGE_Y (w);
1457 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1458 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1460 case ON_MODE_LINE:
1461 return Qmode_line;
1463 case ON_VERTICAL_BORDER:
1464 return Qvertical_line;
1466 case ON_HEADER_LINE:
1467 return Qheader_line;
1469 case ON_LEFT_FRINGE:
1470 return Qleft_fringe;
1472 case ON_RIGHT_FRINGE:
1473 return Qright_fringe;
1475 case ON_LEFT_MARGIN:
1476 return Qleft_margin;
1478 case ON_RIGHT_MARGIN:
1479 return Qright_margin;
1481 case ON_SCROLL_BAR:
1482 /* Historically we are supposed to return nil in this case. */
1483 return Qnil;
1485 case ON_RIGHT_DIVIDER:
1486 return Qright_divider;
1488 case ON_BOTTOM_DIVIDER:
1489 return Qbottom_divider;
1491 default:
1492 emacs_abort ();
1497 /* Callback for foreach_window, used in window_from_coordinates.
1498 Check if window W contains coordinates specified by USER_DATA which
1499 is actually a pointer to a struct check_window_data CW.
1501 Check if window W contains coordinates *CW->x and *CW->y. If it
1502 does, return W in *CW->window, as Lisp_Object, and return in
1503 *CW->part the part of the window under coordinates *X,*Y. Return
1504 zero from this function to stop iterating over windows. */
1506 struct check_window_data
1508 Lisp_Object *window;
1509 int x, y;
1510 enum window_part *part;
1513 static int
1514 check_window_containing (struct window *w, void *user_data)
1516 struct check_window_data *cw = user_data;
1517 enum window_part found;
1518 int continue_p = 1;
1520 found = coordinates_in_window (w, cw->x, cw->y);
1521 if (found != ON_NOTHING)
1523 *cw->part = found;
1524 XSETWINDOW (*cw->window, w);
1525 continue_p = 0;
1528 return continue_p;
1532 /* Find the window containing frame-relative pixel position X/Y and
1533 return it as a Lisp_Object.
1535 If X, Y is on one of the window's special `window_part' elements,
1536 set *PART to the id of that element.
1538 If there is no window under X, Y return nil and leave *PART
1539 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1541 This function was previously implemented with a loop cycling over
1542 windows with Fnext_window, and starting with the frame's selected
1543 window. It turned out that this doesn't work with an
1544 implementation of next_window using Vwindow_list, because
1545 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1546 tree of F when this function is called asynchronously from
1547 note_mouse_highlight. The original loop didn't terminate in this
1548 case. */
1550 Lisp_Object
1551 window_from_coordinates (struct frame *f, int x, int y,
1552 enum window_part *part, bool tool_bar_p)
1554 Lisp_Object window;
1555 struct check_window_data cw;
1556 enum window_part dummy;
1558 if (part == 0)
1559 part = &dummy;
1561 window = Qnil;
1562 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1563 foreach_window (f, check_window_containing, &cw);
1565 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1566 /* If not found above, see if it's in the tool bar window, if a tool
1567 bar exists. */
1568 if (NILP (window)
1569 && tool_bar_p
1570 && WINDOWP (f->tool_bar_window)
1571 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1572 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1573 != ON_NOTHING))
1575 *part = ON_TEXT;
1576 window = f->tool_bar_window;
1578 #endif
1580 return window;
1583 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1584 doc: /* Return window containing coordinates X and Y on FRAME.
1585 FRAME must be a live frame and defaults to the selected one.
1586 The top left corner of the frame is considered to be row 0,
1587 column 0. */)
1588 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1590 struct frame *f = decode_live_frame (frame);
1592 /* Check that arguments are integers or floats. */
1593 CHECK_NUMBER_OR_FLOAT (x);
1594 CHECK_NUMBER_OR_FLOAT (y);
1596 return window_from_coordinates (f,
1597 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1598 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1599 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1600 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1601 0, 0);
1604 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1605 doc: /* Return current value of point in WINDOW.
1606 WINDOW must be a live window and defaults to the selected one.
1608 For a nonselected window, this is the value point would have if that
1609 window were selected.
1611 Note that, when WINDOW is selected, the value returned is the same as
1612 that returned by `point' for WINDOW's buffer. It would be more strictly
1613 correct to return the top-level value of `point', outside of any
1614 `save-excursion' forms. But that is hard to define. */)
1615 (Lisp_Object window)
1617 register struct window *w = decode_live_window (window);
1619 if (w == XWINDOW (selected_window))
1620 return make_number (BUF_PT (XBUFFER (w->contents)));
1621 else
1622 return Fmarker_position (w->pointm);
1625 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1626 doc: /* Return position at which display currently starts in WINDOW.
1627 WINDOW must be a live window and defaults to the selected one.
1628 This is updated by redisplay or by calling `set-window-start'. */)
1629 (Lisp_Object window)
1631 return Fmarker_position (decode_live_window (window)->start);
1634 /* This is text temporarily removed from the doc string below.
1636 This function returns nil if the position is not currently known.
1637 That happens when redisplay is preempted and doesn't finish.
1638 If in that case you want to compute where the end of the window would
1639 have been if redisplay had finished, do this:
1640 (save-excursion
1641 (goto-char (window-start window))
1642 (vertical-motion (1- (window-height window)) window)
1643 (point))") */
1645 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1646 doc: /* Return position at which display currently ends in WINDOW.
1647 WINDOW must be a live window and defaults to the selected one.
1648 This is updated by redisplay, when it runs to completion.
1649 Simply changing the buffer text or setting `window-start'
1650 does not update this value.
1651 Return nil if there is no recorded value. (This can happen if the
1652 last redisplay of WINDOW was preempted, and did not finish.)
1653 If UPDATE is non-nil, compute the up-to-date position
1654 if it isn't already recorded. */)
1655 (Lisp_Object window, Lisp_Object update)
1657 Lisp_Object value;
1658 struct window *w = decode_live_window (window);
1659 Lisp_Object buf;
1660 struct buffer *b;
1662 buf = w->contents;
1663 CHECK_BUFFER (buf);
1664 b = XBUFFER (buf);
1666 if (! NILP (update)
1667 && (windows_or_buffers_changed
1668 || !w->window_end_valid
1669 || b->clip_changed
1670 || b->prevent_redisplay_optimizations_p
1671 || window_outdated (w))
1672 && !noninteractive)
1674 struct text_pos startp;
1675 struct it it;
1676 struct buffer *old_buffer = NULL;
1677 void *itdata = NULL;
1679 /* Cannot use Fvertical_motion because that function doesn't
1680 cope with variable-height lines. */
1681 if (b != current_buffer)
1683 old_buffer = current_buffer;
1684 set_buffer_internal (b);
1687 /* In case W->start is out of the range, use something
1688 reasonable. This situation occurred when loading a file with
1689 `-l' containing a call to `rmail' with subsequent other
1690 commands. At the end, W->start happened to be BEG, while
1691 rmail had already narrowed the buffer. */
1692 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1694 itdata = bidi_shelve_cache ();
1695 start_display (&it, w, startp);
1696 move_it_vertically (&it, window_box_height (w));
1697 if (it.current_y < it.last_visible_y)
1698 move_it_past_eol (&it);
1699 value = make_number (IT_CHARPOS (it));
1700 bidi_unshelve_cache (itdata, 0);
1702 if (old_buffer)
1703 set_buffer_internal (old_buffer);
1705 else
1706 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1708 return value;
1711 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1712 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1713 WINDOW must be a live window and defaults to the selected one.
1714 Return POS. */)
1715 (Lisp_Object window, Lisp_Object pos)
1717 register struct window *w = decode_live_window (window);
1719 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1721 if (w == XWINDOW (selected_window))
1723 if (XBUFFER (w->contents) == current_buffer)
1724 Fgoto_char (pos);
1725 else
1727 struct buffer *old_buffer = current_buffer;
1729 /* ... but here we want to catch type error before buffer change. */
1730 CHECK_NUMBER_COERCE_MARKER (pos);
1731 set_buffer_internal (XBUFFER (w->contents));
1732 Fgoto_char (pos);
1733 set_buffer_internal (old_buffer);
1736 else
1738 set_marker_restricted (w->pointm, pos, w->contents);
1739 /* We have to make sure that redisplay updates the window to show
1740 the new value of point. */
1741 wset_redisplay (w);
1744 return pos;
1747 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1748 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1749 WINDOW must be a live window and defaults to the selected one. Return
1750 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1751 overriding motion of point in order to display at this exact start. */)
1752 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1754 register struct window *w = decode_live_window (window);
1756 set_marker_restricted (w->start, pos, w->contents);
1757 /* This is not right, but much easier than doing what is right. */
1758 w->start_at_line_beg = 0;
1759 if (NILP (noforce))
1760 w->force_start = 1;
1761 w->update_mode_line = 1;
1762 /* Bug#15957. */
1763 w->window_end_valid = 0;
1764 wset_redisplay (w);
1766 return pos;
1769 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1770 Spos_visible_in_window_p, 0, 3, 0,
1771 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1772 WINDOW must be a live window and defaults to the selected one.
1774 Return nil if that position is scrolled vertically out of view. If a
1775 character is only partially visible, nil is returned, unless the
1776 optional argument PARTIALLY is non-nil. If POS is only out of view
1777 because of horizontal scrolling, return non-nil. If POS is t, it
1778 specifies the position of the last visible glyph in WINDOW. POS
1779 defaults to point in WINDOW; WINDOW defaults to the selected window.
1781 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1782 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1783 where X and Y are the pixel coordinates relative to the top left corner
1784 of the window. The remaining elements are omitted if the character after
1785 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1786 off-window at the top and bottom of the screen line ("row") containing
1787 POS, ROWH is the visible height of that row, and VPOS is the row number
1788 \(zero-based). */)
1789 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1791 register struct window *w;
1792 register EMACS_INT posint;
1793 register struct buffer *buf;
1794 struct text_pos top;
1795 Lisp_Object in_window = Qnil;
1796 int rtop, rbot, rowh, vpos, fully_p = 1;
1797 int x, y;
1799 w = decode_live_window (window);
1800 buf = XBUFFER (w->contents);
1801 SET_TEXT_POS_FROM_MARKER (top, w->start);
1803 if (EQ (pos, Qt))
1804 posint = -1;
1805 else if (!NILP (pos))
1807 CHECK_NUMBER_COERCE_MARKER (pos);
1808 posint = XINT (pos);
1810 else if (w == XWINDOW (selected_window))
1811 posint = PT;
1812 else
1813 posint = marker_position (w->pointm);
1815 /* If position is above window start or outside buffer boundaries,
1816 or if window start is out of range, position is not visible. */
1817 if ((EQ (pos, Qt)
1818 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1819 && CHARPOS (top) >= BUF_BEGV (buf)
1820 && CHARPOS (top) <= BUF_ZV (buf)
1821 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1822 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1823 in_window = Qt;
1825 if (!NILP (in_window) && !NILP (partially))
1827 Lisp_Object part = Qnil;
1828 if (!fully_p)
1829 part = list4i (rtop, rbot, rowh, vpos);
1830 in_window = Fcons (make_number (x),
1831 Fcons (make_number (y), part));
1834 return in_window;
1837 DEFUN ("window-line-height", Fwindow_line_height,
1838 Swindow_line_height, 0, 2, 0,
1839 doc: /* Return height in pixels of text line LINE in window WINDOW.
1840 WINDOW must be a live window and defaults to the selected one.
1842 Return height of current line if LINE is omitted or nil. Return height of
1843 header or mode line if LINE is `header-line' or `mode-line'.
1844 Otherwise, LINE is a text line number starting from 0. A negative number
1845 counts from the end of the window.
1847 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1848 in pixels of the visible part of the line, VPOS and YPOS are the
1849 vertical position in lines and pixels of the line, relative to the top
1850 of the first text line, and OFFBOT is the number of off-window pixels at
1851 the bottom of the text line. If there are off-window pixels at the top
1852 of the (first) text line, YPOS is negative.
1854 Return nil if window display is not up-to-date. In that case, use
1855 `pos-visible-in-window-p' to obtain the information. */)
1856 (Lisp_Object line, Lisp_Object window)
1858 register struct window *w;
1859 register struct buffer *b;
1860 struct glyph_row *row, *end_row;
1861 int max_y, crop, i;
1862 EMACS_INT n;
1864 w = decode_live_window (window);
1866 if (noninteractive || w->pseudo_window_p)
1867 return Qnil;
1869 CHECK_BUFFER (w->contents);
1870 b = XBUFFER (w->contents);
1872 /* Fail if current matrix is not up-to-date. */
1873 if (!w->window_end_valid
1874 || windows_or_buffers_changed
1875 || b->clip_changed
1876 || b->prevent_redisplay_optimizations_p
1877 || window_outdated (w))
1878 return Qnil;
1880 if (NILP (line))
1882 i = w->cursor.vpos;
1883 if (i < 0 || i >= w->current_matrix->nrows
1884 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1885 return Qnil;
1886 max_y = window_text_bottom_y (w);
1887 goto found_row;
1890 if (EQ (line, Qheader_line))
1892 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1893 return Qnil;
1894 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1895 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1898 if (EQ (line, Qmode_line))
1900 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1901 return (row->enabled_p ?
1902 list4i (row->height,
1903 0, /* not accurate */
1904 (WINDOW_HEADER_LINE_HEIGHT (w)
1905 + window_text_bottom_y (w)),
1907 : Qnil);
1910 CHECK_NUMBER (line);
1911 n = XINT (line);
1913 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1914 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1915 max_y = window_text_bottom_y (w);
1916 i = 0;
1918 while ((n < 0 || i < n)
1919 && row <= end_row && row->enabled_p
1920 && row->y + row->height < max_y)
1921 row++, i++;
1923 if (row > end_row || !row->enabled_p)
1924 return Qnil;
1926 if (++n < 0)
1928 if (-n > i)
1929 return Qnil;
1930 row += n;
1931 i += n;
1934 found_row:
1935 crop = max (0, (row->y + row->height) - max_y);
1936 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1939 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1940 0, 1, 0,
1941 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1942 More precisely, return the value assigned by the last call of
1943 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1944 never called with WINDOW as its argument, or the value set by that
1945 function was internally reset since its last call. WINDOW must be a
1946 live window and defaults to the selected one.
1948 When a window is dedicated to its buffer, `display-buffer' will refrain
1949 from displaying another buffer in it. `get-lru-window' and
1950 `get-largest-window' treat dedicated windows specially.
1951 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1952 `kill-buffer' can delete a dedicated window and the containing frame.
1954 Functions like `set-window-buffer' may change the buffer displayed by a
1955 window, unless that window is "strongly" dedicated to its buffer, that
1956 is the value returned by `window-dedicated-p' is t. */)
1957 (Lisp_Object window)
1959 return decode_live_window (window)->dedicated;
1962 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1963 Sset_window_dedicated_p, 2, 2, 0,
1964 doc: /* Mark WINDOW as dedicated according to FLAG.
1965 WINDOW must be a live window and defaults to the selected one. FLAG
1966 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1967 mark WINDOW as non-dedicated. Return FLAG.
1969 When a window is dedicated to its buffer, `display-buffer' will refrain
1970 from displaying another buffer in it. `get-lru-window' and
1971 `get-largest-window' treat dedicated windows specially.
1972 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1973 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1974 and the containing frame.
1976 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1977 its buffer. Functions like `set-window-buffer' may change the buffer
1978 displayed by a window, unless that window is strongly dedicated to its
1979 buffer. If and when `set-window-buffer' displays another buffer in a
1980 window, it also makes sure that the window is no more dedicated. */)
1981 (Lisp_Object window, Lisp_Object flag)
1983 wset_dedicated (decode_live_window (window), flag);
1984 return flag;
1987 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1988 0, 1, 0,
1989 doc: /* Return buffers previously shown in WINDOW.
1990 WINDOW must be a live window and defaults to the selected one.
1992 The return value is a list of elements (BUFFER WINDOW-START POS),
1993 where BUFFER is a buffer, WINDOW-START is the start position of the
1994 window for that buffer, and POS is a window-specific point value. */)
1995 (Lisp_Object window)
1997 return decode_live_window (window)->prev_buffers;
2000 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
2001 Sset_window_prev_buffers, 2, 2, 0,
2002 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
2003 WINDOW must be a live window and defaults to the selected one.
2005 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
2006 where BUFFER is a buffer, WINDOW-START is the start position of the
2007 window for that buffer, and POS is a window-specific point value. */)
2008 (Lisp_Object window, Lisp_Object prev_buffers)
2010 wset_prev_buffers (decode_live_window (window), prev_buffers);
2011 return prev_buffers;
2014 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
2015 0, 1, 0,
2016 doc: /* Return list of buffers recently re-shown in WINDOW.
2017 WINDOW must be a live window and defaults to the selected one. */)
2018 (Lisp_Object window)
2020 return decode_live_window (window)->next_buffers;
2023 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
2024 Sset_window_next_buffers, 2, 2, 0,
2025 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
2026 WINDOW must be a live window and defaults to the selected one.
2027 NEXT-BUFFERS should be a list of buffers. */)
2028 (Lisp_Object window, Lisp_Object next_buffers)
2030 wset_next_buffers (decode_live_window (window), next_buffers);
2031 return next_buffers;
2034 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
2035 0, 1, 0,
2036 doc: /* Return the parameters of WINDOW and their values.
2037 WINDOW must be a valid window and defaults to the selected one. The
2038 return value is a list of elements of the form (PARAMETER . VALUE). */)
2039 (Lisp_Object window)
2041 return Fcopy_alist (decode_valid_window (window)->window_parameters);
2044 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
2045 2, 2, 0,
2046 doc: /* Return WINDOW's value for PARAMETER.
2047 WINDOW can be any window and defaults to the selected one. */)
2048 (Lisp_Object window, Lisp_Object parameter)
2050 Lisp_Object result;
2052 result = Fassq (parameter, decode_any_window (window)->window_parameters);
2053 return CDR_SAFE (result);
2056 DEFUN ("set-window-parameter", Fset_window_parameter,
2057 Sset_window_parameter, 3, 3, 0,
2058 doc: /* Set WINDOW's value of PARAMETER to VALUE.
2059 WINDOW can be any window and defaults to the selected one.
2060 Return VALUE. */)
2061 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
2063 register struct window *w = decode_any_window (window);
2064 Lisp_Object old_alist_elt;
2066 old_alist_elt = Fassq (parameter, w->window_parameters);
2067 if (NILP (old_alist_elt))
2068 wset_window_parameters
2069 (w, Fcons (Fcons (parameter, value), w->window_parameters));
2070 else
2071 Fsetcdr (old_alist_elt, value);
2072 return value;
2075 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
2076 0, 1, 0,
2077 doc: /* Return the display-table that WINDOW is using.
2078 WINDOW must be a live window and defaults to the selected one. */)
2079 (Lisp_Object window)
2081 return decode_live_window (window)->display_table;
2084 /* Get the display table for use on window W. This is either W's
2085 display table or W's buffer's display table. Ignore the specified
2086 tables if they are not valid; if no valid table is specified,
2087 return 0. */
2089 struct Lisp_Char_Table *
2090 window_display_table (struct window *w)
2092 struct Lisp_Char_Table *dp = NULL;
2094 if (DISP_TABLE_P (w->display_table))
2095 dp = XCHAR_TABLE (w->display_table);
2096 else if (BUFFERP (w->contents))
2098 struct buffer *b = XBUFFER (w->contents);
2100 if (DISP_TABLE_P (BVAR (b, display_table)))
2101 dp = XCHAR_TABLE (BVAR (b, display_table));
2102 else if (DISP_TABLE_P (Vstandard_display_table))
2103 dp = XCHAR_TABLE (Vstandard_display_table);
2106 return dp;
2109 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
2110 doc: /* Set WINDOW's display-table to TABLE.
2111 WINDOW must be a live window and defaults to the selected one. */)
2112 (register Lisp_Object window, Lisp_Object table)
2114 wset_display_table (decode_live_window (window), table);
2115 return table;
2118 /* Record info on buffer window W is displaying
2119 when it is about to cease to display that buffer. */
2120 static void
2121 unshow_buffer (register struct window *w)
2123 Lisp_Object buf = w->contents;
2124 struct buffer *b = XBUFFER (buf);
2126 eassert (b == XMARKER (w->pointm)->buffer);
2128 #if 0
2129 if (w == XWINDOW (selected_window)
2130 || ! EQ (buf, XWINDOW (selected_window)->contents))
2131 /* Do this except when the selected window's buffer
2132 is being removed from some other window. */
2133 #endif
2134 /* last_window_start records the start position that this buffer
2135 had in the last window to be disconnected from it.
2136 Now that this statement is unconditional,
2137 it is possible for the buffer to be displayed in the
2138 selected window, while last_window_start reflects another
2139 window which was recently showing the same buffer.
2140 Some people might say that might be a good thing. Let's see. */
2141 b->last_window_start = marker_position (w->start);
2143 /* Point in the selected window's buffer
2144 is actually stored in that buffer, and the window's pointm isn't used.
2145 So don't clobber point in that buffer. */
2146 if (! EQ (buf, XWINDOW (selected_window)->contents)
2147 /* Don't clobber point in current buffer either (this could be
2148 useful in connection with bug#12208).
2149 && XBUFFER (buf) != current_buffer */
2150 /* This line helps to fix Horsley's testbug.el bug. */
2151 && !(WINDOWP (BVAR (b, last_selected_window))
2152 && w != XWINDOW (BVAR (b, last_selected_window))
2153 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2154 temp_set_point_both (b,
2155 clip_to_bounds (BUF_BEGV (b),
2156 marker_position (w->pointm),
2157 BUF_ZV (b)),
2158 clip_to_bounds (BUF_BEGV_BYTE (b),
2159 marker_byte_position (w->pointm),
2160 BUF_ZV_BYTE (b)));
2162 if (WINDOWP (BVAR (b, last_selected_window))
2163 && w == XWINDOW (BVAR (b, last_selected_window)))
2164 bset_last_selected_window (b, Qnil);
2167 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2168 means change window structure only. Otherwise store geometry and
2169 other settings as well. */
2170 static void
2171 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2173 register Lisp_Object tem;
2174 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2176 /* If OLD is its frame's root window, then NEW is the new
2177 root window for that frame. */
2178 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2179 fset_root_window (XFRAME (o->frame), new);
2181 if (setflag)
2183 n->pixel_left = o->pixel_left;
2184 n->pixel_top = o->pixel_top;
2185 n->pixel_width = o->pixel_width;
2186 n->pixel_height = o->pixel_height;
2187 n->left_col = o->left_col;
2188 n->top_line = o->top_line;
2189 n->total_cols = o->total_cols;
2190 n->total_lines = o->total_lines;
2191 wset_normal_cols (n, o->normal_cols);
2192 wset_normal_cols (o, make_float (1.0));
2193 wset_normal_lines (n, o->normal_lines);
2194 wset_normal_lines (o, make_float (1.0));
2195 n->desired_matrix = n->current_matrix = 0;
2196 n->vscroll = 0;
2197 memset (&n->cursor, 0, sizeof (n->cursor));
2198 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2199 n->last_cursor_vpos = 0;
2200 #ifdef HAVE_WINDOW_SYSTEM
2201 n->phys_cursor_type = NO_CURSOR;
2202 n->phys_cursor_width = -1;
2203 #endif
2204 n->must_be_updated_p = 0;
2205 n->pseudo_window_p = 0;
2206 n->window_end_vpos = 0;
2207 n->window_end_pos = 0;
2208 n->window_end_valid = 0;
2211 tem = o->next;
2212 wset_next (n, tem);
2213 if (!NILP (tem))
2214 wset_prev (XWINDOW (tem), new);
2216 tem = o->prev;
2217 wset_prev (n, tem);
2218 if (!NILP (tem))
2219 wset_next (XWINDOW (tem), new);
2221 tem = o->parent;
2222 wset_parent (n, tem);
2223 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2224 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2227 /* If window WINDOW and its parent window are iso-combined, merge
2228 WINDOW's children into those of its parent window and mark WINDOW as
2229 deleted. */
2231 static void
2232 recombine_windows (Lisp_Object window)
2234 struct window *w, *p, *c;
2235 Lisp_Object parent, child;
2236 bool horflag;
2238 w = XWINDOW (window);
2239 parent = w->parent;
2240 if (!NILP (parent) && NILP (w->combination_limit))
2242 p = XWINDOW (parent);
2243 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2244 && p->horizontal == w->horizontal)
2245 /* WINDOW and PARENT are both either a vertical or a horizontal
2246 combination. */
2248 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2249 child = w->contents;
2250 c = XWINDOW (child);
2252 /* Splice WINDOW's children into its parent's children and
2253 assign new normal sizes. */
2254 if (NILP (w->prev))
2255 wset_combination (p, horflag, child);
2256 else
2258 wset_prev (c, w->prev);
2259 wset_next (XWINDOW (w->prev), child);
2262 while (c)
2264 wset_parent (c, parent);
2266 if (horflag)
2267 wset_normal_cols
2268 (c, make_float ((double) c->pixel_width
2269 / (double) p->pixel_width));
2270 else
2271 wset_normal_lines
2272 (c, make_float ((double) c->pixel_height
2273 / (double) p->pixel_height));
2275 if (NILP (c->next))
2277 if (!NILP (w->next))
2279 wset_next (c, w->next);
2280 wset_prev (XWINDOW (c->next), child);
2283 c = 0;
2285 else
2287 child = c->next;
2288 c = XWINDOW (child);
2292 /* WINDOW can be deleted now. */
2293 wset_combination (w, 0, Qnil);
2298 /* If WINDOW can be deleted, delete it. */
2299 static void
2300 delete_deletable_window (Lisp_Object window)
2302 if (!NILP (call1 (Qwindow_deletable_p, window)))
2303 call1 (Qdelete_window, window);
2306 /***********************************************************************
2307 Window List
2308 ***********************************************************************/
2310 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2311 pointer. This is a callback function for foreach_window, used in
2312 the window_list function. */
2314 static int
2315 add_window_to_list (struct window *w, void *user_data)
2317 Lisp_Object *list = user_data;
2318 Lisp_Object window;
2319 XSETWINDOW (window, w);
2320 *list = Fcons (window, *list);
2321 return 1;
2325 /* Return a list of all windows, for use by next_window. If
2326 Vwindow_list is a list, return that list. Otherwise, build a new
2327 list, cache it in Vwindow_list, and return that. */
2329 Lisp_Object
2330 window_list (void)
2332 if (!CONSP (Vwindow_list))
2334 Lisp_Object tail, frame;
2336 Vwindow_list = Qnil;
2337 FOR_EACH_FRAME (tail, frame)
2339 Lisp_Object args[2];
2341 /* We are visiting windows in canonical order, and add
2342 new windows at the front of args[1], which means we
2343 have to reverse this list at the end. */
2344 args[1] = Qnil;
2345 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2346 args[0] = Vwindow_list;
2347 args[1] = Fnreverse (args[1]);
2348 Vwindow_list = Fnconc (2, args);
2352 return Vwindow_list;
2356 /* Value is non-zero if WINDOW satisfies the constraints given by
2357 OWINDOW, MINIBUF and ALL_FRAMES.
2359 MINIBUF t means WINDOW may be minibuffer windows.
2360 `lambda' means WINDOW may not be a minibuffer window.
2361 a window means a specific minibuffer window
2363 ALL_FRAMES t means search all frames,
2364 nil means search just current frame,
2365 `visible' means search just visible frames on the
2366 current terminal,
2367 0 means search visible and iconified frames on the
2368 current terminal,
2369 a window means search the frame that window belongs to,
2370 a frame means consider windows on that frame, only. */
2372 static bool
2373 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2374 Lisp_Object minibuf, Lisp_Object all_frames)
2376 struct window *w = XWINDOW (window);
2377 struct frame *f = XFRAME (w->frame);
2378 bool candidate_p = 1;
2380 if (!BUFFERP (w->contents))
2381 candidate_p = 0;
2382 else if (MINI_WINDOW_P (w)
2383 && (EQ (minibuf, Qlambda)
2384 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2386 /* If MINIBUF is `lambda' don't consider any mini-windows.
2387 If it is a window, consider only that one. */
2388 candidate_p = 0;
2390 else if (EQ (all_frames, Qt))
2391 candidate_p = 1;
2392 else if (NILP (all_frames))
2394 eassert (WINDOWP (owindow));
2395 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2397 else if (EQ (all_frames, Qvisible))
2399 candidate_p = FRAME_VISIBLE_P (f)
2400 && (FRAME_TERMINAL (XFRAME (w->frame))
2401 == FRAME_TERMINAL (XFRAME (selected_frame)));
2404 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2406 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2407 #ifdef HAVE_X_WINDOWS
2408 /* Yuck!! If we've just created the frame and the
2409 window-manager requested the user to place it
2410 manually, the window may still not be considered
2411 `visible'. I'd argue it should be at least
2412 something like `iconified', but don't know how to do
2413 that yet. --Stef */
2414 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2415 && !f->output_data.x->has_been_visible)
2416 #endif
2418 && (FRAME_TERMINAL (XFRAME (w->frame))
2419 == FRAME_TERMINAL (XFRAME (selected_frame)));
2421 else if (WINDOWP (all_frames))
2422 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2423 || EQ (XWINDOW (all_frames)->frame, w->frame)
2424 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2425 else if (FRAMEP (all_frames))
2426 candidate_p = EQ (all_frames, w->frame);
2428 return candidate_p;
2432 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2433 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2434 MINIBUF, and ALL_FRAMES. */
2436 static void
2437 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2439 struct window *w = decode_live_window (*window);
2441 XSETWINDOW (*window, w);
2442 /* MINIBUF nil may or may not include minibuffers. Decide if it
2443 does. */
2444 if (NILP (*minibuf))
2445 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2446 else if (!EQ (*minibuf, Qt))
2447 *minibuf = Qlambda;
2449 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2450 => count none of them, or a specific minibuffer window (the
2451 active one) to count. */
2453 /* ALL_FRAMES nil doesn't specify which frames to include. */
2454 if (NILP (*all_frames))
2455 *all_frames
2456 = (!EQ (*minibuf, Qlambda)
2457 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2458 : Qnil);
2459 else if (EQ (*all_frames, Qvisible))
2461 else if (EQ (*all_frames, make_number (0)))
2463 else if (FRAMEP (*all_frames))
2465 else if (!EQ (*all_frames, Qt))
2466 *all_frames = Qnil;
2470 /* Return the next or previous window of WINDOW in cyclic ordering
2471 of windows. NEXT_P non-zero means return the next window. See the
2472 documentation string of next-window for the meaning of MINIBUF and
2473 ALL_FRAMES. */
2475 static Lisp_Object
2476 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2478 decode_next_window_args (&window, &minibuf, &all_frames);
2480 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2481 return the first window on the frame. */
2482 if (FRAMEP (all_frames)
2483 && !EQ (all_frames, XWINDOW (window)->frame))
2484 return Fframe_first_window (all_frames);
2486 if (next_p)
2488 Lisp_Object list;
2490 /* Find WINDOW in the list of all windows. */
2491 list = Fmemq (window, window_list ());
2493 /* Scan forward from WINDOW to the end of the window list. */
2494 if (CONSP (list))
2495 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2496 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2497 break;
2499 /* Scan from the start of the window list up to WINDOW. */
2500 if (!CONSP (list))
2501 for (list = Vwindow_list;
2502 CONSP (list) && !EQ (XCAR (list), window);
2503 list = XCDR (list))
2504 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2505 break;
2507 if (CONSP (list))
2508 window = XCAR (list);
2510 else
2512 Lisp_Object candidate, list;
2514 /* Scan through the list of windows for candidates. If there are
2515 candidate windows in front of WINDOW, the last one of these
2516 is the one we want. If there are candidates following WINDOW
2517 in the list, again the last one of these is the one we want. */
2518 candidate = Qnil;
2519 for (list = window_list (); CONSP (list); list = XCDR (list))
2521 if (EQ (XCAR (list), window))
2523 if (WINDOWP (candidate))
2524 break;
2526 else if (candidate_window_p (XCAR (list), window, minibuf,
2527 all_frames))
2528 candidate = XCAR (list);
2531 if (WINDOWP (candidate))
2532 window = candidate;
2535 return window;
2539 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2540 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2541 WINDOW must be a live window and defaults to the selected one. The
2542 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2543 consider.
2545 MINIBUF nil or omitted means consider the minibuffer window only if the
2546 minibuffer is active. MINIBUF t means consider the minibuffer window
2547 even if the minibuffer is not active. Any other value means do not
2548 consider the minibuffer window even if the minibuffer is active.
2550 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2551 plus the minibuffer window if specified by the MINIBUF argument. If the
2552 minibuffer counts, consider all windows on all frames that share that
2553 minibuffer too. The following non-nil values of ALL-FRAMES have special
2554 meanings:
2556 - t means consider all windows on all existing frames.
2558 - `visible' means consider all windows on all visible frames.
2560 - 0 (the number zero) means consider all windows on all visible and
2561 iconified frames.
2563 - A frame means consider all windows on that frame only.
2565 Anything else means consider all windows on WINDOW's frame and no
2566 others.
2568 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2569 `next-window' to iterate through the entire cycle of acceptable
2570 windows, eventually ending up back at the window you started with.
2571 `previous-window' traverses the same cycle, in the reverse order. */)
2572 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2574 return next_window (window, minibuf, all_frames, 1);
2578 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2579 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2580 WINDOW must be a live window and defaults to the selected one. The
2581 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2582 consider.
2584 MINIBUF nil or omitted means consider the minibuffer window only if the
2585 minibuffer is active. MINIBUF t means consider the minibuffer window
2586 even if the minibuffer is not active. Any other value means do not
2587 consider the minibuffer window even if the minibuffer is active.
2589 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2590 plus the minibuffer window if specified by the MINIBUF argument. If the
2591 minibuffer counts, consider all windows on all frames that share that
2592 minibuffer too. The following non-nil values of ALL-FRAMES have special
2593 meanings:
2595 - t means consider all windows on all existing frames.
2597 - `visible' means consider all windows on all visible frames.
2599 - 0 (the number zero) means consider all windows on all visible and
2600 iconified frames.
2602 - A frame means consider all windows on that frame only.
2604 Anything else means consider all windows on WINDOW's frame and no
2605 others.
2607 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2608 use `previous-window' to iterate through the entire cycle of
2609 acceptable windows, eventually ending up back at the window you
2610 started with. `next-window' traverses the same cycle, in the
2611 reverse order. */)
2612 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2614 return next_window (window, minibuf, all_frames, 0);
2618 /* Return a list of windows in cyclic ordering. Arguments are like
2619 for `next-window'. */
2621 static Lisp_Object
2622 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2624 Lisp_Object tail, list, rest;
2626 decode_next_window_args (&window, &minibuf, &all_frames);
2627 list = Qnil;
2629 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2630 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2631 list = Fcons (XCAR (tail), list);
2633 /* Rotate the list to start with WINDOW. */
2634 list = Fnreverse (list);
2635 rest = Fmemq (window, list);
2636 if (!NILP (rest) && !EQ (rest, list))
2638 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2640 XSETCDR (tail, Qnil);
2641 list = nconc2 (rest, list);
2643 return list;
2647 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2648 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2649 FRAME nil or omitted means use the selected frame.
2650 WINDOW nil or omitted means use the window selected within FRAME.
2651 MINIBUF t means include the minibuffer window, even if it isn't active.
2652 MINIBUF nil or omitted means include the minibuffer window only
2653 if it's active.
2654 MINIBUF neither nil nor t means never include the minibuffer window. */)
2655 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2657 if (NILP (window))
2658 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2659 CHECK_WINDOW (window);
2660 if (NILP (frame))
2661 frame = selected_frame;
2663 if (!EQ (frame, XWINDOW (window)->frame))
2664 error ("Window is on a different frame");
2666 return window_list_1 (window, minibuf, frame);
2670 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2671 doc: /* Return a list of all live windows.
2672 WINDOW specifies the first window to list and defaults to the selected
2673 window.
2675 Optional argument MINIBUF nil or omitted means consider the minibuffer
2676 window only if the minibuffer is active. MINIBUF t means consider the
2677 minibuffer window even if the minibuffer is not active. Any other value
2678 means do not consider the minibuffer window even if the minibuffer is
2679 active.
2681 Optional argument ALL-FRAMES nil or omitted means consider all windows
2682 on WINDOW's frame, plus the minibuffer window if specified by the
2683 MINIBUF argument. If the minibuffer counts, consider all windows on all
2684 frames that share that minibuffer too. The following non-nil values of
2685 ALL-FRAMES have special meanings:
2687 - t means consider all windows on all existing frames.
2689 - `visible' means consider all windows on all visible frames.
2691 - 0 (the number zero) means consider all windows on all visible and
2692 iconified frames.
2694 - A frame means consider all windows on that frame only.
2696 Anything else means consider all windows on WINDOW's frame and no
2697 others.
2699 If WINDOW is not on the list of windows returned, some other window will
2700 be listed first but no error is signaled. */)
2701 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2703 return window_list_1 (window, minibuf, all_frames);
2706 /* Look at all windows, performing an operation specified by TYPE
2707 with argument OBJ.
2708 If FRAMES is Qt, look at all frames;
2709 Qnil, look at just the selected frame;
2710 Qvisible, look at visible frames;
2711 a frame, just look at windows on that frame.
2712 If MINI is non-zero, perform the operation on minibuffer windows too. */
2714 enum window_loop
2716 WINDOW_LOOP_UNUSED,
2717 GET_BUFFER_WINDOW, /* Arg is buffer */
2718 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2719 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2720 CHECK_ALL_WINDOWS /* Arg is ignored */
2723 static Lisp_Object
2724 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2726 Lisp_Object window, windows, best_window, frame_arg;
2727 int frame_best_window_flag = 0;
2728 struct frame *f;
2729 struct gcpro gcpro1;
2731 /* If we're only looping through windows on a particular frame,
2732 frame points to that frame. If we're looping through windows
2733 on all frames, frame is 0. */
2734 if (FRAMEP (frames))
2735 f = XFRAME (frames);
2736 else if (NILP (frames))
2737 f = SELECTED_FRAME ();
2738 else
2739 f = NULL;
2741 if (f)
2742 frame_arg = Qlambda;
2743 else if (EQ (frames, make_number (0)))
2744 frame_arg = frames;
2745 else if (EQ (frames, Qvisible))
2746 frame_arg = frames;
2747 else
2748 frame_arg = Qt;
2750 /* frame_arg is Qlambda to stick to one frame,
2751 Qvisible to consider all visible frames,
2752 or Qt otherwise. */
2754 /* Pick a window to start with. */
2755 if (WINDOWP (obj))
2756 window = obj;
2757 else if (f)
2758 window = FRAME_SELECTED_WINDOW (f);
2759 else
2760 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2762 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2763 GCPRO1 (windows);
2764 best_window = Qnil;
2766 for (; CONSP (windows); windows = XCDR (windows))
2768 struct window *w;
2770 window = XCAR (windows);
2771 w = XWINDOW (window);
2773 /* Note that we do not pay attention here to whether the frame
2774 is visible, since Fwindow_list skips non-visible frames if
2775 that is desired, under the control of frame_arg. */
2776 if (!MINI_WINDOW_P (w)
2777 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2778 consider all windows. */
2779 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2780 || (mini && minibuf_level > 0))
2781 switch (type)
2783 case GET_BUFFER_WINDOW:
2784 if (EQ (w->contents, obj)
2785 /* Don't find any minibuffer window except the one that
2786 is currently in use. */
2787 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2789 if (EQ (window, selected_window))
2790 /* Preferably return the selected window. */
2791 RETURN_UNGCPRO (window);
2792 else if (EQ (XWINDOW (window)->frame, selected_frame)
2793 && !frame_best_window_flag)
2794 /* Prefer windows on the current frame (but don't
2795 choose another one if we have one already). */
2797 best_window = window;
2798 frame_best_window_flag = 1;
2800 else if (NILP (best_window))
2801 best_window = window;
2803 break;
2805 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2806 /* We could simply check whether the buffer shown by window
2807 is live, and show another buffer in case it isn't. */
2808 if (EQ (w->contents, obj))
2810 /* Undedicate WINDOW. */
2811 wset_dedicated (w, Qnil);
2812 /* Make WINDOW show the buffer returned by
2813 other_buffer_safely, don't run any hooks. */
2814 set_window_buffer
2815 (window, other_buffer_safely (w->contents), 0, 0);
2816 /* If WINDOW is the selected window, make its buffer
2817 current. But do so only if the window shows the
2818 current buffer (Bug#6454). */
2819 if (EQ (window, selected_window)
2820 && XBUFFER (w->contents) == current_buffer)
2821 Fset_buffer (w->contents);
2823 break;
2825 case REDISPLAY_BUFFER_WINDOWS:
2826 if (EQ (w->contents, obj))
2828 mark_window_display_accurate (window, 0);
2829 w->update_mode_line = 1;
2830 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2831 update_mode_lines = 27;
2832 best_window = window;
2834 break;
2836 /* Check for a leaf window that has a killed buffer
2837 or broken markers. */
2838 case CHECK_ALL_WINDOWS:
2839 if (BUFFERP (w->contents))
2841 struct buffer *b = XBUFFER (w->contents);
2843 if (!BUFFER_LIVE_P (b))
2844 emacs_abort ();
2845 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2846 emacs_abort ();
2847 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2848 emacs_abort ();
2850 break;
2852 case WINDOW_LOOP_UNUSED:
2853 break;
2857 UNGCPRO;
2858 return best_window;
2861 /* Used for debugging. Abort if any window has a dead buffer. */
2863 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2864 void
2865 check_all_windows (void)
2867 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2870 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2871 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2872 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2873 the current buffer.
2875 The optional argument ALL-FRAMES specifies the frames to consider:
2877 - t means consider all windows on all existing frames.
2879 - `visible' means consider all windows on all visible frames.
2881 - 0 (the number zero) means consider all windows on all visible
2882 and iconified frames.
2884 - A frame means consider all windows on that frame only.
2886 Any other value of ALL-FRAMES means consider all windows on the
2887 selected frame and no others. */)
2888 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2890 Lisp_Object buffer;
2892 if (NILP (buffer_or_name))
2893 buffer = Fcurrent_buffer ();
2894 else
2895 buffer = Fget_buffer (buffer_or_name);
2897 if (BUFFERP (buffer))
2898 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2899 else
2900 return Qnil;
2903 static Lisp_Object
2904 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore, Lisp_Object pixelwise)
2906 return call5 (Qwindow_resize_root_window, window, delta, horizontal, ignore, pixelwise);
2910 static Lisp_Object
2911 window_pixel_to_total (Lisp_Object frame, Lisp_Object horizontal)
2913 return call2(Qwindow_pixel_to_total, frame, horizontal);
2917 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2918 Sdelete_other_windows_internal, 0, 2, "",
2919 doc: /* Make WINDOW fill its frame.
2920 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2921 and defaults to the selected one.
2923 Optional argument ROOT, if non-nil, must specify an internal window such
2924 that WINDOW is in its window subtree. If this is the case, replace ROOT
2925 by WINDOW and leave alone any windows not part of ROOT's subtree.
2927 When WINDOW is live try to reduce display jumps by keeping the text
2928 previously visible in WINDOW in the same place on the frame. Doing this
2929 depends on the value of (window-start WINDOW), so if calling this
2930 function in a program gives strange scrolling, make sure the
2931 window-start value is reasonable when this function is called. */)
2932 (Lisp_Object window, Lisp_Object root)
2934 struct window *w, *r, *s;
2935 struct frame *f;
2936 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2937 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2938 int top IF_LINT (= 0), new_top, resize_failed;
2940 w = decode_valid_window (window);
2941 XSETWINDOW (window, w);
2942 f = XFRAME (w->frame);
2944 if (NILP (root))
2945 /* ROOT is the frame's root window. */
2947 root = FRAME_ROOT_WINDOW (f);
2948 r = XWINDOW (root);
2950 else
2951 /* ROOT must be an ancestor of WINDOW. */
2953 r = decode_valid_window (root);
2954 pwindow = XWINDOW (window)->parent;
2955 while (!NILP (pwindow))
2956 if (EQ (pwindow, root))
2957 break;
2958 else
2959 pwindow = XWINDOW (pwindow)->parent;
2960 if (!EQ (pwindow, root))
2961 error ("Specified root is not an ancestor of specified window");
2964 if (EQ (window, root))
2965 /* A noop. */
2966 return Qnil;
2967 /* I don't understand the "top > 0" part below. If we deal with a
2968 standalone minibuffer it would have been caught by the preceding
2969 test. */
2970 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2971 error ("Can't expand minibuffer to full frame");
2973 if (BUFFERP (w->contents))
2975 startpos = marker_position (w->start);
2976 startbyte = marker_byte_position (w->start);
2977 top = (WINDOW_TOP_EDGE_LINE (w)
2978 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w))));
2979 /* Make sure WINDOW is the frame's selected window. */
2980 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2982 if (EQ (selected_frame, w->frame))
2983 Fselect_window (window, Qnil);
2984 else
2985 fset_selected_window (f, window);
2988 else
2990 /* See if the frame's selected window is a part of the window
2991 subtree rooted at WINDOW, by finding all the selected window's
2992 parents and comparing each one with WINDOW. If it isn't we
2993 need a new selected window for this frame. */
2994 swindow = FRAME_SELECTED_WINDOW (f);
2995 while (1)
2997 pwindow = swindow;
2998 while (!NILP (pwindow) && !EQ (window, pwindow))
2999 pwindow = XWINDOW (pwindow)->parent;
3001 if (EQ (window, pwindow))
3002 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
3003 as the new selected window. */
3004 break;
3005 else
3006 /* Else try the previous window of SWINDOW. */
3007 swindow = Fprevious_window (swindow, Qlambda, Qnil);
3010 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
3012 if (EQ (selected_frame, w->frame))
3013 Fselect_window (swindow, Qnil);
3014 else
3015 fset_selected_window (f, swindow);
3019 block_input ();
3020 if (!FRAME_INITIAL_P (f))
3022 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3024 /* We are going to free the glyph matrices of WINDOW, and with
3025 that we might lose any information about glyph rows that have
3026 some of their glyphs highlighted in mouse face. (These rows
3027 are marked with a non-zero mouse_face_p flag.) If WINDOW
3028 indeed has some glyphs highlighted in mouse face, signal to
3029 frame's up-to-date hook that mouse highlight was overwritten,
3030 so that it will arrange for redisplaying the highlight. */
3031 if (EQ (hlinfo->mouse_face_window, window))
3032 reset_mouse_highlight (hlinfo);
3034 free_window_matrices (r);
3036 fset_redisplay (f);
3037 Vwindow_list = Qnil;
3038 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3039 resize_failed = 0;
3041 if (!WINDOW_LEAF_P (w))
3043 /* Resize child windows vertically. */
3044 XSETINT (delta, r->pixel_height - w->pixel_height);
3045 w->pixel_top = r->pixel_top;
3046 w->top_line = r->top_line;
3047 resize_root_window (window, delta, Qnil, Qnil, Qt);
3048 if (window_resize_check (w, 0))
3050 window_resize_apply (w, 0);
3051 window_pixel_to_total (w->frame, Qnil);
3053 else
3055 resize_root_window (window, delta, Qnil, Qt, Qt);
3056 if (window_resize_check (w, 0))
3058 window_resize_apply (w, 0);
3059 window_pixel_to_total (w->frame, Qnil);
3061 else
3062 resize_failed = 1;
3065 /* Resize child windows horizontally. */
3066 if (!resize_failed)
3068 w->left_col = r->left_col;
3069 w->pixel_left = r->pixel_left;
3070 XSETINT (delta, r->pixel_width - w->pixel_width);
3071 resize_root_window (window, delta, Qt, Qnil, Qt);
3072 if (window_resize_check (w, 1))
3074 window_resize_apply (w, 1);
3075 window_pixel_to_total (w->frame, Qt);
3077 else
3079 resize_root_window (window, delta, Qt, Qt, Qt);
3080 if (window_resize_check (w, 1))
3082 window_resize_apply (w, 1);
3083 window_pixel_to_total (w->frame, Qt);
3085 else
3086 resize_failed = 1;
3090 if (resize_failed)
3091 /* Play safe, if we still can ... */
3093 window = swindow;
3094 w = XWINDOW (window);
3098 /* Cleanly unlink WINDOW from window-tree. */
3099 if (!NILP (w->prev))
3100 /* Get SIBLING above (on the left of) WINDOW. */
3102 sibling = w->prev;
3103 s = XWINDOW (sibling);
3104 wset_next (s, w->next);
3105 if (!NILP (s->next))
3106 wset_prev (XWINDOW (s->next), sibling);
3108 else
3109 /* Get SIBLING below (on the right of) WINDOW. */
3111 sibling = w->next;
3112 s = XWINDOW (sibling);
3113 wset_prev (s, Qnil);
3114 wset_combination (XWINDOW (w->parent),
3115 XWINDOW (w->parent)->horizontal, sibling);
3118 /* Delete ROOT and all child windows of ROOT. */
3119 if (WINDOWP (r->contents))
3121 delete_all_child_windows (r->contents);
3122 wset_combination (r, 0, Qnil);
3125 replace_window (root, window, 1);
3127 /* This must become SWINDOW anyway ....... */
3128 if (BUFFERP (w->contents) && !resize_failed)
3130 /* Try to minimize scrolling, by setting the window start to the
3131 point will cause the text at the old window start to be at the
3132 same place on the frame. But don't try to do this if the
3133 window start is outside the visible portion (as might happen
3134 when the display is not current, due to typeahead). */
3135 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
3136 if (new_top != top
3137 && startpos >= BUF_BEGV (XBUFFER (w->contents))
3138 && startpos <= BUF_ZV (XBUFFER (w->contents)))
3140 struct position pos;
3141 struct buffer *obuf = current_buffer;
3143 Fset_buffer (w->contents);
3144 /* This computation used to temporarily move point, but that
3145 can have unwanted side effects due to text properties. */
3146 pos = *vmotion (startpos, startbyte, -top, w);
3148 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
3149 w->window_end_valid = 0;
3150 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
3151 || FETCH_BYTE (pos.bytepos - 1) == '\n');
3152 /* We need to do this, so that the window-scroll-functions
3153 get called. */
3154 w->optional_new_start = 1;
3156 set_buffer_internal (obuf);
3160 adjust_frame_glyphs (f);
3161 unblock_input ();
3163 run_window_configuration_change_hook (f);
3165 return Qnil;
3169 void
3170 replace_buffer_in_windows (Lisp_Object buffer)
3172 call1 (Qreplace_buffer_in_windows, buffer);
3175 /* If BUFFER is shown in a window, safely replace it with some other
3176 buffer in all windows of all frames, even those on other keyboards. */
3178 void
3179 replace_buffer_in_windows_safely (Lisp_Object buffer)
3181 if (buffer_window_count (XBUFFER (buffer)))
3183 Lisp_Object tail, frame;
3185 /* A single call to window_loop won't do the job because it only
3186 considers frames on the current keyboard. So loop manually over
3187 frames, and handle each one. */
3188 FOR_EACH_FRAME (tail, frame)
3189 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3193 /* If *HEIGHT or *WIDTH are too small a size for FRAME, set them to the
3194 minimum allowable size. PIXELWISE means interpret these as pixel
3195 sizes. */
3197 void
3198 check_frame_size (struct frame *frame, int *width, int *height, bool pixelwise)
3200 /* For height, we have to see:
3201 how many windows the frame has at minimum (one or two),
3202 and whether it has a menu bar or other special stuff at the top. */
3203 if (pixelwise)
3205 int min_height = MIN_SAFE_WINDOW_HEIGHT * FRAME_LINE_HEIGHT (frame);
3206 int min_width = MIN_SAFE_WINDOW_WIDTH * FRAME_COLUMN_WIDTH (frame);
3208 if (!FRAME_MINIBUF_ONLY_P (frame) && FRAME_HAS_MINIBUF_P (frame))
3209 min_height = 2 * min_height;
3211 min_height += FRAME_TOP_MARGIN_HEIGHT (frame);
3212 min_height += FRAME_INTERNAL_BORDER_WIDTH (frame);
3214 if (*height < min_height)
3215 *height = min_height;
3216 if (*width < min_width)
3217 *width = min_width;
3219 else
3221 int min_height
3222 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3223 ? MIN_SAFE_WINDOW_HEIGHT
3224 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3226 if (FRAME_TOP_MARGIN (frame) > 0)
3227 min_height += FRAME_TOP_MARGIN (frame);
3229 if (*height < min_height)
3230 *height = min_height;
3231 if (*width < MIN_SAFE_WINDOW_WIDTH)
3232 *width = MIN_SAFE_WINDOW_WIDTH;
3236 /* Adjust the margins of window W if text area is too small.
3237 Return 1 if window width is ok after adjustment; 0 if window
3238 is still too narrow. */
3240 static int
3241 adjust_window_margins (struct window *w)
3243 int box_width = (WINDOW_PIXEL_WIDTH (w)
3244 - WINDOW_FRINGES_WIDTH (w)
3245 - WINDOW_SCROLL_BAR_AREA_WIDTH (w));
3246 int margin_width = WINDOW_MARGINS_WIDTH (w);
3248 if (box_width - margin_width >= MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3249 return 1;
3251 if (margin_width < 0 || box_width < MIN_SAFE_WINDOW_PIXEL_WIDTH (w))
3252 return 0;
3253 else
3254 /* Window's text area is too narrow, but reducing the window
3255 margins will fix that. */
3257 int unit = WINDOW_FRAME_COLUMN_WIDTH (w);
3259 margin_width = box_width - MIN_SAFE_WINDOW_PIXEL_WIDTH (w);
3261 if (WINDOW_RIGHT_MARGIN_WIDTH (w) > 0)
3263 if (WINDOW_LEFT_MARGIN_WIDTH (w) > 0)
3264 w->left_margin_cols = w->right_margin_cols =
3265 margin_width / (2 * unit);
3266 else
3267 w->right_margin_cols = margin_width / unit;
3269 else
3270 w->left_margin_cols = margin_width / unit;
3272 return 1;
3276 /* The following three routines are needed for running a window's
3277 configuration change hook. */
3278 static void
3279 run_funs (Lisp_Object funs)
3281 for (; CONSP (funs); funs = XCDR (funs))
3282 if (!EQ (XCAR (funs), Qt))
3283 call0 (XCAR (funs));
3286 static void
3287 select_window_norecord (Lisp_Object window)
3289 if (WINDOW_LIVE_P (window))
3290 Fselect_window (window, Qt);
3293 static void
3294 select_frame_norecord (Lisp_Object frame)
3296 if (FRAME_LIVE_P (XFRAME (frame)))
3297 Fselect_frame (frame, Qt);
3300 void
3301 run_window_configuration_change_hook (struct frame *f)
3303 ptrdiff_t count = SPECPDL_INDEX ();
3304 Lisp_Object frame, global_wcch
3305 = Fdefault_value (Qwindow_configuration_change_hook);
3306 XSETFRAME (frame, f);
3308 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3309 return;
3311 /* Use the right buffer. Matters when running the local hooks. */
3312 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3314 record_unwind_current_buffer ();
3315 Fset_buffer (Fwindow_buffer (Qnil));
3318 if (SELECTED_FRAME () != f)
3320 record_unwind_protect (select_frame_norecord, selected_frame);
3321 select_frame_norecord (frame);
3324 /* Look for buffer-local values. */
3326 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3327 for (; CONSP (windows); windows = XCDR (windows))
3329 Lisp_Object window = XCAR (windows);
3330 Lisp_Object buffer = Fwindow_buffer (window);
3331 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3332 buffer)))
3334 ptrdiff_t inner_count = SPECPDL_INDEX ();
3335 record_unwind_protect (select_window_norecord, selected_window);
3336 select_window_norecord (window);
3337 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3338 buffer));
3339 unbind_to (inner_count, Qnil);
3344 run_funs (global_wcch);
3345 unbind_to (count, Qnil);
3348 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3349 Srun_window_configuration_change_hook, 0, 1, 0,
3350 doc: /* Run `window-configuration-change-hook' for FRAME.
3351 If FRAME is omitted or nil, it defaults to the selected frame. */)
3352 (Lisp_Object frame)
3354 run_window_configuration_change_hook (decode_live_frame (frame));
3355 return Qnil;
3358 DEFUN ("run-window-scroll-functions", Frun_window_scroll_functions,
3359 Srun_window_scroll_functions, 0, 1, 0,
3360 doc: /* Run `window-scroll-functions' for WINDOW.
3361 If WINDOW is omitted or nil, it defaults to the selected window. */)
3362 (Lisp_Object window)
3364 if (! NILP (Vwindow_scroll_functions))
3365 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3366 Fmarker_position (decode_live_window (window)->start));
3367 return Qnil;
3370 /* Make WINDOW display BUFFER. RUN_HOOKS_P non-zero means it's allowed
3371 to run hooks. See make_frame for a case where it's not allowed.
3372 KEEP_MARGINS_P non-zero means that the current margins, fringes, and
3373 scroll-bar settings of the window are not reset from the buffer's
3374 local settings. */
3376 void
3377 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3378 bool run_hooks_p, bool keep_margins_p)
3380 struct window *w = XWINDOW (window);
3381 struct buffer *b = XBUFFER (buffer);
3382 ptrdiff_t count = SPECPDL_INDEX ();
3383 bool samebuf = EQ (buffer, w->contents);
3385 wset_buffer (w, buffer);
3387 if (EQ (window, selected_window))
3388 bset_last_selected_window (b, window);
3390 /* Let redisplay errors through. */
3391 b->display_error_modiff = 0;
3393 /* Update time stamps of buffer display. */
3394 if (INTEGERP (BVAR (b, display_count)))
3395 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3396 bset_display_time (b, Fcurrent_time ());
3398 w->window_end_pos = 0;
3399 w->window_end_vpos = 0;
3400 w->last_cursor_vpos = 0;
3402 if (!(keep_margins_p && samebuf))
3403 { /* If we're not actually changing the buffer, don't reset hscroll and
3404 vscroll. This case happens for example when called from
3405 change_frame_size_1, where we use a dummy call to
3406 Fset_window_buffer on the frame's selected window (and no other)
3407 just in order to run window-configuration-change-hook.
3408 Resetting hscroll and vscroll here is problematic for things like
3409 image-mode and doc-view-mode since it resets the image's position
3410 whenever we resize the frame. */
3411 w->hscroll = w->min_hscroll = 0;
3412 w->vscroll = 0;
3413 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3414 set_marker_restricted (w->start,
3415 make_number (b->last_window_start),
3416 buffer);
3417 w->start_at_line_beg = 0;
3418 w->force_start = 0;
3420 /* Maybe we could move this into the `if' but it's not obviously safe and
3421 I doubt it's worth the trouble. */
3422 wset_redisplay (w);
3423 w->update_mode_line = true;
3425 /* We must select BUFFER for running the window-scroll-functions. */
3426 /* We can't check ! NILP (Vwindow_scroll_functions) here
3427 because that might itself be a local variable. */
3428 if (window_initialized)
3430 record_unwind_current_buffer ();
3431 Fset_buffer (buffer);
3434 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3436 if (!keep_margins_p)
3438 /* Set left and right marginal area width etc. from buffer. */
3439 set_window_fringes (w, BVAR (b, left_fringe_width),
3440 BVAR (b, right_fringe_width),
3441 BVAR (b, fringes_outside_margins));
3442 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3443 BVAR (b, vertical_scroll_bar_type), Qnil);
3444 set_window_margins (w, BVAR (b, left_margin_cols),
3445 BVAR (b, right_margin_cols));
3446 apply_window_adjustment (w);
3449 if (run_hooks_p)
3451 if (! NILP (Vwindow_scroll_functions))
3452 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3453 Fmarker_position (w->start));
3454 if (!samebuf)
3455 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3458 unbind_to (count, Qnil);
3461 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3462 doc: /* Make WINDOW display BUFFER-OR-NAME.
3463 WINDOW must be a live window and defaults to the selected one.
3464 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3466 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3467 display margins, fringe widths, and scroll bar settings are preserved;
3468 the default is to reset these from the local settings for BUFFER-OR-NAME
3469 or the frame defaults. Return nil.
3471 This function throws an error when WINDOW is strongly dedicated to its
3472 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3473 already display BUFFER-OR-NAME.
3475 This function runs `window-scroll-functions' before running
3476 `window-configuration-change-hook'. */)
3477 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3479 register Lisp_Object tem, buffer;
3480 register struct window *w = decode_live_window (window);
3482 XSETWINDOW (window, w);
3483 buffer = Fget_buffer (buffer_or_name);
3484 CHECK_BUFFER (buffer);
3485 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3486 error ("Attempt to display deleted buffer");
3488 tem = w->contents;
3489 if (NILP (tem))
3490 error ("Window is deleted");
3491 else
3493 if (!EQ (tem, buffer))
3495 if (EQ (w->dedicated, Qt))
3496 /* WINDOW is strongly dedicated to its buffer, signal an
3497 error. */
3498 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3499 else
3500 /* WINDOW is weakly dedicated to its buffer, reset
3501 dedication. */
3502 wset_dedicated (w, Qnil);
3504 call1 (Qrecord_window_buffer, window);
3507 unshow_buffer (w);
3510 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3512 return Qnil;
3515 static Lisp_Object
3516 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3518 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3521 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3522 0, 1, 0,
3523 doc: /* Force all windows to be updated on next redisplay.
3524 If optional arg OBJECT is a window, force redisplay of that window only.
3525 If OBJECT is a buffer or buffer name, force redisplay of all windows
3526 displaying that buffer. */)
3527 (Lisp_Object object)
3529 if (NILP (object))
3531 windows_or_buffers_changed = 29;
3532 update_mode_lines = 28;
3533 return Qt;
3536 if (WINDOWP (object))
3538 struct window *w = XWINDOW (object);
3539 mark_window_display_accurate (object, 0);
3540 w->update_mode_line = 1;
3541 if (BUFFERP (w->contents))
3542 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3543 update_mode_lines = 29;
3544 return Qt;
3547 if (STRINGP (object))
3548 object = Fget_buffer (object);
3549 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3550 && buffer_window_count (XBUFFER (object)))
3552 /* If buffer is live and shown in at least one window, find
3553 all windows showing this buffer and force update of them. */
3554 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3555 return NILP (object) ? Qnil : Qt;
3558 /* If nothing suitable was found, just return.
3559 We could signal an error, but this feature will typically be used
3560 asynchronously in timers or process sentinels, so we don't. */
3561 return Qnil;
3564 /* Obsolete since 24.3. */
3565 void
3566 temp_output_buffer_show (register Lisp_Object buf)
3568 register struct buffer *old = current_buffer;
3569 register Lisp_Object window;
3570 register struct window *w;
3572 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3574 Fset_buffer (buf);
3575 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3576 BEGV = BEG;
3577 ZV = Z;
3578 SET_PT (BEG);
3579 set_buffer_internal (old);
3581 if (!NILP (Vtemp_buffer_show_function))
3582 call1 (Vtemp_buffer_show_function, buf);
3583 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3585 if (!EQ (XWINDOW (window)->frame, selected_frame))
3586 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3587 Vminibuf_scroll_window = window;
3588 w = XWINDOW (window);
3589 w->hscroll = 0;
3590 w->min_hscroll = 0;
3591 set_marker_restricted_both (w->start, buf, BEG, BEG);
3592 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3594 /* Run temp-buffer-show-hook, with the chosen window selected
3595 and its buffer current. */
3597 ptrdiff_t count = SPECPDL_INDEX ();
3598 Lisp_Object prev_window, prev_buffer;
3599 prev_window = selected_window;
3600 XSETBUFFER (prev_buffer, old);
3602 /* Select the window that was chosen, for running the hook.
3603 Note: Both Fselect_window and select_window_norecord may
3604 set-buffer to the buffer displayed in the window,
3605 so we need to save the current buffer. --stef */
3606 record_unwind_protect (restore_buffer, prev_buffer);
3607 record_unwind_protect (select_window_norecord, prev_window);
3608 Fselect_window (window, Qt);
3609 Fset_buffer (w->contents);
3610 Frun_hooks (1, &Qtemp_buffer_show_hook);
3611 unbind_to (count, Qnil);
3616 /* Make new window, have it replace WINDOW in window-tree, and make
3617 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3618 horizontal child). */
3619 static void
3620 make_parent_window (Lisp_Object window, bool horflag)
3622 Lisp_Object parent;
3623 register struct window *o, *p;
3625 o = XWINDOW (window);
3626 p = allocate_window ();
3627 memcpy ((char *) p + sizeof (struct vectorlike_header),
3628 (char *) o + sizeof (struct vectorlike_header),
3629 word_size * VECSIZE (struct window));
3630 /* P's buffer slot may change from nil to a buffer... */
3631 adjust_window_count (p, 1);
3632 XSETWINDOW (parent, p);
3634 p->sequence_number = ++sequence_number;
3636 replace_window (window, parent, 1);
3638 wset_next (o, Qnil);
3639 wset_prev (o, Qnil);
3640 wset_parent (o, parent);
3641 /* ...but now P becomes an internal window. */
3642 wset_start (p, Qnil);
3643 wset_pointm (p, Qnil);
3644 wset_buffer (p, Qnil);
3645 wset_combination (p, horflag, window);
3646 wset_combination_limit (p, Qnil);
3647 wset_window_parameters (p, Qnil);
3650 /* Make new window from scratch. */
3651 Lisp_Object
3652 make_window (void)
3654 Lisp_Object window;
3655 register struct window *w;
3657 w = allocate_window ();
3658 /* Initialize Lisp data. Note that allocate_window initializes all
3659 Lisp data to nil, so do it only for slots which should not be nil. */
3660 wset_normal_lines (w, make_float (1.0));
3661 wset_normal_cols (w, make_float (1.0));
3662 wset_new_total (w, make_number (0));
3663 wset_new_normal (w, make_number (0));
3664 wset_new_pixel (w, make_number (0));
3665 wset_start (w, Fmake_marker ());
3666 wset_pointm (w, Fmake_marker ());
3667 wset_vertical_scroll_bar_type (w, Qt);
3668 /* These Lisp fields are marked specially so they're not set to nil by
3669 allocate_window. */
3670 wset_prev_buffers (w, Qnil);
3671 wset_next_buffers (w, Qnil);
3673 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3674 non-Lisp data, so do it only for slots which should not be zero. */
3675 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3676 w->left_fringe_width = w->right_fringe_width = -1;
3677 w->mode_line_height = w->header_line_height = -1;
3678 #ifdef HAVE_WINDOW_SYSTEM
3679 w->phys_cursor_type = NO_CURSOR;
3680 w->phys_cursor_width = -1;
3681 #endif
3682 w->sequence_number = ++sequence_number;
3683 w->scroll_bar_width = -1;
3684 w->column_number_displayed = -1;
3686 /* Reset window_list. */
3687 Vwindow_list = Qnil;
3688 /* Return window. */
3689 XSETWINDOW (window, w);
3690 return window;
3693 DEFUN ("set-window-new-pixel", Fset_window_new_pixel, Sset_window_new_pixel, 2, 3, 0,
3694 doc: /* Set new pixel size of WINDOW to SIZE.
3695 WINDOW must be a valid window and defaults to the selected one.
3696 Return SIZE.
3698 Optional argument ADD non-nil means add SIZE to the new pixel size of
3699 WINDOW and return the sum.
3701 Note: This function does not operate on any child windows of WINDOW. */)
3702 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3704 struct window *w = decode_valid_window (window);
3705 EMACS_INT size_min = NILP (add) ? 0 : - XINT (w->new_pixel);
3706 EMACS_INT size_max = size_min + min (INT_MAX, MOST_POSITIVE_FIXNUM);
3708 CHECK_RANGED_INTEGER (size, size_min, size_max);
3709 if (NILP (add))
3710 wset_new_pixel (w, size);
3711 else
3712 wset_new_pixel (w, make_number (XINT (w->new_pixel) + XINT (size)));
3714 return w->new_pixel;
3717 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3718 doc: /* Set new total size of WINDOW to SIZE.
3719 WINDOW must be a valid window and defaults to the selected one.
3720 Return SIZE.
3722 Optional argument ADD non-nil means add SIZE to the new total size of
3723 WINDOW and return the sum.
3725 Note: This function does not operate on any child windows of WINDOW. */)
3726 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3728 struct window *w = decode_valid_window (window);
3730 CHECK_NUMBER (size);
3731 if (NILP (add))
3732 wset_new_total (w, size);
3733 else
3734 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3736 return w->new_total;
3739 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3740 doc: /* Set new normal size of WINDOW to SIZE.
3741 WINDOW must be a valid window and defaults to the selected one.
3742 Return SIZE.
3744 Note: This function does not operate on any child windows of WINDOW. */)
3745 (Lisp_Object window, Lisp_Object size)
3747 wset_new_normal (decode_valid_window (window), size);
3748 return size;
3751 /* Return 1 if setting w->pixel_height (w->pixel_width if HORFLAG is
3752 non-zero) to w->new_pixel would result in correct heights (widths)
3753 for window W and recursively all child windows of W.
3755 Note: This function does not check any of `window-fixed-size-p',
3756 `window-min-height' or `window-min-width'. It does check that window
3757 sizes do not drop below one line (two columns). */
3758 static int
3759 window_resize_check (struct window *w, bool horflag)
3761 struct frame *f = XFRAME (w->frame);
3762 struct window *c;
3764 if (WINDOW_VERTICAL_COMBINATION_P (w))
3765 /* W is a vertical combination. */
3767 c = XWINDOW (w->contents);
3768 if (horflag)
3769 /* All child windows of W must have the same width as W. */
3771 while (c)
3773 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3774 || !window_resize_check (c, horflag))
3775 return 0;
3777 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3780 return 1;
3782 else
3783 /* The sum of the heights of the child windows of W must equal
3784 W's height. */
3786 int remaining_pixels = XINT (w->new_pixel);
3788 while (c)
3790 if (!window_resize_check (c, horflag))
3791 return 0;
3793 remaining_pixels -= XINT (c->new_pixel);
3794 if (remaining_pixels < 0)
3795 return 0;
3796 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3799 return remaining_pixels == 0;
3802 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3803 /* W is a horizontal combination. */
3805 c = XWINDOW (w->contents);
3806 if (horflag)
3807 /* The sum of the widths of the child windows of W must equal W's
3808 width. */
3810 int remaining_pixels = XINT (w->new_pixel);
3812 while (c)
3814 if (!window_resize_check (c, horflag))
3815 return 0;
3817 remaining_pixels -= XINT (c->new_pixel);
3818 if (remaining_pixels < 0)
3819 return 0;
3820 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3823 return remaining_pixels == 0;
3825 else
3826 /* All child windows of W must have the same height as W. */
3828 while (c)
3830 if (XINT (c->new_pixel) != XINT (w->new_pixel)
3831 || !window_resize_check (c, horflag))
3832 return 0;
3834 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3837 return 1;
3840 else
3841 /* A leaf window. Make sure it's not too small. The following
3842 hardcodes the values of `window-safe-min-width' (2) and
3843 `window-safe-min-height' (1) which are defined in window.el. */
3844 return (XINT (w->new_pixel) >= (horflag
3845 ? (2 * FRAME_COLUMN_WIDTH (f))
3846 : FRAME_LINE_HEIGHT (f)));
3850 /* Set w->pixel_height (w->pixel_height if HORIZONTAL is non-zero) to
3851 w->new_pixel for window W and recursively all child windows of W.
3852 Also calculate and assign the new vertical (horizontal) pixel start
3853 positions of each of these windows.
3855 This function does not perform any error checks. Make sure you have
3856 run window_resize_check on W before applying this function. */
3857 static void
3858 window_resize_apply (struct window *w, bool horflag)
3860 struct window *c;
3861 int edge;
3862 int unit = (horflag
3863 ? FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w))
3864 : FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
3866 /* Note: Assigning new_normal requires that the new total size of the
3867 parent window has been set *before*. */
3868 if (horflag)
3870 w->pixel_width = XFASTINT (w->new_pixel);
3871 w->total_cols = w->pixel_width / unit;
3872 if (NUMBERP (w->new_normal))
3873 wset_normal_cols (w, w->new_normal);
3875 edge = w->pixel_left;
3877 else
3879 w->pixel_height = XFASTINT (w->new_pixel);
3880 w->total_lines = w->pixel_height / unit;
3881 if (NUMBERP (w->new_normal))
3882 wset_normal_lines (w, w->new_normal);
3884 edge = w->pixel_top;
3887 if (WINDOW_VERTICAL_COMBINATION_P (w))
3888 /* W is a vertical combination. */
3890 c = XWINDOW (w->contents);
3891 while (c)
3893 if (horflag)
3895 c->pixel_left = edge;
3896 c->left_col = edge / unit;
3898 else
3900 c->pixel_top = edge;
3901 c->top_line = edge / unit;
3903 window_resize_apply (c, horflag);
3904 if (!horflag)
3905 edge = edge + c->pixel_height;
3907 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3910 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3911 /* W is a horizontal combination. */
3913 c = XWINDOW (w->contents);
3914 while (c)
3916 if (horflag)
3918 c->pixel_left = edge;
3919 c->left_col = edge / unit;
3921 else
3923 c->pixel_top = edge;
3924 c->top_line = edge / unit;
3927 window_resize_apply (c, horflag);
3928 if (horflag)
3929 edge = edge + c->pixel_width;
3931 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3934 else
3936 adjust_window_margins (w);
3937 /* Bug#15957. */
3938 w->window_end_valid = 0;
3943 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3944 w->new_total for window W and recursively all child windows of W.
3945 Also calculate and assign the new vertical (horizontal) start
3946 positions of each of these windows. */
3947 static void
3948 window_resize_apply_total (struct window *w, bool horflag)
3950 struct window *c;
3951 int edge;
3953 /* Note: Assigning new_normal requires that the new total size of the
3954 parent window has been set *before*. */
3955 if (horflag)
3957 w->total_cols = XFASTINT (w->new_total);
3958 edge = w->left_col;
3960 else
3962 w->total_lines = XFASTINT (w->new_total);
3963 edge = w->top_line;
3966 if (WINDOW_VERTICAL_COMBINATION_P (w))
3967 /* W is a vertical combination. */
3969 c = XWINDOW (w->contents);
3970 while (c)
3972 if (horflag)
3973 c->left_col = edge;
3974 else
3975 c->top_line = edge;
3977 window_resize_apply_total (c, horflag);
3978 if (!horflag)
3979 edge = edge + c->total_lines;
3981 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3984 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3985 /* W is a horizontal combination. */
3987 c = XWINDOW (w->contents);
3988 while (c)
3990 if (horflag)
3991 c->left_col = edge;
3992 else
3993 c->top_line = edge;
3995 window_resize_apply_total (c, horflag);
3996 if (horflag)
3997 edge = edge + c->total_cols;
3999 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4004 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
4005 doc: /* Apply requested size values for window-tree of FRAME.
4006 If FRAME is omitted or nil, it defaults to the selected frame.
4008 Optional argument HORIZONTAL omitted or nil means apply requested
4009 height values. HORIZONTAL non-nil means apply requested width values.
4011 This function checks whether the requested values sum up to a valid
4012 window layout, recursively assigns the new sizes of all child windows
4013 and calculates and assigns the new start positions of these windows.
4015 Note: This function does not check any of `window-fixed-size-p',
4016 `window-min-height' or `window-min-width'. All these checks have to
4017 be applied on the Elisp level. */)
4018 (Lisp_Object frame, Lisp_Object horizontal)
4020 struct frame *f = decode_live_frame (frame);
4021 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4022 bool horflag = !NILP (horizontal);
4024 if (!window_resize_check (r, horflag)
4025 || (XINT (r->new_pixel)
4026 != (horflag ? r->pixel_width : r->pixel_height)))
4027 return Qnil;
4029 block_input ();
4030 window_resize_apply (r, horflag);
4032 fset_redisplay (f);
4033 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4035 adjust_frame_glyphs (f);
4036 unblock_input ();
4038 return Qt;
4042 DEFUN ("window-resize-apply-total", Fwindow_resize_apply_total, Swindow_resize_apply_total, 0, 2, 0,
4043 doc: /* Apply requested total size values for window-tree of FRAME.
4044 If FRAME is omitted or nil, it defaults to the selected frame.
4046 This function does not assign pixel or normal size values. You should
4047 have run `window-resize-apply' before running this.
4049 Optional argument HORIZONTAL omitted or nil means apply requested
4050 height values. HORIZONTAL non-nil means apply requested width
4051 values. */)
4052 (Lisp_Object frame, Lisp_Object horizontal)
4054 struct frame *f = decode_live_frame (frame);
4055 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
4057 block_input ();
4058 /* Necessary when deleting the top-/or leftmost window. */
4059 r->left_col = 0;
4060 r->top_line = FRAME_TOP_MARGIN (f);
4061 window_resize_apply_total (r, !NILP (horizontal));
4062 /* Handle the mini window. */
4063 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4065 struct window *m = XWINDOW (f->minibuffer_window);
4067 if (NILP (horizontal))
4069 m->top_line = r->top_line + r->total_lines;
4070 m->total_lines = XFASTINT (m->new_total);
4072 else
4073 m->total_cols = XFASTINT (m->new_total);
4076 unblock_input ();
4078 return Qt;
4082 /* Resize frame F's windows when number of lines of F is set to SIZE.
4083 HORFLAG 1 means resize windows when number of columns of F is set to
4084 SIZE. PIXELWISE 1 means to interpret SIZE as pixels.
4086 This function can delete all windows but the selected one in order to
4087 satisfy the request. The result will be meaningful if and only if
4088 F's windows have meaningful sizes when you call this. */
4089 void
4090 resize_frame_windows (struct frame *f, int size, bool horflag, bool pixelwise)
4092 Lisp_Object root = f->root_window;
4093 struct window *r = XWINDOW (root);
4094 Lisp_Object mini = f->minibuffer_window;
4095 struct window *m;
4096 /* old_size is the old size of the frame's root window. */
4097 int old_size = horflag ? r->total_cols : r->total_lines;
4098 int old_pixel_size = horflag ? r->pixel_width : r->pixel_height;
4099 /* new_size is the new size of the frame's root window. */
4100 int new_size, new_pixel_size;
4101 int unit = horflag ? FRAME_COLUMN_WIDTH (f) : FRAME_LINE_HEIGHT (f);
4103 /* Don't let the size drop below one unit. This is more comforting
4104 when we are called from x_set_tool_bar_lines since the latter may
4105 have implicitly given us a zero or negative height. */
4106 if (pixelwise)
4108 /* Note: This does not include the size for internal borders
4109 since these are not part of the frame's text area. */
4110 new_pixel_size = max (horflag
4111 ? size
4112 : (size
4113 - FRAME_TOP_MARGIN_HEIGHT (f)
4114 - ((FRAME_HAS_MINIBUF_P (f)
4115 && !FRAME_MINIBUF_ONLY_P (f))
4116 ? FRAME_LINE_HEIGHT (f) : 0)),
4117 unit);
4118 new_size = new_pixel_size / unit;
4120 else
4122 new_size = max (horflag
4123 ? size
4124 : (size
4125 - FRAME_TOP_MARGIN (f)
4126 - ((FRAME_HAS_MINIBUF_P (f)
4127 && !FRAME_MINIBUF_ONLY_P (f))
4128 ? 1 : 0)),
4130 new_pixel_size = new_size * unit;
4133 r->top_line = FRAME_TOP_MARGIN (f);
4134 r->pixel_top = FRAME_TOP_MARGIN_HEIGHT (f);
4136 if (new_pixel_size == old_pixel_size)
4138 else if (WINDOW_LEAF_P (r))
4139 /* For a leaf root window just set the size. */
4140 if (horflag)
4142 r->total_cols = new_size;
4143 r->pixel_width = new_pixel_size;
4145 else
4147 r->total_lines = new_size;
4148 r->pixel_height = new_pixel_size;
4150 else
4152 Lisp_Object delta;
4154 if (pixelwise)
4155 XSETINT (delta, new_pixel_size - old_pixel_size);
4156 else
4157 XSETINT (delta, new_size - old_size);
4159 /* Try a "normal" resize first. */
4160 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil,
4161 pixelwise ? Qt : Qnil);
4162 if (window_resize_check (r, horflag)
4163 && new_pixel_size == XINT (r->new_pixel))
4165 window_resize_apply (r, horflag);
4166 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4168 else
4170 /* Try with "reasonable" minimum sizes next. */
4171 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt,
4172 pixelwise ? Qt : Qnil);
4173 if (window_resize_check (r, horflag)
4174 && new_pixel_size == XINT (r->new_pixel))
4176 window_resize_apply (r, horflag);
4177 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4179 else
4181 /* Finally, try with "safe" minimum sizes. */
4182 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe,
4183 pixelwise ? Qt : Qnil);
4184 if (window_resize_check (r, horflag)
4185 && new_pixel_size == XINT (r->new_pixel))
4187 window_resize_apply (r, horflag);
4188 window_pixel_to_total (r->frame, horflag ? Qt : Qnil);
4190 #if 0 /* Let's try without killing other windows. */
4191 else
4193 /* We lost. Delete all windows but the frame's
4194 selected one. */
4195 root = f->selected_window;
4196 Fdelete_other_windows_internal (root, Qnil);
4197 if (horflag)
4199 XWINDOW (root)->total_cols = new_size;
4200 XWINDOW (root)->pixel_width = new_pixel_size;
4202 else
4204 XWINDOW (root)->total_lines = new_size;
4205 XWINDOW (root)->pixel_height = new_pixel_size;
4208 #endif /* 0 */
4213 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4215 m = XWINDOW (mini);
4216 if (horflag)
4218 m->total_cols = size;
4219 m->pixel_width = new_pixel_size;
4221 else
4223 /* Are we sure we always want 1 line here? */
4224 m->total_lines = 1;
4225 m->pixel_height = FRAME_LINE_HEIGHT (f);
4226 m->top_line = r->top_line + r->total_lines;
4227 m->pixel_top = r->pixel_top + r->pixel_height;
4231 fset_redisplay (f);
4235 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
4236 doc: /* Split window OLD.
4237 Second argument PIXEL-SIZE specifies the number of pixels of the
4238 new window. In any case TOTAL-SIZE must be a positive integer.
4240 Third argument SIDE nil (or `below') specifies that the new window shall
4241 be located below WINDOW. SIDE `above' means the new window shall be
4242 located above WINDOW. In both cases PIXEL-SIZE specifies the pixel
4243 height of the new window including space reserved for the mode and/or
4244 header line.
4246 SIDE t (or `right') specifies that the new window shall be located on
4247 the right side of WINDOW. SIDE `left' means the new window shall be
4248 located on the left of WINDOW. In both cases PIXEL-SIZE specifies the
4249 width of the new window including space reserved for fringes and the
4250 scrollbar or a divider column.
4252 Fourth argument NORMAL-SIZE specifies the normal size of the new window
4253 according to the SIDE argument.
4255 The new pixel and normal sizes of all involved windows must have been
4256 set correctly. See the code of `split-window' for how this is done. */)
4257 (Lisp_Object old, Lisp_Object pixel_size, Lisp_Object side, Lisp_Object normal_size)
4259 /* OLD (*o) is the window we have to split. (*p) is either OLD's
4260 parent window or an internal window we have to install as OLD's new
4261 parent. REFERENCE (*r) must denote a live window, or is set to OLD
4262 provided OLD is a leaf window, or to the frame's selected window.
4263 NEW (*n) is the new window created with some parameters taken from
4264 REFERENCE (*r). */
4265 register Lisp_Object new, frame, reference;
4266 register struct window *o, *p, *n, *r, *c;
4267 struct frame *f;
4268 bool horflag
4269 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
4270 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
4271 int combination_limit = 0, sum = 0;
4272 int total_size;
4274 CHECK_WINDOW (old);
4275 o = XWINDOW (old);
4276 frame = WINDOW_FRAME (o);
4277 f = XFRAME (frame);
4279 CHECK_NUMBER (pixel_size);
4280 total_size
4281 = XINT (pixel_size) / (horflag
4282 ? FRAME_COLUMN_WIDTH (f)
4283 : FRAME_LINE_HEIGHT (f));
4285 /* Set combination_limit to 1 if we have to make a new parent window.
4286 We do that if either `window-combination-limit' is t, or OLD has no
4287 parent, or OLD is ortho-combined. */
4288 combination_limit =
4289 EQ (Vwindow_combination_limit, Qt)
4290 || NILP (o->parent)
4291 || (horflag
4292 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
4293 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
4295 /* We need a live reference window to initialize some parameters. */
4296 if (WINDOW_LIVE_P (old))
4297 /* OLD is live, use it as reference window. */
4298 reference = old;
4299 else
4300 /* Use the frame's selected window as reference window. */
4301 reference = FRAME_SELECTED_WINDOW (f);
4302 r = XWINDOW (reference);
4304 /* The following bugs are caught by `split-window'. */
4305 if (MINI_WINDOW_P (o))
4306 error ("Attempt to split minibuffer window");
4307 else if (total_size < (horflag ? 2 : 1))
4308 error ("Size of new window too small (after split)");
4309 else if (!combination_limit && !NILP (Vwindow_combination_resize))
4310 /* `window-combination-resize' non-nil means try to resize OLD's siblings
4311 proportionally. */
4313 p = XWINDOW (o->parent);
4314 /* Temporarily pretend we split the parent window. */
4315 wset_new_pixel
4316 (p, make_number ((horflag ? p->pixel_width : p->pixel_height)
4317 - XINT (pixel_size)));
4318 if (!window_resize_check (p, horflag))
4319 error ("Window sizes don't fit");
4320 else
4321 /* Undo the temporary pretension. */
4322 wset_new_pixel (p, make_number (horflag ? p->pixel_width : p->pixel_height));
4324 else
4326 if (!window_resize_check (o, horflag))
4327 error ("Resizing old window failed");
4328 else if (XINT (pixel_size) + XINT (o->new_pixel)
4329 != (horflag ? o->pixel_width : o->pixel_height))
4330 error ("Sum of sizes of old and new window don't fit");
4333 /* This is our point of no return. */
4334 if (combination_limit)
4336 /* Save the old value of o->normal_cols/lines. It gets corrupted
4337 by make_parent_window and we need it below for assigning it to
4338 p->new_normal. */
4339 Lisp_Object new_normal
4340 = horflag ? o->normal_cols : o->normal_lines;
4342 make_parent_window (old, horflag);
4343 p = XWINDOW (o->parent);
4344 if (EQ (Vwindow_combination_limit, Qt))
4345 /* Store t in the new parent's combination_limit slot to avoid
4346 that its children get merged into another window. */
4347 wset_combination_limit (p, Qt);
4348 /* These get applied below. */
4349 wset_new_pixel
4350 (p, make_number (horflag ? o->pixel_width : o->pixel_height));
4351 wset_new_total
4352 (p, make_number (horflag ? o->total_cols : o->total_lines));
4353 wset_new_normal (p, new_normal);
4355 else
4356 p = XWINDOW (o->parent);
4358 fset_redisplay (f);
4359 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4360 new = make_window ();
4361 n = XWINDOW (new);
4362 wset_frame (n, frame);
4363 wset_parent (n, o->parent);
4365 if (EQ (side, Qabove) || EQ (side, Qleft))
4367 wset_prev (n, o->prev);
4368 if (NILP (n->prev))
4369 wset_combination (p, horflag, new);
4370 else
4371 wset_next (XWINDOW (n->prev), new);
4372 wset_next (n, old);
4373 wset_prev (o, new);
4375 else
4377 wset_next (n, o->next);
4378 if (!NILP (n->next))
4379 wset_prev (XWINDOW (n->next), new);
4380 wset_prev (n, old);
4381 wset_next (o, new);
4384 n->window_end_valid = 0;
4385 n->last_cursor_vpos = 0;
4387 /* Get special geometry settings from reference window. */
4388 n->left_margin_cols = r->left_margin_cols;
4389 n->right_margin_cols = r->right_margin_cols;
4390 n->left_fringe_width = r->left_fringe_width;
4391 n->right_fringe_width = r->right_fringe_width;
4392 n->fringes_outside_margins = r->fringes_outside_margins;
4393 n->scroll_bar_width = r->scroll_bar_width;
4394 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
4396 /* Directly assign orthogonal coordinates and sizes. */
4397 if (horflag)
4399 n->pixel_top = o->pixel_top;
4400 n->top_line = o->top_line;
4401 n->pixel_height = o->pixel_height;
4402 n->total_lines = o->total_lines;
4404 else
4406 n->pixel_left = o->pixel_left;
4407 n->left_col = o->left_col;
4408 n->pixel_width = o->pixel_width;
4409 n->total_cols = o->total_cols;
4412 /* Iso-coordinates and sizes are assigned by window_resize_apply,
4413 get them ready here. */
4414 wset_new_pixel (n, pixel_size);
4415 c = XWINDOW (p->contents);
4416 while (c)
4418 if (c != n)
4419 sum = sum + XINT (c->new_total);
4420 c = NILP (c->next) ? 0 : XWINDOW (c->next);
4422 wset_new_total (n, make_number ((horflag
4423 ? p->total_cols
4424 : p->total_lines)
4425 - sum));
4426 wset_new_normal (n, normal_size);
4428 block_input ();
4429 window_resize_apply (p, horflag);
4430 adjust_frame_glyphs (f);
4431 /* Set buffer of NEW to buffer of reference window. Don't run
4432 any hooks. */
4433 set_window_buffer (new, r->contents, 0, 1);
4434 unblock_input ();
4436 /* Maybe we should run the scroll functions in Elisp (which already
4437 runs the configuration change hook). */
4438 if (! NILP (Vwindow_scroll_functions))
4439 run_hook_with_args_2 (Qwindow_scroll_functions, new,
4440 Fmarker_position (n->start));
4441 /* Return NEW. */
4442 return new;
4446 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4447 doc: /* Remove WINDOW from its frame.
4448 WINDOW defaults to the selected window. Return nil.
4449 Signal an error when WINDOW is the only window on its frame. */)
4450 (register Lisp_Object window)
4452 register Lisp_Object parent, sibling, frame, root;
4453 struct window *w, *p, *s, *r;
4454 struct frame *f;
4455 bool horflag, before_sibling = 0;
4457 w = decode_any_window (window);
4458 XSETWINDOW (window, w);
4459 if (NILP (w->contents))
4460 /* It's a no-op to delete an already deleted window. */
4461 return Qnil;
4463 parent = w->parent;
4464 if (NILP (parent))
4465 /* Never delete a minibuffer or frame root window. */
4466 error ("Attempt to delete minibuffer or sole ordinary window");
4467 else if (NILP (w->prev) && NILP (w->next))
4468 /* Rather bow out here, this case should be handled on the Elisp
4469 level. */
4470 error ("Attempt to delete sole window of parent");
4472 p = XWINDOW (parent);
4473 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4475 frame = WINDOW_FRAME (w);
4476 f = XFRAME (frame);
4478 root = FRAME_ROOT_WINDOW (f);
4479 r = XWINDOW (root);
4481 /* Unlink WINDOW from window tree. */
4482 if (NILP (w->prev))
4483 /* Get SIBLING below (on the right of) WINDOW. */
4485 /* before_sibling 1 means WINDOW is the first child of its
4486 parent and thus before the sibling. */
4487 before_sibling = 1;
4488 sibling = w->next;
4489 s = XWINDOW (sibling);
4490 wset_prev (s, Qnil);
4491 wset_combination (p, horflag, sibling);
4493 else
4494 /* Get SIBLING above (on the left of) WINDOW. */
4496 sibling = w->prev;
4497 s = XWINDOW (sibling);
4498 wset_next (s, w->next);
4499 if (!NILP (s->next))
4500 wset_prev (XWINDOW (s->next), sibling);
4503 if (window_resize_check (r, horflag)
4504 && (XINT (r->new_pixel)
4505 == (horflag ? r->pixel_width : r->pixel_height)))
4506 /* We can delete WINDOW now. */
4509 /* Block input. */
4510 block_input ();
4511 window_resize_apply (p, horflag);
4512 /* If this window is referred to by the dpyinfo's mouse
4513 highlight, invalidate that slot to be safe (Bug#9904). */
4514 if (!FRAME_INITIAL_P (f))
4516 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4518 if (EQ (hlinfo->mouse_face_window, window))
4519 hlinfo->mouse_face_window = Qnil;
4522 fset_redisplay (f);
4523 Vwindow_list = Qnil;
4524 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4526 wset_next (w, Qnil); /* Don't delete w->next too. */
4527 free_window_matrices (w);
4529 if (WINDOWP (w->contents))
4531 delete_all_child_windows (w->contents);
4532 wset_combination (w, 0, Qnil);
4534 else
4536 unshow_buffer (w);
4537 unchain_marker (XMARKER (w->pointm));
4538 unchain_marker (XMARKER (w->start));
4539 wset_buffer (w, Qnil);
4542 if (NILP (s->prev) && NILP (s->next))
4543 /* A matrjoshka where SIBLING has become the only child of
4544 PARENT. */
4546 /* Put SIBLING into PARENT's place. */
4547 replace_window (parent, sibling, 0);
4548 /* Have SIBLING inherit the following three slot values from
4549 PARENT (the combination_limit slot is not inherited). */
4550 wset_normal_cols (s, p->normal_cols);
4551 wset_normal_lines (s, p->normal_lines);
4552 /* Mark PARENT as deleted. */
4553 wset_combination (p, 0, Qnil);
4554 /* Try to merge SIBLING into its new parent. */
4555 recombine_windows (sibling);
4558 adjust_frame_glyphs (f);
4560 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4561 /* We deleted the frame's selected window. */
4563 /* Use the frame's first window as fallback ... */
4564 Lisp_Object new_selected_window = Fframe_first_window (frame);
4565 /* ... but preferably use its most recently used window. */
4566 Lisp_Object mru_window;
4568 /* `get-mru-window' might fail for some reason so play it safe
4569 - promote the first window _without recording it_ first. */
4570 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4571 Fselect_window (new_selected_window, Qt);
4572 else
4573 fset_selected_window (f, new_selected_window);
4575 unblock_input ();
4577 /* Now look whether `get-mru-window' gets us something. */
4578 mru_window = call1 (Qget_mru_window, frame);
4579 if (WINDOW_LIVE_P (mru_window)
4580 && EQ (XWINDOW (mru_window)->frame, frame))
4581 new_selected_window = mru_window;
4583 /* If all ended up well, we now promote the mru window. */
4584 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4585 Fselect_window (new_selected_window, Qnil);
4586 else
4587 fset_selected_window (f, new_selected_window);
4589 else
4590 unblock_input ();
4592 /* Must be run by the caller:
4593 run_window_configuration_change_hook (f); */
4595 else
4596 /* We failed: Relink WINDOW into window tree. */
4598 if (before_sibling)
4600 wset_prev (s, window);
4601 wset_combination (p, horflag, window);
4603 else
4605 wset_next (s, window);
4606 if (!NILP (w->next))
4607 wset_prev (XWINDOW (w->next), window);
4609 error ("Deletion failed");
4612 return Qnil;
4615 /***********************************************************************
4616 Resizing Mini-Windows
4617 ***********************************************************************/
4619 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4620 can. */
4621 void
4622 grow_mini_window (struct window *w, int delta, bool pixelwise)
4624 struct frame *f = XFRAME (w->frame);
4625 struct window *r;
4626 Lisp_Object root, height;
4627 int line_height, pixel_height;
4629 eassert (MINI_WINDOW_P (w));
4630 eassert (delta >= 0);
4632 if (delta > 0)
4634 root = FRAME_ROOT_WINDOW (f);
4635 r = XWINDOW (root);
4636 height = call3 (Qwindow_resize_root_window_vertically,
4637 root, make_number (- delta), pixelwise ? Qt : Qnil);
4638 if (INTEGERP (height) && window_resize_check (r, 0))
4640 block_input ();
4641 window_resize_apply (r, 0);
4643 if (pixelwise)
4645 pixel_height = min (-XINT (height), INT_MAX - w->pixel_height);
4646 line_height = pixel_height / FRAME_LINE_HEIGHT (f);
4648 else
4650 line_height = min (-XINT (height),
4651 ((INT_MAX - w->pixel_height)
4652 / FRAME_LINE_HEIGHT (f)));
4653 pixel_height = line_height * FRAME_LINE_HEIGHT (f);
4656 /* Grow the mini-window. */
4657 w->pixel_top = r->pixel_top + r->pixel_height;
4658 w->top_line = r->top_line + r->total_lines;
4659 /* Make sure the mini-window has always at least one line. */
4660 w->pixel_height = max (w->pixel_height + pixel_height,
4661 FRAME_LINE_HEIGHT (f));
4662 w->total_lines = max (w->total_lines + line_height, 1);
4664 /* Enforce full redisplay of the frame. */
4665 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4666 fset_redisplay (f);
4667 adjust_frame_glyphs (f);
4668 unblock_input ();
4673 /* Shrink mini-window W to one line. */
4674 void
4675 shrink_mini_window (struct window *w, bool pixelwise)
4677 struct frame *f = XFRAME (w->frame);
4678 struct window *r;
4679 Lisp_Object root, delta;
4680 EMACS_INT height, unit;
4682 eassert (MINI_WINDOW_P (w));
4684 height = pixelwise ? w->pixel_height : w->total_lines;
4685 unit = pixelwise ? FRAME_LINE_HEIGHT (f) : 1;
4686 if (height > unit)
4688 root = FRAME_ROOT_WINDOW (f);
4689 r = XWINDOW (root);
4690 delta = call3 (Qwindow_resize_root_window_vertically,
4691 root, make_number (height - unit),
4692 pixelwise ? Qt : Qnil);
4693 if (INTEGERP (delta) && window_resize_check (r, 0))
4695 block_input ();
4696 window_resize_apply (r, 0);
4698 /* Shrink the mini-window. */
4699 w->top_line = r->top_line + r->total_lines;
4700 w->total_lines = 1;
4701 w->pixel_top = r->pixel_top + r->pixel_height;
4702 w->pixel_height = FRAME_LINE_HEIGHT (f);
4703 /* Enforce full redisplay of the frame. */
4704 /* FIXME: Shouldn't window--resize-root-window-vertically do it? */
4705 fset_redisplay (f);
4706 adjust_frame_glyphs (f);
4707 unblock_input ();
4709 /* If the above failed for whatever strange reason we must make a
4710 one window frame here. The same routine will be needed when
4711 shrinking the frame (and probably when making the initial
4712 *scratch* window). For the moment leave things as they are. */
4716 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4717 doc: /* Resize minibuffer window WINDOW. */)
4718 (Lisp_Object window)
4720 struct window *w = XWINDOW (window);
4721 struct window *r;
4722 struct frame *f;
4723 int height;
4725 CHECK_WINDOW (window);
4726 f = XFRAME (w->frame);
4728 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4729 error ("Not a valid minibuffer window");
4730 else if (FRAME_MINIBUF_ONLY_P (f))
4731 error ("Cannot resize a minibuffer-only frame");
4733 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4734 height = r->pixel_height + w->pixel_height;
4735 if (window_resize_check (r, 0)
4736 && XINT (w->new_pixel) > 0
4737 && height == XINT (r->new_pixel) + XINT (w->new_pixel))
4739 block_input ();
4740 window_resize_apply (r, 0);
4742 w->total_lines = XFASTINT (w->new_total);
4743 w->top_line = r->top_line + r->total_lines;
4744 w->pixel_height = XFASTINT (w->new_pixel);
4745 w->pixel_top = r->pixel_top + r->pixel_height;
4747 fset_redisplay (f);
4748 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4749 adjust_frame_glyphs (f);
4750 unblock_input ();
4751 return Qt;
4753 else
4754 error ("Failed to resize minibuffer window");
4757 /* Mark window cursors off for all windows in the window tree rooted
4758 at W by setting their phys_cursor_on_p flag to zero. Called from
4759 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4760 the frame are cleared. */
4762 void
4763 mark_window_cursors_off (struct window *w)
4765 while (w)
4767 if (WINDOWP (w->contents))
4768 mark_window_cursors_off (XWINDOW (w->contents));
4769 else
4770 w->phys_cursor_on_p = 0;
4772 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4777 /* Return number of lines of text (not counting mode lines) in W. */
4780 window_internal_height (struct window *w)
4782 int ht = w->total_lines;
4784 if (!MINI_WINDOW_P (w))
4786 if (!NILP (w->parent)
4787 || WINDOWP (w->contents)
4788 || !NILP (w->next)
4789 || !NILP (w->prev)
4790 || WINDOW_WANTS_MODELINE_P (w))
4791 --ht;
4793 if (WINDOW_WANTS_HEADER_LINE_P (w))
4794 --ht;
4797 return ht;
4800 /************************************************************************
4801 Window Scrolling
4802 ***********************************************************************/
4804 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4805 N screen-fulls, which is defined as the height of the window minus
4806 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4807 instead. Negative values of N mean scroll down. NOERROR non-zero
4808 means don't signal an error if we try to move over BEGV or ZV,
4809 respectively. */
4811 static void
4812 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4814 immediate_quit = 1;
4815 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4817 wset_redisplay (XWINDOW (window));
4819 /* If we must, use the pixel-based version which is much slower than
4820 the line-based one but can handle varying line heights. */
4821 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4822 window_scroll_pixel_based (window, n, whole, noerror);
4823 else
4824 window_scroll_line_based (window, n, whole, noerror);
4826 /* Bug#15957. */
4827 XWINDOW (window)->window_end_valid = 0;
4828 immediate_quit = 0;
4832 /* Implementation of window_scroll that works based on pixel line
4833 heights. See the comment of window_scroll for parameter
4834 descriptions. */
4836 static void
4837 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4839 struct it it;
4840 struct window *w = XWINDOW (window);
4841 struct text_pos start;
4842 int this_scroll_margin;
4843 /* True if we fiddled the window vscroll field without really scrolling. */
4844 int vscrolled = 0;
4845 int x, y, rtop, rbot, rowh, vpos;
4846 void *itdata = NULL;
4847 int window_total_lines;
4848 int frame_line_height = default_line_pixel_height (w);
4850 SET_TEXT_POS_FROM_MARKER (start, w->start);
4851 /* Scrolling a minibuffer window via scroll bar when the echo area
4852 shows long text sometimes resets the minibuffer contents behind
4853 our backs. */
4854 if (CHARPOS (start) > ZV)
4855 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4857 /* If PT is not visible in WINDOW, move back one half of
4858 the screen. Allow PT to be partially visible, otherwise
4859 something like (scroll-down 1) with PT in the line before
4860 the partially visible one would recenter. */
4862 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4864 itdata = bidi_shelve_cache ();
4865 /* Move backward half the height of the window. Performance note:
4866 vmotion used here is about 10% faster, but would give wrong
4867 results for variable height lines. */
4868 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4869 it.current_y = it.last_visible_y;
4870 move_it_vertically_backward (&it, window_box_height (w) / 2);
4872 /* The function move_iterator_vertically may move over more than
4873 the specified y-distance. If it->w is small, e.g. a
4874 mini-buffer window, we may end up in front of the window's
4875 display area. This is the case when Start displaying at the
4876 start of the line containing PT in this case. */
4877 if (it.current_y <= 0)
4879 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4880 move_it_vertically_backward (&it, 0);
4881 it.current_y = 0;
4884 start = it.current.pos;
4885 bidi_unshelve_cache (itdata, 0);
4887 else if (auto_window_vscroll_p)
4889 if (rtop || rbot) /* partially visible */
4891 int px;
4892 int dy = frame_line_height;
4893 if (whole)
4894 dy = max ((window_box_height (w)
4895 - next_screen_context_lines * dy),
4896 dy);
4897 dy *= n;
4899 if (n < 0)
4901 /* Only vscroll backwards if already vscrolled forwards. */
4902 if (w->vscroll < 0 && rtop > 0)
4904 px = max (0, -w->vscroll - min (rtop, -dy));
4905 Fset_window_vscroll (window, make_number (px), Qt);
4906 return;
4909 if (n > 0)
4911 /* Do vscroll if already vscrolled or only display line. */
4912 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4914 px = max (0, -w->vscroll + min (rbot, dy));
4915 Fset_window_vscroll (window, make_number (px), Qt);
4916 return;
4919 /* Maybe modify window start instead of scrolling. */
4920 if (rbot > 0 || w->vscroll < 0)
4922 ptrdiff_t spos;
4924 Fset_window_vscroll (window, make_number (0), Qt);
4925 /* If there are other text lines above the current row,
4926 move window start to current row. Else to next row. */
4927 if (rbot > 0)
4928 spos = XINT (Fline_beginning_position (Qnil));
4929 else
4930 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4931 set_marker_restricted (w->start, make_number (spos),
4932 w->contents);
4933 w->start_at_line_beg = 1;
4934 w->update_mode_line = 1;
4935 /* Set force_start so that redisplay_window will run the
4936 window-scroll-functions. */
4937 w->force_start = 1;
4938 return;
4942 /* Cancel previous vscroll. */
4943 Fset_window_vscroll (window, make_number (0), Qt);
4946 itdata = bidi_shelve_cache ();
4947 /* If scroll_preserve_screen_position is non-nil, we try to set
4948 point in the same window line as it is now, so get that line. */
4949 if (!NILP (Vscroll_preserve_screen_position))
4951 /* We preserve the goal pixel coordinate across consecutive
4952 calls to scroll-up, scroll-down and other commands that
4953 have the `scroll-command' property. This avoids the
4954 possibility of point becoming "stuck" on a tall line when
4955 scrolling by one line. */
4956 if (window_scroll_pixel_based_preserve_y < 0
4957 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4958 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4960 start_display (&it, w, start);
4961 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4962 window_scroll_pixel_based_preserve_y = it.current_y;
4963 window_scroll_pixel_based_preserve_x = it.current_x;
4966 else
4967 window_scroll_pixel_based_preserve_y
4968 = window_scroll_pixel_based_preserve_x = -1;
4970 /* Move iterator it from start the specified distance forward or
4971 backward. The result is the new window start. */
4972 start_display (&it, w, start);
4973 if (whole)
4975 ptrdiff_t start_pos = IT_CHARPOS (it);
4976 int dy = frame_line_height;
4977 dy = max ((window_box_height (w)
4978 - next_screen_context_lines * dy),
4979 dy) * n;
4981 /* Note that move_it_vertically always moves the iterator to the
4982 start of a line. So, if the last line doesn't have a newline,
4983 we would end up at the start of the line ending at ZV. */
4984 if (dy <= 0)
4986 move_it_vertically_backward (&it, -dy);
4987 /* Ensure we actually do move, e.g. in case we are currently
4988 looking at an image that is taller that the window height. */
4989 while (start_pos == IT_CHARPOS (it)
4990 && start_pos > BEGV)
4991 move_it_by_lines (&it, -1);
4993 else if (dy > 0)
4995 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4996 MOVE_TO_POS | MOVE_TO_Y);
4997 /* Ensure we actually do move, e.g. in case we are currently
4998 looking at an image that is taller that the window height. */
4999 while (start_pos == IT_CHARPOS (it)
5000 && start_pos < ZV)
5001 move_it_by_lines (&it, 1);
5004 else
5005 move_it_by_lines (&it, n);
5007 /* We failed if we find ZV is already on the screen (scrolling up,
5008 means there's nothing past the end), or if we can't start any
5009 earlier (scrolling down, means there's nothing past the top). */
5010 if ((n > 0 && IT_CHARPOS (it) == ZV)
5011 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5013 if (IT_CHARPOS (it) == ZV)
5015 if (it.current_y < it.last_visible_y
5016 && (it.current_y + it.max_ascent + it.max_descent
5017 > it.last_visible_y))
5019 /* The last line was only partially visible, make it fully
5020 visible. */
5021 w->vscroll = (it.last_visible_y
5022 - it.current_y + it.max_ascent + it.max_descent);
5023 adjust_frame_glyphs (it.f);
5025 else
5027 bidi_unshelve_cache (itdata, 0);
5028 if (noerror)
5029 return;
5030 else if (n < 0) /* could happen with empty buffers */
5031 xsignal0 (Qbeginning_of_buffer);
5032 else
5033 xsignal0 (Qend_of_buffer);
5036 else
5038 if (w->vscroll != 0)
5039 /* The first line was only partially visible, make it fully
5040 visible. */
5041 w->vscroll = 0;
5042 else
5044 bidi_unshelve_cache (itdata, 0);
5045 if (noerror)
5046 return;
5047 else
5048 xsignal0 (Qbeginning_of_buffer);
5052 /* If control gets here, then we vscrolled. */
5054 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
5056 /* Don't try to change the window start below. */
5057 vscrolled = 1;
5060 if (! vscrolled)
5062 ptrdiff_t pos = IT_CHARPOS (it);
5063 ptrdiff_t bytepos;
5065 /* If in the middle of a multi-glyph character move forward to
5066 the next character. */
5067 if (in_display_vector_p (&it))
5069 ++pos;
5070 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5073 /* Set the window start, and set up the window for redisplay. */
5074 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
5075 IT_BYTEPOS (it));
5076 bytepos = marker_byte_position (w->start);
5077 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
5078 w->update_mode_line = 1;
5079 /* Set force_start so that redisplay_window will run the
5080 window-scroll-functions. */
5081 w->force_start = 1;
5084 /* The rest of this function uses current_y in a nonstandard way,
5085 not including the height of the header line if any. */
5086 it.current_y = it.vpos = 0;
5088 /* Move PT out of scroll margins.
5089 This code wants current_y to be zero at the window start position
5090 even if there is a header line. */
5091 window_total_lines
5092 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
5093 this_scroll_margin = max (0, scroll_margin);
5094 this_scroll_margin
5095 = min (this_scroll_margin, window_total_lines / 4);
5096 this_scroll_margin *= frame_line_height;
5098 if (n > 0)
5100 /* We moved the window start towards ZV, so PT may be now
5101 in the scroll margin at the top. */
5102 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5103 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5104 && (NILP (Vscroll_preserve_screen_position)
5105 || EQ (Vscroll_preserve_screen_position, Qt)))
5106 /* We found PT at a legitimate height. Leave it alone. */
5108 else if (window_scroll_pixel_based_preserve_y >= 0)
5110 /* If we have a header line, take account of it.
5111 This is necessary because we set it.current_y to 0, above. */
5112 move_it_to (&it, -1,
5113 window_scroll_pixel_based_preserve_x,
5114 window_scroll_pixel_based_preserve_y
5115 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5116 -1, MOVE_TO_Y | MOVE_TO_X);
5117 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5119 else
5121 while (it.current_y < this_scroll_margin)
5123 int prev = it.current_y;
5124 move_it_by_lines (&it, 1);
5125 if (prev == it.current_y)
5126 break;
5128 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5131 else if (n < 0)
5133 ptrdiff_t charpos, bytepos;
5134 int partial_p;
5136 /* Save our position, for the
5137 window_scroll_pixel_based_preserve_y case. */
5138 charpos = IT_CHARPOS (it);
5139 bytepos = IT_BYTEPOS (it);
5141 /* We moved the window start towards BEGV, so PT may be now
5142 in the scroll margin at the bottom. */
5143 move_it_to (&it, PT, -1,
5144 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5145 - this_scroll_margin - 1),
5147 MOVE_TO_POS | MOVE_TO_Y);
5149 /* Save our position, in case it's correct. */
5150 charpos = IT_CHARPOS (it);
5151 bytepos = IT_BYTEPOS (it);
5153 /* See if point is on a partially visible line at the end. */
5154 if (it.what == IT_EOB)
5155 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5156 else
5158 move_it_by_lines (&it, 1);
5159 partial_p = it.current_y > it.last_visible_y;
5162 if (charpos == PT && !partial_p
5163 && (NILP (Vscroll_preserve_screen_position)
5164 || EQ (Vscroll_preserve_screen_position, Qt)))
5165 /* We found PT before we found the display margin, so PT is ok. */
5167 else if (window_scroll_pixel_based_preserve_y >= 0)
5169 SET_TEXT_POS_FROM_MARKER (start, w->start);
5170 start_display (&it, w, start);
5171 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5172 here because we called start_display again and did not
5173 alter it.current_y this time. */
5174 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5175 window_scroll_pixel_based_preserve_y, -1,
5176 MOVE_TO_Y | MOVE_TO_X);
5177 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5179 else
5181 if (partial_p)
5182 /* The last line was only partially visible, so back up two
5183 lines to make sure we're on a fully visible line. */
5185 move_it_by_lines (&it, -2);
5186 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5188 else
5189 /* No, the position we saved is OK, so use it. */
5190 SET_PT_BOTH (charpos, bytepos);
5193 bidi_unshelve_cache (itdata, 0);
5197 /* Implementation of window_scroll that works based on screen lines.
5198 See the comment of window_scroll for parameter descriptions. */
5200 static void
5201 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
5203 register struct window *w = XWINDOW (window);
5204 /* Fvertical_motion enters redisplay, which can trigger
5205 fontification, which in turn can modify buffer text (e.g., if the
5206 fontification functions replace escape sequences with faces, as
5207 in `grep-mode-font-lock-keywords'). So we use a marker to record
5208 the old point position, to prevent crashes in SET_PT_BOTH. */
5209 Lisp_Object opoint_marker = Fpoint_marker ();
5210 register ptrdiff_t pos, pos_byte;
5211 register int ht = window_internal_height (w);
5212 register Lisp_Object tem;
5213 bool lose;
5214 Lisp_Object bolp;
5215 ptrdiff_t startpos = marker_position (w->start);
5216 ptrdiff_t startbyte = marker_byte_position (w->start);
5217 Lisp_Object original_pos = Qnil;
5219 /* If scrolling screen-fulls, compute the number of lines to
5220 scroll from the window's height. */
5221 if (whole)
5222 n *= max (1, ht - next_screen_context_lines);
5224 if (!NILP (Vscroll_preserve_screen_position))
5226 if (window_scroll_preserve_vpos <= 0
5227 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5228 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5230 struct position posit
5231 = *compute_motion (startpos, startbyte, 0, 0, 0,
5232 PT, ht, 0, -1, w->hscroll, 0, w);
5233 window_scroll_preserve_vpos = posit.vpos;
5234 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
5237 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5238 make_number (window_scroll_preserve_vpos));
5241 XSETFASTINT (tem, PT);
5242 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5244 if (NILP (tem))
5246 Fvertical_motion (make_number (- (ht / 2)), window);
5247 startpos = PT;
5248 startbyte = PT_BYTE;
5251 SET_PT_BOTH (startpos, startbyte);
5252 lose = n < 0 && PT == BEGV;
5253 Fvertical_motion (make_number (n), window);
5254 pos = PT;
5255 pos_byte = PT_BYTE;
5256 bolp = Fbolp ();
5257 SET_PT_BOTH (marker_position (opoint_marker),
5258 marker_byte_position (opoint_marker));
5260 if (lose)
5262 if (noerror)
5263 return;
5264 else
5265 xsignal0 (Qbeginning_of_buffer);
5268 if (pos < ZV)
5270 /* Don't use a scroll margin that is negative or too large. */
5271 int this_scroll_margin =
5272 max (0, min (scroll_margin, w->total_lines / 4));
5274 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
5275 w->start_at_line_beg = !NILP (bolp);
5276 w->update_mode_line = 1;
5277 /* Set force_start so that redisplay_window will run
5278 the window-scroll-functions. */
5279 w->force_start = 1;
5281 if (!NILP (Vscroll_preserve_screen_position)
5282 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5284 SET_PT_BOTH (pos, pos_byte);
5285 Fvertical_motion (original_pos, window);
5287 /* If we scrolled forward, put point enough lines down
5288 that it is outside the scroll margin. */
5289 else if (n > 0)
5291 int top_margin;
5293 if (this_scroll_margin > 0)
5295 SET_PT_BOTH (pos, pos_byte);
5296 Fvertical_motion (make_number (this_scroll_margin), window);
5297 top_margin = PT;
5299 else
5300 top_margin = pos;
5302 if (top_margin <= marker_position (opoint_marker))
5303 SET_PT_BOTH (marker_position (opoint_marker),
5304 marker_byte_position (opoint_marker));
5305 else if (!NILP (Vscroll_preserve_screen_position))
5307 SET_PT_BOTH (pos, pos_byte);
5308 Fvertical_motion (original_pos, window);
5310 else
5311 SET_PT (top_margin);
5313 else if (n < 0)
5315 int bottom_margin;
5317 /* If we scrolled backward, put point near the end of the window
5318 but not within the scroll margin. */
5319 SET_PT_BOTH (pos, pos_byte);
5320 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5321 if (XFASTINT (tem) == ht - this_scroll_margin)
5322 bottom_margin = PT;
5323 else
5324 bottom_margin = PT + 1;
5326 if (bottom_margin > marker_position (opoint_marker))
5327 SET_PT_BOTH (marker_position (opoint_marker),
5328 marker_byte_position (opoint_marker));
5329 else
5331 if (!NILP (Vscroll_preserve_screen_position))
5333 SET_PT_BOTH (pos, pos_byte);
5334 Fvertical_motion (original_pos, window);
5336 else
5337 Fvertical_motion (make_number (-1), window);
5341 else
5343 if (noerror)
5344 return;
5345 else
5346 xsignal0 (Qend_of_buffer);
5351 /* Scroll selected_window up or down. If N is nil, scroll a
5352 screen-full which is defined as the height of the window minus
5353 next_screen_context_lines. If N is the symbol `-', scroll.
5354 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5355 up. This is the guts of Fscroll_up and Fscroll_down. */
5357 static void
5358 scroll_command (Lisp_Object n, int direction)
5360 ptrdiff_t count = SPECPDL_INDEX ();
5362 eassert (eabs (direction) == 1);
5364 /* If selected window's buffer isn't current, make it current for
5365 the moment. But don't screw up if window_scroll gets an error. */
5366 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
5368 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5369 Fset_buffer (XWINDOW (selected_window)->contents);
5372 if (NILP (n))
5373 window_scroll (selected_window, direction, 1, 0);
5374 else if (EQ (n, Qminus))
5375 window_scroll (selected_window, -direction, 1, 0);
5376 else
5378 n = Fprefix_numeric_value (n);
5379 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5382 unbind_to (count, Qnil);
5385 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5386 doc: /* Scroll text of selected window upward ARG lines.
5387 If ARG is omitted or nil, scroll upward by a near full screen.
5388 A near full screen is `next-screen-context-lines' less than a full screen.
5389 Negative ARG means scroll downward.
5390 If ARG is the atom `-', scroll downward by nearly full screen.
5391 When calling from a program, supply as argument a number, nil, or `-'. */)
5392 (Lisp_Object arg)
5394 scroll_command (arg, 1);
5395 return Qnil;
5398 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5399 doc: /* Scroll text of selected window down ARG lines.
5400 If ARG is omitted or nil, scroll down by a near full screen.
5401 A near full screen is `next-screen-context-lines' less than a full screen.
5402 Negative ARG means scroll upward.
5403 If ARG is the atom `-', scroll upward by nearly full screen.
5404 When calling from a program, supply as argument a number, nil, or `-'. */)
5405 (Lisp_Object arg)
5407 scroll_command (arg, -1);
5408 return Qnil;
5411 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5412 doc: /* Return the other window for \"other window scroll\" commands.
5413 If `other-window-scroll-buffer' is non-nil, a window
5414 showing that buffer is used.
5415 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5416 specifies the window. This takes precedence over
5417 `other-window-scroll-buffer'. */)
5418 (void)
5420 Lisp_Object window;
5422 if (MINI_WINDOW_P (XWINDOW (selected_window))
5423 && !NILP (Vminibuf_scroll_window))
5424 window = Vminibuf_scroll_window;
5425 /* If buffer is specified, scroll that buffer. */
5426 else if (!NILP (Vother_window_scroll_buffer))
5428 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5429 if (NILP (window))
5430 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5432 else
5434 /* Nothing specified; look for a neighboring window on the same
5435 frame. */
5436 window = Fnext_window (selected_window, Qnil, Qnil);
5438 if (EQ (window, selected_window))
5439 /* That didn't get us anywhere; look for a window on another
5440 visible frame. */
5442 window = Fnext_window (window, Qnil, Qt);
5443 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5444 && ! EQ (window, selected_window));
5447 CHECK_LIVE_WINDOW (window);
5449 if (EQ (window, selected_window))
5450 error ("There is no other window");
5452 return window;
5455 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5456 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5457 A near full screen is `next-screen-context-lines' less than a full screen.
5458 The next window is the one below the current one; or the one at the top
5459 if the current one is at the bottom. Negative ARG means scroll downward.
5460 If ARG is the atom `-', scroll downward by nearly full screen.
5461 When calling from a program, supply as argument a number, nil, or `-'.
5463 If `other-window-scroll-buffer' is non-nil, scroll the window
5464 showing that buffer, popping the buffer up if necessary.
5465 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5466 specifies the window to scroll. This takes precedence over
5467 `other-window-scroll-buffer'. */)
5468 (Lisp_Object arg)
5470 Lisp_Object window;
5471 struct window *w;
5472 ptrdiff_t count = SPECPDL_INDEX ();
5474 window = Fother_window_for_scrolling ();
5475 w = XWINDOW (window);
5477 /* Don't screw up if window_scroll gets an error. */
5478 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5480 Fset_buffer (w->contents);
5481 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5483 if (NILP (arg))
5484 window_scroll (window, 1, 1, 1);
5485 else if (EQ (arg, Qminus))
5486 window_scroll (window, -1, 1, 1);
5487 else
5489 if (CONSP (arg))
5490 arg = XCAR (arg);
5491 CHECK_NUMBER (arg);
5492 window_scroll (window, XINT (arg), 0, 1);
5495 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5496 unbind_to (count, Qnil);
5498 return Qnil;
5501 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5502 doc: /* Scroll selected window display ARG columns left.
5503 Default for ARG is window width minus 2.
5504 Value is the total amount of leftward horizontal scrolling in
5505 effect after the change.
5506 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5507 lower bound for automatic scrolling, i.e. automatic scrolling
5508 will not scroll a window to a column less than the value returned
5509 by this function. This happens in an interactive call. */)
5510 (register Lisp_Object arg, Lisp_Object set_minimum)
5512 struct window *w = XWINDOW (selected_window);
5513 EMACS_INT requested_arg = (NILP (arg)
5514 ? window_body_width (w, 0) - 2
5515 : XINT (Fprefix_numeric_value (arg)));
5516 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5518 if (!NILP (set_minimum))
5519 w->min_hscroll = w->hscroll;
5521 return result;
5524 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5525 doc: /* Scroll selected window display ARG columns right.
5526 Default for ARG is window width minus 2.
5527 Value is the total amount of leftward horizontal scrolling in
5528 effect after the change.
5529 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5530 lower bound for automatic scrolling, i.e. automatic scrolling
5531 will not scroll a window to a column less than the value returned
5532 by this function. This happens in an interactive call. */)
5533 (register Lisp_Object arg, Lisp_Object set_minimum)
5535 struct window *w = XWINDOW (selected_window);
5536 EMACS_INT requested_arg = (NILP (arg)
5537 ? window_body_width (w, 0) - 2
5538 : XINT (Fprefix_numeric_value (arg)));
5539 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5541 if (!NILP (set_minimum))
5542 w->min_hscroll = w->hscroll;
5544 return result;
5547 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5548 doc: /* Return the window which was selected when entering the minibuffer.
5549 Returns nil, if selected window is not a minibuffer window. */)
5550 (void)
5552 if (minibuf_level > 0
5553 && MINI_WINDOW_P (XWINDOW (selected_window))
5554 && WINDOW_LIVE_P (minibuf_selected_window))
5555 return minibuf_selected_window;
5557 return Qnil;
5560 /* Value is the number of lines actually displayed in window W,
5561 as opposed to its height. */
5563 static int
5564 displayed_window_lines (struct window *w)
5566 struct it it;
5567 struct text_pos start;
5568 int height = window_box_height (w);
5569 struct buffer *old_buffer;
5570 int bottom_y;
5571 void *itdata = NULL;
5573 if (XBUFFER (w->contents) != current_buffer)
5575 old_buffer = current_buffer;
5576 set_buffer_internal (XBUFFER (w->contents));
5578 else
5579 old_buffer = NULL;
5581 /* In case W->start is out of the accessible range, do something
5582 reasonable. This happens in Info mode when Info-scroll-down
5583 calls (recenter -1) while W->start is 1. */
5584 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5586 itdata = bidi_shelve_cache ();
5587 start_display (&it, w, start);
5588 move_it_vertically (&it, height);
5589 bottom_y = line_bottom_y (&it);
5590 bidi_unshelve_cache (itdata, 0);
5592 /* rms: On a non-window display,
5593 the value of it.vpos at the bottom of the screen
5594 seems to be 1 larger than window_box_height (w).
5595 This kludge fixes a bug whereby (move-to-window-line -1)
5596 when ZV is on the last screen line
5597 moves to the previous screen line instead of the last one. */
5598 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5599 height++;
5601 /* Add in empty lines at the bottom of the window. */
5602 if (bottom_y < height)
5604 int uy = FRAME_LINE_HEIGHT (it.f);
5605 it.vpos += (height - bottom_y + uy - 1) / uy;
5608 if (old_buffer)
5609 set_buffer_internal (old_buffer);
5611 return it.vpos;
5615 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5616 doc: /* Center point in selected window and maybe redisplay frame.
5617 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5618 relative to the selected window. If ARG is negative, it counts up from the
5619 bottom of the window. (ARG should be less than the height of the window.)
5621 If ARG is omitted or nil, then recenter with point on the middle line of
5622 the selected window; if the variable `recenter-redisplay' is non-nil,
5623 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5624 is set to `grow-only', this resets the tool-bar's height to the minimum
5625 height needed); if `recenter-redisplay' has the special value `tty',
5626 then only tty frames are redrawn.
5628 Just C-u as prefix means put point in the center of the window
5629 and redisplay normally--don't erase and redraw the frame. */)
5630 (register Lisp_Object arg)
5632 struct window *w = XWINDOW (selected_window);
5633 struct buffer *buf = XBUFFER (w->contents);
5634 struct buffer *obuf = current_buffer;
5635 bool center_p = 0;
5636 ptrdiff_t charpos, bytepos;
5637 EMACS_INT iarg IF_LINT (= 0);
5638 int this_scroll_margin;
5640 /* If redisplay is suppressed due to an error, try again. */
5641 obuf->display_error_modiff = 0;
5643 if (NILP (arg))
5645 if (!NILP (Vrecenter_redisplay)
5646 && (!EQ (Vrecenter_redisplay, Qtty)
5647 || !NILP (Ftty_type (selected_frame))))
5649 ptrdiff_t i;
5651 /* Invalidate pixel data calculated for all compositions. */
5652 for (i = 0; i < n_compositions; i++)
5653 composition_table[i]->font = NULL;
5654 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5655 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5656 #endif
5657 Fredraw_frame (WINDOW_FRAME (w));
5658 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5661 center_p = 1;
5663 else if (CONSP (arg)) /* Just C-u. */
5664 center_p = 1;
5665 else
5667 arg = Fprefix_numeric_value (arg);
5668 CHECK_NUMBER (arg);
5669 iarg = XINT (arg);
5672 set_buffer_internal (buf);
5674 /* Do this after making BUF current
5675 in case scroll_margin is buffer-local. */
5676 this_scroll_margin =
5677 max (0, min (scroll_margin, w->total_lines / 4));
5679 /* Handle centering on a graphical frame specially. Such frames can
5680 have variable-height lines and centering point on the basis of
5681 line counts would lead to strange effects. */
5682 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5684 if (center_p)
5686 struct it it;
5687 struct text_pos pt;
5688 void *itdata = bidi_shelve_cache ();
5690 SET_TEXT_POS (pt, PT, PT_BYTE);
5691 start_display (&it, w, pt);
5692 move_it_vertically_backward (&it, window_box_height (w) / 2);
5693 charpos = IT_CHARPOS (it);
5694 bytepos = IT_BYTEPOS (it);
5695 bidi_unshelve_cache (itdata, 0);
5697 else if (iarg < 0)
5699 struct it it;
5700 struct text_pos pt;
5701 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5702 int extra_line_spacing;
5703 int h = window_box_height (w);
5704 void *itdata = bidi_shelve_cache ();
5706 iarg = - max (-iarg, this_scroll_margin);
5708 SET_TEXT_POS (pt, PT, PT_BYTE);
5709 start_display (&it, w, pt);
5711 /* Be sure we have the exact height of the full line containing PT. */
5712 move_it_by_lines (&it, 0);
5714 /* The amount of pixels we have to move back is the window
5715 height minus what's displayed in the line containing PT,
5716 and the lines below. */
5717 it.current_y = 0;
5718 it.vpos = 0;
5719 move_it_by_lines (&it, nlines);
5721 if (it.vpos == nlines)
5722 h -= it.current_y;
5723 else
5725 /* Last line has no newline */
5726 h -= line_bottom_y (&it);
5727 it.vpos++;
5730 /* Don't reserve space for extra line spacing of last line. */
5731 extra_line_spacing = it.max_extra_line_spacing;
5733 /* If we can't move down NLINES lines because we hit
5734 the end of the buffer, count in some empty lines. */
5735 if (it.vpos < nlines)
5737 nlines -= it.vpos;
5738 extra_line_spacing = it.extra_line_spacing;
5739 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5741 if (h <= 0)
5743 bidi_unshelve_cache (itdata, 0);
5744 return Qnil;
5747 /* Now find the new top line (starting position) of the window. */
5748 start_display (&it, w, pt);
5749 it.current_y = 0;
5750 move_it_vertically_backward (&it, h);
5752 /* If extra line spacing is present, we may move too far
5753 back. This causes the last line to be only partially
5754 visible (which triggers redisplay to recenter that line
5755 in the middle), so move forward.
5756 But ignore extra line spacing on last line, as it is not
5757 considered to be part of the visible height of the line.
5759 h += extra_line_spacing;
5760 while (-it.current_y > h)
5761 move_it_by_lines (&it, 1);
5763 charpos = IT_CHARPOS (it);
5764 bytepos = IT_BYTEPOS (it);
5766 bidi_unshelve_cache (itdata, 0);
5768 else
5770 struct position pos;
5772 iarg = max (iarg, this_scroll_margin);
5774 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5775 charpos = pos.bufpos;
5776 bytepos = pos.bytepos;
5779 else
5781 struct position pos;
5782 int ht = window_internal_height (w);
5784 if (center_p)
5785 iarg = ht / 2;
5786 else if (iarg < 0)
5787 iarg += ht;
5789 /* Don't let it get into the margin at either top or bottom. */
5790 iarg = clip_to_bounds (this_scroll_margin, iarg,
5791 ht - this_scroll_margin - 1);
5793 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5794 charpos = pos.bufpos;
5795 bytepos = pos.bytepos;
5798 /* Set the new window start. */
5799 set_marker_both (w->start, w->contents, charpos, bytepos);
5800 w->window_end_valid = 0;
5802 w->optional_new_start = 1;
5804 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5805 FETCH_BYTE (bytepos - 1) == '\n');
5807 set_buffer_internal (obuf);
5808 return Qnil;
5811 DEFUN ("window-text-width", Fwindow_text_width, Swindow_text_width,
5812 0, 1, 0,
5813 doc: /* Return the width in columns of the text display area of WINDOW.
5814 WINDOW must be a live window and defaults to the selected one.
5816 The returned width does not include dividers, scrollbars, margins,
5817 fringes, nor any partial-width columns at the right of the text
5818 area. */)
5819 (Lisp_Object window)
5821 struct window *w = decode_live_window (window);
5823 return make_number (window_box_width (w, TEXT_AREA)
5824 / FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w)));
5827 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5828 0, 1, 0,
5829 doc: /* Return the height in lines of the text display area of WINDOW.
5830 WINDOW must be a live window and defaults to the selected one.
5832 The returned height does not include dividers, the mode line, any header
5833 line, nor any partial-height lines at the bottom of the text area. */)
5834 (Lisp_Object window)
5836 struct window *w = decode_live_window (window);
5838 return make_number (window_box_height (w)
5839 / FRAME_LINE_HEIGHT (WINDOW_XFRAME (w)));
5842 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5843 1, 1, "P",
5844 doc: /* Position point relative to window.
5845 ARG nil means position point at center of window.
5846 Else, ARG specifies vertical position within the window;
5847 zero means top of window, negative means relative to bottom of window. */)
5848 (Lisp_Object arg)
5850 struct window *w = XWINDOW (selected_window);
5851 int lines, start;
5852 Lisp_Object window;
5853 #if 0
5854 int this_scroll_margin;
5855 #endif
5857 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5858 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5859 when passed below to set_marker_both. */
5860 error ("move-to-window-line called from unrelated buffer");
5862 window = selected_window;
5863 start = marker_position (w->start);
5864 if (start < BEGV || start > ZV)
5866 int height = window_internal_height (w);
5867 Fvertical_motion (make_number (- (height / 2)), window);
5868 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5869 w->start_at_line_beg = !NILP (Fbolp ());
5870 w->force_start = 1;
5872 else
5873 Fgoto_char (w->start);
5875 lines = displayed_window_lines (w);
5877 #if 0
5878 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5879 #endif
5881 if (NILP (arg))
5882 XSETFASTINT (arg, lines / 2);
5883 else
5885 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5887 if (iarg < 0)
5888 iarg = iarg + lines;
5890 #if 0 /* This code would prevent move-to-window-line from moving point
5891 to a place inside the scroll margins (which would cause the
5892 next redisplay to scroll). I wrote this code, but then concluded
5893 it is probably better not to install it. However, it is here
5894 inside #if 0 so as not to lose it. -- rms. */
5896 /* Don't let it get into the margin at either top or bottom. */
5897 iarg = max (iarg, this_scroll_margin);
5898 iarg = min (iarg, lines - this_scroll_margin - 1);
5899 #endif
5901 arg = make_number (iarg);
5904 /* Skip past a partially visible first line. */
5905 if (w->vscroll)
5906 XSETINT (arg, XINT (arg) + 1);
5908 return Fvertical_motion (arg, window);
5913 /***********************************************************************
5914 Window Configuration
5915 ***********************************************************************/
5917 struct save_window_data
5919 struct vectorlike_header header;
5920 Lisp_Object selected_frame;
5921 Lisp_Object current_window;
5922 Lisp_Object current_buffer;
5923 Lisp_Object minibuf_scroll_window;
5924 Lisp_Object minibuf_selected_window;
5925 Lisp_Object root_window;
5926 Lisp_Object focus_frame;
5927 /* A vector, each of whose elements is a struct saved_window
5928 for one window. */
5929 Lisp_Object saved_windows;
5931 /* All fields above are traced by the GC.
5932 From `frame-cols' down, the fields are ignored by the GC. */
5933 /* We should be able to do without the following two. */
5934 int frame_cols, frame_lines;
5935 /* These two should get eventually replaced by their pixel
5936 counterparts. */
5937 int frame_menu_bar_lines, frame_tool_bar_lines;
5938 int frame_text_width, frame_text_height;
5939 /* These are currently unused. We need them as soon as we convert
5940 to pixels. */
5941 int frame_menu_bar_height, frame_tool_bar_height;
5944 /* This is saved as a Lisp_Vector */
5945 struct saved_window
5947 struct vectorlike_header header;
5949 Lisp_Object window, buffer, start, pointm, mark;
5950 Lisp_Object pixel_left, pixel_top, pixel_height, pixel_width;
5951 Lisp_Object left_col, top_line, total_cols, total_lines;
5952 Lisp_Object normal_cols, normal_lines;
5953 Lisp_Object hscroll, min_hscroll;
5954 Lisp_Object parent, prev;
5955 Lisp_Object start_at_line_beg;
5956 Lisp_Object display_table;
5957 Lisp_Object left_margin_cols, right_margin_cols;
5958 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5959 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5960 Lisp_Object combination_limit, window_parameters;
5963 #define SAVED_WINDOW_N(swv,n) \
5964 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5966 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5967 doc: /* Return t if OBJECT is a window-configuration object. */)
5968 (Lisp_Object object)
5970 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5973 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5974 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5975 (Lisp_Object config)
5977 register struct save_window_data *data;
5978 struct Lisp_Vector *saved_windows;
5980 CHECK_WINDOW_CONFIGURATION (config);
5982 data = (struct save_window_data *) XVECTOR (config);
5983 saved_windows = XVECTOR (data->saved_windows);
5984 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5987 /* From Chong's unwind_create_frame_1. */
5988 static void
5989 unwind_change_frame (Lisp_Object val)
5991 inhibit_lisp_code = val;
5994 DEFUN ("set-window-configuration", Fset_window_configuration,
5995 Sset_window_configuration, 1, 1, 0,
5996 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5997 CONFIGURATION must be a value previously returned
5998 by `current-window-configuration' (which see).
5999 If CONFIGURATION was made from a frame that is now deleted,
6000 only frame-independent values can be restored. In this case,
6001 the return value is nil. Otherwise the value is t. */)
6002 (Lisp_Object configuration)
6004 register struct save_window_data *data;
6005 struct Lisp_Vector *saved_windows;
6006 Lisp_Object new_current_buffer;
6007 Lisp_Object frame;
6008 struct frame *f;
6009 ptrdiff_t old_point = -1;
6011 CHECK_WINDOW_CONFIGURATION (configuration);
6013 data = (struct save_window_data *) XVECTOR (configuration);
6014 saved_windows = XVECTOR (data->saved_windows);
6016 new_current_buffer = data->current_buffer;
6017 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
6018 new_current_buffer = Qnil;
6019 else
6021 if (XBUFFER (new_current_buffer) == current_buffer)
6022 /* The code further down "preserves point" by saving here PT in
6023 old_point and then setting it later back into PT. When the
6024 current-selected-window and the final-selected-window both show
6025 the current buffer, this suffers from the problem that the
6026 current PT is the window-point of the current-selected-window,
6027 while the final PT is the point of the final-selected-window, so
6028 this copy from one PT to the other would end up moving the
6029 window-point of the final-selected-window to the window-point of
6030 the current-selected-window. So we have to be careful which
6031 point of the current-buffer we copy into old_point. */
6032 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6033 && WINDOWP (selected_window)
6034 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
6035 && !EQ (selected_window, data->current_window))
6036 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6037 else
6038 old_point = PT;
6039 else
6040 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6041 point in new_current_buffer as of the last time this buffer was
6042 used. This can be non-deterministic since it can be changed by
6043 things like jit-lock by mere temporary selection of some random
6044 window that happens to show this buffer.
6045 So if possible we want this arbitrary choice of "which point" to
6046 be the one from the to-be-selected-window so as to prevent this
6047 window's cursor from being copied from another window. */
6048 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
6049 /* If current_window = selected_window, its point is in BUF_PT. */
6050 && !EQ (selected_window, data->current_window))
6051 old_point = marker_position (XWINDOW (data->current_window)->pointm);
6052 else
6053 old_point = BUF_PT (XBUFFER (new_current_buffer));
6056 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6057 f = XFRAME (frame);
6059 /* If f is a dead frame, don't bother rebuilding its window tree.
6060 However, there is other stuff we should still try to do below. */
6061 if (FRAME_LIVE_P (f))
6063 Lisp_Object window;
6064 Lisp_Object dead_windows = Qnil;
6065 register Lisp_Object tem, par, pers;
6066 register struct window *w;
6067 register struct saved_window *p;
6068 struct window *root_window;
6069 struct window **leaf_windows;
6070 int n_leaf_windows;
6071 ptrdiff_t k;
6072 int i, n;
6073 ptrdiff_t count = SPECPDL_INDEX ();
6074 /* If the frame has been resized since this window configuration was
6075 made, we change the frame to the size specified in the
6076 configuration, restore the configuration, and then resize it
6077 back. We keep track of the prevailing height in these variables. */
6078 int previous_frame_text_height = FRAME_TEXT_HEIGHT (f);
6079 int previous_frame_text_width = FRAME_TEXT_WIDTH (f);
6080 /* int previous_frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f); */
6081 /* int previous_frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f); */
6082 /* int previous_frame_lines = FRAME_LINES (f); */
6083 /* int previous_frame_cols = FRAME_COLS (f); */
6084 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6085 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6087 /* Don't do this within the main loop below: This may call Lisp
6088 code and is thus potentially unsafe while input is blocked. */
6089 for (k = 0; k < saved_windows->header.size; k++)
6091 p = SAVED_WINDOW_N (saved_windows, k);
6092 window = p->window;
6093 w = XWINDOW (window);
6094 if (BUFFERP (w->contents)
6095 && !EQ (w->contents, p->buffer)
6096 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6097 /* If a window we restore gets another buffer, record the
6098 window's old buffer. */
6099 call1 (Qrecord_window_buffer, window);
6102 /* Don't run lisp in the following segment since the frame is in a
6103 completely inconsistent state. See Bug#16207. */
6104 record_unwind_protect (unwind_change_frame, inhibit_lisp_code);
6105 inhibit_lisp_code = Qt;
6106 /* The mouse highlighting code could get screwed up
6107 if it runs during this. */
6108 block_input ();
6110 if (data->frame_text_width != previous_frame_text_width
6111 || data->frame_text_height != previous_frame_text_height)
6112 change_frame_size (f, data->frame_text_width,
6113 data->frame_text_height, 0, 0, 0, 1);
6115 if (data->frame_menu_bar_lines != previous_frame_menu_bar_lines)
6117 #ifdef HAVE_WINDOW_SYSTEM
6118 if (FRAME_WINDOW_P (f))
6119 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6120 make_number (0));
6121 else /* TTY or MSDOS */
6122 #endif
6123 set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6124 make_number (0));
6126 #ifdef HAVE_WINDOW_SYSTEM
6127 if (data->frame_tool_bar_lines != previous_frame_tool_bar_lines)
6128 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6129 make_number (0));
6130 #endif
6132 /* "Swap out" point from the selected window's buffer
6133 into the window itself. (Normally the pointm of the selected
6134 window holds garbage.) We do this now, before
6135 restoring the window contents, and prevent it from
6136 being done later on when we select a new window. */
6137 if (! NILP (XWINDOW (selected_window)->contents))
6139 w = XWINDOW (selected_window);
6140 set_marker_both (w->pointm,
6141 w->contents,
6142 BUF_PT (XBUFFER (w->contents)),
6143 BUF_PT_BYTE (XBUFFER (w->contents)));
6146 fset_redisplay (f);
6147 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6149 /* Problem: Freeing all matrices and later allocating them again
6150 is a serious redisplay flickering problem. What we would
6151 really like to do is to free only those matrices not reused
6152 below. */
6153 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6154 leaf_windows = alloca (count_windows (root_window)
6155 * sizeof *leaf_windows);
6156 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6158 /* Kludge Alert!
6159 Mark all windows now on frame as "deleted".
6160 Restoring the new configuration "undeletes" any that are in it.
6162 Save their current buffers in their height fields, since we may
6163 need it later, if a buffer saved in the configuration is now
6164 dead. */
6165 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
6167 for (k = 0; k < saved_windows->header.size; k++)
6169 p = SAVED_WINDOW_N (saved_windows, k);
6170 window = p->window;
6171 w = XWINDOW (window);
6172 wset_next (w, Qnil);
6174 if (!NILP (p->parent))
6175 wset_parent
6176 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
6177 else
6178 wset_parent (w, Qnil);
6180 if (!NILP (p->prev))
6182 wset_prev
6183 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
6184 wset_next (XWINDOW (w->prev), p->window);
6186 else
6188 wset_prev (w, Qnil);
6189 if (!NILP (w->parent))
6190 wset_combination (XWINDOW (w->parent),
6191 (XINT (p->total_cols)
6192 != XWINDOW (w->parent)->total_cols),
6193 p->window);
6196 /* If we squirreled away the buffer, restore it now. */
6197 if (BUFFERP (w->combination_limit))
6198 wset_buffer (w, w->combination_limit);
6199 w->pixel_left = XFASTINT (p->pixel_left);
6200 w->pixel_top = XFASTINT (p->pixel_top);
6201 w->pixel_width = XFASTINT (p->pixel_width);
6202 w->pixel_height = XFASTINT (p->pixel_height);
6203 w->left_col = XFASTINT (p->left_col);
6204 w->top_line = XFASTINT (p->top_line);
6205 w->total_cols = XFASTINT (p->total_cols);
6206 w->total_lines = XFASTINT (p->total_lines);
6207 wset_normal_cols (w, p->normal_cols);
6208 wset_normal_lines (w, p->normal_lines);
6209 w->hscroll = XFASTINT (p->hscroll);
6210 w->min_hscroll = XFASTINT (p->min_hscroll);
6211 wset_display_table (w, p->display_table);
6212 w->left_margin_cols = XINT (p->left_margin_cols);
6213 w->right_margin_cols = XINT (p->right_margin_cols);
6214 w->left_fringe_width = XINT (p->left_fringe_width);
6215 w->right_fringe_width = XINT (p->right_fringe_width);
6216 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
6217 w->scroll_bar_width = XINT (p->scroll_bar_width);
6218 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
6219 wset_dedicated (w, p->dedicated);
6220 wset_combination_limit (w, p->combination_limit);
6221 /* Restore any window parameters that have been saved.
6222 Parameters that have not been saved are left alone. */
6223 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
6225 pers = XCAR (tem);
6226 if (CONSP (pers))
6228 if (NILP (XCDR (pers)))
6230 par = Fassq (XCAR (pers), w->window_parameters);
6231 if (CONSP (par) && !NILP (XCDR (par)))
6232 /* Reset a parameter to nil if and only if it
6233 has a non-nil association. Don't make new
6234 associations. */
6235 Fsetcdr (par, Qnil);
6237 else
6238 /* Always restore a non-nil value. */
6239 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
6243 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
6244 /* If saved buffer is alive, install it. */
6246 wset_buffer (w, p->buffer);
6247 w->start_at_line_beg = !NILP (p->start_at_line_beg);
6248 set_marker_restricted (w->start, p->start, w->contents);
6249 set_marker_restricted (w->pointm, p->pointm,
6250 w->contents);
6251 Fset_marker (BVAR (XBUFFER (w->contents), mark),
6252 p->mark, w->contents);
6254 /* As documented in Fcurrent_window_configuration, don't
6255 restore the location of point in the buffer which was
6256 current when the window configuration was recorded. */
6257 if (!EQ (p->buffer, new_current_buffer)
6258 && XBUFFER (p->buffer) == current_buffer)
6259 Fgoto_char (w->pointm);
6261 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
6262 /* Keep window's old buffer; make sure the markers are real. */
6264 /* Set window markers at start of visible range. */
6265 if (XMARKER (w->start)->buffer == 0)
6266 set_marker_restricted_both (w->start, w->contents, 0, 0);
6267 if (XMARKER (w->pointm)->buffer == 0)
6268 set_marker_restricted_both
6269 (w->pointm, w->contents,
6270 BUF_PT (XBUFFER (w->contents)),
6271 BUF_PT_BYTE (XBUFFER (w->contents)));
6272 w->start_at_line_beg = 1;
6274 else if (!NILP (w->start))
6275 /* Leaf window has no live buffer, get one. */
6277 /* Get the buffer via other_buffer_safely in order to
6278 avoid showing an unimportant buffer and, if necessary, to
6279 recreate *scratch* in the course (part of Juanma's bs-show
6280 scenario from March 2011). */
6281 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
6282 /* This will set the markers to beginning of visible
6283 range. */
6284 set_marker_restricted_both (w->start, w->contents, 0, 0);
6285 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
6286 w->start_at_line_beg = 1;
6287 if (!NILP (w->dedicated))
6288 /* Record this window as dead. */
6289 dead_windows = Fcons (window, dead_windows);
6290 /* Make sure window is no more dedicated. */
6291 wset_dedicated (w, Qnil);
6295 fset_root_window (f, data->root_window);
6296 /* Arrange *not* to restore point in the buffer that was
6297 current when the window configuration was saved. */
6298 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6299 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6300 make_number (old_point),
6301 XWINDOW (data->current_window)->contents);
6303 /* In the following call to `select-window', prevent "swapping out
6304 point" in the old selected window using the buffer that has
6305 been restored into it. We already swapped out that point from
6306 that window's old buffer.
6308 Do not record the buffer here. We do that in a separate call
6309 to select_window below. See also Bug#16207. */
6310 select_window (data->current_window, Qt, 1);
6311 BVAR (XBUFFER (XWINDOW (selected_window)->contents),
6312 last_selected_window)
6313 = selected_window;
6315 if (NILP (data->focus_frame)
6316 || (FRAMEP (data->focus_frame)
6317 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6318 Fredirect_frame_focus (frame, data->focus_frame);
6320 /* Set the frame size to the value it had before this function. */
6321 if (previous_frame_text_width != FRAME_TEXT_WIDTH (f)
6322 || previous_frame_text_height != FRAME_TEXT_HEIGHT (f))
6323 change_frame_size (f, previous_frame_text_width,
6324 previous_frame_text_height, 0, 0, 0, 1);
6326 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6328 #ifdef HAVE_WINDOW_SYSTEM
6329 if (FRAME_WINDOW_P (f))
6330 x_set_menu_bar_lines (f,
6331 make_number (previous_frame_menu_bar_lines),
6332 make_number (0));
6333 else /* TTY or MSDOS */
6334 #endif
6335 set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6336 make_number (0));
6338 #ifdef HAVE_WINDOW_SYSTEM
6339 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6340 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6341 make_number (0));
6342 #endif
6344 /* Now, free glyph matrices in windows that were not reused. */
6345 for (i = n = 0; i < n_leaf_windows; ++i)
6347 if (NILP (leaf_windows[i]->contents))
6348 free_window_matrices (leaf_windows[i]);
6349 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
6350 ++n;
6353 adjust_frame_glyphs (f);
6354 unblock_input ();
6355 unbind_to (count, Qnil);
6357 /* Scan dead buffer windows. */
6358 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
6360 window = XCAR (dead_windows);
6361 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
6362 delete_deletable_window (window);
6365 /* Record the selected window's buffer here. The window should
6366 already be the selected one from the call above. */
6367 select_window (data->current_window, Qnil, 0);
6369 /* Fselect_window will have made f the selected frame, so we
6370 reselect the proper frame here. Fhandle_switch_frame will change the
6371 selected window too, but that doesn't make the call to
6372 Fselect_window above totally superfluous; it still sets f's
6373 selected window. */
6374 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6375 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6377 run_window_configuration_change_hook (f);
6380 if (!NILP (new_current_buffer))
6382 Fset_buffer (new_current_buffer);
6383 /* If the new current buffer doesn't appear in the selected
6384 window, go to its old point (see bug#12208). */
6385 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
6386 Fgoto_char (make_number (old_point));
6389 Vminibuf_scroll_window = data->minibuf_scroll_window;
6390 minibuf_selected_window = data->minibuf_selected_window;
6392 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6395 void
6396 restore_window_configuration (Lisp_Object configuration)
6398 Fset_window_configuration (configuration);
6402 /* If WINDOW is an internal window, recursively delete all child windows
6403 reachable via the next and contents slots of WINDOW. Otherwise setup
6404 WINDOW to not show any buffer. */
6406 void
6407 delete_all_child_windows (Lisp_Object window)
6409 register struct window *w;
6411 w = XWINDOW (window);
6413 if (!NILP (w->next))
6414 /* Delete WINDOW's siblings (we traverse postorderly). */
6415 delete_all_child_windows (w->next);
6417 if (WINDOWP (w->contents))
6419 delete_all_child_windows (w->contents);
6420 wset_combination (w, 0, Qnil);
6422 else if (BUFFERP (w->contents))
6424 unshow_buffer (w);
6425 unchain_marker (XMARKER (w->pointm));
6426 unchain_marker (XMARKER (w->start));
6427 /* Since combination limit makes sense for an internal windows
6428 only, we use this slot to save the buffer for the sake of
6429 possible resurrection in Fset_window_configuration. */
6430 wset_combination_limit (w, w->contents);
6431 wset_buffer (w, Qnil);
6434 Vwindow_list = Qnil;
6437 static int
6438 count_windows (register struct window *window)
6440 register int count = 1;
6441 if (!NILP (window->next))
6442 count += count_windows (XWINDOW (window->next));
6443 if (WINDOWP (window->contents))
6444 count += count_windows (XWINDOW (window->contents));
6445 return count;
6449 /* Fill vector FLAT with leaf windows under W, starting at index I.
6450 Value is last index + 1. */
6451 static int
6452 get_leaf_windows (struct window *w, struct window **flat, int i)
6454 while (w)
6456 if (WINDOWP (w->contents))
6457 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
6458 else
6459 flat[i++] = w;
6461 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6464 return i;
6468 /* Return a pointer to the glyph W's physical cursor is on. Value is
6469 null if W's current matrix is invalid, so that no meaningful glyph
6470 can be returned. */
6471 struct glyph *
6472 get_phys_cursor_glyph (struct window *w)
6474 struct glyph_row *row;
6475 struct glyph *glyph;
6476 int hpos = w->phys_cursor.hpos;
6478 if (!(w->phys_cursor.vpos >= 0
6479 && w->phys_cursor.vpos < w->current_matrix->nrows))
6480 return NULL;
6482 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6483 if (!row->enabled_p)
6484 return NULL;
6486 if (w->hscroll)
6488 /* When the window is hscrolled, cursor hpos can legitimately be
6489 out of bounds, but we draw the cursor at the corresponding
6490 window margin in that case. */
6491 if (!row->reversed_p && hpos < 0)
6492 hpos = 0;
6493 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6494 hpos = row->used[TEXT_AREA] - 1;
6497 if (0 <= hpos && hpos < row->used[TEXT_AREA])
6498 glyph = row->glyphs[TEXT_AREA] + hpos;
6499 else
6500 glyph = NULL;
6502 return glyph;
6506 static int
6507 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6509 register struct saved_window *p;
6510 register struct window *w;
6511 register Lisp_Object tem, pers, par;
6513 for (; !NILP (window); window = w->next)
6515 p = SAVED_WINDOW_N (vector, i);
6516 w = XWINDOW (window);
6518 wset_temslot (w, make_number (i)); i++;
6519 p->window = window;
6520 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6521 p->pixel_left = make_number (w->pixel_left);
6522 p->pixel_top = make_number (w->pixel_top);
6523 p->pixel_width = make_number (w->pixel_width);
6524 p->pixel_height = make_number (w->pixel_height);
6525 p->left_col = make_number (w->left_col);
6526 p->top_line = make_number (w->top_line);
6527 p->total_cols = make_number (w->total_cols);
6528 p->total_lines = make_number (w->total_lines);
6529 p->normal_cols = w->normal_cols;
6530 p->normal_lines = w->normal_lines;
6531 XSETFASTINT (p->hscroll, w->hscroll);
6532 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6533 p->display_table = w->display_table;
6534 p->left_margin_cols = make_number (w->left_margin_cols);
6535 p->right_margin_cols = make_number (w->right_margin_cols);
6536 p->left_fringe_width = make_number (w->left_fringe_width);
6537 p->right_fringe_width = make_number (w->right_fringe_width);
6538 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6539 p->scroll_bar_width = make_number (w->scroll_bar_width);
6540 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6541 p->dedicated = w->dedicated;
6542 p->combination_limit = w->combination_limit;
6543 p->window_parameters = Qnil;
6545 if (!NILP (Vwindow_persistent_parameters))
6547 /* Run cycle detection on Vwindow_persistent_parameters. */
6548 Lisp_Object tortoise, hare;
6550 hare = tortoise = Vwindow_persistent_parameters;
6551 while (CONSP (hare))
6553 hare = XCDR (hare);
6554 if (!CONSP (hare))
6555 break;
6557 hare = XCDR (hare);
6558 tortoise = XCDR (tortoise);
6560 if (EQ (hare, tortoise))
6561 /* Reset Vwindow_persistent_parameters to Qnil. */
6563 Vwindow_persistent_parameters = Qnil;
6564 break;
6568 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6569 tem = XCDR (tem))
6571 pers = XCAR (tem);
6572 /* Save values for persistent window parameters. */
6573 if (CONSP (pers) && !NILP (XCDR (pers)))
6575 par = Fassq (XCAR (pers), w->window_parameters);
6576 if (NILP (par))
6577 /* If the window has no value for the parameter,
6578 make one. */
6579 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6580 p->window_parameters);
6581 else
6582 /* If the window has a value for the parameter,
6583 save it. */
6584 p->window_parameters = Fcons (Fcons (XCAR (par),
6585 XCDR (par)),
6586 p->window_parameters);
6591 if (BUFFERP (w->contents))
6593 /* Save w's value of point in the window configuration. If w
6594 is the selected window, then get the value of point from
6595 the buffer; pointm is garbage in the selected window. */
6596 if (EQ (window, selected_window))
6597 p->pointm = build_marker (XBUFFER (w->contents),
6598 BUF_PT (XBUFFER (w->contents)),
6599 BUF_PT_BYTE (XBUFFER (w->contents)));
6600 else
6601 p->pointm = Fcopy_marker (w->pointm, Qnil);
6602 XMARKER (p->pointm)->insertion_type
6603 = !NILP (Vwindow_point_insertion_type);
6605 p->start = Fcopy_marker (w->start, Qnil);
6606 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6608 tem = BVAR (XBUFFER (w->contents), mark);
6609 p->mark = Fcopy_marker (tem, Qnil);
6611 else
6613 p->pointm = Qnil;
6614 p->start = Qnil;
6615 p->mark = Qnil;
6616 p->start_at_line_beg = Qnil;
6619 if (NILP (w->parent))
6620 p->parent = Qnil;
6621 else
6622 p->parent = XWINDOW (w->parent)->temslot;
6624 if (NILP (w->prev))
6625 p->prev = Qnil;
6626 else
6627 p->prev = XWINDOW (w->prev)->temslot;
6629 if (WINDOWP (w->contents))
6630 i = save_window_save (w->contents, vector, i);
6633 return i;
6636 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6637 Scurrent_window_configuration, 0, 1, 0,
6638 doc: /* Return an object representing the current window configuration of FRAME.
6639 If FRAME is nil or omitted, use the selected frame.
6640 This describes the number of windows, their sizes and current buffers,
6641 and for each displayed buffer, where display starts, and the positions of
6642 point and mark. An exception is made for point in the current buffer:
6643 its value is -not- saved.
6644 This also records the currently selected frame, and FRAME's focus
6645 redirection (see `redirect-frame-focus'). The variable
6646 `window-persistent-parameters' specifies which window parameters are
6647 saved by this function. */)
6648 (Lisp_Object frame)
6650 register Lisp_Object tem;
6651 register int n_windows;
6652 register struct save_window_data *data;
6653 register int i;
6654 struct frame *f = decode_live_frame (frame);
6656 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6657 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6658 PVEC_WINDOW_CONFIGURATION);
6660 data->frame_cols = FRAME_COLS (f);
6661 data->frame_lines = FRAME_LINES (f);
6662 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6663 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6664 data->frame_text_width = FRAME_TEXT_WIDTH (f);
6665 data->frame_text_height = FRAME_TEXT_HEIGHT (f);
6666 data->frame_menu_bar_height = FRAME_MENU_BAR_HEIGHT (f);
6667 data->frame_tool_bar_height = FRAME_TOOL_BAR_HEIGHT (f);
6668 data->selected_frame = selected_frame;
6669 data->current_window = FRAME_SELECTED_WINDOW (f);
6670 XSETBUFFER (data->current_buffer, current_buffer);
6671 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6672 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6673 data->root_window = FRAME_ROOT_WINDOW (f);
6674 data->focus_frame = FRAME_FOCUS_FRAME (f);
6675 tem = make_uninit_vector (n_windows);
6676 data->saved_windows = tem;
6677 for (i = 0; i < n_windows; i++)
6678 ASET (tem, i,
6679 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6680 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6681 XSETWINDOW_CONFIGURATION (tem, data);
6682 return (tem);
6685 /* Called after W's margins, fringes or scroll bars was adjusted. */
6687 static void
6688 apply_window_adjustment (struct window *w)
6690 eassert (w);
6691 adjust_window_margins (w);
6692 clear_glyph_matrix (w->current_matrix);
6693 w->window_end_valid = 0;
6694 windows_or_buffers_changed = 30;
6695 wset_redisplay (w);
6696 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6700 /***********************************************************************
6701 Marginal Areas
6702 ***********************************************************************/
6704 static struct window *
6705 set_window_margins (struct window *w, Lisp_Object left_width,
6706 Lisp_Object right_width)
6708 int left, right;
6710 /* FIXME: what about margins that are too wide? */
6711 left = (NILP (left_width) ? 0
6712 : (CHECK_NATNUM (left_width), XINT (left_width)));
6713 right = (NILP (right_width) ? 0
6714 : (CHECK_NATNUM (right_width), XINT (right_width)));
6716 if (w->left_margin_cols != left || w->right_margin_cols != right)
6718 w->left_margin_cols = left;
6719 w->right_margin_cols = right;
6720 return w;
6722 return NULL;
6725 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6726 2, 3, 0,
6727 doc: /* Set width of marginal areas of window WINDOW.
6728 WINDOW must be a live window and defaults to the selected one.
6730 Second arg LEFT-WIDTH specifies the number of character cells to
6731 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6732 does the same for the right marginal area. A nil width parameter
6733 means no margin.
6735 Return t if any margin was actually changed and nil otherwise. */)
6736 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6738 struct window *w = set_window_margins (decode_live_window (window),
6739 left_width, right_width);
6740 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6744 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6745 0, 1, 0,
6746 doc: /* Get width of marginal areas of window WINDOW.
6747 WINDOW must be a live window and defaults to the selected one.
6749 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6750 If a marginal area does not exist, its width will be returned
6751 as nil. */)
6752 (Lisp_Object window)
6754 struct window *w = decode_live_window (window);
6755 return Fcons (w->left_margin_cols
6756 ? make_number (w->left_margin_cols) : Qnil,
6757 w->right_margin_cols
6758 ? make_number (w->right_margin_cols) : Qnil);
6763 /***********************************************************************
6764 Fringes
6765 ***********************************************************************/
6767 static struct window *
6768 set_window_fringes (struct window *w, Lisp_Object left_width,
6769 Lisp_Object right_width, Lisp_Object outside_margins)
6771 int left, right, outside = !NILP (outside_margins);
6773 left = (NILP (left_width) ? -1
6774 : (CHECK_NATNUM (left_width), XINT (left_width)));
6775 right = (NILP (right_width) ? -1
6776 : (CHECK_NATNUM (right_width), XINT (right_width)));
6778 /* Do nothing on a tty or if nothing to actually change. */
6779 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6780 && (w->left_fringe_width != left
6781 || w->right_fringe_width != right
6782 || w->fringes_outside_margins != outside))
6784 w->left_fringe_width = left;
6785 w->right_fringe_width = right;
6786 w->fringes_outside_margins = outside;
6787 return w;
6789 return NULL;
6792 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6793 2, 4, 0,
6794 doc: /* Set the fringe widths of window WINDOW.
6795 WINDOW must be a live window and defaults to the selected one.
6797 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6798 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6799 fringe width. If a fringe width arg is nil, that means to use the
6800 frame's default fringe width. Default fringe widths can be set with
6801 the command `set-fringe-style'.
6802 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6803 outside of the display margins. By default, fringes are drawn between
6804 display marginal areas and the text area.
6806 Return t if any fringe was actually changed and nil otherwise. */)
6807 (Lisp_Object window, Lisp_Object left_width,
6808 Lisp_Object right_width, Lisp_Object outside_margins)
6810 struct window *w
6811 = set_window_fringes (decode_live_window (window),
6812 left_width, right_width, outside_margins);
6813 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6817 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6818 0, 1, 0,
6819 doc: /* Get width of fringes of window WINDOW.
6820 WINDOW must be a live window and defaults to the selected one.
6822 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6823 (Lisp_Object window)
6825 struct window *w = decode_live_window (window);
6827 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6828 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6829 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6834 /***********************************************************************
6835 Scroll bars
6836 ***********************************************************************/
6838 static struct window *
6839 set_window_scroll_bars (struct window *w, Lisp_Object width,
6840 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6842 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6844 if (iwidth == 0)
6845 vertical_type = Qnil;
6847 if (!(NILP (vertical_type)
6848 || EQ (vertical_type, Qleft)
6849 || EQ (vertical_type, Qright)
6850 || EQ (vertical_type, Qt)))
6851 error ("Invalid type of vertical scroll bar");
6853 if (w->scroll_bar_width != iwidth
6854 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6856 w->scroll_bar_width = iwidth;
6857 wset_vertical_scroll_bar_type (w, vertical_type);
6858 return w;
6860 return NULL;
6863 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6864 Sset_window_scroll_bars, 2, 4, 0,
6865 doc: /* Set width and type of scroll bars of window WINDOW.
6866 WINDOW must be a live window and defaults to the selected one.
6868 Second parameter WIDTH specifies the pixel width for the scroll bar.
6869 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6870 bar: left, right, or nil.
6871 If WIDTH is nil, use the frame's scroll-bar width.
6872 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6873 Fourth parameter HORIZONTAL-TYPE is currently unused.
6875 Return t if scroll bars were actually changed and nil otherwise. */)
6876 (Lisp_Object window, Lisp_Object width,
6877 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6879 struct window *w
6880 = set_window_scroll_bars (decode_live_window (window),
6881 width, vertical_type, horizontal_type);
6882 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6886 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6887 0, 1, 0,
6888 doc: /* Get width and type of scroll bars of window WINDOW.
6889 WINDOW must be a live window and defaults to the selected one.
6891 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6892 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6893 value. */)
6894 (Lisp_Object window)
6896 struct window *w = decode_live_window (window);
6898 return list4 (make_number (WINDOW_SCROLL_BAR_AREA_WIDTH (w)),
6899 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6900 w->vertical_scroll_bar_type, Qnil);
6905 /***********************************************************************
6906 Smooth scrolling
6907 ***********************************************************************/
6909 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6910 doc: /* Return the amount by which WINDOW is scrolled vertically.
6911 If WINDOW is omitted or nil, it defaults to the selected window.
6912 Normally, value is a multiple of the canonical character height of WINDOW;
6913 optional second arg PIXELS-P means value is measured in pixels. */)
6914 (Lisp_Object window, Lisp_Object pixels_p)
6916 Lisp_Object result;
6917 struct window *w = decode_live_window (window);
6918 struct frame *f = XFRAME (w->frame);
6920 if (FRAME_WINDOW_P (f))
6921 result = (NILP (pixels_p)
6922 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6923 : make_number (-w->vscroll));
6924 else
6925 result = make_number (0);
6926 return result;
6930 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6931 2, 3, 0,
6932 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6933 WINDOW nil means use the selected window. Normally, VSCROLL is a
6934 non-negative multiple of the canonical character height of WINDOW;
6935 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6936 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6937 corresponds to an integral number of pixels. The return value is the
6938 result of this rounding.
6939 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6940 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6942 struct window *w = decode_live_window (window);
6943 struct frame *f = XFRAME (w->frame);
6945 CHECK_NUMBER_OR_FLOAT (vscroll);
6947 if (FRAME_WINDOW_P (f))
6949 int old_dy = w->vscroll;
6951 w->vscroll = - (NILP (pixels_p)
6952 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6953 : XFLOATINT (vscroll));
6954 w->vscroll = min (w->vscroll, 0);
6956 if (w->vscroll != old_dy)
6958 /* Adjust glyph matrix of the frame if the virtual display
6959 area becomes larger than before. */
6960 if (w->vscroll < 0 && w->vscroll < old_dy)
6961 adjust_frame_glyphs (f);
6963 /* Prevent redisplay shortcuts. */
6964 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6968 return Fwindow_vscroll (window, pixels_p);
6972 /* Call FN for all leaf windows on frame F. FN is called with the
6973 first argument being a pointer to the leaf window, and with
6974 additional argument USER_DATA. Stops when FN returns 0. */
6976 static void
6977 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6978 void *user_data)
6980 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6981 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6982 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6986 /* Helper function for foreach_window. Call FN for all leaf windows
6987 reachable from W. FN is called with the first argument being a
6988 pointer to the leaf window, and with additional argument USER_DATA.
6989 Stop when FN returns 0. Value is 0 if stopped by FN. */
6991 static int
6992 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6994 int cont;
6996 for (cont = 1; w && cont;)
6998 if (WINDOWP (w->contents))
6999 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
7000 else
7001 cont = fn (w, user_data);
7003 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7006 return cont;
7009 /***********************************************************************
7010 Initialization
7011 ***********************************************************************/
7013 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
7014 describe the same state of affairs. This is used by Fequal.
7016 IGNORE_POSITIONS means ignore non-matching scroll positions
7017 and the like.
7019 This ignores a couple of things like the dedication status of
7020 window, combination_limit and the like. This might have to be
7021 fixed. */
7023 bool
7024 compare_window_configurations (Lisp_Object configuration1,
7025 Lisp_Object configuration2,
7026 bool ignore_positions)
7028 register struct save_window_data *d1, *d2;
7029 struct Lisp_Vector *sws1, *sws2;
7030 ptrdiff_t i;
7032 CHECK_WINDOW_CONFIGURATION (configuration1);
7033 CHECK_WINDOW_CONFIGURATION (configuration2);
7035 d1 = (struct save_window_data *) XVECTOR (configuration1);
7036 d2 = (struct save_window_data *) XVECTOR (configuration2);
7037 sws1 = XVECTOR (d1->saved_windows);
7038 sws2 = XVECTOR (d2->saved_windows);
7040 /* Frame settings must match. */
7041 if (d1->frame_cols != d2->frame_cols
7042 || d1->frame_lines != d2->frame_lines
7043 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
7044 || !EQ (d1->selected_frame, d2->selected_frame)
7045 || !EQ (d1->current_buffer, d2->current_buffer)
7046 || (!ignore_positions
7047 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
7048 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
7049 || !EQ (d1->focus_frame, d2->focus_frame)
7050 /* Verify that the two configurations have the same number of windows. */
7051 || sws1->header.size != sws2->header.size)
7052 return 0;
7054 for (i = 0; i < sws1->header.size; i++)
7056 struct saved_window *sw1, *sw2;
7058 sw1 = SAVED_WINDOW_N (sws1, i);
7059 sw2 = SAVED_WINDOW_N (sws2, i);
7061 if (
7062 /* The "current" windows in the two configurations must
7063 correspond to each other. */
7064 EQ (d1->current_window, sw1->window)
7065 != EQ (d2->current_window, sw2->window)
7066 /* Windows' buffers must match. */
7067 || !EQ (sw1->buffer, sw2->buffer)
7068 || !EQ (sw1->pixel_left, sw2->pixel_left)
7069 || !EQ (sw1->pixel_top, sw2->pixel_top)
7070 || !EQ (sw1->pixel_height, sw2->pixel_height)
7071 || !EQ (sw1->pixel_width, sw2->pixel_width)
7072 || !EQ (sw1->left_col, sw2->left_col)
7073 || !EQ (sw1->top_line, sw2->top_line)
7074 || !EQ (sw1->total_cols, sw2->total_cols)
7075 || !EQ (sw1->total_lines, sw2->total_lines)
7076 || !EQ (sw1->display_table, sw2->display_table)
7077 /* The next two disjuncts check the window structure for
7078 equality. */
7079 || !EQ (sw1->parent, sw2->parent)
7080 || !EQ (sw1->prev, sw2->prev)
7081 || (!ignore_positions
7082 && (!EQ (sw1->hscroll, sw2->hscroll)
7083 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
7084 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
7085 || NILP (Fequal (sw1->start, sw2->start))
7086 || NILP (Fequal (sw1->pointm, sw2->pointm))
7087 || NILP (Fequal (sw1->mark, sw2->mark))))
7088 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
7089 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
7090 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
7091 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
7092 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
7093 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
7094 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
7095 return 0;
7098 return 1;
7101 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7102 Scompare_window_configurations, 2, 2, 0,
7103 doc: /* Compare two window configurations as regards the structure of windows.
7104 This function ignores details such as the values of point and mark
7105 and scrolling positions. */)
7106 (Lisp_Object x, Lisp_Object y)
7108 if (compare_window_configurations (x, y, 1))
7109 return Qt;
7110 return Qnil;
7113 void
7114 init_window_once (void)
7116 struct frame *f = make_initial_frame ();
7117 XSETFRAME (selected_frame, f);
7118 Vterminal_frame = selected_frame;
7119 minibuf_window = f->minibuffer_window;
7120 selected_window = f->selected_window;
7122 window_initialized = 1;
7125 void
7126 init_window (void)
7128 Vwindow_list = Qnil;
7131 void
7132 syms_of_window (void)
7134 DEFSYM (Qscroll_up, "scroll-up");
7135 DEFSYM (Qscroll_down, "scroll-down");
7136 DEFSYM (Qscroll_command, "scroll-command");
7138 Fput (Qscroll_up, Qscroll_command, Qt);
7139 Fput (Qscroll_down, Qscroll_command, Qt);
7141 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
7142 DEFSYM (Qwindowp, "windowp");
7143 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
7144 DEFSYM (Qwindow_live_p, "window-live-p");
7145 DEFSYM (Qwindow_valid_p, "window-valid-p");
7146 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
7147 DEFSYM (Qdelete_window, "delete-window");
7148 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
7149 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
7150 DEFSYM (Qwindow_pixel_to_total, "window--pixel-to-total");
7151 DEFSYM (Qsafe, "safe");
7152 DEFSYM (Qdisplay_buffer, "display-buffer");
7153 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
7154 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
7155 DEFSYM (Qget_mru_window, "get-mru-window");
7156 DEFSYM (Qwindow_size, "window-size");
7157 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
7158 DEFSYM (Qabove, "above");
7159 DEFSYM (Qbelow, "below");
7160 DEFSYM (Qclone_of, "clone-of");
7161 DEFSYM (Qfloor, "floor");
7162 DEFSYM (Qceiling, "ceiling");
7164 staticpro (&Vwindow_list);
7166 minibuf_selected_window = Qnil;
7167 staticpro (&minibuf_selected_window);
7169 window_scroll_pixel_based_preserve_x = -1;
7170 window_scroll_pixel_based_preserve_y = -1;
7171 window_scroll_preserve_hpos = -1;
7172 window_scroll_preserve_vpos = -1;
7174 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7175 doc: /* Non-nil means call as function to display a help buffer.
7176 The function is called with one argument, the buffer to be displayed.
7177 Used by `with-output-to-temp-buffer'.
7178 If this function is used, then it must do the entire job of showing
7179 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7180 Vtemp_buffer_show_function = Qnil;
7182 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7183 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7184 Vminibuf_scroll_window = Qnil;
7186 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7187 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7188 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7189 is displayed in the `mode-line' face. */);
7190 mode_line_in_non_selected_windows = 1;
7192 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7193 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7194 Vother_window_scroll_buffer = Qnil;
7196 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7197 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7198 auto_window_vscroll_p = 1;
7200 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7201 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
7202 next_screen_context_lines = 2;
7204 DEFVAR_LISP ("scroll-preserve-screen-position",
7205 Vscroll_preserve_screen_position,
7206 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
7207 A value of nil means point does not keep its screen position except
7208 at the scroll margin or window boundary respectively.
7209 A value of t means point keeps its screen position if the scroll
7210 command moved it vertically out of the window, e.g. when scrolling
7211 by full screens.
7212 Any other value means point always keeps its screen position.
7213 Scroll commands should have the `scroll-command' property
7214 on their symbols to be controlled by this variable. */);
7215 Vscroll_preserve_screen_position = Qnil;
7217 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7218 doc: /* Type of marker to use for `window-point'. */);
7219 Vwindow_point_insertion_type = Qnil;
7221 DEFVAR_LISP ("window-configuration-change-hook",
7222 Vwindow_configuration_change_hook,
7223 doc: /* Functions to call when window configuration changes.
7224 The buffer-local part is run once per window, with the relevant window
7225 selected; while the global part is run only once for the modified frame,
7226 with the relevant frame selected. */);
7227 Vwindow_configuration_change_hook = Qnil;
7229 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7230 doc: /* Non-nil means `recenter' redraws entire frame.
7231 If this option is non-nil, then the `recenter' command with a nil
7232 argument will redraw the entire frame; the special value `tty' causes
7233 the frame to be redrawn only if it is a tty frame. */);
7234 Vrecenter_redisplay = Qtty;
7236 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
7237 doc: /* If t, resize window combinations proportionally.
7238 If this variable is nil, splitting a window gets the entire screen space
7239 for displaying the new window from the window to split. Deleting and
7240 resizing a window preferably resizes one adjacent window only.
7242 If this variable is t, splitting a window tries to get the space
7243 proportionally from all windows in the same combination. This also
7244 allows to split a window that is otherwise too small or of fixed size.
7245 Resizing and deleting a window proportionally resize all windows in the
7246 same combination.
7248 Other values are reserved for future use.
7250 This variable takes no effect if the variable `window-combination-limit' is
7251 non-nil. */);
7252 Vwindow_combination_resize = Qnil;
7254 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
7255 doc: /* If non-nil, splitting a window makes a new parent window.
7256 The following values are recognized:
7258 nil means splitting a window will create a new parent window only if the
7259 window has no parent window or the window shall become part of a
7260 combination orthogonal to the one it is part of.
7262 `window-size' means that splitting a window for displaying a buffer
7263 makes a new parent window provided `display-buffer' is supposed to
7264 explicitly set the window's size due to the presence of a
7265 `window-height' or `window-width' entry in the alist used by
7266 `display-buffer'. Otherwise, this value is handled like nil.
7268 `temp-buffer' means that splitting a window for displaying a temporary
7269 buffer always makes a new parent window. Otherwise, this value is
7270 handled like nil.
7272 `display-buffer' means that splitting a window for displaying a buffer
7273 always makes a new parent window. Since temporary buffers are
7274 displayed by the function `display-buffer', this value is stronger
7275 than `temp-buffer'. Splitting a window for other purpose makes a
7276 new parent window only if needed.
7278 t means that splitting a window always creates a new parent window. If
7279 all splits behave this way, each frame's window tree is a binary
7280 tree and every window but the frame's root window has exactly one
7281 sibling.
7283 Other values are reserved for future use. */);
7284 Vwindow_combination_limit = Qwindow_size;
7286 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
7287 doc: /* Alist of persistent window parameters.
7288 This alist specifies which window parameters shall get saved by
7289 `current-window-configuration' and `window-state-get' and subsequently
7290 restored to their previous values by `set-window-configuration' and
7291 `window-state-put'.
7293 The car of each entry of this alist is the symbol specifying the
7294 parameter. The cdr is one of the following:
7296 nil means the parameter is neither saved by `window-state-get' nor by
7297 `current-window-configuration'.
7299 t means the parameter is saved by `current-window-configuration' and,
7300 provided its WRITABLE argument is nil, by `window-state-get'.
7302 The symbol `writable' means the parameter is saved unconditionally by
7303 both `current-window-configuration' and `window-state-get'. Do not use
7304 this value for parameters without read syntax (like windows or frames).
7306 Parameters not saved by `current-window-configuration' or
7307 `window-state-get' are left alone by `set-window-configuration'
7308 respectively are not installed by `window-state-put'. */);
7309 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
7311 DEFVAR_BOOL ("window-resize-pixelwise", window_resize_pixelwise,
7312 doc: /* Non-nil means resize windows pixelwise.
7313 This currently affects the functions: `split-window', `maximize-window',
7314 `minimize-window', `fit-window-to-buffer' and `fit-frame-to-buffer', and
7315 all functions that symmetrically resize a parent window.
7317 Note that when a frame's pixel size is not a multiple of the
7318 frame's character size, at least one window may get resized
7319 pixelwise even if this option is nil. */);
7320 window_resize_pixelwise = 0;
7322 defsubr (&Sselected_window);
7323 defsubr (&Sminibuffer_window);
7324 defsubr (&Swindow_minibuffer_p);
7325 defsubr (&Swindowp);
7326 defsubr (&Swindow_valid_p);
7327 defsubr (&Swindow_live_p);
7328 defsubr (&Swindow_frame);
7329 defsubr (&Sframe_root_window);
7330 defsubr (&Sframe_first_window);
7331 defsubr (&Sframe_selected_window);
7332 defsubr (&Sset_frame_selected_window);
7333 defsubr (&Spos_visible_in_window_p);
7334 defsubr (&Swindow_line_height);
7335 defsubr (&Swindow_buffer);
7336 defsubr (&Swindow_parent);
7337 defsubr (&Swindow_top_child);
7338 defsubr (&Swindow_left_child);
7339 defsubr (&Swindow_next_sibling);
7340 defsubr (&Swindow_prev_sibling);
7341 defsubr (&Swindow_combination_limit);
7342 defsubr (&Sset_window_combination_limit);
7343 defsubr (&Swindow_use_time);
7344 defsubr (&Swindow_pixel_width);
7345 defsubr (&Swindow_pixel_height);
7346 defsubr (&Swindow_total_width);
7347 defsubr (&Swindow_total_height);
7348 defsubr (&Swindow_normal_size);
7349 defsubr (&Swindow_new_pixel);
7350 defsubr (&Swindow_new_total);
7351 defsubr (&Swindow_new_normal);
7352 defsubr (&Swindow_pixel_left);
7353 defsubr (&Swindow_pixel_top);
7354 defsubr (&Swindow_left_column);
7355 defsubr (&Swindow_top_line);
7356 defsubr (&Sset_window_new_pixel);
7357 defsubr (&Sset_window_new_total);
7358 defsubr (&Sset_window_new_normal);
7359 defsubr (&Swindow_resize_apply);
7360 defsubr (&Swindow_resize_apply_total);
7361 defsubr (&Swindow_body_height);
7362 defsubr (&Swindow_body_width);
7363 defsubr (&Swindow_hscroll);
7364 defsubr (&Sset_window_hscroll);
7365 defsubr (&Swindow_redisplay_end_trigger);
7366 defsubr (&Sset_window_redisplay_end_trigger);
7367 defsubr (&Swindow_edges);
7368 defsubr (&Swindow_pixel_edges);
7369 defsubr (&Swindow_absolute_pixel_edges);
7370 defsubr (&Swindow_mode_line_height);
7371 defsubr (&Swindow_header_line_height);
7372 defsubr (&Swindow_right_divider_width);
7373 defsubr (&Swindow_bottom_divider_width);
7374 defsubr (&Swindow_scroll_bar_width);
7375 defsubr (&Swindow_inside_edges);
7376 defsubr (&Swindow_inside_pixel_edges);
7377 defsubr (&Swindow_inside_absolute_pixel_edges);
7378 defsubr (&Scoordinates_in_window_p);
7379 defsubr (&Swindow_at);
7380 defsubr (&Swindow_point);
7381 defsubr (&Swindow_start);
7382 defsubr (&Swindow_end);
7383 defsubr (&Sset_window_point);
7384 defsubr (&Sset_window_start);
7385 defsubr (&Swindow_dedicated_p);
7386 defsubr (&Sset_window_dedicated_p);
7387 defsubr (&Swindow_display_table);
7388 defsubr (&Sset_window_display_table);
7389 defsubr (&Snext_window);
7390 defsubr (&Sprevious_window);
7391 defsubr (&Sget_buffer_window);
7392 defsubr (&Sdelete_other_windows_internal);
7393 defsubr (&Sdelete_window_internal);
7394 defsubr (&Sresize_mini_window_internal);
7395 defsubr (&Sset_window_buffer);
7396 defsubr (&Srun_window_configuration_change_hook);
7397 defsubr (&Srun_window_scroll_functions);
7398 defsubr (&Sselect_window);
7399 defsubr (&Sforce_window_update);
7400 defsubr (&Ssplit_window_internal);
7401 defsubr (&Sscroll_up);
7402 defsubr (&Sscroll_down);
7403 defsubr (&Sscroll_left);
7404 defsubr (&Sscroll_right);
7405 defsubr (&Sother_window_for_scrolling);
7406 defsubr (&Sscroll_other_window);
7407 defsubr (&Sminibuffer_selected_window);
7408 defsubr (&Srecenter);
7409 defsubr (&Swindow_text_width);
7410 defsubr (&Swindow_text_height);
7411 defsubr (&Smove_to_window_line);
7412 defsubr (&Swindow_configuration_p);
7413 defsubr (&Swindow_configuration_frame);
7414 defsubr (&Sset_window_configuration);
7415 defsubr (&Scurrent_window_configuration);
7416 defsubr (&Sset_window_margins);
7417 defsubr (&Swindow_margins);
7418 defsubr (&Sset_window_fringes);
7419 defsubr (&Swindow_fringes);
7420 defsubr (&Sset_window_scroll_bars);
7421 defsubr (&Swindow_scroll_bars);
7422 defsubr (&Swindow_vscroll);
7423 defsubr (&Sset_window_vscroll);
7424 defsubr (&Scompare_window_configurations);
7425 defsubr (&Swindow_list);
7426 defsubr (&Swindow_list_1);
7427 defsubr (&Swindow_prev_buffers);
7428 defsubr (&Sset_window_prev_buffers);
7429 defsubr (&Swindow_next_buffers);
7430 defsubr (&Sset_window_next_buffers);
7431 defsubr (&Swindow_parameters);
7432 defsubr (&Swindow_parameter);
7433 defsubr (&Sset_window_parameter);
7436 void
7437 keys_of_window (void)
7439 initial_define_key (control_x_map, '<', "scroll-left");
7440 initial_define_key (control_x_map, '>', "scroll-right");
7442 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7443 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7444 initial_define_key (meta_map, 'v', "scroll-down-command");